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 Resolve (TypeContainer tc);
125 // Emits the code for the expression
130 // The Emit method is invoked to generate the code
131 // for the expression.
134 public abstract void Emit (EmitContext ec);
137 // Protected constructor. Only derivate types should
138 // be able to be created
141 protected Expression ()
143 eclass = ExprClass.Invalid;
148 // Returns a fully formed expression after a MemberLookup
150 static Expression ExprClassFromMemberInfo (MemberInfo mi)
152 if (mi is EventInfo){
153 return new EventExpr ((EventInfo) mi);
154 } else if (mi is FieldInfo){
155 return new FieldExpr ((FieldInfo) mi);
156 } else if (mi is PropertyInfo){
157 return new PropertyExpr ((PropertyInfo) mi);
158 } else if (mi is Type)
159 return new TypeExpr ((Type) mi);
165 // FIXME: Probably implement a cache for (t,name,current_access_set)?
167 // FIXME: We need to cope with access permissions here, or this wont
170 // This code could use some optimizations, but we need to do some
171 // measurements. For example, we could use a delegate to `flag' when
172 // something can not any longer be a method-group (because it is something
176 // If the return value is an Array, then it is an array of
179 // If the return value is an MemberInfo, it is anything, but a Method
183 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
184 // the arguments here and have MemberLookup return only the methods that
185 // match the argument count/type, unlike we are doing now (we delay this
188 // This is so we can catch correctly attempts to invoke instance methods
189 // from a static body (scan for error 120 in ResolveSimpleName).
191 public static Expression MemberLookup (TypeContainer tc, Type t, string name,
192 bool same_type, MemberTypes mt, BindingFlags bf)
195 bf |= BindingFlags.NonPublic;
197 MemberInfo [] mi = tc.RootContext.TypeManager.FindMembers (
198 t, mt, bf, Type.FilterName, name);
203 if (mi.Length == 1 && !(mi [0] is MethodBase))
204 return Expression.ExprClassFromMemberInfo (mi [0]);
206 for (int i = 0; i < mi.Length; i++)
207 if (!(mi [i] is MethodBase)){
209 -5, "Do not know how to reproduce this case: " +
210 "Methods and non-Method with the same name, " +
211 "report this please");
213 for (i = 0; i < mi.Length; i++){
214 Type tt = mi [i].GetType ();
216 Console.WriteLine (i + ": " + mi [i]);
217 while (tt != TypeManager.object_type){
218 Console.WriteLine (tt);
224 return new MethodGroupExpr (mi);
227 public const MemberTypes AllMemberTypes =
228 MemberTypes.Constructor |
232 MemberTypes.NestedType |
233 MemberTypes.Property;
235 public const BindingFlags AllBindingsFlags =
236 BindingFlags.Public |
237 BindingFlags.Static |
238 BindingFlags.Instance;
240 public static Expression MemberLookup (TypeContainer tc, Type t, string name,
243 return MemberLookup (tc, t, name, same_type, AllMemberTypes, AllBindingsFlags);
247 // I am in general unhappy with this implementation.
249 // I need to revise this.
251 static public Expression ResolveMemberAccess (TypeContainer tc, string name)
253 Expression left_e = null;
254 int dot_pos = name.LastIndexOf (".");
255 string left = name.Substring (0, dot_pos);
256 string right = name.Substring (dot_pos + 1);
259 if ((t = tc.LookupType (left, false)) != null)
260 left_e = new TypeExpr (t);
266 // T.P Static property access (P) on Type T.
267 // e.P instance property access on instance e for P.
273 Error (tc, 246, "Can not find type or namespace `"+left+"'");
277 switch (left_e.ExprClass){
279 return MemberLookup (tc,
281 left_e.Type == tc.TypeBuilder);
283 case ExprClass.Namespace:
284 case ExprClass.PropertyAccess:
285 case ExprClass.IndexerAccess:
286 case ExprClass.Variable:
287 case ExprClass.Value:
288 case ExprClass.Nothing:
289 case ExprClass.EventAccess:
290 case ExprClass.MethodGroup:
291 case ExprClass.Invalid:
292 throw new Exception ("Should have got the " + left_e.ExprClass +
299 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
301 Type expr_type = expr.Type;
303 if (target_type == TypeManager.object_type) {
304 if (expr_type.IsClass)
305 return new EmptyCast (expr, target_type);
306 if (expr_type.IsValueType)
307 return new BoxedCast (expr);
308 } else if (expr_type.IsSubclassOf (target_type))
309 return new EmptyCast (expr, target_type);
311 // FIXME: missing implicit reference conversions:
313 // from any class-type S to any interface-type T.
314 // from any interface type S to interface-type T.
315 // from an array-type S to an array-type of type T
316 // from an array-type to System.Array
317 // from any delegate type to System.Delegate
318 // from any array-type or delegate type into System.ICloneable.
319 // from the null type to any reference-type.
327 // Handles expressions like this: decimal d; d = 1;
328 // and changes them into: decimal d; d = new System.Decimal (1);
330 static Expression InternalTypeConstructor (TypeContainer tc, Expression expr, Type target)
332 ArrayList args = new ArrayList ();
334 args.Add (new Argument (expr, Argument.AType.Expression));
336 Expression ne = new New (target.FullName, args,
337 new Location ("FIXME", 1, 1));
339 return ne.Resolve (tc);
342 static int level = 0;
344 static Hashtable conversion_cache;
346 // Converts implicitly the resolved expression `expr' into the
347 // `target_type'. It returns a new expression that can be used
348 // in a context that expects a `target_type'.
350 static public Expression ConvertImplicit (TypeContainer tc, Expression expr,
353 Type expr_type = expr.Type;
355 Console.WriteLine ("ConvertImplicit " + expr_type + " => " + target_type);
357 if (expr_type == target_type)
361 if (conversion_cache == null)
362 conversion_cache = new Hashtable ();
364 Expression conv = (Expression) conversion_cache [expr_type + "=>" + target_type];
370 // Step 1: Built-in conversions.
372 if (expr_type == TypeManager.sbyte_type){
374 // From sbyte to short, int, long, float, double.
376 if (target_type == TypeManager.int32_type)
377 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
378 if (target_type == TypeManager.int64_type)
379 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
380 if (target_type == TypeManager.double_type)
381 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
382 if (target_type == TypeManager.float_type)
383 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
384 if (target_type == TypeManager.short_type)
385 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
386 if (target_type == TypeManager.decimal_type)
387 return InternalTypeConstructor (tc, expr, target_type);
388 } else if (expr_type == TypeManager.byte_type){
390 // From byte to short, ushort, int, uint, long, ulong, float, double
392 if ((target_type == TypeManager.short_type) ||
393 (target_type == TypeManager.ushort_type) ||
394 (target_type == TypeManager.int32_type) ||
395 (target_type == TypeManager.uint32_type))
396 return new EmptyCast (expr, target_type);
398 if (target_type == TypeManager.uint64_type)
399 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
400 if (target_type == TypeManager.int64_type)
401 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
403 if (target_type == TypeManager.float_type)
404 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
405 if (target_type == TypeManager.double_type)
406 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
407 if (target_type == TypeManager.decimal_type)
408 return InternalTypeConstructor (tc, expr, target_type);
409 } else if (expr_type == TypeManager.short_type){
411 // From short to int, long, float, double
413 if (target_type == TypeManager.int32_type)
414 return new EmptyCast (expr, target_type);
415 if (target_type == TypeManager.int64_type)
416 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
417 if (target_type == TypeManager.double_type)
418 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
419 if (target_type == TypeManager.float_type)
420 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
421 if (target_type == TypeManager.decimal_type)
422 return InternalTypeConstructor (tc, expr, target_type);
423 } else if (expr_type == TypeManager.ushort_type){
425 // From ushort to int, uint, long, ulong, float, double
427 if ((target_type == TypeManager.uint32_type) ||
428 (target_type == TypeManager.uint64_type))
429 return new EmptyCast (expr, target_type);
431 if (target_type == TypeManager.int32_type)
432 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
433 if (target_type == TypeManager.int64_type)
434 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
435 if (target_type == TypeManager.double_type)
436 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
437 if (target_type == TypeManager.float_type)
438 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
439 if (target_type == TypeManager.decimal_type)
440 return InternalTypeConstructor (tc, expr, target_type);
441 } else if (expr_type == TypeManager.int32_type){
443 // From int to long, float, double
445 if (target_type == TypeManager.int64_type)
446 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
447 if (target_type == TypeManager.double_type)
448 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
449 if (target_type == TypeManager.float_type)
450 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
451 if (target_type == TypeManager.decimal_type)
452 return InternalTypeConstructor (tc, expr, target_type);
453 } else if (expr_type == TypeManager.uint32_type){
455 // From uint to long, ulong, float, double
457 if (target_type == TypeManager.int64_type)
458 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
459 if (target_type == TypeManager.uint64_type)
460 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
461 if (target_type == TypeManager.double_type)
462 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
464 if (target_type == TypeManager.float_type)
465 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
467 if (target_type == TypeManager.decimal_type)
468 return InternalTypeConstructor (tc, expr, target_type);
469 } else if ((expr_type == TypeManager.uint64_type) ||
470 (expr_type == TypeManager.int64_type)){
472 // From long to float, double
474 if (target_type == TypeManager.double_type)
475 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
477 if (target_type == TypeManager.float_type)
478 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
480 if (target_type == TypeManager.decimal_type)
481 return InternalTypeConstructor (tc, expr, target_type);
482 } else if (expr_type == TypeManager.char_type){
484 // From char to ushort, int, uint, long, ulong, float, double
486 if ((target_type == TypeManager.ushort_type) ||
487 (target_type == TypeManager.int32_type) ||
488 (target_type == TypeManager.uint32_type))
489 return new EmptyCast (expr, target_type);
490 if (target_type == TypeManager.uint64_type)
491 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
492 if (target_type == TypeManager.int64_type)
493 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
494 if (target_type == TypeManager.float_type)
495 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
496 if (target_type == TypeManager.double_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
498 if (target_type == TypeManager.decimal_type)
499 return InternalTypeConstructor (tc, expr, target_type);
504 e = ImplicitReferenceConversion (expr, target_type);
510 e = UserImplicitCast.CanConvert (tc, expr, target_type);
515 if (conversion_cache == null)
516 conversion_cache = new Hashtable ();
518 conversion_cache.Add (expr_type.ToString () + "=>" + target_type.ToString (), e);
522 // Could not find an implicit cast.
528 // Attemps to perform an implict constant conversion of the IntLiteral
529 // into a different data type using casts (See Implicit Constant
530 // Expression Conversions)
532 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
534 int value = il.Value;
536 if (target_type == TypeManager.sbyte_type){
537 if (value >= SByte.MinValue && value <= SByte.MaxValue)
539 } else if (target_type == TypeManager.byte_type){
540 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
542 } else if (target_type == TypeManager.short_type){
543 if (value >= Int16.MinValue && value <= Int16.MaxValue)
545 } else if (target_type == TypeManager.ushort_type){
546 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
548 } else if (target_type == TypeManager.uint32_type){
550 // we can optimize this case: a positive int32
551 // always fits on a uint32
555 } else if (target_type == TypeManager.uint64_type){
557 // we can optimize this case: a positive int32
558 // always fits on a uint64. But we need an opcode
562 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
569 // Attemptes to implicityly convert `target' into `type', using
570 // ConvertImplicit. If there is no implicit conversion, then
571 // an error is signaled
573 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
574 Type type, Location l)
578 e = ConvertImplicit (tc, target, type);
583 // Attempt to do the implicit constant expression conversions
585 if (target is IntLiteral){
586 e = TryImplicitIntConversion (type, (IntLiteral) target);
589 } else if (target is LongLiteral){
591 // Try the implicit constant expression conversion
592 // from long to ulong, instead of a nice routine,
595 if (((LongLiteral) target).Value > 0)
599 string msg = "Can not convert implicitly from `"+
600 TypeManager.CSharpName (target.Type) + "' to `" +
601 TypeManager.CSharpName (type) + "'";
603 Error (tc, 29, l, msg);
609 // Performs the explicit numeric conversions
611 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
614 Type expr_type = expr.Type;
616 if (expr_type == TypeManager.sbyte_type){
618 // From sbyte to byte, ushort, uint, ulong, char
620 if (target_type == TypeManager.byte_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
622 if (target_type == TypeManager.ushort_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
624 if (target_type == TypeManager.uint32_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
626 if (target_type == TypeManager.uint64_type)
627 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
628 if (target_type == TypeManager.char_type)
629 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
630 } else if (expr_type == TypeManager.byte_type){
632 // From byte to sbyte and char
634 if (target_type == TypeManager.sbyte_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
636 if (target_type == TypeManager.char_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
638 } else if (expr_type == TypeManager.short_type){
640 // From short to sbyte, byte, ushort, uint, ulong, char
642 if (target_type == TypeManager.sbyte_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
644 if (target_type == TypeManager.byte_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
646 if (target_type == TypeManager.ushort_type)
647 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
648 if (target_type == TypeManager.uint32_type)
649 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
650 if (target_type == TypeManager.uint64_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
652 if (target_type == TypeManager.char_type)
653 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
654 } else if (expr_type == TypeManager.ushort_type){
656 // From ushort to sbyte, byte, short, char
658 if (target_type == TypeManager.sbyte_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
660 if (target_type == TypeManager.byte_type)
661 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
662 if (target_type == TypeManager.short_type)
663 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
664 if (target_type == TypeManager.char_type)
665 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
666 } else if (expr_type == TypeManager.int32_type){
668 // From int to sbyte, byte, short, ushort, uint, ulong, char
670 if (target_type == TypeManager.sbyte_type)
671 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
672 if (target_type == TypeManager.byte_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
674 if (target_type == TypeManager.short_type)
675 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
676 if (target_type == TypeManager.ushort_type)
677 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
678 if (target_type == TypeManager.uint32_type)
679 return new EmptyCast (expr, target_type);
680 if (target_type == TypeManager.uint64_type)
681 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
682 if (target_type == TypeManager.char_type)
683 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
684 } else if (expr_type == TypeManager.uint32_type){
686 // From uint to sbyte, byte, short, ushort, int, char
688 if (target_type == TypeManager.sbyte_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
690 if (target_type == TypeManager.byte_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
692 if (target_type == TypeManager.short_type)
693 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
694 if (target_type == TypeManager.ushort_type)
695 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
696 if (target_type == TypeManager.int32_type)
697 return new EmptyCast (expr, target_type);
698 if (target_type == TypeManager.char_type)
699 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
700 } else if (expr_type == TypeManager.int64_type){
702 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
704 if (target_type == TypeManager.sbyte_type)
705 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
706 if (target_type == TypeManager.byte_type)
707 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
708 if (target_type == TypeManager.short_type)
709 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
710 if (target_type == TypeManager.ushort_type)
711 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
712 if (target_type == TypeManager.int32_type)
713 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
714 if (target_type == TypeManager.uint32_type)
715 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
716 if (target_type == TypeManager.uint64_type)
717 return new EmptyCast (expr, target_type);
718 if (target_type == TypeManager.char_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
720 } else if (expr_type == TypeManager.uint64_type){
722 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
724 if (target_type == TypeManager.sbyte_type)
725 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
726 if (target_type == TypeManager.byte_type)
727 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
728 if (target_type == TypeManager.short_type)
729 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
730 if (target_type == TypeManager.ushort_type)
731 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
732 if (target_type == TypeManager.int32_type)
733 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
734 if (target_type == TypeManager.uint32_type)
735 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
736 if (target_type == TypeManager.int64_type)
737 return new EmptyCast (expr, target_type);
738 if (target_type == TypeManager.char_type)
739 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
740 } else if (expr_type == TypeManager.char_type){
742 // From char to sbyte, byte, short
744 if (target_type == TypeManager.sbyte_type)
745 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
746 if (target_type == TypeManager.byte_type)
747 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
748 if (target_type == TypeManager.short_type)
749 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
750 } else if (expr_type == TypeManager.float_type){
752 // From float to sbyte, byte, short,
753 // ushort, int, uint, long, ulong, char
756 if (target_type == TypeManager.sbyte_type)
757 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
758 if (target_type == TypeManager.byte_type)
759 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
760 if (target_type == TypeManager.short_type)
761 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
762 if (target_type == TypeManager.ushort_type)
763 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
764 if (target_type == TypeManager.int32_type)
765 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
766 if (target_type == TypeManager.uint32_type)
767 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
768 if (target_type == TypeManager.int64_type)
769 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
770 if (target_type == TypeManager.uint64_type)
771 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
772 if (target_type == TypeManager.char_type)
773 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
774 if (target_type == TypeManager.decimal_type)
775 return InternalTypeConstructor (tc, expr, target_type);
776 } else if (expr_type == TypeManager.double_type){
778 // From double to byte, byte, short,
779 // ushort, int, uint, long, ulong,
780 // char, float or decimal
782 if (target_type == TypeManager.sbyte_type)
783 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
784 if (target_type == TypeManager.byte_type)
785 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
786 if (target_type == TypeManager.short_type)
787 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
788 if (target_type == TypeManager.ushort_type)
789 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
790 if (target_type == TypeManager.int32_type)
791 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
792 if (target_type == TypeManager.uint32_type)
793 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
794 if (target_type == TypeManager.int64_type)
795 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
796 if (target_type == TypeManager.uint64_type)
797 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
798 if (target_type == TypeManager.char_type)
799 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
800 if (target_type == TypeManager.float_type)
801 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
802 if (target_type == TypeManager.decimal_type)
803 return InternalTypeConstructor (tc, expr, target_type);
806 // decimal is taken care of by the op_Explicit methods.
812 // Performs an explicit conversion of the expression `expr' whose
813 // type is expr.Type to `target_type'.
815 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
818 Expression ne = ConvertImplicit (tc, expr, target_type);
823 ne = ConvertNumericExplicit (tc, expr, target_type);
831 static string ExprClassName (ExprClass c)
834 case ExprClass.Invalid:
836 case ExprClass.Value:
838 case ExprClass.Variable:
840 case ExprClass.Namespace:
844 case ExprClass.MethodGroup:
845 return "method group";
846 case ExprClass.PropertyAccess:
847 return "property access";
848 case ExprClass.EventAccess:
849 return "event access";
850 case ExprClass.IndexerAccess:
851 return "indexer access";
852 case ExprClass.Nothing:
855 throw new Exception ("Should not happen");
859 // Reports that we were expecting `expr' to be of class `expected'
861 protected void report118 (TypeContainer tc, Expression expr, string expected)
863 Error (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
864 "' where an " + expected + " was expected");
869 // This is just a base class for expressions that can
870 // appear on statements (invocations, object creation,
871 // assignments, post/pre increment and decrement). The idea
872 // being that they would support an extra Emition interface that
873 // does not leave a result on the stack.
876 public abstract class ExpressionStatement : Expression {
879 // Requests the expression to be emitted in a `statement'
880 // context. This means that no new value is left on the
881 // stack after invoking this method (constrasted with
882 // Emit that will always leave a value on the stack).
884 public abstract void EmitStatement (EmitContext ec);
888 // This kind of cast is used to encapsulate the child
889 // whose type is child.Type into an expression that is
890 // reported to return "return_type". This is used to encapsulate
891 // expressions which have compatible types, but need to be dealt
892 // at higher levels with.
894 // For example, a "byte" expression could be encapsulated in one
895 // of these as an "unsigned int". The type for the expression
896 // would be "unsigned int".
900 public class EmptyCast : Expression {
901 protected Expression child;
903 public EmptyCast (Expression child, Type return_type)
905 ExprClass = child.ExprClass;
910 public override Expression Resolve (TypeContainer tc)
912 // This should never be invoked, we are born in fully
913 // initialized state.
918 public override void Emit (EmitContext ec)
925 // This kind of cast is used to encapsulate Value Types in objects.
927 // The effect of it is to box the value type emitted by the previous
930 public class BoxedCast : EmptyCast {
932 public BoxedCast (Expression expr)
933 : base (expr, TypeManager.object_type)
937 public override Expression Resolve (TypeContainer tc)
939 // This should never be invoked, we are born in fully
940 // initialized state.
945 public override void Emit (EmitContext ec)
948 ec.ig.Emit (OpCodes.Box, child.Type);
953 // This kind of cast is used to encapsulate a child expression
954 // that can be trivially converted to a target type using one or
955 // two opcodes. The opcodes are passed as arguments.
957 public class OpcodeCast : EmptyCast {
961 public OpcodeCast (Expression child, Type return_type, OpCode op)
962 : base (child, return_type)
966 second_valid = false;
969 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
970 : base (child, return_type)
978 public override Expression Resolve (TypeContainer tc)
980 // This should never be invoked, we are born in fully
981 // initialized state.
986 public override void Emit (EmitContext ec)
998 // Unary expressions.
1002 // Unary implements unary expressions. It derives from
1003 // ExpressionStatement becuase the pre/post increment/decrement
1004 // operators can be used in a statement context.
1006 public class Unary : ExpressionStatement {
1007 public enum Operator {
1008 Addition, Subtraction, Negate, BitComplement,
1009 Indirection, AddressOf, PreIncrement,
1010 PreDecrement, PostIncrement, PostDecrement
1015 ArrayList Arguments;
1019 public Unary (Operator op, Expression expr, Location loc)
1023 this.location = loc;
1026 public Expression Expr {
1036 public Operator Oper {
1047 // Returns a stringified representation of the Operator
1052 case Operator.Addition:
1054 case Operator.Subtraction:
1056 case Operator.Negate:
1058 case Operator.BitComplement:
1060 case Operator.AddressOf:
1062 case Operator.Indirection:
1064 case Operator.PreIncrement : case Operator.PostIncrement :
1066 case Operator.PreDecrement : case Operator.PostDecrement :
1070 return oper.ToString ();
1073 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1075 if (expr.Type == target_type)
1078 return ConvertImplicit (tc, expr, target_type);
1081 void report23 (Report r, Type t)
1083 r.Error (23, "Operator " + OperName () + " cannot be applied to operand of type `" +
1084 TypeManager.CSharpName (t) + "'");
1088 // Returns whether an object of type `t' can be incremented
1089 // or decremented with add/sub (ie, basically whether we can
1090 // use pre-post incr-decr operations on it, but it is not a
1091 // System.Decimal, which we test elsewhere)
1093 static bool IsIncrementableNumber (Type t)
1095 return (t == TypeManager.sbyte_type) ||
1096 (t == TypeManager.byte_type) ||
1097 (t == TypeManager.short_type) ||
1098 (t == TypeManager.ushort_type) ||
1099 (t == TypeManager.int32_type) ||
1100 (t == TypeManager.uint32_type) ||
1101 (t == TypeManager.int64_type) ||
1102 (t == TypeManager.uint64_type) ||
1103 (t == TypeManager.char_type) ||
1104 (t.IsSubclassOf (TypeManager.enum_type)) ||
1105 (t == TypeManager.float_type) ||
1106 (t == TypeManager.double_type);
1109 Expression ResolveOperator (TypeContainer tc)
1111 Type expr_type = expr.Type;
1114 // Step 1: Perform Operator Overload location
1119 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1120 op_name = "op_Increment";
1121 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1122 op_name = "op_Decrement";
1124 op_name = "op_" + oper;
1126 mg = MemberLookup (tc, expr_type, op_name, false);
1129 Arguments = new ArrayList ();
1130 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1132 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg, Arguments, location);
1133 if (method != null) {
1134 MethodInfo mi = (MethodInfo) method;
1136 type = mi.ReturnType;
1142 // Step 2: Default operations on CLI native types.
1145 // Only perform numeric promotions on:
1148 if (expr_type == null)
1151 if (oper == Operator.Negate){
1152 if (expr_type != TypeManager.bool_type) {
1153 report23 (tc.RootContext.Report, expr.Type);
1157 type = TypeManager.bool_type;
1161 if (oper == Operator.BitComplement) {
1162 if (!((expr_type == TypeManager.int32_type) ||
1163 (expr_type == TypeManager.uint32_type) ||
1164 (expr_type == TypeManager.int64_type) ||
1165 (expr_type == TypeManager.uint64_type) ||
1166 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1167 report23 (tc.RootContext.Report, expr.Type);
1174 if (oper == Operator.Addition) {
1176 // A plus in front of something is just a no-op, so return the child.
1182 // Deals with -literals
1183 // int operator- (int x)
1184 // long operator- (long x)
1185 // float operator- (float f)
1186 // double operator- (double d)
1187 // decimal operator- (decimal d)
1189 if (oper == Operator.Subtraction){
1191 // Fold a "- Constant" into a negative constant
1194 Expression e = null;
1197 // Is this a constant?
1199 if (expr is IntLiteral)
1200 e = new IntLiteral (-((IntLiteral) expr).Value);
1201 else if (expr is LongLiteral)
1202 e = new LongLiteral (-((LongLiteral) expr).Value);
1203 else if (expr is FloatLiteral)
1204 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1205 else if (expr is DoubleLiteral)
1206 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1207 else if (expr is DecimalLiteral)
1208 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1216 // Not a constant we can optimize, perform numeric
1217 // promotions to int, long, double.
1220 // The following is inneficient, because we call
1221 // ConvertImplicit too many times.
1223 // It is also not clear if we should convert to Float
1224 // or Double initially.
1226 if (expr_type == TypeManager.uint32_type){
1228 // FIXME: handle exception to this rule that
1229 // permits the int value -2147483648 (-2^31) to
1230 // bt written as a decimal interger literal
1232 type = TypeManager.int64_type;
1233 expr = ConvertImplicit (tc, expr, type);
1237 if (expr_type == TypeManager.uint64_type){
1239 // FIXME: Handle exception of `long value'
1240 // -92233720368547758087 (-2^63) to be written as
1241 // decimal integer literal.
1243 report23 (tc.RootContext.Report, expr_type);
1247 e = ConvertImplicit (tc, expr, TypeManager.int32_type);
1254 e = ConvertImplicit (tc, expr, TypeManager.int64_type);
1261 e = ConvertImplicit (tc, expr, TypeManager.double_type);
1268 report23 (tc.RootContext.Report, expr_type);
1273 // The operand of the prefix/postfix increment decrement operators
1274 // should be an expression that is classified as a variable,
1275 // a property access or an indexer access
1277 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1278 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1279 if (expr.ExprClass == ExprClass.Variable){
1280 if (IsIncrementableNumber (expr_type) ||
1281 expr_type == TypeManager.decimal_type){
1285 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1287 // FIXME: Verify that we have both get and set methods
1289 throw new Exception ("Implement me");
1290 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1292 // FIXME: Verify that we have both get and set methods
1294 throw new Exception ("Implement me");
1296 report118 (tc, expr, "variable, indexer or property access");
1300 if (oper == Operator.AddressOf){
1301 if (expr.ExprClass != ExprClass.Variable){
1302 Error (tc, 211, "Cannot take the address of non-variables");
1305 type = Type.GetType (expr.Type.ToString () + "*");
1308 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1309 TypeManager.CSharpName (expr_type) + "'");
1314 public override Expression Resolve (TypeContainer tc)
1316 expr = expr.Resolve (tc);
1321 return ResolveOperator (tc);
1324 public override void Emit (EmitContext ec)
1326 ILGenerator ig = ec.ig;
1327 Type expr_type = expr.Type;
1329 if (method != null) {
1331 // Note that operators are static anyway
1333 if (Arguments != null)
1334 Invocation.EmitArguments (ec, method, Arguments);
1337 // Post increment/decrement operations need a copy at this
1340 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1341 ig.Emit (OpCodes.Dup);
1344 ig.Emit (OpCodes.Call, (MethodInfo) method);
1347 // Pre Increment and Decrement operators
1349 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1350 ig.Emit (OpCodes.Dup);
1354 // Increment and Decrement should store the result
1356 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1357 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1358 ((LValue) expr).Store (ec);
1364 case Operator.Addition:
1365 throw new Exception ("This should be caught by Resolve");
1367 case Operator.Subtraction:
1369 ig.Emit (OpCodes.Neg);
1372 case Operator.Negate:
1374 ig.Emit (OpCodes.Ldc_I4_0);
1375 ig.Emit (OpCodes.Ceq);
1378 case Operator.BitComplement:
1380 ig.Emit (OpCodes.Not);
1383 case Operator.AddressOf:
1384 ((LValue)expr).AddressOf (ec);
1387 case Operator.Indirection:
1388 throw new Exception ("Not implemented yet");
1390 case Operator.PreIncrement:
1391 case Operator.PreDecrement:
1392 if (expr.ExprClass == ExprClass.Variable){
1394 // Resolve already verified that it is an "incrementable"
1397 ig.Emit (OpCodes.Ldc_I4_1);
1399 if (oper == Operator.PreDecrement)
1400 ig.Emit (OpCodes.Sub);
1402 ig.Emit (OpCodes.Add);
1403 ig.Emit (OpCodes.Dup);
1404 ((LValue) expr).Store (ec);
1406 throw new Exception ("Handle Indexers and Properties here");
1410 case Operator.PostIncrement:
1411 case Operator.PostDecrement:
1412 if (expr.ExprClass == ExprClass.Variable){
1414 // Resolve already verified that it is an "incrementable"
1417 ig.Emit (OpCodes.Dup);
1418 ig.Emit (OpCodes.Ldc_I4_1);
1420 if (oper == Operator.PostDecrement)
1421 ig.Emit (OpCodes.Sub);
1423 ig.Emit (OpCodes.Add);
1424 ((LValue) expr).Store (ec);
1426 throw new Exception ("Handle Indexers and Properties here");
1431 throw new Exception ("This should not happen: Operator = "
1432 + oper.ToString ());
1437 public override void EmitStatement (EmitContext ec)
1440 // FIXME: we should rewrite this code to generate
1441 // better code for ++ and -- as we know we wont need
1442 // the values on the stack
1445 ec.ig.Emit (OpCodes.Pop);
1449 public class Probe : Expression {
1450 public readonly string ProbeType;
1451 public readonly Operator Oper;
1455 public enum Operator {
1459 public Probe (Operator oper, Expression expr, string probe_type)
1462 ProbeType = probe_type;
1466 public Expression Expr {
1472 public override Expression Resolve (TypeContainer tc)
1474 probe_type = tc.LookupType (ProbeType, false);
1476 if (probe_type == null)
1479 expr = expr.Resolve (tc);
1481 type = TypeManager.bool_type;
1482 eclass = ExprClass.Value;
1487 public override void Emit (EmitContext ec)
1491 if (Oper == Operator.Is){
1492 ec.ig.Emit (OpCodes.Isinst, probe_type);
1494 throw new Exception ("Implement as");
1500 // This represents a typecast in the source language.
1502 // FIXME: Cast expressions have an unusual set of parsing
1503 // rules, we need to figure those out.
1505 public class Cast : Expression {
1509 public Cast (string cast_type, Expression expr)
1511 this.target_type = cast_type;
1515 public string TargetType {
1521 public Expression Expr {
1530 public override Expression Resolve (TypeContainer tc)
1532 expr = expr.Resolve (tc);
1536 type = tc.LookupType (target_type, false);
1537 eclass = ExprClass.Value;
1542 expr = ConvertExplicit (tc, expr, type);
1547 public override void Emit (EmitContext ec)
1550 // This one will never happen
1552 throw new Exception ("Should not happen");
1556 public class Binary : Expression {
1557 public enum Operator {
1558 Multiply, Division, Modulus,
1559 Addition, Subtraction,
1560 LeftShift, RightShift,
1561 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1562 Equality, Inequality,
1571 Expression left, right;
1573 ArrayList Arguments;
1577 public Binary (Operator oper, Expression left, Expression right, Location loc)
1582 this.location = loc;
1585 public Operator Oper {
1594 public Expression Left {
1603 public Expression Right {
1614 // Returns a stringified representation of the Operator
1619 case Operator.Multiply:
1621 case Operator.Division:
1623 case Operator.Modulus:
1625 case Operator.Addition:
1627 case Operator.Subtraction:
1629 case Operator.LeftShift:
1631 case Operator.RightShift:
1633 case Operator.LessThan:
1635 case Operator.GreaterThan:
1637 case Operator.LessThanOrEqual:
1639 case Operator.GreaterThanOrEqual:
1641 case Operator.Equality:
1643 case Operator.Inequality:
1645 case Operator.BitwiseAnd:
1647 case Operator.BitwiseOr:
1649 case Operator.ExclusiveOr:
1651 case Operator.LogicalOr:
1653 case Operator.LogicalAnd:
1657 return oper.ToString ();
1660 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1662 if (expr.Type == target_type)
1665 return ConvertImplicit (tc, expr, target_type);
1669 // Note that handling the case l == Decimal || r == Decimal
1670 // is taken care of by the Step 1 Operator Overload resolution.
1672 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1674 if (l == TypeManager.double_type || r == TypeManager.double_type){
1676 // If either operand is of type double, the other operand is
1677 // conveted to type double.
1679 if (r != TypeManager.double_type)
1680 right = ConvertImplicit (tc, right, TypeManager.double_type);
1681 if (l != TypeManager.double_type)
1682 left = ConvertImplicit (tc, left, TypeManager.double_type);
1684 type = TypeManager.double_type;
1685 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1687 // if either operand is of type float, th eother operand is
1688 // converd to type float.
1690 if (r != TypeManager.double_type)
1691 right = ConvertImplicit (tc, right, TypeManager.float_type);
1692 if (l != TypeManager.double_type)
1693 left = ConvertImplicit (tc, left, TypeManager.float_type);
1694 type = TypeManager.float_type;
1695 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1697 // If either operand is of type ulong, the other operand is
1698 // converted to type ulong. or an error ocurrs if the other
1699 // operand is of type sbyte, short, int or long
1703 if (l == TypeManager.uint64_type)
1705 else if (r == TypeManager.uint64_type)
1708 if ((other == TypeManager.sbyte_type) ||
1709 (other == TypeManager.short_type) ||
1710 (other == TypeManager.int32_type) ||
1711 (other == TypeManager.int64_type)){
1712 string oper = OperName ();
1714 Error (tc, 34, "Operator `" + OperName ()
1715 + "' is ambiguous on operands of type `"
1716 + TypeManager.CSharpName (l) + "' "
1717 + "and `" + TypeManager.CSharpName (r)
1720 type = TypeManager.uint64_type;
1721 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1723 // If either operand is of type long, the other operand is converted
1726 if (l != TypeManager.int64_type)
1727 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1728 if (r != TypeManager.int64_type)
1729 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1731 type = TypeManager.int64_type;
1732 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1734 // If either operand is of type uint, and the other
1735 // operand is of type sbyte, short or int, othe operands are
1736 // converted to type long.
1740 if (l == TypeManager.uint32_type)
1742 else if (r == TypeManager.uint32_type)
1745 if ((other == TypeManager.sbyte_type) ||
1746 (other == TypeManager.short_type) ||
1747 (other == TypeManager.int32_type)){
1748 left = ForceConversion (tc, left, TypeManager.int64_type);
1749 right = ForceConversion (tc, right, TypeManager.int64_type);
1750 type = TypeManager.int64_type;
1753 // if either operand is of type uint, the other
1754 // operand is converd to type uint
1756 left = ForceConversion (tc, left, TypeManager.uint32_type);
1757 right = ForceConversion (tc, left, TypeManager.uint32_type);
1758 type = TypeManager.uint32_type;
1760 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1761 if (l != TypeManager.decimal_type)
1762 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1763 if (r != TypeManager.decimal_type)
1764 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1766 type = TypeManager.decimal_type;
1768 left = ForceConversion (tc, left, TypeManager.int32_type);
1769 right = ForceConversion (tc, right, TypeManager.int32_type);
1770 type = TypeManager.int32_type;
1774 void error19 (TypeContainer tc)
1777 "Operator " + OperName () + " cannot be applied to operands of type `" +
1778 TypeManager.CSharpName (left.Type) + "' and `" +
1779 TypeManager.CSharpName (right.Type) + "'");
1783 Expression CheckShiftArguments (TypeContainer tc)
1787 Type r = right.Type;
1789 e = ForceConversion (tc, right, TypeManager.int32_type);
1796 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1797 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1798 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1799 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1808 Expression ResolveOperator (TypeContainer tc)
1811 Type r = right.Type;
1814 // Step 1: Perform Operator Overload location
1816 Expression left_expr, right_expr;
1818 string op = "op_" + oper;
1820 left_expr = MemberLookup (tc, l, op, false);
1822 right_expr = MemberLookup (tc, r, op, false);
1824 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1826 Arguments = new ArrayList ();
1827 Arguments.Add (new Argument (left, Argument.AType.Expression));
1828 Arguments.Add (new Argument (right, Argument.AType.Expression));
1830 if (union != null) {
1831 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1832 if (method != null) {
1833 MethodInfo mi = (MethodInfo) method;
1835 type = mi.ReturnType;
1841 // Step 2: Default operations on CLI native types.
1844 // Only perform numeric promotions on:
1845 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1847 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1848 return CheckShiftArguments (tc);
1849 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1851 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1853 } else if (oper == Operator.Addition){
1855 // If any of the arguments is a string, cast to string
1857 if (l == TypeManager.string_type){
1858 if (r == TypeManager.string_type){
1860 method = TypeManager.string_concat_string_string;
1863 method = TypeManager.string_concat_object_object;
1864 right = ConvertImplicit (tc, right, TypeManager.object_type);
1866 type = TypeManager.string_type;
1868 Arguments = new ArrayList ();
1869 Arguments.Add (new Argument (left, Argument.AType.Expression));
1870 Arguments.Add (new Argument (right, Argument.AType.Expression));
1871 } else if (r == TypeManager.string_type){
1873 method = TypeManager.string_concat_object_object;
1874 Arguments = new ArrayList ();
1875 Arguments.Add (new Argument (left, Argument.AType.Expression));
1876 Arguments.Add (new Argument (right, Argument.AType.Expression));
1878 left = ConvertImplicit (tc, left, TypeManager.object_type);
1879 type = TypeManager.string_type;
1883 // FIXME: is Delegate operator + (D x, D y) handled?
1886 DoNumericPromotions (tc, l, r);
1888 if (left == null || right == null)
1892 if (oper == Operator.BitwiseAnd ||
1893 oper == Operator.BitwiseOr ||
1894 oper == Operator.ExclusiveOr){
1895 if (!((l == TypeManager.int32_type) ||
1896 (l == TypeManager.uint32_type) ||
1897 (l == TypeManager.int64_type) ||
1898 (l == TypeManager.uint64_type))){
1904 if (oper == Operator.Equality ||
1905 oper == Operator.Inequality ||
1906 oper == Operator.LessThanOrEqual ||
1907 oper == Operator.LessThan ||
1908 oper == Operator.GreaterThanOrEqual ||
1909 oper == Operator.GreaterThan){
1910 type = TypeManager.bool_type;
1916 public override Expression Resolve (TypeContainer tc)
1918 left = left.Resolve (tc);
1919 right = right.Resolve (tc);
1921 if (left == null || right == null)
1924 return ResolveOperator (tc);
1927 public bool IsBranchable ()
1929 if (oper == Operator.Equality ||
1930 oper == Operator.Inequality ||
1931 oper == Operator.LessThan ||
1932 oper == Operator.GreaterThan ||
1933 oper == Operator.LessThanOrEqual ||
1934 oper == Operator.GreaterThanOrEqual){
1941 // This entry point is used by routines that might want
1942 // to emit a brfalse/brtrue after an expression, and instead
1943 // they could use a more compact notation.
1945 // Typically the code would generate l.emit/r.emit, followed
1946 // by the comparission and then a brtrue/brfalse. The comparissions
1947 // are sometimes inneficient (there are not as complete as the branches
1948 // look for the hacks in Emit using double ceqs).
1950 // So for those cases we provide EmitBranchable that can emit the
1951 // branch with the test
1953 public void EmitBranchable (EmitContext ec, int target)
1956 bool close_target = false;
1962 case Operator.Equality:
1964 opcode = OpCodes.Beq_S;
1966 opcode = OpCodes.Beq;
1969 case Operator.Inequality:
1971 opcode = OpCodes.Bne_Un_S;
1973 opcode = OpCodes.Bne_Un;
1976 case Operator.LessThan:
1978 opcode = OpCodes.Blt_S;
1980 opcode = OpCodes.Blt;
1983 case Operator.GreaterThan:
1985 opcode = OpCodes.Bgt_S;
1987 opcode = OpCodes.Bgt;
1990 case Operator.LessThanOrEqual:
1992 opcode = OpCodes.Ble_S;
1994 opcode = OpCodes.Ble;
1997 case Operator.GreaterThanOrEqual:
1999 opcode = OpCodes.Bge_S;
2001 opcode = OpCodes.Ble;
2005 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2006 + oper.ToString ());
2009 ec.ig.Emit (opcode, target);
2012 public override void Emit (EmitContext ec)
2014 ILGenerator ig = ec.ig;
2016 Type r = right.Type;
2019 if (method != null) {
2021 // Note that operators are static anyway
2023 if (Arguments != null)
2024 Invocation.EmitArguments (ec, method, Arguments);
2026 if (method is MethodInfo)
2027 ig.Emit (OpCodes.Call, (MethodInfo) method);
2029 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2038 case Operator.Multiply:
2040 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2041 opcode = OpCodes.Mul_Ovf;
2042 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2043 opcode = OpCodes.Mul_Ovf_Un;
2045 opcode = OpCodes.Mul;
2047 opcode = OpCodes.Mul;
2051 case Operator.Division:
2052 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2053 opcode = OpCodes.Div_Un;
2055 opcode = OpCodes.Div;
2058 case Operator.Modulus:
2059 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2060 opcode = OpCodes.Rem_Un;
2062 opcode = OpCodes.Rem;
2065 case Operator.Addition:
2067 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2068 opcode = OpCodes.Add_Ovf;
2069 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2070 opcode = OpCodes.Add_Ovf_Un;
2072 opcode = OpCodes.Mul;
2074 opcode = OpCodes.Add;
2077 case Operator.Subtraction:
2079 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2080 opcode = OpCodes.Sub_Ovf;
2081 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2082 opcode = OpCodes.Sub_Ovf_Un;
2084 opcode = OpCodes.Sub;
2086 opcode = OpCodes.Sub;
2089 case Operator.RightShift:
2090 opcode = OpCodes.Shr;
2093 case Operator.LeftShift:
2094 opcode = OpCodes.Shl;
2097 case Operator.Equality:
2098 opcode = OpCodes.Ceq;
2101 case Operator.Inequality:
2102 ec.ig.Emit (OpCodes.Ceq);
2103 ec.ig.Emit (OpCodes.Ldc_I4_0);
2105 opcode = OpCodes.Ceq;
2108 case Operator.LessThan:
2109 opcode = OpCodes.Clt;
2112 case Operator.GreaterThan:
2113 opcode = OpCodes.Cgt;
2116 case Operator.LessThanOrEqual:
2117 ec.ig.Emit (OpCodes.Cgt);
2118 ec.ig.Emit (OpCodes.Ldc_I4_0);
2120 opcode = OpCodes.Ceq;
2123 case Operator.GreaterThanOrEqual:
2124 ec.ig.Emit (OpCodes.Clt);
2125 ec.ig.Emit (OpCodes.Ldc_I4_1);
2127 opcode = OpCodes.Sub;
2130 case Operator.LogicalOr:
2131 case Operator.BitwiseOr:
2132 opcode = OpCodes.Or;
2135 case Operator.LogicalAnd:
2136 case Operator.BitwiseAnd:
2137 opcode = OpCodes.And;
2140 case Operator.ExclusiveOr:
2141 opcode = OpCodes.Xor;
2145 throw new Exception ("This should not happen: Operator = "
2146 + oper.ToString ());
2153 public class Conditional : Expression {
2154 Expression expr, trueExpr, falseExpr;
2157 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2160 this.trueExpr = trueExpr;
2161 this.falseExpr = falseExpr;
2165 public Expression Expr {
2171 public Expression TrueExpr {
2177 public Expression FalseExpr {
2183 public override Expression Resolve (TypeContainer tc)
2185 expr = expr.Resolve (tc);
2187 if (expr.Type != TypeManager.bool_type)
2188 expr = Expression.ConvertImplicitRequired (
2189 tc, expr, TypeManager.bool_type, l);
2191 trueExpr = trueExpr.Resolve (tc);
2192 falseExpr = falseExpr.Resolve (tc);
2194 if (expr == null || trueExpr == null || falseExpr == null)
2197 if (trueExpr.Type == falseExpr.Type)
2198 type = trueExpr.Type;
2203 // First, if an implicit conversion exists from trueExpr
2204 // to falseExpr, then the result type is of type falseExpr.Type
2206 conv = ConvertImplicit (tc, trueExpr, falseExpr.Type);
2208 type = falseExpr.Type;
2210 } else if ((conv = ConvertImplicit (tc, falseExpr, trueExpr.Type)) != null){
2211 type = trueExpr.Type;
2214 Error (tc, 173, l, "The type of the conditional expression can " +
2215 "not be computed because there is no implicit conversion" +
2216 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2217 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2222 eclass = ExprClass.Value;
2226 public override void Emit (EmitContext ec)
2228 ILGenerator ig = ec.ig;
2229 Label false_target = ig.DefineLabel ();
2230 Label end_target = ig.DefineLabel ();
2233 ig.Emit (OpCodes.Brfalse, false_target);
2235 ig.Emit (OpCodes.Br, end_target);
2236 ig.MarkLabel (false_target);
2237 falseExpr.Emit (ec);
2238 ig.MarkLabel (end_target);
2242 public class SimpleName : Expression {
2243 public readonly string Name;
2244 public readonly Location Location;
2246 public SimpleName (string name, Location l)
2253 // Checks whether we are trying to access an instance
2254 // property, method or field from a static body.
2256 Expression MemberStaticCheck (Report r, Expression e)
2258 if (e is FieldExpr){
2259 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2263 "An object reference is required " +
2264 "for the non-static field `"+Name+"'");
2267 } else if (e is MethodGroupExpr){
2268 // FIXME: Pending reorganization of MemberLookup
2269 // Basically at this point we should have the
2270 // best match already selected for us, and
2271 // we should only have to check a *single*
2272 // Method for its static on/off bit.
2274 } else if (e is PropertyExpr){
2275 if (!((PropertyExpr) e).IsStatic){
2277 "An object reference is required " +
2278 "for the non-static property access `"+
2288 // 7.5.2: Simple Names.
2290 // Local Variables and Parameters are handled at
2291 // parse time, so they never occur as SimpleNames.
2293 Expression ResolveSimpleName (TypeContainer tc)
2296 Report r = tc.RootContext.Report;
2298 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2302 else if (e is FieldExpr){
2303 FieldExpr fe = (FieldExpr) e;
2305 if (!fe.FieldInfo.IsStatic)
2306 fe.Instance = new This ();
2309 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2310 return MemberStaticCheck (r, e);
2316 // Do step 3 of the Simple Name resolution.
2318 // FIXME: implement me.
2320 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2327 // SimpleName needs to handle a multitude of cases:
2329 // simple_names and qualified_identifiers are placed on
2330 // the tree equally.
2332 public override Expression Resolve (TypeContainer tc)
2334 if (Name.IndexOf (".") != -1)
2335 return ResolveMemberAccess (tc, Name);
2337 return ResolveSimpleName (tc);
2340 public override void Emit (EmitContext ec)
2342 throw new Exception ("SimpleNames should be gone from the tree");
2347 // A simple interface that should be implemeneted by LValues
2349 public interface LValue {
2352 // The Store method should store the contents of the top
2353 // of the stack into the storage that is implemented by
2354 // the particular implementation of LValue
2356 void Store (EmitContext ec);
2359 // The AddressOf method should generate code that loads
2360 // the address of the LValue and leaves it on the stack
2362 void AddressOf (EmitContext ec);
2365 public class LocalVariableReference : Expression, LValue {
2366 public readonly string Name;
2367 public readonly Block Block;
2369 public LocalVariableReference (Block block, string name)
2373 eclass = ExprClass.Variable;
2376 public VariableInfo VariableInfo {
2378 return Block.GetVariableInfo (Name);
2382 public override Expression Resolve (TypeContainer tc)
2384 VariableInfo vi = Block.GetVariableInfo (Name);
2386 type = vi.VariableType;
2390 public override void Emit (EmitContext ec)
2392 VariableInfo vi = VariableInfo;
2393 ILGenerator ig = ec.ig;
2400 ig.Emit (OpCodes.Ldloc_0);
2404 ig.Emit (OpCodes.Ldloc_1);
2408 ig.Emit (OpCodes.Ldloc_2);
2412 ig.Emit (OpCodes.Ldloc_3);
2417 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2419 ig.Emit (OpCodes.Ldloc, idx);
2424 public void Store (EmitContext ec)
2426 ILGenerator ig = ec.ig;
2427 VariableInfo vi = VariableInfo;
2433 ig.Emit (OpCodes.Stloc_0);
2437 ig.Emit (OpCodes.Stloc_1);
2441 ig.Emit (OpCodes.Stloc_2);
2445 ig.Emit (OpCodes.Stloc_3);
2450 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2452 ig.Emit (OpCodes.Stloc, idx);
2457 public void AddressOf (EmitContext ec)
2459 VariableInfo vi = VariableInfo;
2466 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2468 ec.ig.Emit (OpCodes.Ldloca, idx);
2472 public class ParameterReference : Expression, LValue {
2473 public readonly Parameters Pars;
2474 public readonly String Name;
2475 public readonly int Idx;
2477 public ParameterReference (Parameters pars, int idx, string name)
2482 eclass = ExprClass.Variable;
2485 public override Expression Resolve (TypeContainer tc)
2487 Type [] types = Pars.GetParameterInfo (tc);
2494 public override void Emit (EmitContext ec)
2497 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
2499 ec.ig.Emit (OpCodes.Ldarg, Idx);
2502 public void Store (EmitContext ec)
2505 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
2507 ec.ig.Emit (OpCodes.Starg, Idx);
2511 public void AddressOf (EmitContext ec)
2514 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
2516 ec.ig.Emit (OpCodes.Ldarga, Idx);
2521 // Used for arguments to New(), Invocation()
2523 public class Argument {
2530 public readonly AType Type;
2533 public Argument (Expression expr, AType type)
2539 public Expression Expr {
2549 public bool Resolve (TypeContainer tc)
2551 expr = expr.Resolve (tc);
2553 return expr != null;
2556 public void Emit (EmitContext ec)
2563 // Invocation of methods or delegates.
2565 public class Invocation : ExpressionStatement {
2566 public readonly ArrayList Arguments;
2567 public readonly Location Location;
2570 MethodBase method = null;
2572 static Hashtable method_parameter_cache;
2574 static Invocation ()
2576 method_parameter_cache = new Hashtable ();
2580 // arguments is an ArrayList, but we do not want to typecast,
2581 // as it might be null.
2583 // FIXME: only allow expr to be a method invocation or a
2584 // delegate invocation (7.5.5)
2586 public Invocation (Expression expr, ArrayList arguments, Location l)
2589 Arguments = arguments;
2593 public Expression Expr {
2600 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2601 /// compatible, and if so, how good is the match (in terms of
2602 /// "better conversions" (7.4.2.3).
2604 /// 0 is the best possible match.
2605 /// -1 represents a type mismatch.
2606 /// -2 represents a ref/out mismatch.
2608 static int Badness (Argument a, Type t)
2610 Expression argument_expr = a.Expr;
2611 Type argument_type = argument_expr.Type;
2613 if (argument_type == null){
2614 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2617 if (t == argument_type)
2621 // Now probe whether an implicit constant expression conversion
2624 // An implicit constant expression conversion permits the following
2627 // * A constant-expression of type `int' can be converted to type
2628 // sbyte, byute, short, ushort, uint, ulong provided the value of
2629 // of the expression is withing the range of the destination type.
2631 // * A constant-expression of type long can be converted to type
2632 // ulong, provided the value of the constant expression is not negative
2634 // FIXME: Note that this assumes that constant folding has
2635 // taken place. We dont do constant folding yet.
2638 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2639 IntLiteral ei = (IntLiteral) argument_expr;
2640 int value = ei.Value;
2642 if (t == TypeManager.sbyte_type){
2643 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2645 } else if (t == TypeManager.byte_type){
2646 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2648 } else if (t == TypeManager.short_type){
2649 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2651 } else if (t == TypeManager.ushort_type){
2652 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2654 } else if (t == TypeManager.uint32_type){
2656 // we can optimize this case: a positive int32
2657 // always fits on a uint32
2661 } else if (t == TypeManager.uint64_type){
2663 // we can optimize this case: a positive int32
2664 // always fits on a uint64
2669 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2670 LongLiteral ll = (LongLiteral) argument_expr;
2672 if (t == TypeManager.uint64_type)
2677 // FIXME: Implement user-defined implicit conversions here.
2678 // FIXME: Implement better conversion here.
2684 // Returns the Parameters (a ParameterData interface) for the
2687 static ParameterData GetParameterData (MethodBase mb)
2689 object pd = method_parameter_cache [mb];
2692 return (ParameterData) pd;
2694 if (mb is MethodBuilder || mb is ConstructorBuilder){
2695 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2697 InternalParameters ip = mc.ParameterInfo;
2698 method_parameter_cache [mb] = ip;
2700 return (ParameterData) ip;
2702 ParameterInfo [] pi = mb.GetParameters ();
2703 ReflectionParameters rp = new ReflectionParameters (pi);
2704 method_parameter_cache [mb] = rp;
2706 return (ParameterData) rp;
2710 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2712 // Locate user-defined implicit operators
2716 mg = MemberLookup (tc, to, "op_Implicit", false);
2719 MethodGroupExpr me = (MethodGroupExpr) mg;
2721 for (int i = me.Methods.Length; i > 0;) {
2723 MethodBase mb = me.Methods [i];
2724 ParameterData pd = GetParameterData (mb);
2726 if (from == pd.ParameterType (0))
2731 mg = MemberLookup (tc, from, "op_Implicit", false);
2734 MethodGroupExpr me = (MethodGroupExpr) mg;
2736 for (int i = me.Methods.Length; i > 0;) {
2738 MethodBase mb = me.Methods [i];
2739 MethodInfo mi = (MethodInfo) mb;
2741 if (mi.ReturnType == to)
2750 // Determines "better conversion" as specified in 7.4.2.3
2751 // Returns : 1 if a->p is better
2752 // 0 if a->q or neither is better
2754 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2757 Type argument_type = a.Expr.Type;
2758 Expression argument_expr = a.Expr;
2760 if (argument_type == null)
2761 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2766 if (argument_type == p)
2769 if (argument_type == q)
2773 // Now probe whether an implicit constant expression conversion
2776 // An implicit constant expression conversion permits the following
2779 // * A constant-expression of type `int' can be converted to type
2780 // sbyte, byute, short, ushort, uint, ulong provided the value of
2781 // of the expression is withing the range of the destination type.
2783 // * A constant-expression of type long can be converted to type
2784 // ulong, provided the value of the constant expression is not negative
2786 // FIXME: Note that this assumes that constant folding has
2787 // taken place. We dont do constant folding yet.
2790 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2791 IntLiteral ei = (IntLiteral) argument_expr;
2792 int value = ei.Value;
2794 if (p == TypeManager.sbyte_type){
2795 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2797 } else if (p == TypeManager.byte_type){
2798 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2800 } else if (p == TypeManager.short_type){
2801 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2803 } else if (p == TypeManager.ushort_type){
2804 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2806 } else if (p == TypeManager.uint32_type){
2808 // we can optimize this case: a positive int32
2809 // always fits on a uint32
2813 } else if (p == TypeManager.uint64_type){
2815 // we can optimize this case: a positive int32
2816 // always fits on a uint64
2821 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2822 LongLiteral ll = (LongLiteral) argument_expr;
2824 if (p == TypeManager.uint64_type){
2834 tmp = ConvertImplicit (tc, argument_expr, p);
2843 Expression p_tmp, q_tmp;
2845 p_tmp = ConvertImplicit (tc, argument_expr, p);
2846 q_tmp = ConvertImplicit (tc, argument_expr, q);
2848 if (p_tmp != null && q_tmp == null)
2851 if (p == TypeManager.sbyte_type)
2852 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2853 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2856 if (p == TypeManager.short_type)
2857 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2858 q == TypeManager.uint64_type)
2861 if (p == TypeManager.int32_type)
2862 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2865 if (p == TypeManager.int64_type)
2866 if (q == TypeManager.uint64_type)
2873 // Determines "Better function" and returns an integer indicating :
2874 // 0 if candidate ain't better
2875 // 1 if candidate is better than the current best match
2877 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2879 ParameterData candidate_pd = GetParameterData (candidate);
2880 ParameterData best_pd;
2886 argument_count = args.Count;
2888 if (candidate_pd.Count == 0 && argument_count == 0)
2892 if (candidate_pd.Count == argument_count) {
2894 for (int j = argument_count; j > 0;) {
2897 Argument a = (Argument) args [j];
2899 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2916 best_pd = GetParameterData (best);
2918 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2919 int rating1 = 0, rating2 = 0;
2921 for (int j = argument_count; j > 0;) {
2925 Argument a = (Argument) args [j];
2927 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2928 best_pd.ParameterType (j));
2929 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2930 candidate_pd.ParameterType (j));
2936 if (rating1 > rating2)
2945 public static string FullMethodDesc (MethodBase mb)
2947 StringBuilder sb = new StringBuilder (mb.Name);
2948 ParameterData pd = GetParameterData (mb);
2951 for (int i = pd.Count; i > 0;) {
2953 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2959 return sb.ToString ();
2962 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2965 if (mg1 != null || mg2 != null) {
2967 MethodGroupExpr left_set = null, right_set = null;
2968 int length1 = 0, length2 = 0;
2971 left_set = (MethodGroupExpr) mg1;
2972 length1 = left_set.Methods.Length;
2976 right_set = (MethodGroupExpr) mg2;
2977 length2 = right_set.Methods.Length;
2980 MemberInfo [] miset = new MemberInfo [length1 + length2];
2981 if (left_set != null)
2982 left_set.Methods.CopyTo (miset, 0);
2983 if (right_set != null)
2984 right_set.Methods.CopyTo (miset, length1);
2986 MethodGroupExpr union = new MethodGroupExpr (miset);
2997 // Find the Applicable Function Members (7.4.2.1)
2999 // me: Method Group expression with the members to select.
3000 // it might contain constructors or methods (or anything
3001 // that maps to a method).
3003 // Arguments: ArrayList containing resolved Argument objects.
3005 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3006 // that is the best match of me on Arguments.
3009 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3010 ArrayList Arguments, Location loc)
3012 ArrayList afm = new ArrayList ();
3013 int best_match_idx = -1;
3014 MethodBase method = null;
3017 for (int i = me.Methods.Length; i > 0; ){
3019 MethodBase candidate = me.Methods [i];
3022 x = BetterFunction (tc, Arguments, candidate, method);
3028 method = me.Methods [best_match_idx];
3032 if (Arguments == null)
3035 argument_count = Arguments.Count;
3039 // Now we see if we can at least find a method with the same number of arguments
3040 // and then try doing implicit conversion on the arguments
3041 if (best_match_idx == -1) {
3043 for (int i = me.Methods.Length; i > 0;) {
3045 MethodBase mb = me.Methods [i];
3046 pd = GetParameterData (mb);
3048 if (pd.Count == argument_count) {
3050 method = me.Methods [best_match_idx];
3061 // And now convert implicitly, each argument to the required type
3063 pd = GetParameterData (method);
3065 for (int j = argument_count; j > 0;) {
3067 Argument a = (Argument) Arguments [j];
3068 Expression a_expr = a.Expr;
3070 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
3073 Error (tc, 1502, loc,
3074 "The best overloaded match for method '" + FullMethodDesc (method) +
3075 "' has some invalid arguments");
3076 Error (tc, 1503, loc,
3077 "Argument " + (j+1) +
3078 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
3079 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
3084 // Update the argument with the implicit conversion
3094 public override Expression Resolve (TypeContainer tc)
3097 // First, resolve the expression that is used to
3098 // trigger the invocation
3100 this.expr = expr.Resolve (tc);
3101 if (this.expr == null)
3104 if (!(this.expr is MethodGroupExpr)){
3105 report118 (tc, this.expr, "method group");
3110 // Next, evaluate all the expressions in the argument list
3112 if (Arguments != null){
3113 for (int i = Arguments.Count; i > 0;){
3115 Argument a = (Argument) Arguments [i];
3117 if (!a.Resolve (tc))
3122 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
3124 if (method == null){
3125 Error (tc, -6, Location,
3126 "Could not find any applicable function for this argument list");
3130 if (method is MethodInfo)
3131 type = ((MethodInfo)method).ReturnType;
3136 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3140 if (Arguments != null)
3141 top = Arguments.Count;
3145 for (int i = 0; i < top; i++){
3146 Argument a = (Argument) Arguments [i];
3152 public override void Emit (EmitContext ec)
3154 bool is_static = method.IsStatic;
3157 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3160 // If this is ourselves, push "this"
3162 if (mg.InstanceExpression == null){
3163 ec.ig.Emit (OpCodes.Ldarg_0);
3166 // Push the instance expression
3168 mg.InstanceExpression.Emit (ec);
3172 if (Arguments != null)
3173 EmitArguments (ec, method, Arguments);
3176 if (method is MethodInfo)
3177 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3179 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3181 if (method is MethodInfo)
3182 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3184 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3188 public override void EmitStatement (EmitContext ec)
3193 // Pop the return value if there is one
3195 if (method is MethodInfo){
3196 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3197 ec.ig.Emit (OpCodes.Pop);
3202 public class New : ExpressionStatement {
3209 public readonly NType NewType;
3210 public readonly ArrayList Arguments;
3211 public readonly string RequestedType;
3212 // These are for the case when we have an array
3213 public readonly string Rank;
3214 public readonly ArrayList Indices;
3215 public readonly ArrayList Initializers;
3218 MethodBase method = null;
3220 public New (string requested_type, ArrayList arguments, Location loc)
3222 RequestedType = requested_type;
3223 Arguments = arguments;
3224 NewType = NType.Object;
3228 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3230 RequestedType = requested_type;
3233 Initializers = initializers;
3234 NewType = NType.Array;
3238 public override Expression Resolve (TypeContainer tc)
3240 type = tc.LookupType (RequestedType, false);
3247 ml = MemberLookup (tc, type, ".ctor", false,
3248 MemberTypes.Constructor, AllBindingsFlags);
3250 if (! (ml is MethodGroupExpr)){
3252 // FIXME: Find proper error
3254 report118 (tc, ml, "method group");
3258 if (Arguments != null){
3259 for (int i = Arguments.Count; i > 0;){
3261 Argument a = (Argument) Arguments [i];
3263 if (!a.Resolve (tc))
3268 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3270 if (method == null) {
3271 Error (tc, -6, Location,
3272 "New invocation: Can not find a constructor for this argument list");
3279 public override void Emit (EmitContext ec)
3281 Invocation.EmitArguments (ec, method, Arguments);
3282 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3285 public override void EmitStatement (EmitContext ec)
3288 ec.ig.Emit (OpCodes.Pop);
3293 // Represents the `this' construct
3295 public class This : Expression, LValue {
3296 public override Expression Resolve (TypeContainer tc)
3298 eclass = ExprClass.Variable;
3299 type = tc.TypeBuilder;
3302 // FIXME: Verify that this is only used in instance contexts.
3307 public override void Emit (EmitContext ec)
3309 ec.ig.Emit (OpCodes.Ldarg_0);
3312 public void Store (EmitContext ec)
3315 // Assignment to the "this" variable.
3317 // FIXME: Apparently this is a bug that we
3318 // must catch as `this' seems to be readonly ;-)
3320 ec.ig.Emit (OpCodes.Starg, 0);
3323 public void AddressOf (EmitContext ec)
3325 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3329 public class TypeOf : Expression {
3330 public readonly string QueriedType;
3332 public TypeOf (string queried_type)
3334 QueriedType = queried_type;
3337 public override Expression Resolve (TypeContainer tc)
3339 type = tc.LookupType (QueriedType, false);
3344 eclass = ExprClass.Type;
3348 public override void Emit (EmitContext ec)
3350 throw new Exception ("Implement me");
3351 // FIXME: Implement.
3355 public class SizeOf : Expression {
3356 public readonly string QueriedType;
3358 public SizeOf (string queried_type)
3360 this.QueriedType = queried_type;
3363 public override Expression Resolve (TypeContainer tc)
3365 // FIXME: Implement;
3366 throw new Exception ("Unimplemented");
3370 public override void Emit (EmitContext ec)
3372 throw new Exception ("Implement me");
3376 public class MemberAccess : Expression {
3377 public readonly string Identifier;
3379 Expression member_lookup;
3381 public MemberAccess (Expression expr, string id)
3387 public Expression Expr {
3393 public override Expression Resolve (TypeContainer tc)
3395 Expression new_expression = expr.Resolve (tc);
3397 if (new_expression == null)
3400 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
3402 if (member_lookup is MethodGroupExpr){
3403 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3406 // Bind the instance expression to it
3408 // FIXME: This is a horrible way of detecting if it is
3409 // an instance expression. Figure out how to fix this.
3412 if (expr is LocalVariableReference ||
3413 expr is ParameterReference ||
3415 mg.InstanceExpression = expr;
3417 return member_lookup;
3418 } else if (member_lookup is FieldExpr){
3419 FieldExpr fe = (FieldExpr) member_lookup;
3423 return member_lookup;
3426 // FIXME: This should generate the proper node
3427 // ie, for a Property Access, it should like call it
3430 return member_lookup;
3433 public override void Emit (EmitContext ec)
3435 throw new Exception ("Should not happen I think");
3441 // Nodes of type Namespace are created during the semantic
3442 // analysis to resolve member_access/qualified_identifier/simple_name
3445 // They are born `resolved'.
3447 public class NamespaceExpr : Expression {
3448 public readonly string Name;
3450 public NamespaceExpr (string name)
3453 eclass = ExprClass.Namespace;
3456 public override Expression Resolve (TypeContainer tc)
3461 public override void Emit (EmitContext ec)
3463 throw new Exception ("Namespace expressions should never be emitted");
3468 // Fully resolved expression that evaluates to a type
3470 public class TypeExpr : Expression {
3471 public TypeExpr (Type t)
3474 eclass = ExprClass.Type;
3477 override public Expression Resolve (TypeContainer tc)
3482 override public void Emit (EmitContext ec)
3484 throw new Exception ("Implement me");
3489 // MethodGroup Expression.
3491 // This is a fully resolved expression that evaluates to a type
3493 public class MethodGroupExpr : Expression {
3494 public readonly MethodBase [] Methods;
3495 Expression instance_expression = null;
3497 public MethodGroupExpr (MemberInfo [] mi)
3499 Methods = new MethodBase [mi.Length];
3500 mi.CopyTo (Methods, 0);
3501 eclass = ExprClass.MethodGroup;
3505 // `A method group may have associated an instance expression'
3507 public Expression InstanceExpression {
3509 return instance_expression;
3513 instance_expression = value;
3517 override public Expression Resolve (TypeContainer tc)
3522 override public void Emit (EmitContext ec)
3524 throw new Exception ("This should never be reached");
3528 // Fully resolved expression that evaluates to a Field
3530 public class FieldExpr : Expression, LValue {
3531 public readonly FieldInfo FieldInfo;
3532 public Expression Instance;
3534 public FieldExpr (FieldInfo fi)
3537 eclass = ExprClass.Variable;
3538 type = fi.FieldType;
3541 override public Expression Resolve (TypeContainer tc)
3543 if (!FieldInfo.IsStatic){
3544 if (Instance == null){
3545 throw new Exception ("non-static FieldExpr without instance var\n" +
3546 "You have to assign the Instance variable\n" +
3547 "Of the FieldExpr to set this\n");
3550 Instance = Instance.Resolve (tc);
3551 if (Instance == null)
3558 override public void Emit (EmitContext ec)
3560 ILGenerator ig = ec.ig;
3562 if (FieldInfo.IsStatic)
3563 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3567 ig.Emit (OpCodes.Ldfld, FieldInfo);
3571 public void Store (EmitContext ec)
3573 if (FieldInfo.IsStatic)
3574 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3576 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3579 public void AddressOf (EmitContext ec)
3581 if (FieldInfo.IsStatic)
3582 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3585 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3591 // Fully resolved expression that evaluates to a Property
3593 public class PropertyExpr : Expression {
3594 public readonly PropertyInfo PropertyInfo;
3595 public readonly bool IsStatic;
3597 public PropertyExpr (PropertyInfo pi)
3600 eclass = ExprClass.PropertyAccess;
3603 MethodBase [] acc = pi.GetAccessors ();
3605 for (int i = 0; i < acc.Length; i++)
3606 if (acc [i].IsStatic)
3609 type = pi.PropertyType;
3612 override public Expression Resolve (TypeContainer tc)
3614 // We are born in resolved state.
3618 override public void Emit (EmitContext ec)
3620 // FIXME: Implement;
3621 throw new Exception ("Unimplemented");
3626 // Fully resolved expression that evaluates to a Expression
3628 public class EventExpr : Expression {
3629 public readonly EventInfo EventInfo;
3631 public EventExpr (EventInfo ei)
3634 eclass = ExprClass.EventAccess;
3637 override public Expression Resolve (TypeContainer tc)
3639 // We are born in resolved state.
3643 override public void Emit (EmitContext ec)
3645 throw new Exception ("Implement me");
3646 // FIXME: Implement.
3650 public class CheckedExpr : Expression {
3652 public Expression Expr;
3654 public CheckedExpr (Expression e)
3659 public override Expression Resolve (TypeContainer tc)
3661 Expr = Expr.Resolve (tc);
3666 eclass = Expr.ExprClass;
3671 public override void Emit (EmitContext ec)
3673 bool last_check = ec.CheckState;
3675 ec.CheckState = true;
3677 ec.CheckState = last_check;
3682 public class UnCheckedExpr : Expression {
3684 public Expression Expr;
3686 public UnCheckedExpr (Expression e)
3691 public override Expression Resolve (TypeContainer tc)
3693 Expr = Expr.Resolve (tc);
3698 eclass = Expr.ExprClass;
3703 public override void Emit (EmitContext ec)
3705 bool last_check = ec.CheckState;
3707 ec.CheckState = false;
3709 ec.CheckState = last_check;
3714 public class ElementAccess : Expression {
3716 public readonly ArrayList Arguments;
3717 public readonly Expression Expr;
3719 public ElementAccess (Expression e, ArrayList e_list)
3725 public override Expression Resolve (TypeContainer tc)
3727 // FIXME: Implement;
3728 throw new Exception ("Unimplemented");
3732 public override void Emit (EmitContext ec)
3734 // FIXME : Implement !
3735 throw new Exception ("Unimplemented");
3740 public class BaseAccess : Expression {
3742 public enum BaseAccessType {
3747 public readonly BaseAccessType BAType;
3748 public readonly string Member;
3749 public readonly ArrayList Arguments;
3751 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3759 public override Expression Resolve (TypeContainer tc)
3761 // FIXME: Implement;
3762 throw new Exception ("Unimplemented");
3766 public override void Emit (EmitContext ec)
3768 throw new Exception ("Unimplemented");
3772 public class UserImplicitCast : Expression {
3774 ArrayList arguments;
3776 public UserImplicitCast (MethodInfo method, ArrayList arguments)
3778 this.method = method;
3779 this.arguments = arguments;
3780 type = method.ReturnType;
3781 eclass = ExprClass.Value;
3784 public override Expression Resolve (TypeContainer tc)
3787 // We are born in a fully resolved state
3792 public static Expression CanConvert (TypeContainer tc, Expression source, Type target)
3794 Expression mg1, mg2;
3796 ArrayList arguments;
3798 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3799 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3801 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3803 if (union != null) {
3804 arguments = new ArrayList ();
3805 arguments.Add (new Argument (source, Argument.AType.Expression));
3807 method = Invocation.OverloadResolve (tc, union, arguments,
3808 new Location ("FIXME", 1, 1));
3810 if (method != null) {
3811 MethodInfo mi = (MethodInfo) method;
3813 if (mi.ReturnType == target)
3814 return new UserImplicitCast (mi, arguments);
3818 // If we have a boolean type, we need to check for the True
3819 // and False operators too.
3821 if (target == TypeManager.bool_type) {
3823 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3824 mg2 = MemberLookup (tc, target, "op_True", false);
3826 union = Invocation.MakeUnionSet (mg1, mg2);
3831 arguments = new ArrayList ();
3832 arguments.Add (new Argument (source, Argument.AType.Expression));
3834 method = Invocation.OverloadResolve (tc, union, arguments,
3835 new Location ("FIXME", 1, 1));
3836 if (method != null) {
3837 MethodInfo mi = (MethodInfo) method;
3839 if (mi.ReturnType == target)
3840 return new UserImplicitCast (mi, arguments);
3847 public override void Emit (EmitContext ec)
3849 ILGenerator ig = ec.ig;
3851 if (method != null) {
3853 // Note that operators are static anyway
3855 if (arguments != null)
3856 Invocation.EmitArguments (ec, method, arguments);
3858 if (method is MethodInfo)
3859 ig.Emit (OpCodes.Call, (MethodInfo) method);
3861 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3866 throw new Exception ("Implement me");