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
19 using System.Collections;
20 using System.Diagnostics;
21 using System.Reflection;
22 using System.Reflection.Emit;
26 // The ExprClass class contains the is used to pass the
27 // classification of an expression (value, variable, namespace,
28 // type, method group, property access, event access, indexer access,
31 public enum ExprClass {
35 Variable, // Every Variable should implement LValue
46 // Base class for expressions
48 public abstract class Expression {
49 protected ExprClass eclass;
62 public ExprClass ExprClass {
73 // Utility wrapper routine for Error, just to beautify the code
75 static protected void Error (TypeContainer tc, int error, string s)
77 Report.Error (error, s);
80 static protected void Error (TypeContainer tc, int error, Location l, string s)
82 Report.Error (error, l, s);
86 // Utility wrapper routine for Warning, just to beautify the code
88 static protected void Warning (TypeContainer tc, int warning, string s)
90 Report.Warning (warning, s);
94 // Performs semantic analysis on the Expression
98 // The Resolve method is invoked to perform the semantic analysis
101 // The return value is an expression (it can be the
102 // same expression in some cases) or a new
103 // expression that better represents this node.
105 // For example, optimizations of Unary (LiteralInt)
106 // would return a new LiteralInt with a negated
109 // If there is an error during semantic analysis,
110 // then an error should
111 // be reported (using TypeContainer.RootContext.Report) and a null
112 // value should be returned.
114 // There are two side effects expected from calling
115 // Resolve(): the the field variable "eclass" should
116 // be set to any value of the enumeration
117 // `ExprClass' and the type variable should be set
118 // to a valid type (this is the type of the
122 public abstract Expression DoResolve (TypeContainer tc);
126 // Currently Resolve wraps DoResolve to perform sanity
127 // checking and assertion checking on what we expect from Resolve
130 public Expression Resolve (TypeContainer tc)
132 Expression e = DoResolve (tc);
135 if (e.ExprClass == ExprClass.Invalid)
136 throw new Exception ("Expression " + e +
137 " ExprClass is Invalid after resolve");
139 if (e.ExprClass != ExprClass.MethodGroup)
141 throw new Exception ("Expression " + e +
142 " did not set its type after Resolve");
149 // Emits the code for the expression
154 // The Emit method is invoked to generate the code
155 // for the expression.
158 public abstract void Emit (EmitContext ec);
161 // Protected constructor. Only derivate types should
162 // be able to be created
165 protected Expression ()
167 eclass = ExprClass.Invalid;
172 // Returns a literalized version of a literal FieldInfo
174 static Expression Literalize (FieldInfo fi)
176 Type t = fi.FieldType;
177 object v = fi.GetValue (fi);
179 if (t == TypeManager.int32_type)
180 return new IntLiteral ((int) v);
181 else if (t == TypeManager.uint32_type)
182 return new UIntLiteral ((uint) v);
183 else if (t == TypeManager.int64_type)
184 return new LongLiteral ((long) v);
185 else if (t == TypeManager.uint64_type)
186 return new ULongLiteral ((ulong) v);
187 else if (t == TypeManager.float_type)
188 return new FloatLiteral ((float) v);
189 else if (t == TypeManager.double_type)
190 return new DoubleLiteral ((double) v);
191 else if (t == TypeManager.string_type)
192 return new StringLiteral ((string) v);
193 else if (t == TypeManager.short_type)
194 return new IntLiteral ((int) ((short)v));
195 else if (t == TypeManager.ushort_type)
196 return new IntLiteral ((int) ((ushort)v));
197 else if (t == TypeManager.sbyte_type)
198 return new IntLiteral ((int) ((sbyte)v));
199 else if (t == TypeManager.byte_type)
200 return new IntLiteral ((int) ((byte)v));
201 else if (t == TypeManager.char_type)
202 return new IntLiteral ((int) ((char)v));
204 throw new Exception ("Unknown type for literal (" + v.GetType () +
205 "), details: " + fi);
209 // Returns a fully formed expression after a MemberLookup
211 static Expression ExprClassFromMemberInfo (TypeContainer tc, MemberInfo mi)
213 if (mi is EventInfo){
214 return new EventExpr ((EventInfo) mi);
215 } else if (mi is FieldInfo){
216 FieldInfo fi = (FieldInfo) mi;
219 Expression e = Literalize (fi);
224 return new FieldExpr (fi);
225 } else if (mi is PropertyInfo){
226 return new PropertyExpr ((PropertyInfo) mi);
227 } else if (mi is Type)
228 return new TypeExpr ((Type) mi);
234 // FIXME: Probably implement a cache for (t,name,current_access_set)?
236 // FIXME: We need to cope with access permissions here, or this wont
239 // This code could use some optimizations, but we need to do some
240 // measurements. For example, we could use a delegate to `flag' when
241 // something can not any longer be a method-group (because it is something
245 // If the return value is an Array, then it is an array of
248 // If the return value is an MemberInfo, it is anything, but a Method
252 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
253 // the arguments here and have MemberLookup return only the methods that
254 // match the argument count/type, unlike we are doing now (we delay this
257 // This is so we can catch correctly attempts to invoke instance methods
258 // from a static body (scan for error 120 in ResolveSimpleName).
260 public static Expression MemberLookup (TypeContainer tc, Type t, string name,
261 bool same_type, MemberTypes mt, BindingFlags bf)
264 bf |= BindingFlags.NonPublic;
266 MemberInfo [] mi = tc.RootContext.TypeManager.FindMembers (
267 t, mt, bf, Type.FilterName, name);
272 // FIXME : How does this wierd case arise ?
276 if (mi.Length == 1 && !(mi [0] is MethodBase))
277 return Expression.ExprClassFromMemberInfo (tc, mi [0]);
279 for (int i = 0; i < mi.Length; i++)
280 if (!(mi [i] is MethodBase)){
282 -5, "Do not know how to reproduce this case: " +
283 "Methods and non-Method with the same name, " +
284 "report this please");
286 for (i = 0; i < mi.Length; i++){
287 Type tt = mi [i].GetType ();
289 Console.WriteLine (i + ": " + mi [i]);
290 while (tt != TypeManager.object_type){
291 Console.WriteLine (tt);
297 return new MethodGroupExpr (mi);
300 public const MemberTypes AllMemberTypes =
301 MemberTypes.Constructor |
305 MemberTypes.NestedType |
306 MemberTypes.Property;
308 public const BindingFlags AllBindingsFlags =
309 BindingFlags.Public |
310 BindingFlags.Static |
311 BindingFlags.Instance;
313 public static Expression MemberLookup (TypeContainer tc, Type t, string name,
316 return MemberLookup (tc, t, name, same_type, AllMemberTypes, AllBindingsFlags);
320 // I am in general unhappy with this implementation.
322 // I need to revise this.
324 static public Expression ResolveMemberAccess (TypeContainer tc, string name)
326 Expression left_e = null;
327 int dot_pos = name.LastIndexOf (".");
328 string left = name.Substring (0, dot_pos);
329 string right = name.Substring (dot_pos + 1);
332 if ((t = tc.LookupType (left, false)) != null){
335 left_e = new TypeExpr (t);
336 e = new MemberAccess (left_e, right);
337 return e.Resolve (tc);
343 // T.P Static property access (P) on Type T.
344 // e.P instance property access on instance e for P.
350 Error (tc, 246, "Can not find type or namespace `"+left+"'");
354 switch (left_e.ExprClass){
356 return MemberLookup (tc,
358 left_e.Type == tc.TypeBuilder);
360 case ExprClass.Namespace:
361 case ExprClass.PropertyAccess:
362 case ExprClass.IndexerAccess:
363 case ExprClass.Variable:
364 case ExprClass.Value:
365 case ExprClass.Nothing:
366 case ExprClass.EventAccess:
367 case ExprClass.MethodGroup:
368 case ExprClass.Invalid:
369 throw new Exception ("Should have got the " + left_e.ExprClass +
376 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
378 Type expr_type = expr.Type;
380 if (target_type == TypeManager.object_type) {
381 if (expr_type.IsClass)
382 return new EmptyCast (expr, target_type);
383 if (expr_type.IsValueType)
384 return new BoxedCast (expr);
385 } else if (expr_type.IsSubclassOf (target_type)) {
386 return new EmptyCast (expr, target_type);
388 // from any class-type S to any interface-type T.
389 if (expr_type.IsClass && target_type.IsInterface) {
390 Type [] interfaces = expr_type.FindInterfaces (Module.FilterTypeName,
391 target_type.FullName);
392 if (interfaces != null)
393 return new EmptyCast (expr, target_type);
396 // from any interface type S to interface-type T.
397 // FIXME : Is it right to use IsAssignableFrom ?
398 if (expr_type.IsInterface && target_type.IsInterface)
399 if (target_type.IsAssignableFrom (expr_type))
400 return new EmptyCast (expr, target_type);
403 // from an array-type S to an array-type of type T
404 if (expr_type.IsArray && target_type.IsArray) {
406 throw new Exception ("Implement array conversion");
410 // from an array-type to System.Array
411 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
412 return new EmptyCast (expr, target_type);
414 // from any delegate type to System.Delegate
415 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
416 target_type == TypeManager.delegate_type)
417 if (target_type.IsAssignableFrom (expr_type))
418 return new EmptyCast (expr, target_type);
420 // from any array-type or delegate type into System.ICloneable.
421 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
422 if (target_type == TypeManager.icloneable_type)
423 throw new Exception ("Implement conversion to System.ICloneable");
425 // from the null type to any reference-type.
426 // FIXME : How do we do this ?
436 // Handles expressions like this: decimal d; d = 1;
437 // and changes them into: decimal d; d = new System.Decimal (1);
439 static Expression InternalTypeConstructor (TypeContainer tc, Expression expr, Type target)
441 ArrayList args = new ArrayList ();
443 args.Add (new Argument (expr, Argument.AType.Expression));
445 Expression ne = new New (target.FullName, args,
448 return ne.Resolve (tc);
452 // Implicit Numeric Conversions.
454 // expr is the expression to convert, returns a new expression of type
455 // target_type or null if an implicit conversion is not possible.
458 static public Expression ImplicitNumericConversion (TypeContainer tc, Expression expr,
459 Type target_type, Location l)
461 Type expr_type = expr.Type;
464 // Attempt to do the implicit constant expression conversions
466 if (expr is IntLiteral){
469 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
472 } else if (expr is LongLiteral){
474 // Try the implicit constant expression conversion
475 // from long to ulong, instead of a nice routine,
478 if (((LongLiteral) expr).Value > 0)
479 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
482 if (expr_type == TypeManager.sbyte_type){
484 // From sbyte to short, int, long, float, double.
486 if (target_type == TypeManager.int32_type)
487 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
488 if (target_type == TypeManager.int64_type)
489 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
490 if (target_type == TypeManager.double_type)
491 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
492 if (target_type == TypeManager.float_type)
493 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
494 if (target_type == TypeManager.short_type)
495 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
496 if (target_type == TypeManager.decimal_type)
497 return InternalTypeConstructor (tc, expr, target_type);
498 } else if (expr_type == TypeManager.byte_type){
500 // From byte to short, ushort, int, uint, long, ulong, float, double
502 if ((target_type == TypeManager.short_type) ||
503 (target_type == TypeManager.ushort_type) ||
504 (target_type == TypeManager.int32_type) ||
505 (target_type == TypeManager.uint32_type))
506 return new EmptyCast (expr, target_type);
508 if (target_type == TypeManager.uint64_type)
509 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
510 if (target_type == TypeManager.int64_type)
511 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
513 if (target_type == TypeManager.float_type)
514 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
515 if (target_type == TypeManager.double_type)
516 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
517 if (target_type == TypeManager.decimal_type)
518 return InternalTypeConstructor (tc, expr, target_type);
519 } else if (expr_type == TypeManager.short_type){
521 // From short to int, long, float, double
523 if (target_type == TypeManager.int32_type)
524 return new EmptyCast (expr, target_type);
525 if (target_type == TypeManager.int64_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
527 if (target_type == TypeManager.double_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
529 if (target_type == TypeManager.float_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
531 if (target_type == TypeManager.decimal_type)
532 return InternalTypeConstructor (tc, expr, target_type);
533 } else if (expr_type == TypeManager.ushort_type){
535 // From ushort to int, uint, long, ulong, float, double
537 if (target_type == TypeManager.uint32_type)
538 return new EmptyCast (expr, target_type);
540 if (target_type == TypeManager.uint64_type)
541 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
542 if (target_type == TypeManager.int32_type)
543 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
544 if (target_type == TypeManager.int64_type)
545 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
546 if (target_type == TypeManager.double_type)
547 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
548 if (target_type == TypeManager.float_type)
549 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
550 if (target_type == TypeManager.decimal_type)
551 return InternalTypeConstructor (tc, expr, target_type);
552 } else if (expr_type == TypeManager.int32_type){
554 // From int to long, float, double
556 if (target_type == TypeManager.int64_type)
557 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
558 if (target_type == TypeManager.double_type)
559 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
560 if (target_type == TypeManager.float_type)
561 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
562 if (target_type == TypeManager.decimal_type)
563 return InternalTypeConstructor (tc, expr, target_type);
564 } else if (expr_type == TypeManager.uint32_type){
566 // From uint to long, ulong, float, double
568 if (target_type == TypeManager.int64_type)
569 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
570 if (target_type == TypeManager.uint64_type)
571 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
572 if (target_type == TypeManager.double_type)
573 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
575 if (target_type == TypeManager.float_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
578 if (target_type == TypeManager.decimal_type)
579 return InternalTypeConstructor (tc, expr, target_type);
580 } else if ((expr_type == TypeManager.uint64_type) ||
581 (expr_type == TypeManager.int64_type)){
583 // From long/ulong to float, double
585 if (target_type == TypeManager.double_type)
586 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
588 if (target_type == TypeManager.float_type)
589 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
591 if (target_type == TypeManager.decimal_type)
592 return InternalTypeConstructor (tc, expr, target_type);
593 } else if (expr_type == TypeManager.char_type){
595 // From char to ushort, int, uint, long, ulong, float, double
597 if ((target_type == TypeManager.ushort_type) ||
598 (target_type == TypeManager.int32_type) ||
599 (target_type == TypeManager.uint32_type))
600 return new EmptyCast (expr, target_type);
601 if (target_type == TypeManager.uint64_type)
602 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
603 if (target_type == TypeManager.int64_type)
604 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
605 if (target_type == TypeManager.float_type)
606 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
607 if (target_type == TypeManager.double_type)
608 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
609 if (target_type == TypeManager.decimal_type)
610 return InternalTypeConstructor (tc, expr, target_type);
611 } else if (expr_type == TypeManager.float_type){
615 if (target_type == TypeManager.double_type)
616 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
623 // Determines if a standard implicit conversion exists from
624 // expr_type to target_type
626 static bool StandardConversionExists (Type expr_type, Type target_type)
628 if (expr_type == target_type)
631 // First numeric conversions
633 if (expr_type == TypeManager.sbyte_type){
635 // From sbyte to short, int, long, float, double.
637 if ((target_type == TypeManager.int32_type) ||
638 (target_type == TypeManager.int64_type) ||
639 (target_type == TypeManager.double_type) ||
640 (target_type == TypeManager.float_type) ||
641 (target_type == TypeManager.short_type) ||
642 (target_type == TypeManager.decimal_type))
645 } else if (expr_type == TypeManager.byte_type){
647 // From byte to short, ushort, int, uint, long, ulong, float, double
649 if ((target_type == TypeManager.short_type) ||
650 (target_type == TypeManager.ushort_type) ||
651 (target_type == TypeManager.int32_type) ||
652 (target_type == TypeManager.uint32_type) ||
653 (target_type == TypeManager.uint64_type) ||
654 (target_type == TypeManager.int64_type) ||
655 (target_type == TypeManager.float_type) ||
656 (target_type == TypeManager.double_type) ||
657 (target_type == TypeManager.decimal_type))
660 } else if (expr_type == TypeManager.short_type){
662 // From short to int, long, float, double
664 if ((target_type == TypeManager.int32_type) ||
665 (target_type == TypeManager.int64_type) ||
666 (target_type == TypeManager.double_type) ||
667 (target_type == TypeManager.float_type) ||
668 (target_type == TypeManager.decimal_type))
671 } else if (expr_type == TypeManager.ushort_type){
673 // From ushort to int, uint, long, ulong, float, double
675 if ((target_type == TypeManager.uint32_type) ||
676 (target_type == TypeManager.uint64_type) ||
677 (target_type == TypeManager.int32_type) ||
678 (target_type == TypeManager.int64_type) ||
679 (target_type == TypeManager.double_type) ||
680 (target_type == TypeManager.float_type) ||
681 (target_type == TypeManager.decimal_type))
684 } else if (expr_type == TypeManager.int32_type){
686 // From int to long, float, double
688 if ((target_type == TypeManager.int64_type) ||
689 (target_type == TypeManager.double_type) ||
690 (target_type == TypeManager.float_type) ||
691 (target_type == TypeManager.decimal_type))
694 } else if (expr_type == TypeManager.uint32_type){
696 // From uint to long, ulong, float, double
698 if ((target_type == TypeManager.int64_type) ||
699 (target_type == TypeManager.uint64_type) ||
700 (target_type == TypeManager.double_type) ||
701 (target_type == TypeManager.float_type) ||
702 (target_type == TypeManager.decimal_type))
705 } else if ((expr_type == TypeManager.uint64_type) ||
706 (expr_type == TypeManager.int64_type)) {
708 // From long/ulong to float, double
710 if ((target_type == TypeManager.double_type) ||
711 (target_type == TypeManager.float_type) ||
712 (target_type == TypeManager.decimal_type))
715 } else if (expr_type == TypeManager.char_type){
717 // From char to ushort, int, uint, long, ulong, float, double
719 if ((target_type == TypeManager.ushort_type) ||
720 (target_type == TypeManager.int32_type) ||
721 (target_type == TypeManager.uint32_type) ||
722 (target_type == TypeManager.uint64_type) ||
723 (target_type == TypeManager.int64_type) ||
724 (target_type == TypeManager.float_type) ||
725 (target_type == TypeManager.double_type) ||
726 (target_type == TypeManager.decimal_type))
729 } else if (expr_type == TypeManager.float_type){
733 if (target_type == TypeManager.double_type)
737 // Next reference conversions
739 if (target_type == TypeManager.object_type) {
740 if ((expr_type.IsClass) ||
741 (expr_type.IsValueType))
744 } else if (expr_type.IsSubclassOf (target_type)) {
748 // from any class-type S to any interface-type T.
749 if (expr_type.IsClass && target_type.IsInterface)
752 // from any interface type S to interface-type T.
753 // FIXME : Is it right to use IsAssignableFrom ?
754 if (expr_type.IsInterface && target_type.IsInterface)
755 if (target_type.IsAssignableFrom (expr_type))
758 // from an array-type S to an array-type of type T
759 if (expr_type.IsArray && target_type.IsArray)
762 // from an array-type to System.Array
763 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
766 // from any delegate type to System.Delegate
767 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
768 target_type == TypeManager.delegate_type)
769 if (target_type.IsAssignableFrom (expr_type))
772 // from any array-type or delegate type into System.ICloneable.
773 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
774 if (target_type == TypeManager.icloneable_type)
777 // from the null type to any reference-type.
778 // FIXME : How do we do this ?
786 // Finds "most encompassed type" according to the spec (13.4.2)
787 // amongst the methods in the MethodGroupExpr which convert from a
788 // type encompassing source_type
790 static Type FindMostEncompassedType (TypeContainer tc, MethodGroupExpr me, Type source_type)
794 for (int i = me.Methods.Length; i > 0; ) {
797 MethodBase mb = me.Methods [i];
798 ParameterData pd = Invocation.GetParameterData (mb);
799 Type param_type = pd.ParameterType (0);
801 if (StandardConversionExists (source_type, param_type)) {
805 if (StandardConversionExists (param_type, best))
814 // Finds "most encompassing type" according to the spec (13.4.2)
815 // amongst the methods in the MethodGroupExpr which convert to a
816 // type encompassed by target_type
818 static Type FindMostEncompassingType (TypeContainer tc, MethodGroupExpr me, Type target)
822 for (int i = me.Methods.Length; i > 0; ) {
825 MethodInfo mi = (MethodInfo) me.Methods [i];
826 Type ret_type = mi.ReturnType;
828 if (StandardConversionExists (ret_type, target)) {
832 if (!StandardConversionExists (ret_type, best))
844 // User-defined Implicit conversions
846 static public Expression ImplicitUserConversion (TypeContainer tc, Expression source, Type target,
849 return UserDefinedConversion (tc, source, target, l, false);
853 // User-defined Explicit conversions
855 static public Expression ExplicitUserConversion (TypeContainer tc, Expression source, Type target,
858 return UserDefinedConversion (tc, source, target, l, true);
862 // User-defined conversions
864 static public Expression UserDefinedConversion (TypeContainer tc, Expression source,
865 Type target, Location l,
866 bool look_for_explicit)
868 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
869 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
871 MethodBase method = null;
872 Type source_type = source.Type;
876 // If we have a boolean type, we need to check for the True operator
878 // FIXME : How does the False operator come into the picture ?
879 // FIXME : This doesn't look complete and very correct !
880 if (target == TypeManager.bool_type)
883 op_name = "op_Implicit";
885 mg1 = MemberLookup (tc, source_type, op_name, false);
887 if (source_type.BaseType != null)
888 mg2 = MemberLookup (tc, source_type.BaseType, op_name, false);
890 mg3 = MemberLookup (tc, target, op_name, false);
892 if (target.BaseType != null)
893 mg4 = MemberLookup (tc, target.BaseType, op_name, false);
895 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
896 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
898 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
900 MethodGroupExpr union4 = null;
902 if (look_for_explicit) {
904 op_name = "op_Explicit";
906 mg5 = MemberLookup (tc, source_type, op_name, false);
908 if (source_type.BaseType != null)
909 mg6 = MemberLookup (tc, source_type.BaseType, op_name, false);
911 mg7 = MemberLookup (tc, target, op_name, false);
913 if (target.BaseType != null)
914 mg8 = MemberLookup (tc, target.BaseType, op_name, false);
916 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
917 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
919 union4 = Invocation.MakeUnionSet (union5, union6);
922 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
926 Type most_specific_source, most_specific_target;
928 most_specific_source = FindMostEncompassedType (tc, union, source_type);
929 if (most_specific_source == null)
932 most_specific_target = FindMostEncompassingType (tc, union, target);
933 if (most_specific_target == null)
938 for (int i = union.Methods.Length; i > 0;) {
941 MethodBase mb = union.Methods [i];
942 ParameterData pd = Invocation.GetParameterData (mb);
943 MethodInfo mi = (MethodInfo) union.Methods [i];
945 if (pd.ParameterType (0) == most_specific_source &&
946 mi.ReturnType == most_specific_target) {
952 if (method == null || count > 1) {
953 Report.Error (-11, l, "Ambiguous user defined conversion");
958 // This will do the conversion to the best match that we
959 // found. Now we need to perform an implict standard conversion
960 // if the best match was not the type that we were requested
963 if (look_for_explicit)
964 source = ConvertExplicit (tc, source, most_specific_source, l);
966 source = ConvertImplicitStandard (tc, source, most_specific_source,
972 e = new UserCast ((MethodInfo) method, source);
974 if (e.Type != target){
975 e = ConvertImplicitStandard (tc, e, target, l);
985 // Converts implicitly the resolved expression `expr' into the
986 // `target_type'. It returns a new expression that can be used
987 // in a context that expects a `target_type'.
989 static public Expression ConvertImplicit (TypeContainer tc, Expression expr,
990 Type target_type, Location l)
992 Type expr_type = expr.Type;
995 if (expr_type == target_type)
998 e = ImplicitNumericConversion (tc, expr, target_type, l);
1002 e = ImplicitReferenceConversion (expr, target_type);
1006 e = ImplicitUserConversion (tc, expr, target_type, l);
1010 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1011 IntLiteral i = (IntLiteral) expr;
1014 return new EmptyCast (expr, target_type);
1022 // Attempts to apply the `Standard Implicit
1023 // Conversion' rules to the expression `expr' into
1024 // the `target_type'. It returns a new expression
1025 // that can be used in a context that expects a
1028 // This is different from `ConvertImplicit' in that the
1029 // user defined implicit conversions are excluded.
1031 static public Expression ConvertImplicitStandard (TypeContainer tc, Expression expr,
1032 Type target_type, Location l)
1034 Type expr_type = expr.Type;
1037 if (expr_type == target_type)
1040 e = ImplicitNumericConversion (tc, expr, target_type, l);
1044 e = ImplicitReferenceConversion (expr, target_type);
1048 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1049 IntLiteral i = (IntLiteral) expr;
1052 return new EmptyCast (expr, target_type);
1057 // Attemps to perform an implict constant conversion of the IntLiteral
1058 // into a different data type using casts (See Implicit Constant
1059 // Expression Conversions)
1061 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1063 int value = il.Value;
1065 if (target_type == TypeManager.sbyte_type){
1066 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1068 } else if (target_type == TypeManager.byte_type){
1069 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1071 } else if (target_type == TypeManager.short_type){
1072 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1074 } else if (target_type == TypeManager.ushort_type){
1075 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1077 } else if (target_type == TypeManager.uint32_type){
1079 // we can optimize this case: a positive int32
1080 // always fits on a uint32
1084 } else if (target_type == TypeManager.uint64_type){
1086 // we can optimize this case: a positive int32
1087 // always fits on a uint64. But we need an opcode
1091 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1098 // Attemptes to implicityly convert `target' into `type', using
1099 // ConvertImplicit. If there is no implicit conversion, then
1100 // an error is signaled
1102 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
1103 Type type, Location l)
1107 e = ConvertImplicit (tc, target, type, l);
1111 string msg = "Can not convert implicitly from `"+
1112 TypeManager.CSharpName (target.Type) + "' to `" +
1113 TypeManager.CSharpName (type) + "'";
1115 Error (tc, 29, l, msg);
1121 // Performs the explicit numeric conversions
1123 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
1126 Type expr_type = expr.Type;
1128 if (expr_type == TypeManager.sbyte_type){
1130 // From sbyte to byte, ushort, uint, ulong, char
1132 if (target_type == TypeManager.byte_type)
1133 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1134 if (target_type == TypeManager.ushort_type)
1135 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1136 if (target_type == TypeManager.uint32_type)
1137 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1138 if (target_type == TypeManager.uint64_type)
1139 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1140 if (target_type == TypeManager.char_type)
1141 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1142 } else if (expr_type == TypeManager.byte_type){
1144 // From byte to sbyte and char
1146 if (target_type == TypeManager.sbyte_type)
1147 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1148 if (target_type == TypeManager.char_type)
1149 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1150 } else if (expr_type == TypeManager.short_type){
1152 // From short to sbyte, byte, ushort, uint, ulong, char
1154 if (target_type == TypeManager.sbyte_type)
1155 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1156 if (target_type == TypeManager.byte_type)
1157 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1158 if (target_type == TypeManager.ushort_type)
1159 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1160 if (target_type == TypeManager.uint32_type)
1161 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1162 if (target_type == TypeManager.uint64_type)
1163 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1164 if (target_type == TypeManager.char_type)
1165 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1166 } else if (expr_type == TypeManager.ushort_type){
1168 // From ushort to sbyte, byte, short, char
1170 if (target_type == TypeManager.sbyte_type)
1171 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1172 if (target_type == TypeManager.byte_type)
1173 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1174 if (target_type == TypeManager.short_type)
1175 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1176 if (target_type == TypeManager.char_type)
1177 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1178 } else if (expr_type == TypeManager.int32_type){
1180 // From int to sbyte, byte, short, ushort, uint, ulong, char
1182 if (target_type == TypeManager.sbyte_type)
1183 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1184 if (target_type == TypeManager.byte_type)
1185 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1186 if (target_type == TypeManager.short_type)
1187 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1188 if (target_type == TypeManager.ushort_type)
1189 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1190 if (target_type == TypeManager.uint32_type)
1191 return new EmptyCast (expr, target_type);
1192 if (target_type == TypeManager.uint64_type)
1193 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1194 if (target_type == TypeManager.char_type)
1195 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1196 } else if (expr_type == TypeManager.uint32_type){
1198 // From uint to sbyte, byte, short, ushort, int, char
1200 if (target_type == TypeManager.sbyte_type)
1201 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1202 if (target_type == TypeManager.byte_type)
1203 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1204 if (target_type == TypeManager.short_type)
1205 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1206 if (target_type == TypeManager.ushort_type)
1207 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1208 if (target_type == TypeManager.int32_type)
1209 return new EmptyCast (expr, target_type);
1210 if (target_type == TypeManager.char_type)
1211 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1212 } else if (expr_type == TypeManager.int64_type){
1214 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1216 if (target_type == TypeManager.sbyte_type)
1217 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1218 if (target_type == TypeManager.byte_type)
1219 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1220 if (target_type == TypeManager.short_type)
1221 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1222 if (target_type == TypeManager.ushort_type)
1223 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1224 if (target_type == TypeManager.int32_type)
1225 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1226 if (target_type == TypeManager.uint32_type)
1227 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1228 if (target_type == TypeManager.uint64_type)
1229 return new EmptyCast (expr, target_type);
1230 if (target_type == TypeManager.char_type)
1231 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1232 } else if (expr_type == TypeManager.uint64_type){
1234 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1236 if (target_type == TypeManager.sbyte_type)
1237 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1238 if (target_type == TypeManager.byte_type)
1239 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1240 if (target_type == TypeManager.short_type)
1241 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1242 if (target_type == TypeManager.ushort_type)
1243 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1244 if (target_type == TypeManager.int32_type)
1245 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1246 if (target_type == TypeManager.uint32_type)
1247 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1248 if (target_type == TypeManager.int64_type)
1249 return new EmptyCast (expr, target_type);
1250 if (target_type == TypeManager.char_type)
1251 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1252 } else if (expr_type == TypeManager.char_type){
1254 // From char to sbyte, byte, short
1256 if (target_type == TypeManager.sbyte_type)
1257 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1258 if (target_type == TypeManager.byte_type)
1259 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1260 if (target_type == TypeManager.short_type)
1261 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1262 } else if (expr_type == TypeManager.float_type){
1264 // From float to sbyte, byte, short,
1265 // ushort, int, uint, long, ulong, char
1268 if (target_type == TypeManager.sbyte_type)
1269 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1270 if (target_type == TypeManager.byte_type)
1271 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1272 if (target_type == TypeManager.short_type)
1273 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1274 if (target_type == TypeManager.ushort_type)
1275 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1276 if (target_type == TypeManager.int32_type)
1277 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1278 if (target_type == TypeManager.uint32_type)
1279 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1280 if (target_type == TypeManager.int64_type)
1281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1282 if (target_type == TypeManager.uint64_type)
1283 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1284 if (target_type == TypeManager.char_type)
1285 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1286 if (target_type == TypeManager.decimal_type)
1287 return InternalTypeConstructor (tc, expr, target_type);
1288 } else if (expr_type == TypeManager.double_type){
1290 // From double to byte, byte, short,
1291 // ushort, int, uint, long, ulong,
1292 // char, float or decimal
1294 if (target_type == TypeManager.sbyte_type)
1295 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1296 if (target_type == TypeManager.byte_type)
1297 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1298 if (target_type == TypeManager.short_type)
1299 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1300 if (target_type == TypeManager.ushort_type)
1301 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1302 if (target_type == TypeManager.int32_type)
1303 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1304 if (target_type == TypeManager.uint32_type)
1305 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1306 if (target_type == TypeManager.int64_type)
1307 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1308 if (target_type == TypeManager.uint64_type)
1309 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1310 if (target_type == TypeManager.char_type)
1311 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1312 if (target_type == TypeManager.float_type)
1313 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1314 if (target_type == TypeManager.decimal_type)
1315 return InternalTypeConstructor (tc, expr, target_type);
1318 // decimal is taken care of by the op_Explicit methods.
1324 // Implements Explicit Reference conversions
1326 static Expression ConvertReferenceExplicit (TypeContainer tc, Expression expr,
1329 Type expr_type = expr.Type;
1330 bool target_is_value_type = target_type.IsValueType;
1333 // From object to any reference type
1335 if (expr_type == TypeManager.object_type && !target_is_value_type)
1336 return new ClassCast (expr, target_type);
1342 // Performs an explicit conversion of the expression `expr' whose
1343 // type is expr.Type to `target_type'.
1345 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
1346 Type target_type, Location l)
1348 Expression ne = ConvertImplicitStandard (tc, expr, target_type, l);
1353 ne = ConvertNumericExplicit (tc, expr, target_type);
1357 ne = ConvertReferenceExplicit (tc, expr, target_type);
1361 ne = ExplicitUserConversion (tc, expr, target_type, l);
1365 Report.Error (30, l, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1366 + TypeManager.CSharpName (target_type) + "'");
1370 static string ExprClassName (ExprClass c)
1373 case ExprClass.Invalid:
1375 case ExprClass.Value:
1377 case ExprClass.Variable:
1379 case ExprClass.Namespace:
1381 case ExprClass.Type:
1383 case ExprClass.MethodGroup:
1384 return "method group";
1385 case ExprClass.PropertyAccess:
1386 return "property access";
1387 case ExprClass.EventAccess:
1388 return "event access";
1389 case ExprClass.IndexerAccess:
1390 return "indexer access";
1391 case ExprClass.Nothing:
1394 throw new Exception ("Should not happen");
1398 // Reports that we were expecting `expr' to be of class `expected'
1400 protected void report118 (TypeContainer tc, Location l, Expression expr, string expected)
1402 string kind = "Unknown";
1405 kind = ExprClassName (expr.ExprClass);
1407 Error (tc, 118, l, "Expression denotes a '" + kind +
1408 "' where an " + expected + " was expected");
1413 // This is just a base class for expressions that can
1414 // appear on statements (invocations, object creation,
1415 // assignments, post/pre increment and decrement). The idea
1416 // being that they would support an extra Emition interface that
1417 // does not leave a result on the stack.
1420 public abstract class ExpressionStatement : Expression {
1423 // Requests the expression to be emitted in a `statement'
1424 // context. This means that no new value is left on the
1425 // stack after invoking this method (constrasted with
1426 // Emit that will always leave a value on the stack).
1428 public abstract void EmitStatement (EmitContext ec);
1432 // This kind of cast is used to encapsulate the child
1433 // whose type is child.Type into an expression that is
1434 // reported to return "return_type". This is used to encapsulate
1435 // expressions which have compatible types, but need to be dealt
1436 // at higher levels with.
1438 // For example, a "byte" expression could be encapsulated in one
1439 // of these as an "unsigned int". The type for the expression
1440 // would be "unsigned int".
1444 public class EmptyCast : Expression {
1445 protected Expression child;
1447 public EmptyCast (Expression child, Type return_type)
1449 ExprClass = child.ExprClass;
1454 public override Expression DoResolve (TypeContainer tc)
1456 // This should never be invoked, we are born in fully
1457 // initialized state.
1462 public override void Emit (EmitContext ec)
1469 // This kind of cast is used to encapsulate Value Types in objects.
1471 // The effect of it is to box the value type emitted by the previous
1474 public class BoxedCast : EmptyCast {
1476 public BoxedCast (Expression expr)
1477 : base (expr, TypeManager.object_type)
1481 public override Expression DoResolve (TypeContainer tc)
1483 // This should never be invoked, we are born in fully
1484 // initialized state.
1489 public override void Emit (EmitContext ec)
1492 ec.ig.Emit (OpCodes.Box, child.Type);
1497 // This kind of cast is used to encapsulate a child expression
1498 // that can be trivially converted to a target type using one or
1499 // two opcodes. The opcodes are passed as arguments.
1501 public class OpcodeCast : EmptyCast {
1505 public OpcodeCast (Expression child, Type return_type, OpCode op)
1506 : base (child, return_type)
1510 second_valid = false;
1513 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1514 : base (child, return_type)
1519 second_valid = true;
1522 public override Expression DoResolve (TypeContainer tc)
1524 // This should never be invoked, we are born in fully
1525 // initialized state.
1530 public override void Emit (EmitContext ec)
1542 // This kind of cast is used to encapsulate a child and cast it
1543 // to the class requested
1545 public class ClassCast : EmptyCast {
1546 public ClassCast (Expression child, Type return_type)
1547 : base (child, return_type)
1552 public override Expression DoResolve (TypeContainer tc)
1554 // This should never be invoked, we are born in fully
1555 // initialized state.
1560 public override void Emit (EmitContext ec)
1564 ec.ig.Emit (OpCodes.Castclass, type);
1570 // Unary expressions.
1574 // Unary implements unary expressions. It derives from
1575 // ExpressionStatement becuase the pre/post increment/decrement
1576 // operators can be used in a statement context.
1578 public class Unary : ExpressionStatement {
1579 public enum Operator {
1580 Addition, Subtraction, Negate, BitComplement,
1581 Indirection, AddressOf, PreIncrement,
1582 PreDecrement, PostIncrement, PostDecrement
1587 ArrayList Arguments;
1591 public Unary (Operator op, Expression expr, Location loc)
1595 this.location = loc;
1598 public Expression Expr {
1608 public Operator Oper {
1619 // Returns a stringified representation of the Operator
1624 case Operator.Addition:
1626 case Operator.Subtraction:
1628 case Operator.Negate:
1630 case Operator.BitComplement:
1632 case Operator.AddressOf:
1634 case Operator.Indirection:
1636 case Operator.PreIncrement : case Operator.PostIncrement :
1638 case Operator.PreDecrement : case Operator.PostDecrement :
1642 return oper.ToString ();
1645 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1647 if (expr.Type == target_type)
1650 return ConvertImplicit (tc, expr, target_type, new Location (-1));
1653 void error23 (TypeContainer tc, Type t)
1655 Report.Error (23, location, "Operator " + OperName () +
1656 " cannot be applied to operand of type `" +
1657 TypeManager.CSharpName (t) + "'");
1661 // Returns whether an object of type `t' can be incremented
1662 // or decremented with add/sub (ie, basically whether we can
1663 // use pre-post incr-decr operations on it, but it is not a
1664 // System.Decimal, which we test elsewhere)
1666 static bool IsIncrementableNumber (Type t)
1668 return (t == TypeManager.sbyte_type) ||
1669 (t == TypeManager.byte_type) ||
1670 (t == TypeManager.short_type) ||
1671 (t == TypeManager.ushort_type) ||
1672 (t == TypeManager.int32_type) ||
1673 (t == TypeManager.uint32_type) ||
1674 (t == TypeManager.int64_type) ||
1675 (t == TypeManager.uint64_type) ||
1676 (t == TypeManager.char_type) ||
1677 (t.IsSubclassOf (TypeManager.enum_type)) ||
1678 (t == TypeManager.float_type) ||
1679 (t == TypeManager.double_type);
1682 Expression ResolveOperator (TypeContainer tc)
1684 Type expr_type = expr.Type;
1687 // Step 1: Perform Operator Overload location
1692 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1693 op_name = "op_Increment";
1694 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1695 op_name = "op_Decrement";
1697 op_name = "op_" + oper;
1699 mg = MemberLookup (tc, expr_type, op_name, false);
1701 if (mg == null && expr_type.BaseType != null)
1702 mg = MemberLookup (tc, expr_type.BaseType, op_name, false);
1705 Arguments = new ArrayList ();
1706 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1708 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg,
1709 Arguments, location);
1710 if (method != null) {
1711 MethodInfo mi = (MethodInfo) method;
1712 type = mi.ReturnType;
1715 error23 (tc, expr_type);
1722 // Step 2: Default operations on CLI native types.
1725 // Only perform numeric promotions on:
1728 if (expr_type == null)
1731 if (oper == Operator.Negate){
1732 if (expr_type != TypeManager.bool_type) {
1733 error23 (tc, expr.Type);
1737 type = TypeManager.bool_type;
1741 if (oper == Operator.BitComplement) {
1742 if (!((expr_type == TypeManager.int32_type) ||
1743 (expr_type == TypeManager.uint32_type) ||
1744 (expr_type == TypeManager.int64_type) ||
1745 (expr_type == TypeManager.uint64_type) ||
1746 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1747 error23 (tc, expr.Type);
1754 if (oper == Operator.Addition) {
1756 // A plus in front of something is just a no-op, so return the child.
1762 // Deals with -literals
1763 // int operator- (int x)
1764 // long operator- (long x)
1765 // float operator- (float f)
1766 // double operator- (double d)
1767 // decimal operator- (decimal d)
1769 if (oper == Operator.Subtraction){
1771 // Fold a "- Constant" into a negative constant
1774 Expression e = null;
1777 // Is this a constant?
1779 if (expr is IntLiteral)
1780 e = new IntLiteral (-((IntLiteral) expr).Value);
1781 else if (expr is LongLiteral)
1782 e = new LongLiteral (-((LongLiteral) expr).Value);
1783 else if (expr is FloatLiteral)
1784 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1785 else if (expr is DoubleLiteral)
1786 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1787 else if (expr is DecimalLiteral)
1788 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1796 // Not a constant we can optimize, perform numeric
1797 // promotions to int, long, double.
1800 // The following is inneficient, because we call
1801 // ConvertImplicit too many times.
1803 // It is also not clear if we should convert to Float
1804 // or Double initially.
1806 Location l = new Location (-1);
1808 if (expr_type == TypeManager.uint32_type){
1810 // FIXME: handle exception to this rule that
1811 // permits the int value -2147483648 (-2^31) to
1812 // bt written as a decimal interger literal
1814 type = TypeManager.int64_type;
1815 expr = ConvertImplicit (tc, expr, type, l);
1819 if (expr_type == TypeManager.uint64_type){
1821 // FIXME: Handle exception of `long value'
1822 // -92233720368547758087 (-2^63) to be written as
1823 // decimal integer literal.
1825 error23 (tc, expr_type);
1829 e = ConvertImplicit (tc, expr, TypeManager.int32_type, l);
1836 e = ConvertImplicit (tc, expr, TypeManager.int64_type, l);
1843 e = ConvertImplicit (tc, expr, TypeManager.double_type, l);
1850 error23 (tc, expr_type);
1855 // The operand of the prefix/postfix increment decrement operators
1856 // should be an expression that is classified as a variable,
1857 // a property access or an indexer access
1859 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1860 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1861 if (expr.ExprClass == ExprClass.Variable){
1862 if (IsIncrementableNumber (expr_type) ||
1863 expr_type == TypeManager.decimal_type){
1867 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1869 // FIXME: Verify that we have both get and set methods
1871 throw new Exception ("Implement me");
1872 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1874 // FIXME: Verify that we have both get and set methods
1876 throw new Exception ("Implement me");
1878 report118 (tc, location, expr,
1879 "variable, indexer or property access");
1883 if (oper == Operator.AddressOf){
1884 if (expr.ExprClass != ExprClass.Variable){
1885 Error (tc, 211, "Cannot take the address of non-variables");
1888 type = Type.GetType (expr.Type.ToString () + "*");
1891 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1892 TypeManager.CSharpName (expr_type) + "'");
1897 public override Expression DoResolve (TypeContainer tc)
1899 expr = expr.Resolve (tc);
1904 eclass = ExprClass.Value;
1905 return ResolveOperator (tc);
1908 public override void Emit (EmitContext ec)
1910 ILGenerator ig = ec.ig;
1911 Type expr_type = expr.Type;
1913 if (method != null) {
1915 // Note that operators are static anyway
1917 if (Arguments != null)
1918 Invocation.EmitArguments (ec, method, Arguments);
1921 // Post increment/decrement operations need a copy at this
1924 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1925 ig.Emit (OpCodes.Dup);
1928 ig.Emit (OpCodes.Call, (MethodInfo) method);
1931 // Pre Increment and Decrement operators
1933 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1934 ig.Emit (OpCodes.Dup);
1938 // Increment and Decrement should store the result
1940 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1941 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1942 ((LValue) expr).Store (ec);
1948 case Operator.Addition:
1949 throw new Exception ("This should be caught by Resolve");
1951 case Operator.Subtraction:
1953 ig.Emit (OpCodes.Neg);
1956 case Operator.Negate:
1958 ig.Emit (OpCodes.Ldc_I4_0);
1959 ig.Emit (OpCodes.Ceq);
1962 case Operator.BitComplement:
1964 ig.Emit (OpCodes.Not);
1967 case Operator.AddressOf:
1968 ((LValue)expr).AddressOf (ec);
1971 case Operator.Indirection:
1972 throw new Exception ("Not implemented yet");
1974 case Operator.PreIncrement:
1975 case Operator.PreDecrement:
1976 if (expr.ExprClass == ExprClass.Variable){
1978 // Resolve already verified that it is an "incrementable"
1981 ig.Emit (OpCodes.Ldc_I4_1);
1983 if (oper == Operator.PreDecrement)
1984 ig.Emit (OpCodes.Sub);
1986 ig.Emit (OpCodes.Add);
1987 ig.Emit (OpCodes.Dup);
1988 ((LValue) expr).Store (ec);
1990 throw new Exception ("Handle Indexers and Properties here");
1994 case Operator.PostIncrement:
1995 case Operator.PostDecrement:
1996 if (expr.ExprClass == ExprClass.Variable){
1998 // Resolve already verified that it is an "incrementable"
2001 ig.Emit (OpCodes.Dup);
2002 ig.Emit (OpCodes.Ldc_I4_1);
2004 if (oper == Operator.PostDecrement)
2005 ig.Emit (OpCodes.Sub);
2007 ig.Emit (OpCodes.Add);
2008 ((LValue) expr).Store (ec);
2010 throw new Exception ("Handle Indexers and Properties here");
2015 throw new Exception ("This should not happen: Operator = "
2016 + oper.ToString ());
2021 public override void EmitStatement (EmitContext ec)
2024 // FIXME: we should rewrite this code to generate
2025 // better code for ++ and -- as we know we wont need
2026 // the values on the stack
2029 ec.ig.Emit (OpCodes.Pop);
2033 public class Probe : Expression {
2034 public readonly string ProbeType;
2035 public readonly Operator Oper;
2039 public enum Operator {
2043 public Probe (Operator oper, Expression expr, string probe_type)
2046 ProbeType = probe_type;
2050 public Expression Expr {
2056 public override Expression DoResolve (TypeContainer tc)
2058 probe_type = tc.LookupType (ProbeType, false);
2060 if (probe_type == null)
2063 expr = expr.Resolve (tc);
2065 type = TypeManager.bool_type;
2066 eclass = ExprClass.Value;
2071 public override void Emit (EmitContext ec)
2073 ILGenerator ig = ec.ig;
2077 if (Oper == Operator.Is){
2078 ig.Emit (OpCodes.Isinst, probe_type);
2079 ig.Emit (OpCodes.Ldnull);
2080 ig.Emit (OpCodes.Cgt_Un);
2082 ig.Emit (OpCodes.Isinst, probe_type);
2088 // This represents a typecast in the source language.
2090 // FIXME: Cast expressions have an unusual set of parsing
2091 // rules, we need to figure those out.
2093 public class Cast : Expression {
2098 public Cast (string cast_type, Expression expr, Location loc)
2100 this.target_type = cast_type;
2102 this.location = loc;
2105 public string TargetType {
2111 public Expression Expr {
2120 public override Expression DoResolve (TypeContainer tc)
2122 expr = expr.Resolve (tc);
2126 type = tc.LookupType (target_type, false);
2127 eclass = ExprClass.Value;
2132 expr = ConvertExplicit (tc, expr, type, location);
2137 public override void Emit (EmitContext ec)
2140 // This one will never happen
2142 throw new Exception ("Should not happen");
2146 public class Binary : Expression {
2147 public enum Operator {
2148 Multiply, Division, Modulus,
2149 Addition, Subtraction,
2150 LeftShift, RightShift,
2151 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
2152 Equality, Inequality,
2161 Expression left, right;
2163 ArrayList Arguments;
2167 public Binary (Operator oper, Expression left, Expression right, Location loc)
2172 this.location = loc;
2175 public Operator Oper {
2184 public Expression Left {
2193 public Expression Right {
2204 // Returns a stringified representation of the Operator
2209 case Operator.Multiply:
2211 case Operator.Division:
2213 case Operator.Modulus:
2215 case Operator.Addition:
2217 case Operator.Subtraction:
2219 case Operator.LeftShift:
2221 case Operator.RightShift:
2223 case Operator.LessThan:
2225 case Operator.GreaterThan:
2227 case Operator.LessThanOrEqual:
2229 case Operator.GreaterThanOrEqual:
2231 case Operator.Equality:
2233 case Operator.Inequality:
2235 case Operator.BitwiseAnd:
2237 case Operator.BitwiseOr:
2239 case Operator.ExclusiveOr:
2241 case Operator.LogicalOr:
2243 case Operator.LogicalAnd:
2247 return oper.ToString ();
2250 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
2252 if (expr.Type == target_type)
2255 return ConvertImplicit (tc, expr, target_type, new Location (-1));
2259 // Note that handling the case l == Decimal || r == Decimal
2260 // is taken care of by the Step 1 Operator Overload resolution.
2262 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
2264 if (l == TypeManager.double_type || r == TypeManager.double_type){
2266 // If either operand is of type double, the other operand is
2267 // conveted to type double.
2269 if (r != TypeManager.double_type)
2270 right = ConvertImplicit (tc, right, TypeManager.double_type, location);
2271 if (l != TypeManager.double_type)
2272 left = ConvertImplicit (tc, left, TypeManager.double_type, location);
2274 type = TypeManager.double_type;
2275 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
2277 // if either operand is of type float, th eother operand is
2278 // converd to type float.
2280 if (r != TypeManager.double_type)
2281 right = ConvertImplicit (tc, right, TypeManager.float_type, location);
2282 if (l != TypeManager.double_type)
2283 left = ConvertImplicit (tc, left, TypeManager.float_type, location);
2284 type = TypeManager.float_type;
2285 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
2289 // If either operand is of type ulong, the other operand is
2290 // converted to type ulong. or an error ocurrs if the other
2291 // operand is of type sbyte, short, int or long
2294 if (l == TypeManager.uint64_type){
2295 if (r != TypeManager.uint64_type && right is IntLiteral){
2296 e = TryImplicitIntConversion (l, (IntLiteral) right);
2302 if (left is IntLiteral){
2303 e = TryImplicitIntConversion (r, (IntLiteral) left);
2310 if ((other == TypeManager.sbyte_type) ||
2311 (other == TypeManager.short_type) ||
2312 (other == TypeManager.int32_type) ||
2313 (other == TypeManager.int64_type)){
2314 string oper = OperName ();
2316 Error (tc, 34, location, "Operator `" + OperName ()
2317 + "' is ambiguous on operands of type `"
2318 + TypeManager.CSharpName (l) + "' "
2319 + "and `" + TypeManager.CSharpName (r)
2322 type = TypeManager.uint64_type;
2323 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
2325 // If either operand is of type long, the other operand is converted
2328 if (l != TypeManager.int64_type)
2329 left = ConvertImplicit (tc, left, TypeManager.int64_type, location);
2330 if (r != TypeManager.int64_type)
2331 right = ConvertImplicit (tc, right, TypeManager.int64_type, location);
2333 type = TypeManager.int64_type;
2334 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
2336 // If either operand is of type uint, and the other
2337 // operand is of type sbyte, short or int, othe operands are
2338 // converted to type long.
2342 if (l == TypeManager.uint32_type)
2344 else if (r == TypeManager.uint32_type)
2347 if ((other == TypeManager.sbyte_type) ||
2348 (other == TypeManager.short_type) ||
2349 (other == TypeManager.int32_type)){
2350 left = ForceConversion (tc, left, TypeManager.int64_type);
2351 right = ForceConversion (tc, right, TypeManager.int64_type);
2352 type = TypeManager.int64_type;
2355 // if either operand is of type uint, the other
2356 // operand is converd to type uint
2358 left = ForceConversion (tc, left, TypeManager.uint32_type);
2359 right = ForceConversion (tc, right, TypeManager.uint32_type);
2360 type = TypeManager.uint32_type;
2362 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2363 if (l != TypeManager.decimal_type)
2364 left = ConvertImplicit (tc, left, TypeManager.decimal_type, location);
2365 if (r != TypeManager.decimal_type)
2366 right = ConvertImplicit (tc, right, TypeManager.decimal_type, location);
2368 type = TypeManager.decimal_type;
2370 Expression l_tmp, r_tmp;
2372 l_tmp = ForceConversion (tc, left, TypeManager.int32_type);
2373 if (l_tmp == null) {
2379 r_tmp = ForceConversion (tc, right, TypeManager.int32_type);
2380 if (r_tmp == null) {
2386 type = TypeManager.int32_type;
2390 void error19 (TypeContainer tc)
2392 Error (tc, 19, location,
2393 "Operator " + OperName () + " cannot be applied to operands of type `" +
2394 TypeManager.CSharpName (left.Type) + "' and `" +
2395 TypeManager.CSharpName (right.Type) + "'");
2399 Expression CheckShiftArguments (TypeContainer tc)
2403 Type r = right.Type;
2405 e = ForceConversion (tc, right, TypeManager.int32_type);
2412 Location loc = location;
2414 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type, loc)) != null) ||
2415 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type, loc)) != null) ||
2416 ((e = ConvertImplicit (tc, left, TypeManager.int64_type, loc)) != null) ||
2417 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type, loc)) != null)){
2427 Expression ResolveOperator (TypeContainer tc)
2430 Type r = right.Type;
2433 // Step 1: Perform Operator Overload location
2435 Expression left_expr, right_expr;
2437 string op = "op_" + oper;
2439 left_expr = MemberLookup (tc, l, op, false);
2440 if (left_expr == null && l.BaseType != null)
2441 left_expr = MemberLookup (tc, l.BaseType, op, false);
2443 right_expr = MemberLookup (tc, r, op, false);
2444 if (right_expr == null && r.BaseType != null)
2445 right_expr = MemberLookup (tc, r.BaseType, op, false);
2447 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2449 if (union != null) {
2450 Arguments = new ArrayList ();
2451 Arguments.Add (new Argument (left, Argument.AType.Expression));
2452 Arguments.Add (new Argument (right, Argument.AType.Expression));
2454 method = Invocation.OverloadResolve (tc, union, Arguments, location);
2455 if (method != null) {
2456 MethodInfo mi = (MethodInfo) method;
2457 type = mi.ReturnType;
2466 // Step 2: Default operations on CLI native types.
2469 // Only perform numeric promotions on:
2470 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2472 if (oper == Operator.Addition){
2474 // If any of the arguments is a string, cast to string
2476 if (l == TypeManager.string_type){
2477 if (r == TypeManager.string_type){
2479 method = TypeManager.string_concat_string_string;
2482 method = TypeManager.string_concat_object_object;
2483 right = ConvertImplicit (tc, right,
2484 TypeManager.object_type, location);
2486 type = TypeManager.string_type;
2488 Arguments = new ArrayList ();
2489 Arguments.Add (new Argument (left, Argument.AType.Expression));
2490 Arguments.Add (new Argument (right, Argument.AType.Expression));
2494 } else if (r == TypeManager.string_type){
2496 method = TypeManager.string_concat_object_object;
2497 Arguments = new ArrayList ();
2498 Arguments.Add (new Argument (left, Argument.AType.Expression));
2499 Arguments.Add (new Argument (right, Argument.AType.Expression));
2501 left = ConvertImplicit (tc, left, TypeManager.object_type, location);
2502 type = TypeManager.string_type;
2508 // FIXME: is Delegate operator + (D x, D y) handled?
2512 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2513 return CheckShiftArguments (tc);
2515 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2516 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2519 type = TypeManager.bool_type;
2524 // We are dealing with numbers
2527 DoNumericPromotions (tc, l, r);
2529 if (left == null || right == null)
2533 if (oper == Operator.BitwiseAnd ||
2534 oper == Operator.BitwiseOr ||
2535 oper == Operator.ExclusiveOr){
2536 if (!((l == TypeManager.int32_type) ||
2537 (l == TypeManager.uint32_type) ||
2538 (l == TypeManager.int64_type) ||
2539 (l == TypeManager.uint64_type))){
2546 if (oper == Operator.Equality ||
2547 oper == Operator.Inequality ||
2548 oper == Operator.LessThanOrEqual ||
2549 oper == Operator.LessThan ||
2550 oper == Operator.GreaterThanOrEqual ||
2551 oper == Operator.GreaterThan){
2552 type = TypeManager.bool_type;
2558 public override Expression DoResolve (TypeContainer tc)
2560 left = left.Resolve (tc);
2561 right = right.Resolve (tc);
2563 if (left == null || right == null)
2566 if (left.Type == null)
2567 throw new Exception (
2568 "Resolve returned non null, but did not set the type! (" +
2570 if (right.Type == null)
2571 throw new Exception (
2572 "Resolve returned non null, but did not set the type! (" +
2575 eclass = ExprClass.Value;
2577 return ResolveOperator (tc);
2580 public bool IsBranchable ()
2582 if (oper == Operator.Equality ||
2583 oper == Operator.Inequality ||
2584 oper == Operator.LessThan ||
2585 oper == Operator.GreaterThan ||
2586 oper == Operator.LessThanOrEqual ||
2587 oper == Operator.GreaterThanOrEqual){
2594 // This entry point is used by routines that might want
2595 // to emit a brfalse/brtrue after an expression, and instead
2596 // they could use a more compact notation.
2598 // Typically the code would generate l.emit/r.emit, followed
2599 // by the comparission and then a brtrue/brfalse. The comparissions
2600 // are sometimes inneficient (there are not as complete as the branches
2601 // look for the hacks in Emit using double ceqs).
2603 // So for those cases we provide EmitBranchable that can emit the
2604 // branch with the test
2606 public void EmitBranchable (EmitContext ec, int target)
2609 bool close_target = false;
2615 case Operator.Equality:
2617 opcode = OpCodes.Beq_S;
2619 opcode = OpCodes.Beq;
2622 case Operator.Inequality:
2624 opcode = OpCodes.Bne_Un_S;
2626 opcode = OpCodes.Bne_Un;
2629 case Operator.LessThan:
2631 opcode = OpCodes.Blt_S;
2633 opcode = OpCodes.Blt;
2636 case Operator.GreaterThan:
2638 opcode = OpCodes.Bgt_S;
2640 opcode = OpCodes.Bgt;
2643 case Operator.LessThanOrEqual:
2645 opcode = OpCodes.Ble_S;
2647 opcode = OpCodes.Ble;
2650 case Operator.GreaterThanOrEqual:
2652 opcode = OpCodes.Bge_S;
2654 opcode = OpCodes.Ble;
2658 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2659 + oper.ToString ());
2662 ec.ig.Emit (opcode, target);
2665 public override void Emit (EmitContext ec)
2667 ILGenerator ig = ec.ig;
2669 Type r = right.Type;
2672 if (method != null) {
2674 // Note that operators are static anyway
2676 if (Arguments != null)
2677 Invocation.EmitArguments (ec, method, Arguments);
2679 if (method is MethodInfo)
2680 ig.Emit (OpCodes.Call, (MethodInfo) method);
2682 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2691 case Operator.Multiply:
2693 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2694 opcode = OpCodes.Mul_Ovf;
2695 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2696 opcode = OpCodes.Mul_Ovf_Un;
2698 opcode = OpCodes.Mul;
2700 opcode = OpCodes.Mul;
2704 case Operator.Division:
2705 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2706 opcode = OpCodes.Div_Un;
2708 opcode = OpCodes.Div;
2711 case Operator.Modulus:
2712 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2713 opcode = OpCodes.Rem_Un;
2715 opcode = OpCodes.Rem;
2718 case Operator.Addition:
2720 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2721 opcode = OpCodes.Add_Ovf;
2722 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2723 opcode = OpCodes.Add_Ovf_Un;
2725 opcode = OpCodes.Mul;
2727 opcode = OpCodes.Add;
2730 case Operator.Subtraction:
2732 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2733 opcode = OpCodes.Sub_Ovf;
2734 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2735 opcode = OpCodes.Sub_Ovf_Un;
2737 opcode = OpCodes.Sub;
2739 opcode = OpCodes.Sub;
2742 case Operator.RightShift:
2743 opcode = OpCodes.Shr;
2746 case Operator.LeftShift:
2747 opcode = OpCodes.Shl;
2750 case Operator.Equality:
2751 opcode = OpCodes.Ceq;
2754 case Operator.Inequality:
2755 ec.ig.Emit (OpCodes.Ceq);
2756 ec.ig.Emit (OpCodes.Ldc_I4_0);
2758 opcode = OpCodes.Ceq;
2761 case Operator.LessThan:
2762 opcode = OpCodes.Clt;
2765 case Operator.GreaterThan:
2766 opcode = OpCodes.Cgt;
2769 case Operator.LessThanOrEqual:
2770 ec.ig.Emit (OpCodes.Cgt);
2771 ec.ig.Emit (OpCodes.Ldc_I4_0);
2773 opcode = OpCodes.Ceq;
2776 case Operator.GreaterThanOrEqual:
2777 ec.ig.Emit (OpCodes.Clt);
2778 ec.ig.Emit (OpCodes.Ldc_I4_1);
2780 opcode = OpCodes.Sub;
2783 case Operator.LogicalOr:
2784 case Operator.BitwiseOr:
2785 opcode = OpCodes.Or;
2788 case Operator.LogicalAnd:
2789 case Operator.BitwiseAnd:
2790 opcode = OpCodes.And;
2793 case Operator.ExclusiveOr:
2794 opcode = OpCodes.Xor;
2798 throw new Exception ("This should not happen: Operator = "
2799 + oper.ToString ());
2806 public class Conditional : Expression {
2807 Expression expr, trueExpr, falseExpr;
2810 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2813 this.trueExpr = trueExpr;
2814 this.falseExpr = falseExpr;
2818 public Expression Expr {
2824 public Expression TrueExpr {
2830 public Expression FalseExpr {
2836 public override Expression DoResolve (TypeContainer tc)
2838 expr = expr.Resolve (tc);
2840 if (expr.Type != TypeManager.bool_type)
2841 expr = Expression.ConvertImplicitRequired (
2842 tc, expr, TypeManager.bool_type, l);
2844 trueExpr = trueExpr.Resolve (tc);
2845 falseExpr = falseExpr.Resolve (tc);
2847 if (expr == null || trueExpr == null || falseExpr == null)
2850 if (trueExpr.Type == falseExpr.Type)
2851 type = trueExpr.Type;
2856 // First, if an implicit conversion exists from trueExpr
2857 // to falseExpr, then the result type is of type falseExpr.Type
2859 conv = ConvertImplicit (tc, trueExpr, falseExpr.Type, l);
2861 type = falseExpr.Type;
2863 } else if ((conv = ConvertImplicit (tc,falseExpr,trueExpr.Type,l)) != null){
2864 type = trueExpr.Type;
2867 Error (tc, 173, l, "The type of the conditional expression can " +
2868 "not be computed because there is no implicit conversion" +
2869 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2870 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2875 eclass = ExprClass.Value;
2879 public override void Emit (EmitContext ec)
2881 ILGenerator ig = ec.ig;
2882 Label false_target = ig.DefineLabel ();
2883 Label end_target = ig.DefineLabel ();
2886 ig.Emit (OpCodes.Brfalse, false_target);
2888 ig.Emit (OpCodes.Br, end_target);
2889 ig.MarkLabel (false_target);
2890 falseExpr.Emit (ec);
2891 ig.MarkLabel (end_target);
2895 public class SimpleName : Expression {
2896 public readonly string Name;
2897 public readonly Location Location;
2899 public SimpleName (string name, Location l)
2906 // Checks whether we are trying to access an instance
2907 // property, method or field from a static body.
2909 Expression MemberStaticCheck (Expression e)
2911 if (e is FieldExpr){
2912 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2917 "An object reference is required " +
2918 "for the non-static field `"+Name+"'");
2921 } else if (e is MethodGroupExpr){
2922 // FIXME: Pending reorganization of MemberLookup
2923 // Basically at this point we should have the
2924 // best match already selected for us, and
2925 // we should only have to check a *single*
2926 // Method for its static on/off bit.
2928 } else if (e is PropertyExpr){
2929 if (!((PropertyExpr) e).IsStatic){
2931 "An object reference is required " +
2932 "for the non-static property access `"+
2942 // 7.5.2: Simple Names.
2944 // Local Variables and Parameters are handled at
2945 // parse time, so they never occur as SimpleNames.
2947 Expression ResolveSimpleName (TypeContainer tc)
2951 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2955 else if (e is FieldExpr){
2956 FieldExpr fe = (FieldExpr) e;
2958 if (!fe.FieldInfo.IsStatic)
2959 fe.Instance = new This ();
2962 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2963 return MemberStaticCheck (e);
2969 // Do step 3 of the Simple Name resolution.
2971 // FIXME: implement me.
2973 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
2980 // SimpleName needs to handle a multitude of cases:
2982 // simple_names and qualified_identifiers are placed on
2983 // the tree equally.
2985 public override Expression DoResolve (TypeContainer tc)
2987 if (Name.IndexOf (".") != -1)
2988 return ResolveMemberAccess (tc, Name);
2990 return ResolveSimpleName (tc);
2993 public override void Emit (EmitContext ec)
2995 throw new Exception ("SimpleNames should be gone from the tree");
3000 // A simple interface that should be implemeneted by LValues
3002 public interface LValue {
3005 // The Store method should store the contents of the top
3006 // of the stack into the storage that is implemented by
3007 // the particular implementation of LValue
3009 void Store (EmitContext ec);
3012 // The AddressOf method should generate code that loads
3013 // the address of the LValue and leaves it on the stack
3015 void AddressOf (EmitContext ec);
3018 public class LocalVariableReference : Expression, LValue {
3019 public readonly string Name;
3020 public readonly Block Block;
3022 public LocalVariableReference (Block block, string name)
3026 eclass = ExprClass.Variable;
3029 public VariableInfo VariableInfo {
3031 return Block.GetVariableInfo (Name);
3035 public override Expression DoResolve (TypeContainer tc)
3037 VariableInfo vi = Block.GetVariableInfo (Name);
3039 type = vi.VariableType;
3043 public override void Emit (EmitContext ec)
3045 VariableInfo vi = VariableInfo;
3046 ILGenerator ig = ec.ig;
3053 ig.Emit (OpCodes.Ldloc_0);
3057 ig.Emit (OpCodes.Ldloc_1);
3061 ig.Emit (OpCodes.Ldloc_2);
3065 ig.Emit (OpCodes.Ldloc_3);
3070 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3072 ig.Emit (OpCodes.Ldloc, idx);
3077 public static void Store (ILGenerator ig, int idx)
3081 ig.Emit (OpCodes.Stloc_0);
3085 ig.Emit (OpCodes.Stloc_1);
3089 ig.Emit (OpCodes.Stloc_2);
3093 ig.Emit (OpCodes.Stloc_3);
3098 ig.Emit (OpCodes.Stloc_S, (byte) idx);
3100 ig.Emit (OpCodes.Stloc, idx);
3105 public void Store (EmitContext ec)
3107 ILGenerator ig = ec.ig;
3108 VariableInfo vi = VariableInfo;
3112 // Funny seems the above generates optimal code for us, but
3113 // seems to take too long to generate what we need.
3114 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3119 public void AddressOf (EmitContext ec)
3121 VariableInfo vi = VariableInfo;
3128 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3130 ec.ig.Emit (OpCodes.Ldloca, idx);
3134 public class ParameterReference : Expression, LValue {
3135 public readonly Parameters Pars;
3136 public readonly String Name;
3137 public readonly int Idx;
3139 public ParameterReference (Parameters pars, int idx, string name)
3144 eclass = ExprClass.Variable;
3147 public override Expression DoResolve (TypeContainer tc)
3149 Type [] types = Pars.GetParameterInfo (tc);
3156 public override void Emit (EmitContext ec)
3159 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
3161 ec.ig.Emit (OpCodes.Ldarg, Idx);
3164 public void Store (EmitContext ec)
3167 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
3169 ec.ig.Emit (OpCodes.Starg, Idx);
3173 public void AddressOf (EmitContext ec)
3176 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
3178 ec.ig.Emit (OpCodes.Ldarga, Idx);
3183 // Used for arguments to New(), Invocation()
3185 public class Argument {
3192 public readonly AType Type;
3195 public Argument (Expression expr, AType type)
3201 public Expression Expr {
3211 public bool Resolve (TypeContainer tc)
3213 expr = expr.Resolve (tc);
3215 return expr != null;
3218 public void Emit (EmitContext ec)
3225 // Invocation of methods or delegates.
3227 public class Invocation : ExpressionStatement {
3228 public readonly ArrayList Arguments;
3229 public readonly Location Location;
3232 MethodBase method = null;
3234 static Hashtable method_parameter_cache;
3236 static Invocation ()
3238 method_parameter_cache = new Hashtable ();
3242 // arguments is an ArrayList, but we do not want to typecast,
3243 // as it might be null.
3245 // FIXME: only allow expr to be a method invocation or a
3246 // delegate invocation (7.5.5)
3248 public Invocation (Expression expr, ArrayList arguments, Location l)
3251 Arguments = arguments;
3255 public Expression Expr {
3262 // Returns the Parameters (a ParameterData interface) for the
3265 public static ParameterData GetParameterData (MethodBase mb)
3267 object pd = method_parameter_cache [mb];
3270 return (ParameterData) pd;
3272 if (mb is MethodBuilder || mb is ConstructorBuilder){
3273 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
3275 InternalParameters ip = mc.ParameterInfo;
3276 method_parameter_cache [mb] = ip;
3278 return (ParameterData) ip;
3280 ParameterInfo [] pi = mb.GetParameters ();
3281 ReflectionParameters rp = new ReflectionParameters (pi);
3282 method_parameter_cache [mb] = rp;
3284 return (ParameterData) rp;
3289 // Tells whether a user defined conversion from Type `from' to
3290 // Type `to' exists.
3292 // FIXME: we could implement a cache here.
3294 static bool ConversionExists (TypeContainer tc, Type from, Type to)
3296 // Locate user-defined implicit operators
3300 mg = MemberLookup (tc, to, "op_Implicit", false);
3303 MethodGroupExpr me = (MethodGroupExpr) mg;
3305 for (int i = me.Methods.Length; i > 0;) {
3307 MethodBase mb = me.Methods [i];
3308 ParameterData pd = GetParameterData (mb);
3310 if (from == pd.ParameterType (0))
3315 mg = MemberLookup (tc, from, "op_Implicit", false);
3318 MethodGroupExpr me = (MethodGroupExpr) mg;
3320 for (int i = me.Methods.Length; i > 0;) {
3322 MethodBase mb = me.Methods [i];
3323 MethodInfo mi = (MethodInfo) mb;
3325 if (mi.ReturnType == to)
3334 // Determines "better conversion" as specified in 7.4.2.3
3335 // Returns : 1 if a->p is better
3336 // 0 if a->q or neither is better
3338 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q, bool use_standard)
3341 Type argument_type = a.Expr.Type;
3342 Expression argument_expr = a.Expr;
3344 if (argument_type == null)
3345 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3350 if (argument_type == p)
3353 if (argument_type == q)
3357 // Now probe whether an implicit constant expression conversion
3360 // An implicit constant expression conversion permits the following
3363 // * A constant-expression of type `int' can be converted to type
3364 // sbyte, byute, short, ushort, uint, ulong provided the value of
3365 // of the expression is withing the range of the destination type.
3367 // * A constant-expression of type long can be converted to type
3368 // ulong, provided the value of the constant expression is not negative
3370 // FIXME: Note that this assumes that constant folding has
3371 // taken place. We dont do constant folding yet.
3374 if (argument_expr is IntLiteral){
3375 IntLiteral ei = (IntLiteral) argument_expr;
3376 int value = ei.Value;
3378 if (p == TypeManager.sbyte_type){
3379 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3381 } else if (p == TypeManager.byte_type){
3382 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3384 } else if (p == TypeManager.short_type){
3385 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3387 } else if (p == TypeManager.ushort_type){
3388 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3390 } else if (p == TypeManager.uint32_type){
3392 // we can optimize this case: a positive int32
3393 // always fits on a uint32
3397 } else if (p == TypeManager.uint64_type){
3399 // we can optimize this case: a positive int32
3400 // always fits on a uint64
3405 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3406 LongLiteral ll = (LongLiteral) argument_expr;
3408 if (p == TypeManager.uint64_type){
3419 tmp = ConvertImplicitStandard (tc, argument_expr, p, Location.Null);
3421 tmp = ConvertImplicit (tc, argument_expr, p, Location.Null);
3430 if (ConversionExists (tc, p, q) == true &&
3431 ConversionExists (tc, q, p) == false)
3434 if (p == TypeManager.sbyte_type)
3435 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3436 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3439 if (p == TypeManager.short_type)
3440 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3441 q == TypeManager.uint64_type)
3444 if (p == TypeManager.int32_type)
3445 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3448 if (p == TypeManager.int64_type)
3449 if (q == TypeManager.uint64_type)
3456 // Determines "Better function" and returns an integer indicating :
3457 // 0 if candidate ain't better
3458 // 1 if candidate is better than the current best match
3460 static int BetterFunction (TypeContainer tc, ArrayList args,
3461 MethodBase candidate, MethodBase best,
3464 ParameterData candidate_pd = GetParameterData (candidate);
3465 ParameterData best_pd;
3471 argument_count = args.Count;
3473 if (candidate_pd.Count == 0 && argument_count == 0)
3477 if (candidate_pd.Count == argument_count) {
3479 for (int j = argument_count; j > 0;) {
3482 Argument a = (Argument) args [j];
3484 x = BetterConversion (
3485 tc, a, candidate_pd.ParameterType (j), null,
3501 best_pd = GetParameterData (best);
3503 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3504 int rating1 = 0, rating2 = 0;
3506 for (int j = argument_count; j > 0;) {
3510 Argument a = (Argument) args [j];
3512 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
3513 best_pd.ParameterType (j), use_standard);
3514 y = BetterConversion (tc, a, best_pd.ParameterType (j),
3515 candidate_pd.ParameterType (j), use_standard);
3521 if (rating1 > rating2)
3530 public static string FullMethodDesc (MethodBase mb)
3532 StringBuilder sb = new StringBuilder (mb.Name);
3533 ParameterData pd = GetParameterData (mb);
3536 for (int i = pd.Count; i > 0;) {
3538 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
3544 return sb.ToString ();
3547 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3549 MemberInfo [] miset;
3550 MethodGroupExpr union;
3552 if (mg1 != null && mg2 != null) {
3554 MethodGroupExpr left_set = null, right_set = null;
3555 int length1 = 0, length2 = 0;
3557 left_set = (MethodGroupExpr) mg1;
3558 length1 = left_set.Methods.Length;
3560 right_set = (MethodGroupExpr) mg2;
3561 length2 = right_set.Methods.Length;
3563 ArrayList common = new ArrayList ();
3565 for (int i = 0; i < left_set.Methods.Length; i++) {
3566 for (int j = 0; j < right_set.Methods.Length; j++) {
3567 if (left_set.Methods [i] == right_set.Methods [j])
3568 common.Add (left_set.Methods [i]);
3572 miset = new MemberInfo [length1 + length2 - common.Count];
3574 left_set.Methods.CopyTo (miset, 0);
3578 for (int j = 0; j < right_set.Methods.Length; j++)
3579 if (!common.Contains (right_set.Methods [j]))
3580 miset [length1 + k++] = right_set.Methods [j];
3582 union = new MethodGroupExpr (miset);
3586 } else if (mg1 == null && mg2 != null) {
3588 MethodGroupExpr me = (MethodGroupExpr) mg2;
3590 miset = new MemberInfo [me.Methods.Length];
3591 me.Methods.CopyTo (miset, 0);
3593 union = new MethodGroupExpr (miset);
3597 } else if (mg2 == null && mg1 != null) {
3599 MethodGroupExpr me = (MethodGroupExpr) mg1;
3601 miset = new MemberInfo [me.Methods.Length];
3602 me.Methods.CopyTo (miset, 0);
3604 union = new MethodGroupExpr (miset);
3613 // Find the Applicable Function Members (7.4.2.1)
3615 // me: Method Group expression with the members to select.
3616 // it might contain constructors or methods (or anything
3617 // that maps to a method).
3619 // Arguments: ArrayList containing resolved Argument objects.
3621 // loc: The location if we want an error to be reported, or a Null
3622 // location for "probing" purposes.
3624 // inside_user_defined: controls whether OverloadResolve should use the
3625 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
3627 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3628 // that is the best match of me on Arguments.
3631 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3632 ArrayList Arguments, Location loc,
3635 ArrayList afm = new ArrayList ();
3636 int best_match_idx = -1;
3637 MethodBase method = null;
3640 for (int i = me.Methods.Length; i > 0; ){
3642 MethodBase candidate = me.Methods [i];
3645 x = BetterFunction (tc, Arguments, candidate, method, use_standard);
3651 method = me.Methods [best_match_idx];
3655 if (Arguments == null)
3658 argument_count = Arguments.Count;
3662 // Now we see if we can at least find a method with the same number of arguments
3663 // and then try doing implicit conversion on the arguments
3664 if (best_match_idx == -1) {
3666 for (int i = me.Methods.Length; i > 0;) {
3668 MethodBase mb = me.Methods [i];
3669 pd = GetParameterData (mb);
3671 if (pd.Count == argument_count) {
3673 method = me.Methods [best_match_idx];
3684 // And now convert implicitly, each argument to the required type
3686 pd = GetParameterData (method);
3688 for (int j = argument_count; j > 0;) {
3690 Argument a = (Argument) Arguments [j];
3691 Expression a_expr = a.Expr;
3692 Type parameter_type = pd.ParameterType (j);
3694 if (a_expr.Type != parameter_type){
3698 conv = ConvertImplicitStandard (tc, a_expr, parameter_type,
3701 conv = ConvertImplicit (tc, a_expr, parameter_type,
3705 if (!Location.IsNull (loc)) {
3706 Error (tc, 1502, loc,
3707 "The best overloaded match for method '" + FullMethodDesc (method) +
3708 "' has some invalid arguments");
3709 Error (tc, 1503, loc,
3710 "Argument " + (j+1) +
3711 ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
3712 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
3717 // Update the argument with the implicit conversion
3727 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3728 ArrayList Arguments, Location loc)
3730 return OverloadResolve (tc, me, Arguments, loc, false);
3733 public override Expression DoResolve (TypeContainer tc)
3736 // First, resolve the expression that is used to
3737 // trigger the invocation
3739 this.expr = expr.Resolve (tc);
3740 if (this.expr == null)
3743 if (!(this.expr is MethodGroupExpr)){
3744 report118 (tc, Location, this.expr, "method group");
3749 // Next, evaluate all the expressions in the argument list
3751 if (Arguments != null){
3752 for (int i = Arguments.Count; i > 0;){
3754 Argument a = (Argument) Arguments [i];
3756 if (!a.Resolve (tc))
3761 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments,
3764 if (method == null){
3765 Error (tc, -6, Location,
3766 "Could not find any applicable function for this argument list");
3770 if (method is MethodInfo)
3771 type = ((MethodInfo)method).ReturnType;
3773 eclass = ExprClass.Value;
3777 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3781 if (Arguments != null)
3782 top = Arguments.Count;
3786 for (int i = 0; i < top; i++){
3787 Argument a = (Argument) Arguments [i];
3793 public override void Emit (EmitContext ec)
3795 bool is_static = method.IsStatic;
3798 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3801 // If this is ourselves, push "this"
3803 if (mg.InstanceExpression == null){
3804 ec.ig.Emit (OpCodes.Ldarg_0);
3807 // Push the instance expression
3809 mg.InstanceExpression.Emit (ec);
3813 if (Arguments != null)
3814 EmitArguments (ec, method, Arguments);
3817 if (method is MethodInfo)
3818 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3820 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3822 if (method is MethodInfo)
3823 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3825 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3829 public override void EmitStatement (EmitContext ec)
3834 // Pop the return value if there is one
3836 if (method is MethodInfo){
3837 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3838 ec.ig.Emit (OpCodes.Pop);
3843 public class New : ExpressionStatement {
3850 public readonly NType NewType;
3851 public readonly ArrayList Arguments;
3852 public readonly string RequestedType;
3853 // These are for the case when we have an array
3854 public readonly string Rank;
3855 public readonly ArrayList Indices;
3856 public readonly ArrayList Initializers;
3859 MethodBase method = null;
3861 public New (string requested_type, ArrayList arguments, Location loc)
3863 RequestedType = requested_type;
3864 Arguments = arguments;
3865 NewType = NType.Object;
3869 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3871 RequestedType = requested_type;
3874 Initializers = initializers;
3875 NewType = NType.Array;
3879 public override Expression DoResolve (TypeContainer tc)
3881 type = tc.LookupType (RequestedType, false);
3888 ml = MemberLookup (tc, type, ".ctor", false,
3889 MemberTypes.Constructor, AllBindingsFlags);
3891 if (! (ml is MethodGroupExpr)){
3893 // FIXME: Find proper error
3895 report118 (tc, Location, ml, "method group");
3899 if (Arguments != null){
3900 for (int i = Arguments.Count; i > 0;){
3902 Argument a = (Argument) Arguments [i];
3904 if (!a.Resolve (tc))
3909 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments,
3912 if (method == null) {
3913 Error (tc, -6, Location,
3914 "New invocation: Can not find a constructor for this argument list");
3918 eclass = ExprClass.Value;
3922 public override void Emit (EmitContext ec)
3924 Invocation.EmitArguments (ec, method, Arguments);
3925 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3928 public override void EmitStatement (EmitContext ec)
3931 ec.ig.Emit (OpCodes.Pop);
3936 // Represents the `this' construct
3938 public class This : Expression, LValue {
3939 public override Expression DoResolve (TypeContainer tc)
3941 eclass = ExprClass.Variable;
3942 type = tc.TypeBuilder;
3945 // FIXME: Verify that this is only used in instance contexts.
3950 public override void Emit (EmitContext ec)
3952 ec.ig.Emit (OpCodes.Ldarg_0);
3955 public void Store (EmitContext ec)
3958 // Assignment to the "this" variable.
3960 // FIXME: Apparently this is a bug that we
3961 // must catch as `this' seems to be readonly ;-)
3963 ec.ig.Emit (OpCodes.Starg, 0);
3966 public void AddressOf (EmitContext ec)
3968 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3973 // Implements the typeof operator
3975 public class TypeOf : Expression {
3976 public readonly string QueriedType;
3979 public TypeOf (string queried_type)
3981 QueriedType = queried_type;
3984 public override Expression DoResolve (TypeContainer tc)
3986 typearg = tc.LookupType (QueriedType, false);
3988 if (typearg == null)
3991 type = TypeManager.type_type;
3992 eclass = ExprClass.Type;
3996 public override void Emit (EmitContext ec)
3998 ec.ig.Emit (OpCodes.Ldtoken, typearg);
3999 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4003 public class SizeOf : Expression {
4004 public readonly string QueriedType;
4006 public SizeOf (string queried_type)
4008 this.QueriedType = queried_type;
4011 public override Expression DoResolve (TypeContainer tc)
4013 // FIXME: Implement;
4014 throw new Exception ("Unimplemented");
4018 public override void Emit (EmitContext ec)
4020 throw new Exception ("Implement me");
4024 public class MemberAccess : Expression {
4025 public readonly string Identifier;
4027 Expression member_lookup;
4029 public MemberAccess (Expression expr, string id)
4035 public Expression Expr {
4041 public override Expression DoResolve (TypeContainer tc)
4043 Expression new_expression = expr.Resolve (tc);
4045 if (new_expression == null)
4048 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
4050 if (member_lookup is MethodGroupExpr){
4051 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4054 // Bind the instance expression to it
4056 // FIXME: This is a horrible way of detecting if it is
4057 // an instance expression. Figure out how to fix this.
4060 if (expr is LocalVariableReference ||
4061 expr is ParameterReference ||
4063 mg.InstanceExpression = expr;
4065 return member_lookup;
4066 } else if (member_lookup is FieldExpr){
4067 FieldExpr fe = (FieldExpr) member_lookup;
4071 return member_lookup;
4074 // FIXME: This should generate the proper node
4075 // ie, for a Property Access, it should like call it
4078 return member_lookup;
4081 public override void Emit (EmitContext ec)
4083 throw new Exception ("Should not happen I think");
4089 // Nodes of type Namespace are created during the semantic
4090 // analysis to resolve member_access/qualified_identifier/simple_name
4093 // They are born `resolved'.
4095 public class NamespaceExpr : Expression {
4096 public readonly string Name;
4098 public NamespaceExpr (string name)
4101 eclass = ExprClass.Namespace;
4104 public override Expression DoResolve (TypeContainer tc)
4109 public override void Emit (EmitContext ec)
4111 throw new Exception ("Namespace expressions should never be emitted");
4116 // Fully resolved expression that evaluates to a type
4118 public class TypeExpr : Expression {
4119 public TypeExpr (Type t)
4122 eclass = ExprClass.Type;
4125 override public Expression DoResolve (TypeContainer tc)
4130 override public void Emit (EmitContext ec)
4132 throw new Exception ("Implement me");
4137 // MethodGroup Expression.
4139 // This is a fully resolved expression that evaluates to a type
4141 public class MethodGroupExpr : Expression {
4142 public readonly MethodBase [] Methods;
4143 Expression instance_expression = null;
4145 public MethodGroupExpr (MemberInfo [] mi)
4147 Methods = new MethodBase [mi.Length];
4148 mi.CopyTo (Methods, 0);
4149 eclass = ExprClass.MethodGroup;
4153 // `A method group may have associated an instance expression'
4155 public Expression InstanceExpression {
4157 return instance_expression;
4161 instance_expression = value;
4165 override public Expression DoResolve (TypeContainer tc)
4170 override public void Emit (EmitContext ec)
4172 throw new Exception ("This should never be reached");
4176 // Fully resolved expression that evaluates to a Field
4178 public class FieldExpr : Expression, LValue {
4179 public readonly FieldInfo FieldInfo;
4180 public Expression Instance;
4182 public FieldExpr (FieldInfo fi)
4185 eclass = ExprClass.Variable;
4186 type = fi.FieldType;
4189 override public Expression DoResolve (TypeContainer tc)
4191 if (!FieldInfo.IsStatic){
4192 if (Instance == null){
4193 throw new Exception ("non-static FieldExpr without instance var\n" +
4194 "You have to assign the Instance variable\n" +
4195 "Of the FieldExpr to set this\n");
4198 Instance = Instance.Resolve (tc);
4199 if (Instance == null)
4206 override public void Emit (EmitContext ec)
4208 ILGenerator ig = ec.ig;
4210 if (FieldInfo.IsStatic)
4211 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4215 ig.Emit (OpCodes.Ldfld, FieldInfo);
4219 public void Store (EmitContext ec)
4221 if (FieldInfo.IsStatic)
4222 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
4224 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
4227 public void AddressOf (EmitContext ec)
4229 if (FieldInfo.IsStatic)
4230 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
4233 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
4239 // Fully resolved expression that evaluates to a Property
4241 public class PropertyExpr : Expression {
4242 public readonly PropertyInfo PropertyInfo;
4243 public readonly bool IsStatic;
4245 public PropertyExpr (PropertyInfo pi)
4248 eclass = ExprClass.PropertyAccess;
4251 MethodBase [] acc = pi.GetAccessors ();
4253 for (int i = 0; i < acc.Length; i++)
4254 if (acc [i].IsStatic)
4257 type = pi.PropertyType;
4260 override public Expression DoResolve (TypeContainer tc)
4262 // We are born in resolved state.
4266 override public void Emit (EmitContext ec)
4268 // FIXME: Implement;
4269 throw new Exception ("Unimplemented");
4274 // Fully resolved expression that evaluates to a Expression
4276 public class EventExpr : Expression {
4277 public readonly EventInfo EventInfo;
4279 public EventExpr (EventInfo ei)
4282 eclass = ExprClass.EventAccess;
4285 override public Expression DoResolve (TypeContainer tc)
4287 // We are born in resolved state.
4291 override public void Emit (EmitContext ec)
4293 throw new Exception ("Implement me");
4294 // FIXME: Implement.
4298 public class CheckedExpr : Expression {
4300 public Expression Expr;
4302 public CheckedExpr (Expression e)
4307 public override Expression DoResolve (TypeContainer tc)
4309 Expr = Expr.Resolve (tc);
4314 eclass = Expr.ExprClass;
4319 public override void Emit (EmitContext ec)
4321 bool last_check = ec.CheckState;
4323 ec.CheckState = true;
4325 ec.CheckState = last_check;
4330 public class UnCheckedExpr : Expression {
4332 public Expression Expr;
4334 public UnCheckedExpr (Expression e)
4339 public override Expression DoResolve (TypeContainer tc)
4341 Expr = Expr.Resolve (tc);
4346 eclass = Expr.ExprClass;
4351 public override void Emit (EmitContext ec)
4353 bool last_check = ec.CheckState;
4355 ec.CheckState = false;
4357 ec.CheckState = last_check;
4362 public class ElementAccess : Expression {
4364 public readonly ArrayList Arguments;
4365 public readonly Expression Expr;
4367 public ElementAccess (Expression e, ArrayList e_list)
4373 public override Expression DoResolve (TypeContainer tc)
4375 // FIXME: Implement;
4376 throw new Exception ("Unimplemented");
4380 public override void Emit (EmitContext ec)
4382 // FIXME : Implement !
4383 throw new Exception ("Unimplemented");
4388 public class BaseAccess : Expression {
4390 public enum BaseAccessType {
4395 public readonly BaseAccessType BAType;
4396 public readonly string Member;
4397 public readonly ArrayList Arguments;
4399 public BaseAccess (BaseAccessType t, string member, ArrayList args)
4407 public override Expression DoResolve (TypeContainer tc)
4409 // FIXME: Implement;
4410 throw new Exception ("Unimplemented");
4414 public override void Emit (EmitContext ec)
4416 throw new Exception ("Unimplemented");
4421 // This class exists solely to pass the Type around and to be a dummy
4422 // that can be passed to the conversion functions (this is used by
4423 // foreach implementation to typecast the object return value from
4424 // get_Current into the proper type. All code has been generated and
4425 // we only care about the side effect conversions to be performed
4428 public class EmptyExpression : Expression {
4429 public EmptyExpression ()
4431 type = TypeManager.object_type;
4432 eclass = ExprClass.Value;
4435 public override Expression DoResolve (TypeContainer tc)
4440 public override void Emit (EmitContext ec)
4442 // nothing, as we only exist to not do anything.
4446 public class UserCast : Expression {
4450 public UserCast (MethodInfo method, Expression source)
4452 this.method = method;
4453 this.source = source;
4454 type = method.ReturnType;
4455 eclass = ExprClass.Value;
4458 public override Expression DoResolve (TypeContainer tc)
4461 // We are born fully resolved
4466 public override void Emit (EmitContext ec)
4468 ILGenerator ig = ec.ig;
4472 if (method is MethodInfo)
4473 ig.Emit (OpCodes.Call, (MethodInfo) method);
4475 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4478 // FIXME : Need to emit the right Opcode for conversion back to the
4479 // type expected by the actual expression. At this point, the type
4480 // of the value on the stack is obviously what the method returns