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 used to tell Resolve in which types of expressions we're
44 public enum ResolveFlags {
45 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
48 // Returns a type expression.
51 // Returns a method group.
54 // Allows SimpleNames to be returned.
55 // This is used by MemberAccess to construct long names that can not be
56 // partially resolved (namespace-qualified names for example).
59 // Mask of all the expression class flags.
62 // Disable control flow analysis while resolving the expression.
63 // This is used when resolving the instance expression of a field expression.
64 DisableFlowAnalysis = 16
68 // This is just as a hint to AddressOf of what will be done with the
71 public enum AddressOp {
78 /// This interface is implemented by variables
80 public interface IMemoryLocation {
82 /// The AddressOf method should generate code that loads
83 /// the address of the object and leaves it on the stack.
85 /// The `mode' argument is used to notify the expression
86 /// of whether this will be used to read from the address or
87 /// write to the address.
89 /// This is just a hint that can be used to provide good error
90 /// reporting, and should have no other side effects.
92 void AddressOf (EmitContext ec, AddressOp mode);
96 /// This interface is implemented by variables
98 public interface IVariable {
100 /// Checks whether the variable has already been assigned at
101 /// the current position of the method's control flow and
102 /// reports an appropriate error message if not.
104 /// If the variable is a struct, then this call checks whether
105 /// all of its fields (including all private ones) have been
108 bool IsAssigned (EmitContext ec, Location loc);
111 /// Checks whether field `name' in this struct has been assigned.
113 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
116 /// Tells the flow analysis code that the variable has already
117 /// been assigned at the current code position.
119 /// If the variable is a struct, this call marks all its fields
120 /// (including private fields) as being assigned.
122 void SetAssigned (EmitContext ec);
125 /// Tells the flow analysis code that field `name' in this struct
126 /// has already been assigned atthe current code position.
128 void SetFieldAssigned (EmitContext ec, string name);
132 /// This interface denotes an expression which evaluates to a member
133 /// of a struct or a class.
135 public interface IMemberExpr
138 /// The name of this member.
145 /// Whether this is an instance member.
152 /// Whether this is a static member.
159 /// The type which declares this member.
166 /// The instance expression associated with this member, if it's a
167 /// non-static member.
169 Expression InstanceExpression {
175 /// Expression which resolves to a type.
177 public interface ITypeExpression
180 /// Resolve the expression, but only lookup types.
182 Expression DoResolveType (EmitContext ec);
186 /// Base class for expressions
188 public abstract class Expression {
189 public ExprClass eclass;
191 protected Location loc;
203 public Location Location {
210 /// Utility wrapper routine for Error, just to beautify the code
212 public void Error (int error, string s)
214 if (!Location.IsNull (loc))
215 Report.Error (error, loc, s);
217 Report.Error (error, s);
221 /// Utility wrapper routine for Warning, just to beautify the code
223 public void Warning (int warning, string s)
225 if (!Location.IsNull (loc))
226 Report.Warning (warning, loc, s);
228 Report.Warning (warning, s);
232 /// Utility wrapper routine for Warning, only prints the warning if
233 /// warnings of level `level' are enabled.
235 public void Warning (int warning, int level, string s)
237 if (level <= RootContext.WarningLevel)
238 Warning (warning, s);
241 static public void Error_CannotConvertType (Location loc, Type source, Type target)
243 Report.Error (30, loc, "Cannot convert type '" +
244 TypeManager.CSharpName (source) + "' to '" +
245 TypeManager.CSharpName (target) + "'");
249 /// Performs semantic analysis on the Expression
253 /// The Resolve method is invoked to perform the semantic analysis
256 /// The return value is an expression (it can be the
257 /// same expression in some cases) or a new
258 /// expression that better represents this node.
260 /// For example, optimizations of Unary (LiteralInt)
261 /// would return a new LiteralInt with a negated
264 /// If there is an error during semantic analysis,
265 /// then an error should be reported (using Report)
266 /// and a null value should be returned.
268 /// There are two side effects expected from calling
269 /// Resolve(): the the field variable "eclass" should
270 /// be set to any value of the enumeration
271 /// `ExprClass' and the type variable should be set
272 /// to a valid type (this is the type of the
275 public abstract Expression DoResolve (EmitContext ec);
277 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
279 return DoResolve (ec);
283 /// Resolves an expression and performs semantic analysis on it.
287 /// Currently Resolve wraps DoResolve to perform sanity
288 /// checking and assertion checking on what we expect from Resolve.
290 public Expression Resolve (EmitContext ec, ResolveFlags flags)
292 // Are we doing a types-only search ?
293 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
294 ITypeExpression type_expr = this as ITypeExpression;
296 if (type_expr == null)
299 return type_expr.DoResolveType (ec);
302 bool old_do_flow_analysis = ec.DoFlowAnalysis;
303 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
304 ec.DoFlowAnalysis = false;
307 if (this is SimpleName)
308 e = ((SimpleName) this).DoResolveAllowStatic (ec);
312 ec.DoFlowAnalysis = old_do_flow_analysis;
317 if (e is SimpleName){
318 SimpleName s = (SimpleName) e;
320 if ((flags & ResolveFlags.SimpleName) == 0) {
322 object lookup = TypeManager.MemberLookup (
323 ec.ContainerType, ec.ContainerType, AllMemberTypes,
324 AllBindingFlags | BindingFlags.NonPublic, s.Name);
326 Error (122, "`" + s.Name + "' " +
327 "is inaccessible because of its protection level");
329 Error (103, "The name `" + s.Name + "' could not be " +
330 "found in `" + ec.DeclSpace.Name + "'");
337 if ((e is TypeExpr) || (e is ComposedCast)) {
338 if ((flags & ResolveFlags.Type) == 0) {
348 if ((flags & ResolveFlags.VariableOrValue) == 0) {
354 case ExprClass.MethodGroup:
355 if ((flags & ResolveFlags.MethodGroup) == 0) {
356 MethodGroupExpr mg = (MethodGroupExpr) e;
357 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
358 Expression te = i.Resolve(ec);
359 //((MethodGroupExpr) e).ReportUsageError ();
365 case ExprClass.Value:
366 case ExprClass.Variable:
367 case ExprClass.PropertyAccess:
368 case ExprClass.EventAccess:
369 case ExprClass.IndexerAccess:
370 if ((flags & ResolveFlags.VariableOrValue) == 0) {
377 throw new Exception ("Expression " + e.GetType () +
378 " ExprClass is Invalid after resolve");
382 throw new Exception (
383 "Expression " + e.GetType () +
384 " did not set its type after Resolve\n" +
385 "called from: " + this.GetType ());
391 /// Resolves an expression and performs semantic analysis on it.
393 public Expression Resolve (EmitContext ec)
395 return Resolve (ec, ResolveFlags.VariableOrValue);
399 /// Resolves an expression for LValue assignment
403 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
404 /// checking and assertion checking on what we expect from Resolve
406 public Expression ResolveLValue (EmitContext ec, Expression right_side)
408 Expression e = DoResolveLValue (ec, right_side);
411 if (e is SimpleName){
412 SimpleName s = (SimpleName) e;
416 "The name `" + s.Name + "' could not be found in `" +
417 ec.DeclSpace.Name + "'");
421 if (e.eclass == ExprClass.Invalid)
422 throw new Exception ("Expression " + e +
423 " ExprClass is Invalid after resolve");
425 if (e.eclass == ExprClass.MethodGroup) {
426 MethodGroupExpr mg = (MethodGroupExpr) e;
427 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
428 Expression te = i.Resolve(ec);
430 //((MethodGroupExpr) e).ReportUsageError ();
435 throw new Exception ("Expression " + e +
436 " did not set its type after Resolve");
443 /// Emits the code for the expression
447 /// The Emit method is invoked to generate the code
448 /// for the expression.
450 public abstract void Emit (EmitContext ec);
453 /// Protected constructor. Only derivate types should
454 /// be able to be created
457 protected Expression ()
459 eclass = ExprClass.Invalid;
464 /// Returns a literalized version of a literal FieldInfo
468 /// The possible return values are:
469 /// IntConstant, UIntConstant
470 /// LongLiteral, ULongConstant
471 /// FloatConstant, DoubleConstant
474 /// The value returned is already resolved.
476 public static Constant Constantify (object v, Type t)
478 if (t == TypeManager.int32_type)
479 return new IntConstant ((int) v);
480 else if (t == TypeManager.uint32_type)
481 return new UIntConstant ((uint) v);
482 else if (t == TypeManager.int64_type)
483 return new LongConstant ((long) v);
484 else if (t == TypeManager.uint64_type)
485 return new ULongConstant ((ulong) v);
486 else if (t == TypeManager.float_type)
487 return new FloatConstant ((float) v);
488 else if (t == TypeManager.double_type)
489 return new DoubleConstant ((double) v);
490 else if (t == TypeManager.string_type)
491 return new StringConstant ((string) v);
492 else if (t == TypeManager.short_type)
493 return new ShortConstant ((short)v);
494 else if (t == TypeManager.ushort_type)
495 return new UShortConstant ((ushort)v);
496 else if (t == TypeManager.sbyte_type)
497 return new SByteConstant (((sbyte)v));
498 else if (t == TypeManager.byte_type)
499 return new ByteConstant ((byte)v);
500 else if (t == TypeManager.char_type)
501 return new CharConstant ((char)v);
502 else if (t == TypeManager.bool_type)
503 return new BoolConstant ((bool) v);
504 else if (TypeManager.IsEnumType (t)){
505 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
507 return new EnumConstant (e, t);
509 throw new Exception ("Unknown type for constant (" + t +
514 /// Returns a fully formed expression after a MemberLookup
516 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
519 return new EventExpr ((EventInfo) mi, loc);
520 else if (mi is FieldInfo)
521 return new FieldExpr ((FieldInfo) mi, loc);
522 else if (mi is PropertyInfo)
523 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
524 else if (mi is Type){
525 return new TypeExpr ((System.Type) mi, loc);
532 // FIXME: Probably implement a cache for (t,name,current_access_set)?
534 // This code could use some optimizations, but we need to do some
535 // measurements. For example, we could use a delegate to `flag' when
536 // something can not any longer be a method-group (because it is something
540 // If the return value is an Array, then it is an array of
543 // If the return value is an MemberInfo, it is anything, but a Method
547 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
548 // the arguments here and have MemberLookup return only the methods that
549 // match the argument count/type, unlike we are doing now (we delay this
552 // This is so we can catch correctly attempts to invoke instance methods
553 // from a static body (scan for error 120 in ResolveSimpleName).
556 // FIXME: Potential optimization, have a static ArrayList
559 public static Expression MemberLookup (EmitContext ec, Type t, string name,
560 MemberTypes mt, BindingFlags bf, Location loc)
562 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
566 // Lookup type `t' for code in class `invocation_type'. Note that it's important
567 // to set `invocation_type' correctly since this method also checks whether the
568 // invoking class is allowed to access the member in class `t'. When you want to
569 // explicitly do a lookup in the base class, you must set both `t' and `invocation_type'
570 // to the base class (although a derived class can access protected members of its base
571 // class it cannot do so through an instance of the base class (error CS1540)).
574 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
575 string name, MemberTypes mt, BindingFlags bf,
578 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
583 int count = mi.Length;
586 return new MethodGroupExpr (mi, loc);
588 if (mi [0] is MethodBase)
589 return new MethodGroupExpr (mi, loc);
591 return ExprClassFromMemberInfo (ec, mi [0], loc);
594 public const MemberTypes AllMemberTypes =
595 MemberTypes.Constructor |
599 MemberTypes.NestedType |
600 MemberTypes.Property;
602 public const BindingFlags AllBindingFlags =
603 BindingFlags.Public |
604 BindingFlags.Static |
605 BindingFlags.Instance |
606 BindingFlags.IgnoreCase;
608 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
610 return MemberLookup (ec, ec.ContainerType, t, name,
611 AllMemberTypes, AllBindingFlags, loc);
614 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
616 return MemberLookup (ec, ec.ContainerType, t, name,
617 MemberTypes.Method, AllBindingFlags, loc);
621 /// This is a wrapper for MemberLookup that is not used to "probe", but
622 /// to find a final definition. If the final definition is not found, we
623 /// look for private members and display a useful debugging message if we
626 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
629 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
632 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
633 MemberTypes mt, BindingFlags bf, Location loc)
637 int errors = Report.Errors;
639 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
644 // Error has already been reported.
645 if (errors < Report.Errors)
648 e = MemberLookup (ec, t, name, AllMemberTypes,
649 AllBindingFlags | BindingFlags.NonPublic, loc);
652 117, loc, "`" + t + "' does not contain a definition " +
653 "for `" + name + "'");
656 122, loc, "`" + t + "." + name +
657 "' is inaccessible due to its protection level");
663 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
665 EventInfo ei = event_expr.EventInfo;
667 return TypeManager.GetPrivateFieldOfEvent (ei);
670 static EmptyExpression MyEmptyExpr;
671 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
673 Type expr_type = expr.Type;
675 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
676 // if we are a method group, emit a warning
682 // notice that it is possible to write "ValueType v = 1", the ValueType here
683 // is an abstract class, and not really a value type, so we apply the same rules.
685 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
687 // A pointer type cannot be converted to object
689 if (expr_type.IsPointer)
692 if (expr_type.IsValueType)
693 return new BoxedCast (expr);
694 if (expr_type.IsClass || expr_type.IsInterface)
695 return new EmptyCast (expr, target_type);
696 } else if (expr_type.IsSubclassOf (target_type)) {
698 // Special case: enumeration to System.Enum.
699 // System.Enum is not a value type, it is a class, so we need
700 // a boxing conversion
702 if (expr_type.IsEnum)
703 return new BoxedCast (expr);
705 return new EmptyCast (expr, target_type);
708 // This code is kind of mirrored inside StandardConversionExists
709 // with the small distinction that we only probe there
711 // Always ensure that the code here and there is in sync
713 // from the null type to any reference-type.
714 if (expr is NullLiteral && !target_type.IsValueType)
715 return new EmptyCast (expr, target_type);
717 // from any class-type S to any interface-type T.
718 if (target_type.IsInterface) {
719 if (TypeManager.ImplementsInterface (expr_type, target_type)){
720 if (expr_type.IsClass)
721 return new EmptyCast (expr, target_type);
722 else if (expr_type.IsValueType)
723 return new BoxedCast (expr);
727 // from any interface type S to interface-type T.
728 if (expr_type.IsInterface && target_type.IsInterface) {
729 if (TypeManager.ImplementsInterface (expr_type, target_type))
730 return new EmptyCast (expr, target_type);
735 // from an array-type S to an array-type of type T
736 if (expr_type.IsArray && target_type.IsArray) {
737 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
739 Type expr_element_type = expr_type.GetElementType ();
741 if (MyEmptyExpr == null)
742 MyEmptyExpr = new EmptyExpression ();
744 MyEmptyExpr.SetType (expr_element_type);
745 Type target_element_type = target_type.GetElementType ();
747 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
748 if (StandardConversionExists (MyEmptyExpr,
749 target_element_type))
750 return new EmptyCast (expr, target_type);
755 // from an array-type to System.Array
756 if (expr_type.IsArray && target_type == TypeManager.array_type)
757 return new EmptyCast (expr, target_type);
759 // from any delegate type to System.Delegate
760 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
761 target_type == TypeManager.delegate_type)
762 return new EmptyCast (expr, target_type);
764 // from any array-type or delegate type into System.ICloneable.
765 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
766 if (target_type == TypeManager.icloneable_type)
767 return new EmptyCast (expr, target_type);
777 /// Implicit Numeric Conversions.
779 /// expr is the expression to convert, returns a new expression of type
780 /// target_type or null if an implicit conversion is not possible.
782 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
783 Type target_type, Location loc)
785 Type expr_type = expr.Type;
788 // Attempt to do the implicit constant expression conversions
790 if (expr is IntConstant){
793 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
797 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
799 // Try the implicit constant expression conversion
800 // from long to ulong, instead of a nice routine,
803 long v = ((LongConstant) expr).Value;
805 return new ULongConstant ((ulong) v);
808 Type real_target_type = target_type;
810 if (expr_type == TypeManager.sbyte_type){
812 // From sbyte to short, int, long, float, double.
814 if (real_target_type == TypeManager.int32_type)
815 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
816 if (real_target_type == TypeManager.int64_type)
817 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
818 if (real_target_type == TypeManager.double_type)
819 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
820 if (real_target_type == TypeManager.float_type)
821 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
822 if (real_target_type == TypeManager.short_type)
823 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
824 } else if (expr_type == TypeManager.byte_type){
826 // From byte to short, ushort, int, uint, long, ulong, float, double
828 if ((real_target_type == TypeManager.short_type) ||
829 (real_target_type == TypeManager.ushort_type) ||
830 (real_target_type == TypeManager.int32_type) ||
831 (real_target_type == TypeManager.uint32_type))
832 return new EmptyCast (expr, target_type);
834 if (real_target_type == TypeManager.uint64_type)
835 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
836 if (real_target_type == TypeManager.int64_type)
837 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
838 if (real_target_type == TypeManager.float_type)
839 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
840 if (real_target_type == TypeManager.double_type)
841 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
842 } else if (expr_type == TypeManager.short_type){
844 // From short to int, long, float, double
846 if (real_target_type == TypeManager.int32_type)
847 return new EmptyCast (expr, target_type);
848 if (real_target_type == TypeManager.int64_type)
849 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
850 if (real_target_type == TypeManager.double_type)
851 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
852 if (real_target_type == TypeManager.float_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
854 } else if (expr_type == TypeManager.ushort_type){
856 // From ushort to int, uint, long, ulong, float, double
858 if (real_target_type == TypeManager.uint32_type)
859 return new EmptyCast (expr, target_type);
861 if (real_target_type == TypeManager.uint64_type)
862 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
863 if (real_target_type == TypeManager.int32_type)
864 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
865 if (real_target_type == TypeManager.int64_type)
866 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
867 if (real_target_type == TypeManager.double_type)
868 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
869 if (real_target_type == TypeManager.float_type)
870 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
871 } else if (expr_type == TypeManager.int32_type){
873 // From int to long, float, double
875 if (real_target_type == TypeManager.int64_type)
876 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
877 if (real_target_type == TypeManager.double_type)
878 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
879 if (real_target_type == TypeManager.float_type)
880 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
881 } else if (expr_type == TypeManager.uint32_type){
883 // From uint to long, ulong, float, double
885 if (real_target_type == TypeManager.int64_type)
886 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
887 if (real_target_type == TypeManager.uint64_type)
888 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
889 if (real_target_type == TypeManager.double_type)
890 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
892 if (real_target_type == TypeManager.float_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
895 } else if (expr_type == TypeManager.int64_type){
897 // From long/ulong to float, double
899 if (real_target_type == TypeManager.double_type)
900 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
901 if (real_target_type == TypeManager.float_type)
902 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
903 } else if (expr_type == TypeManager.uint64_type){
905 // From ulong to float, double
907 if (real_target_type == TypeManager.double_type)
908 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
910 if (real_target_type == TypeManager.float_type)
911 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
913 } else if (expr_type == TypeManager.char_type){
915 // From char to ushort, int, uint, long, ulong, float, double
917 if ((real_target_type == TypeManager.ushort_type) ||
918 (real_target_type == TypeManager.int32_type) ||
919 (real_target_type == TypeManager.uint32_type))
920 return new EmptyCast (expr, target_type);
921 if (real_target_type == TypeManager.uint64_type)
922 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
923 if (real_target_type == TypeManager.int64_type)
924 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
925 if (real_target_type == TypeManager.float_type)
926 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
927 if (real_target_type == TypeManager.double_type)
928 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
929 } else if (expr_type == TypeManager.float_type){
933 if (real_target_type == TypeManager.double_type)
934 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
941 // Tests whether an implicit reference conversion exists between expr_type
944 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
946 Type expr_type = expr.Type;
949 // This is the boxed case.
951 if (target_type == TypeManager.object_type) {
952 if ((expr_type.IsClass) ||
953 (expr_type.IsValueType) ||
954 (expr_type.IsInterface))
957 } else if (expr_type.IsSubclassOf (target_type)) {
960 // Please remember that all code below actually comes
961 // from ImplicitReferenceConversion so make sure code remains in sync
963 // from any class-type S to any interface-type T.
964 if (target_type.IsInterface) {
965 if (TypeManager.ImplementsInterface (expr_type, target_type))
969 // from any interface type S to interface-type T.
970 if (expr_type.IsInterface && target_type.IsInterface)
971 if (TypeManager.ImplementsInterface (expr_type, target_type))
974 // from an array-type S to an array-type of type T
975 if (expr_type.IsArray && target_type.IsArray) {
976 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
978 Type expr_element_type = expr_type.GetElementType ();
980 if (MyEmptyExpr == null)
981 MyEmptyExpr = new EmptyExpression ();
983 MyEmptyExpr.SetType (expr_element_type);
984 Type target_element_type = target_type.GetElementType ();
986 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
987 if (StandardConversionExists (MyEmptyExpr,
988 target_element_type))
993 // from an array-type to System.Array
994 if (expr_type.IsArray && (target_type == TypeManager.array_type))
997 // from any delegate type to System.Delegate
998 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
999 target_type == TypeManager.delegate_type)
1000 if (target_type.IsAssignableFrom (expr_type))
1003 // from any array-type or delegate type into System.ICloneable.
1004 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1005 if (target_type == TypeManager.icloneable_type)
1008 // from the null type to any reference-type.
1009 if (expr is NullLiteral && !target_type.IsValueType &&
1010 !TypeManager.IsEnumType (target_type))
1019 /// Same as StandardConversionExists except that it also looks at
1020 /// implicit user defined conversions - needed for overload resolution
1022 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1024 if (StandardConversionExists (expr, target_type) == true)
1027 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1036 /// Determines if a standard implicit conversion exists from
1037 /// expr_type to target_type
1039 public static bool StandardConversionExists (Expression expr, Type target_type)
1041 Type expr_type = expr.Type;
1043 if (expr_type == TypeManager.void_type)
1046 if (expr_type == target_type)
1049 // First numeric conversions
1051 if (expr_type == TypeManager.sbyte_type){
1053 // From sbyte to short, int, long, float, double.
1055 if ((target_type == TypeManager.int32_type) ||
1056 (target_type == TypeManager.int64_type) ||
1057 (target_type == TypeManager.double_type) ||
1058 (target_type == TypeManager.float_type) ||
1059 (target_type == TypeManager.short_type) ||
1060 (target_type == TypeManager.decimal_type))
1063 } else if (expr_type == TypeManager.byte_type){
1065 // From byte to short, ushort, int, uint, long, ulong, float, double
1067 if ((target_type == TypeManager.short_type) ||
1068 (target_type == TypeManager.ushort_type) ||
1069 (target_type == TypeManager.int32_type) ||
1070 (target_type == TypeManager.uint32_type) ||
1071 (target_type == TypeManager.uint64_type) ||
1072 (target_type == TypeManager.int64_type) ||
1073 (target_type == TypeManager.float_type) ||
1074 (target_type == TypeManager.double_type) ||
1075 (target_type == TypeManager.decimal_type))
1078 } else if (expr_type == TypeManager.short_type){
1080 // From short to int, long, float, double
1082 if ((target_type == TypeManager.int32_type) ||
1083 (target_type == TypeManager.int64_type) ||
1084 (target_type == TypeManager.double_type) ||
1085 (target_type == TypeManager.float_type) ||
1086 (target_type == TypeManager.decimal_type))
1089 } else if (expr_type == TypeManager.ushort_type){
1091 // From ushort to int, uint, long, ulong, float, double
1093 if ((target_type == TypeManager.uint32_type) ||
1094 (target_type == TypeManager.uint64_type) ||
1095 (target_type == TypeManager.int32_type) ||
1096 (target_type == TypeManager.int64_type) ||
1097 (target_type == TypeManager.double_type) ||
1098 (target_type == TypeManager.float_type) ||
1099 (target_type == TypeManager.decimal_type))
1102 } else if (expr_type == TypeManager.int32_type){
1104 // From int to long, float, double
1106 if ((target_type == TypeManager.int64_type) ||
1107 (target_type == TypeManager.double_type) ||
1108 (target_type == TypeManager.float_type) ||
1109 (target_type == TypeManager.decimal_type))
1112 } else if (expr_type == TypeManager.uint32_type){
1114 // From uint to long, ulong, float, double
1116 if ((target_type == TypeManager.int64_type) ||
1117 (target_type == TypeManager.uint64_type) ||
1118 (target_type == TypeManager.double_type) ||
1119 (target_type == TypeManager.float_type) ||
1120 (target_type == TypeManager.decimal_type))
1123 } else if ((expr_type == TypeManager.uint64_type) ||
1124 (expr_type == TypeManager.int64_type)) {
1126 // From long/ulong to float, double
1128 if ((target_type == TypeManager.double_type) ||
1129 (target_type == TypeManager.float_type) ||
1130 (target_type == TypeManager.decimal_type))
1133 } else if (expr_type == TypeManager.char_type){
1135 // From char to ushort, int, uint, long, ulong, float, double
1137 if ((target_type == TypeManager.ushort_type) ||
1138 (target_type == TypeManager.int32_type) ||
1139 (target_type == TypeManager.uint32_type) ||
1140 (target_type == TypeManager.uint64_type) ||
1141 (target_type == TypeManager.int64_type) ||
1142 (target_type == TypeManager.float_type) ||
1143 (target_type == TypeManager.double_type) ||
1144 (target_type == TypeManager.decimal_type))
1147 } else if (expr_type == TypeManager.float_type){
1151 if (target_type == TypeManager.double_type)
1155 if (ImplicitReferenceConversionExists (expr, target_type))
1158 if (expr is IntConstant){
1159 int value = ((IntConstant) expr).Value;
1161 if (target_type == TypeManager.sbyte_type){
1162 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1164 } else if (target_type == TypeManager.byte_type){
1165 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1167 } else if (target_type == TypeManager.short_type){
1168 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1170 } else if (target_type == TypeManager.ushort_type){
1171 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1173 } else if (target_type == TypeManager.uint32_type){
1176 } else if (target_type == TypeManager.uint64_type){
1178 // we can optimize this case: a positive int32
1179 // always fits on a uint64. But we need an opcode
1186 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1190 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1192 // Try the implicit constant expression conversion
1193 // from long to ulong, instead of a nice routine,
1194 // we just inline it
1196 long v = ((LongConstant) expr).Value;
1201 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1202 IntLiteral i = (IntLiteral) expr;
1208 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1215 // Used internally by FindMostEncompassedType, this is used
1216 // to avoid creating lots of objects in the tight loop inside
1217 // FindMostEncompassedType
1219 static EmptyExpression priv_fmet_param;
1222 /// Finds "most encompassed type" according to the spec (13.4.2)
1223 /// amongst the methods in the MethodGroupExpr
1225 static Type FindMostEncompassedType (ArrayList types)
1229 if (priv_fmet_param == null)
1230 priv_fmet_param = new EmptyExpression ();
1232 foreach (Type t in types){
1233 priv_fmet_param.SetType (t);
1240 if (StandardConversionExists (priv_fmet_param, best))
1248 // Used internally by FindMostEncompassingType, this is used
1249 // to avoid creating lots of objects in the tight loop inside
1250 // FindMostEncompassingType
1252 static EmptyExpression priv_fmee_ret;
1255 /// Finds "most encompassing type" according to the spec (13.4.2)
1256 /// amongst the types in the given set
1258 static Type FindMostEncompassingType (ArrayList types)
1262 if (priv_fmee_ret == null)
1263 priv_fmee_ret = new EmptyExpression ();
1265 foreach (Type t in types){
1266 priv_fmee_ret.SetType (best);
1273 if (StandardConversionExists (priv_fmee_ret, t))
1281 // Used to avoid creating too many objects
1283 static EmptyExpression priv_fms_expr;
1286 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1287 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1288 /// for explicit and implicit conversion operators.
1290 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1291 bool apply_explicit_conv_rules,
1294 ArrayList src_types_set = new ArrayList ();
1296 if (priv_fms_expr == null)
1297 priv_fms_expr = new EmptyExpression ();
1300 // If any operator converts from S then Sx = S
1302 Type source_type = source.Type;
1303 foreach (MethodBase mb in me.Methods){
1304 ParameterData pd = Invocation.GetParameterData (mb);
1305 Type param_type = pd.ParameterType (0);
1307 if (param_type == source_type)
1310 if (apply_explicit_conv_rules) {
1313 // Find the set of applicable user-defined conversion operators, U. This set
1315 // user-defined implicit or explicit conversion operators declared by
1316 // the classes or structs in D that convert from a type encompassing
1317 // or encompassed by S to a type encompassing or encompassed by T
1319 priv_fms_expr.SetType (param_type);
1320 if (StandardConversionExists (priv_fms_expr, source_type))
1321 src_types_set.Add (param_type);
1323 if (StandardConversionExists (source, param_type))
1324 src_types_set.Add (param_type);
1328 // Only if S is encompassed by param_type
1330 if (StandardConversionExists (source, param_type))
1331 src_types_set.Add (param_type);
1336 // Explicit Conv rules
1338 if (apply_explicit_conv_rules) {
1339 ArrayList candidate_set = new ArrayList ();
1341 foreach (Type param_type in src_types_set){
1342 if (StandardConversionExists (source, param_type))
1343 candidate_set.Add (param_type);
1346 if (candidate_set.Count != 0)
1347 return FindMostEncompassedType (candidate_set);
1353 if (apply_explicit_conv_rules)
1354 return FindMostEncompassingType (src_types_set);
1356 return FindMostEncompassedType (src_types_set);
1360 // Useful in avoiding proliferation of objects
1362 static EmptyExpression priv_fmt_expr;
1365 /// Finds the most specific target Tx according to section 13.4.4
1367 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1368 bool apply_explicit_conv_rules,
1371 ArrayList tgt_types_set = new ArrayList ();
1373 if (priv_fmt_expr == null)
1374 priv_fmt_expr = new EmptyExpression ();
1377 // If any operator converts to T then Tx = T
1379 foreach (MethodInfo mi in me.Methods){
1380 Type ret_type = mi.ReturnType;
1382 if (ret_type == target)
1385 if (apply_explicit_conv_rules) {
1388 // Find the set of applicable user-defined conversion operators, U.
1390 // This set consists of the
1391 // user-defined implicit or explicit conversion operators declared by
1392 // the classes or structs in D that convert from a type encompassing
1393 // or encompassed by S to a type encompassing or encompassed by T
1395 priv_fms_expr.SetType (ret_type);
1396 if (StandardConversionExists (priv_fms_expr, target))
1397 tgt_types_set.Add (ret_type);
1399 priv_fms_expr.SetType (target);
1400 if (StandardConversionExists (priv_fms_expr, ret_type))
1401 tgt_types_set.Add (ret_type);
1405 // Only if T is encompassed by param_type
1407 priv_fms_expr.SetType (ret_type);
1408 if (StandardConversionExists (priv_fms_expr, target))
1409 tgt_types_set.Add (ret_type);
1414 // Explicit conv rules
1416 if (apply_explicit_conv_rules) {
1417 ArrayList candidate_set = new ArrayList ();
1419 foreach (Type ret_type in tgt_types_set){
1420 priv_fmt_expr.SetType (ret_type);
1422 if (StandardConversionExists (priv_fmt_expr, target))
1423 candidate_set.Add (ret_type);
1426 if (candidate_set.Count != 0)
1427 return FindMostEncompassingType (candidate_set);
1431 // Okay, final case !
1433 if (apply_explicit_conv_rules)
1434 return FindMostEncompassedType (tgt_types_set);
1436 return FindMostEncompassingType (tgt_types_set);
1440 /// User-defined Implicit conversions
1442 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1443 Type target, Location loc)
1445 return UserDefinedConversion (ec, source, target, loc, false);
1449 /// User-defined Explicit conversions
1451 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1452 Type target, Location loc)
1454 return UserDefinedConversion (ec, source, target, loc, true);
1458 /// Computes the MethodGroup for the user-defined conversion
1459 /// operators from source_type to target_type. `look_for_explicit'
1460 /// controls whether we should also include the list of explicit
1463 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1464 Type source_type, Type target_type,
1465 Location loc, bool look_for_explicit)
1467 Expression mg1 = null, mg2 = null;
1468 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1472 // FIXME : How does the False operator come into the picture ?
1473 // This doesn't look complete and very correct !
1475 if (target_type == TypeManager.bool_type && !look_for_explicit)
1476 op_name = "op_True";
1478 op_name = "op_Implicit";
1480 MethodGroupExpr union3;
1482 mg1 = MethodLookup (ec, source_type, op_name, loc);
1483 if (source_type.BaseType != null)
1484 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1487 union3 = (MethodGroupExpr) mg2;
1488 else if (mg2 == null)
1489 union3 = (MethodGroupExpr) mg1;
1491 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1493 mg1 = MethodLookup (ec, target_type, op_name, loc);
1496 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1498 union3 = (MethodGroupExpr) mg1;
1501 if (target_type.BaseType != null)
1502 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1506 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1508 union3 = (MethodGroupExpr) mg1;
1511 MethodGroupExpr union4 = null;
1513 if (look_for_explicit) {
1514 op_name = "op_Explicit";
1516 mg5 = MemberLookup (ec, source_type, op_name, loc);
1517 if (source_type.BaseType != null)
1518 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1520 mg7 = MemberLookup (ec, target_type, op_name, loc);
1521 if (target_type.BaseType != null)
1522 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1524 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1525 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1527 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1530 return Invocation.MakeUnionSet (union3, union4, loc);
1534 /// User-defined conversions
1536 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1537 Type target, Location loc,
1538 bool look_for_explicit)
1540 MethodGroupExpr union;
1541 Type source_type = source.Type;
1542 MethodBase method = null;
1544 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1548 Type most_specific_source, most_specific_target;
1551 foreach (MethodBase m in union.Methods){
1552 Console.WriteLine ("Name: " + m.Name);
1553 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1557 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1558 if (most_specific_source == null)
1561 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1562 if (most_specific_target == null)
1567 foreach (MethodBase mb in union.Methods){
1568 ParameterData pd = Invocation.GetParameterData (mb);
1569 MethodInfo mi = (MethodInfo) mb;
1571 if (pd.ParameterType (0) == most_specific_source &&
1572 mi.ReturnType == most_specific_target) {
1578 if (method == null || count > 1)
1583 // This will do the conversion to the best match that we
1584 // found. Now we need to perform an implict standard conversion
1585 // if the best match was not the type that we were requested
1588 if (look_for_explicit)
1589 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1591 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1597 e = new UserCast ((MethodInfo) method, source, loc);
1598 if (e.Type != target){
1599 if (!look_for_explicit)
1600 e = ConvertImplicitStandard (ec, e, target, loc);
1602 e = ConvertExplicitStandard (ec, e, target, loc);
1608 /// Converts implicitly the resolved expression `expr' into the
1609 /// `target_type'. It returns a new expression that can be used
1610 /// in a context that expects a `target_type'.
1612 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1613 Type target_type, Location loc)
1615 Type expr_type = expr.Type;
1618 if (expr_type == target_type)
1621 if (target_type == null)
1622 throw new Exception ("Target type is null");
1624 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1628 e = ImplicitUserConversion (ec, expr, target_type, loc);
1632 e = RuntimeConversion (ec, expr, target_type, loc);
1640 /// Converts the resolved expression `expr' into the
1641 /// `target_type' using the Microsoft.VisualBasic runtime.
1642 /// It returns a new expression that can be used
1643 /// in a context that expects a `target_type'.
1645 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1651 etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1652 args = new ArrayList();
1653 arg = new Argument (expr, Argument.AType.Expression);
1655 e = (Expression) new Invocation (etmp, args, loc);
1660 static public bool RuntimeConversionExists (EmitContext ec, Expression expr, Type target_type)
1662 return (RuntimeConversion (ec, expr, target_type,Location.Null)) != null;
1665 static public Expression RuntimeConversion (EmitContext ec, Expression expr,
1666 Type target_type, Location loc)
1668 Type expr_type = expr.Type;
1669 TypeCode dest_type = Type.GetTypeCode (target_type);
1670 TypeCode src_type = Type.GetTypeCode (expr_type);
1671 Expression e = null;
1673 // VB.NET Objects can be converted to anything by default
1674 // unless, that is, an exception at runtime blows it all
1675 if (src_type == TypeCode.Object) {
1676 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
1677 Cast ce = new Cast (cast_type, expr, loc);
1678 return ce.Resolve (ec);
1681 switch (dest_type) {
1682 case TypeCode.String:
1684 case TypeCode.SByte:
1686 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
1688 case TypeCode.UInt16:
1689 case TypeCode.Int16:
1690 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
1692 case TypeCode.UInt32:
1693 case TypeCode.Int32:
1694 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
1696 case TypeCode.UInt64:
1697 case TypeCode.Int64:
1698 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
1701 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
1703 case TypeCode.Single:
1704 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
1706 case TypeCode.Double:
1707 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
1709 case TypeCode.Boolean:
1710 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
1712 case TypeCode.DateTime:
1713 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
1715 case TypeCode.Decimal:
1716 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
1718 case TypeCode.Object:
1719 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
1724 case TypeCode.Int32:
1725 case TypeCode.UInt32:
1727 case TypeCode.String:
1728 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
1730 case TypeCode.Object:
1731 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
1736 case TypeCode.Int16:
1737 case TypeCode.UInt16:
1739 case TypeCode.String:
1740 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
1742 case TypeCode.Object:
1743 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
1748 // Ok, this *is* broken
1749 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
1753 // We must examine separately some types that
1754 // don't have a TypeCode but are supported
1756 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
1757 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
1764 /// Attempts to apply the `Standard Implicit
1765 /// Conversion' rules to the expression `expr' into
1766 /// the `target_type'. It returns a new expression
1767 /// that can be used in a context that expects a
1770 /// This is different from `ConvertImplicit' in that the
1771 /// user defined implicit conversions are excluded.
1773 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1774 Type target_type, Location loc)
1776 Type expr_type = expr.Type;
1779 if (expr_type == target_type)
1782 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1786 e = ImplicitReferenceConversion (expr, target_type);
1790 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1791 IntLiteral i = (IntLiteral) expr;
1794 return new EmptyCast (expr, target_type);
1798 if (expr_type.IsPointer){
1799 if (target_type == TypeManager.void_ptr_type)
1800 return new EmptyCast (expr, target_type);
1803 // yep, comparing pointer types cant be done with
1804 // t1 == t2, we have to compare their element types.
1806 if (target_type.IsPointer){
1807 if (target_type.GetElementType()==expr_type.GetElementType())
1812 if (target_type.IsPointer){
1813 if (expr is NullLiteral)
1814 return new EmptyCast (expr, target_type);
1822 /// Attemps to perform an implict constant conversion of the IntConstant
1823 /// into a different data type using casts (See Implicit Constant
1824 /// Expression Conversions)
1826 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1828 int value = ic.Value;
1831 // FIXME: This could return constants instead of EmptyCasts
1833 if (target_type == TypeManager.sbyte_type){
1834 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1835 return new SByteConstant ((sbyte) value);
1836 } else if (target_type == TypeManager.byte_type){
1837 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1838 return new ByteConstant ((byte) value);
1839 } else if (target_type == TypeManager.short_type){
1840 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1841 return new ShortConstant ((short) value);
1842 } else if (target_type == TypeManager.ushort_type){
1843 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1844 return new UShortConstant ((ushort) value);
1845 } else if (target_type == TypeManager.uint32_type){
1847 return new UIntConstant ((uint) value);
1848 } else if (target_type == TypeManager.uint64_type){
1850 // we can optimize this case: a positive int32
1851 // always fits on a uint64. But we need an opcode
1855 return new ULongConstant ((ulong) value);
1858 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1859 Type underlying = TypeManager.EnumToUnderlying (target_type);
1860 Constant e = (Constant) ic;
1863 // Possibly, we need to create a different 0 literal before passing
1866 if (underlying == TypeManager.int64_type)
1867 e = new LongLiteral (0);
1868 else if (underlying == TypeManager.uint64_type)
1869 e = new ULongLiteral (0);
1871 return new EnumConstant (e, target_type);
1876 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1878 string msg = "Cannot convert implicitly from `"+
1879 TypeManager.CSharpName (source) + "' to `" +
1880 TypeManager.CSharpName (target) + "'";
1882 Report.Error (29, loc, msg);
1886 /// Attemptes to implicityly convert `target' into `type', using
1887 /// ConvertImplicit. If there is no implicit conversion, then
1888 /// an error is signaled
1890 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1891 Type target_type, Location loc)
1895 e = ConvertImplicit (ec, source, target_type, loc);
1899 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1900 Report.Error (664, loc,
1901 "Double literal cannot be implicitly converted to " +
1902 "float type, use F suffix to create a float literal");
1905 Error_CannotConvertImplicit (loc, source.Type, target_type);
1911 /// Performs the explicit numeric conversions
1913 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1915 Type expr_type = expr.Type;
1918 // If we have an enumeration, extract the underlying type,
1919 // use this during the comparison, but wrap around the original
1922 Type real_target_type = target_type;
1924 if (TypeManager.IsEnumType (real_target_type))
1925 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1927 if (StandardConversionExists (expr, real_target_type)){
1928 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1930 if (real_target_type != target_type)
1931 return new EmptyCast (ce, target_type);
1935 if (expr_type == TypeManager.sbyte_type){
1937 // From sbyte to byte, ushort, uint, ulong, char
1939 if (real_target_type == TypeManager.byte_type)
1940 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1941 if (real_target_type == TypeManager.ushort_type)
1942 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1943 if (real_target_type == TypeManager.uint32_type)
1944 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1945 if (real_target_type == TypeManager.uint64_type)
1946 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1947 if (real_target_type == TypeManager.char_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1949 } else if (expr_type == TypeManager.byte_type){
1951 // From byte to sbyte and char
1953 if (real_target_type == TypeManager.sbyte_type)
1954 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1955 if (real_target_type == TypeManager.char_type)
1956 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1957 } else if (expr_type == TypeManager.short_type){
1959 // From short to sbyte, byte, ushort, uint, ulong, char
1961 if (real_target_type == TypeManager.sbyte_type)
1962 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1963 if (real_target_type == TypeManager.byte_type)
1964 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1965 if (real_target_type == TypeManager.ushort_type)
1966 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1967 if (real_target_type == TypeManager.uint32_type)
1968 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1969 if (real_target_type == TypeManager.uint64_type)
1970 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1971 if (real_target_type == TypeManager.char_type)
1972 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1973 } else if (expr_type == TypeManager.ushort_type){
1975 // From ushort to sbyte, byte, short, char
1977 if (real_target_type == TypeManager.sbyte_type)
1978 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1979 if (real_target_type == TypeManager.byte_type)
1980 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1981 if (real_target_type == TypeManager.short_type)
1982 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1983 if (real_target_type == TypeManager.char_type)
1984 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1985 } else if (expr_type == TypeManager.int32_type){
1987 // From int to sbyte, byte, short, ushort, uint, ulong, char
1989 if (real_target_type == TypeManager.sbyte_type)
1990 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1991 if (real_target_type == TypeManager.byte_type)
1992 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1993 if (real_target_type == TypeManager.short_type)
1994 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1995 if (real_target_type == TypeManager.ushort_type)
1996 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1997 if (real_target_type == TypeManager.uint32_type)
1998 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1999 if (real_target_type == TypeManager.uint64_type)
2000 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2001 if (real_target_type == TypeManager.char_type)
2002 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2003 } else if (expr_type == TypeManager.uint32_type){
2005 // From uint to sbyte, byte, short, ushort, int, char
2007 if (real_target_type == TypeManager.sbyte_type)
2008 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2009 if (real_target_type == TypeManager.byte_type)
2010 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2011 if (real_target_type == TypeManager.short_type)
2012 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2013 if (real_target_type == TypeManager.ushort_type)
2014 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2015 if (real_target_type == TypeManager.int32_type)
2016 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2017 if (real_target_type == TypeManager.char_type)
2018 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2019 } else if (expr_type == TypeManager.int64_type){
2021 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2023 if (real_target_type == TypeManager.sbyte_type)
2024 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2025 if (real_target_type == TypeManager.byte_type)
2026 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2027 if (real_target_type == TypeManager.short_type)
2028 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2029 if (real_target_type == TypeManager.ushort_type)
2030 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2031 if (real_target_type == TypeManager.int32_type)
2032 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2033 if (real_target_type == TypeManager.uint32_type)
2034 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2035 if (real_target_type == TypeManager.uint64_type)
2036 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2037 if (real_target_type == TypeManager.char_type)
2038 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2039 } else if (expr_type == TypeManager.uint64_type){
2041 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2043 if (real_target_type == TypeManager.sbyte_type)
2044 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2045 if (real_target_type == TypeManager.byte_type)
2046 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2047 if (real_target_type == TypeManager.short_type)
2048 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2049 if (real_target_type == TypeManager.ushort_type)
2050 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2051 if (real_target_type == TypeManager.int32_type)
2052 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2053 if (real_target_type == TypeManager.uint32_type)
2054 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2055 if (real_target_type == TypeManager.int64_type)
2056 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2057 if (real_target_type == TypeManager.char_type)
2058 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2059 } else if (expr_type == TypeManager.char_type){
2061 // From char to sbyte, byte, short
2063 if (real_target_type == TypeManager.sbyte_type)
2064 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2065 if (real_target_type == TypeManager.byte_type)
2066 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2067 if (real_target_type == TypeManager.short_type)
2068 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2069 } else if (expr_type == TypeManager.float_type){
2071 // From float to sbyte, byte, short,
2072 // ushort, int, uint, long, ulong, char
2075 if (real_target_type == TypeManager.sbyte_type)
2076 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2077 if (real_target_type == TypeManager.byte_type)
2078 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2079 if (real_target_type == TypeManager.short_type)
2080 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2081 if (real_target_type == TypeManager.ushort_type)
2082 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2083 if (real_target_type == TypeManager.int32_type)
2084 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2085 if (real_target_type == TypeManager.uint32_type)
2086 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2087 if (real_target_type == TypeManager.int64_type)
2088 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2089 if (real_target_type == TypeManager.uint64_type)
2090 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2091 if (real_target_type == TypeManager.char_type)
2092 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2093 } else if (expr_type == TypeManager.double_type){
2095 // From double to byte, byte, short,
2096 // ushort, int, uint, long, ulong,
2097 // char, float or decimal
2099 if (real_target_type == TypeManager.sbyte_type)
2100 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2101 if (real_target_type == TypeManager.byte_type)
2102 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2103 if (real_target_type == TypeManager.short_type)
2104 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2105 if (real_target_type == TypeManager.ushort_type)
2106 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2107 if (real_target_type == TypeManager.int32_type)
2108 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2109 if (real_target_type == TypeManager.uint32_type)
2110 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2111 if (real_target_type == TypeManager.int64_type)
2112 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2113 if (real_target_type == TypeManager.uint64_type)
2114 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2115 if (real_target_type == TypeManager.char_type)
2116 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2117 if (real_target_type == TypeManager.float_type)
2118 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2121 // decimal is taken care of by the op_Explicit methods.
2127 /// Returns whether an explicit reference conversion can be performed
2128 /// from source_type to target_type
2130 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2132 bool target_is_value_type = target_type.IsValueType;
2134 if (source_type == target_type)
2138 // From object to any reference type
2140 if (source_type == TypeManager.object_type && !target_is_value_type)
2144 // From any class S to any class-type T, provided S is a base class of T
2146 if (target_type.IsSubclassOf (source_type))
2150 // From any interface type S to any interface T provided S is not derived from T
2152 if (source_type.IsInterface && target_type.IsInterface){
2153 if (!target_type.IsSubclassOf (source_type))
2158 // From any class type S to any interface T, provided S is not sealed
2159 // and provided S does not implement T.
2161 if (target_type.IsInterface && !source_type.IsSealed &&
2162 !TypeManager.ImplementsInterface (source_type, target_type))
2166 // From any interface-type S to to any class type T, provided T is not
2167 // sealed, or provided T implements S.
2169 if (source_type.IsInterface &&
2170 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2174 // From an array type S with an element type Se to an array type T with an
2175 // element type Te provided all the following are true:
2176 // * S and T differe only in element type, in other words, S and T
2177 // have the same number of dimensions.
2178 // * Both Se and Te are reference types
2179 // * An explicit referenc conversions exist from Se to Te
2181 if (source_type.IsArray && target_type.IsArray) {
2182 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2184 Type source_element_type = source_type.GetElementType ();
2185 Type target_element_type = target_type.GetElementType ();
2187 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2188 if (ExplicitReferenceConversionExists (source_element_type,
2189 target_element_type))
2195 // From System.Array to any array-type
2196 if (source_type == TypeManager.array_type &&
2197 target_type.IsArray){
2202 // From System delegate to any delegate-type
2204 if (source_type == TypeManager.delegate_type &&
2205 target_type.IsSubclassOf (TypeManager.delegate_type))
2209 // From ICloneable to Array or Delegate types
2211 if (source_type == TypeManager.icloneable_type &&
2212 (target_type == TypeManager.array_type ||
2213 target_type == TypeManager.delegate_type))
2220 /// Implements Explicit Reference conversions
2222 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2224 Type source_type = source.Type;
2225 bool target_is_value_type = target_type.IsValueType;
2228 // From object to any reference type
2230 if (source_type == TypeManager.object_type && !target_is_value_type)
2231 return new ClassCast (source, target_type);
2235 // From any class S to any class-type T, provided S is a base class of T
2237 if (target_type.IsSubclassOf (source_type))
2238 return new ClassCast (source, target_type);
2241 // From any interface type S to any interface T provided S is not derived from T
2243 if (source_type.IsInterface && target_type.IsInterface){
2244 if (TypeManager.ImplementsInterface (source_type, target_type))
2247 return new ClassCast (source, target_type);
2251 // From any class type S to any interface T, provides S is not sealed
2252 // and provided S does not implement T.
2254 if (target_type.IsInterface && !source_type.IsSealed) {
2255 if (TypeManager.ImplementsInterface (source_type, target_type))
2258 return new ClassCast (source, target_type);
2263 // From any interface-type S to to any class type T, provided T is not
2264 // sealed, or provided T implements S.
2266 if (source_type.IsInterface) {
2267 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2268 return new ClassCast (source, target_type);
2273 // From an array type S with an element type Se to an array type T with an
2274 // element type Te provided all the following are true:
2275 // * S and T differe only in element type, in other words, S and T
2276 // have the same number of dimensions.
2277 // * Both Se and Te are reference types
2278 // * An explicit referenc conversions exist from Se to Te
2280 if (source_type.IsArray && target_type.IsArray) {
2281 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2283 Type source_element_type = source_type.GetElementType ();
2284 Type target_element_type = target_type.GetElementType ();
2286 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2287 if (ExplicitReferenceConversionExists (source_element_type,
2288 target_element_type))
2289 return new ClassCast (source, target_type);
2294 // From System.Array to any array-type
2295 if (source_type == TypeManager.array_type &&
2296 target_type.IsArray) {
2297 return new ClassCast (source, target_type);
2301 // From System delegate to any delegate-type
2303 if (source_type == TypeManager.delegate_type &&
2304 target_type.IsSubclassOf (TypeManager.delegate_type))
2305 return new ClassCast (source, target_type);
2308 // From ICloneable to Array or Delegate types
2310 if (source_type == TypeManager.icloneable_type &&
2311 (target_type == TypeManager.array_type ||
2312 target_type == TypeManager.delegate_type))
2313 return new ClassCast (source, target_type);
2319 /// Performs an explicit conversion of the expression `expr' whose
2320 /// type is expr.Type to `target_type'.
2322 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2323 Type target_type, Location loc)
2325 Type expr_type = expr.Type;
2326 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2331 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2336 // Unboxing conversion.
2338 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2339 return new UnboxCast (expr, target_type);
2344 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2348 // FIXME: Is there any reason we should have EnumConstant
2349 // dealt with here instead of just using always the
2350 // UnderlyingSystemType to wrap the type?
2352 if (expr is EnumConstant)
2353 e = ((EnumConstant) expr).Child;
2355 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2358 Expression t = ConvertImplicit (ec, e, target_type, loc);
2362 t = ConvertNumericExplicit (ec, e, target_type, loc);
2366 t = RuntimeConversion (ec, e, target_type, loc);
2370 Error_CannotConvertType (loc, expr_type, target_type);
2374 ne = ConvertReferenceExplicit (expr, target_type);
2379 if (target_type.IsPointer){
2380 if (expr_type.IsPointer)
2381 return new EmptyCast (expr, target_type);
2383 if (expr_type == TypeManager.sbyte_type ||
2384 expr_type == TypeManager.byte_type ||
2385 expr_type == TypeManager.short_type ||
2386 expr_type == TypeManager.ushort_type ||
2387 expr_type == TypeManager.int32_type ||
2388 expr_type == TypeManager.uint32_type ||
2389 expr_type == TypeManager.uint64_type ||
2390 expr_type == TypeManager.int64_type)
2391 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2393 if (expr_type.IsPointer){
2394 if (target_type == TypeManager.sbyte_type ||
2395 target_type == TypeManager.byte_type ||
2396 target_type == TypeManager.short_type ||
2397 target_type == TypeManager.ushort_type ||
2398 target_type == TypeManager.int32_type ||
2399 target_type == TypeManager.uint32_type ||
2400 target_type == TypeManager.uint64_type ||
2401 target_type == TypeManager.int64_type){
2402 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2405 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2410 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2414 // We should always be able to go from an uint32
2415 // implicitly or explicitly to the other integral
2418 throw new Exception ("Internal compiler error");
2423 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2427 ne = RuntimeConversion (ec, expr, target_type, loc);
2431 Error_CannotConvertType (loc, expr_type, target_type);
2436 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2438 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2439 Type target_type, Location l)
2441 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2446 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2450 ne = ConvertReferenceExplicit (expr, target_type);
2454 ne = RuntimeConversion (ec, expr, target_type, l);
2458 Error_CannotConvertType (l, expr.Type, target_type);
2462 static string ExprClassName (ExprClass c)
2465 case ExprClass.Invalid:
2467 case ExprClass.Value:
2469 case ExprClass.Variable:
2471 case ExprClass.Namespace:
2473 case ExprClass.Type:
2475 case ExprClass.MethodGroup:
2476 return "method group";
2477 case ExprClass.PropertyAccess:
2478 return "property access";
2479 case ExprClass.EventAccess:
2480 return "event access";
2481 case ExprClass.IndexerAccess:
2482 return "indexer access";
2483 case ExprClass.Nothing:
2486 throw new Exception ("Should not happen");
2490 /// Reports that we were expecting `expr' to be of class `expected'
2492 public void Error118 (string expected)
2494 string kind = "Unknown";
2496 kind = ExprClassName (eclass);
2498 Error (118, "Expression denotes a `" + kind +
2499 "' where a `" + expected + "' was expected");
2502 public void Error118 (ResolveFlags flags)
2504 ArrayList valid = new ArrayList (10);
2506 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2507 valid.Add ("variable");
2508 valid.Add ("value");
2511 if ((flags & ResolveFlags.Type) != 0)
2514 if ((flags & ResolveFlags.MethodGroup) != 0)
2515 valid.Add ("method group");
2517 if ((flags & ResolveFlags.SimpleName) != 0)
2518 valid.Add ("simple name");
2520 if (valid.Count == 0)
2521 valid.Add ("unknown");
2523 StringBuilder sb = new StringBuilder ();
2524 for (int i = 0; i < valid.Count; i++) {
2527 else if (i == valid.Count)
2529 sb.Append (valid [i]);
2532 string kind = ExprClassName (eclass);
2534 Error (119, "Expression denotes a `" + kind + "' where " +
2535 "a `" + sb.ToString () + "' was expected");
2538 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2540 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2541 TypeManager.CSharpName (t));
2544 public static void UnsafeError (Location loc)
2546 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2550 /// Converts the IntConstant, UIntConstant, LongConstant or
2551 /// ULongConstant into the integral target_type. Notice
2552 /// that we do not return an `Expression' we do return
2553 /// a boxed integral type.
2555 /// FIXME: Since I added the new constants, we need to
2556 /// also support conversions from CharConstant, ByteConstant,
2557 /// SByteConstant, UShortConstant, ShortConstant
2559 /// This is used by the switch statement, so the domain
2560 /// of work is restricted to the literals above, and the
2561 /// targets are int32, uint32, char, byte, sbyte, ushort,
2562 /// short, uint64 and int64
2564 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2568 if (c.Type == target_type)
2569 return ((Constant) c).GetValue ();
2572 // Make into one of the literals we handle, we dont really care
2573 // about this value as we will just return a few limited types
2575 if (c is EnumConstant)
2576 c = ((EnumConstant)c).WidenToCompilerConstant ();
2578 if (c is IntConstant){
2579 int v = ((IntConstant) c).Value;
2581 if (target_type == TypeManager.uint32_type){
2584 } else if (target_type == TypeManager.char_type){
2585 if (v >= Char.MinValue && v <= Char.MaxValue)
2587 } else if (target_type == TypeManager.byte_type){
2588 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2590 } else if (target_type == TypeManager.sbyte_type){
2591 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2593 } else if (target_type == TypeManager.short_type){
2594 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2596 } else if (target_type == TypeManager.ushort_type){
2597 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2599 } else if (target_type == TypeManager.int64_type)
2601 else if (target_type == TypeManager.uint64_type){
2607 } else if (c is UIntConstant){
2608 uint v = ((UIntConstant) c).Value;
2610 if (target_type == TypeManager.int32_type){
2611 if (v <= Int32.MaxValue)
2613 } else if (target_type == TypeManager.char_type){
2614 if (v >= Char.MinValue && v <= Char.MaxValue)
2616 } else if (target_type == TypeManager.byte_type){
2617 if (v <= Byte.MaxValue)
2619 } else if (target_type == TypeManager.sbyte_type){
2620 if (v <= SByte.MaxValue)
2622 } else if (target_type == TypeManager.short_type){
2623 if (v <= UInt16.MaxValue)
2625 } else if (target_type == TypeManager.ushort_type){
2626 if (v <= UInt16.MaxValue)
2628 } else if (target_type == TypeManager.int64_type)
2630 else if (target_type == TypeManager.uint64_type)
2633 } else if (c is LongConstant){
2634 long v = ((LongConstant) c).Value;
2636 if (target_type == TypeManager.int32_type){
2637 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2639 } else if (target_type == TypeManager.uint32_type){
2640 if (v >= 0 && v <= UInt32.MaxValue)
2642 } else if (target_type == TypeManager.char_type){
2643 if (v >= Char.MinValue && v <= Char.MaxValue)
2645 } else if (target_type == TypeManager.byte_type){
2646 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2648 } else if (target_type == TypeManager.sbyte_type){
2649 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2651 } else if (target_type == TypeManager.short_type){
2652 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2654 } else if (target_type == TypeManager.ushort_type){
2655 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2657 } else if (target_type == TypeManager.uint64_type){
2662 } else if (c is ULongConstant){
2663 ulong v = ((ULongConstant) c).Value;
2665 if (target_type == TypeManager.int32_type){
2666 if (v <= Int32.MaxValue)
2668 } else if (target_type == TypeManager.uint32_type){
2669 if (v <= UInt32.MaxValue)
2671 } else if (target_type == TypeManager.char_type){
2672 if (v >= Char.MinValue && v <= Char.MaxValue)
2674 } else if (target_type == TypeManager.byte_type){
2675 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2677 } else if (target_type == TypeManager.sbyte_type){
2678 if (v <= (int) SByte.MaxValue)
2680 } else if (target_type == TypeManager.short_type){
2681 if (v <= UInt16.MaxValue)
2683 } else if (target_type == TypeManager.ushort_type){
2684 if (v <= UInt16.MaxValue)
2686 } else if (target_type == TypeManager.int64_type){
2687 if (v <= Int64.MaxValue)
2691 } else if (c is ByteConstant){
2692 byte v = ((ByteConstant) c).Value;
2694 if (target_type == TypeManager.int32_type)
2696 else if (target_type == TypeManager.uint32_type)
2698 else if (target_type == TypeManager.char_type)
2700 else if (target_type == TypeManager.sbyte_type){
2701 if (v <= SByte.MaxValue)
2703 } else if (target_type == TypeManager.short_type)
2705 else if (target_type == TypeManager.ushort_type)
2707 else if (target_type == TypeManager.int64_type)
2709 else if (target_type == TypeManager.uint64_type)
2712 } else if (c is SByteConstant){
2713 sbyte v = ((SByteConstant) c).Value;
2715 if (target_type == TypeManager.int32_type)
2717 else if (target_type == TypeManager.uint32_type){
2720 } else if (target_type == TypeManager.char_type){
2723 } else if (target_type == TypeManager.byte_type){
2726 } else if (target_type == TypeManager.short_type)
2728 else if (target_type == TypeManager.ushort_type){
2731 } else if (target_type == TypeManager.int64_type)
2733 else if (target_type == TypeManager.uint64_type){
2738 } else if (c is ShortConstant){
2739 short v = ((ShortConstant) c).Value;
2741 if (target_type == TypeManager.int32_type){
2743 } else if (target_type == TypeManager.uint32_type){
2746 } else if (target_type == TypeManager.char_type){
2749 } else if (target_type == TypeManager.byte_type){
2750 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2752 } else if (target_type == TypeManager.sbyte_type){
2753 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2755 } else if (target_type == TypeManager.ushort_type){
2758 } else if (target_type == TypeManager.int64_type)
2760 else if (target_type == TypeManager.uint64_type)
2764 } else if (c is UShortConstant){
2765 ushort v = ((UShortConstant) c).Value;
2767 if (target_type == TypeManager.int32_type)
2769 else if (target_type == TypeManager.uint32_type)
2771 else if (target_type == TypeManager.char_type){
2772 if (v >= Char.MinValue && v <= Char.MaxValue)
2774 } else if (target_type == TypeManager.byte_type){
2775 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2777 } else if (target_type == TypeManager.sbyte_type){
2778 if (v <= SByte.MaxValue)
2780 } else if (target_type == TypeManager.short_type){
2781 if (v <= Int16.MaxValue)
2783 } else if (target_type == TypeManager.int64_type)
2785 else if (target_type == TypeManager.uint64_type)
2789 } else if (c is CharConstant){
2790 char v = ((CharConstant) c).Value;
2792 if (target_type == TypeManager.int32_type)
2794 else if (target_type == TypeManager.uint32_type)
2796 else if (target_type == TypeManager.byte_type){
2797 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2799 } else if (target_type == TypeManager.sbyte_type){
2800 if (v <= SByte.MaxValue)
2802 } else if (target_type == TypeManager.short_type){
2803 if (v <= Int16.MaxValue)
2805 } else if (target_type == TypeManager.ushort_type)
2807 else if (target_type == TypeManager.int64_type)
2809 else if (target_type == TypeManager.uint64_type)
2814 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2819 // Load the object from the pointer.
2821 public static void LoadFromPtr (ILGenerator ig, Type t)
2823 if (t == TypeManager.int32_type)
2824 ig.Emit (OpCodes.Ldind_I4);
2825 else if (t == TypeManager.uint32_type)
2826 ig.Emit (OpCodes.Ldind_U4);
2827 else if (t == TypeManager.short_type)
2828 ig.Emit (OpCodes.Ldind_I2);
2829 else if (t == TypeManager.ushort_type)
2830 ig.Emit (OpCodes.Ldind_U2);
2831 else if (t == TypeManager.char_type)
2832 ig.Emit (OpCodes.Ldind_U2);
2833 else if (t == TypeManager.byte_type)
2834 ig.Emit (OpCodes.Ldind_U1);
2835 else if (t == TypeManager.sbyte_type)
2836 ig.Emit (OpCodes.Ldind_I1);
2837 else if (t == TypeManager.uint64_type)
2838 ig.Emit (OpCodes.Ldind_I8);
2839 else if (t == TypeManager.int64_type)
2840 ig.Emit (OpCodes.Ldind_I8);
2841 else if (t == TypeManager.float_type)
2842 ig.Emit (OpCodes.Ldind_R4);
2843 else if (t == TypeManager.double_type)
2844 ig.Emit (OpCodes.Ldind_R8);
2845 else if (t == TypeManager.bool_type)
2846 ig.Emit (OpCodes.Ldind_I1);
2847 else if (t == TypeManager.intptr_type)
2848 ig.Emit (OpCodes.Ldind_I);
2849 else if (TypeManager.IsEnumType (t)) {
2850 if (t == TypeManager.enum_type)
2851 ig.Emit (OpCodes.Ldind_Ref);
2853 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2854 } else if (t.IsValueType)
2855 ig.Emit (OpCodes.Ldobj, t);
2857 ig.Emit (OpCodes.Ldind_Ref);
2861 // The stack contains the pointer and the value of type `type'
2863 public static void StoreFromPtr (ILGenerator ig, Type type)
2865 if (TypeManager.IsEnumType (type))
2866 type = TypeManager.EnumToUnderlying (type);
2867 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2868 ig.Emit (OpCodes.Stind_I4);
2869 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2870 ig.Emit (OpCodes.Stind_I8);
2871 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2872 type == TypeManager.ushort_type)
2873 ig.Emit (OpCodes.Stind_I2);
2874 else if (type == TypeManager.float_type)
2875 ig.Emit (OpCodes.Stind_R4);
2876 else if (type == TypeManager.double_type)
2877 ig.Emit (OpCodes.Stind_R8);
2878 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2879 type == TypeManager.bool_type)
2880 ig.Emit (OpCodes.Stind_I1);
2881 else if (type == TypeManager.intptr_type)
2882 ig.Emit (OpCodes.Stind_I);
2883 else if (type.IsValueType)
2884 ig.Emit (OpCodes.Stobj, type);
2886 ig.Emit (OpCodes.Stind_Ref);
2890 // Returns the size of type `t' if known, otherwise, 0
2892 public static int GetTypeSize (Type t)
2894 t = TypeManager.TypeToCoreType (t);
2895 if (t == TypeManager.int32_type ||
2896 t == TypeManager.uint32_type ||
2897 t == TypeManager.float_type)
2899 else if (t == TypeManager.int64_type ||
2900 t == TypeManager.uint64_type ||
2901 t == TypeManager.double_type)
2903 else if (t == TypeManager.byte_type ||
2904 t == TypeManager.sbyte_type ||
2905 t == TypeManager.bool_type)
2907 else if (t == TypeManager.short_type ||
2908 t == TypeManager.char_type ||
2909 t == TypeManager.ushort_type)
2911 else if (t == TypeManager.decimal_type)
2918 // Default implementation of IAssignMethod.CacheTemporaries
2920 public void CacheTemporaries (EmitContext ec)
2924 static void Error_NegativeArrayIndex (Location loc)
2926 Report.Error (284, loc, "Can not create array with a negative size");
2930 // Converts `source' to an int, uint, long or ulong.
2932 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2936 bool old_checked = ec.CheckState;
2937 ec.CheckState = true;
2939 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2940 if (target == null){
2941 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2942 if (target == null){
2943 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2944 if (target == null){
2945 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2947 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2951 ec.CheckState = old_checked;
2954 // Only positive constants are allowed at compile time
2956 if (target is Constant){
2957 if (target is IntConstant){
2958 if (((IntConstant) target).Value < 0){
2959 Error_NegativeArrayIndex (loc);
2964 if (target is LongConstant){
2965 if (((LongConstant) target).Value < 0){
2966 Error_NegativeArrayIndex (loc);
2979 /// This is just a base class for expressions that can
2980 /// appear on statements (invocations, object creation,
2981 /// assignments, post/pre increment and decrement). The idea
2982 /// being that they would support an extra Emition interface that
2983 /// does not leave a result on the stack.
2985 public abstract class ExpressionStatement : Expression {
2988 /// Requests the expression to be emitted in a `statement'
2989 /// context. This means that no new value is left on the
2990 /// stack after invoking this method (constrasted with
2991 /// Emit that will always leave a value on the stack).
2993 public abstract void EmitStatement (EmitContext ec);
2997 /// This kind of cast is used to encapsulate the child
2998 /// whose type is child.Type into an expression that is
2999 /// reported to return "return_type". This is used to encapsulate
3000 /// expressions which have compatible types, but need to be dealt
3001 /// at higher levels with.
3003 /// For example, a "byte" expression could be encapsulated in one
3004 /// of these as an "unsigned int". The type for the expression
3005 /// would be "unsigned int".
3008 public class EmptyCast : Expression {
3009 protected Expression child;
3011 public EmptyCast (Expression child, Type return_type)
3013 eclass = child.eclass;
3018 public override Expression DoResolve (EmitContext ec)
3020 // This should never be invoked, we are born in fully
3021 // initialized state.
3026 public override void Emit (EmitContext ec)
3033 /// This class is used to wrap literals which belong inside Enums
3035 public class EnumConstant : Constant {
3036 public Constant Child;
3038 public EnumConstant (Constant child, Type enum_type)
3040 eclass = child.eclass;
3045 public override Expression DoResolve (EmitContext ec)
3047 // This should never be invoked, we are born in fully
3048 // initialized state.
3053 public override void Emit (EmitContext ec)
3058 public override object GetValue ()
3060 return Child.GetValue ();
3064 // Converts from one of the valid underlying types for an enumeration
3065 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3066 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3068 public Constant WidenToCompilerConstant ()
3070 Type t = TypeManager.EnumToUnderlying (Child.Type);
3071 object v = ((Constant) Child).GetValue ();;
3073 if (t == TypeManager.int32_type)
3074 return new IntConstant ((int) v);
3075 if (t == TypeManager.uint32_type)
3076 return new UIntConstant ((uint) v);
3077 if (t == TypeManager.int64_type)
3078 return new LongConstant ((long) v);
3079 if (t == TypeManager.uint64_type)
3080 return new ULongConstant ((ulong) v);
3081 if (t == TypeManager.short_type)
3082 return new ShortConstant ((short) v);
3083 if (t == TypeManager.ushort_type)
3084 return new UShortConstant ((ushort) v);
3085 if (t == TypeManager.byte_type)
3086 return new ByteConstant ((byte) v);
3087 if (t == TypeManager.sbyte_type)
3088 return new SByteConstant ((sbyte) v);
3090 throw new Exception ("Invalid enumeration underlying type: " + t);
3094 // Extracts the value in the enumeration on its native representation
3096 public object GetPlainValue ()
3098 Type t = TypeManager.EnumToUnderlying (Child.Type);
3099 object v = ((Constant) Child).GetValue ();;
3101 if (t == TypeManager.int32_type)
3103 if (t == TypeManager.uint32_type)
3105 if (t == TypeManager.int64_type)
3107 if (t == TypeManager.uint64_type)
3109 if (t == TypeManager.short_type)
3111 if (t == TypeManager.ushort_type)
3113 if (t == TypeManager.byte_type)
3115 if (t == TypeManager.sbyte_type)
3121 public override string AsString ()
3123 return Child.AsString ();
3126 public override DoubleConstant ConvertToDouble ()
3128 return Child.ConvertToDouble ();
3131 public override FloatConstant ConvertToFloat ()
3133 return Child.ConvertToFloat ();
3136 public override ULongConstant ConvertToULong ()
3138 return Child.ConvertToULong ();
3141 public override LongConstant ConvertToLong ()
3143 return Child.ConvertToLong ();
3146 public override UIntConstant ConvertToUInt ()
3148 return Child.ConvertToUInt ();
3151 public override IntConstant ConvertToInt ()
3153 return Child.ConvertToInt ();
3158 /// This kind of cast is used to encapsulate Value Types in objects.
3160 /// The effect of it is to box the value type emitted by the previous
3163 public class BoxedCast : EmptyCast {
3165 public BoxedCast (Expression expr)
3166 : base (expr, TypeManager.object_type)
3170 public override Expression DoResolve (EmitContext ec)
3172 // This should never be invoked, we are born in fully
3173 // initialized state.
3178 public override void Emit (EmitContext ec)
3182 ec.ig.Emit (OpCodes.Box, child.Type);
3186 public class UnboxCast : EmptyCast {
3187 public UnboxCast (Expression expr, Type return_type)
3188 : base (expr, return_type)
3192 public override Expression DoResolve (EmitContext ec)
3194 // This should never be invoked, we are born in fully
3195 // initialized state.
3200 public override void Emit (EmitContext ec)
3203 ILGenerator ig = ec.ig;
3206 ig.Emit (OpCodes.Unbox, t);
3208 LoadFromPtr (ig, t);
3213 /// This is used to perform explicit numeric conversions.
3215 /// Explicit numeric conversions might trigger exceptions in a checked
3216 /// context, so they should generate the conv.ovf opcodes instead of
3219 public class ConvCast : EmptyCast {
3220 public enum Mode : byte {
3221 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3223 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3224 U2_I1, U2_U1, U2_I2, U2_CH,
3225 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3226 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3227 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3228 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3229 CH_I1, CH_U1, CH_I2,
3230 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3231 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3237 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3238 : base (child, return_type)
3240 checked_state = ec.CheckState;
3244 public override Expression DoResolve (EmitContext ec)
3246 // This should never be invoked, we are born in fully
3247 // initialized state.
3252 public override void Emit (EmitContext ec)
3254 ILGenerator ig = ec.ig;
3260 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3261 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3262 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3263 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3264 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3266 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3267 case Mode.U1_CH: /* nothing */ break;
3269 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3270 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3271 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3272 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3273 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3274 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3276 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3277 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3278 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3279 case Mode.U2_CH: /* nothing */ break;
3281 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3282 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3283 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3284 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3285 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3286 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3287 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3289 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3290 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3291 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3292 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3293 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3294 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3296 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3297 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3298 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3299 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3300 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3301 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3302 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3303 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3305 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3306 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3307 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3308 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3309 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3310 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3311 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3312 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3314 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3315 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3316 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3318 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3319 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3320 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3321 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3322 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3323 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3324 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3325 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3326 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3328 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3329 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3330 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3331 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3332 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3333 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3334 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3335 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3336 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3337 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3341 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3342 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3343 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3344 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3345 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3347 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3348 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3350 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3351 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3352 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3353 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3354 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3355 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3357 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3358 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3359 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3360 case Mode.U2_CH: /* nothing */ break;
3362 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3363 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3364 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3365 case Mode.I4_U4: /* nothing */ break;
3366 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3367 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3368 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3370 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3371 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3372 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3373 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3374 case Mode.U4_I4: /* nothing */ break;
3375 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3377 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3378 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3379 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3380 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3381 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3382 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3383 case Mode.I8_U8: /* nothing */ break;
3384 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3386 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3387 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3388 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3389 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3390 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3391 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3392 case Mode.U8_I8: /* nothing */ break;
3393 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3395 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3396 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3397 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3399 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3400 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3401 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3402 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3403 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3404 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3405 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3406 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3407 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3409 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3410 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3411 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3412 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3413 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3414 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3415 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3416 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3417 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3418 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3424 public class OpcodeCast : EmptyCast {
3428 public OpcodeCast (Expression child, Type return_type, OpCode op)
3429 : base (child, return_type)
3433 second_valid = false;
3436 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3437 : base (child, return_type)
3442 second_valid = true;
3445 public override Expression DoResolve (EmitContext ec)
3447 // This should never be invoked, we are born in fully
3448 // initialized state.
3453 public override void Emit (EmitContext ec)
3464 /// This kind of cast is used to encapsulate a child and cast it
3465 /// to the class requested
3467 public class ClassCast : EmptyCast {
3468 public ClassCast (Expression child, Type return_type)
3469 : base (child, return_type)
3474 public override Expression DoResolve (EmitContext ec)
3476 // This should never be invoked, we are born in fully
3477 // initialized state.
3482 public override void Emit (EmitContext ec)
3486 ec.ig.Emit (OpCodes.Castclass, type);
3492 /// SimpleName expressions are initially formed of a single
3493 /// word and it only happens at the beginning of the expression.
3497 /// The expression will try to be bound to a Field, a Method
3498 /// group or a Property. If those fail we pass the name to our
3499 /// caller and the SimpleName is compounded to perform a type
3500 /// lookup. The idea behind this process is that we want to avoid
3501 /// creating a namespace map from the assemblies, as that requires
3502 /// the GetExportedTypes function to be called and a hashtable to
3503 /// be constructed which reduces startup time. If later we find
3504 /// that this is slower, we should create a `NamespaceExpr' expression
3505 /// that fully participates in the resolution process.
3507 /// For example `System.Console.WriteLine' is decomposed into
3508 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3510 /// The first SimpleName wont produce a match on its own, so it will
3512 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3514 /// System.Console will produce a TypeExpr match.
3516 /// The downside of this is that we might be hitting `LookupType' too many
3517 /// times with this scheme.
3519 public class SimpleName : Expression, ITypeExpression {
3520 public readonly string Name;
3522 public SimpleName (string name, Location l)
3528 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3530 if (ec.IsFieldInitializer)
3533 "A field initializer cannot reference the non-static field, " +
3534 "method or property `"+name+"'");
3538 "An object reference is required " +
3539 "for the non-static field `"+name+"'");
3543 // Checks whether we are trying to access an instance
3544 // property, method or field from a static body.
3546 Expression MemberStaticCheck (EmitContext ec, Expression e)
3548 if (e is IMemberExpr){
3549 IMemberExpr member = (IMemberExpr) e;
3551 if (!member.IsStatic){
3552 Error_ObjectRefRequired (ec, loc, Name);
3560 public override Expression DoResolve (EmitContext ec)
3562 return SimpleNameResolve (ec, null, false);
3565 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3567 return SimpleNameResolve (ec, right_side, false);
3571 public Expression DoResolveAllowStatic (EmitContext ec)
3573 return SimpleNameResolve (ec, null, true);
3576 public Expression DoResolveType (EmitContext ec)
3579 // Stage 3: Lookup symbol in the various namespaces.
3581 DeclSpace ds = ec.DeclSpace;
3585 if (ec.ResolvingTypeTree){
3586 int errors = Report.Errors;
3587 Type dt = ec.DeclSpace.FindType (loc, Name);
3588 if (Report.Errors != errors)
3592 return new TypeExpr (dt, loc);
3595 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3596 return new TypeExpr (t, loc);
3600 // Stage 2 part b: Lookup up if we are an alias to a type
3603 // Since we are cheating: we only do the Alias lookup for
3604 // namespaces if the name does not include any dots in it
3607 alias_value = ec.DeclSpace.LookupAlias (Name);
3609 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3610 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3611 return new TypeExpr (t, loc);
3613 // we have alias value, but it isn't Type, so try if it's namespace
3614 return new SimpleName (alias_value, loc);
3617 // No match, maybe our parent can compose us
3618 // into something meaningful.
3623 /// 7.5.2: Simple Names.
3625 /// Local Variables and Parameters are handled at
3626 /// parse time, so they never occur as SimpleNames.
3628 /// The `allow_static' flag is used by MemberAccess only
3629 /// and it is used to inform us that it is ok for us to
3630 /// avoid the static check, because MemberAccess might end
3631 /// up resolving the Name as a Type name and the access as
3632 /// a static type access.
3634 /// ie: Type Type; .... { Type.GetType (""); }
3636 /// Type is both an instance variable and a Type; Type.GetType
3637 /// is the static method not an instance method of type.
3639 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3641 Expression e = null;
3644 // Stage 1: Performed by the parser (binding to locals or parameters).
3646 Block current_block = ec.CurrentBlock;
3647 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
3648 LocalVariableReference var;
3650 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3652 if (right_side != null)
3653 return var.ResolveLValue (ec, right_side);
3655 return var.Resolve (ec);
3658 if (current_block != null){
3660 Parameter par = null;
3661 Parameters pars = current_block.Parameters;
3663 par = pars.GetParameterByName (Name, out idx);
3666 ParameterReference param;
3668 param = new ParameterReference (pars, idx, Name, loc);
3670 if (right_side != null)
3671 return param.ResolveLValue (ec, right_side);
3673 return param.Resolve (ec);
3678 // Stage 2: Lookup members
3682 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3683 // Hence we have two different cases
3686 DeclSpace lookup_ds = ec.DeclSpace;
3688 if (lookup_ds.TypeBuilder == null)
3691 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3696 // Classes/structs keep looking, enums break
3698 if (lookup_ds is TypeContainer)
3699 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3702 } while (lookup_ds != null);
3704 if (e == null && ec.ContainerType != null)
3705 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3708 return DoResolveType (ec);
3713 if (e is IMemberExpr) {
3714 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3718 IMemberExpr me = e as IMemberExpr;
3722 // This fails if ResolveMemberAccess() was unable to decide whether
3723 // it's a field or a type of the same name.
3724 if (!me.IsStatic && (me.InstanceExpression == null))
3727 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
3728 Maybe a grammar-related problem?
3731 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3732 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3733 "outer type `" + me.DeclaringType + "' via nested type `" +
3734 me.InstanceExpression.Type + "'");
3738 if (right_side != null)
3739 e = e.DoResolveLValue (ec, right_side);
3741 e = e.DoResolve (ec);
3746 if (ec.IsStatic || ec.IsFieldInitializer){
3750 return MemberStaticCheck (ec, e);
3755 public override void Emit (EmitContext ec)
3758 // If this is ever reached, then we failed to
3759 // find the name as a namespace
3762 Error (103, "The name `" + Name +
3763 "' does not exist in the class `" +
3764 ec.DeclSpace.Name + "'");
3767 public override string ToString ()
3774 /// Fully resolved expression that evaluates to a type
3776 public class TypeExpr : Expression, ITypeExpression {
3777 public TypeExpr (Type t, Location l)
3780 eclass = ExprClass.Type;
3784 public virtual Expression DoResolveType (EmitContext ec)
3789 override public Expression DoResolve (EmitContext ec)
3794 override public void Emit (EmitContext ec)
3796 throw new Exception ("Should never be called");
3799 public override string ToString ()
3801 return Type.ToString ();
3806 /// Used to create types from a fully qualified name. These are just used
3807 /// by the parser to setup the core types. A TypeLookupExpression is always
3808 /// classified as a type.
3810 public class TypeLookupExpression : TypeExpr {
3813 public TypeLookupExpression (string name) : base (null, Location.Null)
3818 public override Expression DoResolveType (EmitContext ec)
3821 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3825 public override Expression DoResolve (EmitContext ec)
3827 return DoResolveType (ec);
3830 public override void Emit (EmitContext ec)
3832 throw new Exception ("Should never be called");
3835 public override string ToString ()
3842 /// MethodGroup Expression.
3844 /// This is a fully resolved expression that evaluates to a type
3846 public class MethodGroupExpr : Expression, IMemberExpr {
3847 public MethodBase [] Methods;
3848 Expression instance_expression = null;
3849 bool is_explicit_impl = false;
3851 public MethodGroupExpr (MemberInfo [] mi, Location l)
3853 Methods = new MethodBase [mi.Length];
3854 mi.CopyTo (Methods, 0);
3855 eclass = ExprClass.MethodGroup;
3856 type = TypeManager.object_type;
3860 public MethodGroupExpr (ArrayList list, Location l)
3862 Methods = new MethodBase [list.Count];
3865 list.CopyTo (Methods, 0);
3867 foreach (MemberInfo m in list){
3868 if (!(m is MethodBase)){
3869 Console.WriteLine ("Name " + m.Name);
3870 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3876 eclass = ExprClass.MethodGroup;
3877 type = TypeManager.object_type;
3880 public Type DeclaringType {
3882 return Methods [0].DeclaringType;
3887 // `A method group may have associated an instance expression'
3889 public Expression InstanceExpression {
3891 return instance_expression;
3895 instance_expression = value;
3899 public bool IsExplicitImpl {
3901 return is_explicit_impl;
3905 is_explicit_impl = value;
3909 public string Name {
3911 return Methods [0].Name;
3915 public bool IsInstance {
3917 foreach (MethodBase mb in Methods)
3925 public bool IsStatic {
3927 foreach (MethodBase mb in Methods)
3935 override public Expression DoResolve (EmitContext ec)
3937 if (instance_expression != null) {
3938 instance_expression = instance_expression.DoResolve (ec);
3939 if (instance_expression == null)
3946 public void ReportUsageError ()
3948 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3949 Methods [0].Name + "()' is referenced without parentheses");
3952 override public void Emit (EmitContext ec)
3954 ReportUsageError ();
3957 bool RemoveMethods (bool keep_static)
3959 ArrayList smethods = new ArrayList ();
3961 foreach (MethodBase mb in Methods){
3962 if (mb.IsStatic == keep_static)
3966 if (smethods.Count == 0)
3969 Methods = new MethodBase [smethods.Count];
3970 smethods.CopyTo (Methods, 0);
3976 /// Removes any instance methods from the MethodGroup, returns
3977 /// false if the resulting set is empty.
3979 public bool RemoveInstanceMethods ()
3981 return RemoveMethods (true);
3985 /// Removes any static methods from the MethodGroup, returns
3986 /// false if the resulting set is empty.
3988 public bool RemoveStaticMethods ()
3990 return RemoveMethods (false);
3995 /// Fully resolved expression that evaluates to a Field
3997 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3998 public readonly FieldInfo FieldInfo;
3999 Expression instance_expr;
4001 public FieldExpr (FieldInfo fi, Location l)
4004 eclass = ExprClass.Variable;
4005 type = fi.FieldType;
4009 public string Name {
4011 return FieldInfo.Name;
4015 public bool IsInstance {
4017 return !FieldInfo.IsStatic;
4021 public bool IsStatic {
4023 return FieldInfo.IsStatic;
4027 public Type DeclaringType {
4029 return FieldInfo.DeclaringType;
4033 public Expression InstanceExpression {
4035 return instance_expr;
4039 instance_expr = value;
4043 override public Expression DoResolve (EmitContext ec)
4045 if (!FieldInfo.IsStatic){
4046 if (instance_expr == null){
4047 throw new Exception ("non-static FieldExpr without instance var\n" +
4048 "You have to assign the Instance variable\n" +
4049 "Of the FieldExpr to set this\n");
4052 // Resolve the field's instance expression while flow analysis is turned
4053 // off: when accessing a field "a.b", we must check whether the field
4054 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4055 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4056 ResolveFlags.DisableFlowAnalysis);
4057 if (instance_expr == null)
4061 // If the instance expression is a local variable or parameter.
4062 IVariable var = instance_expr as IVariable;
4063 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4069 void Report_AssignToReadonly (bool is_instance)
4074 msg = "Readonly field can not be assigned outside " +
4075 "of constructor or variable initializer";
4077 msg = "A static readonly field can only be assigned in " +
4078 "a static constructor";
4080 Report.Error (is_instance ? 191 : 198, loc, msg);
4083 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4085 IVariable var = instance_expr as IVariable;
4087 var.SetFieldAssigned (ec, FieldInfo.Name);
4089 Expression e = DoResolve (ec);
4094 if (!FieldInfo.IsInitOnly)
4098 // InitOnly fields can only be assigned in constructors
4101 if (ec.IsConstructor)
4104 Report_AssignToReadonly (true);
4109 override public void Emit (EmitContext ec)
4111 ILGenerator ig = ec.ig;
4112 bool is_volatile = false;
4114 if (FieldInfo is FieldBuilder){
4115 FieldBase f = TypeManager.GetField (FieldInfo);
4117 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4120 f.status |= Field.Status.USED;
4123 if (FieldInfo.IsStatic){
4125 ig.Emit (OpCodes.Volatile);
4127 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4129 if (instance_expr.Type.IsValueType){
4131 LocalTemporary tempo = null;
4133 if (!(instance_expr is IMemoryLocation)){
4134 tempo = new LocalTemporary (
4135 ec, instance_expr.Type);
4137 InstanceExpression.Emit (ec);
4141 ml = (IMemoryLocation) instance_expr;
4143 ml.AddressOf (ec, AddressOp.Load);
4145 instance_expr.Emit (ec);
4148 ig.Emit (OpCodes.Volatile);
4150 ig.Emit (OpCodes.Ldfld, FieldInfo);
4154 public void EmitAssign (EmitContext ec, Expression source)
4156 FieldAttributes fa = FieldInfo.Attributes;
4157 bool is_static = (fa & FieldAttributes.Static) != 0;
4158 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4159 ILGenerator ig = ec.ig;
4161 if (is_readonly && !ec.IsConstructor){
4162 Report_AssignToReadonly (!is_static);
4167 Expression instance = instance_expr;
4169 if (instance.Type.IsValueType){
4170 if (instance is IMemoryLocation){
4171 IMemoryLocation ml = (IMemoryLocation) instance;
4173 ml.AddressOf (ec, AddressOp.Store);
4175 throw new Exception ("The " + instance + " of type " +
4177 " represents a ValueType and does " +
4178 "not implement IMemoryLocation");
4184 if (FieldInfo is FieldBuilder){
4185 FieldBase f = TypeManager.GetField (FieldInfo);
4187 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4188 ig.Emit (OpCodes.Volatile);
4192 ig.Emit (OpCodes.Stsfld, FieldInfo);
4194 ig.Emit (OpCodes.Stfld, FieldInfo);
4196 if (FieldInfo is FieldBuilder){
4197 FieldBase f = TypeManager.GetField (FieldInfo);
4199 f.status |= Field.Status.ASSIGNED;
4203 public void AddressOf (EmitContext ec, AddressOp mode)
4205 ILGenerator ig = ec.ig;
4207 if (FieldInfo is FieldBuilder){
4208 FieldBase f = TypeManager.GetField (FieldInfo);
4209 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4210 ig.Emit (OpCodes.Volatile);
4213 if (FieldInfo is FieldBuilder){
4214 FieldBase f = TypeManager.GetField (FieldInfo);
4216 if ((mode & AddressOp.Store) != 0)
4217 f.status |= Field.Status.ASSIGNED;
4218 if ((mode & AddressOp.Load) != 0)
4219 f.status |= Field.Status.USED;
4223 // Handle initonly fields specially: make a copy and then
4224 // get the address of the copy.
4226 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4230 local = ig.DeclareLocal (type);
4231 ig.Emit (OpCodes.Stloc, local);
4232 ig.Emit (OpCodes.Ldloca, local);
4236 if (FieldInfo.IsStatic)
4237 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4239 if (instance_expr is IMemoryLocation)
4240 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4242 instance_expr.Emit (ec);
4243 ig.Emit (OpCodes.Ldflda, FieldInfo);
4249 /// Expression that evaluates to a Property. The Assign class
4250 /// might set the `Value' expression if we are in an assignment.
4252 /// This is not an LValue because we need to re-write the expression, we
4253 /// can not take data from the stack and store it.
4255 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4256 public readonly PropertyInfo PropertyInfo;
4258 MethodInfo getter, setter;
4260 public ArrayList PropertyArgs;
4262 Expression instance_expr;
4264 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4267 eclass = ExprClass.PropertyAccess;
4268 PropertyArgs = new ArrayList();
4272 type = TypeManager.TypeToCoreType (pi.PropertyType);
4274 ResolveAccessors (ec);
4277 public string Name {
4279 return PropertyInfo.Name;
4283 public bool IsInstance {
4289 public bool IsStatic {
4295 public Type DeclaringType {
4297 return PropertyInfo.DeclaringType;
4302 // The instance expression associated with this expression
4304 public Expression InstanceExpression {
4306 instance_expr = value;
4310 return instance_expr;
4314 public bool VerifyAssignable ()
4316 if (!PropertyInfo.CanWrite){
4317 Report.Error (200, loc,
4318 "The property `" + PropertyInfo.Name +
4319 "' can not be assigned to, as it has not set accessor");
4326 void ResolveAccessors (EmitContext ec)
4328 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4329 MemberInfo [] group;
4331 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4332 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4335 // The first method is the closest to us
4337 if (group != null && group.Length > 0){
4338 getter = (MethodInfo) group [0];
4340 if (getter.IsStatic)
4345 // The first method is the closest to us
4347 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4348 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4349 if (group != null && group.Length > 0){
4350 setter = (MethodInfo) group [0];
4351 if (setter.IsStatic)
4356 override public Expression DoResolve (EmitContext ec)
4358 if (getter == null){
4359 Report.Error (154, loc,
4360 "The property `" + PropertyInfo.Name +
4361 "' can not be used in " +
4362 "this context because it lacks a get accessor");
4366 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4367 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4371 if (instance_expr != null) {
4372 instance_expr = instance_expr.DoResolve (ec);
4373 if (instance_expr == null)
4380 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4382 if (setter == null){
4383 Report.Error (154, loc,
4384 "The property `" + PropertyInfo.Name +
4385 "' can not be used in " +
4386 "this context because it lacks a set accessor");
4390 if (instance_expr != null) {
4391 instance_expr = instance_expr.DoResolve (ec);
4392 if (instance_expr == null)
4399 override public void Emit (EmitContext ec)
4402 // Special case: length of single dimension array property is turned into ldlen
4404 if ((getter == TypeManager.system_int_array_get_length) ||
4405 (getter == TypeManager.int_array_get_length)){
4406 Type iet = instance_expr.Type;
4409 // System.Array.Length can be called, but the Type does not
4410 // support invoking GetArrayRank, so test for that case first
4412 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4413 instance_expr.Emit (ec);
4414 ec.ig.Emit (OpCodes.Ldlen);
4418 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4422 // Implements the IAssignMethod interface for assignments
4424 public void EmitAssign (EmitContext ec, Expression source)
4426 Argument arg = new Argument (source, Argument.AType.Expression);
4427 ArrayList args = new ArrayList ();
4430 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4433 override public void EmitStatement (EmitContext ec)
4436 ec.ig.Emit (OpCodes.Pop);
4441 /// Fully resolved expression that evaluates to an Event
4443 public class EventExpr : Expression, IMemberExpr {
4444 public readonly EventInfo EventInfo;
4445 public Expression instance_expr;
4448 MethodInfo add_accessor, remove_accessor;
4450 public EventExpr (EventInfo ei, Location loc)
4454 eclass = ExprClass.EventAccess;
4456 add_accessor = TypeManager.GetAddMethod (ei);
4457 remove_accessor = TypeManager.GetRemoveMethod (ei);
4459 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4462 if (EventInfo is MyEventBuilder)
4463 type = ((MyEventBuilder) EventInfo).EventType;
4465 type = EventInfo.EventHandlerType;
4468 public string Name {
4470 return EventInfo.Name;
4474 public bool IsInstance {
4480 public bool IsStatic {
4486 public Type DeclaringType {
4488 return EventInfo.DeclaringType;
4492 public Expression InstanceExpression {
4494 return instance_expr;
4498 instance_expr = value;
4502 public override Expression DoResolve (EmitContext ec)
4504 if (instance_expr != null) {
4505 instance_expr = instance_expr.DoResolve (ec);
4506 if (instance_expr == null)
4513 public override void Emit (EmitContext ec)
4515 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4518 public void EmitAddOrRemove (EmitContext ec, Expression source)
4520 Expression handler = ((Binary) source).Right;
4522 Argument arg = new Argument (handler, Argument.AType.Expression);
4523 ArrayList args = new ArrayList ();
4527 if (((Binary) source).Oper == Binary.Operator.Addition)
4528 Invocation.EmitCall (
4529 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4531 Invocation.EmitCall (
4532 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);