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);
166 if (e.ExprClass == ExprClass.Invalid)
167 throw new Exception ("Expression " + e +
168 " ExprClass is Invalid after resolve");
170 if (e.ExprClass != ExprClass.MethodGroup)
172 throw new Exception ("Expression " + e +
173 " did not set its type after Resolve");
180 // Currently ResolveLValue wraps DoResolveLValue to perform sanity
181 // checking and assertion checking on what we expect from Resolve
183 public Expression ResolveLValue (EmitContext ec, Expression right_side)
185 Expression e = DoResolveLValue (ec, right_side);
191 if (e.ExprClass == ExprClass.Invalid)
192 throw new Exception ("Expression " + e +
193 " ExprClass is Invalid after resolve");
195 if (e.ExprClass != ExprClass.MethodGroup)
197 throw new Exception ("Expression " + e +
198 " did not set its type after Resolve");
205 // Emits the code for the expression
210 // The Emit method is invoked to generate the code
211 // for the expression.
214 public abstract void Emit (EmitContext ec);
217 // Protected constructor. Only derivate types should
218 // be able to be created
221 protected Expression ()
223 eclass = ExprClass.Invalid;
228 // Returns a literalized version of a literal FieldInfo
230 static Expression Literalize (FieldInfo fi)
232 Type t = fi.FieldType;
233 object v = fi.GetValue (fi);
235 if (t == TypeManager.int32_type)
236 return new IntLiteral ((int) v);
237 else if (t == TypeManager.uint32_type)
238 return new UIntLiteral ((uint) v);
239 else if (t == TypeManager.int64_type)
240 return new LongLiteral ((long) v);
241 else if (t == TypeManager.uint64_type)
242 return new ULongLiteral ((ulong) v);
243 else if (t == TypeManager.float_type)
244 return new FloatLiteral ((float) v);
245 else if (t == TypeManager.double_type)
246 return new DoubleLiteral ((double) v);
247 else if (t == TypeManager.string_type)
248 return new StringLiteral ((string) v);
249 else if (t == TypeManager.short_type)
250 return new IntLiteral ((int) ((short)v));
251 else if (t == TypeManager.ushort_type)
252 return new IntLiteral ((int) ((ushort)v));
253 else if (t == TypeManager.sbyte_type)
254 return new IntLiteral ((int) ((sbyte)v));
255 else if (t == TypeManager.byte_type)
256 return new IntLiteral ((int) ((byte)v));
257 else if (t == TypeManager.char_type)
258 return new IntLiteral ((int) ((char)v));
260 throw new Exception ("Unknown type for literal (" + v.GetType () +
261 "), details: " + fi);
265 // Returns a fully formed expression after a MemberLookup
267 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
269 if (mi is EventInfo){
270 return new EventExpr ((EventInfo) mi, loc);
271 } else if (mi is FieldInfo){
272 FieldInfo fi = (FieldInfo) mi;
275 Expression e = Literalize (fi);
280 return new FieldExpr (fi, loc);
281 } else if (mi is PropertyInfo){
282 return new PropertyExpr ((PropertyInfo) mi, loc);
283 } else if (mi is Type)
284 return new TypeExpr ((Type) mi);
290 // FIXME: Probably implement a cache for (t,name,current_access_set)?
292 // FIXME: We need to cope with access permissions here, or this wont
295 // This code could use some optimizations, but we need to do some
296 // measurements. For example, we could use a delegate to `flag' when
297 // something can not any longer be a method-group (because it is something
301 // If the return value is an Array, then it is an array of
304 // If the return value is an MemberInfo, it is anything, but a Method
308 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
309 // the arguments here and have MemberLookup return only the methods that
310 // match the argument count/type, unlike we are doing now (we delay this
313 // This is so we can catch correctly attempts to invoke instance methods
314 // from a static body (scan for error 120 in ResolveSimpleName).
316 public static Expression MemberLookup (EmitContext ec, Type t, string name,
317 bool same_type, MemberTypes mt,
318 BindingFlags bf, Location loc)
321 bf |= BindingFlags.NonPublic;
323 MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
324 t, mt, bf, Type.FilterName, name);
329 // FIXME : How does this wierd case arise ?
333 if (mi.Length == 1 && !(mi [0] is MethodBase))
334 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
336 for (int i = 0; i < mi.Length; i++)
337 if (!(mi [i] is MethodBase)){
338 Error (-5, "Do not know how to reproduce this case: " +
339 "Methods and non-Method with the same name, " +
340 "report this please");
342 for (i = 0; i < mi.Length; i++){
343 Type tt = mi [i].GetType ();
345 Console.WriteLine (i + ": " + mi [i]);
346 while (tt != TypeManager.object_type){
347 Console.WriteLine (tt);
353 return new MethodGroupExpr (mi);
356 public const MemberTypes AllMemberTypes =
357 MemberTypes.Constructor |
361 MemberTypes.NestedType |
362 MemberTypes.Property;
364 public const BindingFlags AllBindingsFlags =
365 BindingFlags.Public |
366 BindingFlags.Static |
367 BindingFlags.Instance;
369 public static Expression MemberLookup (EmitContext ec, Type t, string name,
370 bool same_type, Location loc)
372 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
375 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
377 Type expr_type = expr.Type;
379 if (target_type == TypeManager.object_type) {
380 if (expr_type.IsClass)
381 return new EmptyCast (expr, target_type);
382 if (expr_type.IsValueType)
383 return new BoxedCast (expr);
384 } else if (expr_type.IsSubclassOf (target_type)) {
385 return new EmptyCast (expr, target_type);
387 // from any class-type S to any interface-type T.
388 if (expr_type.IsClass && target_type.IsInterface) {
390 Type [] ifaces = expr_type.GetInterfaces ();
392 for (int i = ifaces.Length; i > 0;) {
394 if (ifaces [i] == target_type)
395 return new EmptyCast (expr, target_type);
401 // from any interface type S to interface-type T.
402 if (expr_type.IsInterface && target_type.IsInterface) {
404 Type [] ifaces = expr_type.GetInterfaces ();
406 for (int i = ifaces.Length; i > 0;) {
408 if (ifaces [i] == target_type)
409 return new EmptyCast (expr, target_type);
415 // from an array-type S to an array-type of type T
416 if (expr_type.IsArray && target_type.IsArray) {
417 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
419 Type expr_element_type = expr_type.GetElementType ();
420 Type target_element_type = target_type.GetElementType ();
422 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
423 if (StandardConversionExists (expr_element_type,
424 target_element_type))
425 return new EmptyCast (expr, target_type);
430 // from an array-type to System.Array
431 if (expr_type.IsArray && target_type == TypeManager.array_type)
432 return new EmptyCast (expr, target_type);
434 // from any delegate type to System.Delegate
435 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
436 target_type == TypeManager.delegate_type)
437 return new EmptyCast (expr, target_type);
439 // from any array-type or delegate type into System.ICloneable.
440 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
441 if (target_type == TypeManager.icloneable_type)
442 return new EmptyCast (expr, target_type);
444 // from the null type to any reference-type.
445 if (expr is NullLiteral)
446 return new EmptyCast (expr, target_type);
456 // Handles expressions like this: decimal d; d = 1;
457 // and changes them into: decimal d; d = new System.Decimal (1);
459 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
461 ArrayList args = new ArrayList ();
463 args.Add (new Argument (expr, Argument.AType.Expression));
465 Expression ne = new New (target.FullName, args,
468 return ne.Resolve (ec);
472 // Implicit Numeric Conversions.
474 // expr is the expression to convert, returns a new expression of type
475 // target_type or null if an implicit conversion is not possible.
478 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
479 Type target_type, Location loc)
481 Type expr_type = expr.Type;
484 // Attempt to do the implicit constant expression conversions
486 if (expr is IntLiteral){
489 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
492 } else if (expr is LongLiteral){
494 // Try the implicit constant expression conversion
495 // from long to ulong, instead of a nice routine,
498 if (((LongLiteral) expr).Value > 0)
499 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
502 if (expr_type == TypeManager.sbyte_type){
504 // From sbyte to short, int, long, float, double.
506 if (target_type == TypeManager.int32_type)
507 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
508 if (target_type == TypeManager.int64_type)
509 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
510 if (target_type == TypeManager.double_type)
511 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
512 if (target_type == TypeManager.float_type)
513 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
514 if (target_type == TypeManager.short_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
516 if (target_type == TypeManager.decimal_type)
517 return InternalTypeConstructor (ec, expr, target_type);
518 } else if (expr_type == TypeManager.byte_type){
520 // From byte to short, ushort, int, uint, long, ulong, float, double
522 if ((target_type == TypeManager.short_type) ||
523 (target_type == TypeManager.ushort_type) ||
524 (target_type == TypeManager.int32_type) ||
525 (target_type == TypeManager.uint32_type))
526 return new EmptyCast (expr, target_type);
528 if (target_type == TypeManager.uint64_type)
529 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
530 if (target_type == TypeManager.int64_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
533 if (target_type == TypeManager.float_type)
534 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
535 if (target_type == TypeManager.double_type)
536 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
537 if (target_type == TypeManager.decimal_type)
538 return InternalTypeConstructor (ec, expr, target_type);
539 } else if (expr_type == TypeManager.short_type){
541 // From short to int, long, float, double
543 if (target_type == TypeManager.int32_type)
544 return new EmptyCast (expr, target_type);
545 if (target_type == TypeManager.int64_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
547 if (target_type == TypeManager.double_type)
548 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
549 if (target_type == TypeManager.float_type)
550 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
551 if (target_type == TypeManager.decimal_type)
552 return InternalTypeConstructor (ec, expr, target_type);
553 } else if (expr_type == TypeManager.ushort_type){
555 // From ushort to int, uint, long, ulong, float, double
557 if (target_type == TypeManager.uint32_type)
558 return new EmptyCast (expr, target_type);
560 if (target_type == TypeManager.uint64_type)
561 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
562 if (target_type == TypeManager.int32_type)
563 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
564 if (target_type == TypeManager.int64_type)
565 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
566 if (target_type == TypeManager.double_type)
567 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
568 if (target_type == TypeManager.float_type)
569 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
570 if (target_type == TypeManager.decimal_type)
571 return InternalTypeConstructor (ec, expr, target_type);
572 } else if (expr_type == TypeManager.int32_type){
574 // From int to long, float, double
576 if (target_type == TypeManager.int64_type)
577 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
578 if (target_type == TypeManager.double_type)
579 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
580 if (target_type == TypeManager.float_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
582 if (target_type == TypeManager.decimal_type)
583 return InternalTypeConstructor (ec, expr, target_type);
584 } else if (expr_type == TypeManager.uint32_type){
586 // From uint to long, ulong, float, double
588 if (target_type == TypeManager.int64_type)
589 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
590 if (target_type == TypeManager.uint64_type)
591 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
592 if (target_type == TypeManager.double_type)
593 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
595 if (target_type == TypeManager.float_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
598 if (target_type == TypeManager.decimal_type)
599 return InternalTypeConstructor (ec, expr, target_type);
600 } else if ((expr_type == TypeManager.uint64_type) ||
601 (expr_type == TypeManager.int64_type)){
603 // From long/ulong to float, double
605 if (target_type == TypeManager.double_type)
606 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
608 if (target_type == TypeManager.float_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
611 if (target_type == TypeManager.decimal_type)
612 return InternalTypeConstructor (ec, expr, target_type);
613 } else if (expr_type == TypeManager.char_type){
615 // From char to ushort, int, uint, long, ulong, float, double
617 if ((target_type == TypeManager.ushort_type) ||
618 (target_type == TypeManager.int32_type) ||
619 (target_type == TypeManager.uint32_type))
620 return new EmptyCast (expr, target_type);
621 if (target_type == TypeManager.uint64_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
623 if (target_type == TypeManager.int64_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
625 if (target_type == TypeManager.float_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
627 if (target_type == TypeManager.double_type)
628 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
629 if (target_type == TypeManager.decimal_type)
630 return InternalTypeConstructor (ec, expr, target_type);
631 } else if (expr_type == TypeManager.float_type){
635 if (target_type == TypeManager.double_type)
636 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
643 // Determines if a standard implicit conversion exists from
644 // expr_type to target_type
646 public static bool StandardConversionExists (Type expr_type, Type target_type)
648 if (expr_type == target_type)
651 // First numeric conversions
653 if (expr_type == TypeManager.sbyte_type){
655 // From sbyte to short, int, long, float, double.
657 if ((target_type == TypeManager.int32_type) ||
658 (target_type == TypeManager.int64_type) ||
659 (target_type == TypeManager.double_type) ||
660 (target_type == TypeManager.float_type) ||
661 (target_type == TypeManager.short_type) ||
662 (target_type == TypeManager.decimal_type))
665 } else if (expr_type == TypeManager.byte_type){
667 // From byte to short, ushort, int, uint, long, ulong, float, double
669 if ((target_type == TypeManager.short_type) ||
670 (target_type == TypeManager.ushort_type) ||
671 (target_type == TypeManager.int32_type) ||
672 (target_type == TypeManager.uint32_type) ||
673 (target_type == TypeManager.uint64_type) ||
674 (target_type == TypeManager.int64_type) ||
675 (target_type == TypeManager.float_type) ||
676 (target_type == TypeManager.double_type) ||
677 (target_type == TypeManager.decimal_type))
680 } else if (expr_type == TypeManager.short_type){
682 // From short to int, long, float, double
684 if ((target_type == TypeManager.int32_type) ||
685 (target_type == TypeManager.int64_type) ||
686 (target_type == TypeManager.double_type) ||
687 (target_type == TypeManager.float_type) ||
688 (target_type == TypeManager.decimal_type))
691 } else if (expr_type == TypeManager.ushort_type){
693 // From ushort to int, uint, long, ulong, float, double
695 if ((target_type == TypeManager.uint32_type) ||
696 (target_type == TypeManager.uint64_type) ||
697 (target_type == TypeManager.int32_type) ||
698 (target_type == TypeManager.int64_type) ||
699 (target_type == TypeManager.double_type) ||
700 (target_type == TypeManager.float_type) ||
701 (target_type == TypeManager.decimal_type))
704 } else if (expr_type == TypeManager.int32_type){
706 // From int to long, float, double
708 if ((target_type == TypeManager.int64_type) ||
709 (target_type == TypeManager.double_type) ||
710 (target_type == TypeManager.float_type) ||
711 (target_type == TypeManager.decimal_type))
714 } else if (expr_type == TypeManager.uint32_type){
716 // From uint to long, ulong, float, double
718 if ((target_type == TypeManager.int64_type) ||
719 (target_type == TypeManager.uint64_type) ||
720 (target_type == TypeManager.double_type) ||
721 (target_type == TypeManager.float_type) ||
722 (target_type == TypeManager.decimal_type))
725 } else if ((expr_type == TypeManager.uint64_type) ||
726 (expr_type == TypeManager.int64_type)) {
728 // From long/ulong to float, double
730 if ((target_type == TypeManager.double_type) ||
731 (target_type == TypeManager.float_type) ||
732 (target_type == TypeManager.decimal_type))
735 } else if (expr_type == TypeManager.char_type){
737 // From char to ushort, int, uint, long, ulong, float, double
739 if ((target_type == TypeManager.ushort_type) ||
740 (target_type == TypeManager.int32_type) ||
741 (target_type == TypeManager.uint32_type) ||
742 (target_type == TypeManager.uint64_type) ||
743 (target_type == TypeManager.int64_type) ||
744 (target_type == TypeManager.float_type) ||
745 (target_type == TypeManager.double_type) ||
746 (target_type == TypeManager.decimal_type))
749 } else if (expr_type == TypeManager.float_type){
753 if (target_type == TypeManager.double_type)
757 // Next reference conversions
759 if (target_type == TypeManager.object_type) {
760 if ((expr_type.IsClass) ||
761 (expr_type.IsValueType))
764 } else if (expr_type.IsSubclassOf (target_type)) {
768 // from any class-type S to any interface-type T.
769 if (expr_type.IsClass && target_type.IsInterface)
772 // from any interface type S to interface-type T.
773 // FIXME : Is it right to use IsAssignableFrom ?
774 if (expr_type.IsInterface && target_type.IsInterface)
775 if (target_type.IsAssignableFrom (expr_type))
778 // from an array-type S to an array-type of type T
779 if (expr_type.IsArray && target_type.IsArray) {
780 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
782 Type expr_element_type = expr_type.GetElementType ();
783 Type target_element_type = target_type.GetElementType ();
785 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
786 if (StandardConversionExists (expr_element_type,
787 target_element_type))
792 // from an array-type to System.Array
793 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
796 // from any delegate type to System.Delegate
797 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
798 target_type == TypeManager.delegate_type)
799 if (target_type.IsAssignableFrom (expr_type))
802 // from any array-type or delegate type into System.ICloneable.
803 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
804 if (target_type == TypeManager.icloneable_type)
807 // from the null type to any reference-type.
808 // FIXME : How do we do this ?
816 // Finds "most encompassed type" according to the spec (13.4.2)
817 // amongst the methods in the MethodGroupExpr which convert from a
818 // type encompassing source_type
820 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
824 for (int i = me.Methods.Length; i > 0; ) {
827 MethodBase mb = me.Methods [i];
828 ParameterData pd = Invocation.GetParameterData (mb);
829 Type param_type = pd.ParameterType (0);
831 if (StandardConversionExists (source_type, param_type)) {
835 if (StandardConversionExists (param_type, best))
844 // Finds "most encompassing type" according to the spec (13.4.2)
845 // amongst the methods in the MethodGroupExpr which convert to a
846 // type encompassed by target_type
848 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
852 for (int i = me.Methods.Length; i > 0; ) {
855 MethodInfo mi = (MethodInfo) me.Methods [i];
856 Type ret_type = mi.ReturnType;
858 if (StandardConversionExists (ret_type, target)) {
862 if (!StandardConversionExists (ret_type, best))
874 // User-defined Implicit conversions
876 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
877 Type target, Location loc)
879 return UserDefinedConversion (ec, source, target, loc, false);
883 // User-defined Explicit conversions
885 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
886 Type target, Location loc)
888 return UserDefinedConversion (ec, source, target, loc, true);
892 // User-defined conversions
894 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
895 Type target, Location loc,
896 bool look_for_explicit)
898 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
899 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
901 MethodBase method = null;
902 Type source_type = source.Type;
906 // If we have a boolean type, we need to check for the True operator
908 // FIXME : How does the False operator come into the picture ?
909 // FIXME : This doesn't look complete and very correct !
910 if (target == TypeManager.bool_type)
913 op_name = "op_Implicit";
915 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
917 if (source_type.BaseType != null)
918 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
920 mg3 = MemberLookup (ec, target, op_name, false, loc);
922 if (target.BaseType != null)
923 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
925 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
926 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
928 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
930 MethodGroupExpr union4 = null;
932 if (look_for_explicit) {
934 op_name = "op_Explicit";
936 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
938 if (source_type.BaseType != null)
939 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
941 mg7 = MemberLookup (ec, target, op_name, false, loc);
943 if (target.BaseType != null)
944 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
946 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
947 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
949 union4 = Invocation.MakeUnionSet (union5, union6);
952 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
956 Type most_specific_source, most_specific_target;
958 most_specific_source = FindMostEncompassedType (union, source_type);
959 if (most_specific_source == null)
962 most_specific_target = FindMostEncompassingType (union, target);
963 if (most_specific_target == null)
968 for (int i = union.Methods.Length; i > 0;) {
971 MethodBase mb = union.Methods [i];
972 ParameterData pd = Invocation.GetParameterData (mb);
973 MethodInfo mi = (MethodInfo) union.Methods [i];
975 if (pd.ParameterType (0) == most_specific_source &&
976 mi.ReturnType == most_specific_target) {
982 if (method == null || count > 1) {
983 Report.Error (-11, loc, "Ambiguous user defined conversion");
988 // This will do the conversion to the best match that we
989 // found. Now we need to perform an implict standard conversion
990 // if the best match was not the type that we were requested
993 if (look_for_explicit)
994 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
996 source = ConvertImplicitStandard (ec, source,
997 most_specific_source, loc);
1002 e = new UserCast ((MethodInfo) method, source);
1004 if (e.Type != target){
1005 if (!look_for_explicit)
1006 e = ConvertImplicitStandard (ec, e, target, loc);
1008 e = ConvertExplicitStandard (ec, e, target, loc);
1019 // Converts implicitly the resolved expression `expr' into the
1020 // `target_type'. It returns a new expression that can be used
1021 // in a context that expects a `target_type'.
1023 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1024 Type target_type, Location loc)
1026 Type expr_type = expr.Type;
1029 if (expr_type == target_type)
1032 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1036 e = ImplicitReferenceConversion (expr, target_type);
1040 e = ImplicitUserConversion (ec, expr, target_type, loc);
1044 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1045 IntLiteral i = (IntLiteral) expr;
1048 return new EmptyCast (expr, target_type);
1056 // Attempts to apply the `Standard Implicit
1057 // Conversion' rules to the expression `expr' into
1058 // the `target_type'. It returns a new expression
1059 // that can be used in a context that expects a
1062 // This is different from `ConvertImplicit' in that the
1063 // user defined implicit conversions are excluded.
1065 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1066 Type target_type, Location loc)
1068 Type expr_type = expr.Type;
1071 if (expr_type == target_type)
1074 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1078 e = ImplicitReferenceConversion (expr, target_type);
1082 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1083 IntLiteral i = (IntLiteral) expr;
1086 return new EmptyCast (expr, target_type);
1091 // Attemps to perform an implict constant conversion of the IntLiteral
1092 // into a different data type using casts (See Implicit Constant
1093 // Expression Conversions)
1095 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1097 int value = il.Value;
1099 if (target_type == TypeManager.sbyte_type){
1100 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1102 } else if (target_type == TypeManager.byte_type){
1103 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1105 } else if (target_type == TypeManager.short_type){
1106 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1108 } else if (target_type == TypeManager.ushort_type){
1109 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1111 } else if (target_type == TypeManager.uint32_type){
1113 // we can optimize this case: a positive int32
1114 // always fits on a uint32
1118 } else if (target_type == TypeManager.uint64_type){
1120 // we can optimize this case: a positive int32
1121 // always fits on a uint64. But we need an opcode
1125 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1132 // Attemptes to implicityly convert `target' into `type', using
1133 // ConvertImplicit. If there is no implicit conversion, then
1134 // an error is signaled
1136 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1137 Type type, Location loc)
1141 e = ConvertImplicit (ec, target, type, loc);
1145 string msg = "Can not convert implicitly from `"+
1146 TypeManager.CSharpName (target.Type) + "' to `" +
1147 TypeManager.CSharpName (type) + "'";
1149 Error (29, loc, msg);
1155 // Performs the explicit numeric conversions
1157 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1160 Type expr_type = expr.Type;
1162 if (expr_type == TypeManager.sbyte_type){
1164 // From sbyte to byte, ushort, uint, ulong, char
1166 if (target_type == TypeManager.byte_type)
1167 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1168 if (target_type == TypeManager.ushort_type)
1169 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1170 if (target_type == TypeManager.uint32_type)
1171 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1172 if (target_type == TypeManager.uint64_type)
1173 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1174 if (target_type == TypeManager.char_type)
1175 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1176 } else if (expr_type == TypeManager.byte_type){
1178 // From byte to sbyte and char
1180 if (target_type == TypeManager.sbyte_type)
1181 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1182 if (target_type == TypeManager.char_type)
1183 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1184 } else if (expr_type == TypeManager.short_type){
1186 // From short to sbyte, byte, ushort, uint, ulong, char
1188 if (target_type == TypeManager.sbyte_type)
1189 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1190 if (target_type == TypeManager.byte_type)
1191 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1192 if (target_type == TypeManager.ushort_type)
1193 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1194 if (target_type == TypeManager.uint32_type)
1195 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1196 if (target_type == TypeManager.uint64_type)
1197 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1198 if (target_type == TypeManager.char_type)
1199 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1200 } else if (expr_type == TypeManager.ushort_type){
1202 // From ushort to sbyte, byte, short, char
1204 if (target_type == TypeManager.sbyte_type)
1205 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1206 if (target_type == TypeManager.byte_type)
1207 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1208 if (target_type == TypeManager.short_type)
1209 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1210 if (target_type == TypeManager.char_type)
1211 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1212 } else if (expr_type == TypeManager.int32_type){
1214 // From int to sbyte, byte, short, ushort, uint, ulong, char
1216 if (target_type == TypeManager.sbyte_type)
1217 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1218 if (target_type == TypeManager.byte_type)
1219 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1220 if (target_type == TypeManager.short_type)
1221 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1222 if (target_type == TypeManager.ushort_type)
1223 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1224 if (target_type == TypeManager.uint32_type)
1225 return new EmptyCast (expr, target_type);
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.uint32_type){
1232 // From uint to sbyte, byte, short, ushort, int, 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.ushort_type)
1241 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1242 if (target_type == TypeManager.int32_type)
1243 return new EmptyCast (expr, target_type);
1244 if (target_type == TypeManager.char_type)
1245 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1246 } else if (expr_type == TypeManager.int64_type){
1248 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1250 if (target_type == TypeManager.sbyte_type)
1251 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1252 if (target_type == TypeManager.byte_type)
1253 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1254 if (target_type == TypeManager.short_type)
1255 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1256 if (target_type == TypeManager.ushort_type)
1257 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1258 if (target_type == TypeManager.int32_type)
1259 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1260 if (target_type == TypeManager.uint32_type)
1261 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1262 if (target_type == TypeManager.uint64_type)
1263 return new EmptyCast (expr, target_type);
1264 if (target_type == TypeManager.char_type)
1265 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1266 } else if (expr_type == TypeManager.uint64_type){
1268 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1270 if (target_type == TypeManager.sbyte_type)
1271 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1272 if (target_type == TypeManager.byte_type)
1273 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1274 if (target_type == TypeManager.short_type)
1275 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1276 if (target_type == TypeManager.ushort_type)
1277 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1278 if (target_type == TypeManager.int32_type)
1279 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1280 if (target_type == TypeManager.uint32_type)
1281 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1282 if (target_type == TypeManager.int64_type)
1283 return new EmptyCast (expr, target_type);
1284 if (target_type == TypeManager.char_type)
1285 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1286 } else if (expr_type == TypeManager.char_type){
1288 // From char to sbyte, byte, short
1290 if (target_type == TypeManager.sbyte_type)
1291 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1292 if (target_type == TypeManager.byte_type)
1293 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1294 if (target_type == TypeManager.short_type)
1295 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1296 } else if (expr_type == TypeManager.float_type){
1298 // From float to sbyte, byte, short,
1299 // ushort, int, uint, long, ulong, char
1302 if (target_type == TypeManager.sbyte_type)
1303 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1304 if (target_type == TypeManager.byte_type)
1305 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1306 if (target_type == TypeManager.short_type)
1307 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1308 if (target_type == TypeManager.ushort_type)
1309 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1310 if (target_type == TypeManager.int32_type)
1311 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1312 if (target_type == TypeManager.uint32_type)
1313 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1314 if (target_type == TypeManager.int64_type)
1315 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1316 if (target_type == TypeManager.uint64_type)
1317 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1318 if (target_type == TypeManager.char_type)
1319 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1320 if (target_type == TypeManager.decimal_type)
1321 return InternalTypeConstructor (ec, expr, target_type);
1322 } else if (expr_type == TypeManager.double_type){
1324 // From double to byte, byte, short,
1325 // ushort, int, uint, long, ulong,
1326 // char, float or decimal
1328 if (target_type == TypeManager.sbyte_type)
1329 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1330 if (target_type == TypeManager.byte_type)
1331 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1332 if (target_type == TypeManager.short_type)
1333 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1334 if (target_type == TypeManager.ushort_type)
1335 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1336 if (target_type == TypeManager.int32_type)
1337 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1338 if (target_type == TypeManager.uint32_type)
1339 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1340 if (target_type == TypeManager.int64_type)
1341 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1342 if (target_type == TypeManager.uint64_type)
1343 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1344 if (target_type == TypeManager.char_type)
1345 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1346 if (target_type == TypeManager.float_type)
1347 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1348 if (target_type == TypeManager.decimal_type)
1349 return InternalTypeConstructor (ec, expr, target_type);
1352 // decimal is taken care of by the op_Explicit methods.
1358 // Returns whether an explicit reference conversion can be performed
1359 // from source_type to target_type
1361 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1363 bool target_is_value_type = target_type.IsValueType;
1365 if (source_type == target_type)
1369 // From object to any reference type
1371 if (source_type == TypeManager.object_type && !target_is_value_type)
1375 // From any class S to any class-type T, provided S is a base class of T
1377 if (target_type.IsSubclassOf (source_type))
1381 // From any interface type S to any interface T provided S is not derived from T
1383 if (source_type.IsInterface && target_type.IsInterface){
1384 if (!target_type.IsSubclassOf (source_type))
1389 // From any class type S to any interface T, provides S is not sealed
1390 // and provided S does not implement T.
1392 if (target_type.IsInterface && !source_type.IsSealed &&
1393 !target_type.IsAssignableFrom (source_type))
1397 // From any interface-type S to to any class type T, provided T is not
1398 // sealed, or provided T implements S.
1400 if (source_type.IsInterface &&
1401 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1404 // From an array type S with an element type Se to an array type T with an
1405 // element type Te provided all the following are true:
1406 // * S and T differe only in element type, in other words, S and T
1407 // have the same number of dimensions.
1408 // * Both Se and Te are reference types
1409 // * An explicit referenc conversions exist from Se to Te
1411 if (source_type.IsArray && target_type.IsArray) {
1412 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1414 Type source_element_type = source_type.GetElementType ();
1415 Type target_element_type = target_type.GetElementType ();
1417 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1418 if (ExplicitReferenceConversionExists (source_element_type,
1419 target_element_type))
1425 // From System.Array to any array-type
1426 if (source_type == TypeManager.array_type &&
1427 target_type.IsSubclassOf (TypeManager.array_type)){
1432 // From System delegate to any delegate-type
1434 if (source_type == TypeManager.delegate_type &&
1435 target_type.IsSubclassOf (TypeManager.delegate_type))
1439 // From ICloneable to Array or Delegate types
1441 if (source_type == TypeManager.icloneable_type &&
1442 (target_type == TypeManager.array_type ||
1443 target_type == TypeManager.delegate_type))
1450 // Implements Explicit Reference conversions
1452 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1454 Type source_type = source.Type;
1455 bool target_is_value_type = target_type.IsValueType;
1458 // From object to any reference type
1460 if (source_type == TypeManager.object_type && !target_is_value_type)
1461 return new ClassCast (source, target_type);
1465 // From any class S to any class-type T, provided S is a base class of T
1467 if (target_type.IsSubclassOf (source_type))
1468 return new ClassCast (source, target_type);
1471 // From any interface type S to any interface T provided S is not derived from T
1473 if (source_type.IsInterface && target_type.IsInterface){
1475 Type [] ifaces = source_type.GetInterfaces ();
1477 for (int i = ifaces.Length; i > 0; ) {
1480 if (ifaces [i] == target_type)
1484 return new ClassCast (source, target_type);
1488 // From any class type S to any interface T, provides S is not sealed
1489 // and provided S does not implement T.
1491 if (target_type.IsInterface && !source_type.IsSealed) {
1493 Type [] ifaces = source_type.GetInterfaces ();
1495 for (int i = ifaces.Length; i > 0; ) {
1498 if (ifaces [i] == target_type)
1502 return new ClassCast (source, target_type);
1506 // From any interface-type S to to any class type T, provided T is not
1507 // sealed, or provided T implements S.
1509 if (source_type.IsInterface) {
1511 if (target_type.IsSealed)
1514 Type [] ifaces = target_type.GetInterfaces ();
1516 for (int i = ifaces.Length; i > 0; ) {
1519 if (ifaces [i] == source_type)
1520 return new ClassCast (source, target_type);
1526 // From an array type S with an element type Se to an array type T with an
1527 // element type Te provided all the following are true:
1528 // * S and T differe only in element type, in other words, S and T
1529 // have the same number of dimensions.
1530 // * Both Se and Te are reference types
1531 // * An explicit referenc conversions exist from Se to Te
1533 if (source_type.IsArray && target_type.IsArray) {
1534 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1536 Type source_element_type = source_type.GetElementType ();
1537 Type target_element_type = target_type.GetElementType ();
1539 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1540 if (ExplicitReferenceConversionExists (source_element_type,
1541 target_element_type))
1542 return new ClassCast (source, target_type);
1547 // From System.Array to any array-type
1548 if (source_type == TypeManager.array_type &&
1549 target_type.IsSubclassOf (TypeManager.array_type)){
1550 return new ClassCast (source, target_type);
1554 // From System delegate to any delegate-type
1556 if (source_type == TypeManager.delegate_type &&
1557 target_type.IsSubclassOf (TypeManager.delegate_type))
1558 return new ClassCast (source, target_type);
1561 // From ICloneable to Array or Delegate types
1563 if (source_type == TypeManager.icloneable_type &&
1564 (target_type == TypeManager.array_type ||
1565 target_type == TypeManager.delegate_type))
1566 return new ClassCast (source, target_type);
1572 // Performs an explicit conversion of the expression `expr' whose
1573 // type is expr.Type to `target_type'.
1575 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1576 Type target_type, Location loc)
1578 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1583 ne = ConvertNumericExplicit (ec, expr, target_type);
1587 ne = ConvertReferenceExplicit (expr, target_type);
1591 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1595 Report.Error (30, loc, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1596 + TypeManager.CSharpName (target_type) + "'");
1601 // Same as ConverExplicit, only it doesn't include user defined conversions
1603 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1604 Type target_type, Location l)
1606 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1611 ne = ConvertNumericExplicit (ec, expr, target_type);
1615 ne = ConvertReferenceExplicit (expr, target_type);
1619 Report.Error (30, l, "Cannot convert type '" +
1620 TypeManager.CSharpName (expr.Type) + "' to '" +
1621 TypeManager.CSharpName (target_type) + "'");
1625 static string ExprClassName (ExprClass c)
1628 case ExprClass.Invalid:
1630 case ExprClass.Value:
1632 case ExprClass.Variable:
1634 case ExprClass.Namespace:
1636 case ExprClass.Type:
1638 case ExprClass.MethodGroup:
1639 return "method group";
1640 case ExprClass.PropertyAccess:
1641 return "property access";
1642 case ExprClass.EventAccess:
1643 return "event access";
1644 case ExprClass.IndexerAccess:
1645 return "indexer access";
1646 case ExprClass.Nothing:
1649 throw new Exception ("Should not happen");
1653 // Reports that we were expecting `expr' to be of class `expected'
1655 protected void report118 (Location loc, Expression expr, string expected)
1657 string kind = "Unknown";
1660 kind = ExprClassName (expr.ExprClass);
1662 Error (118, loc, "Expression denotes a '" + kind +
1663 "' where an " + expected + " was expected");
1668 // This is just a base class for expressions that can
1669 // appear on statements (invocations, object creation,
1670 // assignments, post/pre increment and decrement). The idea
1671 // being that they would support an extra Emition interface that
1672 // does not leave a result on the stack.
1675 public abstract class ExpressionStatement : Expression {
1678 // Requests the expression to be emitted in a `statement'
1679 // context. This means that no new value is left on the
1680 // stack after invoking this method (constrasted with
1681 // Emit that will always leave a value on the stack).
1683 public abstract void EmitStatement (EmitContext ec);
1687 // This kind of cast is used to encapsulate the child
1688 // whose type is child.Type into an expression that is
1689 // reported to return "return_type". This is used to encapsulate
1690 // expressions which have compatible types, but need to be dealt
1691 // at higher levels with.
1693 // For example, a "byte" expression could be encapsulated in one
1694 // of these as an "unsigned int". The type for the expression
1695 // would be "unsigned int".
1699 public class EmptyCast : Expression {
1700 protected Expression child;
1702 public EmptyCast (Expression child, Type return_type)
1704 ExprClass = child.ExprClass;
1709 public override Expression DoResolve (EmitContext ec)
1711 // This should never be invoked, we are born in fully
1712 // initialized state.
1717 public override void Emit (EmitContext ec)
1724 // This kind of cast is used to encapsulate Value Types in objects.
1726 // The effect of it is to box the value type emitted by the previous
1729 public class BoxedCast : EmptyCast {
1731 public BoxedCast (Expression expr)
1732 : base (expr, TypeManager.object_type)
1736 public override Expression DoResolve (EmitContext ec)
1738 // This should never be invoked, we are born in fully
1739 // initialized state.
1744 public override void Emit (EmitContext ec)
1747 ec.ig.Emit (OpCodes.Box, child.Type);
1752 // This kind of cast is used to encapsulate a child expression
1753 // that can be trivially converted to a target type using one or
1754 // two opcodes. The opcodes are passed as arguments.
1756 public class OpcodeCast : EmptyCast {
1760 public OpcodeCast (Expression child, Type return_type, OpCode op)
1761 : base (child, return_type)
1765 second_valid = false;
1768 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1769 : base (child, return_type)
1774 second_valid = true;
1777 public override Expression DoResolve (EmitContext ec)
1779 // This should never be invoked, we are born in fully
1780 // initialized state.
1785 public override void Emit (EmitContext ec)
1797 // This kind of cast is used to encapsulate a child and cast it
1798 // to the class requested
1800 public class ClassCast : EmptyCast {
1801 public ClassCast (Expression child, Type return_type)
1802 : base (child, return_type)
1807 public override Expression DoResolve (EmitContext ec)
1809 // This should never be invoked, we are born in fully
1810 // initialized state.
1815 public override void Emit (EmitContext ec)
1819 ec.ig.Emit (OpCodes.Castclass, type);
1825 // Unary expressions.
1829 // Unary implements unary expressions. It derives from
1830 // ExpressionStatement becuase the pre/post increment/decrement
1831 // operators can be used in a statement context.
1833 public class Unary : ExpressionStatement {
1834 public enum Operator {
1835 Addition, Subtraction, Negate, BitComplement,
1836 Indirection, AddressOf, PreIncrement,
1837 PreDecrement, PostIncrement, PostDecrement
1842 ArrayList Arguments;
1846 public Unary (Operator op, Expression expr, Location loc)
1853 public Expression Expr {
1863 public Operator Oper {
1874 // Returns a stringified representation of the Operator
1879 case Operator.Addition:
1881 case Operator.Subtraction:
1883 case Operator.Negate:
1885 case Operator.BitComplement:
1887 case Operator.AddressOf:
1889 case Operator.Indirection:
1891 case Operator.PreIncrement : case Operator.PostIncrement :
1893 case Operator.PreDecrement : case Operator.PostDecrement :
1897 return oper.ToString ();
1900 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1902 if (expr.Type == target_type)
1905 return ConvertImplicit (ec, expr, target_type, new Location (-1));
1908 void error23 (Type t)
1911 23, loc, "Operator " + OperName () +
1912 " cannot be applied to operand of type `" +
1913 TypeManager.CSharpName (t) + "'");
1917 // Returns whether an object of type `t' can be incremented
1918 // or decremented with add/sub (ie, basically whether we can
1919 // use pre-post incr-decr operations on it, but it is not a
1920 // System.Decimal, which we test elsewhere)
1922 static bool IsIncrementableNumber (Type t)
1924 return (t == TypeManager.sbyte_type) ||
1925 (t == TypeManager.byte_type) ||
1926 (t == TypeManager.short_type) ||
1927 (t == TypeManager.ushort_type) ||
1928 (t == TypeManager.int32_type) ||
1929 (t == TypeManager.uint32_type) ||
1930 (t == TypeManager.int64_type) ||
1931 (t == TypeManager.uint64_type) ||
1932 (t == TypeManager.char_type) ||
1933 (t.IsSubclassOf (TypeManager.enum_type)) ||
1934 (t == TypeManager.float_type) ||
1935 (t == TypeManager.double_type);
1938 Expression ResolveOperator (EmitContext ec)
1940 Type expr_type = expr.Type;
1943 // Step 1: Perform Operator Overload location
1948 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1949 op_name = "op_Increment";
1950 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1951 op_name = "op_Decrement";
1953 op_name = "op_" + oper;
1955 mg = MemberLookup (ec, expr_type, op_name, false, loc);
1957 if (mg == null && expr_type.BaseType != null)
1958 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
1961 Arguments = new ArrayList ();
1962 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1964 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
1966 if (method != null) {
1967 MethodInfo mi = (MethodInfo) method;
1968 type = mi.ReturnType;
1971 error23 (expr_type);
1978 // Step 2: Default operations on CLI native types.
1981 // Only perform numeric promotions on:
1984 if (expr_type == null)
1987 if (oper == Operator.Negate){
1988 if (expr_type != TypeManager.bool_type) {
1989 error23 (expr.Type);
1993 type = TypeManager.bool_type;
1997 if (oper == Operator.BitComplement) {
1998 if (!((expr_type == TypeManager.int32_type) ||
1999 (expr_type == TypeManager.uint32_type) ||
2000 (expr_type == TypeManager.int64_type) ||
2001 (expr_type == TypeManager.uint64_type) ||
2002 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
2003 error23 (expr.Type);
2010 if (oper == Operator.Addition) {
2012 // A plus in front of something is just a no-op, so return the child.
2018 // Deals with -literals
2019 // int operator- (int x)
2020 // long operator- (long x)
2021 // float operator- (float f)
2022 // double operator- (double d)
2023 // decimal operator- (decimal d)
2025 if (oper == Operator.Subtraction){
2027 // Fold a "- Constant" into a negative constant
2030 Expression e = null;
2033 // Is this a constant?
2035 if (expr is IntLiteral)
2036 e = new IntLiteral (-((IntLiteral) expr).Value);
2037 else if (expr is LongLiteral)
2038 e = new LongLiteral (-((LongLiteral) expr).Value);
2039 else if (expr is FloatLiteral)
2040 e = new FloatLiteral (-((FloatLiteral) expr).Value);
2041 else if (expr is DoubleLiteral)
2042 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
2043 else if (expr is DecimalLiteral)
2044 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
2052 // Not a constant we can optimize, perform numeric
2053 // promotions to int, long, double.
2056 // The following is inneficient, because we call
2057 // ConvertImplicit too many times.
2059 // It is also not clear if we should convert to Float
2060 // or Double initially.
2062 if (expr_type == TypeManager.uint32_type){
2064 // FIXME: handle exception to this rule that
2065 // permits the int value -2147483648 (-2^31) to
2066 // bt written as a decimal interger literal
2068 type = TypeManager.int64_type;
2069 expr = ConvertImplicit (ec, expr, type, loc);
2073 if (expr_type == TypeManager.uint64_type){
2075 // FIXME: Handle exception of `long value'
2076 // -92233720368547758087 (-2^63) to be written as
2077 // decimal integer literal.
2079 error23 (expr_type);
2083 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
2090 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
2097 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
2104 error23 (expr_type);
2109 // The operand of the prefix/postfix increment decrement operators
2110 // should be an expression that is classified as a variable,
2111 // a property access or an indexer access
2113 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2114 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2115 if (expr.ExprClass == ExprClass.Variable){
2116 if (IsIncrementableNumber (expr_type) ||
2117 expr_type == TypeManager.decimal_type){
2121 } else if (expr.ExprClass == ExprClass.IndexerAccess){
2123 // FIXME: Verify that we have both get and set methods
2125 throw new Exception ("Implement me");
2126 } else if (expr.ExprClass == ExprClass.PropertyAccess){
2127 PropertyExpr pe = (PropertyExpr) expr;
2129 if (pe.VerifyAssignable ())
2133 report118 (loc, expr, "variable, indexer or property access");
2137 if (oper == Operator.AddressOf){
2138 if (expr.ExprClass != ExprClass.Variable){
2139 Error (211, "Cannot take the address of non-variables");
2142 type = Type.GetType (expr.Type.ToString () + "*");
2145 Error (187, "No such operator '" + OperName () + "' defined for type '" +
2146 TypeManager.CSharpName (expr_type) + "'");
2151 public override Expression DoResolve (EmitContext ec)
2153 expr = expr.Resolve (ec);
2158 eclass = ExprClass.Value;
2159 return ResolveOperator (ec);
2162 public override void Emit (EmitContext ec)
2164 ILGenerator ig = ec.ig;
2165 Type expr_type = expr.Type;
2168 if (method != null) {
2170 // Note that operators are static anyway
2172 if (Arguments != null)
2173 Invocation.EmitArguments (ec, method, Arguments);
2176 // Post increment/decrement operations need a copy at this
2179 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
2180 ig.Emit (OpCodes.Dup);
2183 ig.Emit (OpCodes.Call, (MethodInfo) method);
2186 // Pre Increment and Decrement operators
2188 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
2189 ig.Emit (OpCodes.Dup);
2193 // Increment and Decrement should store the result
2195 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2196 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2197 ((IStackStore) expr).Store (ec);
2203 case Operator.Addition:
2204 throw new Exception ("This should be caught by Resolve");
2206 case Operator.Subtraction:
2208 ig.Emit (OpCodes.Neg);
2211 case Operator.Negate:
2213 ig.Emit (OpCodes.Ldc_I4_0);
2214 ig.Emit (OpCodes.Ceq);
2217 case Operator.BitComplement:
2219 ig.Emit (OpCodes.Not);
2222 case Operator.AddressOf:
2223 ((IMemoryLocation)expr).AddressOf (ec);
2226 case Operator.Indirection:
2227 throw new Exception ("Not implemented yet");
2229 case Operator.PreIncrement:
2230 case Operator.PreDecrement:
2231 if (expr.ExprClass == ExprClass.Variable){
2233 // Resolve already verified that it is an "incrementable"
2236 ig.Emit (OpCodes.Ldc_I4_1);
2238 if (oper == Operator.PreDecrement)
2239 ig.Emit (OpCodes.Sub);
2241 ig.Emit (OpCodes.Add);
2242 ig.Emit (OpCodes.Dup);
2243 ((IStackStore) expr).Store (ec);
2245 throw new Exception ("Handle Indexers and Properties here");
2249 case Operator.PostIncrement:
2250 case Operator.PostDecrement:
2251 eclass = expr.ExprClass;
2252 if (eclass == ExprClass.Variable){
2254 // Resolve already verified that it is an "incrementable"
2257 ig.Emit (OpCodes.Dup);
2258 ig.Emit (OpCodes.Ldc_I4_1);
2260 if (oper == Operator.PostDecrement)
2261 ig.Emit (OpCodes.Sub);
2263 ig.Emit (OpCodes.Add);
2264 ((IStackStore) expr).Store (ec);
2265 } else if (eclass == ExprClass.PropertyAccess){
2266 throw new Exception ("Handle Properties here");
2267 } else if (eclass == ExprClass.IndexerAccess) {
2268 throw new Exception ("Handle Indexers here");
2270 Console.WriteLine ("Unknown exprclass: " + eclass);
2275 throw new Exception ("This should not happen: Operator = "
2276 + oper.ToString ());
2281 public override void EmitStatement (EmitContext ec)
2284 // FIXME: we should rewrite this code to generate
2285 // better code for ++ and -- as we know we wont need
2286 // the values on the stack
2289 ec.ig.Emit (OpCodes.Pop);
2293 public class Probe : Expression {
2294 public readonly string ProbeType;
2295 public readonly Operator Oper;
2299 public enum Operator {
2303 public Probe (Operator oper, Expression expr, string probe_type)
2306 ProbeType = probe_type;
2310 public Expression Expr {
2316 public override Expression DoResolve (EmitContext ec)
2318 probe_type = ec.TypeContainer.LookupType (ProbeType, false);
2320 if (probe_type == null)
2323 expr = expr.Resolve (ec);
2325 type = TypeManager.bool_type;
2326 eclass = ExprClass.Value;
2331 public override void Emit (EmitContext ec)
2333 ILGenerator ig = ec.ig;
2337 if (Oper == Operator.Is){
2338 ig.Emit (OpCodes.Isinst, probe_type);
2339 ig.Emit (OpCodes.Ldnull);
2340 ig.Emit (OpCodes.Cgt_Un);
2342 ig.Emit (OpCodes.Isinst, probe_type);
2348 // This represents a typecast in the source language.
2350 // FIXME: Cast expressions have an unusual set of parsing
2351 // rules, we need to figure those out.
2353 public class Cast : Expression {
2358 public Cast (string cast_type, Expression expr, Location loc)
2360 this.target_type = cast_type;
2365 public string TargetType {
2371 public Expression Expr {
2380 public override Expression DoResolve (EmitContext ec)
2382 expr = expr.Resolve (ec);
2386 type = ec.TypeContainer.LookupType (target_type, false);
2387 eclass = ExprClass.Value;
2392 expr = ConvertExplicit (ec, expr, type, loc);
2397 public override void Emit (EmitContext ec)
2400 // This one will never happen
2402 throw new Exception ("Should not happen");
2406 public class Binary : Expression {
2407 public enum Operator {
2408 Multiply, Division, Modulus,
2409 Addition, Subtraction,
2410 LeftShift, RightShift,
2411 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
2412 Equality, Inequality,
2421 Expression left, right;
2423 ArrayList Arguments;
2427 public Binary (Operator oper, Expression left, Expression right, Location loc)
2435 public Operator Oper {
2444 public Expression Left {
2453 public Expression Right {
2464 // Returns a stringified representation of the Operator
2469 case Operator.Multiply:
2471 case Operator.Division:
2473 case Operator.Modulus:
2475 case Operator.Addition:
2477 case Operator.Subtraction:
2479 case Operator.LeftShift:
2481 case Operator.RightShift:
2483 case Operator.LessThan:
2485 case Operator.GreaterThan:
2487 case Operator.LessThanOrEqual:
2489 case Operator.GreaterThanOrEqual:
2491 case Operator.Equality:
2493 case Operator.Inequality:
2495 case Operator.BitwiseAnd:
2497 case Operator.BitwiseOr:
2499 case Operator.ExclusiveOr:
2501 case Operator.LogicalOr:
2503 case Operator.LogicalAnd:
2507 return oper.ToString ();
2510 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
2512 if (expr.Type == target_type)
2515 return ConvertImplicit (ec, expr, target_type, new Location (-1));
2519 // Note that handling the case l == Decimal || r == Decimal
2520 // is taken care of by the Step 1 Operator Overload resolution.
2522 void DoNumericPromotions (EmitContext ec, Type l, Type r)
2524 if (l == TypeManager.double_type || r == TypeManager.double_type){
2526 // If either operand is of type double, the other operand is
2527 // conveted to type double.
2529 if (r != TypeManager.double_type)
2530 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
2531 if (l != TypeManager.double_type)
2532 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
2534 type = TypeManager.double_type;
2535 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
2537 // if either operand is of type float, th eother operand is
2538 // converd to type float.
2540 if (r != TypeManager.double_type)
2541 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
2542 if (l != TypeManager.double_type)
2543 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
2544 type = TypeManager.float_type;
2545 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
2549 // If either operand is of type ulong, the other operand is
2550 // converted to type ulong. or an error ocurrs if the other
2551 // operand is of type sbyte, short, int or long
2554 if (l == TypeManager.uint64_type){
2555 if (r != TypeManager.uint64_type && right is IntLiteral){
2556 e = TryImplicitIntConversion (l, (IntLiteral) right);
2562 if (left is IntLiteral){
2563 e = TryImplicitIntConversion (r, (IntLiteral) left);
2570 if ((other == TypeManager.sbyte_type) ||
2571 (other == TypeManager.short_type) ||
2572 (other == TypeManager.int32_type) ||
2573 (other == TypeManager.int64_type)){
2574 string oper = OperName ();
2576 Error (34, loc, "Operator `" + OperName ()
2577 + "' is ambiguous on operands of type `"
2578 + TypeManager.CSharpName (l) + "' "
2579 + "and `" + TypeManager.CSharpName (r)
2582 type = TypeManager.uint64_type;
2583 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
2585 // If either operand is of type long, the other operand is converted
2588 if (l != TypeManager.int64_type)
2589 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
2590 if (r != TypeManager.int64_type)
2591 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
2593 type = TypeManager.int64_type;
2594 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
2596 // If either operand is of type uint, and the other
2597 // operand is of type sbyte, short or int, othe operands are
2598 // converted to type long.
2602 if (l == TypeManager.uint32_type)
2604 else if (r == TypeManager.uint32_type)
2607 if ((other == TypeManager.sbyte_type) ||
2608 (other == TypeManager.short_type) ||
2609 (other == TypeManager.int32_type)){
2610 left = ForceConversion (ec, left, TypeManager.int64_type);
2611 right = ForceConversion (ec, right, TypeManager.int64_type);
2612 type = TypeManager.int64_type;
2615 // if either operand is of type uint, the other
2616 // operand is converd to type uint
2618 left = ForceConversion (ec, left, TypeManager.uint32_type);
2619 right = ForceConversion (ec, right, TypeManager.uint32_type);
2620 type = TypeManager.uint32_type;
2622 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2623 if (l != TypeManager.decimal_type)
2624 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
2625 if (r != TypeManager.decimal_type)
2626 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
2628 type = TypeManager.decimal_type;
2630 Expression l_tmp, r_tmp;
2632 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
2633 if (l_tmp == null) {
2639 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
2640 if (r_tmp == null) {
2646 type = TypeManager.int32_type;
2653 "Operator " + OperName () + " cannot be applied to operands of type `" +
2654 TypeManager.CSharpName (left.Type) + "' and `" +
2655 TypeManager.CSharpName (right.Type) + "'");
2659 Expression CheckShiftArguments (EmitContext ec)
2663 Type r = right.Type;
2665 e = ForceConversion (ec, right, TypeManager.int32_type);
2672 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
2673 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
2674 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
2675 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
2685 Expression ResolveOperator (EmitContext ec)
2688 Type r = right.Type;
2691 // Step 1: Perform Operator Overload location
2693 Expression left_expr, right_expr;
2695 string op = "op_" + oper;
2697 left_expr = MemberLookup (ec, l, op, false, loc);
2698 if (left_expr == null && l.BaseType != null)
2699 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
2701 right_expr = MemberLookup (ec, r, op, false, loc);
2702 if (right_expr == null && r.BaseType != null)
2703 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
2705 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2707 if (union != null) {
2708 Arguments = new ArrayList ();
2709 Arguments.Add (new Argument (left, Argument.AType.Expression));
2710 Arguments.Add (new Argument (right, Argument.AType.Expression));
2712 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
2713 if (method != null) {
2714 MethodInfo mi = (MethodInfo) method;
2715 type = mi.ReturnType;
2724 // Step 2: Default operations on CLI native types.
2727 // Only perform numeric promotions on:
2728 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2730 if (oper == Operator.Addition){
2732 // If any of the arguments is a string, cast to string
2734 if (l == TypeManager.string_type){
2735 if (r == TypeManager.string_type){
2737 method = TypeManager.string_concat_string_string;
2740 method = TypeManager.string_concat_object_object;
2741 right = ConvertImplicit (ec, right,
2742 TypeManager.object_type, loc);
2744 type = TypeManager.string_type;
2746 Arguments = new ArrayList ();
2747 Arguments.Add (new Argument (left, Argument.AType.Expression));
2748 Arguments.Add (new Argument (right, Argument.AType.Expression));
2752 } else if (r == TypeManager.string_type){
2754 method = TypeManager.string_concat_object_object;
2755 Arguments = new ArrayList ();
2756 Arguments.Add (new Argument (left, Argument.AType.Expression));
2757 Arguments.Add (new Argument (right, Argument.AType.Expression));
2759 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
2760 type = TypeManager.string_type;
2766 // FIXME: is Delegate operator + (D x, D y) handled?
2770 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2771 return CheckShiftArguments (ec);
2773 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2774 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2777 type = TypeManager.bool_type;
2782 // We are dealing with numbers
2785 DoNumericPromotions (ec, l, r);
2787 if (left == null || right == null)
2791 if (oper == Operator.BitwiseAnd ||
2792 oper == Operator.BitwiseOr ||
2793 oper == Operator.ExclusiveOr){
2794 if (!((l == TypeManager.int32_type) ||
2795 (l == TypeManager.uint32_type) ||
2796 (l == TypeManager.int64_type) ||
2797 (l == TypeManager.uint64_type))){
2804 if (oper == Operator.Equality ||
2805 oper == Operator.Inequality ||
2806 oper == Operator.LessThanOrEqual ||
2807 oper == Operator.LessThan ||
2808 oper == Operator.GreaterThanOrEqual ||
2809 oper == Operator.GreaterThan){
2810 type = TypeManager.bool_type;
2816 public override Expression DoResolve (EmitContext ec)
2818 left = left.Resolve (ec);
2819 right = right.Resolve (ec);
2821 if (left == null || right == null)
2824 if (left.Type == null)
2825 throw new Exception (
2826 "Resolve returned non null, but did not set the type! (" +
2828 if (right.Type == null)
2829 throw new Exception (
2830 "Resolve returned non null, but did not set the type! (" +
2833 eclass = ExprClass.Value;
2835 return ResolveOperator (ec);
2838 public bool IsBranchable ()
2840 if (oper == Operator.Equality ||
2841 oper == Operator.Inequality ||
2842 oper == Operator.LessThan ||
2843 oper == Operator.GreaterThan ||
2844 oper == Operator.LessThanOrEqual ||
2845 oper == Operator.GreaterThanOrEqual){
2852 // This entry point is used by routines that might want
2853 // to emit a brfalse/brtrue after an expression, and instead
2854 // they could use a more compact notation.
2856 // Typically the code would generate l.emit/r.emit, followed
2857 // by the comparission and then a brtrue/brfalse. The comparissions
2858 // are sometimes inneficient (there are not as complete as the branches
2859 // look for the hacks in Emit using double ceqs).
2861 // So for those cases we provide EmitBranchable that can emit the
2862 // branch with the test
2864 public void EmitBranchable (EmitContext ec, int target)
2867 bool close_target = false;
2873 case Operator.Equality:
2875 opcode = OpCodes.Beq_S;
2877 opcode = OpCodes.Beq;
2880 case Operator.Inequality:
2882 opcode = OpCodes.Bne_Un_S;
2884 opcode = OpCodes.Bne_Un;
2887 case Operator.LessThan:
2889 opcode = OpCodes.Blt_S;
2891 opcode = OpCodes.Blt;
2894 case Operator.GreaterThan:
2896 opcode = OpCodes.Bgt_S;
2898 opcode = OpCodes.Bgt;
2901 case Operator.LessThanOrEqual:
2903 opcode = OpCodes.Ble_S;
2905 opcode = OpCodes.Ble;
2908 case Operator.GreaterThanOrEqual:
2910 opcode = OpCodes.Bge_S;
2912 opcode = OpCodes.Ble;
2916 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2917 + oper.ToString ());
2920 ec.ig.Emit (opcode, target);
2923 public override void Emit (EmitContext ec)
2925 ILGenerator ig = ec.ig;
2927 Type r = right.Type;
2930 if (method != null) {
2932 // Note that operators are static anyway
2934 if (Arguments != null)
2935 Invocation.EmitArguments (ec, method, Arguments);
2937 if (method is MethodInfo)
2938 ig.Emit (OpCodes.Call, (MethodInfo) method);
2940 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2949 case Operator.Multiply:
2951 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2952 opcode = OpCodes.Mul_Ovf;
2953 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2954 opcode = OpCodes.Mul_Ovf_Un;
2956 opcode = OpCodes.Mul;
2958 opcode = OpCodes.Mul;
2962 case Operator.Division:
2963 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2964 opcode = OpCodes.Div_Un;
2966 opcode = OpCodes.Div;
2969 case Operator.Modulus:
2970 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2971 opcode = OpCodes.Rem_Un;
2973 opcode = OpCodes.Rem;
2976 case Operator.Addition:
2978 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2979 opcode = OpCodes.Add_Ovf;
2980 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2981 opcode = OpCodes.Add_Ovf_Un;
2983 opcode = OpCodes.Mul;
2985 opcode = OpCodes.Add;
2988 case Operator.Subtraction:
2990 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2991 opcode = OpCodes.Sub_Ovf;
2992 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2993 opcode = OpCodes.Sub_Ovf_Un;
2995 opcode = OpCodes.Sub;
2997 opcode = OpCodes.Sub;
3000 case Operator.RightShift:
3001 opcode = OpCodes.Shr;
3004 case Operator.LeftShift:
3005 opcode = OpCodes.Shl;
3008 case Operator.Equality:
3009 opcode = OpCodes.Ceq;
3012 case Operator.Inequality:
3013 ec.ig.Emit (OpCodes.Ceq);
3014 ec.ig.Emit (OpCodes.Ldc_I4_0);
3016 opcode = OpCodes.Ceq;
3019 case Operator.LessThan:
3020 opcode = OpCodes.Clt;
3023 case Operator.GreaterThan:
3024 opcode = OpCodes.Cgt;
3027 case Operator.LessThanOrEqual:
3028 ec.ig.Emit (OpCodes.Cgt);
3029 ec.ig.Emit (OpCodes.Ldc_I4_0);
3031 opcode = OpCodes.Ceq;
3034 case Operator.GreaterThanOrEqual:
3035 ec.ig.Emit (OpCodes.Clt);
3036 ec.ig.Emit (OpCodes.Ldc_I4_1);
3038 opcode = OpCodes.Sub;
3041 case Operator.LogicalOr:
3042 case Operator.BitwiseOr:
3043 opcode = OpCodes.Or;
3046 case Operator.LogicalAnd:
3047 case Operator.BitwiseAnd:
3048 opcode = OpCodes.And;
3051 case Operator.ExclusiveOr:
3052 opcode = OpCodes.Xor;
3056 throw new Exception ("This should not happen: Operator = "
3057 + oper.ToString ());
3064 public class Conditional : Expression {
3065 Expression expr, trueExpr, falseExpr;
3068 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3071 this.trueExpr = trueExpr;
3072 this.falseExpr = falseExpr;
3076 public Expression Expr {
3082 public Expression TrueExpr {
3088 public Expression FalseExpr {
3094 public override Expression DoResolve (EmitContext ec)
3096 expr = expr.Resolve (ec);
3098 if (expr.Type != TypeManager.bool_type)
3099 expr = Expression.ConvertImplicitRequired (
3100 ec, expr, TypeManager.bool_type, loc);
3102 trueExpr = trueExpr.Resolve (ec);
3103 falseExpr = falseExpr.Resolve (ec);
3105 if (expr == null || trueExpr == null || falseExpr == null)
3108 if (trueExpr.Type == falseExpr.Type)
3109 type = trueExpr.Type;
3114 // First, if an implicit conversion exists from trueExpr
3115 // to falseExpr, then the result type is of type falseExpr.Type
3117 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
3119 type = falseExpr.Type;
3121 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
3122 type = trueExpr.Type;
3125 Error (173, loc, "The type of the conditional expression can " +
3126 "not be computed because there is no implicit conversion" +
3127 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3128 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3133 eclass = ExprClass.Value;
3137 public override void Emit (EmitContext ec)
3139 ILGenerator ig = ec.ig;
3140 Label false_target = ig.DefineLabel ();
3141 Label end_target = ig.DefineLabel ();
3144 ig.Emit (OpCodes.Brfalse, false_target);
3146 ig.Emit (OpCodes.Br, end_target);
3147 ig.MarkLabel (false_target);
3148 falseExpr.Emit (ec);
3149 ig.MarkLabel (end_target);
3154 // SimpleName expressions are initially formed of a single
3155 // word and it only happens at the beginning of the expression.
3157 // The expression will try to be bound to a Field, a Method
3158 // group or a Property. If those fail we pass the name to our
3159 // caller and the SimpleName is compounded to perform a type
3160 // lookup. The idea behind this process is that we want to avoid
3161 // creating a namespace map from the assemblies, as that requires
3162 // the GetExportedTypes function to be called and a hashtable to
3163 // be constructed which reduces startup time. If later we find
3164 // that this is slower, we should create a `NamespaceExpr' expression
3165 // that fully participates in the resolution process.
3167 // For example `System.Console.WriteLine' is decomposed into
3168 // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3170 // The first SimpleName wont produce a match on its own, so it will
3172 // MemberAccess (SimpleName ("System.Console"), "WriteLine").
3174 // System.Console will produce a TypeExpr match.
3176 // The downside of this is that we might be hitting `LookupType' too many
3177 // times with this scheme.
3179 public class SimpleName : Expression {
3180 public readonly string Name;
3181 public readonly Location Location;
3183 public SimpleName (string name, Location l)
3189 public static void Error120 (Location l, string name)
3193 "An object reference is required " +
3194 "for the non-static field `"+name+"'");
3198 // Checks whether we are trying to access an instance
3199 // property, method or field from a static body.
3201 Expression MemberStaticCheck (Expression e)
3203 if (e is FieldExpr){
3204 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3207 Error120 (Location, Name);
3210 } else if (e is MethodGroupExpr){
3211 MethodGroupExpr mg = (MethodGroupExpr) e;
3213 if (!mg.RemoveInstanceMethods ()){
3214 Error120 (Location, mg.Methods [0].Name);
3218 } else if (e is PropertyExpr){
3219 if (!((PropertyExpr) e).IsStatic){
3220 Error120 (Location, Name);
3229 // 7.5.2: Simple Names.
3231 // Local Variables and Parameters are handled at
3232 // parse time, so they never occur as SimpleNames.
3234 public override Expression DoResolve (EmitContext ec)
3239 // Stage 1: Performed by the parser (binding to local or parameters).
3243 // Stage 2: Lookup members
3245 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
3248 // Stage 3: Lookup symbol in the various namespaces.
3252 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
3253 return new TypeExpr (t);
3256 // Stage 3 part b: Lookup up if we are an alias to a type
3259 // Since we are cheating: we only do the Alias lookup for
3260 // namespaces if the name does not include any dots in it
3263 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
3264 // using NamespaceExprs (dunno how that fixes the alias
3265 // per-file though).
3267 // No match, maybe our parent can compose us
3268 // into something meaningful.
3273 // Step 2, continues here.
3277 if (e is FieldExpr){
3278 FieldExpr fe = (FieldExpr) e;
3280 if (!fe.FieldInfo.IsStatic)
3281 fe.InstanceExpression = new This (Location.Null);
3285 return MemberStaticCheck (e);
3290 public override void Emit (EmitContext ec)
3293 // If this is ever reached, then we failed to
3294 // find the name as a namespace
3297 Error (103, Location, "The name `" + Name +
3298 "' does not exist in the class `" +
3299 ec.TypeContainer.Name + "'");
3303 public class LocalTemporary : Expression, IStackStore, IMemoryLocation {
3304 LocalBuilder builder;
3306 public LocalTemporary (EmitContext ec, Type t)
3309 eclass = ExprClass.Value;
3310 builder = ec.GetTemporaryStorage (t);
3313 public override Expression DoResolve (EmitContext ec)
3318 public override void Emit (EmitContext ec)
3320 ec.ig.Emit (OpCodes.Ldloc, builder);
3323 public void Store (EmitContext ec)
3325 ec.ig.Emit (OpCodes.Stloc, builder);
3328 public void AddressOf (EmitContext ec)
3330 ec.ig.Emit (OpCodes.Ldloca, builder);
3334 public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
3335 public readonly string Name;
3336 public readonly Block Block;
3338 VariableInfo variable_info;
3340 public LocalVariableReference (Block block, string name)
3344 eclass = ExprClass.Variable;
3347 public VariableInfo VariableInfo {
3349 if (variable_info == null)
3350 variable_info = Block.GetVariableInfo (Name);
3351 return variable_info;
3355 public override Expression DoResolve (EmitContext ec)
3357 VariableInfo vi = VariableInfo;
3359 type = vi.VariableType;
3363 public override void Emit (EmitContext ec)
3365 VariableInfo vi = VariableInfo;
3366 ILGenerator ig = ec.ig;
3373 ig.Emit (OpCodes.Ldloc_0);
3377 ig.Emit (OpCodes.Ldloc_1);
3381 ig.Emit (OpCodes.Ldloc_2);
3385 ig.Emit (OpCodes.Ldloc_3);
3390 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3392 ig.Emit (OpCodes.Ldloc, idx);
3397 public static void Store (ILGenerator ig, int idx)
3401 ig.Emit (OpCodes.Stloc_0);
3405 ig.Emit (OpCodes.Stloc_1);
3409 ig.Emit (OpCodes.Stloc_2);
3413 ig.Emit (OpCodes.Stloc_3);
3418 ig.Emit (OpCodes.Stloc_S, (byte) idx);
3420 ig.Emit (OpCodes.Stloc, idx);
3425 public void Store (EmitContext ec)
3427 ILGenerator ig = ec.ig;
3428 VariableInfo vi = VariableInfo;
3432 // Funny seems the above generates optimal code for us, but
3433 // seems to take too long to generate what we need.
3434 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3439 public void AddressOf (EmitContext ec)
3441 VariableInfo vi = VariableInfo;
3448 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3450 ec.ig.Emit (OpCodes.Ldloca, idx);
3454 public class ParameterReference : Expression, IStackStore, IMemoryLocation {
3455 public readonly Parameters Pars;
3456 public readonly String Name;
3457 public readonly int Idx;
3460 public ParameterReference (Parameters pars, int idx, string name)
3465 eclass = ExprClass.Variable;
3468 public override Expression DoResolve (EmitContext ec)
3470 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
3481 public override void Emit (EmitContext ec)
3484 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3486 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3489 public void Store (EmitContext ec)
3492 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3494 ec.ig.Emit (OpCodes.Starg, arg_idx);
3498 public void AddressOf (EmitContext ec)
3501 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3503 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3508 // Used for arguments to New(), Invocation()
3510 public class Argument {
3517 public readonly AType Type;
3518 public Expression expr;
3520 public Argument (Expression expr, AType type)
3526 public Expression Expr {
3536 public bool Resolve (EmitContext ec)
3538 expr = expr.Resolve (ec);
3540 return expr != null;
3543 public void Emit (EmitContext ec)
3550 // Invocation of methods or delegates.
3552 public class Invocation : ExpressionStatement {
3553 public readonly ArrayList Arguments;
3554 public readonly Location Location;
3557 MethodBase method = null;
3559 static Hashtable method_parameter_cache;
3561 static Invocation ()
3563 method_parameter_cache = new Hashtable ();
3567 // arguments is an ArrayList, but we do not want to typecast,
3568 // as it might be null.
3570 // FIXME: only allow expr to be a method invocation or a
3571 // delegate invocation (7.5.5)
3573 public Invocation (Expression expr, ArrayList arguments, Location l)
3576 Arguments = arguments;
3580 public Expression Expr {
3587 // Returns the Parameters (a ParameterData interface) for the
3590 public static ParameterData GetParameterData (MethodBase mb)
3592 object pd = method_parameter_cache [mb];
3596 return (ParameterData) pd;
3599 ip = TypeContainer.LookupParametersByBuilder (mb);
3601 method_parameter_cache [mb] = ip;
3603 return (ParameterData) ip;
3605 ParameterInfo [] pi = mb.GetParameters ();
3606 ReflectionParameters rp = new ReflectionParameters (pi);
3607 method_parameter_cache [mb] = rp;
3609 return (ParameterData) rp;
3614 // Tells whether a user defined conversion from Type `from' to
3615 // Type `to' exists.
3617 // FIXME: we could implement a cache here.
3619 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
3621 // Locate user-defined implicit operators
3625 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
3628 MethodGroupExpr me = (MethodGroupExpr) mg;
3630 for (int i = me.Methods.Length; i > 0;) {
3632 MethodBase mb = me.Methods [i];
3633 ParameterData pd = GetParameterData (mb);
3635 if (from == pd.ParameterType (0))
3640 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
3643 MethodGroupExpr me = (MethodGroupExpr) mg;
3645 for (int i = me.Methods.Length; i > 0;) {
3647 MethodBase mb = me.Methods [i];
3648 MethodInfo mi = (MethodInfo) mb;
3650 if (mi.ReturnType == to)
3659 // Determines "better conversion" as specified in 7.4.2.3
3660 // Returns : 1 if a->p is better
3661 // 0 if a->q or neither is better
3663 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
3666 Type argument_type = a.Expr.Type;
3667 Expression argument_expr = a.Expr;
3669 if (argument_type == null)
3670 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3675 if (argument_type == p)
3678 if (argument_type == q)
3682 // Now probe whether an implicit constant expression conversion
3685 // An implicit constant expression conversion permits the following
3688 // * A constant-expression of type `int' can be converted to type
3689 // sbyte, byute, short, ushort, uint, ulong provided the value of
3690 // of the expression is withing the range of the destination type.
3692 // * A constant-expression of type long can be converted to type
3693 // ulong, provided the value of the constant expression is not negative
3695 // FIXME: Note that this assumes that constant folding has
3696 // taken place. We dont do constant folding yet.
3699 if (argument_expr is IntLiteral){
3700 IntLiteral ei = (IntLiteral) argument_expr;
3701 int value = ei.Value;
3703 if (p == TypeManager.sbyte_type){
3704 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3706 } else if (p == TypeManager.byte_type){
3707 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3709 } else if (p == TypeManager.short_type){
3710 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3712 } else if (p == TypeManager.ushort_type){
3713 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3715 } else if (p == TypeManager.uint32_type){
3717 // we can optimize this case: a positive int32
3718 // always fits on a uint32
3722 } else if (p == TypeManager.uint64_type){
3724 // we can optimize this case: a positive int32
3725 // always fits on a uint64
3730 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3731 LongLiteral ll = (LongLiteral) argument_expr;
3733 if (p == TypeManager.uint64_type){
3744 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
3746 tmp = ConvertImplicit (ec, argument_expr, p, loc);
3755 if (ConversionExists (ec, p, q, loc) == true &&
3756 ConversionExists (ec, q, p, loc) == false)
3759 if (p == TypeManager.sbyte_type)
3760 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3761 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3764 if (p == TypeManager.short_type)
3765 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3766 q == TypeManager.uint64_type)
3769 if (p == TypeManager.int32_type)
3770 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3773 if (p == TypeManager.int64_type)
3774 if (q == TypeManager.uint64_type)
3781 // Determines "Better function" and returns an integer indicating :
3782 // 0 if candidate ain't better
3783 // 1 if candidate is better than the current best match
3785 static int BetterFunction (EmitContext ec, ArrayList args,
3786 MethodBase candidate, MethodBase best,
3787 bool use_standard, Location loc)
3789 ParameterData candidate_pd = GetParameterData (candidate);
3790 ParameterData best_pd;
3796 argument_count = args.Count;
3798 if (candidate_pd.Count == 0 && argument_count == 0)
3802 if (candidate_pd.Count == argument_count) {
3804 for (int j = argument_count; j > 0;) {
3807 Argument a = (Argument) args [j];
3809 x = BetterConversion (
3810 ec, a, candidate_pd.ParameterType (j), null,
3826 best_pd = GetParameterData (best);
3828 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3829 int rating1 = 0, rating2 = 0;
3831 for (int j = argument_count; j > 0;) {
3835 Argument a = (Argument) args [j];
3837 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
3838 best_pd.ParameterType (j), use_standard, loc);
3839 y = BetterConversion (ec, a, best_pd.ParameterType (j),
3840 candidate_pd.ParameterType (j), use_standard,
3847 if (rating1 > rating2)
3856 public static string FullMethodDesc (MethodBase mb)
3858 StringBuilder sb = new StringBuilder (mb.Name);
3859 ParameterData pd = GetParameterData (mb);
3861 int count = pd.Count;
3864 for (int i = count; i > 0; ) {
3867 sb.Append (TypeManager.CSharpName (pd.ParameterType (count - i - 1)));
3873 return sb.ToString ();
3876 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3878 MemberInfo [] miset;
3879 MethodGroupExpr union;
3881 if (mg1 != null && mg2 != null) {
3883 MethodGroupExpr left_set = null, right_set = null;
3884 int length1 = 0, length2 = 0;
3886 left_set = (MethodGroupExpr) mg1;
3887 length1 = left_set.Methods.Length;
3889 right_set = (MethodGroupExpr) mg2;
3890 length2 = right_set.Methods.Length;
3892 ArrayList common = new ArrayList ();
3894 for (int i = 0; i < left_set.Methods.Length; i++) {
3895 for (int j = 0; j < right_set.Methods.Length; j++) {
3896 if (left_set.Methods [i] == right_set.Methods [j])
3897 common.Add (left_set.Methods [i]);
3901 miset = new MemberInfo [length1 + length2 - common.Count];
3903 left_set.Methods.CopyTo (miset, 0);
3907 for (int j = 0; j < right_set.Methods.Length; j++)
3908 if (!common.Contains (right_set.Methods [j]))
3909 miset [length1 + k++] = right_set.Methods [j];
3911 union = new MethodGroupExpr (miset);
3915 } else if (mg1 == null && mg2 != null) {
3917 MethodGroupExpr me = (MethodGroupExpr) mg2;
3919 miset = new MemberInfo [me.Methods.Length];
3920 me.Methods.CopyTo (miset, 0);
3922 union = new MethodGroupExpr (miset);
3926 } else if (mg2 == null && mg1 != null) {
3928 MethodGroupExpr me = (MethodGroupExpr) mg1;
3930 miset = new MemberInfo [me.Methods.Length];
3931 me.Methods.CopyTo (miset, 0);
3933 union = new MethodGroupExpr (miset);
3942 // Find the Applicable Function Members (7.4.2.1)
3944 // me: Method Group expression with the members to select.
3945 // it might contain constructors or methods (or anything
3946 // that maps to a method).
3948 // Arguments: ArrayList containing resolved Argument objects.
3950 // loc: The location if we want an error to be reported, or a Null
3951 // location for "probing" purposes.
3953 // inside_user_defined: controls whether OverloadResolve should use the
3954 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
3956 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3957 // that is the best match of me on Arguments.
3960 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3961 ArrayList Arguments, Location loc,
3964 ArrayList afm = new ArrayList ();
3965 int best_match_idx = -1;
3966 MethodBase method = null;
3969 for (int i = me.Methods.Length; i > 0; ){
3971 MethodBase candidate = me.Methods [i];
3974 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
3980 method = me.Methods [best_match_idx];
3984 if (Arguments == null)
3987 argument_count = Arguments.Count;
3991 // Now we see if we can at least find a method with the same number of arguments
3992 // and then try doing implicit conversion on the arguments
3993 if (best_match_idx == -1) {
3995 for (int i = me.Methods.Length; i > 0;) {
3997 MethodBase mb = me.Methods [i];
3998 pd = GetParameterData (mb);
4000 if (pd.Count == argument_count) {
4002 method = me.Methods [best_match_idx];
4013 // And now convert implicitly, each argument to the required type
4015 pd = GetParameterData (method);
4017 for (int j = argument_count; j > 0;) {
4019 Argument a = (Argument) Arguments [j];
4020 Expression a_expr = a.Expr;
4021 Type parameter_type = pd.ParameterType (j);
4023 if (a_expr.Type != parameter_type){
4027 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
4030 conv = ConvertImplicit (ec, a_expr, parameter_type,
4034 if (!Location.IsNull (loc)) {
4036 "The best overloaded match for method '" + FullMethodDesc (method) +
4037 "' has some invalid arguments");
4039 "Argument " + (j+1) +
4040 ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
4041 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
4046 // Update the argument with the implicit conversion
4056 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4057 ArrayList Arguments, Location loc)
4059 return OverloadResolve (ec, me, Arguments, loc, false);
4062 public override Expression DoResolve (EmitContext ec)
4064 bool IsDelegate = false;
4066 // First, resolve the expression that is used to
4067 // trigger the invocation
4069 this.expr = expr.Resolve (ec);
4070 if (this.expr == null)
4073 Type expr_type = null;
4074 if (!(this.expr is MethodGroupExpr)) {
4075 expr_type = this.expr.Type;
4076 IsDelegate = TypeManager.IsDelegateType (expr_type);
4080 return (new DelegateInvocation (this.expr, Arguments, Location)).Resolve (ec);
4083 if (!(this.expr is MethodGroupExpr)){
4084 report118 (Location, this.expr, "method group");
4089 // Next, evaluate all the expressions in the argument list
4091 if (Arguments != null){
4092 for (int i = Arguments.Count; i > 0;){
4094 Argument a = (Argument) Arguments [i];
4096 if (!a.Resolve (ec))
4101 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
4104 if (method == null){
4105 Error (-6, Location,
4106 "Could not find any applicable function for this argument list");
4110 if (method is MethodInfo)
4111 type = ((MethodInfo)method).ReturnType;
4113 eclass = ExprClass.Value;
4117 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
4121 if (Arguments != null)
4122 top = Arguments.Count;
4126 for (int i = 0; i < top; i++){
4127 Argument a = (Argument) Arguments [i];
4133 public static void EmitCall (EmitContext ec,
4134 bool is_static, Expression instance_expr,
4135 MethodBase method, ArrayList Arguments)
4137 ILGenerator ig = ec.ig;
4138 bool struct_call = false;
4142 // If this is ourselves, push "this"
4144 if (instance_expr == null){
4145 ig.Emit (OpCodes.Ldarg_0);
4148 // Push the instance expression
4150 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
4155 // If the expression implements IMemoryLocation, then
4156 // we can optimize and use AddressOf on the
4159 // If not we have to use some temporary storage for
4161 if (instance_expr is IMemoryLocation)
4162 ((IMemoryLocation) instance_expr).AddressOf (ec);
4164 Type t = instance_expr.Type;
4166 instance_expr.Emit (ec);
4167 LocalBuilder temp = ec.GetTemporaryStorage (t);
4168 ig.Emit (OpCodes.Stloc, temp);
4169 ig.Emit (OpCodes.Ldloca, temp);
4172 instance_expr.Emit (ec);
4176 if (Arguments != null)
4177 EmitArguments (ec, method, Arguments);
4179 if (is_static || struct_call){
4180 if (method is MethodInfo)
4181 ig.Emit (OpCodes.Call, (MethodInfo) method);
4183 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4185 if (method is MethodInfo)
4186 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
4188 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
4192 public override void Emit (EmitContext ec)
4194 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
4195 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
4198 public override void EmitStatement (EmitContext ec)
4203 // Pop the return value if there is one
4205 if (method is MethodInfo){
4206 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
4207 ec.ig.Emit (OpCodes.Pop);
4212 public class New : ExpressionStatement {
4219 public readonly NType NewType;
4220 public readonly ArrayList Arguments;
4221 public readonly string RequestedType;
4223 // These are for the case when we have an array
4224 public readonly string Rank;
4225 public readonly ArrayList Initializers;
4228 MethodBase method = null;
4231 // If set, the new expression is for a value_target, and
4232 // we will not leave anything on the stack.
4234 Expression value_target;
4236 public New (string requested_type, ArrayList arguments, Location loc)
4238 RequestedType = requested_type;
4239 Arguments = arguments;
4240 NewType = NType.Object;
4244 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
4246 RequestedType = requested_type;
4248 Initializers = initializers;
4249 NewType = NType.Array;
4252 Arguments = new ArrayList ();
4254 foreach (Expression e in exprs)
4255 Arguments.Add (new Argument (e, Argument.AType.Expression));
4259 public static string FormLookupType (string base_type, int idx_count, string rank)
4261 StringBuilder sb = new StringBuilder (base_type);
4266 for (int i = 1; i < idx_count; i++)
4270 return sb.ToString ();
4273 public Expression ValueTypeVariable {
4275 return value_target;
4279 value_target = value;
4283 public override Expression DoResolve (EmitContext ec)
4285 if (NewType == NType.Object) {
4287 type = ec.TypeContainer.LookupType (RequestedType, false);
4292 bool IsDelegate = TypeManager.IsDelegateType (type);
4295 return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
4299 ml = MemberLookup (ec, type, ".ctor", false,
4300 MemberTypes.Constructor, AllBindingsFlags, Location);
4302 bool is_struct = false;
4303 is_struct = type.IsSubclassOf (TypeManager.value_type);
4305 if (! (ml is MethodGroupExpr)){
4307 report118 (Location, ml, "method group");
4313 if (Arguments != null){
4314 for (int i = Arguments.Count; i > 0;){
4316 Argument a = (Argument) Arguments [i];
4318 if (!a.Resolve (ec))
4323 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4324 Arguments, Location);
4328 if (method == null && !is_struct) {
4329 Error (-6, Location,
4330 "New invocation: Can not find a constructor for " +
4331 "this argument list");
4335 eclass = ExprClass.Value;
4340 if (NewType == NType.Array) {
4341 throw new Exception ("Finish array creation");
4348 // This DoEmit can be invoked in two contexts:
4349 // * As a mechanism that will leave a value on the stack (new object)
4350 // * As one that wont (init struct)
4352 // You can control whether a value is required on the stack by passing
4353 // need_value_on_stack. The code *might* leave a value on the stack
4354 // so it must be popped manually
4356 // Returns whether a value is left on the stack
4358 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4360 if (method == null){
4361 IMemoryLocation ml = (IMemoryLocation) value_target;
4365 Invocation.EmitArguments (ec, method, Arguments);
4366 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4371 // It must be a value type, sanity check
4373 if (value_target != null){
4374 ec.ig.Emit (OpCodes.Initobj, type);
4376 if (need_value_on_stack){
4377 value_target.Emit (ec);
4383 throw new Exception ("No method and no value type");
4386 public override void Emit (EmitContext ec)
4391 public override void EmitStatement (EmitContext ec)
4393 if (DoEmit (ec, false))
4394 ec.ig.Emit (OpCodes.Pop);
4399 // Represents the `this' construct
4401 public class This : Expression, IStackStore, IMemoryLocation {
4404 public This (Location loc)
4409 public override Expression DoResolve (EmitContext ec)
4411 eclass = ExprClass.Variable;
4412 type = ec.TypeContainer.TypeBuilder;
4415 Report.Error (26, loc,
4416 "Keyword this not valid in static code");
4423 public Expression DoResolveLValue (EmitContext ec)
4427 if (ec.TypeContainer is Class){
4428 Report.Error (1604, loc, "Cannot assign to `this'");
4435 public override void Emit (EmitContext ec)
4437 ec.ig.Emit (OpCodes.Ldarg_0);
4440 public void Store (EmitContext ec)
4442 ec.ig.Emit (OpCodes.Starg, 0);
4445 public void AddressOf (EmitContext ec)
4447 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4452 // Implements the typeof operator
4454 public class TypeOf : Expression {
4455 public readonly string QueriedType;
4458 public TypeOf (string queried_type)
4460 QueriedType = queried_type;
4463 public override Expression DoResolve (EmitContext ec)
4465 typearg = ec.TypeContainer.LookupType (QueriedType, false);
4467 if (typearg == null)
4470 type = TypeManager.type_type;
4471 eclass = ExprClass.Type;
4475 public override void Emit (EmitContext ec)
4477 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4478 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4482 public class SizeOf : Expression {
4483 public readonly string QueriedType;
4485 public SizeOf (string queried_type)
4487 this.QueriedType = queried_type;
4490 public override Expression DoResolve (EmitContext ec)
4492 // FIXME: Implement;
4493 throw new Exception ("Unimplemented");
4497 public override void Emit (EmitContext ec)
4499 throw new Exception ("Implement me");
4503 public class MemberAccess : Expression {
4504 public readonly string Identifier;
4506 Expression member_lookup;
4509 public MemberAccess (Expression expr, string id, Location l)
4516 public Expression Expr {
4522 void error176 (Location loc, string name)
4524 Report.Error (176, loc, "Static member `" +
4525 name + "' cannot be accessed " +
4526 "with an instance reference, qualify with a " +
4527 "type name instead");
4530 public override Expression DoResolve (EmitContext ec)
4532 expr = expr.Resolve (ec);
4537 if (expr is SimpleName){
4538 SimpleName child_expr = (SimpleName) expr;
4540 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4542 return expr.Resolve (ec);
4545 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4550 if (member_lookup is MethodGroupExpr){
4551 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4556 if (expr is TypeExpr){
4557 if (!mg.RemoveInstanceMethods ()){
4558 SimpleName.Error120 (loc, mg.Methods [0].Name);
4562 return member_lookup;
4566 // Instance.MethodGroup
4568 if (!mg.RemoveStaticMethods ()){
4569 error176 (loc, mg.Methods [0].Name);
4573 mg.InstanceExpression = expr;
4575 return member_lookup;
4578 if (member_lookup is FieldExpr){
4579 FieldExpr fe = (FieldExpr) member_lookup;
4581 if (expr is TypeExpr){
4582 if (!fe.FieldInfo.IsStatic){
4583 error176 (loc, fe.FieldInfo.Name);
4586 return member_lookup;
4588 if (fe.FieldInfo.IsStatic){
4589 error176 (loc, fe.FieldInfo.Name);
4592 fe.InstanceExpression = expr;
4598 if (member_lookup is PropertyExpr){
4599 PropertyExpr pe = (PropertyExpr) member_lookup;
4601 if (expr is TypeExpr){
4603 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4609 error176 (loc, pe.PropertyInfo.Name);
4612 pe.InstanceExpression = expr;
4618 Console.WriteLine ("Support for " + member_lookup + " is not present yet");
4619 Environment.Exit (0);
4623 public override void Emit (EmitContext ec)
4625 throw new Exception ("Should not happen I think");
4631 // Fully resolved expression that evaluates to a type
4633 public class TypeExpr : Expression {
4634 public TypeExpr (Type t)
4637 eclass = ExprClass.Type;
4640 override public Expression DoResolve (EmitContext ec)
4645 override public void Emit (EmitContext ec)
4647 throw new Exception ("Implement me");
4652 // MethodGroup Expression.
4654 // This is a fully resolved expression that evaluates to a type
4656 public class MethodGroupExpr : Expression {
4657 public MethodBase [] Methods;
4658 Expression instance_expression = null;
4660 public MethodGroupExpr (MemberInfo [] mi)
4662 Methods = new MethodBase [mi.Length];
4663 mi.CopyTo (Methods, 0);
4664 eclass = ExprClass.MethodGroup;
4667 public MethodGroupExpr (ArrayList l)
4669 Methods = new MethodBase [l.Count];
4671 l.CopyTo (Methods, 0);
4672 eclass = ExprClass.MethodGroup;
4676 // `A method group may have associated an instance expression'
4678 public Expression InstanceExpression {
4680 return instance_expression;
4684 instance_expression = value;
4688 override public Expression DoResolve (EmitContext ec)
4693 override public void Emit (EmitContext ec)
4695 throw new Exception ("This should never be reached");
4698 bool RemoveMethods (bool keep_static)
4700 ArrayList smethods = new ArrayList ();
4701 int top = Methods.Length;
4704 for (i = 0; i < top; i++){
4705 MethodBase mb = Methods [i];
4707 if (mb.IsStatic == keep_static)
4711 if (smethods.Count == 0)
4714 Methods = new MethodBase [smethods.Count];
4715 smethods.CopyTo (Methods, 0);
4721 // Removes any instance methods from the MethodGroup, returns
4722 // false if the resulting set is empty.
4724 public bool RemoveInstanceMethods ()
4726 return RemoveMethods (true);
4730 // Removes any static methods from the MethodGroup, returns
4731 // false if the resulting set is empty.
4733 public bool RemoveStaticMethods ()
4735 return RemoveMethods (false);
4740 // Fully resolved expression that evaluates to a Field
4742 public class FieldExpr : Expression, IStackStore, IMemoryLocation {
4743 public readonly FieldInfo FieldInfo;
4744 public Expression InstanceExpression;
4747 public FieldExpr (FieldInfo fi, Location l)
4750 eclass = ExprClass.Variable;
4751 type = fi.FieldType;
4755 override public Expression DoResolve (EmitContext ec)
4757 if (!FieldInfo.IsStatic){
4758 if (InstanceExpression == null){
4759 throw new Exception ("non-static FieldExpr without instance var\n" +
4760 "You have to assign the Instance variable\n" +
4761 "Of the FieldExpr to set this\n");
4764 InstanceExpression = InstanceExpression.Resolve (ec);
4765 if (InstanceExpression == null)
4772 public Expression DoResolveLValue (EmitContext ec)
4774 if (!FieldInfo.IsInitOnly)
4778 // InitOnly fields can only be assigned in constructors
4781 if (ec.IsConstructor)
4784 Report.Error (191, loc,
4785 "Readonly field can not be assigned outside " +
4786 "of constructor or variable initializer");
4791 override public void Emit (EmitContext ec)
4793 ILGenerator ig = ec.ig;
4795 if (FieldInfo.IsStatic)
4796 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4798 InstanceExpression.Emit (ec);
4800 ig.Emit (OpCodes.Ldfld, FieldInfo);
4804 public void Store (EmitContext ec)
4806 if (FieldInfo.IsStatic)
4807 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
4809 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
4812 public void AddressOf (EmitContext ec)
4814 if (FieldInfo.IsStatic)
4815 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
4817 InstanceExpression.Emit (ec);
4818 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
4824 // Expression that evaluates to a Property. The Assign class
4825 // might set the `Value' expression if we are in an assignment.
4827 // This is not an LValue because we need to re-write the expression, we
4828 // can not take data from the stack and store it.
4830 public class PropertyExpr : ExpressionStatement {
4831 public readonly PropertyInfo PropertyInfo;
4832 public readonly bool IsStatic;
4833 MethodInfo [] Accessors;
4836 Expression instance_expr;
4839 public PropertyExpr (PropertyInfo pi, Location l)
4842 eclass = ExprClass.PropertyAccess;
4845 Accessors = TypeManager.GetAccessors (pi);
4847 if (Accessors != null)
4848 for (int i = 0; i < Accessors.Length; i++){
4849 if (Accessors [i] != null)
4850 if (Accessors [i].IsStatic)
4854 Accessors = new MethodInfo [2];
4856 type = pi.PropertyType;
4860 // Controls the Value of the PropertyExpr. If the value
4861 // is null, then the property is being used in a `read' mode.
4862 // otherwise the property is used in assignment mode.
4864 // The value is set to a fully resolved type by assign.
4866 public Expression Value {
4877 // The instance expression associated with this expression
4879 public Expression InstanceExpression {
4881 instance_expr = value;
4885 return instance_expr;
4889 public bool VerifyAssignable ()
4891 if (!PropertyInfo.CanWrite){
4892 Report.Error (200, loc,
4893 "The property `" + PropertyInfo.Name +
4894 "' can not be assigned to, as it has not set accessor");
4901 override public Expression DoResolve (EmitContext ec)
4903 if (!PropertyInfo.CanRead){
4904 Report.Error (154, loc,
4905 "The property `" + PropertyInfo.Name +
4906 "' can not be used in " +
4907 "this context because it lacks a get accessor");
4914 override public void Emit (EmitContext ec)
4917 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
4919 Argument arg = new Argument (value, Argument.AType.Expression);
4920 ArrayList args = new ArrayList ();
4923 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
4927 override public void EmitStatement (EmitContext ec)
4931 ec.ig.Emit (OpCodes.Pop);
4937 // Fully resolved expression that evaluates to a Expression
4939 public class EventExpr : Expression {
4940 public readonly EventInfo EventInfo;
4943 public EventExpr (EventInfo ei, Location loc)
4947 eclass = ExprClass.EventAccess;
4950 override public Expression DoResolve (EmitContext ec)
4952 // We are born in resolved state.
4956 override public void Emit (EmitContext ec)
4958 throw new Exception ("Implement me");
4959 // FIXME: Implement.
4963 public class CheckedExpr : Expression {
4965 public Expression Expr;
4967 public CheckedExpr (Expression e)
4972 public override Expression DoResolve (EmitContext ec)
4974 Expr = Expr.Resolve (ec);
4979 eclass = Expr.ExprClass;
4984 public override void Emit (EmitContext ec)
4986 bool last_check = ec.CheckState;
4988 ec.CheckState = true;
4990 ec.CheckState = last_check;
4995 public class UnCheckedExpr : Expression {
4997 public Expression Expr;
4999 public UnCheckedExpr (Expression e)
5004 public override Expression DoResolve (EmitContext ec)
5006 Expr = Expr.Resolve (ec);
5011 eclass = Expr.ExprClass;
5016 public override void Emit (EmitContext ec)
5018 bool last_check = ec.CheckState;
5020 ec.CheckState = false;
5022 ec.CheckState = last_check;
5027 public class ElementAccess : Expression {
5029 public ArrayList Arguments;
5030 public Expression Expr;
5031 public Location loc;
5033 public ElementAccess (Expression e, ArrayList e_list, Location l)
5037 Arguments = new ArrayList ();
5038 foreach (Expression tmp in e_list)
5039 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
5044 bool CommonResolve (EmitContext ec)
5046 Expr = Expr.Resolve (ec);
5051 if (Arguments == null)
5054 for (int i = Arguments.Count; i > 0;){
5056 Argument a = (Argument) Arguments [i];
5058 if (!a.Resolve (ec))
5065 public override Expression DoResolve (EmitContext ec)
5067 if (!CommonResolve (ec))
5071 // We perform some simple tests, and then to "split" the emit and store
5072 // code we create an instance of a different class, and return that.
5074 // I am experimenting with this pattern.
5076 if (Expr.Type == TypeManager.array_type)
5077 return (new ArrayAccess (this)).Resolve (ec);
5079 return (new IndexerAccess (this)).Resolve (ec);
5082 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5084 if (!CommonResolve (ec))
5087 if (Expr.Type == TypeManager.array_type)
5088 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
5090 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
5093 public override void Emit (EmitContext ec)
5095 throw new Exception ("Should never be reached");
5099 public class ArrayAccess : Expression, IStackStore {
5101 // Points to our "data" repository
5105 public ArrayAccess (ElementAccess ea_data)
5108 eclass = ExprClass.Variable;
5111 // FIXME: Figure out the type here
5115 Expression CommonResolve (EmitContext ec)
5120 public override Expression DoResolve (EmitContext ec)
5122 if (ea.Expr.ExprClass != ExprClass.Variable) {
5123 report118 (ea.loc, ea.Expr, "variable");
5127 throw new Exception ("Implement me");
5130 public void Store (EmitContext ec)
5132 throw new Exception ("Implement me !");
5135 public override void Emit (EmitContext ec)
5137 throw new Exception ("Implement me !");
5142 public ArrayList getters, setters;
5143 static Hashtable map;
5147 map = new Hashtable ();
5150 Indexers (MemberInfo [] mi)
5152 foreach (PropertyInfo property in mi){
5153 MethodInfo get, set;
5155 get = property.GetGetMethod (true);
5157 if (getters == null)
5158 getters = new ArrayList ();
5163 set = property.GetSetMethod (true);
5165 if (setters == null)
5166 setters = new ArrayList ();
5172 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
5174 Indexers ix = (Indexers) map [t];
5175 string p_name = TypeManager.IndexerPropertyName (t);
5180 MemberInfo [] mi = tm.FindMembers (
5181 t, MemberTypes.Property,
5182 BindingFlags.Public | BindingFlags.Instance,
5183 Type.FilterName, p_name);
5185 if (mi == null || mi.Length == 0){
5186 Report.Error (21, loc,
5187 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5188 "any indexers defined");
5192 ix = new Indexers (mi);
5199 public class IndexerAccess : Expression {
5201 // Points to our "data" repository
5204 MethodInfo get, set;
5206 ArrayList set_arguments;
5208 public IndexerAccess (ElementAccess ea_data)
5211 eclass = ExprClass.Value;
5214 public bool VerifyAssignable (Expression source)
5216 throw new Exception ("Implement me!");
5219 public override Expression DoResolve (EmitContext ec)
5221 Type indexer_type = ea.Expr.Type;
5224 // Step 1: Query for all `Item' *properties*. Notice
5225 // that the actual methods are pointed from here.
5227 // This is a group of properties, piles of them.
5230 ilist = Indexers.GetIndexersForType (
5231 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5233 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5234 get = (MethodInfo) Invocation.OverloadResolve (
5235 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5238 Report.Error (154, ea.loc,
5239 "indexer can not be used in this context, because " +
5240 "it lacks a `get' accessor");
5244 type = get.ReturnType;
5245 eclass = ExprClass.Value;
5249 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5251 Type indexer_type = ea.Expr.Type;
5252 Type right_type = right_side.Type;
5255 ilist = Indexers.GetIndexersForType (
5256 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5258 Console.WriteLine ("ilist = " + ilist);
5260 Console.WriteLine ("ilist.setters = " + ilist.setters);
5261 if (ilist.setters != null)
5262 Console.WriteLine ("count = " + ilist.setters.Count);
5264 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5265 set_arguments = (ArrayList) ea.Arguments.Clone ();
5266 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5268 set = (MethodInfo) Invocation.OverloadResolve (
5269 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5273 Report.Error (200, ea.loc,
5274 "indexer X.this [" + TypeManager.CSharpName (right_type) +
5275 "] lacks a `set' accessor");
5279 type = TypeManager.void_type;
5280 eclass = ExprClass.Value;
5284 public override void Emit (EmitContext ec)
5286 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5289 public void EmitSet (EmitContext ec, Expression expr)
5291 throw new Exception ("Implement me!");
5295 public class BaseAccess : Expression {
5297 public enum BaseAccessType {
5302 public readonly BaseAccessType BAType;
5303 public readonly string Member;
5304 public readonly ArrayList Arguments;
5306 public BaseAccess (BaseAccessType t, string member, ArrayList args)
5314 public override Expression DoResolve (EmitContext ec)
5316 // FIXME: Implement;
5317 throw new Exception ("Unimplemented");
5321 public override void Emit (EmitContext ec)
5323 throw new Exception ("Unimplemented");
5328 // This class exists solely to pass the Type around and to be a dummy
5329 // that can be passed to the conversion functions (this is used by
5330 // foreach implementation to typecast the object return value from
5331 // get_Current into the proper type. All code has been generated and
5332 // we only care about the side effect conversions to be performed
5335 public class EmptyExpression : Expression {
5336 public EmptyExpression ()
5338 type = TypeManager.object_type;
5339 eclass = ExprClass.Value;
5342 public override Expression DoResolve (EmitContext ec)
5347 public override void Emit (EmitContext ec)
5349 // nothing, as we only exist to not do anything.
5353 public class UserCast : Expression {
5357 public UserCast (MethodInfo method, Expression source)
5359 this.method = method;
5360 this.source = source;
5361 type = method.ReturnType;
5362 eclass = ExprClass.Value;
5365 public override Expression DoResolve (EmitContext ec)
5368 // We are born fully resolved
5373 public override void Emit (EmitContext ec)
5375 ILGenerator ig = ec.ig;
5379 if (method is MethodInfo)
5380 ig.Emit (OpCodes.Call, (MethodInfo) method);
5382 ig.Emit (OpCodes.Call, (ConstructorInfo) method);