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.CSharp {
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 // Disable control flow analysis while resolving the expression.
60 // This is used when resolving the instance expression of a field expression.
61 DisableFlowAnalysis = 16
65 // This is just as a hint to AddressOf of what will be done with the
68 public enum AddressOp {
75 /// This interface is implemented by variables
77 public interface IMemoryLocation {
79 /// The AddressOf method should generate code that loads
80 /// the address of the object and leaves it on the stack.
82 /// The `mode' argument is used to notify the expression
83 /// of whether this will be used to read from the address or
84 /// write to the address.
86 /// This is just a hint that can be used to provide good error
87 /// reporting, and should have no other side effects.
89 void AddressOf (EmitContext ec, AddressOp mode);
93 /// This interface is implemented by variables
95 public interface IVariable {
97 /// Checks whether the variable has already been assigned at
98 /// the current position of the method's control flow and
99 /// reports an appropriate error message if not.
101 /// If the variable is a struct, then this call checks whether
102 /// all of its fields (including all private ones) have been
105 bool IsAssigned (EmitContext ec, Location loc);
108 /// Checks whether field `name' in this struct has been assigned.
110 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
113 /// Tells the flow analysis code that the variable has already
114 /// been assigned at the current code position.
116 /// If the variable is a struct, this call marks all its fields
117 /// (including private fields) as being assigned.
119 void SetAssigned (EmitContext ec);
122 /// Tells the flow analysis code that field `name' in this struct
123 /// has already been assigned atthe current code position.
125 void SetFieldAssigned (EmitContext ec, string name);
129 /// This interface denotes an expression which evaluates to a member
130 /// of a struct or a class.
132 public interface IMemberExpr
135 /// The name of this member.
142 /// Whether this is an instance member.
149 /// Whether this is a static member.
156 /// The instance expression associated with this member, if it's a
157 /// non-static member.
159 Expression InstanceExpression {
165 /// Base class for expressions
167 public abstract class Expression {
168 public ExprClass eclass;
170 protected Location loc;
182 public Location Location {
189 /// Utility wrapper routine for Error, just to beautify the code
191 public void Error (int error, string s)
193 if (!Location.IsNull (loc))
194 Report.Error (error, loc, s);
196 Report.Error (error, s);
200 /// Utility wrapper routine for Warning, just to beautify the code
202 public void Warning (int warning, string s)
204 if (!Location.IsNull (loc))
205 Report.Warning (warning, loc, s);
207 Report.Warning (warning, s);
211 /// Utility wrapper routine for Warning, only prints the warning if
212 /// warnings of level `level' are enabled.
214 public void Warning (int warning, int level, string s)
216 if (level <= RootContext.WarningLevel)
217 Warning (warning, s);
220 static public void Error_CannotConvertType (Location loc, Type source, Type target)
222 Report.Error (30, loc, "Cannot convert type '" +
223 TypeManager.CSharpName (source) + "' to '" +
224 TypeManager.CSharpName (target) + "'");
228 /// Performs semantic analysis on the Expression
232 /// The Resolve method is invoked to perform the semantic analysis
235 /// The return value is an expression (it can be the
236 /// same expression in some cases) or a new
237 /// expression that better represents this node.
239 /// For example, optimizations of Unary (LiteralInt)
240 /// would return a new LiteralInt with a negated
243 /// If there is an error during semantic analysis,
244 /// then an error should be reported (using Report)
245 /// and a null value should be returned.
247 /// There are two side effects expected from calling
248 /// Resolve(): the the field variable "eclass" should
249 /// be set to any value of the enumeration
250 /// `ExprClass' and the type variable should be set
251 /// to a valid type (this is the type of the
254 public abstract Expression DoResolve (EmitContext ec);
256 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
258 return DoResolve (ec);
262 /// Resolves an expression and performs semantic analysis on it.
266 /// Currently Resolve wraps DoResolve to perform sanity
267 /// checking and assertion checking on what we expect from Resolve.
269 public Expression Resolve (EmitContext ec, ResolveFlags flags)
273 bool old_do_flow_analysis = ec.DoFlowAnalysis;
274 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
275 ec.DoFlowAnalysis = false;
277 if (this is SimpleName)
278 e = ((SimpleName) this).DoResolveAllowStatic (ec);
282 ec.DoFlowAnalysis = old_do_flow_analysis;
287 if (e is SimpleName){
288 SimpleName s = (SimpleName) e;
290 if ((flags & ResolveFlags.SimpleName) == 0) {
293 "The name `" + s.Name + "' could not be found in `" +
294 ec.DeclSpace.Name + "'");
301 if ((e is TypeExpr) || (e is ComposedCast)) {
302 if ((flags & ResolveFlags.Type) == 0) {
312 if ((flags & ResolveFlags.VariableOrValue) == 0) {
318 case ExprClass.MethodGroup:
319 if ((flags & ResolveFlags.MethodGroup) == 0) {
320 ((MethodGroupExpr) e).ReportUsageError ();
325 case ExprClass.Value:
326 case ExprClass.Variable:
327 case ExprClass.PropertyAccess:
328 case ExprClass.EventAccess:
329 case ExprClass.IndexerAccess:
330 if ((flags & ResolveFlags.VariableOrValue) == 0) {
337 throw new Exception ("Expression " + e.GetType () +
338 " ExprClass is Invalid after resolve");
342 throw new Exception (
343 "Expression " + e.GetType () +
344 " did not set its type after Resolve\n" +
345 "called from: " + this.GetType ());
351 /// Resolves an expression and performs semantic analysis on it.
353 public Expression Resolve (EmitContext ec)
355 return Resolve (ec, ResolveFlags.VariableOrValue);
359 /// Resolves an expression for LValue assignment
363 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
364 /// checking and assertion checking on what we expect from Resolve
366 public Expression ResolveLValue (EmitContext ec, Expression right_side)
368 Expression e = DoResolveLValue (ec, right_side);
371 if (e is SimpleName){
372 SimpleName s = (SimpleName) e;
376 "The name `" + s.Name + "' could not be found in `" +
377 ec.DeclSpace.Name + "'");
381 if (e.eclass == ExprClass.Invalid)
382 throw new Exception ("Expression " + e +
383 " ExprClass is Invalid after resolve");
385 if (e.eclass == ExprClass.MethodGroup) {
386 ((MethodGroupExpr) e).ReportUsageError ();
391 throw new Exception ("Expression " + e +
392 " did not set its type after Resolve");
399 /// Emits the code for the expression
403 /// The Emit method is invoked to generate the code
404 /// for the expression.
406 public abstract void Emit (EmitContext ec);
409 /// Protected constructor. Only derivate types should
410 /// be able to be created
413 protected Expression ()
415 eclass = ExprClass.Invalid;
420 /// Returns a literalized version of a literal FieldInfo
424 /// The possible return values are:
425 /// IntConstant, UIntConstant
426 /// LongLiteral, ULongConstant
427 /// FloatConstant, DoubleConstant
430 /// The value returned is already resolved.
432 public static Constant Constantify (object v, Type t)
434 if (t == TypeManager.int32_type)
435 return new IntConstant ((int) v);
436 else if (t == TypeManager.uint32_type)
437 return new UIntConstant ((uint) v);
438 else if (t == TypeManager.int64_type)
439 return new LongConstant ((long) v);
440 else if (t == TypeManager.uint64_type)
441 return new ULongConstant ((ulong) v);
442 else if (t == TypeManager.float_type)
443 return new FloatConstant ((float) v);
444 else if (t == TypeManager.double_type)
445 return new DoubleConstant ((double) v);
446 else if (t == TypeManager.string_type)
447 return new StringConstant ((string) v);
448 else if (t == TypeManager.short_type)
449 return new ShortConstant ((short)v);
450 else if (t == TypeManager.ushort_type)
451 return new UShortConstant ((ushort)v);
452 else if (t == TypeManager.sbyte_type)
453 return new SByteConstant (((sbyte)v));
454 else if (t == TypeManager.byte_type)
455 return new ByteConstant ((byte)v);
456 else if (t == TypeManager.char_type)
457 return new CharConstant ((char)v);
458 else if (t == TypeManager.bool_type)
459 return new BoolConstant ((bool) v);
460 else if (TypeManager.IsEnumType (t)){
461 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
463 return new EnumConstant (e, t);
465 throw new Exception ("Unknown type for constant (" + t +
470 /// Returns a fully formed expression after a MemberLookup
472 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
475 return new EventExpr ((EventInfo) mi, loc);
476 else if (mi is FieldInfo)
477 return new FieldExpr ((FieldInfo) mi, loc);
478 else if (mi is PropertyInfo)
479 return new PropertyExpr ((PropertyInfo) mi, loc);
480 else if (mi is Type){
481 return new TypeExpr ((System.Type) mi, loc);
488 // FIXME: Probably implement a cache for (t,name,current_access_set)?
490 // This code could use some optimizations, but we need to do some
491 // measurements. For example, we could use a delegate to `flag' when
492 // something can not any longer be a method-group (because it is something
496 // If the return value is an Array, then it is an array of
499 // If the return value is an MemberInfo, it is anything, but a Method
503 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
504 // the arguments here and have MemberLookup return only the methods that
505 // match the argument count/type, unlike we are doing now (we delay this
508 // This is so we can catch correctly attempts to invoke instance methods
509 // from a static body (scan for error 120 in ResolveSimpleName).
512 // FIXME: Potential optimization, have a static ArrayList
515 public static Expression MemberLookup (EmitContext ec, Type t, string name,
516 MemberTypes mt, BindingFlags bf, Location loc)
518 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
521 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
522 string name, MemberTypes mt, BindingFlags bf,
525 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
530 int count = mi.Length;
533 return new MethodGroupExpr (mi, loc);
535 if (mi [0] is MethodBase)
536 return new MethodGroupExpr (mi, loc);
538 return ExprClassFromMemberInfo (ec, mi [0], loc);
541 public const MemberTypes AllMemberTypes =
542 MemberTypes.Constructor |
546 MemberTypes.NestedType |
547 MemberTypes.Property;
549 public const BindingFlags AllBindingFlags =
550 BindingFlags.Public |
551 BindingFlags.Static |
552 BindingFlags.Instance;
554 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
556 return MemberLookup (ec, ec.ContainerType, t, name,
557 AllMemberTypes, AllBindingFlags, loc);
560 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
562 return MemberLookup (ec, ec.ContainerType, t, name,
563 MemberTypes.Method, AllBindingFlags, loc);
567 /// This is a wrapper for MemberLookup that is not used to "probe", but
568 /// to find a final definition. If the final definition is not found, we
569 /// look for private members and display a useful debugging message if we
572 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
575 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
578 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
579 MemberTypes mt, BindingFlags bf, Location loc)
583 int errors = Report.Errors;
585 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
590 // Error has already been reported.
591 if (errors < Report.Errors)
594 e = MemberLookup (ec, t, name, AllMemberTypes,
595 AllBindingFlags | BindingFlags.NonPublic, loc);
598 117, loc, "`" + t + "' does not contain a definition " +
599 "for `" + name + "'");
602 122, loc, "`" + t + "." + name +
603 "' is inaccessible due to its protection level");
609 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
611 EventInfo ei = event_expr.EventInfo;
613 return TypeManager.GetPrivateFieldOfEvent (ei);
616 static EmptyExpression MyEmptyExpr;
617 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
619 Type expr_type = expr.Type;
621 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
622 // if we are a method group, emit a warning
627 if (target_type == TypeManager.object_type) {
629 // A pointer type cannot be converted to object
631 if (expr_type.IsPointer)
634 if (expr_type.IsValueType)
635 return new BoxedCast (expr);
636 if (expr_type.IsClass || expr_type.IsInterface)
637 return new EmptyCast (expr, target_type);
638 } else if (expr_type.IsSubclassOf (target_type)) {
639 return new EmptyCast (expr, target_type);
642 // This code is kind of mirrored inside StandardConversionExists
643 // with the small distinction that we only probe there
645 // Always ensure that the code here and there is in sync
647 // from the null type to any reference-type.
648 if (expr is NullLiteral && !target_type.IsValueType)
649 return new EmptyCast (expr, target_type);
651 // from any class-type S to any interface-type T.
652 if (expr_type.IsClass && target_type.IsInterface) {
653 if (TypeManager.ImplementsInterface (expr_type, target_type))
654 return new EmptyCast (expr, target_type);
659 // from any interface type S to interface-type T.
660 if (expr_type.IsInterface && target_type.IsInterface) {
662 if (TypeManager.ImplementsInterface (expr_type, target_type))
663 return new EmptyCast (expr, target_type);
668 // from an array-type S to an array-type of type T
669 if (expr_type.IsArray && target_type.IsArray) {
670 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
672 Type expr_element_type = expr_type.GetElementType ();
674 if (MyEmptyExpr == null)
675 MyEmptyExpr = new EmptyExpression ();
677 MyEmptyExpr.SetType (expr_element_type);
678 Type target_element_type = target_type.GetElementType ();
680 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
681 if (StandardConversionExists (MyEmptyExpr,
682 target_element_type))
683 return new EmptyCast (expr, target_type);
688 // from an array-type to System.Array
689 if (expr_type.IsArray && target_type == TypeManager.array_type)
690 return new EmptyCast (expr, target_type);
692 // from any delegate type to System.Delegate
693 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
694 target_type == TypeManager.delegate_type)
695 return new EmptyCast (expr, target_type);
697 // from any array-type or delegate type into System.ICloneable.
698 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
699 if (target_type == TypeManager.icloneable_type)
700 return new EmptyCast (expr, target_type);
710 /// Handles expressions like this: decimal d; d = 1;
711 /// and changes them into: decimal d; d = new System.Decimal (1);
713 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
715 ArrayList args = new ArrayList ();
717 args.Add (new Argument (expr, Argument.AType.Expression));
719 Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
721 return ne.Resolve (ec);
725 /// Implicit Numeric Conversions.
727 /// expr is the expression to convert, returns a new expression of type
728 /// target_type or null if an implicit conversion is not possible.
730 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
731 Type target_type, Location loc)
733 Type expr_type = expr.Type;
736 // Attempt to do the implicit constant expression conversions
738 if (expr is IntConstant){
741 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
745 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
747 // Try the implicit constant expression conversion
748 // from long to ulong, instead of a nice routine,
751 long v = ((LongConstant) expr).Value;
753 return new ULongConstant ((ulong) v);
757 // If we have an enumeration, extract the underlying type,
758 // use this during the comparission, but wrap around the original
761 Type real_target_type = target_type;
763 if (TypeManager.IsEnumType (real_target_type))
764 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
766 if (expr_type == real_target_type)
767 return new EmptyCast (expr, target_type);
769 if (expr_type == TypeManager.sbyte_type){
771 // From sbyte to short, int, long, float, double.
773 if (real_target_type == TypeManager.int32_type)
774 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
775 if (real_target_type == TypeManager.int64_type)
776 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
777 if (real_target_type == TypeManager.double_type)
778 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
779 if (real_target_type == TypeManager.float_type)
780 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
781 if (real_target_type == TypeManager.short_type)
782 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
783 if (real_target_type == TypeManager.decimal_type)
784 return InternalTypeConstructor (ec, expr, target_type);
785 } else if (expr_type == TypeManager.byte_type){
787 // From byte to short, ushort, int, uint, long, ulong, float, double
789 if ((real_target_type == TypeManager.short_type) ||
790 (real_target_type == TypeManager.ushort_type) ||
791 (real_target_type == TypeManager.int32_type) ||
792 (real_target_type == TypeManager.uint32_type))
793 return new EmptyCast (expr, target_type);
795 if (real_target_type == TypeManager.uint64_type)
796 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
797 if (real_target_type == TypeManager.int64_type)
798 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
799 if (real_target_type == TypeManager.float_type)
800 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
801 if (real_target_type == TypeManager.double_type)
802 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
803 if (real_target_type == TypeManager.decimal_type)
804 return InternalTypeConstructor (ec, expr, target_type);
805 } else if (expr_type == TypeManager.short_type){
807 // From short to int, long, float, double
809 if (real_target_type == TypeManager.int32_type)
810 return new EmptyCast (expr, target_type);
811 if (real_target_type == TypeManager.int64_type)
812 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
813 if (real_target_type == TypeManager.double_type)
814 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
815 if (real_target_type == TypeManager.float_type)
816 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
817 if (real_target_type == TypeManager.decimal_type)
818 return InternalTypeConstructor (ec, expr, target_type);
819 } else if (expr_type == TypeManager.ushort_type){
821 // From ushort to int, uint, long, ulong, float, double
823 if (real_target_type == TypeManager.uint32_type)
824 return new EmptyCast (expr, target_type);
826 if (real_target_type == TypeManager.uint64_type)
827 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
828 if (real_target_type == TypeManager.int32_type)
829 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
830 if (real_target_type == TypeManager.int64_type)
831 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
832 if (real_target_type == TypeManager.double_type)
833 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
834 if (real_target_type == TypeManager.float_type)
835 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
836 if (real_target_type == TypeManager.decimal_type)
837 return InternalTypeConstructor (ec, expr, target_type);
838 } else if (expr_type == TypeManager.int32_type){
840 // From int to long, float, double
842 if (real_target_type == TypeManager.int64_type)
843 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
844 if (real_target_type == TypeManager.double_type)
845 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
846 if (real_target_type == TypeManager.float_type)
847 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
848 if (real_target_type == TypeManager.decimal_type)
849 return InternalTypeConstructor (ec, expr, target_type);
850 } else if (expr_type == TypeManager.uint32_type){
852 // From uint to long, ulong, float, double
854 if (real_target_type == TypeManager.int64_type)
855 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
856 if (real_target_type == TypeManager.uint64_type)
857 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
858 if (real_target_type == TypeManager.double_type)
859 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
861 if (real_target_type == TypeManager.float_type)
862 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
864 if (real_target_type == TypeManager.decimal_type)
865 return InternalTypeConstructor (ec, expr, target_type);
866 } else if (expr_type == TypeManager.int64_type){
868 // From long/ulong to float, double
870 if (real_target_type == TypeManager.double_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
872 if (real_target_type == TypeManager.float_type)
873 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
874 if (real_target_type == TypeManager.decimal_type)
875 return InternalTypeConstructor (ec, expr, target_type);
876 } else if (expr_type == TypeManager.uint64_type){
878 // From ulong to float, double
880 if (real_target_type == TypeManager.double_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
883 if (real_target_type == TypeManager.float_type)
884 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
886 if (real_target_type == TypeManager.decimal_type)
887 return InternalTypeConstructor (ec, expr, target_type);
888 } else if (expr_type == TypeManager.char_type){
890 // From char to ushort, int, uint, long, ulong, float, double
892 if ((real_target_type == TypeManager.ushort_type) ||
893 (real_target_type == TypeManager.int32_type) ||
894 (real_target_type == TypeManager.uint32_type))
895 return new EmptyCast (expr, target_type);
896 if (real_target_type == TypeManager.uint64_type)
897 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
898 if (real_target_type == TypeManager.int64_type)
899 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
900 if (real_target_type == TypeManager.float_type)
901 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
902 if (real_target_type == TypeManager.double_type)
903 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
904 if (real_target_type == TypeManager.decimal_type)
905 return InternalTypeConstructor (ec, expr, target_type);
906 } else if (expr_type == TypeManager.float_type){
910 if (real_target_type == TypeManager.double_type)
911 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
918 // Tests whether an implicit reference conversion exists between expr_type
921 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
923 Type expr_type = expr.Type;
926 // This is the boxed case.
928 if (target_type == TypeManager.object_type) {
929 if ((expr_type.IsClass) ||
930 (expr_type.IsValueType) ||
931 (expr_type.IsInterface))
934 } else if (expr_type.IsSubclassOf (target_type)) {
938 // Please remember that all code below actually comes
939 // from ImplicitReferenceConversion so make sure code remains in sync
941 // from any class-type S to any interface-type T.
942 if (expr_type.IsClass && target_type.IsInterface) {
943 if (TypeManager.ImplementsInterface (expr_type, target_type))
947 // from any interface type S to interface-type T.
948 if (expr_type.IsInterface && target_type.IsInterface)
949 if (TypeManager.ImplementsInterface (expr_type, target_type))
952 // from an array-type S to an array-type of type T
953 if (expr_type.IsArray && target_type.IsArray) {
954 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
956 Type expr_element_type = expr_type.GetElementType ();
958 if (MyEmptyExpr == null)
959 MyEmptyExpr = new EmptyExpression ();
961 MyEmptyExpr.SetType (expr_element_type);
962 Type target_element_type = target_type.GetElementType ();
964 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
965 if (StandardConversionExists (MyEmptyExpr,
966 target_element_type))
971 // from an array-type to System.Array
972 if (expr_type.IsArray && (target_type == TypeManager.array_type))
975 // from any delegate type to System.Delegate
976 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
977 target_type == TypeManager.delegate_type)
978 if (target_type.IsAssignableFrom (expr_type))
981 // from any array-type or delegate type into System.ICloneable.
982 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
983 if (target_type == TypeManager.icloneable_type)
986 // from the null type to any reference-type.
987 if (expr is NullLiteral && !target_type.IsValueType &&
988 !TypeManager.IsEnumType (target_type))
997 /// Same as StandardConversionExists except that it also looks at
998 /// implicit user defined conversions - needed for overload resolution
1000 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1002 if (StandardConversionExists (expr, target_type) == true)
1005 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1014 /// Determines if a standard implicit conversion exists from
1015 /// expr_type to target_type
1017 public static bool StandardConversionExists (Expression expr, Type target_type)
1019 Type expr_type = expr.Type;
1021 if (expr_type == target_type)
1024 // First numeric conversions
1026 if (expr_type == TypeManager.sbyte_type){
1028 // From sbyte to short, int, long, float, double.
1030 if ((target_type == TypeManager.int32_type) ||
1031 (target_type == TypeManager.int64_type) ||
1032 (target_type == TypeManager.double_type) ||
1033 (target_type == TypeManager.float_type) ||
1034 (target_type == TypeManager.short_type) ||
1035 (target_type == TypeManager.decimal_type))
1038 } else if (expr_type == TypeManager.byte_type){
1040 // From byte to short, ushort, int, uint, long, ulong, float, double
1042 if ((target_type == TypeManager.short_type) ||
1043 (target_type == TypeManager.ushort_type) ||
1044 (target_type == TypeManager.int32_type) ||
1045 (target_type == TypeManager.uint32_type) ||
1046 (target_type == TypeManager.uint64_type) ||
1047 (target_type == TypeManager.int64_type) ||
1048 (target_type == TypeManager.float_type) ||
1049 (target_type == TypeManager.double_type) ||
1050 (target_type == TypeManager.decimal_type))
1053 } else if (expr_type == TypeManager.short_type){
1055 // From short to int, long, float, double
1057 if ((target_type == TypeManager.int32_type) ||
1058 (target_type == TypeManager.int64_type) ||
1059 (target_type == TypeManager.double_type) ||
1060 (target_type == TypeManager.float_type) ||
1061 (target_type == TypeManager.decimal_type))
1064 } else if (expr_type == TypeManager.ushort_type){
1066 // From ushort to int, uint, long, ulong, float, double
1068 if ((target_type == TypeManager.uint32_type) ||
1069 (target_type == TypeManager.uint64_type) ||
1070 (target_type == TypeManager.int32_type) ||
1071 (target_type == TypeManager.int64_type) ||
1072 (target_type == TypeManager.double_type) ||
1073 (target_type == TypeManager.float_type) ||
1074 (target_type == TypeManager.decimal_type))
1077 } else if (expr_type == TypeManager.int32_type){
1079 // From int to long, float, double
1081 if ((target_type == TypeManager.int64_type) ||
1082 (target_type == TypeManager.double_type) ||
1083 (target_type == TypeManager.float_type) ||
1084 (target_type == TypeManager.decimal_type))
1087 } else if (expr_type == TypeManager.uint32_type){
1089 // From uint to long, ulong, float, double
1091 if ((target_type == TypeManager.int64_type) ||
1092 (target_type == TypeManager.uint64_type) ||
1093 (target_type == TypeManager.double_type) ||
1094 (target_type == TypeManager.float_type) ||
1095 (target_type == TypeManager.decimal_type))
1098 } else if ((expr_type == TypeManager.uint64_type) ||
1099 (expr_type == TypeManager.int64_type)) {
1101 // From long/ulong to float, double
1103 if ((target_type == TypeManager.double_type) ||
1104 (target_type == TypeManager.float_type) ||
1105 (target_type == TypeManager.decimal_type))
1108 } else if (expr_type == TypeManager.char_type){
1110 // From char to ushort, int, uint, long, ulong, float, double
1112 if ((target_type == TypeManager.ushort_type) ||
1113 (target_type == TypeManager.int32_type) ||
1114 (target_type == TypeManager.uint32_type) ||
1115 (target_type == TypeManager.uint64_type) ||
1116 (target_type == TypeManager.int64_type) ||
1117 (target_type == TypeManager.float_type) ||
1118 (target_type == TypeManager.double_type) ||
1119 (target_type == TypeManager.decimal_type))
1122 } else if (expr_type == TypeManager.float_type){
1126 if (target_type == TypeManager.double_type)
1130 if (ImplicitReferenceConversionExists (expr, target_type))
1133 if (expr is IntConstant){
1134 int value = ((IntConstant) expr).Value;
1136 if (target_type == TypeManager.sbyte_type){
1137 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1139 } else if (target_type == TypeManager.byte_type){
1140 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1142 } else if (target_type == TypeManager.short_type){
1143 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1145 } else if (target_type == TypeManager.ushort_type){
1146 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1148 } else if (target_type == TypeManager.uint32_type){
1151 } else if (target_type == TypeManager.uint64_type){
1153 // we can optimize this case: a positive int32
1154 // always fits on a uint64. But we need an opcode
1161 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1165 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1167 // Try the implicit constant expression conversion
1168 // from long to ulong, instead of a nice routine,
1169 // we just inline it
1171 long v = ((LongConstant) expr).Value;
1176 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1177 IntLiteral i = (IntLiteral) expr;
1186 // Used internally by FindMostEncompassedType, this is used
1187 // to avoid creating lots of objects in the tight loop inside
1188 // FindMostEncompassedType
1190 static EmptyExpression priv_fmet_param;
1193 /// Finds "most encompassed type" according to the spec (13.4.2)
1194 /// amongst the methods in the MethodGroupExpr
1196 static Type FindMostEncompassedType (ArrayList types)
1200 if (priv_fmet_param == null)
1201 priv_fmet_param = new EmptyExpression ();
1203 foreach (Type t in types){
1204 priv_fmet_param.SetType (t);
1211 if (StandardConversionExists (priv_fmet_param, best))
1219 // Used internally by FindMostEncompassingType, this is used
1220 // to avoid creating lots of objects in the tight loop inside
1221 // FindMostEncompassingType
1223 static EmptyExpression priv_fmee_ret;
1226 /// Finds "most encompassing type" according to the spec (13.4.2)
1227 /// amongst the types in the given set
1229 static Type FindMostEncompassingType (ArrayList types)
1233 if (priv_fmee_ret == null)
1234 priv_fmee_ret = new EmptyExpression ();
1236 foreach (Type t in types){
1237 priv_fmee_ret.SetType (best);
1244 if (StandardConversionExists (priv_fmee_ret, t))
1252 // Used to avoid creating too many objects
1254 static EmptyExpression priv_fms_expr;
1257 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1258 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1259 /// for explicit and implicit conversion operators.
1261 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1262 bool apply_explicit_conv_rules,
1265 ArrayList src_types_set = new ArrayList ();
1267 if (priv_fms_expr == null)
1268 priv_fms_expr = new EmptyExpression ();
1271 // If any operator converts from S then Sx = S
1273 foreach (MethodBase mb in me.Methods){
1274 ParameterData pd = Invocation.GetParameterData (mb);
1275 Type param_type = pd.ParameterType (0);
1277 if (param_type == source_type)
1280 if (apply_explicit_conv_rules) {
1283 // Find the set of applicable user-defined conversion operators, U. This set
1285 // user-defined implicit or explicit conversion operators declared by
1286 // the classes or structs in D that convert from a type encompassing
1287 // or encompassed by S to a type encompassing or encompassed by T
1289 priv_fms_expr.SetType (param_type);
1290 if (StandardConversionExists (priv_fms_expr, source_type))
1291 src_types_set.Add (param_type);
1293 priv_fms_expr.SetType (source_type);
1294 if (StandardConversionExists (priv_fms_expr, param_type))
1295 src_types_set.Add (param_type);
1299 // Only if S is encompassed by param_type
1301 priv_fms_expr.SetType (source_type);
1302 if (StandardConversionExists (priv_fms_expr, param_type))
1303 src_types_set.Add (param_type);
1308 // Explicit Conv rules
1310 if (apply_explicit_conv_rules) {
1311 ArrayList candidate_set = new ArrayList ();
1313 foreach (Type param_type in src_types_set){
1314 priv_fms_expr.SetType (source_type);
1316 if (StandardConversionExists (priv_fms_expr, param_type))
1317 candidate_set.Add (param_type);
1320 if (candidate_set.Count != 0)
1321 return FindMostEncompassedType (candidate_set);
1327 if (apply_explicit_conv_rules)
1328 return FindMostEncompassingType (src_types_set);
1330 return FindMostEncompassedType (src_types_set);
1334 // Useful in avoiding proliferation of objects
1336 static EmptyExpression priv_fmt_expr;
1339 /// Finds the most specific target Tx according to section 13.4.4
1341 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1342 bool apply_explicit_conv_rules,
1345 ArrayList tgt_types_set = new ArrayList ();
1347 if (priv_fmt_expr == null)
1348 priv_fmt_expr = new EmptyExpression ();
1351 // If any operator converts to T then Tx = T
1353 foreach (MethodInfo mi in me.Methods){
1354 Type ret_type = mi.ReturnType;
1356 if (ret_type == target)
1359 if (apply_explicit_conv_rules) {
1362 // Find the set of applicable user-defined conversion operators, U.
1364 // This set consists of the
1365 // user-defined implicit or explicit conversion operators declared by
1366 // the classes or structs in D that convert from a type encompassing
1367 // or encompassed by S to a type encompassing or encompassed by T
1369 priv_fms_expr.SetType (ret_type);
1370 if (StandardConversionExists (priv_fms_expr, target))
1371 tgt_types_set.Add (ret_type);
1373 priv_fms_expr.SetType (target);
1374 if (StandardConversionExists (priv_fms_expr, ret_type))
1375 tgt_types_set.Add (ret_type);
1379 // Only if T is encompassed by param_type
1381 priv_fms_expr.SetType (ret_type);
1382 if (StandardConversionExists (priv_fms_expr, target))
1383 tgt_types_set.Add (ret_type);
1388 // Explicit conv rules
1390 if (apply_explicit_conv_rules) {
1391 ArrayList candidate_set = new ArrayList ();
1393 foreach (Type ret_type in tgt_types_set){
1394 priv_fmt_expr.SetType (ret_type);
1396 if (StandardConversionExists (priv_fmt_expr, target))
1397 candidate_set.Add (ret_type);
1400 if (candidate_set.Count != 0)
1401 return FindMostEncompassingType (candidate_set);
1405 // Okay, final case !
1407 if (apply_explicit_conv_rules)
1408 return FindMostEncompassedType (tgt_types_set);
1410 return FindMostEncompassingType (tgt_types_set);
1414 /// User-defined Implicit conversions
1416 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1417 Type target, Location loc)
1419 return UserDefinedConversion (ec, source, target, loc, false);
1423 /// User-defined Explicit conversions
1425 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1426 Type target, Location loc)
1428 return UserDefinedConversion (ec, source, target, loc, true);
1432 /// Computes the MethodGroup for the user-defined conversion
1433 /// operators from source_type to target_type. `look_for_explicit'
1434 /// controls whether we should also include the list of explicit
1437 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1438 Type source_type, Type target_type,
1439 Location loc, bool look_for_explicit)
1441 Expression mg1 = null, mg2 = null;
1442 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1446 // FIXME : How does the False operator come into the picture ?
1447 // This doesn't look complete and very correct !
1449 if (target_type == TypeManager.bool_type && !look_for_explicit)
1450 op_name = "op_True";
1452 op_name = "op_Implicit";
1454 MethodGroupExpr union3;
1456 mg1 = MethodLookup (ec, source_type, op_name, loc);
1457 if (source_type.BaseType != null)
1458 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1461 union3 = (MethodGroupExpr) mg2;
1462 else if (mg2 == null)
1463 union3 = (MethodGroupExpr) mg1;
1465 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1467 mg1 = MethodLookup (ec, target_type, op_name, loc);
1470 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1472 union3 = (MethodGroupExpr) mg1;
1475 if (target_type.BaseType != null)
1476 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1480 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1482 union3 = (MethodGroupExpr) mg1;
1485 MethodGroupExpr union4 = null;
1487 if (look_for_explicit) {
1488 op_name = "op_Explicit";
1490 mg5 = MemberLookup (ec, source_type, op_name, loc);
1491 if (source_type.BaseType != null)
1492 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1494 mg7 = MemberLookup (ec, target_type, op_name, loc);
1495 if (target_type.BaseType != null)
1496 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1498 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1499 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1501 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1504 return Invocation.MakeUnionSet (union3, union4, loc);
1508 /// User-defined conversions
1510 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1511 Type target, Location loc,
1512 bool look_for_explicit)
1514 MethodGroupExpr union;
1515 Type source_type = source.Type;
1516 MethodBase method = null;
1518 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1522 Type most_specific_source, most_specific_target;
1525 foreach (MethodBase m in union.Methods){
1526 Console.WriteLine ("Name: " + m.Name);
1527 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1531 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1532 if (most_specific_source == null)
1535 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1536 if (most_specific_target == null)
1541 foreach (MethodBase mb in union.Methods){
1542 ParameterData pd = Invocation.GetParameterData (mb);
1543 MethodInfo mi = (MethodInfo) mb;
1545 if (pd.ParameterType (0) == most_specific_source &&
1546 mi.ReturnType == most_specific_target) {
1552 if (method == null || count > 1) {
1553 Report.Error (-11, loc, "Ambiguous user defined conversion");
1558 // This will do the conversion to the best match that we
1559 // found. Now we need to perform an implict standard conversion
1560 // if the best match was not the type that we were requested
1563 if (look_for_explicit)
1564 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1566 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1572 e = new UserCast ((MethodInfo) method, source, loc);
1573 if (e.Type != target){
1574 if (!look_for_explicit)
1575 e = ConvertImplicitStandard (ec, e, target, loc);
1577 e = ConvertExplicitStandard (ec, e, target, loc);
1583 /// Converts implicitly the resolved expression `expr' into the
1584 /// `target_type'. It returns a new expression that can be used
1585 /// in a context that expects a `target_type'.
1587 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1588 Type target_type, Location loc)
1590 Type expr_type = expr.Type;
1593 if (expr_type == target_type)
1596 if (target_type == null)
1597 throw new Exception ("Target type is null");
1599 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1603 e = ImplicitUserConversion (ec, expr, target_type, loc);
1612 /// Attempts to apply the `Standard Implicit
1613 /// Conversion' rules to the expression `expr' into
1614 /// the `target_type'. It returns a new expression
1615 /// that can be used in a context that expects a
1618 /// This is different from `ConvertImplicit' in that the
1619 /// user defined implicit conversions are excluded.
1621 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1622 Type target_type, Location loc)
1624 Type expr_type = expr.Type;
1627 if (expr_type == target_type)
1630 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1634 e = ImplicitReferenceConversion (expr, target_type);
1638 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1639 IntLiteral i = (IntLiteral) expr;
1642 return new EmptyCast (expr, target_type);
1646 if (expr_type.IsPointer){
1647 if (target_type == TypeManager.void_ptr_type)
1648 return new EmptyCast (expr, target_type);
1651 // yep, comparing pointer types cant be done with
1652 // t1 == t2, we have to compare their element types.
1654 if (target_type.IsPointer){
1655 if (target_type.GetElementType()==expr_type.GetElementType())
1660 if (target_type.IsPointer){
1661 if (expr is NullLiteral)
1662 return new EmptyCast (expr, target_type);
1670 /// Attemps to perform an implict constant conversion of the IntConstant
1671 /// into a different data type using casts (See Implicit Constant
1672 /// Expression Conversions)
1674 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1676 int value = ic.Value;
1679 // FIXME: This could return constants instead of EmptyCasts
1681 if (target_type == TypeManager.sbyte_type){
1682 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1683 return new SByteConstant ((sbyte) value);
1684 } else if (target_type == TypeManager.byte_type){
1685 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1686 return new ByteConstant ((byte) value);
1687 } else if (target_type == TypeManager.short_type){
1688 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1689 return new ShortConstant ((short) value);
1690 } else if (target_type == TypeManager.ushort_type){
1691 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1692 return new UShortConstant ((ushort) value);
1693 } else if (target_type == TypeManager.uint32_type){
1695 return new UIntConstant ((uint) value);
1696 } else if (target_type == TypeManager.uint64_type){
1698 // we can optimize this case: a positive int32
1699 // always fits on a uint64. But we need an opcode
1703 return new ULongConstant ((ulong) value);
1706 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1707 return new EnumConstant (ic, target_type);
1712 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1714 string msg = "Cannot convert implicitly from `"+
1715 TypeManager.CSharpName (source) + "' to `" +
1716 TypeManager.CSharpName (target) + "'";
1718 Report.Error (29, loc, msg);
1722 /// Attemptes to implicityly convert `target' into `type', using
1723 /// ConvertImplicit. If there is no implicit conversion, then
1724 /// an error is signaled
1726 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1727 Type target_type, Location loc)
1731 e = ConvertImplicit (ec, source, target_type, loc);
1735 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1736 Report.Error (664, loc,
1737 "Double literal cannot be implicitly converted to " +
1738 "float type, use F suffix to create a float literal");
1741 Error_CannotConvertImplicit (loc, source.Type, target_type);
1747 /// Performs the explicit numeric conversions
1749 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1751 Type expr_type = expr.Type;
1754 // If we have an enumeration, extract the underlying type,
1755 // use this during the comparission, but wrap around the original
1758 Type real_target_type = target_type;
1760 if (TypeManager.IsEnumType (real_target_type))
1761 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1763 if (expr_type == TypeManager.sbyte_type){
1765 // From sbyte to byte, ushort, uint, ulong, char
1767 if (real_target_type == TypeManager.byte_type)
1768 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1769 if (real_target_type == TypeManager.ushort_type)
1770 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1771 if (real_target_type == TypeManager.uint32_type)
1772 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1773 if (real_target_type == TypeManager.uint64_type)
1774 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1775 if (real_target_type == TypeManager.char_type)
1776 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1777 } else if (expr_type == TypeManager.byte_type){
1779 // From byte to sbyte and char
1781 if (real_target_type == TypeManager.sbyte_type)
1782 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1783 if (real_target_type == TypeManager.char_type)
1784 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1785 } else if (expr_type == TypeManager.short_type){
1787 // From short to sbyte, byte, ushort, uint, ulong, char
1789 if (real_target_type == TypeManager.sbyte_type)
1790 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1791 if (real_target_type == TypeManager.byte_type)
1792 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1793 if (real_target_type == TypeManager.ushort_type)
1794 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1795 if (real_target_type == TypeManager.uint32_type)
1796 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1797 if (real_target_type == TypeManager.uint64_type)
1798 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1799 if (real_target_type == TypeManager.char_type)
1800 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1801 } else if (expr_type == TypeManager.ushort_type){
1803 // From ushort to sbyte, byte, short, char
1805 if (real_target_type == TypeManager.sbyte_type)
1806 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1807 if (real_target_type == TypeManager.byte_type)
1808 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1809 if (real_target_type == TypeManager.short_type)
1810 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1811 if (real_target_type == TypeManager.char_type)
1812 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1813 } else if (expr_type == TypeManager.int32_type){
1815 // From int to sbyte, byte, short, ushort, uint, ulong, char
1817 if (real_target_type == TypeManager.sbyte_type)
1818 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1819 if (real_target_type == TypeManager.byte_type)
1820 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1821 if (real_target_type == TypeManager.short_type)
1822 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1823 if (real_target_type == TypeManager.ushort_type)
1824 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1825 if (real_target_type == TypeManager.uint32_type)
1826 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1827 if (real_target_type == TypeManager.uint64_type)
1828 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1829 if (real_target_type == TypeManager.char_type)
1830 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1831 } else if (expr_type == TypeManager.uint32_type){
1833 // From uint to sbyte, byte, short, ushort, int, char
1835 if (real_target_type == TypeManager.sbyte_type)
1836 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1837 if (real_target_type == TypeManager.byte_type)
1838 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1839 if (real_target_type == TypeManager.short_type)
1840 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1841 if (real_target_type == TypeManager.ushort_type)
1842 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1843 if (real_target_type == TypeManager.int32_type)
1844 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1845 if (real_target_type == TypeManager.char_type)
1846 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1847 } else if (expr_type == TypeManager.int64_type){
1849 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1851 if (real_target_type == TypeManager.sbyte_type)
1852 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1853 if (real_target_type == TypeManager.byte_type)
1854 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1855 if (real_target_type == TypeManager.short_type)
1856 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1857 if (real_target_type == TypeManager.ushort_type)
1858 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1859 if (real_target_type == TypeManager.int32_type)
1860 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1861 if (real_target_type == TypeManager.uint32_type)
1862 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1863 if (real_target_type == TypeManager.uint64_type)
1864 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1865 if (real_target_type == TypeManager.char_type)
1866 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1867 } else if (expr_type == TypeManager.uint64_type){
1869 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1871 if (real_target_type == TypeManager.sbyte_type)
1872 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1873 if (real_target_type == TypeManager.byte_type)
1874 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1875 if (real_target_type == TypeManager.short_type)
1876 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1877 if (real_target_type == TypeManager.ushort_type)
1878 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1879 if (real_target_type == TypeManager.int32_type)
1880 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1881 if (real_target_type == TypeManager.uint32_type)
1882 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1883 if (real_target_type == TypeManager.int64_type)
1884 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1885 if (real_target_type == TypeManager.char_type)
1886 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1887 } else if (expr_type == TypeManager.char_type){
1889 // From char to sbyte, byte, short
1891 if (real_target_type == TypeManager.sbyte_type)
1892 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1893 if (real_target_type == TypeManager.byte_type)
1894 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1895 if (real_target_type == TypeManager.short_type)
1896 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1897 } else if (expr_type == TypeManager.float_type){
1899 // From float to sbyte, byte, short,
1900 // ushort, int, uint, long, ulong, char
1903 if (real_target_type == TypeManager.sbyte_type)
1904 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1905 if (real_target_type == TypeManager.byte_type)
1906 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1907 if (real_target_type == TypeManager.short_type)
1908 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1909 if (real_target_type == TypeManager.ushort_type)
1910 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1911 if (real_target_type == TypeManager.int32_type)
1912 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1913 if (real_target_type == TypeManager.uint32_type)
1914 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1915 if (real_target_type == TypeManager.int64_type)
1916 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1917 if (real_target_type == TypeManager.uint64_type)
1918 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1919 if (real_target_type == TypeManager.char_type)
1920 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1921 if (real_target_type == TypeManager.decimal_type)
1922 return InternalTypeConstructor (ec, expr, target_type);
1923 } else if (expr_type == TypeManager.double_type){
1925 // From double to byte, byte, short,
1926 // ushort, int, uint, long, ulong,
1927 // char, float or decimal
1929 if (real_target_type == TypeManager.sbyte_type)
1930 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1931 if (real_target_type == TypeManager.byte_type)
1932 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1933 if (real_target_type == TypeManager.short_type)
1934 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1935 if (real_target_type == TypeManager.ushort_type)
1936 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1937 if (real_target_type == TypeManager.int32_type)
1938 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1939 if (real_target_type == TypeManager.uint32_type)
1940 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1941 if (real_target_type == TypeManager.int64_type)
1942 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1943 if (real_target_type == TypeManager.uint64_type)
1944 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1945 if (real_target_type == TypeManager.char_type)
1946 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1947 if (real_target_type == TypeManager.float_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1949 if (real_target_type == TypeManager.decimal_type)
1950 return InternalTypeConstructor (ec, expr, target_type);
1953 // decimal is taken care of by the op_Explicit methods.
1959 /// Returns whether an explicit reference conversion can be performed
1960 /// from source_type to target_type
1962 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1964 bool target_is_value_type = target_type.IsValueType;
1966 if (source_type == target_type)
1970 // From object to any reference type
1972 if (source_type == TypeManager.object_type && !target_is_value_type)
1976 // From any class S to any class-type T, provided S is a base class of T
1978 if (target_type.IsSubclassOf (source_type))
1982 // From any interface type S to any interface T provided S is not derived from T
1984 if (source_type.IsInterface && target_type.IsInterface){
1985 if (!target_type.IsSubclassOf (source_type))
1990 // From any class type S to any interface T, provided S is not sealed
1991 // and provided S does not implement T.
1993 if (target_type.IsInterface && !source_type.IsSealed &&
1994 !TypeManager.ImplementsInterface (source_type, target_type))
1998 // From any interface-type S to to any class type T, provided T is not
1999 // sealed, or provided T implements S.
2001 if (source_type.IsInterface &&
2002 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2006 // From an array type S with an element type Se to an array type T with an
2007 // element type Te provided all the following are true:
2008 // * S and T differe only in element type, in other words, S and T
2009 // have the same number of dimensions.
2010 // * Both Se and Te are reference types
2011 // * An explicit referenc conversions exist from Se to Te
2013 if (source_type.IsArray && target_type.IsArray) {
2014 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2016 Type source_element_type = source_type.GetElementType ();
2017 Type target_element_type = target_type.GetElementType ();
2019 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2020 if (ExplicitReferenceConversionExists (source_element_type,
2021 target_element_type))
2027 // From System.Array to any array-type
2028 if (source_type == TypeManager.array_type &&
2029 target_type.IsArray){
2034 // From System delegate to any delegate-type
2036 if (source_type == TypeManager.delegate_type &&
2037 target_type.IsSubclassOf (TypeManager.delegate_type))
2041 // From ICloneable to Array or Delegate types
2043 if (source_type == TypeManager.icloneable_type &&
2044 (target_type == TypeManager.array_type ||
2045 target_type == TypeManager.delegate_type))
2052 /// Implements Explicit Reference conversions
2054 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2056 Type source_type = source.Type;
2057 bool target_is_value_type = target_type.IsValueType;
2060 // From object to any reference type
2062 if (source_type == TypeManager.object_type && !target_is_value_type)
2063 return new ClassCast (source, target_type);
2067 // From any class S to any class-type T, provided S is a base class of T
2069 if (target_type.IsSubclassOf (source_type))
2070 return new ClassCast (source, target_type);
2073 // From any interface type S to any interface T provided S is not derived from T
2075 if (source_type.IsInterface && target_type.IsInterface){
2076 if (TypeManager.ImplementsInterface (source_type, target_type))
2079 return new ClassCast (source, target_type);
2083 // From any class type S to any interface T, provides S is not sealed
2084 // and provided S does not implement T.
2086 if (target_type.IsInterface && !source_type.IsSealed) {
2087 if (TypeManager.ImplementsInterface (source_type, target_type))
2090 return new ClassCast (source, target_type);
2095 // From any interface-type S to to any class type T, provided T is not
2096 // sealed, or provided T implements S.
2098 if (source_type.IsInterface) {
2099 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2100 return new ClassCast (source, target_type);
2105 // From an array type S with an element type Se to an array type T with an
2106 // element type Te provided all the following are true:
2107 // * S and T differe only in element type, in other words, S and T
2108 // have the same number of dimensions.
2109 // * Both Se and Te are reference types
2110 // * An explicit referenc conversions exist from Se to Te
2112 if (source_type.IsArray && target_type.IsArray) {
2113 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2115 Type source_element_type = source_type.GetElementType ();
2116 Type target_element_type = target_type.GetElementType ();
2118 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2119 if (ExplicitReferenceConversionExists (source_element_type,
2120 target_element_type))
2121 return new ClassCast (source, target_type);
2126 // From System.Array to any array-type
2127 if (source_type == TypeManager.array_type &&
2128 target_type.IsArray) {
2129 return new ClassCast (source, target_type);
2133 // From System delegate to any delegate-type
2135 if (source_type == TypeManager.delegate_type &&
2136 target_type.IsSubclassOf (TypeManager.delegate_type))
2137 return new ClassCast (source, target_type);
2140 // From ICloneable to Array or Delegate types
2142 if (source_type == TypeManager.icloneable_type &&
2143 (target_type == TypeManager.array_type ||
2144 target_type == TypeManager.delegate_type))
2145 return new ClassCast (source, target_type);
2151 /// Performs an explicit conversion of the expression `expr' whose
2152 /// type is expr.Type to `target_type'.
2154 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2155 Type target_type, Location loc)
2157 Type expr_type = expr.Type;
2158 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2163 ne = ConvertNumericExplicit (ec, expr, target_type);
2168 // Unboxing conversion.
2170 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2171 return new UnboxCast (expr, target_type);
2176 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2180 // FIXME: Is there any reason we should have EnumConstant
2181 // dealt with here instead of just using always the
2182 // UnderlyingSystemType to wrap the type?
2184 if (expr is EnumConstant)
2185 e = ((EnumConstant) expr).Child;
2187 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2190 Expression t = ConvertImplicit (ec, e, target_type, loc);
2194 return ConvertNumericExplicit (ec, e, target_type);
2197 ne = ConvertReferenceExplicit (expr, target_type);
2202 if (target_type.IsPointer){
2203 if (expr_type.IsPointer)
2204 return new EmptyCast (expr, target_type);
2206 if (expr_type == TypeManager.sbyte_type ||
2207 expr_type == TypeManager.byte_type ||
2208 expr_type == TypeManager.short_type ||
2209 expr_type == TypeManager.ushort_type ||
2210 expr_type == TypeManager.int32_type ||
2211 expr_type == TypeManager.uint32_type ||
2212 expr_type == TypeManager.uint64_type ||
2213 expr_type == TypeManager.int64_type)
2214 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2216 if (expr_type.IsPointer){
2217 if (target_type == TypeManager.sbyte_type ||
2218 target_type == TypeManager.byte_type ||
2219 target_type == TypeManager.short_type ||
2220 target_type == TypeManager.ushort_type ||
2221 target_type == TypeManager.int32_type ||
2222 target_type == TypeManager.uint32_type ||
2223 target_type == TypeManager.uint64_type ||
2224 target_type == TypeManager.int64_type){
2225 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2228 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2233 ce = ConvertNumericExplicit (ec, e, target_type);
2237 // We should always be able to go from an uint32
2238 // implicitly or explicitly to the other integral
2241 throw new Exception ("Internal compiler error");
2246 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2250 Error_CannotConvertType (loc, expr_type, target_type);
2255 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2257 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2258 Type target_type, Location l)
2260 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2265 ne = ConvertNumericExplicit (ec, expr, target_type);
2269 ne = ConvertReferenceExplicit (expr, target_type);
2273 Error_CannotConvertType (l, expr.Type, target_type);
2277 static string ExprClassName (ExprClass c)
2280 case ExprClass.Invalid:
2282 case ExprClass.Value:
2284 case ExprClass.Variable:
2286 case ExprClass.Namespace:
2288 case ExprClass.Type:
2290 case ExprClass.MethodGroup:
2291 return "method group";
2292 case ExprClass.PropertyAccess:
2293 return "property access";
2294 case ExprClass.EventAccess:
2295 return "event access";
2296 case ExprClass.IndexerAccess:
2297 return "indexer access";
2298 case ExprClass.Nothing:
2301 throw new Exception ("Should not happen");
2305 /// Reports that we were expecting `expr' to be of class `expected'
2307 public void Error118 (string expected)
2309 string kind = "Unknown";
2311 kind = ExprClassName (eclass);
2313 Error (118, "Expression denotes a `" + kind +
2314 "' where a `" + expected + "' was expected");
2317 public void Error118 (ResolveFlags flags)
2319 ArrayList valid = new ArrayList (10);
2321 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2322 valid.Add ("variable");
2323 valid.Add ("value");
2326 if ((flags & ResolveFlags.Type) != 0)
2329 if ((flags & ResolveFlags.MethodGroup) != 0)
2330 valid.Add ("method group");
2332 if ((flags & ResolveFlags.SimpleName) != 0)
2333 valid.Add ("simple name");
2335 if (valid.Count == 0)
2336 valid.Add ("unknown");
2338 StringBuilder sb = new StringBuilder ();
2339 for (int i = 0; i < valid.Count; i++) {
2342 else if (i == valid.Count)
2344 sb.Append (valid [i]);
2347 string kind = ExprClassName (eclass);
2349 Error (119, "Expression denotes a `" + kind + "' where " +
2350 "a `" + sb.ToString () + "' was expected");
2353 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2355 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2356 TypeManager.CSharpName (t));
2359 public static void UnsafeError (Location loc)
2361 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2365 /// Converts the IntConstant, UIntConstant, LongConstant or
2366 /// ULongConstant into the integral target_type. Notice
2367 /// that we do not return an `Expression' we do return
2368 /// a boxed integral type.
2370 /// FIXME: Since I added the new constants, we need to
2371 /// also support conversions from CharConstant, ByteConstant,
2372 /// SByteConstant, UShortConstant, ShortConstant
2374 /// This is used by the switch statement, so the domain
2375 /// of work is restricted to the literals above, and the
2376 /// targets are int32, uint32, char, byte, sbyte, ushort,
2377 /// short, uint64 and int64
2379 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2383 if (c.Type == target_type)
2384 return ((Constant) c).GetValue ();
2387 // Make into one of the literals we handle, we dont really care
2388 // about this value as we will just return a few limited types
2390 if (c is EnumConstant)
2391 c = ((EnumConstant)c).WidenToCompilerConstant ();
2393 if (c is IntConstant){
2394 int v = ((IntConstant) c).Value;
2396 if (target_type == TypeManager.uint32_type){
2399 } else if (target_type == TypeManager.char_type){
2400 if (v >= Char.MinValue && v <= Char.MaxValue)
2402 } else if (target_type == TypeManager.byte_type){
2403 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2405 } else if (target_type == TypeManager.sbyte_type){
2406 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2408 } else if (target_type == TypeManager.short_type){
2409 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2411 } else if (target_type == TypeManager.ushort_type){
2412 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2414 } else if (target_type == TypeManager.int64_type)
2416 else if (target_type == TypeManager.uint64_type){
2422 } else if (c is UIntConstant){
2423 uint v = ((UIntConstant) c).Value;
2425 if (target_type == TypeManager.int32_type){
2426 if (v <= Int32.MaxValue)
2428 } else if (target_type == TypeManager.char_type){
2429 if (v >= Char.MinValue && v <= Char.MaxValue)
2431 } else if (target_type == TypeManager.byte_type){
2432 if (v <= Byte.MaxValue)
2434 } else if (target_type == TypeManager.sbyte_type){
2435 if (v <= SByte.MaxValue)
2437 } else if (target_type == TypeManager.short_type){
2438 if (v <= UInt16.MaxValue)
2440 } else if (target_type == TypeManager.ushort_type){
2441 if (v <= UInt16.MaxValue)
2443 } else if (target_type == TypeManager.int64_type)
2445 else if (target_type == TypeManager.uint64_type)
2448 } else if (c is LongConstant){
2449 long v = ((LongConstant) c).Value;
2451 if (target_type == TypeManager.int32_type){
2452 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2454 } else if (target_type == TypeManager.uint32_type){
2455 if (v >= 0 && v <= UInt32.MaxValue)
2457 } else if (target_type == TypeManager.char_type){
2458 if (v >= Char.MinValue && v <= Char.MaxValue)
2460 } else if (target_type == TypeManager.byte_type){
2461 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2463 } else if (target_type == TypeManager.sbyte_type){
2464 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2466 } else if (target_type == TypeManager.short_type){
2467 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2469 } else if (target_type == TypeManager.ushort_type){
2470 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2472 } else if (target_type == TypeManager.uint64_type){
2477 } else if (c is ULongConstant){
2478 ulong v = ((ULongConstant) c).Value;
2480 if (target_type == TypeManager.int32_type){
2481 if (v <= Int32.MaxValue)
2483 } else if (target_type == TypeManager.uint32_type){
2484 if (v <= UInt32.MaxValue)
2486 } else if (target_type == TypeManager.char_type){
2487 if (v >= Char.MinValue && v <= Char.MaxValue)
2489 } else if (target_type == TypeManager.byte_type){
2490 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2492 } else if (target_type == TypeManager.sbyte_type){
2493 if (v <= (int) SByte.MaxValue)
2495 } else if (target_type == TypeManager.short_type){
2496 if (v <= UInt16.MaxValue)
2498 } else if (target_type == TypeManager.ushort_type){
2499 if (v <= UInt16.MaxValue)
2501 } else if (target_type == TypeManager.int64_type){
2502 if (v <= Int64.MaxValue)
2506 } else if (c is ByteConstant){
2507 byte v = ((ByteConstant) c).Value;
2509 if (target_type == TypeManager.int32_type)
2511 else if (target_type == TypeManager.uint32_type)
2513 else if (target_type == TypeManager.char_type)
2515 else if (target_type == TypeManager.sbyte_type){
2516 if (v <= SByte.MaxValue)
2518 } else if (target_type == TypeManager.short_type)
2520 else if (target_type == TypeManager.ushort_type)
2522 else if (target_type == TypeManager.int64_type)
2524 else if (target_type == TypeManager.uint64_type)
2527 } else if (c is SByteConstant){
2528 sbyte v = ((SByteConstant) c).Value;
2530 if (target_type == TypeManager.int32_type)
2532 else if (target_type == TypeManager.uint32_type){
2535 } else if (target_type == TypeManager.char_type){
2538 } else if (target_type == TypeManager.byte_type){
2541 } else if (target_type == TypeManager.short_type)
2543 else if (target_type == TypeManager.ushort_type){
2546 } else if (target_type == TypeManager.int64_type)
2548 else if (target_type == TypeManager.uint64_type){
2553 } else if (c is ShortConstant){
2554 short v = ((ShortConstant) c).Value;
2556 if (target_type == TypeManager.int32_type){
2558 } else if (target_type == TypeManager.uint32_type){
2561 } else if (target_type == TypeManager.char_type){
2564 } else if (target_type == TypeManager.byte_type){
2565 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2567 } else if (target_type == TypeManager.sbyte_type){
2568 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2570 } else if (target_type == TypeManager.ushort_type){
2573 } else if (target_type == TypeManager.int64_type)
2575 else if (target_type == TypeManager.uint64_type)
2579 } else if (c is UShortConstant){
2580 ushort v = ((UShortConstant) c).Value;
2582 if (target_type == TypeManager.int32_type)
2584 else if (target_type == TypeManager.uint32_type)
2586 else if (target_type == TypeManager.char_type){
2587 if (v >= Char.MinValue && v <= Char.MaxValue)
2589 } else if (target_type == TypeManager.byte_type){
2590 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2592 } else if (target_type == TypeManager.sbyte_type){
2593 if (v <= SByte.MaxValue)
2595 } else if (target_type == TypeManager.short_type){
2596 if (v <= Int16.MaxValue)
2598 } else if (target_type == TypeManager.int64_type)
2600 else if (target_type == TypeManager.uint64_type)
2604 } else if (c is CharConstant){
2605 char v = ((CharConstant) c).Value;
2607 if (target_type == TypeManager.int32_type)
2609 else if (target_type == TypeManager.uint32_type)
2611 else if (target_type == TypeManager.byte_type){
2612 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2614 } else if (target_type == TypeManager.sbyte_type){
2615 if (v <= SByte.MaxValue)
2617 } else if (target_type == TypeManager.short_type){
2618 if (v <= Int16.MaxValue)
2620 } else if (target_type == TypeManager.ushort_type)
2622 else if (target_type == TypeManager.int64_type)
2624 else if (target_type == TypeManager.uint64_type)
2629 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2634 // Load the object from the pointer.
2636 public static void LoadFromPtr (ILGenerator ig, Type t)
2638 if (t == TypeManager.int32_type)
2639 ig.Emit (OpCodes.Ldind_I4);
2640 else if (t == TypeManager.uint32_type)
2641 ig.Emit (OpCodes.Ldind_U4);
2642 else if (t == TypeManager.short_type)
2643 ig.Emit (OpCodes.Ldind_I2);
2644 else if (t == TypeManager.ushort_type)
2645 ig.Emit (OpCodes.Ldind_U2);
2646 else if (t == TypeManager.char_type)
2647 ig.Emit (OpCodes.Ldind_U2);
2648 else if (t == TypeManager.byte_type)
2649 ig.Emit (OpCodes.Ldind_U1);
2650 else if (t == TypeManager.sbyte_type)
2651 ig.Emit (OpCodes.Ldind_I1);
2652 else if (t == TypeManager.uint64_type)
2653 ig.Emit (OpCodes.Ldind_I8);
2654 else if (t == TypeManager.int64_type)
2655 ig.Emit (OpCodes.Ldind_I8);
2656 else if (t == TypeManager.float_type)
2657 ig.Emit (OpCodes.Ldind_R4);
2658 else if (t == TypeManager.double_type)
2659 ig.Emit (OpCodes.Ldind_R8);
2660 else if (t == TypeManager.bool_type)
2661 ig.Emit (OpCodes.Ldind_I1);
2662 else if (t == TypeManager.intptr_type)
2663 ig.Emit (OpCodes.Ldind_I);
2664 else if (TypeManager.IsEnumType (t)) {
2665 if (t == TypeManager.enum_type)
2666 ig.Emit (OpCodes.Ldind_Ref);
2668 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2669 } else if (t.IsValueType)
2670 ig.Emit (OpCodes.Ldobj, t);
2672 ig.Emit (OpCodes.Ldind_Ref);
2676 // The stack contains the pointer and the value of type `type'
2678 public static void StoreFromPtr (ILGenerator ig, Type type)
2681 type = TypeManager.EnumToUnderlying (type);
2682 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2683 ig.Emit (OpCodes.Stind_I4);
2684 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2685 ig.Emit (OpCodes.Stind_I8);
2686 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2687 type == TypeManager.ushort_type)
2688 ig.Emit (OpCodes.Stind_I2);
2689 else if (type == TypeManager.float_type)
2690 ig.Emit (OpCodes.Stind_R4);
2691 else if (type == TypeManager.double_type)
2692 ig.Emit (OpCodes.Stind_R8);
2693 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2694 type == TypeManager.bool_type)
2695 ig.Emit (OpCodes.Stind_I1);
2696 else if (type == TypeManager.intptr_type)
2697 ig.Emit (OpCodes.Stind_I);
2698 else if (type.IsValueType)
2699 ig.Emit (OpCodes.Stobj, type);
2701 ig.Emit (OpCodes.Stind_Ref);
2705 // Returns the size of type `t' if known, otherwise, 0
2707 public static int GetTypeSize (Type t)
2709 t = TypeManager.TypeToCoreType (t);
2710 if (t == TypeManager.int32_type ||
2711 t == TypeManager.uint32_type ||
2712 t == TypeManager.float_type)
2714 else if (t == TypeManager.int64_type ||
2715 t == TypeManager.uint64_type ||
2716 t == TypeManager.double_type)
2718 else if (t == TypeManager.byte_type ||
2719 t == TypeManager.sbyte_type ||
2720 t == TypeManager.bool_type)
2722 else if (t == TypeManager.short_type ||
2723 t == TypeManager.char_type ||
2724 t == TypeManager.ushort_type)
2731 // Default implementation of IAssignMethod.CacheTemporaries
2733 public void CacheTemporaries (EmitContext ec)
2737 static void Error_NegativeArrayIndex (Location loc)
2739 Report.Error (284, loc, "Can not create array with a negative size");
2743 // Converts `source' to an int, uint, long or ulong.
2745 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2749 bool old_checked = ec.CheckState;
2750 ec.CheckState = true;
2752 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2753 if (target == null){
2754 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2755 if (target == null){
2756 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2757 if (target == null){
2758 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2760 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2764 ec.CheckState = old_checked;
2767 // Only positive constants are allowed at compile time
2769 if (target is Constant){
2770 if (target is IntConstant){
2771 if (((IntConstant) target).Value < 0){
2772 Error_NegativeArrayIndex (loc);
2777 if (target is LongConstant){
2778 if (((LongConstant) target).Value < 0){
2779 Error_NegativeArrayIndex (loc);
2792 /// This is just a base class for expressions that can
2793 /// appear on statements (invocations, object creation,
2794 /// assignments, post/pre increment and decrement). The idea
2795 /// being that they would support an extra Emition interface that
2796 /// does not leave a result on the stack.
2798 public abstract class ExpressionStatement : Expression {
2801 /// Requests the expression to be emitted in a `statement'
2802 /// context. This means that no new value is left on the
2803 /// stack after invoking this method (constrasted with
2804 /// Emit that will always leave a value on the stack).
2806 public abstract void EmitStatement (EmitContext ec);
2810 /// This kind of cast is used to encapsulate the child
2811 /// whose type is child.Type into an expression that is
2812 /// reported to return "return_type". This is used to encapsulate
2813 /// expressions which have compatible types, but need to be dealt
2814 /// at higher levels with.
2816 /// For example, a "byte" expression could be encapsulated in one
2817 /// of these as an "unsigned int". The type for the expression
2818 /// would be "unsigned int".
2821 public class EmptyCast : Expression {
2822 protected Expression child;
2824 public EmptyCast (Expression child, Type return_type)
2826 eclass = child.eclass;
2831 public override Expression DoResolve (EmitContext ec)
2833 // This should never be invoked, we are born in fully
2834 // initialized state.
2839 public override void Emit (EmitContext ec)
2846 /// This class is used to wrap literals which belong inside Enums
2848 public class EnumConstant : Constant {
2849 public Constant Child;
2851 public EnumConstant (Constant child, Type enum_type)
2853 eclass = child.eclass;
2858 public override Expression DoResolve (EmitContext ec)
2860 // This should never be invoked, we are born in fully
2861 // initialized state.
2866 public override void Emit (EmitContext ec)
2871 public override object GetValue ()
2873 return Child.GetValue ();
2877 // Converts from one of the valid underlying types for an enumeration
2878 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2879 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2881 public Constant WidenToCompilerConstant ()
2883 Type t = TypeManager.EnumToUnderlying (Child.Type);
2884 object v = ((Constant) Child).GetValue ();;
2886 if (t == TypeManager.int32_type)
2887 return new IntConstant ((int) v);
2888 if (t == TypeManager.uint32_type)
2889 return new UIntConstant ((uint) v);
2890 if (t == TypeManager.int64_type)
2891 return new LongConstant ((long) v);
2892 if (t == TypeManager.uint64_type)
2893 return new ULongConstant ((ulong) v);
2894 if (t == TypeManager.short_type)
2895 return new ShortConstant ((short) v);
2896 if (t == TypeManager.ushort_type)
2897 return new UShortConstant ((ushort) v);
2898 if (t == TypeManager.byte_type)
2899 return new ByteConstant ((byte) v);
2900 if (t == TypeManager.sbyte_type)
2901 return new SByteConstant ((sbyte) v);
2903 throw new Exception ("Invalid enumeration underlying type: " + t);
2907 // Extracts the value in the enumeration on its native representation
2909 public object GetPlainValue ()
2911 Type t = TypeManager.EnumToUnderlying (Child.Type);
2912 object v = ((Constant) Child).GetValue ();;
2914 if (t == TypeManager.int32_type)
2916 if (t == TypeManager.uint32_type)
2918 if (t == TypeManager.int64_type)
2920 if (t == TypeManager.uint64_type)
2922 if (t == TypeManager.short_type)
2924 if (t == TypeManager.ushort_type)
2926 if (t == TypeManager.byte_type)
2928 if (t == TypeManager.sbyte_type)
2934 public override string AsString ()
2936 return Child.AsString ();
2939 public override DoubleConstant ConvertToDouble ()
2941 return Child.ConvertToDouble ();
2944 public override FloatConstant ConvertToFloat ()
2946 return Child.ConvertToFloat ();
2949 public override ULongConstant ConvertToULong ()
2951 return Child.ConvertToULong ();
2954 public override LongConstant ConvertToLong ()
2956 return Child.ConvertToLong ();
2959 public override UIntConstant ConvertToUInt ()
2961 return Child.ConvertToUInt ();
2964 public override IntConstant ConvertToInt ()
2966 return Child.ConvertToInt ();
2971 /// This kind of cast is used to encapsulate Value Types in objects.
2973 /// The effect of it is to box the value type emitted by the previous
2976 public class BoxedCast : EmptyCast {
2978 public BoxedCast (Expression expr)
2979 : base (expr, TypeManager.object_type)
2983 public override Expression DoResolve (EmitContext ec)
2985 // This should never be invoked, we are born in fully
2986 // initialized state.
2991 public override void Emit (EmitContext ec)
2995 ec.ig.Emit (OpCodes.Box, child.Type);
2999 public class UnboxCast : EmptyCast {
3000 public UnboxCast (Expression expr, Type return_type)
3001 : base (expr, return_type)
3005 public override Expression DoResolve (EmitContext ec)
3007 // This should never be invoked, we are born in fully
3008 // initialized state.
3013 public override void Emit (EmitContext ec)
3016 ILGenerator ig = ec.ig;
3019 ig.Emit (OpCodes.Unbox, t);
3021 LoadFromPtr (ig, t);
3026 /// This is used to perform explicit numeric conversions.
3028 /// Explicit numeric conversions might trigger exceptions in a checked
3029 /// context, so they should generate the conv.ovf opcodes instead of
3032 public class ConvCast : EmptyCast {
3033 public enum Mode : byte {
3034 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3036 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3037 U2_I1, U2_U1, U2_I2, U2_CH,
3038 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3039 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3040 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3041 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3042 CH_I1, CH_U1, CH_I2,
3043 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3044 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3050 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3051 : base (child, return_type)
3053 checked_state = ec.CheckState;
3057 public override Expression DoResolve (EmitContext ec)
3059 // This should never be invoked, we are born in fully
3060 // initialized state.
3065 public override void Emit (EmitContext ec)
3067 ILGenerator ig = ec.ig;
3073 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3074 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3075 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3076 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3077 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3079 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3080 case Mode.U1_CH: /* nothing */ break;
3082 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3083 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3084 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3085 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3086 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3087 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3089 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3090 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3091 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3092 case Mode.U2_CH: /* nothing */ break;
3094 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3095 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3096 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3097 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3098 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3099 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3100 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3102 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3103 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3104 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3105 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3106 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3107 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3109 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3110 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3111 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3112 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3113 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3114 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3115 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3116 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3118 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3119 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3120 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3121 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3122 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3123 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3124 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3125 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3127 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3128 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3129 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3131 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3132 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3133 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3134 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3135 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3136 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3137 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3138 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3139 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3141 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3142 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3143 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3144 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3145 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3146 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3147 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3148 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3149 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3150 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3154 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3155 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3156 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3157 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3158 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3160 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3161 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3163 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3164 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3165 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3166 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3167 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3168 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3170 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3171 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3172 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3173 case Mode.U2_CH: /* nothing */ break;
3175 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3176 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3177 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3178 case Mode.I4_U4: /* nothing */ break;
3179 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3180 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3181 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3183 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3184 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3185 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3186 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3187 case Mode.U4_I4: /* nothing */ break;
3188 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3190 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3191 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3192 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3193 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3194 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3195 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3196 case Mode.I8_U8: /* nothing */ break;
3197 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3199 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3200 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3201 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3202 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3203 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3204 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3205 case Mode.U8_I8: /* nothing */ break;
3206 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3208 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3209 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3210 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3212 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3213 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3214 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3215 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3216 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3217 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3218 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3219 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3220 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3222 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3223 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3224 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3225 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3226 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3227 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3228 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3229 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3230 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3231 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3237 public class OpcodeCast : EmptyCast {
3241 public OpcodeCast (Expression child, Type return_type, OpCode op)
3242 : base (child, return_type)
3246 second_valid = false;
3249 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3250 : base (child, return_type)
3255 second_valid = true;
3258 public override Expression DoResolve (EmitContext ec)
3260 // This should never be invoked, we are born in fully
3261 // initialized state.
3266 public override void Emit (EmitContext ec)
3277 /// This kind of cast is used to encapsulate a child and cast it
3278 /// to the class requested
3280 public class ClassCast : EmptyCast {
3281 public ClassCast (Expression child, Type return_type)
3282 : base (child, return_type)
3287 public override Expression DoResolve (EmitContext ec)
3289 // This should never be invoked, we are born in fully
3290 // initialized state.
3295 public override void Emit (EmitContext ec)
3299 ec.ig.Emit (OpCodes.Castclass, type);
3305 /// SimpleName expressions are initially formed of a single
3306 /// word and it only happens at the beginning of the expression.
3310 /// The expression will try to be bound to a Field, a Method
3311 /// group or a Property. If those fail we pass the name to our
3312 /// caller and the SimpleName is compounded to perform a type
3313 /// lookup. The idea behind this process is that we want to avoid
3314 /// creating a namespace map from the assemblies, as that requires
3315 /// the GetExportedTypes function to be called and a hashtable to
3316 /// be constructed which reduces startup time. If later we find
3317 /// that this is slower, we should create a `NamespaceExpr' expression
3318 /// that fully participates in the resolution process.
3320 /// For example `System.Console.WriteLine' is decomposed into
3321 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3323 /// The first SimpleName wont produce a match on its own, so it will
3325 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3327 /// System.Console will produce a TypeExpr match.
3329 /// The downside of this is that we might be hitting `LookupType' too many
3330 /// times with this scheme.
3332 public class SimpleName : Expression {
3333 public readonly string Name;
3335 public SimpleName (string name, Location l)
3341 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3343 if (ec.IsFieldInitializer)
3346 "A field initializer cannot reference the non-static field, " +
3347 "method or property `"+name+"'");
3351 "An object reference is required " +
3352 "for the non-static field `"+name+"'");
3356 // Checks whether we are trying to access an instance
3357 // property, method or field from a static body.
3359 Expression MemberStaticCheck (EmitContext ec, Expression e)
3361 if (e is IMemberExpr){
3362 IMemberExpr member = (IMemberExpr) e;
3364 if (!member.IsStatic){
3365 Error_ObjectRefRequired (ec, loc, Name);
3373 public override Expression DoResolve (EmitContext ec)
3375 return SimpleNameResolve (ec, null, false);
3378 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3380 return SimpleNameResolve (ec, right_side, false);
3384 public Expression DoResolveAllowStatic (EmitContext ec)
3386 return SimpleNameResolve (ec, null, true);
3390 /// 7.5.2: Simple Names.
3392 /// Local Variables and Parameters are handled at
3393 /// parse time, so they never occur as SimpleNames.
3395 /// The `allow_static' flag is used by MemberAccess only
3396 /// and it is used to inform us that it is ok for us to
3397 /// avoid the static check, because MemberAccess might end
3398 /// up resolving the Name as a Type name and the access as
3399 /// a static type access.
3401 /// ie: Type Type; .... { Type.GetType (""); }
3403 /// Type is both an instance variable and a Type; Type.GetType
3404 /// is the static method not an instance method of type.
3406 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3408 Expression e = null;
3411 // Stage 1: Performed by the parser (binding to locals or parameters).
3413 if (!ec.OnlyLookupTypes){
3414 Block current_block = ec.CurrentBlock;
3415 if (current_block != null && current_block.IsVariableDefined (Name)){
3416 LocalVariableReference var;
3418 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3420 if (right_side != null)
3421 return var.ResolveLValue (ec, right_side);
3423 return var.Resolve (ec);
3426 if (current_block != null){
3428 Parameter par = null;
3429 Parameters pars = current_block.Parameters;
3431 par = pars.GetParameterByName (Name, out idx);
3434 ParameterReference param;
3436 param = new ParameterReference (pars, idx, Name, loc);
3438 if (right_side != null)
3439 return param.ResolveLValue (ec, right_side);
3441 return param.Resolve (ec);
3446 // Stage 2: Lookup members
3450 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3451 // Hence we have two different cases
3454 DeclSpace lookup_ds = ec.DeclSpace;
3456 if (lookup_ds.TypeBuilder == null)
3459 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3464 // Classes/structs keep looking, enums break
3466 if (lookup_ds is TypeContainer)
3467 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3470 } while (lookup_ds != null);
3472 if (e == null && ec.ContainerType != null)
3473 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3476 // Continuation of stage 2
3479 // Stage 3: Lookup symbol in the various namespaces.
3481 DeclSpace ds = ec.DeclSpace;
3485 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3486 return new TypeExpr (t, loc);
3489 // Stage 2 part b: Lookup up if we are an alias to a type
3492 // Since we are cheating: we only do the Alias lookup for
3493 // namespaces if the name does not include any dots in it
3496 alias_value = ec.DeclSpace.LookupAlias (Name);
3498 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3499 if ((t = RootContext.LookupType (ds, alias_value, true, loc))
3501 return new TypeExpr (t, loc);
3503 // we have alias value, but it isn't Type, so try if it's namespace
3504 return new SimpleName (alias_value, loc);
3507 if (ec.ResolvingTypeTree){
3508 Type dt = ec.DeclSpace.FindType (Name);
3510 return new TypeExpr (dt, loc);
3513 // No match, maybe our parent can compose us
3514 // into something meaningful.
3519 // Stage 2 continues here.
3524 if (ec.OnlyLookupTypes)
3527 if (e is IMemberExpr) {
3528 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3532 IMemberExpr me = e as IMemberExpr;
3536 // This fails if ResolveMemberAccess() was unable to decide whether
3537 // it's a field or a type of the same name.
3538 if (!me.IsStatic && (me.InstanceExpression == null))
3541 if (right_side != null)
3542 e = e.DoResolveLValue (ec, right_side);
3544 e = e.DoResolve (ec);
3549 if (ec.IsStatic || ec.IsFieldInitializer){
3553 return MemberStaticCheck (ec, e);
3558 public override void Emit (EmitContext ec)
3561 // If this is ever reached, then we failed to
3562 // find the name as a namespace
3565 Error (103, "The name `" + Name +
3566 "' does not exist in the class `" +
3567 ec.DeclSpace.Name + "'");
3570 public override string ToString ()
3577 /// Fully resolved expression that evaluates to a type
3579 public class TypeExpr : Expression {
3580 public TypeExpr (Type t, Location l)
3583 eclass = ExprClass.Type;
3587 override public Expression DoResolve (EmitContext ec)
3592 override public void Emit (EmitContext ec)
3594 throw new Exception ("Should never be called");
3599 /// Used to create types from a fully qualified name. These are just used
3600 /// by the parser to setup the core types. A TypeExpression is always
3601 /// classified as a type.
3603 public class TypeExpression : TypeExpr {
3606 public TypeExpression (string name) : base (null, Location.Null)
3611 public override Expression DoResolve (EmitContext ec)
3614 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3618 public override void Emit (EmitContext ec)
3620 throw new Exception ("Should never be called");
3623 public override string ToString ()
3630 /// MethodGroup Expression.
3632 /// This is a fully resolved expression that evaluates to a type
3634 public class MethodGroupExpr : Expression, IMemberExpr {
3635 public MethodBase [] Methods;
3636 Expression instance_expression = null;
3638 public MethodGroupExpr (MemberInfo [] mi, Location l)
3640 Methods = new MethodBase [mi.Length];
3641 mi.CopyTo (Methods, 0);
3642 eclass = ExprClass.MethodGroup;
3643 type = TypeManager.object_type;
3647 public MethodGroupExpr (ArrayList list, Location l)
3649 Methods = new MethodBase [list.Count];
3652 list.CopyTo (Methods, 0);
3654 foreach (MemberInfo m in list){
3655 if (!(m is MethodBase)){
3656 Console.WriteLine ("Name " + m.Name);
3657 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3663 eclass = ExprClass.MethodGroup;
3664 type = TypeManager.object_type;
3668 // `A method group may have associated an instance expression'
3670 public Expression InstanceExpression {
3672 return instance_expression;
3676 instance_expression = value;
3680 public string Name {
3682 return Methods [0].Name;
3686 public bool IsInstance {
3688 foreach (MethodBase mb in Methods)
3696 public bool IsStatic {
3698 foreach (MethodBase mb in Methods)
3706 override public Expression DoResolve (EmitContext ec)
3711 public void ReportUsageError ()
3713 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3714 Methods [0].Name + "()' is referenced without parentheses");
3717 override public void Emit (EmitContext ec)
3719 ReportUsageError ();
3722 bool RemoveMethods (bool keep_static)
3724 ArrayList smethods = new ArrayList ();
3726 foreach (MethodBase mb in Methods){
3727 if (mb.IsStatic == keep_static)
3731 if (smethods.Count == 0)
3734 Methods = new MethodBase [smethods.Count];
3735 smethods.CopyTo (Methods, 0);
3741 /// Removes any instance methods from the MethodGroup, returns
3742 /// false if the resulting set is empty.
3744 public bool RemoveInstanceMethods ()
3746 return RemoveMethods (true);
3750 /// Removes any static methods from the MethodGroup, returns
3751 /// false if the resulting set is empty.
3753 public bool RemoveStaticMethods ()
3755 return RemoveMethods (false);
3760 /// Fully resolved expression that evaluates to a Field
3762 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3763 public readonly FieldInfo FieldInfo;
3764 Expression instance_expr;
3766 public FieldExpr (FieldInfo fi, Location l)
3769 eclass = ExprClass.Variable;
3770 type = fi.FieldType;
3774 public string Name {
3776 return FieldInfo.Name;
3780 public bool IsInstance {
3782 return !FieldInfo.IsStatic;
3786 public bool IsStatic {
3788 return FieldInfo.IsStatic;
3792 public Expression InstanceExpression {
3794 return instance_expr;
3798 instance_expr = value;
3802 override public Expression DoResolve (EmitContext ec)
3804 if (!FieldInfo.IsStatic){
3805 if (instance_expr == null){
3806 throw new Exception ("non-static FieldExpr without instance var\n" +
3807 "You have to assign the Instance variable\n" +
3808 "Of the FieldExpr to set this\n");
3811 // Resolve the field's instance expression while flow analysis is turned
3812 // off: when accessing a field "a.b", we must check whether the field
3813 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3814 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3815 ResolveFlags.DisableFlowAnalysis);
3816 if (instance_expr == null)
3820 // If the instance expression is a local variable or parameter.
3821 IVariable var = instance_expr as IVariable;
3822 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3828 void Report_AssignToReadonly (bool is_instance)
3833 msg = "Readonly field can not be assigned outside " +
3834 "of constructor or variable initializer";
3836 msg = "A static readonly field can only be assigned in " +
3837 "a static constructor";
3839 Report.Error (is_instance ? 191 : 198, loc, msg);
3842 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3844 IVariable var = instance_expr as IVariable;
3846 var.SetFieldAssigned (ec, FieldInfo.Name);
3848 Expression e = DoResolve (ec);
3853 if (!FieldInfo.IsInitOnly)
3857 // InitOnly fields can only be assigned in constructors
3860 if (ec.IsConstructor)
3863 Report_AssignToReadonly (true);
3868 override public void Emit (EmitContext ec)
3870 ILGenerator ig = ec.ig;
3871 bool is_volatile = false;
3873 if (FieldInfo is FieldBuilder){
3874 FieldBase f = TypeManager.GetField (FieldInfo);
3876 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3879 f.status |= Field.Status.USED;
3882 if (FieldInfo.IsStatic){
3884 ig.Emit (OpCodes.Volatile);
3886 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3888 if (instance_expr.Type.IsValueType){
3890 LocalTemporary tempo = null;
3892 if (!(instance_expr is IMemoryLocation)){
3893 tempo = new LocalTemporary (
3894 ec, instance_expr.Type);
3896 InstanceExpression.Emit (ec);
3900 ml = (IMemoryLocation) instance_expr;
3902 ml.AddressOf (ec, AddressOp.Load);
3904 instance_expr.Emit (ec);
3907 ig.Emit (OpCodes.Volatile);
3909 ig.Emit (OpCodes.Ldfld, FieldInfo);
3913 public void EmitAssign (EmitContext ec, Expression source)
3915 FieldAttributes fa = FieldInfo.Attributes;
3916 bool is_static = (fa & FieldAttributes.Static) != 0;
3917 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3918 ILGenerator ig = ec.ig;
3920 if (is_readonly && !ec.IsConstructor){
3921 Report_AssignToReadonly (!is_static);
3926 Expression instance = instance_expr;
3928 if (instance.Type.IsValueType){
3929 if (instance is IMemoryLocation){
3930 IMemoryLocation ml = (IMemoryLocation) instance;
3932 ml.AddressOf (ec, AddressOp.Store);
3934 throw new Exception ("The " + instance + " of type " +
3936 " represents a ValueType and does " +
3937 "not implement IMemoryLocation");
3943 if (FieldInfo is FieldBuilder){
3944 FieldBase f = TypeManager.GetField (FieldInfo);
3946 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3947 ig.Emit (OpCodes.Volatile);
3951 ig.Emit (OpCodes.Stsfld, FieldInfo);
3953 ig.Emit (OpCodes.Stfld, FieldInfo);
3955 if (FieldInfo is FieldBuilder){
3956 FieldBase f = TypeManager.GetField (FieldInfo);
3958 f.status |= Field.Status.ASSIGNED;
3962 public void AddressOf (EmitContext ec, AddressOp mode)
3964 ILGenerator ig = ec.ig;
3966 if (FieldInfo is FieldBuilder){
3967 FieldBase f = TypeManager.GetField (FieldInfo);
3968 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3969 ig.Emit (OpCodes.Volatile);
3972 if (FieldInfo is FieldBuilder){
3973 FieldBase f = TypeManager.GetField (FieldInfo);
3975 if ((mode & AddressOp.Store) != 0)
3976 f.status |= Field.Status.ASSIGNED;
3977 if ((mode & AddressOp.Load) != 0)
3978 f.status |= Field.Status.USED;
3982 // Handle initonly fields specially: make a copy and then
3983 // get the address of the copy.
3985 if (FieldInfo.IsInitOnly){
3986 if (ec.IsConstructor) {
3987 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3992 local = ig.DeclareLocal (type);
3993 ig.Emit (OpCodes.Stloc, local);
3994 ig.Emit (OpCodes.Ldloca, local);
3999 if (FieldInfo.IsStatic)
4000 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4002 if (instance_expr is IMemoryLocation)
4003 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4005 instance_expr.Emit (ec);
4006 ig.Emit (OpCodes.Ldflda, FieldInfo);
4012 /// Expression that evaluates to a Property. The Assign class
4013 /// might set the `Value' expression if we are in an assignment.
4015 /// This is not an LValue because we need to re-write the expression, we
4016 /// can not take data from the stack and store it.
4018 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4019 public readonly PropertyInfo PropertyInfo;
4021 MethodInfo [] Accessors;
4024 Expression instance_expr;
4026 public PropertyExpr (PropertyInfo pi, Location l)
4029 eclass = ExprClass.PropertyAccess;
4032 Accessors = TypeManager.GetAccessors (pi);
4034 if (Accessors != null)
4035 foreach (MethodInfo mi in Accessors){
4041 Accessors = new MethodInfo [2];
4043 type = TypeManager.TypeToCoreType (pi.PropertyType);
4046 public string Name {
4048 return PropertyInfo.Name;
4052 public bool IsInstance {
4058 public bool IsStatic {
4065 // The instance expression associated with this expression
4067 public Expression InstanceExpression {
4069 instance_expr = value;
4073 return instance_expr;
4077 public bool VerifyAssignable ()
4079 if (!PropertyInfo.CanWrite){
4080 Report.Error (200, loc,
4081 "The property `" + PropertyInfo.Name +
4082 "' can not be assigned to, as it has not set accessor");
4089 override public Expression DoResolve (EmitContext ec)
4091 if (!PropertyInfo.CanRead){
4092 Report.Error (154, loc,
4093 "The property `" + PropertyInfo.Name +
4094 "' can not be used in " +
4095 "this context because it lacks a get accessor");
4102 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4104 if (!PropertyInfo.CanWrite){
4105 Report.Error (154, loc,
4106 "The property `" + PropertyInfo.Name +
4107 "' can not be used in " +
4108 "this context because it lacks a set accessor");
4115 override public void Emit (EmitContext ec)
4117 MethodInfo method = Accessors [0];
4120 // Special case: length of single dimension array is turned into ldlen
4122 if (method == TypeManager.int_array_get_length){
4123 Type iet = instance_expr.Type;
4126 // System.Array.Length can be called, but the Type does not
4127 // support invoking GetArrayRank, so test for that case first
4129 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4130 instance_expr.Emit (ec);
4131 ec.ig.Emit (OpCodes.Ldlen);
4136 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
4141 // Implements the IAssignMethod interface for assignments
4143 public void EmitAssign (EmitContext ec, Expression source)
4145 Argument arg = new Argument (source, Argument.AType.Expression);
4146 ArrayList args = new ArrayList ();
4149 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
4152 override public void EmitStatement (EmitContext ec)
4155 ec.ig.Emit (OpCodes.Pop);
4160 /// Fully resolved expression that evaluates to an Event
4162 public class EventExpr : Expression, IMemberExpr {
4163 public readonly EventInfo EventInfo;
4164 public Expression instance_expr;
4167 MethodInfo add_accessor, remove_accessor;
4169 public EventExpr (EventInfo ei, Location loc)
4173 eclass = ExprClass.EventAccess;
4175 add_accessor = TypeManager.GetAddMethod (ei);
4176 remove_accessor = TypeManager.GetRemoveMethod (ei);
4178 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4181 if (EventInfo is MyEventBuilder)
4182 type = ((MyEventBuilder) EventInfo).EventType;
4184 type = EventInfo.EventHandlerType;
4187 public string Name {
4189 return EventInfo.Name;
4193 public bool IsInstance {
4199 public bool IsStatic {
4205 public Expression InstanceExpression {
4207 return instance_expr;
4211 instance_expr = value;
4215 public override Expression DoResolve (EmitContext ec)
4217 // We are born fully resolved
4221 public override void Emit (EmitContext ec)
4223 throw new Exception ("Should not happen I think");
4226 public void EmitAddOrRemove (EmitContext ec, Expression source)
4228 Expression handler = ((Binary) source).Right;
4230 Argument arg = new Argument (handler, Argument.AType.Expression);
4231 ArrayList args = new ArrayList ();
4235 if (((Binary) source).Oper == Binary.Operator.Addition)
4236 Invocation.EmitCall (
4237 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4239 Invocation.EmitCall (
4240 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);