2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 // This is just as a hint to AddressOf of what will be done with the
43 public enum AddressOp {
50 /// This interface is implemented by variables
52 public interface IMemoryLocation {
54 /// The AddressOf method should generate code that loads
55 /// the address of the object and leaves it on the stack.
57 /// The `mode' argument is used to notify the expression
58 /// of whether this will be used to read from the address or
59 /// write to the address.
61 /// This is just a hint that can be used to provide good error
62 /// reporting, and should have no other side effects.
64 void AddressOf (EmitContext ec, AddressOp mode);
68 /// Base class for expressions
70 public abstract class Expression {
71 public ExprClass eclass;
85 /// Utility wrapper routine for Error, just to beautify the code
87 static protected void Error (int error, string s)
89 Report.Error (error, s);
92 static protected void Error (int error, Location loc, string s)
94 Report.Error (error, loc, s);
98 /// Utility wrapper routine for Warning, just to beautify the code
100 static protected void Warning (int warning, string s)
102 Report.Warning (warning, s);
105 static public void Error_CannotConvertType (Location loc, Type source, Type target)
107 Report.Error (30, loc, "Cannot convert type '" +
108 TypeManager.CSharpName (source) + "' to '" +
109 TypeManager.CSharpName (target) + "'");
113 /// Performs semantic analysis on the Expression
117 /// The Resolve method is invoked to perform the semantic analysis
120 /// The return value is an expression (it can be the
121 /// same expression in some cases) or a new
122 /// expression that better represents this node.
124 /// For example, optimizations of Unary (LiteralInt)
125 /// would return a new LiteralInt with a negated
128 /// If there is an error during semantic analysis,
129 /// then an error should be reported (using Report)
130 /// and a null value should be returned.
132 /// There are two side effects expected from calling
133 /// Resolve(): the the field variable "eclass" should
134 /// be set to any value of the enumeration
135 /// `ExprClass' and the type variable should be set
136 /// to a valid type (this is the type of the
139 public abstract Expression DoResolve (EmitContext ec);
141 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
143 return DoResolve (ec);
147 /// Resolves an expression and performs semantic analysis on it.
151 /// Currently Resolve wraps DoResolve to perform sanity
152 /// checking and assertion checking on what we expect from Resolve.
154 public Expression Resolve (EmitContext ec)
156 Expression e = DoResolve (ec);
160 if (e is SimpleName){
161 SimpleName s = (SimpleName) e;
165 "The name `" + s.Name + "' could not be found in `" +
166 ec.DeclSpace.Name + "'");
170 if (e.eclass == ExprClass.Invalid)
171 throw new Exception ("Expression " + e.GetType () +
172 " ExprClass is Invalid after resolve");
174 if (e.eclass != ExprClass.MethodGroup)
176 throw new Exception (
177 "Expression " + e.GetType () +
178 " did not set its type after Resolve\n" +
179 "called from: " + this.GetType ());
186 /// Performs expression resolution and semantic analysis, but
187 /// allows SimpleNames to be returned.
191 /// This is used by MemberAccess to construct long names that can not be
192 /// partially resolved (namespace-qualified names for example).
194 public Expression ResolveWithSimpleName (EmitContext ec)
198 if (this is SimpleName)
199 e = ((SimpleName) this).DoResolveAllowStatic (ec);
207 if (e.eclass == ExprClass.Invalid)
208 throw new Exception ("Expression " + e +
209 " ExprClass is Invalid after resolve");
211 if (e.eclass != ExprClass.MethodGroup)
213 throw new Exception ("Expression " + e +
214 " did not set its type after Resolve");
221 /// Resolves an expression for LValue assignment
225 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
226 /// checking and assertion checking on what we expect from Resolve
228 public Expression ResolveLValue (EmitContext ec, Expression right_side)
230 Expression e = DoResolveLValue (ec, right_side);
233 if (e is SimpleName){
234 SimpleName s = (SimpleName) e;
238 "The name `" + s.Name + "' could not be found in `" +
239 ec.DeclSpace.Name + "'");
243 if (e.eclass == ExprClass.Invalid)
244 throw new Exception ("Expression " + e +
245 " ExprClass is Invalid after resolve");
247 if (e.eclass != ExprClass.MethodGroup)
249 throw new Exception ("Expression " + e +
250 " did not set its type after Resolve");
257 /// Emits the code for the expression
261 /// The Emit method is invoked to generate the code
262 /// for the expression.
264 public abstract void Emit (EmitContext ec);
267 /// Protected constructor. Only derivate types should
268 /// be able to be created
271 protected Expression ()
273 eclass = ExprClass.Invalid;
278 /// Returns a literalized version of a literal FieldInfo
282 /// The possible return values are:
283 /// IntConstant, UIntConstant
284 /// LongLiteral, ULongConstant
285 /// FloatConstant, DoubleConstant
288 /// The value returned is already resolved.
290 public static Constant Constantify (object v, Type t)
292 if (t == TypeManager.int32_type)
293 return new IntConstant ((int) v);
294 else if (t == TypeManager.uint32_type)
295 return new UIntConstant ((uint) v);
296 else if (t == TypeManager.int64_type)
297 return new LongConstant ((long) v);
298 else if (t == TypeManager.uint64_type)
299 return new ULongConstant ((ulong) v);
300 else if (t == TypeManager.float_type)
301 return new FloatConstant ((float) v);
302 else if (t == TypeManager.double_type)
303 return new DoubleConstant ((double) v);
304 else if (t == TypeManager.string_type)
305 return new StringConstant ((string) v);
306 else if (t == TypeManager.short_type)
307 return new ShortConstant ((short)v);
308 else if (t == TypeManager.ushort_type)
309 return new UShortConstant ((ushort)v);
310 else if (t == TypeManager.sbyte_type)
311 return new SByteConstant (((sbyte)v));
312 else if (t == TypeManager.byte_type)
313 return new ByteConstant ((byte)v);
314 else if (t == TypeManager.char_type)
315 return new CharConstant ((char)v);
316 else if (TypeManager.IsEnumType (t)){
317 Expression e = Constantify (v, v.GetType ());
319 return new EnumConstant ((Constant) e, t);
321 throw new Exception ("Unknown type for constant (" + t +
326 /// Returns a fully formed expression after a MemberLookup
328 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
331 return new EventExpr ((EventInfo) mi, loc);
332 else if (mi is FieldInfo)
333 return new FieldExpr ((FieldInfo) mi, loc);
334 else if (mi is PropertyInfo)
335 return new PropertyExpr ((PropertyInfo) mi, loc);
336 else if (mi is Type){
337 return new TypeExpr ((System.Type) mi);
344 // Returns whether the array of memberinfos contains the given method
346 static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
348 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
350 foreach (MethodBase method in array){
351 if (method.Name != new_method.Name)
354 Type [] old_args = TypeManager.GetArgumentTypes (method);
355 int old_count = old_args.Length;
358 if (new_args.Length != old_count)
361 for (i = 0; i < old_count; i++){
362 if (old_args [i] != new_args [i])
368 if (!(method is MethodInfo && new_method is MethodInfo))
371 if (((MethodInfo) method).ReturnType == ((MethodInfo) new_method).ReturnType)
378 // We copy methods from `new_members' into `target_list' if the signature
379 // for the method from in the new list does not exist in the target_list
381 // The name is assumed to be the same.
383 public static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
385 if (target_list == null){
386 target_list = new ArrayList ();
388 target_list.AddRange (new_members);
392 MemberInfo [] target_array = new MemberInfo [target_list.Count];
393 target_list.CopyTo (target_array, 0);
395 foreach (MemberInfo mi in new_members){
396 MethodBase new_method = (MethodBase) mi;
398 if (!ArrayContainsMethod (target_array, new_method))
399 target_list.Add (new_method);
405 // FIXME: Probably implement a cache for (t,name,current_access_set)?
407 // This code could use some optimizations, but we need to do some
408 // measurements. For example, we could use a delegate to `flag' when
409 // something can not any longer be a method-group (because it is something
413 // If the return value is an Array, then it is an array of
416 // If the return value is an MemberInfo, it is anything, but a Method
420 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
421 // the arguments here and have MemberLookup return only the methods that
422 // match the argument count/type, unlike we are doing now (we delay this
425 // This is so we can catch correctly attempts to invoke instance methods
426 // from a static body (scan for error 120 in ResolveSimpleName).
429 // FIXME: Potential optimization, have a static ArrayList
432 public static Expression MemberLookup (EmitContext ec, Type t, string name,
433 MemberTypes mt, BindingFlags bf, Location loc)
435 Type source_type = ec.ContainerType;
437 if (source_type != null){
438 if (source_type == t || source_type.IsSubclassOf (t))
439 bf |= BindingFlags.NonPublic;
443 // Lookup for members starting in the type requested and going
444 // up the hierarchy until a match is found.
446 // As soon as a non-method match is found, we return.
448 // If methods are found though, then the search proceeds scanning
449 // for more public methods in the hierarchy with signatures that
450 // do not match any of the signatures found so far.
452 ArrayList method_list = null;
453 Type current_type = t;
454 bool searching = true;
458 mi = RootContext.TypeManager.FindMembers (
459 current_type, mt, bf | BindingFlags.DeclaredOnly,
460 System.Type.FilterName, name);
462 if (current_type == TypeManager.object_type)
465 current_type = current_type.BaseType;
468 // This happens with interfaces, they have a null
471 if (current_type == null)
478 int count = mi.Length;
484 // Events are returned by both `static' and `instance'
485 // searches, which means that our above FindMembers will
486 // return two copies of the same.
488 if (count == 1 && !(mi [0] is MethodBase))
489 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
490 if (count == 2 && (mi [0] is EventInfo))
491 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
494 // We found methods, turn the search into "method scan"
497 method_list = CopyNewMethods (method_list, mi);
498 mt &= (MemberTypes.Method | MemberTypes.Constructor);
501 if (method_list != null && method_list.Count > 0)
502 return new MethodGroupExpr (method_list);
507 public const MemberTypes AllMemberTypes =
508 MemberTypes.Constructor |
512 MemberTypes.NestedType |
513 MemberTypes.Property;
515 public const BindingFlags AllBindingFlags =
516 BindingFlags.Public |
517 BindingFlags.Static |
518 BindingFlags.Instance;
520 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
522 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
526 /// This is a wrapper for MemberLookup that is not used to "probe", but
527 /// to find a final definition. If the final definition is not found, we
528 /// look for private members and display a useful debugging message if we
531 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
536 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
541 e = MemberLookup (ec, t, name, AllMemberTypes,
542 AllBindingFlags | BindingFlags.NonPublic, loc);
545 117, loc, "`" + t + "' does not contain a definition " +
546 "for `" + name + "'");
549 122, loc, "`" + t + "." + name +
550 "' is inaccessible due to its protection level");
556 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
558 Type expr_type = expr.Type;
560 if (target_type == TypeManager.object_type) {
562 // A pointer type cannot be converted to object
564 if (expr_type.IsPointer)
567 if (expr_type.IsValueType)
568 return new BoxedCast (expr);
569 if (expr_type.IsClass || expr_type.IsInterface)
570 return new EmptyCast (expr, target_type);
571 } else if (expr_type.IsSubclassOf (target_type)) {
572 return new EmptyCast (expr, target_type);
575 // This code is kind of mirrored inside StandardConversionExists
576 // with the small distinction that we only probe there
578 // Always ensure that the code here and there is in sync
580 // from the null type to any reference-type.
581 if (expr is NullLiteral && !target_type.IsValueType)
582 return new EmptyCast (expr, target_type);
584 // from any class-type S to any interface-type T.
585 if (expr_type.IsClass && target_type.IsInterface) {
586 if (TypeManager.ImplementsInterface (expr_type, target_type))
587 return new EmptyCast (expr, target_type);
592 // from any interface type S to interface-type T.
593 if (expr_type.IsInterface && target_type.IsInterface) {
595 if (TypeManager.ImplementsInterface (expr_type, target_type))
596 return new EmptyCast (expr, target_type);
601 // from an array-type S to an array-type of type T
602 if (expr_type.IsArray && target_type.IsArray) {
603 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
605 Type expr_element_type = expr_type.GetElementType ();
607 if (MyEmptyExpr == null)
608 MyEmptyExpr = new EmptyExpression ();
610 MyEmptyExpr.SetType (expr_element_type);
611 Type target_element_type = target_type.GetElementType ();
613 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
614 if (StandardConversionExists (MyEmptyExpr,
615 target_element_type))
616 return new EmptyCast (expr, target_type);
621 // from an array-type to System.Array
622 if (expr_type.IsArray && target_type == TypeManager.array_type)
623 return new EmptyCast (expr, target_type);
625 // from any delegate type to System.Delegate
626 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
627 target_type == TypeManager.delegate_type)
628 return new EmptyCast (expr, target_type);
630 // from any array-type or delegate type into System.ICloneable.
631 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
632 if (target_type == TypeManager.icloneable_type)
633 return new EmptyCast (expr, target_type);
643 /// Handles expressions like this: decimal d; d = 1;
644 /// and changes them into: decimal d; d = new System.Decimal (1);
646 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
648 ArrayList args = new ArrayList ();
650 args.Add (new Argument (expr, Argument.AType.Expression));
652 Expression ne = new New (target.FullName, args,
655 return ne.Resolve (ec);
659 /// Implicit Numeric Conversions.
661 /// expr is the expression to convert, returns a new expression of type
662 /// target_type or null if an implicit conversion is not possible.
664 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
665 Type target_type, Location loc)
667 Type expr_type = expr.Type;
670 // Attempt to do the implicit constant expression conversions
672 if (expr is IntConstant){
675 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
679 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
681 // Try the implicit constant expression conversion
682 // from long to ulong, instead of a nice routine,
685 long v = ((LongConstant) expr).Value;
687 return new ULongConstant ((ulong) v);
691 // If we have an enumeration, extract the underlying type,
692 // use this during the comparission, but wrap around the original
695 Type real_target_type = target_type;
697 if (TypeManager.IsEnumType (real_target_type))
698 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
700 if (expr_type == real_target_type)
701 return new EmptyCast (expr, target_type);
703 if (expr_type == TypeManager.sbyte_type){
705 // From sbyte to short, int, long, float, double.
707 if (real_target_type == TypeManager.int32_type)
708 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
709 if (real_target_type == TypeManager.int64_type)
710 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
711 if (real_target_type == TypeManager.double_type)
712 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
713 if (real_target_type == TypeManager.float_type)
714 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
715 if (real_target_type == TypeManager.short_type)
716 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
717 if (real_target_type == TypeManager.decimal_type)
718 return InternalTypeConstructor (ec, expr, target_type);
719 } else if (expr_type == TypeManager.byte_type){
721 // From byte to short, ushort, int, uint, long, ulong, float, double
723 if ((real_target_type == TypeManager.short_type) ||
724 (real_target_type == TypeManager.ushort_type) ||
725 (real_target_type == TypeManager.int32_type) ||
726 (real_target_type == TypeManager.uint32_type))
727 return new EmptyCast (expr, target_type);
729 if (real_target_type == TypeManager.uint64_type)
730 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
731 if (real_target_type == TypeManager.int64_type)
732 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
733 if (real_target_type == TypeManager.float_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
735 if (real_target_type == TypeManager.double_type)
736 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
737 if (real_target_type == TypeManager.decimal_type)
738 return InternalTypeConstructor (ec, expr, target_type);
739 } else if (expr_type == TypeManager.short_type){
741 // From short to int, long, float, double
743 if (real_target_type == TypeManager.int32_type)
744 return new EmptyCast (expr, target_type);
745 if (real_target_type == TypeManager.int64_type)
746 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
747 if (real_target_type == TypeManager.double_type)
748 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
749 if (real_target_type == TypeManager.float_type)
750 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
751 if (real_target_type == TypeManager.decimal_type)
752 return InternalTypeConstructor (ec, expr, target_type);
753 } else if (expr_type == TypeManager.ushort_type){
755 // From ushort to int, uint, long, ulong, float, double
757 if (real_target_type == TypeManager.uint32_type)
758 return new EmptyCast (expr, target_type);
760 if (real_target_type == TypeManager.uint64_type)
761 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
762 if (real_target_type == TypeManager.int32_type)
763 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
764 if (real_target_type == TypeManager.int64_type)
765 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
766 if (real_target_type == TypeManager.double_type)
767 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
768 if (real_target_type == TypeManager.float_type)
769 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
770 if (real_target_type == TypeManager.decimal_type)
771 return InternalTypeConstructor (ec, expr, target_type);
772 } else if (expr_type == TypeManager.int32_type){
774 // From int to long, float, double
776 if (real_target_type == TypeManager.int64_type)
777 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
778 if (real_target_type == TypeManager.double_type)
779 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
780 if (real_target_type == TypeManager.float_type)
781 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
782 if (real_target_type == TypeManager.decimal_type)
783 return InternalTypeConstructor (ec, expr, target_type);
784 } else if (expr_type == TypeManager.uint32_type){
786 // From uint to long, ulong, float, double
788 if (real_target_type == TypeManager.int64_type)
789 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
790 if (real_target_type == TypeManager.uint64_type)
791 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
792 if (real_target_type == TypeManager.double_type)
793 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
795 if (real_target_type == TypeManager.float_type)
796 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
798 if (real_target_type == TypeManager.decimal_type)
799 return InternalTypeConstructor (ec, expr, target_type);
800 } else if ((expr_type == TypeManager.uint64_type) ||
801 (expr_type == TypeManager.int64_type)){
803 // From long/ulong to float, double
805 if (real_target_type == TypeManager.double_type)
806 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
808 if (real_target_type == TypeManager.float_type)
809 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
811 if (real_target_type == TypeManager.decimal_type)
812 return InternalTypeConstructor (ec, expr, target_type);
813 } else if (expr_type == TypeManager.char_type){
815 // From char to ushort, int, uint, long, ulong, float, double
817 if ((real_target_type == TypeManager.ushort_type) ||
818 (real_target_type == TypeManager.int32_type) ||
819 (real_target_type == TypeManager.uint32_type))
820 return new EmptyCast (expr, target_type);
821 if (real_target_type == TypeManager.uint64_type)
822 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
823 if (real_target_type == TypeManager.int64_type)
824 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
825 if (real_target_type == TypeManager.float_type)
826 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
827 if (real_target_type == TypeManager.double_type)
828 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
829 if (real_target_type == TypeManager.decimal_type)
830 return InternalTypeConstructor (ec, expr, target_type);
831 } else if (expr_type == TypeManager.float_type){
835 if (real_target_type == TypeManager.double_type)
836 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
843 /// Determines if a standard implicit conversion exists from
844 /// expr_type to target_type
846 public static bool StandardConversionExists (Expression expr, Type target_type)
848 Type expr_type = expr.Type;
850 if (expr_type == target_type)
853 // First numeric conversions
855 if (expr_type == TypeManager.sbyte_type){
857 // From sbyte to short, int, long, float, double.
859 if ((target_type == TypeManager.int32_type) ||
860 (target_type == TypeManager.int64_type) ||
861 (target_type == TypeManager.double_type) ||
862 (target_type == TypeManager.float_type) ||
863 (target_type == TypeManager.short_type) ||
864 (target_type == TypeManager.decimal_type))
867 } else if (expr_type == TypeManager.byte_type){
869 // From byte to short, ushort, int, uint, long, ulong, float, double
871 if ((target_type == TypeManager.short_type) ||
872 (target_type == TypeManager.ushort_type) ||
873 (target_type == TypeManager.int32_type) ||
874 (target_type == TypeManager.uint32_type) ||
875 (target_type == TypeManager.uint64_type) ||
876 (target_type == TypeManager.int64_type) ||
877 (target_type == TypeManager.float_type) ||
878 (target_type == TypeManager.double_type) ||
879 (target_type == TypeManager.decimal_type))
882 } else if (expr_type == TypeManager.short_type){
884 // From short to int, long, float, double
886 if ((target_type == TypeManager.int32_type) ||
887 (target_type == TypeManager.int64_type) ||
888 (target_type == TypeManager.double_type) ||
889 (target_type == TypeManager.float_type) ||
890 (target_type == TypeManager.decimal_type))
893 } else if (expr_type == TypeManager.ushort_type){
895 // From ushort to int, uint, long, ulong, float, double
897 if ((target_type == TypeManager.uint32_type) ||
898 (target_type == TypeManager.uint64_type) ||
899 (target_type == TypeManager.int32_type) ||
900 (target_type == TypeManager.int64_type) ||
901 (target_type == TypeManager.double_type) ||
902 (target_type == TypeManager.float_type) ||
903 (target_type == TypeManager.decimal_type))
906 } else if (expr_type == TypeManager.int32_type){
908 // From int to long, float, double
910 if ((target_type == TypeManager.int64_type) ||
911 (target_type == TypeManager.double_type) ||
912 (target_type == TypeManager.float_type) ||
913 (target_type == TypeManager.decimal_type))
916 } else if (expr_type == TypeManager.uint32_type){
918 // From uint to long, ulong, float, double
920 if ((target_type == TypeManager.int64_type) ||
921 (target_type == TypeManager.uint64_type) ||
922 (target_type == TypeManager.double_type) ||
923 (target_type == TypeManager.float_type) ||
924 (target_type == TypeManager.decimal_type))
927 } else if ((expr_type == TypeManager.uint64_type) ||
928 (expr_type == TypeManager.int64_type)) {
930 // From long/ulong to float, double
932 if ((target_type == TypeManager.double_type) ||
933 (target_type == TypeManager.float_type) ||
934 (target_type == TypeManager.decimal_type))
937 } else if (expr_type == TypeManager.char_type){
939 // From char to ushort, int, uint, long, ulong, float, double
941 if ((target_type == TypeManager.ushort_type) ||
942 (target_type == TypeManager.int32_type) ||
943 (target_type == TypeManager.uint32_type) ||
944 (target_type == TypeManager.uint64_type) ||
945 (target_type == TypeManager.int64_type) ||
946 (target_type == TypeManager.float_type) ||
947 (target_type == TypeManager.double_type) ||
948 (target_type == TypeManager.decimal_type))
951 } else if (expr_type == TypeManager.float_type){
955 if (target_type == TypeManager.double_type)
959 // Next reference conversions
961 if (target_type == TypeManager.object_type) {
962 if ((expr_type.IsClass) ||
963 (expr_type.IsValueType))
966 } else if (expr_type.IsSubclassOf (target_type)) {
970 // Please remember that all code below actually comes
971 // from ImplicitReferenceConversion so make sure code remains in sync
973 // from any class-type S to any interface-type T.
974 if (expr_type.IsClass && target_type.IsInterface) {
975 if (TypeManager.ImplementsInterface (expr_type, target_type))
979 // from any interface type S to interface-type T.
980 // FIXME : Is it right to use IsAssignableFrom ?
981 if (expr_type.IsInterface && target_type.IsInterface)
982 if (target_type.IsAssignableFrom (expr_type))
985 // from an array-type S to an array-type of type T
986 if (expr_type.IsArray && target_type.IsArray) {
987 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
989 Type expr_element_type = expr_type.GetElementType ();
991 if (MyEmptyExpr == null)
992 MyEmptyExpr = new EmptyExpression ();
994 MyEmptyExpr.SetType (expr_element_type);
995 Type target_element_type = target_type.GetElementType ();
997 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
998 if (StandardConversionExists (MyEmptyExpr,
999 target_element_type))
1004 // from an array-type to System.Array
1005 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
1008 // from any delegate type to System.Delegate
1009 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1010 target_type == TypeManager.delegate_type)
1011 if (target_type.IsAssignableFrom (expr_type))
1014 // from any array-type or delegate type into System.ICloneable.
1015 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1016 if (target_type == TypeManager.icloneable_type)
1019 // from the null type to any reference-type.
1020 if (expr is NullLiteral && !target_type.IsValueType)
1028 static EmptyExpression MyEmptyExpr;
1030 /// Tells whether an implicit conversion exists from expr_type to
1033 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
1036 if (MyEmptyExpr == null)
1037 MyEmptyExpr = new EmptyExpression (expr_type);
1039 MyEmptyExpr.SetType (expr_type);
1041 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
1045 /// Finds "most encompassed type" according to the spec (13.4.2)
1046 /// amongst the methods in the MethodGroupExpr which convert from a
1047 /// type encompassing source_type
1049 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
1053 for (int i = me.Methods.Length; i > 0; ) {
1056 MethodBase mb = me.Methods [i];
1057 ParameterData pd = Invocation.GetParameterData (mb);
1058 Type param_type = pd.ParameterType (0);
1060 Expression source = new EmptyExpression (source_type);
1061 Expression param = new EmptyExpression (param_type);
1063 if (StandardConversionExists (source, param_type)) {
1067 if (StandardConversionExists (param, best))
1076 /// Finds "most encompassing type" according to the spec (13.4.2)
1077 /// amongst the methods in the MethodGroupExpr which convert to a
1078 /// type encompassed by target_type
1080 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
1084 for (int i = me.Methods.Length; i > 0; ) {
1087 MethodInfo mi = (MethodInfo) me.Methods [i];
1088 Type ret_type = mi.ReturnType;
1090 Expression ret = new EmptyExpression (ret_type);
1092 if (StandardConversionExists (ret, target)) {
1096 if (!StandardConversionExists (ret, best))
1108 /// User-defined Implicit conversions
1110 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1111 Type target, Location loc)
1113 return UserDefinedConversion (ec, source, target, loc, false);
1117 /// User-defined Explicit conversions
1119 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1120 Type target, Location loc)
1122 return UserDefinedConversion (ec, source, target, loc, true);
1126 /// User-defined conversions
1128 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1129 Type target, Location loc,
1130 bool look_for_explicit)
1132 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1133 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1135 MethodBase method = null;
1136 Type source_type = source.Type;
1140 // If we have a boolean type, we need to check for the True operator
1142 // FIXME : How does the False operator come into the picture ?
1143 // FIXME : This doesn't look complete and very correct !
1144 if (target == TypeManager.bool_type)
1145 op_name = "op_True";
1147 op_name = "op_Implicit";
1149 mg1 = MemberLookup (ec, source_type, op_name, loc);
1151 if (source_type.BaseType != null)
1152 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1154 mg3 = MemberLookup (ec, target, op_name, loc);
1156 if (target.BaseType != null)
1157 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1159 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1160 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1162 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1164 MethodGroupExpr union4 = null;
1166 if (look_for_explicit) {
1168 op_name = "op_Explicit";
1170 mg5 = MemberLookup (ec, source_type, op_name, loc);
1172 if (source_type.BaseType != null)
1173 mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1175 mg7 = MemberLookup (ec, target, op_name, loc);
1177 if (target.BaseType != null)
1178 mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1180 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1181 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1183 union4 = Invocation.MakeUnionSet (union5, union6);
1186 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1188 if (union != null) {
1190 Type most_specific_source, most_specific_target;
1192 most_specific_source = FindMostEncompassedType (union, source_type);
1193 if (most_specific_source == null)
1196 most_specific_target = FindMostEncompassingType (union, target);
1197 if (most_specific_target == null)
1202 for (int i = union.Methods.Length; i > 0;) {
1205 MethodBase mb = union.Methods [i];
1206 ParameterData pd = Invocation.GetParameterData (mb);
1207 MethodInfo mi = (MethodInfo) union.Methods [i];
1209 if (pd.ParameterType (0) == most_specific_source &&
1210 mi.ReturnType == most_specific_target) {
1216 if (method == null || count > 1) {
1217 Report.Error (-11, loc, "Ambiguous user defined conversion");
1222 // This will do the conversion to the best match that we
1223 // found. Now we need to perform an implict standard conversion
1224 // if the best match was not the type that we were requested
1227 if (look_for_explicit)
1228 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1230 source = ConvertImplicitStandard (ec, source,
1231 most_specific_source, loc);
1236 e = new UserCast ((MethodInfo) method, source);
1238 if (e.Type != target){
1239 if (!look_for_explicit)
1240 e = ConvertImplicitStandard (ec, e, target, loc);
1242 e = ConvertExplicitStandard (ec, e, target, loc);
1253 /// Converts implicitly the resolved expression `expr' into the
1254 /// `target_type'. It returns a new expression that can be used
1255 /// in a context that expects a `target_type'.
1257 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1258 Type target_type, Location loc)
1260 Type expr_type = expr.Type;
1263 if (expr_type == target_type)
1266 if (target_type == null)
1267 throw new Exception ("Target type is null");
1269 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1273 e = ImplicitUserConversion (ec, expr, target_type, loc);
1282 /// Attempts to apply the `Standard Implicit
1283 /// Conversion' rules to the expression `expr' into
1284 /// the `target_type'. It returns a new expression
1285 /// that can be used in a context that expects a
1288 /// This is different from `ConvertImplicit' in that the
1289 /// user defined implicit conversions are excluded.
1291 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1292 Type target_type, Location loc)
1294 Type expr_type = expr.Type;
1297 if (expr_type == target_type)
1300 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1304 e = ImplicitReferenceConversion (expr, target_type);
1308 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1309 IntLiteral i = (IntLiteral) expr;
1312 return new EmptyCast (expr, target_type);
1316 if (expr_type.IsPointer){
1317 if (target_type == TypeManager.void_ptr_type)
1318 return new EmptyCast (expr, target_type);
1321 // yep, comparing pointer types cant be done with
1322 // t1 == t2, we have to compare their element types.
1324 if (target_type.IsPointer){
1325 if (target_type.GetElementType()==expr_type.GetElementType())
1330 if (target_type.IsPointer){
1331 if (expr is NullLiteral)
1332 return new EmptyCast (expr, target_type);
1340 /// Attemps to perform an implict constant conversion of the IntConstant
1341 /// into a different data type using casts (See Implicit Constant
1342 /// Expression Conversions)
1344 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1346 int value = ic.Value;
1349 // FIXME: This could return constants instead of EmptyCasts
1351 if (target_type == TypeManager.sbyte_type){
1352 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1353 return new SByteConstant ((sbyte) value);
1354 } else if (target_type == TypeManager.byte_type){
1355 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1356 return new ByteConstant ((byte) value);
1357 } else if (target_type == TypeManager.short_type){
1358 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1359 return new ShortConstant ((short) value);
1360 } else if (target_type == TypeManager.ushort_type){
1361 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1362 return new UShortConstant ((ushort) value);
1363 } else if (target_type == TypeManager.uint32_type){
1365 return new UIntConstant ((uint) value);
1366 } else if (target_type == TypeManager.uint64_type){
1368 // we can optimize this case: a positive int32
1369 // always fits on a uint64. But we need an opcode
1373 return new ULongConstant ((ulong) value);
1376 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1377 return new EnumConstant (ic, target_type);
1382 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1384 string msg = "Cannot convert implicitly from `"+
1385 TypeManager.CSharpName (source) + "' to `" +
1386 TypeManager.CSharpName (target) + "'";
1388 Error (29, loc, msg);
1392 /// Attemptes to implicityly convert `target' into `type', using
1393 /// ConvertImplicit. If there is no implicit conversion, then
1394 /// an error is signaled
1396 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1397 Type target_type, Location loc)
1401 e = ConvertImplicit (ec, source, target_type, loc);
1405 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1407 "Double literal cannot be implicitly converted to " +
1408 "float type, use F suffix to create a float literal");
1411 Error_CannotConvertImplicit (loc, source.Type, target_type);
1417 /// Performs the explicit numeric conversions
1419 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1422 Type expr_type = expr.Type;
1425 // If we have an enumeration, extract the underlying type,
1426 // use this during the comparission, but wrap around the original
1429 Type real_target_type = target_type;
1431 if (TypeManager.IsEnumType (real_target_type))
1432 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1434 if (expr_type == TypeManager.sbyte_type){
1436 // From sbyte to byte, ushort, uint, ulong, char
1438 if (real_target_type == TypeManager.byte_type)
1439 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1440 if (real_target_type == TypeManager.ushort_type)
1441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1442 if (real_target_type == TypeManager.uint32_type)
1443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1444 if (real_target_type == TypeManager.uint64_type)
1445 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1446 if (real_target_type == TypeManager.char_type)
1447 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1448 } else if (expr_type == TypeManager.byte_type){
1450 // From byte to sbyte and char
1452 if (real_target_type == TypeManager.sbyte_type)
1453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1454 if (real_target_type == TypeManager.char_type)
1455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1456 } else if (expr_type == TypeManager.short_type){
1458 // From short to sbyte, byte, ushort, uint, ulong, char
1460 if (real_target_type == TypeManager.sbyte_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1462 if (real_target_type == TypeManager.byte_type)
1463 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1464 if (real_target_type == TypeManager.ushort_type)
1465 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1466 if (real_target_type == TypeManager.uint32_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1468 if (real_target_type == TypeManager.uint64_type)
1469 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1470 if (real_target_type == TypeManager.char_type)
1471 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1472 } else if (expr_type == TypeManager.ushort_type){
1474 // From ushort to sbyte, byte, short, char
1476 if (real_target_type == TypeManager.sbyte_type)
1477 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1478 if (real_target_type == TypeManager.byte_type)
1479 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1480 if (real_target_type == TypeManager.short_type)
1481 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1482 if (real_target_type == TypeManager.char_type)
1483 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1484 } else if (expr_type == TypeManager.int32_type){
1486 // From int to sbyte, byte, short, ushort, uint, ulong, char
1488 if (real_target_type == TypeManager.sbyte_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1490 if (real_target_type == TypeManager.byte_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1492 if (real_target_type == TypeManager.short_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1494 if (real_target_type == TypeManager.ushort_type)
1495 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1496 if (real_target_type == TypeManager.uint32_type)
1497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1498 if (real_target_type == TypeManager.uint64_type)
1499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1500 if (real_target_type == TypeManager.char_type)
1501 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1502 } else if (expr_type == TypeManager.uint32_type){
1504 // From uint to sbyte, byte, short, ushort, int, char
1506 if (real_target_type == TypeManager.sbyte_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1508 if (real_target_type == TypeManager.byte_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1510 if (real_target_type == TypeManager.short_type)
1511 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1512 if (real_target_type == TypeManager.ushort_type)
1513 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1514 if (real_target_type == TypeManager.int32_type)
1515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1516 if (real_target_type == TypeManager.char_type)
1517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1518 } else if (expr_type == TypeManager.int64_type){
1520 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1522 if (real_target_type == TypeManager.sbyte_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1524 if (real_target_type == TypeManager.byte_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1526 if (real_target_type == TypeManager.short_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1528 if (real_target_type == TypeManager.ushort_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1530 if (real_target_type == TypeManager.int32_type)
1531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1532 if (real_target_type == TypeManager.uint32_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1534 if (real_target_type == TypeManager.uint64_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1536 if (real_target_type == TypeManager.char_type)
1537 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1538 } else if (expr_type == TypeManager.uint64_type){
1540 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1542 if (real_target_type == TypeManager.sbyte_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1544 if (real_target_type == TypeManager.byte_type)
1545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1546 if (real_target_type == TypeManager.short_type)
1547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1548 if (real_target_type == TypeManager.ushort_type)
1549 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1550 if (real_target_type == TypeManager.int32_type)
1551 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1552 if (real_target_type == TypeManager.uint32_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1554 if (real_target_type == TypeManager.int64_type)
1555 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1556 if (real_target_type == TypeManager.char_type)
1557 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1558 } else if (expr_type == TypeManager.char_type){
1560 // From char to sbyte, byte, short
1562 if (real_target_type == TypeManager.sbyte_type)
1563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1564 if (real_target_type == TypeManager.byte_type)
1565 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1566 if (real_target_type == TypeManager.short_type)
1567 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1568 } else if (expr_type == TypeManager.float_type){
1570 // From float to sbyte, byte, short,
1571 // ushort, int, uint, long, ulong, char
1574 if (real_target_type == TypeManager.sbyte_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1576 if (real_target_type == TypeManager.byte_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1578 if (real_target_type == TypeManager.short_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1580 if (real_target_type == TypeManager.ushort_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1582 if (real_target_type == TypeManager.int32_type)
1583 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1584 if (real_target_type == TypeManager.uint32_type)
1585 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1586 if (real_target_type == TypeManager.int64_type)
1587 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1588 if (real_target_type == TypeManager.uint64_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1590 if (real_target_type == TypeManager.char_type)
1591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1592 if (real_target_type == TypeManager.decimal_type)
1593 return InternalTypeConstructor (ec, expr, target_type);
1594 } else if (expr_type == TypeManager.double_type){
1596 // From double to byte, byte, short,
1597 // ushort, int, uint, long, ulong,
1598 // char, float or decimal
1600 if (real_target_type == TypeManager.sbyte_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1602 if (real_target_type == TypeManager.byte_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1604 if (real_target_type == TypeManager.short_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1606 if (real_target_type == TypeManager.ushort_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1608 if (real_target_type == TypeManager.int32_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1610 if (real_target_type == TypeManager.uint32_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1612 if (real_target_type == TypeManager.int64_type)
1613 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1614 if (real_target_type == TypeManager.uint64_type)
1615 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1616 if (real_target_type == TypeManager.char_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1618 if (real_target_type == TypeManager.float_type)
1619 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1620 if (real_target_type == TypeManager.decimal_type)
1621 return InternalTypeConstructor (ec, expr, target_type);
1624 // decimal is taken care of by the op_Explicit methods.
1630 /// Returns whether an explicit reference conversion can be performed
1631 /// from source_type to target_type
1633 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1635 bool target_is_value_type = target_type.IsValueType;
1637 if (source_type == target_type)
1641 // From object to any reference type
1643 if (source_type == TypeManager.object_type && !target_is_value_type)
1647 // From any class S to any class-type T, provided S is a base class of T
1649 if (target_type.IsSubclassOf (source_type))
1653 // From any interface type S to any interface T provided S is not derived from T
1655 if (source_type.IsInterface && target_type.IsInterface){
1656 if (!target_type.IsSubclassOf (source_type))
1661 // From any class type S to any interface T, provides S is not sealed
1662 // and provided S does not implement T.
1664 if (target_type.IsInterface && !source_type.IsSealed &&
1665 !target_type.IsAssignableFrom (source_type))
1669 // From any interface-type S to to any class type T, provided T is not
1670 // sealed, or provided T implements S.
1672 if (source_type.IsInterface &&
1673 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1676 // From an array type S with an element type Se to an array type T with an
1677 // element type Te provided all the following are true:
1678 // * S and T differe only in element type, in other words, S and T
1679 // have the same number of dimensions.
1680 // * Both Se and Te are reference types
1681 // * An explicit referenc conversions exist from Se to Te
1683 if (source_type.IsArray && target_type.IsArray) {
1684 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1686 Type source_element_type = source_type.GetElementType ();
1687 Type target_element_type = target_type.GetElementType ();
1689 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1690 if (ExplicitReferenceConversionExists (source_element_type,
1691 target_element_type))
1697 // From System.Array to any array-type
1698 if (source_type == TypeManager.array_type &&
1699 target_type.IsSubclassOf (TypeManager.array_type)){
1704 // From System delegate to any delegate-type
1706 if (source_type == TypeManager.delegate_type &&
1707 target_type.IsSubclassOf (TypeManager.delegate_type))
1711 // From ICloneable to Array or Delegate types
1713 if (source_type == TypeManager.icloneable_type &&
1714 (target_type == TypeManager.array_type ||
1715 target_type == TypeManager.delegate_type))
1722 /// Implements Explicit Reference conversions
1724 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1726 Type source_type = source.Type;
1727 bool target_is_value_type = target_type.IsValueType;
1730 // From object to any reference type
1732 if (source_type == TypeManager.object_type && !target_is_value_type)
1733 return new ClassCast (source, target_type);
1737 // From any class S to any class-type T, provided S is a base class of T
1739 if (target_type.IsSubclassOf (source_type))
1740 return new ClassCast (source, target_type);
1743 // From any interface type S to any interface T provided S is not derived from T
1745 if (source_type.IsInterface && target_type.IsInterface){
1746 if (TypeManager.ImplementsInterface (source_type, target_type))
1749 return new ClassCast (source, target_type);
1753 // From any class type S to any interface T, provides S is not sealed
1754 // and provided S does not implement T.
1756 if (target_type.IsInterface && !source_type.IsSealed) {
1758 if (TypeManager.ImplementsInterface (source_type, target_type))
1761 return new ClassCast (source, target_type);
1766 // From any interface-type S to to any class type T, provided T is not
1767 // sealed, or provided T implements S.
1769 if (source_type.IsInterface) {
1771 if (target_type.IsSealed)
1774 if (TypeManager.ImplementsInterface (target_type, source_type))
1775 return new ClassCast (source, target_type);
1780 // From an array type S with an element type Se to an array type T with an
1781 // element type Te provided all the following are true:
1782 // * S and T differe only in element type, in other words, S and T
1783 // have the same number of dimensions.
1784 // * Both Se and Te are reference types
1785 // * An explicit referenc conversions exist from Se to Te
1787 if (source_type.IsArray && target_type.IsArray) {
1788 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1790 Type source_element_type = source_type.GetElementType ();
1791 Type target_element_type = target_type.GetElementType ();
1793 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1794 if (ExplicitReferenceConversionExists (source_element_type,
1795 target_element_type))
1796 return new ClassCast (source, target_type);
1801 // From System.Array to any array-type
1802 if (source_type == TypeManager.array_type &&
1803 target_type.IsSubclassOf (TypeManager.array_type)){
1804 return new ClassCast (source, target_type);
1808 // From System delegate to any delegate-type
1810 if (source_type == TypeManager.delegate_type &&
1811 target_type.IsSubclassOf (TypeManager.delegate_type))
1812 return new ClassCast (source, target_type);
1815 // From ICloneable to Array or Delegate types
1817 if (source_type == TypeManager.icloneable_type &&
1818 (target_type == TypeManager.array_type ||
1819 target_type == TypeManager.delegate_type))
1820 return new ClassCast (source, target_type);
1826 /// Performs an explicit conversion of the expression `expr' whose
1827 /// type is expr.Type to `target_type'.
1829 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1830 Type target_type, Location loc)
1832 Type expr_type = expr.Type;
1833 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1838 ne = ConvertNumericExplicit (ec, expr, target_type);
1843 // Unboxing conversion.
1845 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1846 return new UnboxCast (expr, target_type);
1851 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1855 // FIXME: Is there any reason we should have EnumConstant
1856 // dealt with here instead of just using always the
1857 // UnderlyingSystemType to wrap the type?
1859 if (expr is EnumConstant)
1860 e = ((EnumConstant) expr).Child;
1862 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1865 Expression t = ConvertImplicit (ec, e, target_type, loc);
1869 return ConvertNumericExplicit (ec, e, target_type);
1872 ne = ConvertReferenceExplicit (expr, target_type);
1877 if (target_type.IsPointer){
1878 if (expr_type.IsPointer)
1879 return new EmptyCast (expr, target_type);
1881 if (expr_type == TypeManager.sbyte_type ||
1882 expr_type == TypeManager.byte_type ||
1883 expr_type == TypeManager.short_type ||
1884 expr_type == TypeManager.ushort_type ||
1885 expr_type == TypeManager.int32_type ||
1886 expr_type == TypeManager.uint32_type ||
1887 expr_type == TypeManager.uint64_type ||
1888 expr_type == TypeManager.int64_type)
1889 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1891 if (expr_type.IsPointer){
1892 if (target_type == TypeManager.sbyte_type ||
1893 target_type == TypeManager.byte_type ||
1894 target_type == TypeManager.short_type ||
1895 target_type == TypeManager.ushort_type ||
1896 target_type == TypeManager.int32_type ||
1897 target_type == TypeManager.uint32_type ||
1898 target_type == TypeManager.uint64_type ||
1899 target_type == TypeManager.int64_type){
1900 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1903 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1908 ce = ConvertNumericExplicit (ec, e, target_type);
1912 // We should always be able to go from an uint32
1913 // implicitly or explicitly to the other integral
1916 throw new Exception ("Internal compiler error");
1921 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1925 Error_CannotConvertType (loc, expr_type, target_type);
1930 /// Same as ConverExplicit, only it doesn't include user defined conversions
1932 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1933 Type target_type, Location l)
1935 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1940 ne = ConvertNumericExplicit (ec, expr, target_type);
1944 ne = ConvertReferenceExplicit (expr, target_type);
1948 Error_CannotConvertType (l, expr.Type, target_type);
1952 static string ExprClassName (ExprClass c)
1955 case ExprClass.Invalid:
1957 case ExprClass.Value:
1959 case ExprClass.Variable:
1961 case ExprClass.Namespace:
1963 case ExprClass.Type:
1965 case ExprClass.MethodGroup:
1966 return "method group";
1967 case ExprClass.PropertyAccess:
1968 return "property access";
1969 case ExprClass.EventAccess:
1970 return "event access";
1971 case ExprClass.IndexerAccess:
1972 return "indexer access";
1973 case ExprClass.Nothing:
1976 throw new Exception ("Should not happen");
1980 /// Reports that we were expecting `expr' to be of class `expected'
1982 protected void report118 (Location loc, Expression expr, string expected)
1984 string kind = "Unknown";
1987 kind = ExprClassName (expr.eclass);
1989 Error (118, loc, "Expression denotes a `" + kind +
1990 "' where a `" + expected + "' was expected");
1993 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
1995 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1996 TypeManager.CSharpName (t));
1999 public static void UnsafeError (Location loc)
2001 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2005 /// Converts the IntConstant, UIntConstant, LongConstant or
2006 /// ULongConstant into the integral target_type. Notice
2007 /// that we do not return an `Expression' we do return
2008 /// a boxed integral type.
2010 /// FIXME: Since I added the new constants, we need to
2011 /// also support conversions from CharConstant, ByteConstant,
2012 /// SByteConstant, UShortConstant, ShortConstant
2014 /// This is used by the switch statement, so the domain
2015 /// of work is restricted to the literals above, and the
2016 /// targets are int32, uint32, char, byte, sbyte, ushort,
2017 /// short, uint64 and int64
2019 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2023 if (c.Type == target_type)
2024 return ((Constant) c).GetValue ();
2027 // Make into one of the literals we handle, we dont really care
2028 // about this value as we will just return a few limited types
2030 if (c is EnumConstant)
2031 c = ((EnumConstant)c).WidenToCompilerConstant ();
2033 if (c is IntConstant){
2034 int v = ((IntConstant) c).Value;
2036 if (target_type == TypeManager.uint32_type){
2039 } else if (target_type == TypeManager.char_type){
2040 if (v >= Char.MinValue && v <= Char.MaxValue)
2042 } else if (target_type == TypeManager.byte_type){
2043 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2045 } else if (target_type == TypeManager.sbyte_type){
2046 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2048 } else if (target_type == TypeManager.short_type){
2049 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2051 } else if (target_type == TypeManager.ushort_type){
2052 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2054 } else if (target_type == TypeManager.int64_type)
2056 else if (target_type == TypeManager.uint64_type){
2062 } else if (c is UIntConstant){
2063 uint v = ((UIntConstant) c).Value;
2065 if (target_type == TypeManager.int32_type){
2066 if (v <= Int32.MaxValue)
2068 } else if (target_type == TypeManager.char_type){
2069 if (v >= Char.MinValue && v <= Char.MaxValue)
2071 } else if (target_type == TypeManager.byte_type){
2072 if (v <= Byte.MaxValue)
2074 } else if (target_type == TypeManager.sbyte_type){
2075 if (v <= SByte.MaxValue)
2077 } else if (target_type == TypeManager.short_type){
2078 if (v <= UInt16.MaxValue)
2080 } else if (target_type == TypeManager.ushort_type){
2081 if (v <= UInt16.MaxValue)
2083 } else if (target_type == TypeManager.int64_type)
2085 else if (target_type == TypeManager.uint64_type)
2088 } else if (c is LongConstant){
2089 long v = ((LongConstant) c).Value;
2091 if (target_type == TypeManager.int32_type){
2092 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2094 } else if (target_type == TypeManager.uint32_type){
2095 if (v >= 0 && v <= UInt32.MaxValue)
2097 } else if (target_type == TypeManager.char_type){
2098 if (v >= Char.MinValue && v <= Char.MaxValue)
2100 } else if (target_type == TypeManager.byte_type){
2101 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2103 } else if (target_type == TypeManager.sbyte_type){
2104 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2106 } else if (target_type == TypeManager.short_type){
2107 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2109 } else if (target_type == TypeManager.ushort_type){
2110 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2112 } else if (target_type == TypeManager.uint64_type){
2117 } else if (c is ULongConstant){
2118 ulong v = ((ULongConstant) c).Value;
2120 if (target_type == TypeManager.int32_type){
2121 if (v <= Int32.MaxValue)
2123 } else if (target_type == TypeManager.uint32_type){
2124 if (v <= UInt32.MaxValue)
2126 } else if (target_type == TypeManager.char_type){
2127 if (v >= Char.MinValue && v <= Char.MaxValue)
2129 } else if (target_type == TypeManager.byte_type){
2130 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2132 } else if (target_type == TypeManager.sbyte_type){
2133 if (v <= (int) SByte.MaxValue)
2135 } else if (target_type == TypeManager.short_type){
2136 if (v <= UInt16.MaxValue)
2138 } else if (target_type == TypeManager.ushort_type){
2139 if (v <= UInt16.MaxValue)
2141 } else if (target_type == TypeManager.int64_type){
2142 if (v <= Int64.MaxValue)
2146 } else if (c is ByteConstant){
2147 byte v = ((ByteConstant) c).Value;
2149 if (target_type == TypeManager.int32_type)
2151 else if (target_type == TypeManager.uint32_type)
2153 else if (target_type == TypeManager.char_type)
2155 else if (target_type == TypeManager.sbyte_type){
2156 if (v <= SByte.MaxValue)
2158 } else if (target_type == TypeManager.short_type)
2160 else if (target_type == TypeManager.ushort_type)
2162 else if (target_type == TypeManager.int64_type)
2164 else if (target_type == TypeManager.uint64_type)
2167 } else if (c is SByteConstant){
2168 sbyte v = ((SByteConstant) c).Value;
2170 if (target_type == TypeManager.int32_type)
2172 else if (target_type == TypeManager.uint32_type){
2175 } else if (target_type == TypeManager.char_type){
2178 } else if (target_type == TypeManager.byte_type){
2181 } else if (target_type == TypeManager.short_type)
2183 else if (target_type == TypeManager.ushort_type){
2186 } else if (target_type == TypeManager.int64_type)
2188 else if (target_type == TypeManager.uint64_type){
2193 } else if (c is ShortConstant){
2194 short v = ((ShortConstant) c).Value;
2196 if (target_type == TypeManager.int32_type){
2198 } else if (target_type == TypeManager.uint32_type){
2201 } else if (target_type == TypeManager.char_type){
2204 } else if (target_type == TypeManager.byte_type){
2205 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2207 } else if (target_type == TypeManager.sbyte_type){
2208 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2210 } else if (target_type == TypeManager.ushort_type){
2213 } else if (target_type == TypeManager.int64_type)
2215 else if (target_type == TypeManager.uint64_type)
2219 } else if (c is UShortConstant){
2220 ushort v = ((UShortConstant) c).Value;
2222 if (target_type == TypeManager.int32_type)
2224 else if (target_type == TypeManager.uint32_type)
2226 else if (target_type == TypeManager.char_type){
2227 if (v >= Char.MinValue && v <= Char.MaxValue)
2229 } else if (target_type == TypeManager.byte_type){
2230 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2232 } else if (target_type == TypeManager.sbyte_type){
2233 if (v <= SByte.MaxValue)
2235 } else if (target_type == TypeManager.short_type){
2236 if (v <= Int16.MaxValue)
2238 } else if (target_type == TypeManager.int64_type)
2240 else if (target_type == TypeManager.uint64_type)
2244 } else if (c is CharConstant){
2245 char v = ((CharConstant) c).Value;
2247 if (target_type == TypeManager.int32_type)
2249 else if (target_type == TypeManager.uint32_type)
2251 else if (target_type == TypeManager.byte_type){
2252 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2254 } else if (target_type == TypeManager.sbyte_type){
2255 if (v <= SByte.MaxValue)
2257 } else if (target_type == TypeManager.short_type){
2258 if (v <= Int16.MaxValue)
2260 } else if (target_type == TypeManager.ushort_type)
2262 else if (target_type == TypeManager.int64_type)
2264 else if (target_type == TypeManager.uint64_type)
2269 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2274 // Load the object from the pointer. The `IsReference' is used
2275 // to control whether we should use Ldind_Ref or LdObj if the
2276 // value is not a `core' type.
2278 // Maybe we should try to extract this infromation form the type?
2279 // TODO: Maybe this is a bug. The reason we have this flag is because
2280 // I had almost identical code in ParameterReference (for handling
2281 // references) and in UnboxCast.
2283 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2285 if (t == TypeManager.int32_type)
2286 ig.Emit (OpCodes.Ldind_I4);
2287 else if (t == TypeManager.uint32_type)
2288 ig.Emit (OpCodes.Ldind_U4);
2289 else if (t == TypeManager.short_type)
2290 ig.Emit (OpCodes.Ldind_I2);
2291 else if (t == TypeManager.ushort_type)
2292 ig.Emit (OpCodes.Ldind_U2);
2293 else if (t == TypeManager.char_type)
2294 ig.Emit (OpCodes.Ldind_U2);
2295 else if (t == TypeManager.byte_type)
2296 ig.Emit (OpCodes.Ldind_U1);
2297 else if (t == TypeManager.sbyte_type)
2298 ig.Emit (OpCodes.Ldind_I1);
2299 else if (t == TypeManager.uint64_type)
2300 ig.Emit (OpCodes.Ldind_I8);
2301 else if (t == TypeManager.int64_type)
2302 ig.Emit (OpCodes.Ldind_I8);
2303 else if (t == TypeManager.float_type)
2304 ig.Emit (OpCodes.Ldind_R4);
2305 else if (t == TypeManager.double_type)
2306 ig.Emit (OpCodes.Ldind_R8);
2307 else if (t == TypeManager.bool_type)
2308 ig.Emit (OpCodes.Ldind_I1);
2309 else if (t == TypeManager.intptr_type)
2310 ig.Emit (OpCodes.Ldind_I);
2311 else if (TypeManager.IsEnumType (t)){
2312 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2315 ig.Emit (OpCodes.Ldind_Ref);
2317 ig.Emit (OpCodes.Ldobj, t);
2322 // The stack contains the pointer and the value of type `type'
2324 public static void StoreFromPtr (ILGenerator ig, Type type)
2326 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2327 ig.Emit (OpCodes.Stind_I4);
2328 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2329 ig.Emit (OpCodes.Stind_I8);
2330 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2331 type == TypeManager.ushort_type)
2332 ig.Emit (OpCodes.Stind_I2);
2333 else if (type == TypeManager.float_type)
2334 ig.Emit (OpCodes.Stind_R4);
2335 else if (type == TypeManager.double_type)
2336 ig.Emit (OpCodes.Stind_R8);
2337 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2338 type == TypeManager.bool_type)
2339 ig.Emit (OpCodes.Stind_I1);
2340 else if (type == TypeManager.intptr_type)
2341 ig.Emit (OpCodes.Stind_I);
2343 ig.Emit (OpCodes.Stind_Ref);
2347 // Returns the size of type `t' if known, otherwise, 0
2349 public static int GetTypeSize (Type t)
2351 if (t == TypeManager.int32_type ||
2352 t == TypeManager.uint32_type ||
2353 t == TypeManager.float_type)
2355 else if (t == TypeManager.int64_type ||
2356 t == TypeManager.uint64_type ||
2357 t == TypeManager.double_type)
2359 else if (t == TypeManager.byte_type ||
2360 t == TypeManager.sbyte_type ||
2361 t == TypeManager.bool_type)
2363 else if (t == TypeManager.short_type ||
2364 t == TypeManager.char_type ||
2365 t == TypeManager.ushort_type)
2373 /// This is just a base class for expressions that can
2374 /// appear on statements (invocations, object creation,
2375 /// assignments, post/pre increment and decrement). The idea
2376 /// being that they would support an extra Emition interface that
2377 /// does not leave a result on the stack.
2379 public abstract class ExpressionStatement : Expression {
2382 /// Requests the expression to be emitted in a `statement'
2383 /// context. This means that no new value is left on the
2384 /// stack after invoking this method (constrasted with
2385 /// Emit that will always leave a value on the stack).
2387 public abstract void EmitStatement (EmitContext ec);
2391 /// This kind of cast is used to encapsulate the child
2392 /// whose type is child.Type into an expression that is
2393 /// reported to return "return_type". This is used to encapsulate
2394 /// expressions which have compatible types, but need to be dealt
2395 /// at higher levels with.
2397 /// For example, a "byte" expression could be encapsulated in one
2398 /// of these as an "unsigned int". The type for the expression
2399 /// would be "unsigned int".
2402 public class EmptyCast : Expression {
2403 protected Expression child;
2405 public EmptyCast (Expression child, Type return_type)
2407 eclass = child.eclass;
2412 public override Expression DoResolve (EmitContext ec)
2414 // This should never be invoked, we are born in fully
2415 // initialized state.
2420 public override void Emit (EmitContext ec)
2427 /// This class is used to wrap literals which belong inside Enums
2429 public class EnumConstant : Constant {
2430 public Constant Child;
2432 public EnumConstant (Constant child, Type enum_type)
2434 eclass = child.eclass;
2439 public override Expression DoResolve (EmitContext ec)
2441 // This should never be invoked, we are born in fully
2442 // initialized state.
2447 public override void Emit (EmitContext ec)
2452 public override object GetValue ()
2454 return Child.GetValue ();
2458 // Converts from one of the valid underlying types for an enumeration
2459 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2460 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2462 public Constant WidenToCompilerConstant ()
2464 Type t = TypeManager.EnumToUnderlying (Child.Type);
2465 object v = ((Constant) Child).GetValue ();;
2467 if (t == TypeManager.int32_type)
2468 return new IntConstant ((int) v);
2469 if (t == TypeManager.uint32_type)
2470 return new UIntConstant ((uint) v);
2471 if (t == TypeManager.int64_type)
2472 return new LongConstant ((long) v);
2473 if (t == TypeManager.uint64_type)
2474 return new ULongConstant ((ulong) v);
2475 if (t == TypeManager.short_type)
2476 return new ShortConstant ((short) v);
2477 if (t == TypeManager.ushort_type)
2478 return new UShortConstant ((ushort) v);
2479 if (t == TypeManager.byte_type)
2480 return new ByteConstant ((byte) v);
2481 if (t == TypeManager.sbyte_type)
2482 return new SByteConstant ((sbyte) v);
2484 throw new Exception ("Invalid enumeration underlying type: " + t);
2488 // Extracts the value in the enumeration on its native representation
2490 public object GetPlainValue ()
2492 Type t = TypeManager.EnumToUnderlying (Child.Type);
2493 object v = ((Constant) Child).GetValue ();;
2495 if (t == TypeManager.int32_type)
2497 if (t == TypeManager.uint32_type)
2499 if (t == TypeManager.int64_type)
2501 if (t == TypeManager.uint64_type)
2503 if (t == TypeManager.short_type)
2505 if (t == TypeManager.ushort_type)
2507 if (t == TypeManager.byte_type)
2509 if (t == TypeManager.sbyte_type)
2515 public override string AsString ()
2517 return Child.AsString ();
2520 public override DoubleConstant ConvertToDouble ()
2522 return Child.ConvertToDouble ();
2525 public override FloatConstant ConvertToFloat ()
2527 return Child.ConvertToFloat ();
2530 public override ULongConstant ConvertToULong ()
2532 return Child.ConvertToULong ();
2535 public override LongConstant ConvertToLong ()
2537 return Child.ConvertToLong ();
2540 public override UIntConstant ConvertToUInt ()
2542 return Child.ConvertToUInt ();
2545 public override IntConstant ConvertToInt ()
2547 return Child.ConvertToInt ();
2552 /// This kind of cast is used to encapsulate Value Types in objects.
2554 /// The effect of it is to box the value type emitted by the previous
2557 public class BoxedCast : EmptyCast {
2559 public BoxedCast (Expression expr)
2560 : base (expr, TypeManager.object_type)
2564 public override Expression DoResolve (EmitContext ec)
2566 // This should never be invoked, we are born in fully
2567 // initialized state.
2572 public override void Emit (EmitContext ec)
2576 ec.ig.Emit (OpCodes.Box, child.Type);
2580 public class UnboxCast : EmptyCast {
2581 public UnboxCast (Expression expr, Type return_type)
2582 : base (expr, return_type)
2586 public override Expression DoResolve (EmitContext ec)
2588 // This should never be invoked, we are born in fully
2589 // initialized state.
2594 public override void Emit (EmitContext ec)
2597 ILGenerator ig = ec.ig;
2600 ig.Emit (OpCodes.Unbox, t);
2602 LoadFromPtr (ig, t, false);
2607 /// This is used to perform explicit numeric conversions.
2609 /// Explicit numeric conversions might trigger exceptions in a checked
2610 /// context, so they should generate the conv.ovf opcodes instead of
2613 public class ConvCast : EmptyCast {
2614 public enum Mode : byte {
2615 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2617 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2618 U2_I1, U2_U1, U2_I2, U2_CH,
2619 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2620 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2621 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2622 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2623 CH_I1, CH_U1, CH_I2,
2624 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2625 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2631 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2632 : base (child, return_type)
2635 checked_state = ec.CheckState;
2638 public override Expression DoResolve (EmitContext ec)
2640 // This should never be invoked, we are born in fully
2641 // initialized state.
2646 public override void Emit (EmitContext ec)
2648 ILGenerator ig = ec.ig;
2654 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2655 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2656 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2657 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2658 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2660 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2661 case Mode.U1_CH: /* nothing */ break;
2663 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2664 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2665 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2666 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2667 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2668 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2670 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2671 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2672 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2673 case Mode.U2_CH: /* nothing */ break;
2675 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2676 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2677 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2678 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2679 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2680 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2681 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2683 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2684 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2685 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2686 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2687 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2688 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2690 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2691 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2692 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2693 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2694 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2695 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2696 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2697 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2699 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2700 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2701 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2702 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2703 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2704 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2705 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2706 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2708 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2709 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2710 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2712 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2713 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2714 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2715 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2716 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2717 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2718 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2719 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2720 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2722 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2723 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2724 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2725 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2726 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2727 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2728 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2729 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2730 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2731 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2735 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2736 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2737 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2738 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2739 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2741 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2742 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2744 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2745 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2746 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2747 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2748 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2749 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2751 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2752 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2753 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2754 case Mode.U2_CH: /* nothing */ break;
2756 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2757 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2758 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2759 case Mode.I4_U4: /* nothing */ break;
2760 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2761 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2762 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2764 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2765 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2766 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2767 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2768 case Mode.U4_I4: /* nothing */ break;
2769 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2771 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2772 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2773 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2774 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2775 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2776 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2777 case Mode.I8_U8: /* nothing */ break;
2778 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2780 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2781 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2782 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2783 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2784 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2785 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2786 case Mode.U8_I8: /* nothing */ break;
2787 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2789 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2790 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2791 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2793 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2794 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2795 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2796 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2797 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2798 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2799 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2800 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2801 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2803 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2804 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2805 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2806 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2807 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2808 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2809 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2810 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2811 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2812 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2818 public class OpcodeCast : EmptyCast {
2822 public OpcodeCast (Expression child, Type return_type, OpCode op)
2823 : base (child, return_type)
2827 second_valid = false;
2830 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2831 : base (child, return_type)
2836 second_valid = true;
2839 public override Expression DoResolve (EmitContext ec)
2841 // This should never be invoked, we are born in fully
2842 // initialized state.
2847 public override void Emit (EmitContext ec)
2858 /// This kind of cast is used to encapsulate a child and cast it
2859 /// to the class requested
2861 public class ClassCast : EmptyCast {
2862 public ClassCast (Expression child, Type return_type)
2863 : base (child, return_type)
2868 public override Expression DoResolve (EmitContext ec)
2870 // This should never be invoked, we are born in fully
2871 // initialized state.
2876 public override void Emit (EmitContext ec)
2880 ec.ig.Emit (OpCodes.Castclass, type);
2886 /// SimpleName expressions are initially formed of a single
2887 /// word and it only happens at the beginning of the expression.
2891 /// The expression will try to be bound to a Field, a Method
2892 /// group or a Property. If those fail we pass the name to our
2893 /// caller and the SimpleName is compounded to perform a type
2894 /// lookup. The idea behind this process is that we want to avoid
2895 /// creating a namespace map from the assemblies, as that requires
2896 /// the GetExportedTypes function to be called and a hashtable to
2897 /// be constructed which reduces startup time. If later we find
2898 /// that this is slower, we should create a `NamespaceExpr' expression
2899 /// that fully participates in the resolution process.
2901 /// For example `System.Console.WriteLine' is decomposed into
2902 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2904 /// The first SimpleName wont produce a match on its own, so it will
2906 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2908 /// System.Console will produce a TypeExpr match.
2910 /// The downside of this is that we might be hitting `LookupType' too many
2911 /// times with this scheme.
2913 public class SimpleName : Expression {
2914 public readonly string Name;
2915 public readonly Location Location;
2917 public SimpleName (string name, Location l)
2923 public static void Error120 (Location l, string name)
2927 "An object reference is required " +
2928 "for the non-static field `"+name+"'");
2932 // Checks whether we are trying to access an instance
2933 // property, method or field from a static body.
2935 Expression MemberStaticCheck (Expression e)
2937 if (e is FieldExpr){
2938 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2941 Error120 (Location, Name);
2944 } else if (e is MethodGroupExpr){
2945 MethodGroupExpr mg = (MethodGroupExpr) e;
2947 if (!mg.RemoveInstanceMethods ()){
2948 Error120 (Location, mg.Methods [0].Name);
2952 } else if (e is PropertyExpr){
2953 if (!((PropertyExpr) e).IsStatic){
2954 Error120 (Location, Name);
2957 } else if (e is EventExpr) {
2958 if (!((EventExpr) e).IsStatic) {
2959 Error120 (Location, Name);
2967 public override Expression DoResolve (EmitContext ec)
2969 return SimpleNameResolve (ec, false);
2972 public Expression DoResolveAllowStatic (EmitContext ec)
2974 return SimpleNameResolve (ec, true);
2978 /// 7.5.2: Simple Names.
2980 /// Local Variables and Parameters are handled at
2981 /// parse time, so they never occur as SimpleNames.
2983 /// The `allow_static' flag is used by MemberAccess only
2984 /// and it is used to inform us that it is ok for us to
2985 /// avoid the static check, because MemberAccess might end
2986 /// up resolving the Name as a Type name and the access as
2987 /// a static type access.
2989 /// ie: Type Type; .... { Type.GetType (""); }
2991 /// Type is both an instance variable and a Type; Type.GetType
2992 /// is the static method not an instance method of type.
2994 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2996 Expression e = null;
2999 // Stage 1: Performed by the parser (binding to locals or parameters).
3001 if (!ec.OnlyLookupTypes){
3002 Block current_block = ec.CurrentBlock;
3003 if (current_block != null && current_block.IsVariableDefined (Name)){
3004 LocalVariableReference var;
3006 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3008 return var.Resolve (ec);
3012 // Stage 2: Lookup members
3016 // For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
3017 // Hence we have two different cases
3019 e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
3021 if (e == null && ec.TypeContainer.TypeBuilder != null)
3022 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
3025 // Continuation of stage 2
3028 // Stage 3: Lookup symbol in the various namespaces.
3030 DeclSpace ds = ec.DeclSpace;
3034 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3035 return new TypeExpr (t);
3038 // Stage 2 part b: Lookup up if we are an alias to a type
3041 // Since we are cheating: we only do the Alias lookup for
3042 // namespaces if the name does not include any dots in it
3045 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3046 // System.Console.WriteLine (Name + " --> " + alias_value);
3047 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3049 return new TypeExpr (t);
3051 // we have alias value, but it isn't Type, so try if it's namespace
3052 return new SimpleName (alias_value, Location);
3055 // No match, maybe our parent can compose us
3056 // into something meaningful.
3061 // Stage 2 continues here.
3066 if (ec.OnlyLookupTypes)
3069 if (e is FieldExpr){
3070 FieldExpr fe = (FieldExpr) e;
3071 FieldInfo fi = fe.FieldInfo;
3073 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3074 UnsafeError (Location);
3078 if (!allow_static && !fi.IsStatic){
3079 Error120 (Location, Name);
3083 // If we are not in static code and this
3084 // field is not static, set the instance to `this'.
3087 fe.InstanceExpression = ec.This;
3091 if (fi is FieldBuilder) {
3092 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3095 object o = c.LookupConstantValue (ec);
3096 object real_value = ((Constant)c.Expr).GetValue ();
3097 return Constantify (real_value, fi.FieldType);
3104 if (e is EventExpr) {
3106 // If the event is local to this class, we transform ourselves into
3109 EventExpr ee = (EventExpr) e;
3111 Expression ml = MemberLookup (
3112 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3113 MemberTypes.Event, AllBindingFlags, Location);
3116 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3120 // If this happens, then we have an event with its own
3121 // accessors and private field etc so there's no need
3122 // to transform ourselves : we should instead flag an error
3124 Assign.error70 (ee.EventInfo, Location);
3128 ml = ExprClassFromMemberInfo (ec, mi, Location);
3131 Report.Error (-200, Location, "Internal error!!");
3135 Expression instance_expr;
3137 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3140 instance_expr = null;
3142 instance_expr = ec.This;
3144 instance_expr = instance_expr.Resolve (ec);
3146 if (instance_expr != null)
3147 instance_expr = instance_expr.Resolve (ec);
3149 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3158 return MemberStaticCheck (e);
3163 public override void Emit (EmitContext ec)
3166 // If this is ever reached, then we failed to
3167 // find the name as a namespace
3170 Error (103, Location, "The name `" + Name +
3171 "' does not exist in the class `" +
3172 ec.DeclSpace.Name + "'");
3177 /// Fully resolved expression that evaluates to a type
3179 public class TypeExpr : Expression {
3180 public TypeExpr (Type t)
3183 eclass = ExprClass.Type;
3186 override public Expression DoResolve (EmitContext ec)
3191 override public void Emit (EmitContext ec)
3193 throw new Exception ("Implement me");
3198 /// MethodGroup Expression.
3200 /// This is a fully resolved expression that evaluates to a type
3202 public class MethodGroupExpr : Expression {
3203 public MethodBase [] Methods;
3204 Expression instance_expression = null;
3206 public MethodGroupExpr (MemberInfo [] mi)
3208 Methods = new MethodBase [mi.Length];
3209 mi.CopyTo (Methods, 0);
3210 eclass = ExprClass.MethodGroup;
3213 public MethodGroupExpr (ArrayList l)
3215 Methods = new MethodBase [l.Count];
3218 l.CopyTo (Methods, 0);
3220 foreach (MemberInfo m in l){
3221 if (!(m is MethodBase)){
3222 Console.WriteLine ("Name " + m.Name);
3223 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3228 eclass = ExprClass.MethodGroup;
3232 // `A method group may have associated an instance expression'
3234 public Expression InstanceExpression {
3236 return instance_expression;
3240 instance_expression = value;
3244 override public Expression DoResolve (EmitContext ec)
3249 override public void Emit (EmitContext ec)
3251 throw new Exception ("This should never be reached");
3254 bool RemoveMethods (bool keep_static)
3256 ArrayList smethods = new ArrayList ();
3257 int top = Methods.Length;
3260 for (i = 0; i < top; i++){
3261 MethodBase mb = Methods [i];
3263 if (mb.IsStatic == keep_static)
3267 if (smethods.Count == 0)
3270 Methods = new MethodBase [smethods.Count];
3271 smethods.CopyTo (Methods, 0);
3277 /// Removes any instance methods from the MethodGroup, returns
3278 /// false if the resulting set is empty.
3280 public bool RemoveInstanceMethods ()
3282 return RemoveMethods (true);
3286 /// Removes any static methods from the MethodGroup, returns
3287 /// false if the resulting set is empty.
3289 public bool RemoveStaticMethods ()
3291 return RemoveMethods (false);
3296 /// Fully resolved expression that evaluates to a Field
3298 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3299 public readonly FieldInfo FieldInfo;
3300 public Expression InstanceExpression;
3303 public FieldExpr (FieldInfo fi, Location l)
3306 eclass = ExprClass.Variable;
3307 type = fi.FieldType;
3311 override public Expression DoResolve (EmitContext ec)
3313 if (!FieldInfo.IsStatic){
3314 if (InstanceExpression == null){
3315 throw new Exception ("non-static FieldExpr without instance var\n" +
3316 "You have to assign the Instance variable\n" +
3317 "Of the FieldExpr to set this\n");
3320 InstanceExpression = InstanceExpression.Resolve (ec);
3321 if (InstanceExpression == null)
3328 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3330 Expression e = DoResolve (ec);
3335 if (!FieldInfo.IsInitOnly)
3339 // InitOnly fields can only be assigned in constructors
3342 if (ec.IsConstructor)
3345 Report.Error (191, loc,
3346 "Readonly field can not be assigned outside " +
3347 "of constructor or variable initializer");
3352 override public void Emit (EmitContext ec)
3354 ILGenerator ig = ec.ig;
3355 bool is_volatile = false;
3357 if (FieldInfo is FieldBuilder){
3358 Field f = TypeManager.GetField (FieldInfo);
3360 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3363 f.status |= Field.Status.USED;
3367 if (FieldInfo.IsStatic){
3369 ig.Emit (OpCodes.Volatile);
3371 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3373 if (InstanceExpression.Type.IsValueType){
3375 LocalTemporary tempo = null;
3377 if (!(InstanceExpression is IMemoryLocation)){
3378 tempo = new LocalTemporary (
3379 ec, InstanceExpression.Type);
3381 InstanceExpression.Emit (ec);
3385 ml = (IMemoryLocation) InstanceExpression;
3387 ml.AddressOf (ec, AddressOp.Load);
3389 InstanceExpression.Emit (ec);
3392 ig.Emit (OpCodes.Volatile);
3394 ig.Emit (OpCodes.Ldfld, FieldInfo);
3398 public void EmitAssign (EmitContext ec, Expression source)
3400 bool is_static = FieldInfo.IsStatic;
3401 ILGenerator ig = ec.ig;
3404 Expression instance = InstanceExpression;
3406 if (instance.Type.IsValueType){
3407 if (instance is IMemoryLocation){
3408 IMemoryLocation ml = (IMemoryLocation) instance;
3410 ml.AddressOf (ec, AddressOp.Store);
3412 throw new Exception ("The " + instance + " of type " +
3414 " represents a ValueType and does " +
3415 "not implement IMemoryLocation");
3421 if (FieldInfo is FieldBuilder){
3422 Field f = TypeManager.GetField (FieldInfo);
3423 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3424 ig.Emit (OpCodes.Volatile);
3428 ig.Emit (OpCodes.Stsfld, FieldInfo);
3430 ig.Emit (OpCodes.Stfld, FieldInfo);
3432 if (FieldInfo is FieldBuilder){
3433 Field f = TypeManager.GetField (FieldInfo);
3435 f.status |= Field.Status.ASSIGNED;
3439 public void AddressOf (EmitContext ec, AddressOp mode)
3441 ILGenerator ig = ec.ig;
3443 if (FieldInfo is FieldBuilder){
3444 Field f = TypeManager.GetField (FieldInfo);
3445 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3446 ig.Emit (OpCodes.Volatile);
3449 if (FieldInfo is FieldBuilder){
3450 Field f = TypeManager.GetField (FieldInfo);
3452 if ((mode & AddressOp.Store) != 0)
3453 f.status |= Field.Status.ASSIGNED;
3454 if ((mode & AddressOp.Load) != 0)
3455 f.status |= Field.Status.USED;
3459 // Handle initonly fields specially: make a copy and then
3460 // get the address of the copy.
3462 if (FieldInfo.IsInitOnly){
3466 local = ig.DeclareLocal (type);
3467 ig.Emit (OpCodes.Stloc, local);
3468 ig.Emit (OpCodes.Ldloca, local);
3472 if (FieldInfo.IsStatic)
3473 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3475 InstanceExpression.Emit (ec);
3476 ig.Emit (OpCodes.Ldflda, FieldInfo);
3482 /// Expression that evaluates to a Property. The Assign class
3483 /// might set the `Value' expression if we are in an assignment.
3485 /// This is not an LValue because we need to re-write the expression, we
3486 /// can not take data from the stack and store it.
3488 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3489 public readonly PropertyInfo PropertyInfo;
3490 public readonly bool IsStatic;
3492 MethodInfo [] Accessors;
3495 Expression instance_expr;
3497 public PropertyExpr (PropertyInfo pi, Location l)
3500 eclass = ExprClass.PropertyAccess;
3503 Accessors = TypeManager.GetAccessors (pi);
3505 if (Accessors != null)
3506 for (int i = 0; i < Accessors.Length; i++){
3507 if (Accessors [i] != null)
3508 if (Accessors [i].IsStatic)
3512 Accessors = new MethodInfo [2];
3514 type = pi.PropertyType;
3518 // The instance expression associated with this expression
3520 public Expression InstanceExpression {
3522 instance_expr = value;
3526 return instance_expr;
3530 public bool VerifyAssignable ()
3532 if (!PropertyInfo.CanWrite){
3533 Report.Error (200, loc,
3534 "The property `" + PropertyInfo.Name +
3535 "' can not be assigned to, as it has not set accessor");
3542 override public Expression DoResolve (EmitContext ec)
3544 if (!PropertyInfo.CanRead){
3545 Report.Error (154, loc,
3546 "The property `" + PropertyInfo.Name +
3547 "' can not be used in " +
3548 "this context because it lacks a get accessor");
3552 type = PropertyInfo.PropertyType;
3557 override public void Emit (EmitContext ec)
3559 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
3564 // Implements the IAssignMethod interface for assignments
3566 public void EmitAssign (EmitContext ec, Expression source)
3568 Argument arg = new Argument (source, Argument.AType.Expression);
3569 ArrayList args = new ArrayList ();
3572 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3575 override public void EmitStatement (EmitContext ec)
3578 ec.ig.Emit (OpCodes.Pop);
3583 /// Fully resolved expression that evaluates to an Event
3585 public class EventExpr : Expression {
3586 public readonly EventInfo EventInfo;
3588 public Expression InstanceExpression;
3590 public readonly bool IsStatic;
3592 MethodInfo add_accessor, remove_accessor;
3594 public EventExpr (EventInfo ei, Location loc)
3598 eclass = ExprClass.EventAccess;
3600 add_accessor = TypeManager.GetAddMethod (ei);
3601 remove_accessor = TypeManager.GetRemoveMethod (ei);
3603 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3606 if (EventInfo is MyEventBuilder)
3607 type = ((MyEventBuilder) EventInfo).EventType;
3609 type = EventInfo.EventHandlerType;
3612 override public Expression DoResolve (EmitContext ec)
3614 // We are born fully resolved
3618 override public void Emit (EmitContext ec)
3620 throw new Exception ("Should not happen I think");
3623 public void EmitAddOrRemove (EmitContext ec, Expression source)
3625 Expression handler = ((Binary) source).Right;
3627 Argument arg = new Argument (handler, Argument.AType.Expression);
3628 ArrayList args = new ArrayList ();
3632 if (((Binary) source).Oper == Binary.Operator.Addition)
3633 Invocation.EmitCall (
3634 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3636 Invocation.EmitCall (
3637 ec, false, IsStatic, InstanceExpression, remove_accessor, args);