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, 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, 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;
3353 bool ref_parameter = ec.RefOrOutParameter;
3357 if (!ref_parameter) {
3361 ig.Emit (OpCodes.Ldloc_0);
3365 ig.Emit (OpCodes.Ldloc_1);
3369 ig.Emit (OpCodes.Ldloc_2);
3373 ig.Emit (OpCodes.Ldloc_3);
3378 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3380 ig.Emit (OpCodes.Ldloc, idx);
3387 public static void Store (ILGenerator ig, int idx)
3391 ig.Emit (OpCodes.Stloc_0);
3395 ig.Emit (OpCodes.Stloc_1);
3399 ig.Emit (OpCodes.Stloc_2);
3403 ig.Emit (OpCodes.Stloc_3);
3408 ig.Emit (OpCodes.Stloc_S, (byte) idx);
3410 ig.Emit (OpCodes.Stloc, idx);
3415 public void Store (EmitContext ec)
3417 ILGenerator ig = ec.ig;
3418 VariableInfo vi = VariableInfo;
3422 // Funny seems the above generates optimal code for us, but
3423 // seems to take too long to generate what we need.
3424 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3429 public void AddressOf (EmitContext ec)
3431 VariableInfo vi = VariableInfo;
3438 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3440 ec.ig.Emit (OpCodes.Ldloca, idx);
3444 public class ParameterReference : Expression, IStackStore, IMemoryLocation {
3445 public readonly Parameters Pars;
3446 public readonly String Name;
3447 public readonly int Idx;
3450 public ParameterReference (Parameters pars, int idx, string name)
3455 eclass = ExprClass.Variable;
3458 public override Expression DoResolve (EmitContext ec)
3460 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
3471 public override void Emit (EmitContext ec)
3474 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3476 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3479 public void Store (EmitContext ec)
3482 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3484 ec.ig.Emit (OpCodes.Starg, arg_idx);
3488 public void AddressOf (EmitContext ec)
3491 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3493 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3498 // Used for arguments to New(), Invocation()
3500 public class Argument {
3507 public readonly AType ArgType;
3508 public Expression expr;
3510 public Argument (Expression expr, AType type)
3513 this.ArgType = type;
3516 public Expression Expr {
3532 public Parameter.Modifier GetParameterModifier ()
3534 if (ArgType == AType.Ref)
3535 return Parameter.Modifier.REF;
3537 if (ArgType == AType.Out)
3538 return Parameter.Modifier.OUT;
3540 return Parameter.Modifier.NONE;
3543 public static string FullDesc (Argument a)
3545 StringBuilder sb = new StringBuilder ();
3547 if (a.ArgType == AType.Ref)
3550 if (a.ArgType == AType.Out)
3553 sb.Append (TypeManager.CSharpName (a.Expr.Type));
3555 return sb.ToString ();
3558 public bool Resolve (EmitContext ec)
3560 expr = expr.Resolve (ec);
3562 return expr != null;
3565 public void Emit (EmitContext ec)
3567 if (ArgType == AType.Ref || ArgType == AType.Out)
3568 ec.RefOrOutParameter = true;
3571 ec.RefOrOutParameter = false;
3576 // Invocation of methods or delegates.
3578 public class Invocation : ExpressionStatement {
3579 public readonly ArrayList Arguments;
3580 public readonly Location Location;
3583 MethodBase method = null;
3585 static Hashtable method_parameter_cache;
3587 static Invocation ()
3589 method_parameter_cache = new Hashtable ();
3593 // arguments is an ArrayList, but we do not want to typecast,
3594 // as it might be null.
3596 // FIXME: only allow expr to be a method invocation or a
3597 // delegate invocation (7.5.5)
3599 public Invocation (Expression expr, ArrayList arguments, Location l)
3602 Arguments = arguments;
3606 public Expression Expr {
3613 // Returns the Parameters (a ParameterData interface) for the
3616 public static ParameterData GetParameterData (MethodBase mb)
3618 object pd = method_parameter_cache [mb];
3622 return (ParameterData) pd;
3625 ip = TypeContainer.LookupParametersByBuilder (mb);
3627 method_parameter_cache [mb] = ip;
3629 return (ParameterData) ip;
3631 ParameterInfo [] pi = mb.GetParameters ();
3632 ReflectionParameters rp = new ReflectionParameters (pi);
3633 method_parameter_cache [mb] = rp;
3635 return (ParameterData) rp;
3640 // Tells whether a user defined conversion from Type `from' to
3641 // Type `to' exists.
3643 // FIXME: we could implement a cache here.
3645 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
3647 // Locate user-defined implicit operators
3651 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
3654 MethodGroupExpr me = (MethodGroupExpr) mg;
3656 for (int i = me.Methods.Length; i > 0;) {
3658 MethodBase mb = me.Methods [i];
3659 ParameterData pd = GetParameterData (mb);
3661 if (from == pd.ParameterType (0))
3666 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
3669 MethodGroupExpr me = (MethodGroupExpr) mg;
3671 for (int i = me.Methods.Length; i > 0;) {
3673 MethodBase mb = me.Methods [i];
3674 MethodInfo mi = (MethodInfo) mb;
3676 if (mi.ReturnType == to)
3685 // Determines "better conversion" as specified in 7.4.2.3
3686 // Returns : 1 if a->p is better
3687 // 0 if a->q or neither is better
3689 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
3692 Type argument_type = a.Type;
3693 Expression argument_expr = a.Expr;
3695 if (argument_type == null)
3696 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3701 if (argument_type == p)
3704 if (argument_type == q)
3708 // Now probe whether an implicit constant expression conversion
3711 // An implicit constant expression conversion permits the following
3714 // * A constant-expression of type `int' can be converted to type
3715 // sbyte, byute, short, ushort, uint, ulong provided the value of
3716 // of the expression is withing the range of the destination type.
3718 // * A constant-expression of type long can be converted to type
3719 // ulong, provided the value of the constant expression is not negative
3721 // FIXME: Note that this assumes that constant folding has
3722 // taken place. We dont do constant folding yet.
3725 if (argument_expr is IntLiteral){
3726 IntLiteral ei = (IntLiteral) argument_expr;
3727 int value = ei.Value;
3729 if (p == TypeManager.sbyte_type){
3730 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3732 } else if (p == TypeManager.byte_type){
3733 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3735 } else if (p == TypeManager.short_type){
3736 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3738 } else if (p == TypeManager.ushort_type){
3739 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3741 } else if (p == TypeManager.uint32_type){
3743 // we can optimize this case: a positive int32
3744 // always fits on a uint32
3748 } else if (p == TypeManager.uint64_type){
3750 // we can optimize this case: a positive int32
3751 // always fits on a uint64
3756 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3757 LongLiteral ll = (LongLiteral) argument_expr;
3759 if (p == TypeManager.uint64_type){
3770 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
3772 tmp = ConvertImplicit (ec, argument_expr, p, loc);
3781 if (ConversionExists (ec, p, q, loc) == true &&
3782 ConversionExists (ec, q, p, loc) == false)
3785 if (p == TypeManager.sbyte_type)
3786 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3787 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3790 if (p == TypeManager.short_type)
3791 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3792 q == TypeManager.uint64_type)
3795 if (p == TypeManager.int32_type)
3796 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3799 if (p == TypeManager.int64_type)
3800 if (q == TypeManager.uint64_type)
3807 // Determines "Better function" and returns an integer indicating :
3808 // 0 if candidate ain't better
3809 // 1 if candidate is better than the current best match
3811 static int BetterFunction (EmitContext ec, ArrayList args,
3812 MethodBase candidate, MethodBase best,
3813 bool use_standard, Location loc)
3815 ParameterData candidate_pd = GetParameterData (candidate);
3816 ParameterData best_pd;
3822 argument_count = args.Count;
3824 if (candidate_pd.Count == 0 && argument_count == 0)
3828 if (candidate_pd.Count == argument_count) {
3830 for (int j = argument_count; j > 0;) {
3833 Argument a = (Argument) args [j];
3835 x = BetterConversion (
3836 ec, a, candidate_pd.ParameterType (j), null,
3852 best_pd = GetParameterData (best);
3854 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3855 int rating1 = 0, rating2 = 0;
3857 for (int j = argument_count; j > 0;) {
3861 Argument a = (Argument) args [j];
3863 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
3864 best_pd.ParameterType (j), use_standard, loc);
3865 y = BetterConversion (ec, a, best_pd.ParameterType (j),
3866 candidate_pd.ParameterType (j), use_standard,
3873 if (rating1 > rating2)
3882 public static string FullMethodDesc (MethodBase mb)
3884 StringBuilder sb = new StringBuilder (mb.Name);
3885 ParameterData pd = GetParameterData (mb);
3887 int count = pd.Count;
3890 for (int i = count; i > 0; ) {
3893 sb.Append (pd.ParameterDesc (count - i - 1));
3899 return sb.ToString ();
3902 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3904 MemberInfo [] miset;
3905 MethodGroupExpr union;
3907 if (mg1 != null && mg2 != null) {
3909 MethodGroupExpr left_set = null, right_set = null;
3910 int length1 = 0, length2 = 0;
3912 left_set = (MethodGroupExpr) mg1;
3913 length1 = left_set.Methods.Length;
3915 right_set = (MethodGroupExpr) mg2;
3916 length2 = right_set.Methods.Length;
3918 ArrayList common = new ArrayList ();
3920 for (int i = 0; i < left_set.Methods.Length; i++) {
3921 for (int j = 0; j < right_set.Methods.Length; j++) {
3922 if (left_set.Methods [i] == right_set.Methods [j])
3923 common.Add (left_set.Methods [i]);
3927 miset = new MemberInfo [length1 + length2 - common.Count];
3929 left_set.Methods.CopyTo (miset, 0);
3933 for (int j = 0; j < right_set.Methods.Length; j++)
3934 if (!common.Contains (right_set.Methods [j]))
3935 miset [length1 + k++] = right_set.Methods [j];
3937 union = new MethodGroupExpr (miset);
3941 } else if (mg1 == null && mg2 != null) {
3943 MethodGroupExpr me = (MethodGroupExpr) mg2;
3945 miset = new MemberInfo [me.Methods.Length];
3946 me.Methods.CopyTo (miset, 0);
3948 union = new MethodGroupExpr (miset);
3952 } else if (mg2 == null && mg1 != null) {
3954 MethodGroupExpr me = (MethodGroupExpr) mg1;
3956 miset = new MemberInfo [me.Methods.Length];
3957 me.Methods.CopyTo (miset, 0);
3959 union = new MethodGroupExpr (miset);
3968 // Determines if the candidate method is applicable (section 14.4.2.1)
3969 // to the given set of arguments
3971 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
3975 if (arguments == null)
3978 arg_count = arguments.Count;
3980 ParameterData pd = GetParameterData (candidate);
3982 int pd_count = pd.Count;
3984 if (arg_count != pd.Count)
3987 for (int i = arg_count; i > 0; ) {
3990 Argument a = (Argument) arguments [i];
3992 Parameter.Modifier a_mod = a.GetParameterModifier ();
3993 Parameter.Modifier p_mod = pd.ParameterModifier (i);
3995 if (a_mod == p_mod) {
3997 if (a_mod == Parameter.Modifier.NONE)
3998 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
4001 if (a_mod == Parameter.Modifier.REF ||
4002 a_mod == Parameter.Modifier.OUT)
4003 if (pd.ParameterType (i) != a.Type)
4015 // Find the Applicable Function Members (7.4.2.1)
4017 // me: Method Group expression with the members to select.
4018 // it might contain constructors or methods (or anything
4019 // that maps to a method).
4021 // Arguments: ArrayList containing resolved Argument objects.
4023 // loc: The location if we want an error to be reported, or a Null
4024 // location for "probing" purposes.
4026 // inside_user_defined: controls whether OverloadResolve should use the
4027 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
4029 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4030 // that is the best match of me on Arguments.
4033 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4034 ArrayList Arguments, Location loc,
4037 ArrayList afm = new ArrayList ();
4038 int best_match_idx = -1;
4039 MethodBase method = null;
4042 for (int i = me.Methods.Length; i > 0; ){
4044 MethodBase candidate = me.Methods [i];
4047 // Check if candidate is applicable (section 14.4.2.1)
4048 if (!IsApplicable (Arguments, candidate))
4051 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
4057 method = me.Methods [best_match_idx];
4061 if (Arguments == null)
4064 argument_count = Arguments.Count;
4068 // Now we see if we can at least find a method with the same number of arguments
4069 // and then try doing implicit conversion on the arguments
4070 if (best_match_idx == -1) {
4072 for (int i = me.Methods.Length; i > 0;) {
4074 MethodBase mb = me.Methods [i];
4075 pd = GetParameterData (mb);
4077 if (pd.Count == argument_count) {
4079 method = me.Methods [best_match_idx];
4090 // And now convert implicitly, each argument to the required type
4092 pd = GetParameterData (method);
4094 for (int j = 0; j < argument_count; j++) {
4096 Argument a = (Argument) Arguments [j];
4097 Expression a_expr = a.Expr;
4098 Type parameter_type = pd.ParameterType (j);
4100 if (a.Type != parameter_type){
4104 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
4107 conv = ConvertImplicit (ec, a_expr, parameter_type,
4111 if (!Location.IsNull (loc)) {
4113 "The best overloaded match for method '" + FullMethodDesc (method)+
4114 "' has some invalid arguments");
4116 "Argument " + (j+1) +
4117 ": Cannot convert from '" + Argument.FullDesc (a)
4118 + "' to '" + pd.ParameterDesc (j) + "'");
4126 // Update the argument with the implicit conversion
4132 if (a.GetParameterModifier () != pd.ParameterModifier (j)) {
4133 if (!Location.IsNull (loc)) {
4135 "The best overloaded match for method '" + FullMethodDesc (method)+
4136 "' has some invalid arguments");
4138 "Argument " + (j+1) +
4139 ": Cannot convert from '" + Argument.FullDesc (a)
4140 + "' to '" + pd.ParameterDesc (j) + "'");
4149 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4150 ArrayList Arguments, Location loc)
4152 return OverloadResolve (ec, me, Arguments, loc, false);
4155 public override Expression DoResolve (EmitContext ec)
4158 // First, resolve the expression that is used to
4159 // trigger the invocation
4161 expr = expr.Resolve (ec);
4165 if (!(expr is MethodGroupExpr)) {
4166 Type expr_type = expr.Type;
4168 if (expr_type != null){
4169 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
4171 return (new DelegateInvocation (
4172 this.expr, Arguments, Location)).Resolve (ec);
4176 if (!(expr is MethodGroupExpr)){
4177 report118 (Location, this.expr, "method group");
4182 // Next, evaluate all the expressions in the argument list
4184 if (Arguments != null){
4185 for (int i = Arguments.Count; i > 0;){
4187 Argument a = (Argument) Arguments [i];
4189 if (!a.Resolve (ec))
4194 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
4197 if (method == null){
4198 Error (-6, Location,
4199 "Could not find any applicable function for this argument list");
4203 if (method is MethodInfo)
4204 type = ((MethodInfo)method).ReturnType;
4206 eclass = ExprClass.Value;
4210 public static void EmitArguments (EmitContext ec, ArrayList Arguments)
4214 if (Arguments != null)
4215 top = Arguments.Count;
4219 for (int i = 0; i < top; i++){
4220 Argument a = (Argument) Arguments [i];
4226 public static void EmitCall (EmitContext ec,
4227 bool is_static, Expression instance_expr,
4228 MethodBase method, ArrayList Arguments)
4230 ILGenerator ig = ec.ig;
4231 bool struct_call = false;
4235 // If this is ourselves, push "this"
4237 if (instance_expr == null){
4238 ig.Emit (OpCodes.Ldarg_0);
4241 // Push the instance expression
4243 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
4248 // If the expression implements IMemoryLocation, then
4249 // we can optimize and use AddressOf on the
4252 // If not we have to use some temporary storage for
4254 if (instance_expr is IMemoryLocation)
4255 ((IMemoryLocation) instance_expr).AddressOf (ec);
4257 Type t = instance_expr.Type;
4259 instance_expr.Emit (ec);
4260 LocalBuilder temp = ec.GetTemporaryStorage (t);
4261 ig.Emit (OpCodes.Stloc, temp);
4262 ig.Emit (OpCodes.Ldloca, temp);
4265 instance_expr.Emit (ec);
4269 if (Arguments != null)
4270 EmitArguments (ec, Arguments);
4272 if (is_static || struct_call){
4273 if (method is MethodInfo)
4274 ig.Emit (OpCodes.Call, (MethodInfo) method);
4276 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4278 if (method is MethodInfo)
4279 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
4281 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
4285 public override void Emit (EmitContext ec)
4287 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
4288 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
4291 public override void EmitStatement (EmitContext ec)
4296 // Pop the return value if there is one
4298 if (method is MethodInfo){
4299 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
4300 ec.ig.Emit (OpCodes.Pop);
4305 public class New : ExpressionStatement {
4306 public readonly ArrayList Arguments;
4307 public readonly string RequestedType;
4310 MethodBase method = null;
4313 // If set, the new expression is for a value_target, and
4314 // we will not leave anything on the stack.
4316 Expression value_target;
4318 public New (string requested_type, ArrayList arguments, Location loc)
4320 RequestedType = requested_type;
4321 Arguments = arguments;
4325 public Expression ValueTypeVariable {
4327 return value_target;
4331 value_target = value;
4335 public override Expression DoResolve (EmitContext ec)
4337 type = ec.TypeContainer.LookupType (RequestedType, false);
4342 bool IsDelegate = TypeManager.IsDelegateType (type);
4345 return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
4349 ml = MemberLookup (ec, type, ".ctor", false,
4350 MemberTypes.Constructor, AllBindingsFlags, Location);
4352 bool is_struct = false;
4353 is_struct = type.IsSubclassOf (TypeManager.value_type);
4355 if (! (ml is MethodGroupExpr)){
4357 report118 (Location, ml, "method group");
4363 if (Arguments != null){
4364 for (int i = Arguments.Count; i > 0;){
4366 Argument a = (Argument) Arguments [i];
4368 if (!a.Resolve (ec))
4373 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4374 Arguments, Location);
4377 if (method == null && !is_struct) {
4378 Error (-6, Location,
4379 "New invocation: Can not find a constructor for " +
4380 "this argument list");
4384 eclass = ExprClass.Value;
4389 // This DoEmit can be invoked in two contexts:
4390 // * As a mechanism that will leave a value on the stack (new object)
4391 // * As one that wont (init struct)
4393 // You can control whether a value is required on the stack by passing
4394 // need_value_on_stack. The code *might* leave a value on the stack
4395 // so it must be popped manually
4397 // Returns whether a value is left on the stack
4399 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4401 if (method == null){
4402 IMemoryLocation ml = (IMemoryLocation) value_target;
4406 Invocation.EmitArguments (ec, Arguments);
4407 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4412 // It must be a value type, sanity check
4414 if (value_target != null){
4415 ec.ig.Emit (OpCodes.Initobj, type);
4417 if (need_value_on_stack){
4418 value_target.Emit (ec);
4424 throw new Exception ("No method and no value type");
4427 public override void Emit (EmitContext ec)
4432 public override void EmitStatement (EmitContext ec)
4434 if (DoEmit (ec, false))
4435 ec.ig.Emit (OpCodes.Pop);
4440 // Represents an array creation expression.
4444 // There are two possible scenarios here: one is an array creation
4445 // expression that specifies the dimensions and optionally the
4446 // initialization data
4448 public class ArrayCreation : ExpressionStatement {
4450 string RequestedType;
4452 ArrayList Initializers;
4454 ArrayList Arguments;
4456 MethodBase method = null;
4457 Type array_element_type;
4458 bool IsOneDimensional = false;
4460 bool IsBuiltinType = false;
4462 public ArrayCreation (string requested_type, ArrayList exprs,
4463 string rank, ArrayList initializers, Location l)
4465 RequestedType = requested_type;
4467 Initializers = initializers;
4470 Arguments = new ArrayList ();
4472 foreach (Expression e in exprs)
4473 Arguments.Add (new Argument (e, Argument.AType.Expression));
4477 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
4479 RequestedType = requested_type;
4481 Initializers = initializers;
4485 public static string FormArrayType (string base_type, int idx_count, string rank)
4487 StringBuilder sb = new StringBuilder (base_type);
4492 for (int i = 1; i < idx_count; i++)
4496 return sb.ToString ();
4499 public static string FormElementType (string base_type, int idx_count, string rank)
4501 StringBuilder sb = new StringBuilder (base_type);
4504 for (int i = 1; i < idx_count; i++)
4510 string val = sb.ToString ();
4512 return val.Substring (0, val.LastIndexOf ("["));
4516 public override Expression DoResolve (EmitContext ec)
4520 if (Arguments == null)
4523 arg_count = Arguments.Count;
4525 string array_type = FormArrayType (RequestedType, arg_count, Rank);
4527 string element_type = FormElementType (RequestedType, arg_count, Rank);
4529 type = ec.TypeContainer.LookupType (array_type, false);
4531 array_element_type = ec.TypeContainer.LookupType (element_type, false);
4536 if (arg_count == 1) {
4537 IsOneDimensional = true;
4538 eclass = ExprClass.Value;
4542 IsBuiltinType = TypeManager.IsBuiltinType (type);
4544 if (IsBuiltinType) {
4548 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
4549 AllBindingsFlags, Location);
4551 if (!(ml is MethodGroupExpr)){
4552 report118 (Location, ml, "method group");
4557 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
4558 "this argument list");
4562 if (Arguments != null) {
4563 for (int i = arg_count; i > 0;){
4565 Argument a = (Argument) Arguments [i];
4567 if (!a.Resolve (ec))
4572 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, Location);
4574 if (method == null) {
4575 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
4576 "this argument list");
4580 eclass = ExprClass.Value;
4585 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
4587 ArrayList args = new ArrayList ();
4588 if (Arguments != null){
4589 for (int i = arg_count; i > 0;){
4591 Argument a = (Argument) Arguments [i];
4593 if (!a.Resolve (ec))
4600 Type [] arg_types = null;
4603 arg_types = new Type [args.Count];
4605 args.CopyTo (arg_types, 0);
4607 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
4610 if (method == null) {
4611 Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
4612 "this argument list");
4616 eclass = ExprClass.Value;
4622 public override void Emit (EmitContext ec)
4624 if (IsOneDimensional) {
4625 Invocation.EmitArguments (ec, Arguments);
4626 ec.ig.Emit (OpCodes.Newarr, array_element_type);
4629 Invocation.EmitArguments (ec, Arguments);
4632 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4634 ec.ig.Emit (OpCodes.Newobj, (MethodInfo) method);
4639 public override void EmitStatement (EmitContext ec)
4642 ec.ig.Emit (OpCodes.Pop);
4648 // Represents the `this' construct
4650 public class This : Expression, IStackStore, IMemoryLocation {
4653 public This (Location loc)
4658 public override Expression DoResolve (EmitContext ec)
4660 eclass = ExprClass.Variable;
4661 type = ec.TypeContainer.TypeBuilder;
4664 Report.Error (26, loc,
4665 "Keyword this not valid in static code");
4672 public Expression DoResolveLValue (EmitContext ec)
4676 if (ec.TypeContainer is Class){
4677 Report.Error (1604, loc, "Cannot assign to `this'");
4684 public override void Emit (EmitContext ec)
4686 ec.ig.Emit (OpCodes.Ldarg_0);
4689 public void Store (EmitContext ec)
4691 ec.ig.Emit (OpCodes.Starg, 0);
4694 public void AddressOf (EmitContext ec)
4696 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4701 // Implements the typeof operator
4703 public class TypeOf : Expression {
4704 public readonly string QueriedType;
4707 public TypeOf (string queried_type)
4709 QueriedType = queried_type;
4712 public override Expression DoResolve (EmitContext ec)
4714 typearg = ec.TypeContainer.LookupType (QueriedType, false);
4716 if (typearg == null)
4719 type = TypeManager.type_type;
4720 eclass = ExprClass.Type;
4724 public override void Emit (EmitContext ec)
4726 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4727 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4731 public class SizeOf : Expression {
4732 public readonly string QueriedType;
4734 public SizeOf (string queried_type)
4736 this.QueriedType = queried_type;
4739 public override Expression DoResolve (EmitContext ec)
4741 // FIXME: Implement;
4742 throw new Exception ("Unimplemented");
4746 public override void Emit (EmitContext ec)
4748 throw new Exception ("Implement me");
4752 public class MemberAccess : Expression {
4753 public readonly string Identifier;
4755 Expression member_lookup;
4758 public MemberAccess (Expression expr, string id, Location l)
4765 public Expression Expr {
4771 void error176 (Location loc, string name)
4773 Report.Error (176, loc, "Static member `" +
4774 name + "' cannot be accessed " +
4775 "with an instance reference, qualify with a " +
4776 "type name instead");
4779 public override Expression DoResolve (EmitContext ec)
4782 // We are the sole users of ResolveWithSimpleName (ie, the only
4783 // ones that can cope with it
4785 expr = expr.ResolveWithSimpleName (ec);
4790 if (expr is SimpleName){
4791 SimpleName child_expr = (SimpleName) expr;
4793 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4795 return expr.Resolve (ec);
4798 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4800 if (member_lookup == null)
4806 if (member_lookup is MethodGroupExpr){
4807 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4812 if (expr is TypeExpr){
4813 if (!mg.RemoveInstanceMethods ()){
4814 SimpleName.Error120 (loc, mg.Methods [0].Name);
4818 return member_lookup;
4822 // Instance.MethodGroup
4824 if (!mg.RemoveStaticMethods ()){
4825 error176 (loc, mg.Methods [0].Name);
4829 mg.InstanceExpression = expr;
4831 return member_lookup;
4834 if (member_lookup is FieldExpr){
4835 FieldExpr fe = (FieldExpr) member_lookup;
4837 if (expr is TypeExpr){
4838 if (!fe.FieldInfo.IsStatic){
4839 error176 (loc, fe.FieldInfo.Name);
4842 return member_lookup;
4844 if (fe.FieldInfo.IsStatic){
4845 error176 (loc, fe.FieldInfo.Name);
4848 fe.InstanceExpression = expr;
4854 if (member_lookup is PropertyExpr){
4855 PropertyExpr pe = (PropertyExpr) member_lookup;
4857 if (expr is TypeExpr){
4859 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4865 error176 (loc, pe.PropertyInfo.Name);
4868 pe.InstanceExpression = expr;
4874 Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
4875 Environment.Exit (0);
4879 public override void Emit (EmitContext ec)
4881 throw new Exception ("Should not happen I think");
4887 // Fully resolved expression that evaluates to a type
4889 public class TypeExpr : Expression {
4890 public TypeExpr (Type t)
4893 eclass = ExprClass.Type;
4896 override public Expression DoResolve (EmitContext ec)
4901 override public void Emit (EmitContext ec)
4903 throw new Exception ("Implement me");
4908 // MethodGroup Expression.
4910 // This is a fully resolved expression that evaluates to a type
4912 public class MethodGroupExpr : Expression {
4913 public MethodBase [] Methods;
4914 Expression instance_expression = null;
4916 public MethodGroupExpr (MemberInfo [] mi)
4918 Methods = new MethodBase [mi.Length];
4919 mi.CopyTo (Methods, 0);
4920 eclass = ExprClass.MethodGroup;
4923 public MethodGroupExpr (ArrayList l)
4925 Methods = new MethodBase [l.Count];
4927 l.CopyTo (Methods, 0);
4928 eclass = ExprClass.MethodGroup;
4932 // `A method group may have associated an instance expression'
4934 public Expression InstanceExpression {
4936 return instance_expression;
4940 instance_expression = value;
4944 override public Expression DoResolve (EmitContext ec)
4949 override public void Emit (EmitContext ec)
4951 throw new Exception ("This should never be reached");
4954 bool RemoveMethods (bool keep_static)
4956 ArrayList smethods = new ArrayList ();
4957 int top = Methods.Length;
4960 for (i = 0; i < top; i++){
4961 MethodBase mb = Methods [i];
4963 if (mb.IsStatic == keep_static)
4967 if (smethods.Count == 0)
4970 Methods = new MethodBase [smethods.Count];
4971 smethods.CopyTo (Methods, 0);
4977 // Removes any instance methods from the MethodGroup, returns
4978 // false if the resulting set is empty.
4980 public bool RemoveInstanceMethods ()
4982 return RemoveMethods (true);
4986 // Removes any static methods from the MethodGroup, returns
4987 // false if the resulting set is empty.
4989 public bool RemoveStaticMethods ()
4991 return RemoveMethods (false);
4996 // Fully resolved expression that evaluates to a Field
4998 public class FieldExpr : Expression, IStackStore, IMemoryLocation {
4999 public readonly FieldInfo FieldInfo;
5000 public Expression InstanceExpression;
5003 public FieldExpr (FieldInfo fi, Location l)
5006 eclass = ExprClass.Variable;
5007 type = fi.FieldType;
5011 override public Expression DoResolve (EmitContext ec)
5013 if (!FieldInfo.IsStatic){
5014 if (InstanceExpression == null){
5015 throw new Exception ("non-static FieldExpr without instance var\n" +
5016 "You have to assign the Instance variable\n" +
5017 "Of the FieldExpr to set this\n");
5020 InstanceExpression = InstanceExpression.Resolve (ec);
5021 if (InstanceExpression == null)
5028 public Expression DoResolveLValue (EmitContext ec)
5030 if (!FieldInfo.IsInitOnly)
5034 // InitOnly fields can only be assigned in constructors
5037 if (ec.IsConstructor)
5040 Report.Error (191, loc,
5041 "Readonly field can not be assigned outside " +
5042 "of constructor or variable initializer");
5047 override public void Emit (EmitContext ec)
5049 ILGenerator ig = ec.ig;
5051 if (FieldInfo.IsStatic)
5052 ig.Emit (OpCodes.Ldsfld, FieldInfo);
5054 InstanceExpression.Emit (ec);
5056 ig.Emit (OpCodes.Ldfld, FieldInfo);
5060 public void Store (EmitContext ec)
5062 if (FieldInfo.IsStatic)
5063 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
5065 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
5068 public void AddressOf (EmitContext ec)
5070 if (FieldInfo.IsStatic)
5071 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
5073 InstanceExpression.Emit (ec);
5074 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
5080 // Expression that evaluates to a Property. The Assign class
5081 // might set the `Value' expression if we are in an assignment.
5083 // This is not an LValue because we need to re-write the expression, we
5084 // can not take data from the stack and store it.
5086 public class PropertyExpr : ExpressionStatement, IAssignMethod {
5087 public readonly PropertyInfo PropertyInfo;
5088 public readonly bool IsStatic;
5089 MethodInfo [] Accessors;
5092 Expression instance_expr;
5094 public PropertyExpr (PropertyInfo pi, Location l)
5097 eclass = ExprClass.PropertyAccess;
5100 Accessors = TypeManager.GetAccessors (pi);
5102 if (Accessors != null)
5103 for (int i = 0; i < Accessors.Length; i++){
5104 if (Accessors [i] != null)
5105 if (Accessors [i].IsStatic)
5109 Accessors = new MethodInfo [2];
5111 type = pi.PropertyType;
5115 // The instance expression associated with this expression
5117 public Expression InstanceExpression {
5119 instance_expr = value;
5123 return instance_expr;
5127 public bool VerifyAssignable ()
5129 if (!PropertyInfo.CanWrite){
5130 Report.Error (200, loc,
5131 "The property `" + PropertyInfo.Name +
5132 "' can not be assigned to, as it has not set accessor");
5139 override public Expression DoResolve (EmitContext ec)
5141 if (!PropertyInfo.CanRead){
5142 Report.Error (154, loc,
5143 "The property `" + PropertyInfo.Name +
5144 "' can not be used in " +
5145 "this context because it lacks a get accessor");
5152 override public void Emit (EmitContext ec)
5154 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
5159 // Implements the IAssignMethod interface for assignments
5161 public void EmitAssign (EmitContext ec, Expression source)
5163 Argument arg = new Argument (source, Argument.AType.Expression);
5164 ArrayList args = new ArrayList ();
5167 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
5170 override public void EmitStatement (EmitContext ec)
5173 ec.ig.Emit (OpCodes.Pop);
5178 // Fully resolved expression that evaluates to a Expression
5180 public class EventExpr : Expression {
5181 public readonly EventInfo EventInfo;
5184 public EventExpr (EventInfo ei, Location loc)
5188 eclass = ExprClass.EventAccess;
5191 override public Expression DoResolve (EmitContext ec)
5193 // We are born in resolved state.
5197 override public void Emit (EmitContext ec)
5199 throw new Exception ("Implement me");
5200 // FIXME: Implement.
5204 public class CheckedExpr : Expression {
5206 public Expression Expr;
5208 public CheckedExpr (Expression e)
5213 public override Expression DoResolve (EmitContext ec)
5215 Expr = Expr.Resolve (ec);
5220 eclass = Expr.ExprClass;
5225 public override void Emit (EmitContext ec)
5227 bool last_check = ec.CheckState;
5229 ec.CheckState = true;
5231 ec.CheckState = last_check;
5236 public class UnCheckedExpr : Expression {
5238 public Expression Expr;
5240 public UnCheckedExpr (Expression e)
5245 public override Expression DoResolve (EmitContext ec)
5247 Expr = Expr.Resolve (ec);
5252 eclass = Expr.ExprClass;
5257 public override void Emit (EmitContext ec)
5259 bool last_check = ec.CheckState;
5261 ec.CheckState = false;
5263 ec.CheckState = last_check;
5268 public class ElementAccess : Expression {
5270 public ArrayList Arguments;
5271 public Expression Expr;
5272 public Location loc;
5274 public ElementAccess (Expression e, ArrayList e_list, Location l)
5278 Arguments = new ArrayList ();
5279 foreach (Expression tmp in e_list)
5280 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
5285 bool CommonResolve (EmitContext ec)
5287 Expr = Expr.Resolve (ec);
5292 if (Arguments == null)
5295 for (int i = Arguments.Count; i > 0;){
5297 Argument a = (Argument) Arguments [i];
5299 if (!a.Resolve (ec))
5306 public override Expression DoResolve (EmitContext ec)
5308 if (!CommonResolve (ec))
5312 // We perform some simple tests, and then to "split" the emit and store
5313 // code we create an instance of a different class, and return that.
5315 // I am experimenting with this pattern.
5317 if (Expr.Type == TypeManager.array_type)
5318 return (new ArrayAccess (this)).Resolve (ec);
5320 return (new IndexerAccess (this)).Resolve (ec);
5323 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5325 if (!CommonResolve (ec))
5328 if (Expr.Type == TypeManager.array_type)
5329 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
5331 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
5334 public override void Emit (EmitContext ec)
5336 throw new Exception ("Should never be reached");
5340 public class ArrayAccess : Expression, IStackStore {
5342 // Points to our "data" repository
5346 public ArrayAccess (ElementAccess ea_data)
5349 eclass = ExprClass.Variable;
5352 // FIXME: Figure out the type here
5356 Expression CommonResolve (EmitContext ec)
5361 public override Expression DoResolve (EmitContext ec)
5363 if (ea.Expr.ExprClass != ExprClass.Variable) {
5364 report118 (ea.loc, ea.Expr, "variable");
5368 throw new Exception ("Implement me");
5371 public void Store (EmitContext ec)
5373 throw new Exception ("Implement me !");
5376 public override void Emit (EmitContext ec)
5378 throw new Exception ("Implement me !");
5383 public ArrayList getters, setters;
5384 static Hashtable map;
5388 map = new Hashtable ();
5391 Indexers (MemberInfo [] mi)
5393 foreach (PropertyInfo property in mi){
5394 MethodInfo get, set;
5396 get = property.GetGetMethod (true);
5398 if (getters == null)
5399 getters = new ArrayList ();
5404 set = property.GetSetMethod (true);
5406 if (setters == null)
5407 setters = new ArrayList ();
5413 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
5415 Indexers ix = (Indexers) map [t];
5416 string p_name = TypeManager.IndexerPropertyName (t);
5421 MemberInfo [] mi = tm.FindMembers (
5422 t, MemberTypes.Property,
5423 BindingFlags.Public | BindingFlags.Instance,
5424 Type.FilterName, p_name);
5426 if (mi == null || mi.Length == 0){
5427 Report.Error (21, loc,
5428 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5429 "any indexers defined");
5433 ix = new Indexers (mi);
5440 public class IndexerAccess : Expression, IAssignMethod {
5442 // Points to our "data" repository
5445 MethodInfo get, set;
5447 ArrayList set_arguments;
5449 public IndexerAccess (ElementAccess ea_data)
5452 eclass = ExprClass.Value;
5455 public bool VerifyAssignable (Expression source)
5457 throw new Exception ("Implement me!");
5460 public override Expression DoResolve (EmitContext ec)
5462 Type indexer_type = ea.Expr.Type;
5465 // Step 1: Query for all `Item' *properties*. Notice
5466 // that the actual methods are pointed from here.
5468 // This is a group of properties, piles of them.
5471 ilist = Indexers.GetIndexersForType (
5472 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5474 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5475 get = (MethodInfo) Invocation.OverloadResolve (
5476 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5479 Report.Error (154, ea.loc,
5480 "indexer can not be used in this context, because " +
5481 "it lacks a `get' accessor");
5485 type = get.ReturnType;
5486 eclass = ExprClass.Value;
5490 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5492 Type indexer_type = ea.Expr.Type;
5493 Type right_type = right_side.Type;
5496 ilist = Indexers.GetIndexersForType (
5497 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5499 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5500 set_arguments = (ArrayList) ea.Arguments.Clone ();
5501 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5503 set = (MethodInfo) Invocation.OverloadResolve (
5504 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5508 Report.Error (200, ea.loc,
5509 "indexer X.this [" + TypeManager.CSharpName (right_type) +
5510 "] lacks a `set' accessor");
5514 type = TypeManager.void_type;
5515 eclass = ExprClass.IndexerAccess;
5519 public override void Emit (EmitContext ec)
5521 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5525 // source is ignored, because we already have a copy of it from the
5526 // LValue resolution and we have already constructed a pre-cached
5527 // version of the arguments (ea.set_arguments);
5529 public void EmitAssign (EmitContext ec, Expression source)
5531 Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
5535 public class BaseAccess : Expression {
5537 public enum BaseAccessType {
5542 public readonly BaseAccessType BAType;
5543 public readonly string Member;
5544 public readonly ArrayList Arguments;
5546 public BaseAccess (BaseAccessType t, string member, ArrayList args)
5554 public override Expression DoResolve (EmitContext ec)
5556 // FIXME: Implement;
5557 throw new Exception ("Unimplemented");
5561 public override void Emit (EmitContext ec)
5563 throw new Exception ("Unimplemented");
5568 // This class exists solely to pass the Type around and to be a dummy
5569 // that can be passed to the conversion functions (this is used by
5570 // foreach implementation to typecast the object return value from
5571 // get_Current into the proper type. All code has been generated and
5572 // we only care about the side effect conversions to be performed
5575 public class EmptyExpression : Expression {
5576 public EmptyExpression ()
5578 type = TypeManager.object_type;
5579 eclass = ExprClass.Value;
5582 public override Expression DoResolve (EmitContext ec)
5587 public override void Emit (EmitContext ec)
5589 // nothing, as we only exist to not do anything.
5593 public class UserCast : Expression {
5597 public UserCast (MethodInfo method, Expression source)
5599 this.method = method;
5600 this.source = source;
5601 type = method.ReturnType;
5602 eclass = ExprClass.Value;
5605 public override Expression DoResolve (EmitContext ec)
5608 // We are born fully resolved
5613 public override void Emit (EmitContext ec)
5615 ILGenerator ig = ec.ig;
5619 if (method is MethodInfo)
5620 ig.Emit (OpCodes.Call, (MethodInfo) method);
5622 ig.Emit (OpCodes.Call, (ConstructorInfo) method);