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 protected 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;
356 if (conversion_cache == null)
357 conversion_cache = new Hashtable ();
359 if (expr_type == target_type)
363 if (conversion_cache.Contains (expr_type + "=>" + target_type) == false)
364 conversion_cache.Add (expr_type + "=>" + target_type, e);
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 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
378 if (target_type == TypeManager.int64_type)
379 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
380 if (target_type == TypeManager.double_type)
381 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
382 if (target_type == TypeManager.float_type)
383 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
384 if (target_type == TypeManager.short_type)
385 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
386 if (target_type == TypeManager.decimal_type)
387 e = 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 e = new EmptyCast (expr, target_type);
398 if (target_type == TypeManager.uint64_type)
399 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
400 if (target_type == TypeManager.int64_type)
401 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
403 if (target_type == TypeManager.float_type)
404 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
405 if (target_type == TypeManager.double_type)
406 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
407 if (target_type == TypeManager.decimal_type)
408 e = 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 e = new EmptyCast (expr, target_type);
415 if (target_type == TypeManager.int64_type)
416 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
417 if (target_type == TypeManager.double_type)
418 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
419 if (target_type == TypeManager.float_type)
420 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
421 if (target_type == TypeManager.decimal_type)
422 e = 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 e = new EmptyCast (expr, target_type);
431 if (target_type == TypeManager.int32_type)
432 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
433 if (target_type == TypeManager.int64_type)
434 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
435 if (target_type == TypeManager.double_type)
436 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
437 if (target_type == TypeManager.float_type)
438 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
439 if (target_type == TypeManager.decimal_type)
440 e = 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 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
447 if (target_type == TypeManager.double_type)
448 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
449 if (target_type == TypeManager.float_type)
450 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
451 if (target_type == TypeManager.decimal_type)
452 e = 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 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
459 if (target_type == TypeManager.uint64_type)
460 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
461 if (target_type == TypeManager.double_type)
462 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
464 if (target_type == TypeManager.float_type)
465 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
467 if (target_type == TypeManager.decimal_type)
468 e = 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 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
477 if (target_type == TypeManager.float_type)
478 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
480 if (target_type == TypeManager.decimal_type)
481 e = 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 e = new EmptyCast (expr, target_type);
490 if (target_type == TypeManager.uint64_type)
491 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
492 if (target_type == TypeManager.int64_type)
493 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
494 if (target_type == TypeManager.float_type)
495 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
496 if (target_type == TypeManager.double_type)
497 e = new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
498 if (target_type == TypeManager.decimal_type)
499 e = InternalTypeConstructor (tc, expr, target_type);
503 if (conversion_cache.Contains (expr_type + "=>" + target_type) == false)
504 conversion_cache.Add (expr_type + "=>" + target_type, e);
508 e = ImplicitReferenceConversion (expr, target_type);
510 if (conversion_cache.Contains (expr_type + "=>" + target_type) == false)
511 conversion_cache.Add (expr_type + "=>" + target_type, e);
516 throw new Exception ("Lame loop detected");
520 e = UserImplicitCast.CanConvert (tc, expr, target_type);
524 if (conversion_cache.Contains (expr_type + "=>" + target_type) == false)
525 conversion_cache.Add (expr_type + "=>" + target_type, e);
530 // Could not find an implicit cast.
536 // Attemps to perform an implict constant conversion of the IntLiteral
537 // into a different data type using casts (See Implicit Constant
538 // Expression Conversions)
540 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
542 int value = il.Value;
544 if (target_type == TypeManager.sbyte_type){
545 if (value >= SByte.MinValue && value <= SByte.MaxValue)
547 } else if (target_type == TypeManager.byte_type){
548 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
550 } else if (target_type == TypeManager.short_type){
551 if (value >= Int16.MinValue && value <= Int16.MaxValue)
553 } else if (target_type == TypeManager.ushort_type){
554 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
556 } else if (target_type == TypeManager.uint32_type){
558 // we can optimize this case: a positive int32
559 // always fits on a uint32
563 } else if (target_type == TypeManager.uint64_type){
565 // we can optimize this case: a positive int32
566 // always fits on a uint64. But we need an opcode
570 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
577 // Attemptes to implicityly convert `target' into `type', using
578 // ConvertImplicit. If there is no implicit conversion, then
579 // an error is signaled
581 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
582 Type type, Location l)
586 e = ConvertImplicit (tc, target, type);
591 // Attempt to do the implicit constant expression conversions
593 if (target is IntLiteral){
594 e = TryImplicitIntConversion (type, (IntLiteral) target);
597 } else if (target is LongLiteral){
599 // Try the implicit constant expression conversion
600 // from long to ulong, instead of a nice routine,
603 if (((LongLiteral) target).Value > 0)
607 string msg = "Can not convert implicitly from `"+
608 TypeManager.CSharpName (target.Type) + "' to `" +
609 TypeManager.CSharpName (type) + "'";
611 Error (tc, 29, l, msg);
617 // Performs the explicit numeric conversions
619 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
622 Type expr_type = expr.Type;
624 if (expr_type == TypeManager.sbyte_type){
626 // From sbyte to byte, ushort, uint, ulong, char
628 if (target_type == TypeManager.byte_type)
629 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
630 if (target_type == TypeManager.ushort_type)
631 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
632 if (target_type == TypeManager.uint32_type)
633 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
634 if (target_type == TypeManager.uint64_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
636 if (target_type == TypeManager.char_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
638 } else if (expr_type == TypeManager.byte_type){
640 // From byte to sbyte and char
642 if (target_type == TypeManager.sbyte_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
644 if (target_type == TypeManager.char_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
646 } else if (expr_type == TypeManager.short_type){
648 // From short to sbyte, byte, ushort, uint, ulong, char
650 if (target_type == TypeManager.sbyte_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
652 if (target_type == TypeManager.byte_type)
653 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
654 if (target_type == TypeManager.ushort_type)
655 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
656 if (target_type == TypeManager.uint32_type)
657 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
658 if (target_type == TypeManager.uint64_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
660 if (target_type == TypeManager.char_type)
661 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
662 } else if (expr_type == TypeManager.ushort_type){
664 // From ushort to sbyte, byte, short, char
666 if (target_type == TypeManager.sbyte_type)
667 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
668 if (target_type == TypeManager.byte_type)
669 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
670 if (target_type == TypeManager.short_type)
671 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
672 if (target_type == TypeManager.char_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
674 } else if (expr_type == TypeManager.int32_type){
676 // From int to sbyte, byte, short, ushort, uint, ulong, char
678 if (target_type == TypeManager.sbyte_type)
679 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
680 if (target_type == TypeManager.byte_type)
681 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
682 if (target_type == TypeManager.short_type)
683 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
684 if (target_type == TypeManager.ushort_type)
685 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
686 if (target_type == TypeManager.uint32_type)
687 return new EmptyCast (expr, target_type);
688 if (target_type == TypeManager.uint64_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
690 if (target_type == TypeManager.char_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
692 } else if (expr_type == TypeManager.uint32_type){
694 // From uint to sbyte, byte, short, ushort, int, char
696 if (target_type == TypeManager.sbyte_type)
697 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
698 if (target_type == TypeManager.byte_type)
699 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
700 if (target_type == TypeManager.short_type)
701 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
702 if (target_type == TypeManager.ushort_type)
703 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
704 if (target_type == TypeManager.int32_type)
705 return new EmptyCast (expr, target_type);
706 if (target_type == TypeManager.char_type)
707 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
708 } else if (expr_type == TypeManager.int64_type){
710 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
712 if (target_type == TypeManager.sbyte_type)
713 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
714 if (target_type == TypeManager.byte_type)
715 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
716 if (target_type == TypeManager.short_type)
717 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
718 if (target_type == TypeManager.ushort_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
720 if (target_type == TypeManager.int32_type)
721 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
722 if (target_type == TypeManager.uint32_type)
723 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
724 if (target_type == TypeManager.uint64_type)
725 return new EmptyCast (expr, target_type);
726 if (target_type == TypeManager.char_type)
727 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
728 } else if (expr_type == TypeManager.uint64_type){
730 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
732 if (target_type == TypeManager.sbyte_type)
733 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
734 if (target_type == TypeManager.byte_type)
735 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
736 if (target_type == TypeManager.short_type)
737 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
738 if (target_type == TypeManager.ushort_type)
739 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
740 if (target_type == TypeManager.int32_type)
741 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
742 if (target_type == TypeManager.uint32_type)
743 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
744 if (target_type == TypeManager.int64_type)
745 return new EmptyCast (expr, target_type);
746 if (target_type == TypeManager.char_type)
747 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
748 } else if (expr_type == TypeManager.char_type){
750 // From char to sbyte, byte, short
752 if (target_type == TypeManager.sbyte_type)
753 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
754 if (target_type == TypeManager.byte_type)
755 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
756 if (target_type == TypeManager.short_type)
757 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
758 } else if (expr_type == TypeManager.float_type){
760 // From float to sbyte, byte, short,
761 // ushort, int, uint, long, 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.short_type)
769 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
770 if (target_type == TypeManager.ushort_type)
771 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
772 if (target_type == TypeManager.int32_type)
773 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
774 if (target_type == TypeManager.uint32_type)
775 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
776 if (target_type == TypeManager.int64_type)
777 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
778 if (target_type == TypeManager.uint64_type)
779 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
780 if (target_type == TypeManager.char_type)
781 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
782 if (target_type == TypeManager.decimal_type)
783 return InternalTypeConstructor (tc, expr, target_type);
784 } else if (expr_type == TypeManager.double_type){
786 // From double to byte, byte, short,
787 // ushort, int, uint, long, ulong,
788 // char, float or decimal
790 if (target_type == TypeManager.sbyte_type)
791 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
792 if (target_type == TypeManager.byte_type)
793 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
794 if (target_type == TypeManager.short_type)
795 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
796 if (target_type == TypeManager.ushort_type)
797 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
798 if (target_type == TypeManager.int32_type)
799 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
800 if (target_type == TypeManager.uint32_type)
801 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
802 if (target_type == TypeManager.int64_type)
803 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
804 if (target_type == TypeManager.uint64_type)
805 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
806 if (target_type == TypeManager.char_type)
807 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
808 if (target_type == TypeManager.float_type)
809 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
810 if (target_type == TypeManager.decimal_type)
811 return InternalTypeConstructor (tc, expr, target_type);
814 // decimal is taken care of by the op_Explicit methods.
820 // Performs an explicit conversion of the expression `expr' whose
821 // type is expr.Type to `target_type'.
823 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
828 if (Invocation.ConversionExists (tc, expr.Type, target_type) == true) {
829 ne = ConvertImplicit (tc, expr, target_type);
835 ne = ConvertNumericExplicit (tc, expr, target_type);
843 static string ExprClassName (ExprClass c)
846 case ExprClass.Invalid:
848 case ExprClass.Value:
850 case ExprClass.Variable:
852 case ExprClass.Namespace:
856 case ExprClass.MethodGroup:
857 return "method group";
858 case ExprClass.PropertyAccess:
859 return "property access";
860 case ExprClass.EventAccess:
861 return "event access";
862 case ExprClass.IndexerAccess:
863 return "indexer access";
864 case ExprClass.Nothing:
867 throw new Exception ("Should not happen");
871 // Reports that we were expecting `expr' to be of class `expected'
873 protected void report118 (TypeContainer tc, Expression expr, string expected)
875 Error (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
876 "' where an " + expected + " was expected");
881 // This is just a base class for expressions that can
882 // appear on statements (invocations, object creation,
883 // assignments, post/pre increment and decrement). The idea
884 // being that they would support an extra Emition interface that
885 // does not leave a result on the stack.
888 public abstract class ExpressionStatement : Expression {
891 // Requests the expression to be emitted in a `statement'
892 // context. This means that no new value is left on the
893 // stack after invoking this method (constrasted with
894 // Emit that will always leave a value on the stack).
896 public abstract void EmitStatement (EmitContext ec);
900 // This kind of cast is used to encapsulate the child
901 // whose type is child.Type into an expression that is
902 // reported to return "return_type". This is used to encapsulate
903 // expressions which have compatible types, but need to be dealt
904 // at higher levels with.
906 // For example, a "byte" expression could be encapsulated in one
907 // of these as an "unsigned int". The type for the expression
908 // would be "unsigned int".
912 public class EmptyCast : Expression {
913 protected Expression child;
915 public EmptyCast (Expression child, Type return_type)
917 ExprClass = child.ExprClass;
922 public override Expression Resolve (TypeContainer tc)
924 // This should never be invoked, we are born in fully
925 // initialized state.
930 public override void Emit (EmitContext ec)
937 // This kind of cast is used to encapsulate Value Types in objects.
939 // The effect of it is to box the value type emitted by the previous
942 public class BoxedCast : EmptyCast {
944 public BoxedCast (Expression expr)
945 : base (expr, TypeManager.object_type)
949 public override Expression Resolve (TypeContainer tc)
951 // This should never be invoked, we are born in fully
952 // initialized state.
957 public override void Emit (EmitContext ec)
960 ec.ig.Emit (OpCodes.Box, child.Type);
965 // This kind of cast is used to encapsulate a child expression
966 // that can be trivially converted to a target type using one or
967 // two opcodes. The opcodes are passed as arguments.
969 public class OpcodeCast : EmptyCast {
973 public OpcodeCast (Expression child, Type return_type, OpCode op)
974 : base (child, return_type)
978 second_valid = false;
981 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
982 : base (child, return_type)
990 public override Expression Resolve (TypeContainer tc)
992 // This should never be invoked, we are born in fully
993 // initialized state.
998 public override void Emit (EmitContext ec)
1010 // Unary expressions.
1014 // Unary implements unary expressions. It derives from
1015 // ExpressionStatement becuase the pre/post increment/decrement
1016 // operators can be used in a statement context.
1018 public class Unary : ExpressionStatement {
1019 public enum Operator {
1020 Addition, Subtraction, Negate, BitComplement,
1021 Indirection, AddressOf, PreIncrement,
1022 PreDecrement, PostIncrement, PostDecrement
1027 ArrayList Arguments;
1031 public Unary (Operator op, Expression expr, Location loc)
1035 this.location = loc;
1038 public Expression Expr {
1048 public Operator Oper {
1059 // Returns a stringified representation of the Operator
1064 case Operator.Addition:
1066 case Operator.Subtraction:
1068 case Operator.Negate:
1070 case Operator.BitComplement:
1072 case Operator.AddressOf:
1074 case Operator.Indirection:
1076 case Operator.PreIncrement : case Operator.PostIncrement :
1078 case Operator.PreDecrement : case Operator.PostDecrement :
1082 return oper.ToString ();
1085 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1087 if (expr.Type == target_type)
1090 return ConvertImplicit (tc, expr, target_type);
1093 void report23 (Report r, Type t)
1095 r.Error (23, "Operator " + OperName () + " cannot be applied to operand of type `" +
1096 TypeManager.CSharpName (t) + "'");
1100 // Returns whether an object of type `t' can be incremented
1101 // or decremented with add/sub (ie, basically whether we can
1102 // use pre-post incr-decr operations on it, but it is not a
1103 // System.Decimal, which we test elsewhere)
1105 static bool IsIncrementableNumber (Type t)
1107 return (t == TypeManager.sbyte_type) ||
1108 (t == TypeManager.byte_type) ||
1109 (t == TypeManager.short_type) ||
1110 (t == TypeManager.ushort_type) ||
1111 (t == TypeManager.int32_type) ||
1112 (t == TypeManager.uint32_type) ||
1113 (t == TypeManager.int64_type) ||
1114 (t == TypeManager.uint64_type) ||
1115 (t == TypeManager.char_type) ||
1116 (t.IsSubclassOf (TypeManager.enum_type)) ||
1117 (t == TypeManager.float_type) ||
1118 (t == TypeManager.double_type);
1121 Expression ResolveOperator (TypeContainer tc)
1123 Type expr_type = expr.Type;
1126 // Step 1: Perform Operator Overload location
1131 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1132 op_name = "op_Increment";
1133 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1134 op_name = "op_Decrement";
1136 op_name = "op_" + oper;
1138 mg = MemberLookup (tc, expr_type, op_name, false);
1141 Arguments = new ArrayList ();
1142 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1144 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg, Arguments, location);
1145 if (method != null) {
1146 MethodInfo mi = (MethodInfo) method;
1148 type = mi.ReturnType;
1154 // Step 2: Default operations on CLI native types.
1157 // Only perform numeric promotions on:
1160 if (expr_type == null)
1163 if (oper == Operator.Negate){
1164 if (expr_type != TypeManager.bool_type) {
1165 report23 (tc.RootContext.Report, expr.Type);
1169 type = TypeManager.bool_type;
1173 if (oper == Operator.BitComplement) {
1174 if (!((expr_type == TypeManager.int32_type) ||
1175 (expr_type == TypeManager.uint32_type) ||
1176 (expr_type == TypeManager.int64_type) ||
1177 (expr_type == TypeManager.uint64_type) ||
1178 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1179 report23 (tc.RootContext.Report, expr.Type);
1186 if (oper == Operator.Addition) {
1188 // A plus in front of something is just a no-op, so return the child.
1194 // Deals with -literals
1195 // int operator- (int x)
1196 // long operator- (long x)
1197 // float operator- (float f)
1198 // double operator- (double d)
1199 // decimal operator- (decimal d)
1201 if (oper == Operator.Subtraction){
1203 // Fold a "- Constant" into a negative constant
1206 Expression e = null;
1209 // Is this a constant?
1211 if (expr is IntLiteral)
1212 e = new IntLiteral (-((IntLiteral) expr).Value);
1213 else if (expr is LongLiteral)
1214 e = new LongLiteral (-((LongLiteral) expr).Value);
1215 else if (expr is FloatLiteral)
1216 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1217 else if (expr is DoubleLiteral)
1218 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1219 else if (expr is DecimalLiteral)
1220 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1228 // Not a constant we can optimize, perform numeric
1229 // promotions to int, long, double.
1232 // The following is inneficient, because we call
1233 // ConvertImplicit too many times.
1235 // It is also not clear if we should convert to Float
1236 // or Double initially.
1238 if (expr_type == TypeManager.uint32_type){
1240 // FIXME: handle exception to this rule that
1241 // permits the int value -2147483648 (-2^31) to
1242 // bt written as a decimal interger literal
1244 type = TypeManager.int64_type;
1245 expr = ConvertImplicit (tc, expr, type);
1249 if (expr_type == TypeManager.uint64_type){
1251 // FIXME: Handle exception of `long value'
1252 // -92233720368547758087 (-2^63) to be written as
1253 // decimal integer literal.
1255 report23 (tc.RootContext.Report, expr_type);
1259 e = ConvertImplicit (tc, expr, TypeManager.int32_type);
1266 e = ConvertImplicit (tc, expr, TypeManager.int64_type);
1273 e = ConvertImplicit (tc, expr, TypeManager.double_type);
1280 report23 (tc.RootContext.Report, expr_type);
1285 // The operand of the prefix/postfix increment decrement operators
1286 // should be an expression that is classified as a variable,
1287 // a property access or an indexer access
1289 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1290 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1291 if (expr.ExprClass == ExprClass.Variable){
1292 if (IsIncrementableNumber (expr_type) ||
1293 expr_type == TypeManager.decimal_type){
1297 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1299 // FIXME: Verify that we have both get and set methods
1301 throw new Exception ("Implement me");
1302 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1304 // FIXME: Verify that we have both get and set methods
1306 throw new Exception ("Implement me");
1308 report118 (tc, expr, "variable, indexer or property access");
1312 if (oper == Operator.AddressOf){
1313 if (expr.ExprClass != ExprClass.Variable){
1314 Error (tc, 211, "Cannot take the address of non-variables");
1317 type = Type.GetType (expr.Type.ToString () + "*");
1320 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1321 TypeManager.CSharpName (expr_type) + "'");
1326 public override Expression Resolve (TypeContainer tc)
1328 expr = expr.Resolve (tc);
1333 return ResolveOperator (tc);
1336 public override void Emit (EmitContext ec)
1338 ILGenerator ig = ec.ig;
1339 Type expr_type = expr.Type;
1341 if (method != null) {
1343 // Note that operators are static anyway
1345 if (Arguments != null)
1346 Invocation.EmitArguments (ec, method, Arguments);
1349 // Post increment/decrement operations need a copy at this
1352 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1353 ig.Emit (OpCodes.Dup);
1356 ig.Emit (OpCodes.Call, (MethodInfo) method);
1359 // Pre Increment and Decrement operators
1361 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1362 ig.Emit (OpCodes.Dup);
1366 // Increment and Decrement should store the result
1368 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1369 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1370 ((LValue) expr).Store (ec);
1376 case Operator.Addition:
1377 throw new Exception ("This should be caught by Resolve");
1379 case Operator.Subtraction:
1381 ig.Emit (OpCodes.Neg);
1384 case Operator.Negate:
1386 ig.Emit (OpCodes.Ldc_I4_0);
1387 ig.Emit (OpCodes.Ceq);
1390 case Operator.BitComplement:
1392 ig.Emit (OpCodes.Not);
1395 case Operator.AddressOf:
1396 ((LValue)expr).AddressOf (ec);
1399 case Operator.Indirection:
1400 throw new Exception ("Not implemented yet");
1402 case Operator.PreIncrement:
1403 case Operator.PreDecrement:
1404 if (expr.ExprClass == ExprClass.Variable){
1406 // Resolve already verified that it is an "incrementable"
1409 ig.Emit (OpCodes.Ldc_I4_1);
1411 if (oper == Operator.PreDecrement)
1412 ig.Emit (OpCodes.Sub);
1414 ig.Emit (OpCodes.Add);
1415 ig.Emit (OpCodes.Dup);
1416 ((LValue) expr).Store (ec);
1418 throw new Exception ("Handle Indexers and Properties here");
1422 case Operator.PostIncrement:
1423 case Operator.PostDecrement:
1424 if (expr.ExprClass == ExprClass.Variable){
1426 // Resolve already verified that it is an "incrementable"
1429 ig.Emit (OpCodes.Dup);
1430 ig.Emit (OpCodes.Ldc_I4_1);
1432 if (oper == Operator.PostDecrement)
1433 ig.Emit (OpCodes.Sub);
1435 ig.Emit (OpCodes.Add);
1436 ((LValue) expr).Store (ec);
1438 throw new Exception ("Handle Indexers and Properties here");
1443 throw new Exception ("This should not happen: Operator = "
1444 + oper.ToString ());
1449 public override void EmitStatement (EmitContext ec)
1452 // FIXME: we should rewrite this code to generate
1453 // better code for ++ and -- as we know we wont need
1454 // the values on the stack
1457 ec.ig.Emit (OpCodes.Pop);
1461 public class Probe : Expression {
1462 public readonly string ProbeType;
1463 public readonly Operator Oper;
1467 public enum Operator {
1471 public Probe (Operator oper, Expression expr, string probe_type)
1474 ProbeType = probe_type;
1478 public Expression Expr {
1484 public override Expression Resolve (TypeContainer tc)
1486 probe_type = tc.LookupType (ProbeType, false);
1488 if (probe_type == null)
1491 expr = expr.Resolve (tc);
1493 type = TypeManager.bool_type;
1494 eclass = ExprClass.Value;
1499 public override void Emit (EmitContext ec)
1503 if (Oper == Operator.Is){
1504 ec.ig.Emit (OpCodes.Isinst, probe_type);
1506 throw new Exception ("Implement as");
1512 // This represents a typecast in the source language.
1514 // FIXME: Cast expressions have an unusual set of parsing
1515 // rules, we need to figure those out.
1517 public class Cast : Expression {
1521 public Cast (string cast_type, Expression expr)
1523 this.target_type = cast_type;
1527 public string TargetType {
1533 public Expression Expr {
1542 public override Expression Resolve (TypeContainer tc)
1544 expr = expr.Resolve (tc);
1548 type = tc.LookupType (target_type, false);
1549 eclass = ExprClass.Value;
1554 expr = ConvertExplicit (tc, expr, type);
1559 public override void Emit (EmitContext ec)
1562 // This one will never happen
1564 throw new Exception ("Should not happen");
1568 public class Binary : Expression {
1569 public enum Operator {
1570 Multiply, Division, Modulus,
1571 Addition, Subtraction,
1572 LeftShift, RightShift,
1573 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1574 Equality, Inequality,
1583 Expression left, right;
1585 ArrayList Arguments;
1589 public Binary (Operator oper, Expression left, Expression right, Location loc)
1594 this.location = loc;
1597 public Operator Oper {
1606 public Expression Left {
1615 public Expression Right {
1626 // Returns a stringified representation of the Operator
1631 case Operator.Multiply:
1633 case Operator.Division:
1635 case Operator.Modulus:
1637 case Operator.Addition:
1639 case Operator.Subtraction:
1641 case Operator.LeftShift:
1643 case Operator.RightShift:
1645 case Operator.LessThan:
1647 case Operator.GreaterThan:
1649 case Operator.LessThanOrEqual:
1651 case Operator.GreaterThanOrEqual:
1653 case Operator.Equality:
1655 case Operator.Inequality:
1657 case Operator.BitwiseAnd:
1659 case Operator.BitwiseOr:
1661 case Operator.ExclusiveOr:
1663 case Operator.LogicalOr:
1665 case Operator.LogicalAnd:
1669 return oper.ToString ();
1672 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1674 if (expr.Type == target_type)
1677 return ConvertImplicit (tc, expr, target_type);
1681 // Note that handling the case l == Decimal || r == Decimal
1682 // is taken care of by the Step 1 Operator Overload resolution.
1684 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1686 if (l == TypeManager.double_type || r == TypeManager.double_type){
1688 // If either operand is of type double, the other operand is
1689 // conveted to type double.
1691 if (r != TypeManager.double_type)
1692 right = ConvertImplicit (tc, right, TypeManager.double_type);
1693 if (l != TypeManager.double_type)
1694 left = ConvertImplicit (tc, left, TypeManager.double_type);
1696 type = TypeManager.double_type;
1697 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1699 // if either operand is of type float, th eother operand is
1700 // converd to type float.
1702 if (r != TypeManager.double_type)
1703 right = ConvertImplicit (tc, right, TypeManager.float_type);
1704 if (l != TypeManager.double_type)
1705 left = ConvertImplicit (tc, left, TypeManager.float_type);
1706 type = TypeManager.float_type;
1707 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1709 // If either operand is of type ulong, the other operand is
1710 // converted to type ulong. or an error ocurrs if the other
1711 // operand is of type sbyte, short, int or long
1715 if (l == TypeManager.uint64_type)
1717 else if (r == TypeManager.uint64_type)
1720 if ((other == TypeManager.sbyte_type) ||
1721 (other == TypeManager.short_type) ||
1722 (other == TypeManager.int32_type) ||
1723 (other == TypeManager.int64_type)){
1724 string oper = OperName ();
1726 Error (tc, 34, "Operator `" + OperName ()
1727 + "' is ambiguous on operands of type `"
1728 + TypeManager.CSharpName (l) + "' "
1729 + "and `" + TypeManager.CSharpName (r)
1732 type = TypeManager.uint64_type;
1733 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1735 // If either operand is of type long, the other operand is converted
1738 if (l != TypeManager.int64_type)
1739 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1740 if (r != TypeManager.int64_type)
1741 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1743 type = TypeManager.int64_type;
1744 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1746 // If either operand is of type uint, and the other
1747 // operand is of type sbyte, short or int, othe operands are
1748 // converted to type long.
1752 if (l == TypeManager.uint32_type)
1754 else if (r == TypeManager.uint32_type)
1757 if ((other == TypeManager.sbyte_type) ||
1758 (other == TypeManager.short_type) ||
1759 (other == TypeManager.int32_type)){
1760 left = ForceConversion (tc, left, TypeManager.int64_type);
1761 right = ForceConversion (tc, right, TypeManager.int64_type);
1762 type = TypeManager.int64_type;
1765 // if either operand is of type uint, the other
1766 // operand is converd to type uint
1768 left = ForceConversion (tc, left, TypeManager.uint32_type);
1769 right = ForceConversion (tc, left, TypeManager.uint32_type);
1770 type = TypeManager.uint32_type;
1772 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1773 if (l != TypeManager.decimal_type)
1774 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1775 if (r != TypeManager.decimal_type)
1776 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1778 type = TypeManager.decimal_type;
1780 left = ForceConversion (tc, left, TypeManager.int32_type);
1781 right = ForceConversion (tc, right, TypeManager.int32_type);
1782 type = TypeManager.int32_type;
1786 void error19 (TypeContainer tc)
1789 "Operator " + OperName () + " cannot be applied to operands of type `" +
1790 TypeManager.CSharpName (left.Type) + "' and `" +
1791 TypeManager.CSharpName (right.Type) + "'");
1795 Expression CheckShiftArguments (TypeContainer tc)
1799 Type r = right.Type;
1801 e = ForceConversion (tc, right, TypeManager.int32_type);
1808 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1809 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1810 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1811 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1820 Expression ResolveOperator (TypeContainer tc)
1823 Type r = right.Type;
1826 // Step 1: Perform Operator Overload location
1828 Expression left_expr, right_expr;
1830 string op = "op_" + oper;
1832 left_expr = MemberLookup (tc, l, op, false);
1834 right_expr = MemberLookup (tc, r, op, false);
1836 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1838 Arguments = new ArrayList ();
1839 Arguments.Add (new Argument (left, Argument.AType.Expression));
1840 Arguments.Add (new Argument (right, Argument.AType.Expression));
1842 if (union != null) {
1843 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1844 if (method != null) {
1845 MethodInfo mi = (MethodInfo) method;
1847 type = mi.ReturnType;
1853 // Step 2: Default operations on CLI native types.
1856 // Only perform numeric promotions on:
1857 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1859 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1860 return CheckShiftArguments (tc);
1861 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1863 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1865 } else if (oper == Operator.Addition){
1867 // If any of the arguments is a string, cast to string
1869 if (l == TypeManager.string_type){
1870 if (r == TypeManager.string_type){
1872 method = TypeManager.string_concat_string_string;
1875 method = TypeManager.string_concat_object_object;
1876 right = ConvertImplicit (tc, right, TypeManager.object_type);
1878 type = TypeManager.string_type;
1880 Arguments = new ArrayList ();
1881 Arguments.Add (new Argument (left, Argument.AType.Expression));
1882 Arguments.Add (new Argument (right, Argument.AType.Expression));
1883 } else if (r == TypeManager.string_type){
1885 method = TypeManager.string_concat_object_object;
1886 Arguments = new ArrayList ();
1887 Arguments.Add (new Argument (left, Argument.AType.Expression));
1888 Arguments.Add (new Argument (right, Argument.AType.Expression));
1890 left = ConvertImplicit (tc, left, TypeManager.object_type);
1891 type = TypeManager.string_type;
1895 // FIXME: is Delegate operator + (D x, D y) handled?
1898 DoNumericPromotions (tc, l, r);
1900 if (left == null || right == null)
1904 if (oper == Operator.BitwiseAnd ||
1905 oper == Operator.BitwiseOr ||
1906 oper == Operator.ExclusiveOr){
1907 if (!((l == TypeManager.int32_type) ||
1908 (l == TypeManager.uint32_type) ||
1909 (l == TypeManager.int64_type) ||
1910 (l == TypeManager.uint64_type))){
1916 if (oper == Operator.Equality ||
1917 oper == Operator.Inequality ||
1918 oper == Operator.LessThanOrEqual ||
1919 oper == Operator.LessThan ||
1920 oper == Operator.GreaterThanOrEqual ||
1921 oper == Operator.GreaterThan){
1922 type = TypeManager.bool_type;
1928 public override Expression Resolve (TypeContainer tc)
1930 left = left.Resolve (tc);
1931 right = right.Resolve (tc);
1933 if (left == null || right == null)
1936 return ResolveOperator (tc);
1939 public bool IsBranchable ()
1941 if (oper == Operator.Equality ||
1942 oper == Operator.Inequality ||
1943 oper == Operator.LessThan ||
1944 oper == Operator.GreaterThan ||
1945 oper == Operator.LessThanOrEqual ||
1946 oper == Operator.GreaterThanOrEqual){
1953 // This entry point is used by routines that might want
1954 // to emit a brfalse/brtrue after an expression, and instead
1955 // they could use a more compact notation.
1957 // Typically the code would generate l.emit/r.emit, followed
1958 // by the comparission and then a brtrue/brfalse. The comparissions
1959 // are sometimes inneficient (there are not as complete as the branches
1960 // look for the hacks in Emit using double ceqs).
1962 // So for those cases we provide EmitBranchable that can emit the
1963 // branch with the test
1965 public void EmitBranchable (EmitContext ec, int target)
1968 bool close_target = false;
1974 case Operator.Equality:
1976 opcode = OpCodes.Beq_S;
1978 opcode = OpCodes.Beq;
1981 case Operator.Inequality:
1983 opcode = OpCodes.Bne_Un_S;
1985 opcode = OpCodes.Bne_Un;
1988 case Operator.LessThan:
1990 opcode = OpCodes.Blt_S;
1992 opcode = OpCodes.Blt;
1995 case Operator.GreaterThan:
1997 opcode = OpCodes.Bgt_S;
1999 opcode = OpCodes.Bgt;
2002 case Operator.LessThanOrEqual:
2004 opcode = OpCodes.Ble_S;
2006 opcode = OpCodes.Ble;
2009 case Operator.GreaterThanOrEqual:
2011 opcode = OpCodes.Bge_S;
2013 opcode = OpCodes.Ble;
2017 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2018 + oper.ToString ());
2021 ec.ig.Emit (opcode, target);
2024 public override void Emit (EmitContext ec)
2026 ILGenerator ig = ec.ig;
2028 Type r = right.Type;
2031 if (method != null) {
2033 // Note that operators are static anyway
2035 if (Arguments != null)
2036 Invocation.EmitArguments (ec, method, Arguments);
2038 if (method is MethodInfo)
2039 ig.Emit (OpCodes.Call, (MethodInfo) method);
2041 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2050 case Operator.Multiply:
2052 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2053 opcode = OpCodes.Mul_Ovf;
2054 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2055 opcode = OpCodes.Mul_Ovf_Un;
2057 opcode = OpCodes.Mul;
2059 opcode = OpCodes.Mul;
2063 case Operator.Division:
2064 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2065 opcode = OpCodes.Div_Un;
2067 opcode = OpCodes.Div;
2070 case Operator.Modulus:
2071 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2072 opcode = OpCodes.Rem_Un;
2074 opcode = OpCodes.Rem;
2077 case Operator.Addition:
2079 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2080 opcode = OpCodes.Add_Ovf;
2081 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2082 opcode = OpCodes.Add_Ovf_Un;
2084 opcode = OpCodes.Mul;
2086 opcode = OpCodes.Add;
2089 case Operator.Subtraction:
2091 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2092 opcode = OpCodes.Sub_Ovf;
2093 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2094 opcode = OpCodes.Sub_Ovf_Un;
2096 opcode = OpCodes.Sub;
2098 opcode = OpCodes.Sub;
2101 case Operator.RightShift:
2102 opcode = OpCodes.Shr;
2105 case Operator.LeftShift:
2106 opcode = OpCodes.Shl;
2109 case Operator.Equality:
2110 opcode = OpCodes.Ceq;
2113 case Operator.Inequality:
2114 ec.ig.Emit (OpCodes.Ceq);
2115 ec.ig.Emit (OpCodes.Ldc_I4_0);
2117 opcode = OpCodes.Ceq;
2120 case Operator.LessThan:
2121 opcode = OpCodes.Clt;
2124 case Operator.GreaterThan:
2125 opcode = OpCodes.Cgt;
2128 case Operator.LessThanOrEqual:
2129 ec.ig.Emit (OpCodes.Cgt);
2130 ec.ig.Emit (OpCodes.Ldc_I4_0);
2132 opcode = OpCodes.Ceq;
2135 case Operator.GreaterThanOrEqual:
2136 ec.ig.Emit (OpCodes.Clt);
2137 ec.ig.Emit (OpCodes.Ldc_I4_1);
2139 opcode = OpCodes.Sub;
2142 case Operator.LogicalOr:
2143 case Operator.BitwiseOr:
2144 opcode = OpCodes.Or;
2147 case Operator.LogicalAnd:
2148 case Operator.BitwiseAnd:
2149 opcode = OpCodes.And;
2152 case Operator.ExclusiveOr:
2153 opcode = OpCodes.Xor;
2157 throw new Exception ("This should not happen: Operator = "
2158 + oper.ToString ());
2165 public class Conditional : Expression {
2166 Expression expr, trueExpr, falseExpr;
2169 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2172 this.trueExpr = trueExpr;
2173 this.falseExpr = falseExpr;
2177 public Expression Expr {
2183 public Expression TrueExpr {
2189 public Expression FalseExpr {
2195 public override Expression Resolve (TypeContainer tc)
2197 expr = expr.Resolve (tc);
2199 if (expr.Type != TypeManager.bool_type)
2200 expr = Expression.ConvertImplicitRequired (
2201 tc, expr, TypeManager.bool_type, l);
2203 trueExpr = trueExpr.Resolve (tc);
2204 falseExpr = falseExpr.Resolve (tc);
2206 if (expr == null || trueExpr == null || falseExpr == null)
2209 if (trueExpr.Type == falseExpr.Type)
2210 type = trueExpr.Type;
2215 // First, if an implicit conversion exists from trueExpr
2216 // to falseExpr, then the result type is of type falseExpr.Type
2218 conv = ConvertImplicit (tc, trueExpr, falseExpr.Type);
2220 type = falseExpr.Type;
2222 } else if ((conv = ConvertImplicit (tc, falseExpr, trueExpr.Type)) != null){
2223 type = trueExpr.Type;
2226 Error (tc, 173, l, "The type of the conditional expression can " +
2227 "not be computed because there is no implicit conversion" +
2228 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2229 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2234 eclass = ExprClass.Value;
2238 public override void Emit (EmitContext ec)
2240 ILGenerator ig = ec.ig;
2241 Label false_target = ig.DefineLabel ();
2242 Label end_target = ig.DefineLabel ();
2245 ig.Emit (OpCodes.Brfalse, false_target);
2247 ig.Emit (OpCodes.Br, end_target);
2248 ig.MarkLabel (false_target);
2249 falseExpr.Emit (ec);
2250 ig.MarkLabel (end_target);
2254 public class SimpleName : Expression {
2255 public readonly string Name;
2256 public readonly Location Location;
2258 public SimpleName (string name, Location l)
2265 // Checks whether we are trying to access an instance
2266 // property, method or field from a static body.
2268 Expression MemberStaticCheck (Report r, Expression e)
2270 if (e is FieldExpr){
2271 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2275 "An object reference is required " +
2276 "for the non-static field `"+Name+"'");
2279 } else if (e is MethodGroupExpr){
2280 // FIXME: Pending reorganization of MemberLookup
2281 // Basically at this point we should have the
2282 // best match already selected for us, and
2283 // we should only have to check a *single*
2284 // Method for its static on/off bit.
2286 } else if (e is PropertyExpr){
2287 if (!((PropertyExpr) e).IsStatic){
2289 "An object reference is required " +
2290 "for the non-static property access `"+
2300 // 7.5.2: Simple Names.
2302 // Local Variables and Parameters are handled at
2303 // parse time, so they never occur as SimpleNames.
2305 Expression ResolveSimpleName (TypeContainer tc)
2308 Report r = tc.RootContext.Report;
2310 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2314 else if (e is FieldExpr){
2315 FieldExpr fe = (FieldExpr) e;
2317 if (!fe.FieldInfo.IsStatic)
2318 fe.Instance = new This ();
2321 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2322 return MemberStaticCheck (r, e);
2328 // Do step 3 of the Simple Name resolution.
2330 // FIXME: implement me.
2332 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2339 // SimpleName needs to handle a multitude of cases:
2341 // simple_names and qualified_identifiers are placed on
2342 // the tree equally.
2344 public override Expression Resolve (TypeContainer tc)
2346 if (Name.IndexOf (".") != -1)
2347 return ResolveMemberAccess (tc, Name);
2349 return ResolveSimpleName (tc);
2352 public override void Emit (EmitContext ec)
2354 throw new Exception ("SimpleNames should be gone from the tree");
2359 // A simple interface that should be implemeneted by LValues
2361 public interface LValue {
2364 // The Store method should store the contents of the top
2365 // of the stack into the storage that is implemented by
2366 // the particular implementation of LValue
2368 void Store (EmitContext ec);
2371 // The AddressOf method should generate code that loads
2372 // the address of the LValue and leaves it on the stack
2374 void AddressOf (EmitContext ec);
2377 public class LocalVariableReference : Expression, LValue {
2378 public readonly string Name;
2379 public readonly Block Block;
2381 public LocalVariableReference (Block block, string name)
2385 eclass = ExprClass.Variable;
2388 public VariableInfo VariableInfo {
2390 return Block.GetVariableInfo (Name);
2394 public override Expression Resolve (TypeContainer tc)
2396 VariableInfo vi = Block.GetVariableInfo (Name);
2398 type = vi.VariableType;
2402 public override void Emit (EmitContext ec)
2404 VariableInfo vi = VariableInfo;
2405 ILGenerator ig = ec.ig;
2412 ig.Emit (OpCodes.Ldloc_0);
2416 ig.Emit (OpCodes.Ldloc_1);
2420 ig.Emit (OpCodes.Ldloc_2);
2424 ig.Emit (OpCodes.Ldloc_3);
2429 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2431 ig.Emit (OpCodes.Ldloc, idx);
2436 public void Store (EmitContext ec)
2438 ILGenerator ig = ec.ig;
2439 VariableInfo vi = VariableInfo;
2445 ig.Emit (OpCodes.Stloc_0);
2449 ig.Emit (OpCodes.Stloc_1);
2453 ig.Emit (OpCodes.Stloc_2);
2457 ig.Emit (OpCodes.Stloc_3);
2462 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2464 ig.Emit (OpCodes.Stloc, idx);
2469 public void AddressOf (EmitContext ec)
2471 VariableInfo vi = VariableInfo;
2478 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2480 ec.ig.Emit (OpCodes.Ldloca, idx);
2484 public class ParameterReference : Expression, LValue {
2485 public readonly Parameters Pars;
2486 public readonly String Name;
2487 public readonly int Idx;
2489 public ParameterReference (Parameters pars, int idx, string name)
2494 eclass = ExprClass.Variable;
2497 public override Expression Resolve (TypeContainer tc)
2499 Type [] types = Pars.GetParameterInfo (tc);
2506 public override void Emit (EmitContext ec)
2509 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
2511 ec.ig.Emit (OpCodes.Ldarg, Idx);
2514 public void Store (EmitContext ec)
2517 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
2519 ec.ig.Emit (OpCodes.Starg, Idx);
2523 public void AddressOf (EmitContext ec)
2526 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
2528 ec.ig.Emit (OpCodes.Ldarga, Idx);
2533 // Used for arguments to New(), Invocation()
2535 public class Argument {
2542 public readonly AType Type;
2545 public Argument (Expression expr, AType type)
2551 public Expression Expr {
2561 public bool Resolve (TypeContainer tc)
2563 expr = expr.Resolve (tc);
2565 return expr != null;
2568 public void Emit (EmitContext ec)
2575 // Invocation of methods or delegates.
2577 public class Invocation : ExpressionStatement {
2578 public readonly ArrayList Arguments;
2579 public readonly Location Location;
2582 MethodBase method = null;
2584 static Hashtable method_parameter_cache;
2586 static Invocation ()
2588 method_parameter_cache = new Hashtable ();
2592 // arguments is an ArrayList, but we do not want to typecast,
2593 // as it might be null.
2595 // FIXME: only allow expr to be a method invocation or a
2596 // delegate invocation (7.5.5)
2598 public Invocation (Expression expr, ArrayList arguments, Location l)
2601 Arguments = arguments;
2605 public Expression Expr {
2612 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2613 /// compatible, and if so, how good is the match (in terms of
2614 /// "better conversions" (7.4.2.3).
2616 /// 0 is the best possible match.
2617 /// -1 represents a type mismatch.
2618 /// -2 represents a ref/out mismatch.
2620 static int Badness (Argument a, Type t)
2622 Expression argument_expr = a.Expr;
2623 Type argument_type = argument_expr.Type;
2625 if (argument_type == null){
2626 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2629 if (t == argument_type)
2633 // Now probe whether an implicit constant expression conversion
2636 // An implicit constant expression conversion permits the following
2639 // * A constant-expression of type `int' can be converted to type
2640 // sbyte, byute, short, ushort, uint, ulong provided the value of
2641 // of the expression is withing the range of the destination type.
2643 // * A constant-expression of type long can be converted to type
2644 // ulong, provided the value of the constant expression is not negative
2646 // FIXME: Note that this assumes that constant folding has
2647 // taken place. We dont do constant folding yet.
2650 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2651 IntLiteral ei = (IntLiteral) argument_expr;
2652 int value = ei.Value;
2654 if (t == TypeManager.sbyte_type){
2655 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2657 } else if (t == TypeManager.byte_type){
2658 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2660 } else if (t == TypeManager.short_type){
2661 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2663 } else if (t == TypeManager.ushort_type){
2664 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2666 } else if (t == TypeManager.uint32_type){
2668 // we can optimize this case: a positive int32
2669 // always fits on a uint32
2673 } else if (t == TypeManager.uint64_type){
2675 // we can optimize this case: a positive int32
2676 // always fits on a uint64
2681 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2682 LongLiteral ll = (LongLiteral) argument_expr;
2684 if (t == TypeManager.uint64_type)
2689 // FIXME: Implement user-defined implicit conversions here.
2690 // FIXME: Implement better conversion here.
2696 // Returns the Parameters (a ParameterData interface) for the
2699 static ParameterData GetParameterData (MethodBase mb)
2701 object pd = method_parameter_cache [mb];
2704 return (ParameterData) pd;
2706 if (mb is MethodBuilder || mb is ConstructorBuilder){
2707 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2709 InternalParameters ip = mc.ParameterInfo;
2710 method_parameter_cache [mb] = ip;
2712 return (ParameterData) ip;
2714 ParameterInfo [] pi = mb.GetParameters ();
2715 ReflectionParameters rp = new ReflectionParameters (pi);
2716 method_parameter_cache [mb] = rp;
2718 return (ParameterData) rp;
2722 public static bool ConversionExists (TypeContainer tc, Type from, Type to)
2724 if (conversion_cache == null)
2727 Expression e = (Expression) conversion_cache [from + "=>" + to];
2736 // Determines "better conversion" as specified in 7.4.2.3
2737 // Returns : 1 if a->p is better
2738 // 0 if a->q or neither is better
2740 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2743 Type argument_type = a.Expr.Type;
2744 Expression argument_expr = a.Expr;
2746 if (argument_type == null)
2747 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2752 if (argument_type == p)
2755 if (argument_type == q)
2759 // Now probe whether an implicit constant expression conversion
2762 // An implicit constant expression conversion permits the following
2765 // * A constant-expression of type `int' can be converted to type
2766 // sbyte, byute, short, ushort, uint, ulong provided the value of
2767 // of the expression is withing the range of the destination type.
2769 // * A constant-expression of type long can be converted to type
2770 // ulong, provided the value of the constant expression is not negative
2772 // FIXME: Note that this assumes that constant folding has
2773 // taken place. We dont do constant folding yet.
2776 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2777 IntLiteral ei = (IntLiteral) argument_expr;
2778 int value = ei.Value;
2780 if (p == TypeManager.sbyte_type){
2781 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2783 } else if (p == TypeManager.byte_type){
2784 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2786 } else if (p == TypeManager.short_type){
2787 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2789 } else if (p == TypeManager.ushort_type){
2790 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2792 } else if (p == TypeManager.uint32_type){
2794 // we can optimize this case: a positive int32
2795 // always fits on a uint32
2799 } else if (p == TypeManager.uint64_type){
2801 // we can optimize this case: a positive int32
2802 // always fits on a uint64
2807 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2808 LongLiteral ll = (LongLiteral) argument_expr;
2810 if (p == TypeManager.uint64_type){
2817 if (ConversionExists (tc, argument_type, p) == true)
2823 if (ConversionExists (tc, p, q) == true &&
2824 ConversionExists (tc, q, p) == false)
2828 if (p == TypeManager.sbyte_type)
2829 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2830 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2833 if (p == TypeManager.short_type)
2834 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2835 q == TypeManager.uint64_type)
2838 if (p == TypeManager.int32_type)
2839 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2842 if (p == TypeManager.int64_type)
2843 if (q == TypeManager.uint64_type)
2850 // Determines "Better function" and returns an integer indicating :
2851 // 0 if candidate ain't better
2852 // 1 if candidate is better than the current best match
2854 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2856 ParameterData candidate_pd = GetParameterData (candidate);
2857 ParameterData best_pd;
2863 argument_count = args.Count;
2865 if (candidate_pd.Count == 0 && argument_count == 0)
2869 if (candidate_pd.Count == argument_count) {
2871 for (int j = argument_count; j > 0;) {
2874 Argument a = (Argument) args [j];
2876 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2893 best_pd = GetParameterData (best);
2895 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2896 int rating1 = 0, rating2 = 0;
2898 for (int j = argument_count; j > 0;) {
2902 Argument a = (Argument) args [j];
2904 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2905 best_pd.ParameterType (j));
2906 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2907 candidate_pd.ParameterType (j));
2913 if (rating1 > rating2)
2922 public static string FullMethodDesc (MethodBase mb)
2924 StringBuilder sb = new StringBuilder (mb.Name);
2925 ParameterData pd = GetParameterData (mb);
2928 for (int i = pd.Count; i > 0;) {
2930 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2936 return sb.ToString ();
2939 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2942 if (mg1 != null || mg2 != null) {
2944 MethodGroupExpr left_set = null, right_set = null;
2945 int length1 = 0, length2 = 0;
2948 left_set = (MethodGroupExpr) mg1;
2949 length1 = left_set.Methods.Length;
2953 right_set = (MethodGroupExpr) mg2;
2954 length2 = right_set.Methods.Length;
2957 MemberInfo [] miset = new MemberInfo [length1 + length2];
2958 if (left_set != null)
2959 left_set.Methods.CopyTo (miset, 0);
2960 if (right_set != null)
2961 right_set.Methods.CopyTo (miset, length1);
2963 MethodGroupExpr union = new MethodGroupExpr (miset);
2974 // Find the Applicable Function Members (7.4.2.1)
2976 // me: Method Group expression with the members to select.
2977 // it might contain constructors or methods (or anything
2978 // that maps to a method).
2980 // Arguments: ArrayList containing resolved Argument objects.
2982 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2983 // that is the best match of me on Arguments.
2986 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2987 ArrayList Arguments, Location loc)
2989 ArrayList afm = new ArrayList ();
2990 int best_match_idx = -1;
2991 MethodBase method = null;
2994 for (int i = me.Methods.Length; i > 0; ){
2996 MethodBase candidate = me.Methods [i];
2999 x = BetterFunction (tc, Arguments, candidate, method);
3005 method = me.Methods [best_match_idx];
3009 if (Arguments == null)
3012 argument_count = Arguments.Count;
3016 // Now we see if we can at least find a method with the same number of arguments
3017 // and then try doing implicit conversion on the arguments
3018 if (best_match_idx == -1) {
3020 for (int i = me.Methods.Length; i > 0;) {
3022 MethodBase mb = me.Methods [i];
3023 pd = GetParameterData (mb);
3025 if (pd.Count == argument_count) {
3027 method = me.Methods [best_match_idx];
3038 // And now convert implicitly, each argument to the required type
3040 pd = GetParameterData (method);
3042 for (int j = argument_count; j > 0;) {
3044 Argument a = (Argument) Arguments [j];
3045 Expression a_expr = a.Expr;
3047 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
3050 Error (tc, 1502, loc,
3051 "The best overloaded match for method '" + FullMethodDesc (method) +
3052 "' has some invalid arguments");
3053 Error (tc, 1503, loc,
3054 "Argument " + (j+1) +
3055 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
3056 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
3061 // Update the argument with the implicit conversion
3071 public override Expression Resolve (TypeContainer tc)
3074 // First, resolve the expression that is used to
3075 // trigger the invocation
3077 this.expr = expr.Resolve (tc);
3078 if (this.expr == null)
3081 if (!(this.expr is MethodGroupExpr)){
3082 report118 (tc, this.expr, "method group");
3087 // Next, evaluate all the expressions in the argument list
3089 if (Arguments != null){
3090 for (int i = Arguments.Count; i > 0;){
3092 Argument a = (Argument) Arguments [i];
3094 if (!a.Resolve (tc))
3099 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
3101 if (method == null){
3102 Error (tc, -6, Location,
3103 "Could not find any applicable function for this argument list");
3107 if (method is MethodInfo)
3108 type = ((MethodInfo)method).ReturnType;
3113 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3117 if (Arguments != null)
3118 top = Arguments.Count;
3122 for (int i = 0; i < top; i++){
3123 Argument a = (Argument) Arguments [i];
3129 public override void Emit (EmitContext ec)
3131 bool is_static = method.IsStatic;
3134 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3137 // If this is ourselves, push "this"
3139 if (mg.InstanceExpression == null){
3140 ec.ig.Emit (OpCodes.Ldarg_0);
3143 // Push the instance expression
3145 mg.InstanceExpression.Emit (ec);
3149 if (Arguments != null)
3150 EmitArguments (ec, method, Arguments);
3153 if (method is MethodInfo)
3154 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3156 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3158 if (method is MethodInfo)
3159 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3161 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3165 public override void EmitStatement (EmitContext ec)
3170 // Pop the return value if there is one
3172 if (method is MethodInfo){
3173 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3174 ec.ig.Emit (OpCodes.Pop);
3179 public class New : ExpressionStatement {
3186 public readonly NType NewType;
3187 public readonly ArrayList Arguments;
3188 public readonly string RequestedType;
3189 // These are for the case when we have an array
3190 public readonly string Rank;
3191 public readonly ArrayList Indices;
3192 public readonly ArrayList Initializers;
3195 MethodBase method = null;
3197 public New (string requested_type, ArrayList arguments, Location loc)
3199 RequestedType = requested_type;
3200 Arguments = arguments;
3201 NewType = NType.Object;
3205 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3207 RequestedType = requested_type;
3210 Initializers = initializers;
3211 NewType = NType.Array;
3215 public override Expression Resolve (TypeContainer tc)
3217 type = tc.LookupType (RequestedType, false);
3224 ml = MemberLookup (tc, type, ".ctor", false,
3225 MemberTypes.Constructor, AllBindingsFlags);
3227 if (! (ml is MethodGroupExpr)){
3229 // FIXME: Find proper error
3231 report118 (tc, ml, "method group");
3235 if (Arguments != null){
3236 for (int i = Arguments.Count; i > 0;){
3238 Argument a = (Argument) Arguments [i];
3240 if (!a.Resolve (tc))
3245 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3247 if (method == null) {
3248 Error (tc, -6, Location,
3249 "New invocation: Can not find a constructor for this argument list");
3256 public override void Emit (EmitContext ec)
3258 Invocation.EmitArguments (ec, method, Arguments);
3259 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3262 public override void EmitStatement (EmitContext ec)
3265 ec.ig.Emit (OpCodes.Pop);
3270 // Represents the `this' construct
3272 public class This : Expression, LValue {
3273 public override Expression Resolve (TypeContainer tc)
3275 eclass = ExprClass.Variable;
3276 type = tc.TypeBuilder;
3279 // FIXME: Verify that this is only used in instance contexts.
3284 public override void Emit (EmitContext ec)
3286 ec.ig.Emit (OpCodes.Ldarg_0);
3289 public void Store (EmitContext ec)
3292 // Assignment to the "this" variable.
3294 // FIXME: Apparently this is a bug that we
3295 // must catch as `this' seems to be readonly ;-)
3297 ec.ig.Emit (OpCodes.Starg, 0);
3300 public void AddressOf (EmitContext ec)
3302 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3306 public class TypeOf : Expression {
3307 public readonly string QueriedType;
3309 public TypeOf (string queried_type)
3311 QueriedType = queried_type;
3314 public override Expression Resolve (TypeContainer tc)
3316 type = tc.LookupType (QueriedType, false);
3321 eclass = ExprClass.Type;
3325 public override void Emit (EmitContext ec)
3327 throw new Exception ("Implement me");
3328 // FIXME: Implement.
3332 public class SizeOf : Expression {
3333 public readonly string QueriedType;
3335 public SizeOf (string queried_type)
3337 this.QueriedType = queried_type;
3340 public override Expression Resolve (TypeContainer tc)
3342 // FIXME: Implement;
3343 throw new Exception ("Unimplemented");
3347 public override void Emit (EmitContext ec)
3349 throw new Exception ("Implement me");
3353 public class MemberAccess : Expression {
3354 public readonly string Identifier;
3356 Expression member_lookup;
3358 public MemberAccess (Expression expr, string id)
3364 public Expression Expr {
3370 public override Expression Resolve (TypeContainer tc)
3372 Expression new_expression = expr.Resolve (tc);
3374 if (new_expression == null)
3377 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
3379 if (member_lookup is MethodGroupExpr){
3380 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3383 // Bind the instance expression to it
3385 // FIXME: This is a horrible way of detecting if it is
3386 // an instance expression. Figure out how to fix this.
3389 if (expr is LocalVariableReference ||
3390 expr is ParameterReference ||
3392 mg.InstanceExpression = expr;
3394 return member_lookup;
3395 } else if (member_lookup is FieldExpr){
3396 FieldExpr fe = (FieldExpr) member_lookup;
3400 return member_lookup;
3403 // FIXME: This should generate the proper node
3404 // ie, for a Property Access, it should like call it
3407 return member_lookup;
3410 public override void Emit (EmitContext ec)
3412 throw new Exception ("Should not happen I think");
3418 // Nodes of type Namespace are created during the semantic
3419 // analysis to resolve member_access/qualified_identifier/simple_name
3422 // They are born `resolved'.
3424 public class NamespaceExpr : Expression {
3425 public readonly string Name;
3427 public NamespaceExpr (string name)
3430 eclass = ExprClass.Namespace;
3433 public override Expression Resolve (TypeContainer tc)
3438 public override void Emit (EmitContext ec)
3440 throw new Exception ("Namespace expressions should never be emitted");
3445 // Fully resolved expression that evaluates to a type
3447 public class TypeExpr : Expression {
3448 public TypeExpr (Type t)
3451 eclass = ExprClass.Type;
3454 override public Expression Resolve (TypeContainer tc)
3459 override public void Emit (EmitContext ec)
3461 throw new Exception ("Implement me");
3466 // MethodGroup Expression.
3468 // This is a fully resolved expression that evaluates to a type
3470 public class MethodGroupExpr : Expression {
3471 public readonly MethodBase [] Methods;
3472 Expression instance_expression = null;
3474 public MethodGroupExpr (MemberInfo [] mi)
3476 Methods = new MethodBase [mi.Length];
3477 mi.CopyTo (Methods, 0);
3478 eclass = ExprClass.MethodGroup;
3482 // `A method group may have associated an instance expression'
3484 public Expression InstanceExpression {
3486 return instance_expression;
3490 instance_expression = value;
3494 override public Expression Resolve (TypeContainer tc)
3499 override public void Emit (EmitContext ec)
3501 throw new Exception ("This should never be reached");
3505 // Fully resolved expression that evaluates to a Field
3507 public class FieldExpr : Expression, LValue {
3508 public readonly FieldInfo FieldInfo;
3509 public Expression Instance;
3511 public FieldExpr (FieldInfo fi)
3514 eclass = ExprClass.Variable;
3515 type = fi.FieldType;
3518 override public Expression Resolve (TypeContainer tc)
3520 if (!FieldInfo.IsStatic){
3521 if (Instance == null){
3522 throw new Exception ("non-static FieldExpr without instance var\n" +
3523 "You have to assign the Instance variable\n" +
3524 "Of the FieldExpr to set this\n");
3527 Instance = Instance.Resolve (tc);
3528 if (Instance == null)
3535 override public void Emit (EmitContext ec)
3537 ILGenerator ig = ec.ig;
3539 if (FieldInfo.IsStatic)
3540 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3544 ig.Emit (OpCodes.Ldfld, FieldInfo);
3548 public void Store (EmitContext ec)
3550 if (FieldInfo.IsStatic)
3551 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3553 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3556 public void AddressOf (EmitContext ec)
3558 if (FieldInfo.IsStatic)
3559 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3562 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3568 // Fully resolved expression that evaluates to a Property
3570 public class PropertyExpr : Expression {
3571 public readonly PropertyInfo PropertyInfo;
3572 public readonly bool IsStatic;
3574 public PropertyExpr (PropertyInfo pi)
3577 eclass = ExprClass.PropertyAccess;
3580 MethodBase [] acc = pi.GetAccessors ();
3582 for (int i = 0; i < acc.Length; i++)
3583 if (acc [i].IsStatic)
3586 type = pi.PropertyType;
3589 override public Expression Resolve (TypeContainer tc)
3591 // We are born in resolved state.
3595 override public void Emit (EmitContext ec)
3597 // FIXME: Implement;
3598 throw new Exception ("Unimplemented");
3603 // Fully resolved expression that evaluates to a Expression
3605 public class EventExpr : Expression {
3606 public readonly EventInfo EventInfo;
3608 public EventExpr (EventInfo ei)
3611 eclass = ExprClass.EventAccess;
3614 override public Expression Resolve (TypeContainer tc)
3616 // We are born in resolved state.
3620 override public void Emit (EmitContext ec)
3622 throw new Exception ("Implement me");
3623 // FIXME: Implement.
3627 public class CheckedExpr : Expression {
3629 public Expression Expr;
3631 public CheckedExpr (Expression e)
3636 public override Expression Resolve (TypeContainer tc)
3638 Expr = Expr.Resolve (tc);
3643 eclass = Expr.ExprClass;
3648 public override void Emit (EmitContext ec)
3650 bool last_check = ec.CheckState;
3652 ec.CheckState = true;
3654 ec.CheckState = last_check;
3659 public class UnCheckedExpr : Expression {
3661 public Expression Expr;
3663 public UnCheckedExpr (Expression e)
3668 public override Expression Resolve (TypeContainer tc)
3670 Expr = Expr.Resolve (tc);
3675 eclass = Expr.ExprClass;
3680 public override void Emit (EmitContext ec)
3682 bool last_check = ec.CheckState;
3684 ec.CheckState = false;
3686 ec.CheckState = last_check;
3691 public class ElementAccess : Expression {
3693 public readonly ArrayList Arguments;
3694 public readonly Expression Expr;
3696 public ElementAccess (Expression e, ArrayList e_list)
3702 public override Expression Resolve (TypeContainer tc)
3704 // FIXME: Implement;
3705 throw new Exception ("Unimplemented");
3709 public override void Emit (EmitContext ec)
3711 // FIXME : Implement !
3712 throw new Exception ("Unimplemented");
3717 public class BaseAccess : Expression {
3719 public enum BaseAccessType {
3724 public readonly BaseAccessType BAType;
3725 public readonly string Member;
3726 public readonly ArrayList Arguments;
3728 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3736 public override Expression Resolve (TypeContainer tc)
3738 // FIXME: Implement;
3739 throw new Exception ("Unimplemented");
3743 public override void Emit (EmitContext ec)
3745 throw new Exception ("Unimplemented");
3749 public class UserImplicitCast : Expression {
3751 ArrayList arguments;
3753 public UserImplicitCast (MethodInfo method, ArrayList arguments)
3755 this.method = method;
3756 this.arguments = arguments;
3757 type = method.ReturnType;
3758 eclass = ExprClass.Value;
3761 public override Expression Resolve (TypeContainer tc)
3764 // We are born in a fully resolved state
3769 public static Expression CanConvert (TypeContainer tc, Expression source, Type target)
3771 Expression mg1, mg2;
3773 ArrayList arguments;
3775 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3776 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3778 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3780 if (union != null) {
3781 arguments = new ArrayList ();
3782 arguments.Add (new Argument (source, Argument.AType.Expression));
3784 method = Invocation.OverloadResolve (tc, union, arguments,
3785 new Location ("FIXME", 1, 1));
3787 if (method != null) {
3788 MethodInfo mi = (MethodInfo) method;
3790 if (mi.ReturnType == target)
3791 return new UserImplicitCast (mi, arguments);
3795 // If we have a boolean type, we need to check for the True
3796 // and False operators too.
3798 if (target == TypeManager.bool_type) {
3800 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3801 mg2 = MemberLookup (tc, target, "op_True", false);
3803 union = Invocation.MakeUnionSet (mg1, mg2);
3808 arguments = new ArrayList ();
3809 arguments.Add (new Argument (source, Argument.AType.Expression));
3811 method = Invocation.OverloadResolve (tc, union, arguments,
3812 new Location ("FIXME", 1, 1));
3813 if (method != null) {
3814 MethodInfo mi = (MethodInfo) method;
3816 if (mi.ReturnType == target)
3817 return new UserImplicitCast (mi, arguments);
3824 public override void Emit (EmitContext ec)
3826 ILGenerator ig = ec.ig;
3828 if (method != null) {
3830 // Note that operators are static anyway
3832 if (arguments != null)
3833 Invocation.EmitArguments (ec, method, arguments);
3835 if (method is MethodInfo)
3836 ig.Emit (OpCodes.Call, (MethodInfo) method);
3838 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3843 throw new Exception ("Implement me");