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);
409 /// This is a wrapper for MemberLookup that is not used to "probe", but
410 /// to find a final definition. If the final definition is not found, we
411 /// look for private members and display a useful debugging message if we
414 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
419 e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
424 e = MemberLookup (ec, t, name, AllMemberTypes,
425 AllBindingFlags | BindingFlags.NonPublic, loc);
428 117, loc, "`" + t + "' does not contain a definition " +
429 "for `" + name + "'");
432 122, loc, "`" + t + "." + name +
433 "' is inaccessible due to its protection level");
439 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
441 Type expr_type = expr.Type;
443 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
444 // if we are a method group, emit a warning
449 if (target_type == TypeManager.object_type) {
451 // A pointer type cannot be converted to object
453 if (expr_type.IsPointer)
456 if (expr_type.IsValueType)
457 return new BoxedCast (expr);
458 if (expr_type.IsClass || expr_type.IsInterface)
459 return new EmptyCast (expr, target_type);
460 } else if (expr_type.IsSubclassOf (target_type)) {
461 return new EmptyCast (expr, target_type);
464 // This code is kind of mirrored inside StandardConversionExists
465 // with the small distinction that we only probe there
467 // Always ensure that the code here and there is in sync
469 // from the null type to any reference-type.
470 if (expr is NullLiteral && !target_type.IsValueType)
471 return new EmptyCast (expr, target_type);
473 // from any class-type S to any interface-type T.
474 if (expr_type.IsClass && target_type.IsInterface) {
475 if (TypeManager.ImplementsInterface (expr_type, target_type))
476 return new EmptyCast (expr, target_type);
481 // from any interface type S to interface-type T.
482 if (expr_type.IsInterface && target_type.IsInterface) {
484 if (TypeManager.ImplementsInterface (expr_type, target_type))
485 return new EmptyCast (expr, target_type);
490 // from an array-type S to an array-type of type T
491 if (expr_type.IsArray && target_type.IsArray) {
492 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
494 Type expr_element_type = expr_type.GetElementType ();
496 if (MyEmptyExpr == null)
497 MyEmptyExpr = new EmptyExpression ();
499 MyEmptyExpr.SetType (expr_element_type);
500 Type target_element_type = target_type.GetElementType ();
502 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
503 if (StandardConversionExists (MyEmptyExpr,
504 target_element_type))
505 return new EmptyCast (expr, target_type);
510 // from an array-type to System.Array
511 if (expr_type.IsArray && target_type == TypeManager.array_type)
512 return new EmptyCast (expr, target_type);
514 // from any delegate type to System.Delegate
515 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
516 target_type == TypeManager.delegate_type)
517 return new EmptyCast (expr, target_type);
519 // from any array-type or delegate type into System.ICloneable.
520 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
521 if (target_type == TypeManager.icloneable_type)
522 return new EmptyCast (expr, target_type);
532 /// Handles expressions like this: decimal d; d = 1;
533 /// and changes them into: decimal d; d = new System.Decimal (1);
535 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
537 ArrayList args = new ArrayList ();
539 args.Add (new Argument (expr, Argument.AType.Expression));
541 Expression ne = new New (target.FullName, args,
544 return ne.Resolve (ec);
548 /// Implicit Numeric Conversions.
550 /// expr is the expression to convert, returns a new expression of type
551 /// target_type or null if an implicit conversion is not possible.
553 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
554 Type target_type, Location loc)
556 Type expr_type = expr.Type;
559 // Attempt to do the implicit constant expression conversions
561 if (expr is IntConstant){
564 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
568 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
570 // Try the implicit constant expression conversion
571 // from long to ulong, instead of a nice routine,
574 long v = ((LongConstant) expr).Value;
576 return new ULongConstant ((ulong) v);
580 // If we have an enumeration, extract the underlying type,
581 // use this during the comparission, but wrap around the original
584 Type real_target_type = target_type;
586 if (TypeManager.IsEnumType (real_target_type))
587 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
589 if (expr_type == real_target_type)
590 return new EmptyCast (expr, target_type);
592 if (expr_type == TypeManager.sbyte_type){
594 // From sbyte to short, int, long, float, double.
596 if (real_target_type == TypeManager.int32_type)
597 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
598 if (real_target_type == TypeManager.int64_type)
599 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
600 if (real_target_type == TypeManager.double_type)
601 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
602 if (real_target_type == TypeManager.float_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
604 if (real_target_type == TypeManager.short_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
606 if (real_target_type == TypeManager.decimal_type)
607 return InternalTypeConstructor (ec, expr, target_type);
608 } else if (expr_type == TypeManager.byte_type){
610 // From byte to short, ushort, int, uint, long, ulong, float, double
612 if ((real_target_type == TypeManager.short_type) ||
613 (real_target_type == TypeManager.ushort_type) ||
614 (real_target_type == TypeManager.int32_type) ||
615 (real_target_type == TypeManager.uint32_type))
616 return new EmptyCast (expr, target_type);
618 if (real_target_type == TypeManager.uint64_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
620 if (real_target_type == TypeManager.int64_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
622 if (real_target_type == TypeManager.float_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
624 if (real_target_type == TypeManager.double_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
626 if (real_target_type == TypeManager.decimal_type)
627 return InternalTypeConstructor (ec, expr, target_type);
628 } else if (expr_type == TypeManager.short_type){
630 // From short to int, long, float, double
632 if (real_target_type == TypeManager.int32_type)
633 return new EmptyCast (expr, target_type);
634 if (real_target_type == TypeManager.int64_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
636 if (real_target_type == TypeManager.double_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
638 if (real_target_type == TypeManager.float_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
640 if (real_target_type == TypeManager.decimal_type)
641 return InternalTypeConstructor (ec, expr, target_type);
642 } else if (expr_type == TypeManager.ushort_type){
644 // From ushort to int, uint, long, ulong, float, double
646 if (real_target_type == TypeManager.uint32_type)
647 return new EmptyCast (expr, target_type);
649 if (real_target_type == TypeManager.uint64_type)
650 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
651 if (real_target_type == TypeManager.int32_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
653 if (real_target_type == TypeManager.int64_type)
654 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
655 if (real_target_type == TypeManager.double_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
657 if (real_target_type == TypeManager.float_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
659 if (real_target_type == TypeManager.decimal_type)
660 return InternalTypeConstructor (ec, expr, target_type);
661 } else if (expr_type == TypeManager.int32_type){
663 // From int to long, float, double
665 if (real_target_type == TypeManager.int64_type)
666 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
667 if (real_target_type == TypeManager.double_type)
668 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
669 if (real_target_type == TypeManager.float_type)
670 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
671 if (real_target_type == TypeManager.decimal_type)
672 return InternalTypeConstructor (ec, expr, target_type);
673 } else if (expr_type == TypeManager.uint32_type){
675 // From uint to long, ulong, float, double
677 if (real_target_type == TypeManager.int64_type)
678 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
679 if (real_target_type == TypeManager.uint64_type)
680 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
681 if (real_target_type == TypeManager.double_type)
682 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
684 if (real_target_type == TypeManager.float_type)
685 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
687 if (real_target_type == TypeManager.decimal_type)
688 return InternalTypeConstructor (ec, expr, target_type);
689 } else if ((expr_type == TypeManager.uint64_type) ||
690 (expr_type == TypeManager.int64_type)){
692 // From long/ulong to float, double
694 if (real_target_type == TypeManager.double_type)
695 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
697 if (real_target_type == TypeManager.float_type)
698 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
700 if (real_target_type == TypeManager.decimal_type)
701 return InternalTypeConstructor (ec, expr, target_type);
702 } else if (expr_type == TypeManager.char_type){
704 // From char to ushort, int, uint, long, ulong, float, double
706 if ((real_target_type == TypeManager.ushort_type) ||
707 (real_target_type == TypeManager.int32_type) ||
708 (real_target_type == TypeManager.uint32_type))
709 return new EmptyCast (expr, target_type);
710 if (real_target_type == TypeManager.uint64_type)
711 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
712 if (real_target_type == TypeManager.int64_type)
713 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
714 if (real_target_type == TypeManager.float_type)
715 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
716 if (real_target_type == TypeManager.double_type)
717 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
718 if (real_target_type == TypeManager.decimal_type)
719 return InternalTypeConstructor (ec, expr, target_type);
720 } else if (expr_type == TypeManager.float_type){
724 if (real_target_type == TypeManager.double_type)
725 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
732 /// Determines if a standard implicit conversion exists from
733 /// expr_type to target_type
735 public static bool StandardConversionExists (Expression expr, Type target_type)
737 Type expr_type = expr.Type;
739 if (expr_type == target_type)
742 // First numeric conversions
744 if (expr_type == TypeManager.sbyte_type){
746 // From sbyte to short, int, long, float, double.
748 if ((target_type == TypeManager.int32_type) ||
749 (target_type == TypeManager.int64_type) ||
750 (target_type == TypeManager.double_type) ||
751 (target_type == TypeManager.float_type) ||
752 (target_type == TypeManager.short_type) ||
753 (target_type == TypeManager.decimal_type))
756 } else if (expr_type == TypeManager.byte_type){
758 // From byte to short, ushort, int, uint, long, ulong, float, double
760 if ((target_type == TypeManager.short_type) ||
761 (target_type == TypeManager.ushort_type) ||
762 (target_type == TypeManager.int32_type) ||
763 (target_type == TypeManager.uint32_type) ||
764 (target_type == TypeManager.uint64_type) ||
765 (target_type == TypeManager.int64_type) ||
766 (target_type == TypeManager.float_type) ||
767 (target_type == TypeManager.double_type) ||
768 (target_type == TypeManager.decimal_type))
771 } else if (expr_type == TypeManager.short_type){
773 // From short to int, long, float, double
775 if ((target_type == TypeManager.int32_type) ||
776 (target_type == TypeManager.int64_type) ||
777 (target_type == TypeManager.double_type) ||
778 (target_type == TypeManager.float_type) ||
779 (target_type == TypeManager.decimal_type))
782 } else if (expr_type == TypeManager.ushort_type){
784 // From ushort to int, uint, long, ulong, float, double
786 if ((target_type == TypeManager.uint32_type) ||
787 (target_type == TypeManager.uint64_type) ||
788 (target_type == TypeManager.int32_type) ||
789 (target_type == TypeManager.int64_type) ||
790 (target_type == TypeManager.double_type) ||
791 (target_type == TypeManager.float_type) ||
792 (target_type == TypeManager.decimal_type))
795 } else if (expr_type == TypeManager.int32_type){
797 // From int to long, float, double
799 if ((target_type == TypeManager.int64_type) ||
800 (target_type == TypeManager.double_type) ||
801 (target_type == TypeManager.float_type) ||
802 (target_type == TypeManager.decimal_type))
805 } else if (expr_type == TypeManager.uint32_type){
807 // From uint to long, ulong, float, double
809 if ((target_type == TypeManager.int64_type) ||
810 (target_type == TypeManager.uint64_type) ||
811 (target_type == TypeManager.double_type) ||
812 (target_type == TypeManager.float_type) ||
813 (target_type == TypeManager.decimal_type))
816 } else if ((expr_type == TypeManager.uint64_type) ||
817 (expr_type == TypeManager.int64_type)) {
819 // From long/ulong to float, double
821 if ((target_type == TypeManager.double_type) ||
822 (target_type == TypeManager.float_type) ||
823 (target_type == TypeManager.decimal_type))
826 } else if (expr_type == TypeManager.char_type){
828 // From char to ushort, int, uint, long, ulong, float, double
830 if ((target_type == TypeManager.ushort_type) ||
831 (target_type == TypeManager.int32_type) ||
832 (target_type == TypeManager.uint32_type) ||
833 (target_type == TypeManager.uint64_type) ||
834 (target_type == TypeManager.int64_type) ||
835 (target_type == TypeManager.float_type) ||
836 (target_type == TypeManager.double_type) ||
837 (target_type == TypeManager.decimal_type))
840 } else if (expr_type == TypeManager.float_type){
844 if (target_type == TypeManager.double_type)
848 // Next reference conversions
850 if (target_type == TypeManager.object_type) {
851 if ((expr_type.IsClass) ||
852 (expr_type.IsValueType))
855 } else if (expr_type.IsSubclassOf (target_type)) {
859 // Please remember that all code below actually comes
860 // from ImplicitReferenceConversion so make sure code remains in sync
862 // from any class-type S to any interface-type T.
863 if (expr_type.IsClass && target_type.IsInterface) {
864 if (TypeManager.ImplementsInterface (expr_type, target_type))
868 // from any interface type S to interface-type T.
869 // FIXME : Is it right to use IsAssignableFrom ?
870 if (expr_type.IsInterface && target_type.IsInterface)
871 if (target_type.IsAssignableFrom (expr_type))
874 // from an array-type S to an array-type of type T
875 if (expr_type.IsArray && target_type.IsArray) {
876 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
878 Type expr_element_type = expr_type.GetElementType ();
880 if (MyEmptyExpr == null)
881 MyEmptyExpr = new EmptyExpression ();
883 MyEmptyExpr.SetType (expr_element_type);
884 Type target_element_type = target_type.GetElementType ();
886 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
887 if (StandardConversionExists (MyEmptyExpr,
888 target_element_type))
893 // from an array-type to System.Array
894 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
897 // from any delegate type to System.Delegate
898 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
899 target_type == TypeManager.delegate_type)
900 if (target_type.IsAssignableFrom (expr_type))
903 // from any array-type or delegate type into System.ICloneable.
904 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
905 if (target_type == TypeManager.icloneable_type)
908 // from the null type to any reference-type.
909 if (expr is NullLiteral && !target_type.IsValueType)
917 static EmptyExpression MyEmptyExpr;
919 /// Tells whether an implicit conversion exists from expr_type to
922 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
925 if (MyEmptyExpr == null)
926 MyEmptyExpr = new EmptyExpression (expr_type);
928 MyEmptyExpr.SetType (expr_type);
930 return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
934 /// Finds "most encompassed type" according to the spec (13.4.2)
935 /// amongst the methods in the MethodGroupExpr which convert from a
936 /// type encompassing source_type
938 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
942 for (int i = me.Methods.Length; i > 0; ) {
945 MethodBase mb = me.Methods [i];
946 ParameterData pd = Invocation.GetParameterData (mb);
947 Type param_type = pd.ParameterType (0);
949 Expression source = new EmptyExpression (source_type);
950 Expression param = new EmptyExpression (param_type);
952 if (StandardConversionExists (source, param_type)) {
956 if (StandardConversionExists (param, best))
965 /// Finds "most encompassing type" according to the spec (13.4.2)
966 /// amongst the methods in the MethodGroupExpr which convert to a
967 /// type encompassed by target_type
969 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
973 for (int i = me.Methods.Length; i > 0; ) {
976 MethodInfo mi = (MethodInfo) me.Methods [i];
977 Type ret_type = mi.ReturnType;
979 Expression ret = new EmptyExpression (ret_type);
981 if (StandardConversionExists (ret, target)) {
985 if (!StandardConversionExists (ret, best))
997 /// User-defined Implicit conversions
999 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1000 Type target, Location loc)
1002 return UserDefinedConversion (ec, source, target, loc, false);
1006 /// User-defined Explicit conversions
1008 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1009 Type target, Location loc)
1011 return UserDefinedConversion (ec, source, target, loc, true);
1015 /// User-defined conversions
1017 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1018 Type target, Location loc,
1019 bool look_for_explicit)
1021 Expression mg1 = null, mg2 = null;
1022 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1024 MethodBase method = null;
1025 Type source_type = source.Type;
1029 // If we have a boolean type, we need to check for the True operator
1031 // FIXME : How does the False operator come into the picture ?
1032 // FIXME : This doesn't look complete and very correct !
1033 if (target == TypeManager.bool_type)
1034 op_name = "op_True";
1036 op_name = "op_Implicit";
1040 // FIXME: This whole process can be optimized to check if the
1041 // return is non-null and make the union as we go.
1043 mg1 = MemberLookup (ec, source_type, op_name, loc);
1045 if (source_type.BaseType != null)
1046 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1048 mg3 = MemberLookup (ec, target, op_name, loc);
1050 if (target.BaseType != null)
1051 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1053 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2, loc);
1054 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4, loc);
1056 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2, loc);
1058 MethodGroupExpr union3;
1060 mg1 = MemberLookup (ec, source_type, op_name, loc);
1061 if (source_type.BaseType != null)
1062 mg2 = MemberLookup (ec, source_type.BaseType, op_name, MemberTypes.Method, AllBindingFlags, loc);
1065 union3 = (MethodGroupExpr) mg2;
1066 else if (mg2 == null)
1067 union3 = (MethodGroupExpr) mg1;
1069 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1071 mg1 = MemberLookup (ec, target, op_name, MemberTypes.Method, AllBindingFlags, loc);
1074 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1076 union3 = (MethodGroupExpr) mg1;
1078 if (target.BaseType != null)
1079 mg1 = MemberLookup (ec, target.BaseType, op_name, MemberTypes.Method, AllBindingFlags, loc);
1083 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1085 union3 = (MethodGroupExpr) mg1;
1088 MethodGroupExpr union4 = null;
1090 if (look_for_explicit) {
1092 op_name = "op_Explicit";
1094 mg5 = MemberLookup (ec, source_type, op_name, loc);
1096 if (source_type.BaseType != null)
1097 mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1099 mg7 = MemberLookup (ec, target, op_name, loc);
1101 if (target.BaseType != null)
1102 mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1104 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1105 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1107 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1110 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4, loc);
1112 if (union != null) {
1114 Type most_specific_source, most_specific_target;
1116 most_specific_source = FindMostEncompassedType (union, source_type);
1117 if (most_specific_source == null)
1120 most_specific_target = FindMostEncompassingType (union, target);
1121 if (most_specific_target == null)
1126 for (int i = union.Methods.Length; i > 0;) {
1129 MethodBase mb = union.Methods [i];
1130 ParameterData pd = Invocation.GetParameterData (mb);
1131 MethodInfo mi = (MethodInfo) union.Methods [i];
1133 if (pd.ParameterType (0) == most_specific_source &&
1134 mi.ReturnType == most_specific_target) {
1140 if (method == null || count > 1) {
1141 Report.Error (-11, loc, "Ambiguous user defined conversion");
1146 // This will do the conversion to the best match that we
1147 // found. Now we need to perform an implict standard conversion
1148 // if the best match was not the type that we were requested
1151 if (look_for_explicit)
1152 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1154 source = ConvertImplicitStandard (ec, source,
1155 most_specific_source, loc);
1160 e = new UserCast ((MethodInfo) method, source);
1162 if (e.Type != target){
1163 if (!look_for_explicit)
1164 e = ConvertImplicitStandard (ec, e, target, loc);
1166 e = ConvertExplicitStandard (ec, e, target, loc);
1177 /// Converts implicitly the resolved expression `expr' into the
1178 /// `target_type'. It returns a new expression that can be used
1179 /// in a context that expects a `target_type'.
1181 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1182 Type target_type, Location loc)
1184 Type expr_type = expr.Type;
1187 if (expr_type == target_type)
1190 if (target_type == null)
1191 throw new Exception ("Target type is null");
1193 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1197 e = ImplicitUserConversion (ec, expr, target_type, loc);
1206 /// Attempts to apply the `Standard Implicit
1207 /// Conversion' rules to the expression `expr' into
1208 /// the `target_type'. It returns a new expression
1209 /// that can be used in a context that expects a
1212 /// This is different from `ConvertImplicit' in that the
1213 /// user defined implicit conversions are excluded.
1215 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1216 Type target_type, Location loc)
1218 Type expr_type = expr.Type;
1221 if (expr_type == target_type)
1224 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1228 e = ImplicitReferenceConversion (expr, target_type);
1232 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1233 IntLiteral i = (IntLiteral) expr;
1236 return new EmptyCast (expr, target_type);
1240 if (expr_type.IsPointer){
1241 if (target_type == TypeManager.void_ptr_type)
1242 return new EmptyCast (expr, target_type);
1245 // yep, comparing pointer types cant be done with
1246 // t1 == t2, we have to compare their element types.
1248 if (target_type.IsPointer){
1249 if (target_type.GetElementType()==expr_type.GetElementType())
1254 if (target_type.IsPointer){
1255 if (expr is NullLiteral)
1256 return new EmptyCast (expr, target_type);
1264 /// Attemps to perform an implict constant conversion of the IntConstant
1265 /// into a different data type using casts (See Implicit Constant
1266 /// Expression Conversions)
1268 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1270 int value = ic.Value;
1273 // FIXME: This could return constants instead of EmptyCasts
1275 if (target_type == TypeManager.sbyte_type){
1276 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1277 return new SByteConstant ((sbyte) value);
1278 } else if (target_type == TypeManager.byte_type){
1279 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1280 return new ByteConstant ((byte) value);
1281 } else if (target_type == TypeManager.short_type){
1282 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1283 return new ShortConstant ((short) value);
1284 } else if (target_type == TypeManager.ushort_type){
1285 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1286 return new UShortConstant ((ushort) value);
1287 } else if (target_type == TypeManager.uint32_type){
1289 return new UIntConstant ((uint) value);
1290 } else if (target_type == TypeManager.uint64_type){
1292 // we can optimize this case: a positive int32
1293 // always fits on a uint64. But we need an opcode
1297 return new ULongConstant ((ulong) value);
1300 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1301 return new EnumConstant (ic, target_type);
1306 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1308 string msg = "Cannot convert implicitly from `"+
1309 TypeManager.CSharpName (source) + "' to `" +
1310 TypeManager.CSharpName (target) + "'";
1312 Error (29, loc, msg);
1316 /// Attemptes to implicityly convert `target' into `type', using
1317 /// ConvertImplicit. If there is no implicit conversion, then
1318 /// an error is signaled
1320 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1321 Type target_type, Location loc)
1325 e = ConvertImplicit (ec, source, target_type, loc);
1329 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1331 "Double literal cannot be implicitly converted to " +
1332 "float type, use F suffix to create a float literal");
1335 Error_CannotConvertImplicit (loc, source.Type, target_type);
1341 /// Performs the explicit numeric conversions
1343 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1346 Type expr_type = expr.Type;
1349 // If we have an enumeration, extract the underlying type,
1350 // use this during the comparission, but wrap around the original
1353 Type real_target_type = target_type;
1355 if (TypeManager.IsEnumType (real_target_type))
1356 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1358 if (expr_type == TypeManager.sbyte_type){
1360 // From sbyte to byte, ushort, uint, ulong, char
1362 if (real_target_type == TypeManager.byte_type)
1363 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1364 if (real_target_type == TypeManager.ushort_type)
1365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1366 if (real_target_type == TypeManager.uint32_type)
1367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1368 if (real_target_type == TypeManager.uint64_type)
1369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1370 if (real_target_type == TypeManager.char_type)
1371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1372 } else if (expr_type == TypeManager.byte_type){
1374 // From byte to sbyte and char
1376 if (real_target_type == TypeManager.sbyte_type)
1377 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1378 if (real_target_type == TypeManager.char_type)
1379 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1380 } else if (expr_type == TypeManager.short_type){
1382 // From short to sbyte, byte, ushort, uint, ulong, char
1384 if (real_target_type == TypeManager.sbyte_type)
1385 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1386 if (real_target_type == TypeManager.byte_type)
1387 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1388 if (real_target_type == TypeManager.ushort_type)
1389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1390 if (real_target_type == TypeManager.uint32_type)
1391 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1392 if (real_target_type == TypeManager.uint64_type)
1393 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1394 if (real_target_type == TypeManager.char_type)
1395 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1396 } else if (expr_type == TypeManager.ushort_type){
1398 // From ushort to sbyte, byte, short, char
1400 if (real_target_type == TypeManager.sbyte_type)
1401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1402 if (real_target_type == TypeManager.byte_type)
1403 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1404 if (real_target_type == TypeManager.short_type)
1405 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1406 if (real_target_type == TypeManager.char_type)
1407 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1408 } else if (expr_type == TypeManager.int32_type){
1410 // From int to sbyte, byte, short, ushort, uint, ulong, char
1412 if (real_target_type == TypeManager.sbyte_type)
1413 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1414 if (real_target_type == TypeManager.byte_type)
1415 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1416 if (real_target_type == TypeManager.short_type)
1417 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1418 if (real_target_type == TypeManager.ushort_type)
1419 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1420 if (real_target_type == TypeManager.uint32_type)
1421 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1422 if (real_target_type == TypeManager.uint64_type)
1423 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1424 if (real_target_type == TypeManager.char_type)
1425 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1426 } else if (expr_type == TypeManager.uint32_type){
1428 // From uint to sbyte, byte, short, ushort, int, char
1430 if (real_target_type == TypeManager.sbyte_type)
1431 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1432 if (real_target_type == TypeManager.byte_type)
1433 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1434 if (real_target_type == TypeManager.short_type)
1435 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1436 if (real_target_type == TypeManager.ushort_type)
1437 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1438 if (real_target_type == TypeManager.int32_type)
1439 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1440 if (real_target_type == TypeManager.char_type)
1441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1442 } else if (expr_type == TypeManager.int64_type){
1444 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1446 if (real_target_type == TypeManager.sbyte_type)
1447 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1448 if (real_target_type == TypeManager.byte_type)
1449 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1450 if (real_target_type == TypeManager.short_type)
1451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1452 if (real_target_type == TypeManager.ushort_type)
1453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1454 if (real_target_type == TypeManager.int32_type)
1455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1456 if (real_target_type == TypeManager.uint32_type)
1457 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1458 if (real_target_type == TypeManager.uint64_type)
1459 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1460 if (real_target_type == TypeManager.char_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1462 } else if (expr_type == TypeManager.uint64_type){
1464 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1466 if (real_target_type == TypeManager.sbyte_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1468 if (real_target_type == TypeManager.byte_type)
1469 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1470 if (real_target_type == TypeManager.short_type)
1471 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1472 if (real_target_type == TypeManager.ushort_type)
1473 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1474 if (real_target_type == TypeManager.int32_type)
1475 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1476 if (real_target_type == TypeManager.uint32_type)
1477 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1478 if (real_target_type == TypeManager.int64_type)
1479 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1480 if (real_target_type == TypeManager.char_type)
1481 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1482 } else if (expr_type == TypeManager.char_type){
1484 // From char to sbyte, byte, short
1486 if (real_target_type == TypeManager.sbyte_type)
1487 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1488 if (real_target_type == TypeManager.byte_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1490 if (real_target_type == TypeManager.short_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1492 } else if (expr_type == TypeManager.float_type){
1494 // From float to sbyte, byte, short,
1495 // ushort, int, uint, long, ulong, char
1498 if (real_target_type == TypeManager.sbyte_type)
1499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1500 if (real_target_type == TypeManager.byte_type)
1501 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1502 if (real_target_type == TypeManager.short_type)
1503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1504 if (real_target_type == TypeManager.ushort_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1506 if (real_target_type == TypeManager.int32_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1508 if (real_target_type == TypeManager.uint32_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1510 if (real_target_type == TypeManager.int64_type)
1511 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1512 if (real_target_type == TypeManager.uint64_type)
1513 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1514 if (real_target_type == TypeManager.char_type)
1515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1516 if (real_target_type == TypeManager.decimal_type)
1517 return InternalTypeConstructor (ec, expr, target_type);
1518 } else if (expr_type == TypeManager.double_type){
1520 // From double to byte, byte, short,
1521 // ushort, int, uint, long, ulong,
1522 // char, float or decimal
1524 if (real_target_type == TypeManager.sbyte_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1526 if (real_target_type == TypeManager.byte_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1528 if (real_target_type == TypeManager.short_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1530 if (real_target_type == TypeManager.ushort_type)
1531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1532 if (real_target_type == TypeManager.int32_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1534 if (real_target_type == TypeManager.uint32_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1536 if (real_target_type == TypeManager.int64_type)
1537 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1538 if (real_target_type == TypeManager.uint64_type)
1539 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1540 if (real_target_type == TypeManager.char_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1542 if (real_target_type == TypeManager.float_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1544 if (real_target_type == TypeManager.decimal_type)
1545 return InternalTypeConstructor (ec, expr, target_type);
1548 // decimal is taken care of by the op_Explicit methods.
1554 /// Returns whether an explicit reference conversion can be performed
1555 /// from source_type to target_type
1557 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1559 bool target_is_value_type = target_type.IsValueType;
1561 if (source_type == target_type)
1565 // From object to any reference type
1567 if (source_type == TypeManager.object_type && !target_is_value_type)
1571 // From any class S to any class-type T, provided S is a base class of T
1573 if (target_type.IsSubclassOf (source_type))
1577 // From any interface type S to any interface T provided S is not derived from T
1579 if (source_type.IsInterface && target_type.IsInterface){
1580 if (!target_type.IsSubclassOf (source_type))
1585 // From any class type S to any interface T, provides S is not sealed
1586 // and provided S does not implement T.
1588 if (target_type.IsInterface && !source_type.IsSealed &&
1589 !target_type.IsAssignableFrom (source_type))
1593 // From any interface-type S to to any class type T, provided T is not
1594 // sealed, or provided T implements S.
1596 if (source_type.IsInterface &&
1597 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1600 // From an array type S with an element type Se to an array type T with an
1601 // element type Te provided all the following are true:
1602 // * S and T differe only in element type, in other words, S and T
1603 // have the same number of dimensions.
1604 // * Both Se and Te are reference types
1605 // * An explicit referenc conversions exist from Se to Te
1607 if (source_type.IsArray && target_type.IsArray) {
1608 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1610 Type source_element_type = source_type.GetElementType ();
1611 Type target_element_type = target_type.GetElementType ();
1613 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1614 if (ExplicitReferenceConversionExists (source_element_type,
1615 target_element_type))
1621 // From System.Array to any array-type
1622 if (source_type == TypeManager.array_type &&
1623 target_type.IsSubclassOf (TypeManager.array_type)){
1628 // From System delegate to any delegate-type
1630 if (source_type == TypeManager.delegate_type &&
1631 target_type.IsSubclassOf (TypeManager.delegate_type))
1635 // From ICloneable to Array or Delegate types
1637 if (source_type == TypeManager.icloneable_type &&
1638 (target_type == TypeManager.array_type ||
1639 target_type == TypeManager.delegate_type))
1646 /// Implements Explicit Reference conversions
1648 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1650 Type source_type = source.Type;
1651 bool target_is_value_type = target_type.IsValueType;
1654 // From object to any reference type
1656 if (source_type == TypeManager.object_type && !target_is_value_type)
1657 return new ClassCast (source, target_type);
1661 // From any class S to any class-type T, provided S is a base class of T
1663 if (target_type.IsSubclassOf (source_type))
1664 return new ClassCast (source, target_type);
1667 // From any interface type S to any interface T provided S is not derived from T
1669 if (source_type.IsInterface && target_type.IsInterface){
1670 if (TypeManager.ImplementsInterface (source_type, target_type))
1673 return new ClassCast (source, target_type);
1677 // From any class type S to any interface T, provides S is not sealed
1678 // and provided S does not implement T.
1680 if (target_type.IsInterface && !source_type.IsSealed) {
1682 if (TypeManager.ImplementsInterface (source_type, target_type))
1685 return new ClassCast (source, target_type);
1690 // From any interface-type S to to any class type T, provided T is not
1691 // sealed, or provided T implements S.
1693 if (source_type.IsInterface) {
1695 if (target_type.IsSealed)
1698 if (TypeManager.ImplementsInterface (target_type, source_type))
1699 return new ClassCast (source, target_type);
1704 // From an array type S with an element type Se to an array type T with an
1705 // element type Te provided all the following are true:
1706 // * S and T differe only in element type, in other words, S and T
1707 // have the same number of dimensions.
1708 // * Both Se and Te are reference types
1709 // * An explicit referenc conversions exist from Se to Te
1711 if (source_type.IsArray && target_type.IsArray) {
1712 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1714 Type source_element_type = source_type.GetElementType ();
1715 Type target_element_type = target_type.GetElementType ();
1717 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1718 if (ExplicitReferenceConversionExists (source_element_type,
1719 target_element_type))
1720 return new ClassCast (source, target_type);
1725 // From System.Array to any array-type
1726 if (source_type == TypeManager.array_type &&
1727 target_type.IsSubclassOf (TypeManager.array_type)){
1728 return new ClassCast (source, target_type);
1732 // From System delegate to any delegate-type
1734 if (source_type == TypeManager.delegate_type &&
1735 target_type.IsSubclassOf (TypeManager.delegate_type))
1736 return new ClassCast (source, target_type);
1739 // From ICloneable to Array or Delegate types
1741 if (source_type == TypeManager.icloneable_type &&
1742 (target_type == TypeManager.array_type ||
1743 target_type == TypeManager.delegate_type))
1744 return new ClassCast (source, target_type);
1750 /// Performs an explicit conversion of the expression `expr' whose
1751 /// type is expr.Type to `target_type'.
1753 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1754 Type target_type, Location loc)
1756 Type expr_type = expr.Type;
1757 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1762 ne = ConvertNumericExplicit (ec, expr, target_type);
1767 // Unboxing conversion.
1769 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1770 return new UnboxCast (expr, target_type);
1775 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1779 // FIXME: Is there any reason we should have EnumConstant
1780 // dealt with here instead of just using always the
1781 // UnderlyingSystemType to wrap the type?
1783 if (expr is EnumConstant)
1784 e = ((EnumConstant) expr).Child;
1786 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1789 Expression t = ConvertImplicit (ec, e, target_type, loc);
1793 return ConvertNumericExplicit (ec, e, target_type);
1796 ne = ConvertReferenceExplicit (expr, target_type);
1801 if (target_type.IsPointer){
1802 if (expr_type.IsPointer)
1803 return new EmptyCast (expr, target_type);
1805 if (expr_type == TypeManager.sbyte_type ||
1806 expr_type == TypeManager.byte_type ||
1807 expr_type == TypeManager.short_type ||
1808 expr_type == TypeManager.ushort_type ||
1809 expr_type == TypeManager.int32_type ||
1810 expr_type == TypeManager.uint32_type ||
1811 expr_type == TypeManager.uint64_type ||
1812 expr_type == TypeManager.int64_type)
1813 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1815 if (expr_type.IsPointer){
1816 if (target_type == TypeManager.sbyte_type ||
1817 target_type == TypeManager.byte_type ||
1818 target_type == TypeManager.short_type ||
1819 target_type == TypeManager.ushort_type ||
1820 target_type == TypeManager.int32_type ||
1821 target_type == TypeManager.uint32_type ||
1822 target_type == TypeManager.uint64_type ||
1823 target_type == TypeManager.int64_type){
1824 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1827 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1832 ce = ConvertNumericExplicit (ec, e, target_type);
1836 // We should always be able to go from an uint32
1837 // implicitly or explicitly to the other integral
1840 throw new Exception ("Internal compiler error");
1845 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1849 Error_CannotConvertType (loc, expr_type, target_type);
1854 /// Same as ConverExplicit, only it doesn't include user defined conversions
1856 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1857 Type target_type, Location l)
1859 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1864 ne = ConvertNumericExplicit (ec, expr, target_type);
1868 ne = ConvertReferenceExplicit (expr, target_type);
1872 Error_CannotConvertType (l, expr.Type, target_type);
1876 static string ExprClassName (ExprClass c)
1879 case ExprClass.Invalid:
1881 case ExprClass.Value:
1883 case ExprClass.Variable:
1885 case ExprClass.Namespace:
1887 case ExprClass.Type:
1889 case ExprClass.MethodGroup:
1890 return "method group";
1891 case ExprClass.PropertyAccess:
1892 return "property access";
1893 case ExprClass.EventAccess:
1894 return "event access";
1895 case ExprClass.IndexerAccess:
1896 return "indexer access";
1897 case ExprClass.Nothing:
1900 throw new Exception ("Should not happen");
1904 /// Reports that we were expecting `expr' to be of class `expected'
1906 protected void report118 (Location loc, Expression expr, string expected)
1908 string kind = "Unknown";
1911 kind = ExprClassName (expr.eclass);
1913 Error (118, loc, "Expression denotes a `" + kind +
1914 "' where a `" + expected + "' was expected");
1917 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
1919 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1920 TypeManager.CSharpName (t));
1923 public static void UnsafeError (Location loc)
1925 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
1929 /// Converts the IntConstant, UIntConstant, LongConstant or
1930 /// ULongConstant into the integral target_type. Notice
1931 /// that we do not return an `Expression' we do return
1932 /// a boxed integral type.
1934 /// FIXME: Since I added the new constants, we need to
1935 /// also support conversions from CharConstant, ByteConstant,
1936 /// SByteConstant, UShortConstant, ShortConstant
1938 /// This is used by the switch statement, so the domain
1939 /// of work is restricted to the literals above, and the
1940 /// targets are int32, uint32, char, byte, sbyte, ushort,
1941 /// short, uint64 and int64
1943 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1947 if (c.Type == target_type)
1948 return ((Constant) c).GetValue ();
1951 // Make into one of the literals we handle, we dont really care
1952 // about this value as we will just return a few limited types
1954 if (c is EnumConstant)
1955 c = ((EnumConstant)c).WidenToCompilerConstant ();
1957 if (c is IntConstant){
1958 int v = ((IntConstant) c).Value;
1960 if (target_type == TypeManager.uint32_type){
1963 } else if (target_type == TypeManager.char_type){
1964 if (v >= Char.MinValue && v <= Char.MaxValue)
1966 } else if (target_type == TypeManager.byte_type){
1967 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1969 } else if (target_type == TypeManager.sbyte_type){
1970 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1972 } else if (target_type == TypeManager.short_type){
1973 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1975 } else if (target_type == TypeManager.ushort_type){
1976 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1978 } else if (target_type == TypeManager.int64_type)
1980 else if (target_type == TypeManager.uint64_type){
1986 } else if (c is UIntConstant){
1987 uint v = ((UIntConstant) c).Value;
1989 if (target_type == TypeManager.int32_type){
1990 if (v <= Int32.MaxValue)
1992 } else if (target_type == TypeManager.char_type){
1993 if (v >= Char.MinValue && v <= Char.MaxValue)
1995 } else if (target_type == TypeManager.byte_type){
1996 if (v <= Byte.MaxValue)
1998 } else if (target_type == TypeManager.sbyte_type){
1999 if (v <= SByte.MaxValue)
2001 } else if (target_type == TypeManager.short_type){
2002 if (v <= UInt16.MaxValue)
2004 } else if (target_type == TypeManager.ushort_type){
2005 if (v <= UInt16.MaxValue)
2007 } else if (target_type == TypeManager.int64_type)
2009 else if (target_type == TypeManager.uint64_type)
2012 } else if (c is LongConstant){
2013 long v = ((LongConstant) c).Value;
2015 if (target_type == TypeManager.int32_type){
2016 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2018 } else if (target_type == TypeManager.uint32_type){
2019 if (v >= 0 && v <= UInt32.MaxValue)
2021 } else if (target_type == TypeManager.char_type){
2022 if (v >= Char.MinValue && v <= Char.MaxValue)
2024 } else if (target_type == TypeManager.byte_type){
2025 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2027 } else if (target_type == TypeManager.sbyte_type){
2028 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2030 } else if (target_type == TypeManager.short_type){
2031 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2033 } else if (target_type == TypeManager.ushort_type){
2034 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2036 } else if (target_type == TypeManager.uint64_type){
2041 } else if (c is ULongConstant){
2042 ulong v = ((ULongConstant) c).Value;
2044 if (target_type == TypeManager.int32_type){
2045 if (v <= Int32.MaxValue)
2047 } else if (target_type == TypeManager.uint32_type){
2048 if (v <= UInt32.MaxValue)
2050 } else if (target_type == TypeManager.char_type){
2051 if (v >= Char.MinValue && v <= Char.MaxValue)
2053 } else if (target_type == TypeManager.byte_type){
2054 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2056 } else if (target_type == TypeManager.sbyte_type){
2057 if (v <= (int) SByte.MaxValue)
2059 } else if (target_type == TypeManager.short_type){
2060 if (v <= UInt16.MaxValue)
2062 } else if (target_type == TypeManager.ushort_type){
2063 if (v <= UInt16.MaxValue)
2065 } else if (target_type == TypeManager.int64_type){
2066 if (v <= Int64.MaxValue)
2070 } else if (c is ByteConstant){
2071 byte v = ((ByteConstant) c).Value;
2073 if (target_type == TypeManager.int32_type)
2075 else if (target_type == TypeManager.uint32_type)
2077 else if (target_type == TypeManager.char_type)
2079 else if (target_type == TypeManager.sbyte_type){
2080 if (v <= SByte.MaxValue)
2082 } else if (target_type == TypeManager.short_type)
2084 else if (target_type == TypeManager.ushort_type)
2086 else if (target_type == TypeManager.int64_type)
2088 else if (target_type == TypeManager.uint64_type)
2091 } else if (c is SByteConstant){
2092 sbyte v = ((SByteConstant) c).Value;
2094 if (target_type == TypeManager.int32_type)
2096 else if (target_type == TypeManager.uint32_type){
2099 } else if (target_type == TypeManager.char_type){
2102 } else if (target_type == TypeManager.byte_type){
2105 } else if (target_type == TypeManager.short_type)
2107 else if (target_type == TypeManager.ushort_type){
2110 } else if (target_type == TypeManager.int64_type)
2112 else if (target_type == TypeManager.uint64_type){
2117 } else if (c is ShortConstant){
2118 short v = ((ShortConstant) c).Value;
2120 if (target_type == TypeManager.int32_type){
2122 } else if (target_type == TypeManager.uint32_type){
2125 } else if (target_type == TypeManager.char_type){
2128 } else if (target_type == TypeManager.byte_type){
2129 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2131 } else if (target_type == TypeManager.sbyte_type){
2132 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2134 } else if (target_type == TypeManager.ushort_type){
2137 } else if (target_type == TypeManager.int64_type)
2139 else if (target_type == TypeManager.uint64_type)
2143 } else if (c is UShortConstant){
2144 ushort v = ((UShortConstant) c).Value;
2146 if (target_type == TypeManager.int32_type)
2148 else if (target_type == TypeManager.uint32_type)
2150 else if (target_type == TypeManager.char_type){
2151 if (v >= Char.MinValue && v <= Char.MaxValue)
2153 } else if (target_type == TypeManager.byte_type){
2154 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2156 } else if (target_type == TypeManager.sbyte_type){
2157 if (v <= SByte.MaxValue)
2159 } else if (target_type == TypeManager.short_type){
2160 if (v <= Int16.MaxValue)
2162 } else if (target_type == TypeManager.int64_type)
2164 else if (target_type == TypeManager.uint64_type)
2168 } else if (c is CharConstant){
2169 char v = ((CharConstant) c).Value;
2171 if (target_type == TypeManager.int32_type)
2173 else if (target_type == TypeManager.uint32_type)
2175 else if (target_type == TypeManager.byte_type){
2176 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2178 } else if (target_type == TypeManager.sbyte_type){
2179 if (v <= SByte.MaxValue)
2181 } else if (target_type == TypeManager.short_type){
2182 if (v <= Int16.MaxValue)
2184 } else if (target_type == TypeManager.ushort_type)
2186 else if (target_type == TypeManager.int64_type)
2188 else if (target_type == TypeManager.uint64_type)
2193 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2198 // Load the object from the pointer. The `IsReference' is used
2199 // to control whether we should use Ldind_Ref or LdObj if the
2200 // value is not a `core' type.
2202 // Maybe we should try to extract this infromation form the type?
2203 // TODO: Maybe this is a bug. The reason we have this flag is because
2204 // I had almost identical code in ParameterReference (for handling
2205 // references) and in UnboxCast.
2207 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2209 if (t == TypeManager.int32_type)
2210 ig.Emit (OpCodes.Ldind_I4);
2211 else if (t == TypeManager.uint32_type)
2212 ig.Emit (OpCodes.Ldind_U4);
2213 else if (t == TypeManager.short_type)
2214 ig.Emit (OpCodes.Ldind_I2);
2215 else if (t == TypeManager.ushort_type)
2216 ig.Emit (OpCodes.Ldind_U2);
2217 else if (t == TypeManager.char_type)
2218 ig.Emit (OpCodes.Ldind_U2);
2219 else if (t == TypeManager.byte_type)
2220 ig.Emit (OpCodes.Ldind_U1);
2221 else if (t == TypeManager.sbyte_type)
2222 ig.Emit (OpCodes.Ldind_I1);
2223 else if (t == TypeManager.uint64_type)
2224 ig.Emit (OpCodes.Ldind_I8);
2225 else if (t == TypeManager.int64_type)
2226 ig.Emit (OpCodes.Ldind_I8);
2227 else if (t == TypeManager.float_type)
2228 ig.Emit (OpCodes.Ldind_R4);
2229 else if (t == TypeManager.double_type)
2230 ig.Emit (OpCodes.Ldind_R8);
2231 else if (t == TypeManager.bool_type)
2232 ig.Emit (OpCodes.Ldind_I1);
2233 else if (t == TypeManager.intptr_type)
2234 ig.Emit (OpCodes.Ldind_I);
2235 else if (TypeManager.IsEnumType (t)){
2236 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2239 ig.Emit (OpCodes.Ldind_Ref);
2241 ig.Emit (OpCodes.Ldobj, t);
2246 // The stack contains the pointer and the value of type `type'
2248 public static void StoreFromPtr (ILGenerator ig, Type type)
2250 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2251 ig.Emit (OpCodes.Stind_I4);
2252 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2253 ig.Emit (OpCodes.Stind_I8);
2254 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2255 type == TypeManager.ushort_type)
2256 ig.Emit (OpCodes.Stind_I2);
2257 else if (type == TypeManager.float_type)
2258 ig.Emit (OpCodes.Stind_R4);
2259 else if (type == TypeManager.double_type)
2260 ig.Emit (OpCodes.Stind_R8);
2261 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2262 type == TypeManager.bool_type)
2263 ig.Emit (OpCodes.Stind_I1);
2264 else if (type == TypeManager.intptr_type)
2265 ig.Emit (OpCodes.Stind_I);
2267 ig.Emit (OpCodes.Stind_Ref);
2271 // Returns the size of type `t' if known, otherwise, 0
2273 public static int GetTypeSize (Type t)
2275 if (t == TypeManager.int32_type ||
2276 t == TypeManager.uint32_type ||
2277 t == TypeManager.float_type)
2279 else if (t == TypeManager.int64_type ||
2280 t == TypeManager.uint64_type ||
2281 t == TypeManager.double_type)
2283 else if (t == TypeManager.byte_type ||
2284 t == TypeManager.sbyte_type ||
2285 t == TypeManager.bool_type)
2287 else if (t == TypeManager.short_type ||
2288 t == TypeManager.char_type ||
2289 t == TypeManager.ushort_type)
2297 /// This is just a base class for expressions that can
2298 /// appear on statements (invocations, object creation,
2299 /// assignments, post/pre increment and decrement). The idea
2300 /// being that they would support an extra Emition interface that
2301 /// does not leave a result on the stack.
2303 public abstract class ExpressionStatement : Expression {
2306 /// Requests the expression to be emitted in a `statement'
2307 /// context. This means that no new value is left on the
2308 /// stack after invoking this method (constrasted with
2309 /// Emit that will always leave a value on the stack).
2311 public abstract void EmitStatement (EmitContext ec);
2315 /// This kind of cast is used to encapsulate the child
2316 /// whose type is child.Type into an expression that is
2317 /// reported to return "return_type". This is used to encapsulate
2318 /// expressions which have compatible types, but need to be dealt
2319 /// at higher levels with.
2321 /// For example, a "byte" expression could be encapsulated in one
2322 /// of these as an "unsigned int". The type for the expression
2323 /// would be "unsigned int".
2326 public class EmptyCast : Expression {
2327 protected Expression child;
2329 public EmptyCast (Expression child, Type return_type)
2331 eclass = child.eclass;
2336 public override Expression DoResolve (EmitContext ec)
2338 // This should never be invoked, we are born in fully
2339 // initialized state.
2344 public override void Emit (EmitContext ec)
2351 /// This class is used to wrap literals which belong inside Enums
2353 public class EnumConstant : Constant {
2354 public Constant Child;
2356 public EnumConstant (Constant child, Type enum_type)
2358 eclass = child.eclass;
2363 public override Expression DoResolve (EmitContext ec)
2365 // This should never be invoked, we are born in fully
2366 // initialized state.
2371 public override void Emit (EmitContext ec)
2376 public override object GetValue ()
2378 return Child.GetValue ();
2382 // Converts from one of the valid underlying types for an enumeration
2383 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2384 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2386 public Constant WidenToCompilerConstant ()
2388 Type t = TypeManager.EnumToUnderlying (Child.Type);
2389 object v = ((Constant) Child).GetValue ();;
2391 if (t == TypeManager.int32_type)
2392 return new IntConstant ((int) v);
2393 if (t == TypeManager.uint32_type)
2394 return new UIntConstant ((uint) v);
2395 if (t == TypeManager.int64_type)
2396 return new LongConstant ((long) v);
2397 if (t == TypeManager.uint64_type)
2398 return new ULongConstant ((ulong) v);
2399 if (t == TypeManager.short_type)
2400 return new ShortConstant ((short) v);
2401 if (t == TypeManager.ushort_type)
2402 return new UShortConstant ((ushort) v);
2403 if (t == TypeManager.byte_type)
2404 return new ByteConstant ((byte) v);
2405 if (t == TypeManager.sbyte_type)
2406 return new SByteConstant ((sbyte) v);
2408 throw new Exception ("Invalid enumeration underlying type: " + t);
2412 // Extracts the value in the enumeration on its native representation
2414 public object GetPlainValue ()
2416 Type t = TypeManager.EnumToUnderlying (Child.Type);
2417 object v = ((Constant) Child).GetValue ();;
2419 if (t == TypeManager.int32_type)
2421 if (t == TypeManager.uint32_type)
2423 if (t == TypeManager.int64_type)
2425 if (t == TypeManager.uint64_type)
2427 if (t == TypeManager.short_type)
2429 if (t == TypeManager.ushort_type)
2431 if (t == TypeManager.byte_type)
2433 if (t == TypeManager.sbyte_type)
2439 public override string AsString ()
2441 return Child.AsString ();
2444 public override DoubleConstant ConvertToDouble ()
2446 return Child.ConvertToDouble ();
2449 public override FloatConstant ConvertToFloat ()
2451 return Child.ConvertToFloat ();
2454 public override ULongConstant ConvertToULong ()
2456 return Child.ConvertToULong ();
2459 public override LongConstant ConvertToLong ()
2461 return Child.ConvertToLong ();
2464 public override UIntConstant ConvertToUInt ()
2466 return Child.ConvertToUInt ();
2469 public override IntConstant ConvertToInt ()
2471 return Child.ConvertToInt ();
2476 /// This kind of cast is used to encapsulate Value Types in objects.
2478 /// The effect of it is to box the value type emitted by the previous
2481 public class BoxedCast : EmptyCast {
2483 public BoxedCast (Expression expr)
2484 : base (expr, TypeManager.object_type)
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)
2500 ec.ig.Emit (OpCodes.Box, child.Type);
2504 public class UnboxCast : EmptyCast {
2505 public UnboxCast (Expression expr, Type return_type)
2506 : base (expr, return_type)
2510 public override Expression DoResolve (EmitContext ec)
2512 // This should never be invoked, we are born in fully
2513 // initialized state.
2518 public override void Emit (EmitContext ec)
2521 ILGenerator ig = ec.ig;
2524 ig.Emit (OpCodes.Unbox, t);
2526 LoadFromPtr (ig, t, false);
2531 /// This is used to perform explicit numeric conversions.
2533 /// Explicit numeric conversions might trigger exceptions in a checked
2534 /// context, so they should generate the conv.ovf opcodes instead of
2537 public class ConvCast : EmptyCast {
2538 public enum Mode : byte {
2539 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2541 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2542 U2_I1, U2_U1, U2_I2, U2_CH,
2543 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2544 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2545 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2546 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2547 CH_I1, CH_U1, CH_I2,
2548 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2549 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2555 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2556 : base (child, return_type)
2559 checked_state = ec.CheckState;
2562 public override Expression DoResolve (EmitContext ec)
2564 // This should never be invoked, we are born in fully
2565 // initialized state.
2570 public override void Emit (EmitContext ec)
2572 ILGenerator ig = ec.ig;
2578 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2579 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2580 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2581 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2582 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2584 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2585 case Mode.U1_CH: /* nothing */ break;
2587 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2588 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2589 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2590 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2591 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2592 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2594 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2595 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2596 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2597 case Mode.U2_CH: /* nothing */ break;
2599 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2600 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2601 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2602 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2603 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2604 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2605 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2607 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2608 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2609 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2610 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2611 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2612 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2614 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2615 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2616 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2617 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2618 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2619 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2620 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2621 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2623 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2624 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2625 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2626 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2627 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2628 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2629 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2630 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2632 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2633 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2634 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2636 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2637 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2638 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2639 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2640 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2641 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2642 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2643 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2644 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2646 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2647 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2648 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2649 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2650 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2651 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2652 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2653 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2654 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2655 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2659 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2660 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2661 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2662 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2663 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2665 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2666 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2668 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2669 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2670 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2671 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2672 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2673 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2675 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2676 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2677 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2678 case Mode.U2_CH: /* nothing */ break;
2680 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2681 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2682 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2683 case Mode.I4_U4: /* nothing */ break;
2684 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2685 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2686 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2688 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2689 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2690 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2691 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2692 case Mode.U4_I4: /* nothing */ break;
2693 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2695 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2696 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2697 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2698 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2699 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2700 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2701 case Mode.I8_U8: /* nothing */ break;
2702 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2704 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2705 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2706 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2707 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2708 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2709 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2710 case Mode.U8_I8: /* nothing */ break;
2711 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2713 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2714 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2715 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2717 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2718 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2719 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2720 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2721 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2722 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2723 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2724 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2725 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2727 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2728 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2729 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2730 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2731 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2732 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2733 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2734 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2735 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2736 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2742 public class OpcodeCast : EmptyCast {
2746 public OpcodeCast (Expression child, Type return_type, OpCode op)
2747 : base (child, return_type)
2751 second_valid = false;
2754 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2755 : base (child, return_type)
2760 second_valid = true;
2763 public override Expression DoResolve (EmitContext ec)
2765 // This should never be invoked, we are born in fully
2766 // initialized state.
2771 public override void Emit (EmitContext ec)
2782 /// This kind of cast is used to encapsulate a child and cast it
2783 /// to the class requested
2785 public class ClassCast : EmptyCast {
2786 public ClassCast (Expression child, Type return_type)
2787 : base (child, return_type)
2792 public override Expression DoResolve (EmitContext ec)
2794 // This should never be invoked, we are born in fully
2795 // initialized state.
2800 public override void Emit (EmitContext ec)
2804 ec.ig.Emit (OpCodes.Castclass, type);
2810 /// SimpleName expressions are initially formed of a single
2811 /// word and it only happens at the beginning of the expression.
2815 /// The expression will try to be bound to a Field, a Method
2816 /// group or a Property. If those fail we pass the name to our
2817 /// caller and the SimpleName is compounded to perform a type
2818 /// lookup. The idea behind this process is that we want to avoid
2819 /// creating a namespace map from the assemblies, as that requires
2820 /// the GetExportedTypes function to be called and a hashtable to
2821 /// be constructed which reduces startup time. If later we find
2822 /// that this is slower, we should create a `NamespaceExpr' expression
2823 /// that fully participates in the resolution process.
2825 /// For example `System.Console.WriteLine' is decomposed into
2826 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2828 /// The first SimpleName wont produce a match on its own, so it will
2830 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2832 /// System.Console will produce a TypeExpr match.
2834 /// The downside of this is that we might be hitting `LookupType' too many
2835 /// times with this scheme.
2837 public class SimpleName : Expression {
2838 public readonly string Name;
2839 public readonly Location Location;
2841 public SimpleName (string name, Location l)
2847 public static void Error120 (Location l, string name)
2851 "An object reference is required " +
2852 "for the non-static field `"+name+"'");
2856 // Checks whether we are trying to access an instance
2857 // property, method or field from a static body.
2859 Expression MemberStaticCheck (Expression e)
2861 if (e is FieldExpr){
2862 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2865 Error120 (Location, Name);
2868 } else if (e is MethodGroupExpr){
2869 MethodGroupExpr mg = (MethodGroupExpr) e;
2871 if (!mg.RemoveInstanceMethods ()){
2872 Error120 (Location, mg.Methods [0].Name);
2876 } else if (e is PropertyExpr){
2877 if (!((PropertyExpr) e).IsStatic){
2878 Error120 (Location, Name);
2881 } else if (e is EventExpr) {
2882 if (!((EventExpr) e).IsStatic) {
2883 Error120 (Location, Name);
2891 public override Expression DoResolve (EmitContext ec)
2893 return SimpleNameResolve (ec, false);
2896 public Expression DoResolveAllowStatic (EmitContext ec)
2898 return SimpleNameResolve (ec, true);
2902 /// 7.5.2: Simple Names.
2904 /// Local Variables and Parameters are handled at
2905 /// parse time, so they never occur as SimpleNames.
2907 /// The `allow_static' flag is used by MemberAccess only
2908 /// and it is used to inform us that it is ok for us to
2909 /// avoid the static check, because MemberAccess might end
2910 /// up resolving the Name as a Type name and the access as
2911 /// a static type access.
2913 /// ie: Type Type; .... { Type.GetType (""); }
2915 /// Type is both an instance variable and a Type; Type.GetType
2916 /// is the static method not an instance method of type.
2918 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2920 Expression e = null;
2923 // Stage 1: Performed by the parser (binding to locals or parameters).
2925 if (!ec.OnlyLookupTypes){
2926 Block current_block = ec.CurrentBlock;
2927 if (current_block != null && current_block.IsVariableDefined (Name)){
2928 LocalVariableReference var;
2930 var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
2932 return var.Resolve (ec);
2936 // Stage 2: Lookup members
2940 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
2941 // Hence we have two different cases
2944 DeclSpace lookup_ds = ec.DeclSpace;
2946 if (lookup_ds.TypeBuilder == null)
2949 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
2954 // Classes/structs keep looking, enums break
2956 if (lookup_ds is TypeContainer)
2957 lookup_ds = ((TypeContainer) lookup_ds).Parent;
2960 } while (lookup_ds != null);
2962 if (e == null && ec.ContainerType != null)
2963 e = MemberLookup (ec, ec.ContainerType, Name, Location);
2966 // Continuation of stage 2
2969 // Stage 3: Lookup symbol in the various namespaces.
2971 DeclSpace ds = ec.DeclSpace;
2975 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2976 return new TypeExpr (t);
2979 // Stage 2 part b: Lookup up if we are an alias to a type
2982 // Since we are cheating: we only do the Alias lookup for
2983 // namespaces if the name does not include any dots in it
2986 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2987 // System.Console.WriteLine (Name + " --> " + alias_value);
2988 if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2990 return new TypeExpr (t);
2992 // we have alias value, but it isn't Type, so try if it's namespace
2993 return new SimpleName (alias_value, Location);
2996 // No match, maybe our parent can compose us
2997 // into something meaningful.
3002 // Stage 2 continues here.
3007 if (ec.OnlyLookupTypes)
3010 if (e is FieldExpr){
3011 FieldExpr fe = (FieldExpr) e;
3012 FieldInfo fi = fe.FieldInfo;
3014 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3015 UnsafeError (Location);
3019 if (!allow_static && !fi.IsStatic){
3020 Error120 (Location, Name);
3024 // If we are not in static code and this
3025 // field is not static, set the instance to `this'.
3028 fe.InstanceExpression = ec.This;
3032 if (fi is FieldBuilder) {
3033 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3036 object o = c.LookupConstantValue (ec);
3037 object real_value = ((Constant)c.Expr).GetValue ();
3038 return Constantify (real_value, fi.FieldType);
3043 Type t = fi.FieldType;
3044 Type decl_type = fi.DeclaringType;
3047 if (fi is FieldBuilder)
3048 o = TypeManager.GetValue ((FieldBuilder) fi);
3050 o = fi.GetValue (fi);
3052 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3053 Expression enum_member = MemberLookup (
3054 ec, decl_type, "value__", MemberTypes.Field,
3055 AllBindingFlags, Location);
3057 Enum en = TypeManager.LookupEnum (decl_type);
3061 c = Constantify (o, en.UnderlyingType);
3063 c = Constantify (o, enum_member.Type);
3065 return new EnumConstant (c, decl_type);
3068 Expression exp = Constantify (o, t);
3074 if (e is EventExpr) {
3076 // If the event is local to this class, we transform ourselves into
3079 EventExpr ee = (EventExpr) e;
3081 Expression ml = MemberLookup (
3082 ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3083 MemberTypes.Event, AllBindingFlags, Location);
3086 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3090 // If this happens, then we have an event with its own
3091 // accessors and private field etc so there's no need
3092 // to transform ourselves : we should instead flag an error
3094 Assign.error70 (ee.EventInfo, Location);
3098 ml = ExprClassFromMemberInfo (ec, mi, Location);
3101 Report.Error (-200, Location, "Internal error!!");
3105 Expression instance_expr;
3107 FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3110 instance_expr = null;
3112 instance_expr = ec.This;
3114 instance_expr = instance_expr.Resolve (ec);
3116 if (instance_expr != null)
3117 instance_expr = instance_expr.Resolve (ec);
3119 return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3128 return MemberStaticCheck (e);
3133 public override void Emit (EmitContext ec)
3136 // If this is ever reached, then we failed to
3137 // find the name as a namespace
3140 Error (103, Location, "The name `" + Name +
3141 "' does not exist in the class `" +
3142 ec.DeclSpace.Name + "'");
3147 /// Fully resolved expression that evaluates to a type
3149 public class TypeExpr : Expression {
3150 public TypeExpr (Type t)
3153 eclass = ExprClass.Type;
3156 override public Expression DoResolve (EmitContext ec)
3161 override public void Emit (EmitContext ec)
3163 throw new Exception ("Implement me");
3168 /// MethodGroup Expression.
3170 /// This is a fully resolved expression that evaluates to a type
3172 public class MethodGroupExpr : Expression {
3173 public MethodBase [] Methods;
3175 Expression instance_expression = null;
3177 public MethodGroupExpr (MemberInfo [] mi, Location l)
3179 Methods = new MethodBase [mi.Length];
3180 mi.CopyTo (Methods, 0);
3181 eclass = ExprClass.MethodGroup;
3182 type = TypeManager.object_type;
3186 public MethodGroupExpr (ArrayList list, Location l)
3188 Methods = new MethodBase [list.Count];
3191 list.CopyTo (Methods, 0);
3193 foreach (MemberInfo m in list){
3194 if (!(m is MethodBase)){
3195 Console.WriteLine ("Name " + m.Name);
3196 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3202 eclass = ExprClass.MethodGroup;
3203 type = TypeManager.object_type;
3207 // `A method group may have associated an instance expression'
3209 public Expression InstanceExpression {
3211 return instance_expression;
3215 instance_expression = value;
3219 override public Expression DoResolve (EmitContext ec)
3224 public void ReportUsageError ()
3226 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3227 Methods [0].Name + "()' is referenced without parentheses");
3230 override public void Emit (EmitContext ec)
3232 ReportUsageError ();
3235 bool RemoveMethods (bool keep_static)
3237 ArrayList smethods = new ArrayList ();
3238 int top = Methods.Length;
3241 for (i = 0; i < top; i++){
3242 MethodBase mb = Methods [i];
3244 if (mb.IsStatic == keep_static)
3248 if (smethods.Count == 0)
3251 Methods = new MethodBase [smethods.Count];
3252 smethods.CopyTo (Methods, 0);
3258 /// Removes any instance methods from the MethodGroup, returns
3259 /// false if the resulting set is empty.
3261 public bool RemoveInstanceMethods ()
3263 return RemoveMethods (true);
3267 /// Removes any static methods from the MethodGroup, returns
3268 /// false if the resulting set is empty.
3270 public bool RemoveStaticMethods ()
3272 return RemoveMethods (false);
3277 /// Fully resolved expression that evaluates to a Field
3279 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3280 public readonly FieldInfo FieldInfo;
3281 public Expression InstanceExpression;
3284 public FieldExpr (FieldInfo fi, Location l)
3287 eclass = ExprClass.Variable;
3288 type = fi.FieldType;
3292 override public Expression DoResolve (EmitContext ec)
3294 if (!FieldInfo.IsStatic){
3295 if (InstanceExpression == null){
3296 throw new Exception ("non-static FieldExpr without instance var\n" +
3297 "You have to assign the Instance variable\n" +
3298 "Of the FieldExpr to set this\n");
3301 InstanceExpression = InstanceExpression.Resolve (ec);
3302 if (InstanceExpression == null)
3309 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3311 Expression e = DoResolve (ec);
3316 if (!FieldInfo.IsInitOnly)
3320 // InitOnly fields can only be assigned in constructors
3323 if (ec.IsConstructor)
3326 Report.Error (191, loc,
3327 "Readonly field can not be assigned outside " +
3328 "of constructor or variable initializer");
3333 override public void Emit (EmitContext ec)
3335 ILGenerator ig = ec.ig;
3336 bool is_volatile = false;
3338 if (FieldInfo is FieldBuilder){
3339 Field f = TypeManager.GetField (FieldInfo);
3341 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3344 f.status |= Field.Status.USED;
3348 if (FieldInfo.IsStatic){
3350 ig.Emit (OpCodes.Volatile);
3352 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3354 if (InstanceExpression.Type.IsValueType){
3356 LocalTemporary tempo = null;
3358 if (!(InstanceExpression is IMemoryLocation)){
3359 tempo = new LocalTemporary (
3360 ec, InstanceExpression.Type);
3362 InstanceExpression.Emit (ec);
3366 ml = (IMemoryLocation) InstanceExpression;
3368 ml.AddressOf (ec, AddressOp.Load);
3370 InstanceExpression.Emit (ec);
3373 ig.Emit (OpCodes.Volatile);
3375 ig.Emit (OpCodes.Ldfld, FieldInfo);
3379 public void EmitAssign (EmitContext ec, Expression source)
3381 bool is_static = FieldInfo.IsStatic;
3382 ILGenerator ig = ec.ig;
3385 Expression instance = InstanceExpression;
3387 if (instance.Type.IsValueType){
3388 if (instance is IMemoryLocation){
3389 IMemoryLocation ml = (IMemoryLocation) instance;
3391 ml.AddressOf (ec, AddressOp.Store);
3393 throw new Exception ("The " + instance + " of type " +
3395 " represents a ValueType and does " +
3396 "not implement IMemoryLocation");
3402 if (FieldInfo is FieldBuilder){
3403 Field f = TypeManager.GetField (FieldInfo);
3404 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3405 ig.Emit (OpCodes.Volatile);
3409 ig.Emit (OpCodes.Stsfld, FieldInfo);
3411 ig.Emit (OpCodes.Stfld, FieldInfo);
3413 if (FieldInfo is FieldBuilder){
3414 Field f = TypeManager.GetField (FieldInfo);
3416 f.status |= Field.Status.ASSIGNED;
3420 public void AddressOf (EmitContext ec, AddressOp mode)
3422 ILGenerator ig = ec.ig;
3424 if (FieldInfo is FieldBuilder){
3425 Field f = TypeManager.GetField (FieldInfo);
3426 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3427 ig.Emit (OpCodes.Volatile);
3430 if (FieldInfo is FieldBuilder){
3431 Field f = TypeManager.GetField (FieldInfo);
3433 if ((mode & AddressOp.Store) != 0)
3434 f.status |= Field.Status.ASSIGNED;
3435 if ((mode & AddressOp.Load) != 0)
3436 f.status |= Field.Status.USED;
3440 // Handle initonly fields specially: make a copy and then
3441 // get the address of the copy.
3443 if (FieldInfo.IsInitOnly){
3447 local = ig.DeclareLocal (type);
3448 ig.Emit (OpCodes.Stloc, local);
3449 ig.Emit (OpCodes.Ldloca, local);
3453 if (FieldInfo.IsStatic)
3454 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3456 InstanceExpression.Emit (ec);
3457 ig.Emit (OpCodes.Ldflda, FieldInfo);
3463 /// Expression that evaluates to a Property. The Assign class
3464 /// might set the `Value' expression if we are in an assignment.
3466 /// This is not an LValue because we need to re-write the expression, we
3467 /// can not take data from the stack and store it.
3469 public class PropertyExpr : ExpressionStatement, IAssignMethod {
3470 public readonly PropertyInfo PropertyInfo;
3471 public readonly bool IsStatic;
3473 MethodInfo [] Accessors;
3476 Expression instance_expr;
3478 public PropertyExpr (PropertyInfo pi, Location l)
3481 eclass = ExprClass.PropertyAccess;
3484 Accessors = TypeManager.GetAccessors (pi);
3486 if (Accessors != null)
3487 for (int i = 0; i < Accessors.Length; i++){
3488 if (Accessors [i] != null)
3489 if (Accessors [i].IsStatic)
3493 Accessors = new MethodInfo [2];
3495 type = pi.PropertyType;
3499 // The instance expression associated with this expression
3501 public Expression InstanceExpression {
3503 instance_expr = value;
3507 return instance_expr;
3511 public bool VerifyAssignable ()
3513 if (!PropertyInfo.CanWrite){
3514 Report.Error (200, loc,
3515 "The property `" + PropertyInfo.Name +
3516 "' can not be assigned to, as it has not set accessor");
3523 override public Expression DoResolve (EmitContext ec)
3525 if (!PropertyInfo.CanRead){
3526 Report.Error (154, loc,
3527 "The property `" + PropertyInfo.Name +
3528 "' can not be used in " +
3529 "this context because it lacks a get accessor");
3533 type = PropertyInfo.PropertyType;
3538 override public void Emit (EmitContext ec)
3540 MethodInfo method = Accessors [0];
3543 // Special case: length of single dimension array is turned into ldlen
3545 if (method == TypeManager.int_array_get_length){
3546 Type iet = instance_expr.Type;
3548 if (iet.GetArrayRank () == 1){
3549 instance_expr.Emit (ec);
3550 ec.ig.Emit (OpCodes.Ldlen);
3555 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
3560 // Implements the IAssignMethod interface for assignments
3562 public void EmitAssign (EmitContext ec, Expression source)
3564 Argument arg = new Argument (source, Argument.AType.Expression);
3565 ArrayList args = new ArrayList ();
3568 Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3571 override public void EmitStatement (EmitContext ec)
3574 ec.ig.Emit (OpCodes.Pop);
3579 /// Fully resolved expression that evaluates to an Event
3581 public class EventExpr : Expression {
3582 public readonly EventInfo EventInfo;
3584 public Expression InstanceExpression;
3586 public readonly bool IsStatic;
3588 MethodInfo add_accessor, remove_accessor;
3590 public EventExpr (EventInfo ei, Location loc)
3594 eclass = ExprClass.EventAccess;
3596 add_accessor = TypeManager.GetAddMethod (ei);
3597 remove_accessor = TypeManager.GetRemoveMethod (ei);
3599 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3602 if (EventInfo is MyEventBuilder)
3603 type = ((MyEventBuilder) EventInfo).EventType;
3605 type = EventInfo.EventHandlerType;
3608 override public Expression DoResolve (EmitContext ec)
3610 // We are born fully resolved
3614 override public void Emit (EmitContext ec)
3616 throw new Exception ("Should not happen I think");
3619 public void EmitAddOrRemove (EmitContext ec, Expression source)
3621 Expression handler = ((Binary) source).Right;
3623 Argument arg = new Argument (handler, Argument.AType.Expression);
3624 ArrayList args = new ArrayList ();
3628 if (((Binary) source).Oper == Binary.Operator.Addition)
3629 Invocation.EmitCall (
3630 ec, false, IsStatic, InstanceExpression, add_accessor, args);
3632 Invocation.EmitCall (
3633 ec, false, IsStatic, InstanceExpression, remove_accessor, args);