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 type which declares this member.
166 /// The instance expression associated with this member, if it's a
167 /// non-static member.
169 Expression InstanceExpression {
175 /// Expression which resolves to a type.
177 public interface ITypeExpression
180 /// Resolve the expression, but only lookup types.
182 Expression DoResolveType (EmitContext ec);
186 /// Base class for expressions
188 public abstract class Expression {
189 public ExprClass eclass;
191 protected Location loc;
203 public Location Location {
210 /// Utility wrapper routine for Error, just to beautify the code
212 public void Error (int error, string s)
214 if (!Location.IsNull (loc))
215 Report.Error (error, loc, s);
217 Report.Error (error, s);
221 /// Utility wrapper routine for Warning, just to beautify the code
223 public void Warning (int warning, string s)
225 if (!Location.IsNull (loc))
226 Report.Warning (warning, loc, s);
228 Report.Warning (warning, s);
232 /// Utility wrapper routine for Warning, only prints the warning if
233 /// warnings of level `level' are enabled.
235 public void Warning (int warning, int level, string s)
237 if (level <= RootContext.WarningLevel)
238 Warning (warning, s);
241 static public void Error_CannotConvertType (Location loc, Type source, Type target)
243 Report.Error (30, loc, "Cannot convert type '" +
244 TypeManager.CSharpName (source) + "' to '" +
245 TypeManager.CSharpName (target) + "'");
249 /// Performs semantic analysis on the Expression
253 /// The Resolve method is invoked to perform the semantic analysis
256 /// The return value is an expression (it can be the
257 /// same expression in some cases) or a new
258 /// expression that better represents this node.
260 /// For example, optimizations of Unary (LiteralInt)
261 /// would return a new LiteralInt with a negated
264 /// If there is an error during semantic analysis,
265 /// then an error should be reported (using Report)
266 /// and a null value should be returned.
268 /// There are two side effects expected from calling
269 /// Resolve(): the the field variable "eclass" should
270 /// be set to any value of the enumeration
271 /// `ExprClass' and the type variable should be set
272 /// to a valid type (this is the type of the
275 public abstract Expression DoResolve (EmitContext ec);
277 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
279 return DoResolve (ec);
283 /// Resolves an expression and performs semantic analysis on it.
287 /// Currently Resolve wraps DoResolve to perform sanity
288 /// checking and assertion checking on what we expect from Resolve.
290 public Expression Resolve (EmitContext ec, ResolveFlags flags)
292 // Are we doing a types-only search ?
293 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
294 ITypeExpression type_expr = this as ITypeExpression;
296 if (type_expr == null)
299 return type_expr.DoResolveType (ec);
302 bool old_do_flow_analysis = ec.DoFlowAnalysis;
303 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
304 ec.DoFlowAnalysis = false;
307 if (this is SimpleName)
308 e = ((SimpleName) this).DoResolveAllowStatic (ec);
312 ec.DoFlowAnalysis = old_do_flow_analysis;
317 if (e is SimpleName){
318 SimpleName s = (SimpleName) e;
320 if ((flags & ResolveFlags.SimpleName) == 0) {
322 object lookup = TypeManager.MemberLookup (
323 ec.ContainerType, ec.ContainerType, AllMemberTypes,
324 AllBindingFlags | BindingFlags.NonPublic, s.Name);
326 Error (122, "`" + s.Name + "' " +
327 "is inaccessible because of its protection level");
329 Error (103, "The name `" + s.Name + "' could not be " +
330 "found in `" + ec.DeclSpace.Name + "'");
337 if ((e is TypeExpr) || (e is ComposedCast)) {
338 if ((flags & ResolveFlags.Type) == 0) {
348 if ((flags & ResolveFlags.VariableOrValue) == 0) {
354 case ExprClass.MethodGroup:
355 if ((flags & ResolveFlags.MethodGroup) == 0) {
356 ((MethodGroupExpr) e).ReportUsageError ();
361 case ExprClass.Value:
362 case ExprClass.Variable:
363 case ExprClass.PropertyAccess:
364 case ExprClass.EventAccess:
365 case ExprClass.IndexerAccess:
366 if ((flags & ResolveFlags.VariableOrValue) == 0) {
373 throw new Exception ("Expression " + e.GetType () +
374 " ExprClass is Invalid after resolve");
378 throw new Exception (
379 "Expression " + e.GetType () +
380 " did not set its type after Resolve\n" +
381 "called from: " + this.GetType ());
387 /// Resolves an expression and performs semantic analysis on it.
389 public Expression Resolve (EmitContext ec)
391 return Resolve (ec, ResolveFlags.VariableOrValue);
395 /// Resolves an expression for LValue assignment
399 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
400 /// checking and assertion checking on what we expect from Resolve
402 public Expression ResolveLValue (EmitContext ec, Expression right_side)
404 Expression e = DoResolveLValue (ec, right_side);
407 if (e is SimpleName){
408 SimpleName s = (SimpleName) e;
412 "The name `" + s.Name + "' could not be found in `" +
413 ec.DeclSpace.Name + "'");
417 if (e.eclass == ExprClass.Invalid)
418 throw new Exception ("Expression " + e +
419 " ExprClass is Invalid after resolve");
421 if (e.eclass == ExprClass.MethodGroup) {
422 ((MethodGroupExpr) e).ReportUsageError ();
427 throw new Exception ("Expression " + e +
428 " did not set its type after Resolve");
435 /// Emits the code for the expression
439 /// The Emit method is invoked to generate the code
440 /// for the expression.
442 public abstract void Emit (EmitContext ec);
445 /// Protected constructor. Only derivate types should
446 /// be able to be created
449 protected Expression ()
451 eclass = ExprClass.Invalid;
456 /// Returns a literalized version of a literal FieldInfo
460 /// The possible return values are:
461 /// IntConstant, UIntConstant
462 /// LongLiteral, ULongConstant
463 /// FloatConstant, DoubleConstant
466 /// The value returned is already resolved.
468 public static Constant Constantify (object v, Type t)
470 if (t == TypeManager.int32_type)
471 return new IntConstant ((int) v);
472 else if (t == TypeManager.uint32_type)
473 return new UIntConstant ((uint) v);
474 else if (t == TypeManager.int64_type)
475 return new LongConstant ((long) v);
476 else if (t == TypeManager.uint64_type)
477 return new ULongConstant ((ulong) v);
478 else if (t == TypeManager.float_type)
479 return new FloatConstant ((float) v);
480 else if (t == TypeManager.double_type)
481 return new DoubleConstant ((double) v);
482 else if (t == TypeManager.string_type)
483 return new StringConstant ((string) v);
484 else if (t == TypeManager.short_type)
485 return new ShortConstant ((short)v);
486 else if (t == TypeManager.ushort_type)
487 return new UShortConstant ((ushort)v);
488 else if (t == TypeManager.sbyte_type)
489 return new SByteConstant (((sbyte)v));
490 else if (t == TypeManager.byte_type)
491 return new ByteConstant ((byte)v);
492 else if (t == TypeManager.char_type)
493 return new CharConstant ((char)v);
494 else if (t == TypeManager.bool_type)
495 return new BoolConstant ((bool) v);
496 else if (TypeManager.IsEnumType (t)){
497 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
499 return new EnumConstant (e, t);
501 throw new Exception ("Unknown type for constant (" + t +
506 /// Returns a fully formed expression after a MemberLookup
508 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
511 return new EventExpr ((EventInfo) mi, loc);
512 else if (mi is FieldInfo)
513 return new FieldExpr ((FieldInfo) mi, loc);
514 else if (mi is PropertyInfo)
515 return new PropertyExpr ((PropertyInfo) mi, loc);
516 else if (mi is Type){
517 return new TypeExpr ((System.Type) mi, loc);
524 // FIXME: Probably implement a cache for (t,name,current_access_set)?
526 // This code could use some optimizations, but we need to do some
527 // measurements. For example, we could use a delegate to `flag' when
528 // something can not any longer be a method-group (because it is something
532 // If the return value is an Array, then it is an array of
535 // If the return value is an MemberInfo, it is anything, but a Method
539 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
540 // the arguments here and have MemberLookup return only the methods that
541 // match the argument count/type, unlike we are doing now (we delay this
544 // This is so we can catch correctly attempts to invoke instance methods
545 // from a static body (scan for error 120 in ResolveSimpleName).
548 // FIXME: Potential optimization, have a static ArrayList
551 public static Expression MemberLookup (EmitContext ec, Type t, string name,
552 MemberTypes mt, BindingFlags bf, Location loc)
554 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
558 // Lookup type `t' for code in class `invocation_type'. Note that it's important
559 // to set `invocation_type' correctly since this method also checks whether the
560 // invoking class is allowed to access the member in class `t'. When you want to
561 // explicitly do a lookup in the base class, you must set both `t' and `invocation_type'
562 // to the base class (although a derived class can access protected members of its base
563 // class it cannot do so through an instance of the base class (error CS1540)).
566 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
567 string name, MemberTypes mt, BindingFlags bf,
570 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
575 int count = mi.Length;
578 return new MethodGroupExpr (mi, loc);
580 if (mi [0] is MethodBase)
581 return new MethodGroupExpr (mi, loc);
583 return ExprClassFromMemberInfo (ec, mi [0], loc);
586 public const MemberTypes AllMemberTypes =
587 MemberTypes.Constructor |
591 MemberTypes.NestedType |
592 MemberTypes.Property;
594 public const BindingFlags AllBindingFlags =
595 BindingFlags.Public |
596 BindingFlags.Static |
597 BindingFlags.Instance;
599 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
601 return MemberLookup (ec, ec.ContainerType, t, name,
602 AllMemberTypes, AllBindingFlags, loc);
605 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
607 return MemberLookup (ec, ec.ContainerType, t, name,
608 MemberTypes.Method, AllBindingFlags, loc);
612 /// This is a wrapper for MemberLookup that is not used to "probe", but
613 /// to find a final definition. If the final definition is not found, we
614 /// look for private members and display a useful debugging message if we
617 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
620 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
623 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
624 MemberTypes mt, BindingFlags bf, Location loc)
628 int errors = Report.Errors;
630 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
635 // Error has already been reported.
636 if (errors < Report.Errors)
639 e = MemberLookup (ec, t, name, AllMemberTypes,
640 AllBindingFlags | BindingFlags.NonPublic, loc);
643 117, loc, "`" + t + "' does not contain a definition " +
644 "for `" + name + "'");
647 122, loc, "`" + t + "." + name +
648 "' is inaccessible due to its protection level");
654 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
656 EventInfo ei = event_expr.EventInfo;
658 return TypeManager.GetPrivateFieldOfEvent (ei);
661 static EmptyExpression MyEmptyExpr;
662 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
664 Type expr_type = expr.Type;
666 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
667 // if we are a method group, emit a warning
673 // notice that it is possible to write "ValueType v = 1", the ValueType here
674 // is an abstract class, and not really a value type, so we apply the same rules.
676 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
678 // A pointer type cannot be converted to object
680 if (expr_type.IsPointer)
683 if (expr_type.IsValueType)
684 return new BoxedCast (expr);
685 if (expr_type.IsClass || expr_type.IsInterface)
686 return new EmptyCast (expr, target_type);
687 } else if (expr_type.IsSubclassOf (target_type))
688 return new EmptyCast (expr, target_type);
691 // This code is kind of mirrored inside StandardConversionExists
692 // with the small distinction that we only probe there
694 // Always ensure that the code here and there is in sync
696 // from the null type to any reference-type.
697 if (expr is NullLiteral && !target_type.IsValueType)
698 return new EmptyCast (expr, target_type);
700 // from any class-type S to any interface-type T.
701 if (target_type.IsInterface) {
702 if (TypeManager.ImplementsInterface (expr_type, target_type)){
703 if (expr_type.IsClass)
704 return new EmptyCast (expr, target_type);
705 else if (expr_type.IsValueType)
706 return new BoxedCast (expr);
710 // from any interface type S to interface-type T.
711 if (expr_type.IsInterface && target_type.IsInterface) {
712 if (TypeManager.ImplementsInterface (expr_type, target_type))
713 return new EmptyCast (expr, target_type);
718 // from an array-type S to an array-type of type T
719 if (expr_type.IsArray && target_type.IsArray) {
720 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
722 Type expr_element_type = expr_type.GetElementType ();
724 if (MyEmptyExpr == null)
725 MyEmptyExpr = new EmptyExpression ();
727 MyEmptyExpr.SetType (expr_element_type);
728 Type target_element_type = target_type.GetElementType ();
730 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
731 if (StandardConversionExists (MyEmptyExpr,
732 target_element_type))
733 return new EmptyCast (expr, target_type);
738 // from an array-type to System.Array
739 if (expr_type.IsArray && target_type == TypeManager.array_type)
740 return new EmptyCast (expr, target_type);
742 // from any delegate type to System.Delegate
743 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
744 target_type == TypeManager.delegate_type)
745 return new EmptyCast (expr, target_type);
747 // from any array-type or delegate type into System.ICloneable.
748 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
749 if (target_type == TypeManager.icloneable_type)
750 return new EmptyCast (expr, target_type);
760 /// Implicit Numeric Conversions.
762 /// expr is the expression to convert, returns a new expression of type
763 /// target_type or null if an implicit conversion is not possible.
765 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
766 Type target_type, Location loc)
768 Type expr_type = expr.Type;
771 // Attempt to do the implicit constant expression conversions
773 if (expr is IntConstant){
776 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
780 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
782 // Try the implicit constant expression conversion
783 // from long to ulong, instead of a nice routine,
786 long v = ((LongConstant) expr).Value;
788 return new ULongConstant ((ulong) v);
791 Type real_target_type = target_type;
793 if (expr_type == TypeManager.sbyte_type){
795 // From sbyte to short, int, long, float, double.
797 if (real_target_type == TypeManager.int32_type)
798 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
799 if (real_target_type == TypeManager.int64_type)
800 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
801 if (real_target_type == TypeManager.double_type)
802 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
803 if (real_target_type == TypeManager.float_type)
804 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
805 if (real_target_type == TypeManager.short_type)
806 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
807 } else if (expr_type == TypeManager.byte_type){
809 // From byte to short, ushort, int, uint, long, ulong, float, double
811 if ((real_target_type == TypeManager.short_type) ||
812 (real_target_type == TypeManager.ushort_type) ||
813 (real_target_type == TypeManager.int32_type) ||
814 (real_target_type == TypeManager.uint32_type))
815 return new EmptyCast (expr, target_type);
817 if (real_target_type == TypeManager.uint64_type)
818 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
819 if (real_target_type == TypeManager.int64_type)
820 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
821 if (real_target_type == TypeManager.float_type)
822 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
823 if (real_target_type == TypeManager.double_type)
824 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
825 } else if (expr_type == TypeManager.short_type){
827 // From short to int, long, float, double
829 if (real_target_type == TypeManager.int32_type)
830 return new EmptyCast (expr, target_type);
831 if (real_target_type == TypeManager.int64_type)
832 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
833 if (real_target_type == TypeManager.double_type)
834 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
835 if (real_target_type == TypeManager.float_type)
836 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
837 } else if (expr_type == TypeManager.ushort_type){
839 // From ushort to int, uint, long, ulong, float, double
841 if (real_target_type == TypeManager.uint32_type)
842 return new EmptyCast (expr, target_type);
844 if (real_target_type == TypeManager.uint64_type)
845 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
846 if (real_target_type == TypeManager.int32_type)
847 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
848 if (real_target_type == TypeManager.int64_type)
849 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
850 if (real_target_type == TypeManager.double_type)
851 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
852 if (real_target_type == TypeManager.float_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
854 } else if (expr_type == TypeManager.int32_type){
856 // From int to long, float, double
858 if (real_target_type == TypeManager.int64_type)
859 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
860 if (real_target_type == TypeManager.double_type)
861 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
862 if (real_target_type == TypeManager.float_type)
863 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
864 } else if (expr_type == TypeManager.uint32_type){
866 // From uint to long, ulong, float, double
868 if (real_target_type == TypeManager.int64_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
870 if (real_target_type == TypeManager.uint64_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
872 if (real_target_type == TypeManager.double_type)
873 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
875 if (real_target_type == TypeManager.float_type)
876 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
878 } else if (expr_type == TypeManager.int64_type){
880 // From long/ulong to float, double
882 if (real_target_type == TypeManager.double_type)
883 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
884 if (real_target_type == TypeManager.float_type)
885 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
886 } else if (expr_type == TypeManager.uint64_type){
888 // From ulong to float, double
890 if (real_target_type == TypeManager.double_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
893 if (real_target_type == TypeManager.float_type)
894 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
896 } else if (expr_type == TypeManager.char_type){
898 // From char to ushort, int, uint, long, ulong, float, double
900 if ((real_target_type == TypeManager.ushort_type) ||
901 (real_target_type == TypeManager.int32_type) ||
902 (real_target_type == TypeManager.uint32_type))
903 return new EmptyCast (expr, target_type);
904 if (real_target_type == TypeManager.uint64_type)
905 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
906 if (real_target_type == TypeManager.int64_type)
907 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
908 if (real_target_type == TypeManager.float_type)
909 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
910 if (real_target_type == TypeManager.double_type)
911 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
912 } else if (expr_type == TypeManager.float_type){
916 if (real_target_type == TypeManager.double_type)
917 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
924 // Tests whether an implicit reference conversion exists between expr_type
927 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
929 Type expr_type = expr.Type;
932 // This is the boxed case.
934 if (target_type == TypeManager.object_type) {
935 if ((expr_type.IsClass) ||
936 (expr_type.IsValueType) ||
937 (expr_type.IsInterface))
940 } else if (expr_type.IsSubclassOf (target_type)) {
944 // Please remember that all code below actually comes
945 // from ImplicitReferenceConversion so make sure code remains in sync
947 // from any class-type S to any interface-type T.
948 if (target_type.IsInterface) {
949 if (TypeManager.ImplementsInterface (expr_type, target_type))
953 // from any interface type S to interface-type T.
954 if (expr_type.IsInterface && target_type.IsInterface)
955 if (TypeManager.ImplementsInterface (expr_type, target_type))
958 // from an array-type S to an array-type of type T
959 if (expr_type.IsArray && target_type.IsArray) {
960 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
962 Type expr_element_type = expr_type.GetElementType ();
964 if (MyEmptyExpr == null)
965 MyEmptyExpr = new EmptyExpression ();
967 MyEmptyExpr.SetType (expr_element_type);
968 Type target_element_type = target_type.GetElementType ();
970 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
971 if (StandardConversionExists (MyEmptyExpr,
972 target_element_type))
977 // from an array-type to System.Array
978 if (expr_type.IsArray && (target_type == TypeManager.array_type))
981 // from any delegate type to System.Delegate
982 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
983 target_type == TypeManager.delegate_type)
984 if (target_type.IsAssignableFrom (expr_type))
987 // from any array-type or delegate type into System.ICloneable.
988 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
989 if (target_type == TypeManager.icloneable_type)
992 // from the null type to any reference-type.
993 if (expr is NullLiteral && !target_type.IsValueType &&
994 !TypeManager.IsEnumType (target_type))
1003 /// Same as StandardConversionExists except that it also looks at
1004 /// implicit user defined conversions - needed for overload resolution
1006 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1008 if (StandardConversionExists (expr, target_type) == true)
1011 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1020 /// Determines if a standard implicit conversion exists from
1021 /// expr_type to target_type
1023 public static bool StandardConversionExists (Expression expr, Type target_type)
1025 Type expr_type = expr.Type;
1027 if (expr_type == TypeManager.void_type)
1030 if (expr_type == target_type)
1033 // First numeric conversions
1035 if (expr_type == TypeManager.sbyte_type){
1037 // From sbyte to short, int, long, float, double.
1039 if ((target_type == TypeManager.int32_type) ||
1040 (target_type == TypeManager.int64_type) ||
1041 (target_type == TypeManager.double_type) ||
1042 (target_type == TypeManager.float_type) ||
1043 (target_type == TypeManager.short_type) ||
1044 (target_type == TypeManager.decimal_type))
1047 } else if (expr_type == TypeManager.byte_type){
1049 // From byte to short, ushort, int, uint, long, ulong, float, double
1051 if ((target_type == TypeManager.short_type) ||
1052 (target_type == TypeManager.ushort_type) ||
1053 (target_type == TypeManager.int32_type) ||
1054 (target_type == TypeManager.uint32_type) ||
1055 (target_type == TypeManager.uint64_type) ||
1056 (target_type == TypeManager.int64_type) ||
1057 (target_type == TypeManager.float_type) ||
1058 (target_type == TypeManager.double_type) ||
1059 (target_type == TypeManager.decimal_type))
1062 } else if (expr_type == TypeManager.short_type){
1064 // From short to int, long, float, double
1066 if ((target_type == TypeManager.int32_type) ||
1067 (target_type == TypeManager.int64_type) ||
1068 (target_type == TypeManager.double_type) ||
1069 (target_type == TypeManager.float_type) ||
1070 (target_type == TypeManager.decimal_type))
1073 } else if (expr_type == TypeManager.ushort_type){
1075 // From ushort to int, uint, long, ulong, float, double
1077 if ((target_type == TypeManager.uint32_type) ||
1078 (target_type == TypeManager.uint64_type) ||
1079 (target_type == TypeManager.int32_type) ||
1080 (target_type == TypeManager.int64_type) ||
1081 (target_type == TypeManager.double_type) ||
1082 (target_type == TypeManager.float_type) ||
1083 (target_type == TypeManager.decimal_type))
1086 } else if (expr_type == TypeManager.int32_type){
1088 // From int to long, float, double
1090 if ((target_type == TypeManager.int64_type) ||
1091 (target_type == TypeManager.double_type) ||
1092 (target_type == TypeManager.float_type) ||
1093 (target_type == TypeManager.decimal_type))
1096 } else if (expr_type == TypeManager.uint32_type){
1098 // From uint to long, ulong, float, double
1100 if ((target_type == TypeManager.int64_type) ||
1101 (target_type == TypeManager.uint64_type) ||
1102 (target_type == TypeManager.double_type) ||
1103 (target_type == TypeManager.float_type) ||
1104 (target_type == TypeManager.decimal_type))
1107 } else if ((expr_type == TypeManager.uint64_type) ||
1108 (expr_type == TypeManager.int64_type)) {
1110 // From long/ulong to float, double
1112 if ((target_type == TypeManager.double_type) ||
1113 (target_type == TypeManager.float_type) ||
1114 (target_type == TypeManager.decimal_type))
1117 } else if (expr_type == TypeManager.char_type){
1119 // From char to ushort, int, uint, long, ulong, float, double
1121 if ((target_type == TypeManager.ushort_type) ||
1122 (target_type == TypeManager.int32_type) ||
1123 (target_type == TypeManager.uint32_type) ||
1124 (target_type == TypeManager.uint64_type) ||
1125 (target_type == TypeManager.int64_type) ||
1126 (target_type == TypeManager.float_type) ||
1127 (target_type == TypeManager.double_type) ||
1128 (target_type == TypeManager.decimal_type))
1131 } else if (expr_type == TypeManager.float_type){
1135 if (target_type == TypeManager.double_type)
1139 if (ImplicitReferenceConversionExists (expr, target_type))
1142 if (expr is IntConstant){
1143 int value = ((IntConstant) expr).Value;
1145 if (target_type == TypeManager.sbyte_type){
1146 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1148 } else if (target_type == TypeManager.byte_type){
1149 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1151 } else if (target_type == TypeManager.short_type){
1152 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1154 } else if (target_type == TypeManager.ushort_type){
1155 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1157 } else if (target_type == TypeManager.uint32_type){
1160 } else if (target_type == TypeManager.uint64_type){
1162 // we can optimize this case: a positive int32
1163 // always fits on a uint64. But we need an opcode
1170 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1174 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1176 // Try the implicit constant expression conversion
1177 // from long to ulong, instead of a nice routine,
1178 // we just inline it
1180 long v = ((LongConstant) expr).Value;
1185 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1186 IntLiteral i = (IntLiteral) expr;
1192 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1199 // Used internally by FindMostEncompassedType, this is used
1200 // to avoid creating lots of objects in the tight loop inside
1201 // FindMostEncompassedType
1203 static EmptyExpression priv_fmet_param;
1206 /// Finds "most encompassed type" according to the spec (13.4.2)
1207 /// amongst the methods in the MethodGroupExpr
1209 static Type FindMostEncompassedType (ArrayList types)
1213 if (priv_fmet_param == null)
1214 priv_fmet_param = new EmptyExpression ();
1216 foreach (Type t in types){
1217 priv_fmet_param.SetType (t);
1224 if (StandardConversionExists (priv_fmet_param, best))
1232 // Used internally by FindMostEncompassingType, this is used
1233 // to avoid creating lots of objects in the tight loop inside
1234 // FindMostEncompassingType
1236 static EmptyExpression priv_fmee_ret;
1239 /// Finds "most encompassing type" according to the spec (13.4.2)
1240 /// amongst the types in the given set
1242 static Type FindMostEncompassingType (ArrayList types)
1246 if (priv_fmee_ret == null)
1247 priv_fmee_ret = new EmptyExpression ();
1249 foreach (Type t in types){
1250 priv_fmee_ret.SetType (best);
1257 if (StandardConversionExists (priv_fmee_ret, t))
1265 // Used to avoid creating too many objects
1267 static EmptyExpression priv_fms_expr;
1270 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1271 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1272 /// for explicit and implicit conversion operators.
1274 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1275 bool apply_explicit_conv_rules,
1278 ArrayList src_types_set = new ArrayList ();
1280 if (priv_fms_expr == null)
1281 priv_fms_expr = new EmptyExpression ();
1284 // If any operator converts from S then Sx = S
1286 Type source_type = source.Type;
1287 foreach (MethodBase mb in me.Methods){
1288 ParameterData pd = Invocation.GetParameterData (mb);
1289 Type param_type = pd.ParameterType (0);
1291 if (param_type == source_type)
1294 if (apply_explicit_conv_rules) {
1297 // Find the set of applicable user-defined conversion operators, U. This set
1299 // user-defined implicit or explicit conversion operators declared by
1300 // the classes or structs in D that convert from a type encompassing
1301 // or encompassed by S to a type encompassing or encompassed by T
1303 priv_fms_expr.SetType (param_type);
1304 if (StandardConversionExists (priv_fms_expr, source_type))
1305 src_types_set.Add (param_type);
1307 if (StandardConversionExists (source, param_type))
1308 src_types_set.Add (param_type);
1312 // Only if S is encompassed by param_type
1314 if (StandardConversionExists (source, param_type))
1315 src_types_set.Add (param_type);
1320 // Explicit Conv rules
1322 if (apply_explicit_conv_rules) {
1323 ArrayList candidate_set = new ArrayList ();
1325 foreach (Type param_type in src_types_set){
1326 if (StandardConversionExists (source, param_type))
1327 candidate_set.Add (param_type);
1330 if (candidate_set.Count != 0)
1331 return FindMostEncompassedType (candidate_set);
1337 if (apply_explicit_conv_rules)
1338 return FindMostEncompassingType (src_types_set);
1340 return FindMostEncompassedType (src_types_set);
1344 // Useful in avoiding proliferation of objects
1346 static EmptyExpression priv_fmt_expr;
1349 /// Finds the most specific target Tx according to section 13.4.4
1351 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1352 bool apply_explicit_conv_rules,
1355 ArrayList tgt_types_set = new ArrayList ();
1357 if (priv_fmt_expr == null)
1358 priv_fmt_expr = new EmptyExpression ();
1361 // If any operator converts to T then Tx = T
1363 foreach (MethodInfo mi in me.Methods){
1364 Type ret_type = mi.ReturnType;
1366 if (ret_type == target)
1369 if (apply_explicit_conv_rules) {
1372 // Find the set of applicable user-defined conversion operators, U.
1374 // This set consists of the
1375 // user-defined implicit or explicit conversion operators declared by
1376 // the classes or structs in D that convert from a type encompassing
1377 // or encompassed by S to a type encompassing or encompassed by T
1379 priv_fms_expr.SetType (ret_type);
1380 if (StandardConversionExists (priv_fms_expr, target))
1381 tgt_types_set.Add (ret_type);
1383 priv_fms_expr.SetType (target);
1384 if (StandardConversionExists (priv_fms_expr, ret_type))
1385 tgt_types_set.Add (ret_type);
1389 // Only if T is encompassed by param_type
1391 priv_fms_expr.SetType (ret_type);
1392 if (StandardConversionExists (priv_fms_expr, target))
1393 tgt_types_set.Add (ret_type);
1398 // Explicit conv rules
1400 if (apply_explicit_conv_rules) {
1401 ArrayList candidate_set = new ArrayList ();
1403 foreach (Type ret_type in tgt_types_set){
1404 priv_fmt_expr.SetType (ret_type);
1406 if (StandardConversionExists (priv_fmt_expr, target))
1407 candidate_set.Add (ret_type);
1410 if (candidate_set.Count != 0)
1411 return FindMostEncompassingType (candidate_set);
1415 // Okay, final case !
1417 if (apply_explicit_conv_rules)
1418 return FindMostEncompassedType (tgt_types_set);
1420 return FindMostEncompassingType (tgt_types_set);
1424 /// User-defined Implicit conversions
1426 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1427 Type target, Location loc)
1429 return UserDefinedConversion (ec, source, target, loc, false);
1433 /// User-defined Explicit conversions
1435 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1436 Type target, Location loc)
1438 return UserDefinedConversion (ec, source, target, loc, true);
1442 /// Computes the MethodGroup for the user-defined conversion
1443 /// operators from source_type to target_type. `look_for_explicit'
1444 /// controls whether we should also include the list of explicit
1447 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1448 Type source_type, Type target_type,
1449 Location loc, bool look_for_explicit)
1451 Expression mg1 = null, mg2 = null;
1452 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1456 // FIXME : How does the False operator come into the picture ?
1457 // This doesn't look complete and very correct !
1459 if (target_type == TypeManager.bool_type && !look_for_explicit)
1460 op_name = "op_True";
1462 op_name = "op_Implicit";
1464 MethodGroupExpr union3;
1466 mg1 = MethodLookup (ec, source_type, op_name, loc);
1467 if (source_type.BaseType != null)
1468 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1471 union3 = (MethodGroupExpr) mg2;
1472 else if (mg2 == null)
1473 union3 = (MethodGroupExpr) mg1;
1475 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1477 mg1 = MethodLookup (ec, target_type, op_name, loc);
1480 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1482 union3 = (MethodGroupExpr) mg1;
1485 if (target_type.BaseType != null)
1486 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1490 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1492 union3 = (MethodGroupExpr) mg1;
1495 MethodGroupExpr union4 = null;
1497 if (look_for_explicit) {
1498 op_name = "op_Explicit";
1500 mg5 = MemberLookup (ec, source_type, op_name, loc);
1501 if (source_type.BaseType != null)
1502 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1504 mg7 = MemberLookup (ec, target_type, op_name, loc);
1505 if (target_type.BaseType != null)
1506 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1508 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1509 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1511 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1514 return Invocation.MakeUnionSet (union3, union4, loc);
1518 /// User-defined conversions
1520 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1521 Type target, Location loc,
1522 bool look_for_explicit)
1524 MethodGroupExpr union;
1525 Type source_type = source.Type;
1526 MethodBase method = null;
1528 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1532 Type most_specific_source, most_specific_target;
1535 foreach (MethodBase m in union.Methods){
1536 Console.WriteLine ("Name: " + m.Name);
1537 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1541 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1542 if (most_specific_source == null)
1545 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1546 if (most_specific_target == null)
1551 foreach (MethodBase mb in union.Methods){
1552 ParameterData pd = Invocation.GetParameterData (mb);
1553 MethodInfo mi = (MethodInfo) mb;
1555 if (pd.ParameterType (0) == most_specific_source &&
1556 mi.ReturnType == most_specific_target) {
1562 if (method == null || count > 1) {
1563 Report.Error (-11, loc, "Ambiguous user defined conversion");
1568 // This will do the conversion to the best match that we
1569 // found. Now we need to perform an implict standard conversion
1570 // if the best match was not the type that we were requested
1573 if (look_for_explicit)
1574 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1576 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1582 e = new UserCast ((MethodInfo) method, source, loc);
1583 if (e.Type != target){
1584 if (!look_for_explicit)
1585 e = ConvertImplicitStandard (ec, e, target, loc);
1587 e = ConvertExplicitStandard (ec, e, target, loc);
1593 /// Converts implicitly the resolved expression `expr' into the
1594 /// `target_type'. It returns a new expression that can be used
1595 /// in a context that expects a `target_type'.
1597 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1598 Type target_type, Location loc)
1600 Type expr_type = expr.Type;
1603 if (expr_type == target_type)
1606 if (target_type == null)
1607 throw new Exception ("Target type is null");
1609 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1613 e = ImplicitUserConversion (ec, expr, target_type, loc);
1622 /// Attempts to apply the `Standard Implicit
1623 /// Conversion' rules to the expression `expr' into
1624 /// the `target_type'. It returns a new expression
1625 /// that can be used in a context that expects a
1628 /// This is different from `ConvertImplicit' in that the
1629 /// user defined implicit conversions are excluded.
1631 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1632 Type target_type, Location loc)
1634 Type expr_type = expr.Type;
1637 if (expr_type == target_type)
1640 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1644 e = ImplicitReferenceConversion (expr, target_type);
1648 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1649 IntLiteral i = (IntLiteral) expr;
1652 return new EmptyCast (expr, target_type);
1656 if (expr_type.IsPointer){
1657 if (target_type == TypeManager.void_ptr_type)
1658 return new EmptyCast (expr, target_type);
1661 // yep, comparing pointer types cant be done with
1662 // t1 == t2, we have to compare their element types.
1664 if (target_type.IsPointer){
1665 if (target_type.GetElementType()==expr_type.GetElementType())
1670 if (target_type.IsPointer){
1671 if (expr is NullLiteral)
1672 return new EmptyCast (expr, target_type);
1680 /// Attemps to perform an implict constant conversion of the IntConstant
1681 /// into a different data type using casts (See Implicit Constant
1682 /// Expression Conversions)
1684 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1686 int value = ic.Value;
1689 // FIXME: This could return constants instead of EmptyCasts
1691 if (target_type == TypeManager.sbyte_type){
1692 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1693 return new SByteConstant ((sbyte) value);
1694 } else if (target_type == TypeManager.byte_type){
1695 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1696 return new ByteConstant ((byte) value);
1697 } else if (target_type == TypeManager.short_type){
1698 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1699 return new ShortConstant ((short) value);
1700 } else if (target_type == TypeManager.ushort_type){
1701 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1702 return new UShortConstant ((ushort) value);
1703 } else if (target_type == TypeManager.uint32_type){
1705 return new UIntConstant ((uint) value);
1706 } else if (target_type == TypeManager.uint64_type){
1708 // we can optimize this case: a positive int32
1709 // always fits on a uint64. But we need an opcode
1713 return new ULongConstant ((ulong) value);
1716 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1717 Type underlying = TypeManager.EnumToUnderlying (target_type);
1718 Constant e = (Constant) ic;
1721 // Possibly, we need to create a different 0 literal before passing
1724 if (underlying == TypeManager.int64_type)
1725 e = new LongLiteral (0);
1726 else if (underlying == TypeManager.uint64_type)
1727 e = new ULongLiteral (0);
1729 return new EnumConstant (e, target_type);
1734 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1736 string msg = "Cannot convert implicitly from `"+
1737 TypeManager.CSharpName (source) + "' to `" +
1738 TypeManager.CSharpName (target) + "'";
1740 Report.Error (29, loc, msg);
1744 /// Attemptes to implicityly convert `target' into `type', using
1745 /// ConvertImplicit. If there is no implicit conversion, then
1746 /// an error is signaled
1748 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1749 Type target_type, Location loc)
1753 e = ConvertImplicit (ec, source, target_type, loc);
1757 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1758 Report.Error (664, loc,
1759 "Double literal cannot be implicitly converted to " +
1760 "float type, use F suffix to create a float literal");
1763 Error_CannotConvertImplicit (loc, source.Type, target_type);
1769 /// Performs the explicit numeric conversions
1771 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1773 Type expr_type = expr.Type;
1776 // If we have an enumeration, extract the underlying type,
1777 // use this during the comparison, but wrap around the original
1780 Type real_target_type = target_type;
1782 if (TypeManager.IsEnumType (real_target_type))
1783 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1785 if (StandardConversionExists (expr, real_target_type)){
1786 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1788 if (real_target_type != target_type)
1789 return new EmptyCast (ce, target_type);
1793 if (expr_type == TypeManager.sbyte_type){
1795 // From sbyte to byte, ushort, uint, ulong, char
1797 if (real_target_type == TypeManager.byte_type)
1798 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1799 if (real_target_type == TypeManager.ushort_type)
1800 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1801 if (real_target_type == TypeManager.uint32_type)
1802 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1803 if (real_target_type == TypeManager.uint64_type)
1804 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1805 if (real_target_type == TypeManager.char_type)
1806 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1807 } else if (expr_type == TypeManager.byte_type){
1809 // From byte to sbyte and char
1811 if (real_target_type == TypeManager.sbyte_type)
1812 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1813 if (real_target_type == TypeManager.char_type)
1814 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1815 } else if (expr_type == TypeManager.short_type){
1817 // From short to sbyte, byte, ushort, uint, ulong, char
1819 if (real_target_type == TypeManager.sbyte_type)
1820 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1821 if (real_target_type == TypeManager.byte_type)
1822 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1823 if (real_target_type == TypeManager.ushort_type)
1824 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1825 if (real_target_type == TypeManager.uint32_type)
1826 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1827 if (real_target_type == TypeManager.uint64_type)
1828 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1829 if (real_target_type == TypeManager.char_type)
1830 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1831 } else if (expr_type == TypeManager.ushort_type){
1833 // From ushort to sbyte, byte, short, char
1835 if (real_target_type == TypeManager.sbyte_type)
1836 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1837 if (real_target_type == TypeManager.byte_type)
1838 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1839 if (real_target_type == TypeManager.short_type)
1840 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1841 if (real_target_type == TypeManager.char_type)
1842 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1843 } else if (expr_type == TypeManager.int32_type){
1845 // From int to sbyte, byte, short, ushort, uint, ulong, char
1847 if (real_target_type == TypeManager.sbyte_type)
1848 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1849 if (real_target_type == TypeManager.byte_type)
1850 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1851 if (real_target_type == TypeManager.short_type)
1852 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1853 if (real_target_type == TypeManager.ushort_type)
1854 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1855 if (real_target_type == TypeManager.uint32_type)
1856 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1857 if (real_target_type == TypeManager.uint64_type)
1858 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1859 if (real_target_type == TypeManager.char_type)
1860 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1861 } else if (expr_type == TypeManager.uint32_type){
1863 // From uint to sbyte, byte, short, ushort, int, char
1865 if (real_target_type == TypeManager.sbyte_type)
1866 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1867 if (real_target_type == TypeManager.byte_type)
1868 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1869 if (real_target_type == TypeManager.short_type)
1870 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1871 if (real_target_type == TypeManager.ushort_type)
1872 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1873 if (real_target_type == TypeManager.int32_type)
1874 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1875 if (real_target_type == TypeManager.char_type)
1876 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1877 } else if (expr_type == TypeManager.int64_type){
1879 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1881 if (real_target_type == TypeManager.sbyte_type)
1882 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1883 if (real_target_type == TypeManager.byte_type)
1884 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1885 if (real_target_type == TypeManager.short_type)
1886 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1887 if (real_target_type == TypeManager.ushort_type)
1888 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1889 if (real_target_type == TypeManager.int32_type)
1890 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1891 if (real_target_type == TypeManager.uint32_type)
1892 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1893 if (real_target_type == TypeManager.uint64_type)
1894 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1895 if (real_target_type == TypeManager.char_type)
1896 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1897 } else if (expr_type == TypeManager.uint64_type){
1899 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1901 if (real_target_type == TypeManager.sbyte_type)
1902 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1903 if (real_target_type == TypeManager.byte_type)
1904 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1905 if (real_target_type == TypeManager.short_type)
1906 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1907 if (real_target_type == TypeManager.ushort_type)
1908 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1909 if (real_target_type == TypeManager.int32_type)
1910 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1911 if (real_target_type == TypeManager.uint32_type)
1912 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1913 if (real_target_type == TypeManager.int64_type)
1914 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1915 if (real_target_type == TypeManager.char_type)
1916 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1917 } else if (expr_type == TypeManager.char_type){
1919 // From char to sbyte, byte, short
1921 if (real_target_type == TypeManager.sbyte_type)
1922 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1923 if (real_target_type == TypeManager.byte_type)
1924 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1925 if (real_target_type == TypeManager.short_type)
1926 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1927 } else if (expr_type == TypeManager.float_type){
1929 // From float to sbyte, byte, short,
1930 // ushort, int, uint, long, ulong, char
1933 if (real_target_type == TypeManager.sbyte_type)
1934 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1935 if (real_target_type == TypeManager.byte_type)
1936 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1937 if (real_target_type == TypeManager.short_type)
1938 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1939 if (real_target_type == TypeManager.ushort_type)
1940 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1941 if (real_target_type == TypeManager.int32_type)
1942 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1943 if (real_target_type == TypeManager.uint32_type)
1944 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1945 if (real_target_type == TypeManager.int64_type)
1946 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1947 if (real_target_type == TypeManager.uint64_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1949 if (real_target_type == TypeManager.char_type)
1950 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1951 } else if (expr_type == TypeManager.double_type){
1953 // From double to byte, byte, short,
1954 // ushort, int, uint, long, ulong,
1955 // char, float or decimal
1957 if (real_target_type == TypeManager.sbyte_type)
1958 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1959 if (real_target_type == TypeManager.byte_type)
1960 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1961 if (real_target_type == TypeManager.short_type)
1962 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1963 if (real_target_type == TypeManager.ushort_type)
1964 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1965 if (real_target_type == TypeManager.int32_type)
1966 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1967 if (real_target_type == TypeManager.uint32_type)
1968 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1969 if (real_target_type == TypeManager.int64_type)
1970 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1971 if (real_target_type == TypeManager.uint64_type)
1972 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1973 if (real_target_type == TypeManager.char_type)
1974 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1975 if (real_target_type == TypeManager.float_type)
1976 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1979 // decimal is taken care of by the op_Explicit methods.
1985 /// Returns whether an explicit reference conversion can be performed
1986 /// from source_type to target_type
1988 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1990 bool target_is_value_type = target_type.IsValueType;
1992 if (source_type == target_type)
1996 // From object to any reference type
1998 if (source_type == TypeManager.object_type && !target_is_value_type)
2002 // From any class S to any class-type T, provided S is a base class of T
2004 if (target_type.IsSubclassOf (source_type))
2008 // From any interface type S to any interface T provided S is not derived from T
2010 if (source_type.IsInterface && target_type.IsInterface){
2011 if (!target_type.IsSubclassOf (source_type))
2016 // From any class type S to any interface T, provided S is not sealed
2017 // and provided S does not implement T.
2019 if (target_type.IsInterface && !source_type.IsSealed &&
2020 !TypeManager.ImplementsInterface (source_type, target_type))
2024 // From any interface-type S to to any class type T, provided T is not
2025 // sealed, or provided T implements S.
2027 if (source_type.IsInterface &&
2028 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2032 // From an array type S with an element type Se to an array type T with an
2033 // element type Te provided all the following are true:
2034 // * S and T differe only in element type, in other words, S and T
2035 // have the same number of dimensions.
2036 // * Both Se and Te are reference types
2037 // * An explicit referenc conversions exist from Se to Te
2039 if (source_type.IsArray && target_type.IsArray) {
2040 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2042 Type source_element_type = source_type.GetElementType ();
2043 Type target_element_type = target_type.GetElementType ();
2045 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2046 if (ExplicitReferenceConversionExists (source_element_type,
2047 target_element_type))
2053 // From System.Array to any array-type
2054 if (source_type == TypeManager.array_type &&
2055 target_type.IsArray){
2060 // From System delegate to any delegate-type
2062 if (source_type == TypeManager.delegate_type &&
2063 target_type.IsSubclassOf (TypeManager.delegate_type))
2067 // From ICloneable to Array or Delegate types
2069 if (source_type == TypeManager.icloneable_type &&
2070 (target_type == TypeManager.array_type ||
2071 target_type == TypeManager.delegate_type))
2078 /// Implements Explicit Reference conversions
2080 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2082 Type source_type = source.Type;
2083 bool target_is_value_type = target_type.IsValueType;
2086 // From object to any reference type
2088 if (source_type == TypeManager.object_type && !target_is_value_type)
2089 return new ClassCast (source, target_type);
2093 // From any class S to any class-type T, provided S is a base class of T
2095 if (target_type.IsSubclassOf (source_type))
2096 return new ClassCast (source, target_type);
2099 // From any interface type S to any interface T provided S is not derived from T
2101 if (source_type.IsInterface && target_type.IsInterface){
2102 if (TypeManager.ImplementsInterface (source_type, target_type))
2105 return new ClassCast (source, target_type);
2109 // From any class type S to any interface T, provides S is not sealed
2110 // and provided S does not implement T.
2112 if (target_type.IsInterface && !source_type.IsSealed) {
2113 if (TypeManager.ImplementsInterface (source_type, target_type))
2116 return new ClassCast (source, target_type);
2121 // From any interface-type S to to any class type T, provided T is not
2122 // sealed, or provided T implements S.
2124 if (source_type.IsInterface) {
2125 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2126 return new ClassCast (source, target_type);
2131 // From an array type S with an element type Se to an array type T with an
2132 // element type Te provided all the following are true:
2133 // * S and T differe only in element type, in other words, S and T
2134 // have the same number of dimensions.
2135 // * Both Se and Te are reference types
2136 // * An explicit referenc conversions exist from Se to Te
2138 if (source_type.IsArray && target_type.IsArray) {
2139 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2141 Type source_element_type = source_type.GetElementType ();
2142 Type target_element_type = target_type.GetElementType ();
2144 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2145 if (ExplicitReferenceConversionExists (source_element_type,
2146 target_element_type))
2147 return new ClassCast (source, target_type);
2152 // From System.Array to any array-type
2153 if (source_type == TypeManager.array_type &&
2154 target_type.IsArray) {
2155 return new ClassCast (source, target_type);
2159 // From System delegate to any delegate-type
2161 if (source_type == TypeManager.delegate_type &&
2162 target_type.IsSubclassOf (TypeManager.delegate_type))
2163 return new ClassCast (source, target_type);
2166 // From ICloneable to Array or Delegate types
2168 if (source_type == TypeManager.icloneable_type &&
2169 (target_type == TypeManager.array_type ||
2170 target_type == TypeManager.delegate_type))
2171 return new ClassCast (source, target_type);
2177 /// Performs an explicit conversion of the expression `expr' whose
2178 /// type is expr.Type to `target_type'.
2180 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2181 Type target_type, Location loc)
2183 Type expr_type = expr.Type;
2184 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2189 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2194 // Unboxing conversion.
2196 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2197 return new UnboxCast (expr, target_type);
2202 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2206 // FIXME: Is there any reason we should have EnumConstant
2207 // dealt with here instead of just using always the
2208 // UnderlyingSystemType to wrap the type?
2210 if (expr is EnumConstant)
2211 e = ((EnumConstant) expr).Child;
2213 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2216 Expression t = ConvertImplicit (ec, e, target_type, loc);
2220 return ConvertNumericExplicit (ec, e, target_type, loc);
2223 ne = ConvertReferenceExplicit (expr, target_type);
2228 if (target_type.IsPointer){
2229 if (expr_type.IsPointer)
2230 return new EmptyCast (expr, target_type);
2232 if (expr_type == TypeManager.sbyte_type ||
2233 expr_type == TypeManager.byte_type ||
2234 expr_type == TypeManager.short_type ||
2235 expr_type == TypeManager.ushort_type ||
2236 expr_type == TypeManager.int32_type ||
2237 expr_type == TypeManager.uint32_type ||
2238 expr_type == TypeManager.uint64_type ||
2239 expr_type == TypeManager.int64_type)
2240 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2242 if (expr_type.IsPointer){
2243 if (target_type == TypeManager.sbyte_type ||
2244 target_type == TypeManager.byte_type ||
2245 target_type == TypeManager.short_type ||
2246 target_type == TypeManager.ushort_type ||
2247 target_type == TypeManager.int32_type ||
2248 target_type == TypeManager.uint32_type ||
2249 target_type == TypeManager.uint64_type ||
2250 target_type == TypeManager.int64_type){
2251 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2254 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2259 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2263 // We should always be able to go from an uint32
2264 // implicitly or explicitly to the other integral
2267 throw new Exception ("Internal compiler error");
2272 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2276 Error_CannotConvertType (loc, expr_type, target_type);
2281 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2283 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2284 Type target_type, Location l)
2286 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2291 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2295 ne = ConvertReferenceExplicit (expr, target_type);
2299 Error_CannotConvertType (l, expr.Type, target_type);
2303 static string ExprClassName (ExprClass c)
2306 case ExprClass.Invalid:
2308 case ExprClass.Value:
2310 case ExprClass.Variable:
2312 case ExprClass.Namespace:
2314 case ExprClass.Type:
2316 case ExprClass.MethodGroup:
2317 return "method group";
2318 case ExprClass.PropertyAccess:
2319 return "property access";
2320 case ExprClass.EventAccess:
2321 return "event access";
2322 case ExprClass.IndexerAccess:
2323 return "indexer access";
2324 case ExprClass.Nothing:
2327 throw new Exception ("Should not happen");
2331 /// Reports that we were expecting `expr' to be of class `expected'
2333 public void Error118 (string expected)
2335 string kind = "Unknown";
2337 kind = ExprClassName (eclass);
2339 Error (118, "Expression denotes a `" + kind +
2340 "' where a `" + expected + "' was expected");
2343 public void Error118 (ResolveFlags flags)
2345 ArrayList valid = new ArrayList (10);
2347 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2348 valid.Add ("variable");
2349 valid.Add ("value");
2352 if ((flags & ResolveFlags.Type) != 0)
2355 if ((flags & ResolveFlags.MethodGroup) != 0)
2356 valid.Add ("method group");
2358 if ((flags & ResolveFlags.SimpleName) != 0)
2359 valid.Add ("simple name");
2361 if (valid.Count == 0)
2362 valid.Add ("unknown");
2364 StringBuilder sb = new StringBuilder ();
2365 for (int i = 0; i < valid.Count; i++) {
2368 else if (i == valid.Count)
2370 sb.Append (valid [i]);
2373 string kind = ExprClassName (eclass);
2375 Error (119, "Expression denotes a `" + kind + "' where " +
2376 "a `" + sb.ToString () + "' was expected");
2379 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2381 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2382 TypeManager.CSharpName (t));
2385 public static void UnsafeError (Location loc)
2387 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2391 /// Converts the IntConstant, UIntConstant, LongConstant or
2392 /// ULongConstant into the integral target_type. Notice
2393 /// that we do not return an `Expression' we do return
2394 /// a boxed integral type.
2396 /// FIXME: Since I added the new constants, we need to
2397 /// also support conversions from CharConstant, ByteConstant,
2398 /// SByteConstant, UShortConstant, ShortConstant
2400 /// This is used by the switch statement, so the domain
2401 /// of work is restricted to the literals above, and the
2402 /// targets are int32, uint32, char, byte, sbyte, ushort,
2403 /// short, uint64 and int64
2405 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2409 if (c.Type == target_type)
2410 return ((Constant) c).GetValue ();
2413 // Make into one of the literals we handle, we dont really care
2414 // about this value as we will just return a few limited types
2416 if (c is EnumConstant)
2417 c = ((EnumConstant)c).WidenToCompilerConstant ();
2419 if (c is IntConstant){
2420 int v = ((IntConstant) c).Value;
2422 if (target_type == TypeManager.uint32_type){
2425 } else if (target_type == TypeManager.char_type){
2426 if (v >= Char.MinValue && v <= Char.MaxValue)
2428 } else if (target_type == TypeManager.byte_type){
2429 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2431 } else if (target_type == TypeManager.sbyte_type){
2432 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2434 } else if (target_type == TypeManager.short_type){
2435 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2437 } else if (target_type == TypeManager.ushort_type){
2438 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2440 } else if (target_type == TypeManager.int64_type)
2442 else if (target_type == TypeManager.uint64_type){
2448 } else if (c is UIntConstant){
2449 uint v = ((UIntConstant) c).Value;
2451 if (target_type == TypeManager.int32_type){
2452 if (v <= Int32.MaxValue)
2454 } else if (target_type == TypeManager.char_type){
2455 if (v >= Char.MinValue && v <= Char.MaxValue)
2457 } else if (target_type == TypeManager.byte_type){
2458 if (v <= Byte.MaxValue)
2460 } else if (target_type == TypeManager.sbyte_type){
2461 if (v <= SByte.MaxValue)
2463 } else if (target_type == TypeManager.short_type){
2464 if (v <= UInt16.MaxValue)
2466 } else if (target_type == TypeManager.ushort_type){
2467 if (v <= UInt16.MaxValue)
2469 } else if (target_type == TypeManager.int64_type)
2471 else if (target_type == TypeManager.uint64_type)
2474 } else if (c is LongConstant){
2475 long v = ((LongConstant) c).Value;
2477 if (target_type == TypeManager.int32_type){
2478 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2480 } else if (target_type == TypeManager.uint32_type){
2481 if (v >= 0 && v <= UInt32.MaxValue)
2483 } else if (target_type == TypeManager.char_type){
2484 if (v >= Char.MinValue && v <= Char.MaxValue)
2486 } else if (target_type == TypeManager.byte_type){
2487 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2489 } else if (target_type == TypeManager.sbyte_type){
2490 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2492 } else if (target_type == TypeManager.short_type){
2493 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2495 } else if (target_type == TypeManager.ushort_type){
2496 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2498 } else if (target_type == TypeManager.uint64_type){
2503 } else if (c is ULongConstant){
2504 ulong v = ((ULongConstant) c).Value;
2506 if (target_type == TypeManager.int32_type){
2507 if (v <= Int32.MaxValue)
2509 } else if (target_type == TypeManager.uint32_type){
2510 if (v <= UInt32.MaxValue)
2512 } else if (target_type == TypeManager.char_type){
2513 if (v >= Char.MinValue && v <= Char.MaxValue)
2515 } else if (target_type == TypeManager.byte_type){
2516 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2518 } else if (target_type == TypeManager.sbyte_type){
2519 if (v <= (int) SByte.MaxValue)
2521 } else if (target_type == TypeManager.short_type){
2522 if (v <= UInt16.MaxValue)
2524 } else if (target_type == TypeManager.ushort_type){
2525 if (v <= UInt16.MaxValue)
2527 } else if (target_type == TypeManager.int64_type){
2528 if (v <= Int64.MaxValue)
2532 } else if (c is ByteConstant){
2533 byte v = ((ByteConstant) c).Value;
2535 if (target_type == TypeManager.int32_type)
2537 else if (target_type == TypeManager.uint32_type)
2539 else if (target_type == TypeManager.char_type)
2541 else if (target_type == TypeManager.sbyte_type){
2542 if (v <= SByte.MaxValue)
2544 } else if (target_type == TypeManager.short_type)
2546 else if (target_type == TypeManager.ushort_type)
2548 else if (target_type == TypeManager.int64_type)
2550 else if (target_type == TypeManager.uint64_type)
2553 } else if (c is SByteConstant){
2554 sbyte v = ((SByteConstant) c).Value;
2556 if (target_type == TypeManager.int32_type)
2558 else if (target_type == TypeManager.uint32_type){
2561 } else if (target_type == TypeManager.char_type){
2564 } else if (target_type == TypeManager.byte_type){
2567 } else if (target_type == TypeManager.short_type)
2569 else if (target_type == TypeManager.ushort_type){
2572 } else if (target_type == TypeManager.int64_type)
2574 else if (target_type == TypeManager.uint64_type){
2579 } else if (c is ShortConstant){
2580 short v = ((ShortConstant) c).Value;
2582 if (target_type == TypeManager.int32_type){
2584 } else if (target_type == TypeManager.uint32_type){
2587 } else if (target_type == TypeManager.char_type){
2590 } else if (target_type == TypeManager.byte_type){
2591 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2593 } else if (target_type == TypeManager.sbyte_type){
2594 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2596 } else if (target_type == TypeManager.ushort_type){
2599 } else if (target_type == TypeManager.int64_type)
2601 else if (target_type == TypeManager.uint64_type)
2605 } else if (c is UShortConstant){
2606 ushort v = ((UShortConstant) c).Value;
2608 if (target_type == TypeManager.int32_type)
2610 else if (target_type == TypeManager.uint32_type)
2612 else if (target_type == TypeManager.char_type){
2613 if (v >= Char.MinValue && v <= Char.MaxValue)
2615 } else if (target_type == TypeManager.byte_type){
2616 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2618 } else if (target_type == TypeManager.sbyte_type){
2619 if (v <= SByte.MaxValue)
2621 } else if (target_type == TypeManager.short_type){
2622 if (v <= Int16.MaxValue)
2624 } else if (target_type == TypeManager.int64_type)
2626 else if (target_type == TypeManager.uint64_type)
2630 } else if (c is CharConstant){
2631 char v = ((CharConstant) c).Value;
2633 if (target_type == TypeManager.int32_type)
2635 else if (target_type == TypeManager.uint32_type)
2637 else if (target_type == TypeManager.byte_type){
2638 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2640 } else if (target_type == TypeManager.sbyte_type){
2641 if (v <= SByte.MaxValue)
2643 } else if (target_type == TypeManager.short_type){
2644 if (v <= Int16.MaxValue)
2646 } else if (target_type == TypeManager.ushort_type)
2648 else if (target_type == TypeManager.int64_type)
2650 else if (target_type == TypeManager.uint64_type)
2655 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2660 // Load the object from the pointer.
2662 public static void LoadFromPtr (ILGenerator ig, Type t)
2664 if (t == TypeManager.int32_type)
2665 ig.Emit (OpCodes.Ldind_I4);
2666 else if (t == TypeManager.uint32_type)
2667 ig.Emit (OpCodes.Ldind_U4);
2668 else if (t == TypeManager.short_type)
2669 ig.Emit (OpCodes.Ldind_I2);
2670 else if (t == TypeManager.ushort_type)
2671 ig.Emit (OpCodes.Ldind_U2);
2672 else if (t == TypeManager.char_type)
2673 ig.Emit (OpCodes.Ldind_U2);
2674 else if (t == TypeManager.byte_type)
2675 ig.Emit (OpCodes.Ldind_U1);
2676 else if (t == TypeManager.sbyte_type)
2677 ig.Emit (OpCodes.Ldind_I1);
2678 else if (t == TypeManager.uint64_type)
2679 ig.Emit (OpCodes.Ldind_I8);
2680 else if (t == TypeManager.int64_type)
2681 ig.Emit (OpCodes.Ldind_I8);
2682 else if (t == TypeManager.float_type)
2683 ig.Emit (OpCodes.Ldind_R4);
2684 else if (t == TypeManager.double_type)
2685 ig.Emit (OpCodes.Ldind_R8);
2686 else if (t == TypeManager.bool_type)
2687 ig.Emit (OpCodes.Ldind_I1);
2688 else if (t == TypeManager.intptr_type)
2689 ig.Emit (OpCodes.Ldind_I);
2690 else if (TypeManager.IsEnumType (t)) {
2691 if (t == TypeManager.enum_type)
2692 ig.Emit (OpCodes.Ldind_Ref);
2694 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2695 } else if (t.IsValueType)
2696 ig.Emit (OpCodes.Ldobj, t);
2698 ig.Emit (OpCodes.Ldind_Ref);
2702 // The stack contains the pointer and the value of type `type'
2704 public static void StoreFromPtr (ILGenerator ig, Type type)
2706 if (TypeManager.IsEnumType (type))
2707 type = TypeManager.EnumToUnderlying (type);
2708 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2709 ig.Emit (OpCodes.Stind_I4);
2710 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2711 ig.Emit (OpCodes.Stind_I8);
2712 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2713 type == TypeManager.ushort_type)
2714 ig.Emit (OpCodes.Stind_I2);
2715 else if (type == TypeManager.float_type)
2716 ig.Emit (OpCodes.Stind_R4);
2717 else if (type == TypeManager.double_type)
2718 ig.Emit (OpCodes.Stind_R8);
2719 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2720 type == TypeManager.bool_type)
2721 ig.Emit (OpCodes.Stind_I1);
2722 else if (type == TypeManager.intptr_type)
2723 ig.Emit (OpCodes.Stind_I);
2724 else if (type.IsValueType)
2725 ig.Emit (OpCodes.Stobj, type);
2727 ig.Emit (OpCodes.Stind_Ref);
2731 // Returns the size of type `t' if known, otherwise, 0
2733 public static int GetTypeSize (Type t)
2735 t = TypeManager.TypeToCoreType (t);
2736 if (t == TypeManager.int32_type ||
2737 t == TypeManager.uint32_type ||
2738 t == TypeManager.float_type)
2740 else if (t == TypeManager.int64_type ||
2741 t == TypeManager.uint64_type ||
2742 t == TypeManager.double_type)
2744 else if (t == TypeManager.byte_type ||
2745 t == TypeManager.sbyte_type ||
2746 t == TypeManager.bool_type)
2748 else if (t == TypeManager.short_type ||
2749 t == TypeManager.char_type ||
2750 t == TypeManager.ushort_type)
2752 else if (t == TypeManager.decimal_type)
2759 // Default implementation of IAssignMethod.CacheTemporaries
2761 public void CacheTemporaries (EmitContext ec)
2765 static void Error_NegativeArrayIndex (Location loc)
2767 Report.Error (284, loc, "Can not create array with a negative size");
2771 // Converts `source' to an int, uint, long or ulong.
2773 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2777 bool old_checked = ec.CheckState;
2778 ec.CheckState = true;
2780 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2781 if (target == null){
2782 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2783 if (target == null){
2784 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2785 if (target == null){
2786 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2788 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2792 ec.CheckState = old_checked;
2795 // Only positive constants are allowed at compile time
2797 if (target is Constant){
2798 if (target is IntConstant){
2799 if (((IntConstant) target).Value < 0){
2800 Error_NegativeArrayIndex (loc);
2805 if (target is LongConstant){
2806 if (((LongConstant) target).Value < 0){
2807 Error_NegativeArrayIndex (loc);
2820 /// This is just a base class for expressions that can
2821 /// appear on statements (invocations, object creation,
2822 /// assignments, post/pre increment and decrement). The idea
2823 /// being that they would support an extra Emition interface that
2824 /// does not leave a result on the stack.
2826 public abstract class ExpressionStatement : Expression {
2829 /// Requests the expression to be emitted in a `statement'
2830 /// context. This means that no new value is left on the
2831 /// stack after invoking this method (constrasted with
2832 /// Emit that will always leave a value on the stack).
2834 public abstract void EmitStatement (EmitContext ec);
2838 /// This kind of cast is used to encapsulate the child
2839 /// whose type is child.Type into an expression that is
2840 /// reported to return "return_type". This is used to encapsulate
2841 /// expressions which have compatible types, but need to be dealt
2842 /// at higher levels with.
2844 /// For example, a "byte" expression could be encapsulated in one
2845 /// of these as an "unsigned int". The type for the expression
2846 /// would be "unsigned int".
2849 public class EmptyCast : Expression {
2850 protected Expression child;
2852 public EmptyCast (Expression child, Type return_type)
2854 eclass = child.eclass;
2859 public override Expression DoResolve (EmitContext ec)
2861 // This should never be invoked, we are born in fully
2862 // initialized state.
2867 public override void Emit (EmitContext ec)
2874 /// This class is used to wrap literals which belong inside Enums
2876 public class EnumConstant : Constant {
2877 public Constant Child;
2879 public EnumConstant (Constant child, Type enum_type)
2881 eclass = child.eclass;
2886 public override Expression DoResolve (EmitContext ec)
2888 // This should never be invoked, we are born in fully
2889 // initialized state.
2894 public override void Emit (EmitContext ec)
2899 public override object GetValue ()
2901 return Child.GetValue ();
2905 // Converts from one of the valid underlying types for an enumeration
2906 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2907 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2909 public Constant WidenToCompilerConstant ()
2911 Type t = TypeManager.EnumToUnderlying (Child.Type);
2912 object v = ((Constant) Child).GetValue ();;
2914 if (t == TypeManager.int32_type)
2915 return new IntConstant ((int) v);
2916 if (t == TypeManager.uint32_type)
2917 return new UIntConstant ((uint) v);
2918 if (t == TypeManager.int64_type)
2919 return new LongConstant ((long) v);
2920 if (t == TypeManager.uint64_type)
2921 return new ULongConstant ((ulong) v);
2922 if (t == TypeManager.short_type)
2923 return new ShortConstant ((short) v);
2924 if (t == TypeManager.ushort_type)
2925 return new UShortConstant ((ushort) v);
2926 if (t == TypeManager.byte_type)
2927 return new ByteConstant ((byte) v);
2928 if (t == TypeManager.sbyte_type)
2929 return new SByteConstant ((sbyte) v);
2931 throw new Exception ("Invalid enumeration underlying type: " + t);
2935 // Extracts the value in the enumeration on its native representation
2937 public object GetPlainValue ()
2939 Type t = TypeManager.EnumToUnderlying (Child.Type);
2940 object v = ((Constant) Child).GetValue ();;
2942 if (t == TypeManager.int32_type)
2944 if (t == TypeManager.uint32_type)
2946 if (t == TypeManager.int64_type)
2948 if (t == TypeManager.uint64_type)
2950 if (t == TypeManager.short_type)
2952 if (t == TypeManager.ushort_type)
2954 if (t == TypeManager.byte_type)
2956 if (t == TypeManager.sbyte_type)
2962 public override string AsString ()
2964 return Child.AsString ();
2967 public override DoubleConstant ConvertToDouble ()
2969 return Child.ConvertToDouble ();
2972 public override FloatConstant ConvertToFloat ()
2974 return Child.ConvertToFloat ();
2977 public override ULongConstant ConvertToULong ()
2979 return Child.ConvertToULong ();
2982 public override LongConstant ConvertToLong ()
2984 return Child.ConvertToLong ();
2987 public override UIntConstant ConvertToUInt ()
2989 return Child.ConvertToUInt ();
2992 public override IntConstant ConvertToInt ()
2994 return Child.ConvertToInt ();
2999 /// This kind of cast is used to encapsulate Value Types in objects.
3001 /// The effect of it is to box the value type emitted by the previous
3004 public class BoxedCast : EmptyCast {
3006 public BoxedCast (Expression expr)
3007 : base (expr, TypeManager.object_type)
3011 public override Expression DoResolve (EmitContext ec)
3013 // This should never be invoked, we are born in fully
3014 // initialized state.
3019 public override void Emit (EmitContext ec)
3023 ec.ig.Emit (OpCodes.Box, child.Type);
3027 public class UnboxCast : EmptyCast {
3028 public UnboxCast (Expression expr, Type return_type)
3029 : base (expr, return_type)
3033 public override Expression DoResolve (EmitContext ec)
3035 // This should never be invoked, we are born in fully
3036 // initialized state.
3041 public override void Emit (EmitContext ec)
3044 ILGenerator ig = ec.ig;
3047 ig.Emit (OpCodes.Unbox, t);
3049 LoadFromPtr (ig, t);
3054 /// This is used to perform explicit numeric conversions.
3056 /// Explicit numeric conversions might trigger exceptions in a checked
3057 /// context, so they should generate the conv.ovf opcodes instead of
3060 public class ConvCast : EmptyCast {
3061 public enum Mode : byte {
3062 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3064 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3065 U2_I1, U2_U1, U2_I2, U2_CH,
3066 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3067 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3068 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3069 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3070 CH_I1, CH_U1, CH_I2,
3071 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3072 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3078 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3079 : base (child, return_type)
3081 checked_state = ec.CheckState;
3085 public override Expression DoResolve (EmitContext ec)
3087 // This should never be invoked, we are born in fully
3088 // initialized state.
3093 public override void Emit (EmitContext ec)
3095 ILGenerator ig = ec.ig;
3101 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3102 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3103 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3104 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3105 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3107 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3108 case Mode.U1_CH: /* nothing */ break;
3110 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3111 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3112 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3113 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3114 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3115 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3117 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3118 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3119 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3120 case Mode.U2_CH: /* nothing */ break;
3122 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3123 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3124 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3125 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3126 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3127 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3128 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3130 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3131 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3132 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3133 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3134 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3135 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3137 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3138 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3139 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3140 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3141 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3142 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3143 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3144 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3146 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3147 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3148 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3149 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3150 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3151 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3152 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3153 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3155 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3156 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3157 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3159 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3160 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3161 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3162 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3163 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3164 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3165 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3166 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3167 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3169 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3170 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3171 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3172 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3173 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3174 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3175 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3176 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3177 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3178 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3182 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3183 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3184 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3185 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3186 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3188 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3189 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3191 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3192 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3193 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3194 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3195 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3196 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3198 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3199 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3200 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3201 case Mode.U2_CH: /* nothing */ break;
3203 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3204 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3205 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3206 case Mode.I4_U4: /* nothing */ break;
3207 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3208 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3209 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3211 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3212 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3213 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3214 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3215 case Mode.U4_I4: /* nothing */ break;
3216 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3218 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3219 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3220 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3221 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3222 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3223 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3224 case Mode.I8_U8: /* nothing */ break;
3225 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3227 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3228 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3229 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3230 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3231 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3232 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3233 case Mode.U8_I8: /* nothing */ break;
3234 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3236 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3237 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3238 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3240 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3241 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3242 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3243 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3244 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3245 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3246 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3247 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3248 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3250 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3251 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3252 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3253 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3254 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3255 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3256 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3257 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3258 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3259 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3265 public class OpcodeCast : EmptyCast {
3269 public OpcodeCast (Expression child, Type return_type, OpCode op)
3270 : base (child, return_type)
3274 second_valid = false;
3277 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3278 : base (child, return_type)
3283 second_valid = true;
3286 public override Expression DoResolve (EmitContext ec)
3288 // This should never be invoked, we are born in fully
3289 // initialized state.
3294 public override void Emit (EmitContext ec)
3305 /// This kind of cast is used to encapsulate a child and cast it
3306 /// to the class requested
3308 public class ClassCast : EmptyCast {
3309 public ClassCast (Expression child, Type return_type)
3310 : base (child, return_type)
3315 public override Expression DoResolve (EmitContext ec)
3317 // This should never be invoked, we are born in fully
3318 // initialized state.
3323 public override void Emit (EmitContext ec)
3327 ec.ig.Emit (OpCodes.Castclass, type);
3333 /// SimpleName expressions are initially formed of a single
3334 /// word and it only happens at the beginning of the expression.
3338 /// The expression will try to be bound to a Field, a Method
3339 /// group or a Property. If those fail we pass the name to our
3340 /// caller and the SimpleName is compounded to perform a type
3341 /// lookup. The idea behind this process is that we want to avoid
3342 /// creating a namespace map from the assemblies, as that requires
3343 /// the GetExportedTypes function to be called and a hashtable to
3344 /// be constructed which reduces startup time. If later we find
3345 /// that this is slower, we should create a `NamespaceExpr' expression
3346 /// that fully participates in the resolution process.
3348 /// For example `System.Console.WriteLine' is decomposed into
3349 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3351 /// The first SimpleName wont produce a match on its own, so it will
3353 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3355 /// System.Console will produce a TypeExpr match.
3357 /// The downside of this is that we might be hitting `LookupType' too many
3358 /// times with this scheme.
3360 public class SimpleName : Expression, ITypeExpression {
3361 public readonly string Name;
3363 public SimpleName (string name, Location l)
3369 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3371 if (ec.IsFieldInitializer)
3374 "A field initializer cannot reference the non-static field, " +
3375 "method or property `"+name+"'");
3379 "An object reference is required " +
3380 "for the non-static field `"+name+"'");
3384 // Checks whether we are trying to access an instance
3385 // property, method or field from a static body.
3387 Expression MemberStaticCheck (EmitContext ec, Expression e)
3389 if (e is IMemberExpr){
3390 IMemberExpr member = (IMemberExpr) e;
3392 if (!member.IsStatic){
3393 Error_ObjectRefRequired (ec, loc, Name);
3401 public override Expression DoResolve (EmitContext ec)
3403 return SimpleNameResolve (ec, null, false);
3406 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3408 return SimpleNameResolve (ec, right_side, false);
3412 public Expression DoResolveAllowStatic (EmitContext ec)
3414 return SimpleNameResolve (ec, null, true);
3417 public Expression DoResolveType (EmitContext ec)
3420 // Stage 3: Lookup symbol in the various namespaces.
3422 DeclSpace ds = ec.DeclSpace;
3426 if (ec.ResolvingTypeTree){
3427 int errors = Report.Errors;
3428 Type dt = ec.DeclSpace.FindType (loc, Name);
3429 if (Report.Errors != errors)
3433 return new TypeExpr (dt, loc);
3436 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3437 return new TypeExpr (t, loc);
3441 // Stage 2 part b: Lookup up if we are an alias to a type
3444 // Since we are cheating: we only do the Alias lookup for
3445 // namespaces if the name does not include any dots in it
3448 alias_value = ec.DeclSpace.LookupAlias (Name);
3450 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3451 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3452 return new TypeExpr (t, loc);
3454 // we have alias value, but it isn't Type, so try if it's namespace
3455 return new SimpleName (alias_value, loc);
3458 // No match, maybe our parent can compose us
3459 // into something meaningful.
3464 /// 7.5.2: Simple Names.
3466 /// Local Variables and Parameters are handled at
3467 /// parse time, so they never occur as SimpleNames.
3469 /// The `allow_static' flag is used by MemberAccess only
3470 /// and it is used to inform us that it is ok for us to
3471 /// avoid the static check, because MemberAccess might end
3472 /// up resolving the Name as a Type name and the access as
3473 /// a static type access.
3475 /// ie: Type Type; .... { Type.GetType (""); }
3477 /// Type is both an instance variable and a Type; Type.GetType
3478 /// is the static method not an instance method of type.
3480 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3482 Expression e = null;
3485 // Stage 1: Performed by the parser (binding to locals or parameters).
3487 Block current_block = ec.CurrentBlock;
3488 if (current_block != null && current_block.IsVariableDefined (Name)){
3489 LocalVariableReference var;
3491 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3493 if (right_side != null)
3494 return var.ResolveLValue (ec, right_side);
3496 return var.Resolve (ec);
3499 if (current_block != null){
3501 Parameter par = null;
3502 Parameters pars = current_block.Parameters;
3504 par = pars.GetParameterByName (Name, out idx);
3507 ParameterReference param;
3509 param = new ParameterReference (pars, idx, Name, loc);
3511 if (right_side != null)
3512 return param.ResolveLValue (ec, right_side);
3514 return param.Resolve (ec);
3519 // Stage 2: Lookup members
3523 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3524 // Hence we have two different cases
3527 DeclSpace lookup_ds = ec.DeclSpace;
3529 if (lookup_ds.TypeBuilder == null)
3532 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3537 // Classes/structs keep looking, enums break
3539 if (lookup_ds is TypeContainer)
3540 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3543 } while (lookup_ds != null);
3545 if (e == null && ec.ContainerType != null)
3546 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3549 return DoResolveType (ec);
3554 if (e is IMemberExpr) {
3555 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3559 IMemberExpr me = e as IMemberExpr;
3563 // This fails if ResolveMemberAccess() was unable to decide whether
3564 // it's a field or a type of the same name.
3565 if (!me.IsStatic && (me.InstanceExpression == null))
3569 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3570 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3571 "outer type `" + me.DeclaringType + "' via nested type `" +
3572 me.InstanceExpression.Type + "'");
3576 if (right_side != null)
3577 e = e.DoResolveLValue (ec, right_side);
3579 e = e.DoResolve (ec);
3584 if (ec.IsStatic || ec.IsFieldInitializer){
3588 return MemberStaticCheck (ec, e);
3593 public override void Emit (EmitContext ec)
3596 // If this is ever reached, then we failed to
3597 // find the name as a namespace
3600 Error (103, "The name `" + Name +
3601 "' does not exist in the class `" +
3602 ec.DeclSpace.Name + "'");
3605 public override string ToString ()
3612 /// Fully resolved expression that evaluates to a type
3614 public class TypeExpr : Expression, ITypeExpression {
3615 public TypeExpr (Type t, Location l)
3618 eclass = ExprClass.Type;
3622 public virtual Expression DoResolveType (EmitContext ec)
3627 override public Expression DoResolve (EmitContext ec)
3632 override public void Emit (EmitContext ec)
3634 throw new Exception ("Should never be called");
3637 public override string ToString ()
3639 return Type.ToString ();
3644 /// Used to create types from a fully qualified name. These are just used
3645 /// by the parser to setup the core types. A TypeLookupExpression is always
3646 /// classified as a type.
3648 public class TypeLookupExpression : TypeExpr {
3651 public TypeLookupExpression (string name) : base (null, Location.Null)
3656 public override Expression DoResolveType (EmitContext ec)
3659 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3663 public override Expression DoResolve (EmitContext ec)
3665 return DoResolveType (ec);
3668 public override void Emit (EmitContext ec)
3670 throw new Exception ("Should never be called");
3673 public override string ToString ()
3680 /// MethodGroup Expression.
3682 /// This is a fully resolved expression that evaluates to a type
3684 public class MethodGroupExpr : Expression, IMemberExpr {
3685 public MethodBase [] Methods;
3686 Expression instance_expression = null;
3687 bool is_explicit_impl = false;
3689 public MethodGroupExpr (MemberInfo [] mi, Location l)
3691 Methods = new MethodBase [mi.Length];
3692 mi.CopyTo (Methods, 0);
3693 eclass = ExprClass.MethodGroup;
3694 type = TypeManager.object_type;
3698 public MethodGroupExpr (ArrayList list, Location l)
3700 Methods = new MethodBase [list.Count];
3703 list.CopyTo (Methods, 0);
3705 foreach (MemberInfo m in list){
3706 if (!(m is MethodBase)){
3707 Console.WriteLine ("Name " + m.Name);
3708 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3714 eclass = ExprClass.MethodGroup;
3715 type = TypeManager.object_type;
3718 public Type DeclaringType {
3720 return Methods [0].DeclaringType;
3725 // `A method group may have associated an instance expression'
3727 public Expression InstanceExpression {
3729 return instance_expression;
3733 instance_expression = value;
3737 public bool IsExplicitImpl {
3739 return is_explicit_impl;
3743 is_explicit_impl = value;
3747 public string Name {
3749 return Methods [0].Name;
3753 public bool IsInstance {
3755 foreach (MethodBase mb in Methods)
3763 public bool IsStatic {
3765 foreach (MethodBase mb in Methods)
3773 override public Expression DoResolve (EmitContext ec)
3775 if (instance_expression != null) {
3776 instance_expression = instance_expression.DoResolve (ec);
3777 if (instance_expression == null)
3784 public void ReportUsageError ()
3786 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3787 Methods [0].Name + "()' is referenced without parentheses");
3790 override public void Emit (EmitContext ec)
3792 ReportUsageError ();
3795 bool RemoveMethods (bool keep_static)
3797 ArrayList smethods = new ArrayList ();
3799 foreach (MethodBase mb in Methods){
3800 if (mb.IsStatic == keep_static)
3804 if (smethods.Count == 0)
3807 Methods = new MethodBase [smethods.Count];
3808 smethods.CopyTo (Methods, 0);
3814 /// Removes any instance methods from the MethodGroup, returns
3815 /// false if the resulting set is empty.
3817 public bool RemoveInstanceMethods ()
3819 return RemoveMethods (true);
3823 /// Removes any static methods from the MethodGroup, returns
3824 /// false if the resulting set is empty.
3826 public bool RemoveStaticMethods ()
3828 return RemoveMethods (false);
3833 /// Fully resolved expression that evaluates to a Field
3835 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3836 public readonly FieldInfo FieldInfo;
3837 Expression instance_expr;
3839 public FieldExpr (FieldInfo fi, Location l)
3842 eclass = ExprClass.Variable;
3843 type = fi.FieldType;
3847 public string Name {
3849 return FieldInfo.Name;
3853 public bool IsInstance {
3855 return !FieldInfo.IsStatic;
3859 public bool IsStatic {
3861 return FieldInfo.IsStatic;
3865 public Type DeclaringType {
3867 return FieldInfo.DeclaringType;
3871 public Expression InstanceExpression {
3873 return instance_expr;
3877 instance_expr = value;
3881 override public Expression DoResolve (EmitContext ec)
3883 if (!FieldInfo.IsStatic){
3884 if (instance_expr == null){
3885 throw new Exception ("non-static FieldExpr without instance var\n" +
3886 "You have to assign the Instance variable\n" +
3887 "Of the FieldExpr to set this\n");
3890 // Resolve the field's instance expression while flow analysis is turned
3891 // off: when accessing a field "a.b", we must check whether the field
3892 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3893 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3894 ResolveFlags.DisableFlowAnalysis);
3895 if (instance_expr == null)
3899 // If the instance expression is a local variable or parameter.
3900 IVariable var = instance_expr as IVariable;
3901 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3907 void Report_AssignToReadonly (bool is_instance)
3912 msg = "Readonly field can not be assigned outside " +
3913 "of constructor or variable initializer";
3915 msg = "A static readonly field can only be assigned in " +
3916 "a static constructor";
3918 Report.Error (is_instance ? 191 : 198, loc, msg);
3921 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3923 IVariable var = instance_expr as IVariable;
3925 var.SetFieldAssigned (ec, FieldInfo.Name);
3927 Expression e = DoResolve (ec);
3932 if (!FieldInfo.IsInitOnly)
3936 // InitOnly fields can only be assigned in constructors
3939 if (ec.IsConstructor)
3942 Report_AssignToReadonly (true);
3947 override public void Emit (EmitContext ec)
3949 ILGenerator ig = ec.ig;
3950 bool is_volatile = false;
3952 if (FieldInfo is FieldBuilder){
3953 FieldBase f = TypeManager.GetField (FieldInfo);
3955 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3958 f.status |= Field.Status.USED;
3961 if (FieldInfo.IsStatic){
3963 ig.Emit (OpCodes.Volatile);
3965 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3967 if (instance_expr.Type.IsValueType){
3969 LocalTemporary tempo = null;
3971 if (!(instance_expr is IMemoryLocation)){
3972 tempo = new LocalTemporary (
3973 ec, instance_expr.Type);
3975 InstanceExpression.Emit (ec);
3979 ml = (IMemoryLocation) instance_expr;
3981 ml.AddressOf (ec, AddressOp.Load);
3983 instance_expr.Emit (ec);
3986 ig.Emit (OpCodes.Volatile);
3988 ig.Emit (OpCodes.Ldfld, FieldInfo);
3992 public void EmitAssign (EmitContext ec, Expression source)
3994 FieldAttributes fa = FieldInfo.Attributes;
3995 bool is_static = (fa & FieldAttributes.Static) != 0;
3996 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3997 ILGenerator ig = ec.ig;
3999 if (is_readonly && !ec.IsConstructor){
4000 Report_AssignToReadonly (!is_static);
4005 Expression instance = instance_expr;
4007 if (instance.Type.IsValueType){
4008 if (instance is IMemoryLocation){
4009 IMemoryLocation ml = (IMemoryLocation) instance;
4011 ml.AddressOf (ec, AddressOp.Store);
4013 throw new Exception ("The " + instance + " of type " +
4015 " represents a ValueType and does " +
4016 "not implement IMemoryLocation");
4022 if (FieldInfo is FieldBuilder){
4023 FieldBase f = TypeManager.GetField (FieldInfo);
4025 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4026 ig.Emit (OpCodes.Volatile);
4030 ig.Emit (OpCodes.Stsfld, FieldInfo);
4032 ig.Emit (OpCodes.Stfld, FieldInfo);
4034 if (FieldInfo is FieldBuilder){
4035 FieldBase f = TypeManager.GetField (FieldInfo);
4037 f.status |= Field.Status.ASSIGNED;
4041 public void AddressOf (EmitContext ec, AddressOp mode)
4043 ILGenerator ig = ec.ig;
4045 if (FieldInfo is FieldBuilder){
4046 FieldBase f = TypeManager.GetField (FieldInfo);
4047 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4048 ig.Emit (OpCodes.Volatile);
4051 if (FieldInfo is FieldBuilder){
4052 FieldBase f = TypeManager.GetField (FieldInfo);
4054 if ((mode & AddressOp.Store) != 0)
4055 f.status |= Field.Status.ASSIGNED;
4056 if ((mode & AddressOp.Load) != 0)
4057 f.status |= Field.Status.USED;
4061 // Handle initonly fields specially: make a copy and then
4062 // get the address of the copy.
4064 if (FieldInfo.IsInitOnly){
4065 if (ec.IsConstructor) {
4066 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4071 local = ig.DeclareLocal (type);
4072 ig.Emit (OpCodes.Stloc, local);
4073 ig.Emit (OpCodes.Ldloca, local);
4078 if (FieldInfo.IsStatic)
4079 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4081 if (instance_expr is IMemoryLocation)
4082 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4084 instance_expr.Emit (ec);
4085 ig.Emit (OpCodes.Ldflda, FieldInfo);
4091 /// Expression that evaluates to a Property. The Assign class
4092 /// might set the `Value' expression if we are in an assignment.
4094 /// This is not an LValue because we need to re-write the expression, we
4095 /// can not take data from the stack and store it.
4097 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4098 public readonly PropertyInfo PropertyInfo;
4100 MethodInfo [] Accessors;
4103 Expression instance_expr;
4105 public PropertyExpr (PropertyInfo pi, Location l)
4108 eclass = ExprClass.PropertyAccess;
4111 Accessors = TypeManager.GetAccessors (pi);
4113 if (Accessors != null)
4114 foreach (MethodInfo mi in Accessors){
4120 Accessors = new MethodInfo [2];
4122 type = TypeManager.TypeToCoreType (pi.PropertyType);
4125 public string Name {
4127 return PropertyInfo.Name;
4131 public bool IsInstance {
4137 public bool IsStatic {
4143 public Type DeclaringType {
4145 return PropertyInfo.DeclaringType;
4150 // The instance expression associated with this expression
4152 public Expression InstanceExpression {
4154 instance_expr = value;
4158 return instance_expr;
4162 public bool VerifyAssignable ()
4164 if (!PropertyInfo.CanWrite){
4165 Report.Error (200, loc,
4166 "The property `" + PropertyInfo.Name +
4167 "' can not be assigned to, as it has not set accessor");
4174 override public Expression DoResolve (EmitContext ec)
4176 if (!PropertyInfo.CanRead){
4177 Report.Error (154, loc,
4178 "The property `" + PropertyInfo.Name +
4179 "' can not be used in " +
4180 "this context because it lacks a get accessor");
4184 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4185 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4189 if (instance_expr != null) {
4190 instance_expr = instance_expr.DoResolve (ec);
4191 if (instance_expr == null)
4198 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4200 if (!PropertyInfo.CanWrite){
4201 Report.Error (154, loc,
4202 "The property `" + PropertyInfo.Name +
4203 "' can not be used in " +
4204 "this context because it lacks a set accessor");
4208 if (instance_expr != null) {
4209 instance_expr = instance_expr.DoResolve (ec);
4210 if (instance_expr == null)
4217 override public void Emit (EmitContext ec)
4219 MethodInfo method = Accessors [0];
4222 // Special case: length of single dimension array is turned into ldlen
4224 if ((method == TypeManager.system_int_array_get_length) ||
4225 (method == TypeManager.int_array_get_length)){
4226 Type iet = instance_expr.Type;
4229 // System.Array.Length can be called, but the Type does not
4230 // support invoking GetArrayRank, so test for that case first
4232 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4233 instance_expr.Emit (ec);
4234 ec.ig.Emit (OpCodes.Ldlen);
4239 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
4244 // Implements the IAssignMethod interface for assignments
4246 public void EmitAssign (EmitContext ec, Expression source)
4248 Argument arg = new Argument (source, Argument.AType.Expression);
4249 ArrayList args = new ArrayList ();
4252 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
4255 override public void EmitStatement (EmitContext ec)
4258 ec.ig.Emit (OpCodes.Pop);
4263 /// Fully resolved expression that evaluates to an Event
4265 public class EventExpr : Expression, IMemberExpr {
4266 public readonly EventInfo EventInfo;
4267 public Expression instance_expr;
4270 MethodInfo add_accessor, remove_accessor;
4272 public EventExpr (EventInfo ei, Location loc)
4276 eclass = ExprClass.EventAccess;
4278 add_accessor = TypeManager.GetAddMethod (ei);
4279 remove_accessor = TypeManager.GetRemoveMethod (ei);
4281 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4284 if (EventInfo is MyEventBuilder)
4285 type = ((MyEventBuilder) EventInfo).EventType;
4287 type = EventInfo.EventHandlerType;
4290 public string Name {
4292 return EventInfo.Name;
4296 public bool IsInstance {
4302 public bool IsStatic {
4308 public Type DeclaringType {
4310 return EventInfo.DeclaringType;
4314 public Expression InstanceExpression {
4316 return instance_expr;
4320 instance_expr = value;
4324 public override Expression DoResolve (EmitContext ec)
4326 if (instance_expr != null) {
4327 instance_expr = instance_expr.DoResolve (ec);
4328 if (instance_expr == null)
4335 public override void Emit (EmitContext ec)
4337 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4340 public void EmitAddOrRemove (EmitContext ec, Expression source)
4342 Expression handler = ((Binary) source).Right;
4344 Argument arg = new Argument (handler, Argument.AType.Expression);
4345 ArrayList args = new ArrayList ();
4349 if (((Binary) source).Oper == Binary.Operator.Addition)
4350 Invocation.EmitCall (
4351 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4353 Invocation.EmitCall (
4354 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);