2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 // This is just as a hint to AddressOf of what will be done with the
43 public enum AddressOp {
50 /// This interface is implemented by variables
52 public interface IMemoryLocation {
54 /// The AddressOf method should generate code that loads
55 /// the address of the object and leaves it on the stack.
57 /// The `mode' argument is used to notify the expression
58 /// of whether this will be used to read from the address or
59 /// write to the address.
61 /// This is just a hint that can be used to provide good error
62 /// reporting, and should have no other side effects.
64 void AddressOf (EmitContext ec, AddressOp mode);
68 /// Base class for expressions
70 public abstract class Expression {
71 public ExprClass eclass;
85 /// Utility wrapper routine for Error, just to beautify the code
87 static protected void Error (int error, string s)
89 Report.Error (error, s);
92 static protected void Error (int error, Location loc, string s)
94 Report.Error (error, loc, s);
98 /// Utility wrapper routine for Warning, just to beautify the code
100 static protected void Warning (int warning, string s)
102 Report.Warning (warning, s);
105 static public void Error_CannotConvertType (Location loc, Type source, Type target)
107 Report.Error (30, loc, "Cannot convert type '" +
108 TypeManager.CSharpName (source) + "' to '" +
109 TypeManager.CSharpName (target) + "'");
113 /// Performs semantic analysis on the Expression
117 /// The Resolve method is invoked to perform the semantic analysis
120 /// The return value is an expression (it can be the
121 /// same expression in some cases) or a new
122 /// expression that better represents this node.
124 /// For example, optimizations of Unary (LiteralInt)
125 /// would return a new LiteralInt with a negated
128 /// If there is an error during semantic analysis,
129 /// then an error should be reported (using Report)
130 /// and a null value should be returned.
132 /// There are two side effects expected from calling
133 /// Resolve(): the the field variable "eclass" should
134 /// be set to any value of the enumeration
135 /// `ExprClass' and the type variable should be set
136 /// to a valid type (this is the type of the
139 public abstract Expression DoResolve (EmitContext ec);
141 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
143 return DoResolve (ec);
147 /// Resolves an expression and performs semantic analysis on it.
151 /// Currently Resolve wraps DoResolve to perform sanity
152 /// checking and assertion checking on what we expect from Resolve.
154 public Expression Resolve (EmitContext ec)
156 Expression e = DoResolve (ec);
160 if (e is SimpleName){
161 SimpleName s = (SimpleName) e;
165 "The name `" + s.Name + "' could not be found in `" +
166 ec.DeclSpace.Name + "'");
170 if (e.eclass == ExprClass.Invalid)
171 throw new Exception ("Expression " + e.GetType () +
172 " ExprClass is Invalid after resolve");
174 if (e.eclass != ExprClass.MethodGroup)
176 throw new Exception (
177 "Expression " + e.GetType () +
178 " did not set its type after Resolve\n" +
179 "called from: " + this.GetType ());
186 /// Performs expression resolution and semantic analysis, but
187 /// allows SimpleNames to be returned.
191 /// This is used by MemberAccess to construct long names that can not be
192 /// partially resolved (namespace-qualified names for example).
194 public Expression ResolveWithSimpleName (EmitContext ec)
198 if (this is SimpleName)
199 e = ((SimpleName) this).DoResolveAllowStatic (ec);
207 if (e.eclass == ExprClass.Invalid)
208 throw new Exception ("Expression " + e +
209 " ExprClass is Invalid after resolve");
211 if (e.eclass != ExprClass.MethodGroup)
213 throw new Exception ("Expression " + e +
214 " did not set its type after Resolve");
221 /// Resolves an expression for LValue assignment
225 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
226 /// checking and assertion checking on what we expect from Resolve
228 public Expression ResolveLValue (EmitContext ec, Expression right_side)
230 Expression e = DoResolveLValue (ec, right_side);
233 if (e is SimpleName){
234 SimpleName s = (SimpleName) e;
238 "The name `" + s.Name + "' could not be found in `" +
239 ec.DeclSpace.Name + "'");
243 if (e.eclass == ExprClass.Invalid)
244 throw new Exception ("Expression " + e +
245 " ExprClass is Invalid after resolve");
247 if (e.eclass != ExprClass.MethodGroup)
249 throw new Exception ("Expression " + e +
250 " did not set its type after Resolve");
257 /// Emits the code for the expression
261 /// The Emit method is invoked to generate the code
262 /// for the expression.
264 public abstract void Emit (EmitContext ec);
267 /// Protected constructor. Only derivate types should
268 /// be able to be created
271 protected Expression ()
273 eclass = ExprClass.Invalid;
278 /// Returns a literalized version of a literal FieldInfo
282 /// The possible return values are:
283 /// IntConstant, UIntConstant
284 /// LongLiteral, ULongConstant
285 /// FloatConstant, DoubleConstant
288 /// The value returned is already resolved.
290 public static Constant Constantify (object v, Type t)
292 if (t == TypeManager.int32_type)
293 return new IntConstant ((int) v);
294 else if (t == TypeManager.uint32_type)
295 return new UIntConstant ((uint) v);
296 else if (t == TypeManager.int64_type)
297 return new LongConstant ((long) v);
298 else if (t == TypeManager.uint64_type)
299 return new ULongConstant ((ulong) v);
300 else if (t == TypeManager.float_type)
301 return new FloatConstant ((float) v);
302 else if (t == TypeManager.double_type)
303 return new DoubleConstant ((double) v);
304 else if (t == TypeManager.string_type)
305 return new StringConstant ((string) v);
306 else if (t == TypeManager.short_type)
307 return new ShortConstant ((short)v);
308 else if (t == TypeManager.ushort_type)
309 return new UShortConstant ((ushort)v);
310 else if (t == TypeManager.sbyte_type)
311 return new SByteConstant (((sbyte)v));
312 else if (t == TypeManager.byte_type)
313 return new ByteConstant ((byte)v);
314 else if (t == TypeManager.char_type)
315 return new CharConstant ((char)v);
316 else if (TypeManager.IsEnumType (t)){
317 Expression e = Constantify (v, v.GetType ());
319 return new EnumConstant ((Constant) e, t);
321 throw new Exception ("Unknown type for constant (" + t +
326 /// Returns a fully formed expression after a MemberLookup
328 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
331 return new EventExpr ((EventInfo) mi, loc);
332 else if (mi is FieldInfo)
333 return new FieldExpr ((FieldInfo) mi, loc);
334 else if (mi is PropertyInfo)
335 return new PropertyExpr ((PropertyInfo) mi, loc);
336 else if (mi is Type){
337 return new TypeExpr ((System.Type) mi);
344 // FIXME: Probably implement a cache for (t,name,current_access_set)?
346 // This code could use some optimizations, but we need to do some
347 // measurements. For example, we could use a delegate to `flag' when
348 // something can not any longer be a method-group (because it is something
352 // If the return value is an Array, then it is an array of
355 // If the return value is an MemberInfo, it is anything, but a Method
359 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
360 // the arguments here and have MemberLookup return only the methods that
361 // match the argument count/type, unlike we are doing now (we delay this
364 // This is so we can catch correctly attempts to invoke instance methods
365 // from a static body (scan for error 120 in ResolveSimpleName).
368 // FIXME: Potential optimization, have a static ArrayList
371 public static Expression MemberLookup (EmitContext ec, Type t, string name,
372 MemberTypes mt, BindingFlags bf, Location loc)
374 MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
379 int count = mi.Length;
382 return new MethodGroupExpr (mi, loc);
384 if (mi [0] is MethodBase)
385 return new MethodGroupExpr (mi, loc);
387 return ExprClassFromMemberInfo (ec, mi [0], loc);
390 public const MemberTypes AllMemberTypes =
391 MemberTypes.Constructor |
395 MemberTypes.NestedType |
396 MemberTypes.Property;
398 public const BindingFlags AllBindingFlags =
399 BindingFlags.Public |
400 BindingFlags.Static |
401 BindingFlags.Instance;
403 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
405 return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
408 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
410 return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
414 /// This is a wrapper for MemberLookup that is not used to "probe", but
415 /// to find a final definition. If the final definition is not found, we
416 /// look for private members and display a useful debugging message if we
419 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
424 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
429 e = MemberLookup (ec, t, name, AllMemberTypes,
430 AllBindingFlags | BindingFlags.NonPublic, loc);
433 117, loc, "`" + t + "' does not contain a definition " +
434 "for `" + name + "'");
437 122, loc, "`" + t + "." + name +
438 "' is inaccessible due to its protection level");
444 static EmptyExpression MyEmptyExpr;
445 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
447 Type expr_type = expr.Type;
449 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
450 // if we are a method group, emit a warning
455 if (target_type == TypeManager.object_type) {
457 // A pointer type cannot be converted to object
459 if (expr_type.IsPointer)
462 if (expr_type.IsValueType)
463 return new BoxedCast (expr);
464 if (expr_type.IsClass || expr_type.IsInterface)
465 return new EmptyCast (expr, target_type);
466 } else if (expr_type.IsSubclassOf (target_type)) {
467 return new EmptyCast (expr, target_type);
470 // This code is kind of mirrored inside StandardConversionExists
471 // with the small distinction that we only probe there
473 // Always ensure that the code here and there is in sync
475 // from the null type to any reference-type.
476 if (expr is NullLiteral && !target_type.IsValueType)
477 return new EmptyCast (expr, target_type);
479 // from any class-type S to any interface-type T.
480 if (expr_type.IsClass && target_type.IsInterface) {
481 if (TypeManager.ImplementsInterface (expr_type, target_type))
482 return new EmptyCast (expr, target_type);
487 // from any interface type S to interface-type T.
488 if (expr_type.IsInterface && target_type.IsInterface) {
490 if (TypeManager.ImplementsInterface (expr_type, target_type))
491 return new EmptyCast (expr, target_type);
496 // from an array-type S to an array-type of type T
497 if (expr_type.IsArray && target_type.IsArray) {
498 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
500 Type expr_element_type = expr_type.GetElementType ();
502 if (MyEmptyExpr == null)
503 MyEmptyExpr = new EmptyExpression ();
505 MyEmptyExpr.SetType (expr_element_type);
506 Type target_element_type = target_type.GetElementType ();
508 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
509 if (StandardConversionExists (MyEmptyExpr,
510 target_element_type))
511 return new EmptyCast (expr, target_type);
516 // from an array-type to System.Array
517 if (expr_type.IsArray && target_type == TypeManager.array_type)
518 return new EmptyCast (expr, target_type);
520 // from any delegate type to System.Delegate
521 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
522 target_type == TypeManager.delegate_type)
523 return new EmptyCast (expr, target_type);
525 // from any array-type or delegate type into System.ICloneable.
526 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
527 if (target_type == TypeManager.icloneable_type)
528 return new EmptyCast (expr, target_type);
538 /// Handles expressions like this: decimal d; d = 1;
539 /// and changes them into: decimal d; d = new System.Decimal (1);
541 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
543 ArrayList args = new ArrayList ();
545 args.Add (new Argument (expr, Argument.AType.Expression));
547 Expression ne = new New (target.FullName, args,
550 return ne.Resolve (ec);
554 /// Implicit Numeric Conversions.
556 /// expr is the expression to convert, returns a new expression of type
557 /// target_type or null if an implicit conversion is not possible.
559 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
560 Type target_type, Location loc)
562 Type expr_type = expr.Type;
565 // Attempt to do the implicit constant expression conversions
567 if (expr is IntConstant){
570 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
574 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
576 // Try the implicit constant expression conversion
577 // from long to ulong, instead of a nice routine,
580 long v = ((LongConstant) expr).Value;
582 return new ULongConstant ((ulong) v);
586 // If we have an enumeration, extract the underlying type,
587 // use this during the comparission, but wrap around the original
590 Type real_target_type = target_type;
592 if (TypeManager.IsEnumType (real_target_type))
593 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
595 if (expr_type == real_target_type)
596 return new EmptyCast (expr, target_type);
598 if (expr_type == TypeManager.sbyte_type){
600 // From sbyte to short, int, long, float, double.
602 if (real_target_type == TypeManager.int32_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
604 if (real_target_type == TypeManager.int64_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
606 if (real_target_type == TypeManager.double_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
608 if (real_target_type == TypeManager.float_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
610 if (real_target_type == TypeManager.short_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
612 if (real_target_type == TypeManager.decimal_type)
613 return InternalTypeConstructor (ec, expr, target_type);
614 } else if (expr_type == TypeManager.byte_type){
616 // From byte to short, ushort, int, uint, long, ulong, float, double
618 if ((real_target_type == TypeManager.short_type) ||
619 (real_target_type == TypeManager.ushort_type) ||
620 (real_target_type == TypeManager.int32_type) ||
621 (real_target_type == TypeManager.uint32_type))
622 return new EmptyCast (expr, target_type);
624 if (real_target_type == TypeManager.uint64_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
626 if (real_target_type == TypeManager.int64_type)
627 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
628 if (real_target_type == TypeManager.float_type)
629 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
630 if (real_target_type == TypeManager.double_type)
631 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
632 if (real_target_type == TypeManager.decimal_type)
633 return InternalTypeConstructor (ec, expr, target_type);
634 } else if (expr_type == TypeManager.short_type){
636 // From short to int, long, float, double
638 if (real_target_type == TypeManager.int32_type)
639 return new EmptyCast (expr, target_type);
640 if (real_target_type == TypeManager.int64_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
642 if (real_target_type == TypeManager.double_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
644 if (real_target_type == TypeManager.float_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
646 if (real_target_type == TypeManager.decimal_type)
647 return InternalTypeConstructor (ec, expr, target_type);
648 } else if (expr_type == TypeManager.ushort_type){
650 // From ushort to int, uint, long, ulong, float, double
652 if (real_target_type == TypeManager.uint32_type)
653 return new EmptyCast (expr, target_type);
655 if (real_target_type == TypeManager.uint64_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
657 if (real_target_type == TypeManager.int32_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
659 if (real_target_type == TypeManager.int64_type)
660 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
661 if (real_target_type == TypeManager.double_type)
662 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
663 if (real_target_type == TypeManager.float_type)
664 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
665 if (real_target_type == TypeManager.decimal_type)
666 return InternalTypeConstructor (ec, expr, target_type);
667 } else if (expr_type == TypeManager.int32_type){
669 // From int to long, float, double
671 if (real_target_type == TypeManager.int64_type)
672 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
673 if (real_target_type == TypeManager.double_type)
674 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
675 if (real_target_type == TypeManager.float_type)
676 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
677 if (real_target_type == TypeManager.decimal_type)
678 return InternalTypeConstructor (ec, expr, target_type);
679 } else if (expr_type == TypeManager.uint32_type){
681 // From uint to long, ulong, float, double
683 if (real_target_type == TypeManager.int64_type)
684 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
685 if (real_target_type == TypeManager.uint64_type)
686 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
687 if (real_target_type == TypeManager.double_type)
688 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
690 if (real_target_type == TypeManager.float_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
693 if (real_target_type == TypeManager.decimal_type)
694 return InternalTypeConstructor (ec, expr, target_type);
695 } else if ((expr_type == TypeManager.uint64_type) ||
696 (expr_type == TypeManager.int64_type)){
698 // From long/ulong to float, double
700 if (real_target_type == TypeManager.double_type)
701 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
703 if (real_target_type == TypeManager.float_type)
704 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
706 if (real_target_type == TypeManager.decimal_type)
707 return InternalTypeConstructor (ec, expr, target_type);
708 } else if (expr_type == TypeManager.char_type){
710 // From char to ushort, int, uint, long, ulong, float, double
712 if ((real_target_type == TypeManager.ushort_type) ||
713 (real_target_type == TypeManager.int32_type) ||
714 (real_target_type == TypeManager.uint32_type))
715 return new EmptyCast (expr, target_type);
716 if (real_target_type == TypeManager.uint64_type)
717 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
718 if (real_target_type == TypeManager.int64_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
720 if (real_target_type == TypeManager.float_type)
721 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
722 if (real_target_type == TypeManager.double_type)
723 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
724 if (real_target_type == TypeManager.decimal_type)
725 return InternalTypeConstructor (ec, expr, target_type);
726 } else if (expr_type == TypeManager.float_type){
730 if (real_target_type == TypeManager.double_type)
731 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
738 /// Determines if a standard implicit conversion exists from
739 /// expr_type to target_type
741 public static bool StandardConversionExists (Expression expr, Type target_type)
743 Type expr_type = expr.Type;
745 if (expr_type == target_type)
748 // First numeric conversions
750 if (expr_type == TypeManager.sbyte_type){
752 // From sbyte to short, int, long, float, double.
754 if ((target_type == TypeManager.int32_type) ||
755 (target_type == TypeManager.int64_type) ||
756 (target_type == TypeManager.double_type) ||
757 (target_type == TypeManager.float_type) ||
758 (target_type == TypeManager.short_type) ||
759 (target_type == TypeManager.decimal_type))
762 } else if (expr_type == TypeManager.byte_type){
764 // From byte to short, ushort, int, uint, long, ulong, float, double
766 if ((target_type == TypeManager.short_type) ||
767 (target_type == TypeManager.ushort_type) ||
768 (target_type == TypeManager.int32_type) ||
769 (target_type == TypeManager.uint32_type) ||
770 (target_type == TypeManager.uint64_type) ||
771 (target_type == TypeManager.int64_type) ||
772 (target_type == TypeManager.float_type) ||
773 (target_type == TypeManager.double_type) ||
774 (target_type == TypeManager.decimal_type))
777 } else if (expr_type == TypeManager.short_type){
779 // From short to int, long, float, double
781 if ((target_type == TypeManager.int32_type) ||
782 (target_type == TypeManager.int64_type) ||
783 (target_type == TypeManager.double_type) ||
784 (target_type == TypeManager.float_type) ||
785 (target_type == TypeManager.decimal_type))
788 } else if (expr_type == TypeManager.ushort_type){
790 // From ushort to int, uint, long, ulong, float, double
792 if ((target_type == TypeManager.uint32_type) ||
793 (target_type == TypeManager.uint64_type) ||
794 (target_type == TypeManager.int32_type) ||
795 (target_type == TypeManager.int64_type) ||
796 (target_type == TypeManager.double_type) ||
797 (target_type == TypeManager.float_type) ||
798 (target_type == TypeManager.decimal_type))
801 } else if (expr_type == TypeManager.int32_type){
803 // From int to long, float, double
805 if ((target_type == TypeManager.int64_type) ||
806 (target_type == TypeManager.double_type) ||
807 (target_type == TypeManager.float_type) ||
808 (target_type == TypeManager.decimal_type))
811 } else if (expr_type == TypeManager.uint32_type){
813 // From uint to long, ulong, float, double
815 if ((target_type == TypeManager.int64_type) ||
816 (target_type == TypeManager.uint64_type) ||
817 (target_type == TypeManager.double_type) ||
818 (target_type == TypeManager.float_type) ||
819 (target_type == TypeManager.decimal_type))
822 } else if ((expr_type == TypeManager.uint64_type) ||
823 (expr_type == TypeManager.int64_type)) {
825 // From long/ulong to float, double
827 if ((target_type == TypeManager.double_type) ||
828 (target_type == TypeManager.float_type) ||
829 (target_type == TypeManager.decimal_type))
832 } else if (expr_type == TypeManager.char_type){
834 // From char to ushort, int, uint, long, ulong, float, double
836 if ((target_type == TypeManager.ushort_type) ||
837 (target_type == TypeManager.int32_type) ||
838 (target_type == TypeManager.uint32_type) ||
839 (target_type == TypeManager.uint64_type) ||
840 (target_type == TypeManager.int64_type) ||
841 (target_type == TypeManager.float_type) ||
842 (target_type == TypeManager.double_type) ||
843 (target_type == TypeManager.decimal_type))
846 } else if (expr_type == TypeManager.float_type){
850 if (target_type == TypeManager.double_type)
854 // Next reference conversions
856 if (target_type == TypeManager.object_type) {
857 if ((expr_type.IsClass) ||
858 (expr_type.IsValueType))
861 } else if (expr_type.IsSubclassOf (target_type)) {
865 // Please remember that all code below actually comes
866 // from ImplicitReferenceConversion so make sure code remains in sync
868 // from any class-type S to any interface-type T.
869 if (expr_type.IsClass && target_type.IsInterface) {
870 if (TypeManager.ImplementsInterface (expr_type, target_type))
874 // from any interface type S to interface-type T.
875 // FIXME : Is it right to use IsAssignableFrom ?
876 if (expr_type.IsInterface && target_type.IsInterface)
877 if (target_type.IsAssignableFrom (expr_type))
880 // from an array-type S to an array-type of type T
881 if (expr_type.IsArray && target_type.IsArray) {
882 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
884 Type expr_element_type = expr_type.GetElementType ();
886 if (MyEmptyExpr == null)
887 MyEmptyExpr = new EmptyExpression ();
889 MyEmptyExpr.SetType (expr_element_type);
890 Type target_element_type = target_type.GetElementType ();
892 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
893 if (StandardConversionExists (MyEmptyExpr,
894 target_element_type))
899 // from an array-type to System.Array
900 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
903 // from any delegate type to System.Delegate
904 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
905 target_type == TypeManager.delegate_type)
906 if (target_type.IsAssignableFrom (expr_type))
909 // from any array-type or delegate type into System.ICloneable.
910 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
911 if (target_type == TypeManager.icloneable_type)
914 // from the null type to any reference-type.
915 if (expr is NullLiteral && !target_type.IsValueType)
920 if (expr is IntConstant){
921 int value = ((IntConstant) expr).Value;
923 if (target_type == TypeManager.sbyte_type){
924 if (value >= SByte.MinValue && value <= SByte.MaxValue)
926 } else if (target_type == TypeManager.byte_type){
927 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
929 } else if (target_type == TypeManager.short_type){
930 if (value >= Int16.MinValue && value <= Int16.MaxValue)
932 } else if (target_type == TypeManager.ushort_type){
933 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
935 } else if (target_type == TypeManager.uint32_type){
938 } else if (target_type == TypeManager.uint64_type){
940 // we can optimize this case: a positive int32
941 // always fits on a uint64. But we need an opcode
948 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
952 if (expr is LongConstant && target_type == TypeManager.uint64_type){
954 // Try the implicit constant expression conversion
955 // from long to ulong, instead of a nice routine,
958 long v = ((LongConstant) expr).Value;
963 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
964 IntLiteral i = (IntLiteral) expr;
973 // Used internally by FindMostEncompassedType, this is used
974 // to avoid creating lots of objects in the tight loop inside
975 // FindMostEncompassedType
977 static EmptyExpression priv_fmet_param;
980 /// Finds "most encompassed type" according to the spec (13.4.2)
981 /// amongst the methods in the MethodGroupExpr
983 static Type FindMostEncompassedType (ArrayList types)
987 if (priv_fmet_param == null)
988 priv_fmet_param = new EmptyExpression ();
990 for (int i = 0; i < types.Count; ++i) {
991 Type t = (Type) types [i];
992 priv_fmet_param.SetType (t);
999 if (StandardConversionExists (priv_fmet_param, best))
1007 // Used internally by FindMostEncompassingType, this is used
1008 // to avoid creating lots of objects in the tight loop inside
1009 // FindMostEncompassingType
1011 static EmptyExpression priv_fmee_ret;
1014 /// Finds "most encompassing type" according to the spec (13.4.2)
1015 /// amongst the types in the given set
1017 static Type FindMostEncompassingType (ArrayList types)
1021 if (priv_fmee_ret == null)
1022 priv_fmee_ret = new EmptyExpression ();
1024 for (int i = 0; i < types.Count; ++i ) {
1026 Type t = (Type) types [i];
1027 priv_fmee_ret.SetType (best);
1034 if (StandardConversionExists (priv_fmee_ret, t))
1042 // Used to avoid creating too many objects
1044 static EmptyExpression priv_fms_expr;
1047 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1048 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1049 /// for explicit and implicit conversion operators.
1051 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1052 bool apply_explicit_conv_rules,
1055 ArrayList src_types_set = new ArrayList ();
1057 if (priv_fms_expr == null)
1058 priv_fms_expr = new EmptyExpression ();
1061 // If any operator converts from S then Sx = S
1063 for (int i = me.Methods.Length; i > 0; ) {
1066 MethodBase mb = me.Methods [i];
1067 ParameterData pd = Invocation.GetParameterData (mb);
1068 Type param_type = pd.ParameterType (0);
1070 if (param_type == source_type)
1073 src_types_set.Add (param_type);
1077 // Explicit Conv rules
1079 if (apply_explicit_conv_rules) {
1081 ArrayList candidate_set = new ArrayList ();
1083 for (int i = 0; i < src_types_set.Count; ++i) {
1084 Type param_type = (Type) src_types_set [i];
1086 priv_fms_expr.SetType (source_type);
1088 if (StandardConversionExists (priv_fms_expr, param_type))
1089 candidate_set.Add (param_type);
1092 if (candidate_set.Count != 0)
1093 return FindMostEncompassedType (candidate_set);
1099 if (apply_explicit_conv_rules)
1100 return FindMostEncompassingType (src_types_set);
1102 return FindMostEncompassedType (src_types_set);
1106 // Useful in avoiding proliferation of objects
1108 static EmptyExpression priv_fmt_expr;
1111 /// Finds the most specific target Tx according to section 13.4.4
1113 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1114 bool apply_explicit_conv_rules,
1117 ArrayList tgt_types_set = new ArrayList ();
1119 if (priv_fmt_expr == null)
1120 priv_fmt_expr = new EmptyExpression ();
1123 // If any operator converts to T then Tx = T
1125 for (int i = me.Methods.Length; i > 0; ) {
1128 MethodInfo mi = (MethodInfo) me.Methods [i];
1129 Type ret_type = mi.ReturnType;
1131 if (ret_type == target)
1134 tgt_types_set.Add (ret_type);
1138 // Explicit conv rules
1140 if (apply_explicit_conv_rules) {
1142 ArrayList candidate_set = new ArrayList ();
1144 for (int i = 0; i < tgt_types_set.Count; ++i) {
1145 Type ret_type = (Type) tgt_types_set [i];
1147 priv_fmt_expr.SetType (ret_type);
1149 if (StandardConversionExists (priv_fmt_expr, target))
1150 candidate_set.Add (ret_type);
1153 if (candidate_set.Count != 0)
1154 return FindMostEncompassingType (candidate_set);
1158 // Okay, final case !
1160 if (apply_explicit_conv_rules)
1161 return FindMostEncompassedType (tgt_types_set);
1163 return FindMostEncompassingType (tgt_types_set);
1167 /// User-defined Implicit conversions
1169 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1170 Type target, Location loc)
1172 return UserDefinedConversion (ec, source, target, loc, false);
1176 /// User-defined Explicit conversions
1178 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1179 Type target, Location loc)
1181 return UserDefinedConversion (ec, source, target, loc, true);
1185 /// Computes the MethodGroup for the user-defined conversion
1186 /// operators from source_type to target_type. `look_for_explicit'
1187 /// controls whether we should also include the list of explicit
1190 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1191 Type source_type, Type target_type,
1192 Location loc, bool look_for_explicit)
1194 Expression mg1 = null, mg2 = null;
1195 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1199 // FIXME : How does the False operator come into the picture ?
1200 // This doesn't look complete and very correct !
1202 if (target_type == TypeManager.bool_type && !look_for_explicit)
1203 op_name = "op_True";
1205 op_name = "op_Implicit";
1207 MethodGroupExpr union3;
1209 mg1 = MethodLookup (ec, source_type, op_name, loc);
1210 if (source_type.BaseType != null)
1211 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1214 union3 = (MethodGroupExpr) mg2;
1215 else if (mg2 == null)
1216 union3 = (MethodGroupExpr) mg1;
1218 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1220 mg1 = MethodLookup (ec, target_type, op_name, loc);
1223 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1225 union3 = (MethodGroupExpr) mg1;
1228 if (target_type.BaseType != null)
1229 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1233 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1235 union3 = (MethodGroupExpr) mg1;
1238 MethodGroupExpr union4 = null;
1240 if (look_for_explicit) {
1241 op_name = "op_Explicit";
1243 mg5 = MemberLookup (ec, source_type, op_name, loc);
1244 if (source_type.BaseType != null)
1245 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1247 mg7 = MemberLookup (ec, target_type, op_name, loc);
1248 if (target_type.BaseType != null)
1249 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1251 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1252 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1254 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1257 return Invocation.MakeUnionSet (union3, union4, loc);
1261 /// User-defined conversions
1263 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1264 Type target, Location loc,
1265 bool look_for_explicit)
1267 MethodGroupExpr union;
1268 Type source_type = source.Type;
1269 MethodBase method = null;
1271 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1275 Type most_specific_source, most_specific_target;
1277 most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1278 if (most_specific_source == null)
1281 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1282 if (most_specific_target == null)
1287 for (int i = union.Methods.Length; i > 0;) {
1290 MethodBase mb = union.Methods [i];
1291 ParameterData pd = Invocation.GetParameterData (mb);
1292 MethodInfo mi = (MethodInfo) union.Methods [i];
1294 if (pd.ParameterType (0) == most_specific_source &&
1295 mi.ReturnType == most_specific_target) {
1301 if (method == null || count > 1) {
1302 Report.Error (-11, loc, "Ambiguous user defined conversion");
1307 // This will do the conversion to the best match that we
1308 // found. Now we need to perform an implict standard conversion
1309 // if the best match was not the type that we were requested
1312 if (look_for_explicit)
1313 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1315 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1321 e = new UserCast ((MethodInfo) method, source);
1322 if (e.Type != target){
1323 if (!look_for_explicit)
1324 e = ConvertImplicitStandard (ec, e, target, loc);
1326 e = ConvertExplicitStandard (ec, e, target, loc);
1332 /// Converts implicitly the resolved expression `expr' into the
1333 /// `target_type'. It returns a new expression that can be used
1334 /// in a context that expects a `target_type'.
1336 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1337 Type target_type, Location loc)
1339 Type expr_type = expr.Type;
1342 if (expr_type == target_type)
1345 if (target_type == null)
1346 throw new Exception ("Target type is null");
1348 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1352 e = ImplicitUserConversion (ec, expr, target_type, loc);
1361 /// Attempts to apply the `Standard Implicit
1362 /// Conversion' rules to the expression `expr' into
1363 /// the `target_type'. It returns a new expression
1364 /// that can be used in a context that expects a
1367 /// This is different from `ConvertImplicit' in that the
1368 /// user defined implicit conversions are excluded.
1370 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1371 Type target_type, Location loc)
1373 Type expr_type = expr.Type;
1376 if (expr_type == target_type)
1379 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1383 e = ImplicitReferenceConversion (expr, target_type);
1387 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1388 IntLiteral i = (IntLiteral) expr;
1391 return new EmptyCast (expr, target_type);
1395 if (expr_type.IsPointer){
1396 if (target_type == TypeManager.void_ptr_type)
1397 return new EmptyCast (expr, target_type);
1400 // yep, comparing pointer types cant be done with
1401 // t1 == t2, we have to compare their element types.
1403 if (target_type.IsPointer){
1404 if (target_type.GetElementType()==expr_type.GetElementType())
1409 if (target_type.IsPointer){
1410 if (expr is NullLiteral)
1411 return new EmptyCast (expr, target_type);
1419 /// Attemps to perform an implict constant conversion of the IntConstant
1420 /// into a different data type using casts (See Implicit Constant
1421 /// Expression Conversions)
1423 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1425 int value = ic.Value;
1428 // FIXME: This could return constants instead of EmptyCasts
1430 if (target_type == TypeManager.sbyte_type){
1431 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1432 return new SByteConstant ((sbyte) value);
1433 } else if (target_type == TypeManager.byte_type){
1434 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1435 return new ByteConstant ((byte) value);
1436 } else if (target_type == TypeManager.short_type){
1437 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1438 return new ShortConstant ((short) value);
1439 } else if (target_type == TypeManager.ushort_type){
1440 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1441 return new UShortConstant ((ushort) value);
1442 } else if (target_type == TypeManager.uint32_type){
1444 return new UIntConstant ((uint) value);
1445 } else if (target_type == TypeManager.uint64_type){
1447 // we can optimize this case: a positive int32
1448 // always fits on a uint64. But we need an opcode
1452 return new ULongConstant ((ulong) value);
1455 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1456 return new EnumConstant (ic, target_type);
1461 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1463 string msg = "Cannot convert implicitly from `"+
1464 TypeManager.CSharpName (source) + "' to `" +
1465 TypeManager.CSharpName (target) + "'";
1467 Error (29, loc, msg);
1471 /// Attemptes to implicityly convert `target' into `type', using
1472 /// ConvertImplicit. If there is no implicit conversion, then
1473 /// an error is signaled
1475 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1476 Type target_type, Location loc)
1480 e = ConvertImplicit (ec, source, target_type, loc);
1484 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1486 "Double literal cannot be implicitly converted to " +
1487 "float type, use F suffix to create a float literal");
1490 Error_CannotConvertImplicit (loc, source.Type, target_type);
1496 /// Performs the explicit numeric conversions
1498 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1501 Type expr_type = expr.Type;
1504 // If we have an enumeration, extract the underlying type,
1505 // use this during the comparission, but wrap around the original
1508 Type real_target_type = target_type;
1510 if (TypeManager.IsEnumType (real_target_type))
1511 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1513 if (expr_type == TypeManager.sbyte_type){
1515 // From sbyte to byte, ushort, uint, ulong, char
1517 if (real_target_type == TypeManager.byte_type)
1518 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1519 if (real_target_type == TypeManager.ushort_type)
1520 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1521 if (real_target_type == TypeManager.uint32_type)
1522 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1523 if (real_target_type == TypeManager.uint64_type)
1524 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1525 if (real_target_type == TypeManager.char_type)
1526 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1527 } else if (expr_type == TypeManager.byte_type){
1529 // From byte to sbyte and char
1531 if (real_target_type == TypeManager.sbyte_type)
1532 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1533 if (real_target_type == TypeManager.char_type)
1534 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1535 } else if (expr_type == TypeManager.short_type){
1537 // From short to sbyte, byte, ushort, uint, ulong, char
1539 if (real_target_type == TypeManager.sbyte_type)
1540 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1541 if (real_target_type == TypeManager.byte_type)
1542 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1543 if (real_target_type == TypeManager.ushort_type)
1544 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1545 if (real_target_type == TypeManager.uint32_type)
1546 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1547 if (real_target_type == TypeManager.uint64_type)
1548 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1549 if (real_target_type == TypeManager.char_type)
1550 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1551 } else if (expr_type == TypeManager.ushort_type){
1553 // From ushort to sbyte, byte, short, char
1555 if (real_target_type == TypeManager.sbyte_type)
1556 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1557 if (real_target_type == TypeManager.byte_type)
1558 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1559 if (real_target_type == TypeManager.short_type)
1560 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1561 if (real_target_type == TypeManager.char_type)
1562 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1563 } else if (expr_type == TypeManager.int32_type){
1565 // From int to sbyte, byte, short, ushort, uint, ulong, char
1567 if (real_target_type == TypeManager.sbyte_type)
1568 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1569 if (real_target_type == TypeManager.byte_type)
1570 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1571 if (real_target_type == TypeManager.short_type)
1572 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1573 if (real_target_type == TypeManager.ushort_type)
1574 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1575 if (real_target_type == TypeManager.uint32_type)
1576 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1577 if (real_target_type == TypeManager.uint64_type)
1578 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1579 if (real_target_type == TypeManager.char_type)
1580 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1581 } else if (expr_type == TypeManager.uint32_type){
1583 // From uint to sbyte, byte, short, ushort, int, char
1585 if (real_target_type == TypeManager.sbyte_type)
1586 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1587 if (real_target_type == TypeManager.byte_type)
1588 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1589 if (real_target_type == TypeManager.short_type)
1590 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1591 if (real_target_type == TypeManager.ushort_type)
1592 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1593 if (real_target_type == TypeManager.int32_type)
1594 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1595 if (real_target_type == TypeManager.char_type)
1596 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1597 } else if (expr_type == TypeManager.int64_type){
1599 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1601 if (real_target_type == TypeManager.sbyte_type)
1602 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1603 if (real_target_type == TypeManager.byte_type)
1604 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1605 if (real_target_type == TypeManager.short_type)
1606 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1607 if (real_target_type == TypeManager.ushort_type)
1608 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1609 if (real_target_type == TypeManager.int32_type)
1610 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1611 if (real_target_type == TypeManager.uint32_type)
1612 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1613 if (real_target_type == TypeManager.uint64_type)
1614 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1615 if (real_target_type == TypeManager.char_type)
1616 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1617 } else if (expr_type == TypeManager.uint64_type){
1619 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1621 if (real_target_type == TypeManager.sbyte_type)
1622 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1623 if (real_target_type == TypeManager.byte_type)
1624 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1625 if (real_target_type == TypeManager.short_type)
1626 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1627 if (real_target_type == TypeManager.ushort_type)
1628 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1629 if (real_target_type == TypeManager.int32_type)
1630 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1631 if (real_target_type == TypeManager.uint32_type)
1632 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1633 if (real_target_type == TypeManager.int64_type)
1634 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1635 if (real_target_type == TypeManager.char_type)
1636 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1637 } else if (expr_type == TypeManager.char_type){
1639 // From char to sbyte, byte, short
1641 if (real_target_type == TypeManager.sbyte_type)
1642 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1643 if (real_target_type == TypeManager.byte_type)
1644 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1645 if (real_target_type == TypeManager.short_type)
1646 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1647 } else if (expr_type == TypeManager.float_type){
1649 // From float to sbyte, byte, short,
1650 // ushort, int, uint, long, ulong, char
1653 if (real_target_type == TypeManager.sbyte_type)
1654 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1655 if (real_target_type == TypeManager.byte_type)
1656 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1657 if (real_target_type == TypeManager.short_type)
1658 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1659 if (real_target_type == TypeManager.ushort_type)
1660 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1661 if (real_target_type == TypeManager.int32_type)
1662 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1663 if (real_target_type == TypeManager.uint32_type)
1664 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1665 if (real_target_type == TypeManager.int64_type)
1666 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1667 if (real_target_type == TypeManager.uint64_type)
1668 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1669 if (real_target_type == TypeManager.char_type)
1670 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1671 if (real_target_type == TypeManager.decimal_type)
1672 return InternalTypeConstructor (ec, expr, target_type);
1673 } else if (expr_type == TypeManager.double_type){
1675 // From double to byte, byte, short,
1676 // ushort, int, uint, long, ulong,
1677 // char, float or decimal
1679 if (real_target_type == TypeManager.sbyte_type)
1680 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1681 if (real_target_type == TypeManager.byte_type)
1682 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1683 if (real_target_type == TypeManager.short_type)
1684 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1685 if (real_target_type == TypeManager.ushort_type)
1686 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1687 if (real_target_type == TypeManager.int32_type)
1688 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1689 if (real_target_type == TypeManager.uint32_type)
1690 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1691 if (real_target_type == TypeManager.int64_type)
1692 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1693 if (real_target_type == TypeManager.uint64_type)
1694 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1695 if (real_target_type == TypeManager.char_type)
1696 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1697 if (real_target_type == TypeManager.float_type)
1698 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1699 if (real_target_type == TypeManager.decimal_type)
1700 return InternalTypeConstructor (ec, expr, target_type);
1703 // decimal is taken care of by the op_Explicit methods.
1709 /// Returns whether an explicit reference conversion can be performed
1710 /// from source_type to target_type
1712 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1714 bool target_is_value_type = target_type.IsValueType;
1716 if (source_type == target_type)
1720 // From object to any reference type
1722 if (source_type == TypeManager.object_type && !target_is_value_type)
1726 // From any class S to any class-type T, provided S is a base class of T
1728 if (target_type.IsSubclassOf (source_type))
1732 // From any interface type S to any interface T provided S is not derived from T
1734 if (source_type.IsInterface && target_type.IsInterface){
1735 if (!target_type.IsSubclassOf (source_type))
1740 // From any class type S to any interface T, provided S is not sealed
1741 // and provided S does not implement T.
1743 if (target_type.IsInterface && !source_type.IsSealed &&
1744 !TypeManager.ImplementsInterface (source_type, target_type))
1748 // From any interface-type S to to any class type T, provided T is not
1749 // sealed, or provided T implements S.
1751 if (source_type.IsInterface &&
1752 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))) {
1753 Console.WriteLine ("fooo came here !");
1757 // From an array type S with an element type Se to an array type T with an
1758 // element type Te provided all the following are true:
1759 // * S and T differe only in element type, in other words, S and T
1760 // have the same number of dimensions.
1761 // * Both Se and Te are reference types
1762 // * An explicit referenc conversions exist from Se to Te
1764 if (source_type.IsArray && target_type.IsArray) {
1765 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1767 Type source_element_type = source_type.GetElementType ();
1768 Type target_element_type = target_type.GetElementType ();
1770 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1771 if (ExplicitReferenceConversionExists (source_element_type,
1772 target_element_type))
1778 // From System.Array to any array-type
1779 if (source_type == TypeManager.array_type &&
1780 target_type.IsSubclassOf (TypeManager.array_type)){
1785 // From System delegate to any delegate-type
1787 if (source_type == TypeManager.delegate_type &&
1788 target_type.IsSubclassOf (TypeManager.delegate_type))
1792 // From ICloneable to Array or Delegate types
1794 if (source_type == TypeManager.icloneable_type &&
1795 (target_type == TypeManager.array_type ||
1796 target_type == TypeManager.delegate_type))
1803 /// Implements Explicit Reference conversions
1805 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1807 Type source_type = source.Type;
1808 bool target_is_value_type = target_type.IsValueType;
1811 // From object to any reference type
1813 if (source_type == TypeManager.object_type && !target_is_value_type)
1814 return new ClassCast (source, target_type);
1818 // From any class S to any class-type T, provided S is a base class of T
1820 if (target_type.IsSubclassOf (source_type))
1821 return new ClassCast (source, target_type);
1824 // From any interface type S to any interface T provided S is not derived from T
1826 if (source_type.IsInterface && target_type.IsInterface){
1827 if (TypeManager.ImplementsInterface (source_type, target_type))
1830 return new ClassCast (source, target_type);
1834 // From any class type S to any interface T, provides S is not sealed
1835 // and provided S does not implement T.
1837 if (target_type.IsInterface && !source_type.IsSealed) {
1838 if (TypeManager.ImplementsInterface (source_type, target_type))
1841 return new ClassCast (source, target_type);
1846 // From any interface-type S to to any class type T, provided T is not
1847 // sealed, or provided T implements S.
1849 if (source_type.IsInterface) {
1850 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
1851 return new ClassCast (source, target_type);
1856 // From an array type S with an element type Se to an array type T with an
1857 // element type Te provided all the following are true:
1858 // * S and T differe only in element type, in other words, S and T
1859 // have the same number of dimensions.
1860 // * Both Se and Te are reference types
1861 // * An explicit referenc conversions exist from Se to Te
1863 if (source_type.IsArray && target_type.IsArray) {
1864 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1866 Type source_element_type = source_type.GetElementType ();
1867 Type target_element_type = target_type.GetElementType ();
1869 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1870 if (ExplicitReferenceConversionExists (source_element_type,
1871 target_element_type))
1872 return new ClassCast (source, target_type);
1877 // From System.Array to any array-type
1878 if (source_type == TypeManager.array_type &&
1879 target_type.IsSubclassOf (TypeManager.array_type)){
1880 return new ClassCast (source, target_type);
1884 // From System delegate to any delegate-type
1886 if (source_type == TypeManager.delegate_type &&
1887 target_type.IsSubclassOf (TypeManager.delegate_type))
1888 return new ClassCast (source, target_type);
1891 // From ICloneable to Array or Delegate types
1893 if (source_type == TypeManager.icloneable_type &&
1894 (target_type == TypeManager.array_type ||
1895 target_type == TypeManager.delegate_type))
1896 return new ClassCast (source, target_type);
1902 /// Performs an explicit conversion of the expression `expr' whose
1903 /// type is expr.Type to `target_type'.
1905 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1906 Type target_type, Location loc)
1908 Type expr_type = expr.Type;
1909 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1914 ne = ConvertNumericExplicit (ec, expr, target_type);
1919 // Unboxing conversion.
1921 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1922 return new UnboxCast (expr, target_type);
1927 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1931 // FIXME: Is there any reason we should have EnumConstant
1932 // dealt with here instead of just using always the
1933 // UnderlyingSystemType to wrap the type?
1935 if (expr is EnumConstant)
1936 e = ((EnumConstant) expr).Child;
1938 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1941 Expression t = ConvertImplicit (ec, e, target_type, loc);
1945 return ConvertNumericExplicit (ec, e, target_type);
1948 ne = ConvertReferenceExplicit (expr, target_type);
1953 if (target_type.IsPointer){
1954 if (expr_type.IsPointer)
1955 return new EmptyCast (expr, target_type);
1957 if (expr_type == TypeManager.sbyte_type ||
1958 expr_type == TypeManager.byte_type ||
1959 expr_type == TypeManager.short_type ||
1960 expr_type == TypeManager.ushort_type ||
1961 expr_type == TypeManager.int32_type ||
1962 expr_type == TypeManager.uint32_type ||
1963 expr_type == TypeManager.uint64_type ||
1964 expr_type == TypeManager.int64_type)
1965 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1967 if (expr_type.IsPointer){
1968 if (target_type == TypeManager.sbyte_type ||
1969 target_type == TypeManager.byte_type ||
1970 target_type == TypeManager.short_type ||
1971 target_type == TypeManager.ushort_type ||
1972 target_type == TypeManager.int32_type ||
1973 target_type == TypeManager.uint32_type ||
1974 target_type == TypeManager.uint64_type ||
1975 target_type == TypeManager.int64_type){
1976 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1979 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1984 ce = ConvertNumericExplicit (ec, e, target_type);
1988 // We should always be able to go from an uint32
1989 // implicitly or explicitly to the other integral
1992 throw new Exception ("Internal compiler error");
1997 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2001 Error_CannotConvertType (loc, expr_type, target_type);
2006 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2008 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2009 Type target_type, Location l)
2011 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2016 ne = ConvertNumericExplicit (ec, expr, target_type);
2020 ne = ConvertReferenceExplicit (expr, target_type);
2024 Error_CannotConvertType (l, expr.Type, target_type);
2028 static string ExprClassName (ExprClass c)
2031 case ExprClass.Invalid:
2033 case ExprClass.Value:
2035 case ExprClass.Variable:
2037 case ExprClass.Namespace:
2039 case ExprClass.Type:
2041 case ExprClass.MethodGroup:
2042 return "method group";
2043 case ExprClass.PropertyAccess:
2044 return "property access";
2045 case ExprClass.EventAccess:
2046 return "event access";
2047 case ExprClass.IndexerAccess:
2048 return "indexer access";
2049 case ExprClass.Nothing:
2052 throw new Exception ("Should not happen");
2056 /// Reports that we were expecting `expr' to be of class `expected'
2058 protected void report118 (Location loc, Expression expr, string expected)
2060 string kind = "Unknown";
2063 kind = ExprClassName (expr.eclass);
2065 Error (118, loc, "Expression denotes a `" + kind +
2066 "' where a `" + expected + "' was expected");
2069 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2071 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2072 TypeManager.CSharpName (t));
2075 public static void UnsafeError (Location loc)
2077 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2081 /// Converts the IntConstant, UIntConstant, LongConstant or
2082 /// ULongConstant into the integral target_type. Notice
2083 /// that we do not return an `Expression' we do return
2084 /// a boxed integral type.
2086 /// FIXME: Since I added the new constants, we need to
2087 /// also support conversions from CharConstant, ByteConstant,
2088 /// SByteConstant, UShortConstant, ShortConstant
2090 /// This is used by the switch statement, so the domain
2091 /// of work is restricted to the literals above, and the
2092 /// targets are int32, uint32, char, byte, sbyte, ushort,
2093 /// short, uint64 and int64
2095 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2099 if (c.Type == target_type)
2100 return ((Constant) c).GetValue ();
2103 // Make into one of the literals we handle, we dont really care
2104 // about this value as we will just return a few limited types
2106 if (c is EnumConstant)
2107 c = ((EnumConstant)c).WidenToCompilerConstant ();
2109 if (c is IntConstant){
2110 int v = ((IntConstant) c).Value;
2112 if (target_type == TypeManager.uint32_type){
2115 } else if (target_type == TypeManager.char_type){
2116 if (v >= Char.MinValue && v <= Char.MaxValue)
2118 } else if (target_type == TypeManager.byte_type){
2119 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2121 } else if (target_type == TypeManager.sbyte_type){
2122 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2124 } else if (target_type == TypeManager.short_type){
2125 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2127 } else if (target_type == TypeManager.ushort_type){
2128 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2130 } else if (target_type == TypeManager.int64_type)
2132 else if (target_type == TypeManager.uint64_type){
2138 } else if (c is UIntConstant){
2139 uint v = ((UIntConstant) c).Value;
2141 if (target_type == TypeManager.int32_type){
2142 if (v <= Int32.MaxValue)
2144 } else if (target_type == TypeManager.char_type){
2145 if (v >= Char.MinValue && v <= Char.MaxValue)
2147 } else if (target_type == TypeManager.byte_type){
2148 if (v <= Byte.MaxValue)
2150 } else if (target_type == TypeManager.sbyte_type){
2151 if (v <= SByte.MaxValue)
2153 } else if (target_type == TypeManager.short_type){
2154 if (v <= UInt16.MaxValue)
2156 } else if (target_type == TypeManager.ushort_type){
2157 if (v <= UInt16.MaxValue)
2159 } else if (target_type == TypeManager.int64_type)
2161 else if (target_type == TypeManager.uint64_type)
2164 } else if (c is LongConstant){
2165 long v = ((LongConstant) c).Value;
2167 if (target_type == TypeManager.int32_type){
2168 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2170 } else if (target_type == TypeManager.uint32_type){
2171 if (v >= 0 && v <= UInt32.MaxValue)
2173 } else if (target_type == TypeManager.char_type){
2174 if (v >= Char.MinValue && v <= Char.MaxValue)
2176 } else if (target_type == TypeManager.byte_type){
2177 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2179 } else if (target_type == TypeManager.sbyte_type){
2180 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2182 } else if (target_type == TypeManager.short_type){
2183 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2185 } else if (target_type == TypeManager.ushort_type){
2186 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2188 } else if (target_type == TypeManager.uint64_type){
2193 } else if (c is ULongConstant){
2194 ulong v = ((ULongConstant) c).Value;
2196 if (target_type == TypeManager.int32_type){
2197 if (v <= Int32.MaxValue)
2199 } else if (target_type == TypeManager.uint32_type){
2200 if (v <= UInt32.MaxValue)
2202 } else if (target_type == TypeManager.char_type){
2203 if (v >= Char.MinValue && v <= Char.MaxValue)
2205 } else if (target_type == TypeManager.byte_type){
2206 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2208 } else if (target_type == TypeManager.sbyte_type){
2209 if (v <= (int) SByte.MaxValue)
2211 } else if (target_type == TypeManager.short_type){
2212 if (v <= UInt16.MaxValue)
2214 } else if (target_type == TypeManager.ushort_type){
2215 if (v <= UInt16.MaxValue)
2217 } else if (target_type == TypeManager.int64_type){
2218 if (v <= Int64.MaxValue)
2222 } else if (c is ByteConstant){
2223 byte v = ((ByteConstant) c).Value;
2225 if (target_type == TypeManager.int32_type)
2227 else if (target_type == TypeManager.uint32_type)
2229 else if (target_type == TypeManager.char_type)
2231 else if (target_type == TypeManager.sbyte_type){
2232 if (v <= SByte.MaxValue)
2234 } else if (target_type == TypeManager.short_type)
2236 else if (target_type == TypeManager.ushort_type)
2238 else if (target_type == TypeManager.int64_type)
2240 else if (target_type == TypeManager.uint64_type)
2243 } else if (c is SByteConstant){
2244 sbyte v = ((SByteConstant) c).Value;
2246 if (target_type == TypeManager.int32_type)
2248 else if (target_type == TypeManager.uint32_type){
2251 } else if (target_type == TypeManager.char_type){
2254 } else if (target_type == TypeManager.byte_type){
2257 } else if (target_type == TypeManager.short_type)
2259 else if (target_type == TypeManager.ushort_type){
2262 } else if (target_type == TypeManager.int64_type)
2264 else if (target_type == TypeManager.uint64_type){
2269 } else if (c is ShortConstant){
2270 short v = ((ShortConstant) c).Value;
2272 if (target_type == TypeManager.int32_type){
2274 } else if (target_type == TypeManager.uint32_type){
2277 } else if (target_type == TypeManager.char_type){
2280 } else if (target_type == TypeManager.byte_type){
2281 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2283 } else if (target_type == TypeManager.sbyte_type){
2284 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2286 } else if (target_type == TypeManager.ushort_type){
2289 } else if (target_type == TypeManager.int64_type)
2291 else if (target_type == TypeManager.uint64_type)
2295 } else if (c is UShortConstant){
2296 ushort v = ((UShortConstant) c).Value;
2298 if (target_type == TypeManager.int32_type)
2300 else if (target_type == TypeManager.uint32_type)
2302 else if (target_type == TypeManager.char_type){
2303 if (v >= Char.MinValue && v <= Char.MaxValue)
2305 } else if (target_type == TypeManager.byte_type){
2306 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2308 } else if (target_type == TypeManager.sbyte_type){
2309 if (v <= SByte.MaxValue)
2311 } else if (target_type == TypeManager.short_type){
2312 if (v <= Int16.MaxValue)
2314 } else if (target_type == TypeManager.int64_type)
2316 else if (target_type == TypeManager.uint64_type)
2320 } else if (c is CharConstant){
2321 char v = ((CharConstant) c).Value;
2323 if (target_type == TypeManager.int32_type)
2325 else if (target_type == TypeManager.uint32_type)
2327 else if (target_type == TypeManager.byte_type){
2328 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2330 } else if (target_type == TypeManager.sbyte_type){
2331 if (v <= SByte.MaxValue)
2333 } else if (target_type == TypeManager.short_type){
2334 if (v <= Int16.MaxValue)
2336 } else if (target_type == TypeManager.ushort_type)
2338 else if (target_type == TypeManager.int64_type)
2340 else if (target_type == TypeManager.uint64_type)
2345 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2350 // Load the object from the pointer. The `IsReference' is used
2351 // to control whether we should use Ldind_Ref or LdObj if the
2352 // value is not a `core' type.
2354 // Maybe we should try to extract this infromation form the type?
2355 // TODO: Maybe this is a bug. The reason we have this flag is because
2356 // I had almost identical code in ParameterReference (for handling
2357 // references) and in UnboxCast.
2359 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2361 if (t == TypeManager.int32_type)
2362 ig.Emit (OpCodes.Ldind_I4);
2363 else if (t == TypeManager.uint32_type)
2364 ig.Emit (OpCodes.Ldind_U4);
2365 else if (t == TypeManager.short_type)
2366 ig.Emit (OpCodes.Ldind_I2);
2367 else if (t == TypeManager.ushort_type)
2368 ig.Emit (OpCodes.Ldind_U2);
2369 else if (t == TypeManager.char_type)
2370 ig.Emit (OpCodes.Ldind_U2);
2371 else if (t == TypeManager.byte_type)
2372 ig.Emit (OpCodes.Ldind_U1);
2373 else if (t == TypeManager.sbyte_type)
2374 ig.Emit (OpCodes.Ldind_I1);
2375 else if (t == TypeManager.uint64_type)
2376 ig.Emit (OpCodes.Ldind_I8);
2377 else if (t == TypeManager.int64_type)
2378 ig.Emit (OpCodes.Ldind_I8);
2379 else if (t == TypeManager.float_type)
2380 ig.Emit (OpCodes.Ldind_R4);
2381 else if (t == TypeManager.double_type)
2382 ig.Emit (OpCodes.Ldind_R8);
2383 else if (t == TypeManager.bool_type)
2384 ig.Emit (OpCodes.Ldind_I1);
2385 else if (t == TypeManager.intptr_type)
2386 ig.Emit (OpCodes.Ldind_I);
2387 else if (TypeManager.IsEnumType (t)){
2388 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2391 ig.Emit (OpCodes.Ldind_Ref);
2393 ig.Emit (OpCodes.Ldobj, t);
2398 // The stack contains the pointer and the value of type `type'
2400 public static void StoreFromPtr (ILGenerator ig, Type type)
2402 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2403 ig.Emit (OpCodes.Stind_I4);
2404 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2405 ig.Emit (OpCodes.Stind_I8);
2406 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2407 type == TypeManager.ushort_type)
2408 ig.Emit (OpCodes.Stind_I2);
2409 else if (type == TypeManager.float_type)
2410 ig.Emit (OpCodes.Stind_R4);
2411 else if (type == TypeManager.double_type)
2412 ig.Emit (OpCodes.Stind_R8);
2413 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2414 type == TypeManager.bool_type)
2415 ig.Emit (OpCodes.Stind_I1);
2416 else if (type == TypeManager.intptr_type)
2417 ig.Emit (OpCodes.Stind_I);
2419 ig.Emit (OpCodes.Stind_Ref);
2423 // Returns the size of type `t' if known, otherwise, 0
2425 public static int GetTypeSize (Type t)
2427 if (t == TypeManager.int32_type ||
2428 t == TypeManager.uint32_type ||
2429 t == TypeManager.float_type)
2431 else if (t == TypeManager.int64_type ||
2432 t == TypeManager.uint64_type ||
2433 t == TypeManager.double_type)
2435 else if (t == TypeManager.byte_type ||
2436 t == TypeManager.sbyte_type ||
2437 t == TypeManager.bool_type)
2439 else if (t == TypeManager.short_type ||
2440 t == TypeManager.char_type ||
2441 t == TypeManager.ushort_type)
2449 /// This is just a base class for expressions that can
2450 /// appear on statements (invocations, object creation,
2451 /// assignments, post/pre increment and decrement). The idea
2452 /// being that they would support an extra Emition interface that
2453 /// does not leave a result on the stack.
2455 public abstract class ExpressionStatement : Expression {
2458 /// Requests the expression to be emitted in a `statement'
2459 /// context. This means that no new value is left on the
2460 /// stack after invoking this method (constrasted with
2461 /// Emit that will always leave a value on the stack).
2463 public abstract void EmitStatement (EmitContext ec);
2467 /// This kind of cast is used to encapsulate the child
2468 /// whose type is child.Type into an expression that is
2469 /// reported to return "return_type". This is used to encapsulate
2470 /// expressions which have compatible types, but need to be dealt
2471 /// at higher levels with.
2473 /// For example, a "byte" expression could be encapsulated in one
2474 /// of these as an "unsigned int". The type for the expression
2475 /// would be "unsigned int".
2478 public class EmptyCast : Expression {
2479 protected Expression child;
2481 public EmptyCast (Expression child, Type return_type)
2483 eclass = child.eclass;
2488 public override Expression DoResolve (EmitContext ec)
2490 // This should never be invoked, we are born in fully
2491 // initialized state.
2496 public override void Emit (EmitContext ec)
2503 /// This class is used to wrap literals which belong inside Enums
2505 public class EnumConstant : Constant {
2506 public Constant Child;
2508 public EnumConstant (Constant child, Type enum_type)
2510 eclass = child.eclass;
2515 public override Expression DoResolve (EmitContext ec)
2517 // This should never be invoked, we are born in fully
2518 // initialized state.
2523 public override void Emit (EmitContext ec)
2528 public override object GetValue ()
2530 return Child.GetValue ();
2534 // Converts from one of the valid underlying types for an enumeration
2535 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2536 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2538 public Constant WidenToCompilerConstant ()
2540 Type t = TypeManager.EnumToUnderlying (Child.Type);
2541 object v = ((Constant) Child).GetValue ();;
2543 if (t == TypeManager.int32_type)
2544 return new IntConstant ((int) v);
2545 if (t == TypeManager.uint32_type)
2546 return new UIntConstant ((uint) v);
2547 if (t == TypeManager.int64_type)
2548 return new LongConstant ((long) v);
2549 if (t == TypeManager.uint64_type)
2550 return new ULongConstant ((ulong) v);
2551 if (t == TypeManager.short_type)
2552 return new ShortConstant ((short) v);
2553 if (t == TypeManager.ushort_type)
2554 return new UShortConstant ((ushort) v);
2555 if (t == TypeManager.byte_type)
2556 return new ByteConstant ((byte) v);
2557 if (t == TypeManager.sbyte_type)
2558 return new SByteConstant ((sbyte) v);
2560 throw new Exception ("Invalid enumeration underlying type: " + t);
2564 // Extracts the value in the enumeration on its native representation
2566 public object GetPlainValue ()
2568 Type t = TypeManager.EnumToUnderlying (Child.Type);
2569 object v = ((Constant) Child).GetValue ();;
2571 if (t == TypeManager.int32_type)
2573 if (t == TypeManager.uint32_type)
2575 if (t == TypeManager.int64_type)
2577 if (t == TypeManager.uint64_type)
2579 if (t == TypeManager.short_type)
2581 if (t == TypeManager.ushort_type)
2583 if (t == TypeManager.byte_type)
2585 if (t == TypeManager.sbyte_type)
2591 public override string AsString ()
2593 return Child.AsString ();
2596 public override DoubleConstant ConvertToDouble ()
2598 return Child.ConvertToDouble ();
2601 public override FloatConstant ConvertToFloat ()
2603 return Child.ConvertToFloat ();
2606 public override ULongConstant ConvertToULong ()
2608 return Child.ConvertToULong ();
2611 public override LongConstant ConvertToLong ()
2613 return Child.ConvertToLong ();
2616 public override UIntConstant ConvertToUInt ()
2618 return Child.ConvertToUInt ();
2621 public override IntConstant ConvertToInt ()
2623 return Child.ConvertToInt ();
2628 /// This kind of cast is used to encapsulate Value Types in objects.
2630 /// The effect of it is to box the value type emitted by the previous
2633 public class BoxedCast : EmptyCast {
2635 public BoxedCast (Expression expr)
2636 : base (expr, TypeManager.object_type)
2640 public override Expression DoResolve (EmitContext ec)
2642 // This should never be invoked, we are born in fully
2643 // initialized state.
2648 public override void Emit (EmitContext ec)
2652 ec.ig.Emit (OpCodes.Box, child.Type);
2656 public class UnboxCast : EmptyCast {
2657 public UnboxCast (Expression expr, Type return_type)
2658 : base (expr, return_type)
2662 public override Expression DoResolve (EmitContext ec)
2664 // This should never be invoked, we are born in fully
2665 // initialized state.
2670 public override void Emit (EmitContext ec)
2673 ILGenerator ig = ec.ig;
2676 ig.Emit (OpCodes.Unbox, t);
2678 LoadFromPtr (ig, t, false);
2683 /// This is used to perform explicit numeric conversions.
2685 /// Explicit numeric conversions might trigger exceptions in a checked
2686 /// context, so they should generate the conv.ovf opcodes instead of
2689 public class ConvCast : EmptyCast {
2690 public enum Mode : byte {
2691 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2693 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2694 U2_I1, U2_U1, U2_I2, U2_CH,
2695 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2696 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2697 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2698 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2699 CH_I1, CH_U1, CH_I2,
2700 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2701 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2707 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2708 : base (child, return_type)
2711 checked_state = ec.CheckState;
2714 public override Expression DoResolve (EmitContext ec)
2716 // This should never be invoked, we are born in fully
2717 // initialized state.
2722 public override void Emit (EmitContext ec)
2724 ILGenerator ig = ec.ig;
2730 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2731 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2732 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2733 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2734 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2736 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2737 case Mode.U1_CH: /* nothing */ break;
2739 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2740 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2741 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2742 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2743 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2744 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2746 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2747 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2748 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2749 case Mode.U2_CH: /* nothing */ break;
2751 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2752 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2753 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2754 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2755 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2756 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2757 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2759 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2760 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2761 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2762 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2763 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2764 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2766 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2767 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2768 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2769 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2770 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2771 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2772 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2773 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2775 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2776 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2777 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2778 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2779 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2780 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2781 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2782 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2784 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2785 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2786 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2788 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2789 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2790 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2791 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2792 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2793 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2794 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2795 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2796 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2798 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2799 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2800 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2801 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2802 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2803 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2804 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2805 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2806 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2807 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2811 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2812 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2813 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2814 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2815 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2817 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2818 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2820 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2821 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2822 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2823 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2824 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2825 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2827 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2828 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2829 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2830 case Mode.U2_CH: /* nothing */ break;
2832 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2833 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2834 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2835 case Mode.I4_U4: /* nothing */ break;
2836 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2837 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2838 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2840 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2841 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2842 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2843 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2844 case Mode.U4_I4: /* nothing */ break;
2845 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2847 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2848 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2849 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2850 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2851 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2852 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2853 case Mode.I8_U8: /* nothing */ break;
2854 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2856 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2857 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2858 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2859 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2860 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2861 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2862 case Mode.U8_I8: /* nothing */ break;
2863 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2865 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2866 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2867 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2869 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2870 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2871 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2872 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2873 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2874 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2875 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2876 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2877 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2879 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2880 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2881 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2882 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2883 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2884 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2885 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2886 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2887 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2888 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2894 public class OpcodeCast : EmptyCast {
2898 public OpcodeCast (Expression child, Type return_type, OpCode op)
2899 : base (child, return_type)
2903 second_valid = false;
2906 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2907 : base (child, return_type)
2912 second_valid = true;
2915 public override Expression DoResolve (EmitContext ec)
2917 // This should never be invoked, we are born in fully
2918 // initialized state.
2923 public override void Emit (EmitContext ec)
2934 /// This kind of cast is used to encapsulate a child and cast it
2935 /// to the class requested
2937 public class ClassCast : EmptyCast {
2938 public ClassCast (Expression child, Type return_type)
2939 : base (child, return_type)
2944 public override Expression DoResolve (EmitContext ec)
2946 // This should never be invoked, we are born in fully
2947 // initialized state.
2952 public override void Emit (EmitContext ec)
2956 ec.ig.Emit (OpCodes.Castclass, type);
2962 /// SimpleName expressions are initially formed of a single
2963 /// word and it only happens at the beginning of the expression.
2967 /// The expression will try to be bound to a Field, a Method
2968 /// group or a Property. If those fail we pass the name to our
2969 /// caller and the SimpleName is compounded to perform a type
2970 /// lookup. The idea behind this process is that we want to avoid
2971 /// creating a namespace map from the assemblies, as that requires
2972 /// the GetExportedTypes function to be called and a hashtable to
2973 /// be constructed which reduces startup time. If later we find
2974 /// that this is slower, we should create a `NamespaceExpr' expression
2975 /// that fully participates in the resolution process.
2977 /// For example `System.Console.WriteLine' is decomposed into
2978 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2980 /// The first SimpleName wont produce a match on its own, so it will
2982 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2984 /// System.Console will produce a TypeExpr match.
2986 /// The downside of this is that we might be hitting `LookupType' too many
2987 /// times with this scheme.
2989 public class SimpleName : Expression {
2990 public readonly string Name;
2991 public readonly Location Location;
2993 public SimpleName (string name, Location l)
2999 public static void Error120 (Location l, string name)
3003 "An object reference is required " +
3004 "for the non-static field `"+name+"'");
3008 // Checks whether we are trying to access an instance
3009 // property, method or field from a static body.
3011 Expression MemberStaticCheck (Expression e)
3013 if (e is FieldExpr){
3014 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3017 Error120 (Location, Name);
3020 } else if (e is MethodGroupExpr){
3021 MethodGroupExpr mg = (MethodGroupExpr) e;
3023 if (!mg.RemoveInstanceMethods ()){
3024 Error120 (Location, mg.Methods [0].Name);
3028 } else if (e is PropertyExpr){
3029 if (!((PropertyExpr) e).IsStatic){
3030 Error120 (Location, Name);
3033 } else if (e is EventExpr) {
3034 if (!((EventExpr) e).IsStatic) {
3035 Error120 (Location, Name);
3043 public override Expression DoResolve (EmitContext ec)
3045 return SimpleNameResolve (ec, false);
3048 public Expression DoResolveAllowStatic (EmitContext ec)
3050 return SimpleNameResolve (ec, true);
3054 /// 7.5.2: Simple Names.
3056 /// Local Variables and Parameters are handled at
3057 /// parse time, so they never occur as SimpleNames.
3059 /// The `allow_static' flag is used by MemberAccess only
3060 /// and it is used to inform us that it is ok for us to
3061 /// avoid the static check, because MemberAccess might end
3062 /// up resolving the Name as a Type name and the access as
3063 /// a static type access.
3065 /// ie: Type Type; .... { Type.GetType (""); }
3067 /// Type is both an instance variable and a Type; Type.GetType
3068 /// is the static method not an instance method of type.
3070 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
3072 Expression e = null;
3075 // Stage 1: Performed by the parser (binding to locals or parameters).
3077 if (!ec.OnlyLookupTypes){
3078 Block current_block = ec.CurrentBlock;
3079 if (current_block != null && current_block.IsVariableDefined (Name)){
3080 LocalVariableReference var;
3082 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3084 return var.Resolve (ec);
3088 // Stage 2: Lookup members
3092 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3093 // Hence we have two different cases
3096 DeclSpace lookup_ds = ec.DeclSpace;
3098 if (lookup_ds.TypeBuilder == null)
3101 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
3106 // Classes/structs keep looking, enums break
3108 if (lookup_ds is TypeContainer)
3109 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3112 } while (lookup_ds != null);
3114 if (e == null && ec.ContainerType != null)
3115 e = MemberLookup (ec, ec.ContainerType, Name, Location);
3118 // Continuation of stage 2
3121 // Stage 3: Lookup symbol in the various namespaces.
3123 DeclSpace ds = ec.DeclSpace;
3127 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3128 return new TypeExpr (t);
3131 // Stage 2 part b: Lookup up if we are an alias to a type
3134 // Since we are cheating: we only do the Alias lookup for
3135 // namespaces if the name does not include any dots in it
3138 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3139 // System.Console.WriteLine (Name + " --> " + alias_value);
3140 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3142 return new TypeExpr (t);
3144 // we have alias value, but it isn't Type, so try if it's namespace
3145 return new SimpleName (alias_value, Location);
3148 // No match, maybe our parent can compose us
3149 // into something meaningful.
3154 // Stage 2 continues here.
3159 if (ec.OnlyLookupTypes)
3162 if (e is FieldExpr){
3163 FieldExpr fe = (FieldExpr) e;
3164 FieldInfo fi = fe.FieldInfo;
3166 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3167 UnsafeError (Location);
3171 if (!allow_static && !fi.IsStatic){
3172 Error120 (Location, Name);
3176 // If we are not in static code and this
3177 // field is not static, set the instance to `this'.
3180 fe.InstanceExpression = ec.This;
3184 if (fi is FieldBuilder) {
3185 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3188 object o = c.LookupConstantValue (ec);
3189 object real_value = ((Constant)c.Expr).GetValue ();
3190 return Constantify (real_value, fi.FieldType);
3195 Type t = fi.FieldType;
3196 Type decl_type = fi.DeclaringType;
3199 if (fi is FieldBuilder)
3200 o = TypeManager.GetValue ((FieldBuilder) fi);
3202 o = fi.GetValue (fi);
3204 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3205 Expression enum_member = MemberLookup (
3206 ec, decl_type, "value__", MemberTypes.Field,
3207 AllBindingFlags, Location);
3209 Enum en = TypeManager.LookupEnum (decl_type);
3213 c = Constantify (o, en.UnderlyingType);
3215 c = Constantify (o, enum_member.Type);
3217 return new EnumConstant (c, decl_type);
3220 Expression exp = Constantify (o, t);
3226 if (e is EventExpr) {
3228 // If the event is local to this class, we transform ourselves into
3231 EventExpr ee = (EventExpr) e;
3233 Expression ml = MemberLookup (
3234 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3235 MemberTypes.Event, AllBindingFlags, Location);
3238 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3242 // If this happens, then we have an event with its own
3243 // accessors and private field etc so there's no need
3244 // to transform ourselves : we should instead flag an error
3246 Assign.error70 (ee.EventInfo, Location);
3250 ml = ExprClassFromMemberInfo (ec, mi, Location);
3253 Report.Error (-200, Location, "Internal error!!");
3257 Expression instance_expr;
3259 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3262 instance_expr = null;
3264 instance_expr = ec.This;
3266 instance_expr = instance_expr.Resolve (ec);
3268 if (instance_expr != null)
3269 instance_expr = instance_expr.Resolve (ec);
3271 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3280 return MemberStaticCheck (e);
3285 public override void Emit (EmitContext ec)
3288 // If this is ever reached, then we failed to
3289 // find the name as a namespace
3292 Error (103, Location, "The name `" + Name +
3293 "' does not exist in the class `" +
3294 ec.DeclSpace.Name + "'");
3299 /// Fully resolved expression that evaluates to a type
3301 public class TypeExpr : Expression {
3302 public TypeExpr (Type t)
3305 eclass = ExprClass.Type;
3308 override public Expression DoResolve (EmitContext ec)
3313 override public void Emit (EmitContext ec)
3315 throw new Exception ("Implement me");
3320 /// MethodGroup Expression.
3322 /// This is a fully resolved expression that evaluates to a type
3324 public class MethodGroupExpr : Expression {
3325 public MethodBase [] Methods;
3327 Expression instance_expression = null;
3329 public MethodGroupExpr (MemberInfo [] mi, Location l)
3331 Methods = new MethodBase [mi.Length];
3332 mi.CopyTo (Methods, 0);
3333 eclass = ExprClass.MethodGroup;
3334 type = TypeManager.object_type;
3338 public MethodGroupExpr (ArrayList list, Location l)
3340 Methods = new MethodBase [list.Count];
3343 list.CopyTo (Methods, 0);
3345 foreach (MemberInfo m in list){
3346 if (!(m is MethodBase)){
3347 Console.WriteLine ("Name " + m.Name);
3348 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3354 eclass = ExprClass.MethodGroup;
3355 type = TypeManager.object_type;
3359 // `A method group may have associated an instance expression'
3361 public Expression InstanceExpression {
3363 return instance_expression;
3367 instance_expression = value;
3371 override public Expression DoResolve (EmitContext ec)
3376 public void ReportUsageError ()
3378 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3379 Methods [0].Name + "()' is referenced without parentheses");
3382 override public void Emit (EmitContext ec)
3384 ReportUsageError ();
3387 bool RemoveMethods (bool keep_static)
3389 ArrayList smethods = new ArrayList ();
3390 int top = Methods.Length;
3393 for (i = 0; i < top; i++){
3394 MethodBase mb = Methods [i];
3396 if (mb.IsStatic == keep_static)
3400 if (smethods.Count == 0)
3403 Methods = new MethodBase [smethods.Count];
3404 smethods.CopyTo (Methods, 0);
3410 /// Removes any instance methods from the MethodGroup, returns
3411 /// false if the resulting set is empty.
3413 public bool RemoveInstanceMethods ()
3415 return RemoveMethods (true);
3419 /// Removes any static methods from the MethodGroup, returns
3420 /// false if the resulting set is empty.
3422 public bool RemoveStaticMethods ()
3424 return RemoveMethods (false);
3429 /// Fully resolved expression that evaluates to a Field
3431 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3432 public readonly FieldInfo FieldInfo;
3433 public Expression InstanceExpression;
3436 public FieldExpr (FieldInfo fi, Location l)
3439 eclass = ExprClass.Variable;
3440 type = fi.FieldType;
3444 override public Expression DoResolve (EmitContext ec)
3446 if (!FieldInfo.IsStatic){
3447 if (InstanceExpression == null){
3448 throw new Exception ("non-static FieldExpr without instance var\n" +
3449 "You have to assign the Instance variable\n" +
3450 "Of the FieldExpr to set this\n");
3453 InstanceExpression = InstanceExpression.Resolve (ec);
3454 if (InstanceExpression == null)
3461 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3463 Expression e = DoResolve (ec);
3468 if (!FieldInfo.IsInitOnly)
3472 // InitOnly fields can only be assigned in constructors
3475 if (ec.IsConstructor)
3478 Report.Error (191, loc,
3479 "Readonly field can not be assigned outside " +
3480 "of constructor or variable initializer");
3485 override public void Emit (EmitContext ec)
3487 ILGenerator ig = ec.ig;
3488 bool is_volatile = false;
3490 if (FieldInfo is FieldBuilder){
3491 FieldBase f = TypeManager.GetField (FieldInfo);
3493 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3496 f.status |= Field.Status.USED;
3499 if (FieldInfo.IsStatic){
3501 ig.Emit (OpCodes.Volatile);
3503 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3505 if (InstanceExpression.Type.IsValueType){
3507 LocalTemporary tempo = null;
3509 if (!(InstanceExpression is IMemoryLocation)){
3510 tempo = new LocalTemporary (
3511 ec, InstanceExpression.Type);
3513 InstanceExpression.Emit (ec);
3517 ml = (IMemoryLocation) InstanceExpression;
3519 ml.AddressOf (ec, AddressOp.Load);
3521 InstanceExpression.Emit (ec);
3524 ig.Emit (OpCodes.Volatile);
3526 ig.Emit (OpCodes.Ldfld, FieldInfo);
3530 public void EmitAssign (EmitContext ec, Expression source)
3532 bool is_static = FieldInfo.IsStatic;
3533 ILGenerator ig = ec.ig;
3536 Expression instance = InstanceExpression;
3538 if (instance.Type.IsValueType){
3539 if (instance is IMemoryLocation){
3540 IMemoryLocation ml = (IMemoryLocation) instance;
3542 ml.AddressOf (ec, AddressOp.Store);
3544 throw new Exception ("The " + instance + " of type " +
3546 " represents a ValueType and does " +
3547 "not implement IMemoryLocation");
3553 if (FieldInfo is FieldBuilder){
3554 FieldBase f = TypeManager.GetField (FieldInfo);
3556 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3557 ig.Emit (OpCodes.Volatile);
3561 ig.Emit (OpCodes.Stsfld, FieldInfo);
3563 ig.Emit (OpCodes.Stfld, FieldInfo);
3565 if (FieldInfo is FieldBuilder){
3566 FieldBase f = TypeManager.GetField (FieldInfo);
3568 f.status |= Field.Status.ASSIGNED;
3572 public void AddressOf (EmitContext ec, AddressOp mode)
3574 ILGenerator ig = ec.ig;
3576 if (FieldInfo is FieldBuilder){
3577 FieldBase f = TypeManager.GetField (FieldInfo);
3578 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3579 ig.Emit (OpCodes.Volatile);
3582 if (FieldInfo is FieldBuilder){
3583 FieldBase f = TypeManager.GetField (FieldInfo);
3585 if ((mode & AddressOp.Store) != 0)
3586 f.status |= Field.Status.ASSIGNED;
3587 if ((mode & AddressOp.Load) != 0)
3588 f.status |= Field.Status.USED;
3592 // Handle initonly fields specially: make a copy and then
3593 // get the address of the copy.
3595 if (FieldInfo.IsInitOnly){
3599 local = ig.DeclareLocal (type);
3600 ig.Emit (OpCodes.Stloc, local);
3601 ig.Emit (OpCodes.Ldloca, local);
3605 if (FieldInfo.IsStatic)
3606 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3608 InstanceExpression.Emit (ec);
3609 ig.Emit (OpCodes.Ldflda, FieldInfo);
3615 /// Expression that evaluates to a Property. The Assign class
3616 /// might set the `Value' expression if we are in an assignment.
3618 /// This is not an LValue because we need to re-write the expression, we
3619 /// can not take data from the stack and store it.
3621 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3622 public readonly PropertyInfo PropertyInfo;
3623 public readonly bool IsStatic;
3625 MethodInfo [] Accessors;
3628 Expression instance_expr;
3630 public PropertyExpr (PropertyInfo pi, Location l)
3633 eclass = ExprClass.PropertyAccess;
3636 Accessors = TypeManager.GetAccessors (pi);
3638 if (Accessors != null)
3639 for (int i = 0; i < Accessors.Length; i++){
3640 if (Accessors [i] != null)
3641 if (Accessors [i].IsStatic)
3645 Accessors = new MethodInfo [2];
3647 type = pi.PropertyType;
3651 // The instance expression associated with this expression
3653 public Expression InstanceExpression {
3655 instance_expr = value;
3659 return instance_expr;
3663 public bool VerifyAssignable ()
3665 if (!PropertyInfo.CanWrite){
3666 Report.Error (200, loc,
3667 "The property `" + PropertyInfo.Name +
3668 "' can not be assigned to, as it has not set accessor");
3675 override public Expression DoResolve (EmitContext ec)
3677 if (!PropertyInfo.CanRead){
3678 Report.Error (154, loc,
3679 "The property `" + PropertyInfo.Name +
3680 "' can not be used in " +
3681 "this context because it lacks a get accessor");
3685 type = PropertyInfo.PropertyType;
3690 override public void Emit (EmitContext ec)
3692 MethodInfo method = Accessors [0];
3695 // Special case: length of single dimension array is turned into ldlen
3697 if (method == TypeManager.int_array_get_length){
3698 Type iet = instance_expr.Type;
3700 if (iet.GetArrayRank () == 1){
3701 instance_expr.Emit (ec);
3702 ec.ig.Emit (OpCodes.Ldlen);
3707 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
3712 // Implements the IAssignMethod interface for assignments
3714 public void EmitAssign (EmitContext ec, Expression source)
3716 Argument arg = new Argument (source, Argument.AType.Expression);
3717 ArrayList args = new ArrayList ();
3720 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3723 override public void EmitStatement (EmitContext ec)
3726 ec.ig.Emit (OpCodes.Pop);
3731 /// Fully resolved expression that evaluates to an Event
3733 public class EventExpr : Expression {
3734 public readonly EventInfo EventInfo;
3736 public Expression InstanceExpression;
3738 public readonly bool IsStatic;
3740 MethodInfo add_accessor, remove_accessor;
3742 public EventExpr (EventInfo ei, Location loc)
3746 eclass = ExprClass.EventAccess;
3748 add_accessor = TypeManager.GetAddMethod (ei);
3749 remove_accessor = TypeManager.GetRemoveMethod (ei);
3751 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3754 if (EventInfo is MyEventBuilder)
3755 type = ((MyEventBuilder) EventInfo).EventType;
3757 type = EventInfo.EventHandlerType;
3760 override public Expression DoResolve (EmitContext ec)
3762 // We are born fully resolved
3766 override public void Emit (EmitContext ec)
3768 throw new Exception ("Should not happen I think");
3771 public void EmitAddOrRemove (EmitContext ec, Expression source)
3773 Expression handler = ((Binary) source).Right;
3775 Argument arg = new Argument (handler, Argument.AType.Expression);
3776 ArrayList args = new ArrayList ();
3780 if (((Binary) source).Oper == Binary.Operator.Addition)
3781 Invocation.EmitCall (
3782 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3784 Invocation.EmitCall (
3785 ec, false, IsStatic, InstanceExpression, remove_accessor, args);