2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 // This is just as a hint to AddressOf of what will be done with the
43 public enum AddressOp {
50 /// This interface is implemented by variables
52 public interface IMemoryLocation {
54 /// The AddressOf method should generate code that loads
55 /// the address of the object and leaves it on the stack.
57 /// The `mode' argument is used to notify the expression
58 /// of whether this will be used to read from the address or
59 /// write to the address.
61 /// This is just a hint that can be used to provide good error
62 /// reporting, and should have no other side effects.
64 void AddressOf (EmitContext ec, AddressOp mode);
68 /// Base class for expressions
70 public abstract class Expression {
71 public ExprClass eclass;
85 /// Utility wrapper routine for Error, just to beautify the code
87 static protected void Error (int error, string s)
89 Report.Error (error, s);
92 static protected void Error (int error, Location loc, string s)
94 Report.Error (error, loc, s);
98 /// Utility wrapper routine for Warning, just to beautify the code
100 static protected void Warning (int warning, string s)
102 Report.Warning (warning, s);
105 static public void Error_CannotConvertType (Location loc, Type source, Type target)
107 Report.Error (30, loc, "Cannot convert type '" +
108 TypeManager.CSharpName (source) + "' to '" +
109 TypeManager.CSharpName (target) + "'");
113 /// Performs semantic analysis on the Expression
117 /// The Resolve method is invoked to perform the semantic analysis
120 /// The return value is an expression (it can be the
121 /// same expression in some cases) or a new
122 /// expression that better represents this node.
124 /// For example, optimizations of Unary (LiteralInt)
125 /// would return a new LiteralInt with a negated
128 /// If there is an error during semantic analysis,
129 /// then an error should be reported (using Report)
130 /// and a null value should be returned.
132 /// There are two side effects expected from calling
133 /// Resolve(): the the field variable "eclass" should
134 /// be set to any value of the enumeration
135 /// `ExprClass' and the type variable should be set
136 /// to a valid type (this is the type of the
139 public abstract Expression DoResolve (EmitContext ec);
141 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
143 return DoResolve (ec);
147 /// Resolves an expression and performs semantic analysis on it.
151 /// Currently Resolve wraps DoResolve to perform sanity
152 /// checking and assertion checking on what we expect from Resolve.
154 public Expression Resolve (EmitContext ec)
156 Expression e = DoResolve (ec);
160 if (e is SimpleName){
161 SimpleName s = (SimpleName) e;
165 "The name `" + s.Name + "' could not be found in `" +
166 ec.DeclSpace.Name + "'");
170 if (e.eclass == ExprClass.Invalid)
171 throw new Exception ("Expression " + e.GetType () +
172 " ExprClass is Invalid after resolve");
174 if (e.eclass != ExprClass.MethodGroup)
176 throw new Exception (
177 "Expression " + e.GetType () +
178 " did not set its type after Resolve\n" +
179 "called from: " + this.GetType ());
186 /// Performs expression resolution and semantic analysis, but
187 /// allows SimpleNames to be returned.
191 /// This is used by MemberAccess to construct long names that can not be
192 /// partially resolved (namespace-qualified names for example).
194 public Expression ResolveWithSimpleName (EmitContext ec)
198 if (this is SimpleName)
199 e = ((SimpleName) this).DoResolveAllowStatic (ec);
207 if (e.eclass == ExprClass.Invalid)
208 throw new Exception ("Expression " + e +
209 " ExprClass is Invalid after resolve");
211 if (e.eclass != ExprClass.MethodGroup)
213 throw new Exception ("Expression " + e +
214 " did not set its type after Resolve");
221 /// Resolves an expression for LValue assignment
225 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
226 /// checking and assertion checking on what we expect from Resolve
228 public Expression ResolveLValue (EmitContext ec, Expression right_side)
230 Expression e = DoResolveLValue (ec, right_side);
233 if (e is SimpleName){
234 SimpleName s = (SimpleName) e;
238 "The name `" + s.Name + "' could not be found in `" +
239 ec.DeclSpace.Name + "'");
243 if (e.eclass == ExprClass.Invalid)
244 throw new Exception ("Expression " + e +
245 " ExprClass is Invalid after resolve");
247 if (e.eclass != ExprClass.MethodGroup)
249 throw new Exception ("Expression " + e +
250 " did not set its type after Resolve");
257 /// Emits the code for the expression
261 /// The Emit method is invoked to generate the code
262 /// for the expression.
264 public abstract void Emit (EmitContext ec);
267 /// Protected constructor. Only derivate types should
268 /// be able to be created
271 protected Expression ()
273 eclass = ExprClass.Invalid;
278 /// Returns a literalized version of a literal FieldInfo
282 /// The possible return values are:
283 /// IntConstant, UIntConstant
284 /// LongLiteral, ULongConstant
285 /// FloatConstant, DoubleConstant
288 /// The value returned is already resolved.
290 public static Constant Constantify (object v, Type t)
292 if (t == TypeManager.int32_type)
293 return new IntConstant ((int) v);
294 else if (t == TypeManager.uint32_type)
295 return new UIntConstant ((uint) v);
296 else if (t == TypeManager.int64_type)
297 return new LongConstant ((long) v);
298 else if (t == TypeManager.uint64_type)
299 return new ULongConstant ((ulong) v);
300 else if (t == TypeManager.float_type)
301 return new FloatConstant ((float) v);
302 else if (t == TypeManager.double_type)
303 return new DoubleConstant ((double) v);
304 else if (t == TypeManager.string_type)
305 return new StringConstant ((string) v);
306 else if (t == TypeManager.short_type)
307 return new ShortConstant ((short)v);
308 else if (t == TypeManager.ushort_type)
309 return new UShortConstant ((ushort)v);
310 else if (t == TypeManager.sbyte_type)
311 return new SByteConstant (((sbyte)v));
312 else if (t == TypeManager.byte_type)
313 return new ByteConstant ((byte)v);
314 else if (t == TypeManager.char_type)
315 return new CharConstant ((char)v);
316 else if (TypeManager.IsEnumType (t)){
317 Expression e = Constantify (v, v.GetType ());
319 return new EnumConstant ((Constant) e, t);
321 throw new Exception ("Unknown type for constant (" + t +
326 /// Returns a fully formed expression after a MemberLookup
328 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
331 return new EventExpr ((EventInfo) mi, loc);
332 else if (mi is FieldInfo)
333 return new FieldExpr ((FieldInfo) mi, loc);
334 else if (mi is PropertyInfo)
335 return new PropertyExpr ((PropertyInfo) mi, loc);
336 else if (mi is Type){
337 return new TypeExpr ((System.Type) mi);
344 // FIXME: Probably implement a cache for (t,name,current_access_set)?
346 // This code could use some optimizations, but we need to do some
347 // measurements. For example, we could use a delegate to `flag' when
348 // something can not any longer be a method-group (because it is something
352 // If the return value is an Array, then it is an array of
355 // If the return value is an MemberInfo, it is anything, but a Method
359 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
360 // the arguments here and have MemberLookup return only the methods that
361 // match the argument count/type, unlike we are doing now (we delay this
364 // This is so we can catch correctly attempts to invoke instance methods
365 // from a static body (scan for error 120 in ResolveSimpleName).
368 // FIXME: Potential optimization, have a static ArrayList
371 public static Expression MemberLookup (EmitContext ec, Type t, string name,
372 MemberTypes mt, BindingFlags bf, Location loc)
374 MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
379 int count = mi.Length;
382 return new MethodGroupExpr (mi, loc);
384 if (mi [0] is MethodBase)
385 return new MethodGroupExpr (mi, loc);
387 return ExprClassFromMemberInfo (ec, mi [0], loc);
390 public const MemberTypes AllMemberTypes =
391 MemberTypes.Constructor |
395 MemberTypes.NestedType |
396 MemberTypes.Property;
398 public const BindingFlags AllBindingFlags =
399 BindingFlags.Public |
400 BindingFlags.Static |
401 BindingFlags.Instance;
403 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
405 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
408 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
410 return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
414 /// This is a wrapper for MemberLookup that is not used to "probe", but
415 /// to find a final definition. If the final definition is not found, we
416 /// look for private members and display a useful debugging message if we
419 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
424 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
429 e = MemberLookup (ec, t, name, AllMemberTypes,
430 AllBindingFlags | BindingFlags.NonPublic, loc);
433 117, loc, "`" + t + "' does not contain a definition " +
434 "for `" + name + "'");
437 122, loc, "`" + t + "." + name +
438 "' is inaccessible due to its protection level");
444 static EmptyExpression MyEmptyExpr;
445 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
447 Type expr_type = expr.Type;
449 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
450 // if we are a method group, emit a warning
455 if (target_type == TypeManager.object_type) {
457 // A pointer type cannot be converted to object
459 if (expr_type.IsPointer)
462 if (expr_type.IsValueType)
463 return new BoxedCast (expr);
464 if (expr_type.IsClass || expr_type.IsInterface)
465 return new EmptyCast (expr, target_type);
466 } else if (expr_type.IsSubclassOf (target_type)) {
467 return new EmptyCast (expr, target_type);
470 // This code is kind of mirrored inside StandardConversionExists
471 // with the small distinction that we only probe there
473 // Always ensure that the code here and there is in sync
475 // from the null type to any reference-type.
476 if (expr is NullLiteral && !target_type.IsValueType)
477 return new EmptyCast (expr, target_type);
479 // from any class-type S to any interface-type T.
480 if (expr_type.IsClass && target_type.IsInterface) {
481 if (TypeManager.ImplementsInterface (expr_type, target_type))
482 return new EmptyCast (expr, target_type);
487 // from any interface type S to interface-type T.
488 if (expr_type.IsInterface && target_type.IsInterface) {
490 if (TypeManager.ImplementsInterface (expr_type, target_type))
491 return new EmptyCast (expr, target_type);
496 // from an array-type S to an array-type of type T
497 if (expr_type.IsArray && target_type.IsArray) {
498 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
500 Type expr_element_type = expr_type.GetElementType ();
502 if (MyEmptyExpr == null)
503 MyEmptyExpr = new EmptyExpression ();
505 MyEmptyExpr.SetType (expr_element_type);
506 Type target_element_type = target_type.GetElementType ();
508 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
509 if (StandardConversionExists (MyEmptyExpr,
510 target_element_type))
511 return new EmptyCast (expr, target_type);
516 // from an array-type to System.Array
517 if (expr_type.IsArray && target_type == TypeManager.array_type)
518 return new EmptyCast (expr, target_type);
520 // from any delegate type to System.Delegate
521 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
522 target_type == TypeManager.delegate_type)
523 return new EmptyCast (expr, target_type);
525 // from any array-type or delegate type into System.ICloneable.
526 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
527 if (target_type == TypeManager.icloneable_type)
528 return new EmptyCast (expr, target_type);
538 /// Handles expressions like this: decimal d; d = 1;
539 /// and changes them into: decimal d; d = new System.Decimal (1);
541 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
543 ArrayList args = new ArrayList ();
545 args.Add (new Argument (expr, Argument.AType.Expression));
547 Expression ne = new New (target.FullName, args,
550 return ne.Resolve (ec);
554 /// Implicit Numeric Conversions.
556 /// expr is the expression to convert, returns a new expression of type
557 /// target_type or null if an implicit conversion is not possible.
559 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
560 Type target_type, Location loc)
562 Type expr_type = expr.Type;
565 // Attempt to do the implicit constant expression conversions
567 if (expr is IntConstant){
570 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
574 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
576 // Try the implicit constant expression conversion
577 // from long to ulong, instead of a nice routine,
580 long v = ((LongConstant) expr).Value;
582 return new ULongConstant ((ulong) v);
586 // If we have an enumeration, extract the underlying type,
587 // use this during the comparission, but wrap around the original
590 Type real_target_type = target_type;
592 if (TypeManager.IsEnumType (real_target_type))
593 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
595 if (expr_type == real_target_type)
596 return new EmptyCast (expr, target_type);
598 if (expr_type == TypeManager.sbyte_type){
600 // From sbyte to short, int, long, float, double.
602 if (real_target_type == TypeManager.int32_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
604 if (real_target_type == TypeManager.int64_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
606 if (real_target_type == TypeManager.double_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
608 if (real_target_type == TypeManager.float_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
610 if (real_target_type == TypeManager.short_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
612 if (real_target_type == TypeManager.decimal_type)
613 return InternalTypeConstructor (ec, expr, target_type);
614 } else if (expr_type == TypeManager.byte_type){
616 // From byte to short, ushort, int, uint, long, ulong, float, double
618 if ((real_target_type == TypeManager.short_type) ||
619 (real_target_type == TypeManager.ushort_type) ||
620 (real_target_type == TypeManager.int32_type) ||
621 (real_target_type == TypeManager.uint32_type))
622 return new EmptyCast (expr, target_type);
624 if (real_target_type == TypeManager.uint64_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
626 if (real_target_type == TypeManager.int64_type)
627 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
628 if (real_target_type == TypeManager.float_type)
629 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
630 if (real_target_type == TypeManager.double_type)
631 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
632 if (real_target_type == TypeManager.decimal_type)
633 return InternalTypeConstructor (ec, expr, target_type);
634 } else if (expr_type == TypeManager.short_type){
636 // From short to int, long, float, double
638 if (real_target_type == TypeManager.int32_type)
639 return new EmptyCast (expr, target_type);
640 if (real_target_type == TypeManager.int64_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
642 if (real_target_type == TypeManager.double_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
644 if (real_target_type == TypeManager.float_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
646 if (real_target_type == TypeManager.decimal_type)
647 return InternalTypeConstructor (ec, expr, target_type);
648 } else if (expr_type == TypeManager.ushort_type){
650 // From ushort to int, uint, long, ulong, float, double
652 if (real_target_type == TypeManager.uint32_type)
653 return new EmptyCast (expr, target_type);
655 if (real_target_type == TypeManager.uint64_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
657 if (real_target_type == TypeManager.int32_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
659 if (real_target_type == TypeManager.int64_type)
660 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
661 if (real_target_type == TypeManager.double_type)
662 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
663 if (real_target_type == TypeManager.float_type)
664 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
665 if (real_target_type == TypeManager.decimal_type)
666 return InternalTypeConstructor (ec, expr, target_type);
667 } else if (expr_type == TypeManager.int32_type){
669 // From int to long, float, double
671 if (real_target_type == TypeManager.int64_type)
672 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
673 if (real_target_type == TypeManager.double_type)
674 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
675 if (real_target_type == TypeManager.float_type)
676 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
677 if (real_target_type == TypeManager.decimal_type)
678 return InternalTypeConstructor (ec, expr, target_type);
679 } else if (expr_type == TypeManager.uint32_type){
681 // From uint to long, ulong, float, double
683 if (real_target_type == TypeManager.int64_type)
684 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
685 if (real_target_type == TypeManager.uint64_type)
686 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
687 if (real_target_type == TypeManager.double_type)
688 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
690 if (real_target_type == TypeManager.float_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
693 if (real_target_type == TypeManager.decimal_type)
694 return InternalTypeConstructor (ec, expr, target_type);
695 } else if ((expr_type == TypeManager.uint64_type) ||
696 (expr_type == TypeManager.int64_type)){
698 // From long/ulong to float, double
700 if (real_target_type == TypeManager.double_type)
701 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
703 if (real_target_type == TypeManager.float_type)
704 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
706 if (real_target_type == TypeManager.decimal_type)
707 return InternalTypeConstructor (ec, expr, target_type);
708 } else if (expr_type == TypeManager.char_type){
710 // From char to ushort, int, uint, long, ulong, float, double
712 if ((real_target_type == TypeManager.ushort_type) ||
713 (real_target_type == TypeManager.int32_type) ||
714 (real_target_type == TypeManager.uint32_type))
715 return new EmptyCast (expr, target_type);
716 if (real_target_type == TypeManager.uint64_type)
717 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
718 if (real_target_type == TypeManager.int64_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
720 if (real_target_type == TypeManager.float_type)
721 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
722 if (real_target_type == TypeManager.double_type)
723 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
724 if (real_target_type == TypeManager.decimal_type)
725 return InternalTypeConstructor (ec, expr, target_type);
726 } else if (expr_type == TypeManager.float_type){
730 if (real_target_type == TypeManager.double_type)
731 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
738 /// Determines if a standard implicit conversion exists from
739 /// expr_type to target_type
741 public static bool StandardConversionExists (Expression expr, Type target_type)
743 Type expr_type = expr.Type;
745 if (expr_type == target_type)
748 // First numeric conversions
750 if (expr_type == TypeManager.sbyte_type){
752 // From sbyte to short, int, long, float, double.
754 if ((target_type == TypeManager.int32_type) ||
755 (target_type == TypeManager.int64_type) ||
756 (target_type == TypeManager.double_type) ||
757 (target_type == TypeManager.float_type) ||
758 (target_type == TypeManager.short_type) ||
759 (target_type == TypeManager.decimal_type))
762 } else if (expr_type == TypeManager.byte_type){
764 // From byte to short, ushort, int, uint, long, ulong, float, double
766 if ((target_type == TypeManager.short_type) ||
767 (target_type == TypeManager.ushort_type) ||
768 (target_type == TypeManager.int32_type) ||
769 (target_type == TypeManager.uint32_type) ||
770 (target_type == TypeManager.uint64_type) ||
771 (target_type == TypeManager.int64_type) ||
772 (target_type == TypeManager.float_type) ||
773 (target_type == TypeManager.double_type) ||
774 (target_type == TypeManager.decimal_type))
777 } else if (expr_type == TypeManager.short_type){
779 // From short to int, long, float, double
781 if ((target_type == TypeManager.int32_type) ||
782 (target_type == TypeManager.int64_type) ||
783 (target_type == TypeManager.double_type) ||
784 (target_type == TypeManager.float_type) ||
785 (target_type == TypeManager.decimal_type))
788 } else if (expr_type == TypeManager.ushort_type){
790 // From ushort to int, uint, long, ulong, float, double
792 if ((target_type == TypeManager.uint32_type) ||
793 (target_type == TypeManager.uint64_type) ||
794 (target_type == TypeManager.int32_type) ||
795 (target_type == TypeManager.int64_type) ||
796 (target_type == TypeManager.double_type) ||
797 (target_type == TypeManager.float_type) ||
798 (target_type == TypeManager.decimal_type))
801 } else if (expr_type == TypeManager.int32_type){
803 // From int to long, float, double
805 if ((target_type == TypeManager.int64_type) ||
806 (target_type == TypeManager.double_type) ||
807 (target_type == TypeManager.float_type) ||
808 (target_type == TypeManager.decimal_type))
811 } else if (expr_type == TypeManager.uint32_type){
813 // From uint to long, ulong, float, double
815 if ((target_type == TypeManager.int64_type) ||
816 (target_type == TypeManager.uint64_type) ||
817 (target_type == TypeManager.double_type) ||
818 (target_type == TypeManager.float_type) ||
819 (target_type == TypeManager.decimal_type))
822 } else if ((expr_type == TypeManager.uint64_type) ||
823 (expr_type == TypeManager.int64_type)) {
825 // From long/ulong to float, double
827 if ((target_type == TypeManager.double_type) ||
828 (target_type == TypeManager.float_type) ||
829 (target_type == TypeManager.decimal_type))
832 } else if (expr_type == TypeManager.char_type){
834 // From char to ushort, int, uint, long, ulong, float, double
836 if ((target_type == TypeManager.ushort_type) ||
837 (target_type == TypeManager.int32_type) ||
838 (target_type == TypeManager.uint32_type) ||
839 (target_type == TypeManager.uint64_type) ||
840 (target_type == TypeManager.int64_type) ||
841 (target_type == TypeManager.float_type) ||
842 (target_type == TypeManager.double_type) ||
843 (target_type == TypeManager.decimal_type))
846 } else if (expr_type == TypeManager.float_type){
850 if (target_type == TypeManager.double_type)
854 // Next reference conversions
856 if (target_type == TypeManager.object_type) {
857 if ((expr_type.IsClass) ||
858 (expr_type.IsValueType) ||
859 (expr_type.IsInterface))
862 } else if (expr_type.IsSubclassOf (target_type)) {
866 // Please remember that all code below actually comes
867 // from ImplicitReferenceConversion so make sure code remains in sync
869 // from any class-type S to any interface-type T.
870 if (expr_type.IsClass && target_type.IsInterface) {
871 if (TypeManager.ImplementsInterface (expr_type, target_type))
875 // from any interface type S to interface-type T.
876 // FIXME : Is it right to use IsAssignableFrom ?
877 if (expr_type.IsInterface && target_type.IsInterface)
878 if (target_type.IsAssignableFrom (expr_type))
881 // from an array-type S to an array-type of type T
882 if (expr_type.IsArray && target_type.IsArray) {
883 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
885 Type expr_element_type = expr_type.GetElementType ();
887 if (MyEmptyExpr == null)
888 MyEmptyExpr = new EmptyExpression ();
890 MyEmptyExpr.SetType (expr_element_type);
891 Type target_element_type = target_type.GetElementType ();
893 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
894 if (StandardConversionExists (MyEmptyExpr,
895 target_element_type))
900 // from an array-type to System.Array
901 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
904 // from any delegate type to System.Delegate
905 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
906 target_type == TypeManager.delegate_type)
907 if (target_type.IsAssignableFrom (expr_type))
910 // from any array-type or delegate type into System.ICloneable.
911 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
912 if (target_type == TypeManager.icloneable_type)
915 // from the null type to any reference-type.
916 if (expr is NullLiteral && !target_type.IsValueType)
921 if (expr is IntConstant){
922 int value = ((IntConstant) expr).Value;
924 if (target_type == TypeManager.sbyte_type){
925 if (value >= SByte.MinValue && value <= SByte.MaxValue)
927 } else if (target_type == TypeManager.byte_type){
928 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
930 } else if (target_type == TypeManager.short_type){
931 if (value >= Int16.MinValue && value <= Int16.MaxValue)
933 } else if (target_type == TypeManager.ushort_type){
934 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
936 } else if (target_type == TypeManager.uint32_type){
939 } else if (target_type == TypeManager.uint64_type){
941 // we can optimize this case: a positive int32
942 // always fits on a uint64. But we need an opcode
949 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
953 if (expr is LongConstant && target_type == TypeManager.uint64_type){
955 // Try the implicit constant expression conversion
956 // from long to ulong, instead of a nice routine,
959 long v = ((LongConstant) expr).Value;
964 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
965 IntLiteral i = (IntLiteral) expr;
974 // Used internally by FindMostEncompassedType, this is used
975 // to avoid creating lots of objects in the tight loop inside
976 // FindMostEncompassedType
978 static EmptyExpression priv_fmet_param;
981 /// Finds "most encompassed type" according to the spec (13.4.2)
982 /// amongst the methods in the MethodGroupExpr
984 static Type FindMostEncompassedType (ArrayList types)
988 if (priv_fmet_param == null)
989 priv_fmet_param = new EmptyExpression ();
991 for (int i = 0; i < types.Count; ++i) {
992 Type t = (Type) types [i];
993 priv_fmet_param.SetType (t);
1000 if (StandardConversionExists (priv_fmet_param, best))
1008 // Used internally by FindMostEncompassingType, this is used
1009 // to avoid creating lots of objects in the tight loop inside
1010 // FindMostEncompassingType
1012 static EmptyExpression priv_fmee_ret;
1015 /// Finds "most encompassing type" according to the spec (13.4.2)
1016 /// amongst the types in the given set
1018 static Type FindMostEncompassingType (ArrayList types)
1022 if (priv_fmee_ret == null)
1023 priv_fmee_ret = new EmptyExpression ();
1025 for (int i = 0; i < types.Count; ++i ) {
1027 Type t = (Type) types [i];
1028 priv_fmee_ret.SetType (best);
1035 if (StandardConversionExists (priv_fmee_ret, t))
1043 // Used to avoid creating too many objects
1045 static EmptyExpression priv_fms_expr;
1048 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1049 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1050 /// for explicit and implicit conversion operators.
1052 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1053 bool apply_explicit_conv_rules,
1056 ArrayList src_types_set = new ArrayList ();
1058 if (priv_fms_expr == null)
1059 priv_fms_expr = new EmptyExpression ();
1062 // If any operator converts from S then Sx = S
1064 for (int i = me.Methods.Length; i > 0; ) {
1067 MethodBase mb = me.Methods [i];
1068 ParameterData pd = Invocation.GetParameterData (mb);
1069 Type param_type = pd.ParameterType (0);
1071 if (param_type == source_type)
1074 if (apply_explicit_conv_rules) {
1077 // Find the set of applicable user-defined conversion operators, U. This set
1079 // user-defined implicit or explicit conversion operators declared by
1080 // the classes or structs in D that convert from a type encompassing
1081 // or encompassed by S to a type encompassing or encompassed by T
1083 priv_fms_expr.SetType (param_type);
1084 if (StandardConversionExists (priv_fms_expr, source_type))
1085 src_types_set.Add (param_type);
1087 priv_fms_expr.SetType (source_type);
1088 if (StandardConversionExists (priv_fms_expr, param_type))
1089 src_types_set.Add (param_type);
1093 // Only if S is encompassed by param_type
1095 priv_fms_expr.SetType (source_type);
1096 if (StandardConversionExists (priv_fms_expr, param_type))
1097 src_types_set.Add (param_type);
1102 // Explicit Conv rules
1104 if (apply_explicit_conv_rules) {
1106 ArrayList candidate_set = new ArrayList ();
1108 for (int i = 0; i < src_types_set.Count; ++i) {
1109 Type param_type = (Type) src_types_set [i];
1111 priv_fms_expr.SetType (source_type);
1113 if (StandardConversionExists (priv_fms_expr, param_type))
1114 candidate_set.Add (param_type);
1117 if (candidate_set.Count != 0)
1118 return FindMostEncompassedType (candidate_set);
1124 if (apply_explicit_conv_rules)
1125 return FindMostEncompassingType (src_types_set);
1127 return FindMostEncompassedType (src_types_set);
1131 // Useful in avoiding proliferation of objects
1133 static EmptyExpression priv_fmt_expr;
1136 /// Finds the most specific target Tx according to section 13.4.4
1138 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1139 bool apply_explicit_conv_rules,
1142 ArrayList tgt_types_set = new ArrayList ();
1144 if (priv_fmt_expr == null)
1145 priv_fmt_expr = new EmptyExpression ();
1148 // If any operator converts to T then Tx = T
1150 for (int i = me.Methods.Length; i > 0; ) {
1153 MethodInfo mi = (MethodInfo) me.Methods [i];
1154 Type ret_type = mi.ReturnType;
1156 if (ret_type == target)
1159 if (apply_explicit_conv_rules) {
1162 // Find the set of applicable user-defined conversion operators, U. This set
1164 // user-defined implicit or explicit conversion operators declared by
1165 // the classes or structs in D that convert from a type encompassing
1166 // or encompassed by S to a type encompassing or encompassed by T
1168 priv_fms_expr.SetType (ret_type);
1169 if (StandardConversionExists (priv_fms_expr, target))
1170 tgt_types_set.Add (ret_type);
1172 priv_fms_expr.SetType (target);
1173 if (StandardConversionExists (priv_fms_expr, ret_type))
1174 tgt_types_set.Add (ret_type);
1178 // Only if T is encompassed by param_type
1180 priv_fms_expr.SetType (ret_type);
1181 if (StandardConversionExists (priv_fms_expr, target))
1182 tgt_types_set.Add (ret_type);
1187 // Explicit conv rules
1189 if (apply_explicit_conv_rules) {
1191 ArrayList candidate_set = new ArrayList ();
1193 for (int i = 0; i < tgt_types_set.Count; ++i) {
1194 Type ret_type = (Type) tgt_types_set [i];
1196 priv_fmt_expr.SetType (ret_type);
1198 if (StandardConversionExists (priv_fmt_expr, target))
1199 candidate_set.Add (ret_type);
1202 if (candidate_set.Count != 0)
1203 return FindMostEncompassingType (candidate_set);
1207 // Okay, final case !
1209 if (apply_explicit_conv_rules)
1210 return FindMostEncompassedType (tgt_types_set);
1212 return FindMostEncompassingType (tgt_types_set);
1216 /// User-defined Implicit conversions
1218 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1219 Type target, Location loc)
1221 return UserDefinedConversion (ec, source, target, loc, false);
1225 /// User-defined Explicit conversions
1227 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1228 Type target, Location loc)
1230 return UserDefinedConversion (ec, source, target, loc, true);
1234 /// Computes the MethodGroup for the user-defined conversion
1235 /// operators from source_type to target_type. `look_for_explicit'
1236 /// controls whether we should also include the list of explicit
1239 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1240 Type source_type, Type target_type,
1241 Location loc, bool look_for_explicit)
1243 Expression mg1 = null, mg2 = null;
1244 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1248 // FIXME : How does the False operator come into the picture ?
1249 // This doesn't look complete and very correct !
1251 if (target_type == TypeManager.bool_type && !look_for_explicit)
1252 op_name = "op_True";
1254 op_name = "op_Implicit";
1256 MethodGroupExpr union3;
1258 mg1 = MethodLookup (ec, source_type, op_name, loc);
1259 if (source_type.BaseType != null)
1260 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1263 union3 = (MethodGroupExpr) mg2;
1264 else if (mg2 == null)
1265 union3 = (MethodGroupExpr) mg1;
1267 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1269 mg1 = MethodLookup (ec, target_type, op_name, loc);
1272 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1274 union3 = (MethodGroupExpr) mg1;
1277 if (target_type.BaseType != null)
1278 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1282 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1284 union3 = (MethodGroupExpr) mg1;
1287 MethodGroupExpr union4 = null;
1289 if (look_for_explicit) {
1290 op_name = "op_Explicit";
1292 mg5 = MemberLookup (ec, source_type, op_name, loc);
1293 if (source_type.BaseType != null)
1294 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1296 mg7 = MemberLookup (ec, target_type, op_name, loc);
1297 if (target_type.BaseType != null)
1298 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1300 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1301 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1303 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1306 return Invocation.MakeUnionSet (union3, union4, loc);
1310 /// User-defined conversions
1312 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1313 Type target, Location loc,
1314 bool look_for_explicit)
1316 MethodGroupExpr union;
1317 Type source_type = source.Type;
1318 MethodBase method = null;
1320 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1324 Type most_specific_source, most_specific_target;
1326 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1327 if (most_specific_source == null)
1330 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1331 if (most_specific_target == null)
1336 for (int i = union.Methods.Length; i > 0;) {
1339 MethodBase mb = union.Methods [i];
1340 ParameterData pd = Invocation.GetParameterData (mb);
1341 MethodInfo mi = (MethodInfo) union.Methods [i];
1343 if (pd.ParameterType (0) == most_specific_source &&
1344 mi.ReturnType == most_specific_target) {
1350 if (method == null || count > 1) {
1351 Report.Error (-11, loc, "Ambiguous user defined conversion");
1356 // This will do the conversion to the best match that we
1357 // found. Now we need to perform an implict standard conversion
1358 // if the best match was not the type that we were requested
1361 if (look_for_explicit)
1362 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1364 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1370 e = new UserCast ((MethodInfo) method, source);
1371 if (e.Type != target){
1372 if (!look_for_explicit)
1373 e = ConvertImplicitStandard (ec, e, target, loc);
1375 e = ConvertExplicitStandard (ec, e, target, loc);
1381 /// Converts implicitly the resolved expression `expr' into the
1382 /// `target_type'. It returns a new expression that can be used
1383 /// in a context that expects a `target_type'.
1385 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1386 Type target_type, Location loc)
1388 Type expr_type = expr.Type;
1391 if (expr_type == target_type)
1394 if (target_type == null)
1395 throw new Exception ("Target type is null");
1397 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1401 e = ImplicitUserConversion (ec, expr, target_type, loc);
1410 /// Attempts to apply the `Standard Implicit
1411 /// Conversion' rules to the expression `expr' into
1412 /// the `target_type'. It returns a new expression
1413 /// that can be used in a context that expects a
1416 /// This is different from `ConvertImplicit' in that the
1417 /// user defined implicit conversions are excluded.
1419 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1420 Type target_type, Location loc)
1422 Type expr_type = expr.Type;
1425 if (expr_type == target_type)
1428 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1432 e = ImplicitReferenceConversion (expr, target_type);
1436 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1437 IntLiteral i = (IntLiteral) expr;
1440 return new EmptyCast (expr, target_type);
1444 if (expr_type.IsPointer){
1445 if (target_type == TypeManager.void_ptr_type)
1446 return new EmptyCast (expr, target_type);
1449 // yep, comparing pointer types cant be done with
1450 // t1 == t2, we have to compare their element types.
1452 if (target_type.IsPointer){
1453 if (target_type.GetElementType()==expr_type.GetElementType())
1458 if (target_type.IsPointer){
1459 if (expr is NullLiteral)
1460 return new EmptyCast (expr, target_type);
1468 /// Attemps to perform an implict constant conversion of the IntConstant
1469 /// into a different data type using casts (See Implicit Constant
1470 /// Expression Conversions)
1472 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1474 int value = ic.Value;
1477 // FIXME: This could return constants instead of EmptyCasts
1479 if (target_type == TypeManager.sbyte_type){
1480 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1481 return new SByteConstant ((sbyte) value);
1482 } else if (target_type == TypeManager.byte_type){
1483 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1484 return new ByteConstant ((byte) value);
1485 } else if (target_type == TypeManager.short_type){
1486 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1487 return new ShortConstant ((short) value);
1488 } else if (target_type == TypeManager.ushort_type){
1489 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1490 return new UShortConstant ((ushort) value);
1491 } else if (target_type == TypeManager.uint32_type){
1493 return new UIntConstant ((uint) value);
1494 } else if (target_type == TypeManager.uint64_type){
1496 // we can optimize this case: a positive int32
1497 // always fits on a uint64. But we need an opcode
1501 return new ULongConstant ((ulong) value);
1504 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1505 return new EnumConstant (ic, target_type);
1510 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1512 string msg = "Cannot convert implicitly from `"+
1513 TypeManager.CSharpName (source) + "' to `" +
1514 TypeManager.CSharpName (target) + "'";
1516 Error (29, loc, msg);
1520 /// Attemptes to implicityly convert `target' into `type', using
1521 /// ConvertImplicit. If there is no implicit conversion, then
1522 /// an error is signaled
1524 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1525 Type target_type, Location loc)
1529 e = ConvertImplicit (ec, source, target_type, loc);
1533 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1535 "Double literal cannot be implicitly converted to " +
1536 "float type, use F suffix to create a float literal");
1539 Error_CannotConvertImplicit (loc, source.Type, target_type);
1545 /// Performs the explicit numeric conversions
1547 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1550 Type expr_type = expr.Type;
1553 // If we have an enumeration, extract the underlying type,
1554 // use this during the comparission, but wrap around the original
1557 Type real_target_type = target_type;
1559 if (TypeManager.IsEnumType (real_target_type))
1560 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1562 if (expr_type == TypeManager.sbyte_type){
1564 // From sbyte to byte, ushort, uint, ulong, char
1566 if (real_target_type == TypeManager.byte_type)
1567 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1568 if (real_target_type == TypeManager.ushort_type)
1569 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1570 if (real_target_type == TypeManager.uint32_type)
1571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1572 if (real_target_type == TypeManager.uint64_type)
1573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1574 if (real_target_type == TypeManager.char_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1576 } else if (expr_type == TypeManager.byte_type){
1578 // From byte to sbyte and char
1580 if (real_target_type == TypeManager.sbyte_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1582 if (real_target_type == TypeManager.char_type)
1583 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1584 } else if (expr_type == TypeManager.short_type){
1586 // From short to sbyte, byte, ushort, uint, ulong, char
1588 if (real_target_type == TypeManager.sbyte_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1590 if (real_target_type == TypeManager.byte_type)
1591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1592 if (real_target_type == TypeManager.ushort_type)
1593 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1594 if (real_target_type == TypeManager.uint32_type)
1595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1596 if (real_target_type == TypeManager.uint64_type)
1597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1598 if (real_target_type == TypeManager.char_type)
1599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1600 } else if (expr_type == TypeManager.ushort_type){
1602 // From ushort to sbyte, byte, short, char
1604 if (real_target_type == TypeManager.sbyte_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1606 if (real_target_type == TypeManager.byte_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1608 if (real_target_type == TypeManager.short_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1610 if (real_target_type == TypeManager.char_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1612 } else if (expr_type == TypeManager.int32_type){
1614 // From int to sbyte, byte, short, ushort, uint, ulong, char
1616 if (real_target_type == TypeManager.sbyte_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1618 if (real_target_type == TypeManager.byte_type)
1619 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1620 if (real_target_type == TypeManager.short_type)
1621 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1622 if (real_target_type == TypeManager.ushort_type)
1623 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1624 if (real_target_type == TypeManager.uint32_type)
1625 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1626 if (real_target_type == TypeManager.uint64_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1628 if (real_target_type == TypeManager.char_type)
1629 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1630 } else if (expr_type == TypeManager.uint32_type){
1632 // From uint to sbyte, byte, short, ushort, int, char
1634 if (real_target_type == TypeManager.sbyte_type)
1635 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1636 if (real_target_type == TypeManager.byte_type)
1637 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1638 if (real_target_type == TypeManager.short_type)
1639 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1640 if (real_target_type == TypeManager.ushort_type)
1641 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1642 if (real_target_type == TypeManager.int32_type)
1643 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1644 if (real_target_type == TypeManager.char_type)
1645 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1646 } else if (expr_type == TypeManager.int64_type){
1648 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1650 if (real_target_type == TypeManager.sbyte_type)
1651 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1652 if (real_target_type == TypeManager.byte_type)
1653 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1654 if (real_target_type == TypeManager.short_type)
1655 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1656 if (real_target_type == TypeManager.ushort_type)
1657 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1658 if (real_target_type == TypeManager.int32_type)
1659 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1660 if (real_target_type == TypeManager.uint32_type)
1661 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1662 if (real_target_type == TypeManager.uint64_type)
1663 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1664 if (real_target_type == TypeManager.char_type)
1665 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1666 } else if (expr_type == TypeManager.uint64_type){
1668 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1670 if (real_target_type == TypeManager.sbyte_type)
1671 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1672 if (real_target_type == TypeManager.byte_type)
1673 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1674 if (real_target_type == TypeManager.short_type)
1675 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1676 if (real_target_type == TypeManager.ushort_type)
1677 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1678 if (real_target_type == TypeManager.int32_type)
1679 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1680 if (real_target_type == TypeManager.uint32_type)
1681 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1682 if (real_target_type == TypeManager.int64_type)
1683 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1684 if (real_target_type == TypeManager.char_type)
1685 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1686 } else if (expr_type == TypeManager.char_type){
1688 // From char to sbyte, byte, short
1690 if (real_target_type == TypeManager.sbyte_type)
1691 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1692 if (real_target_type == TypeManager.byte_type)
1693 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1694 if (real_target_type == TypeManager.short_type)
1695 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1696 } else if (expr_type == TypeManager.float_type){
1698 // From float to sbyte, byte, short,
1699 // ushort, int, uint, long, ulong, char
1702 if (real_target_type == TypeManager.sbyte_type)
1703 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1704 if (real_target_type == TypeManager.byte_type)
1705 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1706 if (real_target_type == TypeManager.short_type)
1707 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1708 if (real_target_type == TypeManager.ushort_type)
1709 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1710 if (real_target_type == TypeManager.int32_type)
1711 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1712 if (real_target_type == TypeManager.uint32_type)
1713 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1714 if (real_target_type == TypeManager.int64_type)
1715 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1716 if (real_target_type == TypeManager.uint64_type)
1717 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1718 if (real_target_type == TypeManager.char_type)
1719 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1720 if (real_target_type == TypeManager.decimal_type)
1721 return InternalTypeConstructor (ec, expr, target_type);
1722 } else if (expr_type == TypeManager.double_type){
1724 // From double to byte, byte, short,
1725 // ushort, int, uint, long, ulong,
1726 // char, float or decimal
1728 if (real_target_type == TypeManager.sbyte_type)
1729 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1730 if (real_target_type == TypeManager.byte_type)
1731 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1732 if (real_target_type == TypeManager.short_type)
1733 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1734 if (real_target_type == TypeManager.ushort_type)
1735 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1736 if (real_target_type == TypeManager.int32_type)
1737 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1738 if (real_target_type == TypeManager.uint32_type)
1739 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1740 if (real_target_type == TypeManager.int64_type)
1741 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1742 if (real_target_type == TypeManager.uint64_type)
1743 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1744 if (real_target_type == TypeManager.char_type)
1745 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1746 if (real_target_type == TypeManager.float_type)
1747 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1748 if (real_target_type == TypeManager.decimal_type)
1749 return InternalTypeConstructor (ec, expr, target_type);
1752 // decimal is taken care of by the op_Explicit methods.
1758 /// Returns whether an explicit reference conversion can be performed
1759 /// from source_type to target_type
1761 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1763 bool target_is_value_type = target_type.IsValueType;
1765 if (source_type == target_type)
1769 // From object to any reference type
1771 if (source_type == TypeManager.object_type && !target_is_value_type)
1775 // From any class S to any class-type T, provided S is a base class of T
1777 if (target_type.IsSubclassOf (source_type))
1781 // From any interface type S to any interface T provided S is not derived from T
1783 if (source_type.IsInterface && target_type.IsInterface){
1784 if (!target_type.IsSubclassOf (source_type))
1789 // From any class type S to any interface T, provided S is not sealed
1790 // and provided S does not implement T.
1792 if (target_type.IsInterface && !source_type.IsSealed &&
1793 !TypeManager.ImplementsInterface (source_type, target_type))
1797 // From any interface-type S to to any class type T, provided T is not
1798 // sealed, or provided T implements S.
1800 if (source_type.IsInterface &&
1801 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1805 // From an array type S with an element type Se to an array type T with an
1806 // element type Te provided all the following are true:
1807 // * S and T differe only in element type, in other words, S and T
1808 // have the same number of dimensions.
1809 // * Both Se and Te are reference types
1810 // * An explicit referenc conversions exist from Se to Te
1812 if (source_type.IsArray && target_type.IsArray) {
1813 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1815 Type source_element_type = source_type.GetElementType ();
1816 Type target_element_type = target_type.GetElementType ();
1818 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1819 if (ExplicitReferenceConversionExists (source_element_type,
1820 target_element_type))
1826 // From System.Array to any array-type
1827 if (source_type == TypeManager.array_type &&
1828 target_type.IsSubclassOf (TypeManager.array_type)){
1833 // From System delegate to any delegate-type
1835 if (source_type == TypeManager.delegate_type &&
1836 target_type.IsSubclassOf (TypeManager.delegate_type))
1840 // From ICloneable to Array or Delegate types
1842 if (source_type == TypeManager.icloneable_type &&
1843 (target_type == TypeManager.array_type ||
1844 target_type == TypeManager.delegate_type))
1851 /// Implements Explicit Reference conversions
1853 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1855 Type source_type = source.Type;
1856 bool target_is_value_type = target_type.IsValueType;
1859 // From object to any reference type
1861 if (source_type == TypeManager.object_type && !target_is_value_type)
1862 return new ClassCast (source, target_type);
1866 // From any class S to any class-type T, provided S is a base class of T
1868 if (target_type.IsSubclassOf (source_type))
1869 return new ClassCast (source, target_type);
1872 // From any interface type S to any interface T provided S is not derived from T
1874 if (source_type.IsInterface && target_type.IsInterface){
1875 if (TypeManager.ImplementsInterface (source_type, target_type))
1878 return new ClassCast (source, target_type);
1882 // From any class type S to any interface T, provides S is not sealed
1883 // and provided S does not implement T.
1885 if (target_type.IsInterface && !source_type.IsSealed) {
1886 if (TypeManager.ImplementsInterface (source_type, target_type))
1889 return new ClassCast (source, target_type);
1894 // From any interface-type S to to any class type T, provided T is not
1895 // sealed, or provided T implements S.
1897 if (source_type.IsInterface) {
1898 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
1899 return new ClassCast (source, target_type);
1904 // From an array type S with an element type Se to an array type T with an
1905 // element type Te provided all the following are true:
1906 // * S and T differe only in element type, in other words, S and T
1907 // have the same number of dimensions.
1908 // * Both Se and Te are reference types
1909 // * An explicit referenc conversions exist from Se to Te
1911 if (source_type.IsArray && target_type.IsArray) {
1912 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1914 Type source_element_type = source_type.GetElementType ();
1915 Type target_element_type = target_type.GetElementType ();
1917 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1918 if (ExplicitReferenceConversionExists (source_element_type,
1919 target_element_type))
1920 return new ClassCast (source, target_type);
1925 // From System.Array to any array-type
1926 if (source_type == TypeManager.array_type &&
1927 target_type.IsSubclassOf (TypeManager.array_type)){
1928 return new ClassCast (source, target_type);
1932 // From System delegate to any delegate-type
1934 if (source_type == TypeManager.delegate_type &&
1935 target_type.IsSubclassOf (TypeManager.delegate_type))
1936 return new ClassCast (source, target_type);
1939 // From ICloneable to Array or Delegate types
1941 if (source_type == TypeManager.icloneable_type &&
1942 (target_type == TypeManager.array_type ||
1943 target_type == TypeManager.delegate_type))
1944 return new ClassCast (source, target_type);
1950 /// Performs an explicit conversion of the expression `expr' whose
1951 /// type is expr.Type to `target_type'.
1953 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1954 Type target_type, Location loc)
1956 Type expr_type = expr.Type;
1957 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1962 ne = ConvertNumericExplicit (ec, expr, target_type);
1967 // Unboxing conversion.
1969 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1970 return new UnboxCast (expr, target_type);
1975 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1979 // FIXME: Is there any reason we should have EnumConstant
1980 // dealt with here instead of just using always the
1981 // UnderlyingSystemType to wrap the type?
1983 if (expr is EnumConstant)
1984 e = ((EnumConstant) expr).Child;
1986 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1989 Expression t = ConvertImplicit (ec, e, target_type, loc);
1993 return ConvertNumericExplicit (ec, e, target_type);
1996 ne = ConvertReferenceExplicit (expr, target_type);
2001 if (target_type.IsPointer){
2002 if (expr_type.IsPointer)
2003 return new EmptyCast (expr, target_type);
2005 if (expr_type == TypeManager.sbyte_type ||
2006 expr_type == TypeManager.byte_type ||
2007 expr_type == TypeManager.short_type ||
2008 expr_type == TypeManager.ushort_type ||
2009 expr_type == TypeManager.int32_type ||
2010 expr_type == TypeManager.uint32_type ||
2011 expr_type == TypeManager.uint64_type ||
2012 expr_type == TypeManager.int64_type)
2013 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2015 if (expr_type.IsPointer){
2016 if (target_type == TypeManager.sbyte_type ||
2017 target_type == TypeManager.byte_type ||
2018 target_type == TypeManager.short_type ||
2019 target_type == TypeManager.ushort_type ||
2020 target_type == TypeManager.int32_type ||
2021 target_type == TypeManager.uint32_type ||
2022 target_type == TypeManager.uint64_type ||
2023 target_type == TypeManager.int64_type){
2024 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2027 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2032 ce = ConvertNumericExplicit (ec, e, target_type);
2036 // We should always be able to go from an uint32
2037 // implicitly or explicitly to the other integral
2040 throw new Exception ("Internal compiler error");
2045 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2049 Error_CannotConvertType (loc, expr_type, target_type);
2054 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2056 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2057 Type target_type, Location l)
2059 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2064 ne = ConvertNumericExplicit (ec, expr, target_type);
2068 ne = ConvertReferenceExplicit (expr, target_type);
2072 Error_CannotConvertType (l, expr.Type, target_type);
2076 static string ExprClassName (ExprClass c)
2079 case ExprClass.Invalid:
2081 case ExprClass.Value:
2083 case ExprClass.Variable:
2085 case ExprClass.Namespace:
2087 case ExprClass.Type:
2089 case ExprClass.MethodGroup:
2090 return "method group";
2091 case ExprClass.PropertyAccess:
2092 return "property access";
2093 case ExprClass.EventAccess:
2094 return "event access";
2095 case ExprClass.IndexerAccess:
2096 return "indexer access";
2097 case ExprClass.Nothing:
2100 throw new Exception ("Should not happen");
2104 /// Reports that we were expecting `expr' to be of class `expected'
2106 protected void report118 (Location loc, Expression expr, string expected)
2108 string kind = "Unknown";
2111 kind = ExprClassName (expr.eclass);
2113 Error (118, loc, "Expression denotes a `" + kind +
2114 "' where a `" + expected + "' was expected");
2117 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2119 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2120 TypeManager.CSharpName (t));
2123 public static void UnsafeError (Location loc)
2125 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2129 /// Converts the IntConstant, UIntConstant, LongConstant or
2130 /// ULongConstant into the integral target_type. Notice
2131 /// that we do not return an `Expression' we do return
2132 /// a boxed integral type.
2134 /// FIXME: Since I added the new constants, we need to
2135 /// also support conversions from CharConstant, ByteConstant,
2136 /// SByteConstant, UShortConstant, ShortConstant
2138 /// This is used by the switch statement, so the domain
2139 /// of work is restricted to the literals above, and the
2140 /// targets are int32, uint32, char, byte, sbyte, ushort,
2141 /// short, uint64 and int64
2143 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2147 if (c.Type == target_type)
2148 return ((Constant) c).GetValue ();
2151 // Make into one of the literals we handle, we dont really care
2152 // about this value as we will just return a few limited types
2154 if (c is EnumConstant)
2155 c = ((EnumConstant)c).WidenToCompilerConstant ();
2157 if (c is IntConstant){
2158 int v = ((IntConstant) c).Value;
2160 if (target_type == TypeManager.uint32_type){
2163 } else if (target_type == TypeManager.char_type){
2164 if (v >= Char.MinValue && v <= Char.MaxValue)
2166 } else if (target_type == TypeManager.byte_type){
2167 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2169 } else if (target_type == TypeManager.sbyte_type){
2170 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2172 } else if (target_type == TypeManager.short_type){
2173 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2175 } else if (target_type == TypeManager.ushort_type){
2176 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2178 } else if (target_type == TypeManager.int64_type)
2180 else if (target_type == TypeManager.uint64_type){
2186 } else if (c is UIntConstant){
2187 uint v = ((UIntConstant) c).Value;
2189 if (target_type == TypeManager.int32_type){
2190 if (v <= Int32.MaxValue)
2192 } else if (target_type == TypeManager.char_type){
2193 if (v >= Char.MinValue && v <= Char.MaxValue)
2195 } else if (target_type == TypeManager.byte_type){
2196 if (v <= Byte.MaxValue)
2198 } else if (target_type == TypeManager.sbyte_type){
2199 if (v <= SByte.MaxValue)
2201 } else if (target_type == TypeManager.short_type){
2202 if (v <= UInt16.MaxValue)
2204 } else if (target_type == TypeManager.ushort_type){
2205 if (v <= UInt16.MaxValue)
2207 } else if (target_type == TypeManager.int64_type)
2209 else if (target_type == TypeManager.uint64_type)
2212 } else if (c is LongConstant){
2213 long v = ((LongConstant) c).Value;
2215 if (target_type == TypeManager.int32_type){
2216 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2218 } else if (target_type == TypeManager.uint32_type){
2219 if (v >= 0 && v <= UInt32.MaxValue)
2221 } else if (target_type == TypeManager.char_type){
2222 if (v >= Char.MinValue && v <= Char.MaxValue)
2224 } else if (target_type == TypeManager.byte_type){
2225 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2227 } else if (target_type == TypeManager.sbyte_type){
2228 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2230 } else if (target_type == TypeManager.short_type){
2231 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2233 } else if (target_type == TypeManager.ushort_type){
2234 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2236 } else if (target_type == TypeManager.uint64_type){
2241 } else if (c is ULongConstant){
2242 ulong v = ((ULongConstant) c).Value;
2244 if (target_type == TypeManager.int32_type){
2245 if (v <= Int32.MaxValue)
2247 } else if (target_type == TypeManager.uint32_type){
2248 if (v <= UInt32.MaxValue)
2250 } else if (target_type == TypeManager.char_type){
2251 if (v >= Char.MinValue && v <= Char.MaxValue)
2253 } else if (target_type == TypeManager.byte_type){
2254 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2256 } else if (target_type == TypeManager.sbyte_type){
2257 if (v <= (int) SByte.MaxValue)
2259 } else if (target_type == TypeManager.short_type){
2260 if (v <= UInt16.MaxValue)
2262 } else if (target_type == TypeManager.ushort_type){
2263 if (v <= UInt16.MaxValue)
2265 } else if (target_type == TypeManager.int64_type){
2266 if (v <= Int64.MaxValue)
2270 } else if (c is ByteConstant){
2271 byte v = ((ByteConstant) c).Value;
2273 if (target_type == TypeManager.int32_type)
2275 else if (target_type == TypeManager.uint32_type)
2277 else if (target_type == TypeManager.char_type)
2279 else if (target_type == TypeManager.sbyte_type){
2280 if (v <= SByte.MaxValue)
2282 } else if (target_type == TypeManager.short_type)
2284 else if (target_type == TypeManager.ushort_type)
2286 else if (target_type == TypeManager.int64_type)
2288 else if (target_type == TypeManager.uint64_type)
2291 } else if (c is SByteConstant){
2292 sbyte v = ((SByteConstant) c).Value;
2294 if (target_type == TypeManager.int32_type)
2296 else if (target_type == TypeManager.uint32_type){
2299 } else if (target_type == TypeManager.char_type){
2302 } else if (target_type == TypeManager.byte_type){
2305 } else if (target_type == TypeManager.short_type)
2307 else if (target_type == TypeManager.ushort_type){
2310 } else if (target_type == TypeManager.int64_type)
2312 else if (target_type == TypeManager.uint64_type){
2317 } else if (c is ShortConstant){
2318 short v = ((ShortConstant) c).Value;
2320 if (target_type == TypeManager.int32_type){
2322 } else if (target_type == TypeManager.uint32_type){
2325 } else if (target_type == TypeManager.char_type){
2328 } else if (target_type == TypeManager.byte_type){
2329 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2331 } else if (target_type == TypeManager.sbyte_type){
2332 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2334 } else if (target_type == TypeManager.ushort_type){
2337 } else if (target_type == TypeManager.int64_type)
2339 else if (target_type == TypeManager.uint64_type)
2343 } else if (c is UShortConstant){
2344 ushort v = ((UShortConstant) c).Value;
2346 if (target_type == TypeManager.int32_type)
2348 else if (target_type == TypeManager.uint32_type)
2350 else if (target_type == TypeManager.char_type){
2351 if (v >= Char.MinValue && v <= Char.MaxValue)
2353 } else if (target_type == TypeManager.byte_type){
2354 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2356 } else if (target_type == TypeManager.sbyte_type){
2357 if (v <= SByte.MaxValue)
2359 } else if (target_type == TypeManager.short_type){
2360 if (v <= Int16.MaxValue)
2362 } else if (target_type == TypeManager.int64_type)
2364 else if (target_type == TypeManager.uint64_type)
2368 } else if (c is CharConstant){
2369 char v = ((CharConstant) c).Value;
2371 if (target_type == TypeManager.int32_type)
2373 else if (target_type == TypeManager.uint32_type)
2375 else if (target_type == TypeManager.byte_type){
2376 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2378 } else if (target_type == TypeManager.sbyte_type){
2379 if (v <= SByte.MaxValue)
2381 } else if (target_type == TypeManager.short_type){
2382 if (v <= Int16.MaxValue)
2384 } else if (target_type == TypeManager.ushort_type)
2386 else if (target_type == TypeManager.int64_type)
2388 else if (target_type == TypeManager.uint64_type)
2393 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2398 // Load the object from the pointer. The `IsReference' is used
2399 // to control whether we should use Ldind_Ref or LdObj if the
2400 // value is not a `core' type.
2402 // Maybe we should try to extract this infromation form the type?
2403 // TODO: Maybe this is a bug. The reason we have this flag is because
2404 // I had almost identical code in ParameterReference (for handling
2405 // references) and in UnboxCast.
2407 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2409 if (t == TypeManager.int32_type)
2410 ig.Emit (OpCodes.Ldind_I4);
2411 else if (t == TypeManager.uint32_type)
2412 ig.Emit (OpCodes.Ldind_U4);
2413 else if (t == TypeManager.short_type)
2414 ig.Emit (OpCodes.Ldind_I2);
2415 else if (t == TypeManager.ushort_type)
2416 ig.Emit (OpCodes.Ldind_U2);
2417 else if (t == TypeManager.char_type)
2418 ig.Emit (OpCodes.Ldind_U2);
2419 else if (t == TypeManager.byte_type)
2420 ig.Emit (OpCodes.Ldind_U1);
2421 else if (t == TypeManager.sbyte_type)
2422 ig.Emit (OpCodes.Ldind_I1);
2423 else if (t == TypeManager.uint64_type)
2424 ig.Emit (OpCodes.Ldind_I8);
2425 else if (t == TypeManager.int64_type)
2426 ig.Emit (OpCodes.Ldind_I8);
2427 else if (t == TypeManager.float_type)
2428 ig.Emit (OpCodes.Ldind_R4);
2429 else if (t == TypeManager.double_type)
2430 ig.Emit (OpCodes.Ldind_R8);
2431 else if (t == TypeManager.bool_type)
2432 ig.Emit (OpCodes.Ldind_I1);
2433 else if (t == TypeManager.intptr_type)
2434 ig.Emit (OpCodes.Ldind_I);
2435 else if (TypeManager.IsEnumType (t)){
2436 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2439 ig.Emit (OpCodes.Ldind_Ref);
2441 ig.Emit (OpCodes.Ldobj, t);
2446 // The stack contains the pointer and the value of type `type'
2448 public static void StoreFromPtr (ILGenerator ig, Type type)
2450 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2451 ig.Emit (OpCodes.Stind_I4);
2452 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2453 ig.Emit (OpCodes.Stind_I8);
2454 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2455 type == TypeManager.ushort_type)
2456 ig.Emit (OpCodes.Stind_I2);
2457 else if (type == TypeManager.float_type)
2458 ig.Emit (OpCodes.Stind_R4);
2459 else if (type == TypeManager.double_type)
2460 ig.Emit (OpCodes.Stind_R8);
2461 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2462 type == TypeManager.bool_type)
2463 ig.Emit (OpCodes.Stind_I1);
2464 else if (type == TypeManager.intptr_type)
2465 ig.Emit (OpCodes.Stind_I);
2467 ig.Emit (OpCodes.Stind_Ref);
2471 // Returns the size of type `t' if known, otherwise, 0
2473 public static int GetTypeSize (Type t)
2475 if (t == TypeManager.int32_type ||
2476 t == TypeManager.uint32_type ||
2477 t == TypeManager.float_type)
2479 else if (t == TypeManager.int64_type ||
2480 t == TypeManager.uint64_type ||
2481 t == TypeManager.double_type)
2483 else if (t == TypeManager.byte_type ||
2484 t == TypeManager.sbyte_type ||
2485 t == TypeManager.bool_type)
2487 else if (t == TypeManager.short_type ||
2488 t == TypeManager.char_type ||
2489 t == TypeManager.ushort_type)
2497 /// This is just a base class for expressions that can
2498 /// appear on statements (invocations, object creation,
2499 /// assignments, post/pre increment and decrement). The idea
2500 /// being that they would support an extra Emition interface that
2501 /// does not leave a result on the stack.
2503 public abstract class ExpressionStatement : Expression {
2506 /// Requests the expression to be emitted in a `statement'
2507 /// context. This means that no new value is left on the
2508 /// stack after invoking this method (constrasted with
2509 /// Emit that will always leave a value on the stack).
2511 public abstract void EmitStatement (EmitContext ec);
2515 /// This kind of cast is used to encapsulate the child
2516 /// whose type is child.Type into an expression that is
2517 /// reported to return "return_type". This is used to encapsulate
2518 /// expressions which have compatible types, but need to be dealt
2519 /// at higher levels with.
2521 /// For example, a "byte" expression could be encapsulated in one
2522 /// of these as an "unsigned int". The type for the expression
2523 /// would be "unsigned int".
2526 public class EmptyCast : Expression {
2527 protected Expression child;
2529 public EmptyCast (Expression child, Type return_type)
2531 eclass = child.eclass;
2536 public override Expression DoResolve (EmitContext ec)
2538 // This should never be invoked, we are born in fully
2539 // initialized state.
2544 public override void Emit (EmitContext ec)
2551 /// This class is used to wrap literals which belong inside Enums
2553 public class EnumConstant : Constant {
2554 public Constant Child;
2556 public EnumConstant (Constant child, Type enum_type)
2558 eclass = child.eclass;
2563 public override Expression DoResolve (EmitContext ec)
2565 // This should never be invoked, we are born in fully
2566 // initialized state.
2571 public override void Emit (EmitContext ec)
2576 public override object GetValue ()
2578 return Child.GetValue ();
2582 // Converts from one of the valid underlying types for an enumeration
2583 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2584 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2586 public Constant WidenToCompilerConstant ()
2588 Type t = TypeManager.EnumToUnderlying (Child.Type);
2589 object v = ((Constant) Child).GetValue ();;
2591 if (t == TypeManager.int32_type)
2592 return new IntConstant ((int) v);
2593 if (t == TypeManager.uint32_type)
2594 return new UIntConstant ((uint) v);
2595 if (t == TypeManager.int64_type)
2596 return new LongConstant ((long) v);
2597 if (t == TypeManager.uint64_type)
2598 return new ULongConstant ((ulong) v);
2599 if (t == TypeManager.short_type)
2600 return new ShortConstant ((short) v);
2601 if (t == TypeManager.ushort_type)
2602 return new UShortConstant ((ushort) v);
2603 if (t == TypeManager.byte_type)
2604 return new ByteConstant ((byte) v);
2605 if (t == TypeManager.sbyte_type)
2606 return new SByteConstant ((sbyte) v);
2608 throw new Exception ("Invalid enumeration underlying type: " + t);
2612 // Extracts the value in the enumeration on its native representation
2614 public object GetPlainValue ()
2616 Type t = TypeManager.EnumToUnderlying (Child.Type);
2617 object v = ((Constant) Child).GetValue ();;
2619 if (t == TypeManager.int32_type)
2621 if (t == TypeManager.uint32_type)
2623 if (t == TypeManager.int64_type)
2625 if (t == TypeManager.uint64_type)
2627 if (t == TypeManager.short_type)
2629 if (t == TypeManager.ushort_type)
2631 if (t == TypeManager.byte_type)
2633 if (t == TypeManager.sbyte_type)
2639 public override string AsString ()
2641 return Child.AsString ();
2644 public override DoubleConstant ConvertToDouble ()
2646 return Child.ConvertToDouble ();
2649 public override FloatConstant ConvertToFloat ()
2651 return Child.ConvertToFloat ();
2654 public override ULongConstant ConvertToULong ()
2656 return Child.ConvertToULong ();
2659 public override LongConstant ConvertToLong ()
2661 return Child.ConvertToLong ();
2664 public override UIntConstant ConvertToUInt ()
2666 return Child.ConvertToUInt ();
2669 public override IntConstant ConvertToInt ()
2671 return Child.ConvertToInt ();
2676 /// This kind of cast is used to encapsulate Value Types in objects.
2678 /// The effect of it is to box the value type emitted by the previous
2681 public class BoxedCast : EmptyCast {
2683 public BoxedCast (Expression expr)
2684 : base (expr, TypeManager.object_type)
2688 public override Expression DoResolve (EmitContext ec)
2690 // This should never be invoked, we are born in fully
2691 // initialized state.
2696 public override void Emit (EmitContext ec)
2700 ec.ig.Emit (OpCodes.Box, child.Type);
2704 public class UnboxCast : EmptyCast {
2705 public UnboxCast (Expression expr, Type return_type)
2706 : base (expr, return_type)
2710 public override Expression DoResolve (EmitContext ec)
2712 // This should never be invoked, we are born in fully
2713 // initialized state.
2718 public override void Emit (EmitContext ec)
2721 ILGenerator ig = ec.ig;
2724 ig.Emit (OpCodes.Unbox, t);
2726 LoadFromPtr (ig, t, false);
2731 /// This is used to perform explicit numeric conversions.
2733 /// Explicit numeric conversions might trigger exceptions in a checked
2734 /// context, so they should generate the conv.ovf opcodes instead of
2737 public class ConvCast : EmptyCast {
2738 public enum Mode : byte {
2739 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2741 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2742 U2_I1, U2_U1, U2_I2, U2_CH,
2743 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2744 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2745 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2746 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2747 CH_I1, CH_U1, CH_I2,
2748 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2749 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2755 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2756 : base (child, return_type)
2759 checked_state = ec.CheckState;
2762 public override Expression DoResolve (EmitContext ec)
2764 // This should never be invoked, we are born in fully
2765 // initialized state.
2770 public override void Emit (EmitContext ec)
2772 ILGenerator ig = ec.ig;
2778 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2779 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2780 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2781 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2782 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2784 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2785 case Mode.U1_CH: /* nothing */ break;
2787 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2788 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2789 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2790 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2791 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2792 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2794 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2795 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2796 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2797 case Mode.U2_CH: /* nothing */ break;
2799 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2800 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2801 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2802 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2803 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2804 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2805 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2807 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2808 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2809 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2810 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2811 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2812 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2814 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2815 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2816 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2817 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2818 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2819 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2820 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2821 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2823 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2824 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2825 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2826 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2827 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2828 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2829 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2830 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2832 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2833 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2834 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2836 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2837 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2838 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2839 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2840 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2841 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2842 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2843 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2844 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2846 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2847 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2848 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2849 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2850 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2851 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2852 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2853 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2854 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2855 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2859 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2860 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2861 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2862 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2863 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2865 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2866 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2868 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2869 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2870 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2871 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2872 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2873 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2875 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2876 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2877 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2878 case Mode.U2_CH: /* nothing */ break;
2880 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2881 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2882 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2883 case Mode.I4_U4: /* nothing */ break;
2884 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2885 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2886 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2888 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2889 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2890 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2891 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2892 case Mode.U4_I4: /* nothing */ break;
2893 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2895 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2896 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2897 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2898 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2899 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2900 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2901 case Mode.I8_U8: /* nothing */ break;
2902 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2904 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2905 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2906 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2907 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2908 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2909 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2910 case Mode.U8_I8: /* nothing */ break;
2911 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2913 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2914 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2915 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2917 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2918 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2919 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2920 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2921 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2922 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2923 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2924 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2925 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2927 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2928 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2929 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2930 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2931 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2932 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2933 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2934 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2935 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2936 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2942 public class OpcodeCast : EmptyCast {
2946 public OpcodeCast (Expression child, Type return_type, OpCode op)
2947 : base (child, return_type)
2951 second_valid = false;
2954 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2955 : base (child, return_type)
2960 second_valid = true;
2963 public override Expression DoResolve (EmitContext ec)
2965 // This should never be invoked, we are born in fully
2966 // initialized state.
2971 public override void Emit (EmitContext ec)
2982 /// This kind of cast is used to encapsulate a child and cast it
2983 /// to the class requested
2985 public class ClassCast : EmptyCast {
2986 public ClassCast (Expression child, Type return_type)
2987 : base (child, return_type)
2992 public override Expression DoResolve (EmitContext ec)
2994 // This should never be invoked, we are born in fully
2995 // initialized state.
3000 public override void Emit (EmitContext ec)
3004 ec.ig.Emit (OpCodes.Castclass, type);
3010 /// SimpleName expressions are initially formed of a single
3011 /// word and it only happens at the beginning of the expression.
3015 /// The expression will try to be bound to a Field, a Method
3016 /// group or a Property. If those fail we pass the name to our
3017 /// caller and the SimpleName is compounded to perform a type
3018 /// lookup. The idea behind this process is that we want to avoid
3019 /// creating a namespace map from the assemblies, as that requires
3020 /// the GetExportedTypes function to be called and a hashtable to
3021 /// be constructed which reduces startup time. If later we find
3022 /// that this is slower, we should create a `NamespaceExpr' expression
3023 /// that fully participates in the resolution process.
3025 /// For example `System.Console.WriteLine' is decomposed into
3026 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3028 /// The first SimpleName wont produce a match on its own, so it will
3030 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3032 /// System.Console will produce a TypeExpr match.
3034 /// The downside of this is that we might be hitting `LookupType' too many
3035 /// times with this scheme.
3037 public class SimpleName : Expression {
3038 public readonly string Name;
3039 public readonly Location Location;
3041 public SimpleName (string name, Location l)
3047 public static void Error120 (Location l, string name)
3051 "An object reference is required " +
3052 "for the non-static field `"+name+"'");
3056 // Checks whether we are trying to access an instance
3057 // property, method or field from a static body.
3059 Expression MemberStaticCheck (Expression e)
3061 if (e is FieldExpr){
3062 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3065 Error120 (Location, Name);
3068 } else if (e is MethodGroupExpr){
3069 MethodGroupExpr mg = (MethodGroupExpr) e;
3071 if (!mg.RemoveInstanceMethods ()){
3072 Error120 (Location, mg.Methods [0].Name);
3076 } else if (e is PropertyExpr){
3077 if (!((PropertyExpr) e).IsStatic){
3078 Error120 (Location, Name);
3081 } else if (e is EventExpr) {
3082 if (!((EventExpr) e).IsStatic) {
3083 Error120 (Location, Name);
3091 public override Expression DoResolve (EmitContext ec)
3093 return SimpleNameResolve (ec, false);
3096 public Expression DoResolveAllowStatic (EmitContext ec)
3098 return SimpleNameResolve (ec, true);
3102 /// 7.5.2: Simple Names.
3104 /// Local Variables and Parameters are handled at
3105 /// parse time, so they never occur as SimpleNames.
3107 /// The `allow_static' flag is used by MemberAccess only
3108 /// and it is used to inform us that it is ok for us to
3109 /// avoid the static check, because MemberAccess might end
3110 /// up resolving the Name as a Type name and the access as
3111 /// a static type access.
3113 /// ie: Type Type; .... { Type.GetType (""); }
3115 /// Type is both an instance variable and a Type; Type.GetType
3116 /// is the static method not an instance method of type.
3118 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
3120 Expression e = null;
3123 // Stage 1: Performed by the parser (binding to locals or parameters).
3125 if (!ec.OnlyLookupTypes){
3126 Block current_block = ec.CurrentBlock;
3127 if (current_block != null && current_block.IsVariableDefined (Name)){
3128 LocalVariableReference var;
3130 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3132 return var.Resolve (ec);
3136 // Stage 2: Lookup members
3140 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3141 // Hence we have two different cases
3144 DeclSpace lookup_ds = ec.DeclSpace;
3146 if (lookup_ds.TypeBuilder == null)
3149 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
3154 // Classes/structs keep looking, enums break
3156 if (lookup_ds is TypeContainer)
3157 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3160 } while (lookup_ds != null);
3162 if (e == null && ec.ContainerType != null)
3163 e = MemberLookup (ec, ec.ContainerType, Name, Location);
3166 // Continuation of stage 2
3169 // Stage 3: Lookup symbol in the various namespaces.
3171 DeclSpace ds = ec.DeclSpace;
3175 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3176 return new TypeExpr (t);
3179 // Stage 2 part b: Lookup up if we are an alias to a type
3182 // Since we are cheating: we only do the Alias lookup for
3183 // namespaces if the name does not include any dots in it
3186 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3187 // System.Console.WriteLine (Name + " --> " + alias_value);
3188 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3190 return new TypeExpr (t);
3192 // we have alias value, but it isn't Type, so try if it's namespace
3193 return new SimpleName (alias_value, Location);
3196 // No match, maybe our parent can compose us
3197 // into something meaningful.
3202 // Stage 2 continues here.
3207 if (ec.OnlyLookupTypes)
3210 if (e is FieldExpr){
3211 FieldExpr fe = (FieldExpr) e;
3212 FieldInfo fi = fe.FieldInfo;
3214 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3215 UnsafeError (Location);
3219 if (!allow_static && !fi.IsStatic){
3220 Error120 (Location, Name);
3224 // If we are not in static code and this
3225 // field is not static, set the instance to `this'.
3228 fe.InstanceExpression = ec.This;
3232 if (fi is FieldBuilder) {
3233 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3236 object o = c.LookupConstantValue (ec);
3237 object real_value = ((Constant)c.Expr).GetValue ();
3238 return Constantify (real_value, fi.FieldType);
3243 Type t = fi.FieldType;
3244 Type decl_type = fi.DeclaringType;
3247 if (fi is FieldBuilder)
3248 o = TypeManager.GetValue ((FieldBuilder) fi);
3250 o = fi.GetValue (fi);
3252 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3253 Expression enum_member = MemberLookup (
3254 ec, decl_type, "value__", MemberTypes.Field,
3255 AllBindingFlags, Location);
3257 Enum en = TypeManager.LookupEnum (decl_type);
3261 c = Constantify (o, en.UnderlyingType);
3263 c = Constantify (o, enum_member.Type);
3265 return new EnumConstant (c, decl_type);
3268 Expression exp = Constantify (o, t);
3274 if (e is EventExpr) {
3276 // If the event is local to this class, we transform ourselves into
3279 EventExpr ee = (EventExpr) e;
3281 Expression ml = MemberLookup (
3282 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3283 MemberTypes.Event, AllBindingFlags, Location);
3286 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3290 // If this happens, then we have an event with its own
3291 // accessors and private field etc so there's no need
3292 // to transform ourselves : we should instead flag an error
3294 Assign.error70 (ee.EventInfo, Location);
3298 ml = ExprClassFromMemberInfo (ec, mi, Location);
3301 Report.Error (-200, Location, "Internal error!!");
3305 Expression instance_expr;
3307 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3310 instance_expr = null;
3312 instance_expr = ec.This;
3314 instance_expr = instance_expr.Resolve (ec);
3316 if (instance_expr != null)
3317 instance_expr = instance_expr.Resolve (ec);
3319 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3328 return MemberStaticCheck (e);
3333 public override void Emit (EmitContext ec)
3336 // If this is ever reached, then we failed to
3337 // find the name as a namespace
3340 Error (103, Location, "The name `" + Name +
3341 "' does not exist in the class `" +
3342 ec.DeclSpace.Name + "'");
3347 /// Fully resolved expression that evaluates to a type
3349 public class TypeExpr : Expression {
3350 public TypeExpr (Type t)
3353 eclass = ExprClass.Type;
3356 override public Expression DoResolve (EmitContext ec)
3361 override public void Emit (EmitContext ec)
3363 throw new Exception ("Implement me");
3368 /// MethodGroup Expression.
3370 /// This is a fully resolved expression that evaluates to a type
3372 public class MethodGroupExpr : Expression {
3373 public MethodBase [] Methods;
3375 Expression instance_expression = null;
3377 public MethodGroupExpr (MemberInfo [] mi, Location l)
3379 Methods = new MethodBase [mi.Length];
3380 mi.CopyTo (Methods, 0);
3381 eclass = ExprClass.MethodGroup;
3382 type = TypeManager.object_type;
3386 public MethodGroupExpr (ArrayList list, Location l)
3388 Methods = new MethodBase [list.Count];
3391 list.CopyTo (Methods, 0);
3393 foreach (MemberInfo m in list){
3394 if (!(m is MethodBase)){
3395 Console.WriteLine ("Name " + m.Name);
3396 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3402 eclass = ExprClass.MethodGroup;
3403 type = TypeManager.object_type;
3407 // `A method group may have associated an instance expression'
3409 public Expression InstanceExpression {
3411 return instance_expression;
3415 instance_expression = value;
3419 override public Expression DoResolve (EmitContext ec)
3424 public void ReportUsageError ()
3426 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3427 Methods [0].Name + "()' is referenced without parentheses");
3430 override public void Emit (EmitContext ec)
3432 ReportUsageError ();
3435 bool RemoveMethods (bool keep_static)
3437 ArrayList smethods = new ArrayList ();
3438 int top = Methods.Length;
3441 for (i = 0; i < top; i++){
3442 MethodBase mb = Methods [i];
3444 if (mb.IsStatic == keep_static)
3448 if (smethods.Count == 0)
3451 Methods = new MethodBase [smethods.Count];
3452 smethods.CopyTo (Methods, 0);
3458 /// Removes any instance methods from the MethodGroup, returns
3459 /// false if the resulting set is empty.
3461 public bool RemoveInstanceMethods ()
3463 return RemoveMethods (true);
3467 /// Removes any static methods from the MethodGroup, returns
3468 /// false if the resulting set is empty.
3470 public bool RemoveStaticMethods ()
3472 return RemoveMethods (false);
3477 /// Fully resolved expression that evaluates to a Field
3479 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3480 public readonly FieldInfo FieldInfo;
3481 public Expression InstanceExpression;
3484 public FieldExpr (FieldInfo fi, Location l)
3487 eclass = ExprClass.Variable;
3488 type = fi.FieldType;
3492 override public Expression DoResolve (EmitContext ec)
3494 if (!FieldInfo.IsStatic){
3495 if (InstanceExpression == null){
3496 throw new Exception ("non-static FieldExpr without instance var\n" +
3497 "You have to assign the Instance variable\n" +
3498 "Of the FieldExpr to set this\n");
3501 InstanceExpression = InstanceExpression.Resolve (ec);
3502 if (InstanceExpression == null)
3509 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3511 Expression e = DoResolve (ec);
3516 if (!FieldInfo.IsInitOnly)
3520 // InitOnly fields can only be assigned in constructors
3523 if (ec.IsConstructor)
3526 Report.Error (191, loc,
3527 "Readonly field can not be assigned outside " +
3528 "of constructor or variable initializer");
3533 override public void Emit (EmitContext ec)
3535 ILGenerator ig = ec.ig;
3536 bool is_volatile = false;
3538 if (FieldInfo is FieldBuilder){
3539 FieldBase f = TypeManager.GetField (FieldInfo);
3541 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3544 f.status |= Field.Status.USED;
3547 if (FieldInfo.IsStatic){
3549 ig.Emit (OpCodes.Volatile);
3551 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3553 if (InstanceExpression.Type.IsValueType){
3555 LocalTemporary tempo = null;
3557 if (!(InstanceExpression is IMemoryLocation)){
3558 tempo = new LocalTemporary (
3559 ec, InstanceExpression.Type);
3561 InstanceExpression.Emit (ec);
3565 ml = (IMemoryLocation) InstanceExpression;
3567 ml.AddressOf (ec, AddressOp.Load);
3569 InstanceExpression.Emit (ec);
3572 ig.Emit (OpCodes.Volatile);
3574 ig.Emit (OpCodes.Ldfld, FieldInfo);
3578 public void EmitAssign (EmitContext ec, Expression source)
3580 bool is_static = FieldInfo.IsStatic;
3581 ILGenerator ig = ec.ig;
3584 Expression instance = InstanceExpression;
3586 if (instance.Type.IsValueType){
3587 if (instance is IMemoryLocation){
3588 IMemoryLocation ml = (IMemoryLocation) instance;
3590 ml.AddressOf (ec, AddressOp.Store);
3592 throw new Exception ("The " + instance + " of type " +
3594 " represents a ValueType and does " +
3595 "not implement IMemoryLocation");
3601 if (FieldInfo is FieldBuilder){
3602 FieldBase f = TypeManager.GetField (FieldInfo);
3604 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3605 ig.Emit (OpCodes.Volatile);
3609 ig.Emit (OpCodes.Stsfld, FieldInfo);
3611 ig.Emit (OpCodes.Stfld, FieldInfo);
3613 if (FieldInfo is FieldBuilder){
3614 FieldBase f = TypeManager.GetField (FieldInfo);
3616 f.status |= Field.Status.ASSIGNED;
3620 public void AddressOf (EmitContext ec, AddressOp mode)
3622 ILGenerator ig = ec.ig;
3624 if (FieldInfo is FieldBuilder){
3625 FieldBase f = TypeManager.GetField (FieldInfo);
3626 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3627 ig.Emit (OpCodes.Volatile);
3630 if (FieldInfo is FieldBuilder){
3631 FieldBase f = TypeManager.GetField (FieldInfo);
3633 if ((mode & AddressOp.Store) != 0)
3634 f.status |= Field.Status.ASSIGNED;
3635 if ((mode & AddressOp.Load) != 0)
3636 f.status |= Field.Status.USED;
3640 // Handle initonly fields specially: make a copy and then
3641 // get the address of the copy.
3643 if (FieldInfo.IsInitOnly){
3647 local = ig.DeclareLocal (type);
3648 ig.Emit (OpCodes.Stloc, local);
3649 ig.Emit (OpCodes.Ldloca, local);
3653 if (FieldInfo.IsStatic)
3654 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3656 InstanceExpression.Emit (ec);
3657 ig.Emit (OpCodes.Ldflda, FieldInfo);
3663 /// Expression that evaluates to a Property. The Assign class
3664 /// might set the `Value' expression if we are in an assignment.
3666 /// This is not an LValue because we need to re-write the expression, we
3667 /// can not take data from the stack and store it.
3669 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3670 public readonly PropertyInfo PropertyInfo;
3671 public readonly bool IsStatic;
3673 MethodInfo [] Accessors;
3676 Expression instance_expr;
3678 public PropertyExpr (PropertyInfo pi, Location l)
3681 eclass = ExprClass.PropertyAccess;
3684 Accessors = TypeManager.GetAccessors (pi);
3686 if (Accessors != null)
3687 for (int i = 0; i < Accessors.Length; i++){
3688 if (Accessors [i] != null)
3689 if (Accessors [i].IsStatic)
3693 Accessors = new MethodInfo [2];
3695 type = pi.PropertyType;
3699 // The instance expression associated with this expression
3701 public Expression InstanceExpression {
3703 instance_expr = value;
3707 return instance_expr;
3711 public bool VerifyAssignable ()
3713 if (!PropertyInfo.CanWrite){
3714 Report.Error (200, loc,
3715 "The property `" + PropertyInfo.Name +
3716 "' can not be assigned to, as it has not set accessor");
3723 override public Expression DoResolve (EmitContext ec)
3725 if (!PropertyInfo.CanRead){
3726 Report.Error (154, loc,
3727 "The property `" + PropertyInfo.Name +
3728 "' can not be used in " +
3729 "this context because it lacks a get accessor");
3733 type = PropertyInfo.PropertyType;
3738 override public void Emit (EmitContext ec)
3740 MethodInfo method = Accessors [0];
3743 // Special case: length of single dimension array is turned into ldlen
3745 if (method == TypeManager.int_array_get_length){
3746 Type iet = instance_expr.Type;
3748 if (iet.GetArrayRank () == 1){
3749 instance_expr.Emit (ec);
3750 ec.ig.Emit (OpCodes.Ldlen);
3755 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
3760 // Implements the IAssignMethod interface for assignments
3762 public void EmitAssign (EmitContext ec, Expression source)
3764 Argument arg = new Argument (source, Argument.AType.Expression);
3765 ArrayList args = new ArrayList ();
3768 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3771 override public void EmitStatement (EmitContext ec)
3774 ec.ig.Emit (OpCodes.Pop);
3779 /// Fully resolved expression that evaluates to an Event
3781 public class EventExpr : Expression {
3782 public readonly EventInfo EventInfo;
3784 public Expression InstanceExpression;
3786 public readonly bool IsStatic;
3788 MethodInfo add_accessor, remove_accessor;
3790 public EventExpr (EventInfo ei, Location loc)
3794 eclass = ExprClass.EventAccess;
3796 add_accessor = TypeManager.GetAddMethod (ei);
3797 remove_accessor = TypeManager.GetRemoveMethod (ei);
3799 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3802 if (EventInfo is MyEventBuilder)
3803 type = ((MyEventBuilder) EventInfo).EventType;
3805 type = EventInfo.EventHandlerType;
3808 override public Expression DoResolve (EmitContext ec)
3810 // We are born fully resolved
3814 override public void Emit (EmitContext ec)
3816 throw new Exception ("Should not happen I think");
3819 public void EmitAddOrRemove (EmitContext ec, Expression source)
3821 Expression handler = ((Binary) source).Right;
3823 Argument arg = new Argument (handler, Argument.AType.Expression);
3824 ArrayList args = new ArrayList ();
3828 if (((Binary) source).Oper == Binary.Operator.Addition)
3829 Invocation.EmitCall (
3830 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3832 Invocation.EmitCall (
3833 ec, false, IsStatic, InstanceExpression, remove_accessor, args);