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);
325 return new TypeExpr ((Type) mi);
331 // We copy methods from `new_members' into `target_list' if the signature
332 // for the method from in the new list does not exist in the target_list
334 // The name is assumed to be the same.
336 static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
338 if (target_list == null){
339 target_list = new ArrayList ();
341 target_list.AddRange (new_members);
345 MemberInfo [] target_array = new MemberInfo [target_list.Count];
346 target_list.CopyTo (target_array, 0);
348 foreach (MemberInfo mi in new_members){
349 MethodBase new_method = (MethodBase) mi;
350 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
352 foreach (MethodBase method in target_array){
353 Type [] old_args = TypeManager.GetArgumentTypes (method);
354 int new_count = new_args.Length;
355 int old_count = old_args.Length;
357 if (new_count != old_count){
358 target_list.Add (method);
362 for (int i = 0; i < old_count; i++){
363 if (old_args [i] == new_args [i])
365 target_list.Add (method);
374 // FIXME: Probably implement a cache for (t,name,current_access_set)?
376 // FIXME: We need to cope with access permissions here, or this wont
379 // This code could use some optimizations, but we need to do some
380 // measurements. For example, we could use a delegate to `flag' when
381 // something can not any longer be a method-group (because it is something
385 // If the return value is an Array, then it is an array of
388 // If the return value is an MemberInfo, it is anything, but a Method
392 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
393 // the arguments here and have MemberLookup return only the methods that
394 // match the argument count/type, unlike we are doing now (we delay this
397 // This is so we can catch correctly attempts to invoke instance methods
398 // from a static body (scan for error 120 in ResolveSimpleName).
401 // FIXME: Potential optimization, have a static ArrayList
403 public static Expression MemberLookup (EmitContext ec, Type t, string name,
404 bool same_type, MemberTypes mt,
405 BindingFlags bf, Location loc)
408 bf |= BindingFlags.NonPublic;
411 // Lookup for members starting in the type requested and going
412 // up the hierarchy until a match is found.
414 // As soon as a non-method match is found, we return.
416 // If methods are found though, then the search proceeds scanning
417 // for more public methods in the hierarchy with signatures that
418 // do not match any of the signatures found so far.
420 ArrayList method_list = null;
421 Type current_type = t;
422 bool searching = true;
426 mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
427 current_type, mt, bf | BindingFlags.DeclaredOnly,
428 Type.FilterName, name);
430 if (current_type == TypeManager.object_type)
433 current_type = current_type.BaseType;
436 // This happens with interfaces, they have a null
439 if (current_type == null)
446 int count = mi.Length;
451 if (count == 1 && !(mi [0] is MethodBase))
452 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
455 // We found methods, turn the search into "method scan"
458 method_list = CopyNewMethods (method_list, mi);
459 mt &= (MemberTypes.Method | MemberTypes.Constructor);
462 if (method_list != null && method_list.Count > 0)
463 return new MethodGroupExpr (method_list);
468 public const MemberTypes AllMemberTypes =
469 MemberTypes.Constructor |
473 MemberTypes.NestedType |
474 MemberTypes.Property;
476 public const BindingFlags AllBindingsFlags =
477 BindingFlags.Public |
478 BindingFlags.Static |
479 BindingFlags.Instance;
481 public static Expression MemberLookup (EmitContext ec, Type t, string name,
482 bool same_type, Location loc)
484 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
487 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
489 Type expr_type = expr.Type;
491 if (target_type == TypeManager.object_type) {
492 if (expr_type.IsClass)
493 return new EmptyCast (expr, target_type);
494 if (expr_type.IsValueType)
495 return new BoxedCast (expr);
496 } else if (expr_type.IsSubclassOf (target_type)) {
497 return new EmptyCast (expr, target_type);
499 // from any class-type S to any interface-type T.
500 if (expr_type.IsClass && target_type.IsInterface) {
501 if (TypeManager.ImplementsInterface (expr_type, target_type))
502 return new EmptyCast (expr, target_type);
507 // from any interface type S to interface-type T.
508 if (expr_type.IsInterface && target_type.IsInterface) {
510 if (TypeManager.ImplementsInterface (expr_type, target_type))
511 return new EmptyCast (expr, target_type);
516 // from an array-type S to an array-type of type T
517 if (expr_type.IsArray && target_type.IsArray) {
518 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
520 Type expr_element_type = expr_type.GetElementType ();
521 Type target_element_type = target_type.GetElementType ();
523 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
524 if (StandardConversionExists (expr_element_type,
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);
545 // from the null type to any reference-type.
546 if (expr is NullLiteral)
547 return new EmptyCast (expr, target_type);
557 /// Handles expressions like this: decimal d; d = 1;
558 /// and changes them into: decimal d; d = new System.Decimal (1);
560 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
562 ArrayList args = new ArrayList ();
564 args.Add (new Argument (expr, Argument.AType.Expression));
566 Expression ne = new New (target.FullName, args,
569 return ne.Resolve (ec);
573 /// Implicit Numeric Conversions.
575 /// expr is the expression to convert, returns a new expression of type
576 /// target_type or null if an implicit conversion is not possible.
578 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
579 Type target_type, Location loc)
581 Type expr_type = expr.Type;
584 // Attempt to do the implicit constant expression conversions
586 if (expr is IntLiteral){
589 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
593 } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
595 // Try the implicit constant expression conversion
596 // from long to ulong, instead of a nice routine,
599 if (((LongLiteral) expr).Value > 0)
600 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
603 if (expr_type == TypeManager.sbyte_type){
605 // From sbyte to short, int, long, float, double.
607 if (target_type == TypeManager.int32_type)
608 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
609 if (target_type == TypeManager.int64_type)
610 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
611 if (target_type == TypeManager.double_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
613 if (target_type == TypeManager.float_type)
614 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
615 if (target_type == TypeManager.short_type)
616 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
617 if (target_type == TypeManager.decimal_type)
618 return InternalTypeConstructor (ec, expr, target_type);
619 } else if (expr_type == TypeManager.byte_type){
621 // From byte to short, ushort, int, uint, long, ulong, float, double
623 if ((target_type == TypeManager.short_type) ||
624 (target_type == TypeManager.ushort_type) ||
625 (target_type == TypeManager.int32_type) ||
626 (target_type == TypeManager.uint32_type))
627 return new EmptyCast (expr, target_type);
629 if (target_type == TypeManager.uint64_type)
630 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
631 if (target_type == TypeManager.int64_type)
632 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
633 if (target_type == TypeManager.float_type)
634 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
635 if (target_type == TypeManager.double_type)
636 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
637 if (target_type == TypeManager.decimal_type)
638 return InternalTypeConstructor (ec, expr, target_type);
639 } else if (expr_type == TypeManager.short_type){
641 // From short to int, long, float, double
643 if (target_type == TypeManager.int32_type)
644 return new EmptyCast (expr, target_type);
645 if (target_type == TypeManager.int64_type)
646 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
647 if (target_type == TypeManager.double_type)
648 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
649 if (target_type == TypeManager.float_type)
650 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
651 if (target_type == TypeManager.decimal_type)
652 return InternalTypeConstructor (ec, expr, target_type);
653 } else if (expr_type == TypeManager.ushort_type){
655 // From ushort to int, uint, long, ulong, float, double
657 if (target_type == TypeManager.uint32_type)
658 return new EmptyCast (expr, target_type);
660 if (target_type == TypeManager.uint64_type)
661 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
662 if (target_type == TypeManager.int32_type)
663 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
664 if (target_type == TypeManager.int64_type)
665 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
666 if (target_type == TypeManager.double_type)
667 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
668 if (target_type == TypeManager.float_type)
669 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
670 if (target_type == TypeManager.decimal_type)
671 return InternalTypeConstructor (ec, expr, target_type);
672 } else if (expr_type == TypeManager.int32_type){
674 // From int to long, float, double
676 if (target_type == TypeManager.int64_type)
677 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
678 if (target_type == TypeManager.double_type)
679 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
680 if (target_type == TypeManager.float_type)
681 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
682 if (target_type == TypeManager.decimal_type)
683 return InternalTypeConstructor (ec, expr, target_type);
684 } else if (expr_type == TypeManager.uint32_type){
686 // From uint to long, ulong, float, double
688 if (target_type == TypeManager.int64_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
690 if (target_type == TypeManager.uint64_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
692 if (target_type == TypeManager.double_type)
693 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
695 if (target_type == TypeManager.float_type)
696 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
698 if (target_type == TypeManager.decimal_type)
699 return InternalTypeConstructor (ec, expr, target_type);
700 } else if ((expr_type == TypeManager.uint64_type) ||
701 (expr_type == TypeManager.int64_type)){
703 // From long/ulong to float, double
705 if (target_type == TypeManager.double_type)
706 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
708 if (target_type == TypeManager.float_type)
709 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
711 if (target_type == TypeManager.decimal_type)
712 return InternalTypeConstructor (ec, expr, target_type);
713 } else if (expr_type == TypeManager.char_type){
715 // From char to ushort, int, uint, long, ulong, float, double
717 if ((target_type == TypeManager.ushort_type) ||
718 (target_type == TypeManager.int32_type) ||
719 (target_type == TypeManager.uint32_type))
720 return new EmptyCast (expr, target_type);
721 if (target_type == TypeManager.uint64_type)
722 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
723 if (target_type == TypeManager.int64_type)
724 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
725 if (target_type == TypeManager.float_type)
726 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
727 if (target_type == TypeManager.double_type)
728 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
729 if (target_type == TypeManager.decimal_type)
730 return InternalTypeConstructor (ec, expr, target_type);
731 } else if (expr_type == TypeManager.float_type){
735 if (target_type == TypeManager.double_type)
736 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
743 /// Determines if a standard implicit conversion exists from
744 /// expr_type to target_type
746 public static bool StandardConversionExists (Type expr_type, Type target_type)
748 if (expr_type == target_type)
751 // First numeric conversions
753 if (expr_type == TypeManager.sbyte_type){
755 // From sbyte to short, int, long, float, double.
757 if ((target_type == TypeManager.int32_type) ||
758 (target_type == TypeManager.int64_type) ||
759 (target_type == TypeManager.double_type) ||
760 (target_type == TypeManager.float_type) ||
761 (target_type == TypeManager.short_type) ||
762 (target_type == TypeManager.decimal_type))
765 } else if (expr_type == TypeManager.byte_type){
767 // From byte to short, ushort, int, uint, long, ulong, float, double
769 if ((target_type == TypeManager.short_type) ||
770 (target_type == TypeManager.ushort_type) ||
771 (target_type == TypeManager.int32_type) ||
772 (target_type == TypeManager.uint32_type) ||
773 (target_type == TypeManager.uint64_type) ||
774 (target_type == TypeManager.int64_type) ||
775 (target_type == TypeManager.float_type) ||
776 (target_type == TypeManager.double_type) ||
777 (target_type == TypeManager.decimal_type))
780 } else if (expr_type == TypeManager.short_type){
782 // From short to int, long, float, double
784 if ((target_type == TypeManager.int32_type) ||
785 (target_type == TypeManager.int64_type) ||
786 (target_type == TypeManager.double_type) ||
787 (target_type == TypeManager.float_type) ||
788 (target_type == TypeManager.decimal_type))
791 } else if (expr_type == TypeManager.ushort_type){
793 // From ushort to int, uint, long, ulong, float, double
795 if ((target_type == TypeManager.uint32_type) ||
796 (target_type == TypeManager.uint64_type) ||
797 (target_type == TypeManager.int32_type) ||
798 (target_type == TypeManager.int64_type) ||
799 (target_type == TypeManager.double_type) ||
800 (target_type == TypeManager.float_type) ||
801 (target_type == TypeManager.decimal_type))
804 } else if (expr_type == TypeManager.int32_type){
806 // From int to long, float, double
808 if ((target_type == TypeManager.int64_type) ||
809 (target_type == TypeManager.double_type) ||
810 (target_type == TypeManager.float_type) ||
811 (target_type == TypeManager.decimal_type))
814 } else if (expr_type == TypeManager.uint32_type){
816 // From uint to long, ulong, float, double
818 if ((target_type == TypeManager.int64_type) ||
819 (target_type == TypeManager.uint64_type) ||
820 (target_type == TypeManager.double_type) ||
821 (target_type == TypeManager.float_type) ||
822 (target_type == TypeManager.decimal_type))
825 } else if ((expr_type == TypeManager.uint64_type) ||
826 (expr_type == TypeManager.int64_type)) {
828 // From long/ulong to float, double
830 if ((target_type == TypeManager.double_type) ||
831 (target_type == TypeManager.float_type) ||
832 (target_type == TypeManager.decimal_type))
835 } else if (expr_type == TypeManager.char_type){
837 // From char to ushort, int, uint, long, ulong, float, double
839 if ((target_type == TypeManager.ushort_type) ||
840 (target_type == TypeManager.int32_type) ||
841 (target_type == TypeManager.uint32_type) ||
842 (target_type == TypeManager.uint64_type) ||
843 (target_type == TypeManager.int64_type) ||
844 (target_type == TypeManager.float_type) ||
845 (target_type == TypeManager.double_type) ||
846 (target_type == TypeManager.decimal_type))
849 } else if (expr_type == TypeManager.float_type){
853 if (target_type == TypeManager.double_type)
857 // Next reference conversions
859 if (target_type == TypeManager.object_type) {
860 if ((expr_type.IsClass) ||
861 (expr_type.IsValueType))
864 } else if (expr_type.IsSubclassOf (target_type)) {
868 // from any class-type S to any interface-type T.
869 if (expr_type.IsClass && target_type.IsInterface)
872 // from any interface type S to interface-type T.
873 // FIXME : Is it right to use IsAssignableFrom ?
874 if (expr_type.IsInterface && target_type.IsInterface)
875 if (target_type.IsAssignableFrom (expr_type))
878 // from an array-type S to an array-type of type T
879 if (expr_type.IsArray && target_type.IsArray) {
880 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
882 Type expr_element_type = expr_type.GetElementType ();
883 Type target_element_type = target_type.GetElementType ();
885 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
886 if (StandardConversionExists (expr_element_type,
887 target_element_type))
892 // from an array-type to System.Array
893 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
896 // from any delegate type to System.Delegate
897 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
898 target_type == TypeManager.delegate_type)
899 if (target_type.IsAssignableFrom (expr_type))
902 // from any array-type or delegate type into System.ICloneable.
903 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
904 if (target_type == TypeManager.icloneable_type)
907 // from the null type to any reference-type.
908 // FIXME : How do we do this ?
915 static EmptyExpression MyEmptyExpr;
917 /// Tells whether an implicit conversion exists from expr_type to
920 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
923 if (MyEmptyExpr == null)
924 MyEmptyExpr = new EmptyExpression (expr_type);
926 MyEmptyExpr.SetType (expr_type);
928 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
932 /// Finds "most encompassed type" according to the spec (13.4.2)
933 /// amongst the methods in the MethodGroupExpr which convert from a
934 /// type encompassing source_type
936 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
940 for (int i = me.Methods.Length; i > 0; ) {
943 MethodBase mb = me.Methods [i];
944 ParameterData pd = Invocation.GetParameterData (mb);
945 Type param_type = pd.ParameterType (0);
947 if (StandardConversionExists (source_type, param_type)) {
951 if (StandardConversionExists (param_type, best))
960 /// Finds "most encompassing type" according to the spec (13.4.2)
961 /// amongst the methods in the MethodGroupExpr which convert to a
962 /// type encompassed by target_type
964 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
968 for (int i = me.Methods.Length; i > 0; ) {
971 MethodInfo mi = (MethodInfo) me.Methods [i];
972 Type ret_type = mi.ReturnType;
974 if (StandardConversionExists (ret_type, target)) {
978 if (!StandardConversionExists (ret_type, best))
990 /// User-defined Implicit conversions
992 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
993 Type target, Location loc)
995 return UserDefinedConversion (ec, source, target, loc, false);
999 /// User-defined Explicit conversions
1001 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1002 Type target, Location loc)
1004 return UserDefinedConversion (ec, source, target, loc, true);
1008 /// User-defined conversions
1010 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1011 Type target, Location loc,
1012 bool look_for_explicit)
1014 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1015 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1017 MethodBase method = null;
1018 Type source_type = source.Type;
1022 // If we have a boolean type, we need to check for the True operator
1024 // FIXME : How does the False operator come into the picture ?
1025 // FIXME : This doesn't look complete and very correct !
1026 if (target == TypeManager.bool_type)
1027 op_name = "op_True";
1029 op_name = "op_Implicit";
1031 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
1033 if (source_type.BaseType != null)
1034 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1036 mg3 = MemberLookup (ec, target, op_name, false, loc);
1038 if (target.BaseType != null)
1039 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1041 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1042 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1044 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1046 MethodGroupExpr union4 = null;
1048 if (look_for_explicit) {
1050 op_name = "op_Explicit";
1052 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
1054 if (source_type.BaseType != null)
1055 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1057 mg7 = MemberLookup (ec, target, op_name, false, loc);
1059 if (target.BaseType != null)
1060 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1062 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1063 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1065 union4 = Invocation.MakeUnionSet (union5, union6);
1068 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1070 if (union != null) {
1072 Type most_specific_source, most_specific_target;
1074 most_specific_source = FindMostEncompassedType (union, source_type);
1075 if (most_specific_source == null)
1078 most_specific_target = FindMostEncompassingType (union, target);
1079 if (most_specific_target == null)
1084 for (int i = union.Methods.Length; i > 0;) {
1087 MethodBase mb = union.Methods [i];
1088 ParameterData pd = Invocation.GetParameterData (mb);
1089 MethodInfo mi = (MethodInfo) union.Methods [i];
1091 if (pd.ParameterType (0) == most_specific_source &&
1092 mi.ReturnType == most_specific_target) {
1098 if (method == null || count > 1) {
1099 Report.Error (-11, loc, "Ambiguous user defined conversion");
1104 // This will do the conversion to the best match that we
1105 // found. Now we need to perform an implict standard conversion
1106 // if the best match was not the type that we were requested
1109 if (look_for_explicit)
1110 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1112 source = ConvertImplicitStandard (ec, source,
1113 most_specific_source, loc);
1118 e = new UserCast ((MethodInfo) method, source);
1120 if (e.Type != target){
1121 if (!look_for_explicit)
1122 e = ConvertImplicitStandard (ec, e, target, loc);
1124 e = ConvertExplicitStandard (ec, e, target, loc);
1135 /// Converts implicitly the resolved expression `expr' into the
1136 /// `target_type'. It returns a new expression that can be used
1137 /// in a context that expects a `target_type'.
1139 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1140 Type target_type, Location loc)
1142 Type expr_type = expr.Type;
1145 if (expr_type == target_type)
1148 if (target_type == null)
1149 throw new Exception ("Target type is null");
1151 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1155 e = ImplicitReferenceConversion (expr, target_type);
1159 e = ImplicitUserConversion (ec, expr, target_type, loc);
1163 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1164 IntLiteral i = (IntLiteral) expr;
1167 return new EmptyCast (expr, target_type);
1175 /// Attempts to apply the `Standard Implicit
1176 /// Conversion' rules to the expression `expr' into
1177 /// the `target_type'. It returns a new expression
1178 /// that can be used in a context that expects a
1181 /// This is different from `ConvertImplicit' in that the
1182 /// user defined implicit conversions are excluded.
1184 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1185 Type target_type, Location loc)
1187 Type expr_type = expr.Type;
1190 if (expr_type == target_type)
1193 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1197 e = ImplicitReferenceConversion (expr, target_type);
1201 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1202 IntLiteral i = (IntLiteral) expr;
1205 return new EmptyCast (expr, target_type);
1211 /// Attemps to perform an implict constant conversion of the IntLiteral
1212 /// into a different data type using casts (See Implicit Constant
1213 /// Expression Conversions)
1215 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1217 int value = il.Value;
1219 if (target_type == TypeManager.sbyte_type){
1220 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1221 return new EmptyCast (il, target_type);
1222 } else if (target_type == TypeManager.byte_type){
1223 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1224 return new EmptyCast (il, target_type);
1225 } else if (target_type == TypeManager.short_type){
1226 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1227 return new EmptyCast (il, target_type);
1228 } else if (target_type == TypeManager.ushort_type){
1229 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1230 return new EmptyCast (il, target_type);
1231 } else if (target_type == TypeManager.uint32_type){
1233 // we can optimize this case: a positive int32
1234 // always fits on a uint32
1237 return new EmptyCast (il, target_type);
1238 } else if (target_type == TypeManager.uint64_type){
1240 // we can optimize this case: a positive int32
1241 // always fits on a uint64. But we need an opcode
1245 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1252 /// Attemptes to implicityly convert `target' into `type', using
1253 /// ConvertImplicit. If there is no implicit conversion, then
1254 /// an error is signaled
1256 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1257 Type target_type, Location loc)
1261 e = ConvertImplicit (ec, source, target_type, loc);
1265 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1267 "Double literal cannot be implicitly converted to " +
1268 "float type, use F suffix to create a float literal");
1271 string msg = "Cannot convert implicitly from `"+
1272 TypeManager.CSharpName (source.Type) + "' to `" +
1273 TypeManager.CSharpName (target_type) + "'";
1275 Error (29, loc, msg);
1281 /// Performs the explicit numeric conversions
1283 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1286 Type expr_type = expr.Type;
1288 if (expr_type == TypeManager.sbyte_type){
1290 // From sbyte to byte, ushort, uint, ulong, char
1292 if (target_type == TypeManager.byte_type)
1293 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1294 if (target_type == TypeManager.ushort_type)
1295 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1296 if (target_type == TypeManager.uint32_type)
1297 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1298 if (target_type == TypeManager.uint64_type)
1299 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1300 if (target_type == TypeManager.char_type)
1301 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1302 } else if (expr_type == TypeManager.byte_type){
1304 // From byte to sbyte and char
1306 if (target_type == TypeManager.sbyte_type)
1307 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1308 if (target_type == TypeManager.char_type)
1309 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1310 } else if (expr_type == TypeManager.short_type){
1312 // From short to sbyte, byte, ushort, uint, ulong, char
1314 if (target_type == TypeManager.sbyte_type)
1315 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1316 if (target_type == TypeManager.byte_type)
1317 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1318 if (target_type == TypeManager.ushort_type)
1319 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1320 if (target_type == TypeManager.uint32_type)
1321 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1322 if (target_type == TypeManager.uint64_type)
1323 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1324 if (target_type == TypeManager.char_type)
1325 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1326 } else if (expr_type == TypeManager.ushort_type){
1328 // From ushort to sbyte, byte, short, char
1330 if (target_type == TypeManager.sbyte_type)
1331 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1332 if (target_type == TypeManager.byte_type)
1333 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1334 if (target_type == TypeManager.short_type)
1335 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1336 if (target_type == TypeManager.char_type)
1337 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1338 } else if (expr_type == TypeManager.int32_type){
1340 // From int to sbyte, byte, short, ushort, uint, ulong, char
1342 if (target_type == TypeManager.sbyte_type)
1343 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1344 if (target_type == TypeManager.byte_type)
1345 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1346 if (target_type == TypeManager.short_type)
1347 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1348 if (target_type == TypeManager.ushort_type)
1349 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1350 if (target_type == TypeManager.uint32_type)
1351 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1352 if (target_type == TypeManager.uint64_type)
1353 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1354 if (target_type == TypeManager.char_type)
1355 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1356 } else if (expr_type == TypeManager.uint32_type){
1358 // From uint to sbyte, byte, short, ushort, int, char
1360 if (target_type == TypeManager.sbyte_type)
1361 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1362 if (target_type == TypeManager.byte_type)
1363 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1364 if (target_type == TypeManager.short_type)
1365 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1366 if (target_type == TypeManager.ushort_type)
1367 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1368 if (target_type == TypeManager.int32_type)
1369 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1370 if (target_type == TypeManager.char_type)
1371 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1372 } else if (expr_type == TypeManager.int64_type){
1374 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1376 if (target_type == TypeManager.sbyte_type)
1377 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1378 if (target_type == TypeManager.byte_type)
1379 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1380 if (target_type == TypeManager.short_type)
1381 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1382 if (target_type == TypeManager.ushort_type)
1383 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1384 if (target_type == TypeManager.int32_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1386 if (target_type == TypeManager.uint32_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1388 if (target_type == TypeManager.uint64_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1390 if (target_type == TypeManager.char_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1392 } else if (expr_type == TypeManager.uint64_type){
1394 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1396 if (target_type == TypeManager.sbyte_type)
1397 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1398 if (target_type == TypeManager.byte_type)
1399 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1400 if (target_type == TypeManager.short_type)
1401 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1402 if (target_type == TypeManager.ushort_type)
1403 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1404 if (target_type == TypeManager.int32_type)
1405 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1406 if (target_type == TypeManager.uint32_type)
1407 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1408 if (target_type == TypeManager.int64_type)
1409 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1410 if (target_type == TypeManager.char_type)
1411 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1412 } else if (expr_type == TypeManager.char_type){
1414 // From char to sbyte, byte, short
1416 if (target_type == TypeManager.sbyte_type)
1417 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1418 if (target_type == TypeManager.byte_type)
1419 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1420 if (target_type == TypeManager.short_type)
1421 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1422 } else if (expr_type == TypeManager.float_type){
1424 // From float to sbyte, byte, short,
1425 // ushort, int, uint, long, ulong, char
1428 if (target_type == TypeManager.sbyte_type)
1429 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1430 if (target_type == TypeManager.byte_type)
1431 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1432 if (target_type == TypeManager.short_type)
1433 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1434 if (target_type == TypeManager.ushort_type)
1435 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1436 if (target_type == TypeManager.int32_type)
1437 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1438 if (target_type == TypeManager.uint32_type)
1439 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1440 if (target_type == TypeManager.int64_type)
1441 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1442 if (target_type == TypeManager.uint64_type)
1443 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1444 if (target_type == TypeManager.char_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1446 if (target_type == TypeManager.decimal_type)
1447 return InternalTypeConstructor (ec, expr, target_type);
1448 } else if (expr_type == TypeManager.double_type){
1450 // From double to byte, byte, short,
1451 // ushort, int, uint, long, ulong,
1452 // char, float or decimal
1454 if (target_type == TypeManager.sbyte_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1456 if (target_type == TypeManager.byte_type)
1457 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1458 if (target_type == TypeManager.short_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1460 if (target_type == TypeManager.ushort_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1462 if (target_type == TypeManager.int32_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1464 if (target_type == TypeManager.uint32_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1466 if (target_type == TypeManager.int64_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1468 if (target_type == TypeManager.uint64_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1470 if (target_type == TypeManager.char_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1472 if (target_type == TypeManager.float_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1474 if (target_type == TypeManager.decimal_type)
1475 return InternalTypeConstructor (ec, expr, target_type);
1478 // decimal is taken care of by the op_Explicit methods.
1484 /// Returns whether an explicit reference conversion can be performed
1485 /// from source_type to target_type
1487 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1489 bool target_is_value_type = target_type.IsValueType;
1491 if (source_type == target_type)
1495 // From object to any reference type
1497 if (source_type == TypeManager.object_type && !target_is_value_type)
1501 // From any class S to any class-type T, provided S is a base class of T
1503 if (target_type.IsSubclassOf (source_type))
1507 // From any interface type S to any interface T provided S is not derived from T
1509 if (source_type.IsInterface && target_type.IsInterface){
1510 if (!target_type.IsSubclassOf (source_type))
1515 // From any class type S to any interface T, provides S is not sealed
1516 // and provided S does not implement T.
1518 if (target_type.IsInterface && !source_type.IsSealed &&
1519 !target_type.IsAssignableFrom (source_type))
1523 // From any interface-type S to to any class type T, provided T is not
1524 // sealed, or provided T implements S.
1526 if (source_type.IsInterface &&
1527 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1530 // From an array type S with an element type Se to an array type T with an
1531 // element type Te provided all the following are true:
1532 // * S and T differe only in element type, in other words, S and T
1533 // have the same number of dimensions.
1534 // * Both Se and Te are reference types
1535 // * An explicit referenc conversions exist from Se to Te
1537 if (source_type.IsArray && target_type.IsArray) {
1538 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1540 Type source_element_type = source_type.GetElementType ();
1541 Type target_element_type = target_type.GetElementType ();
1543 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1544 if (ExplicitReferenceConversionExists (source_element_type,
1545 target_element_type))
1551 // From System.Array to any array-type
1552 if (source_type == TypeManager.array_type &&
1553 target_type.IsSubclassOf (TypeManager.array_type)){
1558 // From System delegate to any delegate-type
1560 if (source_type == TypeManager.delegate_type &&
1561 target_type.IsSubclassOf (TypeManager.delegate_type))
1565 // From ICloneable to Array or Delegate types
1567 if (source_type == TypeManager.icloneable_type &&
1568 (target_type == TypeManager.array_type ||
1569 target_type == TypeManager.delegate_type))
1576 /// Implements Explicit Reference conversions
1578 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1580 Type source_type = source.Type;
1581 bool target_is_value_type = target_type.IsValueType;
1584 // From object to any reference type
1586 if (source_type == TypeManager.object_type && !target_is_value_type)
1587 return new ClassCast (source, target_type);
1591 // From any class S to any class-type T, provided S is a base class of T
1593 if (target_type.IsSubclassOf (source_type))
1594 return new ClassCast (source, target_type);
1597 // From any interface type S to any interface T provided S is not derived from T
1599 if (source_type.IsInterface && target_type.IsInterface){
1601 Type [] ifaces = source_type.GetInterfaces ();
1603 if (TypeManager.ImplementsInterface (source_type, target_type))
1606 return new ClassCast (source, target_type);
1610 // From any class type S to any interface T, provides S is not sealed
1611 // and provided S does not implement T.
1613 if (target_type.IsInterface && !source_type.IsSealed) {
1615 if (TypeManager.ImplementsInterface (source_type, target_type))
1618 return new ClassCast (source, target_type);
1623 // From any interface-type S to to any class type T, provided T is not
1624 // sealed, or provided T implements S.
1626 if (source_type.IsInterface) {
1628 if (target_type.IsSealed)
1631 if (TypeManager.ImplementsInterface (target_type, source_type))
1632 return new ClassCast (source, target_type);
1637 // From an array type S with an element type Se to an array type T with an
1638 // element type Te provided all the following are true:
1639 // * S and T differe only in element type, in other words, S and T
1640 // have the same number of dimensions.
1641 // * Both Se and Te are reference types
1642 // * An explicit referenc conversions exist from Se to Te
1644 if (source_type.IsArray && target_type.IsArray) {
1645 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1647 Type source_element_type = source_type.GetElementType ();
1648 Type target_element_type = target_type.GetElementType ();
1650 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1651 if (ExplicitReferenceConversionExists (source_element_type,
1652 target_element_type))
1653 return new ClassCast (source, target_type);
1658 // From System.Array to any array-type
1659 if (source_type == TypeManager.array_type &&
1660 target_type.IsSubclassOf (TypeManager.array_type)){
1661 return new ClassCast (source, target_type);
1665 // From System delegate to any delegate-type
1667 if (source_type == TypeManager.delegate_type &&
1668 target_type.IsSubclassOf (TypeManager.delegate_type))
1669 return new ClassCast (source, target_type);
1672 // From ICloneable to Array or Delegate types
1674 if (source_type == TypeManager.icloneable_type &&
1675 (target_type == TypeManager.array_type ||
1676 target_type == TypeManager.delegate_type))
1677 return new ClassCast (source, target_type);
1683 /// Performs an explicit conversion of the expression `expr' whose
1684 /// type is expr.Type to `target_type'.
1686 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1687 Type target_type, Location loc)
1689 Type expr_type = expr.Type;
1690 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1695 ne = ConvertNumericExplicit (ec, expr, target_type);
1700 // Unboxing conversion.
1702 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1703 return new UnboxCast (expr, target_type);
1708 if (expr is EnumLiteral) {
1709 Expression e = ((EnumLiteral) expr).Child;
1711 return ConvertImplicit (ec, e, target_type, loc);
1714 ne = ConvertReferenceExplicit (expr, target_type);
1718 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1722 error30 (loc, expr_type, target_type);
1727 /// Same as ConverExplicit, only it doesn't include user defined conversions
1729 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1730 Type target_type, Location l)
1732 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1737 ne = ConvertNumericExplicit (ec, expr, target_type);
1741 ne = ConvertReferenceExplicit (expr, target_type);
1745 error30 (l, expr.Type, target_type);
1749 static string ExprClassName (ExprClass c)
1752 case ExprClass.Invalid:
1754 case ExprClass.Value:
1756 case ExprClass.Variable:
1758 case ExprClass.Namespace:
1760 case ExprClass.Type:
1762 case ExprClass.MethodGroup:
1763 return "method group";
1764 case ExprClass.PropertyAccess:
1765 return "property access";
1766 case ExprClass.EventAccess:
1767 return "event access";
1768 case ExprClass.IndexerAccess:
1769 return "indexer access";
1770 case ExprClass.Nothing:
1773 throw new Exception ("Should not happen");
1777 /// Reports that we were expecting `expr' to be of class `expected'
1779 protected void report118 (Location loc, Expression expr, string expected)
1781 string kind = "Unknown";
1784 kind = ExprClassName (expr.ExprClass);
1786 Error (118, loc, "Expression denotes a `" + kind +
1787 "' where a `" + expected + "' was expected");
1791 /// This function tries to reduce the expression performing
1792 /// constant folding and common subexpression elimination
1794 static public Expression Reduce (EmitContext ec, Expression e)
1796 //Console.WriteLine ("Calling reduce");
1797 return e.Reduce (ec);
1800 static void error31 (Location l, string val, Type t)
1802 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1803 TypeManager.CSharpName (t));
1807 /// Converts the IntLiteral, UIntLiteral, LongLiteral or
1808 /// ULongLiteral into the integral target_type.
1810 /// This is used by the switch statement, so the domain
1811 /// of work is restricted to the literals above, and the
1812 /// targets are int32, uint32, char, byte, sbyte, ushort,
1813 /// short, uint64 and int64
1815 public static Literal ConvertIntLiteral (Literal l, Type target_type, Location loc)
1819 if (l.Type == target_type)
1823 // Make into one of the literals we handle, we dont really care
1824 // about this value as we will just return a few limited types
1826 if (l is EnumLiteral)
1827 l = ((EnumLiteral)l).WidenToCompilerLiteral ();
1829 if (l is IntLiteral){
1830 int v = ((IntLiteral) l).Value;
1832 if (target_type == TypeManager.uint32_type){
1834 return new UIntLiteral ((uint) v);
1835 } else if (target_type == TypeManager.char_type){
1836 if (v >= Char.MinValue && v <= Char.MaxValue)
1838 } else if (target_type == TypeManager.byte_type){
1839 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1841 } else if (target_type == TypeManager.sbyte_type){
1842 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1844 } else if (target_type == TypeManager.short_type){
1845 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1847 } else if (target_type == TypeManager.ushort_type){
1848 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1850 } else if (target_type == TypeManager.int64_type)
1851 return new LongLiteral (v);
1852 else if (target_type == TypeManager.uint64_type){
1854 return new ULongLiteral ((ulong) v);
1858 } else if (l is UIntLiteral){
1859 uint v = ((UIntLiteral) l).Value;
1861 if (target_type == TypeManager.int32_type){
1862 if (v <= Int32.MaxValue)
1863 return new IntLiteral ((int) v);
1864 } else if (target_type == TypeManager.char_type){
1865 if (v >= Char.MinValue && v <= Char.MaxValue)
1867 } else if (target_type == TypeManager.byte_type){
1868 if (v <= Byte.MaxValue)
1870 } else if (target_type == TypeManager.sbyte_type){
1871 if (v <= SByte.MaxValue)
1873 } else if (target_type == TypeManager.short_type){
1874 if (v <= UInt16.MaxValue)
1876 } else if (target_type == TypeManager.ushort_type){
1877 if (v <= UInt16.MaxValue)
1879 } else if (target_type == TypeManager.int64_type)
1880 return new LongLiteral (v);
1881 else if (target_type == TypeManager.uint64_type)
1882 return new ULongLiteral (v);
1884 } else if (l is LongLiteral){
1885 long v = ((LongLiteral) l).Value;
1887 if (target_type == TypeManager.int32_type){
1888 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1889 return new IntLiteral ((int) v);
1890 } else if (target_type == TypeManager.uint32_type){
1891 if (v >= 0 && v <= UInt32.MaxValue)
1892 return new UIntLiteral ((uint) v);
1893 } else if (target_type == TypeManager.char_type){
1894 if (v >= Char.MinValue && v <= Char.MaxValue)
1895 return new IntLiteral ((int) v);
1896 } else if (target_type == TypeManager.byte_type){
1897 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1898 return new IntLiteral ((int) v);
1899 } else if (target_type == TypeManager.sbyte_type){
1900 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1901 return new IntLiteral ((int) v);
1902 } else if (target_type == TypeManager.short_type){
1903 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1904 return new IntLiteral ((int) v);
1905 } else if (target_type == TypeManager.ushort_type){
1906 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1907 return new IntLiteral ((int) v);
1908 } else if (target_type == TypeManager.uint64_type){
1910 return new ULongLiteral ((ulong) v);
1913 } else if (l is ULongLiteral){
1914 ulong v = ((ULongLiteral) l).Value;
1916 if (target_type == TypeManager.int32_type){
1917 if (v <= Int32.MaxValue)
1918 return new IntLiteral ((int) v);
1919 } else if (target_type == TypeManager.uint32_type){
1920 if (v <= UInt32.MaxValue)
1921 return new UIntLiteral ((uint) v);
1922 } else if (target_type == TypeManager.char_type){
1923 if (v >= Char.MinValue && v <= Char.MaxValue)
1924 return new IntLiteral ((int) v);
1925 } else if (target_type == TypeManager.byte_type){
1926 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1927 return new IntLiteral ((int) v);
1928 } else if (target_type == TypeManager.sbyte_type){
1929 if (v <= (int) SByte.MaxValue)
1930 return new IntLiteral ((int) v);
1931 } else if (target_type == TypeManager.short_type){
1932 if (v <= UInt16.MaxValue)
1933 return new IntLiteral ((int) v);
1934 } else if (target_type == TypeManager.ushort_type){
1935 if (v <= UInt16.MaxValue)
1936 return new IntLiteral ((int) v);
1937 } else if (target_type == TypeManager.int64_type){
1938 if (v <= Int64.MaxValue)
1939 return new LongLiteral ((long) v);
1944 error31 (loc, s, target_type);
1951 /// This is just a base class for expressions that can
1952 /// appear on statements (invocations, object creation,
1953 /// assignments, post/pre increment and decrement). The idea
1954 /// being that they would support an extra Emition interface that
1955 /// does not leave a result on the stack.
1957 public abstract class ExpressionStatement : Expression {
1960 /// Requests the expression to be emitted in a `statement'
1961 /// context. This means that no new value is left on the
1962 /// stack after invoking this method (constrasted with
1963 /// Emit that will always leave a value on the stack).
1965 public abstract void EmitStatement (EmitContext ec);
1969 /// This kind of cast is used to encapsulate the child
1970 /// whose type is child.Type into an expression that is
1971 /// reported to return "return_type". This is used to encapsulate
1972 /// expressions which have compatible types, but need to be dealt
1973 /// at higher levels with.
1975 /// For example, a "byte" expression could be encapsulated in one
1976 /// of these as an "unsigned int". The type for the expression
1977 /// would be "unsigned int".
1980 public class EmptyCast : Expression {
1981 protected Expression child;
1983 public EmptyCast (Expression child, Type return_type)
1985 ExprClass = child.ExprClass;
1990 public override Expression DoResolve (EmitContext ec)
1992 // This should never be invoked, we are born in fully
1993 // initialized state.
1998 public override void Emit (EmitContext ec)
2006 /// This class is used to wrap literals which belong inside Enums
2008 public class EnumLiteral : Literal {
2009 public Expression Child;
2011 public EnumLiteral (Expression child, Type enum_type)
2013 ExprClass = child.ExprClass;
2018 public override Expression DoResolve (EmitContext ec)
2020 // This should never be invoked, we are born in fully
2021 // initialized state.
2026 public override void Emit (EmitContext ec)
2031 public override object GetValue ()
2033 return ((Literal) Child).GetValue ();
2037 // Converts from one of the valid underlying types for an enumeration
2038 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2039 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2041 public Literal WidenToCompilerLiteral ()
2043 Type t = Child.Type.UnderlyingSystemType;
2044 object v = ((Literal) Child).GetValue ();;
2046 if (t == TypeManager.int32_type)
2047 return new IntLiteral ((int) v);
2048 if (t == TypeManager.uint32_type)
2049 return new UIntLiteral ((uint) v);
2050 if (t == TypeManager.int64_type)
2051 return new LongLiteral ((long) v);
2052 if (t == TypeManager.uint64_type)
2053 return new ULongLiteral ((ulong) v);
2054 if (t == TypeManager.short_type)
2055 return new IntLiteral ((short) v);
2056 if (t == TypeManager.ushort_type)
2057 return new UIntLiteral ((ushort) v);
2058 if (t == TypeManager.byte_type)
2059 return new UIntLiteral ((byte) v);
2060 if (t == TypeManager.sbyte_type)
2061 return new IntLiteral ((sbyte) v);
2063 throw new Exception ("Invalid enumeration underlying type: " + t);
2066 public override string AsString ()
2068 return ((Literal) Child).AsString ();
2073 /// This kind of cast is used to encapsulate Value Types in objects.
2075 /// The effect of it is to box the value type emitted by the previous
2078 public class BoxedCast : EmptyCast {
2080 public BoxedCast (Expression expr)
2081 : base (expr, TypeManager.object_type)
2085 public override Expression DoResolve (EmitContext ec)
2087 // This should never be invoked, we are born in fully
2088 // initialized state.
2093 public override void Emit (EmitContext ec)
2096 ec.ig.Emit (OpCodes.Box, child.Type);
2100 public class UnboxCast : EmptyCast {
2101 public UnboxCast (Expression expr, Type return_type)
2102 : base (expr, return_type)
2106 public override Expression DoResolve (EmitContext ec)
2108 // This should never be invoked, we are born in fully
2109 // initialized state.
2114 public override void Emit (EmitContext ec)
2117 ILGenerator ig = ec.ig;
2120 ig.Emit (OpCodes.Unbox, t);
2123 // Load the object from the pointer
2125 if (t == TypeManager.int32_type)
2126 ig.Emit (OpCodes.Ldind_I4);
2127 else if (t == TypeManager.uint32_type)
2128 ig.Emit (OpCodes.Ldind_U4);
2129 else if (t == TypeManager.short_type)
2130 ig.Emit (OpCodes.Ldind_I2);
2131 else if (t == TypeManager.ushort_type)
2132 ig.Emit (OpCodes.Ldind_U2);
2133 else if (t == TypeManager.char_type)
2134 ig.Emit (OpCodes.Ldind_U2);
2135 else if (t == TypeManager.byte_type)
2136 ig.Emit (OpCodes.Ldind_U1);
2137 else if (t == TypeManager.sbyte_type)
2138 ig.Emit (OpCodes.Ldind_I1);
2139 else if (t == TypeManager.uint64_type)
2140 ig.Emit (OpCodes.Ldind_I8);
2141 else if (t == TypeManager.int64_type)
2142 ig.Emit (OpCodes.Ldind_I8);
2143 else if (t == TypeManager.float_type)
2144 ig.Emit (OpCodes.Ldind_R4);
2145 else if (t == TypeManager.double_type)
2146 ig.Emit (OpCodes.Ldind_R8);
2147 else if (t == TypeManager.bool_type)
2148 ig.Emit (OpCodes.Ldind_I1);
2149 else if (t == TypeManager.intptr_type)
2150 ig.Emit (OpCodes.Ldind_I);
2152 ig.Emit (OpCodes.Ldobj, t);
2157 /// This is used to perform explicit numeric conversions.
2159 /// Explicit numeric conversions might trigger exceptions in a checked
2160 /// context, so they should generate the conv.ovf opcodes instead of
2163 public class ConvCast : EmptyCast {
2164 public enum Mode : byte {
2165 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2167 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2168 U2_I1, U2_U1, U2_I2, U2_CH,
2169 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2170 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2171 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2172 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2173 CH_I1, CH_U1, CH_I2,
2174 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2175 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2180 public ConvCast (Expression child, Type return_type, Mode m)
2181 : base (child, return_type)
2186 public override Expression DoResolve (EmitContext ec)
2188 // This should never be invoked, we are born in fully
2189 // initialized state.
2194 public override void Emit (EmitContext ec)
2196 ILGenerator ig = ec.ig;
2202 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2203 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2204 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2205 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2206 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2208 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2209 case Mode.U1_CH: /* nothing */ break;
2211 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2212 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2213 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2214 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2215 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2216 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2218 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2219 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2220 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2221 case Mode.U2_CH: /* nothing */ break;
2223 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2224 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2225 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2226 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2227 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2228 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2229 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2231 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2232 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2233 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2234 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2235 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2236 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2238 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2239 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2240 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2241 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2242 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2243 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2244 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2245 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2247 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2248 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2249 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2250 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2251 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2252 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2253 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2254 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2256 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2257 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2258 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2260 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2261 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2262 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2263 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2264 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2265 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2266 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2267 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2268 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2270 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2271 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2272 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2273 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2274 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2275 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2276 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2277 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2278 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2279 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2283 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2284 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2285 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2286 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2287 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2289 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2290 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2292 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2293 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2294 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2295 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2296 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2297 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2299 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2300 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2301 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2302 case Mode.U2_CH: /* nothing */ break;
2304 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2305 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2306 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2307 case Mode.I4_U4: /* nothing */ break;
2308 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2309 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2310 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2312 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2313 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2314 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2315 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2316 case Mode.U4_I4: /* nothing */ break;
2317 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2319 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2320 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2321 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2322 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2323 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2324 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2325 case Mode.I8_U8: /* nothing */ break;
2326 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2328 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2329 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2330 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2331 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2332 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2333 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2334 case Mode.U8_I8: /* nothing */ break;
2335 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2337 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2338 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2339 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2341 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2342 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2343 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2344 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2345 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2346 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2347 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2348 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2349 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2351 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2352 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2353 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2354 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2355 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2356 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2357 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2358 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2359 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2360 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2366 public class OpcodeCast : EmptyCast {
2370 public OpcodeCast (Expression child, Type return_type, OpCode op)
2371 : base (child, return_type)
2375 second_valid = false;
2378 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2379 : base (child, return_type)
2384 second_valid = true;
2387 public override Expression DoResolve (EmitContext ec)
2389 // This should never be invoked, we are born in fully
2390 // initialized state.
2395 public override void Emit (EmitContext ec)
2406 /// This kind of cast is used to encapsulate a child and cast it
2407 /// to the class requested
2409 public class ClassCast : EmptyCast {
2410 public ClassCast (Expression child, Type return_type)
2411 : base (child, return_type)
2416 public override Expression DoResolve (EmitContext ec)
2418 // This should never be invoked, we are born in fully
2419 // initialized state.
2424 public override void Emit (EmitContext ec)
2428 ec.ig.Emit (OpCodes.Castclass, type);
2434 /// SimpleName expressions are initially formed of a single
2435 /// word and it only happens at the beginning of the expression.
2439 /// The expression will try to be bound to a Field, a Method
2440 /// group or a Property. If those fail we pass the name to our
2441 /// caller and the SimpleName is compounded to perform a type
2442 /// lookup. The idea behind this process is that we want to avoid
2443 /// creating a namespace map from the assemblies, as that requires
2444 /// the GetExportedTypes function to be called and a hashtable to
2445 /// be constructed which reduces startup time. If later we find
2446 /// that this is slower, we should create a `NamespaceExpr' expression
2447 /// that fully participates in the resolution process.
2449 /// For example `System.Console.WriteLine' is decomposed into
2450 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2452 /// The first SimpleName wont produce a match on its own, so it will
2454 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2456 /// System.Console will produce a TypeExpr match.
2458 /// The downside of this is that we might be hitting `LookupType' too many
2459 /// times with this scheme.
2461 public class SimpleName : Expression {
2462 public readonly string Name;
2463 public readonly Location Location;
2465 public SimpleName (string name, Location l)
2471 public static void Error120 (Location l, string name)
2475 "An object reference is required " +
2476 "for the non-static field `"+name+"'");
2480 // Checks whether we are trying to access an instance
2481 // property, method or field from a static body.
2483 Expression MemberStaticCheck (Expression e)
2485 if (e is FieldExpr){
2486 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2489 Error120 (Location, Name);
2492 } else if (e is MethodGroupExpr){
2493 MethodGroupExpr mg = (MethodGroupExpr) e;
2495 if (!mg.RemoveInstanceMethods ()){
2496 Error120 (Location, mg.Methods [0].Name);
2500 } else if (e is PropertyExpr){
2501 if (!((PropertyExpr) e).IsStatic){
2502 Error120 (Location, Name);
2511 // 7.5.2: Simple Names.
2513 // Local Variables and Parameters are handled at
2514 // parse time, so they never occur as SimpleNames.
2516 public override Expression DoResolve (EmitContext ec)
2521 // Stage 1: Performed by the parser (binding to locals or parameters).
2525 // Stage 2: Lookup members
2527 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2530 // Stage 3: Lookup symbol in the various namespaces.
2535 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
2536 return new TypeExpr (t);
2539 // Stage 3 part b: Lookup up if we are an alias to a type
2542 // Since we are cheating: we only do the Alias lookup for
2543 // namespaces if the name does not include any dots in it
2546 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2547 // System.Console.WriteLine (Name + " --> " + alias_value);
2548 if ((t = ec.TypeContainer.LookupType (alias_value, true)) != null)
2549 return new TypeExpr (t);
2551 // we have alias value, but it isn't Type, so try if it's namespace
2552 return new SimpleName (alias_value, Location);
2555 // No match, maybe our parent can compose us
2556 // into something meaningful.
2561 // Step 2, continues here.
2565 if (e is FieldExpr){
2566 FieldExpr fe = (FieldExpr) e;
2568 if (!fe.FieldInfo.IsStatic){
2569 This t = new This (Location.Null);
2571 fe.InstanceExpression = t.DoResolve (ec);
2574 FieldInfo fi = fe.FieldInfo;
2576 if (fi is FieldBuilder) {
2577 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2580 object o = c.LookupConstantValue (ec);
2581 Expression l = Literalize (o, fi.FieldType);
2583 return ((Literal) l);
2589 return MemberStaticCheck (e);
2594 public override void Emit (EmitContext ec)
2597 // If this is ever reached, then we failed to
2598 // find the name as a namespace
2601 Error (103, Location, "The name `" + Name +
2602 "' does not exist in the class `" +
2603 ec.TypeContainer.Name + "'");
2608 /// Fully resolved expression that evaluates to a type
2610 public class TypeExpr : Expression {
2611 public TypeExpr (Type t)
2614 eclass = ExprClass.Type;
2617 override public Expression DoResolve (EmitContext ec)
2622 override public void Emit (EmitContext ec)
2624 throw new Exception ("Implement me");
2629 /// MethodGroup Expression.
2631 /// This is a fully resolved expression that evaluates to a type
2633 public class MethodGroupExpr : Expression {
2634 public MethodBase [] Methods;
2635 Expression instance_expression = null;
2637 public MethodGroupExpr (MemberInfo [] mi)
2639 Methods = new MethodBase [mi.Length];
2640 mi.CopyTo (Methods, 0);
2641 eclass = ExprClass.MethodGroup;
2644 public MethodGroupExpr (ArrayList l)
2646 Methods = new MethodBase [l.Count];
2648 l.CopyTo (Methods, 0);
2649 eclass = ExprClass.MethodGroup;
2653 // `A method group may have associated an instance expression'
2655 public Expression InstanceExpression {
2657 return instance_expression;
2661 instance_expression = value;
2665 override public Expression DoResolve (EmitContext ec)
2670 override public void Emit (EmitContext ec)
2672 throw new Exception ("This should never be reached");
2675 bool RemoveMethods (bool keep_static)
2677 ArrayList smethods = new ArrayList ();
2678 int top = Methods.Length;
2681 for (i = 0; i < top; i++){
2682 MethodBase mb = Methods [i];
2684 if (mb.IsStatic == keep_static)
2688 if (smethods.Count == 0)
2691 Methods = new MethodBase [smethods.Count];
2692 smethods.CopyTo (Methods, 0);
2698 /// Removes any instance methods from the MethodGroup, returns
2699 /// false if the resulting set is empty.
2701 public bool RemoveInstanceMethods ()
2703 return RemoveMethods (true);
2707 /// Removes any static methods from the MethodGroup, returns
2708 /// false if the resulting set is empty.
2710 public bool RemoveStaticMethods ()
2712 return RemoveMethods (false);
2717 /// Fully resolved expression that evaluates to a Field
2719 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2720 public readonly FieldInfo FieldInfo;
2721 public Expression InstanceExpression;
2724 public FieldExpr (FieldInfo fi, Location l)
2727 eclass = ExprClass.Variable;
2728 type = fi.FieldType;
2732 override public Expression DoResolve (EmitContext ec)
2734 if (!FieldInfo.IsStatic){
2735 if (InstanceExpression == null){
2736 throw new Exception ("non-static FieldExpr without instance var\n" +
2737 "You have to assign the Instance variable\n" +
2738 "Of the FieldExpr to set this\n");
2741 InstanceExpression = InstanceExpression.Resolve (ec);
2742 if (InstanceExpression == null)
2750 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2752 Expression e = DoResolve (ec);
2757 if (!FieldInfo.IsInitOnly)
2761 // InitOnly fields can only be assigned in constructors
2764 if (ec.IsConstructor)
2767 Report.Error (191, loc,
2768 "Readonly field can not be assigned outside " +
2769 "of constructor or variable initializer");
2774 override public void Emit (EmitContext ec)
2776 ILGenerator ig = ec.ig;
2778 if (FieldInfo.IsStatic)
2779 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2781 InstanceExpression.Emit (ec);
2783 ig.Emit (OpCodes.Ldfld, FieldInfo);
2787 public void EmitAssign (EmitContext ec, Expression source)
2789 bool is_static = FieldInfo.IsStatic;
2792 Expression instance = InstanceExpression;
2794 if (instance.Type.IsValueType){
2795 if (instance is IMemoryLocation){
2796 IMemoryLocation ml = (IMemoryLocation) instance;
2800 throw new Exception ("The " + instance + " of type " + Type+
2801 "represents a ValueType and does not " +
2802 "implement IMemoryLocation");
2809 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2811 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2815 public void AddressOf (EmitContext ec)
2817 if (FieldInfo.IsStatic)
2818 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2820 InstanceExpression.Emit (ec);
2821 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2827 /// Expression that evaluates to a Property. The Assign class
2828 /// might set the `Value' expression if we are in an assignment.
2830 /// This is not an LValue because we need to re-write the expression, we
2831 /// can not take data from the stack and store it.
2833 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2834 public readonly PropertyInfo PropertyInfo;
2835 public readonly bool IsStatic;
2836 MethodInfo [] Accessors;
2839 Expression instance_expr;
2841 public PropertyExpr (PropertyInfo pi, Location l)
2844 eclass = ExprClass.PropertyAccess;
2847 Accessors = TypeManager.GetAccessors (pi);
2849 if (Accessors != null)
2850 for (int i = 0; i < Accessors.Length; i++){
2851 if (Accessors [i] != null)
2852 if (Accessors [i].IsStatic)
2856 Accessors = new MethodInfo [2];
2858 type = pi.PropertyType;
2862 // The instance expression associated with this expression
2864 public Expression InstanceExpression {
2866 instance_expr = value;
2870 return instance_expr;
2874 public bool VerifyAssignable ()
2876 if (!PropertyInfo.CanWrite){
2877 Report.Error (200, loc,
2878 "The property `" + PropertyInfo.Name +
2879 "' can not be assigned to, as it has not set accessor");
2886 override public Expression DoResolve (EmitContext ec)
2888 if (!PropertyInfo.CanRead){
2889 Report.Error (154, loc,
2890 "The property `" + PropertyInfo.Name +
2891 "' can not be used in " +
2892 "this context because it lacks a get accessor");
2896 type = PropertyInfo.PropertyType;
2901 override public void Emit (EmitContext ec)
2903 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2908 // Implements the IAssignMethod interface for assignments
2910 public void EmitAssign (EmitContext ec, Expression source)
2912 Argument arg = new Argument (source, Argument.AType.Expression);
2913 ArrayList args = new ArrayList ();
2916 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2919 override public void EmitStatement (EmitContext ec)
2922 ec.ig.Emit (OpCodes.Pop);
2927 /// Fully resolved expression that evaluates to a Expression
2929 public class EventExpr : Expression {
2930 public readonly EventInfo EventInfo;
2933 public EventExpr (EventInfo ei, Location loc)
2937 eclass = ExprClass.EventAccess;
2940 override public Expression DoResolve (EmitContext ec)
2942 // We are born in resolved state.
2946 override public void Emit (EmitContext ec)
2948 throw new Exception ("Implement me");
2949 // FIXME: Implement.