2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
12 namespace Mono.CSharp {
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// This is just a helper class, it is generated by Unary, UnaryMutator
21 /// when an overloaded method has been found. It just emits the code for a
24 public class StaticCallExpr : ExpressionStatement {
28 StaticCallExpr (MethodInfo m, ArrayList a)
34 eclass = ExprClass.Value;
37 public override Expression DoResolve (EmitContext ec)
40 // We are born fully resolved
45 public override void Emit (EmitContext ec)
48 Invocation.EmitArguments (ec, mi, args);
50 ec.ig.Emit (OpCodes.Call, mi);
54 static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
55 Expression e, Location loc)
60 args = new ArrayList (1);
61 args.Add (new Argument (e, Argument.AType.Expression));
62 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
67 return new StaticCallExpr ((MethodInfo) method, args);
70 public override void EmitStatement (EmitContext ec)
73 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
74 ec.ig.Emit (OpCodes.Pop);
79 /// Unary expressions.
83 /// Unary implements unary expressions. It derives from
84 /// ExpressionStatement becuase the pre/post increment/decrement
85 /// operators can be used in a statement context.
87 public class Unary : Expression {
88 public enum Operator : byte {
89 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
90 Indirection, AddressOf, TOP
94 public Expression Expr;
97 public Unary (Operator op, Expression expr, Location loc)
105 /// Returns a stringified representation of the Operator
107 static public string OperName (Operator oper)
110 case Operator.UnaryPlus:
112 case Operator.UnaryNegation:
114 case Operator.LogicalNot:
116 case Operator.OnesComplement:
118 case Operator.AddressOf:
120 case Operator.Indirection:
124 return oper.ToString ();
127 static string [] oper_names;
131 oper_names = new string [(int)Operator.TOP];
133 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
134 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
135 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
136 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
137 oper_names [(int) Operator.Indirection] = "op_Indirection";
138 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
141 void Error23 (Type t)
144 23, loc, "Operator " + OperName (Oper) +
145 " cannot be applied to operand of type `" +
146 TypeManager.CSharpName (t) + "'");
150 /// The result has been already resolved:
152 /// FIXME: a minus constant -128 sbyte cant be turned into a
155 static Expression TryReduceNegative (Expression expr)
159 if (expr is IntConstant)
160 e = new IntConstant (-((IntConstant) expr).Value);
161 else if (expr is UIntConstant){
162 uint value = ((UIntConstant) expr).Value;
164 if (value < 2147483649)
165 return new IntConstant (-(int)value);
167 e = new LongConstant (value);
169 else if (expr is LongConstant)
170 e = new LongConstant (-((LongConstant) expr).Value);
171 else if (expr is ULongConstant){
172 ulong value = ((ULongConstant) expr).Value;
174 if (value < 9223372036854775809)
175 return new LongConstant(-(long)value);
177 else if (expr is FloatConstant)
178 e = new FloatConstant (-((FloatConstant) expr).Value);
179 else if (expr is DoubleConstant)
180 e = new DoubleConstant (-((DoubleConstant) expr).Value);
181 else if (expr is DecimalConstant)
182 e = new DecimalConstant (-((DecimalConstant) expr).Value);
183 else if (expr is ShortConstant)
184 e = new IntConstant (-((ShortConstant) expr).Value);
185 else if (expr is UShortConstant)
186 e = new IntConstant (-((UShortConstant) expr).Value);
190 Expression Reduce (EmitContext ec, Expression e)
192 Type expr_type = e.Type;
195 case Operator.UnaryPlus:
198 case Operator.UnaryNegation:
199 return TryReduceNegative (e);
201 case Operator.LogicalNot:
202 if (expr_type != TypeManager.bool_type) {
207 BoolConstant b = (BoolConstant) e;
208 return new BoolConstant (!(b.Value));
210 case Operator.OnesComplement:
211 if (!((expr_type == TypeManager.int32_type) ||
212 (expr_type == TypeManager.uint32_type) ||
213 (expr_type == TypeManager.int64_type) ||
214 (expr_type == TypeManager.uint64_type) ||
215 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
220 if (e is EnumConstant){
221 EnumConstant enum_constant = (EnumConstant) e;
223 Expression reduced = Reduce (ec, enum_constant.Child);
225 return new EnumConstant ((Constant) reduced, enum_constant.Type);
228 if (expr_type == TypeManager.int32_type)
229 return new IntConstant (~ ((IntConstant) e).Value);
230 if (expr_type == TypeManager.uint32_type)
231 return new UIntConstant (~ ((UIntConstant) e).Value);
232 if (expr_type == TypeManager.int64_type)
233 return new LongConstant (~ ((LongConstant) e).Value);
234 if (expr_type == TypeManager.uint64_type)
235 return new ULongConstant (~ ((ULongConstant) e).Value);
240 throw new Exception ("Can not constant fold");
243 Expression ResolveOperator (EmitContext ec)
245 Type expr_type = Expr.Type;
248 // Step 1: Perform Operator Overload location
253 op_name = oper_names [(int) Oper];
255 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
258 Expression e = StaticCallExpr.MakeSimpleCall (
259 ec, (MethodGroupExpr) mg, Expr, loc);
269 // Only perform numeric promotions on:
272 if (expr_type == null)
276 // Step 2: Default operations on CLI native types.
278 if (Expr is Constant)
279 return Reduce (ec, Expr);
281 if (Oper == Operator.LogicalNot){
282 if (expr_type != TypeManager.bool_type) {
287 type = TypeManager.bool_type;
291 if (Oper == Operator.OnesComplement) {
292 if (!((expr_type == TypeManager.int32_type) ||
293 (expr_type == TypeManager.uint32_type) ||
294 (expr_type == TypeManager.int64_type) ||
295 (expr_type == TypeManager.uint64_type) ||
296 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
299 e = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc);
301 type = TypeManager.int32_type;
304 e = ConvertImplicit (ec, Expr, TypeManager.uint32_type, loc);
306 type = TypeManager.uint32_type;
309 e = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc);
311 type = TypeManager.int64_type;
314 e = ConvertImplicit (ec, Expr, TypeManager.uint64_type, loc);
316 type = TypeManager.uint64_type;
326 if (Oper == Operator.UnaryPlus) {
328 // A plus in front of something is just a no-op, so return the child.
334 // Deals with -literals
335 // int operator- (int x)
336 // long operator- (long x)
337 // float operator- (float f)
338 // double operator- (double d)
339 // decimal operator- (decimal d)
341 if (Oper == Operator.UnaryNegation){
345 // transform - - expr into expr
348 Unary unary = (Unary) Expr;
350 if (unary.Oper == Operator.UnaryNegation)
355 // perform numeric promotions to int,
359 // The following is inneficient, because we call
360 // ConvertImplicit too many times.
362 // It is also not clear if we should convert to Float
363 // or Double initially.
365 if (expr_type == TypeManager.uint32_type){
367 // FIXME: handle exception to this rule that
368 // permits the int value -2147483648 (-2^31) to
369 // bt wrote as a decimal interger literal
371 type = TypeManager.int64_type;
372 Expr = ConvertImplicit (ec, Expr, type, loc);
376 if (expr_type == TypeManager.uint64_type){
378 // FIXME: Handle exception of `long value'
379 // -92233720368547758087 (-2^63) to be wrote as
380 // decimal integer literal.
386 if (expr_type == TypeManager.float_type){
391 e = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc);
398 e = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc);
405 e = ConvertImplicit (ec, Expr, TypeManager.double_type, loc);
416 if (Oper == Operator.AddressOf){
417 if (Expr.eclass != ExprClass.Variable){
418 Error (211, loc, "Cannot take the address of non-variables");
427 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
431 string ptr_type_name = Expr.Type.FullName + "*";
432 type = TypeManager.LookupType (ptr_type_name);
437 if (Oper == Operator.Indirection){
443 if (!expr_type.IsPointer){
446 "The * or -> operator can only be applied to pointers");
451 // We create an Indirection expression, because
452 // it can implement the IMemoryLocation.
454 return new Indirection (Expr);
457 Error (187, loc, "No such operator '" + OperName (Oper) + "' defined for type '" +
458 TypeManager.CSharpName (expr_type) + "'");
462 public override Expression DoResolve (EmitContext ec)
464 Expr = Expr.Resolve (ec);
469 eclass = ExprClass.Value;
470 return ResolveOperator (ec);
473 public override void Emit (EmitContext ec)
475 ILGenerator ig = ec.ig;
476 Type expr_type = Expr.Type;
479 case Operator.UnaryPlus:
480 throw new Exception ("This should be caught by Resolve");
482 case Operator.UnaryNegation:
484 ig.Emit (OpCodes.Neg);
487 case Operator.LogicalNot:
489 ig.Emit (OpCodes.Ldc_I4_0);
490 ig.Emit (OpCodes.Ceq);
493 case Operator.OnesComplement:
495 ig.Emit (OpCodes.Not);
498 case Operator.AddressOf:
499 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
503 throw new Exception ("This should not happen: Operator = "
509 /// This will emit the child expression for `ec' avoiding the logical
510 /// not. The parent will take care of changing brfalse/brtrue
512 public void EmitLogicalNot (EmitContext ec)
514 if (Oper != Operator.LogicalNot)
515 throw new Exception ("EmitLogicalNot can only be called with !expr");
520 public override string ToString ()
522 return "Unary (" + Oper + ", " + Expr + ")";
528 // Unary operators are turned into Indirection expressions
529 // after semantic analysis (this is so we can take the address
530 // of an indirection).
532 public class Indirection : Expression, IMemoryLocation, IAssignMethod {
534 LocalTemporary temporary;
537 public Indirection (Expression expr)
540 this.type = TypeManager.TypeToCoreType (expr.Type.GetElementType ());
541 eclass = ExprClass.Variable;
544 void LoadExprValue (EmitContext ec)
548 public override void Emit (EmitContext ec)
550 ILGenerator ig = ec.ig;
552 if (temporary != null){
558 ec.ig.Emit (OpCodes.Dup);
559 temporary.Store (ec);
560 have_temporary = true;
564 LoadFromPtr (ig, Type);
567 public void EmitAssign (EmitContext ec, Expression source)
569 if (temporary != null){
575 ec.ig.Emit (OpCodes.Dup);
576 temporary.Store (ec);
577 have_temporary = true;
582 StoreFromPtr (ec.ig, type);
585 public void AddressOf (EmitContext ec, AddressOp Mode)
587 if (temporary != null){
593 ec.ig.Emit (OpCodes.Dup);
594 temporary.Store (ec);
595 have_temporary = true;
600 public override Expression DoResolve (EmitContext ec)
603 // Born fully resolved
608 public new void CacheTemporaries (EmitContext ec)
610 temporary = new LocalTemporary (ec, type);
615 /// Unary Mutator expressions (pre and post ++ and --)
619 /// UnaryMutator implements ++ and -- expressions. It derives from
620 /// ExpressionStatement becuase the pre/post increment/decrement
621 /// operators can be used in a statement context.
623 /// FIXME: Idea, we could split this up in two classes, one simpler
624 /// for the common case, and one with the extra fields for more complex
625 /// classes (indexers require temporary access; overloaded require method)
627 /// Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
628 /// PostDecrement, that way we could save the `Mode' byte as well.
630 public class UnaryMutator : ExpressionStatement {
631 public enum Mode : byte {
632 PreIncrement, PreDecrement, PostIncrement, PostDecrement
638 LocalTemporary temp_storage;
641 // This is expensive for the simplest case.
645 public UnaryMutator (Mode m, Expression e, Location l)
652 static string OperName (Mode mode)
654 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
658 void Error23 (Type t)
661 23, loc, "Operator " + OperName (mode) +
662 " cannot be applied to operand of type `" +
663 TypeManager.CSharpName (t) + "'");
667 /// Returns whether an object of type `t' can be incremented
668 /// or decremented with add/sub (ie, basically whether we can
669 /// use pre-post incr-decr operations on it, but it is not a
670 /// System.Decimal, which we require operator overloading to catch)
672 static bool IsIncrementableNumber (Type t)
674 return (t == TypeManager.sbyte_type) ||
675 (t == TypeManager.byte_type) ||
676 (t == TypeManager.short_type) ||
677 (t == TypeManager.ushort_type) ||
678 (t == TypeManager.int32_type) ||
679 (t == TypeManager.uint32_type) ||
680 (t == TypeManager.int64_type) ||
681 (t == TypeManager.uint64_type) ||
682 (t == TypeManager.char_type) ||
683 (t.IsSubclassOf (TypeManager.enum_type)) ||
684 (t == TypeManager.float_type) ||
685 (t == TypeManager.double_type) ||
686 (t.IsPointer && t != TypeManager.void_ptr_type);
689 Expression ResolveOperator (EmitContext ec)
691 Type expr_type = expr.Type;
694 // Step 1: Perform Operator Overload location
699 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
700 op_name = "op_Increment";
702 op_name = "op_Decrement";
704 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
706 if (mg == null && expr_type.BaseType != null)
707 mg = MemberLookup (ec, expr_type.BaseType, op_name,
708 MemberTypes.Method, AllBindingFlags, loc);
711 method = StaticCallExpr.MakeSimpleCall (
712 ec, (MethodGroupExpr) mg, expr, loc);
719 // The operand of the prefix/postfix increment decrement operators
720 // should be an expression that is classified as a variable,
721 // a property access or an indexer access
724 if (expr.eclass == ExprClass.Variable){
725 if (IsIncrementableNumber (expr_type) ||
726 expr_type == TypeManager.decimal_type){
729 } else if (expr.eclass == ExprClass.IndexerAccess){
730 IndexerAccess ia = (IndexerAccess) expr;
732 temp_storage = new LocalTemporary (ec, expr.Type);
734 expr = ia.ResolveLValue (ec, temp_storage);
739 } else if (expr.eclass == ExprClass.PropertyAccess){
740 PropertyExpr pe = (PropertyExpr) expr;
742 if (pe.VerifyAssignable ())
747 report118 (loc, expr, "variable, indexer or property access");
751 Error (187, loc, "No such operator '" + OperName (mode) + "' defined for type '" +
752 TypeManager.CSharpName (expr_type) + "'");
756 public override Expression DoResolve (EmitContext ec)
758 expr = expr.Resolve (ec);
763 eclass = ExprClass.Value;
764 return ResolveOperator (ec);
767 static int PtrTypeSize (Type t)
769 return GetTypeSize (t.GetElementType ());
773 // Loads the proper "1" into the stack based on the type
775 static void LoadOne (ILGenerator ig, Type t)
777 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
778 ig.Emit (OpCodes.Ldc_I8, 1L);
779 else if (t == TypeManager.double_type)
780 ig.Emit (OpCodes.Ldc_R8, 1.0);
781 else if (t == TypeManager.float_type)
782 ig.Emit (OpCodes.Ldc_R4, 1.0F);
783 else if (t.IsPointer){
784 int n = PtrTypeSize (t);
787 ig.Emit (OpCodes.Sizeof, t);
789 IntConstant.EmitInt (ig, n);
791 ig.Emit (OpCodes.Ldc_I4_1);
796 // FIXME: We need some way of avoiding the use of temp_storage
797 // for some types of storage (parameters, local variables,
798 // static fields) and single-dimension array access.
800 void EmitCode (EmitContext ec, bool is_expr)
802 ILGenerator ig = ec.ig;
803 IAssignMethod ia = (IAssignMethod) expr;
804 Type expr_type = expr.Type;
806 if (temp_storage == null)
807 temp_storage = new LocalTemporary (ec, expr_type);
809 ia.CacheTemporaries (ec);
810 ig.Emit (OpCodes.Nop);
812 case Mode.PreIncrement:
813 case Mode.PreDecrement:
817 LoadOne (ig, expr_type);
820 // Select the opcode based on the check state (then the type)
821 // and the actual operation
824 if (expr_type == TypeManager.int32_type ||
825 expr_type == TypeManager.int64_type){
826 if (mode == Mode.PreDecrement)
827 ig.Emit (OpCodes.Sub_Ovf);
829 ig.Emit (OpCodes.Add_Ovf);
830 } else if (expr_type == TypeManager.uint32_type ||
831 expr_type == TypeManager.uint64_type){
832 if (mode == Mode.PreDecrement)
833 ig.Emit (OpCodes.Sub_Ovf_Un);
835 ig.Emit (OpCodes.Add_Ovf_Un);
837 if (mode == Mode.PreDecrement)
838 ig.Emit (OpCodes.Sub_Ovf);
840 ig.Emit (OpCodes.Add_Ovf);
843 if (mode == Mode.PreDecrement)
844 ig.Emit (OpCodes.Sub);
846 ig.Emit (OpCodes.Add);
851 temp_storage.Store (ec);
852 ia.EmitAssign (ec, temp_storage);
854 temp_storage.Emit (ec);
857 case Mode.PostIncrement:
858 case Mode.PostDecrement:
866 ig.Emit (OpCodes.Dup);
868 LoadOne (ig, expr_type);
871 if (expr_type == TypeManager.int32_type ||
872 expr_type == TypeManager.int64_type){
873 if (mode == Mode.PostDecrement)
874 ig.Emit (OpCodes.Sub_Ovf);
876 ig.Emit (OpCodes.Add_Ovf);
877 } else if (expr_type == TypeManager.uint32_type ||
878 expr_type == TypeManager.uint64_type){
879 if (mode == Mode.PostDecrement)
880 ig.Emit (OpCodes.Sub_Ovf_Un);
882 ig.Emit (OpCodes.Add_Ovf_Un);
884 if (mode == Mode.PostDecrement)
885 ig.Emit (OpCodes.Sub_Ovf);
887 ig.Emit (OpCodes.Add_Ovf);
890 if (mode == Mode.PostDecrement)
891 ig.Emit (OpCodes.Sub);
893 ig.Emit (OpCodes.Add);
899 temp_storage.Store (ec);
900 ia.EmitAssign (ec, temp_storage);
905 public override void Emit (EmitContext ec)
911 public override void EmitStatement (EmitContext ec)
913 EmitCode (ec, false);
919 /// Base class for the `Is' and `As' classes.
923 /// FIXME: Split this in two, and we get to save the `Operator' Oper
926 public abstract class Probe : Expression {
927 public readonly Expression ProbeType;
928 protected Expression expr;
929 protected Type probe_type;
930 protected Location loc;
932 public Probe (Expression expr, Expression probe_type, Location l)
934 ProbeType = probe_type;
939 public Expression Expr {
945 public override Expression DoResolve (EmitContext ec)
947 probe_type = ec.DeclSpace.ResolveType (ProbeType, false, loc);
949 if (probe_type == null)
952 expr = expr.Resolve (ec);
959 /// Implementation of the `is' operator.
961 public class Is : Probe {
962 public Is (Expression expr, Expression probe_type, Location l)
963 : base (expr, probe_type, l)
968 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
973 public override void Emit (EmitContext ec)
975 ILGenerator ig = ec.ig;
980 case Action.AlwaysFalse:
981 ig.Emit (OpCodes.Pop);
982 IntConstant.EmitInt (ig, 0);
984 case Action.AlwaysTrue:
985 ig.Emit (OpCodes.Pop);
986 ig.Emit (OpCodes.Nop);
987 IntConstant.EmitInt (ig, 1);
989 case Action.LeaveOnStack:
990 // the `e != null' rule.
993 ig.Emit (OpCodes.Isinst, probe_type);
994 ig.Emit (OpCodes.Ldnull);
995 ig.Emit (OpCodes.Cgt_Un);
998 throw new Exception ("never reached");
1001 public override Expression DoResolve (EmitContext ec)
1003 Expression e = base.DoResolve (ec);
1008 Type etype = expr.Type;
1009 bool warning_always_matches = false;
1010 bool warning_never_matches = false;
1012 type = TypeManager.bool_type;
1013 eclass = ExprClass.Value;
1016 // First case, if at compile time, there is an implicit conversion
1017 // then e != null (objects) or true (value types)
1019 e = ConvertImplicitStandard (ec, expr, probe_type, loc);
1022 if (etype.IsValueType)
1023 action = Action.AlwaysTrue;
1025 action = Action.LeaveOnStack;
1027 warning_always_matches = true;
1028 } else if (ExplicitReferenceConversionExists (etype, probe_type)){
1030 // Second case: explicit reference convresion
1032 if (expr is NullLiteral)
1033 action = Action.AlwaysFalse;
1035 action = Action.Probe;
1037 action = Action.AlwaysFalse;
1038 warning_never_matches = true;
1041 if (RootContext.WarningLevel >= 1){
1042 if (warning_always_matches)
1045 "The expression is always of type `" +
1046 TypeManager.CSharpName (probe_type) + "'");
1047 else if (warning_never_matches){
1048 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1051 "The expression is never of type `" +
1052 TypeManager.CSharpName (probe_type) + "'");
1061 /// Implementation of the `as' operator.
1063 public class As : Probe {
1064 public As (Expression expr, Expression probe_type, Location l)
1065 : base (expr, probe_type, l)
1069 bool do_isinst = false;
1071 public override void Emit (EmitContext ec)
1073 ILGenerator ig = ec.ig;
1078 ig.Emit (OpCodes.Isinst, probe_type);
1081 static void Error_CannotConvertType (Type source, Type target, Location loc)
1084 39, loc, "as operator can not convert from `" +
1085 TypeManager.CSharpName (source) + "' to `" +
1086 TypeManager.CSharpName (target) + "'");
1089 public override Expression DoResolve (EmitContext ec)
1091 Expression e = base.DoResolve (ec);
1097 eclass = ExprClass.Value;
1098 Type etype = expr.Type;
1100 e = ConvertImplicit (ec, expr, probe_type, loc);
1107 if (ExplicitReferenceConversionExists (etype, probe_type)){
1112 Error_CannotConvertType (etype, probe_type, loc);
1118 /// This represents a typecast in the source language.
1120 /// FIXME: Cast expressions have an unusual set of parsing
1121 /// rules, we need to figure those out.
1123 public class Cast : Expression {
1124 Expression target_type;
1128 public Cast (Expression cast_type, Expression expr, Location loc)
1130 this.target_type = cast_type;
1135 public Expression TargetType {
1141 public Expression Expr {
1151 /// Attempts to do a compile-time folding of a constant cast.
1153 Expression TryReduce (EmitContext ec, Type target_type)
1155 if (expr is ByteConstant){
1156 byte v = ((ByteConstant) expr).Value;
1158 if (target_type == TypeManager.sbyte_type)
1159 return new SByteConstant ((sbyte) v);
1160 if (target_type == TypeManager.short_type)
1161 return new ShortConstant ((short) v);
1162 if (target_type == TypeManager.ushort_type)
1163 return new UShortConstant ((ushort) v);
1164 if (target_type == TypeManager.int32_type)
1165 return new IntConstant ((int) v);
1166 if (target_type == TypeManager.uint32_type)
1167 return new UIntConstant ((uint) v);
1168 if (target_type == TypeManager.int64_type)
1169 return new LongConstant ((long) v);
1170 if (target_type == TypeManager.uint64_type)
1171 return new ULongConstant ((ulong) v);
1172 if (target_type == TypeManager.float_type)
1173 return new FloatConstant ((float) v);
1174 if (target_type == TypeManager.double_type)
1175 return new DoubleConstant ((double) v);
1176 if (target_type == TypeManager.char_type)
1177 return new CharConstant ((char) v);
1179 if (expr is SByteConstant){
1180 sbyte v = ((SByteConstant) expr).Value;
1182 if (target_type == TypeManager.byte_type)
1183 return new ByteConstant ((byte) v);
1184 if (target_type == TypeManager.short_type)
1185 return new ShortConstant ((short) v);
1186 if (target_type == TypeManager.ushort_type)
1187 return new UShortConstant ((ushort) v);
1188 if (target_type == TypeManager.int32_type)
1189 return new IntConstant ((int) v);
1190 if (target_type == TypeManager.uint32_type)
1191 return new UIntConstant ((uint) v);
1192 if (target_type == TypeManager.int64_type)
1193 return new LongConstant ((long) v);
1194 if (target_type == TypeManager.uint64_type)
1195 return new ULongConstant ((ulong) v);
1196 if (target_type == TypeManager.float_type)
1197 return new FloatConstant ((float) v);
1198 if (target_type == TypeManager.double_type)
1199 return new DoubleConstant ((double) v);
1200 if (target_type == TypeManager.char_type)
1201 return new CharConstant ((char) v);
1203 if (expr is ShortConstant){
1204 short v = ((ShortConstant) expr).Value;
1206 if (target_type == TypeManager.byte_type)
1207 return new ByteConstant ((byte) v);
1208 if (target_type == TypeManager.sbyte_type)
1209 return new SByteConstant ((sbyte) v);
1210 if (target_type == TypeManager.ushort_type)
1211 return new UShortConstant ((ushort) v);
1212 if (target_type == TypeManager.int32_type)
1213 return new IntConstant ((int) v);
1214 if (target_type == TypeManager.uint32_type)
1215 return new UIntConstant ((uint) v);
1216 if (target_type == TypeManager.int64_type)
1217 return new LongConstant ((long) v);
1218 if (target_type == TypeManager.uint64_type)
1219 return new ULongConstant ((ulong) v);
1220 if (target_type == TypeManager.float_type)
1221 return new FloatConstant ((float) v);
1222 if (target_type == TypeManager.double_type)
1223 return new DoubleConstant ((double) v);
1224 if (target_type == TypeManager.char_type)
1225 return new CharConstant ((char) v);
1227 if (expr is UShortConstant){
1228 ushort v = ((UShortConstant) expr).Value;
1230 if (target_type == TypeManager.byte_type)
1231 return new ByteConstant ((byte) v);
1232 if (target_type == TypeManager.sbyte_type)
1233 return new SByteConstant ((sbyte) v);
1234 if (target_type == TypeManager.short_type)
1235 return new ShortConstant ((short) v);
1236 if (target_type == TypeManager.int32_type)
1237 return new IntConstant ((int) v);
1238 if (target_type == TypeManager.uint32_type)
1239 return new UIntConstant ((uint) v);
1240 if (target_type == TypeManager.int64_type)
1241 return new LongConstant ((long) v);
1242 if (target_type == TypeManager.uint64_type)
1243 return new ULongConstant ((ulong) v);
1244 if (target_type == TypeManager.float_type)
1245 return new FloatConstant ((float) v);
1246 if (target_type == TypeManager.double_type)
1247 return new DoubleConstant ((double) v);
1248 if (target_type == TypeManager.char_type)
1249 return new CharConstant ((char) v);
1251 if (expr is IntConstant){
1252 int v = ((IntConstant) expr).Value;
1254 if (target_type == TypeManager.byte_type)
1255 return new ByteConstant ((byte) v);
1256 if (target_type == TypeManager.sbyte_type)
1257 return new SByteConstant ((sbyte) v);
1258 if (target_type == TypeManager.short_type)
1259 return new ShortConstant ((short) v);
1260 if (target_type == TypeManager.ushort_type)
1261 return new UShortConstant ((ushort) v);
1262 if (target_type == TypeManager.uint32_type)
1263 return new UIntConstant ((uint) v);
1264 if (target_type == TypeManager.int64_type)
1265 return new LongConstant ((long) v);
1266 if (target_type == TypeManager.uint64_type)
1267 return new ULongConstant ((ulong) v);
1268 if (target_type == TypeManager.float_type)
1269 return new FloatConstant ((float) v);
1270 if (target_type == TypeManager.double_type)
1271 return new DoubleConstant ((double) v);
1272 if (target_type == TypeManager.char_type)
1273 return new CharConstant ((char) v);
1275 if (expr is UIntConstant){
1276 uint v = ((UIntConstant) expr).Value;
1278 if (target_type == TypeManager.byte_type)
1279 return new ByteConstant ((byte) v);
1280 if (target_type == TypeManager.sbyte_type)
1281 return new SByteConstant ((sbyte) v);
1282 if (target_type == TypeManager.short_type)
1283 return new ShortConstant ((short) v);
1284 if (target_type == TypeManager.ushort_type)
1285 return new UShortConstant ((ushort) v);
1286 if (target_type == TypeManager.int32_type)
1287 return new IntConstant ((int) v);
1288 if (target_type == TypeManager.int64_type)
1289 return new LongConstant ((long) v);
1290 if (target_type == TypeManager.uint64_type)
1291 return new ULongConstant ((ulong) v);
1292 if (target_type == TypeManager.float_type)
1293 return new FloatConstant ((float) v);
1294 if (target_type == TypeManager.double_type)
1295 return new DoubleConstant ((double) v);
1296 if (target_type == TypeManager.char_type)
1297 return new CharConstant ((char) v);
1299 if (expr is LongConstant){
1300 long v = ((LongConstant) expr).Value;
1302 if (target_type == TypeManager.byte_type)
1303 return new ByteConstant ((byte) v);
1304 if (target_type == TypeManager.sbyte_type)
1305 return new SByteConstant ((sbyte) v);
1306 if (target_type == TypeManager.short_type)
1307 return new ShortConstant ((short) v);
1308 if (target_type == TypeManager.ushort_type)
1309 return new UShortConstant ((ushort) v);
1310 if (target_type == TypeManager.int32_type)
1311 return new IntConstant ((int) v);
1312 if (target_type == TypeManager.uint32_type)
1313 return new UIntConstant ((uint) v);
1314 if (target_type == TypeManager.uint64_type)
1315 return new ULongConstant ((ulong) v);
1316 if (target_type == TypeManager.float_type)
1317 return new FloatConstant ((float) v);
1318 if (target_type == TypeManager.double_type)
1319 return new DoubleConstant ((double) v);
1320 if (target_type == TypeManager.char_type)
1321 return new CharConstant ((char) v);
1323 if (expr is ULongConstant){
1324 ulong v = ((ULongConstant) expr).Value;
1326 if (target_type == TypeManager.byte_type)
1327 return new ByteConstant ((byte) v);
1328 if (target_type == TypeManager.sbyte_type)
1329 return new SByteConstant ((sbyte) v);
1330 if (target_type == TypeManager.short_type)
1331 return new ShortConstant ((short) v);
1332 if (target_type == TypeManager.ushort_type)
1333 return new UShortConstant ((ushort) v);
1334 if (target_type == TypeManager.int32_type)
1335 return new IntConstant ((int) v);
1336 if (target_type == TypeManager.uint32_type)
1337 return new UIntConstant ((uint) v);
1338 if (target_type == TypeManager.int64_type)
1339 return new LongConstant ((long) v);
1340 if (target_type == TypeManager.float_type)
1341 return new FloatConstant ((float) v);
1342 if (target_type == TypeManager.double_type)
1343 return new DoubleConstant ((double) v);
1344 if (target_type == TypeManager.char_type)
1345 return new CharConstant ((char) v);
1347 if (expr is FloatConstant){
1348 float v = ((FloatConstant) expr).Value;
1350 if (target_type == TypeManager.byte_type)
1351 return new ByteConstant ((byte) v);
1352 if (target_type == TypeManager.sbyte_type)
1353 return new SByteConstant ((sbyte) v);
1354 if (target_type == TypeManager.short_type)
1355 return new ShortConstant ((short) v);
1356 if (target_type == TypeManager.ushort_type)
1357 return new UShortConstant ((ushort) v);
1358 if (target_type == TypeManager.int32_type)
1359 return new IntConstant ((int) v);
1360 if (target_type == TypeManager.uint32_type)
1361 return new UIntConstant ((uint) v);
1362 if (target_type == TypeManager.int64_type)
1363 return new LongConstant ((long) v);
1364 if (target_type == TypeManager.uint64_type)
1365 return new ULongConstant ((ulong) v);
1366 if (target_type == TypeManager.double_type)
1367 return new DoubleConstant ((double) v);
1368 if (target_type == TypeManager.char_type)
1369 return new CharConstant ((char) v);
1371 if (expr is DoubleConstant){
1372 double v = ((DoubleConstant) expr).Value;
1374 if (target_type == TypeManager.byte_type)
1375 return new ByteConstant ((byte) v);
1376 if (target_type == TypeManager.sbyte_type)
1377 return new SByteConstant ((sbyte) v);
1378 if (target_type == TypeManager.short_type)
1379 return new ShortConstant ((short) v);
1380 if (target_type == TypeManager.ushort_type)
1381 return new UShortConstant ((ushort) v);
1382 if (target_type == TypeManager.int32_type)
1383 return new IntConstant ((int) v);
1384 if (target_type == TypeManager.uint32_type)
1385 return new UIntConstant ((uint) v);
1386 if (target_type == TypeManager.int64_type)
1387 return new LongConstant ((long) v);
1388 if (target_type == TypeManager.uint64_type)
1389 return new ULongConstant ((ulong) v);
1390 if (target_type == TypeManager.float_type)
1391 return new FloatConstant ((float) v);
1392 if (target_type == TypeManager.char_type)
1393 return new CharConstant ((char) v);
1399 public override Expression DoResolve (EmitContext ec)
1401 expr = expr.Resolve (ec);
1405 bool old_state = ec.OnlyLookupTypes;
1406 ec.OnlyLookupTypes = true;
1407 target_type = target_type.Resolve (ec);
1408 ec.OnlyLookupTypes = old_state;
1410 if (target_type == null){
1411 Report.Error (-10, loc, "Can not resolve type");
1415 if (target_type.eclass != ExprClass.Type){
1416 report118 (loc, target_type, "class");
1420 type = target_type.Type;
1421 eclass = ExprClass.Value;
1426 if (expr is Constant){
1427 Expression e = TryReduce (ec, type);
1433 expr = ConvertExplicit (ec, expr, type, loc);
1437 public override void Emit (EmitContext ec)
1440 // This one will never happen
1442 throw new Exception ("Should not happen");
1447 /// Binary operators
1449 public class Binary : Expression {
1450 public enum Operator : byte {
1451 Multiply, Division, Modulus,
1452 Addition, Subtraction,
1453 LeftShift, RightShift,
1454 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1455 Equality, Inequality,
1465 Expression left, right;
1468 // After resolution, method might contain the operator overload
1471 protected MethodBase method;
1472 ArrayList Arguments;
1476 bool DelegateOperation;
1478 // This must be kept in sync with Operator!!!
1479 static string [] oper_names;
1483 oper_names = new string [(int) Operator.TOP];
1485 oper_names [(int) Operator.Multiply] = "op_Multiply";
1486 oper_names [(int) Operator.Division] = "op_Division";
1487 oper_names [(int) Operator.Modulus] = "op_Modulus";
1488 oper_names [(int) Operator.Addition] = "op_Addition";
1489 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1490 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1491 oper_names [(int) Operator.RightShift] = "op_RightShift";
1492 oper_names [(int) Operator.LessThan] = "op_LessThan";
1493 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1494 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1495 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1496 oper_names [(int) Operator.Equality] = "op_Equality";
1497 oper_names [(int) Operator.Inequality] = "op_Inequality";
1498 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1499 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1500 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1501 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1502 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1505 public Binary (Operator oper, Expression left, Expression right, Location loc)
1513 public Operator Oper {
1522 public Expression Left {
1531 public Expression Right {
1542 /// Returns a stringified representation of the Operator
1544 static string OperName (Operator oper)
1547 case Operator.Multiply:
1549 case Operator.Division:
1551 case Operator.Modulus:
1553 case Operator.Addition:
1555 case Operator.Subtraction:
1557 case Operator.LeftShift:
1559 case Operator.RightShift:
1561 case Operator.LessThan:
1563 case Operator.GreaterThan:
1565 case Operator.LessThanOrEqual:
1567 case Operator.GreaterThanOrEqual:
1569 case Operator.Equality:
1571 case Operator.Inequality:
1573 case Operator.BitwiseAnd:
1575 case Operator.BitwiseOr:
1577 case Operator.ExclusiveOr:
1579 case Operator.LogicalOr:
1581 case Operator.LogicalAnd:
1585 return oper.ToString ();
1588 public override string ToString ()
1590 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1591 right.ToString () + ")";
1594 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1596 if (expr.Type == target_type)
1599 return ConvertImplicit (ec, expr, target_type, new Location (-1));
1602 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
1605 34, loc, "Operator `" + OperName (oper)
1606 + "' is ambiguous on operands of type `"
1607 + TypeManager.CSharpName (l) + "' "
1608 + "and `" + TypeManager.CSharpName (r)
1613 // Note that handling the case l == Decimal || r == Decimal
1614 // is taken care of by the Step 1 Operator Overload resolution.
1616 bool DoNumericPromotions (EmitContext ec, Type l, Type r)
1618 if (l == TypeManager.double_type || r == TypeManager.double_type){
1620 // If either operand is of type double, the other operand is
1621 // conveted to type double.
1623 if (r != TypeManager.double_type)
1624 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
1625 if (l != TypeManager.double_type)
1626 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
1628 type = TypeManager.double_type;
1629 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1631 // if either operand is of type float, the other operand is
1632 // converted to type float.
1634 if (r != TypeManager.double_type)
1635 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
1636 if (l != TypeManager.double_type)
1637 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
1638 type = TypeManager.float_type;
1639 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1643 // If either operand is of type ulong, the other operand is
1644 // converted to type ulong. or an error ocurrs if the other
1645 // operand is of type sbyte, short, int or long
1647 if (l == TypeManager.uint64_type){
1648 if (r != TypeManager.uint64_type){
1649 if (right is IntConstant){
1650 IntConstant ic = (IntConstant) right;
1652 e = TryImplicitIntConversion (l, ic);
1655 } else if (right is LongConstant){
1656 long ll = ((LongConstant) right).Value;
1659 right = new ULongConstant ((ulong) ll);
1661 e = ImplicitNumericConversion (ec, right, l, loc);
1668 if (left is IntConstant){
1669 e = TryImplicitIntConversion (r, (IntConstant) left);
1672 } else if (left is LongConstant){
1673 long ll = ((LongConstant) left).Value;
1676 left = new ULongConstant ((ulong) ll);
1678 e = ImplicitNumericConversion (ec, left, r, loc);
1685 if ((other == TypeManager.sbyte_type) ||
1686 (other == TypeManager.short_type) ||
1687 (other == TypeManager.int32_type) ||
1688 (other == TypeManager.int64_type))
1689 Error_OperatorAmbiguous (loc, oper, l, r);
1690 type = TypeManager.uint64_type;
1691 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1693 // If either operand is of type long, the other operand is converted
1696 if (l != TypeManager.int64_type)
1697 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
1698 if (r != TypeManager.int64_type)
1699 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
1701 type = TypeManager.int64_type;
1702 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1704 // If either operand is of type uint, and the other
1705 // operand is of type sbyte, short or int, othe operands are
1706 // converted to type long.
1710 if (l == TypeManager.uint32_type){
1711 if (right is IntConstant){
1712 IntConstant ic = (IntConstant) right;
1716 right = new UIntConstant ((uint) val);
1723 else if (r == TypeManager.uint32_type){
1724 if (left is IntConstant){
1725 IntConstant ic = (IntConstant) left;
1729 left = new UIntConstant ((uint) val);
1738 if ((other == TypeManager.sbyte_type) ||
1739 (other == TypeManager.short_type) ||
1740 (other == TypeManager.int32_type)){
1741 left = ForceConversion (ec, left, TypeManager.int64_type);
1742 right = ForceConversion (ec, right, TypeManager.int64_type);
1743 type = TypeManager.int64_type;
1746 // if either operand is of type uint, the other
1747 // operand is converd to type uint
1749 left = ForceConversion (ec, left, TypeManager.uint32_type);
1750 right = ForceConversion (ec, right, TypeManager.uint32_type);
1751 type = TypeManager.uint32_type;
1753 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1754 if (l != TypeManager.decimal_type)
1755 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
1756 if (r != TypeManager.decimal_type)
1757 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
1759 type = TypeManager.decimal_type;
1761 Expression l_tmp, r_tmp;
1763 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
1767 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
1774 type = TypeManager.int32_type;
1780 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
1783 "Operator " + name + " cannot be applied to operands of type `" +
1784 TypeManager.CSharpName (l) + "' and `" +
1785 TypeManager.CSharpName (r) + "'");
1788 void Error_OperatorCannotBeApplied ()
1790 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
1793 static bool is_32_or_64 (Type t)
1795 return (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
1796 t == TypeManager.int64_type || t == TypeManager.uint64_type);
1799 static bool is_unsigned (Type t)
1801 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
1802 t == TypeManager.short_type || t == TypeManager.byte_type);
1805 Expression CheckShiftArguments (EmitContext ec)
1809 Type r = right.Type;
1811 e = ForceConversion (ec, right, TypeManager.int32_type);
1813 Error_OperatorCannotBeApplied ();
1818 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
1819 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
1820 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
1821 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
1827 Error_OperatorCannotBeApplied ();
1831 Expression ResolveOperator (EmitContext ec)
1834 Type r = right.Type;
1836 bool overload_failed = false;
1839 // Step 1: Perform Operator Overload location
1841 Expression left_expr, right_expr;
1843 string op = oper_names [(int) oper];
1845 MethodGroupExpr union;
1846 left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
1848 right_expr = MemberLookup (
1849 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
1850 union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
1852 union = (MethodGroupExpr) left_expr;
1854 if (union != null) {
1855 Arguments = new ArrayList ();
1856 Arguments.Add (new Argument (left, Argument.AType.Expression));
1857 Arguments.Add (new Argument (right, Argument.AType.Expression));
1859 method = Invocation.OverloadResolve (ec, union, Arguments, Location.Null);
1860 if (method != null) {
1861 MethodInfo mi = (MethodInfo) method;
1863 type = mi.ReturnType;
1866 overload_failed = true;
1871 // Step 2: Default operations on CLI native types.
1875 // Step 0: String concatenation (because overloading will get this wrong)
1877 if (oper == Operator.Addition){
1879 // If any of the arguments is a string, cast to string
1882 if (l == TypeManager.string_type){
1884 if (r == TypeManager.void_type) {
1885 Error_OperatorCannotBeApplied ();
1889 if (r == TypeManager.string_type){
1890 if (left is Constant && right is Constant){
1891 StringConstant ls = (StringConstant) left;
1892 StringConstant rs = (StringConstant) right;
1894 return new StringConstant (
1895 ls.Value + rs.Value);
1899 method = TypeManager.string_concat_string_string;
1902 method = TypeManager.string_concat_object_object;
1903 right = ConvertImplicit (ec, right,
1904 TypeManager.object_type, loc);
1906 type = TypeManager.string_type;
1908 Arguments = new ArrayList ();
1909 Arguments.Add (new Argument (left, Argument.AType.Expression));
1910 Arguments.Add (new Argument (right, Argument.AType.Expression));
1914 } else if (r == TypeManager.string_type){
1917 if (l == TypeManager.void_type) {
1918 Error_OperatorCannotBeApplied ();
1922 method = TypeManager.string_concat_object_object;
1923 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1924 Arguments = new ArrayList ();
1925 Arguments.Add (new Argument (left, Argument.AType.Expression));
1926 Arguments.Add (new Argument (right, Argument.AType.Expression));
1928 type = TypeManager.string_type;
1934 // Transform a + ( - b) into a - b
1936 if (right is Unary){
1937 Unary right_unary = (Unary) right;
1939 if (right_unary.Oper == Unary.Operator.UnaryNegation){
1940 oper = Operator.Subtraction;
1941 right = right_unary.Expr;
1947 if (oper == Operator.Equality || oper == Operator.Inequality){
1948 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
1949 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
1950 Error_OperatorCannotBeApplied ();
1954 type = TypeManager.bool_type;
1959 // operator != (object a, object b)
1960 // operator == (object a, object b)
1962 // For this to be used, both arguments have to be reference-types.
1963 // Read the rationale on the spec (14.9.6)
1965 // Also, if at compile time we know that the classes do not inherit
1966 // one from the other, then we catch the error there.
1968 if (!(l.IsValueType || r.IsValueType)){
1969 type = TypeManager.bool_type;
1974 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
1978 // Also, a standard conversion must exist from either one
1980 if (!(StandardConversionExists (left, r) ||
1981 StandardConversionExists (right, l))){
1982 Error_OperatorCannotBeApplied ();
1986 // We are going to have to convert to an object to compare
1988 if (l != TypeManager.object_type)
1989 left = new EmptyCast (left, TypeManager.object_type);
1990 if (r != TypeManager.object_type)
1991 right = new EmptyCast (right, TypeManager.object_type);
1994 // FIXME: CSC here catches errors cs254 and cs252
2000 // Only perform numeric promotions on:
2001 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2003 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2004 if (l.IsSubclassOf (TypeManager.delegate_type) &&
2005 r.IsSubclassOf (TypeManager.delegate_type)) {
2007 Arguments = new ArrayList ();
2008 Arguments.Add (new Argument (left, Argument.AType.Expression));
2009 Arguments.Add (new Argument (right, Argument.AType.Expression));
2011 if (oper == Operator.Addition)
2012 method = TypeManager.delegate_combine_delegate_delegate;
2014 method = TypeManager.delegate_remove_delegate_delegate;
2016 DelegateOperation = true;
2022 // Pointer arithmetic:
2024 // T* operator + (T* x, int y);
2025 // T* operator + (T* x, uint y);
2026 // T* operator + (T* x, long y);
2027 // T* operator + (T* x, ulong y);
2029 // T* operator + (int y, T* x);
2030 // T* operator + (uint y, T *x);
2031 // T* operator + (long y, T *x);
2032 // T* operator + (ulong y, T *x);
2034 // T* operator - (T* x, int y);
2035 // T* operator - (T* x, uint y);
2036 // T* operator - (T* x, long y);
2037 // T* operator - (T* x, ulong y);
2039 // long operator - (T* x, T *y)
2042 if (r.IsPointer && oper == Operator.Subtraction){
2044 return new PointerArithmetic (
2045 false, left, right, TypeManager.int64_type);
2046 } else if (is_32_or_64 (r))
2047 return new PointerArithmetic (
2048 oper == Operator.Addition, left, right, l);
2049 } else if (r.IsPointer && is_32_or_64 (l) && oper == Operator.Addition)
2050 return new PointerArithmetic (
2051 true, right, left, r);
2055 // Enumeration operators
2057 bool lie = TypeManager.IsEnumType (l);
2058 bool rie = TypeManager.IsEnumType (r);
2063 // operator + (E e, U x)
2065 if (oper == Operator.Addition){
2067 Error_OperatorCannotBeApplied ();
2071 Type enum_type = lie ? l : r;
2072 Type other_type = lie ? r : l;
2073 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2076 if (underlying_type != other_type){
2077 Error_OperatorCannotBeApplied ();
2086 temp = ConvertImplicit (ec, right, l, loc);
2090 temp = ConvertImplicit (ec, left, r, loc);
2097 if (oper == Operator.Equality || oper == Operator.Inequality ||
2098 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2099 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2100 type = TypeManager.bool_type;
2104 if (oper == Operator.BitwiseAnd ||
2105 oper == Operator.BitwiseOr ||
2106 oper == Operator.ExclusiveOr){
2113 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2114 return CheckShiftArguments (ec);
2116 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2117 if (l != TypeManager.bool_type || r != TypeManager.bool_type){
2118 Error_OperatorCannotBeApplied ();
2122 type = TypeManager.bool_type;
2127 // operator & (bool x, bool y)
2128 // operator | (bool x, bool y)
2129 // operator ^ (bool x, bool y)
2131 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2132 if (oper == Operator.BitwiseAnd ||
2133 oper == Operator.BitwiseOr ||
2134 oper == Operator.ExclusiveOr){
2141 // Pointer comparison
2143 if (l.IsPointer && r.IsPointer){
2144 if (oper == Operator.Equality || oper == Operator.Inequality ||
2145 oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2146 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2147 type = TypeManager.bool_type;
2153 // We are dealing with numbers
2155 if (overload_failed){
2156 Error_OperatorCannotBeApplied ();
2160 if (!DoNumericPromotions (ec, l, r)){
2161 Error_OperatorCannotBeApplied ();
2165 if (left == null || right == null)
2169 // reload our cached types if required
2174 if (oper == Operator.BitwiseAnd ||
2175 oper == Operator.BitwiseOr ||
2176 oper == Operator.ExclusiveOr){
2178 if (!((l == TypeManager.int32_type) ||
2179 (l == TypeManager.uint32_type) ||
2180 (l == TypeManager.int64_type) ||
2181 (l == TypeManager.uint64_type)))
2184 Error_OperatorCannotBeApplied ();
2189 if (oper == Operator.Equality ||
2190 oper == Operator.Inequality ||
2191 oper == Operator.LessThanOrEqual ||
2192 oper == Operator.LessThan ||
2193 oper == Operator.GreaterThanOrEqual ||
2194 oper == Operator.GreaterThan){
2195 type = TypeManager.bool_type;
2201 public override Expression DoResolve (EmitContext ec)
2203 left = left.Resolve (ec);
2204 right = right.Resolve (ec);
2206 if (left == null || right == null)
2209 if (left.Type == null)
2210 throw new Exception (
2211 "Resolve returned non null, but did not set the type! (" +
2212 left + ") at Line: " + loc.Row);
2213 if (right.Type == null)
2214 throw new Exception (
2215 "Resolve returned non null, but did not set the type! (" +
2216 right + ") at Line: "+ loc.Row);
2218 eclass = ExprClass.Value;
2220 if (left is Constant && right is Constant){
2221 Expression e = ConstantFold.BinaryFold (
2222 ec, oper, (Constant) left, (Constant) right, loc);
2227 return ResolveOperator (ec);
2231 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2232 /// context of a conditional bool expression. This function will return
2233 /// false if it is was possible to use EmitBranchable, or true if it was.
2235 /// The expression's code is generated, and we will generate a branch to `target'
2236 /// if the resulting expression value is equal to isTrue
2238 public bool EmitBranchable (EmitContext ec, Label target, bool onTrue)
2243 if (!(oper == Operator.Equality ||
2244 oper == Operator.Inequality ||
2245 oper == Operator.LessThan ||
2246 oper == Operator.GreaterThan ||
2247 oper == Operator.LessThanOrEqual ||
2248 oper == Operator.GreaterThanOrEqual))
2254 ILGenerator ig = ec.ig;
2256 bool isUnsigned = is_unsigned (left.Type);
2259 case Operator.Equality:
2261 ig.Emit (OpCodes.Beq, target);
2263 ig.Emit (OpCodes.Bne_Un, target);
2266 case Operator.Inequality:
2268 ig.Emit (OpCodes.Bne_Un, target);
2270 ig.Emit (OpCodes.Beq, target);
2273 case Operator.LessThan:
2276 ig.Emit (OpCodes.Blt_Un, target);
2278 ig.Emit (OpCodes.Blt, target);
2281 ig.Emit (OpCodes.Bge_Un, target);
2283 ig.Emit (OpCodes.Bge, target);
2286 case Operator.GreaterThan:
2289 ig.Emit (OpCodes.Bgt_Un, target);
2291 ig.Emit (OpCodes.Bgt, target);
2294 ig.Emit (OpCodes.Ble_Un, target);
2296 ig.Emit (OpCodes.Ble, target);
2299 case Operator.LessThanOrEqual:
2302 ig.Emit (OpCodes.Ble_Un, target);
2304 ig.Emit (OpCodes.Ble, target);
2307 ig.Emit (OpCodes.Bgt_Un, target);
2309 ig.Emit (OpCodes.Bgt, target);
2313 case Operator.GreaterThanOrEqual:
2316 ig.Emit (OpCodes.Bge_Un, target);
2318 ig.Emit (OpCodes.Bge, target);
2321 ig.Emit (OpCodes.Blt_Un, target);
2323 ig.Emit (OpCodes.Blt, target);
2333 public override void Emit (EmitContext ec)
2335 ILGenerator ig = ec.ig;
2337 Type r = right.Type;
2340 if (method != null) {
2342 // Note that operators are static anyway
2344 if (Arguments != null)
2345 Invocation.EmitArguments (ec, method, Arguments);
2347 if (method is MethodInfo)
2348 ig.Emit (OpCodes.Call, (MethodInfo) method);
2350 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2352 if (DelegateOperation)
2353 ig.Emit (OpCodes.Castclass, type);
2359 // Handle short-circuit operators differently
2362 if (oper == Operator.LogicalAnd){
2363 Label load_zero = ig.DefineLabel ();
2364 Label end = ig.DefineLabel ();
2367 ig.Emit (OpCodes.Brfalse, load_zero);
2369 ig.Emit (OpCodes.Br, end);
2370 ig.MarkLabel (load_zero);
2371 ig.Emit (OpCodes.Ldc_I4_0);
2374 } else if (oper == Operator.LogicalOr){
2375 Label load_one = ig.DefineLabel ();
2376 Label end = ig.DefineLabel ();
2379 ig.Emit (OpCodes.Brtrue, load_one);
2381 ig.Emit (OpCodes.Br, end);
2382 ig.MarkLabel (load_one);
2383 ig.Emit (OpCodes.Ldc_I4_1);
2392 case Operator.Multiply:
2394 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2395 opcode = OpCodes.Mul_Ovf;
2396 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2397 opcode = OpCodes.Mul_Ovf_Un;
2399 opcode = OpCodes.Mul;
2401 opcode = OpCodes.Mul;
2405 case Operator.Division:
2406 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2407 opcode = OpCodes.Div_Un;
2409 opcode = OpCodes.Div;
2412 case Operator.Modulus:
2413 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2414 opcode = OpCodes.Rem_Un;
2416 opcode = OpCodes.Rem;
2419 case Operator.Addition:
2421 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2422 opcode = OpCodes.Add_Ovf;
2423 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2424 opcode = OpCodes.Add_Ovf_Un;
2426 opcode = OpCodes.Add;
2428 opcode = OpCodes.Add;
2431 case Operator.Subtraction:
2433 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2434 opcode = OpCodes.Sub_Ovf;
2435 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2436 opcode = OpCodes.Sub_Ovf_Un;
2438 opcode = OpCodes.Sub;
2440 opcode = OpCodes.Sub;
2443 case Operator.RightShift:
2444 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2445 opcode = OpCodes.Shr_Un;
2447 opcode = OpCodes.Shr;
2450 case Operator.LeftShift:
2451 opcode = OpCodes.Shl;
2454 case Operator.Equality:
2455 opcode = OpCodes.Ceq;
2458 case Operator.Inequality:
2459 ec.ig.Emit (OpCodes.Ceq);
2460 ec.ig.Emit (OpCodes.Ldc_I4_0);
2462 opcode = OpCodes.Ceq;
2465 case Operator.LessThan:
2466 opcode = OpCodes.Clt;
2469 case Operator.GreaterThan:
2470 opcode = OpCodes.Cgt;
2473 case Operator.LessThanOrEqual:
2474 ec.ig.Emit (OpCodes.Cgt);
2475 ec.ig.Emit (OpCodes.Ldc_I4_0);
2477 opcode = OpCodes.Ceq;
2480 case Operator.GreaterThanOrEqual:
2481 ec.ig.Emit (OpCodes.Clt);
2482 ec.ig.Emit (OpCodes.Ldc_I4_1);
2484 opcode = OpCodes.Sub;
2487 case Operator.BitwiseOr:
2488 opcode = OpCodes.Or;
2491 case Operator.BitwiseAnd:
2492 opcode = OpCodes.And;
2495 case Operator.ExclusiveOr:
2496 opcode = OpCodes.Xor;
2500 throw new Exception ("This should not happen: Operator = "
2501 + oper.ToString ());
2507 public bool IsBuiltinOperator {
2509 return method == null;
2514 public class PointerArithmetic : Expression {
2515 Expression left, right;
2519 // We assume that `l' is always a pointer
2521 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t)
2524 eclass = ExprClass.Variable;
2527 is_add = is_addition;
2530 public override Expression DoResolve (EmitContext ec)
2533 // We are born fully resolved
2538 public override void Emit (EmitContext ec)
2540 Type op_type = left.Type;
2541 ILGenerator ig = ec.ig;
2542 int size = GetTypeSize (op_type.GetElementType ());
2544 if (right.Type.IsPointer){
2546 // handle (pointer - pointer)
2550 ig.Emit (OpCodes.Sub);
2554 ig.Emit (OpCodes.Sizeof, op_type);
2556 IntLiteral.EmitInt (ig, size);
2557 ig.Emit (OpCodes.Div);
2559 ig.Emit (OpCodes.Conv_I8);
2562 // handle + and - on (pointer op int)
2565 ig.Emit (OpCodes.Conv_I);
2569 ig.Emit (OpCodes.Sizeof, op_type);
2571 IntLiteral.EmitInt (ig, size);
2572 ig.Emit (OpCodes.Mul);
2575 ig.Emit (OpCodes.Add);
2577 ig.Emit (OpCodes.Sub);
2583 /// Implements the ternary conditiona operator (?:)
2585 public class Conditional : Expression {
2586 Expression expr, trueExpr, falseExpr;
2589 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2592 this.trueExpr = trueExpr;
2593 this.falseExpr = falseExpr;
2597 public Expression Expr {
2603 public Expression TrueExpr {
2609 public Expression FalseExpr {
2615 public override Expression DoResolve (EmitContext ec)
2617 expr = expr.Resolve (ec);
2619 if (expr.Type != TypeManager.bool_type)
2620 expr = Expression.ConvertImplicitRequired (
2621 ec, expr, TypeManager.bool_type, loc);
2623 trueExpr = trueExpr.Resolve (ec);
2624 falseExpr = falseExpr.Resolve (ec);
2626 if (expr == null || trueExpr == null || falseExpr == null)
2629 eclass = ExprClass.Value;
2630 if (trueExpr.Type == falseExpr.Type)
2631 type = trueExpr.Type;
2634 Type true_type = trueExpr.Type;
2635 Type false_type = falseExpr.Type;
2637 if (trueExpr is NullLiteral){
2640 } else if (falseExpr is NullLiteral){
2646 // First, if an implicit conversion exists from trueExpr
2647 // to falseExpr, then the result type is of type falseExpr.Type
2649 conv = ConvertImplicit (ec, trueExpr, false_type, loc);
2652 // Check if both can convert implicitl to each other's type
2654 if (ConvertImplicit (ec, falseExpr, true_type, loc) != null){
2657 "Can not compute type of conditional expression " +
2658 "as `" + TypeManager.CSharpName (trueExpr.Type) +
2659 "' and `" + TypeManager.CSharpName (falseExpr.Type) +
2660 "' convert implicitly to each other");
2665 } else if ((conv = ConvertImplicit(ec, falseExpr, true_type,loc))!= null){
2669 Error (173, loc, "The type of the conditional expression can " +
2670 "not be computed because there is no implicit conversion" +
2671 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2672 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2677 if (expr is BoolConstant){
2678 BoolConstant bc = (BoolConstant) expr;
2689 public override void Emit (EmitContext ec)
2691 ILGenerator ig = ec.ig;
2692 Label false_target = ig.DefineLabel ();
2693 Label end_target = ig.DefineLabel ();
2696 ig.Emit (OpCodes.Brfalse, false_target);
2698 ig.Emit (OpCodes.Br, end_target);
2699 ig.MarkLabel (false_target);
2700 falseExpr.Emit (ec);
2701 ig.MarkLabel (end_target);
2709 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
2710 public readonly string Name;
2711 public readonly Block Block;
2713 VariableInfo variable_info;
2715 public LocalVariableReference (Block block, string name, Location l)
2720 eclass = ExprClass.Variable;
2723 public VariableInfo VariableInfo {
2725 if (variable_info == null)
2726 variable_info = Block.GetVariableInfo (Name);
2727 return variable_info;
2731 public override Expression DoResolve (EmitContext ec)
2733 VariableInfo vi = VariableInfo;
2735 if (Block.IsConstant (Name)) {
2736 Expression e = Block.GetConstantExpression (Name);
2742 type = vi.VariableType;
2746 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2748 Expression e = DoResolve (ec);
2753 VariableInfo vi = VariableInfo;
2757 // Sigh: this breaks `using' and `fixed'. Need to review that
2762 "cannot assign to `" + Name + "' because it is readonly");
2770 public override void Emit (EmitContext ec)
2772 VariableInfo vi = VariableInfo;
2773 ILGenerator ig = ec.ig;
2775 ig.Emit (OpCodes.Ldloc, vi.LocalBuilder);
2779 public void EmitAssign (EmitContext ec, Expression source)
2781 ILGenerator ig = ec.ig;
2782 VariableInfo vi = VariableInfo;
2788 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
2791 public void AddressOf (EmitContext ec, AddressOp mode)
2793 VariableInfo vi = VariableInfo;
2795 if ((mode & AddressOp.Load) != 0)
2797 if ((mode & AddressOp.Store) != 0)
2800 ec.ig.Emit (OpCodes.Ldloca, vi.LocalBuilder);
2805 /// This represents a reference to a parameter in the intermediate
2808 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
2814 public ParameterReference (Parameters pars, int idx, string name)
2819 eclass = ExprClass.Variable;
2823 // Notice that for ref/out parameters, the type exposed is not the
2824 // same type exposed externally.
2827 // externally we expose "int&"
2828 // here we expose "int".
2830 // We record this in "is_ref". This means that the type system can treat
2831 // the type as it is expected, but when we generate the code, we generate
2832 // the alternate kind of code.
2834 public override Expression DoResolve (EmitContext ec)
2836 type = pars.GetParameterInfo (ec.DeclSpace, idx, out is_ref);
2837 eclass = ExprClass.Variable;
2842 static void EmitLdArg (ILGenerator ig, int x)
2846 case 0: ig.Emit (OpCodes.Ldarg_0); break;
2847 case 1: ig.Emit (OpCodes.Ldarg_1); break;
2848 case 2: ig.Emit (OpCodes.Ldarg_2); break;
2849 case 3: ig.Emit (OpCodes.Ldarg_3); break;
2850 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
2853 ig.Emit (OpCodes.Ldarg, x);
2857 // This method is used by parameters that are references, that are
2858 // being passed as references: we only want to pass the pointer (that
2859 // is already stored in the parameter, not the address of the pointer,
2860 // and not the value of the variable).
2862 public void EmitLoad (EmitContext ec)
2864 ILGenerator ig = ec.ig;
2870 EmitLdArg (ig, arg_idx);
2873 public override void Emit (EmitContext ec)
2875 ILGenerator ig = ec.ig;
2881 EmitLdArg (ig, arg_idx);
2887 // If we are a reference, we loaded on the stack a pointer
2888 // Now lets load the real value
2890 LoadFromPtr (ig, type);
2893 public void EmitAssign (EmitContext ec, Expression source)
2895 ILGenerator ig = ec.ig;
2902 EmitLdArg (ig, arg_idx);
2907 StoreFromPtr (ig, type);
2910 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
2912 ig.Emit (OpCodes.Starg, arg_idx);
2916 public void AddressOf (EmitContext ec, AddressOp mode)
2925 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
2927 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
2930 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
2932 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
2938 /// Used for arguments to New(), Invocation()
2940 public class Argument {
2941 public enum AType : byte {
2947 public readonly AType ArgType;
2948 public Expression Expr;
2950 public Argument (Expression expr, AType type)
2953 this.ArgType = type;
2958 if (ArgType == AType.Ref || ArgType == AType.Out)
2959 return TypeManager.LookupType (Expr.Type.ToString () + "&");
2965 public Parameter.Modifier GetParameterModifier ()
2967 if (ArgType == AType.Ref || ArgType == AType.Out)
2968 return Parameter.Modifier.OUT;
2970 return Parameter.Modifier.NONE;
2973 public static string FullDesc (Argument a)
2975 return (a.ArgType == AType.Ref ? "ref " :
2976 (a.ArgType == AType.Out ? "out " : "")) +
2977 TypeManager.CSharpName (a.Expr.Type);
2980 public bool Resolve (EmitContext ec, Location loc)
2982 Expr = Expr.Resolve (ec);
2984 if (ArgType == AType.Expression){
2988 if ((Expr.eclass == ExprClass.Type) && (Expr is TypeExpr)) {
2989 Report.Error (118, loc, "Expression denotes a `type' " +
2990 "where a `variable or value' was expected");
2997 if (Expr.eclass != ExprClass.Variable){
2999 // We just probe to match the CSC output
3001 if (Expr.eclass == ExprClass.PropertyAccess ||
3002 Expr.eclass == ExprClass.IndexerAccess){
3005 "A property or indexer can not be passed as an out or ref " +
3010 "An lvalue is required as an argument to out or ref");
3015 return Expr != null;
3018 public void Emit (EmitContext ec)
3021 // Ref and Out parameters need to have their addresses taken.
3023 // ParameterReferences might already be references, so we want
3024 // to pass just the value
3026 if (ArgType == AType.Ref || ArgType == AType.Out){
3027 AddressOp mode = AddressOp.Store;
3029 if (ArgType == AType.Ref)
3030 mode |= AddressOp.Load;
3032 if (Expr is ParameterReference){
3033 ParameterReference pr = (ParameterReference) Expr;
3039 pr.AddressOf (ec, mode);
3042 ((IMemoryLocation)Expr).AddressOf (ec, mode);
3049 /// Invocation of methods or delegates.
3051 public class Invocation : ExpressionStatement {
3052 public readonly ArrayList Arguments;
3056 MethodBase method = null;
3059 static Hashtable method_parameter_cache;
3061 static Invocation ()
3063 method_parameter_cache = new PtrHashtable ();
3067 // arguments is an ArrayList, but we do not want to typecast,
3068 // as it might be null.
3070 // FIXME: only allow expr to be a method invocation or a
3071 // delegate invocation (7.5.5)
3073 public Invocation (Expression expr, ArrayList arguments, Location l)
3076 Arguments = arguments;
3080 public Expression Expr {
3087 /// Returns the Parameters (a ParameterData interface) for the
3090 public static ParameterData GetParameterData (MethodBase mb)
3092 object pd = method_parameter_cache [mb];
3096 return (ParameterData) pd;
3099 ip = TypeManager.LookupParametersByBuilder (mb);
3101 method_parameter_cache [mb] = ip;
3103 return (ParameterData) ip;
3105 ParameterInfo [] pi = mb.GetParameters ();
3106 ReflectionParameters rp = new ReflectionParameters (pi);
3107 method_parameter_cache [mb] = rp;
3109 return (ParameterData) rp;
3114 /// Determines "better conversion" as specified in 7.4.2.3
3115 /// Returns : 1 if a->p is better
3116 /// 0 if a->q or neither is better
3118 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
3120 Type argument_type = a.Type;
3121 Expression argument_expr = a.Expr;
3123 if (argument_type == null)
3124 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3129 if (argument_type == p)
3132 if (argument_type == q)
3136 // Now probe whether an implicit constant expression conversion
3139 // An implicit constant expression conversion permits the following
3142 // * A constant-expression of type `int' can be converted to type
3143 // sbyte, byute, short, ushort, uint, ulong provided the value of
3144 // of the expression is withing the range of the destination type.
3146 // * A constant-expression of type long can be converted to type
3147 // ulong, provided the value of the constant expression is not negative
3149 // FIXME: Note that this assumes that constant folding has
3150 // taken place. We dont do constant folding yet.
3153 if (argument_expr is IntConstant){
3154 IntConstant ei = (IntConstant) argument_expr;
3155 int value = ei.Value;
3157 if (p == TypeManager.sbyte_type){
3158 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3160 } else if (p == TypeManager.byte_type){
3161 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3163 } else if (p == TypeManager.short_type){
3164 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3166 } else if (p == TypeManager.ushort_type){
3167 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3169 } else if (p == TypeManager.uint32_type){
3171 // we can optimize this case: a positive int32
3172 // always fits on a uint32
3176 } else if (p == TypeManager.uint64_type){
3178 // we can optimize this case: a positive int32
3179 // always fits on a uint64
3184 } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
3185 LongConstant lc = (LongConstant) argument_expr;
3187 if (p == TypeManager.uint64_type){
3194 Expression tmp = ConvertImplicit (ec, argument_expr, p, loc);
3202 Expression p_tmp = new EmptyExpression (p);
3203 Expression q_tmp = new EmptyExpression (q);
3205 if (StandardConversionExists (p_tmp, q) == true &&
3206 StandardConversionExists (q_tmp, p) == false)
3209 if (p == TypeManager.sbyte_type)
3210 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3211 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3214 if (p == TypeManager.short_type)
3215 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3216 q == TypeManager.uint64_type)
3219 if (p == TypeManager.int32_type)
3220 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3223 if (p == TypeManager.int64_type)
3224 if (q == TypeManager.uint64_type)
3231 /// Determines "Better function"
3234 /// and returns an integer indicating :
3235 /// 0 if candidate ain't better
3236 /// 1 if candidate is better than the current best match
3238 static int BetterFunction (EmitContext ec, ArrayList args,
3239 MethodBase candidate, MethodBase best,
3240 bool expanded_form, Location loc)
3242 ParameterData candidate_pd = GetParameterData (candidate);
3243 ParameterData best_pd;
3249 argument_count = args.Count;
3251 int cand_count = candidate_pd.Count;
3253 if (cand_count == 0 && argument_count == 0)
3256 if (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS)
3257 if (cand_count != argument_count)
3263 if (argument_count == 0 && cand_count == 1 &&
3264 candidate_pd.ParameterModifier (cand_count - 1) == Parameter.Modifier.PARAMS)
3267 for (int j = argument_count; j > 0;) {
3270 Argument a = (Argument) args [j];
3271 Type t = candidate_pd.ParameterType (j);
3273 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
3275 t = t.GetElementType ();
3277 x = BetterConversion (ec, a, t, null, loc);
3289 best_pd = GetParameterData (best);
3291 int rating1 = 0, rating2 = 0;
3293 for (int j = 0; j < argument_count; ++j) {
3296 Argument a = (Argument) args [j];
3298 Type ct = candidate_pd.ParameterType (j);
3299 Type bt = best_pd.ParameterType (j);
3301 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
3303 ct = ct.GetElementType ();
3305 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
3307 bt = bt.GetElementType ();
3309 x = BetterConversion (ec, a, ct, bt, loc);
3310 y = BetterConversion (ec, a, bt, ct, loc);
3319 if (rating1 > rating2)
3325 public static string FullMethodDesc (MethodBase mb)
3327 string ret_type = "";
3329 if (mb is MethodInfo)
3330 ret_type = TypeManager.CSharpName (((MethodInfo) mb).ReturnType);
3332 StringBuilder sb = new StringBuilder (ret_type + " " + mb.Name);
3333 ParameterData pd = GetParameterData (mb);
3335 int count = pd.Count;
3338 for (int i = count; i > 0; ) {
3341 sb.Append (pd.ParameterDesc (count - i - 1));
3347 return sb.ToString ();
3350 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
3352 MemberInfo [] miset;
3353 MethodGroupExpr union;
3358 return (MethodGroupExpr) mg2;
3361 return (MethodGroupExpr) mg1;
3364 MethodGroupExpr left_set = null, right_set = null;
3365 int length1 = 0, length2 = 0;
3367 left_set = (MethodGroupExpr) mg1;
3368 length1 = left_set.Methods.Length;
3370 right_set = (MethodGroupExpr) mg2;
3371 length2 = right_set.Methods.Length;
3373 ArrayList common = new ArrayList ();
3375 foreach (MethodBase l in left_set.Methods){
3376 foreach (MethodBase r in right_set.Methods){
3384 miset = new MemberInfo [length1 + length2 - common.Count];
3385 left_set.Methods.CopyTo (miset, 0);
3389 foreach (MemberInfo mi in right_set.Methods){
3390 if (!common.Contains (mi))
3394 union = new MethodGroupExpr (miset, loc);
3400 /// Determines is the candidate method, if a params method, is applicable
3401 /// in its expanded form to the given set of arguments
3403 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
3407 if (arguments == null)
3410 arg_count = arguments.Count;
3412 ParameterData pd = GetParameterData (candidate);
3414 int pd_count = pd.Count;
3419 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
3422 if (pd_count - 1 > arg_count)
3425 if (pd_count == 1 && arg_count == 0)
3429 // If we have come this far, the case which remains is when the number of parameters
3430 // is less than or equal to the argument count.
3432 for (int i = 0; i < pd_count - 1; ++i) {
3434 Argument a = (Argument) arguments [i];
3436 Parameter.Modifier a_mod = a.GetParameterModifier ();
3437 Parameter.Modifier p_mod = pd.ParameterModifier (i);
3439 if (a_mod == p_mod) {
3441 if (a_mod == Parameter.Modifier.NONE)
3442 if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
3445 if (a_mod == Parameter.Modifier.REF ||
3446 a_mod == Parameter.Modifier.OUT) {
3447 Type pt = pd.ParameterType (i);
3450 pt = TypeManager.LookupType (pt.FullName + "&");
3460 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
3462 for (int i = pd_count - 1; i < arg_count; i++) {
3463 Argument a = (Argument) arguments [i];
3465 if (!StandardConversionExists (a.Expr, element_type))
3473 /// Determines if the candidate method is applicable (section 14.4.2.1)
3474 /// to the given set of arguments
3476 static bool IsApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
3480 if (arguments == null)
3483 arg_count = arguments.Count;
3485 ParameterData pd = GetParameterData (candidate);
3487 int pd_count = pd.Count;
3489 if (arg_count != pd.Count)
3492 for (int i = arg_count; i > 0; ) {
3495 Argument a = (Argument) arguments [i];
3497 Parameter.Modifier a_mod = a.GetParameterModifier ();
3498 Parameter.Modifier p_mod = pd.ParameterModifier (i);
3500 if (a_mod == p_mod ||
3501 (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
3502 if (a_mod == Parameter.Modifier.NONE)
3503 if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
3506 if (a_mod == Parameter.Modifier.REF ||
3507 a_mod == Parameter.Modifier.OUT) {
3508 Type pt = pd.ParameterType (i);
3511 pt = TypeManager.LookupType (pt.FullName + "&");
3526 /// Find the Applicable Function Members (7.4.2.1)
3528 /// me: Method Group expression with the members to select.
3529 /// it might contain constructors or methods (or anything
3530 /// that maps to a method).
3532 /// Arguments: ArrayList containing resolved Argument objects.
3534 /// loc: The location if we want an error to be reported, or a Null
3535 /// location for "probing" purposes.
3537 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3538 /// that is the best match of me on Arguments.
3541 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3542 ArrayList Arguments, Location loc)
3544 ArrayList afm = new ArrayList ();
3545 MethodBase method = null;
3546 Type current_type = null;
3548 ArrayList candidates = new ArrayList ();
3551 foreach (MethodBase candidate in me.Methods){
3554 // If we're going one level higher in the class hierarchy, abort if
3555 // we already found an applicable method.
3556 if (candidate.DeclaringType != current_type) {
3557 current_type = candidate.DeclaringType;
3562 // Check if candidate is applicable (section 14.4.2.1)
3563 if (!IsApplicable (ec, Arguments, candidate))
3566 candidates.Add (candidate);
3567 x = BetterFunction (ec, Arguments, candidate, method, false, loc);
3575 if (Arguments == null)
3578 argument_count = Arguments.Count;
3581 // Now we see if we can find params functions, applicable in their expanded form
3582 // since if they were applicable in their normal form, they would have been selected
3585 bool chose_params_expanded = false;
3587 if (method == null) {
3588 candidates = new ArrayList ();
3589 foreach (MethodBase candidate in me.Methods){
3590 if (!IsParamsMethodApplicable (ec, Arguments, candidate))
3593 candidates.Add (candidate);
3595 int x = BetterFunction (ec, Arguments, candidate, method, true, loc);
3600 chose_params_expanded = true;
3608 // Now check that there are no ambiguities i.e the selected method
3609 // should be better than all the others
3612 foreach (MethodBase candidate in candidates){
3613 if (candidate == method)
3617 // If a normal method is applicable in the sense that it has the same
3618 // number of arguments, then the expanded params method is never applicable
3619 // so we debar the params method.
3621 if (IsParamsMethodApplicable (ec, Arguments, candidate) &&
3622 IsApplicable (ec, Arguments, method))
3625 int x = BetterFunction (ec, Arguments, method, candidate,
3626 chose_params_expanded, loc);
3631 "Ambiguous call when selecting function due to implicit casts");
3637 // And now check if the arguments are all compatible, perform conversions
3638 // if necessary etc. and return if everything is all right
3641 if (VerifyArgumentsCompat (ec, Arguments, argument_count, method,
3642 chose_params_expanded, null, loc))
3648 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
3651 bool chose_params_expanded,
3655 ParameterData pd = GetParameterData (method);
3656 int pd_count = pd.Count;
3658 for (int j = 0; j < argument_count; j++) {
3659 Argument a = (Argument) Arguments [j];
3660 Expression a_expr = a.Expr;
3661 Type parameter_type = pd.ParameterType (j);
3663 if (pd.ParameterModifier (j) == Parameter.Modifier.PARAMS &&
3664 chose_params_expanded)
3665 parameter_type = parameter_type.GetElementType ();
3667 if (a.Type != parameter_type){
3670 conv = ConvertImplicit (ec, a_expr, parameter_type, loc);
3673 if (!Location.IsNull (loc)) {
3674 if (delegate_type == null)
3676 "The best overloaded match for method '" +
3677 FullMethodDesc (method) +
3678 "' has some invalid arguments");
3680 Report.Error (1594, loc,
3681 "Delegate '" + delegate_type.ToString () +
3682 "' has some invalid arguments.");
3684 "Argument " + (j+1) +
3685 ": Cannot convert from '" + Argument.FullDesc (a)
3686 + "' to '" + pd.ParameterDesc (j) + "'");
3693 // Update the argument with the implicit conversion
3699 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
3700 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
3701 if (!Location.IsNull (loc)) {
3702 Console.WriteLine ("A:P: " + a.GetParameterModifier ());
3703 Console.WriteLine ("PP:: " + pd.ParameterModifier (j));
3704 Console.WriteLine ("PT: " + parameter_type.IsByRef);
3706 "The best overloaded match for method '" + FullMethodDesc (method)+
3707 "' has some invalid arguments");
3709 "Argument " + (j+1) +
3710 ": Cannot convert from '" + Argument.FullDesc (a)
3711 + "' to '" + pd.ParameterDesc (j) + "'");
3721 public override Expression DoResolve (EmitContext ec)
3724 // First, resolve the expression that is used to
3725 // trigger the invocation
3727 if (expr is BaseAccess)
3730 expr = expr.Resolve (ec);
3734 if (!(expr is MethodGroupExpr)) {
3735 Type expr_type = expr.Type;
3737 if (expr_type != null){
3738 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
3740 return (new DelegateInvocation (
3741 this.expr, Arguments, loc)).Resolve (ec);
3745 if (!(expr is MethodGroupExpr)){
3746 report118 (loc, this.expr, "method group");
3751 // Next, evaluate all the expressions in the argument list
3753 if (Arguments != null){
3754 foreach (Argument a in Arguments){
3755 if (!a.Resolve (ec, loc))
3760 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
3762 if (method == null){
3764 "Could not find any applicable function for this argument list");
3768 if (method is MethodInfo)
3769 type = TypeManager.TypeToCoreType (((MethodInfo)method).ReturnType);
3771 if (type.IsPointer){
3778 eclass = ExprClass.Value;
3783 // Emits the list of arguments as an array
3785 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
3787 ILGenerator ig = ec.ig;
3788 int count = arguments.Count - idx;
3789 Argument a = (Argument) arguments [idx];
3790 Type t = a.Expr.Type;
3791 string array_type = t.FullName + "[]";
3794 array = ig.DeclareLocal (TypeManager.LookupType (array_type));
3795 IntConstant.EmitInt (ig, count);
3796 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
3797 ig.Emit (OpCodes.Stloc, array);
3799 int top = arguments.Count;
3800 for (int j = idx; j < top; j++){
3801 a = (Argument) arguments [j];
3803 ig.Emit (OpCodes.Ldloc, array);
3804 IntConstant.EmitInt (ig, j - idx);
3807 ArrayAccess.EmitStoreOpcode (ig, t);
3809 ig.Emit (OpCodes.Ldloc, array);
3813 /// Emits a list of resolved Arguments that are in the arguments
3816 /// The MethodBase argument might be null if the
3817 /// emission of the arguments is known not to contain
3818 /// a `params' field (for example in constructors or other routines
3819 /// that keep their arguments in this structure)
3821 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
3825 pd = GetParameterData (mb);
3830 // If we are calling a params method with no arguments, special case it
3832 if (arguments == null){
3833 if (pd != null && pd.Count > 0 &&
3834 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
3835 ILGenerator ig = ec.ig;
3837 IntConstant.EmitInt (ig, 0);
3838 ig.Emit (OpCodes.Newarr, pd.ParameterType (0).GetElementType ());
3844 int top = arguments.Count;
3846 for (int i = 0; i < top; i++){
3847 Argument a = (Argument) arguments [i];
3850 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
3852 // Special case if we are passing the same data as the
3853 // params argument, do not put it in an array.
3855 if (pd.ParameterType (i) == a.Type)
3858 EmitParams (ec, i, arguments);
3868 /// is_base tells whether we want to force the use of the `call'
3869 /// opcode instead of using callvirt. Call is required to call
3870 /// a specific method, while callvirt will always use the most
3871 /// recent method in the vtable.
3873 /// is_static tells whether this is an invocation on a static method
3875 /// instance_expr is an expression that represents the instance
3876 /// it must be non-null if is_static is false.
3878 /// method is the method to invoke.
3880 /// Arguments is the list of arguments to pass to the method or constructor.
3882 public static void EmitCall (EmitContext ec, bool is_base,
3883 bool is_static, Expression instance_expr,
3884 MethodBase method, ArrayList Arguments, Location loc)
3886 ILGenerator ig = ec.ig;
3887 bool struct_call = false;
3889 Type decl_type = method.DeclaringType;
3891 if (!RootContext.StdLib) {
3892 // Replace any calls to the system's System.Array type with calls to
3893 // the newly created one.
3894 if (method == TypeManager.system_int_array_get_length)
3895 method = TypeManager.int_array_get_length;
3896 else if (method == TypeManager.system_int_array_get_rank)
3897 method = TypeManager.int_array_get_rank;
3898 else if (method == TypeManager.system_object_array_clone)
3899 method = TypeManager.object_array_clone;
3900 else if (method == TypeManager.system_int_array_get_length_int)
3901 method = TypeManager.int_array_get_length_int;
3902 else if (method == TypeManager.system_int_array_get_lower_bound_int)
3903 method = TypeManager.int_array_get_lower_bound_int;
3904 else if (method == TypeManager.system_int_array_get_upper_bound_int)
3905 method = TypeManager.int_array_get_upper_bound_int;
3906 else if (method == TypeManager.system_void_array_copyto_array_int)
3907 method = TypeManager.void_array_copyto_array_int;
3911 // This checks the `ConditionalAttribute' on the method, and the
3912 // ObsoleteAttribute
3914 TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (method);
3915 if ((flags & TypeManager.MethodFlags.IsObsolete) != 0){
3917 612, loc, "`" + TypeManager.CSharpSignature (method)+
3920 if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
3924 if (decl_type.IsValueType)
3927 // If this is ourselves, push "this"
3929 if (instance_expr == null){
3930 ig.Emit (OpCodes.Ldarg_0);
3933 // Push the instance expression
3935 if (instance_expr.Type.IsValueType){
3937 // Special case: calls to a function declared in a
3938 // reference-type with a value-type argument need
3939 // to have their value boxed.
3942 if (decl_type.IsValueType){
3944 // If the expression implements IMemoryLocation, then
3945 // we can optimize and use AddressOf on the
3948 // If not we have to use some temporary storage for
3950 if (instance_expr is IMemoryLocation){
3951 ((IMemoryLocation)instance_expr).
3952 AddressOf (ec, AddressOp.LoadStore);
3955 Type t = instance_expr.Type;
3957 instance_expr.Emit (ec);
3958 LocalBuilder temp = ig.DeclareLocal (t);
3959 ig.Emit (OpCodes.Stloc, temp);
3960 ig.Emit (OpCodes.Ldloca, temp);
3963 instance_expr.Emit (ec);
3964 ig.Emit (OpCodes.Box, instance_expr.Type);
3967 instance_expr.Emit (ec);
3971 EmitArguments (ec, method, Arguments);
3973 if (is_static || struct_call || is_base){
3974 if (method is MethodInfo)
3975 ig.Emit (OpCodes.Call, (MethodInfo) method);
3977 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3979 if (method is MethodInfo)
3980 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3982 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3986 public override void Emit (EmitContext ec)
3988 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3991 ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
3994 public override void EmitStatement (EmitContext ec)
3999 // Pop the return value if there is one
4001 if (method is MethodInfo){
4002 Type ret = ((MethodInfo)method).ReturnType;
4003 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
4004 ec.ig.Emit (OpCodes.Pop);
4010 // This class is used to "disable" the code generation for the
4011 // temporary variable when initializing value types.
4013 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
4014 public void AddressOf (EmitContext ec, AddressOp Mode)
4021 /// Implements the new expression
4023 public class New : ExpressionStatement {
4024 public readonly ArrayList Arguments;
4025 public readonly Expression RequestedType;
4028 MethodBase method = null;
4031 // If set, the new expression is for a value_target, and
4032 // we will not leave anything on the stack.
4034 Expression value_target;
4035 bool value_target_set = false;
4037 public New (Expression requested_type, ArrayList arguments, Location l)
4039 RequestedType = requested_type;
4040 Arguments = arguments;
4044 public Expression ValueTypeVariable {
4046 return value_target;
4050 value_target = value;
4051 value_target_set = true;
4056 // This function is used to disable the following code sequence for
4057 // value type initialization:
4059 // AddressOf (temporary)
4063 // Instead the provide will have provided us with the address on the
4064 // stack to store the results.
4066 static Expression MyEmptyExpression;
4068 public void DisableTemporaryValueType ()
4070 if (MyEmptyExpression == null)
4071 MyEmptyExpression = new EmptyAddressOf ();
4074 // To enable this, look into:
4075 // test-34 and test-89 and self bootstrapping.
4077 // For instance, we can avoid a copy by using `newobj'
4078 // instead of Call + Push-temp on value types.
4079 // value_target = MyEmptyExpression;
4082 public override Expression DoResolve (EmitContext ec)
4084 type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
4089 bool IsDelegate = TypeManager.IsDelegateType (type);
4092 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
4094 if (type.IsInterface || type.IsAbstract){
4096 144, loc, "It is not possible to create instances of interfaces " +
4097 "or abstract classes");
4101 bool is_struct = false;
4102 is_struct = type.IsValueType;
4103 eclass = ExprClass.Value;
4106 // SRE returns a match for .ctor () on structs (the object constructor),
4107 // so we have to manually ignore it.
4109 if (is_struct && Arguments == null)
4113 ml = MemberLookupFinal (ec, type, ".ctor",
4114 MemberTypes.Constructor,
4115 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
4120 if (! (ml is MethodGroupExpr)){
4122 report118 (loc, ml, "method group");
4128 if (Arguments != null){
4129 foreach (Argument a in Arguments){
4130 if (!a.Resolve (ec, loc))
4135 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4140 if (method == null) {
4141 if (!is_struct || Arguments.Count > 0) {
4143 "New invocation: Can not find a constructor for " +
4144 "this argument list");
4152 // This DoEmit can be invoked in two contexts:
4153 // * As a mechanism that will leave a value on the stack (new object)
4154 // * As one that wont (init struct)
4156 // You can control whether a value is required on the stack by passing
4157 // need_value_on_stack. The code *might* leave a value on the stack
4158 // so it must be popped manually
4160 // If we are dealing with a ValueType, we have a few
4161 // situations to deal with:
4163 // * The target is a ValueType, and we have been provided
4164 // the instance (this is easy, we are being assigned).
4166 // * The target of New is being passed as an argument,
4167 // to a boxing operation or a function that takes a
4170 // In this case, we need to create a temporary variable
4171 // that is the argument of New.
4173 // Returns whether a value is left on the stack
4175 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4177 bool is_value_type = type.IsValueType;
4178 ILGenerator ig = ec.ig;
4183 // Allow DoEmit() to be called multiple times.
4184 // We need to create a new LocalTemporary each time since
4185 // you can't share LocalBuilders among ILGeneators.
4186 if (!value_target_set)
4187 value_target = new LocalTemporary (ec, type);
4189 ml = (IMemoryLocation) value_target;
4190 ml.AddressOf (ec, AddressOp.Store);
4194 Invocation.EmitArguments (ec, method, Arguments);
4198 ig.Emit (OpCodes.Initobj, type);
4200 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4201 if (need_value_on_stack){
4202 value_target.Emit (ec);
4207 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4212 public override void Emit (EmitContext ec)
4217 public override void EmitStatement (EmitContext ec)
4219 if (DoEmit (ec, false))
4220 ec.ig.Emit (OpCodes.Pop);
4225 /// 14.5.10.2: Represents an array creation expression.
4229 /// There are two possible scenarios here: one is an array creation
4230 /// expression that specifies the dimensions and optionally the
4231 /// initialization data and the other which does not need dimensions
4232 /// specified but where initialization data is mandatory.
4234 public class ArrayCreation : ExpressionStatement {
4235 Expression requested_base_type;
4236 ArrayList initializers;
4240 // The list of Argument types.
4241 // This is used to construct the `newarray' or constructor signature
4243 ArrayList arguments;
4246 // Method used to create the array object.
4248 MethodBase new_method = null;
4250 Type array_element_type;
4251 Type underlying_type;
4252 bool is_one_dimensional = false;
4253 bool is_builtin_type = false;
4254 bool expect_initializers = false;
4255 int num_arguments = 0;
4259 ArrayList array_data;
4264 // The number of array initializers that we can handle
4265 // via the InitializeArray method - through EmitStaticInitializers
4267 int num_automatic_initializers;
4269 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
4271 this.requested_base_type = requested_base_type;
4272 this.initializers = initializers;
4276 arguments = new ArrayList ();
4278 foreach (Expression e in exprs) {
4279 arguments.Add (new Argument (e, Argument.AType.Expression));
4284 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
4286 this.requested_base_type = requested_base_type;
4287 this.initializers = initializers;
4291 //this.rank = rank.Substring (0, rank.LastIndexOf ("["));
4293 //string tmp = rank.Substring (rank.LastIndexOf ("["));
4295 //dimensions = tmp.Length - 1;
4296 expect_initializers = true;
4299 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
4301 StringBuilder sb = new StringBuilder (rank);
4304 for (int i = 1; i < idx_count; i++)
4309 return new ComposedCast (base_type, sb.ToString (), loc);
4314 Report.Error (178, loc, "Incorrectly structured array initializer");
4317 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
4319 if (specified_dims) {
4320 Argument a = (Argument) arguments [idx];
4322 if (!a.Resolve (ec, loc))
4325 if (!(a.Expr is Constant)) {
4326 Report.Error (150, loc, "A constant value is expected");
4330 int value = (int) ((Constant) a.Expr).GetValue ();
4332 if (value != probe.Count) {
4337 bounds [idx] = value;
4340 foreach (object o in probe) {
4341 if (o is ArrayList) {
4342 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
4346 Expression tmp = (Expression) o;
4347 tmp = tmp.Resolve (ec);
4351 // Console.WriteLine ("I got: " + tmp);
4352 // Handle initialization from vars, fields etc.
4354 Expression conv = ConvertImplicitRequired (
4355 ec, tmp, underlying_type, loc);
4360 if (conv is StringConstant)
4361 array_data.Add (conv);
4362 else if (conv is Constant) {
4363 array_data.Add (conv);
4364 num_automatic_initializers++;
4366 array_data.Add (conv);
4373 public void UpdateIndices (EmitContext ec)
4376 for (ArrayList probe = initializers; probe != null;) {
4377 if (probe.Count > 0 && probe [0] is ArrayList) {
4378 Expression e = new IntConstant (probe.Count);
4379 arguments.Add (new Argument (e, Argument.AType.Expression));
4381 bounds [i++] = probe.Count;
4383 probe = (ArrayList) probe [0];
4386 Expression e = new IntConstant (probe.Count);
4387 arguments.Add (new Argument (e, Argument.AType.Expression));
4389 bounds [i++] = probe.Count;
4396 public bool ValidateInitializers (EmitContext ec, Type array_type)
4398 if (initializers == null) {
4399 if (expect_initializers)
4405 if (underlying_type == null)
4409 // We use this to store all the date values in the order in which we
4410 // will need to store them in the byte blob later
4412 array_data = new ArrayList ();
4413 bounds = new Hashtable ();
4417 if (arguments != null) {
4418 ret = CheckIndices (ec, initializers, 0, true);
4421 arguments = new ArrayList ();
4423 ret = CheckIndices (ec, initializers, 0, false);
4430 if (arguments.Count != dimensions) {
4439 void Error_NegativeArrayIndex ()
4441 Report.Error (284, loc, "Can not create array with a negative size");
4445 // Converts `source' to an int, uint, long or ulong.
4447 Expression ExpressionToArrayArgument (EmitContext ec, Expression source)
4451 bool old_checked = ec.CheckState;
4452 ec.CheckState = true;
4454 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
4455 if (target == null){
4456 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
4457 if (target == null){
4458 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
4459 if (target == null){
4460 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
4462 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
4466 ec.CheckState = old_checked;
4469 // Only positive constants are allowed at compile time
4471 if (target is Constant){
4472 if (target is IntConstant){
4473 if (((IntConstant) target).Value < 0){
4474 Error_NegativeArrayIndex ();
4479 if (target is LongConstant){
4480 if (((LongConstant) target).Value < 0){
4481 Error_NegativeArrayIndex ();
4492 // Creates the type of the array
4494 bool LookupType (EmitContext ec)
4496 StringBuilder array_qualifier = new StringBuilder (rank);
4499 // `In the first form allocates an array instace of the type that results
4500 // from deleting each of the individual expression from the expression list'
4502 if (num_arguments > 0) {
4503 array_qualifier.Append ("[");
4504 for (int i = num_arguments-1; i > 0; i--)
4505 array_qualifier.Append (",");
4506 array_qualifier.Append ("]");
4512 Expression array_type_expr;
4513 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
4514 type = ec.DeclSpace.ResolveType (array_type_expr, false, loc);
4519 underlying_type = type;
4520 if (underlying_type.IsArray)
4521 underlying_type = TypeManager.TypeToCoreType (underlying_type.GetElementType ());
4522 dimensions = type.GetArrayRank ();
4527 public override Expression DoResolve (EmitContext ec)
4531 if (!LookupType (ec))
4535 // First step is to validate the initializers and fill
4536 // in any missing bits
4538 if (!ValidateInitializers (ec, type))
4541 if (arguments == null)
4544 arg_count = arguments.Count;
4545 foreach (Argument a in arguments){
4546 if (!a.Resolve (ec, loc))
4549 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
4550 if (real_arg == null)
4557 array_element_type = TypeManager.TypeToCoreType (type.GetElementType ());
4559 if (arg_count == 1) {
4560 is_one_dimensional = true;
4561 eclass = ExprClass.Value;
4565 is_builtin_type = TypeManager.IsBuiltinType (type);
4567 if (is_builtin_type) {
4570 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
4571 AllBindingFlags, loc);
4573 if (!(ml is MethodGroupExpr)) {
4574 report118 (loc, ml, "method group");
4579 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
4580 "this argument list");
4584 new_method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, arguments, loc);
4586 if (new_method == null) {
4587 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
4588 "this argument list");
4592 eclass = ExprClass.Value;
4595 ModuleBuilder mb = CodeGen.ModuleBuilder;
4596 ArrayList args = new ArrayList ();
4598 if (arguments != null) {
4599 for (int i = 0; i < arg_count; i++)
4600 args.Add (TypeManager.int32_type);
4603 Type [] arg_types = null;
4606 arg_types = new Type [args.Count];
4608 args.CopyTo (arg_types, 0);
4610 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
4613 if (new_method == null) {
4614 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
4615 "this argument list");
4619 eclass = ExprClass.Value;
4624 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
4629 int count = array_data.Count;
4631 factor = GetTypeSize (underlying_type);
4635 data = new byte [(count * factor + 4) & ~3];
4638 for (int i = 0; i < count; ++i) {
4639 object v = array_data [i];
4641 if (v is EnumConstant)
4642 v = ((EnumConstant) v).Child;
4644 if (v is Constant && !(v is StringConstant))
4645 v = ((Constant) v).GetValue ();
4651 if (underlying_type == TypeManager.int64_type){
4652 if (!(v is Expression)){
4653 long val = (long) v;
4655 for (int j = 0; j < factor; ++j) {
4656 data [idx + j] = (byte) (val & 0xFF);
4660 } else if (underlying_type == TypeManager.uint64_type){
4661 if (!(v is Expression)){
4662 ulong val = (ulong) v;
4664 for (int j = 0; j < factor; ++j) {
4665 data [idx + j] = (byte) (val & 0xFF);
4669 } else if (underlying_type == TypeManager.float_type) {
4670 if (!(v is Expression)){
4671 element = BitConverter.GetBytes ((float) v);
4673 for (int j = 0; j < factor; ++j)
4674 data [idx + j] = element [j];
4676 } else if (underlying_type == TypeManager.double_type) {
4677 if (!(v is Expression)){
4678 element = BitConverter.GetBytes ((double) v);
4680 for (int j = 0; j < factor; ++j)
4681 data [idx + j] = element [j];
4683 } else if (underlying_type == TypeManager.char_type){
4684 if (!(v is Expression)){
4685 int val = (int) ((char) v);
4687 data [idx] = (byte) (val & 0xff);
4688 data [idx+1] = (byte) (val >> 8);
4690 } else if (underlying_type == TypeManager.short_type){
4691 if (!(v is Expression)){
4692 int val = (int) ((short) v);
4694 data [idx] = (byte) (val & 0xff);
4695 data [idx+1] = (byte) (val >> 8);
4697 } else if (underlying_type == TypeManager.ushort_type){
4698 if (!(v is Expression)){
4699 int val = (int) ((ushort) v);
4701 data [idx] = (byte) (val & 0xff);
4702 data [idx+1] = (byte) (val >> 8);
4704 } else if (underlying_type == TypeManager.int32_type) {
4705 if (!(v is Expression)){
4708 data [idx] = (byte) (val & 0xff);
4709 data [idx+1] = (byte) ((val >> 8) & 0xff);
4710 data [idx+2] = (byte) ((val >> 16) & 0xff);
4711 data [idx+3] = (byte) (val >> 24);
4713 } else if (underlying_type == TypeManager.uint32_type) {
4714 if (!(v is Expression)){
4715 uint val = (uint) v;
4717 data [idx] = (byte) (val & 0xff);
4718 data [idx+1] = (byte) ((val >> 8) & 0xff);
4719 data [idx+2] = (byte) ((val >> 16) & 0xff);
4720 data [idx+3] = (byte) (val >> 24);
4722 } else if (underlying_type == TypeManager.sbyte_type) {
4723 if (!(v is Expression)){
4724 sbyte val = (sbyte) v;
4725 data [idx] = (byte) val;
4727 } else if (underlying_type == TypeManager.byte_type) {
4728 if (!(v is Expression)){
4729 byte val = (byte) v;
4730 data [idx] = (byte) val;
4732 } else if (underlying_type == TypeManager.bool_type) {
4733 if (!(v is Expression)){
4734 bool val = (bool) v;
4735 data [idx] = (byte) (val ? 1 : 0);
4738 throw new Exception ("Unrecognized type in MakeByteBlob");
4747 // Emits the initializers for the array
4749 void EmitStaticInitializers (EmitContext ec, bool is_expression)
4752 // First, the static data
4755 ILGenerator ig = ec.ig;
4757 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
4760 fb = RootContext.MakeStaticData (data);
4763 ig.Emit (OpCodes.Dup);
4764 ig.Emit (OpCodes.Ldtoken, fb);
4765 ig.Emit (OpCodes.Call,
4766 TypeManager.void_initializearray_array_fieldhandle);
4771 // Emits pieces of the array that can not be computed at compile
4772 // time (variables and string locations).
4774 // This always expect the top value on the stack to be the array
4776 void EmitDynamicInitializers (EmitContext ec, bool is_expression)
4778 ILGenerator ig = ec.ig;
4779 int dims = bounds.Count;
4780 int [] current_pos = new int [dims];
4781 int top = array_data.Count;
4782 LocalBuilder temp = ig.DeclareLocal (type);
4784 ig.Emit (OpCodes.Stloc, temp);
4786 MethodInfo set = null;
4790 ModuleBuilder mb = null;
4791 mb = CodeGen.ModuleBuilder;
4792 args = new Type [dims + 1];
4795 for (j = 0; j < dims; j++)
4796 args [j] = TypeManager.int32_type;
4798 args [j] = array_element_type;
4800 set = mb.GetArrayMethod (
4802 CallingConventions.HasThis | CallingConventions.Standard,
4803 TypeManager.void_type, args);
4806 for (int i = 0; i < top; i++){
4808 Expression e = null;
4810 if (array_data [i] is Expression)
4811 e = (Expression) array_data [i];
4815 // Basically we do this for string literals and
4816 // other non-literal expressions
4818 if (e is StringConstant || !(e is Constant) ||
4819 num_automatic_initializers <= 2) {
4820 Type etype = e.Type;
4822 ig.Emit (OpCodes.Ldloc, temp);
4824 for (int idx = dims; idx > 0; ) {
4826 IntConstant.EmitInt (ig, current_pos [idx]);
4830 // If we are dealing with a struct, get the
4831 // address of it, so we can store it.
4834 etype.IsSubclassOf (TypeManager.value_type) &&
4835 (!TypeManager.IsBuiltinType (etype) ||
4836 etype == TypeManager.decimal_type)) {
4841 // Let new know that we are providing
4842 // the address where to store the results
4844 n.DisableTemporaryValueType ();
4847 ig.Emit (OpCodes.Ldelema, etype);
4853 ArrayAccess.EmitStoreOpcode (ig, array_element_type);
4855 ig.Emit (OpCodes.Call, set);
4862 for (int j = 0; j < dims; j++){
4864 if (current_pos [j] < (int) bounds [j])
4866 current_pos [j] = 0;
4871 ig.Emit (OpCodes.Ldloc, temp);
4874 void EmitArrayArguments (EmitContext ec)
4876 ILGenerator ig = ec.ig;
4878 foreach (Argument a in arguments) {
4879 Type atype = a.Type;
4882 if (atype == TypeManager.uint64_type)
4883 ig.Emit (OpCodes.Conv_Ovf_U4);
4884 else if (atype == TypeManager.int64_type)
4885 ig.Emit (OpCodes.Conv_Ovf_I4);
4889 void DoEmit (EmitContext ec, bool is_statement)
4891 ILGenerator ig = ec.ig;
4893 EmitArrayArguments (ec);
4894 if (is_one_dimensional)
4895 ig.Emit (OpCodes.Newarr, array_element_type);
4897 if (is_builtin_type)
4898 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
4900 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
4903 if (initializers != null){
4905 // FIXME: Set this variable correctly.
4907 bool dynamic_initializers = true;
4909 if (underlying_type != TypeManager.string_type &&
4910 underlying_type != TypeManager.object_type) {
4911 if (num_automatic_initializers > 2)
4912 EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
4915 if (dynamic_initializers)
4916 EmitDynamicInitializers (ec, !is_statement);
4920 public override void Emit (EmitContext ec)
4925 public override void EmitStatement (EmitContext ec)
4933 /// Represents the `this' construct
4935 public class This : Expression, IAssignMethod, IMemoryLocation {
4938 public This (Location loc)
4943 public override Expression DoResolve (EmitContext ec)
4945 eclass = ExprClass.Variable;
4946 type = ec.ContainerType;
4949 Report.Error (26, loc,
4950 "Keyword this not valid in static code");
4957 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4961 if (ec.TypeContainer is Class){
4962 Report.Error (1604, loc, "Cannot assign to `this'");
4969 public override void Emit (EmitContext ec)
4971 ILGenerator ig = ec.ig;
4973 ig.Emit (OpCodes.Ldarg_0);
4974 if (ec.TypeContainer is Struct)
4975 ig.Emit (OpCodes.Ldobj, type);
4978 public void EmitAssign (EmitContext ec, Expression source)
4980 ILGenerator ig = ec.ig;
4982 if (ec.TypeContainer is Struct){
4983 ig.Emit (OpCodes.Ldarg_0);
4985 ig.Emit (OpCodes.Stobj, type);
4988 ig.Emit (OpCodes.Starg, 0);
4992 public void AddressOf (EmitContext ec, AddressOp mode)
4994 ec.ig.Emit (OpCodes.Ldarg_0);
4997 // FIGURE OUT WHY LDARG_S does not work
4999 // consider: struct X { int val; int P { set { val = value; }}}
5001 // Yes, this looks very bad. Look at `NOTAS' for
5003 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
5008 /// Implements the typeof operator
5010 public class TypeOf : Expression {
5011 public readonly Expression QueriedType;
5015 public TypeOf (Expression queried_type, Location l)
5017 QueriedType = queried_type;
5021 public override Expression DoResolve (EmitContext ec)
5023 typearg = ec.DeclSpace.ResolveType (QueriedType, false, loc);
5025 if (typearg == null)
5028 type = TypeManager.type_type;
5029 eclass = ExprClass.Type;
5033 public override void Emit (EmitContext ec)
5035 ec.ig.Emit (OpCodes.Ldtoken, typearg);
5036 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
5039 public Type TypeArg {
5040 get { return typearg; }
5045 /// Implements the sizeof expression
5047 public class SizeOf : Expression {
5048 public readonly Expression QueriedType;
5052 public SizeOf (Expression queried_type, Location l)
5054 this.QueriedType = queried_type;
5058 public override Expression DoResolve (EmitContext ec)
5060 type_queried = ec.DeclSpace.ResolveType (QueriedType, false, loc);
5061 if (type_queried == null)
5064 type = TypeManager.int32_type;
5065 eclass = ExprClass.Value;
5069 public override void Emit (EmitContext ec)
5071 int size = GetTypeSize (type_queried);
5074 ec.ig.Emit (OpCodes.Sizeof, type_queried);
5076 IntConstant.EmitInt (ec.ig, size);
5081 /// Implements the member access expression
5083 public class MemberAccess : Expression {
5084 public readonly string Identifier;
5086 Expression member_lookup;
5089 public MemberAccess (Expression expr, string id, Location l)
5096 public Expression Expr {
5102 static void error176 (Location loc, string name)
5104 Report.Error (176, loc, "Static member `" +
5105 name + "' cannot be accessed " +
5106 "with an instance reference, qualify with a " +
5107 "type name instead");
5110 static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
5112 if (left_original == null)
5115 if (!(left_original is SimpleName))
5118 SimpleName sn = (SimpleName) left_original;
5120 Type t = RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc);
5127 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
5128 Expression left, Location loc,
5129 Expression left_original)
5134 if (member_lookup is MethodGroupExpr){
5135 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
5140 if (left is TypeExpr){
5141 if (!mg.RemoveInstanceMethods ()){
5142 SimpleName.Error_ObjectRefRequired (loc, mg.Methods [0].Name);
5146 return member_lookup;
5150 // Instance.MethodGroup
5152 if (IdenticalNameAndTypeName (ec, left_original, loc)){
5153 if (mg.RemoveInstanceMethods ())
5154 return member_lookup;
5157 if (!mg.RemoveStaticMethods ()){
5158 error176 (loc, mg.Methods [0].Name);
5162 mg.InstanceExpression = left;
5163 return member_lookup;
5165 if (!mg.RemoveStaticMethods ()){
5166 if (IdenticalNameAndTypeName (ec, left_original, loc)){
5167 if (!mg.RemoveInstanceMethods ()){
5168 SimpleName.Error_ObjectRefRequired (loc, mg.Methods [0].Name);
5171 return member_lookup;
5174 error176 (loc, mg.Methods [0].Name);
5178 mg.InstanceExpression = left;
5180 return member_lookup;
5184 if (member_lookup is FieldExpr){
5185 FieldExpr fe = (FieldExpr) member_lookup;
5186 FieldInfo fi = fe.FieldInfo;
5187 Type decl_type = fi.DeclaringType;
5189 if (fi is FieldBuilder) {
5190 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
5193 object o = c.LookupConstantValue (ec);
5194 object real_value = ((Constant) c.Expr).GetValue ();
5196 return Constantify (real_value, fi.FieldType);
5201 Type t = fi.FieldType;
5205 if (fi is FieldBuilder)
5206 o = TypeManager.GetValue ((FieldBuilder) fi);
5208 o = fi.GetValue (fi);
5210 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
5211 Expression enum_member = MemberLookup (
5212 ec, decl_type, "value__", MemberTypes.Field,
5213 AllBindingFlags, loc);
5215 Enum en = TypeManager.LookupEnum (decl_type);
5219 c = Constantify (o, en.UnderlyingType);
5221 c = Constantify (o, enum_member.Type);
5223 return new EnumConstant (c, decl_type);
5226 Expression exp = Constantify (o, t);
5228 if (!(left is TypeExpr)) {
5229 error176 (loc, fe.FieldInfo.Name);
5236 if (fi.FieldType.IsPointer && !ec.InUnsafe){
5241 if (left is TypeExpr){
5242 // and refers to a type name or an
5243 if (!fe.FieldInfo.IsStatic){
5244 error176 (loc, fe.FieldInfo.Name);
5247 return member_lookup;
5249 if (fe.FieldInfo.IsStatic){
5250 if (IdenticalNameAndTypeName (ec, left_original, loc))
5251 return member_lookup;
5253 error176 (loc, fe.FieldInfo.Name);
5258 // Since we can not check for instance objects in SimpleName,
5259 // becaue of the rule that allows types and variables to share
5260 // the name (as long as they can be de-ambiguated later, see
5261 // IdenticalNameAndTypeName), we have to check whether left
5262 // is an instance variable in a static context
5265 if (ec.IsStatic && left is FieldExpr){
5266 FieldExpr fexp = (FieldExpr) left;
5268 if (!fexp.FieldInfo.IsStatic){
5269 SimpleName.Error_ObjectRefRequired (loc, fexp.FieldInfo.Name);
5273 fe.InstanceExpression = left;
5279 if (member_lookup is PropertyExpr){
5280 PropertyExpr pe = (PropertyExpr) member_lookup;
5282 if (left is TypeExpr){
5284 SimpleName.Error_ObjectRefRequired (loc, pe.PropertyInfo.Name);
5290 if (IdenticalNameAndTypeName (ec, left_original, loc))
5291 return member_lookup;
5292 error176 (loc, pe.PropertyInfo.Name);
5295 pe.InstanceExpression = left;
5301 if (member_lookup is EventExpr) {
5303 EventExpr ee = (EventExpr) member_lookup;
5306 // If the event is local to this class, we transform ourselves into
5310 Expression ml = MemberLookup (
5311 ec, ec.ContainerType, ee.EventInfo.Name, MemberTypes.Event,
5312 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5315 MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
5319 // If this happens, then we have an event with its own
5320 // accessors and private field etc so there's no need
5321 // to transform ourselves : we should instead flag an error
5323 Assign.error70 (ee.EventInfo, loc);
5327 ml = ExprClassFromMemberInfo (ec, mi, loc);
5330 Report.Error (-200, loc, "Internal error!!");
5333 return ResolveMemberAccess (ec, ml, left, loc, left_original);
5336 if (left is TypeExpr) {
5338 SimpleName.Error_ObjectRefRequired (loc, ee.EventInfo.Name);
5346 if (IdenticalNameAndTypeName (ec, left_original, loc))
5349 error176 (loc, ee.EventInfo.Name);
5353 ee.InstanceExpression = left;
5359 if (member_lookup is TypeExpr){
5360 member_lookup.Resolve (ec);
5361 return member_lookup;
5364 Console.WriteLine ("Left is: " + left);
5365 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
5366 Environment.Exit (0);
5370 public override Expression DoResolve (EmitContext ec)
5373 throw new Exception ();
5375 // We are the sole users of ResolveWithSimpleName (ie, the only
5376 // ones that can cope with it)
5378 Expression original = expr;
5379 expr = expr.ResolveWithSimpleName (ec);
5384 if (expr is SimpleName){
5385 SimpleName child_expr = (SimpleName) expr;
5387 Expression new_expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
5389 return new_expr.ResolveWithSimpleName (ec);
5393 // TODO: I mailed Ravi about this, and apparently we can get rid
5394 // of this and put it in the right place.
5396 // Handle enums here when they are in transit.
5397 // Note that we cannot afford to hit MemberLookup in this case because
5398 // it will fail to find any members at all
5401 Type expr_type = expr.Type;
5402 if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))){
5404 Enum en = TypeManager.LookupEnum (expr_type);
5407 object value = en.LookupEnumValue (ec, Identifier, loc);
5410 Constant c = Constantify (value, en.UnderlyingType);
5411 return new EnumConstant (c, expr_type);
5416 if (expr_type.IsPointer){
5417 Report.Error (23, loc,
5418 "The `.' operator can not be applied to pointer operands (" +
5419 TypeManager.CSharpName (expr_type) + ")");
5423 member_lookup = MemberLookup (ec, expr_type, Identifier, loc);
5425 if (member_lookup == null){
5427 // Try looking the member up from the same type, if we find
5428 // it, we know that the error was due to limited visibility
5430 object lookup = TypeManager.MemberLookup (
5431 expr_type, expr_type, AllMemberTypes, AllBindingFlags, Identifier);
5433 Report.Error (117, loc, "`" + expr_type + "' does not contain a " +
5434 "definition for `" + Identifier + "'");
5436 Report.Error (122, loc, "`" + expr_type + "." + Identifier + "' " +
5437 "is inaccessible because of its protection level");
5442 return ResolveMemberAccess (ec, member_lookup, expr, loc, original);
5445 public override void Emit (EmitContext ec)
5447 throw new Exception ("Should not happen");
5450 public override string ToString ()
5452 return expr + "." + Identifier;
5457 /// Implements checked expressions
5459 public class CheckedExpr : Expression {
5461 public Expression Expr;
5463 public CheckedExpr (Expression e)
5468 public override Expression DoResolve (EmitContext ec)
5470 bool last_const_check = ec.ConstantCheckState;
5472 ec.ConstantCheckState = true;
5473 Expr = Expr.Resolve (ec);
5474 ec.ConstantCheckState = last_const_check;
5479 eclass = Expr.eclass;
5484 public override void Emit (EmitContext ec)
5486 bool last_check = ec.CheckState;
5487 bool last_const_check = ec.ConstantCheckState;
5489 ec.CheckState = true;
5490 ec.ConstantCheckState = true;
5492 ec.CheckState = last_check;
5493 ec.ConstantCheckState = last_const_check;
5499 /// Implements the unchecked expression
5501 public class UnCheckedExpr : Expression {
5503 public Expression Expr;
5505 public UnCheckedExpr (Expression e)
5510 public override Expression DoResolve (EmitContext ec)
5512 bool last_const_check = ec.ConstantCheckState;
5514 ec.ConstantCheckState = false;
5515 Expr = Expr.Resolve (ec);
5516 ec.ConstantCheckState = last_const_check;
5521 eclass = Expr.eclass;
5526 public override void Emit (EmitContext ec)
5528 bool last_check = ec.CheckState;
5529 bool last_const_check = ec.ConstantCheckState;
5531 ec.CheckState = false;
5532 ec.ConstantCheckState = false;
5534 ec.CheckState = last_check;
5535 ec.ConstantCheckState = last_const_check;
5541 /// An Element Access expression.
5543 /// During semantic analysis these are transformed into
5544 /// IndexerAccess or ArrayAccess
5546 public class ElementAccess : Expression {
5547 public ArrayList Arguments;
5548 public Expression Expr;
5549 public Location loc;
5551 public ElementAccess (Expression e, ArrayList e_list, Location l)
5560 Arguments = new ArrayList ();
5561 foreach (Expression tmp in e_list)
5562 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
5566 bool CommonResolve (EmitContext ec)
5568 Expr = Expr.Resolve (ec);
5573 if (Arguments == null)
5576 foreach (Argument a in Arguments){
5577 if (!a.Resolve (ec, loc))
5584 Expression MakePointerAccess ()
5588 if (t == TypeManager.void_ptr_type){
5591 "The array index operation is not valid for void pointers");
5594 if (Arguments.Count != 1){
5597 "A pointer must be indexed by a single value");
5600 Expression p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t);
5601 return new Indirection (p);
5604 public override Expression DoResolve (EmitContext ec)
5606 if (!CommonResolve (ec))
5610 // We perform some simple tests, and then to "split" the emit and store
5611 // code we create an instance of a different class, and return that.
5613 // I am experimenting with this pattern.
5618 return (new ArrayAccess (this)).Resolve (ec);
5619 else if (t.IsPointer)
5620 return MakePointerAccess ();
5622 return (new IndexerAccess (this)).Resolve (ec);
5625 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5627 if (!CommonResolve (ec))
5632 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
5633 else if (t.IsPointer)
5634 return MakePointerAccess ();
5636 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
5639 public override void Emit (EmitContext ec)
5641 throw new Exception ("Should never be reached");
5646 /// Implements array access
5648 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
5650 // Points to our "data" repository
5654 LocalTemporary [] cached_locations;
5656 public ArrayAccess (ElementAccess ea_data)
5659 eclass = ExprClass.Variable;
5662 public override Expression DoResolve (EmitContext ec)
5664 ExprClass eclass = ea.Expr.eclass;
5667 // As long as the type is valid
5668 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
5669 eclass == ExprClass.Value)) {
5670 report118 (ea.loc, ea.Expr, "variable or value");
5675 Type t = ea.Expr.Type;
5676 if (t.GetArrayRank () != ea.Arguments.Count){
5677 Report.Error (22, ea.loc,
5678 "Incorrect number of indexes for array " +
5679 " expected: " + t.GetArrayRank () + " got: " +
5680 ea.Arguments.Count);
5683 type = TypeManager.TypeToCoreType (t.GetElementType ());
5684 if (type.IsPointer && !ec.InUnsafe){
5685 UnsafeError (ea.loc);
5689 foreach (Argument a in ea.Arguments){
5690 Type argtype = a.Type;
5692 if (argtype == TypeManager.int32_type ||
5693 argtype == TypeManager.uint32_type ||
5694 argtype == TypeManager.int64_type ||
5695 argtype == TypeManager.uint64_type)
5699 // Mhm. This is strage, because the Argument.Type is not the same as
5700 // Argument.Expr.Type: the value changes depending on the ref/out setting.
5702 // Wonder if I will run into trouble for this.
5704 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.loc);
5709 eclass = ExprClass.Variable;
5715 /// Emits the right opcode to load an object of Type `t'
5716 /// from an array of T
5718 static public void EmitLoadOpcode (ILGenerator ig, Type type)
5720 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
5721 ig.Emit (OpCodes.Ldelem_U1);
5722 else if (type == TypeManager.sbyte_type)
5723 ig.Emit (OpCodes.Ldelem_I1);
5724 else if (type == TypeManager.short_type)
5725 ig.Emit (OpCodes.Ldelem_I2);
5726 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
5727 ig.Emit (OpCodes.Ldelem_U2);
5728 else if (type == TypeManager.int32_type)
5729 ig.Emit (OpCodes.Ldelem_I4);
5730 else if (type == TypeManager.uint32_type)
5731 ig.Emit (OpCodes.Ldelem_U4);
5732 else if (type == TypeManager.uint64_type)
5733 ig.Emit (OpCodes.Ldelem_I8);
5734 else if (type == TypeManager.int64_type)
5735 ig.Emit (OpCodes.Ldelem_I8);
5736 else if (type == TypeManager.float_type)
5737 ig.Emit (OpCodes.Ldelem_R4);
5738 else if (type == TypeManager.double_type)
5739 ig.Emit (OpCodes.Ldelem_R8);
5740 else if (type == TypeManager.intptr_type)
5741 ig.Emit (OpCodes.Ldelem_I);
5742 else if (type.IsValueType){
5743 ig.Emit (OpCodes.Ldelema, type);
5744 ig.Emit (OpCodes.Ldobj, type);
5746 ig.Emit (OpCodes.Ldelem_Ref);
5750 /// Emits the right opcode to store an object of Type `t'
5751 /// from an array of T.
5753 static public void EmitStoreOpcode (ILGenerator ig, Type t)
5755 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
5756 t == TypeManager.bool_type)
5757 ig.Emit (OpCodes.Stelem_I1);
5758 else if (t == TypeManager.short_type || t == TypeManager.ushort_type || t == TypeManager.char_type)
5759 ig.Emit (OpCodes.Stelem_I2);
5760 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
5761 ig.Emit (OpCodes.Stelem_I4);
5762 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
5763 ig.Emit (OpCodes.Stelem_I8);
5764 else if (t == TypeManager.float_type)
5765 ig.Emit (OpCodes.Stelem_R4);
5766 else if (t == TypeManager.double_type)
5767 ig.Emit (OpCodes.Stelem_R8);
5768 else if (t == TypeManager.intptr_type)
5769 ig.Emit (OpCodes.Stelem_I);
5770 else if (t.IsValueType)
5771 ig.Emit (OpCodes.Stobj, TypeManager.TypeToCoreType (t));
5773 ig.Emit (OpCodes.Stelem_Ref);
5776 MethodInfo FetchGetMethod ()
5778 ModuleBuilder mb = CodeGen.ModuleBuilder;
5779 int arg_count = ea.Arguments.Count;
5780 Type [] args = new Type [arg_count];
5783 for (int i = 0; i < arg_count; i++){
5784 //args [i++] = a.Type;
5785 args [i] = TypeManager.int32_type;
5788 get = mb.GetArrayMethod (
5789 ea.Expr.Type, "Get",
5790 CallingConventions.HasThis |
5791 CallingConventions.Standard,
5797 MethodInfo FetchAddressMethod ()
5799 ModuleBuilder mb = CodeGen.ModuleBuilder;
5800 int arg_count = ea.Arguments.Count;
5801 Type [] args = new Type [arg_count];
5803 string ptr_type_name;
5806 ptr_type_name = type.FullName + "&";
5807 ret_type = Type.GetType (ptr_type_name);
5810 // It is a type defined by the source code we are compiling
5812 if (ret_type == null){
5813 ret_type = mb.GetType (ptr_type_name);
5816 for (int i = 0; i < arg_count; i++){
5817 //args [i++] = a.Type;
5818 args [i] = TypeManager.int32_type;
5821 address = mb.GetArrayMethod (
5822 ea.Expr.Type, "Address",
5823 CallingConventions.HasThis |
5824 CallingConventions.Standard,
5831 // Load the array arguments into the stack.
5833 // If we have been requested to cache the values (cached_locations array
5834 // initialized), then load the arguments the first time and store them
5835 // in locals. otherwise load from local variables.
5837 void LoadArrayAndArguments (EmitContext ec)
5839 ILGenerator ig = ec.ig;
5841 if (cached_locations == null){
5843 foreach (Argument a in ea.Arguments){
5844 Type argtype = a.Expr.Type;
5848 if (argtype == TypeManager.int64_type)
5849 ig.Emit (OpCodes.Conv_Ovf_I);
5850 else if (argtype == TypeManager.uint64_type)
5851 ig.Emit (OpCodes.Conv_Ovf_I_Un);
5856 if (cached_locations [0] == null){
5857 cached_locations [0] = new LocalTemporary (ec, ea.Expr.Type);
5859 ig.Emit (OpCodes.Dup);
5860 cached_locations [0].Store (ec);
5864 foreach (Argument a in ea.Arguments){
5865 Type argtype = a.Expr.Type;
5867 cached_locations [j] = new LocalTemporary (ec, TypeManager.intptr_type /* a.Expr.Type */);
5869 if (argtype == TypeManager.int64_type)
5870 ig.Emit (OpCodes.Conv_Ovf_I);
5871 else if (argtype == TypeManager.uint64_type)
5872 ig.Emit (OpCodes.Conv_Ovf_I_Un);
5874 ig.Emit (OpCodes.Dup);
5875 cached_locations [j].Store (ec);
5881 foreach (LocalTemporary lt in cached_locations)
5885 public new void CacheTemporaries (EmitContext ec)
5887 cached_locations = new LocalTemporary [ea.Arguments.Count + 1];
5890 public override void Emit (EmitContext ec)
5892 int rank = ea.Expr.Type.GetArrayRank ();
5893 ILGenerator ig = ec.ig;
5895 LoadArrayAndArguments (ec);
5898 EmitLoadOpcode (ig, type);
5902 method = FetchGetMethod ();
5903 ig.Emit (OpCodes.Call, method);
5907 public void EmitAssign (EmitContext ec, Expression source)
5909 int rank = ea.Expr.Type.GetArrayRank ();
5910 ILGenerator ig = ec.ig;
5911 Type t = source.Type;
5913 LoadArrayAndArguments (ec);
5916 // The stobj opcode used by value types will need
5917 // an address on the stack, not really an array/array
5921 if (t.IsValueType && !TypeManager.IsBuiltinType (t))
5922 ig.Emit (OpCodes.Ldelema, t);
5928 EmitStoreOpcode (ig, t);
5930 ModuleBuilder mb = CodeGen.ModuleBuilder;
5931 int arg_count = ea.Arguments.Count;
5932 Type [] args = new Type [arg_count + 1];
5935 for (int i = 0; i < arg_count; i++){
5936 //args [i++] = a.Type;
5937 args [i] = TypeManager.int32_type;
5940 args [arg_count] = type;
5942 set = mb.GetArrayMethod (
5943 ea.Expr.Type, "Set",
5944 CallingConventions.HasThis |
5945 CallingConventions.Standard,
5946 TypeManager.void_type, args);
5948 ig.Emit (OpCodes.Call, set);
5952 public void AddressOf (EmitContext ec, AddressOp mode)
5954 int rank = ea.Expr.Type.GetArrayRank ();
5955 ILGenerator ig = ec.ig;
5957 LoadArrayAndArguments (ec);
5960 ig.Emit (OpCodes.Ldelema, type);
5962 MethodInfo address = FetchAddressMethod ();
5963 ig.Emit (OpCodes.Call, address);
5970 public ArrayList getters, setters;
5971 static Hashtable map;
5975 map = new Hashtable ();
5978 Indexers (MemberInfo [] mi)
5980 foreach (PropertyInfo property in mi){
5981 MethodInfo get, set;
5983 get = property.GetGetMethod (true);
5985 if (getters == null)
5986 getters = new ArrayList ();
5991 set = property.GetSetMethod (true);
5993 if (setters == null)
5994 setters = new ArrayList ();
6000 static private Indexers GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
6002 Indexers ix = (Indexers) map [lookup_type];
6007 string p_name = TypeManager.IndexerPropertyName (lookup_type);
6009 MemberInfo [] mi = TypeManager.MemberLookup (
6010 caller_type, lookup_type, MemberTypes.Property,
6011 BindingFlags.Public | BindingFlags.Instance, p_name);
6013 if (mi == null || mi.Length == 0)
6016 ix = new Indexers (mi);
6017 map [lookup_type] = ix;
6022 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
6024 Indexers ix = (Indexers) map [lookup_type];
6029 ix = GetIndexersForTypeOrInterface (caller_type, lookup_type);
6033 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
6034 if (ifaces != null) {
6035 foreach (Type itype in ifaces) {
6036 ix = GetIndexersForTypeOrInterface (caller_type, itype);
6042 Report.Error (21, loc,
6043 "Type `" + TypeManager.CSharpName (lookup_type) +
6044 "' does not have any indexers defined");
6050 /// Expressions that represent an indexer call.
6052 public class IndexerAccess : Expression, IAssignMethod {
6054 // Points to our "data" repository
6057 MethodInfo get, set;
6059 ArrayList set_arguments;
6061 public IndexerAccess (ElementAccess ea_data)
6064 eclass = ExprClass.Value;
6067 public override Expression DoResolve (EmitContext ec)
6069 Type indexer_type = ea.Expr.Type;
6072 // Step 1: Query for all `Item' *properties*. Notice
6073 // that the actual methods are pointed from here.
6075 // This is a group of properties, piles of them.
6078 ilist = Indexers.GetIndexersForType (
6079 ec.ContainerType, indexer_type, ea.loc);
6083 // Step 2: find the proper match
6085 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0){
6086 Location loc = ea.loc;
6088 get = (MethodInfo) Invocation.OverloadResolve (
6089 ec, new MethodGroupExpr (ilist.getters, loc), ea.Arguments, loc);
6093 Report.Error (154, ea.loc,
6094 "indexer can not be used in this context, because " +
6095 "it lacks a `get' accessor");
6099 type = get.ReturnType;
6100 if (type.IsPointer && !ec.InUnsafe){
6101 UnsafeError (ea.loc);
6105 eclass = ExprClass.IndexerAccess;
6109 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
6111 Type indexer_type = ea.Expr.Type;
6112 Type right_type = right_side.Type;
6115 ilist = Indexers.GetIndexersForType (
6116 ec.ContainerType, indexer_type, ea.loc);
6118 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
6119 Location loc = ea.loc;
6121 set_arguments = (ArrayList) ea.Arguments.Clone ();
6122 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
6124 set = (MethodInfo) Invocation.OverloadResolve (
6125 ec, new MethodGroupExpr (ilist.setters, loc), set_arguments, loc);
6129 Report.Error (200, ea.loc,
6130 "indexer X.this [" + TypeManager.CSharpName (right_type) +
6131 "] lacks a `set' accessor");
6135 type = TypeManager.void_type;
6136 eclass = ExprClass.IndexerAccess;
6140 public override void Emit (EmitContext ec)
6142 Invocation.EmitCall (ec, false, false, ea.Expr, get, ea.Arguments, ea.loc);
6146 // source is ignored, because we already have a copy of it from the
6147 // LValue resolution and we have already constructed a pre-cached
6148 // version of the arguments (ea.set_arguments);
6150 public void EmitAssign (EmitContext ec, Expression source)
6152 Invocation.EmitCall (ec, false, false, ea.Expr, set, set_arguments, ea.loc);
6157 /// The base operator for method names
6159 public class BaseAccess : Expression {
6163 public BaseAccess (string member, Location l)
6165 this.member = member;
6169 public override Expression DoResolve (EmitContext ec)
6171 Expression member_lookup;
6172 Type current_type = ec.ContainerType;
6173 Type base_type = current_type.BaseType;
6177 Report.Error (1511, loc,
6178 "Keyword base is not allowed in static method");
6182 member_lookup = MemberLookup (ec, base_type, member, loc);
6183 if (member_lookup == null)
6189 left = new TypeExpr (base_type);
6193 e = MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
6194 if (e is PropertyExpr){
6195 PropertyExpr pe = (PropertyExpr) e;
6203 public override void Emit (EmitContext ec)
6205 throw new Exception ("Should never be called");
6210 /// The base indexer operator
6212 public class BaseIndexerAccess : Expression {
6213 ArrayList Arguments;
6216 public BaseIndexerAccess (ArrayList args, Location l)
6222 public override Expression DoResolve (EmitContext ec)
6224 Type current_type = ec.ContainerType;
6225 Type base_type = current_type.BaseType;
6226 Expression member_lookup;
6229 Report.Error (1511, loc,
6230 "Keyword base is not allowed in static method");
6234 member_lookup = MemberLookup (ec, base_type, "get_Item", MemberTypes.Method, AllBindingFlags, loc);
6235 if (member_lookup == null)
6238 return MemberAccess.ResolveMemberAccess (ec, member_lookup, ec.This, loc, null);
6241 public override void Emit (EmitContext ec)
6243 throw new Exception ("Should never be called");
6248 /// This class exists solely to pass the Type around and to be a dummy
6249 /// that can be passed to the conversion functions (this is used by
6250 /// foreach implementation to typecast the object return value from
6251 /// get_Current into the proper type. All code has been generated and
6252 /// we only care about the side effect conversions to be performed
6254 /// This is also now used as a placeholder where a no-action expression
6255 /// is needed (the `New' class).
6257 public class EmptyExpression : Expression {
6258 public EmptyExpression ()
6260 type = TypeManager.object_type;
6261 eclass = ExprClass.Value;
6264 public EmptyExpression (Type t)
6267 eclass = ExprClass.Value;
6270 public override Expression DoResolve (EmitContext ec)
6275 public override void Emit (EmitContext ec)
6277 // nothing, as we only exist to not do anything.
6281 // This is just because we might want to reuse this bad boy
6282 // instead of creating gazillions of EmptyExpressions.
6283 // (CanConvertImplicit uses it)
6285 public void SetType (Type t)
6291 public class UserCast : Expression {
6295 public UserCast (MethodInfo method, Expression source)
6297 this.method = method;
6298 this.source = source;
6299 type = method.ReturnType;
6300 eclass = ExprClass.Value;
6303 public override Expression DoResolve (EmitContext ec)
6306 // We are born fully resolved
6311 public override void Emit (EmitContext ec)
6313 ILGenerator ig = ec.ig;
6317 if (method is MethodInfo)
6318 ig.Emit (OpCodes.Call, (MethodInfo) method);
6320 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
6326 // This class is used to "construct" the type during a typecast
6327 // operation. Since the Type.GetType class in .NET can parse
6328 // the type specification, we just use this to construct the type
6329 // one bit at a time.
6331 public class ComposedCast : Expression {
6336 public ComposedCast (Expression left, string dim, Location l)
6343 public override Expression DoResolve (EmitContext ec)
6345 left = left.Resolve (ec);
6349 if (left.eclass != ExprClass.Type){
6350 report118 (loc, left, "type");
6354 type = RootContext.LookupType (
6355 ec.DeclSpace, left.Type.FullName + dim, false, loc);
6359 if (!ec.ResolvingTypeTree){
6361 // If the above flag is set, this is being invoked from the ResolveType function.
6362 // Upper layers take care of the type validity in this context.
6364 if (!ec.InUnsafe && type.IsPointer){
6370 eclass = ExprClass.Type;
6374 public override void Emit (EmitContext ec)
6376 throw new Exception ("This should never be called");
6379 public override string ToString ()
6386 // This class is used to represent the address of an array, used
6387 // only by the Fixed statement, this is like the C "&a [0]" construct.
6389 public class ArrayPtr : Expression {
6392 public ArrayPtr (Expression array)
6394 Type array_type = array.Type.GetElementType ();
6398 string array_ptr_type_name = array_type.FullName + "*";
6400 type = Type.GetType (array_ptr_type_name);
6402 ModuleBuilder mb = CodeGen.ModuleBuilder;
6404 type = mb.GetType (array_ptr_type_name);
6407 eclass = ExprClass.Value;
6410 public override void Emit (EmitContext ec)
6412 ILGenerator ig = ec.ig;
6415 IntLiteral.EmitInt (ig, 0);
6416 ig.Emit (OpCodes.Ldelema, array.Type.GetElementType ());
6419 public override Expression DoResolve (EmitContext ec)
6422 // We are born fully resolved
6429 // Used by the fixed statement
6431 public class StringPtr : Expression {
6434 public StringPtr (LocalBuilder b)
6437 eclass = ExprClass.Value;
6438 type = TypeManager.char_ptr_type;
6441 public override Expression DoResolve (EmitContext ec)
6443 // This should never be invoked, we are born in fully
6444 // initialized state.
6449 public override void Emit (EmitContext ec)
6451 ILGenerator ig = ec.ig;
6453 ig.Emit (OpCodes.Ldloc, b);
6454 ig.Emit (OpCodes.Conv_I);
6455 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
6456 ig.Emit (OpCodes.Add);
6461 // Implements the `stackalloc' keyword
6463 public class StackAlloc : Expression {
6469 public StackAlloc (Expression type, Expression count, Location l)
6476 public override Expression DoResolve (EmitContext ec)
6478 count = count.Resolve (ec);
6482 if (count.Type != TypeManager.int32_type){
6483 count = ConvertImplicitRequired (ec, count, TypeManager.int32_type, loc);
6488 if (ec.InCatch || ec.InFinally){
6489 Report.Error (255, loc,
6490 "stackalloc can not be used in a catch or finally block");
6494 otype = ec.DeclSpace.ResolveType (t, false, loc);
6499 if (!TypeManager.VerifyUnManaged (otype, loc))
6502 string ptr_name = otype.FullName + "*";
6503 type = Type.GetType (ptr_name);
6505 ModuleBuilder mb = CodeGen.ModuleBuilder;
6507 type = mb.GetType (ptr_name);
6509 eclass = ExprClass.Value;
6514 public override void Emit (EmitContext ec)
6516 int size = GetTypeSize (otype);
6517 ILGenerator ig = ec.ig;
6520 ig.Emit (OpCodes.Sizeof, otype);
6522 IntConstant.EmitInt (ig, size);
6524 ig.Emit (OpCodes.Mul);
6525 ig.Emit (OpCodes.Localloc);