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 Console.WriteLine ("Getting parameters for: " + mb);
1941 ParameterInfo [] pi = mb.GetParameters ();
1942 ReflectionParameters rp = new ReflectionParameters (pi);
1943 method_parameter_cache [mb] = rp;
1945 return (ParameterData) rp;
1950 // Tells whether a user defined conversion from Type `from' to
1951 // Type `to' exists.
1953 // FIXME: we could implement a cache here.
1955 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
1957 // Locate user-defined implicit operators
1961 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
1964 MethodGroupExpr me = (MethodGroupExpr) mg;
1966 for (int i = me.Methods.Length; i > 0;) {
1968 MethodBase mb = me.Methods [i];
1969 ParameterData pd = GetParameterData (mb);
1971 if (from == pd.ParameterType (0))
1976 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
1979 MethodGroupExpr me = (MethodGroupExpr) mg;
1981 for (int i = me.Methods.Length; i > 0;) {
1983 MethodBase mb = me.Methods [i];
1984 MethodInfo mi = (MethodInfo) mb;
1986 if (mi.ReturnType == to)
1995 // Determines "better conversion" as specified in 7.4.2.3
1996 // Returns : 1 if a->p is better
1997 // 0 if a->q or neither is better
1999 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
2002 Type argument_type = a.Type;
2003 Expression argument_expr = a.Expr;
2005 if (argument_type == null)
2006 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2011 if (argument_type == p)
2014 if (argument_type == q)
2018 // Now probe whether an implicit constant expression conversion
2021 // An implicit constant expression conversion permits the following
2024 // * A constant-expression of type `int' can be converted to type
2025 // sbyte, byute, short, ushort, uint, ulong provided the value of
2026 // of the expression is withing the range of the destination type.
2028 // * A constant-expression of type long can be converted to type
2029 // ulong, provided the value of the constant expression is not negative
2031 // FIXME: Note that this assumes that constant folding has
2032 // taken place. We dont do constant folding yet.
2035 if (argument_expr is IntLiteral){
2036 IntLiteral ei = (IntLiteral) argument_expr;
2037 int value = ei.Value;
2039 if (p == TypeManager.sbyte_type){
2040 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2042 } else if (p == TypeManager.byte_type){
2043 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2045 } else if (p == TypeManager.short_type){
2046 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2048 } else if (p == TypeManager.ushort_type){
2049 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2051 } else if (p == TypeManager.uint32_type){
2053 // we can optimize this case: a positive int32
2054 // always fits on a uint32
2058 } else if (p == TypeManager.uint64_type){
2060 // we can optimize this case: a positive int32
2061 // always fits on a uint64
2066 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2067 LongLiteral ll = (LongLiteral) argument_expr;
2069 if (p == TypeManager.uint64_type){
2080 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
2082 tmp = ConvertImplicit (ec, argument_expr, p, loc);
2091 if (ConversionExists (ec, p, q, loc) == true &&
2092 ConversionExists (ec, q, p, loc) == false)
2095 if (p == TypeManager.sbyte_type)
2096 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2097 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2100 if (p == TypeManager.short_type)
2101 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2102 q == TypeManager.uint64_type)
2105 if (p == TypeManager.int32_type)
2106 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2109 if (p == TypeManager.int64_type)
2110 if (q == TypeManager.uint64_type)
2117 // Determines "Better function" and returns an integer indicating :
2118 // 0 if candidate ain't better
2119 // 1 if candidate is better than the current best match
2121 static int BetterFunction (EmitContext ec, ArrayList args,
2122 MethodBase candidate, MethodBase best,
2123 bool use_standard, Location loc)
2125 ParameterData candidate_pd = GetParameterData (candidate);
2126 ParameterData best_pd;
2132 argument_count = args.Count;
2134 if (candidate_pd.Count == 0 && argument_count == 0)
2138 if (candidate_pd.Count == argument_count) {
2140 for (int j = argument_count; j > 0;) {
2143 Argument a = (Argument) args [j];
2145 x = BetterConversion (
2146 ec, a, candidate_pd.ParameterType (j), null,
2162 best_pd = GetParameterData (best);
2164 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2165 int rating1 = 0, rating2 = 0;
2167 for (int j = argument_count; j > 0;) {
2171 Argument a = (Argument) args [j];
2173 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
2174 best_pd.ParameterType (j), use_standard, loc);
2175 y = BetterConversion (ec, a, best_pd.ParameterType (j),
2176 candidate_pd.ParameterType (j), use_standard,
2183 if (rating1 > rating2)
2192 public static string FullMethodDesc (MethodBase mb)
2194 StringBuilder sb = new StringBuilder (mb.Name);
2195 ParameterData pd = GetParameterData (mb);
2197 int count = pd.Count;
2200 for (int i = count; i > 0; ) {
2203 sb.Append (pd.ParameterDesc (count - i - 1));
2209 return sb.ToString ();
2212 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2214 MemberInfo [] miset;
2215 MethodGroupExpr union;
2217 if (mg1 != null && mg2 != null) {
2219 MethodGroupExpr left_set = null, right_set = null;
2220 int length1 = 0, length2 = 0;
2222 left_set = (MethodGroupExpr) mg1;
2223 length1 = left_set.Methods.Length;
2225 right_set = (MethodGroupExpr) mg2;
2226 length2 = right_set.Methods.Length;
2228 ArrayList common = new ArrayList ();
2230 for (int i = 0; i < left_set.Methods.Length; i++) {
2231 for (int j = 0; j < right_set.Methods.Length; j++) {
2232 if (left_set.Methods [i] == right_set.Methods [j])
2233 common.Add (left_set.Methods [i]);
2237 miset = new MemberInfo [length1 + length2 - common.Count];
2239 left_set.Methods.CopyTo (miset, 0);
2243 for (int j = 0; j < right_set.Methods.Length; j++)
2244 if (!common.Contains (right_set.Methods [j]))
2245 miset [length1 + k++] = right_set.Methods [j];
2247 union = new MethodGroupExpr (miset);
2251 } else if (mg1 == null && mg2 != null) {
2253 MethodGroupExpr me = (MethodGroupExpr) mg2;
2255 miset = new MemberInfo [me.Methods.Length];
2256 me.Methods.CopyTo (miset, 0);
2258 union = new MethodGroupExpr (miset);
2262 } else if (mg2 == null && mg1 != null) {
2264 MethodGroupExpr me = (MethodGroupExpr) mg1;
2266 miset = new MemberInfo [me.Methods.Length];
2267 me.Methods.CopyTo (miset, 0);
2269 union = new MethodGroupExpr (miset);
2278 // Determines is the candidate method, if a params method, is applicable
2279 // in its expanded form to the given set of arguments
2281 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2285 if (arguments == null)
2288 arg_count = arguments.Count;
2290 ParameterData pd = GetParameterData (candidate);
2292 int pd_count = pd.Count;
2294 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2297 if (pd_count - 1 > arg_count)
2300 // If we have come this far, the case which remains is when the number of parameters
2301 // is less than or equal to the argument count. So, we now check if the element type
2302 // of the params array is compatible with each argument type
2305 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2307 for (int i = pd_count - 1; i < arg_count - 1; i++) {
2308 Argument a = (Argument) arguments [i];
2309 if (!StandardConversionExists (a.Type, element_type))
2317 // Determines if the candidate method is applicable (section 14.4.2.1)
2318 // to the given set of arguments
2320 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2324 if (arguments == null)
2327 arg_count = arguments.Count;
2329 ParameterData pd = GetParameterData (candidate);
2331 int pd_count = pd.Count;
2333 if (arg_count != pd.Count)
2336 for (int i = arg_count; i > 0; ) {
2339 Argument a = (Argument) arguments [i];
2341 Parameter.Modifier a_mod = a.GetParameterModifier ();
2342 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2344 if (a_mod == p_mod) {
2346 if (a_mod == Parameter.Modifier.NONE)
2347 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2350 if (a_mod == Parameter.Modifier.REF ||
2351 a_mod == Parameter.Modifier.OUT)
2352 if (pd.ParameterType (i) != a.Type)
2364 // Find the Applicable Function Members (7.4.2.1)
2366 // me: Method Group expression with the members to select.
2367 // it might contain constructors or methods (or anything
2368 // that maps to a method).
2370 // Arguments: ArrayList containing resolved Argument objects.
2372 // loc: The location if we want an error to be reported, or a Null
2373 // location for "probing" purposes.
2375 // use_standard: controls whether OverloadResolve should use the
2376 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
2378 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2379 // that is the best match of me on Arguments.
2382 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2383 ArrayList Arguments, Location loc,
2386 ArrayList afm = new ArrayList ();
2387 int best_match_idx = -1;
2388 MethodBase method = null;
2391 for (int i = me.Methods.Length; i > 0; ){
2393 MethodBase candidate = me.Methods [i];
2396 // Check if candidate is applicable (section 14.4.2.1)
2397 if (!IsApplicable (Arguments, candidate))
2400 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
2406 method = me.Methods [best_match_idx];
2410 if (Arguments == null)
2413 argument_count = Arguments.Count;
2416 // Now we see if we can find params functions, applicable in their expanded form
2417 // since if they were applicable in their normal form, they would have been selected
2420 if (best_match_idx == -1) {
2422 for (int i = me.Methods.Length; i > 0; ) {
2424 MethodBase candidate = me.Methods [i];
2426 if (IsParamsMethodApplicable (Arguments, candidate)) {
2428 method = me.Methods [best_match_idx];
2435 // Now we see if we can at least find a method with the same number of arguments
2439 if (best_match_idx == -1) {
2441 for (int i = me.Methods.Length; i > 0;) {
2443 MethodBase mb = me.Methods [i];
2444 pd = GetParameterData (mb);
2446 if (pd.Count == argument_count) {
2448 method = me.Methods [best_match_idx];
2458 // And now convert implicitly, each argument to the required type
2460 pd = GetParameterData (method);
2461 int pd_count = pd.Count;
2463 for (int j = 0; j < argument_count; j++) {
2465 Argument a = (Argument) Arguments [j];
2466 Expression a_expr = a.Expr;
2467 Type parameter_type = pd.ParameterType (j);
2470 // Note that we need to compare against the element type
2471 // when we have a params method
2473 if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
2474 if (j >= pd_count - 1)
2475 parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
2478 if (a.Type != parameter_type){
2482 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
2485 conv = ConvertImplicit (ec, a_expr, parameter_type,
2489 if (!Location.IsNull (loc)) {
2491 "The best overloaded match for method '" + FullMethodDesc (method)+
2492 "' has some invalid arguments");
2494 "Argument " + (j+1) +
2495 ": Cannot convert from '" + Argument.FullDesc (a)
2496 + "' to '" + pd.ParameterDesc (j) + "'");
2504 // Update the argument with the implicit conversion
2509 // FIXME : For the case of params methods, we need to actually instantiate
2510 // an array and initialize it with the argument values etc etc.
2514 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2515 pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2516 if (!Location.IsNull (loc)) {
2518 "The best overloaded match for method '" + FullMethodDesc (method)+
2519 "' has some invalid arguments");
2521 "Argument " + (j+1) +
2522 ": Cannot convert from '" + Argument.FullDesc (a)
2523 + "' to '" + pd.ParameterDesc (j) + "'");
2534 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2535 ArrayList Arguments, Location loc)
2537 return OverloadResolve (ec, me, Arguments, loc, false);
2540 public override Expression DoResolve (EmitContext ec)
2543 // First, resolve the expression that is used to
2544 // trigger the invocation
2546 expr = expr.Resolve (ec);
2550 if (!(expr is MethodGroupExpr)) {
2551 Type expr_type = expr.Type;
2553 if (expr_type != null){
2554 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2556 return (new DelegateInvocation (
2557 this.expr, Arguments, loc)).Resolve (ec);
2561 if (!(expr is MethodGroupExpr)){
2562 report118 (loc, this.expr, "method group");
2567 // Next, evaluate all the expressions in the argument list
2569 if (Arguments != null){
2570 for (int i = Arguments.Count; i > 0;){
2572 Argument a = (Argument) Arguments [i];
2574 if (!a.Resolve (ec, loc))
2579 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
2581 if (method == null){
2583 "Could not find any applicable function for this argument list");
2587 if (method is MethodInfo)
2588 type = ((MethodInfo)method).ReturnType;
2590 eclass = ExprClass.Value;
2595 // Emits the list of arguments as an array
2597 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
2599 ILGenerator ig = ec.ig;
2600 int count = arguments.Count - idx;
2601 Argument a = (Argument) arguments [idx];
2602 Type t = a.expr.Type;
2603 string array_type = t.FullName + "[]";
2606 array = ec.GetTemporaryStorage (Type.GetType (array_type));
2607 IntLiteral.EmitInt (ig, count);
2608 ig.Emit (OpCodes.Newarr, t);
2609 ig.Emit (OpCodes.Stloc, array);
2611 int top = arguments.Count;
2612 for (int j = idx; j < top; j++){
2613 ig.Emit (OpCodes.Ldloc, array);
2614 IntLiteral.EmitInt (ig, j - idx);
2616 ig.Emit (OpCodes.Stelem_Ref);
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 = ec.GetTemporaryStorage (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;
2894 public ArrayCreation (string requested_type, ArrayList exprs,
2895 string rank, ArrayList initializers, Location l)
2897 RequestedType = requested_type;
2899 Initializers = initializers;
2902 Arguments = new ArrayList ();
2904 foreach (Expression e in exprs)
2905 Arguments.Add (new Argument (e, Argument.AType.Expression));
2909 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2911 RequestedType = requested_type;
2913 Initializers = initializers;
2917 public static string FormArrayType (string base_type, int idx_count, string rank)
2919 StringBuilder sb = new StringBuilder (base_type);
2924 for (int i = 1; i < idx_count; i++)
2928 return sb.ToString ();
2931 public static string FormElementType (string base_type, int idx_count, string rank)
2933 StringBuilder sb = new StringBuilder (base_type);
2936 for (int i = 1; i < idx_count; i++)
2942 string val = sb.ToString ();
2944 return val.Substring (0, val.LastIndexOf ("["));
2948 public override Expression DoResolve (EmitContext ec)
2952 if (Arguments == null)
2955 arg_count = Arguments.Count;
2957 string array_type = FormArrayType (RequestedType, arg_count, Rank);
2959 string element_type = FormElementType (RequestedType, arg_count, Rank);
2961 type = ec.TypeContainer.LookupType (array_type, false);
2963 array_element_type = ec.TypeContainer.LookupType (element_type, false);
2968 if (arg_count == 1) {
2969 IsOneDimensional = true;
2970 eclass = ExprClass.Value;
2974 IsBuiltinType = TypeManager.IsBuiltinType (type);
2976 if (IsBuiltinType) {
2980 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
2981 AllBindingsFlags, loc);
2983 if (!(ml is MethodGroupExpr)){
2984 report118 (loc, ml, "method group");
2989 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
2990 "this argument list");
2994 if (Arguments != null) {
2995 for (int i = arg_count; i > 0;){
2997 Argument a = (Argument) Arguments [i];
2999 if (!a.Resolve (ec, loc))
3004 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3006 if (method == null) {
3007 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3008 "this argument list");
3012 eclass = ExprClass.Value;
3017 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3019 ArrayList args = new ArrayList ();
3020 if (Arguments != null){
3021 for (int i = arg_count; i > 0;){
3023 Argument a = (Argument) Arguments [i];
3025 if (!a.Resolve (ec, loc))
3032 Type [] arg_types = null;
3035 arg_types = new Type [args.Count];
3037 args.CopyTo (arg_types, 0);
3039 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3042 if (method == null) {
3043 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3044 "this argument list");
3048 eclass = ExprClass.Value;
3054 public override void Emit (EmitContext ec)
3056 ILGenerator ig = ec.ig;
3058 if (IsOneDimensional) {
3059 Invocation.EmitArguments (ec, null, Arguments);
3060 ig.Emit (OpCodes.Newarr, array_element_type);
3063 Invocation.EmitArguments (ec, method, Arguments);
3066 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3068 ig.Emit (OpCodes.Newobj, (MethodInfo) method);
3071 if (Initializers != null){
3074 // FIXME: This is just sample data, need to fill with
3076 byte [] a = new byte [4] { 1, 2, 3, 4 };
3078 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
3080 ig.Emit (OpCodes.Dup);
3081 ig.Emit (OpCodes.Ldtoken, fb);
3082 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
3086 public override void EmitStatement (EmitContext ec)
3089 ec.ig.Emit (OpCodes.Pop);
3095 // Represents the `this' construct
3097 public class This : Expression, IAssignMethod, IMemoryLocation {
3100 public This (Location loc)
3105 public override Expression DoResolve (EmitContext ec)
3107 eclass = ExprClass.Variable;
3108 type = ec.TypeContainer.TypeBuilder;
3111 Report.Error (26, loc,
3112 "Keyword this not valid in static code");
3119 public Expression DoResolveLValue (EmitContext ec)
3123 if (ec.TypeContainer is Class){
3124 Report.Error (1604, loc, "Cannot assign to `this'");
3131 public override void Emit (EmitContext ec)
3133 ec.ig.Emit (OpCodes.Ldarg_0);
3136 public void EmitAssign (EmitContext ec, Expression source)
3139 ec.ig.Emit (OpCodes.Starg, 0);
3142 public void AddressOf (EmitContext ec)
3144 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3149 // Implements the typeof operator
3151 public class TypeOf : Expression {
3152 public readonly string QueriedType;
3155 public TypeOf (string queried_type)
3157 QueriedType = queried_type;
3160 public override Expression DoResolve (EmitContext ec)
3162 typearg = ec.TypeContainer.LookupType (QueriedType, false);
3164 if (typearg == null)
3167 type = TypeManager.type_type;
3168 eclass = ExprClass.Type;
3172 public override void Emit (EmitContext ec)
3174 ec.ig.Emit (OpCodes.Ldtoken, typearg);
3175 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3179 public class SizeOf : Expression {
3180 public readonly string QueriedType;
3182 public SizeOf (string queried_type)
3184 this.QueriedType = queried_type;
3187 public override Expression DoResolve (EmitContext ec)
3189 // FIXME: Implement;
3190 throw new Exception ("Unimplemented");
3194 public override void Emit (EmitContext ec)
3196 throw new Exception ("Implement me");
3200 public class MemberAccess : Expression {
3201 public readonly string Identifier;
3203 Expression member_lookup;
3206 public MemberAccess (Expression expr, string id, Location l)
3213 public Expression Expr {
3219 void error176 (Location loc, string name)
3221 Report.Error (176, loc, "Static member `" +
3222 name + "' cannot be accessed " +
3223 "with an instance reference, qualify with a " +
3224 "type name instead");
3227 public override Expression DoResolve (EmitContext ec)
3230 // We are the sole users of ResolveWithSimpleName (ie, the only
3231 // ones that can cope with it
3233 expr = expr.ResolveWithSimpleName (ec);
3238 if (expr is SimpleName){
3239 SimpleName child_expr = (SimpleName) expr;
3241 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3243 return expr.Resolve (ec);
3246 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3248 if (member_lookup == null)
3254 if (member_lookup is MethodGroupExpr){
3255 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3260 if (expr is TypeExpr){
3261 if (!mg.RemoveInstanceMethods ()){
3262 SimpleName.Error120 (loc, mg.Methods [0].Name);
3266 return member_lookup;
3270 // Instance.MethodGroup
3272 if (!mg.RemoveStaticMethods ()){
3273 error176 (loc, mg.Methods [0].Name);
3277 mg.InstanceExpression = expr;
3279 return member_lookup;
3282 if (member_lookup is FieldExpr){
3283 FieldExpr fe = (FieldExpr) member_lookup;
3284 FieldInfo fi = fe.FieldInfo;
3287 Type t = fi.FieldType;
3290 if (fi is FieldBuilder)
3291 o = TypeManager.GetValue ((FieldBuilder) fi);
3293 o = fi.GetValue (fi);
3295 if (t.IsSubclassOf (TypeManager.enum_type)) {
3296 Expression enum_member = MemberLookup (ec, t, "value__", false, loc);
3297 Type underlying_type = enum_member.Type;
3299 Expression e = Literalize (o, underlying_type);
3302 return new EnumLiteral (e, t);
3305 Expression exp = Literalize (o, t);
3311 if (expr is TypeExpr){
3312 if (!fe.FieldInfo.IsStatic){
3313 error176 (loc, fe.FieldInfo.Name);
3316 return member_lookup;
3318 if (fe.FieldInfo.IsStatic){
3319 error176 (loc, fe.FieldInfo.Name);
3322 fe.InstanceExpression = expr;
3328 if (member_lookup is PropertyExpr){
3329 PropertyExpr pe = (PropertyExpr) member_lookup;
3331 if (expr is TypeExpr){
3333 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3339 error176 (loc, pe.PropertyInfo.Name);
3342 pe.InstanceExpression = expr;
3348 Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3349 Environment.Exit (0);
3353 public override void Emit (EmitContext ec)
3355 throw new Exception ("Should not happen I think");
3360 public class CheckedExpr : Expression {
3362 public Expression Expr;
3364 public CheckedExpr (Expression e)
3369 public override Expression DoResolve (EmitContext ec)
3371 Expr = Expr.Resolve (ec);
3376 eclass = Expr.ExprClass;
3381 public override void Emit (EmitContext ec)
3383 bool last_check = ec.CheckState;
3385 ec.CheckState = true;
3387 ec.CheckState = last_check;
3392 public class UnCheckedExpr : Expression {
3394 public Expression Expr;
3396 public UnCheckedExpr (Expression e)
3401 public override Expression DoResolve (EmitContext ec)
3403 Expr = Expr.Resolve (ec);
3408 eclass = Expr.ExprClass;
3413 public override void Emit (EmitContext ec)
3415 bool last_check = ec.CheckState;
3417 ec.CheckState = false;
3419 ec.CheckState = last_check;
3424 public class ElementAccess : Expression {
3425 public ArrayList Arguments;
3426 public Expression Expr;
3427 public Location loc;
3429 public ElementAccess (Expression e, ArrayList e_list, Location l)
3433 Arguments = new ArrayList ();
3434 foreach (Expression tmp in e_list)
3435 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3440 bool CommonResolve (EmitContext ec)
3442 Expr = Expr.Resolve (ec);
3447 if (Arguments == null)
3450 for (int i = Arguments.Count; i > 0;){
3452 Argument a = (Argument) Arguments [i];
3454 if (!a.Resolve (ec, loc))
3461 public override Expression DoResolve (EmitContext ec)
3463 if (!CommonResolve (ec))
3467 // We perform some simple tests, and then to "split" the emit and store
3468 // code we create an instance of a different class, and return that.
3470 // I am experimenting with this pattern.
3472 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3473 return (new ArrayAccess (this)).Resolve (ec);
3475 return (new IndexerAccess (this)).Resolve (ec);
3478 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3480 if (!CommonResolve (ec))
3483 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3484 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3486 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3489 public override void Emit (EmitContext ec)
3491 throw new Exception ("Should never be reached");
3496 // Implements array access
3498 public class ArrayAccess : Expression, IAssignMethod {
3500 // Points to our "data" repository
3504 public ArrayAccess (ElementAccess ea_data)
3507 eclass = ExprClass.Variable;
3510 public override Expression DoResolve (EmitContext ec)
3512 if (ea.Expr.ExprClass != ExprClass.Variable) {
3513 report118 (ea.loc, ea.Expr, "variable");
3517 Type t = ea.Expr.Type;
3519 if (t.GetArrayRank () != ea.Arguments.Count){
3520 Report.Error (22, ea.loc,
3521 "Incorrect number of indexes for array " +
3522 " expected: " + t.GetArrayRank () + " got: " +
3523 ea.Arguments.Count);
3526 type = t.GetElementType ();
3527 eclass = ExprClass.Variable;
3533 // Emits the right opcode to load an object of Type `t'
3534 // from an array of T
3536 static public void EmitLoadOpcode (ILGenerator ig, Type type)
3538 if (type == TypeManager.byte_type)
3539 ig.Emit (OpCodes.Ldelem_I1);
3540 else if (type == TypeManager.sbyte_type)
3541 ig.Emit (OpCodes.Ldelem_U1);
3542 else if (type == TypeManager.short_type)
3543 ig.Emit (OpCodes.Ldelem_I2);
3544 else if (type == TypeManager.ushort_type)
3545 ig.Emit (OpCodes.Ldelem_U2);
3546 else if (type == TypeManager.int32_type)
3547 ig.Emit (OpCodes.Ldelem_I4);
3548 else if (type == TypeManager.uint32_type)
3549 ig.Emit (OpCodes.Ldelem_U4);
3550 else if (type == TypeManager.uint64_type)
3551 ig.Emit (OpCodes.Ldelem_I8);
3552 else if (type == TypeManager.int64_type)
3553 ig.Emit (OpCodes.Ldelem_I8);
3554 else if (type == TypeManager.float_type)
3555 ig.Emit (OpCodes.Ldelem_R4);
3556 else if (type == TypeManager.double_type)
3557 ig.Emit (OpCodes.Ldelem_R8);
3558 else if (type == TypeManager.intptr_type)
3559 ig.Emit (OpCodes.Ldelem_I);
3561 ig.Emit (OpCodes.Ldelem_Ref);
3564 public override void Emit (EmitContext ec)
3566 int rank = ea.Expr.Type.GetArrayRank ();
3567 ILGenerator ig = ec.ig;
3571 foreach (Argument a in ea.Arguments)
3575 EmitLoadOpcode (ig, type);
3577 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3578 Type [] args = new Type [ea.Arguments.Count];
3583 foreach (Argument a in ea.Arguments)
3584 args [i++] = a.Type;
3586 get = mb.GetArrayMethod (
3587 ea.Expr.Type, "Get",
3588 CallingConventions.HasThis |
3589 CallingConventions.Standard,
3592 ig.Emit (OpCodes.Call, get);
3596 public void EmitAssign (EmitContext ec, Expression source)
3598 int rank = ea.Expr.Type.GetArrayRank ();
3599 ILGenerator ig = ec.ig;
3603 foreach (Argument a in ea.Arguments)
3608 Type t = source.Type;
3610 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3611 ig.Emit (OpCodes.Stelem_I1);
3612 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3613 ig.Emit (OpCodes.Stelem_I2);
3614 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3615 ig.Emit (OpCodes.Stelem_I4);
3616 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3617 ig.Emit (OpCodes.Stelem_I8);
3618 else if (t == TypeManager.float_type)
3619 ig.Emit (OpCodes.Stelem_R4);
3620 else if (t == TypeManager.double_type)
3621 ig.Emit (OpCodes.Stelem_R8);
3622 else if (t == TypeManager.intptr_type)
3623 ig.Emit (OpCodes.Stelem_I);
3625 ig.Emit (OpCodes.Stelem_Ref);
3627 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3628 Type [] args = new Type [ea.Arguments.Count + 1];
3633 foreach (Argument a in ea.Arguments)
3634 args [i++] = a.Type;
3638 set = mb.GetArrayMethod (
3639 ea.Expr.Type, "Set",
3640 CallingConventions.HasThis |
3641 CallingConventions.Standard,
3642 TypeManager.void_type, args);
3644 ig.Emit (OpCodes.Call, set);
3649 public ArrayList getters, setters;
3650 static Hashtable map;
3654 map = new Hashtable ();
3657 Indexers (MemberInfo [] mi)
3659 foreach (PropertyInfo property in mi){
3660 MethodInfo get, set;
3662 get = property.GetGetMethod (true);
3664 if (getters == null)
3665 getters = new ArrayList ();
3670 set = property.GetSetMethod (true);
3672 if (setters == null)
3673 setters = new ArrayList ();
3679 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
3681 Indexers ix = (Indexers) map [t];
3682 string p_name = TypeManager.IndexerPropertyName (t);
3687 MemberInfo [] mi = tm.FindMembers (
3688 t, MemberTypes.Property,
3689 BindingFlags.Public | BindingFlags.Instance,
3690 Type.FilterName, p_name);
3692 if (mi == null || mi.Length == 0){
3693 Report.Error (21, loc,
3694 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3695 "any indexers defined");
3699 ix = new Indexers (mi);
3707 // Expressions that represent an indexer call.
3709 public class IndexerAccess : Expression, IAssignMethod {
3711 // Points to our "data" repository
3714 MethodInfo get, set;
3716 ArrayList set_arguments;
3718 public IndexerAccess (ElementAccess ea_data)
3721 eclass = ExprClass.Value;
3724 public override Expression DoResolve (EmitContext ec)
3726 Type indexer_type = ea.Expr.Type;
3729 // Step 1: Query for all `Item' *properties*. Notice
3730 // that the actual methods are pointed from here.
3732 // This is a group of properties, piles of them.
3735 ilist = Indexers.GetIndexersForType (
3736 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3740 // Step 2: find the proper match
3742 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3743 get = (MethodInfo) Invocation.OverloadResolve (
3744 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3747 Report.Error (154, ea.loc,
3748 "indexer can not be used in this context, because " +
3749 "it lacks a `get' accessor");
3753 type = get.ReturnType;
3754 eclass = ExprClass.IndexerAccess;
3758 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3760 Type indexer_type = ea.Expr.Type;
3761 Type right_type = right_side.Type;
3764 ilist = Indexers.GetIndexersForType (
3765 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3767 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3768 set_arguments = (ArrayList) ea.Arguments.Clone ();
3769 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3771 set = (MethodInfo) Invocation.OverloadResolve (
3772 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3776 Report.Error (200, ea.loc,
3777 "indexer X.this [" + TypeManager.CSharpName (right_type) +
3778 "] lacks a `set' accessor");
3782 type = TypeManager.void_type;
3783 eclass = ExprClass.IndexerAccess;
3787 public override void Emit (EmitContext ec)
3789 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3793 // source is ignored, because we already have a copy of it from the
3794 // LValue resolution and we have already constructed a pre-cached
3795 // version of the arguments (ea.set_arguments);
3797 public void EmitAssign (EmitContext ec, Expression source)
3799 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3803 public class BaseAccess : Expression {
3805 public enum BaseAccessType : byte {
3810 public readonly BaseAccessType BAType;
3811 public readonly string Member;
3812 public readonly ArrayList Arguments;
3814 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3822 public override Expression DoResolve (EmitContext ec)
3824 // FIXME: Implement;
3825 throw new Exception ("Unimplemented");
3829 public override void Emit (EmitContext ec)
3831 throw new Exception ("Unimplemented");
3836 // This class exists solely to pass the Type around and to be a dummy
3837 // that can be passed to the conversion functions (this is used by
3838 // foreach implementation to typecast the object return value from
3839 // get_Current into the proper type. All code has been generated and
3840 // we only care about the side effect conversions to be performed
3843 public class EmptyExpression : Expression {
3844 public EmptyExpression ()
3846 type = TypeManager.object_type;
3847 eclass = ExprClass.Value;
3850 public EmptyExpression (Type t)
3853 eclass = ExprClass.Value;
3856 public override Expression DoResolve (EmitContext ec)
3861 public override void Emit (EmitContext ec)
3863 // nothing, as we only exist to not do anything.
3867 public class UserCast : Expression {
3871 public UserCast (MethodInfo method, Expression source)
3873 this.method = method;
3874 this.source = source;
3875 type = method.ReturnType;
3876 eclass = ExprClass.Value;
3879 public override Expression DoResolve (EmitContext ec)
3882 // We are born fully resolved
3887 public override void Emit (EmitContext ec)
3889 ILGenerator ig = ec.ig;
3893 if (method is MethodInfo)
3894 ig.Emit (OpCodes.Call, (MethodInfo) method);
3896 ig.Emit (OpCodes.Call, (ConstructorInfo) method);