2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 // (C) 2003, 2004 Novell, 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 public StaticCallExpr (MethodInfo m, ArrayList a, Location l)
34 eclass = ExprClass.Value;
38 public override Expression DoResolve (EmitContext ec)
41 // We are born fully resolved
46 public override void Emit (EmitContext ec)
49 Invocation.EmitArguments (ec, mi, args);
51 ec.ig.Emit (OpCodes.Call, mi);
55 static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
56 Expression e, Location loc)
61 args = new ArrayList (1);
62 Argument a = new Argument (e, Argument.AType.Expression);
64 // We need to resolve the arguments before sending them in !
65 if (!a.Resolve (ec, loc))
69 method = Invocation.OverloadResolve (
70 ec, (MethodGroupExpr) mg, args, false, loc);
75 return new StaticCallExpr ((MethodInfo) method, args, loc);
78 public override void EmitStatement (EmitContext ec)
81 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
82 ec.ig.Emit (OpCodes.Pop);
86 public class ParenthesizedExpression : Expression
88 public Expression Expr;
90 public ParenthesizedExpression (Expression expr, Location loc)
96 public override Expression DoResolve (EmitContext ec)
98 Expr = Expr.Resolve (ec);
102 public override void Emit (EmitContext ec)
104 throw new Exception ("Should not happen");
109 /// Unary expressions.
113 /// Unary implements unary expressions. It derives from
114 /// ExpressionStatement becuase the pre/post increment/decrement
115 /// operators can be used in a statement context.
117 public class Unary : Expression {
118 public enum Operator : byte {
119 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
120 Indirection, AddressOf, TOP
123 public Operator Oper;
124 public Expression Expr;
126 public Unary (Operator op, Expression expr, Location loc)
134 /// Returns a stringified representation of the Operator
136 static public string OperName (Operator oper)
139 case Operator.UnaryPlus:
141 case Operator.UnaryNegation:
143 case Operator.LogicalNot:
145 case Operator.OnesComplement:
147 case Operator.AddressOf:
149 case Operator.Indirection:
153 return oper.ToString ();
156 public static readonly string [] oper_names;
160 oper_names = new string [(int)Operator.TOP];
162 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
163 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
164 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
165 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
166 oper_names [(int) Operator.Indirection] = "op_Indirection";
167 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
170 void Error23 (Type t)
173 23, "Operator " + OperName (Oper) +
174 " cannot be applied to operand of type `" +
175 TypeManager.CSharpName (t) + "'");
179 /// The result has been already resolved:
181 /// FIXME: a minus constant -128 sbyte cant be turned into a
184 static Expression TryReduceNegative (Constant expr)
188 if (expr is IntConstant)
189 e = new IntConstant (-((IntConstant) expr).Value);
190 else if (expr is UIntConstant){
191 uint value = ((UIntConstant) expr).Value;
193 if (value < 2147483649)
194 return new IntConstant (-(int)value);
196 e = new LongConstant (-value);
198 else if (expr is LongConstant)
199 e = new LongConstant (-((LongConstant) expr).Value);
200 else if (expr is ULongConstant){
201 ulong value = ((ULongConstant) expr).Value;
203 if (value < 9223372036854775809)
204 return new LongConstant(-(long)value);
206 else if (expr is FloatConstant)
207 e = new FloatConstant (-((FloatConstant) expr).Value);
208 else if (expr is DoubleConstant)
209 e = new DoubleConstant (-((DoubleConstant) expr).Value);
210 else if (expr is DecimalConstant)
211 e = new DecimalConstant (-((DecimalConstant) expr).Value);
212 else if (expr is ShortConstant)
213 e = new IntConstant (-((ShortConstant) expr).Value);
214 else if (expr is UShortConstant)
215 e = new IntConstant (-((UShortConstant) expr).Value);
220 // This routine will attempt to simplify the unary expression when the
221 // argument is a constant. The result is returned in `result' and the
222 // function returns true or false depending on whether a reduction
223 // was performed or not
225 bool Reduce (EmitContext ec, Constant e, out Expression result)
227 Type expr_type = e.Type;
230 case Operator.UnaryPlus:
234 case Operator.UnaryNegation:
235 result = TryReduceNegative (e);
238 case Operator.LogicalNot:
239 if (expr_type != TypeManager.bool_type) {
245 BoolConstant b = (BoolConstant) e;
246 result = new BoolConstant (!(b.Value));
249 case Operator.OnesComplement:
250 if (!((expr_type == TypeManager.int32_type) ||
251 (expr_type == TypeManager.uint32_type) ||
252 (expr_type == TypeManager.int64_type) ||
253 (expr_type == TypeManager.uint64_type) ||
254 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
257 if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){
258 result = new Cast (new TypeExpression (TypeManager.int32_type, loc), e, loc);
259 result = result.Resolve (ec);
260 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){
261 result = new Cast (new TypeExpression (TypeManager.uint32_type, loc), e, loc);
262 result = result.Resolve (ec);
263 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){
264 result = new Cast (new TypeExpression (TypeManager.int64_type, loc), e, loc);
265 result = result.Resolve (ec);
266 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){
267 result = new Cast (new TypeExpression (TypeManager.uint64_type, loc), e, loc);
268 result = result.Resolve (ec);
271 if (result == null || !(result is Constant)){
277 expr_type = result.Type;
278 e = (Constant) result;
281 if (e is EnumConstant){
282 EnumConstant enum_constant = (EnumConstant) e;
285 if (Reduce (ec, enum_constant.Child, out reduced)){
286 result = new EnumConstant ((Constant) reduced, enum_constant.Type);
294 if (expr_type == TypeManager.int32_type){
295 result = new IntConstant (~ ((IntConstant) e).Value);
296 } else if (expr_type == TypeManager.uint32_type){
297 result = new UIntConstant (~ ((UIntConstant) e).Value);
298 } else if (expr_type == TypeManager.int64_type){
299 result = new LongConstant (~ ((LongConstant) e).Value);
300 } else if (expr_type == TypeManager.uint64_type){
301 result = new ULongConstant (~ ((ULongConstant) e).Value);
309 case Operator.AddressOf:
313 case Operator.Indirection:
317 throw new Exception ("Can not constant fold: " + Oper.ToString());
320 Expression ResolveOperator (EmitContext ec)
322 Type expr_type = Expr.Type;
325 // Step 1: Perform Operator Overload location
330 op_name = oper_names [(int) Oper];
332 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
335 Expression e = StaticCallExpr.MakeSimpleCall (
336 ec, (MethodGroupExpr) mg, Expr, loc);
346 // Only perform numeric promotions on:
349 if (expr_type == null)
353 // Step 2: Default operations on CLI native types.
356 // Attempt to use a constant folding operation.
357 if (Expr is Constant){
360 if (Reduce (ec, (Constant) Expr, out result))
365 case Operator.LogicalNot:
366 if (expr_type != TypeManager.bool_type) {
367 Expr = ResolveBoolean (ec, Expr, loc);
374 type = TypeManager.bool_type;
377 case Operator.OnesComplement:
378 if (!((expr_type == TypeManager.int32_type) ||
379 (expr_type == TypeManager.uint32_type) ||
380 (expr_type == TypeManager.int64_type) ||
381 (expr_type == TypeManager.uint64_type) ||
382 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
385 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
387 type = TypeManager.int32_type;
390 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
392 type = TypeManager.uint32_type;
395 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
397 type = TypeManager.int64_type;
400 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
402 type = TypeManager.uint64_type;
411 case Operator.AddressOf:
412 if (Expr.eclass != ExprClass.Variable){
413 Error (211, "Cannot take the address of non-variables");
422 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
426 IVariable variable = Expr as IVariable;
427 if (!ec.InFixedInitializer && ((variable == null) || !variable.VerifyFixed (false))) {
428 Error (212, "You can only take the address of an unfixed expression inside " +
429 "of a fixed statement initializer");
433 if (ec.InFixedInitializer && ((variable != null) && variable.VerifyFixed (false))) {
434 Error (213, "You can not fix an already fixed expression");
438 // According to the specs, a variable is considered definitely assigned if you take
440 if ((variable != null) && (variable.VariableInfo != null))
441 variable.VariableInfo.SetAssigned (ec);
443 type = TypeManager.GetPointerType (Expr.Type);
446 case Operator.Indirection:
452 if (!expr_type.IsPointer){
453 Error (193, "The * or -> operator can only be applied to pointers");
458 // We create an Indirection expression, because
459 // it can implement the IMemoryLocation.
461 return new Indirection (Expr, loc);
463 case Operator.UnaryPlus:
465 // A plus in front of something is just a no-op, so return the child.
469 case Operator.UnaryNegation:
471 // Deals with -literals
472 // int operator- (int x)
473 // long operator- (long x)
474 // float operator- (float f)
475 // double operator- (double d)
476 // decimal operator- (decimal d)
478 Expression expr = null;
481 // transform - - expr into expr
484 Unary unary = (Unary) Expr;
486 if (unary.Oper == Operator.UnaryNegation)
491 // perform numeric promotions to int,
495 // The following is inneficient, because we call
496 // ImplicitConversion too many times.
498 // It is also not clear if we should convert to Float
499 // or Double initially.
501 if (expr_type == TypeManager.uint32_type){
503 // FIXME: handle exception to this rule that
504 // permits the int value -2147483648 (-2^31) to
505 // bt wrote as a decimal interger literal
507 type = TypeManager.int64_type;
508 Expr = Convert.ImplicitConversion (ec, Expr, type, loc);
512 if (expr_type == TypeManager.uint64_type){
514 // FIXME: Handle exception of `long value'
515 // -92233720368547758087 (-2^63) to be wrote as
516 // decimal integer literal.
522 if (expr_type == TypeManager.float_type){
527 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
534 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
541 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc);
552 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
553 TypeManager.CSharpName (expr_type) + "'");
557 public override Expression DoResolve (EmitContext ec)
559 if (Oper == Operator.AddressOf)
560 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
562 Expr = Expr.Resolve (ec);
567 eclass = ExprClass.Value;
568 return ResolveOperator (ec);
571 public override Expression DoResolveLValue (EmitContext ec, Expression right)
573 if (Oper == Operator.Indirection)
574 return base.DoResolveLValue (ec, right);
576 Error (131, "The left-hand side of an assignment must be a " +
577 "variable, property or indexer");
581 public override void Emit (EmitContext ec)
583 ILGenerator ig = ec.ig;
586 case Operator.UnaryPlus:
587 throw new Exception ("This should be caught by Resolve");
589 case Operator.UnaryNegation:
591 ig.Emit (OpCodes.Ldc_I4_0);
592 if (type == TypeManager.int64_type)
593 ig.Emit (OpCodes.Conv_U8);
595 ig.Emit (OpCodes.Sub_Ovf);
598 ig.Emit (OpCodes.Neg);
603 case Operator.LogicalNot:
605 ig.Emit (OpCodes.Ldc_I4_0);
606 ig.Emit (OpCodes.Ceq);
609 case Operator.OnesComplement:
611 ig.Emit (OpCodes.Not);
614 case Operator.AddressOf:
615 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
619 throw new Exception ("This should not happen: Operator = "
624 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
626 if (Oper == Operator.LogicalNot)
627 Expr.EmitBranchable (ec, target, !onTrue);
629 base.EmitBranchable (ec, target, onTrue);
632 public override string ToString ()
634 return "Unary (" + Oper + ", " + Expr + ")";
640 // Unary operators are turned into Indirection expressions
641 // after semantic analysis (this is so we can take the address
642 // of an indirection).
644 public class Indirection : Expression, IMemoryLocation, IAssignMethod {
646 LocalTemporary temporary;
649 public Indirection (Expression expr, Location l)
652 this.type = TypeManager.GetElementType (expr.Type);
653 eclass = ExprClass.Variable;
657 void LoadExprValue (EmitContext ec)
661 public override void Emit (EmitContext ec)
663 ILGenerator ig = ec.ig;
665 if (temporary != null){
666 if (have_temporary) {
670 ec.ig.Emit (OpCodes.Dup);
671 temporary.Store (ec);
672 have_temporary = true;
677 LoadFromPtr (ig, Type);
680 public void EmitAssign (EmitContext ec, Expression source)
682 if (temporary != null){
687 ec.ig.Emit (OpCodes.Dup);
688 temporary.Store (ec);
689 have_temporary = true;
695 StoreFromPtr (ec.ig, type);
698 public void AddressOf (EmitContext ec, AddressOp Mode)
700 if (temporary != null){
706 ec.ig.Emit (OpCodes.Dup);
707 temporary.Store (ec);
708 have_temporary = true;
713 public override Expression DoResolve (EmitContext ec)
716 // Born fully resolved
721 public new void CacheTemporaries (EmitContext ec)
723 temporary = new LocalTemporary (ec, expr.Type);
726 public override string ToString ()
728 return "*(" + expr + ")";
733 /// Unary Mutator expressions (pre and post ++ and --)
737 /// UnaryMutator implements ++ and -- expressions. It derives from
738 /// ExpressionStatement becuase the pre/post increment/decrement
739 /// operators can be used in a statement context.
741 /// FIXME: Idea, we could split this up in two classes, one simpler
742 /// for the common case, and one with the extra fields for more complex
743 /// classes (indexers require temporary access; overloaded require method)
746 public class UnaryMutator : ExpressionStatement {
748 public enum Mode : byte {
755 PreDecrement = IsDecrement,
756 PostIncrement = IsPost,
757 PostDecrement = IsPost | IsDecrement
762 LocalTemporary temp_storage;
765 // This is expensive for the simplest case.
769 public UnaryMutator (Mode m, Expression e, Location l)
776 static string OperName (Mode mode)
778 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
782 void Error23 (Type t)
785 23, "Operator " + OperName (mode) +
786 " cannot be applied to operand of type `" +
787 TypeManager.CSharpName (t) + "'");
791 /// Returns whether an object of type `t' can be incremented
792 /// or decremented with add/sub (ie, basically whether we can
793 /// use pre-post incr-decr operations on it, but it is not a
794 /// System.Decimal, which we require operator overloading to catch)
796 static bool IsIncrementableNumber (Type t)
798 return (t == TypeManager.sbyte_type) ||
799 (t == TypeManager.byte_type) ||
800 (t == TypeManager.short_type) ||
801 (t == TypeManager.ushort_type) ||
802 (t == TypeManager.int32_type) ||
803 (t == TypeManager.uint32_type) ||
804 (t == TypeManager.int64_type) ||
805 (t == TypeManager.uint64_type) ||
806 (t == TypeManager.char_type) ||
807 (t.IsSubclassOf (TypeManager.enum_type)) ||
808 (t == TypeManager.float_type) ||
809 (t == TypeManager.double_type) ||
810 (t.IsPointer && t != TypeManager.void_ptr_type);
813 Expression ResolveOperator (EmitContext ec)
815 Type expr_type = expr.Type;
818 // Step 1: Perform Operator Overload location
823 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
824 op_name = "op_Increment";
826 op_name = "op_Decrement";
828 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
830 if (mg == null && expr_type.BaseType != null)
831 mg = MemberLookup (ec, expr_type.BaseType, op_name,
832 MemberTypes.Method, AllBindingFlags, loc);
835 method = StaticCallExpr.MakeSimpleCall (
836 ec, (MethodGroupExpr) mg, expr, loc);
843 // The operand of the prefix/postfix increment decrement operators
844 // should be an expression that is classified as a variable,
845 // a property access or an indexer access
848 if (expr.eclass == ExprClass.Variable){
849 LocalVariableReference var = expr as LocalVariableReference;
850 if ((var != null) && var.IsReadOnly)
851 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
852 if (IsIncrementableNumber (expr_type) ||
853 expr_type == TypeManager.decimal_type){
856 } else if (expr.eclass == ExprClass.IndexerAccess){
857 IndexerAccess ia = (IndexerAccess) expr;
859 temp_storage = new LocalTemporary (ec, expr.Type);
861 expr = ia.ResolveLValue (ec, temp_storage);
866 } else if (expr.eclass == ExprClass.PropertyAccess){
867 PropertyExpr pe = (PropertyExpr) expr;
869 if (pe.VerifyAssignable ())
874 expr.Error_UnexpectedKind ("variable, indexer or property access");
878 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
879 TypeManager.CSharpName (expr_type) + "'");
883 public override Expression DoResolve (EmitContext ec)
885 expr = expr.Resolve (ec);
890 eclass = ExprClass.Value;
891 return ResolveOperator (ec);
894 static int PtrTypeSize (Type t)
896 return GetTypeSize (TypeManager.GetElementType (t));
900 // Loads the proper "1" into the stack based on the type, then it emits the
901 // opcode for the operation requested
903 void LoadOneAndEmitOp (EmitContext ec, Type t)
906 // Measure if getting the typecode and using that is more/less efficient
907 // that comparing types. t.GetTypeCode() is an internal call.
909 ILGenerator ig = ec.ig;
911 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
912 LongConstant.EmitLong (ig, 1);
913 else if (t == TypeManager.double_type)
914 ig.Emit (OpCodes.Ldc_R8, 1.0);
915 else if (t == TypeManager.float_type)
916 ig.Emit (OpCodes.Ldc_R4, 1.0F);
917 else if (t.IsPointer){
918 int n = PtrTypeSize (t);
921 ig.Emit (OpCodes.Sizeof, t);
923 IntConstant.EmitInt (ig, n);
925 ig.Emit (OpCodes.Ldc_I4_1);
928 // Now emit the operation
931 if (t == TypeManager.int32_type ||
932 t == TypeManager.int64_type){
933 if ((mode & Mode.IsDecrement) != 0)
934 ig.Emit (OpCodes.Sub_Ovf);
936 ig.Emit (OpCodes.Add_Ovf);
937 } else if (t == TypeManager.uint32_type ||
938 t == TypeManager.uint64_type){
939 if ((mode & Mode.IsDecrement) != 0)
940 ig.Emit (OpCodes.Sub_Ovf_Un);
942 ig.Emit (OpCodes.Add_Ovf_Un);
944 if ((mode & Mode.IsDecrement) != 0)
945 ig.Emit (OpCodes.Sub_Ovf);
947 ig.Emit (OpCodes.Add_Ovf);
950 if ((mode & Mode.IsDecrement) != 0)
951 ig.Emit (OpCodes.Sub);
953 ig.Emit (OpCodes.Add);
956 if (t == TypeManager.sbyte_type){
958 ig.Emit (OpCodes.Conv_Ovf_I1);
960 ig.Emit (OpCodes.Conv_I1);
961 } else if (t == TypeManager.byte_type){
963 ig.Emit (OpCodes.Conv_Ovf_U1);
965 ig.Emit (OpCodes.Conv_U1);
966 } else if (t == TypeManager.short_type){
968 ig.Emit (OpCodes.Conv_Ovf_I2);
970 ig.Emit (OpCodes.Conv_I2);
971 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
973 ig.Emit (OpCodes.Conv_Ovf_U2);
975 ig.Emit (OpCodes.Conv_U2);
980 static EmptyExpression empty_expr;
982 void EmitCode (EmitContext ec, bool is_expr)
984 ILGenerator ig = ec.ig;
985 IAssignMethod ia = (IAssignMethod) expr;
986 Type expr_type = expr.Type;
988 ia.CacheTemporaries (ec);
991 // NOTE: We should probably handle three cases:
993 // * method invocation required.
994 // * direct stack manipulation possible
995 // * the object requires an "instance" field
997 if (temp_storage == null){
999 // Temporary improvement: if we are dealing with something that does
1000 // not require complicated instance setup, avoid using a temporary
1002 // For now: only localvariables when not remapped
1005 if (method == null &&
1006 ((expr is LocalVariableReference) ||(expr is FieldExpr && ((FieldExpr) expr).FieldInfo.IsStatic))){
1007 if (empty_expr == null)
1008 empty_expr = new EmptyExpression ();
1011 case Mode.PreIncrement:
1012 case Mode.PreDecrement:
1015 LoadOneAndEmitOp (ec, expr_type);
1017 ig.Emit (OpCodes.Dup);
1018 ia.EmitAssign (ec, empty_expr);
1021 case Mode.PostIncrement:
1022 case Mode.PostDecrement:
1025 ig.Emit (OpCodes.Dup);
1027 LoadOneAndEmitOp (ec, expr_type);
1028 ia.EmitAssign (ec, empty_expr);
1033 temp_storage = new LocalTemporary (ec, expr_type);
1037 case Mode.PreIncrement:
1038 case Mode.PreDecrement:
1039 if (method == null){
1042 LoadOneAndEmitOp (ec, expr_type);
1046 temp_storage.Store (ec);
1047 ia.EmitAssign (ec, temp_storage);
1049 temp_storage.Emit (ec);
1052 case Mode.PostIncrement:
1053 case Mode.PostDecrement:
1057 if (method == null){
1061 ig.Emit (OpCodes.Dup);
1063 LoadOneAndEmitOp (ec, expr_type);
1068 temp_storage.Store (ec);
1069 ia.EmitAssign (ec, temp_storage);
1073 temp_storage.Release (ec);
1076 public override void Emit (EmitContext ec)
1078 EmitCode (ec, true);
1082 public override void EmitStatement (EmitContext ec)
1084 EmitCode (ec, false);
1090 /// Base class for the `Is' and `As' classes.
1094 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1097 public abstract class Probe : Expression {
1098 public readonly Expression ProbeType;
1099 protected Expression expr;
1100 protected Type probe_type;
1102 public Probe (Expression expr, Expression probe_type, Location l)
1104 ProbeType = probe_type;
1109 public Expression Expr {
1115 public override Expression DoResolve (EmitContext ec)
1117 probe_type = ec.DeclSpace.ResolveType (ProbeType, false, loc);
1119 if (probe_type == null)
1122 expr = expr.Resolve (ec);
1131 /// Implementation of the `is' operator.
1133 public class Is : Probe {
1134 public Is (Expression expr, Expression probe_type, Location l)
1135 : base (expr, probe_type, l)
1140 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
1145 public override void Emit (EmitContext ec)
1147 ILGenerator ig = ec.ig;
1152 case Action.AlwaysFalse:
1153 ig.Emit (OpCodes.Pop);
1154 IntConstant.EmitInt (ig, 0);
1156 case Action.AlwaysTrue:
1157 ig.Emit (OpCodes.Pop);
1158 IntConstant.EmitInt (ig, 1);
1160 case Action.LeaveOnStack:
1161 // the `e != null' rule.
1162 ig.Emit (OpCodes.Ldnull);
1163 ig.Emit (OpCodes.Ceq);
1164 ig.Emit (OpCodes.Ldc_I4_0);
1165 ig.Emit (OpCodes.Ceq);
1168 ig.Emit (OpCodes.Isinst, probe_type);
1169 ig.Emit (OpCodes.Ldnull);
1170 ig.Emit (OpCodes.Cgt_Un);
1173 throw new Exception ("never reached");
1176 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
1178 ILGenerator ig = ec.ig;
1181 case Action.AlwaysFalse:
1183 ig.Emit (OpCodes.Br, target);
1186 case Action.AlwaysTrue:
1188 ig.Emit (OpCodes.Br, target);
1191 case Action.LeaveOnStack:
1192 // the `e != null' rule.
1194 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1198 ig.Emit (OpCodes.Isinst, probe_type);
1199 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1202 throw new Exception ("never reached");
1205 public override Expression DoResolve (EmitContext ec)
1207 Expression e = base.DoResolve (ec);
1209 if ((e == null) || (expr == null))
1212 Type etype = expr.Type;
1213 bool warning_always_matches = false;
1214 bool warning_never_matches = false;
1216 type = TypeManager.bool_type;
1217 eclass = ExprClass.Value;
1220 // First case, if at compile time, there is an implicit conversion
1221 // then e != null (objects) or true (value types)
1223 e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
1226 if (etype.IsValueType)
1227 action = Action.AlwaysTrue;
1229 action = Action.LeaveOnStack;
1231 warning_always_matches = true;
1232 } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1234 // Second case: explicit reference convresion
1236 if (expr is NullLiteral)
1237 action = Action.AlwaysFalse;
1239 action = Action.Probe;
1241 action = Action.AlwaysFalse;
1242 warning_never_matches = true;
1245 if (RootContext.WarningLevel >= 1){
1246 if (warning_always_matches)
1247 Warning (183, "The expression is always of type `" +
1248 TypeManager.CSharpName (probe_type) + "'");
1249 else if (warning_never_matches){
1250 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1252 "The expression is never of type `" +
1253 TypeManager.CSharpName (probe_type) + "'");
1262 /// Implementation of the `as' operator.
1264 public class As : Probe {
1265 public As (Expression expr, Expression probe_type, Location l)
1266 : base (expr, probe_type, l)
1270 bool do_isinst = false;
1272 public override void Emit (EmitContext ec)
1274 ILGenerator ig = ec.ig;
1279 ig.Emit (OpCodes.Isinst, probe_type);
1282 static void Error_CannotConvertType (Type source, Type target, Location loc)
1285 39, loc, "as operator can not convert from `" +
1286 TypeManager.CSharpName (source) + "' to `" +
1287 TypeManager.CSharpName (target) + "'");
1290 public override Expression DoResolve (EmitContext ec)
1292 Expression e = base.DoResolve (ec);
1298 eclass = ExprClass.Value;
1299 Type etype = expr.Type;
1301 if (TypeManager.IsValueType (probe_type)){
1302 Report.Error (77, loc, "The as operator should be used with a reference type only (" +
1303 TypeManager.CSharpName (probe_type) + " is a value type)");
1308 e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
1315 if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1320 Error_CannotConvertType (etype, probe_type, loc);
1326 /// This represents a typecast in the source language.
1328 /// FIXME: Cast expressions have an unusual set of parsing
1329 /// rules, we need to figure those out.
1331 public class Cast : Expression {
1332 Expression target_type;
1335 public Cast (Expression cast_type, Expression expr, Location loc)
1337 this.target_type = cast_type;
1342 public Expression TargetType {
1348 public Expression Expr {
1357 bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
1359 if (!ec.ConstantCheckState)
1362 if ((value < min) || (value > max)) {
1363 Error (221, "Constant value `" + value + "' cannot be converted " +
1364 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1365 "syntax to override)");
1372 bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
1374 if (!ec.ConstantCheckState)
1378 Error (221, "Constant value `" + value + "' cannot be converted " +
1379 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1380 "syntax to override)");
1387 bool CheckUnsigned (EmitContext ec, long value, Type type)
1389 if (!ec.ConstantCheckState)
1393 Error (221, "Constant value `" + value + "' cannot be converted " +
1394 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1395 "syntax to override)");
1403 /// Attempts to do a compile-time folding of a constant cast.
1405 Expression TryReduce (EmitContext ec, Type target_type)
1407 Expression real_expr = expr;
1408 if (real_expr is EnumConstant)
1409 real_expr = ((EnumConstant) real_expr).Child;
1411 if (real_expr is ByteConstant){
1412 byte v = ((ByteConstant) real_expr).Value;
1414 if (target_type == TypeManager.sbyte_type) {
1415 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1417 return new SByteConstant ((sbyte) v);
1419 if (target_type == TypeManager.short_type)
1420 return new ShortConstant ((short) v);
1421 if (target_type == TypeManager.ushort_type)
1422 return new UShortConstant ((ushort) v);
1423 if (target_type == TypeManager.int32_type)
1424 return new IntConstant ((int) v);
1425 if (target_type == TypeManager.uint32_type)
1426 return new UIntConstant ((uint) v);
1427 if (target_type == TypeManager.int64_type)
1428 return new LongConstant ((long) v);
1429 if (target_type == TypeManager.uint64_type)
1430 return new ULongConstant ((ulong) v);
1431 if (target_type == TypeManager.float_type)
1432 return new FloatConstant ((float) v);
1433 if (target_type == TypeManager.double_type)
1434 return new DoubleConstant ((double) v);
1435 if (target_type == TypeManager.char_type)
1436 return new CharConstant ((char) v);
1437 if (target_type == TypeManager.decimal_type)
1438 return new DecimalConstant ((decimal) v);
1440 if (real_expr is SByteConstant){
1441 sbyte v = ((SByteConstant) real_expr).Value;
1443 if (target_type == TypeManager.byte_type) {
1444 if (!CheckUnsigned (ec, v, target_type))
1446 return new ByteConstant ((byte) v);
1448 if (target_type == TypeManager.short_type)
1449 return new ShortConstant ((short) v);
1450 if (target_type == TypeManager.ushort_type) {
1451 if (!CheckUnsigned (ec, v, target_type))
1453 return new UShortConstant ((ushort) v);
1454 } if (target_type == TypeManager.int32_type)
1455 return new IntConstant ((int) v);
1456 if (target_type == TypeManager.uint32_type) {
1457 if (!CheckUnsigned (ec, v, target_type))
1459 return new UIntConstant ((uint) v);
1460 } if (target_type == TypeManager.int64_type)
1461 return new LongConstant ((long) v);
1462 if (target_type == TypeManager.uint64_type) {
1463 if (!CheckUnsigned (ec, v, target_type))
1465 return new ULongConstant ((ulong) v);
1467 if (target_type == TypeManager.float_type)
1468 return new FloatConstant ((float) v);
1469 if (target_type == TypeManager.double_type)
1470 return new DoubleConstant ((double) v);
1471 if (target_type == TypeManager.char_type) {
1472 if (!CheckUnsigned (ec, v, target_type))
1474 return new CharConstant ((char) v);
1476 if (target_type == TypeManager.decimal_type)
1477 return new DecimalConstant ((decimal) v);
1479 if (real_expr is ShortConstant){
1480 short v = ((ShortConstant) real_expr).Value;
1482 if (target_type == TypeManager.byte_type) {
1483 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1485 return new ByteConstant ((byte) v);
1487 if (target_type == TypeManager.sbyte_type) {
1488 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1490 return new SByteConstant ((sbyte) v);
1492 if (target_type == TypeManager.ushort_type) {
1493 if (!CheckUnsigned (ec, v, target_type))
1495 return new UShortConstant ((ushort) v);
1497 if (target_type == TypeManager.int32_type)
1498 return new IntConstant ((int) v);
1499 if (target_type == TypeManager.uint32_type) {
1500 if (!CheckUnsigned (ec, v, target_type))
1502 return new UIntConstant ((uint) v);
1504 if (target_type == TypeManager.int64_type)
1505 return new LongConstant ((long) v);
1506 if (target_type == TypeManager.uint64_type) {
1507 if (!CheckUnsigned (ec, v, target_type))
1509 return new ULongConstant ((ulong) v);
1511 if (target_type == TypeManager.float_type)
1512 return new FloatConstant ((float) v);
1513 if (target_type == TypeManager.double_type)
1514 return new DoubleConstant ((double) v);
1515 if (target_type == TypeManager.char_type) {
1516 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1518 return new CharConstant ((char) v);
1520 if (target_type == TypeManager.decimal_type)
1521 return new DecimalConstant ((decimal) v);
1523 if (real_expr is UShortConstant){
1524 ushort v = ((UShortConstant) real_expr).Value;
1526 if (target_type == TypeManager.byte_type) {
1527 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1529 return new ByteConstant ((byte) v);
1531 if (target_type == TypeManager.sbyte_type) {
1532 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1534 return new SByteConstant ((sbyte) v);
1536 if (target_type == TypeManager.short_type) {
1537 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1539 return new ShortConstant ((short) v);
1541 if (target_type == TypeManager.int32_type)
1542 return new IntConstant ((int) v);
1543 if (target_type == TypeManager.uint32_type)
1544 return new UIntConstant ((uint) v);
1545 if (target_type == TypeManager.int64_type)
1546 return new LongConstant ((long) v);
1547 if (target_type == TypeManager.uint64_type)
1548 return new ULongConstant ((ulong) v);
1549 if (target_type == TypeManager.float_type)
1550 return new FloatConstant ((float) v);
1551 if (target_type == TypeManager.double_type)
1552 return new DoubleConstant ((double) v);
1553 if (target_type == TypeManager.char_type) {
1554 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1556 return new CharConstant ((char) v);
1558 if (target_type == TypeManager.decimal_type)
1559 return new DecimalConstant ((decimal) v);
1561 if (real_expr is IntConstant){
1562 int v = ((IntConstant) real_expr).Value;
1564 if (target_type == TypeManager.byte_type) {
1565 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1567 return new ByteConstant ((byte) v);
1569 if (target_type == TypeManager.sbyte_type) {
1570 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1572 return new SByteConstant ((sbyte) v);
1574 if (target_type == TypeManager.short_type) {
1575 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1577 return new ShortConstant ((short) v);
1579 if (target_type == TypeManager.ushort_type) {
1580 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1582 return new UShortConstant ((ushort) v);
1584 if (target_type == TypeManager.uint32_type) {
1585 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1587 return new UIntConstant ((uint) v);
1589 if (target_type == TypeManager.int64_type)
1590 return new LongConstant ((long) v);
1591 if (target_type == TypeManager.uint64_type) {
1592 if (!CheckUnsigned (ec, v, target_type))
1594 return new ULongConstant ((ulong) v);
1596 if (target_type == TypeManager.float_type)
1597 return new FloatConstant ((float) v);
1598 if (target_type == TypeManager.double_type)
1599 return new DoubleConstant ((double) v);
1600 if (target_type == TypeManager.char_type) {
1601 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1603 return new CharConstant ((char) v);
1605 if (target_type == TypeManager.decimal_type)
1606 return new DecimalConstant ((decimal) v);
1608 if (real_expr is UIntConstant){
1609 uint v = ((UIntConstant) real_expr).Value;
1611 if (target_type == TypeManager.byte_type) {
1612 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1614 return new ByteConstant ((byte) v);
1616 if (target_type == TypeManager.sbyte_type) {
1617 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1619 return new SByteConstant ((sbyte) v);
1621 if (target_type == TypeManager.short_type) {
1622 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1624 return new ShortConstant ((short) v);
1626 if (target_type == TypeManager.ushort_type) {
1627 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1629 return new UShortConstant ((ushort) v);
1631 if (target_type == TypeManager.int32_type) {
1632 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1634 return new IntConstant ((int) v);
1636 if (target_type == TypeManager.int64_type)
1637 return new LongConstant ((long) v);
1638 if (target_type == TypeManager.uint64_type)
1639 return new ULongConstant ((ulong) v);
1640 if (target_type == TypeManager.float_type)
1641 return new FloatConstant ((float) v);
1642 if (target_type == TypeManager.double_type)
1643 return new DoubleConstant ((double) v);
1644 if (target_type == TypeManager.char_type) {
1645 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1647 return new CharConstant ((char) v);
1649 if (target_type == TypeManager.decimal_type)
1650 return new DecimalConstant ((decimal) v);
1652 if (real_expr is LongConstant){
1653 long v = ((LongConstant) real_expr).Value;
1655 if (target_type == TypeManager.byte_type) {
1656 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1658 return new ByteConstant ((byte) v);
1660 if (target_type == TypeManager.sbyte_type) {
1661 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1663 return new SByteConstant ((sbyte) v);
1665 if (target_type == TypeManager.short_type) {
1666 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1668 return new ShortConstant ((short) v);
1670 if (target_type == TypeManager.ushort_type) {
1671 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1673 return new UShortConstant ((ushort) v);
1675 if (target_type == TypeManager.int32_type) {
1676 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1678 return new IntConstant ((int) v);
1680 if (target_type == TypeManager.uint32_type) {
1681 if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
1683 return new UIntConstant ((uint) v);
1685 if (target_type == TypeManager.uint64_type) {
1686 if (!CheckUnsigned (ec, v, target_type))
1688 return new ULongConstant ((ulong) v);
1690 if (target_type == TypeManager.float_type)
1691 return new FloatConstant ((float) v);
1692 if (target_type == TypeManager.double_type)
1693 return new DoubleConstant ((double) v);
1694 if (target_type == TypeManager.char_type) {
1695 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1697 return new CharConstant ((char) v);
1699 if (target_type == TypeManager.decimal_type)
1700 return new DecimalConstant ((decimal) v);
1702 if (real_expr is ULongConstant){
1703 ulong v = ((ULongConstant) real_expr).Value;
1705 if (target_type == TypeManager.byte_type) {
1706 if (!CheckRange (ec, v, target_type, Byte.MaxValue))
1708 return new ByteConstant ((byte) v);
1710 if (target_type == TypeManager.sbyte_type) {
1711 if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
1713 return new SByteConstant ((sbyte) v);
1715 if (target_type == TypeManager.short_type) {
1716 if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
1718 return new ShortConstant ((short) v);
1720 if (target_type == TypeManager.ushort_type) {
1721 if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
1723 return new UShortConstant ((ushort) v);
1725 if (target_type == TypeManager.int32_type) {
1726 if (!CheckRange (ec, v, target_type, Int32.MaxValue))
1728 return new IntConstant ((int) v);
1730 if (target_type == TypeManager.uint32_type) {
1731 if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
1733 return new UIntConstant ((uint) v);
1735 if (target_type == TypeManager.int64_type) {
1736 if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
1738 return new LongConstant ((long) v);
1740 if (target_type == TypeManager.float_type)
1741 return new FloatConstant ((float) v);
1742 if (target_type == TypeManager.double_type)
1743 return new DoubleConstant ((double) v);
1744 if (target_type == TypeManager.char_type) {
1745 if (!CheckRange (ec, v, target_type, Char.MaxValue))
1747 return new CharConstant ((char) v);
1749 if (target_type == TypeManager.decimal_type)
1750 return new DecimalConstant ((decimal) v);
1752 if (real_expr is FloatConstant){
1753 float v = ((FloatConstant) real_expr).Value;
1755 if (target_type == TypeManager.byte_type)
1756 return new ByteConstant ((byte) v);
1757 if (target_type == TypeManager.sbyte_type)
1758 return new SByteConstant ((sbyte) v);
1759 if (target_type == TypeManager.short_type)
1760 return new ShortConstant ((short) v);
1761 if (target_type == TypeManager.ushort_type)
1762 return new UShortConstant ((ushort) v);
1763 if (target_type == TypeManager.int32_type)
1764 return new IntConstant ((int) v);
1765 if (target_type == TypeManager.uint32_type)
1766 return new UIntConstant ((uint) v);
1767 if (target_type == TypeManager.int64_type)
1768 return new LongConstant ((long) v);
1769 if (target_type == TypeManager.uint64_type)
1770 return new ULongConstant ((ulong) v);
1771 if (target_type == TypeManager.double_type)
1772 return new DoubleConstant ((double) v);
1773 if (target_type == TypeManager.char_type)
1774 return new CharConstant ((char) v);
1775 if (target_type == TypeManager.decimal_type)
1776 return new DecimalConstant ((decimal) v);
1778 if (real_expr is DoubleConstant){
1779 double v = ((DoubleConstant) real_expr).Value;
1781 if (target_type == TypeManager.byte_type){
1782 return new ByteConstant ((byte) v);
1783 } if (target_type == TypeManager.sbyte_type)
1784 return new SByteConstant ((sbyte) v);
1785 if (target_type == TypeManager.short_type)
1786 return new ShortConstant ((short) v);
1787 if (target_type == TypeManager.ushort_type)
1788 return new UShortConstant ((ushort) v);
1789 if (target_type == TypeManager.int32_type)
1790 return new IntConstant ((int) v);
1791 if (target_type == TypeManager.uint32_type)
1792 return new UIntConstant ((uint) v);
1793 if (target_type == TypeManager.int64_type)
1794 return new LongConstant ((long) v);
1795 if (target_type == TypeManager.uint64_type)
1796 return new ULongConstant ((ulong) v);
1797 if (target_type == TypeManager.float_type)
1798 return new FloatConstant ((float) v);
1799 if (target_type == TypeManager.char_type)
1800 return new CharConstant ((char) v);
1801 if (target_type == TypeManager.decimal_type)
1802 return new DecimalConstant ((decimal) v);
1805 if (real_expr is CharConstant){
1806 char v = ((CharConstant) real_expr).Value;
1808 if (target_type == TypeManager.byte_type) {
1809 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1811 return new ByteConstant ((byte) v);
1813 if (target_type == TypeManager.sbyte_type) {
1814 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1816 return new SByteConstant ((sbyte) v);
1818 if (target_type == TypeManager.short_type) {
1819 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1821 return new ShortConstant ((short) v);
1823 if (target_type == TypeManager.int32_type)
1824 return new IntConstant ((int) v);
1825 if (target_type == TypeManager.uint32_type)
1826 return new UIntConstant ((uint) v);
1827 if (target_type == TypeManager.int64_type)
1828 return new LongConstant ((long) v);
1829 if (target_type == TypeManager.uint64_type)
1830 return new ULongConstant ((ulong) v);
1831 if (target_type == TypeManager.float_type)
1832 return new FloatConstant ((float) v);
1833 if (target_type == TypeManager.double_type)
1834 return new DoubleConstant ((double) v);
1835 if (target_type == TypeManager.char_type) {
1836 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1838 return new CharConstant ((char) v);
1840 if (target_type == TypeManager.decimal_type)
1841 return new DecimalConstant ((decimal) v);
1847 public override Expression DoResolve (EmitContext ec)
1849 expr = expr.Resolve (ec);
1853 type = ec.DeclSpace.ResolveType (target_type, false, Location);
1858 eclass = ExprClass.Value;
1860 if (expr is Constant){
1861 Expression e = TryReduce (ec, type);
1867 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1871 public override void Emit (EmitContext ec)
1874 // This one will never happen
1876 throw new Exception ("Should not happen");
1881 /// Binary operators
1883 public class Binary : Expression {
1884 public enum Operator : byte {
1885 Multiply, Division, Modulus,
1886 Addition, Subtraction,
1887 LeftShift, RightShift,
1888 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1889 Equality, Inequality,
1899 Expression left, right;
1901 // This must be kept in sync with Operator!!!
1902 public static readonly string [] oper_names;
1906 oper_names = new string [(int) Operator.TOP];
1908 oper_names [(int) Operator.Multiply] = "op_Multiply";
1909 oper_names [(int) Operator.Division] = "op_Division";
1910 oper_names [(int) Operator.Modulus] = "op_Modulus";
1911 oper_names [(int) Operator.Addition] = "op_Addition";
1912 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1913 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1914 oper_names [(int) Operator.RightShift] = "op_RightShift";
1915 oper_names [(int) Operator.LessThan] = "op_LessThan";
1916 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1917 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1918 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1919 oper_names [(int) Operator.Equality] = "op_Equality";
1920 oper_names [(int) Operator.Inequality] = "op_Inequality";
1921 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1922 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1923 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1924 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1925 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1928 public Binary (Operator oper, Expression left, Expression right, Location loc)
1936 public Operator Oper {
1945 public Expression Left {
1954 public Expression Right {
1965 /// Returns a stringified representation of the Operator
1967 static string OperName (Operator oper)
1970 case Operator.Multiply:
1972 case Operator.Division:
1974 case Operator.Modulus:
1976 case Operator.Addition:
1978 case Operator.Subtraction:
1980 case Operator.LeftShift:
1982 case Operator.RightShift:
1984 case Operator.LessThan:
1986 case Operator.GreaterThan:
1988 case Operator.LessThanOrEqual:
1990 case Operator.GreaterThanOrEqual:
1992 case Operator.Equality:
1994 case Operator.Inequality:
1996 case Operator.BitwiseAnd:
1998 case Operator.BitwiseOr:
2000 case Operator.ExclusiveOr:
2002 case Operator.LogicalOr:
2004 case Operator.LogicalAnd:
2008 return oper.ToString ();
2011 public override string ToString ()
2013 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
2014 right.ToString () + ")";
2017 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
2019 if (expr.Type == target_type)
2022 return Convert.ImplicitConversion (ec, expr, target_type, loc);
2025 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
2028 34, loc, "Operator `" + OperName (oper)
2029 + "' is ambiguous on operands of type `"
2030 + TypeManager.CSharpName (l) + "' "
2031 + "and `" + TypeManager.CSharpName (r)
2035 bool IsOfType (EmitContext ec, Type l, Type r, Type t, bool check_user_conversions)
2037 if ((l == t) || (r == t))
2040 if (!check_user_conversions)
2043 if (Convert.ImplicitUserConversionExists (ec, l, t))
2045 else if (Convert.ImplicitUserConversionExists (ec, r, t))
2052 // Note that handling the case l == Decimal || r == Decimal
2053 // is taken care of by the Step 1 Operator Overload resolution.
2055 // If `check_user_conv' is true, we also check whether a user-defined conversion
2056 // exists. Note that we only need to do this if both arguments are of a user-defined
2057 // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
2058 // so we don't explicitly check for performance reasons.
2060 bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
2062 if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
2064 // If either operand is of type double, the other operand is
2065 // conveted to type double.
2067 if (r != TypeManager.double_type)
2068 right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc);
2069 if (l != TypeManager.double_type)
2070 left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc);
2072 type = TypeManager.double_type;
2073 } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){
2075 // if either operand is of type float, the other operand is
2076 // converted to type float.
2078 if (r != TypeManager.double_type)
2079 right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc);
2080 if (l != TypeManager.double_type)
2081 left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc);
2082 type = TypeManager.float_type;
2083 } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){
2087 // If either operand is of type ulong, the other operand is
2088 // converted to type ulong. or an error ocurrs if the other
2089 // operand is of type sbyte, short, int or long
2091 if (l == TypeManager.uint64_type){
2092 if (r != TypeManager.uint64_type){
2093 if (right is IntConstant){
2094 IntConstant ic = (IntConstant) right;
2096 e = Convert.TryImplicitIntConversion (l, ic);
2099 } else if (right is LongConstant){
2100 long ll = ((LongConstant) right).Value;
2103 right = new ULongConstant ((ulong) ll);
2105 e = Convert.ImplicitNumericConversion (ec, right, l, loc);
2112 if (left is IntConstant){
2113 e = Convert.TryImplicitIntConversion (r, (IntConstant) left);
2116 } else if (left is LongConstant){
2117 long ll = ((LongConstant) left).Value;
2120 left = new ULongConstant ((ulong) ll);
2122 e = Convert.ImplicitNumericConversion (ec, left, r, loc);
2129 if ((other == TypeManager.sbyte_type) ||
2130 (other == TypeManager.short_type) ||
2131 (other == TypeManager.int32_type) ||
2132 (other == TypeManager.int64_type))
2133 Error_OperatorAmbiguous (loc, oper, l, r);
2134 type = TypeManager.uint64_type;
2135 } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
2137 // If either operand is of type long, the other operand is converted
2140 if (l != TypeManager.int64_type)
2141 left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc);
2142 if (r != TypeManager.int64_type)
2143 right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc);
2145 type = TypeManager.int64_type;
2146 } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){
2148 // If either operand is of type uint, and the other
2149 // operand is of type sbyte, short or int, othe operands are
2150 // converted to type long (unless we have an int constant).
2154 if (l == TypeManager.uint32_type){
2155 if (right is IntConstant){
2156 IntConstant ic = (IntConstant) right;
2160 right = new UIntConstant ((uint) val);
2167 } else if (r == TypeManager.uint32_type){
2168 if (left is IntConstant){
2169 IntConstant ic = (IntConstant) left;
2173 left = new UIntConstant ((uint) val);
2182 if ((other == TypeManager.sbyte_type) ||
2183 (other == TypeManager.short_type) ||
2184 (other == TypeManager.int32_type)){
2185 left = ForceConversion (ec, left, TypeManager.int64_type);
2186 right = ForceConversion (ec, right, TypeManager.int64_type);
2187 type = TypeManager.int64_type;
2190 // if either operand is of type uint, the other
2191 // operand is converd to type uint
2193 left = ForceConversion (ec, left, TypeManager.uint32_type);
2194 right = ForceConversion (ec, right, TypeManager.uint32_type);
2195 type = TypeManager.uint32_type;
2197 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2198 if (l != TypeManager.decimal_type)
2199 left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc);
2201 if (r != TypeManager.decimal_type)
2202 right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc);
2203 type = TypeManager.decimal_type;
2205 left = ForceConversion (ec, left, TypeManager.int32_type);
2206 right = ForceConversion (ec, right, TypeManager.int32_type);
2208 type = TypeManager.int32_type;
2211 return (left != null) && (right != null);
2214 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
2216 Report.Error (19, loc,
2217 "Operator " + name + " cannot be applied to operands of type `" +
2218 TypeManager.CSharpName (l) + "' and `" +
2219 TypeManager.CSharpName (r) + "'");
2222 void Error_OperatorCannotBeApplied ()
2224 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
2227 static bool is_unsigned (Type t)
2229 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2230 t == TypeManager.short_type || t == TypeManager.byte_type);
2233 static bool is_user_defined (Type t)
2235 if (t.IsSubclassOf (TypeManager.value_type) &&
2236 (!TypeManager.IsBuiltinType (t) || t == TypeManager.decimal_type))
2242 Expression Make32or64 (EmitContext ec, Expression e)
2246 if (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
2247 t == TypeManager.int64_type || t == TypeManager.uint64_type)
2249 Expression ee = Convert.ImplicitConversion (ec, e, TypeManager.int32_type, loc);
2252 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint32_type, loc);
2255 ee = Convert.ImplicitConversion (ec, e, TypeManager.int64_type, loc);
2258 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint64_type, loc);
2264 Expression CheckShiftArguments (EmitContext ec)
2268 e = ForceConversion (ec, right, TypeManager.int32_type);
2270 Error_OperatorCannotBeApplied ();
2275 if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) ||
2276 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) ||
2277 ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) ||
2278 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){
2282 if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
2283 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31), loc);
2284 right = right.DoResolve (ec);
2286 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63), loc);
2287 right = right.DoResolve (ec);
2292 Error_OperatorCannotBeApplied ();
2296 Expression ResolveOperator (EmitContext ec)
2299 Type r = right.Type;
2301 bool overload_failed = false;
2304 // Special cases: string or type parameter comapred to null
2306 if (oper == Operator.Equality || oper == Operator.Inequality){
2307 if ((l == TypeManager.string_type && (right is NullLiteral)) ||
2308 (r == TypeManager.string_type && (left is NullLiteral))){
2309 Type = TypeManager.bool_type;
2314 if (l.IsGenericParameter && (right is NullLiteral)) {
2315 if (l.BaseType == TypeManager.value_type) {
2316 Error_OperatorCannotBeApplied ();
2320 left = new BoxedCast (left);
2321 Type = TypeManager.bool_type;
2325 if (r.IsGenericParameter && (left is NullLiteral)) {
2326 if (r.BaseType == TypeManager.value_type) {
2327 Error_OperatorCannotBeApplied ();
2331 right = new BoxedCast (right);
2332 Type = TypeManager.bool_type;
2337 if (l == TypeManager.intptr_type && r == TypeManager.intptr_type) {
2338 Type = TypeManager.bool_type;
2345 // Do not perform operator overload resolution when both sides are
2348 if (!(TypeManager.IsCLRType (l) && TypeManager.IsCLRType (r))){
2350 // Step 1: Perform Operator Overload location
2352 Expression left_expr, right_expr;
2354 string op = oper_names [(int) oper];
2356 MethodGroupExpr union;
2357 left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
2359 right_expr = MemberLookup (
2360 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
2361 union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
2363 union = (MethodGroupExpr) left_expr;
2365 if (union != null) {
2366 ArrayList args = new ArrayList (2);
2367 args.Add (new Argument (left, Argument.AType.Expression));
2368 args.Add (new Argument (right, Argument.AType.Expression));
2370 MethodBase method = Invocation.OverloadResolve (
2371 ec, union, args, true, Location.Null);
2373 if (method != null) {
2374 MethodInfo mi = (MethodInfo) method;
2376 return new BinaryMethod (mi.ReturnType, method, args);
2378 overload_failed = true;
2384 // Step 0: String concatenation (because overloading will get this wrong)
2386 if (oper == Operator.Addition){
2388 // If any of the arguments is a string, cast to string
2391 // Simple constant folding
2392 if (left is StringConstant && right is StringConstant)
2393 return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value);
2395 if (l == TypeManager.string_type || r == TypeManager.string_type) {
2397 if (r == TypeManager.void_type || l == TypeManager.void_type) {
2398 Error_OperatorCannotBeApplied ();
2402 // try to fold it in on the left
2403 if (left is StringConcat) {
2406 // We have to test here for not-null, since we can be doubly-resolved
2407 // take care of not appending twice
2410 type = TypeManager.string_type;
2411 ((StringConcat) left).Append (ec, right);
2412 return left.Resolve (ec);
2418 // Otherwise, start a new concat expression
2419 return new StringConcat (ec, loc, left, right).Resolve (ec);
2423 // Transform a + ( - b) into a - b
2425 if (right is Unary){
2426 Unary right_unary = (Unary) right;
2428 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2429 oper = Operator.Subtraction;
2430 right = right_unary.Expr;
2436 if (oper == Operator.Equality || oper == Operator.Inequality){
2437 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2438 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2439 Error_OperatorCannotBeApplied ();
2443 type = TypeManager.bool_type;
2448 // operator != (object a, object b)
2449 // operator == (object a, object b)
2451 // For this to be used, both arguments have to be reference-types.
2452 // Read the rationale on the spec (14.9.6)
2454 // Also, if at compile time we know that the classes do not inherit
2455 // one from the other, then we catch the error there.
2457 if (!(l.IsValueType || r.IsValueType)){
2458 type = TypeManager.bool_type;
2463 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
2467 // Also, a standard conversion must exist from either one
2469 if (!(Convert.ImplicitStandardConversionExists (left, r) ||
2470 Convert.ImplicitStandardConversionExists (right, l))){
2471 Error_OperatorCannotBeApplied ();
2475 // We are going to have to convert to an object to compare
2477 if (l != TypeManager.object_type)
2478 left = new EmptyCast (left, TypeManager.object_type);
2479 if (r != TypeManager.object_type)
2480 right = new EmptyCast (right, TypeManager.object_type);
2483 // FIXME: CSC here catches errors cs254 and cs252
2489 // One of them is a valuetype, but the other one is not.
2491 if (!l.IsValueType || !r.IsValueType) {
2492 Error_OperatorCannotBeApplied ();
2497 // Only perform numeric promotions on:
2498 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2500 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2501 if (TypeManager.IsDelegateType (l)){
2502 if (right.eclass == ExprClass.MethodGroup && RootContext.V2){
2503 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2510 if (TypeManager.IsDelegateType (r)){
2512 ArrayList args = new ArrayList (2);
2514 args = new ArrayList (2);
2515 args.Add (new Argument (left, Argument.AType.Expression));
2516 args.Add (new Argument (right, Argument.AType.Expression));
2518 if (oper == Operator.Addition)
2519 method = TypeManager.delegate_combine_delegate_delegate;
2521 method = TypeManager.delegate_remove_delegate_delegate;
2524 Error_OperatorCannotBeApplied ();
2528 return new BinaryDelegate (l, method, args);
2533 // Pointer arithmetic:
2535 // T* operator + (T* x, int y);
2536 // T* operator + (T* x, uint y);
2537 // T* operator + (T* x, long y);
2538 // T* operator + (T* x, ulong y);
2540 // T* operator + (int y, T* x);
2541 // T* operator + (uint y, T *x);
2542 // T* operator + (long y, T *x);
2543 // T* operator + (ulong y, T *x);
2545 // T* operator - (T* x, int y);
2546 // T* operator - (T* x, uint y);
2547 // T* operator - (T* x, long y);
2548 // T* operator - (T* x, ulong y);
2550 // long operator - (T* x, T *y)
2553 if (r.IsPointer && oper == Operator.Subtraction){
2555 return new PointerArithmetic (
2556 false, left, right, TypeManager.int64_type,
2559 Expression t = Make32or64 (ec, right);
2561 return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc);
2563 } else if (r.IsPointer && oper == Operator.Addition){
2564 Expression t = Make32or64 (ec, left);
2566 return new PointerArithmetic (true, right, t, r, loc);
2571 // Enumeration operators
2573 bool lie = TypeManager.IsEnumType (l);
2574 bool rie = TypeManager.IsEnumType (r);
2578 // U operator - (E e, E f)
2580 if (oper == Operator.Subtraction){
2582 type = TypeManager.EnumToUnderlying (l);
2585 Error_OperatorCannotBeApplied ();
2591 // operator + (E e, U x)
2592 // operator - (E e, U x)
2594 if (oper == Operator.Addition || oper == Operator.Subtraction){
2595 Type enum_type = lie ? l : r;
2596 Type other_type = lie ? r : l;
2597 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2599 if (underlying_type != other_type){
2600 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2610 Error_OperatorCannotBeApplied ();
2619 temp = Convert.ImplicitConversion (ec, right, l, loc);
2623 Error_OperatorCannotBeApplied ();
2627 temp = Convert.ImplicitConversion (ec, left, r, loc);
2632 Error_OperatorCannotBeApplied ();
2637 if (oper == Operator.Equality || oper == Operator.Inequality ||
2638 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2639 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2640 if (left.Type != right.Type){
2641 Error_OperatorCannotBeApplied ();
2644 type = TypeManager.bool_type;
2648 if (oper == Operator.BitwiseAnd ||
2649 oper == Operator.BitwiseOr ||
2650 oper == Operator.ExclusiveOr){
2654 Error_OperatorCannotBeApplied ();
2658 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2659 return CheckShiftArguments (ec);
2661 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2662 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2663 type = TypeManager.bool_type;
2668 Error_OperatorCannotBeApplied ();
2672 Expression e = new ConditionalLogicalOperator (
2673 oper == Operator.LogicalAnd, left, right, l, loc);
2674 return e.Resolve (ec);
2678 // operator & (bool x, bool y)
2679 // operator | (bool x, bool y)
2680 // operator ^ (bool x, bool y)
2682 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2683 if (oper == Operator.BitwiseAnd ||
2684 oper == Operator.BitwiseOr ||
2685 oper == Operator.ExclusiveOr){
2692 // Pointer comparison
2694 if (l.IsPointer && r.IsPointer){
2695 if (oper == Operator.Equality || oper == Operator.Inequality ||
2696 oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2697 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2698 type = TypeManager.bool_type;
2704 // We are dealing with numbers
2706 if (overload_failed){
2707 Error_OperatorCannotBeApplied ();
2712 // This will leave left or right set to null if there is an error
2714 bool check_user_conv = is_user_defined (l) && is_user_defined (r);
2715 DoNumericPromotions (ec, l, r, check_user_conv);
2716 if (left == null || right == null){
2717 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2722 // reload our cached types if required
2727 if (oper == Operator.BitwiseAnd ||
2728 oper == Operator.BitwiseOr ||
2729 oper == Operator.ExclusiveOr){
2731 if (((l == TypeManager.int32_type) ||
2732 (l == TypeManager.uint32_type) ||
2733 (l == TypeManager.short_type) ||
2734 (l == TypeManager.ushort_type) ||
2735 (l == TypeManager.int64_type) ||
2736 (l == TypeManager.uint64_type))){
2739 Error_OperatorCannotBeApplied ();
2743 Error_OperatorCannotBeApplied ();
2748 if (oper == Operator.Equality ||
2749 oper == Operator.Inequality ||
2750 oper == Operator.LessThanOrEqual ||
2751 oper == Operator.LessThan ||
2752 oper == Operator.GreaterThanOrEqual ||
2753 oper == Operator.GreaterThan){
2754 type = TypeManager.bool_type;
2760 public override Expression DoResolve (EmitContext ec)
2762 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2763 left = ((ParenthesizedExpression) left).Expr;
2764 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2768 if (left.eclass == ExprClass.Type) {
2769 Error (75, "Casting a negative value needs to have the value in parentheses.");
2773 left = left.Resolve (ec);
2774 right = right.Resolve (ec);
2776 if (left == null || right == null)
2779 eclass = ExprClass.Value;
2781 Constant rc = right as Constant;
2782 Constant lc = left as Constant;
2784 if (rc != null & lc != null){
2785 Expression e = ConstantFold.BinaryFold (
2786 ec, oper, lc, rc, loc);
2791 return ResolveOperator (ec);
2795 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2796 /// context of a conditional bool expression. This function will return
2797 /// false if it is was possible to use EmitBranchable, or true if it was.
2799 /// The expression's code is generated, and we will generate a branch to `target'
2800 /// if the resulting expression value is equal to isTrue
2802 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
2804 ILGenerator ig = ec.ig;
2807 // This is more complicated than it looks, but its just to avoid
2808 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2809 // but on top of that we want for == and != to use a special path
2810 // if we are comparing against null
2812 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
2813 bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
2816 // put the constant on the rhs, for simplicity
2818 if (left is Constant) {
2819 Expression swap = right;
2824 if (((Constant) right).IsZeroInteger) {
2827 ig.Emit (OpCodes.Brtrue, target);
2829 ig.Emit (OpCodes.Brfalse, target);
2832 } else if (right is BoolConstant){
2834 if (my_on_true != ((BoolConstant) right).Value)
2835 ig.Emit (OpCodes.Brtrue, target);
2837 ig.Emit (OpCodes.Brfalse, target);
2842 } else if (oper == Operator.LogicalAnd) {
2845 Label tests_end = ig.DefineLabel ();
2847 left.EmitBranchable (ec, tests_end, false);
2848 right.EmitBranchable (ec, target, true);
2849 ig.MarkLabel (tests_end);
2851 left.EmitBranchable (ec, target, false);
2852 right.EmitBranchable (ec, target, false);
2857 } else if (oper == Operator.LogicalOr){
2859 left.EmitBranchable (ec, target, true);
2860 right.EmitBranchable (ec, target, true);
2863 Label tests_end = ig.DefineLabel ();
2864 left.EmitBranchable (ec, tests_end, true);
2865 right.EmitBranchable (ec, target, false);
2866 ig.MarkLabel (tests_end);
2871 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
2872 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
2873 oper == Operator.Equality || oper == Operator.Inequality)) {
2874 base.EmitBranchable (ec, target, onTrue);
2882 bool isUnsigned = is_unsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
2885 case Operator.Equality:
2887 ig.Emit (OpCodes.Beq, target);
2889 ig.Emit (OpCodes.Bne_Un, target);
2892 case Operator.Inequality:
2894 ig.Emit (OpCodes.Bne_Un, target);
2896 ig.Emit (OpCodes.Beq, target);
2899 case Operator.LessThan:
2902 ig.Emit (OpCodes.Blt_Un, target);
2904 ig.Emit (OpCodes.Blt, target);
2907 ig.Emit (OpCodes.Bge_Un, target);
2909 ig.Emit (OpCodes.Bge, target);
2912 case Operator.GreaterThan:
2915 ig.Emit (OpCodes.Bgt_Un, target);
2917 ig.Emit (OpCodes.Bgt, target);
2920 ig.Emit (OpCodes.Ble_Un, target);
2922 ig.Emit (OpCodes.Ble, target);
2925 case Operator.LessThanOrEqual:
2928 ig.Emit (OpCodes.Ble_Un, target);
2930 ig.Emit (OpCodes.Ble, target);
2933 ig.Emit (OpCodes.Bgt_Un, target);
2935 ig.Emit (OpCodes.Bgt, target);
2939 case Operator.GreaterThanOrEqual:
2942 ig.Emit (OpCodes.Bge_Un, target);
2944 ig.Emit (OpCodes.Bge, target);
2947 ig.Emit (OpCodes.Blt_Un, target);
2949 ig.Emit (OpCodes.Blt, target);
2952 Console.WriteLine (oper);
2953 throw new Exception ("what is THAT");
2957 public override void Emit (EmitContext ec)
2959 ILGenerator ig = ec.ig;
2964 // Handle short-circuit operators differently
2967 if (oper == Operator.LogicalAnd) {
2968 Label load_zero = ig.DefineLabel ();
2969 Label end = ig.DefineLabel ();
2971 left.EmitBranchable (ec, load_zero, false);
2973 ig.Emit (OpCodes.Br, end);
2975 ig.MarkLabel (load_zero);
2976 ig.Emit (OpCodes.Ldc_I4_0);
2979 } else if (oper == Operator.LogicalOr) {
2980 Label load_one = ig.DefineLabel ();
2981 Label end = ig.DefineLabel ();
2983 left.EmitBranchable (ec, load_one, true);
2985 ig.Emit (OpCodes.Br, end);
2987 ig.MarkLabel (load_one);
2988 ig.Emit (OpCodes.Ldc_I4_1);
2996 bool isUnsigned = is_unsigned (left.Type);
2999 case Operator.Multiply:
3001 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3002 opcode = OpCodes.Mul_Ovf;
3003 else if (isUnsigned)
3004 opcode = OpCodes.Mul_Ovf_Un;
3006 opcode = OpCodes.Mul;
3008 opcode = OpCodes.Mul;
3012 case Operator.Division:
3014 opcode = OpCodes.Div_Un;
3016 opcode = OpCodes.Div;
3019 case Operator.Modulus:
3021 opcode = OpCodes.Rem_Un;
3023 opcode = OpCodes.Rem;
3026 case Operator.Addition:
3028 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3029 opcode = OpCodes.Add_Ovf;
3030 else if (isUnsigned)
3031 opcode = OpCodes.Add_Ovf_Un;
3033 opcode = OpCodes.Add;
3035 opcode = OpCodes.Add;
3038 case Operator.Subtraction:
3040 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3041 opcode = OpCodes.Sub_Ovf;
3042 else if (isUnsigned)
3043 opcode = OpCodes.Sub_Ovf_Un;
3045 opcode = OpCodes.Sub;
3047 opcode = OpCodes.Sub;
3050 case Operator.RightShift:
3052 opcode = OpCodes.Shr_Un;
3054 opcode = OpCodes.Shr;
3057 case Operator.LeftShift:
3058 opcode = OpCodes.Shl;
3061 case Operator.Equality:
3062 opcode = OpCodes.Ceq;
3065 case Operator.Inequality:
3066 ig.Emit (OpCodes.Ceq);
3067 ig.Emit (OpCodes.Ldc_I4_0);
3069 opcode = OpCodes.Ceq;
3072 case Operator.LessThan:
3074 opcode = OpCodes.Clt_Un;
3076 opcode = OpCodes.Clt;
3079 case Operator.GreaterThan:
3081 opcode = OpCodes.Cgt_Un;
3083 opcode = OpCodes.Cgt;
3086 case Operator.LessThanOrEqual:
3087 Type lt = left.Type;
3089 if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
3090 ig.Emit (OpCodes.Cgt_Un);
3092 ig.Emit (OpCodes.Cgt);
3093 ig.Emit (OpCodes.Ldc_I4_0);
3095 opcode = OpCodes.Ceq;
3098 case Operator.GreaterThanOrEqual:
3099 Type le = left.Type;
3101 if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
3102 ig.Emit (OpCodes.Clt_Un);
3104 ig.Emit (OpCodes.Clt);
3106 ig.Emit (OpCodes.Ldc_I4_0);
3108 opcode = OpCodes.Ceq;
3111 case Operator.BitwiseOr:
3112 opcode = OpCodes.Or;
3115 case Operator.BitwiseAnd:
3116 opcode = OpCodes.And;
3119 case Operator.ExclusiveOr:
3120 opcode = OpCodes.Xor;
3124 throw new Exception ("This should not happen: Operator = "
3125 + oper.ToString ());
3133 // Object created by Binary when the binary operator uses an method instead of being
3134 // a binary operation that maps to a CIL binary operation.
3136 public class BinaryMethod : Expression {
3137 public MethodBase method;
3138 public ArrayList Arguments;
3140 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3145 eclass = ExprClass.Value;
3148 public override Expression DoResolve (EmitContext ec)
3153 public override void Emit (EmitContext ec)
3155 ILGenerator ig = ec.ig;
3157 if (Arguments != null)
3158 Invocation.EmitArguments (ec, method, Arguments);
3160 if (method is MethodInfo)
3161 ig.Emit (OpCodes.Call, (MethodInfo) method);
3163 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3168 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3169 // b, c, d... may be strings or objects.
3171 public class StringConcat : Expression {
3173 bool invalid = false;
3176 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3179 type = TypeManager.string_type;
3180 eclass = ExprClass.Value;
3182 operands = new ArrayList (2);
3187 public override Expression DoResolve (EmitContext ec)
3195 public void Append (EmitContext ec, Expression operand)
3200 if (operand is StringConstant && operands.Count != 0) {
3201 StringConstant last_operand = operands [operands.Count - 1] as StringConstant;
3202 if (last_operand != null) {
3203 operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value);
3209 // Conversion to object
3211 if (operand.Type != TypeManager.string_type) {
3212 Expression no = Convert.ImplicitConversion (ec, operand, TypeManager.object_type, loc);
3215 Binary.Error_OperatorCannotBeApplied (loc, "+", TypeManager.string_type, operand.Type);
3221 operands.Add (operand);
3224 public override void Emit (EmitContext ec)
3226 MethodInfo concat_method = null;
3229 // Are we also concating objects?
3231 bool is_strings_only = true;
3234 // Do conversion to arguments; check for strings only
3236 for (int i = 0; i < operands.Count; i ++) {
3237 Expression e = (Expression) operands [i];
3238 is_strings_only &= e.Type == TypeManager.string_type;
3241 for (int i = 0; i < operands.Count; i ++) {
3242 Expression e = (Expression) operands [i];
3244 if (! is_strings_only && e.Type == TypeManager.string_type) {
3245 // need to make sure this is an object, because the EmitParams
3246 // method might look at the type of this expression, see it is a
3247 // string and emit a string [] when we want an object [];
3249 e = Convert.ImplicitConversion (ec, e, TypeManager.object_type, loc);
3251 operands [i] = new Argument (e, Argument.AType.Expression);
3255 // Find the right method
3257 switch (operands.Count) {
3260 // This should not be possible, because simple constant folding
3261 // is taken care of in the Binary code.
3263 throw new Exception ("how did you get here?");
3266 concat_method = is_strings_only ?
3267 TypeManager.string_concat_string_string :
3268 TypeManager.string_concat_object_object ;
3271 concat_method = is_strings_only ?
3272 TypeManager.string_concat_string_string_string :
3273 TypeManager.string_concat_object_object_object ;
3277 // There is not a 4 param overlaod for object (the one that there is
3278 // is actually a varargs methods, and is only in corlib because it was
3279 // introduced there before.).
3281 if (!is_strings_only)
3284 concat_method = TypeManager.string_concat_string_string_string_string;
3287 concat_method = is_strings_only ?
3288 TypeManager.string_concat_string_dot_dot_dot :
3289 TypeManager.string_concat_object_dot_dot_dot ;
3293 Invocation.EmitArguments (ec, concat_method, operands);
3294 ec.ig.Emit (OpCodes.Call, concat_method);
3299 // Object created with +/= on delegates
3301 public class BinaryDelegate : Expression {
3305 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3310 eclass = ExprClass.Value;
3313 public override Expression DoResolve (EmitContext ec)
3318 public override void Emit (EmitContext ec)
3320 ILGenerator ig = ec.ig;
3322 Invocation.EmitArguments (ec, method, args);
3324 ig.Emit (OpCodes.Call, (MethodInfo) method);
3325 ig.Emit (OpCodes.Castclass, type);
3328 public Expression Right {
3330 Argument arg = (Argument) args [1];
3335 public bool IsAddition {
3337 return method == TypeManager.delegate_combine_delegate_delegate;
3343 // User-defined conditional logical operator
3344 public class ConditionalLogicalOperator : Expression {
3345 Expression left, right;
3348 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3351 eclass = ExprClass.Value;
3355 this.is_and = is_and;
3358 protected void Error19 ()
3360 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
3363 protected void Error218 ()
3365 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3366 "declarations of operator true and operator false");
3369 Expression op_true, op_false, op;
3370 LocalTemporary left_temp;
3372 public override Expression DoResolve (EmitContext ec)
3375 Expression operator_group;
3377 operator_group = MethodLookup (ec, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc);
3378 if (operator_group == null) {
3383 left_temp = new LocalTemporary (ec, type);
3385 ArrayList arguments = new ArrayList ();
3386 arguments.Add (new Argument (left_temp, Argument.AType.Expression));
3387 arguments.Add (new Argument (right, Argument.AType.Expression));
3388 method = Invocation.OverloadResolve (
3389 ec, (MethodGroupExpr) operator_group, arguments, false, loc)
3391 if ((method == null) || (method.ReturnType != type)) {
3396 op = new StaticCallExpr (method, arguments, loc);
3398 op_true = GetOperatorTrue (ec, left_temp, loc);
3399 op_false = GetOperatorFalse (ec, left_temp, loc);
3400 if ((op_true == null) || (op_false == null)) {
3408 public override void Emit (EmitContext ec)
3410 ILGenerator ig = ec.ig;
3411 Label false_target = ig.DefineLabel ();
3412 Label end_target = ig.DefineLabel ();
3414 ig.Emit (OpCodes.Nop);
3417 left_temp.Store (ec);
3419 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3420 left_temp.Emit (ec);
3421 ig.Emit (OpCodes.Br, end_target);
3422 ig.MarkLabel (false_target);
3424 ig.MarkLabel (end_target);
3426 ig.Emit (OpCodes.Nop);
3430 public class PointerArithmetic : Expression {
3431 Expression left, right;
3435 // We assume that `l' is always a pointer
3437 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3440 eclass = ExprClass.Variable;
3444 is_add = is_addition;
3447 public override Expression DoResolve (EmitContext ec)
3450 // We are born fully resolved
3455 public override void Emit (EmitContext ec)
3457 Type op_type = left.Type;
3458 ILGenerator ig = ec.ig;
3459 int size = GetTypeSize (TypeManager.GetElementType (op_type));
3460 Type rtype = right.Type;
3462 if (rtype.IsPointer){
3464 // handle (pointer - pointer)
3468 ig.Emit (OpCodes.Sub);
3472 ig.Emit (OpCodes.Sizeof, op_type);
3474 IntLiteral.EmitInt (ig, size);
3475 ig.Emit (OpCodes.Div);
3477 ig.Emit (OpCodes.Conv_I8);
3480 // handle + and - on (pointer op int)
3483 ig.Emit (OpCodes.Conv_I);
3487 ig.Emit (OpCodes.Sizeof, op_type);
3489 IntLiteral.EmitInt (ig, size);
3490 if (rtype == TypeManager.int64_type)
3491 ig.Emit (OpCodes.Conv_I8);
3492 else if (rtype == TypeManager.uint64_type)
3493 ig.Emit (OpCodes.Conv_U8);
3494 ig.Emit (OpCodes.Mul);
3495 ig.Emit (OpCodes.Conv_I);
3498 ig.Emit (OpCodes.Add);
3500 ig.Emit (OpCodes.Sub);
3506 /// Implements the ternary conditional operator (?:)
3508 public class Conditional : Expression {
3509 Expression expr, trueExpr, falseExpr;
3511 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3514 this.trueExpr = trueExpr;
3515 this.falseExpr = falseExpr;
3519 public Expression Expr {
3525 public Expression TrueExpr {
3531 public Expression FalseExpr {
3537 public override Expression DoResolve (EmitContext ec)
3539 expr = expr.Resolve (ec);
3544 if (expr.Type != TypeManager.bool_type){
3545 expr = Expression.ResolveBoolean (
3552 trueExpr = trueExpr.Resolve (ec);
3553 falseExpr = falseExpr.Resolve (ec);
3555 if (trueExpr == null || falseExpr == null)
3558 eclass = ExprClass.Value;
3559 if (trueExpr.Type == falseExpr.Type)
3560 type = trueExpr.Type;
3563 Type true_type = trueExpr.Type;
3564 Type false_type = falseExpr.Type;
3566 if (trueExpr is NullLiteral){
3569 } else if (falseExpr is NullLiteral){
3575 // First, if an implicit conversion exists from trueExpr
3576 // to falseExpr, then the result type is of type falseExpr.Type
3578 conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
3581 // Check if both can convert implicitl to each other's type
3583 if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
3585 "Can not compute type of conditional expression " +
3586 "as `" + TypeManager.CSharpName (trueExpr.Type) +
3587 "' and `" + TypeManager.CSharpName (falseExpr.Type) +
3588 "' convert implicitly to each other");
3593 } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
3597 Error (173, "The type of the conditional expression can " +
3598 "not be computed because there is no implicit conversion" +
3599 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3600 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3605 if (expr is BoolConstant){
3606 BoolConstant bc = (BoolConstant) expr;
3617 public override void Emit (EmitContext ec)
3619 ILGenerator ig = ec.ig;
3620 Label false_target = ig.DefineLabel ();
3621 Label end_target = ig.DefineLabel ();
3623 expr.EmitBranchable (ec, false_target, false);
3625 ig.Emit (OpCodes.Br, end_target);
3626 ig.MarkLabel (false_target);
3627 falseExpr.Emit (ec);
3628 ig.MarkLabel (end_target);
3636 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3637 public readonly string Name;
3638 public readonly Block Block;
3639 LocalInfo local_info;
3642 public LocalVariableReference (Block block, string name, Location l)
3647 eclass = ExprClass.Variable;
3650 // Setting `is_readonly' to false will allow you to create a writable
3651 // reference to a read-only variable. This is used by foreach and using.
3652 public LocalVariableReference (Block block, string name, Location l,
3653 LocalInfo local_info, bool is_readonly)
3654 : this (block, name, l)
3656 this.local_info = local_info;
3657 this.is_readonly = is_readonly;
3660 public VariableInfo VariableInfo {
3661 get { return local_info.VariableInfo; }
3664 public bool IsReadOnly {
3670 protected void DoResolveBase (EmitContext ec)
3672 if (local_info == null) {
3673 local_info = Block.GetLocalInfo (Name);
3674 is_readonly = local_info.ReadOnly;
3677 type = local_info.VariableType;
3679 if (ec.InAnonymousMethod)
3680 Block.LiftVariable (local_info);
3684 protected Expression DoResolve (EmitContext ec, bool is_lvalue)
3686 Expression e = Block.GetConstantExpression (Name);
3688 local_info.Used = true;
3689 eclass = ExprClass.Value;
3693 VariableInfo variable_info = local_info.VariableInfo;
3694 if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
3698 local_info.Used = true;
3700 if (local_info.LocalBuilder == null)
3701 return ec.RemapLocal (local_info);
3706 public override Expression DoResolve (EmitContext ec)
3710 return DoResolve (ec, false);
3713 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3717 VariableInfo variable_info = local_info.VariableInfo;
3718 if (variable_info != null)
3719 variable_info.SetAssigned (ec);
3721 Expression e = DoResolve (ec, true);
3727 Error (1604, "cannot assign to `" + Name + "' because it is readonly");
3731 if (local_info.LocalBuilder == null)
3732 return ec.RemapLocalLValue (local_info, right_side);
3737 public bool VerifyFixed (bool is_expression)
3739 return !is_expression || local_info.IsFixed;
3742 public override void Emit (EmitContext ec)
3744 ILGenerator ig = ec.ig;
3746 ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
3749 public void EmitAssign (EmitContext ec, Expression source)
3751 ILGenerator ig = ec.ig;
3754 ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
3757 public void AddressOf (EmitContext ec, AddressOp mode)
3759 ILGenerator ig = ec.ig;
3761 ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
3764 public override string ToString ()
3766 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
3771 /// This represents a reference to a parameter in the intermediate
3774 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3780 public Parameter.Modifier mod;
3781 public bool is_ref, is_out;
3783 public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
3790 eclass = ExprClass.Variable;
3793 public VariableInfo VariableInfo {
3797 public bool VerifyFixed (bool is_expression)
3799 return !is_expression || TypeManager.IsValueType (type);
3802 public bool IsAssigned (EmitContext ec, Location loc)
3804 if (!ec.DoFlowAnalysis || !is_out ||
3805 ec.CurrentBranching.IsAssigned (vi))
3808 Report.Error (165, loc,
3809 "Use of unassigned parameter `" + name + "'");
3813 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
3815 if (!ec.DoFlowAnalysis || !is_out ||
3816 ec.CurrentBranching.IsFieldAssigned (vi, field_name))
3819 Report.Error (170, loc,
3820 "Use of possibly unassigned field `" + field_name + "'");
3824 public void SetAssigned (EmitContext ec)
3826 if (is_out && ec.DoFlowAnalysis)
3827 ec.CurrentBranching.SetAssigned (vi);
3830 public void SetFieldAssigned (EmitContext ec, string field_name)
3832 if (is_out && ec.DoFlowAnalysis)
3833 ec.CurrentBranching.SetFieldAssigned (vi, field_name);
3836 protected void DoResolveBase (EmitContext ec)
3838 type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
3839 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3840 is_out = (mod & Parameter.Modifier.OUT) != 0;
3841 eclass = ExprClass.Variable;
3844 vi = block.ParameterMap [idx];
3848 // Notice that for ref/out parameters, the type exposed is not the
3849 // same type exposed externally.
3852 // externally we expose "int&"
3853 // here we expose "int".
3855 // We record this in "is_ref". This means that the type system can treat
3856 // the type as it is expected, but when we generate the code, we generate
3857 // the alternate kind of code.
3859 public override Expression DoResolve (EmitContext ec)
3863 if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
3866 if (ec.RemapToProxy)
3867 return ec.RemapParameter (idx);
3872 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3878 if (ec.RemapToProxy)
3879 return ec.RemapParameterLValue (idx, right_side);
3884 static public void EmitLdArg (ILGenerator ig, int x)
3888 case 0: ig.Emit (OpCodes.Ldarg_0); break;
3889 case 1: ig.Emit (OpCodes.Ldarg_1); break;
3890 case 2: ig.Emit (OpCodes.Ldarg_2); break;
3891 case 3: ig.Emit (OpCodes.Ldarg_3); break;
3892 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
3895 ig.Emit (OpCodes.Ldarg, x);
3899 // This method is used by parameters that are references, that are
3900 // being passed as references: we only want to pass the pointer (that
3901 // is already stored in the parameter, not the address of the pointer,
3902 // and not the value of the variable).
3904 public void EmitLoad (EmitContext ec)
3906 ILGenerator ig = ec.ig;
3912 EmitLdArg (ig, arg_idx);
3915 public override void Emit (EmitContext ec)
3917 ILGenerator ig = ec.ig;
3924 EmitLdArg (ig, arg_idx);
3930 // If we are a reference, we loaded on the stack a pointer
3931 // Now lets load the real value
3933 LoadFromPtr (ig, type);
3936 public void EmitAssign (EmitContext ec, Expression source)
3938 ILGenerator ig = ec.ig;
3946 EmitLdArg (ig, arg_idx);
3951 StoreFromPtr (ig, type);
3954 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3956 ig.Emit (OpCodes.Starg, arg_idx);
3960 public void AddressOf (EmitContext ec, AddressOp mode)
3969 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3971 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3974 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3976 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3983 /// Used for arguments to New(), Invocation()
3985 public class Argument {
3986 public enum AType : byte {
3992 public readonly AType ArgType;
3993 public Expression Expr;
3995 public Argument (Expression expr, AType type)
3998 this.ArgType = type;
4003 if (ArgType == AType.Ref || ArgType == AType.Out)
4004 return TypeManager.GetReferenceType (Expr.Type);
4010 public Parameter.Modifier GetParameterModifier ()
4014 return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
4017 return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
4020 return Parameter.Modifier.NONE;
4024 public static string FullDesc (Argument a)
4026 return (a.ArgType == AType.Ref ? "ref " :
4027 (a.ArgType == AType.Out ? "out " : "")) +
4028 TypeManager.CSharpName (a.Expr.Type);
4031 public bool ResolveMethodGroup (EmitContext ec, Location loc)
4033 ConstructedType ctype = Expr as ConstructedType;
4035 Expr = ctype.GetMemberAccess (ec);
4037 // FIXME: csc doesn't report any error if you try to use `ref' or
4038 // `out' in a delegate creation expression.
4039 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4046 public bool Resolve (EmitContext ec, Location loc)
4048 if (ArgType == AType.Ref) {
4049 Expr = Expr.Resolve (ec);
4053 Expr = Expr.ResolveLValue (ec, Expr);
4054 } else if (ArgType == AType.Out)
4055 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
4057 Expr = Expr.Resolve (ec);
4062 if (ArgType == AType.Expression)
4066 // Catch errors where fields of a MarshalByRefObject are passed as ref or out
4067 // This is only allowed for `this'
4069 FieldExpr fe = Expr as FieldExpr;
4070 if (fe != null && !fe.IsStatic){
4071 Expression instance = fe.InstanceExpression;
4073 if (instance.GetType () != typeof (This)){
4074 if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
4075 Report.Error (197, loc,
4076 "Can not pass a type that derives from MarshalByRefObject with out or ref");
4083 if (Expr.eclass != ExprClass.Variable){
4085 // We just probe to match the CSC output
4087 if (Expr.eclass == ExprClass.PropertyAccess ||
4088 Expr.eclass == ExprClass.IndexerAccess){
4091 "A property or indexer can not be passed as an out or ref " +
4096 "An lvalue is required as an argument to out or ref");
4104 public void Emit (EmitContext ec)
4107 // Ref and Out parameters need to have their addresses taken.
4109 // ParameterReferences might already be references, so we want
4110 // to pass just the value
4112 if (ArgType == AType.Ref || ArgType == AType.Out){
4113 AddressOp mode = AddressOp.Store;
4115 if (ArgType == AType.Ref)
4116 mode |= AddressOp.Load;
4118 if (Expr is ParameterReference){
4119 ParameterReference pr = (ParameterReference) Expr;
4125 pr.AddressOf (ec, mode);
4128 ((IMemoryLocation)Expr).AddressOf (ec, mode);
4136 /// Invocation of methods or delegates.
4138 public class Invocation : ExpressionStatement {
4139 public readonly ArrayList Arguments;
4142 MethodBase method = null;
4145 static Hashtable method_parameter_cache;
4147 static Invocation ()
4149 method_parameter_cache = new PtrHashtable ();
4153 // arguments is an ArrayList, but we do not want to typecast,
4154 // as it might be null.
4156 // FIXME: only allow expr to be a method invocation or a
4157 // delegate invocation (7.5.5)
4159 public Invocation (Expression expr, ArrayList arguments, Location l)
4162 Arguments = arguments;
4166 public Expression Expr {
4173 /// Returns the Parameters (a ParameterData interface) for the
4176 public static ParameterData GetParameterData (MethodBase mb)
4178 object pd = method_parameter_cache [mb];
4182 return (ParameterData) pd;
4184 ip = TypeManager.LookupParametersByBuilder (mb);
4186 method_parameter_cache [mb] = ip;
4188 return (ParameterData) ip;
4190 ReflectionParameters rp = new ReflectionParameters (mb);
4191 method_parameter_cache [mb] = rp;
4193 return (ParameterData) rp;
4198 /// Determines "better conversion" as specified in 7.4.2.3
4200 /// Returns : 1 if a->p is better
4201 /// 0 if a->q or neither is better
4203 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
4205 Type argument_type = a.Type;
4206 Expression argument_expr = a.Expr;
4208 if (argument_type == null)
4209 throw new Exception ("Expression of type " + a.Expr +
4210 " does not resolve its type");
4213 // This is a special case since csc behaves this way.
4215 if (argument_expr is NullLiteral &&
4216 p == TypeManager.string_type &&
4217 q == TypeManager.object_type)
4219 else if (argument_expr is NullLiteral &&
4220 p == TypeManager.object_type &&
4221 q == TypeManager.string_type)
4225 // csc behaves this way so we emulate it. Basically, if the argument
4226 // is null and one of the types to compare is 'object' and the other
4227 // is a reference type, we prefer the other.
4229 // I can't find this anywhere in the spec but we can interpret this
4230 // to mean that null can be of any type you wish in such a context
4232 if (p != null && q != null) {
4233 if (argument_expr is NullLiteral &&
4235 q == TypeManager.object_type)
4237 else if (argument_expr is NullLiteral &&
4239 p == TypeManager.object_type)
4246 if (argument_type == p)
4249 if (argument_type == q)
4253 // Now probe whether an implicit constant expression conversion
4256 // An implicit constant expression conversion permits the following
4259 // * A constant-expression of type `int' can be converted to type
4260 // sbyte, byute, short, ushort, uint, ulong provided the value of
4261 // of the expression is withing the range of the destination type.
4263 // * A constant-expression of type long can be converted to type
4264 // ulong, provided the value of the constant expression is not negative
4266 // FIXME: Note that this assumes that constant folding has
4267 // taken place. We dont do constant folding yet.
4270 if (argument_expr is IntConstant){
4271 IntConstant ei = (IntConstant) argument_expr;
4272 int value = ei.Value;
4274 if (p == TypeManager.sbyte_type){
4275 if (value >= SByte.MinValue && value <= SByte.MaxValue)
4277 } else if (p == TypeManager.byte_type){
4278 if (q == TypeManager.sbyte_type &&
4279 value >= SByte.MinValue && value <= SByte.MaxValue)
4281 else if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
4283 } else if (p == TypeManager.short_type){
4284 if (value >= Int16.MinValue && value <= Int16.MaxValue)
4286 } else if (p == TypeManager.ushort_type){
4287 if (q == TypeManager.short_type &&
4288 value >= Int16.MinValue && value <= Int16.MaxValue)
4290 else if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
4292 } else if (p == TypeManager.int32_type){
4293 if (value >= Int32.MinValue && value <= Int32.MaxValue)
4295 } else if (p == TypeManager.uint32_type){
4297 // we can optimize this case: a positive int32
4298 // always fits on a uint32
4302 } else if (p == TypeManager.uint64_type){
4304 // we can optimize this case: a positive int32
4305 // always fits on a uint64
4309 // This special case is needed because csc behaves like this.
4310 // int -> uint is better than int -> ulong!
4312 if (q == TypeManager.uint32_type)
4315 if (q == TypeManager.int64_type)
4317 else if (value >= 0)
4319 } else if (p == TypeManager.int64_type){
4322 } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
4323 LongConstant lc = (LongConstant) argument_expr;
4325 if (p == TypeManager.uint64_type){
4332 Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc);
4340 Expression p_tmp = new EmptyExpression (p);
4341 Expression q_tmp = new EmptyExpression (q);
4343 if (Convert.ImplicitConversionExists (ec, p_tmp, q) == true &&
4344 Convert.ImplicitConversionExists (ec, q_tmp, p) == false)
4347 if (p == TypeManager.sbyte_type)
4348 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
4349 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4352 if (p == TypeManager.short_type)
4353 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
4354 q == TypeManager.uint64_type)
4357 if (p == TypeManager.int32_type)
4358 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4361 if (p == TypeManager.int64_type)
4362 if (q == TypeManager.uint64_type)
4369 /// Determines "Better function" between candidate
4370 /// and the current best match
4373 /// Returns an integer indicating :
4374 /// 0 if candidate ain't better
4375 /// 1 if candidate is better than the current best match
4377 static int BetterFunction (EmitContext ec, ArrayList args,
4378 MethodBase candidate, bool candidate_params,
4379 MethodBase best, bool best_params,
4382 ParameterData candidate_pd = GetParameterData (candidate);
4383 ParameterData best_pd;
4389 argument_count = args.Count;
4391 int cand_count = candidate_pd.Count;
4394 // If there is no best method, than this one
4395 // is better, however, if we already found a
4396 // best method, we cant tell. This happens
4407 // interface IFooBar : IFoo, IBar {}
4409 // We cant tell if IFoo.DoIt is better than IBar.DoIt
4411 // However, we have to consider that
4412 // Trim (); is better than Trim (params char[] chars);
4414 if (cand_count == 0 && argument_count == 0)
4415 return best == null || best_params ? 1 : 0;
4417 if (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS)
4418 if (cand_count != argument_count)
4424 if (argument_count == 0 && cand_count == 1 &&
4425 candidate_pd.ParameterModifier (cand_count - 1) == Parameter.Modifier.PARAMS)
4428 for (int j = 0; j < argument_count; ++j) {
4430 Argument a = (Argument) args [j];
4431 Type t = candidate_pd.ParameterType (j);
4433 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4434 if (candidate_params)
4435 t = TypeManager.GetElementType (t);
4437 x = BetterConversion (ec, a, t, null, loc);
4449 best_pd = GetParameterData (best);
4451 int rating1 = 0, rating2 = 0;
4453 for (int j = 0; j < argument_count; ++j) {
4456 Argument a = (Argument) args [j];
4458 Type ct = candidate_pd.ParameterType (j);
4459 Type bt = best_pd.ParameterType (j);
4461 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4462 if (candidate_params)
4463 ct = TypeManager.GetElementType (ct);
4465 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4467 bt = TypeManager.GetElementType (bt);
4469 x = BetterConversion (ec, a, ct, bt, loc);
4470 y = BetterConversion (ec, a, bt, ct, loc);
4480 // If a method (in the normal form) with the
4481 // same signature as the expanded form of the
4482 // current best params method already exists,
4483 // the expanded form is not applicable so we
4484 // force it to select the candidate
4486 if (!candidate_params && best_params && cand_count == argument_count)
4489 if (rating1 > rating2)
4495 public static string FullMethodDesc (MethodBase mb)
4497 string ret_type = "";
4502 if (mb is MethodInfo)
4503 ret_type = TypeManager.CSharpName (((MethodInfo) mb).ReturnType);
4505 StringBuilder sb = new StringBuilder (ret_type);
4507 sb.Append (mb.ReflectedType.ToString ());
4509 sb.Append (mb.Name);
4511 ParameterData pd = GetParameterData (mb);
4513 int count = pd.Count;
4516 for (int i = count; i > 0; ) {
4519 sb.Append (pd.ParameterDesc (count - i - 1));
4525 return sb.ToString ();
4528 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
4530 MemberInfo [] miset;
4531 MethodGroupExpr union;
4536 return (MethodGroupExpr) mg2;
4539 return (MethodGroupExpr) mg1;
4542 MethodGroupExpr left_set = null, right_set = null;
4543 int length1 = 0, length2 = 0;
4545 left_set = (MethodGroupExpr) mg1;
4546 length1 = left_set.Methods.Length;
4548 right_set = (MethodGroupExpr) mg2;
4549 length2 = right_set.Methods.Length;
4551 ArrayList common = new ArrayList ();
4553 foreach (MethodBase r in right_set.Methods){
4554 if (TypeManager.ArrayContainsMethod (left_set.Methods, r))
4558 miset = new MemberInfo [length1 + length2 - common.Count];
4559 left_set.Methods.CopyTo (miset, 0);
4563 foreach (MethodBase r in right_set.Methods) {
4564 if (!common.Contains (r))
4568 union = new MethodGroupExpr (miset, loc);
4573 static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
4574 ArrayList arguments, ref MethodBase candidate)
4576 if (!me.HasTypeArguments &&
4577 !InferParamsTypeArguments (ec, arguments, ref candidate))
4580 return IsParamsMethodApplicable (ec, arguments, candidate);
4584 /// Determines if the candidate method, if a params method, is applicable
4585 /// in its expanded form to the given set of arguments
4587 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
4591 if (arguments == null)
4594 arg_count = arguments.Count;
4596 ParameterData pd = GetParameterData (candidate);
4598 int pd_count = pd.Count;
4603 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
4606 if (pd_count - 1 > arg_count)
4609 if (pd_count == 1 && arg_count == 0)
4613 // If we have come this far, the case which
4614 // remains is when the number of parameters is
4615 // less than or equal to the argument count.
4617 for (int i = 0; i < pd_count - 1; ++i) {
4619 Argument a = (Argument) arguments [i];
4621 Parameter.Modifier a_mod = a.GetParameterModifier () &
4622 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4623 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4624 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4626 if (a_mod == p_mod) {
4628 if (a_mod == Parameter.Modifier.NONE)
4629 if (!Convert.ImplicitConversionExists (ec,
4631 pd.ParameterType (i)))
4634 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4635 Type pt = pd.ParameterType (i);
4638 pt = TypeManager.GetReferenceType (pt);
4648 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
4650 for (int i = pd_count - 1; i < arg_count; i++) {
4651 Argument a = (Argument) arguments [i];
4653 if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
4660 static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
4661 ArrayList arguments, ref MethodBase candidate)
4663 if (!me.HasTypeArguments &&
4664 !InferTypeArguments (ec, arguments, ref candidate))
4667 return IsApplicable (ec, arguments, candidate);
4671 /// Determines if the candidate method is applicable (section 14.4.2.1)
4672 /// to the given set of arguments
4674 static bool IsApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
4678 if (arguments == null)
4681 arg_count = arguments.Count;
4684 ParameterData pd = GetParameterData (candidate);
4686 if (arg_count != pd.Count)
4689 for (int i = arg_count; i > 0; ) {
4692 Argument a = (Argument) arguments [i];
4694 Parameter.Modifier a_mod = a.GetParameterModifier () &
4695 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4696 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4697 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4700 if (a_mod == p_mod ||
4701 (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
4702 if (a_mod == Parameter.Modifier.NONE) {
4703 if (!Convert.ImplicitConversionExists (ec,
4705 pd.ParameterType (i)))
4709 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4710 Type pt = pd.ParameterType (i);
4713 pt = TypeManager.GetReferenceType (pt);
4726 /// Find the Applicable Function Members (7.4.2.1)
4728 /// me: Method Group expression with the members to select.
4729 /// it might contain constructors or methods (or anything
4730 /// that maps to a method).
4732 /// Arguments: ArrayList containing resolved Argument objects.
4734 /// loc: The location if we want an error to be reported, or a Null
4735 /// location for "probing" purposes.
4737 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4738 /// that is the best match of me on Arguments.
4741 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4742 ArrayList Arguments, bool may_fail,
4745 MethodBase method = null;
4746 Type applicable_type = null;
4748 ArrayList candidates = new ArrayList ();
4751 // Used to keep a map between the candidate
4752 // and whether it is being considered in its
4753 // normal or expanded form
4755 // false is normal form, true is expanded form
4757 Hashtable candidate_to_form = new PtrHashtable ();
4761 // First we construct the set of applicable methods
4763 // We start at the top of the type hierarchy and
4764 // go down to find applicable methods
4766 applicable_type = me.DeclaringType;
4768 if (me.Name == "Invoke" && TypeManager.IsDelegateType (applicable_type)) {
4769 Error_InvokeOnDelegate (loc);
4773 bool found_applicable = false;
4775 MethodBase[] methods = me.Methods;
4777 for (int i = 0; i < methods.Length; i++) {
4778 Type decl_type = methods [i].DeclaringType;
4781 // If we have already found an applicable method
4782 // we eliminate all base types (Section 14.5.5.1)
4784 if (decl_type != applicable_type &&
4785 (applicable_type.IsSubclassOf (decl_type) ||
4786 TypeManager.ImplementsInterface (applicable_type, decl_type)) &&
4790 // Check if candidate is applicable (section 14.4.2.1)
4791 if (IsApplicable (ec, me, Arguments, ref methods [i])) {
4792 // Candidate is applicable in normal form
4793 MethodBase candidate = methods [i];
4794 candidates.Add (candidate);
4795 applicable_type = candidate.DeclaringType;
4796 found_applicable = true;
4797 candidate_to_form [candidate] = false;
4798 } else if (IsParamsMethodApplicable (ec, me, Arguments, ref methods [i])) {
4799 // Candidate is applicable in expanded form
4800 MethodBase candidate = methods [i];
4801 candidates.Add (candidate);
4802 applicable_type = candidate.DeclaringType;
4803 found_applicable = true;
4804 candidate_to_form [candidate] = true;
4808 if (Arguments == null)
4811 argument_count = Arguments.Count;
4814 // Now we actually find the best method
4816 int candidate_top = candidates.Count;
4817 for (int ix = 0; ix < candidate_top; ix++){
4818 MethodBase candidate = (MethodBase) candidates [ix];
4820 bool cand_params = (bool) candidate_to_form [candidate];
4821 bool method_params = false;
4824 method_params = (bool) candidate_to_form [method];
4826 int x = BetterFunction (ec, Arguments,
4827 candidate, cand_params,
4828 method, method_params,
4836 if (method == null) {
4837 int errors = Report.Errors;
4840 // Okay so we have failed to find anything so we
4841 // return by providing info about the closest match
4843 for (int i = 0; i < methods.Length; ++i) {
4845 MethodBase c = methods [i];
4849 ParameterData pd = GetParameterData (c);
4850 if (pd.Count != argument_count)
4853 if (!InferTypeArguments (ec, Arguments, ref c))
4856 VerifyArgumentsCompat (ec, Arguments, argument_count,
4857 c, false, null, loc);
4861 if (Report.Errors > errors)
4864 string report_name = me.Name;
4865 if (report_name == ".ctor")
4866 report_name = me.DeclaringType.ToString ();
4868 for (int i = 0; i < methods.Length; ++i) {
4870 MethodBase c = methods [i];
4874 ParameterData pd = GetParameterData (c);
4875 if (pd.Count != argument_count)
4878 if (InferTypeArguments (ec, Arguments, ref c))
4881 Report.Error (411, loc, "The type arguments for " +
4882 "method `{0}' cannot be infered from " +
4883 "the usage. Try specifying the type " +
4884 "arguments explicitly.", report_name);
4888 if (!may_fail && (errors == Report.Errors))
4889 Error_WrongNumArguments (loc, report_name,
4896 // Now check that there are no ambiguities i.e the selected method
4897 // should be better than all the others
4899 bool best_params = (bool) candidate_to_form [method];
4901 for (int ix = 0; ix < candidate_top; ix++){
4902 MethodBase candidate = (MethodBase) candidates [ix];
4904 if (candidate == method)
4908 // If a normal method is applicable in
4909 // the sense that it has the same
4910 // number of arguments, then the
4911 // expanded params method is never
4912 // applicable so we debar the params
4915 // if ((IsParamsMethodApplicable (ec, Arguments, candidate) &&
4916 // IsApplicable (ec, Arguments, method)))
4919 bool cand_params = (bool) candidate_to_form [candidate];
4920 int x = BetterFunction (ec, Arguments,
4921 method, best_params,
4922 candidate, cand_params,
4928 "Ambiguous call when selecting function due to implicit casts");
4934 // And now check if the arguments are all
4935 // compatible, perform conversions if
4936 // necessary etc. and return if everything is
4939 if (!VerifyArgumentsCompat (ec, Arguments, argument_count, method,
4940 best_params, null, loc))
4946 static void Error_WrongNumArguments (Location loc, String name, int arg_count)
4948 Report.Error (1501, loc,
4949 "No overload for method `" + name + "' takes `" +
4950 arg_count + "' arguments");
4953 static void Error_InvokeOnDelegate (Location loc)
4955 Report.Error (1533, loc,
4956 "Invoke cannot be called directly on a delegate");
4959 static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
4960 Type delegate_type, string arg_sig, string par_desc)
4962 if (delegate_type == null)
4963 Report.Error (1502, loc,
4964 "The best overloaded match for method '" +
4965 FullMethodDesc (method) +
4966 "' has some invalid arguments");
4968 Report.Error (1594, loc,
4969 "Delegate '" + delegate_type.ToString () +
4970 "' has some invalid arguments.");
4971 Report.Error (1503, loc,
4972 String.Format ("Argument {0}: Cannot convert from '{1}' to '{2}'",
4973 idx, arg_sig, par_desc));
4976 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
4979 bool chose_params_expanded,
4983 ParameterData pd = GetParameterData (method);
4984 int pd_count = pd.Count;
4986 for (int j = 0; j < argument_count; j++) {
4987 Argument a = (Argument) Arguments [j];
4988 Expression a_expr = a.Expr;
4989 Type parameter_type = pd.ParameterType (j);
4990 Parameter.Modifier pm = pd.ParameterModifier (j);
4992 if (pm == Parameter.Modifier.PARAMS){
4993 if ((pm & ~Parameter.Modifier.PARAMS) != a.GetParameterModifier ()) {
4994 if (!Location.IsNull (loc))
4995 Error_InvalidArguments (
4996 loc, j, method, delegate_type,
4997 Argument.FullDesc (a), pd.ParameterDesc (j));
5001 if (chose_params_expanded)
5002 parameter_type = TypeManager.GetElementType (parameter_type);
5007 if (pd.ParameterModifier (j) != a.GetParameterModifier ()){
5008 if (!Location.IsNull (loc))
5009 Error_InvalidArguments (
5010 loc, j, method, delegate_type,
5011 Argument.FullDesc (a), pd.ParameterDesc (j));
5019 if (a.Type != parameter_type){
5022 conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
5025 if (!Location.IsNull (loc))
5026 Error_InvalidArguments (
5027 loc, j, method, delegate_type,
5028 Argument.FullDesc (a), pd.ParameterDesc (j));
5033 // Update the argument with the implicit conversion
5039 Parameter.Modifier a_mod = a.GetParameterModifier () &
5040 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
5041 Parameter.Modifier p_mod = pd.ParameterModifier (j) &
5042 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
5044 if (a_mod != p_mod &&
5045 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
5046 if (!Location.IsNull (loc)) {
5047 Report.Error (1502, loc,
5048 "The best overloaded match for method '" + FullMethodDesc (method)+
5049 "' has some invalid arguments");
5050 Report.Error (1503, loc,
5051 "Argument " + (j+1) +
5052 ": Cannot convert from '" + Argument.FullDesc (a)
5053 + "' to '" + pd.ParameterDesc (j) + "'");
5063 static bool InferType (Type pt, Type at, ref Type[] infered)
5065 if (pt.IsGenericParameter) {
5066 int pos = pt.GenericParameterPosition;
5068 if (infered [pos] == null) {
5070 while (check.IsArray)
5071 check = check.GetElementType ();
5073 if (pt.Equals (check))
5080 if (infered [pos] != at)
5086 if (!pt.ContainsGenericParameters)
5091 (at.GetArrayRank () != pt.GetArrayRank ()))
5094 return InferType (pt.GetElementType (), at.GetElementType (),
5100 (pt.GetArrayRank () != at.GetArrayRank ()))
5103 return InferType (pt.GetElementType (), at.GetElementType (),
5107 if (!at.IsGenericInstance)
5110 Type[] at_args = at.GetGenericArguments ();
5111 Type[] pt_args = pt.GetGenericArguments ();
5113 if (at_args.Length != pt_args.Length)
5116 Type[] infered_types = new Type [at_args.Length];
5118 for (int i = 0; i < at_args.Length; i++)
5119 if (!InferType (pt_args [i], at_args [i], ref infered_types))
5122 for (int i = 0; i < infered_types.Length; i++)
5123 if (infered_types [i] == null)
5126 for (int i = 0; i < infered_types.Length; i++) {
5127 if (infered [i] == null) {
5128 infered [i] = infered_types [i];
5132 if (infered [i] != infered_types [i])
5139 static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
5140 ref MethodBase method)
5142 if ((arguments == null) || !TypeManager.IsGenericMethod (method))
5147 if (arguments == null)
5150 arg_count = arguments.Count;
5152 ParameterData pd = GetParameterData (method);
5154 int pd_count = pd.Count;
5159 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
5162 if (pd_count - 1 > arg_count)
5165 if (pd_count == 1 && arg_count == 0)
5168 Type[] method_args = method.GetGenericArguments ();
5169 Type[] infered_types = new Type [method_args.Length];
5172 // If we have come this far, the case which
5173 // remains is when the number of parameters is
5174 // less than or equal to the argument count.
5176 for (int i = 0; i < pd_count - 1; ++i) {
5177 Argument a = (Argument) arguments [i];
5179 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
5182 Type pt = pd.ParameterType (i);
5185 if (!InferType (pt, at, ref infered_types))
5189 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
5191 for (int i = pd_count - 1; i < arg_count; i++) {
5192 Argument a = (Argument) arguments [i];
5194 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
5197 if (!InferType (element_type, a.Type, ref infered_types))
5201 for (int i = 0; i < infered_types.Length; i++)
5202 if (infered_types [i] == null)
5205 method = method.BindGenericParameters (infered_types);
5209 public static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
5210 ref Type[] infered_types)
5212 for (int i = 0; i < arg_types.Length; i++) {
5213 if (arg_types [i] == null)
5216 if (!InferType (param_types [i], arg_types [i],
5221 for (int i = 0; i < infered_types.Length; i++)
5222 if (infered_types [i] == null)
5228 static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
5229 ref MethodBase method)
5231 if (!TypeManager.IsGenericMethod (method))
5235 if (arguments != null)
5236 arg_count = arguments.Count;
5240 ParameterData pd = GetParameterData (method);
5241 if (arg_count != pd.Count)
5244 Type[] method_args = method.GetGenericArguments ();
5245 Type[] infered_types = new Type [method_args.Length];
5247 Type[] param_types = new Type [pd.Count];
5248 Type[] arg_types = new Type [pd.Count];
5250 for (int i = 0; i < arg_count; i++) {
5251 param_types [i] = pd.ParameterType (i);
5253 Argument a = (Argument) arguments [i];
5254 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
5257 arg_types [i] = a.Type;
5260 if (!InferTypeArguments (param_types, arg_types, ref infered_types))
5263 method = method.BindGenericParameters (infered_types);
5267 public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
5268 ref MethodBase method)
5270 if (!TypeManager.IsGenericMethod (method))
5273 ParameterData pd = GetParameterData (method);
5274 if (apd.Count != pd.Count)
5277 Type[] method_args = method.GetGenericArguments ();
5278 Type[] infered_types = new Type [method_args.Length];
5280 Type[] param_types = new Type [pd.Count];
5281 Type[] arg_types = new Type [pd.Count];
5283 for (int i = 0; i < apd.Count; i++) {
5284 param_types [i] = pd.ParameterType (i);
5285 arg_types [i] = apd.ParameterType (i);
5288 if (!InferTypeArguments (param_types, arg_types, ref infered_types))
5291 method = method.BindGenericParameters (infered_types);
5295 public override Expression DoResolve (EmitContext ec)
5298 // First, resolve the expression that is used to
5299 // trigger the invocation
5301 if (expr is BaseAccess)
5304 if (expr is ConstructedType)
5305 expr = ((ConstructedType) expr).GetMemberAccess (ec);
5307 expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
5311 if (!(expr is MethodGroupExpr)) {
5312 Type expr_type = expr.Type;
5314 if (expr_type != null){
5315 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
5317 return (new DelegateInvocation (
5318 this.expr, Arguments, loc)).Resolve (ec);
5322 if (!(expr is MethodGroupExpr)){
5323 expr.Error_UnexpectedKind (ResolveFlags.MethodGroup);
5328 // Next, evaluate all the expressions in the argument list
5330 if (Arguments != null){
5331 foreach (Argument a in Arguments){
5332 if (!a.Resolve (ec, loc))
5337 MethodGroupExpr mg = (MethodGroupExpr) expr;
5338 method = OverloadResolve (ec, mg, Arguments, false, loc);
5343 MethodInfo mi = method as MethodInfo;
5345 type = TypeManager.TypeToCoreType (mi.ReturnType);
5346 if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null))
5347 SimpleName.Error_ObjectRefRequired (ec, loc, mi.Name);
5350 if (type.IsPointer){
5358 // Only base will allow this invocation to happen.
5360 if (is_base && method.IsAbstract){
5361 Report.Error (205, loc, "Cannot call an abstract base member: " +
5362 FullMethodDesc (method));
5366 if ((method.Attributes & MethodAttributes.SpecialName) != 0){
5367 if (TypeManager.IsSpecialMethod (method))
5368 Report.Error (571, loc, method.Name + ": can not call operator or accessor");
5371 eclass = ExprClass.Value;
5376 // Emits the list of arguments as an array
5378 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
5380 ILGenerator ig = ec.ig;
5381 int count = arguments.Count - idx;
5382 Argument a = (Argument) arguments [idx];
5383 Type t = a.Expr.Type;
5385 IntConstant.EmitInt (ig, count);
5386 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
5388 int top = arguments.Count;
5389 for (int j = idx; j < top; j++){
5390 a = (Argument) arguments [j];
5392 ig.Emit (OpCodes.Dup);
5393 IntConstant.EmitInt (ig, j - idx);
5395 bool is_stobj, has_type_arg;
5396 OpCode op = ArrayAccess.GetStoreOpcode (t, out is_stobj, out has_type_arg);
5398 ig.Emit (OpCodes.Ldelema, t);
5410 /// Emits a list of resolved Arguments that are in the arguments
5413 /// The MethodBase argument might be null if the
5414 /// emission of the arguments is known not to contain
5415 /// a `params' field (for example in constructors or other routines
5416 /// that keep their arguments in this structure)
5418 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
5422 pd = GetParameterData (mb);
5427 // If we are calling a params method with no arguments, special case it
5429 if (arguments == null){
5430 if (pd != null && pd.Count > 0 &&
5431 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
5432 ILGenerator ig = ec.ig;
5434 IntConstant.EmitInt (ig, 0);
5435 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
5441 int top = arguments.Count;
5443 for (int i = 0; i < top; i++){
5444 Argument a = (Argument) arguments [i];
5447 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
5449 // Special case if we are passing the same data as the
5450 // params argument, do not put it in an array.
5452 if (pd.ParameterType (i) == a.Type)
5455 EmitParams (ec, i, arguments);
5463 if (pd != null && pd.Count > top &&
5464 pd.ParameterModifier (top) == Parameter.Modifier.PARAMS){
5465 ILGenerator ig = ec.ig;
5467 IntConstant.EmitInt (ig, 0);
5468 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (top)));
5473 /// is_base tells whether we want to force the use of the `call'
5474 /// opcode instead of using callvirt. Call is required to call
5475 /// a specific method, while callvirt will always use the most
5476 /// recent method in the vtable.
5478 /// is_static tells whether this is an invocation on a static method
5480 /// instance_expr is an expression that represents the instance
5481 /// it must be non-null if is_static is false.
5483 /// method is the method to invoke.
5485 /// Arguments is the list of arguments to pass to the method or constructor.
5487 public static void EmitCall (EmitContext ec, bool is_base,
5488 bool is_static, Expression instance_expr,
5489 MethodBase method, ArrayList Arguments, Location loc)
5491 ILGenerator ig = ec.ig;
5492 bool struct_call = false;
5493 bool this_call = false;
5495 Type decl_type = method.DeclaringType;
5497 if (!RootContext.StdLib) {
5498 // Replace any calls to the system's System.Array type with calls to
5499 // the newly created one.
5500 if (method == TypeManager.system_int_array_get_length)
5501 method = TypeManager.int_array_get_length;
5502 else if (method == TypeManager.system_int_array_get_rank)
5503 method = TypeManager.int_array_get_rank;
5504 else if (method == TypeManager.system_object_array_clone)
5505 method = TypeManager.object_array_clone;
5506 else if (method == TypeManager.system_int_array_get_length_int)
5507 method = TypeManager.int_array_get_length_int;
5508 else if (method == TypeManager.system_int_array_get_lower_bound_int)
5509 method = TypeManager.int_array_get_lower_bound_int;
5510 else if (method == TypeManager.system_int_array_get_upper_bound_int)
5511 method = TypeManager.int_array_get_upper_bound_int;
5512 else if (method == TypeManager.system_void_array_copyto_array_int)
5513 method = TypeManager.void_array_copyto_array_int;
5517 // This checks the `ConditionalAttribute' on the method, and the
5518 // ObsoleteAttribute
5520 TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (method, loc);
5521 if ((flags & TypeManager.MethodFlags.IsObsoleteError) != 0)
5523 if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
5527 if (TypeManager.IsValueType (decl_type))
5530 // If this is ourselves, push "this"
5532 if (instance_expr == null){
5534 ig.Emit (OpCodes.Ldarg_0);
5536 Type itype = instance_expr.Type;
5539 // Push the instance expression
5541 if (TypeManager.IsValueType (itype)){
5543 // Special case: calls to a function declared in a
5544 // reference-type with a value-type argument need
5545 // to have their value boxed.
5546 if (decl_type.IsValueType || itype.IsGenericParameter){
5548 // If the expression implements IMemoryLocation, then
5549 // we can optimize and use AddressOf on the
5552 // If not we have to use some temporary storage for
5554 if (instance_expr is IMemoryLocation){
5555 ((IMemoryLocation)instance_expr).
5556 AddressOf (ec, AddressOp.LoadStore);
5559 instance_expr.Emit (ec);
5560 LocalBuilder temp = ig.DeclareLocal (itype);
5561 ig.Emit (OpCodes.Stloc, temp);
5562 ig.Emit (OpCodes.Ldloca, temp);
5564 if (itype.IsGenericParameter)
5565 ig.Emit (OpCodes.Constrained, itype);
5569 instance_expr.Emit (ec);
5570 ig.Emit (OpCodes.Box, itype);
5573 instance_expr.Emit (ec);
5577 EmitArguments (ec, method, Arguments);
5581 // and DoFoo is not virtual, you can omit the callvirt,
5582 // because you don't need the null checking behavior.
5584 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual)){
5585 if (method is MethodInfo) {
5586 ig.Emit (OpCodes.Call, (MethodInfo) method);
5588 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5590 if (method is MethodInfo)
5591 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
5593 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
5597 public override void Emit (EmitContext ec)
5599 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
5601 EmitCall (ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
5604 public override void EmitStatement (EmitContext ec)
5609 // Pop the return value if there is one
5611 if (method is MethodInfo){
5612 Type ret = ((MethodInfo)method).ReturnType;
5613 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
5614 ec.ig.Emit (OpCodes.Pop);
5619 public class InvocationOrCast : ExpressionStatement
5622 Expression argument;
5624 public InvocationOrCast (Expression expr, Expression argument, Location loc)
5627 this.argument = argument;
5631 public override Expression DoResolve (EmitContext ec)
5634 // First try to resolve it as a cast.
5636 type = ec.DeclSpace.ResolveType (expr, true, loc);
5638 Cast cast = new Cast (new TypeExpression (type, loc), argument, loc);
5639 return cast.Resolve (ec);
5643 // This can either be a type or a delegate invocation.
5644 // Let's just resolve it and see what we'll get.
5646 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5651 // Ok, so it's a Cast.
5653 if (expr.eclass == ExprClass.Type) {
5654 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
5655 return cast.Resolve (ec);
5659 // It's a delegate invocation.
5661 if (!TypeManager.IsDelegateType (expr.Type)) {
5662 Error (149, "Method name expected");
5666 ArrayList args = new ArrayList ();
5667 args.Add (new Argument (argument, Argument.AType.Expression));
5668 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5669 return invocation.Resolve (ec);
5674 Error (201, "Only assignment, call, increment, decrement and new object " +
5675 "expressions can be used as a statement");
5678 public override ExpressionStatement ResolveStatement (EmitContext ec)
5681 // First try to resolve it as a cast.
5683 type = ec.DeclSpace.ResolveType (expr, true, loc);
5690 // This can either be a type or a delegate invocation.
5691 // Let's just resolve it and see what we'll get.
5693 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5694 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
5700 // It's a delegate invocation.
5702 if (!TypeManager.IsDelegateType (expr.Type)) {
5703 Error (149, "Method name expected");
5707 ArrayList args = new ArrayList ();
5708 args.Add (new Argument (argument, Argument.AType.Expression));
5709 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5710 return invocation.ResolveStatement (ec);
5713 public override void Emit (EmitContext ec)
5715 throw new Exception ("Cannot happen");
5718 public override void EmitStatement (EmitContext ec)
5720 throw new Exception ("Cannot happen");
5725 // This class is used to "disable" the code generation for the
5726 // temporary variable when initializing value types.
5728 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
5729 public void AddressOf (EmitContext ec, AddressOp Mode)
5736 /// Implements the new expression
5738 public class New : ExpressionStatement, IMemoryLocation {
5739 public readonly ArrayList Arguments;
5742 // During bootstrap, it contains the RequestedType,
5743 // but if `type' is not null, it *might* contain a NewDelegate
5744 // (because of field multi-initialization)
5746 public Expression RequestedType;
5748 MethodBase method = null;
5751 // If set, the new expression is for a value_target, and
5752 // we will not leave anything on the stack.
5754 Expression value_target;
5755 bool value_target_set = false;
5756 bool is_type_parameter = false;
5758 public New (Expression requested_type, ArrayList arguments, Location l)
5760 RequestedType = requested_type;
5761 Arguments = arguments;
5765 public bool SetValueTypeVariable (Expression value)
5767 value_target = value;
5768 value_target_set = true;
5769 if (!(value_target is IMemoryLocation)){
5770 Error_UnexpectedKind ("variable");
5777 // This function is used to disable the following code sequence for
5778 // value type initialization:
5780 // AddressOf (temporary)
5784 // Instead the provide will have provided us with the address on the
5785 // stack to store the results.
5787 static Expression MyEmptyExpression;
5789 public void DisableTemporaryValueType ()
5791 if (MyEmptyExpression == null)
5792 MyEmptyExpression = new EmptyAddressOf ();
5795 // To enable this, look into:
5796 // test-34 and test-89 and self bootstrapping.
5798 // For instance, we can avoid a copy by using `newobj'
5799 // instead of Call + Push-temp on value types.
5800 // value_target = MyEmptyExpression;
5803 public override Expression DoResolve (EmitContext ec)
5806 // The New DoResolve might be called twice when initializing field
5807 // expressions (see EmitFieldInitializers, the call to
5808 // GetInitializerExpression will perform a resolve on the expression,
5809 // and later the assign will trigger another resolution
5811 // This leads to bugs (#37014)
5814 if (RequestedType is NewDelegate)
5815 return RequestedType;
5819 type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
5824 bool IsDelegate = TypeManager.IsDelegateType (type);
5827 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5828 if (RequestedType != null)
5829 if (!(RequestedType is NewDelegate))
5830 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
5831 return RequestedType;
5834 if (type.IsGenericParameter) {
5835 if (!TypeManager.HasConstructorConstraint (type)) {
5836 Error (304, String.Format (
5837 "Cannot create an instance of the " +
5838 "variable type '{0}' because it " +
5839 "doesn't have the new() constraint",
5844 if ((Arguments != null) && (Arguments.Count != 0)) {
5845 Error (417, String.Format (
5846 "`{0}': cannot provide arguments " +
5847 "when creating an instance of a " +
5848 "variable type.", type));
5852 is_type_parameter = true;
5853 eclass = ExprClass.Value;
5855 } else if (type.IsInterface || type.IsAbstract){
5856 Error (144, "It is not possible to create instances of interfaces or abstract classes");
5860 bool is_struct = type.IsValueType;
5861 eclass = ExprClass.Value;
5864 // SRE returns a match for .ctor () on structs (the object constructor),
5865 // so we have to manually ignore it.
5867 if (is_struct && Arguments == null)
5871 ml = MemberLookupFinal (ec, null, type, ".ctor",
5872 MemberTypes.Constructor,
5873 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5878 if (! (ml is MethodGroupExpr)){
5880 ml.Error_UnexpectedKind ("method group");
5886 if (Arguments != null){
5887 foreach (Argument a in Arguments){
5888 if (!a.Resolve (ec, loc))
5893 method = Invocation.OverloadResolve (
5894 ec, (MethodGroupExpr) ml, Arguments, false, loc);
5898 if (method == null) {
5899 if (!is_struct || Arguments.Count > 0) {
5900 Error (1501, String.Format (
5901 "New invocation: Can not find a constructor in `{0}' for this argument list",
5902 TypeManager.CSharpName (type)));
5910 bool DoEmitTypeParameter (EmitContext ec)
5912 ILGenerator ig = ec.ig;
5914 ig.Emit (OpCodes.Ldtoken, type);
5915 ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
5916 ig.Emit (OpCodes.Call, TypeManager.activator_create_instance);
5917 ig.Emit (OpCodes.Unbox_Any, type);
5923 // This DoEmit can be invoked in two contexts:
5924 // * As a mechanism that will leave a value on the stack (new object)
5925 // * As one that wont (init struct)
5927 // You can control whether a value is required on the stack by passing
5928 // need_value_on_stack. The code *might* leave a value on the stack
5929 // so it must be popped manually
5931 // If we are dealing with a ValueType, we have a few
5932 // situations to deal with:
5934 // * The target is a ValueType, and we have been provided
5935 // the instance (this is easy, we are being assigned).
5937 // * The target of New is being passed as an argument,
5938 // to a boxing operation or a function that takes a
5941 // In this case, we need to create a temporary variable
5942 // that is the argument of New.
5944 // Returns whether a value is left on the stack
5946 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5948 bool is_value_type = type.IsValueType;
5949 ILGenerator ig = ec.ig;
5954 // Allow DoEmit() to be called multiple times.
5955 // We need to create a new LocalTemporary each time since
5956 // you can't share LocalBuilders among ILGeneators.
5957 if (!value_target_set)
5958 value_target = new LocalTemporary (ec, type);
5960 ml = (IMemoryLocation) value_target;
5961 ml.AddressOf (ec, AddressOp.Store);
5965 Invocation.EmitArguments (ec, method, Arguments);
5969 ig.Emit (OpCodes.Initobj, type);
5971 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5972 if (need_value_on_stack){
5973 value_target.Emit (ec);
5978 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5983 public override void Emit (EmitContext ec)
5985 if (is_type_parameter)
5986 DoEmitTypeParameter (ec);
5991 public override void EmitStatement (EmitContext ec)
5993 if (is_type_parameter)
5994 throw new InvalidOperationException ();
5996 if (DoEmit (ec, false))
5997 ec.ig.Emit (OpCodes.Pop);
6000 public void AddressOf (EmitContext ec, AddressOp Mode)
6002 if (is_type_parameter)
6003 throw new InvalidOperationException ();
6005 if (!type.IsValueType){
6007 // We throw an exception. So far, I believe we only need to support
6009 // foreach (int j in new StructType ())
6012 throw new Exception ("AddressOf should not be used for classes");
6015 if (!value_target_set)
6016 value_target = new LocalTemporary (ec, type);
6018 IMemoryLocation ml = (IMemoryLocation) value_target;
6019 ml.AddressOf (ec, AddressOp.Store);
6021 Invocation.EmitArguments (ec, method, Arguments);
6024 ec.ig.Emit (OpCodes.Initobj, type);
6026 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
6028 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
6033 /// 14.5.10.2: Represents an array creation expression.
6037 /// There are two possible scenarios here: one is an array creation
6038 /// expression that specifies the dimensions and optionally the
6039 /// initialization data and the other which does not need dimensions
6040 /// specified but where initialization data is mandatory.
6042 public class ArrayCreation : ExpressionStatement {
6043 Expression requested_base_type;
6044 ArrayList initializers;
6047 // The list of Argument types.
6048 // This is used to construct the `newarray' or constructor signature
6050 ArrayList arguments;
6053 // Method used to create the array object.
6055 MethodBase new_method = null;
6057 Type array_element_type;
6058 Type underlying_type;
6059 bool is_one_dimensional = false;
6060 bool is_builtin_type = false;
6061 bool expect_initializers = false;
6062 int num_arguments = 0;
6066 ArrayList array_data;
6071 // The number of array initializers that we can handle
6072 // via the InitializeArray method - through EmitStaticInitializers
6074 int num_automatic_initializers;
6076 const int max_automatic_initializers = 6;
6078 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
6080 this.requested_base_type = requested_base_type;
6081 this.initializers = initializers;
6085 arguments = new ArrayList ();
6087 foreach (Expression e in exprs) {
6088 arguments.Add (new Argument (e, Argument.AType.Expression));
6093 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
6095 this.requested_base_type = requested_base_type;
6096 this.initializers = initializers;
6100 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
6102 //string tmp = rank.Substring (rank.LastIndexOf ('['));
6104 //dimensions = tmp.Length - 1;
6105 expect_initializers = true;
6108 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
6110 StringBuilder sb = new StringBuilder (rank);
6113 for (int i = 1; i < idx_count; i++)
6118 return new ComposedCast (base_type, sb.ToString (), loc);
6121 void Error_IncorrectArrayInitializer ()
6123 Error (178, "Incorrectly structured array initializer");
6126 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
6128 if (specified_dims) {
6129 Argument a = (Argument) arguments [idx];
6131 if (!a.Resolve (ec, loc))
6134 if (!(a.Expr is Constant)) {
6135 Error (150, "A constant value is expected");
6139 int value = (int) ((Constant) a.Expr).GetValue ();
6141 if (value != probe.Count) {
6142 Error_IncorrectArrayInitializer ();
6146 bounds [idx] = value;
6149 int child_bounds = -1;
6150 foreach (object o in probe) {
6151 if (o is ArrayList) {
6152 int current_bounds = ((ArrayList) o).Count;
6154 if (child_bounds == -1)
6155 child_bounds = current_bounds;
6157 else if (child_bounds != current_bounds){
6158 Error_IncorrectArrayInitializer ();
6161 if (specified_dims && (idx + 1 >= arguments.Count)){
6162 Error (623, "Array initializers can only be used in a variable or field initializer, try using the new expression");
6166 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
6170 if (child_bounds != -1){
6171 Error_IncorrectArrayInitializer ();
6175 Expression tmp = (Expression) o;
6176 tmp = tmp.Resolve (ec);
6180 // Console.WriteLine ("I got: " + tmp);
6181 // Handle initialization from vars, fields etc.
6183 Expression conv = Convert.ImplicitConversionRequired (
6184 ec, tmp, underlying_type, loc);
6189 if (conv is StringConstant || conv is DecimalConstant || conv is NullCast) {
6190 // These are subclasses of Constant that can appear as elements of an
6191 // array that cannot be statically initialized (with num_automatic_initializers
6192 // > max_automatic_initializers), so num_automatic_initializers should be left as zero.
6193 array_data.Add (conv);
6194 } else if (conv is Constant) {
6195 // These are the types of Constant that can appear in arrays that can be
6196 // statically allocated.
6197 array_data.Add (conv);
6198 num_automatic_initializers++;
6200 array_data.Add (conv);
6207 public void UpdateIndices (EmitContext ec)
6210 for (ArrayList probe = initializers; probe != null;) {
6211 if (probe.Count > 0 && probe [0] is ArrayList) {
6212 Expression e = new IntConstant (probe.Count);
6213 arguments.Add (new Argument (e, Argument.AType.Expression));
6215 bounds [i++] = probe.Count;
6217 probe = (ArrayList) probe [0];
6220 Expression e = new IntConstant (probe.Count);
6221 arguments.Add (new Argument (e, Argument.AType.Expression));
6223 bounds [i++] = probe.Count;
6230 public bool ValidateInitializers (EmitContext ec, Type array_type)
6232 if (initializers == null) {
6233 if (expect_initializers)
6239 if (underlying_type == null)
6243 // We use this to store all the date values in the order in which we
6244 // will need to store them in the byte blob later
6246 array_data = new ArrayList ();
6247 bounds = new Hashtable ();
6251 if (arguments != null) {
6252 ret = CheckIndices (ec, initializers, 0, true);
6255 arguments = new ArrayList ();
6257 ret = CheckIndices (ec, initializers, 0, false);
6264 if (arguments.Count != dimensions) {
6265 Error_IncorrectArrayInitializer ();
6273 void Error_NegativeArrayIndex ()
6275 Error (284, "Can not create array with a negative size");
6279 // Converts `source' to an int, uint, long or ulong.
6281 Expression ExpressionToArrayArgument (EmitContext ec, Expression source)
6285 bool old_checked = ec.CheckState;
6286 ec.CheckState = true;
6288 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
6289 if (target == null){
6290 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
6291 if (target == null){
6292 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
6293 if (target == null){
6294 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
6296 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
6300 ec.CheckState = old_checked;
6303 // Only positive constants are allowed at compile time
6305 if (target is Constant){
6306 if (target is IntConstant){
6307 if (((IntConstant) target).Value < 0){
6308 Error_NegativeArrayIndex ();
6313 if (target is LongConstant){
6314 if (((LongConstant) target).Value < 0){
6315 Error_NegativeArrayIndex ();
6326 // Creates the type of the array
6328 bool LookupType (EmitContext ec)
6330 StringBuilder array_qualifier = new StringBuilder (rank);
6333 // `In the first form allocates an array instace of the type that results
6334 // from deleting each of the individual expression from the expression list'
6336 if (num_arguments > 0) {
6337 array_qualifier.Append ("[");
6338 for (int i = num_arguments-1; i > 0; i--)
6339 array_qualifier.Append (",");
6340 array_qualifier.Append ("]");
6346 Expression array_type_expr;
6347 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
6348 type = ec.DeclSpace.ResolveType (array_type_expr, false, loc);
6353 underlying_type = type;
6354 if (underlying_type.IsArray)
6355 underlying_type = TypeManager.GetElementType (underlying_type);
6356 dimensions = type.GetArrayRank ();
6361 public override Expression DoResolve (EmitContext ec)
6365 if (!LookupType (ec))
6369 // First step is to validate the initializers and fill
6370 // in any missing bits
6372 if (!ValidateInitializers (ec, type))
6375 if (arguments == null)
6378 arg_count = arguments.Count;
6379 foreach (Argument a in arguments){
6380 if (!a.Resolve (ec, loc))
6383 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
6384 if (real_arg == null)
6391 array_element_type = TypeManager.GetElementType (type);
6393 if (arg_count == 1) {
6394 is_one_dimensional = true;
6395 eclass = ExprClass.Value;
6399 is_builtin_type = TypeManager.IsBuiltinType (type);
6401 if (is_builtin_type) {
6404 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
6405 AllBindingFlags, loc);
6407 if (!(ml is MethodGroupExpr)) {
6408 ml.Error_UnexpectedKind ("method group");
6413 Error (-6, "New invocation: Can not find a constructor for " +
6414 "this argument list");
6418 new_method = Invocation.OverloadResolve (
6419 ec, (MethodGroupExpr) ml, arguments, false, loc);
6421 if (new_method == null) {
6422 Error (-6, "New invocation: Can not find a constructor for " +
6423 "this argument list");
6427 eclass = ExprClass.Value;
6430 ModuleBuilder mb = CodeGen.Module.Builder;
6431 ArrayList args = new ArrayList ();
6433 if (arguments != null) {
6434 for (int i = 0; i < arg_count; i++)
6435 args.Add (TypeManager.int32_type);
6438 Type [] arg_types = null;
6441 arg_types = new Type [args.Count];
6443 args.CopyTo (arg_types, 0);
6445 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
6448 if (new_method == null) {
6449 Error (-6, "New invocation: Can not find a constructor for " +
6450 "this argument list");
6454 eclass = ExprClass.Value;
6459 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
6464 int count = array_data.Count;
6466 if (underlying_type.IsEnum)
6467 underlying_type = TypeManager.EnumToUnderlying (underlying_type);
6469 factor = GetTypeSize (underlying_type);
6471 throw new Exception ("unrecognized type in MakeByteBlob: " + underlying_type);
6473 data = new byte [(count * factor + 4) & ~3];
6476 for (int i = 0; i < count; ++i) {
6477 object v = array_data [i];
6479 if (v is EnumConstant)
6480 v = ((EnumConstant) v).Child;
6482 if (v is Constant && !(v is StringConstant))
6483 v = ((Constant) v).GetValue ();
6489 if (underlying_type == TypeManager.int64_type){
6490 if (!(v is Expression)){
6491 long val = (long) v;
6493 for (int j = 0; j < factor; ++j) {
6494 data [idx + j] = (byte) (val & 0xFF);
6498 } else if (underlying_type == TypeManager.uint64_type){
6499 if (!(v is Expression)){
6500 ulong val = (ulong) v;
6502 for (int j = 0; j < factor; ++j) {
6503 data [idx + j] = (byte) (val & 0xFF);
6507 } else if (underlying_type == TypeManager.float_type) {
6508 if (!(v is Expression)){
6509 element = BitConverter.GetBytes ((float) v);
6511 for (int j = 0; j < factor; ++j)
6512 data [idx + j] = element [j];
6514 } else if (underlying_type == TypeManager.double_type) {
6515 if (!(v is Expression)){
6516 element = BitConverter.GetBytes ((double) v);
6518 for (int j = 0; j < factor; ++j)
6519 data [idx + j] = element [j];
6521 } else if (underlying_type == TypeManager.char_type){
6522 if (!(v is Expression)){
6523 int val = (int) ((char) v);
6525 data [idx] = (byte) (val & 0xff);
6526 data [idx+1] = (byte) (val >> 8);
6528 } else if (underlying_type == TypeManager.short_type){
6529 if (!(v is Expression)){
6530 int val = (int) ((short) v);
6532 data [idx] = (byte) (val & 0xff);
6533 data [idx+1] = (byte) (val >> 8);
6535 } else if (underlying_type == TypeManager.ushort_type){
6536 if (!(v is Expression)){
6537 int val = (int) ((ushort) v);
6539 data [idx] = (byte) (val & 0xff);
6540 data [idx+1] = (byte) (val >> 8);
6542 } else if (underlying_type == TypeManager.int32_type) {
6543 if (!(v is Expression)){
6546 data [idx] = (byte) (val & 0xff);
6547 data [idx+1] = (byte) ((val >> 8) & 0xff);
6548 data [idx+2] = (byte) ((val >> 16) & 0xff);
6549 data [idx+3] = (byte) (val >> 24);
6551 } else if (underlying_type == TypeManager.uint32_type) {
6552 if (!(v is Expression)){
6553 uint val = (uint) v;
6555 data [idx] = (byte) (val & 0xff);
6556 data [idx+1] = (byte) ((val >> 8) & 0xff);
6557 data [idx+2] = (byte) ((val >> 16) & 0xff);
6558 data [idx+3] = (byte) (val >> 24);
6560 } else if (underlying_type == TypeManager.sbyte_type) {
6561 if (!(v is Expression)){
6562 sbyte val = (sbyte) v;
6563 data [idx] = (byte) val;
6565 } else if (underlying_type == TypeManager.byte_type) {
6566 if (!(v is Expression)){
6567 byte val = (byte) v;
6568 data [idx] = (byte) val;
6570 } else if (underlying_type == TypeManager.bool_type) {
6571 if (!(v is Expression)){
6572 bool val = (bool) v;
6573 data [idx] = (byte) (val ? 1 : 0);
6575 } else if (underlying_type == TypeManager.decimal_type){
6576 if (!(v is Expression)){
6577 int [] bits = Decimal.GetBits ((decimal) v);
6580 // FIXME: For some reason, this doesn't work on the MS runtime.
6581 int [] nbits = new int [4];
6582 nbits [0] = bits [3];
6583 nbits [1] = bits [2];
6584 nbits [2] = bits [0];
6585 nbits [3] = bits [1];
6587 for (int j = 0; j < 4; j++){
6588 data [p++] = (byte) (nbits [j] & 0xff);
6589 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6590 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6591 data [p++] = (byte) (nbits [j] >> 24);
6595 throw new Exception ("Unrecognized type in MakeByteBlob: " + underlying_type);
6604 // Emits the initializers for the array
6606 void EmitStaticInitializers (EmitContext ec, bool is_expression)
6609 // First, the static data
6612 ILGenerator ig = ec.ig;
6614 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
6616 fb = RootContext.MakeStaticData (data);
6619 ig.Emit (OpCodes.Dup);
6620 ig.Emit (OpCodes.Ldtoken, fb);
6621 ig.Emit (OpCodes.Call,
6622 TypeManager.void_initializearray_array_fieldhandle);
6626 // Emits pieces of the array that can not be computed at compile
6627 // time (variables and string locations).
6629 // This always expect the top value on the stack to be the array
6631 void EmitDynamicInitializers (EmitContext ec, bool is_expression)
6633 ILGenerator ig = ec.ig;
6634 int dims = bounds.Count;
6635 int [] current_pos = new int [dims];
6636 int top = array_data.Count;
6638 MethodInfo set = null;
6642 ModuleBuilder mb = null;
6643 mb = CodeGen.Module.Builder;
6644 args = new Type [dims + 1];
6647 for (j = 0; j < dims; j++)
6648 args [j] = TypeManager.int32_type;
6650 args [j] = array_element_type;
6652 set = mb.GetArrayMethod (
6654 CallingConventions.HasThis | CallingConventions.Standard,
6655 TypeManager.void_type, args);
6658 for (int i = 0; i < top; i++){
6660 Expression e = null;
6662 if (array_data [i] is Expression)
6663 e = (Expression) array_data [i];
6667 // Basically we do this for string literals and
6668 // other non-literal expressions
6670 if (e is EnumConstant){
6671 e = ((EnumConstant) e).Child;
6674 if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
6675 num_automatic_initializers <= max_automatic_initializers) {
6676 Type etype = e.Type;
6678 if (is_expression || i != top - 1)
6679 ig.Emit (OpCodes.Dup);
6681 for (int idx = 0; idx < dims; idx++)
6682 IntConstant.EmitInt (ig, current_pos [idx]);
6685 // If we are dealing with a struct, get the
6686 // address of it, so we can store it.
6689 etype.IsSubclassOf (TypeManager.value_type) &&
6690 (!TypeManager.IsBuiltinOrEnum (etype) ||
6691 etype == TypeManager.decimal_type)) {
6696 // Let new know that we are providing
6697 // the address where to store the results
6699 n.DisableTemporaryValueType ();
6702 ig.Emit (OpCodes.Ldelema, etype);
6708 ArrayAccess.EmitStoreOpcode (ig, array_element_type);
6710 ig.Emit (OpCodes.Call, set);
6718 for (int j = dims - 1; j >= 0; j--){
6720 if (current_pos [j] < (int) bounds [j])
6722 current_pos [j] = 0;
6727 void EmitArrayArguments (EmitContext ec)
6729 ILGenerator ig = ec.ig;
6731 foreach (Argument a in arguments) {
6732 Type atype = a.Type;
6735 if (atype == TypeManager.uint64_type)
6736 ig.Emit (OpCodes.Conv_Ovf_U4);
6737 else if (atype == TypeManager.int64_type)
6738 ig.Emit (OpCodes.Conv_Ovf_I4);
6742 void DoEmit (EmitContext ec, bool is_statement)
6744 ILGenerator ig = ec.ig;
6746 EmitArrayArguments (ec);
6747 if (is_one_dimensional)
6748 ig.Emit (OpCodes.Newarr, array_element_type);
6750 if (is_builtin_type)
6751 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
6753 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
6756 if (initializers != null){
6758 // FIXME: Set this variable correctly.
6760 bool dynamic_initializers = true;
6762 // This will never be true for array types that cannot be statically
6763 // initialized. num_automatic_initializers will always be zero. See
6765 if (num_automatic_initializers > max_automatic_initializers)
6766 EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
6768 if (dynamic_initializers)
6769 EmitDynamicInitializers (ec, !is_statement);
6773 public override void Emit (EmitContext ec)
6778 public override void EmitStatement (EmitContext ec)
6783 public object EncodeAsAttribute ()
6785 if (!is_one_dimensional){
6786 Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6790 if (array_data == null){
6791 Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6795 object [] ret = new object [array_data.Count];
6797 foreach (Expression e in array_data){
6800 if (e is NullLiteral)
6803 if (!Attribute.GetAttributeArgumentExpression (e, Location, out v))
6811 public Expression TurnIntoConstant ()
6814 // Should use something like the above attribute thing.
6815 // It should return a subclass of Constant that just returns
6816 // the computed value of the array
6818 throw new Exception ("Does not support yet Turning array into a Constant");
6823 /// Represents the `this' construct
6825 public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
6828 VariableInfo variable_info;
6830 public This (Block block, Location loc)
6836 public This (Location loc)
6841 public VariableInfo VariableInfo {
6842 get { return variable_info; }
6845 public bool VerifyFixed (bool is_expression)
6847 if ((variable_info == null) || (variable_info.LocalInfo == null))
6850 return variable_info.LocalInfo.IsFixed;
6853 public bool ResolveBase (EmitContext ec)
6855 eclass = ExprClass.Variable;
6857 if (ec.TypeContainer.CurrentType != null)
6858 type = ec.TypeContainer.CurrentType.ResolveType (ec);
6860 type = ec.ContainerType;
6863 Error (26, "Keyword this not valid in static code");
6867 if ((block != null) && (block.ThisVariable != null))
6868 variable_info = block.ThisVariable.VariableInfo;
6873 public override Expression DoResolve (EmitContext ec)
6875 if (!ResolveBase (ec))
6878 if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
6879 Error (188, "The this object cannot be used before all " +
6880 "of its fields are assigned to");
6881 variable_info.SetAssigned (ec);
6885 if (ec.IsFieldInitializer) {
6886 Error (27, "Keyword `this' can't be used outside a constructor, " +
6887 "a method or a property.");
6894 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6896 if (!ResolveBase (ec))
6899 if (variable_info != null)
6900 variable_info.SetAssigned (ec);
6902 if (ec.TypeContainer is Class){
6903 Error (1604, "Cannot assign to `this'");
6910 public override void Emit (EmitContext ec)
6912 ILGenerator ig = ec.ig;
6915 if (ec.TypeContainer is Struct)
6916 ig.Emit (OpCodes.Ldobj, type);
6919 public void EmitAssign (EmitContext ec, Expression source)
6921 ILGenerator ig = ec.ig;
6923 if (ec.TypeContainer is Struct){
6926 ig.Emit (OpCodes.Stobj, type);
6929 ig.Emit (OpCodes.Starg, 0);
6933 public void AddressOf (EmitContext ec, AddressOp mode)
6938 // FIGURE OUT WHY LDARG_S does not work
6940 // consider: struct X { int val; int P { set { val = value; }}}
6942 // Yes, this looks very bad. Look at `NOTAS' for
6944 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
6949 // This produces the value that renders an instance, used by the iterators code
6951 public class ProxyInstance : Expression, IMemoryLocation {
6952 public override Expression DoResolve (EmitContext ec)
6954 eclass = ExprClass.Variable;
6955 type = ec.ContainerType;
6959 public override void Emit (EmitContext ec)
6961 ec.ig.Emit (OpCodes.Ldarg_0);
6965 public void AddressOf (EmitContext ec, AddressOp mode)
6967 ec.ig.Emit (OpCodes.Ldarg_0);
6972 /// Implements the typeof operator
6974 public class TypeOf : Expression {
6975 public readonly Expression QueriedType;
6976 protected Type typearg;
6978 public TypeOf (Expression queried_type, Location l)
6980 QueriedType = queried_type;
6984 public override Expression DoResolve (EmitContext ec)
6986 typearg = ec.DeclSpace.ResolveType (QueriedType, false, loc);
6988 if (typearg == null)
6991 if (typearg == TypeManager.void_type) {
6992 Error (673, "System.Void cannot be used from C# - " +
6993 "use typeof (void) to get the void type object");
6997 type = TypeManager.type_type;
6998 eclass = ExprClass.Type;
7002 public override void Emit (EmitContext ec)
7004 ec.ig.Emit (OpCodes.Ldtoken, typearg);
7005 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
7008 public Type TypeArg {
7009 get { return typearg; }
7014 /// Implements the `typeof (void)' operator
7016 public class TypeOfVoid : TypeOf {
7017 public TypeOfVoid (Location l) : base (null, l)
7022 public override Expression DoResolve (EmitContext ec)
7024 type = TypeManager.type_type;
7025 typearg = TypeManager.void_type;
7026 eclass = ExprClass.Type;
7032 /// Implements the sizeof expression
7034 public class SizeOf : Expression {
7035 public Expression QueriedType;
7038 public SizeOf (Expression queried_type, Location l)
7040 this.QueriedType = queried_type;
7044 public override Expression DoResolve (EmitContext ec)
7048 233, loc, "Sizeof may only be used in an unsafe context " +
7049 "(consider using System.Runtime.InteropServices.Marshal.Sizeof");
7053 QueriedType = ec.DeclSpace.ResolveTypeExpr (QueriedType, false, loc);
7054 if (QueriedType == null || QueriedType.Type == null)
7057 if (QueriedType is TypeParameterExpr){
7058 ((TypeParameterExpr)QueriedType).Error_CannotUseAsUnmanagedType (loc);
7062 type_queried = QueriedType.Type;
7063 if (!TypeManager.IsUnmanagedType (type_queried)){
7064 Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
7068 type = TypeManager.int32_type;
7069 eclass = ExprClass.Value;
7073 public override void Emit (EmitContext ec)
7075 int size = GetTypeSize (type_queried);
7078 ec.ig.Emit (OpCodes.Sizeof, type_queried);
7080 IntConstant.EmitInt (ec.ig, size);
7085 /// Implements the member access expression
7087 public class MemberAccess : Expression {
7088 public string Identifier;
7089 protected Expression expr;
7090 protected TypeArguments args;
7092 public MemberAccess (Expression expr, string id, Location l)
7099 public MemberAccess (Expression expr, string id, TypeArguments args,
7101 : this (expr, id, l)
7106 public Expression Expr {
7112 static void error176 (Location loc, string name)
7114 Report.Error (176, loc, "Static member `" +
7115 name + "' cannot be accessed " +
7116 "with an instance reference, qualify with a " +
7117 "type name instead");
7120 static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
7122 if (left_original == null)
7125 if (!(left_original is SimpleName))
7128 SimpleName sn = (SimpleName) left_original;
7130 TypeExpr t = RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc);
7137 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
7138 Expression left, Location loc,
7139 Expression left_original)
7141 bool left_is_type, left_is_explicit;
7143 // If `left' is null, then we're called from SimpleNameResolve and this is
7144 // a member in the currently defining class.
7146 left_is_type = ec.IsStatic || ec.IsFieldInitializer;
7147 left_is_explicit = false;
7149 // Implicitly default to `this' unless we're static.
7150 if (!ec.IsStatic && !ec.IsFieldInitializer && !ec.InEnumContext)
7151 left = ec.GetThis (loc);
7153 left_is_type = left is TypeExpr;
7154 left_is_explicit = true;
7157 if (member_lookup is FieldExpr){
7158 FieldExpr fe = (FieldExpr) member_lookup;
7159 FieldInfo fi = fe.FieldInfo;
7160 Type decl_type = fi.DeclaringType;
7162 if (fi is FieldBuilder) {
7163 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
7167 if (!c.LookupConstantValue (out o))
7170 object real_value = ((Constant) c.Expr).GetValue ();
7172 return Constantify (real_value, fi.FieldType);
7177 Type t = fi.FieldType;
7181 if (fi is FieldBuilder)
7182 o = TypeManager.GetValue ((FieldBuilder) fi);
7184 o = fi.GetValue (fi);
7186 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
7187 if (left_is_explicit && !left_is_type &&
7188 !IdenticalNameAndTypeName (ec, left_original, loc)) {
7189 error176 (loc, fe.FieldInfo.Name);
7193 Expression enum_member = MemberLookup (
7194 ec, decl_type, "value__", MemberTypes.Field,
7195 AllBindingFlags, loc);
7197 Enum en = TypeManager.LookupEnum (decl_type);
7201 c = Constantify (o, en.UnderlyingType);
7203 c = Constantify (o, enum_member.Type);
7205 return new EnumConstant (c, decl_type);
7208 Expression exp = Constantify (o, t);
7210 if (left_is_explicit && !left_is_type) {
7211 error176 (loc, fe.FieldInfo.Name);
7218 if (fi.FieldType.IsPointer && !ec.InUnsafe){
7224 if (member_lookup is EventExpr) {
7225 EventExpr ee = (EventExpr) member_lookup;
7228 // If the event is local to this class, we transform ourselves into
7232 if (ee.EventInfo.DeclaringType == ec.ContainerType ||
7233 TypeManager.IsNestedChildOf(ec.ContainerType, ee.EventInfo.DeclaringType)) {
7234 MemberInfo mi = GetFieldFromEvent (ee);
7238 // If this happens, then we have an event with its own
7239 // accessors and private field etc so there's no need
7240 // to transform ourselves.
7245 Expression ml = ExprClassFromMemberInfo (ec, mi, loc);
7248 Report.Error (-200, loc, "Internal error!!");
7252 if (!left_is_explicit)
7255 return ResolveMemberAccess (ec, ml, left, loc, left_original);
7259 if (member_lookup is IMemberExpr) {
7260 IMemberExpr me = (IMemberExpr) member_lookup;
7263 MethodGroupExpr mg = me as MethodGroupExpr;
7264 if ((mg != null) && left_is_explicit && left.Type.IsInterface)
7265 mg.IsExplicitImpl = left_is_explicit;
7268 if ((ec.IsFieldInitializer || ec.IsStatic) &&
7269 IdenticalNameAndTypeName (ec, left_original, loc))
7270 return member_lookup;
7272 SimpleName.Error_ObjectRefRequired (ec, loc, me.Name);
7277 if (!me.IsInstance){
7278 if (IdenticalNameAndTypeName (ec, left_original, loc))
7279 return member_lookup;
7281 if (left_is_explicit) {
7282 error176 (loc, me.Name);
7288 // Since we can not check for instance objects in SimpleName,
7289 // becaue of the rule that allows types and variables to share
7290 // the name (as long as they can be de-ambiguated later, see
7291 // IdenticalNameAndTypeName), we have to check whether left
7292 // is an instance variable in a static context
7294 // However, if the left-hand value is explicitly given, then
7295 // it is already our instance expression, so we aren't in
7299 if (ec.IsStatic && !left_is_explicit && left is IMemberExpr){
7300 IMemberExpr mexp = (IMemberExpr) left;
7302 if (!mexp.IsStatic){
7303 SimpleName.Error_ObjectRefRequired (ec, loc, mexp.Name);
7308 me.InstanceExpression = left;
7311 return member_lookup;
7314 Console.WriteLine ("Left is: " + left);
7315 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
7316 Environment.Exit (1);
7320 public virtual Expression DoResolve (EmitContext ec, Expression right_side,
7324 throw new Exception ();
7327 // Resolve the expression with flow analysis turned off, we'll do the definite
7328 // assignment checks later. This is because we don't know yet what the expression
7329 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7330 // definite assignment check on the actual field and not on the whole struct.
7333 Expression original = expr;
7334 expr = expr.Resolve (ec, flags | ResolveFlags.DisableFlowAnalysis);
7338 if (expr is SimpleName){
7339 SimpleName child_expr = (SimpleName) expr;
7341 Expression new_expr = new SimpleName (child_expr.Name, Identifier, loc);
7343 return new_expr.Resolve (ec, flags);
7347 // TODO: I mailed Ravi about this, and apparently we can get rid
7348 // of this and put it in the right place.
7350 // Handle enums here when they are in transit.
7351 // Note that we cannot afford to hit MemberLookup in this case because
7352 // it will fail to find any members at all
7356 if (expr is TypeExpr){
7357 expr_type = ((TypeExpr) expr).ResolveType (ec);
7359 if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
7360 Error (122, "`" + expr_type + "' " +
7361 "is inaccessible because of its protection level");
7365 if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){
7366 Enum en = TypeManager.LookupEnum (expr_type);
7369 object value = en.LookupEnumValue (ec, Identifier, loc);
7372 Constant c = Constantify (value, en.UnderlyingType);
7373 return new EnumConstant (c, expr_type);
7378 expr_type = expr.Type;
7380 if (expr_type.IsPointer){
7381 Error (23, "The `.' operator can not be applied to pointer operands (" +
7382 TypeManager.CSharpName (expr_type) + ")");
7386 int errors = Report.Errors;
7388 Expression member_lookup;
7389 member_lookup = MemberLookup (
7390 ec, expr_type, expr_type, Identifier, loc);
7391 if ((member_lookup == null) && (args != null)) {
7392 string lookup_id = Identifier + "!" + args.Count;
7393 member_lookup = MemberLookup (
7394 ec, expr_type, expr_type, lookup_id, loc);
7396 if (member_lookup == null) {
7397 MemberLookupFailed (
7398 ec, expr_type, expr_type, Identifier, null, loc);
7402 if (member_lookup is TypeExpr) {
7403 if (!(expr is TypeExpr) && !(expr is SimpleName)) {
7404 Error (572, "Can't reference type `" + Identifier + "' through an expression; try `" +
7405 member_lookup.Type + "' instead");
7409 return member_lookup;
7413 string full_name = expr_type + "." + Identifier;
7415 if (member_lookup is FieldExpr) {
7416 Report.Error (307, loc, "The field `{0}' cannot " +
7417 "be used with type arguments", full_name);
7419 } else if (member_lookup is EventExpr) {
7420 Report.Error (307, loc, "The event `{0}' cannot " +
7421 "be used with type arguments", full_name);
7423 } else if (member_lookup is PropertyExpr) {
7424 Report.Error (307, loc, "The property `{0}' cannot " +
7425 "be used with type arguments", full_name);
7430 member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original);
7431 if (member_lookup == null)
7435 MethodGroupExpr mg = member_lookup as MethodGroupExpr;
7437 throw new InternalErrorException ();
7439 if (args.Resolve (ec) == false)
7442 Type[] atypes = args.Arguments;
7444 int first_count = 0;
7445 MethodInfo first = null;
7447 ArrayList list = new ArrayList ();
7448 foreach (MethodBase mb in mg.Methods) {
7449 MethodInfo mi = mb as MethodInfo;
7450 if ((mi == null) || !mi.HasGenericParameters)
7453 Type[] gen_params = mi.GetGenericArguments ();
7455 if (first == null) {
7457 first_count = gen_params.Length;
7460 if (gen_params.Length != atypes.Length)
7463 list.Add (mi.BindGenericParameters (atypes));
7466 if (list.Count > 0) {
7467 MethodGroupExpr new_mg = new MethodGroupExpr (
7469 new_mg.InstanceExpression = mg.InstanceExpression;
7470 new_mg.HasTypeArguments = true;
7474 string name = expr_type + "." + Identifier;
7478 305, loc, "Using the generic method `{0}' " +
7479 "requires {1} type arguments", name,
7483 308, loc, "The non-generic method `{0}' " +
7484 "cannot be used with type arguments", name);
7489 // The following DoResolve/DoResolveLValue will do the definite assignment
7492 if (right_side != null)
7493 member_lookup = member_lookup.DoResolveLValue (ec, right_side);
7495 member_lookup = member_lookup.DoResolve (ec);
7497 return member_lookup;
7500 public override Expression DoResolve (EmitContext ec)
7502 return DoResolve (ec, null, ResolveFlags.VariableOrValue |
7503 ResolveFlags.SimpleName | ResolveFlags.Type);
7506 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7508 return DoResolve (ec, right_side, ResolveFlags.VariableOrValue |
7509 ResolveFlags.SimpleName | ResolveFlags.Type);
7512 public override Expression ResolveAsTypeStep (EmitContext ec)
7514 string fname = null;
7515 MemberAccess full_expr = this;
7516 while (full_expr != null) {
7518 fname = String.Concat (full_expr.Identifier, ".", fname);
7520 fname = full_expr.Identifier;
7522 if (full_expr.Expr is SimpleName) {
7523 string full_name = String.Concat (((SimpleName) full_expr.Expr).Name, ".", fname);
7524 Type fully_qualified = ec.DeclSpace.FindType (loc, full_name);
7525 if (fully_qualified != null)
7526 return new TypeExpression (fully_qualified, loc);
7529 full_expr = full_expr.Expr as MemberAccess;
7532 Expression new_expr = expr.ResolveAsTypeStep (ec);
7534 if (new_expr == null)
7537 if (new_expr is SimpleName){
7538 SimpleName child_expr = (SimpleName) new_expr;
7540 new_expr = new SimpleName (child_expr.Name, Identifier, loc);
7542 return new_expr.ResolveAsTypeStep (ec);
7545 Type expr_type = ((TypeExpr) new_expr).ResolveType (ec);
7547 if (expr_type.IsPointer){
7548 Error (23, "The `.' operator can not be applied to pointer operands (" +
7549 TypeManager.CSharpName (expr_type) + ")");
7553 Expression member_lookup;
7556 lookup_id = Identifier + "!" + args.Count;
7558 lookup_id = Identifier;
7559 member_lookup = MemberLookupFinal (
7560 ec, expr_type, expr_type, lookup_id, loc);
7561 if (member_lookup == null)
7564 TypeExpr texpr = member_lookup as TypeExpr;
7568 Type t = texpr.ResolveType (ec);
7572 if (TypeManager.HasGenericArguments (expr_type)) {
7573 Type[] decl_args = TypeManager.GetTypeArguments (expr_type);
7575 TypeArguments new_args = new TypeArguments (loc);
7576 foreach (Type decl in decl_args)
7577 new_args.Add (new TypeExpression (decl, loc));
7580 new_args.Add (args);
7586 ConstructedType ctype = new ConstructedType (t, args, loc);
7587 return ctype.ResolveAsTypeStep (ec);
7593 public override void Emit (EmitContext ec)
7595 throw new Exception ("Should not happen");
7598 public override string ToString ()
7601 return expr + "." + Identifier + "!" + args.Count;
7603 return expr + "." + Identifier;
7608 /// Implements checked expressions
7610 public class CheckedExpr : Expression {
7612 public Expression Expr;
7614 public CheckedExpr (Expression e, Location l)
7620 public override Expression DoResolve (EmitContext ec)
7622 bool last_check = ec.CheckState;
7623 bool last_const_check = ec.ConstantCheckState;
7625 ec.CheckState = true;
7626 ec.ConstantCheckState = true;
7627 Expr = Expr.Resolve (ec);
7628 ec.CheckState = last_check;
7629 ec.ConstantCheckState = last_const_check;
7634 if (Expr is Constant)
7637 eclass = Expr.eclass;
7642 public override void Emit (EmitContext ec)
7644 bool last_check = ec.CheckState;
7645 bool last_const_check = ec.ConstantCheckState;
7647 ec.CheckState = true;
7648 ec.ConstantCheckState = true;
7650 ec.CheckState = last_check;
7651 ec.ConstantCheckState = last_const_check;
7657 /// Implements the unchecked expression
7659 public class UnCheckedExpr : Expression {
7661 public Expression Expr;
7663 public UnCheckedExpr (Expression e, Location l)
7669 public override Expression DoResolve (EmitContext ec)
7671 bool last_check = ec.CheckState;
7672 bool last_const_check = ec.ConstantCheckState;
7674 ec.CheckState = false;
7675 ec.ConstantCheckState = false;
7676 Expr = Expr.Resolve (ec);
7677 ec.CheckState = last_check;
7678 ec.ConstantCheckState = last_const_check;
7683 if (Expr is Constant)
7686 eclass = Expr.eclass;
7691 public override void Emit (EmitContext ec)
7693 bool last_check = ec.CheckState;
7694 bool last_const_check = ec.ConstantCheckState;
7696 ec.CheckState = false;
7697 ec.ConstantCheckState = false;
7699 ec.CheckState = last_check;
7700 ec.ConstantCheckState = last_const_check;
7706 /// An Element Access expression.
7708 /// During semantic analysis these are transformed into
7709 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7711 public class ElementAccess : Expression {
7712 public ArrayList Arguments;
7713 public Expression Expr;
7715 public ElementAccess (Expression e, ArrayList e_list, Location l)
7724 Arguments = new ArrayList ();
7725 foreach (Expression tmp in e_list)
7726 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7730 bool CommonResolve (EmitContext ec)
7732 Expr = Expr.Resolve (ec);
7737 if (Arguments == null)
7740 foreach (Argument a in Arguments){
7741 if (!a.Resolve (ec, loc))
7748 Expression MakePointerAccess ()
7752 if (t == TypeManager.void_ptr_type){
7753 Error (242, "The array index operation is not valid for void pointers");
7756 if (Arguments.Count != 1){
7757 Error (196, "A pointer must be indexed by a single value");
7762 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc);
7763 return new Indirection (p, loc);
7766 public override Expression DoResolve (EmitContext ec)
7768 if (!CommonResolve (ec))
7772 // We perform some simple tests, and then to "split" the emit and store
7773 // code we create an instance of a different class, and return that.
7775 // I am experimenting with this pattern.
7779 if (t == TypeManager.array_type){
7780 Report.Error (21, loc, "Cannot use indexer on System.Array");
7785 return (new ArrayAccess (this, loc)).Resolve (ec);
7786 else if (t.IsPointer)
7787 return MakePointerAccess ();
7789 return (new IndexerAccess (this, loc)).Resolve (ec);
7792 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7794 if (!CommonResolve (ec))
7799 return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
7800 else if (t.IsPointer)
7801 return MakePointerAccess ();
7803 return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
7806 public override void Emit (EmitContext ec)
7808 throw new Exception ("Should never be reached");
7813 /// Implements array access
7815 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7817 // Points to our "data" repository
7821 LocalTemporary [] cached_locations;
7823 public ArrayAccess (ElementAccess ea_data, Location l)
7826 eclass = ExprClass.Variable;
7830 public override Expression DoResolve (EmitContext ec)
7833 ExprClass eclass = ea.Expr.eclass;
7835 // As long as the type is valid
7836 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7837 eclass == ExprClass.Value)) {
7838 ea.Expr.Error_UnexpectedKind ("variable or value");
7843 Type t = ea.Expr.Type;
7844 if (t.GetArrayRank () != ea.Arguments.Count){
7846 "Incorrect number of indexes for array " +
7847 " expected: " + t.GetArrayRank () + " got: " +
7848 ea.Arguments.Count);
7852 type = TypeManager.GetElementType (t);
7853 if (type.IsPointer && !ec.InUnsafe){
7854 UnsafeError (ea.Location);
7858 foreach (Argument a in ea.Arguments){
7859 Type argtype = a.Type;
7861 if (argtype == TypeManager.int32_type ||
7862 argtype == TypeManager.uint32_type ||
7863 argtype == TypeManager.int64_type ||
7864 argtype == TypeManager.uint64_type)
7868 // Mhm. This is strage, because the Argument.Type is not the same as
7869 // Argument.Expr.Type: the value changes depending on the ref/out setting.
7871 // Wonder if I will run into trouble for this.
7873 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.Location);
7878 eclass = ExprClass.Variable;
7884 /// Emits the right opcode to load an object of Type `t'
7885 /// from an array of T
7887 static public void EmitLoadOpcode (ILGenerator ig, Type type)
7889 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7890 ig.Emit (OpCodes.Ldelem_U1);
7891 else if (type == TypeManager.sbyte_type)
7892 ig.Emit (OpCodes.Ldelem_I1);
7893 else if (type == TypeManager.short_type)
7894 ig.Emit (OpCodes.Ldelem_I2);
7895 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7896 ig.Emit (OpCodes.Ldelem_U2);
7897 else if (type == TypeManager.int32_type)
7898 ig.Emit (OpCodes.Ldelem_I4);
7899 else if (type == TypeManager.uint32_type)
7900 ig.Emit (OpCodes.Ldelem_U4);
7901 else if (type == TypeManager.uint64_type)
7902 ig.Emit (OpCodes.Ldelem_I8);
7903 else if (type == TypeManager.int64_type)
7904 ig.Emit (OpCodes.Ldelem_I8);
7905 else if (type == TypeManager.float_type)
7906 ig.Emit (OpCodes.Ldelem_R4);
7907 else if (type == TypeManager.double_type)
7908 ig.Emit (OpCodes.Ldelem_R8);
7909 else if (type == TypeManager.intptr_type)
7910 ig.Emit (OpCodes.Ldelem_I);
7911 else if (TypeManager.IsEnumType (type)){
7912 EmitLoadOpcode (ig, TypeManager.EnumToUnderlying (type));
7913 } else if (type.IsValueType){
7914 ig.Emit (OpCodes.Ldelema, type);
7915 ig.Emit (OpCodes.Ldobj, type);
7916 } else if (type.IsGenericParameter)
7917 ig.Emit (OpCodes.Ldelem_Any, type);
7919 ig.Emit (OpCodes.Ldelem_Ref);
7923 /// Emits the right opcode to store an object of Type `t'
7924 /// from an array of T.
7926 static public void EmitStoreOpcode (ILGenerator ig, Type t)
7928 bool is_stobj, has_type_arg;
7929 OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
7937 /// Returns the right opcode to store an object of Type `t'
7938 /// from an array of T.
7940 static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
7942 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7943 has_type_arg = false; is_stobj = false;
7944 t = TypeManager.TypeToCoreType (t);
7945 if (TypeManager.IsEnumType (t) && t != TypeManager.enum_type)
7946 t = TypeManager.EnumToUnderlying (t);
7947 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7948 t == TypeManager.bool_type)
7949 return OpCodes.Stelem_I1;
7950 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7951 t == TypeManager.char_type)
7952 return OpCodes.Stelem_I2;
7953 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7954 return OpCodes.Stelem_I4;
7955 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7956 return OpCodes.Stelem_I8;
7957 else if (t == TypeManager.float_type)
7958 return OpCodes.Stelem_R4;
7959 else if (t == TypeManager.double_type)
7960 return OpCodes.Stelem_R8;
7961 else if (t == TypeManager.intptr_type) {
7962 has_type_arg = true;
7964 return OpCodes.Stobj;
7965 } else if (t.IsValueType) {
7966 has_type_arg = true;
7968 return OpCodes.Stobj;
7969 } else if (t.IsGenericParameter) {
7970 has_type_arg = true;
7971 return OpCodes.Stelem_Any;
7973 return OpCodes.Stelem_Ref;
7976 MethodInfo FetchGetMethod ()
7978 ModuleBuilder mb = CodeGen.Module.Builder;
7979 int arg_count = ea.Arguments.Count;
7980 Type [] args = new Type [arg_count];
7983 for (int i = 0; i < arg_count; i++){
7984 //args [i++] = a.Type;
7985 args [i] = TypeManager.int32_type;
7988 get = mb.GetArrayMethod (
7989 ea.Expr.Type, "Get",
7990 CallingConventions.HasThis |
7991 CallingConventions.Standard,
7997 MethodInfo FetchAddressMethod ()
7999 ModuleBuilder mb = CodeGen.Module.Builder;
8000 int arg_count = ea.Arguments.Count;
8001 Type [] args = new Type [arg_count];
8005 ret_type = TypeManager.GetReferenceType (type);
8007 for (int i = 0; i < arg_count; i++){
8008 //args [i++] = a.Type;
8009 args [i] = TypeManager.int32_type;
8012 address = mb.GetArrayMethod (
8013 ea.Expr.Type, "Address",
8014 CallingConventions.HasThis |
8015 CallingConventions.Standard,
8022 // Load the array arguments into the stack.
8024 // If we have been requested to cache the values (cached_locations array
8025 // initialized), then load the arguments the first time and store them
8026 // in locals. otherwise load from local variables.
8028 void LoadArrayAndArguments (EmitContext ec)
8030 ILGenerator ig = ec.ig;
8032 if (cached_locations == null){
8034 foreach (Argument a in ea.Arguments){
8035 Type argtype = a.Expr.Type;
8039 if (argtype == TypeManager.int64_type)
8040 ig.Emit (OpCodes.Conv_Ovf_I);
8041 else if (argtype == TypeManager.uint64_type)
8042 ig.Emit (OpCodes.Conv_Ovf_I_Un);
8047 if (cached_locations [0] == null){
8048 cached_locations [0] = new LocalTemporary (ec, ea.Expr.Type);
8050 ig.Emit (OpCodes.Dup);
8051 cached_locations [0].Store (ec);
8055 foreach (Argument a in ea.Arguments){
8056 Type argtype = a.Expr.Type;
8058 cached_locations [j] = new LocalTemporary (ec, TypeManager.intptr_type /* a.Expr.Type */);
8060 if (argtype == TypeManager.int64_type)
8061 ig.Emit (OpCodes.Conv_Ovf_I);
8062 else if (argtype == TypeManager.uint64_type)
8063 ig.Emit (OpCodes.Conv_Ovf_I_Un);
8065 ig.Emit (OpCodes.Dup);
8066 cached_locations [j].Store (ec);
8072 foreach (LocalTemporary lt in cached_locations)
8076 public new void CacheTemporaries (EmitContext ec)
8078 cached_locations = new LocalTemporary [ea.Arguments.Count + 1];
8081 public override void Emit (EmitContext ec)
8083 int rank = ea.Expr.Type.GetArrayRank ();
8084 ILGenerator ig = ec.ig;
8086 LoadArrayAndArguments (ec);
8089 EmitLoadOpcode (ig, type);
8093 method = FetchGetMethod ();
8094 ig.Emit (OpCodes.Call, method);
8098 public void EmitAssign (EmitContext ec, Expression source)
8100 int rank = ea.Expr.Type.GetArrayRank ();
8101 ILGenerator ig = ec.ig;
8102 Type t = source.Type;
8104 LoadArrayAndArguments (ec);
8107 // The stobj opcode used by value types will need
8108 // an address on the stack, not really an array/array
8112 if (t == TypeManager.enum_type || t == TypeManager.decimal_type ||
8113 (t.IsSubclassOf (TypeManager.value_type) && !TypeManager.IsEnumType (t) && !TypeManager.IsBuiltinType (t)))
8114 ig.Emit (OpCodes.Ldelema, t);
8120 EmitStoreOpcode (ig, t);
8122 ModuleBuilder mb = CodeGen.Module.Builder;
8123 int arg_count = ea.Arguments.Count;
8124 Type [] args = new Type [arg_count + 1];
8127 for (int i = 0; i < arg_count; i++){
8128 //args [i++] = a.Type;
8129 args [i] = TypeManager.int32_type;
8132 args [arg_count] = type;
8134 set = mb.GetArrayMethod (
8135 ea.Expr.Type, "Set",
8136 CallingConventions.HasThis |
8137 CallingConventions.Standard,
8138 TypeManager.void_type, args);
8140 ig.Emit (OpCodes.Call, set);
8144 public void AddressOf (EmitContext ec, AddressOp mode)
8146 int rank = ea.Expr.Type.GetArrayRank ();
8147 ILGenerator ig = ec.ig;
8149 LoadArrayAndArguments (ec);
8152 ig.Emit (OpCodes.Ldelema, type);
8154 MethodInfo address = FetchAddressMethod ();
8155 ig.Emit (OpCodes.Call, address);
8162 public ArrayList Properties;
8163 static Hashtable map;
8165 public struct Indexer {
8166 public readonly Type Type;
8167 public readonly MethodInfo Getter, Setter;
8169 public Indexer (Type type, MethodInfo get, MethodInfo set)
8179 map = new Hashtable ();
8184 Properties = new ArrayList ();
8187 void Append (MemberInfo [] mi)
8189 foreach (PropertyInfo property in mi){
8190 MethodInfo get, set;
8192 get = property.GetGetMethod (true);
8193 set = property.GetSetMethod (true);
8194 Properties.Add (new Indexer (property.PropertyType, get, set));
8198 static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
8200 string p_name = TypeManager.IndexerPropertyName (lookup_type);
8202 MemberInfo [] mi = TypeManager.MemberLookup (
8203 caller_type, caller_type, lookup_type, MemberTypes.Property,
8204 BindingFlags.Public | BindingFlags.Instance |
8205 BindingFlags.DeclaredOnly, p_name);
8207 if (mi == null || mi.Length == 0)
8213 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
8215 Indexers ix = (Indexers) map [lookup_type];
8220 Type copy = lookup_type;
8221 while (copy != TypeManager.object_type && copy != null){
8222 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, copy);
8226 ix = new Indexers ();
8231 copy = copy.BaseType;
8234 if (!lookup_type.IsInterface)
8237 TypeExpr [] ifaces = TypeManager.GetInterfaces (lookup_type);
8238 if (ifaces != null) {
8239 foreach (TypeExpr iface in ifaces) {
8240 Type itype = iface.Type;
8241 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, itype);
8244 ix = new Indexers ();
8256 /// Expressions that represent an indexer call.
8258 public class IndexerAccess : Expression, IAssignMethod {
8260 // Points to our "data" repository
8262 MethodInfo get, set;
8263 ArrayList set_arguments;
8264 bool is_base_indexer;
8266 protected Type indexer_type;
8267 protected Type current_type;
8268 protected Expression instance_expr;
8269 protected ArrayList arguments;
8271 public IndexerAccess (ElementAccess ea, Location loc)
8272 : this (ea.Expr, false, loc)
8274 this.arguments = ea.Arguments;
8277 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
8280 this.instance_expr = instance_expr;
8281 this.is_base_indexer = is_base_indexer;
8282 this.eclass = ExprClass.Value;
8286 protected virtual bool CommonResolve (EmitContext ec)
8288 indexer_type = instance_expr.Type;
8289 current_type = ec.ContainerType;
8294 public override Expression DoResolve (EmitContext ec)
8296 ArrayList AllGetters = new ArrayList();
8297 if (!CommonResolve (ec))
8301 // Step 1: Query for all `Item' *properties*. Notice
8302 // that the actual methods are pointed from here.
8304 // This is a group of properties, piles of them.
8306 bool found_any = false, found_any_getters = false;
8307 Type lookup_type = indexer_type;
8310 ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
8311 if (ilist != null) {
8313 if (ilist.Properties != null) {
8314 foreach (Indexers.Indexer ix in ilist.Properties) {
8315 if (ix.Getter != null)
8316 AllGetters.Add(ix.Getter);
8321 if (AllGetters.Count > 0) {
8322 found_any_getters = true;
8323 get = (MethodInfo) Invocation.OverloadResolve (
8324 ec, new MethodGroupExpr (AllGetters, loc),
8325 arguments, false, loc);
8329 Report.Error (21, loc,
8330 "Type `" + TypeManager.CSharpName (indexer_type) +
8331 "' does not have any indexers defined");
8335 if (!found_any_getters) {
8336 Error (154, "indexer can not be used in this context, because " +
8337 "it lacks a `get' accessor");
8342 Error (1501, "No Overload for method `this' takes `" +
8343 arguments.Count + "' arguments");
8348 // Only base will allow this invocation to happen.
8350 if (get.IsAbstract && this is BaseIndexerAccess){
8351 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (get));
8355 type = get.ReturnType;
8356 if (type.IsPointer && !ec.InUnsafe){
8361 eclass = ExprClass.IndexerAccess;
8365 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8367 ArrayList AllSetters = new ArrayList();
8368 if (!CommonResolve (ec))
8371 bool found_any = false, found_any_setters = false;
8373 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc);
8374 if (ilist != null) {
8376 if (ilist.Properties != null) {
8377 foreach (Indexers.Indexer ix in ilist.Properties) {
8378 if (ix.Setter != null)
8379 AllSetters.Add(ix.Setter);
8383 if (AllSetters.Count > 0) {
8384 found_any_setters = true;
8385 set_arguments = (ArrayList) arguments.Clone ();
8386 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
8387 set = (MethodInfo) Invocation.OverloadResolve (
8388 ec, new MethodGroupExpr (AllSetters, loc),
8389 set_arguments, false, loc);
8393 Report.Error (21, loc,
8394 "Type `" + TypeManager.CSharpName (indexer_type) +
8395 "' does not have any indexers defined");
8399 if (!found_any_setters) {
8400 Error (154, "indexer can not be used in this context, because " +
8401 "it lacks a `set' accessor");
8406 Error (1501, "No Overload for method `this' takes `" +
8407 arguments.Count + "' arguments");
8412 // Only base will allow this invocation to happen.
8414 if (set.IsAbstract && this is BaseIndexerAccess){
8415 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (set));
8420 // Now look for the actual match in the list of indexers to set our "return" type
8422 type = TypeManager.void_type; // default value
8423 foreach (Indexers.Indexer ix in ilist.Properties){
8424 if (ix.Setter == set){
8430 eclass = ExprClass.IndexerAccess;
8434 public override void Emit (EmitContext ec)
8436 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc);
8440 // source is ignored, because we already have a copy of it from the
8441 // LValue resolution and we have already constructed a pre-cached
8442 // version of the arguments (ea.set_arguments);
8444 public void EmitAssign (EmitContext ec, Expression source)
8446 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc);
8451 /// The base operator for method names
8453 public class BaseAccess : Expression {
8456 public BaseAccess (string member, Location l)
8458 this.member = member;
8462 public override Expression DoResolve (EmitContext ec)
8464 Expression c = CommonResolve (ec);
8470 // MethodGroups use this opportunity to flag an error on lacking ()
8472 if (!(c is MethodGroupExpr))
8473 return c.Resolve (ec);
8477 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8479 Expression c = CommonResolve (ec);
8485 // MethodGroups use this opportunity to flag an error on lacking ()
8487 if (! (c is MethodGroupExpr))
8488 return c.DoResolveLValue (ec, right_side);
8493 Expression CommonResolve (EmitContext ec)
8495 Expression member_lookup;
8496 Type current_type = ec.ContainerType;
8497 Type base_type = current_type.BaseType;
8501 Error (1511, "Keyword base is not allowed in static method");
8505 if (ec.IsFieldInitializer){
8506 Error (1512, "Keyword base is not available in the current context");
8510 member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type,
8511 member, AllMemberTypes, AllBindingFlags,
8513 if (member_lookup == null) {
8514 MemberLookupFailed (
8515 ec, base_type, base_type, member, null, loc);
8522 left = new TypeExpression (base_type, loc);
8524 left = ec.GetThis (loc);
8526 e = MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
8528 if (e is PropertyExpr){
8529 PropertyExpr pe = (PropertyExpr) e;
8537 public override void Emit (EmitContext ec)
8539 throw new Exception ("Should never be called");
8544 /// The base indexer operator
8546 public class BaseIndexerAccess : IndexerAccess {
8547 public BaseIndexerAccess (ArrayList args, Location loc)
8548 : base (null, true, loc)
8550 arguments = new ArrayList ();
8551 foreach (Expression tmp in args)
8552 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8555 protected override bool CommonResolve (EmitContext ec)
8557 instance_expr = ec.GetThis (loc);
8559 current_type = ec.ContainerType.BaseType;
8560 indexer_type = current_type;
8562 foreach (Argument a in arguments){
8563 if (!a.Resolve (ec, loc))
8572 /// This class exists solely to pass the Type around and to be a dummy
8573 /// that can be passed to the conversion functions (this is used by
8574 /// foreach implementation to typecast the object return value from
8575 /// get_Current into the proper type. All code has been generated and
8576 /// we only care about the side effect conversions to be performed
8578 /// This is also now used as a placeholder where a no-action expression
8579 /// is needed (the `New' class).
8581 public class EmptyExpression : Expression {
8582 public EmptyExpression ()
8584 type = TypeManager.object_type;
8585 eclass = ExprClass.Value;
8586 loc = Location.Null;
8589 public EmptyExpression (Type t)
8592 eclass = ExprClass.Value;
8593 loc = Location.Null;
8596 public override Expression DoResolve (EmitContext ec)
8601 public override void Emit (EmitContext ec)
8603 // nothing, as we only exist to not do anything.
8607 // This is just because we might want to reuse this bad boy
8608 // instead of creating gazillions of EmptyExpressions.
8609 // (CanImplicitConversion uses it)
8611 public void SetType (Type t)
8617 public class UserCast : Expression {
8621 public UserCast (MethodInfo method, Expression source, Location l)
8623 this.method = method;
8624 this.source = source;
8625 type = method.ReturnType;
8626 eclass = ExprClass.Value;
8630 public override Expression DoResolve (EmitContext ec)
8633 // We are born fully resolved
8638 public override void Emit (EmitContext ec)
8640 ILGenerator ig = ec.ig;
8644 if (method is MethodInfo)
8645 ig.Emit (OpCodes.Call, (MethodInfo) method);
8647 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
8653 // This class is used to "construct" the type during a typecast
8654 // operation. Since the Type.GetType class in .NET can parse
8655 // the type specification, we just use this to construct the type
8656 // one bit at a time.
8658 public class ComposedCast : TypeExpr {
8662 public ComposedCast (Expression left, string dim, Location l)
8669 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
8671 Type ltype = ec.DeclSpace.ResolveType (left, false, loc);
8675 if ((ltype == TypeManager.void_type) && (dim != "*")) {
8676 Report.Error (1547, Location,
8677 "Keyword 'void' cannot be used in this context");
8682 while ((pos < dim.Length) && (dim [pos] == '[')) {
8685 if (dim [pos] == ']') {
8686 ltype = ltype.MakeArrayType ();
8689 if (pos < dim.Length)
8693 eclass = ExprClass.Type;
8698 while (dim [pos] == ',') {
8702 if ((dim [pos] != ']') || (pos != dim.Length-1))
8705 type = ltype.MakeArrayType (rank + 1);
8706 eclass = ExprClass.Type;
8711 // ltype.Fullname is already fully qualified, so we can skip
8712 // a lot of probes, and go directly to TypeManager.LookupType
8714 string fname = ltype.FullName != null ? ltype.FullName : ltype.Name;
8715 string cname = fname + dim;
8716 type = TypeManager.LookupTypeDirect (cname);
8719 // For arrays of enumerations we are having a problem
8720 // with the direct lookup. Need to investigate.
8722 // For now, fall back to the full lookup in that case.
8724 TypeExpr texpr = RootContext.LookupType (
8725 ec.DeclSpace, cname, false, loc);
8730 type = texpr.ResolveType (ec);
8735 if (!ec.ResolvingTypeTree){
8737 // If the above flag is set, this is being invoked from the ResolveType function.
8738 // Upper layers take care of the type validity in this context.
8740 if (!ec.InUnsafe && type.IsPointer){
8746 eclass = ExprClass.Type;
8750 public override string Name {
8758 // This class is used to represent the address of an array, used
8759 // only by the Fixed statement, this is like the C "&a [0]" construct.
8761 public class ArrayPtr : Expression {
8764 public ArrayPtr (Expression array, Location l)
8766 Type array_type = TypeManager.GetElementType (array.Type);
8770 type = TypeManager.GetPointerType (array_type);
8771 eclass = ExprClass.Value;
8775 public override void Emit (EmitContext ec)
8777 ILGenerator ig = ec.ig;
8780 IntLiteral.EmitInt (ig, 0);
8781 ig.Emit (OpCodes.Ldelema, TypeManager.GetElementType (array.Type));
8784 public override Expression DoResolve (EmitContext ec)
8787 // We are born fully resolved
8794 // Used by the fixed statement
8796 public class StringPtr : Expression {
8799 public StringPtr (LocalBuilder b, Location l)
8802 eclass = ExprClass.Value;
8803 type = TypeManager.char_ptr_type;
8807 public override Expression DoResolve (EmitContext ec)
8809 // This should never be invoked, we are born in fully
8810 // initialized state.
8815 public override void Emit (EmitContext ec)
8817 ILGenerator ig = ec.ig;
8819 ig.Emit (OpCodes.Ldloc, b);
8820 ig.Emit (OpCodes.Conv_I);
8821 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
8822 ig.Emit (OpCodes.Add);
8827 // Implements the `stackalloc' keyword
8829 public class StackAlloc : Expression {
8834 public StackAlloc (Expression type, Expression count, Location l)
8841 public override Expression DoResolve (EmitContext ec)
8843 count = count.Resolve (ec);
8847 if (count.Type != TypeManager.int32_type){
8848 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8853 if (ec.CurrentBranching.InCatch () ||
8854 ec.CurrentBranching.InFinally (true)) {
8856 "stackalloc can not be used in a catch or finally block");
8860 otype = ec.DeclSpace.ResolveType (t, false, loc);
8865 if (!TypeManager.VerifyUnManaged (otype, loc))
8868 type = TypeManager.GetPointerType (otype);
8869 eclass = ExprClass.Value;
8874 public override void Emit (EmitContext ec)
8876 int size = GetTypeSize (otype);
8877 ILGenerator ig = ec.ig;
8880 ig.Emit (OpCodes.Sizeof, otype);
8882 IntConstant.EmitInt (ig, size);
8884 ig.Emit (OpCodes.Mul);
8885 ig.Emit (OpCodes.Localloc);