2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 // Maybe we should make Resolve be an instance method that just calls
12 // the virtual DoResolve function and checks conditions like the eclass
13 // and type being set if a non-null value is returned. For robustness
18 using System.Collections;
19 using System.Diagnostics;
21 using System.Reflection;
22 using System.Reflection.Emit;
26 // The ExprClass class contains the is used to pass the
27 // classification of an expression (value, variable, namespace,
28 // type, method group, property access, event access, indexer access,
31 public enum ExprClass {
35 Variable, // Every Variable should implement LValue
46 // Base class for expressions
48 public abstract class Expression {
49 protected ExprClass eclass;
62 public ExprClass ExprClass {
73 // Utility wrapper routine for Error, just to beautify the code
75 static protected void Error (TypeContainer tc, int error, string s)
77 tc.RootContext.Report.Error (error, s);
80 static protected void Error (TypeContainer tc, int error, Location l, string s)
82 tc.RootContext.Report.Error (error, l, s);
86 // Utility wrapper routine for Warning, just to beautify the code
88 static protected void Warning (TypeContainer tc, int warning, string s)
90 tc.RootContext.Report.Warning (warning, s);
94 // Performs semantic analysis on the Expression
98 // The Resolve method is invoked to perform the semantic analysis
101 // The return value is an expression (it can be the
102 // same expression in some cases) or a new
103 // expression that better represents this node.
105 // For example, optimizations of Unary (LiteralInt)
106 // would return a new LiteralInt with a negated
109 // If there is an error during semantic analysis,
110 // then an error should
111 // be reported (using TypeContainer.RootContext.Report) and a null
112 // value should be returned.
114 // There are two side effects expected from calling
115 // Resolve(): the the field variable "eclass" should
116 // be set to any value of the enumeration
117 // `ExprClass' and the type variable should be set
118 // to a valid type (this is the type of the
122 public abstract Expression DoResolve (TypeContainer tc);
126 // Currently Resolve wraps DoResolve to perform sanity
127 // checking and assertion checking on what we expect from Resolve
130 public Expression Resolve (TypeContainer tc)
132 Expression e = DoResolve (tc);
135 if (e.ExprClass == ExprClass.Invalid)
136 throw new Exception ("Expression " + e +
137 " ExprClass is Invalid after resolve");
139 if (e.ExprClass != ExprClass.MethodGroup)
141 throw new Exception ("Expression " + e +
142 " did not set its type after Resolve");
149 // Emits the code for the expression
154 // The Emit method is invoked to generate the code
155 // for the expression.
158 public abstract void Emit (EmitContext ec);
161 // Protected constructor. Only derivate types should
162 // be able to be created
165 protected Expression ()
167 eclass = ExprClass.Invalid;
172 // Returns a fully formed expression after a MemberLookup
174 static Expression ExprClassFromMemberInfo (MemberInfo mi)
176 if (mi is EventInfo){
177 return new EventExpr ((EventInfo) mi);
178 } else if (mi is FieldInfo){
179 return new FieldExpr ((FieldInfo) mi);
180 } else if (mi is PropertyInfo){
181 return new PropertyExpr ((PropertyInfo) mi);
182 } else if (mi is Type)
183 return new TypeExpr ((Type) mi);
189 // FIXME: Probably implement a cache for (t,name,current_access_set)?
191 // FIXME: We need to cope with access permissions here, or this wont
194 // This code could use some optimizations, but we need to do some
195 // measurements. For example, we could use a delegate to `flag' when
196 // something can not any longer be a method-group (because it is something
200 // If the return value is an Array, then it is an array of
203 // If the return value is an MemberInfo, it is anything, but a Method
207 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
208 // the arguments here and have MemberLookup return only the methods that
209 // match the argument count/type, unlike we are doing now (we delay this
212 // This is so we can catch correctly attempts to invoke instance methods
213 // from a static body (scan for error 120 in ResolveSimpleName).
215 public static Expression MemberLookup (TypeContainer tc, Type t, string name,
216 bool same_type, MemberTypes mt, BindingFlags bf)
219 bf |= BindingFlags.NonPublic;
221 MemberInfo [] mi = tc.RootContext.TypeManager.FindMembers (
222 t, mt, bf, Type.FilterName, name);
227 if (mi.Length == 1 && !(mi [0] is MethodBase))
228 return Expression.ExprClassFromMemberInfo (mi [0]);
230 for (int i = 0; i < mi.Length; i++)
231 if (!(mi [i] is MethodBase)){
233 -5, "Do not know how to reproduce this case: " +
234 "Methods and non-Method with the same name, " +
235 "report this please");
237 for (i = 0; i < mi.Length; i++){
238 Type tt = mi [i].GetType ();
240 Console.WriteLine (i + ": " + mi [i]);
241 while (tt != TypeManager.object_type){
242 Console.WriteLine (tt);
248 return new MethodGroupExpr (mi);
251 public const MemberTypes AllMemberTypes =
252 MemberTypes.Constructor |
256 MemberTypes.NestedType |
257 MemberTypes.Property;
259 public const BindingFlags AllBindingsFlags =
260 BindingFlags.Public |
261 BindingFlags.Static |
262 BindingFlags.Instance;
264 public static Expression MemberLookup (TypeContainer tc, Type t, string name,
267 return MemberLookup (tc, t, name, same_type, AllMemberTypes, AllBindingsFlags);
271 // I am in general unhappy with this implementation.
273 // I need to revise this.
275 static public Expression ResolveMemberAccess (TypeContainer tc, string name)
277 Expression left_e = null;
278 int dot_pos = name.LastIndexOf (".");
279 string left = name.Substring (0, dot_pos);
280 string right = name.Substring (dot_pos + 1);
283 if ((t = tc.LookupType (left, false)) != null)
284 left_e = new TypeExpr (t);
290 // T.P Static property access (P) on Type T.
291 // e.P instance property access on instance e for P.
297 Error (tc, 246, "Can not find type or namespace `"+left+"'");
301 switch (left_e.ExprClass){
303 return MemberLookup (tc,
305 left_e.Type == tc.TypeBuilder);
307 case ExprClass.Namespace:
308 case ExprClass.PropertyAccess:
309 case ExprClass.IndexerAccess:
310 case ExprClass.Variable:
311 case ExprClass.Value:
312 case ExprClass.Nothing:
313 case ExprClass.EventAccess:
314 case ExprClass.MethodGroup:
315 case ExprClass.Invalid:
316 throw new Exception ("Should have got the " + left_e.ExprClass +
323 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
325 Type expr_type = expr.Type;
327 if (target_type == TypeManager.object_type) {
328 if (expr_type.IsClass)
329 return new EmptyCast (expr, target_type);
330 if (expr_type.IsValueType)
331 return new BoxedCast (expr);
332 } else if (expr_type.IsSubclassOf (target_type))
333 return new EmptyCast (expr, target_type);
335 // FIXME: missing implicit reference conversions:
337 // from any class-type S to any interface-type T.
338 // from any interface type S to interface-type T.
339 // from an array-type S to an array-type of type T
340 // from an array-type to System.Array
341 // from any delegate type to System.Delegate
342 // from any array-type or delegate type into System.ICloneable.
343 // from the null type to any reference-type.
351 // Handles expressions like this: decimal d; d = 1;
352 // and changes them into: decimal d; d = new System.Decimal (1);
354 static Expression InternalTypeConstructor (TypeContainer tc, Expression expr, Type target)
356 ArrayList args = new ArrayList ();
358 args.Add (new Argument (expr, Argument.AType.Expression));
360 Console.WriteLine ("The InternalTypeConstructor is: " + expr);
361 Expression ne = new New (target.FullName, args,
362 new Location ("FIXME", 1, 1));
364 return ne.Resolve (tc);
368 // Implicit Numeric Conversions.
370 // expr is the expression to convert, returns a new expression of type
371 // target_type or null if an implicit conversion is not possible.
374 static public Expression ImplicitNumericConversion (TypeContainer tc, Expression expr,
375 Type target_type, Location l)
377 Type expr_type = expr.Type;
380 // Attempt to do the implicit constant expression conversions
382 if (expr is IntLiteral){
385 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
388 } else if (expr is LongLiteral){
390 // Try the implicit constant expression conversion
391 // from long to ulong, instead of a nice routine,
394 if (((LongLiteral) expr).Value > 0)
398 if (expr_type == TypeManager.sbyte_type){
400 // From sbyte to short, int, long, float, double.
402 if (target_type == TypeManager.int32_type)
403 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
404 if (target_type == TypeManager.int64_type)
405 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
406 if (target_type == TypeManager.double_type)
407 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
408 if (target_type == TypeManager.float_type)
409 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
410 if (target_type == TypeManager.short_type)
411 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
412 if (target_type == TypeManager.decimal_type)
413 return InternalTypeConstructor (tc, expr, target_type);
414 } else if (expr_type == TypeManager.byte_type){
416 // From byte to short, ushort, int, uint, long, ulong, float, double
418 if ((target_type == TypeManager.short_type) ||
419 (target_type == TypeManager.ushort_type) ||
420 (target_type == TypeManager.int32_type) ||
421 (target_type == TypeManager.uint32_type))
422 return new EmptyCast (expr, target_type);
424 if (target_type == TypeManager.uint64_type)
425 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
426 if (target_type == TypeManager.int64_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
429 if (target_type == TypeManager.float_type)
430 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
431 if (target_type == TypeManager.double_type)
432 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
433 if (target_type == TypeManager.decimal_type)
434 return InternalTypeConstructor (tc, expr, target_type);
435 } else if (expr_type == TypeManager.short_type){
437 // From short to int, long, float, double
439 if (target_type == TypeManager.int32_type)
440 return new EmptyCast (expr, target_type);
441 if (target_type == TypeManager.int64_type)
442 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
443 if (target_type == TypeManager.double_type)
444 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
445 if (target_type == TypeManager.float_type)
446 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
447 if (target_type == TypeManager.decimal_type)
448 return InternalTypeConstructor (tc, expr, target_type);
449 } else if (expr_type == TypeManager.ushort_type){
451 // From ushort to int, uint, long, ulong, float, double
453 if ((target_type == TypeManager.uint32_type) ||
454 (target_type == TypeManager.uint64_type))
455 return new EmptyCast (expr, target_type);
457 if (target_type == TypeManager.int32_type)
458 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
459 if (target_type == TypeManager.int64_type)
460 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
461 if (target_type == TypeManager.double_type)
462 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
463 if (target_type == TypeManager.float_type)
464 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
465 if (target_type == TypeManager.decimal_type)
466 return InternalTypeConstructor (tc, expr, target_type);
467 } else if (expr_type == TypeManager.int32_type){
469 // From int to long, float, double
471 if (target_type == TypeManager.int64_type)
472 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
473 if (target_type == TypeManager.double_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
475 if (target_type == TypeManager.float_type)
476 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
477 if (target_type == TypeManager.decimal_type)
478 return InternalTypeConstructor (tc, expr, target_type);
479 } else if (expr_type == TypeManager.uint32_type){
481 // From uint to long, ulong, float, double
483 if (target_type == TypeManager.int64_type)
484 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
485 if (target_type == TypeManager.uint64_type)
486 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
487 if (target_type == TypeManager.double_type)
488 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
490 if (target_type == TypeManager.float_type)
491 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
493 if (target_type == TypeManager.decimal_type)
494 return InternalTypeConstructor (tc, expr, target_type);
495 } else if ((expr_type == TypeManager.uint64_type) ||
496 (expr_type == TypeManager.int64_type)){
498 // From long/ulong to float, double
500 if (target_type == TypeManager.double_type)
501 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
503 if (target_type == TypeManager.float_type)
504 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
506 if (target_type == TypeManager.decimal_type)
507 return InternalTypeConstructor (tc, expr, target_type);
508 } else if (expr_type == TypeManager.char_type){
510 // From char to ushort, int, uint, long, ulong, float, double
512 if ((target_type == TypeManager.ushort_type) ||
513 (target_type == TypeManager.int32_type) ||
514 (target_type == TypeManager.uint32_type))
515 return new EmptyCast (expr, target_type);
516 if (target_type == TypeManager.uint64_type)
517 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
518 if (target_type == TypeManager.int64_type)
519 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
520 if (target_type == TypeManager.float_type)
521 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
522 if (target_type == TypeManager.double_type)
523 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
524 if (target_type == TypeManager.decimal_type)
525 return InternalTypeConstructor (tc, expr, target_type);
526 } else if (expr_type == TypeManager.float_type){
530 if (target_type == TypeManager.double_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
538 // User-defined implicit conversions
540 static public Expression ImplicitUserConversion (TypeContainer tc, Expression source,
541 Type target, Location l)
547 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
548 mg2 = MemberLookup (tc, target, "op_Implicit", false);
550 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
553 arguments = new ArrayList ();
554 arguments.Add (new Argument (source, Argument.AType.Expression));
556 method = Invocation.OverloadResolve (tc, union, arguments, l, true);
558 if (method != null) {
559 MethodInfo mi = (MethodInfo) method;
561 if (mi.ReturnType == target)
562 return new UserImplicitCast (mi, arguments);
566 // If we have a boolean type, we need to check for the True
567 // and False operators too.
569 if (target == TypeManager.bool_type) {
571 mg1 = MemberLookup (tc, source.Type, "op_True", false);
572 mg2 = MemberLookup (tc, target, "op_True", false);
574 union = Invocation.MakeUnionSet (mg1, mg2);
579 arguments = new ArrayList ();
580 arguments.Add (new Argument (source, Argument.AType.Expression));
582 method = Invocation.OverloadResolve (tc, union, arguments,
583 new Location ("FIXME", 1, 1), true);
584 if (method != null) {
585 MethodInfo mi = (MethodInfo) method;
587 if (mi.ReturnType == target)
588 return new UserImplicitCast (mi, arguments);
596 // Converts implicitly the resolved expression `expr' into the
597 // `target_type'. It returns a new expression that can be used
598 // in a context that expects a `target_type'.
600 static public Expression ConvertImplicit (TypeContainer tc, Expression expr,
601 Type target_type, Location l)
603 Type expr_type = expr.Type;
606 if (expr_type == target_type)
609 e = ImplicitNumericConversion (tc, expr, target_type, l);
613 e = ImplicitReferenceConversion (expr, target_type);
617 e = ImplicitUserConversion (tc, expr, target_type, l);
621 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
622 IntLiteral i = (IntLiteral) expr;
625 return new EmptyCast (expr, target_type);
632 // Attempts to apply the `Standard Implicit
633 // Conversion' rules to the expression `expr' into
634 // the `target_type'. It returns a new expression
635 // that can be used in a context that expects a
638 // This is different from `ConvertImplicit' in that the
639 // user defined implicit conversions are excluded.
641 static public Expression ConvertImplicitStandard (TypeContainer tc, Expression expr,
642 Type target_type, Location l)
644 Type expr_type = expr.Type;
647 if (expr_type == target_type)
650 e = ImplicitNumericConversion (tc, expr, target_type, l);
654 e = ImplicitReferenceConversion (expr, target_type);
658 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
659 IntLiteral i = (IntLiteral) expr;
662 return new EmptyCast (expr, target_type);
667 // Attemps to perform an implict constant conversion of the IntLiteral
668 // into a different data type using casts (See Implicit Constant
669 // Expression Conversions)
671 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
673 int value = il.Value;
675 if (target_type == TypeManager.sbyte_type){
676 if (value >= SByte.MinValue && value <= SByte.MaxValue)
678 } else if (target_type == TypeManager.byte_type){
679 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
681 } else if (target_type == TypeManager.short_type){
682 if (value >= Int16.MinValue && value <= Int16.MaxValue)
684 } else if (target_type == TypeManager.ushort_type){
685 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
687 } else if (target_type == TypeManager.uint32_type){
689 // we can optimize this case: a positive int32
690 // always fits on a uint32
694 } else if (target_type == TypeManager.uint64_type){
696 // we can optimize this case: a positive int32
697 // always fits on a uint64. But we need an opcode
701 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
708 // Attemptes to implicityly convert `target' into `type', using
709 // ConvertImplicit. If there is no implicit conversion, then
710 // an error is signaled
712 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
713 Type type, Location l)
717 e = ConvertImplicit (tc, target, type, l);
721 string msg = "Can not convert implicitly from `"+
722 TypeManager.CSharpName (target.Type) + "' to `" +
723 TypeManager.CSharpName (type) + "'";
725 Error (tc, 29, l, msg);
731 // Performs the explicit numeric conversions
733 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
736 Type expr_type = expr.Type;
738 if (expr_type == TypeManager.sbyte_type){
740 // From sbyte to byte, ushort, uint, ulong, char
742 if (target_type == TypeManager.byte_type)
743 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
744 if (target_type == TypeManager.ushort_type)
745 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
746 if (target_type == TypeManager.uint32_type)
747 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
748 if (target_type == TypeManager.uint64_type)
749 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
750 if (target_type == TypeManager.char_type)
751 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
752 } else if (expr_type == TypeManager.byte_type){
754 // From byte to sbyte and char
756 if (target_type == TypeManager.sbyte_type)
757 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
758 if (target_type == TypeManager.char_type)
759 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
760 } else if (expr_type == TypeManager.short_type){
762 // From short to sbyte, byte, ushort, uint, ulong, char
764 if (target_type == TypeManager.sbyte_type)
765 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
766 if (target_type == TypeManager.byte_type)
767 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
768 if (target_type == TypeManager.ushort_type)
769 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
770 if (target_type == TypeManager.uint32_type)
771 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
772 if (target_type == TypeManager.uint64_type)
773 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
774 if (target_type == TypeManager.char_type)
775 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
776 } else if (expr_type == TypeManager.ushort_type){
778 // From ushort to sbyte, byte, short, char
780 if (target_type == TypeManager.sbyte_type)
781 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
782 if (target_type == TypeManager.byte_type)
783 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
784 if (target_type == TypeManager.short_type)
785 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
786 if (target_type == TypeManager.char_type)
787 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
788 } else if (expr_type == TypeManager.int32_type){
790 // From int to sbyte, byte, short, ushort, uint, ulong, char
792 if (target_type == TypeManager.sbyte_type)
793 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
794 if (target_type == TypeManager.byte_type)
795 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
796 if (target_type == TypeManager.short_type)
797 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
798 if (target_type == TypeManager.ushort_type)
799 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
800 if (target_type == TypeManager.uint32_type)
801 return new EmptyCast (expr, target_type);
802 if (target_type == TypeManager.uint64_type)
803 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
804 if (target_type == TypeManager.char_type)
805 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
806 } else if (expr_type == TypeManager.uint32_type){
808 // From uint to sbyte, byte, short, ushort, int, char
810 if (target_type == TypeManager.sbyte_type)
811 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
812 if (target_type == TypeManager.byte_type)
813 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
814 if (target_type == TypeManager.short_type)
815 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
816 if (target_type == TypeManager.ushort_type)
817 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
818 if (target_type == TypeManager.int32_type)
819 return new EmptyCast (expr, target_type);
820 if (target_type == TypeManager.char_type)
821 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
822 } else if (expr_type == TypeManager.int64_type){
824 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
826 if (target_type == TypeManager.sbyte_type)
827 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
828 if (target_type == TypeManager.byte_type)
829 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
830 if (target_type == TypeManager.short_type)
831 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
832 if (target_type == TypeManager.ushort_type)
833 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
834 if (target_type == TypeManager.int32_type)
835 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
836 if (target_type == TypeManager.uint32_type)
837 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
838 if (target_type == TypeManager.uint64_type)
839 return new EmptyCast (expr, target_type);
840 if (target_type == TypeManager.char_type)
841 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
842 } else if (expr_type == TypeManager.uint64_type){
844 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
846 if (target_type == TypeManager.sbyte_type)
847 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
848 if (target_type == TypeManager.byte_type)
849 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
850 if (target_type == TypeManager.short_type)
851 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
852 if (target_type == TypeManager.ushort_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
854 if (target_type == TypeManager.int32_type)
855 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
856 if (target_type == TypeManager.uint32_type)
857 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
858 if (target_type == TypeManager.int64_type)
859 return new EmptyCast (expr, target_type);
860 if (target_type == TypeManager.char_type)
861 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
862 } else if (expr_type == TypeManager.char_type){
864 // From char to sbyte, byte, short
866 if (target_type == TypeManager.sbyte_type)
867 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
868 if (target_type == TypeManager.byte_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
870 if (target_type == TypeManager.short_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
872 } else if (expr_type == TypeManager.float_type){
874 // From float to sbyte, byte, short,
875 // ushort, int, uint, long, ulong, char
878 if (target_type == TypeManager.sbyte_type)
879 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
880 if (target_type == TypeManager.byte_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
882 if (target_type == TypeManager.short_type)
883 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
884 if (target_type == TypeManager.ushort_type)
885 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
886 if (target_type == TypeManager.int32_type)
887 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
888 if (target_type == TypeManager.uint32_type)
889 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
890 if (target_type == TypeManager.int64_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
892 if (target_type == TypeManager.uint64_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
894 if (target_type == TypeManager.char_type)
895 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
896 if (target_type == TypeManager.decimal_type)
897 return InternalTypeConstructor (tc, expr, target_type);
898 } else if (expr_type == TypeManager.double_type){
900 // From double to byte, byte, short,
901 // ushort, int, uint, long, ulong,
902 // char, float or decimal
904 Console.WriteLine ("Ok, I am a double " + target_type);
905 if (target_type == TypeManager.sbyte_type)
906 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
907 if (target_type == TypeManager.byte_type)
908 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
909 if (target_type == TypeManager.short_type)
910 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
911 if (target_type == TypeManager.ushort_type)
912 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
913 if (target_type == TypeManager.int32_type)
914 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
915 if (target_type == TypeManager.uint32_type)
916 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
917 if (target_type == TypeManager.int64_type)
918 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
919 if (target_type == TypeManager.uint64_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
921 if (target_type == TypeManager.char_type)
922 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
923 if (target_type == TypeManager.float_type)
924 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
925 if (target_type == TypeManager.decimal_type)
926 return InternalTypeConstructor (tc, expr, target_type);
929 // decimal is taken care of by the op_Explicit methods.
935 // Implements Explicit Reference conversions
937 static Expression ConvertReferenceExplicit (TypeContainer tc, Expression expr,
940 Type expr_type = expr.Type;
941 bool target_is_value_type = target_type.IsValueType;
944 // From object to any reference type
946 if (expr_type == TypeManager.object_type && !target_is_value_type)
947 return new ClassCast (expr, expr_type);
953 // Performs an explicit conversion of the expression `expr' whose
954 // type is expr.Type to `target_type'.
956 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
959 Expression ne = ConvertImplicit (tc, expr, target_type, Location.Null);
964 ne = ConvertNumericExplicit (tc, expr, target_type);
968 ne = ConvertReferenceExplicit (tc, expr, target_type);
975 static string ExprClassName (ExprClass c)
978 case ExprClass.Invalid:
980 case ExprClass.Value:
982 case ExprClass.Variable:
984 case ExprClass.Namespace:
988 case ExprClass.MethodGroup:
989 return "method group";
990 case ExprClass.PropertyAccess:
991 return "property access";
992 case ExprClass.EventAccess:
993 return "event access";
994 case ExprClass.IndexerAccess:
995 return "indexer access";
996 case ExprClass.Nothing:
999 throw new Exception ("Should not happen");
1003 // Reports that we were expecting `expr' to be of class `expected'
1005 protected void report118 (TypeContainer tc, Expression expr, string expected)
1007 Error (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
1008 "' where an " + expected + " was expected");
1013 // This is just a base class for expressions that can
1014 // appear on statements (invocations, object creation,
1015 // assignments, post/pre increment and decrement). The idea
1016 // being that they would support an extra Emition interface that
1017 // does not leave a result on the stack.
1020 public abstract class ExpressionStatement : Expression {
1023 // Requests the expression to be emitted in a `statement'
1024 // context. This means that no new value is left on the
1025 // stack after invoking this method (constrasted with
1026 // Emit that will always leave a value on the stack).
1028 public abstract void EmitStatement (EmitContext ec);
1032 // This kind of cast is used to encapsulate the child
1033 // whose type is child.Type into an expression that is
1034 // reported to return "return_type". This is used to encapsulate
1035 // expressions which have compatible types, but need to be dealt
1036 // at higher levels with.
1038 // For example, a "byte" expression could be encapsulated in one
1039 // of these as an "unsigned int". The type for the expression
1040 // would be "unsigned int".
1044 public class EmptyCast : Expression {
1045 protected Expression child;
1047 public EmptyCast (Expression child, Type return_type)
1049 ExprClass = child.ExprClass;
1054 public override Expression DoResolve (TypeContainer tc)
1056 // This should never be invoked, we are born in fully
1057 // initialized state.
1062 public override void Emit (EmitContext ec)
1069 // This kind of cast is used to encapsulate Value Types in objects.
1071 // The effect of it is to box the value type emitted by the previous
1074 public class BoxedCast : EmptyCast {
1076 public BoxedCast (Expression expr)
1077 : base (expr, TypeManager.object_type)
1081 public override Expression DoResolve (TypeContainer tc)
1083 // This should never be invoked, we are born in fully
1084 // initialized state.
1089 public override void Emit (EmitContext ec)
1092 ec.ig.Emit (OpCodes.Box, child.Type);
1097 // This kind of cast is used to encapsulate a child expression
1098 // that can be trivially converted to a target type using one or
1099 // two opcodes. The opcodes are passed as arguments.
1101 public class OpcodeCast : EmptyCast {
1105 public OpcodeCast (Expression child, Type return_type, OpCode op)
1106 : base (child, return_type)
1110 second_valid = false;
1113 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1114 : base (child, return_type)
1119 second_valid = true;
1122 public override Expression DoResolve (TypeContainer tc)
1124 // This should never be invoked, we are born in fully
1125 // initialized state.
1130 public override void Emit (EmitContext ec)
1142 // This kind of cast is used to encapsulate a child and cast it
1143 // to the class requested
1145 public class ClassCast : EmptyCast {
1146 public ClassCast (Expression child, Type return_type)
1147 : base (child, return_type)
1152 public override Expression DoResolve (TypeContainer tc)
1154 // This should never be invoked, we are born in fully
1155 // initialized state.
1160 public override void Emit (EmitContext ec)
1164 ec.ig.Emit (OpCodes.Castclass, type);
1170 // Unary expressions.
1174 // Unary implements unary expressions. It derives from
1175 // ExpressionStatement becuase the pre/post increment/decrement
1176 // operators can be used in a statement context.
1178 public class Unary : ExpressionStatement {
1179 public enum Operator {
1180 Addition, Subtraction, Negate, BitComplement,
1181 Indirection, AddressOf, PreIncrement,
1182 PreDecrement, PostIncrement, PostDecrement
1187 ArrayList Arguments;
1191 public Unary (Operator op, Expression expr, Location loc)
1195 this.location = loc;
1198 public Expression Expr {
1208 public Operator Oper {
1219 // Returns a stringified representation of the Operator
1224 case Operator.Addition:
1226 case Operator.Subtraction:
1228 case Operator.Negate:
1230 case Operator.BitComplement:
1232 case Operator.AddressOf:
1234 case Operator.Indirection:
1236 case Operator.PreIncrement : case Operator.PostIncrement :
1238 case Operator.PreDecrement : case Operator.PostDecrement :
1242 return oper.ToString ();
1245 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1247 if (expr.Type == target_type)
1250 return ConvertImplicit (tc, expr, target_type, new Location ("FIXME", 1, 1));
1253 void report23 (Report r, Type t)
1255 r.Error (23, "Operator " + OperName () + " cannot be applied to operand of type `" +
1256 TypeManager.CSharpName (t) + "'");
1260 // Returns whether an object of type `t' can be incremented
1261 // or decremented with add/sub (ie, basically whether we can
1262 // use pre-post incr-decr operations on it, but it is not a
1263 // System.Decimal, which we test elsewhere)
1265 static bool IsIncrementableNumber (Type t)
1267 return (t == TypeManager.sbyte_type) ||
1268 (t == TypeManager.byte_type) ||
1269 (t == TypeManager.short_type) ||
1270 (t == TypeManager.ushort_type) ||
1271 (t == TypeManager.int32_type) ||
1272 (t == TypeManager.uint32_type) ||
1273 (t == TypeManager.int64_type) ||
1274 (t == TypeManager.uint64_type) ||
1275 (t == TypeManager.char_type) ||
1276 (t.IsSubclassOf (TypeManager.enum_type)) ||
1277 (t == TypeManager.float_type) ||
1278 (t == TypeManager.double_type);
1281 Expression ResolveOperator (TypeContainer tc)
1283 Type expr_type = expr.Type;
1286 // Step 1: Perform Operator Overload location
1291 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1292 op_name = "op_Increment";
1293 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1294 op_name = "op_Decrement";
1296 op_name = "op_" + oper;
1298 mg = MemberLookup (tc, expr_type, op_name, false);
1301 Arguments = new ArrayList ();
1302 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1304 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg,
1305 Arguments, location);
1306 if (method != null) {
1307 MethodInfo mi = (MethodInfo) method;
1309 type = mi.ReturnType;
1315 // Step 2: Default operations on CLI native types.
1318 // Only perform numeric promotions on:
1321 if (expr_type == null)
1324 if (oper == Operator.Negate){
1325 if (expr_type != TypeManager.bool_type) {
1326 report23 (tc.RootContext.Report, expr.Type);
1330 type = TypeManager.bool_type;
1334 if (oper == Operator.BitComplement) {
1335 if (!((expr_type == TypeManager.int32_type) ||
1336 (expr_type == TypeManager.uint32_type) ||
1337 (expr_type == TypeManager.int64_type) ||
1338 (expr_type == TypeManager.uint64_type) ||
1339 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1340 report23 (tc.RootContext.Report, expr.Type);
1347 if (oper == Operator.Addition) {
1349 // A plus in front of something is just a no-op, so return the child.
1355 // Deals with -literals
1356 // int operator- (int x)
1357 // long operator- (long x)
1358 // float operator- (float f)
1359 // double operator- (double d)
1360 // decimal operator- (decimal d)
1362 if (oper == Operator.Subtraction){
1364 // Fold a "- Constant" into a negative constant
1367 Expression e = null;
1370 // Is this a constant?
1372 if (expr is IntLiteral)
1373 e = new IntLiteral (-((IntLiteral) expr).Value);
1374 else if (expr is LongLiteral)
1375 e = new LongLiteral (-((LongLiteral) expr).Value);
1376 else if (expr is FloatLiteral)
1377 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1378 else if (expr is DoubleLiteral)
1379 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1380 else if (expr is DecimalLiteral)
1381 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1389 // Not a constant we can optimize, perform numeric
1390 // promotions to int, long, double.
1393 // The following is inneficient, because we call
1394 // ConvertImplicit too many times.
1396 // It is also not clear if we should convert to Float
1397 // or Double initially.
1399 Location l = new Location ("FIXME", 1, 1);
1401 if (expr_type == TypeManager.uint32_type){
1403 // FIXME: handle exception to this rule that
1404 // permits the int value -2147483648 (-2^31) to
1405 // bt written as a decimal interger literal
1407 type = TypeManager.int64_type;
1408 expr = ConvertImplicit (tc, expr, type, l);
1412 if (expr_type == TypeManager.uint64_type){
1414 // FIXME: Handle exception of `long value'
1415 // -92233720368547758087 (-2^63) to be written as
1416 // decimal integer literal.
1418 report23 (tc.RootContext.Report, expr_type);
1422 e = ConvertImplicit (tc, expr, TypeManager.int32_type, l);
1429 e = ConvertImplicit (tc, expr, TypeManager.int64_type, l);
1436 e = ConvertImplicit (tc, expr, TypeManager.double_type, l);
1443 report23 (tc.RootContext.Report, expr_type);
1448 // The operand of the prefix/postfix increment decrement operators
1449 // should be an expression that is classified as a variable,
1450 // a property access or an indexer access
1452 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1453 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1454 if (expr.ExprClass == ExprClass.Variable){
1455 if (IsIncrementableNumber (expr_type) ||
1456 expr_type == TypeManager.decimal_type){
1460 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1462 // FIXME: Verify that we have both get and set methods
1464 throw new Exception ("Implement me");
1465 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1467 // FIXME: Verify that we have both get and set methods
1469 throw new Exception ("Implement me");
1471 report118 (tc, expr, "variable, indexer or property access");
1475 if (oper == Operator.AddressOf){
1476 if (expr.ExprClass != ExprClass.Variable){
1477 Error (tc, 211, "Cannot take the address of non-variables");
1480 type = Type.GetType (expr.Type.ToString () + "*");
1483 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1484 TypeManager.CSharpName (expr_type) + "'");
1489 public override Expression DoResolve (TypeContainer tc)
1491 expr = expr.Resolve (tc);
1496 eclass = ExprClass.Value;
1497 return ResolveOperator (tc);
1500 public override void Emit (EmitContext ec)
1502 ILGenerator ig = ec.ig;
1503 Type expr_type = expr.Type;
1505 if (method != null) {
1507 // Note that operators are static anyway
1509 if (Arguments != null)
1510 Invocation.EmitArguments (ec, method, Arguments);
1513 // Post increment/decrement operations need a copy at this
1516 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1517 ig.Emit (OpCodes.Dup);
1520 ig.Emit (OpCodes.Call, (MethodInfo) method);
1523 // Pre Increment and Decrement operators
1525 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1526 ig.Emit (OpCodes.Dup);
1530 // Increment and Decrement should store the result
1532 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1533 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1534 ((LValue) expr).Store (ec);
1540 case Operator.Addition:
1541 throw new Exception ("This should be caught by Resolve");
1543 case Operator.Subtraction:
1545 ig.Emit (OpCodes.Neg);
1548 case Operator.Negate:
1550 ig.Emit (OpCodes.Ldc_I4_0);
1551 ig.Emit (OpCodes.Ceq);
1554 case Operator.BitComplement:
1556 ig.Emit (OpCodes.Not);
1559 case Operator.AddressOf:
1560 ((LValue)expr).AddressOf (ec);
1563 case Operator.Indirection:
1564 throw new Exception ("Not implemented yet");
1566 case Operator.PreIncrement:
1567 case Operator.PreDecrement:
1568 if (expr.ExprClass == ExprClass.Variable){
1570 // Resolve already verified that it is an "incrementable"
1573 ig.Emit (OpCodes.Ldc_I4_1);
1575 if (oper == Operator.PreDecrement)
1576 ig.Emit (OpCodes.Sub);
1578 ig.Emit (OpCodes.Add);
1579 ig.Emit (OpCodes.Dup);
1580 ((LValue) expr).Store (ec);
1582 throw new Exception ("Handle Indexers and Properties here");
1586 case Operator.PostIncrement:
1587 case Operator.PostDecrement:
1588 if (expr.ExprClass == ExprClass.Variable){
1590 // Resolve already verified that it is an "incrementable"
1593 ig.Emit (OpCodes.Dup);
1594 ig.Emit (OpCodes.Ldc_I4_1);
1596 if (oper == Operator.PostDecrement)
1597 ig.Emit (OpCodes.Sub);
1599 ig.Emit (OpCodes.Add);
1600 ((LValue) expr).Store (ec);
1602 throw new Exception ("Handle Indexers and Properties here");
1607 throw new Exception ("This should not happen: Operator = "
1608 + oper.ToString ());
1613 public override void EmitStatement (EmitContext ec)
1616 // FIXME: we should rewrite this code to generate
1617 // better code for ++ and -- as we know we wont need
1618 // the values on the stack
1621 ec.ig.Emit (OpCodes.Pop);
1625 public class Probe : Expression {
1626 public readonly string ProbeType;
1627 public readonly Operator Oper;
1631 public enum Operator {
1635 public Probe (Operator oper, Expression expr, string probe_type)
1638 ProbeType = probe_type;
1642 public Expression Expr {
1648 public override Expression DoResolve (TypeContainer tc)
1650 probe_type = tc.LookupType (ProbeType, false);
1652 if (probe_type == null)
1655 expr = expr.Resolve (tc);
1657 type = TypeManager.bool_type;
1658 eclass = ExprClass.Value;
1663 public override void Emit (EmitContext ec)
1665 ILGenerator ig = ec.ig;
1669 if (Oper == Operator.Is){
1670 ig.Emit (OpCodes.Isinst, probe_type);
1671 ig.Emit (OpCodes.Ldnull);
1672 ig.Emit (OpCodes.Cgt_Un);
1674 ig.Emit (OpCodes.Isinst, probe_type);
1680 // This represents a typecast in the source language.
1682 // FIXME: Cast expressions have an unusual set of parsing
1683 // rules, we need to figure those out.
1685 public class Cast : Expression {
1689 public Cast (string cast_type, Expression expr)
1691 this.target_type = cast_type;
1695 public string TargetType {
1701 public Expression Expr {
1710 public override Expression DoResolve (TypeContainer tc)
1712 expr = expr.Resolve (tc);
1716 type = tc.LookupType (target_type, false);
1717 eclass = ExprClass.Value;
1722 expr = ConvertExplicit (tc, expr, type);
1727 public override void Emit (EmitContext ec)
1730 // This one will never happen
1732 throw new Exception ("Should not happen");
1736 public class Binary : Expression {
1737 public enum Operator {
1738 Multiply, Division, Modulus,
1739 Addition, Subtraction,
1740 LeftShift, RightShift,
1741 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1742 Equality, Inequality,
1751 Expression left, right;
1753 ArrayList Arguments;
1757 public Binary (Operator oper, Expression left, Expression right, Location loc)
1762 this.location = loc;
1765 public Operator Oper {
1774 public Expression Left {
1783 public Expression Right {
1794 // Returns a stringified representation of the Operator
1799 case Operator.Multiply:
1801 case Operator.Division:
1803 case Operator.Modulus:
1805 case Operator.Addition:
1807 case Operator.Subtraction:
1809 case Operator.LeftShift:
1811 case Operator.RightShift:
1813 case Operator.LessThan:
1815 case Operator.GreaterThan:
1817 case Operator.LessThanOrEqual:
1819 case Operator.GreaterThanOrEqual:
1821 case Operator.Equality:
1823 case Operator.Inequality:
1825 case Operator.BitwiseAnd:
1827 case Operator.BitwiseOr:
1829 case Operator.ExclusiveOr:
1831 case Operator.LogicalOr:
1833 case Operator.LogicalAnd:
1837 return oper.ToString ();
1840 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1842 if (expr.Type == target_type)
1845 return ConvertImplicit (tc, expr, target_type, new Location ("FIXME", 1, 1));
1849 // Note that handling the case l == Decimal || r == Decimal
1850 // is taken care of by the Step 1 Operator Overload resolution.
1852 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1854 if (l == TypeManager.double_type || r == TypeManager.double_type){
1856 // If either operand is of type double, the other operand is
1857 // conveted to type double.
1859 if (r != TypeManager.double_type)
1860 right = ConvertImplicit (tc, right, TypeManager.double_type, location);
1861 if (l != TypeManager.double_type)
1862 left = ConvertImplicit (tc, left, TypeManager.double_type, location);
1864 type = TypeManager.double_type;
1865 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1867 // if either operand is of type float, th eother operand is
1868 // converd to type float.
1870 if (r != TypeManager.double_type)
1871 right = ConvertImplicit (tc, right, TypeManager.float_type, location);
1872 if (l != TypeManager.double_type)
1873 left = ConvertImplicit (tc, left, TypeManager.float_type, location);
1874 type = TypeManager.float_type;
1875 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1877 // If either operand is of type ulong, the other operand is
1878 // converted to type ulong. or an error ocurrs if the other
1879 // operand is of type sbyte, short, int or long
1883 if (l == TypeManager.uint64_type)
1885 else if (r == TypeManager.uint64_type)
1888 if ((other == TypeManager.sbyte_type) ||
1889 (other == TypeManager.short_type) ||
1890 (other == TypeManager.int32_type) ||
1891 (other == TypeManager.int64_type)){
1892 string oper = OperName ();
1894 Error (tc, 34, "Operator `" + OperName ()
1895 + "' is ambiguous on operands of type `"
1896 + TypeManager.CSharpName (l) + "' "
1897 + "and `" + TypeManager.CSharpName (r)
1900 type = TypeManager.uint64_type;
1901 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1903 // If either operand is of type long, the other operand is converted
1906 if (l != TypeManager.int64_type)
1907 left = ConvertImplicit (tc, left, TypeManager.int64_type, location);
1908 if (r != TypeManager.int64_type)
1909 right = ConvertImplicit (tc, right, TypeManager.int64_type, location);
1911 type = TypeManager.int64_type;
1912 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1914 // If either operand is of type uint, and the other
1915 // operand is of type sbyte, short or int, othe operands are
1916 // converted to type long.
1920 if (l == TypeManager.uint32_type)
1922 else if (r == TypeManager.uint32_type)
1925 if ((other == TypeManager.sbyte_type) ||
1926 (other == TypeManager.short_type) ||
1927 (other == TypeManager.int32_type)){
1928 left = ForceConversion (tc, left, TypeManager.int64_type);
1929 right = ForceConversion (tc, right, TypeManager.int64_type);
1930 type = TypeManager.int64_type;
1933 // if either operand is of type uint, the other
1934 // operand is converd to type uint
1936 left = ForceConversion (tc, left, TypeManager.uint32_type);
1937 right = ForceConversion (tc, left, TypeManager.uint32_type);
1938 type = TypeManager.uint32_type;
1940 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1941 if (l != TypeManager.decimal_type)
1942 left = ConvertImplicit (tc, left, TypeManager.decimal_type, location);
1943 if (r != TypeManager.decimal_type)
1944 right = ConvertImplicit (tc, right, TypeManager.decimal_type, location);
1946 type = TypeManager.decimal_type;
1948 left = ForceConversion (tc, left, TypeManager.int32_type);
1949 right = ForceConversion (tc, right, TypeManager.int32_type);
1950 type = TypeManager.int32_type;
1954 void error19 (TypeContainer tc)
1957 "Operator " + OperName () + " cannot be applied to operands of type `" +
1958 TypeManager.CSharpName (left.Type) + "' and `" +
1959 TypeManager.CSharpName (right.Type) + "'");
1963 Expression CheckShiftArguments (TypeContainer tc)
1967 Type r = right.Type;
1969 e = ForceConversion (tc, right, TypeManager.int32_type);
1976 Location loc = location;
1978 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type, loc)) != null) ||
1979 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type, loc)) != null) ||
1980 ((e = ConvertImplicit (tc, left, TypeManager.int64_type, loc)) != null) ||
1981 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type, loc)) != null)){
1990 Expression ResolveOperator (TypeContainer tc)
1993 Type r = right.Type;
1996 // Step 1: Perform Operator Overload location
1998 Expression left_expr, right_expr;
2000 string op = "op_" + oper;
2002 left_expr = MemberLookup (tc, l, op, false);
2004 right_expr = MemberLookup (tc, r, op, false);
2006 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2008 if (union != null) {
2009 Arguments = new ArrayList ();
2010 Arguments.Add (new Argument (left, Argument.AType.Expression));
2011 Arguments.Add (new Argument (right, Argument.AType.Expression));
2013 method = Invocation.OverloadResolve (tc, union, Arguments, location);
2014 if (method != null) {
2015 MethodInfo mi = (MethodInfo) method;
2017 type = mi.ReturnType;
2023 // Step 2: Default operations on CLI native types.
2026 // Only perform numeric promotions on:
2027 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2029 if (oper == Operator.Addition){
2031 // If any of the arguments is a string, cast to string
2033 if (l == TypeManager.string_type){
2034 if (r == TypeManager.string_type){
2036 method = TypeManager.string_concat_string_string;
2039 method = TypeManager.string_concat_object_object;
2040 right = ConvertImplicit (tc, right,
2041 TypeManager.object_type, location);
2043 type = TypeManager.string_type;
2045 Arguments = new ArrayList ();
2046 Arguments.Add (new Argument (left, Argument.AType.Expression));
2047 Arguments.Add (new Argument (right, Argument.AType.Expression));
2051 } else if (r == TypeManager.string_type){
2053 method = TypeManager.string_concat_object_object;
2054 Arguments = new ArrayList ();
2055 Arguments.Add (new Argument (left, Argument.AType.Expression));
2056 Arguments.Add (new Argument (right, Argument.AType.Expression));
2058 left = ConvertImplicit (tc, left, TypeManager.object_type, location);
2059 type = TypeManager.string_type;
2065 // FIXME: is Delegate operator + (D x, D y) handled?
2069 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2070 return CheckShiftArguments (tc);
2072 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2073 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2080 // We are dealing with numbers
2083 DoNumericPromotions (tc, l, r);
2085 if (left == null || right == null)
2088 if (oper == Operator.BitwiseAnd ||
2089 oper == Operator.BitwiseOr ||
2090 oper == Operator.ExclusiveOr){
2091 if (!((l == TypeManager.int32_type) ||
2092 (l == TypeManager.uint32_type) ||
2093 (l == TypeManager.int64_type) ||
2094 (l == TypeManager.uint64_type))){
2100 if (oper == Operator.Equality ||
2101 oper == Operator.Inequality ||
2102 oper == Operator.LessThanOrEqual ||
2103 oper == Operator.LessThan ||
2104 oper == Operator.GreaterThanOrEqual ||
2105 oper == Operator.GreaterThan){
2106 type = TypeManager.bool_type;
2112 public override Expression DoResolve (TypeContainer tc)
2114 left = left.Resolve (tc);
2115 right = right.Resolve (tc);
2117 if (left == null || right == null)
2120 if (left.Type == null)
2121 throw new Exception ("Resolve returned non null, but did not set the type!");
2122 if (right.Type == null)
2123 throw new Exception ("Resolve returned non null, but did not set the type!");
2125 eclass = ExprClass.Value;
2127 return ResolveOperator (tc);
2130 public bool IsBranchable ()
2132 if (oper == Operator.Equality ||
2133 oper == Operator.Inequality ||
2134 oper == Operator.LessThan ||
2135 oper == Operator.GreaterThan ||
2136 oper == Operator.LessThanOrEqual ||
2137 oper == Operator.GreaterThanOrEqual){
2144 // This entry point is used by routines that might want
2145 // to emit a brfalse/brtrue after an expression, and instead
2146 // they could use a more compact notation.
2148 // Typically the code would generate l.emit/r.emit, followed
2149 // by the comparission and then a brtrue/brfalse. The comparissions
2150 // are sometimes inneficient (there are not as complete as the branches
2151 // look for the hacks in Emit using double ceqs).
2153 // So for those cases we provide EmitBranchable that can emit the
2154 // branch with the test
2156 public void EmitBranchable (EmitContext ec, int target)
2159 bool close_target = false;
2165 case Operator.Equality:
2167 opcode = OpCodes.Beq_S;
2169 opcode = OpCodes.Beq;
2172 case Operator.Inequality:
2174 opcode = OpCodes.Bne_Un_S;
2176 opcode = OpCodes.Bne_Un;
2179 case Operator.LessThan:
2181 opcode = OpCodes.Blt_S;
2183 opcode = OpCodes.Blt;
2186 case Operator.GreaterThan:
2188 opcode = OpCodes.Bgt_S;
2190 opcode = OpCodes.Bgt;
2193 case Operator.LessThanOrEqual:
2195 opcode = OpCodes.Ble_S;
2197 opcode = OpCodes.Ble;
2200 case Operator.GreaterThanOrEqual:
2202 opcode = OpCodes.Bge_S;
2204 opcode = OpCodes.Ble;
2208 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2209 + oper.ToString ());
2212 ec.ig.Emit (opcode, target);
2215 public override void Emit (EmitContext ec)
2217 ILGenerator ig = ec.ig;
2219 Type r = right.Type;
2222 if (method != null) {
2224 // Note that operators are static anyway
2226 if (Arguments != null)
2227 Invocation.EmitArguments (ec, method, Arguments);
2229 if (method is MethodInfo)
2230 ig.Emit (OpCodes.Call, (MethodInfo) method);
2232 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2241 case Operator.Multiply:
2243 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2244 opcode = OpCodes.Mul_Ovf;
2245 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2246 opcode = OpCodes.Mul_Ovf_Un;
2248 opcode = OpCodes.Mul;
2250 opcode = OpCodes.Mul;
2254 case Operator.Division:
2255 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2256 opcode = OpCodes.Div_Un;
2258 opcode = OpCodes.Div;
2261 case Operator.Modulus:
2262 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2263 opcode = OpCodes.Rem_Un;
2265 opcode = OpCodes.Rem;
2268 case Operator.Addition:
2270 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2271 opcode = OpCodes.Add_Ovf;
2272 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2273 opcode = OpCodes.Add_Ovf_Un;
2275 opcode = OpCodes.Mul;
2277 opcode = OpCodes.Add;
2280 case Operator.Subtraction:
2282 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2283 opcode = OpCodes.Sub_Ovf;
2284 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2285 opcode = OpCodes.Sub_Ovf_Un;
2287 opcode = OpCodes.Sub;
2289 opcode = OpCodes.Sub;
2292 case Operator.RightShift:
2293 opcode = OpCodes.Shr;
2296 case Operator.LeftShift:
2297 opcode = OpCodes.Shl;
2300 case Operator.Equality:
2301 opcode = OpCodes.Ceq;
2304 case Operator.Inequality:
2305 ec.ig.Emit (OpCodes.Ceq);
2306 ec.ig.Emit (OpCodes.Ldc_I4_0);
2308 opcode = OpCodes.Ceq;
2311 case Operator.LessThan:
2312 opcode = OpCodes.Clt;
2315 case Operator.GreaterThan:
2316 opcode = OpCodes.Cgt;
2319 case Operator.LessThanOrEqual:
2320 ec.ig.Emit (OpCodes.Cgt);
2321 ec.ig.Emit (OpCodes.Ldc_I4_0);
2323 opcode = OpCodes.Ceq;
2326 case Operator.GreaterThanOrEqual:
2327 ec.ig.Emit (OpCodes.Clt);
2328 ec.ig.Emit (OpCodes.Ldc_I4_1);
2330 opcode = OpCodes.Sub;
2333 case Operator.LogicalOr:
2334 case Operator.BitwiseOr:
2335 opcode = OpCodes.Or;
2338 case Operator.LogicalAnd:
2339 case Operator.BitwiseAnd:
2340 opcode = OpCodes.And;
2343 case Operator.ExclusiveOr:
2344 opcode = OpCodes.Xor;
2348 throw new Exception ("This should not happen: Operator = "
2349 + oper.ToString ());
2356 public class Conditional : Expression {
2357 Expression expr, trueExpr, falseExpr;
2360 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2363 this.trueExpr = trueExpr;
2364 this.falseExpr = falseExpr;
2368 public Expression Expr {
2374 public Expression TrueExpr {
2380 public Expression FalseExpr {
2386 public override Expression DoResolve (TypeContainer tc)
2388 expr = expr.Resolve (tc);
2390 if (expr.Type != TypeManager.bool_type)
2391 expr = Expression.ConvertImplicitRequired (
2392 tc, expr, TypeManager.bool_type, l);
2394 trueExpr = trueExpr.Resolve (tc);
2395 falseExpr = falseExpr.Resolve (tc);
2397 if (expr == null || trueExpr == null || falseExpr == null)
2400 if (trueExpr.Type == falseExpr.Type)
2401 type = trueExpr.Type;
2406 // First, if an implicit conversion exists from trueExpr
2407 // to falseExpr, then the result type is of type falseExpr.Type
2409 conv = ConvertImplicit (tc, trueExpr, falseExpr.Type, l);
2411 type = falseExpr.Type;
2413 } else if ((conv = ConvertImplicit (tc,falseExpr,trueExpr.Type,l)) != null){
2414 type = trueExpr.Type;
2417 Error (tc, 173, l, "The type of the conditional expression can " +
2418 "not be computed because there is no implicit conversion" +
2419 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2420 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2425 eclass = ExprClass.Value;
2429 public override void Emit (EmitContext ec)
2431 ILGenerator ig = ec.ig;
2432 Label false_target = ig.DefineLabel ();
2433 Label end_target = ig.DefineLabel ();
2436 ig.Emit (OpCodes.Brfalse, false_target);
2438 ig.Emit (OpCodes.Br, end_target);
2439 ig.MarkLabel (false_target);
2440 falseExpr.Emit (ec);
2441 ig.MarkLabel (end_target);
2445 public class SimpleName : Expression {
2446 public readonly string Name;
2447 public readonly Location Location;
2449 public SimpleName (string name, Location l)
2456 // Checks whether we are trying to access an instance
2457 // property, method or field from a static body.
2459 Expression MemberStaticCheck (Report r, Expression e)
2461 if (e is FieldExpr){
2462 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2466 "An object reference is required " +
2467 "for the non-static field `"+Name+"'");
2470 } else if (e is MethodGroupExpr){
2471 // FIXME: Pending reorganization of MemberLookup
2472 // Basically at this point we should have the
2473 // best match already selected for us, and
2474 // we should only have to check a *single*
2475 // Method for its static on/off bit.
2477 } else if (e is PropertyExpr){
2478 if (!((PropertyExpr) e).IsStatic){
2480 "An object reference is required " +
2481 "for the non-static property access `"+
2491 // 7.5.2: Simple Names.
2493 // Local Variables and Parameters are handled at
2494 // parse time, so they never occur as SimpleNames.
2496 Expression ResolveSimpleName (TypeContainer tc)
2499 Report r = tc.RootContext.Report;
2501 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2505 else if (e is FieldExpr){
2506 FieldExpr fe = (FieldExpr) e;
2508 if (!fe.FieldInfo.IsStatic)
2509 fe.Instance = new This ();
2512 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2513 return MemberStaticCheck (r, e);
2519 // Do step 3 of the Simple Name resolution.
2521 // FIXME: implement me.
2523 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2530 // SimpleName needs to handle a multitude of cases:
2532 // simple_names and qualified_identifiers are placed on
2533 // the tree equally.
2535 public override Expression DoResolve (TypeContainer tc)
2537 if (Name.IndexOf (".") != -1)
2538 return ResolveMemberAccess (tc, Name);
2540 return ResolveSimpleName (tc);
2543 public override void Emit (EmitContext ec)
2545 throw new Exception ("SimpleNames should be gone from the tree");
2550 // A simple interface that should be implemeneted by LValues
2552 public interface LValue {
2555 // The Store method should store the contents of the top
2556 // of the stack into the storage that is implemented by
2557 // the particular implementation of LValue
2559 void Store (EmitContext ec);
2562 // The AddressOf method should generate code that loads
2563 // the address of the LValue and leaves it on the stack
2565 void AddressOf (EmitContext ec);
2568 public class LocalVariableReference : Expression, LValue {
2569 public readonly string Name;
2570 public readonly Block Block;
2572 public LocalVariableReference (Block block, string name)
2576 eclass = ExprClass.Variable;
2579 public VariableInfo VariableInfo {
2581 return Block.GetVariableInfo (Name);
2585 public override Expression DoResolve (TypeContainer tc)
2587 VariableInfo vi = Block.GetVariableInfo (Name);
2589 type = vi.VariableType;
2593 public override void Emit (EmitContext ec)
2595 VariableInfo vi = VariableInfo;
2596 ILGenerator ig = ec.ig;
2603 ig.Emit (OpCodes.Ldloc_0);
2607 ig.Emit (OpCodes.Ldloc_1);
2611 ig.Emit (OpCodes.Ldloc_2);
2615 ig.Emit (OpCodes.Ldloc_3);
2620 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2622 ig.Emit (OpCodes.Ldloc, idx);
2627 public void Store (EmitContext ec)
2629 ILGenerator ig = ec.ig;
2630 VariableInfo vi = VariableInfo;
2636 ig.Emit (OpCodes.Stloc_0);
2640 ig.Emit (OpCodes.Stloc_1);
2644 ig.Emit (OpCodes.Stloc_2);
2648 ig.Emit (OpCodes.Stloc_3);
2653 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2655 ig.Emit (OpCodes.Stloc, idx);
2660 public void AddressOf (EmitContext ec)
2662 VariableInfo vi = VariableInfo;
2669 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2671 ec.ig.Emit (OpCodes.Ldloca, idx);
2675 public class ParameterReference : Expression, LValue {
2676 public readonly Parameters Pars;
2677 public readonly String Name;
2678 public readonly int Idx;
2680 public ParameterReference (Parameters pars, int idx, string name)
2685 eclass = ExprClass.Variable;
2688 public override Expression DoResolve (TypeContainer tc)
2690 Type [] types = Pars.GetParameterInfo (tc);
2697 public override void Emit (EmitContext ec)
2700 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
2702 ec.ig.Emit (OpCodes.Ldarg, Idx);
2705 public void Store (EmitContext ec)
2708 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
2710 ec.ig.Emit (OpCodes.Starg, Idx);
2714 public void AddressOf (EmitContext ec)
2717 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
2719 ec.ig.Emit (OpCodes.Ldarga, Idx);
2724 // Used for arguments to New(), Invocation()
2726 public class Argument {
2733 public readonly AType Type;
2736 public Argument (Expression expr, AType type)
2742 public Expression Expr {
2752 public bool Resolve (TypeContainer tc)
2754 expr = expr.Resolve (tc);
2756 return expr != null;
2759 public void Emit (EmitContext ec)
2766 // Invocation of methods or delegates.
2768 public class Invocation : ExpressionStatement {
2769 public readonly ArrayList Arguments;
2770 public readonly Location Location;
2773 MethodBase method = null;
2775 static Hashtable method_parameter_cache;
2777 static Invocation ()
2779 method_parameter_cache = new Hashtable ();
2783 // arguments is an ArrayList, but we do not want to typecast,
2784 // as it might be null.
2786 // FIXME: only allow expr to be a method invocation or a
2787 // delegate invocation (7.5.5)
2789 public Invocation (Expression expr, ArrayList arguments, Location l)
2792 Arguments = arguments;
2796 public Expression Expr {
2803 // Returns the Parameters (a ParameterData interface) for the
2806 static ParameterData GetParameterData (MethodBase mb)
2808 object pd = method_parameter_cache [mb];
2811 return (ParameterData) pd;
2813 if (mb is MethodBuilder || mb is ConstructorBuilder){
2814 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2816 InternalParameters ip = mc.ParameterInfo;
2817 method_parameter_cache [mb] = ip;
2819 return (ParameterData) ip;
2821 ParameterInfo [] pi = mb.GetParameters ();
2822 ReflectionParameters rp = new ReflectionParameters (pi);
2823 method_parameter_cache [mb] = rp;
2825 return (ParameterData) rp;
2830 // Tells whether a user defined conversion from Type `from' to
2831 // Type `to' exists.
2833 // FIXME: we could implement a cache here.
2835 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2837 // Locate user-defined implicit operators
2841 mg = MemberLookup (tc, to, "op_Implicit", false);
2844 MethodGroupExpr me = (MethodGroupExpr) mg;
2846 for (int i = me.Methods.Length; i > 0;) {
2848 MethodBase mb = me.Methods [i];
2849 ParameterData pd = GetParameterData (mb);
2851 if (from == pd.ParameterType (0))
2856 mg = MemberLookup (tc, from, "op_Implicit", false);
2859 MethodGroupExpr me = (MethodGroupExpr) mg;
2861 for (int i = me.Methods.Length; i > 0;) {
2863 MethodBase mb = me.Methods [i];
2864 MethodInfo mi = (MethodInfo) mb;
2866 if (mi.ReturnType == to)
2875 // Determines "better conversion" as specified in 7.4.2.3
2876 // Returns : 1 if a->p is better
2877 // 0 if a->q or neither is better
2879 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q, bool use_standard)
2882 Type argument_type = a.Expr.Type;
2883 Expression argument_expr = a.Expr;
2885 if (argument_type == null)
2886 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2891 if (argument_type == p)
2894 if (argument_type == q)
2898 // Now probe whether an implicit constant expression conversion
2901 // An implicit constant expression conversion permits the following
2904 // * A constant-expression of type `int' can be converted to type
2905 // sbyte, byute, short, ushort, uint, ulong provided the value of
2906 // of the expression is withing the range of the destination type.
2908 // * A constant-expression of type long can be converted to type
2909 // ulong, provided the value of the constant expression is not negative
2911 // FIXME: Note that this assumes that constant folding has
2912 // taken place. We dont do constant folding yet.
2915 if (argument_expr is IntLiteral){
2916 IntLiteral ei = (IntLiteral) argument_expr;
2917 int value = ei.Value;
2919 if (p == TypeManager.sbyte_type){
2920 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2922 } else if (p == TypeManager.byte_type){
2923 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2925 } else if (p == TypeManager.short_type){
2926 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2928 } else if (p == TypeManager.ushort_type){
2929 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2931 } else if (p == TypeManager.uint32_type){
2933 // we can optimize this case: a positive int32
2934 // always fits on a uint32
2938 } else if (p == TypeManager.uint64_type){
2940 // we can optimize this case: a positive int32
2941 // always fits on a uint64
2946 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2947 LongLiteral ll = (LongLiteral) argument_expr;
2949 if (p == TypeManager.uint64_type){
2960 tmp = ConvertImplicitStandard (tc, argument_expr, p, Location.Null);
2962 tmp = ConvertImplicit (tc, argument_expr, p, Location.Null);
2971 if (ConversionExists (tc, p, q) == true &&
2972 ConversionExists (tc, q, p) == false)
2975 if (p == TypeManager.sbyte_type)
2976 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2977 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2980 if (p == TypeManager.short_type)
2981 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2982 q == TypeManager.uint64_type)
2985 if (p == TypeManager.int32_type)
2986 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2989 if (p == TypeManager.int64_type)
2990 if (q == TypeManager.uint64_type)
2997 // Determines "Better function" and returns an integer indicating :
2998 // 0 if candidate ain't better
2999 // 1 if candidate is better than the current best match
3001 static int BetterFunction (TypeContainer tc, ArrayList args,
3002 MethodBase candidate, MethodBase best,
3005 ParameterData candidate_pd = GetParameterData (candidate);
3006 ParameterData best_pd;
3012 argument_count = args.Count;
3014 if (candidate_pd.Count == 0 && argument_count == 0)
3018 if (candidate_pd.Count == argument_count) {
3020 for (int j = argument_count; j > 0;) {
3023 Argument a = (Argument) args [j];
3025 x = BetterConversion (
3026 tc, a, candidate_pd.ParameterType (j), null,
3042 best_pd = GetParameterData (best);
3044 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3045 int rating1 = 0, rating2 = 0;
3047 for (int j = argument_count; j > 0;) {
3051 Argument a = (Argument) args [j];
3053 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
3054 best_pd.ParameterType (j), use_standard);
3055 y = BetterConversion (tc, a, best_pd.ParameterType (j),
3056 candidate_pd.ParameterType (j), use_standard);
3062 if (rating1 > rating2)
3071 public static string FullMethodDesc (MethodBase mb)
3073 StringBuilder sb = new StringBuilder (mb.Name);
3074 ParameterData pd = GetParameterData (mb);
3077 for (int i = pd.Count; i > 0;) {
3079 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
3085 return sb.ToString ();
3088 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3091 if (mg1 != null || mg2 != null) {
3093 MethodGroupExpr left_set = null, right_set = null;
3094 int length1 = 0, length2 = 0;
3097 left_set = (MethodGroupExpr) mg1;
3098 length1 = left_set.Methods.Length;
3102 right_set = (MethodGroupExpr) mg2;
3103 length2 = right_set.Methods.Length;
3106 MemberInfo [] miset = new MemberInfo [length1 + length2];
3107 if (left_set != null)
3108 left_set.Methods.CopyTo (miset, 0);
3109 if (right_set != null)
3110 right_set.Methods.CopyTo (miset, length1);
3112 MethodGroupExpr union = new MethodGroupExpr (miset);
3123 // Find the Applicable Function Members (7.4.2.1)
3125 // me: Method Group expression with the members to select.
3126 // it might contain constructors or methods (or anything
3127 // that maps to a method).
3129 // Arguments: ArrayList containing resolved Argument objects.
3131 // loc: The location if we want an error to be reported, or a Null
3132 // location for "probing" purposes.
3134 // inside_user_defined: controls whether OverloadResolve should use the
3135 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
3137 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3138 // that is the best match of me on Arguments.
3141 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3142 ArrayList Arguments, Location loc,
3145 ArrayList afm = new ArrayList ();
3146 int best_match_idx = -1;
3147 MethodBase method = null;
3150 for (int i = me.Methods.Length; i > 0; ){
3152 MethodBase candidate = me.Methods [i];
3155 x = BetterFunction (tc, Arguments, candidate, method, use_standard);
3161 method = me.Methods [best_match_idx];
3165 if (Arguments == null)
3168 argument_count = Arguments.Count;
3172 // Now we see if we can at least find a method with the same number of arguments
3173 // and then try doing implicit conversion on the arguments
3174 if (best_match_idx == -1) {
3176 for (int i = me.Methods.Length; i > 0;) {
3178 MethodBase mb = me.Methods [i];
3179 pd = GetParameterData (mb);
3181 if (pd.Count == argument_count) {
3183 method = me.Methods [best_match_idx];
3194 // And now convert implicitly, each argument to the required type
3196 pd = GetParameterData (method);
3198 for (int j = argument_count; j > 0;) {
3200 Argument a = (Argument) Arguments [j];
3201 Expression a_expr = a.Expr;
3202 Type parameter_type = pd.ParameterType (j);
3204 if (a_expr.Type != parameter_type){
3208 conv = ConvertImplicitStandard (tc, a_expr, parameter_type,
3211 conv = ConvertImplicit (tc, a_expr, parameter_type,
3215 if (!Location.IsNull (loc)) {
3216 Error (tc, 1502, loc,
3217 "The best overloaded match for method '" + FullMethodDesc (method) +
3218 "' has some invalid arguments");
3219 Error (tc, 1503, loc,
3220 "Argument " + (j+1) +
3221 ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
3222 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
3227 // Update the argument with the implicit conversion
3237 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3238 ArrayList Arguments, Location loc)
3240 return OverloadResolve (tc, me, Arguments, loc, false);
3243 public override Expression DoResolve (TypeContainer tc)
3246 // First, resolve the expression that is used to
3247 // trigger the invocation
3249 this.expr = expr.Resolve (tc);
3250 if (this.expr == null)
3253 if (!(this.expr is MethodGroupExpr)){
3254 report118 (tc, this.expr, "method group");
3259 // Next, evaluate all the expressions in the argument list
3261 if (Arguments != null){
3262 for (int i = Arguments.Count; i > 0;){
3264 Argument a = (Argument) Arguments [i];
3266 if (!a.Resolve (tc))
3271 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments,
3274 if (method == null){
3275 Error (tc, -6, Location,
3276 "Could not find any applicable function for this argument list");
3280 if (method is MethodInfo)
3281 type = ((MethodInfo)method).ReturnType;
3283 eclass = ExprClass.Value;
3287 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3291 if (Arguments != null)
3292 top = Arguments.Count;
3296 for (int i = 0; i < top; i++){
3297 Argument a = (Argument) Arguments [i];
3303 public override void Emit (EmitContext ec)
3305 bool is_static = method.IsStatic;
3308 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3311 // If this is ourselves, push "this"
3313 if (mg.InstanceExpression == null){
3314 ec.ig.Emit (OpCodes.Ldarg_0);
3317 // Push the instance expression
3319 mg.InstanceExpression.Emit (ec);
3323 if (Arguments != null)
3324 EmitArguments (ec, method, Arguments);
3327 if (method is MethodInfo)
3328 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3330 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3332 if (method is MethodInfo)
3333 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3335 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3339 public override void EmitStatement (EmitContext ec)
3344 // Pop the return value if there is one
3346 if (method is MethodInfo){
3347 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3348 ec.ig.Emit (OpCodes.Pop);
3353 public class New : ExpressionStatement {
3360 public readonly NType NewType;
3361 public readonly ArrayList Arguments;
3362 public readonly string RequestedType;
3363 // These are for the case when we have an array
3364 public readonly string Rank;
3365 public readonly ArrayList Indices;
3366 public readonly ArrayList Initializers;
3369 MethodBase method = null;
3371 public New (string requested_type, ArrayList arguments, Location loc)
3373 RequestedType = requested_type;
3374 Arguments = arguments;
3375 NewType = NType.Object;
3379 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3381 RequestedType = requested_type;
3384 Initializers = initializers;
3385 NewType = NType.Array;
3389 public override Expression DoResolve (TypeContainer tc)
3391 type = tc.LookupType (RequestedType, false);
3398 ml = MemberLookup (tc, type, ".ctor", false,
3399 MemberTypes.Constructor, AllBindingsFlags);
3401 if (! (ml is MethodGroupExpr)){
3403 // FIXME: Find proper error
3405 report118 (tc, ml, "method group");
3409 if (Arguments != null){
3410 for (int i = Arguments.Count; i > 0;){
3412 Argument a = (Argument) Arguments [i];
3414 if (!a.Resolve (tc))
3419 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments,
3422 if (method == null) {
3423 Error (tc, -6, Location,
3424 "New invocation: Can not find a constructor for this argument list");
3428 eclass = ExprClass.Value;
3432 public override void Emit (EmitContext ec)
3434 Invocation.EmitArguments (ec, method, Arguments);
3435 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3438 public override void EmitStatement (EmitContext ec)
3441 ec.ig.Emit (OpCodes.Pop);
3446 // Represents the `this' construct
3448 public class This : Expression, LValue {
3449 public override Expression DoResolve (TypeContainer tc)
3451 eclass = ExprClass.Variable;
3452 type = tc.TypeBuilder;
3455 // FIXME: Verify that this is only used in instance contexts.
3460 public override void Emit (EmitContext ec)
3462 ec.ig.Emit (OpCodes.Ldarg_0);
3465 public void Store (EmitContext ec)
3468 // Assignment to the "this" variable.
3470 // FIXME: Apparently this is a bug that we
3471 // must catch as `this' seems to be readonly ;-)
3473 ec.ig.Emit (OpCodes.Starg, 0);
3476 public void AddressOf (EmitContext ec)
3478 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3482 public class TypeOf : Expression {
3483 public readonly string QueriedType;
3485 public TypeOf (string queried_type)
3487 QueriedType = queried_type;
3490 public override Expression DoResolve (TypeContainer tc)
3492 type = tc.LookupType (QueriedType, false);
3497 eclass = ExprClass.Type;
3501 public override void Emit (EmitContext ec)
3503 throw new Exception ("Implement me");
3504 // FIXME: Implement.
3508 public class SizeOf : Expression {
3509 public readonly string QueriedType;
3511 public SizeOf (string queried_type)
3513 this.QueriedType = queried_type;
3516 public override Expression DoResolve (TypeContainer tc)
3518 // FIXME: Implement;
3519 throw new Exception ("Unimplemented");
3523 public override void Emit (EmitContext ec)
3525 throw new Exception ("Implement me");
3529 public class MemberAccess : Expression {
3530 public readonly string Identifier;
3532 Expression member_lookup;
3534 public MemberAccess (Expression expr, string id)
3540 public Expression Expr {
3546 public override Expression DoResolve (TypeContainer tc)
3548 Expression new_expression = expr.Resolve (tc);
3550 if (new_expression == null)
3553 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
3555 if (member_lookup is MethodGroupExpr){
3556 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3559 // Bind the instance expression to it
3561 // FIXME: This is a horrible way of detecting if it is
3562 // an instance expression. Figure out how to fix this.
3565 if (expr is LocalVariableReference ||
3566 expr is ParameterReference ||
3568 mg.InstanceExpression = expr;
3570 return member_lookup;
3571 } else if (member_lookup is FieldExpr){
3572 FieldExpr fe = (FieldExpr) member_lookup;
3576 return member_lookup;
3579 // FIXME: This should generate the proper node
3580 // ie, for a Property Access, it should like call it
3583 return member_lookup;
3586 public override void Emit (EmitContext ec)
3588 throw new Exception ("Should not happen I think");
3594 // Nodes of type Namespace are created during the semantic
3595 // analysis to resolve member_access/qualified_identifier/simple_name
3598 // They are born `resolved'.
3600 public class NamespaceExpr : Expression {
3601 public readonly string Name;
3603 public NamespaceExpr (string name)
3606 eclass = ExprClass.Namespace;
3609 public override Expression DoResolve (TypeContainer tc)
3614 public override void Emit (EmitContext ec)
3616 throw new Exception ("Namespace expressions should never be emitted");
3621 // Fully resolved expression that evaluates to a type
3623 public class TypeExpr : Expression {
3624 public TypeExpr (Type t)
3627 eclass = ExprClass.Type;
3630 override public Expression DoResolve (TypeContainer tc)
3635 override public void Emit (EmitContext ec)
3637 throw new Exception ("Implement me");
3642 // MethodGroup Expression.
3644 // This is a fully resolved expression that evaluates to a type
3646 public class MethodGroupExpr : Expression {
3647 public readonly MethodBase [] Methods;
3648 Expression instance_expression = null;
3650 public MethodGroupExpr (MemberInfo [] mi)
3652 Methods = new MethodBase [mi.Length];
3653 mi.CopyTo (Methods, 0);
3654 eclass = ExprClass.MethodGroup;
3658 // `A method group may have associated an instance expression'
3660 public Expression InstanceExpression {
3662 return instance_expression;
3666 instance_expression = value;
3670 override public Expression DoResolve (TypeContainer tc)
3675 override public void Emit (EmitContext ec)
3677 throw new Exception ("This should never be reached");
3681 // Fully resolved expression that evaluates to a Field
3683 public class FieldExpr : Expression, LValue {
3684 public readonly FieldInfo FieldInfo;
3685 public Expression Instance;
3687 public FieldExpr (FieldInfo fi)
3690 eclass = ExprClass.Variable;
3691 type = fi.FieldType;
3694 override public Expression DoResolve (TypeContainer tc)
3696 if (!FieldInfo.IsStatic){
3697 if (Instance == null){
3698 throw new Exception ("non-static FieldExpr without instance var\n" +
3699 "You have to assign the Instance variable\n" +
3700 "Of the FieldExpr to set this\n");
3703 Instance = Instance.Resolve (tc);
3704 if (Instance == null)
3711 override public void Emit (EmitContext ec)
3713 ILGenerator ig = ec.ig;
3715 if (FieldInfo.IsStatic)
3716 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3720 ig.Emit (OpCodes.Ldfld, FieldInfo);
3724 public void Store (EmitContext ec)
3726 if (FieldInfo.IsStatic)
3727 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3729 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3732 public void AddressOf (EmitContext ec)
3734 if (FieldInfo.IsStatic)
3735 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3738 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3744 // Fully resolved expression that evaluates to a Property
3746 public class PropertyExpr : Expression {
3747 public readonly PropertyInfo PropertyInfo;
3748 public readonly bool IsStatic;
3750 public PropertyExpr (PropertyInfo pi)
3753 eclass = ExprClass.PropertyAccess;
3756 MethodBase [] acc = pi.GetAccessors ();
3758 for (int i = 0; i < acc.Length; i++)
3759 if (acc [i].IsStatic)
3762 type = pi.PropertyType;
3765 override public Expression DoResolve (TypeContainer tc)
3767 // We are born in resolved state.
3771 override public void Emit (EmitContext ec)
3773 // FIXME: Implement;
3774 throw new Exception ("Unimplemented");
3779 // Fully resolved expression that evaluates to a Expression
3781 public class EventExpr : Expression {
3782 public readonly EventInfo EventInfo;
3784 public EventExpr (EventInfo ei)
3787 eclass = ExprClass.EventAccess;
3790 override public Expression DoResolve (TypeContainer tc)
3792 // We are born in resolved state.
3796 override public void Emit (EmitContext ec)
3798 throw new Exception ("Implement me");
3799 // FIXME: Implement.
3803 public class CheckedExpr : Expression {
3805 public Expression Expr;
3807 public CheckedExpr (Expression e)
3812 public override Expression DoResolve (TypeContainer tc)
3814 Expr = Expr.Resolve (tc);
3819 eclass = Expr.ExprClass;
3824 public override void Emit (EmitContext ec)
3826 bool last_check = ec.CheckState;
3828 ec.CheckState = true;
3830 ec.CheckState = last_check;
3835 public class UnCheckedExpr : Expression {
3837 public Expression Expr;
3839 public UnCheckedExpr (Expression e)
3844 public override Expression DoResolve (TypeContainer tc)
3846 Expr = Expr.Resolve (tc);
3851 eclass = Expr.ExprClass;
3856 public override void Emit (EmitContext ec)
3858 bool last_check = ec.CheckState;
3860 ec.CheckState = false;
3862 ec.CheckState = last_check;
3867 public class ElementAccess : Expression {
3869 public readonly ArrayList Arguments;
3870 public readonly Expression Expr;
3872 public ElementAccess (Expression e, ArrayList e_list)
3878 public override Expression DoResolve (TypeContainer tc)
3880 // FIXME: Implement;
3881 throw new Exception ("Unimplemented");
3885 public override void Emit (EmitContext ec)
3887 // FIXME : Implement !
3888 throw new Exception ("Unimplemented");
3893 public class BaseAccess : Expression {
3895 public enum BaseAccessType {
3900 public readonly BaseAccessType BAType;
3901 public readonly string Member;
3902 public readonly ArrayList Arguments;
3904 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3912 public override Expression DoResolve (TypeContainer tc)
3914 // FIXME: Implement;
3915 throw new Exception ("Unimplemented");
3919 public override void Emit (EmitContext ec)
3921 throw new Exception ("Unimplemented");
3925 public class UserImplicitCast : Expression {
3927 ArrayList arguments;
3929 public UserImplicitCast (MethodInfo method, ArrayList arguments)
3931 this.method = method;
3932 this.arguments = arguments;
3933 type = method.ReturnType;
3934 eclass = ExprClass.Value;
3937 public override Expression DoResolve (TypeContainer tc)
3940 // We are born in a fully resolved state
3945 public override void Emit (EmitContext ec)
3947 ILGenerator ig = ec.ig;
3949 // Note that operators are static anyway
3951 if (arguments != null)
3952 Invocation.EmitArguments (ec, method, arguments);
3954 if (method is MethodInfo)
3955 ig.Emit (OpCodes.Call, (MethodInfo) method);
3957 ig.Emit (OpCodes.Call, (ConstructorInfo) method);