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) e).ReportUsageError ();
361 case ExprClass.Value:
362 case ExprClass.Variable:
363 case ExprClass.PropertyAccess:
364 case ExprClass.EventAccess:
365 case ExprClass.IndexerAccess:
366 if ((flags & ResolveFlags.VariableOrValue) == 0) {
373 throw new Exception ("Expression " + e.GetType () +
374 " ExprClass is Invalid after resolve");
378 throw new Exception (
379 "Expression " + e.GetType () +
380 " did not set its type after Resolve\n" +
381 "called from: " + this.GetType ());
387 /// Resolves an expression and performs semantic analysis on it.
389 public Expression Resolve (EmitContext ec)
391 return Resolve (ec, ResolveFlags.VariableOrValue);
395 /// Resolves an expression for LValue assignment
399 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
400 /// checking and assertion checking on what we expect from Resolve
402 public Expression ResolveLValue (EmitContext ec, Expression right_side)
404 Expression e = DoResolveLValue (ec, right_side);
407 if (e is SimpleName){
408 SimpleName s = (SimpleName) e;
412 "The name `" + s.Name + "' could not be found in `" +
413 ec.DeclSpace.Name + "'");
417 if (e.eclass == ExprClass.Invalid)
418 throw new Exception ("Expression " + e +
419 " ExprClass is Invalid after resolve");
421 if (e.eclass == ExprClass.MethodGroup) {
422 ((MethodGroupExpr) e).ReportUsageError ();
427 throw new Exception ("Expression " + e +
428 " did not set its type after Resolve");
435 /// Emits the code for the expression
439 /// The Emit method is invoked to generate the code
440 /// for the expression.
442 public abstract void Emit (EmitContext ec);
445 /// Protected constructor. Only derivate types should
446 /// be able to be created
449 protected Expression ()
451 eclass = ExprClass.Invalid;
456 /// Returns a literalized version of a literal FieldInfo
460 /// The possible return values are:
461 /// IntConstant, UIntConstant
462 /// LongLiteral, ULongConstant
463 /// FloatConstant, DoubleConstant
466 /// The value returned is already resolved.
468 public static Constant Constantify (object v, Type t)
470 if (t == TypeManager.int32_type)
471 return new IntConstant ((int) v);
472 else if (t == TypeManager.uint32_type)
473 return new UIntConstant ((uint) v);
474 else if (t == TypeManager.int64_type)
475 return new LongConstant ((long) v);
476 else if (t == TypeManager.uint64_type)
477 return new ULongConstant ((ulong) v);
478 else if (t == TypeManager.float_type)
479 return new FloatConstant ((float) v);
480 else if (t == TypeManager.double_type)
481 return new DoubleConstant ((double) v);
482 else if (t == TypeManager.string_type)
483 return new StringConstant ((string) v);
484 else if (t == TypeManager.short_type)
485 return new ShortConstant ((short)v);
486 else if (t == TypeManager.ushort_type)
487 return new UShortConstant ((ushort)v);
488 else if (t == TypeManager.sbyte_type)
489 return new SByteConstant (((sbyte)v));
490 else if (t == TypeManager.byte_type)
491 return new ByteConstant ((byte)v);
492 else if (t == TypeManager.char_type)
493 return new CharConstant ((char)v);
494 else if (t == TypeManager.bool_type)
495 return new BoolConstant ((bool) v);
496 else if (TypeManager.IsEnumType (t)){
497 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
499 return new EnumConstant (e, t);
501 throw new Exception ("Unknown type for constant (" + t +
506 /// Returns a fully formed expression after a MemberLookup
508 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
511 return new EventExpr ((EventInfo) mi, loc);
512 else if (mi is FieldInfo)
513 return new FieldExpr ((FieldInfo) mi, loc);
514 else if (mi is PropertyInfo)
515 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
516 else if (mi is Type){
517 return new TypeExpr ((System.Type) mi, loc);
524 // FIXME: Probably implement a cache for (t,name,current_access_set)?
526 // This code could use some optimizations, but we need to do some
527 // measurements. For example, we could use a delegate to `flag' when
528 // something can not any longer be a method-group (because it is something
532 // If the return value is an Array, then it is an array of
535 // If the return value is an MemberInfo, it is anything, but a Method
539 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
540 // the arguments here and have MemberLookup return only the methods that
541 // match the argument count/type, unlike we are doing now (we delay this
544 // This is so we can catch correctly attempts to invoke instance methods
545 // from a static body (scan for error 120 in ResolveSimpleName).
548 // FIXME: Potential optimization, have a static ArrayList
551 public static Expression MemberLookup (EmitContext ec, Type t, string name,
552 MemberTypes mt, BindingFlags bf, Location loc)
554 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
558 // Lookup type `t' for code in class `invocation_type'. Note that it's important
559 // to set `invocation_type' correctly since this method also checks whether the
560 // invoking class is allowed to access the member in class `t'. When you want to
561 // explicitly do a lookup in the base class, you must set both `t' and `invocation_type'
562 // to the base class (although a derived class can access protected members of its base
563 // class it cannot do so through an instance of the base class (error CS1540)).
566 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
567 string name, MemberTypes mt, BindingFlags bf,
570 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
575 int count = mi.Length;
578 return new MethodGroupExpr (mi, loc);
580 if (mi [0] is MethodBase)
581 return new MethodGroupExpr (mi, loc);
583 return ExprClassFromMemberInfo (ec, mi [0], loc);
586 public const MemberTypes AllMemberTypes =
587 MemberTypes.Constructor |
591 MemberTypes.NestedType |
592 MemberTypes.Property;
594 public const BindingFlags AllBindingFlags =
595 BindingFlags.Public |
596 BindingFlags.Static |
597 BindingFlags.Instance;
599 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
601 return MemberLookup (ec, ec.ContainerType, t, name,
602 AllMemberTypes, AllBindingFlags, loc);
605 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
607 return MemberLookup (ec, ec.ContainerType, t, name,
608 MemberTypes.Method, AllBindingFlags, loc);
612 /// This is a wrapper for MemberLookup that is not used to "probe", but
613 /// to find a final definition. If the final definition is not found, we
614 /// look for private members and display a useful debugging message if we
617 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
620 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
623 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
624 MemberTypes mt, BindingFlags bf, Location loc)
628 int errors = Report.Errors;
630 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
635 // Error has already been reported.
636 if (errors < Report.Errors)
639 e = MemberLookup (ec, t, name, AllMemberTypes,
640 AllBindingFlags | BindingFlags.NonPublic, loc);
643 117, loc, "`" + t + "' does not contain a definition " +
644 "for `" + name + "'");
647 122, loc, "`" + t + "." + name +
648 "' is inaccessible due to its protection level");
654 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
656 EventInfo ei = event_expr.EventInfo;
658 return TypeManager.GetPrivateFieldOfEvent (ei);
661 static EmptyExpression MyEmptyExpr;
662 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
664 Type expr_type = expr.Type;
666 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
667 // if we are a method group, emit a warning
673 // notice that it is possible to write "ValueType v = 1", the ValueType here
674 // is an abstract class, and not really a value type, so we apply the same rules.
676 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
678 // A pointer type cannot be converted to object
680 if (expr_type.IsPointer)
683 if (expr_type.IsValueType)
684 return new BoxedCast (expr);
685 if (expr_type.IsClass || expr_type.IsInterface)
686 return new EmptyCast (expr, target_type);
687 } else if (expr_type.IsSubclassOf (target_type)) {
689 // Special case: enumeration to System.Enum.
690 // System.Enum is not a value type, it is a class, so we need
691 // a boxing conversion
693 if (expr_type.IsEnum)
694 return new BoxedCast (expr);
696 return new EmptyCast (expr, target_type);
699 // This code is kind of mirrored inside StandardConversionExists
700 // with the small distinction that we only probe there
702 // Always ensure that the code here and there is in sync
704 // from the null type to any reference-type.
705 if (expr is NullLiteral && !target_type.IsValueType)
706 return new EmptyCast (expr, target_type);
708 // from any class-type S to any interface-type T.
709 if (target_type.IsInterface) {
710 if (TypeManager.ImplementsInterface (expr_type, target_type)){
711 if (expr_type.IsClass)
712 return new EmptyCast (expr, target_type);
713 else if (expr_type.IsValueType)
714 return new BoxedCast (expr);
718 // from any interface type S to interface-type T.
719 if (expr_type.IsInterface && target_type.IsInterface) {
720 if (TypeManager.ImplementsInterface (expr_type, target_type))
721 return new EmptyCast (expr, target_type);
726 // from an array-type S to an array-type of type T
727 if (expr_type.IsArray && target_type.IsArray) {
728 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
730 Type expr_element_type = expr_type.GetElementType ();
732 if (MyEmptyExpr == null)
733 MyEmptyExpr = new EmptyExpression ();
735 MyEmptyExpr.SetType (expr_element_type);
736 Type target_element_type = target_type.GetElementType ();
738 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
739 if (StandardConversionExists (MyEmptyExpr,
740 target_element_type))
741 return new EmptyCast (expr, target_type);
746 // from an array-type to System.Array
747 if (expr_type.IsArray && target_type == TypeManager.array_type)
748 return new EmptyCast (expr, target_type);
750 // from any delegate type to System.Delegate
751 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
752 target_type == TypeManager.delegate_type)
753 return new EmptyCast (expr, target_type);
755 // from any array-type or delegate type into System.ICloneable.
756 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
757 if (target_type == TypeManager.icloneable_type)
758 return new EmptyCast (expr, target_type);
768 /// Implicit Numeric Conversions.
770 /// expr is the expression to convert, returns a new expression of type
771 /// target_type or null if an implicit conversion is not possible.
773 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
774 Type target_type, Location loc)
776 Type expr_type = expr.Type;
779 // Attempt to do the implicit constant expression conversions
781 if (expr is IntConstant){
784 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
788 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
790 // Try the implicit constant expression conversion
791 // from long to ulong, instead of a nice routine,
794 long v = ((LongConstant) expr).Value;
796 return new ULongConstant ((ulong) v);
799 Type real_target_type = target_type;
801 if (expr_type == TypeManager.sbyte_type){
803 // From sbyte to short, int, long, float, double.
805 if (real_target_type == TypeManager.int32_type)
806 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
807 if (real_target_type == TypeManager.int64_type)
808 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
809 if (real_target_type == TypeManager.double_type)
810 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
811 if (real_target_type == TypeManager.float_type)
812 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
813 if (real_target_type == TypeManager.short_type)
814 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
815 } else if (expr_type == TypeManager.byte_type){
817 // From byte to short, ushort, int, uint, long, ulong, float, double
819 if ((real_target_type == TypeManager.short_type) ||
820 (real_target_type == TypeManager.ushort_type) ||
821 (real_target_type == TypeManager.int32_type) ||
822 (real_target_type == TypeManager.uint32_type))
823 return new EmptyCast (expr, target_type);
825 if (real_target_type == TypeManager.uint64_type)
826 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
827 if (real_target_type == TypeManager.int64_type)
828 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
829 if (real_target_type == TypeManager.float_type)
830 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
831 if (real_target_type == TypeManager.double_type)
832 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
833 } else if (expr_type == TypeManager.short_type){
835 // From short to int, long, float, double
837 if (real_target_type == TypeManager.int32_type)
838 return new EmptyCast (expr, target_type);
839 if (real_target_type == TypeManager.int64_type)
840 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
841 if (real_target_type == TypeManager.double_type)
842 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
843 if (real_target_type == TypeManager.float_type)
844 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
845 } else if (expr_type == TypeManager.ushort_type){
847 // From ushort to int, uint, long, ulong, float, double
849 if (real_target_type == TypeManager.uint32_type)
850 return new EmptyCast (expr, target_type);
852 if (real_target_type == TypeManager.uint64_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
854 if (real_target_type == TypeManager.int32_type)
855 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
856 if (real_target_type == TypeManager.int64_type)
857 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
858 if (real_target_type == TypeManager.double_type)
859 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
860 if (real_target_type == TypeManager.float_type)
861 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
862 } else if (expr_type == TypeManager.int32_type){
864 // From int to long, float, double
866 if (real_target_type == TypeManager.int64_type)
867 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
868 if (real_target_type == TypeManager.double_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
870 if (real_target_type == TypeManager.float_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
872 } else if (expr_type == TypeManager.uint32_type){
874 // From uint to long, ulong, float, double
876 if (real_target_type == TypeManager.int64_type)
877 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
878 if (real_target_type == TypeManager.uint64_type)
879 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
880 if (real_target_type == TypeManager.double_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
883 if (real_target_type == TypeManager.float_type)
884 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
886 } else if (expr_type == TypeManager.int64_type){
888 // From long/ulong to float, double
890 if (real_target_type == TypeManager.double_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
892 if (real_target_type == TypeManager.float_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
894 } else if (expr_type == TypeManager.uint64_type){
896 // From ulong to float, double
898 if (real_target_type == TypeManager.double_type)
899 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
901 if (real_target_type == TypeManager.float_type)
902 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
904 } else if (expr_type == TypeManager.char_type){
906 // From char to ushort, int, uint, long, ulong, float, double
908 if ((real_target_type == TypeManager.ushort_type) ||
909 (real_target_type == TypeManager.int32_type) ||
910 (real_target_type == TypeManager.uint32_type))
911 return new EmptyCast (expr, target_type);
912 if (real_target_type == TypeManager.uint64_type)
913 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
914 if (real_target_type == TypeManager.int64_type)
915 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
916 if (real_target_type == TypeManager.float_type)
917 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
918 if (real_target_type == TypeManager.double_type)
919 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
920 } else if (expr_type == TypeManager.float_type){
924 if (real_target_type == TypeManager.double_type)
925 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
932 // Tests whether an implicit reference conversion exists between expr_type
935 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
937 Type expr_type = expr.Type;
940 // This is the boxed case.
942 if (target_type == TypeManager.object_type) {
943 if ((expr_type.IsClass) ||
944 (expr_type.IsValueType) ||
945 (expr_type.IsInterface))
948 } else if (expr_type.IsSubclassOf (target_type)) {
951 // Please remember that all code below actually comes
952 // from ImplicitReferenceConversion so make sure code remains in sync
954 // from any class-type S to any interface-type T.
955 if (target_type.IsInterface) {
956 if (TypeManager.ImplementsInterface (expr_type, target_type))
960 // from any interface type S to interface-type T.
961 if (expr_type.IsInterface && target_type.IsInterface)
962 if (TypeManager.ImplementsInterface (expr_type, target_type))
965 // from an array-type S to an array-type of type T
966 if (expr_type.IsArray && target_type.IsArray) {
967 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
969 Type expr_element_type = expr_type.GetElementType ();
971 if (MyEmptyExpr == null)
972 MyEmptyExpr = new EmptyExpression ();
974 MyEmptyExpr.SetType (expr_element_type);
975 Type target_element_type = target_type.GetElementType ();
977 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
978 if (StandardConversionExists (MyEmptyExpr,
979 target_element_type))
984 // from an array-type to System.Array
985 if (expr_type.IsArray && (target_type == TypeManager.array_type))
988 // from any delegate type to System.Delegate
989 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
990 target_type == TypeManager.delegate_type)
991 if (target_type.IsAssignableFrom (expr_type))
994 // from any array-type or delegate type into System.ICloneable.
995 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
996 if (target_type == TypeManager.icloneable_type)
999 // from the null type to any reference-type.
1000 if (expr is NullLiteral && !target_type.IsValueType &&
1001 !TypeManager.IsEnumType (target_type))
1010 /// Same as StandardConversionExists except that it also looks at
1011 /// implicit user defined conversions - needed for overload resolution
1013 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1015 if (StandardConversionExists (expr, target_type) == true)
1018 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1027 /// Determines if a standard implicit conversion exists from
1028 /// expr_type to target_type
1030 public static bool StandardConversionExists (Expression expr, Type target_type)
1032 Type expr_type = expr.Type;
1034 if (expr_type == TypeManager.void_type)
1037 if (expr_type == target_type)
1040 // First numeric conversions
1042 if (expr_type == TypeManager.sbyte_type){
1044 // From sbyte to short, int, long, float, double.
1046 if ((target_type == TypeManager.int32_type) ||
1047 (target_type == TypeManager.int64_type) ||
1048 (target_type == TypeManager.double_type) ||
1049 (target_type == TypeManager.float_type) ||
1050 (target_type == TypeManager.short_type) ||
1051 (target_type == TypeManager.decimal_type))
1054 } else if (expr_type == TypeManager.byte_type){
1056 // From byte to short, ushort, int, uint, long, ulong, float, double
1058 if ((target_type == TypeManager.short_type) ||
1059 (target_type == TypeManager.ushort_type) ||
1060 (target_type == TypeManager.int32_type) ||
1061 (target_type == TypeManager.uint32_type) ||
1062 (target_type == TypeManager.uint64_type) ||
1063 (target_type == TypeManager.int64_type) ||
1064 (target_type == TypeManager.float_type) ||
1065 (target_type == TypeManager.double_type) ||
1066 (target_type == TypeManager.decimal_type))
1069 } else if (expr_type == TypeManager.short_type){
1071 // From short to int, long, float, double
1073 if ((target_type == TypeManager.int32_type) ||
1074 (target_type == TypeManager.int64_type) ||
1075 (target_type == TypeManager.double_type) ||
1076 (target_type == TypeManager.float_type) ||
1077 (target_type == TypeManager.decimal_type))
1080 } else if (expr_type == TypeManager.ushort_type){
1082 // From ushort to int, uint, long, ulong, float, double
1084 if ((target_type == TypeManager.uint32_type) ||
1085 (target_type == TypeManager.uint64_type) ||
1086 (target_type == TypeManager.int32_type) ||
1087 (target_type == TypeManager.int64_type) ||
1088 (target_type == TypeManager.double_type) ||
1089 (target_type == TypeManager.float_type) ||
1090 (target_type == TypeManager.decimal_type))
1093 } else if (expr_type == TypeManager.int32_type){
1095 // From int to long, float, double
1097 if ((target_type == TypeManager.int64_type) ||
1098 (target_type == TypeManager.double_type) ||
1099 (target_type == TypeManager.float_type) ||
1100 (target_type == TypeManager.decimal_type))
1103 } else if (expr_type == TypeManager.uint32_type){
1105 // From uint to long, ulong, float, double
1107 if ((target_type == TypeManager.int64_type) ||
1108 (target_type == TypeManager.uint64_type) ||
1109 (target_type == TypeManager.double_type) ||
1110 (target_type == TypeManager.float_type) ||
1111 (target_type == TypeManager.decimal_type))
1114 } else if ((expr_type == TypeManager.uint64_type) ||
1115 (expr_type == TypeManager.int64_type)) {
1117 // From long/ulong to float, double
1119 if ((target_type == TypeManager.double_type) ||
1120 (target_type == TypeManager.float_type) ||
1121 (target_type == TypeManager.decimal_type))
1124 } else if (expr_type == TypeManager.char_type){
1126 // From char to ushort, int, uint, long, ulong, float, double
1128 if ((target_type == TypeManager.ushort_type) ||
1129 (target_type == TypeManager.int32_type) ||
1130 (target_type == TypeManager.uint32_type) ||
1131 (target_type == TypeManager.uint64_type) ||
1132 (target_type == TypeManager.int64_type) ||
1133 (target_type == TypeManager.float_type) ||
1134 (target_type == TypeManager.double_type) ||
1135 (target_type == TypeManager.decimal_type))
1138 } else if (expr_type == TypeManager.float_type){
1142 if (target_type == TypeManager.double_type)
1146 if (ImplicitReferenceConversionExists (expr, target_type))
1149 if (expr is IntConstant){
1150 int value = ((IntConstant) expr).Value;
1152 if (target_type == TypeManager.sbyte_type){
1153 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1155 } else if (target_type == TypeManager.byte_type){
1156 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1158 } else if (target_type == TypeManager.short_type){
1159 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1161 } else if (target_type == TypeManager.ushort_type){
1162 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1164 } else if (target_type == TypeManager.uint32_type){
1167 } else if (target_type == TypeManager.uint64_type){
1169 // we can optimize this case: a positive int32
1170 // always fits on a uint64. But we need an opcode
1177 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1181 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1183 // Try the implicit constant expression conversion
1184 // from long to ulong, instead of a nice routine,
1185 // we just inline it
1187 long v = ((LongConstant) expr).Value;
1192 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1193 IntLiteral i = (IntLiteral) expr;
1199 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1206 // Used internally by FindMostEncompassedType, this is used
1207 // to avoid creating lots of objects in the tight loop inside
1208 // FindMostEncompassedType
1210 static EmptyExpression priv_fmet_param;
1213 /// Finds "most encompassed type" according to the spec (13.4.2)
1214 /// amongst the methods in the MethodGroupExpr
1216 static Type FindMostEncompassedType (ArrayList types)
1220 if (priv_fmet_param == null)
1221 priv_fmet_param = new EmptyExpression ();
1223 foreach (Type t in types){
1224 priv_fmet_param.SetType (t);
1231 if (StandardConversionExists (priv_fmet_param, best))
1239 // Used internally by FindMostEncompassingType, this is used
1240 // to avoid creating lots of objects in the tight loop inside
1241 // FindMostEncompassingType
1243 static EmptyExpression priv_fmee_ret;
1246 /// Finds "most encompassing type" according to the spec (13.4.2)
1247 /// amongst the types in the given set
1249 static Type FindMostEncompassingType (ArrayList types)
1253 if (priv_fmee_ret == null)
1254 priv_fmee_ret = new EmptyExpression ();
1256 foreach (Type t in types){
1257 priv_fmee_ret.SetType (best);
1264 if (StandardConversionExists (priv_fmee_ret, t))
1272 // Used to avoid creating too many objects
1274 static EmptyExpression priv_fms_expr;
1277 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1278 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1279 /// for explicit and implicit conversion operators.
1281 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1282 bool apply_explicit_conv_rules,
1285 ArrayList src_types_set = new ArrayList ();
1287 if (priv_fms_expr == null)
1288 priv_fms_expr = new EmptyExpression ();
1291 // If any operator converts from S then Sx = S
1293 Type source_type = source.Type;
1294 foreach (MethodBase mb in me.Methods){
1295 ParameterData pd = Invocation.GetParameterData (mb);
1296 Type param_type = pd.ParameterType (0);
1298 if (param_type == source_type)
1301 if (apply_explicit_conv_rules) {
1304 // Find the set of applicable user-defined conversion operators, U. This set
1306 // user-defined implicit or explicit conversion operators declared by
1307 // the classes or structs in D that convert from a type encompassing
1308 // or encompassed by S to a type encompassing or encompassed by T
1310 priv_fms_expr.SetType (param_type);
1311 if (StandardConversionExists (priv_fms_expr, source_type))
1312 src_types_set.Add (param_type);
1314 if (StandardConversionExists (source, param_type))
1315 src_types_set.Add (param_type);
1319 // Only if S is encompassed by param_type
1321 if (StandardConversionExists (source, param_type))
1322 src_types_set.Add (param_type);
1327 // Explicit Conv rules
1329 if (apply_explicit_conv_rules) {
1330 ArrayList candidate_set = new ArrayList ();
1332 foreach (Type param_type in src_types_set){
1333 if (StandardConversionExists (source, param_type))
1334 candidate_set.Add (param_type);
1337 if (candidate_set.Count != 0)
1338 return FindMostEncompassedType (candidate_set);
1344 if (apply_explicit_conv_rules)
1345 return FindMostEncompassingType (src_types_set);
1347 return FindMostEncompassedType (src_types_set);
1351 // Useful in avoiding proliferation of objects
1353 static EmptyExpression priv_fmt_expr;
1356 /// Finds the most specific target Tx according to section 13.4.4
1358 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1359 bool apply_explicit_conv_rules,
1362 ArrayList tgt_types_set = new ArrayList ();
1364 if (priv_fmt_expr == null)
1365 priv_fmt_expr = new EmptyExpression ();
1368 // If any operator converts to T then Tx = T
1370 foreach (MethodInfo mi in me.Methods){
1371 Type ret_type = mi.ReturnType;
1373 if (ret_type == target)
1376 if (apply_explicit_conv_rules) {
1379 // Find the set of applicable user-defined conversion operators, U.
1381 // This set consists of the
1382 // user-defined implicit or explicit conversion operators declared by
1383 // the classes or structs in D that convert from a type encompassing
1384 // or encompassed by S to a type encompassing or encompassed by T
1386 priv_fms_expr.SetType (ret_type);
1387 if (StandardConversionExists (priv_fms_expr, target))
1388 tgt_types_set.Add (ret_type);
1390 priv_fms_expr.SetType (target);
1391 if (StandardConversionExists (priv_fms_expr, ret_type))
1392 tgt_types_set.Add (ret_type);
1396 // Only if T is encompassed by param_type
1398 priv_fms_expr.SetType (ret_type);
1399 if (StandardConversionExists (priv_fms_expr, target))
1400 tgt_types_set.Add (ret_type);
1405 // Explicit conv rules
1407 if (apply_explicit_conv_rules) {
1408 ArrayList candidate_set = new ArrayList ();
1410 foreach (Type ret_type in tgt_types_set){
1411 priv_fmt_expr.SetType (ret_type);
1413 if (StandardConversionExists (priv_fmt_expr, target))
1414 candidate_set.Add (ret_type);
1417 if (candidate_set.Count != 0)
1418 return FindMostEncompassingType (candidate_set);
1422 // Okay, final case !
1424 if (apply_explicit_conv_rules)
1425 return FindMostEncompassedType (tgt_types_set);
1427 return FindMostEncompassingType (tgt_types_set);
1431 /// User-defined Implicit conversions
1433 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1434 Type target, Location loc)
1436 return UserDefinedConversion (ec, source, target, loc, false);
1440 /// User-defined Explicit conversions
1442 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1443 Type target, Location loc)
1445 return UserDefinedConversion (ec, source, target, loc, true);
1449 /// Computes the MethodGroup for the user-defined conversion
1450 /// operators from source_type to target_type. `look_for_explicit'
1451 /// controls whether we should also include the list of explicit
1454 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1455 Type source_type, Type target_type,
1456 Location loc, bool look_for_explicit)
1458 Expression mg1 = null, mg2 = null;
1459 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1463 // FIXME : How does the False operator come into the picture ?
1464 // This doesn't look complete and very correct !
1466 if (target_type == TypeManager.bool_type && !look_for_explicit)
1467 op_name = "op_True";
1469 op_name = "op_Implicit";
1471 MethodGroupExpr union3;
1473 mg1 = MethodLookup (ec, source_type, op_name, loc);
1474 if (source_type.BaseType != null)
1475 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1478 union3 = (MethodGroupExpr) mg2;
1479 else if (mg2 == null)
1480 union3 = (MethodGroupExpr) mg1;
1482 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1484 mg1 = MethodLookup (ec, target_type, op_name, loc);
1487 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1489 union3 = (MethodGroupExpr) mg1;
1492 if (target_type.BaseType != null)
1493 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1497 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1499 union3 = (MethodGroupExpr) mg1;
1502 MethodGroupExpr union4 = null;
1504 if (look_for_explicit) {
1505 op_name = "op_Explicit";
1507 mg5 = MemberLookup (ec, source_type, op_name, loc);
1508 if (source_type.BaseType != null)
1509 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1511 mg7 = MemberLookup (ec, target_type, op_name, loc);
1512 if (target_type.BaseType != null)
1513 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1515 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1516 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1518 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1521 return Invocation.MakeUnionSet (union3, union4, loc);
1525 /// User-defined conversions
1527 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1528 Type target, Location loc,
1529 bool look_for_explicit)
1531 MethodGroupExpr union;
1532 Type source_type = source.Type;
1533 MethodBase method = null;
1535 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1539 Type most_specific_source, most_specific_target;
1542 foreach (MethodBase m in union.Methods){
1543 Console.WriteLine ("Name: " + m.Name);
1544 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1548 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1549 if (most_specific_source == null)
1552 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1553 if (most_specific_target == null)
1558 foreach (MethodBase mb in union.Methods){
1559 ParameterData pd = Invocation.GetParameterData (mb);
1560 MethodInfo mi = (MethodInfo) mb;
1562 if (pd.ParameterType (0) == most_specific_source &&
1563 mi.ReturnType == most_specific_target) {
1569 if (method == null || count > 1)
1574 // This will do the conversion to the best match that we
1575 // found. Now we need to perform an implict standard conversion
1576 // if the best match was not the type that we were requested
1579 if (look_for_explicit)
1580 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1582 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1588 e = new UserCast ((MethodInfo) method, source, loc);
1589 if (e.Type != target){
1590 if (!look_for_explicit)
1591 e = ConvertImplicitStandard (ec, e, target, loc);
1593 e = ConvertExplicitStandard (ec, e, target, loc);
1599 /// Converts implicitly the resolved expression `expr' into the
1600 /// `target_type'. It returns a new expression that can be used
1601 /// in a context that expects a `target_type'.
1603 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1604 Type target_type, Location loc)
1606 Type expr_type = expr.Type;
1609 if (expr_type == target_type)
1612 if (target_type == null)
1613 throw new Exception ("Target type is null");
1615 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1619 e = ImplicitUserConversion (ec, expr, target_type, loc);
1628 /// Attempts to apply the `Standard Implicit
1629 /// Conversion' rules to the expression `expr' into
1630 /// the `target_type'. It returns a new expression
1631 /// that can be used in a context that expects a
1634 /// This is different from `ConvertImplicit' in that the
1635 /// user defined implicit conversions are excluded.
1637 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1638 Type target_type, Location loc)
1640 Type expr_type = expr.Type;
1643 if (expr_type == target_type)
1646 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1650 e = ImplicitReferenceConversion (expr, target_type);
1654 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1655 IntLiteral i = (IntLiteral) expr;
1658 return new EmptyCast (expr, target_type);
1662 if (expr_type.IsPointer){
1663 if (target_type == TypeManager.void_ptr_type)
1664 return new EmptyCast (expr, target_type);
1667 // yep, comparing pointer types cant be done with
1668 // t1 == t2, we have to compare their element types.
1670 if (target_type.IsPointer){
1671 if (target_type.GetElementType()==expr_type.GetElementType())
1676 if (target_type.IsPointer){
1677 if (expr is NullLiteral)
1678 return new EmptyCast (expr, target_type);
1686 /// Attemps to perform an implict constant conversion of the IntConstant
1687 /// into a different data type using casts (See Implicit Constant
1688 /// Expression Conversions)
1690 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1692 int value = ic.Value;
1695 // FIXME: This could return constants instead of EmptyCasts
1697 if (target_type == TypeManager.sbyte_type){
1698 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1699 return new SByteConstant ((sbyte) value);
1700 } else if (target_type == TypeManager.byte_type){
1701 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1702 return new ByteConstant ((byte) value);
1703 } else if (target_type == TypeManager.short_type){
1704 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1705 return new ShortConstant ((short) value);
1706 } else if (target_type == TypeManager.ushort_type){
1707 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1708 return new UShortConstant ((ushort) value);
1709 } else if (target_type == TypeManager.uint32_type){
1711 return new UIntConstant ((uint) value);
1712 } else if (target_type == TypeManager.uint64_type){
1714 // we can optimize this case: a positive int32
1715 // always fits on a uint64. But we need an opcode
1719 return new ULongConstant ((ulong) value);
1720 } else if (target_type == TypeManager.double_type)
1721 return new DoubleConstant ((double) value);
1722 else if (target_type == TypeManager.float_type)
1723 return new FloatConstant ((float) value);
1725 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1726 Type underlying = TypeManager.EnumToUnderlying (target_type);
1727 Constant e = (Constant) ic;
1730 // Possibly, we need to create a different 0 literal before passing
1733 if (underlying == TypeManager.int64_type)
1734 e = new LongLiteral (0);
1735 else if (underlying == TypeManager.uint64_type)
1736 e = new ULongLiteral (0);
1738 return new EnumConstant (e, target_type);
1743 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1745 string msg = "Cannot convert implicitly from `"+
1746 TypeManager.CSharpName (source) + "' to `" +
1747 TypeManager.CSharpName (target) + "'";
1749 Report.Error (29, loc, msg);
1753 /// Attemptes to implicityly convert `target' into `type', using
1754 /// ConvertImplicit. If there is no implicit conversion, then
1755 /// an error is signaled
1757 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1758 Type target_type, Location loc)
1762 e = ConvertImplicit (ec, source, target_type, loc);
1766 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1767 Report.Error (664, loc,
1768 "Double literal cannot be implicitly converted to " +
1769 "float type, use F suffix to create a float literal");
1772 Error_CannotConvertImplicit (loc, source.Type, target_type);
1778 /// Performs the explicit numeric conversions
1780 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1782 Type expr_type = expr.Type;
1785 // If we have an enumeration, extract the underlying type,
1786 // use this during the comparison, but wrap around the original
1789 Type real_target_type = target_type;
1791 if (TypeManager.IsEnumType (real_target_type))
1792 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1794 if (StandardConversionExists (expr, real_target_type)){
1795 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1797 if (real_target_type != target_type)
1798 return new EmptyCast (ce, target_type);
1802 if (expr_type == TypeManager.sbyte_type){
1804 // From sbyte to byte, ushort, uint, ulong, char
1806 if (real_target_type == TypeManager.byte_type)
1807 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1808 if (real_target_type == TypeManager.ushort_type)
1809 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1810 if (real_target_type == TypeManager.uint32_type)
1811 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1812 if (real_target_type == TypeManager.uint64_type)
1813 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1814 if (real_target_type == TypeManager.char_type)
1815 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1816 } else if (expr_type == TypeManager.byte_type){
1818 // From byte to sbyte and char
1820 if (real_target_type == TypeManager.sbyte_type)
1821 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1822 if (real_target_type == TypeManager.char_type)
1823 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1824 } else if (expr_type == TypeManager.short_type){
1826 // From short to sbyte, byte, ushort, uint, ulong, char
1828 if (real_target_type == TypeManager.sbyte_type)
1829 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1830 if (real_target_type == TypeManager.byte_type)
1831 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1832 if (real_target_type == TypeManager.ushort_type)
1833 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1834 if (real_target_type == TypeManager.uint32_type)
1835 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1836 if (real_target_type == TypeManager.uint64_type)
1837 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1838 if (real_target_type == TypeManager.char_type)
1839 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1840 } else if (expr_type == TypeManager.ushort_type){
1842 // From ushort to sbyte, byte, short, char
1844 if (real_target_type == TypeManager.sbyte_type)
1845 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1846 if (real_target_type == TypeManager.byte_type)
1847 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1848 if (real_target_type == TypeManager.short_type)
1849 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1850 if (real_target_type == TypeManager.char_type)
1851 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1852 } else if (expr_type == TypeManager.int32_type){
1854 // From int to sbyte, byte, short, ushort, uint, ulong, char
1856 if (real_target_type == TypeManager.sbyte_type)
1857 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1858 if (real_target_type == TypeManager.byte_type)
1859 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1860 if (real_target_type == TypeManager.short_type)
1861 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1862 if (real_target_type == TypeManager.ushort_type)
1863 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1864 if (real_target_type == TypeManager.uint32_type)
1865 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1866 if (real_target_type == TypeManager.uint64_type)
1867 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1868 if (real_target_type == TypeManager.char_type)
1869 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1870 } else if (expr_type == TypeManager.uint32_type){
1872 // From uint to sbyte, byte, short, ushort, int, char
1874 if (real_target_type == TypeManager.sbyte_type)
1875 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1876 if (real_target_type == TypeManager.byte_type)
1877 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1878 if (real_target_type == TypeManager.short_type)
1879 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1880 if (real_target_type == TypeManager.ushort_type)
1881 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1882 if (real_target_type == TypeManager.int32_type)
1883 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1884 if (real_target_type == TypeManager.char_type)
1885 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1886 } else if (expr_type == TypeManager.int64_type){
1888 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1890 if (real_target_type == TypeManager.sbyte_type)
1891 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1892 if (real_target_type == TypeManager.byte_type)
1893 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1894 if (real_target_type == TypeManager.short_type)
1895 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1896 if (real_target_type == TypeManager.ushort_type)
1897 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1898 if (real_target_type == TypeManager.int32_type)
1899 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1900 if (real_target_type == TypeManager.uint32_type)
1901 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1902 if (real_target_type == TypeManager.uint64_type)
1903 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1904 if (real_target_type == TypeManager.char_type)
1905 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1906 } else if (expr_type == TypeManager.uint64_type){
1908 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1910 if (real_target_type == TypeManager.sbyte_type)
1911 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1912 if (real_target_type == TypeManager.byte_type)
1913 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1914 if (real_target_type == TypeManager.short_type)
1915 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1916 if (real_target_type == TypeManager.ushort_type)
1917 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1918 if (real_target_type == TypeManager.int32_type)
1919 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1920 if (real_target_type == TypeManager.uint32_type)
1921 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1922 if (real_target_type == TypeManager.int64_type)
1923 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1924 if (real_target_type == TypeManager.char_type)
1925 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1926 } else if (expr_type == TypeManager.char_type){
1928 // From char to sbyte, byte, short
1930 if (real_target_type == TypeManager.sbyte_type)
1931 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1932 if (real_target_type == TypeManager.byte_type)
1933 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1934 if (real_target_type == TypeManager.short_type)
1935 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1936 } else if (expr_type == TypeManager.float_type){
1938 // From float to sbyte, byte, short,
1939 // ushort, int, uint, long, ulong, char
1942 if (real_target_type == TypeManager.sbyte_type)
1943 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1944 if (real_target_type == TypeManager.byte_type)
1945 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1946 if (real_target_type == TypeManager.short_type)
1947 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1948 if (real_target_type == TypeManager.ushort_type)
1949 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1950 if (real_target_type == TypeManager.int32_type)
1951 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1952 if (real_target_type == TypeManager.uint32_type)
1953 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1954 if (real_target_type == TypeManager.int64_type)
1955 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1956 if (real_target_type == TypeManager.uint64_type)
1957 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1958 if (real_target_type == TypeManager.char_type)
1959 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1960 } else if (expr_type == TypeManager.double_type){
1962 // From double to byte, byte, short,
1963 // ushort, int, uint, long, ulong,
1964 // char, float or decimal
1966 if (real_target_type == TypeManager.sbyte_type)
1967 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1968 if (real_target_type == TypeManager.byte_type)
1969 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1970 if (real_target_type == TypeManager.short_type)
1971 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1972 if (real_target_type == TypeManager.ushort_type)
1973 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1974 if (real_target_type == TypeManager.int32_type)
1975 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1976 if (real_target_type == TypeManager.uint32_type)
1977 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1978 if (real_target_type == TypeManager.int64_type)
1979 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1980 if (real_target_type == TypeManager.uint64_type)
1981 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1982 if (real_target_type == TypeManager.char_type)
1983 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1984 if (real_target_type == TypeManager.float_type)
1985 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1988 // decimal is taken care of by the op_Explicit methods.
1994 /// Returns whether an explicit reference conversion can be performed
1995 /// from source_type to target_type
1997 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1999 bool target_is_value_type = target_type.IsValueType;
2001 if (source_type == target_type)
2005 // From object to any reference type
2007 if (source_type == TypeManager.object_type && !target_is_value_type)
2011 // From any class S to any class-type T, provided S is a base class of T
2013 if (target_type.IsSubclassOf (source_type))
2017 // From any interface type S to any interface T provided S is not derived from T
2019 if (source_type.IsInterface && target_type.IsInterface){
2020 if (!target_type.IsSubclassOf (source_type))
2025 // From any class type S to any interface T, provided S is not sealed
2026 // and provided S does not implement T.
2028 if (target_type.IsInterface && !source_type.IsSealed &&
2029 !TypeManager.ImplementsInterface (source_type, target_type))
2033 // From any interface-type S to to any class type T, provided T is not
2034 // sealed, or provided T implements S.
2036 if (source_type.IsInterface &&
2037 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2041 // From an array type S with an element type Se to an array type T with an
2042 // element type Te provided all the following are true:
2043 // * S and T differe only in element type, in other words, S and T
2044 // have the same number of dimensions.
2045 // * Both Se and Te are reference types
2046 // * An explicit referenc conversions exist from Se to Te
2048 if (source_type.IsArray && target_type.IsArray) {
2049 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2051 Type source_element_type = source_type.GetElementType ();
2052 Type target_element_type = target_type.GetElementType ();
2054 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2055 if (ExplicitReferenceConversionExists (source_element_type,
2056 target_element_type))
2062 // From System.Array to any array-type
2063 if (source_type == TypeManager.array_type &&
2064 target_type.IsArray){
2069 // From System delegate to any delegate-type
2071 if (source_type == TypeManager.delegate_type &&
2072 target_type.IsSubclassOf (TypeManager.delegate_type))
2076 // From ICloneable to Array or Delegate types
2078 if (source_type == TypeManager.icloneable_type &&
2079 (target_type == TypeManager.array_type ||
2080 target_type == TypeManager.delegate_type))
2087 /// Implements Explicit Reference conversions
2089 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2091 Type source_type = source.Type;
2092 bool target_is_value_type = target_type.IsValueType;
2095 // From object to any reference type
2097 if (source_type == TypeManager.object_type && !target_is_value_type)
2098 return new ClassCast (source, target_type);
2102 // From any class S to any class-type T, provided S is a base class of T
2104 if (target_type.IsSubclassOf (source_type))
2105 return new ClassCast (source, target_type);
2108 // From any interface type S to any interface T provided S is not derived from T
2110 if (source_type.IsInterface && target_type.IsInterface){
2111 if (TypeManager.ImplementsInterface (source_type, target_type))
2114 return new ClassCast (source, target_type);
2118 // From any class type S to any interface T, provides S is not sealed
2119 // and provided S does not implement T.
2121 if (target_type.IsInterface && !source_type.IsSealed) {
2122 if (TypeManager.ImplementsInterface (source_type, target_type))
2125 return new ClassCast (source, target_type);
2130 // From any interface-type S to to any class type T, provided T is not
2131 // sealed, or provided T implements S.
2133 if (source_type.IsInterface) {
2134 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2135 return new ClassCast (source, target_type);
2140 // From an array type S with an element type Se to an array type T with an
2141 // element type Te provided all the following are true:
2142 // * S and T differe only in element type, in other words, S and T
2143 // have the same number of dimensions.
2144 // * Both Se and Te are reference types
2145 // * An explicit referenc conversions exist from Se to Te
2147 if (source_type.IsArray && target_type.IsArray) {
2148 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2150 Type source_element_type = source_type.GetElementType ();
2151 Type target_element_type = target_type.GetElementType ();
2153 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2154 if (ExplicitReferenceConversionExists (source_element_type,
2155 target_element_type))
2156 return new ClassCast (source, target_type);
2161 // From System.Array to any array-type
2162 if (source_type == TypeManager.array_type &&
2163 target_type.IsArray) {
2164 return new ClassCast (source, target_type);
2168 // From System delegate to any delegate-type
2170 if (source_type == TypeManager.delegate_type &&
2171 target_type.IsSubclassOf (TypeManager.delegate_type))
2172 return new ClassCast (source, target_type);
2175 // From ICloneable to Array or Delegate types
2177 if (source_type == TypeManager.icloneable_type &&
2178 (target_type == TypeManager.array_type ||
2179 target_type == TypeManager.delegate_type))
2180 return new ClassCast (source, target_type);
2186 /// Performs an explicit conversion of the expression `expr' whose
2187 /// type is expr.Type to `target_type'.
2189 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2190 Type target_type, Location loc)
2192 Type expr_type = expr.Type;
2193 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2198 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2203 // Unboxing conversion.
2205 if (expr_type == TypeManager.object_type && target_type.IsValueType){
2206 if (expr is NullLiteral){
2207 Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
2210 return new UnboxCast (expr, target_type);
2216 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2220 // FIXME: Is there any reason we should have EnumConstant
2221 // dealt with here instead of just using always the
2222 // UnderlyingSystemType to wrap the type?
2224 if (expr is EnumConstant)
2225 e = ((EnumConstant) expr).Child;
2227 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2230 Expression t = ConvertImplicit (ec, e, target_type, loc);
2234 t = ConvertNumericExplicit (ec, e, target_type, loc);
2238 Error_CannotConvertType (loc, expr_type, target_type);
2242 ne = ConvertReferenceExplicit (expr, target_type);
2247 if (target_type.IsPointer){
2248 if (expr_type.IsPointer)
2249 return new EmptyCast (expr, target_type);
2251 if (expr_type == TypeManager.sbyte_type ||
2252 expr_type == TypeManager.byte_type ||
2253 expr_type == TypeManager.short_type ||
2254 expr_type == TypeManager.ushort_type ||
2255 expr_type == TypeManager.int32_type ||
2256 expr_type == TypeManager.uint32_type ||
2257 expr_type == TypeManager.uint64_type ||
2258 expr_type == TypeManager.int64_type)
2259 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2261 if (expr_type.IsPointer){
2262 if (target_type == TypeManager.sbyte_type ||
2263 target_type == TypeManager.byte_type ||
2264 target_type == TypeManager.short_type ||
2265 target_type == TypeManager.ushort_type ||
2266 target_type == TypeManager.int32_type ||
2267 target_type == TypeManager.uint32_type ||
2268 target_type == TypeManager.uint64_type ||
2269 target_type == TypeManager.int64_type){
2270 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2273 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2278 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2282 // We should always be able to go from an uint32
2283 // implicitly or explicitly to the other integral
2286 throw new Exception ("Internal compiler error");
2291 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2295 Error_CannotConvertType (loc, expr_type, target_type);
2300 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2302 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2303 Type target_type, Location l)
2305 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2310 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2314 ne = ConvertReferenceExplicit (expr, target_type);
2318 Error_CannotConvertType (l, expr.Type, target_type);
2322 static string ExprClassName (ExprClass c)
2325 case ExprClass.Invalid:
2327 case ExprClass.Value:
2329 case ExprClass.Variable:
2331 case ExprClass.Namespace:
2333 case ExprClass.Type:
2335 case ExprClass.MethodGroup:
2336 return "method group";
2337 case ExprClass.PropertyAccess:
2338 return "property access";
2339 case ExprClass.EventAccess:
2340 return "event access";
2341 case ExprClass.IndexerAccess:
2342 return "indexer access";
2343 case ExprClass.Nothing:
2346 throw new Exception ("Should not happen");
2350 /// Reports that we were expecting `expr' to be of class `expected'
2352 public void Error118 (string expected)
2354 string kind = "Unknown";
2356 kind = ExprClassName (eclass);
2358 Error (118, "Expression denotes a `" + kind +
2359 "' where a `" + expected + "' was expected");
2362 public void Error118 (ResolveFlags flags)
2364 ArrayList valid = new ArrayList (10);
2366 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2367 valid.Add ("variable");
2368 valid.Add ("value");
2371 if ((flags & ResolveFlags.Type) != 0)
2374 if ((flags & ResolveFlags.MethodGroup) != 0)
2375 valid.Add ("method group");
2377 if ((flags & ResolveFlags.SimpleName) != 0)
2378 valid.Add ("simple name");
2380 if (valid.Count == 0)
2381 valid.Add ("unknown");
2383 StringBuilder sb = new StringBuilder ();
2384 for (int i = 0; i < valid.Count; i++) {
2387 else if (i == valid.Count)
2389 sb.Append (valid [i]);
2392 string kind = ExprClassName (eclass);
2394 Error (119, "Expression denotes a `" + kind + "' where " +
2395 "a `" + sb.ToString () + "' was expected");
2398 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2400 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2401 TypeManager.CSharpName (t));
2404 public static void UnsafeError (Location loc)
2406 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2410 /// Converts the IntConstant, UIntConstant, LongConstant or
2411 /// ULongConstant into the integral target_type. Notice
2412 /// that we do not return an `Expression' we do return
2413 /// a boxed integral type.
2415 /// FIXME: Since I added the new constants, we need to
2416 /// also support conversions from CharConstant, ByteConstant,
2417 /// SByteConstant, UShortConstant, ShortConstant
2419 /// This is used by the switch statement, so the domain
2420 /// of work is restricted to the literals above, and the
2421 /// targets are int32, uint32, char, byte, sbyte, ushort,
2422 /// short, uint64 and int64
2424 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2428 if (c.Type == target_type)
2429 return ((Constant) c).GetValue ();
2432 // Make into one of the literals we handle, we dont really care
2433 // about this value as we will just return a few limited types
2435 if (c is EnumConstant)
2436 c = ((EnumConstant)c).WidenToCompilerConstant ();
2438 if (c is IntConstant){
2439 int v = ((IntConstant) c).Value;
2441 if (target_type == TypeManager.uint32_type){
2444 } else if (target_type == TypeManager.char_type){
2445 if (v >= Char.MinValue && v <= Char.MaxValue)
2447 } else if (target_type == TypeManager.byte_type){
2448 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2450 } else if (target_type == TypeManager.sbyte_type){
2451 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2453 } else if (target_type == TypeManager.short_type){
2454 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2456 } else if (target_type == TypeManager.ushort_type){
2457 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2459 } else if (target_type == TypeManager.int64_type)
2461 else if (target_type == TypeManager.uint64_type){
2467 } else if (c is UIntConstant){
2468 uint v = ((UIntConstant) c).Value;
2470 if (target_type == TypeManager.int32_type){
2471 if (v <= Int32.MaxValue)
2473 } else if (target_type == TypeManager.char_type){
2474 if (v >= Char.MinValue && v <= Char.MaxValue)
2476 } else if (target_type == TypeManager.byte_type){
2477 if (v <= Byte.MaxValue)
2479 } else if (target_type == TypeManager.sbyte_type){
2480 if (v <= SByte.MaxValue)
2482 } else if (target_type == TypeManager.short_type){
2483 if (v <= UInt16.MaxValue)
2485 } else if (target_type == TypeManager.ushort_type){
2486 if (v <= UInt16.MaxValue)
2488 } else if (target_type == TypeManager.int64_type)
2490 else if (target_type == TypeManager.uint64_type)
2493 } else if (c is LongConstant){
2494 long v = ((LongConstant) c).Value;
2496 if (target_type == TypeManager.int32_type){
2497 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2499 } else if (target_type == TypeManager.uint32_type){
2500 if (v >= 0 && v <= UInt32.MaxValue)
2502 } else if (target_type == TypeManager.char_type){
2503 if (v >= Char.MinValue && v <= Char.MaxValue)
2505 } else if (target_type == TypeManager.byte_type){
2506 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2508 } else if (target_type == TypeManager.sbyte_type){
2509 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2511 } else if (target_type == TypeManager.short_type){
2512 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2514 } else if (target_type == TypeManager.ushort_type){
2515 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2517 } else if (target_type == TypeManager.uint64_type){
2522 } else if (c is ULongConstant){
2523 ulong v = ((ULongConstant) c).Value;
2525 if (target_type == TypeManager.int32_type){
2526 if (v <= Int32.MaxValue)
2528 } else if (target_type == TypeManager.uint32_type){
2529 if (v <= UInt32.MaxValue)
2531 } else if (target_type == TypeManager.char_type){
2532 if (v >= Char.MinValue && v <= Char.MaxValue)
2534 } else if (target_type == TypeManager.byte_type){
2535 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2537 } else if (target_type == TypeManager.sbyte_type){
2538 if (v <= (int) SByte.MaxValue)
2540 } else if (target_type == TypeManager.short_type){
2541 if (v <= UInt16.MaxValue)
2543 } else if (target_type == TypeManager.ushort_type){
2544 if (v <= UInt16.MaxValue)
2546 } else if (target_type == TypeManager.int64_type){
2547 if (v <= Int64.MaxValue)
2551 } else if (c is ByteConstant){
2552 byte v = ((ByteConstant) c).Value;
2554 if (target_type == TypeManager.int32_type)
2556 else if (target_type == TypeManager.uint32_type)
2558 else if (target_type == TypeManager.char_type)
2560 else if (target_type == TypeManager.sbyte_type){
2561 if (v <= SByte.MaxValue)
2563 } else if (target_type == TypeManager.short_type)
2565 else if (target_type == TypeManager.ushort_type)
2567 else if (target_type == TypeManager.int64_type)
2569 else if (target_type == TypeManager.uint64_type)
2572 } else if (c is SByteConstant){
2573 sbyte v = ((SByteConstant) c).Value;
2575 if (target_type == TypeManager.int32_type)
2577 else if (target_type == TypeManager.uint32_type){
2580 } else if (target_type == TypeManager.char_type){
2583 } else if (target_type == TypeManager.byte_type){
2586 } else if (target_type == TypeManager.short_type)
2588 else if (target_type == TypeManager.ushort_type){
2591 } else if (target_type == TypeManager.int64_type)
2593 else if (target_type == TypeManager.uint64_type){
2598 } else if (c is ShortConstant){
2599 short v = ((ShortConstant) c).Value;
2601 if (target_type == TypeManager.int32_type){
2603 } else if (target_type == TypeManager.uint32_type){
2606 } else if (target_type == TypeManager.char_type){
2609 } else if (target_type == TypeManager.byte_type){
2610 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2612 } else if (target_type == TypeManager.sbyte_type){
2613 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2615 } else if (target_type == TypeManager.ushort_type){
2618 } else if (target_type == TypeManager.int64_type)
2620 else if (target_type == TypeManager.uint64_type)
2624 } else if (c is UShortConstant){
2625 ushort v = ((UShortConstant) c).Value;
2627 if (target_type == TypeManager.int32_type)
2629 else if (target_type == TypeManager.uint32_type)
2631 else if (target_type == TypeManager.char_type){
2632 if (v >= Char.MinValue && v <= Char.MaxValue)
2634 } else if (target_type == TypeManager.byte_type){
2635 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2637 } else if (target_type == TypeManager.sbyte_type){
2638 if (v <= SByte.MaxValue)
2640 } else if (target_type == TypeManager.short_type){
2641 if (v <= Int16.MaxValue)
2643 } else if (target_type == TypeManager.int64_type)
2645 else if (target_type == TypeManager.uint64_type)
2649 } else if (c is CharConstant){
2650 char v = ((CharConstant) c).Value;
2652 if (target_type == TypeManager.int32_type)
2654 else if (target_type == TypeManager.uint32_type)
2656 else if (target_type == TypeManager.byte_type){
2657 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2659 } else if (target_type == TypeManager.sbyte_type){
2660 if (v <= SByte.MaxValue)
2662 } else if (target_type == TypeManager.short_type){
2663 if (v <= Int16.MaxValue)
2665 } else if (target_type == TypeManager.ushort_type)
2667 else if (target_type == TypeManager.int64_type)
2669 else if (target_type == TypeManager.uint64_type)
2674 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2679 // Load the object from the pointer.
2681 public static void LoadFromPtr (ILGenerator ig, Type t)
2683 if (t == TypeManager.int32_type)
2684 ig.Emit (OpCodes.Ldind_I4);
2685 else if (t == TypeManager.uint32_type)
2686 ig.Emit (OpCodes.Ldind_U4);
2687 else if (t == TypeManager.short_type)
2688 ig.Emit (OpCodes.Ldind_I2);
2689 else if (t == TypeManager.ushort_type)
2690 ig.Emit (OpCodes.Ldind_U2);
2691 else if (t == TypeManager.char_type)
2692 ig.Emit (OpCodes.Ldind_U2);
2693 else if (t == TypeManager.byte_type)
2694 ig.Emit (OpCodes.Ldind_U1);
2695 else if (t == TypeManager.sbyte_type)
2696 ig.Emit (OpCodes.Ldind_I1);
2697 else if (t == TypeManager.uint64_type)
2698 ig.Emit (OpCodes.Ldind_I8);
2699 else if (t == TypeManager.int64_type)
2700 ig.Emit (OpCodes.Ldind_I8);
2701 else if (t == TypeManager.float_type)
2702 ig.Emit (OpCodes.Ldind_R4);
2703 else if (t == TypeManager.double_type)
2704 ig.Emit (OpCodes.Ldind_R8);
2705 else if (t == TypeManager.bool_type)
2706 ig.Emit (OpCodes.Ldind_I1);
2707 else if (t == TypeManager.intptr_type)
2708 ig.Emit (OpCodes.Ldind_I);
2709 else if (TypeManager.IsEnumType (t)) {
2710 if (t == TypeManager.enum_type)
2711 ig.Emit (OpCodes.Ldind_Ref);
2713 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2714 } else if (t.IsValueType)
2715 ig.Emit (OpCodes.Ldobj, t);
2717 ig.Emit (OpCodes.Ldind_Ref);
2721 // The stack contains the pointer and the value of type `type'
2723 public static void StoreFromPtr (ILGenerator ig, Type type)
2725 if (TypeManager.IsEnumType (type))
2726 type = TypeManager.EnumToUnderlying (type);
2727 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2728 ig.Emit (OpCodes.Stind_I4);
2729 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2730 ig.Emit (OpCodes.Stind_I8);
2731 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2732 type == TypeManager.ushort_type)
2733 ig.Emit (OpCodes.Stind_I2);
2734 else if (type == TypeManager.float_type)
2735 ig.Emit (OpCodes.Stind_R4);
2736 else if (type == TypeManager.double_type)
2737 ig.Emit (OpCodes.Stind_R8);
2738 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2739 type == TypeManager.bool_type)
2740 ig.Emit (OpCodes.Stind_I1);
2741 else if (type == TypeManager.intptr_type)
2742 ig.Emit (OpCodes.Stind_I);
2743 else if (type.IsValueType)
2744 ig.Emit (OpCodes.Stobj, type);
2746 ig.Emit (OpCodes.Stind_Ref);
2750 // Returns the size of type `t' if known, otherwise, 0
2752 public static int GetTypeSize (Type t)
2754 t = TypeManager.TypeToCoreType (t);
2755 if (t == TypeManager.int32_type ||
2756 t == TypeManager.uint32_type ||
2757 t == TypeManager.float_type)
2759 else if (t == TypeManager.int64_type ||
2760 t == TypeManager.uint64_type ||
2761 t == TypeManager.double_type)
2763 else if (t == TypeManager.byte_type ||
2764 t == TypeManager.sbyte_type ||
2765 t == TypeManager.bool_type)
2767 else if (t == TypeManager.short_type ||
2768 t == TypeManager.char_type ||
2769 t == TypeManager.ushort_type)
2771 else if (t == TypeManager.decimal_type)
2778 // Default implementation of IAssignMethod.CacheTemporaries
2780 public void CacheTemporaries (EmitContext ec)
2784 static void Error_NegativeArrayIndex (Location loc)
2786 Report.Error (284, loc, "Can not create array with a negative size");
2790 // Converts `source' to an int, uint, long or ulong.
2792 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2796 bool old_checked = ec.CheckState;
2797 ec.CheckState = true;
2799 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2800 if (target == null){
2801 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2802 if (target == null){
2803 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2804 if (target == null){
2805 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2807 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2811 ec.CheckState = old_checked;
2814 // Only positive constants are allowed at compile time
2816 if (target is Constant){
2817 if (target is IntConstant){
2818 if (((IntConstant) target).Value < 0){
2819 Error_NegativeArrayIndex (loc);
2824 if (target is LongConstant){
2825 if (((LongConstant) target).Value < 0){
2826 Error_NegativeArrayIndex (loc);
2839 /// This is just a base class for expressions that can
2840 /// appear on statements (invocations, object creation,
2841 /// assignments, post/pre increment and decrement). The idea
2842 /// being that they would support an extra Emition interface that
2843 /// does not leave a result on the stack.
2845 public abstract class ExpressionStatement : Expression {
2848 /// Requests the expression to be emitted in a `statement'
2849 /// context. This means that no new value is left on the
2850 /// stack after invoking this method (constrasted with
2851 /// Emit that will always leave a value on the stack).
2853 public abstract void EmitStatement (EmitContext ec);
2857 /// This kind of cast is used to encapsulate the child
2858 /// whose type is child.Type into an expression that is
2859 /// reported to return "return_type". This is used to encapsulate
2860 /// expressions which have compatible types, but need to be dealt
2861 /// at higher levels with.
2863 /// For example, a "byte" expression could be encapsulated in one
2864 /// of these as an "unsigned int". The type for the expression
2865 /// would be "unsigned int".
2868 public class EmptyCast : Expression {
2869 protected Expression child;
2871 public EmptyCast (Expression child, Type return_type)
2873 eclass = child.eclass;
2878 public Expression Peel ()
2880 if (child is EmptyCast)
2881 return ((EmptyCast) child).Peel ();
2885 public override Expression DoResolve (EmitContext ec)
2887 // This should never be invoked, we are born in fully
2888 // initialized state.
2893 public override void Emit (EmitContext ec)
2900 /// This class is used to wrap literals which belong inside Enums
2902 public class EnumConstant : Constant {
2903 public Constant Child;
2905 public EnumConstant (Constant child, Type enum_type)
2907 eclass = child.eclass;
2912 public override Expression DoResolve (EmitContext ec)
2914 // This should never be invoked, we are born in fully
2915 // initialized state.
2920 public override void Emit (EmitContext ec)
2925 public override object GetValue ()
2927 return Child.GetValue ();
2931 // Converts from one of the valid underlying types for an enumeration
2932 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2933 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2935 public Constant WidenToCompilerConstant ()
2937 Type t = TypeManager.EnumToUnderlying (Child.Type);
2938 object v = ((Constant) Child).GetValue ();;
2940 if (t == TypeManager.int32_type)
2941 return new IntConstant ((int) v);
2942 if (t == TypeManager.uint32_type)
2943 return new UIntConstant ((uint) v);
2944 if (t == TypeManager.int64_type)
2945 return new LongConstant ((long) v);
2946 if (t == TypeManager.uint64_type)
2947 return new ULongConstant ((ulong) v);
2948 if (t == TypeManager.short_type)
2949 return new ShortConstant ((short) v);
2950 if (t == TypeManager.ushort_type)
2951 return new UShortConstant ((ushort) v);
2952 if (t == TypeManager.byte_type)
2953 return new ByteConstant ((byte) v);
2954 if (t == TypeManager.sbyte_type)
2955 return new SByteConstant ((sbyte) v);
2957 throw new Exception ("Invalid enumeration underlying type: " + t);
2961 // Extracts the value in the enumeration on its native representation
2963 public object GetPlainValue ()
2965 Type t = TypeManager.EnumToUnderlying (Child.Type);
2966 object v = ((Constant) Child).GetValue ();;
2968 if (t == TypeManager.int32_type)
2970 if (t == TypeManager.uint32_type)
2972 if (t == TypeManager.int64_type)
2974 if (t == TypeManager.uint64_type)
2976 if (t == TypeManager.short_type)
2978 if (t == TypeManager.ushort_type)
2980 if (t == TypeManager.byte_type)
2982 if (t == TypeManager.sbyte_type)
2988 public override string AsString ()
2990 return Child.AsString ();
2993 public override DoubleConstant ConvertToDouble ()
2995 return Child.ConvertToDouble ();
2998 public override FloatConstant ConvertToFloat ()
3000 return Child.ConvertToFloat ();
3003 public override ULongConstant ConvertToULong ()
3005 return Child.ConvertToULong ();
3008 public override LongConstant ConvertToLong ()
3010 return Child.ConvertToLong ();
3013 public override UIntConstant ConvertToUInt ()
3015 return Child.ConvertToUInt ();
3018 public override IntConstant ConvertToInt ()
3020 return Child.ConvertToInt ();
3025 /// This kind of cast is used to encapsulate Value Types in objects.
3027 /// The effect of it is to box the value type emitted by the previous
3030 public class BoxedCast : EmptyCast {
3032 public BoxedCast (Expression expr)
3033 : base (expr, TypeManager.object_type)
3037 public override Expression DoResolve (EmitContext ec)
3039 // This should never be invoked, we are born in fully
3040 // initialized state.
3045 public override void Emit (EmitContext ec)
3049 ec.ig.Emit (OpCodes.Box, child.Type);
3053 public class UnboxCast : EmptyCast {
3054 public UnboxCast (Expression expr, Type return_type)
3055 : base (expr, return_type)
3059 public override Expression DoResolve (EmitContext ec)
3061 // This should never be invoked, we are born in fully
3062 // initialized state.
3067 public override void Emit (EmitContext ec)
3070 ILGenerator ig = ec.ig;
3073 ig.Emit (OpCodes.Unbox, t);
3075 LoadFromPtr (ig, t);
3080 /// This is used to perform explicit numeric conversions.
3082 /// Explicit numeric conversions might trigger exceptions in a checked
3083 /// context, so they should generate the conv.ovf opcodes instead of
3086 public class ConvCast : EmptyCast {
3087 public enum Mode : byte {
3088 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3090 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3091 U2_I1, U2_U1, U2_I2, U2_CH,
3092 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3093 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3094 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3095 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3096 CH_I1, CH_U1, CH_I2,
3097 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3098 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3104 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3105 : base (child, return_type)
3107 checked_state = ec.CheckState;
3111 public override Expression DoResolve (EmitContext ec)
3113 // This should never be invoked, we are born in fully
3114 // initialized state.
3119 public override void Emit (EmitContext ec)
3121 ILGenerator ig = ec.ig;
3127 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3128 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3129 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3130 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3131 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3133 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3134 case Mode.U1_CH: /* nothing */ break;
3136 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3137 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3138 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3139 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3140 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3141 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3143 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3144 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3145 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3146 case Mode.U2_CH: /* nothing */ break;
3148 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3149 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3150 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3151 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3152 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3153 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3154 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3156 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3157 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3158 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3159 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3160 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3161 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3163 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3164 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3165 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3166 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3167 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3168 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3169 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3170 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3172 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3173 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3174 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3175 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3176 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3177 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3178 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3179 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3181 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3182 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3183 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3185 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3186 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3187 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3188 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3189 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3190 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3191 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3192 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3193 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3195 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3196 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3197 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3198 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3199 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3200 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3201 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3202 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3203 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3204 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3208 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3209 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3210 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3211 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3212 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3214 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3215 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3217 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3218 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3219 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3220 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3221 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3222 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3224 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3225 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3226 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3227 case Mode.U2_CH: /* nothing */ break;
3229 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3230 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3231 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3232 case Mode.I4_U4: /* nothing */ break;
3233 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3234 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3235 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3237 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3238 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3239 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3240 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3241 case Mode.U4_I4: /* nothing */ break;
3242 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3244 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3245 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3246 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3247 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3248 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3249 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3250 case Mode.I8_U8: /* nothing */ break;
3251 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3253 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3254 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3255 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3256 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3257 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3258 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3259 case Mode.U8_I8: /* nothing */ break;
3260 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3262 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3263 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3264 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3266 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3267 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3268 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3269 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3270 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3271 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3272 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3273 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3274 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3276 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3277 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3278 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3279 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3280 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3281 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3282 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3283 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3284 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3285 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3291 public class OpcodeCast : EmptyCast {
3295 public OpcodeCast (Expression child, Type return_type, OpCode op)
3296 : base (child, return_type)
3300 second_valid = false;
3303 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3304 : base (child, return_type)
3309 second_valid = true;
3312 public override Expression DoResolve (EmitContext ec)
3314 // This should never be invoked, we are born in fully
3315 // initialized state.
3320 public override void Emit (EmitContext ec)
3331 /// This kind of cast is used to encapsulate a child and cast it
3332 /// to the class requested
3334 public class ClassCast : EmptyCast {
3335 public ClassCast (Expression child, Type return_type)
3336 : base (child, return_type)
3341 public override Expression DoResolve (EmitContext ec)
3343 // This should never be invoked, we are born in fully
3344 // initialized state.
3349 public override void Emit (EmitContext ec)
3353 ec.ig.Emit (OpCodes.Castclass, type);
3359 /// SimpleName expressions are initially formed of a single
3360 /// word and it only happens at the beginning of the expression.
3364 /// The expression will try to be bound to a Field, a Method
3365 /// group or a Property. If those fail we pass the name to our
3366 /// caller and the SimpleName is compounded to perform a type
3367 /// lookup. The idea behind this process is that we want to avoid
3368 /// creating a namespace map from the assemblies, as that requires
3369 /// the GetExportedTypes function to be called and a hashtable to
3370 /// be constructed which reduces startup time. If later we find
3371 /// that this is slower, we should create a `NamespaceExpr' expression
3372 /// that fully participates in the resolution process.
3374 /// For example `System.Console.WriteLine' is decomposed into
3375 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3377 /// The first SimpleName wont produce a match on its own, so it will
3379 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3381 /// System.Console will produce a TypeExpr match.
3383 /// The downside of this is that we might be hitting `LookupType' too many
3384 /// times with this scheme.
3386 public class SimpleName : Expression, ITypeExpression {
3387 public readonly string Name;
3389 public SimpleName (string name, Location l)
3395 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3397 if (ec.IsFieldInitializer)
3400 "A field initializer cannot reference the non-static field, " +
3401 "method or property `"+name+"'");
3405 "An object reference is required " +
3406 "for the non-static field `"+name+"'");
3410 // Checks whether we are trying to access an instance
3411 // property, method or field from a static body.
3413 Expression MemberStaticCheck (EmitContext ec, Expression e)
3415 if (e is IMemberExpr){
3416 IMemberExpr member = (IMemberExpr) e;
3418 if (!member.IsStatic){
3419 Error_ObjectRefRequired (ec, loc, Name);
3427 public override Expression DoResolve (EmitContext ec)
3429 return SimpleNameResolve (ec, null, false);
3432 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3434 return SimpleNameResolve (ec, right_side, false);
3438 public Expression DoResolveAllowStatic (EmitContext ec)
3440 return SimpleNameResolve (ec, null, true);
3443 public Expression DoResolveType (EmitContext ec)
3446 // Stage 3: Lookup symbol in the various namespaces.
3448 DeclSpace ds = ec.DeclSpace;
3452 if (ec.ResolvingTypeTree){
3453 int errors = Report.Errors;
3454 Type dt = ec.DeclSpace.FindType (loc, Name);
3455 if (Report.Errors != errors)
3459 return new TypeExpr (dt, loc);
3462 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3463 return new TypeExpr (t, loc);
3467 // Stage 2 part b: Lookup up if we are an alias to a type
3470 // Since we are cheating: we only do the Alias lookup for
3471 // namespaces if the name does not include any dots in it
3474 alias_value = ec.DeclSpace.LookupAlias (Name);
3476 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3477 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3478 return new TypeExpr (t, loc);
3480 // we have alias value, but it isn't Type, so try if it's namespace
3481 return new SimpleName (alias_value, loc);
3484 // No match, maybe our parent can compose us
3485 // into something meaningful.
3490 /// 7.5.2: Simple Names.
3492 /// Local Variables and Parameters are handled at
3493 /// parse time, so they never occur as SimpleNames.
3495 /// The `allow_static' flag is used by MemberAccess only
3496 /// and it is used to inform us that it is ok for us to
3497 /// avoid the static check, because MemberAccess might end
3498 /// up resolving the Name as a Type name and the access as
3499 /// a static type access.
3501 /// ie: Type Type; .... { Type.GetType (""); }
3503 /// Type is both an instance variable and a Type; Type.GetType
3504 /// is the static method not an instance method of type.
3506 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3508 Expression e = null;
3511 // Stage 1: Performed by the parser (binding to locals or parameters).
3513 Block current_block = ec.CurrentBlock;
3514 if (current_block != null && current_block.GetVariableInfo (Name) != null){
3515 LocalVariableReference var;
3517 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3519 if (right_side != null)
3520 return var.ResolveLValue (ec, right_side);
3522 return var.Resolve (ec);
3525 if (current_block != null){
3527 Parameter par = null;
3528 Parameters pars = current_block.Parameters;
3530 par = pars.GetParameterByName (Name, out idx);
3533 ParameterReference param;
3535 param = new ParameterReference (pars, idx, Name, loc);
3537 if (right_side != null)
3538 return param.ResolveLValue (ec, right_side);
3540 return param.Resolve (ec);
3545 // Stage 2: Lookup members
3548 DeclSpace lookup_ds = ec.DeclSpace;
3550 if (lookup_ds.TypeBuilder == null)
3553 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3557 lookup_ds =lookup_ds.Parent;
3558 } while (lookup_ds != null);
3560 if (e == null && ec.ContainerType != null)
3561 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3564 return DoResolveType (ec);
3569 if (e is IMemberExpr) {
3570 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3574 IMemberExpr me = e as IMemberExpr;
3578 // This fails if ResolveMemberAccess() was unable to decide whether
3579 // it's a field or a type of the same name.
3580 if (!me.IsStatic && (me.InstanceExpression == null))
3584 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3585 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3586 "outer type `" + me.DeclaringType + "' via nested type `" +
3587 me.InstanceExpression.Type + "'");
3591 if (right_side != null)
3592 e = e.DoResolveLValue (ec, right_side);
3594 e = e.DoResolve (ec);
3599 if (ec.IsStatic || ec.IsFieldInitializer){
3603 return MemberStaticCheck (ec, e);
3608 public override void Emit (EmitContext ec)
3611 // If this is ever reached, then we failed to
3612 // find the name as a namespace
3615 Error (103, "The name `" + Name +
3616 "' does not exist in the class `" +
3617 ec.DeclSpace.Name + "'");
3620 public override string ToString ()
3627 /// Fully resolved expression that evaluates to a type
3629 public class TypeExpr : Expression, ITypeExpression {
3630 public TypeExpr (Type t, Location l)
3633 eclass = ExprClass.Type;
3637 public virtual Expression DoResolveType (EmitContext ec)
3642 override public Expression DoResolve (EmitContext ec)
3647 override public void Emit (EmitContext ec)
3649 throw new Exception ("Should never be called");
3652 public override string ToString ()
3654 return Type.ToString ();
3659 /// Used to create types from a fully qualified name. These are just used
3660 /// by the parser to setup the core types. A TypeLookupExpression is always
3661 /// classified as a type.
3663 public class TypeLookupExpression : TypeExpr {
3666 public TypeLookupExpression (string name) : base (null, Location.Null)
3671 public override Expression DoResolveType (EmitContext ec)
3674 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3678 public override Expression DoResolve (EmitContext ec)
3680 return DoResolveType (ec);
3683 public override void Emit (EmitContext ec)
3685 throw new Exception ("Should never be called");
3688 public override string ToString ()
3695 /// MethodGroup Expression.
3697 /// This is a fully resolved expression that evaluates to a type
3699 public class MethodGroupExpr : Expression, IMemberExpr {
3700 public MethodBase [] Methods;
3701 Expression instance_expression = null;
3702 bool is_explicit_impl = false;
3704 public MethodGroupExpr (MemberInfo [] mi, Location l)
3706 Methods = new MethodBase [mi.Length];
3707 mi.CopyTo (Methods, 0);
3708 eclass = ExprClass.MethodGroup;
3709 type = TypeManager.object_type;
3713 public MethodGroupExpr (ArrayList list, Location l)
3715 Methods = new MethodBase [list.Count];
3718 list.CopyTo (Methods, 0);
3720 foreach (MemberInfo m in list){
3721 if (!(m is MethodBase)){
3722 Console.WriteLine ("Name " + m.Name);
3723 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3729 eclass = ExprClass.MethodGroup;
3730 type = TypeManager.object_type;
3733 public Type DeclaringType {
3735 return Methods [0].DeclaringType;
3740 // `A method group may have associated an instance expression'
3742 public Expression InstanceExpression {
3744 return instance_expression;
3748 instance_expression = value;
3752 public bool IsExplicitImpl {
3754 return is_explicit_impl;
3758 is_explicit_impl = value;
3762 public string Name {
3764 return Methods [0].Name;
3768 public bool IsInstance {
3770 foreach (MethodBase mb in Methods)
3778 public bool IsStatic {
3780 foreach (MethodBase mb in Methods)
3788 override public Expression DoResolve (EmitContext ec)
3790 if (instance_expression != null) {
3791 instance_expression = instance_expression.DoResolve (ec);
3792 if (instance_expression == null)
3799 public void ReportUsageError ()
3801 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3802 Methods [0].Name + "()' is referenced without parentheses");
3805 override public void Emit (EmitContext ec)
3807 ReportUsageError ();
3810 bool RemoveMethods (bool keep_static)
3812 ArrayList smethods = new ArrayList ();
3814 foreach (MethodBase mb in Methods){
3815 if (mb.IsStatic == keep_static)
3819 if (smethods.Count == 0)
3822 Methods = new MethodBase [smethods.Count];
3823 smethods.CopyTo (Methods, 0);
3829 /// Removes any instance methods from the MethodGroup, returns
3830 /// false if the resulting set is empty.
3832 public bool RemoveInstanceMethods ()
3834 return RemoveMethods (true);
3838 /// Removes any static methods from the MethodGroup, returns
3839 /// false if the resulting set is empty.
3841 public bool RemoveStaticMethods ()
3843 return RemoveMethods (false);
3848 /// Fully resolved expression that evaluates to a Field
3850 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3851 public readonly FieldInfo FieldInfo;
3852 Expression instance_expr;
3854 public FieldExpr (FieldInfo fi, Location l)
3857 eclass = ExprClass.Variable;
3858 type = fi.FieldType;
3862 public string Name {
3864 return FieldInfo.Name;
3868 public bool IsInstance {
3870 return !FieldInfo.IsStatic;
3874 public bool IsStatic {
3876 return FieldInfo.IsStatic;
3880 public Type DeclaringType {
3882 return FieldInfo.DeclaringType;
3886 public Expression InstanceExpression {
3888 return instance_expr;
3892 instance_expr = value;
3896 override public Expression DoResolve (EmitContext ec)
3898 if (!FieldInfo.IsStatic){
3899 if (instance_expr == null){
3900 throw new Exception ("non-static FieldExpr without instance var\n" +
3901 "You have to assign the Instance variable\n" +
3902 "Of the FieldExpr to set this\n");
3905 // Resolve the field's instance expression while flow analysis is turned
3906 // off: when accessing a field "a.b", we must check whether the field
3907 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3908 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3909 ResolveFlags.DisableFlowAnalysis);
3910 if (instance_expr == null)
3914 // If the instance expression is a local variable or parameter.
3915 IVariable var = instance_expr as IVariable;
3916 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3922 void Report_AssignToReadonly (bool is_instance)
3927 msg = "Readonly field can not be assigned outside " +
3928 "of constructor or variable initializer";
3930 msg = "A static readonly field can only be assigned in " +
3931 "a static constructor";
3933 Report.Error (is_instance ? 191 : 198, loc, msg);
3936 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3938 IVariable var = instance_expr as IVariable;
3940 var.SetFieldAssigned (ec, FieldInfo.Name);
3942 Expression e = DoResolve (ec);
3947 if (!FieldInfo.IsInitOnly)
3951 // InitOnly fields can only be assigned in constructors
3954 if (ec.IsConstructor)
3957 Report_AssignToReadonly (true);
3962 override public void Emit (EmitContext ec)
3964 ILGenerator ig = ec.ig;
3965 bool is_volatile = false;
3967 if (FieldInfo is FieldBuilder){
3968 FieldBase f = TypeManager.GetField (FieldInfo);
3970 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3973 f.status |= Field.Status.USED;
3976 if (FieldInfo.IsStatic){
3978 ig.Emit (OpCodes.Volatile);
3980 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3982 if (instance_expr.Type.IsValueType){
3984 LocalTemporary tempo = null;
3986 if (!(instance_expr is IMemoryLocation)){
3987 tempo = new LocalTemporary (
3988 ec, instance_expr.Type);
3990 InstanceExpression.Emit (ec);
3994 ml = (IMemoryLocation) instance_expr;
3996 ml.AddressOf (ec, AddressOp.Load);
3998 instance_expr.Emit (ec);
4001 ig.Emit (OpCodes.Volatile);
4003 ig.Emit (OpCodes.Ldfld, FieldInfo);
4007 public void EmitAssign (EmitContext ec, Expression source)
4009 FieldAttributes fa = FieldInfo.Attributes;
4010 bool is_static = (fa & FieldAttributes.Static) != 0;
4011 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4012 ILGenerator ig = ec.ig;
4014 if (is_readonly && !ec.IsConstructor){
4015 Report_AssignToReadonly (!is_static);
4020 Expression instance = instance_expr;
4022 if (instance.Type.IsValueType){
4023 if (instance is IMemoryLocation){
4024 IMemoryLocation ml = (IMemoryLocation) instance;
4026 ml.AddressOf (ec, AddressOp.Store);
4028 throw new Exception ("The " + instance + " of type " +
4030 " represents a ValueType and does " +
4031 "not implement IMemoryLocation");
4037 if (FieldInfo is FieldBuilder){
4038 FieldBase f = TypeManager.GetField (FieldInfo);
4040 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4041 ig.Emit (OpCodes.Volatile);
4045 ig.Emit (OpCodes.Stsfld, FieldInfo);
4047 ig.Emit (OpCodes.Stfld, FieldInfo);
4049 if (FieldInfo is FieldBuilder){
4050 FieldBase f = TypeManager.GetField (FieldInfo);
4052 f.status |= Field.Status.ASSIGNED;
4056 public void AddressOf (EmitContext ec, AddressOp mode)
4058 ILGenerator ig = ec.ig;
4060 if (FieldInfo is FieldBuilder){
4061 FieldBase f = TypeManager.GetField (FieldInfo);
4062 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4063 ig.Emit (OpCodes.Volatile);
4066 if (FieldInfo is FieldBuilder){
4067 FieldBase f = TypeManager.GetField (FieldInfo);
4069 if ((mode & AddressOp.Store) != 0)
4070 f.status |= Field.Status.ASSIGNED;
4071 if ((mode & AddressOp.Load) != 0)
4072 f.status |= Field.Status.USED;
4076 // Handle initonly fields specially: make a copy and then
4077 // get the address of the copy.
4079 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4083 local = ig.DeclareLocal (type);
4084 ig.Emit (OpCodes.Stloc, local);
4085 ig.Emit (OpCodes.Ldloca, local);
4089 if (FieldInfo.IsStatic)
4090 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4092 if (instance_expr is IMemoryLocation)
4093 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4095 instance_expr.Emit (ec);
4096 ig.Emit (OpCodes.Ldflda, FieldInfo);
4102 /// Expression that evaluates to a Property. The Assign class
4103 /// might set the `Value' expression if we are in an assignment.
4105 /// This is not an LValue because we need to re-write the expression, we
4106 /// can not take data from the stack and store it.
4108 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4109 public readonly PropertyInfo PropertyInfo;
4112 // This is set externally by the `BaseAccess' class
4115 MethodInfo getter, setter;
4117 bool must_do_cs1540_check;
4119 Expression instance_expr;
4121 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4124 eclass = ExprClass.PropertyAccess;
4128 type = TypeManager.TypeToCoreType (pi.PropertyType);
4130 ResolveAccessors (ec);
4133 public string Name {
4135 return PropertyInfo.Name;
4139 public bool IsInstance {
4145 public bool IsStatic {
4151 public Type DeclaringType {
4153 return PropertyInfo.DeclaringType;
4158 // The instance expression associated with this expression
4160 public Expression InstanceExpression {
4162 instance_expr = value;
4166 return instance_expr;
4170 public bool VerifyAssignable ()
4172 if (setter == null) {
4173 Report.Error (200, loc,
4174 "The property `" + PropertyInfo.Name +
4175 "' can not be assigned to, as it has not set accessor");
4182 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
4184 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
4185 BindingFlags.Static | BindingFlags.Instance;
4188 group = TypeManager.MemberLookup (
4189 invocation_type, PropertyInfo.DeclaringType,
4190 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
4193 // The first method is the closest to us
4198 foreach (MethodInfo mi in group) {
4199 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
4202 // If only accessible to the current class or children
4204 if (ma == MethodAttributes.Private) {
4205 Type declaring_type = mi.DeclaringType;
4207 if (invocation_type != declaring_type){
4208 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4216 // FamAndAssem requires that we not only derivate, but we are on the
4219 if (ma == MethodAttributes.FamANDAssem){
4220 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4226 // Assembly and FamORAssem succeed if we're in the same assembly.
4227 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
4228 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4234 // We already know that we aren't in the same assembly.
4235 if (ma == MethodAttributes.Assembly)
4238 // Family and FamANDAssem require that we derive.
4239 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
4240 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4243 must_do_cs1540_check = true;
4256 // We also perform the permission checking here, as the PropertyInfo does not
4257 // hold the information for the accessibility of its setter/getter
4259 void ResolveAccessors (EmitContext ec)
4261 getter = GetAccessor (ec.ContainerType, "get");
4262 if ((getter != null) && getter.IsStatic)
4265 setter = GetAccessor (ec.ContainerType, "set");
4266 if ((setter != null) && setter.IsStatic)
4269 if (setter == null && getter == null){
4270 Error (122, "`" + PropertyInfo.Name + "' " +
4271 "is inaccessible because of its protection level");
4276 bool InstanceResolve (EmitContext ec)
4278 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4279 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4283 if (instance_expr != null) {
4284 instance_expr = instance_expr.DoResolve (ec);
4285 if (instance_expr == null)
4289 if (must_do_cs1540_check && (instance_expr != null)) {
4290 if ((instance_expr.Type != ec.ContainerType) &&
4291 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
4292 Report.Error (1540, loc, "Cannot access protected member `" +
4293 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
4294 "' via a qualifier of type `" +
4295 TypeManager.CSharpName (instance_expr.Type) +
4296 "'; the qualifier must be of type `" +
4297 TypeManager.CSharpName (ec.ContainerType) +
4298 "' (or derived from it)");
4306 override public Expression DoResolve (EmitContext ec)
4308 if (getter == null){
4310 // The following condition happens if the PropertyExpr was
4311 // created, but is invalid (ie, the property is inaccessible),
4312 // and we did not want to embed the knowledge about this in
4313 // the caller routine. This only avoids double error reporting.
4318 Report.Error (154, loc,
4319 "The property `" + PropertyInfo.Name +
4320 "' can not be used in " +
4321 "this context because it lacks a get accessor");
4325 if (!InstanceResolve (ec))
4329 // Only base will allow this invocation to happen.
4331 if (IsBase && getter.IsAbstract){
4332 Report.Error (205, loc, "Cannot call an abstract base property: " +
4333 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4340 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4342 if (setter == null){
4344 // The following condition happens if the PropertyExpr was
4345 // created, but is invalid (ie, the property is inaccessible),
4346 // and we did not want to embed the knowledge about this in
4347 // the caller routine. This only avoids double error reporting.
4352 Report.Error (154, loc,
4353 "The property `" + PropertyInfo.Name +
4354 "' can not be used in " +
4355 "this context because it lacks a set accessor");
4359 if (!InstanceResolve (ec))
4363 // Only base will allow this invocation to happen.
4365 if (IsBase && setter.IsAbstract){
4366 Report.Error (205, loc, "Cannot call an abstract base property: " +
4367 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4373 override public void Emit (EmitContext ec)
4376 // Special case: length of single dimension array property is turned into ldlen
4378 if ((getter == TypeManager.system_int_array_get_length) ||
4379 (getter == TypeManager.int_array_get_length)){
4380 Type iet = instance_expr.Type;
4383 // System.Array.Length can be called, but the Type does not
4384 // support invoking GetArrayRank, so test for that case first
4386 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4387 instance_expr.Emit (ec);
4388 ec.ig.Emit (OpCodes.Ldlen);
4393 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
4398 // Implements the IAssignMethod interface for assignments
4400 public void EmitAssign (EmitContext ec, Expression source)
4402 Argument arg = new Argument (source, Argument.AType.Expression);
4403 ArrayList args = new ArrayList ();
4406 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
4409 override public void EmitStatement (EmitContext ec)
4412 ec.ig.Emit (OpCodes.Pop);
4417 /// Fully resolved expression that evaluates to an Event
4419 public class EventExpr : Expression, IMemberExpr {
4420 public readonly EventInfo EventInfo;
4421 public Expression instance_expr;
4424 MethodInfo add_accessor, remove_accessor;
4426 public EventExpr (EventInfo ei, Location loc)
4430 eclass = ExprClass.EventAccess;
4432 add_accessor = TypeManager.GetAddMethod (ei);
4433 remove_accessor = TypeManager.GetRemoveMethod (ei);
4435 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4438 if (EventInfo is MyEventBuilder){
4439 MyEventBuilder eb = (MyEventBuilder) EventInfo;
4440 type = eb.EventType;
4443 type = EventInfo.EventHandlerType;
4446 public string Name {
4448 return EventInfo.Name;
4452 public bool IsInstance {
4458 public bool IsStatic {
4464 public Type DeclaringType {
4466 return EventInfo.DeclaringType;
4470 public Expression InstanceExpression {
4472 return instance_expr;
4476 instance_expr = value;
4480 public override Expression DoResolve (EmitContext ec)
4482 if (instance_expr != null) {
4483 instance_expr = instance_expr.DoResolve (ec);
4484 if (instance_expr == null)
4492 public override void Emit (EmitContext ec)
4494 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4497 public void EmitAddOrRemove (EmitContext ec, Expression source)
4499 Expression handler = ((Binary) source).Right;
4501 Argument arg = new Argument (handler, Argument.AType.Expression);
4502 ArrayList args = new ArrayList ();
4506 if (((Binary) source).Oper == Binary.Operator.Addition)
4507 Invocation.EmitCall (
4508 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4510 Invocation.EmitCall (
4511 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);