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)
178 Expression e = DoResolve (ec);
184 if (e.eclass == ExprClass.Invalid)
185 throw new Exception ("Expression " + e +
186 " ExprClass is Invalid after resolve");
188 if (e.eclass != ExprClass.MethodGroup)
190 throw new Exception ("Expression " + e +
191 " did not set its type after Resolve");
198 /// Resolves an expression for LValue assignment
202 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
203 /// checking and assertion checking on what we expect from Resolve
205 public Expression ResolveLValue (EmitContext ec, Expression right_side)
207 Expression e = DoResolveLValue (ec, right_side);
210 if (e is SimpleName){
211 SimpleName s = (SimpleName) e;
215 "The name `" + s.Name + "' could not be found in `" +
216 ec.TypeContainer.Name + "'");
220 if (e.eclass == ExprClass.Invalid)
221 throw new Exception ("Expression " + e +
222 " ExprClass is Invalid after resolve");
224 if (e.eclass != ExprClass.MethodGroup)
226 throw new Exception ("Expression " + e +
227 " did not set its type after Resolve");
234 /// Emits the code for the expression
238 /// The Emit method is invoked to generate the code
239 /// for the expression.
241 public abstract void Emit (EmitContext ec);
244 /// Protected constructor. Only derivate types should
245 /// be able to be created
248 protected Expression ()
250 eclass = ExprClass.Invalid;
255 /// Returns a literalized version of a literal FieldInfo
259 /// The possible return values are:
260 /// IntConstant, UIntConstant
261 /// LongLiteral, ULongConstant
262 /// FloatConstant, DoubleConstant
265 /// The value returned is already resolved.
267 public static Constant Constantify (object v, Type t)
269 if (t == TypeManager.int32_type)
270 return new IntConstant ((int) v);
271 else if (t == TypeManager.uint32_type)
272 return new UIntConstant ((uint) v);
273 else if (t == TypeManager.int64_type)
274 return new LongConstant ((long) v);
275 else if (t == TypeManager.uint64_type)
276 return new ULongConstant ((ulong) v);
277 else if (t == TypeManager.float_type)
278 return new FloatConstant ((float) v);
279 else if (t == TypeManager.double_type)
280 return new DoubleConstant ((double) v);
281 else if (t == TypeManager.string_type)
282 return new StringConstant ((string) v);
283 else if (t == TypeManager.short_type)
284 return new ShortConstant ((short)v);
285 else if (t == TypeManager.ushort_type)
286 return new UShortConstant ((ushort)v);
287 else if (t == TypeManager.sbyte_type)
288 return new SByteConstant (((sbyte)v));
289 else if (t == TypeManager.byte_type)
290 return new ByteConstant ((byte)v);
291 else if (t == TypeManager.char_type)
292 return new CharConstant ((char)v);
293 else if (TypeManager.IsEnumType (t)){
294 Expression e = Constantify (v, v.GetType ());
296 return new EnumConstant ((Constant) e, t);
298 throw new Exception ("Unknown type for constant (" + t +
303 /// Returns a fully formed expression after a MemberLookup
305 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
308 return new EventExpr ((EventInfo) mi, loc);
309 else if (mi is FieldInfo)
310 return new FieldExpr ((FieldInfo) mi, loc);
311 else if (mi is PropertyInfo)
312 return new PropertyExpr ((PropertyInfo) mi, loc);
313 else if (mi is Type){
314 return new TypeExpr ((System.Type) mi);
321 // We copy methods from `new_members' into `target_list' if the signature
322 // for the method from in the new list does not exist in the target_list
324 // The name is assumed to be the same.
326 static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
328 if (target_list == null){
329 target_list = new ArrayList ();
331 target_list.AddRange (new_members);
335 MemberInfo [] target_array = new MemberInfo [target_list.Count];
336 target_list.CopyTo (target_array, 0);
338 foreach (MemberInfo mi in new_members){
339 MethodBase new_method = (MethodBase) mi;
340 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
342 foreach (MethodBase method in target_array){
343 Type [] old_args = TypeManager.GetArgumentTypes (method);
344 int new_count = new_args.Length;
345 int old_count = old_args.Length;
347 if (new_count != old_count){
348 target_list.Add (method);
352 for (int i = 0; i < old_count; i++){
353 if (old_args [i] == new_args [i])
355 target_list.Add (method);
364 // FIXME: Probably implement a cache for (t,name,current_access_set)?
366 // FIXME: We need to cope with access permissions here, or this wont
369 // This code could use some optimizations, but we need to do some
370 // measurements. For example, we could use a delegate to `flag' when
371 // something can not any longer be a method-group (because it is something
375 // If the return value is an Array, then it is an array of
378 // If the return value is an MemberInfo, it is anything, but a Method
382 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
383 // the arguments here and have MemberLookup return only the methods that
384 // match the argument count/type, unlike we are doing now (we delay this
387 // This is so we can catch correctly attempts to invoke instance methods
388 // from a static body (scan for error 120 in ResolveSimpleName).
391 // FIXME: Potential optimization, have a static ArrayList
393 public static Expression MemberLookup (EmitContext ec, Type t, string name,
394 bool same_type, MemberTypes mt,
395 BindingFlags bf, Location loc)
398 bf |= BindingFlags.NonPublic;
401 // Lookup for members starting in the type requested and going
402 // up the hierarchy until a match is found.
404 // As soon as a non-method match is found, we return.
406 // If methods are found though, then the search proceeds scanning
407 // for more public methods in the hierarchy with signatures that
408 // do not match any of the signatures found so far.
410 ArrayList method_list = null;
411 Type current_type = t;
412 bool searching = true;
416 mi = RootContext.TypeManager.FindMembers (
417 current_type, mt, bf | BindingFlags.DeclaredOnly,
418 System.Type.FilterName, name);
420 if (current_type == TypeManager.object_type)
423 current_type = current_type.BaseType;
426 // This happens with interfaces, they have a null
429 if (current_type == null)
436 int count = mi.Length;
441 if (count == 1 && !(mi [0] is MethodBase))
442 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
445 // We found methods, turn the search into "method scan"
448 method_list = CopyNewMethods (method_list, mi);
449 mt &= (MemberTypes.Method | MemberTypes.Constructor);
452 if (method_list != null && method_list.Count > 0)
453 return new MethodGroupExpr (method_list);
458 public const MemberTypes AllMemberTypes =
459 MemberTypes.Constructor |
463 MemberTypes.NestedType |
464 MemberTypes.Property;
466 public const BindingFlags AllBindingFlags =
467 BindingFlags.Public |
468 BindingFlags.Static |
469 BindingFlags.Instance;
471 public static Expression MemberLookup (EmitContext ec, Type t, string name,
472 bool same_type, Location loc)
474 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingFlags, loc);
477 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
479 Type expr_type = expr.Type;
481 if (target_type == TypeManager.object_type) {
482 if (expr_type.IsClass)
483 return new EmptyCast (expr, target_type);
484 if (expr_type.IsValueType)
485 return new BoxedCast (expr);
486 } else if (expr_type.IsSubclassOf (target_type)) {
487 return new EmptyCast (expr, target_type);
489 // from any class-type S to any interface-type T.
490 if (expr_type.IsClass && target_type.IsInterface) {
491 if (TypeManager.ImplementsInterface (expr_type, target_type))
492 return new EmptyCast (expr, target_type);
497 // from any interface type S to interface-type T.
498 if (expr_type.IsInterface && target_type.IsInterface) {
500 if (TypeManager.ImplementsInterface (expr_type, target_type))
501 return new EmptyCast (expr, target_type);
506 // from an array-type S to an array-type of type T
507 if (expr_type.IsArray && target_type.IsArray) {
508 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
510 Type expr_element_type = expr_type.GetElementType ();
511 Type target_element_type = target_type.GetElementType ();
513 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
514 if (StandardConversionExists (expr_element_type,
515 target_element_type))
516 return new EmptyCast (expr, target_type);
521 // from an array-type to System.Array
522 if (expr_type.IsArray && target_type == TypeManager.array_type)
523 return new EmptyCast (expr, target_type);
525 // from any delegate type to System.Delegate
526 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
527 target_type == TypeManager.delegate_type)
528 return new EmptyCast (expr, target_type);
530 // from any array-type or delegate type into System.ICloneable.
531 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
532 if (target_type == TypeManager.icloneable_type)
533 return new EmptyCast (expr, target_type);
535 // from the null type to any reference-type.
536 if (expr is NullLiteral)
537 return new EmptyCast (expr, target_type);
547 /// Handles expressions like this: decimal d; d = 1;
548 /// and changes them into: decimal d; d = new System.Decimal (1);
550 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
552 ArrayList args = new ArrayList ();
554 args.Add (new Argument (expr, Argument.AType.Expression));
556 Expression ne = new New (target.FullName, args,
559 return ne.Resolve (ec);
563 /// Implicit Numeric Conversions.
565 /// expr is the expression to convert, returns a new expression of type
566 /// target_type or null if an implicit conversion is not possible.
568 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
569 Type target_type, Location loc)
571 Type expr_type = expr.Type;
574 // Attempt to do the implicit constant expression conversions
576 if (expr is IntConstant){
579 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
583 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
585 // Try the implicit constant expression conversion
586 // from long to ulong, instead of a nice routine,
589 long v = ((LongConstant) expr).Value;
591 return new ULongConstant ((ulong) v);
594 if (expr_type == TypeManager.sbyte_type){
596 // From sbyte to short, int, long, float, double.
598 if (target_type == TypeManager.int32_type)
599 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
600 if (target_type == TypeManager.int64_type)
601 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
602 if (target_type == TypeManager.double_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
604 if (target_type == TypeManager.float_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
606 if (target_type == TypeManager.short_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
608 if (target_type == TypeManager.decimal_type)
609 return InternalTypeConstructor (ec, expr, target_type);
610 } else if (expr_type == TypeManager.byte_type){
612 // From byte to short, ushort, int, uint, long, ulong, float, double
614 if ((target_type == TypeManager.short_type) ||
615 (target_type == TypeManager.ushort_type) ||
616 (target_type == TypeManager.int32_type) ||
617 (target_type == TypeManager.uint32_type))
618 return new EmptyCast (expr, target_type);
620 if (target_type == TypeManager.uint64_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
622 if (target_type == TypeManager.int64_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
624 if (target_type == TypeManager.float_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
626 if (target_type == TypeManager.double_type)
627 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
628 if (target_type == TypeManager.decimal_type)
629 return InternalTypeConstructor (ec, expr, target_type);
630 } else if (expr_type == TypeManager.short_type){
632 // From short to int, long, float, double
634 if (target_type == TypeManager.int32_type)
635 return new EmptyCast (expr, target_type);
636 if (target_type == TypeManager.int64_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
638 if (target_type == TypeManager.double_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
640 if (target_type == TypeManager.float_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
642 if (target_type == TypeManager.decimal_type)
643 return InternalTypeConstructor (ec, expr, target_type);
644 } else if (expr_type == TypeManager.ushort_type){
646 // From ushort to int, uint, long, ulong, float, double
648 if (target_type == TypeManager.uint32_type)
649 return new EmptyCast (expr, target_type);
651 if (target_type == TypeManager.uint64_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
653 if (target_type == TypeManager.int32_type)
654 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
655 if (target_type == TypeManager.int64_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
657 if (target_type == TypeManager.double_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
659 if (target_type == TypeManager.float_type)
660 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
661 if (target_type == TypeManager.decimal_type)
662 return InternalTypeConstructor (ec, expr, target_type);
663 } else if (expr_type == TypeManager.int32_type){
665 // From int to long, float, double
667 if (target_type == TypeManager.int64_type)
668 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
669 if (target_type == TypeManager.double_type)
670 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
671 if (target_type == TypeManager.float_type)
672 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
673 if (target_type == TypeManager.decimal_type)
674 return InternalTypeConstructor (ec, expr, target_type);
675 } else if (expr_type == TypeManager.uint32_type){
677 // From uint to long, ulong, float, double
679 if (target_type == TypeManager.int64_type)
680 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
681 if (target_type == TypeManager.uint64_type)
682 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
683 if (target_type == TypeManager.double_type)
684 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
686 if (target_type == TypeManager.float_type)
687 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
689 if (target_type == TypeManager.decimal_type)
690 return InternalTypeConstructor (ec, expr, target_type);
691 } else if ((expr_type == TypeManager.uint64_type) ||
692 (expr_type == TypeManager.int64_type)){
694 // From long/ulong to float, double
696 if (target_type == TypeManager.double_type)
697 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
699 if (target_type == TypeManager.float_type)
700 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
702 if (target_type == TypeManager.decimal_type)
703 return InternalTypeConstructor (ec, expr, target_type);
704 } else if (expr_type == TypeManager.char_type){
706 // From char to ushort, int, uint, long, ulong, float, double
708 if ((target_type == TypeManager.ushort_type) ||
709 (target_type == TypeManager.int32_type) ||
710 (target_type == TypeManager.uint32_type))
711 return new EmptyCast (expr, target_type);
712 if (target_type == TypeManager.uint64_type)
713 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
714 if (target_type == TypeManager.int64_type)
715 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
716 if (target_type == TypeManager.float_type)
717 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
718 if (target_type == TypeManager.double_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
720 if (target_type == TypeManager.decimal_type)
721 return InternalTypeConstructor (ec, expr, target_type);
722 } else if (expr_type == TypeManager.float_type){
726 if (target_type == TypeManager.double_type)
727 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
734 /// Determines if a standard implicit conversion exists from
735 /// expr_type to target_type
737 public static bool StandardConversionExists (Type expr_type, Type target_type)
739 if (expr_type == target_type)
742 // First numeric conversions
744 if (expr_type == TypeManager.sbyte_type){
746 // From sbyte to short, int, long, float, double.
748 if ((target_type == TypeManager.int32_type) ||
749 (target_type == TypeManager.int64_type) ||
750 (target_type == TypeManager.double_type) ||
751 (target_type == TypeManager.float_type) ||
752 (target_type == TypeManager.short_type) ||
753 (target_type == TypeManager.decimal_type))
756 } else if (expr_type == TypeManager.byte_type){
758 // From byte to short, ushort, int, uint, long, ulong, float, double
760 if ((target_type == TypeManager.short_type) ||
761 (target_type == TypeManager.ushort_type) ||
762 (target_type == TypeManager.int32_type) ||
763 (target_type == TypeManager.uint32_type) ||
764 (target_type == TypeManager.uint64_type) ||
765 (target_type == TypeManager.int64_type) ||
766 (target_type == TypeManager.float_type) ||
767 (target_type == TypeManager.double_type) ||
768 (target_type == TypeManager.decimal_type))
771 } else if (expr_type == TypeManager.short_type){
773 // From short to int, long, float, double
775 if ((target_type == TypeManager.int32_type) ||
776 (target_type == TypeManager.int64_type) ||
777 (target_type == TypeManager.double_type) ||
778 (target_type == TypeManager.float_type) ||
779 (target_type == TypeManager.decimal_type))
782 } else if (expr_type == TypeManager.ushort_type){
784 // From ushort to int, uint, long, ulong, float, double
786 if ((target_type == TypeManager.uint32_type) ||
787 (target_type == TypeManager.uint64_type) ||
788 (target_type == TypeManager.int32_type) ||
789 (target_type == TypeManager.int64_type) ||
790 (target_type == TypeManager.double_type) ||
791 (target_type == TypeManager.float_type) ||
792 (target_type == TypeManager.decimal_type))
795 } else if (expr_type == TypeManager.int32_type){
797 // From int to long, float, double
799 if ((target_type == TypeManager.int64_type) ||
800 (target_type == TypeManager.double_type) ||
801 (target_type == TypeManager.float_type) ||
802 (target_type == TypeManager.decimal_type))
805 } else if (expr_type == TypeManager.uint32_type){
807 // From uint to long, ulong, float, double
809 if ((target_type == TypeManager.int64_type) ||
810 (target_type == TypeManager.uint64_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.uint64_type) ||
817 (expr_type == TypeManager.int64_type)) {
819 // From long/ulong to float, double
821 if ((target_type == TypeManager.double_type) ||
822 (target_type == TypeManager.float_type) ||
823 (target_type == TypeManager.decimal_type))
826 } else if (expr_type == TypeManager.char_type){
828 // From char to ushort, int, uint, long, ulong, float, double
830 if ((target_type == TypeManager.ushort_type) ||
831 (target_type == TypeManager.int32_type) ||
832 (target_type == TypeManager.uint32_type) ||
833 (target_type == TypeManager.uint64_type) ||
834 (target_type == TypeManager.int64_type) ||
835 (target_type == TypeManager.float_type) ||
836 (target_type == TypeManager.double_type) ||
837 (target_type == TypeManager.decimal_type))
840 } else if (expr_type == TypeManager.float_type){
844 if (target_type == TypeManager.double_type)
848 // Next reference conversions
850 if (target_type == TypeManager.object_type) {
851 if ((expr_type.IsClass) ||
852 (expr_type.IsValueType))
855 } else if (expr_type.IsSubclassOf (target_type)) {
859 // from any class-type S to any interface-type T.
860 if (expr_type.IsClass && target_type.IsInterface)
863 // from any interface type S to interface-type T.
864 // FIXME : Is it right to use IsAssignableFrom ?
865 if (expr_type.IsInterface && target_type.IsInterface)
866 if (target_type.IsAssignableFrom (expr_type))
869 // from an array-type S to an array-type of type T
870 if (expr_type.IsArray && target_type.IsArray) {
871 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
873 Type expr_element_type = expr_type.GetElementType ();
874 Type target_element_type = target_type.GetElementType ();
876 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
877 if (StandardConversionExists (expr_element_type,
878 target_element_type))
883 // from an array-type to System.Array
884 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
887 // from any delegate type to System.Delegate
888 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
889 target_type == TypeManager.delegate_type)
890 if (target_type.IsAssignableFrom (expr_type))
893 // from any array-type or delegate type into System.ICloneable.
894 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
895 if (target_type == TypeManager.icloneable_type)
898 // from the null type to any reference-type.
899 // FIXME : How do we do this ?
906 static EmptyExpression MyEmptyExpr;
908 /// Tells whether an implicit conversion exists from expr_type to
911 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
914 if (MyEmptyExpr == null)
915 MyEmptyExpr = new EmptyExpression (expr_type);
917 MyEmptyExpr.SetType (expr_type);
919 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
923 /// Finds "most encompassed type" according to the spec (13.4.2)
924 /// amongst the methods in the MethodGroupExpr which convert from a
925 /// type encompassing source_type
927 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
931 for (int i = me.Methods.Length; i > 0; ) {
934 MethodBase mb = me.Methods [i];
935 ParameterData pd = Invocation.GetParameterData (mb);
936 Type param_type = pd.ParameterType (0);
938 if (StandardConversionExists (source_type, param_type)) {
942 if (StandardConversionExists (param_type, best))
951 /// Finds "most encompassing type" according to the spec (13.4.2)
952 /// amongst the methods in the MethodGroupExpr which convert to a
953 /// type encompassed by target_type
955 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
959 for (int i = me.Methods.Length; i > 0; ) {
962 MethodInfo mi = (MethodInfo) me.Methods [i];
963 Type ret_type = mi.ReturnType;
965 if (StandardConversionExists (ret_type, target)) {
969 if (!StandardConversionExists (ret_type, best))
981 /// User-defined Implicit conversions
983 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
984 Type target, Location loc)
986 return UserDefinedConversion (ec, source, target, loc, false);
990 /// User-defined Explicit conversions
992 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
993 Type target, Location loc)
995 return UserDefinedConversion (ec, source, target, loc, true);
999 /// User-defined conversions
1001 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1002 Type target, Location loc,
1003 bool look_for_explicit)
1005 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1006 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1008 MethodBase method = null;
1009 Type source_type = source.Type;
1013 // If we have a boolean type, we need to check for the True operator
1015 // FIXME : How does the False operator come into the picture ?
1016 // FIXME : This doesn't look complete and very correct !
1017 if (target == TypeManager.bool_type)
1018 op_name = "op_True";
1020 op_name = "op_Implicit";
1022 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
1024 if (source_type.BaseType != null)
1025 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1027 mg3 = MemberLookup (ec, target, op_name, false, loc);
1029 if (target.BaseType != null)
1030 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1032 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1033 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1035 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1037 MethodGroupExpr union4 = null;
1039 if (look_for_explicit) {
1041 op_name = "op_Explicit";
1043 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
1045 if (source_type.BaseType != null)
1046 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1048 mg7 = MemberLookup (ec, target, op_name, false, loc);
1050 if (target.BaseType != null)
1051 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1053 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1054 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1056 union4 = Invocation.MakeUnionSet (union5, union6);
1059 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1061 if (union != null) {
1063 Type most_specific_source, most_specific_target;
1065 most_specific_source = FindMostEncompassedType (union, source_type);
1066 if (most_specific_source == null)
1069 most_specific_target = FindMostEncompassingType (union, target);
1070 if (most_specific_target == null)
1075 for (int i = union.Methods.Length; i > 0;) {
1078 MethodBase mb = union.Methods [i];
1079 ParameterData pd = Invocation.GetParameterData (mb);
1080 MethodInfo mi = (MethodInfo) union.Methods [i];
1082 if (pd.ParameterType (0) == most_specific_source &&
1083 mi.ReturnType == most_specific_target) {
1089 if (method == null || count > 1) {
1090 Report.Error (-11, loc, "Ambiguous user defined conversion");
1095 // This will do the conversion to the best match that we
1096 // found. Now we need to perform an implict standard conversion
1097 // if the best match was not the type that we were requested
1100 if (look_for_explicit)
1101 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1103 source = ConvertImplicitStandard (ec, source,
1104 most_specific_source, loc);
1109 e = new UserCast ((MethodInfo) method, source);
1111 if (e.Type != target){
1112 if (!look_for_explicit)
1113 e = ConvertImplicitStandard (ec, e, target, loc);
1115 e = ConvertExplicitStandard (ec, e, target, loc);
1126 /// Converts implicitly the resolved expression `expr' into the
1127 /// `target_type'. It returns a new expression that can be used
1128 /// in a context that expects a `target_type'.
1130 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1131 Type target_type, Location loc)
1133 Type expr_type = expr.Type;
1136 if (expr_type == target_type)
1139 if (target_type == null)
1140 throw new Exception ("Target type is null");
1142 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1146 e = ImplicitReferenceConversion (expr, target_type);
1150 e = ImplicitUserConversion (ec, expr, target_type, loc);
1154 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1155 IntLiteral i = (IntLiteral) expr;
1158 return new EmptyCast (expr, target_type);
1166 /// Attempts to apply the `Standard Implicit
1167 /// Conversion' rules to the expression `expr' into
1168 /// the `target_type'. It returns a new expression
1169 /// that can be used in a context that expects a
1172 /// This is different from `ConvertImplicit' in that the
1173 /// user defined implicit conversions are excluded.
1175 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1176 Type target_type, Location loc)
1178 Type expr_type = expr.Type;
1181 if (expr_type == target_type)
1184 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1188 e = ImplicitReferenceConversion (expr, target_type);
1192 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1193 IntLiteral i = (IntLiteral) expr;
1196 return new EmptyCast (expr, target_type);
1202 /// Attemps to perform an implict constant conversion of the IntConstant
1203 /// into a different data type using casts (See Implicit Constant
1204 /// Expression Conversions)
1206 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1208 int value = ic.Value;
1211 // FIXME: This should really return constants instead of EmptyCasts
1213 if (target_type == TypeManager.sbyte_type){
1214 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1215 return new SByteConstant ((sbyte) value);
1216 } else if (target_type == TypeManager.byte_type){
1217 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1218 return new ByteConstant ((byte) value);
1219 } else if (target_type == TypeManager.short_type){
1220 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1221 return new ShortConstant ((short) value);
1222 } else if (target_type == TypeManager.ushort_type){
1223 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1224 return new UShortConstant ((ushort) value);
1225 } else if (target_type == TypeManager.uint32_type){
1227 return new UIntConstant ((uint) value);
1228 } else if (target_type == TypeManager.uint64_type){
1230 // we can optimize this case: a positive int32
1231 // always fits on a uint64. But we need an opcode
1235 return new ULongConstant ((ulong) value);
1238 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1239 return new EnumConstant (ic, target_type);
1245 /// Attemptes to implicityly convert `target' into `type', using
1246 /// ConvertImplicit. If there is no implicit conversion, then
1247 /// an error is signaled
1249 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1250 Type target_type, Location loc)
1254 e = ConvertImplicit (ec, source, target_type, loc);
1258 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1260 "Double literal cannot be implicitly converted to " +
1261 "float type, use F suffix to create a float literal");
1264 string msg = "Cannot convert implicitly from `"+
1265 TypeManager.CSharpName (source.Type) + "' to `" +
1266 TypeManager.CSharpName (target_type) + "'";
1268 Error (29, loc, msg);
1274 /// Performs the explicit numeric conversions
1276 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1279 Type expr_type = expr.Type;
1281 if (expr_type == TypeManager.sbyte_type){
1283 // From sbyte to byte, ushort, uint, ulong, char
1285 if (target_type == TypeManager.byte_type)
1286 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1287 if (target_type == TypeManager.ushort_type)
1288 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1289 if (target_type == TypeManager.uint32_type)
1290 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1291 if (target_type == TypeManager.uint64_type)
1292 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1293 if (target_type == TypeManager.char_type)
1294 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1295 } else if (expr_type == TypeManager.byte_type){
1297 // From byte to sbyte and char
1299 if (target_type == TypeManager.sbyte_type)
1300 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1301 if (target_type == TypeManager.char_type)
1302 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1303 } else if (expr_type == TypeManager.short_type){
1305 // From short to sbyte, byte, ushort, uint, ulong, char
1307 if (target_type == TypeManager.sbyte_type)
1308 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1309 if (target_type == TypeManager.byte_type)
1310 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1311 if (target_type == TypeManager.ushort_type)
1312 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1313 if (target_type == TypeManager.uint32_type)
1314 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1315 if (target_type == TypeManager.uint64_type)
1316 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1317 if (target_type == TypeManager.char_type)
1318 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1319 } else if (expr_type == TypeManager.ushort_type){
1321 // From ushort to sbyte, byte, short, char
1323 if (target_type == TypeManager.sbyte_type)
1324 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1325 if (target_type == TypeManager.byte_type)
1326 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1327 if (target_type == TypeManager.short_type)
1328 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1329 if (target_type == TypeManager.char_type)
1330 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1331 } else if (expr_type == TypeManager.int32_type){
1333 // From int to sbyte, byte, short, ushort, uint, ulong, char
1335 if (target_type == TypeManager.sbyte_type)
1336 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1337 if (target_type == TypeManager.byte_type)
1338 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1339 if (target_type == TypeManager.short_type)
1340 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1341 if (target_type == TypeManager.ushort_type)
1342 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1343 if (target_type == TypeManager.uint32_type)
1344 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1345 if (target_type == TypeManager.uint64_type)
1346 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1347 if (target_type == TypeManager.char_type)
1348 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1349 } else if (expr_type == TypeManager.uint32_type){
1351 // From uint to sbyte, byte, short, ushort, int, char
1353 if (target_type == TypeManager.sbyte_type)
1354 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1355 if (target_type == TypeManager.byte_type)
1356 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1357 if (target_type == TypeManager.short_type)
1358 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1359 if (target_type == TypeManager.ushort_type)
1360 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1361 if (target_type == TypeManager.int32_type)
1362 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1363 if (target_type == TypeManager.char_type)
1364 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1365 } else if (expr_type == TypeManager.int64_type){
1367 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1369 if (target_type == TypeManager.sbyte_type)
1370 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1371 if (target_type == TypeManager.byte_type)
1372 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1373 if (target_type == TypeManager.short_type)
1374 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1375 if (target_type == TypeManager.ushort_type)
1376 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1377 if (target_type == TypeManager.int32_type)
1378 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1379 if (target_type == TypeManager.uint32_type)
1380 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1381 if (target_type == TypeManager.uint64_type)
1382 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1383 if (target_type == TypeManager.char_type)
1384 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1385 } else if (expr_type == TypeManager.uint64_type){
1387 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1389 if (target_type == TypeManager.sbyte_type)
1390 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1391 if (target_type == TypeManager.byte_type)
1392 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1393 if (target_type == TypeManager.short_type)
1394 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1395 if (target_type == TypeManager.ushort_type)
1396 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1397 if (target_type == TypeManager.int32_type)
1398 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1399 if (target_type == TypeManager.uint32_type)
1400 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1401 if (target_type == TypeManager.int64_type)
1402 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1403 if (target_type == TypeManager.char_type)
1404 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1405 } else if (expr_type == TypeManager.char_type){
1407 // From char to sbyte, byte, short
1409 if (target_type == TypeManager.sbyte_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1411 if (target_type == TypeManager.byte_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1413 if (target_type == TypeManager.short_type)
1414 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1415 } else if (expr_type == TypeManager.float_type){
1417 // From float to sbyte, byte, short,
1418 // ushort, int, uint, long, ulong, char
1421 if (target_type == TypeManager.sbyte_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1423 if (target_type == TypeManager.byte_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1425 if (target_type == TypeManager.short_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1427 if (target_type == TypeManager.ushort_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1429 if (target_type == TypeManager.int32_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1431 if (target_type == TypeManager.uint32_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1433 if (target_type == TypeManager.int64_type)
1434 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1435 if (target_type == TypeManager.uint64_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1437 if (target_type == TypeManager.char_type)
1438 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1439 if (target_type == TypeManager.decimal_type)
1440 return InternalTypeConstructor (ec, expr, target_type);
1441 } else if (expr_type == TypeManager.double_type){
1443 // From double to byte, byte, short,
1444 // ushort, int, uint, long, ulong,
1445 // char, float or decimal
1447 if (target_type == TypeManager.sbyte_type)
1448 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1449 if (target_type == TypeManager.byte_type)
1450 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1451 if (target_type == TypeManager.short_type)
1452 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1453 if (target_type == TypeManager.ushort_type)
1454 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1455 if (target_type == TypeManager.int32_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1457 if (target_type == TypeManager.uint32_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1459 if (target_type == TypeManager.int64_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1461 if (target_type == TypeManager.uint64_type)
1462 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1463 if (target_type == TypeManager.char_type)
1464 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1465 if (target_type == TypeManager.float_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1467 if (target_type == TypeManager.decimal_type)
1468 return InternalTypeConstructor (ec, expr, target_type);
1471 // decimal is taken care of by the op_Explicit methods.
1477 /// Returns whether an explicit reference conversion can be performed
1478 /// from source_type to target_type
1480 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1482 bool target_is_value_type = target_type.IsValueType;
1484 if (source_type == target_type)
1488 // From object to any reference type
1490 if (source_type == TypeManager.object_type && !target_is_value_type)
1494 // From any class S to any class-type T, provided S is a base class of T
1496 if (target_type.IsSubclassOf (source_type))
1500 // From any interface type S to any interface T provided S is not derived from T
1502 if (source_type.IsInterface && target_type.IsInterface){
1503 if (!target_type.IsSubclassOf (source_type))
1508 // From any class type S to any interface T, provides S is not sealed
1509 // and provided S does not implement T.
1511 if (target_type.IsInterface && !source_type.IsSealed &&
1512 !target_type.IsAssignableFrom (source_type))
1516 // From any interface-type S to to any class type T, provided T is not
1517 // sealed, or provided T implements S.
1519 if (source_type.IsInterface &&
1520 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1523 // From an array type S with an element type Se to an array type T with an
1524 // element type Te provided all the following are true:
1525 // * S and T differe only in element type, in other words, S and T
1526 // have the same number of dimensions.
1527 // * Both Se and Te are reference types
1528 // * An explicit referenc conversions exist from Se to Te
1530 if (source_type.IsArray && target_type.IsArray) {
1531 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1533 Type source_element_type = source_type.GetElementType ();
1534 Type target_element_type = target_type.GetElementType ();
1536 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1537 if (ExplicitReferenceConversionExists (source_element_type,
1538 target_element_type))
1544 // From System.Array to any array-type
1545 if (source_type == TypeManager.array_type &&
1546 target_type.IsSubclassOf (TypeManager.array_type)){
1551 // From System delegate to any delegate-type
1553 if (source_type == TypeManager.delegate_type &&
1554 target_type.IsSubclassOf (TypeManager.delegate_type))
1558 // From ICloneable to Array or Delegate types
1560 if (source_type == TypeManager.icloneable_type &&
1561 (target_type == TypeManager.array_type ||
1562 target_type == TypeManager.delegate_type))
1569 /// Implements Explicit Reference conversions
1571 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1573 Type source_type = source.Type;
1574 bool target_is_value_type = target_type.IsValueType;
1577 // From object to any reference type
1579 if (source_type == TypeManager.object_type && !target_is_value_type)
1580 return new ClassCast (source, target_type);
1584 // From any class S to any class-type T, provided S is a base class of T
1586 if (target_type.IsSubclassOf (source_type))
1587 return new ClassCast (source, target_type);
1590 // From any interface type S to any interface T provided S is not derived from T
1592 if (source_type.IsInterface && target_type.IsInterface){
1594 Type [] ifaces = source_type.GetInterfaces ();
1596 if (TypeManager.ImplementsInterface (source_type, target_type))
1599 return new ClassCast (source, target_type);
1603 // From any class type S to any interface T, provides S is not sealed
1604 // and provided S does not implement T.
1606 if (target_type.IsInterface && !source_type.IsSealed) {
1608 if (TypeManager.ImplementsInterface (source_type, target_type))
1611 return new ClassCast (source, target_type);
1616 // From any interface-type S to to any class type T, provided T is not
1617 // sealed, or provided T implements S.
1619 if (source_type.IsInterface) {
1621 if (target_type.IsSealed)
1624 if (TypeManager.ImplementsInterface (target_type, source_type))
1625 return new ClassCast (source, target_type);
1630 // From an array type S with an element type Se to an array type T with an
1631 // element type Te provided all the following are true:
1632 // * S and T differe only in element type, in other words, S and T
1633 // have the same number of dimensions.
1634 // * Both Se and Te are reference types
1635 // * An explicit referenc conversions exist from Se to Te
1637 if (source_type.IsArray && target_type.IsArray) {
1638 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1640 Type source_element_type = source_type.GetElementType ();
1641 Type target_element_type = target_type.GetElementType ();
1643 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1644 if (ExplicitReferenceConversionExists (source_element_type,
1645 target_element_type))
1646 return new ClassCast (source, target_type);
1651 // From System.Array to any array-type
1652 if (source_type == TypeManager.array_type &&
1653 target_type.IsSubclassOf (TypeManager.array_type)){
1654 return new ClassCast (source, target_type);
1658 // From System delegate to any delegate-type
1660 if (source_type == TypeManager.delegate_type &&
1661 target_type.IsSubclassOf (TypeManager.delegate_type))
1662 return new ClassCast (source, target_type);
1665 // From ICloneable to Array or Delegate types
1667 if (source_type == TypeManager.icloneable_type &&
1668 (target_type == TypeManager.array_type ||
1669 target_type == TypeManager.delegate_type))
1670 return new ClassCast (source, target_type);
1676 /// Performs an explicit conversion of the expression `expr' whose
1677 /// type is expr.Type to `target_type'.
1679 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1680 Type target_type, Location loc)
1682 Type expr_type = expr.Type;
1683 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1688 ne = ConvertNumericExplicit (ec, expr, target_type);
1693 // Unboxing conversion.
1695 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1696 return new UnboxCast (expr, target_type);
1701 if (expr is EnumConstant) {
1702 Expression e = ((EnumConstant) expr).Child;
1704 return ConvertImplicit (ec, e, target_type, loc);
1707 ne = ConvertReferenceExplicit (expr, target_type);
1711 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1715 error30 (loc, expr_type, target_type);
1720 /// Same as ConverExplicit, only it doesn't include user defined conversions
1722 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1723 Type target_type, Location l)
1725 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1730 ne = ConvertNumericExplicit (ec, expr, target_type);
1734 ne = ConvertReferenceExplicit (expr, target_type);
1738 error30 (l, expr.Type, target_type);
1742 static string ExprClassName (ExprClass c)
1745 case ExprClass.Invalid:
1747 case ExprClass.Value:
1749 case ExprClass.Variable:
1751 case ExprClass.Namespace:
1753 case ExprClass.Type:
1755 case ExprClass.MethodGroup:
1756 return "method group";
1757 case ExprClass.PropertyAccess:
1758 return "property access";
1759 case ExprClass.EventAccess:
1760 return "event access";
1761 case ExprClass.IndexerAccess:
1762 return "indexer access";
1763 case ExprClass.Nothing:
1766 throw new Exception ("Should not happen");
1770 /// Reports that we were expecting `expr' to be of class `expected'
1772 protected void report118 (Location loc, Expression expr, string expected)
1774 string kind = "Unknown";
1777 kind = ExprClassName (expr.eclass);
1779 Error (118, loc, "Expression denotes a `" + kind +
1780 "' where a `" + expected + "' was expected");
1783 static void error31 (Location l, string val, Type t)
1785 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1786 TypeManager.CSharpName (t));
1790 /// Converts the IntConstant, UIntConstant, LongConstant or
1791 /// ULongConstant into the integral target_type. Notice
1792 /// that we do not return an `Expression' we do return
1793 /// a boxed integral type.
1795 /// FIXME: Since I added the new constants, we need to
1796 /// also support conversions from CharConstant, ByteConstant,
1797 /// SByteConstant, UShortConstant, ShortConstant
1799 /// This is used by the switch statement, so the domain
1800 /// of work is restricted to the literals above, and the
1801 /// targets are int32, uint32, char, byte, sbyte, ushort,
1802 /// short, uint64 and int64
1804 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1808 if (c.Type == target_type)
1809 return ((Constant) c).GetValue ();
1812 // Make into one of the literals we handle, we dont really care
1813 // about this value as we will just return a few limited types
1815 if (c is EnumConstant)
1816 c = ((EnumConstant)c).WidenToCompilerConstant ();
1818 if (c is IntConstant){
1819 int v = ((IntConstant) c).Value;
1821 if (target_type == TypeManager.uint32_type){
1824 } else if (target_type == TypeManager.char_type){
1825 if (v >= Char.MinValue && v <= Char.MaxValue)
1827 } else if (target_type == TypeManager.byte_type){
1828 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1830 } else if (target_type == TypeManager.sbyte_type){
1831 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1833 } else if (target_type == TypeManager.short_type){
1834 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1836 } else if (target_type == TypeManager.ushort_type){
1837 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1839 } else if (target_type == TypeManager.int64_type)
1841 else if (target_type == TypeManager.uint64_type){
1847 } else if (c is UIntConstant){
1848 uint v = ((UIntConstant) c).Value;
1850 if (target_type == TypeManager.int32_type){
1851 if (v <= Int32.MaxValue)
1853 } else if (target_type == TypeManager.char_type){
1854 if (v >= Char.MinValue && v <= Char.MaxValue)
1856 } else if (target_type == TypeManager.byte_type){
1857 if (v <= Byte.MaxValue)
1859 } else if (target_type == TypeManager.sbyte_type){
1860 if (v <= SByte.MaxValue)
1862 } else if (target_type == TypeManager.short_type){
1863 if (v <= UInt16.MaxValue)
1865 } else if (target_type == TypeManager.ushort_type){
1866 if (v <= UInt16.MaxValue)
1868 } else if (target_type == TypeManager.int64_type)
1870 else if (target_type == TypeManager.uint64_type)
1873 } else if (c is LongConstant){
1874 long v = ((LongConstant) c).Value;
1876 if (target_type == TypeManager.int32_type){
1877 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1879 } else if (target_type == TypeManager.uint32_type){
1880 if (v >= 0 && v <= UInt32.MaxValue)
1882 } else if (target_type == TypeManager.char_type){
1883 if (v >= Char.MinValue && v <= Char.MaxValue)
1885 } else if (target_type == TypeManager.byte_type){
1886 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1888 } else if (target_type == TypeManager.sbyte_type){
1889 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1891 } else if (target_type == TypeManager.short_type){
1892 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1894 } else if (target_type == TypeManager.ushort_type){
1895 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1897 } else if (target_type == TypeManager.uint64_type){
1902 } else if (c is ULongConstant){
1903 ulong v = ((ULongConstant) c).Value;
1905 if (target_type == TypeManager.int32_type){
1906 if (v <= Int32.MaxValue)
1908 } else if (target_type == TypeManager.uint32_type){
1909 if (v <= UInt32.MaxValue)
1911 } else if (target_type == TypeManager.char_type){
1912 if (v >= Char.MinValue && v <= Char.MaxValue)
1914 } else if (target_type == TypeManager.byte_type){
1915 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1917 } else if (target_type == TypeManager.sbyte_type){
1918 if (v <= (int) SByte.MaxValue)
1920 } else if (target_type == TypeManager.short_type){
1921 if (v <= UInt16.MaxValue)
1923 } else if (target_type == TypeManager.ushort_type){
1924 if (v <= UInt16.MaxValue)
1926 } else if (target_type == TypeManager.int64_type){
1927 if (v <= Int64.MaxValue)
1931 } else if (c is ByteConstant){
1932 byte v = ((ByteConstant) c).Value;
1934 if (target_type == TypeManager.int32_type)
1936 else if (target_type == TypeManager.uint32_type)
1938 else if (target_type == TypeManager.char_type)
1940 else if (target_type == TypeManager.sbyte_type){
1941 if (v <= SByte.MaxValue)
1943 } else if (target_type == TypeManager.short_type)
1945 else if (target_type == TypeManager.ushort_type)
1947 else if (target_type == TypeManager.int64_type)
1949 else if (target_type == TypeManager.uint64_type)
1952 } else if (c is SByteConstant){
1953 sbyte v = ((SByteConstant) c).Value;
1955 if (target_type == TypeManager.int32_type)
1957 else if (target_type == TypeManager.uint32_type){
1960 } else if (target_type == TypeManager.char_type){
1963 } else if (target_type == TypeManager.byte_type){
1966 } else if (target_type == TypeManager.short_type)
1968 else if (target_type == TypeManager.ushort_type){
1971 } else if (target_type == TypeManager.int64_type)
1973 else if (target_type == TypeManager.uint64_type){
1978 } else if (c is ShortConstant){
1979 short v = ((ShortConstant) c).Value;
1981 if (target_type == TypeManager.int32_type){
1983 } else if (target_type == TypeManager.uint32_type){
1986 } else if (target_type == TypeManager.char_type){
1989 } else if (target_type == TypeManager.byte_type){
1990 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1992 } else if (target_type == TypeManager.sbyte_type){
1993 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1995 } else if (target_type == TypeManager.ushort_type){
1998 } else if (target_type == TypeManager.int64_type)
2000 else if (target_type == TypeManager.uint64_type)
2004 } else if (c is UShortConstant){
2005 ushort v = ((UShortConstant) c).Value;
2007 if (target_type == TypeManager.int32_type)
2009 else if (target_type == TypeManager.uint32_type)
2011 else if (target_type == TypeManager.char_type){
2012 if (v >= Char.MinValue && v <= Char.MaxValue)
2014 } else if (target_type == TypeManager.byte_type){
2015 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2017 } else if (target_type == TypeManager.sbyte_type){
2018 if (v <= SByte.MaxValue)
2020 } else if (target_type == TypeManager.short_type){
2021 if (v <= Int16.MaxValue)
2023 } else if (target_type == TypeManager.int64_type)
2025 else if (target_type == TypeManager.uint64_type)
2029 } else if (c is CharConstant){
2030 char v = ((CharConstant) c).Value;
2032 if (target_type == TypeManager.int32_type)
2034 else if (target_type == TypeManager.uint32_type)
2036 else if (target_type == TypeManager.byte_type){
2037 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2039 } else if (target_type == TypeManager.sbyte_type){
2040 if (v <= SByte.MaxValue)
2042 } else if (target_type == TypeManager.short_type){
2043 if (v <= Int16.MaxValue)
2045 } else if (target_type == TypeManager.ushort_type)
2047 else if (target_type == TypeManager.int64_type)
2049 else if (target_type == TypeManager.uint64_type)
2054 error31 (loc, s, target_type);
2061 /// This is just a base class for expressions that can
2062 /// appear on statements (invocations, object creation,
2063 /// assignments, post/pre increment and decrement). The idea
2064 /// being that they would support an extra Emition interface that
2065 /// does not leave a result on the stack.
2067 public abstract class ExpressionStatement : Expression {
2070 /// Requests the expression to be emitted in a `statement'
2071 /// context. This means that no new value is left on the
2072 /// stack after invoking this method (constrasted with
2073 /// Emit that will always leave a value on the stack).
2075 public abstract void EmitStatement (EmitContext ec);
2079 /// This kind of cast is used to encapsulate the child
2080 /// whose type is child.Type into an expression that is
2081 /// reported to return "return_type". This is used to encapsulate
2082 /// expressions which have compatible types, but need to be dealt
2083 /// at higher levels with.
2085 /// For example, a "byte" expression could be encapsulated in one
2086 /// of these as an "unsigned int". The type for the expression
2087 /// would be "unsigned int".
2090 public class EmptyCast : Expression {
2091 protected Expression child;
2093 public EmptyCast (Expression child, Type return_type)
2095 eclass = child.eclass;
2100 public override Expression DoResolve (EmitContext ec)
2102 // This should never be invoked, we are born in fully
2103 // initialized state.
2108 public override void Emit (EmitContext ec)
2115 /// This class is used to wrap literals which belong inside Enums
2117 public class EnumConstant : Constant {
2118 public Constant Child;
2120 public EnumConstant (Constant child, Type enum_type)
2122 eclass = child.eclass;
2127 public override Expression DoResolve (EmitContext ec)
2129 // This should never be invoked, we are born in fully
2130 // initialized state.
2135 public override void Emit (EmitContext ec)
2140 public override object GetValue ()
2142 return Child.GetValue ();
2146 // Converts from one of the valid underlying types for an enumeration
2147 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2148 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2150 public Constant WidenToCompilerConstant ()
2152 Type t = Child.Type.UnderlyingSystemType;
2153 object v = ((Constant) Child).GetValue ();;
2155 if (t == TypeManager.int32_type)
2156 return new IntConstant ((int) v);
2157 if (t == TypeManager.uint32_type)
2158 return new UIntConstant ((uint) v);
2159 if (t == TypeManager.int64_type)
2160 return new LongConstant ((long) v);
2161 if (t == TypeManager.uint64_type)
2162 return new ULongConstant ((ulong) v);
2163 if (t == TypeManager.short_type)
2164 return new ShortConstant ((short) v);
2165 if (t == TypeManager.ushort_type)
2166 return new UShortConstant ((ushort) v);
2167 if (t == TypeManager.byte_type)
2168 return new ByteConstant ((byte) v);
2169 if (t == TypeManager.sbyte_type)
2170 return new SByteConstant ((sbyte) v);
2172 throw new Exception ("Invalid enumeration underlying type: " + t);
2176 // Extracts the value in the enumeration on its native representation
2178 public object GetPlainValue ()
2180 Type t = Child.Type.UnderlyingSystemType;
2181 object v = ((Constant) Child).GetValue ();;
2183 if (t == TypeManager.int32_type)
2185 if (t == TypeManager.uint32_type)
2187 if (t == TypeManager.int64_type)
2189 if (t == TypeManager.uint64_type)
2191 if (t == TypeManager.short_type)
2193 if (t == TypeManager.ushort_type)
2195 if (t == TypeManager.byte_type)
2197 if (t == TypeManager.sbyte_type)
2203 public override string AsString ()
2205 return Child.AsString ();
2210 /// This kind of cast is used to encapsulate Value Types in objects.
2212 /// The effect of it is to box the value type emitted by the previous
2215 public class BoxedCast : EmptyCast {
2217 public BoxedCast (Expression expr)
2218 : base (expr, TypeManager.object_type)
2222 public override Expression DoResolve (EmitContext ec)
2224 // This should never be invoked, we are born in fully
2225 // initialized state.
2230 public override void Emit (EmitContext ec)
2233 ec.ig.Emit (OpCodes.Box, child.Type);
2237 public class UnboxCast : EmptyCast {
2238 public UnboxCast (Expression expr, Type return_type)
2239 : base (expr, return_type)
2243 public override Expression DoResolve (EmitContext ec)
2245 // This should never be invoked, we are born in fully
2246 // initialized state.
2251 public override void Emit (EmitContext ec)
2254 ILGenerator ig = ec.ig;
2257 ig.Emit (OpCodes.Unbox, t);
2260 // Load the object from the pointer
2262 if (t == TypeManager.int32_type)
2263 ig.Emit (OpCodes.Ldind_I4);
2264 else if (t == TypeManager.uint32_type)
2265 ig.Emit (OpCodes.Ldind_U4);
2266 else if (t == TypeManager.short_type)
2267 ig.Emit (OpCodes.Ldind_I2);
2268 else if (t == TypeManager.ushort_type)
2269 ig.Emit (OpCodes.Ldind_U2);
2270 else if (t == TypeManager.char_type)
2271 ig.Emit (OpCodes.Ldind_U2);
2272 else if (t == TypeManager.byte_type)
2273 ig.Emit (OpCodes.Ldind_U1);
2274 else if (t == TypeManager.sbyte_type)
2275 ig.Emit (OpCodes.Ldind_I1);
2276 else if (t == TypeManager.uint64_type)
2277 ig.Emit (OpCodes.Ldind_I8);
2278 else if (t == TypeManager.int64_type)
2279 ig.Emit (OpCodes.Ldind_I8);
2280 else if (t == TypeManager.float_type)
2281 ig.Emit (OpCodes.Ldind_R4);
2282 else if (t == TypeManager.double_type)
2283 ig.Emit (OpCodes.Ldind_R8);
2284 else if (t == TypeManager.bool_type)
2285 ig.Emit (OpCodes.Ldind_I1);
2286 else if (t == TypeManager.intptr_type)
2287 ig.Emit (OpCodes.Ldind_I);
2289 ig.Emit (OpCodes.Ldobj, t);
2294 /// This is used to perform explicit numeric conversions.
2296 /// Explicit numeric conversions might trigger exceptions in a checked
2297 /// context, so they should generate the conv.ovf opcodes instead of
2300 public class ConvCast : EmptyCast {
2301 public enum Mode : byte {
2302 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2304 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2305 U2_I1, U2_U1, U2_I2, U2_CH,
2306 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2307 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2308 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2309 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2310 CH_I1, CH_U1, CH_I2,
2311 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2312 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2317 public ConvCast (Expression child, Type return_type, Mode m)
2318 : base (child, return_type)
2323 public override Expression DoResolve (EmitContext ec)
2325 // This should never be invoked, we are born in fully
2326 // initialized state.
2331 public override void Emit (EmitContext ec)
2333 ILGenerator ig = ec.ig;
2339 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2340 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2341 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2342 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2343 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2345 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2346 case Mode.U1_CH: /* nothing */ break;
2348 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2349 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2350 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2351 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2352 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2353 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2355 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2356 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2357 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2358 case Mode.U2_CH: /* nothing */ break;
2360 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2361 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2362 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2363 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2364 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2365 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2366 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2368 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2369 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2370 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2371 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2372 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2373 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2375 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2376 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2377 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2378 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2379 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2380 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2381 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2382 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2384 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2385 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2386 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2387 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2388 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2389 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2390 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2391 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2393 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2394 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2395 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2397 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2398 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2399 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2400 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2401 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2402 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2403 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2404 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2405 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2407 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2408 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2409 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2410 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2411 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2412 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2413 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2414 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2415 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2416 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2420 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2421 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2422 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2423 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2424 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2426 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2427 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2429 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2430 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2431 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2432 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2433 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2434 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2436 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2437 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2438 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2439 case Mode.U2_CH: /* nothing */ break;
2441 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2442 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2443 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2444 case Mode.I4_U4: /* nothing */ break;
2445 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2446 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2447 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2449 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2450 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2451 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2452 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2453 case Mode.U4_I4: /* nothing */ break;
2454 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2456 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2457 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2458 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2459 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2460 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2461 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2462 case Mode.I8_U8: /* nothing */ break;
2463 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2465 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2466 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2467 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2468 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2469 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2470 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2471 case Mode.U8_I8: /* nothing */ break;
2472 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2474 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2475 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2476 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2478 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2479 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2480 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2481 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2482 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2483 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2484 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2485 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2486 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2488 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2489 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2490 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2491 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2492 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2493 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2494 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2495 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2496 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2497 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2503 public class OpcodeCast : EmptyCast {
2507 public OpcodeCast (Expression child, Type return_type, OpCode op)
2508 : base (child, return_type)
2512 second_valid = false;
2515 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2516 : base (child, return_type)
2521 second_valid = true;
2524 public override Expression DoResolve (EmitContext ec)
2526 // This should never be invoked, we are born in fully
2527 // initialized state.
2532 public override void Emit (EmitContext ec)
2543 /// This kind of cast is used to encapsulate a child and cast it
2544 /// to the class requested
2546 public class ClassCast : EmptyCast {
2547 public ClassCast (Expression child, Type return_type)
2548 : base (child, return_type)
2553 public override Expression DoResolve (EmitContext ec)
2555 // This should never be invoked, we are born in fully
2556 // initialized state.
2561 public override void Emit (EmitContext ec)
2565 ec.ig.Emit (OpCodes.Castclass, type);
2571 /// SimpleName expressions are initially formed of a single
2572 /// word and it only happens at the beginning of the expression.
2576 /// The expression will try to be bound to a Field, a Method
2577 /// group or a Property. If those fail we pass the name to our
2578 /// caller and the SimpleName is compounded to perform a type
2579 /// lookup. The idea behind this process is that we want to avoid
2580 /// creating a namespace map from the assemblies, as that requires
2581 /// the GetExportedTypes function to be called and a hashtable to
2582 /// be constructed which reduces startup time. If later we find
2583 /// that this is slower, we should create a `NamespaceExpr' expression
2584 /// that fully participates in the resolution process.
2586 /// For example `System.Console.WriteLine' is decomposed into
2587 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2589 /// The first SimpleName wont produce a match on its own, so it will
2591 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2593 /// System.Console will produce a TypeExpr match.
2595 /// The downside of this is that we might be hitting `LookupType' too many
2596 /// times with this scheme.
2598 public class SimpleName : Expression {
2599 public readonly string Name;
2600 public readonly Location Location;
2602 public SimpleName (string name, Location l)
2608 public static void Error120 (Location l, string name)
2612 "An object reference is required " +
2613 "for the non-static field `"+name+"'");
2617 // Checks whether we are trying to access an instance
2618 // property, method or field from a static body.
2620 Expression MemberStaticCheck (Expression e)
2622 if (e is FieldExpr){
2623 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2626 Error120 (Location, Name);
2629 } else if (e is MethodGroupExpr){
2630 MethodGroupExpr mg = (MethodGroupExpr) e;
2632 if (!mg.RemoveInstanceMethods ()){
2633 Error120 (Location, mg.Methods [0].Name);
2637 } else if (e is PropertyExpr){
2638 if (!((PropertyExpr) e).IsStatic){
2639 Error120 (Location, Name);
2642 } else if (e is EventExpr) {
2643 if (!((EventExpr) e).IsStatic) {
2644 Error120 (Location, Name);
2653 // 7.5.2: Simple Names.
2655 // Local Variables and Parameters are handled at
2656 // parse time, so they never occur as SimpleNames.
2658 public override Expression DoResolve (EmitContext ec)
2663 // Stage 1: Performed by the parser (binding to locals or parameters).
2667 // Stage 2: Lookup members
2669 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2672 // Stage 3: Lookup symbol in the various namespaces.
2674 DeclSpace ds = ec.TypeContainer;
2678 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2679 return new TypeExpr (t);
2682 // Stage 2 part b: Lookup up if we are an alias to a type
2685 // Since we are cheating: we only do the Alias lookup for
2686 // namespaces if the name does not include any dots in it
2689 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2690 // System.Console.WriteLine (Name + " --> " + alias_value);
2691 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2693 return new TypeExpr (t);
2695 // we have alias value, but it isn't Type, so try if it's namespace
2696 return new SimpleName (alias_value, Location);
2699 // No match, maybe our parent can compose us
2700 // into something meaningful.
2705 // Stage 2 continues here.
2710 if (e is FieldExpr){
2711 FieldExpr fe = (FieldExpr) e;
2713 if (!fe.FieldInfo.IsStatic){
2714 This this_expr = new This (Location.Null);
2716 fe.InstanceExpression = this_expr.DoResolve (ec);
2719 FieldInfo fi = fe.FieldInfo;
2721 if (fi is FieldBuilder) {
2722 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2725 object o = c.LookupConstantValue (ec);
2726 object real_value = ((Constant)c.Expr).GetValue ();
2727 return Constantify (real_value, fi.FieldType);
2732 if (e is EventExpr) {
2734 // If the event is local to this class, we transform ourselves into
2737 EventExpr ee = (EventExpr) e;
2739 Expression ml = MemberLookup (ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
2740 true, MemberTypes.Event, AllBindingFlags, Location);
2743 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
2745 ml = ExprClassFromMemberInfo (ec, mi, Location);
2748 Report.Error (-200, Location, "Internal error!!");
2752 Expression instance_expr;
2754 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
2757 instance_expr = null;
2759 instance_expr = new This (Location.Null);
2761 instance_expr = instance_expr.Resolve (ec);
2763 if (instance_expr != null)
2764 instance_expr = instance_expr.Resolve (ec);
2766 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location);
2772 return MemberStaticCheck (e);
2777 public override void Emit (EmitContext ec)
2780 // If this is ever reached, then we failed to
2781 // find the name as a namespace
2784 Error (103, Location, "The name `" + Name +
2785 "' does not exist in the class `" +
2786 ec.TypeContainer.Name + "'");
2791 /// Fully resolved expression that evaluates to a type
2793 public class TypeExpr : Expression {
2794 public TypeExpr (Type t)
2797 eclass = ExprClass.Type;
2800 override public Expression DoResolve (EmitContext ec)
2805 override public void Emit (EmitContext ec)
2807 throw new Exception ("Implement me");
2812 /// MethodGroup Expression.
2814 /// This is a fully resolved expression that evaluates to a type
2816 public class MethodGroupExpr : Expression {
2817 public MethodBase [] Methods;
2818 Expression instance_expression = null;
2820 public MethodGroupExpr (MemberInfo [] mi)
2822 Methods = new MethodBase [mi.Length];
2823 mi.CopyTo (Methods, 0);
2824 eclass = ExprClass.MethodGroup;
2827 public MethodGroupExpr (ArrayList l)
2829 Methods = new MethodBase [l.Count];
2831 l.CopyTo (Methods, 0);
2832 eclass = ExprClass.MethodGroup;
2836 // `A method group may have associated an instance expression'
2838 public Expression InstanceExpression {
2840 return instance_expression;
2844 instance_expression = value;
2848 override public Expression DoResolve (EmitContext ec)
2853 override public void Emit (EmitContext ec)
2855 throw new Exception ("This should never be reached");
2858 bool RemoveMethods (bool keep_static)
2860 ArrayList smethods = new ArrayList ();
2861 int top = Methods.Length;
2864 for (i = 0; i < top; i++){
2865 MethodBase mb = Methods [i];
2867 if (mb.IsStatic == keep_static)
2871 if (smethods.Count == 0)
2874 Methods = new MethodBase [smethods.Count];
2875 smethods.CopyTo (Methods, 0);
2881 /// Removes any instance methods from the MethodGroup, returns
2882 /// false if the resulting set is empty.
2884 public bool RemoveInstanceMethods ()
2886 return RemoveMethods (true);
2890 /// Removes any static methods from the MethodGroup, returns
2891 /// false if the resulting set is empty.
2893 public bool RemoveStaticMethods ()
2895 return RemoveMethods (false);
2900 /// Fully resolved expression that evaluates to a Field
2902 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2903 public readonly FieldInfo FieldInfo;
2904 public Expression InstanceExpression;
2907 public FieldExpr (FieldInfo fi, Location l)
2910 eclass = ExprClass.Variable;
2911 type = fi.FieldType;
2915 override public Expression DoResolve (EmitContext ec)
2917 if (!FieldInfo.IsStatic){
2918 if (InstanceExpression == null){
2919 throw new Exception ("non-static FieldExpr without instance var\n" +
2920 "You have to assign the Instance variable\n" +
2921 "Of the FieldExpr to set this\n");
2924 InstanceExpression = InstanceExpression.Resolve (ec);
2925 if (InstanceExpression == null)
2933 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2935 Expression e = DoResolve (ec);
2940 if (!FieldInfo.IsInitOnly)
2944 // InitOnly fields can only be assigned in constructors
2947 if (ec.IsConstructor)
2950 Report.Error (191, loc,
2951 "Readonly field can not be assigned outside " +
2952 "of constructor or variable initializer");
2957 override public void Emit (EmitContext ec)
2959 ILGenerator ig = ec.ig;
2961 if (FieldInfo.IsStatic)
2962 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2964 if (InstanceExpression.Type.IsValueType){
2967 if (!(InstanceExpression is IMemoryLocation)){
2968 LocalTemporary tempo = new LocalTemporary (
2969 ec, InstanceExpression.Type);
2971 InstanceExpression.Emit (ec);
2975 ml = (IMemoryLocation) InstanceExpression;
2979 InstanceExpression.Emit (ec);
2981 ig.Emit (OpCodes.Ldfld, FieldInfo);
2985 public void EmitAssign (EmitContext ec, Expression source)
2987 bool is_static = FieldInfo.IsStatic;
2990 Expression instance = InstanceExpression;
2992 if (instance.Type.IsValueType){
2993 if (instance is IMemoryLocation){
2994 IMemoryLocation ml = (IMemoryLocation) instance;
2998 throw new Exception ("The " + instance + " of type " +
3000 " represents a ValueType and does " +
3001 "not implement IMemoryLocation");
3008 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3010 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3014 public void AddressOf (EmitContext ec)
3016 if (FieldInfo.IsStatic)
3017 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3019 InstanceExpression.Emit (ec);
3020 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3026 /// Expression that evaluates to a Property. The Assign class
3027 /// might set the `Value' expression if we are in an assignment.
3029 /// This is not an LValue because we need to re-write the expression, we
3030 /// can not take data from the stack and store it.
3032 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3033 public readonly PropertyInfo PropertyInfo;
3034 public readonly bool IsStatic;
3035 MethodInfo [] Accessors;
3038 Expression instance_expr;
3040 public PropertyExpr (PropertyInfo pi, Location l)
3043 eclass = ExprClass.PropertyAccess;
3046 Accessors = TypeManager.GetAccessors (pi);
3048 if (Accessors != null)
3049 for (int i = 0; i < Accessors.Length; i++){
3050 if (Accessors [i] != null)
3051 if (Accessors [i].IsStatic)
3055 Accessors = new MethodInfo [2];
3057 type = pi.PropertyType;
3061 // The instance expression associated with this expression
3063 public Expression InstanceExpression {
3065 instance_expr = value;
3069 return instance_expr;
3073 public bool VerifyAssignable ()
3075 if (!PropertyInfo.CanWrite){
3076 Report.Error (200, loc,
3077 "The property `" + PropertyInfo.Name +
3078 "' can not be assigned to, as it has not set accessor");
3085 override public Expression DoResolve (EmitContext ec)
3087 if (!PropertyInfo.CanRead){
3088 Report.Error (154, loc,
3089 "The property `" + PropertyInfo.Name +
3090 "' can not be used in " +
3091 "this context because it lacks a get accessor");
3095 type = PropertyInfo.PropertyType;
3100 override public void Emit (EmitContext ec)
3102 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
3107 // Implements the IAssignMethod interface for assignments
3109 public void EmitAssign (EmitContext ec, Expression source)
3111 Argument arg = new Argument (source, Argument.AType.Expression);
3112 ArrayList args = new ArrayList ();
3115 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
3118 override public void EmitStatement (EmitContext ec)
3121 ec.ig.Emit (OpCodes.Pop);
3126 /// Fully resolved expression that evaluates to an Event
3128 public class EventExpr : Expression {
3129 public readonly EventInfo EventInfo;
3131 public Expression InstanceExpression;
3133 public readonly bool IsStatic;
3135 MethodInfo add_accessor, remove_accessor;
3137 public EventExpr (EventInfo ei, Location loc)
3141 eclass = ExprClass.EventAccess;
3143 add_accessor = TypeManager.GetAddMethod (ei);
3144 remove_accessor = TypeManager.GetRemoveMethod (ei);
3146 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3149 if (EventInfo is MyEventBuilder)
3150 type = ((MyEventBuilder) EventInfo).EventType;
3152 type = EventInfo.EventHandlerType;
3155 override public Expression DoResolve (EmitContext ec)
3157 // We are born fully resolved
3161 override public void Emit (EmitContext ec)
3163 throw new Exception ("Should not happen I think");
3166 public void EmitAddOrRemove (EmitContext ec, Expression source)
3168 Expression handler = ((Binary) source).Right;
3170 Argument arg = new Argument (handler, Argument.AType.Expression);
3171 ArrayList args = new ArrayList ();
3175 if (((Binary) source).Oper == Binary.Operator.Addition)
3176 Invocation.EmitCall (ec, IsStatic, InstanceExpression, add_accessor, args);
3178 Invocation.EmitCall (ec, IsStatic, InstanceExpression, remove_accessor, args);