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 {
40 // An interface provided by expressions that can be used as
41 // LValues and can store the value on the top of the stack on
44 public interface IStackStore {
47 // The Store method should store the contents of the top
48 // of the stack into the storage that is implemented by
49 // the particular implementation of LValue
51 void Store (EmitContext ec);
55 // This interface is implemented by variables
57 public interface IMemoryLocation {
59 // The AddressOf method should generate code that loads
60 // the address of the object and leaves it on the stack
62 void AddressOf (EmitContext ec);
66 // Base class for expressions
68 public abstract class Expression {
69 protected ExprClass eclass;
82 public ExprClass ExprClass {
93 // Utility wrapper routine for Error, just to beautify the code
95 static protected void Error (int error, string s)
97 Report.Error (error, s);
100 static protected void Error (int error, Location loc, string s)
102 Report.Error (error, loc, s);
106 // Utility wrapper routine for Warning, just to beautify the code
108 static protected void Warning (int warning, string s)
110 Report.Warning (warning, s);
114 // Performs semantic analysis on the Expression
118 // The Resolve method is invoked to perform the semantic analysis
121 // The return value is an expression (it can be the
122 // same expression in some cases) or a new
123 // expression that better represents this node.
125 // For example, optimizations of Unary (LiteralInt)
126 // would return a new LiteralInt with a negated
129 // If there is an error during semantic analysis,
130 // then an error should be reported (using Report)
131 // and a null value should be returned.
133 // There are two side effects expected from calling
134 // Resolve(): the the field variable "eclass" should
135 // be set to any value of the enumeration
136 // `ExprClass' and the type variable should be set
137 // to a valid type (this is the type of the
141 public abstract Expression DoResolve (EmitContext ec);
143 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
145 return DoResolve (ec);
149 // Currently Resolve wraps DoResolve to perform sanity
150 // checking and assertion checking on what we expect from Resolve
152 public Expression Resolve (EmitContext ec)
154 Console.WriteLine ("Resolving: " + this);
156 Expression e = DoResolve (ec);
159 if (e is SimpleName){
160 SimpleName s = (SimpleName) e;
164 "The name `" + s.Name + "' could not be found in `" +
165 ec.TypeContainer.Name + "'");
169 if (e.ExprClass == ExprClass.Invalid)
170 throw new Exception ("Expression " + e +
171 " ExprClass is Invalid after resolve");
173 if (e.ExprClass != ExprClass.MethodGroup)
175 throw new Exception ("Expression " + e +
176 " did not set its type after Resolve");
179 Console.WriteLine ("Result: " + e.GetType ());
184 // Just like `Resolve' above, but this allows SimpleNames to be returned.
185 // This is used by MemberAccess to construct long names that can not be
186 // partially resolved (namespace-qualified names for example).
188 public Expression ResolveWithSimpleName (EmitContext ec)
190 Expression e = DoResolve (ec);
196 if (e.ExprClass == ExprClass.Invalid)
197 throw new Exception ("Expression " + e +
198 " ExprClass is Invalid after resolve");
200 if (e.ExprClass != ExprClass.MethodGroup)
202 throw new Exception ("Expression " + e +
203 " did not set its type after Resolve");
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
247 // The Emit method is invoked to generate the code
248 // for the expression.
251 public abstract void Emit (EmitContext ec);
254 // This method should perform a reduction of the expression. This should
255 // never return null.
257 public virtual Expression Reduce (EmitContext ec)
263 // Protected constructor. Only derivate types should
264 // be able to be created
267 protected Expression ()
269 eclass = ExprClass.Invalid;
274 // Returns a literalized version of a literal FieldInfo
276 public static Expression Literalize (object v, Type t)
278 if (t == TypeManager.int32_type)
279 return new IntLiteral ((int) v);
280 else if (t == TypeManager.uint32_type)
281 return new UIntLiteral ((uint) v);
282 else if (t == TypeManager.int64_type)
283 return new LongLiteral ((long) v);
284 else if (t == TypeManager.uint64_type)
285 return new ULongLiteral ((ulong) v);
286 else if (t == TypeManager.float_type)
287 return new FloatLiteral ((float) v);
288 else if (t == TypeManager.double_type)
289 return new DoubleLiteral ((double) v);
290 else if (t == TypeManager.string_type)
291 return new StringLiteral ((string) v);
292 else if (t == TypeManager.short_type)
293 return new IntLiteral ((int) ((short)v));
294 else if (t == TypeManager.ushort_type)
295 return new IntLiteral ((int) ((ushort)v));
296 else if (t == TypeManager.sbyte_type)
297 return new IntLiteral ((int) ((sbyte)v));
298 else if (t == TypeManager.byte_type)
299 return new IntLiteral ((int) ((byte)v));
300 else if (t == TypeManager.char_type)
301 return new IntLiteral ((int) ((char)v));
303 throw new Exception ("Unknown type for literal (" + t +
308 // Returns a fully formed expression after a MemberLookup
310 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
313 return new EventExpr ((EventInfo) mi, loc);
314 else if (mi is FieldInfo)
315 return new FieldExpr ((FieldInfo) mi, loc);
316 else if (mi is PropertyInfo)
317 return new PropertyExpr ((PropertyInfo) mi, loc);
319 return new TypeExpr ((Type) mi);
325 // FIXME: Probably implement a cache for (t,name,current_access_set)?
327 // FIXME: We need to cope with access permissions here, or this wont
330 // This code could use some optimizations, but we need to do some
331 // measurements. For example, we could use a delegate to `flag' when
332 // something can not any longer be a method-group (because it is something
336 // If the return value is an Array, then it is an array of
339 // If the return value is an MemberInfo, it is anything, but a Method
343 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
344 // the arguments here and have MemberLookup return only the methods that
345 // match the argument count/type, unlike we are doing now (we delay this
348 // This is so we can catch correctly attempts to invoke instance methods
349 // from a static body (scan for error 120 in ResolveSimpleName).
351 public static Expression MemberLookup (EmitContext ec, Type t, string name,
352 bool same_type, MemberTypes mt,
353 BindingFlags bf, Location loc)
356 bf |= BindingFlags.NonPublic;
358 MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
359 t, mt, bf, Type.FilterName, name);
369 if (mi.Length == 1 && !(mi [0] is MethodBase))
370 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
372 for (int i = 0; i < mi.Length; i++)
373 if (!(mi [i] is MethodBase)){
374 Error (-5, "Do not know how to reproduce this case: " +
375 "Methods and non-Method with the same name, " +
376 "report this please");
378 for (i = 0; i < mi.Length; i++){
379 Type tt = mi [i].GetType ();
381 Console.WriteLine (i + ": " + mi [i]);
382 while (tt != TypeManager.object_type){
383 Console.WriteLine (tt);
389 return new MethodGroupExpr (mi);
392 public const MemberTypes AllMemberTypes =
393 MemberTypes.Constructor |
397 MemberTypes.NestedType |
398 MemberTypes.Property;
400 public const BindingFlags AllBindingsFlags =
401 BindingFlags.Public |
402 BindingFlags.Static |
403 BindingFlags.Instance;
405 public static Expression MemberLookup (EmitContext ec, Type t, string name,
406 bool same_type, Location loc)
408 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
411 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
413 Type expr_type = expr.Type;
415 if (target_type == TypeManager.object_type) {
416 if (expr_type.IsClass)
417 return new EmptyCast (expr, target_type);
418 if (expr_type.IsValueType)
419 return new BoxedCast (expr);
420 } else if (expr_type.IsSubclassOf (target_type)) {
421 return new EmptyCast (expr, target_type);
423 // from any class-type S to any interface-type T.
424 if (expr_type.IsClass && target_type.IsInterface) {
426 if (TypeManager.ImplementsInterface (expr_type, target_type))
427 return new EmptyCast (expr, target_type);
432 // from any interface type S to interface-type T.
433 if (expr_type.IsInterface && target_type.IsInterface) {
435 if (TypeManager.ImplementsInterface (expr_type, target_type))
436 return new EmptyCast (expr, target_type);
441 // from an array-type S to an array-type of type T
442 if (expr_type.IsArray && target_type.IsArray) {
443 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
445 Type expr_element_type = expr_type.GetElementType ();
446 Type target_element_type = target_type.GetElementType ();
448 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
449 if (StandardConversionExists (expr_element_type,
450 target_element_type))
451 return new EmptyCast (expr, target_type);
456 // from an array-type to System.Array
457 if (expr_type.IsArray && target_type == TypeManager.array_type)
458 return new EmptyCast (expr, target_type);
460 // from any delegate type to System.Delegate
461 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
462 target_type == TypeManager.delegate_type)
463 return new EmptyCast (expr, target_type);
465 // from any array-type or delegate type into System.ICloneable.
466 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
467 if (target_type == TypeManager.icloneable_type)
468 return new EmptyCast (expr, target_type);
470 // from the null type to any reference-type.
471 if (expr is NullLiteral)
472 return new EmptyCast (expr, target_type);
482 // Handles expressions like this: decimal d; d = 1;
483 // and changes them into: decimal d; d = new System.Decimal (1);
485 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
487 ArrayList args = new ArrayList ();
489 args.Add (new Argument (expr, Argument.AType.Expression));
491 Expression ne = new New (target.FullName, args,
494 return ne.Resolve (ec);
498 // Implicit Numeric Conversions.
500 // expr is the expression to convert, returns a new expression of type
501 // target_type or null if an implicit conversion is not possible.
504 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
505 Type target_type, Location loc)
507 Type expr_type = expr.Type;
510 // Attempt to do the implicit constant expression conversions
512 if (expr is IntLiteral){
515 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
518 } else if (expr is LongLiteral){
520 // Try the implicit constant expression conversion
521 // from long to ulong, instead of a nice routine,
524 if (((LongLiteral) expr).Value > 0)
525 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
528 if (expr_type == TypeManager.sbyte_type){
530 // From sbyte to short, int, long, float, double.
532 if (target_type == TypeManager.int32_type)
533 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
534 if (target_type == TypeManager.int64_type)
535 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
536 if (target_type == TypeManager.double_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
538 if (target_type == TypeManager.float_type)
539 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
540 if (target_type == TypeManager.short_type)
541 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
542 if (target_type == TypeManager.decimal_type)
543 return InternalTypeConstructor (ec, expr, target_type);
544 } else if (expr_type == TypeManager.byte_type){
546 // From byte to short, ushort, int, uint, long, ulong, float, double
548 if ((target_type == TypeManager.short_type) ||
549 (target_type == TypeManager.ushort_type) ||
550 (target_type == TypeManager.int32_type) ||
551 (target_type == TypeManager.uint32_type))
552 return new EmptyCast (expr, target_type);
554 if (target_type == TypeManager.uint64_type)
555 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
556 if (target_type == TypeManager.int64_type)
557 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
559 if (target_type == TypeManager.float_type)
560 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
561 if (target_type == TypeManager.double_type)
562 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
563 if (target_type == TypeManager.decimal_type)
564 return InternalTypeConstructor (ec, expr, target_type);
565 } else if (expr_type == TypeManager.short_type){
567 // From short to int, long, float, double
569 if (target_type == TypeManager.int32_type)
570 return new EmptyCast (expr, target_type);
571 if (target_type == TypeManager.int64_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
573 if (target_type == TypeManager.double_type)
574 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
575 if (target_type == TypeManager.float_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
577 if (target_type == TypeManager.decimal_type)
578 return InternalTypeConstructor (ec, expr, target_type);
579 } else if (expr_type == TypeManager.ushort_type){
581 // From ushort to int, uint, long, ulong, float, double
583 if (target_type == TypeManager.uint32_type)
584 return new EmptyCast (expr, target_type);
586 if (target_type == TypeManager.uint64_type)
587 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
588 if (target_type == TypeManager.int32_type)
589 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
590 if (target_type == TypeManager.int64_type)
591 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
592 if (target_type == TypeManager.double_type)
593 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
594 if (target_type == TypeManager.float_type)
595 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
596 if (target_type == TypeManager.decimal_type)
597 return InternalTypeConstructor (ec, expr, target_type);
598 } else if (expr_type == TypeManager.int32_type){
600 // From int to long, float, double
602 if (target_type == TypeManager.int64_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
604 if (target_type == TypeManager.double_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
606 if (target_type == TypeManager.float_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
608 if (target_type == TypeManager.decimal_type)
609 return InternalTypeConstructor (ec, expr, target_type);
610 } else if (expr_type == TypeManager.uint32_type){
612 // From uint to long, ulong, float, double
614 if (target_type == TypeManager.int64_type)
615 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
616 if (target_type == TypeManager.uint64_type)
617 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
618 if (target_type == TypeManager.double_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
621 if (target_type == TypeManager.float_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
624 if (target_type == TypeManager.decimal_type)
625 return InternalTypeConstructor (ec, expr, target_type);
626 } else if ((expr_type == TypeManager.uint64_type) ||
627 (expr_type == TypeManager.int64_type)){
629 // From long/ulong to float, double
631 if (target_type == TypeManager.double_type)
632 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
634 if (target_type == TypeManager.float_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
637 if (target_type == TypeManager.decimal_type)
638 return InternalTypeConstructor (ec, expr, target_type);
639 } else if (expr_type == TypeManager.char_type){
641 // From char to ushort, int, uint, long, ulong, float, double
643 if ((target_type == TypeManager.ushort_type) ||
644 (target_type == TypeManager.int32_type) ||
645 (target_type == TypeManager.uint32_type))
646 return new EmptyCast (expr, target_type);
647 if (target_type == TypeManager.uint64_type)
648 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
649 if (target_type == TypeManager.int64_type)
650 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
651 if (target_type == TypeManager.float_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
653 if (target_type == TypeManager.double_type)
654 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
655 if (target_type == TypeManager.decimal_type)
656 return InternalTypeConstructor (ec, expr, target_type);
657 } else if (expr_type == TypeManager.float_type){
661 if (target_type == TypeManager.double_type)
662 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
669 // Determines if a standard implicit conversion exists from
670 // expr_type to target_type
672 public static bool StandardConversionExists (Type expr_type, Type target_type)
674 if (expr_type == target_type)
677 // First numeric conversions
679 if (expr_type == TypeManager.sbyte_type){
681 // From sbyte to short, int, long, float, double.
683 if ((target_type == TypeManager.int32_type) ||
684 (target_type == TypeManager.int64_type) ||
685 (target_type == TypeManager.double_type) ||
686 (target_type == TypeManager.float_type) ||
687 (target_type == TypeManager.short_type) ||
688 (target_type == TypeManager.decimal_type))
691 } else if (expr_type == TypeManager.byte_type){
693 // From byte to short, ushort, int, uint, long, ulong, float, double
695 if ((target_type == TypeManager.short_type) ||
696 (target_type == TypeManager.ushort_type) ||
697 (target_type == TypeManager.int32_type) ||
698 (target_type == TypeManager.uint32_type) ||
699 (target_type == TypeManager.uint64_type) ||
700 (target_type == TypeManager.int64_type) ||
701 (target_type == TypeManager.float_type) ||
702 (target_type == TypeManager.double_type) ||
703 (target_type == TypeManager.decimal_type))
706 } else if (expr_type == TypeManager.short_type){
708 // From short to int, long, float, double
710 if ((target_type == TypeManager.int32_type) ||
711 (target_type == TypeManager.int64_type) ||
712 (target_type == TypeManager.double_type) ||
713 (target_type == TypeManager.float_type) ||
714 (target_type == TypeManager.decimal_type))
717 } else if (expr_type == TypeManager.ushort_type){
719 // From ushort to int, uint, long, ulong, float, double
721 if ((target_type == TypeManager.uint32_type) ||
722 (target_type == TypeManager.uint64_type) ||
723 (target_type == TypeManager.int32_type) ||
724 (target_type == TypeManager.int64_type) ||
725 (target_type == TypeManager.double_type) ||
726 (target_type == TypeManager.float_type) ||
727 (target_type == TypeManager.decimal_type))
730 } else if (expr_type == TypeManager.int32_type){
732 // From int to long, float, double
734 if ((target_type == TypeManager.int64_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.uint32_type){
742 // From uint to long, ulong, float, double
744 if ((target_type == TypeManager.int64_type) ||
745 (target_type == TypeManager.uint64_type) ||
746 (target_type == TypeManager.double_type) ||
747 (target_type == TypeManager.float_type) ||
748 (target_type == TypeManager.decimal_type))
751 } else if ((expr_type == TypeManager.uint64_type) ||
752 (expr_type == TypeManager.int64_type)) {
754 // From long/ulong to float, double
756 if ((target_type == TypeManager.double_type) ||
757 (target_type == TypeManager.float_type) ||
758 (target_type == TypeManager.decimal_type))
761 } else if (expr_type == TypeManager.char_type){
763 // From char to ushort, int, uint, long, ulong, float, double
765 if ((target_type == TypeManager.ushort_type) ||
766 (target_type == TypeManager.int32_type) ||
767 (target_type == TypeManager.uint32_type) ||
768 (target_type == TypeManager.uint64_type) ||
769 (target_type == TypeManager.int64_type) ||
770 (target_type == TypeManager.float_type) ||
771 (target_type == TypeManager.double_type) ||
772 (target_type == TypeManager.decimal_type))
775 } else if (expr_type == TypeManager.float_type){
779 if (target_type == TypeManager.double_type)
783 // Next reference conversions
785 if (target_type == TypeManager.object_type) {
786 if ((expr_type.IsClass) ||
787 (expr_type.IsValueType))
790 } else if (expr_type.IsSubclassOf (target_type)) {
794 // from any class-type S to any interface-type T.
795 if (expr_type.IsClass && target_type.IsInterface)
798 // from any interface type S to interface-type T.
799 // FIXME : Is it right to use IsAssignableFrom ?
800 if (expr_type.IsInterface && target_type.IsInterface)
801 if (target_type.IsAssignableFrom (expr_type))
804 // from an array-type S to an array-type of type T
805 if (expr_type.IsArray && target_type.IsArray) {
806 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
808 Type expr_element_type = expr_type.GetElementType ();
809 Type target_element_type = target_type.GetElementType ();
811 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
812 if (StandardConversionExists (expr_element_type,
813 target_element_type))
818 // from an array-type to System.Array
819 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
822 // from any delegate type to System.Delegate
823 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
824 target_type == TypeManager.delegate_type)
825 if (target_type.IsAssignableFrom (expr_type))
828 // from any array-type or delegate type into System.ICloneable.
829 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
830 if (target_type == TypeManager.icloneable_type)
833 // from the null type to any reference-type.
834 // FIXME : How do we do this ?
842 // Finds "most encompassed type" according to the spec (13.4.2)
843 // amongst the methods in the MethodGroupExpr which convert from a
844 // type encompassing source_type
846 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
850 for (int i = me.Methods.Length; i > 0; ) {
853 MethodBase mb = me.Methods [i];
854 ParameterData pd = Invocation.GetParameterData (mb);
855 Type param_type = pd.ParameterType (0);
857 if (StandardConversionExists (source_type, param_type)) {
861 if (StandardConversionExists (param_type, best))
870 // Finds "most encompassing type" according to the spec (13.4.2)
871 // amongst the methods in the MethodGroupExpr which convert to a
872 // type encompassed by target_type
874 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
878 for (int i = me.Methods.Length; i > 0; ) {
881 MethodInfo mi = (MethodInfo) me.Methods [i];
882 Type ret_type = mi.ReturnType;
884 if (StandardConversionExists (ret_type, target)) {
888 if (!StandardConversionExists (ret_type, best))
900 // User-defined Implicit conversions
902 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
903 Type target, Location loc)
905 return UserDefinedConversion (ec, source, target, loc, false);
909 // User-defined Explicit conversions
911 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
912 Type target, Location loc)
914 return UserDefinedConversion (ec, source, target, loc, true);
918 // User-defined conversions
920 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
921 Type target, Location loc,
922 bool look_for_explicit)
924 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
925 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
927 MethodBase method = null;
928 Type source_type = source.Type;
932 // If we have a boolean type, we need to check for the True operator
934 // FIXME : How does the False operator come into the picture ?
935 // FIXME : This doesn't look complete and very correct !
936 if (target == TypeManager.bool_type)
939 op_name = "op_Implicit";
941 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
943 if (source_type.BaseType != null)
944 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
946 mg3 = MemberLookup (ec, target, op_name, false, loc);
948 if (target.BaseType != null)
949 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
951 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
952 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
954 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
956 MethodGroupExpr union4 = null;
958 if (look_for_explicit) {
960 op_name = "op_Explicit";
962 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
964 if (source_type.BaseType != null)
965 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
967 mg7 = MemberLookup (ec, target, op_name, false, loc);
969 if (target.BaseType != null)
970 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
972 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
973 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
975 union4 = Invocation.MakeUnionSet (union5, union6);
978 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
982 Type most_specific_source, most_specific_target;
984 most_specific_source = FindMostEncompassedType (union, source_type);
985 if (most_specific_source == null)
988 most_specific_target = FindMostEncompassingType (union, target);
989 if (most_specific_target == null)
994 for (int i = union.Methods.Length; i > 0;) {
997 MethodBase mb = union.Methods [i];
998 ParameterData pd = Invocation.GetParameterData (mb);
999 MethodInfo mi = (MethodInfo) union.Methods [i];
1001 if (pd.ParameterType (0) == most_specific_source &&
1002 mi.ReturnType == most_specific_target) {
1008 if (method == null || count > 1) {
1009 Report.Error (-11, loc, "Ambiguous user defined conversion");
1014 // This will do the conversion to the best match that we
1015 // found. Now we need to perform an implict standard conversion
1016 // if the best match was not the type that we were requested
1019 if (look_for_explicit)
1020 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1022 source = ConvertImplicitStandard (ec, source,
1023 most_specific_source, loc);
1028 e = new UserCast ((MethodInfo) method, source);
1030 if (e.Type != target){
1031 if (!look_for_explicit)
1032 e = ConvertImplicitStandard (ec, e, target, loc);
1034 e = ConvertExplicitStandard (ec, e, target, loc);
1045 // Converts implicitly the resolved expression `expr' into the
1046 // `target_type'. It returns a new expression that can be used
1047 // in a context that expects a `target_type'.
1049 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1050 Type target_type, Location loc)
1052 Type expr_type = expr.Type;
1055 if (expr_type == target_type)
1058 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1062 e = ImplicitReferenceConversion (expr, target_type);
1066 e = ImplicitUserConversion (ec, expr, target_type, loc);
1070 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1071 IntLiteral i = (IntLiteral) expr;
1074 return new EmptyCast (expr, target_type);
1082 // Attempts to apply the `Standard Implicit
1083 // Conversion' rules to the expression `expr' into
1084 // the `target_type'. It returns a new expression
1085 // that can be used in a context that expects a
1088 // This is different from `ConvertImplicit' in that the
1089 // user defined implicit conversions are excluded.
1091 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1092 Type target_type, Location loc)
1094 Type expr_type = expr.Type;
1097 if (expr_type == target_type)
1100 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1104 e = ImplicitReferenceConversion (expr, target_type);
1108 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1109 IntLiteral i = (IntLiteral) expr;
1112 return new EmptyCast (expr, target_type);
1117 // Attemps to perform an implict constant conversion of the IntLiteral
1118 // into a different data type using casts (See Implicit Constant
1119 // Expression Conversions)
1121 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1123 int value = il.Value;
1125 if (target_type == TypeManager.sbyte_type){
1126 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1128 } else if (target_type == TypeManager.byte_type){
1129 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1131 } else if (target_type == TypeManager.short_type){
1132 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1134 } else if (target_type == TypeManager.ushort_type){
1135 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1137 } else if (target_type == TypeManager.uint32_type){
1139 // we can optimize this case: a positive int32
1140 // always fits on a uint32
1144 } else if (target_type == TypeManager.uint64_type){
1146 // we can optimize this case: a positive int32
1147 // always fits on a uint64. But we need an opcode
1151 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1158 // Attemptes to implicityly convert `target' into `type', using
1159 // ConvertImplicit. If there is no implicit conversion, then
1160 // an error is signaled
1162 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1163 Type type, Location loc)
1167 e = ConvertImplicit (ec, target, type, loc);
1171 string msg = "Can not convert implicitly from `"+
1172 TypeManager.CSharpName (target.Type) + "' to `" +
1173 TypeManager.CSharpName (type) + "'";
1175 Error (29, loc, msg);
1181 // Performs the explicit numeric conversions
1183 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1186 Type expr_type = expr.Type;
1188 if (expr_type == TypeManager.sbyte_type){
1190 // From sbyte to byte, ushort, uint, ulong, char
1192 if (target_type == TypeManager.byte_type)
1193 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1194 if (target_type == TypeManager.ushort_type)
1195 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1196 if (target_type == TypeManager.uint32_type)
1197 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1198 if (target_type == TypeManager.uint64_type)
1199 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1200 if (target_type == TypeManager.char_type)
1201 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1202 } else if (expr_type == TypeManager.byte_type){
1204 // From byte to sbyte and char
1206 if (target_type == TypeManager.sbyte_type)
1207 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1208 if (target_type == TypeManager.char_type)
1209 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1210 } else if (expr_type == TypeManager.short_type){
1212 // From short to sbyte, byte, ushort, uint, ulong, char
1214 if (target_type == TypeManager.sbyte_type)
1215 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1216 if (target_type == TypeManager.byte_type)
1217 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1218 if (target_type == TypeManager.ushort_type)
1219 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1220 if (target_type == TypeManager.uint32_type)
1221 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1222 if (target_type == TypeManager.uint64_type)
1223 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1224 if (target_type == TypeManager.char_type)
1225 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1226 } else if (expr_type == TypeManager.ushort_type){
1228 // From ushort to sbyte, byte, short, char
1230 if (target_type == TypeManager.sbyte_type)
1231 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1232 if (target_type == TypeManager.byte_type)
1233 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1234 if (target_type == TypeManager.short_type)
1235 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1236 if (target_type == TypeManager.char_type)
1237 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1238 } else if (expr_type == TypeManager.int32_type){
1240 // From int to sbyte, byte, short, ushort, uint, ulong, char
1242 if (target_type == TypeManager.sbyte_type)
1243 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1244 if (target_type == TypeManager.byte_type)
1245 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1246 if (target_type == TypeManager.short_type)
1247 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1248 if (target_type == TypeManager.ushort_type)
1249 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1250 if (target_type == TypeManager.uint32_type)
1251 return new EmptyCast (expr, target_type);
1252 if (target_type == TypeManager.uint64_type)
1253 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1254 if (target_type == TypeManager.char_type)
1255 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1256 } else if (expr_type == TypeManager.uint32_type){
1258 // From uint to sbyte, byte, short, ushort, int, char
1260 if (target_type == TypeManager.sbyte_type)
1261 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1262 if (target_type == TypeManager.byte_type)
1263 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1264 if (target_type == TypeManager.short_type)
1265 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1266 if (target_type == TypeManager.ushort_type)
1267 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1268 if (target_type == TypeManager.int32_type)
1269 return new EmptyCast (expr, target_type);
1270 if (target_type == TypeManager.char_type)
1271 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1272 } else if (expr_type == TypeManager.int64_type){
1274 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1276 if (target_type == TypeManager.sbyte_type)
1277 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1278 if (target_type == TypeManager.byte_type)
1279 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1280 if (target_type == TypeManager.short_type)
1281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1282 if (target_type == TypeManager.ushort_type)
1283 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1284 if (target_type == TypeManager.int32_type)
1285 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1286 if (target_type == TypeManager.uint32_type)
1287 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1288 if (target_type == TypeManager.uint64_type)
1289 return new EmptyCast (expr, target_type);
1290 if (target_type == TypeManager.char_type)
1291 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1292 } else if (expr_type == TypeManager.uint64_type){
1294 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1296 if (target_type == TypeManager.sbyte_type)
1297 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1298 if (target_type == TypeManager.byte_type)
1299 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1300 if (target_type == TypeManager.short_type)
1301 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1302 if (target_type == TypeManager.ushort_type)
1303 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1304 if (target_type == TypeManager.int32_type)
1305 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1306 if (target_type == TypeManager.uint32_type)
1307 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1308 if (target_type == TypeManager.int64_type)
1309 return new EmptyCast (expr, target_type);
1310 if (target_type == TypeManager.char_type)
1311 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1312 } else if (expr_type == TypeManager.char_type){
1314 // From char to sbyte, byte, short
1316 if (target_type == TypeManager.sbyte_type)
1317 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1318 if (target_type == TypeManager.byte_type)
1319 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1320 if (target_type == TypeManager.short_type)
1321 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1322 } else if (expr_type == TypeManager.float_type){
1324 // From float to sbyte, byte, short,
1325 // ushort, int, uint, long, ulong, char
1328 if (target_type == TypeManager.sbyte_type)
1329 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1330 if (target_type == TypeManager.byte_type)
1331 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1332 if (target_type == TypeManager.short_type)
1333 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1334 if (target_type == TypeManager.ushort_type)
1335 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1336 if (target_type == TypeManager.int32_type)
1337 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1338 if (target_type == TypeManager.uint32_type)
1339 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1340 if (target_type == TypeManager.int64_type)
1341 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1342 if (target_type == TypeManager.uint64_type)
1343 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1344 if (target_type == TypeManager.char_type)
1345 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1346 if (target_type == TypeManager.decimal_type)
1347 return InternalTypeConstructor (ec, expr, target_type);
1348 } else if (expr_type == TypeManager.double_type){
1350 // From double to byte, byte, short,
1351 // ushort, int, uint, long, ulong,
1352 // char, float or decimal
1354 if (target_type == TypeManager.sbyte_type)
1355 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1356 if (target_type == TypeManager.byte_type)
1357 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1358 if (target_type == TypeManager.short_type)
1359 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1360 if (target_type == TypeManager.ushort_type)
1361 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1362 if (target_type == TypeManager.int32_type)
1363 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1364 if (target_type == TypeManager.uint32_type)
1365 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1366 if (target_type == TypeManager.int64_type)
1367 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1368 if (target_type == TypeManager.uint64_type)
1369 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1370 if (target_type == TypeManager.char_type)
1371 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1372 if (target_type == TypeManager.float_type)
1373 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1374 if (target_type == TypeManager.decimal_type)
1375 return InternalTypeConstructor (ec, expr, target_type);
1378 // decimal is taken care of by the op_Explicit methods.
1384 // Returns whether an explicit reference conversion can be performed
1385 // from source_type to target_type
1387 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1389 bool target_is_value_type = target_type.IsValueType;
1391 if (source_type == target_type)
1395 // From object to any reference type
1397 if (source_type == TypeManager.object_type && !target_is_value_type)
1401 // From any class S to any class-type T, provided S is a base class of T
1403 if (target_type.IsSubclassOf (source_type))
1407 // From any interface type S to any interface T provided S is not derived from T
1409 if (source_type.IsInterface && target_type.IsInterface){
1410 if (!target_type.IsSubclassOf (source_type))
1415 // From any class type S to any interface T, provides S is not sealed
1416 // and provided S does not implement T.
1418 if (target_type.IsInterface && !source_type.IsSealed &&
1419 !target_type.IsAssignableFrom (source_type))
1423 // From any interface-type S to to any class type T, provided T is not
1424 // sealed, or provided T implements S.
1426 if (source_type.IsInterface &&
1427 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1430 // From an array type S with an element type Se to an array type T with an
1431 // element type Te provided all the following are true:
1432 // * S and T differe only in element type, in other words, S and T
1433 // have the same number of dimensions.
1434 // * Both Se and Te are reference types
1435 // * An explicit referenc conversions exist from Se to Te
1437 if (source_type.IsArray && target_type.IsArray) {
1438 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1440 Type source_element_type = source_type.GetElementType ();
1441 Type target_element_type = target_type.GetElementType ();
1443 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1444 if (ExplicitReferenceConversionExists (source_element_type,
1445 target_element_type))
1451 // From System.Array to any array-type
1452 if (source_type == TypeManager.array_type &&
1453 target_type.IsSubclassOf (TypeManager.array_type)){
1458 // From System delegate to any delegate-type
1460 if (source_type == TypeManager.delegate_type &&
1461 target_type.IsSubclassOf (TypeManager.delegate_type))
1465 // From ICloneable to Array or Delegate types
1467 if (source_type == TypeManager.icloneable_type &&
1468 (target_type == TypeManager.array_type ||
1469 target_type == TypeManager.delegate_type))
1476 // Implements Explicit Reference conversions
1478 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1480 Type source_type = source.Type;
1481 bool target_is_value_type = target_type.IsValueType;
1484 // From object to any reference type
1486 if (source_type == TypeManager.object_type && !target_is_value_type)
1487 return new ClassCast (source, target_type);
1491 // From any class S to any class-type T, provided S is a base class of T
1493 if (target_type.IsSubclassOf (source_type))
1494 return new ClassCast (source, target_type);
1497 // From any interface type S to any interface T provided S is not derived from T
1499 if (source_type.IsInterface && target_type.IsInterface){
1501 Type [] ifaces = source_type.GetInterfaces ();
1503 if (TypeManager.ImplementsInterface (source_type, target_type))
1506 return new ClassCast (source, target_type);
1510 // From any class type S to any interface T, provides S is not sealed
1511 // and provided S does not implement T.
1513 if (target_type.IsInterface && !source_type.IsSealed) {
1515 if (TypeManager.ImplementsInterface (source_type, target_type))
1518 return new ClassCast (source, target_type);
1523 // From any interface-type S to to any class type T, provided T is not
1524 // sealed, or provided T implements S.
1526 if (source_type.IsInterface) {
1528 if (target_type.IsSealed)
1531 if (TypeManager.ImplementsInterface (target_type, source_type))
1532 return new ClassCast (source, target_type);
1537 // From an array type S with an element type Se to an array type T with an
1538 // element type Te provided all the following are true:
1539 // * S and T differe only in element type, in other words, S and T
1540 // have the same number of dimensions.
1541 // * Both Se and Te are reference types
1542 // * An explicit referenc conversions exist from Se to Te
1544 if (source_type.IsArray && target_type.IsArray) {
1545 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1547 Type source_element_type = source_type.GetElementType ();
1548 Type target_element_type = target_type.GetElementType ();
1550 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1551 if (ExplicitReferenceConversionExists (source_element_type,
1552 target_element_type))
1553 return new ClassCast (source, target_type);
1558 // From System.Array to any array-type
1559 if (source_type == TypeManager.array_type &&
1560 target_type.IsSubclassOf (TypeManager.array_type)){
1561 return new ClassCast (source, target_type);
1565 // From System delegate to any delegate-type
1567 if (source_type == TypeManager.delegate_type &&
1568 target_type.IsSubclassOf (TypeManager.delegate_type))
1569 return new ClassCast (source, target_type);
1572 // From ICloneable to Array or Delegate types
1574 if (source_type == TypeManager.icloneable_type &&
1575 (target_type == TypeManager.array_type ||
1576 target_type == TypeManager.delegate_type))
1577 return new ClassCast (source, target_type);
1583 // Performs an explicit conversion of the expression `expr' whose
1584 // type is expr.Type to `target_type'.
1586 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1587 Type target_type, Location loc)
1589 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1594 ne = ConvertNumericExplicit (ec, expr, target_type);
1598 ne = ConvertReferenceExplicit (expr, target_type);
1602 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1606 Report.Error (30, loc, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1607 + TypeManager.CSharpName (target_type) + "'");
1612 // Same as ConverExplicit, only it doesn't include user defined conversions
1614 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1615 Type target_type, Location l)
1617 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1622 ne = ConvertNumericExplicit (ec, expr, target_type);
1626 ne = ConvertReferenceExplicit (expr, target_type);
1630 Report.Error (30, l, "Cannot convert type '" +
1631 TypeManager.CSharpName (expr.Type) + "' to '" +
1632 TypeManager.CSharpName (target_type) + "'");
1636 static string ExprClassName (ExprClass c)
1639 case ExprClass.Invalid:
1641 case ExprClass.Value:
1643 case ExprClass.Variable:
1645 case ExprClass.Namespace:
1647 case ExprClass.Type:
1649 case ExprClass.MethodGroup:
1650 return "method group";
1651 case ExprClass.PropertyAccess:
1652 return "property access";
1653 case ExprClass.EventAccess:
1654 return "event access";
1655 case ExprClass.IndexerAccess:
1656 return "indexer access";
1657 case ExprClass.Nothing:
1660 throw new Exception ("Should not happen");
1664 // Reports that we were expecting `expr' to be of class `expected'
1666 protected void report118 (Location loc, Expression expr, string expected)
1668 string kind = "Unknown";
1671 kind = ExprClassName (expr.ExprClass);
1673 Error (118, loc, "Expression denotes a '" + kind +
1674 "' where an " + expected + " was expected");
1678 // This function tries to reduce the expression performing
1679 // constant folding and common subexpression elimination
1681 static public Expression Reduce (EmitContext ec, Expression e)
1683 //Console.WriteLine ("Calling reduce");
1684 return e.Reduce (ec);
1689 // This is just a base class for expressions that can
1690 // appear on statements (invocations, object creation,
1691 // assignments, post/pre increment and decrement). The idea
1692 // being that they would support an extra Emition interface that
1693 // does not leave a result on the stack.
1696 public abstract class ExpressionStatement : Expression {
1699 // Requests the expression to be emitted in a `statement'
1700 // context. This means that no new value is left on the
1701 // stack after invoking this method (constrasted with
1702 // Emit that will always leave a value on the stack).
1704 public abstract void EmitStatement (EmitContext ec);
1708 // This kind of cast is used to encapsulate the child
1709 // whose type is child.Type into an expression that is
1710 // reported to return "return_type". This is used to encapsulate
1711 // expressions which have compatible types, but need to be dealt
1712 // at higher levels with.
1714 // For example, a "byte" expression could be encapsulated in one
1715 // of these as an "unsigned int". The type for the expression
1716 // would be "unsigned int".
1720 public class EmptyCast : Expression {
1721 protected Expression child;
1723 public EmptyCast (Expression child, Type return_type)
1725 ExprClass = child.ExprClass;
1730 public override Expression DoResolve (EmitContext ec)
1732 // This should never be invoked, we are born in fully
1733 // initialized state.
1738 public override void Emit (EmitContext ec)
1746 // This class is used to wrap literals which belong inside Enums
1749 public class EnumLiteral : Literal {
1752 public EnumLiteral (Expression child, Type enum_type)
1754 ExprClass = child.ExprClass;
1759 public override Expression DoResolve (EmitContext ec)
1761 // This should never be invoked, we are born in fully
1762 // initialized state.
1767 public override void Emit (EmitContext ec)
1772 public override object GetValue ()
1774 return ((Literal) child).GetValue ();
1777 public override string AsString ()
1779 return ((Literal) child).AsString ();
1784 // This kind of cast is used to encapsulate Value Types in objects.
1786 // The effect of it is to box the value type emitted by the previous
1789 public class BoxedCast : EmptyCast {
1791 public BoxedCast (Expression expr)
1792 : base (expr, TypeManager.object_type)
1796 public override Expression DoResolve (EmitContext ec)
1798 // This should never be invoked, we are born in fully
1799 // initialized state.
1804 public override void Emit (EmitContext ec)
1807 ec.ig.Emit (OpCodes.Box, child.Type);
1812 // This kind of cast is used to encapsulate a child expression
1813 // that can be trivially converted to a target type using one or
1814 // two opcodes. The opcodes are passed as arguments.
1816 public class OpcodeCast : EmptyCast {
1820 public OpcodeCast (Expression child, Type return_type, OpCode op)
1821 : base (child, return_type)
1825 second_valid = false;
1828 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1829 : base (child, return_type)
1834 second_valid = true;
1837 public override Expression DoResolve (EmitContext ec)
1839 // This should never be invoked, we are born in fully
1840 // initialized state.
1845 public override void Emit (EmitContext ec)
1857 // This kind of cast is used to encapsulate a child and cast it
1858 // to the class requested
1860 public class ClassCast : EmptyCast {
1861 public ClassCast (Expression child, Type return_type)
1862 : base (child, return_type)
1867 public override Expression DoResolve (EmitContext ec)
1869 // This should never be invoked, we are born in fully
1870 // initialized state.
1875 public override void Emit (EmitContext ec)
1879 ec.ig.Emit (OpCodes.Castclass, type);
1885 // SimpleName expressions are initially formed of a single
1886 // word and it only happens at the beginning of the expression.
1888 // The expression will try to be bound to a Field, a Method
1889 // group or a Property. If those fail we pass the name to our
1890 // caller and the SimpleName is compounded to perform a type
1891 // lookup. The idea behind this process is that we want to avoid
1892 // creating a namespace map from the assemblies, as that requires
1893 // the GetExportedTypes function to be called and a hashtable to
1894 // be constructed which reduces startup time. If later we find
1895 // that this is slower, we should create a `NamespaceExpr' expression
1896 // that fully participates in the resolution process.
1898 // For example `System.Console.WriteLine' is decomposed into
1899 // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1901 // The first SimpleName wont produce a match on its own, so it will
1903 // MemberAccess (SimpleName ("System.Console"), "WriteLine").
1905 // System.Console will produce a TypeExpr match.
1907 // The downside of this is that we might be hitting `LookupType' too many
1908 // times with this scheme.
1910 public class SimpleName : Expression {
1911 public readonly string Name;
1912 public readonly Location Location;
1914 public SimpleName (string name, Location l)
1920 public static void Error120 (Location l, string name)
1924 "An object reference is required " +
1925 "for the non-static field `"+name+"'");
1929 // Checks whether we are trying to access an instance
1930 // property, method or field from a static body.
1932 Expression MemberStaticCheck (Expression e)
1934 if (e is FieldExpr){
1935 FieldInfo fi = ((FieldExpr) e).FieldInfo;
1938 Error120 (Location, Name);
1941 } else if (e is MethodGroupExpr){
1942 MethodGroupExpr mg = (MethodGroupExpr) e;
1944 if (!mg.RemoveInstanceMethods ()){
1945 Error120 (Location, mg.Methods [0].Name);
1949 } else if (e is PropertyExpr){
1950 if (!((PropertyExpr) e).IsStatic){
1951 Error120 (Location, Name);
1960 // 7.5.2: Simple Names.
1962 // Local Variables and Parameters are handled at
1963 // parse time, so they never occur as SimpleNames.
1965 public override Expression DoResolve (EmitContext ec)
1970 // Stage 1: Performed by the parser (binding to local or parameters).
1974 // Stage 2: Lookup members
1976 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
1979 // Stage 3: Lookup symbol in the various namespaces.
1983 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
1984 return new TypeExpr (t);
1987 // Stage 3 part b: Lookup up if we are an alias to a type
1990 // Since we are cheating: we only do the Alias lookup for
1991 // namespaces if the name does not include any dots in it
1994 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
1995 // using NamespaceExprs (dunno how that fixes the alias
1996 // per-file though).
1998 // No match, maybe our parent can compose us
1999 // into something meaningful.
2004 // Step 2, continues here.
2008 if (e is FieldExpr){
2009 FieldExpr fe = (FieldExpr) e;
2011 if (!fe.FieldInfo.IsStatic)
2012 fe.InstanceExpression = new This (Location.Null);
2016 return MemberStaticCheck (e);
2021 public override void Emit (EmitContext ec)
2024 // If this is ever reached, then we failed to
2025 // find the name as a namespace
2028 Error (103, Location, "The name `" + Name +
2029 "' does not exist in the class `" +
2030 ec.TypeContainer.Name + "'");
2035 // Fully resolved expression that evaluates to a type
2037 public class TypeExpr : Expression {
2038 public TypeExpr (Type t)
2041 eclass = ExprClass.Type;
2044 override public Expression DoResolve (EmitContext ec)
2049 override public void Emit (EmitContext ec)
2051 throw new Exception ("Implement me");
2056 // MethodGroup Expression.
2058 // This is a fully resolved expression that evaluates to a type
2060 public class MethodGroupExpr : Expression {
2061 public MethodBase [] Methods;
2062 Expression instance_expression = null;
2064 public MethodGroupExpr (MemberInfo [] mi)
2066 Methods = new MethodBase [mi.Length];
2067 mi.CopyTo (Methods, 0);
2068 eclass = ExprClass.MethodGroup;
2071 public MethodGroupExpr (ArrayList l)
2073 Methods = new MethodBase [l.Count];
2075 l.CopyTo (Methods, 0);
2076 eclass = ExprClass.MethodGroup;
2080 // `A method group may have associated an instance expression'
2082 public Expression InstanceExpression {
2084 return instance_expression;
2088 instance_expression = value;
2092 override public Expression DoResolve (EmitContext ec)
2097 override public void Emit (EmitContext ec)
2099 throw new Exception ("This should never be reached");
2102 bool RemoveMethods (bool keep_static)
2104 ArrayList smethods = new ArrayList ();
2105 int top = Methods.Length;
2108 for (i = 0; i < top; i++){
2109 MethodBase mb = Methods [i];
2111 if (mb.IsStatic == keep_static)
2115 if (smethods.Count == 0)
2118 Methods = new MethodBase [smethods.Count];
2119 smethods.CopyTo (Methods, 0);
2125 // Removes any instance methods from the MethodGroup, returns
2126 // false if the resulting set is empty.
2128 public bool RemoveInstanceMethods ()
2130 return RemoveMethods (true);
2134 // Removes any static methods from the MethodGroup, returns
2135 // false if the resulting set is empty.
2137 public bool RemoveStaticMethods ()
2139 return RemoveMethods (false);
2144 // Fully resolved expression that evaluates to a Field
2146 public class FieldExpr : Expression, IStackStore, IMemoryLocation {
2147 public readonly FieldInfo FieldInfo;
2148 public Expression InstanceExpression;
2151 public FieldExpr (FieldInfo fi, Location l)
2154 eclass = ExprClass.Variable;
2155 type = fi.FieldType;
2159 override public Expression DoResolve (EmitContext ec)
2161 if (!FieldInfo.IsStatic){
2162 if (InstanceExpression == null){
2163 throw new Exception ("non-static FieldExpr without instance var\n" +
2164 "You have to assign the Instance variable\n" +
2165 "Of the FieldExpr to set this\n");
2168 InstanceExpression = InstanceExpression.Resolve (ec);
2169 if (InstanceExpression == null)
2176 public Expression DoResolveLValue (EmitContext ec)
2178 if (!FieldInfo.IsInitOnly)
2182 // InitOnly fields can only be assigned in constructors
2185 if (ec.IsConstructor)
2188 Report.Error (191, loc,
2189 "Readonly field can not be assigned outside " +
2190 "of constructor or variable initializer");
2195 override public void Emit (EmitContext ec)
2197 ILGenerator ig = ec.ig;
2199 if (FieldInfo.IsStatic)
2200 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2202 InstanceExpression.Emit (ec);
2204 ig.Emit (OpCodes.Ldfld, FieldInfo);
2208 public void Store (EmitContext ec)
2210 if (FieldInfo.IsStatic)
2211 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2213 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2216 public void AddressOf (EmitContext ec)
2218 if (FieldInfo.IsStatic)
2219 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2221 InstanceExpression.Emit (ec);
2222 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2228 // Expression that evaluates to a Property. The Assign class
2229 // might set the `Value' expression if we are in an assignment.
2231 // This is not an LValue because we need to re-write the expression, we
2232 // can not take data from the stack and store it.
2234 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2235 public readonly PropertyInfo PropertyInfo;
2236 public readonly bool IsStatic;
2237 MethodInfo [] Accessors;
2240 Expression instance_expr;
2242 public PropertyExpr (PropertyInfo pi, Location l)
2245 eclass = ExprClass.PropertyAccess;
2248 Accessors = TypeManager.GetAccessors (pi);
2250 if (Accessors != null)
2251 for (int i = 0; i < Accessors.Length; i++){
2252 if (Accessors [i] != null)
2253 if (Accessors [i].IsStatic)
2257 Accessors = new MethodInfo [2];
2259 type = pi.PropertyType;
2263 // The instance expression associated with this expression
2265 public Expression InstanceExpression {
2267 instance_expr = value;
2271 return instance_expr;
2275 public bool VerifyAssignable ()
2277 if (!PropertyInfo.CanWrite){
2278 Report.Error (200, loc,
2279 "The property `" + PropertyInfo.Name +
2280 "' can not be assigned to, as it has not set accessor");
2287 override public Expression DoResolve (EmitContext ec)
2289 if (!PropertyInfo.CanRead){
2290 Report.Error (154, loc,
2291 "The property `" + PropertyInfo.Name +
2292 "' can not be used in " +
2293 "this context because it lacks a get accessor");
2300 override public void Emit (EmitContext ec)
2302 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2307 // Implements the IAssignMethod interface for assignments
2309 public void EmitAssign (EmitContext ec, Expression source)
2311 Argument arg = new Argument (source, Argument.AType.Expression);
2312 ArrayList args = new ArrayList ();
2315 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2318 override public void EmitStatement (EmitContext ec)
2321 ec.ig.Emit (OpCodes.Pop);
2326 // Fully resolved expression that evaluates to a Expression
2328 public class EventExpr : Expression {
2329 public readonly EventInfo EventInfo;
2332 public EventExpr (EventInfo ei, Location loc)
2336 eclass = ExprClass.EventAccess;
2339 override public Expression DoResolve (EmitContext ec)
2341 // We are born in resolved state.
2345 override public void Emit (EmitContext ec)
2347 throw new Exception ("Implement me");
2348 // FIXME: Implement.