2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
12 namespace Mono.MonoBASIC {
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// This is just a helper class, it is generated by Unary, UnaryMutator
21 /// when an overloaded method has been found. It just emits the code for a
24 public class StaticCallExpr : ExpressionStatement {
28 StaticCallExpr (MethodInfo m, ArrayList a, Location l)
34 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, loc);
71 public override void EmitStatement (EmitContext ec)
74 if (TypeManager.TypeToCoreType (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
95 public Expression Expr;
97 public Unary (Operator op, Expression expr, Location loc)
105 /// Returns a stringified representation of the Operator
107 static public string OperName (Operator oper)
110 case Operator.UnaryPlus:
112 case Operator.UnaryNegation:
114 case Operator.LogicalNot:
116 case Operator.OnesComplement:
118 case Operator.AddressOf:
120 case Operator.Indirection:
124 return oper.ToString ();
127 static string [] oper_names;
131 oper_names = new string [(int)Operator.TOP];
133 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
134 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
135 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
136 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
137 oper_names [(int) Operator.Indirection] = "op_Indirection";
138 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
141 void Error23 (Type t)
144 23, "Operator " + OperName (Oper) +
145 " cannot be applied to operand of type '" +
146 TypeManager.MonoBASIC_Name (t) + "'");
150 /// The result has been already resolved:
152 /// FIXME: a minus constant -128 sbyte cant be turned into a
155 static Expression TryReduceNegative (Constant expr)
159 if (expr is IntConstant)
160 e = new IntConstant (-((IntConstant) expr).Value);
161 else if (expr is UIntConstant){
162 uint value = ((UIntConstant) expr).Value;
164 if (value < 2147483649)
165 return new IntConstant (-(int)value);
167 e = new LongConstant (value);
169 else if (expr is LongConstant)
170 e = new LongConstant (-((LongConstant) expr).Value);
171 else if (expr is ULongConstant){
172 ulong value = ((ULongConstant) expr).Value;
174 if (value < 9223372036854775809)
175 return new LongConstant(-(long)value);
177 else if (expr is FloatConstant)
178 e = new FloatConstant (-((FloatConstant) expr).Value);
179 else if (expr is DoubleConstant)
180 e = new DoubleConstant (-((DoubleConstant) expr).Value);
181 else if (expr is DecimalConstant)
182 e = new DecimalConstant (-((DecimalConstant) expr).Value);
183 else if (expr is ShortConstant)
184 e = new IntConstant (-((ShortConstant) expr).Value);
185 else if (expr is UShortConstant)
186 e = new IntConstant (-((UShortConstant) expr).Value);
191 // This routine will attempt to simplify the unary expression when the
192 // argument is a constant. The result is returned in 'result' and the
193 // function returns true or false depending on whether a reduction
194 // was performed or not
196 bool Reduce (EmitContext ec, Constant e, out Expression result)
198 Type expr_type = e.Type;
201 case Operator.UnaryPlus:
205 case Operator.UnaryNegation:
206 result = TryReduceNegative (e);
209 case Operator.LogicalNot:
210 if (expr_type != TypeManager.bool_type) {
216 BoolConstant b = (BoolConstant) e;
217 result = 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)))){
231 if (e is EnumConstant){
232 EnumConstant enum_constant = (EnumConstant) e;
235 if (Reduce (ec, enum_constant.Child, out reduced)){
236 result = new EnumConstant ((Constant) reduced, enum_constant.Type);
244 if (expr_type == TypeManager.int32_type){
245 result = new IntConstant (~ ((IntConstant) e).Value);
246 } else if (expr_type == TypeManager.uint32_type){
247 result = new UIntConstant (~ ((UIntConstant) e).Value);
248 } else if (expr_type == TypeManager.int64_type){
249 result = new LongConstant (~ ((LongConstant) e).Value);
250 } else if (expr_type == TypeManager.uint64_type){
251 result = new ULongConstant (~ ((ULongConstant) e).Value);
259 case Operator.AddressOf:
263 case Operator.Indirection:
267 throw new Exception ("Can not constant fold: " + Oper.ToString());
270 Expression ResolveOperator (EmitContext ec)
272 Type expr_type = Expr.Type;
275 // Step 1: Perform Operator Overload location
280 op_name = oper_names [(int) Oper];
282 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
285 Expression e = StaticCallExpr.MakeSimpleCall (
286 ec, (MethodGroupExpr) mg, Expr, loc);
296 // Only perform numeric promotions on:
299 if (expr_type == null)
303 // Step 2: Default operations on CLI native types.
306 // Attempt to use a constant folding operation.
307 if (Expr is Constant){
310 if (Reduce (ec, (Constant) Expr, out result))
315 case Operator.LogicalNot:
316 if (expr_type != TypeManager.bool_type) {
321 type = TypeManager.bool_type;
324 case Operator.OnesComplement:
325 if (!((expr_type == TypeManager.int32_type) ||
326 (expr_type == TypeManager.uint32_type) ||
327 (expr_type == TypeManager.int64_type) ||
328 (expr_type == TypeManager.uint64_type) ||
329 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
332 e = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc);
334 type = TypeManager.int32_type;
337 e = ConvertImplicit (ec, Expr, TypeManager.uint32_type, loc);
339 type = TypeManager.uint32_type;
342 e = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc);
344 type = TypeManager.int64_type;
347 e = ConvertImplicit (ec, Expr, TypeManager.uint64_type, loc);
349 type = TypeManager.uint64_type;
358 case Operator.AddressOf:
359 if (Expr.eclass != ExprClass.Variable){
360 Error (211, "Cannot take the address of non-variables");
369 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
373 string ptr_type_name = Expr.Type.FullName + "*";
374 type = TypeManager.LookupType (ptr_type_name);
378 case Operator.Indirection:
384 if (!expr_type.IsPointer){
387 "The * or -> operator can only be applied to pointers");
392 // We create an Indirection expression, because
393 // it can implement the IMemoryLocation.
395 return new Indirection (Expr, loc);
397 case Operator.UnaryPlus:
399 // A plus in front of something is just a no-op, so return the child.
403 case Operator.UnaryNegation:
405 // Deals with -literals
406 // int operator- (int x)
407 // long operator- (long x)
408 // float operator- (float f)
409 // double operator- (double d)
410 // decimal operator- (decimal d)
412 Expression expr = null;
415 // transform - - expr into expr
418 Unary unary = (Unary) Expr;
420 if (unary.Oper == Operator.UnaryNegation)
425 // perform numeric promotions to int,
429 // The following is inneficient, because we call
430 // ConvertImplicit too many times.
432 // It is also not clear if we should convert to Float
433 // or Double initially.
435 if (expr_type == TypeManager.uint32_type){
437 // FIXME: handle exception to this rule that
438 // permits the int value -2147483648 (-2^31) to
439 // bt wrote as a decimal interger literal
441 type = TypeManager.int64_type;
442 Expr = ConvertImplicit (ec, Expr, type, loc);
446 if (expr_type == TypeManager.uint64_type){
448 // FIXME: Handle exception of 'long value'
449 // -92233720368547758087 (-2^63) to be wrote as
450 // decimal integer literal.
456 if (expr_type == TypeManager.float_type){
461 expr = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc);
468 expr = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc);
475 expr = ConvertImplicit (ec, Expr, TypeManager.double_type, loc);
486 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
487 TypeManager.MonoBASIC_Name (expr_type) + "'");
491 public override Expression DoResolve (EmitContext ec)
493 if (Oper == Operator.AddressOf)
494 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
496 Expr = Expr.Resolve (ec);
501 eclass = ExprClass.Value;
502 return ResolveOperator (ec);
505 public override void Emit (EmitContext ec)
507 ILGenerator ig = ec.ig;
508 Type expr_type = Expr.Type;
511 case Operator.UnaryPlus:
512 throw new Exception ("This should be caught by Resolve");
514 case Operator.UnaryNegation:
516 ig.Emit (OpCodes.Neg);
519 case Operator.LogicalNot:
521 ig.Emit (OpCodes.Ldc_I4_0);
522 ig.Emit (OpCodes.Ceq);
525 case Operator.OnesComplement:
527 ig.Emit (OpCodes.Not);
530 case Operator.AddressOf:
531 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
535 throw new Exception ("This should not happen: Operator = "
541 /// This will emit the child expression for 'ec' avoiding the logical
542 /// not. The parent will take care of changing brfalse/brtrue
544 public void EmitLogicalNot (EmitContext ec)
546 if (Oper != Operator.LogicalNot)
547 throw new Exception ("EmitLogicalNot can only be called with !expr");
552 public override string ToString ()
554 return "Unary (" + Oper + ", " + Expr + ")";
560 // Unary operators are turned into Indirection expressions
561 // after semantic analysis (this is so we can take the address
562 // of an indirection).
564 public class Indirection : Expression, IMemoryLocation, IAssignMethod {
566 LocalTemporary temporary;
569 public Indirection (Expression expr, Location l)
572 this.type = TypeManager.TypeToCoreType (expr.Type.GetElementType ());
573 eclass = ExprClass.Variable;
577 void LoadExprValue (EmitContext ec)
581 public override void Emit (EmitContext ec)
583 ILGenerator ig = ec.ig;
585 if (temporary != null){
591 ec.ig.Emit (OpCodes.Dup);
592 temporary.Store (ec);
593 have_temporary = true;
597 LoadFromPtr (ig, Type);
600 public void EmitAssign (EmitContext ec, Expression source)
602 if (temporary != null){
608 ec.ig.Emit (OpCodes.Dup);
609 temporary.Store (ec);
610 have_temporary = true;
615 StoreFromPtr (ec.ig, type);
618 public void AddressOf (EmitContext ec, AddressOp Mode)
620 if (temporary != null){
626 ec.ig.Emit (OpCodes.Dup);
627 temporary.Store (ec);
628 have_temporary = true;
633 public override Expression DoResolve (EmitContext ec)
636 // Born fully resolved
641 public new void CacheTemporaries (EmitContext ec)
643 temporary = new LocalTemporary (ec, type);
648 /// Unary Mutator expressions (pre and post ++ and --)
652 /// UnaryMutator implements ++ and -- expressions. It derives from
653 /// ExpressionStatement becuase the pre/post increment/decrement
654 /// operators can be used in a statement context.
656 /// FIXME: Idea, we could split this up in two classes, one simpler
657 /// for the common case, and one with the extra fields for more complex
658 /// classes (indexers require temporary access; overloaded require method)
660 /// Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
661 /// PostDecrement, that way we could save the 'Mode' byte as well.
663 public class UnaryMutator : ExpressionStatement {
664 public enum Mode : byte {
665 PreIncrement, PreDecrement, PostIncrement, PostDecrement
670 LocalTemporary temp_storage;
673 // This is expensive for the simplest case.
677 public UnaryMutator (Mode m, Expression e, Location l)
684 static string OperName (Mode mode)
686 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
690 void Error23 (Type t)
693 23, "Operator " + OperName (mode) +
694 " cannot be applied to operand of type '" +
695 TypeManager.MonoBASIC_Name (t) + "'");
699 /// Returns whether an object of type 't' can be incremented
700 /// or decremented with add/sub (ie, basically whether we can
701 /// use pre-post incr-decr operations on it, but it is not a
702 /// System.Decimal, which we require operator overloading to catch)
704 static bool IsIncrementableNumber (Type t)
706 return (t == TypeManager.sbyte_type) ||
707 (t == TypeManager.byte_type) ||
708 (t == TypeManager.short_type) ||
709 (t == TypeManager.ushort_type) ||
710 (t == TypeManager.int32_type) ||
711 (t == TypeManager.uint32_type) ||
712 (t == TypeManager.int64_type) ||
713 (t == TypeManager.uint64_type) ||
714 (t == TypeManager.char_type) ||
715 (t.IsSubclassOf (TypeManager.enum_type)) ||
716 (t == TypeManager.float_type) ||
717 (t == TypeManager.double_type) ||
718 (t.IsPointer && t != TypeManager.void_ptr_type);
721 Expression ResolveOperator (EmitContext ec)
723 Type expr_type = expr.Type;
726 // Step 1: Perform Operator Overload location
731 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
732 op_name = "op_Increment";
734 op_name = "op_Decrement";
736 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
738 if (mg == null && expr_type.BaseType != null)
739 mg = MemberLookup (ec, expr_type.BaseType, op_name,
740 MemberTypes.Method, AllBindingFlags, loc);
743 method = StaticCallExpr.MakeSimpleCall (
744 ec, (MethodGroupExpr) mg, expr, loc);
751 // The operand of the prefix/postfix increment decrement operators
752 // should be an expression that is classified as a variable,
753 // a property access or an indexer access
756 if (expr.eclass == ExprClass.Variable){
757 if (IsIncrementableNumber (expr_type) ||
758 expr_type == TypeManager.decimal_type){
761 } else if (expr.eclass == ExprClass.IndexerAccess){
762 IndexerAccess ia = (IndexerAccess) expr;
764 temp_storage = new LocalTemporary (ec, expr.Type);
766 expr = ia.ResolveLValue (ec, temp_storage);
771 } else if (expr.eclass == ExprClass.PropertyAccess){
772 PropertyExpr pe = (PropertyExpr) expr;
774 if (pe.VerifyAssignable ())
779 expr.Error118 ("variable, indexer or property access");
783 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
784 TypeManager.MonoBASIC_Name (expr_type) + "'");
788 public override Expression DoResolve (EmitContext ec)
790 expr = expr.Resolve (ec);
795 eclass = ExprClass.Value;
796 return ResolveOperator (ec);
799 static int PtrTypeSize (Type t)
801 return GetTypeSize (t.GetElementType ());
805 // Loads the proper "1" into the stack based on the type
807 static void LoadOne (ILGenerator ig, Type t)
809 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
810 ig.Emit (OpCodes.Ldc_I8, 1L);
811 else if (t == TypeManager.double_type)
812 ig.Emit (OpCodes.Ldc_R8, 1.0);
813 else if (t == TypeManager.float_type)
814 ig.Emit (OpCodes.Ldc_R4, 1.0F);
815 else if (t.IsPointer){
816 int n = PtrTypeSize (t);
819 ig.Emit (OpCodes.Sizeof, t);
821 IntConstant.EmitInt (ig, n);
823 ig.Emit (OpCodes.Ldc_I4_1);
828 // FIXME: We need some way of avoiding the use of temp_storage
829 // for some types of storage (parameters, local variables,
830 // static fields) and single-dimension array access.
832 void EmitCode (EmitContext ec, bool is_expr)
834 ILGenerator ig = ec.ig;
835 IAssignMethod ia = (IAssignMethod) expr;
836 Type expr_type = expr.Type;
838 if (temp_storage == null)
839 temp_storage = new LocalTemporary (ec, expr_type);
841 ia.CacheTemporaries (ec);
842 ig.Emit (OpCodes.Nop);
844 case Mode.PreIncrement:
845 case Mode.PreDecrement:
849 LoadOne (ig, expr_type);
852 // Select the opcode based on the check state (then the type)
853 // and the actual operation
856 if (expr_type == TypeManager.int32_type ||
857 expr_type == TypeManager.int64_type){
858 if (mode == Mode.PreDecrement)
859 ig.Emit (OpCodes.Sub_Ovf);
861 ig.Emit (OpCodes.Add_Ovf);
862 } else if (expr_type == TypeManager.uint32_type ||
863 expr_type == TypeManager.uint64_type){
864 if (mode == Mode.PreDecrement)
865 ig.Emit (OpCodes.Sub_Ovf_Un);
867 ig.Emit (OpCodes.Add_Ovf_Un);
869 if (mode == Mode.PreDecrement)
870 ig.Emit (OpCodes.Sub_Ovf);
872 ig.Emit (OpCodes.Add_Ovf);
875 if (mode == Mode.PreDecrement)
876 ig.Emit (OpCodes.Sub);
878 ig.Emit (OpCodes.Add);
883 temp_storage.Store (ec);
884 ia.EmitAssign (ec, temp_storage);
886 temp_storage.Emit (ec);
889 case Mode.PostIncrement:
890 case Mode.PostDecrement:
898 ig.Emit (OpCodes.Dup);
900 LoadOne (ig, expr_type);
903 if (expr_type == TypeManager.int32_type ||
904 expr_type == TypeManager.int64_type){
905 if (mode == Mode.PostDecrement)
906 ig.Emit (OpCodes.Sub_Ovf);
908 ig.Emit (OpCodes.Add_Ovf);
909 } else if (expr_type == TypeManager.uint32_type ||
910 expr_type == TypeManager.uint64_type){
911 if (mode == Mode.PostDecrement)
912 ig.Emit (OpCodes.Sub_Ovf_Un);
914 ig.Emit (OpCodes.Add_Ovf_Un);
916 if (mode == Mode.PostDecrement)
917 ig.Emit (OpCodes.Sub_Ovf);
919 ig.Emit (OpCodes.Add_Ovf);
922 if (mode == Mode.PostDecrement)
923 ig.Emit (OpCodes.Sub);
925 ig.Emit (OpCodes.Add);
931 temp_storage.Store (ec);
932 ia.EmitAssign (ec, temp_storage);
937 public override void Emit (EmitContext ec)
943 public override void EmitStatement (EmitContext ec)
945 EmitCode (ec, false);
951 /// Base class for the 'Is' and 'As' classes.
955 /// FIXME: Split this in two, and we get to save the 'Operator' Oper
958 public abstract class Probe : Expression {
959 public readonly Expression ProbeType;
960 protected Expression expr;
961 protected Type probe_type;
963 public Probe (Expression expr, Expression probe_type, Location l)
965 ProbeType = probe_type;
970 public Expression Expr {
976 public override Expression DoResolve (EmitContext ec)
978 probe_type = ec.DeclSpace.ResolveType (ProbeType, false, loc);
980 if (probe_type == null)
983 expr = expr.Resolve (ec);
990 /// Implementation of the 'is' operator.
992 public class Is : Probe {
993 public Is (Expression expr, Expression probe_type, Location l)
994 : base (expr, probe_type, l)
999 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
1004 public override void Emit (EmitContext ec)
1006 ILGenerator ig = ec.ig;
1011 case Action.AlwaysFalse:
1012 ig.Emit (OpCodes.Pop);
1013 IntConstant.EmitInt (ig, 0);
1015 case Action.AlwaysTrue:
1016 ig.Emit (OpCodes.Pop);
1017 ig.Emit (OpCodes.Nop);
1018 IntConstant.EmitInt (ig, 1);
1020 case Action.LeaveOnStack:
1021 // the 'e != null' rule.
1024 ig.Emit (OpCodes.Isinst, probe_type);
1025 ig.Emit (OpCodes.Ldnull);
1026 ig.Emit (OpCodes.Cgt_Un);
1029 throw new Exception ("never reached");
1032 public override Expression DoResolve (EmitContext ec)
1034 Expression e = base.DoResolve (ec);
1036 if ((e == null) || (expr == null))
1039 Type etype = expr.Type;
1040 bool warning_always_matches = false;
1041 bool warning_never_matches = false;
1043 type = TypeManager.bool_type;
1044 eclass = ExprClass.Value;
1047 // First case, if at compile time, there is an implicit conversion
1048 // then e != null (objects) or true (value types)
1050 e = ConvertImplicitStandard (ec, expr, probe_type, loc);
1053 if (etype.IsValueType)
1054 action = Action.AlwaysTrue;
1056 action = Action.LeaveOnStack;
1058 warning_always_matches = true;
1059 } else if (ExplicitReferenceConversionExists (etype, probe_type)){
1061 // Second case: explicit reference convresion
1063 if (expr is NullLiteral)
1064 action = Action.AlwaysFalse;
1066 action = Action.Probe;
1068 action = Action.AlwaysFalse;
1069 warning_never_matches = true;
1072 if (RootContext.WarningLevel >= 1){
1073 if (warning_always_matches)
1076 "The expression is always of type '" +
1077 TypeManager.MonoBASIC_Name (probe_type) + "'");
1078 else if (warning_never_matches){
1079 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1082 "The expression is never of type '" +
1083 TypeManager.MonoBASIC_Name (probe_type) + "'");
1092 /// Implementation of the 'as' operator.
1094 public class As : Probe {
1095 public As (Expression expr, Expression probe_type, Location l)
1096 : base (expr, probe_type, l)
1100 bool do_isinst = false;
1102 public override void Emit (EmitContext ec)
1104 ILGenerator ig = ec.ig;
1109 ig.Emit (OpCodes.Isinst, probe_type);
1112 static void Error_CannotConvertType (Type source, Type target, Location loc)
1115 39, loc, "as operator can not convert from '" +
1116 TypeManager.MonoBASIC_Name (source) + "' to '" +
1117 TypeManager.MonoBASIC_Name (target) + "'");
1120 public override Expression DoResolve (EmitContext ec)
1122 Expression e = base.DoResolve (ec);
1128 eclass = ExprClass.Value;
1129 Type etype = expr.Type;
1131 if (TypeManager.IsValueType (probe_type)){
1132 Report.Error (77, loc, "The as operator should be used with a reference type only (" +
1133 TypeManager.MonoBASIC_Name (probe_type) + " is a value type)");
1138 e = ConvertImplicit (ec, expr, probe_type, loc);
1145 if (ExplicitReferenceConversionExists (etype, probe_type)){
1150 Error_CannotConvertType (etype, probe_type, loc);
1156 /// This represents a typecast in the source language.
1158 /// FIXME: Cast expressions have an unusual set of parsing
1159 /// rules, we need to figure those out.
1161 public class Cast : Expression {
1162 Expression target_type;
1165 public Cast (Expression cast_type, Expression expr, Location loc)
1167 this.target_type = cast_type;
1172 public Expression TargetType {
1178 public Expression Expr {
1188 /// Attempts to do a compile-time folding of a constant cast.
1190 Expression TryReduce (EmitContext ec, Type target_type)
1192 if (expr is ByteConstant){
1193 byte v = ((ByteConstant) expr).Value;
1195 if (target_type == TypeManager.sbyte_type)
1196 return new SByteConstant ((sbyte) v);
1197 if (target_type == TypeManager.short_type)
1198 return new ShortConstant ((short) v);
1199 if (target_type == TypeManager.ushort_type)
1200 return new UShortConstant ((ushort) v);
1201 if (target_type == TypeManager.int32_type)
1202 return new IntConstant ((int) v);
1203 if (target_type == TypeManager.uint32_type)
1204 return new UIntConstant ((uint) v);
1205 if (target_type == TypeManager.int64_type)
1206 return new LongConstant ((long) v);
1207 if (target_type == TypeManager.uint64_type)
1208 return new ULongConstant ((ulong) v);
1209 if (target_type == TypeManager.float_type)
1210 return new FloatConstant ((float) v);
1211 if (target_type == TypeManager.double_type)
1212 return new DoubleConstant ((double) v);
1213 if (target_type == TypeManager.char_type)
1214 return new CharConstant ((char) v);
1215 if (target_type == TypeManager.decimal_type)
1216 return new DecimalConstant ((decimal) v);
1218 if (expr is SByteConstant){
1219 sbyte v = ((SByteConstant) expr).Value;
1221 if (target_type == TypeManager.byte_type)
1222 return new ByteConstant ((byte) v);
1223 if (target_type == TypeManager.short_type)
1224 return new ShortConstant ((short) v);
1225 if (target_type == TypeManager.ushort_type)
1226 return new UShortConstant ((ushort) v);
1227 if (target_type == TypeManager.int32_type)
1228 return new IntConstant ((int) v);
1229 if (target_type == TypeManager.uint32_type)
1230 return new UIntConstant ((uint) v);
1231 if (target_type == TypeManager.int64_type)
1232 return new LongConstant ((long) v);
1233 if (target_type == TypeManager.uint64_type)
1234 return new ULongConstant ((ulong) v);
1235 if (target_type == TypeManager.float_type)
1236 return new FloatConstant ((float) v);
1237 if (target_type == TypeManager.double_type)
1238 return new DoubleConstant ((double) v);
1239 if (target_type == TypeManager.char_type)
1240 return new CharConstant ((char) v);
1241 if (target_type == TypeManager.decimal_type)
1242 return new DecimalConstant ((decimal) v);
1244 if (expr is ShortConstant){
1245 short v = ((ShortConstant) expr).Value;
1247 if (target_type == TypeManager.byte_type)
1248 return new ByteConstant ((byte) v);
1249 if (target_type == TypeManager.sbyte_type)
1250 return new SByteConstant ((sbyte) v);
1251 if (target_type == TypeManager.ushort_type)
1252 return new UShortConstant ((ushort) v);
1253 if (target_type == TypeManager.int32_type)
1254 return new IntConstant ((int) v);
1255 if (target_type == TypeManager.uint32_type)
1256 return new UIntConstant ((uint) v);
1257 if (target_type == TypeManager.int64_type)
1258 return new LongConstant ((long) v);
1259 if (target_type == TypeManager.uint64_type)
1260 return new ULongConstant ((ulong) v);
1261 if (target_type == TypeManager.float_type)
1262 return new FloatConstant ((float) v);
1263 if (target_type == TypeManager.double_type)
1264 return new DoubleConstant ((double) v);
1265 if (target_type == TypeManager.char_type)
1266 return new CharConstant ((char) v);
1267 if (target_type == TypeManager.decimal_type)
1268 return new DecimalConstant ((decimal) v);
1270 if (expr is UShortConstant){
1271 ushort v = ((UShortConstant) expr).Value;
1273 if (target_type == TypeManager.byte_type)
1274 return new ByteConstant ((byte) v);
1275 if (target_type == TypeManager.sbyte_type)
1276 return new SByteConstant ((sbyte) v);
1277 if (target_type == TypeManager.short_type)
1278 return new ShortConstant ((short) v);
1279 if (target_type == TypeManager.int32_type)
1280 return new IntConstant ((int) v);
1281 if (target_type == TypeManager.uint32_type)
1282 return new UIntConstant ((uint) v);
1283 if (target_type == TypeManager.int64_type)
1284 return new LongConstant ((long) v);
1285 if (target_type == TypeManager.uint64_type)
1286 return new ULongConstant ((ulong) v);
1287 if (target_type == TypeManager.float_type)
1288 return new FloatConstant ((float) v);
1289 if (target_type == TypeManager.double_type)
1290 return new DoubleConstant ((double) v);
1291 if (target_type == TypeManager.char_type)
1292 return new CharConstant ((char) v);
1293 if (target_type == TypeManager.decimal_type)
1294 return new DecimalConstant ((decimal) v);
1296 if (expr is IntConstant){
1297 int v = ((IntConstant) expr).Value;
1299 if (target_type == TypeManager.byte_type)
1300 return new ByteConstant ((byte) v);
1301 if (target_type == TypeManager.sbyte_type)
1302 return new SByteConstant ((sbyte) v);
1303 if (target_type == TypeManager.short_type)
1304 return new ShortConstant ((short) v);
1305 if (target_type == TypeManager.ushort_type)
1306 return new UShortConstant ((ushort) v);
1307 if (target_type == TypeManager.uint32_type)
1308 return new UIntConstant ((uint) v);
1309 if (target_type == TypeManager.int64_type)
1310 return new LongConstant ((long) v);
1311 if (target_type == TypeManager.uint64_type)
1312 return new ULongConstant ((ulong) v);
1313 if (target_type == TypeManager.float_type)
1314 return new FloatConstant ((float) v);
1315 if (target_type == TypeManager.double_type)
1316 return new DoubleConstant ((double) v);
1317 if (target_type == TypeManager.char_type)
1318 return new CharConstant ((char) v);
1319 if (target_type == TypeManager.decimal_type)
1320 return new DecimalConstant ((decimal) v);
1322 if (expr is UIntConstant){
1323 uint v = ((UIntConstant) expr).Value;
1325 if (target_type == TypeManager.byte_type)
1326 return new ByteConstant ((byte) v);
1327 if (target_type == TypeManager.sbyte_type)
1328 return new SByteConstant ((sbyte) v);
1329 if (target_type == TypeManager.short_type)
1330 return new ShortConstant ((short) v);
1331 if (target_type == TypeManager.ushort_type)
1332 return new UShortConstant ((ushort) v);
1333 if (target_type == TypeManager.int32_type)
1334 return new IntConstant ((int) v);
1335 if (target_type == TypeManager.int64_type)
1336 return new LongConstant ((long) v);
1337 if (target_type == TypeManager.uint64_type)
1338 return new ULongConstant ((ulong) v);
1339 if (target_type == TypeManager.float_type)
1340 return new FloatConstant ((float) v);
1341 if (target_type == TypeManager.double_type)
1342 return new DoubleConstant ((double) v);
1343 if (target_type == TypeManager.char_type)
1344 return new CharConstant ((char) v);
1345 if (target_type == TypeManager.decimal_type)
1346 return new DecimalConstant ((decimal) v);
1348 if (expr is LongConstant){
1349 long v = ((LongConstant) expr).Value;
1351 if (target_type == TypeManager.byte_type)
1352 return new ByteConstant ((byte) v);
1353 if (target_type == TypeManager.sbyte_type)
1354 return new SByteConstant ((sbyte) v);
1355 if (target_type == TypeManager.short_type)
1356 return new ShortConstant ((short) v);
1357 if (target_type == TypeManager.ushort_type)
1358 return new UShortConstant ((ushort) v);
1359 if (target_type == TypeManager.int32_type)
1360 return new IntConstant ((int) v);
1361 if (target_type == TypeManager.uint32_type)
1362 return new UIntConstant ((uint) v);
1363 if (target_type == TypeManager.uint64_type)
1364 return new ULongConstant ((ulong) v);
1365 if (target_type == TypeManager.float_type)
1366 return new FloatConstant ((float) v);
1367 if (target_type == TypeManager.double_type)
1368 return new DoubleConstant ((double) v);
1369 if (target_type == TypeManager.char_type)
1370 return new CharConstant ((char) v);
1371 if (target_type == TypeManager.decimal_type)
1372 return new DecimalConstant ((decimal) v);
1374 if (expr is ULongConstant){
1375 ulong v = ((ULongConstant) expr).Value;
1377 if (target_type == TypeManager.byte_type)
1378 return new ByteConstant ((byte) v);
1379 if (target_type == TypeManager.sbyte_type)
1380 return new SByteConstant ((sbyte) v);
1381 if (target_type == TypeManager.short_type)
1382 return new ShortConstant ((short) v);
1383 if (target_type == TypeManager.ushort_type)
1384 return new UShortConstant ((ushort) v);
1385 if (target_type == TypeManager.int32_type)
1386 return new IntConstant ((int) v);
1387 if (target_type == TypeManager.uint32_type)
1388 return new UIntConstant ((uint) v);
1389 if (target_type == TypeManager.int64_type)
1390 return new LongConstant ((long) v);
1391 if (target_type == TypeManager.float_type)
1392 return new FloatConstant ((float) v);
1393 if (target_type == TypeManager.double_type)
1394 return new DoubleConstant ((double) v);
1395 if (target_type == TypeManager.char_type)
1396 return new CharConstant ((char) v);
1397 if (target_type == TypeManager.decimal_type)
1398 return new DecimalConstant ((decimal) v);
1400 if (expr is FloatConstant){
1401 float v = ((FloatConstant) expr).Value;
1403 if (target_type == TypeManager.byte_type)
1404 return new ByteConstant ((byte) v);
1405 if (target_type == TypeManager.sbyte_type)
1406 return new SByteConstant ((sbyte) v);
1407 if (target_type == TypeManager.short_type)
1408 return new ShortConstant ((short) v);
1409 if (target_type == TypeManager.ushort_type)
1410 return new UShortConstant ((ushort) v);
1411 if (target_type == TypeManager.int32_type)
1412 return new IntConstant ((int) v);
1413 if (target_type == TypeManager.uint32_type)
1414 return new UIntConstant ((uint) v);
1415 if (target_type == TypeManager.int64_type)
1416 return new LongConstant ((long) v);
1417 if (target_type == TypeManager.uint64_type)
1418 return new ULongConstant ((ulong) v);
1419 if (target_type == TypeManager.double_type)
1420 return new DoubleConstant ((double) v);
1421 if (target_type == TypeManager.char_type)
1422 return new CharConstant ((char) v);
1423 if (target_type == TypeManager.decimal_type)
1424 return new DecimalConstant ((decimal) v);
1426 if (expr is DoubleConstant){
1427 double v = ((DoubleConstant) expr).Value;
1429 if (target_type == TypeManager.byte_type)
1430 return new ByteConstant ((byte) v);
1431 if (target_type == TypeManager.sbyte_type)
1432 return new SByteConstant ((sbyte) v);
1433 if (target_type == TypeManager.short_type)
1434 return new ShortConstant ((short) v);
1435 if (target_type == TypeManager.ushort_type)
1436 return new UShortConstant ((ushort) v);
1437 if (target_type == TypeManager.int32_type)
1438 return new IntConstant ((int) v);
1439 if (target_type == TypeManager.uint32_type)
1440 return new UIntConstant ((uint) v);
1441 if (target_type == TypeManager.int64_type)
1442 return new LongConstant ((long) v);
1443 if (target_type == TypeManager.uint64_type)
1444 return new ULongConstant ((ulong) v);
1445 if (target_type == TypeManager.float_type)
1446 return new FloatConstant ((float) v);
1447 if (target_type == TypeManager.char_type)
1448 return new CharConstant ((char) v);
1449 if (target_type == TypeManager.decimal_type)
1450 return new DecimalConstant ((decimal) v);
1456 public override Expression DoResolve (EmitContext ec)
1458 expr = expr.Resolve (ec);
1462 int errors = Report.Errors;
1464 type = ec.DeclSpace.ResolveType (target_type, false, Location);
1469 eclass = ExprClass.Value;
1471 if (expr is Constant){
1472 Expression e = TryReduce (ec, type);
1478 expr = ConvertExplicit (ec, expr, type, loc);
1482 public override void Emit (EmitContext ec)
1485 // This one will never happen
1487 throw new Exception ("Should not happen");
1492 /// Binary operators
1494 public class Binary : Expression {
1495 public enum Operator : byte {
1496 Multiply, Division, Modulus,
1497 Addition, Subtraction,
1498 LeftShift, RightShift,
1499 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1500 Equality, Inequality,
1510 Expression left, right;
1513 // After resolution, method might contain the operator overload
1516 protected MethodBase method;
1517 ArrayList Arguments;
1519 bool DelegateOperation;
1521 // This must be kept in sync with Operator!!!
1522 static string [] oper_names;
1526 oper_names = new string [(int) Operator.TOP];
1528 oper_names [(int) Operator.Multiply] = "op_Multiply";
1529 oper_names [(int) Operator.Division] = "op_Division";
1530 oper_names [(int) Operator.Modulus] = "op_Modulus";
1531 oper_names [(int) Operator.Addition] = "op_Addition";
1532 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1533 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1534 oper_names [(int) Operator.RightShift] = "op_RightShift";
1535 oper_names [(int) Operator.LessThan] = "op_LessThan";
1536 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1537 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1538 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1539 oper_names [(int) Operator.Equality] = "op_Equality";
1540 oper_names [(int) Operator.Inequality] = "op_Inequality";
1541 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1542 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1543 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1544 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1545 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1548 public Binary (Operator oper, Expression left, Expression right, Location loc)
1556 public Operator Oper {
1565 public Expression Left {
1574 public Expression Right {
1585 /// Returns a stringified representation of the Operator
1587 static string OperName (Operator oper)
1590 case Operator.Multiply:
1592 case Operator.Division:
1594 case Operator.Modulus:
1596 case Operator.Addition:
1598 case Operator.Subtraction:
1600 case Operator.LeftShift:
1602 case Operator.RightShift:
1604 case Operator.LessThan:
1606 case Operator.GreaterThan:
1608 case Operator.LessThanOrEqual:
1610 case Operator.GreaterThanOrEqual:
1612 case Operator.Equality:
1614 case Operator.Inequality:
1616 case Operator.BitwiseAnd:
1618 case Operator.BitwiseOr:
1620 case Operator.ExclusiveOr:
1622 case Operator.LogicalOr:
1624 case Operator.LogicalAnd:
1628 return oper.ToString ();
1631 public override string ToString ()
1633 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1634 right.ToString () + ")";
1637 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1639 if (expr.Type == target_type)
1642 return ConvertImplicit (ec, expr, target_type, Location.Null);
1645 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
1648 34, loc, "Operator '" + OperName (oper)
1649 + "' is ambiguous on operands of type '"
1650 + TypeManager.MonoBASIC_Name (l) + "' "
1651 + "and '" + TypeManager.MonoBASIC_Name (r)
1656 // Note that handling the case l == Decimal || r == Decimal
1657 // is taken care of by the Step 1 Operator Overload resolution.
1659 bool DoNumericPromotions (EmitContext ec, Type l, Type r)
1661 if (l == TypeManager.double_type || r == TypeManager.double_type){
1663 // If either operand is of type double, the other operand is
1664 // conveted to type double.
1666 if (r != TypeManager.double_type)
1667 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
1668 if (l != TypeManager.double_type)
1669 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
1671 type = TypeManager.double_type;
1672 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1674 // if either operand is of type float, the other operand is
1675 // converted to type float.
1677 if (r != TypeManager.double_type)
1678 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
1679 if (l != TypeManager.double_type)
1680 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
1681 type = TypeManager.float_type;
1682 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1686 // If either operand is of type ulong, the other operand is
1687 // converted to type ulong. or an error ocurrs if the other
1688 // operand is of type sbyte, short, int or long
1690 if (l == TypeManager.uint64_type){
1691 if (r != TypeManager.uint64_type){
1692 if (right is IntConstant){
1693 IntConstant ic = (IntConstant) right;
1695 e = TryImplicitIntConversion (l, ic);
1698 } else if (right is LongConstant){
1699 long ll = ((LongConstant) right).Value;
1702 right = new ULongConstant ((ulong) ll);
1704 e = ImplicitNumericConversion (ec, right, l, loc);
1711 if (left is IntConstant){
1712 e = TryImplicitIntConversion (r, (IntConstant) left);
1715 } else if (left is LongConstant){
1716 long ll = ((LongConstant) left).Value;
1719 left = new ULongConstant ((ulong) ll);
1721 e = ImplicitNumericConversion (ec, left, r, loc);
1728 if ((other == TypeManager.sbyte_type) ||
1729 (other == TypeManager.short_type) ||
1730 (other == TypeManager.int32_type) ||
1731 (other == TypeManager.int64_type))
1732 Error_OperatorAmbiguous (loc, oper, l, r);
1733 type = TypeManager.uint64_type;
1734 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1736 // If either operand is of type long, the other operand is converted
1739 if (l != TypeManager.int64_type)
1740 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
1741 if (r != TypeManager.int64_type)
1742 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
1744 type = TypeManager.int64_type;
1745 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1747 // If either operand is of type uint, and the other
1748 // operand is of type sbyte, short or int, othe operands are
1749 // converted to type long.
1753 if (l == TypeManager.uint32_type){
1754 if (right is IntConstant){
1755 IntConstant ic = (IntConstant) right;
1759 right = new UIntConstant ((uint) val);
1766 else if (r == TypeManager.uint32_type){
1767 if (left is IntConstant){
1768 IntConstant ic = (IntConstant) left;
1772 left = new UIntConstant ((uint) val);
1781 if ((other == TypeManager.sbyte_type) ||
1782 (other == TypeManager.short_type) ||
1783 (other == TypeManager.int32_type)){
1784 left = ForceConversion (ec, left, TypeManager.int64_type);
1785 right = ForceConversion (ec, right, TypeManager.int64_type);
1786 type = TypeManager.int64_type;
1789 // if either operand is of type uint, the other
1790 // operand is converd to type uint
1792 left = ForceConversion (ec, left, TypeManager.uint32_type);
1793 right = ForceConversion (ec, right, TypeManager.uint32_type);
1794 type = TypeManager.uint32_type;
1796 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1797 if (l != TypeManager.decimal_type)
1798 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
1800 if (r != TypeManager.decimal_type)
1801 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
1802 type = TypeManager.decimal_type;
1804 left = ForceConversion (ec, left, TypeManager.int32_type);
1805 right = ForceConversion (ec, right, TypeManager.int32_type);
1807 type = TypeManager.int32_type;
1810 return (left != null) && (right != null);
1813 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
1815 Report.Error (19, loc,
1816 "Operator " + name + " cannot be applied to operands of type '" +
1817 TypeManager.MonoBASIC_Name (l) + "' and '" +
1818 TypeManager.MonoBASIC_Name (r) + "'");
1821 void Error_OperatorCannotBeApplied ()
1823 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
1826 static bool is_32_or_64 (Type t)
1828 return (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
1829 t == TypeManager.int64_type || t == TypeManager.uint64_type);
1832 static bool is_unsigned (Type t)
1834 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
1835 t == TypeManager.short_type || t == TypeManager.byte_type);
1838 Expression CheckShiftArguments (EmitContext ec)
1842 Type r = right.Type;
1844 e = ForceConversion (ec, right, TypeManager.int32_type);
1846 Error_OperatorCannotBeApplied ();
1851 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
1852 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
1853 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
1854 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
1860 Error_OperatorCannotBeApplied ();
1864 Expression ResolveOperator (EmitContext ec)
1867 Type r = right.Type;
1869 bool overload_failed = false;
1872 // Step 1: Perform Operator Overload location
1874 Expression left_expr, right_expr;
1876 string op = oper_names [(int) oper];
1878 MethodGroupExpr union;
1879 left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
1881 right_expr = MemberLookup (
1882 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
1883 union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
1885 union = (MethodGroupExpr) left_expr;
1887 if (union != null) {
1888 Arguments = new ArrayList ();
1889 Arguments.Add (new Argument (left, Argument.AType.Expression));
1890 Arguments.Add (new Argument (right, Argument.AType.Expression));
1892 method = Invocation.OverloadResolve (ec, union, Arguments, Location.Null);
1893 if (method != null) {
1894 MethodInfo mi = (MethodInfo) method;
1896 type = mi.ReturnType;
1899 overload_failed = true;
1904 // Step 2: Default operations on CLI native types.
1908 // Step 0: String concatenation (because overloading will get this wrong)
1910 if (oper == Operator.Addition){
1912 // If any of the arguments is a string, cast to string
1915 if (l == TypeManager.string_type){
1917 if (r == TypeManager.void_type) {
1918 Error_OperatorCannotBeApplied ();
1922 if (r == TypeManager.string_type){
1923 if (left is Constant && right is Constant){
1924 StringConstant ls = (StringConstant) left;
1925 StringConstant rs = (StringConstant) right;
1927 return new StringConstant (
1928 ls.Value + rs.Value);
1932 method = TypeManager.string_concat_string_string;
1935 method = TypeManager.string_concat_object_object;
1936 right = ConvertImplicit (ec, right,
1937 TypeManager.object_type, loc);
1939 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
1943 type = TypeManager.string_type;
1945 Arguments = new ArrayList ();
1946 Arguments.Add (new Argument (left, Argument.AType.Expression));
1947 Arguments.Add (new Argument (right, Argument.AType.Expression));
1951 } else if (r == TypeManager.string_type){
1954 if (l == TypeManager.void_type) {
1955 Error_OperatorCannotBeApplied ();
1959 method = TypeManager.string_concat_object_object;
1960 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1962 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
1965 Arguments = new ArrayList ();
1966 Arguments.Add (new Argument (left, Argument.AType.Expression));
1967 Arguments.Add (new Argument (right, Argument.AType.Expression));
1969 type = TypeManager.string_type;
1975 // Transform a + ( - b) into a - b
1977 if (right is Unary){
1978 Unary right_unary = (Unary) right;
1980 if (right_unary.Oper == Unary.Operator.UnaryNegation){
1981 oper = Operator.Subtraction;
1982 right = right_unary.Expr;
1988 if (oper == Operator.Equality || oper == Operator.Inequality){
1989 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
1990 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
1991 Error_OperatorCannotBeApplied ();
1995 type = TypeManager.bool_type;
2000 // operator != (object a, object b)
2001 // operator == (object a, object b)
2003 // For this to be used, both arguments have to be reference-types.
2004 // Read the rationale on the spec (14.9.6)
2006 // Also, if at compile time we know that the classes do not inherit
2007 // one from the other, then we catch the error there.
2009 if (!(l.IsValueType || r.IsValueType)){
2010 type = TypeManager.bool_type;
2015 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
2019 // Also, a standard conversion must exist from either one
2021 if (!(StandardConversionExists (left, r) ||
2022 StandardConversionExists (right, l))){
2023 Error_OperatorCannotBeApplied ();
2027 // We are going to have to convert to an object to compare
2029 if (l != TypeManager.object_type)
2030 left = new EmptyCast (left, TypeManager.object_type);
2031 if (r != TypeManager.object_type)
2032 right = new EmptyCast (right, TypeManager.object_type);
2035 // FIXME: CSC here catches errors cs254 and cs252
2041 // One of them is a valuetype, but the other one is not.
2043 if (!l.IsValueType || !r.IsValueType) {
2044 Error_OperatorCannotBeApplied ();
2049 // Only perform numeric promotions on:
2050 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2052 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2053 if (l.IsSubclassOf (TypeManager.delegate_type) &&
2054 r.IsSubclassOf (TypeManager.delegate_type)) {
2056 Arguments = new ArrayList ();
2057 Arguments.Add (new Argument (left, Argument.AType.Expression));
2058 Arguments.Add (new Argument (right, Argument.AType.Expression));
2060 if (oper == Operator.Addition)
2061 method = TypeManager.delegate_combine_delegate_delegate;
2063 method = TypeManager.delegate_remove_delegate_delegate;
2066 Error_OperatorCannotBeApplied ();
2070 DelegateOperation = true;
2076 // Pointer arithmetic:
2078 // T* operator + (T* x, int y);
2079 // T* operator + (T* x, uint y);
2080 // T* operator + (T* x, long y);
2081 // T* operator + (T* x, ulong y);
2083 // T* operator + (int y, T* x);
2084 // T* operator + (uint y, T *x);
2085 // T* operator + (long y, T *x);
2086 // T* operator + (ulong y, T *x);
2088 // T* operator - (T* x, int y);
2089 // T* operator - (T* x, uint y);
2090 // T* operator - (T* x, long y);
2091 // T* operator - (T* x, ulong y);
2093 // long operator - (T* x, T *y)
2096 if (r.IsPointer && oper == Operator.Subtraction){
2098 return new PointerArithmetic (
2099 false, left, right, TypeManager.int64_type,
2101 } else if (is_32_or_64 (r))
2102 return new PointerArithmetic (
2103 oper == Operator.Addition, left, right, l, loc);
2104 } else if (r.IsPointer && is_32_or_64 (l) && oper == Operator.Addition)
2105 return new PointerArithmetic (
2106 true, right, left, r, loc);
2110 // Enumeration operators
2112 bool lie = TypeManager.IsEnumType (l);
2113 bool rie = TypeManager.IsEnumType (r);
2117 // U operator - (E e, E f)
2118 if (lie && rie && oper == Operator.Subtraction){
2120 type = TypeManager.EnumToUnderlying (l);
2123 Error_OperatorCannotBeApplied ();
2128 // operator + (E e, U x)
2129 // operator - (E e, U x)
2131 if (oper == Operator.Addition || oper == Operator.Subtraction){
2132 Type enum_type = lie ? l : r;
2133 Type other_type = lie ? r : l;
2134 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2137 if (underlying_type != other_type){
2138 Error_OperatorCannotBeApplied ();
2147 temp = ConvertImplicit (ec, right, l, loc);
2151 Error_OperatorCannotBeApplied ();
2155 temp = ConvertImplicit (ec, left, r, loc);
2160 Error_OperatorCannotBeApplied ();
2165 if (oper == Operator.Equality || oper == Operator.Inequality ||
2166 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2167 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2168 type = TypeManager.bool_type;
2172 if (oper == Operator.BitwiseAnd ||
2173 oper == Operator.BitwiseOr ||
2174 oper == Operator.ExclusiveOr){
2178 Error_OperatorCannotBeApplied ();
2182 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2183 return CheckShiftArguments (ec);
2185 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2186 if (l != TypeManager.bool_type || r != TypeManager.bool_type){
2187 Error_OperatorCannotBeApplied ();
2191 type = TypeManager.bool_type;
2196 // operator & (bool x, bool y)
2197 // operator | (bool x, bool y)
2198 // operator ^ (bool x, bool y)
2200 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2201 if (oper == Operator.BitwiseAnd ||
2202 oper == Operator.BitwiseOr ||
2203 oper == Operator.ExclusiveOr){
2210 // Pointer comparison
2212 if (l.IsPointer && r.IsPointer){
2213 if (oper == Operator.Equality || oper == Operator.Inequality ||
2214 oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2215 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2216 type = TypeManager.bool_type;
2222 // We are dealing with numbers
2224 if (overload_failed){
2225 Error_OperatorCannotBeApplied ();
2230 // This will leave left or right set to null if there is an error
2232 DoNumericPromotions (ec, l, r);
2233 if (left == null || right == null){
2234 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2239 // reload our cached types if required
2244 if (oper == Operator.BitwiseAnd ||
2245 oper == Operator.BitwiseOr ||
2246 oper == Operator.ExclusiveOr){
2248 if (!((l == TypeManager.int32_type) ||
2249 (l == TypeManager.uint32_type) ||
2250 (l == TypeManager.int64_type) ||
2251 (l == TypeManager.uint64_type)))
2254 Error_OperatorCannotBeApplied ();
2259 if (oper == Operator.Equality ||
2260 oper == Operator.Inequality ||
2261 oper == Operator.LessThanOrEqual ||
2262 oper == Operator.LessThan ||
2263 oper == Operator.GreaterThanOrEqual ||
2264 oper == Operator.GreaterThan){
2265 type = TypeManager.bool_type;
2271 public override Expression DoResolve (EmitContext ec)
2273 left = left.Resolve (ec);
2274 right = right.Resolve (ec);
2276 if (left == null || right == null)
2279 if (left.Type == null)
2280 throw new Exception (
2281 "Resolve returned non null, but did not set the type! (" +
2282 left + ") at Line: " + loc.Row);
2283 if (right.Type == null)
2284 throw new Exception (
2285 "Resolve returned non null, but did not set the type! (" +
2286 right + ") at Line: "+ loc.Row);
2288 eclass = ExprClass.Value;
2290 if (left is Constant && right is Constant){
2291 Expression e = ConstantFold.BinaryFold (
2292 ec, oper, (Constant) left, (Constant) right, loc);
2297 return ResolveOperator (ec);
2301 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2302 /// context of a conditional bool expression. This function will return
2303 /// false if it is was possible to use EmitBranchable, or true if it was.
2305 /// The expression's code is generated, and we will generate a branch to 'target'
2306 /// if the resulting expression value is equal to isTrue
2308 public bool EmitBranchable (EmitContext ec, Label target, bool onTrue)
2313 ILGenerator ig = ec.ig;
2316 // This is more complicated than it looks, but its just to avoid
2317 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2318 // but on top of that we want for == and != to use a special path
2319 // if we are comparing against null
2321 if (oper == Operator.Equality || oper == Operator.Inequality){
2322 bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
2324 if (left is NullLiteral){
2327 ig.Emit (OpCodes.Brtrue, target);
2329 ig.Emit (OpCodes.Brfalse, target);
2331 } else if (right is NullLiteral){
2334 ig.Emit (OpCodes.Brtrue, target);
2336 ig.Emit (OpCodes.Brfalse, target);
2339 } else if (!(oper == Operator.LessThan ||
2340 oper == Operator.GreaterThan ||
2341 oper == Operator.LessThanOrEqual ||
2342 oper == Operator.GreaterThanOrEqual))
2350 bool isUnsigned = is_unsigned (left.Type);
2353 case Operator.Equality:
2355 ig.Emit (OpCodes.Beq, target);
2357 ig.Emit (OpCodes.Bne_Un, target);
2360 case Operator.Inequality:
2362 ig.Emit (OpCodes.Bne_Un, target);
2364 ig.Emit (OpCodes.Beq, target);
2367 case Operator.LessThan:
2370 ig.Emit (OpCodes.Blt_Un, target);
2372 ig.Emit (OpCodes.Blt, target);
2375 ig.Emit (OpCodes.Bge_Un, target);
2377 ig.Emit (OpCodes.Bge, target);
2380 case Operator.GreaterThan:
2383 ig.Emit (OpCodes.Bgt_Un, target);
2385 ig.Emit (OpCodes.Bgt, target);
2388 ig.Emit (OpCodes.Ble_Un, target);
2390 ig.Emit (OpCodes.Ble, target);
2393 case Operator.LessThanOrEqual:
2396 ig.Emit (OpCodes.Ble_Un, target);
2398 ig.Emit (OpCodes.Ble, target);
2401 ig.Emit (OpCodes.Bgt_Un, target);
2403 ig.Emit (OpCodes.Bgt, target);
2407 case Operator.GreaterThanOrEqual:
2410 ig.Emit (OpCodes.Bge_Un, target);
2412 ig.Emit (OpCodes.Bge, target);
2415 ig.Emit (OpCodes.Blt_Un, target);
2417 ig.Emit (OpCodes.Blt, target);
2427 public override void Emit (EmitContext ec)
2429 ILGenerator ig = ec.ig;
2431 Type r = right.Type;
2434 if (method != null) {
2436 // Note that operators are static anyway
2438 if (Arguments != null)
2439 Invocation.EmitArguments (ec, method, Arguments);
2441 if (method is MethodInfo)
2442 ig.Emit (OpCodes.Call, (MethodInfo) method);
2444 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2446 if (DelegateOperation)
2447 ig.Emit (OpCodes.Castclass, type);
2453 // Handle short-circuit operators differently
2456 if (oper == Operator.LogicalAnd){
2457 Label load_zero = ig.DefineLabel ();
2458 Label end = ig.DefineLabel ();
2461 ig.Emit (OpCodes.Brfalse, load_zero);
2463 ig.Emit (OpCodes.Br, end);
2464 ig.MarkLabel (load_zero);
2465 ig.Emit (OpCodes.Ldc_I4_0);
2468 } else if (oper == Operator.LogicalOr){
2469 Label load_one = ig.DefineLabel ();
2470 Label end = ig.DefineLabel ();
2473 ig.Emit (OpCodes.Brtrue, load_one);
2475 ig.Emit (OpCodes.Br, end);
2476 ig.MarkLabel (load_one);
2477 ig.Emit (OpCodes.Ldc_I4_1);
2486 case Operator.Multiply:
2488 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2489 opcode = OpCodes.Mul_Ovf;
2490 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2491 opcode = OpCodes.Mul_Ovf_Un;
2493 opcode = OpCodes.Mul;
2495 opcode = OpCodes.Mul;
2499 case Operator.Division:
2500 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2501 opcode = OpCodes.Div_Un;
2503 opcode = OpCodes.Div;
2506 case Operator.Modulus:
2507 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2508 opcode = OpCodes.Rem_Un;
2510 opcode = OpCodes.Rem;
2513 case Operator.Addition:
2515 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2516 opcode = OpCodes.Add_Ovf;
2517 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2518 opcode = OpCodes.Add_Ovf_Un;
2520 opcode = OpCodes.Add;
2522 opcode = OpCodes.Add;
2525 case Operator.Subtraction:
2527 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2528 opcode = OpCodes.Sub_Ovf;
2529 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2530 opcode = OpCodes.Sub_Ovf_Un;
2532 opcode = OpCodes.Sub;
2534 opcode = OpCodes.Sub;
2537 case Operator.RightShift:
2538 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2539 opcode = OpCodes.Shr_Un;
2541 opcode = OpCodes.Shr;
2544 case Operator.LeftShift:
2545 opcode = OpCodes.Shl;
2548 case Operator.Equality:
2549 opcode = OpCodes.Ceq;
2552 case Operator.Inequality:
2553 ec.ig.Emit (OpCodes.Ceq);
2554 ec.ig.Emit (OpCodes.Ldc_I4_0);
2556 opcode = OpCodes.Ceq;
2559 case Operator.LessThan:
2560 opcode = OpCodes.Clt;
2563 case Operator.GreaterThan:
2564 opcode = OpCodes.Cgt;
2567 case Operator.LessThanOrEqual:
2568 ec.ig.Emit (OpCodes.Cgt);
2569 ec.ig.Emit (OpCodes.Ldc_I4_0);
2571 opcode = OpCodes.Ceq;
2574 case Operator.GreaterThanOrEqual:
2575 ec.ig.Emit (OpCodes.Clt);
2576 ec.ig.Emit (OpCodes.Ldc_I4_1);
2578 opcode = OpCodes.Sub;
2581 case Operator.BitwiseOr:
2582 opcode = OpCodes.Or;
2585 case Operator.BitwiseAnd:
2586 opcode = OpCodes.And;
2589 case Operator.ExclusiveOr:
2590 opcode = OpCodes.Xor;
2594 throw new Exception ("This should not happen: Operator = "
2595 + oper.ToString ());
2601 public bool IsBuiltinOperator {
2603 return method == null;
2608 public class PointerArithmetic : Expression {
2609 Expression left, right;
2613 // We assume that 'l' is always a pointer
2615 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t,
2619 eclass = ExprClass.Variable;
2623 is_add = is_addition;
2626 public override Expression DoResolve (EmitContext ec)
2629 // We are born fully resolved
2634 public override void Emit (EmitContext ec)
2636 Type op_type = left.Type;
2637 ILGenerator ig = ec.ig;
2638 int size = GetTypeSize (op_type.GetElementType ());
2640 if (right.Type.IsPointer){
2642 // handle (pointer - pointer)
2646 ig.Emit (OpCodes.Sub);
2650 ig.Emit (OpCodes.Sizeof, op_type);
2652 IntLiteral.EmitInt (ig, size);
2653 ig.Emit (OpCodes.Div);
2655 ig.Emit (OpCodes.Conv_I8);
2658 // handle + and - on (pointer op int)
2661 ig.Emit (OpCodes.Conv_I);
2665 ig.Emit (OpCodes.Sizeof, op_type);
2667 IntLiteral.EmitInt (ig, size);
2668 ig.Emit (OpCodes.Mul);
2671 ig.Emit (OpCodes.Add);
2673 ig.Emit (OpCodes.Sub);
2679 /// Implements the ternary conditional operator (?:)
2681 public class Conditional : Expression {
2682 Expression expr, trueExpr, falseExpr;
2684 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2687 this.trueExpr = trueExpr;
2688 this.falseExpr = falseExpr;
2692 public Expression Expr {
2698 public Expression TrueExpr {
2704 public Expression FalseExpr {
2710 public override Expression DoResolve (EmitContext ec)
2712 expr = expr.Resolve (ec);
2717 if (expr.Type != TypeManager.bool_type)
2718 expr = Expression.ConvertImplicitRequired (
2719 ec, expr, TypeManager.bool_type, loc);
2721 trueExpr = trueExpr.Resolve (ec);
2722 falseExpr = falseExpr.Resolve (ec);
2724 if (trueExpr == null || falseExpr == null)
2727 eclass = ExprClass.Value;
2728 if (trueExpr.Type == falseExpr.Type)
2729 type = trueExpr.Type;
2732 Type true_type = trueExpr.Type;
2733 Type false_type = falseExpr.Type;
2735 if (trueExpr is NullLiteral){
2738 } else if (falseExpr is NullLiteral){
2744 // First, if an implicit conversion exists from trueExpr
2745 // to falseExpr, then the result type is of type falseExpr.Type
2747 conv = ConvertImplicit (ec, trueExpr, false_type, loc);
2750 // Check if both can convert implicitl to each other's type
2752 if (ConvertImplicit (ec, falseExpr, true_type, loc) != null){
2754 "Can not compute type of conditional expression " +
2755 "as '" + TypeManager.MonoBASIC_Name (trueExpr.Type) +
2756 "' and '" + TypeManager.MonoBASIC_Name (falseExpr.Type) +
2757 "' convert implicitly to each other");
2762 } else if ((conv = ConvertImplicit(ec, falseExpr, true_type,loc))!= null){
2766 Error (173, "The type of the conditional expression can " +
2767 "not be computed because there is no implicit conversion" +
2768 " from '" + TypeManager.MonoBASIC_Name (trueExpr.Type) + "'" +
2769 " and '" + TypeManager.MonoBASIC_Name (falseExpr.Type) + "'");
2774 if (expr is BoolConstant){
2775 BoolConstant bc = (BoolConstant) expr;
2786 public override void Emit (EmitContext ec)
2788 ILGenerator ig = ec.ig;
2789 Label false_target = ig.DefineLabel ();
2790 Label end_target = ig.DefineLabel ();
2792 Statement.EmitBoolExpression (ec, expr, false_target, false);
2794 ig.Emit (OpCodes.Br, end_target);
2795 ig.MarkLabel (false_target);
2796 falseExpr.Emit (ec);
2797 ig.MarkLabel (end_target);
2805 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
2806 public readonly string Name;
2807 public readonly Block Block;
2808 VariableInfo variable_info;
2811 public LocalVariableReference (Block block, string name, Location l)
2816 eclass = ExprClass.Variable;
2819 // Setting 'is_readonly' to false will allow you to create a writable
2820 // reference to a read-only variable. This is used by foreach and using.
2821 public LocalVariableReference (Block block, string name, Location l,
2822 VariableInfo variable_info, bool is_readonly)
2823 : this (block, name, l)
2825 this.variable_info = variable_info;
2826 this.is_readonly = is_readonly;
2829 public VariableInfo VariableInfo {
2831 if (variable_info == null) {
2832 variable_info = Block.GetVariableInfo (Name);
2833 is_readonly = variable_info.ReadOnly;
2835 return variable_info;
2839 public bool IsAssigned (EmitContext ec, Location loc)
2841 return VariableInfo.IsAssigned (ec, loc);
2844 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
2846 return VariableInfo.IsFieldAssigned (ec, name, loc);
2849 public void SetAssigned (EmitContext ec)
2851 VariableInfo.SetAssigned (ec);
2854 public void SetFieldAssigned (EmitContext ec, string name)
2856 VariableInfo.SetFieldAssigned (ec, name);
2859 public bool IsReadOnly {
2861 if (variable_info == null) {
2862 variable_info = Block.GetVariableInfo (Name);
2863 is_readonly = variable_info.ReadOnly;
2869 public override Expression DoResolve (EmitContext ec)
2871 VariableInfo vi = VariableInfo;
2873 if (Block.IsConstant (Name)) {
2874 Expression e = Block.GetConstantExpression (Name);
2880 if (ec.DoFlowAnalysis && !IsAssigned (ec, loc))
2883 type = vi.VariableType;
2887 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2889 VariableInfo vi = VariableInfo;
2891 if (ec.DoFlowAnalysis)
2892 ec.SetVariableAssigned (vi);
2894 Expression e = DoResolve (ec);
2900 Error (1604, "cannot assign to '" + Name + "' because it is readonly");
2907 public override void Emit (EmitContext ec)
2909 VariableInfo vi = VariableInfo;
2910 ILGenerator ig = ec.ig;
2912 ig.Emit (OpCodes.Ldloc, vi.LocalBuilder);
2916 public void EmitAssign (EmitContext ec, Expression source)
2918 ILGenerator ig = ec.ig;
2919 VariableInfo vi = VariableInfo;
2925 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
2928 public void AddressOf (EmitContext ec, AddressOp mode)
2930 VariableInfo vi = VariableInfo;
2932 ec.ig.Emit (OpCodes.Ldloca, vi.LocalBuilder);
2937 /// This represents a reference to a parameter in the intermediate
2940 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
2944 public Parameter.Modifier mod;
2945 public bool is_ref, is_out;
2947 public ParameterReference (Parameters pars, int idx, string name, Location loc)
2953 eclass = ExprClass.Variable;
2956 public bool IsAssigned (EmitContext ec, Location loc)
2958 if (!is_out || !ec.DoFlowAnalysis)
2961 if (!ec.CurrentBranching.IsParameterAssigned (idx)) {
2962 Report.Error (165, loc,
2963 "Use of unassigned local variable '" + name + "'");
2970 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
2972 if (!is_out || !ec.DoFlowAnalysis)
2975 if (ec.CurrentBranching.IsParameterAssigned (idx))
2978 if (!ec.CurrentBranching.IsParameterAssigned (idx, field_name)) {
2979 Report.Error (170, loc,
2980 "Use of possibly unassigned field '" + field_name + "'");
2987 public void SetAssigned (EmitContext ec)
2989 if (is_out && ec.DoFlowAnalysis)
2990 ec.CurrentBranching.SetParameterAssigned (idx);
2993 public void SetFieldAssigned (EmitContext ec, string field_name)
2995 if (is_out && ec.DoFlowAnalysis)
2996 ec.CurrentBranching.SetParameterAssigned (idx, field_name);
3000 // Notice that for ref/out parameters, the type exposed is not the
3001 // same type exposed externally.
3004 // externally we expose "int&"
3005 // here we expose "int".
3007 // We record this in "is_ref". This means that the type system can treat
3008 // the type as it is expected, but when we generate the code, we generate
3009 // the alternate kind of code.
3011 public override Expression DoResolve (EmitContext ec)
3013 type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
3014 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3015 is_out = (mod & Parameter.Modifier.OUT) != 0;
3016 eclass = ExprClass.Variable;
3018 if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
3024 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3026 type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
3027 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3028 is_out = (mod & Parameter.Modifier.OUT) != 0;
3029 eclass = ExprClass.Variable;
3031 if (is_out && ec.DoFlowAnalysis)
3032 ec.SetParameterAssigned (idx);
3037 static void EmitLdArg (ILGenerator ig, int x)
3041 case 0: ig.Emit (OpCodes.Ldarg_0); break;
3042 case 1: ig.Emit (OpCodes.Ldarg_1); break;
3043 case 2: ig.Emit (OpCodes.Ldarg_2); break;
3044 case 3: ig.Emit (OpCodes.Ldarg_3); break;
3045 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
3048 ig.Emit (OpCodes.Ldarg, x);
3052 // This method is used by parameters that are references, that are
3053 // being passed as references: we only want to pass the pointer (that
3054 // is already stored in the parameter, not the address of the pointer,
3055 // and not the value of the variable).
3057 public void EmitLoad (EmitContext ec)
3059 ILGenerator ig = ec.ig;
3065 EmitLdArg (ig, arg_idx);
3068 public override void Emit (EmitContext ec)
3070 ILGenerator ig = ec.ig;
3076 EmitLdArg (ig, arg_idx);
3082 // If we are a reference, we loaded on the stack a pointer
3083 // Now lets load the real value
3085 LoadFromPtr (ig, type);
3088 public void EmitAssign (EmitContext ec, Expression source)
3090 ILGenerator ig = ec.ig;
3097 EmitLdArg (ig, arg_idx);
3102 StoreFromPtr (ig, type);
3105 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3107 ig.Emit (OpCodes.Starg, arg_idx);
3111 public void AddressOf (EmitContext ec, AddressOp mode)
3120 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3122 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3125 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3127 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3134 /// Invocation of methods or delegates.
3136 public class Invocation : ExpressionStatement {
3137 public ArrayList Arguments;
3139 public Expression expr;
3140 MethodBase method = null;
3142 bool is_left_hand; // Needed for late bound calls
3143 static Hashtable method_parameter_cache;
3144 static MemberFilter CompareName;
3146 static Invocation ()
3148 method_parameter_cache = new PtrHashtable ();
3152 // arguments is an ArrayList, but we do not want to typecast,
3153 // as it might be null.
3155 // FIXME: only allow expr to be a method invocation or a
3156 // delegate invocation (7.5.5)
3158 public Invocation (Expression expr, ArrayList arguments, Location l)
3161 Arguments = arguments;
3163 CompareName = new MemberFilter (compare_name_filter);
3166 public Expression Expr {
3173 /// Returns the Parameters (a ParameterData interface) for the
3176 public static ParameterData GetParameterData (MethodBase mb)
3178 object pd = method_parameter_cache [mb];
3182 return (ParameterData) pd;
3185 ip = TypeManager.LookupParametersByBuilder (mb);
3187 method_parameter_cache [mb] = ip;
3189 return (ParameterData) ip;
3191 ParameterInfo [] pi = mb.GetParameters ();
3192 ReflectionParameters rp = new ReflectionParameters (pi);
3193 method_parameter_cache [mb] = rp;
3195 return (ParameterData) rp;
3200 /// Determines "better conversion" as specified in 7.4.2.3
3201 /// Returns : 1 if a->p is better
3202 /// 0 if a->q or neither is better
3204 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
3206 Type argument_type = a.Type;
3207 Expression argument_expr = a.Expr;
3209 if (argument_type == null)
3210 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3213 // This is a special case since csc behaves this way. I can't find
3214 // it anywhere in the spec but oh well ...
3216 if (argument_expr is NullLiteral && p == TypeManager.string_type && q == TypeManager.object_type)
3218 else if (argument_expr is NullLiteral && p == TypeManager.object_type && q == TypeManager.string_type)
3224 if (argument_type == p)
3227 if (argument_type == q)
3231 // Now probe whether an implicit constant expression conversion
3234 // An implicit constant expression conversion permits the following
3237 // * A constant-expression of type 'int' can be converted to type
3238 // sbyte, byute, short, ushort, uint, ulong provided the value of
3239 // of the expression is withing the range of the destination type.
3241 // * A constant-expression of type long can be converted to type
3242 // ulong, provided the value of the constant expression is not negative
3244 // FIXME: Note that this assumes that constant folding has
3245 // taken place. We dont do constant folding yet.
3248 if (argument_expr is IntConstant){
3249 IntConstant ei = (IntConstant) argument_expr;
3250 int value = ei.Value;
3252 if (p == TypeManager.sbyte_type){
3253 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3255 } else if (p == TypeManager.byte_type){
3256 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3258 } else if (p == TypeManager.short_type){
3259 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3261 } else if (p == TypeManager.ushort_type){
3262 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3264 } else if (p == TypeManager.uint32_type){
3266 // we can optimize this case: a positive int32
3267 // always fits on a uint32
3271 } else if (p == TypeManager.uint64_type){
3273 // we can optimize this case: a positive int32
3274 // always fits on a uint64
3279 } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
3280 LongConstant lc = (LongConstant) argument_expr;
3282 if (p == TypeManager.uint64_type){
3289 Expression tmp = ConvertImplicit (ec, argument_expr, p, loc);
3297 Expression p_tmp = new EmptyExpression (p);
3298 Expression q_tmp = new EmptyExpression (q);
3300 if (StandardConversionExists (p_tmp, q) == true &&
3301 StandardConversionExists (q_tmp, p) == false)
3304 if (p == TypeManager.sbyte_type)
3305 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3306 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3309 if (p == TypeManager.short_type)
3310 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3311 q == TypeManager.uint64_type)
3314 if (p == TypeManager.int32_type)
3315 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3318 if (p == TypeManager.int64_type)
3319 if (q == TypeManager.uint64_type)
3326 /// Determines "Better function"
3329 /// and returns an integer indicating :
3330 /// 0 if candidate ain't better
3331 /// 1 if candidate is better than the current best match
3333 static int BetterFunction (EmitContext ec, ArrayList args,
3334 MethodBase candidate, MethodBase best,
3335 bool expanded_form, Location loc)
3337 ParameterData candidate_pd = GetParameterData (candidate);
3338 ParameterData best_pd;
3344 argument_count = args.Count;
3346 int cand_count = candidate_pd.Count;
3348 if (cand_count == 0 && argument_count == 0)
3351 if (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS)
3352 if (cand_count != argument_count)
3358 if (argument_count == 0 && cand_count == 1 &&
3359 candidate_pd.ParameterModifier (cand_count - 1) == Parameter.Modifier.PARAMS)
3362 for (int j = argument_count; j > 0;) {
3365 Argument a = (Argument) args [j];
3366 Type t = candidate_pd.ParameterType (j);
3368 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
3370 t = t.GetElementType ();
3372 x = BetterConversion (ec, a, t, null, loc);
3384 best_pd = GetParameterData (best);
3386 int rating1 = 0, rating2 = 0;
3388 for (int j = 0; j < argument_count; ++j) {
3391 Argument a = (Argument) args [j];
3393 Type ct = candidate_pd.ParameterType (j);
3394 Type bt = best_pd.ParameterType (j);
3396 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
3398 ct = ct.GetElementType ();
3400 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
3402 bt = bt.GetElementType ();
3404 x = BetterConversion (ec, a, ct, bt, loc);
3405 y = BetterConversion (ec, a, bt, ct, loc);
3414 if (rating1 > rating2)
3420 public static string FullMethodDesc (MethodBase mb)
3422 string ret_type = "";
3424 if (mb is MethodInfo)
3425 ret_type = TypeManager.MonoBASIC_Name (((MethodInfo) mb).ReturnType) + " ";
3427 StringBuilder sb = new StringBuilder (ret_type + mb.Name);
3428 ParameterData pd = GetParameterData (mb);
3430 int count = pd.Count;
3433 for (int i = count; i > 0; ) {
3436 sb.Append (pd.ParameterDesc (count - i - 1));
3442 return sb.ToString ();
3445 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
3447 MemberInfo [] miset;
3448 MethodGroupExpr union;
3453 return (MethodGroupExpr) mg2;
3456 return (MethodGroupExpr) mg1;
3459 MethodGroupExpr left_set = null, right_set = null;
3460 int length1 = 0, length2 = 0;
3462 left_set = (MethodGroupExpr) mg1;
3463 length1 = left_set.Methods.Length;
3465 right_set = (MethodGroupExpr) mg2;
3466 length2 = right_set.Methods.Length;
3468 ArrayList common = new ArrayList ();
3470 foreach (MethodBase l in left_set.Methods){
3471 foreach (MethodBase r in right_set.Methods){
3479 miset = new MemberInfo [length1 + length2 - common.Count];
3480 left_set.Methods.CopyTo (miset, 0);
3484 foreach (MemberInfo mi in right_set.Methods){
3485 if (!common.Contains (mi))
3489 union = new MethodGroupExpr (miset, loc);
3495 /// Determines is the candidate method, if a params method, is applicable
3496 /// in its expanded form to the given set of arguments
3498 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
3502 if (arguments == null)
3505 arg_count = arguments.Count;
3507 ParameterData pd = GetParameterData (candidate);
3509 int pd_count = pd.Count;
3514 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
3517 if (pd_count - 1 > arg_count)
3520 if (pd_count == 1 && arg_count == 0)
3524 // If we have come this far, the case which remains is when the number of parameters
3525 // is less than or equal to the argument count.
3527 for (int i = 0; i < pd_count - 1; ++i) {
3529 Argument a = (Argument) arguments [i];
3531 Parameter.Modifier a_mod = a.GetParameterModifier () &
3532 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
3533 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
3534 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
3536 if (a_mod == p_mod) {
3538 if (a_mod == Parameter.Modifier.NONE)
3539 if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
3542 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
3543 Type pt = pd.ParameterType (i);
3546 pt = TypeManager.LookupType (pt.FullName + "&");
3556 Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
3558 for (int i = pd_count - 1; i < arg_count; i++) {
3559 Argument a = (Argument) arguments [i];
3561 if (!StandardConversionExists (a.Expr, element_type))
3568 static bool CheckParameterAgainstArgument (EmitContext ec, ParameterData pd, int i, Argument a, Type ptype)
3570 Parameter.Modifier a_mod = a.GetParameterModifier () &
3571 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
3572 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
3573 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
3575 if (a_mod == p_mod || (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
3576 if (a_mod == Parameter.Modifier.NONE)
3577 if (! (ImplicitConversionExists (ec, a.Expr, ptype) || RuntimeConversionExists (ec, a.Expr, ptype)) )
3580 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
3581 Type pt = pd.ParameterType (i);
3584 pt = TypeManager.LookupType (pt.FullName + "&");
3595 /// Determines if the candidate method is applicable (section 14.4.2.1)
3596 /// to the given set of arguments
3598 static bool IsApplicable (EmitContext ec, ref ArrayList arguments, MethodBase candidate)
3600 int arg_count, ps_count, po_count;
3603 if (arguments == null)
3606 arg_count = arguments.Count;
3608 ParameterData pd = GetParameterData (candidate);
3609 Parameters ps = GetFullParameters (candidate);
3617 ps_count = ps.CountStandardParams();
3618 po_count = ps.CountOptionalParams();
3620 int pd_count = pd.Count;
3622 // Validate argument count
3623 if (po_count == 0) {
3624 if (arg_count != pd.Count)
3629 if ((arg_count < ps_count) || (arg_count > pd_count))
3633 if (arg_count > 0) {
3634 for (int i = arg_count; i > 0 ; ) {
3637 Argument a = (Argument) arguments [i];
3638 if (a.ArgType == Argument.AType.NoArg)
3640 Parameter p = (Parameter) ps.FixedParameters[i];
3641 a = new Argument (p.ParameterInitializer, Argument.AType.Expression);
3642 param_type = p.ParameterInitializer.Type;
3646 param_type = pd.ParameterType (i);
3648 Parameter p = (Parameter) ps.FixedParameters[i];
3650 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
3652 a = new Argument (a.Expr, Argument.AType.Ref);
3653 if (!a.Resolve(ec,Location.Null))
3659 if (!CheckParameterAgainstArgument (ec, pd, i, a, param_type))
3665 // If we have no arguments AND the first parameter is optional
3666 // we must check for a candidate (the loop above wouldn't)
3668 ArrayList arglist = new ArrayList();
3670 // Since we got so far, there's no need to check if
3671 // arguments are optional; we simply retrieve
3672 // parameter default values and build a brand-new
3675 for (int i = 0; i < ps.FixedParameters.Length; i++) {
3676 Parameter p = ps.FixedParameters[i];
3677 Argument a = new Argument (p.ParameterInitializer, Argument.AType.Expression);
3678 a.Resolve(ec, Location.Null);
3681 arguments = arglist;
3685 // We've found a candidate, so we exchange the dummy NoArg arguments
3686 // with new arguments containing the default value for that parameter
3687 ArrayList newarglist = new ArrayList();
3688 for (int i = 0; i < arg_count; i++) {
3689 Argument a = (Argument) arguments [i];
3693 p = (Parameter) ps.FixedParameters[i];
3695 if (a.ArgType == Argument.AType.NoArg){
3696 a = new Argument (p.ParameterInitializer, Argument.AType.Expression);
3697 a.Resolve(ec, Location.Null);
3700 if ((p != null) && ((p.ModFlags & Parameter.Modifier.REF) != 0))
3702 a.ArgType = Argument.AType.Ref;
3703 a.Resolve(ec, Location.Null);
3706 int n = pd_count - arg_count;
3709 for (int x = 0; x < n; x++)
3711 Parameter op = (Parameter) ps.FixedParameters[x + arg_count];
3712 Argument b = new Argument (op.ParameterInitializer, Argument.AType.Expression);
3713 b.Resolve(ec, Location.Null);
3718 arguments = newarglist;
3722 static bool compare_name_filter (MemberInfo m, object filterCriteria)
3724 return (m.Name == ((string) filterCriteria));
3727 static Parameters GetFullParameters (MethodBase mb)
3729 TypeContainer tc = TypeManager.LookupTypeContainer (mb.DeclaringType);
3730 InternalParameters ip = TypeManager.LookupParametersByBuilder(mb);
3732 return (ip != null) ? ip.Parameters : null;
3735 // We need an overload for OverloadResolve because Invocation.DoResolve
3736 // must pass Arguments by reference, since a later call to IsApplicable
3737 // can change the argument list if optional parameters are defined
3738 // in the method declaration
3739 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3740 ArrayList Arguments, Location loc)
3742 ArrayList a = Arguments;
3743 return OverloadResolve (ec, me, ref a, loc);
3747 /// Find the Applicable Function Members (7.4.2.1)
3749 /// me: Method Group expression with the members to select.
3750 /// it might contain constructors or methods (or anything
3751 /// that maps to a method).
3753 /// Arguments: ArrayList containing resolved Argument objects.
3755 /// loc: The location if we want an error to be reported, or a Null
3756 /// location for "probing" purposes.
3758 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3759 /// that is the best match of me on Arguments.
3762 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3763 ref ArrayList Arguments, Location loc)
3765 ArrayList afm = new ArrayList ();
3766 MethodBase method = null;
3767 Type current_type = null;
3769 ArrayList candidates = new ArrayList ();
3771 foreach (MethodBase candidate in me.Methods){
3774 // If we're going one level higher in the class hierarchy, abort if
3775 // we already found an applicable method.
3776 if (candidate.DeclaringType != current_type) {
3777 current_type = candidate.DeclaringType;
3782 // Check if candidate is applicable (section 14.4.2.1)
3783 if (!IsApplicable (ec, ref Arguments, candidate))
3786 candidates.Add (candidate);
3787 x = BetterFunction (ec, Arguments, candidate, method, false, loc);
3795 if (Arguments == null)
3798 argument_count = Arguments.Count;
3802 // Now we see if we can find params functions, applicable in their expanded form
3803 // since if they were applicable in their normal form, they would have been selected
3806 bool chose_params_expanded = false;
3808 if (method == null) {
3809 candidates = new ArrayList ();
3810 foreach (MethodBase candidate in me.Methods){
3811 if (!IsParamsMethodApplicable (ec, Arguments, candidate))
3814 candidates.Add (candidate);
3816 int x = BetterFunction (ec, Arguments, candidate, method, true, loc);
3821 chose_params_expanded = true;
3825 if (method == null) {
3827 // Okay so we have failed to find anything so we
3828 // return by providing info about the closest match
3830 for (int i = 0; i < me.Methods.Length; ++i) {
3832 MethodBase c = (MethodBase) me.Methods [i];
3833 ParameterData pd = GetParameterData (c);
3835 if (pd.Count != argument_count)
3838 VerifyArgumentsCompat (ec, Arguments, argument_count, c, false,
3846 // Now check that there are no ambiguities i.e the selected method
3847 // should be better than all the others
3850 foreach (MethodBase candidate in candidates){
3851 if (candidate == method)
3855 // If a normal method is applicable in the sense that it has the same
3856 // number of arguments, then the expanded params method is never applicable
3857 // so we debar the params method.
3859 if (IsParamsMethodApplicable (ec, Arguments, candidate) &&
3860 IsApplicable (ec, ref Arguments, method))
3863 int x = BetterFunction (ec, Arguments, method, candidate,
3864 chose_params_expanded, loc);
3869 "Ambiguous call when selecting function due to implicit casts");
3875 // And now check if the arguments are all compatible, perform conversions
3876 // if necessary etc. and return if everything is all right
3878 if (VerifyArgumentsCompat (ec, Arguments, argument_count, method,
3879 chose_params_expanded, null, loc))
3885 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
3888 bool chose_params_expanded,
3892 return (VerifyArgumentsCompat (ec, Arguments, argument_count,
3893 method, chose_params_expanded, delegate_type, loc, null));
3896 public static bool VerifyArgumentsCompat (EmitContext ec,
3897 ArrayList Arguments,
3900 bool chose_params_expanded,
3903 string InvokingProperty)
3905 ParameterData pd = GetParameterData (method);
3906 int pd_count = pd.Count;
3908 for (int j = 0; j < argument_count; j++) {
3909 Argument a = (Argument) Arguments [j];
3910 Expression a_expr = a.Expr;
3911 Type parameter_type = pd.ParameterType(j);
3913 if (parameter_type == null)
3915 Error_WrongNumArguments(loc, (InvokingProperty == null)?((delegate_type == null)?FullMethodDesc (method):delegate_type.ToString ()):InvokingProperty, argument_count);
3918 if (pd.ParameterModifier (j) == Parameter.Modifier.PARAMS &&
3919 chose_params_expanded)
3920 parameter_type = TypeManager.TypeToCoreType (parameter_type.GetElementType ());
3922 if (a.Type != parameter_type){
3925 conv = ConvertImplicit (ec, a_expr, parameter_type, loc);
3928 if (!Location.IsNull (loc)) {
3929 if (delegate_type == null)
3930 if (InvokingProperty == null)
3931 Report.Error (1502, loc,
3932 "The best overloaded match for method '" +
3933 FullMethodDesc (method) +
3934 "' has some invalid arguments");
3936 Report.Error (1502, loc,
3939 "' has some invalid arguments");
3941 Report.Error (1594, loc,
3942 "Delegate '" + delegate_type.ToString () +
3943 "' has some invalid arguments.");
3944 Report.Error (1503, loc,
3945 "Argument " + (j+1) +
3946 ": Cannot convert from '" + Argument.FullDesc (a)
3947 + "' to '" + pd.ParameterDesc (j) + "'");
3954 // Update the argument with the implicit conversion
3960 Parameter.Modifier a_mod = a.GetParameterModifier () &
3961 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
3962 Parameter.Modifier p_mod = pd.ParameterModifier (j) &
3963 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
3966 if (a_mod != p_mod &&
3967 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
3968 if (!Location.IsNull (loc)) {
3969 Report.Error (1502, loc,
3970 "The best overloaded match for method '" + FullMethodDesc (method)+
3971 "' has some invalid arguments");
3972 Report.Error (1503, loc,
3973 "Argument " + (j+1) +
3974 ": Cannot convert from '" + Argument.FullDesc (a)
3975 + "' to '" + pd.ParameterDesc (j) + "'");
3985 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3987 this.is_left_hand = true;
3988 return DoResolve (ec);
3991 public override Expression DoResolve (EmitContext ec)
3994 // First, resolve the expression that is used to
3995 // trigger the invocation
3997 Expression expr_to_return = null;
3999 if (expr is BaseAccess)
4002 if ((ec.ReturnType != null) && (expr.ToString() == ec.BlockName)) {
4003 ec.InvokingOwnOverload = true;
4004 expr = expr.Resolve (ec, ResolveFlags.MethodGroup);
4005 ec.InvokingOwnOverload = false;
4009 ec.InvokingOwnOverload = false;
4010 expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4015 if (expr is Invocation) {
4016 // FIXME Calls which return an Array are not resolved (here or in the grammar)
4017 expr = expr.Resolve(ec);
4020 if (!(expr is MethodGroupExpr))
4022 Type expr_type = expr.Type;
4024 if (expr_type != null)
4026 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
4028 return (new DelegateInvocation (
4029 this.expr, Arguments, loc)).Resolve (ec);
4034 // Next, evaluate all the expressions in the argument list
4036 if (Arguments != null)
4038 foreach (Argument a in Arguments)
4040 if ((a.ArgType == Argument.AType.NoArg) && (!(expr is MethodGroupExpr)))
4041 Report.Error (999, "This item cannot have empty arguments");
4043 if (!a.Resolve (ec, loc))
4048 if (expr is MethodGroupExpr)
4050 MethodGroupExpr mg = (MethodGroupExpr) expr;
4051 method = OverloadResolve (ec, mg, ref Arguments, loc);
4056 "Could not find any applicable function to invoke for this argument list");
4060 if ((method as MethodInfo) != null)
4062 MethodInfo mi = method as MethodInfo;
4063 type = TypeManager.TypeToCoreType (mi.ReturnType);
4064 if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null))
4065 SimpleName.Error_ObjectRefRequired (ec, loc, mi.Name);
4068 if ((method as ConstructorInfo) != null)
4070 ConstructorInfo ci = method as ConstructorInfo;
4071 type = TypeManager.void_type;
4072 if (!ci.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null))
4073 SimpleName.Error_ObjectRefRequired (ec, loc, ci.Name);
4084 eclass = ExprClass.Value;
4085 expr_to_return = this;
4088 if (expr is PropertyExpr)
4090 PropertyExpr pe = ((PropertyExpr) expr);
4091 pe.PropertyArgs = (ArrayList) Arguments.Clone();
4093 Arguments = new ArrayList();
4094 MethodBase mi = pe.PropertyInfo.GetGetMethod(true);
4096 if(VerifyArgumentsCompat (ec, pe.PropertyArgs,
4097 pe.PropertyArgs.Count, mi, false, null, loc, pe.Name))
4100 expr_to_return = pe.DoResolve (ec);
4101 expr_to_return.eclass = ExprClass.PropertyAccess;
4105 throw new Exception("Error resolving Property Access expression\n" + pe.ToString());
4109 if (expr is FieldExpr || expr is LocalVariableReference || expr is ParameterReference) {
4110 if (expr.Type.IsArray) {
4111 // If we are here, expr must be an ArrayAccess
4112 ArrayList idxs = new ArrayList();
4113 foreach (Argument a in Arguments)
4117 ElementAccess ea = new ElementAccess (expr, idxs, expr.Location);
4118 ArrayAccess aa = new ArrayAccess (ea, expr.Location);
4119 expr_to_return = aa.DoResolve(ec);
4120 expr_to_return.eclass = ExprClass.Variable;
4124 // We can't resolve now, but we
4125 // have to try to access the array with a call
4126 // to LateIndexGet/Set in the runtime
4127 Expression lig_call_expr;
4130 lig_call_expr = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexGet", Location.Null);
4132 lig_call_expr = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexSet", Location.Null);
4133 Expression obj_type = Mono.MonoBASIC.Parser.DecomposeQI("System.Object", Location.Null);
4134 ArrayList adims = new ArrayList();
4136 ArrayList ainit = new ArrayList();
4137 foreach (Argument a in Arguments)
4138 ainit.Add ((Expression) a.Expr);
4140 adims.Add ((Expression) new IntLiteral (Arguments.Count));
4142 Expression oace = new ArrayCreation (obj_type, adims, "", ainit, Location.Null);
4144 ArrayList args = new ArrayList();
4145 args.Add (new Argument(expr, Argument.AType.Expression));
4146 args.Add (new Argument(oace, Argument.AType.Expression));
4147 args.Add (new Argument(NullLiteral.Null, Argument.AType.Expression));
4149 Expression lig_call = new Invocation (lig_call_expr, args, Location.Null);
4150 expr_to_return = lig_call.Resolve(ec);
4151 expr_to_return.eclass = ExprClass.Variable;
4155 return expr_to_return;
4158 static void Error_WrongNumArguments (Location loc, String name, int arg_count)
4160 Report.Error (1501, loc, "No overload for method `" + name + "' takes `" +
4161 arg_count + "' arguments");
4165 // Emits the list of arguments as an array
4167 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
4169 ILGenerator ig = ec.ig;
4170 int count = arguments.Count - idx;
4171 Argument a = (Argument) arguments [idx];
4172 Type t = a.Expr.Type;
4173 string array_type = t.FullName + "[]";
4176 array = ig.DeclareLocal (TypeManager.LookupType (array_type));
4177 IntConstant.EmitInt (ig, count);
4178 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
4179 ig.Emit (OpCodes.Stloc, array);
4181 int top = arguments.Count;
4182 for (int j = idx; j < top; j++){
4183 a = (Argument) arguments [j];
4185 ig.Emit (OpCodes.Ldloc, array);
4186 IntConstant.EmitInt (ig, j - idx);
4189 ArrayAccess.EmitStoreOpcode (ig, t);
4191 ig.Emit (OpCodes.Ldloc, array);
4195 /// Emits a list of resolved Arguments that are in the arguments
4198 /// The MethodBase argument might be null if the
4199 /// emission of the arguments is known not to contain
4200 /// a 'params' field (for example in constructors or other routines
4201 /// that keep their arguments in this structure)
4203 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
4207 pd = GetParameterData (mb);
4212 // If we are calling a params method with no arguments, special case it
4214 if (arguments == null){
4215 if (pd != null && pd.Count > 0 &&
4216 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
4217 ILGenerator ig = ec.ig;
4219 IntConstant.EmitInt (ig, 0);
4220 ig.Emit (OpCodes.Newarr, pd.ParameterType (0).GetElementType ());
4225 int top = arguments.Count;
4227 for (int i = 0; i < top; i++){
4228 Argument a = (Argument) arguments [i];
4231 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
4233 // Special case if we are passing the same data as the
4234 // params argument, do not put it in an array.
4236 if (pd.ParameterType (i) == a.Type)
4239 EmitParams (ec, i, arguments);
4247 if (pd != null && pd.Count > top &&
4248 pd.ParameterModifier (top) == Parameter.Modifier.PARAMS){
4249 ILGenerator ig = ec.ig;
4251 IntConstant.EmitInt (ig, 0);
4252 ig.Emit (OpCodes.Newarr, pd.ParameterType (top).GetElementType ());
4257 /// is_base tells whether we want to force the use of the 'call'
4258 /// opcode instead of using callvirt. Call is required to call
4259 /// a specific method, while callvirt will always use the most
4260 /// recent method in the vtable.
4262 /// is_static tells whether this is an invocation on a static method
4264 /// instance_expr is an expression that represents the instance
4265 /// it must be non-null if is_static is false.
4267 /// method is the method to invoke.
4269 /// Arguments is the list of arguments to pass to the method or constructor.
4271 public static void EmitCall (EmitContext ec, bool is_base,
4272 bool is_static, Expression instance_expr,
4273 MethodBase method, ArrayList Arguments, Location loc)
4275 EmitCall (ec, is_base, is_static, instance_expr, method, Arguments, null, loc);
4278 public static void EmitCall (EmitContext ec, bool is_base,
4279 bool is_static, Expression instance_expr,
4280 MethodBase method, ArrayList Arguments, ArrayList prop_args, Location loc)
4282 ILGenerator ig = ec.ig;
4283 bool struct_call = false;
4285 Type decl_type = method.DeclaringType;
4287 if (!RootContext.StdLib)
4289 // Replace any calls to the system's System.Array type with calls to
4290 // the newly created one.
4291 if (method == TypeManager.system_int_array_get_length)
4292 method = TypeManager.int_array_get_length;
4293 else if (method == TypeManager.system_int_array_get_rank)
4294 method = TypeManager.int_array_get_rank;
4295 else if (method == TypeManager.system_object_array_clone)
4296 method = TypeManager.object_array_clone;
4297 else if (method == TypeManager.system_int_array_get_length_int)
4298 method = TypeManager.int_array_get_length_int;
4299 else if (method == TypeManager.system_int_array_get_lower_bound_int)
4300 method = TypeManager.int_array_get_lower_bound_int;
4301 else if (method == TypeManager.system_int_array_get_upper_bound_int)
4302 method = TypeManager.int_array_get_upper_bound_int;
4303 else if (method == TypeManager.system_void_array_copyto_array_int)
4304 method = TypeManager.void_array_copyto_array_int;
4308 // This checks the 'ConditionalAttribute' on the method, and the
4309 // ObsoleteAttribute
4311 TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (method, loc);
4312 if ((flags & TypeManager.MethodFlags.IsObsoleteError) != 0)
4314 if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
4319 if (decl_type.IsValueType)
4322 // If this is ourselves, push "this"
4324 if (instance_expr == null)
4326 ig.Emit (OpCodes.Ldarg_0);
4331 // Push the instance expression
4333 if (instance_expr.Type.IsValueType)
4336 // Special case: calls to a function declared in a
4337 // reference-type with a value-type argument need
4338 // to have their value boxed.
4341 if (decl_type.IsValueType)
4344 // If the expression implements IMemoryLocation, then
4345 // we can optimize and use AddressOf on the
4348 // If not we have to use some temporary storage for
4350 if (instance_expr is IMemoryLocation)
4352 ((IMemoryLocation)instance_expr).
4353 AddressOf (ec, AddressOp.LoadStore);
4357 Type t = instance_expr.Type;
4359 instance_expr.Emit (ec);
4360 LocalBuilder temp = ig.DeclareLocal (t);
4361 ig.Emit (OpCodes.Stloc, temp);
4362 ig.Emit (OpCodes.Ldloca, temp);
4367 instance_expr.Emit (ec);
4368 ig.Emit (OpCodes.Box, instance_expr.Type);
4372 instance_expr.Emit (ec);
4376 if (prop_args != null && prop_args.Count > 0)
4378 if (Arguments == null)
4379 Arguments = new ArrayList();
4381 for (int i = prop_args.Count-1; i >=0 ; i--)
4383 Arguments.Insert (0,prop_args[i]);
4388 EmitArguments (ec, method, Arguments);
4390 if (is_static || struct_call || is_base)
4392 if (method is MethodInfo)
4394 ig.Emit (OpCodes.Call, (MethodInfo) method);
4397 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4401 if (method is MethodInfo)
4402 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
4404 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
4408 static void EmitPropertyArgs (EmitContext ec, ArrayList prop_args)
4410 int top = prop_args.Count;
4412 for (int i = 0; i < top; i++)
4414 Argument a = (Argument) prop_args [i];
4419 public override void Emit (EmitContext ec)
4421 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
4424 ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
4427 public override void EmitStatement (EmitContext ec)
4432 // Pop the return value if there is one
4434 if (method is MethodInfo){
4435 Type ret = ((MethodInfo)method).ReturnType;
4436 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
4437 ec.ig.Emit (OpCodes.Pop);
4443 // This class is used to "disable" the code generation for the
4444 // temporary variable when initializing value types.
4446 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
4447 public void AddressOf (EmitContext ec, AddressOp Mode)
4454 /// Implements the new expression
4456 public class New : ExpressionStatement {
4457 public readonly ArrayList Arguments;
4458 public readonly Expression RequestedType;
4460 MethodBase method = null;
4463 // If set, the new expression is for a value_target, and
4464 // we will not leave anything on the stack.
4466 Expression value_target;
4467 bool value_target_set = false;
4469 public New (Expression requested_type, ArrayList arguments, Location l)
4471 RequestedType = requested_type;
4472 Arguments = arguments;
4476 public Expression ValueTypeVariable {
4478 return value_target;
4482 value_target = value;
4483 value_target_set = true;
4488 // This function is used to disable the following code sequence for
4489 // value type initialization:
4491 // AddressOf (temporary)
4495 // Instead the provide will have provided us with the address on the
4496 // stack to store the results.
4498 static Expression MyEmptyExpression;
4500 public void DisableTemporaryValueType ()
4502 if (MyEmptyExpression == null)
4503 MyEmptyExpression = new EmptyAddressOf ();
4506 // To enable this, look into:
4507 // test-34 and test-89 and self bootstrapping.
4509 // For instance, we can avoid a copy by using 'newobj'
4510 // instead of Call + Push-temp on value types.
4511 // value_target = MyEmptyExpression;
4514 public override Expression DoResolve (EmitContext ec)
4516 type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
4521 bool IsDelegate = TypeManager.IsDelegateType (type);
4524 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
4526 if (type.IsInterface || type.IsAbstract){
4528 30376, "It is not possible to create instances of Interfaces " +
4529 "or classes marked as MustInherit");
4533 bool is_struct = false;
4534 is_struct = type.IsValueType;
4535 eclass = ExprClass.Value;
4538 // SRE returns a match for .ctor () on structs (the object constructor),
4539 // so we have to manually ignore it.
4541 if (is_struct && Arguments == null)
4545 ml = MemberLookupFinal (ec, type, ".ctor",
4546 MemberTypes.Constructor,
4547 AllBindingFlags | BindingFlags.Public, loc);
4552 if (! (ml is MethodGroupExpr)){
4554 ml.Error118 ("method group");
4560 if (Arguments != null){
4561 foreach (Argument a in Arguments){
4562 if (!a.Resolve (ec, loc))
4567 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4572 if (method == null) {
4573 if (!is_struct || Arguments.Count > 0) {
4575 "New invocation: Can not find a constructor for " +
4576 "this argument list");
4584 // This DoEmit can be invoked in two contexts:
4585 // * As a mechanism that will leave a value on the stack (new object)
4586 // * As one that wont (init struct)
4588 // You can control whether a value is required on the stack by passing
4589 // need_value_on_stack. The code *might* leave a value on the stack
4590 // so it must be popped manually
4592 // If we are dealing with a ValueType, we have a few
4593 // situations to deal with:
4595 // * The target is a ValueType, and we have been provided
4596 // the instance (this is easy, we are being assigned).
4598 // * The target of New is being passed as an argument,
4599 // to a boxing operation or a function that takes a
4602 // In this case, we need to create a temporary variable
4603 // that is the argument of New.
4605 // Returns whether a value is left on the stack
4607 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4609 bool is_value_type = type.IsValueType;
4610 ILGenerator ig = ec.ig;
4615 // Allow DoEmit() to be called multiple times.
4616 // We need to create a new LocalTemporary each time since
4617 // you can't share LocalBuilders among ILGeneators.
4618 if (!value_target_set)
4619 value_target = new LocalTemporary (ec, type);
4621 ml = (IMemoryLocation) value_target;
4622 ml.AddressOf (ec, AddressOp.Store);
4626 Invocation.EmitArguments (ec, method, Arguments);
4630 ig.Emit (OpCodes.Initobj, type);
4632 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4633 if (need_value_on_stack){
4634 value_target.Emit (ec);
4639 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4644 public override void Emit (EmitContext ec)
4649 public override void EmitStatement (EmitContext ec)
4651 if (DoEmit (ec, false))
4652 ec.ig.Emit (OpCodes.Pop);
4657 /// 14.5.10.2: Represents an array creation expression.
4661 /// There are two possible scenarios here: one is an array creation
4662 /// expression that specifies the dimensions and optionally the
4663 /// initialization data and the other which does not need dimensions
4664 /// specified but where initialization data is mandatory.
4666 public class ArrayCreation : ExpressionStatement {
4667 Expression requested_base_type;
4668 ArrayList initializers;
4671 // The list of Argument types.
4672 // This is used to construct the 'newarray' or constructor signature
4674 ArrayList arguments;
4677 // Method used to create the array object.
4679 MethodBase new_method = null;
4681 Type array_element_type;
4682 Type underlying_type;
4683 bool is_one_dimensional = false;
4684 bool is_builtin_type = false;
4685 bool expect_initializers = false;
4686 int num_arguments = 0;
4690 ArrayList array_data;
4695 // The number of array initializers that we can handle
4696 // via the InitializeArray method - through EmitStaticInitializers
4698 int num_automatic_initializers;
4700 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
4702 this.requested_base_type = requested_base_type;
4703 this.initializers = initializers;
4707 arguments = new ArrayList ();
4709 foreach (Expression e in exprs) {
4710 arguments.Add (new Argument (e, Argument.AType.Expression));
4715 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
4717 this.requested_base_type = requested_base_type;
4718 this.initializers = initializers;
4722 //this.rank = rank.Substring (0, rank.LastIndexOf ("["));
4724 //string tmp = rank.Substring (rank.LastIndexOf ("["));
4726 //dimensions = tmp.Length - 1;
4727 expect_initializers = true;
4730 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
4732 StringBuilder sb = new StringBuilder (rank);
4735 for (int i = 1; i < idx_count; i++)
4740 return new ComposedCast (base_type, sb.ToString (), loc);
4743 void Error_IncorrectArrayInitializer ()
4745 Error (30567, "Incorrectly structured array initializer");
4748 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
4750 if (specified_dims) {
4751 Argument a = (Argument) arguments [idx];
4753 if (!a.Resolve (ec, loc))
4756 if (!(a.Expr is Constant)) {
4757 Error (150, "A constant value is expected");
4761 int value = (int) ((Constant) a.Expr).GetValue ();
4763 if (value != probe.Count) {
4764 Error_IncorrectArrayInitializer ();
4768 bounds [idx] = value;
4771 int child_bounds = -1;
4772 foreach (object o in probe) {
4773 if (o is ArrayList) {
4774 int current_bounds = ((ArrayList) o).Count;
4776 if (child_bounds == -1)
4777 child_bounds = current_bounds;
4779 else if (child_bounds != current_bounds){
4780 Error_IncorrectArrayInitializer ();
4783 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
4787 if (child_bounds != -1){
4788 Error_IncorrectArrayInitializer ();
4792 Expression tmp = (Expression) o;
4793 tmp = tmp.Resolve (ec);
4797 // Console.WriteLine ("I got: " + tmp);
4798 // Handle initialization from vars, fields etc.
4800 Expression conv = ConvertImplicitRequired (
4801 ec, tmp, underlying_type, loc);
4806 if (conv is StringConstant)
4807 array_data.Add (conv);
4808 else if (conv is Constant) {
4809 array_data.Add (conv);
4810 num_automatic_initializers++;
4812 array_data.Add (conv);
4819 public void UpdateIndices (EmitContext ec)
4822 for (ArrayList probe = initializers; probe != null;) {
4823 if (probe.Count > 0 && probe [0] is ArrayList) {
4824 Expression e = new IntConstant (probe.Count);
4825 arguments.Add (new Argument (e, Argument.AType.Expression));
4827 bounds [i++] = probe.Count;
4829 probe = (ArrayList) probe [0];
4832 Expression e = new IntConstant (probe.Count);
4833 arguments.Add (new Argument (e, Argument.AType.Expression));
4835 bounds [i++] = probe.Count;
4842 public bool ValidateInitializers (EmitContext ec, Type array_type)
4844 if (initializers == null) {
4845 if (expect_initializers)
4851 if (underlying_type == null)
4855 // We use this to store all the date values in the order in which we
4856 // will need to store them in the byte blob later
4858 array_data = new ArrayList ();
4859 bounds = new Hashtable ();
4863 if (arguments != null) {
4864 ret = CheckIndices (ec, initializers, 0, true);
4867 arguments = new ArrayList ();
4869 ret = CheckIndices (ec, initializers, 0, false);
4876 if (arguments.Count != dimensions) {
4877 Error_IncorrectArrayInitializer ();
4885 void Error_NegativeArrayIndex ()
4887 Error (284, "Can not create array with a negative size");
4891 // Converts 'source' to an int, uint, long or ulong.
4893 Expression ExpressionToArrayArgument (EmitContext ec, Expression source)
4897 bool old_checked = ec.CheckState;
4898 ec.CheckState = true;
4900 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
4901 if (target == null){
4902 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
4903 if (target == null){
4904 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
4905 if (target == null){
4906 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
4908 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
4912 ec.CheckState = old_checked;
4915 // Only positive constants are allowed at compile time
4917 if (target is Constant){
4918 if (target is IntConstant){
4919 if (((IntConstant) target).Value < 0){
4920 Error_NegativeArrayIndex ();
4925 if (target is LongConstant){
4926 if (((LongConstant) target).Value < 0){
4927 Error_NegativeArrayIndex ();
4938 // Creates the type of the array
4940 bool LookupType (EmitContext ec)
4942 StringBuilder array_qualifier = new StringBuilder (rank);
4945 // 'In the first form allocates an array instace of the type that results
4946 // from deleting each of the individual expression from the expression list'
4948 if (num_arguments > 0) {
4949 array_qualifier.Append ("[");
4950 for (int i = num_arguments-1; i > 0; i--)
4951 array_qualifier.Append (",");
4952 array_qualifier.Append ("]");
4958 Expression array_type_expr;
4959 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
4960 string sss = array_qualifier.ToString ();
4961 type = ec.DeclSpace.ResolveType (array_type_expr, false, loc);
4966 underlying_type = type;
4967 if (underlying_type.IsArray)
4968 underlying_type = TypeManager.TypeToCoreType (underlying_type.GetElementType ());
4969 dimensions = type.GetArrayRank ();
4974 public override Expression DoResolve (EmitContext ec)
4978 if (!LookupType (ec))
4982 // First step is to validate the initializers and fill
4983 // in any missing bits
4985 if (!ValidateInitializers (ec, type))
4988 if (arguments == null)
4991 arg_count = arguments.Count;
4992 foreach (Argument a in arguments){
4993 if (!a.Resolve (ec, loc))
4996 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
4997 if (real_arg == null)
5004 array_element_type = TypeManager.TypeToCoreType (type.GetElementType ());
5006 if (arg_count == 1) {
5007 is_one_dimensional = true;
5008 eclass = ExprClass.Value;
5012 is_builtin_type = TypeManager.IsBuiltinType (type);
5014 if (is_builtin_type) {
5017 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
5018 AllBindingFlags, loc);
5020 if (!(ml is MethodGroupExpr)) {
5021 ml.Error118 ("method group");
5026 Error (-6, "New invocation: Can not find a constructor for " +
5027 "this argument list");
5031 new_method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, arguments, loc);
5033 if (new_method == null) {
5034 Error (-6, "New invocation: Can not find a constructor for " +
5035 "this argument list");
5039 eclass = ExprClass.Value;
5042 ModuleBuilder mb = CodeGen.ModuleBuilder;
5043 ArrayList args = new ArrayList ();
5045 if (arguments != null) {
5046 for (int i = 0; i < arg_count; i++)
5047 args.Add (TypeManager.int32_type);
5050 Type [] arg_types = null;
5053 arg_types = new Type [args.Count];
5055 args.CopyTo (arg_types, 0);
5057 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
5060 if (new_method == null) {
5061 Error (-6, "New invocation: Can not find a constructor for " +
5062 "this argument list");
5066 eclass = ExprClass.Value;
5071 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
5076 int count = array_data.Count;
5078 if (underlying_type.IsEnum)
5079 underlying_type = TypeManager.EnumToUnderlying (underlying_type);
5081 factor = GetTypeSize (underlying_type);
5083 throw new Exception ("unrecognized type in MakeByteBlob: " + underlying_type);
5085 data = new byte [(count * factor + 4) & ~3];
5088 for (int i = 0; i < count; ++i) {
5089 object v = array_data [i];
5091 if (v is EnumConstant)
5092 v = ((EnumConstant) v).Child;
5094 if (v is Constant && !(v is StringConstant))
5095 v = ((Constant) v).GetValue ();
5101 if (underlying_type == TypeManager.int64_type){
5102 if (!(v is Expression)){
5103 long val = (long) v;
5105 for (int j = 0; j < factor; ++j) {
5106 data [idx + j] = (byte) (val & 0xFF);
5110 } else if (underlying_type == TypeManager.uint64_type){
5111 if (!(v is Expression)){
5112 ulong val = (ulong) v;
5114 for (int j = 0; j < factor; ++j) {
5115 data [idx + j] = (byte) (val & 0xFF);
5119 } else if (underlying_type == TypeManager.float_type) {
5120 if (!(v is Expression)){
5121 element = BitConverter.GetBytes ((float) v);
5123 for (int j = 0; j < factor; ++j)
5124 data [idx + j] = element [j];
5126 } else if (underlying_type == TypeManager.double_type) {
5127 if (!(v is Expression)){
5128 element = BitConverter.GetBytes ((double) v);
5130 for (int j = 0; j < factor; ++j)
5131 data [idx + j] = element [j];
5133 } else if (underlying_type == TypeManager.char_type){
5134 if (!(v is Expression)){
5135 int val = (int) ((char) v);
5137 data [idx] = (byte) (val & 0xff);
5138 data [idx+1] = (byte) (val >> 8);
5140 } else if (underlying_type == TypeManager.short_type){
5141 if (!(v is Expression)){
5142 int val = (int) ((short) v);
5144 data [idx] = (byte) (val & 0xff);
5145 data [idx+1] = (byte) (val >> 8);
5147 } else if (underlying_type == TypeManager.ushort_type){
5148 if (!(v is Expression)){
5149 int val = (int) ((ushort) v);
5151 data [idx] = (byte) (val & 0xff);
5152 data [idx+1] = (byte) (val >> 8);
5154 } else if (underlying_type == TypeManager.int32_type) {
5155 if (!(v is Expression)){
5158 data [idx] = (byte) (val & 0xff);
5159 data [idx+1] = (byte) ((val >> 8) & 0xff);
5160 data [idx+2] = (byte) ((val >> 16) & 0xff);
5161 data [idx+3] = (byte) (val >> 24);
5163 } else if (underlying_type == TypeManager.uint32_type) {
5164 if (!(v is Expression)){
5165 uint val = (uint) v;
5167 data [idx] = (byte) (val & 0xff);
5168 data [idx+1] = (byte) ((val >> 8) & 0xff);
5169 data [idx+2] = (byte) ((val >> 16) & 0xff);
5170 data [idx+3] = (byte) (val >> 24);
5172 } else if (underlying_type == TypeManager.sbyte_type) {
5173 if (!(v is Expression)){
5174 sbyte val = (sbyte) v;
5175 data [idx] = (byte) val;
5177 } else if (underlying_type == TypeManager.byte_type) {
5178 if (!(v is Expression)){
5179 byte val = (byte) v;
5180 data [idx] = (byte) val;
5182 } else if (underlying_type == TypeManager.bool_type) {
5183 if (!(v is Expression)){
5184 bool val = (bool) v;
5185 data [idx] = (byte) (val ? 1 : 0);
5187 } else if (underlying_type == TypeManager.decimal_type){
5188 if (!(v is Expression)){
5189 int [] bits = Decimal.GetBits ((decimal) v);
5192 for (int j = 0; j < 4; j++){
5193 data [p++] = (byte) (bits [j] & 0xff);
5194 data [p++] = (byte) ((bits [j] >> 8) & 0xff);
5195 data [p++] = (byte) ((bits [j] >> 16) & 0xff);
5196 data [p++] = (byte) (bits [j] >> 24);
5200 throw new Exception ("Unrecognized type in MakeByteBlob: " + underlying_type);
5209 // Emits the initializers for the array
5211 void EmitStaticInitializers (EmitContext ec, bool is_expression)
5214 // First, the static data
5217 ILGenerator ig = ec.ig;
5219 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
5221 fb = RootContext.MakeStaticData (data);
5224 ig.Emit (OpCodes.Dup);
5225 ig.Emit (OpCodes.Ldtoken, fb);
5226 ig.Emit (OpCodes.Call,
5227 TypeManager.void_initializearray_array_fieldhandle);
5231 // Emits pieces of the array that can not be computed at compile
5232 // time (variables and string locations).
5234 // This always expect the top value on the stack to be the array
5236 void EmitDynamicInitializers (EmitContext ec, bool is_expression)
5238 ILGenerator ig = ec.ig;
5239 int dims = bounds.Count;
5240 int [] current_pos = new int [dims];
5241 int top = array_data.Count;
5242 LocalBuilder temp = ig.DeclareLocal (type);
5244 ig.Emit (OpCodes.Stloc, temp);
5246 MethodInfo set = null;
5250 ModuleBuilder mb = null;
5251 mb = CodeGen.ModuleBuilder;
5252 args = new Type [dims + 1];
5255 for (j = 0; j < dims; j++)
5256 args [j] = TypeManager.int32_type;
5258 args [j] = array_element_type;
5260 set = mb.GetArrayMethod (
5262 CallingConventions.HasThis | CallingConventions.Standard,
5263 TypeManager.void_type, args);
5266 for (int i = 0; i < top; i++){
5268 Expression e = null;
5270 if (array_data [i] is Expression)
5271 e = (Expression) array_data [i];
5275 // Basically we do this for string literals and
5276 // other non-literal expressions
5278 if (e is StringConstant || !(e is Constant) ||
5279 num_automatic_initializers <= 2) {
5280 Type etype = e.Type;
5282 ig.Emit (OpCodes.Ldloc, temp);
5284 for (int idx = 0; idx < dims; idx++)
5285 IntConstant.EmitInt (ig, current_pos [idx]);
5288 // If we are dealing with a struct, get the
5289 // address of it, so we can store it.
5292 etype.IsSubclassOf (TypeManager.value_type) &&
5293 (!TypeManager.IsBuiltinType (etype) ||
5294 etype == TypeManager.decimal_type)) {
5299 // Let new know that we are providing
5300 // the address where to store the results
5302 n.DisableTemporaryValueType ();
5305 ig.Emit (OpCodes.Ldelema, etype);
5311 ArrayAccess.EmitStoreOpcode (ig, array_element_type);
5313 ig.Emit (OpCodes.Call, set);
5320 for (int j = dims - 1; j >= 0; j--){
5322 if (current_pos [j] < (int) bounds [j])
5324 current_pos [j] = 0;
5329 ig.Emit (OpCodes.Ldloc, temp);
5332 void EmitArrayArguments (EmitContext ec)
5334 ILGenerator ig = ec.ig;
5336 foreach (Argument a in arguments) {
5337 Type atype = a.Type;
5340 if (atype == TypeManager.uint64_type)
5341 ig.Emit (OpCodes.Conv_Ovf_U4);
5342 else if (atype == TypeManager.int64_type)
5343 ig.Emit (OpCodes.Conv_Ovf_I4);
5347 void DoEmit (EmitContext ec, bool is_statement)
5349 ILGenerator ig = ec.ig;
5351 EmitArrayArguments (ec);
5352 if (is_one_dimensional)
5353 ig.Emit (OpCodes.Newarr, array_element_type);
5355 if (is_builtin_type)
5356 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
5358 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
5361 if (initializers != null){
5363 // FIXME: Set this variable correctly.
5365 bool dynamic_initializers = true;
5367 if (underlying_type != TypeManager.string_type &&
5368 underlying_type != TypeManager.object_type) {
5369 if (num_automatic_initializers > 2)
5370 EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
5373 if (dynamic_initializers)
5374 EmitDynamicInitializers (ec, !is_statement);
5378 public override void Emit (EmitContext ec)
5383 public override void EmitStatement (EmitContext ec)
5391 /// Represents the 'this' construct
5393 public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
5398 public This (Block block, Location loc)
5404 public This (Location loc)
5409 public bool IsAssigned (EmitContext ec, Location loc)
5414 return vi.IsAssigned (ec, loc);
5417 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
5422 return vi.IsFieldAssigned (ec, field_name, loc);
5425 public void SetAssigned (EmitContext ec)
5428 vi.SetAssigned (ec);
5431 public void SetFieldAssigned (EmitContext ec, string field_name)
5434 vi.SetFieldAssigned (ec, field_name);
5437 public override Expression DoResolve (EmitContext ec)
5439 eclass = ExprClass.Variable;
5440 type = ec.ContainerType;
5443 Error (26, "Keyword this not valid in static code");
5448 vi = block.ThisVariable;
5453 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
5457 VariableInfo vi = ec.CurrentBlock.ThisVariable;
5459 vi.SetAssigned (ec);
5461 if (ec.TypeContainer is Class){
5462 Error (1604, "Cannot assign to 'this'");
5469 public override void Emit (EmitContext ec)
5471 ILGenerator ig = ec.ig;
5473 ig.Emit (OpCodes.Ldarg_0);
5474 if (ec.TypeContainer is Struct)
5475 ig.Emit (OpCodes.Ldobj, type);
5478 public void EmitAssign (EmitContext ec, Expression source)
5480 ILGenerator ig = ec.ig;
5482 if (ec.TypeContainer is Struct){
5483 ig.Emit (OpCodes.Ldarg_0);
5485 ig.Emit (OpCodes.Stobj, type);
5488 ig.Emit (OpCodes.Starg, 0);
5492 public void AddressOf (EmitContext ec, AddressOp mode)
5494 ec.ig.Emit (OpCodes.Ldarg_0);
5497 // FIGURE OUT WHY LDARG_S does not work
5499 // consider: struct X { int val; int P { set { val = value; }}}
5501 // Yes, this looks very bad. Look at 'NOTAS' for
5503 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
5508 /// Implements the typeof operator
5510 public class TypeOf : Expression {
5511 public readonly Expression QueriedType;
5514 public TypeOf (Expression queried_type, Location l)
5516 QueriedType = queried_type;
5520 public override Expression DoResolve (EmitContext ec)
5522 typearg = ec.DeclSpace.ResolveType (QueriedType, false, loc);
5524 if (typearg == null)
5527 type = TypeManager.type_type;
5528 eclass = ExprClass.Type;
5532 public override void Emit (EmitContext ec)
5534 ec.ig.Emit (OpCodes.Ldtoken, typearg);
5535 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
5538 public Type TypeArg {
5539 get { return typearg; }
5544 /// Implements the sizeof expression
5546 public class SizeOf : Expression {
5547 public readonly Expression QueriedType;
5550 public SizeOf (Expression queried_type, Location l)
5552 this.QueriedType = queried_type;
5556 public override Expression DoResolve (EmitContext ec)
5559 Error (233, "Sizeof may only be used in an unsafe context " +
5560 "(consider using System.Runtime.InteropServices.Marshal.Sizeof");
5564 type_queried = ec.DeclSpace.ResolveType (QueriedType, false, loc);
5565 if (type_queried == null)
5568 if (!TypeManager.IsUnmanagedType (type_queried)){
5569 Report.Error (208, "Cannot take the size of an unmanaged type (" + TypeManager.MonoBASIC_Name (type_queried) + ")");
5573 type = TypeManager.int32_type;
5574 eclass = ExprClass.Value;
5578 public override void Emit (EmitContext ec)
5580 int size = GetTypeSize (type_queried);
5583 ec.ig.Emit (OpCodes.Sizeof, type_queried);
5585 IntConstant.EmitInt (ec.ig, size);
5590 /// Implements the member access expression
5592 public class MemberAccess : Expression, ITypeExpression {
5593 public readonly string Identifier;
5595 Expression member_lookup;
5597 public MemberAccess (Expression expr, string id, Location l)
5604 public Expression Expr {
5610 static void error176 (Location loc, string name)
5612 Report.Error (176, loc, "Static member '" +
5613 name + "' cannot be accessed " +
5614 "with an instance reference, qualify with a " +
5615 "type name instead");
5618 static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
5620 if (left_original == null)
5623 if (!(left_original is SimpleName))
5626 SimpleName sn = (SimpleName) left_original;
5628 Type t = RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc);
5635 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
5636 Expression left, Location loc,
5637 Expression left_original)
5639 bool left_is_type, left_is_explicit;
5641 // If 'left' is null, then we're called from SimpleNameResolve and this is
5642 // a member in the currently defining class.
5644 left_is_type = ec.IsStatic || ec.IsFieldInitializer;
5645 left_is_explicit = false;
5647 // Implicitly default to 'this' unless we're static.
5648 if (!ec.IsStatic && !ec.IsFieldInitializer && !ec.InEnumContext)
5651 left_is_type = left is TypeExpr;
5652 left_is_explicit = true;
5655 if (member_lookup is FieldExpr){
5656 FieldExpr fe = (FieldExpr) member_lookup;
5657 FieldInfo fi = fe.FieldInfo;
5658 Type decl_type = fi.DeclaringType;
5660 if (fi is FieldBuilder) {
5661 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
5664 object o = c.LookupConstantValue (ec);
5665 object real_value = ((Constant) c.Expr).GetValue ();
5667 return Constantify (real_value, fi.FieldType);
5672 Type t = fi.FieldType;
5676 if (fi is FieldBuilder)
5677 o = TypeManager.GetValue ((FieldBuilder) fi);
5679 o = fi.GetValue (fi);
5681 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
5682 if (left_is_explicit && !left_is_type &&
5683 !IdenticalNameAndTypeName (ec, left_original, loc)) {
5684 error176 (loc, fe.FieldInfo.Name);
5688 Expression enum_member = MemberLookup (
5689 ec, decl_type, "value__", MemberTypes.Field,
5690 AllBindingFlags, loc);
5692 Enum en = TypeManager.LookupEnum (decl_type);
5696 c = Constantify (o, en.UnderlyingType);
5697 return new EnumConstant (c, en.UnderlyingType);
5700 c = Constantify (o, enum_member.Type);
5701 return new EnumConstant (c, enum_member.Type);
5707 Expression exp = Constantify (o, t);
5709 if (left_is_explicit && !left_is_type) {
5710 error176 (loc, fe.FieldInfo.Name);
5717 if (fi.FieldType.IsPointer && !ec.InUnsafe){
5723 if (member_lookup is EventExpr) {
5725 EventExpr ee = (EventExpr) member_lookup;
5728 // If the event is local to this class, we transform ourselves into
5732 if (ee.EventInfo.DeclaringType == ec.ContainerType) {
5733 MemberInfo mi = GetFieldFromEvent (ee);
5737 // If this happens, then we have an event with its own
5738 // accessors and private field etc so there's no need
5739 // to transform ourselves : we should instead flag an error
5741 Assign.error70 (ee.EventInfo, loc);
5745 Expression ml = ExprClassFromMemberInfo (ec, mi, loc);
5748 Report.Error (-200, loc, "Internal error!!");
5752 return ResolveMemberAccess (ec, ml, left, loc, left_original);
5756 if (member_lookup is IMemberExpr) {
5757 IMemberExpr me = (IMemberExpr) member_lookup;
5760 MethodGroupExpr mg = me as MethodGroupExpr;
5761 if ((mg != null) && left_is_explicit && left.Type.IsInterface)
5762 mg.IsExplicitImpl = left_is_explicit;
5765 if (IdenticalNameAndTypeName (ec, left_original, loc))
5766 return member_lookup;
5768 SimpleName.Error_ObjectRefRequired (ec, loc, me.Name);
5773 if (!me.IsInstance){
5774 if (IdenticalNameAndTypeName (ec, left_original, loc))
5775 return member_lookup;
5777 /*if (left_is_explicit) {
5778 error176 (loc, me.Name);
5784 // Since we can not check for instance objects in SimpleName,
5785 // becaue of the rule that allows types and variables to share
5786 // the name (as long as they can be de-ambiguated later, see
5787 // IdenticalNameAndTypeName), we have to check whether left
5788 // is an instance variable in a static context
5790 // However, if the left-hand value is explicitly given, then
5791 // it is already our instance expression, so we aren't in
5795 if (ec.IsStatic && !left_is_explicit && left is IMemberExpr){
5796 IMemberExpr mexp = (IMemberExpr) left;
5798 if (!mexp.IsStatic){
5799 SimpleName.Error_ObjectRefRequired (ec, loc, mexp.Name);
5804 me.InstanceExpression = left;
5807 return member_lookup;
5810 if (member_lookup is TypeExpr){
5811 member_lookup.Resolve (ec, ResolveFlags.Type);
5812 return member_lookup;
5815 Console.WriteLine ("Left is: " + left);
5816 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
5817 Environment.Exit (0);
5821 public Expression DoResolve (EmitContext ec, Expression right_side, ResolveFlags flags)
5824 throw new Exception ();
5826 // Resolve the expression with flow analysis turned off, we'll do the definite
5827 // assignment checks later. This is because we don't know yet what the expression
5828 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
5829 // definite assignment check on the actual field and not on the whole struct.
5832 Expression original = expr;
5833 expr = expr.Resolve (ec, flags | ResolveFlags.DisableFlowAnalysis);
5838 if (expr is SimpleName){
5839 SimpleName child_expr = (SimpleName) expr;
5841 Expression new_expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
5843 return new_expr.Resolve (ec, flags);
5846 int errors = Report.Errors;
5848 Type expr_type = expr.Type;
5850 if (expr_type.IsPointer){
5851 Error (23, "The '.' operator can not be applied to pointer operands (" +
5852 TypeManager.MonoBASIC_Name (expr_type) + ")");
5856 member_lookup = MemberLookup (ec, expr_type, Identifier, loc);
5858 if (member_lookup == null)
5860 // Error has already been reported.
5861 if (errors < Report.Errors)
5865 // Try looking the member up from the same type, if we find
5866 // it, we know that the error was due to limited visibility
5868 object lookup = TypeManager.MemberLookup (
5869 expr_type, expr_type, AllMemberTypes, AllBindingFlags |
5870 BindingFlags.NonPublic, Identifier);
5873 Error (30456, "'" + expr_type + "' does not contain a definition for '" + Identifier + "'");
5876 if ((expr_type != ec.ContainerType) &&
5877 ec.ContainerType.IsSubclassOf (expr_type))
5880 // Although a derived class can access protected members of
5881 // its base class it cannot do so through an instance of the
5882 // base class (CS1540). If the expr_type is a parent of the
5883 // ec.ContainerType and the lookup succeeds with the latter one,
5884 // then we are in this situation.
5886 lookup = TypeManager.MemberLookup(
5887 ec.ContainerType, ec.ContainerType, AllMemberTypes,
5888 AllBindingFlags, Identifier);
5891 Error (1540, "Cannot access protected member '" +
5892 expr_type + "." + Identifier + "' " +
5893 "via a qualifier of type '" + TypeManager.MonoBASIC_Name (expr_type) + "'; the " +
5894 "qualifier must be of type '" + TypeManager.MonoBASIC_Name (ec.ContainerType) + "' " +
5895 "(or derived from it)");
5897 Error (30390, "'" + expr_type + "." + Identifier + "' " +
5898 "is inaccessible because of its protection level");
5900 Error (30390, "'" + expr_type + "." + Identifier + "' " +
5901 "is inaccessible because of its protection level");
5906 if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))) {
5907 Enum en = TypeManager.LookupEnum (expr_type);
5910 object value = en.LookupEnumValue (ec, Identifier, loc);
5911 expr_type = TypeManager.int32_type;
5912 if (value != null) {
5913 Constant c = Constantify (value, en.UnderlyingType);
5914 return new EnumConstant (c, en.UnderlyingType);
5919 if (member_lookup is TypeExpr){
5920 member_lookup.Resolve (ec, ResolveFlags.Type);
5922 return member_lookup;
5923 } else if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
5926 member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original);
5927 if (member_lookup == null)
5930 // The following DoResolve/DoResolveLValue will do the definite assignment
5932 if (right_side != null)
5933 member_lookup = member_lookup.DoResolveLValue (ec, right_side);
5935 member_lookup = member_lookup.DoResolve (ec);
5937 return member_lookup;
5940 public override Expression DoResolve (EmitContext ec)
5942 return DoResolve (ec, null, ResolveFlags.VariableOrValue |
5943 ResolveFlags.SimpleName | ResolveFlags.Type);
5946 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5948 return DoResolve (ec, right_side, ResolveFlags.VariableOrValue |
5949 ResolveFlags.SimpleName | ResolveFlags.Type);
5952 public Expression DoResolveType (EmitContext ec)
5954 return DoResolve (ec, null, ResolveFlags.Type);
5957 public override void Emit (EmitContext ec)
5959 throw new Exception ("Should not happen");
5962 public override string ToString ()
5964 return expr + "." + Identifier;
5971 /// Implements checked expressions
5973 public class CheckedExpr : Expression {
5975 public Expression Expr;
5977 public CheckedExpr (Expression e, Location l)
5983 public override Expression DoResolve (EmitContext ec)
5985 bool last_const_check = ec.ConstantCheckState;
5987 ec.ConstantCheckState = true;
5988 Expr = Expr.Resolve (ec);
5989 ec.ConstantCheckState = last_const_check;
5994 if (Expr is Constant)
5997 eclass = Expr.eclass;
6002 public override void Emit (EmitContext ec)
6004 bool last_check = ec.CheckState;
6005 bool last_const_check = ec.ConstantCheckState;
6007 ec.CheckState = true;
6008 ec.ConstantCheckState = true;
6010 ec.CheckState = last_check;
6011 ec.ConstantCheckState = last_const_check;
6017 /// Implements the unchecked expression
6019 public class UnCheckedExpr : Expression {
6021 public Expression Expr;
6023 public UnCheckedExpr (Expression e, Location l)
6029 public override Expression DoResolve (EmitContext ec)
6031 bool last_const_check = ec.ConstantCheckState;
6033 ec.ConstantCheckState = false;
6034 Expr = Expr.Resolve (ec);
6035 ec.ConstantCheckState = last_const_check;
6040 if (Expr is Constant)
6043 eclass = Expr.eclass;
6048 public override void Emit (EmitContext ec)
6050 bool last_check = ec.CheckState;
6051 bool last_const_check = ec.ConstantCheckState;
6053 ec.CheckState = false;
6054 ec.ConstantCheckState = false;
6056 ec.CheckState = last_check;
6057 ec.ConstantCheckState = last_const_check;
6063 /// An Element Access expression.
6065 /// During semantic analysis these are transformed into
6066 /// IndexerAccess or ArrayAccess
6068 public class ElementAccess : Expression {
6069 public ArrayList Arguments;
6070 public Expression Expr;
6072 public ElementAccess (Expression e, ArrayList e_list, Location l)
6081 Arguments = new ArrayList ();
6082 foreach (Expression tmp in e_list)
6083 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
6087 bool CommonResolve (EmitContext ec)
6089 Expr = Expr.Resolve (ec);
6094 if (Arguments == null)
6097 foreach (Argument a in Arguments){
6098 if (!a.Resolve (ec, loc))
6105 Expression MakePointerAccess ()
6109 if (t == TypeManager.void_ptr_type){
6112 "The array index operation is not valid for void pointers");
6115 if (Arguments.Count != 1){
6118 "A pointer must be indexed by a single value");
6121 Expression p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr,
6123 return new Indirection (p, loc);
6126 public override Expression DoResolve (EmitContext ec)
6128 if (!CommonResolve (ec))
6132 // We perform some simple tests, and then to "split" the emit and store
6133 // code we create an instance of a different class, and return that.
6135 // I am experimenting with this pattern.
6140 return (new ArrayAccess (this, loc)).Resolve (ec);
6141 else if (t.IsPointer)
6142 return MakePointerAccess ();
6144 return (new IndexerAccess (this, loc)).Resolve (ec);
6147 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
6149 if (!CommonResolve (ec))
6154 return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
6155 else if (t.IsPointer)
6156 return MakePointerAccess ();
6158 return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
6161 public override void Emit (EmitContext ec)
6163 throw new Exception ("Should never be reached");
6168 /// Implements array access
6170 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
6172 // Points to our "data" repository
6176 LocalTemporary [] cached_locations;
6178 public ArrayAccess (ElementAccess ea_data, Location l)
6181 eclass = ExprClass.Variable;
6185 public override Expression DoResolve (EmitContext ec)
6187 ExprClass eclass = ea.Expr.eclass;
6190 // As long as the type is valid
6191 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
6192 eclass == ExprClass.Value)) {
6193 ea.Expr.Error118 ("variable or value");
6198 Type t = ea.Expr.Type;
6200 if (t == typeof (System.Object))
6202 // We can't resolve now, but we
6203 // have to try to access the array with a call
6204 // to LateIndexGet in the runtime
6206 Expression lig_call_expr = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexGet", Location.Null);
6207 Expression obj_type = Mono.MonoBASIC.Parser.DecomposeQI("System.Object", Location.Null);
6208 ArrayList adims = new ArrayList();
6210 ArrayList ainit = new ArrayList();
6211 foreach (Argument a in ea.Arguments)
6212 ainit.Add ((Expression) a.Expr);
6214 adims.Add ((Expression) new IntLiteral (ea.Arguments.Count));
6216 Expression oace = new ArrayCreation (obj_type, adims, "", ainit, Location.Null);
6218 ArrayList args = new ArrayList();
6219 args.Add (new Argument(ea.Expr, Argument.AType.Expression));
6220 args.Add (new Argument(oace, Argument.AType.Expression));
6221 args.Add (new Argument(NullLiteral.Null, Argument.AType.Expression));
6223 Expression lig_call = new Invocation (lig_call_expr, args, Location.Null);
6224 lig_call = lig_call.Resolve(ec);
6228 if (t.GetArrayRank () != ea.Arguments.Count){
6230 "Incorrect number of indexes for array " +
6231 " expected: " + t.GetArrayRank () + " got: " +
6232 ea.Arguments.Count);
6235 type = TypeManager.TypeToCoreType (t.GetElementType ());
6236 if (type.IsPointer && !ec.InUnsafe){
6237 UnsafeError (ea.Location);
6241 foreach (Argument a in ea.Arguments){
6242 Type argtype = a.Type;
6244 if (argtype == TypeManager.int32_type ||
6245 argtype == TypeManager.uint32_type ||
6246 argtype == TypeManager.int64_type ||
6247 argtype == TypeManager.uint64_type)
6251 // Mhm. This is strage, because the Argument.Type is not the same as
6252 // Argument.Expr.Type: the value changes depending on the ref/out setting.
6254 // Wonder if I will run into trouble for this.
6256 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.Location);
6261 eclass = ExprClass.Variable;
6267 /// Emits the right opcode to load an object of Type 't'
6268 /// from an array of T
6270 static public void EmitLoadOpcode (ILGenerator ig, Type type)
6272 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
6273 ig.Emit (OpCodes.Ldelem_U1);
6274 else if (type == TypeManager.sbyte_type)
6275 ig.Emit (OpCodes.Ldelem_I1);
6276 else if (type == TypeManager.short_type)
6277 ig.Emit (OpCodes.Ldelem_I2);
6278 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
6279 ig.Emit (OpCodes.Ldelem_U2);
6280 else if (type == TypeManager.int32_type)
6281 ig.Emit (OpCodes.Ldelem_I4);
6282 else if (type == TypeManager.uint32_type)
6283 ig.Emit (OpCodes.Ldelem_U4);
6284 else if (type == TypeManager.uint64_type)
6285 ig.Emit (OpCodes.Ldelem_I8);
6286 else if (type == TypeManager.int64_type)
6287 ig.Emit (OpCodes.Ldelem_I8);
6288 else if (type == TypeManager.float_type)
6289 ig.Emit (OpCodes.Ldelem_R4);
6290 else if (type == TypeManager.double_type)
6291 ig.Emit (OpCodes.Ldelem_R8);
6292 else if (type == TypeManager.intptr_type)
6293 ig.Emit (OpCodes.Ldelem_I);
6294 else if (type.IsValueType){
6295 ig.Emit (OpCodes.Ldelema, type);
6296 ig.Emit (OpCodes.Ldobj, type);
6298 ig.Emit (OpCodes.Ldelem_Ref);
6302 /// Emits the right opcode to store an object of Type 't'
6303 /// from an array of T.
6305 static public void EmitStoreOpcode (ILGenerator ig, Type t)
6307 t = TypeManager.TypeToCoreType (t);
6308 if (TypeManager.IsEnumType (t) && t != TypeManager.enum_type)
6309 t = TypeManager.EnumToUnderlying (t);
6310 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
6311 t == TypeManager.bool_type)
6312 ig.Emit (OpCodes.Stelem_I1);
6313 else if (t == TypeManager.short_type || t == TypeManager.ushort_type || t == TypeManager.char_type)
6314 ig.Emit (OpCodes.Stelem_I2);
6315 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
6316 ig.Emit (OpCodes.Stelem_I4);
6317 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
6318 ig.Emit (OpCodes.Stelem_I8);
6319 else if (t == TypeManager.float_type)
6320 ig.Emit (OpCodes.Stelem_R4);
6321 else if (t == TypeManager.double_type)
6322 ig.Emit (OpCodes.Stelem_R8);
6323 else if (t == TypeManager.intptr_type)
6324 ig.Emit (OpCodes.Stelem_I);
6325 else if (t.IsValueType){
6326 ig.Emit (OpCodes.Stobj, t);
6328 ig.Emit (OpCodes.Stelem_Ref);
6331 MethodInfo FetchGetMethod ()
6333 ModuleBuilder mb = CodeGen.ModuleBuilder;
6334 int arg_count = ea.Arguments.Count;
6335 Type [] args = new Type [arg_count];
6338 for (int i = 0; i < arg_count; i++){
6339 //args [i++] = a.Type;
6340 args [i] = TypeManager.int32_type;
6343 get = mb.GetArrayMethod (
6344 ea.Expr.Type, "Get",
6345 CallingConventions.HasThis |
6346 CallingConventions.Standard,
6352 MethodInfo FetchAddressMethod ()
6354 ModuleBuilder mb = CodeGen.ModuleBuilder;
6355 int arg_count = ea.Arguments.Count;
6356 Type [] args = new Type [arg_count];
6358 string ptr_type_name;
6361 ptr_type_name = type.FullName + "&";
6362 ret_type = Type.GetType (ptr_type_name);
6365 // It is a type defined by the source code we are compiling
6367 if (ret_type == null){
6368 ret_type = mb.GetType (ptr_type_name);
6371 for (int i = 0; i < arg_count; i++){
6372 //args [i++] = a.Type;
6373 args [i] = TypeManager.int32_type;
6376 address = mb.GetArrayMethod (
6377 ea.Expr.Type, "Address",
6378 CallingConventions.HasThis |
6379 CallingConventions.Standard,
6386 // Load the array arguments into the stack.
6388 // If we have been requested to cache the values (cached_locations array
6389 // initialized), then load the arguments the first time and store them
6390 // in locals. otherwise load from local variables.
6392 void LoadArrayAndArguments (EmitContext ec)
6394 ILGenerator ig = ec.ig;
6396 if (cached_locations == null){
6398 foreach (Argument a in ea.Arguments){
6399 Type argtype = a.Expr.Type;
6403 if (argtype == TypeManager.int64_type)
6404 ig.Emit (OpCodes.Conv_Ovf_I);
6405 else if (argtype == TypeManager.uint64_type)
6406 ig.Emit (OpCodes.Conv_Ovf_I_Un);
6411 if (cached_locations [0] == null){
6412 cached_locations [0] = new LocalTemporary (ec, ea.Expr.Type);
6414 ig.Emit (OpCodes.Dup);
6415 cached_locations [0].Store (ec);
6419 foreach (Argument a in ea.Arguments){
6420 Type argtype = a.Expr.Type;
6422 cached_locations [j] = new LocalTemporary (ec, TypeManager.intptr_type /* a.Expr.Type */);
6424 if (argtype == TypeManager.int64_type)
6425 ig.Emit (OpCodes.Conv_Ovf_I);
6426 else if (argtype == TypeManager.uint64_type)
6427 ig.Emit (OpCodes.Conv_Ovf_I_Un);
6429 ig.Emit (OpCodes.Dup);
6430 cached_locations [j].Store (ec);
6436 foreach (LocalTemporary lt in cached_locations)
6440 public new void CacheTemporaries (EmitContext ec)
6442 cached_locations = new LocalTemporary [ea.Arguments.Count + 1];
6445 public override void Emit (EmitContext ec)
6447 int rank = ea.Expr.Type.GetArrayRank ();
6448 ILGenerator ig = ec.ig;
6450 LoadArrayAndArguments (ec);
6453 EmitLoadOpcode (ig, type);
6457 method = FetchGetMethod ();
6458 ig.Emit (OpCodes.Call, method);
6462 public void EmitAssign (EmitContext ec, Expression source)
6464 int rank = ea.Expr.Type.GetArrayRank ();
6465 ILGenerator ig = ec.ig;
6466 Type t = source.Type;
6468 LoadArrayAndArguments (ec);
6471 // The stobj opcode used by value types will need
6472 // an address on the stack, not really an array/array
6476 if (t == TypeManager.enum_type || t == TypeManager.decimal_type ||
6477 (t.IsSubclassOf (TypeManager.value_type) && !TypeManager.IsEnumType (t) && !TypeManager.IsBuiltinType (t)))
6478 ig.Emit (OpCodes.Ldelema, t);
6484 EmitStoreOpcode (ig, t);
6486 ModuleBuilder mb = CodeGen.ModuleBuilder;
6487 int arg_count = ea.Arguments.Count;
6488 Type [] args = new Type [arg_count + 1];
6491 for (int i = 0; i < arg_count; i++){
6492 //args [i++] = a.Type;
6493 args [i] = TypeManager.int32_type;
6496 args [arg_count] = type;
6498 set = mb.GetArrayMethod (
6499 ea.Expr.Type, "Set",
6500 CallingConventions.HasThis |
6501 CallingConventions.Standard,
6502 TypeManager.void_type, args);
6504 ig.Emit (OpCodes.Call, set);
6508 public void AddressOf (EmitContext ec, AddressOp mode)
6510 int rank = ea.Expr.Type.GetArrayRank ();
6511 ILGenerator ig = ec.ig;
6513 LoadArrayAndArguments (ec);
6516 ig.Emit (OpCodes.Ldelema, type);
6518 MethodInfo address = FetchAddressMethod ();
6519 ig.Emit (OpCodes.Call, address);
6526 public ArrayList getters, setters;
6527 static Hashtable map;
6531 map = new Hashtable ();
6534 Indexers (MemberInfo [] mi)
6536 foreach (PropertyInfo property in mi){
6537 MethodInfo get, set;
6539 get = property.GetGetMethod (true);
6541 if (getters == null)
6542 getters = new ArrayList ();
6547 set = property.GetSetMethod (true);
6549 if (setters == null)
6550 setters = new ArrayList ();
6556 static private Indexers GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
6558 Indexers ix = (Indexers) map [lookup_type];
6563 string p_name = TypeManager.IndexerPropertyName (lookup_type);
6565 MemberInfo [] mi = TypeManager.MemberLookup (
6566 caller_type, lookup_type, MemberTypes.Property,
6567 BindingFlags.Public | BindingFlags.Instance, p_name);
6569 if (mi == null || mi.Length == 0)
6572 ix = new Indexers (mi);
6573 map [lookup_type] = ix;
6578 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
6580 Indexers ix = (Indexers) map [lookup_type];
6585 ix = GetIndexersForTypeOrInterface (caller_type, lookup_type);
6589 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
6590 if (ifaces != null) {
6591 foreach (Type itype in ifaces) {
6592 ix = GetIndexersForTypeOrInterface (caller_type, itype);
6598 Report.Error (21, loc,
6599 "Type '" + TypeManager.MonoBASIC_Name (lookup_type) +
6600 "' does not have any indexers defined");
6606 /// Expressions that represent an indexer call.
6608 public class IndexerAccess : Expression, IAssignMethod {
6610 // Points to our "data" repository
6612 MethodInfo get, set;
6614 ArrayList set_arguments;
6615 bool is_base_indexer;
6617 protected Type indexer_type;
6618 protected Type current_type;
6619 protected Expression instance_expr;
6620 protected ArrayList arguments;
6622 public IndexerAccess (ElementAccess ea, Location loc)
6623 : this (ea.Expr, false, loc)
6625 this.arguments = ea.Arguments;
6628 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
6631 this.instance_expr = instance_expr;
6632 this.is_base_indexer = is_base_indexer;
6633 this.eclass = ExprClass.Value;
6637 protected virtual bool CommonResolve (EmitContext ec)
6639 indexer_type = instance_expr.Type;
6640 current_type = ec.ContainerType;
6645 public override Expression DoResolve (EmitContext ec)
6647 if (!CommonResolve (ec))
6651 // Step 1: Query for all 'Item' *properties*. Notice
6652 // that the actual methods are pointed from here.
6654 // This is a group of properties, piles of them.
6657 ilist = Indexers.GetIndexersForType (
6658 current_type, indexer_type, loc);
6661 // Step 2: find the proper match
6663 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
6664 get = (MethodInfo) Invocation.OverloadResolve (
6665 ec, new MethodGroupExpr (ilist.getters, loc), arguments, loc);
6668 Error (154, "indexer can not be used in this context, because " +
6669 "it lacks a 'get' accessor");
6673 type = get.ReturnType;
6674 if (type.IsPointer && !ec.InUnsafe){
6679 eclass = ExprClass.IndexerAccess;
6683 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
6685 if (!CommonResolve (ec))
6688 Type right_type = right_side.Type;
6691 ilist = Indexers.GetIndexersForType (
6692 current_type, indexer_type, loc);
6694 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
6695 set_arguments = (ArrayList) arguments.Clone ();
6696 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
6698 set = (MethodInfo) Invocation.OverloadResolve (
6699 ec, new MethodGroupExpr (ilist.setters, loc), set_arguments, loc);
6703 Error (200, "indexer X.this [" + TypeManager.MonoBASIC_Name (right_type) +
6704 "] lacks a 'set' accessor");
6708 type = TypeManager.void_type;
6709 eclass = ExprClass.IndexerAccess;
6713 public override void Emit (EmitContext ec)
6715 Invocation.EmitCall (ec, false, false, instance_expr, get, arguments, loc);
6719 // source is ignored, because we already have a copy of it from the
6720 // LValue resolution and we have already constructed a pre-cached
6721 // version of the arguments (ea.set_arguments);
6723 public void EmitAssign (EmitContext ec, Expression source)
6725 Invocation.EmitCall (ec, false, false, instance_expr, set, set_arguments, loc);
6730 /// The base operator for method names
6732 public class BaseAccess : Expression {
6733 public string member;
6735 public BaseAccess (string member, Location l)
6737 this.member = member;
6741 public override Expression DoResolve (EmitContext ec)
6743 Expression member_lookup;
6744 Type current_type = ec.ContainerType;
6745 Type base_type = current_type.BaseType;
6749 Error (1511, "Keyword MyBase is not allowed in static method");
6753 if (member == "New")
6756 member_lookup = MemberLookup (ec, base_type, base_type, member,
6757 AllMemberTypes, AllBindingFlags, loc);
6759 if (member_lookup == null) {
6761 TypeManager.MonoBASIC_Name (base_type) + " does not " +
6762 "contain a definition for '" + member + "'");
6769 left = new TypeExpr (base_type, loc);
6773 e = MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
6775 if (e is PropertyExpr){
6776 PropertyExpr pe = (PropertyExpr) e;
6784 public override void Emit (EmitContext ec)
6786 throw new Exception ("Should never be called");
6791 /// The base indexer operator
6793 public class BaseIndexerAccess : IndexerAccess {
6794 public BaseIndexerAccess (ArrayList args, Location loc)
6795 : base (null, true, loc)
6797 arguments = new ArrayList ();
6798 foreach (Expression tmp in args)
6799 arguments.Add (new Argument (tmp, Argument.AType.Expression));
6802 protected override bool CommonResolve (EmitContext ec)
6804 instance_expr = ec.This;
6806 current_type = ec.ContainerType.BaseType;
6807 indexer_type = current_type;
6809 foreach (Argument a in arguments){
6810 if (!a.Resolve (ec, loc))
6819 /// This class exists solely to pass the Type around and to be a dummy
6820 /// that can be passed to the conversion functions (this is used by
6821 /// foreach implementation to typecast the object return value from
6822 /// get_Current into the proper type. All code has been generated and
6823 /// we only care about the side effect conversions to be performed
6825 /// This is also now used as a placeholder where a no-action expression
6826 /// is needed (the 'New' class).
6828 public class EmptyExpression : Expression {
6829 public EmptyExpression ()
6831 type = TypeManager.object_type;
6832 eclass = ExprClass.Value;
6833 loc = Location.Null;
6836 public EmptyExpression (Type t)
6839 eclass = ExprClass.Value;
6840 loc = Location.Null;
6843 public override Expression DoResolve (EmitContext ec)
6848 public override void Emit (EmitContext ec)
6850 // nothing, as we only exist to not do anything.
6854 // This is just because we might want to reuse this bad boy
6855 // instead of creating gazillions of EmptyExpressions.
6856 // (CanConvertImplicit uses it)
6858 public void SetType (Type t)
6864 public class UserCast : Expression {
6868 public UserCast (MethodInfo method, Expression source, Location l)
6870 this.method = method;
6871 this.source = source;
6872 type = method.ReturnType;
6873 eclass = ExprClass.Value;
6877 public override Expression DoResolve (EmitContext ec)
6880 // We are born fully resolved
6885 public override void Emit (EmitContext ec)
6887 ILGenerator ig = ec.ig;
6891 if (method is MethodInfo)
6892 ig.Emit (OpCodes.Call, (MethodInfo) method);
6894 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
6900 // This class is used to "construct" the type during a typecast
6901 // operation. Since the Type.GetType class in .NET can parse
6902 // the type specification, we just use this to construct the type
6903 // one bit at a time.
6905 public class ComposedCast : Expression, ITypeExpression {
6909 public ComposedCast (Expression left, string dim, Location l)
6916 public Expression DoResolveType (EmitContext ec)
6918 Type ltype = ec.DeclSpace.ResolveType (left, false, loc);
6923 // ltype.Fullname is already fully qualified, so we can skip
6924 // a lot of probes, and go directly to TypeManager.LookupType
6926 string cname = ltype.FullName + dim;
6927 type = TypeManager.LookupTypeDirect (cname);
6930 // For arrays of enumerations we are having a problem
6931 // with the direct lookup. Need to investigate.
6933 // For now, fall back to the full lookup in that case.
6935 type = RootContext.LookupType (
6936 ec.DeclSpace, cname, false, loc);
6942 if (!ec.ResolvingTypeTree){
6944 // If the above flag is set, this is being invoked from the ResolveType function.
6945 // Upper layers take care of the type validity in this context.
6947 if (!ec.InUnsafe && type.IsPointer){
6953 eclass = ExprClass.Type;
6957 public override Expression DoResolve (EmitContext ec)
6959 return DoResolveType (ec);
6962 public override void Emit (EmitContext ec)
6964 throw new Exception ("This should never be called");
6967 public override string ToString ()
6974 // This class is used to represent the address of an array, used
6975 // only by the Fixed statement, this is like the C "&a [0]" construct.
6977 public class ArrayPtr : Expression {
6980 public ArrayPtr (Expression array, Location l)
6982 Type array_type = array.Type.GetElementType ();
6986 string array_ptr_type_name = array_type.FullName + "*";
6988 type = Type.GetType (array_ptr_type_name);
6990 ModuleBuilder mb = CodeGen.ModuleBuilder;
6992 type = mb.GetType (array_ptr_type_name);
6995 eclass = ExprClass.Value;
6999 public override void Emit (EmitContext ec)
7001 ILGenerator ig = ec.ig;
7004 IntLiteral.EmitInt (ig, 0);
7005 ig.Emit (OpCodes.Ldelema, array.Type.GetElementType ());
7008 public override Expression DoResolve (EmitContext ec)
7011 // We are born fully resolved
7018 // Used by the fixed statement
7020 public class StringPtr : Expression {
7023 public StringPtr (LocalBuilder b, Location l)
7026 eclass = ExprClass.Value;
7027 type = TypeManager.char_ptr_type;
7031 public override Expression DoResolve (EmitContext ec)
7033 // This should never be invoked, we are born in fully
7034 // initialized state.
7039 public override void Emit (EmitContext ec)
7041 ILGenerator ig = ec.ig;
7043 ig.Emit (OpCodes.Ldloc, b);
7044 ig.Emit (OpCodes.Conv_I);
7045 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
7046 ig.Emit (OpCodes.Add);
7051 // Implements the 'stackalloc' keyword
7053 public class StackAlloc : Expression {
7058 public StackAlloc (Expression type, Expression count, Location l)
7065 public override Expression DoResolve (EmitContext ec)
7067 count = count.Resolve (ec);
7071 if (count.Type != TypeManager.int32_type){
7072 count = ConvertImplicitRequired (ec, count, TypeManager.int32_type, loc);
7077 if (ec.InCatch || ec.InFinally){
7079 "stackalloc can not be used in a catch or finally block");
7083 otype = ec.DeclSpace.ResolveType (t, false, loc);
7088 if (!TypeManager.VerifyUnManaged (otype, loc))
7091 string ptr_name = otype.FullName + "*";
7092 type = Type.GetType (ptr_name);
7094 ModuleBuilder mb = CodeGen.ModuleBuilder;
7096 type = mb.GetType (ptr_name);
7098 eclass = ExprClass.Value;
7103 public override void Emit (EmitContext ec)
7105 int size = GetTypeSize (otype);
7106 ILGenerator ig = ec.ig;
7109 ig.Emit (OpCodes.Sizeof, otype);
7111 IntConstant.EmitInt (ig, size);
7113 ig.Emit (OpCodes.Mul);
7114 ig.Emit (OpCodes.Localloc);