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;
24 // Unary implements unary expressions. It derives from
25 // ExpressionStatement becuase the pre/post increment/decrement
26 // operators can be used in a statement context.
28 public class Unary : ExpressionStatement {
29 public enum Operator {
30 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
31 Indirection, AddressOf, PreIncrement,
32 PreDecrement, PostIncrement, PostDecrement
41 public Unary (Operator op, Expression expr, Location loc)
48 public Expression Expr {
58 public Operator Oper {
69 // Returns a stringified representation of the Operator
74 case Operator.UnaryPlus:
76 case Operator.UnaryNegation:
78 case Operator.LogicalNot:
80 case Operator.OnesComplement:
82 case Operator.AddressOf:
84 case Operator.Indirection:
86 case Operator.PreIncrement : case Operator.PostIncrement :
88 case Operator.PreDecrement : case Operator.PostDecrement :
92 return oper.ToString ();
95 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
97 if (expr.Type == target_type)
100 return ConvertImplicit (ec, expr, target_type, new Location (-1));
103 void error23 (Type t)
106 23, loc, "Operator " + OperName () +
107 " cannot be applied to operand of type `" +
108 TypeManager.CSharpName (t) + "'");
112 // Returns whether an object of type `t' can be incremented
113 // or decremented with add/sub (ie, basically whether we can
114 // use pre-post incr-decr operations on it, but it is not a
115 // System.Decimal, which we test elsewhere)
117 static bool IsIncrementableNumber (Type t)
119 return (t == TypeManager.sbyte_type) ||
120 (t == TypeManager.byte_type) ||
121 (t == TypeManager.short_type) ||
122 (t == TypeManager.ushort_type) ||
123 (t == TypeManager.int32_type) ||
124 (t == TypeManager.uint32_type) ||
125 (t == TypeManager.int64_type) ||
126 (t == TypeManager.uint64_type) ||
127 (t == TypeManager.char_type) ||
128 (t.IsSubclassOf (TypeManager.enum_type)) ||
129 (t == TypeManager.float_type) ||
130 (t == TypeManager.double_type);
133 static Expression TryReduceNegative (Expression expr)
137 if (expr is IntLiteral)
138 e = new IntLiteral (-((IntLiteral) expr).Value);
139 else if (expr is UIntLiteral)
140 e = new LongLiteral (-((UIntLiteral) expr).Value);
141 else if (expr is LongLiteral)
142 e = new LongLiteral (-((LongLiteral) expr).Value);
143 else if (expr is FloatLiteral)
144 e = new FloatLiteral (-((FloatLiteral) expr).Value);
145 else if (expr is DoubleLiteral)
146 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
147 else if (expr is DecimalLiteral)
148 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
153 Expression ResolveOperator (EmitContext ec)
155 Type expr_type = expr.Type;
158 // Step 1: Perform Operator Overload location
163 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
164 op_name = "op_Increment";
165 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
166 op_name = "op_Decrement";
168 op_name = "op_" + oper;
170 mg = MemberLookup (ec, expr_type, op_name, false, loc);
172 if (mg == null && expr_type.BaseType != null)
173 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
176 Arguments = new ArrayList ();
177 Arguments.Add (new Argument (expr, Argument.AType.Expression));
179 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
181 if (method != null) {
182 MethodInfo mi = (MethodInfo) method;
183 type = mi.ReturnType;
193 // Step 2: Default operations on CLI native types.
196 // Only perform numeric promotions on:
199 if (expr_type == null)
202 if (oper == Operator.LogicalNot){
203 if (expr_type != TypeManager.bool_type) {
208 type = TypeManager.bool_type;
212 if (oper == Operator.OnesComplement) {
213 if (!((expr_type == TypeManager.int32_type) ||
214 (expr_type == TypeManager.uint32_type) ||
215 (expr_type == TypeManager.int64_type) ||
216 (expr_type == TypeManager.uint64_type) ||
217 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
225 if (oper == Operator.UnaryPlus) {
227 // A plus in front of something is just a no-op, so return the child.
233 // Deals with -literals
234 // int operator- (int x)
235 // long operator- (long x)
236 // float operator- (float f)
237 // double operator- (double d)
238 // decimal operator- (decimal d)
240 if (oper == Operator.UnaryNegation){
242 // Fold a "- Constant" into a negative constant
248 // Is this a constant?
250 e = TryReduceNegative (expr);
258 // Not a constant we can optimize, perform numeric
259 // promotions to int, long, double.
262 // The following is inneficient, because we call
263 // ConvertImplicit too many times.
265 // It is also not clear if we should convert to Float
266 // or Double initially.
268 if (expr_type == TypeManager.uint32_type){
270 // FIXME: handle exception to this rule that
271 // permits the int value -2147483648 (-2^31) to
272 // bt written as a decimal interger literal
274 type = TypeManager.int64_type;
275 expr = ConvertImplicit (ec, expr, type, loc);
279 if (expr_type == TypeManager.uint64_type){
281 // FIXME: Handle exception of `long value'
282 // -92233720368547758087 (-2^63) to be written as
283 // decimal integer literal.
289 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
296 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
303 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
315 // The operand of the prefix/postfix increment decrement operators
316 // should be an expression that is classified as a variable,
317 // a property access or an indexer access
319 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
320 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
321 if (expr.ExprClass == ExprClass.Variable){
322 if (IsIncrementableNumber (expr_type) ||
323 expr_type == TypeManager.decimal_type){
327 } else if (expr.ExprClass == ExprClass.IndexerAccess){
329 // FIXME: Verify that we have both get and set methods
331 throw new Exception ("Implement me");
332 } else if (expr.ExprClass == ExprClass.PropertyAccess){
333 PropertyExpr pe = (PropertyExpr) expr;
335 if (pe.VerifyAssignable ())
339 report118 (loc, expr, "variable, indexer or property access");
343 if (oper == Operator.AddressOf){
344 if (expr.ExprClass != ExprClass.Variable){
345 Error (211, "Cannot take the address of non-variables");
348 type = Type.GetType (expr.Type.ToString () + "*");
351 Error (187, "No such operator '" + OperName () + "' defined for type '" +
352 TypeManager.CSharpName (expr_type) + "'");
357 public override Expression DoResolve (EmitContext ec)
359 expr = expr.Resolve (ec);
364 eclass = ExprClass.Value;
365 return ResolveOperator (ec);
368 public override void Emit (EmitContext ec)
370 ILGenerator ig = ec.ig;
371 Type expr_type = expr.Type;
373 if (method != null) {
375 // Note that operators are static anyway
377 if (Arguments != null)
378 Invocation.EmitArguments (ec, Arguments);
381 // Post increment/decrement operations need a copy at this
384 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
385 ig.Emit (OpCodes.Dup);
388 ig.Emit (OpCodes.Call, (MethodInfo) method);
391 // Pre Increment and Decrement operators
393 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
394 ig.Emit (OpCodes.Dup);
398 // Increment and Decrement should store the result
400 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
401 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
402 ((IStackStore) expr).Store (ec);
408 case Operator.UnaryPlus:
409 throw new Exception ("This should be caught by Resolve");
411 case Operator.UnaryNegation:
413 ig.Emit (OpCodes.Neg);
416 case Operator.LogicalNot:
418 ig.Emit (OpCodes.Ldc_I4_0);
419 ig.Emit (OpCodes.Ceq);
422 case Operator.OnesComplement:
424 ig.Emit (OpCodes.Not);
427 case Operator.AddressOf:
428 ((IMemoryLocation)expr).AddressOf (ec);
431 case Operator.Indirection:
432 throw new Exception ("Not implemented yet");
434 case Operator.PreIncrement:
435 case Operator.PreDecrement:
436 if (expr.ExprClass == ExprClass.Variable){
438 // Resolve already verified that it is an "incrementable"
441 ig.Emit (OpCodes.Ldc_I4_1);
443 if (oper == Operator.PreDecrement)
444 ig.Emit (OpCodes.Sub);
446 ig.Emit (OpCodes.Add);
447 ig.Emit (OpCodes.Dup);
448 ((IStackStore) expr).Store (ec);
450 throw new Exception ("Handle Indexers and Properties here");
454 case Operator.PostIncrement:
455 case Operator.PostDecrement:
456 if (expr is IStackStore){
458 // Resolve already verified that it is an "incrementable"
461 ig.Emit (OpCodes.Dup);
462 ig.Emit (OpCodes.Ldc_I4_1);
464 if (oper == Operator.PostDecrement)
465 ig.Emit (OpCodes.Sub);
467 ig.Emit (OpCodes.Add);
468 ((IStackStore) expr).Store (ec);
470 Console.WriteLine ("Unknown exprclass: " + expr);
475 throw new Exception ("This should not happen: Operator = "
481 // This will emit the child expression for `ec' avoiding the logical
482 // not. The parent will take care of changing brfalse/brtrue
484 public void EmitLogicalNot (EmitContext ec)
486 if (oper != Operator.LogicalNot)
487 throw new Exception ("EmitLogicalNot can only be called with !expr");
492 public override void EmitStatement (EmitContext ec)
495 // FIXME: we should rewrite this code to generate
496 // better code for ++ and -- as we know we wont need
497 // the values on the stack
500 ec.ig.Emit (OpCodes.Pop);
503 public override Expression Reduce (EmitContext ec)
508 // We can not reduce expressions that invoke operator overloaded functions.
514 // First, reduce our child. Note that although we handle
516 expr = expr.Reduce (ec);
517 if (!(expr is Literal))
521 case Operator.UnaryPlus:
524 case Operator.UnaryNegation:
525 e = TryReduceNegative (expr);
530 case Operator.LogicalNot:
531 BoolLiteral b = (BoolLiteral) expr;
533 return new BoolLiteral (!(b.Value));
535 case Operator.OnesComplement:
538 if (et == TypeManager.int32_type)
539 return new IntLiteral (~ ((IntLiteral) expr).Value);
540 if (et == TypeManager.uint32_type)
541 return new UIntLiteral (~ ((UIntLiteral) expr).Value);
542 if (et == TypeManager.int64_type)
543 return new LongLiteral (~ ((LongLiteral) expr).Value);
544 if (et == TypeManager.uint64_type)
545 return new ULongLiteral (~ ((ULongLiteral) expr).Value);
552 public class Probe : Expression {
553 public readonly string ProbeType;
554 public readonly Operator Oper;
558 public enum Operator {
562 public Probe (Operator oper, Expression expr, string probe_type)
565 ProbeType = probe_type;
569 public Expression Expr {
575 public override Expression DoResolve (EmitContext ec)
577 probe_type = ec.TypeContainer.LookupType (ProbeType, false);
579 if (probe_type == null)
582 expr = expr.Resolve (ec);
584 type = TypeManager.bool_type;
585 eclass = ExprClass.Value;
590 public override void Emit (EmitContext ec)
592 ILGenerator ig = ec.ig;
596 if (Oper == Operator.Is){
597 ig.Emit (OpCodes.Isinst, probe_type);
598 ig.Emit (OpCodes.Ldnull);
599 ig.Emit (OpCodes.Cgt_Un);
601 ig.Emit (OpCodes.Isinst, probe_type);
607 // This represents a typecast in the source language.
609 // FIXME: Cast expressions have an unusual set of parsing
610 // rules, we need to figure those out.
612 public class Cast : Expression {
617 public Cast (string cast_type, Expression expr, Location loc)
619 this.target_type = cast_type;
624 public string TargetType {
630 public Expression Expr {
639 public override Expression DoResolve (EmitContext ec)
641 expr = expr.Resolve (ec);
645 type = ec.TypeContainer.LookupType (target_type, false);
646 eclass = ExprClass.Value;
651 expr = ConvertExplicit (ec, expr, type, loc);
656 public override void Emit (EmitContext ec)
659 // This one will never happen
661 throw new Exception ("Should not happen");
665 public class Binary : Expression {
666 public enum Operator {
667 Multiply, Division, Modulus,
668 Addition, Subtraction,
669 LeftShift, RightShift,
670 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
671 Equality, Inequality,
680 Expression left, right;
686 public Binary (Operator oper, Expression left, Expression right, Location loc)
694 public Operator Oper {
703 public Expression Left {
712 public Expression Right {
723 // Returns a stringified representation of the Operator
728 case Operator.Multiply:
730 case Operator.Division:
732 case Operator.Modulus:
734 case Operator.Addition:
736 case Operator.Subtraction:
738 case Operator.LeftShift:
740 case Operator.RightShift:
742 case Operator.LessThan:
744 case Operator.GreaterThan:
746 case Operator.LessThanOrEqual:
748 case Operator.GreaterThanOrEqual:
750 case Operator.Equality:
752 case Operator.Inequality:
754 case Operator.BitwiseAnd:
756 case Operator.BitwiseOr:
758 case Operator.ExclusiveOr:
760 case Operator.LogicalOr:
762 case Operator.LogicalAnd:
766 return oper.ToString ();
769 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
771 if (expr.Type == target_type)
774 return ConvertImplicit (ec, expr, target_type, new Location (-1));
778 // Note that handling the case l == Decimal || r == Decimal
779 // is taken care of by the Step 1 Operator Overload resolution.
781 bool DoNumericPromotions (EmitContext ec, Type l, Type r)
783 if (l == TypeManager.double_type || r == TypeManager.double_type){
785 // If either operand is of type double, the other operand is
786 // conveted to type double.
788 if (r != TypeManager.double_type)
789 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
790 if (l != TypeManager.double_type)
791 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
793 type = TypeManager.double_type;
794 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
796 // if either operand is of type float, th eother operand is
797 // converd to type float.
799 if (r != TypeManager.double_type)
800 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
801 if (l != TypeManager.double_type)
802 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
803 type = TypeManager.float_type;
804 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
808 // If either operand is of type ulong, the other operand is
809 // converted to type ulong. or an error ocurrs if the other
810 // operand is of type sbyte, short, int or long
813 if (l == TypeManager.uint64_type){
814 if (r != TypeManager.uint64_type && right is IntLiteral){
815 e = TryImplicitIntConversion (l, (IntLiteral) right);
821 if (left is IntLiteral){
822 e = TryImplicitIntConversion (r, (IntLiteral) left);
829 if ((other == TypeManager.sbyte_type) ||
830 (other == TypeManager.short_type) ||
831 (other == TypeManager.int32_type) ||
832 (other == TypeManager.int64_type)){
833 string oper = OperName ();
835 Error (34, loc, "Operator `" + OperName ()
836 + "' is ambiguous on operands of type `"
837 + TypeManager.CSharpName (l) + "' "
838 + "and `" + TypeManager.CSharpName (r)
841 type = TypeManager.uint64_type;
842 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
844 // If either operand is of type long, the other operand is converted
847 if (l != TypeManager.int64_type)
848 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
849 if (r != TypeManager.int64_type)
850 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
852 type = TypeManager.int64_type;
853 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
855 // If either operand is of type uint, and the other
856 // operand is of type sbyte, short or int, othe operands are
857 // converted to type long.
861 if (l == TypeManager.uint32_type)
863 else if (r == TypeManager.uint32_type)
866 if ((other == TypeManager.sbyte_type) ||
867 (other == TypeManager.short_type) ||
868 (other == TypeManager.int32_type)){
869 left = ForceConversion (ec, left, TypeManager.int64_type);
870 right = ForceConversion (ec, right, TypeManager.int64_type);
871 type = TypeManager.int64_type;
874 // if either operand is of type uint, the other
875 // operand is converd to type uint
877 left = ForceConversion (ec, left, TypeManager.uint32_type);
878 right = ForceConversion (ec, right, TypeManager.uint32_type);
879 type = TypeManager.uint32_type;
881 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
882 if (l != TypeManager.decimal_type)
883 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
884 if (r != TypeManager.decimal_type)
885 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
887 type = TypeManager.decimal_type;
889 Expression l_tmp, r_tmp;
891 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
895 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
902 type = TypeManager.int32_type;
911 "Operator " + OperName () + " cannot be applied to operands of type `" +
912 TypeManager.CSharpName (left.Type) + "' and `" +
913 TypeManager.CSharpName (right.Type) + "'");
917 Expression CheckShiftArguments (EmitContext ec)
923 e = ForceConversion (ec, right, TypeManager.int32_type);
930 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
931 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
932 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
933 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
943 Expression ResolveOperator (EmitContext ec)
949 // Step 1: Perform Operator Overload location
951 Expression left_expr, right_expr;
953 string op = "op_" + oper;
955 left_expr = MemberLookup (ec, l, op, false, loc);
956 if (left_expr == null && l.BaseType != null)
957 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
959 right_expr = MemberLookup (ec, r, op, false, loc);
960 if (right_expr == null && r.BaseType != null)
961 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
963 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
966 Arguments = new ArrayList ();
967 Arguments.Add (new Argument (left, Argument.AType.Expression));
968 Arguments.Add (new Argument (right, Argument.AType.Expression));
970 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
971 if (method != null) {
972 MethodInfo mi = (MethodInfo) method;
973 type = mi.ReturnType;
982 // Step 2: Default operations on CLI native types.
985 // Only perform numeric promotions on:
986 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
988 if (oper == Operator.Addition){
990 // If any of the arguments is a string, cast to string
992 if (l == TypeManager.string_type){
993 if (r == TypeManager.string_type){
994 if (left is Literal && right is Literal){
995 StringLiteral ls = (StringLiteral) left;
996 StringLiteral rs = (StringLiteral) right;
998 return new StringLiteral (ls.Value + rs.Value);
1002 method = TypeManager.string_concat_string_string;
1005 method = TypeManager.string_concat_object_object;
1006 right = ConvertImplicit (ec, right,
1007 TypeManager.object_type, loc);
1009 type = TypeManager.string_type;
1011 Arguments = new ArrayList ();
1012 Arguments.Add (new Argument (left, Argument.AType.Expression));
1013 Arguments.Add (new Argument (right, Argument.AType.Expression));
1017 } else if (r == TypeManager.string_type){
1019 method = TypeManager.string_concat_object_object;
1020 Arguments = new ArrayList ();
1021 Arguments.Add (new Argument (left, Argument.AType.Expression));
1022 Arguments.Add (new Argument (right, Argument.AType.Expression));
1024 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1025 type = TypeManager.string_type;
1031 // FIXME: is Delegate operator + (D x, D y) handled?
1035 if (oper == Operator.LeftShift || oper == Operator.RightShift)
1036 return CheckShiftArguments (ec);
1038 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1039 if (l != TypeManager.bool_type || r != TypeManager.bool_type){
1044 type = TypeManager.bool_type;
1048 if (oper == Operator.Equality || oper == Operator.Inequality){
1049 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
1050 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
1055 type = TypeManager.bool_type;
1062 // We are dealing with numbers
1065 if (!DoNumericPromotions (ec, l, r)){
1068 // operator != (object a, object b)
1069 // operator == (object a, object b)
1072 if (oper == Operator.Equality || oper == Operator.Inequality){
1074 li = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1076 ri = ConvertImplicit (ec, right, TypeManager.object_type,
1082 type = TypeManager.bool_type;
1092 if (left == null || right == null)
1096 if (oper == Operator.BitwiseAnd ||
1097 oper == Operator.BitwiseOr ||
1098 oper == Operator.ExclusiveOr){
1099 if (!((l == TypeManager.int32_type) ||
1100 (l == TypeManager.uint32_type) ||
1101 (l == TypeManager.int64_type) ||
1102 (l == TypeManager.uint64_type))){
1109 if (oper == Operator.Equality ||
1110 oper == Operator.Inequality ||
1111 oper == Operator.LessThanOrEqual ||
1112 oper == Operator.LessThan ||
1113 oper == Operator.GreaterThanOrEqual ||
1114 oper == Operator.GreaterThan){
1115 type = TypeManager.bool_type;
1121 public override Expression DoResolve (EmitContext ec)
1123 left = left.Resolve (ec);
1124 right = right.Resolve (ec);
1126 if (left == null || right == null)
1129 if (left.Type == null)
1130 throw new Exception (
1131 "Resolve returned non null, but did not set the type! (" +
1132 left + ") at Line: " + loc.Row);
1133 if (right.Type == null)
1134 throw new Exception (
1135 "Resolve returned non null, but did not set the type! (" +
1136 right + ") at Line: "+ loc.Row);
1138 eclass = ExprClass.Value;
1140 return ResolveOperator (ec);
1143 public bool IsBranchable ()
1145 if (oper == Operator.Equality ||
1146 oper == Operator.Inequality ||
1147 oper == Operator.LessThan ||
1148 oper == Operator.GreaterThan ||
1149 oper == Operator.LessThanOrEqual ||
1150 oper == Operator.GreaterThanOrEqual){
1157 // This entry point is used by routines that might want
1158 // to emit a brfalse/brtrue after an expression, and instead
1159 // they could use a more compact notation.
1161 // Typically the code would generate l.emit/r.emit, followed
1162 // by the comparission and then a brtrue/brfalse. The comparissions
1163 // are sometimes inneficient (there are not as complete as the branches
1164 // look for the hacks in Emit using double ceqs).
1166 // So for those cases we provide EmitBranchable that can emit the
1167 // branch with the test
1169 public void EmitBranchable (EmitContext ec, int target)
1172 bool close_target = false;
1178 case Operator.Equality:
1180 opcode = OpCodes.Beq_S;
1182 opcode = OpCodes.Beq;
1185 case Operator.Inequality:
1187 opcode = OpCodes.Bne_Un_S;
1189 opcode = OpCodes.Bne_Un;
1192 case Operator.LessThan:
1194 opcode = OpCodes.Blt_S;
1196 opcode = OpCodes.Blt;
1199 case Operator.GreaterThan:
1201 opcode = OpCodes.Bgt_S;
1203 opcode = OpCodes.Bgt;
1206 case Operator.LessThanOrEqual:
1208 opcode = OpCodes.Ble_S;
1210 opcode = OpCodes.Ble;
1213 case Operator.GreaterThanOrEqual:
1215 opcode = OpCodes.Bge_S;
1217 opcode = OpCodes.Ble;
1221 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1222 + oper.ToString ());
1225 ec.ig.Emit (opcode, target);
1228 public override void Emit (EmitContext ec)
1230 ILGenerator ig = ec.ig;
1232 Type r = right.Type;
1235 if (method != null) {
1237 // Note that operators are static anyway
1239 if (Arguments != null)
1240 Invocation.EmitArguments (ec, Arguments);
1242 if (method is MethodInfo)
1243 ig.Emit (OpCodes.Call, (MethodInfo) method);
1245 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1254 case Operator.Multiply:
1256 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1257 opcode = OpCodes.Mul_Ovf;
1258 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1259 opcode = OpCodes.Mul_Ovf_Un;
1261 opcode = OpCodes.Mul;
1263 opcode = OpCodes.Mul;
1267 case Operator.Division:
1268 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1269 opcode = OpCodes.Div_Un;
1271 opcode = OpCodes.Div;
1274 case Operator.Modulus:
1275 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1276 opcode = OpCodes.Rem_Un;
1278 opcode = OpCodes.Rem;
1281 case Operator.Addition:
1283 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1284 opcode = OpCodes.Add_Ovf;
1285 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1286 opcode = OpCodes.Add_Ovf_Un;
1288 opcode = OpCodes.Mul;
1290 opcode = OpCodes.Add;
1293 case Operator.Subtraction:
1295 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1296 opcode = OpCodes.Sub_Ovf;
1297 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1298 opcode = OpCodes.Sub_Ovf_Un;
1300 opcode = OpCodes.Sub;
1302 opcode = OpCodes.Sub;
1305 case Operator.RightShift:
1306 opcode = OpCodes.Shr;
1309 case Operator.LeftShift:
1310 opcode = OpCodes.Shl;
1313 case Operator.Equality:
1314 opcode = OpCodes.Ceq;
1317 case Operator.Inequality:
1318 ec.ig.Emit (OpCodes.Ceq);
1319 ec.ig.Emit (OpCodes.Ldc_I4_0);
1321 opcode = OpCodes.Ceq;
1324 case Operator.LessThan:
1325 opcode = OpCodes.Clt;
1328 case Operator.GreaterThan:
1329 opcode = OpCodes.Cgt;
1332 case Operator.LessThanOrEqual:
1333 ec.ig.Emit (OpCodes.Cgt);
1334 ec.ig.Emit (OpCodes.Ldc_I4_0);
1336 opcode = OpCodes.Ceq;
1339 case Operator.GreaterThanOrEqual:
1340 ec.ig.Emit (OpCodes.Clt);
1341 ec.ig.Emit (OpCodes.Ldc_I4_1);
1343 opcode = OpCodes.Sub;
1346 case Operator.LogicalOr:
1347 case Operator.BitwiseOr:
1348 opcode = OpCodes.Or;
1351 case Operator.LogicalAnd:
1352 case Operator.BitwiseAnd:
1353 opcode = OpCodes.And;
1356 case Operator.ExclusiveOr:
1357 opcode = OpCodes.Xor;
1361 throw new Exception ("This should not happen: Operator = "
1362 + oper.ToString ());
1369 // Constant expression reducer for binary operations
1371 public override Expression Reduce (EmitContext ec)
1373 Console.WriteLine ("Reduce called");
1375 left = left.Reduce (ec);
1376 right = right.Reduce (ec);
1378 if (!(left is Literal && right is Literal))
1381 if (method == TypeManager.string_concat_string_string){
1382 StringLiteral ls = (StringLiteral) left;
1383 StringLiteral rs = (StringLiteral) right;
1385 return new StringLiteral (ls.Value + rs.Value);
1394 public class Conditional : Expression {
1395 Expression expr, trueExpr, falseExpr;
1398 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
1401 this.trueExpr = trueExpr;
1402 this.falseExpr = falseExpr;
1406 public Expression Expr {
1412 public Expression TrueExpr {
1418 public Expression FalseExpr {
1424 public override Expression DoResolve (EmitContext ec)
1426 expr = expr.Resolve (ec);
1428 if (expr.Type != TypeManager.bool_type)
1429 expr = Expression.ConvertImplicitRequired (
1430 ec, expr, TypeManager.bool_type, loc);
1432 trueExpr = trueExpr.Resolve (ec);
1433 falseExpr = falseExpr.Resolve (ec);
1435 if (expr == null || trueExpr == null || falseExpr == null)
1438 if (trueExpr.Type == falseExpr.Type)
1439 type = trueExpr.Type;
1444 // First, if an implicit conversion exists from trueExpr
1445 // to falseExpr, then the result type is of type falseExpr.Type
1447 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
1449 type = falseExpr.Type;
1451 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
1452 type = trueExpr.Type;
1455 Error (173, loc, "The type of the conditional expression can " +
1456 "not be computed because there is no implicit conversion" +
1457 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
1458 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
1463 if (expr is BoolLiteral){
1464 BoolLiteral bl = (BoolLiteral) expr;
1472 eclass = ExprClass.Value;
1476 public override void Emit (EmitContext ec)
1478 ILGenerator ig = ec.ig;
1479 Label false_target = ig.DefineLabel ();
1480 Label end_target = ig.DefineLabel ();
1483 ig.Emit (OpCodes.Brfalse, false_target);
1485 ig.Emit (OpCodes.Br, end_target);
1486 ig.MarkLabel (false_target);
1487 falseExpr.Emit (ec);
1488 ig.MarkLabel (end_target);
1491 public override Expression Reduce (EmitContext ec)
1493 expr = expr.Reduce (ec);
1494 trueExpr = trueExpr.Reduce (ec);
1495 falseExpr = falseExpr.Reduce (ec);
1497 if (!(expr is Literal && trueExpr is Literal && falseExpr is Literal))
1500 BoolLiteral bl = (BoolLiteral) expr;
1509 public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
1510 public readonly string Name;
1511 public readonly Block Block;
1513 VariableInfo variable_info;
1515 public LocalVariableReference (Block block, string name)
1519 eclass = ExprClass.Variable;
1522 public VariableInfo VariableInfo {
1524 if (variable_info == null)
1525 variable_info = Block.GetVariableInfo (Name);
1526 return variable_info;
1530 public override Expression DoResolve (EmitContext ec)
1532 VariableInfo vi = VariableInfo;
1534 type = vi.VariableType;
1538 public override void Emit (EmitContext ec)
1540 VariableInfo vi = VariableInfo;
1541 ILGenerator ig = ec.ig;
1548 ig.Emit (OpCodes.Ldloc_0);
1552 ig.Emit (OpCodes.Ldloc_1);
1556 ig.Emit (OpCodes.Ldloc_2);
1560 ig.Emit (OpCodes.Ldloc_3);
1565 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
1567 ig.Emit (OpCodes.Ldloc, idx);
1572 public static void Store (ILGenerator ig, int idx)
1576 ig.Emit (OpCodes.Stloc_0);
1580 ig.Emit (OpCodes.Stloc_1);
1584 ig.Emit (OpCodes.Stloc_2);
1588 ig.Emit (OpCodes.Stloc_3);
1593 ig.Emit (OpCodes.Stloc_S, (byte) idx);
1595 ig.Emit (OpCodes.Stloc, idx);
1600 public void Store (EmitContext ec)
1602 ILGenerator ig = ec.ig;
1603 VariableInfo vi = VariableInfo;
1607 // Funny seems the above generates optimal code for us, but
1608 // seems to take too long to generate what we need.
1609 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
1614 public void AddressOf (EmitContext ec)
1616 VariableInfo vi = VariableInfo;
1623 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
1625 ec.ig.Emit (OpCodes.Ldloca, idx);
1629 public class ParameterReference : Expression, IStackStore, IMemoryLocation {
1630 public readonly Parameters Pars;
1631 public readonly String Name;
1632 public readonly int Idx;
1635 public ParameterReference (Parameters pars, int idx, string name)
1640 eclass = ExprClass.Variable;
1643 public override Expression DoResolve (EmitContext ec)
1645 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
1656 public override void Emit (EmitContext ec)
1659 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
1661 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
1664 public void Store (EmitContext ec)
1667 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
1669 ec.ig.Emit (OpCodes.Starg, arg_idx);
1673 public void AddressOf (EmitContext ec)
1676 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
1678 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
1683 // Used for arguments to New(), Invocation()
1685 public class Argument {
1692 public readonly AType ArgType;
1693 public Expression expr;
1695 public Argument (Expression expr, AType type)
1698 this.ArgType = type;
1701 public Expression Expr {
1717 public Parameter.Modifier GetParameterModifier ()
1719 if (ArgType == AType.Ref)
1720 return Parameter.Modifier.REF;
1722 if (ArgType == AType.Out)
1723 return Parameter.Modifier.OUT;
1725 return Parameter.Modifier.NONE;
1728 public static string FullDesc (Argument a)
1730 StringBuilder sb = new StringBuilder ();
1732 if (a.ArgType == AType.Ref)
1735 if (a.ArgType == AType.Out)
1738 sb.Append (TypeManager.CSharpName (a.Expr.Type));
1740 return sb.ToString ();
1743 public bool Resolve (EmitContext ec)
1745 expr = expr.Resolve (ec);
1747 return expr != null;
1750 public void Emit (EmitContext ec)
1757 // Invocation of methods or delegates.
1759 public class Invocation : ExpressionStatement {
1760 public readonly ArrayList Arguments;
1761 public readonly Location Location;
1764 MethodBase method = null;
1766 static Hashtable method_parameter_cache;
1768 static Invocation ()
1770 method_parameter_cache = new Hashtable ();
1774 // arguments is an ArrayList, but we do not want to typecast,
1775 // as it might be null.
1777 // FIXME: only allow expr to be a method invocation or a
1778 // delegate invocation (7.5.5)
1780 public Invocation (Expression expr, ArrayList arguments, Location l)
1783 Arguments = arguments;
1787 public Expression Expr {
1794 // Returns the Parameters (a ParameterData interface) for the
1797 public static ParameterData GetParameterData (MethodBase mb)
1799 object pd = method_parameter_cache [mb];
1803 return (ParameterData) pd;
1806 ip = TypeContainer.LookupParametersByBuilder (mb);
1808 method_parameter_cache [mb] = ip;
1810 return (ParameterData) ip;
1812 ParameterInfo [] pi = mb.GetParameters ();
1813 ReflectionParameters rp = new ReflectionParameters (pi);
1814 method_parameter_cache [mb] = rp;
1816 return (ParameterData) rp;
1821 // Tells whether a user defined conversion from Type `from' to
1822 // Type `to' exists.
1824 // FIXME: we could implement a cache here.
1826 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
1828 // Locate user-defined implicit operators
1832 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
1835 MethodGroupExpr me = (MethodGroupExpr) mg;
1837 for (int i = me.Methods.Length; i > 0;) {
1839 MethodBase mb = me.Methods [i];
1840 ParameterData pd = GetParameterData (mb);
1842 if (from == pd.ParameterType (0))
1847 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
1850 MethodGroupExpr me = (MethodGroupExpr) mg;
1852 for (int i = me.Methods.Length; i > 0;) {
1854 MethodBase mb = me.Methods [i];
1855 MethodInfo mi = (MethodInfo) mb;
1857 if (mi.ReturnType == to)
1866 // Determines "better conversion" as specified in 7.4.2.3
1867 // Returns : 1 if a->p is better
1868 // 0 if a->q or neither is better
1870 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
1873 Type argument_type = a.Type;
1874 Expression argument_expr = a.Expr;
1876 if (argument_type == null)
1877 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
1882 if (argument_type == p)
1885 if (argument_type == q)
1889 // Now probe whether an implicit constant expression conversion
1892 // An implicit constant expression conversion permits the following
1895 // * A constant-expression of type `int' can be converted to type
1896 // sbyte, byute, short, ushort, uint, ulong provided the value of
1897 // of the expression is withing the range of the destination type.
1899 // * A constant-expression of type long can be converted to type
1900 // ulong, provided the value of the constant expression is not negative
1902 // FIXME: Note that this assumes that constant folding has
1903 // taken place. We dont do constant folding yet.
1906 if (argument_expr is IntLiteral){
1907 IntLiteral ei = (IntLiteral) argument_expr;
1908 int value = ei.Value;
1910 if (p == TypeManager.sbyte_type){
1911 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1913 } else if (p == TypeManager.byte_type){
1914 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1916 } else if (p == TypeManager.short_type){
1917 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1919 } else if (p == TypeManager.ushort_type){
1920 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1922 } else if (p == TypeManager.uint32_type){
1924 // we can optimize this case: a positive int32
1925 // always fits on a uint32
1929 } else if (p == TypeManager.uint64_type){
1931 // we can optimize this case: a positive int32
1932 // always fits on a uint64
1937 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
1938 LongLiteral ll = (LongLiteral) argument_expr;
1940 if (p == TypeManager.uint64_type){
1951 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
1953 tmp = ConvertImplicit (ec, argument_expr, p, loc);
1962 if (ConversionExists (ec, p, q, loc) == true &&
1963 ConversionExists (ec, q, p, loc) == false)
1966 if (p == TypeManager.sbyte_type)
1967 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
1968 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
1971 if (p == TypeManager.short_type)
1972 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
1973 q == TypeManager.uint64_type)
1976 if (p == TypeManager.int32_type)
1977 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
1980 if (p == TypeManager.int64_type)
1981 if (q == TypeManager.uint64_type)
1988 // Determines "Better function" and returns an integer indicating :
1989 // 0 if candidate ain't better
1990 // 1 if candidate is better than the current best match
1992 static int BetterFunction (EmitContext ec, ArrayList args,
1993 MethodBase candidate, MethodBase best,
1994 bool use_standard, Location loc)
1996 ParameterData candidate_pd = GetParameterData (candidate);
1997 ParameterData best_pd;
2003 argument_count = args.Count;
2005 if (candidate_pd.Count == 0 && argument_count == 0)
2009 if (candidate_pd.Count == argument_count) {
2011 for (int j = argument_count; j > 0;) {
2014 Argument a = (Argument) args [j];
2016 x = BetterConversion (
2017 ec, a, candidate_pd.ParameterType (j), null,
2033 best_pd = GetParameterData (best);
2035 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2036 int rating1 = 0, rating2 = 0;
2038 for (int j = argument_count; j > 0;) {
2042 Argument a = (Argument) args [j];
2044 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
2045 best_pd.ParameterType (j), use_standard, loc);
2046 y = BetterConversion (ec, a, best_pd.ParameterType (j),
2047 candidate_pd.ParameterType (j), use_standard,
2054 if (rating1 > rating2)
2063 public static string FullMethodDesc (MethodBase mb)
2065 StringBuilder sb = new StringBuilder (mb.Name);
2066 ParameterData pd = GetParameterData (mb);
2068 int count = pd.Count;
2071 for (int i = count; i > 0; ) {
2074 sb.Append (pd.ParameterDesc (count - i - 1));
2080 return sb.ToString ();
2083 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2085 MemberInfo [] miset;
2086 MethodGroupExpr union;
2088 if (mg1 != null && mg2 != null) {
2090 MethodGroupExpr left_set = null, right_set = null;
2091 int length1 = 0, length2 = 0;
2093 left_set = (MethodGroupExpr) mg1;
2094 length1 = left_set.Methods.Length;
2096 right_set = (MethodGroupExpr) mg2;
2097 length2 = right_set.Methods.Length;
2099 ArrayList common = new ArrayList ();
2101 for (int i = 0; i < left_set.Methods.Length; i++) {
2102 for (int j = 0; j < right_set.Methods.Length; j++) {
2103 if (left_set.Methods [i] == right_set.Methods [j])
2104 common.Add (left_set.Methods [i]);
2108 miset = new MemberInfo [length1 + length2 - common.Count];
2110 left_set.Methods.CopyTo (miset, 0);
2114 for (int j = 0; j < right_set.Methods.Length; j++)
2115 if (!common.Contains (right_set.Methods [j]))
2116 miset [length1 + k++] = right_set.Methods [j];
2118 union = new MethodGroupExpr (miset);
2122 } else if (mg1 == null && mg2 != null) {
2124 MethodGroupExpr me = (MethodGroupExpr) mg2;
2126 miset = new MemberInfo [me.Methods.Length];
2127 me.Methods.CopyTo (miset, 0);
2129 union = new MethodGroupExpr (miset);
2133 } else if (mg2 == null && mg1 != null) {
2135 MethodGroupExpr me = (MethodGroupExpr) mg1;
2137 miset = new MemberInfo [me.Methods.Length];
2138 me.Methods.CopyTo (miset, 0);
2140 union = new MethodGroupExpr (miset);
2149 // Determines is the candidate method, if a params method, is applicable
2150 // in its expanded form to the given set of arguments
2152 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2156 if (arguments == null)
2159 arg_count = arguments.Count;
2161 ParameterData pd = GetParameterData (candidate);
2163 int pd_count = pd.Count;
2165 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2168 if (pd_count - 1 > arg_count)
2171 // If we have come this far, the case which remains is when the number of parameters
2172 // is less than or equal to the argument count. So, we now check if the element type
2173 // of the params array is compatible with each argument type
2176 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2178 for (int i = pd_count - 1; i < arg_count - 1; i++) {
2179 Argument a = (Argument) arguments [i];
2180 if (!StandardConversionExists (a.Type, element_type))
2188 // Determines if the candidate method is applicable (section 14.4.2.1)
2189 // to the given set of arguments
2191 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2195 if (arguments == null)
2198 arg_count = arguments.Count;
2200 ParameterData pd = GetParameterData (candidate);
2202 int pd_count = pd.Count;
2204 if (arg_count != pd.Count)
2207 for (int i = arg_count; i > 0; ) {
2210 Argument a = (Argument) arguments [i];
2212 Parameter.Modifier a_mod = a.GetParameterModifier ();
2213 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2215 if (a_mod == p_mod) {
2217 if (a_mod == Parameter.Modifier.NONE)
2218 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2221 if (a_mod == Parameter.Modifier.REF ||
2222 a_mod == Parameter.Modifier.OUT)
2223 if (pd.ParameterType (i) != a.Type)
2235 // Find the Applicable Function Members (7.4.2.1)
2237 // me: Method Group expression with the members to select.
2238 // it might contain constructors or methods (or anything
2239 // that maps to a method).
2241 // Arguments: ArrayList containing resolved Argument objects.
2243 // loc: The location if we want an error to be reported, or a Null
2244 // location for "probing" purposes.
2246 // use_standard: controls whether OverloadResolve should use the
2247 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
2249 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2250 // that is the best match of me on Arguments.
2253 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2254 ArrayList Arguments, Location loc,
2257 ArrayList afm = new ArrayList ();
2258 int best_match_idx = -1;
2259 MethodBase method = null;
2262 for (int i = me.Methods.Length; i > 0; ){
2264 MethodBase candidate = me.Methods [i];
2267 // Check if candidate is applicable (section 14.4.2.1)
2268 if (!IsApplicable (Arguments, candidate))
2271 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
2277 method = me.Methods [best_match_idx];
2281 if (Arguments == null)
2284 argument_count = Arguments.Count;
2287 // Now we see if we can find params functions, applicable in their expanded form
2288 // since if they were applicable in their normal form, they would have been selected
2291 if (best_match_idx == -1) {
2293 for (int i = me.Methods.Length; i > 0; ) {
2295 MethodBase candidate = me.Methods [i];
2297 if (IsParamsMethodApplicable (Arguments, candidate)) {
2299 method = me.Methods [best_match_idx];
2306 // Now we see if we can at least find a method with the same number of arguments
2310 if (best_match_idx == -1) {
2312 for (int i = me.Methods.Length; i > 0;) {
2314 MethodBase mb = me.Methods [i];
2315 pd = GetParameterData (mb);
2317 if (pd.Count == argument_count) {
2319 method = me.Methods [best_match_idx];
2329 // And now convert implicitly, each argument to the required type
2331 pd = GetParameterData (method);
2332 int pd_count = pd.Count;
2334 for (int j = 0; j < argument_count; j++) {
2336 Argument a = (Argument) Arguments [j];
2337 Expression a_expr = a.Expr;
2338 Type parameter_type = pd.ParameterType (j);
2341 // Note that we need to compare against the element type
2342 // when we have a params method
2344 if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
2345 if (j >= pd_count - 1)
2346 parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
2349 if (a.Type != parameter_type){
2353 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
2356 conv = ConvertImplicit (ec, a_expr, parameter_type,
2360 if (!Location.IsNull (loc)) {
2362 "The best overloaded match for method '" + FullMethodDesc (method)+
2363 "' has some invalid arguments");
2365 "Argument " + (j+1) +
2366 ": Cannot convert from '" + Argument.FullDesc (a)
2367 + "' to '" + pd.ParameterDesc (j) + "'");
2375 // Update the argument with the implicit conversion
2380 // FIXME : For the case of params methods, we need to actually instantiate
2381 // an array and initialize it with the argument values etc etc.
2385 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2386 pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2387 if (!Location.IsNull (loc)) {
2389 "The best overloaded match for method '" + FullMethodDesc (method)+
2390 "' has some invalid arguments");
2392 "Argument " + (j+1) +
2393 ": Cannot convert from '" + Argument.FullDesc (a)
2394 + "' to '" + pd.ParameterDesc (j) + "'");
2405 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2406 ArrayList Arguments, Location loc)
2408 return OverloadResolve (ec, me, Arguments, loc, false);
2411 public override Expression DoResolve (EmitContext ec)
2414 // First, resolve the expression that is used to
2415 // trigger the invocation
2417 expr = expr.Resolve (ec);
2421 if (!(expr is MethodGroupExpr)) {
2422 Type expr_type = expr.Type;
2424 if (expr_type != null){
2425 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2427 return (new DelegateInvocation (
2428 this.expr, Arguments, Location)).Resolve (ec);
2432 if (!(expr is MethodGroupExpr)){
2433 report118 (Location, this.expr, "method group");
2438 // Next, evaluate all the expressions in the argument list
2440 if (Arguments != null){
2441 for (int i = Arguments.Count; i > 0;){
2443 Argument a = (Argument) Arguments [i];
2445 if (!a.Resolve (ec))
2450 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
2453 if (method == null){
2454 Error (-6, Location,
2455 "Could not find any applicable function for this argument list");
2459 if (method is MethodInfo)
2460 type = ((MethodInfo)method).ReturnType;
2462 eclass = ExprClass.Value;
2466 public static void EmitArguments (EmitContext ec, ArrayList Arguments)
2470 if (Arguments != null)
2471 top = Arguments.Count;
2475 for (int i = 0; i < top; i++){
2476 Argument a = (Argument) Arguments [i];
2482 public static void EmitCall (EmitContext ec,
2483 bool is_static, Expression instance_expr,
2484 MethodBase method, ArrayList Arguments)
2486 ILGenerator ig = ec.ig;
2487 bool struct_call = false;
2491 // If this is ourselves, push "this"
2493 if (instance_expr == null){
2494 ig.Emit (OpCodes.Ldarg_0);
2497 // Push the instance expression
2499 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
2504 // If the expression implements IMemoryLocation, then
2505 // we can optimize and use AddressOf on the
2508 // If not we have to use some temporary storage for
2510 if (instance_expr is IMemoryLocation)
2511 ((IMemoryLocation) instance_expr).AddressOf (ec);
2513 Type t = instance_expr.Type;
2515 instance_expr.Emit (ec);
2516 LocalBuilder temp = ec.GetTemporaryStorage (t);
2517 ig.Emit (OpCodes.Stloc, temp);
2518 ig.Emit (OpCodes.Ldloca, temp);
2521 instance_expr.Emit (ec);
2525 if (Arguments != null)
2526 EmitArguments (ec, Arguments);
2528 if (is_static || struct_call){
2529 if (method is MethodInfo)
2530 ig.Emit (OpCodes.Call, (MethodInfo) method);
2532 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2534 if (method is MethodInfo)
2535 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
2537 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
2541 public override void Emit (EmitContext ec)
2543 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2544 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
2547 public override void EmitStatement (EmitContext ec)
2552 // Pop the return value if there is one
2554 if (method is MethodInfo){
2555 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
2556 ec.ig.Emit (OpCodes.Pop);
2561 public class New : ExpressionStatement {
2562 public readonly ArrayList Arguments;
2563 public readonly string RequestedType;
2566 MethodBase method = null;
2569 // If set, the new expression is for a value_target, and
2570 // we will not leave anything on the stack.
2572 Expression value_target;
2574 public New (string requested_type, ArrayList arguments, Location loc)
2576 RequestedType = requested_type;
2577 Arguments = arguments;
2581 public Expression ValueTypeVariable {
2583 return value_target;
2587 value_target = value;
2591 public override Expression DoResolve (EmitContext ec)
2593 type = ec.TypeContainer.LookupType (RequestedType, false);
2598 bool IsDelegate = TypeManager.IsDelegateType (type);
2601 return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
2605 ml = MemberLookup (ec, type, ".ctor", false,
2606 MemberTypes.Constructor, AllBindingsFlags, Location);
2608 bool is_struct = false;
2609 is_struct = type.IsSubclassOf (TypeManager.value_type);
2611 if (! (ml is MethodGroupExpr)){
2613 report118 (Location, ml, "method group");
2619 if (Arguments != null){
2620 for (int i = Arguments.Count; i > 0;){
2622 Argument a = (Argument) Arguments [i];
2624 if (!a.Resolve (ec))
2629 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
2630 Arguments, Location);
2633 if (method == null && !is_struct) {
2634 Error (-6, Location,
2635 "New invocation: Can not find a constructor for " +
2636 "this argument list");
2640 eclass = ExprClass.Value;
2645 // This DoEmit can be invoked in two contexts:
2646 // * As a mechanism that will leave a value on the stack (new object)
2647 // * As one that wont (init struct)
2649 // You can control whether a value is required on the stack by passing
2650 // need_value_on_stack. The code *might* leave a value on the stack
2651 // so it must be popped manually
2653 // Returns whether a value is left on the stack
2655 bool DoEmit (EmitContext ec, bool need_value_on_stack)
2657 if (method == null){
2658 IMemoryLocation ml = (IMemoryLocation) value_target;
2662 Invocation.EmitArguments (ec, Arguments);
2663 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2668 // It must be a value type, sanity check
2670 if (value_target != null){
2671 ec.ig.Emit (OpCodes.Initobj, type);
2673 if (need_value_on_stack){
2674 value_target.Emit (ec);
2680 throw new Exception ("No method and no value type");
2683 public override void Emit (EmitContext ec)
2688 public override void EmitStatement (EmitContext ec)
2690 if (DoEmit (ec, false))
2691 ec.ig.Emit (OpCodes.Pop);
2696 // Represents an array creation expression.
2700 // There are two possible scenarios here: one is an array creation
2701 // expression that specifies the dimensions and optionally the
2702 // initialization data
2704 public class ArrayCreation : ExpressionStatement {
2706 string RequestedType;
2708 ArrayList Initializers;
2710 ArrayList Arguments;
2712 MethodBase method = null;
2713 Type array_element_type;
2714 bool IsOneDimensional = false;
2716 bool IsBuiltinType = false;
2718 public ArrayCreation (string requested_type, ArrayList exprs,
2719 string rank, ArrayList initializers, Location l)
2721 RequestedType = requested_type;
2723 Initializers = initializers;
2726 Arguments = new ArrayList ();
2728 foreach (Expression e in exprs)
2729 Arguments.Add (new Argument (e, Argument.AType.Expression));
2733 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2735 RequestedType = requested_type;
2737 Initializers = initializers;
2741 public static string FormArrayType (string base_type, int idx_count, string rank)
2743 StringBuilder sb = new StringBuilder (base_type);
2748 for (int i = 1; i < idx_count; i++)
2752 return sb.ToString ();
2755 public static string FormElementType (string base_type, int idx_count, string rank)
2757 StringBuilder sb = new StringBuilder (base_type);
2760 for (int i = 1; i < idx_count; i++)
2766 string val = sb.ToString ();
2768 return val.Substring (0, val.LastIndexOf ("["));
2772 public override Expression DoResolve (EmitContext ec)
2776 if (Arguments == null)
2779 arg_count = Arguments.Count;
2781 string array_type = FormArrayType (RequestedType, arg_count, Rank);
2783 string element_type = FormElementType (RequestedType, arg_count, Rank);
2785 type = ec.TypeContainer.LookupType (array_type, false);
2787 array_element_type = ec.TypeContainer.LookupType (element_type, false);
2792 if (arg_count == 1) {
2793 IsOneDimensional = true;
2794 eclass = ExprClass.Value;
2798 IsBuiltinType = TypeManager.IsBuiltinType (type);
2800 if (IsBuiltinType) {
2804 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
2805 AllBindingsFlags, Location);
2807 if (!(ml is MethodGroupExpr)){
2808 report118 (Location, ml, "method group");
2813 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
2814 "this argument list");
2818 if (Arguments != null) {
2819 for (int i = arg_count; i > 0;){
2821 Argument a = (Argument) Arguments [i];
2823 if (!a.Resolve (ec))
2828 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, Location);
2830 if (method == null) {
2831 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
2832 "this argument list");
2836 eclass = ExprClass.Value;
2841 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
2843 ArrayList args = new ArrayList ();
2844 if (Arguments != null){
2845 for (int i = arg_count; i > 0;){
2847 Argument a = (Argument) Arguments [i];
2849 if (!a.Resolve (ec))
2856 Type [] arg_types = null;
2859 arg_types = new Type [args.Count];
2861 args.CopyTo (arg_types, 0);
2863 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
2866 if (method == null) {
2867 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
2868 "this argument list");
2872 eclass = ExprClass.Value;
2878 public override void Emit (EmitContext ec)
2880 ILGenerator ig = ec.ig;
2882 if (IsOneDimensional) {
2883 Invocation.EmitArguments (ec, Arguments);
2884 ig.Emit (OpCodes.Newarr, array_element_type);
2887 Invocation.EmitArguments (ec, Arguments);
2890 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2892 ig.Emit (OpCodes.Newobj, (MethodInfo) method);
2895 if (Initializers != null){
2898 // FIXME: This is just sample data, need to fill with
2900 byte [] a = new byte [4] { 1, 2, 3, 4 };
2902 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
2904 ig.Emit (OpCodes.Dup);
2905 ig.Emit (OpCodes.Ldtoken, fb);
2906 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
2910 public override void EmitStatement (EmitContext ec)
2913 ec.ig.Emit (OpCodes.Pop);
2919 // Represents the `this' construct
2921 public class This : Expression, IStackStore, IMemoryLocation {
2924 public This (Location loc)
2929 public override Expression DoResolve (EmitContext ec)
2931 eclass = ExprClass.Variable;
2932 type = ec.TypeContainer.TypeBuilder;
2935 Report.Error (26, loc,
2936 "Keyword this not valid in static code");
2943 public Expression DoResolveLValue (EmitContext ec)
2947 if (ec.TypeContainer is Class){
2948 Report.Error (1604, loc, "Cannot assign to `this'");
2955 public override void Emit (EmitContext ec)
2957 ec.ig.Emit (OpCodes.Ldarg_0);
2960 public void Store (EmitContext ec)
2962 ec.ig.Emit (OpCodes.Starg, 0);
2965 public void AddressOf (EmitContext ec)
2967 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
2972 // Implements the typeof operator
2974 public class TypeOf : Expression {
2975 public readonly string QueriedType;
2978 public TypeOf (string queried_type)
2980 QueriedType = queried_type;
2983 public override Expression DoResolve (EmitContext ec)
2985 typearg = ec.TypeContainer.LookupType (QueriedType, false);
2987 if (typearg == null)
2990 type = TypeManager.type_type;
2991 eclass = ExprClass.Type;
2995 public override void Emit (EmitContext ec)
2997 ec.ig.Emit (OpCodes.Ldtoken, typearg);
2998 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3002 public class SizeOf : Expression {
3003 public readonly string QueriedType;
3005 public SizeOf (string queried_type)
3007 this.QueriedType = queried_type;
3010 public override Expression DoResolve (EmitContext ec)
3012 // FIXME: Implement;
3013 throw new Exception ("Unimplemented");
3017 public override void Emit (EmitContext ec)
3019 throw new Exception ("Implement me");
3023 public class MemberAccess : Expression {
3024 public readonly string Identifier;
3026 Expression member_lookup;
3029 public MemberAccess (Expression expr, string id, Location l)
3036 public Expression Expr {
3042 void error176 (Location loc, string name)
3044 Report.Error (176, loc, "Static member `" +
3045 name + "' cannot be accessed " +
3046 "with an instance reference, qualify with a " +
3047 "type name instead");
3050 public override Expression DoResolve (EmitContext ec)
3053 // We are the sole users of ResolveWithSimpleName (ie, the only
3054 // ones that can cope with it
3056 expr = expr.ResolveWithSimpleName (ec);
3061 if (expr is SimpleName){
3062 SimpleName child_expr = (SimpleName) expr;
3064 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3066 return expr.Resolve (ec);
3069 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3071 if (member_lookup == null)
3077 if (member_lookup is MethodGroupExpr){
3078 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3083 if (expr is TypeExpr){
3084 if (!mg.RemoveInstanceMethods ()){
3085 SimpleName.Error120 (loc, mg.Methods [0].Name);
3089 return member_lookup;
3093 // Instance.MethodGroup
3095 if (!mg.RemoveStaticMethods ()){
3096 error176 (loc, mg.Methods [0].Name);
3100 mg.InstanceExpression = expr;
3102 return member_lookup;
3105 if (member_lookup is FieldExpr){
3106 FieldExpr fe = (FieldExpr) member_lookup;
3107 FieldInfo fi = fe.FieldInfo;
3110 Type t = fi.FieldType;
3111 object o = fi.GetValue (null);
3113 if (t.IsSubclassOf (TypeManager.enum_type)) {
3114 Expression enum_member = MemberLookup (ec, t, "value__", false, loc);
3115 Type underlying_type = enum_member.Type;
3117 Expression e = Literalize (o, underlying_type);
3120 return new EnumLiteral (e, t);
3123 Expression exp = Literalize (o, t);
3129 if (expr is TypeExpr){
3130 if (!fe.FieldInfo.IsStatic){
3131 error176 (loc, fe.FieldInfo.Name);
3134 return member_lookup;
3136 if (fe.FieldInfo.IsStatic){
3137 error176 (loc, fe.FieldInfo.Name);
3140 fe.InstanceExpression = expr;
3146 if (member_lookup is PropertyExpr){
3147 PropertyExpr pe = (PropertyExpr) member_lookup;
3149 if (expr is TypeExpr){
3151 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3157 error176 (loc, pe.PropertyInfo.Name);
3160 pe.InstanceExpression = expr;
3166 Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3167 Environment.Exit (0);
3171 public override void Emit (EmitContext ec)
3173 throw new Exception ("Should not happen I think");
3178 public class CheckedExpr : Expression {
3180 public Expression Expr;
3182 public CheckedExpr (Expression e)
3187 public override Expression DoResolve (EmitContext ec)
3189 Expr = Expr.Resolve (ec);
3194 eclass = Expr.ExprClass;
3199 public override void Emit (EmitContext ec)
3201 bool last_check = ec.CheckState;
3203 ec.CheckState = true;
3205 ec.CheckState = last_check;
3210 public class UnCheckedExpr : Expression {
3212 public Expression Expr;
3214 public UnCheckedExpr (Expression e)
3219 public override Expression DoResolve (EmitContext ec)
3221 Expr = Expr.Resolve (ec);
3226 eclass = Expr.ExprClass;
3231 public override void Emit (EmitContext ec)
3233 bool last_check = ec.CheckState;
3235 ec.CheckState = false;
3237 ec.CheckState = last_check;
3242 public class ElementAccess : Expression {
3243 public ArrayList Arguments;
3244 public Expression Expr;
3245 public Location loc;
3247 public ElementAccess (Expression e, ArrayList e_list, Location l)
3251 Arguments = new ArrayList ();
3252 foreach (Expression tmp in e_list)
3253 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3258 bool CommonResolve (EmitContext ec)
3260 Expr = Expr.Resolve (ec);
3265 if (Arguments == null)
3268 for (int i = Arguments.Count; i > 0;){
3270 Argument a = (Argument) Arguments [i];
3272 if (!a.Resolve (ec))
3279 public override Expression DoResolve (EmitContext ec)
3281 if (!CommonResolve (ec))
3285 // We perform some simple tests, and then to "split" the emit and store
3286 // code we create an instance of a different class, and return that.
3288 // I am experimenting with this pattern.
3290 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3291 return (new ArrayAccess (this)).Resolve (ec);
3293 return (new IndexerAccess (this)).Resolve (ec);
3296 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3298 if (!CommonResolve (ec))
3301 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3302 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3304 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3307 public override void Emit (EmitContext ec)
3309 throw new Exception ("Should never be reached");
3314 // Implements array access
3316 public class ArrayAccess : Expression, IAssignMethod {
3318 // Points to our "data" repository
3322 public ArrayAccess (ElementAccess ea_data)
3325 eclass = ExprClass.Variable;
3328 public override Expression DoResolve (EmitContext ec)
3330 if (ea.Expr.ExprClass != ExprClass.Variable) {
3331 report118 (ea.loc, ea.Expr, "variable");
3335 Type t = ea.Expr.Type;
3337 if (t.GetArrayRank () != ea.Arguments.Count){
3338 Report.Error (22, ea.loc,
3339 "Incorrect number of indexes for array " +
3340 " expected: " + t.GetArrayRank () + " got: " +
3341 ea.Arguments.Count);
3344 type = t.GetElementType ();
3345 eclass = ExprClass.Variable;
3350 public override void Emit (EmitContext ec)
3352 int rank = ea.Expr.Type.GetArrayRank ();
3353 ILGenerator ig = ec.ig;
3357 foreach (Argument a in ea.Arguments)
3361 if (type == TypeManager.byte_type)
3362 ig.Emit (OpCodes.Ldelem_I1);
3363 else if (type == TypeManager.sbyte_type)
3364 ig.Emit (OpCodes.Ldelem_U1);
3365 else if (type == TypeManager.short_type)
3366 ig.Emit (OpCodes.Ldelem_I2);
3367 else if (type == TypeManager.ushort_type)
3368 ig.Emit (OpCodes.Ldelem_U2);
3369 else if (type == TypeManager.int32_type)
3370 ig.Emit (OpCodes.Ldelem_I4);
3371 else if (type == TypeManager.uint32_type)
3372 ig.Emit (OpCodes.Ldelem_U4);
3373 else if (type == TypeManager.uint64_type)
3374 ig.Emit (OpCodes.Ldelem_I8);
3375 else if (type == TypeManager.int64_type)
3376 ig.Emit (OpCodes.Ldelem_I8);
3377 else if (type == TypeManager.float_type)
3378 ig.Emit (OpCodes.Ldelem_R4);
3379 else if (type == TypeManager.double_type)
3380 ig.Emit (OpCodes.Ldelem_R8);
3381 else if (type == TypeManager.intptr_type)
3382 ig.Emit (OpCodes.Ldelem_I);
3384 ig.Emit (OpCodes.Ldelem_Ref);
3386 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3387 Type [] args = new Type [ea.Arguments.Count];
3392 foreach (Argument a in ea.Arguments)
3393 args [i++] = a.Type;
3395 get = mb.GetArrayMethod (
3396 ea.Expr.Type, "Get",
3397 CallingConventions.HasThis |
3398 CallingConventions.Standard,
3401 ig.Emit (OpCodes.Call, get);
3405 public void EmitAssign (EmitContext ec, Expression source)
3407 int rank = ea.Expr.Type.GetArrayRank ();
3408 ILGenerator ig = ec.ig;
3412 foreach (Argument a in ea.Arguments)
3417 Type t = source.Type;
3419 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3420 ig.Emit (OpCodes.Stelem_I1);
3421 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3422 ig.Emit (OpCodes.Stelem_I2);
3423 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3424 ig.Emit (OpCodes.Stelem_I4);
3425 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3426 ig.Emit (OpCodes.Stelem_I8);
3427 else if (t == TypeManager.float_type)
3428 ig.Emit (OpCodes.Stelem_R4);
3429 else if (t == TypeManager.double_type)
3430 ig.Emit (OpCodes.Stelem_R8);
3431 else if (t == TypeManager.intptr_type)
3432 ig.Emit (OpCodes.Stelem_I);
3434 ig.Emit (OpCodes.Stelem_Ref);
3436 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3437 Type [] args = new Type [ea.Arguments.Count + 1];
3442 foreach (Argument a in ea.Arguments)
3443 args [i++] = a.Type;
3447 set = mb.GetArrayMethod (
3448 ea.Expr.Type, "Set",
3449 CallingConventions.HasThis |
3450 CallingConventions.Standard,
3451 TypeManager.void_type, args);
3453 ig.Emit (OpCodes.Call, set);
3458 public ArrayList getters, setters;
3459 static Hashtable map;
3463 map = new Hashtable ();
3466 Indexers (MemberInfo [] mi)
3468 foreach (PropertyInfo property in mi){
3469 MethodInfo get, set;
3471 get = property.GetGetMethod (true);
3473 if (getters == null)
3474 getters = new ArrayList ();
3479 set = property.GetSetMethod (true);
3481 if (setters == null)
3482 setters = new ArrayList ();
3488 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
3490 Indexers ix = (Indexers) map [t];
3491 string p_name = TypeManager.IndexerPropertyName (t);
3496 MemberInfo [] mi = tm.FindMembers (
3497 t, MemberTypes.Property,
3498 BindingFlags.Public | BindingFlags.Instance,
3499 Type.FilterName, p_name);
3501 if (mi == null || mi.Length == 0){
3502 Report.Error (21, loc,
3503 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3504 "any indexers defined");
3508 ix = new Indexers (mi);
3515 public class IndexerAccess : Expression, IAssignMethod {
3517 // Points to our "data" repository
3520 MethodInfo get, set;
3522 ArrayList set_arguments;
3524 public IndexerAccess (ElementAccess ea_data)
3527 eclass = ExprClass.Value;
3530 public bool VerifyAssignable (Expression source)
3532 throw new Exception ("Implement me!");
3535 public override Expression DoResolve (EmitContext ec)
3537 Type indexer_type = ea.Expr.Type;
3540 // Step 1: Query for all `Item' *properties*. Notice
3541 // that the actual methods are pointed from here.
3543 // This is a group of properties, piles of them.
3546 ilist = Indexers.GetIndexersForType (
3547 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3549 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3550 get = (MethodInfo) Invocation.OverloadResolve (
3551 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3554 Report.Error (154, ea.loc,
3555 "indexer can not be used in this context, because " +
3556 "it lacks a `get' accessor");
3560 type = get.ReturnType;
3561 eclass = ExprClass.Value;
3565 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3567 Type indexer_type = ea.Expr.Type;
3568 Type right_type = right_side.Type;
3571 ilist = Indexers.GetIndexersForType (
3572 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3574 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3575 set_arguments = (ArrayList) ea.Arguments.Clone ();
3576 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3578 set = (MethodInfo) Invocation.OverloadResolve (
3579 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3583 Report.Error (200, ea.loc,
3584 "indexer X.this [" + TypeManager.CSharpName (right_type) +
3585 "] lacks a `set' accessor");
3589 type = TypeManager.void_type;
3590 eclass = ExprClass.IndexerAccess;
3594 public override void Emit (EmitContext ec)
3596 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3600 // source is ignored, because we already have a copy of it from the
3601 // LValue resolution and we have already constructed a pre-cached
3602 // version of the arguments (ea.set_arguments);
3604 public void EmitAssign (EmitContext ec, Expression source)
3606 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3610 public class BaseAccess : Expression {
3612 public enum BaseAccessType {
3617 public readonly BaseAccessType BAType;
3618 public readonly string Member;
3619 public readonly ArrayList Arguments;
3621 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3629 public override Expression DoResolve (EmitContext ec)
3631 // FIXME: Implement;
3632 throw new Exception ("Unimplemented");
3636 public override void Emit (EmitContext ec)
3638 throw new Exception ("Unimplemented");
3643 // This class exists solely to pass the Type around and to be a dummy
3644 // that can be passed to the conversion functions (this is used by
3645 // foreach implementation to typecast the object return value from
3646 // get_Current into the proper type. All code has been generated and
3647 // we only care about the side effect conversions to be performed
3650 public class EmptyExpression : Expression {
3651 public EmptyExpression ()
3653 type = TypeManager.object_type;
3654 eclass = ExprClass.Value;
3657 public override Expression DoResolve (EmitContext ec)
3662 public override void Emit (EmitContext ec)
3664 // nothing, as we only exist to not do anything.
3668 public class UserCast : Expression {
3672 public UserCast (MethodInfo method, Expression source)
3674 this.method = method;
3675 this.source = source;
3676 type = method.ReturnType;
3677 eclass = ExprClass.Value;
3680 public override Expression DoResolve (EmitContext ec)
3683 // We are born fully resolved
3688 public override void Emit (EmitContext ec)
3690 ILGenerator ig = ec.ig;
3694 if (method is MethodInfo)
3695 ig.Emit (OpCodes.Call, (MethodInfo) method);
3697 ig.Emit (OpCodes.Call, (ConstructorInfo) method);