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 {
46 // An interface provided by expressions that can be used as
47 // LValues and can store the value on the top of the stack on
50 public interface IStackStore {
53 // The Store method should store the contents of the top
54 // of the stack into the storage that is implemented by
55 // the particular implementation of LValue
57 void Store (EmitContext ec);
61 // This interface is implemented by variables
63 public interface IMemoryLocation {
65 // The AddressOf method should generate code that loads
66 // the address of the object and leaves it on the stack
68 void AddressOf (EmitContext ec);
72 // Base class for expressions
74 public abstract class Expression {
75 protected ExprClass eclass;
88 public ExprClass ExprClass {
99 // Utility wrapper routine for Error, just to beautify the code
101 static protected void Error (int error, string s)
103 Report.Error (error, s);
106 static protected void Error (int error, Location loc, string s)
108 Report.Error (error, loc, s);
112 // Utility wrapper routine for Warning, just to beautify the code
114 static protected void Warning (int warning, string s)
116 Report.Warning (warning, s);
120 // Performs semantic analysis on the Expression
124 // The Resolve method is invoked to perform the semantic analysis
127 // The return value is an expression (it can be the
128 // same expression in some cases) or a new
129 // expression that better represents this node.
131 // For example, optimizations of Unary (LiteralInt)
132 // would return a new LiteralInt with a negated
135 // If there is an error during semantic analysis,
136 // then an error should be reported (using Report)
137 // and a null value should be returned.
139 // There are two side effects expected from calling
140 // Resolve(): the the field variable "eclass" should
141 // be set to any value of the enumeration
142 // `ExprClass' and the type variable should be set
143 // to a valid type (this is the type of the
147 public abstract Expression DoResolve (EmitContext ec);
149 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
151 return DoResolve (ec);
155 // Currently Resolve wraps DoResolve to perform sanity
156 // checking and assertion checking on what we expect from Resolve
158 public Expression Resolve (EmitContext ec)
160 Expression e = DoResolve (ec);
163 if (e is SimpleName){
164 SimpleName s = (SimpleName) e;
168 "The name `" + s.Name + "' could not be found in `" +
169 ec.TypeContainer.Name + "'");
173 if (e.ExprClass == ExprClass.Invalid)
174 throw new Exception ("Expression " + e +
175 " ExprClass is Invalid after resolve");
177 if (e.ExprClass != ExprClass.MethodGroup)
179 throw new Exception ("Expression " + e +
180 " did not set its type after Resolve");
187 // Just like `Resolve' above, but this allows SimpleNames to be returned.
188 // This is used by MemberAccess to construct long names that can not be
189 // partially resolved (namespace-qualified names for example).
191 public Expression ResolveWithSimpleName (EmitContext ec)
193 Expression e = DoResolve (ec);
199 if (e.ExprClass == ExprClass.Invalid)
200 throw new Exception ("Expression " + e +
201 " ExprClass is Invalid after resolve");
203 if (e.ExprClass != ExprClass.MethodGroup)
205 throw new Exception ("Expression " + e +
206 " did not set its type after Resolve");
213 // Currently ResolveLValue wraps DoResolveLValue to perform sanity
214 // checking and assertion checking on what we expect from Resolve
216 public Expression ResolveLValue (EmitContext ec, Expression right_side)
218 Expression e = DoResolveLValue (ec, right_side);
221 if (e is SimpleName){
222 SimpleName s = (SimpleName) e;
226 "The name `" + s.Name + "' could not be found in `" +
227 ec.TypeContainer.Name + "'");
231 if (e.ExprClass == ExprClass.Invalid)
232 throw new Exception ("Expression " + e +
233 " ExprClass is Invalid after resolve");
235 if (e.ExprClass != ExprClass.MethodGroup)
237 throw new Exception ("Expression " + e +
238 " did not set its type after Resolve");
245 // Emits the code for the expression
250 // The Emit method is invoked to generate the code
251 // for the expression.
254 public abstract void Emit (EmitContext ec);
257 // Protected constructor. Only derivate types should
258 // be able to be created
261 protected Expression ()
263 eclass = ExprClass.Invalid;
268 // Returns a literalized version of a literal FieldInfo
270 static Expression Literalize (FieldInfo fi)
272 Type t = fi.FieldType;
273 object v = fi.GetValue (fi);
275 if (t == TypeManager.int32_type)
276 return new IntLiteral ((int) v);
277 else if (t == TypeManager.uint32_type)
278 return new UIntLiteral ((uint) v);
279 else if (t == TypeManager.int64_type)
280 return new LongLiteral ((long) v);
281 else if (t == TypeManager.uint64_type)
282 return new ULongLiteral ((ulong) v);
283 else if (t == TypeManager.float_type)
284 return new FloatLiteral ((float) v);
285 else if (t == TypeManager.double_type)
286 return new DoubleLiteral ((double) v);
287 else if (t == TypeManager.string_type)
288 return new StringLiteral ((string) v);
289 else if (t == TypeManager.short_type)
290 return new IntLiteral ((int) ((short)v));
291 else if (t == TypeManager.ushort_type)
292 return new IntLiteral ((int) ((ushort)v));
293 else if (t == TypeManager.sbyte_type)
294 return new IntLiteral ((int) ((sbyte)v));
295 else if (t == TypeManager.byte_type)
296 return new IntLiteral ((int) ((byte)v));
297 else if (t == TypeManager.char_type)
298 return new IntLiteral ((int) ((char)v));
300 throw new Exception ("Unknown type for literal (" + v.GetType () +
301 "), details: " + fi);
305 // Returns a fully formed expression after a MemberLookup
307 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
309 if (mi is EventInfo){
310 return new EventExpr ((EventInfo) mi, loc);
311 } else if (mi is FieldInfo){
312 FieldInfo fi = (FieldInfo) mi;
315 Expression e = Literalize (fi);
320 return new FieldExpr (fi, loc);
321 } else if (mi is PropertyInfo){
322 return new PropertyExpr ((PropertyInfo) mi, loc);
323 } else if (mi is Type)
324 return new TypeExpr ((Type) mi);
330 // FIXME: Probably implement a cache for (t,name,current_access_set)?
332 // FIXME: We need to cope with access permissions here, or this wont
335 // This code could use some optimizations, but we need to do some
336 // measurements. For example, we could use a delegate to `flag' when
337 // something can not any longer be a method-group (because it is something
341 // If the return value is an Array, then it is an array of
344 // If the return value is an MemberInfo, it is anything, but a Method
348 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
349 // the arguments here and have MemberLookup return only the methods that
350 // match the argument count/type, unlike we are doing now (we delay this
353 // This is so we can catch correctly attempts to invoke instance methods
354 // from a static body (scan for error 120 in ResolveSimpleName).
356 public static Expression MemberLookup (EmitContext ec, Type t, string name,
357 bool same_type, MemberTypes mt,
358 BindingFlags bf, Location loc)
361 bf |= BindingFlags.NonPublic;
363 MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
364 t, mt, bf, Type.FilterName, name);
369 // FIXME : How does this wierd case arise ?
373 if (mi.Length == 1 && !(mi [0] is MethodBase))
374 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
376 for (int i = 0; i < mi.Length; i++)
377 if (!(mi [i] is MethodBase)){
378 Error (-5, "Do not know how to reproduce this case: " +
379 "Methods and non-Method with the same name, " +
380 "report this please");
382 for (i = 0; i < mi.Length; i++){
383 Type tt = mi [i].GetType ();
385 Console.WriteLine (i + ": " + mi [i]);
386 while (tt != TypeManager.object_type){
387 Console.WriteLine (tt);
393 return new MethodGroupExpr (mi);
396 public const MemberTypes AllMemberTypes =
397 MemberTypes.Constructor |
401 MemberTypes.NestedType |
402 MemberTypes.Property;
404 public const BindingFlags AllBindingsFlags =
405 BindingFlags.Public |
406 BindingFlags.Static |
407 BindingFlags.Instance;
409 public static Expression MemberLookup (EmitContext ec, Type t, string name,
410 bool same_type, Location loc)
412 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
415 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
417 Type expr_type = expr.Type;
419 if (target_type == TypeManager.object_type) {
420 if (expr_type.IsClass)
421 return new EmptyCast (expr, target_type);
422 if (expr_type.IsValueType)
423 return new BoxedCast (expr);
424 } else if (expr_type.IsSubclassOf (target_type)) {
425 return new EmptyCast (expr, target_type);
427 // from any class-type S to any interface-type T.
428 if (expr_type.IsClass && target_type.IsInterface) {
430 if (TypeManager.ImplementsInterface (expr_type, target_type))
431 return new EmptyCast (expr, target_type);
436 // from any interface type S to interface-type T.
437 if (expr_type.IsInterface && target_type.IsInterface) {
439 if (TypeManager.ImplementsInterface (expr_type, target_type))
440 return new EmptyCast (expr, target_type);
445 // from an array-type S to an array-type of type T
446 if (expr_type.IsArray && target_type.IsArray) {
447 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
449 Type expr_element_type = expr_type.GetElementType ();
450 Type target_element_type = target_type.GetElementType ();
452 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
453 if (StandardConversionExists (expr_element_type,
454 target_element_type))
455 return new EmptyCast (expr, target_type);
460 // from an array-type to System.Array
461 if (expr_type.IsArray && target_type == TypeManager.array_type)
462 return new EmptyCast (expr, target_type);
464 // from any delegate type to System.Delegate
465 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
466 target_type == TypeManager.delegate_type)
467 return new EmptyCast (expr, target_type);
469 // from any array-type or delegate type into System.ICloneable.
470 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
471 if (target_type == TypeManager.icloneable_type)
472 return new EmptyCast (expr, target_type);
474 // from the null type to any reference-type.
475 if (expr is NullLiteral)
476 return new EmptyCast (expr, target_type);
486 // Handles expressions like this: decimal d; d = 1;
487 // and changes them into: decimal d; d = new System.Decimal (1);
489 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
491 ArrayList args = new ArrayList ();
493 args.Add (new Argument (expr, Argument.AType.Expression));
495 Expression ne = new New (target.FullName, args,
498 return ne.Resolve (ec);
502 // Implicit Numeric Conversions.
504 // expr is the expression to convert, returns a new expression of type
505 // target_type or null if an implicit conversion is not possible.
508 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
509 Type target_type, Location loc)
511 Type expr_type = expr.Type;
514 // Attempt to do the implicit constant expression conversions
516 if (expr is IntLiteral){
519 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
522 } else if (expr is LongLiteral){
524 // Try the implicit constant expression conversion
525 // from long to ulong, instead of a nice routine,
528 if (((LongLiteral) expr).Value > 0)
529 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
532 if (expr_type == TypeManager.sbyte_type){
534 // From sbyte to short, int, long, float, double.
536 if (target_type == TypeManager.int32_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
538 if (target_type == TypeManager.int64_type)
539 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
540 if (target_type == TypeManager.double_type)
541 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
542 if (target_type == TypeManager.float_type)
543 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
544 if (target_type == TypeManager.short_type)
545 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
546 if (target_type == TypeManager.decimal_type)
547 return InternalTypeConstructor (ec, expr, target_type);
548 } else if (expr_type == TypeManager.byte_type){
550 // From byte to short, ushort, int, uint, long, ulong, float, double
552 if ((target_type == TypeManager.short_type) ||
553 (target_type == TypeManager.ushort_type) ||
554 (target_type == TypeManager.int32_type) ||
555 (target_type == TypeManager.uint32_type))
556 return new EmptyCast (expr, target_type);
558 if (target_type == TypeManager.uint64_type)
559 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
560 if (target_type == TypeManager.int64_type)
561 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
563 if (target_type == TypeManager.float_type)
564 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
565 if (target_type == TypeManager.double_type)
566 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
567 if (target_type == TypeManager.decimal_type)
568 return InternalTypeConstructor (ec, expr, target_type);
569 } else if (expr_type == TypeManager.short_type){
571 // From short to int, long, float, double
573 if (target_type == TypeManager.int32_type)
574 return new EmptyCast (expr, target_type);
575 if (target_type == TypeManager.int64_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
577 if (target_type == TypeManager.double_type)
578 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
579 if (target_type == TypeManager.float_type)
580 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
581 if (target_type == TypeManager.decimal_type)
582 return InternalTypeConstructor (ec, expr, target_type);
583 } else if (expr_type == TypeManager.ushort_type){
585 // From ushort to int, uint, long, ulong, float, double
587 if (target_type == TypeManager.uint32_type)
588 return new EmptyCast (expr, target_type);
590 if (target_type == TypeManager.uint64_type)
591 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
592 if (target_type == TypeManager.int32_type)
593 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
594 if (target_type == TypeManager.int64_type)
595 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
596 if (target_type == TypeManager.double_type)
597 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
598 if (target_type == TypeManager.float_type)
599 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
600 if (target_type == TypeManager.decimal_type)
601 return InternalTypeConstructor (ec, expr, target_type);
602 } else if (expr_type == TypeManager.int32_type){
604 // From int to long, float, double
606 if (target_type == TypeManager.int64_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
608 if (target_type == TypeManager.double_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
610 if (target_type == TypeManager.float_type)
611 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
612 if (target_type == TypeManager.decimal_type)
613 return InternalTypeConstructor (ec, expr, target_type);
614 } else if (expr_type == TypeManager.uint32_type){
616 // From uint to long, ulong, float, double
618 if (target_type == TypeManager.int64_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
620 if (target_type == TypeManager.uint64_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
622 if (target_type == TypeManager.double_type)
623 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
625 if (target_type == TypeManager.float_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
628 if (target_type == TypeManager.decimal_type)
629 return InternalTypeConstructor (ec, expr, target_type);
630 } else if ((expr_type == TypeManager.uint64_type) ||
631 (expr_type == TypeManager.int64_type)){
633 // From long/ulong to float, double
635 if (target_type == TypeManager.double_type)
636 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
638 if (target_type == TypeManager.float_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
641 if (target_type == TypeManager.decimal_type)
642 return InternalTypeConstructor (ec, expr, target_type);
643 } else if (expr_type == TypeManager.char_type){
645 // From char to ushort, int, uint, long, ulong, float, double
647 if ((target_type == TypeManager.ushort_type) ||
648 (target_type == TypeManager.int32_type) ||
649 (target_type == TypeManager.uint32_type))
650 return new EmptyCast (expr, target_type);
651 if (target_type == TypeManager.uint64_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
653 if (target_type == TypeManager.int64_type)
654 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
655 if (target_type == TypeManager.float_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
657 if (target_type == TypeManager.double_type)
658 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
659 if (target_type == TypeManager.decimal_type)
660 return InternalTypeConstructor (ec, expr, target_type);
661 } else if (expr_type == TypeManager.float_type){
665 if (target_type == TypeManager.double_type)
666 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
673 // Determines if a standard implicit conversion exists from
674 // expr_type to target_type
676 public static bool StandardConversionExists (Type expr_type, Type target_type)
678 if (expr_type == target_type)
681 // First numeric conversions
683 if (expr_type == TypeManager.sbyte_type){
685 // From sbyte to short, int, long, float, double.
687 if ((target_type == TypeManager.int32_type) ||
688 (target_type == TypeManager.int64_type) ||
689 (target_type == TypeManager.double_type) ||
690 (target_type == TypeManager.float_type) ||
691 (target_type == TypeManager.short_type) ||
692 (target_type == TypeManager.decimal_type))
695 } else if (expr_type == TypeManager.byte_type){
697 // From byte to short, ushort, int, uint, long, ulong, float, double
699 if ((target_type == TypeManager.short_type) ||
700 (target_type == TypeManager.ushort_type) ||
701 (target_type == TypeManager.int32_type) ||
702 (target_type == TypeManager.uint32_type) ||
703 (target_type == TypeManager.uint64_type) ||
704 (target_type == TypeManager.int64_type) ||
705 (target_type == TypeManager.float_type) ||
706 (target_type == TypeManager.double_type) ||
707 (target_type == TypeManager.decimal_type))
710 } else if (expr_type == TypeManager.short_type){
712 // From short to int, long, float, double
714 if ((target_type == TypeManager.int32_type) ||
715 (target_type == TypeManager.int64_type) ||
716 (target_type == TypeManager.double_type) ||
717 (target_type == TypeManager.float_type) ||
718 (target_type == TypeManager.decimal_type))
721 } else if (expr_type == TypeManager.ushort_type){
723 // From ushort to int, uint, long, ulong, float, double
725 if ((target_type == TypeManager.uint32_type) ||
726 (target_type == TypeManager.uint64_type) ||
727 (target_type == TypeManager.int32_type) ||
728 (target_type == TypeManager.int64_type) ||
729 (target_type == TypeManager.double_type) ||
730 (target_type == TypeManager.float_type) ||
731 (target_type == TypeManager.decimal_type))
734 } else if (expr_type == TypeManager.int32_type){
736 // From int to long, float, double
738 if ((target_type == TypeManager.int64_type) ||
739 (target_type == TypeManager.double_type) ||
740 (target_type == TypeManager.float_type) ||
741 (target_type == TypeManager.decimal_type))
744 } else if (expr_type == TypeManager.uint32_type){
746 // From uint to long, ulong, float, double
748 if ((target_type == TypeManager.int64_type) ||
749 (target_type == TypeManager.uint64_type) ||
750 (target_type == TypeManager.double_type) ||
751 (target_type == TypeManager.float_type) ||
752 (target_type == TypeManager.decimal_type))
755 } else if ((expr_type == TypeManager.uint64_type) ||
756 (expr_type == TypeManager.int64_type)) {
758 // From long/ulong to float, double
760 if ((target_type == TypeManager.double_type) ||
761 (target_type == TypeManager.float_type) ||
762 (target_type == TypeManager.decimal_type))
765 } else if (expr_type == TypeManager.char_type){
767 // From char to ushort, int, uint, long, ulong, float, double
769 if ((target_type == TypeManager.ushort_type) ||
770 (target_type == TypeManager.int32_type) ||
771 (target_type == TypeManager.uint32_type) ||
772 (target_type == TypeManager.uint64_type) ||
773 (target_type == TypeManager.int64_type) ||
774 (target_type == TypeManager.float_type) ||
775 (target_type == TypeManager.double_type) ||
776 (target_type == TypeManager.decimal_type))
779 } else if (expr_type == TypeManager.float_type){
783 if (target_type == TypeManager.double_type)
787 // Next reference conversions
789 if (target_type == TypeManager.object_type) {
790 if ((expr_type.IsClass) ||
791 (expr_type.IsValueType))
794 } else if (expr_type.IsSubclassOf (target_type)) {
798 // from any class-type S to any interface-type T.
799 if (expr_type.IsClass && target_type.IsInterface)
802 // from any interface type S to interface-type T.
803 // FIXME : Is it right to use IsAssignableFrom ?
804 if (expr_type.IsInterface && target_type.IsInterface)
805 if (target_type.IsAssignableFrom (expr_type))
808 // from an array-type S to an array-type of type T
809 if (expr_type.IsArray && target_type.IsArray) {
810 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
812 Type expr_element_type = expr_type.GetElementType ();
813 Type target_element_type = target_type.GetElementType ();
815 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
816 if (StandardConversionExists (expr_element_type,
817 target_element_type))
822 // from an array-type to System.Array
823 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
826 // from any delegate type to System.Delegate
827 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
828 target_type == TypeManager.delegate_type)
829 if (target_type.IsAssignableFrom (expr_type))
832 // from any array-type or delegate type into System.ICloneable.
833 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
834 if (target_type == TypeManager.icloneable_type)
837 // from the null type to any reference-type.
838 // FIXME : How do we do this ?
846 // Finds "most encompassed type" according to the spec (13.4.2)
847 // amongst the methods in the MethodGroupExpr which convert from a
848 // type encompassing source_type
850 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
854 for (int i = me.Methods.Length; i > 0; ) {
857 MethodBase mb = me.Methods [i];
858 ParameterData pd = Invocation.GetParameterData (mb);
859 Type param_type = pd.ParameterType (0);
861 if (StandardConversionExists (source_type, param_type)) {
865 if (StandardConversionExists (param_type, best))
874 // Finds "most encompassing type" according to the spec (13.4.2)
875 // amongst the methods in the MethodGroupExpr which convert to a
876 // type encompassed by target_type
878 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
882 for (int i = me.Methods.Length; i > 0; ) {
885 MethodInfo mi = (MethodInfo) me.Methods [i];
886 Type ret_type = mi.ReturnType;
888 if (StandardConversionExists (ret_type, target)) {
892 if (!StandardConversionExists (ret_type, best))
904 // User-defined Implicit conversions
906 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
907 Type target, Location loc)
909 return UserDefinedConversion (ec, source, target, loc, false);
913 // User-defined Explicit conversions
915 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
916 Type target, Location loc)
918 return UserDefinedConversion (ec, source, target, loc, true);
922 // User-defined conversions
924 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
925 Type target, Location loc,
926 bool look_for_explicit)
928 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
929 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
931 MethodBase method = null;
932 Type source_type = source.Type;
936 // If we have a boolean type, we need to check for the True operator
938 // FIXME : How does the False operator come into the picture ?
939 // FIXME : This doesn't look complete and very correct !
940 if (target == TypeManager.bool_type)
943 op_name = "op_Implicit";
945 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
947 if (source_type.BaseType != null)
948 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
950 mg3 = MemberLookup (ec, target, op_name, false, loc);
952 if (target.BaseType != null)
953 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
955 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
956 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
958 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
960 MethodGroupExpr union4 = null;
962 if (look_for_explicit) {
964 op_name = "op_Explicit";
966 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
968 if (source_type.BaseType != null)
969 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
971 mg7 = MemberLookup (ec, target, op_name, false, loc);
973 if (target.BaseType != null)
974 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
976 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
977 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
979 union4 = Invocation.MakeUnionSet (union5, union6);
982 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
986 Type most_specific_source, most_specific_target;
988 most_specific_source = FindMostEncompassedType (union, source_type);
989 if (most_specific_source == null)
992 most_specific_target = FindMostEncompassingType (union, target);
993 if (most_specific_target == null)
998 for (int i = union.Methods.Length; i > 0;) {
1001 MethodBase mb = union.Methods [i];
1002 ParameterData pd = Invocation.GetParameterData (mb);
1003 MethodInfo mi = (MethodInfo) union.Methods [i];
1005 if (pd.ParameterType (0) == most_specific_source &&
1006 mi.ReturnType == most_specific_target) {
1012 if (method == null || count > 1) {
1013 Report.Error (-11, loc, "Ambiguous user defined conversion");
1018 // This will do the conversion to the best match that we
1019 // found. Now we need to perform an implict standard conversion
1020 // if the best match was not the type that we were requested
1023 if (look_for_explicit)
1024 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1026 source = ConvertImplicitStandard (ec, source,
1027 most_specific_source, loc);
1032 e = new UserCast ((MethodInfo) method, source);
1034 if (e.Type != target){
1035 if (!look_for_explicit)
1036 e = ConvertImplicitStandard (ec, e, target, loc);
1038 e = ConvertExplicitStandard (ec, e, target, loc);
1049 // Converts implicitly the resolved expression `expr' into the
1050 // `target_type'. It returns a new expression that can be used
1051 // in a context that expects a `target_type'.
1053 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1054 Type target_type, Location loc)
1056 Type expr_type = expr.Type;
1059 if (expr_type == target_type)
1062 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1066 e = ImplicitReferenceConversion (expr, target_type);
1070 e = ImplicitUserConversion (ec, expr, target_type, loc);
1074 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1075 IntLiteral i = (IntLiteral) expr;
1078 return new EmptyCast (expr, target_type);
1086 // Attempts to apply the `Standard Implicit
1087 // Conversion' rules to the expression `expr' into
1088 // the `target_type'. It returns a new expression
1089 // that can be used in a context that expects a
1092 // This is different from `ConvertImplicit' in that the
1093 // user defined implicit conversions are excluded.
1095 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1096 Type target_type, Location loc)
1098 Type expr_type = expr.Type;
1101 if (expr_type == target_type)
1104 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1108 e = ImplicitReferenceConversion (expr, target_type);
1112 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1113 IntLiteral i = (IntLiteral) expr;
1116 return new EmptyCast (expr, target_type);
1121 // Attemps to perform an implict constant conversion of the IntLiteral
1122 // into a different data type using casts (See Implicit Constant
1123 // Expression Conversions)
1125 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1127 int value = il.Value;
1129 if (target_type == TypeManager.sbyte_type){
1130 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1132 } else if (target_type == TypeManager.byte_type){
1133 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1135 } else if (target_type == TypeManager.short_type){
1136 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1138 } else if (target_type == TypeManager.ushort_type){
1139 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1141 } else if (target_type == TypeManager.uint32_type){
1143 // we can optimize this case: a positive int32
1144 // always fits on a uint32
1148 } else if (target_type == TypeManager.uint64_type){
1150 // we can optimize this case: a positive int32
1151 // always fits on a uint64. But we need an opcode
1155 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1162 // Attemptes to implicityly convert `target' into `type', using
1163 // ConvertImplicit. If there is no implicit conversion, then
1164 // an error is signaled
1166 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1167 Type type, Location loc)
1171 e = ConvertImplicit (ec, target, type, loc);
1175 string msg = "Can not convert implicitly from `"+
1176 TypeManager.CSharpName (target.Type) + "' to `" +
1177 TypeManager.CSharpName (type) + "'";
1179 Error (29, loc, msg);
1185 // Performs the explicit numeric conversions
1187 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1190 Type expr_type = expr.Type;
1192 if (expr_type == TypeManager.sbyte_type){
1194 // From sbyte to byte, ushort, uint, ulong, char
1196 if (target_type == TypeManager.byte_type)
1197 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1198 if (target_type == TypeManager.ushort_type)
1199 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1200 if (target_type == TypeManager.uint32_type)
1201 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1202 if (target_type == TypeManager.uint64_type)
1203 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1204 if (target_type == TypeManager.char_type)
1205 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1206 } else if (expr_type == TypeManager.byte_type){
1208 // From byte to sbyte and char
1210 if (target_type == TypeManager.sbyte_type)
1211 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1212 if (target_type == TypeManager.char_type)
1213 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1214 } else if (expr_type == TypeManager.short_type){
1216 // From short to sbyte, byte, ushort, uint, ulong, char
1218 if (target_type == TypeManager.sbyte_type)
1219 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1220 if (target_type == TypeManager.byte_type)
1221 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1222 if (target_type == TypeManager.ushort_type)
1223 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1224 if (target_type == TypeManager.uint32_type)
1225 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1226 if (target_type == TypeManager.uint64_type)
1227 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1228 if (target_type == TypeManager.char_type)
1229 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1230 } else if (expr_type == TypeManager.ushort_type){
1232 // From ushort to sbyte, byte, short, char
1234 if (target_type == TypeManager.sbyte_type)
1235 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1236 if (target_type == TypeManager.byte_type)
1237 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1238 if (target_type == TypeManager.short_type)
1239 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1240 if (target_type == TypeManager.char_type)
1241 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1242 } else if (expr_type == TypeManager.int32_type){
1244 // From int to sbyte, byte, short, ushort, uint, ulong, char
1246 if (target_type == TypeManager.sbyte_type)
1247 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1248 if (target_type == TypeManager.byte_type)
1249 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1250 if (target_type == TypeManager.short_type)
1251 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1252 if (target_type == TypeManager.ushort_type)
1253 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1254 if (target_type == TypeManager.uint32_type)
1255 return new EmptyCast (expr, target_type);
1256 if (target_type == TypeManager.uint64_type)
1257 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1258 if (target_type == TypeManager.char_type)
1259 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1260 } else if (expr_type == TypeManager.uint32_type){
1262 // From uint to sbyte, byte, short, ushort, int, char
1264 if (target_type == TypeManager.sbyte_type)
1265 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1266 if (target_type == TypeManager.byte_type)
1267 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1268 if (target_type == TypeManager.short_type)
1269 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1270 if (target_type == TypeManager.ushort_type)
1271 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1272 if (target_type == TypeManager.int32_type)
1273 return new EmptyCast (expr, target_type);
1274 if (target_type == TypeManager.char_type)
1275 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1276 } else if (expr_type == TypeManager.int64_type){
1278 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1280 if (target_type == TypeManager.sbyte_type)
1281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1282 if (target_type == TypeManager.byte_type)
1283 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1284 if (target_type == TypeManager.short_type)
1285 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1286 if (target_type == TypeManager.ushort_type)
1287 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1288 if (target_type == TypeManager.int32_type)
1289 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1290 if (target_type == TypeManager.uint32_type)
1291 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1292 if (target_type == TypeManager.uint64_type)
1293 return new EmptyCast (expr, target_type);
1294 if (target_type == TypeManager.char_type)
1295 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1296 } else if (expr_type == TypeManager.uint64_type){
1298 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1300 if (target_type == TypeManager.sbyte_type)
1301 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1302 if (target_type == TypeManager.byte_type)
1303 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1304 if (target_type == TypeManager.short_type)
1305 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1306 if (target_type == TypeManager.ushort_type)
1307 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1308 if (target_type == TypeManager.int32_type)
1309 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1310 if (target_type == TypeManager.uint32_type)
1311 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1312 if (target_type == TypeManager.int64_type)
1313 return new EmptyCast (expr, target_type);
1314 if (target_type == TypeManager.char_type)
1315 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1316 } else if (expr_type == TypeManager.char_type){
1318 // From char to sbyte, byte, short
1320 if (target_type == TypeManager.sbyte_type)
1321 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1322 if (target_type == TypeManager.byte_type)
1323 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1324 if (target_type == TypeManager.short_type)
1325 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1326 } else if (expr_type == TypeManager.float_type){
1328 // From float to sbyte, byte, short,
1329 // ushort, int, uint, long, ulong, char
1332 if (target_type == TypeManager.sbyte_type)
1333 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1334 if (target_type == TypeManager.byte_type)
1335 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1336 if (target_type == TypeManager.short_type)
1337 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1338 if (target_type == TypeManager.ushort_type)
1339 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1340 if (target_type == TypeManager.int32_type)
1341 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1342 if (target_type == TypeManager.uint32_type)
1343 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1344 if (target_type == TypeManager.int64_type)
1345 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1346 if (target_type == TypeManager.uint64_type)
1347 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1348 if (target_type == TypeManager.char_type)
1349 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1350 if (target_type == TypeManager.decimal_type)
1351 return InternalTypeConstructor (ec, expr, target_type);
1352 } else if (expr_type == TypeManager.double_type){
1354 // From double to byte, byte, short,
1355 // ushort, int, uint, long, ulong,
1356 // char, float or decimal
1358 if (target_type == TypeManager.sbyte_type)
1359 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1360 if (target_type == TypeManager.byte_type)
1361 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1362 if (target_type == TypeManager.short_type)
1363 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1364 if (target_type == TypeManager.ushort_type)
1365 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1366 if (target_type == TypeManager.int32_type)
1367 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1368 if (target_type == TypeManager.uint32_type)
1369 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1370 if (target_type == TypeManager.int64_type)
1371 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1372 if (target_type == TypeManager.uint64_type)
1373 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1374 if (target_type == TypeManager.char_type)
1375 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1376 if (target_type == TypeManager.float_type)
1377 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1378 if (target_type == TypeManager.decimal_type)
1379 return InternalTypeConstructor (ec, expr, target_type);
1382 // decimal is taken care of by the op_Explicit methods.
1388 // Returns whether an explicit reference conversion can be performed
1389 // from source_type to target_type
1391 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1393 bool target_is_value_type = target_type.IsValueType;
1395 if (source_type == target_type)
1399 // From object to any reference type
1401 if (source_type == TypeManager.object_type && !target_is_value_type)
1405 // From any class S to any class-type T, provided S is a base class of T
1407 if (target_type.IsSubclassOf (source_type))
1411 // From any interface type S to any interface T provided S is not derived from T
1413 if (source_type.IsInterface && target_type.IsInterface){
1414 if (!target_type.IsSubclassOf (source_type))
1419 // From any class type S to any interface T, provides S is not sealed
1420 // and provided S does not implement T.
1422 if (target_type.IsInterface && !source_type.IsSealed &&
1423 !target_type.IsAssignableFrom (source_type))
1427 // From any interface-type S to to any class type T, provided T is not
1428 // sealed, or provided T implements S.
1430 if (source_type.IsInterface &&
1431 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1434 // From an array type S with an element type Se to an array type T with an
1435 // element type Te provided all the following are true:
1436 // * S and T differe only in element type, in other words, S and T
1437 // have the same number of dimensions.
1438 // * Both Se and Te are reference types
1439 // * An explicit referenc conversions exist from Se to Te
1441 if (source_type.IsArray && target_type.IsArray) {
1442 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1444 Type source_element_type = source_type.GetElementType ();
1445 Type target_element_type = target_type.GetElementType ();
1447 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1448 if (ExplicitReferenceConversionExists (source_element_type,
1449 target_element_type))
1455 // From System.Array to any array-type
1456 if (source_type == TypeManager.array_type &&
1457 target_type.IsSubclassOf (TypeManager.array_type)){
1462 // From System delegate to any delegate-type
1464 if (source_type == TypeManager.delegate_type &&
1465 target_type.IsSubclassOf (TypeManager.delegate_type))
1469 // From ICloneable to Array or Delegate types
1471 if (source_type == TypeManager.icloneable_type &&
1472 (target_type == TypeManager.array_type ||
1473 target_type == TypeManager.delegate_type))
1480 // Implements Explicit Reference conversions
1482 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1484 Type source_type = source.Type;
1485 bool target_is_value_type = target_type.IsValueType;
1488 // From object to any reference type
1490 if (source_type == TypeManager.object_type && !target_is_value_type)
1491 return new ClassCast (source, target_type);
1495 // From any class S to any class-type T, provided S is a base class of T
1497 if (target_type.IsSubclassOf (source_type))
1498 return new ClassCast (source, target_type);
1501 // From any interface type S to any interface T provided S is not derived from T
1503 if (source_type.IsInterface && target_type.IsInterface){
1505 Type [] ifaces = source_type.GetInterfaces ();
1507 if (TypeManager.ImplementsInterface (source_type, target_type))
1510 return new ClassCast (source, target_type);
1514 // From any class type S to any interface T, provides S is not sealed
1515 // and provided S does not implement T.
1517 if (target_type.IsInterface && !source_type.IsSealed) {
1519 if (TypeManager.ImplementsInterface (source_type, target_type))
1522 return new ClassCast (source, target_type);
1527 // From any interface-type S to to any class type T, provided T is not
1528 // sealed, or provided T implements S.
1530 if (source_type.IsInterface) {
1532 if (target_type.IsSealed)
1535 if (TypeManager.ImplementsInterface (target_type, source_type))
1536 return new ClassCast (source, target_type);
1541 // From an array type S with an element type Se to an array type T with an
1542 // element type Te provided all the following are true:
1543 // * S and T differe only in element type, in other words, S and T
1544 // have the same number of dimensions.
1545 // * Both Se and Te are reference types
1546 // * An explicit referenc conversions exist from Se to Te
1548 if (source_type.IsArray && target_type.IsArray) {
1549 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1551 Type source_element_type = source_type.GetElementType ();
1552 Type target_element_type = target_type.GetElementType ();
1554 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1555 if (ExplicitReferenceConversionExists (source_element_type,
1556 target_element_type))
1557 return new ClassCast (source, target_type);
1562 // From System.Array to any array-type
1563 if (source_type == TypeManager.array_type &&
1564 target_type.IsSubclassOf (TypeManager.array_type)){
1565 return new ClassCast (source, target_type);
1569 // From System delegate to any delegate-type
1571 if (source_type == TypeManager.delegate_type &&
1572 target_type.IsSubclassOf (TypeManager.delegate_type))
1573 return new ClassCast (source, target_type);
1576 // From ICloneable to Array or Delegate types
1578 if (source_type == TypeManager.icloneable_type &&
1579 (target_type == TypeManager.array_type ||
1580 target_type == TypeManager.delegate_type))
1581 return new ClassCast (source, target_type);
1587 // Performs an explicit conversion of the expression `expr' whose
1588 // type is expr.Type to `target_type'.
1590 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1591 Type target_type, Location loc)
1593 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1598 ne = ConvertNumericExplicit (ec, expr, target_type);
1602 ne = ConvertReferenceExplicit (expr, target_type);
1606 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1610 Report.Error (30, loc, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1611 + TypeManager.CSharpName (target_type) + "'");
1616 // Same as ConverExplicit, only it doesn't include user defined conversions
1618 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1619 Type target_type, Location l)
1621 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1626 ne = ConvertNumericExplicit (ec, expr, target_type);
1630 ne = ConvertReferenceExplicit (expr, target_type);
1634 Report.Error (30, l, "Cannot convert type '" +
1635 TypeManager.CSharpName (expr.Type) + "' to '" +
1636 TypeManager.CSharpName (target_type) + "'");
1640 static string ExprClassName (ExprClass c)
1643 case ExprClass.Invalid:
1645 case ExprClass.Value:
1647 case ExprClass.Variable:
1649 case ExprClass.Namespace:
1651 case ExprClass.Type:
1653 case ExprClass.MethodGroup:
1654 return "method group";
1655 case ExprClass.PropertyAccess:
1656 return "property access";
1657 case ExprClass.EventAccess:
1658 return "event access";
1659 case ExprClass.IndexerAccess:
1660 return "indexer access";
1661 case ExprClass.Nothing:
1664 throw new Exception ("Should not happen");
1668 // Reports that we were expecting `expr' to be of class `expected'
1670 protected void report118 (Location loc, Expression expr, string expected)
1672 string kind = "Unknown";
1675 kind = ExprClassName (expr.ExprClass);
1677 Error (118, loc, "Expression denotes a '" + kind +
1678 "' where an " + expected + " was expected");
1683 // This is just a base class for expressions that can
1684 // appear on statements (invocations, object creation,
1685 // assignments, post/pre increment and decrement). The idea
1686 // being that they would support an extra Emition interface that
1687 // does not leave a result on the stack.
1690 public abstract class ExpressionStatement : Expression {
1693 // Requests the expression to be emitted in a `statement'
1694 // context. This means that no new value is left on the
1695 // stack after invoking this method (constrasted with
1696 // Emit that will always leave a value on the stack).
1698 public abstract void EmitStatement (EmitContext ec);
1702 // This kind of cast is used to encapsulate the child
1703 // whose type is child.Type into an expression that is
1704 // reported to return "return_type". This is used to encapsulate
1705 // expressions which have compatible types, but need to be dealt
1706 // at higher levels with.
1708 // For example, a "byte" expression could be encapsulated in one
1709 // of these as an "unsigned int". The type for the expression
1710 // would be "unsigned int".
1714 public class EmptyCast : Expression {
1715 protected Expression child;
1717 public EmptyCast (Expression child, Type return_type)
1719 ExprClass = child.ExprClass;
1724 public override Expression DoResolve (EmitContext ec)
1726 // This should never be invoked, we are born in fully
1727 // initialized state.
1732 public override void Emit (EmitContext ec)
1739 // This kind of cast is used to encapsulate Value Types in objects.
1741 // The effect of it is to box the value type emitted by the previous
1744 public class BoxedCast : EmptyCast {
1746 public BoxedCast (Expression expr)
1747 : base (expr, TypeManager.object_type)
1751 public override Expression DoResolve (EmitContext ec)
1753 // This should never be invoked, we are born in fully
1754 // initialized state.
1759 public override void Emit (EmitContext ec)
1762 ec.ig.Emit (OpCodes.Box, child.Type);
1767 // This kind of cast is used to encapsulate a child expression
1768 // that can be trivially converted to a target type using one or
1769 // two opcodes. The opcodes are passed as arguments.
1771 public class OpcodeCast : EmptyCast {
1775 public OpcodeCast (Expression child, Type return_type, OpCode op)
1776 : base (child, return_type)
1780 second_valid = false;
1783 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1784 : base (child, return_type)
1789 second_valid = true;
1792 public override Expression DoResolve (EmitContext ec)
1794 // This should never be invoked, we are born in fully
1795 // initialized state.
1800 public override void Emit (EmitContext ec)
1812 // This kind of cast is used to encapsulate a child and cast it
1813 // to the class requested
1815 public class ClassCast : EmptyCast {
1816 public ClassCast (Expression child, Type return_type)
1817 : base (child, return_type)
1822 public override Expression DoResolve (EmitContext ec)
1824 // This should never be invoked, we are born in fully
1825 // initialized state.
1830 public override void Emit (EmitContext ec)
1834 ec.ig.Emit (OpCodes.Castclass, type);
1840 // Unary expressions.
1844 // Unary implements unary expressions. It derives from
1845 // ExpressionStatement becuase the pre/post increment/decrement
1846 // operators can be used in a statement context.
1848 public class Unary : ExpressionStatement {
1849 public enum Operator {
1850 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
1851 Indirection, AddressOf, PreIncrement,
1852 PreDecrement, PostIncrement, PostDecrement
1857 ArrayList Arguments;
1861 public Unary (Operator op, Expression expr, Location loc)
1868 public Expression Expr {
1878 public Operator Oper {
1889 // Returns a stringified representation of the Operator
1894 case Operator.UnaryPlus:
1896 case Operator.UnaryNegation:
1898 case Operator.LogicalNot:
1900 case Operator.OnesComplement:
1902 case Operator.AddressOf:
1904 case Operator.Indirection:
1906 case Operator.PreIncrement : case Operator.PostIncrement :
1908 case Operator.PreDecrement : case Operator.PostDecrement :
1912 return oper.ToString ();
1915 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1917 if (expr.Type == target_type)
1920 return ConvertImplicit (ec, expr, target_type, new Location (-1));
1923 void error23 (Type t)
1926 23, loc, "Operator " + OperName () +
1927 " cannot be applied to operand of type `" +
1928 TypeManager.CSharpName (t) + "'");
1932 // Returns whether an object of type `t' can be incremented
1933 // or decremented with add/sub (ie, basically whether we can
1934 // use pre-post incr-decr operations on it, but it is not a
1935 // System.Decimal, which we test elsewhere)
1937 static bool IsIncrementableNumber (Type t)
1939 return (t == TypeManager.sbyte_type) ||
1940 (t == TypeManager.byte_type) ||
1941 (t == TypeManager.short_type) ||
1942 (t == TypeManager.ushort_type) ||
1943 (t == TypeManager.int32_type) ||
1944 (t == TypeManager.uint32_type) ||
1945 (t == TypeManager.int64_type) ||
1946 (t == TypeManager.uint64_type) ||
1947 (t == TypeManager.char_type) ||
1948 (t.IsSubclassOf (TypeManager.enum_type)) ||
1949 (t == TypeManager.float_type) ||
1950 (t == TypeManager.double_type);
1953 Expression ResolveOperator (EmitContext ec)
1955 Type expr_type = expr.Type;
1958 // Step 1: Perform Operator Overload location
1963 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1964 op_name = "op_Increment";
1965 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1966 op_name = "op_Decrement";
1968 op_name = "op_" + oper;
1970 mg = MemberLookup (ec, expr_type, op_name, false, loc);
1972 if (mg == null && expr_type.BaseType != null)
1973 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
1976 Arguments = new ArrayList ();
1977 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1979 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
1981 if (method != null) {
1982 MethodInfo mi = (MethodInfo) method;
1983 type = mi.ReturnType;
1986 error23 (expr_type);
1993 // Step 2: Default operations on CLI native types.
1996 // Only perform numeric promotions on:
1999 if (expr_type == null)
2002 if (oper == Operator.LogicalNot){
2003 if (expr_type != TypeManager.bool_type) {
2004 error23 (expr.Type);
2008 type = TypeManager.bool_type;
2012 if (oper == Operator.OnesComplement) {
2013 if (!((expr_type == TypeManager.int32_type) ||
2014 (expr_type == TypeManager.uint32_type) ||
2015 (expr_type == TypeManager.int64_type) ||
2016 (expr_type == TypeManager.uint64_type) ||
2017 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
2018 error23 (expr.Type);
2025 if (oper == Operator.UnaryPlus) {
2027 // A plus in front of something is just a no-op, so return the child.
2033 // Deals with -literals
2034 // int operator- (int x)
2035 // long operator- (long x)
2036 // float operator- (float f)
2037 // double operator- (double d)
2038 // decimal operator- (decimal d)
2040 if (oper == Operator.UnaryNegation){
2042 // Fold a "- Constant" into a negative constant
2045 Expression e = null;
2048 // Is this a constant?
2050 if (expr is IntLiteral)
2051 e = new IntLiteral (-((IntLiteral) expr).Value);
2052 else if (expr is LongLiteral)
2053 e = new LongLiteral (-((LongLiteral) expr).Value);
2054 else if (expr is FloatLiteral)
2055 e = new FloatLiteral (-((FloatLiteral) expr).Value);
2056 else if (expr is DoubleLiteral)
2057 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
2058 else if (expr is DecimalLiteral)
2059 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
2067 // Not a constant we can optimize, perform numeric
2068 // promotions to int, long, double.
2071 // The following is inneficient, because we call
2072 // ConvertImplicit too many times.
2074 // It is also not clear if we should convert to Float
2075 // or Double initially.
2077 if (expr_type == TypeManager.uint32_type){
2079 // FIXME: handle exception to this rule that
2080 // permits the int value -2147483648 (-2^31) to
2081 // bt written as a decimal interger literal
2083 type = TypeManager.int64_type;
2084 expr = ConvertImplicit (ec, expr, type, loc);
2088 if (expr_type == TypeManager.uint64_type){
2090 // FIXME: Handle exception of `long value'
2091 // -92233720368547758087 (-2^63) to be written as
2092 // decimal integer literal.
2094 error23 (expr_type);
2098 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
2105 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
2112 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
2119 error23 (expr_type);
2124 // The operand of the prefix/postfix increment decrement operators
2125 // should be an expression that is classified as a variable,
2126 // a property access or an indexer access
2128 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2129 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2130 if (expr.ExprClass == ExprClass.Variable){
2131 if (IsIncrementableNumber (expr_type) ||
2132 expr_type == TypeManager.decimal_type){
2136 } else if (expr.ExprClass == ExprClass.IndexerAccess){
2138 // FIXME: Verify that we have both get and set methods
2140 throw new Exception ("Implement me");
2141 } else if (expr.ExprClass == ExprClass.PropertyAccess){
2142 PropertyExpr pe = (PropertyExpr) expr;
2144 if (pe.VerifyAssignable ())
2148 report118 (loc, expr, "variable, indexer or property access");
2152 if (oper == Operator.AddressOf){
2153 if (expr.ExprClass != ExprClass.Variable){
2154 Error (211, "Cannot take the address of non-variables");
2157 type = Type.GetType (expr.Type.ToString () + "*");
2160 Error (187, "No such operator '" + OperName () + "' defined for type '" +
2161 TypeManager.CSharpName (expr_type) + "'");
2166 public override Expression DoResolve (EmitContext ec)
2168 expr = expr.Resolve (ec);
2173 eclass = ExprClass.Value;
2174 return ResolveOperator (ec);
2177 public override void Emit (EmitContext ec)
2179 ILGenerator ig = ec.ig;
2180 Type expr_type = expr.Type;
2183 if (method != null) {
2185 // Note that operators are static anyway
2187 if (Arguments != null)
2188 Invocation.EmitArguments (ec, method, Arguments);
2191 // Post increment/decrement operations need a copy at this
2194 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
2195 ig.Emit (OpCodes.Dup);
2198 ig.Emit (OpCodes.Call, (MethodInfo) method);
2201 // Pre Increment and Decrement operators
2203 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
2204 ig.Emit (OpCodes.Dup);
2208 // Increment and Decrement should store the result
2210 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2211 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2212 ((IStackStore) expr).Store (ec);
2218 case Operator.UnaryPlus:
2219 throw new Exception ("This should be caught by Resolve");
2221 case Operator.UnaryNegation:
2223 ig.Emit (OpCodes.Neg);
2226 case Operator.LogicalNot:
2228 ig.Emit (OpCodes.Ldc_I4_0);
2229 ig.Emit (OpCodes.Ceq);
2232 case Operator.OnesComplement:
2234 ig.Emit (OpCodes.Not);
2237 case Operator.AddressOf:
2238 ((IMemoryLocation)expr).AddressOf (ec);
2241 case Operator.Indirection:
2242 throw new Exception ("Not implemented yet");
2244 case Operator.PreIncrement:
2245 case Operator.PreDecrement:
2246 if (expr.ExprClass == ExprClass.Variable){
2248 // Resolve already verified that it is an "incrementable"
2251 ig.Emit (OpCodes.Ldc_I4_1);
2253 if (oper == Operator.PreDecrement)
2254 ig.Emit (OpCodes.Sub);
2256 ig.Emit (OpCodes.Add);
2257 ig.Emit (OpCodes.Dup);
2258 ((IStackStore) expr).Store (ec);
2260 throw new Exception ("Handle Indexers and Properties here");
2264 case Operator.PostIncrement:
2265 case Operator.PostDecrement:
2266 eclass = expr.ExprClass;
2267 if (eclass == ExprClass.Variable){
2269 // Resolve already verified that it is an "incrementable"
2272 ig.Emit (OpCodes.Dup);
2273 ig.Emit (OpCodes.Ldc_I4_1);
2275 if (oper == Operator.PostDecrement)
2276 ig.Emit (OpCodes.Sub);
2278 ig.Emit (OpCodes.Add);
2279 ((IStackStore) expr).Store (ec);
2280 } else if (eclass == ExprClass.PropertyAccess){
2281 throw new Exception ("Handle Properties here");
2282 } else if (eclass == ExprClass.IndexerAccess) {
2283 throw new Exception ("Handle Indexers here");
2285 Console.WriteLine ("Unknown exprclass: " + eclass);
2290 throw new Exception ("This should not happen: Operator = "
2291 + oper.ToString ());
2296 public override void EmitStatement (EmitContext ec)
2299 // FIXME: we should rewrite this code to generate
2300 // better code for ++ and -- as we know we wont need
2301 // the values on the stack
2304 ec.ig.Emit (OpCodes.Pop);
2308 public class Probe : Expression {
2309 public readonly string ProbeType;
2310 public readonly Operator Oper;
2314 public enum Operator {
2318 public Probe (Operator oper, Expression expr, string probe_type)
2321 ProbeType = probe_type;
2325 public Expression Expr {
2331 public override Expression DoResolve (EmitContext ec)
2333 probe_type = ec.TypeContainer.LookupType (ProbeType, false);
2335 if (probe_type == null)
2338 expr = expr.Resolve (ec);
2340 type = TypeManager.bool_type;
2341 eclass = ExprClass.Value;
2346 public override void Emit (EmitContext ec)
2348 ILGenerator ig = ec.ig;
2352 if (Oper == Operator.Is){
2353 ig.Emit (OpCodes.Isinst, probe_type);
2354 ig.Emit (OpCodes.Ldnull);
2355 ig.Emit (OpCodes.Cgt_Un);
2357 ig.Emit (OpCodes.Isinst, probe_type);
2363 // This represents a typecast in the source language.
2365 // FIXME: Cast expressions have an unusual set of parsing
2366 // rules, we need to figure those out.
2368 public class Cast : Expression {
2373 public Cast (string cast_type, Expression expr, Location loc)
2375 this.target_type = cast_type;
2380 public string TargetType {
2386 public Expression Expr {
2395 public override Expression DoResolve (EmitContext ec)
2397 expr = expr.Resolve (ec);
2401 type = ec.TypeContainer.LookupType (target_type, false);
2402 eclass = ExprClass.Value;
2407 expr = ConvertExplicit (ec, expr, type, loc);
2412 public override void Emit (EmitContext ec)
2415 // This one will never happen
2417 throw new Exception ("Should not happen");
2421 public class Binary : Expression {
2422 public enum Operator {
2423 Multiply, Division, Modulus,
2424 Addition, Subtraction,
2425 LeftShift, RightShift,
2426 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
2427 Equality, Inequality,
2436 Expression left, right;
2438 ArrayList Arguments;
2442 public Binary (Operator oper, Expression left, Expression right, Location loc)
2450 public Operator Oper {
2459 public Expression Left {
2468 public Expression Right {
2479 // Returns a stringified representation of the Operator
2484 case Operator.Multiply:
2486 case Operator.Division:
2488 case Operator.Modulus:
2490 case Operator.Addition:
2492 case Operator.Subtraction:
2494 case Operator.LeftShift:
2496 case Operator.RightShift:
2498 case Operator.LessThan:
2500 case Operator.GreaterThan:
2502 case Operator.LessThanOrEqual:
2504 case Operator.GreaterThanOrEqual:
2506 case Operator.Equality:
2508 case Operator.Inequality:
2510 case Operator.BitwiseAnd:
2512 case Operator.BitwiseOr:
2514 case Operator.ExclusiveOr:
2516 case Operator.LogicalOr:
2518 case Operator.LogicalAnd:
2522 return oper.ToString ();
2525 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
2527 if (expr.Type == target_type)
2530 return ConvertImplicit (ec, expr, target_type, new Location (-1));
2534 // Note that handling the case l == Decimal || r == Decimal
2535 // is taken care of by the Step 1 Operator Overload resolution.
2537 void DoNumericPromotions (EmitContext ec, Type l, Type r)
2539 if (l == TypeManager.double_type || r == TypeManager.double_type){
2541 // If either operand is of type double, the other operand is
2542 // conveted to type double.
2544 if (r != TypeManager.double_type)
2545 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
2546 if (l != TypeManager.double_type)
2547 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
2549 type = TypeManager.double_type;
2550 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
2552 // if either operand is of type float, th eother operand is
2553 // converd to type float.
2555 if (r != TypeManager.double_type)
2556 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
2557 if (l != TypeManager.double_type)
2558 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
2559 type = TypeManager.float_type;
2560 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
2564 // If either operand is of type ulong, the other operand is
2565 // converted to type ulong. or an error ocurrs if the other
2566 // operand is of type sbyte, short, int or long
2569 if (l == TypeManager.uint64_type){
2570 if (r != TypeManager.uint64_type && right is IntLiteral){
2571 e = TryImplicitIntConversion (l, (IntLiteral) right);
2577 if (left is IntLiteral){
2578 e = TryImplicitIntConversion (r, (IntLiteral) left);
2585 if ((other == TypeManager.sbyte_type) ||
2586 (other == TypeManager.short_type) ||
2587 (other == TypeManager.int32_type) ||
2588 (other == TypeManager.int64_type)){
2589 string oper = OperName ();
2591 Error (34, loc, "Operator `" + OperName ()
2592 + "' is ambiguous on operands of type `"
2593 + TypeManager.CSharpName (l) + "' "
2594 + "and `" + TypeManager.CSharpName (r)
2597 type = TypeManager.uint64_type;
2598 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
2600 // If either operand is of type long, the other operand is converted
2603 if (l != TypeManager.int64_type)
2604 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
2605 if (r != TypeManager.int64_type)
2606 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
2608 type = TypeManager.int64_type;
2609 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
2611 // If either operand is of type uint, and the other
2612 // operand is of type sbyte, short or int, othe operands are
2613 // converted to type long.
2617 if (l == TypeManager.uint32_type)
2619 else if (r == TypeManager.uint32_type)
2622 if ((other == TypeManager.sbyte_type) ||
2623 (other == TypeManager.short_type) ||
2624 (other == TypeManager.int32_type)){
2625 left = ForceConversion (ec, left, TypeManager.int64_type);
2626 right = ForceConversion (ec, right, TypeManager.int64_type);
2627 type = TypeManager.int64_type;
2630 // if either operand is of type uint, the other
2631 // operand is converd to type uint
2633 left = ForceConversion (ec, left, TypeManager.uint32_type);
2634 right = ForceConversion (ec, right, TypeManager.uint32_type);
2635 type = TypeManager.uint32_type;
2637 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2638 if (l != TypeManager.decimal_type)
2639 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
2640 if (r != TypeManager.decimal_type)
2641 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
2643 type = TypeManager.decimal_type;
2645 Expression l_tmp, r_tmp;
2647 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
2648 if (l_tmp == null) {
2654 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
2655 if (r_tmp == null) {
2661 type = TypeManager.int32_type;
2668 "Operator " + OperName () + " cannot be applied to operands of type `" +
2669 TypeManager.CSharpName (left.Type) + "' and `" +
2670 TypeManager.CSharpName (right.Type) + "'");
2674 Expression CheckShiftArguments (EmitContext ec)
2678 Type r = right.Type;
2680 e = ForceConversion (ec, right, TypeManager.int32_type);
2687 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
2688 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
2689 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
2690 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
2700 Expression ResolveOperator (EmitContext ec)
2703 Type r = right.Type;
2706 // Step 1: Perform Operator Overload location
2708 Expression left_expr, right_expr;
2710 string op = "op_" + oper;
2712 left_expr = MemberLookup (ec, l, op, false, loc);
2713 if (left_expr == null && l.BaseType != null)
2714 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
2716 right_expr = MemberLookup (ec, r, op, false, loc);
2717 if (right_expr == null && r.BaseType != null)
2718 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
2720 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2722 if (union != null) {
2723 Arguments = new ArrayList ();
2724 Arguments.Add (new Argument (left, Argument.AType.Expression));
2725 Arguments.Add (new Argument (right, Argument.AType.Expression));
2727 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
2728 if (method != null) {
2729 MethodInfo mi = (MethodInfo) method;
2730 type = mi.ReturnType;
2739 // Step 2: Default operations on CLI native types.
2742 // Only perform numeric promotions on:
2743 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2745 if (oper == Operator.Addition){
2747 // If any of the arguments is a string, cast to string
2749 if (l == TypeManager.string_type){
2750 if (r == TypeManager.string_type){
2752 method = TypeManager.string_concat_string_string;
2755 method = TypeManager.string_concat_object_object;
2756 right = ConvertImplicit (ec, right,
2757 TypeManager.object_type, loc);
2759 type = TypeManager.string_type;
2761 Arguments = new ArrayList ();
2762 Arguments.Add (new Argument (left, Argument.AType.Expression));
2763 Arguments.Add (new Argument (right, Argument.AType.Expression));
2767 } else if (r == TypeManager.string_type){
2769 method = TypeManager.string_concat_object_object;
2770 Arguments = new ArrayList ();
2771 Arguments.Add (new Argument (left, Argument.AType.Expression));
2772 Arguments.Add (new Argument (right, Argument.AType.Expression));
2774 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
2775 type = TypeManager.string_type;
2781 // FIXME: is Delegate operator + (D x, D y) handled?
2785 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2786 return CheckShiftArguments (ec);
2788 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2789 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2792 type = TypeManager.bool_type;
2797 // We are dealing with numbers
2800 DoNumericPromotions (ec, l, r);
2802 if (left == null || right == null)
2806 if (oper == Operator.BitwiseAnd ||
2807 oper == Operator.BitwiseOr ||
2808 oper == Operator.ExclusiveOr){
2809 if (!((l == TypeManager.int32_type) ||
2810 (l == TypeManager.uint32_type) ||
2811 (l == TypeManager.int64_type) ||
2812 (l == TypeManager.uint64_type))){
2819 if (oper == Operator.Equality ||
2820 oper == Operator.Inequality ||
2821 oper == Operator.LessThanOrEqual ||
2822 oper == Operator.LessThan ||
2823 oper == Operator.GreaterThanOrEqual ||
2824 oper == Operator.GreaterThan){
2825 type = TypeManager.bool_type;
2831 public override Expression DoResolve (EmitContext ec)
2833 left = left.Resolve (ec);
2834 right = right.Resolve (ec);
2836 if (left == null || right == null)
2839 if (left.Type == null)
2840 throw new Exception (
2841 "Resolve returned non null, but did not set the type! (" +
2842 left + ") at Line: " + loc.Row);
2843 if (right.Type == null)
2844 throw new Exception (
2845 "Resolve returned non null, but did not set the type! (" +
2846 right + ") at Line: "+ loc.Row);
2848 eclass = ExprClass.Value;
2850 return ResolveOperator (ec);
2853 public bool IsBranchable ()
2855 if (oper == Operator.Equality ||
2856 oper == Operator.Inequality ||
2857 oper == Operator.LessThan ||
2858 oper == Operator.GreaterThan ||
2859 oper == Operator.LessThanOrEqual ||
2860 oper == Operator.GreaterThanOrEqual){
2867 // This entry point is used by routines that might want
2868 // to emit a brfalse/brtrue after an expression, and instead
2869 // they could use a more compact notation.
2871 // Typically the code would generate l.emit/r.emit, followed
2872 // by the comparission and then a brtrue/brfalse. The comparissions
2873 // are sometimes inneficient (there are not as complete as the branches
2874 // look for the hacks in Emit using double ceqs).
2876 // So for those cases we provide EmitBranchable that can emit the
2877 // branch with the test
2879 public void EmitBranchable (EmitContext ec, int target)
2882 bool close_target = false;
2888 case Operator.Equality:
2890 opcode = OpCodes.Beq_S;
2892 opcode = OpCodes.Beq;
2895 case Operator.Inequality:
2897 opcode = OpCodes.Bne_Un_S;
2899 opcode = OpCodes.Bne_Un;
2902 case Operator.LessThan:
2904 opcode = OpCodes.Blt_S;
2906 opcode = OpCodes.Blt;
2909 case Operator.GreaterThan:
2911 opcode = OpCodes.Bgt_S;
2913 opcode = OpCodes.Bgt;
2916 case Operator.LessThanOrEqual:
2918 opcode = OpCodes.Ble_S;
2920 opcode = OpCodes.Ble;
2923 case Operator.GreaterThanOrEqual:
2925 opcode = OpCodes.Bge_S;
2927 opcode = OpCodes.Ble;
2931 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2932 + oper.ToString ());
2935 ec.ig.Emit (opcode, target);
2938 public override void Emit (EmitContext ec)
2940 ILGenerator ig = ec.ig;
2942 Type r = right.Type;
2945 if (method != null) {
2947 // Note that operators are static anyway
2949 if (Arguments != null)
2950 Invocation.EmitArguments (ec, method, Arguments);
2952 if (method is MethodInfo)
2953 ig.Emit (OpCodes.Call, (MethodInfo) method);
2955 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2964 case Operator.Multiply:
2966 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2967 opcode = OpCodes.Mul_Ovf;
2968 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2969 opcode = OpCodes.Mul_Ovf_Un;
2971 opcode = OpCodes.Mul;
2973 opcode = OpCodes.Mul;
2977 case Operator.Division:
2978 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2979 opcode = OpCodes.Div_Un;
2981 opcode = OpCodes.Div;
2984 case Operator.Modulus:
2985 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2986 opcode = OpCodes.Rem_Un;
2988 opcode = OpCodes.Rem;
2991 case Operator.Addition:
2993 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2994 opcode = OpCodes.Add_Ovf;
2995 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2996 opcode = OpCodes.Add_Ovf_Un;
2998 opcode = OpCodes.Mul;
3000 opcode = OpCodes.Add;
3003 case Operator.Subtraction:
3005 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3006 opcode = OpCodes.Sub_Ovf;
3007 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
3008 opcode = OpCodes.Sub_Ovf_Un;
3010 opcode = OpCodes.Sub;
3012 opcode = OpCodes.Sub;
3015 case Operator.RightShift:
3016 opcode = OpCodes.Shr;
3019 case Operator.LeftShift:
3020 opcode = OpCodes.Shl;
3023 case Operator.Equality:
3024 opcode = OpCodes.Ceq;
3027 case Operator.Inequality:
3028 ec.ig.Emit (OpCodes.Ceq);
3029 ec.ig.Emit (OpCodes.Ldc_I4_0);
3031 opcode = OpCodes.Ceq;
3034 case Operator.LessThan:
3035 opcode = OpCodes.Clt;
3038 case Operator.GreaterThan:
3039 opcode = OpCodes.Cgt;
3042 case Operator.LessThanOrEqual:
3043 ec.ig.Emit (OpCodes.Cgt);
3044 ec.ig.Emit (OpCodes.Ldc_I4_0);
3046 opcode = OpCodes.Ceq;
3049 case Operator.GreaterThanOrEqual:
3050 ec.ig.Emit (OpCodes.Clt);
3051 ec.ig.Emit (OpCodes.Ldc_I4_1);
3053 opcode = OpCodes.Sub;
3056 case Operator.LogicalOr:
3057 case Operator.BitwiseOr:
3058 opcode = OpCodes.Or;
3061 case Operator.LogicalAnd:
3062 case Operator.BitwiseAnd:
3063 opcode = OpCodes.And;
3066 case Operator.ExclusiveOr:
3067 opcode = OpCodes.Xor;
3071 throw new Exception ("This should not happen: Operator = "
3072 + oper.ToString ());
3079 public class Conditional : Expression {
3080 Expression expr, trueExpr, falseExpr;
3083 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3086 this.trueExpr = trueExpr;
3087 this.falseExpr = falseExpr;
3091 public Expression Expr {
3097 public Expression TrueExpr {
3103 public Expression FalseExpr {
3109 public override Expression DoResolve (EmitContext ec)
3111 expr = expr.Resolve (ec);
3113 if (expr.Type != TypeManager.bool_type)
3114 expr = Expression.ConvertImplicitRequired (
3115 ec, expr, TypeManager.bool_type, loc);
3117 trueExpr = trueExpr.Resolve (ec);
3118 falseExpr = falseExpr.Resolve (ec);
3120 if (expr == null || trueExpr == null || falseExpr == null)
3123 if (trueExpr.Type == falseExpr.Type)
3124 type = trueExpr.Type;
3129 // First, if an implicit conversion exists from trueExpr
3130 // to falseExpr, then the result type is of type falseExpr.Type
3132 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
3134 type = falseExpr.Type;
3136 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
3137 type = trueExpr.Type;
3140 Error (173, loc, "The type of the conditional expression can " +
3141 "not be computed because there is no implicit conversion" +
3142 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3143 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3148 eclass = ExprClass.Value;
3152 public override void Emit (EmitContext ec)
3154 ILGenerator ig = ec.ig;
3155 Label false_target = ig.DefineLabel ();
3156 Label end_target = ig.DefineLabel ();
3159 ig.Emit (OpCodes.Brfalse, false_target);
3161 ig.Emit (OpCodes.Br, end_target);
3162 ig.MarkLabel (false_target);
3163 falseExpr.Emit (ec);
3164 ig.MarkLabel (end_target);
3169 // SimpleName expressions are initially formed of a single
3170 // word and it only happens at the beginning of the expression.
3172 // The expression will try to be bound to a Field, a Method
3173 // group or a Property. If those fail we pass the name to our
3174 // caller and the SimpleName is compounded to perform a type
3175 // lookup. The idea behind this process is that we want to avoid
3176 // creating a namespace map from the assemblies, as that requires
3177 // the GetExportedTypes function to be called and a hashtable to
3178 // be constructed which reduces startup time. If later we find
3179 // that this is slower, we should create a `NamespaceExpr' expression
3180 // that fully participates in the resolution process.
3182 // For example `System.Console.WriteLine' is decomposed into
3183 // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3185 // The first SimpleName wont produce a match on its own, so it will
3187 // MemberAccess (SimpleName ("System.Console"), "WriteLine").
3189 // System.Console will produce a TypeExpr match.
3191 // The downside of this is that we might be hitting `LookupType' too many
3192 // times with this scheme.
3194 public class SimpleName : Expression {
3195 public readonly string Name;
3196 public readonly Location Location;
3198 public SimpleName (string name, Location l)
3204 public static void Error120 (Location l, string name)
3208 "An object reference is required " +
3209 "for the non-static field `"+name+"'");
3213 // Checks whether we are trying to access an instance
3214 // property, method or field from a static body.
3216 Expression MemberStaticCheck (Expression e)
3218 if (e is FieldExpr){
3219 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3222 Error120 (Location, Name);
3225 } else if (e is MethodGroupExpr){
3226 MethodGroupExpr mg = (MethodGroupExpr) e;
3228 if (!mg.RemoveInstanceMethods ()){
3229 Error120 (Location, mg.Methods [0].Name);
3233 } else if (e is PropertyExpr){
3234 if (!((PropertyExpr) e).IsStatic){
3235 Error120 (Location, Name);
3244 // 7.5.2: Simple Names.
3246 // Local Variables and Parameters are handled at
3247 // parse time, so they never occur as SimpleNames.
3249 public override Expression DoResolve (EmitContext ec)
3254 // Stage 1: Performed by the parser (binding to local or parameters).
3258 // Stage 2: Lookup members
3260 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
3263 // Stage 3: Lookup symbol in the various namespaces.
3267 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
3268 return new TypeExpr (t);
3271 // Stage 3 part b: Lookup up if we are an alias to a type
3274 // Since we are cheating: we only do the Alias lookup for
3275 // namespaces if the name does not include any dots in it
3278 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
3279 // using NamespaceExprs (dunno how that fixes the alias
3280 // per-file though).
3282 // No match, maybe our parent can compose us
3283 // into something meaningful.
3288 // Step 2, continues here.
3292 if (e is FieldExpr){
3293 FieldExpr fe = (FieldExpr) e;
3295 if (!fe.FieldInfo.IsStatic)
3296 fe.InstanceExpression = new This (Location.Null);
3300 return MemberStaticCheck (e);
3305 public override void Emit (EmitContext ec)
3308 // If this is ever reached, then we failed to
3309 // find the name as a namespace
3312 Error (103, Location, "The name `" + Name +
3313 "' does not exist in the class `" +
3314 ec.TypeContainer.Name + "'");
3318 public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
3319 public readonly string Name;
3320 public readonly Block Block;
3322 VariableInfo variable_info;
3324 public LocalVariableReference (Block block, string name)
3328 eclass = ExprClass.Variable;
3331 public VariableInfo VariableInfo {
3333 if (variable_info == null)
3334 variable_info = Block.GetVariableInfo (Name);
3335 return variable_info;
3339 public override Expression DoResolve (EmitContext ec)
3341 VariableInfo vi = VariableInfo;
3343 type = vi.VariableType;
3347 public override void Emit (EmitContext ec)
3349 VariableInfo vi = VariableInfo;
3350 ILGenerator ig = ec.ig;
3357 ig.Emit (OpCodes.Ldloc_0);
3361 ig.Emit (OpCodes.Ldloc_1);
3365 ig.Emit (OpCodes.Ldloc_2);
3369 ig.Emit (OpCodes.Ldloc_3);
3374 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3376 ig.Emit (OpCodes.Ldloc, idx);
3381 public static void Store (ILGenerator ig, int idx)
3385 ig.Emit (OpCodes.Stloc_0);
3389 ig.Emit (OpCodes.Stloc_1);
3393 ig.Emit (OpCodes.Stloc_2);
3397 ig.Emit (OpCodes.Stloc_3);
3402 ig.Emit (OpCodes.Stloc_S, (byte) idx);
3404 ig.Emit (OpCodes.Stloc, idx);
3409 public void Store (EmitContext ec)
3411 ILGenerator ig = ec.ig;
3412 VariableInfo vi = VariableInfo;
3416 // Funny seems the above generates optimal code for us, but
3417 // seems to take too long to generate what we need.
3418 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3423 public void AddressOf (EmitContext ec)
3425 VariableInfo vi = VariableInfo;
3432 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3434 ec.ig.Emit (OpCodes.Ldloca, idx);
3438 public class ParameterReference : Expression, IStackStore, IMemoryLocation {
3439 public readonly Parameters Pars;
3440 public readonly String Name;
3441 public readonly int Idx;
3444 public ParameterReference (Parameters pars, int idx, string name)
3449 eclass = ExprClass.Variable;
3452 public override Expression DoResolve (EmitContext ec)
3454 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
3465 public override void Emit (EmitContext ec)
3468 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3470 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3473 public void Store (EmitContext ec)
3476 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3478 ec.ig.Emit (OpCodes.Starg, arg_idx);
3482 public void AddressOf (EmitContext ec)
3485 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3487 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3492 // Used for arguments to New(), Invocation()
3494 public class Argument {
3501 public readonly AType Type;
3502 public Expression expr;
3504 public Argument (Expression expr, AType type)
3510 public Expression Expr {
3520 public bool Resolve (EmitContext ec)
3522 expr = expr.Resolve (ec);
3524 return expr != null;
3527 public void Emit (EmitContext ec)
3534 // Invocation of methods or delegates.
3536 public class Invocation : ExpressionStatement {
3537 public readonly ArrayList Arguments;
3538 public readonly Location Location;
3541 MethodBase method = null;
3543 static Hashtable method_parameter_cache;
3545 static Invocation ()
3547 method_parameter_cache = new Hashtable ();
3551 // arguments is an ArrayList, but we do not want to typecast,
3552 // as it might be null.
3554 // FIXME: only allow expr to be a method invocation or a
3555 // delegate invocation (7.5.5)
3557 public Invocation (Expression expr, ArrayList arguments, Location l)
3560 Arguments = arguments;
3564 public Expression Expr {
3571 // Returns the Parameters (a ParameterData interface) for the
3574 public static ParameterData GetParameterData (MethodBase mb)
3576 object pd = method_parameter_cache [mb];
3580 return (ParameterData) pd;
3583 ip = TypeContainer.LookupParametersByBuilder (mb);
3585 method_parameter_cache [mb] = ip;
3587 return (ParameterData) ip;
3589 ParameterInfo [] pi = mb.GetParameters ();
3590 ReflectionParameters rp = new ReflectionParameters (pi);
3591 method_parameter_cache [mb] = rp;
3593 return (ParameterData) rp;
3598 // Tells whether a user defined conversion from Type `from' to
3599 // Type `to' exists.
3601 // FIXME: we could implement a cache here.
3603 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
3605 // Locate user-defined implicit operators
3609 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
3612 MethodGroupExpr me = (MethodGroupExpr) mg;
3614 for (int i = me.Methods.Length; i > 0;) {
3616 MethodBase mb = me.Methods [i];
3617 ParameterData pd = GetParameterData (mb);
3619 if (from == pd.ParameterType (0))
3624 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
3627 MethodGroupExpr me = (MethodGroupExpr) mg;
3629 for (int i = me.Methods.Length; i > 0;) {
3631 MethodBase mb = me.Methods [i];
3632 MethodInfo mi = (MethodInfo) mb;
3634 if (mi.ReturnType == to)
3643 // Determines "better conversion" as specified in 7.4.2.3
3644 // Returns : 1 if a->p is better
3645 // 0 if a->q or neither is better
3647 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
3650 Type argument_type = a.Expr.Type;
3651 Expression argument_expr = a.Expr;
3653 if (argument_type == null)
3654 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3659 if (argument_type == p)
3662 if (argument_type == q)
3666 // Now probe whether an implicit constant expression conversion
3669 // An implicit constant expression conversion permits the following
3672 // * A constant-expression of type `int' can be converted to type
3673 // sbyte, byute, short, ushort, uint, ulong provided the value of
3674 // of the expression is withing the range of the destination type.
3676 // * A constant-expression of type long can be converted to type
3677 // ulong, provided the value of the constant expression is not negative
3679 // FIXME: Note that this assumes that constant folding has
3680 // taken place. We dont do constant folding yet.
3683 if (argument_expr is IntLiteral){
3684 IntLiteral ei = (IntLiteral) argument_expr;
3685 int value = ei.Value;
3687 if (p == TypeManager.sbyte_type){
3688 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3690 } else if (p == TypeManager.byte_type){
3691 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3693 } else if (p == TypeManager.short_type){
3694 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3696 } else if (p == TypeManager.ushort_type){
3697 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3699 } else if (p == TypeManager.uint32_type){
3701 // we can optimize this case: a positive int32
3702 // always fits on a uint32
3706 } else if (p == TypeManager.uint64_type){
3708 // we can optimize this case: a positive int32
3709 // always fits on a uint64
3714 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3715 LongLiteral ll = (LongLiteral) argument_expr;
3717 if (p == TypeManager.uint64_type){
3728 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
3730 tmp = ConvertImplicit (ec, argument_expr, p, loc);
3739 if (ConversionExists (ec, p, q, loc) == true &&
3740 ConversionExists (ec, q, p, loc) == false)
3743 if (p == TypeManager.sbyte_type)
3744 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3745 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3748 if (p == TypeManager.short_type)
3749 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3750 q == TypeManager.uint64_type)
3753 if (p == TypeManager.int32_type)
3754 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3757 if (p == TypeManager.int64_type)
3758 if (q == TypeManager.uint64_type)
3765 // Determines "Better function" and returns an integer indicating :
3766 // 0 if candidate ain't better
3767 // 1 if candidate is better than the current best match
3769 static int BetterFunction (EmitContext ec, ArrayList args,
3770 MethodBase candidate, MethodBase best,
3771 bool use_standard, Location loc)
3773 ParameterData candidate_pd = GetParameterData (candidate);
3774 ParameterData best_pd;
3780 argument_count = args.Count;
3782 if (candidate_pd.Count == 0 && argument_count == 0)
3786 if (candidate_pd.Count == argument_count) {
3788 for (int j = argument_count; j > 0;) {
3791 Argument a = (Argument) args [j];
3793 x = BetterConversion (
3794 ec, a, candidate_pd.ParameterType (j), null,
3810 best_pd = GetParameterData (best);
3812 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3813 int rating1 = 0, rating2 = 0;
3815 for (int j = argument_count; j > 0;) {
3819 Argument a = (Argument) args [j];
3821 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
3822 best_pd.ParameterType (j), use_standard, loc);
3823 y = BetterConversion (ec, a, best_pd.ParameterType (j),
3824 candidate_pd.ParameterType (j), use_standard,
3831 if (rating1 > rating2)
3840 public static string FullMethodDesc (MethodBase mb)
3842 StringBuilder sb = new StringBuilder (mb.Name);
3843 ParameterData pd = GetParameterData (mb);
3845 int count = pd.Count;
3848 for (int i = count; i > 0; ) {
3851 sb.Append (TypeManager.CSharpName (pd.ParameterType (count - i - 1)));
3857 return sb.ToString ();
3860 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3862 MemberInfo [] miset;
3863 MethodGroupExpr union;
3865 if (mg1 != null && mg2 != null) {
3867 MethodGroupExpr left_set = null, right_set = null;
3868 int length1 = 0, length2 = 0;
3870 left_set = (MethodGroupExpr) mg1;
3871 length1 = left_set.Methods.Length;
3873 right_set = (MethodGroupExpr) mg2;
3874 length2 = right_set.Methods.Length;
3876 ArrayList common = new ArrayList ();
3878 for (int i = 0; i < left_set.Methods.Length; i++) {
3879 for (int j = 0; j < right_set.Methods.Length; j++) {
3880 if (left_set.Methods [i] == right_set.Methods [j])
3881 common.Add (left_set.Methods [i]);
3885 miset = new MemberInfo [length1 + length2 - common.Count];
3887 left_set.Methods.CopyTo (miset, 0);
3891 for (int j = 0; j < right_set.Methods.Length; j++)
3892 if (!common.Contains (right_set.Methods [j]))
3893 miset [length1 + k++] = right_set.Methods [j];
3895 union = new MethodGroupExpr (miset);
3899 } else if (mg1 == null && mg2 != null) {
3901 MethodGroupExpr me = (MethodGroupExpr) mg2;
3903 miset = new MemberInfo [me.Methods.Length];
3904 me.Methods.CopyTo (miset, 0);
3906 union = new MethodGroupExpr (miset);
3910 } else if (mg2 == null && mg1 != null) {
3912 MethodGroupExpr me = (MethodGroupExpr) mg1;
3914 miset = new MemberInfo [me.Methods.Length];
3915 me.Methods.CopyTo (miset, 0);
3917 union = new MethodGroupExpr (miset);
3926 // Find the Applicable Function Members (7.4.2.1)
3928 // me: Method Group expression with the members to select.
3929 // it might contain constructors or methods (or anything
3930 // that maps to a method).
3932 // Arguments: ArrayList containing resolved Argument objects.
3934 // loc: The location if we want an error to be reported, or a Null
3935 // location for "probing" purposes.
3937 // inside_user_defined: controls whether OverloadResolve should use the
3938 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
3940 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3941 // that is the best match of me on Arguments.
3944 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3945 ArrayList Arguments, Location loc,
3948 ArrayList afm = new ArrayList ();
3949 int best_match_idx = -1;
3950 MethodBase method = null;
3953 for (int i = me.Methods.Length; i > 0; ){
3955 MethodBase candidate = me.Methods [i];
3958 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
3964 method = me.Methods [best_match_idx];
3968 if (Arguments == null)
3971 argument_count = Arguments.Count;
3975 // Now we see if we can at least find a method with the same number of arguments
3976 // and then try doing implicit conversion on the arguments
3977 if (best_match_idx == -1) {
3979 for (int i = me.Methods.Length; i > 0;) {
3981 MethodBase mb = me.Methods [i];
3982 pd = GetParameterData (mb);
3984 if (pd.Count == argument_count) {
3986 method = me.Methods [best_match_idx];
3997 // And now convert implicitly, each argument to the required type
3999 pd = GetParameterData (method);
4001 for (int j = argument_count; j > 0;) {
4003 Argument a = (Argument) Arguments [j];
4004 Expression a_expr = a.Expr;
4005 Type parameter_type = pd.ParameterType (j);
4007 if (a_expr.Type != parameter_type){
4011 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
4014 conv = ConvertImplicit (ec, a_expr, parameter_type,
4018 if (!Location.IsNull (loc)) {
4020 "The best overloaded match for method '" + FullMethodDesc (method) +
4021 "' has some invalid arguments");
4023 "Argument " + (j+1) +
4024 ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
4025 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
4030 // Update the argument with the implicit conversion
4040 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4041 ArrayList Arguments, Location loc)
4043 return OverloadResolve (ec, me, Arguments, loc, false);
4046 public override Expression DoResolve (EmitContext ec)
4049 // First, resolve the expression that is used to
4050 // trigger the invocation
4052 expr = expr.Resolve (ec);
4056 if (!(expr is MethodGroupExpr)) {
4057 Type expr_type = expr.Type;
4059 if (expr_type != null){
4060 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
4062 return (new DelegateInvocation (
4063 this.expr, Arguments, Location)).Resolve (ec);
4067 if (!(expr is MethodGroupExpr)){
4068 report118 (Location, this.expr, "method group");
4073 // Next, evaluate all the expressions in the argument list
4075 if (Arguments != null){
4076 for (int i = Arguments.Count; i > 0;){
4078 Argument a = (Argument) Arguments [i];
4080 if (!a.Resolve (ec))
4085 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
4088 if (method == null){
4089 Error (-6, Location,
4090 "Could not find any applicable function for this argument list");
4094 if (method is MethodInfo)
4095 type = ((MethodInfo)method).ReturnType;
4097 eclass = ExprClass.Value;
4101 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
4105 if (Arguments != null)
4106 top = Arguments.Count;
4110 for (int i = 0; i < top; i++){
4111 Argument a = (Argument) Arguments [i];
4117 public static void EmitCall (EmitContext ec,
4118 bool is_static, Expression instance_expr,
4119 MethodBase method, ArrayList Arguments)
4121 ILGenerator ig = ec.ig;
4122 bool struct_call = false;
4126 // If this is ourselves, push "this"
4128 if (instance_expr == null){
4129 ig.Emit (OpCodes.Ldarg_0);
4132 // Push the instance expression
4134 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
4139 // If the expression implements IMemoryLocation, then
4140 // we can optimize and use AddressOf on the
4143 // If not we have to use some temporary storage for
4145 if (instance_expr is IMemoryLocation)
4146 ((IMemoryLocation) instance_expr).AddressOf (ec);
4148 Type t = instance_expr.Type;
4150 instance_expr.Emit (ec);
4151 LocalBuilder temp = ec.GetTemporaryStorage (t);
4152 ig.Emit (OpCodes.Stloc, temp);
4153 ig.Emit (OpCodes.Ldloca, temp);
4156 instance_expr.Emit (ec);
4160 if (Arguments != null)
4161 EmitArguments (ec, method, Arguments);
4163 if (is_static || struct_call){
4164 if (method is MethodInfo)
4165 ig.Emit (OpCodes.Call, (MethodInfo) method);
4167 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4169 if (method is MethodInfo)
4170 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
4172 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
4176 public override void Emit (EmitContext ec)
4178 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
4179 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
4182 public override void EmitStatement (EmitContext ec)
4187 // Pop the return value if there is one
4189 if (method is MethodInfo){
4190 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
4191 ec.ig.Emit (OpCodes.Pop);
4196 public class New : ExpressionStatement {
4203 public readonly NType NewType;
4204 public readonly ArrayList Arguments;
4205 public readonly string RequestedType;
4207 // These are for the case when we have an array
4208 public readonly string Rank;
4209 public readonly ArrayList Initializers;
4212 MethodBase method = null;
4215 // If set, the new expression is for a value_target, and
4216 // we will not leave anything on the stack.
4218 Expression value_target;
4220 public New (string requested_type, ArrayList arguments, Location loc)
4222 RequestedType = requested_type;
4223 Arguments = arguments;
4224 NewType = NType.Object;
4228 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
4230 RequestedType = requested_type;
4232 Initializers = initializers;
4233 NewType = NType.Array;
4236 Arguments = new ArrayList ();
4238 foreach (Expression e in exprs)
4239 Arguments.Add (new Argument (e, Argument.AType.Expression));
4243 public static string FormLookupType (string base_type, int idx_count, string rank)
4245 StringBuilder sb = new StringBuilder (base_type);
4250 for (int i = 1; i < idx_count; i++)
4254 return sb.ToString ();
4257 public Expression ValueTypeVariable {
4259 return value_target;
4263 value_target = value;
4267 public override Expression DoResolve (EmitContext ec)
4269 if (NewType == NType.Object) {
4271 type = ec.TypeContainer.LookupType (RequestedType, false);
4276 bool IsDelegate = TypeManager.IsDelegateType (type);
4279 return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
4283 ml = MemberLookup (ec, type, ".ctor", false,
4284 MemberTypes.Constructor, AllBindingsFlags, Location);
4286 bool is_struct = false;
4287 is_struct = type.IsSubclassOf (TypeManager.value_type);
4289 if (! (ml is MethodGroupExpr)){
4291 report118 (Location, ml, "method group");
4297 if (Arguments != null){
4298 for (int i = Arguments.Count; i > 0;){
4300 Argument a = (Argument) Arguments [i];
4302 if (!a.Resolve (ec))
4307 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4308 Arguments, Location);
4312 if (method == null && !is_struct) {
4313 Error (-6, Location,
4314 "New invocation: Can not find a constructor for " +
4315 "this argument list");
4319 eclass = ExprClass.Value;
4324 if (NewType == NType.Array) {
4325 throw new Exception ("Finish array creation");
4332 // This DoEmit can be invoked in two contexts:
4333 // * As a mechanism that will leave a value on the stack (new object)
4334 // * As one that wont (init struct)
4336 // You can control whether a value is required on the stack by passing
4337 // need_value_on_stack. The code *might* leave a value on the stack
4338 // so it must be popped manually
4340 // Returns whether a value is left on the stack
4342 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4344 if (method == null){
4345 IMemoryLocation ml = (IMemoryLocation) value_target;
4349 Invocation.EmitArguments (ec, method, Arguments);
4350 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4355 // It must be a value type, sanity check
4357 if (value_target != null){
4358 ec.ig.Emit (OpCodes.Initobj, type);
4360 if (need_value_on_stack){
4361 value_target.Emit (ec);
4367 throw new Exception ("No method and no value type");
4370 public override void Emit (EmitContext ec)
4375 public override void EmitStatement (EmitContext ec)
4377 if (DoEmit (ec, false))
4378 ec.ig.Emit (OpCodes.Pop);
4383 // Represents the `this' construct
4385 public class This : Expression, IStackStore, IMemoryLocation {
4388 public This (Location loc)
4393 public override Expression DoResolve (EmitContext ec)
4395 eclass = ExprClass.Variable;
4396 type = ec.TypeContainer.TypeBuilder;
4399 Report.Error (26, loc,
4400 "Keyword this not valid in static code");
4407 public Expression DoResolveLValue (EmitContext ec)
4411 if (ec.TypeContainer is Class){
4412 Report.Error (1604, loc, "Cannot assign to `this'");
4419 public override void Emit (EmitContext ec)
4421 ec.ig.Emit (OpCodes.Ldarg_0);
4424 public void Store (EmitContext ec)
4426 ec.ig.Emit (OpCodes.Starg, 0);
4429 public void AddressOf (EmitContext ec)
4431 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4436 // Implements the typeof operator
4438 public class TypeOf : Expression {
4439 public readonly string QueriedType;
4442 public TypeOf (string queried_type)
4444 QueriedType = queried_type;
4447 public override Expression DoResolve (EmitContext ec)
4449 typearg = ec.TypeContainer.LookupType (QueriedType, false);
4451 if (typearg == null)
4454 type = TypeManager.type_type;
4455 eclass = ExprClass.Type;
4459 public override void Emit (EmitContext ec)
4461 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4462 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4466 public class SizeOf : Expression {
4467 public readonly string QueriedType;
4469 public SizeOf (string queried_type)
4471 this.QueriedType = queried_type;
4474 public override Expression DoResolve (EmitContext ec)
4476 // FIXME: Implement;
4477 throw new Exception ("Unimplemented");
4481 public override void Emit (EmitContext ec)
4483 throw new Exception ("Implement me");
4487 public class MemberAccess : Expression {
4488 public readonly string Identifier;
4490 Expression member_lookup;
4493 public MemberAccess (Expression expr, string id, Location l)
4500 public Expression Expr {
4506 void error176 (Location loc, string name)
4508 Report.Error (176, loc, "Static member `" +
4509 name + "' cannot be accessed " +
4510 "with an instance reference, qualify with a " +
4511 "type name instead");
4514 public override Expression DoResolve (EmitContext ec)
4517 // We are the sole users of ResolveWithSimpleName (ie, the only
4518 // ones that can cope with it
4520 expr = expr.ResolveWithSimpleName (ec);
4525 if (expr is SimpleName){
4526 SimpleName child_expr = (SimpleName) expr;
4528 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4530 return expr.Resolve (ec);
4533 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4535 if (member_lookup == null)
4541 if (member_lookup is MethodGroupExpr){
4542 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4547 if (expr is TypeExpr){
4548 if (!mg.RemoveInstanceMethods ()){
4549 SimpleName.Error120 (loc, mg.Methods [0].Name);
4553 return member_lookup;
4557 // Instance.MethodGroup
4559 if (!mg.RemoveStaticMethods ()){
4560 error176 (loc, mg.Methods [0].Name);
4564 mg.InstanceExpression = expr;
4566 return member_lookup;
4569 if (member_lookup is FieldExpr){
4570 FieldExpr fe = (FieldExpr) member_lookup;
4572 if (expr is TypeExpr){
4573 if (!fe.FieldInfo.IsStatic){
4574 error176 (loc, fe.FieldInfo.Name);
4577 return member_lookup;
4579 if (fe.FieldInfo.IsStatic){
4580 error176 (loc, fe.FieldInfo.Name);
4583 fe.InstanceExpression = expr;
4589 if (member_lookup is PropertyExpr){
4590 PropertyExpr pe = (PropertyExpr) member_lookup;
4592 if (expr is TypeExpr){
4594 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4600 error176 (loc, pe.PropertyInfo.Name);
4603 pe.InstanceExpression = expr;
4609 Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
4610 Environment.Exit (0);
4614 public override void Emit (EmitContext ec)
4616 throw new Exception ("Should not happen I think");
4622 // Fully resolved expression that evaluates to a type
4624 public class TypeExpr : Expression {
4625 public TypeExpr (Type t)
4628 eclass = ExprClass.Type;
4631 override public Expression DoResolve (EmitContext ec)
4636 override public void Emit (EmitContext ec)
4638 throw new Exception ("Implement me");
4643 // MethodGroup Expression.
4645 // This is a fully resolved expression that evaluates to a type
4647 public class MethodGroupExpr : Expression {
4648 public MethodBase [] Methods;
4649 Expression instance_expression = null;
4651 public MethodGroupExpr (MemberInfo [] mi)
4653 Methods = new MethodBase [mi.Length];
4654 mi.CopyTo (Methods, 0);
4655 eclass = ExprClass.MethodGroup;
4658 public MethodGroupExpr (ArrayList l)
4660 Methods = new MethodBase [l.Count];
4662 l.CopyTo (Methods, 0);
4663 eclass = ExprClass.MethodGroup;
4667 // `A method group may have associated an instance expression'
4669 public Expression InstanceExpression {
4671 return instance_expression;
4675 instance_expression = value;
4679 override public Expression DoResolve (EmitContext ec)
4684 override public void Emit (EmitContext ec)
4686 throw new Exception ("This should never be reached");
4689 bool RemoveMethods (bool keep_static)
4691 ArrayList smethods = new ArrayList ();
4692 int top = Methods.Length;
4695 for (i = 0; i < top; i++){
4696 MethodBase mb = Methods [i];
4698 if (mb.IsStatic == keep_static)
4702 if (smethods.Count == 0)
4705 Methods = new MethodBase [smethods.Count];
4706 smethods.CopyTo (Methods, 0);
4712 // Removes any instance methods from the MethodGroup, returns
4713 // false if the resulting set is empty.
4715 public bool RemoveInstanceMethods ()
4717 return RemoveMethods (true);
4721 // Removes any static methods from the MethodGroup, returns
4722 // false if the resulting set is empty.
4724 public bool RemoveStaticMethods ()
4726 return RemoveMethods (false);
4731 // Fully resolved expression that evaluates to a Field
4733 public class FieldExpr : Expression, IStackStore, IMemoryLocation {
4734 public readonly FieldInfo FieldInfo;
4735 public Expression InstanceExpression;
4738 public FieldExpr (FieldInfo fi, Location l)
4741 eclass = ExprClass.Variable;
4742 type = fi.FieldType;
4746 override public Expression DoResolve (EmitContext ec)
4748 if (!FieldInfo.IsStatic){
4749 if (InstanceExpression == null){
4750 throw new Exception ("non-static FieldExpr without instance var\n" +
4751 "You have to assign the Instance variable\n" +
4752 "Of the FieldExpr to set this\n");
4755 InstanceExpression = InstanceExpression.Resolve (ec);
4756 if (InstanceExpression == null)
4763 public Expression DoResolveLValue (EmitContext ec)
4765 if (!FieldInfo.IsInitOnly)
4769 // InitOnly fields can only be assigned in constructors
4772 if (ec.IsConstructor)
4775 Report.Error (191, loc,
4776 "Readonly field can not be assigned outside " +
4777 "of constructor or variable initializer");
4782 override public void Emit (EmitContext ec)
4784 ILGenerator ig = ec.ig;
4786 if (FieldInfo.IsStatic)
4787 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4789 InstanceExpression.Emit (ec);
4791 ig.Emit (OpCodes.Ldfld, FieldInfo);
4795 public void Store (EmitContext ec)
4797 if (FieldInfo.IsStatic)
4798 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
4800 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
4803 public void AddressOf (EmitContext ec)
4805 if (FieldInfo.IsStatic)
4806 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
4808 InstanceExpression.Emit (ec);
4809 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
4815 // Expression that evaluates to a Property. The Assign class
4816 // might set the `Value' expression if we are in an assignment.
4818 // This is not an LValue because we need to re-write the expression, we
4819 // can not take data from the stack and store it.
4821 public class PropertyExpr : ExpressionStatement, IAssignMethod {
4822 public readonly PropertyInfo PropertyInfo;
4823 public readonly bool IsStatic;
4824 MethodInfo [] Accessors;
4827 Expression instance_expr;
4829 public PropertyExpr (PropertyInfo pi, Location l)
4832 eclass = ExprClass.PropertyAccess;
4835 Accessors = TypeManager.GetAccessors (pi);
4837 if (Accessors != null)
4838 for (int i = 0; i < Accessors.Length; i++){
4839 if (Accessors [i] != null)
4840 if (Accessors [i].IsStatic)
4844 Accessors = new MethodInfo [2];
4846 type = pi.PropertyType;
4850 // The instance expression associated with this expression
4852 public Expression InstanceExpression {
4854 instance_expr = value;
4858 return instance_expr;
4862 public bool VerifyAssignable ()
4864 if (!PropertyInfo.CanWrite){
4865 Report.Error (200, loc,
4866 "The property `" + PropertyInfo.Name +
4867 "' can not be assigned to, as it has not set accessor");
4874 override public Expression DoResolve (EmitContext ec)
4876 if (!PropertyInfo.CanRead){
4877 Report.Error (154, loc,
4878 "The property `" + PropertyInfo.Name +
4879 "' can not be used in " +
4880 "this context because it lacks a get accessor");
4887 override public void Emit (EmitContext ec)
4889 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
4894 // Implements the IAssignMethod interface for assignments
4896 public void EmitAssign (EmitContext ec, Expression source)
4898 Argument arg = new Argument (source, Argument.AType.Expression);
4899 ArrayList args = new ArrayList ();
4902 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
4905 override public void EmitStatement (EmitContext ec)
4908 ec.ig.Emit (OpCodes.Pop);
4913 // Fully resolved expression that evaluates to a Expression
4915 public class EventExpr : Expression {
4916 public readonly EventInfo EventInfo;
4919 public EventExpr (EventInfo ei, Location loc)
4923 eclass = ExprClass.EventAccess;
4926 override public Expression DoResolve (EmitContext ec)
4928 // We are born in resolved state.
4932 override public void Emit (EmitContext ec)
4934 throw new Exception ("Implement me");
4935 // FIXME: Implement.
4939 public class CheckedExpr : Expression {
4941 public Expression Expr;
4943 public CheckedExpr (Expression e)
4948 public override Expression DoResolve (EmitContext ec)
4950 Expr = Expr.Resolve (ec);
4955 eclass = Expr.ExprClass;
4960 public override void Emit (EmitContext ec)
4962 bool last_check = ec.CheckState;
4964 ec.CheckState = true;
4966 ec.CheckState = last_check;
4971 public class UnCheckedExpr : Expression {
4973 public Expression Expr;
4975 public UnCheckedExpr (Expression e)
4980 public override Expression DoResolve (EmitContext ec)
4982 Expr = Expr.Resolve (ec);
4987 eclass = Expr.ExprClass;
4992 public override void Emit (EmitContext ec)
4994 bool last_check = ec.CheckState;
4996 ec.CheckState = false;
4998 ec.CheckState = last_check;
5003 public class ElementAccess : Expression {
5005 public ArrayList Arguments;
5006 public Expression Expr;
5007 public Location loc;
5009 public ElementAccess (Expression e, ArrayList e_list, Location l)
5013 Arguments = new ArrayList ();
5014 foreach (Expression tmp in e_list)
5015 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
5020 bool CommonResolve (EmitContext ec)
5022 Expr = Expr.Resolve (ec);
5027 if (Arguments == null)
5030 for (int i = Arguments.Count; i > 0;){
5032 Argument a = (Argument) Arguments [i];
5034 if (!a.Resolve (ec))
5041 public override Expression DoResolve (EmitContext ec)
5043 if (!CommonResolve (ec))
5047 // We perform some simple tests, and then to "split" the emit and store
5048 // code we create an instance of a different class, and return that.
5050 // I am experimenting with this pattern.
5052 if (Expr.Type == TypeManager.array_type)
5053 return (new ArrayAccess (this)).Resolve (ec);
5055 return (new IndexerAccess (this)).Resolve (ec);
5058 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5060 if (!CommonResolve (ec))
5063 if (Expr.Type == TypeManager.array_type)
5064 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
5066 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
5069 public override void Emit (EmitContext ec)
5071 throw new Exception ("Should never be reached");
5075 public class ArrayAccess : Expression, IStackStore {
5077 // Points to our "data" repository
5081 public ArrayAccess (ElementAccess ea_data)
5084 eclass = ExprClass.Variable;
5087 // FIXME: Figure out the type here
5091 Expression CommonResolve (EmitContext ec)
5096 public override Expression DoResolve (EmitContext ec)
5098 if (ea.Expr.ExprClass != ExprClass.Variable) {
5099 report118 (ea.loc, ea.Expr, "variable");
5103 throw new Exception ("Implement me");
5106 public void Store (EmitContext ec)
5108 throw new Exception ("Implement me !");
5111 public override void Emit (EmitContext ec)
5113 throw new Exception ("Implement me !");
5118 public ArrayList getters, setters;
5119 static Hashtable map;
5123 map = new Hashtable ();
5126 Indexers (MemberInfo [] mi)
5128 foreach (PropertyInfo property in mi){
5129 MethodInfo get, set;
5131 get = property.GetGetMethod (true);
5133 if (getters == null)
5134 getters = new ArrayList ();
5139 set = property.GetSetMethod (true);
5141 if (setters == null)
5142 setters = new ArrayList ();
5148 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
5150 Indexers ix = (Indexers) map [t];
5151 string p_name = TypeManager.IndexerPropertyName (t);
5156 MemberInfo [] mi = tm.FindMembers (
5157 t, MemberTypes.Property,
5158 BindingFlags.Public | BindingFlags.Instance,
5159 Type.FilterName, p_name);
5161 if (mi == null || mi.Length == 0){
5162 Report.Error (21, loc,
5163 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5164 "any indexers defined");
5168 ix = new Indexers (mi);
5175 public class IndexerAccess : Expression, IAssignMethod {
5177 // Points to our "data" repository
5180 MethodInfo get, set;
5182 ArrayList set_arguments;
5184 public IndexerAccess (ElementAccess ea_data)
5187 eclass = ExprClass.Value;
5190 public bool VerifyAssignable (Expression source)
5192 throw new Exception ("Implement me!");
5195 public override Expression DoResolve (EmitContext ec)
5197 Type indexer_type = ea.Expr.Type;
5200 // Step 1: Query for all `Item' *properties*. Notice
5201 // that the actual methods are pointed from here.
5203 // This is a group of properties, piles of them.
5206 ilist = Indexers.GetIndexersForType (
5207 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5209 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5210 get = (MethodInfo) Invocation.OverloadResolve (
5211 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5214 Report.Error (154, ea.loc,
5215 "indexer can not be used in this context, because " +
5216 "it lacks a `get' accessor");
5220 type = get.ReturnType;
5221 eclass = ExprClass.Value;
5225 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5227 Type indexer_type = ea.Expr.Type;
5228 Type right_type = right_side.Type;
5231 ilist = Indexers.GetIndexersForType (
5232 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5234 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5235 set_arguments = (ArrayList) ea.Arguments.Clone ();
5236 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5238 set = (MethodInfo) Invocation.OverloadResolve (
5239 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5243 Report.Error (200, ea.loc,
5244 "indexer X.this [" + TypeManager.CSharpName (right_type) +
5245 "] lacks a `set' accessor");
5249 type = TypeManager.void_type;
5250 eclass = ExprClass.IndexerAccess;
5254 public override void Emit (EmitContext ec)
5256 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5260 // source is ignored, because we already have a copy of it from the
5261 // LValue resolution and we have already constructed a pre-cached
5262 // version of the arguments (ea.set_arguments);
5264 public void EmitAssign (EmitContext ec, Expression source)
5266 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
5270 public class BaseAccess : Expression {
5272 public enum BaseAccessType {
5277 public readonly BaseAccessType BAType;
5278 public readonly string Member;
5279 public readonly ArrayList Arguments;
5281 public BaseAccess (BaseAccessType t, string member, ArrayList args)
5289 public override Expression DoResolve (EmitContext ec)
5291 // FIXME: Implement;
5292 throw new Exception ("Unimplemented");
5296 public override void Emit (EmitContext ec)
5298 throw new Exception ("Unimplemented");
5303 // This class exists solely to pass the Type around and to be a dummy
5304 // that can be passed to the conversion functions (this is used by
5305 // foreach implementation to typecast the object return value from
5306 // get_Current into the proper type. All code has been generated and
5307 // we only care about the side effect conversions to be performed
5310 public class EmptyExpression : Expression {
5311 public EmptyExpression ()
5313 type = TypeManager.object_type;
5314 eclass = ExprClass.Value;
5317 public override Expression DoResolve (EmitContext ec)
5322 public override void Emit (EmitContext ec)
5324 // nothing, as we only exist to not do anything.
5328 public class UserCast : Expression {
5332 public UserCast (MethodInfo method, Expression source)
5334 this.method = method;
5335 this.source = source;
5336 type = method.ReturnType;
5337 eclass = ExprClass.Value;
5340 public override Expression DoResolve (EmitContext ec)
5343 // We are born fully resolved
5348 public override void Emit (EmitContext ec)
5350 ILGenerator ig = ec.ig;
5354 if (method is MethodInfo)
5355 ig.Emit (OpCodes.Call, (MethodInfo) method);
5357 ig.Emit (OpCodes.Call, (ConstructorInfo) method);