2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
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
133 public abstract Expression DoResolve (EmitContext ec);
135 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
137 return DoResolve (ec);
141 // Currently Resolve wraps DoResolve to perform sanity
142 // checking and assertion checking on what we expect from Resolve
144 public Expression Resolve (EmitContext ec)
146 Expression e = DoResolve (ec);
149 if (e is SimpleName){
150 SimpleName s = (SimpleName) e;
154 "The name `" + s.Name + "' could not be found in `" +
155 ec.TypeContainer.Name + "'");
159 if (e.ExprClass == ExprClass.Invalid)
160 throw new Exception ("Expression " + e +
161 " ExprClass is Invalid after resolve");
163 if (e.ExprClass != ExprClass.MethodGroup)
165 throw new Exception ("Expression " + e +
166 " did not set its type after Resolve");
173 // Just like `Resolve' above, but this allows SimpleNames to be returned.
174 // This is used by MemberAccess to construct long names that can not be
175 // partially resolved (namespace-qualified names for example).
177 public Expression ResolveWithSimpleName (EmitContext ec)
179 Expression e = DoResolve (ec);
185 if (e.ExprClass == ExprClass.Invalid)
186 throw new Exception ("Expression " + e +
187 " ExprClass is Invalid after resolve");
189 if (e.ExprClass != ExprClass.MethodGroup)
191 throw new Exception ("Expression " + e +
192 " did not set its type after Resolve");
199 // Currently ResolveLValue wraps DoResolveLValue to perform sanity
200 // checking and assertion checking on what we expect from Resolve
202 public Expression ResolveLValue (EmitContext ec, Expression right_side)
204 Expression e = DoResolveLValue (ec, right_side);
207 if (e is SimpleName){
208 SimpleName s = (SimpleName) e;
212 "The name `" + s.Name + "' could not be found in `" +
213 ec.TypeContainer.Name + "'");
217 if (e.ExprClass == ExprClass.Invalid)
218 throw new Exception ("Expression " + e +
219 " ExprClass is Invalid after resolve");
221 if (e.ExprClass != ExprClass.MethodGroup)
223 throw new Exception ("Expression " + e +
224 " did not set its type after Resolve");
231 // Emits the code for the expression
236 // The Emit method is invoked to generate the code
237 // for the expression.
240 public abstract void Emit (EmitContext ec);
243 // This method should perform a reduction of the expression. This should
244 // never return null.
246 public virtual Expression Reduce (EmitContext ec)
252 // Protected constructor. Only derivate types should
253 // be able to be created
256 protected Expression ()
258 eclass = ExprClass.Invalid;
263 // Returns a literalized version of a literal FieldInfo
265 public static Expression Literalize (object v, Type t)
267 if (t == TypeManager.int32_type)
268 return new IntLiteral ((int) v);
269 else if (t == TypeManager.uint32_type)
270 return new UIntLiteral ((uint) v);
271 else if (t == TypeManager.int64_type)
272 return new LongLiteral ((long) v);
273 else if (t == TypeManager.uint64_type)
274 return new ULongLiteral ((ulong) v);
275 else if (t == TypeManager.float_type)
276 return new FloatLiteral ((float) v);
277 else if (t == TypeManager.double_type)
278 return new DoubleLiteral ((double) v);
279 else if (t == TypeManager.string_type)
280 return new StringLiteral ((string) v);
281 else if (t == TypeManager.short_type)
282 return new IntLiteral ((int) ((short)v));
283 else if (t == TypeManager.ushort_type)
284 return new IntLiteral ((int) ((ushort)v));
285 else if (t == TypeManager.sbyte_type)
286 return new IntLiteral ((int) ((sbyte)v));
287 else if (t == TypeManager.byte_type)
288 return new IntLiteral ((int) ((byte)v));
289 else if (t == TypeManager.char_type)
290 return new IntLiteral ((int) ((char)v));
292 throw new Exception ("Unknown type for literal (" + t +
297 // Returns a fully formed expression after a MemberLookup
299 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
302 return new EventExpr ((EventInfo) mi, loc);
303 else if (mi is FieldInfo)
304 return new FieldExpr ((FieldInfo) mi, loc);
305 else if (mi is PropertyInfo)
306 return new PropertyExpr ((PropertyInfo) mi, loc);
308 return new TypeExpr ((Type) mi);
314 // FIXME: Probably implement a cache for (t,name,current_access_set)?
316 // FIXME: We need to cope with access permissions here, or this wont
319 // This code could use some optimizations, but we need to do some
320 // measurements. For example, we could use a delegate to `flag' when
321 // something can not any longer be a method-group (because it is something
325 // If the return value is an Array, then it is an array of
328 // If the return value is an MemberInfo, it is anything, but a Method
332 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
333 // the arguments here and have MemberLookup return only the methods that
334 // match the argument count/type, unlike we are doing now (we delay this
337 // This is so we can catch correctly attempts to invoke instance methods
338 // from a static body (scan for error 120 in ResolveSimpleName).
340 public static Expression MemberLookup (EmitContext ec, Type t, string name,
341 bool same_type, MemberTypes mt,
342 BindingFlags bf, Location loc)
345 bf |= BindingFlags.NonPublic;
347 MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
348 t, mt, bf, Type.FilterName, name);
358 if (mi.Length == 1 && !(mi [0] is MethodBase))
359 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
361 for (int i = 0; i < mi.Length; i++)
362 if (!(mi [i] is MethodBase)){
363 Error (-5, "Do not know how to reproduce this case: " +
364 "Methods and non-Method with the same name, " +
365 "report this please");
367 for (i = 0; i < mi.Length; i++){
368 Type tt = mi [i].GetType ();
370 Console.WriteLine (i + ": " + mi [i]);
371 while (tt != TypeManager.object_type){
372 Console.WriteLine (tt);
378 return new MethodGroupExpr (mi);
381 public const MemberTypes AllMemberTypes =
382 MemberTypes.Constructor |
386 MemberTypes.NestedType |
387 MemberTypes.Property;
389 public const BindingFlags AllBindingsFlags =
390 BindingFlags.Public |
391 BindingFlags.Static |
392 BindingFlags.Instance;
394 public static Expression MemberLookup (EmitContext ec, Type t, string name,
395 bool same_type, Location loc)
397 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
400 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
402 Type expr_type = expr.Type;
404 if (target_type == TypeManager.object_type) {
405 if (expr_type.IsClass)
406 return new EmptyCast (expr, target_type);
407 if (expr_type.IsValueType)
408 return new BoxedCast (expr);
409 } else if (expr_type.IsSubclassOf (target_type)) {
410 return new EmptyCast (expr, target_type);
412 // from any class-type S to any interface-type T.
413 if (expr_type.IsClass && target_type.IsInterface) {
415 if (TypeManager.ImplementsInterface (expr_type, target_type))
416 return new EmptyCast (expr, target_type);
421 // from any interface type S to interface-type T.
422 if (expr_type.IsInterface && target_type.IsInterface) {
424 if (TypeManager.ImplementsInterface (expr_type, target_type))
425 return new EmptyCast (expr, target_type);
430 // from an array-type S to an array-type of type T
431 if (expr_type.IsArray && target_type.IsArray) {
432 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
434 Type expr_element_type = expr_type.GetElementType ();
435 Type target_element_type = target_type.GetElementType ();
437 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
438 if (StandardConversionExists (expr_element_type,
439 target_element_type))
440 return new EmptyCast (expr, target_type);
445 // from an array-type to System.Array
446 if (expr_type.IsArray && target_type == TypeManager.array_type)
447 return new EmptyCast (expr, target_type);
449 // from any delegate type to System.Delegate
450 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
451 target_type == TypeManager.delegate_type)
452 return new EmptyCast (expr, target_type);
454 // from any array-type or delegate type into System.ICloneable.
455 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
456 if (target_type == TypeManager.icloneable_type)
457 return new EmptyCast (expr, target_type);
459 // from the null type to any reference-type.
460 if (expr is NullLiteral)
461 return new EmptyCast (expr, target_type);
471 // Handles expressions like this: decimal d; d = 1;
472 // and changes them into: decimal d; d = new System.Decimal (1);
474 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
476 ArrayList args = new ArrayList ();
478 args.Add (new Argument (expr, Argument.AType.Expression));
480 Expression ne = new New (target.FullName, args,
483 return ne.Resolve (ec);
487 // Implicit Numeric Conversions.
489 // expr is the expression to convert, returns a new expression of type
490 // target_type or null if an implicit conversion is not possible.
493 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
494 Type target_type, Location loc)
496 Type expr_type = expr.Type;
499 // Attempt to do the implicit constant expression conversions
501 if (expr is IntLiteral){
504 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
507 } else if (expr is LongLiteral){
509 // Try the implicit constant expression conversion
510 // from long to ulong, instead of a nice routine,
513 if (((LongLiteral) expr).Value > 0)
514 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
517 if (expr_type == TypeManager.sbyte_type){
519 // From sbyte to short, int, long, float, double.
521 if (target_type == TypeManager.int32_type)
522 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
523 if (target_type == TypeManager.int64_type)
524 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
525 if (target_type == TypeManager.double_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
527 if (target_type == TypeManager.float_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
529 if (target_type == TypeManager.short_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
531 if (target_type == TypeManager.decimal_type)
532 return InternalTypeConstructor (ec, expr, target_type);
533 } else if (expr_type == TypeManager.byte_type){
535 // From byte to short, ushort, int, uint, long, ulong, float, double
537 if ((target_type == TypeManager.short_type) ||
538 (target_type == TypeManager.ushort_type) ||
539 (target_type == TypeManager.int32_type) ||
540 (target_type == TypeManager.uint32_type))
541 return new EmptyCast (expr, target_type);
543 if (target_type == TypeManager.uint64_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
545 if (target_type == TypeManager.int64_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
548 if (target_type == TypeManager.float_type)
549 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
550 if (target_type == TypeManager.double_type)
551 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
552 if (target_type == TypeManager.decimal_type)
553 return InternalTypeConstructor (ec, expr, target_type);
554 } else if (expr_type == TypeManager.short_type){
556 // From short to int, long, float, double
558 if (target_type == TypeManager.int32_type)
559 return new EmptyCast (expr, target_type);
560 if (target_type == TypeManager.int64_type)
561 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
562 if (target_type == TypeManager.double_type)
563 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
564 if (target_type == TypeManager.float_type)
565 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
566 if (target_type == TypeManager.decimal_type)
567 return InternalTypeConstructor (ec, expr, target_type);
568 } else if (expr_type == TypeManager.ushort_type){
570 // From ushort to int, uint, long, ulong, float, double
572 if (target_type == TypeManager.uint32_type)
573 return new EmptyCast (expr, target_type);
575 if (target_type == TypeManager.uint64_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
577 if (target_type == TypeManager.int32_type)
578 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
579 if (target_type == TypeManager.int64_type)
580 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
581 if (target_type == TypeManager.double_type)
582 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
583 if (target_type == TypeManager.float_type)
584 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
585 if (target_type == TypeManager.decimal_type)
586 return InternalTypeConstructor (ec, expr, target_type);
587 } else if (expr_type == TypeManager.int32_type){
589 // From int to long, float, double
591 if (target_type == TypeManager.int64_type)
592 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
593 if (target_type == TypeManager.double_type)
594 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
595 if (target_type == TypeManager.float_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
597 if (target_type == TypeManager.decimal_type)
598 return InternalTypeConstructor (ec, expr, target_type);
599 } else if (expr_type == TypeManager.uint32_type){
601 // From uint to long, ulong, float, double
603 if (target_type == TypeManager.int64_type)
604 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
605 if (target_type == TypeManager.uint64_type)
606 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
607 if (target_type == TypeManager.double_type)
608 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
610 if (target_type == TypeManager.float_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
613 if (target_type == TypeManager.decimal_type)
614 return InternalTypeConstructor (ec, expr, target_type);
615 } else if ((expr_type == TypeManager.uint64_type) ||
616 (expr_type == TypeManager.int64_type)){
618 // From long/ulong to float, double
620 if (target_type == TypeManager.double_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
623 if (target_type == TypeManager.float_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
626 if (target_type == TypeManager.decimal_type)
627 return InternalTypeConstructor (ec, expr, target_type);
628 } else if (expr_type == TypeManager.char_type){
630 // From char to ushort, int, uint, long, ulong, float, double
632 if ((target_type == TypeManager.ushort_type) ||
633 (target_type == TypeManager.int32_type) ||
634 (target_type == TypeManager.uint32_type))
635 return new EmptyCast (expr, target_type);
636 if (target_type == TypeManager.uint64_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
638 if (target_type == TypeManager.int64_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
640 if (target_type == TypeManager.float_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
642 if (target_type == TypeManager.double_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
644 if (target_type == TypeManager.decimal_type)
645 return InternalTypeConstructor (ec, expr, target_type);
646 } else if (expr_type == TypeManager.float_type){
650 if (target_type == TypeManager.double_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
658 // Determines if a standard implicit conversion exists from
659 // expr_type to target_type
661 public static bool StandardConversionExists (Type expr_type, Type target_type)
663 if (expr_type == target_type)
666 // First numeric conversions
668 if (expr_type == TypeManager.sbyte_type){
670 // From sbyte to short, int, long, float, double.
672 if ((target_type == TypeManager.int32_type) ||
673 (target_type == TypeManager.int64_type) ||
674 (target_type == TypeManager.double_type) ||
675 (target_type == TypeManager.float_type) ||
676 (target_type == TypeManager.short_type) ||
677 (target_type == TypeManager.decimal_type))
680 } else if (expr_type == TypeManager.byte_type){
682 // From byte to short, ushort, int, uint, long, ulong, float, double
684 if ((target_type == TypeManager.short_type) ||
685 (target_type == TypeManager.ushort_type) ||
686 (target_type == TypeManager.int32_type) ||
687 (target_type == TypeManager.uint32_type) ||
688 (target_type == TypeManager.uint64_type) ||
689 (target_type == TypeManager.int64_type) ||
690 (target_type == TypeManager.float_type) ||
691 (target_type == TypeManager.double_type) ||
692 (target_type == TypeManager.decimal_type))
695 } else if (expr_type == TypeManager.short_type){
697 // From short to int, long, float, double
699 if ((target_type == TypeManager.int32_type) ||
700 (target_type == TypeManager.int64_type) ||
701 (target_type == TypeManager.double_type) ||
702 (target_type == TypeManager.float_type) ||
703 (target_type == TypeManager.decimal_type))
706 } else if (expr_type == TypeManager.ushort_type){
708 // From ushort to int, uint, long, ulong, float, double
710 if ((target_type == TypeManager.uint32_type) ||
711 (target_type == TypeManager.uint64_type) ||
712 (target_type == TypeManager.int32_type) ||
713 (target_type == TypeManager.int64_type) ||
714 (target_type == TypeManager.double_type) ||
715 (target_type == TypeManager.float_type) ||
716 (target_type == TypeManager.decimal_type))
719 } else if (expr_type == TypeManager.int32_type){
721 // From int to long, float, double
723 if ((target_type == TypeManager.int64_type) ||
724 (target_type == TypeManager.double_type) ||
725 (target_type == TypeManager.float_type) ||
726 (target_type == TypeManager.decimal_type))
729 } else if (expr_type == TypeManager.uint32_type){
731 // From uint to long, ulong, float, double
733 if ((target_type == TypeManager.int64_type) ||
734 (target_type == TypeManager.uint64_type) ||
735 (target_type == TypeManager.double_type) ||
736 (target_type == TypeManager.float_type) ||
737 (target_type == TypeManager.decimal_type))
740 } else if ((expr_type == TypeManager.uint64_type) ||
741 (expr_type == TypeManager.int64_type)) {
743 // From long/ulong to float, double
745 if ((target_type == TypeManager.double_type) ||
746 (target_type == TypeManager.float_type) ||
747 (target_type == TypeManager.decimal_type))
750 } else if (expr_type == TypeManager.char_type){
752 // From char to ushort, int, uint, long, ulong, float, double
754 if ((target_type == TypeManager.ushort_type) ||
755 (target_type == TypeManager.int32_type) ||
756 (target_type == TypeManager.uint32_type) ||
757 (target_type == TypeManager.uint64_type) ||
758 (target_type == TypeManager.int64_type) ||
759 (target_type == TypeManager.float_type) ||
760 (target_type == TypeManager.double_type) ||
761 (target_type == TypeManager.decimal_type))
764 } else if (expr_type == TypeManager.float_type){
768 if (target_type == TypeManager.double_type)
772 // Next reference conversions
774 if (target_type == TypeManager.object_type) {
775 if ((expr_type.IsClass) ||
776 (expr_type.IsValueType))
779 } else if (expr_type.IsSubclassOf (target_type)) {
783 // from any class-type S to any interface-type T.
784 if (expr_type.IsClass && target_type.IsInterface)
787 // from any interface type S to interface-type T.
788 // FIXME : Is it right to use IsAssignableFrom ?
789 if (expr_type.IsInterface && target_type.IsInterface)
790 if (target_type.IsAssignableFrom (expr_type))
793 // from an array-type S to an array-type of type T
794 if (expr_type.IsArray && target_type.IsArray) {
795 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
797 Type expr_element_type = expr_type.GetElementType ();
798 Type target_element_type = target_type.GetElementType ();
800 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
801 if (StandardConversionExists (expr_element_type,
802 target_element_type))
807 // from an array-type to System.Array
808 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
811 // from any delegate type to System.Delegate
812 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
813 target_type == TypeManager.delegate_type)
814 if (target_type.IsAssignableFrom (expr_type))
817 // from any array-type or delegate type into System.ICloneable.
818 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
819 if (target_type == TypeManager.icloneable_type)
822 // from the null type to any reference-type.
823 // FIXME : How do we do this ?
831 // Finds "most encompassed type" according to the spec (13.4.2)
832 // amongst the methods in the MethodGroupExpr which convert from a
833 // type encompassing source_type
835 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
839 for (int i = me.Methods.Length; i > 0; ) {
842 MethodBase mb = me.Methods [i];
843 ParameterData pd = Invocation.GetParameterData (mb);
844 Type param_type = pd.ParameterType (0);
846 if (StandardConversionExists (source_type, param_type)) {
850 if (StandardConversionExists (param_type, best))
859 // Finds "most encompassing type" according to the spec (13.4.2)
860 // amongst the methods in the MethodGroupExpr which convert to a
861 // type encompassed by target_type
863 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
867 for (int i = me.Methods.Length; i > 0; ) {
870 MethodInfo mi = (MethodInfo) me.Methods [i];
871 Type ret_type = mi.ReturnType;
873 if (StandardConversionExists (ret_type, target)) {
877 if (!StandardConversionExists (ret_type, best))
889 // User-defined Implicit conversions
891 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
892 Type target, Location loc)
894 return UserDefinedConversion (ec, source, target, loc, false);
898 // User-defined Explicit conversions
900 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
901 Type target, Location loc)
903 return UserDefinedConversion (ec, source, target, loc, true);
907 // User-defined conversions
909 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
910 Type target, Location loc,
911 bool look_for_explicit)
913 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
914 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
916 MethodBase method = null;
917 Type source_type = source.Type;
921 // If we have a boolean type, we need to check for the True operator
923 // FIXME : How does the False operator come into the picture ?
924 // FIXME : This doesn't look complete and very correct !
925 if (target == TypeManager.bool_type)
928 op_name = "op_Implicit";
930 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
932 if (source_type.BaseType != null)
933 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
935 mg3 = MemberLookup (ec, target, op_name, false, loc);
937 if (target.BaseType != null)
938 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
940 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
941 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
943 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
945 MethodGroupExpr union4 = null;
947 if (look_for_explicit) {
949 op_name = "op_Explicit";
951 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
953 if (source_type.BaseType != null)
954 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
956 mg7 = MemberLookup (ec, target, op_name, false, loc);
958 if (target.BaseType != null)
959 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
961 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
962 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
964 union4 = Invocation.MakeUnionSet (union5, union6);
967 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
971 Type most_specific_source, most_specific_target;
973 most_specific_source = FindMostEncompassedType (union, source_type);
974 if (most_specific_source == null)
977 most_specific_target = FindMostEncompassingType (union, target);
978 if (most_specific_target == null)
983 for (int i = union.Methods.Length; i > 0;) {
986 MethodBase mb = union.Methods [i];
987 ParameterData pd = Invocation.GetParameterData (mb);
988 MethodInfo mi = (MethodInfo) union.Methods [i];
990 if (pd.ParameterType (0) == most_specific_source &&
991 mi.ReturnType == most_specific_target) {
997 if (method == null || count > 1) {
998 Report.Error (-11, loc, "Ambiguous user defined conversion");
1003 // This will do the conversion to the best match that we
1004 // found. Now we need to perform an implict standard conversion
1005 // if the best match was not the type that we were requested
1008 if (look_for_explicit)
1009 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1011 source = ConvertImplicitStandard (ec, source,
1012 most_specific_source, loc);
1017 e = new UserCast ((MethodInfo) method, source);
1019 if (e.Type != target){
1020 if (!look_for_explicit)
1021 e = ConvertImplicitStandard (ec, e, target, loc);
1023 e = ConvertExplicitStandard (ec, e, target, loc);
1034 // Converts implicitly the resolved expression `expr' into the
1035 // `target_type'. It returns a new expression that can be used
1036 // in a context that expects a `target_type'.
1038 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1039 Type target_type, Location loc)
1041 Type expr_type = expr.Type;
1044 if (expr_type == target_type)
1047 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1051 e = ImplicitReferenceConversion (expr, target_type);
1055 e = ImplicitUserConversion (ec, expr, target_type, loc);
1059 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1060 IntLiteral i = (IntLiteral) expr;
1063 return new EmptyCast (expr, target_type);
1071 // Attempts to apply the `Standard Implicit
1072 // Conversion' rules to the expression `expr' into
1073 // the `target_type'. It returns a new expression
1074 // that can be used in a context that expects a
1077 // This is different from `ConvertImplicit' in that the
1078 // user defined implicit conversions are excluded.
1080 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1081 Type target_type, Location loc)
1083 Type expr_type = expr.Type;
1086 if (expr_type == target_type)
1089 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1093 e = ImplicitReferenceConversion (expr, target_type);
1097 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1098 IntLiteral i = (IntLiteral) expr;
1101 return new EmptyCast (expr, target_type);
1106 // Attemps to perform an implict constant conversion of the IntLiteral
1107 // into a different data type using casts (See Implicit Constant
1108 // Expression Conversions)
1110 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1112 int value = il.Value;
1114 if (target_type == TypeManager.sbyte_type){
1115 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1117 } else if (target_type == TypeManager.byte_type){
1118 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1120 } else if (target_type == TypeManager.short_type){
1121 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1123 } else if (target_type == TypeManager.ushort_type){
1124 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1126 } else if (target_type == TypeManager.uint32_type){
1128 // we can optimize this case: a positive int32
1129 // always fits on a uint32
1133 } else if (target_type == TypeManager.uint64_type){
1135 // we can optimize this case: a positive int32
1136 // always fits on a uint64. But we need an opcode
1140 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1147 // Attemptes to implicityly convert `target' into `type', using
1148 // ConvertImplicit. If there is no implicit conversion, then
1149 // an error is signaled
1151 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1152 Type type, Location loc)
1156 e = ConvertImplicit (ec, target, type, loc);
1160 string msg = "Can not convert implicitly from `"+
1161 TypeManager.CSharpName (target.Type) + "' to `" +
1162 TypeManager.CSharpName (type) + "'";
1164 Error (29, loc, msg);
1170 // Performs the explicit numeric conversions
1172 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1175 Type expr_type = expr.Type;
1177 if (expr_type == TypeManager.sbyte_type){
1179 // From sbyte to byte, ushort, uint, ulong, char
1181 if (target_type == TypeManager.byte_type)
1182 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1183 if (target_type == TypeManager.ushort_type)
1184 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1185 if (target_type == TypeManager.uint32_type)
1186 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1187 if (target_type == TypeManager.uint64_type)
1188 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1189 if (target_type == TypeManager.char_type)
1190 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1191 } else if (expr_type == TypeManager.byte_type){
1193 // From byte to sbyte and char
1195 if (target_type == TypeManager.sbyte_type)
1196 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1197 if (target_type == TypeManager.char_type)
1198 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1199 } else if (expr_type == TypeManager.short_type){
1201 // From short to sbyte, byte, ushort, uint, ulong, char
1203 if (target_type == TypeManager.sbyte_type)
1204 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1205 if (target_type == TypeManager.byte_type)
1206 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1207 if (target_type == TypeManager.ushort_type)
1208 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1209 if (target_type == TypeManager.uint32_type)
1210 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1211 if (target_type == TypeManager.uint64_type)
1212 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1213 if (target_type == TypeManager.char_type)
1214 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1215 } else if (expr_type == TypeManager.ushort_type){
1217 // From ushort to sbyte, byte, short, char
1219 if (target_type == TypeManager.sbyte_type)
1220 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1221 if (target_type == TypeManager.byte_type)
1222 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1223 if (target_type == TypeManager.short_type)
1224 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1225 if (target_type == TypeManager.char_type)
1226 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1227 } else if (expr_type == TypeManager.int32_type){
1229 // From int to sbyte, byte, short, ushort, uint, ulong, char
1231 if (target_type == TypeManager.sbyte_type)
1232 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1233 if (target_type == TypeManager.byte_type)
1234 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1235 if (target_type == TypeManager.short_type)
1236 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1237 if (target_type == TypeManager.ushort_type)
1238 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1239 if (target_type == TypeManager.uint32_type)
1240 return new EmptyCast (expr, target_type);
1241 if (target_type == TypeManager.uint64_type)
1242 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1243 if (target_type == TypeManager.char_type)
1244 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1245 } else if (expr_type == TypeManager.uint32_type){
1247 // From uint to sbyte, byte, short, ushort, int, char
1249 if (target_type == TypeManager.sbyte_type)
1250 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1251 if (target_type == TypeManager.byte_type)
1252 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1253 if (target_type == TypeManager.short_type)
1254 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1255 if (target_type == TypeManager.ushort_type)
1256 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1257 if (target_type == TypeManager.int32_type)
1258 return new EmptyCast (expr, target_type);
1259 if (target_type == TypeManager.char_type)
1260 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1261 } else if (expr_type == TypeManager.int64_type){
1263 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1265 if (target_type == TypeManager.sbyte_type)
1266 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1267 if (target_type == TypeManager.byte_type)
1268 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1269 if (target_type == TypeManager.short_type)
1270 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1271 if (target_type == TypeManager.ushort_type)
1272 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1273 if (target_type == TypeManager.int32_type)
1274 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1275 if (target_type == TypeManager.uint32_type)
1276 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1277 if (target_type == TypeManager.uint64_type)
1278 return new EmptyCast (expr, target_type);
1279 if (target_type == TypeManager.char_type)
1280 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1281 } else if (expr_type == TypeManager.uint64_type){
1283 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1285 if (target_type == TypeManager.sbyte_type)
1286 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1287 if (target_type == TypeManager.byte_type)
1288 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1289 if (target_type == TypeManager.short_type)
1290 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1291 if (target_type == TypeManager.ushort_type)
1292 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1293 if (target_type == TypeManager.int32_type)
1294 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1295 if (target_type == TypeManager.uint32_type)
1296 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1297 if (target_type == TypeManager.int64_type)
1298 return new EmptyCast (expr, target_type);
1299 if (target_type == TypeManager.char_type)
1300 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1301 } else if (expr_type == TypeManager.char_type){
1303 // From char to sbyte, byte, short
1305 if (target_type == TypeManager.sbyte_type)
1306 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1307 if (target_type == TypeManager.byte_type)
1308 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1309 if (target_type == TypeManager.short_type)
1310 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1311 } else if (expr_type == TypeManager.float_type){
1313 // From float to sbyte, byte, short,
1314 // ushort, int, uint, long, ulong, char
1317 if (target_type == TypeManager.sbyte_type)
1318 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1319 if (target_type == TypeManager.byte_type)
1320 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1321 if (target_type == TypeManager.short_type)
1322 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1323 if (target_type == TypeManager.ushort_type)
1324 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1325 if (target_type == TypeManager.int32_type)
1326 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1327 if (target_type == TypeManager.uint32_type)
1328 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1329 if (target_type == TypeManager.int64_type)
1330 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1331 if (target_type == TypeManager.uint64_type)
1332 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1333 if (target_type == TypeManager.char_type)
1334 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1335 if (target_type == TypeManager.decimal_type)
1336 return InternalTypeConstructor (ec, expr, target_type);
1337 } else if (expr_type == TypeManager.double_type){
1339 // From double to byte, byte, short,
1340 // ushort, int, uint, long, ulong,
1341 // char, float or decimal
1343 if (target_type == TypeManager.sbyte_type)
1344 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1345 if (target_type == TypeManager.byte_type)
1346 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1347 if (target_type == TypeManager.short_type)
1348 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1349 if (target_type == TypeManager.ushort_type)
1350 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1351 if (target_type == TypeManager.int32_type)
1352 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1353 if (target_type == TypeManager.uint32_type)
1354 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1355 if (target_type == TypeManager.int64_type)
1356 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1357 if (target_type == TypeManager.uint64_type)
1358 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1359 if (target_type == TypeManager.char_type)
1360 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1361 if (target_type == TypeManager.float_type)
1362 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1363 if (target_type == TypeManager.decimal_type)
1364 return InternalTypeConstructor (ec, expr, target_type);
1367 // decimal is taken care of by the op_Explicit methods.
1373 // Returns whether an explicit reference conversion can be performed
1374 // from source_type to target_type
1376 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1378 bool target_is_value_type = target_type.IsValueType;
1380 if (source_type == target_type)
1384 // From object to any reference type
1386 if (source_type == TypeManager.object_type && !target_is_value_type)
1390 // From any class S to any class-type T, provided S is a base class of T
1392 if (target_type.IsSubclassOf (source_type))
1396 // From any interface type S to any interface T provided S is not derived from T
1398 if (source_type.IsInterface && target_type.IsInterface){
1399 if (!target_type.IsSubclassOf (source_type))
1404 // From any class type S to any interface T, provides S is not sealed
1405 // and provided S does not implement T.
1407 if (target_type.IsInterface && !source_type.IsSealed &&
1408 !target_type.IsAssignableFrom (source_type))
1412 // From any interface-type S to to any class type T, provided T is not
1413 // sealed, or provided T implements S.
1415 if (source_type.IsInterface &&
1416 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1419 // From an array type S with an element type Se to an array type T with an
1420 // element type Te provided all the following are true:
1421 // * S and T differe only in element type, in other words, S and T
1422 // have the same number of dimensions.
1423 // * Both Se and Te are reference types
1424 // * An explicit referenc conversions exist from Se to Te
1426 if (source_type.IsArray && target_type.IsArray) {
1427 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1429 Type source_element_type = source_type.GetElementType ();
1430 Type target_element_type = target_type.GetElementType ();
1432 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1433 if (ExplicitReferenceConversionExists (source_element_type,
1434 target_element_type))
1440 // From System.Array to any array-type
1441 if (source_type == TypeManager.array_type &&
1442 target_type.IsSubclassOf (TypeManager.array_type)){
1447 // From System delegate to any delegate-type
1449 if (source_type == TypeManager.delegate_type &&
1450 target_type.IsSubclassOf (TypeManager.delegate_type))
1454 // From ICloneable to Array or Delegate types
1456 if (source_type == TypeManager.icloneable_type &&
1457 (target_type == TypeManager.array_type ||
1458 target_type == TypeManager.delegate_type))
1465 // Implements Explicit Reference conversions
1467 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1469 Type source_type = source.Type;
1470 bool target_is_value_type = target_type.IsValueType;
1473 // From object to any reference type
1475 if (source_type == TypeManager.object_type && !target_is_value_type)
1476 return new ClassCast (source, target_type);
1480 // From any class S to any class-type T, provided S is a base class of T
1482 if (target_type.IsSubclassOf (source_type))
1483 return new ClassCast (source, target_type);
1486 // From any interface type S to any interface T provided S is not derived from T
1488 if (source_type.IsInterface && target_type.IsInterface){
1490 Type [] ifaces = source_type.GetInterfaces ();
1492 if (TypeManager.ImplementsInterface (source_type, target_type))
1495 return new ClassCast (source, target_type);
1499 // From any class type S to any interface T, provides S is not sealed
1500 // and provided S does not implement T.
1502 if (target_type.IsInterface && !source_type.IsSealed) {
1504 if (TypeManager.ImplementsInterface (source_type, target_type))
1507 return new ClassCast (source, target_type);
1512 // From any interface-type S to to any class type T, provided T is not
1513 // sealed, or provided T implements S.
1515 if (source_type.IsInterface) {
1517 if (target_type.IsSealed)
1520 if (TypeManager.ImplementsInterface (target_type, source_type))
1521 return new ClassCast (source, target_type);
1526 // From an array type S with an element type Se to an array type T with an
1527 // element type Te provided all the following are true:
1528 // * S and T differe only in element type, in other words, S and T
1529 // have the same number of dimensions.
1530 // * Both Se and Te are reference types
1531 // * An explicit referenc conversions exist from Se to Te
1533 if (source_type.IsArray && target_type.IsArray) {
1534 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1536 Type source_element_type = source_type.GetElementType ();
1537 Type target_element_type = target_type.GetElementType ();
1539 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1540 if (ExplicitReferenceConversionExists (source_element_type,
1541 target_element_type))
1542 return new ClassCast (source, target_type);
1547 // From System.Array to any array-type
1548 if (source_type == TypeManager.array_type &&
1549 target_type.IsSubclassOf (TypeManager.array_type)){
1550 return new ClassCast (source, target_type);
1554 // From System delegate to any delegate-type
1556 if (source_type == TypeManager.delegate_type &&
1557 target_type.IsSubclassOf (TypeManager.delegate_type))
1558 return new ClassCast (source, target_type);
1561 // From ICloneable to Array or Delegate types
1563 if (source_type == TypeManager.icloneable_type &&
1564 (target_type == TypeManager.array_type ||
1565 target_type == TypeManager.delegate_type))
1566 return new ClassCast (source, target_type);
1572 // Performs an explicit conversion of the expression `expr' whose
1573 // type is expr.Type to `target_type'.
1575 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1576 Type target_type, Location loc)
1578 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1583 ne = ConvertNumericExplicit (ec, expr, target_type);
1587 ne = ConvertReferenceExplicit (expr, target_type);
1591 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1595 error30 (loc, expr.Type, target_type);
1600 // Same as ConverExplicit, only it doesn't include user defined conversions
1602 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1603 Type target_type, Location l)
1605 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1610 ne = ConvertNumericExplicit (ec, expr, target_type);
1614 ne = ConvertReferenceExplicit (expr, target_type);
1618 error30 (l, expr.Type, target_type);
1622 static string ExprClassName (ExprClass c)
1625 case ExprClass.Invalid:
1627 case ExprClass.Value:
1629 case ExprClass.Variable:
1631 case ExprClass.Namespace:
1633 case ExprClass.Type:
1635 case ExprClass.MethodGroup:
1636 return "method group";
1637 case ExprClass.PropertyAccess:
1638 return "property access";
1639 case ExprClass.EventAccess:
1640 return "event access";
1641 case ExprClass.IndexerAccess:
1642 return "indexer access";
1643 case ExprClass.Nothing:
1646 throw new Exception ("Should not happen");
1650 // Reports that we were expecting `expr' to be of class `expected'
1652 protected void report118 (Location loc, Expression expr, string expected)
1654 string kind = "Unknown";
1657 kind = ExprClassName (expr.ExprClass);
1659 Error (118, loc, "Expression denotes a '" + kind +
1660 "' where an " + expected + " was expected");
1664 // This function tries to reduce the expression performing
1665 // constant folding and common subexpression elimination
1667 static public Expression Reduce (EmitContext ec, Expression e)
1669 //Console.WriteLine ("Calling reduce");
1670 return e.Reduce (ec);
1675 // This is just a base class for expressions that can
1676 // appear on statements (invocations, object creation,
1677 // assignments, post/pre increment and decrement). The idea
1678 // being that they would support an extra Emition interface that
1679 // does not leave a result on the stack.
1682 public abstract class ExpressionStatement : Expression {
1685 // Requests the expression to be emitted in a `statement'
1686 // context. This means that no new value is left on the
1687 // stack after invoking this method (constrasted with
1688 // Emit that will always leave a value on the stack).
1690 public abstract void EmitStatement (EmitContext ec);
1694 // This kind of cast is used to encapsulate the child
1695 // whose type is child.Type into an expression that is
1696 // reported to return "return_type". This is used to encapsulate
1697 // expressions which have compatible types, but need to be dealt
1698 // at higher levels with.
1700 // For example, a "byte" expression could be encapsulated in one
1701 // of these as an "unsigned int". The type for the expression
1702 // would be "unsigned int".
1706 public class EmptyCast : Expression {
1707 protected Expression child;
1709 public EmptyCast (Expression child, Type return_type)
1711 ExprClass = child.ExprClass;
1716 public override Expression DoResolve (EmitContext ec)
1718 // This should never be invoked, we are born in fully
1719 // initialized state.
1724 public override void Emit (EmitContext ec)
1732 // This class is used to wrap literals which belong inside Enums
1735 public class EnumLiteral : Literal {
1738 public EnumLiteral (Expression child, Type enum_type)
1740 ExprClass = child.ExprClass;
1745 public override Expression DoResolve (EmitContext ec)
1747 // This should never be invoked, we are born in fully
1748 // initialized state.
1753 public override void Emit (EmitContext ec)
1758 public override object GetValue ()
1760 return ((Literal) child).GetValue ();
1763 public override string AsString ()
1765 return ((Literal) child).AsString ();
1770 // This kind of cast is used to encapsulate Value Types in objects.
1772 // The effect of it is to box the value type emitted by the previous
1775 public class BoxedCast : EmptyCast {
1777 public BoxedCast (Expression expr)
1778 : base (expr, TypeManager.object_type)
1782 public override Expression DoResolve (EmitContext ec)
1784 // This should never be invoked, we are born in fully
1785 // initialized state.
1790 public override void Emit (EmitContext ec)
1793 ec.ig.Emit (OpCodes.Box, child.Type);
1798 // This kind of cast is used to encapsulate a child expression
1799 // that can be trivially converted to a target type using one or
1800 // two opcodes. The opcodes are passed as arguments.
1802 public class OpcodeCast : EmptyCast {
1806 public OpcodeCast (Expression child, Type return_type, OpCode op)
1807 : base (child, return_type)
1811 second_valid = false;
1814 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1815 : base (child, return_type)
1820 second_valid = true;
1823 public override Expression DoResolve (EmitContext ec)
1825 // This should never be invoked, we are born in fully
1826 // initialized state.
1831 public override void Emit (EmitContext ec)
1843 // This kind of cast is used to encapsulate a child and cast it
1844 // to the class requested
1846 public class ClassCast : EmptyCast {
1847 public ClassCast (Expression child, Type return_type)
1848 : base (child, return_type)
1853 public override Expression DoResolve (EmitContext ec)
1855 // This should never be invoked, we are born in fully
1856 // initialized state.
1861 public override void Emit (EmitContext ec)
1865 ec.ig.Emit (OpCodes.Castclass, type);
1871 // SimpleName expressions are initially formed of a single
1872 // word and it only happens at the beginning of the expression.
1874 // The expression will try to be bound to a Field, a Method
1875 // group or a Property. If those fail we pass the name to our
1876 // caller and the SimpleName is compounded to perform a type
1877 // lookup. The idea behind this process is that we want to avoid
1878 // creating a namespace map from the assemblies, as that requires
1879 // the GetExportedTypes function to be called and a hashtable to
1880 // be constructed which reduces startup time. If later we find
1881 // that this is slower, we should create a `NamespaceExpr' expression
1882 // that fully participates in the resolution process.
1884 // For example `System.Console.WriteLine' is decomposed into
1885 // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1887 // The first SimpleName wont produce a match on its own, so it will
1889 // MemberAccess (SimpleName ("System.Console"), "WriteLine").
1891 // System.Console will produce a TypeExpr match.
1893 // The downside of this is that we might be hitting `LookupType' too many
1894 // times with this scheme.
1896 public class SimpleName : Expression {
1897 public readonly string Name;
1898 public readonly Location Location;
1900 public SimpleName (string name, Location l)
1906 public static void Error120 (Location l, string name)
1910 "An object reference is required " +
1911 "for the non-static field `"+name+"'");
1915 // Checks whether we are trying to access an instance
1916 // property, method or field from a static body.
1918 Expression MemberStaticCheck (Expression e)
1920 if (e is FieldExpr){
1921 FieldInfo fi = ((FieldExpr) e).FieldInfo;
1924 Error120 (Location, Name);
1927 } else if (e is MethodGroupExpr){
1928 MethodGroupExpr mg = (MethodGroupExpr) e;
1930 if (!mg.RemoveInstanceMethods ()){
1931 Error120 (Location, mg.Methods [0].Name);
1935 } else if (e is PropertyExpr){
1936 if (!((PropertyExpr) e).IsStatic){
1937 Error120 (Location, Name);
1946 // 7.5.2: Simple Names.
1948 // Local Variables and Parameters are handled at
1949 // parse time, so they never occur as SimpleNames.
1951 public override Expression DoResolve (EmitContext ec)
1956 // Stage 1: Performed by the parser (binding to local or parameters).
1960 // Stage 2: Lookup members
1962 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
1965 // Stage 3: Lookup symbol in the various namespaces.
1969 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
1970 return new TypeExpr (t);
1973 // Stage 3 part b: Lookup up if we are an alias to a type
1976 // Since we are cheating: we only do the Alias lookup for
1977 // namespaces if the name does not include any dots in it
1980 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
1981 // using NamespaceExprs (dunno how that fixes the alias
1982 // per-file though).
1984 // No match, maybe our parent can compose us
1985 // into something meaningful.
1990 // Step 2, continues here.
1994 if (e is FieldExpr){
1995 FieldExpr fe = (FieldExpr) e;
1997 if (!fe.FieldInfo.IsStatic)
1998 fe.InstanceExpression = new This (Location.Null);
2002 return MemberStaticCheck (e);
2007 public override void Emit (EmitContext ec)
2010 // If this is ever reached, then we failed to
2011 // find the name as a namespace
2014 Error (103, Location, "The name `" + Name +
2015 "' does not exist in the class `" +
2016 ec.TypeContainer.Name + "'");
2021 // Fully resolved expression that evaluates to a type
2023 public class TypeExpr : Expression {
2024 public TypeExpr (Type t)
2027 eclass = ExprClass.Type;
2030 override public Expression DoResolve (EmitContext ec)
2035 override public void Emit (EmitContext ec)
2037 throw new Exception ("Implement me");
2042 // MethodGroup Expression.
2044 // This is a fully resolved expression that evaluates to a type
2046 public class MethodGroupExpr : Expression {
2047 public MethodBase [] Methods;
2048 Expression instance_expression = null;
2050 public MethodGroupExpr (MemberInfo [] mi)
2052 Methods = new MethodBase [mi.Length];
2053 mi.CopyTo (Methods, 0);
2054 eclass = ExprClass.MethodGroup;
2057 public MethodGroupExpr (ArrayList l)
2059 Methods = new MethodBase [l.Count];
2061 l.CopyTo (Methods, 0);
2062 eclass = ExprClass.MethodGroup;
2066 // `A method group may have associated an instance expression'
2068 public Expression InstanceExpression {
2070 return instance_expression;
2074 instance_expression = value;
2078 override public Expression DoResolve (EmitContext ec)
2083 override public void Emit (EmitContext ec)
2085 throw new Exception ("This should never be reached");
2088 bool RemoveMethods (bool keep_static)
2090 ArrayList smethods = new ArrayList ();
2091 int top = Methods.Length;
2094 for (i = 0; i < top; i++){
2095 MethodBase mb = Methods [i];
2097 if (mb.IsStatic == keep_static)
2101 if (smethods.Count == 0)
2104 Methods = new MethodBase [smethods.Count];
2105 smethods.CopyTo (Methods, 0);
2111 // Removes any instance methods from the MethodGroup, returns
2112 // false if the resulting set is empty.
2114 public bool RemoveInstanceMethods ()
2116 return RemoveMethods (true);
2120 // Removes any static methods from the MethodGroup, returns
2121 // false if the resulting set is empty.
2123 public bool RemoveStaticMethods ()
2125 return RemoveMethods (false);
2130 // Fully resolved expression that evaluates to a Field
2132 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2133 public readonly FieldInfo FieldInfo;
2134 public Expression InstanceExpression;
2137 public FieldExpr (FieldInfo fi, Location l)
2140 eclass = ExprClass.Variable;
2141 type = fi.FieldType;
2145 override public Expression DoResolve (EmitContext ec)
2147 if (!FieldInfo.IsStatic){
2148 if (InstanceExpression == null){
2149 throw new Exception ("non-static FieldExpr without instance var\n" +
2150 "You have to assign the Instance variable\n" +
2151 "Of the FieldExpr to set this\n");
2154 InstanceExpression = InstanceExpression.Resolve (ec);
2155 if (InstanceExpression == null)
2162 public Expression DoResolveLValue (EmitContext ec)
2164 if (!FieldInfo.IsInitOnly)
2168 // InitOnly fields can only be assigned in constructors
2171 if (ec.IsConstructor)
2174 Report.Error (191, loc,
2175 "Readonly field can not be assigned outside " +
2176 "of constructor or variable initializer");
2181 override public void Emit (EmitContext ec)
2183 ILGenerator ig = ec.ig;
2185 if (FieldInfo.IsStatic)
2186 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2188 InstanceExpression.Emit (ec);
2190 ig.Emit (OpCodes.Ldfld, FieldInfo);
2194 public void EmitAssign (EmitContext ec, Expression source)
2196 bool is_static = FieldInfo.IsStatic;
2199 InstanceExpression.Emit (ec);
2203 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2205 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2209 public void AddressOf (EmitContext ec)
2211 if (FieldInfo.IsStatic)
2212 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2214 InstanceExpression.Emit (ec);
2215 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2221 // Expression that evaluates to a Property. The Assign class
2222 // might set the `Value' expression if we are in an assignment.
2224 // This is not an LValue because we need to re-write the expression, we
2225 // can not take data from the stack and store it.
2227 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2228 public readonly PropertyInfo PropertyInfo;
2229 public readonly bool IsStatic;
2230 MethodInfo [] Accessors;
2233 Expression instance_expr;
2235 public PropertyExpr (PropertyInfo pi, Location l)
2238 eclass = ExprClass.PropertyAccess;
2241 Accessors = TypeManager.GetAccessors (pi);
2243 if (Accessors != null)
2244 for (int i = 0; i < Accessors.Length; i++){
2245 if (Accessors [i] != null)
2246 if (Accessors [i].IsStatic)
2250 Accessors = new MethodInfo [2];
2252 type = pi.PropertyType;
2256 // The instance expression associated with this expression
2258 public Expression InstanceExpression {
2260 instance_expr = value;
2264 return instance_expr;
2268 public bool VerifyAssignable ()
2270 if (!PropertyInfo.CanWrite){
2271 Report.Error (200, loc,
2272 "The property `" + PropertyInfo.Name +
2273 "' can not be assigned to, as it has not set accessor");
2280 override public Expression DoResolve (EmitContext ec)
2282 if (!PropertyInfo.CanRead){
2283 Report.Error (154, loc,
2284 "The property `" + PropertyInfo.Name +
2285 "' can not be used in " +
2286 "this context because it lacks a get accessor");
2293 override public void Emit (EmitContext ec)
2295 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2300 // Implements the IAssignMethod interface for assignments
2302 public void EmitAssign (EmitContext ec, Expression source)
2304 Argument arg = new Argument (source, Argument.AType.Expression);
2305 ArrayList args = new ArrayList ();
2308 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2311 override public void EmitStatement (EmitContext ec)
2314 ec.ig.Emit (OpCodes.Pop);
2319 // Fully resolved expression that evaluates to a Expression
2321 public class EventExpr : Expression {
2322 public readonly EventInfo EventInfo;
2325 public EventExpr (EventInfo ei, Location loc)
2329 eclass = ExprClass.EventAccess;
2332 override public Expression DoResolve (EmitContext ec)
2334 // We are born in resolved state.
2338 override public void Emit (EmitContext ec)
2340 throw new Exception ("Implement me");
2341 // FIXME: Implement.