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 just as a hint to AddressOf of what will be done with the
43 public enum AddressOp {
50 /// This interface is implemented by variables
52 public interface IMemoryLocation {
54 /// The AddressOf method should generate code that loads
55 /// the address of the object and leaves it on the stack.
57 /// The `mode' argument is used to notify the expression
58 /// of whether this will be used to read from the address or
59 /// write to the address.
61 /// This is just a hint that can be used to provide good error
62 /// reporting, and should have no other side effects.
64 void AddressOf (EmitContext ec, AddressOp mode);
68 /// Base class for expressions
70 public abstract class Expression {
71 public ExprClass eclass;
85 /// Utility wrapper routine for Error, just to beautify the code
87 static protected void Error (int error, string s)
89 Report.Error (error, s);
92 static protected void Error (int error, Location loc, string s)
94 Report.Error (error, loc, s);
98 /// Utility wrapper routine for Warning, just to beautify the code
100 static protected void Warning (int warning, string s)
102 Report.Warning (warning, s);
105 static public void Error_CannotConvertType (Location loc, Type source, Type target)
107 Report.Error (30, loc, "Cannot convert type '" +
108 TypeManager.CSharpName (source) + "' to '" +
109 TypeManager.CSharpName (target) + "'");
113 /// Performs semantic analysis on the Expression
117 /// The Resolve method is invoked to perform the semantic analysis
120 /// The return value is an expression (it can be the
121 /// same expression in some cases) or a new
122 /// expression that better represents this node.
124 /// For example, optimizations of Unary (LiteralInt)
125 /// would return a new LiteralInt with a negated
128 /// If there is an error during semantic analysis,
129 /// then an error should be reported (using Report)
130 /// and a null value should be returned.
132 /// There are two side effects expected from calling
133 /// Resolve(): the the field variable "eclass" should
134 /// be set to any value of the enumeration
135 /// `ExprClass' and the type variable should be set
136 /// to a valid type (this is the type of the
139 public abstract Expression DoResolve (EmitContext ec);
141 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
143 return DoResolve (ec);
147 /// Resolves an expression and performs semantic analysis on it.
151 /// Currently Resolve wraps DoResolve to perform sanity
152 /// checking and assertion checking on what we expect from Resolve.
154 public Expression Resolve (EmitContext ec)
156 Expression e = DoResolve (ec);
160 if (e is SimpleName){
161 SimpleName s = (SimpleName) e;
165 "The name `" + s.Name + "' could not be found in `" +
166 ec.DeclSpace.Name + "'");
170 if (e.eclass == ExprClass.Invalid)
171 throw new Exception ("Expression " + e.GetType () +
172 " ExprClass is Invalid after resolve");
174 if (e.eclass != ExprClass.MethodGroup)
176 throw new Exception (
177 "Expression " + e.GetType () +
178 " did not set its type after Resolve\n" +
179 "called from: " + this.GetType ());
186 /// Performs expression resolution and semantic analysis, but
187 /// allows SimpleNames to be returned.
191 /// This is used by MemberAccess to construct long names that can not be
192 /// partially resolved (namespace-qualified names for example).
194 public Expression ResolveWithSimpleName (EmitContext ec)
198 if (this is SimpleName)
199 e = ((SimpleName) this).DoResolveAllowStatic (ec);
207 if (e.eclass == ExprClass.Invalid)
208 throw new Exception ("Expression " + e +
209 " ExprClass is Invalid after resolve");
211 if (e.eclass != ExprClass.MethodGroup)
213 throw new Exception ("Expression " + e +
214 " did not set its type after Resolve");
221 /// Resolves an expression for LValue assignment
225 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
226 /// checking and assertion checking on what we expect from Resolve
228 public Expression ResolveLValue (EmitContext ec, Expression right_side)
230 Expression e = DoResolveLValue (ec, right_side);
233 if (e is SimpleName){
234 SimpleName s = (SimpleName) e;
238 "The name `" + s.Name + "' could not be found in `" +
239 ec.DeclSpace.Name + "'");
243 if (e.eclass == ExprClass.Invalid)
244 throw new Exception ("Expression " + e +
245 " ExprClass is Invalid after resolve");
247 if (e.eclass != ExprClass.MethodGroup)
249 throw new Exception ("Expression " + e +
250 " did not set its type after Resolve");
257 /// Emits the code for the expression
261 /// The Emit method is invoked to generate the code
262 /// for the expression.
264 public abstract void Emit (EmitContext ec);
267 /// Protected constructor. Only derivate types should
268 /// be able to be created
271 protected Expression ()
273 eclass = ExprClass.Invalid;
278 /// Returns a literalized version of a literal FieldInfo
282 /// The possible return values are:
283 /// IntConstant, UIntConstant
284 /// LongLiteral, ULongConstant
285 /// FloatConstant, DoubleConstant
288 /// The value returned is already resolved.
290 public static Constant Constantify (object v, Type t)
292 if (t == TypeManager.int32_type)
293 return new IntConstant ((int) v);
294 else if (t == TypeManager.uint32_type)
295 return new UIntConstant ((uint) v);
296 else if (t == TypeManager.int64_type)
297 return new LongConstant ((long) v);
298 else if (t == TypeManager.uint64_type)
299 return new ULongConstant ((ulong) v);
300 else if (t == TypeManager.float_type)
301 return new FloatConstant ((float) v);
302 else if (t == TypeManager.double_type)
303 return new DoubleConstant ((double) v);
304 else if (t == TypeManager.string_type)
305 return new StringConstant ((string) v);
306 else if (t == TypeManager.short_type)
307 return new ShortConstant ((short)v);
308 else if (t == TypeManager.ushort_type)
309 return new UShortConstant ((ushort)v);
310 else if (t == TypeManager.sbyte_type)
311 return new SByteConstant (((sbyte)v));
312 else if (t == TypeManager.byte_type)
313 return new ByteConstant ((byte)v);
314 else if (t == TypeManager.char_type)
315 return new CharConstant ((char)v);
316 else if (t == TypeManager.bool_type)
317 return new BoolConstant ((bool) v);
318 else if (TypeManager.IsEnumType (t)){
319 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
321 return new EnumConstant (e, t);
323 throw new Exception ("Unknown type for constant (" + t +
328 /// Returns a fully formed expression after a MemberLookup
330 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
333 return new EventExpr ((EventInfo) mi, loc);
334 else if (mi is FieldInfo)
335 return new FieldExpr ((FieldInfo) mi, loc);
336 else if (mi is PropertyInfo)
337 return new PropertyExpr ((PropertyInfo) mi, loc);
338 else if (mi is Type){
339 return new TypeExpr ((System.Type) mi);
346 // FIXME: Probably implement a cache for (t,name,current_access_set)?
348 // This code could use some optimizations, but we need to do some
349 // measurements. For example, we could use a delegate to `flag' when
350 // something can not any longer be a method-group (because it is something
354 // If the return value is an Array, then it is an array of
357 // If the return value is an MemberInfo, it is anything, but a Method
361 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
362 // the arguments here and have MemberLookup return only the methods that
363 // match the argument count/type, unlike we are doing now (we delay this
366 // This is so we can catch correctly attempts to invoke instance methods
367 // from a static body (scan for error 120 in ResolveSimpleName).
370 // FIXME: Potential optimization, have a static ArrayList
373 public static Expression MemberLookup (EmitContext ec, Type t, string name,
374 MemberTypes mt, BindingFlags bf, Location loc)
376 MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
381 int count = mi.Length;
384 return new MethodGroupExpr (mi, loc);
386 if (mi [0] is MethodBase)
387 return new MethodGroupExpr (mi, loc);
389 return ExprClassFromMemberInfo (ec, mi [0], loc);
392 public const MemberTypes AllMemberTypes =
393 MemberTypes.Constructor |
397 MemberTypes.NestedType |
398 MemberTypes.Property;
400 public const BindingFlags AllBindingFlags =
401 BindingFlags.Public |
402 BindingFlags.Static |
403 BindingFlags.Instance;
405 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
407 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
410 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
412 return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
416 /// This is a wrapper for MemberLookup that is not used to "probe", but
417 /// to find a final definition. If the final definition is not found, we
418 /// look for private members and display a useful debugging message if we
421 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
424 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
427 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
428 MemberTypes mt, BindingFlags bf, Location loc)
432 e = MemberLookup (ec, t, name, mt, bf, loc);
437 e = MemberLookup (ec, t, name, AllMemberTypes,
438 AllBindingFlags | BindingFlags.NonPublic, loc);
441 117, loc, "`" + t + "' does not contain a definition " +
442 "for `" + name + "'");
445 122, loc, "`" + t + "." + name +
446 "' is inaccessible due to its protection level");
452 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
454 EventInfo ei = event_expr.EventInfo;
456 return TypeManager.GetPrivateFieldOfEvent (ei);
459 static EmptyExpression MyEmptyExpr;
460 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
462 Type expr_type = expr.Type;
464 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
465 // if we are a method group, emit a warning
470 if (target_type == TypeManager.object_type) {
472 // A pointer type cannot be converted to object
474 if (expr_type.IsPointer)
477 if (expr_type.IsValueType)
478 return new BoxedCast (expr);
479 if (expr_type.IsClass || expr_type.IsInterface)
480 return new EmptyCast (expr, target_type);
481 } else if (expr_type.IsSubclassOf (target_type)) {
482 return new EmptyCast (expr, target_type);
485 // This code is kind of mirrored inside StandardConversionExists
486 // with the small distinction that we only probe there
488 // Always ensure that the code here and there is in sync
490 // from the null type to any reference-type.
491 if (expr is NullLiteral && !target_type.IsValueType)
492 return new EmptyCast (expr, target_type);
494 // from any class-type S to any interface-type T.
495 if (expr_type.IsClass && target_type.IsInterface) {
496 if (TypeManager.ImplementsInterface (expr_type, target_type))
497 return new EmptyCast (expr, target_type);
502 // from any interface type S to interface-type T.
503 if (expr_type.IsInterface && target_type.IsInterface) {
505 if (TypeManager.ImplementsInterface (expr_type, target_type))
506 return new EmptyCast (expr, target_type);
511 // from an array-type S to an array-type of type T
512 if (expr_type.IsArray && target_type.IsArray) {
513 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
515 Type expr_element_type = expr_type.GetElementType ();
517 if (MyEmptyExpr == null)
518 MyEmptyExpr = new EmptyExpression ();
520 MyEmptyExpr.SetType (expr_element_type);
521 Type target_element_type = target_type.GetElementType ();
523 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
524 if (StandardConversionExists (MyEmptyExpr,
525 target_element_type))
526 return new EmptyCast (expr, target_type);
531 // from an array-type to System.Array
532 if (expr_type.IsArray && target_type == TypeManager.array_type)
533 return new EmptyCast (expr, target_type);
535 // from any delegate type to System.Delegate
536 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
537 target_type == TypeManager.delegate_type)
538 return new EmptyCast (expr, target_type);
540 // from any array-type or delegate type into System.ICloneable.
541 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
542 if (target_type == TypeManager.icloneable_type)
543 return new EmptyCast (expr, target_type);
553 /// Handles expressions like this: decimal d; d = 1;
554 /// and changes them into: decimal d; d = new System.Decimal (1);
556 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
558 ArrayList args = new ArrayList ();
560 args.Add (new Argument (expr, Argument.AType.Expression));
562 Expression ne = new New (new TypeExpr (target), args, new Location (-1));
564 return ne.Resolve (ec);
568 /// Implicit Numeric Conversions.
570 /// expr is the expression to convert, returns a new expression of type
571 /// target_type or null if an implicit conversion is not possible.
573 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
574 Type target_type, Location loc)
576 Type expr_type = expr.Type;
579 // Attempt to do the implicit constant expression conversions
581 if (expr is IntConstant){
584 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
588 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
590 // Try the implicit constant expression conversion
591 // from long to ulong, instead of a nice routine,
594 long v = ((LongConstant) expr).Value;
596 return new ULongConstant ((ulong) v);
600 // If we have an enumeration, extract the underlying type,
601 // use this during the comparission, but wrap around the original
604 Type real_target_type = target_type;
606 if (TypeManager.IsEnumType (real_target_type))
607 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
609 if (expr_type == real_target_type)
610 return new EmptyCast (expr, target_type);
612 if (expr_type == TypeManager.sbyte_type){
614 // From sbyte to short, int, long, float, double.
616 if (real_target_type == TypeManager.int32_type)
617 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
618 if (real_target_type == TypeManager.int64_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
620 if (real_target_type == TypeManager.double_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
622 if (real_target_type == TypeManager.float_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
624 if (real_target_type == TypeManager.short_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
626 if (real_target_type == TypeManager.decimal_type)
627 return InternalTypeConstructor (ec, expr, target_type);
628 } else if (expr_type == TypeManager.byte_type){
630 // From byte to short, ushort, int, uint, long, ulong, float, double
632 if ((real_target_type == TypeManager.short_type) ||
633 (real_target_type == TypeManager.ushort_type) ||
634 (real_target_type == TypeManager.int32_type) ||
635 (real_target_type == TypeManager.uint32_type))
636 return new EmptyCast (expr, target_type);
638 if (real_target_type == TypeManager.uint64_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
640 if (real_target_type == TypeManager.int64_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
642 if (real_target_type == TypeManager.float_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
644 if (real_target_type == TypeManager.double_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
646 if (real_target_type == TypeManager.decimal_type)
647 return InternalTypeConstructor (ec, expr, target_type);
648 } else if (expr_type == TypeManager.short_type){
650 // From short to int, long, float, double
652 if (real_target_type == TypeManager.int32_type)
653 return new EmptyCast (expr, target_type);
654 if (real_target_type == TypeManager.int64_type)
655 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
656 if (real_target_type == TypeManager.double_type)
657 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
658 if (real_target_type == TypeManager.float_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
660 if (real_target_type == TypeManager.decimal_type)
661 return InternalTypeConstructor (ec, expr, target_type);
662 } else if (expr_type == TypeManager.ushort_type){
664 // From ushort to int, uint, long, ulong, float, double
666 if (real_target_type == TypeManager.uint32_type)
667 return new EmptyCast (expr, target_type);
669 if (real_target_type == TypeManager.uint64_type)
670 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
671 if (real_target_type == TypeManager.int32_type)
672 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
673 if (real_target_type == TypeManager.int64_type)
674 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
675 if (real_target_type == TypeManager.double_type)
676 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
677 if (real_target_type == TypeManager.float_type)
678 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
679 if (real_target_type == TypeManager.decimal_type)
680 return InternalTypeConstructor (ec, expr, target_type);
681 } else if (expr_type == TypeManager.int32_type){
683 // From int to long, float, double
685 if (real_target_type == TypeManager.int64_type)
686 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
687 if (real_target_type == TypeManager.double_type)
688 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
689 if (real_target_type == TypeManager.float_type)
690 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
691 if (real_target_type == TypeManager.decimal_type)
692 return InternalTypeConstructor (ec, expr, target_type);
693 } else if (expr_type == TypeManager.uint32_type){
695 // From uint to long, ulong, float, double
697 if (real_target_type == TypeManager.int64_type)
698 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
699 if (real_target_type == TypeManager.uint64_type)
700 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
701 if (real_target_type == TypeManager.double_type)
702 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
704 if (real_target_type == TypeManager.float_type)
705 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
707 if (real_target_type == TypeManager.decimal_type)
708 return InternalTypeConstructor (ec, expr, target_type);
709 } else if (expr_type == TypeManager.int64_type){
711 // From long/ulong to float, double
713 if (real_target_type == TypeManager.double_type)
714 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
715 if (real_target_type == TypeManager.float_type)
716 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
717 if (real_target_type == TypeManager.decimal_type)
718 return InternalTypeConstructor (ec, expr, target_type);
719 } else if (expr_type == TypeManager.uint64_type){
721 // From ulong to float, double
723 if (real_target_type == TypeManager.double_type)
724 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
726 if (real_target_type == TypeManager.float_type)
727 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
729 if (real_target_type == TypeManager.decimal_type)
730 return InternalTypeConstructor (ec, expr, target_type);
731 } else if (expr_type == TypeManager.char_type){
733 // From char to ushort, int, uint, long, ulong, float, double
735 if ((real_target_type == TypeManager.ushort_type) ||
736 (real_target_type == TypeManager.int32_type) ||
737 (real_target_type == TypeManager.uint32_type))
738 return new EmptyCast (expr, target_type);
739 if (real_target_type == TypeManager.uint64_type)
740 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
741 if (real_target_type == TypeManager.int64_type)
742 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
743 if (real_target_type == TypeManager.float_type)
744 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
745 if (real_target_type == TypeManager.double_type)
746 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
747 if (real_target_type == TypeManager.decimal_type)
748 return InternalTypeConstructor (ec, expr, target_type);
749 } else if (expr_type == TypeManager.float_type){
753 if (real_target_type == TypeManager.double_type)
754 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
761 // Tests whether an implicit reference conversion exists between expr_type
764 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
766 Type expr_type = expr.Type;
769 // This is the boxed case.
771 if (target_type == TypeManager.object_type) {
772 if ((expr_type.IsClass) ||
773 (expr_type.IsValueType) ||
774 (expr_type.IsInterface))
777 } else if (expr_type.IsSubclassOf (target_type)) {
781 // Please remember that all code below actually comes
782 // from ImplicitReferenceConversion so make sure code remains in sync
784 // from any class-type S to any interface-type T.
785 if (expr_type.IsClass && target_type.IsInterface) {
786 if (TypeManager.ImplementsInterface (expr_type, target_type))
790 // from any interface type S to interface-type T.
791 if (expr_type.IsInterface && target_type.IsInterface)
792 if (TypeManager.ImplementsInterface (expr_type, target_type))
795 // from an array-type S to an array-type of type T
796 if (expr_type.IsArray && target_type.IsArray) {
797 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
799 Type expr_element_type = expr_type.GetElementType ();
801 if (MyEmptyExpr == null)
802 MyEmptyExpr = new EmptyExpression ();
804 MyEmptyExpr.SetType (expr_element_type);
805 Type target_element_type = target_type.GetElementType ();
807 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
808 if (StandardConversionExists (MyEmptyExpr,
809 target_element_type))
814 // from an array-type to System.Array
815 if (expr_type.IsArray && (target_type == TypeManager.array_type))
818 // from any delegate type to System.Delegate
819 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
820 target_type == TypeManager.delegate_type)
821 if (target_type.IsAssignableFrom (expr_type))
824 // from any array-type or delegate type into System.ICloneable.
825 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
826 if (target_type == TypeManager.icloneable_type)
829 // from the null type to any reference-type.
830 if (expr is NullLiteral && !target_type.IsValueType &&
831 !TypeManager.IsEnumType (target_type))
840 /// Same as StandardConversionExists except that it also looks at
841 /// implicit user defined conversions - needed for overload resolution
843 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
845 if (StandardConversionExists (expr, target_type) == true)
848 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
857 /// Determines if a standard implicit conversion exists from
858 /// expr_type to target_type
860 public static bool StandardConversionExists (Expression expr, Type target_type)
862 Type expr_type = expr.Type;
864 if (expr_type == target_type)
867 // First numeric conversions
869 if (expr_type == TypeManager.sbyte_type){
871 // From sbyte to short, int, long, float, double.
873 if ((target_type == TypeManager.int32_type) ||
874 (target_type == TypeManager.int64_type) ||
875 (target_type == TypeManager.double_type) ||
876 (target_type == TypeManager.float_type) ||
877 (target_type == TypeManager.short_type) ||
878 (target_type == TypeManager.decimal_type))
881 } else if (expr_type == TypeManager.byte_type){
883 // From byte to short, ushort, int, uint, long, ulong, float, double
885 if ((target_type == TypeManager.short_type) ||
886 (target_type == TypeManager.ushort_type) ||
887 (target_type == TypeManager.int32_type) ||
888 (target_type == TypeManager.uint32_type) ||
889 (target_type == TypeManager.uint64_type) ||
890 (target_type == TypeManager.int64_type) ||
891 (target_type == TypeManager.float_type) ||
892 (target_type == TypeManager.double_type) ||
893 (target_type == TypeManager.decimal_type))
896 } else if (expr_type == TypeManager.short_type){
898 // From short to int, long, float, double
900 if ((target_type == TypeManager.int32_type) ||
901 (target_type == TypeManager.int64_type) ||
902 (target_type == TypeManager.double_type) ||
903 (target_type == TypeManager.float_type) ||
904 (target_type == TypeManager.decimal_type))
907 } else if (expr_type == TypeManager.ushort_type){
909 // From ushort to int, uint, long, ulong, float, double
911 if ((target_type == TypeManager.uint32_type) ||
912 (target_type == TypeManager.uint64_type) ||
913 (target_type == TypeManager.int32_type) ||
914 (target_type == TypeManager.int64_type) ||
915 (target_type == TypeManager.double_type) ||
916 (target_type == TypeManager.float_type) ||
917 (target_type == TypeManager.decimal_type))
920 } else if (expr_type == TypeManager.int32_type){
922 // From int to long, float, double
924 if ((target_type == TypeManager.int64_type) ||
925 (target_type == TypeManager.double_type) ||
926 (target_type == TypeManager.float_type) ||
927 (target_type == TypeManager.decimal_type))
930 } else if (expr_type == TypeManager.uint32_type){
932 // From uint to long, ulong, float, double
934 if ((target_type == TypeManager.int64_type) ||
935 (target_type == TypeManager.uint64_type) ||
936 (target_type == TypeManager.double_type) ||
937 (target_type == TypeManager.float_type) ||
938 (target_type == TypeManager.decimal_type))
941 } else if ((expr_type == TypeManager.uint64_type) ||
942 (expr_type == TypeManager.int64_type)) {
944 // From long/ulong to float, double
946 if ((target_type == TypeManager.double_type) ||
947 (target_type == TypeManager.float_type) ||
948 (target_type == TypeManager.decimal_type))
951 } else if (expr_type == TypeManager.char_type){
953 // From char to ushort, int, uint, long, ulong, float, double
955 if ((target_type == TypeManager.ushort_type) ||
956 (target_type == TypeManager.int32_type) ||
957 (target_type == TypeManager.uint32_type) ||
958 (target_type == TypeManager.uint64_type) ||
959 (target_type == TypeManager.int64_type) ||
960 (target_type == TypeManager.float_type) ||
961 (target_type == TypeManager.double_type) ||
962 (target_type == TypeManager.decimal_type))
965 } else if (expr_type == TypeManager.float_type){
969 if (target_type == TypeManager.double_type)
973 if (ImplicitReferenceConversionExists (expr, target_type))
976 if (expr is IntConstant){
977 int value = ((IntConstant) expr).Value;
979 if (target_type == TypeManager.sbyte_type){
980 if (value >= SByte.MinValue && value <= SByte.MaxValue)
982 } else if (target_type == TypeManager.byte_type){
983 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
985 } else if (target_type == TypeManager.short_type){
986 if (value >= Int16.MinValue && value <= Int16.MaxValue)
988 } else if (target_type == TypeManager.ushort_type){
989 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
991 } else if (target_type == TypeManager.uint32_type){
994 } else if (target_type == TypeManager.uint64_type){
996 // we can optimize this case: a positive int32
997 // always fits on a uint64. But we need an opcode
1004 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1008 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1010 // Try the implicit constant expression conversion
1011 // from long to ulong, instead of a nice routine,
1012 // we just inline it
1014 long v = ((LongConstant) expr).Value;
1019 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1020 IntLiteral i = (IntLiteral) expr;
1029 // Used internally by FindMostEncompassedType, this is used
1030 // to avoid creating lots of objects in the tight loop inside
1031 // FindMostEncompassedType
1033 static EmptyExpression priv_fmet_param;
1036 /// Finds "most encompassed type" according to the spec (13.4.2)
1037 /// amongst the methods in the MethodGroupExpr
1039 static Type FindMostEncompassedType (ArrayList types)
1043 if (priv_fmet_param == null)
1044 priv_fmet_param = new EmptyExpression ();
1046 foreach (Type t in types){
1047 priv_fmet_param.SetType (t);
1054 if (StandardConversionExists (priv_fmet_param, best))
1062 // Used internally by FindMostEncompassingType, this is used
1063 // to avoid creating lots of objects in the tight loop inside
1064 // FindMostEncompassingType
1066 static EmptyExpression priv_fmee_ret;
1069 /// Finds "most encompassing type" according to the spec (13.4.2)
1070 /// amongst the types in the given set
1072 static Type FindMostEncompassingType (ArrayList types)
1076 if (priv_fmee_ret == null)
1077 priv_fmee_ret = new EmptyExpression ();
1079 foreach (Type t in types){
1080 priv_fmee_ret.SetType (best);
1087 if (StandardConversionExists (priv_fmee_ret, t))
1095 // Used to avoid creating too many objects
1097 static EmptyExpression priv_fms_expr;
1100 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1101 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1102 /// for explicit and implicit conversion operators.
1104 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1105 bool apply_explicit_conv_rules,
1108 ArrayList src_types_set = new ArrayList ();
1110 if (priv_fms_expr == null)
1111 priv_fms_expr = new EmptyExpression ();
1114 // If any operator converts from S then Sx = S
1116 foreach (MethodBase mb in me.Methods){
1117 ParameterData pd = Invocation.GetParameterData (mb);
1118 Type param_type = pd.ParameterType (0);
1120 if (param_type == source_type)
1123 if (apply_explicit_conv_rules) {
1126 // Find the set of applicable user-defined conversion operators, U. This set
1128 // user-defined implicit or explicit conversion operators declared by
1129 // the classes or structs in D that convert from a type encompassing
1130 // or encompassed by S to a type encompassing or encompassed by T
1132 priv_fms_expr.SetType (param_type);
1133 if (StandardConversionExists (priv_fms_expr, source_type))
1134 src_types_set.Add (param_type);
1136 priv_fms_expr.SetType (source_type);
1137 if (StandardConversionExists (priv_fms_expr, param_type))
1138 src_types_set.Add (param_type);
1142 // Only if S is encompassed by param_type
1144 priv_fms_expr.SetType (source_type);
1145 if (StandardConversionExists (priv_fms_expr, param_type))
1146 src_types_set.Add (param_type);
1151 // Explicit Conv rules
1153 if (apply_explicit_conv_rules) {
1154 ArrayList candidate_set = new ArrayList ();
1156 foreach (Type param_type in src_types_set){
1157 priv_fms_expr.SetType (source_type);
1159 if (StandardConversionExists (priv_fms_expr, param_type))
1160 candidate_set.Add (param_type);
1163 if (candidate_set.Count != 0)
1164 return FindMostEncompassedType (candidate_set);
1170 if (apply_explicit_conv_rules)
1171 return FindMostEncompassingType (src_types_set);
1173 return FindMostEncompassedType (src_types_set);
1177 // Useful in avoiding proliferation of objects
1179 static EmptyExpression priv_fmt_expr;
1182 /// Finds the most specific target Tx according to section 13.4.4
1184 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1185 bool apply_explicit_conv_rules,
1188 ArrayList tgt_types_set = new ArrayList ();
1190 if (priv_fmt_expr == null)
1191 priv_fmt_expr = new EmptyExpression ();
1194 // If any operator converts to T then Tx = T
1196 foreach (MethodInfo mi in me.Methods){
1197 Type ret_type = mi.ReturnType;
1199 if (ret_type == target)
1202 if (apply_explicit_conv_rules) {
1205 // Find the set of applicable user-defined conversion operators, U.
1207 // This set consists of the
1208 // user-defined implicit or explicit conversion operators declared by
1209 // the classes or structs in D that convert from a type encompassing
1210 // or encompassed by S to a type encompassing or encompassed by T
1212 priv_fms_expr.SetType (ret_type);
1213 if (StandardConversionExists (priv_fms_expr, target))
1214 tgt_types_set.Add (ret_type);
1216 priv_fms_expr.SetType (target);
1217 if (StandardConversionExists (priv_fms_expr, ret_type))
1218 tgt_types_set.Add (ret_type);
1222 // Only if T is encompassed by param_type
1224 priv_fms_expr.SetType (ret_type);
1225 if (StandardConversionExists (priv_fms_expr, target))
1226 tgt_types_set.Add (ret_type);
1231 // Explicit conv rules
1233 if (apply_explicit_conv_rules) {
1234 ArrayList candidate_set = new ArrayList ();
1236 foreach (Type ret_type in tgt_types_set){
1237 priv_fmt_expr.SetType (ret_type);
1239 if (StandardConversionExists (priv_fmt_expr, target))
1240 candidate_set.Add (ret_type);
1243 if (candidate_set.Count != 0)
1244 return FindMostEncompassingType (candidate_set);
1248 // Okay, final case !
1250 if (apply_explicit_conv_rules)
1251 return FindMostEncompassedType (tgt_types_set);
1253 return FindMostEncompassingType (tgt_types_set);
1257 /// User-defined Implicit conversions
1259 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1260 Type target, Location loc)
1262 return UserDefinedConversion (ec, source, target, loc, false);
1266 /// User-defined Explicit conversions
1268 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1269 Type target, Location loc)
1271 return UserDefinedConversion (ec, source, target, loc, true);
1275 /// Computes the MethodGroup for the user-defined conversion
1276 /// operators from source_type to target_type. `look_for_explicit'
1277 /// controls whether we should also include the list of explicit
1280 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1281 Type source_type, Type target_type,
1282 Location loc, bool look_for_explicit)
1284 Expression mg1 = null, mg2 = null;
1285 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1289 // FIXME : How does the False operator come into the picture ?
1290 // This doesn't look complete and very correct !
1292 if (target_type == TypeManager.bool_type && !look_for_explicit)
1293 op_name = "op_True";
1295 op_name = "op_Implicit";
1297 MethodGroupExpr union3;
1299 mg1 = MethodLookup (ec, source_type, op_name, loc);
1300 if (source_type.BaseType != null)
1301 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1304 union3 = (MethodGroupExpr) mg2;
1305 else if (mg2 == null)
1306 union3 = (MethodGroupExpr) mg1;
1308 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1310 mg1 = MethodLookup (ec, target_type, op_name, loc);
1313 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1315 union3 = (MethodGroupExpr) mg1;
1318 if (target_type.BaseType != null)
1319 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1323 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1325 union3 = (MethodGroupExpr) mg1;
1328 MethodGroupExpr union4 = null;
1330 if (look_for_explicit) {
1331 op_name = "op_Explicit";
1333 mg5 = MemberLookup (ec, source_type, op_name, loc);
1334 if (source_type.BaseType != null)
1335 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1337 mg7 = MemberLookup (ec, target_type, op_name, loc);
1338 if (target_type.BaseType != null)
1339 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1341 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1342 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1344 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1347 return Invocation.MakeUnionSet (union3, union4, loc);
1351 /// User-defined conversions
1353 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1354 Type target, Location loc,
1355 bool look_for_explicit)
1357 MethodGroupExpr union;
1358 Type source_type = source.Type;
1359 MethodBase method = null;
1361 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1365 Type most_specific_source, most_specific_target;
1368 foreach (MethodBase m in union.Methods){
1369 Console.WriteLine ("Name: " + m.Name);
1370 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1374 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1375 if (most_specific_source == null)
1378 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1379 if (most_specific_target == null)
1384 foreach (MethodBase mb in union.Methods){
1385 ParameterData pd = Invocation.GetParameterData (mb);
1386 MethodInfo mi = (MethodInfo) mb;
1388 if (pd.ParameterType (0) == most_specific_source &&
1389 mi.ReturnType == most_specific_target) {
1395 if (method == null || count > 1) {
1396 Report.Error (-11, loc, "Ambiguous user defined conversion");
1401 // This will do the conversion to the best match that we
1402 // found. Now we need to perform an implict standard conversion
1403 // if the best match was not the type that we were requested
1406 if (look_for_explicit)
1407 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1409 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1415 e = new UserCast ((MethodInfo) method, source);
1416 if (e.Type != target){
1417 if (!look_for_explicit)
1418 e = ConvertImplicitStandard (ec, e, target, loc);
1420 e = ConvertExplicitStandard (ec, e, target, loc);
1426 /// Converts implicitly the resolved expression `expr' into the
1427 /// `target_type'. It returns a new expression that can be used
1428 /// in a context that expects a `target_type'.
1430 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1431 Type target_type, Location loc)
1433 Type expr_type = expr.Type;
1436 if (expr_type == target_type)
1439 if (target_type == null)
1440 throw new Exception ("Target type is null");
1442 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1446 e = ImplicitUserConversion (ec, expr, target_type, loc);
1455 /// Attempts to apply the `Standard Implicit
1456 /// Conversion' rules to the expression `expr' into
1457 /// the `target_type'. It returns a new expression
1458 /// that can be used in a context that expects a
1461 /// This is different from `ConvertImplicit' in that the
1462 /// user defined implicit conversions are excluded.
1464 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1465 Type target_type, Location loc)
1467 Type expr_type = expr.Type;
1470 if (expr_type == target_type)
1473 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1477 e = ImplicitReferenceConversion (expr, target_type);
1481 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1482 IntLiteral i = (IntLiteral) expr;
1485 return new EmptyCast (expr, target_type);
1489 if (expr_type.IsPointer){
1490 if (target_type == TypeManager.void_ptr_type)
1491 return new EmptyCast (expr, target_type);
1494 // yep, comparing pointer types cant be done with
1495 // t1 == t2, we have to compare their element types.
1497 if (target_type.IsPointer){
1498 if (target_type.GetElementType()==expr_type.GetElementType())
1503 if (target_type.IsPointer){
1504 if (expr is NullLiteral)
1505 return new EmptyCast (expr, target_type);
1513 /// Attemps to perform an implict constant conversion of the IntConstant
1514 /// into a different data type using casts (See Implicit Constant
1515 /// Expression Conversions)
1517 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1519 int value = ic.Value;
1522 // FIXME: This could return constants instead of EmptyCasts
1524 if (target_type == TypeManager.sbyte_type){
1525 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1526 return new SByteConstant ((sbyte) value);
1527 } else if (target_type == TypeManager.byte_type){
1528 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1529 return new ByteConstant ((byte) value);
1530 } else if (target_type == TypeManager.short_type){
1531 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1532 return new ShortConstant ((short) value);
1533 } else if (target_type == TypeManager.ushort_type){
1534 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1535 return new UShortConstant ((ushort) value);
1536 } else if (target_type == TypeManager.uint32_type){
1538 return new UIntConstant ((uint) value);
1539 } else if (target_type == TypeManager.uint64_type){
1541 // we can optimize this case: a positive int32
1542 // always fits on a uint64. But we need an opcode
1546 return new ULongConstant ((ulong) value);
1549 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1550 return new EnumConstant (ic, target_type);
1555 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1557 string msg = "Cannot convert implicitly from `"+
1558 TypeManager.CSharpName (source) + "' to `" +
1559 TypeManager.CSharpName (target) + "'";
1561 Error (29, loc, msg);
1565 /// Attemptes to implicityly convert `target' into `type', using
1566 /// ConvertImplicit. If there is no implicit conversion, then
1567 /// an error is signaled
1569 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1570 Type target_type, Location loc)
1574 e = ConvertImplicit (ec, source, target_type, loc);
1578 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1580 "Double literal cannot be implicitly converted to " +
1581 "float type, use F suffix to create a float literal");
1584 Error_CannotConvertImplicit (loc, source.Type, target_type);
1590 /// Performs the explicit numeric conversions
1592 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1594 Type expr_type = expr.Type;
1597 // If we have an enumeration, extract the underlying type,
1598 // use this during the comparission, but wrap around the original
1601 Type real_target_type = target_type;
1603 if (TypeManager.IsEnumType (real_target_type))
1604 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1606 if (expr_type == TypeManager.sbyte_type){
1608 // From sbyte to byte, ushort, uint, ulong, char
1610 if (real_target_type == TypeManager.byte_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1612 if (real_target_type == TypeManager.ushort_type)
1613 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1614 if (real_target_type == TypeManager.uint32_type)
1615 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1616 if (real_target_type == TypeManager.uint64_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1618 if (real_target_type == TypeManager.char_type)
1619 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1620 } else if (expr_type == TypeManager.byte_type){
1622 // From byte to sbyte and char
1624 if (real_target_type == TypeManager.sbyte_type)
1625 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1626 if (real_target_type == TypeManager.char_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1628 } else if (expr_type == TypeManager.short_type){
1630 // From short to sbyte, byte, ushort, uint, ulong, char
1632 if (real_target_type == TypeManager.sbyte_type)
1633 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1634 if (real_target_type == TypeManager.byte_type)
1635 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1636 if (real_target_type == TypeManager.ushort_type)
1637 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1638 if (real_target_type == TypeManager.uint32_type)
1639 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1640 if (real_target_type == TypeManager.uint64_type)
1641 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1642 if (real_target_type == TypeManager.char_type)
1643 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1644 } else if (expr_type == TypeManager.ushort_type){
1646 // From ushort to sbyte, byte, short, char
1648 if (real_target_type == TypeManager.sbyte_type)
1649 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1650 if (real_target_type == TypeManager.byte_type)
1651 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1652 if (real_target_type == TypeManager.short_type)
1653 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1654 if (real_target_type == TypeManager.char_type)
1655 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1656 } else if (expr_type == TypeManager.int32_type){
1658 // From int to sbyte, byte, short, ushort, uint, ulong, char
1660 if (real_target_type == TypeManager.sbyte_type)
1661 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1662 if (real_target_type == TypeManager.byte_type)
1663 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1664 if (real_target_type == TypeManager.short_type)
1665 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1666 if (real_target_type == TypeManager.ushort_type)
1667 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1668 if (real_target_type == TypeManager.uint32_type)
1669 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1670 if (real_target_type == TypeManager.uint64_type)
1671 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1672 if (real_target_type == TypeManager.char_type)
1673 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1674 } else if (expr_type == TypeManager.uint32_type){
1676 // From uint to sbyte, byte, short, ushort, int, char
1678 if (real_target_type == TypeManager.sbyte_type)
1679 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1680 if (real_target_type == TypeManager.byte_type)
1681 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1682 if (real_target_type == TypeManager.short_type)
1683 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1684 if (real_target_type == TypeManager.ushort_type)
1685 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1686 if (real_target_type == TypeManager.int32_type)
1687 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1688 if (real_target_type == TypeManager.char_type)
1689 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1690 } else if (expr_type == TypeManager.int64_type){
1692 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1694 if (real_target_type == TypeManager.sbyte_type)
1695 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1696 if (real_target_type == TypeManager.byte_type)
1697 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1698 if (real_target_type == TypeManager.short_type)
1699 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1700 if (real_target_type == TypeManager.ushort_type)
1701 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1702 if (real_target_type == TypeManager.int32_type)
1703 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1704 if (real_target_type == TypeManager.uint32_type)
1705 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1706 if (real_target_type == TypeManager.uint64_type)
1707 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1708 if (real_target_type == TypeManager.char_type)
1709 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1710 } else if (expr_type == TypeManager.uint64_type){
1712 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1714 if (real_target_type == TypeManager.sbyte_type)
1715 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1716 if (real_target_type == TypeManager.byte_type)
1717 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1718 if (real_target_type == TypeManager.short_type)
1719 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1720 if (real_target_type == TypeManager.ushort_type)
1721 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1722 if (real_target_type == TypeManager.int32_type)
1723 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1724 if (real_target_type == TypeManager.uint32_type)
1725 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1726 if (real_target_type == TypeManager.int64_type)
1727 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1728 if (real_target_type == TypeManager.char_type)
1729 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1730 } else if (expr_type == TypeManager.char_type){
1732 // From char to sbyte, byte, short
1734 if (real_target_type == TypeManager.sbyte_type)
1735 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1736 if (real_target_type == TypeManager.byte_type)
1737 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1738 if (real_target_type == TypeManager.short_type)
1739 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1740 } else if (expr_type == TypeManager.float_type){
1742 // From float to sbyte, byte, short,
1743 // ushort, int, uint, long, ulong, char
1746 if (real_target_type == TypeManager.sbyte_type)
1747 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1748 if (real_target_type == TypeManager.byte_type)
1749 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1750 if (real_target_type == TypeManager.short_type)
1751 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1752 if (real_target_type == TypeManager.ushort_type)
1753 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1754 if (real_target_type == TypeManager.int32_type)
1755 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1756 if (real_target_type == TypeManager.uint32_type)
1757 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1758 if (real_target_type == TypeManager.int64_type)
1759 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1760 if (real_target_type == TypeManager.uint64_type)
1761 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1762 if (real_target_type == TypeManager.char_type)
1763 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1764 if (real_target_type == TypeManager.decimal_type)
1765 return InternalTypeConstructor (ec, expr, target_type);
1766 } else if (expr_type == TypeManager.double_type){
1768 // From double to byte, byte, short,
1769 // ushort, int, uint, long, ulong,
1770 // char, float or decimal
1772 if (real_target_type == TypeManager.sbyte_type)
1773 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1774 if (real_target_type == TypeManager.byte_type)
1775 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1776 if (real_target_type == TypeManager.short_type)
1777 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1778 if (real_target_type == TypeManager.ushort_type)
1779 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1780 if (real_target_type == TypeManager.int32_type)
1781 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1782 if (real_target_type == TypeManager.uint32_type)
1783 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1784 if (real_target_type == TypeManager.int64_type)
1785 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1786 if (real_target_type == TypeManager.uint64_type)
1787 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1788 if (real_target_type == TypeManager.char_type)
1789 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1790 if (real_target_type == TypeManager.float_type)
1791 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1792 if (real_target_type == TypeManager.decimal_type)
1793 return InternalTypeConstructor (ec, expr, target_type);
1796 // decimal is taken care of by the op_Explicit methods.
1802 /// Returns whether an explicit reference conversion can be performed
1803 /// from source_type to target_type
1805 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1807 bool target_is_value_type = target_type.IsValueType;
1809 if (source_type == target_type)
1813 // From object to any reference type
1815 if (source_type == TypeManager.object_type && !target_is_value_type)
1819 // From any class S to any class-type T, provided S is a base class of T
1821 if (target_type.IsSubclassOf (source_type))
1825 // From any interface type S to any interface T provided S is not derived from T
1827 if (source_type.IsInterface && target_type.IsInterface){
1828 if (!target_type.IsSubclassOf (source_type))
1833 // From any class type S to any interface T, provided S is not sealed
1834 // and provided S does not implement T.
1836 if (target_type.IsInterface && !source_type.IsSealed &&
1837 !TypeManager.ImplementsInterface (source_type, target_type))
1841 // From any interface-type S to to any class type T, provided T is not
1842 // sealed, or provided T implements S.
1844 if (source_type.IsInterface &&
1845 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1849 // From an array type S with an element type Se to an array type T with an
1850 // element type Te provided all the following are true:
1851 // * S and T differe only in element type, in other words, S and T
1852 // have the same number of dimensions.
1853 // * Both Se and Te are reference types
1854 // * An explicit referenc conversions exist from Se to Te
1856 if (source_type.IsArray && target_type.IsArray) {
1857 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1859 Type source_element_type = source_type.GetElementType ();
1860 Type target_element_type = target_type.GetElementType ();
1862 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1863 if (ExplicitReferenceConversionExists (source_element_type,
1864 target_element_type))
1870 // From System.Array to any array-type
1871 if (source_type == TypeManager.array_type &&
1872 target_type.IsArray){
1877 // From System delegate to any delegate-type
1879 if (source_type == TypeManager.delegate_type &&
1880 target_type.IsSubclassOf (TypeManager.delegate_type))
1884 // From ICloneable to Array or Delegate types
1886 if (source_type == TypeManager.icloneable_type &&
1887 (target_type == TypeManager.array_type ||
1888 target_type == TypeManager.delegate_type))
1895 /// Implements Explicit Reference conversions
1897 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1899 Type source_type = source.Type;
1900 bool target_is_value_type = target_type.IsValueType;
1903 // From object to any reference type
1905 if (source_type == TypeManager.object_type && !target_is_value_type)
1906 return new ClassCast (source, target_type);
1910 // From any class S to any class-type T, provided S is a base class of T
1912 if (target_type.IsSubclassOf (source_type))
1913 return new ClassCast (source, target_type);
1916 // From any interface type S to any interface T provided S is not derived from T
1918 if (source_type.IsInterface && target_type.IsInterface){
1919 if (TypeManager.ImplementsInterface (source_type, target_type))
1922 return new ClassCast (source, target_type);
1926 // From any class type S to any interface T, provides S is not sealed
1927 // and provided S does not implement T.
1929 if (target_type.IsInterface && !source_type.IsSealed) {
1930 if (TypeManager.ImplementsInterface (source_type, target_type))
1933 return new ClassCast (source, target_type);
1938 // From any interface-type S to to any class type T, provided T is not
1939 // sealed, or provided T implements S.
1941 if (source_type.IsInterface) {
1942 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
1943 return new ClassCast (source, target_type);
1948 // From an array type S with an element type Se to an array type T with an
1949 // element type Te provided all the following are true:
1950 // * S and T differe only in element type, in other words, S and T
1951 // have the same number of dimensions.
1952 // * Both Se and Te are reference types
1953 // * An explicit referenc conversions exist from Se to Te
1955 if (source_type.IsArray && target_type.IsArray) {
1956 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1958 Type source_element_type = source_type.GetElementType ();
1959 Type target_element_type = target_type.GetElementType ();
1961 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1962 if (ExplicitReferenceConversionExists (source_element_type,
1963 target_element_type))
1964 return new ClassCast (source, target_type);
1969 // From System.Array to any array-type
1970 if (source_type == TypeManager.array_type &&
1971 target_type.IsArray) {
1972 return new ClassCast (source, target_type);
1976 // From System delegate to any delegate-type
1978 if (source_type == TypeManager.delegate_type &&
1979 target_type.IsSubclassOf (TypeManager.delegate_type))
1980 return new ClassCast (source, target_type);
1983 // From ICloneable to Array or Delegate types
1985 if (source_type == TypeManager.icloneable_type &&
1986 (target_type == TypeManager.array_type ||
1987 target_type == TypeManager.delegate_type))
1988 return new ClassCast (source, target_type);
1994 /// Performs an explicit conversion of the expression `expr' whose
1995 /// type is expr.Type to `target_type'.
1997 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1998 Type target_type, Location loc)
2000 Type expr_type = expr.Type;
2001 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2006 ne = ConvertNumericExplicit (ec, expr, target_type);
2011 // Unboxing conversion.
2013 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2014 return new UnboxCast (expr, target_type);
2019 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2023 // FIXME: Is there any reason we should have EnumConstant
2024 // dealt with here instead of just using always the
2025 // UnderlyingSystemType to wrap the type?
2027 if (expr is EnumConstant)
2028 e = ((EnumConstant) expr).Child;
2030 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2033 Expression t = ConvertImplicit (ec, e, target_type, loc);
2037 return ConvertNumericExplicit (ec, e, target_type);
2040 ne = ConvertReferenceExplicit (expr, target_type);
2045 if (target_type.IsPointer){
2046 if (expr_type.IsPointer)
2047 return new EmptyCast (expr, target_type);
2049 if (expr_type == TypeManager.sbyte_type ||
2050 expr_type == TypeManager.byte_type ||
2051 expr_type == TypeManager.short_type ||
2052 expr_type == TypeManager.ushort_type ||
2053 expr_type == TypeManager.int32_type ||
2054 expr_type == TypeManager.uint32_type ||
2055 expr_type == TypeManager.uint64_type ||
2056 expr_type == TypeManager.int64_type)
2057 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2059 if (expr_type.IsPointer){
2060 if (target_type == TypeManager.sbyte_type ||
2061 target_type == TypeManager.byte_type ||
2062 target_type == TypeManager.short_type ||
2063 target_type == TypeManager.ushort_type ||
2064 target_type == TypeManager.int32_type ||
2065 target_type == TypeManager.uint32_type ||
2066 target_type == TypeManager.uint64_type ||
2067 target_type == TypeManager.int64_type){
2068 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2071 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2076 ce = ConvertNumericExplicit (ec, e, target_type);
2080 // We should always be able to go from an uint32
2081 // implicitly or explicitly to the other integral
2084 throw new Exception ("Internal compiler error");
2089 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2093 Error_CannotConvertType (loc, expr_type, target_type);
2098 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2100 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2101 Type target_type, Location l)
2103 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2108 ne = ConvertNumericExplicit (ec, expr, target_type);
2112 ne = ConvertReferenceExplicit (expr, target_type);
2116 Error_CannotConvertType (l, expr.Type, target_type);
2120 static string ExprClassName (ExprClass c)
2123 case ExprClass.Invalid:
2125 case ExprClass.Value:
2127 case ExprClass.Variable:
2129 case ExprClass.Namespace:
2131 case ExprClass.Type:
2133 case ExprClass.MethodGroup:
2134 return "method group";
2135 case ExprClass.PropertyAccess:
2136 return "property access";
2137 case ExprClass.EventAccess:
2138 return "event access";
2139 case ExprClass.IndexerAccess:
2140 return "indexer access";
2141 case ExprClass.Nothing:
2144 throw new Exception ("Should not happen");
2148 /// Reports that we were expecting `expr' to be of class `expected'
2150 protected void report118 (Location loc, Expression expr, string expected)
2152 string kind = "Unknown";
2155 kind = ExprClassName (expr.eclass);
2157 Error (118, loc, "Expression denotes a `" + kind +
2158 "' where a `" + expected + "' was expected");
2161 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2163 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2164 TypeManager.CSharpName (t));
2167 public static void UnsafeError (Location loc)
2169 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2173 /// Converts the IntConstant, UIntConstant, LongConstant or
2174 /// ULongConstant into the integral target_type. Notice
2175 /// that we do not return an `Expression' we do return
2176 /// a boxed integral type.
2178 /// FIXME: Since I added the new constants, we need to
2179 /// also support conversions from CharConstant, ByteConstant,
2180 /// SByteConstant, UShortConstant, ShortConstant
2182 /// This is used by the switch statement, so the domain
2183 /// of work is restricted to the literals above, and the
2184 /// targets are int32, uint32, char, byte, sbyte, ushort,
2185 /// short, uint64 and int64
2187 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2191 if (c.Type == target_type)
2192 return ((Constant) c).GetValue ();
2195 // Make into one of the literals we handle, we dont really care
2196 // about this value as we will just return a few limited types
2198 if (c is EnumConstant)
2199 c = ((EnumConstant)c).WidenToCompilerConstant ();
2201 if (c is IntConstant){
2202 int v = ((IntConstant) c).Value;
2204 if (target_type == TypeManager.uint32_type){
2207 } else if (target_type == TypeManager.char_type){
2208 if (v >= Char.MinValue && v <= Char.MaxValue)
2210 } else if (target_type == TypeManager.byte_type){
2211 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2213 } else if (target_type == TypeManager.sbyte_type){
2214 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2216 } else if (target_type == TypeManager.short_type){
2217 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2219 } else if (target_type == TypeManager.ushort_type){
2220 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2222 } else if (target_type == TypeManager.int64_type)
2224 else if (target_type == TypeManager.uint64_type){
2230 } else if (c is UIntConstant){
2231 uint v = ((UIntConstant) c).Value;
2233 if (target_type == TypeManager.int32_type){
2234 if (v <= Int32.MaxValue)
2236 } else if (target_type == TypeManager.char_type){
2237 if (v >= Char.MinValue && v <= Char.MaxValue)
2239 } else if (target_type == TypeManager.byte_type){
2240 if (v <= Byte.MaxValue)
2242 } else if (target_type == TypeManager.sbyte_type){
2243 if (v <= SByte.MaxValue)
2245 } else if (target_type == TypeManager.short_type){
2246 if (v <= UInt16.MaxValue)
2248 } else if (target_type == TypeManager.ushort_type){
2249 if (v <= UInt16.MaxValue)
2251 } else if (target_type == TypeManager.int64_type)
2253 else if (target_type == TypeManager.uint64_type)
2256 } else if (c is LongConstant){
2257 long v = ((LongConstant) c).Value;
2259 if (target_type == TypeManager.int32_type){
2260 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2262 } else if (target_type == TypeManager.uint32_type){
2263 if (v >= 0 && v <= UInt32.MaxValue)
2265 } else if (target_type == TypeManager.char_type){
2266 if (v >= Char.MinValue && v <= Char.MaxValue)
2268 } else if (target_type == TypeManager.byte_type){
2269 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2271 } else if (target_type == TypeManager.sbyte_type){
2272 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2274 } else if (target_type == TypeManager.short_type){
2275 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2277 } else if (target_type == TypeManager.ushort_type){
2278 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2280 } else if (target_type == TypeManager.uint64_type){
2285 } else if (c is ULongConstant){
2286 ulong v = ((ULongConstant) c).Value;
2288 if (target_type == TypeManager.int32_type){
2289 if (v <= Int32.MaxValue)
2291 } else if (target_type == TypeManager.uint32_type){
2292 if (v <= UInt32.MaxValue)
2294 } else if (target_type == TypeManager.char_type){
2295 if (v >= Char.MinValue && v <= Char.MaxValue)
2297 } else if (target_type == TypeManager.byte_type){
2298 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2300 } else if (target_type == TypeManager.sbyte_type){
2301 if (v <= (int) SByte.MaxValue)
2303 } else if (target_type == TypeManager.short_type){
2304 if (v <= UInt16.MaxValue)
2306 } else if (target_type == TypeManager.ushort_type){
2307 if (v <= UInt16.MaxValue)
2309 } else if (target_type == TypeManager.int64_type){
2310 if (v <= Int64.MaxValue)
2314 } else if (c is ByteConstant){
2315 byte v = ((ByteConstant) c).Value;
2317 if (target_type == TypeManager.int32_type)
2319 else if (target_type == TypeManager.uint32_type)
2321 else if (target_type == TypeManager.char_type)
2323 else if (target_type == TypeManager.sbyte_type){
2324 if (v <= SByte.MaxValue)
2326 } else if (target_type == TypeManager.short_type)
2328 else if (target_type == TypeManager.ushort_type)
2330 else if (target_type == TypeManager.int64_type)
2332 else if (target_type == TypeManager.uint64_type)
2335 } else if (c is SByteConstant){
2336 sbyte v = ((SByteConstant) c).Value;
2338 if (target_type == TypeManager.int32_type)
2340 else if (target_type == TypeManager.uint32_type){
2343 } else if (target_type == TypeManager.char_type){
2346 } else if (target_type == TypeManager.byte_type){
2349 } else if (target_type == TypeManager.short_type)
2351 else if (target_type == TypeManager.ushort_type){
2354 } else if (target_type == TypeManager.int64_type)
2356 else if (target_type == TypeManager.uint64_type){
2361 } else if (c is ShortConstant){
2362 short v = ((ShortConstant) c).Value;
2364 if (target_type == TypeManager.int32_type){
2366 } else if (target_type == TypeManager.uint32_type){
2369 } else if (target_type == TypeManager.char_type){
2372 } else if (target_type == TypeManager.byte_type){
2373 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2375 } else if (target_type == TypeManager.sbyte_type){
2376 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2378 } else if (target_type == TypeManager.ushort_type){
2381 } else if (target_type == TypeManager.int64_type)
2383 else if (target_type == TypeManager.uint64_type)
2387 } else if (c is UShortConstant){
2388 ushort v = ((UShortConstant) c).Value;
2390 if (target_type == TypeManager.int32_type)
2392 else if (target_type == TypeManager.uint32_type)
2394 else if (target_type == TypeManager.char_type){
2395 if (v >= Char.MinValue && v <= Char.MaxValue)
2397 } else if (target_type == TypeManager.byte_type){
2398 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2400 } else if (target_type == TypeManager.sbyte_type){
2401 if (v <= SByte.MaxValue)
2403 } else if (target_type == TypeManager.short_type){
2404 if (v <= Int16.MaxValue)
2406 } else if (target_type == TypeManager.int64_type)
2408 else if (target_type == TypeManager.uint64_type)
2412 } else if (c is CharConstant){
2413 char v = ((CharConstant) c).Value;
2415 if (target_type == TypeManager.int32_type)
2417 else if (target_type == TypeManager.uint32_type)
2419 else if (target_type == TypeManager.byte_type){
2420 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2422 } else if (target_type == TypeManager.sbyte_type){
2423 if (v <= SByte.MaxValue)
2425 } else if (target_type == TypeManager.short_type){
2426 if (v <= Int16.MaxValue)
2428 } else if (target_type == TypeManager.ushort_type)
2430 else if (target_type == TypeManager.int64_type)
2432 else if (target_type == TypeManager.uint64_type)
2437 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2442 // Load the object from the pointer.
2444 public static void LoadFromPtr (ILGenerator ig, Type t)
2446 if (t == TypeManager.int32_type)
2447 ig.Emit (OpCodes.Ldind_I4);
2448 else if (t == TypeManager.uint32_type)
2449 ig.Emit (OpCodes.Ldind_U4);
2450 else if (t == TypeManager.short_type)
2451 ig.Emit (OpCodes.Ldind_I2);
2452 else if (t == TypeManager.ushort_type)
2453 ig.Emit (OpCodes.Ldind_U2);
2454 else if (t == TypeManager.char_type)
2455 ig.Emit (OpCodes.Ldind_U2);
2456 else if (t == TypeManager.byte_type)
2457 ig.Emit (OpCodes.Ldind_U1);
2458 else if (t == TypeManager.sbyte_type)
2459 ig.Emit (OpCodes.Ldind_I1);
2460 else if (t == TypeManager.uint64_type)
2461 ig.Emit (OpCodes.Ldind_I8);
2462 else if (t == TypeManager.int64_type)
2463 ig.Emit (OpCodes.Ldind_I8);
2464 else if (t == TypeManager.float_type)
2465 ig.Emit (OpCodes.Ldind_R4);
2466 else if (t == TypeManager.double_type)
2467 ig.Emit (OpCodes.Ldind_R8);
2468 else if (t == TypeManager.bool_type)
2469 ig.Emit (OpCodes.Ldind_I1);
2470 else if (t == TypeManager.intptr_type)
2471 ig.Emit (OpCodes.Ldind_I);
2472 else if (TypeManager.IsEnumType (t)) {
2473 if (t == TypeManager.enum_type)
2474 ig.Emit (OpCodes.Ldind_Ref);
2476 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2477 } else if (t.IsValueType)
2478 ig.Emit (OpCodes.Ldobj, t);
2480 ig.Emit (OpCodes.Ldind_Ref);
2484 // The stack contains the pointer and the value of type `type'
2486 public static void StoreFromPtr (ILGenerator ig, Type type)
2489 type = TypeManager.EnumToUnderlying (type);
2490 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2491 ig.Emit (OpCodes.Stind_I4);
2492 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2493 ig.Emit (OpCodes.Stind_I8);
2494 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2495 type == TypeManager.ushort_type)
2496 ig.Emit (OpCodes.Stind_I2);
2497 else if (type == TypeManager.float_type)
2498 ig.Emit (OpCodes.Stind_R4);
2499 else if (type == TypeManager.double_type)
2500 ig.Emit (OpCodes.Stind_R8);
2501 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2502 type == TypeManager.bool_type)
2503 ig.Emit (OpCodes.Stind_I1);
2504 else if (type == TypeManager.intptr_type)
2505 ig.Emit (OpCodes.Stind_I);
2506 else if (type.IsValueType)
2507 ig.Emit (OpCodes.Stobj, type);
2509 ig.Emit (OpCodes.Stind_Ref);
2513 // Returns the size of type `t' if known, otherwise, 0
2515 public static int GetTypeSize (Type t)
2517 t = TypeManager.TypeToCoreType (t);
2518 if (t == TypeManager.int32_type ||
2519 t == TypeManager.uint32_type ||
2520 t == TypeManager.float_type)
2522 else if (t == TypeManager.int64_type ||
2523 t == TypeManager.uint64_type ||
2524 t == TypeManager.double_type)
2526 else if (t == TypeManager.byte_type ||
2527 t == TypeManager.sbyte_type ||
2528 t == TypeManager.bool_type)
2530 else if (t == TypeManager.short_type ||
2531 t == TypeManager.char_type ||
2532 t == TypeManager.ushort_type)
2539 // Default implementation of IAssignMethod.CacheTemporaries
2541 public void CacheTemporaries (EmitContext ec)
2545 static void Error_NegativeArrayIndex (Location loc)
2547 Report.Error (284, loc, "Can not create array with a negative size");
2551 // Converts `source' to an int, uint, long or ulong.
2553 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2557 bool old_checked = ec.CheckState;
2558 ec.CheckState = true;
2560 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2561 if (target == null){
2562 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2563 if (target == null){
2564 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2565 if (target == null){
2566 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2568 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2572 ec.CheckState = old_checked;
2575 // Only positive constants are allowed at compile time
2577 if (target is Constant){
2578 if (target is IntConstant){
2579 if (((IntConstant) target).Value < 0){
2580 Error_NegativeArrayIndex (loc);
2585 if (target is LongConstant){
2586 if (((LongConstant) target).Value < 0){
2587 Error_NegativeArrayIndex (loc);
2600 /// This is just a base class for expressions that can
2601 /// appear on statements (invocations, object creation,
2602 /// assignments, post/pre increment and decrement). The idea
2603 /// being that they would support an extra Emition interface that
2604 /// does not leave a result on the stack.
2606 public abstract class ExpressionStatement : Expression {
2609 /// Requests the expression to be emitted in a `statement'
2610 /// context. This means that no new value is left on the
2611 /// stack after invoking this method (constrasted with
2612 /// Emit that will always leave a value on the stack).
2614 public abstract void EmitStatement (EmitContext ec);
2618 /// This kind of cast is used to encapsulate the child
2619 /// whose type is child.Type into an expression that is
2620 /// reported to return "return_type". This is used to encapsulate
2621 /// expressions which have compatible types, but need to be dealt
2622 /// at higher levels with.
2624 /// For example, a "byte" expression could be encapsulated in one
2625 /// of these as an "unsigned int". The type for the expression
2626 /// would be "unsigned int".
2629 public class EmptyCast : Expression {
2630 protected Expression child;
2632 public EmptyCast (Expression child, Type return_type)
2634 eclass = child.eclass;
2639 public override Expression DoResolve (EmitContext ec)
2641 // This should never be invoked, we are born in fully
2642 // initialized state.
2647 public override void Emit (EmitContext ec)
2654 /// This class is used to wrap literals which belong inside Enums
2656 public class EnumConstant : Constant {
2657 public Constant Child;
2659 public EnumConstant (Constant child, Type enum_type)
2661 eclass = child.eclass;
2666 public override Expression DoResolve (EmitContext ec)
2668 // This should never be invoked, we are born in fully
2669 // initialized state.
2674 public override void Emit (EmitContext ec)
2679 public override object GetValue ()
2681 return Child.GetValue ();
2685 // Converts from one of the valid underlying types for an enumeration
2686 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2687 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2689 public Constant WidenToCompilerConstant ()
2691 Type t = TypeManager.EnumToUnderlying (Child.Type);
2692 object v = ((Constant) Child).GetValue ();;
2694 if (t == TypeManager.int32_type)
2695 return new IntConstant ((int) v);
2696 if (t == TypeManager.uint32_type)
2697 return new UIntConstant ((uint) v);
2698 if (t == TypeManager.int64_type)
2699 return new LongConstant ((long) v);
2700 if (t == TypeManager.uint64_type)
2701 return new ULongConstant ((ulong) v);
2702 if (t == TypeManager.short_type)
2703 return new ShortConstant ((short) v);
2704 if (t == TypeManager.ushort_type)
2705 return new UShortConstant ((ushort) v);
2706 if (t == TypeManager.byte_type)
2707 return new ByteConstant ((byte) v);
2708 if (t == TypeManager.sbyte_type)
2709 return new SByteConstant ((sbyte) v);
2711 throw new Exception ("Invalid enumeration underlying type: " + t);
2715 // Extracts the value in the enumeration on its native representation
2717 public object GetPlainValue ()
2719 Type t = TypeManager.EnumToUnderlying (Child.Type);
2720 object v = ((Constant) Child).GetValue ();;
2722 if (t == TypeManager.int32_type)
2724 if (t == TypeManager.uint32_type)
2726 if (t == TypeManager.int64_type)
2728 if (t == TypeManager.uint64_type)
2730 if (t == TypeManager.short_type)
2732 if (t == TypeManager.ushort_type)
2734 if (t == TypeManager.byte_type)
2736 if (t == TypeManager.sbyte_type)
2742 public override string AsString ()
2744 return Child.AsString ();
2747 public override DoubleConstant ConvertToDouble ()
2749 return Child.ConvertToDouble ();
2752 public override FloatConstant ConvertToFloat ()
2754 return Child.ConvertToFloat ();
2757 public override ULongConstant ConvertToULong ()
2759 return Child.ConvertToULong ();
2762 public override LongConstant ConvertToLong ()
2764 return Child.ConvertToLong ();
2767 public override UIntConstant ConvertToUInt ()
2769 return Child.ConvertToUInt ();
2772 public override IntConstant ConvertToInt ()
2774 return Child.ConvertToInt ();
2779 /// This kind of cast is used to encapsulate Value Types in objects.
2781 /// The effect of it is to box the value type emitted by the previous
2784 public class BoxedCast : EmptyCast {
2786 public BoxedCast (Expression expr)
2787 : base (expr, TypeManager.object_type)
2791 public override Expression DoResolve (EmitContext ec)
2793 // This should never be invoked, we are born in fully
2794 // initialized state.
2799 public override void Emit (EmitContext ec)
2803 ec.ig.Emit (OpCodes.Box, child.Type);
2807 public class UnboxCast : EmptyCast {
2808 public UnboxCast (Expression expr, Type return_type)
2809 : base (expr, return_type)
2813 public override Expression DoResolve (EmitContext ec)
2815 // This should never be invoked, we are born in fully
2816 // initialized state.
2821 public override void Emit (EmitContext ec)
2824 ILGenerator ig = ec.ig;
2827 ig.Emit (OpCodes.Unbox, t);
2829 LoadFromPtr (ig, t);
2834 /// This is used to perform explicit numeric conversions.
2836 /// Explicit numeric conversions might trigger exceptions in a checked
2837 /// context, so they should generate the conv.ovf opcodes instead of
2840 public class ConvCast : EmptyCast {
2841 public enum Mode : byte {
2842 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2844 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2845 U2_I1, U2_U1, U2_I2, U2_CH,
2846 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2847 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2848 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2849 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2850 CH_I1, CH_U1, CH_I2,
2851 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2852 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2858 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2859 : base (child, return_type)
2861 checked_state = ec.CheckState;
2865 public override Expression DoResolve (EmitContext ec)
2867 // This should never be invoked, we are born in fully
2868 // initialized state.
2873 public override void Emit (EmitContext ec)
2875 ILGenerator ig = ec.ig;
2881 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2882 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2883 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2884 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2885 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2887 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2888 case Mode.U1_CH: /* nothing */ break;
2890 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2891 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2892 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2893 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2894 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2895 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2897 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2898 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2899 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2900 case Mode.U2_CH: /* nothing */ break;
2902 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2903 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2904 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2905 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2906 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2907 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2908 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2910 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2911 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2912 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2913 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2914 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2915 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2917 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2918 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2919 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2920 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2921 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2922 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2923 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2924 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2926 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2927 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2928 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2929 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2930 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2931 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2932 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2933 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2935 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2936 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2937 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2939 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2940 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2941 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2942 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2943 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2944 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2945 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2946 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2947 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2949 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2950 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2951 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2952 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2953 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2954 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2955 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2956 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2957 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2958 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2962 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2963 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2964 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2965 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2966 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2968 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2969 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2971 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2972 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2973 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2974 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2975 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2976 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2978 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2979 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2980 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2981 case Mode.U2_CH: /* nothing */ break;
2983 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2984 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2985 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2986 case Mode.I4_U4: /* nothing */ break;
2987 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2988 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2989 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2991 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2992 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2993 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2994 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2995 case Mode.U4_I4: /* nothing */ break;
2996 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2998 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2999 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3000 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3001 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3002 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3003 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3004 case Mode.I8_U8: /* nothing */ break;
3005 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3007 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3008 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3009 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3010 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3011 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3012 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3013 case Mode.U8_I8: /* nothing */ break;
3014 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3016 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3017 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3018 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3020 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3021 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3022 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3023 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3024 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3025 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3026 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3027 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3028 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3030 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3031 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3032 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3033 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3034 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3035 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3036 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3037 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3038 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3039 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3045 public class OpcodeCast : EmptyCast {
3049 public OpcodeCast (Expression child, Type return_type, OpCode op)
3050 : base (child, return_type)
3054 second_valid = false;
3057 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3058 : base (child, return_type)
3063 second_valid = true;
3066 public override Expression DoResolve (EmitContext ec)
3068 // This should never be invoked, we are born in fully
3069 // initialized state.
3074 public override void Emit (EmitContext ec)
3085 /// This kind of cast is used to encapsulate a child and cast it
3086 /// to the class requested
3088 public class ClassCast : EmptyCast {
3089 public ClassCast (Expression child, Type return_type)
3090 : base (child, return_type)
3095 public override Expression DoResolve (EmitContext ec)
3097 // This should never be invoked, we are born in fully
3098 // initialized state.
3103 public override void Emit (EmitContext ec)
3107 ec.ig.Emit (OpCodes.Castclass, type);
3113 /// SimpleName expressions are initially formed of a single
3114 /// word and it only happens at the beginning of the expression.
3118 /// The expression will try to be bound to a Field, a Method
3119 /// group or a Property. If those fail we pass the name to our
3120 /// caller and the SimpleName is compounded to perform a type
3121 /// lookup. The idea behind this process is that we want to avoid
3122 /// creating a namespace map from the assemblies, as that requires
3123 /// the GetExportedTypes function to be called and a hashtable to
3124 /// be constructed which reduces startup time. If later we find
3125 /// that this is slower, we should create a `NamespaceExpr' expression
3126 /// that fully participates in the resolution process.
3128 /// For example `System.Console.WriteLine' is decomposed into
3129 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3131 /// The first SimpleName wont produce a match on its own, so it will
3133 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3135 /// System.Console will produce a TypeExpr match.
3137 /// The downside of this is that we might be hitting `LookupType' too many
3138 /// times with this scheme.
3140 public class SimpleName : Expression {
3141 public readonly string Name;
3142 public readonly Location Location;
3144 public SimpleName (string name, Location l)
3150 public static void Error_ObjectRefRequired (Location l, string name)
3154 "An object reference is required " +
3155 "for the non-static field `"+name+"'");
3159 // Checks whether we are trying to access an instance
3160 // property, method or field from a static body.
3162 Expression MemberStaticCheck (Expression e)
3164 if (e is FieldExpr){
3165 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3168 Error_ObjectRefRequired (Location, Name);
3171 } else if (e is MethodGroupExpr){
3172 MethodGroupExpr mg = (MethodGroupExpr) e;
3174 if (!mg.RemoveInstanceMethods ()){
3175 Error_ObjectRefRequired (Location, mg.Methods [0].Name);
3179 } else if (e is PropertyExpr){
3180 if (!((PropertyExpr) e).IsStatic){
3181 Error_ObjectRefRequired (Location, Name);
3184 } else if (e is EventExpr) {
3185 if (!((EventExpr) e).IsStatic) {
3186 Error_ObjectRefRequired (Location, Name);
3194 public override Expression DoResolve (EmitContext ec)
3196 return SimpleNameResolve (ec, null, false);
3199 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3201 return SimpleNameResolve (ec, right_side, false);
3205 public Expression DoResolveAllowStatic (EmitContext ec)
3207 return SimpleNameResolve (ec, null, true);
3211 /// 7.5.2: Simple Names.
3213 /// Local Variables and Parameters are handled at
3214 /// parse time, so they never occur as SimpleNames.
3216 /// The `allow_static' flag is used by MemberAccess only
3217 /// and it is used to inform us that it is ok for us to
3218 /// avoid the static check, because MemberAccess might end
3219 /// up resolving the Name as a Type name and the access as
3220 /// a static type access.
3222 /// ie: Type Type; .... { Type.GetType (""); }
3224 /// Type is both an instance variable and a Type; Type.GetType
3225 /// is the static method not an instance method of type.
3227 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3229 Expression e = null;
3232 // Stage 1: Performed by the parser (binding to locals or parameters).
3234 if (!ec.OnlyLookupTypes){
3235 Block current_block = ec.CurrentBlock;
3236 if (current_block != null && current_block.IsVariableDefined (Name)){
3237 LocalVariableReference var;
3239 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3241 if (right_side != null)
3242 return var.ResolveLValue (ec, right_side);
3244 return var.Resolve (ec);
3248 // Stage 2: Lookup members
3252 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3253 // Hence we have two different cases
3256 DeclSpace lookup_ds = ec.DeclSpace;
3258 if (lookup_ds.TypeBuilder == null)
3261 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
3266 // Classes/structs keep looking, enums break
3268 if (lookup_ds is TypeContainer)
3269 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3272 } while (lookup_ds != null);
3274 if (e == null && ec.ContainerType != null)
3275 e = MemberLookup (ec, ec.ContainerType, Name, Location);
3278 // Continuation of stage 2
3281 // Stage 3: Lookup symbol in the various namespaces.
3283 DeclSpace ds = ec.DeclSpace;
3287 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3288 return new TypeExpr (t);
3291 // Stage 2 part b: Lookup up if we are an alias to a type
3294 // Since we are cheating: we only do the Alias lookup for
3295 // namespaces if the name does not include any dots in it
3298 alias_value = ec.DeclSpace.LookupAlias (Name);
3300 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3301 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3303 return new TypeExpr (t);
3305 // we have alias value, but it isn't Type, so try if it's namespace
3306 return new SimpleName (alias_value, Location);
3309 if (ec.ResolvingTypeTree){
3310 Type dt = ec.DeclSpace.FindType (Name);
3312 return new TypeExpr (dt);
3315 // No match, maybe our parent can compose us
3316 // into something meaningful.
3321 // Stage 2 continues here.
3326 if (ec.OnlyLookupTypes)
3329 if (e is FieldExpr){
3330 FieldExpr fe = (FieldExpr) e;
3331 FieldInfo fi = fe.FieldInfo;
3333 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3334 UnsafeError (Location);
3338 if (!allow_static && !fi.IsStatic){
3339 Error_ObjectRefRequired (Location, Name);
3343 // If we are not in static code and this
3344 // field is not static, set the instance to `this'.
3347 fe.InstanceExpression = ec.This;
3351 if (fi is FieldBuilder) {
3352 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3355 object o = c.LookupConstantValue (ec);
3356 object real_value = ((Constant)c.Expr).GetValue ();
3357 return Constantify (real_value, fi.FieldType);
3362 Type t = fi.FieldType;
3363 Type decl_type = fi.DeclaringType;
3366 if (fi is FieldBuilder)
3367 o = TypeManager.GetValue ((FieldBuilder) fi);
3369 o = fi.GetValue (fi);
3371 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3372 Expression enum_member = MemberLookup (
3373 ec, decl_type, "value__", MemberTypes.Field,
3374 AllBindingFlags, Location);
3376 Enum en = TypeManager.LookupEnum (decl_type);
3380 c = Constantify (o, en.UnderlyingType);
3382 c = Constantify (o, enum_member.Type);
3384 return new EnumConstant (c, decl_type);
3387 Expression exp = Constantify (o, t);
3393 if (e is PropertyExpr) {
3394 PropertyExpr pe = (PropertyExpr) e;
3400 return MemberStaticCheck (e);
3402 // If we are not in static code and this
3403 // field is not static, set the instance to `this'.
3406 pe.InstanceExpression = ec.This;
3412 if (e is EventExpr) {
3414 // If the event is local to this class, we transform ourselves into
3417 EventExpr ee = (EventExpr) e;
3419 Expression ml = MemberLookup (
3420 ec, ec.ContainerType, ee.EventInfo.Name,
3421 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, Location);
3424 MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
3428 // If this happens, then we have an event with its own
3429 // accessors and private field etc so there's no need
3430 // to transform ourselves : we should instead flag an error
3432 Assign.error70 (ee.EventInfo, Location);
3436 ml = ExprClassFromMemberInfo (ec, mi, Location);
3439 Report.Error (-200, Location, "Internal error!!");
3443 Expression instance_expr;
3445 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3448 instance_expr = null;
3450 instance_expr = ec.This;
3451 instance_expr = instance_expr.Resolve (ec);
3454 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3463 return MemberStaticCheck (e);
3470 public override void Emit (EmitContext ec)
3473 // If this is ever reached, then we failed to
3474 // find the name as a namespace
3477 Error (103, Location, "The name `" + Name +
3478 "' does not exist in the class `" +
3479 ec.DeclSpace.Name + "'");
3482 public override string ToString ()
3489 /// Fully resolved expression that evaluates to a type
3491 public class TypeExpr : Expression {
3492 public TypeExpr (Type t)
3495 eclass = ExprClass.Type;
3498 override public Expression DoResolve (EmitContext ec)
3503 override public void Emit (EmitContext ec)
3505 throw new Exception ("Should never be called");
3510 /// Used to create types from a fully qualified name. These are just used
3511 /// by the parser to setup the core types. A TypeExpression is always
3512 /// classified as a type.
3514 public class TypeExpression : TypeExpr {
3517 public TypeExpression (string name) : base (null)
3522 public override Expression DoResolve (EmitContext ec)
3525 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3529 public override void Emit (EmitContext ec)
3531 throw new Exception ("Should never be called");
3534 public override string ToString ()
3541 /// MethodGroup Expression.
3543 /// This is a fully resolved expression that evaluates to a type
3545 public class MethodGroupExpr : Expression {
3546 public MethodBase [] Methods;
3548 Expression instance_expression = null;
3550 public MethodGroupExpr (MemberInfo [] mi, Location l)
3552 Methods = new MethodBase [mi.Length];
3553 mi.CopyTo (Methods, 0);
3554 eclass = ExprClass.MethodGroup;
3555 type = TypeManager.object_type;
3559 public MethodGroupExpr (ArrayList list, Location l)
3561 Methods = new MethodBase [list.Count];
3564 list.CopyTo (Methods, 0);
3566 foreach (MemberInfo m in list){
3567 if (!(m is MethodBase)){
3568 Console.WriteLine ("Name " + m.Name);
3569 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3575 eclass = ExprClass.MethodGroup;
3576 type = TypeManager.object_type;
3580 // `A method group may have associated an instance expression'
3582 public Expression InstanceExpression {
3584 return instance_expression;
3588 instance_expression = value;
3592 override public Expression DoResolve (EmitContext ec)
3597 public void ReportUsageError ()
3599 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3600 Methods [0].Name + "()' is referenced without parentheses");
3603 override public void Emit (EmitContext ec)
3605 ReportUsageError ();
3608 bool RemoveMethods (bool keep_static)
3610 ArrayList smethods = new ArrayList ();
3612 foreach (MethodBase mb in Methods){
3613 if (mb.IsStatic == keep_static)
3617 if (smethods.Count == 0)
3620 Methods = new MethodBase [smethods.Count];
3621 smethods.CopyTo (Methods, 0);
3627 /// Removes any instance methods from the MethodGroup, returns
3628 /// false if the resulting set is empty.
3630 public bool RemoveInstanceMethods ()
3632 return RemoveMethods (true);
3636 /// Removes any static methods from the MethodGroup, returns
3637 /// false if the resulting set is empty.
3639 public bool RemoveStaticMethods ()
3641 return RemoveMethods (false);
3646 /// Fully resolved expression that evaluates to a Field
3648 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3649 public readonly FieldInfo FieldInfo;
3650 public Expression InstanceExpression;
3653 public FieldExpr (FieldInfo fi, Location l)
3656 eclass = ExprClass.Variable;
3657 type = fi.FieldType;
3661 override public Expression DoResolve (EmitContext ec)
3663 if (!FieldInfo.IsStatic){
3664 if (InstanceExpression == null){
3665 throw new Exception ("non-static FieldExpr without instance var\n" +
3666 "You have to assign the Instance variable\n" +
3667 "Of the FieldExpr to set this\n");
3670 InstanceExpression = InstanceExpression.Resolve (ec);
3671 if (InstanceExpression == null)
3678 void Report_AssignToReadonly (bool is_instance)
3683 msg = "Readonly field can not be assigned outside " +
3684 "of constructor or variable initializer";
3686 msg = "A static readonly field can only be assigned in " +
3687 "a static constructor";
3689 Report.Error (is_instance ? 191 : 198, loc, msg);
3692 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3694 Expression e = DoResolve (ec);
3699 if (!FieldInfo.IsInitOnly)
3703 // InitOnly fields can only be assigned in constructors
3706 if (ec.IsConstructor)
3709 Report_AssignToReadonly (true);
3714 override public void Emit (EmitContext ec)
3716 ILGenerator ig = ec.ig;
3717 bool is_volatile = false;
3719 if (FieldInfo is FieldBuilder){
3720 FieldBase f = TypeManager.GetField (FieldInfo);
3722 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3725 f.status |= Field.Status.USED;
3728 if (FieldInfo.IsStatic){
3730 ig.Emit (OpCodes.Volatile);
3732 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3734 if (InstanceExpression.Type.IsValueType){
3736 LocalTemporary tempo = null;
3738 if (!(InstanceExpression is IMemoryLocation)){
3739 tempo = new LocalTemporary (
3740 ec, InstanceExpression.Type);
3742 InstanceExpression.Emit (ec);
3746 ml = (IMemoryLocation) InstanceExpression;
3748 ml.AddressOf (ec, AddressOp.Load);
3750 InstanceExpression.Emit (ec);
3753 ig.Emit (OpCodes.Volatile);
3755 ig.Emit (OpCodes.Ldfld, FieldInfo);
3759 public void EmitAssign (EmitContext ec, Expression source)
3761 FieldAttributes fa = FieldInfo.Attributes;
3762 bool is_static = (fa & FieldAttributes.Static) != 0;
3763 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3764 ILGenerator ig = ec.ig;
3766 if (is_readonly && !ec.IsConstructor){
3767 Report_AssignToReadonly (!is_static);
3772 Expression instance = InstanceExpression;
3774 if (instance.Type.IsValueType){
3775 if (instance is IMemoryLocation){
3776 IMemoryLocation ml = (IMemoryLocation) instance;
3778 ml.AddressOf (ec, AddressOp.Store);
3780 throw new Exception ("The " + instance + " of type " +
3782 " represents a ValueType and does " +
3783 "not implement IMemoryLocation");
3789 if (FieldInfo is FieldBuilder){
3790 FieldBase f = TypeManager.GetField (FieldInfo);
3792 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3793 ig.Emit (OpCodes.Volatile);
3797 ig.Emit (OpCodes.Stsfld, FieldInfo);
3799 ig.Emit (OpCodes.Stfld, FieldInfo);
3801 if (FieldInfo is FieldBuilder){
3802 FieldBase f = TypeManager.GetField (FieldInfo);
3804 f.status |= Field.Status.ASSIGNED;
3808 public void AddressOf (EmitContext ec, AddressOp mode)
3810 ILGenerator ig = ec.ig;
3812 if (FieldInfo is FieldBuilder){
3813 FieldBase f = TypeManager.GetField (FieldInfo);
3814 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3815 ig.Emit (OpCodes.Volatile);
3818 if (FieldInfo is FieldBuilder){
3819 FieldBase f = TypeManager.GetField (FieldInfo);
3821 if ((mode & AddressOp.Store) != 0)
3822 f.status |= Field.Status.ASSIGNED;
3823 if ((mode & AddressOp.Load) != 0)
3824 f.status |= Field.Status.USED;
3828 // Handle initonly fields specially: make a copy and then
3829 // get the address of the copy.
3831 if (FieldInfo.IsInitOnly){
3832 if (ec.IsConstructor) {
3833 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3838 local = ig.DeclareLocal (type);
3839 ig.Emit (OpCodes.Stloc, local);
3840 ig.Emit (OpCodes.Ldloca, local);
3845 if (FieldInfo.IsStatic)
3846 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3848 if (InstanceExpression is IMemoryLocation)
3849 ((IMemoryLocation)InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
3851 InstanceExpression.Emit (ec);
3852 ig.Emit (OpCodes.Ldflda, FieldInfo);
3858 /// Expression that evaluates to a Property. The Assign class
3859 /// might set the `Value' expression if we are in an assignment.
3861 /// This is not an LValue because we need to re-write the expression, we
3862 /// can not take data from the stack and store it.
3864 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3865 public readonly PropertyInfo PropertyInfo;
3866 public readonly bool IsStatic;
3868 MethodInfo [] Accessors;
3871 Expression instance_expr;
3873 public PropertyExpr (PropertyInfo pi, Location l)
3876 eclass = ExprClass.PropertyAccess;
3879 Accessors = TypeManager.GetAccessors (pi);
3881 if (Accessors != null)
3882 foreach (MethodInfo mi in Accessors){
3888 Accessors = new MethodInfo [2];
3890 type = TypeManager.TypeToCoreType (pi.PropertyType);
3894 // The instance expression associated with this expression
3896 public Expression InstanceExpression {
3898 instance_expr = value;
3902 return instance_expr;
3906 public bool VerifyAssignable ()
3908 if (!PropertyInfo.CanWrite){
3909 Report.Error (200, loc,
3910 "The property `" + PropertyInfo.Name +
3911 "' can not be assigned to, as it has not set accessor");
3918 override public Expression DoResolve (EmitContext ec)
3920 if (!PropertyInfo.CanRead){
3921 Report.Error (154, loc,
3922 "The property `" + PropertyInfo.Name +
3923 "' can not be used in " +
3924 "this context because it lacks a get accessor");
3928 type = PropertyInfo.PropertyType;
3933 override public void Emit (EmitContext ec)
3935 MethodInfo method = Accessors [0];
3938 // Special case: length of single dimension array is turned into ldlen
3940 if (method == TypeManager.int_array_get_length){
3941 Type iet = instance_expr.Type;
3944 // System.Array.Length can be called, but the Type does not
3945 // support invoking GetArrayRank, so test for that case first
3947 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3948 instance_expr.Emit (ec);
3949 ec.ig.Emit (OpCodes.Ldlen);
3954 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
3959 // Implements the IAssignMethod interface for assignments
3961 public void EmitAssign (EmitContext ec, Expression source)
3963 Argument arg = new Argument (source, Argument.AType.Expression);
3964 ArrayList args = new ArrayList ();
3967 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
3970 override public void EmitStatement (EmitContext ec)
3973 ec.ig.Emit (OpCodes.Pop);
3978 /// Fully resolved expression that evaluates to an Event
3980 public class EventExpr : Expression {
3981 public readonly EventInfo EventInfo;
3983 public Expression InstanceExpression;
3985 public readonly bool IsStatic;
3987 MethodInfo add_accessor, remove_accessor;
3989 public EventExpr (EventInfo ei, Location loc)
3993 eclass = ExprClass.EventAccess;
3995 add_accessor = TypeManager.GetAddMethod (ei);
3996 remove_accessor = TypeManager.GetRemoveMethod (ei);
3998 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4001 if (EventInfo is MyEventBuilder)
4002 type = ((MyEventBuilder) EventInfo).EventType;
4004 type = EventInfo.EventHandlerType;
4007 public override Expression DoResolve (EmitContext ec)
4009 // We are born fully resolved
4013 public override void Emit (EmitContext ec)
4015 throw new Exception ("Should not happen I think");
4018 public void EmitAddOrRemove (EmitContext ec, Expression source)
4020 Expression handler = ((Binary) source).Right;
4022 Argument arg = new Argument (handler, Argument.AType.Expression);
4023 ArrayList args = new ArrayList ();
4027 if (((Binary) source).Oper == Binary.Operator.Addition)
4028 Invocation.EmitCall (
4029 ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
4031 Invocation.EmitCall (
4032 ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);