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 /// Base class for expressions
177 public abstract class Expression {
178 public ExprClass eclass;
180 protected Location loc;
192 public Location Location {
199 /// Utility wrapper routine for Error, just to beautify the code
201 public void Error (int error, string s)
203 if (!Location.IsNull (loc))
204 Report.Error (error, loc, s);
206 Report.Error (error, s);
210 /// Utility wrapper routine for Warning, just to beautify the code
212 public void Warning (int warning, string s)
214 if (!Location.IsNull (loc))
215 Report.Warning (warning, loc, s);
217 Report.Warning (warning, s);
221 /// Utility wrapper routine for Warning, only prints the warning if
222 /// warnings of level `level' are enabled.
224 public void Warning (int warning, int level, string s)
226 if (level <= RootContext.WarningLevel)
227 Warning (warning, s);
230 static public void Error_CannotConvertType (Location loc, Type source, Type target)
232 Report.Error (30, loc, "Cannot convert type '" +
233 TypeManager.CSharpName (source) + "' to '" +
234 TypeManager.CSharpName (target) + "'");
238 /// Performs semantic analysis on the Expression
242 /// The Resolve method is invoked to perform the semantic analysis
245 /// The return value is an expression (it can be the
246 /// same expression in some cases) or a new
247 /// expression that better represents this node.
249 /// For example, optimizations of Unary (LiteralInt)
250 /// would return a new LiteralInt with a negated
253 /// If there is an error during semantic analysis,
254 /// then an error should be reported (using Report)
255 /// and a null value should be returned.
257 /// There are two side effects expected from calling
258 /// Resolve(): the the field variable "eclass" should
259 /// be set to any value of the enumeration
260 /// `ExprClass' and the type variable should be set
261 /// to a valid type (this is the type of the
264 public abstract Expression DoResolve (EmitContext ec);
266 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
268 return DoResolve (ec);
272 // This is used if the expression should be resolved as a type.
273 // the default implementation fails. Use this method in
274 // those participants in the SimpleName chain system.
276 public virtual Expression ResolveAsTypeStep (EmitContext ec)
282 // This is used to resolve the expression as a type, a null
283 // value will be returned if the expression is not a type
286 public Expression ResolveAsTypeTerminal (EmitContext ec)
288 Expression e = ResolveAsTypeStep (ec);
298 /// Resolves an expression and performs semantic analysis on it.
302 /// Currently Resolve wraps DoResolve to perform sanity
303 /// checking and assertion checking on what we expect from Resolve.
305 public Expression Resolve (EmitContext ec, ResolveFlags flags)
307 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
308 return ResolveAsTypeStep (ec);
310 bool old_do_flow_analysis = ec.DoFlowAnalysis;
311 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
312 ec.DoFlowAnalysis = false;
315 if (this is SimpleName)
316 e = ((SimpleName) this).DoResolveAllowStatic (ec);
320 ec.DoFlowAnalysis = old_do_flow_analysis;
325 if (e is SimpleName){
326 SimpleName s = (SimpleName) e;
328 if ((flags & ResolveFlags.SimpleName) == 0) {
329 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
330 ec.DeclSpace.Name, loc);
337 if ((e is TypeExpr) || (e is ComposedCast)) {
338 if ((flags & ResolveFlags.Type) == 0) {
339 e.Error_UnexpectedKind (flags);
348 if ((flags & ResolveFlags.VariableOrValue) == 0) {
349 e.Error_UnexpectedKind (flags);
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) {
367 Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
368 Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
369 FieldInfo fi = ((FieldExpr) e).FieldInfo;
371 Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
372 e.Error_UnexpectedKind (flags);
378 throw new Exception ("Expression " + e.GetType () +
379 " ExprClass is Invalid after resolve");
383 throw new Exception (
384 "Expression " + e.GetType () +
385 " did not set its type after Resolve\n" +
386 "called from: " + this.GetType ());
392 /// Resolves an expression and performs semantic analysis on it.
394 public Expression Resolve (EmitContext ec)
396 return Resolve (ec, ResolveFlags.VariableOrValue);
400 /// Resolves an expression for LValue assignment
404 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
405 /// checking and assertion checking on what we expect from Resolve
407 public Expression ResolveLValue (EmitContext ec, Expression right_side)
409 Expression e = DoResolveLValue (ec, right_side);
412 if (e is SimpleName){
413 SimpleName s = (SimpleName) e;
414 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
415 ec.DeclSpace.Name, loc);
419 if (e.eclass == ExprClass.Invalid)
420 throw new Exception ("Expression " + e +
421 " ExprClass is Invalid after resolve");
423 if (e.eclass == ExprClass.MethodGroup) {
424 ((MethodGroupExpr) e).ReportUsageError ();
429 throw new Exception ("Expression " + e +
430 " did not set its type after Resolve");
437 /// Emits the code for the expression
441 /// The Emit method is invoked to generate the code
442 /// for the expression.
444 public abstract void Emit (EmitContext ec);
447 /// Protected constructor. Only derivate types should
448 /// be able to be created
451 protected Expression ()
453 eclass = ExprClass.Invalid;
458 /// Returns a literalized version of a literal FieldInfo
462 /// The possible return values are:
463 /// IntConstant, UIntConstant
464 /// LongLiteral, ULongConstant
465 /// FloatConstant, DoubleConstant
468 /// The value returned is already resolved.
470 public static Constant Constantify (object v, Type t)
472 if (t == TypeManager.int32_type)
473 return new IntConstant ((int) v);
474 else if (t == TypeManager.uint32_type)
475 return new UIntConstant ((uint) v);
476 else if (t == TypeManager.int64_type)
477 return new LongConstant ((long) v);
478 else if (t == TypeManager.uint64_type)
479 return new ULongConstant ((ulong) v);
480 else if (t == TypeManager.float_type)
481 return new FloatConstant ((float) v);
482 else if (t == TypeManager.double_type)
483 return new DoubleConstant ((double) v);
484 else if (t == TypeManager.string_type)
485 return new StringConstant ((string) v);
486 else if (t == TypeManager.short_type)
487 return new ShortConstant ((short)v);
488 else if (t == TypeManager.ushort_type)
489 return new UShortConstant ((ushort)v);
490 else if (t == TypeManager.sbyte_type)
491 return new SByteConstant (((sbyte)v));
492 else if (t == TypeManager.byte_type)
493 return new ByteConstant ((byte)v);
494 else if (t == TypeManager.char_type)
495 return new CharConstant ((char)v);
496 else if (t == TypeManager.bool_type)
497 return new BoolConstant ((bool) v);
498 else if (TypeManager.IsEnumType (t)){
499 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
501 return new EnumConstant (e, t);
503 throw new Exception ("Unknown type for constant (" + t +
508 /// Returns a fully formed expression after a MemberLookup
510 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
513 return new EventExpr ((EventInfo) mi, loc);
514 else if (mi is FieldInfo)
515 return new FieldExpr ((FieldInfo) mi, loc);
516 else if (mi is PropertyInfo)
517 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
518 else if (mi is Type){
519 return new TypeExpr ((System.Type) mi, loc);
526 // FIXME: Probably implement a cache for (t,name,current_access_set)?
528 // This code could use some optimizations, but we need to do some
529 // measurements. For example, we could use a delegate to `flag' when
530 // something can not any longer be a method-group (because it is something
534 // If the return value is an Array, then it is an array of
537 // If the return value is an MemberInfo, it is anything, but a Method
541 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
542 // the arguments here and have MemberLookup return only the methods that
543 // match the argument count/type, unlike we are doing now (we delay this
546 // This is so we can catch correctly attempts to invoke instance methods
547 // from a static body (scan for error 120 in ResolveSimpleName).
550 // FIXME: Potential optimization, have a static ArrayList
553 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
554 MemberTypes mt, BindingFlags bf, Location loc)
556 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
560 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
561 // `qualifier_type' or null to lookup members in the current class.
564 public static Expression MemberLookup (EmitContext ec, Type container_type,
565 Type qualifier_type, Type queried_type,
566 string name, MemberTypes mt,
567 BindingFlags bf, Location loc)
569 MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
570 queried_type, mt, bf, name);
575 int count = mi.Length;
577 if (mi [0] is MethodBase)
578 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 queried_type,
600 string name, Location loc)
602 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
603 AllMemberTypes, AllBindingFlags, loc);
606 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
607 Type queried_type, string name, Location loc)
609 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
610 name, AllMemberTypes, AllBindingFlags, loc);
613 public static Expression MethodLookup (EmitContext ec, Type queried_type,
614 string name, Location loc)
616 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
617 MemberTypes.Method, AllBindingFlags, loc);
621 /// This is a wrapper for MemberLookup that is not used to "probe", but
622 /// to find a final definition. If the final definition is not found, we
623 /// look for private members and display a useful debugging message if we
626 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
627 Type queried_type, string name, Location loc)
629 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
630 AllMemberTypes, AllBindingFlags, loc);
633 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
634 Type queried_type, string name,
635 MemberTypes mt, BindingFlags bf,
640 int errors = Report.Errors;
642 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
648 // Error has already been reported.
649 if (errors < Report.Errors)
652 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
656 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
657 Type queried_type, string name,
658 string class_name, Location loc)
660 object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
661 AllMemberTypes, AllBindingFlags |
662 BindingFlags.NonPublic, name);
664 if (lookup == null) {
665 if (class_name != null)
666 Report.Error (103, loc, "The name `" + name + "' could not be " +
667 "found in `" + class_name + "'");
670 117, loc, "`" + queried_type + "' does not contain a " +
671 "definition for `" + name + "'");
675 if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
676 ec.ContainerType.IsSubclassOf (qualifier_type)) {
677 // Although a derived class can access protected members of
678 // its base class it cannot do so through an instance of the
679 // base class (CS1540). If the qualifier_type is a parent of the
680 // ec.ContainerType and the lookup succeeds with the latter one,
681 // then we are in this situation.
683 lookup = TypeManager.MemberLookup (
684 ec.ContainerType, ec.ContainerType, ec.ContainerType,
685 AllMemberTypes, AllBindingFlags, name);
687 if (lookup != null) {
689 1540, loc, "Cannot access protected member `" +
690 TypeManager.CSharpName (qualifier_type) + "." +
691 name + "' " + "via a qualifier of type `" +
692 TypeManager.CSharpName (qualifier_type) + "'; the " +
693 "qualifier must be of type `" +
694 TypeManager.CSharpName (ec.ContainerType) + "' " +
695 "(or derived from it)");
700 if (qualifier_type != null)
702 122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
703 name + "' is inaccessible due to its protection level");
706 122, loc, "`" + name + "' is inaccessible due to its " +
710 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
712 EventInfo ei = event_expr.EventInfo;
714 return TypeManager.GetPrivateFieldOfEvent (ei);
717 static EmptyExpression MyEmptyExpr;
718 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
720 Type expr_type = expr.Type;
722 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
723 // if we are a method group, emit a warning
729 // notice that it is possible to write "ValueType v = 1", the ValueType here
730 // is an abstract class, and not really a value type, so we apply the same rules.
732 if (target_type == TypeManager.object_type) {
734 // A pointer type cannot be converted to object
736 if (expr_type.IsPointer)
739 if (expr_type.IsValueType)
740 return new BoxedCast (expr);
741 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
742 return new EmptyCast (expr, target_type);
743 } else if (target_type == TypeManager.value_type) {
744 if (expr_type.IsValueType)
745 return new BoxedCast (expr);
746 if (expr is NullLiteral)
747 return new BoxedCast (expr);
748 } else if (expr_type.IsSubclassOf (target_type)) {
750 // Special case: enumeration to System.Enum.
751 // System.Enum is not a value type, it is a class, so we need
752 // a boxing conversion
754 if (expr_type.IsEnum)
755 return new BoxedCast (expr);
757 return new EmptyCast (expr, target_type);
760 // This code is kind of mirrored inside StandardConversionExists
761 // with the small distinction that we only probe there
763 // Always ensure that the code here and there is in sync
765 // from the null type to any reference-type.
766 if (expr is NullLiteral && !target_type.IsValueType)
767 return new NullLiteralTyped (target_type);
769 // from any class-type S to any interface-type T.
770 if (target_type.IsInterface) {
771 if (TypeManager.ImplementsInterface (expr_type, target_type)){
772 if (expr_type.IsClass)
773 return new EmptyCast (expr, target_type);
774 else if (expr_type.IsValueType)
775 return new BoxedCast (expr);
779 // from any interface type S to interface-type T.
780 if (expr_type.IsInterface && target_type.IsInterface) {
781 if (TypeManager.ImplementsInterface (expr_type, target_type))
782 return new EmptyCast (expr, target_type);
787 // from an array-type S to an array-type of type T
788 if (expr_type.IsArray && target_type.IsArray) {
789 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
791 Type expr_element_type = expr_type.GetElementType ();
793 if (MyEmptyExpr == null)
794 MyEmptyExpr = new EmptyExpression ();
796 MyEmptyExpr.SetType (expr_element_type);
797 Type target_element_type = target_type.GetElementType ();
799 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
800 if (StandardConversionExists (MyEmptyExpr,
801 target_element_type))
802 return new EmptyCast (expr, target_type);
807 // from an array-type to System.Array
808 if (expr_type.IsArray && target_type == TypeManager.array_type)
809 return new EmptyCast (expr, target_type);
811 // from any delegate type to System.Delegate
812 if ((expr_type == TypeManager.delegate_type ||
813 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
814 target_type == TypeManager.delegate_type)
815 return new EmptyCast (expr, target_type);
817 // from any array-type or delegate type into System.ICloneable.
818 if (expr_type.IsArray ||
819 expr_type == TypeManager.delegate_type ||
820 expr_type.IsSubclassOf (TypeManager.delegate_type))
821 if (target_type == TypeManager.icloneable_type)
822 return new EmptyCast (expr, target_type);
832 /// Implicit Numeric Conversions.
834 /// expr is the expression to convert, returns a new expression of type
835 /// target_type or null if an implicit conversion is not possible.
837 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
838 Type target_type, Location loc)
840 Type expr_type = expr.Type;
843 // Attempt to do the implicit constant expression conversions
845 if (expr is Constant){
847 if (expr is IntConstant){
850 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
854 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
856 // Try the implicit constant expression conversion
857 // from long to ulong, instead of a nice routine,
860 long v = ((LongConstant) expr).Value;
862 return new ULongConstant ((ulong) v);
866 Type real_target_type = target_type;
868 if (expr_type == TypeManager.sbyte_type){
870 // From sbyte to short, int, long, float, double.
872 if (real_target_type == TypeManager.int32_type)
873 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
874 if (real_target_type == TypeManager.int64_type)
875 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
876 if (real_target_type == TypeManager.double_type)
877 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
878 if (real_target_type == TypeManager.float_type)
879 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
880 if (real_target_type == TypeManager.short_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
882 } else if (expr_type == TypeManager.byte_type){
884 // From byte to short, ushort, int, uint, long, ulong, float, double
886 if ((real_target_type == TypeManager.short_type) ||
887 (real_target_type == TypeManager.ushort_type) ||
888 (real_target_type == TypeManager.int32_type) ||
889 (real_target_type == TypeManager.uint32_type))
890 return new EmptyCast (expr, target_type);
892 if (real_target_type == TypeManager.uint64_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
894 if (real_target_type == TypeManager.int64_type)
895 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
896 if (real_target_type == TypeManager.float_type)
897 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
898 if (real_target_type == TypeManager.double_type)
899 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
900 } else if (expr_type == TypeManager.short_type){
902 // From short to int, long, float, double
904 if (real_target_type == TypeManager.int32_type)
905 return new EmptyCast (expr, target_type);
906 if (real_target_type == TypeManager.int64_type)
907 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
908 if (real_target_type == TypeManager.double_type)
909 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
910 if (real_target_type == TypeManager.float_type)
911 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
912 } else if (expr_type == TypeManager.ushort_type){
914 // From ushort to int, uint, long, ulong, float, double
916 if (real_target_type == TypeManager.uint32_type)
917 return new EmptyCast (expr, target_type);
919 if (real_target_type == TypeManager.uint64_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
921 if (real_target_type == TypeManager.int32_type)
922 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
923 if (real_target_type == TypeManager.int64_type)
924 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
925 if (real_target_type == TypeManager.double_type)
926 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
927 if (real_target_type == TypeManager.float_type)
928 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
929 } else if (expr_type == TypeManager.int32_type){
931 // From int to long, float, double
933 if (real_target_type == TypeManager.int64_type)
934 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
935 if (real_target_type == TypeManager.double_type)
936 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
937 if (real_target_type == TypeManager.float_type)
938 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
939 } else if (expr_type == TypeManager.uint32_type){
941 // From uint to long, ulong, float, double
943 if (real_target_type == TypeManager.int64_type)
944 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
945 if (real_target_type == TypeManager.uint64_type)
946 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
947 if (real_target_type == TypeManager.double_type)
948 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
950 if (real_target_type == TypeManager.float_type)
951 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
953 } else if (expr_type == TypeManager.int64_type){
955 // From long/ulong to float, double
957 if (real_target_type == TypeManager.double_type)
958 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
959 if (real_target_type == TypeManager.float_type)
960 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
961 } else if (expr_type == TypeManager.uint64_type){
963 // From ulong to float, double
965 if (real_target_type == TypeManager.double_type)
966 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
968 if (real_target_type == TypeManager.float_type)
969 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
971 } else if (expr_type == TypeManager.char_type){
973 // From char to ushort, int, uint, long, ulong, float, double
975 if ((real_target_type == TypeManager.ushort_type) ||
976 (real_target_type == TypeManager.int32_type) ||
977 (real_target_type == TypeManager.uint32_type))
978 return new EmptyCast (expr, target_type);
979 if (real_target_type == TypeManager.uint64_type)
980 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
981 if (real_target_type == TypeManager.int64_type)
982 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
983 if (real_target_type == TypeManager.float_type)
984 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
985 if (real_target_type == TypeManager.double_type)
986 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
987 } else if (expr_type == TypeManager.float_type){
991 if (real_target_type == TypeManager.double_type)
992 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
999 // Tests whether an implicit reference conversion exists between expr_type
1002 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
1004 Type expr_type = expr.Type;
1007 // This is the boxed case.
1009 if (target_type == TypeManager.object_type) {
1010 if (expr_type.IsClass || expr_type.IsValueType ||
1011 expr_type.IsInterface || expr_type == TypeManager.enum_type)
1013 } else if (expr_type.IsSubclassOf (target_type)) {
1016 // Please remember that all code below actually comes
1017 // from ImplicitReferenceConversion so make sure code remains in sync
1019 // from any class-type S to any interface-type T.
1020 if (target_type.IsInterface) {
1021 if (TypeManager.ImplementsInterface (expr_type, target_type))
1025 // from any interface type S to interface-type T.
1026 if (expr_type.IsInterface && target_type.IsInterface)
1027 if (TypeManager.ImplementsInterface (expr_type, target_type))
1030 // from an array-type S to an array-type of type T
1031 if (expr_type.IsArray && target_type.IsArray) {
1032 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1034 Type expr_element_type = expr_type.GetElementType ();
1036 if (MyEmptyExpr == null)
1037 MyEmptyExpr = new EmptyExpression ();
1039 MyEmptyExpr.SetType (expr_element_type);
1040 Type target_element_type = target_type.GetElementType ();
1042 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1043 if (StandardConversionExists (MyEmptyExpr,
1044 target_element_type))
1049 // from an array-type to System.Array
1050 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1053 // from any delegate type to System.Delegate
1054 if ((expr_type == TypeManager.delegate_type ||
1055 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
1056 target_type == TypeManager.delegate_type)
1057 if (target_type.IsAssignableFrom (expr_type))
1060 // from any array-type or delegate type into System.ICloneable.
1061 if (expr_type.IsArray ||
1062 expr_type == TypeManager.delegate_type ||
1063 expr_type.IsSubclassOf (TypeManager.delegate_type))
1064 if (target_type == TypeManager.icloneable_type)
1067 // from the null type to any reference-type.
1068 if (expr is NullLiteral && !target_type.IsValueType &&
1069 !TypeManager.IsEnumType (target_type))
1078 /// Same as StandardConversionExists except that it also looks at
1079 /// implicit user defined conversions - needed for overload resolution
1081 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1083 if (StandardConversionExists (expr, target_type) == true)
1086 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1094 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
1096 Expression dummy = ImplicitUserConversion (
1097 ec, new EmptyExpression (source), target, Location.Null);
1098 return dummy != null;
1102 /// Determines if a standard implicit conversion exists from
1103 /// expr_type to target_type
1105 public static bool StandardConversionExists (Expression expr, Type target_type)
1107 Type expr_type = expr.Type;
1109 if (expr_type == TypeManager.void_type)
1112 if (expr_type == target_type)
1115 // First numeric conversions
1117 if (expr_type == TypeManager.sbyte_type){
1119 // From sbyte to short, int, long, float, double.
1121 if ((target_type == TypeManager.int32_type) ||
1122 (target_type == TypeManager.int64_type) ||
1123 (target_type == TypeManager.double_type) ||
1124 (target_type == TypeManager.float_type) ||
1125 (target_type == TypeManager.short_type) ||
1126 (target_type == TypeManager.decimal_type))
1129 } else if (expr_type == TypeManager.byte_type){
1131 // From byte to short, ushort, int, uint, long, ulong, float, double
1133 if ((target_type == TypeManager.short_type) ||
1134 (target_type == TypeManager.ushort_type) ||
1135 (target_type == TypeManager.int32_type) ||
1136 (target_type == TypeManager.uint32_type) ||
1137 (target_type == TypeManager.uint64_type) ||
1138 (target_type == TypeManager.int64_type) ||
1139 (target_type == TypeManager.float_type) ||
1140 (target_type == TypeManager.double_type) ||
1141 (target_type == TypeManager.decimal_type))
1144 } else if (expr_type == TypeManager.short_type){
1146 // From short to int, long, float, double
1148 if ((target_type == TypeManager.int32_type) ||
1149 (target_type == TypeManager.int64_type) ||
1150 (target_type == TypeManager.double_type) ||
1151 (target_type == TypeManager.float_type) ||
1152 (target_type == TypeManager.decimal_type))
1155 } else if (expr_type == TypeManager.ushort_type){
1157 // From ushort to int, uint, long, ulong, float, double
1159 if ((target_type == TypeManager.uint32_type) ||
1160 (target_type == TypeManager.uint64_type) ||
1161 (target_type == TypeManager.int32_type) ||
1162 (target_type == TypeManager.int64_type) ||
1163 (target_type == TypeManager.double_type) ||
1164 (target_type == TypeManager.float_type) ||
1165 (target_type == TypeManager.decimal_type))
1168 } else if (expr_type == TypeManager.int32_type){
1170 // From int to long, float, double
1172 if ((target_type == TypeManager.int64_type) ||
1173 (target_type == TypeManager.double_type) ||
1174 (target_type == TypeManager.float_type) ||
1175 (target_type == TypeManager.decimal_type))
1178 } else if (expr_type == TypeManager.uint32_type){
1180 // From uint to long, ulong, float, double
1182 if ((target_type == TypeManager.int64_type) ||
1183 (target_type == TypeManager.uint64_type) ||
1184 (target_type == TypeManager.double_type) ||
1185 (target_type == TypeManager.float_type) ||
1186 (target_type == TypeManager.decimal_type))
1189 } else if ((expr_type == TypeManager.uint64_type) ||
1190 (expr_type == TypeManager.int64_type)) {
1192 // From long/ulong to float, double
1194 if ((target_type == TypeManager.double_type) ||
1195 (target_type == TypeManager.float_type) ||
1196 (target_type == TypeManager.decimal_type))
1199 } else if (expr_type == TypeManager.char_type){
1201 // From char to ushort, int, uint, long, ulong, float, double
1203 if ((target_type == TypeManager.ushort_type) ||
1204 (target_type == TypeManager.int32_type) ||
1205 (target_type == TypeManager.uint32_type) ||
1206 (target_type == TypeManager.uint64_type) ||
1207 (target_type == TypeManager.int64_type) ||
1208 (target_type == TypeManager.float_type) ||
1209 (target_type == TypeManager.double_type) ||
1210 (target_type == TypeManager.decimal_type))
1213 } else if (expr_type == TypeManager.float_type){
1217 if (target_type == TypeManager.double_type)
1221 if (ImplicitReferenceConversionExists (expr, target_type))
1224 if (expr is IntConstant){
1225 int value = ((IntConstant) expr).Value;
1227 if (target_type == TypeManager.sbyte_type){
1228 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1230 } else if (target_type == TypeManager.byte_type){
1231 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1233 } else if (target_type == TypeManager.short_type){
1234 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1236 } else if (target_type == TypeManager.ushort_type){
1237 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1239 } else if (target_type == TypeManager.uint32_type){
1242 } else if (target_type == TypeManager.uint64_type){
1244 // we can optimize this case: a positive int32
1245 // always fits on a uint64. But we need an opcode
1252 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1256 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1258 // Try the implicit constant expression conversion
1259 // from long to ulong, instead of a nice routine,
1260 // we just inline it
1262 long v = ((LongConstant) expr).Value;
1267 if ((target_type == TypeManager.enum_type ||
1268 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1269 expr is IntLiteral){
1270 IntLiteral i = (IntLiteral) expr;
1276 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1283 // Used internally by FindMostEncompassedType, this is used
1284 // to avoid creating lots of objects in the tight loop inside
1285 // FindMostEncompassedType
1287 static EmptyExpression priv_fmet_param;
1290 /// Finds "most encompassed type" according to the spec (13.4.2)
1291 /// amongst the methods in the MethodGroupExpr
1293 static Type FindMostEncompassedType (ArrayList types)
1297 if (priv_fmet_param == null)
1298 priv_fmet_param = new EmptyExpression ();
1300 foreach (Type t in types){
1301 priv_fmet_param.SetType (t);
1308 if (StandardConversionExists (priv_fmet_param, best))
1316 // Used internally by FindMostEncompassingType, this is used
1317 // to avoid creating lots of objects in the tight loop inside
1318 // FindMostEncompassingType
1320 static EmptyExpression priv_fmee_ret;
1323 /// Finds "most encompassing type" according to the spec (13.4.2)
1324 /// amongst the types in the given set
1326 static Type FindMostEncompassingType (ArrayList types)
1330 if (priv_fmee_ret == null)
1331 priv_fmee_ret = new EmptyExpression ();
1333 foreach (Type t in types){
1334 priv_fmee_ret.SetType (best);
1341 if (StandardConversionExists (priv_fmee_ret, t))
1349 // Used to avoid creating too many objects
1351 static EmptyExpression priv_fms_expr;
1354 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1355 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1356 /// for explicit and implicit conversion operators.
1358 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1359 bool apply_explicit_conv_rules,
1362 ArrayList src_types_set = new ArrayList ();
1364 if (priv_fms_expr == null)
1365 priv_fms_expr = new EmptyExpression ();
1368 // If any operator converts from S then Sx = S
1370 Type source_type = source.Type;
1371 foreach (MethodBase mb in me.Methods){
1372 ParameterData pd = Invocation.GetParameterData (mb);
1373 Type param_type = pd.ParameterType (0);
1375 if (param_type == source_type)
1378 if (apply_explicit_conv_rules) {
1381 // Find the set of applicable user-defined conversion operators, U. This set
1383 // user-defined implicit or explicit conversion operators declared by
1384 // the classes or structs in D that convert from a type encompassing
1385 // or encompassed by S to a type encompassing or encompassed by T
1387 priv_fms_expr.SetType (param_type);
1388 if (StandardConversionExists (priv_fms_expr, source_type))
1389 src_types_set.Add (param_type);
1391 if (StandardConversionExists (source, param_type))
1392 src_types_set.Add (param_type);
1396 // Only if S is encompassed by param_type
1398 if (StandardConversionExists (source, param_type))
1399 src_types_set.Add (param_type);
1404 // Explicit Conv rules
1406 if (apply_explicit_conv_rules) {
1407 ArrayList candidate_set = new ArrayList ();
1409 foreach (Type param_type in src_types_set){
1410 if (StandardConversionExists (source, param_type))
1411 candidate_set.Add (param_type);
1414 if (candidate_set.Count != 0)
1415 return FindMostEncompassedType (candidate_set);
1421 if (apply_explicit_conv_rules)
1422 return FindMostEncompassingType (src_types_set);
1424 return FindMostEncompassedType (src_types_set);
1428 // Useful in avoiding proliferation of objects
1430 static EmptyExpression priv_fmt_expr;
1433 /// Finds the most specific target Tx according to section 13.4.4
1435 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1436 bool apply_explicit_conv_rules,
1439 ArrayList tgt_types_set = new ArrayList ();
1441 if (priv_fmt_expr == null)
1442 priv_fmt_expr = new EmptyExpression ();
1445 // If any operator converts to T then Tx = T
1447 foreach (MethodInfo mi in me.Methods){
1448 Type ret_type = mi.ReturnType;
1450 if (ret_type == target)
1453 if (apply_explicit_conv_rules) {
1456 // Find the set of applicable user-defined conversion operators, U.
1458 // This set consists of the
1459 // user-defined implicit or explicit conversion operators declared by
1460 // the classes or structs in D that convert from a type encompassing
1461 // or encompassed by S to a type encompassing or encompassed by T
1463 priv_fms_expr.SetType (ret_type);
1464 if (StandardConversionExists (priv_fms_expr, target))
1465 tgt_types_set.Add (ret_type);
1467 priv_fms_expr.SetType (target);
1468 if (StandardConversionExists (priv_fms_expr, ret_type))
1469 tgt_types_set.Add (ret_type);
1473 // Only if T is encompassed by param_type
1475 priv_fms_expr.SetType (ret_type);
1476 if (StandardConversionExists (priv_fms_expr, target))
1477 tgt_types_set.Add (ret_type);
1482 // Explicit conv rules
1484 if (apply_explicit_conv_rules) {
1485 ArrayList candidate_set = new ArrayList ();
1487 foreach (Type ret_type in tgt_types_set){
1488 priv_fmt_expr.SetType (ret_type);
1490 if (StandardConversionExists (priv_fmt_expr, target))
1491 candidate_set.Add (ret_type);
1494 if (candidate_set.Count != 0)
1495 return FindMostEncompassingType (candidate_set);
1499 // Okay, final case !
1501 if (apply_explicit_conv_rules)
1502 return FindMostEncompassedType (tgt_types_set);
1504 return FindMostEncompassingType (tgt_types_set);
1508 /// User-defined Implicit conversions
1510 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1511 Type target, Location loc)
1513 return UserDefinedConversion (ec, source, target, loc, false);
1517 /// User-defined Explicit conversions
1519 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1520 Type target, Location loc)
1522 return UserDefinedConversion (ec, source, target, loc, true);
1526 /// Returns an expression that can be used to invoke operator true
1527 /// on the expression if it exists.
1529 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
1532 Expression operator_group;
1534 operator_group = MethodLookup (ec, e.Type, "op_True", loc);
1535 if (operator_group == null)
1538 ArrayList arguments = new ArrayList ();
1539 arguments.Add (new Argument (e, Argument.AType.Expression));
1540 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc);
1545 return new StaticCallExpr ((MethodInfo) method, arguments, loc);
1549 /// Resolves the expression `e' into a boolean expression: either through
1550 /// an implicit conversion, or through an `operator true' invocation
1552 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
1558 Expression converted = e;
1559 if (e.Type != TypeManager.bool_type)
1560 converted = Expression.ConvertImplicit (ec, e, TypeManager.bool_type, new Location (-1));
1563 // If no implicit conversion to bool exists, try using `operator true'
1565 if (converted == null){
1566 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
1567 if (operator_true == null){
1569 31, loc, "Can not convert the expression to a boolean");
1580 /// Computes the MethodGroup for the user-defined conversion
1581 /// operators from source_type to target_type. `look_for_explicit'
1582 /// controls whether we should also include the list of explicit
1585 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1586 Type source_type, Type target_type,
1587 Location loc, bool look_for_explicit)
1589 Expression mg1 = null, mg2 = null;
1590 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1593 op_name = "op_Implicit";
1595 MethodGroupExpr union3;
1597 mg1 = MethodLookup (ec, source_type, op_name, loc);
1598 if (source_type.BaseType != null)
1599 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1602 union3 = (MethodGroupExpr) mg2;
1603 else if (mg2 == null)
1604 union3 = (MethodGroupExpr) mg1;
1606 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1608 mg1 = MethodLookup (ec, target_type, op_name, loc);
1611 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1613 union3 = (MethodGroupExpr) mg1;
1616 if (target_type.BaseType != null)
1617 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1621 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1623 union3 = (MethodGroupExpr) mg1;
1626 MethodGroupExpr union4 = null;
1628 if (look_for_explicit) {
1629 op_name = "op_Explicit";
1631 mg5 = MemberLookup (ec, source_type, op_name, loc);
1632 if (source_type.BaseType != null)
1633 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1635 mg7 = MemberLookup (ec, target_type, op_name, loc);
1636 if (target_type.BaseType != null)
1637 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1639 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1640 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1642 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1645 return Invocation.MakeUnionSet (union3, union4, loc);
1649 /// User-defined conversions
1651 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1652 Type target, Location loc,
1653 bool look_for_explicit)
1655 MethodGroupExpr union;
1656 Type source_type = source.Type;
1657 MethodBase method = null;
1659 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1663 Type most_specific_source, most_specific_target;
1665 foreach (MethodBase m in union.Methods){
1666 Console.WriteLine ("Name: " + m.Name);
1667 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1671 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1672 if (most_specific_source == null)
1675 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1676 if (most_specific_target == null)
1682 foreach (MethodBase mb in union.Methods){
1683 ParameterData pd = Invocation.GetParameterData (mb);
1684 MethodInfo mi = (MethodInfo) mb;
1686 if (pd.ParameterType (0) == most_specific_source &&
1687 mi.ReturnType == most_specific_target) {
1693 if (method == null || count > 1)
1698 // This will do the conversion to the best match that we
1699 // found. Now we need to perform an implict standard conversion
1700 // if the best match was not the type that we were requested
1703 if (look_for_explicit)
1704 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1706 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1712 e = new UserCast ((MethodInfo) method, source, loc);
1713 if (e.Type != target){
1714 if (!look_for_explicit)
1715 e = ConvertImplicitStandard (ec, e, target, loc);
1717 e = ConvertExplicitStandard (ec, e, target, loc);
1724 /// Converts implicitly the resolved expression `expr' into the
1725 /// `target_type'. It returns a new expression that can be used
1726 /// in a context that expects a `target_type'.
1728 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1729 Type target_type, Location loc)
1731 Type expr_type = expr.Type;
1734 if (expr_type == target_type)
1737 if (target_type == null)
1738 throw new Exception ("Target type is null");
1740 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1744 e = ImplicitUserConversion (ec, expr, target_type, loc);
1753 /// Attempts to apply the `Standard Implicit
1754 /// Conversion' rules to the expression `expr' into
1755 /// the `target_type'. It returns a new expression
1756 /// that can be used in a context that expects a
1759 /// This is different from `ConvertImplicit' in that the
1760 /// user defined implicit conversions are excluded.
1762 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1763 Type target_type, Location loc)
1765 Type expr_type = expr.Type;
1768 if (expr_type == target_type)
1771 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1775 e = ImplicitReferenceConversion (expr, target_type);
1779 if ((target_type == TypeManager.enum_type ||
1780 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1781 expr is IntLiteral){
1782 IntLiteral i = (IntLiteral) expr;
1785 return new EnumConstant ((Constant) expr, target_type);
1789 if (expr_type.IsPointer){
1790 if (target_type == TypeManager.void_ptr_type)
1791 return new EmptyCast (expr, target_type);
1794 // yep, comparing pointer types cant be done with
1795 // t1 == t2, we have to compare their element types.
1797 if (target_type.IsPointer){
1798 if (target_type.GetElementType() == expr_type.GetElementType())
1803 if (target_type.IsPointer) {
1804 if (expr is NullLiteral)
1805 return new EmptyCast (expr, target_type);
1807 if (expr_type == TypeManager.void_ptr_type)
1808 return new EmptyCast (expr, target_type);
1816 /// Attemps to perform an implict constant conversion of the IntConstant
1817 /// into a different data type using casts (See Implicit Constant
1818 /// Expression Conversions)
1820 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1822 int value = ic.Value;
1824 if (target_type == TypeManager.sbyte_type){
1825 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1826 return new SByteConstant ((sbyte) value);
1827 } else if (target_type == TypeManager.byte_type){
1828 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1829 return new ByteConstant ((byte) value);
1830 } else if (target_type == TypeManager.short_type){
1831 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1832 return new ShortConstant ((short) value);
1833 } else if (target_type == TypeManager.ushort_type){
1834 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1835 return new UShortConstant ((ushort) value);
1836 } else if (target_type == TypeManager.uint32_type){
1838 return new UIntConstant ((uint) value);
1839 } else if (target_type == TypeManager.uint64_type){
1841 // we can optimize this case: a positive int32
1842 // always fits on a uint64. But we need an opcode
1846 return new ULongConstant ((ulong) value);
1847 } else if (target_type == TypeManager.double_type)
1848 return new DoubleConstant ((double) value);
1849 else if (target_type == TypeManager.float_type)
1850 return new FloatConstant ((float) value);
1852 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1853 Type underlying = TypeManager.EnumToUnderlying (target_type);
1854 Constant e = (Constant) ic;
1857 // Possibly, we need to create a different 0 literal before passing
1860 if (underlying == TypeManager.int64_type)
1861 e = new LongLiteral (0);
1862 else if (underlying == TypeManager.uint64_type)
1863 e = new ULongLiteral (0);
1865 return new EnumConstant (e, target_type);
1870 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1872 string msg = "Cannot convert implicitly from `"+
1873 TypeManager.CSharpName (source) + "' to `" +
1874 TypeManager.CSharpName (target) + "'";
1876 Report.Error (29, loc, msg);
1880 /// Attemptes to implicityly convert `target' into `type', using
1881 /// ConvertImplicit. If there is no implicit conversion, then
1882 /// an error is signaled
1884 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1885 Type target_type, Location loc)
1889 e = ConvertImplicit (ec, source, target_type, loc);
1893 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1894 Report.Error (664, loc,
1895 "Double literal cannot be implicitly converted to " +
1896 "float type, use F suffix to create a float literal");
1899 Error_CannotConvertImplicit (loc, source.Type, target_type);
1905 /// Performs the explicit numeric conversions
1907 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1909 Type expr_type = expr.Type;
1912 // If we have an enumeration, extract the underlying type,
1913 // use this during the comparison, but wrap around the original
1916 Type real_target_type = target_type;
1918 if (TypeManager.IsEnumType (real_target_type))
1919 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1921 if (StandardConversionExists (expr, real_target_type)){
1922 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1924 if (real_target_type != target_type)
1925 return new EmptyCast (ce, target_type);
1929 if (expr_type == TypeManager.sbyte_type){
1931 // From sbyte to byte, ushort, uint, ulong, char
1933 if (real_target_type == TypeManager.byte_type)
1934 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1935 if (real_target_type == TypeManager.ushort_type)
1936 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1937 if (real_target_type == TypeManager.uint32_type)
1938 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1939 if (real_target_type == TypeManager.uint64_type)
1940 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1941 if (real_target_type == TypeManager.char_type)
1942 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1943 } else if (expr_type == TypeManager.byte_type){
1945 // From byte to sbyte and char
1947 if (real_target_type == TypeManager.sbyte_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1949 if (real_target_type == TypeManager.char_type)
1950 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1951 } else if (expr_type == TypeManager.short_type){
1953 // From short to sbyte, byte, ushort, uint, ulong, char
1955 if (real_target_type == TypeManager.sbyte_type)
1956 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1957 if (real_target_type == TypeManager.byte_type)
1958 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1959 if (real_target_type == TypeManager.ushort_type)
1960 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1961 if (real_target_type == TypeManager.uint32_type)
1962 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1963 if (real_target_type == TypeManager.uint64_type)
1964 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1965 if (real_target_type == TypeManager.char_type)
1966 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1967 } else if (expr_type == TypeManager.ushort_type){
1969 // From ushort to sbyte, byte, short, char
1971 if (real_target_type == TypeManager.sbyte_type)
1972 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1973 if (real_target_type == TypeManager.byte_type)
1974 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1975 if (real_target_type == TypeManager.short_type)
1976 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1977 if (real_target_type == TypeManager.char_type)
1978 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1979 } else if (expr_type == TypeManager.int32_type){
1981 // From int to sbyte, byte, short, ushort, uint, ulong, char
1983 if (real_target_type == TypeManager.sbyte_type)
1984 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1985 if (real_target_type == TypeManager.byte_type)
1986 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1987 if (real_target_type == TypeManager.short_type)
1988 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1989 if (real_target_type == TypeManager.ushort_type)
1990 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1991 if (real_target_type == TypeManager.uint32_type)
1992 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1993 if (real_target_type == TypeManager.uint64_type)
1994 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1995 if (real_target_type == TypeManager.char_type)
1996 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1997 } else if (expr_type == TypeManager.uint32_type){
1999 // From uint to sbyte, byte, short, ushort, int, char
2001 if (real_target_type == TypeManager.sbyte_type)
2002 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2003 if (real_target_type == TypeManager.byte_type)
2004 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2005 if (real_target_type == TypeManager.short_type)
2006 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2007 if (real_target_type == TypeManager.ushort_type)
2008 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2009 if (real_target_type == TypeManager.int32_type)
2010 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2011 if (real_target_type == TypeManager.char_type)
2012 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2013 } else if (expr_type == TypeManager.int64_type){
2015 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2017 if (real_target_type == TypeManager.sbyte_type)
2018 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2019 if (real_target_type == TypeManager.byte_type)
2020 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2021 if (real_target_type == TypeManager.short_type)
2022 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2023 if (real_target_type == TypeManager.ushort_type)
2024 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2025 if (real_target_type == TypeManager.int32_type)
2026 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2027 if (real_target_type == TypeManager.uint32_type)
2028 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2029 if (real_target_type == TypeManager.uint64_type)
2030 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2031 if (real_target_type == TypeManager.char_type)
2032 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2033 } else if (expr_type == TypeManager.uint64_type){
2035 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2037 if (real_target_type == TypeManager.sbyte_type)
2038 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2039 if (real_target_type == TypeManager.byte_type)
2040 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2041 if (real_target_type == TypeManager.short_type)
2042 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2043 if (real_target_type == TypeManager.ushort_type)
2044 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2045 if (real_target_type == TypeManager.int32_type)
2046 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2047 if (real_target_type == TypeManager.uint32_type)
2048 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2049 if (real_target_type == TypeManager.int64_type)
2050 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2051 if (real_target_type == TypeManager.char_type)
2052 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2053 } else if (expr_type == TypeManager.char_type){
2055 // From char to sbyte, byte, short
2057 if (real_target_type == TypeManager.sbyte_type)
2058 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2059 if (real_target_type == TypeManager.byte_type)
2060 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2061 if (real_target_type == TypeManager.short_type)
2062 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2063 } else if (expr_type == TypeManager.float_type){
2065 // From float to sbyte, byte, short,
2066 // ushort, int, uint, long, ulong, char
2069 if (real_target_type == TypeManager.sbyte_type)
2070 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2071 if (real_target_type == TypeManager.byte_type)
2072 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2073 if (real_target_type == TypeManager.short_type)
2074 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2075 if (real_target_type == TypeManager.ushort_type)
2076 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2077 if (real_target_type == TypeManager.int32_type)
2078 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2079 if (real_target_type == TypeManager.uint32_type)
2080 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2081 if (real_target_type == TypeManager.int64_type)
2082 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2083 if (real_target_type == TypeManager.uint64_type)
2084 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2085 if (real_target_type == TypeManager.char_type)
2086 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2087 } else if (expr_type == TypeManager.double_type){
2089 // From double to byte, byte, short,
2090 // ushort, int, uint, long, ulong,
2091 // char, float or decimal
2093 if (real_target_type == TypeManager.sbyte_type)
2094 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2095 if (real_target_type == TypeManager.byte_type)
2096 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2097 if (real_target_type == TypeManager.short_type)
2098 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2099 if (real_target_type == TypeManager.ushort_type)
2100 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2101 if (real_target_type == TypeManager.int32_type)
2102 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2103 if (real_target_type == TypeManager.uint32_type)
2104 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2105 if (real_target_type == TypeManager.int64_type)
2106 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2107 if (real_target_type == TypeManager.uint64_type)
2108 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2109 if (real_target_type == TypeManager.char_type)
2110 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2111 if (real_target_type == TypeManager.float_type)
2112 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2115 // decimal is taken care of by the op_Explicit methods.
2121 /// Returns whether an explicit reference conversion can be performed
2122 /// from source_type to target_type
2124 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2126 bool target_is_value_type = target_type.IsValueType;
2128 if (source_type == target_type)
2132 // From object to any reference type
2134 if (source_type == TypeManager.object_type && !target_is_value_type)
2138 // From any class S to any class-type T, provided S is a base class of T
2140 if (target_type.IsSubclassOf (source_type))
2144 // From any interface type S to any interface T provided S is not derived from T
2146 if (source_type.IsInterface && target_type.IsInterface){
2147 if (!target_type.IsSubclassOf (source_type))
2152 // From any class type S to any interface T, provided S is not sealed
2153 // and provided S does not implement T.
2155 if (target_type.IsInterface && !source_type.IsSealed &&
2156 !TypeManager.ImplementsInterface (source_type, target_type))
2160 // From any interface-type S to to any class type T, provided T is not
2161 // sealed, or provided T implements S.
2163 if (source_type.IsInterface &&
2164 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2168 // From an array type S with an element type Se to an array type T with an
2169 // element type Te provided all the following are true:
2170 // * S and T differe only in element type, in other words, S and T
2171 // have the same number of dimensions.
2172 // * Both Se and Te are reference types
2173 // * An explicit referenc conversions exist from Se to Te
2175 if (source_type.IsArray && target_type.IsArray) {
2176 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2178 Type source_element_type = source_type.GetElementType ();
2179 Type target_element_type = target_type.GetElementType ();
2181 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2182 if (ExplicitReferenceConversionExists (source_element_type,
2183 target_element_type))
2189 // From System.Array to any array-type
2190 if (source_type == TypeManager.array_type &&
2191 target_type.IsArray){
2196 // From System delegate to any delegate-type
2198 if (source_type == TypeManager.delegate_type &&
2199 target_type.IsSubclassOf (TypeManager.delegate_type))
2203 // From ICloneable to Array or Delegate types
2205 if (source_type == TypeManager.icloneable_type &&
2206 (target_type == TypeManager.array_type ||
2207 target_type == TypeManager.delegate_type))
2214 /// Implements Explicit Reference conversions
2216 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2218 Type source_type = source.Type;
2219 bool target_is_value_type = target_type.IsValueType;
2222 // From object to any reference type
2224 if (source_type == TypeManager.object_type && !target_is_value_type)
2225 return new ClassCast (source, target_type);
2229 // From any class S to any class-type T, provided S is a base class of T
2231 if (target_type.IsSubclassOf (source_type))
2232 return new ClassCast (source, target_type);
2235 // From any interface type S to any interface T provided S is not derived from T
2237 if (source_type.IsInterface && target_type.IsInterface){
2238 if (TypeManager.ImplementsInterface (source_type, target_type))
2241 return new ClassCast (source, target_type);
2245 // From any class type S to any interface T, provides S is not sealed
2246 // and provided S does not implement T.
2248 if (target_type.IsInterface && !source_type.IsSealed) {
2249 if (TypeManager.ImplementsInterface (source_type, target_type))
2252 return new ClassCast (source, target_type);
2257 // From any interface-type S to to any class type T, provided T is not
2258 // sealed, or provided T implements S.
2260 if (source_type.IsInterface) {
2261 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2262 return new ClassCast (source, target_type);
2267 // From an array type S with an element type Se to an array type T with an
2268 // element type Te provided all the following are true:
2269 // * S and T differe only in element type, in other words, S and T
2270 // have the same number of dimensions.
2271 // * Both Se and Te are reference types
2272 // * An explicit referenc conversions exist from Se to Te
2274 if (source_type.IsArray && target_type.IsArray) {
2275 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2277 Type source_element_type = source_type.GetElementType ();
2278 Type target_element_type = target_type.GetElementType ();
2280 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2281 if (ExplicitReferenceConversionExists (source_element_type,
2282 target_element_type))
2283 return new ClassCast (source, target_type);
2288 // From System.Array to any array-type
2289 if (source_type == TypeManager.array_type &&
2290 target_type.IsArray) {
2291 return new ClassCast (source, target_type);
2295 // From System delegate to any delegate-type
2297 if (source_type == TypeManager.delegate_type &&
2298 target_type.IsSubclassOf (TypeManager.delegate_type))
2299 return new ClassCast (source, target_type);
2302 // From ICloneable to Array or Delegate types
2304 if (source_type == TypeManager.icloneable_type &&
2305 (target_type == TypeManager.array_type ||
2306 target_type == TypeManager.delegate_type))
2307 return new ClassCast (source, target_type);
2313 /// Performs an explicit conversion of the expression `expr' whose
2314 /// type is expr.Type to `target_type'.
2316 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2317 Type target_type, Location loc)
2319 Type expr_type = expr.Type;
2320 Type original_expr_type = expr_type;
2322 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
2323 if (target_type == TypeManager.enum_type ||
2324 target_type == TypeManager.object_type) {
2325 if (expr is EnumConstant)
2326 expr = ((EnumConstant) expr).Child;
2327 // We really need all these casts here .... :-(
2328 expr = new BoxedCast (new EmptyCast (expr, expr_type));
2329 return new EmptyCast (expr, target_type);
2330 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
2331 target_type.IsSubclassOf (TypeManager.enum_type))
2332 return new UnboxCast (expr, target_type);
2335 // Notice that we have kept the expr_type unmodified, which is only
2337 if (expr is EnumConstant)
2338 expr = ((EnumConstant) expr).Child;
2340 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2341 expr_type = expr.Type;
2344 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2349 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2354 // Unboxing conversion.
2356 if (expr_type == TypeManager.object_type && target_type.IsValueType){
2357 if (expr is NullLiteral){
2358 Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
2361 return new UnboxCast (expr, target_type);
2365 ne = ConvertReferenceExplicit (expr, target_type);
2370 if (target_type.IsPointer){
2371 if (expr_type.IsPointer)
2372 return new EmptyCast (expr, target_type);
2374 if (expr_type == TypeManager.sbyte_type ||
2375 expr_type == TypeManager.byte_type ||
2376 expr_type == TypeManager.short_type ||
2377 expr_type == TypeManager.ushort_type ||
2378 expr_type == TypeManager.int32_type ||
2379 expr_type == TypeManager.uint32_type ||
2380 expr_type == TypeManager.uint64_type ||
2381 expr_type == TypeManager.int64_type)
2382 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2384 if (expr_type.IsPointer){
2385 if (target_type == TypeManager.sbyte_type ||
2386 target_type == TypeManager.byte_type ||
2387 target_type == TypeManager.short_type ||
2388 target_type == TypeManager.ushort_type ||
2389 target_type == TypeManager.int32_type ||
2390 target_type == TypeManager.uint32_type ||
2391 target_type == TypeManager.uint64_type ||
2392 target_type == TypeManager.int64_type){
2393 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2396 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2401 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2405 // We should always be able to go from an uint32
2406 // implicitly or explicitly to the other integral
2409 throw new Exception ("Internal compiler error");
2414 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2418 Error_CannotConvertType (loc, original_expr_type, target_type);
2423 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2425 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2426 Type target_type, Location l)
2428 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2433 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2437 ne = ConvertReferenceExplicit (expr, target_type);
2441 Error_CannotConvertType (l, expr.Type, target_type);
2445 static string ExprClassName (ExprClass c)
2448 case ExprClass.Invalid:
2450 case ExprClass.Value:
2452 case ExprClass.Variable:
2454 case ExprClass.Namespace:
2456 case ExprClass.Type:
2458 case ExprClass.MethodGroup:
2459 return "method group";
2460 case ExprClass.PropertyAccess:
2461 return "property access";
2462 case ExprClass.EventAccess:
2463 return "event access";
2464 case ExprClass.IndexerAccess:
2465 return "indexer access";
2466 case ExprClass.Nothing:
2469 throw new Exception ("Should not happen");
2473 /// Reports that we were expecting `expr' to be of class `expected'
2475 public void Error_UnexpectedKind (string expected)
2477 string kind = "Unknown";
2479 kind = ExprClassName (eclass);
2481 Error (118, "Expression denotes a `" + kind +
2482 "' where a `" + expected + "' was expected");
2485 public void Error_UnexpectedKind (ResolveFlags flags)
2487 ArrayList valid = new ArrayList (10);
2489 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2490 valid.Add ("variable");
2491 valid.Add ("value");
2494 if ((flags & ResolveFlags.Type) != 0)
2497 if ((flags & ResolveFlags.MethodGroup) != 0)
2498 valid.Add ("method group");
2500 if ((flags & ResolveFlags.SimpleName) != 0)
2501 valid.Add ("simple name");
2503 if (valid.Count == 0)
2504 valid.Add ("unknown");
2506 StringBuilder sb = new StringBuilder ();
2507 for (int i = 0; i < valid.Count; i++) {
2510 else if (i == valid.Count)
2512 sb.Append (valid [i]);
2515 string kind = ExprClassName (eclass);
2517 Error (119, "Expression denotes a `" + kind + "' where " +
2518 "a `" + sb.ToString () + "' was expected");
2521 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2523 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2524 TypeManager.CSharpName (t));
2527 public static void UnsafeError (Location loc)
2529 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2533 /// Converts the IntConstant, UIntConstant, LongConstant or
2534 /// ULongConstant into the integral target_type. Notice
2535 /// that we do not return an `Expression' we do return
2536 /// a boxed integral type.
2538 /// FIXME: Since I added the new constants, we need to
2539 /// also support conversions from CharConstant, ByteConstant,
2540 /// SByteConstant, UShortConstant, ShortConstant
2542 /// This is used by the switch statement, so the domain
2543 /// of work is restricted to the literals above, and the
2544 /// targets are int32, uint32, char, byte, sbyte, ushort,
2545 /// short, uint64 and int64
2547 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2551 if (c.Type == target_type)
2552 return ((Constant) c).GetValue ();
2555 // Make into one of the literals we handle, we dont really care
2556 // about this value as we will just return a few limited types
2558 if (c is EnumConstant)
2559 c = ((EnumConstant)c).WidenToCompilerConstant ();
2561 if (c is IntConstant){
2562 int v = ((IntConstant) c).Value;
2564 if (target_type == TypeManager.uint32_type){
2567 } else if (target_type == TypeManager.char_type){
2568 if (v >= Char.MinValue && v <= Char.MaxValue)
2570 } else if (target_type == TypeManager.byte_type){
2571 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2573 } else if (target_type == TypeManager.sbyte_type){
2574 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2576 } else if (target_type == TypeManager.short_type){
2577 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2579 } else if (target_type == TypeManager.ushort_type){
2580 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2582 } else if (target_type == TypeManager.int64_type)
2584 else if (target_type == TypeManager.uint64_type){
2590 } else if (c is UIntConstant){
2591 uint v = ((UIntConstant) c).Value;
2593 if (target_type == TypeManager.int32_type){
2594 if (v <= Int32.MaxValue)
2596 } else if (target_type == TypeManager.char_type){
2597 if (v >= Char.MinValue && v <= Char.MaxValue)
2599 } else if (target_type == TypeManager.byte_type){
2600 if (v <= Byte.MaxValue)
2602 } else if (target_type == TypeManager.sbyte_type){
2603 if (v <= SByte.MaxValue)
2605 } else if (target_type == TypeManager.short_type){
2606 if (v <= UInt16.MaxValue)
2608 } else if (target_type == TypeManager.ushort_type){
2609 if (v <= UInt16.MaxValue)
2611 } else if (target_type == TypeManager.int64_type)
2613 else if (target_type == TypeManager.uint64_type)
2616 } else if (c is LongConstant){
2617 long v = ((LongConstant) c).Value;
2619 if (target_type == TypeManager.int32_type){
2620 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2622 } else if (target_type == TypeManager.uint32_type){
2623 if (v >= 0 && v <= UInt32.MaxValue)
2625 } else if (target_type == TypeManager.char_type){
2626 if (v >= Char.MinValue && v <= Char.MaxValue)
2628 } else if (target_type == TypeManager.byte_type){
2629 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2631 } else if (target_type == TypeManager.sbyte_type){
2632 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2634 } else if (target_type == TypeManager.short_type){
2635 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2637 } else if (target_type == TypeManager.ushort_type){
2638 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2640 } else if (target_type == TypeManager.uint64_type){
2645 } else if (c is ULongConstant){
2646 ulong v = ((ULongConstant) c).Value;
2648 if (target_type == TypeManager.int32_type){
2649 if (v <= Int32.MaxValue)
2651 } else if (target_type == TypeManager.uint32_type){
2652 if (v <= UInt32.MaxValue)
2654 } else if (target_type == TypeManager.char_type){
2655 if (v >= Char.MinValue && v <= Char.MaxValue)
2657 } else if (target_type == TypeManager.byte_type){
2658 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2660 } else if (target_type == TypeManager.sbyte_type){
2661 if (v <= (int) SByte.MaxValue)
2663 } else if (target_type == TypeManager.short_type){
2664 if (v <= UInt16.MaxValue)
2666 } else if (target_type == TypeManager.ushort_type){
2667 if (v <= UInt16.MaxValue)
2669 } else if (target_type == TypeManager.int64_type){
2670 if (v <= Int64.MaxValue)
2674 } else if (c is ByteConstant){
2675 byte v = ((ByteConstant) c).Value;
2677 if (target_type == TypeManager.int32_type)
2679 else if (target_type == TypeManager.uint32_type)
2681 else if (target_type == TypeManager.char_type)
2683 else if (target_type == TypeManager.sbyte_type){
2684 if (v <= SByte.MaxValue)
2686 } else if (target_type == TypeManager.short_type)
2688 else if (target_type == TypeManager.ushort_type)
2690 else if (target_type == TypeManager.int64_type)
2692 else if (target_type == TypeManager.uint64_type)
2695 } else if (c is SByteConstant){
2696 sbyte v = ((SByteConstant) c).Value;
2698 if (target_type == TypeManager.int32_type)
2700 else if (target_type == TypeManager.uint32_type){
2703 } else if (target_type == TypeManager.char_type){
2706 } else if (target_type == TypeManager.byte_type){
2709 } else if (target_type == TypeManager.short_type)
2711 else if (target_type == TypeManager.ushort_type){
2714 } else if (target_type == TypeManager.int64_type)
2716 else if (target_type == TypeManager.uint64_type){
2721 } else if (c is ShortConstant){
2722 short v = ((ShortConstant) c).Value;
2724 if (target_type == TypeManager.int32_type){
2726 } else if (target_type == TypeManager.uint32_type){
2729 } else if (target_type == TypeManager.char_type){
2732 } else if (target_type == TypeManager.byte_type){
2733 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2735 } else if (target_type == TypeManager.sbyte_type){
2736 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2738 } else if (target_type == TypeManager.ushort_type){
2741 } else if (target_type == TypeManager.int64_type)
2743 else if (target_type == TypeManager.uint64_type)
2747 } else if (c is UShortConstant){
2748 ushort v = ((UShortConstant) c).Value;
2750 if (target_type == TypeManager.int32_type)
2752 else if (target_type == TypeManager.uint32_type)
2754 else if (target_type == TypeManager.char_type){
2755 if (v >= Char.MinValue && v <= Char.MaxValue)
2757 } else if (target_type == TypeManager.byte_type){
2758 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2760 } else if (target_type == TypeManager.sbyte_type){
2761 if (v <= SByte.MaxValue)
2763 } else if (target_type == TypeManager.short_type){
2764 if (v <= Int16.MaxValue)
2766 } else if (target_type == TypeManager.int64_type)
2768 else if (target_type == TypeManager.uint64_type)
2772 } else if (c is CharConstant){
2773 char v = ((CharConstant) c).Value;
2775 if (target_type == TypeManager.int32_type)
2777 else if (target_type == TypeManager.uint32_type)
2779 else if (target_type == TypeManager.byte_type){
2780 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2782 } else if (target_type == TypeManager.sbyte_type){
2783 if (v <= SByte.MaxValue)
2785 } else if (target_type == TypeManager.short_type){
2786 if (v <= Int16.MaxValue)
2788 } else if (target_type == TypeManager.ushort_type)
2790 else if (target_type == TypeManager.int64_type)
2792 else if (target_type == TypeManager.uint64_type)
2797 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2802 // Load the object from the pointer.
2804 public static void LoadFromPtr (ILGenerator ig, Type t)
2806 if (t == TypeManager.int32_type)
2807 ig.Emit (OpCodes.Ldind_I4);
2808 else if (t == TypeManager.uint32_type)
2809 ig.Emit (OpCodes.Ldind_U4);
2810 else if (t == TypeManager.short_type)
2811 ig.Emit (OpCodes.Ldind_I2);
2812 else if (t == TypeManager.ushort_type)
2813 ig.Emit (OpCodes.Ldind_U2);
2814 else if (t == TypeManager.char_type)
2815 ig.Emit (OpCodes.Ldind_U2);
2816 else if (t == TypeManager.byte_type)
2817 ig.Emit (OpCodes.Ldind_U1);
2818 else if (t == TypeManager.sbyte_type)
2819 ig.Emit (OpCodes.Ldind_I1);
2820 else if (t == TypeManager.uint64_type)
2821 ig.Emit (OpCodes.Ldind_I8);
2822 else if (t == TypeManager.int64_type)
2823 ig.Emit (OpCodes.Ldind_I8);
2824 else if (t == TypeManager.float_type)
2825 ig.Emit (OpCodes.Ldind_R4);
2826 else if (t == TypeManager.double_type)
2827 ig.Emit (OpCodes.Ldind_R8);
2828 else if (t == TypeManager.bool_type)
2829 ig.Emit (OpCodes.Ldind_I1);
2830 else if (t == TypeManager.intptr_type)
2831 ig.Emit (OpCodes.Ldind_I);
2832 else if (TypeManager.IsEnumType (t)) {
2833 if (t == TypeManager.enum_type)
2834 ig.Emit (OpCodes.Ldind_Ref);
2836 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2837 } else if (t.IsValueType)
2838 ig.Emit (OpCodes.Ldobj, t);
2840 ig.Emit (OpCodes.Ldind_Ref);
2844 // The stack contains the pointer and the value of type `type'
2846 public static void StoreFromPtr (ILGenerator ig, Type type)
2848 if (TypeManager.IsEnumType (type))
2849 type = TypeManager.EnumToUnderlying (type);
2850 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2851 ig.Emit (OpCodes.Stind_I4);
2852 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2853 ig.Emit (OpCodes.Stind_I8);
2854 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2855 type == TypeManager.ushort_type)
2856 ig.Emit (OpCodes.Stind_I2);
2857 else if (type == TypeManager.float_type)
2858 ig.Emit (OpCodes.Stind_R4);
2859 else if (type == TypeManager.double_type)
2860 ig.Emit (OpCodes.Stind_R8);
2861 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2862 type == TypeManager.bool_type)
2863 ig.Emit (OpCodes.Stind_I1);
2864 else if (type == TypeManager.intptr_type)
2865 ig.Emit (OpCodes.Stind_I);
2866 else if (type.IsValueType)
2867 ig.Emit (OpCodes.Stobj, type);
2869 ig.Emit (OpCodes.Stind_Ref);
2873 // Returns the size of type `t' if known, otherwise, 0
2875 public static int GetTypeSize (Type t)
2877 t = TypeManager.TypeToCoreType (t);
2878 if (t == TypeManager.int32_type ||
2879 t == TypeManager.uint32_type ||
2880 t == TypeManager.float_type)
2882 else if (t == TypeManager.int64_type ||
2883 t == TypeManager.uint64_type ||
2884 t == TypeManager.double_type)
2886 else if (t == TypeManager.byte_type ||
2887 t == TypeManager.sbyte_type ||
2888 t == TypeManager.bool_type)
2890 else if (t == TypeManager.short_type ||
2891 t == TypeManager.char_type ||
2892 t == TypeManager.ushort_type)
2894 else if (t == TypeManager.decimal_type)
2901 // Default implementation of IAssignMethod.CacheTemporaries
2903 public void CacheTemporaries (EmitContext ec)
2907 static void Error_NegativeArrayIndex (Location loc)
2909 Report.Error (284, loc, "Can not create array with a negative size");
2913 // Converts `source' to an int, uint, long or ulong.
2915 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2919 bool old_checked = ec.CheckState;
2920 ec.CheckState = true;
2922 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2923 if (target == null){
2924 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2925 if (target == null){
2926 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2927 if (target == null){
2928 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2930 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2934 ec.CheckState = old_checked;
2937 // Only positive constants are allowed at compile time
2939 if (target is Constant){
2940 if (target is IntConstant){
2941 if (((IntConstant) target).Value < 0){
2942 Error_NegativeArrayIndex (loc);
2947 if (target is LongConstant){
2948 if (((LongConstant) target).Value < 0){
2949 Error_NegativeArrayIndex (loc);
2962 /// This is just a base class for expressions that can
2963 /// appear on statements (invocations, object creation,
2964 /// assignments, post/pre increment and decrement). The idea
2965 /// being that they would support an extra Emition interface that
2966 /// does not leave a result on the stack.
2968 public abstract class ExpressionStatement : Expression {
2971 /// Requests the expression to be emitted in a `statement'
2972 /// context. This means that no new value is left on the
2973 /// stack after invoking this method (constrasted with
2974 /// Emit that will always leave a value on the stack).
2976 public abstract void EmitStatement (EmitContext ec);
2980 /// This kind of cast is used to encapsulate the child
2981 /// whose type is child.Type into an expression that is
2982 /// reported to return "return_type". This is used to encapsulate
2983 /// expressions which have compatible types, but need to be dealt
2984 /// at higher levels with.
2986 /// For example, a "byte" expression could be encapsulated in one
2987 /// of these as an "unsigned int". The type for the expression
2988 /// would be "unsigned int".
2991 public class EmptyCast : Expression {
2992 protected Expression child;
2994 public EmptyCast (Expression child, Type return_type)
2996 eclass = child.eclass;
3001 public override Expression DoResolve (EmitContext ec)
3003 // This should never be invoked, we are born in fully
3004 // initialized state.
3009 public override void Emit (EmitContext ec)
3016 /// This class is used to wrap literals which belong inside Enums
3018 public class EnumConstant : Constant {
3019 public Constant Child;
3021 public EnumConstant (Constant child, Type enum_type)
3023 eclass = child.eclass;
3028 public override Expression DoResolve (EmitContext ec)
3030 // This should never be invoked, we are born in fully
3031 // initialized state.
3036 public override void Emit (EmitContext ec)
3041 public override object GetValue ()
3043 return Child.GetValue ();
3047 // Converts from one of the valid underlying types for an enumeration
3048 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3049 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3051 public Constant WidenToCompilerConstant ()
3053 Type t = TypeManager.EnumToUnderlying (Child.Type);
3054 object v = ((Constant) Child).GetValue ();;
3056 if (t == TypeManager.int32_type)
3057 return new IntConstant ((int) v);
3058 if (t == TypeManager.uint32_type)
3059 return new UIntConstant ((uint) v);
3060 if (t == TypeManager.int64_type)
3061 return new LongConstant ((long) v);
3062 if (t == TypeManager.uint64_type)
3063 return new ULongConstant ((ulong) v);
3064 if (t == TypeManager.short_type)
3065 return new ShortConstant ((short) v);
3066 if (t == TypeManager.ushort_type)
3067 return new UShortConstant ((ushort) v);
3068 if (t == TypeManager.byte_type)
3069 return new ByteConstant ((byte) v);
3070 if (t == TypeManager.sbyte_type)
3071 return new SByteConstant ((sbyte) v);
3073 throw new Exception ("Invalid enumeration underlying type: " + t);
3077 // Extracts the value in the enumeration on its native representation
3079 public object GetPlainValue ()
3081 Type t = TypeManager.EnumToUnderlying (Child.Type);
3082 object v = ((Constant) Child).GetValue ();;
3084 if (t == TypeManager.int32_type)
3086 if (t == TypeManager.uint32_type)
3088 if (t == TypeManager.int64_type)
3090 if (t == TypeManager.uint64_type)
3092 if (t == TypeManager.short_type)
3094 if (t == TypeManager.ushort_type)
3096 if (t == TypeManager.byte_type)
3098 if (t == TypeManager.sbyte_type)
3104 public override string AsString ()
3106 return Child.AsString ();
3109 public override DoubleConstant ConvertToDouble ()
3111 return Child.ConvertToDouble ();
3114 public override FloatConstant ConvertToFloat ()
3116 return Child.ConvertToFloat ();
3119 public override ULongConstant ConvertToULong ()
3121 return Child.ConvertToULong ();
3124 public override LongConstant ConvertToLong ()
3126 return Child.ConvertToLong ();
3129 public override UIntConstant ConvertToUInt ()
3131 return Child.ConvertToUInt ();
3134 public override IntConstant ConvertToInt ()
3136 return Child.ConvertToInt ();
3141 /// This kind of cast is used to encapsulate Value Types in objects.
3143 /// The effect of it is to box the value type emitted by the previous
3146 public class BoxedCast : EmptyCast {
3148 public BoxedCast (Expression expr)
3149 : base (expr, TypeManager.object_type)
3153 public override Expression DoResolve (EmitContext ec)
3155 // This should never be invoked, we are born in fully
3156 // initialized state.
3161 public override void Emit (EmitContext ec)
3165 ec.ig.Emit (OpCodes.Box, child.Type);
3169 public class UnboxCast : EmptyCast {
3170 public UnboxCast (Expression expr, Type return_type)
3171 : base (expr, return_type)
3175 public override Expression DoResolve (EmitContext ec)
3177 // This should never be invoked, we are born in fully
3178 // initialized state.
3183 public override void Emit (EmitContext ec)
3186 ILGenerator ig = ec.ig;
3189 ig.Emit (OpCodes.Unbox, t);
3191 LoadFromPtr (ig, t);
3196 /// This is used to perform explicit numeric conversions.
3198 /// Explicit numeric conversions might trigger exceptions in a checked
3199 /// context, so they should generate the conv.ovf opcodes instead of
3202 public class ConvCast : EmptyCast {
3203 public enum Mode : byte {
3204 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3206 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3207 U2_I1, U2_U1, U2_I2, U2_CH,
3208 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3209 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3210 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3211 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3212 CH_I1, CH_U1, CH_I2,
3213 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3214 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3220 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3221 : base (child, return_type)
3223 checked_state = ec.CheckState;
3227 public override Expression DoResolve (EmitContext ec)
3229 // This should never be invoked, we are born in fully
3230 // initialized state.
3235 public override string ToString ()
3237 return String.Format ("ConvCast ({0}, {1})", mode, child);
3240 public override void Emit (EmitContext ec)
3242 ILGenerator ig = ec.ig;
3248 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3249 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3250 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3251 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3252 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3254 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3255 case Mode.U1_CH: /* nothing */ break;
3257 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3258 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3259 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3260 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3261 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3262 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3264 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3265 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3266 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3267 case Mode.U2_CH: /* nothing */ break;
3269 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3270 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3271 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3272 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3273 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3274 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3275 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3277 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3278 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3279 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3280 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3281 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3282 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3284 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3285 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3286 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3287 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3288 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3289 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3290 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3291 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3293 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3294 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3295 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3296 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3297 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3298 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3299 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3300 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3302 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3303 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3304 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3306 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3307 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3308 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3309 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3310 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3311 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3312 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3313 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3314 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3316 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3317 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3318 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3319 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3320 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3321 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3322 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3323 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3324 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3325 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3329 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3330 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3331 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3332 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3333 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3335 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3336 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3338 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3339 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3340 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3341 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3342 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3343 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3345 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3346 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3347 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3348 case Mode.U2_CH: /* nothing */ break;
3350 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3351 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3352 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3353 case Mode.I4_U4: /* nothing */ break;
3354 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3355 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3356 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3358 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3359 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3360 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3361 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3362 case Mode.U4_I4: /* nothing */ break;
3363 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3365 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3366 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3367 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3368 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3369 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3370 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3371 case Mode.I8_U8: /* nothing */ break;
3372 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3374 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3375 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3376 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3377 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3378 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3379 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3380 case Mode.U8_I8: /* nothing */ break;
3381 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3383 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3384 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3385 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3387 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3388 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3389 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3390 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3391 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3392 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3393 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3394 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3395 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3397 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3398 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3399 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3400 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3401 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3402 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3403 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3404 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3405 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3406 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3412 public class OpcodeCast : EmptyCast {
3416 public OpcodeCast (Expression child, Type return_type, OpCode op)
3417 : base (child, return_type)
3421 second_valid = false;
3424 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3425 : base (child, return_type)
3430 second_valid = true;
3433 public override Expression DoResolve (EmitContext ec)
3435 // This should never be invoked, we are born in fully
3436 // initialized state.
3441 public override void Emit (EmitContext ec)
3452 /// This kind of cast is used to encapsulate a child and cast it
3453 /// to the class requested
3455 public class ClassCast : EmptyCast {
3456 public ClassCast (Expression child, Type return_type)
3457 : base (child, return_type)
3462 public override Expression DoResolve (EmitContext ec)
3464 // This should never be invoked, we are born in fully
3465 // initialized state.
3470 public override void Emit (EmitContext ec)
3474 ec.ig.Emit (OpCodes.Castclass, type);
3480 /// SimpleName expressions are initially formed of a single
3481 /// word and it only happens at the beginning of the expression.
3485 /// The expression will try to be bound to a Field, a Method
3486 /// group or a Property. If those fail we pass the name to our
3487 /// caller and the SimpleName is compounded to perform a type
3488 /// lookup. The idea behind this process is that we want to avoid
3489 /// creating a namespace map from the assemblies, as that requires
3490 /// the GetExportedTypes function to be called and a hashtable to
3491 /// be constructed which reduces startup time. If later we find
3492 /// that this is slower, we should create a `NamespaceExpr' expression
3493 /// that fully participates in the resolution process.
3495 /// For example `System.Console.WriteLine' is decomposed into
3496 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3498 /// The first SimpleName wont produce a match on its own, so it will
3500 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3502 /// System.Console will produce a TypeExpr match.
3504 /// The downside of this is that we might be hitting `LookupType' too many
3505 /// times with this scheme.
3507 public class SimpleName : Expression {
3508 public readonly string Name;
3511 // If true, then we are a simple name, not composed with a ".
3515 public SimpleName (string a, string b, Location l)
3517 Name = String.Concat (a, ".", b);
3522 public SimpleName (string name, Location l)
3529 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3531 if (ec.IsFieldInitializer)
3534 "A field initializer cannot reference the non-static field, " +
3535 "method or property `"+name+"'");
3539 "An object reference is required " +
3540 "for the non-static field `"+name+"'");
3544 // Checks whether we are trying to access an instance
3545 // property, method or field from a static body.
3547 Expression MemberStaticCheck (EmitContext ec, Expression e)
3549 if (e is IMemberExpr){
3550 IMemberExpr member = (IMemberExpr) e;
3552 if (!member.IsStatic){
3553 Error_ObjectRefRequired (ec, loc, Name);
3561 public override Expression DoResolve (EmitContext ec)
3563 return SimpleNameResolve (ec, null, false);
3566 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3568 return SimpleNameResolve (ec, right_side, false);
3572 public Expression DoResolveAllowStatic (EmitContext ec)
3574 return SimpleNameResolve (ec, null, true);
3577 public override Expression ResolveAsTypeStep (EmitContext ec)
3579 DeclSpace ds = ec.DeclSpace;
3580 Namespace ns = ds.Namespace;
3585 // Since we are cheating: we only do the Alias lookup for
3586 // namespaces if the name does not include any dots in it
3588 if (ns != null && is_base)
3589 alias_value = ns.LookupAlias (Name);
3593 if (ec.ResolvingTypeTree){
3594 if (alias_value != null){
3595 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3596 return new TypeExpr (t, loc);
3599 int errors = Report.Errors;
3600 Type dt = ec.DeclSpace.FindType (loc, Name);
3601 if (Report.Errors != errors)
3605 return new TypeExpr (dt, loc);
3609 // First, the using aliases
3611 if (alias_value != null){
3612 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3613 return new TypeExpr (t, loc);
3615 // we have alias value, but it isn't Type, so try if it's namespace
3616 return new SimpleName (alias_value, loc);
3620 // Stage 2: Lookup up if we are an alias to a type
3624 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3625 return new TypeExpr (t, loc);
3627 // No match, maybe our parent can compose us
3628 // into something meaningful.
3633 /// 7.5.2: Simple Names.
3635 /// Local Variables and Parameters are handled at
3636 /// parse time, so they never occur as SimpleNames.
3638 /// The `allow_static' flag is used by MemberAccess only
3639 /// and it is used to inform us that it is ok for us to
3640 /// avoid the static check, because MemberAccess might end
3641 /// up resolving the Name as a Type name and the access as
3642 /// a static type access.
3644 /// ie: Type Type; .... { Type.GetType (""); }
3646 /// Type is both an instance variable and a Type; Type.GetType
3647 /// is the static method not an instance method of type.
3649 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3651 Expression e = null;
3654 // Stage 1: Performed by the parser (binding to locals or parameters).
3656 Block current_block = ec.CurrentBlock;
3657 if (current_block != null && current_block.GetVariableInfo (Name) != null){
3658 LocalVariableReference var;
3660 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3662 if (right_side != null)
3663 return var.ResolveLValue (ec, right_side);
3665 return var.Resolve (ec);
3668 if (current_block != null){
3670 Parameter par = null;
3671 Parameters pars = current_block.Parameters;
3673 par = pars.GetParameterByName (Name, out idx);
3676 ParameterReference param;
3678 param = new ParameterReference (pars, idx, Name, loc);
3680 if (right_side != null)
3681 return param.ResolveLValue (ec, right_side);
3683 return param.Resolve (ec);
3688 // Stage 2: Lookup members
3691 DeclSpace lookup_ds = ec.DeclSpace;
3693 if (lookup_ds.TypeBuilder == null)
3696 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3700 lookup_ds =lookup_ds.Parent;
3701 } while (lookup_ds != null);
3703 if (e == null && ec.ContainerType != null)
3704 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3707 return ResolveAsTypeStep (ec);
3712 if (e is IMemberExpr) {
3713 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3717 IMemberExpr me = e as IMemberExpr;
3721 // This fails if ResolveMemberAccess() was unable to decide whether
3722 // it's a field or a type of the same name.
3723 if (!me.IsStatic && (me.InstanceExpression == null))
3727 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
3728 !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
3729 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3730 "outer type `" + me.DeclaringType + "' via nested type `" +
3731 me.InstanceExpression.Type + "'");
3735 if (right_side != null)
3736 e = e.DoResolveLValue (ec, right_side);
3738 e = e.DoResolve (ec);
3743 if (ec.IsStatic || ec.IsFieldInitializer){
3747 return MemberStaticCheck (ec, e);
3752 public override void Emit (EmitContext ec)
3755 // If this is ever reached, then we failed to
3756 // find the name as a namespace
3759 Error (103, "The name `" + Name +
3760 "' does not exist in the class `" +
3761 ec.DeclSpace.Name + "'");
3764 public override string ToString ()
3771 /// Fully resolved expression that evaluates to a type
3773 public class TypeExpr : Expression {
3774 public TypeExpr (Type t, Location l)
3777 eclass = ExprClass.Type;
3781 public override Expression ResolveAsTypeStep (EmitContext ec)
3786 override public Expression DoResolve (EmitContext ec)
3791 override public void Emit (EmitContext ec)
3793 throw new Exception ("Should never be called");
3796 public override string ToString ()
3798 return Type.ToString ();
3803 /// Used to create types from a fully qualified name. These are just used
3804 /// by the parser to setup the core types. A TypeLookupExpression is always
3805 /// classified as a type.
3807 public class TypeLookupExpression : TypeExpr {
3810 public TypeLookupExpression (string name) : base (null, Location.Null)
3815 public override Expression ResolveAsTypeStep (EmitContext ec)
3818 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3822 public override Expression DoResolve (EmitContext ec)
3824 return ResolveAsTypeStep (ec);
3827 public override void Emit (EmitContext ec)
3829 throw new Exception ("Should never be called");
3832 public override string ToString ()
3839 /// MethodGroup Expression.
3841 /// This is a fully resolved expression that evaluates to a type
3843 public class MethodGroupExpr : Expression, IMemberExpr {
3844 public MethodBase [] Methods;
3845 Expression instance_expression = null;
3846 bool is_explicit_impl = false;
3848 public MethodGroupExpr (MemberInfo [] mi, Location l)
3850 Methods = new MethodBase [mi.Length];
3851 mi.CopyTo (Methods, 0);
3852 eclass = ExprClass.MethodGroup;
3853 type = TypeManager.object_type;
3857 public MethodGroupExpr (ArrayList list, Location l)
3859 Methods = new MethodBase [list.Count];
3862 list.CopyTo (Methods, 0);
3864 foreach (MemberInfo m in list){
3865 if (!(m is MethodBase)){
3866 Console.WriteLine ("Name " + m.Name);
3867 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3873 eclass = ExprClass.MethodGroup;
3874 type = TypeManager.object_type;
3877 public Type DeclaringType {
3879 return Methods [0].DeclaringType;
3884 // `A method group may have associated an instance expression'
3886 public Expression InstanceExpression {
3888 return instance_expression;
3892 instance_expression = value;
3896 public bool IsExplicitImpl {
3898 return is_explicit_impl;
3902 is_explicit_impl = value;
3906 public string Name {
3908 return Methods [0].Name;
3912 public bool IsInstance {
3914 foreach (MethodBase mb in Methods)
3922 public bool IsStatic {
3924 foreach (MethodBase mb in Methods)
3932 override public Expression DoResolve (EmitContext ec)
3934 if (instance_expression != null) {
3935 instance_expression = instance_expression.DoResolve (ec);
3936 if (instance_expression == null)
3943 public void ReportUsageError ()
3945 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3946 Methods [0].Name + "()' is referenced without parentheses");
3949 override public void Emit (EmitContext ec)
3951 ReportUsageError ();
3954 bool RemoveMethods (bool keep_static)
3956 ArrayList smethods = new ArrayList ();
3958 foreach (MethodBase mb in Methods){
3959 if (mb.IsStatic == keep_static)
3963 if (smethods.Count == 0)
3966 Methods = new MethodBase [smethods.Count];
3967 smethods.CopyTo (Methods, 0);
3973 /// Removes any instance methods from the MethodGroup, returns
3974 /// false if the resulting set is empty.
3976 public bool RemoveInstanceMethods ()
3978 return RemoveMethods (true);
3982 /// Removes any static methods from the MethodGroup, returns
3983 /// false if the resulting set is empty.
3985 public bool RemoveStaticMethods ()
3987 return RemoveMethods (false);
3992 /// Fully resolved expression that evaluates to a Field
3994 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3995 public readonly FieldInfo FieldInfo;
3996 Expression instance_expr;
3998 public FieldExpr (FieldInfo fi, Location l)
4001 eclass = ExprClass.Variable;
4002 type = fi.FieldType;
4006 public string Name {
4008 return FieldInfo.Name;
4012 public bool IsInstance {
4014 return !FieldInfo.IsStatic;
4018 public bool IsStatic {
4020 return FieldInfo.IsStatic;
4024 public Type DeclaringType {
4026 return FieldInfo.DeclaringType;
4030 public Expression InstanceExpression {
4032 return instance_expr;
4036 instance_expr = value;
4040 override public Expression DoResolve (EmitContext ec)
4042 if (!FieldInfo.IsStatic){
4043 if (instance_expr == null){
4045 // This can happen when referencing an instance field using
4046 // a fully qualified type expression: TypeName.InstanceField = xxx
4048 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
4052 // Resolve the field's instance expression while flow analysis is turned
4053 // off: when accessing a field "a.b", we must check whether the field
4054 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4055 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4056 ResolveFlags.DisableFlowAnalysis);
4057 if (instance_expr == null)
4061 // If the instance expression is a local variable or parameter.
4062 IVariable var = instance_expr as IVariable;
4063 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4069 void Report_AssignToReadonly (bool is_instance)
4074 msg = "Readonly field can not be assigned outside " +
4075 "of constructor or variable initializer";
4077 msg = "A static readonly field can only be assigned in " +
4078 "a static constructor";
4080 Report.Error (is_instance ? 191 : 198, loc, msg);
4083 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4085 IVariable var = instance_expr as IVariable;
4087 var.SetFieldAssigned (ec, FieldInfo.Name);
4089 Expression e = DoResolve (ec);
4094 if (!FieldInfo.IsInitOnly)
4098 // InitOnly fields can only be assigned in constructors
4101 if (ec.IsConstructor){
4102 if (ec.ContainerType == FieldInfo.DeclaringType)
4106 Report_AssignToReadonly (true);
4111 override public void Emit (EmitContext ec)
4113 ILGenerator ig = ec.ig;
4114 bool is_volatile = false;
4116 if (FieldInfo is FieldBuilder){
4117 FieldBase f = TypeManager.GetField (FieldInfo);
4119 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4122 f.status |= Field.Status.USED;
4125 if (FieldInfo.IsStatic){
4127 ig.Emit (OpCodes.Volatile);
4129 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4131 if (instance_expr.Type.IsValueType){
4133 LocalTemporary tempo = null;
4135 if (!(instance_expr is IMemoryLocation)){
4136 tempo = new LocalTemporary (
4137 ec, instance_expr.Type);
4139 InstanceExpression.Emit (ec);
4143 ml = (IMemoryLocation) instance_expr;
4145 ml.AddressOf (ec, AddressOp.Load);
4147 instance_expr.Emit (ec);
4150 ig.Emit (OpCodes.Volatile);
4152 ig.Emit (OpCodes.Ldfld, FieldInfo);
4156 public void EmitAssign (EmitContext ec, Expression source)
4158 FieldAttributes fa = FieldInfo.Attributes;
4159 bool is_static = (fa & FieldAttributes.Static) != 0;
4160 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4161 ILGenerator ig = ec.ig;
4163 if (is_readonly && !ec.IsConstructor){
4164 Report_AssignToReadonly (!is_static);
4169 Expression instance = instance_expr;
4171 if (instance.Type.IsValueType){
4172 if (instance is IMemoryLocation){
4173 IMemoryLocation ml = (IMemoryLocation) instance;
4175 ml.AddressOf (ec, AddressOp.Store);
4177 throw new Exception ("The " + instance + " of type " +
4179 " represents a ValueType and does " +
4180 "not implement IMemoryLocation");
4186 if (FieldInfo is FieldBuilder){
4187 FieldBase f = TypeManager.GetField (FieldInfo);
4189 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4190 ig.Emit (OpCodes.Volatile);
4194 ig.Emit (OpCodes.Stsfld, FieldInfo);
4196 ig.Emit (OpCodes.Stfld, FieldInfo);
4198 if (FieldInfo is FieldBuilder){
4199 FieldBase f = TypeManager.GetField (FieldInfo);
4201 f.status |= Field.Status.ASSIGNED;
4205 public void AddressOf (EmitContext ec, AddressOp mode)
4207 ILGenerator ig = ec.ig;
4209 if (FieldInfo is FieldBuilder){
4210 FieldBase f = TypeManager.GetField (FieldInfo);
4211 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4212 ig.Emit (OpCodes.Volatile);
4215 if (FieldInfo is FieldBuilder){
4216 FieldBase f = TypeManager.GetField (FieldInfo);
4218 if ((mode & AddressOp.Store) != 0)
4219 f.status |= Field.Status.ASSIGNED;
4220 if ((mode & AddressOp.Load) != 0)
4221 f.status |= Field.Status.USED;
4225 // Handle initonly fields specially: make a copy and then
4226 // get the address of the copy.
4228 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4232 local = ig.DeclareLocal (type);
4233 ig.Emit (OpCodes.Stloc, local);
4234 ig.Emit (OpCodes.Ldloca, local);
4238 if (FieldInfo.IsStatic)
4239 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4242 // In the case of `This', we call the AddressOf method, which will
4243 // only load the pointer, and not perform an Ldobj immediately after
4244 // the value has been loaded into the stack.
4246 if (instance_expr is This)
4247 ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4248 else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
4249 IMemoryLocation ml = (IMemoryLocation) instance_expr;
4251 ml.AddressOf (ec, AddressOp.LoadStore);
4253 instance_expr.Emit (ec);
4254 ig.Emit (OpCodes.Ldflda, FieldInfo);
4260 /// Expression that evaluates to a Property. The Assign class
4261 /// might set the `Value' expression if we are in an assignment.
4263 /// This is not an LValue because we need to re-write the expression, we
4264 /// can not take data from the stack and store it.
4266 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4267 public readonly PropertyInfo PropertyInfo;
4270 // This is set externally by the `BaseAccess' class
4273 MethodInfo getter, setter;
4275 bool must_do_cs1540_check;
4277 Expression instance_expr;
4279 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4282 eclass = ExprClass.PropertyAccess;
4286 type = TypeManager.TypeToCoreType (pi.PropertyType);
4288 ResolveAccessors (ec);
4291 public string Name {
4293 return PropertyInfo.Name;
4297 public bool IsInstance {
4303 public bool IsStatic {
4309 public Type DeclaringType {
4311 return PropertyInfo.DeclaringType;
4316 // The instance expression associated with this expression
4318 public Expression InstanceExpression {
4320 instance_expr = value;
4324 return instance_expr;
4328 public bool VerifyAssignable ()
4330 if (setter == null) {
4331 Report.Error (200, loc,
4332 "The property `" + PropertyInfo.Name +
4333 "' can not be assigned to, as it has not set accessor");
4340 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
4342 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
4343 BindingFlags.Static | BindingFlags.Instance;
4346 group = TypeManager.MemberLookup (
4347 invocation_type, invocation_type, PropertyInfo.DeclaringType,
4348 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
4351 // The first method is the closest to us
4356 foreach (MethodInfo mi in group) {
4357 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
4360 // If only accessible to the current class or children
4362 if (ma == MethodAttributes.Private) {
4363 Type declaring_type = mi.DeclaringType;
4365 if (invocation_type != declaring_type){
4366 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4374 // FamAndAssem requires that we not only derivate, but we are on the
4377 if (ma == MethodAttributes.FamANDAssem){
4378 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4384 // Assembly and FamORAssem succeed if we're in the same assembly.
4385 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
4386 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4392 // We already know that we aren't in the same assembly.
4393 if (ma == MethodAttributes.Assembly)
4396 // Family and FamANDAssem require that we derive.
4397 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
4398 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4401 must_do_cs1540_check = true;
4414 // We also perform the permission checking here, as the PropertyInfo does not
4415 // hold the information for the accessibility of its setter/getter
4417 void ResolveAccessors (EmitContext ec)
4419 getter = GetAccessor (ec.ContainerType, "get");
4420 if ((getter != null) && getter.IsStatic)
4423 setter = GetAccessor (ec.ContainerType, "set");
4424 if ((setter != null) && setter.IsStatic)
4427 if (setter == null && getter == null){
4428 Error (122, "`" + PropertyInfo.Name + "' " +
4429 "is inaccessible because of its protection level");
4434 bool InstanceResolve (EmitContext ec)
4436 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4437 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4441 if (instance_expr != null) {
4442 instance_expr = instance_expr.DoResolve (ec);
4443 if (instance_expr == null)
4447 if (must_do_cs1540_check && (instance_expr != null)) {
4448 if ((instance_expr.Type != ec.ContainerType) &&
4449 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
4450 Report.Error (1540, loc, "Cannot access protected member `" +
4451 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
4452 "' via a qualifier of type `" +
4453 TypeManager.CSharpName (instance_expr.Type) +
4454 "'; the qualifier must be of type `" +
4455 TypeManager.CSharpName (ec.ContainerType) +
4456 "' (or derived from it)");
4464 override public Expression DoResolve (EmitContext ec)
4466 if (getter == null){
4468 // The following condition happens if the PropertyExpr was
4469 // created, but is invalid (ie, the property is inaccessible),
4470 // and we did not want to embed the knowledge about this in
4471 // the caller routine. This only avoids double error reporting.
4476 Report.Error (154, loc,
4477 "The property `" + PropertyInfo.Name +
4478 "' can not be used in " +
4479 "this context because it lacks a get accessor");
4483 if (!InstanceResolve (ec))
4487 // Only base will allow this invocation to happen.
4489 if (IsBase && getter.IsAbstract){
4490 Report.Error (205, loc, "Cannot call an abstract base property: " +
4491 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4498 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4500 if (setter == null){
4502 // The following condition happens if the PropertyExpr was
4503 // created, but is invalid (ie, the property is inaccessible),
4504 // and we did not want to embed the knowledge about this in
4505 // the caller routine. This only avoids double error reporting.
4510 Report.Error (154, loc,
4511 "The property `" + PropertyInfo.Name +
4512 "' can not be used in " +
4513 "this context because it lacks a set accessor");
4517 if (!InstanceResolve (ec))
4521 // Only base will allow this invocation to happen.
4523 if (IsBase && setter.IsAbstract){
4524 Report.Error (205, loc, "Cannot call an abstract base property: " +
4525 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4531 override public void Emit (EmitContext ec)
4534 // Special case: length of single dimension array property is turned into ldlen
4536 if ((getter == TypeManager.system_int_array_get_length) ||
4537 (getter == TypeManager.int_array_get_length)){
4538 Type iet = instance_expr.Type;
4541 // System.Array.Length can be called, but the Type does not
4542 // support invoking GetArrayRank, so test for that case first
4544 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4545 instance_expr.Emit (ec);
4546 ec.ig.Emit (OpCodes.Ldlen);
4551 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
4556 // Implements the IAssignMethod interface for assignments
4558 public void EmitAssign (EmitContext ec, Expression source)
4560 Argument arg = new Argument (source, Argument.AType.Expression);
4561 ArrayList args = new ArrayList ();
4564 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
4567 override public void EmitStatement (EmitContext ec)
4570 ec.ig.Emit (OpCodes.Pop);
4575 /// Fully resolved expression that evaluates to an Event
4577 public class EventExpr : Expression, IMemberExpr {
4578 public readonly EventInfo EventInfo;
4579 public Expression instance_expr;
4582 MethodInfo add_accessor, remove_accessor;
4584 public EventExpr (EventInfo ei, Location loc)
4588 eclass = ExprClass.EventAccess;
4590 add_accessor = TypeManager.GetAddMethod (ei);
4591 remove_accessor = TypeManager.GetRemoveMethod (ei);
4593 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4596 if (EventInfo is MyEventBuilder){
4597 MyEventBuilder eb = (MyEventBuilder) EventInfo;
4598 type = eb.EventType;
4601 type = EventInfo.EventHandlerType;
4604 public string Name {
4606 return EventInfo.Name;
4610 public bool IsInstance {
4616 public bool IsStatic {
4622 public Type DeclaringType {
4624 return EventInfo.DeclaringType;
4628 public Expression InstanceExpression {
4630 return instance_expr;
4634 instance_expr = value;
4638 public override Expression DoResolve (EmitContext ec)
4640 if (instance_expr != null) {
4641 instance_expr = instance_expr.DoResolve (ec);
4642 if (instance_expr == null)
4650 public override void Emit (EmitContext ec)
4652 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4655 public void EmitAddOrRemove (EmitContext ec, Expression source)
4657 Expression handler = ((Binary) source).Right;
4659 Argument arg = new Argument (handler, Argument.AType.Expression);
4660 ArrayList args = new ArrayList ();
4664 if (((Binary) source).Oper == Binary.Operator.Addition)
4665 Invocation.EmitCall (
4666 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4668 Invocation.EmitCall (
4669 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);