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 (t == TypeManager.bool_type)
317 return new BoolConstant ((bool) v);
318 else if (TypeManager.IsEnumType (t)){
319 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
321 return new EnumConstant (e, t);
323 throw new Exception ("Unknown type for constant (" + t +
328 /// Returns a fully formed expression after a MemberLookup
330 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
333 return new EventExpr ((EventInfo) mi, loc);
334 else if (mi is FieldInfo)
335 return new FieldExpr ((FieldInfo) mi, loc);
336 else if (mi is PropertyInfo)
337 return new PropertyExpr ((PropertyInfo) mi, loc);
338 else if (mi is Type){
339 return new TypeExpr ((System.Type) mi);
346 // FIXME: Probably implement a cache for (t,name,current_access_set)?
348 // This code could use some optimizations, but we need to do some
349 // measurements. For example, we could use a delegate to `flag' when
350 // something can not any longer be a method-group (because it is something
354 // If the return value is an Array, then it is an array of
357 // If the return value is an MemberInfo, it is anything, but a Method
361 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
362 // the arguments here and have MemberLookup return only the methods that
363 // match the argument count/type, unlike we are doing now (we delay this
366 // This is so we can catch correctly attempts to invoke instance methods
367 // from a static body (scan for error 120 in ResolveSimpleName).
370 // FIXME: Potential optimization, have a static ArrayList
373 public static Expression MemberLookup (EmitContext ec, Type t, string name,
374 MemberTypes mt, BindingFlags bf, Location loc)
376 MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
381 int count = mi.Length;
384 return new MethodGroupExpr (mi, loc);
386 if (mi [0] is MethodBase)
387 return new MethodGroupExpr (mi, loc);
389 return ExprClassFromMemberInfo (ec, mi [0], loc);
392 public const MemberTypes AllMemberTypes =
393 MemberTypes.Constructor |
397 MemberTypes.NestedType |
398 MemberTypes.Property;
400 public const BindingFlags AllBindingFlags =
401 BindingFlags.Public |
402 BindingFlags.Static |
403 BindingFlags.Instance;
405 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
407 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
410 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
412 return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
416 /// This is a wrapper for MemberLookup that is not used to "probe", but
417 /// to find a final definition. If the final definition is not found, we
418 /// look for private members and display a useful debugging message if we
421 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
424 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
427 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
428 MemberTypes mt, BindingFlags bf, Location loc)
432 e = MemberLookup (ec, t, name, mt, bf, loc);
437 e = MemberLookup (ec, t, name, AllMemberTypes,
438 AllBindingFlags | BindingFlags.NonPublic, loc);
441 117, loc, "`" + t + "' does not contain a definition " +
442 "for `" + name + "'");
445 122, loc, "`" + t + "." + name +
446 "' is inaccessible due to its protection level");
452 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
454 EventInfo ei = event_expr.EventInfo;
456 return TypeManager.GetPrivateFieldOfEvent (ei);
459 static EmptyExpression MyEmptyExpr;
460 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
462 Type expr_type = expr.Type;
464 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
465 // if we are a method group, emit a warning
470 if (target_type == TypeManager.object_type) {
472 // A pointer type cannot be converted to object
474 if (expr_type.IsPointer)
477 if (expr_type.IsValueType)
478 return new BoxedCast (expr);
479 if (expr_type.IsClass || expr_type.IsInterface)
480 return new EmptyCast (expr, target_type);
481 } else if (expr_type.IsSubclassOf (target_type)) {
482 return new EmptyCast (expr, target_type);
485 // This code is kind of mirrored inside StandardConversionExists
486 // with the small distinction that we only probe there
488 // Always ensure that the code here and there is in sync
490 // from the null type to any reference-type.
491 if (expr is NullLiteral && !target_type.IsValueType)
492 return new EmptyCast (expr, target_type);
494 // from any class-type S to any interface-type T.
495 if (expr_type.IsClass && target_type.IsInterface) {
496 if (TypeManager.ImplementsInterface (expr_type, target_type))
497 return new EmptyCast (expr, target_type);
502 // from any interface type S to interface-type T.
503 if (expr_type.IsInterface && target_type.IsInterface) {
505 if (TypeManager.ImplementsInterface (expr_type, target_type))
506 return new EmptyCast (expr, target_type);
511 // from an array-type S to an array-type of type T
512 if (expr_type.IsArray && target_type.IsArray) {
513 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
515 Type expr_element_type = expr_type.GetElementType ();
517 if (MyEmptyExpr == null)
518 MyEmptyExpr = new EmptyExpression ();
520 MyEmptyExpr.SetType (expr_element_type);
521 Type target_element_type = target_type.GetElementType ();
523 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
524 if (StandardConversionExists (MyEmptyExpr,
525 target_element_type))
526 return new EmptyCast (expr, target_type);
531 // from an array-type to System.Array
532 if (expr_type.IsArray && target_type == TypeManager.array_type)
533 return new EmptyCast (expr, target_type);
535 // from any delegate type to System.Delegate
536 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
537 target_type == TypeManager.delegate_type)
538 return new EmptyCast (expr, target_type);
540 // from any array-type or delegate type into System.ICloneable.
541 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
542 if (target_type == TypeManager.icloneable_type)
543 return new EmptyCast (expr, target_type);
553 /// Handles expressions like this: decimal d; d = 1;
554 /// and changes them into: decimal d; d = new System.Decimal (1);
556 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
558 ArrayList args = new ArrayList ();
560 args.Add (new Argument (expr, Argument.AType.Expression));
562 Expression ne = new New (target.FullName, args,
565 return ne.Resolve (ec);
569 /// Implicit Numeric Conversions.
571 /// expr is the expression to convert, returns a new expression of type
572 /// target_type or null if an implicit conversion is not possible.
574 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
575 Type target_type, Location loc)
577 Type expr_type = expr.Type;
580 // Attempt to do the implicit constant expression conversions
582 if (expr is IntConstant){
585 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
589 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
591 // Try the implicit constant expression conversion
592 // from long to ulong, instead of a nice routine,
595 long v = ((LongConstant) expr).Value;
597 return new ULongConstant ((ulong) v);
601 // If we have an enumeration, extract the underlying type,
602 // use this during the comparission, but wrap around the original
605 Type real_target_type = target_type;
607 if (TypeManager.IsEnumType (real_target_type))
608 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
610 if (expr_type == real_target_type)
611 return new EmptyCast (expr, target_type);
613 if (expr_type == TypeManager.sbyte_type){
615 // From sbyte to short, int, long, float, double.
617 if (real_target_type == TypeManager.int32_type)
618 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
619 if (real_target_type == TypeManager.int64_type)
620 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
621 if (real_target_type == TypeManager.double_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
623 if (real_target_type == TypeManager.float_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
625 if (real_target_type == TypeManager.short_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
627 if (real_target_type == TypeManager.decimal_type)
628 return InternalTypeConstructor (ec, expr, target_type);
629 } else if (expr_type == TypeManager.byte_type){
631 // From byte to short, ushort, int, uint, long, ulong, float, double
633 if ((real_target_type == TypeManager.short_type) ||
634 (real_target_type == TypeManager.ushort_type) ||
635 (real_target_type == TypeManager.int32_type) ||
636 (real_target_type == TypeManager.uint32_type))
637 return new EmptyCast (expr, target_type);
639 if (real_target_type == TypeManager.uint64_type)
640 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
641 if (real_target_type == TypeManager.int64_type)
642 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
643 if (real_target_type == TypeManager.float_type)
644 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
645 if (real_target_type == TypeManager.double_type)
646 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
647 if (real_target_type == TypeManager.decimal_type)
648 return InternalTypeConstructor (ec, expr, target_type);
649 } else if (expr_type == TypeManager.short_type){
651 // From short to int, long, float, double
653 if (real_target_type == TypeManager.int32_type)
654 return new EmptyCast (expr, target_type);
655 if (real_target_type == TypeManager.int64_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
657 if (real_target_type == TypeManager.double_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
659 if (real_target_type == TypeManager.float_type)
660 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
661 if (real_target_type == TypeManager.decimal_type)
662 return InternalTypeConstructor (ec, expr, target_type);
663 } else if (expr_type == TypeManager.ushort_type){
665 // From ushort to int, uint, long, ulong, float, double
667 if (real_target_type == TypeManager.uint32_type)
668 return new EmptyCast (expr, target_type);
670 if (real_target_type == TypeManager.uint64_type)
671 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
672 if (real_target_type == TypeManager.int32_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
674 if (real_target_type == TypeManager.int64_type)
675 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
676 if (real_target_type == TypeManager.double_type)
677 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
678 if (real_target_type == TypeManager.float_type)
679 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
680 if (real_target_type == TypeManager.decimal_type)
681 return InternalTypeConstructor (ec, expr, target_type);
682 } else if (expr_type == TypeManager.int32_type){
684 // From int to long, float, double
686 if (real_target_type == TypeManager.int64_type)
687 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
688 if (real_target_type == TypeManager.double_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
690 if (real_target_type == TypeManager.float_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
692 if (real_target_type == TypeManager.decimal_type)
693 return InternalTypeConstructor (ec, expr, target_type);
694 } else if (expr_type == TypeManager.uint32_type){
696 // From uint to long, ulong, float, double
698 if (real_target_type == TypeManager.int64_type)
699 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
700 if (real_target_type == TypeManager.uint64_type)
701 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
702 if (real_target_type == TypeManager.double_type)
703 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
705 if (real_target_type == TypeManager.float_type)
706 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
708 if (real_target_type == TypeManager.decimal_type)
709 return InternalTypeConstructor (ec, expr, target_type);
710 } else if ((expr_type == TypeManager.uint64_type) ||
711 (expr_type == TypeManager.int64_type)){
713 // From long/ulong to float, double
715 if (real_target_type == TypeManager.double_type)
716 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
718 if (real_target_type == TypeManager.float_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
721 if (real_target_type == TypeManager.decimal_type)
722 return InternalTypeConstructor (ec, expr, target_type);
723 } else if (expr_type == TypeManager.char_type){
725 // From char to ushort, int, uint, long, ulong, float, double
727 if ((real_target_type == TypeManager.ushort_type) ||
728 (real_target_type == TypeManager.int32_type) ||
729 (real_target_type == TypeManager.uint32_type))
730 return new EmptyCast (expr, target_type);
731 if (real_target_type == TypeManager.uint64_type)
732 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
733 if (real_target_type == TypeManager.int64_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
735 if (real_target_type == TypeManager.float_type)
736 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
737 if (real_target_type == TypeManager.double_type)
738 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
739 if (real_target_type == TypeManager.decimal_type)
740 return InternalTypeConstructor (ec, expr, target_type);
741 } else if (expr_type == TypeManager.float_type){
745 if (real_target_type == TypeManager.double_type)
746 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
753 // Tests whether an implicit reference conversion exists between expr_type
756 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
758 Type expr_type = expr.Type;
761 // This is the boxed case.
763 if (target_type == TypeManager.object_type) {
764 if ((expr_type.IsClass) ||
765 (expr_type.IsValueType) ||
766 (expr_type.IsInterface))
769 } else if (expr_type.IsSubclassOf (target_type)) {
773 // Please remember that all code below actually comes
774 // from ImplicitReferenceConversion so make sure code remains in sync
776 // from any class-type S to any interface-type T.
777 if (expr_type.IsClass && target_type.IsInterface) {
778 if (TypeManager.ImplementsInterface (expr_type, target_type))
782 // from any interface type S to interface-type T.
783 if (expr_type.IsInterface && target_type.IsInterface)
784 if (TypeManager.ImplementsInterface (expr_type, target_type))
787 // from an array-type S to an array-type of type T
788 if (expr_type.IsArray && target_type.IsArray) {
789 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
791 Type expr_element_type = expr_type.GetElementType ();
793 if (MyEmptyExpr == null)
794 MyEmptyExpr = new EmptyExpression ();
796 MyEmptyExpr.SetType (expr_element_type);
797 Type target_element_type = target_type.GetElementType ();
799 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
800 if (StandardConversionExists (MyEmptyExpr,
801 target_element_type))
806 // from an array-type to System.Array
807 if (expr_type.IsArray && (target_type == TypeManager.array_type))
810 // from any delegate type to System.Delegate
811 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
812 target_type == TypeManager.delegate_type)
813 if (target_type.IsAssignableFrom (expr_type))
816 // from any array-type or delegate type into System.ICloneable.
817 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
818 if (target_type == TypeManager.icloneable_type)
821 // from the null type to any reference-type.
822 if (expr is NullLiteral && !target_type.IsValueType &&
823 !TypeManager.IsEnumType (target_type))
832 /// Same as StandardConversionExists except that it also looks at
833 /// implicit user defined conversions - needed for overload resolution
835 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
837 if (StandardConversionExists (expr, target_type) == true)
840 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
849 /// Determines if a standard implicit conversion exists from
850 /// expr_type to target_type
852 public static bool StandardConversionExists (Expression expr, Type target_type)
854 Type expr_type = expr.Type;
856 if (expr_type == target_type)
859 // First numeric conversions
861 if (expr_type == TypeManager.sbyte_type){
863 // From sbyte to short, int, long, float, double.
865 if ((target_type == TypeManager.int32_type) ||
866 (target_type == TypeManager.int64_type) ||
867 (target_type == TypeManager.double_type) ||
868 (target_type == TypeManager.float_type) ||
869 (target_type == TypeManager.short_type) ||
870 (target_type == TypeManager.decimal_type))
873 } else if (expr_type == TypeManager.byte_type){
875 // From byte to short, ushort, int, uint, long, ulong, float, double
877 if ((target_type == TypeManager.short_type) ||
878 (target_type == TypeManager.ushort_type) ||
879 (target_type == TypeManager.int32_type) ||
880 (target_type == TypeManager.uint32_type) ||
881 (target_type == TypeManager.uint64_type) ||
882 (target_type == TypeManager.int64_type) ||
883 (target_type == TypeManager.float_type) ||
884 (target_type == TypeManager.double_type) ||
885 (target_type == TypeManager.decimal_type))
888 } else if (expr_type == TypeManager.short_type){
890 // From short to int, long, float, double
892 if ((target_type == TypeManager.int32_type) ||
893 (target_type == TypeManager.int64_type) ||
894 (target_type == TypeManager.double_type) ||
895 (target_type == TypeManager.float_type) ||
896 (target_type == TypeManager.decimal_type))
899 } else if (expr_type == TypeManager.ushort_type){
901 // From ushort to int, uint, long, ulong, float, double
903 if ((target_type == TypeManager.uint32_type) ||
904 (target_type == TypeManager.uint64_type) ||
905 (target_type == TypeManager.int32_type) ||
906 (target_type == TypeManager.int64_type) ||
907 (target_type == TypeManager.double_type) ||
908 (target_type == TypeManager.float_type) ||
909 (target_type == TypeManager.decimal_type))
912 } else if (expr_type == TypeManager.int32_type){
914 // From int to long, float, double
916 if ((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.uint32_type){
924 // From uint to long, ulong, float, double
926 if ((target_type == TypeManager.int64_type) ||
927 (target_type == TypeManager.uint64_type) ||
928 (target_type == TypeManager.double_type) ||
929 (target_type == TypeManager.float_type) ||
930 (target_type == TypeManager.decimal_type))
933 } else if ((expr_type == TypeManager.uint64_type) ||
934 (expr_type == TypeManager.int64_type)) {
936 // From long/ulong to float, double
938 if ((target_type == TypeManager.double_type) ||
939 (target_type == TypeManager.float_type) ||
940 (target_type == TypeManager.decimal_type))
943 } else if (expr_type == TypeManager.char_type){
945 // From char to ushort, int, uint, long, ulong, float, double
947 if ((target_type == TypeManager.ushort_type) ||
948 (target_type == TypeManager.int32_type) ||
949 (target_type == TypeManager.uint32_type) ||
950 (target_type == TypeManager.uint64_type) ||
951 (target_type == TypeManager.int64_type) ||
952 (target_type == TypeManager.float_type) ||
953 (target_type == TypeManager.double_type) ||
954 (target_type == TypeManager.decimal_type))
957 } else if (expr_type == TypeManager.float_type){
961 if (target_type == TypeManager.double_type)
965 if (ImplicitReferenceConversionExists (expr, target_type))
968 if (expr is IntConstant){
969 int value = ((IntConstant) expr).Value;
971 if (target_type == TypeManager.sbyte_type){
972 if (value >= SByte.MinValue && value <= SByte.MaxValue)
974 } else if (target_type == TypeManager.byte_type){
975 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
977 } else if (target_type == TypeManager.short_type){
978 if (value >= Int16.MinValue && value <= Int16.MaxValue)
980 } else if (target_type == TypeManager.ushort_type){
981 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
983 } else if (target_type == TypeManager.uint32_type){
986 } else if (target_type == TypeManager.uint64_type){
988 // we can optimize this case: a positive int32
989 // always fits on a uint64. But we need an opcode
996 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1000 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1002 // Try the implicit constant expression conversion
1003 // from long to ulong, instead of a nice routine,
1004 // we just inline it
1006 long v = ((LongConstant) expr).Value;
1011 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1012 IntLiteral i = (IntLiteral) expr;
1021 // Used internally by FindMostEncompassedType, this is used
1022 // to avoid creating lots of objects in the tight loop inside
1023 // FindMostEncompassedType
1025 static EmptyExpression priv_fmet_param;
1028 /// Finds "most encompassed type" according to the spec (13.4.2)
1029 /// amongst the methods in the MethodGroupExpr
1031 static Type FindMostEncompassedType (ArrayList types)
1035 if (priv_fmet_param == null)
1036 priv_fmet_param = new EmptyExpression ();
1038 foreach (Type t in types){
1039 priv_fmet_param.SetType (t);
1046 if (StandardConversionExists (priv_fmet_param, best))
1054 // Used internally by FindMostEncompassingType, this is used
1055 // to avoid creating lots of objects in the tight loop inside
1056 // FindMostEncompassingType
1058 static EmptyExpression priv_fmee_ret;
1061 /// Finds "most encompassing type" according to the spec (13.4.2)
1062 /// amongst the types in the given set
1064 static Type FindMostEncompassingType (ArrayList types)
1068 if (priv_fmee_ret == null)
1069 priv_fmee_ret = new EmptyExpression ();
1071 foreach (Type t in types){
1072 priv_fmee_ret.SetType (best);
1079 if (StandardConversionExists (priv_fmee_ret, t))
1087 // Used to avoid creating too many objects
1089 static EmptyExpression priv_fms_expr;
1092 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1093 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1094 /// for explicit and implicit conversion operators.
1096 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1097 bool apply_explicit_conv_rules,
1100 ArrayList src_types_set = new ArrayList ();
1102 if (priv_fms_expr == null)
1103 priv_fms_expr = new EmptyExpression ();
1106 // If any operator converts from S then Sx = S
1108 foreach (MethodBase mb in me.Methods){
1109 ParameterData pd = Invocation.GetParameterData (mb);
1110 Type param_type = pd.ParameterType (0);
1112 if (param_type == source_type)
1115 if (apply_explicit_conv_rules) {
1118 // Find the set of applicable user-defined conversion operators, U. This set
1120 // user-defined implicit or explicit conversion operators declared by
1121 // the classes or structs in D that convert from a type encompassing
1122 // or encompassed by S to a type encompassing or encompassed by T
1124 priv_fms_expr.SetType (param_type);
1125 if (StandardConversionExists (priv_fms_expr, source_type))
1126 src_types_set.Add (param_type);
1128 priv_fms_expr.SetType (source_type);
1129 if (StandardConversionExists (priv_fms_expr, param_type))
1130 src_types_set.Add (param_type);
1134 // Only if S is encompassed by param_type
1136 priv_fms_expr.SetType (source_type);
1137 if (StandardConversionExists (priv_fms_expr, param_type))
1138 src_types_set.Add (param_type);
1143 // Explicit Conv rules
1145 if (apply_explicit_conv_rules) {
1146 ArrayList candidate_set = new ArrayList ();
1148 foreach (Type param_type in src_types_set){
1149 priv_fms_expr.SetType (source_type);
1151 if (StandardConversionExists (priv_fms_expr, param_type))
1152 candidate_set.Add (param_type);
1155 if (candidate_set.Count != 0)
1156 return FindMostEncompassedType (candidate_set);
1162 if (apply_explicit_conv_rules)
1163 return FindMostEncompassingType (src_types_set);
1165 return FindMostEncompassedType (src_types_set);
1169 // Useful in avoiding proliferation of objects
1171 static EmptyExpression priv_fmt_expr;
1174 /// Finds the most specific target Tx according to section 13.4.4
1176 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1177 bool apply_explicit_conv_rules,
1180 ArrayList tgt_types_set = new ArrayList ();
1182 if (priv_fmt_expr == null)
1183 priv_fmt_expr = new EmptyExpression ();
1186 // If any operator converts to T then Tx = T
1188 foreach (MethodInfo mi in me.Methods){
1189 Type ret_type = mi.ReturnType;
1191 if (ret_type == target)
1194 if (apply_explicit_conv_rules) {
1197 // Find the set of applicable user-defined conversion operators, U.
1199 // This set consists of the
1200 // user-defined implicit or explicit conversion operators declared by
1201 // the classes or structs in D that convert from a type encompassing
1202 // or encompassed by S to a type encompassing or encompassed by T
1204 priv_fms_expr.SetType (ret_type);
1205 if (StandardConversionExists (priv_fms_expr, target))
1206 tgt_types_set.Add (ret_type);
1208 priv_fms_expr.SetType (target);
1209 if (StandardConversionExists (priv_fms_expr, ret_type))
1210 tgt_types_set.Add (ret_type);
1214 // Only if T is encompassed by param_type
1216 priv_fms_expr.SetType (ret_type);
1217 if (StandardConversionExists (priv_fms_expr, target))
1218 tgt_types_set.Add (ret_type);
1223 // Explicit conv rules
1225 if (apply_explicit_conv_rules) {
1226 ArrayList candidate_set = new ArrayList ();
1228 foreach (Type ret_type in tgt_types_set){
1229 priv_fmt_expr.SetType (ret_type);
1231 if (StandardConversionExists (priv_fmt_expr, target))
1232 candidate_set.Add (ret_type);
1235 if (candidate_set.Count != 0)
1236 return FindMostEncompassingType (candidate_set);
1240 // Okay, final case !
1242 if (apply_explicit_conv_rules)
1243 return FindMostEncompassedType (tgt_types_set);
1245 return FindMostEncompassingType (tgt_types_set);
1249 /// User-defined Implicit conversions
1251 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1252 Type target, Location loc)
1254 return UserDefinedConversion (ec, source, target, loc, false);
1258 /// User-defined Explicit conversions
1260 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1261 Type target, Location loc)
1263 return UserDefinedConversion (ec, source, target, loc, true);
1267 /// Computes the MethodGroup for the user-defined conversion
1268 /// operators from source_type to target_type. `look_for_explicit'
1269 /// controls whether we should also include the list of explicit
1272 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1273 Type source_type, Type target_type,
1274 Location loc, bool look_for_explicit)
1276 Expression mg1 = null, mg2 = null;
1277 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1281 // FIXME : How does the False operator come into the picture ?
1282 // This doesn't look complete and very correct !
1284 if (target_type == TypeManager.bool_type && !look_for_explicit)
1285 op_name = "op_True";
1287 op_name = "op_Implicit";
1289 MethodGroupExpr union3;
1291 mg1 = MethodLookup (ec, source_type, op_name, loc);
1292 if (source_type.BaseType != null)
1293 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1296 union3 = (MethodGroupExpr) mg2;
1297 else if (mg2 == null)
1298 union3 = (MethodGroupExpr) mg1;
1300 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1302 mg1 = MethodLookup (ec, target_type, op_name, loc);
1305 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1307 union3 = (MethodGroupExpr) mg1;
1310 if (target_type.BaseType != null)
1311 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1315 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1317 union3 = (MethodGroupExpr) mg1;
1320 MethodGroupExpr union4 = null;
1322 if (look_for_explicit) {
1323 op_name = "op_Explicit";
1325 mg5 = MemberLookup (ec, source_type, op_name, loc);
1326 if (source_type.BaseType != null)
1327 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1329 mg7 = MemberLookup (ec, target_type, op_name, loc);
1330 if (target_type.BaseType != null)
1331 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1333 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1334 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1336 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1339 return Invocation.MakeUnionSet (union3, union4, loc);
1343 /// User-defined conversions
1345 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1346 Type target, Location loc,
1347 bool look_for_explicit)
1349 MethodGroupExpr union;
1350 Type source_type = source.Type;
1351 MethodBase method = null;
1353 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1357 Type most_specific_source, most_specific_target;
1360 foreach (MethodBase m in union.Methods){
1361 Console.WriteLine ("Name: " + m.Name);
1362 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1366 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1367 if (most_specific_source == null)
1370 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1371 if (most_specific_target == null)
1376 foreach (MethodBase mb in union.Methods){
1377 ParameterData pd = Invocation.GetParameterData (mb);
1378 MethodInfo mi = (MethodInfo) mb;
1380 if (pd.ParameterType (0) == most_specific_source &&
1381 mi.ReturnType == most_specific_target) {
1387 if (method == null || count > 1) {
1388 Report.Error (-11, loc, "Ambiguous user defined conversion");
1393 // This will do the conversion to the best match that we
1394 // found. Now we need to perform an implict standard conversion
1395 // if the best match was not the type that we were requested
1398 if (look_for_explicit)
1399 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1401 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1407 e = new UserCast ((MethodInfo) method, source);
1408 if (e.Type != target){
1409 if (!look_for_explicit)
1410 e = ConvertImplicitStandard (ec, e, target, loc);
1412 e = ConvertExplicitStandard (ec, e, target, loc);
1418 /// Converts implicitly the resolved expression `expr' into the
1419 /// `target_type'. It returns a new expression that can be used
1420 /// in a context that expects a `target_type'.
1422 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1423 Type target_type, Location loc)
1425 Type expr_type = expr.Type;
1428 if (expr_type == target_type)
1431 if (target_type == null)
1432 throw new Exception ("Target type is null");
1434 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1438 e = ImplicitUserConversion (ec, expr, target_type, loc);
1447 /// Attempts to apply the `Standard Implicit
1448 /// Conversion' rules to the expression `expr' into
1449 /// the `target_type'. It returns a new expression
1450 /// that can be used in a context that expects a
1453 /// This is different from `ConvertImplicit' in that the
1454 /// user defined implicit conversions are excluded.
1456 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1457 Type target_type, Location loc)
1459 Type expr_type = expr.Type;
1462 if (expr_type == target_type)
1465 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1469 e = ImplicitReferenceConversion (expr, target_type);
1473 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1474 IntLiteral i = (IntLiteral) expr;
1477 return new EmptyCast (expr, target_type);
1481 if (expr_type.IsPointer){
1482 if (target_type == TypeManager.void_ptr_type)
1483 return new EmptyCast (expr, target_type);
1486 // yep, comparing pointer types cant be done with
1487 // t1 == t2, we have to compare their element types.
1489 if (target_type.IsPointer){
1490 if (target_type.GetElementType()==expr_type.GetElementType())
1495 if (target_type.IsPointer){
1496 if (expr is NullLiteral)
1497 return new EmptyCast (expr, target_type);
1505 /// Attemps to perform an implict constant conversion of the IntConstant
1506 /// into a different data type using casts (See Implicit Constant
1507 /// Expression Conversions)
1509 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1511 int value = ic.Value;
1514 // FIXME: This could return constants instead of EmptyCasts
1516 if (target_type == TypeManager.sbyte_type){
1517 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1518 return new SByteConstant ((sbyte) value);
1519 } else if (target_type == TypeManager.byte_type){
1520 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1521 return new ByteConstant ((byte) value);
1522 } else if (target_type == TypeManager.short_type){
1523 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1524 return new ShortConstant ((short) value);
1525 } else if (target_type == TypeManager.ushort_type){
1526 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1527 return new UShortConstant ((ushort) value);
1528 } else if (target_type == TypeManager.uint32_type){
1530 return new UIntConstant ((uint) value);
1531 } else if (target_type == TypeManager.uint64_type){
1533 // we can optimize this case: a positive int32
1534 // always fits on a uint64. But we need an opcode
1538 return new ULongConstant ((ulong) value);
1541 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1542 return new EnumConstant (ic, target_type);
1547 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1549 string msg = "Cannot convert implicitly from `"+
1550 TypeManager.CSharpName (source) + "' to `" +
1551 TypeManager.CSharpName (target) + "'";
1553 Error (29, loc, msg);
1557 /// Attemptes to implicityly convert `target' into `type', using
1558 /// ConvertImplicit. If there is no implicit conversion, then
1559 /// an error is signaled
1561 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1562 Type target_type, Location loc)
1566 e = ConvertImplicit (ec, source, target_type, loc);
1570 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1572 "Double literal cannot be implicitly converted to " +
1573 "float type, use F suffix to create a float literal");
1576 Error_CannotConvertImplicit (loc, source.Type, target_type);
1582 /// Performs the explicit numeric conversions
1584 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1586 Type expr_type = expr.Type;
1589 // If we have an enumeration, extract the underlying type,
1590 // use this during the comparission, but wrap around the original
1593 Type real_target_type = target_type;
1595 if (TypeManager.IsEnumType (real_target_type))
1596 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1598 if (expr_type == TypeManager.sbyte_type){
1600 // From sbyte to byte, ushort, uint, ulong, char
1602 if (real_target_type == TypeManager.byte_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1604 if (real_target_type == TypeManager.ushort_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1606 if (real_target_type == TypeManager.uint32_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1608 if (real_target_type == TypeManager.uint64_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1610 if (real_target_type == TypeManager.char_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1612 } else if (expr_type == TypeManager.byte_type){
1614 // From byte to sbyte and char
1616 if (real_target_type == TypeManager.sbyte_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1618 if (real_target_type == TypeManager.char_type)
1619 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1620 } else if (expr_type == TypeManager.short_type){
1622 // From short to sbyte, byte, ushort, uint, ulong, char
1624 if (real_target_type == TypeManager.sbyte_type)
1625 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1626 if (real_target_type == TypeManager.byte_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1628 if (real_target_type == TypeManager.ushort_type)
1629 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1630 if (real_target_type == TypeManager.uint32_type)
1631 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1632 if (real_target_type == TypeManager.uint64_type)
1633 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1634 if (real_target_type == TypeManager.char_type)
1635 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1636 } else if (expr_type == TypeManager.ushort_type){
1638 // From ushort to sbyte, byte, short, char
1640 if (real_target_type == TypeManager.sbyte_type)
1641 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1642 if (real_target_type == TypeManager.byte_type)
1643 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1644 if (real_target_type == TypeManager.short_type)
1645 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1646 if (real_target_type == TypeManager.char_type)
1647 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1648 } else if (expr_type == TypeManager.int32_type){
1650 // From int to sbyte, byte, short, ushort, uint, ulong, char
1652 if (real_target_type == TypeManager.sbyte_type)
1653 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1654 if (real_target_type == TypeManager.byte_type)
1655 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1656 if (real_target_type == TypeManager.short_type)
1657 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1658 if (real_target_type == TypeManager.ushort_type)
1659 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1660 if (real_target_type == TypeManager.uint32_type)
1661 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1662 if (real_target_type == TypeManager.uint64_type)
1663 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1664 if (real_target_type == TypeManager.char_type)
1665 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1666 } else if (expr_type == TypeManager.uint32_type){
1668 // From uint to sbyte, byte, short, ushort, int, char
1670 if (real_target_type == TypeManager.sbyte_type)
1671 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1672 if (real_target_type == TypeManager.byte_type)
1673 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1674 if (real_target_type == TypeManager.short_type)
1675 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1676 if (real_target_type == TypeManager.ushort_type)
1677 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1678 if (real_target_type == TypeManager.int32_type)
1679 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1680 if (real_target_type == TypeManager.char_type)
1681 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1682 } else if (expr_type == TypeManager.int64_type){
1684 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1686 if (real_target_type == TypeManager.sbyte_type)
1687 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1688 if (real_target_type == TypeManager.byte_type)
1689 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1690 if (real_target_type == TypeManager.short_type)
1691 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1692 if (real_target_type == TypeManager.ushort_type)
1693 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1694 if (real_target_type == TypeManager.int32_type)
1695 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1696 if (real_target_type == TypeManager.uint32_type)
1697 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1698 if (real_target_type == TypeManager.uint64_type)
1699 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1700 if (real_target_type == TypeManager.char_type)
1701 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1702 } else if (expr_type == TypeManager.uint64_type){
1704 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1706 if (real_target_type == TypeManager.sbyte_type)
1707 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1708 if (real_target_type == TypeManager.byte_type)
1709 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1710 if (real_target_type == TypeManager.short_type)
1711 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1712 if (real_target_type == TypeManager.ushort_type)
1713 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1714 if (real_target_type == TypeManager.int32_type)
1715 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1716 if (real_target_type == TypeManager.uint32_type)
1717 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1718 if (real_target_type == TypeManager.int64_type)
1719 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1720 if (real_target_type == TypeManager.char_type)
1721 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1722 } else if (expr_type == TypeManager.char_type){
1724 // From char to sbyte, byte, short
1726 if (real_target_type == TypeManager.sbyte_type)
1727 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1728 if (real_target_type == TypeManager.byte_type)
1729 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1730 if (real_target_type == TypeManager.short_type)
1731 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1732 } else if (expr_type == TypeManager.float_type){
1734 // From float to sbyte, byte, short,
1735 // ushort, int, uint, long, ulong, char
1738 if (real_target_type == TypeManager.sbyte_type)
1739 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1740 if (real_target_type == TypeManager.byte_type)
1741 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1742 if (real_target_type == TypeManager.short_type)
1743 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1744 if (real_target_type == TypeManager.ushort_type)
1745 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1746 if (real_target_type == TypeManager.int32_type)
1747 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1748 if (real_target_type == TypeManager.uint32_type)
1749 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1750 if (real_target_type == TypeManager.int64_type)
1751 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1752 if (real_target_type == TypeManager.uint64_type)
1753 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1754 if (real_target_type == TypeManager.char_type)
1755 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1756 if (real_target_type == TypeManager.decimal_type)
1757 return InternalTypeConstructor (ec, expr, target_type);
1758 } else if (expr_type == TypeManager.double_type){
1760 // From double to byte, byte, short,
1761 // ushort, int, uint, long, ulong,
1762 // char, float or decimal
1764 if (real_target_type == TypeManager.sbyte_type)
1765 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1766 if (real_target_type == TypeManager.byte_type)
1767 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1768 if (real_target_type == TypeManager.short_type)
1769 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1770 if (real_target_type == TypeManager.ushort_type)
1771 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1772 if (real_target_type == TypeManager.int32_type)
1773 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1774 if (real_target_type == TypeManager.uint32_type)
1775 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1776 if (real_target_type == TypeManager.int64_type)
1777 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1778 if (real_target_type == TypeManager.uint64_type)
1779 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1780 if (real_target_type == TypeManager.char_type)
1781 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1782 if (real_target_type == TypeManager.float_type)
1783 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1784 if (real_target_type == TypeManager.decimal_type)
1785 return InternalTypeConstructor (ec, expr, target_type);
1788 // decimal is taken care of by the op_Explicit methods.
1794 /// Returns whether an explicit reference conversion can be performed
1795 /// from source_type to target_type
1797 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1799 bool target_is_value_type = target_type.IsValueType;
1801 if (source_type == target_type)
1805 // From object to any reference type
1807 if (source_type == TypeManager.object_type && !target_is_value_type)
1811 // From any class S to any class-type T, provided S is a base class of T
1813 if (target_type.IsSubclassOf (source_type))
1817 // From any interface type S to any interface T provided S is not derived from T
1819 if (source_type.IsInterface && target_type.IsInterface){
1820 if (!target_type.IsSubclassOf (source_type))
1825 // From any class type S to any interface T, provided S is not sealed
1826 // and provided S does not implement T.
1828 if (target_type.IsInterface && !source_type.IsSealed &&
1829 !TypeManager.ImplementsInterface (source_type, target_type))
1833 // From any interface-type S to to any class type T, provided T is not
1834 // sealed, or provided T implements S.
1836 if (source_type.IsInterface &&
1837 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1841 // From an array type S with an element type Se to an array type T with an
1842 // element type Te provided all the following are true:
1843 // * S and T differe only in element type, in other words, S and T
1844 // have the same number of dimensions.
1845 // * Both Se and Te are reference types
1846 // * An explicit referenc conversions exist from Se to Te
1848 if (source_type.IsArray && target_type.IsArray) {
1849 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1851 Type source_element_type = source_type.GetElementType ();
1852 Type target_element_type = target_type.GetElementType ();
1854 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1855 if (ExplicitReferenceConversionExists (source_element_type,
1856 target_element_type))
1862 // From System.Array to any array-type
1863 if (source_type == TypeManager.array_type &&
1864 target_type.IsArray){
1869 // From System delegate to any delegate-type
1871 if (source_type == TypeManager.delegate_type &&
1872 target_type.IsSubclassOf (TypeManager.delegate_type))
1876 // From ICloneable to Array or Delegate types
1878 if (source_type == TypeManager.icloneable_type &&
1879 (target_type == TypeManager.array_type ||
1880 target_type == TypeManager.delegate_type))
1887 /// Implements Explicit Reference conversions
1889 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1891 Type source_type = source.Type;
1892 bool target_is_value_type = target_type.IsValueType;
1895 // From object to any reference type
1897 if (source_type == TypeManager.object_type && !target_is_value_type)
1898 return new ClassCast (source, target_type);
1902 // From any class S to any class-type T, provided S is a base class of T
1904 if (target_type.IsSubclassOf (source_type))
1905 return new ClassCast (source, target_type);
1908 // From any interface type S to any interface T provided S is not derived from T
1910 if (source_type.IsInterface && target_type.IsInterface){
1911 if (TypeManager.ImplementsInterface (source_type, target_type))
1914 return new ClassCast (source, target_type);
1918 // From any class type S to any interface T, provides S is not sealed
1919 // and provided S does not implement T.
1921 if (target_type.IsInterface && !source_type.IsSealed) {
1922 if (TypeManager.ImplementsInterface (source_type, target_type))
1925 return new ClassCast (source, target_type);
1930 // From any interface-type S to to any class type T, provided T is not
1931 // sealed, or provided T implements S.
1933 if (source_type.IsInterface) {
1934 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
1935 return new ClassCast (source, target_type);
1940 // From an array type S with an element type Se to an array type T with an
1941 // element type Te provided all the following are true:
1942 // * S and T differe only in element type, in other words, S and T
1943 // have the same number of dimensions.
1944 // * Both Se and Te are reference types
1945 // * An explicit referenc conversions exist from Se to Te
1947 if (source_type.IsArray && target_type.IsArray) {
1948 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1950 Type source_element_type = source_type.GetElementType ();
1951 Type target_element_type = target_type.GetElementType ();
1953 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1954 if (ExplicitReferenceConversionExists (source_element_type,
1955 target_element_type))
1956 return new ClassCast (source, target_type);
1961 // From System.Array to any array-type
1962 if (source_type == TypeManager.array_type &&
1963 target_type.IsArray) {
1964 return new ClassCast (source, target_type);
1968 // From System delegate to any delegate-type
1970 if (source_type == TypeManager.delegate_type &&
1971 target_type.IsSubclassOf (TypeManager.delegate_type))
1972 return new ClassCast (source, target_type);
1975 // From ICloneable to Array or Delegate types
1977 if (source_type == TypeManager.icloneable_type &&
1978 (target_type == TypeManager.array_type ||
1979 target_type == TypeManager.delegate_type))
1980 return new ClassCast (source, target_type);
1986 /// Performs an explicit conversion of the expression `expr' whose
1987 /// type is expr.Type to `target_type'.
1989 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1990 Type target_type, Location loc)
1992 Type expr_type = expr.Type;
1993 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1998 ne = ConvertNumericExplicit (ec, expr, target_type);
2003 // Unboxing conversion.
2005 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2006 return new UnboxCast (expr, target_type);
2011 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2015 // FIXME: Is there any reason we should have EnumConstant
2016 // dealt with here instead of just using always the
2017 // UnderlyingSystemType to wrap the type?
2019 if (expr is EnumConstant)
2020 e = ((EnumConstant) expr).Child;
2022 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2025 Expression t = ConvertImplicit (ec, e, target_type, loc);
2029 return ConvertNumericExplicit (ec, e, target_type);
2032 ne = ConvertReferenceExplicit (expr, target_type);
2037 if (target_type.IsPointer){
2038 if (expr_type.IsPointer)
2039 return new EmptyCast (expr, target_type);
2041 if (expr_type == TypeManager.sbyte_type ||
2042 expr_type == TypeManager.byte_type ||
2043 expr_type == TypeManager.short_type ||
2044 expr_type == TypeManager.ushort_type ||
2045 expr_type == TypeManager.int32_type ||
2046 expr_type == TypeManager.uint32_type ||
2047 expr_type == TypeManager.uint64_type ||
2048 expr_type == TypeManager.int64_type)
2049 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2051 if (expr_type.IsPointer){
2052 if (target_type == TypeManager.sbyte_type ||
2053 target_type == TypeManager.byte_type ||
2054 target_type == TypeManager.short_type ||
2055 target_type == TypeManager.ushort_type ||
2056 target_type == TypeManager.int32_type ||
2057 target_type == TypeManager.uint32_type ||
2058 target_type == TypeManager.uint64_type ||
2059 target_type == TypeManager.int64_type){
2060 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2063 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2068 ce = ConvertNumericExplicit (ec, e, target_type);
2072 // We should always be able to go from an uint32
2073 // implicitly or explicitly to the other integral
2076 throw new Exception ("Internal compiler error");
2081 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2085 Error_CannotConvertType (loc, expr_type, target_type);
2090 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2092 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2093 Type target_type, Location l)
2095 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2100 ne = ConvertNumericExplicit (ec, expr, target_type);
2104 ne = ConvertReferenceExplicit (expr, target_type);
2108 Error_CannotConvertType (l, expr.Type, target_type);
2112 static string ExprClassName (ExprClass c)
2115 case ExprClass.Invalid:
2117 case ExprClass.Value:
2119 case ExprClass.Variable:
2121 case ExprClass.Namespace:
2123 case ExprClass.Type:
2125 case ExprClass.MethodGroup:
2126 return "method group";
2127 case ExprClass.PropertyAccess:
2128 return "property access";
2129 case ExprClass.EventAccess:
2130 return "event access";
2131 case ExprClass.IndexerAccess:
2132 return "indexer access";
2133 case ExprClass.Nothing:
2136 throw new Exception ("Should not happen");
2140 /// Reports that we were expecting `expr' to be of class `expected'
2142 protected void report118 (Location loc, Expression expr, string expected)
2144 string kind = "Unknown";
2147 kind = ExprClassName (expr.eclass);
2149 Error (118, loc, "Expression denotes a `" + kind +
2150 "' where a `" + expected + "' was expected");
2153 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2155 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2156 TypeManager.CSharpName (t));
2159 public static void UnsafeError (Location loc)
2161 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2165 /// Converts the IntConstant, UIntConstant, LongConstant or
2166 /// ULongConstant into the integral target_type. Notice
2167 /// that we do not return an `Expression' we do return
2168 /// a boxed integral type.
2170 /// FIXME: Since I added the new constants, we need to
2171 /// also support conversions from CharConstant, ByteConstant,
2172 /// SByteConstant, UShortConstant, ShortConstant
2174 /// This is used by the switch statement, so the domain
2175 /// of work is restricted to the literals above, and the
2176 /// targets are int32, uint32, char, byte, sbyte, ushort,
2177 /// short, uint64 and int64
2179 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2183 if (c.Type == target_type)
2184 return ((Constant) c).GetValue ();
2187 // Make into one of the literals we handle, we dont really care
2188 // about this value as we will just return a few limited types
2190 if (c is EnumConstant)
2191 c = ((EnumConstant)c).WidenToCompilerConstant ();
2193 if (c is IntConstant){
2194 int v = ((IntConstant) c).Value;
2196 if (target_type == TypeManager.uint32_type){
2199 } else if (target_type == TypeManager.char_type){
2200 if (v >= Char.MinValue && v <= Char.MaxValue)
2202 } else if (target_type == TypeManager.byte_type){
2203 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2205 } else if (target_type == TypeManager.sbyte_type){
2206 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2208 } else if (target_type == TypeManager.short_type){
2209 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2211 } else if (target_type == TypeManager.ushort_type){
2212 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2214 } else if (target_type == TypeManager.int64_type)
2216 else if (target_type == TypeManager.uint64_type){
2222 } else if (c is UIntConstant){
2223 uint v = ((UIntConstant) c).Value;
2225 if (target_type == TypeManager.int32_type){
2226 if (v <= Int32.MaxValue)
2228 } else if (target_type == TypeManager.char_type){
2229 if (v >= Char.MinValue && v <= Char.MaxValue)
2231 } else if (target_type == TypeManager.byte_type){
2232 if (v <= Byte.MaxValue)
2234 } else if (target_type == TypeManager.sbyte_type){
2235 if (v <= SByte.MaxValue)
2237 } else if (target_type == TypeManager.short_type){
2238 if (v <= UInt16.MaxValue)
2240 } else if (target_type == TypeManager.ushort_type){
2241 if (v <= UInt16.MaxValue)
2243 } else if (target_type == TypeManager.int64_type)
2245 else if (target_type == TypeManager.uint64_type)
2248 } else if (c is LongConstant){
2249 long v = ((LongConstant) c).Value;
2251 if (target_type == TypeManager.int32_type){
2252 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2254 } else if (target_type == TypeManager.uint32_type){
2255 if (v >= 0 && v <= UInt32.MaxValue)
2257 } else if (target_type == TypeManager.char_type){
2258 if (v >= Char.MinValue && v <= Char.MaxValue)
2260 } else if (target_type == TypeManager.byte_type){
2261 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2263 } else if (target_type == TypeManager.sbyte_type){
2264 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2266 } else if (target_type == TypeManager.short_type){
2267 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2269 } else if (target_type == TypeManager.ushort_type){
2270 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2272 } else if (target_type == TypeManager.uint64_type){
2277 } else if (c is ULongConstant){
2278 ulong v = ((ULongConstant) c).Value;
2280 if (target_type == TypeManager.int32_type){
2281 if (v <= Int32.MaxValue)
2283 } else if (target_type == TypeManager.uint32_type){
2284 if (v <= UInt32.MaxValue)
2286 } else if (target_type == TypeManager.char_type){
2287 if (v >= Char.MinValue && v <= Char.MaxValue)
2289 } else if (target_type == TypeManager.byte_type){
2290 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2292 } else if (target_type == TypeManager.sbyte_type){
2293 if (v <= (int) SByte.MaxValue)
2295 } else if (target_type == TypeManager.short_type){
2296 if (v <= UInt16.MaxValue)
2298 } else if (target_type == TypeManager.ushort_type){
2299 if (v <= UInt16.MaxValue)
2301 } else if (target_type == TypeManager.int64_type){
2302 if (v <= Int64.MaxValue)
2306 } else if (c is ByteConstant){
2307 byte v = ((ByteConstant) c).Value;
2309 if (target_type == TypeManager.int32_type)
2311 else if (target_type == TypeManager.uint32_type)
2313 else if (target_type == TypeManager.char_type)
2315 else if (target_type == TypeManager.sbyte_type){
2316 if (v <= SByte.MaxValue)
2318 } else if (target_type == TypeManager.short_type)
2320 else if (target_type == TypeManager.ushort_type)
2322 else if (target_type == TypeManager.int64_type)
2324 else if (target_type == TypeManager.uint64_type)
2327 } else if (c is SByteConstant){
2328 sbyte v = ((SByteConstant) c).Value;
2330 if (target_type == TypeManager.int32_type)
2332 else if (target_type == TypeManager.uint32_type){
2335 } else if (target_type == TypeManager.char_type){
2338 } else if (target_type == TypeManager.byte_type){
2341 } else if (target_type == TypeManager.short_type)
2343 else if (target_type == TypeManager.ushort_type){
2346 } else if (target_type == TypeManager.int64_type)
2348 else if (target_type == TypeManager.uint64_type){
2353 } else if (c is ShortConstant){
2354 short v = ((ShortConstant) c).Value;
2356 if (target_type == TypeManager.int32_type){
2358 } else if (target_type == TypeManager.uint32_type){
2361 } else if (target_type == TypeManager.char_type){
2364 } else if (target_type == TypeManager.byte_type){
2365 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2367 } else if (target_type == TypeManager.sbyte_type){
2368 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2370 } else if (target_type == TypeManager.ushort_type){
2373 } else if (target_type == TypeManager.int64_type)
2375 else if (target_type == TypeManager.uint64_type)
2379 } else if (c is UShortConstant){
2380 ushort v = ((UShortConstant) c).Value;
2382 if (target_type == TypeManager.int32_type)
2384 else if (target_type == TypeManager.uint32_type)
2386 else if (target_type == TypeManager.char_type){
2387 if (v >= Char.MinValue && v <= Char.MaxValue)
2389 } else if (target_type == TypeManager.byte_type){
2390 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2392 } else if (target_type == TypeManager.sbyte_type){
2393 if (v <= SByte.MaxValue)
2395 } else if (target_type == TypeManager.short_type){
2396 if (v <= Int16.MaxValue)
2398 } else if (target_type == TypeManager.int64_type)
2400 else if (target_type == TypeManager.uint64_type)
2404 } else if (c is CharConstant){
2405 char v = ((CharConstant) c).Value;
2407 if (target_type == TypeManager.int32_type)
2409 else if (target_type == TypeManager.uint32_type)
2411 else if (target_type == TypeManager.byte_type){
2412 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2414 } else if (target_type == TypeManager.sbyte_type){
2415 if (v <= SByte.MaxValue)
2417 } else if (target_type == TypeManager.short_type){
2418 if (v <= Int16.MaxValue)
2420 } else if (target_type == TypeManager.ushort_type)
2422 else if (target_type == TypeManager.int64_type)
2424 else if (target_type == TypeManager.uint64_type)
2429 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2434 // Load the object from the pointer.
2436 public static void LoadFromPtr (ILGenerator ig, Type t)
2438 if (t == TypeManager.int32_type)
2439 ig.Emit (OpCodes.Ldind_I4);
2440 else if (t == TypeManager.uint32_type)
2441 ig.Emit (OpCodes.Ldind_U4);
2442 else if (t == TypeManager.short_type)
2443 ig.Emit (OpCodes.Ldind_I2);
2444 else if (t == TypeManager.ushort_type)
2445 ig.Emit (OpCodes.Ldind_U2);
2446 else if (t == TypeManager.char_type)
2447 ig.Emit (OpCodes.Ldind_U2);
2448 else if (t == TypeManager.byte_type)
2449 ig.Emit (OpCodes.Ldind_U1);
2450 else if (t == TypeManager.sbyte_type)
2451 ig.Emit (OpCodes.Ldind_I1);
2452 else if (t == TypeManager.uint64_type)
2453 ig.Emit (OpCodes.Ldind_I8);
2454 else if (t == TypeManager.int64_type)
2455 ig.Emit (OpCodes.Ldind_I8);
2456 else if (t == TypeManager.float_type)
2457 ig.Emit (OpCodes.Ldind_R4);
2458 else if (t == TypeManager.double_type)
2459 ig.Emit (OpCodes.Ldind_R8);
2460 else if (t == TypeManager.bool_type)
2461 ig.Emit (OpCodes.Ldind_I1);
2462 else if (t == TypeManager.intptr_type)
2463 ig.Emit (OpCodes.Ldind_I);
2464 else if (TypeManager.IsEnumType (t)) {
2465 if (t == TypeManager.enum_type)
2466 ig.Emit (OpCodes.Ldind_Ref);
2468 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2469 } else if (t.IsValueType)
2470 ig.Emit (OpCodes.Ldobj, t);
2472 ig.Emit (OpCodes.Ldind_Ref);
2476 // The stack contains the pointer and the value of type `type'
2478 public static void StoreFromPtr (ILGenerator ig, Type type)
2481 type = TypeManager.EnumToUnderlying (type);
2482 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2483 ig.Emit (OpCodes.Stind_I4);
2484 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2485 ig.Emit (OpCodes.Stind_I8);
2486 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2487 type == TypeManager.ushort_type)
2488 ig.Emit (OpCodes.Stind_I2);
2489 else if (type == TypeManager.float_type)
2490 ig.Emit (OpCodes.Stind_R4);
2491 else if (type == TypeManager.double_type)
2492 ig.Emit (OpCodes.Stind_R8);
2493 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2494 type == TypeManager.bool_type)
2495 ig.Emit (OpCodes.Stind_I1);
2496 else if (type == TypeManager.intptr_type)
2497 ig.Emit (OpCodes.Stind_I);
2498 else if (type.IsValueType)
2499 ig.Emit (OpCodes.Stobj, type);
2501 ig.Emit (OpCodes.Stind_Ref);
2505 // Returns the size of type `t' if known, otherwise, 0
2507 public static int GetTypeSize (Type t)
2509 if (t == TypeManager.int32_type ||
2510 t == TypeManager.uint32_type ||
2511 t == TypeManager.float_type)
2513 else if (t == TypeManager.int64_type ||
2514 t == TypeManager.uint64_type ||
2515 t == TypeManager.double_type)
2517 else if (t == TypeManager.byte_type ||
2518 t == TypeManager.sbyte_type ||
2519 t == TypeManager.bool_type)
2521 else if (t == TypeManager.short_type ||
2522 t == TypeManager.char_type ||
2523 t == TypeManager.ushort_type)
2530 // Default implementation of IAssignMethod.CacheTemporaries
2532 public void CacheTemporaries (EmitContext ec)
2538 /// This is just a base class for expressions that can
2539 /// appear on statements (invocations, object creation,
2540 /// assignments, post/pre increment and decrement). The idea
2541 /// being that they would support an extra Emition interface that
2542 /// does not leave a result on the stack.
2544 public abstract class ExpressionStatement : Expression {
2547 /// Requests the expression to be emitted in a `statement'
2548 /// context. This means that no new value is left on the
2549 /// stack after invoking this method (constrasted with
2550 /// Emit that will always leave a value on the stack).
2552 public abstract void EmitStatement (EmitContext ec);
2556 /// This kind of cast is used to encapsulate the child
2557 /// whose type is child.Type into an expression that is
2558 /// reported to return "return_type". This is used to encapsulate
2559 /// expressions which have compatible types, but need to be dealt
2560 /// at higher levels with.
2562 /// For example, a "byte" expression could be encapsulated in one
2563 /// of these as an "unsigned int". The type for the expression
2564 /// would be "unsigned int".
2567 public class EmptyCast : Expression {
2568 protected Expression child;
2570 public EmptyCast (Expression child, Type return_type)
2572 eclass = child.eclass;
2577 public override Expression DoResolve (EmitContext ec)
2579 // This should never be invoked, we are born in fully
2580 // initialized state.
2585 public override void Emit (EmitContext ec)
2592 /// This class is used to wrap literals which belong inside Enums
2594 public class EnumConstant : Constant {
2595 public Constant Child;
2597 public EnumConstant (Constant child, Type enum_type)
2599 eclass = child.eclass;
2604 public override Expression DoResolve (EmitContext ec)
2606 // This should never be invoked, we are born in fully
2607 // initialized state.
2612 public override void Emit (EmitContext ec)
2617 public override object GetValue ()
2619 return Child.GetValue ();
2623 // Converts from one of the valid underlying types for an enumeration
2624 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2625 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2627 public Constant WidenToCompilerConstant ()
2629 Type t = TypeManager.EnumToUnderlying (Child.Type);
2630 object v = ((Constant) Child).GetValue ();;
2632 if (t == TypeManager.int32_type)
2633 return new IntConstant ((int) v);
2634 if (t == TypeManager.uint32_type)
2635 return new UIntConstant ((uint) v);
2636 if (t == TypeManager.int64_type)
2637 return new LongConstant ((long) v);
2638 if (t == TypeManager.uint64_type)
2639 return new ULongConstant ((ulong) v);
2640 if (t == TypeManager.short_type)
2641 return new ShortConstant ((short) v);
2642 if (t == TypeManager.ushort_type)
2643 return new UShortConstant ((ushort) v);
2644 if (t == TypeManager.byte_type)
2645 return new ByteConstant ((byte) v);
2646 if (t == TypeManager.sbyte_type)
2647 return new SByteConstant ((sbyte) v);
2649 throw new Exception ("Invalid enumeration underlying type: " + t);
2653 // Extracts the value in the enumeration on its native representation
2655 public object GetPlainValue ()
2657 Type t = TypeManager.EnumToUnderlying (Child.Type);
2658 object v = ((Constant) Child).GetValue ();;
2660 if (t == TypeManager.int32_type)
2662 if (t == TypeManager.uint32_type)
2664 if (t == TypeManager.int64_type)
2666 if (t == TypeManager.uint64_type)
2668 if (t == TypeManager.short_type)
2670 if (t == TypeManager.ushort_type)
2672 if (t == TypeManager.byte_type)
2674 if (t == TypeManager.sbyte_type)
2680 public override string AsString ()
2682 return Child.AsString ();
2685 public override DoubleConstant ConvertToDouble ()
2687 return Child.ConvertToDouble ();
2690 public override FloatConstant ConvertToFloat ()
2692 return Child.ConvertToFloat ();
2695 public override ULongConstant ConvertToULong ()
2697 return Child.ConvertToULong ();
2700 public override LongConstant ConvertToLong ()
2702 return Child.ConvertToLong ();
2705 public override UIntConstant ConvertToUInt ()
2707 return Child.ConvertToUInt ();
2710 public override IntConstant ConvertToInt ()
2712 return Child.ConvertToInt ();
2717 /// This kind of cast is used to encapsulate Value Types in objects.
2719 /// The effect of it is to box the value type emitted by the previous
2722 public class BoxedCast : EmptyCast {
2724 public BoxedCast (Expression expr)
2725 : base (expr, TypeManager.object_type)
2729 public override Expression DoResolve (EmitContext ec)
2731 // This should never be invoked, we are born in fully
2732 // initialized state.
2737 public override void Emit (EmitContext ec)
2741 ec.ig.Emit (OpCodes.Box, child.Type);
2745 public class UnboxCast : EmptyCast {
2746 public UnboxCast (Expression expr, Type return_type)
2747 : base (expr, return_type)
2751 public override Expression DoResolve (EmitContext ec)
2753 // This should never be invoked, we are born in fully
2754 // initialized state.
2759 public override void Emit (EmitContext ec)
2762 ILGenerator ig = ec.ig;
2765 ig.Emit (OpCodes.Unbox, t);
2767 LoadFromPtr (ig, t);
2772 /// This is used to perform explicit numeric conversions.
2774 /// Explicit numeric conversions might trigger exceptions in a checked
2775 /// context, so they should generate the conv.ovf opcodes instead of
2778 public class ConvCast : EmptyCast {
2779 public enum Mode : byte {
2780 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2782 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2783 U2_I1, U2_U1, U2_I2, U2_CH,
2784 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2785 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2786 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2787 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2788 CH_I1, CH_U1, CH_I2,
2789 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2790 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2796 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2797 : base (child, return_type)
2799 checked_state = ec.CheckState;
2803 public override Expression DoResolve (EmitContext ec)
2805 // This should never be invoked, we are born in fully
2806 // initialized state.
2811 public override void Emit (EmitContext ec)
2813 ILGenerator ig = ec.ig;
2819 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2820 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2821 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2822 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2823 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2825 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2826 case Mode.U1_CH: /* nothing */ break;
2828 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2829 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2830 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2831 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2832 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2833 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2835 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2836 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2837 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2838 case Mode.U2_CH: /* nothing */ break;
2840 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2841 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2842 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2843 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2844 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2845 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2846 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2848 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2849 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2850 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2851 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2852 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2853 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2855 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2856 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2857 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2858 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2859 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2860 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2861 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2862 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2864 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2865 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2866 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2867 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2868 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2869 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2870 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2871 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2873 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2874 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2875 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2877 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2878 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2879 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2880 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2881 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2882 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2883 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2884 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2885 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2887 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2888 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2889 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2890 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2891 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2892 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2893 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2894 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2895 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2896 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2900 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2901 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2902 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2903 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2904 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2906 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2907 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2909 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2910 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2911 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2912 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2913 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2914 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2916 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2917 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2918 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2919 case Mode.U2_CH: /* nothing */ break;
2921 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2922 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2923 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2924 case Mode.I4_U4: /* nothing */ break;
2925 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2926 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2927 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2929 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2930 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2931 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2932 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2933 case Mode.U4_I4: /* nothing */ break;
2934 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2936 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2937 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2938 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2939 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2940 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2941 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2942 case Mode.I8_U8: /* nothing */ break;
2943 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2945 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2946 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2947 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2948 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2949 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2950 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2951 case Mode.U8_I8: /* nothing */ break;
2952 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2954 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2955 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2956 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2958 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2959 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2960 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2961 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2962 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2963 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2964 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2965 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2966 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2968 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2969 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2970 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2971 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2972 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2973 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2974 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2975 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2976 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2977 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2983 public class OpcodeCast : EmptyCast {
2987 public OpcodeCast (Expression child, Type return_type, OpCode op)
2988 : base (child, return_type)
2992 second_valid = false;
2995 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2996 : base (child, return_type)
3001 second_valid = true;
3004 public override Expression DoResolve (EmitContext ec)
3006 // This should never be invoked, we are born in fully
3007 // initialized state.
3012 public override void Emit (EmitContext ec)
3023 /// This kind of cast is used to encapsulate a child and cast it
3024 /// to the class requested
3026 public class ClassCast : EmptyCast {
3027 public ClassCast (Expression child, Type return_type)
3028 : base (child, return_type)
3033 public override Expression DoResolve (EmitContext ec)
3035 // This should never be invoked, we are born in fully
3036 // initialized state.
3041 public override void Emit (EmitContext ec)
3045 ec.ig.Emit (OpCodes.Castclass, type);
3051 /// SimpleName expressions are initially formed of a single
3052 /// word and it only happens at the beginning of the expression.
3056 /// The expression will try to be bound to a Field, a Method
3057 /// group or a Property. If those fail we pass the name to our
3058 /// caller and the SimpleName is compounded to perform a type
3059 /// lookup. The idea behind this process is that we want to avoid
3060 /// creating a namespace map from the assemblies, as that requires
3061 /// the GetExportedTypes function to be called and a hashtable to
3062 /// be constructed which reduces startup time. If later we find
3063 /// that this is slower, we should create a `NamespaceExpr' expression
3064 /// that fully participates in the resolution process.
3066 /// For example `System.Console.WriteLine' is decomposed into
3067 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3069 /// The first SimpleName wont produce a match on its own, so it will
3071 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3073 /// System.Console will produce a TypeExpr match.
3075 /// The downside of this is that we might be hitting `LookupType' too many
3076 /// times with this scheme.
3078 public class SimpleName : Expression {
3079 public readonly string Name;
3080 public readonly Location Location;
3082 public SimpleName (string name, Location l)
3088 public static void Error_ObjectRefRequired (Location l, string name)
3092 "An object reference is required " +
3093 "for the non-static field `"+name+"'");
3097 // Checks whether we are trying to access an instance
3098 // property, method or field from a static body.
3100 Expression MemberStaticCheck (Expression e)
3102 if (e is FieldExpr){
3103 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3106 Error_ObjectRefRequired (Location, Name);
3109 } else if (e is MethodGroupExpr){
3110 MethodGroupExpr mg = (MethodGroupExpr) e;
3112 if (!mg.RemoveInstanceMethods ()){
3113 Error_ObjectRefRequired (Location, mg.Methods [0].Name);
3117 } else if (e is PropertyExpr){
3118 if (!((PropertyExpr) e).IsStatic){
3119 Error_ObjectRefRequired (Location, Name);
3122 } else if (e is EventExpr) {
3123 if (!((EventExpr) e).IsStatic) {
3124 Error_ObjectRefRequired (Location, Name);
3132 public override Expression DoResolve (EmitContext ec)
3134 return SimpleNameResolve (ec, null, false);
3137 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3139 return SimpleNameResolve (ec, right_side, false);
3143 public Expression DoResolveAllowStatic (EmitContext ec)
3145 return SimpleNameResolve (ec, null, true);
3149 /// 7.5.2: Simple Names.
3151 /// Local Variables and Parameters are handled at
3152 /// parse time, so they never occur as SimpleNames.
3154 /// The `allow_static' flag is used by MemberAccess only
3155 /// and it is used to inform us that it is ok for us to
3156 /// avoid the static check, because MemberAccess might end
3157 /// up resolving the Name as a Type name and the access as
3158 /// a static type access.
3160 /// ie: Type Type; .... { Type.GetType (""); }
3162 /// Type is both an instance variable and a Type; Type.GetType
3163 /// is the static method not an instance method of type.
3165 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3167 Expression e = null;
3170 // Stage 1: Performed by the parser (binding to locals or parameters).
3172 if (!ec.OnlyLookupTypes){
3173 Block current_block = ec.CurrentBlock;
3174 if (current_block != null && current_block.IsVariableDefined (Name)){
3175 LocalVariableReference var;
3177 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3179 if (right_side != null)
3180 return var.ResolveLValue (ec, right_side);
3182 return var.Resolve (ec);
3186 // Stage 2: Lookup members
3190 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3191 // Hence we have two different cases
3194 DeclSpace lookup_ds = ec.DeclSpace;
3196 if (lookup_ds.TypeBuilder == null)
3199 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
3204 // Classes/structs keep looking, enums break
3206 if (lookup_ds is TypeContainer)
3207 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3210 } while (lookup_ds != null);
3212 if (e == null && ec.ContainerType != null)
3213 e = MemberLookup (ec, ec.ContainerType, Name, Location);
3216 // Continuation of stage 2
3219 // Stage 3: Lookup symbol in the various namespaces.
3221 DeclSpace ds = ec.DeclSpace;
3225 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3226 return new TypeExpr (t);
3229 // Stage 2 part b: Lookup up if we are an alias to a type
3232 // Since we are cheating: we only do the Alias lookup for
3233 // namespaces if the name does not include any dots in it
3236 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3237 // System.Console.WriteLine (Name + " --> " + alias_value);
3238 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3240 return new TypeExpr (t);
3242 // we have alias value, but it isn't Type, so try if it's namespace
3243 return new SimpleName (alias_value, Location);
3246 // No match, maybe our parent can compose us
3247 // into something meaningful.
3252 // Stage 2 continues here.
3257 if (ec.OnlyLookupTypes)
3260 if (e is FieldExpr){
3261 FieldExpr fe = (FieldExpr) e;
3262 FieldInfo fi = fe.FieldInfo;
3264 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3265 UnsafeError (Location);
3269 if (!allow_static && !fi.IsStatic){
3270 Error_ObjectRefRequired (Location, Name);
3274 // If we are not in static code and this
3275 // field is not static, set the instance to `this'.
3278 fe.InstanceExpression = ec.This;
3282 if (fi is FieldBuilder) {
3283 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3286 object o = c.LookupConstantValue (ec);
3287 object real_value = ((Constant)c.Expr).GetValue ();
3288 return Constantify (real_value, fi.FieldType);
3293 Type t = fi.FieldType;
3294 Type decl_type = fi.DeclaringType;
3297 if (fi is FieldBuilder)
3298 o = TypeManager.GetValue ((FieldBuilder) fi);
3300 o = fi.GetValue (fi);
3302 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3303 Expression enum_member = MemberLookup (
3304 ec, decl_type, "value__", MemberTypes.Field,
3305 AllBindingFlags, Location);
3307 Enum en = TypeManager.LookupEnum (decl_type);
3311 c = Constantify (o, en.UnderlyingType);
3313 c = Constantify (o, enum_member.Type);
3315 return new EnumConstant (c, decl_type);
3318 Expression exp = Constantify (o, t);
3324 if (e is PropertyExpr) {
3325 PropertyExpr pe = (PropertyExpr) e;
3331 return MemberStaticCheck (e);
3333 // If we are not in static code and this
3334 // field is not static, set the instance to `this'.
3337 pe.InstanceExpression = ec.This;
3343 if (e is EventExpr) {
3345 // If the event is local to this class, we transform ourselves into
3348 EventExpr ee = (EventExpr) e;
3350 Expression ml = MemberLookup (
3351 ec, ec.ContainerType, ee.EventInfo.Name,
3352 MemberTypes.Event, AllBindingFlags, Location);
3355 MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
3359 // If this happens, then we have an event with its own
3360 // accessors and private field etc so there's no need
3361 // to transform ourselves : we should instead flag an error
3363 Assign.error70 (ee.EventInfo, Location);
3367 ml = ExprClassFromMemberInfo (ec, mi, Location);
3370 Report.Error (-200, Location, "Internal error!!");
3374 Expression instance_expr;
3376 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3379 instance_expr = null;
3381 instance_expr = ec.This;
3382 instance_expr = instance_expr.Resolve (ec);
3385 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3394 return MemberStaticCheck (e);
3401 public override void Emit (EmitContext ec)
3404 // If this is ever reached, then we failed to
3405 // find the name as a namespace
3408 Error (103, Location, "The name `" + Name +
3409 "' does not exist in the class `" +
3410 ec.DeclSpace.Name + "'");
3415 /// Fully resolved expression that evaluates to a type
3417 public class TypeExpr : Expression {
3418 public TypeExpr (Type t)
3421 eclass = ExprClass.Type;
3424 override public Expression DoResolve (EmitContext ec)
3429 override public void Emit (EmitContext ec)
3431 throw new Exception ("Implement me");
3436 /// MethodGroup Expression.
3438 /// This is a fully resolved expression that evaluates to a type
3440 public class MethodGroupExpr : Expression {
3441 public MethodBase [] Methods;
3443 Expression instance_expression = null;
3445 public MethodGroupExpr (MemberInfo [] mi, Location l)
3447 Methods = new MethodBase [mi.Length];
3448 mi.CopyTo (Methods, 0);
3449 eclass = ExprClass.MethodGroup;
3450 type = TypeManager.object_type;
3454 public MethodGroupExpr (ArrayList list, Location l)
3456 Methods = new MethodBase [list.Count];
3459 list.CopyTo (Methods, 0);
3461 foreach (MemberInfo m in list){
3462 if (!(m is MethodBase)){
3463 Console.WriteLine ("Name " + m.Name);
3464 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3470 eclass = ExprClass.MethodGroup;
3471 type = TypeManager.object_type;
3475 // `A method group may have associated an instance expression'
3477 public Expression InstanceExpression {
3479 return instance_expression;
3483 instance_expression = value;
3487 override public Expression DoResolve (EmitContext ec)
3492 public void ReportUsageError ()
3494 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3495 Methods [0].Name + "()' is referenced without parentheses");
3498 override public void Emit (EmitContext ec)
3500 ReportUsageError ();
3503 bool RemoveMethods (bool keep_static)
3505 ArrayList smethods = new ArrayList ();
3507 foreach (MethodBase mb in Methods){
3508 if (mb.IsStatic == keep_static)
3512 if (smethods.Count == 0)
3515 Methods = new MethodBase [smethods.Count];
3516 smethods.CopyTo (Methods, 0);
3522 /// Removes any instance methods from the MethodGroup, returns
3523 /// false if the resulting set is empty.
3525 public bool RemoveInstanceMethods ()
3527 return RemoveMethods (true);
3531 /// Removes any static methods from the MethodGroup, returns
3532 /// false if the resulting set is empty.
3534 public bool RemoveStaticMethods ()
3536 return RemoveMethods (false);
3541 /// Fully resolved expression that evaluates to a Field
3543 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3544 public readonly FieldInfo FieldInfo;
3545 public Expression InstanceExpression;
3548 public FieldExpr (FieldInfo fi, Location l)
3551 eclass = ExprClass.Variable;
3552 type = fi.FieldType;
3556 override public Expression DoResolve (EmitContext ec)
3558 if (!FieldInfo.IsStatic){
3559 if (InstanceExpression == null){
3560 throw new Exception ("non-static FieldExpr without instance var\n" +
3561 "You have to assign the Instance variable\n" +
3562 "Of the FieldExpr to set this\n");
3565 InstanceExpression = InstanceExpression.Resolve (ec);
3566 if (InstanceExpression == null)
3573 void Report_AssignToReadonly (bool is_instance)
3578 msg = "Readonly field can not be assigned outside " +
3579 "of constructor or variable initializer";
3581 msg = "A static readonly field can only be assigned in " +
3582 "a static constructor";
3584 Report.Error (is_instance ? 191 : 198, loc, msg);
3587 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3589 Expression e = DoResolve (ec);
3594 if (!FieldInfo.IsInitOnly)
3598 // InitOnly fields can only be assigned in constructors
3601 if (ec.IsConstructor)
3604 Report_AssignToReadonly (true);
3609 override public void Emit (EmitContext ec)
3611 ILGenerator ig = ec.ig;
3612 bool is_volatile = false;
3614 if (FieldInfo is FieldBuilder){
3615 FieldBase f = TypeManager.GetField (FieldInfo);
3617 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3620 f.status |= Field.Status.USED;
3623 if (FieldInfo.IsStatic){
3625 ig.Emit (OpCodes.Volatile);
3627 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3629 if (InstanceExpression.Type.IsValueType){
3631 LocalTemporary tempo = null;
3633 if (!(InstanceExpression is IMemoryLocation)){
3634 tempo = new LocalTemporary (
3635 ec, InstanceExpression.Type);
3637 InstanceExpression.Emit (ec);
3641 ml = (IMemoryLocation) InstanceExpression;
3643 ml.AddressOf (ec, AddressOp.Load);
3645 InstanceExpression.Emit (ec);
3648 ig.Emit (OpCodes.Volatile);
3650 ig.Emit (OpCodes.Ldfld, FieldInfo);
3654 public void EmitAssign (EmitContext ec, Expression source)
3656 FieldAttributes fa = FieldInfo.Attributes;
3657 bool is_static = (fa & FieldAttributes.Static) != 0;
3658 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3659 ILGenerator ig = ec.ig;
3661 if (is_readonly && !ec.IsConstructor){
3662 Report_AssignToReadonly (!is_static);
3667 Expression instance = InstanceExpression;
3669 if (instance.Type.IsValueType){
3670 if (instance is IMemoryLocation){
3671 IMemoryLocation ml = (IMemoryLocation) instance;
3673 ml.AddressOf (ec, AddressOp.Store);
3675 throw new Exception ("The " + instance + " of type " +
3677 " represents a ValueType and does " +
3678 "not implement IMemoryLocation");
3684 if (FieldInfo is FieldBuilder){
3685 FieldBase f = TypeManager.GetField (FieldInfo);
3687 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3688 ig.Emit (OpCodes.Volatile);
3692 ig.Emit (OpCodes.Stsfld, FieldInfo);
3694 ig.Emit (OpCodes.Stfld, FieldInfo);
3696 if (FieldInfo is FieldBuilder){
3697 FieldBase f = TypeManager.GetField (FieldInfo);
3699 f.status |= Field.Status.ASSIGNED;
3703 public void AddressOf (EmitContext ec, AddressOp mode)
3705 ILGenerator ig = ec.ig;
3707 if (FieldInfo is FieldBuilder){
3708 FieldBase f = TypeManager.GetField (FieldInfo);
3709 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3710 ig.Emit (OpCodes.Volatile);
3713 if (FieldInfo is FieldBuilder){
3714 FieldBase f = TypeManager.GetField (FieldInfo);
3716 if ((mode & AddressOp.Store) != 0)
3717 f.status |= Field.Status.ASSIGNED;
3718 if ((mode & AddressOp.Load) != 0)
3719 f.status |= Field.Status.USED;
3723 // Handle initonly fields specially: make a copy and then
3724 // get the address of the copy.
3726 if (FieldInfo.IsInitOnly){
3730 local = ig.DeclareLocal (type);
3731 ig.Emit (OpCodes.Stloc, local);
3732 ig.Emit (OpCodes.Ldloca, local);
3736 if (FieldInfo.IsStatic)
3737 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3739 if (InstanceExpression is IMemoryLocation)
3740 ((IMemoryLocation)InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
3742 InstanceExpression.Emit (ec);
3743 ig.Emit (OpCodes.Ldflda, FieldInfo);
3749 /// Expression that evaluates to a Property. The Assign class
3750 /// might set the `Value' expression if we are in an assignment.
3752 /// This is not an LValue because we need to re-write the expression, we
3753 /// can not take data from the stack and store it.
3755 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3756 public readonly PropertyInfo PropertyInfo;
3757 public readonly bool IsStatic;
3759 MethodInfo [] Accessors;
3762 Expression instance_expr;
3764 public PropertyExpr (PropertyInfo pi, Location l)
3767 eclass = ExprClass.PropertyAccess;
3770 Accessors = TypeManager.GetAccessors (pi);
3772 if (Accessors != null)
3773 foreach (MethodInfo mi in Accessors){
3779 Accessors = new MethodInfo [2];
3781 type = TypeManager.TypeToCoreType (pi.PropertyType);
3785 // The instance expression associated with this expression
3787 public Expression InstanceExpression {
3789 instance_expr = value;
3793 return instance_expr;
3797 public bool VerifyAssignable ()
3799 if (!PropertyInfo.CanWrite){
3800 Report.Error (200, loc,
3801 "The property `" + PropertyInfo.Name +
3802 "' can not be assigned to, as it has not set accessor");
3809 override public Expression DoResolve (EmitContext ec)
3811 if (!PropertyInfo.CanRead){
3812 Report.Error (154, loc,
3813 "The property `" + PropertyInfo.Name +
3814 "' can not be used in " +
3815 "this context because it lacks a get accessor");
3819 type = PropertyInfo.PropertyType;
3824 override public void Emit (EmitContext ec)
3826 MethodInfo method = Accessors [0];
3829 // Special case: length of single dimension array is turned into ldlen
3831 if (method == TypeManager.int_array_get_length){
3832 Type iet = instance_expr.Type;
3835 // System.Array.Length can be called, but the Type does not
3836 // support invoking GetArrayRank, so test for that case first
3838 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3839 instance_expr.Emit (ec);
3840 ec.ig.Emit (OpCodes.Ldlen);
3845 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
3850 // Implements the IAssignMethod interface for assignments
3852 public void EmitAssign (EmitContext ec, Expression source)
3854 Argument arg = new Argument (source, Argument.AType.Expression);
3855 ArrayList args = new ArrayList ();
3858 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
3861 override public void EmitStatement (EmitContext ec)
3864 ec.ig.Emit (OpCodes.Pop);
3869 /// Fully resolved expression that evaluates to an Event
3871 public class EventExpr : Expression {
3872 public readonly EventInfo EventInfo;
3874 public Expression InstanceExpression;
3876 public readonly bool IsStatic;
3878 MethodInfo add_accessor, remove_accessor;
3880 public EventExpr (EventInfo ei, Location loc)
3884 eclass = ExprClass.EventAccess;
3886 add_accessor = TypeManager.GetAddMethod (ei);
3887 remove_accessor = TypeManager.GetRemoveMethod (ei);
3889 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3892 if (EventInfo is MyEventBuilder)
3893 type = ((MyEventBuilder) EventInfo).EventType;
3895 type = EventInfo.EventHandlerType;
3898 override public Expression DoResolve (EmitContext ec)
3900 // We are born fully resolved
3904 override public void Emit (EmitContext ec)
3906 throw new Exception ("Should not happen I think");
3909 public void EmitAddOrRemove (EmitContext ec, Expression source)
3911 Expression handler = ((Binary) source).Right;
3913 Argument arg = new Argument (handler, Argument.AType.Expression);
3914 ArrayList args = new ArrayList ();
3918 if (((Binary) source).Oper == Binary.Operator.Addition)
3919 Invocation.EmitCall (
3920 ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
3922 Invocation.EmitCall (
3923 ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);