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 interface is implemented by variables
42 public interface IMemoryLocation {
44 /// The AddressOf method should generate code that loads
45 /// the address of the object and leaves it on the stack
47 void AddressOf (EmitContext ec);
51 /// Base class for expressions
53 public abstract class Expression {
54 protected ExprClass eclass;
67 public ExprClass ExprClass {
78 /// Utility wrapper routine for Error, just to beautify the code
80 static protected void Error (int error, string s)
82 Report.Error (error, s);
85 static protected void Error (int error, Location loc, string s)
87 Report.Error (error, loc, s);
91 /// Utility wrapper routine for Warning, just to beautify the code
93 static protected void Warning (int warning, string s)
95 Report.Warning (warning, s);
98 static public void error30 (Location loc, Type source, Type target)
100 Report.Error (30, loc, "Cannot convert type '" +
101 TypeManager.CSharpName (source) + "' to '" +
102 TypeManager.CSharpName (target) + "'");
106 /// Performs semantic analysis on the Expression
110 /// The Resolve method is invoked to perform the semantic analysis
113 /// The return value is an expression (it can be the
114 /// same expression in some cases) or a new
115 /// expression that better represents this node.
117 /// For example, optimizations of Unary (LiteralInt)
118 /// would return a new LiteralInt with a negated
121 /// If there is an error during semantic analysis,
122 /// then an error should be reported (using Report)
123 /// and a null value should be returned.
125 /// There are two side effects expected from calling
126 /// Resolve(): the the field variable "eclass" should
127 /// be set to any value of the enumeration
128 /// `ExprClass' and the type variable should be set
129 /// to a valid type (this is the type of the
132 public abstract Expression DoResolve (EmitContext ec);
134 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
136 return DoResolve (ec);
140 /// Resolves an expression and performs semantic analysis on it.
144 /// Currently Resolve wraps DoResolve to perform sanity
145 /// checking and assertion checking on what we expect from Resolve.
147 public Expression Resolve (EmitContext ec)
149 Expression e = DoResolve (ec);
152 if (e is SimpleName){
153 SimpleName s = (SimpleName) e;
157 "The name `" + s.Name + "' could not be found in `" +
158 ec.TypeContainer.Name + "'");
162 if (e.ExprClass == ExprClass.Invalid)
163 throw new Exception ("Expression " + e +
164 " ExprClass is Invalid after resolve");
166 if (e.ExprClass != ExprClass.MethodGroup)
168 throw new Exception ("Expression " + e +
169 " did not set its type after Resolve");
176 /// Performs expression resolution and semantic analysis, but
177 /// allows SimpleNames to be returned.
181 /// This is used by MemberAccess to construct long names that can not be
182 /// partially resolved (namespace-qualified names for example).
184 public Expression ResolveWithSimpleName (EmitContext ec)
186 Expression e = DoResolve (ec);
192 if (e.ExprClass == ExprClass.Invalid)
193 throw new Exception ("Expression " + e +
194 " ExprClass is Invalid after resolve");
196 if (e.ExprClass != ExprClass.MethodGroup)
198 throw new Exception ("Expression " + e +
199 " did not set its type after Resolve");
206 /// Resolves an expression for LValue assignment
210 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
211 /// checking and assertion checking on what we expect from Resolve
213 public Expression ResolveLValue (EmitContext ec, Expression right_side)
215 Expression e = DoResolveLValue (ec, right_side);
218 if (e is SimpleName){
219 SimpleName s = (SimpleName) e;
223 "The name `" + s.Name + "' could not be found in `" +
224 ec.TypeContainer.Name + "'");
228 if (e.ExprClass == ExprClass.Invalid)
229 throw new Exception ("Expression " + e +
230 " ExprClass is Invalid after resolve");
232 if (e.ExprClass != ExprClass.MethodGroup)
234 throw new Exception ("Expression " + e +
235 " did not set its type after Resolve");
242 /// Emits the code for the expression
246 /// The Emit method is invoked to generate the code
247 /// for the expression.
249 public abstract void Emit (EmitContext ec);
252 /// This method should perform a reduction of the expression. This should
253 /// never return null.
255 public virtual Expression Reduce (EmitContext ec)
261 /// Protected constructor. Only derivate types should
262 /// be able to be created
265 protected Expression ()
267 eclass = ExprClass.Invalid;
272 /// Returns a literalized version of a literal FieldInfo
276 /// The possible return values are:
277 /// IntLiteral, UIntLiteral
278 /// LongLiteral, ULongLiteral
279 /// FloatLiteral, DoubleLiteral
282 public static Expression Literalize (object v, Type t)
284 if (t == TypeManager.int32_type)
285 return new IntLiteral ((int) v);
286 else if (t == TypeManager.uint32_type)
287 return new UIntLiteral ((uint) v);
288 else if (t == TypeManager.int64_type)
289 return new LongLiteral ((long) v);
290 else if (t == TypeManager.uint64_type)
291 return new ULongLiteral ((ulong) v);
292 else if (t == TypeManager.float_type)
293 return new FloatLiteral ((float) v);
294 else if (t == TypeManager.double_type)
295 return new DoubleLiteral ((double) v);
296 else if (t == TypeManager.string_type)
297 return new StringLiteral ((string) v);
298 else if (t == TypeManager.short_type)
299 return new IntLiteral ((int) ((short)v));
300 else if (t == TypeManager.ushort_type)
301 return new IntLiteral ((int) ((ushort)v));
302 else if (t == TypeManager.sbyte_type)
303 return new IntLiteral ((int) ((sbyte)v));
304 else if (t == TypeManager.byte_type)
305 return new IntLiteral ((int) ((byte)v));
306 else if (t == TypeManager.char_type)
307 return new IntLiteral ((int) ((char)v));
309 throw new Exception ("Unknown type for literal (" + t +
314 /// Returns a fully formed expression after a MemberLookup
316 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
319 return new EventExpr ((EventInfo) mi, loc);
320 else if (mi is FieldInfo)
321 return new FieldExpr ((FieldInfo) mi, loc);
322 else if (mi is PropertyInfo)
323 return new PropertyExpr ((PropertyInfo) mi, loc);
324 else if (mi is Type){
325 return new TypeExpr ((Type) mi);
332 // We copy methods from `new_members' into `target_list' if the signature
333 // for the method from in the new list does not exist in the target_list
335 // The name is assumed to be the same.
337 static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
339 if (target_list == null){
340 target_list = new ArrayList ();
342 target_list.AddRange (new_members);
346 MemberInfo [] target_array = new MemberInfo [target_list.Count];
347 target_list.CopyTo (target_array, 0);
349 foreach (MemberInfo mi in new_members){
350 MethodBase new_method = (MethodBase) mi;
351 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
353 foreach (MethodBase method in target_array){
354 Type [] old_args = TypeManager.GetArgumentTypes (method);
355 int new_count = new_args.Length;
356 int old_count = old_args.Length;
358 if (new_count != old_count){
359 target_list.Add (method);
363 for (int i = 0; i < old_count; i++){
364 if (old_args [i] == new_args [i])
366 target_list.Add (method);
375 // FIXME: Probably implement a cache for (t,name,current_access_set)?
377 // FIXME: We need to cope with access permissions here, or this wont
380 // This code could use some optimizations, but we need to do some
381 // measurements. For example, we could use a delegate to `flag' when
382 // something can not any longer be a method-group (because it is something
386 // If the return value is an Array, then it is an array of
389 // If the return value is an MemberInfo, it is anything, but a Method
393 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
394 // the arguments here and have MemberLookup return only the methods that
395 // match the argument count/type, unlike we are doing now (we delay this
398 // This is so we can catch correctly attempts to invoke instance methods
399 // from a static body (scan for error 120 in ResolveSimpleName).
402 // FIXME: Potential optimization, have a static ArrayList
404 public static Expression MemberLookup (EmitContext ec, Type t, string name,
405 bool same_type, MemberTypes mt,
406 BindingFlags bf, Location loc)
409 bf |= BindingFlags.NonPublic;
412 // Lookup for members starting in the type requested and going
413 // up the hierarchy until a match is found.
415 // As soon as a non-method match is found, we return.
417 // If methods are found though, then the search proceeds scanning
418 // for more public methods in the hierarchy with signatures that
419 // do not match any of the signatures found so far.
421 ArrayList method_list = null;
422 Type current_type = t;
423 bool searching = true;
427 mi = RootContext.TypeManager.FindMembers (
428 current_type, mt, bf | BindingFlags.DeclaredOnly,
429 System.Type.FilterName, name);
431 if (current_type == TypeManager.object_type)
434 current_type = current_type.BaseType;
437 // This happens with interfaces, they have a null
440 if (current_type == null)
447 int count = mi.Length;
452 if (count == 1 && !(mi [0] is MethodBase))
453 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
456 // We found methods, turn the search into "method scan"
459 method_list = CopyNewMethods (method_list, mi);
460 mt &= (MemberTypes.Method | MemberTypes.Constructor);
463 if (method_list != null && method_list.Count > 0)
464 return new MethodGroupExpr (method_list);
469 public const MemberTypes AllMemberTypes =
470 MemberTypes.Constructor |
474 MemberTypes.NestedType |
475 MemberTypes.Property;
477 public const BindingFlags AllBindingsFlags =
478 BindingFlags.Public |
479 BindingFlags.Static |
480 BindingFlags.Instance;
482 public static Expression MemberLookup (EmitContext ec, Type t, string name,
483 bool same_type, Location loc)
485 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
488 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
490 Type expr_type = expr.Type;
492 if (target_type == TypeManager.object_type) {
493 if (expr_type.IsClass)
494 return new EmptyCast (expr, target_type);
495 if (expr_type.IsValueType)
496 return new BoxedCast (expr);
497 } else if (expr_type.IsSubclassOf (target_type)) {
498 return new EmptyCast (expr, target_type);
500 // from any class-type S to any interface-type T.
501 if (expr_type.IsClass && target_type.IsInterface) {
502 if (TypeManager.ImplementsInterface (expr_type, target_type))
503 return new EmptyCast (expr, target_type);
508 // from any interface type S to interface-type T.
509 if (expr_type.IsInterface && target_type.IsInterface) {
511 if (TypeManager.ImplementsInterface (expr_type, target_type))
512 return new EmptyCast (expr, target_type);
517 // from an array-type S to an array-type of type T
518 if (expr_type.IsArray && target_type.IsArray) {
519 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
521 Type expr_element_type = expr_type.GetElementType ();
522 Type target_element_type = target_type.GetElementType ();
524 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
525 if (StandardConversionExists (expr_element_type,
526 target_element_type))
527 return new EmptyCast (expr, target_type);
532 // from an array-type to System.Array
533 if (expr_type.IsArray && target_type == TypeManager.array_type)
534 return new EmptyCast (expr, target_type);
536 // from any delegate type to System.Delegate
537 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
538 target_type == TypeManager.delegate_type)
539 return new EmptyCast (expr, target_type);
541 // from any array-type or delegate type into System.ICloneable.
542 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
543 if (target_type == TypeManager.icloneable_type)
544 return new EmptyCast (expr, target_type);
546 // from the null type to any reference-type.
547 if (expr is NullLiteral)
548 return new EmptyCast (expr, target_type);
558 /// Handles expressions like this: decimal d; d = 1;
559 /// and changes them into: decimal d; d = new System.Decimal (1);
561 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
563 ArrayList args = new ArrayList ();
565 args.Add (new Argument (expr, Argument.AType.Expression));
567 Expression ne = new New (target.FullName, args,
570 return ne.Resolve (ec);
574 /// Implicit Numeric Conversions.
576 /// expr is the expression to convert, returns a new expression of type
577 /// target_type or null if an implicit conversion is not possible.
579 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
580 Type target_type, Location loc)
582 Type expr_type = expr.Type;
585 // Attempt to do the implicit constant expression conversions
587 if (expr is IntLiteral){
590 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
594 } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
596 // Try the implicit constant expression conversion
597 // from long to ulong, instead of a nice routine,
600 if (((LongLiteral) expr).Value > 0)
601 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
604 if (expr_type == TypeManager.sbyte_type){
606 // From sbyte to short, int, long, float, double.
608 if (target_type == TypeManager.int32_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
610 if (target_type == TypeManager.int64_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
612 if (target_type == TypeManager.double_type)
613 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
614 if (target_type == TypeManager.float_type)
615 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
616 if (target_type == TypeManager.short_type)
617 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
618 if (target_type == TypeManager.decimal_type)
619 return InternalTypeConstructor (ec, expr, target_type);
620 } else if (expr_type == TypeManager.byte_type){
622 // From byte to short, ushort, int, uint, long, ulong, float, double
624 if ((target_type == TypeManager.short_type) ||
625 (target_type == TypeManager.ushort_type) ||
626 (target_type == TypeManager.int32_type) ||
627 (target_type == TypeManager.uint32_type))
628 return new EmptyCast (expr, target_type);
630 if (target_type == TypeManager.uint64_type)
631 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
632 if (target_type == TypeManager.int64_type)
633 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
634 if (target_type == TypeManager.float_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
636 if (target_type == TypeManager.double_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
638 if (target_type == TypeManager.decimal_type)
639 return InternalTypeConstructor (ec, expr, target_type);
640 } else if (expr_type == TypeManager.short_type){
642 // From short to int, long, float, double
644 if (target_type == TypeManager.int32_type)
645 return new EmptyCast (expr, target_type);
646 if (target_type == TypeManager.int64_type)
647 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
648 if (target_type == TypeManager.double_type)
649 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
650 if (target_type == TypeManager.float_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
652 if (target_type == TypeManager.decimal_type)
653 return InternalTypeConstructor (ec, expr, target_type);
654 } else if (expr_type == TypeManager.ushort_type){
656 // From ushort to int, uint, long, ulong, float, double
658 if (target_type == TypeManager.uint32_type)
659 return new EmptyCast (expr, target_type);
661 if (target_type == TypeManager.uint64_type)
662 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
663 if (target_type == TypeManager.int32_type)
664 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
665 if (target_type == TypeManager.int64_type)
666 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
667 if (target_type == TypeManager.double_type)
668 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
669 if (target_type == TypeManager.float_type)
670 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
671 if (target_type == TypeManager.decimal_type)
672 return InternalTypeConstructor (ec, expr, target_type);
673 } else if (expr_type == TypeManager.int32_type){
675 // From int to long, float, double
677 if (target_type == TypeManager.int64_type)
678 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
679 if (target_type == TypeManager.double_type)
680 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
681 if (target_type == TypeManager.float_type)
682 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
683 if (target_type == TypeManager.decimal_type)
684 return InternalTypeConstructor (ec, expr, target_type);
685 } else if (expr_type == TypeManager.uint32_type){
687 // From uint to long, ulong, float, double
689 if (target_type == TypeManager.int64_type)
690 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
691 if (target_type == TypeManager.uint64_type)
692 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
693 if (target_type == TypeManager.double_type)
694 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
696 if (target_type == TypeManager.float_type)
697 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
699 if (target_type == TypeManager.decimal_type)
700 return InternalTypeConstructor (ec, expr, target_type);
701 } else if ((expr_type == TypeManager.uint64_type) ||
702 (expr_type == TypeManager.int64_type)){
704 // From long/ulong to float, double
706 if (target_type == TypeManager.double_type)
707 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
709 if (target_type == TypeManager.float_type)
710 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
712 if (target_type == TypeManager.decimal_type)
713 return InternalTypeConstructor (ec, expr, target_type);
714 } else if (expr_type == TypeManager.char_type){
716 // From char to ushort, int, uint, long, ulong, float, double
718 if ((target_type == TypeManager.ushort_type) ||
719 (target_type == TypeManager.int32_type) ||
720 (target_type == TypeManager.uint32_type))
721 return new EmptyCast (expr, target_type);
722 if (target_type == TypeManager.uint64_type)
723 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
724 if (target_type == TypeManager.int64_type)
725 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
726 if (target_type == TypeManager.float_type)
727 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
728 if (target_type == TypeManager.double_type)
729 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
730 if (target_type == TypeManager.decimal_type)
731 return InternalTypeConstructor (ec, expr, target_type);
732 } else if (expr_type == TypeManager.float_type){
736 if (target_type == TypeManager.double_type)
737 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
744 /// Determines if a standard implicit conversion exists from
745 /// expr_type to target_type
747 public static bool StandardConversionExists (Type expr_type, Type target_type)
749 if (expr_type == target_type)
752 // First numeric conversions
754 if (expr_type == TypeManager.sbyte_type){
756 // From sbyte to short, int, long, float, double.
758 if ((target_type == TypeManager.int32_type) ||
759 (target_type == TypeManager.int64_type) ||
760 (target_type == TypeManager.double_type) ||
761 (target_type == TypeManager.float_type) ||
762 (target_type == TypeManager.short_type) ||
763 (target_type == TypeManager.decimal_type))
766 } else if (expr_type == TypeManager.byte_type){
768 // From byte to short, ushort, int, uint, long, ulong, float, double
770 if ((target_type == TypeManager.short_type) ||
771 (target_type == TypeManager.ushort_type) ||
772 (target_type == TypeManager.int32_type) ||
773 (target_type == TypeManager.uint32_type) ||
774 (target_type == TypeManager.uint64_type) ||
775 (target_type == TypeManager.int64_type) ||
776 (target_type == TypeManager.float_type) ||
777 (target_type == TypeManager.double_type) ||
778 (target_type == TypeManager.decimal_type))
781 } else if (expr_type == TypeManager.short_type){
783 // From short to int, long, float, double
785 if ((target_type == TypeManager.int32_type) ||
786 (target_type == TypeManager.int64_type) ||
787 (target_type == TypeManager.double_type) ||
788 (target_type == TypeManager.float_type) ||
789 (target_type == TypeManager.decimal_type))
792 } else if (expr_type == TypeManager.ushort_type){
794 // From ushort to int, uint, long, ulong, float, double
796 if ((target_type == TypeManager.uint32_type) ||
797 (target_type == TypeManager.uint64_type) ||
798 (target_type == TypeManager.int32_type) ||
799 (target_type == TypeManager.int64_type) ||
800 (target_type == TypeManager.double_type) ||
801 (target_type == TypeManager.float_type) ||
802 (target_type == TypeManager.decimal_type))
805 } else if (expr_type == TypeManager.int32_type){
807 // From int to long, float, double
809 if ((target_type == TypeManager.int64_type) ||
810 (target_type == TypeManager.double_type) ||
811 (target_type == TypeManager.float_type) ||
812 (target_type == TypeManager.decimal_type))
815 } else if (expr_type == TypeManager.uint32_type){
817 // From uint to long, ulong, float, double
819 if ((target_type == TypeManager.int64_type) ||
820 (target_type == TypeManager.uint64_type) ||
821 (target_type == TypeManager.double_type) ||
822 (target_type == TypeManager.float_type) ||
823 (target_type == TypeManager.decimal_type))
826 } else if ((expr_type == TypeManager.uint64_type) ||
827 (expr_type == TypeManager.int64_type)) {
829 // From long/ulong to float, double
831 if ((target_type == TypeManager.double_type) ||
832 (target_type == TypeManager.float_type) ||
833 (target_type == TypeManager.decimal_type))
836 } else if (expr_type == TypeManager.char_type){
838 // From char to ushort, int, uint, long, ulong, float, double
840 if ((target_type == TypeManager.ushort_type) ||
841 (target_type == TypeManager.int32_type) ||
842 (target_type == TypeManager.uint32_type) ||
843 (target_type == TypeManager.uint64_type) ||
844 (target_type == TypeManager.int64_type) ||
845 (target_type == TypeManager.float_type) ||
846 (target_type == TypeManager.double_type) ||
847 (target_type == TypeManager.decimal_type))
850 } else if (expr_type == TypeManager.float_type){
854 if (target_type == TypeManager.double_type)
858 // Next reference conversions
860 if (target_type == TypeManager.object_type) {
861 if ((expr_type.IsClass) ||
862 (expr_type.IsValueType))
865 } else if (expr_type.IsSubclassOf (target_type)) {
869 // from any class-type S to any interface-type T.
870 if (expr_type.IsClass && target_type.IsInterface)
873 // from any interface type S to interface-type T.
874 // FIXME : Is it right to use IsAssignableFrom ?
875 if (expr_type.IsInterface && target_type.IsInterface)
876 if (target_type.IsAssignableFrom (expr_type))
879 // from an array-type S to an array-type of type T
880 if (expr_type.IsArray && target_type.IsArray) {
881 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
883 Type expr_element_type = expr_type.GetElementType ();
884 Type target_element_type = target_type.GetElementType ();
886 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
887 if (StandardConversionExists (expr_element_type,
888 target_element_type))
893 // from an array-type to System.Array
894 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
897 // from any delegate type to System.Delegate
898 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
899 target_type == TypeManager.delegate_type)
900 if (target_type.IsAssignableFrom (expr_type))
903 // from any array-type or delegate type into System.ICloneable.
904 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
905 if (target_type == TypeManager.icloneable_type)
908 // from the null type to any reference-type.
909 // FIXME : How do we do this ?
916 static EmptyExpression MyEmptyExpr;
918 /// Tells whether an implicit conversion exists from expr_type to
921 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
924 if (MyEmptyExpr == null)
925 MyEmptyExpr = new EmptyExpression (expr_type);
927 MyEmptyExpr.SetType (expr_type);
929 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
933 /// Finds "most encompassed type" according to the spec (13.4.2)
934 /// amongst the methods in the MethodGroupExpr which convert from a
935 /// type encompassing source_type
937 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
941 for (int i = me.Methods.Length; i > 0; ) {
944 MethodBase mb = me.Methods [i];
945 ParameterData pd = Invocation.GetParameterData (mb);
946 Type param_type = pd.ParameterType (0);
948 if (StandardConversionExists (source_type, param_type)) {
952 if (StandardConversionExists (param_type, best))
961 /// Finds "most encompassing type" according to the spec (13.4.2)
962 /// amongst the methods in the MethodGroupExpr which convert to a
963 /// type encompassed by target_type
965 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
969 for (int i = me.Methods.Length; i > 0; ) {
972 MethodInfo mi = (MethodInfo) me.Methods [i];
973 Type ret_type = mi.ReturnType;
975 if (StandardConversionExists (ret_type, target)) {
979 if (!StandardConversionExists (ret_type, best))
991 /// User-defined Implicit conversions
993 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
994 Type target, Location loc)
996 return UserDefinedConversion (ec, source, target, loc, false);
1000 /// User-defined Explicit conversions
1002 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1003 Type target, Location loc)
1005 return UserDefinedConversion (ec, source, target, loc, true);
1009 /// User-defined conversions
1011 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1012 Type target, Location loc,
1013 bool look_for_explicit)
1015 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1016 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1018 MethodBase method = null;
1019 Type source_type = source.Type;
1023 // If we have a boolean type, we need to check for the True operator
1025 // FIXME : How does the False operator come into the picture ?
1026 // FIXME : This doesn't look complete and very correct !
1027 if (target == TypeManager.bool_type)
1028 op_name = "op_True";
1030 op_name = "op_Implicit";
1032 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
1034 if (source_type.BaseType != null)
1035 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1037 mg3 = MemberLookup (ec, target, op_name, false, loc);
1039 if (target.BaseType != null)
1040 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1042 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1043 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1045 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1047 MethodGroupExpr union4 = null;
1049 if (look_for_explicit) {
1051 op_name = "op_Explicit";
1053 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
1055 if (source_type.BaseType != null)
1056 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1058 mg7 = MemberLookup (ec, target, op_name, false, loc);
1060 if (target.BaseType != null)
1061 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1063 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1064 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1066 union4 = Invocation.MakeUnionSet (union5, union6);
1069 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1071 if (union != null) {
1073 Type most_specific_source, most_specific_target;
1075 most_specific_source = FindMostEncompassedType (union, source_type);
1076 if (most_specific_source == null)
1079 most_specific_target = FindMostEncompassingType (union, target);
1080 if (most_specific_target == null)
1085 for (int i = union.Methods.Length; i > 0;) {
1088 MethodBase mb = union.Methods [i];
1089 ParameterData pd = Invocation.GetParameterData (mb);
1090 MethodInfo mi = (MethodInfo) union.Methods [i];
1092 if (pd.ParameterType (0) == most_specific_source &&
1093 mi.ReturnType == most_specific_target) {
1099 if (method == null || count > 1) {
1100 Report.Error (-11, loc, "Ambiguous user defined conversion");
1105 // This will do the conversion to the best match that we
1106 // found. Now we need to perform an implict standard conversion
1107 // if the best match was not the type that we were requested
1110 if (look_for_explicit)
1111 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1113 source = ConvertImplicitStandard (ec, source,
1114 most_specific_source, loc);
1119 e = new UserCast ((MethodInfo) method, source);
1121 if (e.Type != target){
1122 if (!look_for_explicit)
1123 e = ConvertImplicitStandard (ec, e, target, loc);
1125 e = ConvertExplicitStandard (ec, e, target, loc);
1136 /// Converts implicitly the resolved expression `expr' into the
1137 /// `target_type'. It returns a new expression that can be used
1138 /// in a context that expects a `target_type'.
1140 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1141 Type target_type, Location loc)
1143 Type expr_type = expr.Type;
1146 if (expr_type == target_type)
1149 if (target_type == null)
1150 throw new Exception ("Target type is null");
1152 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1156 e = ImplicitReferenceConversion (expr, target_type);
1160 e = ImplicitUserConversion (ec, expr, target_type, loc);
1164 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1165 IntLiteral i = (IntLiteral) expr;
1168 return new EmptyCast (expr, target_type);
1176 /// Attempts to apply the `Standard Implicit
1177 /// Conversion' rules to the expression `expr' into
1178 /// the `target_type'. It returns a new expression
1179 /// that can be used in a context that expects a
1182 /// This is different from `ConvertImplicit' in that the
1183 /// user defined implicit conversions are excluded.
1185 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1186 Type target_type, Location loc)
1188 Type expr_type = expr.Type;
1191 if (expr_type == target_type)
1194 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1198 e = ImplicitReferenceConversion (expr, target_type);
1202 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1203 IntLiteral i = (IntLiteral) expr;
1206 return new EmptyCast (expr, target_type);
1212 /// Attemps to perform an implict constant conversion of the IntLiteral
1213 /// into a different data type using casts (See Implicit Constant
1214 /// Expression Conversions)
1216 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1218 int value = il.Value;
1220 if (target_type == TypeManager.sbyte_type){
1221 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1222 return new EmptyCast (il, target_type);
1223 } else if (target_type == TypeManager.byte_type){
1224 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1225 return new EmptyCast (il, target_type);
1226 } else if (target_type == TypeManager.short_type){
1227 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1228 return new EmptyCast (il, target_type);
1229 } else if (target_type == TypeManager.ushort_type){
1230 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1231 return new EmptyCast (il, target_type);
1232 } else if (target_type == TypeManager.uint32_type){
1234 // we can optimize this case: a positive int32
1235 // always fits on a uint32
1238 return new EmptyCast (il, target_type);
1239 } else if (target_type == TypeManager.uint64_type){
1241 // we can optimize this case: a positive int32
1242 // always fits on a uint64. But we need an opcode
1246 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1253 /// Attemptes to implicityly convert `target' into `type', using
1254 /// ConvertImplicit. If there is no implicit conversion, then
1255 /// an error is signaled
1257 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1258 Type target_type, Location loc)
1262 e = ConvertImplicit (ec, source, target_type, loc);
1266 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1268 "Double literal cannot be implicitly converted to " +
1269 "float type, use F suffix to create a float literal");
1272 string msg = "Cannot convert implicitly from `"+
1273 TypeManager.CSharpName (source.Type) + "' to `" +
1274 TypeManager.CSharpName (target_type) + "'";
1276 Error (29, loc, msg);
1282 /// Performs the explicit numeric conversions
1284 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1287 Type expr_type = expr.Type;
1289 if (expr_type == TypeManager.sbyte_type){
1291 // From sbyte to byte, ushort, uint, ulong, char
1293 if (target_type == TypeManager.byte_type)
1294 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1295 if (target_type == TypeManager.ushort_type)
1296 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1297 if (target_type == TypeManager.uint32_type)
1298 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1299 if (target_type == TypeManager.uint64_type)
1300 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1301 if (target_type == TypeManager.char_type)
1302 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1303 } else if (expr_type == TypeManager.byte_type){
1305 // From byte to sbyte and char
1307 if (target_type == TypeManager.sbyte_type)
1308 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1309 if (target_type == TypeManager.char_type)
1310 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1311 } else if (expr_type == TypeManager.short_type){
1313 // From short to sbyte, byte, ushort, uint, ulong, char
1315 if (target_type == TypeManager.sbyte_type)
1316 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1317 if (target_type == TypeManager.byte_type)
1318 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1319 if (target_type == TypeManager.ushort_type)
1320 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1321 if (target_type == TypeManager.uint32_type)
1322 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1323 if (target_type == TypeManager.uint64_type)
1324 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1325 if (target_type == TypeManager.char_type)
1326 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1327 } else if (expr_type == TypeManager.ushort_type){
1329 // From ushort to sbyte, byte, short, char
1331 if (target_type == TypeManager.sbyte_type)
1332 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1333 if (target_type == TypeManager.byte_type)
1334 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1335 if (target_type == TypeManager.short_type)
1336 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1337 if (target_type == TypeManager.char_type)
1338 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1339 } else if (expr_type == TypeManager.int32_type){
1341 // From int to sbyte, byte, short, ushort, uint, ulong, char
1343 if (target_type == TypeManager.sbyte_type)
1344 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1345 if (target_type == TypeManager.byte_type)
1346 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1347 if (target_type == TypeManager.short_type)
1348 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1349 if (target_type == TypeManager.ushort_type)
1350 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1351 if (target_type == TypeManager.uint32_type)
1352 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1353 if (target_type == TypeManager.uint64_type)
1354 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1355 if (target_type == TypeManager.char_type)
1356 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1357 } else if (expr_type == TypeManager.uint32_type){
1359 // From uint to sbyte, byte, short, ushort, int, char
1361 if (target_type == TypeManager.sbyte_type)
1362 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1363 if (target_type == TypeManager.byte_type)
1364 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1365 if (target_type == TypeManager.short_type)
1366 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1367 if (target_type == TypeManager.ushort_type)
1368 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1369 if (target_type == TypeManager.int32_type)
1370 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1371 if (target_type == TypeManager.char_type)
1372 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1373 } else if (expr_type == TypeManager.int64_type){
1375 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1377 if (target_type == TypeManager.sbyte_type)
1378 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1379 if (target_type == TypeManager.byte_type)
1380 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1381 if (target_type == TypeManager.short_type)
1382 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1383 if (target_type == TypeManager.ushort_type)
1384 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1385 if (target_type == TypeManager.int32_type)
1386 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1387 if (target_type == TypeManager.uint32_type)
1388 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1389 if (target_type == TypeManager.uint64_type)
1390 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1391 if (target_type == TypeManager.char_type)
1392 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1393 } else if (expr_type == TypeManager.uint64_type){
1395 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1397 if (target_type == TypeManager.sbyte_type)
1398 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1399 if (target_type == TypeManager.byte_type)
1400 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1401 if (target_type == TypeManager.short_type)
1402 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1403 if (target_type == TypeManager.ushort_type)
1404 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1405 if (target_type == TypeManager.int32_type)
1406 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1407 if (target_type == TypeManager.uint32_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1409 if (target_type == TypeManager.int64_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1411 if (target_type == TypeManager.char_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1413 } else if (expr_type == TypeManager.char_type){
1415 // From char to sbyte, byte, short
1417 if (target_type == TypeManager.sbyte_type)
1418 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1419 if (target_type == TypeManager.byte_type)
1420 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1421 if (target_type == TypeManager.short_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1423 } else if (expr_type == TypeManager.float_type){
1425 // From float to sbyte, byte, short,
1426 // ushort, int, uint, long, ulong, char
1429 if (target_type == TypeManager.sbyte_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1431 if (target_type == TypeManager.byte_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1433 if (target_type == TypeManager.short_type)
1434 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1435 if (target_type == TypeManager.ushort_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1437 if (target_type == TypeManager.int32_type)
1438 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1439 if (target_type == TypeManager.uint32_type)
1440 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1441 if (target_type == TypeManager.int64_type)
1442 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1443 if (target_type == TypeManager.uint64_type)
1444 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1445 if (target_type == TypeManager.char_type)
1446 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1447 if (target_type == TypeManager.decimal_type)
1448 return InternalTypeConstructor (ec, expr, target_type);
1449 } else if (expr_type == TypeManager.double_type){
1451 // From double to byte, byte, short,
1452 // ushort, int, uint, long, ulong,
1453 // char, float or decimal
1455 if (target_type == TypeManager.sbyte_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1457 if (target_type == TypeManager.byte_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1459 if (target_type == TypeManager.short_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1461 if (target_type == TypeManager.ushort_type)
1462 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1463 if (target_type == TypeManager.int32_type)
1464 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1465 if (target_type == TypeManager.uint32_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1467 if (target_type == TypeManager.int64_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1469 if (target_type == TypeManager.uint64_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1471 if (target_type == TypeManager.char_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1473 if (target_type == TypeManager.float_type)
1474 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1475 if (target_type == TypeManager.decimal_type)
1476 return InternalTypeConstructor (ec, expr, target_type);
1479 // decimal is taken care of by the op_Explicit methods.
1485 /// Returns whether an explicit reference conversion can be performed
1486 /// from source_type to target_type
1488 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1490 bool target_is_value_type = target_type.IsValueType;
1492 if (source_type == target_type)
1496 // From object to any reference type
1498 if (source_type == TypeManager.object_type && !target_is_value_type)
1502 // From any class S to any class-type T, provided S is a base class of T
1504 if (target_type.IsSubclassOf (source_type))
1508 // From any interface type S to any interface T provided S is not derived from T
1510 if (source_type.IsInterface && target_type.IsInterface){
1511 if (!target_type.IsSubclassOf (source_type))
1516 // From any class type S to any interface T, provides S is not sealed
1517 // and provided S does not implement T.
1519 if (target_type.IsInterface && !source_type.IsSealed &&
1520 !target_type.IsAssignableFrom (source_type))
1524 // From any interface-type S to to any class type T, provided T is not
1525 // sealed, or provided T implements S.
1527 if (source_type.IsInterface &&
1528 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1531 // From an array type S with an element type Se to an array type T with an
1532 // element type Te provided all the following are true:
1533 // * S and T differe only in element type, in other words, S and T
1534 // have the same number of dimensions.
1535 // * Both Se and Te are reference types
1536 // * An explicit referenc conversions exist from Se to Te
1538 if (source_type.IsArray && target_type.IsArray) {
1539 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1541 Type source_element_type = source_type.GetElementType ();
1542 Type target_element_type = target_type.GetElementType ();
1544 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1545 if (ExplicitReferenceConversionExists (source_element_type,
1546 target_element_type))
1552 // From System.Array to any array-type
1553 if (source_type == TypeManager.array_type &&
1554 target_type.IsSubclassOf (TypeManager.array_type)){
1559 // From System delegate to any delegate-type
1561 if (source_type == TypeManager.delegate_type &&
1562 target_type.IsSubclassOf (TypeManager.delegate_type))
1566 // From ICloneable to Array or Delegate types
1568 if (source_type == TypeManager.icloneable_type &&
1569 (target_type == TypeManager.array_type ||
1570 target_type == TypeManager.delegate_type))
1577 /// Implements Explicit Reference conversions
1579 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1581 Type source_type = source.Type;
1582 bool target_is_value_type = target_type.IsValueType;
1585 // From object to any reference type
1587 if (source_type == TypeManager.object_type && !target_is_value_type)
1588 return new ClassCast (source, target_type);
1592 // From any class S to any class-type T, provided S is a base class of T
1594 if (target_type.IsSubclassOf (source_type))
1595 return new ClassCast (source, target_type);
1598 // From any interface type S to any interface T provided S is not derived from T
1600 if (source_type.IsInterface && target_type.IsInterface){
1602 Type [] ifaces = source_type.GetInterfaces ();
1604 if (TypeManager.ImplementsInterface (source_type, target_type))
1607 return new ClassCast (source, target_type);
1611 // From any class type S to any interface T, provides S is not sealed
1612 // and provided S does not implement T.
1614 if (target_type.IsInterface && !source_type.IsSealed) {
1616 if (TypeManager.ImplementsInterface (source_type, target_type))
1619 return new ClassCast (source, target_type);
1624 // From any interface-type S to to any class type T, provided T is not
1625 // sealed, or provided T implements S.
1627 if (source_type.IsInterface) {
1629 if (target_type.IsSealed)
1632 if (TypeManager.ImplementsInterface (target_type, source_type))
1633 return new ClassCast (source, target_type);
1638 // From an array type S with an element type Se to an array type T with an
1639 // element type Te provided all the following are true:
1640 // * S and T differe only in element type, in other words, S and T
1641 // have the same number of dimensions.
1642 // * Both Se and Te are reference types
1643 // * An explicit referenc conversions exist from Se to Te
1645 if (source_type.IsArray && target_type.IsArray) {
1646 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1648 Type source_element_type = source_type.GetElementType ();
1649 Type target_element_type = target_type.GetElementType ();
1651 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1652 if (ExplicitReferenceConversionExists (source_element_type,
1653 target_element_type))
1654 return new ClassCast (source, target_type);
1659 // From System.Array to any array-type
1660 if (source_type == TypeManager.array_type &&
1661 target_type.IsSubclassOf (TypeManager.array_type)){
1662 return new ClassCast (source, target_type);
1666 // From System delegate to any delegate-type
1668 if (source_type == TypeManager.delegate_type &&
1669 target_type.IsSubclassOf (TypeManager.delegate_type))
1670 return new ClassCast (source, target_type);
1673 // From ICloneable to Array or Delegate types
1675 if (source_type == TypeManager.icloneable_type &&
1676 (target_type == TypeManager.array_type ||
1677 target_type == TypeManager.delegate_type))
1678 return new ClassCast (source, target_type);
1684 /// Performs an explicit conversion of the expression `expr' whose
1685 /// type is expr.Type to `target_type'.
1687 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1688 Type target_type, Location loc)
1690 Type expr_type = expr.Type;
1691 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1696 ne = ConvertNumericExplicit (ec, expr, target_type);
1701 // Unboxing conversion.
1703 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1704 return new UnboxCast (expr, target_type);
1709 if (expr is EnumLiteral) {
1710 Expression e = ((EnumLiteral) expr).Child;
1712 return ConvertImplicit (ec, e, target_type, loc);
1715 ne = ConvertReferenceExplicit (expr, target_type);
1719 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1723 error30 (loc, expr_type, target_type);
1728 /// Same as ConverExplicit, only it doesn't include user defined conversions
1730 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1731 Type target_type, Location l)
1733 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1738 ne = ConvertNumericExplicit (ec, expr, target_type);
1742 ne = ConvertReferenceExplicit (expr, target_type);
1746 error30 (l, expr.Type, target_type);
1750 static string ExprClassName (ExprClass c)
1753 case ExprClass.Invalid:
1755 case ExprClass.Value:
1757 case ExprClass.Variable:
1759 case ExprClass.Namespace:
1761 case ExprClass.Type:
1763 case ExprClass.MethodGroup:
1764 return "method group";
1765 case ExprClass.PropertyAccess:
1766 return "property access";
1767 case ExprClass.EventAccess:
1768 return "event access";
1769 case ExprClass.IndexerAccess:
1770 return "indexer access";
1771 case ExprClass.Nothing:
1774 throw new Exception ("Should not happen");
1778 /// Reports that we were expecting `expr' to be of class `expected'
1780 protected void report118 (Location loc, Expression expr, string expected)
1782 string kind = "Unknown";
1785 kind = ExprClassName (expr.ExprClass);
1787 Error (118, loc, "Expression denotes a `" + kind +
1788 "' where a `" + expected + "' was expected");
1792 /// This function tries to reduce the expression performing
1793 /// constant folding and common subexpression elimination
1795 static public Expression Reduce (EmitContext ec, Expression e)
1797 //Console.WriteLine ("Calling reduce");
1798 return e.Reduce (ec);
1801 static void error31 (Location l, string val, Type t)
1803 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1804 TypeManager.CSharpName (t));
1808 /// Converts the IntLiteral, UIntLiteral, LongLiteral or
1809 /// ULongLiteral into the integral target_type.
1811 /// This is used by the switch statement, so the domain
1812 /// of work is restricted to the literals above, and the
1813 /// targets are int32, uint32, char, byte, sbyte, ushort,
1814 /// short, uint64 and int64
1816 public static Literal ConvertIntLiteral (Literal l, Type target_type, Location loc)
1820 if (l.Type == target_type)
1824 // Make into one of the literals we handle, we dont really care
1825 // about this value as we will just return a few limited types
1827 if (l is EnumLiteral)
1828 l = ((EnumLiteral)l).WidenToCompilerLiteral ();
1830 if (l is IntLiteral){
1831 int v = ((IntLiteral) l).Value;
1833 if (target_type == TypeManager.uint32_type){
1835 return new UIntLiteral ((uint) v);
1836 } else if (target_type == TypeManager.char_type){
1837 if (v >= Char.MinValue && v <= Char.MaxValue)
1839 } else if (target_type == TypeManager.byte_type){
1840 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1842 } else if (target_type == TypeManager.sbyte_type){
1843 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1845 } else if (target_type == TypeManager.short_type){
1846 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1848 } else if (target_type == TypeManager.ushort_type){
1849 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1851 } else if (target_type == TypeManager.int64_type)
1852 return new LongLiteral (v);
1853 else if (target_type == TypeManager.uint64_type){
1855 return new ULongLiteral ((ulong) v);
1859 } else if (l is UIntLiteral){
1860 uint v = ((UIntLiteral) l).Value;
1862 if (target_type == TypeManager.int32_type){
1863 if (v <= Int32.MaxValue)
1864 return new IntLiteral ((int) v);
1865 } else if (target_type == TypeManager.char_type){
1866 if (v >= Char.MinValue && v <= Char.MaxValue)
1868 } else if (target_type == TypeManager.byte_type){
1869 if (v <= Byte.MaxValue)
1871 } else if (target_type == TypeManager.sbyte_type){
1872 if (v <= SByte.MaxValue)
1874 } else if (target_type == TypeManager.short_type){
1875 if (v <= UInt16.MaxValue)
1877 } else if (target_type == TypeManager.ushort_type){
1878 if (v <= UInt16.MaxValue)
1880 } else if (target_type == TypeManager.int64_type)
1881 return new LongLiteral (v);
1882 else if (target_type == TypeManager.uint64_type)
1883 return new ULongLiteral (v);
1885 } else if (l is LongLiteral){
1886 long v = ((LongLiteral) l).Value;
1888 if (target_type == TypeManager.int32_type){
1889 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1890 return new IntLiteral ((int) v);
1891 } else if (target_type == TypeManager.uint32_type){
1892 if (v >= 0 && v <= UInt32.MaxValue)
1893 return new UIntLiteral ((uint) v);
1894 } else if (target_type == TypeManager.char_type){
1895 if (v >= Char.MinValue && v <= Char.MaxValue)
1896 return new IntLiteral ((int) v);
1897 } else if (target_type == TypeManager.byte_type){
1898 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1899 return new IntLiteral ((int) v);
1900 } else if (target_type == TypeManager.sbyte_type){
1901 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1902 return new IntLiteral ((int) v);
1903 } else if (target_type == TypeManager.short_type){
1904 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1905 return new IntLiteral ((int) v);
1906 } else if (target_type == TypeManager.ushort_type){
1907 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1908 return new IntLiteral ((int) v);
1909 } else if (target_type == TypeManager.uint64_type){
1911 return new ULongLiteral ((ulong) v);
1914 } else if (l is ULongLiteral){
1915 ulong v = ((ULongLiteral) l).Value;
1917 if (target_type == TypeManager.int32_type){
1918 if (v <= Int32.MaxValue)
1919 return new IntLiteral ((int) v);
1920 } else if (target_type == TypeManager.uint32_type){
1921 if (v <= UInt32.MaxValue)
1922 return new UIntLiteral ((uint) v);
1923 } else if (target_type == TypeManager.char_type){
1924 if (v >= Char.MinValue && v <= Char.MaxValue)
1925 return new IntLiteral ((int) v);
1926 } else if (target_type == TypeManager.byte_type){
1927 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1928 return new IntLiteral ((int) v);
1929 } else if (target_type == TypeManager.sbyte_type){
1930 if (v <= (int) SByte.MaxValue)
1931 return new IntLiteral ((int) v);
1932 } else if (target_type == TypeManager.short_type){
1933 if (v <= UInt16.MaxValue)
1934 return new IntLiteral ((int) v);
1935 } else if (target_type == TypeManager.ushort_type){
1936 if (v <= UInt16.MaxValue)
1937 return new IntLiteral ((int) v);
1938 } else if (target_type == TypeManager.int64_type){
1939 if (v <= Int64.MaxValue)
1940 return new LongLiteral ((long) v);
1945 error31 (loc, s, target_type);
1952 /// This is just a base class for expressions that can
1953 /// appear on statements (invocations, object creation,
1954 /// assignments, post/pre increment and decrement). The idea
1955 /// being that they would support an extra Emition interface that
1956 /// does not leave a result on the stack.
1958 public abstract class ExpressionStatement : Expression {
1961 /// Requests the expression to be emitted in a `statement'
1962 /// context. This means that no new value is left on the
1963 /// stack after invoking this method (constrasted with
1964 /// Emit that will always leave a value on the stack).
1966 public abstract void EmitStatement (EmitContext ec);
1970 /// This kind of cast is used to encapsulate the child
1971 /// whose type is child.Type into an expression that is
1972 /// reported to return "return_type". This is used to encapsulate
1973 /// expressions which have compatible types, but need to be dealt
1974 /// at higher levels with.
1976 /// For example, a "byte" expression could be encapsulated in one
1977 /// of these as an "unsigned int". The type for the expression
1978 /// would be "unsigned int".
1981 public class EmptyCast : Expression {
1982 protected Expression child;
1984 public EmptyCast (Expression child, Type return_type)
1986 ExprClass = child.ExprClass;
1991 public override Expression DoResolve (EmitContext ec)
1993 // This should never be invoked, we are born in fully
1994 // initialized state.
1999 public override void Emit (EmitContext ec)
2007 /// This class is used to wrap literals which belong inside Enums
2009 public class EnumLiteral : Literal {
2010 public Expression Child;
2012 public EnumLiteral (Expression child, Type enum_type)
2014 ExprClass = child.ExprClass;
2019 public override Expression DoResolve (EmitContext ec)
2021 // This should never be invoked, we are born in fully
2022 // initialized state.
2027 public override void Emit (EmitContext ec)
2032 public override object GetValue ()
2034 return ((Literal) Child).GetValue ();
2038 // Converts from one of the valid underlying types for an enumeration
2039 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2040 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2042 public Literal WidenToCompilerLiteral ()
2044 Type t = Child.Type.UnderlyingSystemType;
2045 object v = ((Literal) Child).GetValue ();;
2047 if (t == TypeManager.int32_type)
2048 return new IntLiteral ((int) v);
2049 if (t == TypeManager.uint32_type)
2050 return new UIntLiteral ((uint) v);
2051 if (t == TypeManager.int64_type)
2052 return new LongLiteral ((long) v);
2053 if (t == TypeManager.uint64_type)
2054 return new ULongLiteral ((ulong) v);
2055 if (t == TypeManager.short_type)
2056 return new IntLiteral ((short) v);
2057 if (t == TypeManager.ushort_type)
2058 return new UIntLiteral ((ushort) v);
2059 if (t == TypeManager.byte_type)
2060 return new UIntLiteral ((byte) v);
2061 if (t == TypeManager.sbyte_type)
2062 return new IntLiteral ((sbyte) v);
2064 throw new Exception ("Invalid enumeration underlying type: " + t);
2067 public override string AsString ()
2069 return ((Literal) Child).AsString ();
2074 /// This kind of cast is used to encapsulate Value Types in objects.
2076 /// The effect of it is to box the value type emitted by the previous
2079 public class BoxedCast : EmptyCast {
2081 public BoxedCast (Expression expr)
2082 : base (expr, TypeManager.object_type)
2086 public override Expression DoResolve (EmitContext ec)
2088 // This should never be invoked, we are born in fully
2089 // initialized state.
2094 public override void Emit (EmitContext ec)
2097 ec.ig.Emit (OpCodes.Box, child.Type);
2101 public class UnboxCast : EmptyCast {
2102 public UnboxCast (Expression expr, Type return_type)
2103 : base (expr, return_type)
2107 public override Expression DoResolve (EmitContext ec)
2109 // This should never be invoked, we are born in fully
2110 // initialized state.
2115 public override void Emit (EmitContext ec)
2118 ILGenerator ig = ec.ig;
2121 ig.Emit (OpCodes.Unbox, t);
2124 // Load the object from the pointer
2126 if (t == TypeManager.int32_type)
2127 ig.Emit (OpCodes.Ldind_I4);
2128 else if (t == TypeManager.uint32_type)
2129 ig.Emit (OpCodes.Ldind_U4);
2130 else if (t == TypeManager.short_type)
2131 ig.Emit (OpCodes.Ldind_I2);
2132 else if (t == TypeManager.ushort_type)
2133 ig.Emit (OpCodes.Ldind_U2);
2134 else if (t == TypeManager.char_type)
2135 ig.Emit (OpCodes.Ldind_U2);
2136 else if (t == TypeManager.byte_type)
2137 ig.Emit (OpCodes.Ldind_U1);
2138 else if (t == TypeManager.sbyte_type)
2139 ig.Emit (OpCodes.Ldind_I1);
2140 else if (t == TypeManager.uint64_type)
2141 ig.Emit (OpCodes.Ldind_I8);
2142 else if (t == TypeManager.int64_type)
2143 ig.Emit (OpCodes.Ldind_I8);
2144 else if (t == TypeManager.float_type)
2145 ig.Emit (OpCodes.Ldind_R4);
2146 else if (t == TypeManager.double_type)
2147 ig.Emit (OpCodes.Ldind_R8);
2148 else if (t == TypeManager.bool_type)
2149 ig.Emit (OpCodes.Ldind_I1);
2150 else if (t == TypeManager.intptr_type)
2151 ig.Emit (OpCodes.Ldind_I);
2153 ig.Emit (OpCodes.Ldobj, t);
2158 /// This is used to perform explicit numeric conversions.
2160 /// Explicit numeric conversions might trigger exceptions in a checked
2161 /// context, so they should generate the conv.ovf opcodes instead of
2164 public class ConvCast : EmptyCast {
2165 public enum Mode : byte {
2166 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2168 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2169 U2_I1, U2_U1, U2_I2, U2_CH,
2170 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2171 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2172 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2173 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2174 CH_I1, CH_U1, CH_I2,
2175 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2176 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2181 public ConvCast (Expression child, Type return_type, Mode m)
2182 : base (child, return_type)
2187 public override Expression DoResolve (EmitContext ec)
2189 // This should never be invoked, we are born in fully
2190 // initialized state.
2195 public override void Emit (EmitContext ec)
2197 ILGenerator ig = ec.ig;
2203 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2204 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2205 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2206 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2207 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2209 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2210 case Mode.U1_CH: /* nothing */ break;
2212 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2213 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2214 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2215 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2216 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2217 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2219 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2220 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2221 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2222 case Mode.U2_CH: /* nothing */ break;
2224 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2225 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2226 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2227 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2228 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2229 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2230 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2232 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2233 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2234 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2235 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2236 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2237 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2239 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2240 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2241 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2242 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2243 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2244 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2245 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2246 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2248 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2249 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2250 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2251 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2252 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2253 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2254 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2255 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2257 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2258 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2259 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2261 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2262 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2263 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2264 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2265 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2266 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2267 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2268 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2269 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2271 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2272 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2273 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2274 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2275 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2276 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2277 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2278 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2279 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2280 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2284 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2285 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2286 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2287 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2288 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2290 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2291 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2293 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2294 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2295 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2296 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2297 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2298 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2300 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2301 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2302 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2303 case Mode.U2_CH: /* nothing */ break;
2305 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2306 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2307 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2308 case Mode.I4_U4: /* nothing */ break;
2309 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2310 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2311 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2313 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2314 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2315 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2316 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2317 case Mode.U4_I4: /* nothing */ break;
2318 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2320 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2321 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2322 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2323 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2324 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2325 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2326 case Mode.I8_U8: /* nothing */ break;
2327 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2329 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2330 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2331 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2332 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2333 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2334 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2335 case Mode.U8_I8: /* nothing */ break;
2336 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2338 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2339 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2340 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2342 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2343 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2344 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2345 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2346 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2347 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2348 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2349 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2350 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2352 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2353 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2354 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2355 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2356 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2357 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2358 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2359 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2360 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2361 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2367 public class OpcodeCast : EmptyCast {
2371 public OpcodeCast (Expression child, Type return_type, OpCode op)
2372 : base (child, return_type)
2376 second_valid = false;
2379 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2380 : base (child, return_type)
2385 second_valid = true;
2388 public override Expression DoResolve (EmitContext ec)
2390 // This should never be invoked, we are born in fully
2391 // initialized state.
2396 public override void Emit (EmitContext ec)
2407 /// This kind of cast is used to encapsulate a child and cast it
2408 /// to the class requested
2410 public class ClassCast : EmptyCast {
2411 public ClassCast (Expression child, Type return_type)
2412 : base (child, return_type)
2417 public override Expression DoResolve (EmitContext ec)
2419 // This should never be invoked, we are born in fully
2420 // initialized state.
2425 public override void Emit (EmitContext ec)
2429 ec.ig.Emit (OpCodes.Castclass, type);
2435 /// SimpleName expressions are initially formed of a single
2436 /// word and it only happens at the beginning of the expression.
2440 /// The expression will try to be bound to a Field, a Method
2441 /// group or a Property. If those fail we pass the name to our
2442 /// caller and the SimpleName is compounded to perform a type
2443 /// lookup. The idea behind this process is that we want to avoid
2444 /// creating a namespace map from the assemblies, as that requires
2445 /// the GetExportedTypes function to be called and a hashtable to
2446 /// be constructed which reduces startup time. If later we find
2447 /// that this is slower, we should create a `NamespaceExpr' expression
2448 /// that fully participates in the resolution process.
2450 /// For example `System.Console.WriteLine' is decomposed into
2451 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2453 /// The first SimpleName wont produce a match on its own, so it will
2455 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2457 /// System.Console will produce a TypeExpr match.
2459 /// The downside of this is that we might be hitting `LookupType' too many
2460 /// times with this scheme.
2462 public class SimpleName : Expression {
2463 public readonly string Name;
2464 public readonly Location Location;
2466 public SimpleName (string name, Location l)
2472 public static void Error120 (Location l, string name)
2476 "An object reference is required " +
2477 "for the non-static field `"+name+"'");
2481 // Checks whether we are trying to access an instance
2482 // property, method or field from a static body.
2484 Expression MemberStaticCheck (Expression e)
2486 if (e is FieldExpr){
2487 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2490 Error120 (Location, Name);
2493 } else if (e is MethodGroupExpr){
2494 MethodGroupExpr mg = (MethodGroupExpr) e;
2496 if (!mg.RemoveInstanceMethods ()){
2497 Error120 (Location, mg.Methods [0].Name);
2501 } else if (e is PropertyExpr){
2502 if (!((PropertyExpr) e).IsStatic){
2503 Error120 (Location, Name);
2512 // 7.5.2: Simple Names.
2514 // Local Variables and Parameters are handled at
2515 // parse time, so they never occur as SimpleNames.
2517 public override Expression DoResolve (EmitContext ec)
2522 // Stage 1: Performed by the parser (binding to locals or parameters).
2526 // Stage 2: Lookup members
2528 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2531 // Stage 3: Lookup symbol in the various namespaces.
2533 DeclSpace ds = ec.TypeContainer;
2537 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2538 return new TypeExpr (t);
2541 // Stage 3 part b: Lookup up if we are an alias to a type
2544 // Since we are cheating: we only do the Alias lookup for
2545 // namespaces if the name does not include any dots in it
2548 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2549 // System.Console.WriteLine (Name + " --> " + alias_value);
2550 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2552 return new TypeExpr (t);
2554 // we have alias value, but it isn't Type, so try if it's namespace
2555 return new SimpleName (alias_value, Location);
2558 // No match, maybe our parent can compose us
2559 // into something meaningful.
2564 // Step 2, continues here.
2568 if (e is FieldExpr){
2569 FieldExpr fe = (FieldExpr) e;
2571 if (!fe.FieldInfo.IsStatic){
2572 This t = new This (Location.Null);
2574 fe.InstanceExpression = t.DoResolve (ec);
2577 FieldInfo fi = fe.FieldInfo;
2579 if (fi is FieldBuilder) {
2580 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2583 object o = c.LookupConstantValue (ec);
2584 Expression l = Literalize (o, fi.FieldType);
2586 return ((Literal) l);
2592 return MemberStaticCheck (e);
2597 public override void Emit (EmitContext ec)
2600 // If this is ever reached, then we failed to
2601 // find the name as a namespace
2604 Error (103, Location, "The name `" + Name +
2605 "' does not exist in the class `" +
2606 ec.TypeContainer.Name + "'");
2611 /// Fully resolved expression that evaluates to a type
2613 public class TypeExpr : Expression {
2614 public TypeExpr (Type t)
2617 eclass = ExprClass.Type;
2620 override public Expression DoResolve (EmitContext ec)
2625 override public void Emit (EmitContext ec)
2627 throw new Exception ("Implement me");
2632 /// MethodGroup Expression.
2634 /// This is a fully resolved expression that evaluates to a type
2636 public class MethodGroupExpr : Expression {
2637 public MethodBase [] Methods;
2638 Expression instance_expression = null;
2640 public MethodGroupExpr (MemberInfo [] mi)
2642 Methods = new MethodBase [mi.Length];
2643 mi.CopyTo (Methods, 0);
2644 eclass = ExprClass.MethodGroup;
2647 public MethodGroupExpr (ArrayList l)
2649 Methods = new MethodBase [l.Count];
2651 l.CopyTo (Methods, 0);
2652 eclass = ExprClass.MethodGroup;
2656 // `A method group may have associated an instance expression'
2658 public Expression InstanceExpression {
2660 return instance_expression;
2664 instance_expression = value;
2668 override public Expression DoResolve (EmitContext ec)
2673 override public void Emit (EmitContext ec)
2675 throw new Exception ("This should never be reached");
2678 bool RemoveMethods (bool keep_static)
2680 ArrayList smethods = new ArrayList ();
2681 int top = Methods.Length;
2684 for (i = 0; i < top; i++){
2685 MethodBase mb = Methods [i];
2687 if (mb.IsStatic == keep_static)
2691 if (smethods.Count == 0)
2694 Methods = new MethodBase [smethods.Count];
2695 smethods.CopyTo (Methods, 0);
2701 /// Removes any instance methods from the MethodGroup, returns
2702 /// false if the resulting set is empty.
2704 public bool RemoveInstanceMethods ()
2706 return RemoveMethods (true);
2710 /// Removes any static methods from the MethodGroup, returns
2711 /// false if the resulting set is empty.
2713 public bool RemoveStaticMethods ()
2715 return RemoveMethods (false);
2720 /// Fully resolved expression that evaluates to a Field
2722 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2723 public readonly FieldInfo FieldInfo;
2724 public Expression InstanceExpression;
2727 public FieldExpr (FieldInfo fi, Location l)
2730 eclass = ExprClass.Variable;
2731 type = fi.FieldType;
2735 override public Expression DoResolve (EmitContext ec)
2737 if (!FieldInfo.IsStatic){
2738 if (InstanceExpression == null){
2739 throw new Exception ("non-static FieldExpr without instance var\n" +
2740 "You have to assign the Instance variable\n" +
2741 "Of the FieldExpr to set this\n");
2744 InstanceExpression = InstanceExpression.Resolve (ec);
2745 if (InstanceExpression == null)
2753 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2755 Expression e = DoResolve (ec);
2760 if (!FieldInfo.IsInitOnly)
2764 // InitOnly fields can only be assigned in constructors
2767 if (ec.IsConstructor)
2770 Report.Error (191, loc,
2771 "Readonly field can not be assigned outside " +
2772 "of constructor or variable initializer");
2777 override public void Emit (EmitContext ec)
2779 ILGenerator ig = ec.ig;
2781 if (FieldInfo.IsStatic)
2782 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2784 InstanceExpression.Emit (ec);
2786 ig.Emit (OpCodes.Ldfld, FieldInfo);
2790 public void EmitAssign (EmitContext ec, Expression source)
2792 bool is_static = FieldInfo.IsStatic;
2795 Expression instance = InstanceExpression;
2797 if (instance.Type.IsValueType){
2798 if (instance is IMemoryLocation){
2799 IMemoryLocation ml = (IMemoryLocation) instance;
2803 throw new Exception ("The " + instance + " of type " + Type+
2804 "represents a ValueType and does not " +
2805 "implement IMemoryLocation");
2812 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2814 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2818 public void AddressOf (EmitContext ec)
2820 if (FieldInfo.IsStatic)
2821 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2823 InstanceExpression.Emit (ec);
2824 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2830 /// Expression that evaluates to a Property. The Assign class
2831 /// might set the `Value' expression if we are in an assignment.
2833 /// This is not an LValue because we need to re-write the expression, we
2834 /// can not take data from the stack and store it.
2836 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2837 public readonly PropertyInfo PropertyInfo;
2838 public readonly bool IsStatic;
2839 MethodInfo [] Accessors;
2842 Expression instance_expr;
2844 public PropertyExpr (PropertyInfo pi, Location l)
2847 eclass = ExprClass.PropertyAccess;
2850 Accessors = TypeManager.GetAccessors (pi);
2852 if (Accessors != null)
2853 for (int i = 0; i < Accessors.Length; i++){
2854 if (Accessors [i] != null)
2855 if (Accessors [i].IsStatic)
2859 Accessors = new MethodInfo [2];
2861 type = pi.PropertyType;
2865 // The instance expression associated with this expression
2867 public Expression InstanceExpression {
2869 instance_expr = value;
2873 return instance_expr;
2877 public bool VerifyAssignable ()
2879 if (!PropertyInfo.CanWrite){
2880 Report.Error (200, loc,
2881 "The property `" + PropertyInfo.Name +
2882 "' can not be assigned to, as it has not set accessor");
2889 override public Expression DoResolve (EmitContext ec)
2891 if (!PropertyInfo.CanRead){
2892 Report.Error (154, loc,
2893 "The property `" + PropertyInfo.Name +
2894 "' can not be used in " +
2895 "this context because it lacks a get accessor");
2899 type = PropertyInfo.PropertyType;
2904 override public void Emit (EmitContext ec)
2906 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2911 // Implements the IAssignMethod interface for assignments
2913 public void EmitAssign (EmitContext ec, Expression source)
2915 Argument arg = new Argument (source, Argument.AType.Expression);
2916 ArrayList args = new ArrayList ();
2919 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2922 override public void EmitStatement (EmitContext ec)
2925 ec.ig.Emit (OpCodes.Pop);
2930 /// Fully resolved expression that evaluates to a Expression
2932 public class EventExpr : Expression {
2933 public readonly EventInfo EventInfo;
2935 public Expression InstanceExpression;
2937 public readonly bool IsStatic;
2939 public EventExpr (EventInfo ei, Location loc)
2943 eclass = ExprClass.EventAccess;
2945 MethodInfo add_accessor = TypeManager.GetAddMethod (ei);
2946 MethodInfo remove_accessor = TypeManager.GetRemoveMethod (ei);
2948 if (add_accessor != null)
2949 if (add_accessor.IsStatic)
2952 if (remove_accessor != null)
2953 if (remove_accessor.IsStatic)
2957 override public Expression DoResolve (EmitContext ec)
2959 // We are born in resolved state.
2961 Console.WriteLine ("Came here");
2962 type = EventInfo.EventHandlerType;
2966 override public void Emit (EmitContext ec)
2968 throw new Exception ("Implement me");
2969 // FIXME: Implement.