2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 /// This interface is implemented by variables
42 public interface IMemoryLocation {
44 /// The AddressOf method should generate code that loads
45 /// the address of the object and leaves it on the stack
47 void AddressOf (EmitContext ec);
51 /// Base class for expressions
53 public abstract class Expression {
54 public ExprClass eclass;
68 /// Utility wrapper routine for Error, just to beautify the code
70 static protected void Error (int error, string s)
72 Report.Error (error, s);
75 static protected void Error (int error, Location loc, string s)
77 Report.Error (error, loc, s);
81 /// Utility wrapper routine for Warning, just to beautify the code
83 static protected void Warning (int warning, string s)
85 Report.Warning (warning, s);
88 static public void error30 (Location loc, Type source, Type target)
90 Report.Error (30, loc, "Cannot convert type '" +
91 TypeManager.CSharpName (source) + "' to '" +
92 TypeManager.CSharpName (target) + "'");
96 /// Performs semantic analysis on the Expression
100 /// The Resolve method is invoked to perform the semantic analysis
103 /// The return value is an expression (it can be the
104 /// same expression in some cases) or a new
105 /// expression that better represents this node.
107 /// For example, optimizations of Unary (LiteralInt)
108 /// would return a new LiteralInt with a negated
111 /// If there is an error during semantic analysis,
112 /// then an error should be reported (using Report)
113 /// and a null value should be returned.
115 /// There are two side effects expected from calling
116 /// Resolve(): the the field variable "eclass" should
117 /// be set to any value of the enumeration
118 /// `ExprClass' and the type variable should be set
119 /// to a valid type (this is the type of the
122 public abstract Expression DoResolve (EmitContext ec);
124 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
126 return DoResolve (ec);
130 /// Resolves an expression and performs semantic analysis on it.
134 /// Currently Resolve wraps DoResolve to perform sanity
135 /// checking and assertion checking on what we expect from Resolve.
137 public Expression Resolve (EmitContext ec)
139 Expression e = DoResolve (ec);
142 if (e is SimpleName){
143 SimpleName s = (SimpleName) e;
147 "The name `" + s.Name + "' could not be found in `" +
148 ec.TypeContainer.Name + "'");
152 if (e.eclass == ExprClass.Invalid)
153 throw new Exception ("Expression " + e.GetType () +
154 " ExprClass is Invalid after resolve");
156 if (e.eclass != ExprClass.MethodGroup)
158 throw new Exception (
159 "Expression " + e.GetType () +
160 " did not set its type after Resolve\n" +
161 "called from: " + this.GetType ());
168 /// Performs expression resolution and semantic analysis, but
169 /// allows SimpleNames to be returned.
173 /// This is used by MemberAccess to construct long names that can not be
174 /// partially resolved (namespace-qualified names for example).
176 public Expression ResolveWithSimpleName (EmitContext ec)
180 if (this is SimpleName)
181 e = ((SimpleName) this).DoResolveAllowStatic (ec);
189 if (e.eclass == ExprClass.Invalid)
190 throw new Exception ("Expression " + e +
191 " ExprClass is Invalid after resolve");
193 if (e.eclass != ExprClass.MethodGroup)
195 throw new Exception ("Expression " + e +
196 " did not set its type after Resolve");
203 /// Resolves an expression for LValue assignment
207 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
208 /// checking and assertion checking on what we expect from Resolve
210 public Expression ResolveLValue (EmitContext ec, Expression right_side)
212 Expression e = DoResolveLValue (ec, right_side);
215 if (e is SimpleName){
216 SimpleName s = (SimpleName) e;
220 "The name `" + s.Name + "' could not be found in `" +
221 ec.TypeContainer.Name + "'");
225 if (e.eclass == ExprClass.Invalid)
226 throw new Exception ("Expression " + e +
227 " ExprClass is Invalid after resolve");
229 if (e.eclass != ExprClass.MethodGroup)
231 throw new Exception ("Expression " + e +
232 " did not set its type after Resolve");
239 /// Emits the code for the expression
243 /// The Emit method is invoked to generate the code
244 /// for the expression.
246 public abstract void Emit (EmitContext ec);
249 /// Protected constructor. Only derivate types should
250 /// be able to be created
253 protected Expression ()
255 eclass = ExprClass.Invalid;
260 /// Returns a literalized version of a literal FieldInfo
264 /// The possible return values are:
265 /// IntConstant, UIntConstant
266 /// LongLiteral, ULongConstant
267 /// FloatConstant, DoubleConstant
270 /// The value returned is already resolved.
272 public static Constant Constantify (object v, Type t)
274 if (t == TypeManager.int32_type)
275 return new IntConstant ((int) v);
276 else if (t == TypeManager.uint32_type)
277 return new UIntConstant ((uint) v);
278 else if (t == TypeManager.int64_type)
279 return new LongConstant ((long) v);
280 else if (t == TypeManager.uint64_type)
281 return new ULongConstant ((ulong) v);
282 else if (t == TypeManager.float_type)
283 return new FloatConstant ((float) v);
284 else if (t == TypeManager.double_type)
285 return new DoubleConstant ((double) v);
286 else if (t == TypeManager.string_type)
287 return new StringConstant ((string) v);
288 else if (t == TypeManager.short_type)
289 return new ShortConstant ((short)v);
290 else if (t == TypeManager.ushort_type)
291 return new UShortConstant ((ushort)v);
292 else if (t == TypeManager.sbyte_type)
293 return new SByteConstant (((sbyte)v));
294 else if (t == TypeManager.byte_type)
295 return new ByteConstant ((byte)v);
296 else if (t == TypeManager.char_type)
297 return new CharConstant ((char)v);
298 else if (TypeManager.IsEnumType (t)){
299 Expression e = Constantify (v, v.GetType ());
301 return new EnumConstant ((Constant) e, t);
303 throw new Exception ("Unknown type for constant (" + t +
308 /// Returns a fully formed expression after a MemberLookup
310 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
313 return new EventExpr ((EventInfo) mi, loc);
314 else if (mi is FieldInfo)
315 return new FieldExpr ((FieldInfo) mi, loc);
316 else if (mi is PropertyInfo)
317 return new PropertyExpr ((PropertyInfo) mi, loc);
318 else if (mi is Type){
319 return new TypeExpr ((System.Type) mi);
326 // Returns whether the array of memberinfos contains the given method
328 static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
330 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
332 foreach (MethodBase method in array){
333 if (method.Name != new_method.Name)
336 Type [] old_args = TypeManager.GetArgumentTypes (method);
337 int old_count = old_args.Length;
340 if (new_args.Length != old_count)
343 for (i = 0; i < old_count; i++){
344 if (old_args [i] != new_args [i])
350 if (!(method is MethodInfo && new_method is MethodInfo))
353 if (((MethodInfo) method).ReturnType == ((MethodInfo) new_method).ReturnType)
360 // We copy methods from `new_members' into `target_list' if the signature
361 // for the method from in the new list does not exist in the target_list
363 // The name is assumed to be the same.
365 public static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
367 if (target_list == null){
368 target_list = new ArrayList ();
370 target_list.AddRange (new_members);
374 MemberInfo [] target_array = new MemberInfo [target_list.Count];
375 target_list.CopyTo (target_array, 0);
377 foreach (MemberInfo mi in new_members){
378 MethodBase new_method = (MethodBase) mi;
380 if (!ArrayContainsMethod (target_array, new_method))
381 target_list.Add (new_method);
387 // FIXME: Probably implement a cache for (t,name,current_access_set)?
389 // This code could use some optimizations, but we need to do some
390 // measurements. For example, we could use a delegate to `flag' when
391 // something can not any longer be a method-group (because it is something
395 // If the return value is an Array, then it is an array of
398 // If the return value is an MemberInfo, it is anything, but a Method
402 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
403 // the arguments here and have MemberLookup return only the methods that
404 // match the argument count/type, unlike we are doing now (we delay this
407 // This is so we can catch correctly attempts to invoke instance methods
408 // from a static body (scan for error 120 in ResolveSimpleName).
411 // FIXME: Potential optimization, have a static ArrayList
414 public static Expression MemberLookup (EmitContext ec, Type t, string name,
415 MemberTypes mt, BindingFlags bf, Location loc)
417 Type source_type = ec.ContainerType;
419 if (source_type != null){
420 if (source_type == t || source_type.IsSubclassOf (t))
421 bf |= BindingFlags.NonPublic;
425 // Lookup for members starting in the type requested and going
426 // up the hierarchy until a match is found.
428 // As soon as a non-method match is found, we return.
430 // If methods are found though, then the search proceeds scanning
431 // for more public methods in the hierarchy with signatures that
432 // do not match any of the signatures found so far.
434 ArrayList method_list = null;
435 Type current_type = t;
436 bool searching = true;
440 mi = RootContext.TypeManager.FindMembers (
441 current_type, mt, bf | BindingFlags.DeclaredOnly,
442 System.Type.FilterName, name);
444 if (current_type == TypeManager.object_type)
447 current_type = current_type.BaseType;
450 // This happens with interfaces, they have a null
453 if (current_type == null)
460 int count = mi.Length;
465 if (count == 1 && !(mi [0] is MethodBase))
466 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
469 // We found methods, turn the search into "method scan"
472 method_list = CopyNewMethods (method_list, mi);
473 mt &= (MemberTypes.Method | MemberTypes.Constructor);
476 if (method_list != null && method_list.Count > 0)
477 return new MethodGroupExpr (method_list);
482 public const MemberTypes AllMemberTypes =
483 MemberTypes.Constructor |
487 MemberTypes.NestedType |
488 MemberTypes.Property;
490 public const BindingFlags AllBindingFlags =
491 BindingFlags.Public |
492 BindingFlags.Static |
493 BindingFlags.Instance;
495 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
497 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
501 /// This is a wrapper for MemberLookup that is not used to "probe", but
502 /// to find a final definition. If the final definition is not found, we
503 /// look for private members and display a useful debugging message if we
506 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
511 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
516 e = MemberLookup (ec, t, name, AllMemberTypes,
517 AllBindingFlags | BindingFlags.NonPublic, loc);
520 117, loc, "`" + t + "' does not contain a definition " +
521 "for `" + name + "'");
524 122, loc, "`" + t + "." + name +
525 "' is inaccessible due to its protection level");
531 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
533 Type expr_type = expr.Type;
535 if (target_type == TypeManager.object_type) {
537 // A pointer type cannot be converted to object
539 if (expr_type.IsPointer)
542 if (expr_type.IsClass)
543 return new EmptyCast (expr, target_type);
544 if (expr_type.IsValueType)
545 return new BoxedCast (expr);
546 } else if (expr_type.IsSubclassOf (target_type)) {
547 return new EmptyCast (expr, target_type);
550 // This code is kind of mirrored inside StandardConversionExists
551 // with the small distinction that we only probe there
553 // Always ensure that the code here and there is in sync
555 // from the null type to any reference-type.
556 if (expr is NullLiteral && !target_type.IsValueType)
557 return new EmptyCast (expr, target_type);
559 // from any class-type S to any interface-type T.
560 if (expr_type.IsClass && target_type.IsInterface) {
561 if (TypeManager.ImplementsInterface (expr_type, target_type))
562 return new EmptyCast (expr, target_type);
567 // from any interface type S to interface-type T.
568 if (expr_type.IsInterface && target_type.IsInterface) {
570 if (TypeManager.ImplementsInterface (expr_type, target_type))
571 return new EmptyCast (expr, target_type);
576 // from an array-type S to an array-type of type T
577 if (expr_type.IsArray && target_type.IsArray) {
578 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
580 Type expr_element_type = expr_type.GetElementType ();
581 Type target_element_type = target_type.GetElementType ();
583 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
584 if (StandardConversionExists (expr_element_type,
585 target_element_type))
586 return new EmptyCast (expr, target_type);
591 // from an array-type to System.Array
592 if (expr_type.IsArray && target_type == TypeManager.array_type)
593 return new EmptyCast (expr, target_type);
595 // from any delegate type to System.Delegate
596 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
597 target_type == TypeManager.delegate_type)
598 return new EmptyCast (expr, target_type);
600 // from any array-type or delegate type into System.ICloneable.
601 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
602 if (target_type == TypeManager.icloneable_type)
603 return new EmptyCast (expr, target_type);
613 /// Handles expressions like this: decimal d; d = 1;
614 /// and changes them into: decimal d; d = new System.Decimal (1);
616 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
618 ArrayList args = new ArrayList ();
620 args.Add (new Argument (expr, Argument.AType.Expression));
622 Expression ne = new New (target.FullName, args,
625 return ne.Resolve (ec);
629 /// Implicit Numeric Conversions.
631 /// expr is the expression to convert, returns a new expression of type
632 /// target_type or null if an implicit conversion is not possible.
634 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
635 Type target_type, Location loc)
637 Type expr_type = expr.Type;
640 // Attempt to do the implicit constant expression conversions
642 if (expr is IntConstant){
645 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
649 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
651 // Try the implicit constant expression conversion
652 // from long to ulong, instead of a nice routine,
655 long v = ((LongConstant) expr).Value;
657 return new ULongConstant ((ulong) v);
660 if (expr_type == TypeManager.sbyte_type){
662 // From sbyte to short, int, long, float, double.
664 if (target_type == TypeManager.int32_type)
665 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
666 if (target_type == TypeManager.int64_type)
667 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
668 if (target_type == TypeManager.double_type)
669 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
670 if (target_type == TypeManager.float_type)
671 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
672 if (target_type == TypeManager.short_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
674 if (target_type == TypeManager.decimal_type)
675 return InternalTypeConstructor (ec, expr, target_type);
676 } else if (expr_type == TypeManager.byte_type){
678 // From byte to short, ushort, int, uint, long, ulong, float, double
680 if ((target_type == TypeManager.short_type) ||
681 (target_type == TypeManager.ushort_type) ||
682 (target_type == TypeManager.int32_type) ||
683 (target_type == TypeManager.uint32_type))
684 return new EmptyCast (expr, target_type);
686 if (target_type == TypeManager.uint64_type)
687 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
688 if (target_type == TypeManager.int64_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
690 if (target_type == TypeManager.float_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
692 if (target_type == TypeManager.double_type)
693 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
694 if (target_type == TypeManager.decimal_type)
695 return InternalTypeConstructor (ec, expr, target_type);
696 } else if (expr_type == TypeManager.short_type){
698 // From short to int, long, float, double
700 if (target_type == TypeManager.int32_type)
701 return new EmptyCast (expr, target_type);
702 if (target_type == TypeManager.int64_type)
703 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
704 if (target_type == TypeManager.double_type)
705 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
706 if (target_type == TypeManager.float_type)
707 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
708 if (target_type == TypeManager.decimal_type)
709 return InternalTypeConstructor (ec, expr, target_type);
710 } else if (expr_type == TypeManager.ushort_type){
712 // From ushort to int, uint, long, ulong, float, double
714 if (target_type == TypeManager.uint32_type)
715 return new EmptyCast (expr, target_type);
717 if (target_type == TypeManager.uint64_type)
718 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
719 if (target_type == TypeManager.int32_type)
720 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
721 if (target_type == TypeManager.int64_type)
722 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
723 if (target_type == TypeManager.double_type)
724 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
725 if (target_type == TypeManager.float_type)
726 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
727 if (target_type == TypeManager.decimal_type)
728 return InternalTypeConstructor (ec, expr, target_type);
729 } else if (expr_type == TypeManager.int32_type){
731 // From int to long, float, double
733 if (target_type == TypeManager.int64_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
735 if (target_type == TypeManager.double_type)
736 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
737 if (target_type == TypeManager.float_type)
738 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
739 if (target_type == TypeManager.decimal_type)
740 return InternalTypeConstructor (ec, expr, target_type);
741 } else if (expr_type == TypeManager.uint32_type){
743 // From uint to long, ulong, float, double
745 if (target_type == TypeManager.int64_type)
746 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
747 if (target_type == TypeManager.uint64_type)
748 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
749 if (target_type == TypeManager.double_type)
750 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
752 if (target_type == TypeManager.float_type)
753 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
755 if (target_type == TypeManager.decimal_type)
756 return InternalTypeConstructor (ec, expr, target_type);
757 } else if ((expr_type == TypeManager.uint64_type) ||
758 (expr_type == TypeManager.int64_type)){
760 // From long/ulong to float, double
762 if (target_type == TypeManager.double_type)
763 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
765 if (target_type == TypeManager.float_type)
766 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
768 if (target_type == TypeManager.decimal_type)
769 return InternalTypeConstructor (ec, expr, target_type);
770 } else if (expr_type == TypeManager.char_type){
772 // From char to ushort, int, uint, long, ulong, float, double
774 if ((target_type == TypeManager.ushort_type) ||
775 (target_type == TypeManager.int32_type) ||
776 (target_type == TypeManager.uint32_type))
777 return new EmptyCast (expr, target_type);
778 if (target_type == TypeManager.uint64_type)
779 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
780 if (target_type == TypeManager.int64_type)
781 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
782 if (target_type == TypeManager.float_type)
783 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
784 if (target_type == TypeManager.double_type)
785 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
786 if (target_type == TypeManager.decimal_type)
787 return InternalTypeConstructor (ec, expr, target_type);
788 } else if (expr_type == TypeManager.float_type){
792 if (target_type == TypeManager.double_type)
793 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
800 /// Determines if a standard implicit conversion exists from
801 /// expr_type to target_type
803 public static bool StandardConversionExists (Type expr_type, Type target_type)
805 if (expr_type == target_type)
808 // First numeric conversions
810 if (expr_type == TypeManager.sbyte_type){
812 // From sbyte to short, int, long, float, double.
814 if ((target_type == TypeManager.int32_type) ||
815 (target_type == TypeManager.int64_type) ||
816 (target_type == TypeManager.double_type) ||
817 (target_type == TypeManager.float_type) ||
818 (target_type == TypeManager.short_type) ||
819 (target_type == TypeManager.decimal_type))
822 } else if (expr_type == TypeManager.byte_type){
824 // From byte to short, ushort, int, uint, long, ulong, float, double
826 if ((target_type == TypeManager.short_type) ||
827 (target_type == TypeManager.ushort_type) ||
828 (target_type == TypeManager.int32_type) ||
829 (target_type == TypeManager.uint32_type) ||
830 (target_type == TypeManager.uint64_type) ||
831 (target_type == TypeManager.int64_type) ||
832 (target_type == TypeManager.float_type) ||
833 (target_type == TypeManager.double_type) ||
834 (target_type == TypeManager.decimal_type))
837 } else if (expr_type == TypeManager.short_type){
839 // From short to int, long, float, double
841 if ((target_type == TypeManager.int32_type) ||
842 (target_type == TypeManager.int64_type) ||
843 (target_type == TypeManager.double_type) ||
844 (target_type == TypeManager.float_type) ||
845 (target_type == TypeManager.decimal_type))
848 } else if (expr_type == TypeManager.ushort_type){
850 // From ushort to int, uint, long, ulong, float, double
852 if ((target_type == TypeManager.uint32_type) ||
853 (target_type == TypeManager.uint64_type) ||
854 (target_type == TypeManager.int32_type) ||
855 (target_type == TypeManager.int64_type) ||
856 (target_type == TypeManager.double_type) ||
857 (target_type == TypeManager.float_type) ||
858 (target_type == TypeManager.decimal_type))
861 } else if (expr_type == TypeManager.int32_type){
863 // From int to long, float, double
865 if ((target_type == TypeManager.int64_type) ||
866 (target_type == TypeManager.double_type) ||
867 (target_type == TypeManager.float_type) ||
868 (target_type == TypeManager.decimal_type))
871 } else if (expr_type == TypeManager.uint32_type){
873 // From uint to long, ulong, float, double
875 if ((target_type == TypeManager.int64_type) ||
876 (target_type == TypeManager.uint64_type) ||
877 (target_type == TypeManager.double_type) ||
878 (target_type == TypeManager.float_type) ||
879 (target_type == TypeManager.decimal_type))
882 } else if ((expr_type == TypeManager.uint64_type) ||
883 (expr_type == TypeManager.int64_type)) {
885 // From long/ulong to float, double
887 if ((target_type == TypeManager.double_type) ||
888 (target_type == TypeManager.float_type) ||
889 (target_type == TypeManager.decimal_type))
892 } else if (expr_type == TypeManager.char_type){
894 // From char to ushort, int, uint, long, ulong, float, double
896 if ((target_type == TypeManager.ushort_type) ||
897 (target_type == TypeManager.int32_type) ||
898 (target_type == TypeManager.uint32_type) ||
899 (target_type == TypeManager.uint64_type) ||
900 (target_type == TypeManager.int64_type) ||
901 (target_type == TypeManager.float_type) ||
902 (target_type == TypeManager.double_type) ||
903 (target_type == TypeManager.decimal_type))
906 } else if (expr_type == TypeManager.float_type){
910 if (target_type == TypeManager.double_type)
914 // Next reference conversions
916 if (target_type == TypeManager.object_type) {
917 if ((expr_type.IsClass) ||
918 (expr_type.IsValueType))
921 } else if (expr_type.IsSubclassOf (target_type)) {
925 // Please remember that all code below actuall comes
926 // from ImplicitReferenceConversion so make sure code remains in sync
928 // from any class-type S to any interface-type T.
929 if (expr_type.IsClass && target_type.IsInterface) {
930 if (TypeManager.ImplementsInterface (expr_type, target_type))
934 // from any interface type S to interface-type T.
935 // FIXME : Is it right to use IsAssignableFrom ?
936 if (expr_type.IsInterface && target_type.IsInterface)
937 if (target_type.IsAssignableFrom (expr_type))
940 // from an array-type S to an array-type of type T
941 if (expr_type.IsArray && target_type.IsArray) {
942 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
944 Type expr_element_type = expr_type.GetElementType ();
945 Type target_element_type = target_type.GetElementType ();
947 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
948 if (StandardConversionExists (expr_element_type,
949 target_element_type))
954 // from an array-type to System.Array
955 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
958 // from any delegate type to System.Delegate
959 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
960 target_type == TypeManager.delegate_type)
961 if (target_type.IsAssignableFrom (expr_type))
964 // from any array-type or delegate type into System.ICloneable.
965 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
966 if (target_type == TypeManager.icloneable_type)
969 // from the null type to any reference-type.
970 // FIXME : How do we do this ?
977 static EmptyExpression MyEmptyExpr;
979 /// Tells whether an implicit conversion exists from expr_type to
982 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
985 if (MyEmptyExpr == null)
986 MyEmptyExpr = new EmptyExpression (expr_type);
988 MyEmptyExpr.SetType (expr_type);
990 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
994 /// Finds "most encompassed type" according to the spec (13.4.2)
995 /// amongst the methods in the MethodGroupExpr which convert from a
996 /// type encompassing source_type
998 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
1002 for (int i = me.Methods.Length; i > 0; ) {
1005 MethodBase mb = me.Methods [i];
1006 ParameterData pd = Invocation.GetParameterData (mb);
1007 Type param_type = pd.ParameterType (0);
1009 if (StandardConversionExists (source_type, param_type)) {
1013 if (StandardConversionExists (param_type, best))
1022 /// Finds "most encompassing type" according to the spec (13.4.2)
1023 /// amongst the methods in the MethodGroupExpr which convert to a
1024 /// type encompassed by target_type
1026 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
1030 for (int i = me.Methods.Length; i > 0; ) {
1033 MethodInfo mi = (MethodInfo) me.Methods [i];
1034 Type ret_type = mi.ReturnType;
1036 if (StandardConversionExists (ret_type, target)) {
1040 if (!StandardConversionExists (ret_type, best))
1052 /// User-defined Implicit conversions
1054 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1055 Type target, Location loc)
1057 return UserDefinedConversion (ec, source, target, loc, false);
1061 /// User-defined Explicit conversions
1063 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1064 Type target, Location loc)
1066 return UserDefinedConversion (ec, source, target, loc, true);
1070 /// User-defined conversions
1072 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1073 Type target, Location loc,
1074 bool look_for_explicit)
1076 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1077 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1079 MethodBase method = null;
1080 Type source_type = source.Type;
1084 // If we have a boolean type, we need to check for the True operator
1086 // FIXME : How does the False operator come into the picture ?
1087 // FIXME : This doesn't look complete and very correct !
1088 if (target == TypeManager.bool_type)
1089 op_name = "op_True";
1091 op_name = "op_Implicit";
1093 mg1 = MemberLookup (ec, source_type, op_name, loc);
1095 if (source_type.BaseType != null)
1096 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1098 mg3 = MemberLookup (ec, target, op_name, loc);
1100 if (target.BaseType != null)
1101 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1103 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1104 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1106 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1108 MethodGroupExpr union4 = null;
1110 if (look_for_explicit) {
1112 op_name = "op_Explicit";
1114 mg5 = MemberLookup (ec, source_type, op_name, loc);
1116 if (source_type.BaseType != null)
1117 mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1119 mg7 = MemberLookup (ec, target, op_name, loc);
1121 if (target.BaseType != null)
1122 mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1124 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1125 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1127 union4 = Invocation.MakeUnionSet (union5, union6);
1130 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1132 if (union != null) {
1134 Type most_specific_source, most_specific_target;
1136 most_specific_source = FindMostEncompassedType (union, source_type);
1137 if (most_specific_source == null)
1140 most_specific_target = FindMostEncompassingType (union, target);
1141 if (most_specific_target == null)
1146 for (int i = union.Methods.Length; i > 0;) {
1149 MethodBase mb = union.Methods [i];
1150 ParameterData pd = Invocation.GetParameterData (mb);
1151 MethodInfo mi = (MethodInfo) union.Methods [i];
1153 if (pd.ParameterType (0) == most_specific_source &&
1154 mi.ReturnType == most_specific_target) {
1160 if (method == null || count > 1) {
1161 Report.Error (-11, loc, "Ambiguous user defined conversion");
1166 // This will do the conversion to the best match that we
1167 // found. Now we need to perform an implict standard conversion
1168 // if the best match was not the type that we were requested
1171 if (look_for_explicit)
1172 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1174 source = ConvertImplicitStandard (ec, source,
1175 most_specific_source, loc);
1180 e = new UserCast ((MethodInfo) method, source);
1182 if (e.Type != target){
1183 if (!look_for_explicit)
1184 e = ConvertImplicitStandard (ec, e, target, loc);
1186 e = ConvertExplicitStandard (ec, e, target, loc);
1197 /// Converts implicitly the resolved expression `expr' into the
1198 /// `target_type'. It returns a new expression that can be used
1199 /// in a context that expects a `target_type'.
1201 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1202 Type target_type, Location loc)
1204 Type expr_type = expr.Type;
1207 if (expr_type == target_type)
1210 if (target_type == null)
1211 throw new Exception ("Target type is null");
1213 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1217 e = ImplicitUserConversion (ec, expr, target_type, loc);
1226 /// Attempts to apply the `Standard Implicit
1227 /// Conversion' rules to the expression `expr' into
1228 /// the `target_type'. It returns a new expression
1229 /// that can be used in a context that expects a
1232 /// This is different from `ConvertImplicit' in that the
1233 /// user defined implicit conversions are excluded.
1235 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1236 Type target_type, Location loc)
1238 Type expr_type = expr.Type;
1241 if (expr_type == target_type)
1244 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1248 e = ImplicitReferenceConversion (expr, target_type);
1252 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1253 IntLiteral i = (IntLiteral) expr;
1256 return new EmptyCast (expr, target_type);
1260 if (expr_type.IsPointer){
1261 if (target_type == TypeManager.void_ptr_type)
1262 return new EmptyCast (expr, target_type);
1265 // yep, comparing pointer types cant be done with
1266 // t1 == t2, we have to compare their element types.
1268 if (target_type.IsPointer){
1269 if (target_type.GetElementType()==expr_type.GetElementType())
1274 if (target_type.IsPointer){
1275 if (expr is NullLiteral)
1276 return new EmptyCast (expr, target_type);
1284 /// Attemps to perform an implict constant conversion of the IntConstant
1285 /// into a different data type using casts (See Implicit Constant
1286 /// Expression Conversions)
1288 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1290 int value = ic.Value;
1293 // FIXME: This should really return constants instead of EmptyCasts
1295 if (target_type == TypeManager.sbyte_type){
1296 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1297 return new SByteConstant ((sbyte) value);
1298 } else if (target_type == TypeManager.byte_type){
1299 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1300 return new ByteConstant ((byte) value);
1301 } else if (target_type == TypeManager.short_type){
1302 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1303 return new ShortConstant ((short) value);
1304 } else if (target_type == TypeManager.ushort_type){
1305 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1306 return new UShortConstant ((ushort) value);
1307 } else if (target_type == TypeManager.uint32_type){
1309 return new UIntConstant ((uint) value);
1310 } else if (target_type == TypeManager.uint64_type){
1312 // we can optimize this case: a positive int32
1313 // always fits on a uint64. But we need an opcode
1317 return new ULongConstant ((ulong) value);
1320 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1321 return new EnumConstant (ic, target_type);
1327 /// Attemptes to implicityly convert `target' into `type', using
1328 /// ConvertImplicit. If there is no implicit conversion, then
1329 /// an error is signaled
1331 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1332 Type target_type, Location loc)
1336 e = ConvertImplicit (ec, source, target_type, loc);
1340 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1342 "Double literal cannot be implicitly converted to " +
1343 "float type, use F suffix to create a float literal");
1346 string msg = "Cannot convert implicitly from `"+
1347 TypeManager.CSharpName (source.Type) + "' to `" +
1348 TypeManager.CSharpName (target_type) + "'";
1350 Error (29, loc, msg);
1356 /// Performs the explicit numeric conversions
1358 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1361 Type expr_type = expr.Type;
1363 if (expr_type == TypeManager.sbyte_type){
1365 // From sbyte to byte, ushort, uint, ulong, char
1367 if (target_type == TypeManager.byte_type)
1368 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1369 if (target_type == TypeManager.ushort_type)
1370 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1371 if (target_type == TypeManager.uint32_type)
1372 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1373 if (target_type == TypeManager.uint64_type)
1374 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1375 if (target_type == TypeManager.char_type)
1376 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1377 } else if (expr_type == TypeManager.byte_type){
1379 // From byte to sbyte and char
1381 if (target_type == TypeManager.sbyte_type)
1382 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1383 if (target_type == TypeManager.char_type)
1384 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1385 } else if (expr_type == TypeManager.short_type){
1387 // From short to sbyte, byte, ushort, uint, ulong, char
1389 if (target_type == TypeManager.sbyte_type)
1390 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1391 if (target_type == TypeManager.byte_type)
1392 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1393 if (target_type == TypeManager.ushort_type)
1394 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1395 if (target_type == TypeManager.uint32_type)
1396 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1397 if (target_type == TypeManager.uint64_type)
1398 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1399 if (target_type == TypeManager.char_type)
1400 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1401 } else if (expr_type == TypeManager.ushort_type){
1403 // From ushort to sbyte, byte, short, char
1405 if (target_type == TypeManager.sbyte_type)
1406 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1407 if (target_type == TypeManager.byte_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1409 if (target_type == TypeManager.short_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1411 if (target_type == TypeManager.char_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1413 } else if (expr_type == TypeManager.int32_type){
1415 // From int to sbyte, byte, short, ushort, uint, ulong, char
1417 if (target_type == TypeManager.sbyte_type)
1418 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1419 if (target_type == TypeManager.byte_type)
1420 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1421 if (target_type == TypeManager.short_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1423 if (target_type == TypeManager.ushort_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1425 if (target_type == TypeManager.uint32_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1427 if (target_type == TypeManager.uint64_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1429 if (target_type == TypeManager.char_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1431 } else if (expr_type == TypeManager.uint32_type){
1433 // From uint to sbyte, byte, short, ushort, int, char
1435 if (target_type == TypeManager.sbyte_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1437 if (target_type == TypeManager.byte_type)
1438 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1439 if (target_type == TypeManager.short_type)
1440 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1441 if (target_type == TypeManager.ushort_type)
1442 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1443 if (target_type == TypeManager.int32_type)
1444 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1445 if (target_type == TypeManager.char_type)
1446 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1447 } else if (expr_type == TypeManager.int64_type){
1449 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1451 if (target_type == TypeManager.sbyte_type)
1452 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1453 if (target_type == TypeManager.byte_type)
1454 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1455 if (target_type == TypeManager.short_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1457 if (target_type == TypeManager.ushort_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1459 if (target_type == TypeManager.int32_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1461 if (target_type == TypeManager.uint32_type)
1462 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1463 if (target_type == TypeManager.uint64_type)
1464 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1465 if (target_type == TypeManager.char_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1467 } else if (expr_type == TypeManager.uint64_type){
1469 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1471 if (target_type == TypeManager.sbyte_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1473 if (target_type == TypeManager.byte_type)
1474 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1475 if (target_type == TypeManager.short_type)
1476 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1477 if (target_type == TypeManager.ushort_type)
1478 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1479 if (target_type == TypeManager.int32_type)
1480 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1481 if (target_type == TypeManager.uint32_type)
1482 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1483 if (target_type == TypeManager.int64_type)
1484 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1485 if (target_type == TypeManager.char_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1487 } else if (expr_type == TypeManager.char_type){
1489 // From char to sbyte, byte, short
1491 if (target_type == TypeManager.sbyte_type)
1492 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1493 if (target_type == TypeManager.byte_type)
1494 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1495 if (target_type == TypeManager.short_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1497 } else if (expr_type == TypeManager.float_type){
1499 // From float to sbyte, byte, short,
1500 // ushort, int, uint, long, ulong, char
1503 if (target_type == TypeManager.sbyte_type)
1504 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1505 if (target_type == TypeManager.byte_type)
1506 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1507 if (target_type == TypeManager.short_type)
1508 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1509 if (target_type == TypeManager.ushort_type)
1510 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1511 if (target_type == TypeManager.int32_type)
1512 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1513 if (target_type == TypeManager.uint32_type)
1514 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1515 if (target_type == TypeManager.int64_type)
1516 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1517 if (target_type == TypeManager.uint64_type)
1518 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1519 if (target_type == TypeManager.char_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1521 if (target_type == TypeManager.decimal_type)
1522 return InternalTypeConstructor (ec, expr, target_type);
1523 } else if (expr_type == TypeManager.double_type){
1525 // From double to byte, byte, short,
1526 // ushort, int, uint, long, ulong,
1527 // char, float or decimal
1529 if (target_type == TypeManager.sbyte_type)
1530 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1531 if (target_type == TypeManager.byte_type)
1532 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1533 if (target_type == TypeManager.short_type)
1534 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1535 if (target_type == TypeManager.ushort_type)
1536 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1537 if (target_type == TypeManager.int32_type)
1538 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1539 if (target_type == TypeManager.uint32_type)
1540 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1541 if (target_type == TypeManager.int64_type)
1542 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1543 if (target_type == TypeManager.uint64_type)
1544 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1545 if (target_type == TypeManager.char_type)
1546 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1547 if (target_type == TypeManager.float_type)
1548 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1549 if (target_type == TypeManager.decimal_type)
1550 return InternalTypeConstructor (ec, expr, target_type);
1553 // decimal is taken care of by the op_Explicit methods.
1559 /// Returns whether an explicit reference conversion can be performed
1560 /// from source_type to target_type
1562 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1564 bool target_is_value_type = target_type.IsValueType;
1566 if (source_type == target_type)
1570 // From object to any reference type
1572 if (source_type == TypeManager.object_type && !target_is_value_type)
1576 // From any class S to any class-type T, provided S is a base class of T
1578 if (target_type.IsSubclassOf (source_type))
1582 // From any interface type S to any interface T provided S is not derived from T
1584 if (source_type.IsInterface && target_type.IsInterface){
1585 if (!target_type.IsSubclassOf (source_type))
1590 // From any class type S to any interface T, provides S is not sealed
1591 // and provided S does not implement T.
1593 if (target_type.IsInterface && !source_type.IsSealed &&
1594 !target_type.IsAssignableFrom (source_type))
1598 // From any interface-type S to to any class type T, provided T is not
1599 // sealed, or provided T implements S.
1601 if (source_type.IsInterface &&
1602 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1605 // From an array type S with an element type Se to an array type T with an
1606 // element type Te provided all the following are true:
1607 // * S and T differe only in element type, in other words, S and T
1608 // have the same number of dimensions.
1609 // * Both Se and Te are reference types
1610 // * An explicit referenc conversions exist from Se to Te
1612 if (source_type.IsArray && target_type.IsArray) {
1613 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1615 Type source_element_type = source_type.GetElementType ();
1616 Type target_element_type = target_type.GetElementType ();
1618 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1619 if (ExplicitReferenceConversionExists (source_element_type,
1620 target_element_type))
1626 // From System.Array to any array-type
1627 if (source_type == TypeManager.array_type &&
1628 target_type.IsSubclassOf (TypeManager.array_type)){
1633 // From System delegate to any delegate-type
1635 if (source_type == TypeManager.delegate_type &&
1636 target_type.IsSubclassOf (TypeManager.delegate_type))
1640 // From ICloneable to Array or Delegate types
1642 if (source_type == TypeManager.icloneable_type &&
1643 (target_type == TypeManager.array_type ||
1644 target_type == TypeManager.delegate_type))
1651 /// Implements Explicit Reference conversions
1653 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1655 Type source_type = source.Type;
1656 bool target_is_value_type = target_type.IsValueType;
1659 // From object to any reference type
1661 if (source_type == TypeManager.object_type && !target_is_value_type)
1662 return new ClassCast (source, target_type);
1666 // From any class S to any class-type T, provided S is a base class of T
1668 if (target_type.IsSubclassOf (source_type))
1669 return new ClassCast (source, target_type);
1672 // From any interface type S to any interface T provided S is not derived from T
1674 if (source_type.IsInterface && target_type.IsInterface){
1676 Type [] ifaces = source_type.GetInterfaces ();
1678 if (TypeManager.ImplementsInterface (source_type, target_type))
1681 return new ClassCast (source, target_type);
1685 // From any class type S to any interface T, provides S is not sealed
1686 // and provided S does not implement T.
1688 if (target_type.IsInterface && !source_type.IsSealed) {
1690 if (TypeManager.ImplementsInterface (source_type, target_type))
1693 return new ClassCast (source, target_type);
1698 // From any interface-type S to to any class type T, provided T is not
1699 // sealed, or provided T implements S.
1701 if (source_type.IsInterface) {
1703 if (target_type.IsSealed)
1706 if (TypeManager.ImplementsInterface (target_type, source_type))
1707 return new ClassCast (source, target_type);
1712 // From an array type S with an element type Se to an array type T with an
1713 // element type Te provided all the following are true:
1714 // * S and T differe only in element type, in other words, S and T
1715 // have the same number of dimensions.
1716 // * Both Se and Te are reference types
1717 // * An explicit referenc conversions exist from Se to Te
1719 if (source_type.IsArray && target_type.IsArray) {
1720 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1722 Type source_element_type = source_type.GetElementType ();
1723 Type target_element_type = target_type.GetElementType ();
1725 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1726 if (ExplicitReferenceConversionExists (source_element_type,
1727 target_element_type))
1728 return new ClassCast (source, target_type);
1733 // From System.Array to any array-type
1734 if (source_type == TypeManager.array_type &&
1735 target_type.IsSubclassOf (TypeManager.array_type)){
1736 return new ClassCast (source, target_type);
1740 // From System delegate to any delegate-type
1742 if (source_type == TypeManager.delegate_type &&
1743 target_type.IsSubclassOf (TypeManager.delegate_type))
1744 return new ClassCast (source, target_type);
1747 // From ICloneable to Array or Delegate types
1749 if (source_type == TypeManager.icloneable_type &&
1750 (target_type == TypeManager.array_type ||
1751 target_type == TypeManager.delegate_type))
1752 return new ClassCast (source, target_type);
1758 /// Performs an explicit conversion of the expression `expr' whose
1759 /// type is expr.Type to `target_type'.
1761 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1762 Type target_type, Location loc)
1764 Type expr_type = expr.Type;
1765 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1770 ne = ConvertNumericExplicit (ec, expr, target_type);
1775 // Unboxing conversion.
1777 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1778 return new UnboxCast (expr, target_type);
1783 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1787 // FIXME: Is there any reason we should have EnumConstant
1788 // dealt with here instead of just using always the
1789 // UnderlyingSystemType to wrap the type?
1791 if (expr is EnumConstant)
1792 e = ((EnumConstant) expr).Child;
1794 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1797 e = ConvertImplicit (ec, e, target_type, loc);
1801 return ConvertNumericExplicit (ec, e, target_type);
1804 ne = ConvertReferenceExplicit (expr, target_type);
1809 if (target_type.IsPointer){
1810 if (expr_type.IsPointer)
1811 return new EmptyCast (expr, target_type);
1813 if (expr_type == TypeManager.sbyte_type ||
1814 expr_type == TypeManager.byte_type ||
1815 expr_type == TypeManager.short_type ||
1816 expr_type == TypeManager.ushort_type ||
1817 expr_type == TypeManager.int32_type ||
1818 expr_type == TypeManager.uint32_type ||
1819 expr_type == TypeManager.uint64_type ||
1820 expr_type == TypeManager.int64_type)
1821 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1823 if (expr_type.IsPointer){
1824 if (target_type == TypeManager.sbyte_type ||
1825 target_type == TypeManager.byte_type ||
1826 target_type == TypeManager.short_type ||
1827 target_type == TypeManager.ushort_type ||
1828 target_type == TypeManager.int32_type ||
1829 target_type == TypeManager.uint32_type ||
1830 target_type == TypeManager.uint64_type ||
1831 target_type == TypeManager.int64_type){
1832 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1835 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1840 ce = ConvertNumericExplicit (ec, e, target_type);
1844 // We should always be able to go from an uint32
1845 // implicitly or explicitly to the other integral
1848 throw new Exception ("Internal compiler error");
1853 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1857 error30 (loc, expr_type, target_type);
1862 /// Same as ConverExplicit, only it doesn't include user defined conversions
1864 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1865 Type target_type, Location l)
1867 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1872 ne = ConvertNumericExplicit (ec, expr, target_type);
1876 ne = ConvertReferenceExplicit (expr, target_type);
1880 error30 (l, expr.Type, target_type);
1884 static string ExprClassName (ExprClass c)
1887 case ExprClass.Invalid:
1889 case ExprClass.Value:
1891 case ExprClass.Variable:
1893 case ExprClass.Namespace:
1895 case ExprClass.Type:
1897 case ExprClass.MethodGroup:
1898 return "method group";
1899 case ExprClass.PropertyAccess:
1900 return "property access";
1901 case ExprClass.EventAccess:
1902 return "event access";
1903 case ExprClass.IndexerAccess:
1904 return "indexer access";
1905 case ExprClass.Nothing:
1908 throw new Exception ("Should not happen");
1912 /// Reports that we were expecting `expr' to be of class `expected'
1914 protected void report118 (Location loc, Expression expr, string expected)
1916 string kind = "Unknown";
1919 kind = ExprClassName (expr.eclass);
1921 Error (118, loc, "Expression denotes a `" + kind +
1922 "' where a `" + expected + "' was expected");
1925 static void error31 (Location l, string val, Type t)
1927 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1928 TypeManager.CSharpName (t));
1931 public static void UnsafeError (Location loc)
1933 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
1937 /// Converts the IntConstant, UIntConstant, LongConstant or
1938 /// ULongConstant into the integral target_type. Notice
1939 /// that we do not return an `Expression' we do return
1940 /// a boxed integral type.
1942 /// FIXME: Since I added the new constants, we need to
1943 /// also support conversions from CharConstant, ByteConstant,
1944 /// SByteConstant, UShortConstant, ShortConstant
1946 /// This is used by the switch statement, so the domain
1947 /// of work is restricted to the literals above, and the
1948 /// targets are int32, uint32, char, byte, sbyte, ushort,
1949 /// short, uint64 and int64
1951 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1955 if (c.Type == target_type)
1956 return ((Constant) c).GetValue ();
1959 // Make into one of the literals we handle, we dont really care
1960 // about this value as we will just return a few limited types
1962 if (c is EnumConstant)
1963 c = ((EnumConstant)c).WidenToCompilerConstant ();
1965 if (c is IntConstant){
1966 int v = ((IntConstant) c).Value;
1968 if (target_type == TypeManager.uint32_type){
1971 } else if (target_type == TypeManager.char_type){
1972 if (v >= Char.MinValue && v <= Char.MaxValue)
1974 } else if (target_type == TypeManager.byte_type){
1975 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1977 } else if (target_type == TypeManager.sbyte_type){
1978 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1980 } else if (target_type == TypeManager.short_type){
1981 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1983 } else if (target_type == TypeManager.ushort_type){
1984 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1986 } else if (target_type == TypeManager.int64_type)
1988 else if (target_type == TypeManager.uint64_type){
1994 } else if (c is UIntConstant){
1995 uint v = ((UIntConstant) c).Value;
1997 if (target_type == TypeManager.int32_type){
1998 if (v <= Int32.MaxValue)
2000 } else if (target_type == TypeManager.char_type){
2001 if (v >= Char.MinValue && v <= Char.MaxValue)
2003 } else if (target_type == TypeManager.byte_type){
2004 if (v <= Byte.MaxValue)
2006 } else if (target_type == TypeManager.sbyte_type){
2007 if (v <= SByte.MaxValue)
2009 } else if (target_type == TypeManager.short_type){
2010 if (v <= UInt16.MaxValue)
2012 } else if (target_type == TypeManager.ushort_type){
2013 if (v <= UInt16.MaxValue)
2015 } else if (target_type == TypeManager.int64_type)
2017 else if (target_type == TypeManager.uint64_type)
2020 } else if (c is LongConstant){
2021 long v = ((LongConstant) c).Value;
2023 if (target_type == TypeManager.int32_type){
2024 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2026 } else if (target_type == TypeManager.uint32_type){
2027 if (v >= 0 && v <= UInt32.MaxValue)
2029 } else if (target_type == TypeManager.char_type){
2030 if (v >= Char.MinValue && v <= Char.MaxValue)
2032 } else if (target_type == TypeManager.byte_type){
2033 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2035 } else if (target_type == TypeManager.sbyte_type){
2036 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2038 } else if (target_type == TypeManager.short_type){
2039 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2041 } else if (target_type == TypeManager.ushort_type){
2042 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2044 } else if (target_type == TypeManager.uint64_type){
2049 } else if (c is ULongConstant){
2050 ulong v = ((ULongConstant) c).Value;
2052 if (target_type == TypeManager.int32_type){
2053 if (v <= Int32.MaxValue)
2055 } else if (target_type == TypeManager.uint32_type){
2056 if (v <= UInt32.MaxValue)
2058 } else if (target_type == TypeManager.char_type){
2059 if (v >= Char.MinValue && v <= Char.MaxValue)
2061 } else if (target_type == TypeManager.byte_type){
2062 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2064 } else if (target_type == TypeManager.sbyte_type){
2065 if (v <= (int) SByte.MaxValue)
2067 } else if (target_type == TypeManager.short_type){
2068 if (v <= UInt16.MaxValue)
2070 } else if (target_type == TypeManager.ushort_type){
2071 if (v <= UInt16.MaxValue)
2073 } else if (target_type == TypeManager.int64_type){
2074 if (v <= Int64.MaxValue)
2078 } else if (c is ByteConstant){
2079 byte v = ((ByteConstant) c).Value;
2081 if (target_type == TypeManager.int32_type)
2083 else if (target_type == TypeManager.uint32_type)
2085 else if (target_type == TypeManager.char_type)
2087 else if (target_type == TypeManager.sbyte_type){
2088 if (v <= SByte.MaxValue)
2090 } else if (target_type == TypeManager.short_type)
2092 else if (target_type == TypeManager.ushort_type)
2094 else if (target_type == TypeManager.int64_type)
2096 else if (target_type == TypeManager.uint64_type)
2099 } else if (c is SByteConstant){
2100 sbyte v = ((SByteConstant) c).Value;
2102 if (target_type == TypeManager.int32_type)
2104 else if (target_type == TypeManager.uint32_type){
2107 } else if (target_type == TypeManager.char_type){
2110 } else if (target_type == TypeManager.byte_type){
2113 } else if (target_type == TypeManager.short_type)
2115 else if (target_type == TypeManager.ushort_type){
2118 } else if (target_type == TypeManager.int64_type)
2120 else if (target_type == TypeManager.uint64_type){
2125 } else if (c is ShortConstant){
2126 short v = ((ShortConstant) c).Value;
2128 if (target_type == TypeManager.int32_type){
2130 } else if (target_type == TypeManager.uint32_type){
2133 } else if (target_type == TypeManager.char_type){
2136 } else if (target_type == TypeManager.byte_type){
2137 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2139 } else if (target_type == TypeManager.sbyte_type){
2140 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2142 } else if (target_type == TypeManager.ushort_type){
2145 } else if (target_type == TypeManager.int64_type)
2147 else if (target_type == TypeManager.uint64_type)
2151 } else if (c is UShortConstant){
2152 ushort v = ((UShortConstant) c).Value;
2154 if (target_type == TypeManager.int32_type)
2156 else if (target_type == TypeManager.uint32_type)
2158 else if (target_type == TypeManager.char_type){
2159 if (v >= Char.MinValue && v <= Char.MaxValue)
2161 } else if (target_type == TypeManager.byte_type){
2162 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2164 } else if (target_type == TypeManager.sbyte_type){
2165 if (v <= SByte.MaxValue)
2167 } else if (target_type == TypeManager.short_type){
2168 if (v <= Int16.MaxValue)
2170 } else if (target_type == TypeManager.int64_type)
2172 else if (target_type == TypeManager.uint64_type)
2176 } else if (c is CharConstant){
2177 char v = ((CharConstant) c).Value;
2179 if (target_type == TypeManager.int32_type)
2181 else if (target_type == TypeManager.uint32_type)
2183 else if (target_type == TypeManager.byte_type){
2184 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2186 } else if (target_type == TypeManager.sbyte_type){
2187 if (v <= SByte.MaxValue)
2189 } else if (target_type == TypeManager.short_type){
2190 if (v <= Int16.MaxValue)
2192 } else if (target_type == TypeManager.ushort_type)
2194 else if (target_type == TypeManager.int64_type)
2196 else if (target_type == TypeManager.uint64_type)
2201 error31 (loc, s, target_type);
2206 // Load the object from the pointer. The `IsReference' is used
2207 // to control whether we should use Ldind_Ref or LdObj if the
2208 // value is not a `core' type.
2210 // Maybe we should try to extract this infromation form the type?
2211 // TODO: Maybe this is a bug. The reason we have this flag is because
2212 // I had almost identical code in ParameterReference (for handling
2213 // references) and in UnboxCast.
2215 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2217 if (t == TypeManager.int32_type)
2218 ig.Emit (OpCodes.Ldind_I4);
2219 else if (t == TypeManager.uint32_type)
2220 ig.Emit (OpCodes.Ldind_U4);
2221 else if (t == TypeManager.short_type)
2222 ig.Emit (OpCodes.Ldind_I2);
2223 else if (t == TypeManager.ushort_type)
2224 ig.Emit (OpCodes.Ldind_U2);
2225 else if (t == TypeManager.char_type)
2226 ig.Emit (OpCodes.Ldind_U2);
2227 else if (t == TypeManager.byte_type)
2228 ig.Emit (OpCodes.Ldind_U1);
2229 else if (t == TypeManager.sbyte_type)
2230 ig.Emit (OpCodes.Ldind_I1);
2231 else if (t == TypeManager.uint64_type)
2232 ig.Emit (OpCodes.Ldind_I8);
2233 else if (t == TypeManager.int64_type)
2234 ig.Emit (OpCodes.Ldind_I8);
2235 else if (t == TypeManager.float_type)
2236 ig.Emit (OpCodes.Ldind_R4);
2237 else if (t == TypeManager.double_type)
2238 ig.Emit (OpCodes.Ldind_R8);
2239 else if (t == TypeManager.bool_type)
2240 ig.Emit (OpCodes.Ldind_I1);
2241 else if (t == TypeManager.intptr_type)
2242 ig.Emit (OpCodes.Ldind_I);
2243 else if (TypeManager.IsEnumType (t)){
2244 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2247 ig.Emit (OpCodes.Ldind_Ref);
2249 ig.Emit (OpCodes.Ldobj, t);
2254 // The stack contains the pointer and the value of type `type'
2256 public static void StoreFromPtr (ILGenerator ig, Type type)
2258 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2259 ig.Emit (OpCodes.Stind_I4);
2260 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2261 ig.Emit (OpCodes.Stind_I8);
2262 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2263 type == TypeManager.ushort_type)
2264 ig.Emit (OpCodes.Stind_I2);
2265 else if (type == TypeManager.float_type)
2266 ig.Emit (OpCodes.Stind_R4);
2267 else if (type == TypeManager.double_type)
2268 ig.Emit (OpCodes.Stind_R8);
2269 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2270 type == TypeManager.bool_type)
2271 ig.Emit (OpCodes.Stind_I1);
2272 else if (type == TypeManager.intptr_type)
2273 ig.Emit (OpCodes.Stind_I);
2275 ig.Emit (OpCodes.Stind_Ref);
2279 // Returns the size of type `t' if known, otherwise, 0
2281 public static int GetTypeSize (Type t)
2283 if (t == TypeManager.int32_type ||
2284 t == TypeManager.uint32_type ||
2285 t == TypeManager.float_type)
2287 else if (t == TypeManager.int64_type ||
2288 t == TypeManager.uint64_type ||
2289 t == TypeManager.double_type)
2291 else if (t == TypeManager.byte_type ||
2292 t == TypeManager.sbyte_type ||
2293 t == TypeManager.bool_type)
2295 else if (t == TypeManager.short_type ||
2296 t == TypeManager.char_type ||
2297 t == TypeManager.ushort_type)
2305 /// This is just a base class for expressions that can
2306 /// appear on statements (invocations, object creation,
2307 /// assignments, post/pre increment and decrement). The idea
2308 /// being that they would support an extra Emition interface that
2309 /// does not leave a result on the stack.
2311 public abstract class ExpressionStatement : Expression {
2314 /// Requests the expression to be emitted in a `statement'
2315 /// context. This means that no new value is left on the
2316 /// stack after invoking this method (constrasted with
2317 /// Emit that will always leave a value on the stack).
2319 public abstract void EmitStatement (EmitContext ec);
2323 /// This kind of cast is used to encapsulate the child
2324 /// whose type is child.Type into an expression that is
2325 /// reported to return "return_type". This is used to encapsulate
2326 /// expressions which have compatible types, but need to be dealt
2327 /// at higher levels with.
2329 /// For example, a "byte" expression could be encapsulated in one
2330 /// of these as an "unsigned int". The type for the expression
2331 /// would be "unsigned int".
2334 public class EmptyCast : Expression {
2335 protected Expression child;
2337 public EmptyCast (Expression child, Type return_type)
2339 eclass = child.eclass;
2344 public override Expression DoResolve (EmitContext ec)
2346 // This should never be invoked, we are born in fully
2347 // initialized state.
2352 public override void Emit (EmitContext ec)
2359 /// This class is used to wrap literals which belong inside Enums
2361 public class EnumConstant : Constant {
2362 public Constant Child;
2364 public EnumConstant (Constant child, Type enum_type)
2366 eclass = child.eclass;
2371 public override Expression DoResolve (EmitContext ec)
2373 // This should never be invoked, we are born in fully
2374 // initialized state.
2379 public override void Emit (EmitContext ec)
2384 public override object GetValue ()
2386 return Child.GetValue ();
2390 // Converts from one of the valid underlying types for an enumeration
2391 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2392 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2394 public Constant WidenToCompilerConstant ()
2396 Type t = TypeManager.EnumToUnderlying (Child.Type);
2397 object v = ((Constant) Child).GetValue ();;
2399 if (t == TypeManager.int32_type)
2400 return new IntConstant ((int) v);
2401 if (t == TypeManager.uint32_type)
2402 return new UIntConstant ((uint) v);
2403 if (t == TypeManager.int64_type)
2404 return new LongConstant ((long) v);
2405 if (t == TypeManager.uint64_type)
2406 return new ULongConstant ((ulong) v);
2407 if (t == TypeManager.short_type)
2408 return new ShortConstant ((short) v);
2409 if (t == TypeManager.ushort_type)
2410 return new UShortConstant ((ushort) v);
2411 if (t == TypeManager.byte_type)
2412 return new ByteConstant ((byte) v);
2413 if (t == TypeManager.sbyte_type)
2414 return new SByteConstant ((sbyte) v);
2416 throw new Exception ("Invalid enumeration underlying type: " + t);
2420 // Extracts the value in the enumeration on its native representation
2422 public object GetPlainValue ()
2424 Type t = TypeManager.EnumToUnderlying (Child.Type);
2425 object v = ((Constant) Child).GetValue ();;
2427 if (t == TypeManager.int32_type)
2429 if (t == TypeManager.uint32_type)
2431 if (t == TypeManager.int64_type)
2433 if (t == TypeManager.uint64_type)
2435 if (t == TypeManager.short_type)
2437 if (t == TypeManager.ushort_type)
2439 if (t == TypeManager.byte_type)
2441 if (t == TypeManager.sbyte_type)
2447 public override string AsString ()
2449 return Child.AsString ();
2454 /// This kind of cast is used to encapsulate Value Types in objects.
2456 /// The effect of it is to box the value type emitted by the previous
2459 public class BoxedCast : EmptyCast {
2461 public BoxedCast (Expression expr)
2462 : base (expr, TypeManager.object_type)
2466 public override Expression DoResolve (EmitContext ec)
2468 // This should never be invoked, we are born in fully
2469 // initialized state.
2474 public override void Emit (EmitContext ec)
2477 ec.ig.Emit (OpCodes.Box, child.Type);
2481 public class UnboxCast : EmptyCast {
2482 public UnboxCast (Expression expr, Type return_type)
2483 : base (expr, return_type)
2487 public override Expression DoResolve (EmitContext ec)
2489 // This should never be invoked, we are born in fully
2490 // initialized state.
2495 public override void Emit (EmitContext ec)
2498 ILGenerator ig = ec.ig;
2501 ig.Emit (OpCodes.Unbox, t);
2503 LoadFromPtr (ig, t, false);
2508 /// This is used to perform explicit numeric conversions.
2510 /// Explicit numeric conversions might trigger exceptions in a checked
2511 /// context, so they should generate the conv.ovf opcodes instead of
2514 public class ConvCast : EmptyCast {
2515 public enum Mode : byte {
2516 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2518 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2519 U2_I1, U2_U1, U2_I2, U2_CH,
2520 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2521 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2522 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2523 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2524 CH_I1, CH_U1, CH_I2,
2525 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2526 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2531 public ConvCast (Expression child, Type return_type, Mode m)
2532 : base (child, return_type)
2537 public override Expression DoResolve (EmitContext ec)
2539 // This should never be invoked, we are born in fully
2540 // initialized state.
2545 public override void Emit (EmitContext ec)
2547 ILGenerator ig = ec.ig;
2553 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2554 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2555 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2556 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2557 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2559 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2560 case Mode.U1_CH: /* nothing */ break;
2562 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2563 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2564 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2565 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2566 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2567 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2569 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2570 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2571 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2572 case Mode.U2_CH: /* nothing */ break;
2574 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2575 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2576 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2577 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2578 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2579 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2580 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2582 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2583 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2584 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2585 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2586 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2587 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2589 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2590 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2591 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2592 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2593 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2594 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2595 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2596 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2598 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2599 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2600 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2601 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2602 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2603 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2604 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2605 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2607 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2608 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2609 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2611 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2612 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2613 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2614 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2615 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2616 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2617 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2618 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2619 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2621 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2622 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2623 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2624 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2625 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2626 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2627 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2628 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2629 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2630 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2634 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2635 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2636 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2637 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2638 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2640 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2641 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2643 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2644 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2645 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2646 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2647 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2648 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2650 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2651 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2652 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2653 case Mode.U2_CH: /* nothing */ break;
2655 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2656 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2657 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2658 case Mode.I4_U4: /* nothing */ break;
2659 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2660 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2661 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2663 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2664 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2665 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2666 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2667 case Mode.U4_I4: /* nothing */ break;
2668 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2670 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2671 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2672 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2673 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2674 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2675 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2676 case Mode.I8_U8: /* nothing */ break;
2677 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2679 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2680 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2681 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2682 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2683 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2684 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2685 case Mode.U8_I8: /* nothing */ break;
2686 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2688 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2689 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2690 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2692 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2693 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2694 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2695 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2696 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2697 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2698 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2699 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2700 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2702 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2703 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2704 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2705 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2706 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2707 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2708 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2709 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2710 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2711 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2717 public class OpcodeCast : EmptyCast {
2721 public OpcodeCast (Expression child, Type return_type, OpCode op)
2722 : base (child, return_type)
2726 second_valid = false;
2729 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2730 : base (child, return_type)
2735 second_valid = true;
2738 public override Expression DoResolve (EmitContext ec)
2740 // This should never be invoked, we are born in fully
2741 // initialized state.
2746 public override void Emit (EmitContext ec)
2757 /// This kind of cast is used to encapsulate a child and cast it
2758 /// to the class requested
2760 public class ClassCast : EmptyCast {
2761 public ClassCast (Expression child, Type return_type)
2762 : base (child, return_type)
2767 public override Expression DoResolve (EmitContext ec)
2769 // This should never be invoked, we are born in fully
2770 // initialized state.
2775 public override void Emit (EmitContext ec)
2779 ec.ig.Emit (OpCodes.Castclass, type);
2785 /// SimpleName expressions are initially formed of a single
2786 /// word and it only happens at the beginning of the expression.
2790 /// The expression will try to be bound to a Field, a Method
2791 /// group or a Property. If those fail we pass the name to our
2792 /// caller and the SimpleName is compounded to perform a type
2793 /// lookup. The idea behind this process is that we want to avoid
2794 /// creating a namespace map from the assemblies, as that requires
2795 /// the GetExportedTypes function to be called and a hashtable to
2796 /// be constructed which reduces startup time. If later we find
2797 /// that this is slower, we should create a `NamespaceExpr' expression
2798 /// that fully participates in the resolution process.
2800 /// For example `System.Console.WriteLine' is decomposed into
2801 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2803 /// The first SimpleName wont produce a match on its own, so it will
2805 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2807 /// System.Console will produce a TypeExpr match.
2809 /// The downside of this is that we might be hitting `LookupType' too many
2810 /// times with this scheme.
2812 public class SimpleName : Expression {
2813 public readonly string Name;
2814 public readonly Location Location;
2816 public SimpleName (string name, Location l)
2822 public static void Error120 (Location l, string name)
2826 "An object reference is required " +
2827 "for the non-static field `"+name+"'");
2831 // Checks whether we are trying to access an instance
2832 // property, method or field from a static body.
2834 Expression MemberStaticCheck (Expression e)
2836 if (e is FieldExpr){
2837 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2840 Error120 (Location, Name);
2843 } else if (e is MethodGroupExpr){
2844 MethodGroupExpr mg = (MethodGroupExpr) e;
2846 if (!mg.RemoveInstanceMethods ()){
2847 Error120 (Location, mg.Methods [0].Name);
2851 } else if (e is PropertyExpr){
2852 if (!((PropertyExpr) e).IsStatic){
2853 Error120 (Location, Name);
2856 } else if (e is EventExpr) {
2857 if (!((EventExpr) e).IsStatic) {
2858 Error120 (Location, Name);
2866 public override Expression DoResolve (EmitContext ec)
2868 return SimpleNameResolve (ec, false);
2871 public Expression DoResolveAllowStatic (EmitContext ec)
2873 return SimpleNameResolve (ec, true);
2877 /// 7.5.2: Simple Names.
2879 /// Local Variables and Parameters are handled at
2880 /// parse time, so they never occur as SimpleNames.
2882 /// The `allow_static' flag is used by MemberAccess only
2883 /// and it is used to inform us that it is ok for us to
2884 /// avoid the static check, because MemberAccess might end
2885 /// up resolving the Name as a Type name and the access as
2886 /// a static type access.
2888 /// ie: Type Type; .... { Type.GetType (""); }
2890 /// Type is both an instance variable and a Type; Type.GetType
2891 /// is the static method not an instance method of type.
2893 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2898 // Stage 1: Performed by the parser (binding to locals or parameters).
2902 // Stage 2: Lookup members
2904 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
2907 // Stage 3: Lookup symbol in the various namespaces.
2909 DeclSpace ds = ec.TypeContainer;
2913 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2914 return new TypeExpr (t);
2917 // Stage 2 part b: Lookup up if we are an alias to a type
2920 // Since we are cheating: we only do the Alias lookup for
2921 // namespaces if the name does not include any dots in it
2924 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2925 // System.Console.WriteLine (Name + " --> " + alias_value);
2926 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2928 return new TypeExpr (t);
2930 // we have alias value, but it isn't Type, so try if it's namespace
2931 return new SimpleName (alias_value, Location);
2934 // No match, maybe our parent can compose us
2935 // into something meaningful.
2940 // Stage 2 continues here.
2945 if (e is FieldExpr){
2946 FieldExpr fe = (FieldExpr) e;
2947 FieldInfo fi = fe.FieldInfo;
2949 if (fi.FieldType.IsPointer && !ec.InUnsafe){
2950 UnsafeError (Location);
2954 if (!allow_static && !fi.IsStatic){
2955 Error120 (Location, Name);
2959 // If we are not in static code and this
2960 // field is not static, set the instance to `this'.
2963 fe.InstanceExpression = ec.This;
2967 if (fi is FieldBuilder) {
2968 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2971 object o = c.LookupConstantValue (ec);
2972 object real_value = ((Constant)c.Expr).GetValue ();
2973 return Constantify (real_value, fi.FieldType);
2980 if (e is EventExpr) {
2982 // If the event is local to this class, we transform ourselves into
2985 EventExpr ee = (EventExpr) e;
2987 Expression ml = MemberLookup (
2988 ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
2989 MemberTypes.Event, AllBindingFlags, Location);
2992 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
2996 // If this happens, then we have an event with its own
2997 // accessors and private field etc so there's no need
2998 // to transform ourselves : we should instead flag an error
3000 Assign.error70 (ee.EventInfo, Location);
3004 ml = ExprClassFromMemberInfo (ec, mi, Location);
3007 Report.Error (-200, Location, "Internal error!!");
3011 Expression instance_expr;
3013 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3016 instance_expr = null;
3018 instance_expr = ec.This;
3020 instance_expr = instance_expr.Resolve (ec);
3022 if (instance_expr != null)
3023 instance_expr = instance_expr.Resolve (ec);
3025 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3034 return MemberStaticCheck (e);
3039 public override void Emit (EmitContext ec)
3042 // If this is ever reached, then we failed to
3043 // find the name as a namespace
3046 Error (103, Location, "The name `" + Name +
3047 "' does not exist in the class `" +
3048 ec.TypeContainer.Name + "'");
3053 /// Fully resolved expression that evaluates to a type
3055 public class TypeExpr : Expression {
3056 public TypeExpr (Type t)
3059 eclass = ExprClass.Type;
3062 override public Expression DoResolve (EmitContext ec)
3067 override public void Emit (EmitContext ec)
3069 throw new Exception ("Implement me");
3074 /// MethodGroup Expression.
3076 /// This is a fully resolved expression that evaluates to a type
3078 public class MethodGroupExpr : Expression {
3079 public MethodBase [] Methods;
3080 Expression instance_expression = null;
3082 public MethodGroupExpr (MemberInfo [] mi)
3084 Methods = new MethodBase [mi.Length];
3085 mi.CopyTo (Methods, 0);
3086 eclass = ExprClass.MethodGroup;
3089 public MethodGroupExpr (ArrayList l)
3091 Methods = new MethodBase [l.Count];
3093 l.CopyTo (Methods, 0);
3094 eclass = ExprClass.MethodGroup;
3098 // `A method group may have associated an instance expression'
3100 public Expression InstanceExpression {
3102 return instance_expression;
3106 instance_expression = value;
3110 override public Expression DoResolve (EmitContext ec)
3115 override public void Emit (EmitContext ec)
3117 throw new Exception ("This should never be reached");
3120 bool RemoveMethods (bool keep_static)
3122 ArrayList smethods = new ArrayList ();
3123 int top = Methods.Length;
3126 for (i = 0; i < top; i++){
3127 MethodBase mb = Methods [i];
3129 if (mb.IsStatic == keep_static)
3133 if (smethods.Count == 0)
3136 Methods = new MethodBase [smethods.Count];
3137 smethods.CopyTo (Methods, 0);
3143 /// Removes any instance methods from the MethodGroup, returns
3144 /// false if the resulting set is empty.
3146 public bool RemoveInstanceMethods ()
3148 return RemoveMethods (true);
3152 /// Removes any static methods from the MethodGroup, returns
3153 /// false if the resulting set is empty.
3155 public bool RemoveStaticMethods ()
3157 return RemoveMethods (false);
3162 /// Fully resolved expression that evaluates to a Field
3164 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3165 public readonly FieldInfo FieldInfo;
3166 public Expression InstanceExpression;
3169 public FieldExpr (FieldInfo fi, Location l)
3172 eclass = ExprClass.Variable;
3173 type = fi.FieldType;
3177 override public Expression DoResolve (EmitContext ec)
3179 if (!FieldInfo.IsStatic){
3180 if (InstanceExpression == null){
3181 throw new Exception ("non-static FieldExpr without instance var\n" +
3182 "You have to assign the Instance variable\n" +
3183 "Of the FieldExpr to set this\n");
3186 InstanceExpression = InstanceExpression.Resolve (ec);
3187 if (InstanceExpression == null)
3194 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3196 Expression e = DoResolve (ec);
3201 if (!FieldInfo.IsInitOnly)
3205 // InitOnly fields can only be assigned in constructors
3208 if (ec.IsConstructor)
3211 Report.Error (191, loc,
3212 "Readonly field can not be assigned outside " +
3213 "of constructor or variable initializer");
3218 override public void Emit (EmitContext ec)
3220 ILGenerator ig = ec.ig;
3221 bool is_volatile = false;
3223 if (FieldInfo is FieldBuilder){
3224 Field f = TypeManager.GetField (FieldInfo);
3225 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3228 f.status |= Field.Status.USED;
3231 if (FieldInfo.IsStatic){
3233 ig.Emit (OpCodes.Volatile);
3235 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3237 if (InstanceExpression.Type.IsValueType){
3239 LocalTemporary tempo = null;
3241 if (!(InstanceExpression is IMemoryLocation)){
3242 tempo = new LocalTemporary (
3243 ec, InstanceExpression.Type);
3245 InstanceExpression.Emit (ec);
3249 ml = (IMemoryLocation) InstanceExpression;
3253 InstanceExpression.Emit (ec);
3256 ig.Emit (OpCodes.Volatile);
3258 ig.Emit (OpCodes.Ldfld, FieldInfo);
3262 public void EmitAssign (EmitContext ec, Expression source)
3264 bool is_static = FieldInfo.IsStatic;
3265 ILGenerator ig = ec.ig;
3268 Expression instance = InstanceExpression;
3270 if (instance.Type.IsValueType){
3271 if (instance is IMemoryLocation){
3272 IMemoryLocation ml = (IMemoryLocation) instance;
3276 throw new Exception ("The " + instance + " of type " +
3278 " represents a ValueType and does " +
3279 "not implement IMemoryLocation");
3285 if (FieldInfo is FieldBuilder){
3286 Field f = TypeManager.GetField (FieldInfo);
3287 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3288 ig.Emit (OpCodes.Volatile);
3292 ig.Emit (OpCodes.Stsfld, FieldInfo);
3294 ig.Emit (OpCodes.Stfld, FieldInfo);
3296 if (FieldInfo is FieldBuilder){
3297 Field f = TypeManager.GetField (FieldInfo);
3299 f.status |= Field.Status.ASSIGNED;
3303 public void AddressOf (EmitContext ec)
3305 ILGenerator ig = ec.ig;
3307 if (FieldInfo is FieldBuilder){
3308 Field f = TypeManager.GetField (FieldInfo);
3309 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3310 ig.Emit (OpCodes.Volatile);
3316 // Mhm. We do not know what we are being used for:
3317 // READING or WRITING the field.
3319 // I think we want an extra argument to AddressOf to pass
3320 // this semantic information.
3322 // For now: just flag both assigned and used.
3324 if (FieldInfo is FieldBuilder){
3325 Field f = TypeManager.GetField (FieldInfo);
3327 f.status |= Field.Status.ASSIGNED | Field.Status.USED;
3331 // Handle initonly fields specially: make a copy and then
3332 // get the address of the copy.
3334 if (FieldInfo.IsInitOnly){
3338 local = ig.DeclareLocal (type);
3339 ig.Emit (OpCodes.Stloc, local);
3340 ig.Emit (OpCodes.Ldloca, local);
3344 if (FieldInfo.IsStatic)
3345 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3347 InstanceExpression.Emit (ec);
3348 ig.Emit (OpCodes.Ldflda, FieldInfo);
3354 /// Expression that evaluates to a Property. The Assign class
3355 /// might set the `Value' expression if we are in an assignment.
3357 /// This is not an LValue because we need to re-write the expression, we
3358 /// can not take data from the stack and store it.
3360 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3361 public readonly PropertyInfo PropertyInfo;
3362 public readonly bool IsStatic;
3364 MethodInfo [] Accessors;
3367 Expression instance_expr;
3369 public PropertyExpr (PropertyInfo pi, Location l)
3372 eclass = ExprClass.PropertyAccess;
3375 Accessors = TypeManager.GetAccessors (pi);
3377 if (Accessors != null)
3378 for (int i = 0; i < Accessors.Length; i++){
3379 if (Accessors [i] != null)
3380 if (Accessors [i].IsStatic)
3384 Accessors = new MethodInfo [2];
3386 type = pi.PropertyType;
3390 // The instance expression associated with this expression
3392 public Expression InstanceExpression {
3394 instance_expr = value;
3398 return instance_expr;
3402 public bool VerifyAssignable ()
3404 if (!PropertyInfo.CanWrite){
3405 Report.Error (200, loc,
3406 "The property `" + PropertyInfo.Name +
3407 "' can not be assigned to, as it has not set accessor");
3414 override public Expression DoResolve (EmitContext ec)
3416 if (!PropertyInfo.CanRead){
3417 Report.Error (154, loc,
3418 "The property `" + PropertyInfo.Name +
3419 "' can not be used in " +
3420 "this context because it lacks a get accessor");
3424 type = PropertyInfo.PropertyType;
3429 override public void Emit (EmitContext ec)
3431 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
3436 // Implements the IAssignMethod interface for assignments
3438 public void EmitAssign (EmitContext ec, Expression source)
3440 Argument arg = new Argument (source, Argument.AType.Expression);
3441 ArrayList args = new ArrayList ();
3444 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3447 override public void EmitStatement (EmitContext ec)
3450 ec.ig.Emit (OpCodes.Pop);
3455 /// Fully resolved expression that evaluates to an Event
3457 public class EventExpr : Expression {
3458 public readonly EventInfo EventInfo;
3460 public Expression InstanceExpression;
3462 public readonly bool IsStatic;
3464 MethodInfo add_accessor, remove_accessor;
3466 public EventExpr (EventInfo ei, Location loc)
3470 eclass = ExprClass.EventAccess;
3472 add_accessor = TypeManager.GetAddMethod (ei);
3473 remove_accessor = TypeManager.GetRemoveMethod (ei);
3475 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3478 if (EventInfo is MyEventBuilder)
3479 type = ((MyEventBuilder) EventInfo).EventType;
3481 type = EventInfo.EventHandlerType;
3484 override public Expression DoResolve (EmitContext ec)
3486 // We are born fully resolved
3490 override public void Emit (EmitContext ec)
3492 throw new Exception ("Should not happen I think");
3495 public void EmitAddOrRemove (EmitContext ec, Expression source)
3497 Expression handler = ((Binary) source).Right;
3499 Argument arg = new Argument (handler, Argument.AType.Expression);
3500 ArrayList args = new ArrayList ();
3504 if (((Binary) source).Oper == Binary.Operator.Addition)
3505 Invocation.EmitCall (
3506 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3508 Invocation.EmitCall (
3509 ec, false, IsStatic, InstanceExpression, remove_accessor, args);