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);
2839 else if (t.IsPointer)
2840 ig.Emit (OpCodes.Ldind_I);
2842 ig.Emit (OpCodes.Ldind_Ref);
2846 // The stack contains the pointer and the value of type `type'
2848 public static void StoreFromPtr (ILGenerator ig, Type type)
2850 if (TypeManager.IsEnumType (type))
2851 type = TypeManager.EnumToUnderlying (type);
2852 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2853 ig.Emit (OpCodes.Stind_I4);
2854 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2855 ig.Emit (OpCodes.Stind_I8);
2856 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2857 type == TypeManager.ushort_type)
2858 ig.Emit (OpCodes.Stind_I2);
2859 else if (type == TypeManager.float_type)
2860 ig.Emit (OpCodes.Stind_R4);
2861 else if (type == TypeManager.double_type)
2862 ig.Emit (OpCodes.Stind_R8);
2863 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2864 type == TypeManager.bool_type)
2865 ig.Emit (OpCodes.Stind_I1);
2866 else if (type == TypeManager.intptr_type)
2867 ig.Emit (OpCodes.Stind_I);
2868 else if (type.IsValueType)
2869 ig.Emit (OpCodes.Stobj, type);
2871 ig.Emit (OpCodes.Stind_Ref);
2875 // Returns the size of type `t' if known, otherwise, 0
2877 public static int GetTypeSize (Type t)
2879 t = TypeManager.TypeToCoreType (t);
2880 if (t == TypeManager.int32_type ||
2881 t == TypeManager.uint32_type ||
2882 t == TypeManager.float_type)
2884 else if (t == TypeManager.int64_type ||
2885 t == TypeManager.uint64_type ||
2886 t == TypeManager.double_type)
2888 else if (t == TypeManager.byte_type ||
2889 t == TypeManager.sbyte_type ||
2890 t == TypeManager.bool_type)
2892 else if (t == TypeManager.short_type ||
2893 t == TypeManager.char_type ||
2894 t == TypeManager.ushort_type)
2896 else if (t == TypeManager.decimal_type)
2903 // Default implementation of IAssignMethod.CacheTemporaries
2905 public void CacheTemporaries (EmitContext ec)
2909 static void Error_NegativeArrayIndex (Location loc)
2911 Report.Error (284, loc, "Can not create array with a negative size");
2915 // Converts `source' to an int, uint, long or ulong.
2917 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2921 bool old_checked = ec.CheckState;
2922 ec.CheckState = true;
2924 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2925 if (target == null){
2926 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2927 if (target == null){
2928 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2929 if (target == null){
2930 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2932 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2936 ec.CheckState = old_checked;
2939 // Only positive constants are allowed at compile time
2941 if (target is Constant){
2942 if (target is IntConstant){
2943 if (((IntConstant) target).Value < 0){
2944 Error_NegativeArrayIndex (loc);
2949 if (target is LongConstant){
2950 if (((LongConstant) target).Value < 0){
2951 Error_NegativeArrayIndex (loc);
2964 /// This is just a base class for expressions that can
2965 /// appear on statements (invocations, object creation,
2966 /// assignments, post/pre increment and decrement). The idea
2967 /// being that they would support an extra Emition interface that
2968 /// does not leave a result on the stack.
2970 public abstract class ExpressionStatement : Expression {
2973 /// Requests the expression to be emitted in a `statement'
2974 /// context. This means that no new value is left on the
2975 /// stack after invoking this method (constrasted with
2976 /// Emit that will always leave a value on the stack).
2978 public abstract void EmitStatement (EmitContext ec);
2982 /// This kind of cast is used to encapsulate the child
2983 /// whose type is child.Type into an expression that is
2984 /// reported to return "return_type". This is used to encapsulate
2985 /// expressions which have compatible types, but need to be dealt
2986 /// at higher levels with.
2988 /// For example, a "byte" expression could be encapsulated in one
2989 /// of these as an "unsigned int". The type for the expression
2990 /// would be "unsigned int".
2993 public class EmptyCast : Expression {
2994 protected Expression child;
2996 public EmptyCast (Expression child, Type return_type)
2998 eclass = child.eclass;
3003 public override Expression DoResolve (EmitContext ec)
3005 // This should never be invoked, we are born in fully
3006 // initialized state.
3011 public override void Emit (EmitContext ec)
3018 /// This class is used to wrap literals which belong inside Enums
3020 public class EnumConstant : Constant {
3021 public Constant Child;
3023 public EnumConstant (Constant child, Type enum_type)
3025 eclass = child.eclass;
3030 public override Expression DoResolve (EmitContext ec)
3032 // This should never be invoked, we are born in fully
3033 // initialized state.
3038 public override void Emit (EmitContext ec)
3043 public override object GetValue ()
3045 return Child.GetValue ();
3049 // Converts from one of the valid underlying types for an enumeration
3050 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3051 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3053 public Constant WidenToCompilerConstant ()
3055 Type t = TypeManager.EnumToUnderlying (Child.Type);
3056 object v = ((Constant) Child).GetValue ();;
3058 if (t == TypeManager.int32_type)
3059 return new IntConstant ((int) v);
3060 if (t == TypeManager.uint32_type)
3061 return new UIntConstant ((uint) v);
3062 if (t == TypeManager.int64_type)
3063 return new LongConstant ((long) v);
3064 if (t == TypeManager.uint64_type)
3065 return new ULongConstant ((ulong) v);
3066 if (t == TypeManager.short_type)
3067 return new ShortConstant ((short) v);
3068 if (t == TypeManager.ushort_type)
3069 return new UShortConstant ((ushort) v);
3070 if (t == TypeManager.byte_type)
3071 return new ByteConstant ((byte) v);
3072 if (t == TypeManager.sbyte_type)
3073 return new SByteConstant ((sbyte) v);
3075 throw new Exception ("Invalid enumeration underlying type: " + t);
3079 // Extracts the value in the enumeration on its native representation
3081 public object GetPlainValue ()
3083 Type t = TypeManager.EnumToUnderlying (Child.Type);
3084 object v = ((Constant) Child).GetValue ();;
3086 if (t == TypeManager.int32_type)
3088 if (t == TypeManager.uint32_type)
3090 if (t == TypeManager.int64_type)
3092 if (t == TypeManager.uint64_type)
3094 if (t == TypeManager.short_type)
3096 if (t == TypeManager.ushort_type)
3098 if (t == TypeManager.byte_type)
3100 if (t == TypeManager.sbyte_type)
3106 public override string AsString ()
3108 return Child.AsString ();
3111 public override DoubleConstant ConvertToDouble ()
3113 return Child.ConvertToDouble ();
3116 public override FloatConstant ConvertToFloat ()
3118 return Child.ConvertToFloat ();
3121 public override ULongConstant ConvertToULong ()
3123 return Child.ConvertToULong ();
3126 public override LongConstant ConvertToLong ()
3128 return Child.ConvertToLong ();
3131 public override UIntConstant ConvertToUInt ()
3133 return Child.ConvertToUInt ();
3136 public override IntConstant ConvertToInt ()
3138 return Child.ConvertToInt ();
3143 /// This kind of cast is used to encapsulate Value Types in objects.
3145 /// The effect of it is to box the value type emitted by the previous
3148 public class BoxedCast : EmptyCast {
3150 public BoxedCast (Expression expr)
3151 : base (expr, TypeManager.object_type)
3155 public override Expression DoResolve (EmitContext ec)
3157 // This should never be invoked, we are born in fully
3158 // initialized state.
3163 public override void Emit (EmitContext ec)
3167 ec.ig.Emit (OpCodes.Box, child.Type);
3171 public class UnboxCast : EmptyCast {
3172 public UnboxCast (Expression expr, Type return_type)
3173 : base (expr, return_type)
3177 public override Expression DoResolve (EmitContext ec)
3179 // This should never be invoked, we are born in fully
3180 // initialized state.
3185 public override void Emit (EmitContext ec)
3188 ILGenerator ig = ec.ig;
3191 ig.Emit (OpCodes.Unbox, t);
3193 LoadFromPtr (ig, t);
3198 /// This is used to perform explicit numeric conversions.
3200 /// Explicit numeric conversions might trigger exceptions in a checked
3201 /// context, so they should generate the conv.ovf opcodes instead of
3204 public class ConvCast : EmptyCast {
3205 public enum Mode : byte {
3206 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3208 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3209 U2_I1, U2_U1, U2_I2, U2_CH,
3210 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3211 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3212 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3213 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3214 CH_I1, CH_U1, CH_I2,
3215 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3216 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3222 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3223 : base (child, return_type)
3225 checked_state = ec.CheckState;
3229 public override Expression DoResolve (EmitContext ec)
3231 // This should never be invoked, we are born in fully
3232 // initialized state.
3237 public override string ToString ()
3239 return String.Format ("ConvCast ({0}, {1})", mode, child);
3242 public override void Emit (EmitContext ec)
3244 ILGenerator ig = ec.ig;
3250 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3251 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3252 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3253 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3254 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3256 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3257 case Mode.U1_CH: /* nothing */ break;
3259 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3260 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3261 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3262 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3263 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3264 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3266 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3267 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3268 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3269 case Mode.U2_CH: /* nothing */ break;
3271 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3272 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3273 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3274 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3275 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3276 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3277 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3279 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3280 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3281 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3282 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3283 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3284 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3286 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3287 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3288 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3289 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3290 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3291 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3292 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3293 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3295 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3296 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3297 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3298 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3299 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3300 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3301 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3302 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3304 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3305 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3306 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3308 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3309 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3310 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3311 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3312 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3313 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3314 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3315 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3316 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3318 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3319 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3320 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3321 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3322 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3323 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3324 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3325 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3326 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3327 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3331 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3332 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3333 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3334 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3335 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3337 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3338 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3340 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3341 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3342 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3343 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3344 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3345 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3347 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3348 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3349 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3350 case Mode.U2_CH: /* nothing */ break;
3352 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3353 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3354 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3355 case Mode.I4_U4: /* nothing */ break;
3356 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3357 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3358 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3360 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3361 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3362 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3363 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3364 case Mode.U4_I4: /* nothing */ break;
3365 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3367 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3368 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3369 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3370 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3371 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3372 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3373 case Mode.I8_U8: /* nothing */ break;
3374 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3376 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3377 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3378 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3379 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3380 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3381 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3382 case Mode.U8_I8: /* nothing */ break;
3383 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3385 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3386 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3387 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3389 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3390 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3391 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3392 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3393 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3394 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3395 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3396 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3397 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3399 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3400 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3401 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3402 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3403 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3404 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3405 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3406 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3407 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3408 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3414 public class OpcodeCast : EmptyCast {
3418 public OpcodeCast (Expression child, Type return_type, OpCode op)
3419 : base (child, return_type)
3423 second_valid = false;
3426 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3427 : base (child, return_type)
3432 second_valid = true;
3435 public override Expression DoResolve (EmitContext ec)
3437 // This should never be invoked, we are born in fully
3438 // initialized state.
3443 public override void Emit (EmitContext ec)
3454 /// This kind of cast is used to encapsulate a child and cast it
3455 /// to the class requested
3457 public class ClassCast : EmptyCast {
3458 public ClassCast (Expression child, Type return_type)
3459 : base (child, return_type)
3464 public override Expression DoResolve (EmitContext ec)
3466 // This should never be invoked, we are born in fully
3467 // initialized state.
3472 public override void Emit (EmitContext ec)
3476 ec.ig.Emit (OpCodes.Castclass, type);
3482 /// SimpleName expressions are initially formed of a single
3483 /// word and it only happens at the beginning of the expression.
3487 /// The expression will try to be bound to a Field, a Method
3488 /// group or a Property. If those fail we pass the name to our
3489 /// caller and the SimpleName is compounded to perform a type
3490 /// lookup. The idea behind this process is that we want to avoid
3491 /// creating a namespace map from the assemblies, as that requires
3492 /// the GetExportedTypes function to be called and a hashtable to
3493 /// be constructed which reduces startup time. If later we find
3494 /// that this is slower, we should create a `NamespaceExpr' expression
3495 /// that fully participates in the resolution process.
3497 /// For example `System.Console.WriteLine' is decomposed into
3498 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3500 /// The first SimpleName wont produce a match on its own, so it will
3502 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3504 /// System.Console will produce a TypeExpr match.
3506 /// The downside of this is that we might be hitting `LookupType' too many
3507 /// times with this scheme.
3509 public class SimpleName : Expression {
3510 public readonly string Name;
3513 // If true, then we are a simple name, not composed with a ".
3517 public SimpleName (string a, string b, Location l)
3519 Name = String.Concat (a, ".", b);
3524 public SimpleName (string name, Location l)
3531 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3533 if (ec.IsFieldInitializer)
3536 "A field initializer cannot reference the non-static field, " +
3537 "method or property `"+name+"'");
3541 "An object reference is required " +
3542 "for the non-static field `"+name+"'");
3546 // Checks whether we are trying to access an instance
3547 // property, method or field from a static body.
3549 Expression MemberStaticCheck (EmitContext ec, Expression e)
3551 if (e is IMemberExpr){
3552 IMemberExpr member = (IMemberExpr) e;
3554 if (!member.IsStatic){
3555 Error_ObjectRefRequired (ec, loc, Name);
3563 public override Expression DoResolve (EmitContext ec)
3565 return SimpleNameResolve (ec, null, false);
3568 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3570 return SimpleNameResolve (ec, right_side, false);
3574 public Expression DoResolveAllowStatic (EmitContext ec)
3576 return SimpleNameResolve (ec, null, true);
3579 public override Expression ResolveAsTypeStep (EmitContext ec)
3581 DeclSpace ds = ec.DeclSpace;
3582 Namespace ns = ds.Namespace;
3587 // Since we are cheating: we only do the Alias lookup for
3588 // namespaces if the name does not include any dots in it
3590 if (ns != null && is_base)
3591 alias_value = ns.LookupAlias (Name);
3595 if (ec.ResolvingTypeTree){
3596 if (alias_value != null){
3597 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3598 return new TypeExpr (t, loc);
3601 int errors = Report.Errors;
3602 Type dt = ec.DeclSpace.FindType (loc, Name);
3604 if (Report.Errors != errors)
3608 return new TypeExpr (dt, loc);
3612 // First, the using aliases
3614 if (alias_value != null){
3615 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3616 return new TypeExpr (t, loc);
3618 // we have alias value, but it isn't Type, so try if it's namespace
3619 return new SimpleName (alias_value, loc);
3623 // Stage 2: Lookup up if we are an alias to a type
3627 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3628 return new TypeExpr (t, loc);
3630 // No match, maybe our parent can compose us
3631 // into something meaningful.
3636 /// 7.5.2: Simple Names.
3638 /// Local Variables and Parameters are handled at
3639 /// parse time, so they never occur as SimpleNames.
3641 /// The `allow_static' flag is used by MemberAccess only
3642 /// and it is used to inform us that it is ok for us to
3643 /// avoid the static check, because MemberAccess might end
3644 /// up resolving the Name as a Type name and the access as
3645 /// a static type access.
3647 /// ie: Type Type; .... { Type.GetType (""); }
3649 /// Type is both an instance variable and a Type; Type.GetType
3650 /// is the static method not an instance method of type.
3652 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3654 Expression e = null;
3657 // Stage 1: Performed by the parser (binding to locals or parameters).
3659 Block current_block = ec.CurrentBlock;
3660 if (current_block != null){
3661 VariableInfo vi = current_block.GetVariableInfo (Name);
3665 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3667 if (right_side != null)
3668 return var.ResolveLValue (ec, right_side);
3670 return var.Resolve (ec);
3674 Parameter par = null;
3675 Parameters pars = current_block.Parameters;
3677 par = pars.GetParameterByName (Name, out idx);
3680 ParameterReference param;
3682 param = new ParameterReference (pars, idx, Name, loc);
3684 if (right_side != null)
3685 return param.ResolveLValue (ec, right_side);
3687 return param.Resolve (ec);
3692 // Stage 2: Lookup members
3695 DeclSpace lookup_ds = ec.DeclSpace;
3697 if (lookup_ds.TypeBuilder == null)
3700 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3704 lookup_ds =lookup_ds.Parent;
3705 } while (lookup_ds != null);
3707 if (e == null && ec.ContainerType != null)
3708 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3711 return ResolveAsTypeStep (ec);
3716 if (e is IMemberExpr) {
3717 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3721 IMemberExpr me = e as IMemberExpr;
3725 // This fails if ResolveMemberAccess() was unable to decide whether
3726 // it's a field or a type of the same name.
3727 if (!me.IsStatic && (me.InstanceExpression == null))
3731 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
3732 !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
3733 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3734 "outer type `" + me.DeclaringType + "' via nested type `" +
3735 me.InstanceExpression.Type + "'");
3739 if (right_side != null)
3740 e = e.DoResolveLValue (ec, right_side);
3742 e = e.DoResolve (ec);
3747 if (ec.IsStatic || ec.IsFieldInitializer){
3751 return MemberStaticCheck (ec, e);
3756 public override void Emit (EmitContext ec)
3759 // If this is ever reached, then we failed to
3760 // find the name as a namespace
3763 Error (103, "The name `" + Name +
3764 "' does not exist in the class `" +
3765 ec.DeclSpace.Name + "'");
3768 public override string ToString ()
3775 /// Fully resolved expression that evaluates to a type
3777 public class TypeExpr : Expression {
3778 public TypeExpr (Type t, Location l)
3781 eclass = ExprClass.Type;
3785 public override Expression ResolveAsTypeStep (EmitContext ec)
3790 override public Expression DoResolve (EmitContext ec)
3795 override public void Emit (EmitContext ec)
3797 throw new Exception ("Should never be called");
3800 public override string ToString ()
3802 return Type.ToString ();
3807 /// Used to create types from a fully qualified name. These are just used
3808 /// by the parser to setup the core types. A TypeLookupExpression is always
3809 /// classified as a type.
3811 public class TypeLookupExpression : TypeExpr {
3814 public TypeLookupExpression (string name) : base (null, Location.Null)
3819 public override Expression ResolveAsTypeStep (EmitContext ec)
3822 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3826 public override Expression DoResolve (EmitContext ec)
3828 return ResolveAsTypeStep (ec);
3831 public override void Emit (EmitContext ec)
3833 throw new Exception ("Should never be called");
3836 public override string ToString ()
3843 /// MethodGroup Expression.
3845 /// This is a fully resolved expression that evaluates to a type
3847 public class MethodGroupExpr : Expression, IMemberExpr {
3848 public MethodBase [] Methods;
3849 Expression instance_expression = null;
3850 bool is_explicit_impl = false;
3852 public MethodGroupExpr (MemberInfo [] mi, Location l)
3854 Methods = new MethodBase [mi.Length];
3855 mi.CopyTo (Methods, 0);
3856 eclass = ExprClass.MethodGroup;
3857 type = TypeManager.object_type;
3861 public MethodGroupExpr (ArrayList list, Location l)
3863 Methods = new MethodBase [list.Count];
3866 list.CopyTo (Methods, 0);
3868 foreach (MemberInfo m in list){
3869 if (!(m is MethodBase)){
3870 Console.WriteLine ("Name " + m.Name);
3871 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3877 eclass = ExprClass.MethodGroup;
3878 type = TypeManager.object_type;
3881 public Type DeclaringType {
3883 return Methods [0].DeclaringType;
3888 // `A method group may have associated an instance expression'
3890 public Expression InstanceExpression {
3892 return instance_expression;
3896 instance_expression = value;
3900 public bool IsExplicitImpl {
3902 return is_explicit_impl;
3906 is_explicit_impl = value;
3910 public string Name {
3912 return Methods [0].Name;
3916 public bool IsInstance {
3918 foreach (MethodBase mb in Methods)
3926 public bool IsStatic {
3928 foreach (MethodBase mb in Methods)
3936 override public Expression DoResolve (EmitContext ec)
3938 if (instance_expression != null) {
3939 instance_expression = instance_expression.DoResolve (ec);
3940 if (instance_expression == null)
3947 public void ReportUsageError ()
3949 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3950 Methods [0].Name + "()' is referenced without parentheses");
3953 override public void Emit (EmitContext ec)
3955 ReportUsageError ();
3958 bool RemoveMethods (bool keep_static)
3960 ArrayList smethods = new ArrayList ();
3962 foreach (MethodBase mb in Methods){
3963 if (mb.IsStatic == keep_static)
3967 if (smethods.Count == 0)
3970 Methods = new MethodBase [smethods.Count];
3971 smethods.CopyTo (Methods, 0);
3977 /// Removes any instance methods from the MethodGroup, returns
3978 /// false if the resulting set is empty.
3980 public bool RemoveInstanceMethods ()
3982 return RemoveMethods (true);
3986 /// Removes any static methods from the MethodGroup, returns
3987 /// false if the resulting set is empty.
3989 public bool RemoveStaticMethods ()
3991 return RemoveMethods (false);
3996 /// Fully resolved expression that evaluates to a Field
3998 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3999 public readonly FieldInfo FieldInfo;
4000 Expression instance_expr;
4002 public FieldExpr (FieldInfo fi, Location l)
4005 eclass = ExprClass.Variable;
4006 type = fi.FieldType;
4010 public string Name {
4012 return FieldInfo.Name;
4016 public bool IsInstance {
4018 return !FieldInfo.IsStatic;
4022 public bool IsStatic {
4024 return FieldInfo.IsStatic;
4028 public Type DeclaringType {
4030 return FieldInfo.DeclaringType;
4034 public Expression InstanceExpression {
4036 return instance_expr;
4040 instance_expr = value;
4044 override public Expression DoResolve (EmitContext ec)
4046 if (!FieldInfo.IsStatic){
4047 if (instance_expr == null){
4049 // This can happen when referencing an instance field using
4050 // a fully qualified type expression: TypeName.InstanceField = xxx
4052 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
4056 // Resolve the field's instance expression while flow analysis is turned
4057 // off: when accessing a field "a.b", we must check whether the field
4058 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4059 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4060 ResolveFlags.DisableFlowAnalysis);
4061 if (instance_expr == null)
4065 // If the instance expression is a local variable or parameter.
4066 IVariable var = instance_expr as IVariable;
4067 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4073 void Report_AssignToReadonly (bool is_instance)
4078 msg = "Readonly field can not be assigned outside " +
4079 "of constructor or variable initializer";
4081 msg = "A static readonly field can only be assigned in " +
4082 "a static constructor";
4084 Report.Error (is_instance ? 191 : 198, loc, msg);
4087 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4089 IVariable var = instance_expr as IVariable;
4091 var.SetFieldAssigned (ec, FieldInfo.Name);
4093 Expression e = DoResolve (ec);
4098 if (!FieldInfo.IsInitOnly)
4102 // InitOnly fields can only be assigned in constructors
4105 if (ec.IsConstructor){
4106 if (ec.ContainerType == FieldInfo.DeclaringType)
4110 Report_AssignToReadonly (true);
4115 override public void Emit (EmitContext ec)
4117 ILGenerator ig = ec.ig;
4118 bool is_volatile = false;
4120 if (FieldInfo is FieldBuilder){
4121 FieldBase f = TypeManager.GetField (FieldInfo);
4123 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4126 f.status |= Field.Status.USED;
4129 if (FieldInfo.IsStatic){
4131 ig.Emit (OpCodes.Volatile);
4133 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4137 if (instance_expr.Type.IsValueType){
4139 LocalTemporary tempo = null;
4141 if (!(instance_expr is IMemoryLocation)){
4142 tempo = new LocalTemporary (ec, instance_expr.Type);
4144 if (ec.RemapToProxy)
4147 InstanceExpression.Emit (ec);
4151 ml = (IMemoryLocation) instance_expr;
4153 ml.AddressOf (ec, AddressOp.Load);
4155 if (ec.RemapToProxy)
4158 instance_expr.Emit (ec);
4161 ig.Emit (OpCodes.Volatile);
4163 ig.Emit (OpCodes.Ldfld, FieldInfo);
4166 public void EmitAssign (EmitContext ec, Expression source)
4168 FieldAttributes fa = FieldInfo.Attributes;
4169 bool is_static = (fa & FieldAttributes.Static) != 0;
4170 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4171 ILGenerator ig = ec.ig;
4173 if (is_readonly && !ec.IsConstructor){
4174 Report_AssignToReadonly (!is_static);
4179 Expression instance = instance_expr;
4181 if (instance.Type.IsValueType){
4182 if (instance is IMemoryLocation){
4183 IMemoryLocation ml = (IMemoryLocation) instance;
4185 ml.AddressOf (ec, AddressOp.Store);
4187 throw new Exception ("The " + instance + " of type " +
4189 " represents a ValueType and does " +
4190 "not implement IMemoryLocation");
4192 if (ec.RemapToProxy)
4201 if (FieldInfo is FieldBuilder){
4202 FieldBase f = TypeManager.GetField (FieldInfo);
4204 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4205 ig.Emit (OpCodes.Volatile);
4207 f.status |= Field.Status.ASSIGNED;
4211 ig.Emit (OpCodes.Stsfld, FieldInfo);
4213 ig.Emit (OpCodes.Stfld, FieldInfo);
4216 public void AddressOf (EmitContext ec, AddressOp mode)
4218 ILGenerator ig = ec.ig;
4220 if (FieldInfo is FieldBuilder){
4221 FieldBase f = TypeManager.GetField (FieldInfo);
4222 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
4223 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
4227 if ((mode & AddressOp.Store) != 0)
4228 f.status |= Field.Status.ASSIGNED;
4229 if ((mode & AddressOp.Load) != 0)
4230 f.status |= Field.Status.USED;
4234 // Handle initonly fields specially: make a copy and then
4235 // get the address of the copy.
4237 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4241 local = ig.DeclareLocal (type);
4242 ig.Emit (OpCodes.Stloc, local);
4243 ig.Emit (OpCodes.Ldloca, local);
4247 if (FieldInfo.IsStatic)
4248 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4251 // In the case of `This', we call the AddressOf method, which will
4252 // only load the pointer, and not perform an Ldobj immediately after
4253 // the value has been loaded into the stack.
4255 if (instance_expr is This)
4256 ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4257 else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
4258 IMemoryLocation ml = (IMemoryLocation) instance_expr;
4260 ml.AddressOf (ec, AddressOp.LoadStore);
4262 instance_expr.Emit (ec);
4263 ig.Emit (OpCodes.Ldflda, FieldInfo);
4269 /// Expression that evaluates to a Property. The Assign class
4270 /// might set the `Value' expression if we are in an assignment.
4272 /// This is not an LValue because we need to re-write the expression, we
4273 /// can not take data from the stack and store it.
4275 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4276 public readonly PropertyInfo PropertyInfo;
4279 // This is set externally by the `BaseAccess' class
4282 MethodInfo getter, setter;
4284 bool must_do_cs1540_check;
4286 Expression instance_expr;
4288 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4291 eclass = ExprClass.PropertyAccess;
4295 type = TypeManager.TypeToCoreType (pi.PropertyType);
4297 ResolveAccessors (ec);
4300 public string Name {
4302 return PropertyInfo.Name;
4306 public bool IsInstance {
4312 public bool IsStatic {
4318 public Type DeclaringType {
4320 return PropertyInfo.DeclaringType;
4325 // The instance expression associated with this expression
4327 public Expression InstanceExpression {
4329 instance_expr = value;
4333 return instance_expr;
4337 public bool VerifyAssignable ()
4339 if (setter == null) {
4340 Report.Error (200, loc,
4341 "The property `" + PropertyInfo.Name +
4342 "' can not be assigned to, as it has not set accessor");
4349 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
4351 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
4352 BindingFlags.Static | BindingFlags.Instance;
4355 group = TypeManager.MemberLookup (
4356 invocation_type, invocation_type, PropertyInfo.DeclaringType,
4357 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
4360 // The first method is the closest to us
4365 foreach (MethodInfo mi in group) {
4366 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
4369 // If only accessible to the current class or children
4371 if (ma == MethodAttributes.Private) {
4372 Type declaring_type = mi.DeclaringType;
4374 if (invocation_type != declaring_type){
4375 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4383 // FamAndAssem requires that we not only derivate, but we are on the
4386 if (ma == MethodAttributes.FamANDAssem){
4387 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4393 // Assembly and FamORAssem succeed if we're in the same assembly.
4394 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
4395 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4401 // We already know that we aren't in the same assembly.
4402 if (ma == MethodAttributes.Assembly)
4405 // Family and FamANDAssem require that we derive.
4406 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
4407 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4410 must_do_cs1540_check = true;
4423 // We also perform the permission checking here, as the PropertyInfo does not
4424 // hold the information for the accessibility of its setter/getter
4426 void ResolveAccessors (EmitContext ec)
4428 getter = GetAccessor (ec.ContainerType, "get");
4429 if ((getter != null) && getter.IsStatic)
4432 setter = GetAccessor (ec.ContainerType, "set");
4433 if ((setter != null) && setter.IsStatic)
4436 if (setter == null && getter == null){
4437 Error (122, "`" + PropertyInfo.Name + "' " +
4438 "is inaccessible because of its protection level");
4443 bool InstanceResolve (EmitContext ec)
4445 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4446 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4450 if (instance_expr != null) {
4451 instance_expr = instance_expr.DoResolve (ec);
4452 if (instance_expr == null)
4456 if (must_do_cs1540_check && (instance_expr != null)) {
4457 if ((instance_expr.Type != ec.ContainerType) &&
4458 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
4459 Report.Error (1540, loc, "Cannot access protected member `" +
4460 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
4461 "' via a qualifier of type `" +
4462 TypeManager.CSharpName (instance_expr.Type) +
4463 "'; the qualifier must be of type `" +
4464 TypeManager.CSharpName (ec.ContainerType) +
4465 "' (or derived from it)");
4473 override public Expression DoResolve (EmitContext ec)
4475 if (getter == null){
4477 // The following condition happens if the PropertyExpr was
4478 // created, but is invalid (ie, the property is inaccessible),
4479 // and we did not want to embed the knowledge about this in
4480 // the caller routine. This only avoids double error reporting.
4485 Report.Error (154, loc,
4486 "The property `" + PropertyInfo.Name +
4487 "' can not be used in " +
4488 "this context because it lacks a get accessor");
4492 if (!InstanceResolve (ec))
4496 // Only base will allow this invocation to happen.
4498 if (IsBase && getter.IsAbstract){
4499 Report.Error (205, loc, "Cannot call an abstract base property: " +
4500 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4507 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4509 if (setter == null){
4511 // The following condition happens if the PropertyExpr was
4512 // created, but is invalid (ie, the property is inaccessible),
4513 // and we did not want to embed the knowledge about this in
4514 // the caller routine. This only avoids double error reporting.
4519 Report.Error (154, loc,
4520 "The property `" + PropertyInfo.Name +
4521 "' can not be used in " +
4522 "this context because it lacks a set accessor");
4526 if (!InstanceResolve (ec))
4530 // Only base will allow this invocation to happen.
4532 if (IsBase && setter.IsAbstract){
4533 Report.Error (205, loc, "Cannot call an abstract base property: " +
4534 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4540 override public void Emit (EmitContext ec)
4543 // Special case: length of single dimension array property is turned into ldlen
4545 if ((getter == TypeManager.system_int_array_get_length) ||
4546 (getter == TypeManager.int_array_get_length)){
4547 Type iet = instance_expr.Type;
4550 // System.Array.Length can be called, but the Type does not
4551 // support invoking GetArrayRank, so test for that case first
4553 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4554 instance_expr.Emit (ec);
4555 ec.ig.Emit (OpCodes.Ldlen);
4560 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
4565 // Implements the IAssignMethod interface for assignments
4567 public void EmitAssign (EmitContext ec, Expression source)
4569 Argument arg = new Argument (source, Argument.AType.Expression);
4570 ArrayList args = new ArrayList ();
4573 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
4576 override public void EmitStatement (EmitContext ec)
4579 ec.ig.Emit (OpCodes.Pop);
4584 /// Fully resolved expression that evaluates to an Event
4586 public class EventExpr : Expression, IMemberExpr {
4587 public readonly EventInfo EventInfo;
4588 public Expression instance_expr;
4591 MethodInfo add_accessor, remove_accessor;
4593 public EventExpr (EventInfo ei, Location loc)
4597 eclass = ExprClass.EventAccess;
4599 add_accessor = TypeManager.GetAddMethod (ei);
4600 remove_accessor = TypeManager.GetRemoveMethod (ei);
4602 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4605 if (EventInfo is MyEventBuilder){
4606 MyEventBuilder eb = (MyEventBuilder) EventInfo;
4607 type = eb.EventType;
4610 type = EventInfo.EventHandlerType;
4613 public string Name {
4615 return EventInfo.Name;
4619 public bool IsInstance {
4625 public bool IsStatic {
4631 public Type DeclaringType {
4633 return EventInfo.DeclaringType;
4637 public Expression InstanceExpression {
4639 return instance_expr;
4643 instance_expr = value;
4647 public override Expression DoResolve (EmitContext ec)
4649 if (instance_expr != null) {
4650 instance_expr = instance_expr.DoResolve (ec);
4651 if (instance_expr == null)
4659 public override void Emit (EmitContext ec)
4661 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4664 public void EmitAddOrRemove (EmitContext ec, Expression source)
4666 Expression handler = ((Binary) source).Right;
4668 Argument arg = new Argument (handler, Argument.AType.Expression);
4669 ArrayList args = new ArrayList ();
4673 if (((Binary) source).Oper == Binary.Operator.Addition)
4674 Invocation.EmitCall (
4675 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4677 Invocation.EmitCall (
4678 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);