2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 /// This interface is implemented by variables
42 public interface IMemoryLocation {
44 /// The AddressOf method should generate code that loads
45 /// the address of the object and leaves it on the stack
47 void AddressOf (EmitContext ec);
51 /// Base class for expressions
53 public abstract class Expression {
54 protected ExprClass eclass;
67 public ExprClass ExprClass {
78 /// Utility wrapper routine for Error, just to beautify the code
80 static protected void Error (int error, string s)
82 Report.Error (error, s);
85 static protected void Error (int error, Location loc, string s)
87 Report.Error (error, loc, s);
91 /// Utility wrapper routine for Warning, just to beautify the code
93 static protected void Warning (int warning, string s)
95 Report.Warning (warning, s);
98 static public void error30 (Location loc, Type source, Type target)
100 Report.Error (30, loc, "Cannot convert type '" +
101 TypeManager.CSharpName (source) + "' to '" +
102 TypeManager.CSharpName (target) + "'");
106 /// Performs semantic analysis on the Expression
110 /// The Resolve method is invoked to perform the semantic analysis
113 /// The return value is an expression (it can be the
114 /// same expression in some cases) or a new
115 /// expression that better represents this node.
117 /// For example, optimizations of Unary (LiteralInt)
118 /// would return a new LiteralInt with a negated
121 /// If there is an error during semantic analysis,
122 /// then an error should be reported (using Report)
123 /// and a null value should be returned.
125 /// There are two side effects expected from calling
126 /// Resolve(): the the field variable "eclass" should
127 /// be set to any value of the enumeration
128 /// `ExprClass' and the type variable should be set
129 /// to a valid type (this is the type of the
132 public abstract Expression DoResolve (EmitContext ec);
134 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
136 return DoResolve (ec);
140 /// Resolves an expression and performs semantic analysis on it.
144 /// Currently Resolve wraps DoResolve to perform sanity
145 /// checking and assertion checking on what we expect from Resolve.
147 public Expression Resolve (EmitContext ec)
149 Expression e = DoResolve (ec);
152 if (e is SimpleName){
153 SimpleName s = (SimpleName) e;
157 "The name `" + s.Name + "' could not be found in `" +
158 ec.TypeContainer.Name + "'");
162 if (e.ExprClass == ExprClass.Invalid)
163 throw new Exception ("Expression " + e +
164 " ExprClass is Invalid after resolve");
166 if (e.ExprClass != ExprClass.MethodGroup)
168 throw new Exception ("Expression " + e +
169 " did not set its type after Resolve");
176 /// Performs expression resolution and semantic analysis, but
177 /// allows SimpleNames to be returned.
181 /// This is used by MemberAccess to construct long names that can not be
182 /// partially resolved (namespace-qualified names for example).
184 public Expression ResolveWithSimpleName (EmitContext ec)
186 Expression e = DoResolve (ec);
192 if (e.ExprClass == ExprClass.Invalid)
193 throw new Exception ("Expression " + e +
194 " ExprClass is Invalid after resolve");
196 if (e.ExprClass != ExprClass.MethodGroup)
198 throw new Exception ("Expression " + e +
199 " did not set its type after Resolve");
206 /// Resolves an expression for LValue assignment
210 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
211 /// checking and assertion checking on what we expect from Resolve
213 public Expression ResolveLValue (EmitContext ec, Expression right_side)
215 Expression e = DoResolveLValue (ec, right_side);
218 if (e is SimpleName){
219 SimpleName s = (SimpleName) e;
223 "The name `" + s.Name + "' could not be found in `" +
224 ec.TypeContainer.Name + "'");
228 if (e.ExprClass == ExprClass.Invalid)
229 throw new Exception ("Expression " + e +
230 " ExprClass is Invalid after resolve");
232 if (e.ExprClass != ExprClass.MethodGroup)
234 throw new Exception ("Expression " + e +
235 " did not set its type after Resolve");
242 /// Emits the code for the expression
246 /// The Emit method is invoked to generate the code
247 /// for the expression.
249 public abstract void Emit (EmitContext ec);
252 /// This method should perform a reduction of the expression. This should
253 /// never return null.
255 public virtual Expression Reduce (EmitContext ec)
261 /// Protected constructor. Only derivate types should
262 /// be able to be created
265 protected Expression ()
267 eclass = ExprClass.Invalid;
272 /// Returns a literalized version of a literal FieldInfo
274 public static Expression Literalize (object v, Type t)
276 if (t == TypeManager.int32_type)
277 return new IntLiteral ((int) v);
278 else if (t == TypeManager.uint32_type)
279 return new UIntLiteral ((uint) v);
280 else if (t == TypeManager.int64_type)
281 return new LongLiteral ((long) v);
282 else if (t == TypeManager.uint64_type)
283 return new ULongLiteral ((ulong) v);
284 else if (t == TypeManager.float_type)
285 return new FloatLiteral ((float) v);
286 else if (t == TypeManager.double_type)
287 return new DoubleLiteral ((double) v);
288 else if (t == TypeManager.string_type)
289 return new StringLiteral ((string) v);
290 else if (t == TypeManager.short_type)
291 return new IntLiteral ((int) ((short)v));
292 else if (t == TypeManager.ushort_type)
293 return new IntLiteral ((int) ((ushort)v));
294 else if (t == TypeManager.sbyte_type)
295 return new IntLiteral ((int) ((sbyte)v));
296 else if (t == TypeManager.byte_type)
297 return new IntLiteral ((int) ((byte)v));
298 else if (t == TypeManager.char_type)
299 return new IntLiteral ((int) ((char)v));
301 throw new Exception ("Unknown type for literal (" + t +
306 /// Returns a fully formed expression after a MemberLookup
308 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
311 return new EventExpr ((EventInfo) mi, loc);
312 else if (mi is FieldInfo)
313 return new FieldExpr ((FieldInfo) mi, loc);
314 else if (mi is PropertyInfo)
315 return new PropertyExpr ((PropertyInfo) mi, loc);
317 return new TypeExpr ((Type) mi);
323 // FIXME: Probably implement a cache for (t,name,current_access_set)?
325 // FIXME: We need to cope with access permissions here, or this wont
328 // This code could use some optimizations, but we need to do some
329 // measurements. For example, we could use a delegate to `flag' when
330 // something can not any longer be a method-group (because it is something
334 // If the return value is an Array, then it is an array of
337 // If the return value is an MemberInfo, it is anything, but a Method
341 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
342 // the arguments here and have MemberLookup return only the methods that
343 // match the argument count/type, unlike we are doing now (we delay this
346 // This is so we can catch correctly attempts to invoke instance methods
347 // from a static body (scan for error 120 in ResolveSimpleName).
349 public static Expression MemberLookup (EmitContext ec, Type t, string name,
350 bool same_type, MemberTypes mt,
351 BindingFlags bf, Location loc)
354 bf |= BindingFlags.NonPublic;
356 MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
357 t, mt, bf, Type.FilterName, name);
367 if (mi.Length == 1 && !(mi [0] is MethodBase))
368 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
370 for (int i = 0; i < mi.Length; i++)
371 if (!(mi [i] is MethodBase)){
372 Error (-5, "Do not know how to reproduce this case: " +
373 "Methods and non-Method with the same name, " +
374 "report this please");
376 for (i = 0; i < mi.Length; i++){
377 Type tt = mi [i].GetType ();
379 Console.WriteLine (i + ": " + mi [i]);
380 while (tt != TypeManager.object_type){
381 Console.WriteLine (tt);
387 return new MethodGroupExpr (mi);
390 public const MemberTypes AllMemberTypes =
391 MemberTypes.Constructor |
395 MemberTypes.NestedType |
396 MemberTypes.Property;
398 public const BindingFlags AllBindingsFlags =
399 BindingFlags.Public |
400 BindingFlags.Static |
401 BindingFlags.Instance;
403 public static Expression MemberLookup (EmitContext ec, Type t, string name,
404 bool same_type, Location loc)
406 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
409 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
411 Type expr_type = expr.Type;
413 if (target_type == TypeManager.object_type) {
414 if (expr_type.IsClass)
415 return new EmptyCast (expr, target_type);
416 if (expr_type.IsValueType)
417 return new BoxedCast (expr);
418 } else if (expr_type.IsSubclassOf (target_type)) {
419 return new EmptyCast (expr, target_type);
421 // from any class-type S to any interface-type T.
422 if (expr_type.IsClass && target_type.IsInterface) {
424 if (TypeManager.ImplementsInterface (expr_type, target_type))
425 return new EmptyCast (expr, target_type);
430 // from any interface type S to interface-type T.
431 if (expr_type.IsInterface && target_type.IsInterface) {
433 if (TypeManager.ImplementsInterface (expr_type, target_type))
434 return new EmptyCast (expr, target_type);
439 // from an array-type S to an array-type of type T
440 if (expr_type.IsArray && target_type.IsArray) {
441 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
443 Type expr_element_type = expr_type.GetElementType ();
444 Type target_element_type = target_type.GetElementType ();
446 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
447 if (StandardConversionExists (expr_element_type,
448 target_element_type))
449 return new EmptyCast (expr, target_type);
454 // from an array-type to System.Array
455 if (expr_type.IsArray && target_type == TypeManager.array_type)
456 return new EmptyCast (expr, target_type);
458 // from any delegate type to System.Delegate
459 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
460 target_type == TypeManager.delegate_type)
461 return new EmptyCast (expr, target_type);
463 // from any array-type or delegate type into System.ICloneable.
464 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
465 if (target_type == TypeManager.icloneable_type)
466 return new EmptyCast (expr, target_type);
468 // from the null type to any reference-type.
469 if (expr is NullLiteral)
470 return new EmptyCast (expr, target_type);
480 /// Handles expressions like this: decimal d; d = 1;
481 /// and changes them into: decimal d; d = new System.Decimal (1);
483 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
485 ArrayList args = new ArrayList ();
487 args.Add (new Argument (expr, Argument.AType.Expression));
489 Expression ne = new New (target.FullName, args,
492 return ne.Resolve (ec);
496 /// Implicit Numeric Conversions.
498 /// expr is the expression to convert, returns a new expression of type
499 /// target_type or null if an implicit conversion is not possible.
501 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
502 Type target_type, Location loc)
504 Type expr_type = expr.Type;
507 // Attempt to do the implicit constant expression conversions
509 if (expr is IntLiteral){
512 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
516 } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
518 // Try the implicit constant expression conversion
519 // from long to ulong, instead of a nice routine,
522 if (((LongLiteral) expr).Value > 0)
523 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
526 if (expr_type == TypeManager.sbyte_type){
528 // From sbyte to short, int, long, float, double.
530 if (target_type == TypeManager.int32_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
532 if (target_type == TypeManager.int64_type)
533 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
534 if (target_type == TypeManager.double_type)
535 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
536 if (target_type == TypeManager.float_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
538 if (target_type == TypeManager.short_type)
539 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
540 if (target_type == TypeManager.decimal_type)
541 return InternalTypeConstructor (ec, expr, target_type);
542 } else if (expr_type == TypeManager.byte_type){
544 // From byte to short, ushort, int, uint, long, ulong, float, double
546 if ((target_type == TypeManager.short_type) ||
547 (target_type == TypeManager.ushort_type) ||
548 (target_type == TypeManager.int32_type) ||
549 (target_type == TypeManager.uint32_type))
550 return new EmptyCast (expr, target_type);
552 if (target_type == TypeManager.uint64_type)
553 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
554 if (target_type == TypeManager.int64_type)
555 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
557 if (target_type == TypeManager.float_type)
558 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
559 if (target_type == TypeManager.double_type)
560 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
561 if (target_type == TypeManager.decimal_type)
562 return InternalTypeConstructor (ec, expr, target_type);
563 } else if (expr_type == TypeManager.short_type){
565 // From short to int, long, float, double
567 if (target_type == TypeManager.int32_type)
568 return new EmptyCast (expr, target_type);
569 if (target_type == TypeManager.int64_type)
570 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
571 if (target_type == TypeManager.double_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
573 if (target_type == TypeManager.float_type)
574 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
575 if (target_type == TypeManager.decimal_type)
576 return InternalTypeConstructor (ec, expr, target_type);
577 } else if (expr_type == TypeManager.ushort_type){
579 // From ushort to int, uint, long, ulong, float, double
581 if (target_type == TypeManager.uint32_type)
582 return new EmptyCast (expr, target_type);
584 if (target_type == TypeManager.uint64_type)
585 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
586 if (target_type == TypeManager.int32_type)
587 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
588 if (target_type == TypeManager.int64_type)
589 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
590 if (target_type == TypeManager.double_type)
591 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
592 if (target_type == TypeManager.float_type)
593 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
594 if (target_type == TypeManager.decimal_type)
595 return InternalTypeConstructor (ec, expr, target_type);
596 } else if (expr_type == TypeManager.int32_type){
598 // From int to long, float, double
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.decimal_type)
607 return InternalTypeConstructor (ec, expr, target_type);
608 } else if (expr_type == TypeManager.uint32_type){
610 // From uint to long, ulong, float, double
612 if (target_type == TypeManager.int64_type)
613 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
614 if (target_type == TypeManager.uint64_type)
615 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
616 if (target_type == TypeManager.double_type)
617 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
619 if (target_type == TypeManager.float_type)
620 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
622 if (target_type == TypeManager.decimal_type)
623 return InternalTypeConstructor (ec, expr, target_type);
624 } else if ((expr_type == TypeManager.uint64_type) ||
625 (expr_type == TypeManager.int64_type)){
627 // From long/ulong to float, double
629 if (target_type == TypeManager.double_type)
630 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
632 if (target_type == TypeManager.float_type)
633 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
635 if (target_type == TypeManager.decimal_type)
636 return InternalTypeConstructor (ec, expr, target_type);
637 } else if (expr_type == TypeManager.char_type){
639 // From char to ushort, int, uint, long, ulong, float, double
641 if ((target_type == TypeManager.ushort_type) ||
642 (target_type == TypeManager.int32_type) ||
643 (target_type == TypeManager.uint32_type))
644 return new EmptyCast (expr, target_type);
645 if (target_type == TypeManager.uint64_type)
646 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
647 if (target_type == TypeManager.int64_type)
648 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
649 if (target_type == TypeManager.float_type)
650 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
651 if (target_type == TypeManager.double_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
653 if (target_type == TypeManager.decimal_type)
654 return InternalTypeConstructor (ec, expr, target_type);
655 } else if (expr_type == TypeManager.float_type){
659 if (target_type == TypeManager.double_type)
660 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
667 /// Determines if a standard implicit conversion exists from
668 /// expr_type to target_type
670 public static bool StandardConversionExists (Type expr_type, Type target_type)
672 if (expr_type == target_type)
675 // First numeric conversions
677 if (expr_type == TypeManager.sbyte_type){
679 // From sbyte to short, int, long, float, double.
681 if ((target_type == TypeManager.int32_type) ||
682 (target_type == TypeManager.int64_type) ||
683 (target_type == TypeManager.double_type) ||
684 (target_type == TypeManager.float_type) ||
685 (target_type == TypeManager.short_type) ||
686 (target_type == TypeManager.decimal_type))
689 } else if (expr_type == TypeManager.byte_type){
691 // From byte to short, ushort, int, uint, long, ulong, float, double
693 if ((target_type == TypeManager.short_type) ||
694 (target_type == TypeManager.ushort_type) ||
695 (target_type == TypeManager.int32_type) ||
696 (target_type == TypeManager.uint32_type) ||
697 (target_type == TypeManager.uint64_type) ||
698 (target_type == TypeManager.int64_type) ||
699 (target_type == TypeManager.float_type) ||
700 (target_type == TypeManager.double_type) ||
701 (target_type == TypeManager.decimal_type))
704 } else if (expr_type == TypeManager.short_type){
706 // From short to int, long, float, double
708 if ((target_type == TypeManager.int32_type) ||
709 (target_type == TypeManager.int64_type) ||
710 (target_type == TypeManager.double_type) ||
711 (target_type == TypeManager.float_type) ||
712 (target_type == TypeManager.decimal_type))
715 } else if (expr_type == TypeManager.ushort_type){
717 // From ushort to int, uint, long, ulong, float, double
719 if ((target_type == TypeManager.uint32_type) ||
720 (target_type == TypeManager.uint64_type) ||
721 (target_type == TypeManager.int32_type) ||
722 (target_type == TypeManager.int64_type) ||
723 (target_type == TypeManager.double_type) ||
724 (target_type == TypeManager.float_type) ||
725 (target_type == TypeManager.decimal_type))
728 } else if (expr_type == TypeManager.int32_type){
730 // From int to long, float, double
732 if ((target_type == TypeManager.int64_type) ||
733 (target_type == TypeManager.double_type) ||
734 (target_type == TypeManager.float_type) ||
735 (target_type == TypeManager.decimal_type))
738 } else if (expr_type == TypeManager.uint32_type){
740 // From uint to long, ulong, float, double
742 if ((target_type == TypeManager.int64_type) ||
743 (target_type == TypeManager.uint64_type) ||
744 (target_type == TypeManager.double_type) ||
745 (target_type == TypeManager.float_type) ||
746 (target_type == TypeManager.decimal_type))
749 } else if ((expr_type == TypeManager.uint64_type) ||
750 (expr_type == TypeManager.int64_type)) {
752 // From long/ulong to float, double
754 if ((target_type == TypeManager.double_type) ||
755 (target_type == TypeManager.float_type) ||
756 (target_type == TypeManager.decimal_type))
759 } else if (expr_type == TypeManager.char_type){
761 // From char to ushort, int, uint, long, ulong, float, double
763 if ((target_type == TypeManager.ushort_type) ||
764 (target_type == TypeManager.int32_type) ||
765 (target_type == TypeManager.uint32_type) ||
766 (target_type == TypeManager.uint64_type) ||
767 (target_type == TypeManager.int64_type) ||
768 (target_type == TypeManager.float_type) ||
769 (target_type == TypeManager.double_type) ||
770 (target_type == TypeManager.decimal_type))
773 } else if (expr_type == TypeManager.float_type){
777 if (target_type == TypeManager.double_type)
781 // Next reference conversions
783 if (target_type == TypeManager.object_type) {
784 if ((expr_type.IsClass) ||
785 (expr_type.IsValueType))
788 } else if (expr_type.IsSubclassOf (target_type)) {
792 // from any class-type S to any interface-type T.
793 if (expr_type.IsClass && target_type.IsInterface)
796 // from any interface type S to interface-type T.
797 // FIXME : Is it right to use IsAssignableFrom ?
798 if (expr_type.IsInterface && target_type.IsInterface)
799 if (target_type.IsAssignableFrom (expr_type))
802 // from an array-type S to an array-type of type T
803 if (expr_type.IsArray && target_type.IsArray) {
804 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
806 Type expr_element_type = expr_type.GetElementType ();
807 Type target_element_type = target_type.GetElementType ();
809 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
810 if (StandardConversionExists (expr_element_type,
811 target_element_type))
816 // from an array-type to System.Array
817 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
820 // from any delegate type to System.Delegate
821 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
822 target_type == TypeManager.delegate_type)
823 if (target_type.IsAssignableFrom (expr_type))
826 // from any array-type or delegate type into System.ICloneable.
827 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
828 if (target_type == TypeManager.icloneable_type)
831 // from the null type to any reference-type.
832 // FIXME : How do we do this ?
840 /// Finds "most encompassed type" according to the spec (13.4.2)
841 /// amongst the methods in the MethodGroupExpr which convert from a
842 /// type encompassing source_type
844 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
848 for (int i = me.Methods.Length; i > 0; ) {
851 MethodBase mb = me.Methods [i];
852 ParameterData pd = Invocation.GetParameterData (mb);
853 Type param_type = pd.ParameterType (0);
855 if (StandardConversionExists (source_type, param_type)) {
859 if (StandardConversionExists (param_type, best))
868 /// Finds "most encompassing type" according to the spec (13.4.2)
869 /// amongst the methods in the MethodGroupExpr which convert to a
870 /// type encompassed by target_type
872 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
876 for (int i = me.Methods.Length; i > 0; ) {
879 MethodInfo mi = (MethodInfo) me.Methods [i];
880 Type ret_type = mi.ReturnType;
882 if (StandardConversionExists (ret_type, target)) {
886 if (!StandardConversionExists (ret_type, best))
898 /// User-defined Implicit conversions
900 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
901 Type target, Location loc)
903 return UserDefinedConversion (ec, source, target, loc, false);
907 /// User-defined Explicit conversions
909 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
910 Type target, Location loc)
912 return UserDefinedConversion (ec, source, target, loc, true);
916 /// User-defined conversions
918 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
919 Type target, Location loc,
920 bool look_for_explicit)
922 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
923 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
925 MethodBase method = null;
926 Type source_type = source.Type;
930 // If we have a boolean type, we need to check for the True operator
932 // FIXME : How does the False operator come into the picture ?
933 // FIXME : This doesn't look complete and very correct !
934 if (target == TypeManager.bool_type)
937 op_name = "op_Implicit";
939 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
941 if (source_type.BaseType != null)
942 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
944 mg3 = MemberLookup (ec, target, op_name, false, loc);
946 if (target.BaseType != null)
947 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
949 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
950 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
952 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
954 MethodGroupExpr union4 = null;
956 if (look_for_explicit) {
958 op_name = "op_Explicit";
960 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
962 if (source_type.BaseType != null)
963 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
965 mg7 = MemberLookup (ec, target, op_name, false, loc);
967 if (target.BaseType != null)
968 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
970 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
971 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
973 union4 = Invocation.MakeUnionSet (union5, union6);
976 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
980 Type most_specific_source, most_specific_target;
982 most_specific_source = FindMostEncompassedType (union, source_type);
983 if (most_specific_source == null)
986 most_specific_target = FindMostEncompassingType (union, target);
987 if (most_specific_target == null)
992 for (int i = union.Methods.Length; i > 0;) {
995 MethodBase mb = union.Methods [i];
996 ParameterData pd = Invocation.GetParameterData (mb);
997 MethodInfo mi = (MethodInfo) union.Methods [i];
999 if (pd.ParameterType (0) == most_specific_source &&
1000 mi.ReturnType == most_specific_target) {
1006 if (method == null || count > 1) {
1007 Report.Error (-11, loc, "Ambiguous user defined conversion");
1012 // This will do the conversion to the best match that we
1013 // found. Now we need to perform an implict standard conversion
1014 // if the best match was not the type that we were requested
1017 if (look_for_explicit)
1018 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1020 source = ConvertImplicitStandard (ec, source,
1021 most_specific_source, loc);
1026 e = new UserCast ((MethodInfo) method, source);
1028 if (e.Type != target){
1029 if (!look_for_explicit)
1030 e = ConvertImplicitStandard (ec, e, target, loc);
1032 e = ConvertExplicitStandard (ec, e, target, loc);
1043 /// Converts implicitly the resolved expression `expr' into the
1044 /// `target_type'. It returns a new expression that can be used
1045 /// in a context that expects a `target_type'.
1047 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1048 Type target_type, Location loc)
1050 Type expr_type = expr.Type;
1053 if (expr_type == target_type)
1056 if (target_type == null)
1057 throw new Exception ("Target type is null");
1059 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1063 e = ImplicitReferenceConversion (expr, target_type);
1067 e = ImplicitUserConversion (ec, expr, target_type, loc);
1071 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1072 IntLiteral i = (IntLiteral) expr;
1075 return new EmptyCast (expr, target_type);
1083 /// Attempts to apply the `Standard Implicit
1084 /// Conversion' rules to the expression `expr' into
1085 /// the `target_type'. It returns a new expression
1086 /// that can be used in a context that expects a
1089 /// This is different from `ConvertImplicit' in that the
1090 /// user defined implicit conversions are excluded.
1092 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1093 Type target_type, Location loc)
1095 Type expr_type = expr.Type;
1098 if (expr_type == target_type)
1101 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1105 e = ImplicitReferenceConversion (expr, target_type);
1109 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1110 IntLiteral i = (IntLiteral) expr;
1113 return new EmptyCast (expr, target_type);
1119 /// Attemps to perform an implict constant conversion of the IntLiteral
1120 /// into a different data type using casts (See Implicit Constant
1121 /// Expression Conversions)
1123 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1125 int value = il.Value;
1127 if (target_type == TypeManager.sbyte_type){
1128 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1129 return new EmptyCast (il, target_type);
1130 } else if (target_type == TypeManager.byte_type){
1131 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1132 return new EmptyCast (il, target_type);
1133 } else if (target_type == TypeManager.short_type){
1134 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1135 return new EmptyCast (il, target_type);
1136 } else if (target_type == TypeManager.ushort_type){
1137 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1138 return new EmptyCast (il, target_type);
1139 } else if (target_type == TypeManager.uint32_type){
1141 // we can optimize this case: a positive int32
1142 // always fits on a uint32
1145 return new EmptyCast (il, target_type);
1146 } else if (target_type == TypeManager.uint64_type){
1148 // we can optimize this case: a positive int32
1149 // always fits on a uint64. But we need an opcode
1153 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1160 /// Attemptes to implicityly convert `target' into `type', using
1161 /// ConvertImplicit. If there is no implicit conversion, then
1162 /// an error is signaled
1164 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1165 Type target_type, Location loc)
1169 e = ConvertImplicit (ec, source, target_type, loc);
1173 string msg = "Cannot convert implicitly from `"+
1174 TypeManager.CSharpName (source.Type) + "' to `" +
1175 TypeManager.CSharpName (target_type) + "'";
1177 Error (29, loc, msg);
1183 /// Performs the explicit numeric conversions
1185 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1188 Type expr_type = expr.Type;
1190 if (expr_type == TypeManager.sbyte_type){
1192 // From sbyte to byte, ushort, uint, ulong, char
1194 if (target_type == TypeManager.byte_type)
1195 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1196 if (target_type == TypeManager.ushort_type)
1197 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1198 if (target_type == TypeManager.uint32_type)
1199 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1200 if (target_type == TypeManager.uint64_type)
1201 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1202 if (target_type == TypeManager.char_type)
1203 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1204 } else if (expr_type == TypeManager.byte_type){
1206 // From byte to sbyte and char
1208 if (target_type == TypeManager.sbyte_type)
1209 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1210 if (target_type == TypeManager.char_type)
1211 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1212 } else if (expr_type == TypeManager.short_type){
1214 // From short to sbyte, byte, ushort, uint, ulong, char
1216 if (target_type == TypeManager.sbyte_type)
1217 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1218 if (target_type == TypeManager.byte_type)
1219 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1220 if (target_type == TypeManager.ushort_type)
1221 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1222 if (target_type == TypeManager.uint32_type)
1223 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1224 if (target_type == TypeManager.uint64_type)
1225 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1226 if (target_type == TypeManager.char_type)
1227 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1228 } else if (expr_type == TypeManager.ushort_type){
1230 // From ushort to sbyte, byte, short, char
1232 if (target_type == TypeManager.sbyte_type)
1233 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1234 if (target_type == TypeManager.byte_type)
1235 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1236 if (target_type == TypeManager.short_type)
1237 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1238 if (target_type == TypeManager.char_type)
1239 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1240 } else if (expr_type == TypeManager.int32_type){
1242 // From int to sbyte, byte, short, ushort, uint, ulong, char
1244 if (target_type == TypeManager.sbyte_type)
1245 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1246 if (target_type == TypeManager.byte_type)
1247 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1248 if (target_type == TypeManager.short_type)
1249 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1250 if (target_type == TypeManager.ushort_type)
1251 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1252 if (target_type == TypeManager.uint32_type)
1253 return new EmptyCast (expr, target_type);
1254 if (target_type == TypeManager.uint64_type)
1255 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1256 if (target_type == TypeManager.char_type)
1257 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1258 } else if (expr_type == TypeManager.uint32_type){
1260 // From uint to sbyte, byte, short, ushort, int, char
1262 if (target_type == TypeManager.sbyte_type)
1263 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1264 if (target_type == TypeManager.byte_type)
1265 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1266 if (target_type == TypeManager.short_type)
1267 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1268 if (target_type == TypeManager.ushort_type)
1269 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1270 if (target_type == TypeManager.int32_type)
1271 return new EmptyCast (expr, target_type);
1272 if (target_type == TypeManager.char_type)
1273 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1274 } else if (expr_type == TypeManager.int64_type){
1276 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1278 if (target_type == TypeManager.sbyte_type)
1279 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1280 if (target_type == TypeManager.byte_type)
1281 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1282 if (target_type == TypeManager.short_type)
1283 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1284 if (target_type == TypeManager.ushort_type)
1285 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1286 if (target_type == TypeManager.int32_type)
1287 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1288 if (target_type == TypeManager.uint32_type)
1289 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1290 if (target_type == TypeManager.uint64_type)
1291 return new EmptyCast (expr, target_type);
1292 if (target_type == TypeManager.char_type)
1293 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1294 } else if (expr_type == TypeManager.uint64_type){
1296 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1298 if (target_type == TypeManager.sbyte_type)
1299 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1300 if (target_type == TypeManager.byte_type)
1301 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1302 if (target_type == TypeManager.short_type)
1303 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1304 if (target_type == TypeManager.ushort_type)
1305 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1306 if (target_type == TypeManager.int32_type)
1307 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1308 if (target_type == TypeManager.uint32_type)
1309 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1310 if (target_type == TypeManager.int64_type)
1311 return new EmptyCast (expr, target_type);
1312 if (target_type == TypeManager.char_type)
1313 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1314 } else if (expr_type == TypeManager.char_type){
1316 // From char to sbyte, byte, short
1318 if (target_type == TypeManager.sbyte_type)
1319 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1320 if (target_type == TypeManager.byte_type)
1321 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1322 if (target_type == TypeManager.short_type)
1323 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1324 } else if (expr_type == TypeManager.float_type){
1326 // From float to sbyte, byte, short,
1327 // ushort, int, uint, long, ulong, char
1330 if (target_type == TypeManager.sbyte_type)
1331 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1332 if (target_type == TypeManager.byte_type)
1333 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1334 if (target_type == TypeManager.short_type)
1335 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1336 if (target_type == TypeManager.ushort_type)
1337 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1338 if (target_type == TypeManager.int32_type)
1339 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1340 if (target_type == TypeManager.uint32_type)
1341 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1342 if (target_type == TypeManager.int64_type)
1343 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1344 if (target_type == TypeManager.uint64_type)
1345 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1346 if (target_type == TypeManager.char_type)
1347 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1348 if (target_type == TypeManager.decimal_type)
1349 return InternalTypeConstructor (ec, expr, target_type);
1350 } else if (expr_type == TypeManager.double_type){
1352 // From double to byte, byte, short,
1353 // ushort, int, uint, long, ulong,
1354 // char, float or decimal
1356 if (target_type == TypeManager.sbyte_type)
1357 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1358 if (target_type == TypeManager.byte_type)
1359 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1360 if (target_type == TypeManager.short_type)
1361 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1362 if (target_type == TypeManager.ushort_type)
1363 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1364 if (target_type == TypeManager.int32_type)
1365 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1366 if (target_type == TypeManager.uint32_type)
1367 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1368 if (target_type == TypeManager.int64_type)
1369 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1370 if (target_type == TypeManager.uint64_type)
1371 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1372 if (target_type == TypeManager.char_type)
1373 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1374 if (target_type == TypeManager.float_type)
1375 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1376 if (target_type == TypeManager.decimal_type)
1377 return InternalTypeConstructor (ec, expr, target_type);
1380 // decimal is taken care of by the op_Explicit methods.
1386 /// Returns whether an explicit reference conversion can be performed
1387 /// from source_type to target_type
1389 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1391 bool target_is_value_type = target_type.IsValueType;
1393 if (source_type == target_type)
1397 // From object to any reference type
1399 if (source_type == TypeManager.object_type && !target_is_value_type)
1403 // From any class S to any class-type T, provided S is a base class of T
1405 if (target_type.IsSubclassOf (source_type))
1409 // From any interface type S to any interface T provided S is not derived from T
1411 if (source_type.IsInterface && target_type.IsInterface){
1412 if (!target_type.IsSubclassOf (source_type))
1417 // From any class type S to any interface T, provides S is not sealed
1418 // and provided S does not implement T.
1420 if (target_type.IsInterface && !source_type.IsSealed &&
1421 !target_type.IsAssignableFrom (source_type))
1425 // From any interface-type S to to any class type T, provided T is not
1426 // sealed, or provided T implements S.
1428 if (source_type.IsInterface &&
1429 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1432 // From an array type S with an element type Se to an array type T with an
1433 // element type Te provided all the following are true:
1434 // * S and T differe only in element type, in other words, S and T
1435 // have the same number of dimensions.
1436 // * Both Se and Te are reference types
1437 // * An explicit referenc conversions exist from Se to Te
1439 if (source_type.IsArray && target_type.IsArray) {
1440 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1442 Type source_element_type = source_type.GetElementType ();
1443 Type target_element_type = target_type.GetElementType ();
1445 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1446 if (ExplicitReferenceConversionExists (source_element_type,
1447 target_element_type))
1453 // From System.Array to any array-type
1454 if (source_type == TypeManager.array_type &&
1455 target_type.IsSubclassOf (TypeManager.array_type)){
1460 // From System delegate to any delegate-type
1462 if (source_type == TypeManager.delegate_type &&
1463 target_type.IsSubclassOf (TypeManager.delegate_type))
1467 // From ICloneable to Array or Delegate types
1469 if (source_type == TypeManager.icloneable_type &&
1470 (target_type == TypeManager.array_type ||
1471 target_type == TypeManager.delegate_type))
1478 /// Implements Explicit Reference conversions
1480 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1482 Type source_type = source.Type;
1483 bool target_is_value_type = target_type.IsValueType;
1486 // From object to any reference type
1488 if (source_type == TypeManager.object_type && !target_is_value_type)
1489 return new ClassCast (source, target_type);
1493 // From any class S to any class-type T, provided S is a base class of T
1495 if (target_type.IsSubclassOf (source_type))
1496 return new ClassCast (source, target_type);
1499 // From any interface type S to any interface T provided S is not derived from T
1501 if (source_type.IsInterface && target_type.IsInterface){
1503 Type [] ifaces = source_type.GetInterfaces ();
1505 if (TypeManager.ImplementsInterface (source_type, target_type))
1508 return new ClassCast (source, target_type);
1512 // From any class type S to any interface T, provides S is not sealed
1513 // and provided S does not implement T.
1515 if (target_type.IsInterface && !source_type.IsSealed) {
1517 if (TypeManager.ImplementsInterface (source_type, target_type))
1520 return new ClassCast (source, target_type);
1525 // From any interface-type S to to any class type T, provided T is not
1526 // sealed, or provided T implements S.
1528 if (source_type.IsInterface) {
1530 if (target_type.IsSealed)
1533 if (TypeManager.ImplementsInterface (target_type, source_type))
1534 return new ClassCast (source, target_type);
1539 // From an array type S with an element type Se to an array type T with an
1540 // element type Te provided all the following are true:
1541 // * S and T differe only in element type, in other words, S and T
1542 // have the same number of dimensions.
1543 // * Both Se and Te are reference types
1544 // * An explicit referenc conversions exist from Se to Te
1546 if (source_type.IsArray && target_type.IsArray) {
1547 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1549 Type source_element_type = source_type.GetElementType ();
1550 Type target_element_type = target_type.GetElementType ();
1552 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1553 if (ExplicitReferenceConversionExists (source_element_type,
1554 target_element_type))
1555 return new ClassCast (source, target_type);
1560 // From System.Array to any array-type
1561 if (source_type == TypeManager.array_type &&
1562 target_type.IsSubclassOf (TypeManager.array_type)){
1563 return new ClassCast (source, target_type);
1567 // From System delegate to any delegate-type
1569 if (source_type == TypeManager.delegate_type &&
1570 target_type.IsSubclassOf (TypeManager.delegate_type))
1571 return new ClassCast (source, target_type);
1574 // From ICloneable to Array or Delegate types
1576 if (source_type == TypeManager.icloneable_type &&
1577 (target_type == TypeManager.array_type ||
1578 target_type == TypeManager.delegate_type))
1579 return new ClassCast (source, target_type);
1585 /// Performs an explicit conversion of the expression `expr' whose
1586 /// type is expr.Type to `target_type'.
1588 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1589 Type target_type, Location loc)
1591 Type expr_type = expr.Type;
1592 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1597 ne = ConvertNumericExplicit (ec, expr, target_type);
1602 // Unboxing conversion.
1604 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1605 return new UnboxCast (expr, target_type);
1610 if (expr is EnumLiteral) {
1611 Expression e = ((EnumLiteral) expr).Child;
1613 return ConvertImplicit (ec, e, target_type, loc);
1616 ne = ConvertReferenceExplicit (expr, target_type);
1620 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1624 error30 (loc, expr_type, target_type);
1629 /// Same as ConverExplicit, only it doesn't include user defined conversions
1631 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1632 Type target_type, Location l)
1634 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1639 ne = ConvertNumericExplicit (ec, expr, target_type);
1643 ne = ConvertReferenceExplicit (expr, target_type);
1647 error30 (l, expr.Type, target_type);
1651 static string ExprClassName (ExprClass c)
1654 case ExprClass.Invalid:
1656 case ExprClass.Value:
1658 case ExprClass.Variable:
1660 case ExprClass.Namespace:
1662 case ExprClass.Type:
1664 case ExprClass.MethodGroup:
1665 return "method group";
1666 case ExprClass.PropertyAccess:
1667 return "property access";
1668 case ExprClass.EventAccess:
1669 return "event access";
1670 case ExprClass.IndexerAccess:
1671 return "indexer access";
1672 case ExprClass.Nothing:
1675 throw new Exception ("Should not happen");
1679 /// Reports that we were expecting `expr' to be of class `expected'
1681 protected void report118 (Location loc, Expression expr, string expected)
1683 string kind = "Unknown";
1686 kind = ExprClassName (expr.ExprClass);
1688 Error (118, loc, "Expression denotes a '" + kind +
1689 "' where an " + expected + " was expected");
1693 /// This function tries to reduce the expression performing
1694 /// constant folding and common subexpression elimination
1696 static public Expression Reduce (EmitContext ec, Expression e)
1698 //Console.WriteLine ("Calling reduce");
1699 return e.Reduce (ec);
1704 /// This is just a base class for expressions that can
1705 /// appear on statements (invocations, object creation,
1706 /// assignments, post/pre increment and decrement). The idea
1707 /// being that they would support an extra Emition interface that
1708 /// does not leave a result on the stack.
1710 public abstract class ExpressionStatement : Expression {
1713 /// Requests the expression to be emitted in a `statement'
1714 /// context. This means that no new value is left on the
1715 /// stack after invoking this method (constrasted with
1716 /// Emit that will always leave a value on the stack).
1718 public abstract void EmitStatement (EmitContext ec);
1722 /// This kind of cast is used to encapsulate the child
1723 /// whose type is child.Type into an expression that is
1724 /// reported to return "return_type". This is used to encapsulate
1725 /// expressions which have compatible types, but need to be dealt
1726 /// at higher levels with.
1728 /// For example, a "byte" expression could be encapsulated in one
1729 /// of these as an "unsigned int". The type for the expression
1730 /// would be "unsigned int".
1733 public class EmptyCast : Expression {
1734 protected Expression child;
1736 public EmptyCast (Expression child, Type return_type)
1738 ExprClass = child.ExprClass;
1743 public override Expression DoResolve (EmitContext ec)
1745 // This should never be invoked, we are born in fully
1746 // initialized state.
1751 public override void Emit (EmitContext ec)
1759 /// This class is used to wrap literals which belong inside Enums
1761 public class EnumLiteral : Literal {
1762 public Expression Child;
1764 public EnumLiteral (Expression child, Type enum_type)
1766 ExprClass = child.ExprClass;
1771 public override Expression DoResolve (EmitContext ec)
1773 // This should never be invoked, we are born in fully
1774 // initialized state.
1779 public override void Emit (EmitContext ec)
1784 public override object GetValue ()
1786 return ((Literal) Child).GetValue ();
1789 public override string AsString ()
1791 return ((Literal) Child).AsString ();
1796 /// This kind of cast is used to encapsulate Value Types in objects.
1798 /// The effect of it is to box the value type emitted by the previous
1801 public class BoxedCast : EmptyCast {
1803 public BoxedCast (Expression expr)
1804 : base (expr, TypeManager.object_type)
1808 public override Expression DoResolve (EmitContext ec)
1810 // This should never be invoked, we are born in fully
1811 // initialized state.
1816 public override void Emit (EmitContext ec)
1819 ec.ig.Emit (OpCodes.Box, child.Type);
1823 public class UnboxCast : EmptyCast {
1824 public UnboxCast (Expression expr, Type return_type)
1825 : base (expr, return_type)
1829 public override Expression DoResolve (EmitContext ec)
1831 // This should never be invoked, we are born in fully
1832 // initialized state.
1837 public override void Emit (EmitContext ec)
1840 ILGenerator ig = ec.ig;
1843 ig.Emit (OpCodes.Unbox, t);
1846 // Load the object from the pointer
1848 if (t == TypeManager.int32_type)
1849 ig.Emit (OpCodes.Ldind_I4);
1850 else if (t == TypeManager.uint32_type)
1851 ig.Emit (OpCodes.Ldind_U4);
1852 else if (t == TypeManager.short_type)
1853 ig.Emit (OpCodes.Ldind_I2);
1854 else if (t == TypeManager.ushort_type)
1855 ig.Emit (OpCodes.Ldind_U2);
1856 else if (t == TypeManager.char_type)
1857 ig.Emit (OpCodes.Ldind_U2);
1858 else if (t == TypeManager.byte_type)
1859 ig.Emit (OpCodes.Ldind_U1);
1860 else if (t == TypeManager.sbyte_type)
1861 ig.Emit (OpCodes.Ldind_I1);
1862 else if (t == TypeManager.uint64_type)
1863 ig.Emit (OpCodes.Ldind_I8);
1864 else if (t == TypeManager.int64_type)
1865 ig.Emit (OpCodes.Ldind_I8);
1866 else if (t == TypeManager.float_type)
1867 ig.Emit (OpCodes.Ldind_R4);
1868 else if (t == TypeManager.double_type)
1869 ig.Emit (OpCodes.Ldind_R8);
1870 else if (t == TypeManager.bool_type)
1871 ig.Emit (OpCodes.Ldind_I1);
1872 else if (t == TypeManager.intptr_type)
1873 ig.Emit (OpCodes.Ldind_I);
1875 ig.Emit (OpCodes.Ldobj, t);
1880 /// This kind of cast is used to encapsulate a child expression
1881 /// that can be trivially converted to a target type using one or
1882 /// two opcodes. The opcodes are passed as arguments.
1884 public class OpcodeCast : EmptyCast {
1888 public OpcodeCast (Expression child, Type return_type, OpCode op)
1889 : base (child, return_type)
1893 second_valid = false;
1896 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1897 : base (child, return_type)
1902 second_valid = true;
1905 public override Expression DoResolve (EmitContext ec)
1907 // This should never be invoked, we are born in fully
1908 // initialized state.
1913 public override void Emit (EmitContext ec)
1925 /// This kind of cast is used to encapsulate a child and cast it
1926 /// to the class requested
1928 public class ClassCast : EmptyCast {
1929 public ClassCast (Expression child, Type return_type)
1930 : base (child, return_type)
1935 public override Expression DoResolve (EmitContext ec)
1937 // This should never be invoked, we are born in fully
1938 // initialized state.
1943 public override void Emit (EmitContext ec)
1947 ec.ig.Emit (OpCodes.Castclass, type);
1953 /// SimpleName expressions are initially formed of a single
1954 /// word and it only happens at the beginning of the expression.
1958 /// The expression will try to be bound to a Field, a Method
1959 /// group or a Property. If those fail we pass the name to our
1960 /// caller and the SimpleName is compounded to perform a type
1961 /// lookup. The idea behind this process is that we want to avoid
1962 /// creating a namespace map from the assemblies, as that requires
1963 /// the GetExportedTypes function to be called and a hashtable to
1964 /// be constructed which reduces startup time. If later we find
1965 /// that this is slower, we should create a `NamespaceExpr' expression
1966 /// that fully participates in the resolution process.
1968 /// For example `System.Console.WriteLine' is decomposed into
1969 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1971 /// The first SimpleName wont produce a match on its own, so it will
1973 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
1975 /// System.Console will produce a TypeExpr match.
1977 /// The downside of this is that we might be hitting `LookupType' too many
1978 /// times with this scheme.
1980 public class SimpleName : Expression {
1981 public readonly string Name;
1982 public readonly Location Location;
1984 public SimpleName (string name, Location l)
1990 public static void Error120 (Location l, string name)
1994 "An object reference is required " +
1995 "for the non-static field `"+name+"'");
1999 // Checks whether we are trying to access an instance
2000 // property, method or field from a static body.
2002 Expression MemberStaticCheck (Expression e)
2004 if (e is FieldExpr){
2005 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2008 Error120 (Location, Name);
2011 } else if (e is MethodGroupExpr){
2012 MethodGroupExpr mg = (MethodGroupExpr) e;
2014 if (!mg.RemoveInstanceMethods ()){
2015 Error120 (Location, mg.Methods [0].Name);
2019 } else if (e is PropertyExpr){
2020 if (!((PropertyExpr) e).IsStatic){
2021 Error120 (Location, Name);
2030 // 7.5.2: Simple Names.
2032 // Local Variables and Parameters are handled at
2033 // parse time, so they never occur as SimpleNames.
2035 public override Expression DoResolve (EmitContext ec)
2040 // Stage 1: Performed by the parser (binding to local or parameters).
2044 // Stage 2: Lookup members
2046 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2049 // Stage 3: Lookup symbol in the various namespaces.
2053 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
2054 return new TypeExpr (t);
2057 // Stage 3 part b: Lookup up if we are an alias to a type
2060 // Since we are cheating: we only do the Alias lookup for
2061 // namespaces if the name does not include any dots in it
2064 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
2065 // using NamespaceExprs (dunno how that fixes the alias
2066 // per-file though).
2068 // No match, maybe our parent can compose us
2069 // into something meaningful.
2074 // Step 2, continues here.
2078 if (e is FieldExpr){
2079 FieldExpr fe = (FieldExpr) e;
2081 if (!fe.FieldInfo.IsStatic)
2082 fe.InstanceExpression = new This (Location.Null);
2086 return MemberStaticCheck (e);
2091 public override void Emit (EmitContext ec)
2094 // If this is ever reached, then we failed to
2095 // find the name as a namespace
2098 Error (103, Location, "The name `" + Name +
2099 "' does not exist in the class `" +
2100 ec.TypeContainer.Name + "'");
2105 /// Fully resolved expression that evaluates to a type
2107 public class TypeExpr : Expression {
2108 public TypeExpr (Type t)
2111 eclass = ExprClass.Type;
2114 override public Expression DoResolve (EmitContext ec)
2119 override public void Emit (EmitContext ec)
2121 throw new Exception ("Implement me");
2126 /// MethodGroup Expression.
2128 /// This is a fully resolved expression that evaluates to a type
2130 public class MethodGroupExpr : Expression {
2131 public MethodBase [] Methods;
2132 Expression instance_expression = null;
2134 public MethodGroupExpr (MemberInfo [] mi)
2136 Methods = new MethodBase [mi.Length];
2137 mi.CopyTo (Methods, 0);
2138 eclass = ExprClass.MethodGroup;
2141 public MethodGroupExpr (ArrayList l)
2143 Methods = new MethodBase [l.Count];
2145 l.CopyTo (Methods, 0);
2146 eclass = ExprClass.MethodGroup;
2150 // `A method group may have associated an instance expression'
2152 public Expression InstanceExpression {
2154 return instance_expression;
2158 instance_expression = value;
2162 override public Expression DoResolve (EmitContext ec)
2167 override public void Emit (EmitContext ec)
2169 throw new Exception ("This should never be reached");
2172 bool RemoveMethods (bool keep_static)
2174 ArrayList smethods = new ArrayList ();
2175 int top = Methods.Length;
2178 for (i = 0; i < top; i++){
2179 MethodBase mb = Methods [i];
2181 if (mb.IsStatic == keep_static)
2185 if (smethods.Count == 0)
2188 Methods = new MethodBase [smethods.Count];
2189 smethods.CopyTo (Methods, 0);
2195 /// Removes any instance methods from the MethodGroup, returns
2196 /// false if the resulting set is empty.
2198 public bool RemoveInstanceMethods ()
2200 return RemoveMethods (true);
2204 /// Removes any static methods from the MethodGroup, returns
2205 /// false if the resulting set is empty.
2207 public bool RemoveStaticMethods ()
2209 return RemoveMethods (false);
2214 /// Fully resolved expression that evaluates to a Field
2216 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2217 public readonly FieldInfo FieldInfo;
2218 public Expression InstanceExpression;
2221 public FieldExpr (FieldInfo fi, Location l)
2224 eclass = ExprClass.Variable;
2225 type = fi.FieldType;
2229 override public Expression DoResolve (EmitContext ec)
2231 if (!FieldInfo.IsStatic){
2232 if (InstanceExpression == null){
2233 throw new Exception ("non-static FieldExpr without instance var\n" +
2234 "You have to assign the Instance variable\n" +
2235 "Of the FieldExpr to set this\n");
2238 InstanceExpression = InstanceExpression.Resolve (ec);
2239 if (InstanceExpression == null)
2246 public Expression DoResolveLValue (EmitContext ec)
2248 if (!FieldInfo.IsInitOnly)
2252 // InitOnly fields can only be assigned in constructors
2255 if (ec.IsConstructor)
2258 Report.Error (191, loc,
2259 "Readonly field can not be assigned outside " +
2260 "of constructor or variable initializer");
2265 override public void Emit (EmitContext ec)
2267 ILGenerator ig = ec.ig;
2269 if (FieldInfo.IsStatic)
2270 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2272 InstanceExpression.Emit (ec);
2274 ig.Emit (OpCodes.Ldfld, FieldInfo);
2278 public void EmitAssign (EmitContext ec, Expression source)
2280 bool is_static = FieldInfo.IsStatic;
2283 InstanceExpression.Emit (ec);
2287 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2289 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2293 public void AddressOf (EmitContext ec)
2295 if (FieldInfo.IsStatic)
2296 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2298 InstanceExpression.Emit (ec);
2299 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2305 /// Expression that evaluates to a Property. The Assign class
2306 /// might set the `Value' expression if we are in an assignment.
2308 /// This is not an LValue because we need to re-write the expression, we
2309 /// can not take data from the stack and store it.
2311 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2312 public readonly PropertyInfo PropertyInfo;
2313 public readonly bool IsStatic;
2314 MethodInfo [] Accessors;
2317 Expression instance_expr;
2319 public PropertyExpr (PropertyInfo pi, Location l)
2322 eclass = ExprClass.PropertyAccess;
2325 Accessors = TypeManager.GetAccessors (pi);
2327 if (Accessors != null)
2328 for (int i = 0; i < Accessors.Length; i++){
2329 if (Accessors [i] != null)
2330 if (Accessors [i].IsStatic)
2334 Accessors = new MethodInfo [2];
2336 type = pi.PropertyType;
2340 // The instance expression associated with this expression
2342 public Expression InstanceExpression {
2344 instance_expr = value;
2348 return instance_expr;
2352 public bool VerifyAssignable ()
2354 if (!PropertyInfo.CanWrite){
2355 Report.Error (200, loc,
2356 "The property `" + PropertyInfo.Name +
2357 "' can not be assigned to, as it has not set accessor");
2364 override public Expression DoResolve (EmitContext ec)
2366 if (!PropertyInfo.CanRead){
2367 Report.Error (154, loc,
2368 "The property `" + PropertyInfo.Name +
2369 "' can not be used in " +
2370 "this context because it lacks a get accessor");
2377 override public void Emit (EmitContext ec)
2379 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2384 // Implements the IAssignMethod interface for assignments
2386 public void EmitAssign (EmitContext ec, Expression source)
2388 Argument arg = new Argument (source, Argument.AType.Expression);
2389 ArrayList args = new ArrayList ();
2392 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2395 override public void EmitStatement (EmitContext ec)
2398 ec.ig.Emit (OpCodes.Pop);
2403 /// Fully resolved expression that evaluates to a Expression
2405 public class EventExpr : Expression {
2406 public readonly EventInfo EventInfo;
2409 public EventExpr (EventInfo ei, Location loc)
2413 eclass = ExprClass.EventAccess;
2416 override public Expression DoResolve (EmitContext ec)
2418 // We are born in resolved state.
2422 override public void Emit (EmitContext ec)
2424 throw new Exception ("Implement me");
2425 // FIXME: Implement.