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 type = tc.LookupType (target_type, false);
1507 eclass = ExprClass.Value;
1512 expr = ConvertExplicit (tc, expr, type);
1517 public override void Emit (EmitContext ec)
1520 // This one will never happen
1522 throw new Exception ("Should not happen");
1526 public class Binary : Expression {
1527 public enum Operator {
1528 Multiply, Division, Modulus,
1529 Addition, Subtraction,
1530 LeftShift, RightShift,
1531 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1532 Equality, Inequality,
1541 Expression left, right;
1543 ArrayList Arguments;
1547 public Binary (Operator oper, Expression left, Expression right, Location loc)
1552 this.location = loc;
1555 public Operator Oper {
1564 public Expression Left {
1573 public Expression Right {
1584 // Returns a stringified representation of the Operator
1589 case Operator.Multiply:
1591 case Operator.Division:
1593 case Operator.Modulus:
1595 case Operator.Addition:
1597 case Operator.Subtraction:
1599 case Operator.LeftShift:
1601 case Operator.RightShift:
1603 case Operator.LessThan:
1605 case Operator.GreaterThan:
1607 case Operator.LessThanOrEqual:
1609 case Operator.GreaterThanOrEqual:
1611 case Operator.Equality:
1613 case Operator.Inequality:
1615 case Operator.BitwiseAnd:
1617 case Operator.BitwiseOr:
1619 case Operator.ExclusiveOr:
1621 case Operator.LogicalOr:
1623 case Operator.LogicalAnd:
1627 return oper.ToString ();
1630 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1632 if (expr.Type == target_type)
1635 return ConvertImplicit (tc, expr, target_type);
1639 // Note that handling the case l == Decimal || r == Decimal
1640 // is taken care of by the Step 1 Operator Overload resolution.
1642 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1644 if (l == TypeManager.double_type || r == TypeManager.double_type){
1646 // If either operand is of type double, the other operand is
1647 // conveted to type double.
1649 if (r != TypeManager.double_type)
1650 right = ConvertImplicit (tc, right, TypeManager.double_type);
1651 if (l != TypeManager.double_type)
1652 left = ConvertImplicit (tc, left, TypeManager.double_type);
1654 type = TypeManager.double_type;
1655 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1657 // if either operand is of type float, th eother operand is
1658 // converd to type float.
1660 if (r != TypeManager.double_type)
1661 right = ConvertImplicit (tc, right, TypeManager.float_type);
1662 if (l != TypeManager.double_type)
1663 left = ConvertImplicit (tc, left, TypeManager.float_type);
1664 type = TypeManager.float_type;
1665 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1667 // If either operand is of type ulong, the other operand is
1668 // converted to type ulong. or an error ocurrs if the other
1669 // operand is of type sbyte, short, int or long
1673 if (l == TypeManager.uint64_type)
1675 else if (r == TypeManager.uint64_type)
1678 if ((other == TypeManager.sbyte_type) ||
1679 (other == TypeManager.short_type) ||
1680 (other == TypeManager.int32_type) ||
1681 (other == TypeManager.int64_type)){
1682 string oper = OperName ();
1684 Error (tc, 34, "Operator `" + OperName ()
1685 + "' is ambiguous on operands of type `"
1686 + TypeManager.CSharpName (l) + "' "
1687 + "and `" + TypeManager.CSharpName (r)
1690 type = TypeManager.uint64_type;
1691 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1693 // If either operand is of type long, the other operand is converted
1696 if (l != TypeManager.int64_type)
1697 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1698 if (r != TypeManager.int64_type)
1699 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1701 type = TypeManager.int64_type;
1702 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1704 // If either operand is of type uint, and the other
1705 // operand is of type sbyte, short or int, othe operands are
1706 // converted to type long.
1710 if (l == TypeManager.uint32_type)
1712 else if (r == TypeManager.uint32_type)
1715 if ((other == TypeManager.sbyte_type) ||
1716 (other == TypeManager.short_type) ||
1717 (other == TypeManager.int32_type)){
1718 left = ForceConversion (tc, left, TypeManager.int64_type);
1719 right = ForceConversion (tc, right, TypeManager.int64_type);
1720 type = TypeManager.int64_type;
1723 // if either operand is of type uint, the other
1724 // operand is converd to type uint
1726 left = ForceConversion (tc, left, TypeManager.uint32_type);
1727 right = ForceConversion (tc, left, TypeManager.uint32_type);
1728 type = TypeManager.uint32_type;
1730 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1731 if (l != TypeManager.decimal_type)
1732 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1733 if (r != TypeManager.decimal_type)
1734 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1736 type = TypeManager.decimal_type;
1738 left = ForceConversion (tc, left, TypeManager.int32_type);
1739 right = ForceConversion (tc, right, TypeManager.int32_type);
1740 type = TypeManager.int32_type;
1744 void error19 (TypeContainer tc)
1747 "Operator " + OperName () + " cannot be applied to operands of type `" +
1748 TypeManager.CSharpName (left.Type) + "' and `" +
1749 TypeManager.CSharpName (right.Type) + "'");
1753 Expression CheckShiftArguments (TypeContainer tc)
1757 Type r = right.Type;
1759 e = ForceConversion (tc, right, TypeManager.int32_type);
1766 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1767 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1768 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1769 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1778 Expression ResolveOperator (TypeContainer tc)
1781 Type r = right.Type;
1784 // Step 1: Perform Operator Overload location
1786 Expression left_expr, right_expr;
1788 string op = "op_" + oper;
1790 left_expr = MemberLookup (tc, l, op, false);
1792 right_expr = MemberLookup (tc, r, op, false);
1794 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1796 Arguments = new ArrayList ();
1797 Arguments.Add (new Argument (left, Argument.AType.Expression));
1798 Arguments.Add (new Argument (right, Argument.AType.Expression));
1800 if (union != null) {
1801 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1802 if (method != null) {
1803 MethodInfo mi = (MethodInfo) method;
1805 type = mi.ReturnType;
1811 // Step 2: Default operations on CLI native types.
1814 // Only perform numeric promotions on:
1815 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1817 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1818 return CheckShiftArguments (tc);
1819 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1821 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1824 DoNumericPromotions (tc, l, r);
1826 if (left == null || right == null)
1829 if (oper == Operator.BitwiseAnd ||
1830 oper == Operator.BitwiseOr ||
1831 oper == Operator.ExclusiveOr){
1832 if (!((l == TypeManager.int32_type) ||
1833 (l == TypeManager.uint32_type) ||
1834 (l == TypeManager.int64_type) ||
1835 (l == TypeManager.uint64_type))){
1841 if (oper == Operator.Equality ||
1842 oper == Operator.Inequality ||
1843 oper == Operator.LessThanOrEqual ||
1844 oper == Operator.LessThan ||
1845 oper == Operator.GreaterThanOrEqual ||
1846 oper == Operator.GreaterThan){
1847 type = TypeManager.bool_type;
1853 public override Expression Resolve (TypeContainer tc)
1855 left = left.Resolve (tc);
1856 right = right.Resolve (tc);
1858 if (left == null || right == null)
1861 return ResolveOperator (tc);
1864 public bool IsBranchable ()
1866 if (oper == Operator.Equality ||
1867 oper == Operator.Inequality ||
1868 oper == Operator.LessThan ||
1869 oper == Operator.GreaterThan ||
1870 oper == Operator.LessThanOrEqual ||
1871 oper == Operator.GreaterThanOrEqual){
1878 // This entry point is used by routines that might want
1879 // to emit a brfalse/brtrue after an expression, and instead
1880 // they could use a more compact notation.
1882 // Typically the code would generate l.emit/r.emit, followed
1883 // by the comparission and then a brtrue/brfalse. The comparissions
1884 // are sometimes inneficient (there are not as complete as the branches
1885 // look for the hacks in Emit using double ceqs).
1887 // So for those cases we provide EmitBranchable that can emit the
1888 // branch with the test
1890 public void EmitBranchable (EmitContext ec, int target)
1893 bool close_target = false;
1899 case Operator.Equality:
1901 opcode = OpCodes.Beq_S;
1903 opcode = OpCodes.Beq;
1906 case Operator.Inequality:
1908 opcode = OpCodes.Bne_Un_S;
1910 opcode = OpCodes.Bne_Un;
1913 case Operator.LessThan:
1915 opcode = OpCodes.Blt_S;
1917 opcode = OpCodes.Blt;
1920 case Operator.GreaterThan:
1922 opcode = OpCodes.Bgt_S;
1924 opcode = OpCodes.Bgt;
1927 case Operator.LessThanOrEqual:
1929 opcode = OpCodes.Ble_S;
1931 opcode = OpCodes.Ble;
1934 case Operator.GreaterThanOrEqual:
1936 opcode = OpCodes.Bge_S;
1938 opcode = OpCodes.Ble;
1942 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1943 + oper.ToString ());
1946 ec.ig.Emit (opcode, target);
1949 public override void Emit (EmitContext ec)
1951 ILGenerator ig = ec.ig;
1953 Type r = right.Type;
1956 if (method != null) {
1958 // Note that operators are static anyway
1960 if (Arguments != null)
1961 Invocation.EmitArguments (ec, method, Arguments);
1963 if (method is MethodInfo)
1964 ig.Emit (OpCodes.Call, (MethodInfo) method);
1966 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1975 case Operator.Multiply:
1977 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1978 opcode = OpCodes.Mul_Ovf;
1979 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1980 opcode = OpCodes.Mul_Ovf_Un;
1982 opcode = OpCodes.Mul;
1984 opcode = OpCodes.Mul;
1988 case Operator.Division:
1989 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1990 opcode = OpCodes.Div_Un;
1992 opcode = OpCodes.Div;
1995 case Operator.Modulus:
1996 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1997 opcode = OpCodes.Rem_Un;
1999 opcode = OpCodes.Rem;
2002 case Operator.Addition:
2004 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2005 opcode = OpCodes.Add_Ovf;
2006 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2007 opcode = OpCodes.Add_Ovf_Un;
2009 opcode = OpCodes.Mul;
2011 opcode = OpCodes.Add;
2014 case Operator.Subtraction:
2016 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2017 opcode = OpCodes.Sub_Ovf;
2018 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2019 opcode = OpCodes.Sub_Ovf_Un;
2021 opcode = OpCodes.Sub;
2023 opcode = OpCodes.Sub;
2026 case Operator.RightShift:
2027 opcode = OpCodes.Shr;
2030 case Operator.LeftShift:
2031 opcode = OpCodes.Shl;
2034 case Operator.Equality:
2035 opcode = OpCodes.Ceq;
2038 case Operator.Inequality:
2039 ec.ig.Emit (OpCodes.Ceq);
2040 ec.ig.Emit (OpCodes.Ldc_I4_0);
2042 opcode = OpCodes.Ceq;
2045 case Operator.LessThan:
2046 opcode = OpCodes.Clt;
2049 case Operator.GreaterThan:
2050 opcode = OpCodes.Cgt;
2053 case Operator.LessThanOrEqual:
2054 ec.ig.Emit (OpCodes.Cgt);
2055 ec.ig.Emit (OpCodes.Ldc_I4_0);
2057 opcode = OpCodes.Ceq;
2060 case Operator.GreaterThanOrEqual:
2061 ec.ig.Emit (OpCodes.Clt);
2062 ec.ig.Emit (OpCodes.Ldc_I4_1);
2064 opcode = OpCodes.Sub;
2067 case Operator.LogicalOr:
2068 case Operator.BitwiseOr:
2069 opcode = OpCodes.Or;
2072 case Operator.LogicalAnd:
2073 case Operator.BitwiseAnd:
2074 opcode = OpCodes.And;
2077 case Operator.ExclusiveOr:
2078 opcode = OpCodes.Xor;
2082 throw new Exception ("This should not happen: Operator = "
2083 + oper.ToString ());
2090 public class Conditional : Expression {
2091 Expression expr, trueExpr, falseExpr;
2093 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
2096 this.trueExpr = trueExpr;
2097 this.falseExpr = falseExpr;
2100 public Expression Expr {
2106 public Expression TrueExpr {
2112 public Expression FalseExpr {
2118 public override Expression Resolve (TypeContainer tc)
2120 // FIXME: Implement;
2121 throw new Exception ("Unimplemented");
2125 public override void Emit (EmitContext ec)
2130 public class SimpleName : Expression {
2131 public readonly string Name;
2132 public readonly Location Location;
2134 public SimpleName (string name, Location l)
2141 // Checks whether we are trying to access an instance
2142 // property, method or field from a static body.
2144 Expression MemberStaticCheck (Report r, Expression e)
2146 if (e is FieldExpr){
2147 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2151 "An object reference is required " +
2152 "for the non-static field `"+Name+"'");
2155 } else if (e is MethodGroupExpr){
2156 // FIXME: Pending reorganization of MemberLookup
2157 // Basically at this point we should have the
2158 // best match already selected for us, and
2159 // we should only have to check a *single*
2160 // Method for its static on/off bit.
2162 } else if (e is PropertyExpr){
2163 if (!((PropertyExpr) e).IsStatic){
2165 "An object reference is required " +
2166 "for the non-static property access `"+
2176 // 7.5.2: Simple Names.
2178 // Local Variables and Parameters are handled at
2179 // parse time, so they never occur as SimpleNames.
2181 Expression ResolveSimpleName (TypeContainer tc)
2184 Report r = tc.RootContext.Report;
2186 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2190 else if (e is FieldExpr){
2191 FieldExpr fe = (FieldExpr) e;
2193 if (!fe.FieldInfo.IsStatic)
2194 fe.Instance = new This ();
2197 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2198 return MemberStaticCheck (r, e);
2204 // Do step 3 of the Simple Name resolution.
2206 // FIXME: implement me.
2208 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2215 // SimpleName needs to handle a multitude of cases:
2217 // simple_names and qualified_identifiers are placed on
2218 // the tree equally.
2220 public override Expression Resolve (TypeContainer tc)
2222 if (Name.IndexOf (".") != -1)
2223 return ResolveMemberAccess (tc, Name);
2225 return ResolveSimpleName (tc);
2228 public override void Emit (EmitContext ec)
2230 throw new Exception ("SimpleNames should be gone from the tree");
2235 // A simple interface that should be implemeneted by LValues
2237 public interface LValue {
2240 // The Store method should store the contents of the top
2241 // of the stack into the storage that is implemented by
2242 // the particular implementation of LValue
2244 void Store (EmitContext ec);
2247 // The AddressOf method should generate code that loads
2248 // the address of the LValue and leaves it on the stack
2250 void AddressOf (EmitContext ec);
2253 public class LocalVariableReference : Expression, LValue {
2254 public readonly string Name;
2255 public readonly Block Block;
2257 public LocalVariableReference (Block block, string name)
2261 eclass = ExprClass.Variable;
2264 public VariableInfo VariableInfo {
2266 return Block.GetVariableInfo (Name);
2270 public override Expression Resolve (TypeContainer tc)
2272 VariableInfo vi = Block.GetVariableInfo (Name);
2274 type = vi.VariableType;
2278 public override void Emit (EmitContext ec)
2280 VariableInfo vi = VariableInfo;
2281 ILGenerator ig = ec.ig;
2286 ig.Emit (OpCodes.Ldloc_0);
2290 ig.Emit (OpCodes.Ldloc_1);
2294 ig.Emit (OpCodes.Ldloc_2);
2298 ig.Emit (OpCodes.Ldloc_3);
2303 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
2305 ig.Emit (OpCodes.Ldloc, idx);
2310 public void Store (EmitContext ec)
2312 ILGenerator ig = ec.ig;
2313 VariableInfo vi = VariableInfo;
2318 ig.Emit (OpCodes.Stloc_0);
2322 ig.Emit (OpCodes.Stloc_1);
2326 ig.Emit (OpCodes.Stloc_2);
2330 ig.Emit (OpCodes.Stloc_3);
2335 ig.Emit (OpCodes.Stloc_S, (byte) idx);
2337 ig.Emit (OpCodes.Stloc, idx);
2342 public void AddressOf (EmitContext ec)
2344 VariableInfo vi = VariableInfo;
2348 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
2350 ec.ig.Emit (OpCodes.Ldloca, idx);
2354 public class ParameterReference : Expression, LValue {
2355 public readonly Parameters Pars;
2356 public readonly String Name;
2357 public readonly int Idx;
2359 public ParameterReference (Parameters pars, int idx, string name)
2364 eclass = ExprClass.Variable;
2367 public override Expression Resolve (TypeContainer tc)
2369 Type [] types = Pars.GetParameterInfo (tc);
2376 public override void Emit (EmitContext ec)
2379 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
2381 ec.ig.Emit (OpCodes.Ldarg, Idx);
2384 public void Store (EmitContext ec)
2387 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
2389 ec.ig.Emit (OpCodes.Starg, Idx);
2393 public void AddressOf (EmitContext ec)
2396 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
2398 ec.ig.Emit (OpCodes.Ldarga, Idx);
2403 // Used for arguments to New(), Invocation()
2405 public class Argument {
2412 public readonly AType Type;
2415 public Argument (Expression expr, AType type)
2421 public Expression Expr {
2431 public bool Resolve (TypeContainer tc)
2433 expr = expr.Resolve (tc);
2435 return expr != null;
2438 public void Emit (EmitContext ec)
2445 // Invocation of methods or delegates.
2447 public class Invocation : ExpressionStatement {
2448 public readonly ArrayList Arguments;
2449 public readonly Location Location;
2452 MethodBase method = null;
2454 static Hashtable method_parameter_cache;
2456 static Invocation ()
2458 method_parameter_cache = new Hashtable ();
2462 // arguments is an ArrayList, but we do not want to typecast,
2463 // as it might be null.
2465 // FIXME: only allow expr to be a method invocation or a
2466 // delegate invocation (7.5.5)
2468 public Invocation (Expression expr, ArrayList arguments, Location l)
2471 Arguments = arguments;
2475 public Expression Expr {
2482 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2483 /// compatible, and if so, how good is the match (in terms of
2484 /// "better conversions" (7.4.2.3).
2486 /// 0 is the best possible match.
2487 /// -1 represents a type mismatch.
2488 /// -2 represents a ref/out mismatch.
2490 static int Badness (Argument a, Type t)
2492 Expression argument_expr = a.Expr;
2493 Type argument_type = argument_expr.Type;
2495 if (argument_type == null){
2496 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2499 if (t == argument_type)
2503 // Now probe whether an implicit constant expression conversion
2506 // An implicit constant expression conversion permits the following
2509 // * A constant-expression of type `int' can be converted to type
2510 // sbyte, byute, short, ushort, uint, ulong provided the value of
2511 // of the expression is withing the range of the destination type.
2513 // * A constant-expression of type long can be converted to type
2514 // ulong, provided the value of the constant expression is not negative
2516 // FIXME: Note that this assumes that constant folding has
2517 // taken place. We dont do constant folding yet.
2520 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2521 IntLiteral ei = (IntLiteral) argument_expr;
2522 int value = ei.Value;
2524 if (t == TypeManager.sbyte_type){
2525 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2527 } else if (t == TypeManager.byte_type){
2528 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2530 } else if (t == TypeManager.short_type){
2531 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2533 } else if (t == TypeManager.ushort_type){
2534 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2536 } else if (t == TypeManager.uint32_type){
2538 // we can optimize this case: a positive int32
2539 // always fits on a uint32
2543 } else if (t == TypeManager.uint64_type){
2545 // we can optimize this case: a positive int32
2546 // always fits on a uint64
2551 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2552 LongLiteral ll = (LongLiteral) argument_expr;
2554 if (t == TypeManager.uint64_type)
2559 // FIXME: Implement user-defined implicit conversions here.
2560 // FIXME: Implement better conversion here.
2566 // Returns the Parameters (a ParameterData interface) for the
2569 static ParameterData GetParameterData (MethodBase mb)
2571 object pd = method_parameter_cache [mb];
2574 return (ParameterData) pd;
2576 if (mb is MethodBuilder || mb is ConstructorBuilder){
2577 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2579 InternalParameters ip = mc.ParameterInfo;
2580 method_parameter_cache [mb] = ip;
2582 return (ParameterData) ip;
2584 ParameterInfo [] pi = mb.GetParameters ();
2585 ReflectionParameters rp = new ReflectionParameters (pi);
2586 method_parameter_cache [mb] = rp;
2588 return (ParameterData) rp;
2592 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2594 // Locate user-defined implicit operators
2598 mg = MemberLookup (tc, to, "op_Implicit", false);
2601 MethodGroupExpr me = (MethodGroupExpr) mg;
2603 for (int i = me.Methods.Length; i > 0;) {
2605 MethodBase mb = me.Methods [i];
2606 ParameterData pd = GetParameterData (mb);
2608 if (from == pd.ParameterType (0))
2613 mg = MemberLookup (tc, from, "op_Implicit", false);
2616 MethodGroupExpr me = (MethodGroupExpr) mg;
2618 for (int i = me.Methods.Length; i > 0;) {
2620 MethodBase mb = me.Methods [i];
2621 MethodInfo mi = (MethodInfo) mb;
2623 if (mi.ReturnType == to)
2632 // Determines "better conversion" as specified in 7.4.2.3
2633 // Returns : 1 if a->p is better
2634 // 0 if a->q or neither is better
2636 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2639 Type argument_type = a.Expr.Type;
2640 Expression argument_expr = a.Expr;
2642 if (argument_type == null)
2643 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2648 if (argument_type == p)
2651 if (argument_type == q)
2655 // Now probe whether an implicit constant expression conversion
2658 // An implicit constant expression conversion permits the following
2661 // * A constant-expression of type `int' can be converted to type
2662 // sbyte, byute, short, ushort, uint, ulong provided the value of
2663 // of the expression is withing the range of the destination type.
2665 // * A constant-expression of type long can be converted to type
2666 // ulong, provided the value of the constant expression is not negative
2668 // FIXME: Note that this assumes that constant folding has
2669 // taken place. We dont do constant folding yet.
2672 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2673 IntLiteral ei = (IntLiteral) argument_expr;
2674 int value = ei.Value;
2676 if (p == TypeManager.sbyte_type){
2677 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2679 } else if (p == TypeManager.byte_type){
2680 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2682 } else if (p == TypeManager.short_type){
2683 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2685 } else if (p == TypeManager.ushort_type){
2686 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2688 } else if (p == TypeManager.uint32_type){
2690 // we can optimize this case: a positive int32
2691 // always fits on a uint32
2695 } else if (p == TypeManager.uint64_type){
2697 // we can optimize this case: a positive int32
2698 // always fits on a uint64
2703 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2704 LongLiteral ll = (LongLiteral) argument_expr;
2706 if (p == TypeManager.uint64_type){
2716 tmp = ConvertImplicit (tc, argument_expr, p);
2725 if (ConversionExists (tc, p, q) == true &&
2726 ConversionExists (tc, q, p) == false)
2729 if (p == TypeManager.sbyte_type)
2730 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2731 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2734 if (p == TypeManager.short_type)
2735 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2736 q == TypeManager.uint64_type)
2739 if (p == TypeManager.int32_type)
2740 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2743 if (p == TypeManager.int64_type)
2744 if (q == TypeManager.uint64_type)
2751 // Determines "Better function" and returns an integer indicating :
2752 // 0 if candidate ain't better
2753 // 1 if candidate is better than the current best match
2755 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2757 ParameterData candidate_pd = GetParameterData (candidate);
2758 ParameterData best_pd;
2764 argument_count = args.Count;
2766 if (candidate_pd.Count == 0 && argument_count == 0)
2770 if (candidate_pd.Count == argument_count) {
2772 for (int j = argument_count; j > 0;) {
2775 Argument a = (Argument) args [j];
2777 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2794 best_pd = GetParameterData (best);
2796 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2797 int rating1 = 0, rating2 = 0;
2799 for (int j = argument_count; j > 0;) {
2803 Argument a = (Argument) args [j];
2805 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2806 best_pd.ParameterType (j));
2807 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2808 candidate_pd.ParameterType (j));
2814 if (rating1 > rating2)
2823 public static string FullMethodDesc (MethodBase mb)
2825 StringBuilder sb = new StringBuilder (mb.Name);
2826 ParameterData pd = GetParameterData (mb);
2829 for (int i = pd.Count; i > 0;) {
2831 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2837 return sb.ToString ();
2840 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2843 if (mg1 != null || mg2 != null) {
2845 MethodGroupExpr left_set = null, right_set = null;
2846 int length1 = 0, length2 = 0;
2849 left_set = (MethodGroupExpr) mg1;
2850 length1 = left_set.Methods.Length;
2854 right_set = (MethodGroupExpr) mg2;
2855 length2 = right_set.Methods.Length;
2858 MemberInfo [] miset = new MemberInfo [length1 + length2];
2859 if (left_set != null)
2860 left_set.Methods.CopyTo (miset, 0);
2861 if (right_set != null)
2862 right_set.Methods.CopyTo (miset, length1);
2864 MethodGroupExpr union = new MethodGroupExpr (miset);
2875 // Find the Applicable Function Members (7.4.2.1)
2877 // me: Method Group expression with the members to select.
2878 // it might contain constructors or methods (or anything
2879 // that maps to a method).
2881 // Arguments: ArrayList containing resolved Argument objects.
2883 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2884 // that is the best match of me on Arguments.
2887 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2888 ArrayList Arguments, Location loc)
2890 ArrayList afm = new ArrayList ();
2891 int best_match_idx = -1;
2892 MethodBase method = null;
2895 for (int i = me.Methods.Length; i > 0; ){
2897 MethodBase candidate = me.Methods [i];
2900 x = BetterFunction (tc, Arguments, candidate, method);
2906 method = me.Methods [best_match_idx];
2910 if (Arguments == null)
2913 argument_count = Arguments.Count;
2917 // Now we see if we can at least find a method with the same number of arguments
2918 // and then try doing implicit conversion on the arguments
2919 if (best_match_idx == -1) {
2921 for (int i = me.Methods.Length; i > 0;) {
2923 MethodBase mb = me.Methods [i];
2924 pd = GetParameterData (mb);
2926 if (pd.Count == argument_count) {
2928 method = me.Methods [best_match_idx];
2939 // And now convert implicitly, each argument to the required type
2941 pd = GetParameterData (method);
2943 for (int j = argument_count; j > 0;) {
2945 Argument a = (Argument) Arguments [j];
2946 Expression a_expr = a.Expr;
2948 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
2951 Error (tc, 1502, loc,
2952 "The best overloaded match for method '" + FullMethodDesc (method) +
2953 "' has some invalid arguments");
2954 Error (tc, 1503, loc,
2955 "Argument " + (j+1) +
2956 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
2957 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
2962 // Update the argument with the implicit conversion
2972 public override Expression Resolve (TypeContainer tc)
2975 // First, resolve the expression that is used to
2976 // trigger the invocation
2978 this.expr = expr.Resolve (tc);
2979 if (this.expr == null)
2982 if (!(this.expr is MethodGroupExpr)){
2983 report118 (tc, this.expr, "method group");
2988 // Next, evaluate all the expressions in the argument list
2990 if (Arguments != null){
2991 for (int i = Arguments.Count; i > 0;){
2993 Argument a = (Argument) Arguments [i];
2995 if (!a.Resolve (tc))
3000 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
3002 if (method == null){
3003 Error (tc, -6, Location,
3004 "Could not find any applicable function for this argument list");
3008 if (method is MethodInfo)
3009 type = ((MethodInfo)method).ReturnType;
3014 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3018 if (Arguments != null)
3019 top = Arguments.Count;
3023 for (int i = 0; i < top; i++){
3024 Argument a = (Argument) Arguments [i];
3030 public override void Emit (EmitContext ec)
3032 bool is_static = method.IsStatic;
3035 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3038 // If this is ourselves, push "this"
3040 if (mg.InstanceExpression == null){
3041 ec.ig.Emit (OpCodes.Ldarg_0);
3044 // Push the instance expression
3046 mg.InstanceExpression.Emit (ec);
3050 if (Arguments != null)
3051 EmitArguments (ec, method, Arguments);
3054 if (method is MethodInfo)
3055 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3057 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3059 if (method is MethodInfo)
3060 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3062 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3066 public override void EmitStatement (EmitContext ec)
3071 // Pop the return value if there is one
3073 if (method is MethodInfo){
3074 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3075 ec.ig.Emit (OpCodes.Pop);
3080 public class New : ExpressionStatement {
3087 public readonly NType NewType;
3088 public readonly ArrayList Arguments;
3089 public readonly string RequestedType;
3090 // These are for the case when we have an array
3091 public readonly string Rank;
3092 public readonly ArrayList Indices;
3093 public readonly ArrayList Initializers;
3096 MethodBase method = null;
3098 public New (string requested_type, ArrayList arguments, Location loc)
3100 RequestedType = requested_type;
3101 Arguments = arguments;
3102 NewType = NType.Object;
3106 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3108 RequestedType = requested_type;
3111 Initializers = initializers;
3112 NewType = NType.Array;
3116 public override Expression Resolve (TypeContainer tc)
3118 type = tc.LookupType (RequestedType, false);
3125 ml = MemberLookup (tc, type, ".ctor", false,
3126 MemberTypes.Constructor, AllBindingsFlags);
3128 if (! (ml is MethodGroupExpr)){
3130 // FIXME: Find proper error
3132 report118 (tc, ml, "method group");
3136 if (Arguments != null){
3137 for (int i = Arguments.Count; i > 0;){
3139 Argument a = (Argument) Arguments [i];
3141 if (!a.Resolve (tc))
3146 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3148 if (method == null) {
3149 Error (tc, -6, Location,
3150 "New invocation: Can not find a constructor for this argument list");
3157 public override void Emit (EmitContext ec)
3159 Invocation.EmitArguments (ec, method, Arguments);
3160 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3163 public override void EmitStatement (EmitContext ec)
3166 ec.ig.Emit (OpCodes.Pop);
3171 // Represents the `this' construct
3173 public class This : Expression, LValue {
3174 public override Expression Resolve (TypeContainer tc)
3176 eclass = ExprClass.Variable;
3177 type = tc.TypeBuilder;
3180 // FIXME: Verify that this is only used in instance contexts.
3185 public override void Emit (EmitContext ec)
3187 ec.ig.Emit (OpCodes.Ldarg_0);
3190 public void Store (EmitContext ec)
3193 // Assignment to the "this" variable.
3195 // FIXME: Apparently this is a bug that we
3196 // must catch as `this' seems to be readonly ;-)
3198 ec.ig.Emit (OpCodes.Starg, 0);
3201 public void AddressOf (EmitContext ec)
3203 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3207 public class TypeOf : Expression {
3208 public readonly string QueriedType;
3210 public TypeOf (string queried_type)
3212 QueriedType = queried_type;
3215 public override Expression Resolve (TypeContainer tc)
3217 type = tc.LookupType (QueriedType, false);
3222 eclass = ExprClass.Type;
3226 public override void Emit (EmitContext ec)
3228 throw new Exception ("Implement me");
3229 // FIXME: Implement.
3233 public class SizeOf : Expression {
3234 public readonly string QueriedType;
3236 public SizeOf (string queried_type)
3238 this.QueriedType = queried_type;
3241 public override Expression Resolve (TypeContainer tc)
3243 // FIXME: Implement;
3244 throw new Exception ("Unimplemented");
3248 public override void Emit (EmitContext ec)
3250 throw new Exception ("Implement me");
3254 public class MemberAccess : Expression {
3255 public readonly string Identifier;
3257 Expression member_lookup;
3259 public MemberAccess (Expression expr, string id)
3265 public Expression Expr {
3271 public override Expression Resolve (TypeContainer tc)
3273 Expression new_expression = expr.Resolve (tc);
3275 if (new_expression == null)
3278 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
3280 if (member_lookup is MethodGroupExpr){
3281 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3284 // Bind the instance expression to it
3286 // FIXME: This is a horrible way of detecting if it is
3287 // an instance expression. Figure out how to fix this.
3290 if (expr is LocalVariableReference ||
3291 expr is ParameterReference ||
3293 mg.InstanceExpression = expr;
3295 return member_lookup;
3296 } else if (member_lookup is FieldExpr){
3297 FieldExpr fe = (FieldExpr) member_lookup;
3301 return member_lookup;
3304 // FIXME: This should generate the proper node
3305 // ie, for a Property Access, it should like call it
3308 return member_lookup;
3311 public override void Emit (EmitContext ec)
3313 throw new Exception ("Implement me");
3319 // Nodes of type Namespace are created during the semantic
3320 // analysis to resolve member_access/qualified_identifier/simple_name
3323 // They are born `resolved'.
3325 public class NamespaceExpr : Expression {
3326 public readonly string Name;
3328 public NamespaceExpr (string name)
3331 eclass = ExprClass.Namespace;
3334 public override Expression Resolve (TypeContainer tc)
3339 public override void Emit (EmitContext ec)
3341 throw new Exception ("Namespace expressions should never be emitted");
3346 // Fully resolved expression that evaluates to a type
3348 public class TypeExpr : Expression {
3349 public TypeExpr (Type t)
3352 eclass = ExprClass.Type;
3355 override public Expression Resolve (TypeContainer tc)
3360 override public void Emit (EmitContext ec)
3362 throw new Exception ("Implement me");
3367 // MethodGroup Expression.
3369 // This is a fully resolved expression that evaluates to a type
3371 public class MethodGroupExpr : Expression {
3372 public readonly MethodBase [] Methods;
3373 Expression instance_expression = null;
3375 public MethodGroupExpr (MemberInfo [] mi)
3377 Methods = new MethodBase [mi.Length];
3378 mi.CopyTo (Methods, 0);
3379 eclass = ExprClass.MethodGroup;
3383 // `A method group may have associated an instance expression'
3385 public Expression InstanceExpression {
3387 return instance_expression;
3391 instance_expression = value;
3395 override public Expression Resolve (TypeContainer tc)
3400 override public void Emit (EmitContext ec)
3402 throw new Exception ("This should never be reached");
3406 public class BuiltinTypeAccess : Expression {
3407 public readonly string AccessBase;
3408 public readonly string Method;
3410 public BuiltinTypeAccess (string type, string method)
3412 System.Console.WriteLine ("DUDE! This type should be fully resolved!");
3417 public override Expression Resolve (TypeContainer tc)
3419 // FIXME: Implement;
3420 throw new Exception ("Unimplemented");
3424 public override void Emit (EmitContext ec)
3426 throw new Exception ("Unimplemented");
3431 // Fully resolved expression that evaluates to a Field
3433 public class FieldExpr : Expression, LValue {
3434 public readonly FieldInfo FieldInfo;
3435 public Expression Instance;
3437 public FieldExpr (FieldInfo fi)
3440 eclass = ExprClass.Variable;
3441 type = fi.FieldType;
3444 override public Expression Resolve (TypeContainer tc)
3446 if (!FieldInfo.IsStatic){
3447 if (Instance == null){
3448 throw new Exception ("non-static FieldExpr without instance var\n" +
3449 "You have to assign the Instance variable\n" +
3450 "Of the FieldExpr to set this\n");
3453 Instance = Instance.Resolve (tc);
3454 if (Instance == null)
3461 override public void Emit (EmitContext ec)
3463 ILGenerator ig = ec.ig;
3465 if (FieldInfo.IsStatic)
3466 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3470 ig.Emit (OpCodes.Ldfld, FieldInfo);
3474 public void Store (EmitContext ec)
3476 if (FieldInfo.IsStatic)
3477 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
3479 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
3482 public void AddressOf (EmitContext ec)
3484 if (FieldInfo.IsStatic)
3485 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
3488 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
3494 // Fully resolved expression that evaluates to a Property
3496 public class PropertyExpr : Expression {
3497 public readonly PropertyInfo PropertyInfo;
3498 public readonly bool IsStatic;
3500 public PropertyExpr (PropertyInfo pi)
3503 eclass = ExprClass.PropertyAccess;
3506 MethodBase [] acc = pi.GetAccessors ();
3508 for (int i = 0; i < acc.Length; i++)
3509 if (acc [i].IsStatic)
3512 type = pi.PropertyType;
3515 override public Expression Resolve (TypeContainer tc)
3517 // We are born in resolved state.
3521 override public void Emit (EmitContext ec)
3523 // FIXME: Implement;
3524 throw new Exception ("Unimplemented");
3529 // Fully resolved expression that evaluates to a Property
3531 public class EventExpr : Expression {
3532 public readonly EventInfo EventInfo;
3534 public EventExpr (EventInfo ei)
3537 eclass = ExprClass.EventAccess;
3540 override public Expression Resolve (TypeContainer tc)
3542 // We are born in resolved state.
3546 override public void Emit (EmitContext ec)
3548 throw new Exception ("Implement me");
3549 // FIXME: Implement.
3553 public class CheckedExpr : Expression {
3555 public Expression Expr;
3557 public CheckedExpr (Expression e)
3562 public override Expression Resolve (TypeContainer tc)
3564 Expr = Expr.Resolve (tc);
3569 eclass = Expr.ExprClass;
3574 public override void Emit (EmitContext ec)
3576 bool last_check = ec.CheckState;
3578 ec.CheckState = true;
3580 ec.CheckState = last_check;
3585 public class UnCheckedExpr : Expression {
3587 public Expression Expr;
3589 public UnCheckedExpr (Expression e)
3594 public override Expression Resolve (TypeContainer tc)
3596 Expr = Expr.Resolve (tc);
3601 eclass = Expr.ExprClass;
3606 public override void Emit (EmitContext ec)
3608 bool last_check = ec.CheckState;
3610 ec.CheckState = false;
3612 ec.CheckState = last_check;
3617 public class ElementAccess : Expression {
3619 public readonly ArrayList Arguments;
3620 public readonly Expression Expr;
3622 public ElementAccess (Expression e, ArrayList e_list)
3628 public override Expression Resolve (TypeContainer tc)
3630 // FIXME: Implement;
3631 throw new Exception ("Unimplemented");
3635 public override void Emit (EmitContext ec)
3637 // FIXME : Implement !
3638 throw new Exception ("Unimplemented");
3643 public class BaseAccess : Expression {
3645 public enum BaseAccessType {
3650 public readonly BaseAccessType BAType;
3651 public readonly string Member;
3652 public readonly ArrayList Arguments;
3654 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3662 public override Expression Resolve (TypeContainer tc)
3664 // FIXME: Implement;
3665 throw new Exception ("Unimplemented");
3669 public override void Emit (EmitContext ec)
3671 throw new Exception ("Unimplemented");
3675 public class UserImplicitCast : Expression {
3680 ArrayList arguments;
3682 public UserImplicitCast (Expression source, Type target)
3684 this.source = source;
3685 this.target = target;
3688 public override Expression Resolve (TypeContainer tc)
3690 source = source.Resolve (tc);
3695 Expression mg1, mg2;
3696 MethodGroupExpr union;
3699 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3700 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3702 union = Invocation.MakeUnionSet (mg1, mg2);
3704 arguments = new ArrayList ();
3705 arguments.Add (new Argument (source, Argument.AType.Expression));
3707 if (union != null) {
3708 method = Invocation.OverloadResolve (tc, union, arguments,
3709 new Location ("FIXME", 1, 1));
3711 if (method != null) {
3712 mi = (MethodInfo) method;
3714 if (mi.ReturnType == target) {
3715 type = mi.ReturnType;
3721 if (target == TypeManager.bool_type) {
3723 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3724 mg2 = MemberLookup (tc, target, "op_True", false);
3726 union = Invocation.MakeUnionSet (mg1, mg2);
3731 method = Invocation.OverloadResolve (tc, union, arguments,
3732 new Location ("FIXME", 1, 1));
3734 if (method != null) {
3735 mi = (MethodInfo) method;
3737 if (mi.ReturnType == target) {
3738 type = mi.ReturnType;
3747 public static bool CanConvert (TypeContainer tc, Expression source, Type target)
3749 source = source.Resolve (tc);
3754 Expression mg1, mg2;
3756 ArrayList arguments;
3758 mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
3759 mg2 = MemberLookup (tc, target, "op_Implicit", false);
3761 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3763 arguments = new ArrayList ();
3764 arguments.Add (new Argument (source, Argument.AType.Expression));
3766 if (union != null) {
3768 method = Invocation.OverloadResolve (tc, union, arguments,
3769 new Location ("FIXME", 1, 1));
3771 if (method != null) {
3772 MethodInfo mi = (MethodInfo) method;
3774 if (mi.ReturnType == target)
3779 // If we have a boolean type, we need to check for the True
3780 // and False operators too.
3782 if (target == TypeManager.bool_type) {
3784 mg1 = MemberLookup (tc, source.Type, "op_True", false);
3785 mg2 = MemberLookup (tc, target, "op_True", false);
3787 union = Invocation.MakeUnionSet (mg1, mg2);
3792 method = Invocation.OverloadResolve (tc, union, arguments,
3793 new Location ("FIXME", 1, 1));
3794 if (method != null) {
3795 MethodInfo mi = (MethodInfo) method;
3797 if (mi.ReturnType == target)
3806 public override void Emit (EmitContext ec)
3808 ILGenerator ig = ec.ig;
3810 if (method != null) {
3812 // Note that operators are static anyway
3814 if (arguments != null)
3815 Invocation.EmitArguments (ec, method, arguments);
3817 if (method is MethodInfo)
3818 ig.Emit (OpCodes.Call, (MethodInfo) method);
3820 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3825 throw new Exception ("Implement me");