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 == target_type)
1047 // First numeric conversions
1049 if (expr_type == TypeManager.sbyte_type){
1051 // From sbyte to short, int, long, float, double.
1053 if ((target_type == TypeManager.int32_type) ||
1054 (target_type == TypeManager.int64_type) ||
1055 (target_type == TypeManager.double_type) ||
1056 (target_type == TypeManager.float_type) ||
1057 (target_type == TypeManager.short_type) ||
1058 (target_type == TypeManager.decimal_type))
1061 } else if (expr_type == TypeManager.byte_type){
1063 // From byte to short, ushort, int, uint, long, ulong, float, double
1065 if ((target_type == TypeManager.short_type) ||
1066 (target_type == TypeManager.ushort_type) ||
1067 (target_type == TypeManager.int32_type) ||
1068 (target_type == TypeManager.uint32_type) ||
1069 (target_type == TypeManager.uint64_type) ||
1070 (target_type == TypeManager.int64_type) ||
1071 (target_type == TypeManager.float_type) ||
1072 (target_type == TypeManager.double_type) ||
1073 (target_type == TypeManager.decimal_type))
1076 } else if (expr_type == TypeManager.short_type){
1078 // From short to int, long, float, double
1080 if ((target_type == TypeManager.int32_type) ||
1081 (target_type == TypeManager.int64_type) ||
1082 (target_type == TypeManager.double_type) ||
1083 (target_type == TypeManager.float_type) ||
1084 (target_type == TypeManager.decimal_type))
1087 } else if (expr_type == TypeManager.ushort_type){
1089 // From ushort to int, uint, long, ulong, float, double
1091 if ((target_type == TypeManager.uint32_type) ||
1092 (target_type == TypeManager.uint64_type) ||
1093 (target_type == TypeManager.int32_type) ||
1094 (target_type == TypeManager.int64_type) ||
1095 (target_type == TypeManager.double_type) ||
1096 (target_type == TypeManager.float_type) ||
1097 (target_type == TypeManager.decimal_type))
1100 } else if (expr_type == TypeManager.int32_type){
1102 // From int to long, float, double
1104 if ((target_type == TypeManager.int64_type) ||
1105 (target_type == TypeManager.double_type) ||
1106 (target_type == TypeManager.float_type) ||
1107 (target_type == TypeManager.decimal_type))
1110 } else if (expr_type == TypeManager.uint32_type){
1112 // From uint to long, ulong, float, double
1114 if ((target_type == TypeManager.int64_type) ||
1115 (target_type == TypeManager.uint64_type) ||
1116 (target_type == TypeManager.double_type) ||
1117 (target_type == TypeManager.float_type) ||
1118 (target_type == TypeManager.decimal_type))
1121 } else if ((expr_type == TypeManager.uint64_type) ||
1122 (expr_type == TypeManager.int64_type)) {
1124 // From long/ulong to float, double
1126 if ((target_type == TypeManager.double_type) ||
1127 (target_type == TypeManager.float_type) ||
1128 (target_type == TypeManager.decimal_type))
1131 } else if (expr_type == TypeManager.char_type){
1133 // From char to ushort, int, uint, long, ulong, float, double
1135 if ((target_type == TypeManager.ushort_type) ||
1136 (target_type == TypeManager.int32_type) ||
1137 (target_type == TypeManager.uint32_type) ||
1138 (target_type == TypeManager.uint64_type) ||
1139 (target_type == TypeManager.int64_type) ||
1140 (target_type == TypeManager.float_type) ||
1141 (target_type == TypeManager.double_type) ||
1142 (target_type == TypeManager.decimal_type))
1145 } else if (expr_type == TypeManager.float_type){
1149 if (target_type == TypeManager.double_type)
1153 if (ImplicitReferenceConversionExists (expr, target_type))
1156 if (expr is IntConstant){
1157 int value = ((IntConstant) expr).Value;
1159 if (target_type == TypeManager.sbyte_type){
1160 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1162 } else if (target_type == TypeManager.byte_type){
1163 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1165 } else if (target_type == TypeManager.short_type){
1166 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1168 } else if (target_type == TypeManager.ushort_type){
1169 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1171 } else if (target_type == TypeManager.uint32_type){
1174 } else if (target_type == TypeManager.uint64_type){
1176 // we can optimize this case: a positive int32
1177 // always fits on a uint64. But we need an opcode
1184 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1188 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1190 // Try the implicit constant expression conversion
1191 // from long to ulong, instead of a nice routine,
1192 // we just inline it
1194 long v = ((LongConstant) expr).Value;
1199 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1200 IntLiteral i = (IntLiteral) expr;
1209 // Used internally by FindMostEncompassedType, this is used
1210 // to avoid creating lots of objects in the tight loop inside
1211 // FindMostEncompassedType
1213 static EmptyExpression priv_fmet_param;
1216 /// Finds "most encompassed type" according to the spec (13.4.2)
1217 /// amongst the methods in the MethodGroupExpr
1219 static Type FindMostEncompassedType (ArrayList types)
1223 if (priv_fmet_param == null)
1224 priv_fmet_param = new EmptyExpression ();
1226 foreach (Type t in types){
1227 priv_fmet_param.SetType (t);
1234 if (StandardConversionExists (priv_fmet_param, best))
1242 // Used internally by FindMostEncompassingType, this is used
1243 // to avoid creating lots of objects in the tight loop inside
1244 // FindMostEncompassingType
1246 static EmptyExpression priv_fmee_ret;
1249 /// Finds "most encompassing type" according to the spec (13.4.2)
1250 /// amongst the types in the given set
1252 static Type FindMostEncompassingType (ArrayList types)
1256 if (priv_fmee_ret == null)
1257 priv_fmee_ret = new EmptyExpression ();
1259 foreach (Type t in types){
1260 priv_fmee_ret.SetType (best);
1267 if (StandardConversionExists (priv_fmee_ret, t))
1275 // Used to avoid creating too many objects
1277 static EmptyExpression priv_fms_expr;
1280 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1281 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1282 /// for explicit and implicit conversion operators.
1284 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1285 bool apply_explicit_conv_rules,
1288 ArrayList src_types_set = new ArrayList ();
1290 if (priv_fms_expr == null)
1291 priv_fms_expr = new EmptyExpression ();
1294 // If any operator converts from S then Sx = S
1296 foreach (MethodBase mb in me.Methods){
1297 ParameterData pd = Invocation.GetParameterData (mb);
1298 Type param_type = pd.ParameterType (0);
1300 if (param_type == source_type)
1303 if (apply_explicit_conv_rules) {
1306 // Find the set of applicable user-defined conversion operators, U. This set
1308 // user-defined implicit or explicit conversion operators declared by
1309 // the classes or structs in D that convert from a type encompassing
1310 // or encompassed by S to a type encompassing or encompassed by T
1312 priv_fms_expr.SetType (param_type);
1313 if (StandardConversionExists (priv_fms_expr, source_type))
1314 src_types_set.Add (param_type);
1316 priv_fms_expr.SetType (source_type);
1317 if (StandardConversionExists (priv_fms_expr, param_type))
1318 src_types_set.Add (param_type);
1322 // Only if S is encompassed by param_type
1324 priv_fms_expr.SetType (source_type);
1325 if (StandardConversionExists (priv_fms_expr, param_type))
1326 src_types_set.Add (param_type);
1331 // Explicit Conv rules
1333 if (apply_explicit_conv_rules) {
1334 ArrayList candidate_set = new ArrayList ();
1336 foreach (Type param_type in src_types_set){
1337 priv_fms_expr.SetType (source_type);
1339 if (StandardConversionExists (priv_fms_expr, param_type))
1340 candidate_set.Add (param_type);
1343 if (candidate_set.Count != 0)
1344 return FindMostEncompassedType (candidate_set);
1350 if (apply_explicit_conv_rules)
1351 return FindMostEncompassingType (src_types_set);
1353 return FindMostEncompassedType (src_types_set);
1357 // Useful in avoiding proliferation of objects
1359 static EmptyExpression priv_fmt_expr;
1362 /// Finds the most specific target Tx according to section 13.4.4
1364 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1365 bool apply_explicit_conv_rules,
1368 ArrayList tgt_types_set = new ArrayList ();
1370 if (priv_fmt_expr == null)
1371 priv_fmt_expr = new EmptyExpression ();
1374 // If any operator converts to T then Tx = T
1376 foreach (MethodInfo mi in me.Methods){
1377 Type ret_type = mi.ReturnType;
1379 if (ret_type == target)
1382 if (apply_explicit_conv_rules) {
1385 // Find the set of applicable user-defined conversion operators, U.
1387 // This set consists of the
1388 // user-defined implicit or explicit conversion operators declared by
1389 // the classes or structs in D that convert from a type encompassing
1390 // or encompassed by S to a type encompassing or encompassed by T
1392 priv_fms_expr.SetType (ret_type);
1393 if (StandardConversionExists (priv_fms_expr, target))
1394 tgt_types_set.Add (ret_type);
1396 priv_fms_expr.SetType (target);
1397 if (StandardConversionExists (priv_fms_expr, ret_type))
1398 tgt_types_set.Add (ret_type);
1402 // Only if T is encompassed by param_type
1404 priv_fms_expr.SetType (ret_type);
1405 if (StandardConversionExists (priv_fms_expr, target))
1406 tgt_types_set.Add (ret_type);
1411 // Explicit conv rules
1413 if (apply_explicit_conv_rules) {
1414 ArrayList candidate_set = new ArrayList ();
1416 foreach (Type ret_type in tgt_types_set){
1417 priv_fmt_expr.SetType (ret_type);
1419 if (StandardConversionExists (priv_fmt_expr, target))
1420 candidate_set.Add (ret_type);
1423 if (candidate_set.Count != 0)
1424 return FindMostEncompassingType (candidate_set);
1428 // Okay, final case !
1430 if (apply_explicit_conv_rules)
1431 return FindMostEncompassedType (tgt_types_set);
1433 return FindMostEncompassingType (tgt_types_set);
1437 /// User-defined Implicit conversions
1439 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1440 Type target, Location loc)
1442 return UserDefinedConversion (ec, source, target, loc, false);
1446 /// User-defined Explicit conversions
1448 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1449 Type target, Location loc)
1451 return UserDefinedConversion (ec, source, target, loc, true);
1455 /// Computes the MethodGroup for the user-defined conversion
1456 /// operators from source_type to target_type. `look_for_explicit'
1457 /// controls whether we should also include the list of explicit
1460 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1461 Type source_type, Type target_type,
1462 Location loc, bool look_for_explicit)
1464 Expression mg1 = null, mg2 = null;
1465 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1469 // FIXME : How does the False operator come into the picture ?
1470 // This doesn't look complete and very correct !
1472 if (target_type == TypeManager.bool_type && !look_for_explicit)
1473 op_name = "op_True";
1475 op_name = "op_Implicit";
1477 MethodGroupExpr union3;
1479 mg1 = MethodLookup (ec, source_type, op_name, loc);
1480 if (source_type.BaseType != null)
1481 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1484 union3 = (MethodGroupExpr) mg2;
1485 else if (mg2 == null)
1486 union3 = (MethodGroupExpr) mg1;
1488 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1490 mg1 = MethodLookup (ec, target_type, op_name, loc);
1493 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1495 union3 = (MethodGroupExpr) mg1;
1498 if (target_type.BaseType != null)
1499 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1503 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1505 union3 = (MethodGroupExpr) mg1;
1508 MethodGroupExpr union4 = null;
1510 if (look_for_explicit) {
1511 op_name = "op_Explicit";
1513 mg5 = MemberLookup (ec, source_type, op_name, loc);
1514 if (source_type.BaseType != null)
1515 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1517 mg7 = MemberLookup (ec, target_type, op_name, loc);
1518 if (target_type.BaseType != null)
1519 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1521 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1522 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1524 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1527 return Invocation.MakeUnionSet (union3, union4, loc);
1531 /// User-defined conversions
1533 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1534 Type target, Location loc,
1535 bool look_for_explicit)
1537 MethodGroupExpr union;
1538 Type source_type = source.Type;
1539 MethodBase method = null;
1541 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1545 Type most_specific_source, most_specific_target;
1548 foreach (MethodBase m in union.Methods){
1549 Console.WriteLine ("Name: " + m.Name);
1550 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1554 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1555 if (most_specific_source == null)
1558 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1559 if (most_specific_target == null)
1564 foreach (MethodBase mb in union.Methods){
1565 ParameterData pd = Invocation.GetParameterData (mb);
1566 MethodInfo mi = (MethodInfo) mb;
1568 if (pd.ParameterType (0) == most_specific_source &&
1569 mi.ReturnType == most_specific_target) {
1575 if (method == null || count > 1) {
1576 Report.Error (-11, loc, "Ambiguous user defined conversion");
1581 // This will do the conversion to the best match that we
1582 // found. Now we need to perform an implict standard conversion
1583 // if the best match was not the type that we were requested
1586 if (look_for_explicit)
1587 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1589 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1595 e = new UserCast ((MethodInfo) method, source, loc);
1596 if (e.Type != target){
1597 if (!look_for_explicit)
1598 e = ConvertImplicitStandard (ec, e, target, loc);
1600 e = ConvertExplicitStandard (ec, e, target, loc);
1606 /// Converts implicitly the resolved expression `expr' into the
1607 /// `target_type'. It returns a new expression that can be used
1608 /// in a context that expects a `target_type'.
1610 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1611 Type target_type, Location loc)
1613 Type expr_type = expr.Type;
1616 if (expr_type == target_type)
1619 if (target_type == null)
1620 throw new Exception ("Target type is null");
1622 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1626 e = ImplicitUserConversion (ec, expr, target_type, loc);
1635 /// Attempts to apply the `Standard Implicit
1636 /// Conversion' rules to the expression `expr' into
1637 /// the `target_type'. It returns a new expression
1638 /// that can be used in a context that expects a
1641 /// This is different from `ConvertImplicit' in that the
1642 /// user defined implicit conversions are excluded.
1644 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1645 Type target_type, Location loc)
1647 Type expr_type = expr.Type;
1650 if (expr_type == target_type)
1653 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1657 e = ImplicitReferenceConversion (expr, target_type);
1661 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1662 IntLiteral i = (IntLiteral) expr;
1665 return new EmptyCast (expr, target_type);
1669 if (expr_type.IsPointer){
1670 if (target_type == TypeManager.void_ptr_type)
1671 return new EmptyCast (expr, target_type);
1674 // yep, comparing pointer types cant be done with
1675 // t1 == t2, we have to compare their element types.
1677 if (target_type.IsPointer){
1678 if (target_type.GetElementType()==expr_type.GetElementType())
1683 if (target_type.IsPointer){
1684 if (expr is NullLiteral)
1685 return new EmptyCast (expr, target_type);
1693 /// Attemps to perform an implict constant conversion of the IntConstant
1694 /// into a different data type using casts (See Implicit Constant
1695 /// Expression Conversions)
1697 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1699 int value = ic.Value;
1702 // FIXME: This could return constants instead of EmptyCasts
1704 if (target_type == TypeManager.sbyte_type){
1705 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1706 return new SByteConstant ((sbyte) value);
1707 } else if (target_type == TypeManager.byte_type){
1708 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1709 return new ByteConstant ((byte) value);
1710 } else if (target_type == TypeManager.short_type){
1711 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1712 return new ShortConstant ((short) value);
1713 } else if (target_type == TypeManager.ushort_type){
1714 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1715 return new UShortConstant ((ushort) value);
1716 } else if (target_type == TypeManager.uint32_type){
1718 return new UIntConstant ((uint) value);
1719 } else if (target_type == TypeManager.uint64_type){
1721 // we can optimize this case: a positive int32
1722 // always fits on a uint64. But we need an opcode
1726 return new ULongConstant ((ulong) value);
1729 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1730 return new EnumConstant (ic, target_type);
1735 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1737 string msg = "Cannot convert implicitly from `"+
1738 TypeManager.CSharpName (source) + "' to `" +
1739 TypeManager.CSharpName (target) + "'";
1741 Report.Error (29, loc, msg);
1745 /// Attemptes to implicityly convert `target' into `type', using
1746 /// ConvertImplicit. If there is no implicit conversion, then
1747 /// an error is signaled
1749 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1750 Type target_type, Location loc)
1754 e = ConvertImplicit (ec, source, target_type, loc);
1758 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1759 Report.Error (664, loc,
1760 "Double literal cannot be implicitly converted to " +
1761 "float type, use F suffix to create a float literal");
1764 Error_CannotConvertImplicit (loc, source.Type, target_type);
1770 /// Performs the explicit numeric conversions
1772 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1774 Type expr_type = expr.Type;
1777 // If we have an enumeration, extract the underlying type,
1778 // use this during the comparission, but wrap around the original
1781 Type real_target_type = target_type;
1783 if (TypeManager.IsEnumType (real_target_type))
1784 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1786 if (expr_type == TypeManager.sbyte_type){
1788 // From sbyte to byte, ushort, uint, ulong, char
1790 if (real_target_type == TypeManager.byte_type)
1791 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1792 if (real_target_type == TypeManager.ushort_type)
1793 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1794 if (real_target_type == TypeManager.uint32_type)
1795 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1796 if (real_target_type == TypeManager.uint64_type)
1797 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1798 if (real_target_type == TypeManager.char_type)
1799 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1800 } else if (expr_type == TypeManager.byte_type){
1802 // From byte to sbyte and char
1804 if (real_target_type == TypeManager.sbyte_type)
1805 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1806 if (real_target_type == TypeManager.char_type)
1807 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1808 } else if (expr_type == TypeManager.short_type){
1810 // From short to sbyte, byte, ushort, uint, ulong, char
1812 if (real_target_type == TypeManager.sbyte_type)
1813 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1814 if (real_target_type == TypeManager.byte_type)
1815 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1816 if (real_target_type == TypeManager.ushort_type)
1817 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1818 if (real_target_type == TypeManager.uint32_type)
1819 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1820 if (real_target_type == TypeManager.uint64_type)
1821 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1822 if (real_target_type == TypeManager.char_type)
1823 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1824 } else if (expr_type == TypeManager.ushort_type){
1826 // From ushort to sbyte, byte, short, char
1828 if (real_target_type == TypeManager.sbyte_type)
1829 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1830 if (real_target_type == TypeManager.byte_type)
1831 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1832 if (real_target_type == TypeManager.short_type)
1833 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1834 if (real_target_type == TypeManager.char_type)
1835 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1836 } else if (expr_type == TypeManager.int32_type){
1838 // From int to sbyte, byte, short, ushort, uint, ulong, char
1840 if (real_target_type == TypeManager.sbyte_type)
1841 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1842 if (real_target_type == TypeManager.byte_type)
1843 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1844 if (real_target_type == TypeManager.short_type)
1845 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1846 if (real_target_type == TypeManager.ushort_type)
1847 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1848 if (real_target_type == TypeManager.uint32_type)
1849 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1850 if (real_target_type == TypeManager.uint64_type)
1851 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1852 if (real_target_type == TypeManager.char_type)
1853 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1854 } else if (expr_type == TypeManager.uint32_type){
1856 // From uint to sbyte, byte, short, ushort, int, char
1858 if (real_target_type == TypeManager.sbyte_type)
1859 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1860 if (real_target_type == TypeManager.byte_type)
1861 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1862 if (real_target_type == TypeManager.short_type)
1863 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1864 if (real_target_type == TypeManager.ushort_type)
1865 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1866 if (real_target_type == TypeManager.int32_type)
1867 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1868 if (real_target_type == TypeManager.char_type)
1869 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1870 } else if (expr_type == TypeManager.int64_type){
1872 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1874 if (real_target_type == TypeManager.sbyte_type)
1875 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1876 if (real_target_type == TypeManager.byte_type)
1877 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1878 if (real_target_type == TypeManager.short_type)
1879 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1880 if (real_target_type == TypeManager.ushort_type)
1881 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1882 if (real_target_type == TypeManager.int32_type)
1883 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1884 if (real_target_type == TypeManager.uint32_type)
1885 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1886 if (real_target_type == TypeManager.uint64_type)
1887 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1888 if (real_target_type == TypeManager.char_type)
1889 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1890 } else if (expr_type == TypeManager.uint64_type){
1892 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1894 if (real_target_type == TypeManager.sbyte_type)
1895 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1896 if (real_target_type == TypeManager.byte_type)
1897 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1898 if (real_target_type == TypeManager.short_type)
1899 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1900 if (real_target_type == TypeManager.ushort_type)
1901 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1902 if (real_target_type == TypeManager.int32_type)
1903 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1904 if (real_target_type == TypeManager.uint32_type)
1905 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1906 if (real_target_type == TypeManager.int64_type)
1907 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1908 if (real_target_type == TypeManager.char_type)
1909 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1910 } else if (expr_type == TypeManager.char_type){
1912 // From char to sbyte, byte, short
1914 if (real_target_type == TypeManager.sbyte_type)
1915 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1916 if (real_target_type == TypeManager.byte_type)
1917 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1918 if (real_target_type == TypeManager.short_type)
1919 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1920 } else if (expr_type == TypeManager.float_type){
1922 // From float to sbyte, byte, short,
1923 // ushort, int, uint, long, ulong, char
1926 if (real_target_type == TypeManager.sbyte_type)
1927 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1928 if (real_target_type == TypeManager.byte_type)
1929 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1930 if (real_target_type == TypeManager.short_type)
1931 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1932 if (real_target_type == TypeManager.ushort_type)
1933 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1934 if (real_target_type == TypeManager.int32_type)
1935 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1936 if (real_target_type == TypeManager.uint32_type)
1937 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1938 if (real_target_type == TypeManager.int64_type)
1939 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1940 if (real_target_type == TypeManager.uint64_type)
1941 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1942 if (real_target_type == TypeManager.char_type)
1943 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1944 if (real_target_type == TypeManager.decimal_type)
1945 return InternalTypeConstructor (ec, expr, target_type);
1946 } else if (expr_type == TypeManager.double_type){
1948 // From double to byte, byte, short,
1949 // ushort, int, uint, long, ulong,
1950 // char, float or decimal
1952 if (real_target_type == TypeManager.sbyte_type)
1953 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1954 if (real_target_type == TypeManager.byte_type)
1955 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1956 if (real_target_type == TypeManager.short_type)
1957 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1958 if (real_target_type == TypeManager.ushort_type)
1959 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1960 if (real_target_type == TypeManager.int32_type)
1961 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1962 if (real_target_type == TypeManager.uint32_type)
1963 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1964 if (real_target_type == TypeManager.int64_type)
1965 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1966 if (real_target_type == TypeManager.uint64_type)
1967 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1968 if (real_target_type == TypeManager.char_type)
1969 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1970 if (real_target_type == TypeManager.float_type)
1971 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1972 if (real_target_type == TypeManager.decimal_type)
1973 return InternalTypeConstructor (ec, expr, target_type);
1976 // decimal is taken care of by the op_Explicit methods.
1982 /// Returns whether an explicit reference conversion can be performed
1983 /// from source_type to target_type
1985 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1987 bool target_is_value_type = target_type.IsValueType;
1989 if (source_type == target_type)
1993 // From object to any reference type
1995 if (source_type == TypeManager.object_type && !target_is_value_type)
1999 // From any class S to any class-type T, provided S is a base class of T
2001 if (target_type.IsSubclassOf (source_type))
2005 // From any interface type S to any interface T provided S is not derived from T
2007 if (source_type.IsInterface && target_type.IsInterface){
2008 if (!target_type.IsSubclassOf (source_type))
2013 // From any class type S to any interface T, provided S is not sealed
2014 // and provided S does not implement T.
2016 if (target_type.IsInterface && !source_type.IsSealed &&
2017 !TypeManager.ImplementsInterface (source_type, target_type))
2021 // From any interface-type S to to any class type T, provided T is not
2022 // sealed, or provided T implements S.
2024 if (source_type.IsInterface &&
2025 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2029 // From an array type S with an element type Se to an array type T with an
2030 // element type Te provided all the following are true:
2031 // * S and T differe only in element type, in other words, S and T
2032 // have the same number of dimensions.
2033 // * Both Se and Te are reference types
2034 // * An explicit referenc conversions exist from Se to Te
2036 if (source_type.IsArray && target_type.IsArray) {
2037 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2039 Type source_element_type = source_type.GetElementType ();
2040 Type target_element_type = target_type.GetElementType ();
2042 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2043 if (ExplicitReferenceConversionExists (source_element_type,
2044 target_element_type))
2050 // From System.Array to any array-type
2051 if (source_type == TypeManager.array_type &&
2052 target_type.IsArray){
2057 // From System delegate to any delegate-type
2059 if (source_type == TypeManager.delegate_type &&
2060 target_type.IsSubclassOf (TypeManager.delegate_type))
2064 // From ICloneable to Array or Delegate types
2066 if (source_type == TypeManager.icloneable_type &&
2067 (target_type == TypeManager.array_type ||
2068 target_type == TypeManager.delegate_type))
2075 /// Implements Explicit Reference conversions
2077 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2079 Type source_type = source.Type;
2080 bool target_is_value_type = target_type.IsValueType;
2083 // From object to any reference type
2085 if (source_type == TypeManager.object_type && !target_is_value_type)
2086 return new ClassCast (source, target_type);
2090 // From any class S to any class-type T, provided S is a base class of T
2092 if (target_type.IsSubclassOf (source_type))
2093 return new ClassCast (source, target_type);
2096 // From any interface type S to any interface T provided S is not derived from T
2098 if (source_type.IsInterface && target_type.IsInterface){
2099 if (TypeManager.ImplementsInterface (source_type, target_type))
2102 return new ClassCast (source, target_type);
2106 // From any class type S to any interface T, provides S is not sealed
2107 // and provided S does not implement T.
2109 if (target_type.IsInterface && !source_type.IsSealed) {
2110 if (TypeManager.ImplementsInterface (source_type, target_type))
2113 return new ClassCast (source, target_type);
2118 // From any interface-type S to to any class type T, provided T is not
2119 // sealed, or provided T implements S.
2121 if (source_type.IsInterface) {
2122 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2123 return new ClassCast (source, target_type);
2128 // From an array type S with an element type Se to an array type T with an
2129 // element type Te provided all the following are true:
2130 // * S and T differe only in element type, in other words, S and T
2131 // have the same number of dimensions.
2132 // * Both Se and Te are reference types
2133 // * An explicit referenc conversions exist from Se to Te
2135 if (source_type.IsArray && target_type.IsArray) {
2136 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2138 Type source_element_type = source_type.GetElementType ();
2139 Type target_element_type = target_type.GetElementType ();
2141 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2142 if (ExplicitReferenceConversionExists (source_element_type,
2143 target_element_type))
2144 return new ClassCast (source, target_type);
2149 // From System.Array to any array-type
2150 if (source_type == TypeManager.array_type &&
2151 target_type.IsArray) {
2152 return new ClassCast (source, target_type);
2156 // From System delegate to any delegate-type
2158 if (source_type == TypeManager.delegate_type &&
2159 target_type.IsSubclassOf (TypeManager.delegate_type))
2160 return new ClassCast (source, target_type);
2163 // From ICloneable to Array or Delegate types
2165 if (source_type == TypeManager.icloneable_type &&
2166 (target_type == TypeManager.array_type ||
2167 target_type == TypeManager.delegate_type))
2168 return new ClassCast (source, target_type);
2174 /// Performs an explicit conversion of the expression `expr' whose
2175 /// type is expr.Type to `target_type'.
2177 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2178 Type target_type, Location loc)
2180 Type expr_type = expr.Type;
2181 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2186 ne = ConvertNumericExplicit (ec, expr, target_type);
2191 // Unboxing conversion.
2193 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2194 return new UnboxCast (expr, target_type);
2199 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2203 // FIXME: Is there any reason we should have EnumConstant
2204 // dealt with here instead of just using always the
2205 // UnderlyingSystemType to wrap the type?
2207 if (expr is EnumConstant)
2208 e = ((EnumConstant) expr).Child;
2210 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2213 Expression t = ConvertImplicit (ec, e, target_type, loc);
2217 return ConvertNumericExplicit (ec, e, target_type);
2220 ne = ConvertReferenceExplicit (expr, target_type);
2225 if (target_type.IsPointer){
2226 if (expr_type.IsPointer)
2227 return new EmptyCast (expr, target_type);
2229 if (expr_type == TypeManager.sbyte_type ||
2230 expr_type == TypeManager.byte_type ||
2231 expr_type == TypeManager.short_type ||
2232 expr_type == TypeManager.ushort_type ||
2233 expr_type == TypeManager.int32_type ||
2234 expr_type == TypeManager.uint32_type ||
2235 expr_type == TypeManager.uint64_type ||
2236 expr_type == TypeManager.int64_type)
2237 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2239 if (expr_type.IsPointer){
2240 if (target_type == TypeManager.sbyte_type ||
2241 target_type == TypeManager.byte_type ||
2242 target_type == TypeManager.short_type ||
2243 target_type == TypeManager.ushort_type ||
2244 target_type == TypeManager.int32_type ||
2245 target_type == TypeManager.uint32_type ||
2246 target_type == TypeManager.uint64_type ||
2247 target_type == TypeManager.int64_type){
2248 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2251 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2256 ce = ConvertNumericExplicit (ec, e, target_type);
2260 // We should always be able to go from an uint32
2261 // implicitly or explicitly to the other integral
2264 throw new Exception ("Internal compiler error");
2269 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2273 Error_CannotConvertType (loc, expr_type, target_type);
2278 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2280 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2281 Type target_type, Location l)
2283 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2288 ne = ConvertNumericExplicit (ec, expr, target_type);
2292 ne = ConvertReferenceExplicit (expr, target_type);
2296 Error_CannotConvertType (l, expr.Type, target_type);
2300 static string ExprClassName (ExprClass c)
2303 case ExprClass.Invalid:
2305 case ExprClass.Value:
2307 case ExprClass.Variable:
2309 case ExprClass.Namespace:
2311 case ExprClass.Type:
2313 case ExprClass.MethodGroup:
2314 return "method group";
2315 case ExprClass.PropertyAccess:
2316 return "property access";
2317 case ExprClass.EventAccess:
2318 return "event access";
2319 case ExprClass.IndexerAccess:
2320 return "indexer access";
2321 case ExprClass.Nothing:
2324 throw new Exception ("Should not happen");
2328 /// Reports that we were expecting `expr' to be of class `expected'
2330 public void Error118 (string expected)
2332 string kind = "Unknown";
2334 kind = ExprClassName (eclass);
2336 Error (118, "Expression denotes a `" + kind +
2337 "' where a `" + expected + "' was expected");
2340 public void Error118 (ResolveFlags flags)
2342 ArrayList valid = new ArrayList (10);
2344 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2345 valid.Add ("variable");
2346 valid.Add ("value");
2349 if ((flags & ResolveFlags.Type) != 0)
2352 if ((flags & ResolveFlags.MethodGroup) != 0)
2353 valid.Add ("method group");
2355 if ((flags & ResolveFlags.SimpleName) != 0)
2356 valid.Add ("simple name");
2358 if (valid.Count == 0)
2359 valid.Add ("unknown");
2361 StringBuilder sb = new StringBuilder ();
2362 for (int i = 0; i < valid.Count; i++) {
2365 else if (i == valid.Count)
2367 sb.Append (valid [i]);
2370 string kind = ExprClassName (eclass);
2372 Error (119, "Expression denotes a `" + kind + "' where " +
2373 "a `" + sb.ToString () + "' was expected");
2376 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2378 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2379 TypeManager.CSharpName (t));
2382 public static void UnsafeError (Location loc)
2384 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2388 /// Converts the IntConstant, UIntConstant, LongConstant or
2389 /// ULongConstant into the integral target_type. Notice
2390 /// that we do not return an `Expression' we do return
2391 /// a boxed integral type.
2393 /// FIXME: Since I added the new constants, we need to
2394 /// also support conversions from CharConstant, ByteConstant,
2395 /// SByteConstant, UShortConstant, ShortConstant
2397 /// This is used by the switch statement, so the domain
2398 /// of work is restricted to the literals above, and the
2399 /// targets are int32, uint32, char, byte, sbyte, ushort,
2400 /// short, uint64 and int64
2402 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2406 if (c.Type == target_type)
2407 return ((Constant) c).GetValue ();
2410 // Make into one of the literals we handle, we dont really care
2411 // about this value as we will just return a few limited types
2413 if (c is EnumConstant)
2414 c = ((EnumConstant)c).WidenToCompilerConstant ();
2416 if (c is IntConstant){
2417 int v = ((IntConstant) c).Value;
2419 if (target_type == TypeManager.uint32_type){
2422 } else if (target_type == TypeManager.char_type){
2423 if (v >= Char.MinValue && v <= Char.MaxValue)
2425 } else if (target_type == TypeManager.byte_type){
2426 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2428 } else if (target_type == TypeManager.sbyte_type){
2429 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2431 } else if (target_type == TypeManager.short_type){
2432 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2434 } else if (target_type == TypeManager.ushort_type){
2435 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2437 } else if (target_type == TypeManager.int64_type)
2439 else if (target_type == TypeManager.uint64_type){
2445 } else if (c is UIntConstant){
2446 uint v = ((UIntConstant) c).Value;
2448 if (target_type == TypeManager.int32_type){
2449 if (v <= Int32.MaxValue)
2451 } else if (target_type == TypeManager.char_type){
2452 if (v >= Char.MinValue && v <= Char.MaxValue)
2454 } else if (target_type == TypeManager.byte_type){
2455 if (v <= Byte.MaxValue)
2457 } else if (target_type == TypeManager.sbyte_type){
2458 if (v <= SByte.MaxValue)
2460 } else if (target_type == TypeManager.short_type){
2461 if (v <= UInt16.MaxValue)
2463 } else if (target_type == TypeManager.ushort_type){
2464 if (v <= UInt16.MaxValue)
2466 } else if (target_type == TypeManager.int64_type)
2468 else if (target_type == TypeManager.uint64_type)
2471 } else if (c is LongConstant){
2472 long v = ((LongConstant) c).Value;
2474 if (target_type == TypeManager.int32_type){
2475 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2477 } else if (target_type == TypeManager.uint32_type){
2478 if (v >= 0 && v <= UInt32.MaxValue)
2480 } else if (target_type == TypeManager.char_type){
2481 if (v >= Char.MinValue && v <= Char.MaxValue)
2483 } else if (target_type == TypeManager.byte_type){
2484 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2486 } else if (target_type == TypeManager.sbyte_type){
2487 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2489 } else if (target_type == TypeManager.short_type){
2490 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2492 } else if (target_type == TypeManager.ushort_type){
2493 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2495 } else if (target_type == TypeManager.uint64_type){
2500 } else if (c is ULongConstant){
2501 ulong v = ((ULongConstant) c).Value;
2503 if (target_type == TypeManager.int32_type){
2504 if (v <= Int32.MaxValue)
2506 } else if (target_type == TypeManager.uint32_type){
2507 if (v <= UInt32.MaxValue)
2509 } else if (target_type == TypeManager.char_type){
2510 if (v >= Char.MinValue && v <= Char.MaxValue)
2512 } else if (target_type == TypeManager.byte_type){
2513 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2515 } else if (target_type == TypeManager.sbyte_type){
2516 if (v <= (int) SByte.MaxValue)
2518 } else if (target_type == TypeManager.short_type){
2519 if (v <= UInt16.MaxValue)
2521 } else if (target_type == TypeManager.ushort_type){
2522 if (v <= UInt16.MaxValue)
2524 } else if (target_type == TypeManager.int64_type){
2525 if (v <= Int64.MaxValue)
2529 } else if (c is ByteConstant){
2530 byte v = ((ByteConstant) c).Value;
2532 if (target_type == TypeManager.int32_type)
2534 else if (target_type == TypeManager.uint32_type)
2536 else if (target_type == TypeManager.char_type)
2538 else if (target_type == TypeManager.sbyte_type){
2539 if (v <= SByte.MaxValue)
2541 } else if (target_type == TypeManager.short_type)
2543 else if (target_type == TypeManager.ushort_type)
2545 else if (target_type == TypeManager.int64_type)
2547 else if (target_type == TypeManager.uint64_type)
2550 } else if (c is SByteConstant){
2551 sbyte v = ((SByteConstant) c).Value;
2553 if (target_type == TypeManager.int32_type)
2555 else if (target_type == TypeManager.uint32_type){
2558 } else if (target_type == TypeManager.char_type){
2561 } else if (target_type == TypeManager.byte_type){
2564 } else if (target_type == TypeManager.short_type)
2566 else if (target_type == TypeManager.ushort_type){
2569 } else if (target_type == TypeManager.int64_type)
2571 else if (target_type == TypeManager.uint64_type){
2576 } else if (c is ShortConstant){
2577 short v = ((ShortConstant) c).Value;
2579 if (target_type == TypeManager.int32_type){
2581 } else if (target_type == TypeManager.uint32_type){
2584 } else if (target_type == TypeManager.char_type){
2587 } else if (target_type == TypeManager.byte_type){
2588 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2590 } else if (target_type == TypeManager.sbyte_type){
2591 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2593 } else if (target_type == TypeManager.ushort_type){
2596 } else if (target_type == TypeManager.int64_type)
2598 else if (target_type == TypeManager.uint64_type)
2602 } else if (c is UShortConstant){
2603 ushort v = ((UShortConstant) c).Value;
2605 if (target_type == TypeManager.int32_type)
2607 else if (target_type == TypeManager.uint32_type)
2609 else if (target_type == TypeManager.char_type){
2610 if (v >= Char.MinValue && v <= Char.MaxValue)
2612 } else if (target_type == TypeManager.byte_type){
2613 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2615 } else if (target_type == TypeManager.sbyte_type){
2616 if (v <= SByte.MaxValue)
2618 } else if (target_type == TypeManager.short_type){
2619 if (v <= Int16.MaxValue)
2621 } else if (target_type == TypeManager.int64_type)
2623 else if (target_type == TypeManager.uint64_type)
2627 } else if (c is CharConstant){
2628 char v = ((CharConstant) c).Value;
2630 if (target_type == TypeManager.int32_type)
2632 else if (target_type == TypeManager.uint32_type)
2634 else if (target_type == TypeManager.byte_type){
2635 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2637 } else if (target_type == TypeManager.sbyte_type){
2638 if (v <= SByte.MaxValue)
2640 } else if (target_type == TypeManager.short_type){
2641 if (v <= Int16.MaxValue)
2643 } else if (target_type == TypeManager.ushort_type)
2645 else if (target_type == TypeManager.int64_type)
2647 else if (target_type == TypeManager.uint64_type)
2652 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2657 // Load the object from the pointer.
2659 public static void LoadFromPtr (ILGenerator ig, Type t)
2661 if (t == TypeManager.int32_type)
2662 ig.Emit (OpCodes.Ldind_I4);
2663 else if (t == TypeManager.uint32_type)
2664 ig.Emit (OpCodes.Ldind_U4);
2665 else if (t == TypeManager.short_type)
2666 ig.Emit (OpCodes.Ldind_I2);
2667 else if (t == TypeManager.ushort_type)
2668 ig.Emit (OpCodes.Ldind_U2);
2669 else if (t == TypeManager.char_type)
2670 ig.Emit (OpCodes.Ldind_U2);
2671 else if (t == TypeManager.byte_type)
2672 ig.Emit (OpCodes.Ldind_U1);
2673 else if (t == TypeManager.sbyte_type)
2674 ig.Emit (OpCodes.Ldind_I1);
2675 else if (t == TypeManager.uint64_type)
2676 ig.Emit (OpCodes.Ldind_I8);
2677 else if (t == TypeManager.int64_type)
2678 ig.Emit (OpCodes.Ldind_I8);
2679 else if (t == TypeManager.float_type)
2680 ig.Emit (OpCodes.Ldind_R4);
2681 else if (t == TypeManager.double_type)
2682 ig.Emit (OpCodes.Ldind_R8);
2683 else if (t == TypeManager.bool_type)
2684 ig.Emit (OpCodes.Ldind_I1);
2685 else if (t == TypeManager.intptr_type)
2686 ig.Emit (OpCodes.Ldind_I);
2687 else if (TypeManager.IsEnumType (t)) {
2688 if (t == TypeManager.enum_type)
2689 ig.Emit (OpCodes.Ldind_Ref);
2691 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2692 } else if (t.IsValueType)
2693 ig.Emit (OpCodes.Ldobj, t);
2695 ig.Emit (OpCodes.Ldind_Ref);
2699 // The stack contains the pointer and the value of type `type'
2701 public static void StoreFromPtr (ILGenerator ig, Type type)
2704 type = TypeManager.EnumToUnderlying (type);
2705 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2706 ig.Emit (OpCodes.Stind_I4);
2707 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2708 ig.Emit (OpCodes.Stind_I8);
2709 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2710 type == TypeManager.ushort_type)
2711 ig.Emit (OpCodes.Stind_I2);
2712 else if (type == TypeManager.float_type)
2713 ig.Emit (OpCodes.Stind_R4);
2714 else if (type == TypeManager.double_type)
2715 ig.Emit (OpCodes.Stind_R8);
2716 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2717 type == TypeManager.bool_type)
2718 ig.Emit (OpCodes.Stind_I1);
2719 else if (type == TypeManager.intptr_type)
2720 ig.Emit (OpCodes.Stind_I);
2721 else if (type.IsValueType)
2722 ig.Emit (OpCodes.Stobj, type);
2724 ig.Emit (OpCodes.Stind_Ref);
2728 // Returns the size of type `t' if known, otherwise, 0
2730 public static int GetTypeSize (Type t)
2732 t = TypeManager.TypeToCoreType (t);
2733 if (t == TypeManager.int32_type ||
2734 t == TypeManager.uint32_type ||
2735 t == TypeManager.float_type)
2737 else if (t == TypeManager.int64_type ||
2738 t == TypeManager.uint64_type ||
2739 t == TypeManager.double_type)
2741 else if (t == TypeManager.byte_type ||
2742 t == TypeManager.sbyte_type ||
2743 t == TypeManager.bool_type)
2745 else if (t == TypeManager.short_type ||
2746 t == TypeManager.char_type ||
2747 t == TypeManager.ushort_type)
2754 // Default implementation of IAssignMethod.CacheTemporaries
2756 public void CacheTemporaries (EmitContext ec)
2760 static void Error_NegativeArrayIndex (Location loc)
2762 Report.Error (284, loc, "Can not create array with a negative size");
2766 // Converts `source' to an int, uint, long or ulong.
2768 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2772 bool old_checked = ec.CheckState;
2773 ec.CheckState = true;
2775 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2776 if (target == null){
2777 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2778 if (target == null){
2779 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2780 if (target == null){
2781 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2783 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2787 ec.CheckState = old_checked;
2790 // Only positive constants are allowed at compile time
2792 if (target is Constant){
2793 if (target is IntConstant){
2794 if (((IntConstant) target).Value < 0){
2795 Error_NegativeArrayIndex (loc);
2800 if (target is LongConstant){
2801 if (((LongConstant) target).Value < 0){
2802 Error_NegativeArrayIndex (loc);
2815 /// This is just a base class for expressions that can
2816 /// appear on statements (invocations, object creation,
2817 /// assignments, post/pre increment and decrement). The idea
2818 /// being that they would support an extra Emition interface that
2819 /// does not leave a result on the stack.
2821 public abstract class ExpressionStatement : Expression {
2824 /// Requests the expression to be emitted in a `statement'
2825 /// context. This means that no new value is left on the
2826 /// stack after invoking this method (constrasted with
2827 /// Emit that will always leave a value on the stack).
2829 public abstract void EmitStatement (EmitContext ec);
2833 /// This kind of cast is used to encapsulate the child
2834 /// whose type is child.Type into an expression that is
2835 /// reported to return "return_type". This is used to encapsulate
2836 /// expressions which have compatible types, but need to be dealt
2837 /// at higher levels with.
2839 /// For example, a "byte" expression could be encapsulated in one
2840 /// of these as an "unsigned int". The type for the expression
2841 /// would be "unsigned int".
2844 public class EmptyCast : Expression {
2845 protected Expression child;
2847 public EmptyCast (Expression child, Type return_type)
2849 eclass = child.eclass;
2854 public override Expression DoResolve (EmitContext ec)
2856 // This should never be invoked, we are born in fully
2857 // initialized state.
2862 public override void Emit (EmitContext ec)
2869 /// This class is used to wrap literals which belong inside Enums
2871 public class EnumConstant : Constant {
2872 public Constant Child;
2874 public EnumConstant (Constant child, Type enum_type)
2876 eclass = child.eclass;
2881 public override Expression DoResolve (EmitContext ec)
2883 // This should never be invoked, we are born in fully
2884 // initialized state.
2889 public override void Emit (EmitContext ec)
2894 public override object GetValue ()
2896 return Child.GetValue ();
2900 // Converts from one of the valid underlying types for an enumeration
2901 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2902 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2904 public Constant WidenToCompilerConstant ()
2906 Type t = TypeManager.EnumToUnderlying (Child.Type);
2907 object v = ((Constant) Child).GetValue ();;
2909 if (t == TypeManager.int32_type)
2910 return new IntConstant ((int) v);
2911 if (t == TypeManager.uint32_type)
2912 return new UIntConstant ((uint) v);
2913 if (t == TypeManager.int64_type)
2914 return new LongConstant ((long) v);
2915 if (t == TypeManager.uint64_type)
2916 return new ULongConstant ((ulong) v);
2917 if (t == TypeManager.short_type)
2918 return new ShortConstant ((short) v);
2919 if (t == TypeManager.ushort_type)
2920 return new UShortConstant ((ushort) v);
2921 if (t == TypeManager.byte_type)
2922 return new ByteConstant ((byte) v);
2923 if (t == TypeManager.sbyte_type)
2924 return new SByteConstant ((sbyte) v);
2926 throw new Exception ("Invalid enumeration underlying type: " + t);
2930 // Extracts the value in the enumeration on its native representation
2932 public object GetPlainValue ()
2934 Type t = TypeManager.EnumToUnderlying (Child.Type);
2935 object v = ((Constant) Child).GetValue ();;
2937 if (t == TypeManager.int32_type)
2939 if (t == TypeManager.uint32_type)
2941 if (t == TypeManager.int64_type)
2943 if (t == TypeManager.uint64_type)
2945 if (t == TypeManager.short_type)
2947 if (t == TypeManager.ushort_type)
2949 if (t == TypeManager.byte_type)
2951 if (t == TypeManager.sbyte_type)
2957 public override string AsString ()
2959 return Child.AsString ();
2962 public override DoubleConstant ConvertToDouble ()
2964 return Child.ConvertToDouble ();
2967 public override FloatConstant ConvertToFloat ()
2969 return Child.ConvertToFloat ();
2972 public override ULongConstant ConvertToULong ()
2974 return Child.ConvertToULong ();
2977 public override LongConstant ConvertToLong ()
2979 return Child.ConvertToLong ();
2982 public override UIntConstant ConvertToUInt ()
2984 return Child.ConvertToUInt ();
2987 public override IntConstant ConvertToInt ()
2989 return Child.ConvertToInt ();
2994 /// This kind of cast is used to encapsulate Value Types in objects.
2996 /// The effect of it is to box the value type emitted by the previous
2999 public class BoxedCast : EmptyCast {
3001 public BoxedCast (Expression expr)
3002 : base (expr, TypeManager.object_type)
3006 public override Expression DoResolve (EmitContext ec)
3008 // This should never be invoked, we are born in fully
3009 // initialized state.
3014 public override void Emit (EmitContext ec)
3018 ec.ig.Emit (OpCodes.Box, child.Type);
3022 public class UnboxCast : EmptyCast {
3023 public UnboxCast (Expression expr, Type return_type)
3024 : base (expr, return_type)
3028 public override Expression DoResolve (EmitContext ec)
3030 // This should never be invoked, we are born in fully
3031 // initialized state.
3036 public override void Emit (EmitContext ec)
3039 ILGenerator ig = ec.ig;
3042 ig.Emit (OpCodes.Unbox, t);
3044 LoadFromPtr (ig, t);
3049 /// This is used to perform explicit numeric conversions.
3051 /// Explicit numeric conversions might trigger exceptions in a checked
3052 /// context, so they should generate the conv.ovf opcodes instead of
3055 public class ConvCast : EmptyCast {
3056 public enum Mode : byte {
3057 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3059 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3060 U2_I1, U2_U1, U2_I2, U2_CH,
3061 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3062 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3063 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3064 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3065 CH_I1, CH_U1, CH_I2,
3066 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3067 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3073 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3074 : base (child, return_type)
3076 checked_state = ec.CheckState;
3080 public override Expression DoResolve (EmitContext ec)
3082 // This should never be invoked, we are born in fully
3083 // initialized state.
3088 public override void Emit (EmitContext ec)
3090 ILGenerator ig = ec.ig;
3096 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3097 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3098 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3099 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3100 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3102 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3103 case Mode.U1_CH: /* nothing */ break;
3105 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3106 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3107 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3108 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3109 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3110 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3112 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3113 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3114 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3115 case Mode.U2_CH: /* nothing */ break;
3117 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3118 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3119 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3120 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3121 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3122 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3123 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3125 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3126 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3127 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3128 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3129 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3130 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3132 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3133 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3134 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3135 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3136 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3137 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3138 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3139 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3141 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3142 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3143 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3144 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3145 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3146 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3147 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3148 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3150 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3151 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3152 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3154 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3155 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3156 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3157 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3158 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3159 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3160 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3161 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3162 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3164 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3165 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3166 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3167 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3168 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3169 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3170 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3171 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3172 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3173 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3177 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3178 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3179 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3180 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3181 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3183 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3184 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3186 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3187 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3188 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3189 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3190 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3191 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3193 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3194 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3195 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3196 case Mode.U2_CH: /* nothing */ break;
3198 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3199 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3200 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3201 case Mode.I4_U4: /* nothing */ break;
3202 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3203 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3204 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3206 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3207 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3208 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3209 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3210 case Mode.U4_I4: /* nothing */ break;
3211 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3213 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3214 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3215 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3216 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3217 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3218 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3219 case Mode.I8_U8: /* nothing */ break;
3220 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3222 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3223 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3224 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3225 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3226 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3227 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3228 case Mode.U8_I8: /* nothing */ break;
3229 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3231 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3232 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3233 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3235 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3236 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3237 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3238 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3239 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3240 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3241 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3242 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3243 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3245 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3246 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3247 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3248 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3249 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3250 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3251 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3252 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3253 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3254 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3260 public class OpcodeCast : EmptyCast {
3264 public OpcodeCast (Expression child, Type return_type, OpCode op)
3265 : base (child, return_type)
3269 second_valid = false;
3272 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3273 : base (child, return_type)
3278 second_valid = true;
3281 public override Expression DoResolve (EmitContext ec)
3283 // This should never be invoked, we are born in fully
3284 // initialized state.
3289 public override void Emit (EmitContext ec)
3300 /// This kind of cast is used to encapsulate a child and cast it
3301 /// to the class requested
3303 public class ClassCast : EmptyCast {
3304 public ClassCast (Expression child, Type return_type)
3305 : base (child, return_type)
3310 public override Expression DoResolve (EmitContext ec)
3312 // This should never be invoked, we are born in fully
3313 // initialized state.
3318 public override void Emit (EmitContext ec)
3322 ec.ig.Emit (OpCodes.Castclass, type);
3328 /// SimpleName expressions are initially formed of a single
3329 /// word and it only happens at the beginning of the expression.
3333 /// The expression will try to be bound to a Field, a Method
3334 /// group or a Property. If those fail we pass the name to our
3335 /// caller and the SimpleName is compounded to perform a type
3336 /// lookup. The idea behind this process is that we want to avoid
3337 /// creating a namespace map from the assemblies, as that requires
3338 /// the GetExportedTypes function to be called and a hashtable to
3339 /// be constructed which reduces startup time. If later we find
3340 /// that this is slower, we should create a `NamespaceExpr' expression
3341 /// that fully participates in the resolution process.
3343 /// For example `System.Console.WriteLine' is decomposed into
3344 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3346 /// The first SimpleName wont produce a match on its own, so it will
3348 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3350 /// System.Console will produce a TypeExpr match.
3352 /// The downside of this is that we might be hitting `LookupType' too many
3353 /// times with this scheme.
3355 public class SimpleName : Expression, ITypeExpression {
3356 public readonly string Name;
3358 public SimpleName (string name, Location l)
3364 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3366 if (ec.IsFieldInitializer)
3369 "A field initializer cannot reference the non-static field, " +
3370 "method or property `"+name+"'");
3374 "An object reference is required " +
3375 "for the non-static field `"+name+"'");
3379 // Checks whether we are trying to access an instance
3380 // property, method or field from a static body.
3382 Expression MemberStaticCheck (EmitContext ec, Expression e)
3384 if (e is IMemberExpr){
3385 IMemberExpr member = (IMemberExpr) e;
3387 if (!member.IsStatic){
3388 Error_ObjectRefRequired (ec, loc, Name);
3396 public override Expression DoResolve (EmitContext ec)
3398 return SimpleNameResolve (ec, null, false);
3401 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3403 return SimpleNameResolve (ec, right_side, false);
3407 public Expression DoResolveAllowStatic (EmitContext ec)
3409 return SimpleNameResolve (ec, null, true);
3412 public Expression DoResolveType (EmitContext ec)
3415 // Stage 3: Lookup symbol in the various namespaces.
3417 DeclSpace ds = ec.DeclSpace;
3421 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3422 return new TypeExpr (t, loc);
3425 // Stage 2 part b: Lookup up if we are an alias to a type
3428 // Since we are cheating: we only do the Alias lookup for
3429 // namespaces if the name does not include any dots in it
3432 alias_value = ec.DeclSpace.LookupAlias (Name);
3434 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3435 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3436 return new TypeExpr (t, loc);
3438 // we have alias value, but it isn't Type, so try if it's namespace
3439 return new SimpleName (alias_value, loc);
3442 if (ec.ResolvingTypeTree){
3443 Type dt = ec.DeclSpace.FindType (Name);
3445 return new TypeExpr (dt, loc);
3448 // No match, maybe our parent can compose us
3449 // into something meaningful.
3454 /// 7.5.2: Simple Names.
3456 /// Local Variables and Parameters are handled at
3457 /// parse time, so they never occur as SimpleNames.
3459 /// The `allow_static' flag is used by MemberAccess only
3460 /// and it is used to inform us that it is ok for us to
3461 /// avoid the static check, because MemberAccess might end
3462 /// up resolving the Name as a Type name and the access as
3463 /// a static type access.
3465 /// ie: Type Type; .... { Type.GetType (""); }
3467 /// Type is both an instance variable and a Type; Type.GetType
3468 /// is the static method not an instance method of type.
3470 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3472 Expression e = null;
3475 // Stage 1: Performed by the parser (binding to locals or parameters).
3477 Block current_block = ec.CurrentBlock;
3478 if (current_block != null && current_block.IsVariableDefined (Name)){
3479 LocalVariableReference var;
3481 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3483 if (right_side != null)
3484 return var.ResolveLValue (ec, right_side);
3486 return var.Resolve (ec);
3489 if (current_block != null){
3491 Parameter par = null;
3492 Parameters pars = current_block.Parameters;
3494 par = pars.GetParameterByName (Name, out idx);
3497 ParameterReference param;
3499 param = new ParameterReference (pars, idx, Name, loc);
3501 if (right_side != null)
3502 return param.ResolveLValue (ec, right_side);
3504 return param.Resolve (ec);
3509 // Stage 2: Lookup members
3513 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3514 // Hence we have two different cases
3517 DeclSpace lookup_ds = ec.DeclSpace;
3519 if (lookup_ds.TypeBuilder == null)
3522 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3527 // Classes/structs keep looking, enums break
3529 if (lookup_ds is TypeContainer)
3530 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3533 } while (lookup_ds != null);
3535 if (e == null && ec.ContainerType != null)
3536 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3539 return DoResolveType (ec);
3544 if (e is IMemberExpr) {
3545 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3549 IMemberExpr me = e as IMemberExpr;
3553 // This fails if ResolveMemberAccess() was unable to decide whether
3554 // it's a field or a type of the same name.
3555 if (!me.IsStatic && (me.InstanceExpression == null))
3558 if (right_side != null)
3559 e = e.DoResolveLValue (ec, right_side);
3561 e = e.DoResolve (ec);
3566 if (ec.IsStatic || ec.IsFieldInitializer){
3570 return MemberStaticCheck (ec, e);
3575 public override void Emit (EmitContext ec)
3578 // If this is ever reached, then we failed to
3579 // find the name as a namespace
3582 Error (103, "The name `" + Name +
3583 "' does not exist in the class `" +
3584 ec.DeclSpace.Name + "'");
3587 public override string ToString ()
3594 /// Fully resolved expression that evaluates to a type
3596 public class TypeExpr : Expression, ITypeExpression {
3597 public TypeExpr (Type t, Location l)
3600 eclass = ExprClass.Type;
3604 public virtual Expression DoResolveType (EmitContext ec)
3609 override public Expression DoResolve (EmitContext ec)
3614 override public void Emit (EmitContext ec)
3616 throw new Exception ("Should never be called");
3621 /// Used to create types from a fully qualified name. These are just used
3622 /// by the parser to setup the core types. A TypeLookupExpression is always
3623 /// classified as a type.
3625 public class TypeLookupExpression : TypeExpr {
3628 public TypeLookupExpression (string name) : base (null, Location.Null)
3633 public override Expression DoResolveType (EmitContext ec)
3636 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3640 public override Expression DoResolve (EmitContext ec)
3642 return DoResolveType (ec);
3645 public override void Emit (EmitContext ec)
3647 throw new Exception ("Should never be called");
3650 public override string ToString ()
3657 /// MethodGroup Expression.
3659 /// This is a fully resolved expression that evaluates to a type
3661 public class MethodGroupExpr : Expression, IMemberExpr {
3662 public MethodBase [] Methods;
3663 Expression instance_expression = null;
3665 public MethodGroupExpr (MemberInfo [] mi, Location l)
3667 Methods = new MethodBase [mi.Length];
3668 mi.CopyTo (Methods, 0);
3669 eclass = ExprClass.MethodGroup;
3670 type = TypeManager.object_type;
3674 public MethodGroupExpr (ArrayList list, Location l)
3676 Methods = new MethodBase [list.Count];
3679 list.CopyTo (Methods, 0);
3681 foreach (MemberInfo m in list){
3682 if (!(m is MethodBase)){
3683 Console.WriteLine ("Name " + m.Name);
3684 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3690 eclass = ExprClass.MethodGroup;
3691 type = TypeManager.object_type;
3695 // `A method group may have associated an instance expression'
3697 public Expression InstanceExpression {
3699 return instance_expression;
3703 instance_expression = value;
3707 public string Name {
3709 return Methods [0].Name;
3713 public bool IsInstance {
3715 foreach (MethodBase mb in Methods)
3723 public bool IsStatic {
3725 foreach (MethodBase mb in Methods)
3733 override public Expression DoResolve (EmitContext ec)
3738 public void ReportUsageError ()
3740 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3741 Methods [0].Name + "()' is referenced without parentheses");
3744 override public void Emit (EmitContext ec)
3746 ReportUsageError ();
3749 bool RemoveMethods (bool keep_static)
3751 ArrayList smethods = new ArrayList ();
3753 foreach (MethodBase mb in Methods){
3754 if (mb.IsStatic == keep_static)
3758 if (smethods.Count == 0)
3761 Methods = new MethodBase [smethods.Count];
3762 smethods.CopyTo (Methods, 0);
3768 /// Removes any instance methods from the MethodGroup, returns
3769 /// false if the resulting set is empty.
3771 public bool RemoveInstanceMethods ()
3773 return RemoveMethods (true);
3777 /// Removes any static methods from the MethodGroup, returns
3778 /// false if the resulting set is empty.
3780 public bool RemoveStaticMethods ()
3782 return RemoveMethods (false);
3787 /// Fully resolved expression that evaluates to a Field
3789 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3790 public readonly FieldInfo FieldInfo;
3791 Expression instance_expr;
3793 public FieldExpr (FieldInfo fi, Location l)
3796 eclass = ExprClass.Variable;
3797 type = fi.FieldType;
3801 public string Name {
3803 return FieldInfo.Name;
3807 public bool IsInstance {
3809 return !FieldInfo.IsStatic;
3813 public bool IsStatic {
3815 return FieldInfo.IsStatic;
3819 public Expression InstanceExpression {
3821 return instance_expr;
3825 instance_expr = value;
3829 override public Expression DoResolve (EmitContext ec)
3831 if (!FieldInfo.IsStatic){
3832 if (instance_expr == null){
3833 throw new Exception ("non-static FieldExpr without instance var\n" +
3834 "You have to assign the Instance variable\n" +
3835 "Of the FieldExpr to set this\n");
3838 // Resolve the field's instance expression while flow analysis is turned
3839 // off: when accessing a field "a.b", we must check whether the field
3840 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3841 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3842 ResolveFlags.DisableFlowAnalysis);
3843 if (instance_expr == null)
3847 // If the instance expression is a local variable or parameter.
3848 IVariable var = instance_expr as IVariable;
3849 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3855 void Report_AssignToReadonly (bool is_instance)
3860 msg = "Readonly field can not be assigned outside " +
3861 "of constructor or variable initializer";
3863 msg = "A static readonly field can only be assigned in " +
3864 "a static constructor";
3866 Report.Error (is_instance ? 191 : 198, loc, msg);
3869 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3871 IVariable var = instance_expr as IVariable;
3873 var.SetFieldAssigned (ec, FieldInfo.Name);
3875 Expression e = DoResolve (ec);
3880 if (!FieldInfo.IsInitOnly)
3884 // InitOnly fields can only be assigned in constructors
3887 if (ec.IsConstructor)
3890 Report_AssignToReadonly (true);
3895 override public void Emit (EmitContext ec)
3897 ILGenerator ig = ec.ig;
3898 bool is_volatile = false;
3900 if (FieldInfo is FieldBuilder){
3901 FieldBase f = TypeManager.GetField (FieldInfo);
3903 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3906 f.status |= Field.Status.USED;
3909 if (FieldInfo.IsStatic){
3911 ig.Emit (OpCodes.Volatile);
3913 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3915 if (instance_expr.Type.IsValueType){
3917 LocalTemporary tempo = null;
3919 if (!(instance_expr is IMemoryLocation)){
3920 tempo = new LocalTemporary (
3921 ec, instance_expr.Type);
3923 InstanceExpression.Emit (ec);
3927 ml = (IMemoryLocation) instance_expr;
3929 ml.AddressOf (ec, AddressOp.Load);
3931 instance_expr.Emit (ec);
3934 ig.Emit (OpCodes.Volatile);
3936 ig.Emit (OpCodes.Ldfld, FieldInfo);
3940 public void EmitAssign (EmitContext ec, Expression source)
3942 FieldAttributes fa = FieldInfo.Attributes;
3943 bool is_static = (fa & FieldAttributes.Static) != 0;
3944 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3945 ILGenerator ig = ec.ig;
3947 if (is_readonly && !ec.IsConstructor){
3948 Report_AssignToReadonly (!is_static);
3953 Expression instance = instance_expr;
3955 if (instance.Type.IsValueType){
3956 if (instance is IMemoryLocation){
3957 IMemoryLocation ml = (IMemoryLocation) instance;
3959 ml.AddressOf (ec, AddressOp.Store);
3961 throw new Exception ("The " + instance + " of type " +
3963 " represents a ValueType and does " +
3964 "not implement IMemoryLocation");
3970 if (FieldInfo is FieldBuilder){
3971 FieldBase f = TypeManager.GetField (FieldInfo);
3973 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3974 ig.Emit (OpCodes.Volatile);
3978 ig.Emit (OpCodes.Stsfld, FieldInfo);
3980 ig.Emit (OpCodes.Stfld, FieldInfo);
3982 if (FieldInfo is FieldBuilder){
3983 FieldBase f = TypeManager.GetField (FieldInfo);
3985 f.status |= Field.Status.ASSIGNED;
3989 public void AddressOf (EmitContext ec, AddressOp mode)
3991 ILGenerator ig = ec.ig;
3993 if (FieldInfo is FieldBuilder){
3994 FieldBase f = TypeManager.GetField (FieldInfo);
3995 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3996 ig.Emit (OpCodes.Volatile);
3999 if (FieldInfo is FieldBuilder){
4000 FieldBase f = TypeManager.GetField (FieldInfo);
4002 if ((mode & AddressOp.Store) != 0)
4003 f.status |= Field.Status.ASSIGNED;
4004 if ((mode & AddressOp.Load) != 0)
4005 f.status |= Field.Status.USED;
4009 // Handle initonly fields specially: make a copy and then
4010 // get the address of the copy.
4012 if (FieldInfo.IsInitOnly){
4013 if (ec.IsConstructor) {
4014 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4019 local = ig.DeclareLocal (type);
4020 ig.Emit (OpCodes.Stloc, local);
4021 ig.Emit (OpCodes.Ldloca, local);
4026 if (FieldInfo.IsStatic)
4027 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4029 if (instance_expr is IMemoryLocation)
4030 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4032 instance_expr.Emit (ec);
4033 ig.Emit (OpCodes.Ldflda, FieldInfo);
4039 /// Expression that evaluates to a Property. The Assign class
4040 /// might set the `Value' expression if we are in an assignment.
4042 /// This is not an LValue because we need to re-write the expression, we
4043 /// can not take data from the stack and store it.
4045 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4046 public readonly PropertyInfo PropertyInfo;
4048 MethodInfo [] Accessors;
4051 Expression instance_expr;
4053 public PropertyExpr (PropertyInfo pi, Location l)
4056 eclass = ExprClass.PropertyAccess;
4059 Accessors = TypeManager.GetAccessors (pi);
4061 if (Accessors != null)
4062 foreach (MethodInfo mi in Accessors){
4068 Accessors = new MethodInfo [2];
4070 type = TypeManager.TypeToCoreType (pi.PropertyType);
4073 public string Name {
4075 return PropertyInfo.Name;
4079 public bool IsInstance {
4085 public bool IsStatic {
4092 // The instance expression associated with this expression
4094 public Expression InstanceExpression {
4096 instance_expr = value;
4100 return instance_expr;
4104 public bool VerifyAssignable ()
4106 if (!PropertyInfo.CanWrite){
4107 Report.Error (200, loc,
4108 "The property `" + PropertyInfo.Name +
4109 "' can not be assigned to, as it has not set accessor");
4116 override public Expression DoResolve (EmitContext ec)
4118 if (!PropertyInfo.CanRead){
4119 Report.Error (154, loc,
4120 "The property `" + PropertyInfo.Name +
4121 "' can not be used in " +
4122 "this context because it lacks a get accessor");
4129 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4131 if (!PropertyInfo.CanWrite){
4132 Report.Error (154, loc,
4133 "The property `" + PropertyInfo.Name +
4134 "' can not be used in " +
4135 "this context because it lacks a set accessor");
4142 override public void Emit (EmitContext ec)
4144 MethodInfo method = Accessors [0];
4147 // Special case: length of single dimension array is turned into ldlen
4149 if (method == TypeManager.int_array_get_length){
4150 Type iet = instance_expr.Type;
4153 // System.Array.Length can be called, but the Type does not
4154 // support invoking GetArrayRank, so test for that case first
4156 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4157 instance_expr.Emit (ec);
4158 ec.ig.Emit (OpCodes.Ldlen);
4163 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
4168 // Implements the IAssignMethod interface for assignments
4170 public void EmitAssign (EmitContext ec, Expression source)
4172 Argument arg = new Argument (source, Argument.AType.Expression);
4173 ArrayList args = new ArrayList ();
4176 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
4179 override public void EmitStatement (EmitContext ec)
4182 ec.ig.Emit (OpCodes.Pop);
4187 /// Fully resolved expression that evaluates to an Event
4189 public class EventExpr : Expression, IMemberExpr {
4190 public readonly EventInfo EventInfo;
4191 public Expression instance_expr;
4194 MethodInfo add_accessor, remove_accessor;
4196 public EventExpr (EventInfo ei, Location loc)
4200 eclass = ExprClass.EventAccess;
4202 add_accessor = TypeManager.GetAddMethod (ei);
4203 remove_accessor = TypeManager.GetRemoveMethod (ei);
4205 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4208 if (EventInfo is MyEventBuilder)
4209 type = ((MyEventBuilder) EventInfo).EventType;
4211 type = EventInfo.EventHandlerType;
4214 public string Name {
4216 return EventInfo.Name;
4220 public bool IsInstance {
4226 public bool IsStatic {
4232 public Expression InstanceExpression {
4234 return instance_expr;
4238 instance_expr = value;
4242 public override Expression DoResolve (EmitContext ec)
4244 // We are born fully resolved
4248 public override void Emit (EmitContext ec)
4250 throw new Exception ("Should not happen I think");
4253 public void EmitAddOrRemove (EmitContext ec, Expression source)
4255 Expression handler = ((Binary) source).Right;
4257 Argument arg = new Argument (handler, Argument.AType.Expression);
4258 ArrayList args = new ArrayList ();
4262 if (((Binary) source).Oper == Binary.Operator.Addition)
4263 Invocation.EmitCall (
4264 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4266 Invocation.EmitCall (
4267 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);