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 public ExprClass eclass;
68 /// Utility wrapper routine for Error, just to beautify the code
70 static protected void Error (int error, string s)
72 Report.Error (error, s);
75 static protected void Error (int error, Location loc, string s)
77 Report.Error (error, loc, s);
81 /// Utility wrapper routine for Warning, just to beautify the code
83 static protected void Warning (int warning, string s)
85 Report.Warning (warning, s);
88 static public void error30 (Location loc, Type source, Type target)
90 Report.Error (30, loc, "Cannot convert type '" +
91 TypeManager.CSharpName (source) + "' to '" +
92 TypeManager.CSharpName (target) + "'");
96 /// Performs semantic analysis on the Expression
100 /// The Resolve method is invoked to perform the semantic analysis
103 /// The return value is an expression (it can be the
104 /// same expression in some cases) or a new
105 /// expression that better represents this node.
107 /// For example, optimizations of Unary (LiteralInt)
108 /// would return a new LiteralInt with a negated
111 /// If there is an error during semantic analysis,
112 /// then an error should be reported (using Report)
113 /// and a null value should be returned.
115 /// There are two side effects expected from calling
116 /// Resolve(): the the field variable "eclass" should
117 /// be set to any value of the enumeration
118 /// `ExprClass' and the type variable should be set
119 /// to a valid type (this is the type of the
122 public abstract Expression DoResolve (EmitContext ec);
124 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
126 return DoResolve (ec);
130 /// Resolves an expression and performs semantic analysis on it.
134 /// Currently Resolve wraps DoResolve to perform sanity
135 /// checking and assertion checking on what we expect from Resolve.
137 public Expression Resolve (EmitContext ec)
139 Expression e = DoResolve (ec);
142 if (e is SimpleName){
143 SimpleName s = (SimpleName) e;
147 "The name `" + s.Name + "' could not be found in `" +
148 ec.TypeContainer.Name + "'");
152 if (e.eclass == ExprClass.Invalid)
153 throw new Exception ("Expression " + e.GetType () +
154 " ExprClass is Invalid after resolve");
156 if (e.eclass != ExprClass.MethodGroup)
158 throw new Exception (
159 "Expression " + e.GetType () +
160 " did not set its type after Resolve\n" +
161 "called from: " + this.GetType ());
168 /// Performs expression resolution and semantic analysis, but
169 /// allows SimpleNames to be returned.
173 /// This is used by MemberAccess to construct long names that can not be
174 /// partially resolved (namespace-qualified names for example).
176 public Expression ResolveWithSimpleName (EmitContext ec)
180 if (this is SimpleName)
181 e = ((SimpleName) this).DoResolveAllowStatic (ec);
189 if (e.eclass == ExprClass.Invalid)
190 throw new Exception ("Expression " + e +
191 " ExprClass is Invalid after resolve");
193 if (e.eclass != ExprClass.MethodGroup)
195 throw new Exception ("Expression " + e +
196 " did not set its type after Resolve");
203 /// Resolves an expression for LValue assignment
207 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
208 /// checking and assertion checking on what we expect from Resolve
210 public Expression ResolveLValue (EmitContext ec, Expression right_side)
212 Expression e = DoResolveLValue (ec, right_side);
215 if (e is SimpleName){
216 SimpleName s = (SimpleName) e;
220 "The name `" + s.Name + "' could not be found in `" +
221 ec.TypeContainer.Name + "'");
225 if (e.eclass == ExprClass.Invalid)
226 throw new Exception ("Expression " + e +
227 " ExprClass is Invalid after resolve");
229 if (e.eclass != ExprClass.MethodGroup)
231 throw new Exception ("Expression " + e +
232 " did not set its type after Resolve");
239 /// Emits the code for the expression
243 /// The Emit method is invoked to generate the code
244 /// for the expression.
246 public abstract void Emit (EmitContext ec);
249 /// Protected constructor. Only derivate types should
250 /// be able to be created
253 protected Expression ()
255 eclass = ExprClass.Invalid;
260 /// Returns a literalized version of a literal FieldInfo
264 /// The possible return values are:
265 /// IntConstant, UIntConstant
266 /// LongLiteral, ULongConstant
267 /// FloatConstant, DoubleConstant
270 /// The value returned is already resolved.
272 public static Constant Constantify (object v, Type t)
274 if (t == TypeManager.int32_type)
275 return new IntConstant ((int) v);
276 else if (t == TypeManager.uint32_type)
277 return new UIntConstant ((uint) v);
278 else if (t == TypeManager.int64_type)
279 return new LongConstant ((long) v);
280 else if (t == TypeManager.uint64_type)
281 return new ULongConstant ((ulong) v);
282 else if (t == TypeManager.float_type)
283 return new FloatConstant ((float) v);
284 else if (t == TypeManager.double_type)
285 return new DoubleConstant ((double) v);
286 else if (t == TypeManager.string_type)
287 return new StringConstant ((string) v);
288 else if (t == TypeManager.short_type)
289 return new ShortConstant ((short)v);
290 else if (t == TypeManager.ushort_type)
291 return new UShortConstant ((ushort)v);
292 else if (t == TypeManager.sbyte_type)
293 return new SByteConstant (((sbyte)v));
294 else if (t == TypeManager.byte_type)
295 return new ByteConstant ((byte)v);
296 else if (t == TypeManager.char_type)
297 return new CharConstant ((char)v);
298 else if (TypeManager.IsEnumType (t)){
299 Expression e = Constantify (v, v.GetType ());
301 return new EnumConstant ((Constant) e, t);
303 throw new Exception ("Unknown type for constant (" + t +
308 /// Returns a fully formed expression after a MemberLookup
310 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
313 return new EventExpr ((EventInfo) mi, loc);
314 else if (mi is FieldInfo)
315 return new FieldExpr ((FieldInfo) mi, loc);
316 else if (mi is PropertyInfo)
317 return new PropertyExpr ((PropertyInfo) mi, loc);
318 else if (mi is Type){
319 return new TypeExpr ((System.Type) mi);
326 // We copy methods from `new_members' into `target_list' if the signature
327 // for the method from in the new list does not exist in the target_list
329 // The name is assumed to be the same.
331 static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
333 if (target_list == null){
334 target_list = new ArrayList ();
336 target_list.AddRange (new_members);
340 MemberInfo [] target_array = new MemberInfo [target_list.Count];
341 target_list.CopyTo (target_array, 0);
343 foreach (MemberInfo mi in new_members){
344 MethodBase new_method = (MethodBase) mi;
345 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
347 foreach (MethodBase method in target_array){
348 Type [] old_args = TypeManager.GetArgumentTypes (method);
349 int new_count = new_args.Length;
350 int old_count = old_args.Length;
352 if (new_count != old_count){
353 target_list.Add (method);
357 for (int i = 0; i < old_count; i++){
358 if (old_args [i] == new_args [i])
360 target_list.Add (method);
369 // FIXME: Probably implement a cache for (t,name,current_access_set)?
371 // This code could use some optimizations, but we need to do some
372 // measurements. For example, we could use a delegate to `flag' when
373 // something can not any longer be a method-group (because it is something
377 // If the return value is an Array, then it is an array of
380 // If the return value is an MemberInfo, it is anything, but a Method
384 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
385 // the arguments here and have MemberLookup return only the methods that
386 // match the argument count/type, unlike we are doing now (we delay this
389 // This is so we can catch correctly attempts to invoke instance methods
390 // from a static body (scan for error 120 in ResolveSimpleName).
393 // FIXME: Potential optimization, have a static ArrayList
396 public static Expression MemberLookup (EmitContext ec, Type t, string name,
397 MemberTypes mt, BindingFlags bf, Location loc)
399 Type source_type = ec.ContainerType;
401 if (source_type == t || source_type.IsSubclassOf (t))
402 bf |= BindingFlags.NonPublic;
405 // Lookup for members starting in the type requested and going
406 // up the hierarchy until a match is found.
408 // As soon as a non-method match is found, we return.
410 // If methods are found though, then the search proceeds scanning
411 // for more public methods in the hierarchy with signatures that
412 // do not match any of the signatures found so far.
414 ArrayList method_list = null;
415 Type current_type = t;
416 bool searching = true;
420 mi = RootContext.TypeManager.FindMembers (
421 current_type, mt, bf | BindingFlags.DeclaredOnly,
422 System.Type.FilterName, name);
424 if (current_type == TypeManager.object_type)
427 current_type = current_type.BaseType;
430 // This happens with interfaces, they have a null
433 if (current_type == null)
440 int count = mi.Length;
445 if (count == 1 && !(mi [0] is MethodBase))
446 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
449 // We found methods, turn the search into "method scan"
452 method_list = CopyNewMethods (method_list, mi);
453 mt &= (MemberTypes.Method | MemberTypes.Constructor);
456 if (method_list != null && method_list.Count > 0)
457 return new MethodGroupExpr (method_list);
462 public const MemberTypes AllMemberTypes =
463 MemberTypes.Constructor |
467 MemberTypes.NestedType |
468 MemberTypes.Property;
470 public const BindingFlags AllBindingFlags =
471 BindingFlags.Public |
472 BindingFlags.Static |
473 BindingFlags.Instance;
475 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
477 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
481 /// This is a wrapper for MemberLookup that is not used to "probe", but
482 /// to find a final definition. If the final definition is not found, we
483 /// look for private members and display a useful debugging message if we
486 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
491 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
496 e = MemberLookup (ec, t, name, AllMemberTypes,
497 AllBindingFlags | BindingFlags.NonPublic, loc);
500 117, loc, "`" + t + "' does not contain a definition " +
501 "for `" + name + "'");
504 122, loc, "`" + t + "." + name +
505 "' is inaccessible due to its protection level");
511 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
513 Type expr_type = expr.Type;
515 if (target_type == TypeManager.object_type) {
516 if (expr_type.IsClass)
517 return new EmptyCast (expr, target_type);
518 if (expr_type.IsValueType)
519 return new BoxedCast (expr);
520 } else if (expr_type.IsSubclassOf (target_type)) {
521 return new EmptyCast (expr, target_type);
523 // from the null type to any reference-type.
524 if (expr is NullLiteral && !target_type.IsValueType)
525 return new EmptyCast (expr, target_type);
527 // from any class-type S to any interface-type T.
528 if (expr_type.IsClass && target_type.IsInterface) {
529 if (TypeManager.ImplementsInterface (expr_type, target_type))
530 return new EmptyCast (expr, target_type);
535 // from any interface type S to interface-type T.
536 if (expr_type.IsInterface && target_type.IsInterface) {
538 if (TypeManager.ImplementsInterface (expr_type, target_type))
539 return new EmptyCast (expr, target_type);
544 // from an array-type S to an array-type of type T
545 if (expr_type.IsArray && target_type.IsArray) {
546 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
548 Type expr_element_type = expr_type.GetElementType ();
549 Type target_element_type = target_type.GetElementType ();
551 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
552 if (StandardConversionExists (expr_element_type,
553 target_element_type))
554 return new EmptyCast (expr, target_type);
559 // from an array-type to System.Array
560 if (expr_type.IsArray && target_type == TypeManager.array_type)
561 return new EmptyCast (expr, target_type);
563 // from any delegate type to System.Delegate
564 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
565 target_type == TypeManager.delegate_type)
566 return new EmptyCast (expr, target_type);
568 // from any array-type or delegate type into System.ICloneable.
569 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
570 if (target_type == TypeManager.icloneable_type)
571 return new EmptyCast (expr, target_type);
581 /// Handles expressions like this: decimal d; d = 1;
582 /// and changes them into: decimal d; d = new System.Decimal (1);
584 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
586 ArrayList args = new ArrayList ();
588 args.Add (new Argument (expr, Argument.AType.Expression));
590 Expression ne = new New (target.FullName, args,
593 return ne.Resolve (ec);
597 /// Implicit Numeric Conversions.
599 /// expr is the expression to convert, returns a new expression of type
600 /// target_type or null if an implicit conversion is not possible.
602 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
603 Type target_type, Location loc)
605 Type expr_type = expr.Type;
608 // Attempt to do the implicit constant expression conversions
610 if (expr is IntConstant){
613 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
617 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
619 // Try the implicit constant expression conversion
620 // from long to ulong, instead of a nice routine,
623 long v = ((LongConstant) expr).Value;
625 return new ULongConstant ((ulong) v);
628 if (expr_type == TypeManager.sbyte_type){
630 // From sbyte to short, int, long, float, double.
632 if (target_type == TypeManager.int32_type)
633 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
634 if (target_type == TypeManager.int64_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
636 if (target_type == TypeManager.double_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
638 if (target_type == TypeManager.float_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
640 if (target_type == TypeManager.short_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
642 if (target_type == TypeManager.decimal_type)
643 return InternalTypeConstructor (ec, expr, target_type);
644 } else if (expr_type == TypeManager.byte_type){
646 // From byte to short, ushort, int, uint, long, ulong, float, double
648 if ((target_type == TypeManager.short_type) ||
649 (target_type == TypeManager.ushort_type) ||
650 (target_type == TypeManager.int32_type) ||
651 (target_type == TypeManager.uint32_type))
652 return new EmptyCast (expr, target_type);
654 if (target_type == TypeManager.uint64_type)
655 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
656 if (target_type == TypeManager.int64_type)
657 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
658 if (target_type == TypeManager.float_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
660 if (target_type == TypeManager.double_type)
661 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
662 if (target_type == TypeManager.decimal_type)
663 return InternalTypeConstructor (ec, expr, target_type);
664 } else if (expr_type == TypeManager.short_type){
666 // From short to int, long, float, double
668 if (target_type == TypeManager.int32_type)
669 return new EmptyCast (expr, target_type);
670 if (target_type == TypeManager.int64_type)
671 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
672 if (target_type == TypeManager.double_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
674 if (target_type == TypeManager.float_type)
675 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
676 if (target_type == TypeManager.decimal_type)
677 return InternalTypeConstructor (ec, expr, target_type);
678 } else if (expr_type == TypeManager.ushort_type){
680 // From ushort to int, uint, long, ulong, float, double
682 if (target_type == TypeManager.uint32_type)
683 return new EmptyCast (expr, target_type);
685 if (target_type == TypeManager.uint64_type)
686 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
687 if (target_type == TypeManager.int32_type)
688 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
689 if (target_type == TypeManager.int64_type)
690 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
691 if (target_type == TypeManager.double_type)
692 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
693 if (target_type == TypeManager.float_type)
694 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
695 if (target_type == TypeManager.decimal_type)
696 return InternalTypeConstructor (ec, expr, target_type);
697 } else if (expr_type == TypeManager.int32_type){
699 // From int to long, float, double
701 if (target_type == TypeManager.int64_type)
702 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
703 if (target_type == TypeManager.double_type)
704 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
705 if (target_type == TypeManager.float_type)
706 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
707 if (target_type == TypeManager.decimal_type)
708 return InternalTypeConstructor (ec, expr, target_type);
709 } else if (expr_type == TypeManager.uint32_type){
711 // From uint to long, ulong, float, double
713 if (target_type == TypeManager.int64_type)
714 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
715 if (target_type == TypeManager.uint64_type)
716 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
717 if (target_type == TypeManager.double_type)
718 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
720 if (target_type == TypeManager.float_type)
721 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
723 if (target_type == TypeManager.decimal_type)
724 return InternalTypeConstructor (ec, expr, target_type);
725 } else if ((expr_type == TypeManager.uint64_type) ||
726 (expr_type == TypeManager.int64_type)){
728 // From long/ulong to float, double
730 if (target_type == TypeManager.double_type)
731 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
733 if (target_type == TypeManager.float_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
736 if (target_type == TypeManager.decimal_type)
737 return InternalTypeConstructor (ec, expr, target_type);
738 } else if (expr_type == TypeManager.char_type){
740 // From char to ushort, int, uint, long, ulong, float, double
742 if ((target_type == TypeManager.ushort_type) ||
743 (target_type == TypeManager.int32_type) ||
744 (target_type == TypeManager.uint32_type))
745 return new EmptyCast (expr, target_type);
746 if (target_type == TypeManager.uint64_type)
747 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
748 if (target_type == TypeManager.int64_type)
749 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
750 if (target_type == TypeManager.float_type)
751 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
752 if (target_type == TypeManager.double_type)
753 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
754 if (target_type == TypeManager.decimal_type)
755 return InternalTypeConstructor (ec, expr, target_type);
756 } else if (expr_type == TypeManager.float_type){
760 if (target_type == TypeManager.double_type)
761 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
768 /// Determines if a standard implicit conversion exists from
769 /// expr_type to target_type
771 public static bool StandardConversionExists (Type expr_type, Type target_type)
773 if (expr_type == target_type)
776 // First numeric conversions
778 if (expr_type == TypeManager.sbyte_type){
780 // From sbyte to short, int, long, float, double.
782 if ((target_type == TypeManager.int32_type) ||
783 (target_type == TypeManager.int64_type) ||
784 (target_type == TypeManager.double_type) ||
785 (target_type == TypeManager.float_type) ||
786 (target_type == TypeManager.short_type) ||
787 (target_type == TypeManager.decimal_type))
790 } else if (expr_type == TypeManager.byte_type){
792 // From byte to short, ushort, int, uint, long, ulong, float, double
794 if ((target_type == TypeManager.short_type) ||
795 (target_type == TypeManager.ushort_type) ||
796 (target_type == TypeManager.int32_type) ||
797 (target_type == TypeManager.uint32_type) ||
798 (target_type == TypeManager.uint64_type) ||
799 (target_type == TypeManager.int64_type) ||
800 (target_type == TypeManager.float_type) ||
801 (target_type == TypeManager.double_type) ||
802 (target_type == TypeManager.decimal_type))
805 } else if (expr_type == TypeManager.short_type){
807 // From short to int, long, float, double
809 if ((target_type == TypeManager.int32_type) ||
810 (target_type == TypeManager.int64_type) ||
811 (target_type == TypeManager.double_type) ||
812 (target_type == TypeManager.float_type) ||
813 (target_type == TypeManager.decimal_type))
816 } else if (expr_type == TypeManager.ushort_type){
818 // From ushort to int, uint, long, ulong, float, double
820 if ((target_type == TypeManager.uint32_type) ||
821 (target_type == TypeManager.uint64_type) ||
822 (target_type == TypeManager.int32_type) ||
823 (target_type == TypeManager.int64_type) ||
824 (target_type == TypeManager.double_type) ||
825 (target_type == TypeManager.float_type) ||
826 (target_type == TypeManager.decimal_type))
829 } else if (expr_type == TypeManager.int32_type){
831 // From int to long, float, double
833 if ((target_type == TypeManager.int64_type) ||
834 (target_type == TypeManager.double_type) ||
835 (target_type == TypeManager.float_type) ||
836 (target_type == TypeManager.decimal_type))
839 } else if (expr_type == TypeManager.uint32_type){
841 // From uint to long, ulong, float, double
843 if ((target_type == TypeManager.int64_type) ||
844 (target_type == TypeManager.uint64_type) ||
845 (target_type == TypeManager.double_type) ||
846 (target_type == TypeManager.float_type) ||
847 (target_type == TypeManager.decimal_type))
850 } else if ((expr_type == TypeManager.uint64_type) ||
851 (expr_type == TypeManager.int64_type)) {
853 // From long/ulong to float, double
855 if ((target_type == TypeManager.double_type) ||
856 (target_type == TypeManager.float_type) ||
857 (target_type == TypeManager.decimal_type))
860 } else if (expr_type == TypeManager.char_type){
862 // From char to ushort, int, uint, long, ulong, float, double
864 if ((target_type == TypeManager.ushort_type) ||
865 (target_type == TypeManager.int32_type) ||
866 (target_type == TypeManager.uint32_type) ||
867 (target_type == TypeManager.uint64_type) ||
868 (target_type == TypeManager.int64_type) ||
869 (target_type == TypeManager.float_type) ||
870 (target_type == TypeManager.double_type) ||
871 (target_type == TypeManager.decimal_type))
874 } else if (expr_type == TypeManager.float_type){
878 if (target_type == TypeManager.double_type)
882 // Next reference conversions
884 if (target_type == TypeManager.object_type) {
885 if ((expr_type.IsClass) ||
886 (expr_type.IsValueType))
889 } else if (expr_type.IsSubclassOf (target_type)) {
893 // from any class-type S to any interface-type T.
894 if (expr_type.IsClass && target_type.IsInterface)
897 // from any interface type S to interface-type T.
898 // FIXME : Is it right to use IsAssignableFrom ?
899 if (expr_type.IsInterface && target_type.IsInterface)
900 if (target_type.IsAssignableFrom (expr_type))
903 // from an array-type S to an array-type of type T
904 if (expr_type.IsArray && target_type.IsArray) {
905 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
907 Type expr_element_type = expr_type.GetElementType ();
908 Type target_element_type = target_type.GetElementType ();
910 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
911 if (StandardConversionExists (expr_element_type,
912 target_element_type))
917 // from an array-type to System.Array
918 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
921 // from any delegate type to System.Delegate
922 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
923 target_type == TypeManager.delegate_type)
924 if (target_type.IsAssignableFrom (expr_type))
927 // from any array-type or delegate type into System.ICloneable.
928 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
929 if (target_type == TypeManager.icloneable_type)
932 // from the null type to any reference-type.
933 // FIXME : How do we do this ?
940 static EmptyExpression MyEmptyExpr;
942 /// Tells whether an implicit conversion exists from expr_type to
945 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
948 if (MyEmptyExpr == null)
949 MyEmptyExpr = new EmptyExpression (expr_type);
951 MyEmptyExpr.SetType (expr_type);
953 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
957 /// Finds "most encompassed type" according to the spec (13.4.2)
958 /// amongst the methods in the MethodGroupExpr which convert from a
959 /// type encompassing source_type
961 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
965 for (int i = me.Methods.Length; i > 0; ) {
968 MethodBase mb = me.Methods [i];
969 ParameterData pd = Invocation.GetParameterData (mb);
970 Type param_type = pd.ParameterType (0);
972 if (StandardConversionExists (source_type, param_type)) {
976 if (StandardConversionExists (param_type, best))
985 /// Finds "most encompassing type" according to the spec (13.4.2)
986 /// amongst the methods in the MethodGroupExpr which convert to a
987 /// type encompassed by target_type
989 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
993 for (int i = me.Methods.Length; i > 0; ) {
996 MethodInfo mi = (MethodInfo) me.Methods [i];
997 Type ret_type = mi.ReturnType;
999 if (StandardConversionExists (ret_type, target)) {
1003 if (!StandardConversionExists (ret_type, best))
1015 /// User-defined Implicit conversions
1017 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1018 Type target, Location loc)
1020 return UserDefinedConversion (ec, source, target, loc, false);
1024 /// User-defined Explicit conversions
1026 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1027 Type target, Location loc)
1029 return UserDefinedConversion (ec, source, target, loc, true);
1033 /// User-defined conversions
1035 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1036 Type target, Location loc,
1037 bool look_for_explicit)
1039 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1040 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1042 MethodBase method = null;
1043 Type source_type = source.Type;
1047 // If we have a boolean type, we need to check for the True operator
1049 // FIXME : How does the False operator come into the picture ?
1050 // FIXME : This doesn't look complete and very correct !
1051 if (target == TypeManager.bool_type)
1052 op_name = "op_True";
1054 op_name = "op_Implicit";
1056 mg1 = MemberLookup (ec, source_type, op_name, loc);
1058 if (source_type.BaseType != null)
1059 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1061 mg3 = MemberLookup (ec, target, op_name, loc);
1063 if (target.BaseType != null)
1064 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1066 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1067 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1069 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1071 MethodGroupExpr union4 = null;
1073 if (look_for_explicit) {
1075 op_name = "op_Explicit";
1077 mg5 = MemberLookup (ec, source_type, op_name, loc);
1079 if (source_type.BaseType != null)
1080 mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1082 mg7 = MemberLookup (ec, target, op_name, loc);
1084 if (target.BaseType != null)
1085 mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1087 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1088 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1090 union4 = Invocation.MakeUnionSet (union5, union6);
1093 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1095 if (union != null) {
1097 Type most_specific_source, most_specific_target;
1099 most_specific_source = FindMostEncompassedType (union, source_type);
1100 if (most_specific_source == null)
1103 most_specific_target = FindMostEncompassingType (union, target);
1104 if (most_specific_target == null)
1109 for (int i = union.Methods.Length; i > 0;) {
1112 MethodBase mb = union.Methods [i];
1113 ParameterData pd = Invocation.GetParameterData (mb);
1114 MethodInfo mi = (MethodInfo) union.Methods [i];
1116 if (pd.ParameterType (0) == most_specific_source &&
1117 mi.ReturnType == most_specific_target) {
1123 if (method == null || count > 1) {
1124 Report.Error (-11, loc, "Ambiguous user defined conversion");
1129 // This will do the conversion to the best match that we
1130 // found. Now we need to perform an implict standard conversion
1131 // if the best match was not the type that we were requested
1134 if (look_for_explicit)
1135 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1137 source = ConvertImplicitStandard (ec, source,
1138 most_specific_source, loc);
1143 e = new UserCast ((MethodInfo) method, source);
1145 if (e.Type != target){
1146 if (!look_for_explicit)
1147 e = ConvertImplicitStandard (ec, e, target, loc);
1149 e = ConvertExplicitStandard (ec, e, target, loc);
1160 /// Converts implicitly the resolved expression `expr' into the
1161 /// `target_type'. It returns a new expression that can be used
1162 /// in a context that expects a `target_type'.
1164 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1165 Type target_type, Location loc)
1167 Type expr_type = expr.Type;
1170 if (expr_type == target_type)
1173 if (target_type == null)
1174 throw new Exception ("Target type is null");
1176 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1180 e = ImplicitReferenceConversion (expr, target_type);
1184 e = ImplicitUserConversion (ec, expr, target_type, loc);
1188 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1189 IntLiteral i = (IntLiteral) expr;
1192 return new EmptyCast (expr, target_type);
1200 /// Attempts to apply the `Standard Implicit
1201 /// Conversion' rules to the expression `expr' into
1202 /// the `target_type'. It returns a new expression
1203 /// that can be used in a context that expects a
1206 /// This is different from `ConvertImplicit' in that the
1207 /// user defined implicit conversions are excluded.
1209 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1210 Type target_type, Location loc)
1212 Type expr_type = expr.Type;
1215 if (expr_type == target_type)
1218 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1222 e = ImplicitReferenceConversion (expr, target_type);
1226 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1227 IntLiteral i = (IntLiteral) expr;
1230 return new EmptyCast (expr, target_type);
1236 /// Attemps to perform an implict constant conversion of the IntConstant
1237 /// into a different data type using casts (See Implicit Constant
1238 /// Expression Conversions)
1240 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1242 int value = ic.Value;
1245 // FIXME: This should really return constants instead of EmptyCasts
1247 if (target_type == TypeManager.sbyte_type){
1248 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1249 return new SByteConstant ((sbyte) value);
1250 } else if (target_type == TypeManager.byte_type){
1251 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1252 return new ByteConstant ((byte) value);
1253 } else if (target_type == TypeManager.short_type){
1254 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1255 return new ShortConstant ((short) value);
1256 } else if (target_type == TypeManager.ushort_type){
1257 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1258 return new UShortConstant ((ushort) value);
1259 } else if (target_type == TypeManager.uint32_type){
1261 return new UIntConstant ((uint) value);
1262 } else if (target_type == TypeManager.uint64_type){
1264 // we can optimize this case: a positive int32
1265 // always fits on a uint64. But we need an opcode
1269 return new ULongConstant ((ulong) value);
1272 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1273 return new EnumConstant (ic, target_type);
1279 /// Attemptes to implicityly convert `target' into `type', using
1280 /// ConvertImplicit. If there is no implicit conversion, then
1281 /// an error is signaled
1283 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1284 Type target_type, Location loc)
1288 e = ConvertImplicit (ec, source, target_type, loc);
1292 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1294 "Double literal cannot be implicitly converted to " +
1295 "float type, use F suffix to create a float literal");
1298 string msg = "Cannot convert implicitly from `"+
1299 TypeManager.CSharpName (source.Type) + "' to `" +
1300 TypeManager.CSharpName (target_type) + "'";
1302 Error (29, loc, msg);
1308 /// Performs the explicit numeric conversions
1310 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1313 Type expr_type = expr.Type;
1315 if (expr_type == TypeManager.sbyte_type){
1317 // From sbyte to byte, ushort, uint, ulong, char
1319 if (target_type == TypeManager.byte_type)
1320 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1321 if (target_type == TypeManager.ushort_type)
1322 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1323 if (target_type == TypeManager.uint32_type)
1324 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1325 if (target_type == TypeManager.uint64_type)
1326 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1327 if (target_type == TypeManager.char_type)
1328 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1329 } else if (expr_type == TypeManager.byte_type){
1331 // From byte to sbyte and char
1333 if (target_type == TypeManager.sbyte_type)
1334 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1335 if (target_type == TypeManager.char_type)
1336 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1337 } else if (expr_type == TypeManager.short_type){
1339 // From short to sbyte, byte, ushort, uint, ulong, char
1341 if (target_type == TypeManager.sbyte_type)
1342 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1343 if (target_type == TypeManager.byte_type)
1344 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1345 if (target_type == TypeManager.ushort_type)
1346 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1347 if (target_type == TypeManager.uint32_type)
1348 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1349 if (target_type == TypeManager.uint64_type)
1350 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1351 if (target_type == TypeManager.char_type)
1352 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1353 } else if (expr_type == TypeManager.ushort_type){
1355 // From ushort to sbyte, byte, short, char
1357 if (target_type == TypeManager.sbyte_type)
1358 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1359 if (target_type == TypeManager.byte_type)
1360 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1361 if (target_type == TypeManager.short_type)
1362 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1363 if (target_type == TypeManager.char_type)
1364 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1365 } else if (expr_type == TypeManager.int32_type){
1367 // From int to sbyte, byte, short, ushort, uint, ulong, char
1369 if (target_type == TypeManager.sbyte_type)
1370 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1371 if (target_type == TypeManager.byte_type)
1372 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1373 if (target_type == TypeManager.short_type)
1374 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1375 if (target_type == TypeManager.ushort_type)
1376 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1377 if (target_type == TypeManager.uint32_type)
1378 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1379 if (target_type == TypeManager.uint64_type)
1380 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1381 if (target_type == TypeManager.char_type)
1382 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1383 } else if (expr_type == TypeManager.uint32_type){
1385 // From uint to sbyte, byte, short, ushort, int, char
1387 if (target_type == TypeManager.sbyte_type)
1388 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1389 if (target_type == TypeManager.byte_type)
1390 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1391 if (target_type == TypeManager.short_type)
1392 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1393 if (target_type == TypeManager.ushort_type)
1394 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1395 if (target_type == TypeManager.int32_type)
1396 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1397 if (target_type == TypeManager.char_type)
1398 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1399 } else if (expr_type == TypeManager.int64_type){
1401 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1403 if (target_type == TypeManager.sbyte_type)
1404 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1405 if (target_type == TypeManager.byte_type)
1406 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1407 if (target_type == TypeManager.short_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1409 if (target_type == TypeManager.ushort_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1411 if (target_type == TypeManager.int32_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1413 if (target_type == TypeManager.uint32_type)
1414 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1415 if (target_type == TypeManager.uint64_type)
1416 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1417 if (target_type == TypeManager.char_type)
1418 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1419 } else if (expr_type == TypeManager.uint64_type){
1421 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1423 if (target_type == TypeManager.sbyte_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1425 if (target_type == TypeManager.byte_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1427 if (target_type == TypeManager.short_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1429 if (target_type == TypeManager.ushort_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1431 if (target_type == TypeManager.int32_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1433 if (target_type == TypeManager.uint32_type)
1434 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1435 if (target_type == TypeManager.int64_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1437 if (target_type == TypeManager.char_type)
1438 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1439 } else if (expr_type == TypeManager.char_type){
1441 // From char to sbyte, byte, short
1443 if (target_type == TypeManager.sbyte_type)
1444 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1445 if (target_type == TypeManager.byte_type)
1446 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1447 if (target_type == TypeManager.short_type)
1448 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1449 } else if (expr_type == TypeManager.float_type){
1451 // From float to sbyte, byte, short,
1452 // ushort, int, uint, long, ulong, char
1455 if (target_type == TypeManager.sbyte_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1457 if (target_type == TypeManager.byte_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1459 if (target_type == TypeManager.short_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1461 if (target_type == TypeManager.ushort_type)
1462 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1463 if (target_type == TypeManager.int32_type)
1464 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1465 if (target_type == TypeManager.uint32_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1467 if (target_type == TypeManager.int64_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1469 if (target_type == TypeManager.uint64_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1471 if (target_type == TypeManager.char_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1473 if (target_type == TypeManager.decimal_type)
1474 return InternalTypeConstructor (ec, expr, target_type);
1475 } else if (expr_type == TypeManager.double_type){
1477 // From double to byte, byte, short,
1478 // ushort, int, uint, long, ulong,
1479 // char, float or decimal
1481 if (target_type == TypeManager.sbyte_type)
1482 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1483 if (target_type == TypeManager.byte_type)
1484 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1485 if (target_type == TypeManager.short_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1487 if (target_type == TypeManager.ushort_type)
1488 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1489 if (target_type == TypeManager.int32_type)
1490 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1491 if (target_type == TypeManager.uint32_type)
1492 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1493 if (target_type == TypeManager.int64_type)
1494 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1495 if (target_type == TypeManager.uint64_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1497 if (target_type == TypeManager.char_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1499 if (target_type == TypeManager.float_type)
1500 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1501 if (target_type == TypeManager.decimal_type)
1502 return InternalTypeConstructor (ec, expr, target_type);
1505 // decimal is taken care of by the op_Explicit methods.
1511 /// Returns whether an explicit reference conversion can be performed
1512 /// from source_type to target_type
1514 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1516 bool target_is_value_type = target_type.IsValueType;
1518 if (source_type == target_type)
1522 // From object to any reference type
1524 if (source_type == TypeManager.object_type && !target_is_value_type)
1528 // From any class S to any class-type T, provided S is a base class of T
1530 if (target_type.IsSubclassOf (source_type))
1534 // From any interface type S to any interface T provided S is not derived from T
1536 if (source_type.IsInterface && target_type.IsInterface){
1537 if (!target_type.IsSubclassOf (source_type))
1542 // From any class type S to any interface T, provides S is not sealed
1543 // and provided S does not implement T.
1545 if (target_type.IsInterface && !source_type.IsSealed &&
1546 !target_type.IsAssignableFrom (source_type))
1550 // From any interface-type S to to any class type T, provided T is not
1551 // sealed, or provided T implements S.
1553 if (source_type.IsInterface &&
1554 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1557 // From an array type S with an element type Se to an array type T with an
1558 // element type Te provided all the following are true:
1559 // * S and T differe only in element type, in other words, S and T
1560 // have the same number of dimensions.
1561 // * Both Se and Te are reference types
1562 // * An explicit referenc conversions exist from Se to Te
1564 if (source_type.IsArray && target_type.IsArray) {
1565 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1567 Type source_element_type = source_type.GetElementType ();
1568 Type target_element_type = target_type.GetElementType ();
1570 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1571 if (ExplicitReferenceConversionExists (source_element_type,
1572 target_element_type))
1578 // From System.Array to any array-type
1579 if (source_type == TypeManager.array_type &&
1580 target_type.IsSubclassOf (TypeManager.array_type)){
1585 // From System delegate to any delegate-type
1587 if (source_type == TypeManager.delegate_type &&
1588 target_type.IsSubclassOf (TypeManager.delegate_type))
1592 // From ICloneable to Array or Delegate types
1594 if (source_type == TypeManager.icloneable_type &&
1595 (target_type == TypeManager.array_type ||
1596 target_type == TypeManager.delegate_type))
1603 /// Implements Explicit Reference conversions
1605 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1607 Type source_type = source.Type;
1608 bool target_is_value_type = target_type.IsValueType;
1611 // From object to any reference type
1613 if (source_type == TypeManager.object_type && !target_is_value_type)
1614 return new ClassCast (source, target_type);
1618 // From any class S to any class-type T, provided S is a base class of T
1620 if (target_type.IsSubclassOf (source_type))
1621 return new ClassCast (source, target_type);
1624 // From any interface type S to any interface T provided S is not derived from T
1626 if (source_type.IsInterface && target_type.IsInterface){
1628 Type [] ifaces = source_type.GetInterfaces ();
1630 if (TypeManager.ImplementsInterface (source_type, target_type))
1633 return new ClassCast (source, target_type);
1637 // From any class type S to any interface T, provides S is not sealed
1638 // and provided S does not implement T.
1640 if (target_type.IsInterface && !source_type.IsSealed) {
1642 if (TypeManager.ImplementsInterface (source_type, target_type))
1645 return new ClassCast (source, target_type);
1650 // From any interface-type S to to any class type T, provided T is not
1651 // sealed, or provided T implements S.
1653 if (source_type.IsInterface) {
1655 if (target_type.IsSealed)
1658 if (TypeManager.ImplementsInterface (target_type, source_type))
1659 return new ClassCast (source, target_type);
1664 // From an array type S with an element type Se to an array type T with an
1665 // element type Te provided all the following are true:
1666 // * S and T differe only in element type, in other words, S and T
1667 // have the same number of dimensions.
1668 // * Both Se and Te are reference types
1669 // * An explicit referenc conversions exist from Se to Te
1671 if (source_type.IsArray && target_type.IsArray) {
1672 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1674 Type source_element_type = source_type.GetElementType ();
1675 Type target_element_type = target_type.GetElementType ();
1677 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1678 if (ExplicitReferenceConversionExists (source_element_type,
1679 target_element_type))
1680 return new ClassCast (source, target_type);
1685 // From System.Array to any array-type
1686 if (source_type == TypeManager.array_type &&
1687 target_type.IsSubclassOf (TypeManager.array_type)){
1688 return new ClassCast (source, target_type);
1692 // From System delegate to any delegate-type
1694 if (source_type == TypeManager.delegate_type &&
1695 target_type.IsSubclassOf (TypeManager.delegate_type))
1696 return new ClassCast (source, target_type);
1699 // From ICloneable to Array or Delegate types
1701 if (source_type == TypeManager.icloneable_type &&
1702 (target_type == TypeManager.array_type ||
1703 target_type == TypeManager.delegate_type))
1704 return new ClassCast (source, target_type);
1710 /// Performs an explicit conversion of the expression `expr' whose
1711 /// type is expr.Type to `target_type'.
1713 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1714 Type target_type, Location loc)
1716 Type expr_type = expr.Type;
1717 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1722 ne = ConvertNumericExplicit (ec, expr, target_type);
1727 // Unboxing conversion.
1729 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1730 return new UnboxCast (expr, target_type);
1735 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1739 // FIXME: Is there any reason we should have EnumConstant
1740 // dealt with here instead of just using always the
1741 // UnderlyingSystemType to wrap the type?
1743 if (expr is EnumConstant)
1744 e = ((EnumConstant) expr).Child;
1746 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1749 e = ConvertImplicit (ec, e, target_type, loc);
1753 return ConvertNumericExplicit (ec, e, target_type);
1756 ne = ConvertReferenceExplicit (expr, target_type);
1760 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1764 error30 (loc, expr_type, target_type);
1769 /// Same as ConverExplicit, only it doesn't include user defined conversions
1771 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1772 Type target_type, Location l)
1774 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1779 ne = ConvertNumericExplicit (ec, expr, target_type);
1783 ne = ConvertReferenceExplicit (expr, target_type);
1787 error30 (l, expr.Type, target_type);
1791 static string ExprClassName (ExprClass c)
1794 case ExprClass.Invalid:
1796 case ExprClass.Value:
1798 case ExprClass.Variable:
1800 case ExprClass.Namespace:
1802 case ExprClass.Type:
1804 case ExprClass.MethodGroup:
1805 return "method group";
1806 case ExprClass.PropertyAccess:
1807 return "property access";
1808 case ExprClass.EventAccess:
1809 return "event access";
1810 case ExprClass.IndexerAccess:
1811 return "indexer access";
1812 case ExprClass.Nothing:
1815 throw new Exception ("Should not happen");
1819 /// Reports that we were expecting `expr' to be of class `expected'
1821 protected void report118 (Location loc, Expression expr, string expected)
1823 string kind = "Unknown";
1826 kind = ExprClassName (expr.eclass);
1828 Error (118, loc, "Expression denotes a `" + kind +
1829 "' where a `" + expected + "' was expected");
1832 static void error31 (Location l, string val, Type t)
1834 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1835 TypeManager.CSharpName (t));
1839 /// Converts the IntConstant, UIntConstant, LongConstant or
1840 /// ULongConstant into the integral target_type. Notice
1841 /// that we do not return an `Expression' we do return
1842 /// a boxed integral type.
1844 /// FIXME: Since I added the new constants, we need to
1845 /// also support conversions from CharConstant, ByteConstant,
1846 /// SByteConstant, UShortConstant, ShortConstant
1848 /// This is used by the switch statement, so the domain
1849 /// of work is restricted to the literals above, and the
1850 /// targets are int32, uint32, char, byte, sbyte, ushort,
1851 /// short, uint64 and int64
1853 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1857 if (c.Type == target_type)
1858 return ((Constant) c).GetValue ();
1861 // Make into one of the literals we handle, we dont really care
1862 // about this value as we will just return a few limited types
1864 if (c is EnumConstant)
1865 c = ((EnumConstant)c).WidenToCompilerConstant ();
1867 if (c is IntConstant){
1868 int v = ((IntConstant) c).Value;
1870 if (target_type == TypeManager.uint32_type){
1873 } else if (target_type == TypeManager.char_type){
1874 if (v >= Char.MinValue && v <= Char.MaxValue)
1876 } else if (target_type == TypeManager.byte_type){
1877 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1879 } else if (target_type == TypeManager.sbyte_type){
1880 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1882 } else if (target_type == TypeManager.short_type){
1883 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1885 } else if (target_type == TypeManager.ushort_type){
1886 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1888 } else if (target_type == TypeManager.int64_type)
1890 else if (target_type == TypeManager.uint64_type){
1896 } else if (c is UIntConstant){
1897 uint v = ((UIntConstant) c).Value;
1899 if (target_type == TypeManager.int32_type){
1900 if (v <= Int32.MaxValue)
1902 } else if (target_type == TypeManager.char_type){
1903 if (v >= Char.MinValue && v <= Char.MaxValue)
1905 } else if (target_type == TypeManager.byte_type){
1906 if (v <= Byte.MaxValue)
1908 } else if (target_type == TypeManager.sbyte_type){
1909 if (v <= SByte.MaxValue)
1911 } else if (target_type == TypeManager.short_type){
1912 if (v <= UInt16.MaxValue)
1914 } else if (target_type == TypeManager.ushort_type){
1915 if (v <= UInt16.MaxValue)
1917 } else if (target_type == TypeManager.int64_type)
1919 else if (target_type == TypeManager.uint64_type)
1922 } else if (c is LongConstant){
1923 long v = ((LongConstant) c).Value;
1925 if (target_type == TypeManager.int32_type){
1926 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1928 } else if (target_type == TypeManager.uint32_type){
1929 if (v >= 0 && v <= UInt32.MaxValue)
1931 } else if (target_type == TypeManager.char_type){
1932 if (v >= Char.MinValue && v <= Char.MaxValue)
1934 } else if (target_type == TypeManager.byte_type){
1935 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1937 } else if (target_type == TypeManager.sbyte_type){
1938 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1940 } else if (target_type == TypeManager.short_type){
1941 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1943 } else if (target_type == TypeManager.ushort_type){
1944 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1946 } else if (target_type == TypeManager.uint64_type){
1951 } else if (c is ULongConstant){
1952 ulong v = ((ULongConstant) c).Value;
1954 if (target_type == TypeManager.int32_type){
1955 if (v <= Int32.MaxValue)
1957 } else if (target_type == TypeManager.uint32_type){
1958 if (v <= UInt32.MaxValue)
1960 } else if (target_type == TypeManager.char_type){
1961 if (v >= Char.MinValue && v <= Char.MaxValue)
1963 } else if (target_type == TypeManager.byte_type){
1964 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1966 } else if (target_type == TypeManager.sbyte_type){
1967 if (v <= (int) SByte.MaxValue)
1969 } else if (target_type == TypeManager.short_type){
1970 if (v <= UInt16.MaxValue)
1972 } else if (target_type == TypeManager.ushort_type){
1973 if (v <= UInt16.MaxValue)
1975 } else if (target_type == TypeManager.int64_type){
1976 if (v <= Int64.MaxValue)
1980 } else if (c is ByteConstant){
1981 byte v = ((ByteConstant) c).Value;
1983 if (target_type == TypeManager.int32_type)
1985 else if (target_type == TypeManager.uint32_type)
1987 else if (target_type == TypeManager.char_type)
1989 else if (target_type == TypeManager.sbyte_type){
1990 if (v <= SByte.MaxValue)
1992 } else if (target_type == TypeManager.short_type)
1994 else if (target_type == TypeManager.ushort_type)
1996 else if (target_type == TypeManager.int64_type)
1998 else if (target_type == TypeManager.uint64_type)
2001 } else if (c is SByteConstant){
2002 sbyte v = ((SByteConstant) c).Value;
2004 if (target_type == TypeManager.int32_type)
2006 else if (target_type == TypeManager.uint32_type){
2009 } else if (target_type == TypeManager.char_type){
2012 } else if (target_type == TypeManager.byte_type){
2015 } else if (target_type == TypeManager.short_type)
2017 else if (target_type == TypeManager.ushort_type){
2020 } else if (target_type == TypeManager.int64_type)
2022 else if (target_type == TypeManager.uint64_type){
2027 } else if (c is ShortConstant){
2028 short v = ((ShortConstant) c).Value;
2030 if (target_type == TypeManager.int32_type){
2032 } else if (target_type == TypeManager.uint32_type){
2035 } else if (target_type == TypeManager.char_type){
2038 } else if (target_type == TypeManager.byte_type){
2039 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2041 } else if (target_type == TypeManager.sbyte_type){
2042 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2044 } else if (target_type == TypeManager.ushort_type){
2047 } else if (target_type == TypeManager.int64_type)
2049 else if (target_type == TypeManager.uint64_type)
2053 } else if (c is UShortConstant){
2054 ushort v = ((UShortConstant) c).Value;
2056 if (target_type == TypeManager.int32_type)
2058 else if (target_type == TypeManager.uint32_type)
2060 else if (target_type == TypeManager.char_type){
2061 if (v >= Char.MinValue && v <= Char.MaxValue)
2063 } else if (target_type == TypeManager.byte_type){
2064 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2066 } else if (target_type == TypeManager.sbyte_type){
2067 if (v <= SByte.MaxValue)
2069 } else if (target_type == TypeManager.short_type){
2070 if (v <= Int16.MaxValue)
2072 } else if (target_type == TypeManager.int64_type)
2074 else if (target_type == TypeManager.uint64_type)
2078 } else if (c is CharConstant){
2079 char v = ((CharConstant) c).Value;
2081 if (target_type == TypeManager.int32_type)
2083 else if (target_type == TypeManager.uint32_type)
2085 else if (target_type == TypeManager.byte_type){
2086 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2088 } else if (target_type == TypeManager.sbyte_type){
2089 if (v <= SByte.MaxValue)
2091 } else if (target_type == TypeManager.short_type){
2092 if (v <= Int16.MaxValue)
2094 } else if (target_type == TypeManager.ushort_type)
2096 else if (target_type == TypeManager.int64_type)
2098 else if (target_type == TypeManager.uint64_type)
2103 error31 (loc, s, target_type);
2110 /// This is just a base class for expressions that can
2111 /// appear on statements (invocations, object creation,
2112 /// assignments, post/pre increment and decrement). The idea
2113 /// being that they would support an extra Emition interface that
2114 /// does not leave a result on the stack.
2116 public abstract class ExpressionStatement : Expression {
2119 /// Requests the expression to be emitted in a `statement'
2120 /// context. This means that no new value is left on the
2121 /// stack after invoking this method (constrasted with
2122 /// Emit that will always leave a value on the stack).
2124 public abstract void EmitStatement (EmitContext ec);
2128 /// This kind of cast is used to encapsulate the child
2129 /// whose type is child.Type into an expression that is
2130 /// reported to return "return_type". This is used to encapsulate
2131 /// expressions which have compatible types, but need to be dealt
2132 /// at higher levels with.
2134 /// For example, a "byte" expression could be encapsulated in one
2135 /// of these as an "unsigned int". The type for the expression
2136 /// would be "unsigned int".
2139 public class EmptyCast : Expression {
2140 protected Expression child;
2142 public EmptyCast (Expression child, Type return_type)
2144 eclass = child.eclass;
2149 public override Expression DoResolve (EmitContext ec)
2151 // This should never be invoked, we are born in fully
2152 // initialized state.
2157 public override void Emit (EmitContext ec)
2164 /// This class is used to wrap literals which belong inside Enums
2166 public class EnumConstant : Constant {
2167 public Constant Child;
2169 public EnumConstant (Constant child, Type enum_type)
2171 eclass = child.eclass;
2176 public override Expression DoResolve (EmitContext ec)
2178 // This should never be invoked, we are born in fully
2179 // initialized state.
2184 public override void Emit (EmitContext ec)
2189 public override object GetValue ()
2191 return Child.GetValue ();
2195 // Converts from one of the valid underlying types for an enumeration
2196 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2197 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2199 public Constant WidenToCompilerConstant ()
2201 Type t = TypeManager.EnumToUnderlying (Child.Type);
2202 object v = ((Constant) Child).GetValue ();;
2204 if (t == TypeManager.int32_type)
2205 return new IntConstant ((int) v);
2206 if (t == TypeManager.uint32_type)
2207 return new UIntConstant ((uint) v);
2208 if (t == TypeManager.int64_type)
2209 return new LongConstant ((long) v);
2210 if (t == TypeManager.uint64_type)
2211 return new ULongConstant ((ulong) v);
2212 if (t == TypeManager.short_type)
2213 return new ShortConstant ((short) v);
2214 if (t == TypeManager.ushort_type)
2215 return new UShortConstant ((ushort) v);
2216 if (t == TypeManager.byte_type)
2217 return new ByteConstant ((byte) v);
2218 if (t == TypeManager.sbyte_type)
2219 return new SByteConstant ((sbyte) v);
2221 throw new Exception ("Invalid enumeration underlying type: " + t);
2225 // Extracts the value in the enumeration on its native representation
2227 public object GetPlainValue ()
2229 Type t = TypeManager.EnumToUnderlying (Child.Type);
2230 object v = ((Constant) Child).GetValue ();;
2232 if (t == TypeManager.int32_type)
2234 if (t == TypeManager.uint32_type)
2236 if (t == TypeManager.int64_type)
2238 if (t == TypeManager.uint64_type)
2240 if (t == TypeManager.short_type)
2242 if (t == TypeManager.ushort_type)
2244 if (t == TypeManager.byte_type)
2246 if (t == TypeManager.sbyte_type)
2252 public override string AsString ()
2254 return Child.AsString ();
2259 /// This kind of cast is used to encapsulate Value Types in objects.
2261 /// The effect of it is to box the value type emitted by the previous
2264 public class BoxedCast : EmptyCast {
2266 public BoxedCast (Expression expr)
2267 : base (expr, TypeManager.object_type)
2271 public override Expression DoResolve (EmitContext ec)
2273 // This should never be invoked, we are born in fully
2274 // initialized state.
2279 public override void Emit (EmitContext ec)
2282 ec.ig.Emit (OpCodes.Box, child.Type);
2286 public class UnboxCast : EmptyCast {
2287 public UnboxCast (Expression expr, Type return_type)
2288 : base (expr, return_type)
2292 public override Expression DoResolve (EmitContext ec)
2294 // This should never be invoked, we are born in fully
2295 // initialized state.
2300 public override void Emit (EmitContext ec)
2303 ILGenerator ig = ec.ig;
2306 ig.Emit (OpCodes.Unbox, t);
2309 // Load the object from the pointer
2313 if (t == TypeManager.int32_type)
2314 ig.Emit (OpCodes.Ldind_I4);
2315 else if (t == TypeManager.uint32_type)
2316 ig.Emit (OpCodes.Ldind_U4);
2317 else if (t == TypeManager.short_type)
2318 ig.Emit (OpCodes.Ldind_I2);
2319 else if (t == TypeManager.ushort_type)
2320 ig.Emit (OpCodes.Ldind_U2);
2321 else if (t == TypeManager.char_type)
2322 ig.Emit (OpCodes.Ldind_U2);
2323 else if (t == TypeManager.byte_type)
2324 ig.Emit (OpCodes.Ldind_U1);
2325 else if (t == TypeManager.sbyte_type)
2326 ig.Emit (OpCodes.Ldind_I1);
2327 else if (t == TypeManager.uint64_type)
2328 ig.Emit (OpCodes.Ldind_I8);
2329 else if (t == TypeManager.int64_type)
2330 ig.Emit (OpCodes.Ldind_I8);
2331 else if (t == TypeManager.float_type)
2332 ig.Emit (OpCodes.Ldind_R4);
2333 else if (t == TypeManager.double_type)
2334 ig.Emit (OpCodes.Ldind_R8);
2335 else if (t == TypeManager.bool_type)
2336 ig.Emit (OpCodes.Ldind_I1);
2337 else if (t == TypeManager.intptr_type)
2338 ig.Emit (OpCodes.Ldind_I);
2339 else if (TypeManager.IsEnumType (t)){
2340 t = TypeManager.EnumToUnderlying (t);
2343 ig.Emit (OpCodes.Ldobj, t);
2348 /// This is used to perform explicit numeric conversions.
2350 /// Explicit numeric conversions might trigger exceptions in a checked
2351 /// context, so they should generate the conv.ovf opcodes instead of
2354 public class ConvCast : EmptyCast {
2355 public enum Mode : byte {
2356 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2358 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2359 U2_I1, U2_U1, U2_I2, U2_CH,
2360 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2361 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2362 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2363 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2364 CH_I1, CH_U1, CH_I2,
2365 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2366 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2371 public ConvCast (Expression child, Type return_type, Mode m)
2372 : base (child, return_type)
2377 public override Expression DoResolve (EmitContext ec)
2379 // This should never be invoked, we are born in fully
2380 // initialized state.
2385 public override void Emit (EmitContext ec)
2387 ILGenerator ig = ec.ig;
2393 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2394 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2395 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2396 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2397 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2399 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2400 case Mode.U1_CH: /* nothing */ break;
2402 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2403 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2404 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2405 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2406 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2407 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2409 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2410 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2411 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2412 case Mode.U2_CH: /* nothing */ break;
2414 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2415 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2416 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2417 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2418 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2419 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2420 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2422 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2423 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2424 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2425 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2426 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2427 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2429 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2430 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2431 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2432 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2433 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2434 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2435 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2436 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2438 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2439 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2440 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2441 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2442 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2443 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2444 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2445 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2447 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2448 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2449 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2451 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2452 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2453 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2454 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2455 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2456 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2457 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2458 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2459 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2461 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2462 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2463 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2464 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2465 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2466 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2467 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2468 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2469 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2470 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2474 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2475 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2476 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2477 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2478 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2480 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2481 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2483 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2484 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2485 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2486 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2487 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2488 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2490 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2491 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2492 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2493 case Mode.U2_CH: /* nothing */ break;
2495 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2496 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2497 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2498 case Mode.I4_U4: /* nothing */ break;
2499 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2500 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2501 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2503 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2504 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2505 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2506 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2507 case Mode.U4_I4: /* nothing */ break;
2508 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2510 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2511 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2512 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2513 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2514 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2515 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2516 case Mode.I8_U8: /* nothing */ break;
2517 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2519 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2520 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2521 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2522 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2523 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2524 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2525 case Mode.U8_I8: /* nothing */ break;
2526 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2528 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2529 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2530 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2532 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2533 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2534 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2535 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2536 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2537 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2538 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2539 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2540 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2542 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2543 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2544 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2545 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2546 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2547 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2548 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2549 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2550 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2551 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2557 public class OpcodeCast : EmptyCast {
2561 public OpcodeCast (Expression child, Type return_type, OpCode op)
2562 : base (child, return_type)
2566 second_valid = false;
2569 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2570 : base (child, return_type)
2575 second_valid = true;
2578 public override Expression DoResolve (EmitContext ec)
2580 // This should never be invoked, we are born in fully
2581 // initialized state.
2586 public override void Emit (EmitContext ec)
2597 /// This kind of cast is used to encapsulate a child and cast it
2598 /// to the class requested
2600 public class ClassCast : EmptyCast {
2601 public ClassCast (Expression child, Type return_type)
2602 : base (child, return_type)
2607 public override Expression DoResolve (EmitContext ec)
2609 // This should never be invoked, we are born in fully
2610 // initialized state.
2615 public override void Emit (EmitContext ec)
2619 ec.ig.Emit (OpCodes.Castclass, type);
2625 /// SimpleName expressions are initially formed of a single
2626 /// word and it only happens at the beginning of the expression.
2630 /// The expression will try to be bound to a Field, a Method
2631 /// group or a Property. If those fail we pass the name to our
2632 /// caller and the SimpleName is compounded to perform a type
2633 /// lookup. The idea behind this process is that we want to avoid
2634 /// creating a namespace map from the assemblies, as that requires
2635 /// the GetExportedTypes function to be called and a hashtable to
2636 /// be constructed which reduces startup time. If later we find
2637 /// that this is slower, we should create a `NamespaceExpr' expression
2638 /// that fully participates in the resolution process.
2640 /// For example `System.Console.WriteLine' is decomposed into
2641 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2643 /// The first SimpleName wont produce a match on its own, so it will
2645 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2647 /// System.Console will produce a TypeExpr match.
2649 /// The downside of this is that we might be hitting `LookupType' too many
2650 /// times with this scheme.
2652 public class SimpleName : Expression {
2653 public readonly string Name;
2654 public readonly Location Location;
2656 public SimpleName (string name, Location l)
2662 public static void Error120 (Location l, string name)
2666 "An object reference is required " +
2667 "for the non-static field `"+name+"'");
2671 // Checks whether we are trying to access an instance
2672 // property, method or field from a static body.
2674 Expression MemberStaticCheck (Expression e)
2676 if (e is FieldExpr){
2677 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2680 Error120 (Location, Name);
2683 } else if (e is MethodGroupExpr){
2684 MethodGroupExpr mg = (MethodGroupExpr) e;
2686 if (!mg.RemoveInstanceMethods ()){
2687 Error120 (Location, mg.Methods [0].Name);
2691 } else if (e is PropertyExpr){
2692 if (!((PropertyExpr) e).IsStatic){
2693 Error120 (Location, Name);
2696 } else if (e is EventExpr) {
2697 if (!((EventExpr) e).IsStatic) {
2698 Error120 (Location, Name);
2706 public override Expression DoResolve (EmitContext ec)
2708 return SimpleNameResolve (ec, false);
2711 public Expression DoResolveAllowStatic (EmitContext ec)
2713 return SimpleNameResolve (ec, true);
2717 /// 7.5.2: Simple Names.
2719 /// Local Variables and Parameters are handled at
2720 /// parse time, so they never occur as SimpleNames.
2722 /// The `allow_static' flag is used by MemberAccess only
2723 /// and it is used to inform us that it is ok for us to
2724 /// avoid the static check, because MemberAccess might end
2725 /// up resolving the Name as a Type name and the access as
2726 /// a static type access.
2728 /// ie: Type Type; .... { Type.GetType (""); }
2730 /// Type is both an instance variable and a Type; Type.GetType
2731 /// is the static method not an instance method of type.
2733 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2738 // Stage 1: Performed by the parser (binding to locals or parameters).
2742 // Stage 2: Lookup members
2744 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
2747 // Stage 3: Lookup symbol in the various namespaces.
2749 DeclSpace ds = ec.TypeContainer;
2753 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2754 return new TypeExpr (t);
2757 // Stage 2 part b: Lookup up if we are an alias to a type
2760 // Since we are cheating: we only do the Alias lookup for
2761 // namespaces if the name does not include any dots in it
2764 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2765 // System.Console.WriteLine (Name + " --> " + alias_value);
2766 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2768 return new TypeExpr (t);
2770 // we have alias value, but it isn't Type, so try if it's namespace
2771 return new SimpleName (alias_value, Location);
2774 // No match, maybe our parent can compose us
2775 // into something meaningful.
2780 // Stage 2 continues here.
2785 if (e is FieldExpr){
2786 FieldExpr fe = (FieldExpr) e;
2787 FieldInfo fi = fe.FieldInfo;
2790 if (!allow_static && !fi.IsStatic){
2791 Error120 (Location, Name);
2795 // If we are not in static code and this
2796 // field is not static, set the instance to `this'.
2799 fe.InstanceExpression = ec.This;
2803 if (fi is FieldBuilder) {
2804 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2807 object o = c.LookupConstantValue (ec);
2808 object real_value = ((Constant)c.Expr).GetValue ();
2809 return Constantify (real_value, fi.FieldType);
2816 if (e is EventExpr) {
2818 // If the event is local to this class, we transform ourselves into
2821 EventExpr ee = (EventExpr) e;
2823 Expression ml = MemberLookup (
2824 ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
2825 MemberTypes.Event, AllBindingFlags, Location);
2828 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
2832 // If this happens, then we have an event with its own
2833 // accessors and private field etc so there's no need
2834 // to transform ourselves : we should instead flag an error
2836 Assign.error70 (ee.EventInfo, Location);
2840 ml = ExprClassFromMemberInfo (ec, mi, Location);
2843 Report.Error (-200, Location, "Internal error!!");
2847 Expression instance_expr;
2849 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
2852 instance_expr = null;
2854 instance_expr = ec.This;
2856 instance_expr = instance_expr.Resolve (ec);
2858 if (instance_expr != null)
2859 instance_expr = instance_expr.Resolve (ec);
2861 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
2870 return MemberStaticCheck (e);
2875 public override void Emit (EmitContext ec)
2878 // If this is ever reached, then we failed to
2879 // find the name as a namespace
2882 Error (103, Location, "The name `" + Name +
2883 "' does not exist in the class `" +
2884 ec.TypeContainer.Name + "'");
2889 /// Fully resolved expression that evaluates to a type
2891 public class TypeExpr : Expression {
2892 public TypeExpr (Type t)
2895 eclass = ExprClass.Type;
2898 override public Expression DoResolve (EmitContext ec)
2903 override public void Emit (EmitContext ec)
2905 throw new Exception ("Implement me");
2910 /// MethodGroup Expression.
2912 /// This is a fully resolved expression that evaluates to a type
2914 public class MethodGroupExpr : Expression {
2915 public MethodBase [] Methods;
2916 Expression instance_expression = null;
2918 public MethodGroupExpr (MemberInfo [] mi)
2920 Methods = new MethodBase [mi.Length];
2921 mi.CopyTo (Methods, 0);
2922 eclass = ExprClass.MethodGroup;
2925 public MethodGroupExpr (ArrayList l)
2927 Methods = new MethodBase [l.Count];
2929 l.CopyTo (Methods, 0);
2930 eclass = ExprClass.MethodGroup;
2934 // `A method group may have associated an instance expression'
2936 public Expression InstanceExpression {
2938 return instance_expression;
2942 instance_expression = value;
2946 override public Expression DoResolve (EmitContext ec)
2951 override public void Emit (EmitContext ec)
2953 throw new Exception ("This should never be reached");
2956 bool RemoveMethods (bool keep_static)
2958 ArrayList smethods = new ArrayList ();
2959 int top = Methods.Length;
2962 for (i = 0; i < top; i++){
2963 MethodBase mb = Methods [i];
2965 if (mb.IsStatic == keep_static)
2969 if (smethods.Count == 0)
2972 Methods = new MethodBase [smethods.Count];
2973 smethods.CopyTo (Methods, 0);
2979 /// Removes any instance methods from the MethodGroup, returns
2980 /// false if the resulting set is empty.
2982 public bool RemoveInstanceMethods ()
2984 return RemoveMethods (true);
2988 /// Removes any static methods from the MethodGroup, returns
2989 /// false if the resulting set is empty.
2991 public bool RemoveStaticMethods ()
2993 return RemoveMethods (false);
2998 /// Fully resolved expression that evaluates to a Field
3000 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3001 public readonly FieldInfo FieldInfo;
3002 public Expression InstanceExpression;
3005 public FieldExpr (FieldInfo fi, Location l)
3008 eclass = ExprClass.Variable;
3009 type = fi.FieldType;
3013 override public Expression DoResolve (EmitContext ec)
3015 if (!FieldInfo.IsStatic){
3016 if (InstanceExpression == null){
3017 throw new Exception ("non-static FieldExpr without instance var\n" +
3018 "You have to assign the Instance variable\n" +
3019 "Of the FieldExpr to set this\n");
3022 InstanceExpression = InstanceExpression.Resolve (ec);
3023 if (InstanceExpression == null)
3031 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3033 Expression e = DoResolve (ec);
3038 if (!FieldInfo.IsInitOnly)
3042 // InitOnly fields can only be assigned in constructors
3045 if (ec.IsConstructor)
3048 Report.Error (191, loc,
3049 "Readonly field can not be assigned outside " +
3050 "of constructor or variable initializer");
3055 override public void Emit (EmitContext ec)
3057 ILGenerator ig = ec.ig;
3059 if (FieldInfo.IsStatic)
3060 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3062 if (InstanceExpression.Type.IsValueType){
3065 if (!(InstanceExpression is IMemoryLocation)){
3066 LocalTemporary tempo = new LocalTemporary (
3067 ec, InstanceExpression.Type);
3069 InstanceExpression.Emit (ec);
3073 ml = (IMemoryLocation) InstanceExpression;
3077 InstanceExpression.Emit (ec);
3079 ig.Emit (OpCodes.Ldfld, FieldInfo);
3083 public void EmitAssign (EmitContext ec, Expression source)
3085 bool is_static = FieldInfo.IsStatic;
3088 Expression instance = InstanceExpression;
3090 if (instance.Type.IsValueType){
3091 if (instance is IMemoryLocation){
3092 IMemoryLocation ml = (IMemoryLocation) instance;
3096 throw new Exception ("The " + instance + " of type " +
3098 " represents a ValueType and does " +
3099 "not implement IMemoryLocation");
3106 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3108 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3112 public void AddressOf (EmitContext ec)
3114 if (FieldInfo.IsStatic)
3115 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3117 InstanceExpression.Emit (ec);
3118 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3124 /// Expression that evaluates to a Property. The Assign class
3125 /// might set the `Value' expression if we are in an assignment.
3127 /// This is not an LValue because we need to re-write the expression, we
3128 /// can not take data from the stack and store it.
3130 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3131 public readonly PropertyInfo PropertyInfo;
3132 public readonly bool IsStatic;
3134 MethodInfo [] Accessors;
3137 Expression instance_expr;
3139 public PropertyExpr (PropertyInfo pi, Location l)
3142 eclass = ExprClass.PropertyAccess;
3145 Accessors = TypeManager.GetAccessors (pi);
3147 if (Accessors != null)
3148 for (int i = 0; i < Accessors.Length; i++){
3149 if (Accessors [i] != null)
3150 if (Accessors [i].IsStatic)
3154 Accessors = new MethodInfo [2];
3156 type = pi.PropertyType;
3160 // The instance expression associated with this expression
3162 public Expression InstanceExpression {
3164 instance_expr = value;
3168 return instance_expr;
3172 public bool VerifyAssignable ()
3174 if (!PropertyInfo.CanWrite){
3175 Report.Error (200, loc,
3176 "The property `" + PropertyInfo.Name +
3177 "' can not be assigned to, as it has not set accessor");
3184 override public Expression DoResolve (EmitContext ec)
3186 if (!PropertyInfo.CanRead){
3187 Report.Error (154, loc,
3188 "The property `" + PropertyInfo.Name +
3189 "' can not be used in " +
3190 "this context because it lacks a get accessor");
3194 type = PropertyInfo.PropertyType;
3199 override public void Emit (EmitContext ec)
3201 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
3206 // Implements the IAssignMethod interface for assignments
3208 public void EmitAssign (EmitContext ec, Expression source)
3210 Argument arg = new Argument (source, Argument.AType.Expression);
3211 ArrayList args = new ArrayList ();
3214 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3217 override public void EmitStatement (EmitContext ec)
3220 ec.ig.Emit (OpCodes.Pop);
3225 /// Fully resolved expression that evaluates to an Event
3227 public class EventExpr : Expression {
3228 public readonly EventInfo EventInfo;
3230 public Expression InstanceExpression;
3232 public readonly bool IsStatic;
3234 MethodInfo add_accessor, remove_accessor;
3236 public EventExpr (EventInfo ei, Location loc)
3240 eclass = ExprClass.EventAccess;
3242 add_accessor = TypeManager.GetAddMethod (ei);
3243 remove_accessor = TypeManager.GetRemoveMethod (ei);
3245 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3248 if (EventInfo is MyEventBuilder)
3249 type = ((MyEventBuilder) EventInfo).EventType;
3251 type = EventInfo.EventHandlerType;
3254 override public Expression DoResolve (EmitContext ec)
3256 // We are born fully resolved
3260 override public void Emit (EmitContext ec)
3262 throw new Exception ("Should not happen I think");
3265 public void EmitAddOrRemove (EmitContext ec, Expression source)
3267 Expression handler = ((Binary) source).Right;
3269 Argument arg = new Argument (handler, Argument.AType.Expression);
3270 ArrayList args = new ArrayList ();
3274 if (((Binary) source).Oper == Binary.Operator.Addition)
3275 Invocation.EmitCall (
3276 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3278 Invocation.EmitCall (
3279 ec, false, IsStatic, InstanceExpression, remove_accessor, args);