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 Console.WriteLine ("{0} -> {1}", expr_type, target_type);
407 throw new Exception ("Implement array conversion");
411 // from an array-type to System.Array
412 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
413 return new EmptyCast (expr, target_type);
415 // from any delegate type to System.Delegate
416 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
417 target_type == TypeManager.delegate_type)
418 if (target_type.IsAssignableFrom (expr_type))
419 return new EmptyCast (expr, target_type);
421 // from any array-type or delegate type into System.ICloneable.
422 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
423 if (target_type == TypeManager.icloneable_type)
424 throw new Exception ("Implement conversion to System.ICloneable");
426 // from the null type to any reference-type.
427 // FIXME : How do we do this ?
437 // Handles expressions like this: decimal d; d = 1;
438 // and changes them into: decimal d; d = new System.Decimal (1);
440 static Expression InternalTypeConstructor (TypeContainer tc, Expression expr, Type target)
442 ArrayList args = new ArrayList ();
444 args.Add (new Argument (expr, Argument.AType.Expression));
446 Expression ne = new New (target.FullName, args,
449 return ne.Resolve (tc);
453 // Implicit Numeric Conversions.
455 // expr is the expression to convert, returns a new expression of type
456 // target_type or null if an implicit conversion is not possible.
459 static public Expression ImplicitNumericConversion (TypeContainer tc, Expression expr,
460 Type target_type, Location l)
462 Type expr_type = expr.Type;
465 // Attempt to do the implicit constant expression conversions
467 if (expr is IntLiteral){
470 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
473 } else if (expr is LongLiteral){
475 // Try the implicit constant expression conversion
476 // from long to ulong, instead of a nice routine,
479 if (((LongLiteral) expr).Value > 0)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
483 if (expr_type == TypeManager.sbyte_type){
485 // From sbyte to short, int, long, float, double.
487 if (target_type == TypeManager.int32_type)
488 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
489 if (target_type == TypeManager.int64_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
491 if (target_type == TypeManager.double_type)
492 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
493 if (target_type == TypeManager.float_type)
494 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
495 if (target_type == TypeManager.short_type)
496 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
497 if (target_type == TypeManager.decimal_type)
498 return InternalTypeConstructor (tc, expr, target_type);
499 } else if (expr_type == TypeManager.byte_type){
501 // From byte to short, ushort, int, uint, long, ulong, float, double
503 if ((target_type == TypeManager.short_type) ||
504 (target_type == TypeManager.ushort_type) ||
505 (target_type == TypeManager.int32_type) ||
506 (target_type == TypeManager.uint32_type))
507 return new EmptyCast (expr, target_type);
509 if (target_type == TypeManager.uint64_type)
510 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
511 if (target_type == TypeManager.int64_type)
512 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
514 if (target_type == TypeManager.float_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
516 if (target_type == TypeManager.double_type)
517 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
518 if (target_type == TypeManager.decimal_type)
519 return InternalTypeConstructor (tc, expr, target_type);
520 } else if (expr_type == TypeManager.short_type){
522 // From short to int, long, float, double
524 if (target_type == TypeManager.int32_type)
525 return new EmptyCast (expr, target_type);
526 if (target_type == TypeManager.int64_type)
527 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
528 if (target_type == TypeManager.double_type)
529 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
530 if (target_type == TypeManager.float_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
532 if (target_type == TypeManager.decimal_type)
533 return InternalTypeConstructor (tc, expr, target_type);
534 } else if (expr_type == TypeManager.ushort_type){
536 // From ushort to int, uint, long, ulong, float, double
538 if (target_type == TypeManager.uint32_type)
539 return new EmptyCast (expr, target_type);
541 if (target_type == TypeManager.uint64_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
543 if (target_type == TypeManager.int32_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
545 if (target_type == TypeManager.int64_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
547 if (target_type == TypeManager.double_type)
548 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
549 if (target_type == TypeManager.float_type)
550 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
551 if (target_type == TypeManager.decimal_type)
552 return InternalTypeConstructor (tc, expr, target_type);
553 } else if (expr_type == TypeManager.int32_type){
555 // From int to long, float, double
557 if (target_type == TypeManager.int64_type)
558 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
559 if (target_type == TypeManager.double_type)
560 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
561 if (target_type == TypeManager.float_type)
562 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
563 if (target_type == TypeManager.decimal_type)
564 return InternalTypeConstructor (tc, expr, target_type);
565 } else if (expr_type == TypeManager.uint32_type){
567 // From uint to long, ulong, float, double
569 if (target_type == TypeManager.int64_type)
570 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
571 if (target_type == TypeManager.uint64_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
573 if (target_type == TypeManager.double_type)
574 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
576 if (target_type == TypeManager.float_type)
577 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
579 if (target_type == TypeManager.decimal_type)
580 return InternalTypeConstructor (tc, expr, target_type);
581 } else if ((expr_type == TypeManager.uint64_type) ||
582 (expr_type == TypeManager.int64_type)){
584 // From long/ulong to float, double
586 if (target_type == TypeManager.double_type)
587 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
589 if (target_type == TypeManager.float_type)
590 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
592 if (target_type == TypeManager.decimal_type)
593 return InternalTypeConstructor (tc, expr, target_type);
594 } else if (expr_type == TypeManager.char_type){
596 // From char to ushort, int, uint, long, ulong, float, double
598 if ((target_type == TypeManager.ushort_type) ||
599 (target_type == TypeManager.int32_type) ||
600 (target_type == TypeManager.uint32_type))
601 return new EmptyCast (expr, target_type);
602 if (target_type == TypeManager.uint64_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
604 if (target_type == TypeManager.int64_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
606 if (target_type == TypeManager.float_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
608 if (target_type == TypeManager.double_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
610 if (target_type == TypeManager.decimal_type)
611 return InternalTypeConstructor (tc, expr, target_type);
612 } else if (expr_type == TypeManager.float_type){
616 if (target_type == TypeManager.double_type)
617 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
624 // Determines if a standard implicit conversion exists from
625 // expr_type to target_type
627 public static bool StandardConversionExists (Type expr_type, Type target_type)
629 if (expr_type == target_type)
632 // First numeric conversions
634 if (expr_type == TypeManager.sbyte_type){
636 // From sbyte to short, int, long, float, double.
638 if ((target_type == TypeManager.int32_type) ||
639 (target_type == TypeManager.int64_type) ||
640 (target_type == TypeManager.double_type) ||
641 (target_type == TypeManager.float_type) ||
642 (target_type == TypeManager.short_type) ||
643 (target_type == TypeManager.decimal_type))
646 } else if (expr_type == TypeManager.byte_type){
648 // From byte to short, ushort, int, uint, long, ulong, float, double
650 if ((target_type == TypeManager.short_type) ||
651 (target_type == TypeManager.ushort_type) ||
652 (target_type == TypeManager.int32_type) ||
653 (target_type == TypeManager.uint32_type) ||
654 (target_type == TypeManager.uint64_type) ||
655 (target_type == TypeManager.int64_type) ||
656 (target_type == TypeManager.float_type) ||
657 (target_type == TypeManager.double_type) ||
658 (target_type == TypeManager.decimal_type))
661 } else if (expr_type == TypeManager.short_type){
663 // From short to int, long, float, double
665 if ((target_type == TypeManager.int32_type) ||
666 (target_type == TypeManager.int64_type) ||
667 (target_type == TypeManager.double_type) ||
668 (target_type == TypeManager.float_type) ||
669 (target_type == TypeManager.decimal_type))
672 } else if (expr_type == TypeManager.ushort_type){
674 // From ushort to int, uint, long, ulong, float, double
676 if ((target_type == TypeManager.uint32_type) ||
677 (target_type == TypeManager.uint64_type) ||
678 (target_type == TypeManager.int32_type) ||
679 (target_type == TypeManager.int64_type) ||
680 (target_type == TypeManager.double_type) ||
681 (target_type == TypeManager.float_type) ||
682 (target_type == TypeManager.decimal_type))
685 } else if (expr_type == TypeManager.int32_type){
687 // From int to long, float, double
689 if ((target_type == TypeManager.int64_type) ||
690 (target_type == TypeManager.double_type) ||
691 (target_type == TypeManager.float_type) ||
692 (target_type == TypeManager.decimal_type))
695 } else if (expr_type == TypeManager.uint32_type){
697 // From uint to long, ulong, float, double
699 if ((target_type == TypeManager.int64_type) ||
700 (target_type == TypeManager.uint64_type) ||
701 (target_type == TypeManager.double_type) ||
702 (target_type == TypeManager.float_type) ||
703 (target_type == TypeManager.decimal_type))
706 } else if ((expr_type == TypeManager.uint64_type) ||
707 (expr_type == TypeManager.int64_type)) {
709 // From long/ulong to float, double
711 if ((target_type == TypeManager.double_type) ||
712 (target_type == TypeManager.float_type) ||
713 (target_type == TypeManager.decimal_type))
716 } else if (expr_type == TypeManager.char_type){
718 // From char to ushort, int, uint, long, ulong, float, double
720 if ((target_type == TypeManager.ushort_type) ||
721 (target_type == TypeManager.int32_type) ||
722 (target_type == TypeManager.uint32_type) ||
723 (target_type == TypeManager.uint64_type) ||
724 (target_type == TypeManager.int64_type) ||
725 (target_type == TypeManager.float_type) ||
726 (target_type == TypeManager.double_type) ||
727 (target_type == TypeManager.decimal_type))
730 } else if (expr_type == TypeManager.float_type){
734 if (target_type == TypeManager.double_type)
738 // Next reference conversions
740 if (target_type == TypeManager.object_type) {
741 if ((expr_type.IsClass) ||
742 (expr_type.IsValueType))
745 } else if (expr_type.IsSubclassOf (target_type)) {
749 // from any class-type S to any interface-type T.
750 if (expr_type.IsClass && target_type.IsInterface)
753 // from any interface type S to interface-type T.
754 // FIXME : Is it right to use IsAssignableFrom ?
755 if (expr_type.IsInterface && target_type.IsInterface)
756 if (target_type.IsAssignableFrom (expr_type))
759 // from an array-type S to an array-type of type T
760 if (expr_type.IsArray && target_type.IsArray)
763 // from an array-type to System.Array
764 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
767 // from any delegate type to System.Delegate
768 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
769 target_type == TypeManager.delegate_type)
770 if (target_type.IsAssignableFrom (expr_type))
773 // from any array-type or delegate type into System.ICloneable.
774 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
775 if (target_type == TypeManager.icloneable_type)
778 // from the null type to any reference-type.
779 // FIXME : How do we do this ?
787 // Finds "most encompassed type" according to the spec (13.4.2)
788 // amongst the methods in the MethodGroupExpr which convert from a
789 // type encompassing source_type
791 static Type FindMostEncompassedType (TypeContainer tc, MethodGroupExpr me, Type source_type)
795 for (int i = me.Methods.Length; i > 0; ) {
798 MethodBase mb = me.Methods [i];
799 ParameterData pd = Invocation.GetParameterData (mb);
800 Type param_type = pd.ParameterType (0);
802 if (StandardConversionExists (source_type, param_type)) {
806 if (StandardConversionExists (param_type, best))
815 // Finds "most encompassing type" according to the spec (13.4.2)
816 // amongst the methods in the MethodGroupExpr which convert to a
817 // type encompassed by target_type
819 static Type FindMostEncompassingType (TypeContainer tc, MethodGroupExpr me, Type target)
823 for (int i = me.Methods.Length; i > 0; ) {
826 MethodInfo mi = (MethodInfo) me.Methods [i];
827 Type ret_type = mi.ReturnType;
829 if (StandardConversionExists (ret_type, target)) {
833 if (!StandardConversionExists (ret_type, best))
845 // User-defined Implicit conversions
847 static public Expression ImplicitUserConversion (TypeContainer tc, Expression source, Type target,
850 return UserDefinedConversion (tc, source, target, l, false);
854 // User-defined Explicit conversions
856 static public Expression ExplicitUserConversion (TypeContainer tc, Expression source, Type target,
859 return UserDefinedConversion (tc, source, target, l, true);
863 // User-defined conversions
865 static public Expression UserDefinedConversion (TypeContainer tc, Expression source,
866 Type target, Location l,
867 bool look_for_explicit)
869 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
870 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
872 MethodBase method = null;
873 Type source_type = source.Type;
877 // If we have a boolean type, we need to check for the True operator
879 // FIXME : How does the False operator come into the picture ?
880 // FIXME : This doesn't look complete and very correct !
881 if (target == TypeManager.bool_type)
884 op_name = "op_Implicit";
886 mg1 = MemberLookup (tc, source_type, op_name, false);
888 if (source_type.BaseType != null)
889 mg2 = MemberLookup (tc, source_type.BaseType, op_name, false);
891 mg3 = MemberLookup (tc, target, op_name, false);
893 if (target.BaseType != null)
894 mg4 = MemberLookup (tc, target.BaseType, op_name, false);
896 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
897 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
899 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
901 MethodGroupExpr union4 = null;
903 if (look_for_explicit) {
905 op_name = "op_Explicit";
907 mg5 = MemberLookup (tc, source_type, op_name, false);
909 if (source_type.BaseType != null)
910 mg6 = MemberLookup (tc, source_type.BaseType, op_name, false);
912 mg7 = MemberLookup (tc, target, op_name, false);
914 if (target.BaseType != null)
915 mg8 = MemberLookup (tc, target.BaseType, op_name, false);
917 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
918 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
920 union4 = Invocation.MakeUnionSet (union5, union6);
923 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
927 Type most_specific_source, most_specific_target;
929 most_specific_source = FindMostEncompassedType (tc, union, source_type);
930 if (most_specific_source == null)
933 most_specific_target = FindMostEncompassingType (tc, union, target);
934 if (most_specific_target == null)
939 for (int i = union.Methods.Length; i > 0;) {
942 MethodBase mb = union.Methods [i];
943 ParameterData pd = Invocation.GetParameterData (mb);
944 MethodInfo mi = (MethodInfo) union.Methods [i];
946 if (pd.ParameterType (0) == most_specific_source &&
947 mi.ReturnType == most_specific_target) {
953 if (method == null || count > 1) {
954 Report.Error (-11, l, "Ambiguous user defined conversion");
959 // This will do the conversion to the best match that we
960 // found. Now we need to perform an implict standard conversion
961 // if the best match was not the type that we were requested
964 if (look_for_explicit)
965 source = ConvertExplicit (tc, source, most_specific_source, l);
967 source = ConvertImplicitStandard (tc, source, most_specific_source,
973 e = new UserCast ((MethodInfo) method, source);
975 if (e.Type != target){
976 if (!look_for_explicit)
977 e = ConvertImplicitStandard (tc, e, target, l);
979 e = ConvertExplicitStandard (tc, e, target, l);
990 // Converts implicitly the resolved expression `expr' into the
991 // `target_type'. It returns a new expression that can be used
992 // in a context that expects a `target_type'.
994 static public Expression ConvertImplicit (TypeContainer tc, Expression expr,
995 Type target_type, Location l)
997 Type expr_type = expr.Type;
1000 if (expr_type == target_type)
1003 e = ImplicitNumericConversion (tc, expr, target_type, l);
1007 e = ImplicitReferenceConversion (expr, target_type);
1011 e = ImplicitUserConversion (tc, expr, target_type, l);
1015 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1016 IntLiteral i = (IntLiteral) expr;
1019 return new EmptyCast (expr, target_type);
1027 // Attempts to apply the `Standard Implicit
1028 // Conversion' rules to the expression `expr' into
1029 // the `target_type'. It returns a new expression
1030 // that can be used in a context that expects a
1033 // This is different from `ConvertImplicit' in that the
1034 // user defined implicit conversions are excluded.
1036 static public Expression ConvertImplicitStandard (TypeContainer tc, Expression expr,
1037 Type target_type, Location l)
1039 Type expr_type = expr.Type;
1042 if (expr_type == target_type)
1045 e = ImplicitNumericConversion (tc, expr, target_type, l);
1049 e = ImplicitReferenceConversion (expr, target_type);
1053 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1054 IntLiteral i = (IntLiteral) expr;
1057 return new EmptyCast (expr, target_type);
1062 // Attemps to perform an implict constant conversion of the IntLiteral
1063 // into a different data type using casts (See Implicit Constant
1064 // Expression Conversions)
1066 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1068 int value = il.Value;
1070 if (target_type == TypeManager.sbyte_type){
1071 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1073 } else if (target_type == TypeManager.byte_type){
1074 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1076 } else if (target_type == TypeManager.short_type){
1077 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1079 } else if (target_type == TypeManager.ushort_type){
1080 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1082 } else if (target_type == TypeManager.uint32_type){
1084 // we can optimize this case: a positive int32
1085 // always fits on a uint32
1089 } else if (target_type == TypeManager.uint64_type){
1091 // we can optimize this case: a positive int32
1092 // always fits on a uint64. But we need an opcode
1096 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1103 // Attemptes to implicityly convert `target' into `type', using
1104 // ConvertImplicit. If there is no implicit conversion, then
1105 // an error is signaled
1107 static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
1108 Type type, Location l)
1112 e = ConvertImplicit (tc, target, type, l);
1116 string msg = "Can not convert implicitly from `"+
1117 TypeManager.CSharpName (target.Type) + "' to `" +
1118 TypeManager.CSharpName (type) + "'";
1120 Error (tc, 29, l, msg);
1126 // Performs the explicit numeric conversions
1128 static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
1131 Type expr_type = expr.Type;
1133 if (expr_type == TypeManager.sbyte_type){
1135 // From sbyte to byte, ushort, uint, ulong, char
1137 if (target_type == TypeManager.byte_type)
1138 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1139 if (target_type == TypeManager.ushort_type)
1140 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1141 if (target_type == TypeManager.uint32_type)
1142 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1143 if (target_type == TypeManager.uint64_type)
1144 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1145 if (target_type == TypeManager.char_type)
1146 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1147 } else if (expr_type == TypeManager.byte_type){
1149 // From byte to sbyte and char
1151 if (target_type == TypeManager.sbyte_type)
1152 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1153 if (target_type == TypeManager.char_type)
1154 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1155 } else if (expr_type == TypeManager.short_type){
1157 // From short to sbyte, byte, ushort, uint, ulong, char
1159 if (target_type == TypeManager.sbyte_type)
1160 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1161 if (target_type == TypeManager.byte_type)
1162 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1163 if (target_type == TypeManager.ushort_type)
1164 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1165 if (target_type == TypeManager.uint32_type)
1166 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1167 if (target_type == TypeManager.uint64_type)
1168 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1169 if (target_type == TypeManager.char_type)
1170 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1171 } else if (expr_type == TypeManager.ushort_type){
1173 // From ushort to sbyte, byte, short, char
1175 if (target_type == TypeManager.sbyte_type)
1176 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1177 if (target_type == TypeManager.byte_type)
1178 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1179 if (target_type == TypeManager.short_type)
1180 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1181 if (target_type == TypeManager.char_type)
1182 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1183 } else if (expr_type == TypeManager.int32_type){
1185 // From int to sbyte, byte, short, ushort, uint, ulong, char
1187 if (target_type == TypeManager.sbyte_type)
1188 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1189 if (target_type == TypeManager.byte_type)
1190 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1191 if (target_type == TypeManager.short_type)
1192 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1193 if (target_type == TypeManager.ushort_type)
1194 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1195 if (target_type == TypeManager.uint32_type)
1196 return new EmptyCast (expr, target_type);
1197 if (target_type == TypeManager.uint64_type)
1198 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1199 if (target_type == TypeManager.char_type)
1200 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1201 } else if (expr_type == TypeManager.uint32_type){
1203 // From uint to sbyte, byte, short, ushort, int, char
1205 if (target_type == TypeManager.sbyte_type)
1206 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1207 if (target_type == TypeManager.byte_type)
1208 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1209 if (target_type == TypeManager.short_type)
1210 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1211 if (target_type == TypeManager.ushort_type)
1212 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1213 if (target_type == TypeManager.int32_type)
1214 return new EmptyCast (expr, target_type);
1215 if (target_type == TypeManager.char_type)
1216 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1217 } else if (expr_type == TypeManager.int64_type){
1219 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1221 if (target_type == TypeManager.sbyte_type)
1222 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1223 if (target_type == TypeManager.byte_type)
1224 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1225 if (target_type == TypeManager.short_type)
1226 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1227 if (target_type == TypeManager.ushort_type)
1228 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1229 if (target_type == TypeManager.int32_type)
1230 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1231 if (target_type == TypeManager.uint32_type)
1232 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1233 if (target_type == TypeManager.uint64_type)
1234 return new EmptyCast (expr, target_type);
1235 if (target_type == TypeManager.char_type)
1236 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1237 } else if (expr_type == TypeManager.uint64_type){
1239 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1241 if (target_type == TypeManager.sbyte_type)
1242 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1243 if (target_type == TypeManager.byte_type)
1244 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1245 if (target_type == TypeManager.short_type)
1246 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1247 if (target_type == TypeManager.ushort_type)
1248 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1249 if (target_type == TypeManager.int32_type)
1250 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1251 if (target_type == TypeManager.uint32_type)
1252 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1253 if (target_type == TypeManager.int64_type)
1254 return new EmptyCast (expr, target_type);
1255 if (target_type == TypeManager.char_type)
1256 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1257 } else if (expr_type == TypeManager.char_type){
1259 // From char to sbyte, byte, short
1261 if (target_type == TypeManager.sbyte_type)
1262 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1263 if (target_type == TypeManager.byte_type)
1264 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1265 if (target_type == TypeManager.short_type)
1266 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1267 } else if (expr_type == TypeManager.float_type){
1269 // From float to sbyte, byte, short,
1270 // ushort, int, uint, long, ulong, char
1273 if (target_type == TypeManager.sbyte_type)
1274 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1275 if (target_type == TypeManager.byte_type)
1276 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1277 if (target_type == TypeManager.short_type)
1278 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1279 if (target_type == TypeManager.ushort_type)
1280 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1281 if (target_type == TypeManager.int32_type)
1282 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1283 if (target_type == TypeManager.uint32_type)
1284 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1285 if (target_type == TypeManager.int64_type)
1286 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1287 if (target_type == TypeManager.uint64_type)
1288 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1289 if (target_type == TypeManager.char_type)
1290 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1291 if (target_type == TypeManager.decimal_type)
1292 return InternalTypeConstructor (tc, expr, target_type);
1293 } else if (expr_type == TypeManager.double_type){
1295 // From double to byte, byte, short,
1296 // ushort, int, uint, long, ulong,
1297 // char, float or decimal
1299 if (target_type == TypeManager.sbyte_type)
1300 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1301 if (target_type == TypeManager.byte_type)
1302 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1303 if (target_type == TypeManager.short_type)
1304 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1305 if (target_type == TypeManager.ushort_type)
1306 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1307 if (target_type == TypeManager.int32_type)
1308 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1309 if (target_type == TypeManager.uint32_type)
1310 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1311 if (target_type == TypeManager.int64_type)
1312 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1313 if (target_type == TypeManager.uint64_type)
1314 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1315 if (target_type == TypeManager.char_type)
1316 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1317 if (target_type == TypeManager.float_type)
1318 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1319 if (target_type == TypeManager.decimal_type)
1320 return InternalTypeConstructor (tc, expr, target_type);
1323 // decimal is taken care of by the op_Explicit methods.
1329 // Implements Explicit Reference conversions
1331 static Expression ConvertReferenceExplicit (TypeContainer tc, Expression expr,
1334 Type expr_type = expr.Type;
1335 bool target_is_value_type = target_type.IsValueType;
1338 // From object to any reference type
1340 if (expr_type == TypeManager.object_type && !target_is_value_type)
1341 return new ClassCast (expr, target_type);
1347 // Performs an explicit conversion of the expression `expr' whose
1348 // type is expr.Type to `target_type'.
1350 static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
1351 Type target_type, Location l)
1353 Expression ne = ConvertImplicitStandard (tc, expr, target_type, l);
1358 ne = ConvertNumericExplicit (tc, expr, target_type);
1362 ne = ConvertReferenceExplicit (tc, expr, target_type);
1366 ne = ExplicitUserConversion (tc, expr, target_type, l);
1370 Report.Error (30, l, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1371 + TypeManager.CSharpName (target_type) + "'");
1376 // Same as ConverExplicit, only it doesn't include user defined conversions
1378 static public Expression ConvertExplicitStandard (TypeContainer tc, Expression expr,
1379 Type target_type, Location l)
1381 Expression ne = ConvertImplicitStandard (tc, expr, target_type, l);
1386 ne = ConvertNumericExplicit (tc, expr, target_type);
1390 ne = ConvertReferenceExplicit (tc, expr, target_type);
1394 Report.Error (30, l, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1395 + TypeManager.CSharpName (target_type) + "'");
1399 static string ExprClassName (ExprClass c)
1402 case ExprClass.Invalid:
1404 case ExprClass.Value:
1406 case ExprClass.Variable:
1408 case ExprClass.Namespace:
1410 case ExprClass.Type:
1412 case ExprClass.MethodGroup:
1413 return "method group";
1414 case ExprClass.PropertyAccess:
1415 return "property access";
1416 case ExprClass.EventAccess:
1417 return "event access";
1418 case ExprClass.IndexerAccess:
1419 return "indexer access";
1420 case ExprClass.Nothing:
1423 throw new Exception ("Should not happen");
1427 // Reports that we were expecting `expr' to be of class `expected'
1429 protected void report118 (TypeContainer tc, Location l, Expression expr, string expected)
1431 string kind = "Unknown";
1434 kind = ExprClassName (expr.ExprClass);
1436 Error (tc, 118, l, "Expression denotes a '" + kind +
1437 "' where an " + expected + " was expected");
1442 // This is just a base class for expressions that can
1443 // appear on statements (invocations, object creation,
1444 // assignments, post/pre increment and decrement). The idea
1445 // being that they would support an extra Emition interface that
1446 // does not leave a result on the stack.
1449 public abstract class ExpressionStatement : Expression {
1452 // Requests the expression to be emitted in a `statement'
1453 // context. This means that no new value is left on the
1454 // stack after invoking this method (constrasted with
1455 // Emit that will always leave a value on the stack).
1457 public abstract void EmitStatement (EmitContext ec);
1461 // This kind of cast is used to encapsulate the child
1462 // whose type is child.Type into an expression that is
1463 // reported to return "return_type". This is used to encapsulate
1464 // expressions which have compatible types, but need to be dealt
1465 // at higher levels with.
1467 // For example, a "byte" expression could be encapsulated in one
1468 // of these as an "unsigned int". The type for the expression
1469 // would be "unsigned int".
1473 public class EmptyCast : Expression {
1474 protected Expression child;
1476 public EmptyCast (Expression child, Type return_type)
1478 ExprClass = child.ExprClass;
1483 public override Expression DoResolve (TypeContainer tc)
1485 // This should never be invoked, we are born in fully
1486 // initialized state.
1491 public override void Emit (EmitContext ec)
1498 // This kind of cast is used to encapsulate Value Types in objects.
1500 // The effect of it is to box the value type emitted by the previous
1503 public class BoxedCast : EmptyCast {
1505 public BoxedCast (Expression expr)
1506 : base (expr, TypeManager.object_type)
1510 public override Expression DoResolve (TypeContainer tc)
1512 // This should never be invoked, we are born in fully
1513 // initialized state.
1518 public override void Emit (EmitContext ec)
1521 ec.ig.Emit (OpCodes.Box, child.Type);
1526 // This kind of cast is used to encapsulate a child expression
1527 // that can be trivially converted to a target type using one or
1528 // two opcodes. The opcodes are passed as arguments.
1530 public class OpcodeCast : EmptyCast {
1534 public OpcodeCast (Expression child, Type return_type, OpCode op)
1535 : base (child, return_type)
1539 second_valid = false;
1542 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1543 : base (child, return_type)
1548 second_valid = true;
1551 public override Expression DoResolve (TypeContainer tc)
1553 // This should never be invoked, we are born in fully
1554 // initialized state.
1559 public override void Emit (EmitContext ec)
1571 // This kind of cast is used to encapsulate a child and cast it
1572 // to the class requested
1574 public class ClassCast : EmptyCast {
1575 public ClassCast (Expression child, Type return_type)
1576 : base (child, return_type)
1581 public override Expression DoResolve (TypeContainer tc)
1583 // This should never be invoked, we are born in fully
1584 // initialized state.
1589 public override void Emit (EmitContext ec)
1593 ec.ig.Emit (OpCodes.Castclass, type);
1599 // Unary expressions.
1603 // Unary implements unary expressions. It derives from
1604 // ExpressionStatement becuase the pre/post increment/decrement
1605 // operators can be used in a statement context.
1607 public class Unary : ExpressionStatement {
1608 public enum Operator {
1609 Addition, Subtraction, Negate, BitComplement,
1610 Indirection, AddressOf, PreIncrement,
1611 PreDecrement, PostIncrement, PostDecrement
1616 ArrayList Arguments;
1620 public Unary (Operator op, Expression expr, Location loc)
1624 this.location = loc;
1627 public Expression Expr {
1637 public Operator Oper {
1648 // Returns a stringified representation of the Operator
1653 case Operator.Addition:
1655 case Operator.Subtraction:
1657 case Operator.Negate:
1659 case Operator.BitComplement:
1661 case Operator.AddressOf:
1663 case Operator.Indirection:
1665 case Operator.PreIncrement : case Operator.PostIncrement :
1667 case Operator.PreDecrement : case Operator.PostDecrement :
1671 return oper.ToString ();
1674 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1676 if (expr.Type == target_type)
1679 return ConvertImplicit (tc, expr, target_type, new Location (-1));
1682 void error23 (TypeContainer tc, Type t)
1684 Report.Error (23, location, "Operator " + OperName () +
1685 " cannot be applied to operand of type `" +
1686 TypeManager.CSharpName (t) + "'");
1690 // Returns whether an object of type `t' can be incremented
1691 // or decremented with add/sub (ie, basically whether we can
1692 // use pre-post incr-decr operations on it, but it is not a
1693 // System.Decimal, which we test elsewhere)
1695 static bool IsIncrementableNumber (Type t)
1697 return (t == TypeManager.sbyte_type) ||
1698 (t == TypeManager.byte_type) ||
1699 (t == TypeManager.short_type) ||
1700 (t == TypeManager.ushort_type) ||
1701 (t == TypeManager.int32_type) ||
1702 (t == TypeManager.uint32_type) ||
1703 (t == TypeManager.int64_type) ||
1704 (t == TypeManager.uint64_type) ||
1705 (t == TypeManager.char_type) ||
1706 (t.IsSubclassOf (TypeManager.enum_type)) ||
1707 (t == TypeManager.float_type) ||
1708 (t == TypeManager.double_type);
1711 Expression ResolveOperator (TypeContainer tc)
1713 Type expr_type = expr.Type;
1716 // Step 1: Perform Operator Overload location
1721 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1722 op_name = "op_Increment";
1723 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1724 op_name = "op_Decrement";
1726 op_name = "op_" + oper;
1728 mg = MemberLookup (tc, expr_type, op_name, false);
1730 if (mg == null && expr_type.BaseType != null)
1731 mg = MemberLookup (tc, expr_type.BaseType, op_name, false);
1734 Arguments = new ArrayList ();
1735 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1737 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg,
1738 Arguments, location);
1739 if (method != null) {
1740 MethodInfo mi = (MethodInfo) method;
1741 type = mi.ReturnType;
1744 error23 (tc, expr_type);
1751 // Step 2: Default operations on CLI native types.
1754 // Only perform numeric promotions on:
1757 if (expr_type == null)
1760 if (oper == Operator.Negate){
1761 if (expr_type != TypeManager.bool_type) {
1762 error23 (tc, expr.Type);
1766 type = TypeManager.bool_type;
1770 if (oper == Operator.BitComplement) {
1771 if (!((expr_type == TypeManager.int32_type) ||
1772 (expr_type == TypeManager.uint32_type) ||
1773 (expr_type == TypeManager.int64_type) ||
1774 (expr_type == TypeManager.uint64_type) ||
1775 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1776 error23 (tc, expr.Type);
1783 if (oper == Operator.Addition) {
1785 // A plus in front of something is just a no-op, so return the child.
1791 // Deals with -literals
1792 // int operator- (int x)
1793 // long operator- (long x)
1794 // float operator- (float f)
1795 // double operator- (double d)
1796 // decimal operator- (decimal d)
1798 if (oper == Operator.Subtraction){
1800 // Fold a "- Constant" into a negative constant
1803 Expression e = null;
1806 // Is this a constant?
1808 if (expr is IntLiteral)
1809 e = new IntLiteral (-((IntLiteral) expr).Value);
1810 else if (expr is LongLiteral)
1811 e = new LongLiteral (-((LongLiteral) expr).Value);
1812 else if (expr is FloatLiteral)
1813 e = new FloatLiteral (-((FloatLiteral) expr).Value);
1814 else if (expr is DoubleLiteral)
1815 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
1816 else if (expr is DecimalLiteral)
1817 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
1825 // Not a constant we can optimize, perform numeric
1826 // promotions to int, long, double.
1829 // The following is inneficient, because we call
1830 // ConvertImplicit too many times.
1832 // It is also not clear if we should convert to Float
1833 // or Double initially.
1835 Location l = new Location (-1);
1837 if (expr_type == TypeManager.uint32_type){
1839 // FIXME: handle exception to this rule that
1840 // permits the int value -2147483648 (-2^31) to
1841 // bt written as a decimal interger literal
1843 type = TypeManager.int64_type;
1844 expr = ConvertImplicit (tc, expr, type, l);
1848 if (expr_type == TypeManager.uint64_type){
1850 // FIXME: Handle exception of `long value'
1851 // -92233720368547758087 (-2^63) to be written as
1852 // decimal integer literal.
1854 error23 (tc, expr_type);
1858 e = ConvertImplicit (tc, expr, TypeManager.int32_type, l);
1865 e = ConvertImplicit (tc, expr, TypeManager.int64_type, l);
1872 e = ConvertImplicit (tc, expr, TypeManager.double_type, l);
1879 error23 (tc, expr_type);
1884 // The operand of the prefix/postfix increment decrement operators
1885 // should be an expression that is classified as a variable,
1886 // a property access or an indexer access
1888 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1889 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1890 if (expr.ExprClass == ExprClass.Variable){
1891 if (IsIncrementableNumber (expr_type) ||
1892 expr_type == TypeManager.decimal_type){
1896 } else if (expr.ExprClass == ExprClass.IndexerAccess){
1898 // FIXME: Verify that we have both get and set methods
1900 throw new Exception ("Implement me");
1901 } else if (expr.ExprClass == ExprClass.PropertyAccess){
1903 // FIXME: Verify that we have both get and set methods
1905 throw new Exception ("Implement me");
1907 report118 (tc, location, expr,
1908 "variable, indexer or property access");
1912 if (oper == Operator.AddressOf){
1913 if (expr.ExprClass != ExprClass.Variable){
1914 Error (tc, 211, "Cannot take the address of non-variables");
1917 type = Type.GetType (expr.Type.ToString () + "*");
1920 Error (tc, 187, "No such operator '" + OperName () + "' defined for type '" +
1921 TypeManager.CSharpName (expr_type) + "'");
1926 public override Expression DoResolve (TypeContainer tc)
1928 expr = expr.Resolve (tc);
1933 eclass = ExprClass.Value;
1934 return ResolveOperator (tc);
1937 public override void Emit (EmitContext ec)
1939 ILGenerator ig = ec.ig;
1940 Type expr_type = expr.Type;
1942 if (method != null) {
1944 // Note that operators are static anyway
1946 if (Arguments != null)
1947 Invocation.EmitArguments (ec, method, Arguments);
1950 // Post increment/decrement operations need a copy at this
1953 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
1954 ig.Emit (OpCodes.Dup);
1957 ig.Emit (OpCodes.Call, (MethodInfo) method);
1960 // Pre Increment and Decrement operators
1962 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
1963 ig.Emit (OpCodes.Dup);
1967 // Increment and Decrement should store the result
1969 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
1970 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
1971 ((LValue) expr).Store (ec);
1977 case Operator.Addition:
1978 throw new Exception ("This should be caught by Resolve");
1980 case Operator.Subtraction:
1982 ig.Emit (OpCodes.Neg);
1985 case Operator.Negate:
1987 ig.Emit (OpCodes.Ldc_I4_0);
1988 ig.Emit (OpCodes.Ceq);
1991 case Operator.BitComplement:
1993 ig.Emit (OpCodes.Not);
1996 case Operator.AddressOf:
1997 ((LValue)expr).AddressOf (ec);
2000 case Operator.Indirection:
2001 throw new Exception ("Not implemented yet");
2003 case Operator.PreIncrement:
2004 case Operator.PreDecrement:
2005 if (expr.ExprClass == ExprClass.Variable){
2007 // Resolve already verified that it is an "incrementable"
2010 ig.Emit (OpCodes.Ldc_I4_1);
2012 if (oper == Operator.PreDecrement)
2013 ig.Emit (OpCodes.Sub);
2015 ig.Emit (OpCodes.Add);
2016 ig.Emit (OpCodes.Dup);
2017 ((LValue) expr).Store (ec);
2019 throw new Exception ("Handle Indexers and Properties here");
2023 case Operator.PostIncrement:
2024 case Operator.PostDecrement:
2025 if (expr.ExprClass == ExprClass.Variable){
2027 // Resolve already verified that it is an "incrementable"
2030 ig.Emit (OpCodes.Dup);
2031 ig.Emit (OpCodes.Ldc_I4_1);
2033 if (oper == Operator.PostDecrement)
2034 ig.Emit (OpCodes.Sub);
2036 ig.Emit (OpCodes.Add);
2037 ((LValue) expr).Store (ec);
2039 throw new Exception ("Handle Indexers and Properties here");
2044 throw new Exception ("This should not happen: Operator = "
2045 + oper.ToString ());
2050 public override void EmitStatement (EmitContext ec)
2053 // FIXME: we should rewrite this code to generate
2054 // better code for ++ and -- as we know we wont need
2055 // the values on the stack
2058 ec.ig.Emit (OpCodes.Pop);
2062 public class Probe : Expression {
2063 public readonly string ProbeType;
2064 public readonly Operator Oper;
2068 public enum Operator {
2072 public Probe (Operator oper, Expression expr, string probe_type)
2075 ProbeType = probe_type;
2079 public Expression Expr {
2085 public override Expression DoResolve (TypeContainer tc)
2087 probe_type = tc.LookupType (ProbeType, false);
2089 if (probe_type == null)
2092 expr = expr.Resolve (tc);
2094 type = TypeManager.bool_type;
2095 eclass = ExprClass.Value;
2100 public override void Emit (EmitContext ec)
2102 ILGenerator ig = ec.ig;
2106 if (Oper == Operator.Is){
2107 ig.Emit (OpCodes.Isinst, probe_type);
2108 ig.Emit (OpCodes.Ldnull);
2109 ig.Emit (OpCodes.Cgt_Un);
2111 ig.Emit (OpCodes.Isinst, probe_type);
2117 // This represents a typecast in the source language.
2119 // FIXME: Cast expressions have an unusual set of parsing
2120 // rules, we need to figure those out.
2122 public class Cast : Expression {
2127 public Cast (string cast_type, Expression expr, Location loc)
2129 this.target_type = cast_type;
2131 this.location = loc;
2134 public string TargetType {
2140 public Expression Expr {
2149 public override Expression DoResolve (TypeContainer tc)
2151 expr = expr.Resolve (tc);
2155 type = tc.LookupType (target_type, false);
2156 eclass = ExprClass.Value;
2161 expr = ConvertExplicit (tc, expr, type, location);
2166 public override void Emit (EmitContext ec)
2169 // This one will never happen
2171 throw new Exception ("Should not happen");
2175 public class Binary : Expression {
2176 public enum Operator {
2177 Multiply, Division, Modulus,
2178 Addition, Subtraction,
2179 LeftShift, RightShift,
2180 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
2181 Equality, Inequality,
2190 Expression left, right;
2192 ArrayList Arguments;
2196 public Binary (Operator oper, Expression left, Expression right, Location loc)
2201 this.location = loc;
2204 public Operator Oper {
2213 public Expression Left {
2222 public Expression Right {
2233 // Returns a stringified representation of the Operator
2238 case Operator.Multiply:
2240 case Operator.Division:
2242 case Operator.Modulus:
2244 case Operator.Addition:
2246 case Operator.Subtraction:
2248 case Operator.LeftShift:
2250 case Operator.RightShift:
2252 case Operator.LessThan:
2254 case Operator.GreaterThan:
2256 case Operator.LessThanOrEqual:
2258 case Operator.GreaterThanOrEqual:
2260 case Operator.Equality:
2262 case Operator.Inequality:
2264 case Operator.BitwiseAnd:
2266 case Operator.BitwiseOr:
2268 case Operator.ExclusiveOr:
2270 case Operator.LogicalOr:
2272 case Operator.LogicalAnd:
2276 return oper.ToString ();
2279 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
2281 if (expr.Type == target_type)
2284 return ConvertImplicit (tc, expr, target_type, new Location (-1));
2288 // Note that handling the case l == Decimal || r == Decimal
2289 // is taken care of by the Step 1 Operator Overload resolution.
2291 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
2293 if (l == TypeManager.double_type || r == TypeManager.double_type){
2295 // If either operand is of type double, the other operand is
2296 // conveted to type double.
2298 if (r != TypeManager.double_type)
2299 right = ConvertImplicit (tc, right, TypeManager.double_type, location);
2300 if (l != TypeManager.double_type)
2301 left = ConvertImplicit (tc, left, TypeManager.double_type, location);
2303 type = TypeManager.double_type;
2304 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
2306 // if either operand is of type float, th eother operand is
2307 // converd to type float.
2309 if (r != TypeManager.double_type)
2310 right = ConvertImplicit (tc, right, TypeManager.float_type, location);
2311 if (l != TypeManager.double_type)
2312 left = ConvertImplicit (tc, left, TypeManager.float_type, location);
2313 type = TypeManager.float_type;
2314 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
2318 // If either operand is of type ulong, the other operand is
2319 // converted to type ulong. or an error ocurrs if the other
2320 // operand is of type sbyte, short, int or long
2323 if (l == TypeManager.uint64_type){
2324 if (r != TypeManager.uint64_type && right is IntLiteral){
2325 e = TryImplicitIntConversion (l, (IntLiteral) right);
2331 if (left is IntLiteral){
2332 e = TryImplicitIntConversion (r, (IntLiteral) left);
2339 if ((other == TypeManager.sbyte_type) ||
2340 (other == TypeManager.short_type) ||
2341 (other == TypeManager.int32_type) ||
2342 (other == TypeManager.int64_type)){
2343 string oper = OperName ();
2345 Error (tc, 34, location, "Operator `" + OperName ()
2346 + "' is ambiguous on operands of type `"
2347 + TypeManager.CSharpName (l) + "' "
2348 + "and `" + TypeManager.CSharpName (r)
2351 type = TypeManager.uint64_type;
2352 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
2354 // If either operand is of type long, the other operand is converted
2357 if (l != TypeManager.int64_type)
2358 left = ConvertImplicit (tc, left, TypeManager.int64_type, location);
2359 if (r != TypeManager.int64_type)
2360 right = ConvertImplicit (tc, right, TypeManager.int64_type, location);
2362 type = TypeManager.int64_type;
2363 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
2365 // If either operand is of type uint, and the other
2366 // operand is of type sbyte, short or int, othe operands are
2367 // converted to type long.
2371 if (l == TypeManager.uint32_type)
2373 else if (r == TypeManager.uint32_type)
2376 if ((other == TypeManager.sbyte_type) ||
2377 (other == TypeManager.short_type) ||
2378 (other == TypeManager.int32_type)){
2379 left = ForceConversion (tc, left, TypeManager.int64_type);
2380 right = ForceConversion (tc, right, TypeManager.int64_type);
2381 type = TypeManager.int64_type;
2384 // if either operand is of type uint, the other
2385 // operand is converd to type uint
2387 left = ForceConversion (tc, left, TypeManager.uint32_type);
2388 right = ForceConversion (tc, right, TypeManager.uint32_type);
2389 type = TypeManager.uint32_type;
2391 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2392 if (l != TypeManager.decimal_type)
2393 left = ConvertImplicit (tc, left, TypeManager.decimal_type, location);
2394 if (r != TypeManager.decimal_type)
2395 right = ConvertImplicit (tc, right, TypeManager.decimal_type, location);
2397 type = TypeManager.decimal_type;
2399 Expression l_tmp, r_tmp;
2401 l_tmp = ForceConversion (tc, left, TypeManager.int32_type);
2402 if (l_tmp == null) {
2408 r_tmp = ForceConversion (tc, right, TypeManager.int32_type);
2409 if (r_tmp == null) {
2415 type = TypeManager.int32_type;
2419 void error19 (TypeContainer tc)
2421 Error (tc, 19, location,
2422 "Operator " + OperName () + " cannot be applied to operands of type `" +
2423 TypeManager.CSharpName (left.Type) + "' and `" +
2424 TypeManager.CSharpName (right.Type) + "'");
2428 Expression CheckShiftArguments (TypeContainer tc)
2432 Type r = right.Type;
2434 e = ForceConversion (tc, right, TypeManager.int32_type);
2441 Location loc = location;
2443 if (((e = ConvertImplicit (tc, left, TypeManager.int32_type, loc)) != null) ||
2444 ((e = ConvertImplicit (tc, left, TypeManager.uint32_type, loc)) != null) ||
2445 ((e = ConvertImplicit (tc, left, TypeManager.int64_type, loc)) != null) ||
2446 ((e = ConvertImplicit (tc, left, TypeManager.uint64_type, loc)) != null)){
2456 Expression ResolveOperator (TypeContainer tc)
2459 Type r = right.Type;
2462 // Step 1: Perform Operator Overload location
2464 Expression left_expr, right_expr;
2466 string op = "op_" + oper;
2468 left_expr = MemberLookup (tc, l, op, false);
2469 if (left_expr == null && l.BaseType != null)
2470 left_expr = MemberLookup (tc, l.BaseType, op, false);
2472 right_expr = MemberLookup (tc, r, op, false);
2473 if (right_expr == null && r.BaseType != null)
2474 right_expr = MemberLookup (tc, r.BaseType, op, false);
2476 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2478 if (union != null) {
2479 Arguments = new ArrayList ();
2480 Arguments.Add (new Argument (left, Argument.AType.Expression));
2481 Arguments.Add (new Argument (right, Argument.AType.Expression));
2483 method = Invocation.OverloadResolve (tc, union, Arguments, location);
2484 if (method != null) {
2485 MethodInfo mi = (MethodInfo) method;
2486 type = mi.ReturnType;
2495 // Step 2: Default operations on CLI native types.
2498 // Only perform numeric promotions on:
2499 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2501 if (oper == Operator.Addition){
2503 // If any of the arguments is a string, cast to string
2505 if (l == TypeManager.string_type){
2506 if (r == TypeManager.string_type){
2508 method = TypeManager.string_concat_string_string;
2511 method = TypeManager.string_concat_object_object;
2512 right = ConvertImplicit (tc, right,
2513 TypeManager.object_type, location);
2515 type = TypeManager.string_type;
2517 Arguments = new ArrayList ();
2518 Arguments.Add (new Argument (left, Argument.AType.Expression));
2519 Arguments.Add (new Argument (right, Argument.AType.Expression));
2523 } else if (r == TypeManager.string_type){
2525 method = TypeManager.string_concat_object_object;
2526 Arguments = new ArrayList ();
2527 Arguments.Add (new Argument (left, Argument.AType.Expression));
2528 Arguments.Add (new Argument (right, Argument.AType.Expression));
2530 left = ConvertImplicit (tc, left, TypeManager.object_type, location);
2531 type = TypeManager.string_type;
2537 // FIXME: is Delegate operator + (D x, D y) handled?
2541 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2542 return CheckShiftArguments (tc);
2544 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2545 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2548 type = TypeManager.bool_type;
2553 // We are dealing with numbers
2556 DoNumericPromotions (tc, l, r);
2558 if (left == null || right == null)
2562 if (oper == Operator.BitwiseAnd ||
2563 oper == Operator.BitwiseOr ||
2564 oper == Operator.ExclusiveOr){
2565 if (!((l == TypeManager.int32_type) ||
2566 (l == TypeManager.uint32_type) ||
2567 (l == TypeManager.int64_type) ||
2568 (l == TypeManager.uint64_type))){
2575 if (oper == Operator.Equality ||
2576 oper == Operator.Inequality ||
2577 oper == Operator.LessThanOrEqual ||
2578 oper == Operator.LessThan ||
2579 oper == Operator.GreaterThanOrEqual ||
2580 oper == Operator.GreaterThan){
2581 type = TypeManager.bool_type;
2587 public override Expression DoResolve (TypeContainer tc)
2589 left = left.Resolve (tc);
2590 right = right.Resolve (tc);
2592 if (left == null || right == null)
2595 if (left.Type == null)
2596 throw new Exception (
2597 "Resolve returned non null, but did not set the type! (" +
2599 if (right.Type == null)
2600 throw new Exception (
2601 "Resolve returned non null, but did not set the type! (" +
2604 eclass = ExprClass.Value;
2606 return ResolveOperator (tc);
2609 public bool IsBranchable ()
2611 if (oper == Operator.Equality ||
2612 oper == Operator.Inequality ||
2613 oper == Operator.LessThan ||
2614 oper == Operator.GreaterThan ||
2615 oper == Operator.LessThanOrEqual ||
2616 oper == Operator.GreaterThanOrEqual){
2623 // This entry point is used by routines that might want
2624 // to emit a brfalse/brtrue after an expression, and instead
2625 // they could use a more compact notation.
2627 // Typically the code would generate l.emit/r.emit, followed
2628 // by the comparission and then a brtrue/brfalse. The comparissions
2629 // are sometimes inneficient (there are not as complete as the branches
2630 // look for the hacks in Emit using double ceqs).
2632 // So for those cases we provide EmitBranchable that can emit the
2633 // branch with the test
2635 public void EmitBranchable (EmitContext ec, int target)
2638 bool close_target = false;
2644 case Operator.Equality:
2646 opcode = OpCodes.Beq_S;
2648 opcode = OpCodes.Beq;
2651 case Operator.Inequality:
2653 opcode = OpCodes.Bne_Un_S;
2655 opcode = OpCodes.Bne_Un;
2658 case Operator.LessThan:
2660 opcode = OpCodes.Blt_S;
2662 opcode = OpCodes.Blt;
2665 case Operator.GreaterThan:
2667 opcode = OpCodes.Bgt_S;
2669 opcode = OpCodes.Bgt;
2672 case Operator.LessThanOrEqual:
2674 opcode = OpCodes.Ble_S;
2676 opcode = OpCodes.Ble;
2679 case Operator.GreaterThanOrEqual:
2681 opcode = OpCodes.Bge_S;
2683 opcode = OpCodes.Ble;
2687 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2688 + oper.ToString ());
2691 ec.ig.Emit (opcode, target);
2694 public override void Emit (EmitContext ec)
2696 ILGenerator ig = ec.ig;
2698 Type r = right.Type;
2701 if (method != null) {
2703 // Note that operators are static anyway
2705 if (Arguments != null)
2706 Invocation.EmitArguments (ec, method, Arguments);
2708 if (method is MethodInfo)
2709 ig.Emit (OpCodes.Call, (MethodInfo) method);
2711 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2720 case Operator.Multiply:
2722 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2723 opcode = OpCodes.Mul_Ovf;
2724 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2725 opcode = OpCodes.Mul_Ovf_Un;
2727 opcode = OpCodes.Mul;
2729 opcode = OpCodes.Mul;
2733 case Operator.Division:
2734 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2735 opcode = OpCodes.Div_Un;
2737 opcode = OpCodes.Div;
2740 case Operator.Modulus:
2741 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2742 opcode = OpCodes.Rem_Un;
2744 opcode = OpCodes.Rem;
2747 case Operator.Addition:
2749 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2750 opcode = OpCodes.Add_Ovf;
2751 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2752 opcode = OpCodes.Add_Ovf_Un;
2754 opcode = OpCodes.Mul;
2756 opcode = OpCodes.Add;
2759 case Operator.Subtraction:
2761 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2762 opcode = OpCodes.Sub_Ovf;
2763 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2764 opcode = OpCodes.Sub_Ovf_Un;
2766 opcode = OpCodes.Sub;
2768 opcode = OpCodes.Sub;
2771 case Operator.RightShift:
2772 opcode = OpCodes.Shr;
2775 case Operator.LeftShift:
2776 opcode = OpCodes.Shl;
2779 case Operator.Equality:
2780 opcode = OpCodes.Ceq;
2783 case Operator.Inequality:
2784 ec.ig.Emit (OpCodes.Ceq);
2785 ec.ig.Emit (OpCodes.Ldc_I4_0);
2787 opcode = OpCodes.Ceq;
2790 case Operator.LessThan:
2791 opcode = OpCodes.Clt;
2794 case Operator.GreaterThan:
2795 opcode = OpCodes.Cgt;
2798 case Operator.LessThanOrEqual:
2799 ec.ig.Emit (OpCodes.Cgt);
2800 ec.ig.Emit (OpCodes.Ldc_I4_0);
2802 opcode = OpCodes.Ceq;
2805 case Operator.GreaterThanOrEqual:
2806 ec.ig.Emit (OpCodes.Clt);
2807 ec.ig.Emit (OpCodes.Ldc_I4_1);
2809 opcode = OpCodes.Sub;
2812 case Operator.LogicalOr:
2813 case Operator.BitwiseOr:
2814 opcode = OpCodes.Or;
2817 case Operator.LogicalAnd:
2818 case Operator.BitwiseAnd:
2819 opcode = OpCodes.And;
2822 case Operator.ExclusiveOr:
2823 opcode = OpCodes.Xor;
2827 throw new Exception ("This should not happen: Operator = "
2828 + oper.ToString ());
2835 public class Conditional : Expression {
2836 Expression expr, trueExpr, falseExpr;
2839 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
2842 this.trueExpr = trueExpr;
2843 this.falseExpr = falseExpr;
2847 public Expression Expr {
2853 public Expression TrueExpr {
2859 public Expression FalseExpr {
2865 public override Expression DoResolve (TypeContainer tc)
2867 expr = expr.Resolve (tc);
2869 if (expr.Type != TypeManager.bool_type)
2870 expr = Expression.ConvertImplicitRequired (
2871 tc, expr, TypeManager.bool_type, l);
2873 trueExpr = trueExpr.Resolve (tc);
2874 falseExpr = falseExpr.Resolve (tc);
2876 if (expr == null || trueExpr == null || falseExpr == null)
2879 if (trueExpr.Type == falseExpr.Type)
2880 type = trueExpr.Type;
2885 // First, if an implicit conversion exists from trueExpr
2886 // to falseExpr, then the result type is of type falseExpr.Type
2888 conv = ConvertImplicit (tc, trueExpr, falseExpr.Type, l);
2890 type = falseExpr.Type;
2892 } else if ((conv = ConvertImplicit (tc,falseExpr,trueExpr.Type,l)) != null){
2893 type = trueExpr.Type;
2896 Error (tc, 173, l, "The type of the conditional expression can " +
2897 "not be computed because there is no implicit conversion" +
2898 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
2899 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
2904 eclass = ExprClass.Value;
2908 public override void Emit (EmitContext ec)
2910 ILGenerator ig = ec.ig;
2911 Label false_target = ig.DefineLabel ();
2912 Label end_target = ig.DefineLabel ();
2915 ig.Emit (OpCodes.Brfalse, false_target);
2917 ig.Emit (OpCodes.Br, end_target);
2918 ig.MarkLabel (false_target);
2919 falseExpr.Emit (ec);
2920 ig.MarkLabel (end_target);
2924 public class SimpleName : Expression {
2925 public readonly string Name;
2926 public readonly Location Location;
2928 public SimpleName (string name, Location l)
2935 // Checks whether we are trying to access an instance
2936 // property, method or field from a static body.
2938 Expression MemberStaticCheck (Expression e)
2940 if (e is FieldExpr){
2941 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2946 "An object reference is required " +
2947 "for the non-static field `"+Name+"'");
2950 } else if (e is MethodGroupExpr){
2951 // FIXME: Pending reorganization of MemberLookup
2952 // Basically at this point we should have the
2953 // best match already selected for us, and
2954 // we should only have to check a *single*
2955 // Method for its static on/off bit.
2957 } else if (e is PropertyExpr){
2958 if (!((PropertyExpr) e).IsStatic){
2960 "An object reference is required " +
2961 "for the non-static property access `"+
2971 // 7.5.2: Simple Names.
2973 // Local Variables and Parameters are handled at
2974 // parse time, so they never occur as SimpleNames.
2976 Expression ResolveSimpleName (TypeContainer tc)
2980 e = MemberLookup (tc, tc.TypeBuilder, Name, true);
2984 else if (e is FieldExpr){
2985 FieldExpr fe = (FieldExpr) e;
2987 if (!fe.FieldInfo.IsStatic)
2988 fe.Instance = new This ();
2991 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2992 return MemberStaticCheck (e);
2998 // Do step 3 of the Simple Name resolution.
3000 // FIXME: implement me.
3002 Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
3009 // SimpleName needs to handle a multitude of cases:
3011 // simple_names and qualified_identifiers are placed on
3012 // the tree equally.
3014 public override Expression DoResolve (TypeContainer tc)
3016 if (Name.IndexOf (".") != -1)
3017 return ResolveMemberAccess (tc, Name);
3019 return ResolveSimpleName (tc);
3022 public override void Emit (EmitContext ec)
3024 throw new Exception ("SimpleNames should be gone from the tree");
3029 // A simple interface that should be implemeneted by LValues
3031 public interface LValue {
3034 // The Store method should store the contents of the top
3035 // of the stack into the storage that is implemented by
3036 // the particular implementation of LValue
3038 void Store (EmitContext ec);
3041 // The AddressOf method should generate code that loads
3042 // the address of the LValue and leaves it on the stack
3044 void AddressOf (EmitContext ec);
3047 public class LocalVariableReference : Expression, LValue {
3048 public readonly string Name;
3049 public readonly Block Block;
3051 public LocalVariableReference (Block block, string name)
3055 eclass = ExprClass.Variable;
3058 public VariableInfo VariableInfo {
3060 return Block.GetVariableInfo (Name);
3064 public override Expression DoResolve (TypeContainer tc)
3066 VariableInfo vi = Block.GetVariableInfo (Name);
3068 type = vi.VariableType;
3072 public override void Emit (EmitContext ec)
3074 VariableInfo vi = VariableInfo;
3075 ILGenerator ig = ec.ig;
3082 ig.Emit (OpCodes.Ldloc_0);
3086 ig.Emit (OpCodes.Ldloc_1);
3090 ig.Emit (OpCodes.Ldloc_2);
3094 ig.Emit (OpCodes.Ldloc_3);
3099 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3101 ig.Emit (OpCodes.Ldloc, idx);
3106 public static void Store (ILGenerator ig, int idx)
3110 ig.Emit (OpCodes.Stloc_0);
3114 ig.Emit (OpCodes.Stloc_1);
3118 ig.Emit (OpCodes.Stloc_2);
3122 ig.Emit (OpCodes.Stloc_3);
3127 ig.Emit (OpCodes.Stloc_S, (byte) idx);
3129 ig.Emit (OpCodes.Stloc, idx);
3134 public void Store (EmitContext ec)
3136 ILGenerator ig = ec.ig;
3137 VariableInfo vi = VariableInfo;
3141 // Funny seems the above generates optimal code for us, but
3142 // seems to take too long to generate what we need.
3143 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3148 public void AddressOf (EmitContext ec)
3150 VariableInfo vi = VariableInfo;
3157 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3159 ec.ig.Emit (OpCodes.Ldloca, idx);
3163 public class ParameterReference : Expression, LValue {
3164 public readonly Parameters Pars;
3165 public readonly String Name;
3166 public readonly int Idx;
3168 public ParameterReference (Parameters pars, int idx, string name)
3173 eclass = ExprClass.Variable;
3176 public override Expression DoResolve (TypeContainer tc)
3178 Type [] types = Pars.GetParameterInfo (tc);
3185 public override void Emit (EmitContext ec)
3188 ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
3190 ec.ig.Emit (OpCodes.Ldarg, Idx);
3193 public void Store (EmitContext ec)
3196 ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
3198 ec.ig.Emit (OpCodes.Starg, Idx);
3202 public void AddressOf (EmitContext ec)
3205 ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
3207 ec.ig.Emit (OpCodes.Ldarga, Idx);
3212 // Used for arguments to New(), Invocation()
3214 public class Argument {
3221 public readonly AType Type;
3222 public Expression expr;
3224 public Argument (Expression expr, AType type)
3230 public Expression Expr {
3240 public bool Resolve (TypeContainer tc)
3242 expr = expr.Resolve (tc);
3244 return expr != null;
3247 public void Emit (EmitContext ec)
3254 // Invocation of methods or delegates.
3256 public class Invocation : ExpressionStatement {
3257 public readonly ArrayList Arguments;
3258 public readonly Location Location;
3261 MethodBase method = null;
3263 static Hashtable method_parameter_cache;
3265 static Invocation ()
3267 method_parameter_cache = new Hashtable ();
3271 // arguments is an ArrayList, but we do not want to typecast,
3272 // as it might be null.
3274 // FIXME: only allow expr to be a method invocation or a
3275 // delegate invocation (7.5.5)
3277 public Invocation (Expression expr, ArrayList arguments, Location l)
3280 Arguments = arguments;
3284 public Expression Expr {
3291 // Returns the Parameters (a ParameterData interface) for the
3294 public static ParameterData GetParameterData (MethodBase mb)
3296 object pd = method_parameter_cache [mb];
3299 return (ParameterData) pd;
3301 if (mb is MethodBuilder || mb is ConstructorBuilder){
3302 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
3304 InternalParameters ip = mc.ParameterInfo;
3305 method_parameter_cache [mb] = ip;
3307 return (ParameterData) ip;
3309 ParameterInfo [] pi = mb.GetParameters ();
3310 ReflectionParameters rp = new ReflectionParameters (pi);
3311 method_parameter_cache [mb] = rp;
3313 return (ParameterData) rp;
3318 // Tells whether a user defined conversion from Type `from' to
3319 // Type `to' exists.
3321 // FIXME: we could implement a cache here.
3323 static bool ConversionExists (TypeContainer tc, Type from, Type to)
3325 // Locate user-defined implicit operators
3329 mg = MemberLookup (tc, to, "op_Implicit", false);
3332 MethodGroupExpr me = (MethodGroupExpr) mg;
3334 for (int i = me.Methods.Length; i > 0;) {
3336 MethodBase mb = me.Methods [i];
3337 ParameterData pd = GetParameterData (mb);
3339 if (from == pd.ParameterType (0))
3344 mg = MemberLookup (tc, from, "op_Implicit", false);
3347 MethodGroupExpr me = (MethodGroupExpr) mg;
3349 for (int i = me.Methods.Length; i > 0;) {
3351 MethodBase mb = me.Methods [i];
3352 MethodInfo mi = (MethodInfo) mb;
3354 if (mi.ReturnType == to)
3363 // Determines "better conversion" as specified in 7.4.2.3
3364 // Returns : 1 if a->p is better
3365 // 0 if a->q or neither is better
3367 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q, bool use_standard)
3370 Type argument_type = a.Expr.Type;
3371 Expression argument_expr = a.Expr;
3373 if (argument_type == null)
3374 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3379 if (argument_type == p)
3382 if (argument_type == q)
3386 // Now probe whether an implicit constant expression conversion
3389 // An implicit constant expression conversion permits the following
3392 // * A constant-expression of type `int' can be converted to type
3393 // sbyte, byute, short, ushort, uint, ulong provided the value of
3394 // of the expression is withing the range of the destination type.
3396 // * A constant-expression of type long can be converted to type
3397 // ulong, provided the value of the constant expression is not negative
3399 // FIXME: Note that this assumes that constant folding has
3400 // taken place. We dont do constant folding yet.
3403 if (argument_expr is IntLiteral){
3404 IntLiteral ei = (IntLiteral) argument_expr;
3405 int value = ei.Value;
3407 if (p == TypeManager.sbyte_type){
3408 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3410 } else if (p == TypeManager.byte_type){
3411 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3413 } else if (p == TypeManager.short_type){
3414 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3416 } else if (p == TypeManager.ushort_type){
3417 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3419 } else if (p == TypeManager.uint32_type){
3421 // we can optimize this case: a positive int32
3422 // always fits on a uint32
3426 } else if (p == TypeManager.uint64_type){
3428 // we can optimize this case: a positive int32
3429 // always fits on a uint64
3434 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3435 LongLiteral ll = (LongLiteral) argument_expr;
3437 if (p == TypeManager.uint64_type){
3448 tmp = ConvertImplicitStandard (tc, argument_expr, p, Location.Null);
3450 tmp = ConvertImplicit (tc, argument_expr, p, Location.Null);
3459 if (ConversionExists (tc, p, q) == true &&
3460 ConversionExists (tc, q, p) == false)
3463 if (p == TypeManager.sbyte_type)
3464 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3465 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3468 if (p == TypeManager.short_type)
3469 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3470 q == TypeManager.uint64_type)
3473 if (p == TypeManager.int32_type)
3474 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3477 if (p == TypeManager.int64_type)
3478 if (q == TypeManager.uint64_type)
3485 // Determines "Better function" and returns an integer indicating :
3486 // 0 if candidate ain't better
3487 // 1 if candidate is better than the current best match
3489 static int BetterFunction (TypeContainer tc, ArrayList args,
3490 MethodBase candidate, MethodBase best,
3493 ParameterData candidate_pd = GetParameterData (candidate);
3494 ParameterData best_pd;
3500 argument_count = args.Count;
3502 if (candidate_pd.Count == 0 && argument_count == 0)
3506 if (candidate_pd.Count == argument_count) {
3508 for (int j = argument_count; j > 0;) {
3511 Argument a = (Argument) args [j];
3513 x = BetterConversion (
3514 tc, a, candidate_pd.ParameterType (j), null,
3530 best_pd = GetParameterData (best);
3532 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3533 int rating1 = 0, rating2 = 0;
3535 for (int j = argument_count; j > 0;) {
3539 Argument a = (Argument) args [j];
3541 x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
3542 best_pd.ParameterType (j), use_standard);
3543 y = BetterConversion (tc, a, best_pd.ParameterType (j),
3544 candidate_pd.ParameterType (j), use_standard);
3550 if (rating1 > rating2)
3559 public static string FullMethodDesc (MethodBase mb)
3561 StringBuilder sb = new StringBuilder (mb.Name);
3562 ParameterData pd = GetParameterData (mb);
3565 for (int i = pd.Count; i > 0;) {
3567 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
3573 return sb.ToString ();
3576 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3578 MemberInfo [] miset;
3579 MethodGroupExpr union;
3581 if (mg1 != null && mg2 != null) {
3583 MethodGroupExpr left_set = null, right_set = null;
3584 int length1 = 0, length2 = 0;
3586 left_set = (MethodGroupExpr) mg1;
3587 length1 = left_set.Methods.Length;
3589 right_set = (MethodGroupExpr) mg2;
3590 length2 = right_set.Methods.Length;
3592 ArrayList common = new ArrayList ();
3594 for (int i = 0; i < left_set.Methods.Length; i++) {
3595 for (int j = 0; j < right_set.Methods.Length; j++) {
3596 if (left_set.Methods [i] == right_set.Methods [j])
3597 common.Add (left_set.Methods [i]);
3601 miset = new MemberInfo [length1 + length2 - common.Count];
3603 left_set.Methods.CopyTo (miset, 0);
3607 for (int j = 0; j < right_set.Methods.Length; j++)
3608 if (!common.Contains (right_set.Methods [j]))
3609 miset [length1 + k++] = right_set.Methods [j];
3611 union = new MethodGroupExpr (miset);
3615 } else if (mg1 == null && mg2 != null) {
3617 MethodGroupExpr me = (MethodGroupExpr) mg2;
3619 miset = new MemberInfo [me.Methods.Length];
3620 me.Methods.CopyTo (miset, 0);
3622 union = new MethodGroupExpr (miset);
3626 } else if (mg2 == null && mg1 != null) {
3628 MethodGroupExpr me = (MethodGroupExpr) mg1;
3630 miset = new MemberInfo [me.Methods.Length];
3631 me.Methods.CopyTo (miset, 0);
3633 union = new MethodGroupExpr (miset);
3642 // Find the Applicable Function Members (7.4.2.1)
3644 // me: Method Group expression with the members to select.
3645 // it might contain constructors or methods (or anything
3646 // that maps to a method).
3648 // Arguments: ArrayList containing resolved Argument objects.
3650 // loc: The location if we want an error to be reported, or a Null
3651 // location for "probing" purposes.
3653 // inside_user_defined: controls whether OverloadResolve should use the
3654 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
3656 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3657 // that is the best match of me on Arguments.
3660 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3661 ArrayList Arguments, Location loc,
3664 ArrayList afm = new ArrayList ();
3665 int best_match_idx = -1;
3666 MethodBase method = null;
3669 for (int i = me.Methods.Length; i > 0; ){
3671 MethodBase candidate = me.Methods [i];
3674 Console.WriteLine ("Candidate : " + candidate.ToString ());
3675 x = BetterFunction (tc, Arguments, candidate, method, use_standard);
3681 method = me.Methods [best_match_idx];
3685 if (Arguments == null)
3688 argument_count = Arguments.Count;
3692 // Now we see if we can at least find a method with the same number of arguments
3693 // and then try doing implicit conversion on the arguments
3694 if (best_match_idx == -1) {
3696 for (int i = me.Methods.Length; i > 0;) {
3698 MethodBase mb = me.Methods [i];
3699 pd = GetParameterData (mb);
3701 if (pd.Count == argument_count) {
3703 method = me.Methods [best_match_idx];
3714 // And now convert implicitly, each argument to the required type
3716 pd = GetParameterData (method);
3718 for (int j = argument_count; j > 0;) {
3720 Argument a = (Argument) Arguments [j];
3721 Expression a_expr = a.Expr;
3722 Type parameter_type = pd.ParameterType (j);
3724 if (a_expr.Type != parameter_type){
3728 conv = ConvertImplicitStandard (tc, a_expr, parameter_type,
3731 conv = ConvertImplicit (tc, a_expr, parameter_type,
3735 if (!Location.IsNull (loc)) {
3736 Error (tc, 1502, loc,
3737 "The best overloaded match for method '" + FullMethodDesc (method) +
3738 "' has some invalid arguments");
3739 Error (tc, 1503, loc,
3740 "Argument " + (j+1) +
3741 ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
3742 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
3747 // Update the argument with the implicit conversion
3757 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
3758 ArrayList Arguments, Location loc)
3760 return OverloadResolve (tc, me, Arguments, loc, false);
3763 public override Expression DoResolve (TypeContainer tc)
3766 // First, resolve the expression that is used to
3767 // trigger the invocation
3769 this.expr = expr.Resolve (tc);
3770 if (this.expr == null)
3773 if (!(this.expr is MethodGroupExpr)){
3774 report118 (tc, Location, this.expr, "method group");
3779 // Next, evaluate all the expressions in the argument list
3781 if (Arguments != null){
3782 for (int i = Arguments.Count; i > 0;){
3784 Argument a = (Argument) Arguments [i];
3786 if (!a.Resolve (tc))
3791 method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments,
3794 if (method == null){
3795 Error (tc, -6, Location,
3796 "Could not find any applicable function for this argument list");
3800 if (method is MethodInfo)
3801 type = ((MethodInfo)method).ReturnType;
3803 eclass = ExprClass.Value;
3807 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
3811 if (Arguments != null)
3812 top = Arguments.Count;
3816 for (int i = 0; i < top; i++){
3817 Argument a = (Argument) Arguments [i];
3823 public override void Emit (EmitContext ec)
3825 bool is_static = method.IsStatic;
3828 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
3831 // If this is ourselves, push "this"
3833 if (mg.InstanceExpression == null){
3834 ec.ig.Emit (OpCodes.Ldarg_0);
3837 // Push the instance expression
3839 mg.InstanceExpression.Emit (ec);
3843 if (Arguments != null)
3844 EmitArguments (ec, method, Arguments);
3847 if (method is MethodInfo)
3848 ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
3850 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3852 if (method is MethodInfo)
3853 ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3855 ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3859 public override void EmitStatement (EmitContext ec)
3864 // Pop the return value if there is one
3866 if (method is MethodInfo){
3867 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3868 ec.ig.Emit (OpCodes.Pop);
3873 public class New : ExpressionStatement {
3880 public readonly NType NewType;
3881 public readonly ArrayList Arguments;
3882 public readonly string RequestedType;
3884 // These are for the case when we have an array
3885 public readonly string Rank;
3886 public readonly ArrayList Initializers;
3889 MethodBase method = null;
3891 public New (string requested_type, ArrayList arguments, Location loc)
3893 RequestedType = requested_type;
3894 Arguments = arguments;
3895 NewType = NType.Object;
3899 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3901 RequestedType = requested_type;
3903 Initializers = initializers;
3904 NewType = NType.Array;
3907 Arguments = new ArrayList ();
3909 foreach (Expression e in exprs)
3910 Arguments.Add (new Argument (e, Argument.AType.Expression));
3914 public static string FormLookupType (string base_type, int idx_count, string rank)
3916 StringBuilder sb = new StringBuilder (base_type);
3921 for (int i = 1; i < idx_count; i++)
3925 return sb.ToString ();
3929 public override Expression DoResolve (TypeContainer tc)
3931 if (NewType == NType.Object) {
3933 type = tc.LookupType (RequestedType, false);
3940 ml = MemberLookup (tc, type, ".ctor", false,
3941 MemberTypes.Constructor, AllBindingsFlags);
3943 if (! (ml is MethodGroupExpr)){
3945 // FIXME: Find proper error
3947 report118 (tc, Location, ml, "method group");
3951 if (Arguments != null){
3952 for (int i = Arguments.Count; i > 0;){
3954 Argument a = (Argument) Arguments [i];
3956 if (!a.Resolve (tc))
3961 method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments,
3964 if (method == null) {
3965 Error (tc, -6, Location,
3966 "New invocation: Can not find a constructor for this argument list");
3970 eclass = ExprClass.Value;
3974 if (NewType == NType.Array) {
3976 throw new Exception ("Finish array creation");
3983 public override void Emit (EmitContext ec)
3985 Invocation.EmitArguments (ec, method, Arguments);
3986 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3989 public override void EmitStatement (EmitContext ec)
3992 ec.ig.Emit (OpCodes.Pop);
3997 // Represents the `this' construct
3999 public class This : Expression, LValue {
4000 public override Expression DoResolve (TypeContainer tc)
4002 eclass = ExprClass.Variable;
4003 type = tc.TypeBuilder;
4006 // FIXME: Verify that this is only used in instance contexts.
4011 public override void Emit (EmitContext ec)
4013 ec.ig.Emit (OpCodes.Ldarg_0);
4016 public void Store (EmitContext ec)
4019 // Assignment to the "this" variable.
4021 // FIXME: Apparently this is a bug that we
4022 // must catch as `this' seems to be readonly ;-)
4024 ec.ig.Emit (OpCodes.Starg, 0);
4027 public void AddressOf (EmitContext ec)
4029 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4034 // Implements the typeof operator
4036 public class TypeOf : Expression {
4037 public readonly string QueriedType;
4040 public TypeOf (string queried_type)
4042 QueriedType = queried_type;
4045 public override Expression DoResolve (TypeContainer tc)
4047 typearg = tc.LookupType (QueriedType, false);
4049 if (typearg == null)
4052 type = TypeManager.type_type;
4053 eclass = ExprClass.Type;
4057 public override void Emit (EmitContext ec)
4059 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4060 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4064 public class SizeOf : Expression {
4065 public readonly string QueriedType;
4067 public SizeOf (string queried_type)
4069 this.QueriedType = queried_type;
4072 public override Expression DoResolve (TypeContainer tc)
4074 // FIXME: Implement;
4075 throw new Exception ("Unimplemented");
4079 public override void Emit (EmitContext ec)
4081 throw new Exception ("Implement me");
4085 public class MemberAccess : Expression {
4086 public readonly string Identifier;
4088 Expression member_lookup;
4090 public MemberAccess (Expression expr, string id)
4096 public Expression Expr {
4102 public override Expression DoResolve (TypeContainer tc)
4104 Expression new_expression = expr.Resolve (tc);
4106 if (new_expression == null)
4109 member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
4111 if (member_lookup is MethodGroupExpr){
4112 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4115 // Bind the instance expression to it
4117 // FIXME: This is a horrible way of detecting if it is
4118 // an instance expression. Figure out how to fix this.
4121 if (expr is LocalVariableReference ||
4122 expr is ParameterReference ||
4124 mg.InstanceExpression = expr;
4126 return member_lookup;
4127 } else if (member_lookup is FieldExpr){
4128 FieldExpr fe = (FieldExpr) member_lookup;
4132 return member_lookup;
4135 // FIXME: This should generate the proper node
4136 // ie, for a Property Access, it should like call it
4139 return member_lookup;
4142 public override void Emit (EmitContext ec)
4144 throw new Exception ("Should not happen I think");
4150 // Nodes of type Namespace are created during the semantic
4151 // analysis to resolve member_access/qualified_identifier/simple_name
4154 // They are born `resolved'.
4156 public class NamespaceExpr : Expression {
4157 public readonly string Name;
4159 public NamespaceExpr (string name)
4162 eclass = ExprClass.Namespace;
4165 public override Expression DoResolve (TypeContainer tc)
4170 public override void Emit (EmitContext ec)
4172 throw new Exception ("Namespace expressions should never be emitted");
4177 // Fully resolved expression that evaluates to a type
4179 public class TypeExpr : Expression {
4180 public TypeExpr (Type t)
4183 eclass = ExprClass.Type;
4186 override public Expression DoResolve (TypeContainer tc)
4191 override public void Emit (EmitContext ec)
4193 throw new Exception ("Implement me");
4198 // MethodGroup Expression.
4200 // This is a fully resolved expression that evaluates to a type
4202 public class MethodGroupExpr : Expression {
4203 public readonly MethodBase [] Methods;
4204 Expression instance_expression = null;
4206 public MethodGroupExpr (MemberInfo [] mi)
4208 Methods = new MethodBase [mi.Length];
4209 mi.CopyTo (Methods, 0);
4210 eclass = ExprClass.MethodGroup;
4214 // `A method group may have associated an instance expression'
4216 public Expression InstanceExpression {
4218 return instance_expression;
4222 instance_expression = value;
4226 override public Expression DoResolve (TypeContainer tc)
4231 override public void Emit (EmitContext ec)
4233 throw new Exception ("This should never be reached");
4237 // Fully resolved expression that evaluates to a Field
4239 public class FieldExpr : Expression, LValue {
4240 public readonly FieldInfo FieldInfo;
4241 public Expression Instance;
4243 public FieldExpr (FieldInfo fi)
4246 eclass = ExprClass.Variable;
4247 type = fi.FieldType;
4250 override public Expression DoResolve (TypeContainer tc)
4252 if (!FieldInfo.IsStatic){
4253 if (Instance == null){
4254 throw new Exception ("non-static FieldExpr without instance var\n" +
4255 "You have to assign the Instance variable\n" +
4256 "Of the FieldExpr to set this\n");
4259 Instance = Instance.Resolve (tc);
4260 if (Instance == null)
4267 override public void Emit (EmitContext ec)
4269 ILGenerator ig = ec.ig;
4271 if (FieldInfo.IsStatic)
4272 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4276 ig.Emit (OpCodes.Ldfld, FieldInfo);
4280 public void Store (EmitContext ec)
4282 if (FieldInfo.IsStatic)
4283 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
4285 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
4288 public void AddressOf (EmitContext ec)
4290 if (FieldInfo.IsStatic)
4291 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
4294 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
4300 // Fully resolved expression that evaluates to a Property
4302 public class PropertyExpr : Expression {
4303 public readonly PropertyInfo PropertyInfo;
4304 public readonly bool IsStatic;
4306 public PropertyExpr (PropertyInfo pi)
4309 eclass = ExprClass.PropertyAccess;
4312 MethodBase [] acc = pi.GetAccessors ();
4314 for (int i = 0; i < acc.Length; i++)
4315 if (acc [i].IsStatic)
4318 type = pi.PropertyType;
4321 override public Expression DoResolve (TypeContainer tc)
4323 // We are born in resolved state.
4327 override public void Emit (EmitContext ec)
4329 // FIXME: Implement;
4330 throw new Exception ("Unimplemented");
4335 // Fully resolved expression that evaluates to a Expression
4337 public class EventExpr : Expression {
4338 public readonly EventInfo EventInfo;
4340 public EventExpr (EventInfo ei)
4343 eclass = ExprClass.EventAccess;
4346 override public Expression DoResolve (TypeContainer tc)
4348 // We are born in resolved state.
4352 override public void Emit (EmitContext ec)
4354 throw new Exception ("Implement me");
4355 // FIXME: Implement.
4359 public class CheckedExpr : Expression {
4361 public Expression Expr;
4363 public CheckedExpr (Expression e)
4368 public override Expression DoResolve (TypeContainer tc)
4370 Expr = Expr.Resolve (tc);
4375 eclass = Expr.ExprClass;
4380 public override void Emit (EmitContext ec)
4382 bool last_check = ec.CheckState;
4384 ec.CheckState = true;
4386 ec.CheckState = last_check;
4391 public class UnCheckedExpr : Expression {
4393 public Expression Expr;
4395 public UnCheckedExpr (Expression e)
4400 public override Expression DoResolve (TypeContainer tc)
4402 Expr = Expr.Resolve (tc);
4407 eclass = Expr.ExprClass;
4412 public override void Emit (EmitContext ec)
4414 bool last_check = ec.CheckState;
4416 ec.CheckState = false;
4418 ec.CheckState = last_check;
4423 public class ElementAccess : Expression, LValue {
4425 public ArrayList Arguments;
4426 public Expression Expr;
4430 public ElementAccess (Expression e, ArrayList e_list, Location loc)
4434 Arguments = new ArrayList ();
4435 foreach (Expression tmp in e_list)
4436 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
4441 public override Expression DoResolve (TypeContainer tc)
4443 Expr = Expr.Resolve (tc);
4445 //Console.WriteLine (Expr.ToString ());
4450 if (Arguments == null)
4453 if (Expr.ExprClass != ExprClass.Variable) {
4454 report118 (tc, location, Expr, "variable");
4458 if (Arguments != null){
4459 for (int i = Arguments.Count; i > 0;){
4461 Argument a = (Argument) Arguments [i];
4463 if (!a.Resolve (tc))
4466 Type a_type = a.expr.Type;
4467 if (!(StandardConversionExists (a_type, TypeManager.int32_type) ||
4468 StandardConversionExists (a_type, TypeManager.uint32_type) ||
4469 StandardConversionExists (a_type, TypeManager.int64_type) ||
4470 StandardConversionExists (a_type, TypeManager.uint64_type)))
4476 // FIXME : Implement the actual storage here.
4478 throw new Exception ("Finish element access");
4482 public void Store (EmitContext ec)
4484 throw new Exception ("Implement me !");
4487 public void AddressOf (EmitContext ec)
4489 throw new Exception ("Implement me !");
4492 public override void Emit (EmitContext ec)
4494 throw new Exception ("Implement me !");
4499 public class BaseAccess : Expression {
4501 public enum BaseAccessType {
4506 public readonly BaseAccessType BAType;
4507 public readonly string Member;
4508 public readonly ArrayList Arguments;
4510 public BaseAccess (BaseAccessType t, string member, ArrayList args)
4518 public override Expression DoResolve (TypeContainer tc)
4520 // FIXME: Implement;
4521 throw new Exception ("Unimplemented");
4525 public override void Emit (EmitContext ec)
4527 throw new Exception ("Unimplemented");
4532 // This class exists solely to pass the Type around and to be a dummy
4533 // that can be passed to the conversion functions (this is used by
4534 // foreach implementation to typecast the object return value from
4535 // get_Current into the proper type. All code has been generated and
4536 // we only care about the side effect conversions to be performed
4539 public class EmptyExpression : Expression {
4540 public EmptyExpression ()
4542 type = TypeManager.object_type;
4543 eclass = ExprClass.Value;
4546 public override Expression DoResolve (TypeContainer tc)
4551 public override void Emit (EmitContext ec)
4553 // nothing, as we only exist to not do anything.
4557 public class UserCast : Expression {
4561 public UserCast (MethodInfo method, Expression source)
4563 this.method = method;
4564 this.source = source;
4565 type = method.ReturnType;
4566 eclass = ExprClass.Value;
4569 public override Expression DoResolve (TypeContainer tc)
4572 // We are born fully resolved
4577 public override void Emit (EmitContext ec)
4579 ILGenerator ig = ec.ig;
4583 if (method is MethodInfo)
4584 ig.Emit (OpCodes.Call, (MethodInfo) method);
4586 ig.Emit (OpCodes.Call, (ConstructorInfo) method);