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;
483 if (count == 1 && !(mi [0] is MethodBase))
484 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
487 // We found methods, turn the search into "method scan"
490 method_list = CopyNewMethods (method_list, mi);
491 mt &= (MemberTypes.Method | MemberTypes.Constructor);
494 if (method_list != null && method_list.Count > 0)
495 return new MethodGroupExpr (method_list);
500 public const MemberTypes AllMemberTypes =
501 MemberTypes.Constructor |
505 MemberTypes.NestedType |
506 MemberTypes.Property;
508 public const BindingFlags AllBindingFlags =
509 BindingFlags.Public |
510 BindingFlags.Static |
511 BindingFlags.Instance;
513 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
515 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
519 /// This is a wrapper for MemberLookup that is not used to "probe", but
520 /// to find a final definition. If the final definition is not found, we
521 /// look for private members and display a useful debugging message if we
524 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
529 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
534 e = MemberLookup (ec, t, name, AllMemberTypes,
535 AllBindingFlags | BindingFlags.NonPublic, loc);
538 117, loc, "`" + t + "' does not contain a definition " +
539 "for `" + name + "'");
542 122, loc, "`" + t + "." + name +
543 "' is inaccessible due to its protection level");
549 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
551 Type expr_type = expr.Type;
553 if (target_type == TypeManager.object_type) {
555 // A pointer type cannot be converted to object
557 if (expr_type.IsPointer)
560 if (expr_type.IsValueType)
561 return new BoxedCast (expr);
562 if (expr_type.IsClass || expr_type.IsInterface)
563 return new EmptyCast (expr, target_type);
564 } else if (expr_type.IsSubclassOf (target_type)) {
565 return new EmptyCast (expr, target_type);
568 // This code is kind of mirrored inside StandardConversionExists
569 // with the small distinction that we only probe there
571 // Always ensure that the code here and there is in sync
573 // from the null type to any reference-type.
574 if (expr is NullLiteral && !target_type.IsValueType)
575 return new EmptyCast (expr, target_type);
577 // from any class-type S to any interface-type T.
578 if (expr_type.IsClass && target_type.IsInterface) {
579 if (TypeManager.ImplementsInterface (expr_type, target_type))
580 return new EmptyCast (expr, target_type);
585 // from any interface type S to interface-type T.
586 if (expr_type.IsInterface && target_type.IsInterface) {
588 if (TypeManager.ImplementsInterface (expr_type, target_type))
589 return new EmptyCast (expr, target_type);
594 // from an array-type S to an array-type of type T
595 if (expr_type.IsArray && target_type.IsArray) {
596 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
598 Type expr_element_type = expr_type.GetElementType ();
599 Type target_element_type = target_type.GetElementType ();
601 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
602 if (StandardConversionExists (expr_element_type,
603 target_element_type))
604 return new EmptyCast (expr, target_type);
609 // from an array-type to System.Array
610 if (expr_type.IsArray && target_type == TypeManager.array_type)
611 return new EmptyCast (expr, target_type);
613 // from any delegate type to System.Delegate
614 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
615 target_type == TypeManager.delegate_type)
616 return new EmptyCast (expr, target_type);
618 // from any array-type or delegate type into System.ICloneable.
619 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
620 if (target_type == TypeManager.icloneable_type)
621 return new EmptyCast (expr, target_type);
631 /// Handles expressions like this: decimal d; d = 1;
632 /// and changes them into: decimal d; d = new System.Decimal (1);
634 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
636 ArrayList args = new ArrayList ();
638 args.Add (new Argument (expr, Argument.AType.Expression));
640 Expression ne = new New (target.FullName, args,
643 return ne.Resolve (ec);
647 /// Implicit Numeric Conversions.
649 /// expr is the expression to convert, returns a new expression of type
650 /// target_type or null if an implicit conversion is not possible.
652 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
653 Type target_type, Location loc)
655 Type expr_type = expr.Type;
658 // Attempt to do the implicit constant expression conversions
660 if (expr is IntConstant){
663 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
667 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
669 // Try the implicit constant expression conversion
670 // from long to ulong, instead of a nice routine,
673 long v = ((LongConstant) expr).Value;
675 return new ULongConstant ((ulong) v);
679 // If we have an enumeration, extract the underlying type,
680 // use this during the comparission, but wrap around the original
683 Type real_target_type = target_type;
685 if (TypeManager.IsEnumType (real_target_type))
686 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
688 if (expr_type == real_target_type)
689 return new EmptyCast (expr, target_type);
691 if (expr_type == TypeManager.sbyte_type){
693 // From sbyte to short, int, long, float, double.
695 if (real_target_type == TypeManager.int32_type)
696 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
697 if (real_target_type == TypeManager.int64_type)
698 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
699 if (real_target_type == TypeManager.double_type)
700 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
701 if (real_target_type == TypeManager.float_type)
702 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
703 if (real_target_type == TypeManager.short_type)
704 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
705 if (real_target_type == TypeManager.decimal_type)
706 return InternalTypeConstructor (ec, expr, target_type);
707 } else if (expr_type == TypeManager.byte_type){
709 // From byte to short, ushort, int, uint, long, ulong, float, double
711 if ((real_target_type == TypeManager.short_type) ||
712 (real_target_type == TypeManager.ushort_type) ||
713 (real_target_type == TypeManager.int32_type) ||
714 (real_target_type == TypeManager.uint32_type))
715 return new EmptyCast (expr, target_type);
717 if (real_target_type == TypeManager.uint64_type)
718 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
719 if (real_target_type == TypeManager.int64_type)
720 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
721 if (real_target_type == TypeManager.float_type)
722 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
723 if (real_target_type == TypeManager.double_type)
724 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
725 if (real_target_type == TypeManager.decimal_type)
726 return InternalTypeConstructor (ec, expr, target_type);
727 } else if (expr_type == TypeManager.short_type){
729 // From short to int, long, float, double
731 if (real_target_type == TypeManager.int32_type)
732 return new EmptyCast (expr, target_type);
733 if (real_target_type == TypeManager.int64_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
735 if (real_target_type == TypeManager.double_type)
736 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
737 if (real_target_type == TypeManager.float_type)
738 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
739 if (real_target_type == TypeManager.decimal_type)
740 return InternalTypeConstructor (ec, expr, target_type);
741 } else if (expr_type == TypeManager.ushort_type){
743 // From ushort to int, uint, long, ulong, float, double
745 if (real_target_type == TypeManager.uint32_type)
746 return new EmptyCast (expr, target_type);
748 if (real_target_type == TypeManager.uint64_type)
749 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
750 if (real_target_type == TypeManager.int32_type)
751 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
752 if (real_target_type == TypeManager.int64_type)
753 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
754 if (real_target_type == TypeManager.double_type)
755 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
756 if (real_target_type == TypeManager.float_type)
757 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
758 if (real_target_type == TypeManager.decimal_type)
759 return InternalTypeConstructor (ec, expr, target_type);
760 } else if (expr_type == TypeManager.int32_type){
762 // From int to long, float, double
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.uint32_type){
774 // From uint to long, ulong, float, double
776 if (real_target_type == TypeManager.int64_type)
777 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
778 if (real_target_type == TypeManager.uint64_type)
779 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
780 if (real_target_type == TypeManager.double_type)
781 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
783 if (real_target_type == TypeManager.float_type)
784 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
786 if (real_target_type == TypeManager.decimal_type)
787 return InternalTypeConstructor (ec, expr, target_type);
788 } else if ((expr_type == TypeManager.uint64_type) ||
789 (expr_type == TypeManager.int64_type)){
791 // From long/ulong to float, double
793 if (real_target_type == TypeManager.double_type)
794 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
796 if (real_target_type == TypeManager.float_type)
797 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
799 if (real_target_type == TypeManager.decimal_type)
800 return InternalTypeConstructor (ec, expr, target_type);
801 } else if (expr_type == TypeManager.char_type){
803 // From char to ushort, int, uint, long, ulong, float, double
805 if ((real_target_type == TypeManager.ushort_type) ||
806 (real_target_type == TypeManager.int32_type) ||
807 (real_target_type == TypeManager.uint32_type))
808 return new EmptyCast (expr, target_type);
809 if (real_target_type == TypeManager.uint64_type)
810 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
811 if (real_target_type == TypeManager.int64_type)
812 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
813 if (real_target_type == TypeManager.float_type)
814 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
815 if (real_target_type == TypeManager.double_type)
816 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
817 if (real_target_type == TypeManager.decimal_type)
818 return InternalTypeConstructor (ec, expr, target_type);
819 } else if (expr_type == TypeManager.float_type){
823 if (real_target_type == TypeManager.double_type)
824 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
831 /// Determines if a standard implicit conversion exists from
832 /// expr_type to target_type
834 public static bool StandardConversionExists (Type expr_type, Type target_type)
836 if (expr_type == target_type)
839 // First numeric conversions
841 if (expr_type == TypeManager.sbyte_type){
843 // From sbyte to short, int, long, float, double.
845 if ((target_type == TypeManager.int32_type) ||
846 (target_type == TypeManager.int64_type) ||
847 (target_type == TypeManager.double_type) ||
848 (target_type == TypeManager.float_type) ||
849 (target_type == TypeManager.short_type) ||
850 (target_type == TypeManager.decimal_type))
853 } else if (expr_type == TypeManager.byte_type){
855 // From byte to short, ushort, int, uint, long, ulong, float, double
857 if ((target_type == TypeManager.short_type) ||
858 (target_type == TypeManager.ushort_type) ||
859 (target_type == TypeManager.int32_type) ||
860 (target_type == TypeManager.uint32_type) ||
861 (target_type == TypeManager.uint64_type) ||
862 (target_type == TypeManager.int64_type) ||
863 (target_type == TypeManager.float_type) ||
864 (target_type == TypeManager.double_type) ||
865 (target_type == TypeManager.decimal_type))
868 } else if (expr_type == TypeManager.short_type){
870 // From short to int, long, float, double
872 if ((target_type == TypeManager.int32_type) ||
873 (target_type == TypeManager.int64_type) ||
874 (target_type == TypeManager.double_type) ||
875 (target_type == TypeManager.float_type) ||
876 (target_type == TypeManager.decimal_type))
879 } else if (expr_type == TypeManager.ushort_type){
881 // From ushort to int, uint, long, ulong, float, double
883 if ((target_type == TypeManager.uint32_type) ||
884 (target_type == TypeManager.uint64_type) ||
885 (target_type == TypeManager.int32_type) ||
886 (target_type == TypeManager.int64_type) ||
887 (target_type == TypeManager.double_type) ||
888 (target_type == TypeManager.float_type) ||
889 (target_type == TypeManager.decimal_type))
892 } else if (expr_type == TypeManager.int32_type){
894 // From int to long, float, double
896 if ((target_type == TypeManager.int64_type) ||
897 (target_type == TypeManager.double_type) ||
898 (target_type == TypeManager.float_type) ||
899 (target_type == TypeManager.decimal_type))
902 } else if (expr_type == TypeManager.uint32_type){
904 // From uint to long, ulong, float, double
906 if ((target_type == TypeManager.int64_type) ||
907 (target_type == TypeManager.uint64_type) ||
908 (target_type == TypeManager.double_type) ||
909 (target_type == TypeManager.float_type) ||
910 (target_type == TypeManager.decimal_type))
913 } else if ((expr_type == TypeManager.uint64_type) ||
914 (expr_type == TypeManager.int64_type)) {
916 // From long/ulong to float, double
918 if ((target_type == TypeManager.double_type) ||
919 (target_type == TypeManager.float_type) ||
920 (target_type == TypeManager.decimal_type))
923 } else if (expr_type == TypeManager.char_type){
925 // From char to ushort, int, uint, long, ulong, float, double
927 if ((target_type == TypeManager.ushort_type) ||
928 (target_type == TypeManager.int32_type) ||
929 (target_type == TypeManager.uint32_type) ||
930 (target_type == TypeManager.uint64_type) ||
931 (target_type == TypeManager.int64_type) ||
932 (target_type == TypeManager.float_type) ||
933 (target_type == TypeManager.double_type) ||
934 (target_type == TypeManager.decimal_type))
937 } else if (expr_type == TypeManager.float_type){
941 if (target_type == TypeManager.double_type)
945 // Next reference conversions
947 if (target_type == TypeManager.object_type) {
948 if ((expr_type.IsClass) ||
949 (expr_type.IsValueType))
952 } else if (expr_type.IsSubclassOf (target_type)) {
956 // Please remember that all code below actuall comes
957 // from ImplicitReferenceConversion so make sure code remains in sync
959 // from any class-type S to any interface-type T.
960 if (expr_type.IsClass && target_type.IsInterface) {
961 if (TypeManager.ImplementsInterface (expr_type, target_type))
965 // from any interface type S to interface-type T.
966 // FIXME : Is it right to use IsAssignableFrom ?
967 if (expr_type.IsInterface && target_type.IsInterface)
968 if (target_type.IsAssignableFrom (expr_type))
971 // from an array-type S to an array-type of type T
972 if (expr_type.IsArray && target_type.IsArray) {
973 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
975 Type expr_element_type = expr_type.GetElementType ();
976 Type target_element_type = target_type.GetElementType ();
978 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
979 if (StandardConversionExists (expr_element_type,
980 target_element_type))
985 // from an array-type to System.Array
986 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
989 // from any delegate type to System.Delegate
990 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
991 target_type == TypeManager.delegate_type)
992 if (target_type.IsAssignableFrom (expr_type))
995 // from any array-type or delegate type into System.ICloneable.
996 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
997 if (target_type == TypeManager.icloneable_type)
1000 // from the null type to any reference-type.
1001 // FIXME : How do we do this ?
1008 static EmptyExpression MyEmptyExpr;
1010 /// Tells whether an implicit conversion exists from expr_type to
1013 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
1016 if (MyEmptyExpr == null)
1017 MyEmptyExpr = new EmptyExpression (expr_type);
1019 MyEmptyExpr.SetType (expr_type);
1021 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
1025 /// Finds "most encompassed type" according to the spec (13.4.2)
1026 /// amongst the methods in the MethodGroupExpr which convert from a
1027 /// type encompassing source_type
1029 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
1033 for (int i = me.Methods.Length; i > 0; ) {
1036 MethodBase mb = me.Methods [i];
1037 ParameterData pd = Invocation.GetParameterData (mb);
1038 Type param_type = pd.ParameterType (0);
1040 if (StandardConversionExists (source_type, param_type)) {
1044 if (StandardConversionExists (param_type, best))
1053 /// Finds "most encompassing type" according to the spec (13.4.2)
1054 /// amongst the methods in the MethodGroupExpr which convert to a
1055 /// type encompassed by target_type
1057 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
1061 for (int i = me.Methods.Length; i > 0; ) {
1064 MethodInfo mi = (MethodInfo) me.Methods [i];
1065 Type ret_type = mi.ReturnType;
1067 if (StandardConversionExists (ret_type, target)) {
1071 if (!StandardConversionExists (ret_type, best))
1083 /// User-defined Implicit conversions
1085 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1086 Type target, Location loc)
1088 return UserDefinedConversion (ec, source, target, loc, false);
1092 /// User-defined Explicit conversions
1094 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1095 Type target, Location loc)
1097 return UserDefinedConversion (ec, source, target, loc, true);
1101 /// User-defined conversions
1103 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1104 Type target, Location loc,
1105 bool look_for_explicit)
1107 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1108 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1110 MethodBase method = null;
1111 Type source_type = source.Type;
1115 // If we have a boolean type, we need to check for the True operator
1117 // FIXME : How does the False operator come into the picture ?
1118 // FIXME : This doesn't look complete and very correct !
1119 if (target == TypeManager.bool_type)
1120 op_name = "op_True";
1122 op_name = "op_Implicit";
1124 mg1 = MemberLookup (ec, source_type, op_name, loc);
1126 if (source_type.BaseType != null)
1127 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1129 mg3 = MemberLookup (ec, target, op_name, loc);
1131 if (target.BaseType != null)
1132 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1134 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1135 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1137 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1139 MethodGroupExpr union4 = null;
1141 if (look_for_explicit) {
1143 op_name = "op_Explicit";
1145 mg5 = MemberLookup (ec, source_type, op_name, loc);
1147 if (source_type.BaseType != null)
1148 mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1150 mg7 = MemberLookup (ec, target, op_name, loc);
1152 if (target.BaseType != null)
1153 mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1155 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1156 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1158 union4 = Invocation.MakeUnionSet (union5, union6);
1161 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1163 if (union != null) {
1165 Type most_specific_source, most_specific_target;
1167 most_specific_source = FindMostEncompassedType (union, source_type);
1168 if (most_specific_source == null)
1171 most_specific_target = FindMostEncompassingType (union, target);
1172 if (most_specific_target == null)
1177 for (int i = union.Methods.Length; i > 0;) {
1180 MethodBase mb = union.Methods [i];
1181 ParameterData pd = Invocation.GetParameterData (mb);
1182 MethodInfo mi = (MethodInfo) union.Methods [i];
1184 if (pd.ParameterType (0) == most_specific_source &&
1185 mi.ReturnType == most_specific_target) {
1191 if (method == null || count > 1) {
1192 Report.Error (-11, loc, "Ambiguous user defined conversion");
1197 // This will do the conversion to the best match that we
1198 // found. Now we need to perform an implict standard conversion
1199 // if the best match was not the type that we were requested
1202 if (look_for_explicit)
1203 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1205 source = ConvertImplicitStandard (ec, source,
1206 most_specific_source, loc);
1211 e = new UserCast ((MethodInfo) method, source);
1213 if (e.Type != target){
1214 if (!look_for_explicit)
1215 e = ConvertImplicitStandard (ec, e, target, loc);
1217 e = ConvertExplicitStandard (ec, e, target, loc);
1228 /// Converts implicitly the resolved expression `expr' into the
1229 /// `target_type'. It returns a new expression that can be used
1230 /// in a context that expects a `target_type'.
1232 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1233 Type target_type, Location loc)
1235 Type expr_type = expr.Type;
1238 if (expr_type == target_type)
1241 if (target_type == null)
1242 throw new Exception ("Target type is null");
1244 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1248 e = ImplicitUserConversion (ec, expr, target_type, loc);
1257 /// Attempts to apply the `Standard Implicit
1258 /// Conversion' rules to the expression `expr' into
1259 /// the `target_type'. It returns a new expression
1260 /// that can be used in a context that expects a
1263 /// This is different from `ConvertImplicit' in that the
1264 /// user defined implicit conversions are excluded.
1266 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1267 Type target_type, Location loc)
1269 Type expr_type = expr.Type;
1272 if (expr_type == target_type)
1275 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1279 e = ImplicitReferenceConversion (expr, target_type);
1283 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1284 IntLiteral i = (IntLiteral) expr;
1287 return new EmptyCast (expr, target_type);
1291 if (expr_type.IsPointer){
1292 if (target_type == TypeManager.void_ptr_type)
1293 return new EmptyCast (expr, target_type);
1296 // yep, comparing pointer types cant be done with
1297 // t1 == t2, we have to compare their element types.
1299 if (target_type.IsPointer){
1300 if (target_type.GetElementType()==expr_type.GetElementType())
1305 if (target_type.IsPointer){
1306 if (expr is NullLiteral)
1307 return new EmptyCast (expr, target_type);
1315 /// Attemps to perform an implict constant conversion of the IntConstant
1316 /// into a different data type using casts (See Implicit Constant
1317 /// Expression Conversions)
1319 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1321 int value = ic.Value;
1324 // FIXME: This could return constants instead of EmptyCasts
1326 if (target_type == TypeManager.sbyte_type){
1327 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1328 return new SByteConstant ((sbyte) value);
1329 } else if (target_type == TypeManager.byte_type){
1330 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1331 return new ByteConstant ((byte) value);
1332 } else if (target_type == TypeManager.short_type){
1333 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1334 return new ShortConstant ((short) value);
1335 } else if (target_type == TypeManager.ushort_type){
1336 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1337 return new UShortConstant ((ushort) value);
1338 } else if (target_type == TypeManager.uint32_type){
1340 return new UIntConstant ((uint) value);
1341 } else if (target_type == TypeManager.uint64_type){
1343 // we can optimize this case: a positive int32
1344 // always fits on a uint64. But we need an opcode
1348 return new ULongConstant ((ulong) value);
1351 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1352 return new EnumConstant (ic, target_type);
1357 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1359 string msg = "Cannot convert implicitly from `"+
1360 TypeManager.CSharpName (source) + "' to `" +
1361 TypeManager.CSharpName (target) + "'";
1363 Error (29, loc, msg);
1367 /// Attemptes to implicityly convert `target' into `type', using
1368 /// ConvertImplicit. If there is no implicit conversion, then
1369 /// an error is signaled
1371 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1372 Type target_type, Location loc)
1376 e = ConvertImplicit (ec, source, target_type, loc);
1380 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1382 "Double literal cannot be implicitly converted to " +
1383 "float type, use F suffix to create a float literal");
1386 Error_CannotConvertImplicit (loc, source.Type, target_type);
1392 /// Performs the explicit numeric conversions
1394 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1397 Type expr_type = expr.Type;
1400 // If we have an enumeration, extract the underlying type,
1401 // use this during the comparission, but wrap around the original
1404 Type real_target_type = target_type;
1406 if (TypeManager.IsEnumType (real_target_type))
1407 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1409 if (expr_type == TypeManager.sbyte_type){
1411 // From sbyte to byte, ushort, uint, ulong, char
1413 if (real_target_type == TypeManager.byte_type)
1414 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1415 if (real_target_type == TypeManager.ushort_type)
1416 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1417 if (real_target_type == TypeManager.uint32_type)
1418 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1419 if (real_target_type == TypeManager.uint64_type)
1420 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1421 if (real_target_type == TypeManager.char_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1423 } else if (expr_type == TypeManager.byte_type){
1425 // From byte to sbyte and char
1427 if (real_target_type == TypeManager.sbyte_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1429 if (real_target_type == TypeManager.char_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1431 } else if (expr_type == TypeManager.short_type){
1433 // From short to sbyte, byte, ushort, uint, ulong, char
1435 if (real_target_type == TypeManager.sbyte_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1437 if (real_target_type == TypeManager.byte_type)
1438 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1439 if (real_target_type == TypeManager.ushort_type)
1440 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1441 if (real_target_type == TypeManager.uint32_type)
1442 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1443 if (real_target_type == TypeManager.uint64_type)
1444 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1445 if (real_target_type == TypeManager.char_type)
1446 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1447 } else if (expr_type == TypeManager.ushort_type){
1449 // From ushort to sbyte, byte, short, char
1451 if (real_target_type == TypeManager.sbyte_type)
1452 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1453 if (real_target_type == TypeManager.byte_type)
1454 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1455 if (real_target_type == TypeManager.short_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1457 if (real_target_type == TypeManager.char_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1459 } else if (expr_type == TypeManager.int32_type){
1461 // From int to sbyte, byte, short, ushort, uint, ulong, char
1463 if (real_target_type == TypeManager.sbyte_type)
1464 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1465 if (real_target_type == TypeManager.byte_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1467 if (real_target_type == TypeManager.short_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1469 if (real_target_type == TypeManager.ushort_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1471 if (real_target_type == TypeManager.uint32_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1473 if (real_target_type == TypeManager.uint64_type)
1474 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1475 if (real_target_type == TypeManager.char_type)
1476 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1477 } else if (expr_type == TypeManager.uint32_type){
1479 // From uint to sbyte, byte, short, ushort, int, char
1481 if (real_target_type == TypeManager.sbyte_type)
1482 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1483 if (real_target_type == TypeManager.byte_type)
1484 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1485 if (real_target_type == TypeManager.short_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1487 if (real_target_type == TypeManager.ushort_type)
1488 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1489 if (real_target_type == TypeManager.int32_type)
1490 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1491 if (real_target_type == TypeManager.char_type)
1492 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1493 } else if (expr_type == TypeManager.int64_type){
1495 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1497 if (real_target_type == TypeManager.sbyte_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1499 if (real_target_type == TypeManager.byte_type)
1500 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1501 if (real_target_type == TypeManager.short_type)
1502 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1503 if (real_target_type == TypeManager.ushort_type)
1504 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1505 if (real_target_type == TypeManager.int32_type)
1506 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1507 if (real_target_type == TypeManager.uint32_type)
1508 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1509 if (real_target_type == TypeManager.uint64_type)
1510 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1511 if (real_target_type == TypeManager.char_type)
1512 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1513 } else if (expr_type == TypeManager.uint64_type){
1515 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1517 if (real_target_type == TypeManager.sbyte_type)
1518 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1519 if (real_target_type == TypeManager.byte_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1521 if (real_target_type == TypeManager.short_type)
1522 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1523 if (real_target_type == TypeManager.ushort_type)
1524 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1525 if (real_target_type == TypeManager.int32_type)
1526 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1527 if (real_target_type == TypeManager.uint32_type)
1528 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1529 if (real_target_type == TypeManager.int64_type)
1530 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1531 if (real_target_type == TypeManager.char_type)
1532 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1533 } else if (expr_type == TypeManager.char_type){
1535 // From char to sbyte, byte, short
1537 if (real_target_type == TypeManager.sbyte_type)
1538 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1539 if (real_target_type == TypeManager.byte_type)
1540 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1541 if (real_target_type == TypeManager.short_type)
1542 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1543 } else if (expr_type == TypeManager.float_type){
1545 // From float to sbyte, byte, short,
1546 // ushort, int, uint, long, ulong, char
1549 if (real_target_type == TypeManager.sbyte_type)
1550 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1551 if (real_target_type == TypeManager.byte_type)
1552 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1553 if (real_target_type == TypeManager.short_type)
1554 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1555 if (real_target_type == TypeManager.ushort_type)
1556 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1557 if (real_target_type == TypeManager.int32_type)
1558 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1559 if (real_target_type == TypeManager.uint32_type)
1560 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1561 if (real_target_type == TypeManager.int64_type)
1562 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1563 if (real_target_type == TypeManager.uint64_type)
1564 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1565 if (real_target_type == TypeManager.char_type)
1566 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1567 if (real_target_type == TypeManager.decimal_type)
1568 return InternalTypeConstructor (ec, expr, target_type);
1569 } else if (expr_type == TypeManager.double_type){
1571 // From double to byte, byte, short,
1572 // ushort, int, uint, long, ulong,
1573 // char, float or decimal
1575 if (real_target_type == TypeManager.sbyte_type)
1576 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1577 if (real_target_type == TypeManager.byte_type)
1578 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1579 if (real_target_type == TypeManager.short_type)
1580 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1581 if (real_target_type == TypeManager.ushort_type)
1582 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1583 if (real_target_type == TypeManager.int32_type)
1584 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1585 if (real_target_type == TypeManager.uint32_type)
1586 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1587 if (real_target_type == TypeManager.int64_type)
1588 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1589 if (real_target_type == TypeManager.uint64_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1591 if (real_target_type == TypeManager.char_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1593 if (real_target_type == TypeManager.float_type)
1594 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1595 if (real_target_type == TypeManager.decimal_type)
1596 return InternalTypeConstructor (ec, expr, target_type);
1599 // decimal is taken care of by the op_Explicit methods.
1605 /// Returns whether an explicit reference conversion can be performed
1606 /// from source_type to target_type
1608 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1610 bool target_is_value_type = target_type.IsValueType;
1612 if (source_type == target_type)
1616 // From object to any reference type
1618 if (source_type == TypeManager.object_type && !target_is_value_type)
1622 // From any class S to any class-type T, provided S is a base class of T
1624 if (target_type.IsSubclassOf (source_type))
1628 // From any interface type S to any interface T provided S is not derived from T
1630 if (source_type.IsInterface && target_type.IsInterface){
1631 if (!target_type.IsSubclassOf (source_type))
1636 // From any class type S to any interface T, provides S is not sealed
1637 // and provided S does not implement T.
1639 if (target_type.IsInterface && !source_type.IsSealed &&
1640 !target_type.IsAssignableFrom (source_type))
1644 // From any interface-type S to to any class type T, provided T is not
1645 // sealed, or provided T implements S.
1647 if (source_type.IsInterface &&
1648 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1651 // From an array type S with an element type Se to an array type T with an
1652 // element type Te provided all the following are true:
1653 // * S and T differe only in element type, in other words, S and T
1654 // have the same number of dimensions.
1655 // * Both Se and Te are reference types
1656 // * An explicit referenc conversions exist from Se to Te
1658 if (source_type.IsArray && target_type.IsArray) {
1659 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1661 Type source_element_type = source_type.GetElementType ();
1662 Type target_element_type = target_type.GetElementType ();
1664 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1665 if (ExplicitReferenceConversionExists (source_element_type,
1666 target_element_type))
1672 // From System.Array to any array-type
1673 if (source_type == TypeManager.array_type &&
1674 target_type.IsSubclassOf (TypeManager.array_type)){
1679 // From System delegate to any delegate-type
1681 if (source_type == TypeManager.delegate_type &&
1682 target_type.IsSubclassOf (TypeManager.delegate_type))
1686 // From ICloneable to Array or Delegate types
1688 if (source_type == TypeManager.icloneable_type &&
1689 (target_type == TypeManager.array_type ||
1690 target_type == TypeManager.delegate_type))
1697 /// Implements Explicit Reference conversions
1699 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1701 Type source_type = source.Type;
1702 bool target_is_value_type = target_type.IsValueType;
1705 // From object to any reference type
1707 if (source_type == TypeManager.object_type && !target_is_value_type)
1708 return new ClassCast (source, target_type);
1712 // From any class S to any class-type T, provided S is a base class of T
1714 if (target_type.IsSubclassOf (source_type))
1715 return new ClassCast (source, target_type);
1718 // From any interface type S to any interface T provided S is not derived from T
1720 if (source_type.IsInterface && target_type.IsInterface){
1722 Type [] ifaces = source_type.GetInterfaces ();
1724 if (TypeManager.ImplementsInterface (source_type, target_type))
1727 return new ClassCast (source, target_type);
1731 // From any class type S to any interface T, provides S is not sealed
1732 // and provided S does not implement T.
1734 if (target_type.IsInterface && !source_type.IsSealed) {
1736 if (TypeManager.ImplementsInterface (source_type, target_type))
1739 return new ClassCast (source, target_type);
1744 // From any interface-type S to to any class type T, provided T is not
1745 // sealed, or provided T implements S.
1747 if (source_type.IsInterface) {
1749 if (target_type.IsSealed)
1752 if (TypeManager.ImplementsInterface (target_type, source_type))
1753 return new ClassCast (source, target_type);
1758 // From an array type S with an element type Se to an array type T with an
1759 // element type Te provided all the following are true:
1760 // * S and T differe only in element type, in other words, S and T
1761 // have the same number of dimensions.
1762 // * Both Se and Te are reference types
1763 // * An explicit referenc conversions exist from Se to Te
1765 if (source_type.IsArray && target_type.IsArray) {
1766 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1768 Type source_element_type = source_type.GetElementType ();
1769 Type target_element_type = target_type.GetElementType ();
1771 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1772 if (ExplicitReferenceConversionExists (source_element_type,
1773 target_element_type))
1774 return new ClassCast (source, target_type);
1779 // From System.Array to any array-type
1780 if (source_type == TypeManager.array_type &&
1781 target_type.IsSubclassOf (TypeManager.array_type)){
1782 return new ClassCast (source, target_type);
1786 // From System delegate to any delegate-type
1788 if (source_type == TypeManager.delegate_type &&
1789 target_type.IsSubclassOf (TypeManager.delegate_type))
1790 return new ClassCast (source, target_type);
1793 // From ICloneable to Array or Delegate types
1795 if (source_type == TypeManager.icloneable_type &&
1796 (target_type == TypeManager.array_type ||
1797 target_type == TypeManager.delegate_type))
1798 return new ClassCast (source, target_type);
1804 /// Performs an explicit conversion of the expression `expr' whose
1805 /// type is expr.Type to `target_type'.
1807 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1808 Type target_type, Location loc)
1810 Type expr_type = expr.Type;
1811 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1816 ne = ConvertNumericExplicit (ec, expr, target_type);
1821 // Unboxing conversion.
1823 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1824 return new UnboxCast (expr, target_type);
1829 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1833 // FIXME: Is there any reason we should have EnumConstant
1834 // dealt with here instead of just using always the
1835 // UnderlyingSystemType to wrap the type?
1837 if (expr is EnumConstant)
1838 e = ((EnumConstant) expr).Child;
1840 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1843 Expression t = ConvertImplicit (ec, e, target_type, loc);
1847 return ConvertNumericExplicit (ec, e, target_type);
1850 ne = ConvertReferenceExplicit (expr, target_type);
1855 if (target_type.IsPointer){
1856 if (expr_type.IsPointer)
1857 return new EmptyCast (expr, target_type);
1859 if (expr_type == TypeManager.sbyte_type ||
1860 expr_type == TypeManager.byte_type ||
1861 expr_type == TypeManager.short_type ||
1862 expr_type == TypeManager.ushort_type ||
1863 expr_type == TypeManager.int32_type ||
1864 expr_type == TypeManager.uint32_type ||
1865 expr_type == TypeManager.uint64_type ||
1866 expr_type == TypeManager.int64_type)
1867 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1869 if (expr_type.IsPointer){
1870 if (target_type == TypeManager.sbyte_type ||
1871 target_type == TypeManager.byte_type ||
1872 target_type == TypeManager.short_type ||
1873 target_type == TypeManager.ushort_type ||
1874 target_type == TypeManager.int32_type ||
1875 target_type == TypeManager.uint32_type ||
1876 target_type == TypeManager.uint64_type ||
1877 target_type == TypeManager.int64_type){
1878 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1881 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1886 ce = ConvertNumericExplicit (ec, e, target_type);
1890 // We should always be able to go from an uint32
1891 // implicitly or explicitly to the other integral
1894 throw new Exception ("Internal compiler error");
1899 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1903 Error_CannotConvertType (loc, expr_type, target_type);
1908 /// Same as ConverExplicit, only it doesn't include user defined conversions
1910 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1911 Type target_type, Location l)
1913 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1918 ne = ConvertNumericExplicit (ec, expr, target_type);
1922 ne = ConvertReferenceExplicit (expr, target_type);
1926 Error_CannotConvertType (l, expr.Type, target_type);
1930 static string ExprClassName (ExprClass c)
1933 case ExprClass.Invalid:
1935 case ExprClass.Value:
1937 case ExprClass.Variable:
1939 case ExprClass.Namespace:
1941 case ExprClass.Type:
1943 case ExprClass.MethodGroup:
1944 return "method group";
1945 case ExprClass.PropertyAccess:
1946 return "property access";
1947 case ExprClass.EventAccess:
1948 return "event access";
1949 case ExprClass.IndexerAccess:
1950 return "indexer access";
1951 case ExprClass.Nothing:
1954 throw new Exception ("Should not happen");
1958 /// Reports that we were expecting `expr' to be of class `expected'
1960 protected void report118 (Location loc, Expression expr, string expected)
1962 string kind = "Unknown";
1965 kind = ExprClassName (expr.eclass);
1967 Error (118, loc, "Expression denotes a `" + kind +
1968 "' where a `" + expected + "' was expected");
1971 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
1973 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1974 TypeManager.CSharpName (t));
1977 public static void UnsafeError (Location loc)
1979 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
1983 /// Converts the IntConstant, UIntConstant, LongConstant or
1984 /// ULongConstant into the integral target_type. Notice
1985 /// that we do not return an `Expression' we do return
1986 /// a boxed integral type.
1988 /// FIXME: Since I added the new constants, we need to
1989 /// also support conversions from CharConstant, ByteConstant,
1990 /// SByteConstant, UShortConstant, ShortConstant
1992 /// This is used by the switch statement, so the domain
1993 /// of work is restricted to the literals above, and the
1994 /// targets are int32, uint32, char, byte, sbyte, ushort,
1995 /// short, uint64 and int64
1997 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2001 if (c.Type == target_type)
2002 return ((Constant) c).GetValue ();
2005 // Make into one of the literals we handle, we dont really care
2006 // about this value as we will just return a few limited types
2008 if (c is EnumConstant)
2009 c = ((EnumConstant)c).WidenToCompilerConstant ();
2011 if (c is IntConstant){
2012 int v = ((IntConstant) c).Value;
2014 if (target_type == TypeManager.uint32_type){
2017 } else if (target_type == TypeManager.char_type){
2018 if (v >= Char.MinValue && v <= Char.MaxValue)
2020 } else if (target_type == TypeManager.byte_type){
2021 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2023 } else if (target_type == TypeManager.sbyte_type){
2024 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2026 } else if (target_type == TypeManager.short_type){
2027 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2029 } else if (target_type == TypeManager.ushort_type){
2030 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2032 } else if (target_type == TypeManager.int64_type)
2034 else if (target_type == TypeManager.uint64_type){
2040 } else if (c is UIntConstant){
2041 uint v = ((UIntConstant) c).Value;
2043 if (target_type == TypeManager.int32_type){
2044 if (v <= Int32.MaxValue)
2046 } else if (target_type == TypeManager.char_type){
2047 if (v >= Char.MinValue && v <= Char.MaxValue)
2049 } else if (target_type == TypeManager.byte_type){
2050 if (v <= Byte.MaxValue)
2052 } else if (target_type == TypeManager.sbyte_type){
2053 if (v <= SByte.MaxValue)
2055 } else if (target_type == TypeManager.short_type){
2056 if (v <= UInt16.MaxValue)
2058 } else if (target_type == TypeManager.ushort_type){
2059 if (v <= UInt16.MaxValue)
2061 } else if (target_type == TypeManager.int64_type)
2063 else if (target_type == TypeManager.uint64_type)
2066 } else if (c is LongConstant){
2067 long v = ((LongConstant) c).Value;
2069 if (target_type == TypeManager.int32_type){
2070 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2072 } else if (target_type == TypeManager.uint32_type){
2073 if (v >= 0 && v <= UInt32.MaxValue)
2075 } else if (target_type == TypeManager.char_type){
2076 if (v >= Char.MinValue && v <= Char.MaxValue)
2078 } else if (target_type == TypeManager.byte_type){
2079 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2081 } else if (target_type == TypeManager.sbyte_type){
2082 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2084 } else if (target_type == TypeManager.short_type){
2085 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2087 } else if (target_type == TypeManager.ushort_type){
2088 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2090 } else if (target_type == TypeManager.uint64_type){
2095 } else if (c is ULongConstant){
2096 ulong v = ((ULongConstant) c).Value;
2098 if (target_type == TypeManager.int32_type){
2099 if (v <= Int32.MaxValue)
2101 } else if (target_type == TypeManager.uint32_type){
2102 if (v <= UInt32.MaxValue)
2104 } else if (target_type == TypeManager.char_type){
2105 if (v >= Char.MinValue && v <= Char.MaxValue)
2107 } else if (target_type == TypeManager.byte_type){
2108 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2110 } else if (target_type == TypeManager.sbyte_type){
2111 if (v <= (int) SByte.MaxValue)
2113 } else if (target_type == TypeManager.short_type){
2114 if (v <= UInt16.MaxValue)
2116 } else if (target_type == TypeManager.ushort_type){
2117 if (v <= UInt16.MaxValue)
2119 } else if (target_type == TypeManager.int64_type){
2120 if (v <= Int64.MaxValue)
2124 } else if (c is ByteConstant){
2125 byte v = ((ByteConstant) c).Value;
2127 if (target_type == TypeManager.int32_type)
2129 else if (target_type == TypeManager.uint32_type)
2131 else if (target_type == TypeManager.char_type)
2133 else if (target_type == TypeManager.sbyte_type){
2134 if (v <= SByte.MaxValue)
2136 } else if (target_type == TypeManager.short_type)
2138 else if (target_type == TypeManager.ushort_type)
2140 else if (target_type == TypeManager.int64_type)
2142 else if (target_type == TypeManager.uint64_type)
2145 } else if (c is SByteConstant){
2146 sbyte v = ((SByteConstant) c).Value;
2148 if (target_type == TypeManager.int32_type)
2150 else if (target_type == TypeManager.uint32_type){
2153 } else if (target_type == TypeManager.char_type){
2156 } else if (target_type == TypeManager.byte_type){
2159 } else if (target_type == TypeManager.short_type)
2161 else if (target_type == TypeManager.ushort_type){
2164 } else if (target_type == TypeManager.int64_type)
2166 else if (target_type == TypeManager.uint64_type){
2171 } else if (c is ShortConstant){
2172 short v = ((ShortConstant) c).Value;
2174 if (target_type == TypeManager.int32_type){
2176 } else if (target_type == TypeManager.uint32_type){
2179 } else if (target_type == TypeManager.char_type){
2182 } else if (target_type == TypeManager.byte_type){
2183 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2185 } else if (target_type == TypeManager.sbyte_type){
2186 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2188 } else if (target_type == TypeManager.ushort_type){
2191 } else if (target_type == TypeManager.int64_type)
2193 else if (target_type == TypeManager.uint64_type)
2197 } else if (c is UShortConstant){
2198 ushort v = ((UShortConstant) c).Value;
2200 if (target_type == TypeManager.int32_type)
2202 else if (target_type == TypeManager.uint32_type)
2204 else if (target_type == TypeManager.char_type){
2205 if (v >= Char.MinValue && v <= Char.MaxValue)
2207 } else if (target_type == TypeManager.byte_type){
2208 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2210 } else if (target_type == TypeManager.sbyte_type){
2211 if (v <= SByte.MaxValue)
2213 } else if (target_type == TypeManager.short_type){
2214 if (v <= Int16.MaxValue)
2216 } else if (target_type == TypeManager.int64_type)
2218 else if (target_type == TypeManager.uint64_type)
2222 } else if (c is CharConstant){
2223 char v = ((CharConstant) c).Value;
2225 if (target_type == TypeManager.int32_type)
2227 else if (target_type == TypeManager.uint32_type)
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.ushort_type)
2240 else if (target_type == TypeManager.int64_type)
2242 else if (target_type == TypeManager.uint64_type)
2247 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2252 // Load the object from the pointer. The `IsReference' is used
2253 // to control whether we should use Ldind_Ref or LdObj if the
2254 // value is not a `core' type.
2256 // Maybe we should try to extract this infromation form the type?
2257 // TODO: Maybe this is a bug. The reason we have this flag is because
2258 // I had almost identical code in ParameterReference (for handling
2259 // references) and in UnboxCast.
2261 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2263 if (t == TypeManager.int32_type)
2264 ig.Emit (OpCodes.Ldind_I4);
2265 else if (t == TypeManager.uint32_type)
2266 ig.Emit (OpCodes.Ldind_U4);
2267 else if (t == TypeManager.short_type)
2268 ig.Emit (OpCodes.Ldind_I2);
2269 else if (t == TypeManager.ushort_type)
2270 ig.Emit (OpCodes.Ldind_U2);
2271 else if (t == TypeManager.char_type)
2272 ig.Emit (OpCodes.Ldind_U2);
2273 else if (t == TypeManager.byte_type)
2274 ig.Emit (OpCodes.Ldind_U1);
2275 else if (t == TypeManager.sbyte_type)
2276 ig.Emit (OpCodes.Ldind_I1);
2277 else if (t == TypeManager.uint64_type)
2278 ig.Emit (OpCodes.Ldind_I8);
2279 else if (t == TypeManager.int64_type)
2280 ig.Emit (OpCodes.Ldind_I8);
2281 else if (t == TypeManager.float_type)
2282 ig.Emit (OpCodes.Ldind_R4);
2283 else if (t == TypeManager.double_type)
2284 ig.Emit (OpCodes.Ldind_R8);
2285 else if (t == TypeManager.bool_type)
2286 ig.Emit (OpCodes.Ldind_I1);
2287 else if (t == TypeManager.intptr_type)
2288 ig.Emit (OpCodes.Ldind_I);
2289 else if (TypeManager.IsEnumType (t)){
2290 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2293 ig.Emit (OpCodes.Ldind_Ref);
2295 ig.Emit (OpCodes.Ldobj, t);
2300 // The stack contains the pointer and the value of type `type'
2302 public static void StoreFromPtr (ILGenerator ig, Type type)
2304 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2305 ig.Emit (OpCodes.Stind_I4);
2306 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2307 ig.Emit (OpCodes.Stind_I8);
2308 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2309 type == TypeManager.ushort_type)
2310 ig.Emit (OpCodes.Stind_I2);
2311 else if (type == TypeManager.float_type)
2312 ig.Emit (OpCodes.Stind_R4);
2313 else if (type == TypeManager.double_type)
2314 ig.Emit (OpCodes.Stind_R8);
2315 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2316 type == TypeManager.bool_type)
2317 ig.Emit (OpCodes.Stind_I1);
2318 else if (type == TypeManager.intptr_type)
2319 ig.Emit (OpCodes.Stind_I);
2321 ig.Emit (OpCodes.Stind_Ref);
2325 // Returns the size of type `t' if known, otherwise, 0
2327 public static int GetTypeSize (Type t)
2329 if (t == TypeManager.int32_type ||
2330 t == TypeManager.uint32_type ||
2331 t == TypeManager.float_type)
2333 else if (t == TypeManager.int64_type ||
2334 t == TypeManager.uint64_type ||
2335 t == TypeManager.double_type)
2337 else if (t == TypeManager.byte_type ||
2338 t == TypeManager.sbyte_type ||
2339 t == TypeManager.bool_type)
2341 else if (t == TypeManager.short_type ||
2342 t == TypeManager.char_type ||
2343 t == TypeManager.ushort_type)
2351 /// This is just a base class for expressions that can
2352 /// appear on statements (invocations, object creation,
2353 /// assignments, post/pre increment and decrement). The idea
2354 /// being that they would support an extra Emition interface that
2355 /// does not leave a result on the stack.
2357 public abstract class ExpressionStatement : Expression {
2360 /// Requests the expression to be emitted in a `statement'
2361 /// context. This means that no new value is left on the
2362 /// stack after invoking this method (constrasted with
2363 /// Emit that will always leave a value on the stack).
2365 public abstract void EmitStatement (EmitContext ec);
2369 /// This kind of cast is used to encapsulate the child
2370 /// whose type is child.Type into an expression that is
2371 /// reported to return "return_type". This is used to encapsulate
2372 /// expressions which have compatible types, but need to be dealt
2373 /// at higher levels with.
2375 /// For example, a "byte" expression could be encapsulated in one
2376 /// of these as an "unsigned int". The type for the expression
2377 /// would be "unsigned int".
2380 public class EmptyCast : Expression {
2381 protected Expression child;
2383 public EmptyCast (Expression child, Type return_type)
2385 eclass = child.eclass;
2390 public override Expression DoResolve (EmitContext ec)
2392 // This should never be invoked, we are born in fully
2393 // initialized state.
2398 public override void Emit (EmitContext ec)
2405 /// This class is used to wrap literals which belong inside Enums
2407 public class EnumConstant : Constant {
2408 public Constant Child;
2410 public EnumConstant (Constant child, Type enum_type)
2412 eclass = child.eclass;
2417 public override Expression DoResolve (EmitContext ec)
2419 // This should never be invoked, we are born in fully
2420 // initialized state.
2425 public override void Emit (EmitContext ec)
2430 public override object GetValue ()
2432 return Child.GetValue ();
2436 // Converts from one of the valid underlying types for an enumeration
2437 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2438 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2440 public Constant WidenToCompilerConstant ()
2442 Type t = TypeManager.EnumToUnderlying (Child.Type);
2443 object v = ((Constant) Child).GetValue ();;
2445 if (t == TypeManager.int32_type)
2446 return new IntConstant ((int) v);
2447 if (t == TypeManager.uint32_type)
2448 return new UIntConstant ((uint) v);
2449 if (t == TypeManager.int64_type)
2450 return new LongConstant ((long) v);
2451 if (t == TypeManager.uint64_type)
2452 return new ULongConstant ((ulong) v);
2453 if (t == TypeManager.short_type)
2454 return new ShortConstant ((short) v);
2455 if (t == TypeManager.ushort_type)
2456 return new UShortConstant ((ushort) v);
2457 if (t == TypeManager.byte_type)
2458 return new ByteConstant ((byte) v);
2459 if (t == TypeManager.sbyte_type)
2460 return new SByteConstant ((sbyte) v);
2462 throw new Exception ("Invalid enumeration underlying type: " + t);
2466 // Extracts the value in the enumeration on its native representation
2468 public object GetPlainValue ()
2470 Type t = TypeManager.EnumToUnderlying (Child.Type);
2471 object v = ((Constant) Child).GetValue ();;
2473 if (t == TypeManager.int32_type)
2475 if (t == TypeManager.uint32_type)
2477 if (t == TypeManager.int64_type)
2479 if (t == TypeManager.uint64_type)
2481 if (t == TypeManager.short_type)
2483 if (t == TypeManager.ushort_type)
2485 if (t == TypeManager.byte_type)
2487 if (t == TypeManager.sbyte_type)
2493 public override string AsString ()
2495 return Child.AsString ();
2498 public override DoubleConstant ConvertToDouble ()
2500 return Child.ConvertToDouble ();
2503 public override FloatConstant ConvertToFloat ()
2505 return Child.ConvertToFloat ();
2508 public override ULongConstant ConvertToULong ()
2510 return Child.ConvertToULong ();
2513 public override LongConstant ConvertToLong ()
2515 return Child.ConvertToLong ();
2518 public override UIntConstant ConvertToUInt ()
2520 return Child.ConvertToUInt ();
2523 public override IntConstant ConvertToInt ()
2525 return Child.ConvertToInt ();
2530 /// This kind of cast is used to encapsulate Value Types in objects.
2532 /// The effect of it is to box the value type emitted by the previous
2535 public class BoxedCast : EmptyCast {
2537 public BoxedCast (Expression expr)
2538 : base (expr, TypeManager.object_type)
2542 public override Expression DoResolve (EmitContext ec)
2544 // This should never be invoked, we are born in fully
2545 // initialized state.
2550 public override void Emit (EmitContext ec)
2554 ec.ig.Emit (OpCodes.Box, child.Type);
2558 public class UnboxCast : EmptyCast {
2559 public UnboxCast (Expression expr, Type return_type)
2560 : base (expr, return_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)
2575 ILGenerator ig = ec.ig;
2578 ig.Emit (OpCodes.Unbox, t);
2580 LoadFromPtr (ig, t, false);
2585 /// This is used to perform explicit numeric conversions.
2587 /// Explicit numeric conversions might trigger exceptions in a checked
2588 /// context, so they should generate the conv.ovf opcodes instead of
2591 public class ConvCast : EmptyCast {
2592 public enum Mode : byte {
2593 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2595 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2596 U2_I1, U2_U1, U2_I2, U2_CH,
2597 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2598 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2599 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2600 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2601 CH_I1, CH_U1, CH_I2,
2602 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2603 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2608 public ConvCast (Expression child, Type return_type, Mode m)
2609 : base (child, return_type)
2614 public override Expression DoResolve (EmitContext ec)
2616 // This should never be invoked, we are born in fully
2617 // initialized state.
2622 public override void Emit (EmitContext ec)
2624 ILGenerator ig = ec.ig;
2630 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2631 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2632 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2633 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2634 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2636 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2637 case Mode.U1_CH: /* nothing */ break;
2639 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2640 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2641 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2642 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2643 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2644 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2646 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2647 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2648 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2649 case Mode.U2_CH: /* nothing */ break;
2651 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2652 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2653 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2654 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2655 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2656 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2657 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2659 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2660 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2661 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2662 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2663 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2664 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2666 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2667 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2668 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2669 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2670 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2671 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2672 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2673 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2675 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2676 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2677 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2678 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2679 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2680 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2681 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2682 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2684 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2685 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2686 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2688 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2689 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2690 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2691 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2692 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2693 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2694 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2695 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2696 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2698 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2699 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2700 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2701 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2702 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2703 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2704 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2705 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2706 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2707 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2711 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2712 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2713 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2714 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2715 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2717 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2718 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2720 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2721 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2722 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2723 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2724 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2725 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2727 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2728 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2729 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2730 case Mode.U2_CH: /* nothing */ break;
2732 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2733 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2734 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2735 case Mode.I4_U4: /* nothing */ break;
2736 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2737 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2738 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2740 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2741 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2742 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2743 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2744 case Mode.U4_I4: /* nothing */ break;
2745 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2747 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2748 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2749 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2750 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2751 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2752 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2753 case Mode.I8_U8: /* nothing */ break;
2754 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2756 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2757 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2758 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2759 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2760 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2761 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2762 case Mode.U8_I8: /* nothing */ break;
2763 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2765 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2766 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2767 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2769 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2770 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2771 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2772 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2773 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2774 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2775 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2776 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2777 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2779 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2780 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2781 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2782 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2783 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2784 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2785 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2786 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2787 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2788 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2794 public class OpcodeCast : EmptyCast {
2798 public OpcodeCast (Expression child, Type return_type, OpCode op)
2799 : base (child, return_type)
2803 second_valid = false;
2806 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2807 : base (child, return_type)
2812 second_valid = true;
2815 public override Expression DoResolve (EmitContext ec)
2817 // This should never be invoked, we are born in fully
2818 // initialized state.
2823 public override void Emit (EmitContext ec)
2834 /// This kind of cast is used to encapsulate a child and cast it
2835 /// to the class requested
2837 public class ClassCast : EmptyCast {
2838 public ClassCast (Expression child, Type return_type)
2839 : base (child, return_type)
2844 public override Expression DoResolve (EmitContext ec)
2846 // This should never be invoked, we are born in fully
2847 // initialized state.
2852 public override void Emit (EmitContext ec)
2856 ec.ig.Emit (OpCodes.Castclass, type);
2862 /// SimpleName expressions are initially formed of a single
2863 /// word and it only happens at the beginning of the expression.
2867 /// The expression will try to be bound to a Field, a Method
2868 /// group or a Property. If those fail we pass the name to our
2869 /// caller and the SimpleName is compounded to perform a type
2870 /// lookup. The idea behind this process is that we want to avoid
2871 /// creating a namespace map from the assemblies, as that requires
2872 /// the GetExportedTypes function to be called and a hashtable to
2873 /// be constructed which reduces startup time. If later we find
2874 /// that this is slower, we should create a `NamespaceExpr' expression
2875 /// that fully participates in the resolution process.
2877 /// For example `System.Console.WriteLine' is decomposed into
2878 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2880 /// The first SimpleName wont produce a match on its own, so it will
2882 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2884 /// System.Console will produce a TypeExpr match.
2886 /// The downside of this is that we might be hitting `LookupType' too many
2887 /// times with this scheme.
2889 public class SimpleName : Expression {
2890 public readonly string Name;
2891 public readonly Location Location;
2893 public SimpleName (string name, Location l)
2899 public static void Error120 (Location l, string name)
2903 "An object reference is required " +
2904 "for the non-static field `"+name+"'");
2908 // Checks whether we are trying to access an instance
2909 // property, method or field from a static body.
2911 Expression MemberStaticCheck (Expression e)
2913 if (e is FieldExpr){
2914 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2917 Error120 (Location, Name);
2920 } else if (e is MethodGroupExpr){
2921 MethodGroupExpr mg = (MethodGroupExpr) e;
2923 if (!mg.RemoveInstanceMethods ()){
2924 Error120 (Location, mg.Methods [0].Name);
2928 } else if (e is PropertyExpr){
2929 if (!((PropertyExpr) e).IsStatic){
2930 Error120 (Location, Name);
2933 } else if (e is EventExpr) {
2934 if (!((EventExpr) e).IsStatic) {
2935 Error120 (Location, Name);
2943 public override Expression DoResolve (EmitContext ec)
2945 return SimpleNameResolve (ec, false);
2948 public Expression DoResolveAllowStatic (EmitContext ec)
2950 return SimpleNameResolve (ec, true);
2954 /// 7.5.2: Simple Names.
2956 /// Local Variables and Parameters are handled at
2957 /// parse time, so they never occur as SimpleNames.
2959 /// The `allow_static' flag is used by MemberAccess only
2960 /// and it is used to inform us that it is ok for us to
2961 /// avoid the static check, because MemberAccess might end
2962 /// up resolving the Name as a Type name and the access as
2963 /// a static type access.
2965 /// ie: Type Type; .... { Type.GetType (""); }
2967 /// Type is both an instance variable and a Type; Type.GetType
2968 /// is the static method not an instance method of type.
2970 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2972 Expression e = null;
2975 // Stage 1: Performed by the parser (binding to locals or parameters).
2979 // Stage 2: Lookup members
2983 // For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
2984 // Hence we have two different cases
2986 e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
2988 if (e == null && ec.TypeContainer.TypeBuilder != null)
2989 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
2993 // Stage 3: Lookup symbol in the various namespaces.
2995 DeclSpace ds = ec.DeclSpace;
2999 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3000 return new TypeExpr (t);
3003 // Stage 2 part b: Lookup up if we are an alias to a type
3006 // Since we are cheating: we only do the Alias lookup for
3007 // namespaces if the name does not include any dots in it
3010 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3011 // System.Console.WriteLine (Name + " --> " + alias_value);
3012 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3014 return new TypeExpr (t);
3016 // we have alias value, but it isn't Type, so try if it's namespace
3017 return new SimpleName (alias_value, Location);
3020 // No match, maybe our parent can compose us
3021 // into something meaningful.
3026 // Stage 2 continues here.
3031 if (e is FieldExpr){
3032 FieldExpr fe = (FieldExpr) e;
3033 FieldInfo fi = fe.FieldInfo;
3035 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3036 UnsafeError (Location);
3040 if (!allow_static && !fi.IsStatic){
3041 Error120 (Location, Name);
3045 // If we are not in static code and this
3046 // field is not static, set the instance to `this'.
3049 fe.InstanceExpression = ec.This;
3053 if (fi is FieldBuilder) {
3054 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3057 object o = c.LookupConstantValue (ec);
3058 object real_value = ((Constant)c.Expr).GetValue ();
3059 return Constantify (real_value, fi.FieldType);
3066 if (e is EventExpr) {
3068 // If the event is local to this class, we transform ourselves into
3071 EventExpr ee = (EventExpr) e;
3073 Expression ml = MemberLookup (
3074 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3075 MemberTypes.Event, AllBindingFlags, Location);
3078 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3082 // If this happens, then we have an event with its own
3083 // accessors and private field etc so there's no need
3084 // to transform ourselves : we should instead flag an error
3086 Assign.error70 (ee.EventInfo, Location);
3090 ml = ExprClassFromMemberInfo (ec, mi, Location);
3093 Report.Error (-200, Location, "Internal error!!");
3097 Expression instance_expr;
3099 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3102 instance_expr = null;
3104 instance_expr = ec.This;
3106 instance_expr = instance_expr.Resolve (ec);
3108 if (instance_expr != null)
3109 instance_expr = instance_expr.Resolve (ec);
3111 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3120 return MemberStaticCheck (e);
3125 public override void Emit (EmitContext ec)
3128 // If this is ever reached, then we failed to
3129 // find the name as a namespace
3132 Error (103, Location, "The name `" + Name +
3133 "' does not exist in the class `" +
3134 ec.DeclSpace.Name + "'");
3139 /// Fully resolved expression that evaluates to a type
3141 public class TypeExpr : Expression {
3142 public TypeExpr (Type t)
3145 eclass = ExprClass.Type;
3148 override public Expression DoResolve (EmitContext ec)
3153 override public void Emit (EmitContext ec)
3155 throw new Exception ("Implement me");
3160 /// MethodGroup Expression.
3162 /// This is a fully resolved expression that evaluates to a type
3164 public class MethodGroupExpr : Expression {
3165 public MethodBase [] Methods;
3166 Expression instance_expression = null;
3168 public MethodGroupExpr (MemberInfo [] mi)
3170 Methods = new MethodBase [mi.Length];
3171 mi.CopyTo (Methods, 0);
3172 eclass = ExprClass.MethodGroup;
3175 public MethodGroupExpr (ArrayList l)
3177 Methods = new MethodBase [l.Count];
3179 l.CopyTo (Methods, 0);
3180 eclass = ExprClass.MethodGroup;
3184 // `A method group may have associated an instance expression'
3186 public Expression InstanceExpression {
3188 return instance_expression;
3192 instance_expression = value;
3196 override public Expression DoResolve (EmitContext ec)
3201 override public void Emit (EmitContext ec)
3203 throw new Exception ("This should never be reached");
3206 bool RemoveMethods (bool keep_static)
3208 ArrayList smethods = new ArrayList ();
3209 int top = Methods.Length;
3212 for (i = 0; i < top; i++){
3213 MethodBase mb = Methods [i];
3215 if (mb.IsStatic == keep_static)
3219 if (smethods.Count == 0)
3222 Methods = new MethodBase [smethods.Count];
3223 smethods.CopyTo (Methods, 0);
3229 /// Removes any instance methods from the MethodGroup, returns
3230 /// false if the resulting set is empty.
3232 public bool RemoveInstanceMethods ()
3234 return RemoveMethods (true);
3238 /// Removes any static methods from the MethodGroup, returns
3239 /// false if the resulting set is empty.
3241 public bool RemoveStaticMethods ()
3243 return RemoveMethods (false);
3248 /// Fully resolved expression that evaluates to a Field
3250 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3251 public readonly FieldInfo FieldInfo;
3252 public Expression InstanceExpression;
3255 public FieldExpr (FieldInfo fi, Location l)
3258 eclass = ExprClass.Variable;
3259 type = fi.FieldType;
3263 override public Expression DoResolve (EmitContext ec)
3265 if (!FieldInfo.IsStatic){
3266 if (InstanceExpression == null){
3267 throw new Exception ("non-static FieldExpr without instance var\n" +
3268 "You have to assign the Instance variable\n" +
3269 "Of the FieldExpr to set this\n");
3272 InstanceExpression = InstanceExpression.Resolve (ec);
3273 if (InstanceExpression == null)
3280 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3282 Expression e = DoResolve (ec);
3287 if (!FieldInfo.IsInitOnly)
3291 // InitOnly fields can only be assigned in constructors
3294 if (ec.IsConstructor)
3297 Report.Error (191, loc,
3298 "Readonly field can not be assigned outside " +
3299 "of constructor or variable initializer");
3304 override public void Emit (EmitContext ec)
3306 ILGenerator ig = ec.ig;
3307 bool is_volatile = false;
3309 if (FieldInfo is FieldBuilder){
3310 Field f = TypeManager.GetField (FieldInfo);
3311 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3314 f.status |= Field.Status.USED;
3317 if (FieldInfo.IsStatic){
3319 ig.Emit (OpCodes.Volatile);
3321 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3323 if (InstanceExpression.Type.IsValueType){
3325 LocalTemporary tempo = null;
3327 if (!(InstanceExpression is IMemoryLocation)){
3328 tempo = new LocalTemporary (
3329 ec, InstanceExpression.Type);
3331 InstanceExpression.Emit (ec);
3335 ml = (IMemoryLocation) InstanceExpression;
3337 ml.AddressOf (ec, AddressOp.Load);
3339 InstanceExpression.Emit (ec);
3342 ig.Emit (OpCodes.Volatile);
3344 ig.Emit (OpCodes.Ldfld, FieldInfo);
3348 public void EmitAssign (EmitContext ec, Expression source)
3350 bool is_static = FieldInfo.IsStatic;
3351 ILGenerator ig = ec.ig;
3354 Expression instance = InstanceExpression;
3356 if (instance.Type.IsValueType){
3357 if (instance is IMemoryLocation){
3358 IMemoryLocation ml = (IMemoryLocation) instance;
3360 ml.AddressOf (ec, AddressOp.Store);
3362 throw new Exception ("The " + instance + " of type " +
3364 " represents a ValueType and does " +
3365 "not implement IMemoryLocation");
3371 if (FieldInfo is FieldBuilder){
3372 Field f = TypeManager.GetField (FieldInfo);
3373 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3374 ig.Emit (OpCodes.Volatile);
3378 ig.Emit (OpCodes.Stsfld, FieldInfo);
3380 ig.Emit (OpCodes.Stfld, FieldInfo);
3382 if (FieldInfo is FieldBuilder){
3383 Field f = TypeManager.GetField (FieldInfo);
3385 f.status |= Field.Status.ASSIGNED;
3389 public void AddressOf (EmitContext ec, AddressOp mode)
3391 ILGenerator ig = ec.ig;
3393 if (FieldInfo is FieldBuilder){
3394 Field f = TypeManager.GetField (FieldInfo);
3395 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3396 ig.Emit (OpCodes.Volatile);
3399 if (FieldInfo is FieldBuilder){
3400 Field f = TypeManager.GetField (FieldInfo);
3402 if ((mode & AddressOp.Store) != 0)
3403 f.status |= Field.Status.ASSIGNED;
3404 if ((mode & AddressOp.Load) != 0)
3405 f.status |= Field.Status.USED;
3409 // Handle initonly fields specially: make a copy and then
3410 // get the address of the copy.
3412 if (FieldInfo.IsInitOnly){
3416 local = ig.DeclareLocal (type);
3417 ig.Emit (OpCodes.Stloc, local);
3418 ig.Emit (OpCodes.Ldloca, local);
3422 if (FieldInfo.IsStatic)
3423 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3425 InstanceExpression.Emit (ec);
3426 ig.Emit (OpCodes.Ldflda, FieldInfo);
3432 /// Expression that evaluates to a Property. The Assign class
3433 /// might set the `Value' expression if we are in an assignment.
3435 /// This is not an LValue because we need to re-write the expression, we
3436 /// can not take data from the stack and store it.
3438 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3439 public readonly PropertyInfo PropertyInfo;
3440 public readonly bool IsStatic;
3442 MethodInfo [] Accessors;
3445 Expression instance_expr;
3447 public PropertyExpr (PropertyInfo pi, Location l)
3450 eclass = ExprClass.PropertyAccess;
3453 Accessors = TypeManager.GetAccessors (pi);
3455 if (Accessors != null)
3456 for (int i = 0; i < Accessors.Length; i++){
3457 if (Accessors [i] != null)
3458 if (Accessors [i].IsStatic)
3462 Accessors = new MethodInfo [2];
3464 type = pi.PropertyType;
3468 // The instance expression associated with this expression
3470 public Expression InstanceExpression {
3472 instance_expr = value;
3476 return instance_expr;
3480 public bool VerifyAssignable ()
3482 if (!PropertyInfo.CanWrite){
3483 Report.Error (200, loc,
3484 "The property `" + PropertyInfo.Name +
3485 "' can not be assigned to, as it has not set accessor");
3492 override public Expression DoResolve (EmitContext ec)
3494 if (!PropertyInfo.CanRead){
3495 Report.Error (154, loc,
3496 "The property `" + PropertyInfo.Name +
3497 "' can not be used in " +
3498 "this context because it lacks a get accessor");
3502 type = PropertyInfo.PropertyType;
3507 override public void Emit (EmitContext ec)
3509 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
3514 // Implements the IAssignMethod interface for assignments
3516 public void EmitAssign (EmitContext ec, Expression source)
3518 Argument arg = new Argument (source, Argument.AType.Expression);
3519 ArrayList args = new ArrayList ();
3522 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3525 override public void EmitStatement (EmitContext ec)
3528 ec.ig.Emit (OpCodes.Pop);
3533 /// Fully resolved expression that evaluates to an Event
3535 public class EventExpr : Expression {
3536 public readonly EventInfo EventInfo;
3538 public Expression InstanceExpression;
3540 public readonly bool IsStatic;
3542 MethodInfo add_accessor, remove_accessor;
3544 public EventExpr (EventInfo ei, Location loc)
3548 eclass = ExprClass.EventAccess;
3550 add_accessor = TypeManager.GetAddMethod (ei);
3551 remove_accessor = TypeManager.GetRemoveMethod (ei);
3553 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3556 if (EventInfo is MyEventBuilder)
3557 type = ((MyEventBuilder) EventInfo).EventType;
3559 type = EventInfo.EventHandlerType;
3562 override public Expression DoResolve (EmitContext ec)
3564 // We are born fully resolved
3568 override public void Emit (EmitContext ec)
3570 throw new Exception ("Should not happen I think");
3573 public void EmitAddOrRemove (EmitContext ec, Expression source)
3575 Expression handler = ((Binary) source).Right;
3577 Argument arg = new Argument (handler, Argument.AType.Expression);
3578 ArrayList args = new ArrayList ();
3582 if (((Binary) source).Oper == Binary.Operator.Addition)
3583 Invocation.EmitCall (
3584 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3586 Invocation.EmitCall (
3587 ec, false, IsStatic, InstanceExpression, remove_accessor, args);