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 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 AllBindingsFlags =
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, AllBindingsFlags, 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);
1242 /// Attemptes to implicityly convert `target' into `type', using
1243 /// ConvertImplicit. If there is no implicit conversion, then
1244 /// an error is signaled
1246 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1247 Type target_type, Location loc)
1251 e = ConvertImplicit (ec, source, target_type, loc);
1255 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1257 "Double literal cannot be implicitly converted to " +
1258 "float type, use F suffix to create a float literal");
1261 string msg = "Cannot convert implicitly from `"+
1262 TypeManager.CSharpName (source.Type) + "' to `" +
1263 TypeManager.CSharpName (target_type) + "'";
1265 Error (29, loc, msg);
1271 /// Performs the explicit numeric conversions
1273 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1276 Type expr_type = expr.Type;
1278 if (expr_type == TypeManager.sbyte_type){
1280 // From sbyte to byte, ushort, uint, ulong, char
1282 if (target_type == TypeManager.byte_type)
1283 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1284 if (target_type == TypeManager.ushort_type)
1285 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1286 if (target_type == TypeManager.uint32_type)
1287 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1288 if (target_type == TypeManager.uint64_type)
1289 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1290 if (target_type == TypeManager.char_type)
1291 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1292 } else if (expr_type == TypeManager.byte_type){
1294 // From byte to sbyte and char
1296 if (target_type == TypeManager.sbyte_type)
1297 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1298 if (target_type == TypeManager.char_type)
1299 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1300 } else if (expr_type == TypeManager.short_type){
1302 // From short to sbyte, byte, ushort, uint, ulong, char
1304 if (target_type == TypeManager.sbyte_type)
1305 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1306 if (target_type == TypeManager.byte_type)
1307 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1308 if (target_type == TypeManager.ushort_type)
1309 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1310 if (target_type == TypeManager.uint32_type)
1311 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1312 if (target_type == TypeManager.uint64_type)
1313 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1314 if (target_type == TypeManager.char_type)
1315 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1316 } else if (expr_type == TypeManager.ushort_type){
1318 // From ushort to sbyte, byte, short, char
1320 if (target_type == TypeManager.sbyte_type)
1321 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1322 if (target_type == TypeManager.byte_type)
1323 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1324 if (target_type == TypeManager.short_type)
1325 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1326 if (target_type == TypeManager.char_type)
1327 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1328 } else if (expr_type == TypeManager.int32_type){
1330 // From int to sbyte, byte, short, ushort, uint, ulong, char
1332 if (target_type == TypeManager.sbyte_type)
1333 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1334 if (target_type == TypeManager.byte_type)
1335 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1336 if (target_type == TypeManager.short_type)
1337 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1338 if (target_type == TypeManager.ushort_type)
1339 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1340 if (target_type == TypeManager.uint32_type)
1341 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1342 if (target_type == TypeManager.uint64_type)
1343 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1344 if (target_type == TypeManager.char_type)
1345 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1346 } else if (expr_type == TypeManager.uint32_type){
1348 // From uint to sbyte, byte, short, ushort, int, char
1350 if (target_type == TypeManager.sbyte_type)
1351 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1352 if (target_type == TypeManager.byte_type)
1353 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1354 if (target_type == TypeManager.short_type)
1355 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1356 if (target_type == TypeManager.ushort_type)
1357 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1358 if (target_type == TypeManager.int32_type)
1359 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1360 if (target_type == TypeManager.char_type)
1361 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1362 } else if (expr_type == TypeManager.int64_type){
1364 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1366 if (target_type == TypeManager.sbyte_type)
1367 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1368 if (target_type == TypeManager.byte_type)
1369 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1370 if (target_type == TypeManager.short_type)
1371 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1372 if (target_type == TypeManager.ushort_type)
1373 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1374 if (target_type == TypeManager.int32_type)
1375 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1376 if (target_type == TypeManager.uint32_type)
1377 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1378 if (target_type == TypeManager.uint64_type)
1379 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1380 if (target_type == TypeManager.char_type)
1381 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1382 } else if (expr_type == TypeManager.uint64_type){
1384 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1386 if (target_type == TypeManager.sbyte_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1388 if (target_type == TypeManager.byte_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1390 if (target_type == TypeManager.short_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1392 if (target_type == TypeManager.ushort_type)
1393 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1394 if (target_type == TypeManager.int32_type)
1395 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1396 if (target_type == TypeManager.uint32_type)
1397 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1398 if (target_type == TypeManager.int64_type)
1399 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1400 if (target_type == TypeManager.char_type)
1401 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1402 } else if (expr_type == TypeManager.char_type){
1404 // From char to sbyte, byte, short
1406 if (target_type == TypeManager.sbyte_type)
1407 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1408 if (target_type == TypeManager.byte_type)
1409 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1410 if (target_type == TypeManager.short_type)
1411 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1412 } else if (expr_type == TypeManager.float_type){
1414 // From float to sbyte, byte, short,
1415 // ushort, int, uint, long, ulong, char
1418 if (target_type == TypeManager.sbyte_type)
1419 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1420 if (target_type == TypeManager.byte_type)
1421 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1422 if (target_type == TypeManager.short_type)
1423 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1424 if (target_type == TypeManager.ushort_type)
1425 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1426 if (target_type == TypeManager.int32_type)
1427 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1428 if (target_type == TypeManager.uint32_type)
1429 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1430 if (target_type == TypeManager.int64_type)
1431 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1432 if (target_type == TypeManager.uint64_type)
1433 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1434 if (target_type == TypeManager.char_type)
1435 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1436 if (target_type == TypeManager.decimal_type)
1437 return InternalTypeConstructor (ec, expr, target_type);
1438 } else if (expr_type == TypeManager.double_type){
1440 // From double to byte, byte, short,
1441 // ushort, int, uint, long, ulong,
1442 // char, float or decimal
1444 if (target_type == TypeManager.sbyte_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1446 if (target_type == TypeManager.byte_type)
1447 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1448 if (target_type == TypeManager.short_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1450 if (target_type == TypeManager.ushort_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1452 if (target_type == TypeManager.int32_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1454 if (target_type == TypeManager.uint32_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1456 if (target_type == TypeManager.int64_type)
1457 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1458 if (target_type == TypeManager.uint64_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1460 if (target_type == TypeManager.char_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1462 if (target_type == TypeManager.float_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1464 if (target_type == TypeManager.decimal_type)
1465 return InternalTypeConstructor (ec, expr, target_type);
1468 // decimal is taken care of by the op_Explicit methods.
1474 /// Returns whether an explicit reference conversion can be performed
1475 /// from source_type to target_type
1477 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1479 bool target_is_value_type = target_type.IsValueType;
1481 if (source_type == target_type)
1485 // From object to any reference type
1487 if (source_type == TypeManager.object_type && !target_is_value_type)
1491 // From any class S to any class-type T, provided S is a base class of T
1493 if (target_type.IsSubclassOf (source_type))
1497 // From any interface type S to any interface T provided S is not derived from T
1499 if (source_type.IsInterface && target_type.IsInterface){
1500 if (!target_type.IsSubclassOf (source_type))
1505 // From any class type S to any interface T, provides S is not sealed
1506 // and provided S does not implement T.
1508 if (target_type.IsInterface && !source_type.IsSealed &&
1509 !target_type.IsAssignableFrom (source_type))
1513 // From any interface-type S to to any class type T, provided T is not
1514 // sealed, or provided T implements S.
1516 if (source_type.IsInterface &&
1517 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1520 // From an array type S with an element type Se to an array type T with an
1521 // element type Te provided all the following are true:
1522 // * S and T differe only in element type, in other words, S and T
1523 // have the same number of dimensions.
1524 // * Both Se and Te are reference types
1525 // * An explicit referenc conversions exist from Se to Te
1527 if (source_type.IsArray && target_type.IsArray) {
1528 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1530 Type source_element_type = source_type.GetElementType ();
1531 Type target_element_type = target_type.GetElementType ();
1533 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1534 if (ExplicitReferenceConversionExists (source_element_type,
1535 target_element_type))
1541 // From System.Array to any array-type
1542 if (source_type == TypeManager.array_type &&
1543 target_type.IsSubclassOf (TypeManager.array_type)){
1548 // From System delegate to any delegate-type
1550 if (source_type == TypeManager.delegate_type &&
1551 target_type.IsSubclassOf (TypeManager.delegate_type))
1555 // From ICloneable to Array or Delegate types
1557 if (source_type == TypeManager.icloneable_type &&
1558 (target_type == TypeManager.array_type ||
1559 target_type == TypeManager.delegate_type))
1566 /// Implements Explicit Reference conversions
1568 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1570 Type source_type = source.Type;
1571 bool target_is_value_type = target_type.IsValueType;
1574 // From object to any reference type
1576 if (source_type == TypeManager.object_type && !target_is_value_type)
1577 return new ClassCast (source, target_type);
1581 // From any class S to any class-type T, provided S is a base class of T
1583 if (target_type.IsSubclassOf (source_type))
1584 return new ClassCast (source, target_type);
1587 // From any interface type S to any interface T provided S is not derived from T
1589 if (source_type.IsInterface && target_type.IsInterface){
1591 Type [] ifaces = source_type.GetInterfaces ();
1593 if (TypeManager.ImplementsInterface (source_type, target_type))
1596 return new ClassCast (source, target_type);
1600 // From any class type S to any interface T, provides S is not sealed
1601 // and provided S does not implement T.
1603 if (target_type.IsInterface && !source_type.IsSealed) {
1605 if (TypeManager.ImplementsInterface (source_type, target_type))
1608 return new ClassCast (source, target_type);
1613 // From any interface-type S to to any class type T, provided T is not
1614 // sealed, or provided T implements S.
1616 if (source_type.IsInterface) {
1618 if (target_type.IsSealed)
1621 if (TypeManager.ImplementsInterface (target_type, source_type))
1622 return new ClassCast (source, target_type);
1627 // From an array type S with an element type Se to an array type T with an
1628 // element type Te provided all the following are true:
1629 // * S and T differe only in element type, in other words, S and T
1630 // have the same number of dimensions.
1631 // * Both Se and Te are reference types
1632 // * An explicit referenc conversions exist from Se to Te
1634 if (source_type.IsArray && target_type.IsArray) {
1635 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1637 Type source_element_type = source_type.GetElementType ();
1638 Type target_element_type = target_type.GetElementType ();
1640 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1641 if (ExplicitReferenceConversionExists (source_element_type,
1642 target_element_type))
1643 return new ClassCast (source, target_type);
1648 // From System.Array to any array-type
1649 if (source_type == TypeManager.array_type &&
1650 target_type.IsSubclassOf (TypeManager.array_type)){
1651 return new ClassCast (source, target_type);
1655 // From System delegate to any delegate-type
1657 if (source_type == TypeManager.delegate_type &&
1658 target_type.IsSubclassOf (TypeManager.delegate_type))
1659 return new ClassCast (source, target_type);
1662 // From ICloneable to Array or Delegate types
1664 if (source_type == TypeManager.icloneable_type &&
1665 (target_type == TypeManager.array_type ||
1666 target_type == TypeManager.delegate_type))
1667 return new ClassCast (source, target_type);
1673 /// Performs an explicit conversion of the expression `expr' whose
1674 /// type is expr.Type to `target_type'.
1676 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1677 Type target_type, Location loc)
1679 Type expr_type = expr.Type;
1680 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1685 ne = ConvertNumericExplicit (ec, expr, target_type);
1690 // Unboxing conversion.
1692 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1693 return new UnboxCast (expr, target_type);
1698 if (expr is EnumConstant) {
1699 Expression e = ((EnumConstant) expr).Child;
1701 return ConvertImplicit (ec, e, target_type, loc);
1704 ne = ConvertReferenceExplicit (expr, target_type);
1708 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1712 error30 (loc, expr_type, target_type);
1717 /// Same as ConverExplicit, only it doesn't include user defined conversions
1719 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1720 Type target_type, Location l)
1722 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1727 ne = ConvertNumericExplicit (ec, expr, target_type);
1731 ne = ConvertReferenceExplicit (expr, target_type);
1735 error30 (l, expr.Type, target_type);
1739 static string ExprClassName (ExprClass c)
1742 case ExprClass.Invalid:
1744 case ExprClass.Value:
1746 case ExprClass.Variable:
1748 case ExprClass.Namespace:
1750 case ExprClass.Type:
1752 case ExprClass.MethodGroup:
1753 return "method group";
1754 case ExprClass.PropertyAccess:
1755 return "property access";
1756 case ExprClass.EventAccess:
1757 return "event access";
1758 case ExprClass.IndexerAccess:
1759 return "indexer access";
1760 case ExprClass.Nothing:
1763 throw new Exception ("Should not happen");
1767 /// Reports that we were expecting `expr' to be of class `expected'
1769 protected void report118 (Location loc, Expression expr, string expected)
1771 string kind = "Unknown";
1774 kind = ExprClassName (expr.eclass);
1776 Error (118, loc, "Expression denotes a `" + kind +
1777 "' where a `" + expected + "' was expected");
1780 static void error31 (Location l, string val, Type t)
1782 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1783 TypeManager.CSharpName (t));
1787 /// Converts the IntConstant, UIntConstant, LongConstant or
1788 /// ULongConstant into the integral target_type. Notice
1789 /// that we do not return an `Expression' we do return
1790 /// a boxed integral type.
1792 /// FIXME: Since I added the new constants, we need to
1793 /// also support conversions from CharConstant, ByteConstant,
1794 /// SByteConstant, UShortConstant, ShortConstant
1796 /// This is used by the switch statement, so the domain
1797 /// of work is restricted to the literals above, and the
1798 /// targets are int32, uint32, char, byte, sbyte, ushort,
1799 /// short, uint64 and int64
1801 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1805 if (c.Type == target_type)
1809 // Make into one of the literals we handle, we dont really care
1810 // about this value as we will just return a few limited types
1812 if (c is EnumConstant)
1813 c = ((EnumConstant)c).WidenToCompilerConstant ();
1815 if (c is IntConstant){
1816 int v = ((IntConstant) c).Value;
1818 if (target_type == TypeManager.uint32_type){
1820 return (object) ((uint) v);
1821 } else if (target_type == TypeManager.char_type){
1822 if (v >= Char.MinValue && v <= Char.MaxValue)
1823 return (object) ((char) v);
1824 } else if (target_type == TypeManager.byte_type){
1825 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1826 return (object) ((byte) v);
1827 } else if (target_type == TypeManager.sbyte_type){
1828 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1829 return (object) ((sbyte) v);
1830 } else if (target_type == TypeManager.short_type){
1831 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1832 return (object) ((short) v);
1833 } else if (target_type == TypeManager.ushort_type){
1834 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1835 return (object) ((ushort) v);
1836 } else if (target_type == TypeManager.int64_type)
1837 return (object) ((long) v);
1838 else if (target_type == TypeManager.uint64_type){
1840 return (object) ((ulong) v);
1844 } else if (c is UIntConstant){
1845 uint v = ((UIntConstant) c).Value;
1847 if (target_type == TypeManager.int32_type){
1848 if (v <= Int32.MaxValue)
1849 return (object) ((int) v);
1850 } else if (target_type == TypeManager.char_type){
1851 if (v >= Char.MinValue && v <= Char.MaxValue)
1852 return (object) ((char) v);
1853 } else if (target_type == TypeManager.byte_type){
1854 if (v <= Byte.MaxValue)
1855 return (object) ((byte) v);
1856 } else if (target_type == TypeManager.sbyte_type){
1857 if (v <= SByte.MaxValue)
1858 return (object) ((sbyte) v);
1859 } else if (target_type == TypeManager.short_type){
1860 if (v <= UInt16.MaxValue)
1861 return (object) ((short) v);
1862 } else if (target_type == TypeManager.ushort_type){
1863 if (v <= UInt16.MaxValue)
1864 return (object) ((ushort) v);
1865 } else if (target_type == TypeManager.int64_type)
1866 return (object) ((long) v);
1867 else if (target_type == TypeManager.uint64_type)
1868 return (object) ((ulong) v);
1870 } else if (c is LongLiteral){
1871 long v = ((LongLiteral) c).Value;
1873 if (target_type == TypeManager.int32_type){
1874 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1875 return (object) ((int) v);
1876 } else if (target_type == TypeManager.uint32_type){
1877 if (v >= 0 && v <= UInt32.MaxValue)
1878 return (object) ((uint) v);
1879 } else if (target_type == TypeManager.char_type){
1880 if (v >= Char.MinValue && v <= Char.MaxValue)
1881 return (object) ((char) v);
1882 } else if (target_type == TypeManager.byte_type){
1883 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1884 return (object) ((byte) v);
1885 } else if (target_type == TypeManager.sbyte_type){
1886 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1887 return (object) ((sbyte) v);
1888 } else if (target_type == TypeManager.short_type){
1889 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1890 return (object) ((short) v);
1891 } else if (target_type == TypeManager.ushort_type){
1892 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1893 return (object) ((ushort) v);
1894 } else if (target_type == TypeManager.uint64_type){
1896 return (object) ((ulong) v);
1899 } else if (c is ULongLiteral){
1900 ulong v = ((ULongLiteral) c).Value;
1902 if (target_type == TypeManager.int32_type){
1903 if (v <= Int32.MaxValue)
1904 return (object) ((int) v);
1905 } else if (target_type == TypeManager.uint32_type){
1906 if (v <= UInt32.MaxValue)
1907 return (object) ((uint) v);
1908 } else if (target_type == TypeManager.char_type){
1909 if (v >= Char.MinValue && v <= Char.MaxValue)
1910 return (object) ((char) v);
1911 } else if (target_type == TypeManager.byte_type){
1912 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1913 return (object) ((byte) v);
1914 } else if (target_type == TypeManager.sbyte_type){
1915 if (v <= (int) SByte.MaxValue)
1916 return (object) ((sbyte) v);
1917 } else if (target_type == TypeManager.short_type){
1918 if (v <= UInt16.MaxValue)
1919 return (object) ((short) v);
1920 } else if (target_type == TypeManager.ushort_type){
1921 if (v <= UInt16.MaxValue)
1922 return (object) ((ushort) v);
1923 } else if (target_type == TypeManager.int64_type){
1924 if (v <= Int64.MaxValue)
1925 return (object) ((long) v);
1928 } else if (c is ByteConstant){
1929 throw new Exception ("Implement me");
1930 } else if (c is SByteConstant){
1931 throw new Exception ("Implement me");
1932 } else if (c is ShortConstant){
1933 throw new Exception ("Implement me");
1934 } else if (c is UShortConstant){
1935 throw new Exception ("Implement me");
1938 error31 (loc, s, target_type);
1945 /// This is just a base class for expressions that can
1946 /// appear on statements (invocations, object creation,
1947 /// assignments, post/pre increment and decrement). The idea
1948 /// being that they would support an extra Emition interface that
1949 /// does not leave a result on the stack.
1951 public abstract class ExpressionStatement : Expression {
1954 /// Requests the expression to be emitted in a `statement'
1955 /// context. This means that no new value is left on the
1956 /// stack after invoking this method (constrasted with
1957 /// Emit that will always leave a value on the stack).
1959 public abstract void EmitStatement (EmitContext ec);
1963 /// This kind of cast is used to encapsulate the child
1964 /// whose type is child.Type into an expression that is
1965 /// reported to return "return_type". This is used to encapsulate
1966 /// expressions which have compatible types, but need to be dealt
1967 /// at higher levels with.
1969 /// For example, a "byte" expression could be encapsulated in one
1970 /// of these as an "unsigned int". The type for the expression
1971 /// would be "unsigned int".
1974 public class EmptyCast : Expression {
1975 protected Expression child;
1977 public EmptyCast (Expression child, Type return_type)
1979 eclass = child.eclass;
1984 public override Expression DoResolve (EmitContext ec)
1986 // This should never be invoked, we are born in fully
1987 // initialized state.
1992 public override void Emit (EmitContext ec)
2000 /// This class is used to wrap literals which belong inside Enums
2002 public class EnumConstant : Constant {
2003 public Constant Child;
2005 public EnumConstant (Constant child, Type enum_type)
2007 eclass = child.eclass;
2012 public override Expression DoResolve (EmitContext ec)
2014 // This should never be invoked, we are born in fully
2015 // initialized state.
2020 public override void Emit (EmitContext ec)
2025 public override object GetValue ()
2027 return Child.GetValue ();
2031 // Converts from one of the valid underlying types for an enumeration
2032 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2033 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2035 public Constant WidenToCompilerConstant ()
2037 Type t = Child.Type.UnderlyingSystemType;
2038 object v = ((Constant) Child).GetValue ();;
2040 if (t == TypeManager.int32_type)
2041 return new IntConstant ((int) v);
2042 if (t == TypeManager.uint32_type)
2043 return new UIntConstant ((uint) v);
2044 if (t == TypeManager.int64_type)
2045 return new LongConstant ((long) v);
2046 if (t == TypeManager.uint64_type)
2047 return new ULongConstant ((ulong) v);
2048 if (t == TypeManager.short_type)
2049 return new ShortConstant ((short) v);
2050 if (t == TypeManager.ushort_type)
2051 return new UShortConstant ((ushort) v);
2052 if (t == TypeManager.byte_type)
2053 return new ByteConstant ((byte) v);
2054 if (t == TypeManager.sbyte_type)
2055 return new SByteConstant ((sbyte) v);
2057 throw new Exception ("Invalid enumeration underlying type: " + t);
2061 // Extracts the value in the enumeration on its native representation
2063 public object GetPlainValue ()
2065 Type t = Child.Type.UnderlyingSystemType;
2066 object v = ((Constant) Child).GetValue ();;
2068 if (t == TypeManager.int32_type)
2070 if (t == TypeManager.uint32_type)
2072 if (t == TypeManager.int64_type)
2074 if (t == TypeManager.uint64_type)
2076 if (t == TypeManager.short_type)
2078 if (t == TypeManager.ushort_type)
2080 if (t == TypeManager.byte_type)
2082 if (t == TypeManager.sbyte_type)
2088 public override string AsString ()
2090 return Child.AsString ();
2095 /// This kind of cast is used to encapsulate Value Types in objects.
2097 /// The effect of it is to box the value type emitted by the previous
2100 public class BoxedCast : EmptyCast {
2102 public BoxedCast (Expression expr)
2103 : base (expr, TypeManager.object_type)
2107 public override Expression DoResolve (EmitContext ec)
2109 // This should never be invoked, we are born in fully
2110 // initialized state.
2115 public override void Emit (EmitContext ec)
2118 ec.ig.Emit (OpCodes.Box, child.Type);
2122 public class UnboxCast : EmptyCast {
2123 public UnboxCast (Expression expr, Type return_type)
2124 : base (expr, return_type)
2128 public override Expression DoResolve (EmitContext ec)
2130 // This should never be invoked, we are born in fully
2131 // initialized state.
2136 public override void Emit (EmitContext ec)
2139 ILGenerator ig = ec.ig;
2142 ig.Emit (OpCodes.Unbox, t);
2145 // Load the object from the pointer
2147 if (t == TypeManager.int32_type)
2148 ig.Emit (OpCodes.Ldind_I4);
2149 else if (t == TypeManager.uint32_type)
2150 ig.Emit (OpCodes.Ldind_U4);
2151 else if (t == TypeManager.short_type)
2152 ig.Emit (OpCodes.Ldind_I2);
2153 else if (t == TypeManager.ushort_type)
2154 ig.Emit (OpCodes.Ldind_U2);
2155 else if (t == TypeManager.char_type)
2156 ig.Emit (OpCodes.Ldind_U2);
2157 else if (t == TypeManager.byte_type)
2158 ig.Emit (OpCodes.Ldind_U1);
2159 else if (t == TypeManager.sbyte_type)
2160 ig.Emit (OpCodes.Ldind_I1);
2161 else if (t == TypeManager.uint64_type)
2162 ig.Emit (OpCodes.Ldind_I8);
2163 else if (t == TypeManager.int64_type)
2164 ig.Emit (OpCodes.Ldind_I8);
2165 else if (t == TypeManager.float_type)
2166 ig.Emit (OpCodes.Ldind_R4);
2167 else if (t == TypeManager.double_type)
2168 ig.Emit (OpCodes.Ldind_R8);
2169 else if (t == TypeManager.bool_type)
2170 ig.Emit (OpCodes.Ldind_I1);
2171 else if (t == TypeManager.intptr_type)
2172 ig.Emit (OpCodes.Ldind_I);
2174 ig.Emit (OpCodes.Ldobj, t);
2179 /// This is used to perform explicit numeric conversions.
2181 /// Explicit numeric conversions might trigger exceptions in a checked
2182 /// context, so they should generate the conv.ovf opcodes instead of
2185 public class ConvCast : EmptyCast {
2186 public enum Mode : byte {
2187 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2189 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2190 U2_I1, U2_U1, U2_I2, U2_CH,
2191 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2192 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2193 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2194 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2195 CH_I1, CH_U1, CH_I2,
2196 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2197 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2202 public ConvCast (Expression child, Type return_type, Mode m)
2203 : base (child, return_type)
2208 public override Expression DoResolve (EmitContext ec)
2210 // This should never be invoked, we are born in fully
2211 // initialized state.
2216 public override void Emit (EmitContext ec)
2218 ILGenerator ig = ec.ig;
2224 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2225 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2226 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2227 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2228 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2230 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2231 case Mode.U1_CH: /* nothing */ break;
2233 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2234 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2235 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2236 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2237 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2238 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2240 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2241 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2242 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2243 case Mode.U2_CH: /* nothing */ break;
2245 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2246 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2247 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2248 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2249 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2250 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2251 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2253 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2254 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2255 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2256 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2257 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2258 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2260 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2261 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2262 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2263 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2264 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2265 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2266 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2267 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2269 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2270 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2271 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2272 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2273 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2274 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2275 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2276 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2278 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2279 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2280 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2282 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2283 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2284 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2285 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2286 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2287 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2288 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2289 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2290 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2292 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2293 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2294 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2295 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2296 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2297 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2298 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2299 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2300 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2301 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2305 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2306 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2307 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2308 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2309 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2311 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2312 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2314 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2315 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2316 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2317 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2318 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2319 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2321 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2322 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2323 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2324 case Mode.U2_CH: /* nothing */ break;
2326 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2327 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2328 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2329 case Mode.I4_U4: /* nothing */ break;
2330 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2331 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2332 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2334 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2335 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2336 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2337 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2338 case Mode.U4_I4: /* nothing */ break;
2339 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2341 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2342 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2343 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2344 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2345 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2346 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2347 case Mode.I8_U8: /* nothing */ break;
2348 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2350 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2351 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2352 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2353 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2354 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2355 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2356 case Mode.U8_I8: /* nothing */ break;
2357 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2359 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2360 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2361 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2363 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2364 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2365 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2366 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2367 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2368 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2369 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2370 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2371 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2373 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2374 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2375 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2376 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2377 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2378 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2379 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2380 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2381 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2382 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2388 public class OpcodeCast : EmptyCast {
2392 public OpcodeCast (Expression child, Type return_type, OpCode op)
2393 : base (child, return_type)
2397 second_valid = false;
2400 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2401 : base (child, return_type)
2406 second_valid = true;
2409 public override Expression DoResolve (EmitContext ec)
2411 // This should never be invoked, we are born in fully
2412 // initialized state.
2417 public override void Emit (EmitContext ec)
2428 /// This kind of cast is used to encapsulate a child and cast it
2429 /// to the class requested
2431 public class ClassCast : EmptyCast {
2432 public ClassCast (Expression child, Type return_type)
2433 : base (child, return_type)
2438 public override Expression DoResolve (EmitContext ec)
2440 // This should never be invoked, we are born in fully
2441 // initialized state.
2446 public override void Emit (EmitContext ec)
2450 ec.ig.Emit (OpCodes.Castclass, type);
2456 /// SimpleName expressions are initially formed of a single
2457 /// word and it only happens at the beginning of the expression.
2461 /// The expression will try to be bound to a Field, a Method
2462 /// group or a Property. If those fail we pass the name to our
2463 /// caller and the SimpleName is compounded to perform a type
2464 /// lookup. The idea behind this process is that we want to avoid
2465 /// creating a namespace map from the assemblies, as that requires
2466 /// the GetExportedTypes function to be called and a hashtable to
2467 /// be constructed which reduces startup time. If later we find
2468 /// that this is slower, we should create a `NamespaceExpr' expression
2469 /// that fully participates in the resolution process.
2471 /// For example `System.Console.WriteLine' is decomposed into
2472 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2474 /// The first SimpleName wont produce a match on its own, so it will
2476 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2478 /// System.Console will produce a TypeExpr match.
2480 /// The downside of this is that we might be hitting `LookupType' too many
2481 /// times with this scheme.
2483 public class SimpleName : Expression {
2484 public readonly string Name;
2485 public readonly Location Location;
2487 public SimpleName (string name, Location l)
2493 public static void Error120 (Location l, string name)
2497 "An object reference is required " +
2498 "for the non-static field `"+name+"'");
2502 // Checks whether we are trying to access an instance
2503 // property, method or field from a static body.
2505 Expression MemberStaticCheck (Expression e)
2507 if (e is FieldExpr){
2508 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2511 Error120 (Location, Name);
2514 } else if (e is MethodGroupExpr){
2515 MethodGroupExpr mg = (MethodGroupExpr) e;
2517 if (!mg.RemoveInstanceMethods ()){
2518 Error120 (Location, mg.Methods [0].Name);
2522 } else if (e is PropertyExpr){
2523 if (!((PropertyExpr) e).IsStatic){
2524 Error120 (Location, Name);
2527 } else if (e is EventExpr) {
2528 if (!((EventExpr) e).IsStatic) {
2529 Error120 (Location, Name);
2538 // 7.5.2: Simple Names.
2540 // Local Variables and Parameters are handled at
2541 // parse time, so they never occur as SimpleNames.
2543 public override Expression DoResolve (EmitContext ec)
2548 // Stage 1: Performed by the parser (binding to locals or parameters).
2552 // Stage 2: Lookup members
2554 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2557 // Stage 3: Lookup symbol in the various namespaces.
2559 DeclSpace ds = ec.TypeContainer;
2563 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2564 return new TypeExpr (t);
2567 // Stage 3 part b: Lookup up if we are an alias to a type
2570 // Since we are cheating: we only do the Alias lookup for
2571 // namespaces if the name does not include any dots in it
2574 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2575 // System.Console.WriteLine (Name + " --> " + alias_value);
2576 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2578 return new TypeExpr (t);
2580 // we have alias value, but it isn't Type, so try if it's namespace
2581 return new SimpleName (alias_value, Location);
2584 // No match, maybe our parent can compose us
2585 // into something meaningful.
2590 // Step 2, continues here.
2594 if (e is FieldExpr){
2595 FieldExpr fe = (FieldExpr) e;
2597 if (!fe.FieldInfo.IsStatic){
2598 This t = new This (Location.Null);
2600 fe.InstanceExpression = t.DoResolve (ec);
2603 FieldInfo fi = fe.FieldInfo;
2605 if (fi is FieldBuilder) {
2606 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2609 object o = c.LookupConstantValue (ec);
2610 object real_value = ((Constant)c.Expr).GetValue ();
2611 return Constantify (real_value, fi.FieldType);
2617 return MemberStaticCheck (e);
2622 public override void Emit (EmitContext ec)
2625 // If this is ever reached, then we failed to
2626 // find the name as a namespace
2629 Error (103, Location, "The name `" + Name +
2630 "' does not exist in the class `" +
2631 ec.TypeContainer.Name + "'");
2636 /// Fully resolved expression that evaluates to a type
2638 public class TypeExpr : Expression {
2639 public TypeExpr (Type t)
2642 eclass = ExprClass.Type;
2645 override public Expression DoResolve (EmitContext ec)
2650 override public void Emit (EmitContext ec)
2652 throw new Exception ("Implement me");
2657 /// MethodGroup Expression.
2659 /// This is a fully resolved expression that evaluates to a type
2661 public class MethodGroupExpr : Expression {
2662 public MethodBase [] Methods;
2663 Expression instance_expression = null;
2665 public MethodGroupExpr (MemberInfo [] mi)
2667 Methods = new MethodBase [mi.Length];
2668 mi.CopyTo (Methods, 0);
2669 eclass = ExprClass.MethodGroup;
2672 public MethodGroupExpr (ArrayList l)
2674 Methods = new MethodBase [l.Count];
2676 l.CopyTo (Methods, 0);
2677 eclass = ExprClass.MethodGroup;
2681 // `A method group may have associated an instance expression'
2683 public Expression InstanceExpression {
2685 return instance_expression;
2689 instance_expression = value;
2693 override public Expression DoResolve (EmitContext ec)
2698 override public void Emit (EmitContext ec)
2700 throw new Exception ("This should never be reached");
2703 bool RemoveMethods (bool keep_static)
2705 ArrayList smethods = new ArrayList ();
2706 int top = Methods.Length;
2709 for (i = 0; i < top; i++){
2710 MethodBase mb = Methods [i];
2712 if (mb.IsStatic == keep_static)
2716 if (smethods.Count == 0)
2719 Methods = new MethodBase [smethods.Count];
2720 smethods.CopyTo (Methods, 0);
2726 /// Removes any instance methods from the MethodGroup, returns
2727 /// false if the resulting set is empty.
2729 public bool RemoveInstanceMethods ()
2731 return RemoveMethods (true);
2735 /// Removes any static methods from the MethodGroup, returns
2736 /// false if the resulting set is empty.
2738 public bool RemoveStaticMethods ()
2740 return RemoveMethods (false);
2745 /// Fully resolved expression that evaluates to a Field
2747 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2748 public readonly FieldInfo FieldInfo;
2749 public Expression InstanceExpression;
2752 public FieldExpr (FieldInfo fi, Location l)
2755 eclass = ExprClass.Variable;
2756 type = fi.FieldType;
2760 override public Expression DoResolve (EmitContext ec)
2762 if (!FieldInfo.IsStatic){
2763 if (InstanceExpression == null){
2764 throw new Exception ("non-static FieldExpr without instance var\n" +
2765 "You have to assign the Instance variable\n" +
2766 "Of the FieldExpr to set this\n");
2769 InstanceExpression = InstanceExpression.Resolve (ec);
2770 if (InstanceExpression == null)
2778 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2780 Expression e = DoResolve (ec);
2785 if (!FieldInfo.IsInitOnly)
2789 // InitOnly fields can only be assigned in constructors
2792 if (ec.IsConstructor)
2795 Report.Error (191, loc,
2796 "Readonly field can not be assigned outside " +
2797 "of constructor or variable initializer");
2802 override public void Emit (EmitContext ec)
2804 ILGenerator ig = ec.ig;
2806 if (FieldInfo.IsStatic)
2807 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2809 InstanceExpression.Emit (ec);
2811 ig.Emit (OpCodes.Ldfld, FieldInfo);
2815 public void EmitAssign (EmitContext ec, Expression source)
2817 bool is_static = FieldInfo.IsStatic;
2820 Expression instance = InstanceExpression;
2822 if (instance.Type.IsValueType){
2823 if (instance is IMemoryLocation){
2824 IMemoryLocation ml = (IMemoryLocation) instance;
2828 throw new Exception ("The " + instance + " of type " + Type+
2829 "represents a ValueType and does not " +
2830 "implement IMemoryLocation");
2837 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2839 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2843 public void AddressOf (EmitContext ec)
2845 if (FieldInfo.IsStatic)
2846 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2848 InstanceExpression.Emit (ec);
2849 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2855 /// Expression that evaluates to a Property. The Assign class
2856 /// might set the `Value' expression if we are in an assignment.
2858 /// This is not an LValue because we need to re-write the expression, we
2859 /// can not take data from the stack and store it.
2861 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2862 public readonly PropertyInfo PropertyInfo;
2863 public readonly bool IsStatic;
2864 MethodInfo [] Accessors;
2867 Expression instance_expr;
2869 public PropertyExpr (PropertyInfo pi, Location l)
2872 eclass = ExprClass.PropertyAccess;
2875 Accessors = TypeManager.GetAccessors (pi);
2877 if (Accessors != null)
2878 for (int i = 0; i < Accessors.Length; i++){
2879 if (Accessors [i] != null)
2880 if (Accessors [i].IsStatic)
2884 Accessors = new MethodInfo [2];
2886 type = pi.PropertyType;
2890 // The instance expression associated with this expression
2892 public Expression InstanceExpression {
2894 instance_expr = value;
2898 return instance_expr;
2902 public bool VerifyAssignable ()
2904 if (!PropertyInfo.CanWrite){
2905 Report.Error (200, loc,
2906 "The property `" + PropertyInfo.Name +
2907 "' can not be assigned to, as it has not set accessor");
2914 override public Expression DoResolve (EmitContext ec)
2916 if (!PropertyInfo.CanRead){
2917 Report.Error (154, loc,
2918 "The property `" + PropertyInfo.Name +
2919 "' can not be used in " +
2920 "this context because it lacks a get accessor");
2924 type = PropertyInfo.PropertyType;
2929 override public void Emit (EmitContext ec)
2931 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2936 // Implements the IAssignMethod interface for assignments
2938 public void EmitAssign (EmitContext ec, Expression source)
2940 Argument arg = new Argument (source, Argument.AType.Expression);
2941 ArrayList args = new ArrayList ();
2944 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2947 override public void EmitStatement (EmitContext ec)
2950 ec.ig.Emit (OpCodes.Pop);
2955 /// Fully resolved expression that evaluates to an Event
2957 public class EventExpr : Expression, IAssignMethod {
2958 public readonly EventInfo EventInfo;
2960 public Expression InstanceExpression;
2962 public readonly bool IsStatic;
2964 MethodInfo add_accessor, remove_accessor;
2966 public EventExpr (EventInfo ei, Location loc)
2970 eclass = ExprClass.EventAccess;
2972 add_accessor = TypeManager.GetAddMethod (ei);
2973 remove_accessor = TypeManager.GetRemoveMethod (ei);
2975 if (add_accessor.IsStatic || remove_accessor.IsStatic)
2978 if (EventInfo is MyEventBuilder)
2979 type = ((MyEventBuilder) EventInfo).EventType;
2981 type = EventInfo.EventHandlerType;
2984 override public Expression DoResolve (EmitContext ec)
2986 // We are born fully resolved
2990 override public void Emit (EmitContext ec)
2992 // FIXME : Implement
2995 public void EmitAssign (EmitContext ec, Expression source)
2997 Expression handler = ((Binary) source).Right;
2999 Argument arg = new Argument (handler, Argument.AType.Expression);
3000 ArrayList args = new ArrayList ();
3004 if (((Binary) source).Oper == Binary.Operator.Addition)
3005 Invocation.EmitCall (ec, IsStatic, InstanceExpression, add_accessor, args);
3007 Invocation.EmitCall (ec, IsStatic, InstanceExpression, remove_accessor, args);