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 = 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 // Only do private searches on the current type,
469 // never in our parents.
471 // bf &= ~BindingFlags.NonPublic;
474 // This happens with interfaces, they have a null
477 if (current_type == null)
484 int count = mi.Length;
490 // Events are returned by both `static' and `instance'
491 // searches, which means that our above FindMembers will
492 // return two copies of the same.
494 if (count == 1 && !(mi [0] is MethodBase))
495 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
496 if (count == 2 && (mi [0] is EventInfo))
497 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
500 // We found methods, turn the search into "method scan"
503 method_list = CopyNewMethods (method_list, mi);
504 mt &= (MemberTypes.Method | MemberTypes.Constructor);
507 if (method_list != null && method_list.Count > 0)
508 return new MethodGroupExpr (method_list, loc);
511 // Interfaces do not list members they inherit, so we have to
517 Type [] ifaces = t.GetInterfaces ();
519 foreach (Type itype in ifaces){
522 x = MemberLookup (ec, itype, name, mt, bf, loc);
530 public const MemberTypes AllMemberTypes =
531 MemberTypes.Constructor |
535 MemberTypes.NestedType |
536 MemberTypes.Property;
538 public const BindingFlags AllBindingFlags =
539 BindingFlags.Public |
540 BindingFlags.Static |
541 BindingFlags.Instance;
543 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
545 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
549 /// This is a wrapper for MemberLookup that is not used to "probe", but
550 /// to find a final definition. If the final definition is not found, we
551 /// look for private members and display a useful debugging message if we
554 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
559 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
564 e = MemberLookup (ec, t, name, AllMemberTypes,
565 AllBindingFlags | BindingFlags.NonPublic, loc);
568 117, loc, "`" + t + "' does not contain a definition " +
569 "for `" + name + "'");
572 122, loc, "`" + t + "." + name +
573 "' is inaccessible due to its protection level");
579 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
581 Type expr_type = expr.Type;
583 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
584 // if we are a method group, emit a warning
589 if (target_type == TypeManager.object_type) {
591 // A pointer type cannot be converted to object
593 if (expr_type.IsPointer)
596 if (expr_type.IsValueType)
597 return new BoxedCast (expr);
598 if (expr_type.IsClass || expr_type.IsInterface)
599 return new EmptyCast (expr, target_type);
600 } else if (expr_type.IsSubclassOf (target_type)) {
601 return new EmptyCast (expr, target_type);
604 // This code is kind of mirrored inside StandardConversionExists
605 // with the small distinction that we only probe there
607 // Always ensure that the code here and there is in sync
609 // from the null type to any reference-type.
610 if (expr is NullLiteral && !target_type.IsValueType)
611 return new EmptyCast (expr, target_type);
613 // from any class-type S to any interface-type T.
614 if (expr_type.IsClass && target_type.IsInterface) {
615 if (TypeManager.ImplementsInterface (expr_type, target_type))
616 return new EmptyCast (expr, target_type);
621 // from any interface type S to interface-type T.
622 if (expr_type.IsInterface && target_type.IsInterface) {
624 if (TypeManager.ImplementsInterface (expr_type, target_type))
625 return new EmptyCast (expr, target_type);
630 // from an array-type S to an array-type of type T
631 if (expr_type.IsArray && target_type.IsArray) {
632 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
634 Type expr_element_type = expr_type.GetElementType ();
636 if (MyEmptyExpr == null)
637 MyEmptyExpr = new EmptyExpression ();
639 MyEmptyExpr.SetType (expr_element_type);
640 Type target_element_type = target_type.GetElementType ();
642 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
643 if (StandardConversionExists (MyEmptyExpr,
644 target_element_type))
645 return new EmptyCast (expr, target_type);
650 // from an array-type to System.Array
651 if (expr_type.IsArray && target_type == TypeManager.array_type)
652 return new EmptyCast (expr, target_type);
654 // from any delegate type to System.Delegate
655 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
656 target_type == TypeManager.delegate_type)
657 return new EmptyCast (expr, target_type);
659 // from any array-type or delegate type into System.ICloneable.
660 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
661 if (target_type == TypeManager.icloneable_type)
662 return new EmptyCast (expr, target_type);
672 /// Handles expressions like this: decimal d; d = 1;
673 /// and changes them into: decimal d; d = new System.Decimal (1);
675 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
677 ArrayList args = new ArrayList ();
679 args.Add (new Argument (expr, Argument.AType.Expression));
681 Expression ne = new New (target.FullName, args,
684 return ne.Resolve (ec);
688 /// Implicit Numeric Conversions.
690 /// expr is the expression to convert, returns a new expression of type
691 /// target_type or null if an implicit conversion is not possible.
693 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
694 Type target_type, Location loc)
696 Type expr_type = expr.Type;
699 // Attempt to do the implicit constant expression conversions
701 if (expr is IntConstant){
704 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
708 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
710 // Try the implicit constant expression conversion
711 // from long to ulong, instead of a nice routine,
714 long v = ((LongConstant) expr).Value;
716 return new ULongConstant ((ulong) v);
720 // If we have an enumeration, extract the underlying type,
721 // use this during the comparission, but wrap around the original
724 Type real_target_type = target_type;
726 if (TypeManager.IsEnumType (real_target_type))
727 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
729 if (expr_type == real_target_type)
730 return new EmptyCast (expr, target_type);
732 if (expr_type == TypeManager.sbyte_type){
734 // From sbyte to short, int, long, float, double.
736 if (real_target_type == TypeManager.int32_type)
737 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
738 if (real_target_type == TypeManager.int64_type)
739 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
740 if (real_target_type == TypeManager.double_type)
741 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
742 if (real_target_type == TypeManager.float_type)
743 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
744 if (real_target_type == TypeManager.short_type)
745 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
746 if (real_target_type == TypeManager.decimal_type)
747 return InternalTypeConstructor (ec, expr, target_type);
748 } else if (expr_type == TypeManager.byte_type){
750 // From byte to short, ushort, int, uint, long, ulong, float, double
752 if ((real_target_type == TypeManager.short_type) ||
753 (real_target_type == TypeManager.ushort_type) ||
754 (real_target_type == TypeManager.int32_type) ||
755 (real_target_type == TypeManager.uint32_type))
756 return new EmptyCast (expr, target_type);
758 if (real_target_type == TypeManager.uint64_type)
759 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
760 if (real_target_type == TypeManager.int64_type)
761 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
762 if (real_target_type == TypeManager.float_type)
763 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
764 if (real_target_type == TypeManager.double_type)
765 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
766 if (real_target_type == TypeManager.decimal_type)
767 return InternalTypeConstructor (ec, expr, target_type);
768 } else if (expr_type == TypeManager.short_type){
770 // From short to int, long, float, double
772 if (real_target_type == TypeManager.int32_type)
773 return new EmptyCast (expr, target_type);
774 if (real_target_type == TypeManager.int64_type)
775 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
776 if (real_target_type == TypeManager.double_type)
777 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
778 if (real_target_type == TypeManager.float_type)
779 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
780 if (real_target_type == TypeManager.decimal_type)
781 return InternalTypeConstructor (ec, expr, target_type);
782 } else if (expr_type == TypeManager.ushort_type){
784 // From ushort to int, uint, long, ulong, float, double
786 if (real_target_type == TypeManager.uint32_type)
787 return new EmptyCast (expr, target_type);
789 if (real_target_type == TypeManager.uint64_type)
790 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
791 if (real_target_type == TypeManager.int32_type)
792 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
793 if (real_target_type == TypeManager.int64_type)
794 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
795 if (real_target_type == TypeManager.double_type)
796 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
797 if (real_target_type == TypeManager.float_type)
798 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
799 if (real_target_type == TypeManager.decimal_type)
800 return InternalTypeConstructor (ec, expr, target_type);
801 } else if (expr_type == TypeManager.int32_type){
803 // From int to long, float, double
805 if (real_target_type == TypeManager.int64_type)
806 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
807 if (real_target_type == TypeManager.double_type)
808 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
809 if (real_target_type == TypeManager.float_type)
810 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
811 if (real_target_type == TypeManager.decimal_type)
812 return InternalTypeConstructor (ec, expr, target_type);
813 } else if (expr_type == TypeManager.uint32_type){
815 // From uint to long, ulong, float, double
817 if (real_target_type == TypeManager.int64_type)
818 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
819 if (real_target_type == TypeManager.uint64_type)
820 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
821 if (real_target_type == TypeManager.double_type)
822 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
824 if (real_target_type == TypeManager.float_type)
825 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
827 if (real_target_type == TypeManager.decimal_type)
828 return InternalTypeConstructor (ec, expr, target_type);
829 } else if ((expr_type == TypeManager.uint64_type) ||
830 (expr_type == TypeManager.int64_type)){
832 // From long/ulong to float, double
834 if (real_target_type == TypeManager.double_type)
835 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
837 if (real_target_type == TypeManager.float_type)
838 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
840 if (real_target_type == TypeManager.decimal_type)
841 return InternalTypeConstructor (ec, expr, target_type);
842 } else if (expr_type == TypeManager.char_type){
844 // From char to ushort, int, uint, long, ulong, float, double
846 if ((real_target_type == TypeManager.ushort_type) ||
847 (real_target_type == TypeManager.int32_type) ||
848 (real_target_type == TypeManager.uint32_type))
849 return new EmptyCast (expr, target_type);
850 if (real_target_type == TypeManager.uint64_type)
851 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
852 if (real_target_type == TypeManager.int64_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
854 if (real_target_type == TypeManager.float_type)
855 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
856 if (real_target_type == TypeManager.double_type)
857 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
858 if (real_target_type == TypeManager.decimal_type)
859 return InternalTypeConstructor (ec, expr, target_type);
860 } else if (expr_type == TypeManager.float_type){
864 if (real_target_type == TypeManager.double_type)
865 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
872 /// Determines if a standard implicit conversion exists from
873 /// expr_type to target_type
875 public static bool StandardConversionExists (Expression expr, Type target_type)
877 Type expr_type = expr.Type;
879 if (expr_type == target_type)
882 // First numeric conversions
884 if (expr_type == TypeManager.sbyte_type){
886 // From sbyte to short, int, long, float, double.
888 if ((target_type == TypeManager.int32_type) ||
889 (target_type == TypeManager.int64_type) ||
890 (target_type == TypeManager.double_type) ||
891 (target_type == TypeManager.float_type) ||
892 (target_type == TypeManager.short_type) ||
893 (target_type == TypeManager.decimal_type))
896 } else if (expr_type == TypeManager.byte_type){
898 // From byte to short, ushort, int, uint, long, ulong, float, double
900 if ((target_type == TypeManager.short_type) ||
901 (target_type == TypeManager.ushort_type) ||
902 (target_type == TypeManager.int32_type) ||
903 (target_type == TypeManager.uint32_type) ||
904 (target_type == TypeManager.uint64_type) ||
905 (target_type == TypeManager.int64_type) ||
906 (target_type == TypeManager.float_type) ||
907 (target_type == TypeManager.double_type) ||
908 (target_type == TypeManager.decimal_type))
911 } else if (expr_type == TypeManager.short_type){
913 // From short to int, long, float, double
915 if ((target_type == TypeManager.int32_type) ||
916 (target_type == TypeManager.int64_type) ||
917 (target_type == TypeManager.double_type) ||
918 (target_type == TypeManager.float_type) ||
919 (target_type == TypeManager.decimal_type))
922 } else if (expr_type == TypeManager.ushort_type){
924 // From ushort to int, uint, long, ulong, float, double
926 if ((target_type == TypeManager.uint32_type) ||
927 (target_type == TypeManager.uint64_type) ||
928 (target_type == TypeManager.int32_type) ||
929 (target_type == TypeManager.int64_type) ||
930 (target_type == TypeManager.double_type) ||
931 (target_type == TypeManager.float_type) ||
932 (target_type == TypeManager.decimal_type))
935 } else if (expr_type == TypeManager.int32_type){
937 // From int to long, float, double
939 if ((target_type == TypeManager.int64_type) ||
940 (target_type == TypeManager.double_type) ||
941 (target_type == TypeManager.float_type) ||
942 (target_type == TypeManager.decimal_type))
945 } else if (expr_type == TypeManager.uint32_type){
947 // From uint to long, ulong, float, double
949 if ((target_type == TypeManager.int64_type) ||
950 (target_type == TypeManager.uint64_type) ||
951 (target_type == TypeManager.double_type) ||
952 (target_type == TypeManager.float_type) ||
953 (target_type == TypeManager.decimal_type))
956 } else if ((expr_type == TypeManager.uint64_type) ||
957 (expr_type == TypeManager.int64_type)) {
959 // From long/ulong to float, double
961 if ((target_type == TypeManager.double_type) ||
962 (target_type == TypeManager.float_type) ||
963 (target_type == TypeManager.decimal_type))
966 } else if (expr_type == TypeManager.char_type){
968 // From char to ushort, int, uint, long, ulong, float, double
970 if ((target_type == TypeManager.ushort_type) ||
971 (target_type == TypeManager.int32_type) ||
972 (target_type == TypeManager.uint32_type) ||
973 (target_type == TypeManager.uint64_type) ||
974 (target_type == TypeManager.int64_type) ||
975 (target_type == TypeManager.float_type) ||
976 (target_type == TypeManager.double_type) ||
977 (target_type == TypeManager.decimal_type))
980 } else if (expr_type == TypeManager.float_type){
984 if (target_type == TypeManager.double_type)
988 // Next reference conversions
990 if (target_type == TypeManager.object_type) {
991 if ((expr_type.IsClass) ||
992 (expr_type.IsValueType))
995 } else if (expr_type.IsSubclassOf (target_type)) {
999 // Please remember that all code below actually comes
1000 // from ImplicitReferenceConversion so make sure code remains in sync
1002 // from any class-type S to any interface-type T.
1003 if (expr_type.IsClass && target_type.IsInterface) {
1004 if (TypeManager.ImplementsInterface (expr_type, target_type))
1008 // from any interface type S to interface-type T.
1009 // FIXME : Is it right to use IsAssignableFrom ?
1010 if (expr_type.IsInterface && target_type.IsInterface)
1011 if (target_type.IsAssignableFrom (expr_type))
1014 // from an array-type S to an array-type of type T
1015 if (expr_type.IsArray && target_type.IsArray) {
1016 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1018 Type expr_element_type = expr_type.GetElementType ();
1020 if (MyEmptyExpr == null)
1021 MyEmptyExpr = new EmptyExpression ();
1023 MyEmptyExpr.SetType (expr_element_type);
1024 Type target_element_type = target_type.GetElementType ();
1026 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1027 if (StandardConversionExists (MyEmptyExpr,
1028 target_element_type))
1033 // from an array-type to System.Array
1034 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
1037 // from any delegate type to System.Delegate
1038 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1039 target_type == TypeManager.delegate_type)
1040 if (target_type.IsAssignableFrom (expr_type))
1043 // from any array-type or delegate type into System.ICloneable.
1044 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1045 if (target_type == TypeManager.icloneable_type)
1048 // from the null type to any reference-type.
1049 if (expr is NullLiteral && !target_type.IsValueType)
1057 static EmptyExpression MyEmptyExpr;
1059 /// Tells whether an implicit conversion exists from expr_type to
1062 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
1065 if (MyEmptyExpr == null)
1066 MyEmptyExpr = new EmptyExpression (expr_type);
1068 MyEmptyExpr.SetType (expr_type);
1070 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
1074 /// Finds "most encompassed type" according to the spec (13.4.2)
1075 /// amongst the methods in the MethodGroupExpr which convert from a
1076 /// type encompassing source_type
1078 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
1082 for (int i = me.Methods.Length; i > 0; ) {
1085 MethodBase mb = me.Methods [i];
1086 ParameterData pd = Invocation.GetParameterData (mb);
1087 Type param_type = pd.ParameterType (0);
1089 Expression source = new EmptyExpression (source_type);
1090 Expression param = new EmptyExpression (param_type);
1092 if (StandardConversionExists (source, param_type)) {
1096 if (StandardConversionExists (param, best))
1105 /// Finds "most encompassing type" according to the spec (13.4.2)
1106 /// amongst the methods in the MethodGroupExpr which convert to a
1107 /// type encompassed by target_type
1109 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
1113 for (int i = me.Methods.Length; i > 0; ) {
1116 MethodInfo mi = (MethodInfo) me.Methods [i];
1117 Type ret_type = mi.ReturnType;
1119 Expression ret = new EmptyExpression (ret_type);
1121 if (StandardConversionExists (ret, target)) {
1125 if (!StandardConversionExists (ret, best))
1137 /// User-defined Implicit conversions
1139 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1140 Type target, Location loc)
1142 return UserDefinedConversion (ec, source, target, loc, false);
1146 /// User-defined Explicit conversions
1148 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1149 Type target, Location loc)
1151 return UserDefinedConversion (ec, source, target, loc, true);
1155 /// User-defined conversions
1157 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1158 Type target, Location loc,
1159 bool look_for_explicit)
1161 Expression mg1 = null, mg2 = null;
1162 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1164 MethodBase method = null;
1165 Type source_type = source.Type;
1169 // If we have a boolean type, we need to check for the True operator
1171 // FIXME : How does the False operator come into the picture ?
1172 // FIXME : This doesn't look complete and very correct !
1173 if (target == TypeManager.bool_type)
1174 op_name = "op_True";
1176 op_name = "op_Implicit";
1180 // FIXME: This whole process can be optimized to check if the
1181 // return is non-null and make the union as we go.
1183 mg1 = MemberLookup (ec, source_type, op_name, loc);
1185 if (source_type.BaseType != null)
1186 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1188 mg3 = MemberLookup (ec, target, op_name, loc);
1190 if (target.BaseType != null)
1191 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1193 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2, loc);
1194 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4, loc);
1196 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2, loc);
1198 MethodGroupExpr union3;
1200 mg1 = MemberLookup (ec, source_type, op_name, loc);
1201 if (source_type.BaseType != null)
1202 mg2 = MemberLookup (ec, source_type.BaseType, op_name, MemberTypes.Method, AllBindingFlags, loc);
1205 union3 = (MethodGroupExpr) mg2;
1206 else if (mg2 == null)
1207 union3 = (MethodGroupExpr) mg1;
1209 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1211 mg1 = MemberLookup (ec, target, op_name, MemberTypes.Method, AllBindingFlags, loc);
1214 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1216 union3 = (MethodGroupExpr) mg1;
1218 if (target.BaseType != null)
1219 mg1 = MemberLookup (ec, target.BaseType, op_name, MemberTypes.Method, AllBindingFlags, loc);
1223 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1225 union3 = (MethodGroupExpr) mg1;
1228 MethodGroupExpr union4 = null;
1230 if (look_for_explicit) {
1232 op_name = "op_Explicit";
1234 mg5 = MemberLookup (ec, source_type, op_name, loc);
1236 if (source_type.BaseType != null)
1237 mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1239 mg7 = MemberLookup (ec, target, op_name, loc);
1241 if (target.BaseType != null)
1242 mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1244 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1245 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1247 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1250 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4, loc);
1252 if (union != null) {
1254 Type most_specific_source, most_specific_target;
1256 most_specific_source = FindMostEncompassedType (union, source_type);
1257 if (most_specific_source == null)
1260 most_specific_target = FindMostEncompassingType (union, target);
1261 if (most_specific_target == null)
1266 for (int i = union.Methods.Length; i > 0;) {
1269 MethodBase mb = union.Methods [i];
1270 ParameterData pd = Invocation.GetParameterData (mb);
1271 MethodInfo mi = (MethodInfo) union.Methods [i];
1273 if (pd.ParameterType (0) == most_specific_source &&
1274 mi.ReturnType == most_specific_target) {
1280 if (method == null || count > 1) {
1281 Report.Error (-11, loc, "Ambiguous user defined conversion");
1286 // This will do the conversion to the best match that we
1287 // found. Now we need to perform an implict standard conversion
1288 // if the best match was not the type that we were requested
1291 if (look_for_explicit)
1292 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1294 source = ConvertImplicitStandard (ec, source,
1295 most_specific_source, loc);
1300 e = new UserCast ((MethodInfo) method, source);
1302 if (e.Type != target){
1303 if (!look_for_explicit)
1304 e = ConvertImplicitStandard (ec, e, target, loc);
1306 e = ConvertExplicitStandard (ec, e, target, loc);
1317 /// Converts implicitly the resolved expression `expr' into the
1318 /// `target_type'. It returns a new expression that can be used
1319 /// in a context that expects a `target_type'.
1321 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1322 Type target_type, Location loc)
1324 Type expr_type = expr.Type;
1327 if (expr_type == target_type)
1330 if (target_type == null)
1331 throw new Exception ("Target type is null");
1333 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1337 e = ImplicitUserConversion (ec, expr, target_type, loc);
1346 /// Attempts to apply the `Standard Implicit
1347 /// Conversion' rules to the expression `expr' into
1348 /// the `target_type'. It returns a new expression
1349 /// that can be used in a context that expects a
1352 /// This is different from `ConvertImplicit' in that the
1353 /// user defined implicit conversions are excluded.
1355 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1356 Type target_type, Location loc)
1358 Type expr_type = expr.Type;
1361 if (expr_type == target_type)
1364 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1368 e = ImplicitReferenceConversion (expr, target_type);
1372 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1373 IntLiteral i = (IntLiteral) expr;
1376 return new EmptyCast (expr, target_type);
1380 if (expr_type.IsPointer){
1381 if (target_type == TypeManager.void_ptr_type)
1382 return new EmptyCast (expr, target_type);
1385 // yep, comparing pointer types cant be done with
1386 // t1 == t2, we have to compare their element types.
1388 if (target_type.IsPointer){
1389 if (target_type.GetElementType()==expr_type.GetElementType())
1394 if (target_type.IsPointer){
1395 if (expr is NullLiteral)
1396 return new EmptyCast (expr, target_type);
1404 /// Attemps to perform an implict constant conversion of the IntConstant
1405 /// into a different data type using casts (See Implicit Constant
1406 /// Expression Conversions)
1408 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1410 int value = ic.Value;
1413 // FIXME: This could return constants instead of EmptyCasts
1415 if (target_type == TypeManager.sbyte_type){
1416 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1417 return new SByteConstant ((sbyte) value);
1418 } else if (target_type == TypeManager.byte_type){
1419 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1420 return new ByteConstant ((byte) value);
1421 } else if (target_type == TypeManager.short_type){
1422 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1423 return new ShortConstant ((short) value);
1424 } else if (target_type == TypeManager.ushort_type){
1425 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1426 return new UShortConstant ((ushort) value);
1427 } else if (target_type == TypeManager.uint32_type){
1429 return new UIntConstant ((uint) value);
1430 } else if (target_type == TypeManager.uint64_type){
1432 // we can optimize this case: a positive int32
1433 // always fits on a uint64. But we need an opcode
1437 return new ULongConstant ((ulong) value);
1440 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1441 return new EnumConstant (ic, target_type);
1446 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1448 string msg = "Cannot convert implicitly from `"+
1449 TypeManager.CSharpName (source) + "' to `" +
1450 TypeManager.CSharpName (target) + "'";
1452 Error (29, loc, msg);
1456 /// Attemptes to implicityly convert `target' into `type', using
1457 /// ConvertImplicit. If there is no implicit conversion, then
1458 /// an error is signaled
1460 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1461 Type target_type, Location loc)
1465 e = ConvertImplicit (ec, source, target_type, loc);
1469 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1471 "Double literal cannot be implicitly converted to " +
1472 "float type, use F suffix to create a float literal");
1475 Error_CannotConvertImplicit (loc, source.Type, target_type);
1481 /// Performs the explicit numeric conversions
1483 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1486 Type expr_type = expr.Type;
1489 // If we have an enumeration, extract the underlying type,
1490 // use this during the comparission, but wrap around the original
1493 Type real_target_type = target_type;
1495 if (TypeManager.IsEnumType (real_target_type))
1496 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1498 if (expr_type == TypeManager.sbyte_type){
1500 // From sbyte to byte, ushort, uint, ulong, char
1502 if (real_target_type == TypeManager.byte_type)
1503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1504 if (real_target_type == TypeManager.ushort_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1506 if (real_target_type == TypeManager.uint32_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1508 if (real_target_type == TypeManager.uint64_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1510 if (real_target_type == TypeManager.char_type)
1511 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1512 } else if (expr_type == TypeManager.byte_type){
1514 // From byte to sbyte and char
1516 if (real_target_type == TypeManager.sbyte_type)
1517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1518 if (real_target_type == TypeManager.char_type)
1519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1520 } else if (expr_type == TypeManager.short_type){
1522 // From short to sbyte, byte, ushort, uint, ulong, char
1524 if (real_target_type == TypeManager.sbyte_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1526 if (real_target_type == TypeManager.byte_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1528 if (real_target_type == TypeManager.ushort_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1530 if (real_target_type == TypeManager.uint32_type)
1531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1532 if (real_target_type == TypeManager.uint64_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1534 if (real_target_type == TypeManager.char_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1536 } else if (expr_type == TypeManager.ushort_type){
1538 // From ushort to sbyte, byte, short, char
1540 if (real_target_type == TypeManager.sbyte_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1542 if (real_target_type == TypeManager.byte_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1544 if (real_target_type == TypeManager.short_type)
1545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1546 if (real_target_type == TypeManager.char_type)
1547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1548 } else if (expr_type == TypeManager.int32_type){
1550 // From int to sbyte, byte, short, ushort, uint, ulong, char
1552 if (real_target_type == TypeManager.sbyte_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1554 if (real_target_type == TypeManager.byte_type)
1555 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1556 if (real_target_type == TypeManager.short_type)
1557 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1558 if (real_target_type == TypeManager.ushort_type)
1559 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1560 if (real_target_type == TypeManager.uint32_type)
1561 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1562 if (real_target_type == TypeManager.uint64_type)
1563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1564 if (real_target_type == TypeManager.char_type)
1565 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1566 } else if (expr_type == TypeManager.uint32_type){
1568 // From uint to sbyte, byte, short, ushort, int, char
1570 if (real_target_type == TypeManager.sbyte_type)
1571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1572 if (real_target_type == TypeManager.byte_type)
1573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1574 if (real_target_type == TypeManager.short_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1576 if (real_target_type == TypeManager.ushort_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1578 if (real_target_type == TypeManager.int32_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1580 if (real_target_type == TypeManager.char_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1582 } else if (expr_type == TypeManager.int64_type){
1584 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1586 if (real_target_type == TypeManager.sbyte_type)
1587 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1588 if (real_target_type == TypeManager.byte_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1590 if (real_target_type == TypeManager.short_type)
1591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1592 if (real_target_type == TypeManager.ushort_type)
1593 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1594 if (real_target_type == TypeManager.int32_type)
1595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1596 if (real_target_type == TypeManager.uint32_type)
1597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1598 if (real_target_type == TypeManager.uint64_type)
1599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1600 if (real_target_type == TypeManager.char_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1602 } else if (expr_type == TypeManager.uint64_type){
1604 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1606 if (real_target_type == TypeManager.sbyte_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1608 if (real_target_type == TypeManager.byte_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1610 if (real_target_type == TypeManager.short_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1612 if (real_target_type == TypeManager.ushort_type)
1613 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1614 if (real_target_type == TypeManager.int32_type)
1615 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1616 if (real_target_type == TypeManager.uint32_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1618 if (real_target_type == TypeManager.int64_type)
1619 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1620 if (real_target_type == TypeManager.char_type)
1621 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1622 } else if (expr_type == TypeManager.char_type){
1624 // From char to sbyte, byte, short
1626 if (real_target_type == TypeManager.sbyte_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1628 if (real_target_type == TypeManager.byte_type)
1629 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1630 if (real_target_type == TypeManager.short_type)
1631 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1632 } else if (expr_type == TypeManager.float_type){
1634 // From float to sbyte, byte, short,
1635 // ushort, int, uint, long, ulong, char
1638 if (real_target_type == TypeManager.sbyte_type)
1639 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1640 if (real_target_type == TypeManager.byte_type)
1641 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1642 if (real_target_type == TypeManager.short_type)
1643 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1644 if (real_target_type == TypeManager.ushort_type)
1645 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1646 if (real_target_type == TypeManager.int32_type)
1647 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1648 if (real_target_type == TypeManager.uint32_type)
1649 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1650 if (real_target_type == TypeManager.int64_type)
1651 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1652 if (real_target_type == TypeManager.uint64_type)
1653 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1654 if (real_target_type == TypeManager.char_type)
1655 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1656 if (real_target_type == TypeManager.decimal_type)
1657 return InternalTypeConstructor (ec, expr, target_type);
1658 } else if (expr_type == TypeManager.double_type){
1660 // From double to byte, byte, short,
1661 // ushort, int, uint, long, ulong,
1662 // char, float or decimal
1664 if (real_target_type == TypeManager.sbyte_type)
1665 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1666 if (real_target_type == TypeManager.byte_type)
1667 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1668 if (real_target_type == TypeManager.short_type)
1669 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1670 if (real_target_type == TypeManager.ushort_type)
1671 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1672 if (real_target_type == TypeManager.int32_type)
1673 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1674 if (real_target_type == TypeManager.uint32_type)
1675 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1676 if (real_target_type == TypeManager.int64_type)
1677 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1678 if (real_target_type == TypeManager.uint64_type)
1679 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1680 if (real_target_type == TypeManager.char_type)
1681 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1682 if (real_target_type == TypeManager.float_type)
1683 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1684 if (real_target_type == TypeManager.decimal_type)
1685 return InternalTypeConstructor (ec, expr, target_type);
1688 // decimal is taken care of by the op_Explicit methods.
1694 /// Returns whether an explicit reference conversion can be performed
1695 /// from source_type to target_type
1697 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1699 bool target_is_value_type = target_type.IsValueType;
1701 if (source_type == target_type)
1705 // From object to any reference type
1707 if (source_type == TypeManager.object_type && !target_is_value_type)
1711 // From any class S to any class-type T, provided S is a base class of T
1713 if (target_type.IsSubclassOf (source_type))
1717 // From any interface type S to any interface T provided S is not derived from T
1719 if (source_type.IsInterface && target_type.IsInterface){
1720 if (!target_type.IsSubclassOf (source_type))
1725 // From any class type S to any interface T, provides S is not sealed
1726 // and provided S does not implement T.
1728 if (target_type.IsInterface && !source_type.IsSealed &&
1729 !target_type.IsAssignableFrom (source_type))
1733 // From any interface-type S to to any class type T, provided T is not
1734 // sealed, or provided T implements S.
1736 if (source_type.IsInterface &&
1737 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1740 // From an array type S with an element type Se to an array type T with an
1741 // element type Te provided all the following are true:
1742 // * S and T differe only in element type, in other words, S and T
1743 // have the same number of dimensions.
1744 // * Both Se and Te are reference types
1745 // * An explicit referenc conversions exist from Se to Te
1747 if (source_type.IsArray && target_type.IsArray) {
1748 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1750 Type source_element_type = source_type.GetElementType ();
1751 Type target_element_type = target_type.GetElementType ();
1753 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1754 if (ExplicitReferenceConversionExists (source_element_type,
1755 target_element_type))
1761 // From System.Array to any array-type
1762 if (source_type == TypeManager.array_type &&
1763 target_type.IsSubclassOf (TypeManager.array_type)){
1768 // From System delegate to any delegate-type
1770 if (source_type == TypeManager.delegate_type &&
1771 target_type.IsSubclassOf (TypeManager.delegate_type))
1775 // From ICloneable to Array or Delegate types
1777 if (source_type == TypeManager.icloneable_type &&
1778 (target_type == TypeManager.array_type ||
1779 target_type == TypeManager.delegate_type))
1786 /// Implements Explicit Reference conversions
1788 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1790 Type source_type = source.Type;
1791 bool target_is_value_type = target_type.IsValueType;
1794 // From object to any reference type
1796 if (source_type == TypeManager.object_type && !target_is_value_type)
1797 return new ClassCast (source, target_type);
1801 // From any class S to any class-type T, provided S is a base class of T
1803 if (target_type.IsSubclassOf (source_type))
1804 return new ClassCast (source, target_type);
1807 // From any interface type S to any interface T provided S is not derived from T
1809 if (source_type.IsInterface && target_type.IsInterface){
1810 if (TypeManager.ImplementsInterface (source_type, target_type))
1813 return new ClassCast (source, target_type);
1817 // From any class type S to any interface T, provides S is not sealed
1818 // and provided S does not implement T.
1820 if (target_type.IsInterface && !source_type.IsSealed) {
1822 if (TypeManager.ImplementsInterface (source_type, target_type))
1825 return new ClassCast (source, target_type);
1830 // From any interface-type S to to any class type T, provided T is not
1831 // sealed, or provided T implements S.
1833 if (source_type.IsInterface) {
1835 if (target_type.IsSealed)
1838 if (TypeManager.ImplementsInterface (target_type, source_type))
1839 return new ClassCast (source, target_type);
1844 // From an array type S with an element type Se to an array type T with an
1845 // element type Te provided all the following are true:
1846 // * S and T differe only in element type, in other words, S and T
1847 // have the same number of dimensions.
1848 // * Both Se and Te are reference types
1849 // * An explicit referenc conversions exist from Se to Te
1851 if (source_type.IsArray && target_type.IsArray) {
1852 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1854 Type source_element_type = source_type.GetElementType ();
1855 Type target_element_type = target_type.GetElementType ();
1857 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1858 if (ExplicitReferenceConversionExists (source_element_type,
1859 target_element_type))
1860 return new ClassCast (source, target_type);
1865 // From System.Array to any array-type
1866 if (source_type == TypeManager.array_type &&
1867 target_type.IsSubclassOf (TypeManager.array_type)){
1868 return new ClassCast (source, target_type);
1872 // From System delegate to any delegate-type
1874 if (source_type == TypeManager.delegate_type &&
1875 target_type.IsSubclassOf (TypeManager.delegate_type))
1876 return new ClassCast (source, target_type);
1879 // From ICloneable to Array or Delegate types
1881 if (source_type == TypeManager.icloneable_type &&
1882 (target_type == TypeManager.array_type ||
1883 target_type == TypeManager.delegate_type))
1884 return new ClassCast (source, target_type);
1890 /// Performs an explicit conversion of the expression `expr' whose
1891 /// type is expr.Type to `target_type'.
1893 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1894 Type target_type, Location loc)
1896 Type expr_type = expr.Type;
1897 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1902 ne = ConvertNumericExplicit (ec, expr, target_type);
1907 // Unboxing conversion.
1909 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1910 return new UnboxCast (expr, target_type);
1915 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1919 // FIXME: Is there any reason we should have EnumConstant
1920 // dealt with here instead of just using always the
1921 // UnderlyingSystemType to wrap the type?
1923 if (expr is EnumConstant)
1924 e = ((EnumConstant) expr).Child;
1926 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1929 Expression t = ConvertImplicit (ec, e, target_type, loc);
1933 return ConvertNumericExplicit (ec, e, target_type);
1936 ne = ConvertReferenceExplicit (expr, target_type);
1941 if (target_type.IsPointer){
1942 if (expr_type.IsPointer)
1943 return new EmptyCast (expr, target_type);
1945 if (expr_type == TypeManager.sbyte_type ||
1946 expr_type == TypeManager.byte_type ||
1947 expr_type == TypeManager.short_type ||
1948 expr_type == TypeManager.ushort_type ||
1949 expr_type == TypeManager.int32_type ||
1950 expr_type == TypeManager.uint32_type ||
1951 expr_type == TypeManager.uint64_type ||
1952 expr_type == TypeManager.int64_type)
1953 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1955 if (expr_type.IsPointer){
1956 if (target_type == TypeManager.sbyte_type ||
1957 target_type == TypeManager.byte_type ||
1958 target_type == TypeManager.short_type ||
1959 target_type == TypeManager.ushort_type ||
1960 target_type == TypeManager.int32_type ||
1961 target_type == TypeManager.uint32_type ||
1962 target_type == TypeManager.uint64_type ||
1963 target_type == TypeManager.int64_type){
1964 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1967 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1972 ce = ConvertNumericExplicit (ec, e, target_type);
1976 // We should always be able to go from an uint32
1977 // implicitly or explicitly to the other integral
1980 throw new Exception ("Internal compiler error");
1985 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1989 Error_CannotConvertType (loc, expr_type, target_type);
1994 /// Same as ConverExplicit, only it doesn't include user defined conversions
1996 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1997 Type target_type, Location l)
1999 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2004 ne = ConvertNumericExplicit (ec, expr, target_type);
2008 ne = ConvertReferenceExplicit (expr, target_type);
2012 Error_CannotConvertType (l, expr.Type, target_type);
2016 static string ExprClassName (ExprClass c)
2019 case ExprClass.Invalid:
2021 case ExprClass.Value:
2023 case ExprClass.Variable:
2025 case ExprClass.Namespace:
2027 case ExprClass.Type:
2029 case ExprClass.MethodGroup:
2030 return "method group";
2031 case ExprClass.PropertyAccess:
2032 return "property access";
2033 case ExprClass.EventAccess:
2034 return "event access";
2035 case ExprClass.IndexerAccess:
2036 return "indexer access";
2037 case ExprClass.Nothing:
2040 throw new Exception ("Should not happen");
2044 /// Reports that we were expecting `expr' to be of class `expected'
2046 protected void report118 (Location loc, Expression expr, string expected)
2048 string kind = "Unknown";
2051 kind = ExprClassName (expr.eclass);
2053 Error (118, loc, "Expression denotes a `" + kind +
2054 "' where a `" + expected + "' was expected");
2057 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2059 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2060 TypeManager.CSharpName (t));
2063 public static void UnsafeError (Location loc)
2065 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2069 /// Converts the IntConstant, UIntConstant, LongConstant or
2070 /// ULongConstant into the integral target_type. Notice
2071 /// that we do not return an `Expression' we do return
2072 /// a boxed integral type.
2074 /// FIXME: Since I added the new constants, we need to
2075 /// also support conversions from CharConstant, ByteConstant,
2076 /// SByteConstant, UShortConstant, ShortConstant
2078 /// This is used by the switch statement, so the domain
2079 /// of work is restricted to the literals above, and the
2080 /// targets are int32, uint32, char, byte, sbyte, ushort,
2081 /// short, uint64 and int64
2083 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2087 if (c.Type == target_type)
2088 return ((Constant) c).GetValue ();
2091 // Make into one of the literals we handle, we dont really care
2092 // about this value as we will just return a few limited types
2094 if (c is EnumConstant)
2095 c = ((EnumConstant)c).WidenToCompilerConstant ();
2097 if (c is IntConstant){
2098 int v = ((IntConstant) c).Value;
2100 if (target_type == TypeManager.uint32_type){
2103 } else if (target_type == TypeManager.char_type){
2104 if (v >= Char.MinValue && v <= Char.MaxValue)
2106 } else if (target_type == TypeManager.byte_type){
2107 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2109 } else if (target_type == TypeManager.sbyte_type){
2110 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2112 } else if (target_type == TypeManager.short_type){
2113 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2115 } else if (target_type == TypeManager.ushort_type){
2116 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2118 } else if (target_type == TypeManager.int64_type)
2120 else if (target_type == TypeManager.uint64_type){
2126 } else if (c is UIntConstant){
2127 uint v = ((UIntConstant) c).Value;
2129 if (target_type == TypeManager.int32_type){
2130 if (v <= Int32.MaxValue)
2132 } else if (target_type == TypeManager.char_type){
2133 if (v >= Char.MinValue && v <= Char.MaxValue)
2135 } else if (target_type == TypeManager.byte_type){
2136 if (v <= Byte.MaxValue)
2138 } else if (target_type == TypeManager.sbyte_type){
2139 if (v <= SByte.MaxValue)
2141 } else if (target_type == TypeManager.short_type){
2142 if (v <= UInt16.MaxValue)
2144 } else if (target_type == TypeManager.ushort_type){
2145 if (v <= UInt16.MaxValue)
2147 } else if (target_type == TypeManager.int64_type)
2149 else if (target_type == TypeManager.uint64_type)
2152 } else if (c is LongConstant){
2153 long v = ((LongConstant) c).Value;
2155 if (target_type == TypeManager.int32_type){
2156 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2158 } else if (target_type == TypeManager.uint32_type){
2159 if (v >= 0 && v <= UInt32.MaxValue)
2161 } else if (target_type == TypeManager.char_type){
2162 if (v >= Char.MinValue && v <= Char.MaxValue)
2164 } else if (target_type == TypeManager.byte_type){
2165 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2167 } else if (target_type == TypeManager.sbyte_type){
2168 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2170 } else if (target_type == TypeManager.short_type){
2171 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2173 } else if (target_type == TypeManager.ushort_type){
2174 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2176 } else if (target_type == TypeManager.uint64_type){
2181 } else if (c is ULongConstant){
2182 ulong v = ((ULongConstant) c).Value;
2184 if (target_type == TypeManager.int32_type){
2185 if (v <= Int32.MaxValue)
2187 } else if (target_type == TypeManager.uint32_type){
2188 if (v <= UInt32.MaxValue)
2190 } else if (target_type == TypeManager.char_type){
2191 if (v >= Char.MinValue && v <= Char.MaxValue)
2193 } else if (target_type == TypeManager.byte_type){
2194 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2196 } else if (target_type == TypeManager.sbyte_type){
2197 if (v <= (int) SByte.MaxValue)
2199 } else if (target_type == TypeManager.short_type){
2200 if (v <= UInt16.MaxValue)
2202 } else if (target_type == TypeManager.ushort_type){
2203 if (v <= UInt16.MaxValue)
2205 } else if (target_type == TypeManager.int64_type){
2206 if (v <= Int64.MaxValue)
2210 } else if (c is ByteConstant){
2211 byte v = ((ByteConstant) c).Value;
2213 if (target_type == TypeManager.int32_type)
2215 else if (target_type == TypeManager.uint32_type)
2217 else if (target_type == TypeManager.char_type)
2219 else if (target_type == TypeManager.sbyte_type){
2220 if (v <= SByte.MaxValue)
2222 } else if (target_type == TypeManager.short_type)
2224 else if (target_type == TypeManager.ushort_type)
2226 else if (target_type == TypeManager.int64_type)
2228 else if (target_type == TypeManager.uint64_type)
2231 } else if (c is SByteConstant){
2232 sbyte v = ((SByteConstant) c).Value;
2234 if (target_type == TypeManager.int32_type)
2236 else if (target_type == TypeManager.uint32_type){
2239 } else if (target_type == TypeManager.char_type){
2242 } else if (target_type == TypeManager.byte_type){
2245 } else if (target_type == TypeManager.short_type)
2247 else if (target_type == TypeManager.ushort_type){
2250 } else if (target_type == TypeManager.int64_type)
2252 else if (target_type == TypeManager.uint64_type){
2257 } else if (c is ShortConstant){
2258 short v = ((ShortConstant) c).Value;
2260 if (target_type == TypeManager.int32_type){
2262 } else if (target_type == TypeManager.uint32_type){
2265 } else if (target_type == TypeManager.char_type){
2268 } else if (target_type == TypeManager.byte_type){
2269 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2271 } else if (target_type == TypeManager.sbyte_type){
2272 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2274 } else if (target_type == TypeManager.ushort_type){
2277 } else if (target_type == TypeManager.int64_type)
2279 else if (target_type == TypeManager.uint64_type)
2283 } else if (c is UShortConstant){
2284 ushort v = ((UShortConstant) c).Value;
2286 if (target_type == TypeManager.int32_type)
2288 else if (target_type == TypeManager.uint32_type)
2290 else if (target_type == TypeManager.char_type){
2291 if (v >= Char.MinValue && v <= Char.MaxValue)
2293 } else if (target_type == TypeManager.byte_type){
2294 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2296 } else if (target_type == TypeManager.sbyte_type){
2297 if (v <= SByte.MaxValue)
2299 } else if (target_type == TypeManager.short_type){
2300 if (v <= Int16.MaxValue)
2302 } else if (target_type == TypeManager.int64_type)
2304 else if (target_type == TypeManager.uint64_type)
2308 } else if (c is CharConstant){
2309 char v = ((CharConstant) c).Value;
2311 if (target_type == TypeManager.int32_type)
2313 else if (target_type == TypeManager.uint32_type)
2315 else if (target_type == TypeManager.byte_type){
2316 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2318 } else if (target_type == TypeManager.sbyte_type){
2319 if (v <= SByte.MaxValue)
2321 } else if (target_type == TypeManager.short_type){
2322 if (v <= Int16.MaxValue)
2324 } else if (target_type == TypeManager.ushort_type)
2326 else if (target_type == TypeManager.int64_type)
2328 else if (target_type == TypeManager.uint64_type)
2333 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2338 // Load the object from the pointer. The `IsReference' is used
2339 // to control whether we should use Ldind_Ref or LdObj if the
2340 // value is not a `core' type.
2342 // Maybe we should try to extract this infromation form the type?
2343 // TODO: Maybe this is a bug. The reason we have this flag is because
2344 // I had almost identical code in ParameterReference (for handling
2345 // references) and in UnboxCast.
2347 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2349 if (t == TypeManager.int32_type)
2350 ig.Emit (OpCodes.Ldind_I4);
2351 else if (t == TypeManager.uint32_type)
2352 ig.Emit (OpCodes.Ldind_U4);
2353 else if (t == TypeManager.short_type)
2354 ig.Emit (OpCodes.Ldind_I2);
2355 else if (t == TypeManager.ushort_type)
2356 ig.Emit (OpCodes.Ldind_U2);
2357 else if (t == TypeManager.char_type)
2358 ig.Emit (OpCodes.Ldind_U2);
2359 else if (t == TypeManager.byte_type)
2360 ig.Emit (OpCodes.Ldind_U1);
2361 else if (t == TypeManager.sbyte_type)
2362 ig.Emit (OpCodes.Ldind_I1);
2363 else if (t == TypeManager.uint64_type)
2364 ig.Emit (OpCodes.Ldind_I8);
2365 else if (t == TypeManager.int64_type)
2366 ig.Emit (OpCodes.Ldind_I8);
2367 else if (t == TypeManager.float_type)
2368 ig.Emit (OpCodes.Ldind_R4);
2369 else if (t == TypeManager.double_type)
2370 ig.Emit (OpCodes.Ldind_R8);
2371 else if (t == TypeManager.bool_type)
2372 ig.Emit (OpCodes.Ldind_I1);
2373 else if (t == TypeManager.intptr_type)
2374 ig.Emit (OpCodes.Ldind_I);
2375 else if (TypeManager.IsEnumType (t)){
2376 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2379 ig.Emit (OpCodes.Ldind_Ref);
2381 ig.Emit (OpCodes.Ldobj, t);
2386 // The stack contains the pointer and the value of type `type'
2388 public static void StoreFromPtr (ILGenerator ig, Type type)
2390 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2391 ig.Emit (OpCodes.Stind_I4);
2392 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2393 ig.Emit (OpCodes.Stind_I8);
2394 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2395 type == TypeManager.ushort_type)
2396 ig.Emit (OpCodes.Stind_I2);
2397 else if (type == TypeManager.float_type)
2398 ig.Emit (OpCodes.Stind_R4);
2399 else if (type == TypeManager.double_type)
2400 ig.Emit (OpCodes.Stind_R8);
2401 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2402 type == TypeManager.bool_type)
2403 ig.Emit (OpCodes.Stind_I1);
2404 else if (type == TypeManager.intptr_type)
2405 ig.Emit (OpCodes.Stind_I);
2407 ig.Emit (OpCodes.Stind_Ref);
2411 // Returns the size of type `t' if known, otherwise, 0
2413 public static int GetTypeSize (Type t)
2415 if (t == TypeManager.int32_type ||
2416 t == TypeManager.uint32_type ||
2417 t == TypeManager.float_type)
2419 else if (t == TypeManager.int64_type ||
2420 t == TypeManager.uint64_type ||
2421 t == TypeManager.double_type)
2423 else if (t == TypeManager.byte_type ||
2424 t == TypeManager.sbyte_type ||
2425 t == TypeManager.bool_type)
2427 else if (t == TypeManager.short_type ||
2428 t == TypeManager.char_type ||
2429 t == TypeManager.ushort_type)
2437 /// This is just a base class for expressions that can
2438 /// appear on statements (invocations, object creation,
2439 /// assignments, post/pre increment and decrement). The idea
2440 /// being that they would support an extra Emition interface that
2441 /// does not leave a result on the stack.
2443 public abstract class ExpressionStatement : Expression {
2446 /// Requests the expression to be emitted in a `statement'
2447 /// context. This means that no new value is left on the
2448 /// stack after invoking this method (constrasted with
2449 /// Emit that will always leave a value on the stack).
2451 public abstract void EmitStatement (EmitContext ec);
2455 /// This kind of cast is used to encapsulate the child
2456 /// whose type is child.Type into an expression that is
2457 /// reported to return "return_type". This is used to encapsulate
2458 /// expressions which have compatible types, but need to be dealt
2459 /// at higher levels with.
2461 /// For example, a "byte" expression could be encapsulated in one
2462 /// of these as an "unsigned int". The type for the expression
2463 /// would be "unsigned int".
2466 public class EmptyCast : Expression {
2467 protected Expression child;
2469 public EmptyCast (Expression child, Type return_type)
2471 eclass = child.eclass;
2476 public override Expression DoResolve (EmitContext ec)
2478 // This should never be invoked, we are born in fully
2479 // initialized state.
2484 public override void Emit (EmitContext ec)
2491 /// This class is used to wrap literals which belong inside Enums
2493 public class EnumConstant : Constant {
2494 public Constant Child;
2496 public EnumConstant (Constant child, Type enum_type)
2498 eclass = child.eclass;
2503 public override Expression DoResolve (EmitContext ec)
2505 // This should never be invoked, we are born in fully
2506 // initialized state.
2511 public override void Emit (EmitContext ec)
2516 public override object GetValue ()
2518 return Child.GetValue ();
2522 // Converts from one of the valid underlying types for an enumeration
2523 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2524 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2526 public Constant WidenToCompilerConstant ()
2528 Type t = TypeManager.EnumToUnderlying (Child.Type);
2529 object v = ((Constant) Child).GetValue ();;
2531 if (t == TypeManager.int32_type)
2532 return new IntConstant ((int) v);
2533 if (t == TypeManager.uint32_type)
2534 return new UIntConstant ((uint) v);
2535 if (t == TypeManager.int64_type)
2536 return new LongConstant ((long) v);
2537 if (t == TypeManager.uint64_type)
2538 return new ULongConstant ((ulong) v);
2539 if (t == TypeManager.short_type)
2540 return new ShortConstant ((short) v);
2541 if (t == TypeManager.ushort_type)
2542 return new UShortConstant ((ushort) v);
2543 if (t == TypeManager.byte_type)
2544 return new ByteConstant ((byte) v);
2545 if (t == TypeManager.sbyte_type)
2546 return new SByteConstant ((sbyte) v);
2548 throw new Exception ("Invalid enumeration underlying type: " + t);
2552 // Extracts the value in the enumeration on its native representation
2554 public object GetPlainValue ()
2556 Type t = TypeManager.EnumToUnderlying (Child.Type);
2557 object v = ((Constant) Child).GetValue ();;
2559 if (t == TypeManager.int32_type)
2561 if (t == TypeManager.uint32_type)
2563 if (t == TypeManager.int64_type)
2565 if (t == TypeManager.uint64_type)
2567 if (t == TypeManager.short_type)
2569 if (t == TypeManager.ushort_type)
2571 if (t == TypeManager.byte_type)
2573 if (t == TypeManager.sbyte_type)
2579 public override string AsString ()
2581 return Child.AsString ();
2584 public override DoubleConstant ConvertToDouble ()
2586 return Child.ConvertToDouble ();
2589 public override FloatConstant ConvertToFloat ()
2591 return Child.ConvertToFloat ();
2594 public override ULongConstant ConvertToULong ()
2596 return Child.ConvertToULong ();
2599 public override LongConstant ConvertToLong ()
2601 return Child.ConvertToLong ();
2604 public override UIntConstant ConvertToUInt ()
2606 return Child.ConvertToUInt ();
2609 public override IntConstant ConvertToInt ()
2611 return Child.ConvertToInt ();
2616 /// This kind of cast is used to encapsulate Value Types in objects.
2618 /// The effect of it is to box the value type emitted by the previous
2621 public class BoxedCast : EmptyCast {
2623 public BoxedCast (Expression expr)
2624 : base (expr, TypeManager.object_type)
2628 public override Expression DoResolve (EmitContext ec)
2630 // This should never be invoked, we are born in fully
2631 // initialized state.
2636 public override void Emit (EmitContext ec)
2640 ec.ig.Emit (OpCodes.Box, child.Type);
2644 public class UnboxCast : EmptyCast {
2645 public UnboxCast (Expression expr, Type return_type)
2646 : base (expr, return_type)
2650 public override Expression DoResolve (EmitContext ec)
2652 // This should never be invoked, we are born in fully
2653 // initialized state.
2658 public override void Emit (EmitContext ec)
2661 ILGenerator ig = ec.ig;
2664 ig.Emit (OpCodes.Unbox, t);
2666 LoadFromPtr (ig, t, false);
2671 /// This is used to perform explicit numeric conversions.
2673 /// Explicit numeric conversions might trigger exceptions in a checked
2674 /// context, so they should generate the conv.ovf opcodes instead of
2677 public class ConvCast : EmptyCast {
2678 public enum Mode : byte {
2679 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2681 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2682 U2_I1, U2_U1, U2_I2, U2_CH,
2683 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2684 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2685 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2686 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2687 CH_I1, CH_U1, CH_I2,
2688 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2689 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2695 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2696 : base (child, return_type)
2699 checked_state = ec.CheckState;
2702 public override Expression DoResolve (EmitContext ec)
2704 // This should never be invoked, we are born in fully
2705 // initialized state.
2710 public override void Emit (EmitContext ec)
2712 ILGenerator ig = ec.ig;
2718 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2719 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2720 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2721 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2722 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2724 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2725 case Mode.U1_CH: /* nothing */ break;
2727 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2728 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2729 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2730 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2731 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2732 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2734 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2735 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2736 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2737 case Mode.U2_CH: /* nothing */ break;
2739 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2740 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2741 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2742 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2743 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2744 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2745 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2747 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2748 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2749 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2750 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2751 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2752 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2754 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2755 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2756 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2757 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2758 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2759 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2760 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2761 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2763 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2764 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2765 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2766 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2767 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2768 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2769 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2770 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2772 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2773 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2774 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2776 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2777 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2778 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2779 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2780 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2781 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2782 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2783 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2784 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2786 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2787 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2788 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2789 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2790 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2791 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2792 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2793 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2794 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2795 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2799 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2800 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2801 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2802 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2803 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2805 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2806 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2808 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2809 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2810 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2811 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2812 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2813 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2815 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2816 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2817 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2818 case Mode.U2_CH: /* nothing */ break;
2820 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2821 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2822 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2823 case Mode.I4_U4: /* nothing */ break;
2824 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2825 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2826 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2828 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2829 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2830 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2831 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2832 case Mode.U4_I4: /* nothing */ break;
2833 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2835 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2836 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2837 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2838 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2839 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2840 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2841 case Mode.I8_U8: /* nothing */ break;
2842 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2844 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2845 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2846 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2847 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2848 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2849 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2850 case Mode.U8_I8: /* nothing */ break;
2851 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2853 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2854 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2855 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2857 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2858 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2859 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2860 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2861 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2862 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2863 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2864 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2865 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2867 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2868 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2869 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2870 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2871 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2872 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2873 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2874 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2875 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2876 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2882 public class OpcodeCast : EmptyCast {
2886 public OpcodeCast (Expression child, Type return_type, OpCode op)
2887 : base (child, return_type)
2891 second_valid = false;
2894 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2895 : base (child, return_type)
2900 second_valid = true;
2903 public override Expression DoResolve (EmitContext ec)
2905 // This should never be invoked, we are born in fully
2906 // initialized state.
2911 public override void Emit (EmitContext ec)
2922 /// This kind of cast is used to encapsulate a child and cast it
2923 /// to the class requested
2925 public class ClassCast : EmptyCast {
2926 public ClassCast (Expression child, Type return_type)
2927 : base (child, return_type)
2932 public override Expression DoResolve (EmitContext ec)
2934 // This should never be invoked, we are born in fully
2935 // initialized state.
2940 public override void Emit (EmitContext ec)
2944 ec.ig.Emit (OpCodes.Castclass, type);
2950 /// SimpleName expressions are initially formed of a single
2951 /// word and it only happens at the beginning of the expression.
2955 /// The expression will try to be bound to a Field, a Method
2956 /// group or a Property. If those fail we pass the name to our
2957 /// caller and the SimpleName is compounded to perform a type
2958 /// lookup. The idea behind this process is that we want to avoid
2959 /// creating a namespace map from the assemblies, as that requires
2960 /// the GetExportedTypes function to be called and a hashtable to
2961 /// be constructed which reduces startup time. If later we find
2962 /// that this is slower, we should create a `NamespaceExpr' expression
2963 /// that fully participates in the resolution process.
2965 /// For example `System.Console.WriteLine' is decomposed into
2966 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2968 /// The first SimpleName wont produce a match on its own, so it will
2970 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2972 /// System.Console will produce a TypeExpr match.
2974 /// The downside of this is that we might be hitting `LookupType' too many
2975 /// times with this scheme.
2977 public class SimpleName : Expression {
2978 public readonly string Name;
2979 public readonly Location Location;
2981 public SimpleName (string name, Location l)
2987 public static void Error120 (Location l, string name)
2991 "An object reference is required " +
2992 "for the non-static field `"+name+"'");
2996 // Checks whether we are trying to access an instance
2997 // property, method or field from a static body.
2999 Expression MemberStaticCheck (Expression e)
3001 if (e is FieldExpr){
3002 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3005 Error120 (Location, Name);
3008 } else if (e is MethodGroupExpr){
3009 MethodGroupExpr mg = (MethodGroupExpr) e;
3011 if (!mg.RemoveInstanceMethods ()){
3012 Error120 (Location, mg.Methods [0].Name);
3016 } else if (e is PropertyExpr){
3017 if (!((PropertyExpr) e).IsStatic){
3018 Error120 (Location, Name);
3021 } else if (e is EventExpr) {
3022 if (!((EventExpr) e).IsStatic) {
3023 Error120 (Location, Name);
3031 public override Expression DoResolve (EmitContext ec)
3033 return SimpleNameResolve (ec, false);
3036 public Expression DoResolveAllowStatic (EmitContext ec)
3038 return SimpleNameResolve (ec, true);
3042 /// 7.5.2: Simple Names.
3044 /// Local Variables and Parameters are handled at
3045 /// parse time, so they never occur as SimpleNames.
3047 /// The `allow_static' flag is used by MemberAccess only
3048 /// and it is used to inform us that it is ok for us to
3049 /// avoid the static check, because MemberAccess might end
3050 /// up resolving the Name as a Type name and the access as
3051 /// a static type access.
3053 /// ie: Type Type; .... { Type.GetType (""); }
3055 /// Type is both an instance variable and a Type; Type.GetType
3056 /// is the static method not an instance method of type.
3058 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
3060 Expression e = null;
3063 // Stage 1: Performed by the parser (binding to locals or parameters).
3065 if (!ec.OnlyLookupTypes){
3066 Block current_block = ec.CurrentBlock;
3067 if (current_block != null && current_block.IsVariableDefined (Name)){
3068 LocalVariableReference var;
3070 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3072 return var.Resolve (ec);
3076 // Stage 2: Lookup members
3080 // For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
3081 // Hence we have two different cases
3083 e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
3085 if (e == null && ec.ContainerType != null)
3086 e = MemberLookup (ec, ec.ContainerType, Name, Location);
3089 // Continuation of stage 2
3092 // Stage 3: Lookup symbol in the various namespaces.
3094 DeclSpace ds = ec.DeclSpace;
3098 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3099 return new TypeExpr (t);
3102 // Stage 2 part b: Lookup up if we are an alias to a type
3105 // Since we are cheating: we only do the Alias lookup for
3106 // namespaces if the name does not include any dots in it
3109 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3110 // System.Console.WriteLine (Name + " --> " + alias_value);
3111 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3113 return new TypeExpr (t);
3115 // we have alias value, but it isn't Type, so try if it's namespace
3116 return new SimpleName (alias_value, Location);
3119 // No match, maybe our parent can compose us
3120 // into something meaningful.
3125 // Stage 2 continues here.
3130 if (ec.OnlyLookupTypes)
3133 if (e is FieldExpr){
3134 FieldExpr fe = (FieldExpr) e;
3135 FieldInfo fi = fe.FieldInfo;
3137 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3138 UnsafeError (Location);
3142 if (!allow_static && !fi.IsStatic){
3143 Error120 (Location, Name);
3147 // If we are not in static code and this
3148 // field is not static, set the instance to `this'.
3151 fe.InstanceExpression = ec.This;
3155 if (fi is FieldBuilder) {
3156 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3159 object o = c.LookupConstantValue (ec);
3160 object real_value = ((Constant)c.Expr).GetValue ();
3161 return Constantify (real_value, fi.FieldType);
3166 Type t = fi.FieldType;
3167 Type decl_type = fi.DeclaringType;
3170 if (fi is FieldBuilder)
3171 o = TypeManager.GetValue ((FieldBuilder) fi);
3173 o = fi.GetValue (fi);
3175 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3176 Expression enum_member = MemberLookup (
3177 ec, decl_type, "value__", MemberTypes.Field,
3178 AllBindingFlags, Location);
3180 Enum en = TypeManager.LookupEnum (decl_type);
3184 c = Constantify (o, en.UnderlyingType);
3186 c = Constantify (o, enum_member.Type);
3188 return new EnumConstant (c, decl_type);
3191 Expression exp = Constantify (o, t);
3197 if (e is EventExpr) {
3199 // If the event is local to this class, we transform ourselves into
3202 EventExpr ee = (EventExpr) e;
3204 Expression ml = MemberLookup (
3205 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3206 MemberTypes.Event, AllBindingFlags, Location);
3209 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3213 // If this happens, then we have an event with its own
3214 // accessors and private field etc so there's no need
3215 // to transform ourselves : we should instead flag an error
3217 Assign.error70 (ee.EventInfo, Location);
3221 ml = ExprClassFromMemberInfo (ec, mi, Location);
3224 Report.Error (-200, Location, "Internal error!!");
3228 Expression instance_expr;
3230 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3233 instance_expr = null;
3235 instance_expr = ec.This;
3237 instance_expr = instance_expr.Resolve (ec);
3239 if (instance_expr != null)
3240 instance_expr = instance_expr.Resolve (ec);
3242 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3251 return MemberStaticCheck (e);
3256 public override void Emit (EmitContext ec)
3259 // If this is ever reached, then we failed to
3260 // find the name as a namespace
3263 Error (103, Location, "The name `" + Name +
3264 "' does not exist in the class `" +
3265 ec.DeclSpace.Name + "'");
3270 /// Fully resolved expression that evaluates to a type
3272 public class TypeExpr : Expression {
3273 public TypeExpr (Type t)
3276 eclass = ExprClass.Type;
3279 override public Expression DoResolve (EmitContext ec)
3284 override public void Emit (EmitContext ec)
3286 throw new Exception ("Implement me");
3291 /// MethodGroup Expression.
3293 /// This is a fully resolved expression that evaluates to a type
3295 public class MethodGroupExpr : Expression {
3296 public MethodBase [] Methods;
3298 Expression instance_expression = null;
3300 public MethodGroupExpr (MemberInfo [] mi, Location l)
3302 Methods = new MethodBase [mi.Length];
3303 mi.CopyTo (Methods, 0);
3304 eclass = ExprClass.MethodGroup;
3305 type = TypeManager.object_type;
3309 public MethodGroupExpr (ArrayList list, Location l)
3311 Methods = new MethodBase [list.Count];
3314 list.CopyTo (Methods, 0);
3316 foreach (MemberInfo m in list){
3317 if (!(m is MethodBase)){
3318 Console.WriteLine ("Name " + m.Name);
3319 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3325 eclass = ExprClass.MethodGroup;
3326 type = TypeManager.object_type;
3330 // `A method group may have associated an instance expression'
3332 public Expression InstanceExpression {
3334 return instance_expression;
3338 instance_expression = value;
3342 override public Expression DoResolve (EmitContext ec)
3347 public void ReportUsageError ()
3349 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3350 Methods [0].Name + "()' is referenced without parentheses");
3353 override public void Emit (EmitContext ec)
3355 ReportUsageError ();
3358 bool RemoveMethods (bool keep_static)
3360 ArrayList smethods = new ArrayList ();
3361 int top = Methods.Length;
3364 for (i = 0; i < top; i++){
3365 MethodBase mb = Methods [i];
3367 if (mb.IsStatic == keep_static)
3371 if (smethods.Count == 0)
3374 Methods = new MethodBase [smethods.Count];
3375 smethods.CopyTo (Methods, 0);
3381 /// Removes any instance methods from the MethodGroup, returns
3382 /// false if the resulting set is empty.
3384 public bool RemoveInstanceMethods ()
3386 return RemoveMethods (true);
3390 /// Removes any static methods from the MethodGroup, returns
3391 /// false if the resulting set is empty.
3393 public bool RemoveStaticMethods ()
3395 return RemoveMethods (false);
3400 /// Fully resolved expression that evaluates to a Field
3402 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3403 public readonly FieldInfo FieldInfo;
3404 public Expression InstanceExpression;
3407 public FieldExpr (FieldInfo fi, Location l)
3410 eclass = ExprClass.Variable;
3411 type = fi.FieldType;
3415 override public Expression DoResolve (EmitContext ec)
3417 if (!FieldInfo.IsStatic){
3418 if (InstanceExpression == null){
3419 throw new Exception ("non-static FieldExpr without instance var\n" +
3420 "You have to assign the Instance variable\n" +
3421 "Of the FieldExpr to set this\n");
3424 InstanceExpression = InstanceExpression.Resolve (ec);
3425 if (InstanceExpression == null)
3432 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3434 Expression e = DoResolve (ec);
3439 if (!FieldInfo.IsInitOnly)
3443 // InitOnly fields can only be assigned in constructors
3446 if (ec.IsConstructor)
3449 Report.Error (191, loc,
3450 "Readonly field can not be assigned outside " +
3451 "of constructor or variable initializer");
3456 override public void Emit (EmitContext ec)
3458 ILGenerator ig = ec.ig;
3459 bool is_volatile = false;
3461 if (FieldInfo is FieldBuilder){
3462 Field f = TypeManager.GetField (FieldInfo);
3464 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3467 f.status |= Field.Status.USED;
3471 if (FieldInfo.IsStatic){
3473 ig.Emit (OpCodes.Volatile);
3475 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3477 if (InstanceExpression.Type.IsValueType){
3479 LocalTemporary tempo = null;
3481 if (!(InstanceExpression is IMemoryLocation)){
3482 tempo = new LocalTemporary (
3483 ec, InstanceExpression.Type);
3485 InstanceExpression.Emit (ec);
3489 ml = (IMemoryLocation) InstanceExpression;
3491 ml.AddressOf (ec, AddressOp.Load);
3493 InstanceExpression.Emit (ec);
3496 ig.Emit (OpCodes.Volatile);
3498 ig.Emit (OpCodes.Ldfld, FieldInfo);
3502 public void EmitAssign (EmitContext ec, Expression source)
3504 bool is_static = FieldInfo.IsStatic;
3505 ILGenerator ig = ec.ig;
3508 Expression instance = InstanceExpression;
3510 if (instance.Type.IsValueType){
3511 if (instance is IMemoryLocation){
3512 IMemoryLocation ml = (IMemoryLocation) instance;
3514 ml.AddressOf (ec, AddressOp.Store);
3516 throw new Exception ("The " + instance + " of type " +
3518 " represents a ValueType and does " +
3519 "not implement IMemoryLocation");
3525 if (FieldInfo is FieldBuilder){
3526 Field f = TypeManager.GetField (FieldInfo);
3527 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3528 ig.Emit (OpCodes.Volatile);
3532 ig.Emit (OpCodes.Stsfld, FieldInfo);
3534 ig.Emit (OpCodes.Stfld, FieldInfo);
3536 if (FieldInfo is FieldBuilder){
3537 Field f = TypeManager.GetField (FieldInfo);
3539 f.status |= Field.Status.ASSIGNED;
3543 public void AddressOf (EmitContext ec, AddressOp mode)
3545 ILGenerator ig = ec.ig;
3547 if (FieldInfo is FieldBuilder){
3548 Field f = TypeManager.GetField (FieldInfo);
3549 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3550 ig.Emit (OpCodes.Volatile);
3553 if (FieldInfo is FieldBuilder){
3554 Field f = TypeManager.GetField (FieldInfo);
3556 if ((mode & AddressOp.Store) != 0)
3557 f.status |= Field.Status.ASSIGNED;
3558 if ((mode & AddressOp.Load) != 0)
3559 f.status |= Field.Status.USED;
3563 // Handle initonly fields specially: make a copy and then
3564 // get the address of the copy.
3566 if (FieldInfo.IsInitOnly){
3570 local = ig.DeclareLocal (type);
3571 ig.Emit (OpCodes.Stloc, local);
3572 ig.Emit (OpCodes.Ldloca, local);
3576 if (FieldInfo.IsStatic)
3577 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3579 InstanceExpression.Emit (ec);
3580 ig.Emit (OpCodes.Ldflda, FieldInfo);
3586 /// Expression that evaluates to a Property. The Assign class
3587 /// might set the `Value' expression if we are in an assignment.
3589 /// This is not an LValue because we need to re-write the expression, we
3590 /// can not take data from the stack and store it.
3592 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3593 public readonly PropertyInfo PropertyInfo;
3594 public readonly bool IsStatic;
3596 MethodInfo [] Accessors;
3599 Expression instance_expr;
3601 public PropertyExpr (PropertyInfo pi, Location l)
3604 eclass = ExprClass.PropertyAccess;
3607 Accessors = TypeManager.GetAccessors (pi);
3609 if (Accessors != null)
3610 for (int i = 0; i < Accessors.Length; i++){
3611 if (Accessors [i] != null)
3612 if (Accessors [i].IsStatic)
3616 Accessors = new MethodInfo [2];
3618 type = pi.PropertyType;
3622 // The instance expression associated with this expression
3624 public Expression InstanceExpression {
3626 instance_expr = value;
3630 return instance_expr;
3634 public bool VerifyAssignable ()
3636 if (!PropertyInfo.CanWrite){
3637 Report.Error (200, loc,
3638 "The property `" + PropertyInfo.Name +
3639 "' can not be assigned to, as it has not set accessor");
3646 override public Expression DoResolve (EmitContext ec)
3648 if (!PropertyInfo.CanRead){
3649 Report.Error (154, loc,
3650 "The property `" + PropertyInfo.Name +
3651 "' can not be used in " +
3652 "this context because it lacks a get accessor");
3656 type = PropertyInfo.PropertyType;
3661 override public void Emit (EmitContext ec)
3663 MethodInfo method = Accessors [0];
3666 // Special case: length of single dimension array is turned into ldlen
3668 if (method == TypeManager.int_array_get_length){
3669 Type iet = instance_expr.Type;
3671 if (iet.GetArrayRank () == 1){
3672 instance_expr.Emit (ec);
3673 ec.ig.Emit (OpCodes.Ldlen);
3678 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
3683 // Implements the IAssignMethod interface for assignments
3685 public void EmitAssign (EmitContext ec, Expression source)
3687 Argument arg = new Argument (source, Argument.AType.Expression);
3688 ArrayList args = new ArrayList ();
3691 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3694 override public void EmitStatement (EmitContext ec)
3697 ec.ig.Emit (OpCodes.Pop);
3702 /// Fully resolved expression that evaluates to an Event
3704 public class EventExpr : Expression {
3705 public readonly EventInfo EventInfo;
3707 public Expression InstanceExpression;
3709 public readonly bool IsStatic;
3711 MethodInfo add_accessor, remove_accessor;
3713 public EventExpr (EventInfo ei, Location loc)
3717 eclass = ExprClass.EventAccess;
3719 add_accessor = TypeManager.GetAddMethod (ei);
3720 remove_accessor = TypeManager.GetRemoveMethod (ei);
3722 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3725 if (EventInfo is MyEventBuilder)
3726 type = ((MyEventBuilder) EventInfo).EventType;
3728 type = EventInfo.EventHandlerType;
3731 override public Expression DoResolve (EmitContext ec)
3733 // We are born fully resolved
3737 override public void Emit (EmitContext ec)
3739 throw new Exception ("Should not happen I think");
3742 public void EmitAddOrRemove (EmitContext ec, Expression source)
3744 Expression handler = ((Binary) source).Right;
3746 Argument arg = new Argument (handler, Argument.AType.Expression);
3747 ArrayList args = new ArrayList ();
3751 if (((Binary) source).Oper == Binary.Operator.Addition)
3752 Invocation.EmitCall (
3753 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3755 Invocation.EmitCall (
3756 ec, false, IsStatic, InstanceExpression, remove_accessor, args);