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, target_type);
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);
343 // Converts implicitly the resolved expression `expr' into the
344 // `target_type'. It returns a new expression that can be used
345 // in a context that expects a `target_type'.
347 static public Expression ConvertImplicit (TypeContainer tc, Expression expr, Type target_type)
349 Type expr_type = expr.Type;
351 if (expr_type == target_type)
355 // Step 2: Built-in conversions.
357 if (expr_type == TypeManager.sbyte_type){
359 // From sbyte to short, int, long, float, double.
361 if (target_type == TypeManager.int32_type)
362 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
363 if (target_type == TypeManager.int64_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
365 if (target_type == TypeManager.double_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
367 if (target_type == TypeManager.float_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
369 if (target_type == TypeManager.short_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
371 if (target_type == TypeManager.decimal_type)
372 return InternalTypeConstructor (tc, expr, target_type);
373 } else if (expr_type == TypeManager.byte_type){
375 // From byte to short, ushort, int, uint, long, ulong, float, double
377 if ((target_type == TypeManager.short_type) ||
378 (target_type == TypeManager.ushort_type) ||
379 (target_type == TypeManager.int32_type) ||
380 (target_type == TypeManager.uint32_type))
381 return new EmptyCast (expr, target_type);
383 if (target_type == TypeManager.uint64_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
385 if (target_type == TypeManager.int64_type)
386 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
388 if (target_type == TypeManager.float_type)
389 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
390 if (target_type == TypeManager.double_type)
391 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
392 if (target_type == TypeManager.decimal_type)
393 return InternalTypeConstructor (tc, expr, target_type);
394 } else if (expr_type == TypeManager.short_type){
396 // From short to int, long, float, double
398 if (target_type == TypeManager.int32_type)
399 return new EmptyCast (expr, target_type);
400 if (target_type == TypeManager.int64_type)
401 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
402 if (target_type == TypeManager.double_type)
403 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
404 if (target_type == TypeManager.float_type)
405 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
406 if (target_type == TypeManager.decimal_type)
407 return InternalTypeConstructor (tc, expr, target_type);
408 } else if (expr_type == TypeManager.ushort_type){
410 // From ushort to int, uint, long, ulong, float, double
412 if ((target_type == TypeManager.uint32_type) ||
413 (target_type == TypeManager.uint64_type))
414 return new EmptyCast (expr, target_type);
416 if (target_type == TypeManager.int32_type)
417 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
418 if (target_type == TypeManager.int64_type)
419 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
420 if (target_type == TypeManager.double_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
422 if (target_type == TypeManager.float_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
424 if (target_type == TypeManager.decimal_type)
425 return InternalTypeConstructor (tc, expr, target_type);
426 } else if (expr_type == TypeManager.int32_type){
428 // From int to long, float, double
430 if (target_type == TypeManager.int64_type)
431 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
432 if (target_type == TypeManager.double_type)
433 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
434 if (target_type == TypeManager.float_type)
435 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
436 if (target_type == TypeManager.decimal_type)
437 return InternalTypeConstructor (tc, expr, target_type);
438 } else if (expr_type == TypeManager.uint32_type){
440 // From uint to long, ulong, float, double
442 if (target_type == TypeManager.int64_type)
443 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
444 if (target_type == TypeManager.uint64_type)
445 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
446 if (target_type == TypeManager.double_type)
447 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
449 if (target_type == TypeManager.float_type)
450 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
452 if (target_type == TypeManager.decimal_type)
453 return InternalTypeConstructor (tc, expr, target_type);
454 } else if ((expr_type == TypeManager.uint64_type) ||
455 (expr_type == TypeManager.int64_type)){
457 // From long to float, double
459 if (target_type == TypeManager.double_type)
460 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
462 if (target_type == TypeManager.float_type)
463 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
465 if (target_type == TypeManager.decimal_type)
466 return InternalTypeConstructor (tc, expr, target_type);
467 } else if (expr_type == TypeManager.char_type){
469 // From char to ushort, int, uint, long, ulong, float, double
471 if ((target_type == TypeManager.ushort_type) ||
472 (target_type == TypeManager.int32_type) ||
473 (target_type == TypeManager.uint32_type))
474 return new EmptyCast (expr, target_type);
475 if (target_type == TypeManager.uint64_type)
476 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
477 if (target_type == TypeManager.int64_type)
478 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
479 if (target_type == TypeManager.float_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
481 if (target_type == TypeManager.double_type)
482 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
483 if (target_type == TypeManager.decimal_type)
484 return InternalTypeConstructor (tc, expr, target_type);
486 return ImplicitReferenceConversion (expr, target_type);
489 if (UserImplicitCast.CanConvert (tc, expr, target_type) == true) {
490 Expression imp = new UserImplicitCast (expr, target_type);
496 // Could not find an implicit cast.
502 // Attemps to perform an implict constant conversion of the IntLiteral
503 // into a different data type using casts (See Implicit Constant
504 // Expression Conversions)
506 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
508 int value = il.Value;
510 if (target_type == TypeManager.sbyte_type){
511 if (value >= SByte.MinValue && value <= SByte.MaxValue)
513 } else if (target_type == TypeManager.byte_type){
514 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
516 } else if (target_type == TypeManager.short_type){
517 if (value >= Int16.MinValue && value <= Int16.MaxValue)
519 } else if (target_type == TypeManager.ushort_type){
520 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
522 } else if (target_type == TypeManager.uint32_type){
524 // we can optimize this case: a positive int32
525 // always fits on a uint32
529 } else if (target_type == TypeManager.uint64_type){
531 // we can optimize this case: a positive int32
532 // always fits on a uint64. But we need an opcode
536 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
543 // Attemptes to implicityly convert `target' into `type', using
544 // ConvertImplicit. If there is no implicit conversion, then
545 // an error is signaled
547 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
548 Type type, Location l)
552 e = ConvertImplicit (tc, target, type);
557 // Attempt to do the implicit constant expression conversions
559 if (target is IntLiteral){
560 e = TryImplicitIntConversion (type, (IntLiteral) target);
563 } else if (target is LongLiteral){
565 // Try the implicit constant expression conversion
566 // from long to ulong, instead of a nice routine,
569 if (((LongLiteral) target).Value > 0)
573 string msg = "Can not convert implicitly from `"+
574 TypeManager.CSharpName (target.Type) + "' to `" +
575 TypeManager.CSharpName (type) + "'";
577 Error (tc, 29, l, msg);
583 // Performs the explicit numeric conversions
585 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
588 Type expr_type = expr.Type;
590 if (expr_type == TypeManager.sbyte_type){
592 // From sbyte to byte, ushort, uint, ulong, char
594 if (target_type == TypeManager.byte_type)
595 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
596 if (target_type == TypeManager.ushort_type)
597 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
598 if (target_type == TypeManager.uint32_type)
599 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
600 if (target_type == TypeManager.uint64_type)
601 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
602 if (target_type == TypeManager.char_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
604 } else if (expr_type == TypeManager.byte_type){
606 // From byte to sbyte and char
608 if (target_type == TypeManager.sbyte_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
610 if (target_type == TypeManager.char_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
612 } else if (expr_type == TypeManager.short_type){
614 // From short to sbyte, byte, ushort, uint, ulong, char
616 if (target_type == TypeManager.sbyte_type)
617 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
618 if (target_type == TypeManager.byte_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
620 if (target_type == TypeManager.ushort_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
622 if (target_type == TypeManager.uint32_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
624 if (target_type == TypeManager.uint64_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
626 if (target_type == TypeManager.char_type)
627 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
628 } else if (expr_type == TypeManager.ushort_type){
630 // From ushort to sbyte, byte, short, char
632 if (target_type == TypeManager.sbyte_type)
633 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
634 if (target_type == TypeManager.byte_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
636 if (target_type == TypeManager.short_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
638 if (target_type == TypeManager.char_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
640 } else if (expr_type == TypeManager.int32_type){
642 // From int to sbyte, byte, short, ushort, uint, ulong, char
644 if (target_type == TypeManager.sbyte_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
646 if (target_type == TypeManager.byte_type)
647 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
648 if (target_type == TypeManager.short_type)
649 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
650 if (target_type == TypeManager.ushort_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
652 if (target_type == TypeManager.uint32_type)
653 return new EmptyCast (expr, target_type);
654 if (target_type == TypeManager.uint64_type)
655 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
656 if (target_type == TypeManager.char_type)
657 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
658 } else if (expr_type == TypeManager.uint32_type){
660 // From uint to sbyte, byte, short, ushort, int, char
662 if (target_type == TypeManager.sbyte_type)
663 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
664 if (target_type == TypeManager.byte_type)
665 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
666 if (target_type == TypeManager.short_type)
667 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
668 if (target_type == TypeManager.ushort_type)
669 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
670 if (target_type == TypeManager.int32_type)
671 return new EmptyCast (expr, target_type);
672 if (target_type == TypeManager.char_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
674 } else if (expr_type == TypeManager.int64_type){
676 // From long to sbyte, byte, short, ushort, int, 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.int32_type)
687 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
688 if (target_type == TypeManager.uint32_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
690 if (target_type == TypeManager.uint64_type)
691 return new EmptyCast (expr, target_type);
692 if (target_type == TypeManager.char_type)
693 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
694 } else if (expr_type == TypeManager.uint64_type){
696 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
698 if (target_type == TypeManager.sbyte_type)
699 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
700 if (target_type == TypeManager.byte_type)
701 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
702 if (target_type == TypeManager.short_type)
703 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
704 if (target_type == TypeManager.ushort_type)
705 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
706 if (target_type == TypeManager.int32_type)
707 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
708 if (target_type == TypeManager.uint32_type)
709 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
710 if (target_type == TypeManager.int64_type)
711 return new EmptyCast (expr, target_type);
712 if (target_type == TypeManager.char_type)
713 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
714 } else if (expr_type == TypeManager.char_type){
716 // From char to sbyte, byte, short
718 if (target_type == TypeManager.sbyte_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
720 if (target_type == TypeManager.byte_type)
721 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
722 if (target_type == TypeManager.short_type)
723 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
724 } else if (expr_type == TypeManager.float_type){
726 // From float to sbyte, byte, short,
727 // ushort, int, uint, long, ulong, char
730 if (target_type == TypeManager.sbyte_type)
731 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
732 if (target_type == TypeManager.byte_type)
733 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
734 if (target_type == TypeManager.short_type)
735 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
736 if (target_type == TypeManager.ushort_type)
737 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
738 if (target_type == TypeManager.int32_type)
739 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
740 if (target_type == TypeManager.uint32_type)
741 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
742 if (target_type == TypeManager.int64_type)
743 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
744 if (target_type == TypeManager.uint64_type)
745 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
746 if (target_type == TypeManager.char_type)
747 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
748 if (target_type == TypeManager.decimal_type)
749 return InternalTypeConstructor (tc, expr, target_type);
750 } else if (expr_type == TypeManager.double_type){
752 // From double to byte, byte, short,
753 // ushort, int, uint, long, ulong,
754 // char, float or decimal
756 if (target_type == TypeManager.sbyte_type)
757 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
758 if (target_type == TypeManager.byte_type)
759 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
760 if (target_type == TypeManager.short_type)
761 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
762 if (target_type == TypeManager.ushort_type)
763 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
764 if (target_type == TypeManager.int32_type)
765 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
766 if (target_type == TypeManager.uint32_type)
767 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
768 if (target_type == TypeManager.int64_type)
769 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
770 if (target_type == TypeManager.uint64_type)
771 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
772 if (target_type == TypeManager.char_type)
773 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
774 if (target_type == TypeManager.float_type)
775 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
776 if (target_type == TypeManager.decimal_type)
777 return InternalTypeConstructor (tc, expr, target_type);
780 // decimal is taken care of by the op_Explicit methods.
786 // Performs an explicit conversion of the expression `expr' whose
787 // type is expr.Type to `target_type'.
789 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
792 Expression ne = ConvertImplicit (tc, expr, target_type);
797 ne = ConvertNumericExplicit (tc, expr, target_type);
805 static string ExprClassName (ExprClass c)
808 case ExprClass.Invalid:
810 case ExprClass.Value:
812 case ExprClass.Variable:
814 case ExprClass.Namespace:
818 case ExprClass.MethodGroup:
819 return "method group";
820 case ExprClass.PropertyAccess:
821 return "property access";
822 case ExprClass.EventAccess:
823 return "event access";
824 case ExprClass.IndexerAccess:
825 return "indexer access";
826 case ExprClass.Nothing:
829 throw new Exception ("Should not happen");
833 // Reports that we were expecting `expr' to be of class `expected'
835 protected void report118 (TypeContainer tc, Expression expr, string expected)
837 Error (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
838 "' where an " + expected + " was expected");
843 // This is just a base class for expressions that can
844 // appear on statements (invocations, object creation,
845 // assignments, post/pre increment and decrement). The idea
846 // being that they would support an extra Emition interface that
847 // does not leave a result on the stack.
850 public abstract class ExpressionStatement : Expression {
853 // Requests the expression to be emitted in a `statement'
854 // context. This means that no new value is left on the
855 // stack after invoking this method (constrasted with
856 // Emit that will always leave a value on the stack).
858 public abstract void EmitStatement (EmitContext ec);
862 // This kind of cast is used to encapsulate the child
863 // whose type is child.Type into an expression that is
864 // reported to return "return_type". This is used to encapsulate
865 // expressions which have compatible types, but need to be dealt
866 // at higher levels with.
868 // For example, a "byte" expression could be encapsulated in one
869 // of these as an "unsigned int". The type for the expression
870 // would be "unsigned int".
874 public class EmptyCast : Expression {
875 protected Expression child;
877 public EmptyCast (Expression child, Type return_type)
879 ExprClass = child.ExprClass;
884 public override Expression Resolve (TypeContainer tc)
886 // This should never be invoked, we are born in fully
887 // initialized state.
892 public override void Emit (EmitContext ec)
899 // This kind of cast is used to encapsulate Value Types in objects.
901 // The effect of it is to box the value type emitted by the previous
904 public class BoxedCast : EmptyCast {
906 public BoxedCast (Expression expr, Type target_type)
907 : base (expr, target_type)
911 public override Expression Resolve (TypeContainer tc)
913 // This should never be invoked, we are born in fully
914 // initialized state.
919 public override void Emit (EmitContext ec)
922 ec.ig.Emit (OpCodes.Box, child.Type);
927 // This kind of cast is used to encapsulate a child expression
928 // that can be trivially converted to a target type using one or
929 // two opcodes. The opcodes are passed as arguments.
931 public class OpcodeCast : EmptyCast {
935 public OpcodeCast (Expression child, Type return_type, OpCode op)
936 : base (child, return_type)
940 second_valid = false;
943 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
944 : base (child, return_type)
952 public override Expression Resolve (TypeContainer tc)
954 // This should never be invoked, we are born in fully
955 // initialized state.
960 public override void Emit (EmitContext ec)
972 // Unary expressions.
976 // Unary implements unary expressions. It derives from
977 // ExpressionStatement becuase the pre/post increment/decrement
978 // operators can be used in a statement context.
980 public class Unary : ExpressionStatement {
981 public enum Operator {
982 Addition, Subtraction, Negate, BitComplement,
983 Indirection, AddressOf, PreIncrement,
984 PreDecrement, PostIncrement, PostDecrement
993 public Unary (Operator op, Expression expr, Location loc)
1000 public Expression Expr {
1010 public Operator Oper {
1021 // Returns a stringified representation of the Operator
1026 case Operator.Addition:
1028 case Operator.Subtraction:
1030 case Operator.Negate:
1032 case Operator.BitComplement:
1034 case Operator.AddressOf:
1036 case Operator.Indirection:
1038 case Operator.PreIncrement : case Operator.PostIncrement :
1040 case Operator.PreDecrement : case Operator.PostDecrement :
1044 return oper.ToString ();
1047 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1049 if (expr.Type == target_type)
1052 return ConvertImplicit (tc, expr, target_type);
1055 void report23 (Report r, Type t)
1057 r.Error (23, "Operator " + OperName () + " cannot be applied to operand of type `" +
1058 TypeManager.CSharpName (t) + "'");
1062 // Returns whether an object of type `t' can be incremented
1063 // or decremented with add/sub (ie, basically whether we can
1064 // use pre-post incr-decr operations on it, but it is not a
1065 // System.Decimal, which we test elsewhere)
1067 static bool IsIncrementableNumber (Type t)
1069 return (t == TypeManager.sbyte_type) ||
1070 (t == TypeManager.byte_type) ||
1071 (t == TypeManager.short_type) ||
1072 (t == TypeManager.ushort_type) ||
1073 (t == TypeManager.int32_type) ||
1074 (t == TypeManager.uint32_type) ||
1075 (t == TypeManager.int64_type) ||
1076 (t == TypeManager.uint64_type) ||
1077 (t == TypeManager.char_type) ||
1078 (t.IsSubclassOf (TypeManager.enum_type)) ||
1079 (t == TypeManager.float_type) ||
1080 (t == TypeManager.double_type);
1083 Expression ResolveOperator (TypeContainer tc)
1085 Type expr_type = expr.Type;
1088 // Step 1: Perform Operator Overload location
1093 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1094 op_name = "op_Increment";
1095 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1096 op_name = "op_Decrement";
1098 op_name = "op_" + oper;
1100 mg = MemberLookup (tc, expr_type, op_name, false);
1103 Arguments = new ArrayList ();
1104 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1106 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg, Arguments, location);
1107 if (method != null) {
1108 MethodInfo mi = (MethodInfo) method;
1110 type = mi.ReturnType;
1116 // Step 2: Default operations on CLI native types.
1119 // Only perform numeric promotions on:
1122 if (expr_type == null)
1125 if (oper == Operator.Negate){
1126 if (expr_type != TypeManager.bool_type) {
1127 report23 (tc.RootContext.Report, expr.Type);
1131 type = TypeManager.bool_type;
1135 if (oper == Operator.BitComplement) {
1136 if (!((expr_type == TypeManager.int32_type) ||
1137 (expr_type == TypeManager.uint32_type) ||
1138 (expr_type == TypeManager.int64_type) ||
1139 (expr_type == TypeManager.uint64_type) ||
1140 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1141 report23 (tc.RootContext.Report, expr.Type);
1148 if (oper == Operator.Addition) {
1150 // A plus in front of something is just a no-op, so return the child.
1156 // Deals with -literals
1157 // int operator- (int x)
1158 // long operator- (long x)
1159 // float operator- (float f)
1160 // double operator- (double d)
1161 // decimal operator- (decimal d)
1163 if (oper == Operator.Subtraction){
1165 // Fold a "- Constant" into a negative constant
1168 Expression e = null;
1171 // Is this a constant?
1173 if (expr is IntLiteral)
1174 e = new IntLiteral (-((IntLiteral) expr).Value);
1175 else if (expr is LongLiteral)
1176 e = new LongLiteral (-((LongLiteral) expr).Value);
1177 else if (expr is FloatLiteral)
1178 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1179 else if (expr is DoubleLiteral)
1180 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1181 else if (expr is DecimalLiteral)
1182 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1190 // Not a constant we can optimize, perform numeric
1191 // promotions to int, long, double.
1194 // The following is inneficient, because we call
1195 // ConvertImplicit too many times.
1197 // It is also not clear if we should convert to Float
1198 // or Double initially.
1200 if (expr_type == TypeManager.uint32_type){
1202 // FIXME: handle exception to this rule that
1203 // permits the int value -2147483648 (-2^31) to
1204 // bt written as a decimal interger literal
1206 type = TypeManager.int64_type;
1207 expr = ConvertImplicit (tc, expr, type);
1211 if (expr_type == TypeManager.uint64_type){
1213 // FIXME: Handle exception of `long value'
1214 // -92233720368547758087 (-2^63) to be written as
1215 // decimal integer literal.
1217 report23 (tc.RootContext.Report, expr_type);
1221 e = ConvertImplicit (tc, expr, TypeManager.int32_type);
1228 e = ConvertImplicit (tc, expr, TypeManager.int64_type);
1235 e = ConvertImplicit (tc, expr, TypeManager.double_type);
1242 report23 (tc.RootContext.Report, expr_type);
1247 // The operand of the prefix/postfix increment decrement operators
1248 // should be an expression that is classified as a variable,
1249 // a property access or an indexer access
1251 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1252 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1253 if (expr.ExprClass == ExprClass.Variable){
1254 if (IsIncrementableNumber (expr_type) ||
1255 expr_type == TypeManager.decimal_type){
1259 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1261 // FIXME: Verify that we have both get and set methods
1263 throw new Exception ("Implement me");
1264 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1266 // FIXME: Verify that we have both get and set methods
1268 throw new Exception ("Implement me");
1270 report118 (tc, expr, "variable, indexer or property access");
1274 if (oper == Operator.AddressOf){
1275 if (expr.ExprClass != ExprClass.Variable){
1276 Error (tc, 211, "Cannot take the address of non-variables");
1279 type = Type.GetType (expr.Type.ToString () + "*");
1282 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1283 TypeManager.CSharpName (expr_type) + "'");
1288 public override Expression Resolve (TypeContainer tc)
1290 expr = expr.Resolve (tc);
1295 return ResolveOperator (tc);
1298 public override void Emit (EmitContext ec)
1300 ILGenerator ig = ec.ig;
1301 Type expr_type = expr.Type;
1303 if (method != null) {
1305 // Note that operators are static anyway
1307 if (Arguments != null)
1308 Invocation.EmitArguments (ec, method, Arguments);
1311 // Post increment/decrement operations need a copy at this
1314 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1315 ig.Emit (OpCodes.Dup);
1318 ig.Emit (OpCodes.Call, (MethodInfo) method);
1321 // Pre Increment and Decrement operators
1323 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1324 ig.Emit (OpCodes.Dup);
1328 // Increment and Decrement should store the result
1330 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1331 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1332 ((LValue) expr).Store (ec);
1338 case Operator.Addition:
1339 throw new Exception ("This should be caught by Resolve");
1341 case Operator.Subtraction:
1343 ig.Emit (OpCodes.Neg);
1346 case Operator.Negate:
1348 ig.Emit (OpCodes.Ldc_I4_0);
1349 ig.Emit (OpCodes.Ceq);
1352 case Operator.BitComplement:
1354 ig.Emit (OpCodes.Not);
1357 case Operator.AddressOf:
1358 ((LValue)expr).AddressOf (ec);
1361 case Operator.Indirection:
1362 throw new Exception ("Not implemented yet");
1364 case Operator.PreIncrement:
1365 case Operator.PreDecrement:
1366 if (expr.ExprClass == ExprClass.Variable){
1368 // Resolve already verified that it is an "incrementable"
1371 ig.Emit (OpCodes.Ldc_I4_1);
1373 if (oper == Operator.PreDecrement)
1374 ig.Emit (OpCodes.Sub);
1376 ig.Emit (OpCodes.Add);
1377 ig.Emit (OpCodes.Dup);
1378 ((LValue) expr).Store (ec);
1380 throw new Exception ("Handle Indexers and Properties here");
1384 case Operator.PostIncrement:
1385 case Operator.PostDecrement:
1386 if (expr.ExprClass == ExprClass.Variable){
1388 // Resolve already verified that it is an "incrementable"
1391 ig.Emit (OpCodes.Dup);
1392 ig.Emit (OpCodes.Ldc_I4_1);
1394 if (oper == Operator.PostDecrement)
1395 ig.Emit (OpCodes.Sub);
1397 ig.Emit (OpCodes.Add);
1398 ((LValue) expr).Store (ec);
1400 throw new Exception ("Handle Indexers and Properties here");
1405 throw new Exception ("This should not happen: Operator = "
1406 + oper.ToString ());
1411 public override void EmitStatement (EmitContext ec)
1414 // FIXME: we should rewrite this code to generate
1415 // better code for ++ and -- as we know we wont need
1416 // the values on the stack
1419 ec.ig.Emit (OpCodes.Pop);
1423 public class Probe : Expression {
1424 public readonly string ProbeType;
1425 public readonly Operator Oper;
1429 public enum Operator {
1433 public Probe (Operator oper, Expression expr, string probe_type)
1436 ProbeType = probe_type;
1440 public Expression Expr {
1446 public override Expression Resolve (TypeContainer tc)
1448 probe_type = tc.LookupType (ProbeType, false);
1450 if (probe_type == null)
1453 expr = expr.Resolve (tc);
1455 type = TypeManager.bool_type;
1456 eclass = ExprClass.Value;
1461 public override void Emit (EmitContext ec)
1465 if (Oper == Operator.Is){
1466 ec.ig.Emit (OpCodes.Isinst, probe_type);
1468 throw new Exception ("Implement as");
1474 // This represents a typecast in the source language.
1476 // FIXME: Cast expressions have an unusual set of parsing
1477 // rules, we need to figure those out.
1479 public class Cast : Expression {
1483 public Cast (string cast_type, Expression expr)
1485 this.target_type = cast_type;
1489 public string TargetType {
1495 public Expression Expr {
1504 public override Expression Resolve (TypeContainer tc)
1506 expr = expr.Resolve (tc);
1510 type = tc.LookupType (target_type, false);
1511 eclass = ExprClass.Value;
1516 expr = ConvertExplicit (tc, expr, type);
1521 public override void Emit (EmitContext ec)
1524 // This one will never happen
1526 throw new Exception ("Should not happen");
1530 public class Binary : Expression {
1531 public enum Operator {
1532 Multiply, Division, Modulus,
1533 Addition, Subtraction,
1534 LeftShift, RightShift,
1535 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1536 Equality, Inequality,
1545 Expression left, right;
1547 ArrayList Arguments;
1551 public Binary (Operator oper, Expression left, Expression right, Location loc)
1556 this.location = loc;
1559 public Operator Oper {
1568 public Expression Left {
1577 public Expression Right {
1588 // Returns a stringified representation of the Operator
1593 case Operator.Multiply:
1595 case Operator.Division:
1597 case Operator.Modulus:
1599 case Operator.Addition:
1601 case Operator.Subtraction:
1603 case Operator.LeftShift:
1605 case Operator.RightShift:
1607 case Operator.LessThan:
1609 case Operator.GreaterThan:
1611 case Operator.LessThanOrEqual:
1613 case Operator.GreaterThanOrEqual:
1615 case Operator.Equality:
1617 case Operator.Inequality:
1619 case Operator.BitwiseAnd:
1621 case Operator.BitwiseOr:
1623 case Operator.ExclusiveOr:
1625 case Operator.LogicalOr:
1627 case Operator.LogicalAnd:
1631 return oper.ToString ();
1634 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1636 if (expr.Type == target_type)
1639 return ConvertImplicit (tc, expr, target_type);
1643 // Note that handling the case l == Decimal || r == Decimal
1644 // is taken care of by the Step 1 Operator Overload resolution.
1646 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1648 if (l == TypeManager.double_type || r == TypeManager.double_type){
1650 // If either operand is of type double, the other operand is
1651 // conveted to type double.
1653 if (r != TypeManager.double_type)
1654 right = ConvertImplicit (tc, right, TypeManager.double_type);
1655 if (l != TypeManager.double_type)
1656 left = ConvertImplicit (tc, left, TypeManager.double_type);
1658 type = TypeManager.double_type;
1659 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1661 // if either operand is of type float, th eother operand is
1662 // converd to type float.
1664 if (r != TypeManager.double_type)
1665 right = ConvertImplicit (tc, right, TypeManager.float_type);
1666 if (l != TypeManager.double_type)
1667 left = ConvertImplicit (tc, left, TypeManager.float_type);
1668 type = TypeManager.float_type;
1669 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1671 // If either operand is of type ulong, the other operand is
1672 // converted to type ulong. or an error ocurrs if the other
1673 // operand is of type sbyte, short, int or long
1677 if (l == TypeManager.uint64_type)
1679 else if (r == TypeManager.uint64_type)
1682 if ((other == TypeManager.sbyte_type) ||
1683 (other == TypeManager.short_type) ||
1684 (other == TypeManager.int32_type) ||
1685 (other == TypeManager.int64_type)){
1686 string oper = OperName ();
1688 Error (tc, 34, "Operator `" + OperName ()
1689 + "' is ambiguous on operands of type `"
1690 + TypeManager.CSharpName (l) + "' "
1691 + "and `" + TypeManager.CSharpName (r)
1694 type = TypeManager.uint64_type;
1695 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1697 // If either operand is of type long, the other operand is converted
1700 if (l != TypeManager.int64_type)
1701 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1702 if (r != TypeManager.int64_type)
1703 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1705 type = TypeManager.int64_type;
1706 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1708 // If either operand is of type uint, and the other
1709 // operand is of type sbyte, short or int, othe operands are
1710 // converted to type long.
1714 if (l == TypeManager.uint32_type)
1716 else if (r == TypeManager.uint32_type)
1719 if ((other == TypeManager.sbyte_type) ||
1720 (other == TypeManager.short_type) ||
1721 (other == TypeManager.int32_type)){
1722 left = ForceConversion (tc, left, TypeManager.int64_type);
1723 right = ForceConversion (tc, right, TypeManager.int64_type);
1724 type = TypeManager.int64_type;
1727 // if either operand is of type uint, the other
1728 // operand is converd to type uint
1730 left = ForceConversion (tc, left, TypeManager.uint32_type);
1731 right = ForceConversion (tc, left, TypeManager.uint32_type);
1732 type = TypeManager.uint32_type;
1734 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1735 if (l != TypeManager.decimal_type)
1736 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1737 if (r != TypeManager.decimal_type)
1738 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1740 type = TypeManager.decimal_type;
1742 left = ForceConversion (tc, left, TypeManager.int32_type);
1743 right = ForceConversion (tc, right, TypeManager.int32_type);
1744 type = TypeManager.int32_type;
1748 void error19 (TypeContainer tc)
1751 "Operator " + OperName () + " cannot be applied to operands of type `" +
1752 TypeManager.CSharpName (left.Type) + "' and `" +
1753 TypeManager.CSharpName (right.Type) + "'");
1757 Expression CheckShiftArguments (TypeContainer tc)
1761 Type r = right.Type;
1763 e = ForceConversion (tc, right, TypeManager.int32_type);
1770 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1771 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1772 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1773 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1782 Expression ResolveOperator (TypeContainer tc)
1785 Type r = right.Type;
1788 // Step 1: Perform Operator Overload location
1790 Expression left_expr, right_expr;
1792 string op = "op_" + oper;
1794 left_expr = MemberLookup (tc, l, op, false);
1796 right_expr = MemberLookup (tc, r, op, false);
1798 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1800 Arguments = new ArrayList ();
1801 Arguments.Add (new Argument (left, Argument.AType.Expression));
1802 Arguments.Add (new Argument (right, Argument.AType.Expression));
1804 if (union != null) {
1805 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1806 if (method != null) {
1807 MethodInfo mi = (MethodInfo) method;
1809 type = mi.ReturnType;
1815 // Step 2: Default operations on CLI native types.
1818 // Only perform numeric promotions on:
1819 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1821 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1822 return CheckShiftArguments (tc);
1823 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1825 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1828 DoNumericPromotions (tc, l, r);
1830 if (left == null || right == null)
1833 if (oper == Operator.BitwiseAnd ||
1834 oper == Operator.BitwiseOr ||
1835 oper == Operator.ExclusiveOr){
1836 if (!((l == TypeManager.int32_type) ||
1837 (l == TypeManager.uint32_type) ||
1838 (l == TypeManager.int64_type) ||
1839 (l == TypeManager.uint64_type))){
1845 if (oper == Operator.Equality ||
1846 oper == Operator.Inequality ||
1847 oper == Operator.LessThanOrEqual ||
1848 oper == Operator.LessThan ||
1849 oper == Operator.GreaterThanOrEqual ||
1850 oper == Operator.GreaterThan){
1851 type = TypeManager.bool_type;
1857 public override Expression Resolve (TypeContainer tc)
1859 left = left.Resolve (tc);
1860 right = right.Resolve (tc);
1862 if (left == null || right == null)
1865 return ResolveOperator (tc);
1868 public bool IsBranchable ()
1870 if (oper == Operator.Equality ||
1871 oper == Operator.Inequality ||
1872 oper == Operator.LessThan ||
1873 oper == Operator.GreaterThan ||
1874 oper == Operator.LessThanOrEqual ||
1875 oper == Operator.GreaterThanOrEqual){
1882 // This entry point is used by routines that might want
1883 // to emit a brfalse/brtrue after an expression, and instead
1884 // they could use a more compact notation.
1886 // Typically the code would generate l.emit/r.emit, followed
1887 // by the comparission and then a brtrue/brfalse. The comparissions
1888 // are sometimes inneficient (there are not as complete as the branches
1889 // look for the hacks in Emit using double ceqs).
1891 // So for those cases we provide EmitBranchable that can emit the
1892 // branch with the test
1894 public void EmitBranchable (EmitContext ec, int target)
1897 bool close_target = false;
1903 case Operator.Equality:
1905 opcode = OpCodes.Beq_S;
1907 opcode = OpCodes.Beq;
1910 case Operator.Inequality:
1912 opcode = OpCodes.Bne_Un_S;
1914 opcode = OpCodes.Bne_Un;
1917 case Operator.LessThan:
1919 opcode = OpCodes.Blt_S;
1921 opcode = OpCodes.Blt;
1924 case Operator.GreaterThan:
1926 opcode = OpCodes.Bgt_S;
1928 opcode = OpCodes.Bgt;
1931 case Operator.LessThanOrEqual:
1933 opcode = OpCodes.Ble_S;
1935 opcode = OpCodes.Ble;
1938 case Operator.GreaterThanOrEqual:
1940 opcode = OpCodes.Bge_S;
1942 opcode = OpCodes.Ble;
1946 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1947 + oper.ToString ());
1950 ec.ig.Emit (opcode, target);
1953 public override void Emit (EmitContext ec)
1955 ILGenerator ig = ec.ig;
1957 Type r = right.Type;
1960 if (method != null) {
1962 // Note that operators are static anyway
1964 if (Arguments != null)
1965 Invocation.EmitArguments (ec, method, Arguments);
1967 if (method is MethodInfo)
1968 ig.Emit (OpCodes.Call, (MethodInfo) method);
1970 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1979 case Operator.Multiply:
1981 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1982 opcode = OpCodes.Mul_Ovf;
1983 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1984 opcode = OpCodes.Mul_Ovf_Un;
1986 opcode = OpCodes.Mul;
1988 opcode = OpCodes.Mul;
1992 case Operator.Division:
1993 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1994 opcode = OpCodes.Div_Un;
1996 opcode = OpCodes.Div;
1999 case Operator.Modulus:
2000 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2001 opcode = OpCodes.Rem_Un;
2003 opcode = OpCodes.Rem;
2006 case Operator.Addition:
2008 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2009 opcode = OpCodes.Add_Ovf;
2010 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2011 opcode = OpCodes.Add_Ovf_Un;
2013 opcode = OpCodes.Mul;
2015 opcode = OpCodes.Add;
2018 case Operator.Subtraction:
2020 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2021 opcode = OpCodes.Sub_Ovf;
2022 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2023 opcode = OpCodes.Sub_Ovf_Un;
2025 opcode = OpCodes.Sub;
2027 opcode = OpCodes.Sub;
2030 case Operator.RightShift:
2031 opcode = OpCodes.Shr;
2034 case Operator.LeftShift:
2035 opcode = OpCodes.Shl;
2038 case Operator.Equality:
2039 opcode = OpCodes.Ceq;
2042 case Operator.Inequality:
2043 ec.ig.Emit (OpCodes.Ceq);
2044 ec.ig.Emit (OpCodes.Ldc_I4_0);
2046 opcode = OpCodes.Ceq;
2049 case Operator.LessThan:
2050 opcode = OpCodes.Clt;
2053 case Operator.GreaterThan:
2054 opcode = OpCodes.Cgt;
2057 case Operator.LessThanOrEqual:
2058 ec.ig.Emit (OpCodes.Cgt);
2059 ec.ig.Emit (OpCodes.Ldc_I4_0);
2061 opcode = OpCodes.Ceq;
2064 case Operator.GreaterThanOrEqual:
2065 ec.ig.Emit (OpCodes.Clt);
2066 ec.ig.Emit (OpCodes.Ldc_I4_1);
2068 opcode = OpCodes.Sub;
2071 case Operator.LogicalOr:
2072 case Operator.BitwiseOr:
2073 opcode = OpCodes.Or;
2076 case Operator.LogicalAnd:
2077 case Operator.BitwiseAnd:
2078 opcode = OpCodes.And;
2081 case Operator.ExclusiveOr:
2082 opcode = OpCodes.Xor;
2086 throw new Exception ("This should not happen: Operator = "
2087 + oper.ToString ());
2094 public class Conditional : Expression {
2095 Expression expr, trueExpr, falseExpr;
2097 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
2100 this.trueExpr = trueExpr;
2101 this.falseExpr = falseExpr;
2104 public Expression Expr {
2110 public Expression TrueExpr {
2116 public Expression FalseExpr {
2122 public override Expression Resolve (TypeContainer tc)
2124 // FIXME: Implement;
2125 throw new Exception ("Unimplemented");
2129 public override void Emit (EmitContext ec)
2134 public class SimpleName : Expression {
2135 public readonly string Name;
2136 public readonly Location Location;
2138 public SimpleName (string name, Location l)
2145 // Checks whether we are trying to access an instance
2146 // property, method or field from a static body.
2148 Expression MemberStaticCheck (Report r, Expression e)
2150 if (e is FieldExpr){
2151 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2155 "An object reference is required " +
2156 "for the non-static field `"+Name+"'");
2159 } else if (e is MethodGroupExpr){
2160 // FIXME: Pending reorganization of MemberLookup
2161 // Basically at this point we should have the
2162 // best match already selected for us, and
2163 // we should only have to check a *single*
2164 // Method for its static on/off bit.
2166 } else if (e is PropertyExpr){
2167 if (!((PropertyExpr) e).IsStatic){
2169 "An object reference is required " +
2170 "for the non-static property access `"+
2180 // 7.5.2: Simple Names.
2182 // Local Variables and Parameters are handled at
2183 // parse time, so they never occur as SimpleNames.
2185 Expression ResolveSimpleName (TypeContainer tc)
2188 Report r = tc.RootContext.Report;
2190 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2194 else if (e is FieldExpr){
2195 FieldExpr fe = (FieldExpr) e;
2197 if (!fe.FieldInfo.IsStatic)
2198 fe.Instance = new This ();
2201 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2202 return MemberStaticCheck (r, e);
2208 // Do step 3 of the Simple Name resolution.
2210 // FIXME: implement me.
2212 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2219 // SimpleName needs to handle a multitude of cases:
2221 // simple_names and qualified_identifiers are placed on
2222 // the tree equally.
2224 public override Expression Resolve (TypeContainer tc)
2226 if (Name.IndexOf (".") != -1)
2227 return ResolveMemberAccess (tc, Name);
2229 return ResolveSimpleName (tc);
2232 public override void Emit (EmitContext ec)
2234 throw new Exception ("SimpleNames should be gone from the tree");
2239 // A simple interface that should be implemeneted by LValues
2241 public interface LValue {
2244 // The Store method should store the contents of the top
2245 // of the stack into the storage that is implemented by
2246 // the particular implementation of LValue
2248 void Store (EmitContext ec);
2251 // The AddressOf method should generate code that loads
2252 // the address of the LValue and leaves it on the stack
2254 void AddressOf (EmitContext ec);
2257 public class LocalVariableReference : Expression, LValue {
2258 public readonly string Name;
2259 public readonly Block Block;
2261 public LocalVariableReference (Block block, string name)
2265 eclass = ExprClass.Variable;
2268 public VariableInfo VariableInfo {
2270 return Block.GetVariableInfo (Name);
2274 public override Expression Resolve (TypeContainer tc)
2276 VariableInfo vi = Block.GetVariableInfo (Name);
2278 type = vi.VariableType;
2282 public override void Emit (EmitContext ec)
2284 VariableInfo vi = VariableInfo;
2285 ILGenerator ig = ec.ig;
2290 ig.Emit (OpCodes.Ldloc_0);
2294 ig.Emit (OpCodes.Ldloc_1);
2298 ig.Emit (OpCodes.Ldloc_2);
2302 ig.Emit (OpCodes.Ldloc_3);
2307 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2309 ig.Emit (OpCodes.Ldloc, idx);
2314 public void Store (EmitContext ec)
2316 ILGenerator ig = ec.ig;
2317 VariableInfo vi = VariableInfo;
2322 ig.Emit (OpCodes.Stloc_0);
2326 ig.Emit (OpCodes.Stloc_1);
2330 ig.Emit (OpCodes.Stloc_2);
2334 ig.Emit (OpCodes.Stloc_3);
2339 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2341 ig.Emit (OpCodes.Stloc, idx);
2346 public void AddressOf (EmitContext ec)
2348 VariableInfo vi = VariableInfo;
2352 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2354 ec.ig.Emit (OpCodes.Ldloca, idx);
2358 public class ParameterReference : Expression, LValue {
2359 public readonly Parameters Pars;
2360 public readonly String Name;
2361 public readonly int Idx;
2363 public ParameterReference (Parameters pars, int idx, string name)
2368 eclass = ExprClass.Variable;
2371 public override Expression Resolve (TypeContainer tc)
2373 Type [] types = Pars.GetParameterInfo (tc);
2380 public override void Emit (EmitContext ec)
2383 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
2385 ec.ig.Emit (OpCodes.Ldarg, Idx);
2388 public void Store (EmitContext ec)
2391 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
2393 ec.ig.Emit (OpCodes.Starg, Idx);
2397 public void AddressOf (EmitContext ec)
2400 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
2402 ec.ig.Emit (OpCodes.Ldarga, Idx);
2407 // Used for arguments to New(), Invocation()
2409 public class Argument {
2416 public readonly AType Type;
2419 public Argument (Expression expr, AType type)
2425 public Expression Expr {
2435 public bool Resolve (TypeContainer tc)
2437 expr = expr.Resolve (tc);
2439 return expr != null;
2442 public void Emit (EmitContext ec)
2449 // Invocation of methods or delegates.
2451 public class Invocation : ExpressionStatement {
2452 public readonly ArrayList Arguments;
2453 public readonly Location Location;
2456 MethodBase method = null;
2458 static Hashtable method_parameter_cache;
2460 static Invocation ()
2462 method_parameter_cache = new Hashtable ();
2466 // arguments is an ArrayList, but we do not want to typecast,
2467 // as it might be null.
2469 // FIXME: only allow expr to be a method invocation or a
2470 // delegate invocation (7.5.5)
2472 public Invocation (Expression expr, ArrayList arguments, Location l)
2475 Arguments = arguments;
2479 public Expression Expr {
2486 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2487 /// compatible, and if so, how good is the match (in terms of
2488 /// "better conversions" (7.4.2.3).
2490 /// 0 is the best possible match.
2491 /// -1 represents a type mismatch.
2492 /// -2 represents a ref/out mismatch.
2494 static int Badness (Argument a, Type t)
2496 Expression argument_expr = a.Expr;
2497 Type argument_type = argument_expr.Type;
2499 if (argument_type == null){
2500 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2503 if (t == argument_type)
2507 // Now probe whether an implicit constant expression conversion
2510 // An implicit constant expression conversion permits the following
2513 // * A constant-expression of type `int' can be converted to type
2514 // sbyte, byute, short, ushort, uint, ulong provided the value of
2515 // of the expression is withing the range of the destination type.
2517 // * A constant-expression of type long can be converted to type
2518 // ulong, provided the value of the constant expression is not negative
2520 // FIXME: Note that this assumes that constant folding has
2521 // taken place. We dont do constant folding yet.
2524 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2525 IntLiteral ei = (IntLiteral) argument_expr;
2526 int value = ei.Value;
2528 if (t == TypeManager.sbyte_type){
2529 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2531 } else if (t == TypeManager.byte_type){
2532 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2534 } else if (t == TypeManager.short_type){
2535 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2537 } else if (t == TypeManager.ushort_type){
2538 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2540 } else if (t == TypeManager.uint32_type){
2542 // we can optimize this case: a positive int32
2543 // always fits on a uint32
2547 } else if (t == TypeManager.uint64_type){
2549 // we can optimize this case: a positive int32
2550 // always fits on a uint64
2555 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2556 LongLiteral ll = (LongLiteral) argument_expr;
2558 if (t == TypeManager.uint64_type)
2563 // FIXME: Implement user-defined implicit conversions here.
2564 // FIXME: Implement better conversion here.
2570 // Returns the Parameters (a ParameterData interface) for the
2573 static ParameterData GetParameterData (MethodBase mb)
2575 object pd = method_parameter_cache [mb];
2578 return (ParameterData) pd;
2580 if (mb is MethodBuilder || mb is ConstructorBuilder){
2581 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2583 InternalParameters ip = mc.ParameterInfo;
2584 method_parameter_cache [mb] = ip;
2586 return (ParameterData) ip;
2588 ParameterInfo [] pi = mb.GetParameters ();
2589 ReflectionParameters rp = new ReflectionParameters (pi);
2590 method_parameter_cache [mb] = rp;
2592 return (ParameterData) rp;
2596 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2598 // Locate user-defined implicit operators
2602 mg = MemberLookup (tc, to, "op_Implicit", false);
2605 MethodGroupExpr me = (MethodGroupExpr) mg;
2607 for (int i = me.Methods.Length; i > 0;) {
2609 MethodBase mb = me.Methods [i];
2610 ParameterData pd = GetParameterData (mb);
2612 if (from == pd.ParameterType (0))
2617 mg = MemberLookup (tc, from, "op_Implicit", false);
2620 MethodGroupExpr me = (MethodGroupExpr) mg;
2622 for (int i = me.Methods.Length; i > 0;) {
2624 MethodBase mb = me.Methods [i];
2625 MethodInfo mi = (MethodInfo) mb;
2627 if (mi.ReturnType == to)
2636 // Determines "better conversion" as specified in 7.4.2.3
2637 // Returns : 1 if a->p is better
2638 // 0 if a->q or neither is better
2640 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2643 Type argument_type = a.Expr.Type;
2644 Expression argument_expr = a.Expr;
2646 if (argument_type == null)
2647 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2652 if (argument_type == p)
2655 if (argument_type == q)
2659 // Now probe whether an implicit constant expression conversion
2662 // An implicit constant expression conversion permits the following
2665 // * A constant-expression of type `int' can be converted to type
2666 // sbyte, byute, short, ushort, uint, ulong provided the value of
2667 // of the expression is withing the range of the destination type.
2669 // * A constant-expression of type long can be converted to type
2670 // ulong, provided the value of the constant expression is not negative
2672 // FIXME: Note that this assumes that constant folding has
2673 // taken place. We dont do constant folding yet.
2676 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2677 IntLiteral ei = (IntLiteral) argument_expr;
2678 int value = ei.Value;
2680 if (p == TypeManager.sbyte_type){
2681 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2683 } else if (p == TypeManager.byte_type){
2684 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2686 } else if (p == TypeManager.short_type){
2687 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2689 } else if (p == TypeManager.ushort_type){
2690 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2692 } else if (p == TypeManager.uint32_type){
2694 // we can optimize this case: a positive int32
2695 // always fits on a uint32
2699 } else if (p == TypeManager.uint64_type){
2701 // we can optimize this case: a positive int32
2702 // always fits on a uint64
2707 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2708 LongLiteral ll = (LongLiteral) argument_expr;
2710 if (p == TypeManager.uint64_type){
2720 tmp = ConvertImplicit (tc, argument_expr, p);
2729 if (ConversionExists (tc, p, q) == true &&
2730 ConversionExists (tc, q, p) == false)
2733 if (p == TypeManager.sbyte_type)
2734 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2735 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2738 if (p == TypeManager.short_type)
2739 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2740 q == TypeManager.uint64_type)
2743 if (p == TypeManager.int32_type)
2744 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2747 if (p == TypeManager.int64_type)
2748 if (q == TypeManager.uint64_type)
2755 // Determines "Better function" and returns an integer indicating :
2756 // 0 if candidate ain't better
2757 // 1 if candidate is better than the current best match
2759 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2761 ParameterData candidate_pd = GetParameterData (candidate);
2762 ParameterData best_pd;
2768 argument_count = args.Count;
2770 if (candidate_pd.Count == 0 && argument_count == 0)
2774 if (candidate_pd.Count == argument_count) {
2776 for (int j = argument_count; j > 0;) {
2779 Argument a = (Argument) args [j];
2781 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2798 best_pd = GetParameterData (best);
2800 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2801 int rating1 = 0, rating2 = 0;
2803 for (int j = argument_count; j > 0;) {
2807 Argument a = (Argument) args [j];
2809 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2810 best_pd.ParameterType (j));
2811 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2812 candidate_pd.ParameterType (j));
2818 if (rating1 > rating2)
2827 public static string FullMethodDesc (MethodBase mb)
2829 StringBuilder sb = new StringBuilder (mb.Name);
2830 ParameterData pd = GetParameterData (mb);
2833 for (int i = pd.Count; i > 0;) {
2835 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2841 return sb.ToString ();
2844 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2847 if (mg1 != null || mg2 != null) {
2849 MethodGroupExpr left_set = null, right_set = null;
2850 int length1 = 0, length2 = 0;
2853 left_set = (MethodGroupExpr) mg1;
2854 length1 = left_set.Methods.Length;
2858 right_set = (MethodGroupExpr) mg2;
2859 length2 = right_set.Methods.Length;
2862 MemberInfo [] miset = new MemberInfo [length1 + length2];
2863 if (left_set != null)
2864 left_set.Methods.CopyTo (miset, 0);
2865 if (right_set != null)
2866 right_set.Methods.CopyTo (miset, length1);
2868 MethodGroupExpr union = new MethodGroupExpr (miset);
2879 // Find the Applicable Function Members (7.4.2.1)
2881 // me: Method Group expression with the members to select.
2882 // it might contain constructors or methods (or anything
2883 // that maps to a method).
2885 // Arguments: ArrayList containing resolved Argument objects.
2887 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2888 // that is the best match of me on Arguments.
2891 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2892 ArrayList Arguments, Location loc)
2894 ArrayList afm = new ArrayList ();
2895 int best_match_idx = -1;
2896 MethodBase method = null;
2899 for (int i = me.Methods.Length; i > 0; ){
2901 MethodBase candidate = me.Methods [i];
2904 x = BetterFunction (tc, Arguments, candidate, method);
2910 method = me.Methods [best_match_idx];
2914 if (Arguments == null)
2917 argument_count = Arguments.Count;
2921 // Now we see if we can at least find a method with the same number of arguments
2922 // and then try doing implicit conversion on the arguments
2923 if (best_match_idx == -1) {
2925 for (int i = me.Methods.Length; i > 0;) {
2927 MethodBase mb = me.Methods [i];
2928 pd = GetParameterData (mb);
2930 if (pd.Count == argument_count) {
2932 method = me.Methods [best_match_idx];
2943 // And now convert implicitly, each argument to the required type
2945 pd = GetParameterData (method);
2947 for (int j = argument_count; j > 0;) {
2949 Argument a = (Argument) Arguments [j];
2950 Expression a_expr = a.Expr;
2952 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
2955 Error (tc, 1502, loc,
2956 "The best overloaded match for method '" + FullMethodDesc (method) +
2957 "' has some invalid arguments");
2958 Error (tc, 1503, loc,
2959 "Argument " + (j+1) +
2960 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
2961 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
2966 // Update the argument with the implicit conversion
2976 public override Expression Resolve (TypeContainer tc)
2979 // First, resolve the expression that is used to
2980 // trigger the invocation
2982 this.expr = expr.Resolve (tc);
2983 if (this.expr == null)
2986 if (!(this.expr is MethodGroupExpr)){
2987 report118 (tc, this.expr, "method group");
2992 // Next, evaluate all the expressions in the argument list
2994 if (Arguments != null){
2995 for (int i = Arguments.Count; i > 0;){
2997 Argument a = (Argument) Arguments [i];
2999 if (!a.Resolve (tc))
3004 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
3006 if (method == null){
3007 Error (tc, -6, Location,
3008 "Could not find any applicable function for this argument list");
3012 if (method is MethodInfo)
3013 type = ((MethodInfo)method).ReturnType;
3018 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3022 if (Arguments != null)
3023 top = Arguments.Count;
3027 for (int i = 0; i < top; i++){
3028 Argument a = (Argument) Arguments [i];
3034 public override void Emit (EmitContext ec)
3036 bool is_static = method.IsStatic;
3039 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3042 // If this is ourselves, push "this"
3044 if (mg.InstanceExpression == null){
3045 ec.ig.Emit (OpCodes.Ldarg_0);
3048 // Push the instance expression
3050 mg.InstanceExpression.Emit (ec);
3054 if (Arguments != null)
3055 EmitArguments (ec, method, Arguments);
3058 if (method is MethodInfo)
3059 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3061 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3063 if (method is MethodInfo)
3064 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3066 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3070 public override void EmitStatement (EmitContext ec)
3075 // Pop the return value if there is one
3077 if (method is MethodInfo){
3078 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3079 ec.ig.Emit (OpCodes.Pop);
3084 public class New : ExpressionStatement {
3091 public readonly NType NewType;
3092 public readonly ArrayList Arguments;
3093 public readonly string RequestedType;
3094 // These are for the case when we have an array
3095 public readonly string Rank;
3096 public readonly ArrayList Indices;
3097 public readonly ArrayList Initializers;
3100 MethodBase method = null;
3102 public New (string requested_type, ArrayList arguments, Location loc)
3104 RequestedType = requested_type;
3105 Arguments = arguments;
3106 NewType = NType.Object;
3110 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3112 RequestedType = requested_type;
3115 Initializers = initializers;
3116 NewType = NType.Array;
3120 public override Expression Resolve (TypeContainer tc)
3122 type = tc.LookupType (RequestedType, false);
3129 ml = MemberLookup (tc, type, ".ctor", false,
3130 MemberTypes.Constructor, AllBindingsFlags);
3132 if (! (ml is MethodGroupExpr)){
3134 // FIXME: Find proper error
3136 report118 (tc, ml, "method group");
3140 if (Arguments != null){
3141 for (int i = Arguments.Count; i > 0;){
3143 Argument a = (Argument) Arguments [i];
3145 if (!a.Resolve (tc))
3150 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3152 if (method == null) {
3153 Error (tc, -6, Location,
3154 "New invocation: Can not find a constructor for this argument list");
3161 public override void Emit (EmitContext ec)
3163 Invocation.EmitArguments (ec, method, Arguments);
3164 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3167 public override void EmitStatement (EmitContext ec)
3170 ec.ig.Emit (OpCodes.Pop);
3175 // Represents the `this' construct
3177 public class This : Expression, LValue {
3178 public override Expression Resolve (TypeContainer tc)
3180 eclass = ExprClass.Variable;
3181 type = tc.TypeBuilder;
3184 // FIXME: Verify that this is only used in instance contexts.
3189 public override void Emit (EmitContext ec)
3191 ec.ig.Emit (OpCodes.Ldarg_0);
3194 public void Store (EmitContext ec)
3197 // Assignment to the "this" variable.
3199 // FIXME: Apparently this is a bug that we
3200 // must catch as `this' seems to be readonly ;-)
3202 ec.ig.Emit (OpCodes.Starg, 0);
3205 public void AddressOf (EmitContext ec)
3207 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3211 public class TypeOf : Expression {
3212 public readonly string QueriedType;
3214 public TypeOf (string queried_type)
3216 QueriedType = queried_type;
3219 public override Expression Resolve (TypeContainer tc)
3221 type = tc.LookupType (QueriedType, false);
3226 eclass = ExprClass.Type;
3230 public override void Emit (EmitContext ec)
3232 throw new Exception ("Implement me");
3233 // FIXME: Implement.
3237 public class SizeOf : Expression {
3238 public readonly string QueriedType;
3240 public SizeOf (string queried_type)
3242 this.QueriedType = queried_type;
3245 public override Expression Resolve (TypeContainer tc)
3247 // FIXME: Implement;
3248 throw new Exception ("Unimplemented");
3252 public override void Emit (EmitContext ec)
3254 throw new Exception ("Implement me");
3258 public class MemberAccess : Expression {
3259 public readonly string Identifier;
3261 Expression member_lookup;
3263 public MemberAccess (Expression expr, string id)
3269 public Expression Expr {
3275 public override Expression Resolve (TypeContainer tc)
3277 Expression new_expression = expr.Resolve (tc);
3279 if (new_expression == null)
3282 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
3284 if (member_lookup is MethodGroupExpr){
3285 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3288 // Bind the instance expression to it
3290 // FIXME: This is a horrible way of detecting if it is
3291 // an instance expression. Figure out how to fix this.
3294 if (expr is LocalVariableReference ||
3295 expr is ParameterReference ||
3297 mg.InstanceExpression = expr;
3299 return member_lookup;
3300 } else if (member_lookup is FieldExpr){
3301 FieldExpr fe = (FieldExpr) member_lookup;
3305 return member_lookup;
3308 // FIXME: This should generate the proper node
3309 // ie, for a Property Access, it should like call it
3312 return member_lookup;
3315 public override void Emit (EmitContext ec)
3317 throw new Exception ("Implement me");
3323 // Nodes of type Namespace are created during the semantic
3324 // analysis to resolve member_access/qualified_identifier/simple_name
3327 // They are born `resolved'.
3329 public class NamespaceExpr : Expression {
3330 public readonly string Name;
3332 public NamespaceExpr (string name)
3335 eclass = ExprClass.Namespace;
3338 public override Expression Resolve (TypeContainer tc)
3343 public override void Emit (EmitContext ec)
3345 throw new Exception ("Namespace expressions should never be emitted");
3350 // Fully resolved expression that evaluates to a type
3352 public class TypeExpr : Expression {
3353 public TypeExpr (Type t)
3356 eclass = ExprClass.Type;
3359 override public Expression Resolve (TypeContainer tc)
3364 override public void Emit (EmitContext ec)
3366 throw new Exception ("Implement me");
3371 // MethodGroup Expression.
3373 // This is a fully resolved expression that evaluates to a type
3375 public class MethodGroupExpr : Expression {
3376 public readonly MethodBase [] Methods;
3377 Expression instance_expression = null;
3379 public MethodGroupExpr (MemberInfo [] mi)
3381 Methods = new MethodBase [mi.Length];
3382 mi.CopyTo (Methods, 0);
3383 eclass = ExprClass.MethodGroup;
3387 // `A method group may have associated an instance expression'
3389 public Expression InstanceExpression {
3391 return instance_expression;
3395 instance_expression = value;
3399 override public Expression Resolve (TypeContainer tc)
3404 override public void Emit (EmitContext ec)
3406 throw new Exception ("This should never be reached");
3410 public class BuiltinTypeAccess : Expression {
3411 public readonly string AccessBase;
3412 public readonly string Method;
3414 public BuiltinTypeAccess (string type, string method)
3416 System.Console.WriteLine ("DUDE! This type should be fully resolved!");
3421 public override Expression Resolve (TypeContainer tc)
3423 // FIXME: Implement;
3424 throw new Exception ("Unimplemented");
3428 public override void Emit (EmitContext ec)
3430 throw new Exception ("Unimplemented");
3435 // Fully resolved expression that evaluates to a Field
3437 public class FieldExpr : Expression, LValue {
3438 public readonly FieldInfo FieldInfo;
3439 public Expression Instance;
3441 public FieldExpr (FieldInfo fi)
3444 eclass = ExprClass.Variable;
3445 type = fi.FieldType;
3448 override public Expression Resolve (TypeContainer tc)
3450 if (!FieldInfo.IsStatic){
3451 if (Instance == null){
3452 throw new Exception ("non-static FieldExpr without instance var\n" +
3453 "You have to assign the Instance variable\n" +
3454 "Of the FieldExpr to set this\n");
3457 Instance = Instance.Resolve (tc);
3458 if (Instance == null)
3465 override public void Emit (EmitContext ec)
3467 ILGenerator ig = ec.ig;
3469 if (FieldInfo.IsStatic)
3470 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3474 ig.Emit (OpCodes.Ldfld, FieldInfo);
3478 public void Store (EmitContext ec)
3480 if (FieldInfo.IsStatic)
3481 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3483 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3486 public void AddressOf (EmitContext ec)
3488 if (FieldInfo.IsStatic)
3489 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3492 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3498 // Fully resolved expression that evaluates to a Property
3500 public class PropertyExpr : Expression {
3501 public readonly PropertyInfo PropertyInfo;
3502 public readonly bool IsStatic;
3504 public PropertyExpr (PropertyInfo pi)
3507 eclass = ExprClass.PropertyAccess;
3510 MethodBase [] acc = pi.GetAccessors ();
3512 for (int i = 0; i < acc.Length; i++)
3513 if (acc [i].IsStatic)
3516 type = pi.PropertyType;
3519 override public Expression Resolve (TypeContainer tc)
3521 // We are born in resolved state.
3525 override public void Emit (EmitContext ec)
3527 // FIXME: Implement;
3528 throw new Exception ("Unimplemented");
3533 // Fully resolved expression that evaluates to a Property
3535 public class EventExpr : Expression {
3536 public readonly EventInfo EventInfo;
3538 public EventExpr (EventInfo ei)
3541 eclass = ExprClass.EventAccess;
3544 override public Expression Resolve (TypeContainer tc)
3546 // We are born in resolved state.
3550 override public void Emit (EmitContext ec)
3552 throw new Exception ("Implement me");
3553 // FIXME: Implement.
3557 public class CheckedExpr : Expression {
3559 public Expression Expr;
3561 public CheckedExpr (Expression e)
3566 public override Expression Resolve (TypeContainer tc)
3568 Expr = Expr.Resolve (tc);
3573 eclass = Expr.ExprClass;
3578 public override void Emit (EmitContext ec)
3580 bool last_check = ec.CheckState;
3582 ec.CheckState = true;
3584 ec.CheckState = last_check;
3589 public class UnCheckedExpr : Expression {
3591 public Expression Expr;
3593 public UnCheckedExpr (Expression e)
3598 public override Expression Resolve (TypeContainer tc)
3600 Expr = Expr.Resolve (tc);
3605 eclass = Expr.ExprClass;
3610 public override void Emit (EmitContext ec)
3612 bool last_check = ec.CheckState;
3614 ec.CheckState = false;
3616 ec.CheckState = last_check;
3621 public class ElementAccess : Expression {
3623 public readonly ArrayList Arguments;
3624 public readonly Expression Expr;
3626 public ElementAccess (Expression e, ArrayList e_list)
3632 public override Expression Resolve (TypeContainer tc)
3634 // FIXME: Implement;
3635 throw new Exception ("Unimplemented");
3639 public override void Emit (EmitContext ec)
3641 // FIXME : Implement !
3642 throw new Exception ("Unimplemented");
3647 public class BaseAccess : Expression {
3649 public enum BaseAccessType {
3654 public readonly BaseAccessType BAType;
3655 public readonly string Member;
3656 public readonly ArrayList Arguments;
3658 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3666 public override Expression Resolve (TypeContainer tc)
3668 // FIXME: Implement;
3669 throw new Exception ("Unimplemented");
3673 public override void Emit (EmitContext ec)
3675 throw new Exception ("Unimplemented");
3679 public class UserImplicitCast : Expression {
3684 ArrayList arguments;
3686 public UserImplicitCast (Expression source, Type target)
3688 this.source = source;
3689 this.target = target;
3692 public override Expression Resolve (TypeContainer tc)
3694 source = source.Resolve (tc);
3699 Expression mg1, mg2;
3700 MethodGroupExpr union;
3703 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3704 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3706 union = Invocation.MakeUnionSet (mg1, mg2);
3708 arguments = new ArrayList ();
3709 arguments.Add (new Argument (source, Argument.AType.Expression));
3711 if (union != null) {
3712 method = Invocation.OverloadResolve (tc, union, arguments,
3713 new Location ("FIXME", 1, 1));
3715 if (method != null) {
3716 mi = (MethodInfo) method;
3718 if (mi.ReturnType == target) {
3719 type = mi.ReturnType;
3725 if (target == TypeManager.bool_type) {
3727 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3728 mg2 = MemberLookup (tc, target, "op_True", false);
3730 union = Invocation.MakeUnionSet (mg1, mg2);
3735 method = Invocation.OverloadResolve (tc, union, arguments,
3736 new Location ("FIXME", 1, 1));
3738 if (method != null) {
3739 mi = (MethodInfo) method;
3741 if (mi.ReturnType == target) {
3742 type = mi.ReturnType;
3751 public static bool CanConvert (TypeContainer tc, Expression source, Type target)
3753 source = source.Resolve (tc);
3758 Expression mg1, mg2;
3760 ArrayList arguments;
3762 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3763 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3765 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3767 arguments = new ArrayList ();
3768 arguments.Add (new Argument (source, Argument.AType.Expression));
3770 if (union != null) {
3772 method = Invocation.OverloadResolve (tc, union, arguments,
3773 new Location ("FIXME", 1, 1));
3775 if (method != null) {
3776 MethodInfo mi = (MethodInfo) method;
3778 if (mi.ReturnType == target)
3783 // If we have a boolean type, we need to check for the True
3784 // and False operators too.
3786 if (target == TypeManager.bool_type) {
3788 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3789 mg2 = MemberLookup (tc, target, "op_True", false);
3791 union = Invocation.MakeUnionSet (mg1, mg2);
3796 method = Invocation.OverloadResolve (tc, union, arguments,
3797 new Location ("FIXME", 1, 1));
3798 if (method != null) {
3799 MethodInfo mi = (MethodInfo) method;
3801 if (mi.ReturnType == target)
3810 public override void Emit (EmitContext ec)
3812 ILGenerator ig = ec.ig;
3814 if (method != null) {
3816 // Note that operators are static anyway
3818 if (arguments != null)
3819 Invocation.EmitArguments (ec, method, arguments);
3821 if (method is MethodInfo)
3822 ig.Emit (OpCodes.Call, (MethodInfo) method);
3824 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3829 throw new Exception ("Implement me");