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).
61 // This is just as a hint to AddressOf of what will be done with the
64 public enum AddressOp {
71 /// This interface is implemented by variables
73 public interface IMemoryLocation {
75 /// The AddressOf method should generate code that loads
76 /// the address of the object and leaves it on the stack.
78 /// The `mode' argument is used to notify the expression
79 /// of whether this will be used to read from the address or
80 /// write to the address.
82 /// This is just a hint that can be used to provide good error
83 /// reporting, and should have no other side effects.
85 void AddressOf (EmitContext ec, AddressOp mode);
89 /// This interface denotes an expression which evaluates to a member
90 /// of a struct or a class.
92 public interface IMemberExpr
95 /// The name of this member.
102 /// Whether this is a static member.
109 /// The instance expression associated with this member, if it's a
110 /// non-static member.
112 Expression InstanceExpression {
118 /// Base class for expressions
120 public abstract class Expression {
121 public ExprClass eclass;
123 protected Location loc;
135 public Location Location {
142 /// Utility wrapper routine for Error, just to beautify the code
144 public void Error (int error, string s)
146 if (!Location.IsNull (loc))
147 Report.Error (error, loc, s);
149 Report.Error (error, s);
153 /// Utility wrapper routine for Warning, just to beautify the code
155 public void Warning (int warning, string s)
157 if (!Location.IsNull (loc))
158 Report.Warning (warning, loc, s);
160 Report.Warning (warning, s);
164 /// Utility wrapper routine for Warning, only prints the warning if
165 /// warnings of level `level' are enabled.
167 public void Warning (int warning, int level, string s)
169 if (level <= RootContext.WarningLevel)
170 Warning (warning, s);
173 static public void Error_CannotConvertType (Location loc, Type source, Type target)
175 Report.Error (30, loc, "Cannot convert type '" +
176 TypeManager.CSharpName (source) + "' to '" +
177 TypeManager.CSharpName (target) + "'");
181 /// Performs semantic analysis on the Expression
185 /// The Resolve method is invoked to perform the semantic analysis
188 /// The return value is an expression (it can be the
189 /// same expression in some cases) or a new
190 /// expression that better represents this node.
192 /// For example, optimizations of Unary (LiteralInt)
193 /// would return a new LiteralInt with a negated
196 /// If there is an error during semantic analysis,
197 /// then an error should be reported (using Report)
198 /// and a null value should be returned.
200 /// There are two side effects expected from calling
201 /// Resolve(): the the field variable "eclass" should
202 /// be set to any value of the enumeration
203 /// `ExprClass' and the type variable should be set
204 /// to a valid type (this is the type of the
207 public abstract Expression DoResolve (EmitContext ec);
209 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
211 return DoResolve (ec);
215 /// Resolves an expression and performs semantic analysis on it.
219 /// Currently Resolve wraps DoResolve to perform sanity
220 /// checking and assertion checking on what we expect from Resolve.
222 public Expression Resolve (EmitContext ec, ResolveFlags flags)
226 if (this is SimpleName)
227 e = ((SimpleName) this).DoResolveAllowStatic (ec);
234 if (e is SimpleName){
235 SimpleName s = (SimpleName) e;
237 if ((flags & ResolveFlags.SimpleName) == 0) {
240 "The name `" + s.Name + "' could not be found in `" +
241 ec.DeclSpace.Name + "'");
248 if ((e is TypeExpr) || (e is ComposedCast)) {
249 if ((flags & ResolveFlags.Type) == 0) {
259 if ((flags & ResolveFlags.VariableOrValue) == 0) {
265 case ExprClass.MethodGroup:
266 if ((flags & ResolveFlags.MethodGroup) == 0) {
267 ((MethodGroupExpr) e).ReportUsageError ();
272 case ExprClass.Value:
273 case ExprClass.Variable:
274 case ExprClass.PropertyAccess:
275 case ExprClass.EventAccess:
276 case ExprClass.IndexerAccess:
277 if ((flags & ResolveFlags.VariableOrValue) == 0) {
284 throw new Exception ("Expression " + e.GetType () +
285 " ExprClass is Invalid after resolve");
289 throw new Exception (
290 "Expression " + e.GetType () +
291 " did not set its type after Resolve\n" +
292 "called from: " + this.GetType ());
298 /// Resolves an expression and performs semantic analysis on it.
300 public Expression Resolve (EmitContext ec)
302 return Resolve (ec, ResolveFlags.VariableOrValue);
306 /// Resolves an expression for LValue assignment
310 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
311 /// checking and assertion checking on what we expect from Resolve
313 public Expression ResolveLValue (EmitContext ec, Expression right_side)
315 Expression e = DoResolveLValue (ec, right_side);
318 if (e is SimpleName){
319 SimpleName s = (SimpleName) e;
323 "The name `" + s.Name + "' could not be found in `" +
324 ec.DeclSpace.Name + "'");
328 if (e.eclass == ExprClass.Invalid)
329 throw new Exception ("Expression " + e +
330 " ExprClass is Invalid after resolve");
332 if (e.eclass == ExprClass.MethodGroup) {
333 ((MethodGroupExpr) e).ReportUsageError ();
338 throw new Exception ("Expression " + e +
339 " did not set its type after Resolve");
346 /// Emits the code for the expression
350 /// The Emit method is invoked to generate the code
351 /// for the expression.
353 public abstract void Emit (EmitContext ec);
356 /// Protected constructor. Only derivate types should
357 /// be able to be created
360 protected Expression ()
362 eclass = ExprClass.Invalid;
367 /// Returns a literalized version of a literal FieldInfo
371 /// The possible return values are:
372 /// IntConstant, UIntConstant
373 /// LongLiteral, ULongConstant
374 /// FloatConstant, DoubleConstant
377 /// The value returned is already resolved.
379 public static Constant Constantify (object v, Type t)
381 if (t == TypeManager.int32_type)
382 return new IntConstant ((int) v);
383 else if (t == TypeManager.uint32_type)
384 return new UIntConstant ((uint) v);
385 else if (t == TypeManager.int64_type)
386 return new LongConstant ((long) v);
387 else if (t == TypeManager.uint64_type)
388 return new ULongConstant ((ulong) v);
389 else if (t == TypeManager.float_type)
390 return new FloatConstant ((float) v);
391 else if (t == TypeManager.double_type)
392 return new DoubleConstant ((double) v);
393 else if (t == TypeManager.string_type)
394 return new StringConstant ((string) v);
395 else if (t == TypeManager.short_type)
396 return new ShortConstant ((short)v);
397 else if (t == TypeManager.ushort_type)
398 return new UShortConstant ((ushort)v);
399 else if (t == TypeManager.sbyte_type)
400 return new SByteConstant (((sbyte)v));
401 else if (t == TypeManager.byte_type)
402 return new ByteConstant ((byte)v);
403 else if (t == TypeManager.char_type)
404 return new CharConstant ((char)v);
405 else if (t == TypeManager.bool_type)
406 return new BoolConstant ((bool) v);
407 else if (TypeManager.IsEnumType (t)){
408 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
410 return new EnumConstant (e, t);
412 throw new Exception ("Unknown type for constant (" + t +
417 /// Returns a fully formed expression after a MemberLookup
419 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
422 return new EventExpr ((EventInfo) mi, loc);
423 else if (mi is FieldInfo)
424 return new FieldExpr ((FieldInfo) mi, loc);
425 else if (mi is PropertyInfo)
426 return new PropertyExpr ((PropertyInfo) mi, loc);
427 else if (mi is Type){
428 return new TypeExpr ((System.Type) mi, loc);
435 // FIXME: Probably implement a cache for (t,name,current_access_set)?
437 // This code could use some optimizations, but we need to do some
438 // measurements. For example, we could use a delegate to `flag' when
439 // something can not any longer be a method-group (because it is something
443 // If the return value is an Array, then it is an array of
446 // If the return value is an MemberInfo, it is anything, but a Method
450 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
451 // the arguments here and have MemberLookup return only the methods that
452 // match the argument count/type, unlike we are doing now (we delay this
455 // This is so we can catch correctly attempts to invoke instance methods
456 // from a static body (scan for error 120 in ResolveSimpleName).
459 // FIXME: Potential optimization, have a static ArrayList
462 public static Expression MemberLookup (EmitContext ec, Type t, string name,
463 MemberTypes mt, BindingFlags bf, Location loc)
465 MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
470 int count = mi.Length;
473 return new MethodGroupExpr (mi, loc);
475 if (mi [0] is MethodBase)
476 return new MethodGroupExpr (mi, loc);
478 return ExprClassFromMemberInfo (ec, mi [0], loc);
481 public const MemberTypes AllMemberTypes =
482 MemberTypes.Constructor |
486 MemberTypes.NestedType |
487 MemberTypes.Property;
489 public const BindingFlags AllBindingFlags =
490 BindingFlags.Public |
491 BindingFlags.Static |
492 BindingFlags.Instance;
494 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
496 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
499 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
501 return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
505 /// This is a wrapper for MemberLookup that is not used to "probe", but
506 /// to find a final definition. If the final definition is not found, we
507 /// look for private members and display a useful debugging message if we
510 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
513 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
516 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
517 MemberTypes mt, BindingFlags bf, Location loc)
521 int errors = Report.Errors;
523 e = MemberLookup (ec, t, name, mt, bf, loc);
528 // Error has already been reported.
529 if (errors < Report.Errors)
532 e = MemberLookup (ec, t, name, AllMemberTypes,
533 AllBindingFlags | BindingFlags.NonPublic, loc);
536 117, loc, "`" + t + "' does not contain a definition " +
537 "for `" + name + "'");
540 122, loc, "`" + t + "." + name +
541 "' is inaccessible due to its protection level");
547 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
549 EventInfo ei = event_expr.EventInfo;
551 return TypeManager.GetPrivateFieldOfEvent (ei);
554 static EmptyExpression MyEmptyExpr;
555 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
557 Type expr_type = expr.Type;
559 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
560 // if we are a method group, emit a warning
565 if (target_type == TypeManager.object_type) {
567 // A pointer type cannot be converted to object
569 if (expr_type.IsPointer)
572 if (expr_type.IsValueType)
573 return new BoxedCast (expr);
574 if (expr_type.IsClass || expr_type.IsInterface)
575 return new EmptyCast (expr, target_type);
576 } else if (expr_type.IsSubclassOf (target_type)) {
577 return new EmptyCast (expr, target_type);
580 // This code is kind of mirrored inside StandardConversionExists
581 // with the small distinction that we only probe there
583 // Always ensure that the code here and there is in sync
585 // from the null type to any reference-type.
586 if (expr is NullLiteral && !target_type.IsValueType)
587 return new EmptyCast (expr, target_type);
589 // from any class-type S to any interface-type T.
590 if (expr_type.IsClass && target_type.IsInterface) {
591 if (TypeManager.ImplementsInterface (expr_type, target_type))
592 return new EmptyCast (expr, target_type);
597 // from any interface type S to interface-type T.
598 if (expr_type.IsInterface && target_type.IsInterface) {
600 if (TypeManager.ImplementsInterface (expr_type, target_type))
601 return new EmptyCast (expr, target_type);
606 // from an array-type S to an array-type of type T
607 if (expr_type.IsArray && target_type.IsArray) {
608 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
610 Type expr_element_type = expr_type.GetElementType ();
612 if (MyEmptyExpr == null)
613 MyEmptyExpr = new EmptyExpression ();
615 MyEmptyExpr.SetType (expr_element_type);
616 Type target_element_type = target_type.GetElementType ();
618 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
619 if (StandardConversionExists (MyEmptyExpr,
620 target_element_type))
621 return new EmptyCast (expr, target_type);
626 // from an array-type to System.Array
627 if (expr_type.IsArray && target_type == TypeManager.array_type)
628 return new EmptyCast (expr, target_type);
630 // from any delegate type to System.Delegate
631 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
632 target_type == TypeManager.delegate_type)
633 return new EmptyCast (expr, target_type);
635 // from any array-type or delegate type into System.ICloneable.
636 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
637 if (target_type == TypeManager.icloneable_type)
638 return new EmptyCast (expr, target_type);
648 /// Handles expressions like this: decimal d; d = 1;
649 /// and changes them into: decimal d; d = new System.Decimal (1);
651 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
653 ArrayList args = new ArrayList ();
655 args.Add (new Argument (expr, Argument.AType.Expression));
657 Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
659 return ne.Resolve (ec);
663 /// Implicit Numeric Conversions.
665 /// expr is the expression to convert, returns a new expression of type
666 /// target_type or null if an implicit conversion is not possible.
668 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
669 Type target_type, Location loc)
671 Type expr_type = expr.Type;
674 // Attempt to do the implicit constant expression conversions
676 if (expr is IntConstant){
679 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
683 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
685 // Try the implicit constant expression conversion
686 // from long to ulong, instead of a nice routine,
689 long v = ((LongConstant) expr).Value;
691 return new ULongConstant ((ulong) v);
695 // If we have an enumeration, extract the underlying type,
696 // use this during the comparission, but wrap around the original
699 Type real_target_type = target_type;
701 if (TypeManager.IsEnumType (real_target_type))
702 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
704 if (expr_type == real_target_type)
705 return new EmptyCast (expr, target_type);
707 if (expr_type == TypeManager.sbyte_type){
709 // From sbyte to short, int, long, float, double.
711 if (real_target_type == TypeManager.int32_type)
712 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
713 if (real_target_type == TypeManager.int64_type)
714 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
715 if (real_target_type == TypeManager.double_type)
716 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
717 if (real_target_type == TypeManager.float_type)
718 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
719 if (real_target_type == TypeManager.short_type)
720 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
721 if (real_target_type == TypeManager.decimal_type)
722 return InternalTypeConstructor (ec, expr, target_type);
723 } else if (expr_type == TypeManager.byte_type){
725 // From byte to short, ushort, int, uint, long, ulong, float, double
727 if ((real_target_type == TypeManager.short_type) ||
728 (real_target_type == TypeManager.ushort_type) ||
729 (real_target_type == TypeManager.int32_type) ||
730 (real_target_type == TypeManager.uint32_type))
731 return new EmptyCast (expr, target_type);
733 if (real_target_type == TypeManager.uint64_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
735 if (real_target_type == TypeManager.int64_type)
736 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
737 if (real_target_type == TypeManager.float_type)
738 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
739 if (real_target_type == TypeManager.double_type)
740 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
741 if (real_target_type == TypeManager.decimal_type)
742 return InternalTypeConstructor (ec, expr, target_type);
743 } else if (expr_type == TypeManager.short_type){
745 // From short to int, long, float, double
747 if (real_target_type == TypeManager.int32_type)
748 return new EmptyCast (expr, target_type);
749 if (real_target_type == TypeManager.int64_type)
750 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
751 if (real_target_type == TypeManager.double_type)
752 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
753 if (real_target_type == TypeManager.float_type)
754 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
755 if (real_target_type == TypeManager.decimal_type)
756 return InternalTypeConstructor (ec, expr, target_type);
757 } else if (expr_type == TypeManager.ushort_type){
759 // From ushort to int, uint, long, ulong, float, double
761 if (real_target_type == TypeManager.uint32_type)
762 return new EmptyCast (expr, target_type);
764 if (real_target_type == TypeManager.uint64_type)
765 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
766 if (real_target_type == TypeManager.int32_type)
767 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
768 if (real_target_type == TypeManager.int64_type)
769 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
770 if (real_target_type == TypeManager.double_type)
771 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
772 if (real_target_type == TypeManager.float_type)
773 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
774 if (real_target_type == TypeManager.decimal_type)
775 return InternalTypeConstructor (ec, expr, target_type);
776 } else if (expr_type == TypeManager.int32_type){
778 // From int to long, float, double
780 if (real_target_type == TypeManager.int64_type)
781 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
782 if (real_target_type == TypeManager.double_type)
783 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
784 if (real_target_type == TypeManager.float_type)
785 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
786 if (real_target_type == TypeManager.decimal_type)
787 return InternalTypeConstructor (ec, expr, target_type);
788 } else if (expr_type == TypeManager.uint32_type){
790 // From uint to long, ulong, float, double
792 if (real_target_type == TypeManager.int64_type)
793 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
794 if (real_target_type == TypeManager.uint64_type)
795 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
796 if (real_target_type == TypeManager.double_type)
797 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
799 if (real_target_type == TypeManager.float_type)
800 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
802 if (real_target_type == TypeManager.decimal_type)
803 return InternalTypeConstructor (ec, expr, target_type);
804 } else if (expr_type == TypeManager.int64_type){
806 // From long/ulong to float, double
808 if (real_target_type == TypeManager.double_type)
809 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
810 if (real_target_type == TypeManager.float_type)
811 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
812 if (real_target_type == TypeManager.decimal_type)
813 return InternalTypeConstructor (ec, expr, target_type);
814 } else if (expr_type == TypeManager.uint64_type){
816 // From ulong to float, double
818 if (real_target_type == TypeManager.double_type)
819 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
821 if (real_target_type == TypeManager.float_type)
822 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
824 if (real_target_type == TypeManager.decimal_type)
825 return InternalTypeConstructor (ec, expr, target_type);
826 } else if (expr_type == TypeManager.char_type){
828 // From char to ushort, int, uint, long, ulong, float, double
830 if ((real_target_type == TypeManager.ushort_type) ||
831 (real_target_type == TypeManager.int32_type) ||
832 (real_target_type == TypeManager.uint32_type))
833 return new EmptyCast (expr, target_type);
834 if (real_target_type == TypeManager.uint64_type)
835 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
836 if (real_target_type == TypeManager.int64_type)
837 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
838 if (real_target_type == TypeManager.float_type)
839 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
840 if (real_target_type == TypeManager.double_type)
841 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
842 if (real_target_type == TypeManager.decimal_type)
843 return InternalTypeConstructor (ec, expr, target_type);
844 } else if (expr_type == TypeManager.float_type){
848 if (real_target_type == TypeManager.double_type)
849 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
856 // Tests whether an implicit reference conversion exists between expr_type
859 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
861 Type expr_type = expr.Type;
864 // This is the boxed case.
866 if (target_type == TypeManager.object_type) {
867 if ((expr_type.IsClass) ||
868 (expr_type.IsValueType) ||
869 (expr_type.IsInterface))
872 } else if (expr_type.IsSubclassOf (target_type)) {
876 // Please remember that all code below actually comes
877 // from ImplicitReferenceConversion so make sure code remains in sync
879 // from any class-type S to any interface-type T.
880 if (expr_type.IsClass && target_type.IsInterface) {
881 if (TypeManager.ImplementsInterface (expr_type, target_type))
885 // from any interface type S to interface-type T.
886 if (expr_type.IsInterface && target_type.IsInterface)
887 if (TypeManager.ImplementsInterface (expr_type, target_type))
890 // from an array-type S to an array-type of type T
891 if (expr_type.IsArray && target_type.IsArray) {
892 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
894 Type expr_element_type = expr_type.GetElementType ();
896 if (MyEmptyExpr == null)
897 MyEmptyExpr = new EmptyExpression ();
899 MyEmptyExpr.SetType (expr_element_type);
900 Type target_element_type = target_type.GetElementType ();
902 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
903 if (StandardConversionExists (MyEmptyExpr,
904 target_element_type))
909 // from an array-type to System.Array
910 if (expr_type.IsArray && (target_type == TypeManager.array_type))
913 // from any delegate type to System.Delegate
914 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
915 target_type == TypeManager.delegate_type)
916 if (target_type.IsAssignableFrom (expr_type))
919 // from any array-type or delegate type into System.ICloneable.
920 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
921 if (target_type == TypeManager.icloneable_type)
924 // from the null type to any reference-type.
925 if (expr is NullLiteral && !target_type.IsValueType &&
926 !TypeManager.IsEnumType (target_type))
935 /// Same as StandardConversionExists except that it also looks at
936 /// implicit user defined conversions - needed for overload resolution
938 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
940 if (StandardConversionExists (expr, target_type) == true)
943 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
952 /// Determines if a standard implicit conversion exists from
953 /// expr_type to target_type
955 public static bool StandardConversionExists (Expression expr, Type target_type)
957 Type expr_type = expr.Type;
959 if (expr_type == target_type)
962 // First numeric conversions
964 if (expr_type == TypeManager.sbyte_type){
966 // From sbyte to short, int, long, float, double.
968 if ((target_type == TypeManager.int32_type) ||
969 (target_type == TypeManager.int64_type) ||
970 (target_type == TypeManager.double_type) ||
971 (target_type == TypeManager.float_type) ||
972 (target_type == TypeManager.short_type) ||
973 (target_type == TypeManager.decimal_type))
976 } else if (expr_type == TypeManager.byte_type){
978 // From byte to short, ushort, int, uint, long, ulong, float, double
980 if ((target_type == TypeManager.short_type) ||
981 (target_type == TypeManager.ushort_type) ||
982 (target_type == TypeManager.int32_type) ||
983 (target_type == TypeManager.uint32_type) ||
984 (target_type == TypeManager.uint64_type) ||
985 (target_type == TypeManager.int64_type) ||
986 (target_type == TypeManager.float_type) ||
987 (target_type == TypeManager.double_type) ||
988 (target_type == TypeManager.decimal_type))
991 } else if (expr_type == TypeManager.short_type){
993 // From short to int, long, float, double
995 if ((target_type == TypeManager.int32_type) ||
996 (target_type == TypeManager.int64_type) ||
997 (target_type == TypeManager.double_type) ||
998 (target_type == TypeManager.float_type) ||
999 (target_type == TypeManager.decimal_type))
1002 } else if (expr_type == TypeManager.ushort_type){
1004 // From ushort to int, uint, long, ulong, float, double
1006 if ((target_type == TypeManager.uint32_type) ||
1007 (target_type == TypeManager.uint64_type) ||
1008 (target_type == TypeManager.int32_type) ||
1009 (target_type == TypeManager.int64_type) ||
1010 (target_type == TypeManager.double_type) ||
1011 (target_type == TypeManager.float_type) ||
1012 (target_type == TypeManager.decimal_type))
1015 } else if (expr_type == TypeManager.int32_type){
1017 // From int to long, float, double
1019 if ((target_type == TypeManager.int64_type) ||
1020 (target_type == TypeManager.double_type) ||
1021 (target_type == TypeManager.float_type) ||
1022 (target_type == TypeManager.decimal_type))
1025 } else if (expr_type == TypeManager.uint32_type){
1027 // From uint to long, ulong, float, double
1029 if ((target_type == TypeManager.int64_type) ||
1030 (target_type == TypeManager.uint64_type) ||
1031 (target_type == TypeManager.double_type) ||
1032 (target_type == TypeManager.float_type) ||
1033 (target_type == TypeManager.decimal_type))
1036 } else if ((expr_type == TypeManager.uint64_type) ||
1037 (expr_type == TypeManager.int64_type)) {
1039 // From long/ulong to float, double
1041 if ((target_type == TypeManager.double_type) ||
1042 (target_type == TypeManager.float_type) ||
1043 (target_type == TypeManager.decimal_type))
1046 } else if (expr_type == TypeManager.char_type){
1048 // From char to ushort, int, uint, long, ulong, float, double
1050 if ((target_type == TypeManager.ushort_type) ||
1051 (target_type == TypeManager.int32_type) ||
1052 (target_type == TypeManager.uint32_type) ||
1053 (target_type == TypeManager.uint64_type) ||
1054 (target_type == TypeManager.int64_type) ||
1055 (target_type == TypeManager.float_type) ||
1056 (target_type == TypeManager.double_type) ||
1057 (target_type == TypeManager.decimal_type))
1060 } else if (expr_type == TypeManager.float_type){
1064 if (target_type == TypeManager.double_type)
1068 if (ImplicitReferenceConversionExists (expr, target_type))
1071 if (expr is IntConstant){
1072 int value = ((IntConstant) expr).Value;
1074 if (target_type == TypeManager.sbyte_type){
1075 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1077 } else if (target_type == TypeManager.byte_type){
1078 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1080 } else if (target_type == TypeManager.short_type){
1081 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1083 } else if (target_type == TypeManager.ushort_type){
1084 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1086 } else if (target_type == TypeManager.uint32_type){
1089 } else if (target_type == TypeManager.uint64_type){
1091 // we can optimize this case: a positive int32
1092 // always fits on a uint64. But we need an opcode
1099 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1103 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1105 // Try the implicit constant expression conversion
1106 // from long to ulong, instead of a nice routine,
1107 // we just inline it
1109 long v = ((LongConstant) expr).Value;
1114 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1115 IntLiteral i = (IntLiteral) expr;
1124 // Used internally by FindMostEncompassedType, this is used
1125 // to avoid creating lots of objects in the tight loop inside
1126 // FindMostEncompassedType
1128 static EmptyExpression priv_fmet_param;
1131 /// Finds "most encompassed type" according to the spec (13.4.2)
1132 /// amongst the methods in the MethodGroupExpr
1134 static Type FindMostEncompassedType (ArrayList types)
1138 if (priv_fmet_param == null)
1139 priv_fmet_param = new EmptyExpression ();
1141 foreach (Type t in types){
1142 priv_fmet_param.SetType (t);
1149 if (StandardConversionExists (priv_fmet_param, best))
1157 // Used internally by FindMostEncompassingType, this is used
1158 // to avoid creating lots of objects in the tight loop inside
1159 // FindMostEncompassingType
1161 static EmptyExpression priv_fmee_ret;
1164 /// Finds "most encompassing type" according to the spec (13.4.2)
1165 /// amongst the types in the given set
1167 static Type FindMostEncompassingType (ArrayList types)
1171 if (priv_fmee_ret == null)
1172 priv_fmee_ret = new EmptyExpression ();
1174 foreach (Type t in types){
1175 priv_fmee_ret.SetType (best);
1182 if (StandardConversionExists (priv_fmee_ret, t))
1190 // Used to avoid creating too many objects
1192 static EmptyExpression priv_fms_expr;
1195 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1196 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1197 /// for explicit and implicit conversion operators.
1199 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1200 bool apply_explicit_conv_rules,
1203 ArrayList src_types_set = new ArrayList ();
1205 if (priv_fms_expr == null)
1206 priv_fms_expr = new EmptyExpression ();
1209 // If any operator converts from S then Sx = S
1211 foreach (MethodBase mb in me.Methods){
1212 ParameterData pd = Invocation.GetParameterData (mb);
1213 Type param_type = pd.ParameterType (0);
1215 if (param_type == source_type)
1218 if (apply_explicit_conv_rules) {
1221 // Find the set of applicable user-defined conversion operators, U. This set
1223 // user-defined implicit or explicit conversion operators declared by
1224 // the classes or structs in D that convert from a type encompassing
1225 // or encompassed by S to a type encompassing or encompassed by T
1227 priv_fms_expr.SetType (param_type);
1228 if (StandardConversionExists (priv_fms_expr, source_type))
1229 src_types_set.Add (param_type);
1231 priv_fms_expr.SetType (source_type);
1232 if (StandardConversionExists (priv_fms_expr, param_type))
1233 src_types_set.Add (param_type);
1237 // Only if S is encompassed by param_type
1239 priv_fms_expr.SetType (source_type);
1240 if (StandardConversionExists (priv_fms_expr, param_type))
1241 src_types_set.Add (param_type);
1246 // Explicit Conv rules
1248 if (apply_explicit_conv_rules) {
1249 ArrayList candidate_set = new ArrayList ();
1251 foreach (Type param_type in src_types_set){
1252 priv_fms_expr.SetType (source_type);
1254 if (StandardConversionExists (priv_fms_expr, param_type))
1255 candidate_set.Add (param_type);
1258 if (candidate_set.Count != 0)
1259 return FindMostEncompassedType (candidate_set);
1265 if (apply_explicit_conv_rules)
1266 return FindMostEncompassingType (src_types_set);
1268 return FindMostEncompassedType (src_types_set);
1272 // Useful in avoiding proliferation of objects
1274 static EmptyExpression priv_fmt_expr;
1277 /// Finds the most specific target Tx according to section 13.4.4
1279 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1280 bool apply_explicit_conv_rules,
1283 ArrayList tgt_types_set = new ArrayList ();
1285 if (priv_fmt_expr == null)
1286 priv_fmt_expr = new EmptyExpression ();
1289 // If any operator converts to T then Tx = T
1291 foreach (MethodInfo mi in me.Methods){
1292 Type ret_type = mi.ReturnType;
1294 if (ret_type == target)
1297 if (apply_explicit_conv_rules) {
1300 // Find the set of applicable user-defined conversion operators, U.
1302 // This set consists of the
1303 // user-defined implicit or explicit conversion operators declared by
1304 // the classes or structs in D that convert from a type encompassing
1305 // or encompassed by S to a type encompassing or encompassed by T
1307 priv_fms_expr.SetType (ret_type);
1308 if (StandardConversionExists (priv_fms_expr, target))
1309 tgt_types_set.Add (ret_type);
1311 priv_fms_expr.SetType (target);
1312 if (StandardConversionExists (priv_fms_expr, ret_type))
1313 tgt_types_set.Add (ret_type);
1317 // Only if T is encompassed by param_type
1319 priv_fms_expr.SetType (ret_type);
1320 if (StandardConversionExists (priv_fms_expr, target))
1321 tgt_types_set.Add (ret_type);
1326 // Explicit conv rules
1328 if (apply_explicit_conv_rules) {
1329 ArrayList candidate_set = new ArrayList ();
1331 foreach (Type ret_type in tgt_types_set){
1332 priv_fmt_expr.SetType (ret_type);
1334 if (StandardConversionExists (priv_fmt_expr, target))
1335 candidate_set.Add (ret_type);
1338 if (candidate_set.Count != 0)
1339 return FindMostEncompassingType (candidate_set);
1343 // Okay, final case !
1345 if (apply_explicit_conv_rules)
1346 return FindMostEncompassedType (tgt_types_set);
1348 return FindMostEncompassingType (tgt_types_set);
1352 /// User-defined Implicit conversions
1354 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1355 Type target, Location loc)
1357 return UserDefinedConversion (ec, source, target, loc, false);
1361 /// User-defined Explicit conversions
1363 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1364 Type target, Location loc)
1366 return UserDefinedConversion (ec, source, target, loc, true);
1370 /// Computes the MethodGroup for the user-defined conversion
1371 /// operators from source_type to target_type. `look_for_explicit'
1372 /// controls whether we should also include the list of explicit
1375 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1376 Type source_type, Type target_type,
1377 Location loc, bool look_for_explicit)
1379 Expression mg1 = null, mg2 = null;
1380 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1384 // FIXME : How does the False operator come into the picture ?
1385 // This doesn't look complete and very correct !
1387 if (target_type == TypeManager.bool_type && !look_for_explicit)
1388 op_name = "op_True";
1390 op_name = "op_Implicit";
1392 MethodGroupExpr union3;
1394 mg1 = MethodLookup (ec, source_type, op_name, loc);
1395 if (source_type.BaseType != null)
1396 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1399 union3 = (MethodGroupExpr) mg2;
1400 else if (mg2 == null)
1401 union3 = (MethodGroupExpr) mg1;
1403 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1405 mg1 = MethodLookup (ec, target_type, op_name, loc);
1408 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1410 union3 = (MethodGroupExpr) mg1;
1413 if (target_type.BaseType != null)
1414 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1418 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1420 union3 = (MethodGroupExpr) mg1;
1423 MethodGroupExpr union4 = null;
1425 if (look_for_explicit) {
1426 op_name = "op_Explicit";
1428 mg5 = MemberLookup (ec, source_type, op_name, loc);
1429 if (source_type.BaseType != null)
1430 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1432 mg7 = MemberLookup (ec, target_type, op_name, loc);
1433 if (target_type.BaseType != null)
1434 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1436 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1437 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1439 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1442 return Invocation.MakeUnionSet (union3, union4, loc);
1446 /// User-defined conversions
1448 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1449 Type target, Location loc,
1450 bool look_for_explicit)
1452 MethodGroupExpr union;
1453 Type source_type = source.Type;
1454 MethodBase method = null;
1456 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1460 Type most_specific_source, most_specific_target;
1463 foreach (MethodBase m in union.Methods){
1464 Console.WriteLine ("Name: " + m.Name);
1465 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1469 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1470 if (most_specific_source == null)
1473 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1474 if (most_specific_target == null)
1479 foreach (MethodBase mb in union.Methods){
1480 ParameterData pd = Invocation.GetParameterData (mb);
1481 MethodInfo mi = (MethodInfo) mb;
1483 if (pd.ParameterType (0) == most_specific_source &&
1484 mi.ReturnType == most_specific_target) {
1490 if (method == null || count > 1) {
1491 Report.Error (-11, loc, "Ambiguous user defined conversion");
1496 // This will do the conversion to the best match that we
1497 // found. Now we need to perform an implict standard conversion
1498 // if the best match was not the type that we were requested
1501 if (look_for_explicit)
1502 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1504 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1510 e = new UserCast ((MethodInfo) method, source, loc);
1511 if (e.Type != target){
1512 if (!look_for_explicit)
1513 e = ConvertImplicitStandard (ec, e, target, loc);
1515 e = ConvertExplicitStandard (ec, e, target, loc);
1521 /// Converts implicitly the resolved expression `expr' into the
1522 /// `target_type'. It returns a new expression that can be used
1523 /// in a context that expects a `target_type'.
1525 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1526 Type target_type, Location loc)
1528 Type expr_type = expr.Type;
1531 if (expr_type == target_type)
1534 if (target_type == null)
1535 throw new Exception ("Target type is null");
1537 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1541 e = ImplicitUserConversion (ec, expr, target_type, loc);
1550 /// Attempts to apply the `Standard Implicit
1551 /// Conversion' rules to the expression `expr' into
1552 /// the `target_type'. It returns a new expression
1553 /// that can be used in a context that expects a
1556 /// This is different from `ConvertImplicit' in that the
1557 /// user defined implicit conversions are excluded.
1559 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1560 Type target_type, Location loc)
1562 Type expr_type = expr.Type;
1565 if (expr_type == target_type)
1568 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1572 e = ImplicitReferenceConversion (expr, target_type);
1576 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1577 IntLiteral i = (IntLiteral) expr;
1580 return new EmptyCast (expr, target_type);
1584 if (expr_type.IsPointer){
1585 if (target_type == TypeManager.void_ptr_type)
1586 return new EmptyCast (expr, target_type);
1589 // yep, comparing pointer types cant be done with
1590 // t1 == t2, we have to compare their element types.
1592 if (target_type.IsPointer){
1593 if (target_type.GetElementType()==expr_type.GetElementType())
1598 if (target_type.IsPointer){
1599 if (expr is NullLiteral)
1600 return new EmptyCast (expr, target_type);
1608 /// Attemps to perform an implict constant conversion of the IntConstant
1609 /// into a different data type using casts (See Implicit Constant
1610 /// Expression Conversions)
1612 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1614 int value = ic.Value;
1617 // FIXME: This could return constants instead of EmptyCasts
1619 if (target_type == TypeManager.sbyte_type){
1620 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1621 return new SByteConstant ((sbyte) value);
1622 } else if (target_type == TypeManager.byte_type){
1623 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1624 return new ByteConstant ((byte) value);
1625 } else if (target_type == TypeManager.short_type){
1626 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1627 return new ShortConstant ((short) value);
1628 } else if (target_type == TypeManager.ushort_type){
1629 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1630 return new UShortConstant ((ushort) value);
1631 } else if (target_type == TypeManager.uint32_type){
1633 return new UIntConstant ((uint) value);
1634 } else if (target_type == TypeManager.uint64_type){
1636 // we can optimize this case: a positive int32
1637 // always fits on a uint64. But we need an opcode
1641 return new ULongConstant ((ulong) value);
1644 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1645 return new EnumConstant (ic, target_type);
1650 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1652 string msg = "Cannot convert implicitly from `"+
1653 TypeManager.CSharpName (source) + "' to `" +
1654 TypeManager.CSharpName (target) + "'";
1656 Report.Error (29, loc, msg);
1660 /// Attemptes to implicityly convert `target' into `type', using
1661 /// ConvertImplicit. If there is no implicit conversion, then
1662 /// an error is signaled
1664 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1665 Type target_type, Location loc)
1669 e = ConvertImplicit (ec, source, target_type, loc);
1673 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1674 Report.Error (664, loc,
1675 "Double literal cannot be implicitly converted to " +
1676 "float type, use F suffix to create a float literal");
1679 Error_CannotConvertImplicit (loc, source.Type, target_type);
1685 /// Performs the explicit numeric conversions
1687 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1689 Type expr_type = expr.Type;
1692 // If we have an enumeration, extract the underlying type,
1693 // use this during the comparission, but wrap around the original
1696 Type real_target_type = target_type;
1698 if (TypeManager.IsEnumType (real_target_type))
1699 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1701 if (expr_type == TypeManager.sbyte_type){
1703 // From sbyte to byte, ushort, uint, ulong, char
1705 if (real_target_type == TypeManager.byte_type)
1706 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1707 if (real_target_type == TypeManager.ushort_type)
1708 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1709 if (real_target_type == TypeManager.uint32_type)
1710 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1711 if (real_target_type == TypeManager.uint64_type)
1712 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1713 if (real_target_type == TypeManager.char_type)
1714 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1715 } else if (expr_type == TypeManager.byte_type){
1717 // From byte to sbyte and char
1719 if (real_target_type == TypeManager.sbyte_type)
1720 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1721 if (real_target_type == TypeManager.char_type)
1722 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1723 } else if (expr_type == TypeManager.short_type){
1725 // From short to sbyte, byte, ushort, uint, ulong, char
1727 if (real_target_type == TypeManager.sbyte_type)
1728 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1729 if (real_target_type == TypeManager.byte_type)
1730 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1731 if (real_target_type == TypeManager.ushort_type)
1732 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1733 if (real_target_type == TypeManager.uint32_type)
1734 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1735 if (real_target_type == TypeManager.uint64_type)
1736 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1737 if (real_target_type == TypeManager.char_type)
1738 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1739 } else if (expr_type == TypeManager.ushort_type){
1741 // From ushort to sbyte, byte, short, char
1743 if (real_target_type == TypeManager.sbyte_type)
1744 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1745 if (real_target_type == TypeManager.byte_type)
1746 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1747 if (real_target_type == TypeManager.short_type)
1748 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1749 if (real_target_type == TypeManager.char_type)
1750 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1751 } else if (expr_type == TypeManager.int32_type){
1753 // From int to sbyte, byte, short, ushort, uint, ulong, char
1755 if (real_target_type == TypeManager.sbyte_type)
1756 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1757 if (real_target_type == TypeManager.byte_type)
1758 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1759 if (real_target_type == TypeManager.short_type)
1760 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1761 if (real_target_type == TypeManager.ushort_type)
1762 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1763 if (real_target_type == TypeManager.uint32_type)
1764 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1765 if (real_target_type == TypeManager.uint64_type)
1766 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1767 if (real_target_type == TypeManager.char_type)
1768 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1769 } else if (expr_type == TypeManager.uint32_type){
1771 // From uint to sbyte, byte, short, ushort, int, char
1773 if (real_target_type == TypeManager.sbyte_type)
1774 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1775 if (real_target_type == TypeManager.byte_type)
1776 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1777 if (real_target_type == TypeManager.short_type)
1778 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1779 if (real_target_type == TypeManager.ushort_type)
1780 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1781 if (real_target_type == TypeManager.int32_type)
1782 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1783 if (real_target_type == TypeManager.char_type)
1784 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1785 } else if (expr_type == TypeManager.int64_type){
1787 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1789 if (real_target_type == TypeManager.sbyte_type)
1790 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1791 if (real_target_type == TypeManager.byte_type)
1792 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1793 if (real_target_type == TypeManager.short_type)
1794 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1795 if (real_target_type == TypeManager.ushort_type)
1796 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1797 if (real_target_type == TypeManager.int32_type)
1798 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1799 if (real_target_type == TypeManager.uint32_type)
1800 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1801 if (real_target_type == TypeManager.uint64_type)
1802 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1803 if (real_target_type == TypeManager.char_type)
1804 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1805 } else if (expr_type == TypeManager.uint64_type){
1807 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1809 if (real_target_type == TypeManager.sbyte_type)
1810 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1811 if (real_target_type == TypeManager.byte_type)
1812 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1813 if (real_target_type == TypeManager.short_type)
1814 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1815 if (real_target_type == TypeManager.ushort_type)
1816 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1817 if (real_target_type == TypeManager.int32_type)
1818 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1819 if (real_target_type == TypeManager.uint32_type)
1820 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1821 if (real_target_type == TypeManager.int64_type)
1822 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1823 if (real_target_type == TypeManager.char_type)
1824 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1825 } else if (expr_type == TypeManager.char_type){
1827 // From char to sbyte, byte, short
1829 if (real_target_type == TypeManager.sbyte_type)
1830 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1831 if (real_target_type == TypeManager.byte_type)
1832 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1833 if (real_target_type == TypeManager.short_type)
1834 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1835 } else if (expr_type == TypeManager.float_type){
1837 // From float to sbyte, byte, short,
1838 // ushort, int, uint, long, ulong, char
1841 if (real_target_type == TypeManager.sbyte_type)
1842 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1843 if (real_target_type == TypeManager.byte_type)
1844 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1845 if (real_target_type == TypeManager.short_type)
1846 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1847 if (real_target_type == TypeManager.ushort_type)
1848 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1849 if (real_target_type == TypeManager.int32_type)
1850 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1851 if (real_target_type == TypeManager.uint32_type)
1852 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1853 if (real_target_type == TypeManager.int64_type)
1854 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1855 if (real_target_type == TypeManager.uint64_type)
1856 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1857 if (real_target_type == TypeManager.char_type)
1858 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1859 if (real_target_type == TypeManager.decimal_type)
1860 return InternalTypeConstructor (ec, expr, target_type);
1861 } else if (expr_type == TypeManager.double_type){
1863 // From double to byte, byte, short,
1864 // ushort, int, uint, long, ulong,
1865 // char, float or decimal
1867 if (real_target_type == TypeManager.sbyte_type)
1868 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1869 if (real_target_type == TypeManager.byte_type)
1870 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1871 if (real_target_type == TypeManager.short_type)
1872 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1873 if (real_target_type == TypeManager.ushort_type)
1874 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1875 if (real_target_type == TypeManager.int32_type)
1876 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1877 if (real_target_type == TypeManager.uint32_type)
1878 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1879 if (real_target_type == TypeManager.int64_type)
1880 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1881 if (real_target_type == TypeManager.uint64_type)
1882 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1883 if (real_target_type == TypeManager.char_type)
1884 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1885 if (real_target_type == TypeManager.float_type)
1886 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1887 if (real_target_type == TypeManager.decimal_type)
1888 return InternalTypeConstructor (ec, expr, target_type);
1891 // decimal is taken care of by the op_Explicit methods.
1897 /// Returns whether an explicit reference conversion can be performed
1898 /// from source_type to target_type
1900 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1902 bool target_is_value_type = target_type.IsValueType;
1904 if (source_type == target_type)
1908 // From object to any reference type
1910 if (source_type == TypeManager.object_type && !target_is_value_type)
1914 // From any class S to any class-type T, provided S is a base class of T
1916 if (target_type.IsSubclassOf (source_type))
1920 // From any interface type S to any interface T provided S is not derived from T
1922 if (source_type.IsInterface && target_type.IsInterface){
1923 if (!target_type.IsSubclassOf (source_type))
1928 // From any class type S to any interface T, provided S is not sealed
1929 // and provided S does not implement T.
1931 if (target_type.IsInterface && !source_type.IsSealed &&
1932 !TypeManager.ImplementsInterface (source_type, target_type))
1936 // From any interface-type S to to any class type T, provided T is not
1937 // sealed, or provided T implements S.
1939 if (source_type.IsInterface &&
1940 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1944 // From an array type S with an element type Se to an array type T with an
1945 // element type Te provided all the following are true:
1946 // * S and T differe only in element type, in other words, S and T
1947 // have the same number of dimensions.
1948 // * Both Se and Te are reference types
1949 // * An explicit referenc conversions exist from Se to Te
1951 if (source_type.IsArray && target_type.IsArray) {
1952 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1954 Type source_element_type = source_type.GetElementType ();
1955 Type target_element_type = target_type.GetElementType ();
1957 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1958 if (ExplicitReferenceConversionExists (source_element_type,
1959 target_element_type))
1965 // From System.Array to any array-type
1966 if (source_type == TypeManager.array_type &&
1967 target_type.IsArray){
1972 // From System delegate to any delegate-type
1974 if (source_type == TypeManager.delegate_type &&
1975 target_type.IsSubclassOf (TypeManager.delegate_type))
1979 // From ICloneable to Array or Delegate types
1981 if (source_type == TypeManager.icloneable_type &&
1982 (target_type == TypeManager.array_type ||
1983 target_type == TypeManager.delegate_type))
1990 /// Implements Explicit Reference conversions
1992 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1994 Type source_type = source.Type;
1995 bool target_is_value_type = target_type.IsValueType;
1998 // From object to any reference type
2000 if (source_type == TypeManager.object_type && !target_is_value_type)
2001 return new ClassCast (source, target_type);
2005 // From any class S to any class-type T, provided S is a base class of T
2007 if (target_type.IsSubclassOf (source_type))
2008 return new ClassCast (source, target_type);
2011 // From any interface type S to any interface T provided S is not derived from T
2013 if (source_type.IsInterface && target_type.IsInterface){
2014 if (TypeManager.ImplementsInterface (source_type, target_type))
2017 return new ClassCast (source, target_type);
2021 // From any class type S to any interface T, provides S is not sealed
2022 // and provided S does not implement T.
2024 if (target_type.IsInterface && !source_type.IsSealed) {
2025 if (TypeManager.ImplementsInterface (source_type, target_type))
2028 return new ClassCast (source, target_type);
2033 // From any interface-type S to to any class type T, provided T is not
2034 // sealed, or provided T implements S.
2036 if (source_type.IsInterface) {
2037 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2038 return new ClassCast (source, target_type);
2043 // From an array type S with an element type Se to an array type T with an
2044 // element type Te provided all the following are true:
2045 // * S and T differe only in element type, in other words, S and T
2046 // have the same number of dimensions.
2047 // * Both Se and Te are reference types
2048 // * An explicit referenc conversions exist from Se to Te
2050 if (source_type.IsArray && target_type.IsArray) {
2051 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2053 Type source_element_type = source_type.GetElementType ();
2054 Type target_element_type = target_type.GetElementType ();
2056 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2057 if (ExplicitReferenceConversionExists (source_element_type,
2058 target_element_type))
2059 return new ClassCast (source, target_type);
2064 // From System.Array to any array-type
2065 if (source_type == TypeManager.array_type &&
2066 target_type.IsArray) {
2067 return new ClassCast (source, target_type);
2071 // From System delegate to any delegate-type
2073 if (source_type == TypeManager.delegate_type &&
2074 target_type.IsSubclassOf (TypeManager.delegate_type))
2075 return new ClassCast (source, target_type);
2078 // From ICloneable to Array or Delegate types
2080 if (source_type == TypeManager.icloneable_type &&
2081 (target_type == TypeManager.array_type ||
2082 target_type == TypeManager.delegate_type))
2083 return new ClassCast (source, target_type);
2089 /// Performs an explicit conversion of the expression `expr' whose
2090 /// type is expr.Type to `target_type'.
2092 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2093 Type target_type, Location loc)
2095 Type expr_type = expr.Type;
2096 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2101 ne = ConvertNumericExplicit (ec, expr, target_type);
2106 // Unboxing conversion.
2108 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2109 return new UnboxCast (expr, target_type);
2114 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2118 // FIXME: Is there any reason we should have EnumConstant
2119 // dealt with here instead of just using always the
2120 // UnderlyingSystemType to wrap the type?
2122 if (expr is EnumConstant)
2123 e = ((EnumConstant) expr).Child;
2125 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2128 Expression t = ConvertImplicit (ec, e, target_type, loc);
2132 return ConvertNumericExplicit (ec, e, target_type);
2135 ne = ConvertReferenceExplicit (expr, target_type);
2140 if (target_type.IsPointer){
2141 if (expr_type.IsPointer)
2142 return new EmptyCast (expr, target_type);
2144 if (expr_type == TypeManager.sbyte_type ||
2145 expr_type == TypeManager.byte_type ||
2146 expr_type == TypeManager.short_type ||
2147 expr_type == TypeManager.ushort_type ||
2148 expr_type == TypeManager.int32_type ||
2149 expr_type == TypeManager.uint32_type ||
2150 expr_type == TypeManager.uint64_type ||
2151 expr_type == TypeManager.int64_type)
2152 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2154 if (expr_type.IsPointer){
2155 if (target_type == TypeManager.sbyte_type ||
2156 target_type == TypeManager.byte_type ||
2157 target_type == TypeManager.short_type ||
2158 target_type == TypeManager.ushort_type ||
2159 target_type == TypeManager.int32_type ||
2160 target_type == TypeManager.uint32_type ||
2161 target_type == TypeManager.uint64_type ||
2162 target_type == TypeManager.int64_type){
2163 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2166 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2171 ce = ConvertNumericExplicit (ec, e, target_type);
2175 // We should always be able to go from an uint32
2176 // implicitly or explicitly to the other integral
2179 throw new Exception ("Internal compiler error");
2184 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2188 Error_CannotConvertType (loc, expr_type, target_type);
2193 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2195 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2196 Type target_type, Location l)
2198 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2203 ne = ConvertNumericExplicit (ec, expr, target_type);
2207 ne = ConvertReferenceExplicit (expr, target_type);
2211 Error_CannotConvertType (l, expr.Type, target_type);
2215 static string ExprClassName (ExprClass c)
2218 case ExprClass.Invalid:
2220 case ExprClass.Value:
2222 case ExprClass.Variable:
2224 case ExprClass.Namespace:
2226 case ExprClass.Type:
2228 case ExprClass.MethodGroup:
2229 return "method group";
2230 case ExprClass.PropertyAccess:
2231 return "property access";
2232 case ExprClass.EventAccess:
2233 return "event access";
2234 case ExprClass.IndexerAccess:
2235 return "indexer access";
2236 case ExprClass.Nothing:
2239 throw new Exception ("Should not happen");
2243 /// Reports that we were expecting `expr' to be of class `expected'
2245 public void Error118 (string expected)
2247 string kind = "Unknown";
2249 kind = ExprClassName (eclass);
2251 Error (118, "Expression denotes a `" + kind +
2252 "' where a `" + expected + "' was expected");
2255 public void Error118 (ResolveFlags flags)
2257 ArrayList valid = new ArrayList (10);
2259 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2260 valid.Add ("variable");
2261 valid.Add ("value");
2264 if ((flags & ResolveFlags.Type) != 0)
2267 if ((flags & ResolveFlags.MethodGroup) != 0)
2268 valid.Add ("method group");
2270 if ((flags & ResolveFlags.SimpleName) != 0)
2271 valid.Add ("simple name");
2273 if (valid.Count == 0)
2274 valid.Add ("unknown");
2276 StringBuilder sb = new StringBuilder ();
2277 for (int i = 0; i < valid.Count; i++) {
2280 else if (i == valid.Count)
2282 sb.Append (valid [i]);
2285 string kind = ExprClassName (eclass);
2287 Error (119, "Expression denotes a `" + kind + "' where " +
2288 "a `" + sb.ToString () + "' was expected");
2291 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2293 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2294 TypeManager.CSharpName (t));
2297 public static void UnsafeError (Location loc)
2299 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2303 /// Converts the IntConstant, UIntConstant, LongConstant or
2304 /// ULongConstant into the integral target_type. Notice
2305 /// that we do not return an `Expression' we do return
2306 /// a boxed integral type.
2308 /// FIXME: Since I added the new constants, we need to
2309 /// also support conversions from CharConstant, ByteConstant,
2310 /// SByteConstant, UShortConstant, ShortConstant
2312 /// This is used by the switch statement, so the domain
2313 /// of work is restricted to the literals above, and the
2314 /// targets are int32, uint32, char, byte, sbyte, ushort,
2315 /// short, uint64 and int64
2317 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2321 if (c.Type == target_type)
2322 return ((Constant) c).GetValue ();
2325 // Make into one of the literals we handle, we dont really care
2326 // about this value as we will just return a few limited types
2328 if (c is EnumConstant)
2329 c = ((EnumConstant)c).WidenToCompilerConstant ();
2331 if (c is IntConstant){
2332 int v = ((IntConstant) c).Value;
2334 if (target_type == TypeManager.uint32_type){
2337 } else if (target_type == TypeManager.char_type){
2338 if (v >= Char.MinValue && v <= Char.MaxValue)
2340 } else if (target_type == TypeManager.byte_type){
2341 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2343 } else if (target_type == TypeManager.sbyte_type){
2344 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2346 } else if (target_type == TypeManager.short_type){
2347 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2349 } else if (target_type == TypeManager.ushort_type){
2350 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2352 } else if (target_type == TypeManager.int64_type)
2354 else if (target_type == TypeManager.uint64_type){
2360 } else if (c is UIntConstant){
2361 uint v = ((UIntConstant) c).Value;
2363 if (target_type == TypeManager.int32_type){
2364 if (v <= Int32.MaxValue)
2366 } else if (target_type == TypeManager.char_type){
2367 if (v >= Char.MinValue && v <= Char.MaxValue)
2369 } else if (target_type == TypeManager.byte_type){
2370 if (v <= Byte.MaxValue)
2372 } else if (target_type == TypeManager.sbyte_type){
2373 if (v <= SByte.MaxValue)
2375 } else if (target_type == TypeManager.short_type){
2376 if (v <= UInt16.MaxValue)
2378 } else if (target_type == TypeManager.ushort_type){
2379 if (v <= UInt16.MaxValue)
2381 } else if (target_type == TypeManager.int64_type)
2383 else if (target_type == TypeManager.uint64_type)
2386 } else if (c is LongConstant){
2387 long v = ((LongConstant) c).Value;
2389 if (target_type == TypeManager.int32_type){
2390 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2392 } else if (target_type == TypeManager.uint32_type){
2393 if (v >= 0 && v <= UInt32.MaxValue)
2395 } else if (target_type == TypeManager.char_type){
2396 if (v >= Char.MinValue && v <= Char.MaxValue)
2398 } else if (target_type == TypeManager.byte_type){
2399 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2401 } else if (target_type == TypeManager.sbyte_type){
2402 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2404 } else if (target_type == TypeManager.short_type){
2405 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2407 } else if (target_type == TypeManager.ushort_type){
2408 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2410 } else if (target_type == TypeManager.uint64_type){
2415 } else if (c is ULongConstant){
2416 ulong v = ((ULongConstant) c).Value;
2418 if (target_type == TypeManager.int32_type){
2419 if (v <= Int32.MaxValue)
2421 } else if (target_type == TypeManager.uint32_type){
2422 if (v <= UInt32.MaxValue)
2424 } else if (target_type == TypeManager.char_type){
2425 if (v >= Char.MinValue && v <= Char.MaxValue)
2427 } else if (target_type == TypeManager.byte_type){
2428 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2430 } else if (target_type == TypeManager.sbyte_type){
2431 if (v <= (int) SByte.MaxValue)
2433 } else if (target_type == TypeManager.short_type){
2434 if (v <= UInt16.MaxValue)
2436 } else if (target_type == TypeManager.ushort_type){
2437 if (v <= UInt16.MaxValue)
2439 } else if (target_type == TypeManager.int64_type){
2440 if (v <= Int64.MaxValue)
2444 } else if (c is ByteConstant){
2445 byte v = ((ByteConstant) c).Value;
2447 if (target_type == TypeManager.int32_type)
2449 else if (target_type == TypeManager.uint32_type)
2451 else if (target_type == TypeManager.char_type)
2453 else if (target_type == TypeManager.sbyte_type){
2454 if (v <= SByte.MaxValue)
2456 } else if (target_type == TypeManager.short_type)
2458 else if (target_type == TypeManager.ushort_type)
2460 else if (target_type == TypeManager.int64_type)
2462 else if (target_type == TypeManager.uint64_type)
2465 } else if (c is SByteConstant){
2466 sbyte v = ((SByteConstant) c).Value;
2468 if (target_type == TypeManager.int32_type)
2470 else if (target_type == TypeManager.uint32_type){
2473 } else if (target_type == TypeManager.char_type){
2476 } else if (target_type == TypeManager.byte_type){
2479 } else if (target_type == TypeManager.short_type)
2481 else if (target_type == TypeManager.ushort_type){
2484 } else if (target_type == TypeManager.int64_type)
2486 else if (target_type == TypeManager.uint64_type){
2491 } else if (c is ShortConstant){
2492 short v = ((ShortConstant) c).Value;
2494 if (target_type == TypeManager.int32_type){
2496 } else if (target_type == TypeManager.uint32_type){
2499 } else if (target_type == TypeManager.char_type){
2502 } else if (target_type == TypeManager.byte_type){
2503 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2505 } else if (target_type == TypeManager.sbyte_type){
2506 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2508 } else if (target_type == TypeManager.ushort_type){
2511 } else if (target_type == TypeManager.int64_type)
2513 else if (target_type == TypeManager.uint64_type)
2517 } else if (c is UShortConstant){
2518 ushort v = ((UShortConstant) c).Value;
2520 if (target_type == TypeManager.int32_type)
2522 else if (target_type == TypeManager.uint32_type)
2524 else if (target_type == TypeManager.char_type){
2525 if (v >= Char.MinValue && v <= Char.MaxValue)
2527 } else if (target_type == TypeManager.byte_type){
2528 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2530 } else if (target_type == TypeManager.sbyte_type){
2531 if (v <= SByte.MaxValue)
2533 } else if (target_type == TypeManager.short_type){
2534 if (v <= Int16.MaxValue)
2536 } else if (target_type == TypeManager.int64_type)
2538 else if (target_type == TypeManager.uint64_type)
2542 } else if (c is CharConstant){
2543 char v = ((CharConstant) c).Value;
2545 if (target_type == TypeManager.int32_type)
2547 else if (target_type == TypeManager.uint32_type)
2549 else if (target_type == TypeManager.byte_type){
2550 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2552 } else if (target_type == TypeManager.sbyte_type){
2553 if (v <= SByte.MaxValue)
2555 } else if (target_type == TypeManager.short_type){
2556 if (v <= Int16.MaxValue)
2558 } else if (target_type == TypeManager.ushort_type)
2560 else if (target_type == TypeManager.int64_type)
2562 else if (target_type == TypeManager.uint64_type)
2567 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2572 // Load the object from the pointer.
2574 public static void LoadFromPtr (ILGenerator ig, Type t)
2576 if (t == TypeManager.int32_type)
2577 ig.Emit (OpCodes.Ldind_I4);
2578 else if (t == TypeManager.uint32_type)
2579 ig.Emit (OpCodes.Ldind_U4);
2580 else if (t == TypeManager.short_type)
2581 ig.Emit (OpCodes.Ldind_I2);
2582 else if (t == TypeManager.ushort_type)
2583 ig.Emit (OpCodes.Ldind_U2);
2584 else if (t == TypeManager.char_type)
2585 ig.Emit (OpCodes.Ldind_U2);
2586 else if (t == TypeManager.byte_type)
2587 ig.Emit (OpCodes.Ldind_U1);
2588 else if (t == TypeManager.sbyte_type)
2589 ig.Emit (OpCodes.Ldind_I1);
2590 else if (t == TypeManager.uint64_type)
2591 ig.Emit (OpCodes.Ldind_I8);
2592 else if (t == TypeManager.int64_type)
2593 ig.Emit (OpCodes.Ldind_I8);
2594 else if (t == TypeManager.float_type)
2595 ig.Emit (OpCodes.Ldind_R4);
2596 else if (t == TypeManager.double_type)
2597 ig.Emit (OpCodes.Ldind_R8);
2598 else if (t == TypeManager.bool_type)
2599 ig.Emit (OpCodes.Ldind_I1);
2600 else if (t == TypeManager.intptr_type)
2601 ig.Emit (OpCodes.Ldind_I);
2602 else if (TypeManager.IsEnumType (t)) {
2603 if (t == TypeManager.enum_type)
2604 ig.Emit (OpCodes.Ldind_Ref);
2606 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2607 } else if (t.IsValueType)
2608 ig.Emit (OpCodes.Ldobj, t);
2610 ig.Emit (OpCodes.Ldind_Ref);
2614 // The stack contains the pointer and the value of type `type'
2616 public static void StoreFromPtr (ILGenerator ig, Type type)
2619 type = TypeManager.EnumToUnderlying (type);
2620 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2621 ig.Emit (OpCodes.Stind_I4);
2622 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2623 ig.Emit (OpCodes.Stind_I8);
2624 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2625 type == TypeManager.ushort_type)
2626 ig.Emit (OpCodes.Stind_I2);
2627 else if (type == TypeManager.float_type)
2628 ig.Emit (OpCodes.Stind_R4);
2629 else if (type == TypeManager.double_type)
2630 ig.Emit (OpCodes.Stind_R8);
2631 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2632 type == TypeManager.bool_type)
2633 ig.Emit (OpCodes.Stind_I1);
2634 else if (type == TypeManager.intptr_type)
2635 ig.Emit (OpCodes.Stind_I);
2636 else if (type.IsValueType)
2637 ig.Emit (OpCodes.Stobj, type);
2639 ig.Emit (OpCodes.Stind_Ref);
2643 // Returns the size of type `t' if known, otherwise, 0
2645 public static int GetTypeSize (Type t)
2647 t = TypeManager.TypeToCoreType (t);
2648 if (t == TypeManager.int32_type ||
2649 t == TypeManager.uint32_type ||
2650 t == TypeManager.float_type)
2652 else if (t == TypeManager.int64_type ||
2653 t == TypeManager.uint64_type ||
2654 t == TypeManager.double_type)
2656 else if (t == TypeManager.byte_type ||
2657 t == TypeManager.sbyte_type ||
2658 t == TypeManager.bool_type)
2660 else if (t == TypeManager.short_type ||
2661 t == TypeManager.char_type ||
2662 t == TypeManager.ushort_type)
2669 // Default implementation of IAssignMethod.CacheTemporaries
2671 public void CacheTemporaries (EmitContext ec)
2675 static void Error_NegativeArrayIndex (Location loc)
2677 Report.Error (284, loc, "Can not create array with a negative size");
2681 // Converts `source' to an int, uint, long or ulong.
2683 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2687 bool old_checked = ec.CheckState;
2688 ec.CheckState = true;
2690 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2691 if (target == null){
2692 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2693 if (target == null){
2694 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2695 if (target == null){
2696 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2698 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2702 ec.CheckState = old_checked;
2705 // Only positive constants are allowed at compile time
2707 if (target is Constant){
2708 if (target is IntConstant){
2709 if (((IntConstant) target).Value < 0){
2710 Error_NegativeArrayIndex (loc);
2715 if (target is LongConstant){
2716 if (((LongConstant) target).Value < 0){
2717 Error_NegativeArrayIndex (loc);
2730 /// This is just a base class for expressions that can
2731 /// appear on statements (invocations, object creation,
2732 /// assignments, post/pre increment and decrement). The idea
2733 /// being that they would support an extra Emition interface that
2734 /// does not leave a result on the stack.
2736 public abstract class ExpressionStatement : Expression {
2739 /// Requests the expression to be emitted in a `statement'
2740 /// context. This means that no new value is left on the
2741 /// stack after invoking this method (constrasted with
2742 /// Emit that will always leave a value on the stack).
2744 public abstract void EmitStatement (EmitContext ec);
2748 /// This kind of cast is used to encapsulate the child
2749 /// whose type is child.Type into an expression that is
2750 /// reported to return "return_type". This is used to encapsulate
2751 /// expressions which have compatible types, but need to be dealt
2752 /// at higher levels with.
2754 /// For example, a "byte" expression could be encapsulated in one
2755 /// of these as an "unsigned int". The type for the expression
2756 /// would be "unsigned int".
2759 public class EmptyCast : Expression {
2760 protected Expression child;
2762 public EmptyCast (Expression child, Type return_type)
2764 eclass = child.eclass;
2769 public override Expression DoResolve (EmitContext ec)
2771 // This should never be invoked, we are born in fully
2772 // initialized state.
2777 public override void Emit (EmitContext ec)
2784 /// This class is used to wrap literals which belong inside Enums
2786 public class EnumConstant : Constant {
2787 public Constant Child;
2789 public EnumConstant (Constant child, Type enum_type)
2791 eclass = child.eclass;
2796 public override Expression DoResolve (EmitContext ec)
2798 // This should never be invoked, we are born in fully
2799 // initialized state.
2804 public override void Emit (EmitContext ec)
2809 public override object GetValue ()
2811 return Child.GetValue ();
2815 // Converts from one of the valid underlying types for an enumeration
2816 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2817 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2819 public Constant WidenToCompilerConstant ()
2821 Type t = TypeManager.EnumToUnderlying (Child.Type);
2822 object v = ((Constant) Child).GetValue ();;
2824 if (t == TypeManager.int32_type)
2825 return new IntConstant ((int) v);
2826 if (t == TypeManager.uint32_type)
2827 return new UIntConstant ((uint) v);
2828 if (t == TypeManager.int64_type)
2829 return new LongConstant ((long) v);
2830 if (t == TypeManager.uint64_type)
2831 return new ULongConstant ((ulong) v);
2832 if (t == TypeManager.short_type)
2833 return new ShortConstant ((short) v);
2834 if (t == TypeManager.ushort_type)
2835 return new UShortConstant ((ushort) v);
2836 if (t == TypeManager.byte_type)
2837 return new ByteConstant ((byte) v);
2838 if (t == TypeManager.sbyte_type)
2839 return new SByteConstant ((sbyte) v);
2841 throw new Exception ("Invalid enumeration underlying type: " + t);
2845 // Extracts the value in the enumeration on its native representation
2847 public object GetPlainValue ()
2849 Type t = TypeManager.EnumToUnderlying (Child.Type);
2850 object v = ((Constant) Child).GetValue ();;
2852 if (t == TypeManager.int32_type)
2854 if (t == TypeManager.uint32_type)
2856 if (t == TypeManager.int64_type)
2858 if (t == TypeManager.uint64_type)
2860 if (t == TypeManager.short_type)
2862 if (t == TypeManager.ushort_type)
2864 if (t == TypeManager.byte_type)
2866 if (t == TypeManager.sbyte_type)
2872 public override string AsString ()
2874 return Child.AsString ();
2877 public override DoubleConstant ConvertToDouble ()
2879 return Child.ConvertToDouble ();
2882 public override FloatConstant ConvertToFloat ()
2884 return Child.ConvertToFloat ();
2887 public override ULongConstant ConvertToULong ()
2889 return Child.ConvertToULong ();
2892 public override LongConstant ConvertToLong ()
2894 return Child.ConvertToLong ();
2897 public override UIntConstant ConvertToUInt ()
2899 return Child.ConvertToUInt ();
2902 public override IntConstant ConvertToInt ()
2904 return Child.ConvertToInt ();
2909 /// This kind of cast is used to encapsulate Value Types in objects.
2911 /// The effect of it is to box the value type emitted by the previous
2914 public class BoxedCast : EmptyCast {
2916 public BoxedCast (Expression expr)
2917 : base (expr, TypeManager.object_type)
2921 public override Expression DoResolve (EmitContext ec)
2923 // This should never be invoked, we are born in fully
2924 // initialized state.
2929 public override void Emit (EmitContext ec)
2933 ec.ig.Emit (OpCodes.Box, child.Type);
2937 public class UnboxCast : EmptyCast {
2938 public UnboxCast (Expression expr, Type return_type)
2939 : base (expr, return_type)
2943 public override Expression DoResolve (EmitContext ec)
2945 // This should never be invoked, we are born in fully
2946 // initialized state.
2951 public override void Emit (EmitContext ec)
2954 ILGenerator ig = ec.ig;
2957 ig.Emit (OpCodes.Unbox, t);
2959 LoadFromPtr (ig, t);
2964 /// This is used to perform explicit numeric conversions.
2966 /// Explicit numeric conversions might trigger exceptions in a checked
2967 /// context, so they should generate the conv.ovf opcodes instead of
2970 public class ConvCast : EmptyCast {
2971 public enum Mode : byte {
2972 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2974 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2975 U2_I1, U2_U1, U2_I2, U2_CH,
2976 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2977 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2978 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2979 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2980 CH_I1, CH_U1, CH_I2,
2981 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2982 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2988 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2989 : base (child, return_type)
2991 checked_state = ec.CheckState;
2995 public override Expression DoResolve (EmitContext ec)
2997 // This should never be invoked, we are born in fully
2998 // initialized state.
3003 public override void Emit (EmitContext ec)
3005 ILGenerator ig = ec.ig;
3011 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3012 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3013 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3014 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3015 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3017 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3018 case Mode.U1_CH: /* nothing */ break;
3020 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3021 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3022 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3023 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3024 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3025 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3027 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3028 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3029 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3030 case Mode.U2_CH: /* nothing */ break;
3032 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3033 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3034 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3035 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3036 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3037 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3038 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3040 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3041 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3042 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3043 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3044 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3045 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3047 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3048 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3049 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3050 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3051 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3052 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3053 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3054 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3056 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3057 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3058 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3059 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3060 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3061 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3062 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3063 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3065 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3066 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3067 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3069 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3070 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3071 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3072 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3073 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3074 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3075 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3076 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3077 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3079 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3080 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3081 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3082 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3083 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3084 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3085 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3086 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3087 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3088 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3092 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3093 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3094 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3095 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3096 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3098 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3099 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3101 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3102 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3103 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3104 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3105 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3106 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3108 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3109 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3110 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3111 case Mode.U2_CH: /* nothing */ break;
3113 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3114 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3115 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3116 case Mode.I4_U4: /* nothing */ break;
3117 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3118 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3119 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3121 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3122 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3123 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3124 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3125 case Mode.U4_I4: /* nothing */ break;
3126 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3128 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3129 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3130 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3131 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3132 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3133 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3134 case Mode.I8_U8: /* nothing */ break;
3135 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3137 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3138 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3139 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3140 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3141 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3142 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3143 case Mode.U8_I8: /* nothing */ break;
3144 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3146 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3147 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3148 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3150 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3151 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3152 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3153 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3154 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3155 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3156 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3157 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3158 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3160 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3161 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3162 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3163 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3164 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3165 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3166 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3167 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3168 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3169 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3175 public class OpcodeCast : EmptyCast {
3179 public OpcodeCast (Expression child, Type return_type, OpCode op)
3180 : base (child, return_type)
3184 second_valid = false;
3187 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3188 : base (child, return_type)
3193 second_valid = true;
3196 public override Expression DoResolve (EmitContext ec)
3198 // This should never be invoked, we are born in fully
3199 // initialized state.
3204 public override void Emit (EmitContext ec)
3215 /// This kind of cast is used to encapsulate a child and cast it
3216 /// to the class requested
3218 public class ClassCast : EmptyCast {
3219 public ClassCast (Expression child, Type return_type)
3220 : base (child, return_type)
3225 public override Expression DoResolve (EmitContext ec)
3227 // This should never be invoked, we are born in fully
3228 // initialized state.
3233 public override void Emit (EmitContext ec)
3237 ec.ig.Emit (OpCodes.Castclass, type);
3243 /// SimpleName expressions are initially formed of a single
3244 /// word and it only happens at the beginning of the expression.
3248 /// The expression will try to be bound to a Field, a Method
3249 /// group or a Property. If those fail we pass the name to our
3250 /// caller and the SimpleName is compounded to perform a type
3251 /// lookup. The idea behind this process is that we want to avoid
3252 /// creating a namespace map from the assemblies, as that requires
3253 /// the GetExportedTypes function to be called and a hashtable to
3254 /// be constructed which reduces startup time. If later we find
3255 /// that this is slower, we should create a `NamespaceExpr' expression
3256 /// that fully participates in the resolution process.
3258 /// For example `System.Console.WriteLine' is decomposed into
3259 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3261 /// The first SimpleName wont produce a match on its own, so it will
3263 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3265 /// System.Console will produce a TypeExpr match.
3267 /// The downside of this is that we might be hitting `LookupType' too many
3268 /// times with this scheme.
3270 public class SimpleName : Expression {
3271 public readonly string Name;
3273 public SimpleName (string name, Location l)
3279 public static void Error_ObjectRefRequired (Location l, string name)
3283 "An object reference is required " +
3284 "for the non-static field `"+name+"'");
3288 // Checks whether we are trying to access an instance
3289 // property, method or field from a static body.
3291 Expression MemberStaticCheck (Expression e)
3293 if (e is IMemberExpr){
3294 IMemberExpr member = (IMemberExpr) e;
3296 if (!member.IsStatic){
3297 Error_ObjectRefRequired (loc, Name);
3300 } else if (e is MethodGroupExpr){
3301 MethodGroupExpr mg = (MethodGroupExpr) e;
3303 if (!mg.RemoveInstanceMethods ()){
3304 Error_ObjectRefRequired (loc, mg.Methods [0].Name);
3313 public override Expression DoResolve (EmitContext ec)
3315 return SimpleNameResolve (ec, null, false);
3318 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3320 return SimpleNameResolve (ec, right_side, false);
3324 public Expression DoResolveAllowStatic (EmitContext ec)
3326 return SimpleNameResolve (ec, null, true);
3330 /// 7.5.2: Simple Names.
3332 /// Local Variables and Parameters are handled at
3333 /// parse time, so they never occur as SimpleNames.
3335 /// The `allow_static' flag is used by MemberAccess only
3336 /// and it is used to inform us that it is ok for us to
3337 /// avoid the static check, because MemberAccess might end
3338 /// up resolving the Name as a Type name and the access as
3339 /// a static type access.
3341 /// ie: Type Type; .... { Type.GetType (""); }
3343 /// Type is both an instance variable and a Type; Type.GetType
3344 /// is the static method not an instance method of type.
3346 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3348 Expression e = null;
3351 // Stage 1: Performed by the parser (binding to locals or parameters).
3353 if (!ec.OnlyLookupTypes){
3354 Block current_block = ec.CurrentBlock;
3355 if (current_block != null && current_block.IsVariableDefined (Name)){
3356 LocalVariableReference var;
3358 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3360 if (right_side != null)
3361 return var.ResolveLValue (ec, right_side);
3363 return var.Resolve (ec);
3366 if (current_block != null){
3368 Parameter par = null;
3369 Parameters pars = current_block.Parameters;
3371 par = pars.GetParameterByName (Name, out idx);
3374 ParameterReference param;
3376 param = new ParameterReference (pars, idx, Name, loc);
3378 if (right_side != null)
3379 return param.ResolveLValue (ec, right_side);
3381 return param.Resolve (ec);
3386 // Stage 2: Lookup members
3390 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3391 // Hence we have two different cases
3394 DeclSpace lookup_ds = ec.DeclSpace;
3396 if (lookup_ds.TypeBuilder == null)
3399 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3404 // Classes/structs keep looking, enums break
3406 if (lookup_ds is TypeContainer)
3407 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3410 } while (lookup_ds != null);
3412 if (e == null && ec.ContainerType != null)
3413 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3416 // Continuation of stage 2
3419 // Stage 3: Lookup symbol in the various namespaces.
3421 DeclSpace ds = ec.DeclSpace;
3425 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3426 return new TypeExpr (t, loc);
3429 // Stage 2 part b: Lookup up if we are an alias to a type
3432 // Since we are cheating: we only do the Alias lookup for
3433 // namespaces if the name does not include any dots in it
3436 alias_value = ec.DeclSpace.LookupAlias (Name);
3438 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3439 if ((t = RootContext.LookupType (ds, alias_value, true, loc))
3441 return new TypeExpr (t, loc);
3443 // we have alias value, but it isn't Type, so try if it's namespace
3444 return new SimpleName (alias_value, loc);
3447 if (ec.ResolvingTypeTree){
3448 Type dt = ec.DeclSpace.FindType (Name);
3450 return new TypeExpr (dt, loc);
3453 // No match, maybe our parent can compose us
3454 // into something meaningful.
3459 // Stage 2 continues here.
3464 if (ec.OnlyLookupTypes)
3467 if (e is IMemberExpr)
3468 return MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3474 return MemberStaticCheck (e);
3479 public override void Emit (EmitContext ec)
3482 // If this is ever reached, then we failed to
3483 // find the name as a namespace
3486 Error (103, "The name `" + Name +
3487 "' does not exist in the class `" +
3488 ec.DeclSpace.Name + "'");
3491 public override string ToString ()
3498 /// Fully resolved expression that evaluates to a type
3500 public class TypeExpr : Expression {
3501 public TypeExpr (Type t, Location l)
3504 eclass = ExprClass.Type;
3508 override public Expression DoResolve (EmitContext ec)
3513 override public void Emit (EmitContext ec)
3515 throw new Exception ("Should never be called");
3520 /// Used to create types from a fully qualified name. These are just used
3521 /// by the parser to setup the core types. A TypeExpression is always
3522 /// classified as a type.
3524 public class TypeExpression : TypeExpr {
3527 public TypeExpression (string name) : base (null, Location.Null)
3532 public override Expression DoResolve (EmitContext ec)
3535 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3539 public override void Emit (EmitContext ec)
3541 throw new Exception ("Should never be called");
3544 public override string ToString ()
3551 /// MethodGroup Expression.
3553 /// This is a fully resolved expression that evaluates to a type
3555 public class MethodGroupExpr : Expression {
3556 public MethodBase [] Methods;
3557 Expression instance_expression = null;
3559 public MethodGroupExpr (MemberInfo [] mi, Location l)
3561 Methods = new MethodBase [mi.Length];
3562 mi.CopyTo (Methods, 0);
3563 eclass = ExprClass.MethodGroup;
3564 type = TypeManager.object_type;
3568 public MethodGroupExpr (ArrayList list, Location l)
3570 Methods = new MethodBase [list.Count];
3573 list.CopyTo (Methods, 0);
3575 foreach (MemberInfo m in list){
3576 if (!(m is MethodBase)){
3577 Console.WriteLine ("Name " + m.Name);
3578 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3584 eclass = ExprClass.MethodGroup;
3585 type = TypeManager.object_type;
3589 // `A method group may have associated an instance expression'
3591 public Expression InstanceExpression {
3593 return instance_expression;
3597 instance_expression = value;
3601 override public Expression DoResolve (EmitContext ec)
3606 public void ReportUsageError ()
3608 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3609 Methods [0].Name + "()' is referenced without parentheses");
3612 override public void Emit (EmitContext ec)
3614 ReportUsageError ();
3617 bool RemoveMethods (bool keep_static)
3619 ArrayList smethods = new ArrayList ();
3621 foreach (MethodBase mb in Methods){
3622 if (mb.IsStatic == keep_static)
3626 if (smethods.Count == 0)
3629 Methods = new MethodBase [smethods.Count];
3630 smethods.CopyTo (Methods, 0);
3636 /// Removes any instance methods from the MethodGroup, returns
3637 /// false if the resulting set is empty.
3639 public bool RemoveInstanceMethods ()
3641 return RemoveMethods (true);
3645 /// Removes any static methods from the MethodGroup, returns
3646 /// false if the resulting set is empty.
3648 public bool RemoveStaticMethods ()
3650 return RemoveMethods (false);
3655 /// Fully resolved expression that evaluates to a Field
3657 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3658 public readonly FieldInfo FieldInfo;
3659 Expression instance_expr;
3661 public FieldExpr (FieldInfo fi, Location l)
3664 eclass = ExprClass.Variable;
3665 type = fi.FieldType;
3669 public string Name {
3671 return FieldInfo.Name;
3675 public bool IsStatic {
3677 return FieldInfo.IsStatic;
3681 public Expression InstanceExpression {
3683 return instance_expr;
3687 instance_expr = value;
3691 override public Expression DoResolve (EmitContext ec)
3693 if (!FieldInfo.IsStatic){
3694 if (instance_expr == null){
3695 throw new Exception ("non-static FieldExpr without instance var\n" +
3696 "You have to assign the Instance variable\n" +
3697 "Of the FieldExpr to set this\n");
3700 instance_expr = instance_expr.Resolve (ec);
3701 if (instance_expr == null)
3708 void Report_AssignToReadonly (bool is_instance)
3713 msg = "Readonly field can not be assigned outside " +
3714 "of constructor or variable initializer";
3716 msg = "A static readonly field can only be assigned in " +
3717 "a static constructor";
3719 Report.Error (is_instance ? 191 : 198, loc, msg);
3722 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3724 Expression e = DoResolve (ec);
3729 if (!FieldInfo.IsInitOnly)
3733 // InitOnly fields can only be assigned in constructors
3736 if (ec.IsConstructor)
3739 Report_AssignToReadonly (true);
3744 override public void Emit (EmitContext ec)
3746 ILGenerator ig = ec.ig;
3747 bool is_volatile = false;
3749 if (FieldInfo is FieldBuilder){
3750 FieldBase f = TypeManager.GetField (FieldInfo);
3752 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3755 f.status |= Field.Status.USED;
3758 if (FieldInfo.IsStatic){
3760 ig.Emit (OpCodes.Volatile);
3762 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3764 if (instance_expr.Type.IsValueType){
3766 LocalTemporary tempo = null;
3768 if (!(instance_expr is IMemoryLocation)){
3769 tempo = new LocalTemporary (
3770 ec, instance_expr.Type);
3772 InstanceExpression.Emit (ec);
3776 ml = (IMemoryLocation) instance_expr;
3778 ml.AddressOf (ec, AddressOp.Load);
3780 instance_expr.Emit (ec);
3783 ig.Emit (OpCodes.Volatile);
3785 ig.Emit (OpCodes.Ldfld, FieldInfo);
3789 public void EmitAssign (EmitContext ec, Expression source)
3791 FieldAttributes fa = FieldInfo.Attributes;
3792 bool is_static = (fa & FieldAttributes.Static) != 0;
3793 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3794 ILGenerator ig = ec.ig;
3796 if (is_readonly && !ec.IsConstructor){
3797 Report_AssignToReadonly (!is_static);
3802 Expression instance = instance_expr;
3804 if (instance.Type.IsValueType){
3805 if (instance is IMemoryLocation){
3806 IMemoryLocation ml = (IMemoryLocation) instance;
3808 ml.AddressOf (ec, AddressOp.Store);
3810 throw new Exception ("The " + instance + " of type " +
3812 " represents a ValueType and does " +
3813 "not implement IMemoryLocation");
3819 if (FieldInfo is FieldBuilder){
3820 FieldBase f = TypeManager.GetField (FieldInfo);
3822 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3823 ig.Emit (OpCodes.Volatile);
3827 ig.Emit (OpCodes.Stsfld, FieldInfo);
3829 ig.Emit (OpCodes.Stfld, FieldInfo);
3831 if (FieldInfo is FieldBuilder){
3832 FieldBase f = TypeManager.GetField (FieldInfo);
3834 f.status |= Field.Status.ASSIGNED;
3838 public void AddressOf (EmitContext ec, AddressOp mode)
3840 ILGenerator ig = ec.ig;
3842 if (FieldInfo is FieldBuilder){
3843 FieldBase f = TypeManager.GetField (FieldInfo);
3844 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3845 ig.Emit (OpCodes.Volatile);
3848 if (FieldInfo is FieldBuilder){
3849 FieldBase f = TypeManager.GetField (FieldInfo);
3851 if ((mode & AddressOp.Store) != 0)
3852 f.status |= Field.Status.ASSIGNED;
3853 if ((mode & AddressOp.Load) != 0)
3854 f.status |= Field.Status.USED;
3858 // Handle initonly fields specially: make a copy and then
3859 // get the address of the copy.
3861 if (FieldInfo.IsInitOnly){
3862 if (ec.IsConstructor) {
3863 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3868 local = ig.DeclareLocal (type);
3869 ig.Emit (OpCodes.Stloc, local);
3870 ig.Emit (OpCodes.Ldloca, local);
3875 if (FieldInfo.IsStatic)
3876 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3878 if (instance_expr is IMemoryLocation)
3879 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
3881 instance_expr.Emit (ec);
3882 ig.Emit (OpCodes.Ldflda, FieldInfo);
3888 /// Expression that evaluates to a Property. The Assign class
3889 /// might set the `Value' expression if we are in an assignment.
3891 /// This is not an LValue because we need to re-write the expression, we
3892 /// can not take data from the stack and store it.
3894 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
3895 public readonly PropertyInfo PropertyInfo;
3897 MethodInfo [] Accessors;
3900 Expression instance_expr;
3902 public PropertyExpr (PropertyInfo pi, Location l)
3905 eclass = ExprClass.PropertyAccess;
3908 Accessors = TypeManager.GetAccessors (pi);
3910 if (Accessors != null)
3911 foreach (MethodInfo mi in Accessors){
3917 Accessors = new MethodInfo [2];
3919 type = TypeManager.TypeToCoreType (pi.PropertyType);
3922 public string Name {
3924 return PropertyInfo.Name;
3928 public bool IsStatic {
3935 // The instance expression associated with this expression
3937 public Expression InstanceExpression {
3939 instance_expr = value;
3943 return instance_expr;
3947 public bool VerifyAssignable ()
3949 if (!PropertyInfo.CanWrite){
3950 Report.Error (200, loc,
3951 "The property `" + PropertyInfo.Name +
3952 "' can not be assigned to, as it has not set accessor");
3959 override public Expression DoResolve (EmitContext ec)
3961 if (!PropertyInfo.CanRead){
3962 Report.Error (154, loc,
3963 "The property `" + PropertyInfo.Name +
3964 "' can not be used in " +
3965 "this context because it lacks a get accessor");
3969 type = PropertyInfo.PropertyType;
3974 override public void Emit (EmitContext ec)
3976 MethodInfo method = Accessors [0];
3979 // Special case: length of single dimension array is turned into ldlen
3981 if (method == TypeManager.int_array_get_length){
3982 Type iet = instance_expr.Type;
3985 // System.Array.Length can be called, but the Type does not
3986 // support invoking GetArrayRank, so test for that case first
3988 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3989 instance_expr.Emit (ec);
3990 ec.ig.Emit (OpCodes.Ldlen);
3995 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
4000 // Implements the IAssignMethod interface for assignments
4002 public void EmitAssign (EmitContext ec, Expression source)
4004 Argument arg = new Argument (source, Argument.AType.Expression);
4005 ArrayList args = new ArrayList ();
4008 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
4011 override public void EmitStatement (EmitContext ec)
4014 ec.ig.Emit (OpCodes.Pop);
4019 /// Fully resolved expression that evaluates to an Event
4021 public class EventExpr : Expression, IMemberExpr {
4022 public readonly EventInfo EventInfo;
4023 public Expression instance_expr;
4026 MethodInfo add_accessor, remove_accessor;
4028 public EventExpr (EventInfo ei, Location loc)
4032 eclass = ExprClass.EventAccess;
4034 add_accessor = TypeManager.GetAddMethod (ei);
4035 remove_accessor = TypeManager.GetRemoveMethod (ei);
4037 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4040 if (EventInfo is MyEventBuilder)
4041 type = ((MyEventBuilder) EventInfo).EventType;
4043 type = EventInfo.EventHandlerType;
4046 public string Name {
4048 return EventInfo.Name;
4052 public bool IsStatic {
4058 public Expression InstanceExpression {
4060 return instance_expr;
4064 instance_expr = value;
4068 public override Expression DoResolve (EmitContext ec)
4070 // We are born fully resolved
4074 public override void Emit (EmitContext ec)
4076 throw new Exception ("Should not happen I think");
4079 public void EmitAddOrRemove (EmitContext ec, Expression source)
4081 Expression handler = ((Binary) source).Right;
4083 Argument arg = new Argument (handler, Argument.AType.Expression);
4084 ArrayList args = new ArrayList ();
4088 if (((Binary) source).Oper == Binary.Operator.Addition)
4089 Invocation.EmitCall (
4090 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4092 Invocation.EmitCall (
4093 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);