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)
1374 left = left.Reduce (ec);
1375 right = right.Reduce (ec);
1377 if (!(left is Literal && right is Literal))
1380 if (method == TypeManager.string_concat_string_string){
1381 StringLiteral ls = (StringLiteral) left;
1382 StringLiteral rs = (StringLiteral) right;
1384 return new StringLiteral (ls.Value + rs.Value);
1393 public class Conditional : Expression {
1394 Expression expr, trueExpr, falseExpr;
1397 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
1400 this.trueExpr = trueExpr;
1401 this.falseExpr = falseExpr;
1405 public Expression Expr {
1411 public Expression TrueExpr {
1417 public Expression FalseExpr {
1423 public override Expression DoResolve (EmitContext ec)
1425 expr = expr.Resolve (ec);
1427 if (expr.Type != TypeManager.bool_type)
1428 expr = Expression.ConvertImplicitRequired (
1429 ec, expr, TypeManager.bool_type, loc);
1431 trueExpr = trueExpr.Resolve (ec);
1432 falseExpr = falseExpr.Resolve (ec);
1434 if (expr == null || trueExpr == null || falseExpr == null)
1437 if (trueExpr.Type == falseExpr.Type)
1438 type = trueExpr.Type;
1443 // First, if an implicit conversion exists from trueExpr
1444 // to falseExpr, then the result type is of type falseExpr.Type
1446 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
1448 type = falseExpr.Type;
1450 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
1451 type = trueExpr.Type;
1454 Error (173, loc, "The type of the conditional expression can " +
1455 "not be computed because there is no implicit conversion" +
1456 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
1457 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
1462 if (expr is BoolLiteral){
1463 BoolLiteral bl = (BoolLiteral) expr;
1471 eclass = ExprClass.Value;
1475 public override void Emit (EmitContext ec)
1477 ILGenerator ig = ec.ig;
1478 Label false_target = ig.DefineLabel ();
1479 Label end_target = ig.DefineLabel ();
1482 ig.Emit (OpCodes.Brfalse, false_target);
1484 ig.Emit (OpCodes.Br, end_target);
1485 ig.MarkLabel (false_target);
1486 falseExpr.Emit (ec);
1487 ig.MarkLabel (end_target);
1490 public override Expression Reduce (EmitContext ec)
1492 expr = expr.Reduce (ec);
1493 trueExpr = trueExpr.Reduce (ec);
1494 falseExpr = falseExpr.Reduce (ec);
1496 if (!(expr is Literal && trueExpr is Literal && falseExpr is Literal))
1499 BoolLiteral bl = (BoolLiteral) expr;
1508 public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
1509 public readonly string Name;
1510 public readonly Block Block;
1512 VariableInfo variable_info;
1514 public LocalVariableReference (Block block, string name)
1518 eclass = ExprClass.Variable;
1521 public VariableInfo VariableInfo {
1523 if (variable_info == null)
1524 variable_info = Block.GetVariableInfo (Name);
1525 return variable_info;
1529 public override Expression DoResolve (EmitContext ec)
1531 VariableInfo vi = VariableInfo;
1533 type = vi.VariableType;
1537 public override void Emit (EmitContext ec)
1539 VariableInfo vi = VariableInfo;
1540 ILGenerator ig = ec.ig;
1547 ig.Emit (OpCodes.Ldloc_0);
1551 ig.Emit (OpCodes.Ldloc_1);
1555 ig.Emit (OpCodes.Ldloc_2);
1559 ig.Emit (OpCodes.Ldloc_3);
1564 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
1566 ig.Emit (OpCodes.Ldloc, idx);
1571 public static void Store (ILGenerator ig, int idx)
1575 ig.Emit (OpCodes.Stloc_0);
1579 ig.Emit (OpCodes.Stloc_1);
1583 ig.Emit (OpCodes.Stloc_2);
1587 ig.Emit (OpCodes.Stloc_3);
1592 ig.Emit (OpCodes.Stloc_S, (byte) idx);
1594 ig.Emit (OpCodes.Stloc, idx);
1599 public void Store (EmitContext ec)
1601 ILGenerator ig = ec.ig;
1602 VariableInfo vi = VariableInfo;
1606 // Funny seems the above generates optimal code for us, but
1607 // seems to take too long to generate what we need.
1608 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
1613 public void AddressOf (EmitContext ec)
1615 VariableInfo vi = VariableInfo;
1622 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
1624 ec.ig.Emit (OpCodes.Ldloca, idx);
1628 public class ParameterReference : Expression, IStackStore, IMemoryLocation {
1629 public readonly Parameters Pars;
1630 public readonly String Name;
1631 public readonly int Idx;
1634 public ParameterReference (Parameters pars, int idx, string name)
1639 eclass = ExprClass.Variable;
1642 public override Expression DoResolve (EmitContext ec)
1644 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
1655 public override void Emit (EmitContext ec)
1658 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
1660 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
1663 public void Store (EmitContext ec)
1666 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
1668 ec.ig.Emit (OpCodes.Starg, arg_idx);
1672 public void AddressOf (EmitContext ec)
1675 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
1677 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
1682 // Used for arguments to New(), Invocation()
1684 public class Argument {
1691 public readonly AType ArgType;
1692 public Expression expr;
1694 public Argument (Expression expr, AType type)
1697 this.ArgType = type;
1700 public Expression Expr {
1716 public Parameter.Modifier GetParameterModifier ()
1718 if (ArgType == AType.Ref)
1719 return Parameter.Modifier.REF;
1721 if (ArgType == AType.Out)
1722 return Parameter.Modifier.OUT;
1724 return Parameter.Modifier.NONE;
1727 public static string FullDesc (Argument a)
1729 StringBuilder sb = new StringBuilder ();
1731 if (a.ArgType == AType.Ref)
1734 if (a.ArgType == AType.Out)
1737 sb.Append (TypeManager.CSharpName (a.Expr.Type));
1739 return sb.ToString ();
1742 public bool Resolve (EmitContext ec)
1744 expr = expr.Resolve (ec);
1746 return expr != null;
1749 public void Emit (EmitContext ec)
1756 // Invocation of methods or delegates.
1758 public class Invocation : ExpressionStatement {
1759 public readonly ArrayList Arguments;
1760 public readonly Location Location;
1763 MethodBase method = null;
1765 static Hashtable method_parameter_cache;
1767 static Invocation ()
1769 method_parameter_cache = new Hashtable ();
1773 // arguments is an ArrayList, but we do not want to typecast,
1774 // as it might be null.
1776 // FIXME: only allow expr to be a method invocation or a
1777 // delegate invocation (7.5.5)
1779 public Invocation (Expression expr, ArrayList arguments, Location l)
1782 Arguments = arguments;
1786 public Expression Expr {
1793 // Returns the Parameters (a ParameterData interface) for the
1796 public static ParameterData GetParameterData (MethodBase mb)
1798 object pd = method_parameter_cache [mb];
1802 return (ParameterData) pd;
1805 ip = TypeContainer.LookupParametersByBuilder (mb);
1807 method_parameter_cache [mb] = ip;
1809 return (ParameterData) ip;
1811 ParameterInfo [] pi = mb.GetParameters ();
1812 ReflectionParameters rp = new ReflectionParameters (pi);
1813 method_parameter_cache [mb] = rp;
1815 return (ParameterData) rp;
1820 // Tells whether a user defined conversion from Type `from' to
1821 // Type `to' exists.
1823 // FIXME: we could implement a cache here.
1825 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
1827 // Locate user-defined implicit operators
1831 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
1834 MethodGroupExpr me = (MethodGroupExpr) mg;
1836 for (int i = me.Methods.Length; i > 0;) {
1838 MethodBase mb = me.Methods [i];
1839 ParameterData pd = GetParameterData (mb);
1841 if (from == pd.ParameterType (0))
1846 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
1849 MethodGroupExpr me = (MethodGroupExpr) mg;
1851 for (int i = me.Methods.Length; i > 0;) {
1853 MethodBase mb = me.Methods [i];
1854 MethodInfo mi = (MethodInfo) mb;
1856 if (mi.ReturnType == to)
1865 // Determines "better conversion" as specified in 7.4.2.3
1866 // Returns : 1 if a->p is better
1867 // 0 if a->q or neither is better
1869 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
1872 Type argument_type = a.Type;
1873 Expression argument_expr = a.Expr;
1875 if (argument_type == null)
1876 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
1881 if (argument_type == p)
1884 if (argument_type == q)
1888 // Now probe whether an implicit constant expression conversion
1891 // An implicit constant expression conversion permits the following
1894 // * A constant-expression of type `int' can be converted to type
1895 // sbyte, byute, short, ushort, uint, ulong provided the value of
1896 // of the expression is withing the range of the destination type.
1898 // * A constant-expression of type long can be converted to type
1899 // ulong, provided the value of the constant expression is not negative
1901 // FIXME: Note that this assumes that constant folding has
1902 // taken place. We dont do constant folding yet.
1905 if (argument_expr is IntLiteral){
1906 IntLiteral ei = (IntLiteral) argument_expr;
1907 int value = ei.Value;
1909 if (p == TypeManager.sbyte_type){
1910 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1912 } else if (p == TypeManager.byte_type){
1913 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1915 } else if (p == TypeManager.short_type){
1916 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1918 } else if (p == TypeManager.ushort_type){
1919 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1921 } else if (p == TypeManager.uint32_type){
1923 // we can optimize this case: a positive int32
1924 // always fits on a uint32
1928 } else if (p == TypeManager.uint64_type){
1930 // we can optimize this case: a positive int32
1931 // always fits on a uint64
1936 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
1937 LongLiteral ll = (LongLiteral) argument_expr;
1939 if (p == TypeManager.uint64_type){
1950 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
1952 tmp = ConvertImplicit (ec, argument_expr, p, loc);
1961 if (ConversionExists (ec, p, q, loc) == true &&
1962 ConversionExists (ec, q, p, loc) == false)
1965 if (p == TypeManager.sbyte_type)
1966 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
1967 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
1970 if (p == TypeManager.short_type)
1971 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
1972 q == TypeManager.uint64_type)
1975 if (p == TypeManager.int32_type)
1976 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
1979 if (p == TypeManager.int64_type)
1980 if (q == TypeManager.uint64_type)
1987 // Determines "Better function" and returns an integer indicating :
1988 // 0 if candidate ain't better
1989 // 1 if candidate is better than the current best match
1991 static int BetterFunction (EmitContext ec, ArrayList args,
1992 MethodBase candidate, MethodBase best,
1993 bool use_standard, Location loc)
1995 ParameterData candidate_pd = GetParameterData (candidate);
1996 ParameterData best_pd;
2002 argument_count = args.Count;
2004 if (candidate_pd.Count == 0 && argument_count == 0)
2008 if (candidate_pd.Count == argument_count) {
2010 for (int j = argument_count; j > 0;) {
2013 Argument a = (Argument) args [j];
2015 x = BetterConversion (
2016 ec, a, candidate_pd.ParameterType (j), null,
2032 best_pd = GetParameterData (best);
2034 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2035 int rating1 = 0, rating2 = 0;
2037 for (int j = argument_count; j > 0;) {
2041 Argument a = (Argument) args [j];
2043 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
2044 best_pd.ParameterType (j), use_standard, loc);
2045 y = BetterConversion (ec, a, best_pd.ParameterType (j),
2046 candidate_pd.ParameterType (j), use_standard,
2053 if (rating1 > rating2)
2062 public static string FullMethodDesc (MethodBase mb)
2064 StringBuilder sb = new StringBuilder (mb.Name);
2065 ParameterData pd = GetParameterData (mb);
2067 int count = pd.Count;
2070 for (int i = count; i > 0; ) {
2073 sb.Append (pd.ParameterDesc (count - i - 1));
2079 return sb.ToString ();
2082 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2084 MemberInfo [] miset;
2085 MethodGroupExpr union;
2087 if (mg1 != null && mg2 != null) {
2089 MethodGroupExpr left_set = null, right_set = null;
2090 int length1 = 0, length2 = 0;
2092 left_set = (MethodGroupExpr) mg1;
2093 length1 = left_set.Methods.Length;
2095 right_set = (MethodGroupExpr) mg2;
2096 length2 = right_set.Methods.Length;
2098 ArrayList common = new ArrayList ();
2100 for (int i = 0; i < left_set.Methods.Length; i++) {
2101 for (int j = 0; j < right_set.Methods.Length; j++) {
2102 if (left_set.Methods [i] == right_set.Methods [j])
2103 common.Add (left_set.Methods [i]);
2107 miset = new MemberInfo [length1 + length2 - common.Count];
2109 left_set.Methods.CopyTo (miset, 0);
2113 for (int j = 0; j < right_set.Methods.Length; j++)
2114 if (!common.Contains (right_set.Methods [j]))
2115 miset [length1 + k++] = right_set.Methods [j];
2117 union = new MethodGroupExpr (miset);
2121 } else if (mg1 == null && mg2 != null) {
2123 MethodGroupExpr me = (MethodGroupExpr) mg2;
2125 miset = new MemberInfo [me.Methods.Length];
2126 me.Methods.CopyTo (miset, 0);
2128 union = new MethodGroupExpr (miset);
2132 } else if (mg2 == null && mg1 != null) {
2134 MethodGroupExpr me = (MethodGroupExpr) mg1;
2136 miset = new MemberInfo [me.Methods.Length];
2137 me.Methods.CopyTo (miset, 0);
2139 union = new MethodGroupExpr (miset);
2148 // Determines is the candidate method, if a params method, is applicable
2149 // in its expanded form to the given set of arguments
2151 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2155 if (arguments == null)
2158 arg_count = arguments.Count;
2160 ParameterData pd = GetParameterData (candidate);
2162 int pd_count = pd.Count;
2164 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2167 if (pd_count - 1 > arg_count)
2170 // If we have come this far, the case which remains is when the number of parameters
2171 // is less than or equal to the argument count. So, we now check if the element type
2172 // of the params array is compatible with each argument type
2175 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2177 for (int i = pd_count - 1; i < arg_count - 1; i++) {
2178 Argument a = (Argument) arguments [i];
2179 if (!StandardConversionExists (a.Type, element_type))
2187 // Determines if the candidate method is applicable (section 14.4.2.1)
2188 // to the given set of arguments
2190 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2194 if (arguments == null)
2197 arg_count = arguments.Count;
2199 ParameterData pd = GetParameterData (candidate);
2201 int pd_count = pd.Count;
2203 if (arg_count != pd.Count)
2206 for (int i = arg_count; i > 0; ) {
2209 Argument a = (Argument) arguments [i];
2211 Parameter.Modifier a_mod = a.GetParameterModifier ();
2212 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2214 if (a_mod == p_mod) {
2216 if (a_mod == Parameter.Modifier.NONE)
2217 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2220 if (a_mod == Parameter.Modifier.REF ||
2221 a_mod == Parameter.Modifier.OUT)
2222 if (pd.ParameterType (i) != a.Type)
2234 // Find the Applicable Function Members (7.4.2.1)
2236 // me: Method Group expression with the members to select.
2237 // it might contain constructors or methods (or anything
2238 // that maps to a method).
2240 // Arguments: ArrayList containing resolved Argument objects.
2242 // loc: The location if we want an error to be reported, or a Null
2243 // location for "probing" purposes.
2245 // use_standard: controls whether OverloadResolve should use the
2246 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
2248 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2249 // that is the best match of me on Arguments.
2252 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2253 ArrayList Arguments, Location loc,
2256 ArrayList afm = new ArrayList ();
2257 int best_match_idx = -1;
2258 MethodBase method = null;
2261 for (int i = me.Methods.Length; i > 0; ){
2263 MethodBase candidate = me.Methods [i];
2266 // Check if candidate is applicable (section 14.4.2.1)
2267 if (!IsApplicable (Arguments, candidate))
2270 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
2276 method = me.Methods [best_match_idx];
2280 if (Arguments == null)
2283 argument_count = Arguments.Count;
2286 // Now we see if we can find params functions, applicable in their expanded form
2287 // since if they were applicable in their normal form, they would have been selected
2290 if (best_match_idx == -1) {
2292 for (int i = me.Methods.Length; i > 0; ) {
2294 MethodBase candidate = me.Methods [i];
2296 if (IsParamsMethodApplicable (Arguments, candidate)) {
2298 method = me.Methods [best_match_idx];
2305 // Now we see if we can at least find a method with the same number of arguments
2309 if (best_match_idx == -1) {
2311 for (int i = me.Methods.Length; i > 0;) {
2313 MethodBase mb = me.Methods [i];
2314 pd = GetParameterData (mb);
2316 if (pd.Count == argument_count) {
2318 method = me.Methods [best_match_idx];
2328 // And now convert implicitly, each argument to the required type
2330 pd = GetParameterData (method);
2331 int pd_count = pd.Count;
2333 for (int j = 0; j < argument_count; j++) {
2335 Argument a = (Argument) Arguments [j];
2336 Expression a_expr = a.Expr;
2337 Type parameter_type = pd.ParameterType (j);
2340 // Note that we need to compare against the element type
2341 // when we have a params method
2343 if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
2344 if (j >= pd_count - 1)
2345 parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
2348 if (a.Type != parameter_type){
2352 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
2355 conv = ConvertImplicit (ec, a_expr, parameter_type,
2359 if (!Location.IsNull (loc)) {
2361 "The best overloaded match for method '" + FullMethodDesc (method)+
2362 "' has some invalid arguments");
2364 "Argument " + (j+1) +
2365 ": Cannot convert from '" + Argument.FullDesc (a)
2366 + "' to '" + pd.ParameterDesc (j) + "'");
2374 // Update the argument with the implicit conversion
2379 // FIXME : For the case of params methods, we need to actually instantiate
2380 // an array and initialize it with the argument values etc etc.
2384 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2385 pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2386 if (!Location.IsNull (loc)) {
2388 "The best overloaded match for method '" + FullMethodDesc (method)+
2389 "' has some invalid arguments");
2391 "Argument " + (j+1) +
2392 ": Cannot convert from '" + Argument.FullDesc (a)
2393 + "' to '" + pd.ParameterDesc (j) + "'");
2404 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2405 ArrayList Arguments, Location loc)
2407 return OverloadResolve (ec, me, Arguments, loc, false);
2410 public override Expression DoResolve (EmitContext ec)
2413 // First, resolve the expression that is used to
2414 // trigger the invocation
2416 expr = expr.Resolve (ec);
2420 if (!(expr is MethodGroupExpr)) {
2421 Type expr_type = expr.Type;
2423 if (expr_type != null){
2424 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2426 return (new DelegateInvocation (
2427 this.expr, Arguments, Location)).Resolve (ec);
2431 if (!(expr is MethodGroupExpr)){
2432 report118 (Location, this.expr, "method group");
2437 // Next, evaluate all the expressions in the argument list
2439 if (Arguments != null){
2440 for (int i = Arguments.Count; i > 0;){
2442 Argument a = (Argument) Arguments [i];
2444 if (!a.Resolve (ec))
2449 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
2452 if (method == null){
2453 Error (-6, Location,
2454 "Could not find any applicable function for this argument list");
2458 if (method is MethodInfo)
2459 type = ((MethodInfo)method).ReturnType;
2461 eclass = ExprClass.Value;
2465 public static void EmitArguments (EmitContext ec, ArrayList Arguments)
2469 if (Arguments != null)
2470 top = Arguments.Count;
2474 for (int i = 0; i < top; i++){
2475 Argument a = (Argument) Arguments [i];
2481 public static void EmitCall (EmitContext ec,
2482 bool is_static, Expression instance_expr,
2483 MethodBase method, ArrayList Arguments)
2485 ILGenerator ig = ec.ig;
2486 bool struct_call = false;
2490 // If this is ourselves, push "this"
2492 if (instance_expr == null){
2493 ig.Emit (OpCodes.Ldarg_0);
2496 // Push the instance expression
2498 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
2503 // If the expression implements IMemoryLocation, then
2504 // we can optimize and use AddressOf on the
2507 // If not we have to use some temporary storage for
2509 if (instance_expr is IMemoryLocation)
2510 ((IMemoryLocation) instance_expr).AddressOf (ec);
2512 Type t = instance_expr.Type;
2514 instance_expr.Emit (ec);
2515 LocalBuilder temp = ec.GetTemporaryStorage (t);
2516 ig.Emit (OpCodes.Stloc, temp);
2517 ig.Emit (OpCodes.Ldloca, temp);
2520 instance_expr.Emit (ec);
2524 if (Arguments != null)
2525 EmitArguments (ec, Arguments);
2527 if (is_static || struct_call){
2528 if (method is MethodInfo)
2529 ig.Emit (OpCodes.Call, (MethodInfo) method);
2531 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2533 if (method is MethodInfo)
2534 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
2536 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
2540 public override void Emit (EmitContext ec)
2542 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2543 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
2546 public override void EmitStatement (EmitContext ec)
2551 // Pop the return value if there is one
2553 if (method is MethodInfo){
2554 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
2555 ec.ig.Emit (OpCodes.Pop);
2560 public class New : ExpressionStatement {
2561 public readonly ArrayList Arguments;
2562 public readonly string RequestedType;
2565 MethodBase method = null;
2568 // If set, the new expression is for a value_target, and
2569 // we will not leave anything on the stack.
2571 Expression value_target;
2573 public New (string requested_type, ArrayList arguments, Location loc)
2575 RequestedType = requested_type;
2576 Arguments = arguments;
2580 public Expression ValueTypeVariable {
2582 return value_target;
2586 value_target = value;
2590 public override Expression DoResolve (EmitContext ec)
2592 type = ec.TypeContainer.LookupType (RequestedType, false);
2597 bool IsDelegate = TypeManager.IsDelegateType (type);
2600 return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
2604 ml = MemberLookup (ec, type, ".ctor", false,
2605 MemberTypes.Constructor, AllBindingsFlags, Location);
2607 bool is_struct = false;
2608 is_struct = type.IsSubclassOf (TypeManager.value_type);
2610 if (! (ml is MethodGroupExpr)){
2612 report118 (Location, ml, "method group");
2618 if (Arguments != null){
2619 for (int i = Arguments.Count; i > 0;){
2621 Argument a = (Argument) Arguments [i];
2623 if (!a.Resolve (ec))
2628 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
2629 Arguments, Location);
2632 if (method == null && !is_struct) {
2633 Error (-6, Location,
2634 "New invocation: Can not find a constructor for " +
2635 "this argument list");
2639 eclass = ExprClass.Value;
2644 // This DoEmit can be invoked in two contexts:
2645 // * As a mechanism that will leave a value on the stack (new object)
2646 // * As one that wont (init struct)
2648 // You can control whether a value is required on the stack by passing
2649 // need_value_on_stack. The code *might* leave a value on the stack
2650 // so it must be popped manually
2652 // Returns whether a value is left on the stack
2654 bool DoEmit (EmitContext ec, bool need_value_on_stack)
2656 if (method == null){
2657 IMemoryLocation ml = (IMemoryLocation) value_target;
2661 Invocation.EmitArguments (ec, Arguments);
2662 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2667 // It must be a value type, sanity check
2669 if (value_target != null){
2670 ec.ig.Emit (OpCodes.Initobj, type);
2672 if (need_value_on_stack){
2673 value_target.Emit (ec);
2679 throw new Exception ("No method and no value type");
2682 public override void Emit (EmitContext ec)
2687 public override void EmitStatement (EmitContext ec)
2689 if (DoEmit (ec, false))
2690 ec.ig.Emit (OpCodes.Pop);
2695 // Represents an array creation expression.
2699 // There are two possible scenarios here: one is an array creation
2700 // expression that specifies the dimensions and optionally the
2701 // initialization data
2703 public class ArrayCreation : ExpressionStatement {
2705 string RequestedType;
2707 ArrayList Initializers;
2709 ArrayList Arguments;
2711 MethodBase method = null;
2712 Type array_element_type;
2713 bool IsOneDimensional = false;
2715 bool IsBuiltinType = false;
2717 public ArrayCreation (string requested_type, ArrayList exprs,
2718 string rank, ArrayList initializers, Location l)
2720 RequestedType = requested_type;
2722 Initializers = initializers;
2725 Arguments = new ArrayList ();
2727 foreach (Expression e in exprs)
2728 Arguments.Add (new Argument (e, Argument.AType.Expression));
2732 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2734 RequestedType = requested_type;
2736 Initializers = initializers;
2740 public static string FormArrayType (string base_type, int idx_count, string rank)
2742 StringBuilder sb = new StringBuilder (base_type);
2747 for (int i = 1; i < idx_count; i++)
2751 return sb.ToString ();
2754 public static string FormElementType (string base_type, int idx_count, string rank)
2756 StringBuilder sb = new StringBuilder (base_type);
2759 for (int i = 1; i < idx_count; i++)
2765 string val = sb.ToString ();
2767 return val.Substring (0, val.LastIndexOf ("["));
2771 public override Expression DoResolve (EmitContext ec)
2775 if (Arguments == null)
2778 arg_count = Arguments.Count;
2780 string array_type = FormArrayType (RequestedType, arg_count, Rank);
2782 string element_type = FormElementType (RequestedType, arg_count, Rank);
2784 type = ec.TypeContainer.LookupType (array_type, false);
2786 array_element_type = ec.TypeContainer.LookupType (element_type, false);
2791 if (arg_count == 1) {
2792 IsOneDimensional = true;
2793 eclass = ExprClass.Value;
2797 IsBuiltinType = TypeManager.IsBuiltinType (type);
2799 if (IsBuiltinType) {
2803 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
2804 AllBindingsFlags, Location);
2806 if (!(ml is MethodGroupExpr)){
2807 report118 (Location, ml, "method group");
2812 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
2813 "this argument list");
2817 if (Arguments != null) {
2818 for (int i = arg_count; i > 0;){
2820 Argument a = (Argument) Arguments [i];
2822 if (!a.Resolve (ec))
2827 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, Location);
2829 if (method == null) {
2830 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
2831 "this argument list");
2835 eclass = ExprClass.Value;
2840 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
2842 ArrayList args = new ArrayList ();
2843 if (Arguments != null){
2844 for (int i = arg_count; i > 0;){
2846 Argument a = (Argument) Arguments [i];
2848 if (!a.Resolve (ec))
2855 Type [] arg_types = null;
2858 arg_types = new Type [args.Count];
2860 args.CopyTo (arg_types, 0);
2862 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
2865 if (method == null) {
2866 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
2867 "this argument list");
2871 eclass = ExprClass.Value;
2877 public override void Emit (EmitContext ec)
2879 ILGenerator ig = ec.ig;
2881 if (IsOneDimensional) {
2882 Invocation.EmitArguments (ec, Arguments);
2883 ig.Emit (OpCodes.Newarr, array_element_type);
2886 Invocation.EmitArguments (ec, Arguments);
2889 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2891 ig.Emit (OpCodes.Newobj, (MethodInfo) method);
2894 if (Initializers != null){
2897 // FIXME: This is just sample data, need to fill with
2899 byte [] a = new byte [4] { 1, 2, 3, 4 };
2901 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
2903 ig.Emit (OpCodes.Dup);
2904 ig.Emit (OpCodes.Ldtoken, fb);
2905 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
2909 public override void EmitStatement (EmitContext ec)
2912 ec.ig.Emit (OpCodes.Pop);
2918 // Represents the `this' construct
2920 public class This : Expression, IStackStore, IMemoryLocation {
2923 public This (Location loc)
2928 public override Expression DoResolve (EmitContext ec)
2930 eclass = ExprClass.Variable;
2931 type = ec.TypeContainer.TypeBuilder;
2934 Report.Error (26, loc,
2935 "Keyword this not valid in static code");
2942 public Expression DoResolveLValue (EmitContext ec)
2946 if (ec.TypeContainer is Class){
2947 Report.Error (1604, loc, "Cannot assign to `this'");
2954 public override void Emit (EmitContext ec)
2956 ec.ig.Emit (OpCodes.Ldarg_0);
2959 public void Store (EmitContext ec)
2961 ec.ig.Emit (OpCodes.Starg, 0);
2964 public void AddressOf (EmitContext ec)
2966 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
2971 // Implements the typeof operator
2973 public class TypeOf : Expression {
2974 public readonly string QueriedType;
2977 public TypeOf (string queried_type)
2979 QueriedType = queried_type;
2982 public override Expression DoResolve (EmitContext ec)
2984 typearg = ec.TypeContainer.LookupType (QueriedType, false);
2986 if (typearg == null)
2989 type = TypeManager.type_type;
2990 eclass = ExprClass.Type;
2994 public override void Emit (EmitContext ec)
2996 ec.ig.Emit (OpCodes.Ldtoken, typearg);
2997 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3001 public class SizeOf : Expression {
3002 public readonly string QueriedType;
3004 public SizeOf (string queried_type)
3006 this.QueriedType = queried_type;
3009 public override Expression DoResolve (EmitContext ec)
3011 // FIXME: Implement;
3012 throw new Exception ("Unimplemented");
3016 public override void Emit (EmitContext ec)
3018 throw new Exception ("Implement me");
3022 public class MemberAccess : Expression {
3023 public readonly string Identifier;
3025 Expression member_lookup;
3028 public MemberAccess (Expression expr, string id, Location l)
3035 public Expression Expr {
3041 void error176 (Location loc, string name)
3043 Report.Error (176, loc, "Static member `" +
3044 name + "' cannot be accessed " +
3045 "with an instance reference, qualify with a " +
3046 "type name instead");
3049 public override Expression DoResolve (EmitContext ec)
3052 // We are the sole users of ResolveWithSimpleName (ie, the only
3053 // ones that can cope with it
3055 expr = expr.ResolveWithSimpleName (ec);
3060 if (expr is SimpleName){
3061 SimpleName child_expr = (SimpleName) expr;
3063 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3065 return expr.Resolve (ec);
3068 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3070 if (member_lookup == null)
3076 if (member_lookup is MethodGroupExpr){
3077 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3082 if (expr is TypeExpr){
3083 if (!mg.RemoveInstanceMethods ()){
3084 SimpleName.Error120 (loc, mg.Methods [0].Name);
3088 return member_lookup;
3092 // Instance.MethodGroup
3094 if (!mg.RemoveStaticMethods ()){
3095 error176 (loc, mg.Methods [0].Name);
3099 mg.InstanceExpression = expr;
3101 return member_lookup;
3104 if (member_lookup is FieldExpr){
3105 FieldExpr fe = (FieldExpr) member_lookup;
3106 FieldInfo fi = fe.FieldInfo;
3109 Type t = fi.FieldType;
3112 if (fi is FieldBuilder)
3113 o = TypeManager.GetValue ((FieldBuilder) fi);
3115 o = fi.GetValue (fi);
3117 if (t.IsSubclassOf (TypeManager.enum_type)) {
3118 Expression enum_member = MemberLookup (ec, t, "value__", false, loc);
3119 Type underlying_type = enum_member.Type;
3121 Expression e = Literalize (o, underlying_type);
3124 return new EnumLiteral (e, t);
3127 Expression exp = Literalize (o, t);
3133 if (expr is TypeExpr){
3134 if (!fe.FieldInfo.IsStatic){
3135 error176 (loc, fe.FieldInfo.Name);
3138 return member_lookup;
3140 if (fe.FieldInfo.IsStatic){
3141 error176 (loc, fe.FieldInfo.Name);
3144 fe.InstanceExpression = expr;
3150 if (member_lookup is PropertyExpr){
3151 PropertyExpr pe = (PropertyExpr) member_lookup;
3153 if (expr is TypeExpr){
3155 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3161 error176 (loc, pe.PropertyInfo.Name);
3164 pe.InstanceExpression = expr;
3170 Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3171 Environment.Exit (0);
3175 public override void Emit (EmitContext ec)
3177 throw new Exception ("Should not happen I think");
3182 public class CheckedExpr : Expression {
3184 public Expression Expr;
3186 public CheckedExpr (Expression e)
3191 public override Expression DoResolve (EmitContext ec)
3193 Expr = Expr.Resolve (ec);
3198 eclass = Expr.ExprClass;
3203 public override void Emit (EmitContext ec)
3205 bool last_check = ec.CheckState;
3207 ec.CheckState = true;
3209 ec.CheckState = last_check;
3214 public class UnCheckedExpr : Expression {
3216 public Expression Expr;
3218 public UnCheckedExpr (Expression e)
3223 public override Expression DoResolve (EmitContext ec)
3225 Expr = Expr.Resolve (ec);
3230 eclass = Expr.ExprClass;
3235 public override void Emit (EmitContext ec)
3237 bool last_check = ec.CheckState;
3239 ec.CheckState = false;
3241 ec.CheckState = last_check;
3246 public class ElementAccess : Expression {
3247 public ArrayList Arguments;
3248 public Expression Expr;
3249 public Location loc;
3251 public ElementAccess (Expression e, ArrayList e_list, Location l)
3255 Arguments = new ArrayList ();
3256 foreach (Expression tmp in e_list)
3257 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3262 bool CommonResolve (EmitContext ec)
3264 Expr = Expr.Resolve (ec);
3269 if (Arguments == null)
3272 for (int i = Arguments.Count; i > 0;){
3274 Argument a = (Argument) Arguments [i];
3276 if (!a.Resolve (ec))
3283 public override Expression DoResolve (EmitContext ec)
3285 if (!CommonResolve (ec))
3289 // We perform some simple tests, and then to "split" the emit and store
3290 // code we create an instance of a different class, and return that.
3292 // I am experimenting with this pattern.
3294 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3295 return (new ArrayAccess (this)).Resolve (ec);
3297 return (new IndexerAccess (this)).Resolve (ec);
3300 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3302 if (!CommonResolve (ec))
3305 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3306 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3308 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3311 public override void Emit (EmitContext ec)
3313 throw new Exception ("Should never be reached");
3318 // Implements array access
3320 public class ArrayAccess : Expression, IAssignMethod {
3322 // Points to our "data" repository
3326 public ArrayAccess (ElementAccess ea_data)
3329 eclass = ExprClass.Variable;
3332 public override Expression DoResolve (EmitContext ec)
3334 if (ea.Expr.ExprClass != ExprClass.Variable) {
3335 report118 (ea.loc, ea.Expr, "variable");
3339 Type t = ea.Expr.Type;
3341 if (t.GetArrayRank () != ea.Arguments.Count){
3342 Report.Error (22, ea.loc,
3343 "Incorrect number of indexes for array " +
3344 " expected: " + t.GetArrayRank () + " got: " +
3345 ea.Arguments.Count);
3348 type = t.GetElementType ();
3349 eclass = ExprClass.Variable;
3354 public override void Emit (EmitContext ec)
3356 int rank = ea.Expr.Type.GetArrayRank ();
3357 ILGenerator ig = ec.ig;
3361 foreach (Argument a in ea.Arguments)
3365 if (type == TypeManager.byte_type)
3366 ig.Emit (OpCodes.Ldelem_I1);
3367 else if (type == TypeManager.sbyte_type)
3368 ig.Emit (OpCodes.Ldelem_U1);
3369 else if (type == TypeManager.short_type)
3370 ig.Emit (OpCodes.Ldelem_I2);
3371 else if (type == TypeManager.ushort_type)
3372 ig.Emit (OpCodes.Ldelem_U2);
3373 else if (type == TypeManager.int32_type)
3374 ig.Emit (OpCodes.Ldelem_I4);
3375 else if (type == TypeManager.uint32_type)
3376 ig.Emit (OpCodes.Ldelem_U4);
3377 else if (type == TypeManager.uint64_type)
3378 ig.Emit (OpCodes.Ldelem_I8);
3379 else if (type == TypeManager.int64_type)
3380 ig.Emit (OpCodes.Ldelem_I8);
3381 else if (type == TypeManager.float_type)
3382 ig.Emit (OpCodes.Ldelem_R4);
3383 else if (type == TypeManager.double_type)
3384 ig.Emit (OpCodes.Ldelem_R8);
3385 else if (type == TypeManager.intptr_type)
3386 ig.Emit (OpCodes.Ldelem_I);
3388 ig.Emit (OpCodes.Ldelem_Ref);
3390 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3391 Type [] args = new Type [ea.Arguments.Count];
3396 foreach (Argument a in ea.Arguments)
3397 args [i++] = a.Type;
3399 get = mb.GetArrayMethod (
3400 ea.Expr.Type, "Get",
3401 CallingConventions.HasThis |
3402 CallingConventions.Standard,
3405 ig.Emit (OpCodes.Call, get);
3409 public void EmitAssign (EmitContext ec, Expression source)
3411 int rank = ea.Expr.Type.GetArrayRank ();
3412 ILGenerator ig = ec.ig;
3416 foreach (Argument a in ea.Arguments)
3421 Type t = source.Type;
3423 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3424 ig.Emit (OpCodes.Stelem_I1);
3425 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3426 ig.Emit (OpCodes.Stelem_I2);
3427 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3428 ig.Emit (OpCodes.Stelem_I4);
3429 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3430 ig.Emit (OpCodes.Stelem_I8);
3431 else if (t == TypeManager.float_type)
3432 ig.Emit (OpCodes.Stelem_R4);
3433 else if (t == TypeManager.double_type)
3434 ig.Emit (OpCodes.Stelem_R8);
3435 else if (t == TypeManager.intptr_type)
3436 ig.Emit (OpCodes.Stelem_I);
3438 ig.Emit (OpCodes.Stelem_Ref);
3440 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3441 Type [] args = new Type [ea.Arguments.Count + 1];
3446 foreach (Argument a in ea.Arguments)
3447 args [i++] = a.Type;
3451 set = mb.GetArrayMethod (
3452 ea.Expr.Type, "Set",
3453 CallingConventions.HasThis |
3454 CallingConventions.Standard,
3455 TypeManager.void_type, args);
3457 ig.Emit (OpCodes.Call, set);
3462 public ArrayList getters, setters;
3463 static Hashtable map;
3467 map = new Hashtable ();
3470 Indexers (MemberInfo [] mi)
3472 foreach (PropertyInfo property in mi){
3473 MethodInfo get, set;
3475 get = property.GetGetMethod (true);
3477 if (getters == null)
3478 getters = new ArrayList ();
3483 set = property.GetSetMethod (true);
3485 if (setters == null)
3486 setters = new ArrayList ();
3492 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
3494 Indexers ix = (Indexers) map [t];
3495 string p_name = TypeManager.IndexerPropertyName (t);
3500 MemberInfo [] mi = tm.FindMembers (
3501 t, MemberTypes.Property,
3502 BindingFlags.Public | BindingFlags.Instance,
3503 Type.FilterName, p_name);
3505 if (mi == null || mi.Length == 0){
3506 Report.Error (21, loc,
3507 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3508 "any indexers defined");
3512 ix = new Indexers (mi);
3519 public class IndexerAccess : Expression, IAssignMethod {
3521 // Points to our "data" repository
3524 MethodInfo get, set;
3526 ArrayList set_arguments;
3528 public IndexerAccess (ElementAccess ea_data)
3531 eclass = ExprClass.Value;
3534 public bool VerifyAssignable (Expression source)
3536 throw new Exception ("Implement me!");
3539 public override Expression DoResolve (EmitContext ec)
3541 Type indexer_type = ea.Expr.Type;
3544 // Step 1: Query for all `Item' *properties*. Notice
3545 // that the actual methods are pointed from here.
3547 // This is a group of properties, piles of them.
3550 ilist = Indexers.GetIndexersForType (
3551 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3553 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3554 get = (MethodInfo) Invocation.OverloadResolve (
3555 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3558 Report.Error (154, ea.loc,
3559 "indexer can not be used in this context, because " +
3560 "it lacks a `get' accessor");
3564 type = get.ReturnType;
3565 eclass = ExprClass.Value;
3569 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3571 Type indexer_type = ea.Expr.Type;
3572 Type right_type = right_side.Type;
3575 ilist = Indexers.GetIndexersForType (
3576 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3578 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3579 set_arguments = (ArrayList) ea.Arguments.Clone ();
3580 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3582 set = (MethodInfo) Invocation.OverloadResolve (
3583 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3587 Report.Error (200, ea.loc,
3588 "indexer X.this [" + TypeManager.CSharpName (right_type) +
3589 "] lacks a `set' accessor");
3593 type = TypeManager.void_type;
3594 eclass = ExprClass.IndexerAccess;
3598 public override void Emit (EmitContext ec)
3600 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3604 // source is ignored, because we already have a copy of it from the
3605 // LValue resolution and we have already constructed a pre-cached
3606 // version of the arguments (ea.set_arguments);
3608 public void EmitAssign (EmitContext ec, Expression source)
3610 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3614 public class BaseAccess : Expression {
3616 public enum BaseAccessType {
3621 public readonly BaseAccessType BAType;
3622 public readonly string Member;
3623 public readonly ArrayList Arguments;
3625 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3633 public override Expression DoResolve (EmitContext ec)
3635 // FIXME: Implement;
3636 throw new Exception ("Unimplemented");
3640 public override void Emit (EmitContext ec)
3642 throw new Exception ("Unimplemented");
3647 // This class exists solely to pass the Type around and to be a dummy
3648 // that can be passed to the conversion functions (this is used by
3649 // foreach implementation to typecast the object return value from
3650 // get_Current into the proper type. All code has been generated and
3651 // we only care about the side effect conversions to be performed
3654 public class EmptyExpression : Expression {
3655 public EmptyExpression ()
3657 type = TypeManager.object_type;
3658 eclass = ExprClass.Value;
3661 public override Expression DoResolve (EmitContext ec)
3666 public override void Emit (EmitContext ec)
3668 // nothing, as we only exist to not do anything.
3672 public class UserCast : Expression {
3676 public UserCast (MethodInfo method, Expression source)
3678 this.method = method;
3679 this.source = source;
3680 type = method.ReturnType;
3681 eclass = ExprClass.Value;
3684 public override Expression DoResolve (EmitContext ec)
3687 // We are born fully resolved
3692 public override void Emit (EmitContext ec)
3694 ILGenerator ig = ec.ig;
3698 if (method is MethodInfo)
3699 ig.Emit (OpCodes.Call, (MethodInfo) method);
3701 ig.Emit (OpCodes.Call, (ConstructorInfo) method);