2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 // This is just a helper class, it is generated by Unary, UnaryMutator
21 // when an overloaded method has been found. It just emits the code for a
24 public class StaticCallExpr : ExpressionStatement {
28 StaticCallExpr (MethodInfo m, ArrayList a)
34 eclass = ExprClass.Value;
37 public override Expression DoResolve (EmitContext ec)
40 // We are born fully resolved
45 public override void Emit (EmitContext ec)
48 Invocation.EmitArguments (ec, mi, args);
50 ec.ig.Emit (OpCodes.Call, mi);
54 static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
55 Expression e, Location loc)
60 args = new ArrayList (1);
61 args.Add (new Argument (e, Argument.AType.Expression));
62 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
67 return new StaticCallExpr ((MethodInfo) method, args);
70 public override void EmitStatement (EmitContext ec)
73 if (type != TypeManager.void_type)
74 ec.ig.Emit (OpCodes.Pop);
83 // Unary implements unary expressions. It derives from
84 // ExpressionStatement becuase the pre/post increment/decrement
85 // operators can be used in a statement context.
87 public class Unary : Expression {
88 public enum Operator : byte {
89 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
90 Indirection, AddressOf,
97 public Unary (Operator op, Expression expr, Location loc)
104 public Expression Expr {
114 public Operator Oper {
125 // Returns a stringified representation of the Operator
130 case Operator.UnaryPlus:
132 case Operator.UnaryNegation:
134 case Operator.LogicalNot:
136 case Operator.OnesComplement:
138 case Operator.AddressOf:
140 case Operator.Indirection:
144 return oper.ToString ();
147 void error23 (Type t)
150 23, loc, "Operator " + OperName () +
151 " cannot be applied to operand of type `" +
152 TypeManager.CSharpName (t) + "'");
155 static Expression TryReduceNegative (Expression expr)
159 if (expr is IntLiteral)
160 e = new IntLiteral (-((IntLiteral) expr).Value);
161 else if (expr is UIntLiteral)
162 e = new LongLiteral (-((UIntLiteral) expr).Value);
163 else if (expr is LongLiteral)
164 e = new LongLiteral (-((LongLiteral) expr).Value);
165 else if (expr is FloatLiteral)
166 e = new FloatLiteral (-((FloatLiteral) expr).Value);
167 else if (expr is DoubleLiteral)
168 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
169 else if (expr is DecimalLiteral)
170 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
175 Expression ResolveOperator (EmitContext ec)
177 Type expr_type = expr.Type;
180 // Step 1: Perform Operator Overload location
185 op_name = "op_" + oper;
187 mg = MemberLookup (ec, expr_type, op_name, false, loc);
189 if (mg == null && expr_type.BaseType != null)
190 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
193 Expression e = StaticCallExpr.MakeSimpleCall (
194 ec, (MethodGroupExpr) mg, expr, loc);
205 // Step 2: Default operations on CLI native types.
208 // Only perform numeric promotions on:
211 if (expr_type == null)
214 if (oper == Operator.LogicalNot){
215 if (expr_type != TypeManager.bool_type) {
220 type = TypeManager.bool_type;
224 if (oper == Operator.OnesComplement) {
225 if (!((expr_type == TypeManager.int32_type) ||
226 (expr_type == TypeManager.uint32_type) ||
227 (expr_type == TypeManager.int64_type) ||
228 (expr_type == TypeManager.uint64_type) ||
229 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
237 if (oper == Operator.UnaryPlus) {
239 // A plus in front of something is just a no-op, so return the child.
245 // Deals with -literals
246 // int operator- (int x)
247 // long operator- (long x)
248 // float operator- (float f)
249 // double operator- (double d)
250 // decimal operator- (decimal d)
252 if (oper == Operator.UnaryNegation){
254 // Fold a "- Constant" into a negative constant
260 // Is this a constant?
262 e = TryReduceNegative (expr);
270 // Not a constant we can optimize, perform numeric
271 // promotions to int, long, double.
274 // The following is inneficient, because we call
275 // ConvertImplicit too many times.
277 // It is also not clear if we should convert to Float
278 // or Double initially.
280 if (expr_type == TypeManager.uint32_type){
282 // FIXME: handle exception to this rule that
283 // permits the int value -2147483648 (-2^31) to
284 // bt written as a decimal interger literal
286 type = TypeManager.int64_type;
287 expr = ConvertImplicit (ec, expr, type, loc);
291 if (expr_type == TypeManager.uint64_type){
293 // FIXME: Handle exception of `long value'
294 // -92233720368547758087 (-2^63) to be written as
295 // decimal integer literal.
301 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
308 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
315 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
326 if (oper == Operator.AddressOf){
327 if (expr.ExprClass != ExprClass.Variable){
328 Error (211, loc, "Cannot take the address of non-variables");
331 type = Type.GetType (expr.Type.ToString () + "*");
336 Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
337 TypeManager.CSharpName (expr_type) + "'");
341 public override Expression DoResolve (EmitContext ec)
343 expr = expr.Resolve (ec);
348 eclass = ExprClass.Value;
349 return ResolveOperator (ec);
352 public override void Emit (EmitContext ec)
354 ILGenerator ig = ec.ig;
355 Type expr_type = expr.Type;
358 case Operator.UnaryPlus:
359 throw new Exception ("This should be caught by Resolve");
361 case Operator.UnaryNegation:
363 ig.Emit (OpCodes.Neg);
366 case Operator.LogicalNot:
368 ig.Emit (OpCodes.Ldc_I4_0);
369 ig.Emit (OpCodes.Ceq);
372 case Operator.OnesComplement:
374 ig.Emit (OpCodes.Not);
377 case Operator.AddressOf:
378 ((IMemoryLocation)expr).AddressOf (ec);
381 case Operator.Indirection:
382 throw new Exception ("Not implemented yet");
385 throw new Exception ("This should not happen: Operator = "
391 // This will emit the child expression for `ec' avoiding the logical
392 // not. The parent will take care of changing brfalse/brtrue
394 public void EmitLogicalNot (EmitContext ec)
396 if (oper != Operator.LogicalNot)
397 throw new Exception ("EmitLogicalNot can only be called with !expr");
402 public override Expression Reduce (EmitContext ec)
407 // First, reduce our child. Note that although we handle
409 expr = expr.Reduce (ec);
410 if (!(expr is Literal))
414 case Operator.UnaryPlus:
417 case Operator.UnaryNegation:
418 e = TryReduceNegative (expr);
423 case Operator.LogicalNot:
424 BoolLiteral b = (BoolLiteral) expr;
426 return new BoolLiteral (!(b.Value));
428 case Operator.OnesComplement:
431 if (et == TypeManager.int32_type)
432 return new IntLiteral (~ ((IntLiteral) expr).Value);
433 if (et == TypeManager.uint32_type)
434 return new UIntLiteral (~ ((UIntLiteral) expr).Value);
435 if (et == TypeManager.int64_type)
436 return new LongLiteral (~ ((LongLiteral) expr).Value);
437 if (et == TypeManager.uint64_type)
438 return new ULongLiteral (~ ((ULongLiteral) expr).Value);
446 // Unary Mutator expressions (pre and post ++ and --)
450 // UnaryMutator implements ++ and -- expressions. It derives from
451 // ExpressionStatement becuase the pre/post increment/decrement
452 // operators can be used in a statement context.
455 // FIXME: Idea, we could split this up in two classes, one simpler
456 // for the common case, and one with the extra fields for more complex
457 // classes (indexers require temporary access; overloaded require method)
459 // Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
460 // PostDecrement, that way we could save the `Mode' byte as well.
462 public class UnaryMutator : ExpressionStatement {
463 public enum Mode : byte {
464 PreIncrement, PreDecrement, PostIncrement, PostDecrement
470 LocalTemporary temp_storage;
473 // This is expensive for the simplest case.
477 public UnaryMutator (Mode m, Expression e, Location l)
486 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
490 void error23 (Type t)
493 23, loc, "Operator " + OperName () +
494 " cannot be applied to operand of type `" +
495 TypeManager.CSharpName (t) + "'");
499 // Returns whether an object of type `t' can be incremented
500 // or decremented with add/sub (ie, basically whether we can
501 // use pre-post incr-decr operations on it, but it is not a
502 // System.Decimal, which we require operator overloading to catch)
504 static bool IsIncrementableNumber (Type t)
506 return (t == TypeManager.sbyte_type) ||
507 (t == TypeManager.byte_type) ||
508 (t == TypeManager.short_type) ||
509 (t == TypeManager.ushort_type) ||
510 (t == TypeManager.int32_type) ||
511 (t == TypeManager.uint32_type) ||
512 (t == TypeManager.int64_type) ||
513 (t == TypeManager.uint64_type) ||
514 (t == TypeManager.char_type) ||
515 (t.IsSubclassOf (TypeManager.enum_type)) ||
516 (t == TypeManager.float_type) ||
517 (t == TypeManager.double_type);
520 Expression ResolveOperator (EmitContext ec)
522 Type expr_type = expr.Type;
525 // Step 1: Perform Operator Overload location
530 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
531 op_name = "op_Increment";
533 op_name = "op_Decrement";
535 mg = MemberLookup (ec, expr_type, op_name, false, loc);
537 if (mg == null && expr_type.BaseType != null)
538 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
541 method = StaticCallExpr.MakeSimpleCall (
542 ec, (MethodGroupExpr) mg, expr, loc);
549 // The operand of the prefix/postfix increment decrement operators
550 // should be an expression that is classified as a variable,
551 // a property access or an indexer access
554 if (expr.ExprClass == ExprClass.Variable){
555 if (IsIncrementableNumber (expr_type) ||
556 expr_type == TypeManager.decimal_type){
559 } else if (expr.ExprClass == ExprClass.IndexerAccess){
560 IndexerAccess ia = (IndexerAccess) expr;
562 temp_storage = new LocalTemporary (ec, expr.Type);
564 expr = ia.ResolveLValue (ec, temp_storage);
569 } else if (expr.ExprClass == ExprClass.PropertyAccess){
570 PropertyExpr pe = (PropertyExpr) expr;
572 if (pe.VerifyAssignable ())
577 report118 (loc, expr, "variable, indexer or property access");
581 Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
582 TypeManager.CSharpName (expr_type) + "'");
586 public override Expression DoResolve (EmitContext ec)
588 expr = expr.Resolve (ec);
593 eclass = ExprClass.Value;
594 return ResolveOperator (ec);
599 // FIXME: We need some way of avoiding the use of temp_storage
600 // for some types of storage (parameters, local variables,
601 // static fields) and single-dimension array access.
603 void EmitCode (EmitContext ec, bool is_expr)
605 ILGenerator ig = ec.ig;
606 IAssignMethod ia = (IAssignMethod) expr;
608 if (temp_storage == null)
609 temp_storage = new LocalTemporary (ec, expr.Type);
612 case Mode.PreIncrement:
613 case Mode.PreDecrement:
617 ig.Emit (OpCodes.Ldc_I4_1);
619 if (mode == Mode.PreDecrement)
620 ig.Emit (OpCodes.Sub);
622 ig.Emit (OpCodes.Add);
626 temp_storage.Store (ec);
627 ia.EmitAssign (ec, temp_storage);
629 temp_storage.Emit (ec);
632 case Mode.PostIncrement:
633 case Mode.PostDecrement:
641 ig.Emit (OpCodes.Dup);
643 ig.Emit (OpCodes.Ldc_I4_1);
645 if (mode == Mode.PostDecrement)
646 ig.Emit (OpCodes.Sub);
648 ig.Emit (OpCodes.Add);
653 temp_storage.Store (ec);
654 ia.EmitAssign (ec, temp_storage);
659 public override void Emit (EmitContext ec)
665 public override void EmitStatement (EmitContext ec)
667 EmitCode (ec, false);
672 public class Probe : Expression {
673 public readonly string ProbeType;
674 public readonly Operator Oper;
678 public enum Operator : byte {
682 public Probe (Operator oper, Expression expr, string probe_type)
685 ProbeType = probe_type;
689 public Expression Expr {
695 public override Expression DoResolve (EmitContext ec)
697 probe_type = ec.TypeContainer.LookupType (ProbeType, false);
699 if (probe_type == null)
702 expr = expr.Resolve (ec);
704 type = TypeManager.bool_type;
705 eclass = ExprClass.Value;
710 public override void Emit (EmitContext ec)
712 ILGenerator ig = ec.ig;
716 if (Oper == Operator.Is){
717 ig.Emit (OpCodes.Isinst, probe_type);
718 ig.Emit (OpCodes.Ldnull);
719 ig.Emit (OpCodes.Cgt_Un);
721 ig.Emit (OpCodes.Isinst, probe_type);
727 // This represents a typecast in the source language.
729 // FIXME: Cast expressions have an unusual set of parsing
730 // rules, we need to figure those out.
732 public class Cast : Expression {
737 public Cast (string cast_type, Expression expr, Location loc)
739 this.target_type = cast_type;
744 public string TargetType {
750 public Expression Expr {
759 public override Expression DoResolve (EmitContext ec)
761 expr = expr.Resolve (ec);
765 type = ec.TypeContainer.LookupType (target_type, false);
766 eclass = ExprClass.Value;
771 expr = ConvertExplicit (ec, expr, type, loc);
776 public override void Emit (EmitContext ec)
779 // This one will never happen
781 throw new Exception ("Should not happen");
785 public class Binary : Expression {
786 public enum Operator : byte {
787 Multiply, Division, Modulus,
788 Addition, Subtraction,
789 LeftShift, RightShift,
790 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
791 Equality, Inequality,
800 Expression left, right;
806 public Binary (Operator oper, Expression left, Expression right, Location loc)
814 public Operator Oper {
823 public Expression Left {
832 public Expression Right {
843 // Returns a stringified representation of the Operator
848 case Operator.Multiply:
850 case Operator.Division:
852 case Operator.Modulus:
854 case Operator.Addition:
856 case Operator.Subtraction:
858 case Operator.LeftShift:
860 case Operator.RightShift:
862 case Operator.LessThan:
864 case Operator.GreaterThan:
866 case Operator.LessThanOrEqual:
868 case Operator.GreaterThanOrEqual:
870 case Operator.Equality:
872 case Operator.Inequality:
874 case Operator.BitwiseAnd:
876 case Operator.BitwiseOr:
878 case Operator.ExclusiveOr:
880 case Operator.LogicalOr:
882 case Operator.LogicalAnd:
886 return oper.ToString ();
889 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
891 if (expr.Type == target_type)
894 return ConvertImplicit (ec, expr, target_type, new Location (-1));
898 // Note that handling the case l == Decimal || r == Decimal
899 // is taken care of by the Step 1 Operator Overload resolution.
901 bool DoNumericPromotions (EmitContext ec, Type l, Type r)
903 if (l == TypeManager.double_type || r == TypeManager.double_type){
905 // If either operand is of type double, the other operand is
906 // conveted to type double.
908 if (r != TypeManager.double_type)
909 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
910 if (l != TypeManager.double_type)
911 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
913 type = TypeManager.double_type;
914 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
916 // if either operand is of type float, th eother operand is
917 // converd to type float.
919 if (r != TypeManager.double_type)
920 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
921 if (l != TypeManager.double_type)
922 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
923 type = TypeManager.float_type;
924 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
928 // If either operand is of type ulong, the other operand is
929 // converted to type ulong. or an error ocurrs if the other
930 // operand is of type sbyte, short, int or long
933 if (l == TypeManager.uint64_type){
934 if (r != TypeManager.uint64_type && right is IntLiteral){
935 e = TryImplicitIntConversion (l, (IntLiteral) right);
941 if (left is IntLiteral){
942 e = TryImplicitIntConversion (r, (IntLiteral) left);
949 if ((other == TypeManager.sbyte_type) ||
950 (other == TypeManager.short_type) ||
951 (other == TypeManager.int32_type) ||
952 (other == TypeManager.int64_type)){
953 string oper = OperName ();
955 Error (34, loc, "Operator `" + OperName ()
956 + "' is ambiguous on operands of type `"
957 + TypeManager.CSharpName (l) + "' "
958 + "and `" + TypeManager.CSharpName (r)
961 type = TypeManager.uint64_type;
962 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
964 // If either operand is of type long, the other operand is converted
967 if (l != TypeManager.int64_type)
968 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
969 if (r != TypeManager.int64_type)
970 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
972 type = TypeManager.int64_type;
973 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
975 // If either operand is of type uint, and the other
976 // operand is of type sbyte, short or int, othe operands are
977 // converted to type long.
981 if (l == TypeManager.uint32_type)
983 else if (r == TypeManager.uint32_type)
986 if ((other == TypeManager.sbyte_type) ||
987 (other == TypeManager.short_type) ||
988 (other == TypeManager.int32_type)){
989 left = ForceConversion (ec, left, TypeManager.int64_type);
990 right = ForceConversion (ec, right, TypeManager.int64_type);
991 type = TypeManager.int64_type;
994 // if either operand is of type uint, the other
995 // operand is converd to type uint
997 left = ForceConversion (ec, left, TypeManager.uint32_type);
998 right = ForceConversion (ec, right, TypeManager.uint32_type);
999 type = TypeManager.uint32_type;
1001 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1002 if (l != TypeManager.decimal_type)
1003 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
1004 if (r != TypeManager.decimal_type)
1005 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
1007 type = TypeManager.decimal_type;
1009 Expression l_tmp, r_tmp;
1011 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
1015 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
1022 type = TypeManager.int32_type;
1031 "Operator " + OperName () + " cannot be applied to operands of type `" +
1032 TypeManager.CSharpName (left.Type) + "' and `" +
1033 TypeManager.CSharpName (right.Type) + "'");
1037 Expression CheckShiftArguments (EmitContext ec)
1041 Type r = right.Type;
1043 e = ForceConversion (ec, right, TypeManager.int32_type);
1050 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
1051 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
1052 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
1053 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
1063 Expression ResolveOperator (EmitContext ec)
1066 Type r = right.Type;
1069 // Step 1: Perform Operator Overload location
1071 Expression left_expr, right_expr;
1073 string op = "op_" + oper;
1075 left_expr = MemberLookup (ec, l, op, false, loc);
1076 if (left_expr == null && l.BaseType != null)
1077 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
1079 right_expr = MemberLookup (ec, r, op, false, loc);
1080 if (right_expr == null && r.BaseType != null)
1081 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
1083 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1085 if (union != null) {
1086 Arguments = new ArrayList ();
1087 Arguments.Add (new Argument (left, Argument.AType.Expression));
1088 Arguments.Add (new Argument (right, Argument.AType.Expression));
1090 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
1091 if (method != null) {
1092 MethodInfo mi = (MethodInfo) method;
1093 type = mi.ReturnType;
1102 // Step 2: Default operations on CLI native types.
1105 // Only perform numeric promotions on:
1106 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1108 if (oper == Operator.Addition){
1110 // If any of the arguments is a string, cast to string
1112 if (l == TypeManager.string_type){
1113 if (r == TypeManager.string_type){
1114 if (left is Literal && right is Literal){
1115 StringLiteral ls = (StringLiteral) left;
1116 StringLiteral rs = (StringLiteral) right;
1118 return new StringLiteral (ls.Value + rs.Value);
1122 method = TypeManager.string_concat_string_string;
1125 method = TypeManager.string_concat_object_object;
1126 right = ConvertImplicit (ec, right,
1127 TypeManager.object_type, loc);
1129 type = TypeManager.string_type;
1131 Arguments = new ArrayList ();
1132 Arguments.Add (new Argument (left, Argument.AType.Expression));
1133 Arguments.Add (new Argument (right, Argument.AType.Expression));
1137 } else if (r == TypeManager.string_type){
1139 method = TypeManager.string_concat_object_object;
1140 Arguments = new ArrayList ();
1141 Arguments.Add (new Argument (left, Argument.AType.Expression));
1142 Arguments.Add (new Argument (right, Argument.AType.Expression));
1144 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1145 type = TypeManager.string_type;
1151 // FIXME: is Delegate operator + (D x, D y) handled?
1155 if (oper == Operator.LeftShift || oper == Operator.RightShift)
1156 return CheckShiftArguments (ec);
1158 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1159 if (l != TypeManager.bool_type || r != TypeManager.bool_type){
1164 type = TypeManager.bool_type;
1168 if (oper == Operator.Equality || oper == Operator.Inequality){
1169 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
1170 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
1175 type = TypeManager.bool_type;
1182 // We are dealing with numbers
1185 if (!DoNumericPromotions (ec, l, r)){
1188 // operator != (object a, object b)
1189 // operator == (object a, object b)
1192 if (oper == Operator.Equality || oper == Operator.Inequality){
1194 li = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1196 ri = ConvertImplicit (ec, right, TypeManager.object_type,
1202 type = TypeManager.bool_type;
1212 if (left == null || right == null)
1216 if (oper == Operator.BitwiseAnd ||
1217 oper == Operator.BitwiseOr ||
1218 oper == Operator.ExclusiveOr){
1219 if (!((l == TypeManager.int32_type) ||
1220 (l == TypeManager.uint32_type) ||
1221 (l == TypeManager.int64_type) ||
1222 (l == TypeManager.uint64_type))){
1229 if (oper == Operator.Equality ||
1230 oper == Operator.Inequality ||
1231 oper == Operator.LessThanOrEqual ||
1232 oper == Operator.LessThan ||
1233 oper == Operator.GreaterThanOrEqual ||
1234 oper == Operator.GreaterThan){
1235 type = TypeManager.bool_type;
1241 public override Expression DoResolve (EmitContext ec)
1243 left = left.Resolve (ec);
1244 right = right.Resolve (ec);
1246 if (left == null || right == null)
1249 if (left.Type == null)
1250 throw new Exception (
1251 "Resolve returned non null, but did not set the type! (" +
1252 left + ") at Line: " + loc.Row);
1253 if (right.Type == null)
1254 throw new Exception (
1255 "Resolve returned non null, but did not set the type! (" +
1256 right + ") at Line: "+ loc.Row);
1258 eclass = ExprClass.Value;
1260 return ResolveOperator (ec);
1263 public bool IsBranchable ()
1265 if (oper == Operator.Equality ||
1266 oper == Operator.Inequality ||
1267 oper == Operator.LessThan ||
1268 oper == Operator.GreaterThan ||
1269 oper == Operator.LessThanOrEqual ||
1270 oper == Operator.GreaterThanOrEqual){
1277 // This entry point is used by routines that might want
1278 // to emit a brfalse/brtrue after an expression, and instead
1279 // they could use a more compact notation.
1281 // Typically the code would generate l.emit/r.emit, followed
1282 // by the comparission and then a brtrue/brfalse. The comparissions
1283 // are sometimes inneficient (there are not as complete as the branches
1284 // look for the hacks in Emit using double ceqs).
1286 // So for those cases we provide EmitBranchable that can emit the
1287 // branch with the test
1289 public void EmitBranchable (EmitContext ec, int target)
1292 bool close_target = false;
1298 case Operator.Equality:
1300 opcode = OpCodes.Beq_S;
1302 opcode = OpCodes.Beq;
1305 case Operator.Inequality:
1307 opcode = OpCodes.Bne_Un_S;
1309 opcode = OpCodes.Bne_Un;
1312 case Operator.LessThan:
1314 opcode = OpCodes.Blt_S;
1316 opcode = OpCodes.Blt;
1319 case Operator.GreaterThan:
1321 opcode = OpCodes.Bgt_S;
1323 opcode = OpCodes.Bgt;
1326 case Operator.LessThanOrEqual:
1328 opcode = OpCodes.Ble_S;
1330 opcode = OpCodes.Ble;
1333 case Operator.GreaterThanOrEqual:
1335 opcode = OpCodes.Bge_S;
1337 opcode = OpCodes.Ble;
1341 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1342 + oper.ToString ());
1345 ec.ig.Emit (opcode, target);
1348 public override void Emit (EmitContext ec)
1350 ILGenerator ig = ec.ig;
1352 Type r = right.Type;
1355 if (method != null) {
1357 // Note that operators are static anyway
1359 if (Arguments != null)
1360 Invocation.EmitArguments (ec, method, Arguments);
1362 if (method is MethodInfo)
1363 ig.Emit (OpCodes.Call, (MethodInfo) method);
1365 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1374 case Operator.Multiply:
1376 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1377 opcode = OpCodes.Mul_Ovf;
1378 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1379 opcode = OpCodes.Mul_Ovf_Un;
1381 opcode = OpCodes.Mul;
1383 opcode = OpCodes.Mul;
1387 case Operator.Division:
1388 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1389 opcode = OpCodes.Div_Un;
1391 opcode = OpCodes.Div;
1394 case Operator.Modulus:
1395 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1396 opcode = OpCodes.Rem_Un;
1398 opcode = OpCodes.Rem;
1401 case Operator.Addition:
1403 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1404 opcode = OpCodes.Add_Ovf;
1405 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1406 opcode = OpCodes.Add_Ovf_Un;
1408 opcode = OpCodes.Mul;
1410 opcode = OpCodes.Add;
1413 case Operator.Subtraction:
1415 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1416 opcode = OpCodes.Sub_Ovf;
1417 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1418 opcode = OpCodes.Sub_Ovf_Un;
1420 opcode = OpCodes.Sub;
1422 opcode = OpCodes.Sub;
1425 case Operator.RightShift:
1426 opcode = OpCodes.Shr;
1429 case Operator.LeftShift:
1430 opcode = OpCodes.Shl;
1433 case Operator.Equality:
1434 opcode = OpCodes.Ceq;
1437 case Operator.Inequality:
1438 ec.ig.Emit (OpCodes.Ceq);
1439 ec.ig.Emit (OpCodes.Ldc_I4_0);
1441 opcode = OpCodes.Ceq;
1444 case Operator.LessThan:
1445 opcode = OpCodes.Clt;
1448 case Operator.GreaterThan:
1449 opcode = OpCodes.Cgt;
1452 case Operator.LessThanOrEqual:
1453 ec.ig.Emit (OpCodes.Cgt);
1454 ec.ig.Emit (OpCodes.Ldc_I4_0);
1456 opcode = OpCodes.Ceq;
1459 case Operator.GreaterThanOrEqual:
1460 ec.ig.Emit (OpCodes.Clt);
1461 ec.ig.Emit (OpCodes.Ldc_I4_1);
1463 opcode = OpCodes.Sub;
1466 case Operator.LogicalOr:
1467 case Operator.BitwiseOr:
1468 opcode = OpCodes.Or;
1471 case Operator.LogicalAnd:
1472 case Operator.BitwiseAnd:
1473 opcode = OpCodes.And;
1476 case Operator.ExclusiveOr:
1477 opcode = OpCodes.Xor;
1481 throw new Exception ("This should not happen: Operator = "
1482 + oper.ToString ());
1489 // Constant expression reducer for binary operations
1491 public override Expression Reduce (EmitContext ec)
1494 left = left.Reduce (ec);
1495 right = right.Reduce (ec);
1497 if (!(left is Literal && right is Literal))
1500 if (method == TypeManager.string_concat_string_string){
1501 StringLiteral ls = (StringLiteral) left;
1502 StringLiteral rs = (StringLiteral) right;
1504 return new StringLiteral (ls.Value + rs.Value);
1513 public class Conditional : Expression {
1514 Expression expr, trueExpr, falseExpr;
1517 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
1520 this.trueExpr = trueExpr;
1521 this.falseExpr = falseExpr;
1525 public Expression Expr {
1531 public Expression TrueExpr {
1537 public Expression FalseExpr {
1543 public override Expression DoResolve (EmitContext ec)
1545 expr = expr.Resolve (ec);
1547 if (expr.Type != TypeManager.bool_type)
1548 expr = Expression.ConvertImplicitRequired (
1549 ec, expr, TypeManager.bool_type, loc);
1551 trueExpr = trueExpr.Resolve (ec);
1552 falseExpr = falseExpr.Resolve (ec);
1554 if (expr == null || trueExpr == null || falseExpr == null)
1557 if (trueExpr.Type == falseExpr.Type)
1558 type = trueExpr.Type;
1563 // First, if an implicit conversion exists from trueExpr
1564 // to falseExpr, then the result type is of type falseExpr.Type
1566 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
1568 type = falseExpr.Type;
1570 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
1571 type = trueExpr.Type;
1574 Error (173, loc, "The type of the conditional expression can " +
1575 "not be computed because there is no implicit conversion" +
1576 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
1577 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
1582 if (expr is BoolLiteral){
1583 BoolLiteral bl = (BoolLiteral) expr;
1591 eclass = ExprClass.Value;
1595 public override void Emit (EmitContext ec)
1597 ILGenerator ig = ec.ig;
1598 Label false_target = ig.DefineLabel ();
1599 Label end_target = ig.DefineLabel ();
1602 ig.Emit (OpCodes.Brfalse, false_target);
1604 ig.Emit (OpCodes.Br, end_target);
1605 ig.MarkLabel (false_target);
1606 falseExpr.Emit (ec);
1607 ig.MarkLabel (end_target);
1610 public override Expression Reduce (EmitContext ec)
1612 expr = expr.Reduce (ec);
1613 trueExpr = trueExpr.Reduce (ec);
1614 falseExpr = falseExpr.Reduce (ec);
1616 if (!(expr is Literal && trueExpr is Literal && falseExpr is Literal))
1619 BoolLiteral bl = (BoolLiteral) expr;
1628 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
1629 public readonly string Name;
1630 public readonly Block Block;
1632 VariableInfo variable_info;
1634 public LocalVariableReference (Block block, string name)
1638 eclass = ExprClass.Variable;
1641 public VariableInfo VariableInfo {
1643 if (variable_info == null)
1644 variable_info = Block.GetVariableInfo (Name);
1645 return variable_info;
1649 public override Expression DoResolve (EmitContext ec)
1651 VariableInfo vi = VariableInfo;
1653 type = vi.VariableType;
1657 public override void Emit (EmitContext ec)
1659 VariableInfo vi = VariableInfo;
1660 ILGenerator ig = ec.ig;
1667 ig.Emit (OpCodes.Ldloc_0);
1671 ig.Emit (OpCodes.Ldloc_1);
1675 ig.Emit (OpCodes.Ldloc_2);
1679 ig.Emit (OpCodes.Ldloc_3);
1684 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
1686 ig.Emit (OpCodes.Ldloc, idx);
1691 public static void Store (ILGenerator ig, int idx)
1695 ig.Emit (OpCodes.Stloc_0);
1699 ig.Emit (OpCodes.Stloc_1);
1703 ig.Emit (OpCodes.Stloc_2);
1707 ig.Emit (OpCodes.Stloc_3);
1712 ig.Emit (OpCodes.Stloc_S, (byte) idx);
1714 ig.Emit (OpCodes.Stloc, idx);
1719 public void EmitAssign (EmitContext ec, Expression source)
1721 ILGenerator ig = ec.ig;
1722 VariableInfo vi = VariableInfo;
1728 // Funny seems the code below generates optimal code for us, but
1729 // seems to take too long to generate what we need.
1730 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
1735 public void AddressOf (EmitContext ec)
1737 VariableInfo vi = VariableInfo;
1744 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
1746 ec.ig.Emit (OpCodes.Ldloca, idx);
1750 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
1751 public readonly Parameters Pars;
1752 public readonly String Name;
1753 public readonly int Idx;
1756 public ParameterReference (Parameters pars, int idx, string name)
1761 eclass = ExprClass.Variable;
1764 public override Expression DoResolve (EmitContext ec)
1766 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
1777 public override void Emit (EmitContext ec)
1780 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
1782 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
1785 public void EmitAssign (EmitContext ec, Expression source)
1790 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
1792 ec.ig.Emit (OpCodes.Starg, arg_idx);
1796 public void AddressOf (EmitContext ec)
1799 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
1801 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
1806 // Used for arguments to New(), Invocation()
1808 public class Argument {
1809 public enum AType : byte {
1815 public readonly AType ArgType;
1816 public Expression expr;
1818 public Argument (Expression expr, AType type)
1821 this.ArgType = type;
1824 public Expression Expr {
1840 public Parameter.Modifier GetParameterModifier ()
1842 if (ArgType == AType.Ref)
1843 return Parameter.Modifier.REF;
1845 if (ArgType == AType.Out)
1846 return Parameter.Modifier.OUT;
1848 return Parameter.Modifier.NONE;
1851 public static string FullDesc (Argument a)
1853 return (a.ArgType == AType.Ref ? "ref " :
1854 (a.ArgType == AType.Out ? "out " : "")) +
1855 TypeManager.CSharpName (a.Expr.Type);
1858 public bool Resolve (EmitContext ec, Location loc)
1860 expr = expr.Resolve (ec);
1862 if (ArgType == AType.Expression)
1863 return expr != null;
1865 if (expr.ExprClass != ExprClass.Variable){
1866 Report.Error (206, loc,
1867 "A property or indexer can not be passed as an out or ref " +
1872 return expr != null;
1875 public void Emit (EmitContext ec)
1877 if (ArgType == AType.Ref || ArgType == AType.Out)
1878 ((IMemoryLocation)expr).AddressOf (ec);
1885 // Invocation of methods or delegates.
1887 public class Invocation : ExpressionStatement {
1888 public readonly ArrayList Arguments;
1892 MethodBase method = null;
1894 static Hashtable method_parameter_cache;
1896 static Invocation ()
1898 method_parameter_cache = new Hashtable ();
1902 // arguments is an ArrayList, but we do not want to typecast,
1903 // as it might be null.
1905 // FIXME: only allow expr to be a method invocation or a
1906 // delegate invocation (7.5.5)
1908 public Invocation (Expression expr, ArrayList arguments, Location l)
1911 Arguments = arguments;
1915 public Expression Expr {
1922 // Returns the Parameters (a ParameterData interface) for the
1925 public static ParameterData GetParameterData (MethodBase mb)
1927 object pd = method_parameter_cache [mb];
1931 return (ParameterData) pd;
1934 ip = TypeContainer.LookupParametersByBuilder (mb);
1936 method_parameter_cache [mb] = ip;
1938 return (ParameterData) ip;
1940 ParameterInfo [] pi = mb.GetParameters ();
1941 ReflectionParameters rp = new ReflectionParameters (pi);
1942 method_parameter_cache [mb] = rp;
1944 return (ParameterData) rp;
1949 // Tells whether a user defined conversion from Type `from' to
1950 // Type `to' exists.
1952 // FIXME: we could implement a cache here.
1954 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
1956 // Locate user-defined implicit operators
1960 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
1963 MethodGroupExpr me = (MethodGroupExpr) mg;
1965 for (int i = me.Methods.Length; i > 0;) {
1967 MethodBase mb = me.Methods [i];
1968 ParameterData pd = GetParameterData (mb);
1970 if (from == pd.ParameterType (0))
1975 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
1978 MethodGroupExpr me = (MethodGroupExpr) mg;
1980 for (int i = me.Methods.Length; i > 0;) {
1982 MethodBase mb = me.Methods [i];
1983 MethodInfo mi = (MethodInfo) mb;
1985 if (mi.ReturnType == to)
1994 // Determines "better conversion" as specified in 7.4.2.3
1995 // Returns : 1 if a->p is better
1996 // 0 if a->q or neither is better
1998 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
2001 Type argument_type = a.Type;
2002 Expression argument_expr = a.Expr;
2004 if (argument_type == null)
2005 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2010 if (argument_type == p)
2013 if (argument_type == q)
2017 // Now probe whether an implicit constant expression conversion
2020 // An implicit constant expression conversion permits the following
2023 // * A constant-expression of type `int' can be converted to type
2024 // sbyte, byute, short, ushort, uint, ulong provided the value of
2025 // of the expression is withing the range of the destination type.
2027 // * A constant-expression of type long can be converted to type
2028 // ulong, provided the value of the constant expression is not negative
2030 // FIXME: Note that this assumes that constant folding has
2031 // taken place. We dont do constant folding yet.
2034 if (argument_expr is IntLiteral){
2035 IntLiteral ei = (IntLiteral) argument_expr;
2036 int value = ei.Value;
2038 if (p == TypeManager.sbyte_type){
2039 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2041 } else if (p == TypeManager.byte_type){
2042 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2044 } else if (p == TypeManager.short_type){
2045 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2047 } else if (p == TypeManager.ushort_type){
2048 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2050 } else if (p == TypeManager.uint32_type){
2052 // we can optimize this case: a positive int32
2053 // always fits on a uint32
2057 } else if (p == TypeManager.uint64_type){
2059 // we can optimize this case: a positive int32
2060 // always fits on a uint64
2065 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2066 LongLiteral ll = (LongLiteral) argument_expr;
2068 if (p == TypeManager.uint64_type){
2079 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
2081 tmp = ConvertImplicit (ec, argument_expr, p, loc);
2090 if (ConversionExists (ec, p, q, loc) == true &&
2091 ConversionExists (ec, q, p, loc) == false)
2094 if (p == TypeManager.sbyte_type)
2095 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2096 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2099 if (p == TypeManager.short_type)
2100 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2101 q == TypeManager.uint64_type)
2104 if (p == TypeManager.int32_type)
2105 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2108 if (p == TypeManager.int64_type)
2109 if (q == TypeManager.uint64_type)
2116 // Determines "Better function" and returns an integer indicating :
2117 // 0 if candidate ain't better
2118 // 1 if candidate is better than the current best match
2120 static int BetterFunction (EmitContext ec, ArrayList args,
2121 MethodBase candidate, MethodBase best,
2122 bool use_standard, Location loc)
2124 ParameterData candidate_pd = GetParameterData (candidate);
2125 ParameterData best_pd;
2131 argument_count = args.Count;
2133 if (candidate_pd.Count == 0 && argument_count == 0)
2137 if (candidate_pd.Count == argument_count) {
2139 for (int j = argument_count; j > 0;) {
2142 Argument a = (Argument) args [j];
2144 x = BetterConversion (
2145 ec, a, candidate_pd.ParameterType (j), null,
2161 best_pd = GetParameterData (best);
2163 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2164 int rating1 = 0, rating2 = 0;
2166 for (int j = argument_count; j > 0;) {
2170 Argument a = (Argument) args [j];
2172 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
2173 best_pd.ParameterType (j), use_standard, loc);
2174 y = BetterConversion (ec, a, best_pd.ParameterType (j),
2175 candidate_pd.ParameterType (j), use_standard,
2182 if (rating1 > rating2)
2191 public static string FullMethodDesc (MethodBase mb)
2193 StringBuilder sb = new StringBuilder (mb.Name);
2194 ParameterData pd = GetParameterData (mb);
2196 int count = pd.Count;
2199 for (int i = count; i > 0; ) {
2202 sb.Append (pd.ParameterDesc (count - i - 1));
2208 return sb.ToString ();
2211 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2213 MemberInfo [] miset;
2214 MethodGroupExpr union;
2216 if (mg1 != null && mg2 != null) {
2218 MethodGroupExpr left_set = null, right_set = null;
2219 int length1 = 0, length2 = 0;
2221 left_set = (MethodGroupExpr) mg1;
2222 length1 = left_set.Methods.Length;
2224 right_set = (MethodGroupExpr) mg2;
2225 length2 = right_set.Methods.Length;
2227 ArrayList common = new ArrayList ();
2229 for (int i = 0; i < left_set.Methods.Length; i++) {
2230 for (int j = 0; j < right_set.Methods.Length; j++) {
2231 if (left_set.Methods [i] == right_set.Methods [j])
2232 common.Add (left_set.Methods [i]);
2236 miset = new MemberInfo [length1 + length2 - common.Count];
2238 left_set.Methods.CopyTo (miset, 0);
2242 for (int j = 0; j < right_set.Methods.Length; j++)
2243 if (!common.Contains (right_set.Methods [j]))
2244 miset [length1 + k++] = right_set.Methods [j];
2246 union = new MethodGroupExpr (miset);
2250 } else if (mg1 == null && mg2 != null) {
2252 MethodGroupExpr me = (MethodGroupExpr) mg2;
2254 miset = new MemberInfo [me.Methods.Length];
2255 me.Methods.CopyTo (miset, 0);
2257 union = new MethodGroupExpr (miset);
2261 } else if (mg2 == null && mg1 != null) {
2263 MethodGroupExpr me = (MethodGroupExpr) mg1;
2265 miset = new MemberInfo [me.Methods.Length];
2266 me.Methods.CopyTo (miset, 0);
2268 union = new MethodGroupExpr (miset);
2277 // Determines is the candidate method, if a params method, is applicable
2278 // in its expanded form to the given set of arguments
2280 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2284 if (arguments == null)
2287 arg_count = arguments.Count;
2289 ParameterData pd = GetParameterData (candidate);
2291 int pd_count = pd.Count;
2293 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2296 if (pd_count - 1 > arg_count)
2299 // If we have come this far, the case which remains is when the number of parameters
2300 // is less than or equal to the argument count. So, we now check if the element type
2301 // of the params array is compatible with each argument type
2304 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2306 for (int i = pd_count - 1; i < arg_count - 1; i++) {
2307 Argument a = (Argument) arguments [i];
2308 if (!StandardConversionExists (a.Type, element_type))
2316 // Determines if the candidate method is applicable (section 14.4.2.1)
2317 // to the given set of arguments
2319 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2323 if (arguments == null)
2326 arg_count = arguments.Count;
2328 ParameterData pd = GetParameterData (candidate);
2330 int pd_count = pd.Count;
2332 if (arg_count != pd.Count)
2335 for (int i = arg_count; i > 0; ) {
2338 Argument a = (Argument) arguments [i];
2340 Parameter.Modifier a_mod = a.GetParameterModifier ();
2341 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2343 if (a_mod == p_mod) {
2345 if (a_mod == Parameter.Modifier.NONE)
2346 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2349 if (a_mod == Parameter.Modifier.REF ||
2350 a_mod == Parameter.Modifier.OUT)
2351 if (pd.ParameterType (i) != a.Type)
2363 // Find the Applicable Function Members (7.4.2.1)
2365 // me: Method Group expression with the members to select.
2366 // it might contain constructors or methods (or anything
2367 // that maps to a method).
2369 // Arguments: ArrayList containing resolved Argument objects.
2371 // loc: The location if we want an error to be reported, or a Null
2372 // location for "probing" purposes.
2374 // use_standard: controls whether OverloadResolve should use the
2375 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
2377 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2378 // that is the best match of me on Arguments.
2381 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2382 ArrayList Arguments, Location loc,
2385 ArrayList afm = new ArrayList ();
2386 int best_match_idx = -1;
2387 MethodBase method = null;
2390 for (int i = me.Methods.Length; i > 0; ){
2392 MethodBase candidate = me.Methods [i];
2395 // Check if candidate is applicable (section 14.4.2.1)
2396 if (!IsApplicable (Arguments, candidate))
2399 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
2405 method = me.Methods [best_match_idx];
2409 if (Arguments == null)
2412 argument_count = Arguments.Count;
2415 // Now we see if we can find params functions, applicable in their expanded form
2416 // since if they were applicable in their normal form, they would have been selected
2419 if (best_match_idx == -1) {
2421 for (int i = me.Methods.Length; i > 0; ) {
2423 MethodBase candidate = me.Methods [i];
2425 if (IsParamsMethodApplicable (Arguments, candidate)) {
2427 method = me.Methods [best_match_idx];
2434 // Now we see if we can at least find a method with the same number of arguments
2438 if (best_match_idx == -1) {
2440 for (int i = me.Methods.Length; i > 0;) {
2442 MethodBase mb = me.Methods [i];
2443 pd = GetParameterData (mb);
2445 if (pd.Count == argument_count) {
2447 method = me.Methods [best_match_idx];
2457 // And now convert implicitly, each argument to the required type
2459 pd = GetParameterData (method);
2460 int pd_count = pd.Count;
2462 for (int j = 0; j < argument_count; j++) {
2464 Argument a = (Argument) Arguments [j];
2465 Expression a_expr = a.Expr;
2466 Type parameter_type = pd.ParameterType (j);
2469 // Note that we need to compare against the element type
2470 // when we have a params method
2472 if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
2473 if (j >= pd_count - 1)
2474 parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
2477 if (a.Type != parameter_type){
2481 conv = ConvertImplicitStandard (ec, a_expr, parameter_type, Location.Null);
2483 conv = ConvertImplicit (ec, a_expr, parameter_type, Location.Null);
2486 if (!Location.IsNull (loc)) {
2488 "The best overloaded match for method '" + FullMethodDesc (method)+
2489 "' has some invalid arguments");
2491 "Argument " + (j+1) +
2492 ": Cannot convert from '" + Argument.FullDesc (a)
2493 + "' to '" + pd.ParameterDesc (j) + "'");
2501 // Update the argument with the implicit conversion
2506 // FIXME : For the case of params methods, we need to actually instantiate
2507 // an array and initialize it with the argument values etc etc.
2511 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2512 pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2513 if (!Location.IsNull (loc)) {
2515 "The best overloaded match for method '" + FullMethodDesc (method)+
2516 "' has some invalid arguments");
2518 "Argument " + (j+1) +
2519 ": Cannot convert from '" + Argument.FullDesc (a)
2520 + "' to '" + pd.ParameterDesc (j) + "'");
2531 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2532 ArrayList Arguments, Location loc)
2534 return OverloadResolve (ec, me, Arguments, loc, false);
2537 public override Expression DoResolve (EmitContext ec)
2540 // First, resolve the expression that is used to
2541 // trigger the invocation
2543 expr = expr.Resolve (ec);
2547 if (!(expr is MethodGroupExpr)) {
2548 Type expr_type = expr.Type;
2550 if (expr_type != null){
2551 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2553 return (new DelegateInvocation (
2554 this.expr, Arguments, loc)).Resolve (ec);
2558 if (!(expr is MethodGroupExpr)){
2559 report118 (loc, this.expr, "method group");
2564 // Next, evaluate all the expressions in the argument list
2566 if (Arguments != null){
2567 for (int i = Arguments.Count; i > 0;){
2569 Argument a = (Argument) Arguments [i];
2571 if (!a.Resolve (ec, loc))
2576 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
2578 if (method == null){
2580 "Could not find any applicable function for this argument list");
2584 if (method is MethodInfo)
2585 type = ((MethodInfo)method).ReturnType;
2587 eclass = ExprClass.Value;
2592 // Emits the list of arguments as an array
2594 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
2596 ILGenerator ig = ec.ig;
2597 int count = arguments.Count - idx;
2598 Argument a = (Argument) arguments [idx];
2599 Type t = a.expr.Type;
2600 string array_type = t.FullName + "[]";
2603 array = ig.DeclareLocal (Type.GetType (array_type));
2604 IntLiteral.EmitInt (ig, count);
2605 ig.Emit (OpCodes.Newarr, t);
2606 ig.Emit (OpCodes.Stloc, array);
2608 int top = arguments.Count;
2609 for (int j = idx; j < top; j++){
2610 a = (Argument) arguments [j];
2612 ig.Emit (OpCodes.Ldloc, array);
2613 IntLiteral.EmitInt (ig, j - idx);
2616 ArrayAccess.EmitStoreOpcode (ig, t);
2618 ig.Emit (OpCodes.Ldloc, array);
2622 // Emits a list of resolved Arguments that are in the arguments
2625 // The MethodBase argument might be null if the
2626 // emission of the arguments is known not to contain
2627 // a `params' field (for example in constructors or other routines
2628 // that keep their arguments in this structure
2631 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
2633 ParameterData pd = null;
2636 if (arguments != null)
2637 top = arguments.Count;
2642 pd = GetParameterData (mb);
2644 for (int i = 0; i < top; i++){
2645 Argument a = (Argument) arguments [i];
2648 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
2649 EmitParams (ec, i, arguments);
2658 public static void EmitCall (EmitContext ec,
2659 bool is_static, Expression instance_expr,
2660 MethodBase method, ArrayList Arguments)
2662 ILGenerator ig = ec.ig;
2663 bool struct_call = false;
2667 // If this is ourselves, push "this"
2669 if (instance_expr == null){
2670 ig.Emit (OpCodes.Ldarg_0);
2673 // Push the instance expression
2675 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
2680 // If the expression implements IMemoryLocation, then
2681 // we can optimize and use AddressOf on the
2684 // If not we have to use some temporary storage for
2686 if (instance_expr is IMemoryLocation)
2687 ((IMemoryLocation) instance_expr).AddressOf (ec);
2689 Type t = instance_expr.Type;
2691 instance_expr.Emit (ec);
2692 LocalBuilder temp = ig.DeclareLocal (t);
2693 ig.Emit (OpCodes.Stloc, temp);
2694 ig.Emit (OpCodes.Ldloca, temp);
2697 instance_expr.Emit (ec);
2701 if (Arguments != null)
2702 EmitArguments (ec, method, Arguments);
2704 if (is_static || struct_call){
2705 if (method is MethodInfo)
2706 ig.Emit (OpCodes.Call, (MethodInfo) method);
2708 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2710 if (method is MethodInfo)
2711 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
2713 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
2717 public override void Emit (EmitContext ec)
2719 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2720 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
2723 public override void EmitStatement (EmitContext ec)
2728 // Pop the return value if there is one
2730 if (method is MethodInfo){
2731 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
2732 ec.ig.Emit (OpCodes.Pop);
2737 public class New : ExpressionStatement {
2738 public readonly ArrayList Arguments;
2739 public readonly string RequestedType;
2742 MethodBase method = null;
2745 // If set, the new expression is for a value_target, and
2746 // we will not leave anything on the stack.
2748 Expression value_target;
2750 public New (string requested_type, ArrayList arguments, Location l)
2752 RequestedType = requested_type;
2753 Arguments = arguments;
2757 public Expression ValueTypeVariable {
2759 return value_target;
2763 value_target = value;
2767 public override Expression DoResolve (EmitContext ec)
2769 type = ec.TypeContainer.LookupType (RequestedType, false);
2774 bool IsDelegate = TypeManager.IsDelegateType (type);
2777 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
2781 ml = MemberLookup (ec, type, ".ctor", false,
2782 MemberTypes.Constructor, AllBindingsFlags, loc);
2784 bool is_struct = false;
2785 is_struct = type.IsSubclassOf (TypeManager.value_type);
2787 if (! (ml is MethodGroupExpr)){
2789 report118 (loc, ml, "method group");
2795 if (Arguments != null){
2796 for (int i = Arguments.Count; i > 0;){
2798 Argument a = (Argument) Arguments [i];
2800 if (!a.Resolve (ec, loc))
2805 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
2809 if (method == null && !is_struct) {
2811 "New invocation: Can not find a constructor for " +
2812 "this argument list");
2816 eclass = ExprClass.Value;
2821 // This DoEmit can be invoked in two contexts:
2822 // * As a mechanism that will leave a value on the stack (new object)
2823 // * As one that wont (init struct)
2825 // You can control whether a value is required on the stack by passing
2826 // need_value_on_stack. The code *might* leave a value on the stack
2827 // so it must be popped manually
2829 // Returns whether a value is left on the stack
2831 bool DoEmit (EmitContext ec, bool need_value_on_stack)
2833 if (method == null){
2834 IMemoryLocation ml = (IMemoryLocation) value_target;
2838 Invocation.EmitArguments (ec, method, Arguments);
2839 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2844 // It must be a value type, sanity check
2846 if (value_target != null){
2847 ec.ig.Emit (OpCodes.Initobj, type);
2849 if (need_value_on_stack){
2850 value_target.Emit (ec);
2856 throw new Exception ("No method and no value type");
2859 public override void Emit (EmitContext ec)
2864 public override void EmitStatement (EmitContext ec)
2866 if (DoEmit (ec, false))
2867 ec.ig.Emit (OpCodes.Pop);
2872 // Represents an array creation expression.
2876 // There are two possible scenarios here: one is an array creation
2877 // expression that specifies the dimensions and optionally the
2878 // initialization data
2880 public class ArrayCreation : ExpressionStatement {
2882 string RequestedType;
2884 ArrayList Initializers;
2886 ArrayList Arguments;
2888 MethodBase method = null;
2889 Type array_element_type;
2890 bool IsOneDimensional = false;
2892 bool IsBuiltinType = false;
2896 public ArrayCreation (string requested_type, ArrayList exprs,
2897 string rank, ArrayList initializers, Location l)
2899 RequestedType = requested_type;
2901 Initializers = initializers;
2904 Arguments = new ArrayList ();
2906 foreach (Expression e in exprs)
2907 Arguments.Add (new Argument (e, Argument.AType.Expression));
2911 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2913 RequestedType = requested_type;
2914 Initializers = initializers;
2917 Rank = rank.Substring (0, rank.LastIndexOf ("["));
2919 string tmp = rank.Substring (rank.LastIndexOf ("["));
2921 dimensions = tmp.Length - 1;
2924 public static string FormArrayType (string base_type, int idx_count, string rank)
2926 StringBuilder sb = new StringBuilder (base_type);
2931 for (int i = 1; i < idx_count; i++)
2935 return sb.ToString ();
2938 public static string FormElementType (string base_type, int idx_count, string rank)
2940 StringBuilder sb = new StringBuilder (base_type);
2943 for (int i = 1; i < idx_count; i++)
2949 string val = sb.ToString ();
2951 return val.Substring (0, val.LastIndexOf ("["));
2956 Report.Error (178, loc, "Incorrectly structured array initializer");
2959 bool ValidateInitializers (EmitContext ec)
2961 if (Initializers == null)
2964 Type underlying_type = ec.TypeContainer.LookupType (RequestedType, false);
2966 ArrayList probe = Initializers;
2968 if (Arguments != null) {
2969 for (int i = 0; i < Arguments.Count; i++) {
2970 Argument a = (Argument) Arguments [i];
2972 Expression e = Expression.Reduce (ec, a.Expr);
2974 if (!(e is Literal)) {
2975 Report.Error (150, loc, "A constant value is expected");
2979 int value = (int) ((Literal) e).GetValue ();
2981 if (probe == null) {
2986 if (value != probe.Count) {
2991 if (probe [0] is ArrayList)
2992 probe = (ArrayList) probe [0];
2994 for (int j = 0; j < probe.Count; ++j) {
2995 Expression tmp = (Expression) probe [j];
2997 tmp = tmp.Resolve (ec);
2999 Expression conv = ConvertImplicitRequired (ec, tmp,
3000 underlying_type, loc);
3012 // Here is where we update dimension info in the case
3013 // that the user skips doing that
3016 Arguments = new ArrayList ();
3018 for (probe = Initializers; probe != null; ) {
3019 Expression e = new IntLiteral (probe.Count);
3021 Arguments.Add (new Argument (e, Argument.AType.Expression));
3023 if (probe [0] is ArrayList)
3024 probe = (ArrayList) probe [0];
3026 for (int j = 0; j < probe.Count; ++j) {
3027 Expression tmp = (Expression) probe [j];
3029 tmp = tmp.Resolve (ec);
3031 Expression conv = ConvertImplicitRequired (ec, tmp,
3032 underlying_type, loc);
3042 if (Arguments.Count != dimensions) {
3051 public override Expression DoResolve (EmitContext ec)
3055 if (!ValidateInitializers (ec))
3058 if (Arguments == null)
3061 arg_count = Arguments.Count;
3063 string array_type = FormArrayType (RequestedType, arg_count, Rank);
3065 string element_type = FormElementType (RequestedType, arg_count, Rank);
3067 type = ec.TypeContainer.LookupType (array_type, false);
3069 array_element_type = ec.TypeContainer.LookupType (element_type, false);
3074 if (arg_count == 1) {
3075 IsOneDimensional = true;
3076 eclass = ExprClass.Value;
3080 IsBuiltinType = TypeManager.IsBuiltinType (type);
3082 if (IsBuiltinType) {
3086 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
3087 AllBindingsFlags, loc);
3089 if (!(ml is MethodGroupExpr)){
3090 report118 (loc, ml, "method group");
3095 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3096 "this argument list");
3100 if (Arguments != null) {
3101 for (int i = arg_count; i > 0;){
3103 Argument a = (Argument) Arguments [i];
3105 if (!a.Resolve (ec, loc))
3110 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3112 if (method == null) {
3113 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3114 "this argument list");
3118 eclass = ExprClass.Value;
3123 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3125 ArrayList args = new ArrayList ();
3126 if (Arguments != null){
3127 for (int i = arg_count; i > 0;){
3129 Argument a = (Argument) Arguments [i];
3131 if (!a.Resolve (ec, loc))
3138 Type [] arg_types = null;
3141 arg_types = new Type [args.Count];
3143 args.CopyTo (arg_types, 0);
3145 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3148 if (method == null) {
3149 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3150 "this argument list");
3154 eclass = ExprClass.Value;
3160 public override void Emit (EmitContext ec)
3162 ILGenerator ig = ec.ig;
3164 if (IsOneDimensional) {
3165 Invocation.EmitArguments (ec, null, Arguments);
3166 ig.Emit (OpCodes.Newarr, array_element_type);
3169 Invocation.EmitArguments (ec, null, Arguments);
3172 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3174 ig.Emit (OpCodes.Newobj, (MethodInfo) method);
3177 if (Initializers != null){
3180 // FIXME: This is just sample data, need to fill with
3182 byte [] a = new byte [4] { 1, 2, 3, 4 };
3184 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
3186 ig.Emit (OpCodes.Dup);
3187 ig.Emit (OpCodes.Ldtoken, fb);
3188 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
3192 public override void EmitStatement (EmitContext ec)
3195 ec.ig.Emit (OpCodes.Pop);
3201 // Represents the `this' construct
3203 public class This : Expression, IAssignMethod, IMemoryLocation {
3206 public This (Location loc)
3211 public override Expression DoResolve (EmitContext ec)
3213 eclass = ExprClass.Variable;
3214 type = ec.TypeContainer.TypeBuilder;
3217 Report.Error (26, loc,
3218 "Keyword this not valid in static code");
3225 public Expression DoResolveLValue (EmitContext ec)
3229 if (ec.TypeContainer is Class){
3230 Report.Error (1604, loc, "Cannot assign to `this'");
3237 public override void Emit (EmitContext ec)
3239 ec.ig.Emit (OpCodes.Ldarg_0);
3242 public void EmitAssign (EmitContext ec, Expression source)
3245 ec.ig.Emit (OpCodes.Starg, 0);
3248 public void AddressOf (EmitContext ec)
3250 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3255 // Implements the typeof operator
3257 public class TypeOf : Expression {
3258 public readonly string QueriedType;
3261 public TypeOf (string queried_type)
3263 QueriedType = queried_type;
3266 public override Expression DoResolve (EmitContext ec)
3268 typearg = ec.TypeContainer.LookupType (QueriedType, false);
3270 if (typearg == null)
3273 type = TypeManager.type_type;
3274 eclass = ExprClass.Type;
3278 public override void Emit (EmitContext ec)
3280 ec.ig.Emit (OpCodes.Ldtoken, typearg);
3281 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3285 public class SizeOf : Expression {
3286 public readonly string QueriedType;
3288 public SizeOf (string queried_type)
3290 this.QueriedType = queried_type;
3293 public override Expression DoResolve (EmitContext ec)
3295 // FIXME: Implement;
3296 throw new Exception ("Unimplemented");
3300 public override void Emit (EmitContext ec)
3302 throw new Exception ("Implement me");
3306 public class MemberAccess : Expression {
3307 public readonly string Identifier;
3309 Expression member_lookup;
3312 public MemberAccess (Expression expr, string id, Location l)
3319 public Expression Expr {
3325 void error176 (Location loc, string name)
3327 Report.Error (176, loc, "Static member `" +
3328 name + "' cannot be accessed " +
3329 "with an instance reference, qualify with a " +
3330 "type name instead");
3333 public override Expression DoResolve (EmitContext ec)
3336 // We are the sole users of ResolveWithSimpleName (ie, the only
3337 // ones that can cope with it
3339 expr = expr.ResolveWithSimpleName (ec);
3344 if (expr is SimpleName){
3345 SimpleName child_expr = (SimpleName) expr;
3347 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3349 return expr.Resolve (ec);
3352 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3354 if (member_lookup == null)
3360 if (member_lookup is MethodGroupExpr){
3361 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3366 if (expr is TypeExpr){
3367 if (!mg.RemoveInstanceMethods ()){
3368 SimpleName.Error120 (loc, mg.Methods [0].Name);
3372 return member_lookup;
3376 // Instance.MethodGroup
3378 if (!mg.RemoveStaticMethods ()){
3379 error176 (loc, mg.Methods [0].Name);
3383 mg.InstanceExpression = expr;
3385 return member_lookup;
3388 if (member_lookup is FieldExpr){
3389 FieldExpr fe = (FieldExpr) member_lookup;
3390 FieldInfo fi = fe.FieldInfo;
3393 Type t = fi.FieldType;
3396 if (fi is FieldBuilder)
3397 o = TypeManager.GetValue ((FieldBuilder) fi);
3399 o = fi.GetValue (fi);
3401 if (t.IsSubclassOf (TypeManager.enum_type)) {
3402 Expression enum_member = MemberLookup (ec, t, "value__", false, loc);
3403 Type underlying_type = enum_member.Type;
3405 Expression e = Literalize (o, underlying_type);
3408 return new EnumLiteral (e, t);
3411 Expression exp = Literalize (o, t);
3417 if (expr is TypeExpr){
3418 if (!fe.FieldInfo.IsStatic){
3419 error176 (loc, fe.FieldInfo.Name);
3422 return member_lookup;
3424 if (fe.FieldInfo.IsStatic){
3425 error176 (loc, fe.FieldInfo.Name);
3428 fe.InstanceExpression = expr;
3434 if (member_lookup is PropertyExpr){
3435 PropertyExpr pe = (PropertyExpr) member_lookup;
3437 if (expr is TypeExpr){
3439 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3445 error176 (loc, pe.PropertyInfo.Name);
3448 pe.InstanceExpression = expr;
3454 Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3455 Environment.Exit (0);
3459 public override void Emit (EmitContext ec)
3461 throw new Exception ("Should not happen I think");
3466 public class CheckedExpr : Expression {
3468 public Expression Expr;
3470 public CheckedExpr (Expression e)
3475 public override Expression DoResolve (EmitContext ec)
3477 Expr = Expr.Resolve (ec);
3482 eclass = Expr.ExprClass;
3487 public override void Emit (EmitContext ec)
3489 bool last_check = ec.CheckState;
3491 ec.CheckState = true;
3493 ec.CheckState = last_check;
3498 public class UnCheckedExpr : Expression {
3500 public Expression Expr;
3502 public UnCheckedExpr (Expression e)
3507 public override Expression DoResolve (EmitContext ec)
3509 Expr = Expr.Resolve (ec);
3514 eclass = Expr.ExprClass;
3519 public override void Emit (EmitContext ec)
3521 bool last_check = ec.CheckState;
3523 ec.CheckState = false;
3525 ec.CheckState = last_check;
3530 public class ElementAccess : Expression {
3531 public ArrayList Arguments;
3532 public Expression Expr;
3533 public Location loc;
3535 public ElementAccess (Expression e, ArrayList e_list, Location l)
3539 Arguments = new ArrayList ();
3540 foreach (Expression tmp in e_list)
3541 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3546 bool CommonResolve (EmitContext ec)
3548 Expr = Expr.Resolve (ec);
3553 if (Arguments == null)
3556 for (int i = Arguments.Count; i > 0;){
3558 Argument a = (Argument) Arguments [i];
3560 if (!a.Resolve (ec, loc))
3567 public override Expression DoResolve (EmitContext ec)
3569 if (!CommonResolve (ec))
3573 // We perform some simple tests, and then to "split" the emit and store
3574 // code we create an instance of a different class, and return that.
3576 // I am experimenting with this pattern.
3578 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3579 return (new ArrayAccess (this)).Resolve (ec);
3581 return (new IndexerAccess (this)).Resolve (ec);
3584 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3586 if (!CommonResolve (ec))
3589 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3590 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3592 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3595 public override void Emit (EmitContext ec)
3597 throw new Exception ("Should never be reached");
3602 // Implements array access
3604 public class ArrayAccess : Expression, IAssignMethod {
3606 // Points to our "data" repository
3610 public ArrayAccess (ElementAccess ea_data)
3613 eclass = ExprClass.Variable;
3616 public override Expression DoResolve (EmitContext ec)
3618 if (ea.Expr.ExprClass != ExprClass.Variable) {
3619 report118 (ea.loc, ea.Expr, "variable");
3623 Type t = ea.Expr.Type;
3625 if (t.GetArrayRank () != ea.Arguments.Count){
3626 Report.Error (22, ea.loc,
3627 "Incorrect number of indexes for array " +
3628 " expected: " + t.GetArrayRank () + " got: " +
3629 ea.Arguments.Count);
3632 type = t.GetElementType ();
3633 eclass = ExprClass.Variable;
3639 // Emits the right opcode to load an object of Type `t'
3640 // from an array of T
3642 static public void EmitLoadOpcode (ILGenerator ig, Type type)
3644 if (type == TypeManager.byte_type)
3645 ig.Emit (OpCodes.Ldelem_I1);
3646 else if (type == TypeManager.sbyte_type)
3647 ig.Emit (OpCodes.Ldelem_U1);
3648 else if (type == TypeManager.short_type)
3649 ig.Emit (OpCodes.Ldelem_I2);
3650 else if (type == TypeManager.ushort_type)
3651 ig.Emit (OpCodes.Ldelem_U2);
3652 else if (type == TypeManager.int32_type)
3653 ig.Emit (OpCodes.Ldelem_I4);
3654 else if (type == TypeManager.uint32_type)
3655 ig.Emit (OpCodes.Ldelem_U4);
3656 else if (type == TypeManager.uint64_type)
3657 ig.Emit (OpCodes.Ldelem_I8);
3658 else if (type == TypeManager.int64_type)
3659 ig.Emit (OpCodes.Ldelem_I8);
3660 else if (type == TypeManager.float_type)
3661 ig.Emit (OpCodes.Ldelem_R4);
3662 else if (type == TypeManager.double_type)
3663 ig.Emit (OpCodes.Ldelem_R8);
3664 else if (type == TypeManager.intptr_type)
3665 ig.Emit (OpCodes.Ldelem_I);
3667 ig.Emit (OpCodes.Ldelem_Ref);
3671 // Emits the right opcode to store an object of Type `t'
3672 // from an array of T.
3674 static public void EmitStoreOpcode (ILGenerator ig, Type t)
3676 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3677 ig.Emit (OpCodes.Stelem_I1);
3678 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3679 ig.Emit (OpCodes.Stelem_I2);
3680 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3681 ig.Emit (OpCodes.Stelem_I4);
3682 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3683 ig.Emit (OpCodes.Stelem_I8);
3684 else if (t == TypeManager.float_type)
3685 ig.Emit (OpCodes.Stelem_R4);
3686 else if (t == TypeManager.double_type)
3687 ig.Emit (OpCodes.Stelem_R8);
3688 else if (t == TypeManager.intptr_type)
3689 ig.Emit (OpCodes.Stelem_I);
3691 ig.Emit (OpCodes.Stelem_Ref);
3694 public override void Emit (EmitContext ec)
3696 int rank = ea.Expr.Type.GetArrayRank ();
3697 ILGenerator ig = ec.ig;
3701 foreach (Argument a in ea.Arguments)
3705 EmitLoadOpcode (ig, type);
3707 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3708 Type [] args = new Type [ea.Arguments.Count];
3713 foreach (Argument a in ea.Arguments)
3714 args [i++] = a.Type;
3716 get = mb.GetArrayMethod (
3717 ea.Expr.Type, "Get",
3718 CallingConventions.HasThis |
3719 CallingConventions.Standard,
3722 ig.Emit (OpCodes.Call, get);
3726 public void EmitAssign (EmitContext ec, Expression source)
3728 int rank = ea.Expr.Type.GetArrayRank ();
3729 ILGenerator ig = ec.ig;
3733 foreach (Argument a in ea.Arguments)
3738 Type t = source.Type;
3740 EmitStoreOpcode (ig, t);
3742 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3743 Type [] args = new Type [ea.Arguments.Count + 1];
3748 foreach (Argument a in ea.Arguments)
3749 args [i++] = a.Type;
3753 set = mb.GetArrayMethod (
3754 ea.Expr.Type, "Set",
3755 CallingConventions.HasThis |
3756 CallingConventions.Standard,
3757 TypeManager.void_type, args);
3759 ig.Emit (OpCodes.Call, set);
3764 public ArrayList getters, setters;
3765 static Hashtable map;
3769 map = new Hashtable ();
3772 Indexers (MemberInfo [] mi)
3774 foreach (PropertyInfo property in mi){
3775 MethodInfo get, set;
3777 get = property.GetGetMethod (true);
3779 if (getters == null)
3780 getters = new ArrayList ();
3785 set = property.GetSetMethod (true);
3787 if (setters == null)
3788 setters = new ArrayList ();
3794 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
3796 Indexers ix = (Indexers) map [t];
3797 string p_name = TypeManager.IndexerPropertyName (t);
3802 MemberInfo [] mi = tm.FindMembers (
3803 t, MemberTypes.Property,
3804 BindingFlags.Public | BindingFlags.Instance,
3805 Type.FilterName, p_name);
3807 if (mi == null || mi.Length == 0){
3808 Report.Error (21, loc,
3809 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3810 "any indexers defined");
3814 ix = new Indexers (mi);
3822 // Expressions that represent an indexer call.
3824 public class IndexerAccess : Expression, IAssignMethod {
3826 // Points to our "data" repository
3829 MethodInfo get, set;
3831 ArrayList set_arguments;
3833 public IndexerAccess (ElementAccess ea_data)
3836 eclass = ExprClass.Value;
3839 public override Expression DoResolve (EmitContext ec)
3841 Type indexer_type = ea.Expr.Type;
3844 // Step 1: Query for all `Item' *properties*. Notice
3845 // that the actual methods are pointed from here.
3847 // This is a group of properties, piles of them.
3850 ilist = Indexers.GetIndexersForType (
3851 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3855 // Step 2: find the proper match
3857 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3858 get = (MethodInfo) Invocation.OverloadResolve (
3859 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3862 Report.Error (154, ea.loc,
3863 "indexer can not be used in this context, because " +
3864 "it lacks a `get' accessor");
3868 type = get.ReturnType;
3869 eclass = ExprClass.IndexerAccess;
3873 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3875 Type indexer_type = ea.Expr.Type;
3876 Type right_type = right_side.Type;
3879 ilist = Indexers.GetIndexersForType (
3880 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3882 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3883 set_arguments = (ArrayList) ea.Arguments.Clone ();
3884 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3886 set = (MethodInfo) Invocation.OverloadResolve (
3887 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3891 Report.Error (200, ea.loc,
3892 "indexer X.this [" + TypeManager.CSharpName (right_type) +
3893 "] lacks a `set' accessor");
3897 type = TypeManager.void_type;
3898 eclass = ExprClass.IndexerAccess;
3902 public override void Emit (EmitContext ec)
3904 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3908 // source is ignored, because we already have a copy of it from the
3909 // LValue resolution and we have already constructed a pre-cached
3910 // version of the arguments (ea.set_arguments);
3912 public void EmitAssign (EmitContext ec, Expression source)
3914 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3918 public class BaseAccess : Expression {
3920 public enum BaseAccessType : byte {
3925 public readonly BaseAccessType BAType;
3926 public readonly string Member;
3927 public readonly ArrayList Arguments;
3929 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3937 public override Expression DoResolve (EmitContext ec)
3939 // FIXME: Implement;
3940 throw new Exception ("Unimplemented");
3944 public override void Emit (EmitContext ec)
3946 throw new Exception ("Unimplemented");
3951 // This class exists solely to pass the Type around and to be a dummy
3952 // that can be passed to the conversion functions (this is used by
3953 // foreach implementation to typecast the object return value from
3954 // get_Current into the proper type. All code has been generated and
3955 // we only care about the side effect conversions to be performed
3958 public class EmptyExpression : Expression {
3959 public EmptyExpression ()
3961 type = TypeManager.object_type;
3962 eclass = ExprClass.Value;
3965 public EmptyExpression (Type t)
3968 eclass = ExprClass.Value;
3971 public override Expression DoResolve (EmitContext ec)
3976 public override void Emit (EmitContext ec)
3978 // nothing, as we only exist to not do anything.
3982 public class UserCast : Expression {
3986 public UserCast (MethodInfo method, Expression source)
3988 this.method = method;
3989 this.source = source;
3990 type = method.ReturnType;
3991 eclass = ExprClass.Value;
3994 public override Expression DoResolve (EmitContext ec)
3997 // We are born fully resolved
4002 public override void Emit (EmitContext ec)
4004 ILGenerator ig = ec.ig;
4008 if (method is MethodInfo)
4009 ig.Emit (OpCodes.Call, (MethodInfo) method);
4011 ig.Emit (OpCodes.Call, (ConstructorInfo) method);