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 // Performs semantic analysis on the Expression
77 // The Resolve method is invoked to perform the semantic analysis
80 // The return value is an expression (it can be the
81 // same expression in some cases) or a new
82 // expression that better represents this node.
84 // For example, optimizations of Unary (LiteralInt)
85 // would return a new LiteralInt with a negated
88 // If there is an error during semantic analysis,
89 // then an error should
90 // be reported (using TypeContainer.RootContext.Report) and a null
91 // value should be returned.
93 // There are two side effects expected from calling
94 // Resolve(): the the field variable "eclass" should
95 // be set to any value of the enumeration
96 // `ExprClass' and the type variable should be set
97 // to a valid type (this is the type of the
101 public abstract Expression Resolve (TypeContainer tc);
104 // Emits the code for the expression
109 // The Emit method is invoked to generate the code
110 // for the expression.
113 public abstract void Emit (EmitContext ec);
116 // Protected constructor. Only derivate types should
117 // be able to be created
120 protected Expression ()
122 eclass = ExprClass.Invalid;
127 // Returns a fully formed expression after a MemberLookup
129 static Expression ExprClassFromMemberInfo (MemberInfo mi)
131 if (mi is EventInfo){
132 return new EventExpr ((EventInfo) mi);
133 } else if (mi is FieldInfo){
134 return new FieldExpr ((FieldInfo) mi);
135 } else if (mi is PropertyInfo){
136 return new PropertyExpr ((PropertyInfo) mi);
137 } else if (mi is Type)
138 return new TypeExpr ((Type) mi);
144 // FIXME: Probably implement a cache for (t,name,current_access_set)?
146 // FIXME: We need to cope with access permissions here, or this wont
149 // This code could use some optimizations, but we need to do some
150 // measurements. For example, we could use a delegate to `flag' when
151 // something can not any longer be a method-group (because it is something
155 // If the return value is an Array, then it is an array of
158 // If the return value is an MemberInfo, it is anything, but a Method
162 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
163 // the arguments here and have MemberLookup return only the methods that
164 // match the argument count/type, unlike we are doing now (we delay this
167 // This is so we can catch correctly attempts to invoke instance methods
168 // from a static body (scan for error 120 in ResolveSimpleName).
170 public static Expression MemberLookup (RootContext rc, Type t, string name,
171 bool same_type, MemberTypes mt, BindingFlags bf)
174 bf |= BindingFlags.NonPublic;
176 MemberInfo [] mi = rc.TypeManager.FindMembers (t, mt, bf, Type.FilterName, name);
181 if (mi.Length == 1 && !(mi [0] is MethodBase))
182 return Expression.ExprClassFromMemberInfo (mi [0]);
184 for (int i = 0; i < mi.Length; i++)
185 if (!(mi [i] is MethodBase)){
186 rc.Report.Error (-5, "Do not know how to reproduce this case: " +
187 "Methods and non-Method with the same name, report this please");
189 for (i = 0; i < mi.Length; i++){
190 Type tt = mi [i].GetType ();
192 Console.WriteLine (i + ": " + mi [i]);
193 while (tt != TypeManager.object_type){
194 Console.WriteLine (tt);
200 return new MethodGroupExpr (mi);
203 public const MemberTypes AllMemberTypes =
204 MemberTypes.Constructor |
208 MemberTypes.NestedType |
209 MemberTypes.Property;
211 public const BindingFlags AllBindingsFlags =
212 BindingFlags.Public |
213 BindingFlags.Static |
214 BindingFlags.Instance;
216 public static Expression MemberLookup (RootContext rc, Type t, string name,
219 return MemberLookup (rc, t, name, same_type, AllMemberTypes, AllBindingsFlags);
223 // Resolves the E in `E.I' side for a member_access
225 // This is suboptimal and should be merged with ResolveMemberAccess
227 static Expression ResolvePrimary (TypeContainer tc, string name)
229 int dot_pos = name.LastIndexOf (".");
231 if (tc.RootContext.IsNamespace (name))
232 return new NamespaceExpr (name);
236 Type t = tc.LookupType (name, false);
239 return new TypeExpr (t);
245 static public Expression ResolveMemberAccess (TypeContainer tc, string name)
248 int dot_pos = name.LastIndexOf (".");
249 string left = name.Substring (0, dot_pos);
250 string right = name.Substring (dot_pos + 1);
252 left_e = ResolvePrimary (tc, left);
256 switch (left_e.ExprClass){
258 return MemberLookup (tc.RootContext,
260 left_e.Type == tc.TypeBuilder);
262 case ExprClass.Namespace:
263 case ExprClass.PropertyAccess:
264 case ExprClass.IndexerAccess:
265 case ExprClass.Variable:
266 case ExprClass.Value:
267 case ExprClass.Nothing:
268 case ExprClass.EventAccess:
269 case ExprClass.MethodGroup:
270 case ExprClass.Invalid:
271 tc.RootContext.Report.Error (-1000,
272 "Internal compiler error, should have " +
273 "got these handled before");
280 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
282 Type expr_type = expr.Type;
284 if (target_type == TypeManager.object_type) {
285 if (expr_type.IsClass)
286 return new EmptyCast (expr, target_type);
287 if (expr_type.IsValueType)
288 return new BoxedCast (expr, target_type);
289 } else if (expr_type.IsSubclassOf (target_type))
290 return new EmptyCast (expr, target_type);
292 // FIXME: missing implicit reference conversions:
294 // from any class-type S to any interface-type T.
295 // from any interface type S to interface-type T.
296 // from an array-type S to an array-type of type T
297 // from an array-type to System.Array
298 // from any delegate type to System.Delegate
299 // from any array-type or delegate type into System.ICloneable.
300 // from the null type to any reference-type.
308 // Handles expressions like this: decimal d; d = 1;
309 // and changes them into: decimal d; d = new System.Decimal (1);
311 static Expression InternalTypeConstructor (TypeContainer tc, Expression expr, Type target)
313 ArrayList args = new ArrayList ();
315 args.Add (new Argument (expr, Argument.AType.Expression));
317 Expression ne = new New (target.FullName, args,
318 new Location ("FIXME", 1, 1));
320 return ne.Resolve (tc);
324 // Converts implicitly the resolved expression `expr' into the
325 // `target_type'. It returns a new expression that can be used
326 // in a context that expects a `target_type'.
328 static public Expression ConvertImplicit (TypeContainer tc, Expression expr, Type target_type)
330 Type expr_type = expr.Type;
332 if (expr_type == target_type)
336 // Step 1: Perform implicit conversions as found on expr.Type
339 if (UserImplicitCast.CanConvert (tc, expr, target_type) == true) {
340 Expression imp = new UserImplicitCast (expr, target_type);
346 // Step 2: Built-in conversions.
348 if (expr_type == TypeManager.sbyte_type){
350 // From sbyte to short, int, long, float, double.
352 if (target_type == TypeManager.int32_type)
353 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
354 if (target_type == TypeManager.int64_type)
355 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
356 if (target_type == TypeManager.double_type)
357 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
358 if (target_type == TypeManager.float_type)
359 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
360 if (target_type == TypeManager.short_type)
361 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
362 if (target_type == TypeManager.decimal_type)
363 return InternalTypeConstructor (tc, expr, target_type);
364 } else if (expr_type == TypeManager.byte_type){
366 // From byte to short, ushort, int, uint, long, ulong, float, double
368 if ((target_type == TypeManager.short_type) ||
369 (target_type == TypeManager.ushort_type) ||
370 (target_type == TypeManager.int32_type) ||
371 (target_type == TypeManager.uint32_type))
372 return new EmptyCast (expr, target_type);
374 if (target_type == TypeManager.uint64_type)
375 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
376 if (target_type == TypeManager.int64_type)
377 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
379 if (target_type == TypeManager.float_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
381 if (target_type == TypeManager.double_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
383 if (target_type == TypeManager.decimal_type)
384 return InternalTypeConstructor (tc, expr, target_type);
385 } else if (expr_type == TypeManager.short_type){
387 // From short to int, long, float, double
389 if (target_type == TypeManager.int32_type)
390 return new EmptyCast (expr, target_type);
391 if (target_type == TypeManager.int64_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
393 if (target_type == TypeManager.double_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
395 if (target_type == TypeManager.float_type)
396 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
397 if (target_type == TypeManager.decimal_type)
398 return InternalTypeConstructor (tc, expr, target_type);
399 } else if (expr_type == TypeManager.ushort_type){
401 // From ushort to int, uint, long, ulong, float, double
403 if ((target_type == TypeManager.uint32_type) ||
404 (target_type == TypeManager.uint64_type))
405 return new EmptyCast (expr, target_type);
407 if (target_type == TypeManager.int32_type)
408 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
409 if (target_type == TypeManager.int64_type)
410 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
411 if (target_type == TypeManager.double_type)
412 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
413 if (target_type == TypeManager.float_type)
414 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
415 if (target_type == TypeManager.decimal_type)
416 return InternalTypeConstructor (tc, expr, target_type);
417 } else if (expr_type == TypeManager.int32_type){
419 // From int to long, float, double
421 if (target_type == TypeManager.int64_type)
422 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
423 if (target_type == TypeManager.double_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
425 if (target_type == TypeManager.float_type)
426 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
427 if (target_type == TypeManager.decimal_type)
428 return InternalTypeConstructor (tc, expr, target_type);
429 } else if (expr_type == TypeManager.uint32_type){
431 // From uint to long, ulong, float, double
433 if (target_type == TypeManager.int64_type)
434 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
435 if (target_type == TypeManager.uint64_type)
436 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
437 if (target_type == TypeManager.double_type)
438 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
440 if (target_type == TypeManager.float_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
443 if (target_type == TypeManager.decimal_type)
444 return InternalTypeConstructor (tc, expr, target_type);
445 } else if ((expr_type == TypeManager.uint64_type) ||
446 (expr_type == TypeManager.int64_type)){
448 // From long to float, double
450 if (target_type == TypeManager.double_type)
451 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
453 if (target_type == TypeManager.float_type)
454 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
456 if (target_type == TypeManager.decimal_type)
457 return InternalTypeConstructor (tc, expr, target_type);
458 } else if (expr_type == TypeManager.char_type){
460 // From char to ushort, int, uint, long, ulong, float, double
462 if ((target_type == TypeManager.ushort_type) ||
463 (target_type == TypeManager.int32_type) ||
464 (target_type == TypeManager.uint32_type))
465 return new EmptyCast (expr, target_type);
466 if (target_type == TypeManager.uint64_type)
467 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
468 if (target_type == TypeManager.int64_type)
469 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
470 if (target_type == TypeManager.float_type)
471 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
472 if (target_type == TypeManager.double_type)
473 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
474 if (target_type == TypeManager.decimal_type)
475 return InternalTypeConstructor (tc, expr, target_type);
477 return ImplicitReferenceConversion (expr, target_type);
482 // Could not find an implicit cast.
488 // Attemps to perform an implict constant conversion of the IntLiteral
489 // into a different data type using casts (See Implicit Constant
490 // Expression Conversions)
492 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
494 int value = il.Value;
496 if (target_type == TypeManager.sbyte_type){
497 if (value >= SByte.MinValue && value <= SByte.MaxValue)
499 } else if (target_type == TypeManager.byte_type){
500 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
502 } else if (target_type == TypeManager.short_type){
503 if (value >= Int16.MinValue && value <= Int16.MaxValue)
505 } else if (target_type == TypeManager.ushort_type){
506 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
508 } else if (target_type == TypeManager.uint32_type){
510 // we can optimize this case: a positive int32
511 // always fits on a uint32
515 } else if (target_type == TypeManager.uint64_type){
517 // we can optimize this case: a positive int32
518 // always fits on a uint64. But we need an opcode
522 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
529 // Attemptes to implicityly convert `target' into `type', using
530 // ConvertImplicit. If there is no implicit conversion, then
531 // an error is signaled
533 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
534 Type type, Location l)
538 e = ConvertImplicit (tc, target, type);
543 // Attempt to do the implicit constant expression conversions
545 if (target is IntLiteral){
546 e = TryImplicitIntConversion (type, (IntLiteral) target);
549 } else if (target is LongLiteral){
551 // Try the implicit constant expression conversion
552 // from long to ulong, instead of a nice routine,
555 if (((LongLiteral) target).Value > 0)
559 string msg = "Can not convert implicitly from `"+
560 TypeManager.CSharpName (target.Type) + "' to `" +
561 TypeManager.CSharpName (type) + "'";
563 tc.RootContext.Report.Error (29, l, msg);
569 // Performs the explicit numeric conversions
571 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
574 Type expr_type = expr.Type;
576 if (expr_type == TypeManager.sbyte_type){
578 // From sbyte to byte, ushort, uint, ulong, char
580 if (target_type == TypeManager.byte_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
582 if (target_type == TypeManager.ushort_type)
583 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
584 if (target_type == TypeManager.uint32_type)
585 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
586 if (target_type == TypeManager.uint64_type)
587 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
588 if (target_type == TypeManager.char_type)
589 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
590 } else if (expr_type == TypeManager.byte_type){
592 // From byte to sbyte and char
594 if (target_type == TypeManager.sbyte_type)
595 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
596 if (target_type == TypeManager.char_type)
597 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
598 } else if (expr_type == TypeManager.short_type){
600 // From short to sbyte, byte, ushort, uint, ulong, char
602 if (target_type == TypeManager.sbyte_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
604 if (target_type == TypeManager.byte_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
606 if (target_type == TypeManager.ushort_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
608 if (target_type == TypeManager.uint32_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
610 if (target_type == TypeManager.uint64_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
612 if (target_type == TypeManager.char_type)
613 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
614 } else if (expr_type == TypeManager.ushort_type){
616 // From ushort to sbyte, byte, short, char
618 if (target_type == TypeManager.sbyte_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
620 if (target_type == TypeManager.byte_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
622 if (target_type == TypeManager.short_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
624 if (target_type == TypeManager.char_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
626 } else if (expr_type == TypeManager.int32_type){
628 // From int to sbyte, byte, short, ushort, uint, ulong, char
630 if (target_type == TypeManager.sbyte_type)
631 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
632 if (target_type == TypeManager.byte_type)
633 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
634 if (target_type == TypeManager.short_type)
635 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
636 if (target_type == TypeManager.ushort_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
638 if (target_type == TypeManager.uint32_type)
639 return new EmptyCast (expr, target_type);
640 if (target_type == TypeManager.uint64_type)
641 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
642 if (target_type == TypeManager.char_type)
643 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
644 } else if (expr_type == TypeManager.uint32_type){
646 // From uint to sbyte, byte, short, ushort, int, char
648 if (target_type == TypeManager.sbyte_type)
649 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
650 if (target_type == TypeManager.byte_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
652 if (target_type == TypeManager.short_type)
653 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
654 if (target_type == TypeManager.ushort_type)
655 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
656 if (target_type == TypeManager.int32_type)
657 return new EmptyCast (expr, target_type);
658 if (target_type == TypeManager.char_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
660 } else if (expr_type == TypeManager.int64_type){
662 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
664 if (target_type == TypeManager.sbyte_type)
665 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
666 if (target_type == TypeManager.byte_type)
667 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
668 if (target_type == TypeManager.short_type)
669 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
670 if (target_type == TypeManager.ushort_type)
671 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
672 if (target_type == TypeManager.int32_type)
673 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
674 if (target_type == TypeManager.uint32_type)
675 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
676 if (target_type == TypeManager.uint64_type)
677 return new EmptyCast (expr, target_type);
678 if (target_type == TypeManager.char_type)
679 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
680 } else if (expr_type == TypeManager.uint64_type){
682 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
684 if (target_type == TypeManager.sbyte_type)
685 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
686 if (target_type == TypeManager.byte_type)
687 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
688 if (target_type == TypeManager.short_type)
689 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
690 if (target_type == TypeManager.ushort_type)
691 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
692 if (target_type == TypeManager.int32_type)
693 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
694 if (target_type == TypeManager.uint32_type)
695 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
696 if (target_type == TypeManager.int64_type)
697 return new EmptyCast (expr, target_type);
698 if (target_type == TypeManager.char_type)
699 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
700 } else if (expr_type == TypeManager.char_type){
702 // From char to sbyte, byte, short
704 if (target_type == TypeManager.sbyte_type)
705 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
706 if (target_type == TypeManager.byte_type)
707 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
708 if (target_type == TypeManager.short_type)
709 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
710 } else if (expr_type == TypeManager.float_type){
712 // From float to sbyte, byte, short,
713 // ushort, int, uint, long, ulong, char
716 if (target_type == TypeManager.sbyte_type)
717 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
718 if (target_type == TypeManager.byte_type)
719 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
720 if (target_type == TypeManager.short_type)
721 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
722 if (target_type == TypeManager.ushort_type)
723 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
724 if (target_type == TypeManager.int32_type)
725 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
726 if (target_type == TypeManager.uint32_type)
727 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
728 if (target_type == TypeManager.int64_type)
729 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
730 if (target_type == TypeManager.uint64_type)
731 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
732 if (target_type == TypeManager.char_type)
733 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
734 if (target_type == TypeManager.decimal_type)
735 return InternalTypeConstructor (tc, expr, target_type);
736 } else if (expr_type == TypeManager.double_type){
738 // From double to byte, byte, short,
739 // ushort, int, uint, long, ulong,
740 // char, float or decimal
742 if (target_type == TypeManager.sbyte_type)
743 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
744 if (target_type == TypeManager.byte_type)
745 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
746 if (target_type == TypeManager.short_type)
747 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
748 if (target_type == TypeManager.ushort_type)
749 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
750 if (target_type == TypeManager.int32_type)
751 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
752 if (target_type == TypeManager.uint32_type)
753 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
754 if (target_type == TypeManager.int64_type)
755 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
756 if (target_type == TypeManager.uint64_type)
757 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
758 if (target_type == TypeManager.char_type)
759 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
760 if (target_type == TypeManager.float_type)
761 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
762 if (target_type == TypeManager.decimal_type)
763 return InternalTypeConstructor (tc, expr, target_type);
766 // decimal is taken care of by the op_Explicit methods.
772 // Performs an explicit conversion of the expression `expr' whose
773 // type is expr.Type to `target_type'.
775 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
778 Expression ne = ConvertImplicit (tc, expr, target_type);
783 ne = ConvertNumericExplicit (tc, expr, target_type);
791 void report (TypeContainer tc, int error, string s)
793 tc.RootContext.Report.Error (error, s);
796 static string ExprClassName (ExprClass c)
799 case ExprClass.Invalid:
801 case ExprClass.Value:
803 case ExprClass.Variable:
805 case ExprClass.Namespace:
809 case ExprClass.MethodGroup:
810 return "method group";
811 case ExprClass.PropertyAccess:
812 return "property access";
813 case ExprClass.EventAccess:
814 return "event access";
815 case ExprClass.IndexerAccess:
816 return "indexer access";
817 case ExprClass.Nothing:
820 throw new Exception ("Should not happen");
824 // Reports that we were expecting `expr' to be of class `expected'
826 protected void report118 (TypeContainer tc, Expression expr, string expected)
828 report (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
829 "' where an " + expected + " was expected");
834 // This is just a base class for expressions that can
835 // appear on statements (invocations, object creation,
836 // assignments, post/pre increment and decrement). The idea
837 // being that they would support an extra Emition interface that
838 // does not leave a result on the stack.
841 public abstract class ExpressionStatement : Expression {
844 // Requests the expression to be emitted in a `statement'
845 // context. This means that no new value is left on the
846 // stack after invoking this method (constrasted with
847 // Emit that will always leave a value on the stack).
849 public abstract void EmitStatement (EmitContext ec);
853 // This kind of cast is used to encapsulate the child
854 // whose type is child.Type into an expression that is
855 // reported to return "return_type". This is used to encapsulate
856 // expressions which have compatible types, but need to be dealt
857 // at higher levels with.
859 // For example, a "byte" expression could be encapsulated in one
860 // of these as an "unsigned int". The type for the expression
861 // would be "unsigned int".
865 public class EmptyCast : Expression {
866 protected Expression child;
868 public EmptyCast (Expression child, Type return_type)
870 ExprClass = child.ExprClass;
875 public override Expression Resolve (TypeContainer tc)
877 // This should never be invoked, we are born in fully
878 // initialized state.
883 public override void Emit (EmitContext ec)
890 // This kind of cast is used to encapsulate Value Types in objects.
892 // The effect of it is to box the value type emitted by the previous
895 public class BoxedCast : EmptyCast {
897 public BoxedCast (Expression expr, Type target_type)
898 : base (expr, target_type)
902 public override Expression Resolve (TypeContainer tc)
904 // This should never be invoked, we are born in fully
905 // initialized state.
910 public override void Emit (EmitContext ec)
913 ec.ig.Emit (OpCodes.Box, child.Type);
918 // This kind of cast is used to encapsulate a child expression
919 // that can be trivially converted to a target type using one or
920 // two opcodes. The opcodes are passed as arguments.
922 public class OpcodeCast : EmptyCast {
926 public OpcodeCast (Expression child, Type return_type, OpCode op)
927 : base (child, return_type)
931 second_valid = false;
934 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
935 : base (child, return_type)
943 public override Expression Resolve (TypeContainer tc)
945 // This should never be invoked, we are born in fully
946 // initialized state.
951 public override void Emit (EmitContext ec)
963 // Unary expressions.
967 // Unary implements unary expressions. It derives from
968 // ExpressionStatement becuase the pre/post increment/decrement
969 // operators can be used in a statement context.
971 public class Unary : ExpressionStatement {
972 public enum Operator {
973 Addition, Subtraction, Negate, BitComplement,
974 Indirection, AddressOf, PreIncrement,
975 PreDecrement, PostIncrement, PostDecrement
984 public Unary (Operator op, Expression expr, Location loc)
991 public Expression Expr {
1001 public Operator Oper {
1012 // Returns a stringified representation of the Operator
1017 case Operator.Addition:
1019 case Operator.Subtraction:
1021 case Operator.Negate:
1023 case Operator.BitComplement:
1025 case Operator.AddressOf:
1027 case Operator.Indirection:
1029 case Operator.PreIncrement : case Operator.PostIncrement :
1031 case Operator.PreDecrement : case Operator.PostDecrement :
1035 return oper.ToString ();
1038 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1040 if (expr.Type == target_type)
1043 return ConvertImplicit (tc, expr, target_type);
1046 void report23 (Report r, Type t)
1048 r.Error (23, "Operator " + OperName () + " cannot be applied to operand of type `" +
1049 TypeManager.CSharpName (t) + "'");
1053 // Returns whether an object of type `t' can be incremented
1054 // or decremented with add/sub (ie, basically whether we can
1055 // use pre-post incr-decr operations on it, but it is not a
1056 // System.Decimal, which we test elsewhere)
1058 static bool IsIncrementableNumber (Type t)
1060 return (t == TypeManager.sbyte_type) ||
1061 (t == TypeManager.byte_type) ||
1062 (t == TypeManager.short_type) ||
1063 (t == TypeManager.ushort_type) ||
1064 (t == TypeManager.int32_type) ||
1065 (t == TypeManager.uint32_type) ||
1066 (t == TypeManager.int64_type) ||
1067 (t == TypeManager.uint64_type) ||
1068 (t == TypeManager.char_type) ||
1069 (t.IsSubclassOf (TypeManager.enum_type)) ||
1070 (t == TypeManager.float_type) ||
1071 (t == TypeManager.double_type);
1074 Expression ResolveOperator (TypeContainer tc)
1076 Type expr_type = expr.Type;
1079 // Step 1: Perform Operator Overload location
1084 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1085 op_name = "op_Increment";
1086 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1087 op_name = "op_Decrement";
1089 op_name = "op_" + oper;
1091 mg = MemberLookup (tc.RootContext, expr_type, op_name, false);
1094 Arguments = new ArrayList ();
1095 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1097 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg, Arguments, location);
1098 if (method != null) {
1099 MethodInfo mi = (MethodInfo) method;
1101 type = mi.ReturnType;
1107 // Step 2: Default operations on CLI native types.
1110 // Only perform numeric promotions on:
1113 if (expr_type == null)
1116 if (oper == Operator.Negate){
1117 if (expr_type != TypeManager.bool_type) {
1118 report23 (tc.RootContext.Report, expr.Type);
1122 type = TypeManager.bool_type;
1126 if (oper == Operator.BitComplement) {
1127 if (!((expr_type == TypeManager.int32_type) ||
1128 (expr_type == TypeManager.uint32_type) ||
1129 (expr_type == TypeManager.int64_type) ||
1130 (expr_type == TypeManager.uint64_type) ||
1131 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1132 report23 (tc.RootContext.Report, expr.Type);
1139 if (oper == Operator.Addition) {
1141 // A plus in front of something is just a no-op, so return the child.
1147 // Deals with -literals
1148 // int operator- (int x)
1149 // long operator- (long x)
1150 // float operator- (float f)
1151 // double operator- (double d)
1152 // decimal operator- (decimal d)
1154 if (oper == Operator.Subtraction){
1156 // Fold a "- Constant" into a negative constant
1159 Expression e = null;
1162 // Is this a constant?
1164 if (expr is IntLiteral)
1165 e = new IntLiteral (-((IntLiteral) expr).Value);
1166 else if (expr is LongLiteral)
1167 e = new LongLiteral (-((LongLiteral) expr).Value);
1168 else if (expr is FloatLiteral)
1169 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1170 else if (expr is DoubleLiteral)
1171 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1172 else if (expr is DecimalLiteral)
1173 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1181 // Not a constant we can optimize, perform numeric
1182 // promotions to int, long, double.
1185 // The following is inneficient, because we call
1186 // ConvertImplicit too many times.
1188 // It is also not clear if we should convert to Float
1189 // or Double initially.
1191 if (expr_type == TypeManager.uint32_type){
1193 // FIXME: handle exception to this rule that
1194 // permits the int value -2147483648 (-2^31) to
1195 // bt written as a decimal interger literal
1197 type = TypeManager.int64_type;
1198 expr = ConvertImplicit (tc, expr, type);
1202 if (expr_type == TypeManager.uint64_type){
1204 // FIXME: Handle exception of `long value'
1205 // -92233720368547758087 (-2^63) to be written as
1206 // decimal integer literal.
1208 report23 (tc.RootContext.Report, expr_type);
1212 e = ConvertImplicit (tc, expr, TypeManager.int32_type);
1219 e = ConvertImplicit (tc, expr, TypeManager.int64_type);
1226 e = ConvertImplicit (tc, expr, TypeManager.double_type);
1233 report23 (tc.RootContext.Report, expr_type);
1238 // The operand of the prefix/postfix increment decrement operators
1239 // should be an expression that is classified as a variable,
1240 // a property access or an indexer access
1242 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1243 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1244 if (expr.ExprClass == ExprClass.Variable){
1245 if (IsIncrementableNumber (expr_type) ||
1246 expr_type == TypeManager.decimal_type){
1250 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1252 // FIXME: Verify that we have both get and set methods
1254 throw new Exception ("Implement me");
1255 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1257 // FIXME: Verify that we have both get and set methods
1259 throw new Exception ("Implement me");
1261 report118 (tc, expr, "variable, indexer or property access");
1265 tc.RootContext.Report.Error (187, "No such operator '" + OperName () +
1266 "' defined for type '" +
1267 TypeManager.CSharpName (expr_type) + "'");
1272 public override Expression Resolve (TypeContainer tc)
1274 expr = expr.Resolve (tc);
1279 return ResolveOperator (tc);
1282 public override void Emit (EmitContext ec)
1284 ILGenerator ig = ec.ig;
1285 Type expr_type = expr.Type;
1287 if (method != null) {
1289 // Note that operators are static anyway
1291 if (Arguments != null)
1292 Invocation.EmitArguments (ec, method, Arguments);
1295 // Post increment/decrement operations need a copy at this
1298 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1299 ig.Emit (OpCodes.Dup);
1302 ig.Emit (OpCodes.Call, (MethodInfo) method);
1305 // Pre Increment and Decrement operators
1307 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1308 ig.Emit (OpCodes.Dup);
1312 // Increment and Decrement should store the result
1314 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1315 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1316 ((LValue) expr).Store (ig);
1322 case Operator.Addition:
1323 throw new Exception ("This should be caught by Resolve");
1325 case Operator.Subtraction:
1327 ig.Emit (OpCodes.Neg);
1330 case Operator.Negate:
1332 ig.Emit (OpCodes.Ldc_I4_0);
1333 ig.Emit (OpCodes.Ceq);
1336 case Operator.BitComplement:
1338 ig.Emit (OpCodes.Not);
1341 case Operator.AddressOf:
1342 throw new Exception ("Not implemented yet");
1344 case Operator.Indirection:
1345 throw new Exception ("Not implemented yet");
1347 case Operator.PreIncrement:
1348 case Operator.PreDecrement:
1349 if (expr.ExprClass == ExprClass.Variable){
1351 // Resolve already verified that it is an "incrementable"
1354 ig.Emit (OpCodes.Ldc_I4_1);
1356 if (oper == Operator.PreDecrement)
1357 ig.Emit (OpCodes.Sub);
1359 ig.Emit (OpCodes.Add);
1360 ig.Emit (OpCodes.Dup);
1361 ((LValue) expr).Store (ig);
1363 throw new Exception ("Handle Indexers and Properties here");
1367 case Operator.PostIncrement:
1368 case Operator.PostDecrement:
1369 if (expr.ExprClass == ExprClass.Variable){
1371 // Resolve already verified that it is an "incrementable"
1374 ig.Emit (OpCodes.Dup);
1375 ig.Emit (OpCodes.Ldc_I4_1);
1377 if (oper == Operator.PostDecrement)
1378 ig.Emit (OpCodes.Sub);
1380 ig.Emit (OpCodes.Add);
1381 ((LValue) expr).Store (ig);
1383 throw new Exception ("Handle Indexers and Properties here");
1388 throw new Exception ("This should not happen: Operator = "
1389 + oper.ToString ());
1394 public override void EmitStatement (EmitContext ec)
1397 // FIXME: we should rewrite this code to generate
1398 // better code for ++ and -- as we know we wont need
1399 // the values on the stack
1402 ec.ig.Emit (OpCodes.Pop);
1406 public class Probe : Expression {
1407 public readonly string ProbeType;
1408 public readonly Operator Oper;
1412 public enum Operator {
1416 public Probe (Operator oper, Expression expr, string probe_type)
1419 ProbeType = probe_type;
1423 public Expression Expr {
1429 public override Expression Resolve (TypeContainer tc)
1431 probe_type = tc.LookupType (ProbeType, false);
1433 if (probe_type == null)
1436 expr = expr.Resolve (tc);
1438 type = TypeManager.bool_type;
1439 eclass = ExprClass.Value;
1444 public override void Emit (EmitContext ec)
1448 if (Oper == Operator.Is){
1449 ec.ig.Emit (OpCodes.Isinst, probe_type);
1451 throw new Exception ("Implement as");
1457 // This represents a typecast in the source language.
1459 // FIXME: Cast expressions have an unusual set of parsing
1460 // rules, we need to figure those out.
1462 public class Cast : Expression {
1466 public Cast (string cast_type, Expression expr)
1468 this.target_type = cast_type;
1472 public string TargetType {
1478 public Expression Expr {
1487 public override Expression Resolve (TypeContainer tc)
1489 type = tc.LookupType (target_type, false);
1490 eclass = ExprClass.Value;
1495 expr = ConvertExplicit (tc, expr, type);
1500 public override void Emit (EmitContext ec)
1503 // This one will never happen
1505 throw new Exception ("Should not happen");
1509 public class Binary : Expression {
1510 public enum Operator {
1511 Multiply, Division, Modulus,
1512 Addition, Subtraction,
1513 LeftShift, RightShift,
1514 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1515 Equality, Inequality,
1524 Expression left, right;
1526 ArrayList Arguments;
1530 public Binary (Operator oper, Expression left, Expression right, Location loc)
1535 this.location = loc;
1538 public Operator Oper {
1547 public Expression Left {
1556 public Expression Right {
1567 // Returns a stringified representation of the Operator
1572 case Operator.Multiply:
1574 case Operator.Division:
1576 case Operator.Modulus:
1578 case Operator.Addition:
1580 case Operator.Subtraction:
1582 case Operator.LeftShift:
1584 case Operator.RightShift:
1586 case Operator.LessThan:
1588 case Operator.GreaterThan:
1590 case Operator.LessThanOrEqual:
1592 case Operator.GreaterThanOrEqual:
1594 case Operator.Equality:
1596 case Operator.Inequality:
1598 case Operator.BitwiseAnd:
1600 case Operator.BitwiseOr:
1602 case Operator.ExclusiveOr:
1604 case Operator.LogicalOr:
1606 case Operator.LogicalAnd:
1610 return oper.ToString ();
1613 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1615 if (expr.Type == target_type)
1618 return ConvertImplicit (tc, expr, target_type);
1622 // Note that handling the case l == Decimal || r == Decimal
1623 // is taken care of by the Step 1 Operator Overload resolution.
1625 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1627 if (l == TypeManager.double_type || r == TypeManager.double_type){
1629 // If either operand is of type double, the other operand is
1630 // conveted to type double.
1632 if (r != TypeManager.double_type)
1633 right = ConvertImplicit (tc, right, TypeManager.double_type);
1634 if (l != TypeManager.double_type)
1635 left = ConvertImplicit (tc, left, TypeManager.double_type);
1637 type = TypeManager.double_type;
1638 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1640 // if either operand is of type float, th eother operand is
1641 // converd to type float.
1643 if (r != TypeManager.double_type)
1644 right = ConvertImplicit (tc, right, TypeManager.float_type);
1645 if (l != TypeManager.double_type)
1646 left = ConvertImplicit (tc, left, TypeManager.float_type);
1647 type = TypeManager.float_type;
1648 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1650 // If either operand is of type ulong, the other operand is
1651 // converted to type ulong. or an error ocurrs if the other
1652 // operand is of type sbyte, short, int or long
1656 if (l == TypeManager.uint64_type)
1658 else if (r == TypeManager.uint64_type)
1661 if ((other == TypeManager.sbyte_type) ||
1662 (other == TypeManager.short_type) ||
1663 (other == TypeManager.int32_type) ||
1664 (other == TypeManager.int64_type)){
1665 string oper = OperName ();
1667 tc.RootContext.Report.Error (34, "Operator `" + OperName ()
1668 + "' is ambiguous on operands of type `"
1669 + TypeManager.CSharpName (l) + "' "
1670 + "and `" + TypeManager.CSharpName (r)
1673 type = TypeManager.uint64_type;
1674 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1676 // If either operand is of type long, the other operand is converted
1679 if (l != TypeManager.int64_type)
1680 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1681 if (r != TypeManager.int64_type)
1682 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1684 type = TypeManager.int64_type;
1685 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1687 // If either operand is of type uint, and the other
1688 // operand is of type sbyte, short or int, othe operands are
1689 // converted to type long.
1693 if (l == TypeManager.uint32_type)
1695 else if (r == TypeManager.uint32_type)
1698 if ((other == TypeManager.sbyte_type) ||
1699 (other == TypeManager.short_type) ||
1700 (other == TypeManager.int32_type)){
1701 left = ForceConversion (tc, left, TypeManager.int64_type);
1702 right = ForceConversion (tc, right, TypeManager.int64_type);
1703 type = TypeManager.int64_type;
1706 // if either operand is of type uint, the other
1707 // operand is converd to type uint
1709 left = ForceConversion (tc, left, TypeManager.uint32_type);
1710 right = ForceConversion (tc, left, TypeManager.uint32_type);
1711 type = TypeManager.uint32_type;
1713 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1714 if (l != TypeManager.decimal_type)
1715 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1716 if (r != TypeManager.decimal_type)
1717 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1719 type = TypeManager.decimal_type;
1721 left = ForceConversion (tc, left, TypeManager.int32_type);
1722 right = ForceConversion (tc, right, TypeManager.int32_type);
1723 type = TypeManager.int32_type;
1727 void error19 (TypeContainer tc)
1729 tc.RootContext.Report.Error (
1731 "Operator " + OperName () + " cannot be applied to operands of type `" +
1732 TypeManager.CSharpName (left.Type) + "' and `" +
1733 TypeManager.CSharpName (right.Type) + "'");
1737 Expression CheckShiftArguments (TypeContainer tc)
1741 Type r = right.Type;
1743 e = ForceConversion (tc, right, TypeManager.int32_type);
1750 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1751 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1752 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1753 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1762 Expression ResolveOperator (TypeContainer tc)
1765 Type r = right.Type;
1768 // Step 1: Perform Operator Overload location
1770 Expression left_expr, right_expr;
1772 string op = "op_" + oper;
1774 left_expr = MemberLookup (tc.RootContext, l, op, false);
1776 right_expr = MemberLookup (tc.RootContext, r, op, false);
1778 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1780 Arguments = new ArrayList ();
1781 Arguments.Add (new Argument (left, Argument.AType.Expression));
1782 Arguments.Add (new Argument (right, Argument.AType.Expression));
1784 if (union != null) {
1785 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1786 if (method != null) {
1787 MethodInfo mi = (MethodInfo) method;
1789 type = mi.ReturnType;
1795 // Step 2: Default operations on CLI native types.
1798 // Only perform numeric promotions on:
1799 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1801 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1802 return CheckShiftArguments (tc);
1803 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1805 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1808 DoNumericPromotions (tc, l, r);
1810 if (left == null || right == null)
1813 if (oper == Operator.BitwiseAnd ||
1814 oper == Operator.BitwiseOr ||
1815 oper == Operator.ExclusiveOr){
1816 if (!((l == TypeManager.int32_type) ||
1817 (l == TypeManager.uint32_type) ||
1818 (l == TypeManager.int64_type) ||
1819 (l == TypeManager.uint64_type))){
1825 if (oper == Operator.Equality ||
1826 oper == Operator.Inequality ||
1827 oper == Operator.LessThanOrEqual ||
1828 oper == Operator.LessThan ||
1829 oper == Operator.GreaterThanOrEqual ||
1830 oper == Operator.GreaterThan){
1831 type = TypeManager.bool_type;
1837 public override Expression Resolve (TypeContainer tc)
1839 left = left.Resolve (tc);
1840 right = right.Resolve (tc);
1842 if (left == null || right == null)
1845 return ResolveOperator (tc);
1848 public bool IsBranchable ()
1850 if (oper == Operator.Equality ||
1851 oper == Operator.Inequality ||
1852 oper == Operator.LessThan ||
1853 oper == Operator.GreaterThan ||
1854 oper == Operator.LessThanOrEqual ||
1855 oper == Operator.GreaterThanOrEqual){
1862 // This entry point is used by routines that might want
1863 // to emit a brfalse/brtrue after an expression, and instead
1864 // they could use a more compact notation.
1866 // Typically the code would generate l.emit/r.emit, followed
1867 // by the comparission and then a brtrue/brfalse. The comparissions
1868 // are sometimes inneficient (there are not as complete as the branches
1869 // look for the hacks in Emit using double ceqs).
1871 // So for those cases we provide EmitBranchable that can emit the
1872 // branch with the test
1874 public void EmitBranchable (EmitContext ec, int target)
1877 bool close_target = false;
1883 case Operator.Equality:
1885 opcode = OpCodes.Beq_S;
1887 opcode = OpCodes.Beq;
1890 case Operator.Inequality:
1892 opcode = OpCodes.Bne_Un_S;
1894 opcode = OpCodes.Bne_Un;
1897 case Operator.LessThan:
1899 opcode = OpCodes.Blt_S;
1901 opcode = OpCodes.Blt;
1904 case Operator.GreaterThan:
1906 opcode = OpCodes.Bgt_S;
1908 opcode = OpCodes.Bgt;
1911 case Operator.LessThanOrEqual:
1913 opcode = OpCodes.Ble_S;
1915 opcode = OpCodes.Ble;
1918 case Operator.GreaterThanOrEqual:
1920 opcode = OpCodes.Bge_S;
1922 opcode = OpCodes.Ble;
1926 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1927 + oper.ToString ());
1930 ec.ig.Emit (opcode, target);
1933 public override void Emit (EmitContext ec)
1935 ILGenerator ig = ec.ig;
1937 Type r = right.Type;
1940 if (method != null) {
1942 // Note that operators are static anyway
1944 if (Arguments != null)
1945 Invocation.EmitArguments (ec, method, Arguments);
1947 if (method is MethodInfo)
1948 ig.Emit (OpCodes.Call, (MethodInfo) method);
1950 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1959 case Operator.Multiply:
1961 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1962 opcode = OpCodes.Mul_Ovf;
1963 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1964 opcode = OpCodes.Mul_Ovf_Un;
1966 opcode = OpCodes.Mul;
1968 opcode = OpCodes.Mul;
1972 case Operator.Division:
1973 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1974 opcode = OpCodes.Div_Un;
1976 opcode = OpCodes.Div;
1979 case Operator.Modulus:
1980 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1981 opcode = OpCodes.Rem_Un;
1983 opcode = OpCodes.Rem;
1986 case Operator.Addition:
1988 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1989 opcode = OpCodes.Add_Ovf;
1990 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1991 opcode = OpCodes.Add_Ovf_Un;
1993 opcode = OpCodes.Mul;
1995 opcode = OpCodes.Add;
1998 case Operator.Subtraction:
2000 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2001 opcode = OpCodes.Sub_Ovf;
2002 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2003 opcode = OpCodes.Sub_Ovf_Un;
2005 opcode = OpCodes.Sub;
2007 opcode = OpCodes.Sub;
2010 case Operator.RightShift:
2011 opcode = OpCodes.Shr;
2014 case Operator.LeftShift:
2015 opcode = OpCodes.Shl;
2018 case Operator.Equality:
2019 opcode = OpCodes.Ceq;
2022 case Operator.Inequality:
2023 ec.ig.Emit (OpCodes.Ceq);
2024 ec.ig.Emit (OpCodes.Ldc_I4_0);
2026 opcode = OpCodes.Ceq;
2029 case Operator.LessThan:
2030 opcode = OpCodes.Clt;
2033 case Operator.GreaterThan:
2034 opcode = OpCodes.Cgt;
2037 case Operator.LessThanOrEqual:
2038 ec.ig.Emit (OpCodes.Cgt);
2039 ec.ig.Emit (OpCodes.Ldc_I4_0);
2041 opcode = OpCodes.Ceq;
2044 case Operator.GreaterThanOrEqual:
2045 ec.ig.Emit (OpCodes.Clt);
2046 ec.ig.Emit (OpCodes.Ldc_I4_1);
2048 opcode = OpCodes.Sub;
2051 case Operator.LogicalOr:
2052 case Operator.BitwiseOr:
2053 opcode = OpCodes.Or;
2056 case Operator.LogicalAnd:
2057 case Operator.BitwiseAnd:
2058 opcode = OpCodes.And;
2061 case Operator.ExclusiveOr:
2062 opcode = OpCodes.Xor;
2066 throw new Exception ("This should not happen: Operator = "
2067 + oper.ToString ());
2074 public class Conditional : Expression {
2075 Expression expr, trueExpr, falseExpr;
2077 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
2080 this.trueExpr = trueExpr;
2081 this.falseExpr = falseExpr;
2084 public Expression Expr {
2090 public Expression TrueExpr {
2096 public Expression FalseExpr {
2102 public override Expression Resolve (TypeContainer tc)
2104 // FIXME: Implement;
2105 throw new Exception ("Unimplemented");
2109 public override void Emit (EmitContext ec)
2114 public class SimpleName : Expression {
2115 public readonly string Name;
2116 public readonly Location Location;
2118 public SimpleName (string name, Location l)
2125 // Checks whether we are trying to access an instance
2126 // property, method or field from a static body.
2128 Expression MemberStaticCheck (Report r, Expression e)
2130 if (e is FieldExpr){
2131 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2135 "An object reference is required " +
2136 "for the non-static field `"+Name+"'");
2139 } else if (e is MethodGroupExpr){
2140 // FIXME: Pending reorganization of MemberLookup
2141 // Basically at this point we should have the
2142 // best match already selected for us, and
2143 // we should only have to check a *single*
2144 // Method for its static on/off bit.
2146 } else if (e is PropertyExpr){
2147 if (!((PropertyExpr) e).IsStatic){
2149 "An object reference is required " +
2150 "for the non-static property access `"+
2160 // 7.5.2: Simple Names.
2162 // Local Variables and Parameters are handled at
2163 // parse time, so they never occur as SimpleNames.
2165 Expression ResolveSimpleName (TypeContainer tc)
2168 Report r = tc.RootContext.Report;
2170 e = MemberLookup (tc.RootContext, tc.TypeBuilder, Name, true);
2174 else if (e is FieldExpr){
2175 FieldExpr fe = (FieldExpr) e;
2177 if (!fe.FieldInfo.IsStatic)
2178 fe.Instance = new This ();
2181 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2182 return MemberStaticCheck (r, e);
2188 // Do step 3 of the Simple Name resolution.
2190 // FIXME: implement me.
2192 r.Error (103, Location, "The name `" + Name + "' does not exist in the class `" +
2199 // SimpleName needs to handle a multitude of cases:
2201 // simple_names and qualified_identifiers are placed on
2202 // the tree equally.
2204 public override Expression Resolve (TypeContainer tc)
2206 if (Name.IndexOf (".") != -1)
2207 return ResolveMemberAccess (tc, Name);
2209 return ResolveSimpleName (tc);
2212 public override void Emit (EmitContext ec)
2214 throw new Exception ("SimpleNames should be gone from the tree");
2219 // A simple interface that should be implemeneted by LValues
2221 public interface LValue {
2222 void Store (ILGenerator ig);
2225 public class LocalVariableReference : Expression, LValue {
2226 public readonly string Name;
2227 public readonly Block Block;
2229 public LocalVariableReference (Block block, string name)
2233 eclass = ExprClass.Variable;
2236 public VariableInfo VariableInfo {
2238 return Block.GetVariableInfo (Name);
2242 public override Expression Resolve (TypeContainer tc)
2244 VariableInfo vi = Block.GetVariableInfo (Name);
2246 type = vi.VariableType;
2250 public override void Emit (EmitContext ec)
2252 VariableInfo vi = VariableInfo;
2253 ILGenerator ig = ec.ig;
2258 ig.Emit (OpCodes.Ldloc_0);
2262 ig.Emit (OpCodes.Ldloc_1);
2266 ig.Emit (OpCodes.Ldloc_2);
2270 ig.Emit (OpCodes.Ldloc_3);
2275 ig.Emit (OpCodes.Ldloc_S, idx);
2277 ig.Emit (OpCodes.Ldloc, idx);
2282 public void Store (ILGenerator ig)
2284 VariableInfo vi = VariableInfo;
2289 ig.Emit (OpCodes.Stloc_0);
2293 ig.Emit (OpCodes.Stloc_1);
2297 ig.Emit (OpCodes.Stloc_2);
2301 ig.Emit (OpCodes.Stloc_3);
2306 ig.Emit (OpCodes.Stloc_S, idx);
2308 ig.Emit (OpCodes.Stloc, idx);
2314 public class ParameterReference : Expression, LValue {
2315 public readonly Parameters Pars;
2316 public readonly String Name;
2317 public readonly int Idx;
2319 public ParameterReference (Parameters pars, int idx, string name)
2324 eclass = ExprClass.Variable;
2327 public override Expression Resolve (TypeContainer tc)
2329 Type [] types = Pars.GetParameterInfo (tc);
2336 public override void Emit (EmitContext ec)
2339 ec.ig.Emit (OpCodes.Ldarg_S, Idx);
2341 ec.ig.Emit (OpCodes.Ldarg, Idx);
2344 public void Store (ILGenerator ig)
2347 ig.Emit (OpCodes.Starg_S, Idx);
2349 ig.Emit (OpCodes.Starg, Idx);
2355 // Used for arguments to New(), Invocation()
2357 public class Argument {
2364 public readonly AType Type;
2367 public Argument (Expression expr, AType type)
2373 public Expression Expr {
2383 public bool Resolve (TypeContainer tc)
2385 expr = expr.Resolve (tc);
2387 return expr != null;
2390 public void Emit (EmitContext ec)
2397 // Invocation of methods or delegates.
2399 public class Invocation : ExpressionStatement {
2400 public readonly ArrayList Arguments;
2401 public readonly Location Location;
2404 MethodBase method = null;
2406 static Hashtable method_parameter_cache;
2408 static Invocation ()
2410 method_parameter_cache = new Hashtable ();
2414 // arguments is an ArrayList, but we do not want to typecast,
2415 // as it might be null.
2417 // FIXME: only allow expr to be a method invocation or a
2418 // delegate invocation (7.5.5)
2420 public Invocation (Expression expr, ArrayList arguments, Location l)
2423 Arguments = arguments;
2427 public Expression Expr {
2434 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2435 /// compatible, and if so, how good is the match (in terms of
2436 /// "better conversions" (7.4.2.3).
2438 /// 0 is the best possible match.
2439 /// -1 represents a type mismatch.
2440 /// -2 represents a ref/out mismatch.
2442 static int Badness (Argument a, Type t)
2444 Expression argument_expr = a.Expr;
2445 Type argument_type = argument_expr.Type;
2447 if (argument_type == null){
2448 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2451 if (t == argument_type)
2455 // Now probe whether an implicit constant expression conversion
2458 // An implicit constant expression conversion permits the following
2461 // * A constant-expression of type `int' can be converted to type
2462 // sbyte, byute, short, ushort, uint, ulong provided the value of
2463 // of the expression is withing the range of the destination type.
2465 // * A constant-expression of type long can be converted to type
2466 // ulong, provided the value of the constant expression is not negative
2468 // FIXME: Note that this assumes that constant folding has
2469 // taken place. We dont do constant folding yet.
2472 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2473 IntLiteral ei = (IntLiteral) argument_expr;
2474 int value = ei.Value;
2476 if (t == TypeManager.sbyte_type){
2477 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2479 } else if (t == TypeManager.byte_type){
2480 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2482 } else if (t == TypeManager.short_type){
2483 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2485 } else if (t == TypeManager.ushort_type){
2486 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2488 } else if (t == TypeManager.uint32_type){
2490 // we can optimize this case: a positive int32
2491 // always fits on a uint32
2495 } else if (t == TypeManager.uint64_type){
2497 // we can optimize this case: a positive int32
2498 // always fits on a uint64
2503 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2504 LongLiteral ll = (LongLiteral) argument_expr;
2506 if (t == TypeManager.uint64_type)
2511 // FIXME: Implement user-defined implicit conversions here.
2512 // FIXME: Implement better conversion here.
2518 // Returns the Parameters (a ParameterData interface) for the
2521 static ParameterData GetParameterData (MethodBase mb)
2523 object pd = method_parameter_cache [mb];
2526 return (ParameterData) pd;
2528 if (mb is MethodBuilder || mb is ConstructorBuilder){
2529 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2531 InternalParameters ip = mc.ParameterInfo;
2532 method_parameter_cache [mb] = ip;
2534 return (ParameterData) ip;
2536 ParameterInfo [] pi = mb.GetParameters ();
2537 ReflectionParameters rp = new ReflectionParameters (pi);
2538 method_parameter_cache [mb] = rp;
2540 return (ParameterData) rp;
2544 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2546 // Locate user-defined implicit operators
2550 mg = MemberLookup (tc.RootContext, to, "op_Implicit", false);
2553 MethodGroupExpr me = (MethodGroupExpr) mg;
2555 for (int i = me.Methods.Length; i > 0;) {
2557 MethodBase mb = me.Methods [i];
2558 ParameterData pd = GetParameterData (mb);
2560 if (from == pd.ParameterType (0))
2565 mg = MemberLookup (tc.RootContext, from, "op_Implicit", false);
2568 MethodGroupExpr me = (MethodGroupExpr) mg;
2570 for (int i = me.Methods.Length; i > 0;) {
2572 MethodBase mb = me.Methods [i];
2573 MethodInfo mi = (MethodInfo) mb;
2575 if (mi.ReturnType == to)
2584 // Determines "better conversion" as specified in 7.4.2.3
2585 // Returns : 1 if a->p is better
2586 // 0 if a->q or neither is better
2588 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2591 Type argument_type = a.Expr.Type;
2592 Expression argument_expr = a.Expr;
2594 if (argument_type == null)
2595 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2601 if (argument_type == p)
2604 if (argument_type == q)
2608 // Now probe whether an implicit constant expression conversion
2611 // An implicit constant expression conversion permits the following
2614 // * A constant-expression of type `int' can be converted to type
2615 // sbyte, byute, short, ushort, uint, ulong provided the value of
2616 // of the expression is withing the range of the destination type.
2618 // * A constant-expression of type long can be converted to type
2619 // ulong, provided the value of the constant expression is not negative
2621 // FIXME: Note that this assumes that constant folding has
2622 // taken place. We dont do constant folding yet.
2625 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2626 IntLiteral ei = (IntLiteral) argument_expr;
2627 int value = ei.Value;
2629 if (p == TypeManager.sbyte_type){
2630 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2632 } else if (p == TypeManager.byte_type){
2633 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2635 } else if (p == TypeManager.short_type){
2636 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2638 } else if (p == TypeManager.ushort_type){
2639 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2641 } else if (p == TypeManager.uint32_type){
2643 // we can optimize this case: a positive int32
2644 // always fits on a uint32
2648 } else if (p == TypeManager.uint64_type){
2650 // we can optimize this case: a positive int32
2651 // always fits on a uint64
2656 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2657 LongLiteral ll = (LongLiteral) argument_expr;
2659 if (p == TypeManager.uint64_type){
2665 // User-defined Implicit conversions come here
2668 if (ConversionExists (tc, p, q) == true &&
2669 ConversionExists (tc, q, p) == false)
2672 if (p == TypeManager.sbyte_type)
2673 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2674 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2677 if (p == TypeManager.short_type)
2678 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2679 q == TypeManager.uint64_type)
2682 if (p == TypeManager.int32_type)
2683 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2686 if (p == TypeManager.int64_type)
2687 if (q == TypeManager.uint64_type)
2694 // Determines "Better function" and returns an integer indicating :
2695 // 0 if candidate ain't better
2696 // 1 if candidate is better than the current best match
2698 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2700 ParameterData candidate_pd = GetParameterData (candidate);
2701 ParameterData best_pd;
2707 argument_count = args.Count;
2709 if (candidate_pd.Count == 0 && argument_count == 0)
2713 if (candidate_pd.Count == argument_count) {
2715 for (int j = argument_count; j > 0;) {
2718 Argument a = (Argument) args [j];
2720 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2737 best_pd = GetParameterData (best);
2739 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2740 int rating1 = 0, rating2 = 0;
2742 for (int j = argument_count; j > 0;) {
2746 Argument a = (Argument) args [j];
2748 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2749 best_pd.ParameterType (j));
2750 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2751 candidate_pd.ParameterType (j));
2757 if (rating1 > rating2)
2766 public static string FullMethodDesc (MethodBase mb)
2768 StringBuilder sb = new StringBuilder (mb.Name);
2769 ParameterData pd = GetParameterData (mb);
2772 for (int i = pd.Count; i > 0;) {
2774 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2780 return sb.ToString ();
2783 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2786 if (mg1 != null || mg2 != null) {
2788 MethodGroupExpr left_set = null, right_set = null;
2789 int length1 = 0, length2 = 0;
2792 left_set = (MethodGroupExpr) mg1;
2793 length1 = left_set.Methods.Length;
2797 right_set = (MethodGroupExpr) mg2;
2798 length2 = right_set.Methods.Length;
2801 MemberInfo [] miset = new MemberInfo [length1 + length2];
2802 if (left_set != null)
2803 left_set.Methods.CopyTo (miset, 0);
2804 if (right_set != null)
2805 right_set.Methods.CopyTo (miset, length1);
2807 MethodGroupExpr union = new MethodGroupExpr (miset);
2818 // Find the Applicable Function Members (7.4.2.1)
2820 // me: Method Group expression with the members to select.
2821 // it might contain constructors or methods (or anything
2822 // that maps to a method).
2824 // Arguments: ArrayList containing resolved Argument objects.
2826 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2827 // that is the best match of me on Arguments.
2830 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2831 ArrayList Arguments, Location loc)
2833 ArrayList afm = new ArrayList ();
2834 int best_match_idx = -1;
2835 MethodBase method = null;
2838 for (int i = me.Methods.Length; i > 0; ){
2840 MethodBase candidate = me.Methods [i];
2843 x = BetterFunction (tc, Arguments, candidate, method);
2849 method = me.Methods [best_match_idx];
2853 if (best_match_idx != -1)
2856 // Now we see if we can at least find a method with the same number of arguments
2857 // and then try doing implicit conversion on the arguments
2859 if (Arguments == null)
2862 argument_count = Arguments.Count;
2864 ParameterData pd = null;
2866 for (int i = me.Methods.Length; i > 0;) {
2868 MethodBase mb = me.Methods [i];
2869 pd = GetParameterData (mb);
2871 if (pd.Count == argument_count) {
2873 method = me.Methods [best_match_idx];
2879 if (best_match_idx == -1)
2882 // And now convert implicitly, each argument to the required type
2884 pd = GetParameterData (method);
2886 for (int j = argument_count; j > 0;) {
2888 Argument a = (Argument) Arguments [j];
2889 Expression a_expr = a.Expr;
2891 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
2894 tc.RootContext.Report.Error (1502, loc,
2895 "The best overloaded match for method '" + FullMethodDesc (method) +
2896 "' has some invalid arguments");
2897 tc.RootContext.Report.Error (1503, loc,
2898 "Argument " + (j+1) +
2899 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
2900 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
2905 // Update the argument with the implicit conversion
2915 public override Expression Resolve (TypeContainer tc)
2918 // First, resolve the expression that is used to
2919 // trigger the invocation
2921 this.expr = expr.Resolve (tc);
2922 if (this.expr == null)
2925 if (!(this.expr is MethodGroupExpr)){
2926 report118 (tc, this.expr, "method group");
2931 // Next, evaluate all the expressions in the argument list
2933 if (Arguments != null){
2934 for (int i = Arguments.Count; i > 0;){
2936 Argument a = (Argument) Arguments [i];
2938 if (!a.Resolve (tc))
2943 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
2945 if (method == null){
2946 tc.RootContext.Report.Error (-6, Location,
2947 "Could not find any applicable function for this argument list");
2951 if (method is MethodInfo)
2952 type = ((MethodInfo)method).ReturnType;
2957 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
2961 if (Arguments != null)
2962 top = Arguments.Count;
2966 for (int i = 0; i < top; i++){
2967 Argument a = (Argument) Arguments [i];
2973 public override void Emit (EmitContext ec)
2975 bool is_static = method.IsStatic;
2978 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2981 // If this is ourselves, push "this"
2983 if (mg.InstanceExpression == null){
2984 ec.ig.Emit (OpCodes.Ldarg_0);
2987 // Push the instance expression
2989 mg.InstanceExpression.Emit (ec);
2993 if (Arguments != null)
2994 EmitArguments (ec, method, Arguments);
2997 if (method is MethodInfo)
2998 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3000 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3002 if (method is MethodInfo)
3003 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3005 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3009 public override void EmitStatement (EmitContext ec)
3014 // Pop the return value if there is one
3016 if (method is MethodInfo){
3017 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3018 ec.ig.Emit (OpCodes.Pop);
3023 public class New : ExpressionStatement {
3030 public readonly NType NewType;
3031 public readonly ArrayList Arguments;
3032 public readonly string RequestedType;
3033 // These are for the case when we have an array
3034 public readonly string Rank;
3035 public readonly ArrayList Indices;
3036 public readonly ArrayList Initializers;
3039 MethodBase method = null;
3041 public New (string requested_type, ArrayList arguments, Location loc)
3043 RequestedType = requested_type;
3044 Arguments = arguments;
3045 NewType = NType.Object;
3049 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3051 RequestedType = requested_type;
3054 Initializers = initializers;
3055 NewType = NType.Array;
3059 public override Expression Resolve (TypeContainer tc)
3061 type = tc.LookupType (RequestedType, false);
3068 ml = MemberLookup (tc.RootContext, type, ".ctor", false,
3069 MemberTypes.Constructor, AllBindingsFlags);
3071 if (! (ml is MethodGroupExpr)){
3073 // FIXME: Find proper error
3075 report118 (tc, ml, "method group");
3079 if (Arguments != null){
3080 for (int i = Arguments.Count; i > 0;){
3082 Argument a = (Argument) Arguments [i];
3084 if (!a.Resolve (tc))
3089 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3091 if (method == null) {
3092 tc.RootContext.Report.Error (-6, Location,
3093 "New invocation: Can not find a constructor for this argument list");
3100 public override void Emit (EmitContext ec)
3102 Invocation.EmitArguments (ec, method, Arguments);
3103 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3106 public override void EmitStatement (EmitContext ec)
3109 ec.ig.Emit (OpCodes.Pop);
3114 // Represents the `this' construct
3116 public class This : Expression, LValue {
3117 public override Expression Resolve (TypeContainer tc)
3119 eclass = ExprClass.Variable;
3120 type = tc.TypeBuilder;
3123 // FIXME: Verify that this is only used in instance contexts.
3128 public override void Emit (EmitContext ec)
3130 ec.ig.Emit (OpCodes.Ldarg_0);
3133 public void Store (ILGenerator ig)
3136 // Assignment to the "this" variable
3138 ig.Emit (OpCodes.Starg, 0);
3142 public class TypeOf : Expression {
3143 public readonly string QueriedType;
3145 public TypeOf (string queried_type)
3147 QueriedType = queried_type;
3150 public override Expression Resolve (TypeContainer tc)
3152 type = tc.LookupType (QueriedType, false);
3157 eclass = ExprClass.Type;
3161 public override void Emit (EmitContext ec)
3163 throw new Exception ("Implement me");
3164 // FIXME: Implement.
3168 public class SizeOf : Expression {
3169 public readonly string QueriedType;
3171 public SizeOf (string queried_type)
3173 this.QueriedType = queried_type;
3176 public override Expression Resolve (TypeContainer tc)
3178 // FIXME: Implement;
3179 throw new Exception ("Unimplemented");
3183 public override void Emit (EmitContext ec)
3185 throw new Exception ("Implement me");
3189 public class MemberAccess : Expression {
3190 public readonly string Identifier;
3192 Expression member_lookup;
3194 public MemberAccess (Expression expr, string id)
3200 public Expression Expr {
3206 public override Expression Resolve (TypeContainer tc)
3208 Expression new_expression = expr.Resolve (tc);
3210 if (new_expression == null)
3213 member_lookup = MemberLookup (tc.RootContext, expr.Type, Identifier, false);
3215 if (member_lookup is MethodGroupExpr){
3216 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3219 // Bind the instance expression to it
3221 // FIXME: This is a horrible way of detecting if it is
3222 // an instance expression. Figure out how to fix this.
3225 if (expr is LocalVariableReference ||
3226 expr is ParameterReference ||
3228 mg.InstanceExpression = expr;
3230 return member_lookup;
3231 } else if (member_lookup is FieldExpr){
3232 FieldExpr fe = (FieldExpr) member_lookup;
3236 return member_lookup;
3239 // FIXME: This should generate the proper node
3240 // ie, for a Property Access, it should like call it
3243 return member_lookup;
3246 public override void Emit (EmitContext ec)
3248 throw new Exception ("Implement me");
3254 // Nodes of type Namespace are created during the semantic
3255 // analysis to resolve member_access/qualified_identifier/simple_name
3258 // They are born `resolved'.
3260 public class NamespaceExpr : Expression {
3261 public readonly string Name;
3263 public NamespaceExpr (string name)
3266 eclass = ExprClass.Namespace;
3269 public override Expression Resolve (TypeContainer tc)
3274 public override void Emit (EmitContext ec)
3276 throw new Exception ("Namespace expressions should never be emitted");
3281 // Fully resolved expression that evaluates to a type
3283 public class TypeExpr : Expression {
3284 public TypeExpr (Type t)
3287 eclass = ExprClass.Type;
3290 override public Expression Resolve (TypeContainer tc)
3295 override public void Emit (EmitContext ec)
3297 throw new Exception ("Implement me");
3302 // MethodGroup Expression.
3304 // This is a fully resolved expression that evaluates to a type
3306 public class MethodGroupExpr : Expression {
3307 public readonly MethodBase [] Methods;
3308 Expression instance_expression = null;
3310 public MethodGroupExpr (MemberInfo [] mi)
3312 Methods = new MethodBase [mi.Length];
3313 mi.CopyTo (Methods, 0);
3314 eclass = ExprClass.MethodGroup;
3318 // `A method group may have associated an instance expression'
3320 public Expression InstanceExpression {
3322 return instance_expression;
3326 instance_expression = value;
3330 override public Expression Resolve (TypeContainer tc)
3335 override public void Emit (EmitContext ec)
3337 throw new Exception ("This should never be reached");
3341 public class BuiltinTypeAccess : Expression {
3342 public readonly string AccessBase;
3343 public readonly string Method;
3345 public BuiltinTypeAccess (string type, string method)
3347 System.Console.WriteLine ("DUDE! This type should be fully resolved!");
3352 public override Expression Resolve (TypeContainer tc)
3354 // FIXME: Implement;
3355 throw new Exception ("Unimplemented");
3359 public override void Emit (EmitContext ec)
3361 throw new Exception ("Unimplemented");
3366 // Fully resolved expression that evaluates to a Field
3368 public class FieldExpr : Expression, LValue {
3369 public readonly FieldInfo FieldInfo;
3370 public Expression Instance;
3372 public FieldExpr (FieldInfo fi)
3375 eclass = ExprClass.Variable;
3376 type = fi.FieldType;
3379 override public Expression Resolve (TypeContainer tc)
3381 if (!FieldInfo.IsStatic){
3382 if (Instance == null){
3383 throw new Exception ("non-static FieldExpr without instance var\n" +
3384 "You have to assign the Instance variable\n" +
3385 "Of the FieldExpr to set this\n");
3388 Instance = Instance.Resolve (tc);
3389 if (Instance == null)
3396 override public void Emit (EmitContext ec)
3398 ILGenerator ig = ec.ig;
3400 if (FieldInfo.IsStatic)
3401 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3405 ig.Emit (OpCodes.Ldfld, FieldInfo);
3409 public void Store (ILGenerator ig)
3411 if (FieldInfo.IsStatic)
3412 ig.Emit (OpCodes.Stsfld, FieldInfo);
3414 ig.Emit (OpCodes.Stfld, FieldInfo);
3419 // Fully resolved expression that evaluates to a Property
3421 public class PropertyExpr : Expression {
3422 public readonly PropertyInfo PropertyInfo;
3423 public readonly bool IsStatic;
3425 public PropertyExpr (PropertyInfo pi)
3428 eclass = ExprClass.PropertyAccess;
3431 MethodBase [] acc = pi.GetAccessors ();
3433 for (int i = 0; i < acc.Length; i++)
3434 if (acc [i].IsStatic)
3437 type = pi.PropertyType;
3440 override public Expression Resolve (TypeContainer tc)
3442 // We are born in resolved state.
3446 override public void Emit (EmitContext ec)
3448 // FIXME: Implement;
3449 throw new Exception ("Unimplemented");
3454 // Fully resolved expression that evaluates to a Property
3456 public class EventExpr : Expression {
3457 public readonly EventInfo EventInfo;
3459 public EventExpr (EventInfo ei)
3462 eclass = ExprClass.EventAccess;
3465 override public Expression Resolve (TypeContainer tc)
3467 // We are born in resolved state.
3471 override public void Emit (EmitContext ec)
3473 throw new Exception ("Implement me");
3474 // FIXME: Implement.
3478 public class CheckedExpr : Expression {
3480 public Expression Expr;
3482 public CheckedExpr (Expression e)
3487 public override Expression Resolve (TypeContainer tc)
3489 Expr = Expr.Resolve (tc);
3494 eclass = Expr.ExprClass;
3499 public override void Emit (EmitContext ec)
3501 bool last_check = ec.CheckState;
3503 ec.CheckState = true;
3505 ec.CheckState = last_check;
3510 public class UnCheckedExpr : Expression {
3512 public Expression Expr;
3514 public UnCheckedExpr (Expression e)
3519 public override Expression Resolve (TypeContainer tc)
3521 Expr = Expr.Resolve (tc);
3526 eclass = Expr.ExprClass;
3531 public override void Emit (EmitContext ec)
3533 bool last_check = ec.CheckState;
3535 ec.CheckState = false;
3537 ec.CheckState = last_check;
3542 public class ElementAccess : Expression {
3544 public readonly ArrayList Arguments;
3545 public readonly Expression Expr;
3547 public ElementAccess (Expression e, ArrayList e_list)
3553 public override Expression Resolve (TypeContainer tc)
3555 // FIXME: Implement;
3556 throw new Exception ("Unimplemented");
3560 public override void Emit (EmitContext ec)
3562 // FIXME : Implement !
3563 throw new Exception ("Unimplemented");
3568 public class BaseAccess : Expression {
3570 public enum BaseAccessType {
3575 public readonly BaseAccessType BAType;
3576 public readonly string Member;
3577 public readonly ArrayList Arguments;
3579 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3587 public override Expression Resolve (TypeContainer tc)
3589 // FIXME: Implement;
3590 throw new Exception ("Unimplemented");
3594 public override void Emit (EmitContext ec)
3596 throw new Exception ("Unimplemented");
3600 public class UserImplicitCast : Expression {
3605 ArrayList arguments;
3607 public UserImplicitCast (Expression source, Type target)
3609 this.source = source;
3610 this.target = target;
3613 public override Expression Resolve (TypeContainer tc)
3615 source = source.Resolve (tc);
3620 Expression mg1, mg2;
3621 MethodGroupExpr union;
3624 mg1 = MemberLookup (tc.RootContext, source.Type, "op_Implicit", false);
3625 mg2 = MemberLookup (tc.RootContext, target, "op_Implicit", false);
3627 union = Invocation.MakeUnionSet (mg1, mg2);
3629 arguments = new ArrayList ();
3630 arguments.Add (new Argument (source, Argument.AType.Expression));
3632 if (union != null) {
3633 method = Invocation.OverloadResolve (tc, union, arguments,
3634 new Location ("FIXME", 1, 1));
3636 if (method != null) {
3637 mi = (MethodInfo) method;
3639 if (mi.ReturnType == target) {
3640 type = mi.ReturnType;
3646 if (target == TypeManager.bool_type) {
3648 mg1 = MemberLookup (tc.RootContext, source.Type, "op_True", false);
3649 mg2 = MemberLookup (tc.RootContext, target, "op_True", false);
3651 union = Invocation.MakeUnionSet (mg1, mg2);
3656 method = Invocation.OverloadResolve (tc, union, arguments,
3657 new Location ("FIXME", 1, 1));
3659 if (method != null) {
3660 mi = (MethodInfo) method;
3662 if (mi.ReturnType == target) {
3663 type = mi.ReturnType;
3672 public static bool CanConvert (TypeContainer tc, Expression source, Type target)
3674 source = source.Resolve (tc);
3679 Expression mg1, mg2;
3681 ArrayList arguments;
3683 mg1 = MemberLookup (tc.RootContext, source.Type, "op_Implicit", false);
3684 mg2 = MemberLookup (tc.RootContext, target, "op_Implicit", false);
3686 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3688 arguments = new ArrayList ();
3689 arguments.Add (new Argument (source, Argument.AType.Expression));
3691 if (union != null) {
3693 method = Invocation.OverloadResolve (tc, union, arguments,
3694 new Location ("FIXME", 1, 1));
3695 if (method != null) {
3696 MethodInfo mi = (MethodInfo) method;
3698 if (mi.ReturnType == target)
3703 // If we have a boolean type, we need to check for the True
3704 // and False operators too.
3706 if (target == TypeManager.bool_type) {
3708 mg1 = MemberLookup (tc.RootContext, source.Type, "op_True", false);
3709 mg2 = MemberLookup (tc.RootContext, target, "op_True", false);
3711 union = Invocation.MakeUnionSet (mg1, mg2);
3716 method = Invocation.OverloadResolve (tc, union, arguments,
3717 new Location ("FIXME", 1, 1));
3718 if (method != null) {
3719 MethodInfo mi = (MethodInfo) method;
3721 if (mi.ReturnType == target)
3730 public override void Emit (EmitContext ec)
3732 ILGenerator ig = ec.ig;
3734 if (method != null) {
3736 // Note that operators are static anyway
3738 if (arguments != null)
3739 Invocation.EmitArguments (ec, method, arguments);
3741 if (method is MethodInfo)
3742 ig.Emit (OpCodes.Call, (MethodInfo) method);
3744 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3749 throw new Exception ("Implement me");