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 // Mask of all the expression class flags.
62 // Disable control flow analysis while resolving the expression.
63 // This is used when resolving the instance expression of a field expression.
64 DisableFlowAnalysis = 16
68 // This is just as a hint to AddressOf of what will be done with the
71 public enum AddressOp {
78 /// This interface is implemented by variables
80 public interface IMemoryLocation {
82 /// The AddressOf method should generate code that loads
83 /// the address of the object and leaves it on the stack.
85 /// The `mode' argument is used to notify the expression
86 /// of whether this will be used to read from the address or
87 /// write to the address.
89 /// This is just a hint that can be used to provide good error
90 /// reporting, and should have no other side effects.
92 void AddressOf (EmitContext ec, AddressOp mode);
96 /// This interface is implemented by variables
98 public interface IVariable {
100 /// Checks whether the variable has already been assigned at
101 /// the current position of the method's control flow and
102 /// reports an appropriate error message if not.
104 /// If the variable is a struct, then this call checks whether
105 /// all of its fields (including all private ones) have been
108 bool IsAssigned (EmitContext ec, Location loc);
111 /// Checks whether field `name' in this struct has been assigned.
113 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
116 /// Tells the flow analysis code that the variable has already
117 /// been assigned at the current code position.
119 /// If the variable is a struct, this call marks all its fields
120 /// (including private fields) as being assigned.
122 void SetAssigned (EmitContext ec);
125 /// Tells the flow analysis code that field `name' in this struct
126 /// has already been assigned atthe current code position.
128 void SetFieldAssigned (EmitContext ec, string name);
132 /// This interface denotes an expression which evaluates to a member
133 /// of a struct or a class.
135 public interface IMemberExpr
138 /// The name of this member.
145 /// Whether this is an instance member.
152 /// Whether this is a static member.
159 /// The instance expression associated with this member, if it's a
160 /// non-static member.
162 Expression InstanceExpression {
168 /// Expression which resolves to a type.
170 public interface ITypeExpression
173 /// Resolve the expression, but only lookup types.
175 Expression DoResolveType (EmitContext ec);
179 /// Base class for expressions
181 public abstract class Expression {
182 public ExprClass eclass;
184 protected Location loc;
196 public Location Location {
203 /// Utility wrapper routine for Error, just to beautify the code
205 public void Error (int error, string s)
207 if (!Location.IsNull (loc))
208 Report.Error (error, loc, s);
210 Report.Error (error, s);
214 /// Utility wrapper routine for Warning, just to beautify the code
216 public void Warning (int warning, string s)
218 if (!Location.IsNull (loc))
219 Report.Warning (warning, loc, s);
221 Report.Warning (warning, s);
225 /// Utility wrapper routine for Warning, only prints the warning if
226 /// warnings of level `level' are enabled.
228 public void Warning (int warning, int level, string s)
230 if (level <= RootContext.WarningLevel)
231 Warning (warning, s);
234 static public void Error_CannotConvertType (Location loc, Type source, Type target)
236 Report.Error (30, loc, "Cannot convert type '" +
237 TypeManager.CSharpName (source) + "' to '" +
238 TypeManager.CSharpName (target) + "'");
242 /// Performs semantic analysis on the Expression
246 /// The Resolve method is invoked to perform the semantic analysis
249 /// The return value is an expression (it can be the
250 /// same expression in some cases) or a new
251 /// expression that better represents this node.
253 /// For example, optimizations of Unary (LiteralInt)
254 /// would return a new LiteralInt with a negated
257 /// If there is an error during semantic analysis,
258 /// then an error should be reported (using Report)
259 /// and a null value should be returned.
261 /// There are two side effects expected from calling
262 /// Resolve(): the the field variable "eclass" should
263 /// be set to any value of the enumeration
264 /// `ExprClass' and the type variable should be set
265 /// to a valid type (this is the type of the
268 public abstract Expression DoResolve (EmitContext ec);
270 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
272 return DoResolve (ec);
276 /// Resolves an expression and performs semantic analysis on it.
280 /// Currently Resolve wraps DoResolve to perform sanity
281 /// checking and assertion checking on what we expect from Resolve.
283 public Expression Resolve (EmitContext ec, ResolveFlags flags)
285 // Are we doing a types-only search ?
286 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
287 ITypeExpression type_expr = this as ITypeExpression;
289 if (type_expr == null)
292 return type_expr.DoResolveType (ec);
295 bool old_do_flow_analysis = ec.DoFlowAnalysis;
296 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
297 ec.DoFlowAnalysis = false;
300 if (this is SimpleName)
301 e = ((SimpleName) this).DoResolveAllowStatic (ec);
305 ec.DoFlowAnalysis = old_do_flow_analysis;
310 if (e is SimpleName){
311 SimpleName s = (SimpleName) e;
313 if ((flags & ResolveFlags.SimpleName) == 0) {
316 "The name `" + s.Name + "' could not be found in `" +
317 ec.DeclSpace.Name + "'");
324 if ((e is TypeExpr) || (e is ComposedCast)) {
325 if ((flags & ResolveFlags.Type) == 0) {
335 if ((flags & ResolveFlags.VariableOrValue) == 0) {
341 case ExprClass.MethodGroup:
342 if ((flags & ResolveFlags.MethodGroup) == 0) {
343 ((MethodGroupExpr) e).ReportUsageError ();
348 case ExprClass.Value:
349 case ExprClass.Variable:
350 case ExprClass.PropertyAccess:
351 case ExprClass.EventAccess:
352 case ExprClass.IndexerAccess:
353 if ((flags & ResolveFlags.VariableOrValue) == 0) {
360 throw new Exception ("Expression " + e.GetType () +
361 " ExprClass is Invalid after resolve");
365 throw new Exception (
366 "Expression " + e.GetType () +
367 " did not set its type after Resolve\n" +
368 "called from: " + this.GetType ());
374 /// Resolves an expression and performs semantic analysis on it.
376 public Expression Resolve (EmitContext ec)
378 return Resolve (ec, ResolveFlags.VariableOrValue);
382 /// Resolves an expression for LValue assignment
386 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
387 /// checking and assertion checking on what we expect from Resolve
389 public Expression ResolveLValue (EmitContext ec, Expression right_side)
391 Expression e = DoResolveLValue (ec, right_side);
394 if (e is SimpleName){
395 SimpleName s = (SimpleName) e;
399 "The name `" + s.Name + "' could not be found in `" +
400 ec.DeclSpace.Name + "'");
404 if (e.eclass == ExprClass.Invalid)
405 throw new Exception ("Expression " + e +
406 " ExprClass is Invalid after resolve");
408 if (e.eclass == ExprClass.MethodGroup) {
409 ((MethodGroupExpr) e).ReportUsageError ();
414 throw new Exception ("Expression " + e +
415 " did not set its type after Resolve");
422 /// Emits the code for the expression
426 /// The Emit method is invoked to generate the code
427 /// for the expression.
429 public abstract void Emit (EmitContext ec);
432 /// Protected constructor. Only derivate types should
433 /// be able to be created
436 protected Expression ()
438 eclass = ExprClass.Invalid;
443 /// Returns a literalized version of a literal FieldInfo
447 /// The possible return values are:
448 /// IntConstant, UIntConstant
449 /// LongLiteral, ULongConstant
450 /// FloatConstant, DoubleConstant
453 /// The value returned is already resolved.
455 public static Constant Constantify (object v, Type t)
457 if (t == TypeManager.int32_type)
458 return new IntConstant ((int) v);
459 else if (t == TypeManager.uint32_type)
460 return new UIntConstant ((uint) v);
461 else if (t == TypeManager.int64_type)
462 return new LongConstant ((long) v);
463 else if (t == TypeManager.uint64_type)
464 return new ULongConstant ((ulong) v);
465 else if (t == TypeManager.float_type)
466 return new FloatConstant ((float) v);
467 else if (t == TypeManager.double_type)
468 return new DoubleConstant ((double) v);
469 else if (t == TypeManager.string_type)
470 return new StringConstant ((string) v);
471 else if (t == TypeManager.short_type)
472 return new ShortConstant ((short)v);
473 else if (t == TypeManager.ushort_type)
474 return new UShortConstant ((ushort)v);
475 else if (t == TypeManager.sbyte_type)
476 return new SByteConstant (((sbyte)v));
477 else if (t == TypeManager.byte_type)
478 return new ByteConstant ((byte)v);
479 else if (t == TypeManager.char_type)
480 return new CharConstant ((char)v);
481 else if (t == TypeManager.bool_type)
482 return new BoolConstant ((bool) v);
483 else if (TypeManager.IsEnumType (t)){
484 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
486 return new EnumConstant (e, t);
488 throw new Exception ("Unknown type for constant (" + t +
493 /// Returns a fully formed expression after a MemberLookup
495 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
498 return new EventExpr ((EventInfo) mi, loc);
499 else if (mi is FieldInfo)
500 return new FieldExpr ((FieldInfo) mi, loc);
501 else if (mi is PropertyInfo)
502 return new PropertyExpr ((PropertyInfo) mi, loc);
503 else if (mi is Type){
504 return new TypeExpr ((System.Type) mi, loc);
511 // FIXME: Probably implement a cache for (t,name,current_access_set)?
513 // This code could use some optimizations, but we need to do some
514 // measurements. For example, we could use a delegate to `flag' when
515 // something can not any longer be a method-group (because it is something
519 // If the return value is an Array, then it is an array of
522 // If the return value is an MemberInfo, it is anything, but a Method
526 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
527 // the arguments here and have MemberLookup return only the methods that
528 // match the argument count/type, unlike we are doing now (we delay this
531 // This is so we can catch correctly attempts to invoke instance methods
532 // from a static body (scan for error 120 in ResolveSimpleName).
535 // FIXME: Potential optimization, have a static ArrayList
538 public static Expression MemberLookup (EmitContext ec, Type t, string name,
539 MemberTypes mt, BindingFlags bf, Location loc)
541 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
544 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
545 string name, MemberTypes mt, BindingFlags bf,
548 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
553 int count = mi.Length;
556 return new MethodGroupExpr (mi, loc);
558 if (mi [0] is MethodBase)
559 return new MethodGroupExpr (mi, loc);
561 return ExprClassFromMemberInfo (ec, mi [0], loc);
564 public const MemberTypes AllMemberTypes =
565 MemberTypes.Constructor |
569 MemberTypes.NestedType |
570 MemberTypes.Property;
572 public const BindingFlags AllBindingFlags =
573 BindingFlags.Public |
574 BindingFlags.Static |
575 BindingFlags.Instance;
577 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
579 return MemberLookup (ec, ec.ContainerType, t, name,
580 AllMemberTypes, AllBindingFlags, loc);
583 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
585 return MemberLookup (ec, ec.ContainerType, t, name,
586 MemberTypes.Method, AllBindingFlags, loc);
590 /// This is a wrapper for MemberLookup that is not used to "probe", but
591 /// to find a final definition. If the final definition is not found, we
592 /// look for private members and display a useful debugging message if we
595 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
598 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
601 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
602 MemberTypes mt, BindingFlags bf, Location loc)
606 int errors = Report.Errors;
608 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
613 // Error has already been reported.
614 if (errors < Report.Errors)
617 e = MemberLookup (ec, t, name, AllMemberTypes,
618 AllBindingFlags | BindingFlags.NonPublic, loc);
621 117, loc, "`" + t + "' does not contain a definition " +
622 "for `" + name + "'");
625 122, loc, "`" + t + "." + name +
626 "' is inaccessible due to its protection level");
632 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
634 EventInfo ei = event_expr.EventInfo;
636 return TypeManager.GetPrivateFieldOfEvent (ei);
639 static EmptyExpression MyEmptyExpr;
640 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
642 Type expr_type = expr.Type;
644 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
645 // if we are a method group, emit a warning
650 if (target_type == TypeManager.object_type) {
652 // A pointer type cannot be converted to object
654 if (expr_type.IsPointer)
657 if (expr_type.IsValueType)
658 return new BoxedCast (expr);
659 if (expr_type.IsClass || expr_type.IsInterface)
660 return new EmptyCast (expr, target_type);
661 } else if (expr_type.IsSubclassOf (target_type)) {
662 return new EmptyCast (expr, target_type);
665 // This code is kind of mirrored inside StandardConversionExists
666 // with the small distinction that we only probe there
668 // Always ensure that the code here and there is in sync
670 // from the null type to any reference-type.
671 if (expr is NullLiteral && !target_type.IsValueType)
672 return new EmptyCast (expr, target_type);
674 // from any class-type S to any interface-type T.
675 if (expr_type.IsClass && target_type.IsInterface) {
676 if (TypeManager.ImplementsInterface (expr_type, target_type))
677 return new EmptyCast (expr, target_type);
682 // from any interface type S to interface-type T.
683 if (expr_type.IsInterface && target_type.IsInterface) {
685 if (TypeManager.ImplementsInterface (expr_type, target_type))
686 return new EmptyCast (expr, target_type);
691 // from an array-type S to an array-type of type T
692 if (expr_type.IsArray && target_type.IsArray) {
693 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
695 Type expr_element_type = expr_type.GetElementType ();
697 if (MyEmptyExpr == null)
698 MyEmptyExpr = new EmptyExpression ();
700 MyEmptyExpr.SetType (expr_element_type);
701 Type target_element_type = target_type.GetElementType ();
703 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
704 if (StandardConversionExists (MyEmptyExpr,
705 target_element_type))
706 return new EmptyCast (expr, target_type);
711 // from an array-type to System.Array
712 if (expr_type.IsArray && target_type == TypeManager.array_type)
713 return new EmptyCast (expr, target_type);
715 // from any delegate type to System.Delegate
716 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
717 target_type == TypeManager.delegate_type)
718 return new EmptyCast (expr, target_type);
720 // from any array-type or delegate type into System.ICloneable.
721 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
722 if (target_type == TypeManager.icloneable_type)
723 return new EmptyCast (expr, target_type);
733 /// Handles expressions like this: decimal d; d = 1;
734 /// and changes them into: decimal d; d = new System.Decimal (1);
736 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
738 ArrayList args = new ArrayList ();
740 args.Add (new Argument (expr, Argument.AType.Expression));
742 Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
744 return ne.Resolve (ec);
748 /// Implicit Numeric Conversions.
750 /// expr is the expression to convert, returns a new expression of type
751 /// target_type or null if an implicit conversion is not possible.
753 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
754 Type target_type, Location loc)
756 Type expr_type = expr.Type;
759 // Attempt to do the implicit constant expression conversions
761 if (expr is IntConstant){
764 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
768 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
770 // Try the implicit constant expression conversion
771 // from long to ulong, instead of a nice routine,
774 long v = ((LongConstant) expr).Value;
776 return new ULongConstant ((ulong) v);
780 // If we have an enumeration, extract the underlying type,
781 // use this during the comparission, but wrap around the original
784 Type real_target_type = target_type;
786 if (TypeManager.IsEnumType (real_target_type))
787 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
789 if (expr_type == real_target_type)
790 return new EmptyCast (expr, target_type);
792 if (expr_type == TypeManager.sbyte_type){
794 // From sbyte to short, int, long, float, double.
796 if (real_target_type == TypeManager.int32_type)
797 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
798 if (real_target_type == TypeManager.int64_type)
799 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
800 if (real_target_type == TypeManager.double_type)
801 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
802 if (real_target_type == TypeManager.float_type)
803 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
804 if (real_target_type == TypeManager.short_type)
805 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
806 if (real_target_type == TypeManager.decimal_type)
807 return InternalTypeConstructor (ec, expr, target_type);
808 } else if (expr_type == TypeManager.byte_type){
810 // From byte to short, ushort, int, uint, long, ulong, float, double
812 if ((real_target_type == TypeManager.short_type) ||
813 (real_target_type == TypeManager.ushort_type) ||
814 (real_target_type == TypeManager.int32_type) ||
815 (real_target_type == TypeManager.uint32_type))
816 return new EmptyCast (expr, target_type);
818 if (real_target_type == TypeManager.uint64_type)
819 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
820 if (real_target_type == TypeManager.int64_type)
821 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
822 if (real_target_type == TypeManager.float_type)
823 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
824 if (real_target_type == TypeManager.double_type)
825 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
826 if (real_target_type == TypeManager.decimal_type)
827 return InternalTypeConstructor (ec, expr, target_type);
828 } else if (expr_type == TypeManager.short_type){
830 // From short to int, long, float, double
832 if (real_target_type == TypeManager.int32_type)
833 return new EmptyCast (expr, target_type);
834 if (real_target_type == TypeManager.int64_type)
835 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
836 if (real_target_type == TypeManager.double_type)
837 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
838 if (real_target_type == TypeManager.float_type)
839 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
840 if (real_target_type == TypeManager.decimal_type)
841 return InternalTypeConstructor (ec, expr, target_type);
842 } else if (expr_type == TypeManager.ushort_type){
844 // From ushort to int, uint, long, ulong, float, double
846 if (real_target_type == TypeManager.uint32_type)
847 return new EmptyCast (expr, target_type);
849 if (real_target_type == TypeManager.uint64_type)
850 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
851 if (real_target_type == TypeManager.int32_type)
852 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
853 if (real_target_type == TypeManager.int64_type)
854 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
855 if (real_target_type == TypeManager.double_type)
856 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
857 if (real_target_type == TypeManager.float_type)
858 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
859 if (real_target_type == TypeManager.decimal_type)
860 return InternalTypeConstructor (ec, expr, target_type);
861 } else if (expr_type == TypeManager.int32_type){
863 // From int to long, float, double
865 if (real_target_type == TypeManager.int64_type)
866 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
867 if (real_target_type == TypeManager.double_type)
868 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
869 if (real_target_type == TypeManager.float_type)
870 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
871 if (real_target_type == TypeManager.decimal_type)
872 return InternalTypeConstructor (ec, expr, target_type);
873 } else if (expr_type == TypeManager.uint32_type){
875 // From uint to long, ulong, float, double
877 if (real_target_type == TypeManager.int64_type)
878 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
879 if (real_target_type == TypeManager.uint64_type)
880 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
881 if (real_target_type == TypeManager.double_type)
882 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
884 if (real_target_type == TypeManager.float_type)
885 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
887 if (real_target_type == TypeManager.decimal_type)
888 return InternalTypeConstructor (ec, expr, target_type);
889 } else if (expr_type == TypeManager.int64_type){
891 // From long/ulong to float, double
893 if (real_target_type == TypeManager.double_type)
894 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
895 if (real_target_type == TypeManager.float_type)
896 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
897 if (real_target_type == TypeManager.decimal_type)
898 return InternalTypeConstructor (ec, expr, target_type);
899 } else if (expr_type == TypeManager.uint64_type){
901 // From ulong to float, double
903 if (real_target_type == TypeManager.double_type)
904 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
906 if (real_target_type == TypeManager.float_type)
907 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
909 if (real_target_type == TypeManager.decimal_type)
910 return InternalTypeConstructor (ec, expr, target_type);
911 } else if (expr_type == TypeManager.char_type){
913 // From char to ushort, int, uint, long, ulong, float, double
915 if ((real_target_type == TypeManager.ushort_type) ||
916 (real_target_type == TypeManager.int32_type) ||
917 (real_target_type == TypeManager.uint32_type))
918 return new EmptyCast (expr, target_type);
919 if (real_target_type == TypeManager.uint64_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
921 if (real_target_type == TypeManager.int64_type)
922 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
923 if (real_target_type == TypeManager.float_type)
924 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
925 if (real_target_type == TypeManager.double_type)
926 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
927 if (real_target_type == TypeManager.decimal_type)
928 return InternalTypeConstructor (ec, expr, target_type);
929 } else if (expr_type == TypeManager.float_type){
933 if (real_target_type == TypeManager.double_type)
934 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
941 // Tests whether an implicit reference conversion exists between expr_type
944 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
946 Type expr_type = expr.Type;
949 // This is the boxed case.
951 if (target_type == TypeManager.object_type) {
952 if ((expr_type.IsClass) ||
953 (expr_type.IsValueType) ||
954 (expr_type.IsInterface))
957 } else if (expr_type.IsSubclassOf (target_type)) {
961 // Please remember that all code below actually comes
962 // from ImplicitReferenceConversion so make sure code remains in sync
964 // from any class-type S to any interface-type T.
965 if (expr_type.IsClass && target_type.IsInterface) {
966 if (TypeManager.ImplementsInterface (expr_type, target_type))
970 // from any interface type S to interface-type T.
971 if (expr_type.IsInterface && target_type.IsInterface)
972 if (TypeManager.ImplementsInterface (expr_type, target_type))
975 // from an array-type S to an array-type of type T
976 if (expr_type.IsArray && target_type.IsArray) {
977 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
979 Type expr_element_type = expr_type.GetElementType ();
981 if (MyEmptyExpr == null)
982 MyEmptyExpr = new EmptyExpression ();
984 MyEmptyExpr.SetType (expr_element_type);
985 Type target_element_type = target_type.GetElementType ();
987 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
988 if (StandardConversionExists (MyEmptyExpr,
989 target_element_type))
994 // from an array-type to System.Array
995 if (expr_type.IsArray && (target_type == TypeManager.array_type))
998 // from any delegate type to System.Delegate
999 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1000 target_type == TypeManager.delegate_type)
1001 if (target_type.IsAssignableFrom (expr_type))
1004 // from any array-type or delegate type into System.ICloneable.
1005 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1006 if (target_type == TypeManager.icloneable_type)
1009 // from the null type to any reference-type.
1010 if (expr is NullLiteral && !target_type.IsValueType &&
1011 !TypeManager.IsEnumType (target_type))
1020 /// Same as StandardConversionExists except that it also looks at
1021 /// implicit user defined conversions - needed for overload resolution
1023 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1025 if (StandardConversionExists (expr, target_type) == true)
1028 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1037 /// Determines if a standard implicit conversion exists from
1038 /// expr_type to target_type
1040 public static bool StandardConversionExists (Expression expr, Type target_type)
1042 Type expr_type = expr.Type;
1044 if (expr_type == TypeManager.void_type)
1047 if (expr_type == target_type)
1050 // First numeric conversions
1052 if (expr_type == TypeManager.sbyte_type){
1054 // From sbyte to short, int, long, float, double.
1056 if ((target_type == TypeManager.int32_type) ||
1057 (target_type == TypeManager.int64_type) ||
1058 (target_type == TypeManager.double_type) ||
1059 (target_type == TypeManager.float_type) ||
1060 (target_type == TypeManager.short_type) ||
1061 (target_type == TypeManager.decimal_type))
1064 } else if (expr_type == TypeManager.byte_type){
1066 // From byte to short, ushort, int, uint, long, ulong, float, double
1068 if ((target_type == TypeManager.short_type) ||
1069 (target_type == TypeManager.ushort_type) ||
1070 (target_type == TypeManager.int32_type) ||
1071 (target_type == TypeManager.uint32_type) ||
1072 (target_type == TypeManager.uint64_type) ||
1073 (target_type == TypeManager.int64_type) ||
1074 (target_type == TypeManager.float_type) ||
1075 (target_type == TypeManager.double_type) ||
1076 (target_type == TypeManager.decimal_type))
1079 } else if (expr_type == TypeManager.short_type){
1081 // From short to int, long, float, double
1083 if ((target_type == TypeManager.int32_type) ||
1084 (target_type == TypeManager.int64_type) ||
1085 (target_type == TypeManager.double_type) ||
1086 (target_type == TypeManager.float_type) ||
1087 (target_type == TypeManager.decimal_type))
1090 } else if (expr_type == TypeManager.ushort_type){
1092 // From ushort to int, uint, long, ulong, float, double
1094 if ((target_type == TypeManager.uint32_type) ||
1095 (target_type == TypeManager.uint64_type) ||
1096 (target_type == TypeManager.int32_type) ||
1097 (target_type == TypeManager.int64_type) ||
1098 (target_type == TypeManager.double_type) ||
1099 (target_type == TypeManager.float_type) ||
1100 (target_type == TypeManager.decimal_type))
1103 } else if (expr_type == TypeManager.int32_type){
1105 // From int to long, float, double
1107 if ((target_type == TypeManager.int64_type) ||
1108 (target_type == TypeManager.double_type) ||
1109 (target_type == TypeManager.float_type) ||
1110 (target_type == TypeManager.decimal_type))
1113 } else if (expr_type == TypeManager.uint32_type){
1115 // From uint to long, ulong, float, double
1117 if ((target_type == TypeManager.int64_type) ||
1118 (target_type == TypeManager.uint64_type) ||
1119 (target_type == TypeManager.double_type) ||
1120 (target_type == TypeManager.float_type) ||
1121 (target_type == TypeManager.decimal_type))
1124 } else if ((expr_type == TypeManager.uint64_type) ||
1125 (expr_type == TypeManager.int64_type)) {
1127 // From long/ulong to float, double
1129 if ((target_type == TypeManager.double_type) ||
1130 (target_type == TypeManager.float_type) ||
1131 (target_type == TypeManager.decimal_type))
1134 } else if (expr_type == TypeManager.char_type){
1136 // From char to ushort, int, uint, long, ulong, float, double
1138 if ((target_type == TypeManager.ushort_type) ||
1139 (target_type == TypeManager.int32_type) ||
1140 (target_type == TypeManager.uint32_type) ||
1141 (target_type == TypeManager.uint64_type) ||
1142 (target_type == TypeManager.int64_type) ||
1143 (target_type == TypeManager.float_type) ||
1144 (target_type == TypeManager.double_type) ||
1145 (target_type == TypeManager.decimal_type))
1148 } else if (expr_type == TypeManager.float_type){
1152 if (target_type == TypeManager.double_type)
1156 if (ImplicitReferenceConversionExists (expr, target_type))
1159 if (expr is IntConstant){
1160 int value = ((IntConstant) expr).Value;
1162 if (target_type == TypeManager.sbyte_type){
1163 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1165 } else if (target_type == TypeManager.byte_type){
1166 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1168 } else if (target_type == TypeManager.short_type){
1169 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1171 } else if (target_type == TypeManager.ushort_type){
1172 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1174 } else if (target_type == TypeManager.uint32_type){
1177 } else if (target_type == TypeManager.uint64_type){
1179 // we can optimize this case: a positive int32
1180 // always fits on a uint64. But we need an opcode
1187 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1191 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1193 // Try the implicit constant expression conversion
1194 // from long to ulong, instead of a nice routine,
1195 // we just inline it
1197 long v = ((LongConstant) expr).Value;
1202 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1203 IntLiteral i = (IntLiteral) expr;
1213 // Used internally by FindMostEncompassedType, this is used
1214 // to avoid creating lots of objects in the tight loop inside
1215 // FindMostEncompassedType
1217 static EmptyExpression priv_fmet_param;
1220 /// Finds "most encompassed type" according to the spec (13.4.2)
1221 /// amongst the methods in the MethodGroupExpr
1223 static Type FindMostEncompassedType (ArrayList types)
1227 if (priv_fmet_param == null)
1228 priv_fmet_param = new EmptyExpression ();
1230 foreach (Type t in types){
1231 priv_fmet_param.SetType (t);
1238 if (StandardConversionExists (priv_fmet_param, best))
1246 // Used internally by FindMostEncompassingType, this is used
1247 // to avoid creating lots of objects in the tight loop inside
1248 // FindMostEncompassingType
1250 static EmptyExpression priv_fmee_ret;
1253 /// Finds "most encompassing type" according to the spec (13.4.2)
1254 /// amongst the types in the given set
1256 static Type FindMostEncompassingType (ArrayList types)
1260 if (priv_fmee_ret == null)
1261 priv_fmee_ret = new EmptyExpression ();
1263 foreach (Type t in types){
1264 priv_fmee_ret.SetType (best);
1271 if (StandardConversionExists (priv_fmee_ret, t))
1279 // Used to avoid creating too many objects
1281 static EmptyExpression priv_fms_expr;
1284 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1285 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1286 /// for explicit and implicit conversion operators.
1288 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1289 bool apply_explicit_conv_rules,
1292 ArrayList src_types_set = new ArrayList ();
1294 if (priv_fms_expr == null)
1295 priv_fms_expr = new EmptyExpression ();
1298 // If any operator converts from S then Sx = S
1300 foreach (MethodBase mb in me.Methods){
1301 ParameterData pd = Invocation.GetParameterData (mb);
1302 Type param_type = pd.ParameterType (0);
1304 if (param_type == source_type)
1307 if (apply_explicit_conv_rules) {
1310 // Find the set of applicable user-defined conversion operators, U. This set
1312 // user-defined implicit or explicit conversion operators declared by
1313 // the classes or structs in D that convert from a type encompassing
1314 // or encompassed by S to a type encompassing or encompassed by T
1316 priv_fms_expr.SetType (param_type);
1317 if (StandardConversionExists (priv_fms_expr, source_type))
1318 src_types_set.Add (param_type);
1320 priv_fms_expr.SetType (source_type);
1321 if (StandardConversionExists (priv_fms_expr, param_type))
1322 src_types_set.Add (param_type);
1326 // Only if S is encompassed by param_type
1328 priv_fms_expr.SetType (source_type);
1329 if (StandardConversionExists (priv_fms_expr, param_type))
1330 src_types_set.Add (param_type);
1335 // Explicit Conv rules
1337 if (apply_explicit_conv_rules) {
1338 ArrayList candidate_set = new ArrayList ();
1340 foreach (Type param_type in src_types_set){
1341 priv_fms_expr.SetType (source_type);
1343 if (StandardConversionExists (priv_fms_expr, param_type))
1344 candidate_set.Add (param_type);
1347 if (candidate_set.Count != 0)
1348 return FindMostEncompassedType (candidate_set);
1354 if (apply_explicit_conv_rules)
1355 return FindMostEncompassingType (src_types_set);
1357 return FindMostEncompassedType (src_types_set);
1361 // Useful in avoiding proliferation of objects
1363 static EmptyExpression priv_fmt_expr;
1366 /// Finds the most specific target Tx according to section 13.4.4
1368 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1369 bool apply_explicit_conv_rules,
1372 ArrayList tgt_types_set = new ArrayList ();
1374 if (priv_fmt_expr == null)
1375 priv_fmt_expr = new EmptyExpression ();
1378 // If any operator converts to T then Tx = T
1380 foreach (MethodInfo mi in me.Methods){
1381 Type ret_type = mi.ReturnType;
1383 if (ret_type == target)
1386 if (apply_explicit_conv_rules) {
1389 // Find the set of applicable user-defined conversion operators, U.
1391 // This set consists of the
1392 // user-defined implicit or explicit conversion operators declared by
1393 // the classes or structs in D that convert from a type encompassing
1394 // or encompassed by S to a type encompassing or encompassed by T
1396 priv_fms_expr.SetType (ret_type);
1397 if (StandardConversionExists (priv_fms_expr, target))
1398 tgt_types_set.Add (ret_type);
1400 priv_fms_expr.SetType (target);
1401 if (StandardConversionExists (priv_fms_expr, ret_type))
1402 tgt_types_set.Add (ret_type);
1406 // Only if T is encompassed by param_type
1408 priv_fms_expr.SetType (ret_type);
1409 if (StandardConversionExists (priv_fms_expr, target))
1410 tgt_types_set.Add (ret_type);
1415 // Explicit conv rules
1417 if (apply_explicit_conv_rules) {
1418 ArrayList candidate_set = new ArrayList ();
1420 foreach (Type ret_type in tgt_types_set){
1421 priv_fmt_expr.SetType (ret_type);
1423 if (StandardConversionExists (priv_fmt_expr, target))
1424 candidate_set.Add (ret_type);
1427 if (candidate_set.Count != 0)
1428 return FindMostEncompassingType (candidate_set);
1432 // Okay, final case !
1434 if (apply_explicit_conv_rules)
1435 return FindMostEncompassedType (tgt_types_set);
1437 return FindMostEncompassingType (tgt_types_set);
1441 /// User-defined Implicit conversions
1443 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1444 Type target, Location loc)
1446 return UserDefinedConversion (ec, source, target, loc, false);
1450 /// User-defined Explicit conversions
1452 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1453 Type target, Location loc)
1455 return UserDefinedConversion (ec, source, target, loc, true);
1459 /// Computes the MethodGroup for the user-defined conversion
1460 /// operators from source_type to target_type. `look_for_explicit'
1461 /// controls whether we should also include the list of explicit
1464 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1465 Type source_type, Type target_type,
1466 Location loc, bool look_for_explicit)
1468 Expression mg1 = null, mg2 = null;
1469 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1473 // FIXME : How does the False operator come into the picture ?
1474 // This doesn't look complete and very correct !
1476 if (target_type == TypeManager.bool_type && !look_for_explicit)
1477 op_name = "op_True";
1479 op_name = "op_Implicit";
1481 MethodGroupExpr union3;
1483 mg1 = MethodLookup (ec, source_type, op_name, loc);
1484 if (source_type.BaseType != null)
1485 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1488 union3 = (MethodGroupExpr) mg2;
1489 else if (mg2 == null)
1490 union3 = (MethodGroupExpr) mg1;
1492 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1494 mg1 = MethodLookup (ec, target_type, op_name, loc);
1497 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1499 union3 = (MethodGroupExpr) mg1;
1502 if (target_type.BaseType != null)
1503 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1507 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1509 union3 = (MethodGroupExpr) mg1;
1512 MethodGroupExpr union4 = null;
1514 if (look_for_explicit) {
1515 op_name = "op_Explicit";
1517 mg5 = MemberLookup (ec, source_type, op_name, loc);
1518 if (source_type.BaseType != null)
1519 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1521 mg7 = MemberLookup (ec, target_type, op_name, loc);
1522 if (target_type.BaseType != null)
1523 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1525 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1526 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1528 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1531 return Invocation.MakeUnionSet (union3, union4, loc);
1535 /// User-defined conversions
1537 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1538 Type target, Location loc,
1539 bool look_for_explicit)
1541 MethodGroupExpr union;
1542 Type source_type = source.Type;
1543 MethodBase method = null;
1545 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1549 Type most_specific_source, most_specific_target;
1552 foreach (MethodBase m in union.Methods){
1553 Console.WriteLine ("Name: " + m.Name);
1554 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1558 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1559 if (most_specific_source == null)
1562 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1563 if (most_specific_target == null)
1568 foreach (MethodBase mb in union.Methods){
1569 ParameterData pd = Invocation.GetParameterData (mb);
1570 MethodInfo mi = (MethodInfo) mb;
1572 if (pd.ParameterType (0) == most_specific_source &&
1573 mi.ReturnType == most_specific_target) {
1579 if (method == null || count > 1) {
1580 Report.Error (-11, loc, "Ambiguous user defined conversion");
1585 // This will do the conversion to the best match that we
1586 // found. Now we need to perform an implict standard conversion
1587 // if the best match was not the type that we were requested
1590 if (look_for_explicit)
1591 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1593 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1599 e = new UserCast ((MethodInfo) method, source, loc);
1600 if (e.Type != target){
1601 if (!look_for_explicit)
1602 e = ConvertImplicitStandard (ec, e, target, loc);
1604 e = ConvertExplicitStandard (ec, e, target, loc);
1610 /// Converts implicitly the resolved expression `expr' into the
1611 /// `target_type'. It returns a new expression that can be used
1612 /// in a context that expects a `target_type'.
1614 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1615 Type target_type, Location loc)
1617 Type expr_type = expr.Type;
1620 if (expr_type == target_type)
1623 if (target_type == null)
1624 throw new Exception ("Target type is null");
1626 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1630 e = ImplicitUserConversion (ec, expr, target_type, loc);
1639 /// Attempts to apply the `Standard Implicit
1640 /// Conversion' rules to the expression `expr' into
1641 /// the `target_type'. It returns a new expression
1642 /// that can be used in a context that expects a
1645 /// This is different from `ConvertImplicit' in that the
1646 /// user defined implicit conversions are excluded.
1648 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1649 Type target_type, Location loc)
1651 Type expr_type = expr.Type;
1654 if (expr_type == target_type)
1657 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1661 e = ImplicitReferenceConversion (expr, target_type);
1665 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1666 IntLiteral i = (IntLiteral) expr;
1669 return new EmptyCast (expr, target_type);
1673 if (expr_type.IsPointer){
1674 if (target_type == TypeManager.void_ptr_type)
1675 return new EmptyCast (expr, target_type);
1678 // yep, comparing pointer types cant be done with
1679 // t1 == t2, we have to compare their element types.
1681 if (target_type.IsPointer){
1682 if (target_type.GetElementType()==expr_type.GetElementType())
1687 if (target_type.IsPointer){
1688 if (expr is NullLiteral)
1689 return new EmptyCast (expr, target_type);
1697 /// Attemps to perform an implict constant conversion of the IntConstant
1698 /// into a different data type using casts (See Implicit Constant
1699 /// Expression Conversions)
1701 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1703 int value = ic.Value;
1706 // FIXME: This could return constants instead of EmptyCasts
1708 if (target_type == TypeManager.sbyte_type){
1709 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1710 return new SByteConstant ((sbyte) value);
1711 } else if (target_type == TypeManager.byte_type){
1712 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1713 return new ByteConstant ((byte) value);
1714 } else if (target_type == TypeManager.short_type){
1715 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1716 return new ShortConstant ((short) value);
1717 } else if (target_type == TypeManager.ushort_type){
1718 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1719 return new UShortConstant ((ushort) value);
1720 } else if (target_type == TypeManager.uint32_type){
1722 return new UIntConstant ((uint) value);
1723 } else if (target_type == TypeManager.uint64_type){
1725 // we can optimize this case: a positive int32
1726 // always fits on a uint64. But we need an opcode
1730 return new ULongConstant ((ulong) value);
1733 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1734 return new EnumConstant (ic, target_type);
1739 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1741 string msg = "Cannot convert implicitly from `"+
1742 TypeManager.CSharpName (source) + "' to `" +
1743 TypeManager.CSharpName (target) + "'";
1745 Report.Error (29, loc, msg);
1749 /// Attemptes to implicityly convert `target' into `type', using
1750 /// ConvertImplicit. If there is no implicit conversion, then
1751 /// an error is signaled
1753 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1754 Type target_type, Location loc)
1758 e = ConvertImplicit (ec, source, target_type, loc);
1762 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1763 Report.Error (664, loc,
1764 "Double literal cannot be implicitly converted to " +
1765 "float type, use F suffix to create a float literal");
1768 Error_CannotConvertImplicit (loc, source.Type, target_type);
1774 /// Performs the explicit numeric conversions
1776 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1778 Type expr_type = expr.Type;
1781 // If we have an enumeration, extract the underlying type,
1782 // use this during the comparission, but wrap around the original
1785 Type real_target_type = target_type;
1787 if (TypeManager.IsEnumType (real_target_type))
1788 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1790 if (expr_type == TypeManager.sbyte_type){
1792 // From sbyte to byte, ushort, uint, ulong, char
1794 if (real_target_type == TypeManager.byte_type)
1795 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1796 if (real_target_type == TypeManager.ushort_type)
1797 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1798 if (real_target_type == TypeManager.uint32_type)
1799 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1800 if (real_target_type == TypeManager.uint64_type)
1801 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1802 if (real_target_type == TypeManager.char_type)
1803 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1804 } else if (expr_type == TypeManager.byte_type){
1806 // From byte to sbyte and char
1808 if (real_target_type == TypeManager.sbyte_type)
1809 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1810 if (real_target_type == TypeManager.char_type)
1811 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1812 } else if (expr_type == TypeManager.short_type){
1814 // From short to sbyte, byte, ushort, uint, ulong, char
1816 if (real_target_type == TypeManager.sbyte_type)
1817 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1818 if (real_target_type == TypeManager.byte_type)
1819 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1820 if (real_target_type == TypeManager.ushort_type)
1821 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1822 if (real_target_type == TypeManager.uint32_type)
1823 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1824 if (real_target_type == TypeManager.uint64_type)
1825 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1826 if (real_target_type == TypeManager.char_type)
1827 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1828 } else if (expr_type == TypeManager.ushort_type){
1830 // From ushort to sbyte, byte, short, char
1832 if (real_target_type == TypeManager.sbyte_type)
1833 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1834 if (real_target_type == TypeManager.byte_type)
1835 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1836 if (real_target_type == TypeManager.short_type)
1837 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1838 if (real_target_type == TypeManager.char_type)
1839 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1840 } else if (expr_type == TypeManager.int32_type){
1842 // From int to sbyte, byte, short, ushort, uint, ulong, char
1844 if (real_target_type == TypeManager.sbyte_type)
1845 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1846 if (real_target_type == TypeManager.byte_type)
1847 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1848 if (real_target_type == TypeManager.short_type)
1849 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1850 if (real_target_type == TypeManager.ushort_type)
1851 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1852 if (real_target_type == TypeManager.uint32_type)
1853 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1854 if (real_target_type == TypeManager.uint64_type)
1855 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1856 if (real_target_type == TypeManager.char_type)
1857 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1858 } else if (expr_type == TypeManager.uint32_type){
1860 // From uint to sbyte, byte, short, ushort, int, char
1862 if (real_target_type == TypeManager.sbyte_type)
1863 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1864 if (real_target_type == TypeManager.byte_type)
1865 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1866 if (real_target_type == TypeManager.short_type)
1867 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1868 if (real_target_type == TypeManager.ushort_type)
1869 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1870 if (real_target_type == TypeManager.int32_type)
1871 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1872 if (real_target_type == TypeManager.char_type)
1873 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1874 } else if (expr_type == TypeManager.int64_type){
1876 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1878 if (real_target_type == TypeManager.sbyte_type)
1879 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1880 if (real_target_type == TypeManager.byte_type)
1881 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1882 if (real_target_type == TypeManager.short_type)
1883 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1884 if (real_target_type == TypeManager.ushort_type)
1885 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1886 if (real_target_type == TypeManager.int32_type)
1887 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1888 if (real_target_type == TypeManager.uint32_type)
1889 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1890 if (real_target_type == TypeManager.uint64_type)
1891 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1892 if (real_target_type == TypeManager.char_type)
1893 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1894 } else if (expr_type == TypeManager.uint64_type){
1896 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1898 if (real_target_type == TypeManager.sbyte_type)
1899 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1900 if (real_target_type == TypeManager.byte_type)
1901 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1902 if (real_target_type == TypeManager.short_type)
1903 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1904 if (real_target_type == TypeManager.ushort_type)
1905 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1906 if (real_target_type == TypeManager.int32_type)
1907 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1908 if (real_target_type == TypeManager.uint32_type)
1909 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1910 if (real_target_type == TypeManager.int64_type)
1911 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1912 if (real_target_type == TypeManager.char_type)
1913 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1914 } else if (expr_type == TypeManager.char_type){
1916 // From char to sbyte, byte, short
1918 if (real_target_type == TypeManager.sbyte_type)
1919 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1920 if (real_target_type == TypeManager.byte_type)
1921 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1922 if (real_target_type == TypeManager.short_type)
1923 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1924 } else if (expr_type == TypeManager.float_type){
1926 // From float to sbyte, byte, short,
1927 // ushort, int, uint, long, ulong, char
1930 if (real_target_type == TypeManager.sbyte_type)
1931 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1932 if (real_target_type == TypeManager.byte_type)
1933 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1934 if (real_target_type == TypeManager.short_type)
1935 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1936 if (real_target_type == TypeManager.ushort_type)
1937 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1938 if (real_target_type == TypeManager.int32_type)
1939 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1940 if (real_target_type == TypeManager.uint32_type)
1941 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1942 if (real_target_type == TypeManager.int64_type)
1943 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1944 if (real_target_type == TypeManager.uint64_type)
1945 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1946 if (real_target_type == TypeManager.char_type)
1947 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1948 if (real_target_type == TypeManager.decimal_type)
1949 return InternalTypeConstructor (ec, expr, target_type);
1950 } else if (expr_type == TypeManager.double_type){
1952 // From double to byte, byte, short,
1953 // ushort, int, uint, long, ulong,
1954 // char, float or decimal
1956 if (real_target_type == TypeManager.sbyte_type)
1957 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1958 if (real_target_type == TypeManager.byte_type)
1959 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1960 if (real_target_type == TypeManager.short_type)
1961 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1962 if (real_target_type == TypeManager.ushort_type)
1963 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1964 if (real_target_type == TypeManager.int32_type)
1965 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1966 if (real_target_type == TypeManager.uint32_type)
1967 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1968 if (real_target_type == TypeManager.int64_type)
1969 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1970 if (real_target_type == TypeManager.uint64_type)
1971 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1972 if (real_target_type == TypeManager.char_type)
1973 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1974 if (real_target_type == TypeManager.float_type)
1975 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1976 if (real_target_type == TypeManager.decimal_type)
1977 return InternalTypeConstructor (ec, expr, target_type);
1980 // decimal is taken care of by the op_Explicit methods.
1986 /// Returns whether an explicit reference conversion can be performed
1987 /// from source_type to target_type
1989 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1991 bool target_is_value_type = target_type.IsValueType;
1993 if (source_type == target_type)
1997 // From object to any reference type
1999 if (source_type == TypeManager.object_type && !target_is_value_type)
2003 // From any class S to any class-type T, provided S is a base class of T
2005 if (target_type.IsSubclassOf (source_type))
2009 // From any interface type S to any interface T provided S is not derived from T
2011 if (source_type.IsInterface && target_type.IsInterface){
2012 if (!target_type.IsSubclassOf (source_type))
2017 // From any class type S to any interface T, provided S is not sealed
2018 // and provided S does not implement T.
2020 if (target_type.IsInterface && !source_type.IsSealed &&
2021 !TypeManager.ImplementsInterface (source_type, target_type))
2025 // From any interface-type S to to any class type T, provided T is not
2026 // sealed, or provided T implements S.
2028 if (source_type.IsInterface &&
2029 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2033 // From an array type S with an element type Se to an array type T with an
2034 // element type Te provided all the following are true:
2035 // * S and T differe only in element type, in other words, S and T
2036 // have the same number of dimensions.
2037 // * Both Se and Te are reference types
2038 // * An explicit referenc conversions exist from Se to Te
2040 if (source_type.IsArray && target_type.IsArray) {
2041 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2043 Type source_element_type = source_type.GetElementType ();
2044 Type target_element_type = target_type.GetElementType ();
2046 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2047 if (ExplicitReferenceConversionExists (source_element_type,
2048 target_element_type))
2054 // From System.Array to any array-type
2055 if (source_type == TypeManager.array_type &&
2056 target_type.IsArray){
2061 // From System delegate to any delegate-type
2063 if (source_type == TypeManager.delegate_type &&
2064 target_type.IsSubclassOf (TypeManager.delegate_type))
2068 // From ICloneable to Array or Delegate types
2070 if (source_type == TypeManager.icloneable_type &&
2071 (target_type == TypeManager.array_type ||
2072 target_type == TypeManager.delegate_type))
2079 /// Implements Explicit Reference conversions
2081 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2083 Type source_type = source.Type;
2084 bool target_is_value_type = target_type.IsValueType;
2087 // From object to any reference type
2089 if (source_type == TypeManager.object_type && !target_is_value_type)
2090 return new ClassCast (source, target_type);
2094 // From any class S to any class-type T, provided S is a base class of T
2096 if (target_type.IsSubclassOf (source_type))
2097 return new ClassCast (source, target_type);
2100 // From any interface type S to any interface T provided S is not derived from T
2102 if (source_type.IsInterface && target_type.IsInterface){
2103 if (TypeManager.ImplementsInterface (source_type, target_type))
2106 return new ClassCast (source, target_type);
2110 // From any class type S to any interface T, provides S is not sealed
2111 // and provided S does not implement T.
2113 if (target_type.IsInterface && !source_type.IsSealed) {
2114 if (TypeManager.ImplementsInterface (source_type, target_type))
2117 return new ClassCast (source, target_type);
2122 // From any interface-type S to to any class type T, provided T is not
2123 // sealed, or provided T implements S.
2125 if (source_type.IsInterface) {
2126 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2127 return new ClassCast (source, target_type);
2132 // From an array type S with an element type Se to an array type T with an
2133 // element type Te provided all the following are true:
2134 // * S and T differe only in element type, in other words, S and T
2135 // have the same number of dimensions.
2136 // * Both Se and Te are reference types
2137 // * An explicit referenc conversions exist from Se to Te
2139 if (source_type.IsArray && target_type.IsArray) {
2140 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2142 Type source_element_type = source_type.GetElementType ();
2143 Type target_element_type = target_type.GetElementType ();
2145 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2146 if (ExplicitReferenceConversionExists (source_element_type,
2147 target_element_type))
2148 return new ClassCast (source, target_type);
2153 // From System.Array to any array-type
2154 if (source_type == TypeManager.array_type &&
2155 target_type.IsArray) {
2156 return new ClassCast (source, target_type);
2160 // From System delegate to any delegate-type
2162 if (source_type == TypeManager.delegate_type &&
2163 target_type.IsSubclassOf (TypeManager.delegate_type))
2164 return new ClassCast (source, target_type);
2167 // From ICloneable to Array or Delegate types
2169 if (source_type == TypeManager.icloneable_type &&
2170 (target_type == TypeManager.array_type ||
2171 target_type == TypeManager.delegate_type))
2172 return new ClassCast (source, target_type);
2178 /// Performs an explicit conversion of the expression `expr' whose
2179 /// type is expr.Type to `target_type'.
2181 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2182 Type target_type, Location loc)
2184 Type expr_type = expr.Type;
2185 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2190 ne = ConvertNumericExplicit (ec, expr, target_type);
2195 // Unboxing conversion.
2197 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2198 return new UnboxCast (expr, target_type);
2203 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2207 // FIXME: Is there any reason we should have EnumConstant
2208 // dealt with here instead of just using always the
2209 // UnderlyingSystemType to wrap the type?
2211 if (expr is EnumConstant)
2212 e = ((EnumConstant) expr).Child;
2214 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2217 Expression t = ConvertImplicit (ec, e, target_type, loc);
2221 return ConvertNumericExplicit (ec, e, target_type);
2224 ne = ConvertReferenceExplicit (expr, target_type);
2229 if (target_type.IsPointer){
2230 if (expr_type.IsPointer)
2231 return new EmptyCast (expr, target_type);
2233 if (expr_type == TypeManager.sbyte_type ||
2234 expr_type == TypeManager.byte_type ||
2235 expr_type == TypeManager.short_type ||
2236 expr_type == TypeManager.ushort_type ||
2237 expr_type == TypeManager.int32_type ||
2238 expr_type == TypeManager.uint32_type ||
2239 expr_type == TypeManager.uint64_type ||
2240 expr_type == TypeManager.int64_type)
2241 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2243 if (expr_type.IsPointer){
2244 if (target_type == TypeManager.sbyte_type ||
2245 target_type == TypeManager.byte_type ||
2246 target_type == TypeManager.short_type ||
2247 target_type == TypeManager.ushort_type ||
2248 target_type == TypeManager.int32_type ||
2249 target_type == TypeManager.uint32_type ||
2250 target_type == TypeManager.uint64_type ||
2251 target_type == TypeManager.int64_type){
2252 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2255 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2260 ce = ConvertNumericExplicit (ec, e, target_type);
2264 // We should always be able to go from an uint32
2265 // implicitly or explicitly to the other integral
2268 throw new Exception ("Internal compiler error");
2273 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2277 Error_CannotConvertType (loc, expr_type, target_type);
2282 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2284 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2285 Type target_type, Location l)
2287 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2292 ne = ConvertNumericExplicit (ec, expr, target_type);
2296 ne = ConvertReferenceExplicit (expr, target_type);
2300 Error_CannotConvertType (l, expr.Type, target_type);
2304 static string ExprClassName (ExprClass c)
2307 case ExprClass.Invalid:
2309 case ExprClass.Value:
2311 case ExprClass.Variable:
2313 case ExprClass.Namespace:
2315 case ExprClass.Type:
2317 case ExprClass.MethodGroup:
2318 return "method group";
2319 case ExprClass.PropertyAccess:
2320 return "property access";
2321 case ExprClass.EventAccess:
2322 return "event access";
2323 case ExprClass.IndexerAccess:
2324 return "indexer access";
2325 case ExprClass.Nothing:
2328 throw new Exception ("Should not happen");
2332 /// Reports that we were expecting `expr' to be of class `expected'
2334 public void Error118 (string expected)
2336 string kind = "Unknown";
2338 kind = ExprClassName (eclass);
2340 Error (118, "Expression denotes a `" + kind +
2341 "' where a `" + expected + "' was expected");
2344 public void Error118 (ResolveFlags flags)
2346 ArrayList valid = new ArrayList (10);
2348 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2349 valid.Add ("variable");
2350 valid.Add ("value");
2353 if ((flags & ResolveFlags.Type) != 0)
2356 if ((flags & ResolveFlags.MethodGroup) != 0)
2357 valid.Add ("method group");
2359 if ((flags & ResolveFlags.SimpleName) != 0)
2360 valid.Add ("simple name");
2362 if (valid.Count == 0)
2363 valid.Add ("unknown");
2365 StringBuilder sb = new StringBuilder ();
2366 for (int i = 0; i < valid.Count; i++) {
2369 else if (i == valid.Count)
2371 sb.Append (valid [i]);
2374 string kind = ExprClassName (eclass);
2376 Error (119, "Expression denotes a `" + kind + "' where " +
2377 "a `" + sb.ToString () + "' was expected");
2380 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2382 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2383 TypeManager.CSharpName (t));
2386 public static void UnsafeError (Location loc)
2388 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2392 /// Converts the IntConstant, UIntConstant, LongConstant or
2393 /// ULongConstant into the integral target_type. Notice
2394 /// that we do not return an `Expression' we do return
2395 /// a boxed integral type.
2397 /// FIXME: Since I added the new constants, we need to
2398 /// also support conversions from CharConstant, ByteConstant,
2399 /// SByteConstant, UShortConstant, ShortConstant
2401 /// This is used by the switch statement, so the domain
2402 /// of work is restricted to the literals above, and the
2403 /// targets are int32, uint32, char, byte, sbyte, ushort,
2404 /// short, uint64 and int64
2406 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2410 if (c.Type == target_type)
2411 return ((Constant) c).GetValue ();
2414 // Make into one of the literals we handle, we dont really care
2415 // about this value as we will just return a few limited types
2417 if (c is EnumConstant)
2418 c = ((EnumConstant)c).WidenToCompilerConstant ();
2420 if (c is IntConstant){
2421 int v = ((IntConstant) c).Value;
2423 if (target_type == TypeManager.uint32_type){
2426 } else if (target_type == TypeManager.char_type){
2427 if (v >= Char.MinValue && v <= Char.MaxValue)
2429 } else if (target_type == TypeManager.byte_type){
2430 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2432 } else if (target_type == TypeManager.sbyte_type){
2433 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2435 } else if (target_type == TypeManager.short_type){
2436 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2438 } else if (target_type == TypeManager.ushort_type){
2439 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2441 } else if (target_type == TypeManager.int64_type)
2443 else if (target_type == TypeManager.uint64_type){
2449 } else if (c is UIntConstant){
2450 uint v = ((UIntConstant) c).Value;
2452 if (target_type == TypeManager.int32_type){
2453 if (v <= Int32.MaxValue)
2455 } else if (target_type == TypeManager.char_type){
2456 if (v >= Char.MinValue && v <= Char.MaxValue)
2458 } else if (target_type == TypeManager.byte_type){
2459 if (v <= Byte.MaxValue)
2461 } else if (target_type == TypeManager.sbyte_type){
2462 if (v <= SByte.MaxValue)
2464 } else if (target_type == TypeManager.short_type){
2465 if (v <= UInt16.MaxValue)
2467 } else if (target_type == TypeManager.ushort_type){
2468 if (v <= UInt16.MaxValue)
2470 } else if (target_type == TypeManager.int64_type)
2472 else if (target_type == TypeManager.uint64_type)
2475 } else if (c is LongConstant){
2476 long v = ((LongConstant) c).Value;
2478 if (target_type == TypeManager.int32_type){
2479 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2481 } else if (target_type == TypeManager.uint32_type){
2482 if (v >= 0 && v <= UInt32.MaxValue)
2484 } else if (target_type == TypeManager.char_type){
2485 if (v >= Char.MinValue && v <= Char.MaxValue)
2487 } else if (target_type == TypeManager.byte_type){
2488 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2490 } else if (target_type == TypeManager.sbyte_type){
2491 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2493 } else if (target_type == TypeManager.short_type){
2494 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2496 } else if (target_type == TypeManager.ushort_type){
2497 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2499 } else if (target_type == TypeManager.uint64_type){
2504 } else if (c is ULongConstant){
2505 ulong v = ((ULongConstant) c).Value;
2507 if (target_type == TypeManager.int32_type){
2508 if (v <= Int32.MaxValue)
2510 } else if (target_type == TypeManager.uint32_type){
2511 if (v <= UInt32.MaxValue)
2513 } else if (target_type == TypeManager.char_type){
2514 if (v >= Char.MinValue && v <= Char.MaxValue)
2516 } else if (target_type == TypeManager.byte_type){
2517 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2519 } else if (target_type == TypeManager.sbyte_type){
2520 if (v <= (int) SByte.MaxValue)
2522 } else if (target_type == TypeManager.short_type){
2523 if (v <= UInt16.MaxValue)
2525 } else if (target_type == TypeManager.ushort_type){
2526 if (v <= UInt16.MaxValue)
2528 } else if (target_type == TypeManager.int64_type){
2529 if (v <= Int64.MaxValue)
2533 } else if (c is ByteConstant){
2534 byte v = ((ByteConstant) c).Value;
2536 if (target_type == TypeManager.int32_type)
2538 else if (target_type == TypeManager.uint32_type)
2540 else if (target_type == TypeManager.char_type)
2542 else if (target_type == TypeManager.sbyte_type){
2543 if (v <= SByte.MaxValue)
2545 } else if (target_type == TypeManager.short_type)
2547 else if (target_type == TypeManager.ushort_type)
2549 else if (target_type == TypeManager.int64_type)
2551 else if (target_type == TypeManager.uint64_type)
2554 } else if (c is SByteConstant){
2555 sbyte v = ((SByteConstant) c).Value;
2557 if (target_type == TypeManager.int32_type)
2559 else if (target_type == TypeManager.uint32_type){
2562 } else if (target_type == TypeManager.char_type){
2565 } else if (target_type == TypeManager.byte_type){
2568 } else if (target_type == TypeManager.short_type)
2570 else if (target_type == TypeManager.ushort_type){
2573 } else if (target_type == TypeManager.int64_type)
2575 else if (target_type == TypeManager.uint64_type){
2580 } else if (c is ShortConstant){
2581 short v = ((ShortConstant) c).Value;
2583 if (target_type == TypeManager.int32_type){
2585 } else if (target_type == TypeManager.uint32_type){
2588 } else if (target_type == TypeManager.char_type){
2591 } else if (target_type == TypeManager.byte_type){
2592 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2594 } else if (target_type == TypeManager.sbyte_type){
2595 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2597 } else if (target_type == TypeManager.ushort_type){
2600 } else if (target_type == TypeManager.int64_type)
2602 else if (target_type == TypeManager.uint64_type)
2606 } else if (c is UShortConstant){
2607 ushort v = ((UShortConstant) c).Value;
2609 if (target_type == TypeManager.int32_type)
2611 else if (target_type == TypeManager.uint32_type)
2613 else if (target_type == TypeManager.char_type){
2614 if (v >= Char.MinValue && v <= Char.MaxValue)
2616 } else if (target_type == TypeManager.byte_type){
2617 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2619 } else if (target_type == TypeManager.sbyte_type){
2620 if (v <= SByte.MaxValue)
2622 } else if (target_type == TypeManager.short_type){
2623 if (v <= Int16.MaxValue)
2625 } else if (target_type == TypeManager.int64_type)
2627 else if (target_type == TypeManager.uint64_type)
2631 } else if (c is CharConstant){
2632 char v = ((CharConstant) c).Value;
2634 if (target_type == TypeManager.int32_type)
2636 else if (target_type == TypeManager.uint32_type)
2638 else if (target_type == TypeManager.byte_type){
2639 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2641 } else if (target_type == TypeManager.sbyte_type){
2642 if (v <= SByte.MaxValue)
2644 } else if (target_type == TypeManager.short_type){
2645 if (v <= Int16.MaxValue)
2647 } else if (target_type == TypeManager.ushort_type)
2649 else if (target_type == TypeManager.int64_type)
2651 else if (target_type == TypeManager.uint64_type)
2656 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2661 // Load the object from the pointer.
2663 public static void LoadFromPtr (ILGenerator ig, Type t)
2665 if (t == TypeManager.int32_type)
2666 ig.Emit (OpCodes.Ldind_I4);
2667 else if (t == TypeManager.uint32_type)
2668 ig.Emit (OpCodes.Ldind_U4);
2669 else if (t == TypeManager.short_type)
2670 ig.Emit (OpCodes.Ldind_I2);
2671 else if (t == TypeManager.ushort_type)
2672 ig.Emit (OpCodes.Ldind_U2);
2673 else if (t == TypeManager.char_type)
2674 ig.Emit (OpCodes.Ldind_U2);
2675 else if (t == TypeManager.byte_type)
2676 ig.Emit (OpCodes.Ldind_U1);
2677 else if (t == TypeManager.sbyte_type)
2678 ig.Emit (OpCodes.Ldind_I1);
2679 else if (t == TypeManager.uint64_type)
2680 ig.Emit (OpCodes.Ldind_I8);
2681 else if (t == TypeManager.int64_type)
2682 ig.Emit (OpCodes.Ldind_I8);
2683 else if (t == TypeManager.float_type)
2684 ig.Emit (OpCodes.Ldind_R4);
2685 else if (t == TypeManager.double_type)
2686 ig.Emit (OpCodes.Ldind_R8);
2687 else if (t == TypeManager.bool_type)
2688 ig.Emit (OpCodes.Ldind_I1);
2689 else if (t == TypeManager.intptr_type)
2690 ig.Emit (OpCodes.Ldind_I);
2691 else if (TypeManager.IsEnumType (t)) {
2692 if (t == TypeManager.enum_type)
2693 ig.Emit (OpCodes.Ldind_Ref);
2695 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2696 } else if (t.IsValueType)
2697 ig.Emit (OpCodes.Ldobj, t);
2699 ig.Emit (OpCodes.Ldind_Ref);
2703 // The stack contains the pointer and the value of type `type'
2705 public static void StoreFromPtr (ILGenerator ig, Type type)
2708 type = TypeManager.EnumToUnderlying (type);
2709 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2710 ig.Emit (OpCodes.Stind_I4);
2711 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2712 ig.Emit (OpCodes.Stind_I8);
2713 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2714 type == TypeManager.ushort_type)
2715 ig.Emit (OpCodes.Stind_I2);
2716 else if (type == TypeManager.float_type)
2717 ig.Emit (OpCodes.Stind_R4);
2718 else if (type == TypeManager.double_type)
2719 ig.Emit (OpCodes.Stind_R8);
2720 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2721 type == TypeManager.bool_type)
2722 ig.Emit (OpCodes.Stind_I1);
2723 else if (type == TypeManager.intptr_type)
2724 ig.Emit (OpCodes.Stind_I);
2725 else if (type.IsValueType)
2726 ig.Emit (OpCodes.Stobj, type);
2728 ig.Emit (OpCodes.Stind_Ref);
2732 // Returns the size of type `t' if known, otherwise, 0
2734 public static int GetTypeSize (Type t)
2736 t = TypeManager.TypeToCoreType (t);
2737 if (t == TypeManager.int32_type ||
2738 t == TypeManager.uint32_type ||
2739 t == TypeManager.float_type)
2741 else if (t == TypeManager.int64_type ||
2742 t == TypeManager.uint64_type ||
2743 t == TypeManager.double_type)
2745 else if (t == TypeManager.byte_type ||
2746 t == TypeManager.sbyte_type ||
2747 t == TypeManager.bool_type)
2749 else if (t == TypeManager.short_type ||
2750 t == TypeManager.char_type ||
2751 t == TypeManager.ushort_type)
2758 // Default implementation of IAssignMethod.CacheTemporaries
2760 public void CacheTemporaries (EmitContext ec)
2764 static void Error_NegativeArrayIndex (Location loc)
2766 Report.Error (284, loc, "Can not create array with a negative size");
2770 // Converts `source' to an int, uint, long or ulong.
2772 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2776 bool old_checked = ec.CheckState;
2777 ec.CheckState = true;
2779 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2780 if (target == null){
2781 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2782 if (target == null){
2783 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2784 if (target == null){
2785 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2787 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2791 ec.CheckState = old_checked;
2794 // Only positive constants are allowed at compile time
2796 if (target is Constant){
2797 if (target is IntConstant){
2798 if (((IntConstant) target).Value < 0){
2799 Error_NegativeArrayIndex (loc);
2804 if (target is LongConstant){
2805 if (((LongConstant) target).Value < 0){
2806 Error_NegativeArrayIndex (loc);
2819 /// This is just a base class for expressions that can
2820 /// appear on statements (invocations, object creation,
2821 /// assignments, post/pre increment and decrement). The idea
2822 /// being that they would support an extra Emition interface that
2823 /// does not leave a result on the stack.
2825 public abstract class ExpressionStatement : Expression {
2828 /// Requests the expression to be emitted in a `statement'
2829 /// context. This means that no new value is left on the
2830 /// stack after invoking this method (constrasted with
2831 /// Emit that will always leave a value on the stack).
2833 public abstract void EmitStatement (EmitContext ec);
2837 /// This kind of cast is used to encapsulate the child
2838 /// whose type is child.Type into an expression that is
2839 /// reported to return "return_type". This is used to encapsulate
2840 /// expressions which have compatible types, but need to be dealt
2841 /// at higher levels with.
2843 /// For example, a "byte" expression could be encapsulated in one
2844 /// of these as an "unsigned int". The type for the expression
2845 /// would be "unsigned int".
2848 public class EmptyCast : Expression {
2849 protected Expression child;
2851 public EmptyCast (Expression child, Type return_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)
2873 /// This class is used to wrap literals which belong inside Enums
2875 public class EnumConstant : Constant {
2876 public Constant Child;
2878 public EnumConstant (Constant child, Type enum_type)
2880 eclass = child.eclass;
2885 public override Expression DoResolve (EmitContext ec)
2887 // This should never be invoked, we are born in fully
2888 // initialized state.
2893 public override void Emit (EmitContext ec)
2898 public override object GetValue ()
2900 return Child.GetValue ();
2904 // Converts from one of the valid underlying types for an enumeration
2905 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2906 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2908 public Constant WidenToCompilerConstant ()
2910 Type t = TypeManager.EnumToUnderlying (Child.Type);
2911 object v = ((Constant) Child).GetValue ();;
2913 if (t == TypeManager.int32_type)
2914 return new IntConstant ((int) v);
2915 if (t == TypeManager.uint32_type)
2916 return new UIntConstant ((uint) v);
2917 if (t == TypeManager.int64_type)
2918 return new LongConstant ((long) v);
2919 if (t == TypeManager.uint64_type)
2920 return new ULongConstant ((ulong) v);
2921 if (t == TypeManager.short_type)
2922 return new ShortConstant ((short) v);
2923 if (t == TypeManager.ushort_type)
2924 return new UShortConstant ((ushort) v);
2925 if (t == TypeManager.byte_type)
2926 return new ByteConstant ((byte) v);
2927 if (t == TypeManager.sbyte_type)
2928 return new SByteConstant ((sbyte) v);
2930 throw new Exception ("Invalid enumeration underlying type: " + t);
2934 // Extracts the value in the enumeration on its native representation
2936 public object GetPlainValue ()
2938 Type t = TypeManager.EnumToUnderlying (Child.Type);
2939 object v = ((Constant) Child).GetValue ();;
2941 if (t == TypeManager.int32_type)
2943 if (t == TypeManager.uint32_type)
2945 if (t == TypeManager.int64_type)
2947 if (t == TypeManager.uint64_type)
2949 if (t == TypeManager.short_type)
2951 if (t == TypeManager.ushort_type)
2953 if (t == TypeManager.byte_type)
2955 if (t == TypeManager.sbyte_type)
2961 public override string AsString ()
2963 return Child.AsString ();
2966 public override DoubleConstant ConvertToDouble ()
2968 return Child.ConvertToDouble ();
2971 public override FloatConstant ConvertToFloat ()
2973 return Child.ConvertToFloat ();
2976 public override ULongConstant ConvertToULong ()
2978 return Child.ConvertToULong ();
2981 public override LongConstant ConvertToLong ()
2983 return Child.ConvertToLong ();
2986 public override UIntConstant ConvertToUInt ()
2988 return Child.ConvertToUInt ();
2991 public override IntConstant ConvertToInt ()
2993 return Child.ConvertToInt ();
2998 /// This kind of cast is used to encapsulate Value Types in objects.
3000 /// The effect of it is to box the value type emitted by the previous
3003 public class BoxedCast : EmptyCast {
3005 public BoxedCast (Expression expr)
3006 : base (expr, TypeManager.object_type)
3010 public override Expression DoResolve (EmitContext ec)
3012 // This should never be invoked, we are born in fully
3013 // initialized state.
3018 public override void Emit (EmitContext ec)
3022 ec.ig.Emit (OpCodes.Box, child.Type);
3026 public class UnboxCast : EmptyCast {
3027 public UnboxCast (Expression expr, Type return_type)
3028 : base (expr, return_type)
3032 public override Expression DoResolve (EmitContext ec)
3034 // This should never be invoked, we are born in fully
3035 // initialized state.
3040 public override void Emit (EmitContext ec)
3043 ILGenerator ig = ec.ig;
3046 ig.Emit (OpCodes.Unbox, t);
3048 LoadFromPtr (ig, t);
3053 /// This is used to perform explicit numeric conversions.
3055 /// Explicit numeric conversions might trigger exceptions in a checked
3056 /// context, so they should generate the conv.ovf opcodes instead of
3059 public class ConvCast : EmptyCast {
3060 public enum Mode : byte {
3061 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3063 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3064 U2_I1, U2_U1, U2_I2, U2_CH,
3065 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3066 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3067 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3068 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3069 CH_I1, CH_U1, CH_I2,
3070 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3071 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3077 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3078 : base (child, return_type)
3080 checked_state = ec.CheckState;
3084 public override Expression DoResolve (EmitContext ec)
3086 // This should never be invoked, we are born in fully
3087 // initialized state.
3092 public override void Emit (EmitContext ec)
3094 ILGenerator ig = ec.ig;
3100 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3101 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3102 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3103 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3104 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3106 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3107 case Mode.U1_CH: /* nothing */ break;
3109 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3110 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3111 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3112 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3113 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3114 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3116 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3117 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3118 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3119 case Mode.U2_CH: /* nothing */ break;
3121 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3122 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3123 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3124 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3125 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3126 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3127 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3129 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3130 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3131 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3132 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3133 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3134 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3136 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3137 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3138 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3139 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3140 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3141 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3142 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3143 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3145 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3146 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3147 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3148 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3149 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3150 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3151 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3152 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3154 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3155 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3156 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3158 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3159 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3160 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3161 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3162 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3163 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3164 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3165 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3166 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3168 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3169 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3170 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3171 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3172 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3173 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3174 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3175 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3176 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3177 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3181 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3182 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3183 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3184 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3185 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3187 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3188 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3190 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3191 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3192 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3193 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3194 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3195 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3197 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3198 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3199 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3200 case Mode.U2_CH: /* nothing */ break;
3202 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3203 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3204 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3205 case Mode.I4_U4: /* nothing */ break;
3206 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3207 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3208 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3210 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3211 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3212 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3213 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3214 case Mode.U4_I4: /* nothing */ break;
3215 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3217 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3218 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3219 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3220 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3221 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3222 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3223 case Mode.I8_U8: /* nothing */ break;
3224 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3226 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3227 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3228 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3229 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3230 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3231 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3232 case Mode.U8_I8: /* nothing */ break;
3233 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3235 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3236 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3237 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3239 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3240 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3241 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3242 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3243 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3244 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3245 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3246 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3247 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3249 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3250 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3251 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3252 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3253 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3254 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3255 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3256 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3257 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3258 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3264 public class OpcodeCast : EmptyCast {
3268 public OpcodeCast (Expression child, Type return_type, OpCode op)
3269 : base (child, return_type)
3273 second_valid = false;
3276 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3277 : base (child, return_type)
3282 second_valid = true;
3285 public override Expression DoResolve (EmitContext ec)
3287 // This should never be invoked, we are born in fully
3288 // initialized state.
3293 public override void Emit (EmitContext ec)
3304 /// This kind of cast is used to encapsulate a child and cast it
3305 /// to the class requested
3307 public class ClassCast : EmptyCast {
3308 public ClassCast (Expression child, Type return_type)
3309 : base (child, return_type)
3314 public override Expression DoResolve (EmitContext ec)
3316 // This should never be invoked, we are born in fully
3317 // initialized state.
3322 public override void Emit (EmitContext ec)
3326 ec.ig.Emit (OpCodes.Castclass, type);
3332 /// SimpleName expressions are initially formed of a single
3333 /// word and it only happens at the beginning of the expression.
3337 /// The expression will try to be bound to a Field, a Method
3338 /// group or a Property. If those fail we pass the name to our
3339 /// caller and the SimpleName is compounded to perform a type
3340 /// lookup. The idea behind this process is that we want to avoid
3341 /// creating a namespace map from the assemblies, as that requires
3342 /// the GetExportedTypes function to be called and a hashtable to
3343 /// be constructed which reduces startup time. If later we find
3344 /// that this is slower, we should create a `NamespaceExpr' expression
3345 /// that fully participates in the resolution process.
3347 /// For example `System.Console.WriteLine' is decomposed into
3348 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3350 /// The first SimpleName wont produce a match on its own, so it will
3352 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3354 /// System.Console will produce a TypeExpr match.
3356 /// The downside of this is that we might be hitting `LookupType' too many
3357 /// times with this scheme.
3359 public class SimpleName : Expression, ITypeExpression {
3360 public readonly string Name;
3362 public SimpleName (string name, Location l)
3368 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3370 if (ec.IsFieldInitializer)
3373 "A field initializer cannot reference the non-static field, " +
3374 "method or property `"+name+"'");
3378 "An object reference is required " +
3379 "for the non-static field `"+name+"'");
3383 // Checks whether we are trying to access an instance
3384 // property, method or field from a static body.
3386 Expression MemberStaticCheck (EmitContext ec, Expression e)
3388 if (e is IMemberExpr){
3389 IMemberExpr member = (IMemberExpr) e;
3391 if (!member.IsStatic){
3392 Error_ObjectRefRequired (ec, loc, Name);
3400 public override Expression DoResolve (EmitContext ec)
3402 return SimpleNameResolve (ec, null, false);
3405 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3407 return SimpleNameResolve (ec, right_side, false);
3411 public Expression DoResolveAllowStatic (EmitContext ec)
3413 return SimpleNameResolve (ec, null, true);
3416 public Expression DoResolveType (EmitContext ec)
3419 // Stage 3: Lookup symbol in the various namespaces.
3421 DeclSpace ds = ec.DeclSpace;
3425 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3426 return new TypeExpr (t, loc);
3429 // Stage 2 part b: Lookup up if we are an alias to a type
3432 // Since we are cheating: we only do the Alias lookup for
3433 // namespaces if the name does not include any dots in it
3436 alias_value = ec.DeclSpace.LookupAlias (Name);
3438 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3439 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3440 return new TypeExpr (t, loc);
3442 // we have alias value, but it isn't Type, so try if it's namespace
3443 return new SimpleName (alias_value, loc);
3446 if (ec.ResolvingTypeTree){
3447 Type dt = ec.DeclSpace.FindType (Name);
3449 return new TypeExpr (dt, loc);
3452 // No match, maybe our parent can compose us
3453 // into something meaningful.
3458 /// 7.5.2: Simple Names.
3460 /// Local Variables and Parameters are handled at
3461 /// parse time, so they never occur as SimpleNames.
3463 /// The `allow_static' flag is used by MemberAccess only
3464 /// and it is used to inform us that it is ok for us to
3465 /// avoid the static check, because MemberAccess might end
3466 /// up resolving the Name as a Type name and the access as
3467 /// a static type access.
3469 /// ie: Type Type; .... { Type.GetType (""); }
3471 /// Type is both an instance variable and a Type; Type.GetType
3472 /// is the static method not an instance method of type.
3474 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3476 Expression e = null;
3479 // Stage 1: Performed by the parser (binding to locals or parameters).
3481 Block current_block = ec.CurrentBlock;
3482 if (current_block != null && current_block.IsVariableDefined (Name)){
3483 LocalVariableReference var;
3485 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3487 if (right_side != null)
3488 return var.ResolveLValue (ec, right_side);
3490 return var.Resolve (ec);
3493 if (current_block != null){
3495 Parameter par = null;
3496 Parameters pars = current_block.Parameters;
3498 par = pars.GetParameterByName (Name, out idx);
3501 ParameterReference param;
3503 param = new ParameterReference (pars, idx, Name, loc);
3505 if (right_side != null)
3506 return param.ResolveLValue (ec, right_side);
3508 return param.Resolve (ec);
3513 // Stage 2: Lookup members
3517 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3518 // Hence we have two different cases
3521 DeclSpace lookup_ds = ec.DeclSpace;
3523 if (lookup_ds.TypeBuilder == null)
3526 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3531 // Classes/structs keep looking, enums break
3533 if (lookup_ds is TypeContainer)
3534 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3537 } while (lookup_ds != null);
3539 if (e == null && ec.ContainerType != null)
3540 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3543 return DoResolveType (ec);
3548 if (e is IMemberExpr) {
3549 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3553 IMemberExpr me = e as IMemberExpr;
3557 // This fails if ResolveMemberAccess() was unable to decide whether
3558 // it's a field or a type of the same name.
3559 if (!me.IsStatic && (me.InstanceExpression == null))
3562 if (right_side != null)
3563 e = e.DoResolveLValue (ec, right_side);
3565 e = e.DoResolve (ec);
3570 if (ec.IsStatic || ec.IsFieldInitializer){
3574 return MemberStaticCheck (ec, e);
3579 public override void Emit (EmitContext ec)
3582 // If this is ever reached, then we failed to
3583 // find the name as a namespace
3586 Error (103, "The name `" + Name +
3587 "' does not exist in the class `" +
3588 ec.DeclSpace.Name + "'");
3591 public override string ToString ()
3598 /// Fully resolved expression that evaluates to a type
3600 public class TypeExpr : Expression, ITypeExpression {
3601 public TypeExpr (Type t, Location l)
3604 eclass = ExprClass.Type;
3608 public virtual Expression DoResolveType (EmitContext ec)
3613 override public Expression DoResolve (EmitContext ec)
3618 override public void Emit (EmitContext ec)
3620 throw new Exception ("Should never be called");
3625 /// Used to create types from a fully qualified name. These are just used
3626 /// by the parser to setup the core types. A TypeLookupExpression is always
3627 /// classified as a type.
3629 public class TypeLookupExpression : TypeExpr {
3632 public TypeLookupExpression (string name) : base (null, Location.Null)
3637 public override Expression DoResolveType (EmitContext ec)
3640 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3644 public override Expression DoResolve (EmitContext ec)
3646 return DoResolveType (ec);
3649 public override void Emit (EmitContext ec)
3651 throw new Exception ("Should never be called");
3654 public override string ToString ()
3661 /// MethodGroup Expression.
3663 /// This is a fully resolved expression that evaluates to a type
3665 public class MethodGroupExpr : Expression, IMemberExpr {
3666 public MethodBase [] Methods;
3667 Expression instance_expression = null;
3669 public MethodGroupExpr (MemberInfo [] mi, Location l)
3671 Methods = new MethodBase [mi.Length];
3672 mi.CopyTo (Methods, 0);
3673 eclass = ExprClass.MethodGroup;
3674 type = TypeManager.object_type;
3678 public MethodGroupExpr (ArrayList list, Location l)
3680 Methods = new MethodBase [list.Count];
3683 list.CopyTo (Methods, 0);
3685 foreach (MemberInfo m in list){
3686 if (!(m is MethodBase)){
3687 Console.WriteLine ("Name " + m.Name);
3688 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3694 eclass = ExprClass.MethodGroup;
3695 type = TypeManager.object_type;
3699 // `A method group may have associated an instance expression'
3701 public Expression InstanceExpression {
3703 return instance_expression;
3707 instance_expression = value;
3711 public string Name {
3713 return Methods [0].Name;
3717 public bool IsInstance {
3719 foreach (MethodBase mb in Methods)
3727 public bool IsStatic {
3729 foreach (MethodBase mb in Methods)
3737 override public Expression DoResolve (EmitContext ec)
3742 public void ReportUsageError ()
3744 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3745 Methods [0].Name + "()' is referenced without parentheses");
3748 override public void Emit (EmitContext ec)
3750 ReportUsageError ();
3753 bool RemoveMethods (bool keep_static)
3755 ArrayList smethods = new ArrayList ();
3757 foreach (MethodBase mb in Methods){
3758 if (mb.IsStatic == keep_static)
3762 if (smethods.Count == 0)
3765 Methods = new MethodBase [smethods.Count];
3766 smethods.CopyTo (Methods, 0);
3772 /// Removes any instance methods from the MethodGroup, returns
3773 /// false if the resulting set is empty.
3775 public bool RemoveInstanceMethods ()
3777 return RemoveMethods (true);
3781 /// Removes any static methods from the MethodGroup, returns
3782 /// false if the resulting set is empty.
3784 public bool RemoveStaticMethods ()
3786 return RemoveMethods (false);
3791 /// Fully resolved expression that evaluates to a Field
3793 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3794 public readonly FieldInfo FieldInfo;
3795 Expression instance_expr;
3797 public FieldExpr (FieldInfo fi, Location l)
3800 eclass = ExprClass.Variable;
3801 type = fi.FieldType;
3805 public string Name {
3807 return FieldInfo.Name;
3811 public bool IsInstance {
3813 return !FieldInfo.IsStatic;
3817 public bool IsStatic {
3819 return FieldInfo.IsStatic;
3823 public Expression InstanceExpression {
3825 return instance_expr;
3829 instance_expr = value;
3833 override public Expression DoResolve (EmitContext ec)
3835 if (!FieldInfo.IsStatic){
3836 if (instance_expr == null){
3837 throw new Exception ("non-static FieldExpr without instance var\n" +
3838 "You have to assign the Instance variable\n" +
3839 "Of the FieldExpr to set this\n");
3842 // Resolve the field's instance expression while flow analysis is turned
3843 // off: when accessing a field "a.b", we must check whether the field
3844 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3845 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3846 ResolveFlags.DisableFlowAnalysis);
3847 if (instance_expr == null)
3851 // If the instance expression is a local variable or parameter.
3852 IVariable var = instance_expr as IVariable;
3853 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3859 void Report_AssignToReadonly (bool is_instance)
3864 msg = "Readonly field can not be assigned outside " +
3865 "of constructor or variable initializer";
3867 msg = "A static readonly field can only be assigned in " +
3868 "a static constructor";
3870 Report.Error (is_instance ? 191 : 198, loc, msg);
3873 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3875 IVariable var = instance_expr as IVariable;
3877 var.SetFieldAssigned (ec, FieldInfo.Name);
3879 Expression e = DoResolve (ec);
3884 if (!FieldInfo.IsInitOnly)
3888 // InitOnly fields can only be assigned in constructors
3891 if (ec.IsConstructor)
3894 Report_AssignToReadonly (true);
3899 override public void Emit (EmitContext ec)
3901 ILGenerator ig = ec.ig;
3902 bool is_volatile = false;
3904 if (FieldInfo is FieldBuilder){
3905 FieldBase f = TypeManager.GetField (FieldInfo);
3907 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3910 f.status |= Field.Status.USED;
3913 if (FieldInfo.IsStatic){
3915 ig.Emit (OpCodes.Volatile);
3917 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3919 if (instance_expr.Type.IsValueType){
3921 LocalTemporary tempo = null;
3923 if (!(instance_expr is IMemoryLocation)){
3924 tempo = new LocalTemporary (
3925 ec, instance_expr.Type);
3927 InstanceExpression.Emit (ec);
3931 ml = (IMemoryLocation) instance_expr;
3933 ml.AddressOf (ec, AddressOp.Load);
3935 instance_expr.Emit (ec);
3938 ig.Emit (OpCodes.Volatile);
3940 ig.Emit (OpCodes.Ldfld, FieldInfo);
3944 public void EmitAssign (EmitContext ec, Expression source)
3946 FieldAttributes fa = FieldInfo.Attributes;
3947 bool is_static = (fa & FieldAttributes.Static) != 0;
3948 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3949 ILGenerator ig = ec.ig;
3951 if (is_readonly && !ec.IsConstructor){
3952 Report_AssignToReadonly (!is_static);
3957 Expression instance = instance_expr;
3959 if (instance.Type.IsValueType){
3960 if (instance is IMemoryLocation){
3961 IMemoryLocation ml = (IMemoryLocation) instance;
3963 ml.AddressOf (ec, AddressOp.Store);
3965 throw new Exception ("The " + instance + " of type " +
3967 " represents a ValueType and does " +
3968 "not implement IMemoryLocation");
3974 if (FieldInfo is FieldBuilder){
3975 FieldBase f = TypeManager.GetField (FieldInfo);
3977 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3978 ig.Emit (OpCodes.Volatile);
3982 ig.Emit (OpCodes.Stsfld, FieldInfo);
3984 ig.Emit (OpCodes.Stfld, FieldInfo);
3986 if (FieldInfo is FieldBuilder){
3987 FieldBase f = TypeManager.GetField (FieldInfo);
3989 f.status |= Field.Status.ASSIGNED;
3993 public void AddressOf (EmitContext ec, AddressOp mode)
3995 ILGenerator ig = ec.ig;
3997 if (FieldInfo is FieldBuilder){
3998 FieldBase f = TypeManager.GetField (FieldInfo);
3999 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4000 ig.Emit (OpCodes.Volatile);
4003 if (FieldInfo is FieldBuilder){
4004 FieldBase f = TypeManager.GetField (FieldInfo);
4006 if ((mode & AddressOp.Store) != 0)
4007 f.status |= Field.Status.ASSIGNED;
4008 if ((mode & AddressOp.Load) != 0)
4009 f.status |= Field.Status.USED;
4013 // Handle initonly fields specially: make a copy and then
4014 // get the address of the copy.
4016 if (FieldInfo.IsInitOnly){
4017 if (ec.IsConstructor) {
4018 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4023 local = ig.DeclareLocal (type);
4024 ig.Emit (OpCodes.Stloc, local);
4025 ig.Emit (OpCodes.Ldloca, local);
4030 if (FieldInfo.IsStatic)
4031 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4033 if (instance_expr is IMemoryLocation)
4034 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4036 instance_expr.Emit (ec);
4037 ig.Emit (OpCodes.Ldflda, FieldInfo);
4043 /// Expression that evaluates to a Property. The Assign class
4044 /// might set the `Value' expression if we are in an assignment.
4046 /// This is not an LValue because we need to re-write the expression, we
4047 /// can not take data from the stack and store it.
4049 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4050 public readonly PropertyInfo PropertyInfo;
4052 MethodInfo [] Accessors;
4055 Expression instance_expr;
4057 public PropertyExpr (PropertyInfo pi, Location l)
4060 eclass = ExprClass.PropertyAccess;
4063 Accessors = TypeManager.GetAccessors (pi);
4065 if (Accessors != null)
4066 foreach (MethodInfo mi in Accessors){
4072 Accessors = new MethodInfo [2];
4074 type = TypeManager.TypeToCoreType (pi.PropertyType);
4077 public string Name {
4079 return PropertyInfo.Name;
4083 public bool IsInstance {
4089 public bool IsStatic {
4096 // The instance expression associated with this expression
4098 public Expression InstanceExpression {
4100 instance_expr = value;
4104 return instance_expr;
4108 public bool VerifyAssignable ()
4110 if (!PropertyInfo.CanWrite){
4111 Report.Error (200, loc,
4112 "The property `" + PropertyInfo.Name +
4113 "' can not be assigned to, as it has not set accessor");
4120 override public Expression DoResolve (EmitContext ec)
4122 if (!PropertyInfo.CanRead){
4123 Report.Error (154, loc,
4124 "The property `" + PropertyInfo.Name +
4125 "' can not be used in " +
4126 "this context because it lacks a get accessor");
4133 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4135 if (!PropertyInfo.CanWrite){
4136 Report.Error (154, loc,
4137 "The property `" + PropertyInfo.Name +
4138 "' can not be used in " +
4139 "this context because it lacks a set accessor");
4146 override public void Emit (EmitContext ec)
4148 MethodInfo method = Accessors [0];
4151 // Special case: length of single dimension array is turned into ldlen
4153 if (method == TypeManager.int_array_get_length){
4154 Type iet = instance_expr.Type;
4157 // System.Array.Length can be called, but the Type does not
4158 // support invoking GetArrayRank, so test for that case first
4160 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4161 instance_expr.Emit (ec);
4162 ec.ig.Emit (OpCodes.Ldlen);
4167 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
4172 // Implements the IAssignMethod interface for assignments
4174 public void EmitAssign (EmitContext ec, Expression source)
4176 Argument arg = new Argument (source, Argument.AType.Expression);
4177 ArrayList args = new ArrayList ();
4180 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
4183 override public void EmitStatement (EmitContext ec)
4186 ec.ig.Emit (OpCodes.Pop);
4191 /// Fully resolved expression that evaluates to an Event
4193 public class EventExpr : Expression, IMemberExpr {
4194 public readonly EventInfo EventInfo;
4195 public Expression instance_expr;
4198 MethodInfo add_accessor, remove_accessor;
4200 public EventExpr (EventInfo ei, Location loc)
4204 eclass = ExprClass.EventAccess;
4206 add_accessor = TypeManager.GetAddMethod (ei);
4207 remove_accessor = TypeManager.GetRemoveMethod (ei);
4209 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4212 if (EventInfo is MyEventBuilder)
4213 type = ((MyEventBuilder) EventInfo).EventType;
4215 type = EventInfo.EventHandlerType;
4218 public string Name {
4220 return EventInfo.Name;
4224 public bool IsInstance {
4230 public bool IsStatic {
4236 public Expression InstanceExpression {
4238 return instance_expr;
4242 instance_expr = value;
4246 public override Expression DoResolve (EmitContext ec)
4248 // We are born fully resolved
4252 public override void Emit (EmitContext ec)
4254 throw new Exception ("Should not happen I think");
4257 public void EmitAddOrRemove (EmitContext ec, Expression source)
4259 Expression handler = ((Binary) source).Right;
4261 Argument arg = new Argument (handler, Argument.AType.Expression);
4262 ArrayList args = new ArrayList ();
4266 if (((Binary) source).Oper == Binary.Operator.Addition)
4267 Invocation.EmitCall (
4268 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4270 Invocation.EmitCall (
4271 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);