2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
12 namespace Mono.CSharp {
14 using System.Collections;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
21 /// This is just a helper class, it is generated by Unary, UnaryMutator
22 /// when an overloaded method has been found. It just emits the code for a
25 public class StaticCallExpr : ExpressionStatement {
29 StaticCallExpr (MethodInfo m, ArrayList a)
35 eclass = ExprClass.Value;
38 public override Expression DoResolve (EmitContext ec)
41 // We are born fully resolved
46 public override void Emit (EmitContext ec)
49 Invocation.EmitArguments (ec, mi, args);
51 ec.ig.Emit (OpCodes.Call, mi);
55 static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
56 Expression e, Location loc)
61 args = new ArrayList (1);
62 args.Add (new Argument (e, Argument.AType.Expression));
63 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
68 return new StaticCallExpr ((MethodInfo) method, args);
71 public override void EmitStatement (EmitContext ec)
74 if (type != TypeManager.void_type)
75 ec.ig.Emit (OpCodes.Pop);
80 /// Unary expressions.
84 /// Unary implements unary expressions. It derives from
85 /// ExpressionStatement becuase the pre/post increment/decrement
86 /// operators can be used in a statement context.
88 public class Unary : Expression {
89 public enum Operator : byte {
90 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
91 Indirection, AddressOf, TOP
98 public Unary (Operator op, Expression expr, Location loc)
105 public Expression Expr {
115 public Operator Oper {
126 /// Returns a stringified representation of the Operator
131 case Operator.UnaryPlus:
133 case Operator.UnaryNegation:
135 case Operator.LogicalNot:
137 case Operator.OnesComplement:
139 case Operator.AddressOf:
141 case Operator.Indirection:
145 return oper.ToString ();
148 static string [] oper_names;
152 oper_names = new string [(int)Operator.TOP];
154 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
155 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
156 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
157 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
158 oper_names [(int) Operator.Indirection] = "op_Indirection";
159 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
162 void error23 (Type t)
165 23, loc, "Operator " + OperName () +
166 " cannot be applied to operand of type `" +
167 TypeManager.CSharpName (t) + "'");
171 /// The result has been already resolved:
173 /// FIXME: a minus constant -128 sbyte cant be turned into a
176 static Expression TryReduceNegative (Expression expr)
180 if (expr is IntConstant)
181 e = new IntConstant (-((IntConstant) expr).Value);
182 else if (expr is UIntConstant)
183 e = new LongConstant (-((UIntConstant) expr).Value);
184 else if (expr is LongConstant)
185 e = new LongConstant (-((LongConstant) expr).Value);
186 else if (expr is FloatConstant)
187 e = new FloatConstant (-((FloatConstant) expr).Value);
188 else if (expr is DoubleConstant)
189 e = new DoubleConstant (-((DoubleConstant) expr).Value);
190 else if (expr is DecimalConstant)
191 e = new DecimalConstant (-((DecimalConstant) expr).Value);
192 else if (expr is ShortConstant)
193 e = new IntConstant (-((ShortConstant) expr).Value);
194 else if (expr is UShortConstant)
195 e = new IntConstant (-((UShortConstant) expr).Value);
200 Expression Reduce (EmitContext ec, Expression e)
202 Type expr_type = e.Type;
205 case Operator.UnaryPlus:
208 case Operator.UnaryNegation:
209 return TryReduceNegative (e);
211 case Operator.LogicalNot:
212 if (expr_type != TypeManager.bool_type) {
217 BoolConstant b = (BoolConstant) e;
218 return new BoolConstant (!(b.Value));
220 case Operator.OnesComplement:
221 if (!((expr_type == TypeManager.int32_type) ||
222 (expr_type == TypeManager.uint32_type) ||
223 (expr_type == TypeManager.int64_type) ||
224 (expr_type == TypeManager.uint64_type) ||
225 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
230 if (e is EnumConstant){
231 EnumConstant enum_constant = (EnumConstant) e;
233 Expression reduced = Reduce (ec, enum_constant.Child);
235 return new EnumConstant ((Constant) reduced, enum_constant.Type);
238 if (expr_type == TypeManager.int32_type)
239 return new IntConstant (~ ((IntConstant) e).Value);
240 if (expr_type == TypeManager.uint32_type)
241 return new UIntConstant (~ ((UIntConstant) e).Value);
242 if (expr_type == TypeManager.int64_type)
243 return new LongConstant (~ ((LongConstant) e).Value);
244 if (expr_type == TypeManager.uint64_type)
245 return new ULongConstant (~ ((ULongConstant) e).Value);
247 throw new Exception (
248 "FIXME: Implement constant OnesComplement of:" +
251 throw new Exception ("Can not constant fold");
254 Expression ResolveOperator (EmitContext ec)
256 Type expr_type = expr.Type;
259 // Step 1: Perform Operator Overload location
264 op_name = oper_names [(int) oper];
266 mg = MemberLookup (ec, expr_type, op_name, false, loc);
268 if (mg == null && expr_type.BaseType != null)
269 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
272 Expression e = StaticCallExpr.MakeSimpleCall (
273 ec, (MethodGroupExpr) mg, expr, loc);
283 // Only perform numeric promotions on:
286 if (expr_type == null)
290 // Step 2: Default operations on CLI native types.
292 if (expr is Constant)
293 return Reduce (ec, expr);
295 if (oper == Operator.LogicalNot){
296 if (expr_type != TypeManager.bool_type) {
301 type = TypeManager.bool_type;
305 if (oper == Operator.OnesComplement) {
306 if (!((expr_type == TypeManager.int32_type) ||
307 (expr_type == TypeManager.uint32_type) ||
308 (expr_type == TypeManager.int64_type) ||
309 (expr_type == TypeManager.uint64_type) ||
310 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
318 if (oper == Operator.UnaryPlus) {
320 // A plus in front of something is just a no-op, so return the child.
326 // Deals with -literals
327 // int operator- (int x)
328 // long operator- (long x)
329 // float operator- (float f)
330 // double operator- (double d)
331 // decimal operator- (decimal d)
333 if (oper == Operator.UnaryNegation){
337 // perform numeric promotions to int,
341 // The following is inneficient, because we call
342 // ConvertImplicit too many times.
344 // It is also not clear if we should convert to Float
345 // or Double initially.
347 if (expr_type == TypeManager.uint32_type){
349 // FIXME: handle exception to this rule that
350 // permits the int value -2147483648 (-2^31) to
351 // bt wrote as a decimal interger literal
353 type = TypeManager.int64_type;
354 expr = ConvertImplicit (ec, expr, type, loc);
358 if (expr_type == TypeManager.uint64_type){
360 // FIXME: Handle exception of `long value'
361 // -92233720368547758087 (-2^63) to be wrote as
362 // decimal integer literal.
368 if (expr_type == TypeManager.float_type){
373 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
380 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
387 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
398 if (oper == Operator.AddressOf){
399 if (expr.eclass != ExprClass.Variable){
400 Error (211, loc, "Cannot take the address of non-variables");
403 type = Type.GetType (expr.Type.ToString () + "*");
408 Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
409 TypeManager.CSharpName (expr_type) + "'");
413 public override Expression DoResolve (EmitContext ec)
415 expr = expr.Resolve (ec);
420 eclass = ExprClass.Value;
421 return ResolveOperator (ec);
424 public override void Emit (EmitContext ec)
426 ILGenerator ig = ec.ig;
427 Type expr_type = expr.Type;
430 case Operator.UnaryPlus:
431 throw new Exception ("This should be caught by Resolve");
433 case Operator.UnaryNegation:
435 ig.Emit (OpCodes.Neg);
438 case Operator.LogicalNot:
440 ig.Emit (OpCodes.Ldc_I4_0);
441 ig.Emit (OpCodes.Ceq);
444 case Operator.OnesComplement:
446 ig.Emit (OpCodes.Not);
449 case Operator.AddressOf:
450 ((IMemoryLocation)expr).AddressOf (ec);
453 case Operator.Indirection:
454 throw new Exception ("Not implemented yet");
457 throw new Exception ("This should not happen: Operator = "
463 /// This will emit the child expression for `ec' avoiding the logical
464 /// not. The parent will take care of changing brfalse/brtrue
466 public void EmitLogicalNot (EmitContext ec)
468 if (oper != Operator.LogicalNot)
469 throw new Exception ("EmitLogicalNot can only be called with !expr");
477 /// Unary Mutator expressions (pre and post ++ and --)
481 /// UnaryMutator implements ++ and -- expressions. It derives from
482 /// ExpressionStatement becuase the pre/post increment/decrement
483 /// operators can be used in a statement context.
485 /// FIXME: Idea, we could split this up in two classes, one simpler
486 /// for the common case, and one with the extra fields for more complex
487 /// classes (indexers require temporary access; overloaded require method)
489 /// Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
490 /// PostDecrement, that way we could save the `Mode' byte as well.
492 public class UnaryMutator : ExpressionStatement {
493 public enum Mode : byte {
494 PreIncrement, PreDecrement, PostIncrement, PostDecrement
500 LocalTemporary temp_storage;
503 // This is expensive for the simplest case.
507 public UnaryMutator (Mode m, Expression e, Location l)
516 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
520 void error23 (Type t)
523 23, loc, "Operator " + OperName () +
524 " cannot be applied to operand of type `" +
525 TypeManager.CSharpName (t) + "'");
529 /// Returns whether an object of type `t' can be incremented
530 /// or decremented with add/sub (ie, basically whether we can
531 /// use pre-post incr-decr operations on it, but it is not a
532 /// System.Decimal, which we require operator overloading to catch)
534 static bool IsIncrementableNumber (Type t)
536 return (t == TypeManager.sbyte_type) ||
537 (t == TypeManager.byte_type) ||
538 (t == TypeManager.short_type) ||
539 (t == TypeManager.ushort_type) ||
540 (t == TypeManager.int32_type) ||
541 (t == TypeManager.uint32_type) ||
542 (t == TypeManager.int64_type) ||
543 (t == TypeManager.uint64_type) ||
544 (t == TypeManager.char_type) ||
545 (t.IsSubclassOf (TypeManager.enum_type)) ||
546 (t == TypeManager.float_type) ||
547 (t == TypeManager.double_type);
550 Expression ResolveOperator (EmitContext ec)
552 Type expr_type = expr.Type;
555 // Step 1: Perform Operator Overload location
560 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
561 op_name = "op_Increment";
563 op_name = "op_Decrement";
565 mg = MemberLookup (ec, expr_type, op_name, false, loc);
567 if (mg == null && expr_type.BaseType != null)
568 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
571 method = StaticCallExpr.MakeSimpleCall (
572 ec, (MethodGroupExpr) mg, expr, loc);
579 // The operand of the prefix/postfix increment decrement operators
580 // should be an expression that is classified as a variable,
581 // a property access or an indexer access
584 if (expr.eclass == ExprClass.Variable){
585 if (IsIncrementableNumber (expr_type) ||
586 expr_type == TypeManager.decimal_type){
589 } else if (expr.eclass == ExprClass.IndexerAccess){
590 IndexerAccess ia = (IndexerAccess) expr;
592 temp_storage = new LocalTemporary (ec, expr.Type);
594 expr = ia.ResolveLValue (ec, temp_storage);
599 } else if (expr.eclass == ExprClass.PropertyAccess){
600 PropertyExpr pe = (PropertyExpr) expr;
602 if (pe.VerifyAssignable ())
607 report118 (loc, expr, "variable, indexer or property access");
611 Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
612 TypeManager.CSharpName (expr_type) + "'");
616 public override Expression DoResolve (EmitContext ec)
618 expr = expr.Resolve (ec);
623 eclass = ExprClass.Value;
624 return ResolveOperator (ec);
629 // FIXME: We need some way of avoiding the use of temp_storage
630 // for some types of storage (parameters, local variables,
631 // static fields) and single-dimension array access.
633 void EmitCode (EmitContext ec, bool is_expr)
635 ILGenerator ig = ec.ig;
636 IAssignMethod ia = (IAssignMethod) expr;
637 Type expr_type = expr.Type;
639 if (temp_storage == null)
640 temp_storage = new LocalTemporary (ec, expr_type);
643 case Mode.PreIncrement:
644 case Mode.PreDecrement:
648 if (expr_type == TypeManager.uint64_type ||
649 expr_type == TypeManager.int64_type)
650 ig.Emit (OpCodes.Ldc_I8, 1L);
651 else if (expr_type == TypeManager.double_type)
652 ig.Emit (OpCodes.Ldc_R8, 1.0);
653 else if (expr_type == TypeManager.float_type)
654 ig.Emit (OpCodes.Ldc_R4, 1.0F);
656 ig.Emit (OpCodes.Ldc_I4_1);
658 if (mode == Mode.PreDecrement)
659 ig.Emit (OpCodes.Sub);
661 ig.Emit (OpCodes.Add);
665 temp_storage.Store (ec);
666 ia.EmitAssign (ec, temp_storage);
668 temp_storage.Emit (ec);
671 case Mode.PostIncrement:
672 case Mode.PostDecrement:
680 ig.Emit (OpCodes.Dup);
682 if (expr_type == TypeManager.uint64_type ||
683 expr_type == TypeManager.int64_type)
684 ig.Emit (OpCodes.Ldc_I8, 1L);
685 else if (expr_type == TypeManager.double_type)
686 ig.Emit (OpCodes.Ldc_R8, 1.0);
687 else if (expr_type == TypeManager.float_type)
688 ig.Emit (OpCodes.Ldc_R4, 1.0F);
690 ig.Emit (OpCodes.Ldc_I4_1);
692 if (mode == Mode.PostDecrement)
693 ig.Emit (OpCodes.Sub);
695 ig.Emit (OpCodes.Add);
700 temp_storage.Store (ec);
701 ia.EmitAssign (ec, temp_storage);
706 public override void Emit (EmitContext ec)
712 public override void EmitStatement (EmitContext ec)
714 EmitCode (ec, false);
720 /// Base class for the `Is' and `As' classes.
724 /// FIXME: Split this in two, and we get to save the `Operator' Oper
727 public abstract class Probe : Expression {
728 public readonly string ProbeType;
729 protected Expression expr;
730 protected Type probe_type;
733 public Probe (Expression expr, string probe_type, Location l)
735 ProbeType = probe_type;
740 public Expression Expr {
746 public override Expression DoResolve (EmitContext ec)
748 probe_type = RootContext.LookupType (ec.TypeContainer, ProbeType, false, loc);
750 if (probe_type == null)
753 expr = expr.Resolve (ec);
760 /// Implementation of the `is' operator.
762 public class Is : Probe {
763 public Is (Expression expr, string probe_type, Location l)
764 : base (expr, probe_type, l)
768 public override void Emit (EmitContext ec)
770 ILGenerator ig = ec.ig;
774 ig.Emit (OpCodes.Isinst, probe_type);
775 ig.Emit (OpCodes.Ldnull);
776 ig.Emit (OpCodes.Cgt_Un);
779 public override Expression DoResolve (EmitContext ec)
781 Expression e = base.DoResolve (ec);
786 type = TypeManager.bool_type;
787 eclass = ExprClass.Value;
794 /// Implementation of the `as' operator.
796 public class As : Probe {
797 public As (Expression expr, string probe_type, Location l)
798 : base (expr, probe_type, l)
802 public override void Emit (EmitContext ec)
804 ILGenerator ig = ec.ig;
807 ig.Emit (OpCodes.Isinst, probe_type);
810 public override Expression DoResolve (EmitContext ec)
812 Expression e = base.DoResolve (ec);
818 eclass = ExprClass.Value;
825 /// This represents a typecast in the source language.
827 /// FIXME: Cast expressions have an unusual set of parsing
828 /// rules, we need to figure those out.
830 public class Cast : Expression {
831 Expression target_type;
835 public Cast (Expression cast_type, Expression expr, Location loc)
837 this.target_type = cast_type;
842 public Expression TargetType {
848 public Expression Expr {
858 /// Attempts to do a compile-time folding of a constant cast.
860 Expression TryReduce (EmitContext ec, Type target_type)
862 if (expr is ByteConstant){
863 byte v = ((ByteConstant) expr).Value;
865 if (target_type == TypeManager.sbyte_type)
866 return new SByteConstant ((sbyte) v);
867 if (target_type == TypeManager.short_type)
868 return new ShortConstant ((short) v);
869 if (target_type == TypeManager.ushort_type)
870 return new UShortConstant ((ushort) v);
871 if (target_type == TypeManager.int32_type)
872 return new IntConstant ((int) v);
873 if (target_type == TypeManager.uint32_type)
874 return new UIntConstant ((uint) v);
875 if (target_type == TypeManager.int64_type)
876 return new LongConstant ((long) v);
877 if (target_type == TypeManager.uint64_type)
878 return new ULongConstant ((ulong) v);
879 if (target_type == TypeManager.float_type)
880 return new FloatConstant ((float) v);
881 if (target_type == TypeManager.double_type)
882 return new DoubleConstant ((double) v);
884 if (expr is SByteConstant){
885 sbyte v = ((SByteConstant) expr).Value;
887 if (target_type == TypeManager.byte_type)
888 return new ByteConstant ((byte) v);
889 if (target_type == TypeManager.short_type)
890 return new ShortConstant ((short) v);
891 if (target_type == TypeManager.ushort_type)
892 return new UShortConstant ((ushort) v);
893 if (target_type == TypeManager.int32_type)
894 return new IntConstant ((int) v);
895 if (target_type == TypeManager.uint32_type)
896 return new UIntConstant ((uint) v);
897 if (target_type == TypeManager.int64_type)
898 return new LongConstant ((long) v);
899 if (target_type == TypeManager.uint64_type)
900 return new ULongConstant ((ulong) v);
901 if (target_type == TypeManager.float_type)
902 return new FloatConstant ((float) v);
903 if (target_type == TypeManager.double_type)
904 return new DoubleConstant ((double) v);
906 if (expr is ShortConstant){
907 short v = ((ShortConstant) expr).Value;
909 if (target_type == TypeManager.byte_type)
910 return new ByteConstant ((byte) v);
911 if (target_type == TypeManager.sbyte_type)
912 return new SByteConstant ((sbyte) v);
913 if (target_type == TypeManager.ushort_type)
914 return new UShortConstant ((ushort) v);
915 if (target_type == TypeManager.int32_type)
916 return new IntConstant ((int) v);
917 if (target_type == TypeManager.uint32_type)
918 return new UIntConstant ((uint) v);
919 if (target_type == TypeManager.int64_type)
920 return new LongConstant ((long) v);
921 if (target_type == TypeManager.uint64_type)
922 return new ULongConstant ((ulong) v);
923 if (target_type == TypeManager.float_type)
924 return new FloatConstant ((float) v);
925 if (target_type == TypeManager.double_type)
926 return new DoubleConstant ((double) v);
928 if (expr is UShortConstant){
929 ushort v = ((UShortConstant) expr).Value;
931 if (target_type == TypeManager.byte_type)
932 return new ByteConstant ((byte) v);
933 if (target_type == TypeManager.sbyte_type)
934 return new SByteConstant ((sbyte) v);
935 if (target_type == TypeManager.short_type)
936 return new ShortConstant ((short) v);
937 if (target_type == TypeManager.int32_type)
938 return new IntConstant ((int) v);
939 if (target_type == TypeManager.uint32_type)
940 return new UIntConstant ((uint) v);
941 if (target_type == TypeManager.int64_type)
942 return new LongConstant ((long) v);
943 if (target_type == TypeManager.uint64_type)
944 return new ULongConstant ((ulong) v);
945 if (target_type == TypeManager.float_type)
946 return new FloatConstant ((float) v);
947 if (target_type == TypeManager.double_type)
948 return new DoubleConstant ((double) v);
950 if (expr is IntConstant){
951 int v = ((IntConstant) expr).Value;
953 if (target_type == TypeManager.byte_type)
954 return new ByteConstant ((byte) v);
955 if (target_type == TypeManager.sbyte_type)
956 return new SByteConstant ((sbyte) v);
957 if (target_type == TypeManager.short_type)
958 return new ShortConstant ((short) v);
959 if (target_type == TypeManager.ushort_type)
960 return new UShortConstant ((ushort) v);
961 if (target_type == TypeManager.uint32_type)
962 return new UIntConstant ((uint) v);
963 if (target_type == TypeManager.int64_type)
964 return new LongConstant ((long) v);
965 if (target_type == TypeManager.uint64_type)
966 return new ULongConstant ((ulong) v);
967 if (target_type == TypeManager.float_type)
968 return new FloatConstant ((float) v);
969 if (target_type == TypeManager.double_type)
970 return new DoubleConstant ((double) v);
972 if (expr is UIntConstant){
973 uint v = ((UIntConstant) expr).Value;
975 if (target_type == TypeManager.byte_type)
976 return new ByteConstant ((byte) v);
977 if (target_type == TypeManager.sbyte_type)
978 return new SByteConstant ((sbyte) v);
979 if (target_type == TypeManager.short_type)
980 return new ShortConstant ((short) v);
981 if (target_type == TypeManager.ushort_type)
982 return new UShortConstant ((ushort) v);
983 if (target_type == TypeManager.int32_type)
984 return new IntConstant ((int) v);
985 if (target_type == TypeManager.int64_type)
986 return new LongConstant ((long) v);
987 if (target_type == TypeManager.uint64_type)
988 return new ULongConstant ((ulong) v);
989 if (target_type == TypeManager.float_type)
990 return new FloatConstant ((float) v);
991 if (target_type == TypeManager.double_type)
992 return new DoubleConstant ((double) v);
994 if (expr is LongConstant){
995 long v = ((LongConstant) expr).Value;
997 if (target_type == TypeManager.byte_type)
998 return new ByteConstant ((byte) v);
999 if (target_type == TypeManager.sbyte_type)
1000 return new SByteConstant ((sbyte) v);
1001 if (target_type == TypeManager.short_type)
1002 return new ShortConstant ((short) v);
1003 if (target_type == TypeManager.ushort_type)
1004 return new UShortConstant ((ushort) v);
1005 if (target_type == TypeManager.int32_type)
1006 return new IntConstant ((int) v);
1007 if (target_type == TypeManager.uint32_type)
1008 return new UIntConstant ((uint) v);
1009 if (target_type == TypeManager.uint64_type)
1010 return new ULongConstant ((ulong) v);
1011 if (target_type == TypeManager.float_type)
1012 return new FloatConstant ((float) v);
1013 if (target_type == TypeManager.double_type)
1014 return new DoubleConstant ((double) v);
1016 if (expr is ULongConstant){
1017 ulong v = ((ULongConstant) expr).Value;
1019 if (target_type == TypeManager.byte_type)
1020 return new ByteConstant ((byte) v);
1021 if (target_type == TypeManager.sbyte_type)
1022 return new SByteConstant ((sbyte) v);
1023 if (target_type == TypeManager.short_type)
1024 return new ShortConstant ((short) v);
1025 if (target_type == TypeManager.ushort_type)
1026 return new UShortConstant ((ushort) v);
1027 if (target_type == TypeManager.int32_type)
1028 return new IntConstant ((int) v);
1029 if (target_type == TypeManager.uint32_type)
1030 return new UIntConstant ((uint) v);
1031 if (target_type == TypeManager.int64_type)
1032 return new LongConstant ((long) v);
1033 if (target_type == TypeManager.float_type)
1034 return new FloatConstant ((float) v);
1035 if (target_type == TypeManager.double_type)
1036 return new DoubleConstant ((double) v);
1038 if (expr is FloatConstant){
1039 float v = ((FloatConstant) expr).Value;
1041 if (target_type == TypeManager.byte_type)
1042 return new ByteConstant ((byte) v);
1043 if (target_type == TypeManager.sbyte_type)
1044 return new SByteConstant ((sbyte) v);
1045 if (target_type == TypeManager.short_type)
1046 return new ShortConstant ((short) v);
1047 if (target_type == TypeManager.ushort_type)
1048 return new UShortConstant ((ushort) v);
1049 if (target_type == TypeManager.int32_type)
1050 return new IntConstant ((int) v);
1051 if (target_type == TypeManager.uint32_type)
1052 return new UIntConstant ((uint) v);
1053 if (target_type == TypeManager.int64_type)
1054 return new LongConstant ((long) v);
1055 if (target_type == TypeManager.uint64_type)
1056 return new ULongConstant ((ulong) v);
1057 if (target_type == TypeManager.double_type)
1058 return new DoubleConstant ((double) v);
1060 if (expr is DoubleConstant){
1061 double v = ((DoubleConstant) expr).Value;
1063 if (target_type == TypeManager.byte_type)
1064 return new ByteConstant ((byte) v);
1065 if (target_type == TypeManager.sbyte_type)
1066 return new SByteConstant ((sbyte) v);
1067 if (target_type == TypeManager.short_type)
1068 return new ShortConstant ((short) v);
1069 if (target_type == TypeManager.ushort_type)
1070 return new UShortConstant ((ushort) v);
1071 if (target_type == TypeManager.int32_type)
1072 return new IntConstant ((int) v);
1073 if (target_type == TypeManager.uint32_type)
1074 return new UIntConstant ((uint) v);
1075 if (target_type == TypeManager.int64_type)
1076 return new LongConstant ((long) v);
1077 if (target_type == TypeManager.uint64_type)
1078 return new ULongConstant ((ulong) v);
1079 if (target_type == TypeManager.float_type)
1080 return new FloatConstant ((float) v);
1086 public override Expression DoResolve (EmitContext ec)
1088 expr = expr.Resolve (ec);
1092 target_type = target_type.Resolve (ec);
1093 if (target_type == null)
1096 if (target_type.eclass != ExprClass.Type){
1097 report118 (loc, target_type, "class");
1101 type = target_type.Type;
1102 eclass = ExprClass.Value;
1107 if (expr is Constant){
1108 Expression e = TryReduce (ec, type);
1114 expr = ConvertExplicit (ec, expr, type, loc);
1118 public override void Emit (EmitContext ec)
1121 // This one will never happen
1123 throw new Exception ("Should not happen");
1128 /// Binary operators
1130 public class Binary : Expression {
1131 public enum Operator : byte {
1132 Multiply, Division, Modulus,
1133 Addition, Subtraction,
1134 LeftShift, RightShift,
1135 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1136 Equality, Inequality,
1145 Expression left, right;
1147 ArrayList Arguments;
1150 bool DelegateOperation;
1152 public Binary (Operator oper, Expression left, Expression right, Location loc)
1160 public Operator Oper {
1169 public Expression Left {
1178 public Expression Right {
1189 /// Returns a stringified representation of the Operator
1194 case Operator.Multiply:
1196 case Operator.Division:
1198 case Operator.Modulus:
1200 case Operator.Addition:
1202 case Operator.Subtraction:
1204 case Operator.LeftShift:
1206 case Operator.RightShift:
1208 case Operator.LessThan:
1210 case Operator.GreaterThan:
1212 case Operator.LessThanOrEqual:
1214 case Operator.GreaterThanOrEqual:
1216 case Operator.Equality:
1218 case Operator.Inequality:
1220 case Operator.BitwiseAnd:
1222 case Operator.BitwiseOr:
1224 case Operator.ExclusiveOr:
1226 case Operator.LogicalOr:
1228 case Operator.LogicalAnd:
1232 return oper.ToString ();
1235 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1237 if (expr.Type == target_type)
1240 return ConvertImplicit (ec, expr, target_type, new Location (-1));
1244 // Note that handling the case l == Decimal || r == Decimal
1245 // is taken care of by the Step 1 Operator Overload resolution.
1247 bool DoNumericPromotions (EmitContext ec, Type l, Type r)
1249 if (l == TypeManager.double_type || r == TypeManager.double_type){
1251 // If either operand is of type double, the other operand is
1252 // conveted to type double.
1254 if (r != TypeManager.double_type)
1255 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
1256 if (l != TypeManager.double_type)
1257 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
1259 type = TypeManager.double_type;
1260 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1262 // if either operand is of type float, th eother operand is
1263 // converd to type float.
1265 if (r != TypeManager.double_type)
1266 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
1267 if (l != TypeManager.double_type)
1268 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
1269 type = TypeManager.float_type;
1270 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1274 // If either operand is of type ulong, the other operand is
1275 // converted to type ulong. or an error ocurrs if the other
1276 // operand is of type sbyte, short, int or long
1278 if (l == TypeManager.uint64_type){
1279 if (r != TypeManager.uint64_type){
1280 if (right is IntConstant){
1281 IntConstant ic = (IntConstant) right;
1283 e = TryImplicitIntConversion (l, ic);
1286 } else if (right is LongConstant){
1287 long ll = ((LongConstant) right).Value;
1290 right = new ULongConstant ((ulong) ll);
1292 e = ImplicitNumericConversion (ec, right, l, loc);
1299 if (left is IntConstant){
1300 e = TryImplicitIntConversion (r, (IntConstant) left);
1303 } else if (left is LongConstant){
1304 long ll = ((LongConstant) left).Value;
1307 left = new ULongConstant ((ulong) ll);
1309 e = ImplicitNumericConversion (ec, left, r, loc);
1316 if ((other == TypeManager.sbyte_type) ||
1317 (other == TypeManager.short_type) ||
1318 (other == TypeManager.int32_type) ||
1319 (other == TypeManager.int64_type)){
1320 string oper = OperName ();
1322 Error (34, loc, "Operator `" + OperName ()
1323 + "' is ambiguous on operands of type `"
1324 + TypeManager.CSharpName (l) + "' "
1325 + "and `" + TypeManager.CSharpName (r)
1328 type = TypeManager.uint64_type;
1329 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1331 // If either operand is of type long, the other operand is converted
1334 if (l != TypeManager.int64_type)
1335 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
1336 if (r != TypeManager.int64_type)
1337 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
1339 type = TypeManager.int64_type;
1340 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1342 // If either operand is of type uint, and the other
1343 // operand is of type sbyte, short or int, othe operands are
1344 // converted to type long.
1348 if (l == TypeManager.uint32_type)
1350 else if (r == TypeManager.uint32_type)
1353 if ((other == TypeManager.sbyte_type) ||
1354 (other == TypeManager.short_type) ||
1355 (other == TypeManager.int32_type)){
1356 left = ForceConversion (ec, left, TypeManager.int64_type);
1357 right = ForceConversion (ec, right, TypeManager.int64_type);
1358 type = TypeManager.int64_type;
1361 // if either operand is of type uint, the other
1362 // operand is converd to type uint
1364 left = ForceConversion (ec, left, TypeManager.uint32_type);
1365 right = ForceConversion (ec, right, TypeManager.uint32_type);
1366 type = TypeManager.uint32_type;
1368 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1369 if (l != TypeManager.decimal_type)
1370 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
1371 if (r != TypeManager.decimal_type)
1372 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
1374 type = TypeManager.decimal_type;
1376 Expression l_tmp, r_tmp;
1378 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
1382 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
1389 type = TypeManager.int32_type;
1398 "Operator " + OperName () + " cannot be applied to operands of type `" +
1399 TypeManager.CSharpName (left.Type) + "' and `" +
1400 TypeManager.CSharpName (right.Type) + "'");
1404 Expression CheckShiftArguments (EmitContext ec)
1408 Type r = right.Type;
1410 e = ForceConversion (ec, right, TypeManager.int32_type);
1417 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
1418 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
1419 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
1420 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
1430 Expression ResolveOperator (EmitContext ec)
1433 Type r = right.Type;
1436 // Step 1: Perform Operator Overload location
1438 Expression left_expr, right_expr;
1440 string op = "op_" + oper;
1442 left_expr = MemberLookup (ec, l, op, false, loc);
1443 if (left_expr == null && l.BaseType != null)
1444 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
1446 right_expr = MemberLookup (ec, r, op, false, loc);
1447 if (right_expr == null && r.BaseType != null)
1448 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
1450 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1452 if (union != null) {
1453 Arguments = new ArrayList ();
1454 Arguments.Add (new Argument (left, Argument.AType.Expression));
1455 Arguments.Add (new Argument (right, Argument.AType.Expression));
1457 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
1458 if (method != null) {
1459 MethodInfo mi = (MethodInfo) method;
1460 type = mi.ReturnType;
1469 // Step 2: Default operations on CLI native types.
1472 // Only perform numeric promotions on:
1473 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1475 if (oper == Operator.Addition){
1477 // If any of the arguments is a string, cast to string
1479 if (l == TypeManager.string_type){
1480 if (r == TypeManager.string_type){
1481 if (left is Constant && right is Constant){
1482 StringConstant ls = (StringConstant) left;
1483 StringConstant rs = (StringConstant) right;
1485 return new StringConstant (
1486 ls.Value + rs.Value);
1490 method = TypeManager.string_concat_string_string;
1493 method = TypeManager.string_concat_object_object;
1494 right = ConvertImplicit (ec, right,
1495 TypeManager.object_type, loc);
1497 type = TypeManager.string_type;
1499 Arguments = new ArrayList ();
1500 Arguments.Add (new Argument (left, Argument.AType.Expression));
1501 Arguments.Add (new Argument (right, Argument.AType.Expression));
1505 } else if (r == TypeManager.string_type){
1507 method = TypeManager.string_concat_object_object;
1508 Arguments = new ArrayList ();
1509 Arguments.Add (new Argument (left, Argument.AType.Expression));
1510 Arguments.Add (new Argument (right, Argument.AType.Expression));
1512 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1513 type = TypeManager.string_type;
1519 if (oper == Operator.Addition || oper == Operator.Subtraction) {
1520 if (l.IsSubclassOf (TypeManager.delegate_type) &&
1521 r.IsSubclassOf (TypeManager.delegate_type)) {
1523 Arguments = new ArrayList ();
1524 Arguments.Add (new Argument (left, Argument.AType.Expression));
1525 Arguments.Add (new Argument (right, Argument.AType.Expression));
1527 if (oper == Operator.Addition)
1528 method = TypeManager.delegate_combine_delegate_delegate;
1530 method = TypeManager.delegate_remove_delegate_delegate;
1532 DelegateOperation = true;
1539 // Enumeration operators
1541 bool lie = TypeManager.IsEnumType (l);
1542 bool rie = TypeManager.IsEnumType (r);
1547 temp = ConvertImplicit (ec, right, l, loc);
1551 temp = ConvertImplicit (ec, left, r, loc);
1558 if (oper == Operator.Equality || oper == Operator.Inequality ||
1559 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
1560 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
1561 type = TypeManager.bool_type;
1565 if (oper == Operator.BitwiseAnd ||
1566 oper == Operator.BitwiseOr ||
1567 oper == Operator.ExclusiveOr){
1573 if (oper == Operator.LeftShift || oper == Operator.RightShift)
1574 return CheckShiftArguments (ec);
1576 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1577 if (l != TypeManager.bool_type || r != TypeManager.bool_type){
1582 type = TypeManager.bool_type;
1586 if (oper == Operator.Equality || oper == Operator.Inequality){
1587 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
1588 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
1593 type = TypeManager.bool_type;
1598 // operator != (object a, object b)
1599 // operator == (object a, object b)
1601 // For this to be used, both arguments have to be reference-types.
1602 // Read the rationale on the spec (14.9.6)
1604 // Also, if at compile time we know that the classes do not inherit
1605 // one from the other, then we catch the error there.
1607 if (!(l.IsValueType || r.IsValueType)){
1608 type = TypeManager.bool_type;
1613 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
1617 // We are going to have to convert to an object to compare
1619 if (l != TypeManager.object_type)
1620 left = new EmptyCast (left, TypeManager.object_type);
1621 if (r != TypeManager.object_type)
1622 right = new EmptyCast (right, TypeManager.object_type);
1629 // We are dealing with numbers
1632 if (!DoNumericPromotions (ec, l, r)){
1637 if (left == null || right == null)
1641 // reload our cached types if required
1646 if (oper == Operator.BitwiseAnd ||
1647 oper == Operator.BitwiseOr ||
1648 oper == Operator.ExclusiveOr){
1650 if (!((l == TypeManager.int32_type) ||
1651 (l == TypeManager.uint32_type) ||
1652 (l == TypeManager.int64_type) ||
1653 (l == TypeManager.uint64_type)))
1661 if (oper == Operator.Equality ||
1662 oper == Operator.Inequality ||
1663 oper == Operator.LessThanOrEqual ||
1664 oper == Operator.LessThan ||
1665 oper == Operator.GreaterThanOrEqual ||
1666 oper == Operator.GreaterThan){
1667 type = TypeManager.bool_type;
1674 /// Constant expression reducer for binary operations
1676 public Expression ConstantFold (EmitContext ec)
1678 object l = ((Constant) left).GetValue ();
1679 object r = ((Constant) right).GetValue ();
1681 if (l is string && r is string)
1682 return new StringConstant ((string) l + (string) r);
1684 Type result_type = null;
1687 // Enumerator folding
1689 if (left.Type == right.Type && left is EnumConstant)
1690 result_type = left.Type;
1693 case Operator.BitwiseOr:
1694 if ((l is int) && (r is int)){
1696 int res = (int)l | (int)r;
1698 v = new IntConstant (res);
1699 if (result_type == null)
1702 return new EnumConstant (v, result_type);
1706 case Operator.BitwiseAnd:
1707 if ((l is int) && (r is int)){
1709 int res = (int)l & (int)r;
1711 v = new IntConstant (res);
1712 if (result_type == null)
1715 return new EnumConstant (v, result_type);
1723 public override Expression DoResolve (EmitContext ec)
1725 left = left.Resolve (ec);
1726 right = right.Resolve (ec);
1728 if (left == null || right == null)
1731 if (left.Type == null)
1732 throw new Exception (
1733 "Resolve returned non null, but did not set the type! (" +
1734 left + ") at Line: " + loc.Row);
1735 if (right.Type == null)
1736 throw new Exception (
1737 "Resolve returned non null, but did not set the type! (" +
1738 right + ") at Line: "+ loc.Row);
1740 eclass = ExprClass.Value;
1742 if (left is Constant && right is Constant){
1744 // This is temporary until we do the full folding
1746 Expression e = ConstantFold (ec);
1751 return ResolveOperator (ec);
1754 public bool IsBranchable ()
1756 if (oper == Operator.Equality ||
1757 oper == Operator.Inequality ||
1758 oper == Operator.LessThan ||
1759 oper == Operator.GreaterThan ||
1760 oper == Operator.LessThanOrEqual ||
1761 oper == Operator.GreaterThanOrEqual){
1768 /// This entry point is used by routines that might want
1769 /// to emit a brfalse/brtrue after an expression, and instead
1770 /// they could use a more compact notation.
1772 /// Typically the code would generate l.emit/r.emit, followed
1773 /// by the comparission and then a brtrue/brfalse. The comparissions
1774 /// are sometimes inneficient (there are not as complete as the branches
1775 /// look for the hacks in Emit using double ceqs).
1777 /// So for those cases we provide EmitBranchable that can emit the
1778 /// branch with the test
1780 public void EmitBranchable (EmitContext ec, int target)
1783 bool close_target = false;
1784 ILGenerator ig = ec.ig;
1787 // short-circuit operators
1789 if (oper == Operator.LogicalAnd){
1791 ig.Emit (OpCodes.Brfalse, target);
1793 ig.Emit (OpCodes.Brfalse, target);
1794 } else if (oper == Operator.LogicalOr){
1796 ig.Emit (OpCodes.Brtrue, target);
1798 ig.Emit (OpCodes.Brfalse, target);
1805 case Operator.Equality:
1807 opcode = OpCodes.Beq_S;
1809 opcode = OpCodes.Beq;
1812 case Operator.Inequality:
1814 opcode = OpCodes.Bne_Un_S;
1816 opcode = OpCodes.Bne_Un;
1819 case Operator.LessThan:
1821 opcode = OpCodes.Blt_S;
1823 opcode = OpCodes.Blt;
1826 case Operator.GreaterThan:
1828 opcode = OpCodes.Bgt_S;
1830 opcode = OpCodes.Bgt;
1833 case Operator.LessThanOrEqual:
1835 opcode = OpCodes.Ble_S;
1837 opcode = OpCodes.Ble;
1840 case Operator.GreaterThanOrEqual:
1842 opcode = OpCodes.Bge_S;
1844 opcode = OpCodes.Ble;
1848 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1849 + oper.ToString ());
1852 ig.Emit (opcode, target);
1855 public override void Emit (EmitContext ec)
1857 ILGenerator ig = ec.ig;
1859 Type r = right.Type;
1862 if (method != null) {
1864 // Note that operators are static anyway
1866 if (Arguments != null)
1867 Invocation.EmitArguments (ec, method, Arguments);
1869 if (method is MethodInfo)
1870 ig.Emit (OpCodes.Call, (MethodInfo) method);
1872 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1874 if (DelegateOperation)
1875 ig.Emit (OpCodes.Castclass, type);
1881 // Handle short-circuit operators differently
1884 if (oper == Operator.LogicalAnd){
1885 Label load_zero = ig.DefineLabel ();
1886 Label end = ig.DefineLabel ();
1889 ig.Emit (OpCodes.Brfalse, load_zero);
1891 ig.Emit (OpCodes.Br, end);
1892 ig.MarkLabel (load_zero);
1893 ig.Emit (OpCodes.Ldc_I4_0);
1896 } else if (oper == Operator.LogicalOr){
1897 Label load_one = ig.DefineLabel ();
1898 Label end = ig.DefineLabel ();
1901 ig.Emit (OpCodes.Brtrue, load_one);
1903 ig.Emit (OpCodes.Br, end);
1904 ig.MarkLabel (load_one);
1905 ig.Emit (OpCodes.Ldc_I4_1);
1914 case Operator.Multiply:
1916 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1917 opcode = OpCodes.Mul_Ovf;
1918 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1919 opcode = OpCodes.Mul_Ovf_Un;
1921 opcode = OpCodes.Mul;
1923 opcode = OpCodes.Mul;
1927 case Operator.Division:
1928 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1929 opcode = OpCodes.Div_Un;
1931 opcode = OpCodes.Div;
1934 case Operator.Modulus:
1935 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1936 opcode = OpCodes.Rem_Un;
1938 opcode = OpCodes.Rem;
1941 case Operator.Addition:
1943 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1944 opcode = OpCodes.Add_Ovf;
1945 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1946 opcode = OpCodes.Add_Ovf_Un;
1948 opcode = OpCodes.Mul;
1950 opcode = OpCodes.Add;
1953 case Operator.Subtraction:
1955 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1956 opcode = OpCodes.Sub_Ovf;
1957 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1958 opcode = OpCodes.Sub_Ovf_Un;
1960 opcode = OpCodes.Sub;
1962 opcode = OpCodes.Sub;
1965 case Operator.RightShift:
1966 opcode = OpCodes.Shr;
1969 case Operator.LeftShift:
1970 opcode = OpCodes.Shl;
1973 case Operator.Equality:
1974 opcode = OpCodes.Ceq;
1977 case Operator.Inequality:
1978 ec.ig.Emit (OpCodes.Ceq);
1979 ec.ig.Emit (OpCodes.Ldc_I4_0);
1981 opcode = OpCodes.Ceq;
1984 case Operator.LessThan:
1985 opcode = OpCodes.Clt;
1988 case Operator.GreaterThan:
1989 opcode = OpCodes.Cgt;
1992 case Operator.LessThanOrEqual:
1993 ec.ig.Emit (OpCodes.Cgt);
1994 ec.ig.Emit (OpCodes.Ldc_I4_0);
1996 opcode = OpCodes.Ceq;
1999 case Operator.GreaterThanOrEqual:
2000 ec.ig.Emit (OpCodes.Clt);
2001 ec.ig.Emit (OpCodes.Ldc_I4_1);
2003 opcode = OpCodes.Sub;
2006 case Operator.BitwiseOr:
2007 opcode = OpCodes.Or;
2010 case Operator.BitwiseAnd:
2011 opcode = OpCodes.And;
2014 case Operator.ExclusiveOr:
2015 opcode = OpCodes.Xor;
2019 throw new Exception ("This should not happen: Operator = "
2020 + oper.ToString ());
2028 /// Implements the ternary conditiona operator (?:)
2030 public class Conditional : Expression {
2031 Expression expr, trueExpr, falseExpr;
2034 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2037 this.trueExpr = trueExpr;
2038 this.falseExpr = falseExpr;
2042 public Expression Expr {
2048 public Expression TrueExpr {
2054 public Expression FalseExpr {
2060 public override Expression DoResolve (EmitContext ec)
2062 expr = expr.Resolve (ec);
2064 if (expr.Type != TypeManager.bool_type)
2065 expr = Expression.ConvertImplicitRequired (
2066 ec, expr, TypeManager.bool_type, loc);
2068 trueExpr = trueExpr.Resolve (ec);
2069 falseExpr = falseExpr.Resolve (ec);
2071 if (expr == null || trueExpr == null || falseExpr == null)
2074 if (trueExpr.Type == falseExpr.Type)
2075 type = trueExpr.Type;
2080 // First, if an implicit conversion exists from trueExpr
2081 // to falseExpr, then the result type is of type falseExpr.Type
2083 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
2085 type = falseExpr.Type;
2087 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
2088 type = trueExpr.Type;
2091 Error (173, loc, "The type of the conditional expression can " +
2092 "not be computed because there is no implicit conversion" +
2093 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2094 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2099 if (expr is BoolConstant){
2100 BoolConstant bc = (BoolConstant) expr;
2108 eclass = ExprClass.Value;
2112 public override void Emit (EmitContext ec)
2114 ILGenerator ig = ec.ig;
2115 Label false_target = ig.DefineLabel ();
2116 Label end_target = ig.DefineLabel ();
2119 ig.Emit (OpCodes.Brfalse, false_target);
2121 ig.Emit (OpCodes.Br, end_target);
2122 ig.MarkLabel (false_target);
2123 falseExpr.Emit (ec);
2124 ig.MarkLabel (end_target);
2132 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
2133 public readonly string Name;
2134 public readonly Block Block;
2136 VariableInfo variable_info;
2138 public LocalVariableReference (Block block, string name, Location l)
2143 eclass = ExprClass.Variable;
2146 public VariableInfo VariableInfo {
2148 if (variable_info == null)
2149 variable_info = Block.GetVariableInfo (Name);
2150 return variable_info;
2154 public override Expression DoResolve (EmitContext ec)
2156 VariableInfo vi = VariableInfo;
2158 if (Block.IsConstant (Name)) {
2159 Expression e = Block.GetConstantExpression (Name);
2165 if (!(e is Constant)) {
2166 Report.Error (150, loc, "A constant value is expected");
2174 type = vi.VariableType;
2178 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2180 Expression e = DoResolve (ec);
2185 VariableInfo vi = VariableInfo;
2191 "cannot assign to `" + Name + "' because it is readonly");
2199 public override void Emit (EmitContext ec)
2201 VariableInfo vi = VariableInfo;
2202 ILGenerator ig = ec.ig;
2209 ig.Emit (OpCodes.Ldloc_0);
2213 ig.Emit (OpCodes.Ldloc_1);
2217 ig.Emit (OpCodes.Ldloc_2);
2221 ig.Emit (OpCodes.Ldloc_3);
2226 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2228 ig.Emit (OpCodes.Ldloc, idx);
2233 public static void Store (ILGenerator ig, int idx)
2237 ig.Emit (OpCodes.Stloc_0);
2241 ig.Emit (OpCodes.Stloc_1);
2245 ig.Emit (OpCodes.Stloc_2);
2249 ig.Emit (OpCodes.Stloc_3);
2254 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2256 ig.Emit (OpCodes.Stloc, idx);
2261 public void EmitAssign (EmitContext ec, Expression source)
2263 ILGenerator ig = ec.ig;
2264 VariableInfo vi = VariableInfo;
2270 // Funny seems the code below generates optimal code for us, but
2271 // seems to take too long to generate what we need.
2272 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
2277 public void AddressOf (EmitContext ec)
2279 VariableInfo vi = VariableInfo;
2286 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2288 ec.ig.Emit (OpCodes.Ldloca, idx);
2293 /// This represents a reference to a parameter in the intermediate
2296 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
2302 public ParameterReference (Parameters pars, int idx, string name)
2307 eclass = ExprClass.Variable;
2311 // Notice that for ref/out parameters, the type exposed is not the
2312 // same type exposed externally.
2315 // externally we expose "int&"
2316 // here we expose "int".
2318 // We record this in "is_ref". This means that the type system can treat
2319 // the type as it is expected, but when we generate the code, we generate
2320 // the alternate kind of code.
2322 public override Expression DoResolve (EmitContext ec)
2324 type = pars.GetParameterInfo (ec.TypeContainer, idx, out is_ref);
2325 eclass = ExprClass.Variable;
2330 public override void Emit (EmitContext ec)
2332 ILGenerator ig = ec.ig;
2339 ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
2341 ig.Emit (OpCodes.Ldarg, arg_idx);
2347 // If we are a reference, we loaded on the stack a pointer
2348 // Now lets load the real value
2351 if (type == TypeManager.int32_type)
2352 ig.Emit (OpCodes.Ldind_I4);
2353 else if (type == TypeManager.uint32_type)
2354 ig.Emit (OpCodes.Ldind_U4);
2355 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2356 ig.Emit (OpCodes.Ldind_I8);
2357 else if (type == TypeManager.char_type)
2358 ig.Emit (OpCodes.Ldind_U2);
2359 else if (type == TypeManager.short_type)
2360 ig.Emit (OpCodes.Ldind_I2);
2361 else if (type == TypeManager.ushort_type)
2362 ig.Emit (OpCodes.Ldind_U2);
2363 else if (type == TypeManager.float_type)
2364 ig.Emit (OpCodes.Ldind_R4);
2365 else if (type == TypeManager.double_type)
2366 ig.Emit (OpCodes.Ldind_R8);
2367 else if (type == TypeManager.byte_type)
2368 ig.Emit (OpCodes.Ldind_U1);
2369 else if (type == TypeManager.sbyte_type || type == TypeManager.bool_type)
2370 ig.Emit (OpCodes.Ldind_I1);
2371 else if (type == TypeManager.intptr_type)
2372 ig.Emit (OpCodes.Ldind_I);
2374 ig.Emit (OpCodes.Ldind_Ref);
2377 public void EmitAssign (EmitContext ec, Expression source)
2379 ILGenerator ig = ec.ig;
2388 ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
2390 ig.Emit (OpCodes.Ldarg, arg_idx);
2396 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2397 ig.Emit (OpCodes.Stind_I4);
2398 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2399 ig.Emit (OpCodes.Stind_I8);
2400 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2401 type == TypeManager.ushort_type)
2402 ig.Emit (OpCodes.Stind_I2);
2403 else if (type == TypeManager.float_type)
2404 ig.Emit (OpCodes.Stind_R4);
2405 else if (type == TypeManager.double_type)
2406 ig.Emit (OpCodes.Stind_R8);
2407 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2408 type == TypeManager.bool_type)
2409 ig.Emit (OpCodes.Stind_I1);
2410 else if (type == TypeManager.intptr_type)
2411 ig.Emit (OpCodes.Stind_I);
2413 ig.Emit (OpCodes.Stind_Ref);
2416 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
2418 ig.Emit (OpCodes.Starg, arg_idx);
2423 public void AddressOf (EmitContext ec)
2431 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
2433 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
2438 /// Used for arguments to New(), Invocation()
2440 public class Argument {
2441 public enum AType : byte {
2447 public readonly AType ArgType;
2448 public Expression expr;
2450 public Argument (Expression expr, AType type)
2453 this.ArgType = type;
2456 public Expression Expr {
2472 public Parameter.Modifier GetParameterModifier ()
2474 if (ArgType == AType.Ref)
2475 return Parameter.Modifier.REF;
2477 if (ArgType == AType.Out)
2478 return Parameter.Modifier.OUT;
2480 return Parameter.Modifier.NONE;
2483 public static string FullDesc (Argument a)
2485 return (a.ArgType == AType.Ref ? "ref " :
2486 (a.ArgType == AType.Out ? "out " : "")) +
2487 TypeManager.CSharpName (a.Expr.Type);
2490 public bool Resolve (EmitContext ec, Location loc)
2492 expr = expr.Resolve (ec);
2494 if (ArgType == AType.Expression)
2495 return expr != null;
2497 if (expr.eclass != ExprClass.Variable){
2498 Report.Error (206, loc,
2499 "A property or indexer can not be passed as an out or ref " +
2504 return expr != null;
2507 public void Emit (EmitContext ec)
2509 if (ArgType == AType.Ref || ArgType == AType.Out)
2510 ((IMemoryLocation)expr).AddressOf (ec);
2517 /// Invocation of methods or delegates.
2519 public class Invocation : ExpressionStatement {
2520 public readonly ArrayList Arguments;
2524 MethodBase method = null;
2526 static Hashtable method_parameter_cache;
2528 static Invocation ()
2530 method_parameter_cache = new PtrHashtable ();
2534 // arguments is an ArrayList, but we do not want to typecast,
2535 // as it might be null.
2537 // FIXME: only allow expr to be a method invocation or a
2538 // delegate invocation (7.5.5)
2540 public Invocation (Expression expr, ArrayList arguments, Location l)
2543 Arguments = arguments;
2547 public Expression Expr {
2554 /// Returns the Parameters (a ParameterData interface) for the
2557 public static ParameterData GetParameterData (MethodBase mb)
2559 object pd = method_parameter_cache [mb];
2563 return (ParameterData) pd;
2566 ip = TypeManager.LookupParametersByBuilder (mb);
2568 method_parameter_cache [mb] = ip;
2570 return (ParameterData) ip;
2572 ParameterInfo [] pi = mb.GetParameters ();
2573 ReflectionParameters rp = new ReflectionParameters (pi);
2574 method_parameter_cache [mb] = rp;
2576 return (ParameterData) rp;
2581 /// Tells whether a user defined conversion from Type `from' to
2582 /// Type `to' exists.
2584 /// FIXME: we could implement a cache here.
2586 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
2588 // Locate user-defined implicit operators
2592 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
2595 MethodGroupExpr me = (MethodGroupExpr) mg;
2597 for (int i = me.Methods.Length; i > 0;) {
2599 MethodBase mb = me.Methods [i];
2600 ParameterData pd = GetParameterData (mb);
2602 if (from == pd.ParameterType (0))
2607 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
2610 MethodGroupExpr me = (MethodGroupExpr) mg;
2612 for (int i = me.Methods.Length; i > 0;) {
2614 MethodBase mb = me.Methods [i];
2615 MethodInfo mi = (MethodInfo) mb;
2617 if (mi.ReturnType == to)
2626 /// Determines "better conversion" as specified in 7.4.2.3
2627 /// Returns : 1 if a->p is better
2628 /// 0 if a->q or neither is better
2630 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
2632 Type argument_type = a.Type;
2633 Expression argument_expr = a.Expr;
2635 if (argument_type == null)
2636 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2641 if (argument_type == p)
2644 if (argument_type == q)
2648 // Now probe whether an implicit constant expression conversion
2651 // An implicit constant expression conversion permits the following
2654 // * A constant-expression of type `int' can be converted to type
2655 // sbyte, byute, short, ushort, uint, ulong provided the value of
2656 // of the expression is withing the range of the destination type.
2658 // * A constant-expression of type long can be converted to type
2659 // ulong, provided the value of the constant expression is not negative
2661 // FIXME: Note that this assumes that constant folding has
2662 // taken place. We dont do constant folding yet.
2665 if (argument_expr is IntConstant){
2666 IntConstant ei = (IntConstant) argument_expr;
2667 int value = ei.Value;
2669 if (p == TypeManager.sbyte_type){
2670 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2672 } else if (p == TypeManager.byte_type){
2673 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2675 } else if (p == TypeManager.short_type){
2676 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2678 } else if (p == TypeManager.ushort_type){
2679 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2681 } else if (p == TypeManager.uint32_type){
2683 // we can optimize this case: a positive int32
2684 // always fits on a uint32
2688 } else if (p == TypeManager.uint64_type){
2690 // we can optimize this case: a positive int32
2691 // always fits on a uint64
2696 } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
2697 LongConstant lc = (LongConstant) argument_expr;
2699 if (p == TypeManager.uint64_type){
2709 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
2718 if (ConversionExists (ec, p, q, loc) == true &&
2719 ConversionExists (ec, q, p, loc) == false)
2722 if (p == TypeManager.sbyte_type)
2723 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2724 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2727 if (p == TypeManager.short_type)
2728 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2729 q == TypeManager.uint64_type)
2732 if (p == TypeManager.int32_type)
2733 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2736 if (p == TypeManager.int64_type)
2737 if (q == TypeManager.uint64_type)
2744 /// Determines "Better function"
2747 /// and returns an integer indicating :
2748 /// 0 if candidate ain't better
2749 /// 1 if candidate is better than the current best match
2751 static int BetterFunction (EmitContext ec, ArrayList args,
2752 MethodBase candidate, MethodBase best,
2753 bool expanded_form, Location loc)
2755 ParameterData candidate_pd = GetParameterData (candidate);
2756 ParameterData best_pd;
2763 argument_count = args.Count;
2765 if (candidate_pd.Count == 0 && argument_count == 0)
2768 if (candidate_pd.ParameterModifier (candidate_pd.Count - 1) != Parameter.Modifier.PARAMS)
2769 if (candidate_pd.Count != argument_count)
2774 for (int j = argument_count; j > 0;) {
2777 Argument a = (Argument) args [j];
2778 Type t = candidate_pd.ParameterType (j);
2780 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
2782 t = t.GetElementType ();
2784 x = BetterConversion (ec, a, t, null, loc);
2796 best_pd = GetParameterData (best);
2798 int rating1 = 0, rating2 = 0;
2800 for (int j = 0; j < argument_count; ++j) {
2803 Argument a = (Argument) args [j];
2805 Type ct = candidate_pd.ParameterType (j);
2806 Type bt = best_pd.ParameterType (j);
2808 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
2810 ct = ct.GetElementType ();
2812 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
2814 bt = bt.GetElementType ();
2816 x = BetterConversion (ec, a, ct, bt, loc);
2817 y = BetterConversion (ec, a, bt, ct, loc);
2826 if (rating1 > rating2)
2832 public static string FullMethodDesc (MethodBase mb)
2834 StringBuilder sb = new StringBuilder (mb.Name);
2835 ParameterData pd = GetParameterData (mb);
2837 int count = pd.Count;
2840 for (int i = count; i > 0; ) {
2843 sb.Append (pd.ParameterDesc (count - i - 1));
2849 return sb.ToString ();
2852 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2854 MemberInfo [] miset;
2855 MethodGroupExpr union;
2857 if (mg1 != null && mg2 != null) {
2859 MethodGroupExpr left_set = null, right_set = null;
2860 int length1 = 0, length2 = 0;
2862 left_set = (MethodGroupExpr) mg1;
2863 length1 = left_set.Methods.Length;
2865 right_set = (MethodGroupExpr) mg2;
2866 length2 = right_set.Methods.Length;
2868 ArrayList common = new ArrayList ();
2870 for (int i = 0; i < left_set.Methods.Length; i++) {
2871 for (int j = 0; j < right_set.Methods.Length; j++) {
2872 if (left_set.Methods [i] == right_set.Methods [j])
2873 common.Add (left_set.Methods [i]);
2877 miset = new MemberInfo [length1 + length2 - common.Count];
2879 left_set.Methods.CopyTo (miset, 0);
2883 for (int j = 0; j < right_set.Methods.Length; j++)
2884 if (!common.Contains (right_set.Methods [j]))
2885 miset [length1 + k++] = right_set.Methods [j];
2887 union = new MethodGroupExpr (miset);
2891 } else if (mg1 == null && mg2 != null) {
2893 MethodGroupExpr me = (MethodGroupExpr) mg2;
2895 miset = new MemberInfo [me.Methods.Length];
2896 me.Methods.CopyTo (miset, 0);
2898 union = new MethodGroupExpr (miset);
2902 } else if (mg2 == null && mg1 != null) {
2904 MethodGroupExpr me = (MethodGroupExpr) mg1;
2906 miset = new MemberInfo [me.Methods.Length];
2907 me.Methods.CopyTo (miset, 0);
2909 union = new MethodGroupExpr (miset);
2918 /// Determines is the candidate method, if a params method, is applicable
2919 /// in its expanded form to the given set of arguments
2921 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2925 if (arguments == null)
2928 arg_count = arguments.Count;
2930 ParameterData pd = GetParameterData (candidate);
2932 int pd_count = pd.Count;
2937 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2940 if (pd_count - 1 > arg_count)
2944 // If we have come this far, the case which remains is when the number of parameters
2945 // is less than or equal to the argument count. So, we now check if the element type
2946 // of the params array is compatible with each argument type
2949 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2951 for (int i = pd_count - 1; i < arg_count - 1; i++) {
2952 Argument a = (Argument) arguments [i];
2954 if (!StandardConversionExists (a.Type, element_type))
2962 /// Determines if the candidate method is applicable (section 14.4.2.1)
2963 /// to the given set of arguments
2965 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2969 if (arguments == null)
2972 arg_count = arguments.Count;
2974 ParameterData pd = GetParameterData (candidate);
2976 int pd_count = pd.Count;
2978 if (arg_count != pd.Count)
2981 for (int i = arg_count; i > 0; ) {
2984 Argument a = (Argument) arguments [i];
2986 Parameter.Modifier a_mod = a.GetParameterModifier ();
2987 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2989 if (a_mod == p_mod) {
2991 if (a_mod == Parameter.Modifier.NONE)
2992 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2995 if (a_mod == Parameter.Modifier.REF ||
2996 a_mod == Parameter.Modifier.OUT)
2997 if (pd.ParameterType (i) != a.Type)
3009 /// Find the Applicable Function Members (7.4.2.1)
3011 /// me: Method Group expression with the members to select.
3012 /// it might contain constructors or methods (or anything
3013 /// that maps to a method).
3015 /// Arguments: ArrayList containing resolved Argument objects.
3017 /// loc: The location if we want an error to be reported, or a Null
3018 /// location for "probing" purposes.
3020 /// use_standard: controls whether OverloadResolve should use the
3021 /// ConvertImplicit or ConvertImplicitStandard during overload resolution.
3023 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3024 /// that is the best match of me on Arguments.
3027 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3028 ArrayList Arguments, Location loc)
3030 ArrayList afm = new ArrayList ();
3031 int best_match_idx = -1;
3032 MethodBase method = null;
3034 ArrayList candidates = new ArrayList ();
3036 for (int i = me.Methods.Length; i > 0; ){
3038 MethodBase candidate = me.Methods [i];
3041 // Check if candidate is applicable (section 14.4.2.1)
3042 if (!IsApplicable (Arguments, candidate))
3045 candidates.Add (candidate);
3046 x = BetterFunction (ec, Arguments, candidate, method, false, loc);
3052 method = me.Methods [best_match_idx];
3056 if (Arguments == null)
3059 argument_count = Arguments.Count;
3062 // Now we see if we can find params functions, applicable in their expanded form
3063 // since if they were applicable in their normal form, they would have been selected
3066 bool chose_params_expanded = false;
3068 if (best_match_idx == -1) {
3070 candidates = new ArrayList ();
3071 for (int i = me.Methods.Length; i > 0; ) {
3073 MethodBase candidate = me.Methods [i];
3075 if (!IsParamsMethodApplicable (Arguments, candidate))
3078 candidates.Add (candidate);
3080 int x = BetterFunction (ec, Arguments, candidate, method, true, loc);
3086 method = me.Methods [best_match_idx];
3087 chose_params_expanded = true;
3093 // Now we see if we can at least find a method with the same number of arguments
3096 int method_count = 0;
3098 if (best_match_idx == -1) {
3100 for (int i = me.Methods.Length; i > 0;) {
3102 MethodBase mb = me.Methods [i];
3103 pd = GetParameterData (mb);
3105 if (pd.Count == argument_count) {
3107 method = me.Methods [best_match_idx];
3119 // Now check that there are no ambiguities i.e the selected method
3120 // should be better than all the others
3123 for (int i = 0; i < candidates.Count; ++i) {
3124 MethodBase candidate = (MethodBase) candidates [i];
3126 if (candidate == method)
3129 int x = BetterFunction (ec, Arguments, method, candidate,
3130 chose_params_expanded, loc);
3133 Console.WriteLine (candidate + " " + method);
3136 "Ambiguous call when selecting function due to implicit casts");
3141 // And now convert implicitly, each argument to the required type
3143 pd = GetParameterData (method);
3144 int pd_count = pd.Count;
3146 for (int j = 0; j < argument_count; j++) {
3147 Argument a = (Argument) Arguments [j];
3148 Expression a_expr = a.Expr;
3149 Type parameter_type = pd.ParameterType (j);
3151 if (pd.ParameterModifier (j) == Parameter.Modifier.PARAMS && chose_params_expanded)
3152 parameter_type = parameter_type.GetElementType ();
3154 if (a.Type != parameter_type){
3157 conv = ConvertImplicitStandard (ec, a_expr, parameter_type, Location.Null);
3160 if (!Location.IsNull (loc)) {
3162 "The best overloaded match for method '" +
3163 FullMethodDesc (method) +
3164 "' has some invalid arguments");
3166 "Argument " + (j+1) +
3167 ": Cannot convert from '" + Argument.FullDesc (a)
3168 + "' to '" + pd.ParameterDesc (j) + "'");
3174 // Update the argument with the implicit conversion
3179 // FIXME : For the case of params methods, we need to actually instantiate
3180 // an array and initialize it with the argument values etc etc.
3184 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
3185 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
3186 if (!Location.IsNull (loc)) {
3188 "The best overloaded match for method '" + FullMethodDesc (method)+
3189 "' has some invalid arguments");
3191 "Argument " + (j+1) +
3192 ": Cannot convert from '" + Argument.FullDesc (a)
3193 + "' to '" + pd.ParameterDesc (j) + "'");
3202 public override Expression DoResolve (EmitContext ec)
3205 // First, resolve the expression that is used to
3206 // trigger the invocation
3208 expr = expr.Resolve (ec);
3212 if (!(expr is MethodGroupExpr)) {
3213 Type expr_type = expr.Type;
3215 if (expr_type != null){
3216 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
3218 return (new DelegateInvocation (
3219 this.expr, Arguments, loc)).Resolve (ec);
3223 if (!(expr is MethodGroupExpr)){
3224 report118 (loc, this.expr, "method group");
3229 // Next, evaluate all the expressions in the argument list
3231 if (Arguments != null){
3232 for (int i = Arguments.Count; i > 0;){
3234 Argument a = (Argument) Arguments [i];
3236 if (!a.Resolve (ec, loc))
3241 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
3243 if (method == null){
3245 "Could not find any applicable function for this argument list");
3249 if (method is MethodInfo)
3250 type = ((MethodInfo)method).ReturnType;
3252 eclass = ExprClass.Value;
3257 // Emits the list of arguments as an array
3259 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
3261 ILGenerator ig = ec.ig;
3262 int count = arguments.Count - idx;
3263 Argument a = (Argument) arguments [idx];
3264 Type t = a.expr.Type;
3265 string array_type = t.FullName + "[]";
3268 array = ig.DeclareLocal (Type.GetType (array_type));
3269 IntConstant.EmitInt (ig, count);
3270 ig.Emit (OpCodes.Newarr, t);
3271 ig.Emit (OpCodes.Stloc, array);
3273 int top = arguments.Count;
3274 for (int j = idx; j < top; j++){
3275 a = (Argument) arguments [j];
3277 ig.Emit (OpCodes.Ldloc, array);
3278 IntConstant.EmitInt (ig, j - idx);
3281 ArrayAccess.EmitStoreOpcode (ig, t);
3283 ig.Emit (OpCodes.Ldloc, array);
3287 /// Emits a list of resolved Arguments that are in the arguments
3290 /// The MethodBase argument might be null if the
3291 /// emission of the arguments is known not to contain
3292 /// a `params' field (for example in constructors or other routines
3293 /// that keep their arguments in this structure
3295 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
3297 ParameterData pd = null;
3300 if (arguments != null)
3301 top = arguments.Count;
3306 pd = GetParameterData (mb);
3308 for (int i = 0; i < top; i++){
3309 Argument a = (Argument) arguments [i];
3312 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
3313 EmitParams (ec, i, arguments);
3322 public static void EmitCall (EmitContext ec,
3323 bool is_static, Expression instance_expr,
3324 MethodBase method, ArrayList Arguments)
3326 ILGenerator ig = ec.ig;
3327 bool struct_call = false;
3331 // If this is ourselves, push "this"
3333 if (instance_expr == null){
3334 ig.Emit (OpCodes.Ldarg_0);
3337 // Push the instance expression
3339 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
3341 // Special case: calls to a function declared in a
3342 // reference-type with a value-type argument need
3343 // to have their value boxed.
3346 if (method.DeclaringType.IsValueType){
3348 // If the expression implements IMemoryLocation, then
3349 // we can optimize and use AddressOf on the
3352 // If not we have to use some temporary storage for
3354 if (instance_expr is IMemoryLocation){
3355 ((IMemoryLocation)instance_expr).
3359 Type t = instance_expr.Type;
3361 instance_expr.Emit (ec);
3362 LocalBuilder temp = ig.DeclareLocal (t);
3363 ig.Emit (OpCodes.Stloc, temp);
3364 ig.Emit (OpCodes.Ldloca, temp);
3367 instance_expr.Emit (ec);
3368 ig.Emit (OpCodes.Box, instance_expr.Type);
3371 instance_expr.Emit (ec);
3375 if (Arguments != null)
3376 EmitArguments (ec, method, Arguments);
3378 if (method is MethodInfo){
3379 MethodInfo mi = (MethodInfo) method;
3385 if (is_static || struct_call){
3386 if (method is MethodInfo)
3387 ig.Emit (OpCodes.Call, (MethodInfo) method);
3389 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3391 if (method is MethodInfo)
3392 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3394 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3398 public override void Emit (EmitContext ec)
3400 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3402 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
3405 public override void EmitStatement (EmitContext ec)
3410 // Pop the return value if there is one
3412 if (method is MethodInfo){
3413 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3414 ec.ig.Emit (OpCodes.Pop);
3420 /// Implements the new expression
3422 public class New : ExpressionStatement {
3423 public readonly ArrayList Arguments;
3424 public readonly string RequestedType;
3427 MethodBase method = null;
3430 // If set, the new expression is for a value_target, and
3431 // we will not leave anything on the stack.
3433 Expression value_target;
3435 public New (string requested_type, ArrayList arguments, Location l)
3437 RequestedType = requested_type;
3438 Arguments = arguments;
3442 public Expression ValueTypeVariable {
3444 return value_target;
3448 value_target = value;
3452 public override Expression DoResolve (EmitContext ec)
3454 type = RootContext.LookupType (ec.TypeContainer, RequestedType, false, loc);
3459 bool IsDelegate = TypeManager.IsDelegateType (type);
3462 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
3464 bool is_struct = false;
3465 is_struct = type.IsSubclassOf (TypeManager.value_type);
3466 eclass = ExprClass.Value;
3469 // SRE returns a match for .ctor () on structs (the object constructor),
3470 // so we have to manually ignore it.
3472 if (is_struct && Arguments == null)
3476 ml = MemberLookup (ec, type, ".ctor", false,
3477 MemberTypes.Constructor, AllBindingFlags, loc);
3479 if (! (ml is MethodGroupExpr)){
3481 report118 (loc, ml, "method group");
3487 if (Arguments != null){
3488 for (int i = Arguments.Count; i > 0;){
3490 Argument a = (Argument) Arguments [i];
3492 if (!a.Resolve (ec, loc))
3497 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
3502 if (method == null && !is_struct) {
3504 "New invocation: Can not find a constructor for " +
3505 "this argument list");
3512 // This DoEmit can be invoked in two contexts:
3513 // * As a mechanism that will leave a value on the stack (new object)
3514 // * As one that wont (init struct)
3516 // You can control whether a value is required on the stack by passing
3517 // need_value_on_stack. The code *might* leave a value on the stack
3518 // so it must be popped manually
3520 // If we are dealing with a ValueType, we have a few
3521 // situations to deal with:
3523 // * The target is a ValueType, and we have been provided
3524 // the instance (this is easy, we are being assigned).
3526 // * The target of New is being passed as an argument,
3527 // to a boxing operation or a function that takes a
3530 // In this case, we need to create a temporary variable
3531 // that is the argument of New.
3533 // Returns whether a value is left on the stack
3535 bool DoEmit (EmitContext ec, bool need_value_on_stack)
3537 if (method == null){
3540 if (value_target == null)
3541 value_target = new LocalTemporary (ec, type);
3543 ml = (IMemoryLocation) value_target;
3546 Invocation.EmitArguments (ec, method, Arguments);
3547 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3552 // It must be a value type, sanity check
3554 if (value_target != null){
3555 ec.ig.Emit (OpCodes.Initobj, type);
3557 if (need_value_on_stack){
3558 value_target.Emit (ec);
3564 throw new Exception ("No method and no value type");
3567 public override void Emit (EmitContext ec)
3572 public override void EmitStatement (EmitContext ec)
3574 if (DoEmit (ec, false))
3575 ec.ig.Emit (OpCodes.Pop);
3580 /// Represents an array creation expression.
3584 /// There are two possible scenarios here: one is an array creation
3585 /// expression that specifies the dimensions and optionally the
3586 /// initialization data and the other which does not need dimensions
3587 /// specified but where initialization data is mandatory.
3589 public class ArrayCreation : ExpressionStatement {
3590 string RequestedType;
3592 ArrayList Initializers;
3594 ArrayList Arguments;
3596 MethodBase method = null;
3597 Type array_element_type;
3598 bool IsOneDimensional = false;
3599 bool IsBuiltinType = false;
3600 bool ExpectInitializers = false;
3603 Type underlying_type;
3605 ArrayList ArrayData;
3609 public ArrayCreation (string requested_type, ArrayList exprs,
3610 string rank, ArrayList initializers, Location l)
3612 RequestedType = requested_type;
3614 Initializers = initializers;
3617 Arguments = new ArrayList ();
3619 foreach (Expression e in exprs)
3620 Arguments.Add (new Argument (e, Argument.AType.Expression));
3624 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
3626 RequestedType = requested_type;
3627 Initializers = initializers;
3630 Rank = rank.Substring (0, rank.LastIndexOf ("["));
3632 string tmp = rank.Substring (rank.LastIndexOf ("["));
3634 dimensions = tmp.Length - 1;
3635 ExpectInitializers = true;
3638 public static string FormArrayType (string base_type, int idx_count, string rank)
3640 StringBuilder sb = new StringBuilder (base_type);
3645 for (int i = 1; i < idx_count; i++)
3650 return sb.ToString ();
3653 public static string FormElementType (string base_type, int idx_count, string rank)
3655 StringBuilder sb = new StringBuilder (base_type);
3658 for (int i = 1; i < idx_count; i++)
3665 string val = sb.ToString ();
3667 return val.Substring (0, val.LastIndexOf ("["));
3672 Report.Error (178, loc, "Incorrectly structured array initializer");
3675 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
3677 if (specified_dims) {
3678 Argument a = (Argument) Arguments [idx];
3680 if (!a.Resolve (ec, loc))
3683 if (!(a.Expr is Constant)) {
3684 Report.Error (150, loc, "A constant value is expected");
3688 int value = (int) ((Constant) a.Expr).GetValue ();
3690 if (value != probe.Count) {
3695 Bounds [idx] = value;
3698 foreach (object o in probe) {
3699 if (o is ArrayList) {
3700 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
3704 Expression tmp = (Expression) o;
3705 tmp = tmp.Resolve (ec);
3709 // Handle initialization from vars, fields etc.
3711 Expression conv = ConvertImplicitRequired (
3712 ec, tmp, underlying_type, loc);
3717 if (conv is StringConstant)
3718 ArrayData.Add (conv);
3719 else if (conv is Constant)
3720 ArrayData.Add (((Constant) conv).GetValue ());
3722 ArrayData.Add (conv);
3729 public void UpdateIndices (EmitContext ec)
3732 for (ArrayList probe = Initializers; probe != null;) {
3733 if (probe.Count > 0 && probe [0] is ArrayList) {
3734 Expression e = new IntConstant (probe.Count);
3735 Arguments.Add (new Argument (e, Argument.AType.Expression));
3737 Bounds [i++] = probe.Count;
3739 probe = (ArrayList) probe [0];
3742 Expression e = new IntConstant (probe.Count);
3743 Arguments.Add (new Argument (e, Argument.AType.Expression));
3745 Bounds [i++] = probe.Count;
3752 public bool ValidateInitializers (EmitContext ec)
3754 if (Initializers == null) {
3755 if (ExpectInitializers)
3761 underlying_type = RootContext.LookupType (
3762 ec.TypeContainer, RequestedType, false, loc);
3765 // We use this to store all the date values in the order in which we
3766 // will need to store them in the byte blob later
3768 ArrayData = new ArrayList ();
3769 Bounds = new Hashtable ();
3773 if (Arguments != null) {
3774 ret = CheckIndices (ec, Initializers, 0, true);
3778 Arguments = new ArrayList ();
3780 ret = CheckIndices (ec, Initializers, 0, false);
3787 if (Arguments.Count != dimensions) {
3796 public override Expression DoResolve (EmitContext ec)
3800 if (!ValidateInitializers (ec))
3803 if (Arguments == null)
3806 arg_count = Arguments.Count;
3807 for (int i = arg_count; i > 0;){
3809 Argument a = (Argument) Arguments [i];
3811 if (!a.Resolve (ec, loc))
3816 string array_type = FormArrayType (RequestedType, arg_count, Rank);
3817 string element_type = FormElementType (RequestedType, arg_count, Rank);
3819 type = RootContext.LookupType (ec.TypeContainer, array_type, false, loc);
3821 array_element_type = RootContext.LookupType (
3822 ec.TypeContainer, element_type, false, loc);
3827 if (arg_count == 1) {
3828 IsOneDimensional = true;
3829 eclass = ExprClass.Value;
3833 IsBuiltinType = TypeManager.IsBuiltinType (type);
3835 if (IsBuiltinType) {
3839 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
3840 AllBindingFlags, loc);
3842 if (!(ml is MethodGroupExpr)){
3843 report118 (loc, ml, "method group");
3848 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3849 "this argument list");
3853 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3855 if (method == null) {
3856 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3857 "this argument list");
3861 eclass = ExprClass.Value;
3865 ModuleBuilder mb = RootContext.ModuleBuilder;
3867 ArrayList args = new ArrayList ();
3868 if (Arguments != null){
3869 for (int i = arg_count; i > 0;){
3871 Argument a = (Argument) Arguments [i];
3877 Type [] arg_types = null;
3880 arg_types = new Type [args.Count];
3882 args.CopyTo (arg_types, 0);
3884 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3887 if (method == null) {
3888 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3889 "this argument list");
3893 eclass = ExprClass.Value;
3899 public static byte [] MakeByteBlob (ArrayList ArrayData, Type underlying_type, Location loc)
3904 int count = ArrayData.Count;
3906 if (underlying_type == TypeManager.int32_type ||
3907 underlying_type == TypeManager.uint32_type ||
3908 underlying_type == TypeManager.float_type)
3910 else if (underlying_type == TypeManager.int64_type ||
3911 underlying_type == TypeManager.uint64_type ||
3912 underlying_type == TypeManager.double_type)
3914 else if (underlying_type == TypeManager.byte_type ||
3915 underlying_type == TypeManager.sbyte_type ||
3916 underlying_type == TypeManager.bool_type)
3918 else if (underlying_type == TypeManager.short_type ||
3919 underlying_type == TypeManager.char_type ||
3920 underlying_type == TypeManager.ushort_type)
3925 data = new byte [count * factor];
3928 for (int i = 0; i < count; ++i) {
3929 object v = ArrayData [i];
3931 if (v is EnumConstant)
3932 v = ((EnumConstant) v).Child;
3934 if (underlying_type == TypeManager.int64_type){
3936 if (!(v is Expression))
3939 for (int j = 0; j < factor; ++j) {
3940 data [idx + j] = (byte) (val & 0xFF);
3943 } else if (underlying_type == TypeManager.uint64_type){
3945 if (!(v is Expression))
3948 for (int j = 0; j < factor; ++j) {
3949 data [idx + j] = (byte) (val & 0xFF);
3952 } else if (underlying_type == TypeManager.float_type) {
3958 if (!(v is Expression))
3961 byte *ptr = (byte *) &val;
3963 for (int j = 0; j < factor; ++j)
3964 data [idx + j] = (byte) ptr [j];
3967 } else if (underlying_type == TypeManager.double_type) {
3973 if (!(v is Expression))
3976 byte *ptr = (byte *) &val;
3978 for (int j = 0; j < factor; ++j)
3979 data [idx + j] = (byte) ptr [j];
3982 } else if (underlying_type == TypeManager.char_type){
3985 if (!(v is Expression))
3986 v = (int) ((char) v);
3988 data [idx] = (byte) (val & 0xff);
3989 data [idx+1] = (byte) (val >> 8);
3991 } else if (underlying_type == TypeManager.short_type){
3994 if (!(v is Expression))
3995 v = (int) ((short) v);
3997 data [idx] = (byte) (val & 0xff);
3998 data [idx+1] = (byte) (val >> 8);
4000 } else if (underlying_type == TypeManager.ushort_type){
4003 if (!(v is Expression))
4004 v = (int) ((ushort) v);
4006 data [idx] = (byte) (val & 0xff);
4007 data [idx+1] = (byte) (val >> 8);
4009 } else if (underlying_type == TypeManager.int32_type) {
4012 if (!(v is Expression))
4015 data [idx] = (byte) (val & 0xff);
4016 data [idx+1] = (byte) ((val >> 8) & 0xff);
4017 data [idx+2] = (byte) ((val >> 16) & 0xff);
4018 data [idx+3] = (byte) (val >> 24);
4019 } else if (underlying_type == TypeManager.uint32_type) {
4022 if (!(v is Expression))
4025 data [idx] = (byte) (val & 0xff);
4026 data [idx+1] = (byte) ((val >> 8) & 0xff);
4027 data [idx+2] = (byte) ((val >> 16) & 0xff);
4028 data [idx+3] = (byte) (val >> 24);
4029 } else if (underlying_type == TypeManager.sbyte_type) {
4032 if (!(v is Expression))
4035 data [idx] = (byte) val;
4036 } else if (underlying_type == TypeManager.byte_type) {
4039 if (!(v is Expression))
4042 data [idx] = (byte) val;
4044 throw new Exception ("Unrecognized type in MakeByteBlob");
4053 // Emits the initializers for the array
4055 void EmitStaticInitializers (EmitContext ec, bool is_expression)
4058 // First, the static data
4061 ILGenerator ig = ec.ig;
4063 byte [] data = MakeByteBlob (ArrayData, underlying_type, loc);
4066 fb = RootContext.MakeStaticData (data);
4069 ig.Emit (OpCodes.Dup);
4070 ig.Emit (OpCodes.Ldtoken, fb);
4071 ig.Emit (OpCodes.Call,
4072 TypeManager.void_initializearray_array_fieldhandle);
4077 // Emits pieces of the array that can not be computed at compile
4078 // time (variables and string locations).
4080 // This always expect the top value on the stack to be the array
4082 void EmitDynamicInitializers (EmitContext ec, bool is_expression)
4084 ILGenerator ig = ec.ig;
4085 int dims = Bounds.Count;
4086 int [] current_pos = new int [dims];
4087 int top = ArrayData.Count;
4088 LocalBuilder temp = ig.DeclareLocal (type);
4090 ig.Emit (OpCodes.Stloc, temp);
4092 MethodInfo set = null;
4096 ModuleBuilder mb = null;
4097 mb = RootContext.ModuleBuilder;
4098 args = new Type [dims + 1];
4101 for (j = 0; j < dims; j++)
4102 args [j] = TypeManager.int32_type;
4104 args [j] = array_element_type;
4106 set = mb.GetArrayMethod (
4108 CallingConventions.HasThis | CallingConventions.Standard,
4109 TypeManager.void_type, args);
4112 for (int i = 0; i < top; i++){
4114 Expression e = null;
4116 if (ArrayData [i] is Expression)
4117 e = (Expression) ArrayData [i];
4121 // Basically we do this for string literals and
4122 // other non-literal expressions
4124 if (e is StringConstant || !(e is Constant)) {
4126 ig.Emit (OpCodes.Ldloc, temp);
4128 for (int idx = dims; idx > 0; ) {
4130 IntConstant.EmitInt (ig, current_pos [idx]);
4136 ArrayAccess.EmitStoreOpcode (ig, array_element_type);
4138 ig.Emit (OpCodes.Call, set);
4146 for (int j = 0; j < dims; j++){
4148 if (current_pos [j] < (int) Bounds [j])
4150 current_pos [j] = 0;
4155 ig.Emit (OpCodes.Ldloc, temp);
4158 void DoEmit (EmitContext ec, bool is_statement)
4160 ILGenerator ig = ec.ig;
4162 if (IsOneDimensional) {
4163 Invocation.EmitArguments (ec, null, Arguments);
4164 ig.Emit (OpCodes.Newarr, array_element_type);
4167 Invocation.EmitArguments (ec, null, Arguments);
4170 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4172 ig.Emit (OpCodes.Newobj, (MethodInfo) method);
4175 if (Initializers != null){
4177 // FIXME: Set this variable correctly.
4179 bool dynamic_initializers = true;
4181 if (underlying_type != TypeManager.string_type &&
4182 underlying_type != TypeManager.object_type)
4183 EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
4185 if (dynamic_initializers)
4186 EmitDynamicInitializers (ec, !is_statement);
4190 public override void Emit (EmitContext ec)
4195 public override void EmitStatement (EmitContext ec)
4203 /// Represents the `this' construct
4205 public class This : Expression, IAssignMethod, IMemoryLocation {
4208 public This (Location loc)
4213 public override Expression DoResolve (EmitContext ec)
4215 eclass = ExprClass.Variable;
4216 type = ec.TypeContainer.TypeBuilder;
4219 Report.Error (26, loc,
4220 "Keyword this not valid in static code");
4227 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4231 if (ec.TypeContainer is Class){
4232 Report.Error (1604, loc, "Cannot assign to `this'");
4239 public override void Emit (EmitContext ec)
4241 ec.ig.Emit (OpCodes.Ldarg_0);
4244 public void EmitAssign (EmitContext ec, Expression source)
4247 ec.ig.Emit (OpCodes.Starg, 0);
4250 public void AddressOf (EmitContext ec)
4252 ec.ig.Emit (OpCodes.Ldarg_0);
4255 // FIGURE OUT WHY LDARG_S does not work
4257 // consider: struct X { int val; int P { set { val = value; }}}
4259 // Yes, this looks very bad. Look at `NOTAS' for
4261 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4266 /// Implements the typeof operator
4268 public class TypeOf : Expression {
4269 public readonly string QueriedType;
4273 public TypeOf (string queried_type, Location l)
4275 QueriedType = queried_type;
4279 public override Expression DoResolve (EmitContext ec)
4281 typearg = RootContext.LookupType (
4282 ec.TypeContainer, QueriedType, false, loc);
4284 if (typearg == null)
4287 type = TypeManager.type_type;
4288 eclass = ExprClass.Type;
4292 public override void Emit (EmitContext ec)
4294 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4295 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4300 /// Implements the sizeof expression
4302 public class SizeOf : Expression {
4303 public readonly string QueriedType;
4305 public SizeOf (string queried_type)
4307 this.QueriedType = queried_type;
4310 public override Expression DoResolve (EmitContext ec)
4312 // FIXME: Implement;
4313 throw new Exception ("Unimplemented");
4317 public override void Emit (EmitContext ec)
4319 throw new Exception ("Implement me");
4324 /// Implements the member access expression
4326 public class MemberAccess : Expression {
4327 public readonly string Identifier;
4329 Expression member_lookup;
4332 public MemberAccess (Expression expr, string id, Location l)
4339 public Expression Expr {
4345 static void error176 (Location loc, string name)
4347 Report.Error (176, loc, "Static member `" +
4348 name + "' cannot be accessed " +
4349 "with an instance reference, qualify with a " +
4350 "type name instead");
4354 static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
4356 if (left_original == null)
4359 if (!(left_original is SimpleName))
4362 SimpleName sn = (SimpleName) left_original;
4364 Type t = RootContext.LookupType (ec.TypeContainer, sn.Name, true, loc);
4371 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
4372 Expression left, Location loc,
4373 Expression left_original)
4378 if (member_lookup is MethodGroupExpr){
4379 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4384 if (left is TypeExpr){
4385 if (!mg.RemoveInstanceMethods ()){
4386 SimpleName.Error120 (loc, mg.Methods [0].Name);
4390 return member_lookup;
4394 // Instance.MethodGroup
4396 if (!mg.RemoveStaticMethods ()){
4397 if (IdenticalNameAndTypeName (ec, left_original, loc)){
4398 if (!mg.RemoveInstanceMethods ()){
4399 SimpleName.Error120 (loc, mg.Methods [0].Name);
4402 return member_lookup;
4405 error176 (loc, mg.Methods [0].Name);
4409 mg.InstanceExpression = left;
4411 return member_lookup;
4414 if (member_lookup is FieldExpr){
4415 FieldExpr fe = (FieldExpr) member_lookup;
4416 FieldInfo fi = fe.FieldInfo;
4418 if (fi is FieldBuilder) {
4419 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
4422 object o = c.LookupConstantValue (ec);
4423 object real_value = ((Constant) c.Expr).GetValue ();
4424 return Constantify (real_value, fi.FieldType);
4429 Type t = fi.FieldType;
4430 Type decl_type = fi.DeclaringType;
4433 if (fi is FieldBuilder)
4434 o = TypeManager.GetValue ((FieldBuilder) fi);
4436 o = fi.GetValue (fi);
4438 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
4439 Expression enum_member = MemberLookup (
4440 ec, decl_type, "value__", false, loc);
4442 Enum en = TypeManager.LookupEnum (decl_type);
4446 c = Constantify (o, en.UnderlyingType);
4448 c = Constantify (o, enum_member.Type);
4450 return new EnumConstant (c, decl_type);
4453 Expression exp = Constantify (o, t);
4455 if (!(left is TypeExpr)) {
4456 error176 (loc, fe.FieldInfo.Name);
4463 if (left is TypeExpr){
4464 // and refers to a type name or an
4465 if (!fe.FieldInfo.IsStatic){
4466 error176 (loc, fe.FieldInfo.Name);
4469 return member_lookup;
4471 if (fe.FieldInfo.IsStatic){
4472 if (IdenticalNameAndTypeName (ec, left_original, loc))
4473 return member_lookup;
4475 error176 (loc, fe.FieldInfo.Name);
4478 fe.InstanceExpression = left;
4484 if (member_lookup is PropertyExpr){
4485 PropertyExpr pe = (PropertyExpr) member_lookup;
4487 if (left is TypeExpr){
4489 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4495 if (IdenticalNameAndTypeName (ec, left_original, loc))
4496 return member_lookup;
4497 error176 (loc, pe.PropertyInfo.Name);
4500 pe.InstanceExpression = left;
4506 if (member_lookup is EventExpr) {
4508 EventExpr ee = (EventExpr) member_lookup;
4511 // If the event is local to this class, we transform ourselves into
4515 Expression ml = MemberLookup (ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
4516 true, MemberTypes.Event, AllBindingFlags, loc);
4519 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
4523 // If this happens, then we have an event with its own
4524 // accessors and private field etc so there's no need
4525 // to transform ourselves : we should instead flag an error
4527 Assign.error70 (ee.EventInfo, loc);
4531 ml = ExprClassFromMemberInfo (ec, mi, loc);
4534 Report.Error (-200, loc, "Internal error!!");
4537 return ResolveMemberAccess (ec, ml, left, loc, left_original);
4540 if (left is TypeExpr) {
4542 SimpleName.Error120 (loc, ee.EventInfo.Name);
4550 if (IdenticalNameAndTypeName (ec, left_original, loc))
4553 error176 (loc, ee.EventInfo.Name);
4557 ee.InstanceExpression = left;
4563 if (member_lookup is TypeExpr){
4564 member_lookup.Resolve (ec);
4565 return member_lookup;
4568 Console.WriteLine ("Left is: " + left);
4569 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
4570 Environment.Exit (0);
4574 public override Expression DoResolve (EmitContext ec)
4577 // We are the sole users of ResolveWithSimpleName (ie, the only
4578 // ones that can cope with it
4580 Expression original = expr;
4581 expr = expr.ResolveWithSimpleName (ec);
4586 if (expr is SimpleName){
4587 SimpleName child_expr = (SimpleName) expr;
4589 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4591 return expr.ResolveWithSimpleName (ec);
4595 // Handle enums here when they are in transit.
4596 // Note that we cannot afford to hit MemberLookup in this case because
4597 // it will fail to find any members at all
4600 Type expr_type = expr.Type;
4601 if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))){
4603 Enum en = TypeManager.LookupEnum (expr_type);
4606 object value = en.LookupEnumValue (ec, Identifier, loc);
4609 Constant c = Constantify (value, en.UnderlyingType);
4610 return new EnumConstant (c, expr_type);
4615 member_lookup = MemberLookup (ec, expr_type, Identifier, false, loc);
4617 if (member_lookup == null)
4620 return ResolveMemberAccess (ec, member_lookup, expr, loc, original);
4627 // This code is more conformant to the spec (it follows it step by step),
4628 // but it has not been tested yet, and there is nothing here that is not
4629 // caught by the above code. But it might be a better foundation to improve
4632 public ResolveTypeMemberAccess (EmitContext ec, Expression member_lookup,
4633 Expression left, Location loc)
4635 if (member_lookup is TypeExpr){
4636 member_lookup.Resolve (ec);
4637 return member_lookup;
4640 if (member_lookup is MethodGroupExpr){
4641 if (!mg.RemoveStaticMethods ()){
4642 SimpleName.Error120 (loc, mg.Methods [0].Name);
4646 return member_lookup;
4649 if (member_lookup is PropertyExpr){
4650 PropertyExpr pe = (PropertyExpr) member_lookup;
4653 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4659 if (member_lookup is FieldExpr){
4660 FieldExpr fe = (FieldExpr) member_lookup;
4661 FieldInfo fi = fe.FieldInfo;
4663 if (fi is FieldBuilder) {
4664 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
4667 object o = c.LookupConstantValue (ec);
4668 return Constantify (o, fi.FieldType);
4673 Type t = fi.FieldType;
4674 Type decl_type = fi.DeclaringType;
4677 if (fi is FieldBuilder)
4678 o = TypeManager.GetValue ((FieldBuilder) fi);
4680 o = fi.GetValue (fi);
4682 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
4683 Expression enum_member = MemberLookup (
4684 ec, decl_type, "value__",
4687 Enum en = TypeManager.LookupEnum (decl_type);
4691 c = Constantify (o, en.UnderlyingType);
4693 c = Constantify (o, enum_member.Type);
4695 return new EnumConstant (c, decl_type);
4698 Expression exp = Constantify (o, t);
4703 if (!fe.FieldInfo.IsStatic){
4704 error176 (loc, fe.FieldInfo.Name);
4707 return member_lookup;
4710 if (member_lookup is EventExpr){
4712 EventExpr ee = (EventExpr) member_lookup;
4715 // If the event is local to this class, we transform ourselves into
4719 Expression ml = MemberLookup (
4720 ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
4721 true, MemberTypes.Event, AllBindingFlags, loc);
4724 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
4726 ml = ExprClassFromMemberInfo (ec, mi, loc);
4729 Report.Error (-200, loc, "Internal error!!");
4733 return ResolveMemberAccess (ec, ml, left, loc);
4737 SimpleName.Error120 (loc, ee.EventInfo.Name);
4744 Console.WriteLine ("Left is: " + left);
4745 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
4746 Environment.Exit (0);
4751 public ResolveInstanceMemberAccess (EmitContext ec, Expression member_lookup,
4752 Expression left, Location loc)
4754 if (member_lookup is MethodGroupExpr){
4756 // Instance.MethodGroup
4758 if (!mg.RemoveStaticMethods ()){
4759 error176 (loc, mg.Methods [0].Name);
4763 mg.InstanceExpression = left;
4765 return member_lookup;
4768 if (member_lookup is PropertyExpr){
4769 PropertyExpr pe = (PropertyExpr) member_lookup;
4772 error176 (loc, pe.PropertyInfo.Name);
4775 pe.InstanceExpression = left;
4780 Type left_type = left.type;
4782 if (left_type.IsValueType){
4788 public override Expression DoResolve (EmitContext ec)
4791 // We are the sole users of ResolveWithSimpleName (ie, the only
4792 // ones that can cope with it
4794 expr = expr.ResolveWithSimpleName (ec);
4799 if (expr is SimpleName){
4800 SimpleName child_expr = (SimpleName) expr;
4802 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4804 return expr.ResolveWithSimpleName (ec);
4808 // Handle enums here when they are in transit.
4809 // Note that we cannot afford to hit MemberLookup in this case because
4810 // it will fail to find any members at all (Why?)
4813 Type expr_type = expr.Type;
4814 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
4816 Enum en = TypeManager.LookupEnum (expr_type);
4819 object value = en.LookupEnumValue (ec, Identifier, loc);
4824 Constant c = Constantify (value, en.UnderlyingType);
4825 return new EnumConstant (c, expr_type);
4829 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4831 if (member_lookup == null)
4834 if (expr is TypeExpr)
4835 return ResolveTypeMemberAccess (ec, member_lookup, expr, loc);
4837 return ResolveInstanceMemberAccess (ec, member_lookup, expr, loc);
4840 public override void Emit (EmitContext ec)
4842 throw new Exception ("Should not happen");
4847 /// Implements checked expressions
4849 public class CheckedExpr : Expression {
4851 public Expression Expr;
4853 public CheckedExpr (Expression e)
4858 public override Expression DoResolve (EmitContext ec)
4860 Expr = Expr.Resolve (ec);
4865 eclass = Expr.eclass;
4870 public override void Emit (EmitContext ec)
4872 bool last_check = ec.CheckState;
4874 ec.CheckState = true;
4876 ec.CheckState = last_check;
4882 /// Implements the unchecked expression
4884 public class UnCheckedExpr : Expression {
4886 public Expression Expr;
4888 public UnCheckedExpr (Expression e)
4893 public override Expression DoResolve (EmitContext ec)
4895 Expr = Expr.Resolve (ec);
4900 eclass = Expr.eclass;
4905 public override void Emit (EmitContext ec)
4907 bool last_check = ec.CheckState;
4909 ec.CheckState = false;
4911 ec.CheckState = last_check;
4917 /// An Element Access expression.
4919 /// During semantic analysis these are transformed into
4920 /// IndexerAccess or ArrayAccess
4922 public class ElementAccess : Expression {
4923 public ArrayList Arguments;
4924 public Expression Expr;
4925 public Location loc;
4927 public ElementAccess (Expression e, ArrayList e_list, Location l)
4936 Arguments = new ArrayList ();
4937 foreach (Expression tmp in e_list)
4938 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
4942 bool CommonResolve (EmitContext ec)
4944 Expr = Expr.Resolve (ec);
4949 if (Arguments == null)
4952 for (int i = Arguments.Count; i > 0;){
4954 Argument a = (Argument) Arguments [i];
4956 if (!a.Resolve (ec, loc))
4963 public override Expression DoResolve (EmitContext ec)
4965 if (!CommonResolve (ec))
4969 // We perform some simple tests, and then to "split" the emit and store
4970 // code we create an instance of a different class, and return that.
4972 // I am experimenting with this pattern.
4974 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
4975 return (new ArrayAccess (this)).Resolve (ec);
4977 return (new IndexerAccess (this)).Resolve (ec);
4980 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
4982 if (!CommonResolve (ec))
4985 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
4986 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
4988 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
4991 public override void Emit (EmitContext ec)
4993 throw new Exception ("Should never be reached");
4998 /// Implements array access
5000 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
5002 // Points to our "data" repository
5006 public ArrayAccess (ElementAccess ea_data)
5009 eclass = ExprClass.Variable;
5012 public override Expression DoResolve (EmitContext ec)
5014 if (ea.Expr.eclass != ExprClass.Variable) {
5015 report118 (ea.loc, ea.Expr, "variable");
5019 Type t = ea.Expr.Type;
5021 if (t.GetArrayRank () != ea.Arguments.Count){
5022 Report.Error (22, ea.loc,
5023 "Incorrect number of indexes for array " +
5024 " expected: " + t.GetArrayRank () + " got: " +
5025 ea.Arguments.Count);
5028 type = t.GetElementType ();
5029 eclass = ExprClass.Variable;
5035 /// Emits the right opcode to load an object of Type `t'
5036 /// from an array of T
5038 static public void EmitLoadOpcode (ILGenerator ig, Type type)
5040 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
5041 ig.Emit (OpCodes.Ldelem_I1);
5042 else if (type == TypeManager.sbyte_type)
5043 ig.Emit (OpCodes.Ldelem_U1);
5044 else if (type == TypeManager.short_type)
5045 ig.Emit (OpCodes.Ldelem_I2);
5046 else if (type == TypeManager.ushort_type)
5047 ig.Emit (OpCodes.Ldelem_U2);
5048 else if (type == TypeManager.int32_type)
5049 ig.Emit (OpCodes.Ldelem_I4);
5050 else if (type == TypeManager.uint32_type)
5051 ig.Emit (OpCodes.Ldelem_U4);
5052 else if (type == TypeManager.uint64_type)
5053 ig.Emit (OpCodes.Ldelem_I8);
5054 else if (type == TypeManager.int64_type)
5055 ig.Emit (OpCodes.Ldelem_I8);
5056 else if (type == TypeManager.float_type)
5057 ig.Emit (OpCodes.Ldelem_R4);
5058 else if (type == TypeManager.double_type)
5059 ig.Emit (OpCodes.Ldelem_R8);
5060 else if (type == TypeManager.intptr_type)
5061 ig.Emit (OpCodes.Ldelem_I);
5062 else if (type.IsValueType){
5063 ig.Emit (OpCodes.Ldelema, type);
5064 ig.Emit (OpCodes.Ldobj, type);
5066 ig.Emit (OpCodes.Ldelem_Ref);
5070 /// Emits the right opcode to store an object of Type `t'
5071 /// from an array of T.
5073 static public void EmitStoreOpcode (ILGenerator ig, Type t)
5075 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
5076 t == TypeManager.bool_type)
5077 ig.Emit (OpCodes.Stelem_I1);
5078 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
5079 ig.Emit (OpCodes.Stelem_I2);
5080 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
5081 ig.Emit (OpCodes.Stelem_I4);
5082 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
5083 ig.Emit (OpCodes.Stelem_I8);
5084 else if (t == TypeManager.float_type)
5085 ig.Emit (OpCodes.Stelem_R4);
5086 else if (t == TypeManager.double_type)
5087 ig.Emit (OpCodes.Stelem_R8);
5088 else if (t == TypeManager.intptr_type)
5089 ig.Emit (OpCodes.Stelem_I);
5090 else if (t.IsValueType)
5091 ig.Emit (OpCodes.Stobj, t);
5093 ig.Emit (OpCodes.Stelem_Ref);
5096 MethodInfo FetchGetMethod ()
5098 ModuleBuilder mb = RootContext.ModuleBuilder;
5099 Type [] args = new Type [ea.Arguments.Count];
5104 foreach (Argument a in ea.Arguments)
5105 args [i++] = a.Type;
5107 get = mb.GetArrayMethod (
5108 ea.Expr.Type, "Get",
5109 CallingConventions.HasThis |
5110 CallingConventions.Standard,
5116 MethodInfo FetchAddressMethod ()
5118 ModuleBuilder mb = RootContext.ModuleBuilder;
5119 Type [] args = new Type [ea.Arguments.Count];
5121 string ptr_type_name;
5125 ptr_type_name = type.FullName + "&";
5126 ret_type = Type.GetType (ptr_type_name);
5129 // It is a type defined by the source code we are compiling
5131 if (ret_type == null){
5132 ret_type = mb.GetType (ptr_type_name);
5135 foreach (Argument a in ea.Arguments)
5136 args [i++] = a.Type;
5138 address = mb.GetArrayMethod (
5139 ea.Expr.Type, "Address",
5140 CallingConventions.HasThis |
5141 CallingConventions.Standard,
5147 public override void Emit (EmitContext ec)
5149 int rank = ea.Expr.Type.GetArrayRank ();
5150 ILGenerator ig = ec.ig;
5154 foreach (Argument a in ea.Arguments)
5158 EmitLoadOpcode (ig, type);
5162 method = FetchGetMethod ();
5163 ig.Emit (OpCodes.Call, method);
5167 public void EmitAssign (EmitContext ec, Expression source)
5169 int rank = ea.Expr.Type.GetArrayRank ();
5170 ILGenerator ig = ec.ig;
5174 foreach (Argument a in ea.Arguments)
5177 Type t = source.Type;
5180 // The stobj opcode used by value types will need
5181 // an address on the stack, not really an array/array
5185 if (t.IsValueType && !TypeManager.IsBuiltinType (t))
5186 ig.Emit (OpCodes.Ldelema, t);
5192 EmitStoreOpcode (ig, t);
5194 ModuleBuilder mb = RootContext.ModuleBuilder;
5195 Type [] args = new Type [ea.Arguments.Count + 1];
5200 foreach (Argument a in ea.Arguments)
5201 args [i++] = a.Type;
5205 set = mb.GetArrayMethod (
5206 ea.Expr.Type, "Set",
5207 CallingConventions.HasThis |
5208 CallingConventions.Standard,
5209 TypeManager.void_type, args);
5211 ig.Emit (OpCodes.Call, set);
5215 public void AddressOf (EmitContext ec)
5217 int rank = ea.Expr.Type.GetArrayRank ();
5218 ILGenerator ig = ec.ig;
5222 foreach (Argument a in ea.Arguments)
5226 ig.Emit (OpCodes.Ldelema, type);
5228 MethodInfo address = FetchAddressMethod ();
5229 ig.Emit (OpCodes.Call, address);
5236 public ArrayList getters, setters;
5237 static Hashtable map;
5241 map = new Hashtable ();
5244 Indexers (MemberInfo [] mi)
5246 foreach (PropertyInfo property in mi){
5247 MethodInfo get, set;
5249 get = property.GetGetMethod (true);
5251 if (getters == null)
5252 getters = new ArrayList ();
5257 set = property.GetSetMethod (true);
5259 if (setters == null)
5260 setters = new ArrayList ();
5266 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
5268 Indexers ix = (Indexers) map [t];
5269 string p_name = TypeManager.IndexerPropertyName (t);
5274 MemberInfo [] mi = tm.FindMembers (
5275 t, MemberTypes.Property,
5276 BindingFlags.Public | BindingFlags.Instance,
5277 Type.FilterName, p_name);
5279 if (mi == null || mi.Length == 0){
5280 Report.Error (21, loc,
5281 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5282 "any indexers defined");
5286 ix = new Indexers (mi);
5294 /// Expressions that represent an indexer call.
5296 public class IndexerAccess : Expression, IAssignMethod {
5298 // Points to our "data" repository
5301 MethodInfo get, set;
5303 ArrayList set_arguments;
5305 public IndexerAccess (ElementAccess ea_data)
5308 eclass = ExprClass.Value;
5311 public override Expression DoResolve (EmitContext ec)
5313 Type indexer_type = ea.Expr.Type;
5316 // Step 1: Query for all `Item' *properties*. Notice
5317 // that the actual methods are pointed from here.
5319 // This is a group of properties, piles of them.
5322 ilist = Indexers.GetIndexersForType (
5323 indexer_type, RootContext.TypeManager, ea.loc);
5327 // Step 2: find the proper match
5329 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5330 get = (MethodInfo) Invocation.OverloadResolve (
5331 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5334 Report.Error (154, ea.loc,
5335 "indexer can not be used in this context, because " +
5336 "it lacks a `get' accessor");
5340 type = get.ReturnType;
5341 eclass = ExprClass.IndexerAccess;
5345 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5347 Type indexer_type = ea.Expr.Type;
5348 Type right_type = right_side.Type;
5351 ilist = Indexers.GetIndexersForType (
5352 indexer_type, RootContext.TypeManager, ea.loc);
5354 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5355 set_arguments = (ArrayList) ea.Arguments.Clone ();
5356 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5358 set = (MethodInfo) Invocation.OverloadResolve (
5359 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5363 Report.Error (200, ea.loc,
5364 "indexer X.this [" + TypeManager.CSharpName (right_type) +
5365 "] lacks a `set' accessor");
5369 type = TypeManager.void_type;
5370 eclass = ExprClass.IndexerAccess;
5374 public override void Emit (EmitContext ec)
5376 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5380 // source is ignored, because we already have a copy of it from the
5381 // LValue resolution and we have already constructed a pre-cached
5382 // version of the arguments (ea.set_arguments);
5384 public void EmitAssign (EmitContext ec, Expression source)
5386 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
5391 /// The base operator for method names
5393 public class BaseAccess : Expression {
5397 public BaseAccess (string member, Location l)
5399 this.member = member;
5403 public override Expression DoResolve (EmitContext ec)
5405 Expression member_lookup;
5406 Type current_type = ec.TypeContainer.TypeBuilder;
5407 Type base_type = current_type.BaseType;
5409 member_lookup = MemberLookup (ec, base_type, member, false, loc);
5410 if (member_lookup == null)
5416 left = new TypeExpr (base_type);
5418 left = new This (loc).Resolve (ec);
5420 return MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
5423 public override void Emit (EmitContext ec)
5425 throw new Exception ("Should never be called");
5430 /// The base indexer operator
5432 public class BaseIndexerAccess : Expression {
5433 ArrayList Arguments;
5435 public BaseIndexerAccess (ArrayList args)
5440 public override Expression DoResolve (EmitContext ec)
5442 // FIXME: Implement;
5443 throw new Exception ("Unimplemented");
5447 public override void Emit (EmitContext ec)
5449 throw new Exception ("Unimplemented");
5454 /// This class exists solely to pass the Type around and to be a dummy
5455 /// that can be passed to the conversion functions (this is used by
5456 /// foreach implementation to typecast the object return value from
5457 /// get_Current into the proper type. All code has been generated and
5458 /// we only care about the side effect conversions to be performed
5460 public class EmptyExpression : Expression {
5461 public EmptyExpression ()
5463 type = TypeManager.object_type;
5464 eclass = ExprClass.Value;
5467 public EmptyExpression (Type t)
5470 eclass = ExprClass.Value;
5473 public override Expression DoResolve (EmitContext ec)
5478 public override void Emit (EmitContext ec)
5480 // nothing, as we only exist to not do anything.
5484 // This is just because we might want to reuse this bad boy
5485 // instead of creating gazillions of EmptyExpressions.
5486 // (CanConvertImplicit uses it)
5488 public void SetType (Type t)
5494 public class UserCast : Expression {
5498 public UserCast (MethodInfo method, Expression source)
5500 this.method = method;
5501 this.source = source;
5502 type = method.ReturnType;
5503 eclass = ExprClass.Value;
5506 public override Expression DoResolve (EmitContext ec)
5509 // We are born fully resolved
5514 public override void Emit (EmitContext ec)
5516 ILGenerator ig = ec.ig;
5520 if (method is MethodInfo)
5521 ig.Emit (OpCodes.Call, (MethodInfo) method);
5523 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5530 // This class is used to "construct" the type during a typecast
5531 // operation. Since the Type.GetType class in .NET can parse
5532 // the type specification, we just use this to construct the type
5533 // one bit at a time.
5535 public class ComposedCast : Expression {
5540 public ComposedCast (Expression left, string dim, Location l)
5547 public override Expression DoResolve (EmitContext ec)
5549 left = left.Resolve (ec);
5553 if (left.eclass != ExprClass.Type){
5554 report118 (loc, left, "type");
5558 type = RootContext.LookupType (
5559 ec.TypeContainer, left.Type.FullName + dim, false, loc);
5563 eclass = ExprClass.Type;
5567 public override void Emit (EmitContext ec)
5569 throw new Exception ("This should never be called");