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 (method.DeclaringType.IsValueType)
3334 // If this is ourselves, push "this"
3336 if (instance_expr == null){
3337 ig.Emit (OpCodes.Ldarg_0);
3340 // Push the instance expression
3342 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
3344 // Special case: calls to a function declared in a
3345 // reference-type with a value-type argument need
3346 // to have their value boxed.
3349 if (method.DeclaringType.IsValueType){
3351 // If the expression implements IMemoryLocation, then
3352 // we can optimize and use AddressOf on the
3355 // If not we have to use some temporary storage for
3357 if (instance_expr is IMemoryLocation){
3358 ((IMemoryLocation)instance_expr).
3362 Type t = instance_expr.Type;
3364 instance_expr.Emit (ec);
3365 LocalBuilder temp = ig.DeclareLocal (t);
3366 ig.Emit (OpCodes.Stloc, temp);
3367 ig.Emit (OpCodes.Ldloca, temp);
3370 instance_expr.Emit (ec);
3371 ig.Emit (OpCodes.Box, instance_expr.Type);
3374 instance_expr.Emit (ec);
3378 if (Arguments != null)
3379 EmitArguments (ec, method, Arguments);
3381 if (method is MethodInfo){
3382 MethodInfo mi = (MethodInfo) method;
3388 if (is_static || struct_call){
3389 if (method is MethodInfo)
3390 ig.Emit (OpCodes.Call, (MethodInfo) method);
3392 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3394 if (method is MethodInfo)
3395 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3397 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3401 public override void Emit (EmitContext ec)
3403 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3405 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
3408 public override void EmitStatement (EmitContext ec)
3413 // Pop the return value if there is one
3415 if (method is MethodInfo){
3416 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3417 ec.ig.Emit (OpCodes.Pop);
3423 /// Implements the new expression
3425 public class New : ExpressionStatement {
3426 public readonly ArrayList Arguments;
3427 public readonly string RequestedType;
3430 MethodBase method = null;
3433 // If set, the new expression is for a value_target, and
3434 // we will not leave anything on the stack.
3436 Expression value_target;
3438 public New (string requested_type, ArrayList arguments, Location l)
3440 RequestedType = requested_type;
3441 Arguments = arguments;
3445 public Expression ValueTypeVariable {
3447 return value_target;
3451 value_target = value;
3455 public override Expression DoResolve (EmitContext ec)
3457 type = RootContext.LookupType (ec.TypeContainer, RequestedType, false, loc);
3462 bool IsDelegate = TypeManager.IsDelegateType (type);
3465 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
3467 bool is_struct = false;
3468 is_struct = type.IsSubclassOf (TypeManager.value_type);
3469 eclass = ExprClass.Value;
3472 // SRE returns a match for .ctor () on structs (the object constructor),
3473 // so we have to manually ignore it.
3475 if (is_struct && Arguments == null)
3479 ml = MemberLookup (ec, type, ".ctor", false,
3480 MemberTypes.Constructor, AllBindingFlags, loc);
3482 if (! (ml is MethodGroupExpr)){
3484 report118 (loc, ml, "method group");
3490 if (Arguments != null){
3491 for (int i = Arguments.Count; i > 0;){
3493 Argument a = (Argument) Arguments [i];
3495 if (!a.Resolve (ec, loc))
3500 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
3505 if (method == null && !is_struct) {
3507 "New invocation: Can not find a constructor for " +
3508 "this argument list");
3515 // This DoEmit can be invoked in two contexts:
3516 // * As a mechanism that will leave a value on the stack (new object)
3517 // * As one that wont (init struct)
3519 // You can control whether a value is required on the stack by passing
3520 // need_value_on_stack. The code *might* leave a value on the stack
3521 // so it must be popped manually
3523 // If we are dealing with a ValueType, we have a few
3524 // situations to deal with:
3526 // * The target is a ValueType, and we have been provided
3527 // the instance (this is easy, we are being assigned).
3529 // * The target of New is being passed as an argument,
3530 // to a boxing operation or a function that takes a
3533 // In this case, we need to create a temporary variable
3534 // that is the argument of New.
3536 // Returns whether a value is left on the stack
3538 bool DoEmit (EmitContext ec, bool need_value_on_stack)
3540 if (method == null){
3543 if (value_target == null)
3544 value_target = new LocalTemporary (ec, type);
3546 ml = (IMemoryLocation) value_target;
3549 Invocation.EmitArguments (ec, method, Arguments);
3550 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3555 // It must be a value type, sanity check
3557 if (value_target != null){
3558 ec.ig.Emit (OpCodes.Initobj, type);
3560 if (need_value_on_stack){
3561 value_target.Emit (ec);
3567 throw new Exception ("No method and no value type");
3570 public override void Emit (EmitContext ec)
3575 public override void EmitStatement (EmitContext ec)
3577 if (DoEmit (ec, false))
3578 ec.ig.Emit (OpCodes.Pop);
3583 /// Represents an array creation expression.
3587 /// There are two possible scenarios here: one is an array creation
3588 /// expression that specifies the dimensions and optionally the
3589 /// initialization data and the other which does not need dimensions
3590 /// specified but where initialization data is mandatory.
3592 public class ArrayCreation : ExpressionStatement {
3593 string RequestedType;
3595 ArrayList Initializers;
3597 ArrayList Arguments;
3599 MethodBase method = null;
3600 Type array_element_type;
3601 bool IsOneDimensional = false;
3602 bool IsBuiltinType = false;
3603 bool ExpectInitializers = false;
3606 Type underlying_type;
3608 ArrayList ArrayData;
3612 public ArrayCreation (string requested_type, ArrayList exprs,
3613 string rank, ArrayList initializers, Location l)
3615 RequestedType = requested_type;
3617 Initializers = initializers;
3620 Arguments = new ArrayList ();
3622 foreach (Expression e in exprs)
3623 Arguments.Add (new Argument (e, Argument.AType.Expression));
3627 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
3629 RequestedType = requested_type;
3630 Initializers = initializers;
3633 Rank = rank.Substring (0, rank.LastIndexOf ("["));
3635 string tmp = rank.Substring (rank.LastIndexOf ("["));
3637 dimensions = tmp.Length - 1;
3638 ExpectInitializers = true;
3641 public static string FormArrayType (string base_type, int idx_count, string rank)
3643 StringBuilder sb = new StringBuilder (base_type);
3648 for (int i = 1; i < idx_count; i++)
3653 return sb.ToString ();
3656 public static string FormElementType (string base_type, int idx_count, string rank)
3658 StringBuilder sb = new StringBuilder (base_type);
3661 for (int i = 1; i < idx_count; i++)
3668 string val = sb.ToString ();
3670 return val.Substring (0, val.LastIndexOf ("["));
3675 Report.Error (178, loc, "Incorrectly structured array initializer");
3678 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
3680 if (specified_dims) {
3681 Argument a = (Argument) Arguments [idx];
3683 if (!a.Resolve (ec, loc))
3686 if (!(a.Expr is Constant)) {
3687 Report.Error (150, loc, "A constant value is expected");
3691 int value = (int) ((Constant) a.Expr).GetValue ();
3693 if (value != probe.Count) {
3698 Bounds [idx] = value;
3701 foreach (object o in probe) {
3702 if (o is ArrayList) {
3703 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
3707 Expression tmp = (Expression) o;
3708 tmp = tmp.Resolve (ec);
3712 // Handle initialization from vars, fields etc.
3714 Expression conv = ConvertImplicitRequired (
3715 ec, tmp, underlying_type, loc);
3720 if (conv is StringConstant)
3721 ArrayData.Add (conv);
3722 else if (conv is Constant)
3723 ArrayData.Add (((Constant) conv).GetValue ());
3725 ArrayData.Add (conv);
3732 public void UpdateIndices (EmitContext ec)
3735 for (ArrayList probe = Initializers; probe != null;) {
3736 if (probe.Count > 0 && probe [0] is ArrayList) {
3737 Expression e = new IntConstant (probe.Count);
3738 Arguments.Add (new Argument (e, Argument.AType.Expression));
3740 Bounds [i++] = probe.Count;
3742 probe = (ArrayList) probe [0];
3745 Expression e = new IntConstant (probe.Count);
3746 Arguments.Add (new Argument (e, Argument.AType.Expression));
3748 Bounds [i++] = probe.Count;
3755 public bool ValidateInitializers (EmitContext ec)
3757 if (Initializers == null) {
3758 if (ExpectInitializers)
3764 underlying_type = RootContext.LookupType (
3765 ec.TypeContainer, RequestedType, false, loc);
3768 // We use this to store all the date values in the order in which we
3769 // will need to store them in the byte blob later
3771 ArrayData = new ArrayList ();
3772 Bounds = new Hashtable ();
3776 if (Arguments != null) {
3777 ret = CheckIndices (ec, Initializers, 0, true);
3781 Arguments = new ArrayList ();
3783 ret = CheckIndices (ec, Initializers, 0, false);
3790 if (Arguments.Count != dimensions) {
3799 public override Expression DoResolve (EmitContext ec)
3803 if (!ValidateInitializers (ec))
3806 if (Arguments == null)
3809 arg_count = Arguments.Count;
3810 for (int i = arg_count; i > 0;){
3812 Argument a = (Argument) Arguments [i];
3814 if (!a.Resolve (ec, loc))
3819 string array_type = FormArrayType (RequestedType, arg_count, Rank);
3820 string element_type = FormElementType (RequestedType, arg_count, Rank);
3822 type = RootContext.LookupType (ec.TypeContainer, array_type, false, loc);
3824 array_element_type = RootContext.LookupType (
3825 ec.TypeContainer, element_type, false, loc);
3830 if (arg_count == 1) {
3831 IsOneDimensional = true;
3832 eclass = ExprClass.Value;
3836 IsBuiltinType = TypeManager.IsBuiltinType (type);
3838 if (IsBuiltinType) {
3842 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
3843 AllBindingFlags, loc);
3845 if (!(ml is MethodGroupExpr)){
3846 report118 (loc, ml, "method group");
3851 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3852 "this argument list");
3856 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3858 if (method == null) {
3859 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3860 "this argument list");
3864 eclass = ExprClass.Value;
3868 ModuleBuilder mb = RootContext.ModuleBuilder;
3870 ArrayList args = new ArrayList ();
3871 if (Arguments != null){
3872 for (int i = arg_count; i > 0;){
3874 Argument a = (Argument) Arguments [i];
3880 Type [] arg_types = null;
3883 arg_types = new Type [args.Count];
3885 args.CopyTo (arg_types, 0);
3887 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3890 if (method == null) {
3891 Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3892 "this argument list");
3896 eclass = ExprClass.Value;
3902 public static byte [] MakeByteBlob (ArrayList ArrayData, Type underlying_type, Location loc)
3907 int count = ArrayData.Count;
3909 if (underlying_type == TypeManager.int32_type ||
3910 underlying_type == TypeManager.uint32_type ||
3911 underlying_type == TypeManager.float_type)
3913 else if (underlying_type == TypeManager.int64_type ||
3914 underlying_type == TypeManager.uint64_type ||
3915 underlying_type == TypeManager.double_type)
3917 else if (underlying_type == TypeManager.byte_type ||
3918 underlying_type == TypeManager.sbyte_type ||
3919 underlying_type == TypeManager.bool_type)
3921 else if (underlying_type == TypeManager.short_type ||
3922 underlying_type == TypeManager.char_type ||
3923 underlying_type == TypeManager.ushort_type)
3928 data = new byte [count * factor];
3931 for (int i = 0; i < count; ++i) {
3932 object v = ArrayData [i];
3934 if (v is EnumConstant)
3935 v = ((EnumConstant) v).Child;
3937 if (underlying_type == TypeManager.int64_type){
3939 if (!(v is Expression))
3942 for (int j = 0; j < factor; ++j) {
3943 data [idx + j] = (byte) (val & 0xFF);
3946 } else if (underlying_type == TypeManager.uint64_type){
3948 if (!(v is Expression))
3951 for (int j = 0; j < factor; ++j) {
3952 data [idx + j] = (byte) (val & 0xFF);
3955 } else if (underlying_type == TypeManager.float_type) {
3961 if (!(v is Expression))
3964 byte *ptr = (byte *) &val;
3966 for (int j = 0; j < factor; ++j)
3967 data [idx + j] = (byte) ptr [j];
3970 } else if (underlying_type == TypeManager.double_type) {
3976 if (!(v is Expression))
3979 byte *ptr = (byte *) &val;
3981 for (int j = 0; j < factor; ++j)
3982 data [idx + j] = (byte) ptr [j];
3985 } else if (underlying_type == TypeManager.char_type){
3988 if (!(v is Expression))
3989 val = (int) ((char) v);
3991 data [idx] = (byte) (val & 0xff);
3992 data [idx+1] = (byte) (val >> 8);
3993 } else if (underlying_type == TypeManager.short_type){
3996 if (!(v is Expression))
3997 val = (int) ((short) v);
3999 data [idx] = (byte) (val & 0xff);
4000 data [idx+1] = (byte) (val >> 8);
4002 } else if (underlying_type == TypeManager.ushort_type){
4005 if (!(v is Expression))
4006 val = (int) ((ushort) v);
4008 data [idx] = (byte) (val & 0xff);
4009 data [idx+1] = (byte) (val >> 8);
4011 } else if (underlying_type == TypeManager.int32_type) {
4014 if (!(v is Expression))
4017 data [idx] = (byte) (val & 0xff);
4018 data [idx+1] = (byte) ((val >> 8) & 0xff);
4019 data [idx+2] = (byte) ((val >> 16) & 0xff);
4020 data [idx+3] = (byte) (val >> 24);
4021 } else if (underlying_type == TypeManager.uint32_type) {
4024 if (!(v is Expression))
4027 data [idx] = (byte) (val & 0xff);
4028 data [idx+1] = (byte) ((val >> 8) & 0xff);
4029 data [idx+2] = (byte) ((val >> 16) & 0xff);
4030 data [idx+3] = (byte) (val >> 24);
4031 } else if (underlying_type == TypeManager.sbyte_type) {
4034 if (!(v is Expression))
4037 data [idx] = (byte) val;
4038 } else if (underlying_type == TypeManager.byte_type) {
4041 if (!(v is Expression))
4044 data [idx] = (byte) val;
4046 throw new Exception ("Unrecognized type in MakeByteBlob");
4055 // Emits the initializers for the array
4057 void EmitStaticInitializers (EmitContext ec, bool is_expression)
4060 // First, the static data
4063 ILGenerator ig = ec.ig;
4065 byte [] data = MakeByteBlob (ArrayData, underlying_type, loc);
4068 fb = RootContext.MakeStaticData (data);
4071 ig.Emit (OpCodes.Dup);
4072 ig.Emit (OpCodes.Ldtoken, fb);
4073 ig.Emit (OpCodes.Call,
4074 TypeManager.void_initializearray_array_fieldhandle);
4079 // Emits pieces of the array that can not be computed at compile
4080 // time (variables and string locations).
4082 // This always expect the top value on the stack to be the array
4084 void EmitDynamicInitializers (EmitContext ec, bool is_expression)
4086 ILGenerator ig = ec.ig;
4087 int dims = Bounds.Count;
4088 int [] current_pos = new int [dims];
4089 int top = ArrayData.Count;
4090 LocalBuilder temp = ig.DeclareLocal (type);
4092 ig.Emit (OpCodes.Stloc, temp);
4094 MethodInfo set = null;
4098 ModuleBuilder mb = null;
4099 mb = RootContext.ModuleBuilder;
4100 args = new Type [dims + 1];
4103 for (j = 0; j < dims; j++)
4104 args [j] = TypeManager.int32_type;
4106 args [j] = array_element_type;
4108 set = mb.GetArrayMethod (
4110 CallingConventions.HasThis | CallingConventions.Standard,
4111 TypeManager.void_type, args);
4114 for (int i = 0; i < top; i++){
4116 Expression e = null;
4118 if (ArrayData [i] is Expression)
4119 e = (Expression) ArrayData [i];
4123 // Basically we do this for string literals and
4124 // other non-literal expressions
4126 if (e is StringConstant || !(e is Constant)) {
4128 ig.Emit (OpCodes.Ldloc, temp);
4130 for (int idx = dims; idx > 0; ) {
4132 IntConstant.EmitInt (ig, current_pos [idx]);
4138 ArrayAccess.EmitStoreOpcode (ig, array_element_type);
4140 ig.Emit (OpCodes.Call, set);
4148 for (int j = 0; j < dims; j++){
4150 if (current_pos [j] < (int) Bounds [j])
4152 current_pos [j] = 0;
4157 ig.Emit (OpCodes.Ldloc, temp);
4160 void DoEmit (EmitContext ec, bool is_statement)
4162 ILGenerator ig = ec.ig;
4164 if (IsOneDimensional) {
4165 Invocation.EmitArguments (ec, null, Arguments);
4166 ig.Emit (OpCodes.Newarr, array_element_type);
4169 Invocation.EmitArguments (ec, null, Arguments);
4172 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4174 ig.Emit (OpCodes.Newobj, (MethodInfo) method);
4177 if (Initializers != null){
4179 // FIXME: Set this variable correctly.
4181 bool dynamic_initializers = true;
4183 if (underlying_type != TypeManager.string_type &&
4184 underlying_type != TypeManager.object_type)
4185 EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
4187 if (dynamic_initializers)
4188 EmitDynamicInitializers (ec, !is_statement);
4192 public override void Emit (EmitContext ec)
4197 public override void EmitStatement (EmitContext ec)
4205 /// Represents the `this' construct
4207 public class This : Expression, IAssignMethod, IMemoryLocation {
4210 public This (Location loc)
4215 public override Expression DoResolve (EmitContext ec)
4217 eclass = ExprClass.Variable;
4218 type = ec.TypeContainer.TypeBuilder;
4221 Report.Error (26, loc,
4222 "Keyword this not valid in static code");
4229 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4233 if (ec.TypeContainer is Class){
4234 Report.Error (1604, loc, "Cannot assign to `this'");
4241 public override void Emit (EmitContext ec)
4243 ec.ig.Emit (OpCodes.Ldarg_0);
4246 public void EmitAssign (EmitContext ec, Expression source)
4249 ec.ig.Emit (OpCodes.Starg, 0);
4252 public void AddressOf (EmitContext ec)
4254 ec.ig.Emit (OpCodes.Ldarg_0);
4257 // FIGURE OUT WHY LDARG_S does not work
4259 // consider: struct X { int val; int P { set { val = value; }}}
4261 // Yes, this looks very bad. Look at `NOTAS' for
4263 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4268 /// Implements the typeof operator
4270 public class TypeOf : Expression {
4271 public readonly string QueriedType;
4275 public TypeOf (string queried_type, Location l)
4277 QueriedType = queried_type;
4281 public override Expression DoResolve (EmitContext ec)
4283 typearg = RootContext.LookupType (
4284 ec.TypeContainer, QueriedType, false, loc);
4286 if (typearg == null)
4289 type = TypeManager.type_type;
4290 eclass = ExprClass.Type;
4294 public override void Emit (EmitContext ec)
4296 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4297 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4302 /// Implements the sizeof expression
4304 public class SizeOf : Expression {
4305 public readonly string QueriedType;
4307 public SizeOf (string queried_type)
4309 this.QueriedType = queried_type;
4312 public override Expression DoResolve (EmitContext ec)
4314 // FIXME: Implement;
4315 throw new Exception ("Unimplemented");
4319 public override void Emit (EmitContext ec)
4321 throw new Exception ("Implement me");
4326 /// Implements the member access expression
4328 public class MemberAccess : Expression {
4329 public readonly string Identifier;
4331 Expression member_lookup;
4334 public MemberAccess (Expression expr, string id, Location l)
4341 public Expression Expr {
4347 static void error176 (Location loc, string name)
4349 Report.Error (176, loc, "Static member `" +
4350 name + "' cannot be accessed " +
4351 "with an instance reference, qualify with a " +
4352 "type name instead");
4356 static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
4358 if (left_original == null)
4361 if (!(left_original is SimpleName))
4364 SimpleName sn = (SimpleName) left_original;
4366 Type t = RootContext.LookupType (ec.TypeContainer, sn.Name, true, loc);
4373 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
4374 Expression left, Location loc,
4375 Expression left_original)
4380 if (member_lookup is MethodGroupExpr){
4381 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4386 if (left is TypeExpr){
4387 if (!mg.RemoveInstanceMethods ()){
4388 SimpleName.Error120 (loc, mg.Methods [0].Name);
4392 return member_lookup;
4396 // Instance.MethodGroup
4398 if (!mg.RemoveStaticMethods ()){
4399 if (IdenticalNameAndTypeName (ec, left_original, loc)){
4400 if (!mg.RemoveInstanceMethods ()){
4401 SimpleName.Error120 (loc, mg.Methods [0].Name);
4404 return member_lookup;
4407 error176 (loc, mg.Methods [0].Name);
4411 mg.InstanceExpression = left;
4413 return member_lookup;
4416 if (member_lookup is FieldExpr){
4417 FieldExpr fe = (FieldExpr) member_lookup;
4418 FieldInfo fi = fe.FieldInfo;
4420 if (fi is FieldBuilder) {
4421 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
4424 object o = c.LookupConstantValue (ec);
4425 object real_value = ((Constant) c.Expr).GetValue ();
4426 return Constantify (real_value, fi.FieldType);
4431 Type t = fi.FieldType;
4432 Type decl_type = fi.DeclaringType;
4435 if (fi is FieldBuilder)
4436 o = TypeManager.GetValue ((FieldBuilder) fi);
4438 o = fi.GetValue (fi);
4440 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
4441 Expression enum_member = MemberLookup (
4442 ec, decl_type, "value__", false, loc);
4444 Enum en = TypeManager.LookupEnum (decl_type);
4448 c = Constantify (o, en.UnderlyingType);
4450 c = Constantify (o, enum_member.Type);
4452 return new EnumConstant (c, decl_type);
4455 Expression exp = Constantify (o, t);
4457 if (!(left is TypeExpr)) {
4458 error176 (loc, fe.FieldInfo.Name);
4465 if (left is TypeExpr){
4466 // and refers to a type name or an
4467 if (!fe.FieldInfo.IsStatic){
4468 error176 (loc, fe.FieldInfo.Name);
4471 return member_lookup;
4473 if (fe.FieldInfo.IsStatic){
4474 if (IdenticalNameAndTypeName (ec, left_original, loc))
4475 return member_lookup;
4477 error176 (loc, fe.FieldInfo.Name);
4480 fe.InstanceExpression = left;
4486 if (member_lookup is PropertyExpr){
4487 PropertyExpr pe = (PropertyExpr) member_lookup;
4489 if (left is TypeExpr){
4491 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4497 if (IdenticalNameAndTypeName (ec, left_original, loc))
4498 return member_lookup;
4499 error176 (loc, pe.PropertyInfo.Name);
4502 pe.InstanceExpression = left;
4508 if (member_lookup is EventExpr) {
4510 EventExpr ee = (EventExpr) member_lookup;
4513 // If the event is local to this class, we transform ourselves into
4517 Expression ml = MemberLookup (ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
4518 true, MemberTypes.Event, AllBindingFlags, loc);
4521 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
4525 // If this happens, then we have an event with its own
4526 // accessors and private field etc so there's no need
4527 // to transform ourselves : we should instead flag an error
4529 Assign.error70 (ee.EventInfo, loc);
4533 ml = ExprClassFromMemberInfo (ec, mi, loc);
4536 Report.Error (-200, loc, "Internal error!!");
4539 return ResolveMemberAccess (ec, ml, left, loc, left_original);
4542 if (left is TypeExpr) {
4544 SimpleName.Error120 (loc, ee.EventInfo.Name);
4552 if (IdenticalNameAndTypeName (ec, left_original, loc))
4555 error176 (loc, ee.EventInfo.Name);
4559 ee.InstanceExpression = left;
4565 if (member_lookup is TypeExpr){
4566 member_lookup.Resolve (ec);
4567 return member_lookup;
4570 Console.WriteLine ("Left is: " + left);
4571 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
4572 Environment.Exit (0);
4576 public override Expression DoResolve (EmitContext ec)
4579 // We are the sole users of ResolveWithSimpleName (ie, the only
4580 // ones that can cope with it
4582 Expression original = expr;
4583 expr = expr.ResolveWithSimpleName (ec);
4588 if (expr is SimpleName){
4589 SimpleName child_expr = (SimpleName) expr;
4591 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4593 return expr.ResolveWithSimpleName (ec);
4597 // Handle enums here when they are in transit.
4598 // Note that we cannot afford to hit MemberLookup in this case because
4599 // it will fail to find any members at all
4602 Type expr_type = expr.Type;
4603 if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))){
4605 Enum en = TypeManager.LookupEnum (expr_type);
4608 object value = en.LookupEnumValue (ec, Identifier, loc);
4611 Constant c = Constantify (value, en.UnderlyingType);
4612 return new EnumConstant (c, expr_type);
4617 member_lookup = MemberLookup (ec, expr_type, Identifier, false, loc);
4619 if (member_lookup == null)
4622 return ResolveMemberAccess (ec, member_lookup, expr, loc, original);
4629 // This code is more conformant to the spec (it follows it step by step),
4630 // but it has not been tested yet, and there is nothing here that is not
4631 // caught by the above code. But it might be a better foundation to improve
4634 public ResolveTypeMemberAccess (EmitContext ec, Expression member_lookup,
4635 Expression left, Location loc)
4637 if (member_lookup is TypeExpr){
4638 member_lookup.Resolve (ec);
4639 return member_lookup;
4642 if (member_lookup is MethodGroupExpr){
4643 if (!mg.RemoveStaticMethods ()){
4644 SimpleName.Error120 (loc, mg.Methods [0].Name);
4648 return member_lookup;
4651 if (member_lookup is PropertyExpr){
4652 PropertyExpr pe = (PropertyExpr) member_lookup;
4655 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4661 if (member_lookup is FieldExpr){
4662 FieldExpr fe = (FieldExpr) member_lookup;
4663 FieldInfo fi = fe.FieldInfo;
4665 if (fi is FieldBuilder) {
4666 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
4669 object o = c.LookupConstantValue (ec);
4670 return Constantify (o, fi.FieldType);
4675 Type t = fi.FieldType;
4676 Type decl_type = fi.DeclaringType;
4679 if (fi is FieldBuilder)
4680 o = TypeManager.GetValue ((FieldBuilder) fi);
4682 o = fi.GetValue (fi);
4684 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
4685 Expression enum_member = MemberLookup (
4686 ec, decl_type, "value__",
4689 Enum en = TypeManager.LookupEnum (decl_type);
4693 c = Constantify (o, en.UnderlyingType);
4695 c = Constantify (o, enum_member.Type);
4697 return new EnumConstant (c, decl_type);
4700 Expression exp = Constantify (o, t);
4705 if (!fe.FieldInfo.IsStatic){
4706 error176 (loc, fe.FieldInfo.Name);
4709 return member_lookup;
4712 if (member_lookup is EventExpr){
4714 EventExpr ee = (EventExpr) member_lookup;
4717 // If the event is local to this class, we transform ourselves into
4721 Expression ml = MemberLookup (
4722 ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
4723 true, MemberTypes.Event, AllBindingFlags, loc);
4726 MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
4728 ml = ExprClassFromMemberInfo (ec, mi, loc);
4731 Report.Error (-200, loc, "Internal error!!");
4735 return ResolveMemberAccess (ec, ml, left, loc);
4739 SimpleName.Error120 (loc, ee.EventInfo.Name);
4746 Console.WriteLine ("Left is: " + left);
4747 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
4748 Environment.Exit (0);
4753 public ResolveInstanceMemberAccess (EmitContext ec, Expression member_lookup,
4754 Expression left, Location loc)
4756 if (member_lookup is MethodGroupExpr){
4758 // Instance.MethodGroup
4760 if (!mg.RemoveStaticMethods ()){
4761 error176 (loc, mg.Methods [0].Name);
4765 mg.InstanceExpression = left;
4767 return member_lookup;
4770 if (member_lookup is PropertyExpr){
4771 PropertyExpr pe = (PropertyExpr) member_lookup;
4774 error176 (loc, pe.PropertyInfo.Name);
4777 pe.InstanceExpression = left;
4782 Type left_type = left.type;
4784 if (left_type.IsValueType){
4790 public override Expression DoResolve (EmitContext ec)
4793 // We are the sole users of ResolveWithSimpleName (ie, the only
4794 // ones that can cope with it
4796 expr = expr.ResolveWithSimpleName (ec);
4801 if (expr is SimpleName){
4802 SimpleName child_expr = (SimpleName) expr;
4804 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4806 return expr.ResolveWithSimpleName (ec);
4810 // Handle enums here when they are in transit.
4811 // Note that we cannot afford to hit MemberLookup in this case because
4812 // it will fail to find any members at all (Why?)
4815 Type expr_type = expr.Type;
4816 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
4818 Enum en = TypeManager.LookupEnum (expr_type);
4821 object value = en.LookupEnumValue (ec, Identifier, loc);
4826 Constant c = Constantify (value, en.UnderlyingType);
4827 return new EnumConstant (c, expr_type);
4831 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4833 if (member_lookup == null)
4836 if (expr is TypeExpr)
4837 return ResolveTypeMemberAccess (ec, member_lookup, expr, loc);
4839 return ResolveInstanceMemberAccess (ec, member_lookup, expr, loc);
4842 public override void Emit (EmitContext ec)
4844 throw new Exception ("Should not happen");
4849 /// Implements checked expressions
4851 public class CheckedExpr : Expression {
4853 public Expression Expr;
4855 public CheckedExpr (Expression e)
4860 public override Expression DoResolve (EmitContext ec)
4862 Expr = Expr.Resolve (ec);
4867 eclass = Expr.eclass;
4872 public override void Emit (EmitContext ec)
4874 bool last_check = ec.CheckState;
4876 ec.CheckState = true;
4878 ec.CheckState = last_check;
4884 /// Implements the unchecked expression
4886 public class UnCheckedExpr : Expression {
4888 public Expression Expr;
4890 public UnCheckedExpr (Expression e)
4895 public override Expression DoResolve (EmitContext ec)
4897 Expr = Expr.Resolve (ec);
4902 eclass = Expr.eclass;
4907 public override void Emit (EmitContext ec)
4909 bool last_check = ec.CheckState;
4911 ec.CheckState = false;
4913 ec.CheckState = last_check;
4919 /// An Element Access expression.
4921 /// During semantic analysis these are transformed into
4922 /// IndexerAccess or ArrayAccess
4924 public class ElementAccess : Expression {
4925 public ArrayList Arguments;
4926 public Expression Expr;
4927 public Location loc;
4929 public ElementAccess (Expression e, ArrayList e_list, Location l)
4938 Arguments = new ArrayList ();
4939 foreach (Expression tmp in e_list)
4940 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
4944 bool CommonResolve (EmitContext ec)
4946 Expr = Expr.Resolve (ec);
4951 if (Arguments == null)
4954 for (int i = Arguments.Count; i > 0;){
4956 Argument a = (Argument) Arguments [i];
4958 if (!a.Resolve (ec, loc))
4965 public override Expression DoResolve (EmitContext ec)
4967 if (!CommonResolve (ec))
4971 // We perform some simple tests, and then to "split" the emit and store
4972 // code we create an instance of a different class, and return that.
4974 // I am experimenting with this pattern.
4976 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
4977 return (new ArrayAccess (this)).Resolve (ec);
4979 return (new IndexerAccess (this)).Resolve (ec);
4982 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
4984 if (!CommonResolve (ec))
4987 if (Expr.Type.IsSubclassOf (TypeManager.array_type))
4988 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
4990 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
4993 public override void Emit (EmitContext ec)
4995 throw new Exception ("Should never be reached");
5000 /// Implements array access
5002 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
5004 // Points to our "data" repository
5008 public ArrayAccess (ElementAccess ea_data)
5011 eclass = ExprClass.Variable;
5014 public override Expression DoResolve (EmitContext ec)
5016 if (ea.Expr.eclass != ExprClass.Variable) {
5017 report118 (ea.loc, ea.Expr, "variable");
5021 Type t = ea.Expr.Type;
5023 if (t.GetArrayRank () != ea.Arguments.Count){
5024 Report.Error (22, ea.loc,
5025 "Incorrect number of indexes for array " +
5026 " expected: " + t.GetArrayRank () + " got: " +
5027 ea.Arguments.Count);
5030 type = t.GetElementType ();
5031 eclass = ExprClass.Variable;
5037 /// Emits the right opcode to load an object of Type `t'
5038 /// from an array of T
5040 static public void EmitLoadOpcode (ILGenerator ig, Type type)
5042 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
5043 ig.Emit (OpCodes.Ldelem_I1);
5044 else if (type == TypeManager.sbyte_type)
5045 ig.Emit (OpCodes.Ldelem_U1);
5046 else if (type == TypeManager.short_type)
5047 ig.Emit (OpCodes.Ldelem_I2);
5048 else if (type == TypeManager.ushort_type)
5049 ig.Emit (OpCodes.Ldelem_U2);
5050 else if (type == TypeManager.int32_type)
5051 ig.Emit (OpCodes.Ldelem_I4);
5052 else if (type == TypeManager.uint32_type)
5053 ig.Emit (OpCodes.Ldelem_U4);
5054 else if (type == TypeManager.uint64_type)
5055 ig.Emit (OpCodes.Ldelem_I8);
5056 else if (type == TypeManager.int64_type)
5057 ig.Emit (OpCodes.Ldelem_I8);
5058 else if (type == TypeManager.float_type)
5059 ig.Emit (OpCodes.Ldelem_R4);
5060 else if (type == TypeManager.double_type)
5061 ig.Emit (OpCodes.Ldelem_R8);
5062 else if (type == TypeManager.intptr_type)
5063 ig.Emit (OpCodes.Ldelem_I);
5064 else if (type.IsValueType){
5065 ig.Emit (OpCodes.Ldelema, type);
5066 ig.Emit (OpCodes.Ldobj, type);
5068 ig.Emit (OpCodes.Ldelem_Ref);
5072 /// Emits the right opcode to store an object of Type `t'
5073 /// from an array of T.
5075 static public void EmitStoreOpcode (ILGenerator ig, Type t)
5077 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
5078 t == TypeManager.bool_type)
5079 ig.Emit (OpCodes.Stelem_I1);
5080 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
5081 ig.Emit (OpCodes.Stelem_I2);
5082 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
5083 ig.Emit (OpCodes.Stelem_I4);
5084 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
5085 ig.Emit (OpCodes.Stelem_I8);
5086 else if (t == TypeManager.float_type)
5087 ig.Emit (OpCodes.Stelem_R4);
5088 else if (t == TypeManager.double_type)
5089 ig.Emit (OpCodes.Stelem_R8);
5090 else if (t == TypeManager.intptr_type)
5091 ig.Emit (OpCodes.Stelem_I);
5092 else if (t.IsValueType)
5093 ig.Emit (OpCodes.Stobj, t);
5095 ig.Emit (OpCodes.Stelem_Ref);
5098 MethodInfo FetchGetMethod ()
5100 ModuleBuilder mb = RootContext.ModuleBuilder;
5101 Type [] args = new Type [ea.Arguments.Count];
5106 foreach (Argument a in ea.Arguments)
5107 args [i++] = a.Type;
5109 get = mb.GetArrayMethod (
5110 ea.Expr.Type, "Get",
5111 CallingConventions.HasThis |
5112 CallingConventions.Standard,
5118 MethodInfo FetchAddressMethod ()
5120 ModuleBuilder mb = RootContext.ModuleBuilder;
5121 Type [] args = new Type [ea.Arguments.Count];
5123 string ptr_type_name;
5127 ptr_type_name = type.FullName + "&";
5128 ret_type = Type.GetType (ptr_type_name);
5131 // It is a type defined by the source code we are compiling
5133 if (ret_type == null){
5134 ret_type = mb.GetType (ptr_type_name);
5137 foreach (Argument a in ea.Arguments)
5138 args [i++] = a.Type;
5140 address = mb.GetArrayMethod (
5141 ea.Expr.Type, "Address",
5142 CallingConventions.HasThis |
5143 CallingConventions.Standard,
5149 public override void Emit (EmitContext ec)
5151 int rank = ea.Expr.Type.GetArrayRank ();
5152 ILGenerator ig = ec.ig;
5156 foreach (Argument a in ea.Arguments)
5160 EmitLoadOpcode (ig, type);
5164 method = FetchGetMethod ();
5165 ig.Emit (OpCodes.Call, method);
5169 public void EmitAssign (EmitContext ec, Expression source)
5171 int rank = ea.Expr.Type.GetArrayRank ();
5172 ILGenerator ig = ec.ig;
5176 foreach (Argument a in ea.Arguments)
5179 Type t = source.Type;
5182 // The stobj opcode used by value types will need
5183 // an address on the stack, not really an array/array
5187 if (t.IsValueType && !TypeManager.IsBuiltinType (t))
5188 ig.Emit (OpCodes.Ldelema, t);
5194 EmitStoreOpcode (ig, t);
5196 ModuleBuilder mb = RootContext.ModuleBuilder;
5197 Type [] args = new Type [ea.Arguments.Count + 1];
5202 foreach (Argument a in ea.Arguments)
5203 args [i++] = a.Type;
5207 set = mb.GetArrayMethod (
5208 ea.Expr.Type, "Set",
5209 CallingConventions.HasThis |
5210 CallingConventions.Standard,
5211 TypeManager.void_type, args);
5213 ig.Emit (OpCodes.Call, set);
5217 public void AddressOf (EmitContext ec)
5219 int rank = ea.Expr.Type.GetArrayRank ();
5220 ILGenerator ig = ec.ig;
5224 foreach (Argument a in ea.Arguments)
5228 ig.Emit (OpCodes.Ldelema, type);
5230 MethodInfo address = FetchAddressMethod ();
5231 ig.Emit (OpCodes.Call, address);
5238 public ArrayList getters, setters;
5239 static Hashtable map;
5243 map = new Hashtable ();
5246 Indexers (MemberInfo [] mi)
5248 foreach (PropertyInfo property in mi){
5249 MethodInfo get, set;
5251 get = property.GetGetMethod (true);
5253 if (getters == null)
5254 getters = new ArrayList ();
5259 set = property.GetSetMethod (true);
5261 if (setters == null)
5262 setters = new ArrayList ();
5268 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
5270 Indexers ix = (Indexers) map [t];
5271 string p_name = TypeManager.IndexerPropertyName (t);
5276 MemberInfo [] mi = tm.FindMembers (
5277 t, MemberTypes.Property,
5278 BindingFlags.Public | BindingFlags.Instance,
5279 Type.FilterName, p_name);
5281 if (mi == null || mi.Length == 0){
5282 Report.Error (21, loc,
5283 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5284 "any indexers defined");
5288 ix = new Indexers (mi);
5296 /// Expressions that represent an indexer call.
5298 public class IndexerAccess : Expression, IAssignMethod {
5300 // Points to our "data" repository
5303 MethodInfo get, set;
5305 ArrayList set_arguments;
5307 public IndexerAccess (ElementAccess ea_data)
5310 eclass = ExprClass.Value;
5313 public override Expression DoResolve (EmitContext ec)
5315 Type indexer_type = ea.Expr.Type;
5318 // Step 1: Query for all `Item' *properties*. Notice
5319 // that the actual methods are pointed from here.
5321 // This is a group of properties, piles of them.
5324 ilist = Indexers.GetIndexersForType (
5325 indexer_type, RootContext.TypeManager, ea.loc);
5329 // Step 2: find the proper match
5331 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5332 get = (MethodInfo) Invocation.OverloadResolve (
5333 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5336 Report.Error (154, ea.loc,
5337 "indexer can not be used in this context, because " +
5338 "it lacks a `get' accessor");
5342 type = get.ReturnType;
5343 eclass = ExprClass.IndexerAccess;
5347 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5349 Type indexer_type = ea.Expr.Type;
5350 Type right_type = right_side.Type;
5353 ilist = Indexers.GetIndexersForType (
5354 indexer_type, RootContext.TypeManager, ea.loc);
5356 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5357 set_arguments = (ArrayList) ea.Arguments.Clone ();
5358 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5360 set = (MethodInfo) Invocation.OverloadResolve (
5361 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5365 Report.Error (200, ea.loc,
5366 "indexer X.this [" + TypeManager.CSharpName (right_type) +
5367 "] lacks a `set' accessor");
5371 type = TypeManager.void_type;
5372 eclass = ExprClass.IndexerAccess;
5376 public override void Emit (EmitContext ec)
5378 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5382 // source is ignored, because we already have a copy of it from the
5383 // LValue resolution and we have already constructed a pre-cached
5384 // version of the arguments (ea.set_arguments);
5386 public void EmitAssign (EmitContext ec, Expression source)
5388 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
5393 /// The base operator for method names
5395 public class BaseAccess : Expression {
5399 public BaseAccess (string member, Location l)
5401 this.member = member;
5405 public override Expression DoResolve (EmitContext ec)
5407 Expression member_lookup;
5408 Type current_type = ec.TypeContainer.TypeBuilder;
5409 Type base_type = current_type.BaseType;
5411 member_lookup = MemberLookup (ec, base_type, member, false, loc);
5412 if (member_lookup == null)
5418 left = new TypeExpr (base_type);
5422 return MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
5425 public override void Emit (EmitContext ec)
5427 throw new Exception ("Should never be called");
5432 /// The base indexer operator
5434 public class BaseIndexerAccess : Expression {
5435 ArrayList Arguments;
5437 public BaseIndexerAccess (ArrayList args)
5442 public override Expression DoResolve (EmitContext ec)
5444 // FIXME: Implement;
5445 throw new Exception ("Unimplemented");
5449 public override void Emit (EmitContext ec)
5451 throw new Exception ("Unimplemented");
5456 /// This class exists solely to pass the Type around and to be a dummy
5457 /// that can be passed to the conversion functions (this is used by
5458 /// foreach implementation to typecast the object return value from
5459 /// get_Current into the proper type. All code has been generated and
5460 /// we only care about the side effect conversions to be performed
5462 public class EmptyExpression : Expression {
5463 public EmptyExpression ()
5465 type = TypeManager.object_type;
5466 eclass = ExprClass.Value;
5469 public EmptyExpression (Type t)
5472 eclass = ExprClass.Value;
5475 public override Expression DoResolve (EmitContext ec)
5480 public override void Emit (EmitContext ec)
5482 // nothing, as we only exist to not do anything.
5486 // This is just because we might want to reuse this bad boy
5487 // instead of creating gazillions of EmptyExpressions.
5488 // (CanConvertImplicit uses it)
5490 public void SetType (Type t)
5496 public class UserCast : Expression {
5500 public UserCast (MethodInfo method, Expression source)
5502 this.method = method;
5503 this.source = source;
5504 type = method.ReturnType;
5505 eclass = ExprClass.Value;
5508 public override Expression DoResolve (EmitContext ec)
5511 // We are born fully resolved
5516 public override void Emit (EmitContext ec)
5518 ILGenerator ig = ec.ig;
5522 if (method is MethodInfo)
5523 ig.Emit (OpCodes.Call, (MethodInfo) method);
5525 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5532 // This class is used to "construct" the type during a typecast
5533 // operation. Since the Type.GetType class in .NET can parse
5534 // the type specification, we just use this to construct the type
5535 // one bit at a time.
5537 public class ComposedCast : Expression {
5542 public ComposedCast (Expression left, string dim, Location l)
5549 public override Expression DoResolve (EmitContext ec)
5551 left = left.Resolve (ec);
5555 if (left.eclass != ExprClass.Type){
5556 report118 (loc, left, "type");
5560 type = RootContext.LookupType (
5561 ec.TypeContainer, left.Type.FullName + dim, false, loc);
5565 eclass = ExprClass.Type;
5569 public override void Emit (EmitContext ec)
5571 throw new Exception ("This should never be called");