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){
1721 if (TypeManager.ImplementsInterface (source_type, target_type))
1724 return new ClassCast (source, target_type);
1728 // From any class type S to any interface T, provides S is not sealed
1729 // and provided S does not implement T.
1731 if (target_type.IsInterface && !source_type.IsSealed) {
1733 if (TypeManager.ImplementsInterface (source_type, target_type))
1736 return new ClassCast (source, target_type);
1741 // From any interface-type S to to any class type T, provided T is not
1742 // sealed, or provided T implements S.
1744 if (source_type.IsInterface) {
1746 if (target_type.IsSealed)
1749 if (TypeManager.ImplementsInterface (target_type, source_type))
1750 return new ClassCast (source, target_type);
1755 // From an array type S with an element type Se to an array type T with an
1756 // element type Te provided all the following are true:
1757 // * S and T differe only in element type, in other words, S and T
1758 // have the same number of dimensions.
1759 // * Both Se and Te are reference types
1760 // * An explicit referenc conversions exist from Se to Te
1762 if (source_type.IsArray && target_type.IsArray) {
1763 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1765 Type source_element_type = source_type.GetElementType ();
1766 Type target_element_type = target_type.GetElementType ();
1768 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1769 if (ExplicitReferenceConversionExists (source_element_type,
1770 target_element_type))
1771 return new ClassCast (source, target_type);
1776 // From System.Array to any array-type
1777 if (source_type == TypeManager.array_type &&
1778 target_type.IsSubclassOf (TypeManager.array_type)){
1779 return new ClassCast (source, target_type);
1783 // From System delegate to any delegate-type
1785 if (source_type == TypeManager.delegate_type &&
1786 target_type.IsSubclassOf (TypeManager.delegate_type))
1787 return new ClassCast (source, target_type);
1790 // From ICloneable to Array or Delegate types
1792 if (source_type == TypeManager.icloneable_type &&
1793 (target_type == TypeManager.array_type ||
1794 target_type == TypeManager.delegate_type))
1795 return new ClassCast (source, target_type);
1801 /// Performs an explicit conversion of the expression `expr' whose
1802 /// type is expr.Type to `target_type'.
1804 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1805 Type target_type, Location loc)
1807 Type expr_type = expr.Type;
1808 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1813 ne = ConvertNumericExplicit (ec, expr, target_type);
1818 // Unboxing conversion.
1820 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1821 return new UnboxCast (expr, target_type);
1826 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1830 // FIXME: Is there any reason we should have EnumConstant
1831 // dealt with here instead of just using always the
1832 // UnderlyingSystemType to wrap the type?
1834 if (expr is EnumConstant)
1835 e = ((EnumConstant) expr).Child;
1837 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1840 Expression t = ConvertImplicit (ec, e, target_type, loc);
1844 return ConvertNumericExplicit (ec, e, target_type);
1847 ne = ConvertReferenceExplicit (expr, target_type);
1852 if (target_type.IsPointer){
1853 if (expr_type.IsPointer)
1854 return new EmptyCast (expr, target_type);
1856 if (expr_type == TypeManager.sbyte_type ||
1857 expr_type == TypeManager.byte_type ||
1858 expr_type == TypeManager.short_type ||
1859 expr_type == TypeManager.ushort_type ||
1860 expr_type == TypeManager.int32_type ||
1861 expr_type == TypeManager.uint32_type ||
1862 expr_type == TypeManager.uint64_type ||
1863 expr_type == TypeManager.int64_type)
1864 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1866 if (expr_type.IsPointer){
1867 if (target_type == TypeManager.sbyte_type ||
1868 target_type == TypeManager.byte_type ||
1869 target_type == TypeManager.short_type ||
1870 target_type == TypeManager.ushort_type ||
1871 target_type == TypeManager.int32_type ||
1872 target_type == TypeManager.uint32_type ||
1873 target_type == TypeManager.uint64_type ||
1874 target_type == TypeManager.int64_type){
1875 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1878 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1883 ce = ConvertNumericExplicit (ec, e, target_type);
1887 // We should always be able to go from an uint32
1888 // implicitly or explicitly to the other integral
1891 throw new Exception ("Internal compiler error");
1896 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1900 Error_CannotConvertType (loc, expr_type, target_type);
1905 /// Same as ConverExplicit, only it doesn't include user defined conversions
1907 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1908 Type target_type, Location l)
1910 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1915 ne = ConvertNumericExplicit (ec, expr, target_type);
1919 ne = ConvertReferenceExplicit (expr, target_type);
1923 Error_CannotConvertType (l, expr.Type, target_type);
1927 static string ExprClassName (ExprClass c)
1930 case ExprClass.Invalid:
1932 case ExprClass.Value:
1934 case ExprClass.Variable:
1936 case ExprClass.Namespace:
1938 case ExprClass.Type:
1940 case ExprClass.MethodGroup:
1941 return "method group";
1942 case ExprClass.PropertyAccess:
1943 return "property access";
1944 case ExprClass.EventAccess:
1945 return "event access";
1946 case ExprClass.IndexerAccess:
1947 return "indexer access";
1948 case ExprClass.Nothing:
1951 throw new Exception ("Should not happen");
1955 /// Reports that we were expecting `expr' to be of class `expected'
1957 protected void report118 (Location loc, Expression expr, string expected)
1959 string kind = "Unknown";
1962 kind = ExprClassName (expr.eclass);
1964 Error (118, loc, "Expression denotes a `" + kind +
1965 "' where a `" + expected + "' was expected");
1968 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
1970 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1971 TypeManager.CSharpName (t));
1974 public static void UnsafeError (Location loc)
1976 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
1980 /// Converts the IntConstant, UIntConstant, LongConstant or
1981 /// ULongConstant into the integral target_type. Notice
1982 /// that we do not return an `Expression' we do return
1983 /// a boxed integral type.
1985 /// FIXME: Since I added the new constants, we need to
1986 /// also support conversions from CharConstant, ByteConstant,
1987 /// SByteConstant, UShortConstant, ShortConstant
1989 /// This is used by the switch statement, so the domain
1990 /// of work is restricted to the literals above, and the
1991 /// targets are int32, uint32, char, byte, sbyte, ushort,
1992 /// short, uint64 and int64
1994 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1998 if (c.Type == target_type)
1999 return ((Constant) c).GetValue ();
2002 // Make into one of the literals we handle, we dont really care
2003 // about this value as we will just return a few limited types
2005 if (c is EnumConstant)
2006 c = ((EnumConstant)c).WidenToCompilerConstant ();
2008 if (c is IntConstant){
2009 int v = ((IntConstant) c).Value;
2011 if (target_type == TypeManager.uint32_type){
2014 } else if (target_type == TypeManager.char_type){
2015 if (v >= Char.MinValue && v <= Char.MaxValue)
2017 } else if (target_type == TypeManager.byte_type){
2018 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2020 } else if (target_type == TypeManager.sbyte_type){
2021 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2023 } else if (target_type == TypeManager.short_type){
2024 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2026 } else if (target_type == TypeManager.ushort_type){
2027 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2029 } else if (target_type == TypeManager.int64_type)
2031 else if (target_type == TypeManager.uint64_type){
2037 } else if (c is UIntConstant){
2038 uint v = ((UIntConstant) c).Value;
2040 if (target_type == TypeManager.int32_type){
2041 if (v <= Int32.MaxValue)
2043 } else if (target_type == TypeManager.char_type){
2044 if (v >= Char.MinValue && v <= Char.MaxValue)
2046 } else if (target_type == TypeManager.byte_type){
2047 if (v <= Byte.MaxValue)
2049 } else if (target_type == TypeManager.sbyte_type){
2050 if (v <= SByte.MaxValue)
2052 } else if (target_type == TypeManager.short_type){
2053 if (v <= UInt16.MaxValue)
2055 } else if (target_type == TypeManager.ushort_type){
2056 if (v <= UInt16.MaxValue)
2058 } else if (target_type == TypeManager.int64_type)
2060 else if (target_type == TypeManager.uint64_type)
2063 } else if (c is LongConstant){
2064 long v = ((LongConstant) c).Value;
2066 if (target_type == TypeManager.int32_type){
2067 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2069 } else if (target_type == TypeManager.uint32_type){
2070 if (v >= 0 && v <= UInt32.MaxValue)
2072 } else if (target_type == TypeManager.char_type){
2073 if (v >= Char.MinValue && v <= Char.MaxValue)
2075 } else if (target_type == TypeManager.byte_type){
2076 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2078 } else if (target_type == TypeManager.sbyte_type){
2079 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2081 } else if (target_type == TypeManager.short_type){
2082 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2084 } else if (target_type == TypeManager.ushort_type){
2085 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2087 } else if (target_type == TypeManager.uint64_type){
2092 } else if (c is ULongConstant){
2093 ulong v = ((ULongConstant) c).Value;
2095 if (target_type == TypeManager.int32_type){
2096 if (v <= Int32.MaxValue)
2098 } else if (target_type == TypeManager.uint32_type){
2099 if (v <= UInt32.MaxValue)
2101 } else if (target_type == TypeManager.char_type){
2102 if (v >= Char.MinValue && v <= Char.MaxValue)
2104 } else if (target_type == TypeManager.byte_type){
2105 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2107 } else if (target_type == TypeManager.sbyte_type){
2108 if (v <= (int) SByte.MaxValue)
2110 } else if (target_type == TypeManager.short_type){
2111 if (v <= UInt16.MaxValue)
2113 } else if (target_type == TypeManager.ushort_type){
2114 if (v <= UInt16.MaxValue)
2116 } else if (target_type == TypeManager.int64_type){
2117 if (v <= Int64.MaxValue)
2121 } else if (c is ByteConstant){
2122 byte v = ((ByteConstant) c).Value;
2124 if (target_type == TypeManager.int32_type)
2126 else if (target_type == TypeManager.uint32_type)
2128 else if (target_type == TypeManager.char_type)
2130 else if (target_type == TypeManager.sbyte_type){
2131 if (v <= SByte.MaxValue)
2133 } else if (target_type == TypeManager.short_type)
2135 else if (target_type == TypeManager.ushort_type)
2137 else if (target_type == TypeManager.int64_type)
2139 else if (target_type == TypeManager.uint64_type)
2142 } else if (c is SByteConstant){
2143 sbyte v = ((SByteConstant) c).Value;
2145 if (target_type == TypeManager.int32_type)
2147 else if (target_type == TypeManager.uint32_type){
2150 } else if (target_type == TypeManager.char_type){
2153 } else if (target_type == TypeManager.byte_type){
2156 } else if (target_type == TypeManager.short_type)
2158 else if (target_type == TypeManager.ushort_type){
2161 } else if (target_type == TypeManager.int64_type)
2163 else if (target_type == TypeManager.uint64_type){
2168 } else if (c is ShortConstant){
2169 short v = ((ShortConstant) c).Value;
2171 if (target_type == TypeManager.int32_type){
2173 } else if (target_type == TypeManager.uint32_type){
2176 } else if (target_type == TypeManager.char_type){
2179 } else if (target_type == TypeManager.byte_type){
2180 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2182 } else if (target_type == TypeManager.sbyte_type){
2183 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2185 } else if (target_type == TypeManager.ushort_type){
2188 } else if (target_type == TypeManager.int64_type)
2190 else if (target_type == TypeManager.uint64_type)
2194 } else if (c is UShortConstant){
2195 ushort v = ((UShortConstant) c).Value;
2197 if (target_type == TypeManager.int32_type)
2199 else if (target_type == TypeManager.uint32_type)
2201 else if (target_type == TypeManager.char_type){
2202 if (v >= Char.MinValue && v <= Char.MaxValue)
2204 } else if (target_type == TypeManager.byte_type){
2205 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2207 } else if (target_type == TypeManager.sbyte_type){
2208 if (v <= SByte.MaxValue)
2210 } else if (target_type == TypeManager.short_type){
2211 if (v <= Int16.MaxValue)
2213 } else if (target_type == TypeManager.int64_type)
2215 else if (target_type == TypeManager.uint64_type)
2219 } else if (c is CharConstant){
2220 char v = ((CharConstant) c).Value;
2222 if (target_type == TypeManager.int32_type)
2224 else if (target_type == TypeManager.uint32_type)
2226 else if (target_type == TypeManager.byte_type){
2227 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2229 } else if (target_type == TypeManager.sbyte_type){
2230 if (v <= SByte.MaxValue)
2232 } else if (target_type == TypeManager.short_type){
2233 if (v <= Int16.MaxValue)
2235 } else if (target_type == TypeManager.ushort_type)
2237 else if (target_type == TypeManager.int64_type)
2239 else if (target_type == TypeManager.uint64_type)
2244 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2249 // Load the object from the pointer. The `IsReference' is used
2250 // to control whether we should use Ldind_Ref or LdObj if the
2251 // value is not a `core' type.
2253 // Maybe we should try to extract this infromation form the type?
2254 // TODO: Maybe this is a bug. The reason we have this flag is because
2255 // I had almost identical code in ParameterReference (for handling
2256 // references) and in UnboxCast.
2258 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2260 if (t == TypeManager.int32_type)
2261 ig.Emit (OpCodes.Ldind_I4);
2262 else if (t == TypeManager.uint32_type)
2263 ig.Emit (OpCodes.Ldind_U4);
2264 else if (t == TypeManager.short_type)
2265 ig.Emit (OpCodes.Ldind_I2);
2266 else if (t == TypeManager.ushort_type)
2267 ig.Emit (OpCodes.Ldind_U2);
2268 else if (t == TypeManager.char_type)
2269 ig.Emit (OpCodes.Ldind_U2);
2270 else if (t == TypeManager.byte_type)
2271 ig.Emit (OpCodes.Ldind_U1);
2272 else if (t == TypeManager.sbyte_type)
2273 ig.Emit (OpCodes.Ldind_I1);
2274 else if (t == TypeManager.uint64_type)
2275 ig.Emit (OpCodes.Ldind_I8);
2276 else if (t == TypeManager.int64_type)
2277 ig.Emit (OpCodes.Ldind_I8);
2278 else if (t == TypeManager.float_type)
2279 ig.Emit (OpCodes.Ldind_R4);
2280 else if (t == TypeManager.double_type)
2281 ig.Emit (OpCodes.Ldind_R8);
2282 else if (t == TypeManager.bool_type)
2283 ig.Emit (OpCodes.Ldind_I1);
2284 else if (t == TypeManager.intptr_type)
2285 ig.Emit (OpCodes.Ldind_I);
2286 else if (TypeManager.IsEnumType (t)){
2287 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2290 ig.Emit (OpCodes.Ldind_Ref);
2292 ig.Emit (OpCodes.Ldobj, t);
2297 // The stack contains the pointer and the value of type `type'
2299 public static void StoreFromPtr (ILGenerator ig, Type type)
2301 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2302 ig.Emit (OpCodes.Stind_I4);
2303 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2304 ig.Emit (OpCodes.Stind_I8);
2305 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2306 type == TypeManager.ushort_type)
2307 ig.Emit (OpCodes.Stind_I2);
2308 else if (type == TypeManager.float_type)
2309 ig.Emit (OpCodes.Stind_R4);
2310 else if (type == TypeManager.double_type)
2311 ig.Emit (OpCodes.Stind_R8);
2312 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2313 type == TypeManager.bool_type)
2314 ig.Emit (OpCodes.Stind_I1);
2315 else if (type == TypeManager.intptr_type)
2316 ig.Emit (OpCodes.Stind_I);
2318 ig.Emit (OpCodes.Stind_Ref);
2322 // Returns the size of type `t' if known, otherwise, 0
2324 public static int GetTypeSize (Type t)
2326 if (t == TypeManager.int32_type ||
2327 t == TypeManager.uint32_type ||
2328 t == TypeManager.float_type)
2330 else if (t == TypeManager.int64_type ||
2331 t == TypeManager.uint64_type ||
2332 t == TypeManager.double_type)
2334 else if (t == TypeManager.byte_type ||
2335 t == TypeManager.sbyte_type ||
2336 t == TypeManager.bool_type)
2338 else if (t == TypeManager.short_type ||
2339 t == TypeManager.char_type ||
2340 t == TypeManager.ushort_type)
2348 /// This is just a base class for expressions that can
2349 /// appear on statements (invocations, object creation,
2350 /// assignments, post/pre increment and decrement). The idea
2351 /// being that they would support an extra Emition interface that
2352 /// does not leave a result on the stack.
2354 public abstract class ExpressionStatement : Expression {
2357 /// Requests the expression to be emitted in a `statement'
2358 /// context. This means that no new value is left on the
2359 /// stack after invoking this method (constrasted with
2360 /// Emit that will always leave a value on the stack).
2362 public abstract void EmitStatement (EmitContext ec);
2366 /// This kind of cast is used to encapsulate the child
2367 /// whose type is child.Type into an expression that is
2368 /// reported to return "return_type". This is used to encapsulate
2369 /// expressions which have compatible types, but need to be dealt
2370 /// at higher levels with.
2372 /// For example, a "byte" expression could be encapsulated in one
2373 /// of these as an "unsigned int". The type for the expression
2374 /// would be "unsigned int".
2377 public class EmptyCast : Expression {
2378 protected Expression child;
2380 public EmptyCast (Expression child, Type return_type)
2382 eclass = child.eclass;
2387 public override Expression DoResolve (EmitContext ec)
2389 // This should never be invoked, we are born in fully
2390 // initialized state.
2395 public override void Emit (EmitContext ec)
2402 /// This class is used to wrap literals which belong inside Enums
2404 public class EnumConstant : Constant {
2405 public Constant Child;
2407 public EnumConstant (Constant child, Type enum_type)
2409 eclass = child.eclass;
2414 public override Expression DoResolve (EmitContext ec)
2416 // This should never be invoked, we are born in fully
2417 // initialized state.
2422 public override void Emit (EmitContext ec)
2427 public override object GetValue ()
2429 return Child.GetValue ();
2433 // Converts from one of the valid underlying types for an enumeration
2434 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2435 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2437 public Constant WidenToCompilerConstant ()
2439 Type t = TypeManager.EnumToUnderlying (Child.Type);
2440 object v = ((Constant) Child).GetValue ();;
2442 if (t == TypeManager.int32_type)
2443 return new IntConstant ((int) v);
2444 if (t == TypeManager.uint32_type)
2445 return new UIntConstant ((uint) v);
2446 if (t == TypeManager.int64_type)
2447 return new LongConstant ((long) v);
2448 if (t == TypeManager.uint64_type)
2449 return new ULongConstant ((ulong) v);
2450 if (t == TypeManager.short_type)
2451 return new ShortConstant ((short) v);
2452 if (t == TypeManager.ushort_type)
2453 return new UShortConstant ((ushort) v);
2454 if (t == TypeManager.byte_type)
2455 return new ByteConstant ((byte) v);
2456 if (t == TypeManager.sbyte_type)
2457 return new SByteConstant ((sbyte) v);
2459 throw new Exception ("Invalid enumeration underlying type: " + t);
2463 // Extracts the value in the enumeration on its native representation
2465 public object GetPlainValue ()
2467 Type t = TypeManager.EnumToUnderlying (Child.Type);
2468 object v = ((Constant) Child).GetValue ();;
2470 if (t == TypeManager.int32_type)
2472 if (t == TypeManager.uint32_type)
2474 if (t == TypeManager.int64_type)
2476 if (t == TypeManager.uint64_type)
2478 if (t == TypeManager.short_type)
2480 if (t == TypeManager.ushort_type)
2482 if (t == TypeManager.byte_type)
2484 if (t == TypeManager.sbyte_type)
2490 public override string AsString ()
2492 return Child.AsString ();
2495 public override DoubleConstant ConvertToDouble ()
2497 return Child.ConvertToDouble ();
2500 public override FloatConstant ConvertToFloat ()
2502 return Child.ConvertToFloat ();
2505 public override ULongConstant ConvertToULong ()
2507 return Child.ConvertToULong ();
2510 public override LongConstant ConvertToLong ()
2512 return Child.ConvertToLong ();
2515 public override UIntConstant ConvertToUInt ()
2517 return Child.ConvertToUInt ();
2520 public override IntConstant ConvertToInt ()
2522 return Child.ConvertToInt ();
2527 /// This kind of cast is used to encapsulate Value Types in objects.
2529 /// The effect of it is to box the value type emitted by the previous
2532 public class BoxedCast : EmptyCast {
2534 public BoxedCast (Expression expr)
2535 : base (expr, TypeManager.object_type)
2539 public override Expression DoResolve (EmitContext ec)
2541 // This should never be invoked, we are born in fully
2542 // initialized state.
2547 public override void Emit (EmitContext ec)
2551 ec.ig.Emit (OpCodes.Box, child.Type);
2555 public class UnboxCast : EmptyCast {
2556 public UnboxCast (Expression expr, Type return_type)
2557 : base (expr, return_type)
2561 public override Expression DoResolve (EmitContext ec)
2563 // This should never be invoked, we are born in fully
2564 // initialized state.
2569 public override void Emit (EmitContext ec)
2572 ILGenerator ig = ec.ig;
2575 ig.Emit (OpCodes.Unbox, t);
2577 LoadFromPtr (ig, t, false);
2582 /// This is used to perform explicit numeric conversions.
2584 /// Explicit numeric conversions might trigger exceptions in a checked
2585 /// context, so they should generate the conv.ovf opcodes instead of
2588 public class ConvCast : EmptyCast {
2589 public enum Mode : byte {
2590 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2592 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2593 U2_I1, U2_U1, U2_I2, U2_CH,
2594 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2595 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2596 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2597 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2598 CH_I1, CH_U1, CH_I2,
2599 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2600 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2605 public ConvCast (Expression child, Type return_type, Mode m)
2606 : base (child, return_type)
2611 public override Expression DoResolve (EmitContext ec)
2613 // This should never be invoked, we are born in fully
2614 // initialized state.
2619 public override void Emit (EmitContext ec)
2621 ILGenerator ig = ec.ig;
2627 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2628 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2629 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2630 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2631 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2633 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2634 case Mode.U1_CH: /* nothing */ break;
2636 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2637 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2638 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2639 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2640 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2641 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2643 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2644 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2645 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2646 case Mode.U2_CH: /* nothing */ break;
2648 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2649 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2650 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2651 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2652 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2653 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2654 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2656 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2657 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2658 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2659 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2660 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2661 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2663 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2664 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2665 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2666 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2667 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2668 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2669 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2670 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2672 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2673 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2674 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2675 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2676 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2677 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2678 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2679 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2681 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2682 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2683 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2685 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2686 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2687 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2688 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2689 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2690 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2691 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2692 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2693 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2695 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2696 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2697 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2698 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2699 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2700 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2701 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2702 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2703 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2704 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2708 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2709 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2710 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2711 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2712 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2714 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2715 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2717 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2718 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2719 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2720 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2721 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2722 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2724 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2725 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2726 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2727 case Mode.U2_CH: /* nothing */ break;
2729 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2730 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2731 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2732 case Mode.I4_U4: /* nothing */ break;
2733 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2734 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2735 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2737 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2738 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2739 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2740 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2741 case Mode.U4_I4: /* nothing */ break;
2742 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2744 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2745 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2746 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2747 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2748 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2749 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2750 case Mode.I8_U8: /* nothing */ break;
2751 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2753 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2754 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2755 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2756 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2757 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2758 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2759 case Mode.U8_I8: /* nothing */ break;
2760 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2762 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2763 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2764 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2766 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2767 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2768 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2769 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2770 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2771 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2772 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2773 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2774 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2776 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2777 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2778 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2779 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2780 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2781 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2782 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2783 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2784 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2785 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2791 public class OpcodeCast : EmptyCast {
2795 public OpcodeCast (Expression child, Type return_type, OpCode op)
2796 : base (child, return_type)
2800 second_valid = false;
2803 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2804 : base (child, return_type)
2809 second_valid = true;
2812 public override Expression DoResolve (EmitContext ec)
2814 // This should never be invoked, we are born in fully
2815 // initialized state.
2820 public override void Emit (EmitContext ec)
2831 /// This kind of cast is used to encapsulate a child and cast it
2832 /// to the class requested
2834 public class ClassCast : EmptyCast {
2835 public ClassCast (Expression child, Type return_type)
2836 : base (child, return_type)
2841 public override Expression DoResolve (EmitContext ec)
2843 // This should never be invoked, we are born in fully
2844 // initialized state.
2849 public override void Emit (EmitContext ec)
2853 ec.ig.Emit (OpCodes.Castclass, type);
2859 /// SimpleName expressions are initially formed of a single
2860 /// word and it only happens at the beginning of the expression.
2864 /// The expression will try to be bound to a Field, a Method
2865 /// group or a Property. If those fail we pass the name to our
2866 /// caller and the SimpleName is compounded to perform a type
2867 /// lookup. The idea behind this process is that we want to avoid
2868 /// creating a namespace map from the assemblies, as that requires
2869 /// the GetExportedTypes function to be called and a hashtable to
2870 /// be constructed which reduces startup time. If later we find
2871 /// that this is slower, we should create a `NamespaceExpr' expression
2872 /// that fully participates in the resolution process.
2874 /// For example `System.Console.WriteLine' is decomposed into
2875 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2877 /// The first SimpleName wont produce a match on its own, so it will
2879 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2881 /// System.Console will produce a TypeExpr match.
2883 /// The downside of this is that we might be hitting `LookupType' too many
2884 /// times with this scheme.
2886 public class SimpleName : Expression {
2887 public readonly string Name;
2888 public readonly Location Location;
2890 public SimpleName (string name, Location l)
2896 public static void Error120 (Location l, string name)
2900 "An object reference is required " +
2901 "for the non-static field `"+name+"'");
2905 // Checks whether we are trying to access an instance
2906 // property, method or field from a static body.
2908 Expression MemberStaticCheck (Expression e)
2910 if (e is FieldExpr){
2911 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2914 Error120 (Location, Name);
2917 } else if (e is MethodGroupExpr){
2918 MethodGroupExpr mg = (MethodGroupExpr) e;
2920 if (!mg.RemoveInstanceMethods ()){
2921 Error120 (Location, mg.Methods [0].Name);
2925 } else if (e is PropertyExpr){
2926 if (!((PropertyExpr) e).IsStatic){
2927 Error120 (Location, Name);
2930 } else if (e is EventExpr) {
2931 if (!((EventExpr) e).IsStatic) {
2932 Error120 (Location, Name);
2940 public override Expression DoResolve (EmitContext ec)
2942 return SimpleNameResolve (ec, false);
2945 public Expression DoResolveAllowStatic (EmitContext ec)
2947 return SimpleNameResolve (ec, true);
2951 /// 7.5.2: Simple Names.
2953 /// Local Variables and Parameters are handled at
2954 /// parse time, so they never occur as SimpleNames.
2956 /// The `allow_static' flag is used by MemberAccess only
2957 /// and it is used to inform us that it is ok for us to
2958 /// avoid the static check, because MemberAccess might end
2959 /// up resolving the Name as a Type name and the access as
2960 /// a static type access.
2962 /// ie: Type Type; .... { Type.GetType (""); }
2964 /// Type is both an instance variable and a Type; Type.GetType
2965 /// is the static method not an instance method of type.
2967 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2969 Expression e = null;
2972 // Stage 1: Performed by the parser (binding to locals or parameters).
2976 // Stage 2: Lookup members
2980 // For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
2981 // Hence we have two different cases
2983 e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
2985 if (e == null && ec.TypeContainer.TypeBuilder != null)
2986 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
2990 // Stage 3: Lookup symbol in the various namespaces.
2992 DeclSpace ds = ec.DeclSpace;
2996 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2997 return new TypeExpr (t);
3000 // Stage 2 part b: Lookup up if we are an alias to a type
3003 // Since we are cheating: we only do the Alias lookup for
3004 // namespaces if the name does not include any dots in it
3007 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3008 // System.Console.WriteLine (Name + " --> " + alias_value);
3009 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3011 return new TypeExpr (t);
3013 // we have alias value, but it isn't Type, so try if it's namespace
3014 return new SimpleName (alias_value, Location);
3017 // No match, maybe our parent can compose us
3018 // into something meaningful.
3023 // Stage 2 continues here.
3028 if (e is FieldExpr){
3029 FieldExpr fe = (FieldExpr) e;
3030 FieldInfo fi = fe.FieldInfo;
3032 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3033 UnsafeError (Location);
3037 if (!allow_static && !fi.IsStatic){
3038 Error120 (Location, Name);
3042 // If we are not in static code and this
3043 // field is not static, set the instance to `this'.
3046 fe.InstanceExpression = ec.This;
3050 if (fi is FieldBuilder) {
3051 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3054 object o = c.LookupConstantValue (ec);
3055 object real_value = ((Constant)c.Expr).GetValue ();
3056 return Constantify (real_value, fi.FieldType);
3063 if (e is EventExpr) {
3065 // If the event is local to this class, we transform ourselves into
3068 EventExpr ee = (EventExpr) e;
3070 Expression ml = MemberLookup (
3071 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3072 MemberTypes.Event, AllBindingFlags, Location);
3075 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3079 // If this happens, then we have an event with its own
3080 // accessors and private field etc so there's no need
3081 // to transform ourselves : we should instead flag an error
3083 Assign.error70 (ee.EventInfo, Location);
3087 ml = ExprClassFromMemberInfo (ec, mi, Location);
3090 Report.Error (-200, Location, "Internal error!!");
3094 Expression instance_expr;
3096 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3099 instance_expr = null;
3101 instance_expr = ec.This;
3103 instance_expr = instance_expr.Resolve (ec);
3105 if (instance_expr != null)
3106 instance_expr = instance_expr.Resolve (ec);
3108 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3117 return MemberStaticCheck (e);
3122 public override void Emit (EmitContext ec)
3125 // If this is ever reached, then we failed to
3126 // find the name as a namespace
3129 Error (103, Location, "The name `" + Name +
3130 "' does not exist in the class `" +
3131 ec.DeclSpace.Name + "'");
3136 /// Fully resolved expression that evaluates to a type
3138 public class TypeExpr : Expression {
3139 public TypeExpr (Type t)
3142 eclass = ExprClass.Type;
3145 override public Expression DoResolve (EmitContext ec)
3150 override public void Emit (EmitContext ec)
3152 throw new Exception ("Implement me");
3157 /// MethodGroup Expression.
3159 /// This is a fully resolved expression that evaluates to a type
3161 public class MethodGroupExpr : Expression {
3162 public MethodBase [] Methods;
3163 Expression instance_expression = null;
3165 public MethodGroupExpr (MemberInfo [] mi)
3167 Methods = new MethodBase [mi.Length];
3168 mi.CopyTo (Methods, 0);
3169 eclass = ExprClass.MethodGroup;
3172 public MethodGroupExpr (ArrayList l)
3174 Methods = new MethodBase [l.Count];
3176 l.CopyTo (Methods, 0);
3177 eclass = ExprClass.MethodGroup;
3181 // `A method group may have associated an instance expression'
3183 public Expression InstanceExpression {
3185 return instance_expression;
3189 instance_expression = value;
3193 override public Expression DoResolve (EmitContext ec)
3198 override public void Emit (EmitContext ec)
3200 throw new Exception ("This should never be reached");
3203 bool RemoveMethods (bool keep_static)
3205 ArrayList smethods = new ArrayList ();
3206 int top = Methods.Length;
3209 for (i = 0; i < top; i++){
3210 MethodBase mb = Methods [i];
3212 if (mb.IsStatic == keep_static)
3216 if (smethods.Count == 0)
3219 Methods = new MethodBase [smethods.Count];
3220 smethods.CopyTo (Methods, 0);
3226 /// Removes any instance methods from the MethodGroup, returns
3227 /// false if the resulting set is empty.
3229 public bool RemoveInstanceMethods ()
3231 return RemoveMethods (true);
3235 /// Removes any static methods from the MethodGroup, returns
3236 /// false if the resulting set is empty.
3238 public bool RemoveStaticMethods ()
3240 return RemoveMethods (false);
3245 /// Fully resolved expression that evaluates to a Field
3247 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3248 public readonly FieldInfo FieldInfo;
3249 public Expression InstanceExpression;
3252 public FieldExpr (FieldInfo fi, Location l)
3255 eclass = ExprClass.Variable;
3256 type = fi.FieldType;
3260 override public Expression DoResolve (EmitContext ec)
3262 if (!FieldInfo.IsStatic){
3263 if (InstanceExpression == null){
3264 throw new Exception ("non-static FieldExpr without instance var\n" +
3265 "You have to assign the Instance variable\n" +
3266 "Of the FieldExpr to set this\n");
3269 InstanceExpression = InstanceExpression.Resolve (ec);
3270 if (InstanceExpression == null)
3277 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3279 Expression e = DoResolve (ec);
3284 if (!FieldInfo.IsInitOnly)
3288 // InitOnly fields can only be assigned in constructors
3291 if (ec.IsConstructor)
3294 Report.Error (191, loc,
3295 "Readonly field can not be assigned outside " +
3296 "of constructor or variable initializer");
3301 override public void Emit (EmitContext ec)
3303 ILGenerator ig = ec.ig;
3304 bool is_volatile = false;
3306 if (FieldInfo is FieldBuilder){
3307 Field f = TypeManager.GetField (FieldInfo);
3308 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3311 f.status |= Field.Status.USED;
3314 if (FieldInfo.IsStatic){
3316 ig.Emit (OpCodes.Volatile);
3318 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3320 if (InstanceExpression.Type.IsValueType){
3322 LocalTemporary tempo = null;
3324 if (!(InstanceExpression is IMemoryLocation)){
3325 tempo = new LocalTemporary (
3326 ec, InstanceExpression.Type);
3328 InstanceExpression.Emit (ec);
3332 ml = (IMemoryLocation) InstanceExpression;
3334 ml.AddressOf (ec, AddressOp.Load);
3336 InstanceExpression.Emit (ec);
3339 ig.Emit (OpCodes.Volatile);
3341 ig.Emit (OpCodes.Ldfld, FieldInfo);
3345 public void EmitAssign (EmitContext ec, Expression source)
3347 bool is_static = FieldInfo.IsStatic;
3348 ILGenerator ig = ec.ig;
3351 Expression instance = InstanceExpression;
3353 if (instance.Type.IsValueType){
3354 if (instance is IMemoryLocation){
3355 IMemoryLocation ml = (IMemoryLocation) instance;
3357 ml.AddressOf (ec, AddressOp.Store);
3359 throw new Exception ("The " + instance + " of type " +
3361 " represents a ValueType and does " +
3362 "not implement IMemoryLocation");
3368 if (FieldInfo is FieldBuilder){
3369 Field f = TypeManager.GetField (FieldInfo);
3370 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3371 ig.Emit (OpCodes.Volatile);
3375 ig.Emit (OpCodes.Stsfld, FieldInfo);
3377 ig.Emit (OpCodes.Stfld, FieldInfo);
3379 if (FieldInfo is FieldBuilder){
3380 Field f = TypeManager.GetField (FieldInfo);
3382 f.status |= Field.Status.ASSIGNED;
3386 public void AddressOf (EmitContext ec, AddressOp mode)
3388 ILGenerator ig = ec.ig;
3390 if (FieldInfo is FieldBuilder){
3391 Field f = TypeManager.GetField (FieldInfo);
3392 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3393 ig.Emit (OpCodes.Volatile);
3396 if (FieldInfo is FieldBuilder){
3397 Field f = TypeManager.GetField (FieldInfo);
3399 if ((mode & AddressOp.Store) != 0)
3400 f.status |= Field.Status.ASSIGNED;
3401 if ((mode & AddressOp.Load) != 0)
3402 f.status |= Field.Status.USED;
3406 // Handle initonly fields specially: make a copy and then
3407 // get the address of the copy.
3409 if (FieldInfo.IsInitOnly){
3413 local = ig.DeclareLocal (type);
3414 ig.Emit (OpCodes.Stloc, local);
3415 ig.Emit (OpCodes.Ldloca, local);
3419 if (FieldInfo.IsStatic)
3420 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3422 InstanceExpression.Emit (ec);
3423 ig.Emit (OpCodes.Ldflda, FieldInfo);
3429 /// Expression that evaluates to a Property. The Assign class
3430 /// might set the `Value' expression if we are in an assignment.
3432 /// This is not an LValue because we need to re-write the expression, we
3433 /// can not take data from the stack and store it.
3435 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3436 public readonly PropertyInfo PropertyInfo;
3437 public readonly bool IsStatic;
3439 MethodInfo [] Accessors;
3442 Expression instance_expr;
3444 public PropertyExpr (PropertyInfo pi, Location l)
3447 eclass = ExprClass.PropertyAccess;
3450 Accessors = TypeManager.GetAccessors (pi);
3452 if (Accessors != null)
3453 for (int i = 0; i < Accessors.Length; i++){
3454 if (Accessors [i] != null)
3455 if (Accessors [i].IsStatic)
3459 Accessors = new MethodInfo [2];
3461 type = pi.PropertyType;
3465 // The instance expression associated with this expression
3467 public Expression InstanceExpression {
3469 instance_expr = value;
3473 return instance_expr;
3477 public bool VerifyAssignable ()
3479 if (!PropertyInfo.CanWrite){
3480 Report.Error (200, loc,
3481 "The property `" + PropertyInfo.Name +
3482 "' can not be assigned to, as it has not set accessor");
3489 override public Expression DoResolve (EmitContext ec)
3491 if (!PropertyInfo.CanRead){
3492 Report.Error (154, loc,
3493 "The property `" + PropertyInfo.Name +
3494 "' can not be used in " +
3495 "this context because it lacks a get accessor");
3499 type = PropertyInfo.PropertyType;
3504 override public void Emit (EmitContext ec)
3506 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
3511 // Implements the IAssignMethod interface for assignments
3513 public void EmitAssign (EmitContext ec, Expression source)
3515 Argument arg = new Argument (source, Argument.AType.Expression);
3516 ArrayList args = new ArrayList ();
3519 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3522 override public void EmitStatement (EmitContext ec)
3525 ec.ig.Emit (OpCodes.Pop);
3530 /// Fully resolved expression that evaluates to an Event
3532 public class EventExpr : Expression {
3533 public readonly EventInfo EventInfo;
3535 public Expression InstanceExpression;
3537 public readonly bool IsStatic;
3539 MethodInfo add_accessor, remove_accessor;
3541 public EventExpr (EventInfo ei, Location loc)
3545 eclass = ExprClass.EventAccess;
3547 add_accessor = TypeManager.GetAddMethod (ei);
3548 remove_accessor = TypeManager.GetRemoveMethod (ei);
3550 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3553 if (EventInfo is MyEventBuilder)
3554 type = ((MyEventBuilder) EventInfo).EventType;
3556 type = EventInfo.EventHandlerType;
3559 override public Expression DoResolve (EmitContext ec)
3561 // We are born fully resolved
3565 override public void Emit (EmitContext ec)
3567 throw new Exception ("Should not happen I think");
3570 public void EmitAddOrRemove (EmitContext ec, Expression source)
3572 Expression handler = ((Binary) source).Right;
3574 Argument arg = new Argument (handler, Argument.AType.Expression);
3575 ArrayList args = new ArrayList ();
3579 if (((Binary) source).Oper == Binary.Operator.Addition)
3580 Invocation.EmitCall (
3581 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3583 Invocation.EmitCall (
3584 ec, false, IsStatic, InstanceExpression, remove_accessor, args);