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;
345 // Converts implicitly the resolved expression `expr' into the
346 // `target_type'. It returns a new expression that can be used
347 // in a context that expects a `target_type'.
349 static public Expression ConvertImplicit (TypeContainer tc, Expression expr, Type target_type)
351 Type expr_type = expr.Type;
353 Console.WriteLine ("ConvertImplicit " + expr_type + " => " + target_type);
355 throw new Exception ("Lame Loop Detector Triggered");
357 if (expr_type == target_type)
361 // Step 1: Built-in conversions.
363 if (expr_type == TypeManager.sbyte_type){
365 // From sbyte to short, int, long, float, double.
367 if (target_type == TypeManager.int32_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
369 if (target_type == TypeManager.int64_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
371 if (target_type == TypeManager.double_type)
372 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
373 if (target_type == TypeManager.float_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
375 if (target_type == TypeManager.short_type)
376 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
377 if (target_type == TypeManager.decimal_type)
378 return InternalTypeConstructor (tc, expr, target_type);
379 } else if (expr_type == TypeManager.byte_type){
381 // From byte to short, ushort, int, uint, long, ulong, float, double
383 if ((target_type == TypeManager.short_type) ||
384 (target_type == TypeManager.ushort_type) ||
385 (target_type == TypeManager.int32_type) ||
386 (target_type == TypeManager.uint32_type))
387 return new EmptyCast (expr, target_type);
389 if (target_type == TypeManager.uint64_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
391 if (target_type == TypeManager.int64_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
394 if (target_type == TypeManager.float_type)
395 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
396 if (target_type == TypeManager.double_type)
397 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
398 if (target_type == TypeManager.decimal_type)
399 return InternalTypeConstructor (tc, expr, target_type);
400 } else if (expr_type == TypeManager.short_type){
402 // From short to int, long, float, double
404 if (target_type == TypeManager.int32_type)
405 return new EmptyCast (expr, target_type);
406 if (target_type == TypeManager.int64_type)
407 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
408 if (target_type == TypeManager.double_type)
409 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
410 if (target_type == TypeManager.float_type)
411 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
412 if (target_type == TypeManager.decimal_type)
413 return InternalTypeConstructor (tc, expr, target_type);
414 } else if (expr_type == TypeManager.ushort_type){
416 // From ushort to int, uint, long, ulong, float, double
418 if ((target_type == TypeManager.uint32_type) ||
419 (target_type == TypeManager.uint64_type))
420 return new EmptyCast (expr, target_type);
422 if (target_type == TypeManager.int32_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
424 if (target_type == TypeManager.int64_type)
425 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
426 if (target_type == TypeManager.double_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
428 if (target_type == TypeManager.float_type)
429 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
430 if (target_type == TypeManager.decimal_type)
431 return InternalTypeConstructor (tc, expr, target_type);
432 } else if (expr_type == TypeManager.int32_type){
434 // From int to long, float, double
436 if (target_type == TypeManager.int64_type)
437 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
438 if (target_type == TypeManager.double_type)
439 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
440 if (target_type == TypeManager.float_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
442 if (target_type == TypeManager.decimal_type)
443 return InternalTypeConstructor (tc, expr, target_type);
444 } else if (expr_type == TypeManager.uint32_type){
446 // From uint to long, ulong, float, double
448 if (target_type == TypeManager.int64_type)
449 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
450 if (target_type == TypeManager.uint64_type)
451 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
452 if (target_type == TypeManager.double_type)
453 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
455 if (target_type == TypeManager.float_type)
456 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
458 if (target_type == TypeManager.decimal_type)
459 return InternalTypeConstructor (tc, expr, target_type);
460 } else if ((expr_type == TypeManager.uint64_type) ||
461 (expr_type == TypeManager.int64_type)){
463 // From long to float, double
465 if (target_type == TypeManager.double_type)
466 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
468 if (target_type == TypeManager.float_type)
469 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
471 if (target_type == TypeManager.decimal_type)
472 return InternalTypeConstructor (tc, expr, target_type);
473 } else if (expr_type == TypeManager.char_type){
475 // From char to ushort, int, uint, long, ulong, float, double
477 if ((target_type == TypeManager.ushort_type) ||
478 (target_type == TypeManager.int32_type) ||
479 (target_type == TypeManager.uint32_type))
480 return new EmptyCast (expr, target_type);
481 if (target_type == TypeManager.uint64_type)
482 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
483 if (target_type == TypeManager.int64_type)
484 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
485 if (target_type == TypeManager.float_type)
486 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
487 if (target_type == TypeManager.double_type)
488 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
489 if (target_type == TypeManager.decimal_type)
490 return InternalTypeConstructor (tc, expr, target_type);
495 e = ImplicitReferenceConversion (expr, target_type);
501 e = UserImplicitCast.CanConvert (tc, expr, target_type);
507 // Could not find an implicit cast.
513 // Attemps to perform an implict constant conversion of the IntLiteral
514 // into a different data type using casts (See Implicit Constant
515 // Expression Conversions)
517 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
519 int value = il.Value;
521 if (target_type == TypeManager.sbyte_type){
522 if (value >= SByte.MinValue && value <= SByte.MaxValue)
524 } else if (target_type == TypeManager.byte_type){
525 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
527 } else if (target_type == TypeManager.short_type){
528 if (value >= Int16.MinValue && value <= Int16.MaxValue)
530 } else if (target_type == TypeManager.ushort_type){
531 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
533 } else if (target_type == TypeManager.uint32_type){
535 // we can optimize this case: a positive int32
536 // always fits on a uint32
540 } else if (target_type == TypeManager.uint64_type){
542 // we can optimize this case: a positive int32
543 // always fits on a uint64. But we need an opcode
547 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
554 // Attemptes to implicityly convert `target' into `type', using
555 // ConvertImplicit. If there is no implicit conversion, then
556 // an error is signaled
558 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
559 Type type, Location l)
563 e = ConvertImplicit (tc, target, type);
568 // Attempt to do the implicit constant expression conversions
570 if (target is IntLiteral){
571 e = TryImplicitIntConversion (type, (IntLiteral) target);
574 } else if (target is LongLiteral){
576 // Try the implicit constant expression conversion
577 // from long to ulong, instead of a nice routine,
580 if (((LongLiteral) target).Value > 0)
584 string msg = "Can not convert implicitly from `"+
585 TypeManager.CSharpName (target.Type) + "' to `" +
586 TypeManager.CSharpName (type) + "'";
588 Error (tc, 29, l, msg);
594 // Performs the explicit numeric conversions
596 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
599 Type expr_type = expr.Type;
601 if (expr_type == TypeManager.sbyte_type){
603 // From sbyte to byte, ushort, uint, ulong, char
605 if (target_type == TypeManager.byte_type)
606 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
607 if (target_type == TypeManager.ushort_type)
608 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
609 if (target_type == TypeManager.uint32_type)
610 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
611 if (target_type == TypeManager.uint64_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
613 if (target_type == TypeManager.char_type)
614 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
615 } else if (expr_type == TypeManager.byte_type){
617 // From byte to sbyte and char
619 if (target_type == TypeManager.sbyte_type)
620 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
621 if (target_type == TypeManager.char_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
623 } else if (expr_type == TypeManager.short_type){
625 // From short to sbyte, byte, ushort, uint, ulong, char
627 if (target_type == TypeManager.sbyte_type)
628 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
629 if (target_type == TypeManager.byte_type)
630 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
631 if (target_type == TypeManager.ushort_type)
632 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
633 if (target_type == TypeManager.uint32_type)
634 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
635 if (target_type == TypeManager.uint64_type)
636 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
637 if (target_type == TypeManager.char_type)
638 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
639 } else if (expr_type == TypeManager.ushort_type){
641 // From ushort to sbyte, byte, short, char
643 if (target_type == TypeManager.sbyte_type)
644 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
645 if (target_type == TypeManager.byte_type)
646 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
647 if (target_type == TypeManager.short_type)
648 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
649 if (target_type == TypeManager.char_type)
650 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
651 } else if (expr_type == TypeManager.int32_type){
653 // From int to sbyte, byte, short, ushort, uint, ulong, char
655 if (target_type == TypeManager.sbyte_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
657 if (target_type == TypeManager.byte_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
659 if (target_type == TypeManager.short_type)
660 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
661 if (target_type == TypeManager.ushort_type)
662 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
663 if (target_type == TypeManager.uint32_type)
664 return new EmptyCast (expr, target_type);
665 if (target_type == TypeManager.uint64_type)
666 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
667 if (target_type == TypeManager.char_type)
668 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
669 } else if (expr_type == TypeManager.uint32_type){
671 // From uint to sbyte, byte, short, ushort, int, char
673 if (target_type == TypeManager.sbyte_type)
674 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
675 if (target_type == TypeManager.byte_type)
676 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
677 if (target_type == TypeManager.short_type)
678 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
679 if (target_type == TypeManager.ushort_type)
680 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
681 if (target_type == TypeManager.int32_type)
682 return new EmptyCast (expr, target_type);
683 if (target_type == TypeManager.char_type)
684 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
685 } else if (expr_type == TypeManager.int64_type){
687 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
689 if (target_type == TypeManager.sbyte_type)
690 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
691 if (target_type == TypeManager.byte_type)
692 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
693 if (target_type == TypeManager.short_type)
694 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
695 if (target_type == TypeManager.ushort_type)
696 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
697 if (target_type == TypeManager.int32_type)
698 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
699 if (target_type == TypeManager.uint32_type)
700 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
701 if (target_type == TypeManager.uint64_type)
702 return new EmptyCast (expr, target_type);
703 if (target_type == TypeManager.char_type)
704 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
705 } else if (expr_type == TypeManager.uint64_type){
707 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
709 if (target_type == TypeManager.sbyte_type)
710 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
711 if (target_type == TypeManager.byte_type)
712 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
713 if (target_type == TypeManager.short_type)
714 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
715 if (target_type == TypeManager.ushort_type)
716 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
717 if (target_type == TypeManager.int32_type)
718 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
719 if (target_type == TypeManager.uint32_type)
720 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
721 if (target_type == TypeManager.int64_type)
722 return new EmptyCast (expr, target_type);
723 if (target_type == TypeManager.char_type)
724 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
725 } else if (expr_type == TypeManager.char_type){
727 // From char to sbyte, byte, short
729 if (target_type == TypeManager.sbyte_type)
730 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
731 if (target_type == TypeManager.byte_type)
732 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
733 if (target_type == TypeManager.short_type)
734 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
735 } else if (expr_type == TypeManager.float_type){
737 // From float to sbyte, byte, short,
738 // ushort, int, uint, long, ulong, char
741 if (target_type == TypeManager.sbyte_type)
742 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
743 if (target_type == TypeManager.byte_type)
744 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
745 if (target_type == TypeManager.short_type)
746 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
747 if (target_type == TypeManager.ushort_type)
748 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
749 if (target_type == TypeManager.int32_type)
750 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
751 if (target_type == TypeManager.uint32_type)
752 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
753 if (target_type == TypeManager.int64_type)
754 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
755 if (target_type == TypeManager.uint64_type)
756 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
757 if (target_type == TypeManager.char_type)
758 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
759 if (target_type == TypeManager.decimal_type)
760 return InternalTypeConstructor (tc, expr, target_type);
761 } else if (expr_type == TypeManager.double_type){
763 // From double to byte, byte, short,
764 // ushort, int, uint, long, ulong,
765 // char, float or decimal
767 if (target_type == TypeManager.sbyte_type)
768 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
769 if (target_type == TypeManager.byte_type)
770 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
771 if (target_type == TypeManager.short_type)
772 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
773 if (target_type == TypeManager.ushort_type)
774 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
775 if (target_type == TypeManager.int32_type)
776 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
777 if (target_type == TypeManager.uint32_type)
778 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
779 if (target_type == TypeManager.int64_type)
780 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
781 if (target_type == TypeManager.uint64_type)
782 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
783 if (target_type == TypeManager.char_type)
784 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
785 if (target_type == TypeManager.float_type)
786 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
787 if (target_type == TypeManager.decimal_type)
788 return InternalTypeConstructor (tc, expr, target_type);
791 // decimal is taken care of by the op_Explicit methods.
797 // Performs an explicit conversion of the expression `expr' whose
798 // type is expr.Type to `target_type'.
800 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
803 Expression ne = ConvertImplicit (tc, expr, target_type);
808 ne = ConvertNumericExplicit (tc, expr, target_type);
816 static string ExprClassName (ExprClass c)
819 case ExprClass.Invalid:
821 case ExprClass.Value:
823 case ExprClass.Variable:
825 case ExprClass.Namespace:
829 case ExprClass.MethodGroup:
830 return "method group";
831 case ExprClass.PropertyAccess:
832 return "property access";
833 case ExprClass.EventAccess:
834 return "event access";
835 case ExprClass.IndexerAccess:
836 return "indexer access";
837 case ExprClass.Nothing:
840 throw new Exception ("Should not happen");
844 // Reports that we were expecting `expr' to be of class `expected'
846 protected void report118 (TypeContainer tc, Expression expr, string expected)
848 Error (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
849 "' where an " + expected + " was expected");
854 // This is just a base class for expressions that can
855 // appear on statements (invocations, object creation,
856 // assignments, post/pre increment and decrement). The idea
857 // being that they would support an extra Emition interface that
858 // does not leave a result on the stack.
861 public abstract class ExpressionStatement : Expression {
864 // Requests the expression to be emitted in a `statement'
865 // context. This means that no new value is left on the
866 // stack after invoking this method (constrasted with
867 // Emit that will always leave a value on the stack).
869 public abstract void EmitStatement (EmitContext ec);
873 // This kind of cast is used to encapsulate the child
874 // whose type is child.Type into an expression that is
875 // reported to return "return_type". This is used to encapsulate
876 // expressions which have compatible types, but need to be dealt
877 // at higher levels with.
879 // For example, a "byte" expression could be encapsulated in one
880 // of these as an "unsigned int". The type for the expression
881 // would be "unsigned int".
885 public class EmptyCast : Expression {
886 protected Expression child;
888 public EmptyCast (Expression child, Type return_type)
890 ExprClass = child.ExprClass;
895 public override Expression Resolve (TypeContainer tc)
897 // This should never be invoked, we are born in fully
898 // initialized state.
903 public override void Emit (EmitContext ec)
910 // This kind of cast is used to encapsulate Value Types in objects.
912 // The effect of it is to box the value type emitted by the previous
915 public class BoxedCast : EmptyCast {
917 public BoxedCast (Expression expr)
918 : base (expr, TypeManager.object_type)
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)
933 ec.ig.Emit (OpCodes.Box, child.Type);
938 // This kind of cast is used to encapsulate a child expression
939 // that can be trivially converted to a target type using one or
940 // two opcodes. The opcodes are passed as arguments.
942 public class OpcodeCast : EmptyCast {
946 public OpcodeCast (Expression child, Type return_type, OpCode op)
947 : base (child, return_type)
951 second_valid = false;
954 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
955 : base (child, return_type)
963 public override Expression Resolve (TypeContainer tc)
965 // This should never be invoked, we are born in fully
966 // initialized state.
971 public override void Emit (EmitContext ec)
983 // Unary expressions.
987 // Unary implements unary expressions. It derives from
988 // ExpressionStatement becuase the pre/post increment/decrement
989 // operators can be used in a statement context.
991 public class Unary : ExpressionStatement {
992 public enum Operator {
993 Addition, Subtraction, Negate, BitComplement,
994 Indirection, AddressOf, PreIncrement,
995 PreDecrement, PostIncrement, PostDecrement
1000 ArrayList Arguments;
1004 public Unary (Operator op, Expression expr, Location loc)
1008 this.location = loc;
1011 public Expression Expr {
1021 public Operator Oper {
1032 // Returns a stringified representation of the Operator
1037 case Operator.Addition:
1039 case Operator.Subtraction:
1041 case Operator.Negate:
1043 case Operator.BitComplement:
1045 case Operator.AddressOf:
1047 case Operator.Indirection:
1049 case Operator.PreIncrement : case Operator.PostIncrement :
1051 case Operator.PreDecrement : case Operator.PostDecrement :
1055 return oper.ToString ();
1058 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1060 if (expr.Type == target_type)
1063 return ConvertImplicit (tc, expr, target_type);
1066 void report23 (Report r, Type t)
1068 r.Error (23, "Operator " + OperName () + " cannot be applied to operand of type `" +
1069 TypeManager.CSharpName (t) + "'");
1073 // Returns whether an object of type `t' can be incremented
1074 // or decremented with add/sub (ie, basically whether we can
1075 // use pre-post incr-decr operations on it, but it is not a
1076 // System.Decimal, which we test elsewhere)
1078 static bool IsIncrementableNumber (Type t)
1080 return (t == TypeManager.sbyte_type) ||
1081 (t == TypeManager.byte_type) ||
1082 (t == TypeManager.short_type) ||
1083 (t == TypeManager.ushort_type) ||
1084 (t == TypeManager.int32_type) ||
1085 (t == TypeManager.uint32_type) ||
1086 (t == TypeManager.int64_type) ||
1087 (t == TypeManager.uint64_type) ||
1088 (t == TypeManager.char_type) ||
1089 (t.IsSubclassOf (TypeManager.enum_type)) ||
1090 (t == TypeManager.float_type) ||
1091 (t == TypeManager.double_type);
1094 Expression ResolveOperator (TypeContainer tc)
1096 Type expr_type = expr.Type;
1099 // Step 1: Perform Operator Overload location
1104 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1105 op_name = "op_Increment";
1106 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1107 op_name = "op_Decrement";
1109 op_name = "op_" + oper;
1111 mg = MemberLookup (tc, expr_type, op_name, false);
1114 Arguments = new ArrayList ();
1115 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1117 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg, Arguments, location);
1118 if (method != null) {
1119 MethodInfo mi = (MethodInfo) method;
1121 type = mi.ReturnType;
1127 // Step 2: Default operations on CLI native types.
1130 // Only perform numeric promotions on:
1133 if (expr_type == null)
1136 if (oper == Operator.Negate){
1137 if (expr_type != TypeManager.bool_type) {
1138 report23 (tc.RootContext.Report, expr.Type);
1142 type = TypeManager.bool_type;
1146 if (oper == Operator.BitComplement) {
1147 if (!((expr_type == TypeManager.int32_type) ||
1148 (expr_type == TypeManager.uint32_type) ||
1149 (expr_type == TypeManager.int64_type) ||
1150 (expr_type == TypeManager.uint64_type) ||
1151 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1152 report23 (tc.RootContext.Report, expr.Type);
1159 if (oper == Operator.Addition) {
1161 // A plus in front of something is just a no-op, so return the child.
1167 // Deals with -literals
1168 // int operator- (int x)
1169 // long operator- (long x)
1170 // float operator- (float f)
1171 // double operator- (double d)
1172 // decimal operator- (decimal d)
1174 if (oper == Operator.Subtraction){
1176 // Fold a "- Constant" into a negative constant
1179 Expression e = null;
1182 // Is this a constant?
1184 if (expr is IntLiteral)
1185 e = new IntLiteral (-((IntLiteral) expr).Value);
1186 else if (expr is LongLiteral)
1187 e = new LongLiteral (-((LongLiteral) expr).Value);
1188 else if (expr is FloatLiteral)
1189 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1190 else if (expr is DoubleLiteral)
1191 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1192 else if (expr is DecimalLiteral)
1193 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1201 // Not a constant we can optimize, perform numeric
1202 // promotions to int, long, double.
1205 // The following is inneficient, because we call
1206 // ConvertImplicit too many times.
1208 // It is also not clear if we should convert to Float
1209 // or Double initially.
1211 if (expr_type == TypeManager.uint32_type){
1213 // FIXME: handle exception to this rule that
1214 // permits the int value -2147483648 (-2^31) to
1215 // bt written as a decimal interger literal
1217 type = TypeManager.int64_type;
1218 expr = ConvertImplicit (tc, expr, type);
1222 if (expr_type == TypeManager.uint64_type){
1224 // FIXME: Handle exception of `long value'
1225 // -92233720368547758087 (-2^63) to be written as
1226 // decimal integer literal.
1228 report23 (tc.RootContext.Report, expr_type);
1232 e = ConvertImplicit (tc, expr, TypeManager.int32_type);
1239 e = ConvertImplicit (tc, expr, TypeManager.int64_type);
1246 e = ConvertImplicit (tc, expr, TypeManager.double_type);
1253 report23 (tc.RootContext.Report, expr_type);
1258 // The operand of the prefix/postfix increment decrement operators
1259 // should be an expression that is classified as a variable,
1260 // a property access or an indexer access
1262 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1263 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1264 if (expr.ExprClass == ExprClass.Variable){
1265 if (IsIncrementableNumber (expr_type) ||
1266 expr_type == TypeManager.decimal_type){
1270 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1272 // FIXME: Verify that we have both get and set methods
1274 throw new Exception ("Implement me");
1275 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1277 // FIXME: Verify that we have both get and set methods
1279 throw new Exception ("Implement me");
1281 report118 (tc, expr, "variable, indexer or property access");
1285 if (oper == Operator.AddressOf){
1286 if (expr.ExprClass != ExprClass.Variable){
1287 Error (tc, 211, "Cannot take the address of non-variables");
1290 type = Type.GetType (expr.Type.ToString () + "*");
1293 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1294 TypeManager.CSharpName (expr_type) + "'");
1299 public override Expression Resolve (TypeContainer tc)
1301 expr = expr.Resolve (tc);
1306 return ResolveOperator (tc);
1309 public override void Emit (EmitContext ec)
1311 ILGenerator ig = ec.ig;
1312 Type expr_type = expr.Type;
1314 if (method != null) {
1316 // Note that operators are static anyway
1318 if (Arguments != null)
1319 Invocation.EmitArguments (ec, method, Arguments);
1322 // Post increment/decrement operations need a copy at this
1325 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1326 ig.Emit (OpCodes.Dup);
1329 ig.Emit (OpCodes.Call, (MethodInfo) method);
1332 // Pre Increment and Decrement operators
1334 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1335 ig.Emit (OpCodes.Dup);
1339 // Increment and Decrement should store the result
1341 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1342 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1343 ((LValue) expr).Store (ec);
1349 case Operator.Addition:
1350 throw new Exception ("This should be caught by Resolve");
1352 case Operator.Subtraction:
1354 ig.Emit (OpCodes.Neg);
1357 case Operator.Negate:
1359 ig.Emit (OpCodes.Ldc_I4_0);
1360 ig.Emit (OpCodes.Ceq);
1363 case Operator.BitComplement:
1365 ig.Emit (OpCodes.Not);
1368 case Operator.AddressOf:
1369 ((LValue)expr).AddressOf (ec);
1372 case Operator.Indirection:
1373 throw new Exception ("Not implemented yet");
1375 case Operator.PreIncrement:
1376 case Operator.PreDecrement:
1377 if (expr.ExprClass == ExprClass.Variable){
1379 // Resolve already verified that it is an "incrementable"
1382 ig.Emit (OpCodes.Ldc_I4_1);
1384 if (oper == Operator.PreDecrement)
1385 ig.Emit (OpCodes.Sub);
1387 ig.Emit (OpCodes.Add);
1388 ig.Emit (OpCodes.Dup);
1389 ((LValue) expr).Store (ec);
1391 throw new Exception ("Handle Indexers and Properties here");
1395 case Operator.PostIncrement:
1396 case Operator.PostDecrement:
1397 if (expr.ExprClass == ExprClass.Variable){
1399 // Resolve already verified that it is an "incrementable"
1402 ig.Emit (OpCodes.Dup);
1403 ig.Emit (OpCodes.Ldc_I4_1);
1405 if (oper == Operator.PostDecrement)
1406 ig.Emit (OpCodes.Sub);
1408 ig.Emit (OpCodes.Add);
1409 ((LValue) expr).Store (ec);
1411 throw new Exception ("Handle Indexers and Properties here");
1416 throw new Exception ("This should not happen: Operator = "
1417 + oper.ToString ());
1422 public override void EmitStatement (EmitContext ec)
1425 // FIXME: we should rewrite this code to generate
1426 // better code for ++ and -- as we know we wont need
1427 // the values on the stack
1430 ec.ig.Emit (OpCodes.Pop);
1434 public class Probe : Expression {
1435 public readonly string ProbeType;
1436 public readonly Operator Oper;
1440 public enum Operator {
1444 public Probe (Operator oper, Expression expr, string probe_type)
1447 ProbeType = probe_type;
1451 public Expression Expr {
1457 public override Expression Resolve (TypeContainer tc)
1459 probe_type = tc.LookupType (ProbeType, false);
1461 if (probe_type == null)
1464 expr = expr.Resolve (tc);
1466 type = TypeManager.bool_type;
1467 eclass = ExprClass.Value;
1472 public override void Emit (EmitContext ec)
1476 if (Oper == Operator.Is){
1477 ec.ig.Emit (OpCodes.Isinst, probe_type);
1479 throw new Exception ("Implement as");
1485 // This represents a typecast in the source language.
1487 // FIXME: Cast expressions have an unusual set of parsing
1488 // rules, we need to figure those out.
1490 public class Cast : Expression {
1494 public Cast (string cast_type, Expression expr)
1496 this.target_type = cast_type;
1500 public string TargetType {
1506 public Expression Expr {
1515 public override Expression Resolve (TypeContainer tc)
1517 expr = expr.Resolve (tc);
1521 type = tc.LookupType (target_type, false);
1522 eclass = ExprClass.Value;
1527 expr = ConvertExplicit (tc, expr, type);
1532 public override void Emit (EmitContext ec)
1535 // This one will never happen
1537 throw new Exception ("Should not happen");
1541 public class Binary : Expression {
1542 public enum Operator {
1543 Multiply, Division, Modulus,
1544 Addition, Subtraction,
1545 LeftShift, RightShift,
1546 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1547 Equality, Inequality,
1556 Expression left, right;
1558 ArrayList Arguments;
1562 public Binary (Operator oper, Expression left, Expression right, Location loc)
1567 this.location = loc;
1570 public Operator Oper {
1579 public Expression Left {
1588 public Expression Right {
1599 // Returns a stringified representation of the Operator
1604 case Operator.Multiply:
1606 case Operator.Division:
1608 case Operator.Modulus:
1610 case Operator.Addition:
1612 case Operator.Subtraction:
1614 case Operator.LeftShift:
1616 case Operator.RightShift:
1618 case Operator.LessThan:
1620 case Operator.GreaterThan:
1622 case Operator.LessThanOrEqual:
1624 case Operator.GreaterThanOrEqual:
1626 case Operator.Equality:
1628 case Operator.Inequality:
1630 case Operator.BitwiseAnd:
1632 case Operator.BitwiseOr:
1634 case Operator.ExclusiveOr:
1636 case Operator.LogicalOr:
1638 case Operator.LogicalAnd:
1642 return oper.ToString ();
1645 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1647 if (expr.Type == target_type)
1650 return ConvertImplicit (tc, expr, target_type);
1654 // Note that handling the case l == Decimal || r == Decimal
1655 // is taken care of by the Step 1 Operator Overload resolution.
1657 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1659 if (l == TypeManager.double_type || r == TypeManager.double_type){
1661 // If either operand is of type double, the other operand is
1662 // conveted to type double.
1664 if (r != TypeManager.double_type)
1665 right = ConvertImplicit (tc, right, TypeManager.double_type);
1666 if (l != TypeManager.double_type)
1667 left = ConvertImplicit (tc, left, TypeManager.double_type);
1669 type = TypeManager.double_type;
1670 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1672 // if either operand is of type float, th eother operand is
1673 // converd to type float.
1675 if (r != TypeManager.double_type)
1676 right = ConvertImplicit (tc, right, TypeManager.float_type);
1677 if (l != TypeManager.double_type)
1678 left = ConvertImplicit (tc, left, TypeManager.float_type);
1679 type = TypeManager.float_type;
1680 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1682 // If either operand is of type ulong, the other operand is
1683 // converted to type ulong. or an error ocurrs if the other
1684 // operand is of type sbyte, short, int or long
1688 if (l == TypeManager.uint64_type)
1690 else if (r == TypeManager.uint64_type)
1693 if ((other == TypeManager.sbyte_type) ||
1694 (other == TypeManager.short_type) ||
1695 (other == TypeManager.int32_type) ||
1696 (other == TypeManager.int64_type)){
1697 string oper = OperName ();
1699 Error (tc, 34, "Operator `" + OperName ()
1700 + "' is ambiguous on operands of type `"
1701 + TypeManager.CSharpName (l) + "' "
1702 + "and `" + TypeManager.CSharpName (r)
1705 type = TypeManager.uint64_type;
1706 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1708 // If either operand is of type long, the other operand is converted
1711 if (l != TypeManager.int64_type)
1712 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1713 if (r != TypeManager.int64_type)
1714 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1716 type = TypeManager.int64_type;
1717 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1719 // If either operand is of type uint, and the other
1720 // operand is of type sbyte, short or int, othe operands are
1721 // converted to type long.
1725 if (l == TypeManager.uint32_type)
1727 else if (r == TypeManager.uint32_type)
1730 if ((other == TypeManager.sbyte_type) ||
1731 (other == TypeManager.short_type) ||
1732 (other == TypeManager.int32_type)){
1733 left = ForceConversion (tc, left, TypeManager.int64_type);
1734 right = ForceConversion (tc, right, TypeManager.int64_type);
1735 type = TypeManager.int64_type;
1738 // if either operand is of type uint, the other
1739 // operand is converd to type uint
1741 left = ForceConversion (tc, left, TypeManager.uint32_type);
1742 right = ForceConversion (tc, left, TypeManager.uint32_type);
1743 type = TypeManager.uint32_type;
1745 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1746 if (l != TypeManager.decimal_type)
1747 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1748 if (r != TypeManager.decimal_type)
1749 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1751 type = TypeManager.decimal_type;
1753 left = ForceConversion (tc, left, TypeManager.int32_type);
1754 right = ForceConversion (tc, right, TypeManager.int32_type);
1755 type = TypeManager.int32_type;
1759 void error19 (TypeContainer tc)
1762 "Operator " + OperName () + " cannot be applied to operands of type `" +
1763 TypeManager.CSharpName (left.Type) + "' and `" +
1764 TypeManager.CSharpName (right.Type) + "'");
1768 Expression CheckShiftArguments (TypeContainer tc)
1772 Type r = right.Type;
1774 e = ForceConversion (tc, right, TypeManager.int32_type);
1781 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1782 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1783 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1784 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1793 Expression ResolveOperator (TypeContainer tc)
1796 Type r = right.Type;
1799 // Step 1: Perform Operator Overload location
1801 Expression left_expr, right_expr;
1803 string op = "op_" + oper;
1805 left_expr = MemberLookup (tc, l, op, false);
1807 right_expr = MemberLookup (tc, r, op, false);
1809 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1811 Arguments = new ArrayList ();
1812 Arguments.Add (new Argument (left, Argument.AType.Expression));
1813 Arguments.Add (new Argument (right, Argument.AType.Expression));
1815 if (union != null) {
1816 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1817 if (method != null) {
1818 MethodInfo mi = (MethodInfo) method;
1820 type = mi.ReturnType;
1826 // Step 2: Default operations on CLI native types.
1829 // Only perform numeric promotions on:
1830 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1832 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1833 return CheckShiftArguments (tc);
1834 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1836 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1839 DoNumericPromotions (tc, l, r);
1841 if (left == null || right == null)
1844 if (oper == Operator.BitwiseAnd ||
1845 oper == Operator.BitwiseOr ||
1846 oper == Operator.ExclusiveOr){
1847 if (!((l == TypeManager.int32_type) ||
1848 (l == TypeManager.uint32_type) ||
1849 (l == TypeManager.int64_type) ||
1850 (l == TypeManager.uint64_type))){
1856 if (oper == Operator.Equality ||
1857 oper == Operator.Inequality ||
1858 oper == Operator.LessThanOrEqual ||
1859 oper == Operator.LessThan ||
1860 oper == Operator.GreaterThanOrEqual ||
1861 oper == Operator.GreaterThan){
1862 type = TypeManager.bool_type;
1868 public override Expression Resolve (TypeContainer tc)
1870 left = left.Resolve (tc);
1871 right = right.Resolve (tc);
1873 if (left == null || right == null)
1876 return ResolveOperator (tc);
1879 public bool IsBranchable ()
1881 if (oper == Operator.Equality ||
1882 oper == Operator.Inequality ||
1883 oper == Operator.LessThan ||
1884 oper == Operator.GreaterThan ||
1885 oper == Operator.LessThanOrEqual ||
1886 oper == Operator.GreaterThanOrEqual){
1893 // This entry point is used by routines that might want
1894 // to emit a brfalse/brtrue after an expression, and instead
1895 // they could use a more compact notation.
1897 // Typically the code would generate l.emit/r.emit, followed
1898 // by the comparission and then a brtrue/brfalse. The comparissions
1899 // are sometimes inneficient (there are not as complete as the branches
1900 // look for the hacks in Emit using double ceqs).
1902 // So for those cases we provide EmitBranchable that can emit the
1903 // branch with the test
1905 public void EmitBranchable (EmitContext ec, int target)
1908 bool close_target = false;
1914 case Operator.Equality:
1916 opcode = OpCodes.Beq_S;
1918 opcode = OpCodes.Beq;
1921 case Operator.Inequality:
1923 opcode = OpCodes.Bne_Un_S;
1925 opcode = OpCodes.Bne_Un;
1928 case Operator.LessThan:
1930 opcode = OpCodes.Blt_S;
1932 opcode = OpCodes.Blt;
1935 case Operator.GreaterThan:
1937 opcode = OpCodes.Bgt_S;
1939 opcode = OpCodes.Bgt;
1942 case Operator.LessThanOrEqual:
1944 opcode = OpCodes.Ble_S;
1946 opcode = OpCodes.Ble;
1949 case Operator.GreaterThanOrEqual:
1951 opcode = OpCodes.Bge_S;
1953 opcode = OpCodes.Ble;
1957 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1958 + oper.ToString ());
1961 ec.ig.Emit (opcode, target);
1964 public override void Emit (EmitContext ec)
1966 ILGenerator ig = ec.ig;
1968 Type r = right.Type;
1971 if (method != null) {
1973 // Note that operators are static anyway
1975 if (Arguments != null)
1976 Invocation.EmitArguments (ec, method, Arguments);
1978 if (method is MethodInfo)
1979 ig.Emit (OpCodes.Call, (MethodInfo) method);
1981 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1990 case Operator.Multiply:
1992 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1993 opcode = OpCodes.Mul_Ovf;
1994 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1995 opcode = OpCodes.Mul_Ovf_Un;
1997 opcode = OpCodes.Mul;
1999 opcode = OpCodes.Mul;
2003 case Operator.Division:
2004 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2005 opcode = OpCodes.Div_Un;
2007 opcode = OpCodes.Div;
2010 case Operator.Modulus:
2011 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2012 opcode = OpCodes.Rem_Un;
2014 opcode = OpCodes.Rem;
2017 case Operator.Addition:
2019 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2020 opcode = OpCodes.Add_Ovf;
2021 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2022 opcode = OpCodes.Add_Ovf_Un;
2024 opcode = OpCodes.Mul;
2026 opcode = OpCodes.Add;
2029 case Operator.Subtraction:
2031 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2032 opcode = OpCodes.Sub_Ovf;
2033 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2034 opcode = OpCodes.Sub_Ovf_Un;
2036 opcode = OpCodes.Sub;
2038 opcode = OpCodes.Sub;
2041 case Operator.RightShift:
2042 opcode = OpCodes.Shr;
2045 case Operator.LeftShift:
2046 opcode = OpCodes.Shl;
2049 case Operator.Equality:
2050 opcode = OpCodes.Ceq;
2053 case Operator.Inequality:
2054 ec.ig.Emit (OpCodes.Ceq);
2055 ec.ig.Emit (OpCodes.Ldc_I4_0);
2057 opcode = OpCodes.Ceq;
2060 case Operator.LessThan:
2061 opcode = OpCodes.Clt;
2064 case Operator.GreaterThan:
2065 opcode = OpCodes.Cgt;
2068 case Operator.LessThanOrEqual:
2069 ec.ig.Emit (OpCodes.Cgt);
2070 ec.ig.Emit (OpCodes.Ldc_I4_0);
2072 opcode = OpCodes.Ceq;
2075 case Operator.GreaterThanOrEqual:
2076 ec.ig.Emit (OpCodes.Clt);
2077 ec.ig.Emit (OpCodes.Ldc_I4_1);
2079 opcode = OpCodes.Sub;
2082 case Operator.LogicalOr:
2083 case Operator.BitwiseOr:
2084 opcode = OpCodes.Or;
2087 case Operator.LogicalAnd:
2088 case Operator.BitwiseAnd:
2089 opcode = OpCodes.And;
2092 case Operator.ExclusiveOr:
2093 opcode = OpCodes.Xor;
2097 throw new Exception ("This should not happen: Operator = "
2098 + oper.ToString ());
2105 public class Conditional : Expression {
2106 Expression expr, trueExpr, falseExpr;
2108 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
2111 this.trueExpr = trueExpr;
2112 this.falseExpr = falseExpr;
2115 public Expression Expr {
2121 public Expression TrueExpr {
2127 public Expression FalseExpr {
2133 public override Expression Resolve (TypeContainer tc)
2135 // FIXME: Implement;
2136 throw new Exception ("Unimplemented");
2140 public override void Emit (EmitContext ec)
2145 public class SimpleName : Expression {
2146 public readonly string Name;
2147 public readonly Location Location;
2149 public SimpleName (string name, Location l)
2156 // Checks whether we are trying to access an instance
2157 // property, method or field from a static body.
2159 Expression MemberStaticCheck (Report r, Expression e)
2161 if (e is FieldExpr){
2162 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2166 "An object reference is required " +
2167 "for the non-static field `"+Name+"'");
2170 } else if (e is MethodGroupExpr){
2171 // FIXME: Pending reorganization of MemberLookup
2172 // Basically at this point we should have the
2173 // best match already selected for us, and
2174 // we should only have to check a *single*
2175 // Method for its static on/off bit.
2177 } else if (e is PropertyExpr){
2178 if (!((PropertyExpr) e).IsStatic){
2180 "An object reference is required " +
2181 "for the non-static property access `"+
2191 // 7.5.2: Simple Names.
2193 // Local Variables and Parameters are handled at
2194 // parse time, so they never occur as SimpleNames.
2196 Expression ResolveSimpleName (TypeContainer tc)
2199 Report r = tc.RootContext.Report;
2201 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2205 else if (e is FieldExpr){
2206 FieldExpr fe = (FieldExpr) e;
2208 if (!fe.FieldInfo.IsStatic)
2209 fe.Instance = new This ();
2212 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2213 return MemberStaticCheck (r, e);
2219 // Do step 3 of the Simple Name resolution.
2221 // FIXME: implement me.
2223 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2230 // SimpleName needs to handle a multitude of cases:
2232 // simple_names and qualified_identifiers are placed on
2233 // the tree equally.
2235 public override Expression Resolve (TypeContainer tc)
2237 if (Name.IndexOf (".") != -1)
2238 return ResolveMemberAccess (tc, Name);
2240 return ResolveSimpleName (tc);
2243 public override void Emit (EmitContext ec)
2245 throw new Exception ("SimpleNames should be gone from the tree");
2250 // A simple interface that should be implemeneted by LValues
2252 public interface LValue {
2255 // The Store method should store the contents of the top
2256 // of the stack into the storage that is implemented by
2257 // the particular implementation of LValue
2259 void Store (EmitContext ec);
2262 // The AddressOf method should generate code that loads
2263 // the address of the LValue and leaves it on the stack
2265 void AddressOf (EmitContext ec);
2268 public class LocalVariableReference : Expression, LValue {
2269 public readonly string Name;
2270 public readonly Block Block;
2272 public LocalVariableReference (Block block, string name)
2276 eclass = ExprClass.Variable;
2279 public VariableInfo VariableInfo {
2281 return Block.GetVariableInfo (Name);
2285 public override Expression Resolve (TypeContainer tc)
2287 VariableInfo vi = Block.GetVariableInfo (Name);
2289 type = vi.VariableType;
2293 public override void Emit (EmitContext ec)
2295 VariableInfo vi = VariableInfo;
2296 ILGenerator ig = ec.ig;
2301 ig.Emit (OpCodes.Ldloc_0);
2305 ig.Emit (OpCodes.Ldloc_1);
2309 ig.Emit (OpCodes.Ldloc_2);
2313 ig.Emit (OpCodes.Ldloc_3);
2318 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2320 ig.Emit (OpCodes.Ldloc, idx);
2325 public void Store (EmitContext ec)
2327 ILGenerator ig = ec.ig;
2328 VariableInfo vi = VariableInfo;
2333 ig.Emit (OpCodes.Stloc_0);
2337 ig.Emit (OpCodes.Stloc_1);
2341 ig.Emit (OpCodes.Stloc_2);
2345 ig.Emit (OpCodes.Stloc_3);
2350 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2352 ig.Emit (OpCodes.Stloc, idx);
2357 public void AddressOf (EmitContext ec)
2359 VariableInfo vi = VariableInfo;
2363 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2365 ec.ig.Emit (OpCodes.Ldloca, idx);
2369 public class ParameterReference : Expression, LValue {
2370 public readonly Parameters Pars;
2371 public readonly String Name;
2372 public readonly int Idx;
2374 public ParameterReference (Parameters pars, int idx, string name)
2379 eclass = ExprClass.Variable;
2382 public override Expression Resolve (TypeContainer tc)
2384 Type [] types = Pars.GetParameterInfo (tc);
2391 public override void Emit (EmitContext ec)
2394 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
2396 ec.ig.Emit (OpCodes.Ldarg, Idx);
2399 public void Store (EmitContext ec)
2402 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
2404 ec.ig.Emit (OpCodes.Starg, Idx);
2408 public void AddressOf (EmitContext ec)
2411 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
2413 ec.ig.Emit (OpCodes.Ldarga, Idx);
2418 // Used for arguments to New(), Invocation()
2420 public class Argument {
2427 public readonly AType Type;
2430 public Argument (Expression expr, AType type)
2436 public Expression Expr {
2446 public bool Resolve (TypeContainer tc)
2448 expr = expr.Resolve (tc);
2450 return expr != null;
2453 public void Emit (EmitContext ec)
2460 // Invocation of methods or delegates.
2462 public class Invocation : ExpressionStatement {
2463 public readonly ArrayList Arguments;
2464 public readonly Location Location;
2467 MethodBase method = null;
2469 static Hashtable method_parameter_cache;
2471 static Invocation ()
2473 method_parameter_cache = new Hashtable ();
2477 // arguments is an ArrayList, but we do not want to typecast,
2478 // as it might be null.
2480 // FIXME: only allow expr to be a method invocation or a
2481 // delegate invocation (7.5.5)
2483 public Invocation (Expression expr, ArrayList arguments, Location l)
2486 Arguments = arguments;
2490 public Expression Expr {
2497 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2498 /// compatible, and if so, how good is the match (in terms of
2499 /// "better conversions" (7.4.2.3).
2501 /// 0 is the best possible match.
2502 /// -1 represents a type mismatch.
2503 /// -2 represents a ref/out mismatch.
2505 static int Badness (Argument a, Type t)
2507 Expression argument_expr = a.Expr;
2508 Type argument_type = argument_expr.Type;
2510 if (argument_type == null){
2511 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2514 if (t == argument_type)
2518 // Now probe whether an implicit constant expression conversion
2521 // An implicit constant expression conversion permits the following
2524 // * A constant-expression of type `int' can be converted to type
2525 // sbyte, byute, short, ushort, uint, ulong provided the value of
2526 // of the expression is withing the range of the destination type.
2528 // * A constant-expression of type long can be converted to type
2529 // ulong, provided the value of the constant expression is not negative
2531 // FIXME: Note that this assumes that constant folding has
2532 // taken place. We dont do constant folding yet.
2535 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2536 IntLiteral ei = (IntLiteral) argument_expr;
2537 int value = ei.Value;
2539 if (t == TypeManager.sbyte_type){
2540 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2542 } else if (t == TypeManager.byte_type){
2543 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2545 } else if (t == TypeManager.short_type){
2546 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2548 } else if (t == TypeManager.ushort_type){
2549 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2551 } else if (t == TypeManager.uint32_type){
2553 // we can optimize this case: a positive int32
2554 // always fits on a uint32
2558 } else if (t == TypeManager.uint64_type){
2560 // we can optimize this case: a positive int32
2561 // always fits on a uint64
2566 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2567 LongLiteral ll = (LongLiteral) argument_expr;
2569 if (t == TypeManager.uint64_type)
2574 // FIXME: Implement user-defined implicit conversions here.
2575 // FIXME: Implement better conversion here.
2581 // Returns the Parameters (a ParameterData interface) for the
2584 static ParameterData GetParameterData (MethodBase mb)
2586 object pd = method_parameter_cache [mb];
2589 return (ParameterData) pd;
2591 if (mb is MethodBuilder || mb is ConstructorBuilder){
2592 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2594 InternalParameters ip = mc.ParameterInfo;
2595 method_parameter_cache [mb] = ip;
2597 return (ParameterData) ip;
2599 ParameterInfo [] pi = mb.GetParameters ();
2600 ReflectionParameters rp = new ReflectionParameters (pi);
2601 method_parameter_cache [mb] = rp;
2603 return (ParameterData) rp;
2607 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2609 // Locate user-defined implicit operators
2613 mg = MemberLookup (tc, to, "op_Implicit", false);
2616 MethodGroupExpr me = (MethodGroupExpr) mg;
2618 for (int i = me.Methods.Length; i > 0;) {
2620 MethodBase mb = me.Methods [i];
2621 ParameterData pd = GetParameterData (mb);
2623 if (from == pd.ParameterType (0))
2628 mg = MemberLookup (tc, from, "op_Implicit", false);
2631 MethodGroupExpr me = (MethodGroupExpr) mg;
2633 for (int i = me.Methods.Length; i > 0;) {
2635 MethodBase mb = me.Methods [i];
2636 MethodInfo mi = (MethodInfo) mb;
2638 if (mi.ReturnType == to)
2647 // Determines "better conversion" as specified in 7.4.2.3
2648 // Returns : 1 if a->p is better
2649 // 0 if a->q or neither is better
2651 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2654 Type argument_type = a.Expr.Type;
2655 Expression argument_expr = a.Expr;
2657 if (argument_type == null)
2658 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2663 if (argument_type == p)
2666 if (argument_type == q)
2670 // Now probe whether an implicit constant expression conversion
2673 // An implicit constant expression conversion permits the following
2676 // * A constant-expression of type `int' can be converted to type
2677 // sbyte, byute, short, ushort, uint, ulong provided the value of
2678 // of the expression is withing the range of the destination type.
2680 // * A constant-expression of type long can be converted to type
2681 // ulong, provided the value of the constant expression is not negative
2683 // FIXME: Note that this assumes that constant folding has
2684 // taken place. We dont do constant folding yet.
2687 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2688 IntLiteral ei = (IntLiteral) argument_expr;
2689 int value = ei.Value;
2691 if (p == TypeManager.sbyte_type){
2692 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2694 } else if (p == TypeManager.byte_type){
2695 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2697 } else if (p == TypeManager.short_type){
2698 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2700 } else if (p == TypeManager.ushort_type){
2701 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2703 } else if (p == TypeManager.uint32_type){
2705 // we can optimize this case: a positive int32
2706 // always fits on a uint32
2710 } else if (p == TypeManager.uint64_type){
2712 // we can optimize this case: a positive int32
2713 // always fits on a uint64
2718 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2719 LongLiteral ll = (LongLiteral) argument_expr;
2721 if (p == TypeManager.uint64_type){
2731 tmp = ConvertImplicit (tc, argument_expr, p);
2740 if (ConversionExists (tc, p, q) == true &&
2741 ConversionExists (tc, q, p) == false)
2744 if (p == TypeManager.sbyte_type)
2745 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2746 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2749 if (p == TypeManager.short_type)
2750 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2751 q == TypeManager.uint64_type)
2754 if (p == TypeManager.int32_type)
2755 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2758 if (p == TypeManager.int64_type)
2759 if (q == TypeManager.uint64_type)
2766 // Determines "Better function" and returns an integer indicating :
2767 // 0 if candidate ain't better
2768 // 1 if candidate is better than the current best match
2770 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2772 ParameterData candidate_pd = GetParameterData (candidate);
2773 ParameterData best_pd;
2779 argument_count = args.Count;
2781 if (candidate_pd.Count == 0 && argument_count == 0)
2785 if (candidate_pd.Count == argument_count) {
2787 for (int j = argument_count; j > 0;) {
2790 Argument a = (Argument) args [j];
2792 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2809 best_pd = GetParameterData (best);
2811 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2812 int rating1 = 0, rating2 = 0;
2814 for (int j = argument_count; j > 0;) {
2818 Argument a = (Argument) args [j];
2820 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2821 best_pd.ParameterType (j));
2822 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2823 candidate_pd.ParameterType (j));
2829 if (rating1 > rating2)
2838 public static string FullMethodDesc (MethodBase mb)
2840 StringBuilder sb = new StringBuilder (mb.Name);
2841 ParameterData pd = GetParameterData (mb);
2844 for (int i = pd.Count; i > 0;) {
2846 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2852 return sb.ToString ();
2855 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2858 if (mg1 != null || mg2 != null) {
2860 MethodGroupExpr left_set = null, right_set = null;
2861 int length1 = 0, length2 = 0;
2864 left_set = (MethodGroupExpr) mg1;
2865 length1 = left_set.Methods.Length;
2869 right_set = (MethodGroupExpr) mg2;
2870 length2 = right_set.Methods.Length;
2873 MemberInfo [] miset = new MemberInfo [length1 + length2];
2874 if (left_set != null)
2875 left_set.Methods.CopyTo (miset, 0);
2876 if (right_set != null)
2877 right_set.Methods.CopyTo (miset, length1);
2879 MethodGroupExpr union = new MethodGroupExpr (miset);
2890 // Find the Applicable Function Members (7.4.2.1)
2892 // me: Method Group expression with the members to select.
2893 // it might contain constructors or methods (or anything
2894 // that maps to a method).
2896 // Arguments: ArrayList containing resolved Argument objects.
2898 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2899 // that is the best match of me on Arguments.
2902 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2903 ArrayList Arguments, Location loc)
2905 ArrayList afm = new ArrayList ();
2906 int best_match_idx = -1;
2907 MethodBase method = null;
2910 for (int i = me.Methods.Length; i > 0; ){
2912 MethodBase candidate = me.Methods [i];
2915 x = BetterFunction (tc, Arguments, candidate, method);
2921 method = me.Methods [best_match_idx];
2925 if (Arguments == null)
2928 argument_count = Arguments.Count;
2932 // Now we see if we can at least find a method with the same number of arguments
2933 // and then try doing implicit conversion on the arguments
2934 if (best_match_idx == -1) {
2936 for (int i = me.Methods.Length; i > 0;) {
2938 MethodBase mb = me.Methods [i];
2939 pd = GetParameterData (mb);
2941 if (pd.Count == argument_count) {
2943 method = me.Methods [best_match_idx];
2954 // And now convert implicitly, each argument to the required type
2956 pd = GetParameterData (method);
2958 for (int j = argument_count; j > 0;) {
2960 Argument a = (Argument) Arguments [j];
2961 Expression a_expr = a.Expr;
2963 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
2966 Error (tc, 1502, loc,
2967 "The best overloaded match for method '" + FullMethodDesc (method) +
2968 "' has some invalid arguments");
2969 Error (tc, 1503, loc,
2970 "Argument " + (j+1) +
2971 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
2972 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
2977 // Update the argument with the implicit conversion
2987 public override Expression Resolve (TypeContainer tc)
2990 // First, resolve the expression that is used to
2991 // trigger the invocation
2993 this.expr = expr.Resolve (tc);
2994 if (this.expr == null)
2997 if (!(this.expr is MethodGroupExpr)){
2998 report118 (tc, this.expr, "method group");
3003 // Next, evaluate all the expressions in the argument list
3005 if (Arguments != null){
3006 for (int i = Arguments.Count; i > 0;){
3008 Argument a = (Argument) Arguments [i];
3010 if (!a.Resolve (tc))
3015 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
3017 if (method == null){
3018 Error (tc, -6, Location,
3019 "Could not find any applicable function for this argument list");
3023 if (method is MethodInfo)
3024 type = ((MethodInfo)method).ReturnType;
3029 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3033 if (Arguments != null)
3034 top = Arguments.Count;
3038 for (int i = 0; i < top; i++){
3039 Argument a = (Argument) Arguments [i];
3045 public override void Emit (EmitContext ec)
3047 bool is_static = method.IsStatic;
3050 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3053 // If this is ourselves, push "this"
3055 if (mg.InstanceExpression == null){
3056 ec.ig.Emit (OpCodes.Ldarg_0);
3059 // Push the instance expression
3061 mg.InstanceExpression.Emit (ec);
3065 if (Arguments != null)
3066 EmitArguments (ec, method, Arguments);
3069 if (method is MethodInfo)
3070 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3072 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3074 if (method is MethodInfo)
3075 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3077 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3081 public override void EmitStatement (EmitContext ec)
3086 // Pop the return value if there is one
3088 if (method is MethodInfo){
3089 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3090 ec.ig.Emit (OpCodes.Pop);
3095 public class New : ExpressionStatement {
3102 public readonly NType NewType;
3103 public readonly ArrayList Arguments;
3104 public readonly string RequestedType;
3105 // These are for the case when we have an array
3106 public readonly string Rank;
3107 public readonly ArrayList Indices;
3108 public readonly ArrayList Initializers;
3111 MethodBase method = null;
3113 public New (string requested_type, ArrayList arguments, Location loc)
3115 RequestedType = requested_type;
3116 Arguments = arguments;
3117 NewType = NType.Object;
3121 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3123 RequestedType = requested_type;
3126 Initializers = initializers;
3127 NewType = NType.Array;
3131 public override Expression Resolve (TypeContainer tc)
3133 type = tc.LookupType (RequestedType, false);
3140 ml = MemberLookup (tc, type, ".ctor", false,
3141 MemberTypes.Constructor, AllBindingsFlags);
3143 if (! (ml is MethodGroupExpr)){
3145 // FIXME: Find proper error
3147 report118 (tc, ml, "method group");
3151 if (Arguments != null){
3152 for (int i = Arguments.Count; i > 0;){
3154 Argument a = (Argument) Arguments [i];
3156 if (!a.Resolve (tc))
3161 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3163 if (method == null) {
3164 Error (tc, -6, Location,
3165 "New invocation: Can not find a constructor for this argument list");
3172 public override void Emit (EmitContext ec)
3174 Invocation.EmitArguments (ec, method, Arguments);
3175 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3178 public override void EmitStatement (EmitContext ec)
3181 ec.ig.Emit (OpCodes.Pop);
3186 // Represents the `this' construct
3188 public class This : Expression, LValue {
3189 public override Expression Resolve (TypeContainer tc)
3191 eclass = ExprClass.Variable;
3192 type = tc.TypeBuilder;
3195 // FIXME: Verify that this is only used in instance contexts.
3200 public override void Emit (EmitContext ec)
3202 ec.ig.Emit (OpCodes.Ldarg_0);
3205 public void Store (EmitContext ec)
3208 // Assignment to the "this" variable.
3210 // FIXME: Apparently this is a bug that we
3211 // must catch as `this' seems to be readonly ;-)
3213 ec.ig.Emit (OpCodes.Starg, 0);
3216 public void AddressOf (EmitContext ec)
3218 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3222 public class TypeOf : Expression {
3223 public readonly string QueriedType;
3225 public TypeOf (string queried_type)
3227 QueriedType = queried_type;
3230 public override Expression Resolve (TypeContainer tc)
3232 type = tc.LookupType (QueriedType, false);
3237 eclass = ExprClass.Type;
3241 public override void Emit (EmitContext ec)
3243 throw new Exception ("Implement me");
3244 // FIXME: Implement.
3248 public class SizeOf : Expression {
3249 public readonly string QueriedType;
3251 public SizeOf (string queried_type)
3253 this.QueriedType = queried_type;
3256 public override Expression Resolve (TypeContainer tc)
3258 // FIXME: Implement;
3259 throw new Exception ("Unimplemented");
3263 public override void Emit (EmitContext ec)
3265 throw new Exception ("Implement me");
3269 public class MemberAccess : Expression {
3270 public readonly string Identifier;
3272 Expression member_lookup;
3274 public MemberAccess (Expression expr, string id)
3280 public Expression Expr {
3286 public override Expression Resolve (TypeContainer tc)
3288 Expression new_expression = expr.Resolve (tc);
3290 if (new_expression == null)
3293 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
3295 if (member_lookup is MethodGroupExpr){
3296 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3299 // Bind the instance expression to it
3301 // FIXME: This is a horrible way of detecting if it is
3302 // an instance expression. Figure out how to fix this.
3305 if (expr is LocalVariableReference ||
3306 expr is ParameterReference ||
3308 mg.InstanceExpression = expr;
3310 return member_lookup;
3311 } else if (member_lookup is FieldExpr){
3312 FieldExpr fe = (FieldExpr) member_lookup;
3316 return member_lookup;
3319 // FIXME: This should generate the proper node
3320 // ie, for a Property Access, it should like call it
3323 return member_lookup;
3326 public override void Emit (EmitContext ec)
3328 throw new Exception ("Implement me");
3334 // Nodes of type Namespace are created during the semantic
3335 // analysis to resolve member_access/qualified_identifier/simple_name
3338 // They are born `resolved'.
3340 public class NamespaceExpr : Expression {
3341 public readonly string Name;
3343 public NamespaceExpr (string name)
3346 eclass = ExprClass.Namespace;
3349 public override Expression Resolve (TypeContainer tc)
3354 public override void Emit (EmitContext ec)
3356 throw new Exception ("Namespace expressions should never be emitted");
3361 // Fully resolved expression that evaluates to a type
3363 public class TypeExpr : Expression {
3364 public TypeExpr (Type t)
3367 eclass = ExprClass.Type;
3370 override public Expression Resolve (TypeContainer tc)
3375 override public void Emit (EmitContext ec)
3377 throw new Exception ("Implement me");
3382 // MethodGroup Expression.
3384 // This is a fully resolved expression that evaluates to a type
3386 public class MethodGroupExpr : Expression {
3387 public readonly MethodBase [] Methods;
3388 Expression instance_expression = null;
3390 public MethodGroupExpr (MemberInfo [] mi)
3392 Methods = new MethodBase [mi.Length];
3393 mi.CopyTo (Methods, 0);
3394 eclass = ExprClass.MethodGroup;
3398 // `A method group may have associated an instance expression'
3400 public Expression InstanceExpression {
3402 return instance_expression;
3406 instance_expression = value;
3410 override public Expression Resolve (TypeContainer tc)
3415 override public void Emit (EmitContext ec)
3417 throw new Exception ("This should never be reached");
3421 // Fully resolved expression that evaluates to a Field
3423 public class FieldExpr : Expression, LValue {
3424 public readonly FieldInfo FieldInfo;
3425 public Expression Instance;
3427 public FieldExpr (FieldInfo fi)
3430 eclass = ExprClass.Variable;
3431 type = fi.FieldType;
3434 override public Expression Resolve (TypeContainer tc)
3436 if (!FieldInfo.IsStatic){
3437 if (Instance == null){
3438 throw new Exception ("non-static FieldExpr without instance var\n" +
3439 "You have to assign the Instance variable\n" +
3440 "Of the FieldExpr to set this\n");
3443 Instance = Instance.Resolve (tc);
3444 if (Instance == null)
3451 override public void Emit (EmitContext ec)
3453 ILGenerator ig = ec.ig;
3455 if (FieldInfo.IsStatic)
3456 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3460 ig.Emit (OpCodes.Ldfld, FieldInfo);
3464 public void Store (EmitContext ec)
3466 if (FieldInfo.IsStatic)
3467 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3469 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3472 public void AddressOf (EmitContext ec)
3474 if (FieldInfo.IsStatic)
3475 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3478 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3484 // Fully resolved expression that evaluates to a Property
3486 public class PropertyExpr : Expression {
3487 public readonly PropertyInfo PropertyInfo;
3488 public readonly bool IsStatic;
3490 public PropertyExpr (PropertyInfo pi)
3493 eclass = ExprClass.PropertyAccess;
3496 MethodBase [] acc = pi.GetAccessors ();
3498 for (int i = 0; i < acc.Length; i++)
3499 if (acc [i].IsStatic)
3502 type = pi.PropertyType;
3505 override public Expression Resolve (TypeContainer tc)
3507 // We are born in resolved state.
3511 override public void Emit (EmitContext ec)
3513 // FIXME: Implement;
3514 throw new Exception ("Unimplemented");
3519 // Fully resolved expression that evaluates to a Expression
3521 public class EventExpr : Expression {
3522 public readonly EventInfo EventInfo;
3524 public EventExpr (EventInfo ei)
3527 eclass = ExprClass.EventAccess;
3530 override public Expression Resolve (TypeContainer tc)
3532 // We are born in resolved state.
3536 override public void Emit (EmitContext ec)
3538 throw new Exception ("Implement me");
3539 // FIXME: Implement.
3543 public class CheckedExpr : Expression {
3545 public Expression Expr;
3547 public CheckedExpr (Expression e)
3552 public override Expression Resolve (TypeContainer tc)
3554 Expr = Expr.Resolve (tc);
3559 eclass = Expr.ExprClass;
3564 public override void Emit (EmitContext ec)
3566 bool last_check = ec.CheckState;
3568 ec.CheckState = true;
3570 ec.CheckState = last_check;
3575 public class UnCheckedExpr : Expression {
3577 public Expression Expr;
3579 public UnCheckedExpr (Expression e)
3584 public override Expression Resolve (TypeContainer tc)
3586 Expr = Expr.Resolve (tc);
3591 eclass = Expr.ExprClass;
3596 public override void Emit (EmitContext ec)
3598 bool last_check = ec.CheckState;
3600 ec.CheckState = false;
3602 ec.CheckState = last_check;
3607 public class ElementAccess : Expression {
3609 public readonly ArrayList Arguments;
3610 public readonly Expression Expr;
3612 public ElementAccess (Expression e, ArrayList e_list)
3618 public override Expression Resolve (TypeContainer tc)
3620 // FIXME: Implement;
3621 throw new Exception ("Unimplemented");
3625 public override void Emit (EmitContext ec)
3627 // FIXME : Implement !
3628 throw new Exception ("Unimplemented");
3633 public class BaseAccess : Expression {
3635 public enum BaseAccessType {
3640 public readonly BaseAccessType BAType;
3641 public readonly string Member;
3642 public readonly ArrayList Arguments;
3644 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3652 public override Expression Resolve (TypeContainer tc)
3654 // FIXME: Implement;
3655 throw new Exception ("Unimplemented");
3659 public override void Emit (EmitContext ec)
3661 throw new Exception ("Unimplemented");
3665 public class UserImplicitCast : Expression {
3667 ArrayList arguments;
3669 public UserImplicitCast (MethodInfo method, ArrayList arguments)
3671 this.method = method;
3672 this.arguments = arguments;
3673 type = method.ReturnType;
3674 eclass = ExprClass.Value;
3677 public override Expression Resolve (TypeContainer tc)
3680 // We are born in a fully resolved state
3685 public static Expression CanConvert (TypeContainer tc, Expression source, Type target)
3687 Expression mg1, mg2;
3689 ArrayList arguments;
3691 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3692 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3694 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3696 if (union != null) {
3697 arguments = new ArrayList ();
3698 arguments.Add (new Argument (source, Argument.AType.Expression));
3700 method = Invocation.OverloadResolve (tc, union, arguments,
3701 new Location ("FIXME", 1, 1));
3703 if (method != null) {
3704 MethodInfo mi = (MethodInfo) method;
3706 if (mi.ReturnType == target)
3707 return new UserImplicitCast (mi, arguments);
3711 // If we have a boolean type, we need to check for the True
3712 // and False operators too.
3714 if (target == TypeManager.bool_type) {
3716 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3717 mg2 = MemberLookup (tc, target, "op_True", false);
3719 union = Invocation.MakeUnionSet (mg1, mg2);
3724 arguments = new ArrayList ();
3725 arguments.Add (new Argument (source, Argument.AType.Expression));
3727 method = Invocation.OverloadResolve (tc, union, arguments,
3728 new Location ("FIXME", 1, 1));
3729 if (method != null) {
3730 MethodInfo mi = (MethodInfo) method;
3732 if (mi.ReturnType == target)
3733 return new UserImplicitCast (mi, arguments);
3740 public override void Emit (EmitContext ec)
3742 ILGenerator ig = ec.ig;
3744 if (method != null) {
3746 // Note that operators are static anyway
3748 if (arguments != null)
3749 Invocation.EmitArguments (ec, method, arguments);
3751 if (method is MethodInfo)
3752 ig.Emit (OpCodes.Call, (MethodInfo) method);
3754 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3759 throw new Exception ("Implement me");