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");
1456 public class Cast : Expression {
1460 public Cast (string cast_type, Expression expr)
1462 this.target_type = cast_type;
1466 public string TargetType {
1472 public Expression Expr {
1481 public override Expression Resolve (TypeContainer tc)
1483 type = tc.LookupType (target_type, false);
1484 eclass = ExprClass.Value;
1489 expr = ConvertExplicit (tc, expr, type);
1494 public override void Emit (EmitContext ec)
1497 // This one will never happen
1499 throw new Exception ("Should not happen");
1503 public class Binary : Expression {
1504 public enum Operator {
1505 Multiply, Division, Modulus,
1506 Addition, Subtraction,
1507 LeftShift, RightShift,
1508 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1509 Equality, Inequality,
1518 Expression left, right;
1520 ArrayList Arguments;
1524 public Binary (Operator oper, Expression left, Expression right, Location loc)
1529 this.location = loc;
1532 public Operator Oper {
1541 public Expression Left {
1550 public Expression Right {
1561 // Returns a stringified representation of the Operator
1566 case Operator.Multiply:
1568 case Operator.Division:
1570 case Operator.Modulus:
1572 case Operator.Addition:
1574 case Operator.Subtraction:
1576 case Operator.LeftShift:
1578 case Operator.RightShift:
1580 case Operator.LessThan:
1582 case Operator.GreaterThan:
1584 case Operator.LessThanOrEqual:
1586 case Operator.GreaterThanOrEqual:
1588 case Operator.Equality:
1590 case Operator.Inequality:
1592 case Operator.BitwiseAnd:
1594 case Operator.BitwiseOr:
1596 case Operator.ExclusiveOr:
1598 case Operator.LogicalOr:
1600 case Operator.LogicalAnd:
1604 return oper.ToString ();
1607 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1609 if (expr.Type == target_type)
1612 return ConvertImplicit (tc, expr, target_type);
1616 // Note that handling the case l == Decimal || r == Decimal
1617 // is taken care of by the Step 1 Operator Overload resolution.
1619 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1621 if (l == TypeManager.double_type || r == TypeManager.double_type){
1623 // If either operand is of type double, the other operand is
1624 // conveted to type double.
1626 if (r != TypeManager.double_type)
1627 right = ConvertImplicit (tc, right, TypeManager.double_type);
1628 if (l != TypeManager.double_type)
1629 left = ConvertImplicit (tc, left, TypeManager.double_type);
1631 type = TypeManager.double_type;
1632 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1634 // if either operand is of type float, th eother operand is
1635 // converd to type float.
1637 if (r != TypeManager.double_type)
1638 right = ConvertImplicit (tc, right, TypeManager.float_type);
1639 if (l != TypeManager.double_type)
1640 left = ConvertImplicit (tc, left, TypeManager.float_type);
1641 type = TypeManager.float_type;
1642 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1644 // If either operand is of type ulong, the other operand is
1645 // converted to type ulong. or an error ocurrs if the other
1646 // operand is of type sbyte, short, int or long
1650 if (l == TypeManager.uint64_type)
1652 else if (r == TypeManager.uint64_type)
1655 if ((other == TypeManager.sbyte_type) ||
1656 (other == TypeManager.short_type) ||
1657 (other == TypeManager.int32_type) ||
1658 (other == TypeManager.int64_type)){
1659 string oper = OperName ();
1661 tc.RootContext.Report.Error (34, "Operator `" + OperName ()
1662 + "' is ambiguous on operands of type `"
1663 + TypeManager.CSharpName (l) + "' "
1664 + "and `" + TypeManager.CSharpName (r)
1667 type = TypeManager.uint64_type;
1668 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1670 // If either operand is of type long, the other operand is converted
1673 if (l != TypeManager.int64_type)
1674 left = ConvertImplicit (tc, left, TypeManager.int64_type);
1675 if (r != TypeManager.int64_type)
1676 right = ConvertImplicit (tc, right, TypeManager.int64_type);
1678 type = TypeManager.int64_type;
1679 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1681 // If either operand is of type uint, and the other
1682 // operand is of type sbyte, short or int, othe operands are
1683 // converted to type long.
1687 if (l == TypeManager.uint32_type)
1689 else if (r == TypeManager.uint32_type)
1692 if ((other == TypeManager.sbyte_type) ||
1693 (other == TypeManager.short_type) ||
1694 (other == TypeManager.int32_type)){
1695 left = ForceConversion (tc, left, TypeManager.int64_type);
1696 right = ForceConversion (tc, right, TypeManager.int64_type);
1697 type = TypeManager.int64_type;
1700 // if either operand is of type uint, the other
1701 // operand is converd to type uint
1703 left = ForceConversion (tc, left, TypeManager.uint32_type);
1704 right = ForceConversion (tc, left, TypeManager.uint32_type);
1705 type = TypeManager.uint32_type;
1707 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1708 if (l != TypeManager.decimal_type)
1709 left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1710 if (r != TypeManager.decimal_type)
1711 right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1713 type = TypeManager.decimal_type;
1715 left = ForceConversion (tc, left, TypeManager.int32_type);
1716 right = ForceConversion (tc, right, TypeManager.int32_type);
1717 type = TypeManager.int32_type;
1721 void error19 (TypeContainer tc)
1723 tc.RootContext.Report.Error (
1725 "Operator " + OperName () + " cannot be applied to operands of type `" +
1726 TypeManager.CSharpName (left.Type) + "' and `" +
1727 TypeManager.CSharpName (right.Type) + "'");
1731 Expression CheckShiftArguments (TypeContainer tc)
1735 Type r = right.Type;
1737 e = ForceConversion (tc, right, TypeManager.int32_type);
1744 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1745 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1746 ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1747 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1756 Expression ResolveOperator (TypeContainer tc)
1759 Type r = right.Type;
1762 // Step 1: Perform Operator Overload location
1764 Expression left_expr, right_expr;
1766 string op = "op_" + oper;
1768 left_expr = MemberLookup (tc.RootContext, l, op, false);
1770 right_expr = MemberLookup (tc.RootContext, r, op, false);
1772 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1774 Arguments = new ArrayList ();
1775 Arguments.Add (new Argument (left, Argument.AType.Expression));
1776 Arguments.Add (new Argument (right, Argument.AType.Expression));
1778 if (union != null) {
1779 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1780 if (method != null) {
1781 MethodInfo mi = (MethodInfo) method;
1783 type = mi.ReturnType;
1789 // Step 2: Default operations on CLI native types.
1792 // Only perform numeric promotions on:
1793 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1795 if (oper == Operator.LeftShift || oper == Operator.RightShift){
1796 return CheckShiftArguments (tc);
1797 } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1799 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1802 DoNumericPromotions (tc, l, r);
1804 if (left == null || right == null)
1807 if (oper == Operator.BitwiseAnd ||
1808 oper == Operator.BitwiseOr ||
1809 oper == Operator.ExclusiveOr){
1810 if (!((l == TypeManager.int32_type) ||
1811 (l == TypeManager.uint32_type) ||
1812 (l == TypeManager.int64_type) ||
1813 (l == TypeManager.uint64_type))){
1819 if (oper == Operator.Equality ||
1820 oper == Operator.Inequality ||
1821 oper == Operator.LessThanOrEqual ||
1822 oper == Operator.LessThan ||
1823 oper == Operator.GreaterThanOrEqual ||
1824 oper == Operator.GreaterThan){
1825 type = TypeManager.bool_type;
1831 public override Expression Resolve (TypeContainer tc)
1833 left = left.Resolve (tc);
1834 right = right.Resolve (tc);
1836 if (left == null || right == null)
1839 return ResolveOperator (tc);
1842 public bool IsBranchable ()
1844 if (oper == Operator.Equality ||
1845 oper == Operator.Inequality ||
1846 oper == Operator.LessThan ||
1847 oper == Operator.GreaterThan ||
1848 oper == Operator.LessThanOrEqual ||
1849 oper == Operator.GreaterThanOrEqual){
1856 // This entry point is used by routines that might want
1857 // to emit a brfalse/brtrue after an expression, and instead
1858 // they could use a more compact notation.
1860 // Typically the code would generate l.emit/r.emit, followed
1861 // by the comparission and then a brtrue/brfalse. The comparissions
1862 // are sometimes inneficient (there are not as complete as the branches
1863 // look for the hacks in Emit using double ceqs).
1865 // So for those cases we provide EmitBranchable that can emit the
1866 // branch with the test
1868 public void EmitBranchable (EmitContext ec, int target)
1871 bool close_target = false;
1877 case Operator.Equality:
1879 opcode = OpCodes.Beq_S;
1881 opcode = OpCodes.Beq;
1884 case Operator.Inequality:
1886 opcode = OpCodes.Bne_Un_S;
1888 opcode = OpCodes.Bne_Un;
1891 case Operator.LessThan:
1893 opcode = OpCodes.Blt_S;
1895 opcode = OpCodes.Blt;
1898 case Operator.GreaterThan:
1900 opcode = OpCodes.Bgt_S;
1902 opcode = OpCodes.Bgt;
1905 case Operator.LessThanOrEqual:
1907 opcode = OpCodes.Ble_S;
1909 opcode = OpCodes.Ble;
1912 case Operator.GreaterThanOrEqual:
1914 opcode = OpCodes.Bge_S;
1916 opcode = OpCodes.Ble;
1920 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1921 + oper.ToString ());
1924 ec.ig.Emit (opcode, target);
1927 public override void Emit (EmitContext ec)
1929 ILGenerator ig = ec.ig;
1931 Type r = right.Type;
1934 if (method != null) {
1936 // Note that operators are static anyway
1938 if (Arguments != null)
1939 Invocation.EmitArguments (ec, method, Arguments);
1941 if (method is MethodInfo)
1942 ig.Emit (OpCodes.Call, (MethodInfo) method);
1944 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1953 case Operator.Multiply:
1955 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1956 opcode = OpCodes.Mul_Ovf;
1957 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1958 opcode = OpCodes.Mul_Ovf_Un;
1960 opcode = OpCodes.Mul;
1962 opcode = OpCodes.Mul;
1966 case Operator.Division:
1967 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1968 opcode = OpCodes.Div_Un;
1970 opcode = OpCodes.Div;
1973 case Operator.Modulus:
1974 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1975 opcode = OpCodes.Rem_Un;
1977 opcode = OpCodes.Rem;
1980 case Operator.Addition:
1982 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1983 opcode = OpCodes.Add_Ovf;
1984 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1985 opcode = OpCodes.Add_Ovf_Un;
1987 opcode = OpCodes.Mul;
1989 opcode = OpCodes.Add;
1992 case Operator.Subtraction:
1994 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1995 opcode = OpCodes.Sub_Ovf;
1996 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1997 opcode = OpCodes.Sub_Ovf_Un;
1999 opcode = OpCodes.Sub;
2001 opcode = OpCodes.Sub;
2004 case Operator.RightShift:
2005 opcode = OpCodes.Shr;
2008 case Operator.LeftShift:
2009 opcode = OpCodes.Shl;
2012 case Operator.Equality:
2013 opcode = OpCodes.Ceq;
2016 case Operator.Inequality:
2017 ec.ig.Emit (OpCodes.Ceq);
2018 ec.ig.Emit (OpCodes.Ldc_I4_0);
2020 opcode = OpCodes.Ceq;
2023 case Operator.LessThan:
2024 opcode = OpCodes.Clt;
2027 case Operator.GreaterThan:
2028 opcode = OpCodes.Cgt;
2031 case Operator.LessThanOrEqual:
2032 ec.ig.Emit (OpCodes.Cgt);
2033 ec.ig.Emit (OpCodes.Ldc_I4_0);
2035 opcode = OpCodes.Ceq;
2038 case Operator.GreaterThanOrEqual:
2039 ec.ig.Emit (OpCodes.Clt);
2040 ec.ig.Emit (OpCodes.Ldc_I4_1);
2042 opcode = OpCodes.Sub;
2045 case Operator.LogicalOr:
2046 case Operator.BitwiseOr:
2047 opcode = OpCodes.Or;
2050 case Operator.LogicalAnd:
2051 case Operator.BitwiseAnd:
2052 opcode = OpCodes.And;
2055 case Operator.ExclusiveOr:
2056 opcode = OpCodes.Xor;
2060 throw new Exception ("This should not happen: Operator = "
2061 + oper.ToString ());
2068 public class Conditional : Expression {
2069 Expression expr, trueExpr, falseExpr;
2071 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
2074 this.trueExpr = trueExpr;
2075 this.falseExpr = falseExpr;
2078 public Expression Expr {
2084 public Expression TrueExpr {
2090 public Expression FalseExpr {
2096 public override Expression Resolve (TypeContainer tc)
2098 // FIXME: Implement;
2099 throw new Exception ("Unimplemented");
2103 public override void Emit (EmitContext ec)
2108 public class SimpleName : Expression {
2109 public readonly string Name;
2110 public readonly Location Location;
2112 public SimpleName (string name, Location l)
2119 // Checks whether we are trying to access an instance
2120 // property, method or field from a static body.
2122 Expression MemberStaticCheck (Report r, Expression e)
2124 if (e is FieldExpr){
2125 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2129 "An object reference is required " +
2130 "for the non-static field `"+Name+"'");
2133 } else if (e is MethodGroupExpr){
2134 // FIXME: Pending reorganization of MemberLookup
2135 // Basically at this point we should have the
2136 // best match already selected for us, and
2137 // we should only have to check a *single*
2138 // Method for its static on/off bit.
2140 } else if (e is PropertyExpr){
2141 if (!((PropertyExpr) e).IsStatic){
2143 "An object reference is required " +
2144 "for the non-static property access `"+
2154 // 7.5.2: Simple Names.
2156 // Local Variables and Parameters are handled at
2157 // parse time, so they never occur as SimpleNames.
2159 Expression ResolveSimpleName (TypeContainer tc)
2162 Report r = tc.RootContext.Report;
2164 e = MemberLookup (tc.RootContext, tc.TypeBuilder, Name, true);
2168 else if (e is FieldExpr){
2169 FieldExpr fe = (FieldExpr) e;
2171 if (!fe.FieldInfo.IsStatic)
2172 fe.Instance = new This ();
2175 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2176 return MemberStaticCheck (r, e);
2182 // Do step 3 of the Simple Name resolution.
2184 // FIXME: implement me.
2186 r.Error (103, Location, "The name `" + Name + "' does not exist in the class `" +
2193 // SimpleName needs to handle a multitude of cases:
2195 // simple_names and qualified_identifiers are placed on
2196 // the tree equally.
2198 public override Expression Resolve (TypeContainer tc)
2200 if (Name.IndexOf (".") != -1)
2201 return ResolveMemberAccess (tc, Name);
2203 return ResolveSimpleName (tc);
2206 public override void Emit (EmitContext ec)
2208 throw new Exception ("SimpleNames should be gone from the tree");
2213 // A simple interface that should be implemeneted by LValues
2215 public interface LValue {
2216 void Store (ILGenerator ig);
2219 public class LocalVariableReference : Expression, LValue {
2220 public readonly string Name;
2221 public readonly Block Block;
2223 public LocalVariableReference (Block block, string name)
2227 eclass = ExprClass.Variable;
2230 public VariableInfo VariableInfo {
2232 return Block.GetVariableInfo (Name);
2236 public override Expression Resolve (TypeContainer tc)
2238 VariableInfo vi = Block.GetVariableInfo (Name);
2240 type = vi.VariableType;
2244 public override void Emit (EmitContext ec)
2246 VariableInfo vi = VariableInfo;
2247 ILGenerator ig = ec.ig;
2252 ig.Emit (OpCodes.Ldloc_0);
2256 ig.Emit (OpCodes.Ldloc_1);
2260 ig.Emit (OpCodes.Ldloc_2);
2264 ig.Emit (OpCodes.Ldloc_3);
2269 ig.Emit (OpCodes.Ldloc_S, idx);
2271 ig.Emit (OpCodes.Ldloc, idx);
2276 public void Store (ILGenerator ig)
2278 VariableInfo vi = VariableInfo;
2283 ig.Emit (OpCodes.Stloc_0);
2287 ig.Emit (OpCodes.Stloc_1);
2291 ig.Emit (OpCodes.Stloc_2);
2295 ig.Emit (OpCodes.Stloc_3);
2300 ig.Emit (OpCodes.Stloc_S, idx);
2302 ig.Emit (OpCodes.Stloc, idx);
2308 public class ParameterReference : Expression, LValue {
2309 public readonly Parameters Pars;
2310 public readonly String Name;
2311 public readonly int Idx;
2313 public ParameterReference (Parameters pars, int idx, string name)
2318 eclass = ExprClass.Variable;
2321 public override Expression Resolve (TypeContainer tc)
2323 Type [] types = Pars.GetParameterInfo (tc);
2330 public override void Emit (EmitContext ec)
2333 ec.ig.Emit (OpCodes.Ldarg_S, Idx);
2335 ec.ig.Emit (OpCodes.Ldarg, Idx);
2338 public void Store (ILGenerator ig)
2341 ig.Emit (OpCodes.Starg_S, Idx);
2343 ig.Emit (OpCodes.Starg, Idx);
2349 // Used for arguments to New(), Invocation()
2351 public class Argument {
2358 public readonly AType Type;
2361 public Argument (Expression expr, AType type)
2367 public Expression Expr {
2377 public bool Resolve (TypeContainer tc)
2379 expr = expr.Resolve (tc);
2381 return expr != null;
2384 public void Emit (EmitContext ec)
2391 // Invocation of methods or delegates.
2393 public class Invocation : ExpressionStatement {
2394 public readonly ArrayList Arguments;
2395 public readonly Location Location;
2398 MethodBase method = null;
2400 static Hashtable method_parameter_cache;
2402 static Invocation ()
2404 method_parameter_cache = new Hashtable ();
2408 // arguments is an ArrayList, but we do not want to typecast,
2409 // as it might be null.
2411 // FIXME: only allow expr to be a method invocation or a
2412 // delegate invocation (7.5.5)
2414 public Invocation (Expression expr, ArrayList arguments, Location l)
2417 Arguments = arguments;
2421 public Expression Expr {
2428 /// Computes whether Argument `a' and the Type t of the ParameterInfo `pi' are
2429 /// compatible, and if so, how good is the match (in terms of
2430 /// "better conversions" (7.4.2.3).
2432 /// 0 is the best possible match.
2433 /// -1 represents a type mismatch.
2434 /// -2 represents a ref/out mismatch.
2436 static int Badness (Argument a, Type t)
2438 Expression argument_expr = a.Expr;
2439 Type argument_type = argument_expr.Type;
2441 if (argument_type == null){
2442 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2445 if (t == argument_type)
2449 // Now probe whether an implicit constant expression conversion
2452 // An implicit constant expression conversion permits the following
2455 // * A constant-expression of type `int' can be converted to type
2456 // sbyte, byute, short, ushort, uint, ulong provided the value of
2457 // of the expression is withing the range of the destination type.
2459 // * A constant-expression of type long can be converted to type
2460 // ulong, provided the value of the constant expression is not negative
2462 // FIXME: Note that this assumes that constant folding has
2463 // taken place. We dont do constant folding yet.
2466 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2467 IntLiteral ei = (IntLiteral) argument_expr;
2468 int value = ei.Value;
2470 if (t == TypeManager.sbyte_type){
2471 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2473 } else if (t == TypeManager.byte_type){
2474 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2476 } else if (t == TypeManager.short_type){
2477 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2479 } else if (t == TypeManager.ushort_type){
2480 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2482 } else if (t == TypeManager.uint32_type){
2484 // we can optimize this case: a positive int32
2485 // always fits on a uint32
2489 } else if (t == TypeManager.uint64_type){
2491 // we can optimize this case: a positive int32
2492 // always fits on a uint64
2497 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2498 LongLiteral ll = (LongLiteral) argument_expr;
2500 if (t == TypeManager.uint64_type)
2505 // FIXME: Implement user-defined implicit conversions here.
2506 // FIXME: Implement better conversion here.
2512 // Returns the Parameters (a ParameterData interface) for the
2515 static ParameterData GetParameterData (MethodBase mb)
2517 object pd = method_parameter_cache [mb];
2520 return (ParameterData) pd;
2522 if (mb is MethodBuilder || mb is ConstructorBuilder){
2523 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2525 InternalParameters ip = mc.ParameterInfo;
2526 method_parameter_cache [mb] = ip;
2528 return (ParameterData) ip;
2530 ParameterInfo [] pi = mb.GetParameters ();
2531 ReflectionParameters rp = new ReflectionParameters (pi);
2532 method_parameter_cache [mb] = rp;
2534 return (ParameterData) rp;
2538 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2540 // Locate user-defined implicit operators
2544 mg = MemberLookup (tc.RootContext, to, "op_Implicit", false);
2547 MethodGroupExpr me = (MethodGroupExpr) mg;
2549 for (int i = me.Methods.Length; i > 0;) {
2551 MethodBase mb = me.Methods [i];
2552 ParameterData pd = GetParameterData (mb);
2554 if (from == pd.ParameterType (0))
2559 mg = MemberLookup (tc.RootContext, from, "op_Implicit", false);
2562 MethodGroupExpr me = (MethodGroupExpr) mg;
2564 for (int i = me.Methods.Length; i > 0;) {
2566 MethodBase mb = me.Methods [i];
2567 MethodInfo mi = (MethodInfo) mb;
2569 if (mi.ReturnType == to)
2578 // Determines "better conversion" as specified in 7.4.2.3
2579 // Returns : 1 if a->p is better
2580 // 0 if a->q or neither is better
2582 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2585 Type argument_type = a.Expr.Type;
2586 Expression argument_expr = a.Expr;
2588 if (argument_type == null)
2589 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2595 if (argument_type == p)
2598 if (argument_type == q)
2602 // Now probe whether an implicit constant expression conversion
2605 // An implicit constant expression conversion permits the following
2608 // * A constant-expression of type `int' can be converted to type
2609 // sbyte, byute, short, ushort, uint, ulong provided the value of
2610 // of the expression is withing the range of the destination type.
2612 // * A constant-expression of type long can be converted to type
2613 // ulong, provided the value of the constant expression is not negative
2615 // FIXME: Note that this assumes that constant folding has
2616 // taken place. We dont do constant folding yet.
2619 if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2620 IntLiteral ei = (IntLiteral) argument_expr;
2621 int value = ei.Value;
2623 if (p == TypeManager.sbyte_type){
2624 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2626 } else if (p == TypeManager.byte_type){
2627 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2629 } else if (p == TypeManager.short_type){
2630 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2632 } else if (p == TypeManager.ushort_type){
2633 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2635 } else if (p == TypeManager.uint32_type){
2637 // we can optimize this case: a positive int32
2638 // always fits on a uint32
2642 } else if (p == TypeManager.uint64_type){
2644 // we can optimize this case: a positive int32
2645 // always fits on a uint64
2650 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2651 LongLiteral ll = (LongLiteral) argument_expr;
2653 if (p == TypeManager.uint64_type){
2659 // User-defined Implicit conversions come here
2662 if (ConversionExists (tc, p, q) == true &&
2663 ConversionExists (tc, q, p) == false)
2666 if (p == TypeManager.sbyte_type)
2667 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2668 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2671 if (p == TypeManager.short_type)
2672 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2673 q == TypeManager.uint64_type)
2676 if (p == TypeManager.int32_type)
2677 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2680 if (p == TypeManager.int64_type)
2681 if (q == TypeManager.uint64_type)
2688 // Determines "Better function" and returns an integer indicating :
2689 // 0 if candidate ain't better
2690 // 1 if candidate is better than the current best match
2692 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2694 ParameterData candidate_pd = GetParameterData (candidate);
2695 ParameterData best_pd;
2701 argument_count = args.Count;
2703 if (candidate_pd.Count == 0 && argument_count == 0)
2707 if (candidate_pd.Count == argument_count) {
2709 for (int j = argument_count; j > 0;) {
2712 Argument a = (Argument) args [j];
2714 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2731 best_pd = GetParameterData (best);
2733 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2734 int rating1 = 0, rating2 = 0;
2736 for (int j = argument_count; j > 0;) {
2740 Argument a = (Argument) args [j];
2742 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2743 best_pd.ParameterType (j));
2744 y = BetterConversion (tc, a, best_pd.ParameterType (j),
2745 candidate_pd.ParameterType (j));
2751 if (rating1 > rating2)
2760 public static string FullMethodDesc (MethodBase mb)
2762 StringBuilder sb = new StringBuilder (mb.Name);
2763 ParameterData pd = GetParameterData (mb);
2766 for (int i = pd.Count; i > 0;) {
2768 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2774 return sb.ToString ();
2777 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2780 if (mg1 != null || mg2 != null) {
2782 MethodGroupExpr left_set = null, right_set = null;
2783 int length1 = 0, length2 = 0;
2786 left_set = (MethodGroupExpr) mg1;
2787 length1 = left_set.Methods.Length;
2791 right_set = (MethodGroupExpr) mg2;
2792 length2 = right_set.Methods.Length;
2795 MemberInfo [] miset = new MemberInfo [length1 + length2];
2796 if (left_set != null)
2797 left_set.Methods.CopyTo (miset, 0);
2798 if (right_set != null)
2799 right_set.Methods.CopyTo (miset, length1);
2801 MethodGroupExpr union = new MethodGroupExpr (miset);
2812 // Find the Applicable Function Members (7.4.2.1)
2814 // me: Method Group expression with the members to select.
2815 // it might contain constructors or methods (or anything
2816 // that maps to a method).
2818 // Arguments: ArrayList containing resolved Argument objects.
2820 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2821 // that is the best match of me on Arguments.
2824 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2825 ArrayList Arguments, Location loc)
2827 ArrayList afm = new ArrayList ();
2828 int best_match_idx = -1;
2829 MethodBase method = null;
2832 for (int i = me.Methods.Length; i > 0; ){
2834 MethodBase candidate = me.Methods [i];
2837 x = BetterFunction (tc, Arguments, candidate, method);
2843 method = me.Methods [best_match_idx];
2847 if (best_match_idx != -1)
2850 // Now we see if we can at least find a method with the same number of arguments
2851 // and then try doing implicit conversion on the arguments
2853 if (Arguments == null)
2856 argument_count = Arguments.Count;
2858 ParameterData pd = null;
2860 for (int i = me.Methods.Length; i > 0;) {
2862 MethodBase mb = me.Methods [i];
2863 pd = GetParameterData (mb);
2865 if (pd.Count == argument_count) {
2867 method = me.Methods [best_match_idx];
2873 if (best_match_idx == -1)
2876 // And now convert implicitly, each argument to the required type
2878 pd = GetParameterData (method);
2880 for (int j = argument_count; j > 0;) {
2882 Argument a = (Argument) Arguments [j];
2883 Expression a_expr = a.Expr;
2885 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
2888 tc.RootContext.Report.Error (1502, loc,
2889 "The best overloaded match for method '" + FullMethodDesc (method) +
2890 "' has some invalid arguments");
2891 tc.RootContext.Report.Error (1503, loc,
2892 "Argument " + (j+1) +
2893 " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
2894 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
2899 // Update the argument with the implicit conversion
2909 public override Expression Resolve (TypeContainer tc)
2912 // First, resolve the expression that is used to
2913 // trigger the invocation
2915 this.expr = expr.Resolve (tc);
2916 if (this.expr == null)
2919 if (!(this.expr is MethodGroupExpr)){
2920 report118 (tc, this.expr, "method group");
2925 // Next, evaluate all the expressions in the argument list
2927 if (Arguments != null){
2928 for (int i = Arguments.Count; i > 0;){
2930 Argument a = (Argument) Arguments [i];
2932 if (!a.Resolve (tc))
2937 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
2939 if (method == null){
2940 tc.RootContext.Report.Error (-6, Location,
2941 "Could not find any applicable function for this argument list");
2945 if (method is MethodInfo)
2946 type = ((MethodInfo)method).ReturnType;
2951 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
2955 if (Arguments != null)
2956 top = Arguments.Count;
2960 for (int i = 0; i < top; i++){
2961 Argument a = (Argument) Arguments [i];
2967 public override void Emit (EmitContext ec)
2969 bool is_static = method.IsStatic;
2972 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2975 // If this is ourselves, push "this"
2977 if (mg.InstanceExpression == null){
2978 ec.ig.Emit (OpCodes.Ldarg_0);
2981 // Push the instance expression
2983 mg.InstanceExpression.Emit (ec);
2987 if (Arguments != null)
2988 EmitArguments (ec, method, Arguments);
2991 if (method is MethodInfo)
2992 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
2994 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2996 if (method is MethodInfo)
2997 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
2999 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3003 public override void EmitStatement (EmitContext ec)
3008 // Pop the return value if there is one
3010 if (method is MethodInfo){
3011 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3012 ec.ig.Emit (OpCodes.Pop);
3017 public class New : ExpressionStatement {
3024 public readonly NType NewType;
3025 public readonly ArrayList Arguments;
3026 public readonly string RequestedType;
3027 // These are for the case when we have an array
3028 public readonly string Rank;
3029 public readonly ArrayList Indices;
3030 public readonly ArrayList Initializers;
3033 MethodBase method = null;
3035 public New (string requested_type, ArrayList arguments, Location loc)
3037 RequestedType = requested_type;
3038 Arguments = arguments;
3039 NewType = NType.Object;
3043 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3045 RequestedType = requested_type;
3048 Initializers = initializers;
3049 NewType = NType.Array;
3053 public override Expression Resolve (TypeContainer tc)
3055 type = tc.LookupType (RequestedType, false);
3062 ml = MemberLookup (tc.RootContext, type, ".ctor", false,
3063 MemberTypes.Constructor, AllBindingsFlags);
3065 if (! (ml is MethodGroupExpr)){
3067 // FIXME: Find proper error
3069 report118 (tc, ml, "method group");
3073 if (Arguments != null){
3074 for (int i = Arguments.Count; i > 0;){
3076 Argument a = (Argument) Arguments [i];
3078 if (!a.Resolve (tc))
3083 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3085 if (method == null) {
3086 tc.RootContext.Report.Error (-6, Location,
3087 "New invocation: Can not find a constructor for this argument list");
3094 public override void Emit (EmitContext ec)
3096 Invocation.EmitArguments (ec, method, Arguments);
3097 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3100 public override void EmitStatement (EmitContext ec)
3103 ec.ig.Emit (OpCodes.Pop);
3108 // Represents the `this' construct
3110 public class This : Expression, LValue {
3111 public override Expression Resolve (TypeContainer tc)
3113 eclass = ExprClass.Variable;
3114 type = tc.TypeBuilder;
3117 // FIXME: Verify that this is only used in instance contexts.
3122 public override void Emit (EmitContext ec)
3124 ec.ig.Emit (OpCodes.Ldarg_0);
3127 public void Store (ILGenerator ig)
3130 // Assignment to the "this" variable
3132 ig.Emit (OpCodes.Starg, 0);
3136 public class TypeOf : Expression {
3137 public readonly string QueriedType;
3139 public TypeOf (string queried_type)
3141 QueriedType = queried_type;
3144 public override Expression Resolve (TypeContainer tc)
3146 type = tc.LookupType (QueriedType, false);
3151 eclass = ExprClass.Type;
3155 public override void Emit (EmitContext ec)
3157 throw new Exception ("Implement me");
3158 // FIXME: Implement.
3162 public class SizeOf : Expression {
3163 public readonly string QueriedType;
3165 public SizeOf (string queried_type)
3167 this.QueriedType = queried_type;
3170 public override Expression Resolve (TypeContainer tc)
3172 // FIXME: Implement;
3173 throw new Exception ("Unimplemented");
3177 public override void Emit (EmitContext ec)
3179 throw new Exception ("Implement me");
3183 public class MemberAccess : Expression {
3184 public readonly string Identifier;
3186 Expression member_lookup;
3188 public MemberAccess (Expression expr, string id)
3194 public Expression Expr {
3200 public override Expression Resolve (TypeContainer tc)
3202 Expression new_expression = expr.Resolve (tc);
3204 if (new_expression == null)
3207 member_lookup = MemberLookup (tc.RootContext, expr.Type, Identifier, false);
3209 if (member_lookup is MethodGroupExpr){
3210 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3213 // Bind the instance expression to it
3215 // FIXME: This is a horrible way of detecting if it is
3216 // an instance expression. Figure out how to fix this.
3219 if (expr is LocalVariableReference ||
3220 expr is ParameterReference ||
3222 mg.InstanceExpression = expr;
3224 return member_lookup;
3225 } else if (member_lookup is FieldExpr){
3226 FieldExpr fe = (FieldExpr) member_lookup;
3230 return member_lookup;
3233 // FIXME: This should generate the proper node
3234 // ie, for a Property Access, it should like call it
3237 return member_lookup;
3240 public override void Emit (EmitContext ec)
3242 throw new Exception ("Implement me");
3248 // Nodes of type Namespace are created during the semantic
3249 // analysis to resolve member_access/qualified_identifier/simple_name
3252 // They are born `resolved'.
3254 public class NamespaceExpr : Expression {
3255 public readonly string Name;
3257 public NamespaceExpr (string name)
3260 eclass = ExprClass.Namespace;
3263 public override Expression Resolve (TypeContainer tc)
3268 public override void Emit (EmitContext ec)
3270 throw new Exception ("Namespace expressions should never be emitted");
3275 // Fully resolved expression that evaluates to a type
3277 public class TypeExpr : Expression {
3278 public TypeExpr (Type t)
3281 eclass = ExprClass.Type;
3284 override public Expression Resolve (TypeContainer tc)
3289 override public void Emit (EmitContext ec)
3291 throw new Exception ("Implement me");
3296 // MethodGroup Expression.
3298 // This is a fully resolved expression that evaluates to a type
3300 public class MethodGroupExpr : Expression {
3301 public readonly MethodBase [] Methods;
3302 Expression instance_expression = null;
3304 public MethodGroupExpr (MemberInfo [] mi)
3306 Methods = new MethodBase [mi.Length];
3307 mi.CopyTo (Methods, 0);
3308 eclass = ExprClass.MethodGroup;
3312 // `A method group may have associated an instance expression'
3314 public Expression InstanceExpression {
3316 return instance_expression;
3320 instance_expression = value;
3324 override public Expression Resolve (TypeContainer tc)
3329 override public void Emit (EmitContext ec)
3331 throw new Exception ("This should never be reached");
3335 public class BuiltinTypeAccess : Expression {
3336 public readonly string AccessBase;
3337 public readonly string Method;
3339 public BuiltinTypeAccess (string type, string method)
3341 System.Console.WriteLine ("DUDE! This type should be fully resolved!");
3346 public override Expression Resolve (TypeContainer tc)
3348 // FIXME: Implement;
3349 throw new Exception ("Unimplemented");
3353 public override void Emit (EmitContext ec)
3355 throw new Exception ("Unimplemented");
3360 // Fully resolved expression that evaluates to a Field
3362 public class FieldExpr : Expression, LValue {
3363 public readonly FieldInfo FieldInfo;
3364 public Expression Instance;
3366 public FieldExpr (FieldInfo fi)
3369 eclass = ExprClass.Variable;
3370 type = fi.FieldType;
3373 override public Expression Resolve (TypeContainer tc)
3375 if (!FieldInfo.IsStatic){
3376 if (Instance == null){
3377 throw new Exception ("non-static FieldExpr without instance var\n" +
3378 "You have to assign the Instance variable\n" +
3379 "Of the FieldExpr to set this\n");
3382 Instance = Instance.Resolve (tc);
3383 if (Instance == null)
3390 override public void Emit (EmitContext ec)
3392 ILGenerator ig = ec.ig;
3394 if (FieldInfo.IsStatic)
3395 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3399 ig.Emit (OpCodes.Ldfld, FieldInfo);
3403 public void Store (ILGenerator ig)
3405 if (FieldInfo.IsStatic)
3406 ig.Emit (OpCodes.Stsfld, FieldInfo);
3408 ig.Emit (OpCodes.Stfld, FieldInfo);
3413 // Fully resolved expression that evaluates to a Property
3415 public class PropertyExpr : Expression {
3416 public readonly PropertyInfo PropertyInfo;
3417 public readonly bool IsStatic;
3419 public PropertyExpr (PropertyInfo pi)
3422 eclass = ExprClass.PropertyAccess;
3425 MethodBase [] acc = pi.GetAccessors ();
3427 for (int i = 0; i < acc.Length; i++)
3428 if (acc [i].IsStatic)
3431 type = pi.PropertyType;
3434 override public Expression Resolve (TypeContainer tc)
3436 // We are born in resolved state.
3440 override public void Emit (EmitContext ec)
3442 // FIXME: Implement;
3443 throw new Exception ("Unimplemented");
3448 // Fully resolved expression that evaluates to a Property
3450 public class EventExpr : Expression {
3451 public readonly EventInfo EventInfo;
3453 public EventExpr (EventInfo ei)
3456 eclass = ExprClass.EventAccess;
3459 override public Expression Resolve (TypeContainer tc)
3461 // We are born in resolved state.
3465 override public void Emit (EmitContext ec)
3467 throw new Exception ("Implement me");
3468 // FIXME: Implement.
3472 public class CheckedExpr : Expression {
3474 public Expression Expr;
3476 public CheckedExpr (Expression e)
3481 public override Expression Resolve (TypeContainer tc)
3483 Expr = Expr.Resolve (tc);
3488 eclass = Expr.ExprClass;
3493 public override void Emit (EmitContext ec)
3495 bool last_check = ec.CheckState;
3497 ec.CheckState = true;
3499 ec.CheckState = last_check;
3504 public class UnCheckedExpr : Expression {
3506 public Expression Expr;
3508 public UnCheckedExpr (Expression e)
3513 public override Expression Resolve (TypeContainer tc)
3515 Expr = Expr.Resolve (tc);
3520 eclass = Expr.ExprClass;
3525 public override void Emit (EmitContext ec)
3527 bool last_check = ec.CheckState;
3529 ec.CheckState = false;
3531 ec.CheckState = last_check;
3536 public class ElementAccess : Expression {
3538 public readonly ArrayList Arguments;
3539 public readonly Expression Expr;
3541 public ElementAccess (Expression e, ArrayList e_list)
3547 public override Expression Resolve (TypeContainer tc)
3549 // FIXME: Implement;
3550 throw new Exception ("Unimplemented");
3554 public override void Emit (EmitContext ec)
3556 // FIXME : Implement !
3557 throw new Exception ("Unimplemented");
3562 public class BaseAccess : Expression {
3564 public enum BaseAccessType {
3569 public readonly BaseAccessType BAType;
3570 public readonly string Member;
3571 public readonly ArrayList Arguments;
3573 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3581 public override Expression Resolve (TypeContainer tc)
3583 // FIXME: Implement;
3584 throw new Exception ("Unimplemented");
3588 public override void Emit (EmitContext ec)
3590 throw new Exception ("Unimplemented");
3594 public class UserImplicitCast : Expression {
3599 ArrayList arguments;
3601 public UserImplicitCast (Expression source, Type target)
3603 this.source = source;
3604 this.target = target;
3607 public override Expression Resolve (TypeContainer tc)
3609 source = source.Resolve (tc);
3614 Expression mg1, mg2;
3615 MethodGroupExpr union;
3617 mg1 = MemberLookup (tc.RootContext, source.Type, "op_Implicit", false);
3618 mg2 = MemberLookup (tc.RootContext, target, "op_Implicit", false);
3620 union = Invocation.MakeUnionSet (mg1, mg2);
3622 arguments = new ArrayList ();
3623 arguments.Add (new Argument (source, Argument.AType.Expression));
3625 if (union != null) {
3626 method = Invocation.OverloadResolve (tc, union, arguments,
3627 new Location ("FIXME", 1, 1));
3629 if (method != null) {
3630 type = ((MethodInfo) method).ReturnType;
3635 if (target == TypeManager.bool_type) {
3637 mg1 = MemberLookup (tc.RootContext, source.Type, "op_True", false);
3638 mg2 = MemberLookup (tc.RootContext, target, "op_True", false);
3640 union = Invocation.MakeUnionSet (mg1, mg2);
3645 method = Invocation.OverloadResolve (tc, union, arguments,
3646 new Location ("FIXME", 1, 1));
3648 if (method != null) {
3649 type = ((MethodInfo) method).ReturnType;
3657 public static bool CanConvert (TypeContainer tc, Expression source, Type target)
3659 source = source.Resolve (tc);
3664 Expression mg1, mg2;
3666 ArrayList arguments;
3668 mg1 = MemberLookup (tc.RootContext, source.Type, "op_Implicit", false);
3669 mg2 = MemberLookup (tc.RootContext, target, "op_Implicit", false);
3671 MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3673 arguments = new ArrayList ();
3674 arguments.Add (new Argument (source, Argument.AType.Expression));
3676 if (union != null) {
3678 method = Invocation.OverloadResolve (tc, union, arguments,
3679 new Location ("FIXME", 1, 1));
3680 if (method != null) {
3681 MethodInfo mi = (MethodInfo) method;
3683 if (mi.ReturnType == target)
3688 // If we have a boolean type, we need to check for the True
3689 // and False operators too.
3691 if (target == TypeManager.bool_type) {
3693 mg1 = MemberLookup (tc.RootContext, source.Type, "op_True", false);
3694 mg2 = MemberLookup (tc.RootContext, target, "op_True", false);
3696 union = Invocation.MakeUnionSet (mg1, mg2);
3701 method = Invocation.OverloadResolve (tc, union, arguments,
3702 new Location ("FIXME", 1, 1));
3703 if (method != null) {
3704 MethodInfo mi = (MethodInfo) method;
3706 if (mi.ReturnType == target)
3715 public override void Emit (EmitContext ec)
3717 ILGenerator ig = ec.ig;
3719 if (method != null) {
3721 // Note that operators are static anyway
3723 if (arguments != null)
3724 Invocation.EmitArguments (ec, method, arguments);
3726 if (method is MethodInfo)
3727 ig.Emit (OpCodes.Call, (MethodInfo) method);
3729 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3734 throw new Exception ("Implement me");