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 if (TypeManager.ImplementsInterface (expr_type, target_type))
391 return new EmptyCast (expr, target_type);
396 // from any interface type S to interface-type T.
397 if (expr_type.IsInterface && target_type.IsInterface) {
399 if (TypeManager.ImplementsInterface (expr_type, target_type))
400 return new EmptyCast (expr, target_type);
405 // from an array-type S to an array-type of type T
406 if (expr_type.IsArray && target_type.IsArray) {
407 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
409 Type expr_element_type = expr_type.GetElementType ();
410 Type target_element_type = target_type.GetElementType ();
412 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
413 if (StandardConversionExists (expr_element_type,
414 target_element_type))
415 return new EmptyCast (expr, target_type);
420 // from an array-type to System.Array
421 if (expr_type.IsArray && target_type == TypeManager.array_type)
422 return new EmptyCast (expr, target_type);
424 // from any delegate type to System.Delegate
425 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
426 target_type == TypeManager.delegate_type)
427 return new EmptyCast (expr, target_type);
429 // from any array-type or delegate type into System.ICloneable.
430 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
431 if (target_type == TypeManager.icloneable_type)
432 return new EmptyCast (expr, target_type);
434 // from the null type to any reference-type.
435 if (expr is NullLiteral)
436 return new EmptyCast (expr, target_type);
446 // Handles expressions like this: decimal d; d = 1;
447 // and changes them into: decimal d; d = new System.Decimal (1);
449 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
451 ArrayList args = new ArrayList ();
453 args.Add (new Argument (expr, Argument.AType.Expression));
455 Expression ne = new New (target.FullName, args,
458 return ne.Resolve (ec);
462 // Implicit Numeric Conversions.
464 // expr is the expression to convert, returns a new expression of type
465 // target_type or null if an implicit conversion is not possible.
468 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
469 Type target_type, Location loc)
471 Type expr_type = expr.Type;
474 // Attempt to do the implicit constant expression conversions
476 if (expr is IntLiteral){
479 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
482 } else if (expr is LongLiteral){
484 // Try the implicit constant expression conversion
485 // from long to ulong, instead of a nice routine,
488 if (((LongLiteral) expr).Value > 0)
489 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
492 if (expr_type == TypeManager.sbyte_type){
494 // From sbyte to short, int, long, float, double.
496 if (target_type == TypeManager.int32_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
498 if (target_type == TypeManager.int64_type)
499 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
500 if (target_type == TypeManager.double_type)
501 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
502 if (target_type == TypeManager.float_type)
503 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
504 if (target_type == TypeManager.short_type)
505 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
506 if (target_type == TypeManager.decimal_type)
507 return InternalTypeConstructor (ec, expr, target_type);
508 } else if (expr_type == TypeManager.byte_type){
510 // From byte to short, ushort, int, uint, long, ulong, float, double
512 if ((target_type == TypeManager.short_type) ||
513 (target_type == TypeManager.ushort_type) ||
514 (target_type == TypeManager.int32_type) ||
515 (target_type == TypeManager.uint32_type))
516 return new EmptyCast (expr, target_type);
518 if (target_type == TypeManager.uint64_type)
519 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
520 if (target_type == TypeManager.int64_type)
521 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
523 if (target_type == TypeManager.float_type)
524 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
525 if (target_type == TypeManager.double_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
527 if (target_type == TypeManager.decimal_type)
528 return InternalTypeConstructor (ec, expr, target_type);
529 } else if (expr_type == TypeManager.short_type){
531 // From short to int, long, float, double
533 if (target_type == TypeManager.int32_type)
534 return new EmptyCast (expr, target_type);
535 if (target_type == TypeManager.int64_type)
536 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
537 if (target_type == TypeManager.double_type)
538 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
539 if (target_type == TypeManager.float_type)
540 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
541 if (target_type == TypeManager.decimal_type)
542 return InternalTypeConstructor (ec, expr, target_type);
543 } else if (expr_type == TypeManager.ushort_type){
545 // From ushort to int, uint, long, ulong, float, double
547 if (target_type == TypeManager.uint32_type)
548 return new EmptyCast (expr, target_type);
550 if (target_type == TypeManager.uint64_type)
551 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
552 if (target_type == TypeManager.int32_type)
553 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
554 if (target_type == TypeManager.int64_type)
555 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
556 if (target_type == TypeManager.double_type)
557 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
558 if (target_type == TypeManager.float_type)
559 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
560 if (target_type == TypeManager.decimal_type)
561 return InternalTypeConstructor (ec, expr, target_type);
562 } else if (expr_type == TypeManager.int32_type){
564 // From int to long, float, double
566 if (target_type == TypeManager.int64_type)
567 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
568 if (target_type == TypeManager.double_type)
569 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
570 if (target_type == TypeManager.float_type)
571 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
572 if (target_type == TypeManager.decimal_type)
573 return InternalTypeConstructor (ec, expr, target_type);
574 } else if (expr_type == TypeManager.uint32_type){
576 // From uint to long, ulong, float, double
578 if (target_type == TypeManager.int64_type)
579 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
580 if (target_type == TypeManager.uint64_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
582 if (target_type == TypeManager.double_type)
583 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
585 if (target_type == TypeManager.float_type)
586 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
588 if (target_type == TypeManager.decimal_type)
589 return InternalTypeConstructor (ec, expr, target_type);
590 } else if ((expr_type == TypeManager.uint64_type) ||
591 (expr_type == TypeManager.int64_type)){
593 // From long/ulong to float, double
595 if (target_type == TypeManager.double_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
598 if (target_type == TypeManager.float_type)
599 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
601 if (target_type == TypeManager.decimal_type)
602 return InternalTypeConstructor (ec, expr, target_type);
603 } else if (expr_type == TypeManager.char_type){
605 // From char to ushort, int, uint, long, ulong, float, double
607 if ((target_type == TypeManager.ushort_type) ||
608 (target_type == TypeManager.int32_type) ||
609 (target_type == TypeManager.uint32_type))
610 return new EmptyCast (expr, target_type);
611 if (target_type == TypeManager.uint64_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
613 if (target_type == TypeManager.int64_type)
614 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
615 if (target_type == TypeManager.float_type)
616 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
617 if (target_type == TypeManager.double_type)
618 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
619 if (target_type == TypeManager.decimal_type)
620 return InternalTypeConstructor (ec, expr, target_type);
621 } else if (expr_type == TypeManager.float_type){
625 if (target_type == TypeManager.double_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
633 // Determines if a standard implicit conversion exists from
634 // expr_type to target_type
636 public static bool StandardConversionExists (Type expr_type, Type target_type)
638 if (expr_type == target_type)
641 // First numeric conversions
643 if (expr_type == TypeManager.sbyte_type){
645 // From sbyte to short, int, long, float, double.
647 if ((target_type == TypeManager.int32_type) ||
648 (target_type == TypeManager.int64_type) ||
649 (target_type == TypeManager.double_type) ||
650 (target_type == TypeManager.float_type) ||
651 (target_type == TypeManager.short_type) ||
652 (target_type == TypeManager.decimal_type))
655 } else if (expr_type == TypeManager.byte_type){
657 // From byte to short, ushort, int, uint, long, ulong, float, double
659 if ((target_type == TypeManager.short_type) ||
660 (target_type == TypeManager.ushort_type) ||
661 (target_type == TypeManager.int32_type) ||
662 (target_type == TypeManager.uint32_type) ||
663 (target_type == TypeManager.uint64_type) ||
664 (target_type == TypeManager.int64_type) ||
665 (target_type == TypeManager.float_type) ||
666 (target_type == TypeManager.double_type) ||
667 (target_type == TypeManager.decimal_type))
670 } else if (expr_type == TypeManager.short_type){
672 // From short to int, long, float, double
674 if ((target_type == TypeManager.int32_type) ||
675 (target_type == TypeManager.int64_type) ||
676 (target_type == TypeManager.double_type) ||
677 (target_type == TypeManager.float_type) ||
678 (target_type == TypeManager.decimal_type))
681 } else if (expr_type == TypeManager.ushort_type){
683 // From ushort to int, uint, long, ulong, float, double
685 if ((target_type == TypeManager.uint32_type) ||
686 (target_type == TypeManager.uint64_type) ||
687 (target_type == TypeManager.int32_type) ||
688 (target_type == TypeManager.int64_type) ||
689 (target_type == TypeManager.double_type) ||
690 (target_type == TypeManager.float_type) ||
691 (target_type == TypeManager.decimal_type))
694 } else if (expr_type == TypeManager.int32_type){
696 // From int to long, float, double
698 if ((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.uint32_type){
706 // From uint to long, ulong, float, double
708 if ((target_type == TypeManager.int64_type) ||
709 (target_type == TypeManager.uint64_type) ||
710 (target_type == TypeManager.double_type) ||
711 (target_type == TypeManager.float_type) ||
712 (target_type == TypeManager.decimal_type))
715 } else if ((expr_type == TypeManager.uint64_type) ||
716 (expr_type == TypeManager.int64_type)) {
718 // From long/ulong to float, double
720 if ((target_type == TypeManager.double_type) ||
721 (target_type == TypeManager.float_type) ||
722 (target_type == TypeManager.decimal_type))
725 } else if (expr_type == TypeManager.char_type){
727 // From char to ushort, int, uint, long, ulong, float, double
729 if ((target_type == TypeManager.ushort_type) ||
730 (target_type == TypeManager.int32_type) ||
731 (target_type == TypeManager.uint32_type) ||
732 (target_type == TypeManager.uint64_type) ||
733 (target_type == TypeManager.int64_type) ||
734 (target_type == TypeManager.float_type) ||
735 (target_type == TypeManager.double_type) ||
736 (target_type == TypeManager.decimal_type))
739 } else if (expr_type == TypeManager.float_type){
743 if (target_type == TypeManager.double_type)
747 // Next reference conversions
749 if (target_type == TypeManager.object_type) {
750 if ((expr_type.IsClass) ||
751 (expr_type.IsValueType))
754 } else if (expr_type.IsSubclassOf (target_type)) {
758 // from any class-type S to any interface-type T.
759 if (expr_type.IsClass && target_type.IsInterface)
762 // from any interface type S to interface-type T.
763 // FIXME : Is it right to use IsAssignableFrom ?
764 if (expr_type.IsInterface && target_type.IsInterface)
765 if (target_type.IsAssignableFrom (expr_type))
768 // from an array-type S to an array-type of type T
769 if (expr_type.IsArray && target_type.IsArray) {
770 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
772 Type expr_element_type = expr_type.GetElementType ();
773 Type target_element_type = target_type.GetElementType ();
775 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
776 if (StandardConversionExists (expr_element_type,
777 target_element_type))
782 // from an array-type to System.Array
783 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
786 // from any delegate type to System.Delegate
787 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
788 target_type == TypeManager.delegate_type)
789 if (target_type.IsAssignableFrom (expr_type))
792 // from any array-type or delegate type into System.ICloneable.
793 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
794 if (target_type == TypeManager.icloneable_type)
797 // from the null type to any reference-type.
798 // FIXME : How do we do this ?
806 // Finds "most encompassed type" according to the spec (13.4.2)
807 // amongst the methods in the MethodGroupExpr which convert from a
808 // type encompassing source_type
810 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
814 for (int i = me.Methods.Length; i > 0; ) {
817 MethodBase mb = me.Methods [i];
818 ParameterData pd = Invocation.GetParameterData (mb);
819 Type param_type = pd.ParameterType (0);
821 if (StandardConversionExists (source_type, param_type)) {
825 if (StandardConversionExists (param_type, best))
834 // Finds "most encompassing type" according to the spec (13.4.2)
835 // amongst the methods in the MethodGroupExpr which convert to a
836 // type encompassed by target_type
838 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
842 for (int i = me.Methods.Length; i > 0; ) {
845 MethodInfo mi = (MethodInfo) me.Methods [i];
846 Type ret_type = mi.ReturnType;
848 if (StandardConversionExists (ret_type, target)) {
852 if (!StandardConversionExists (ret_type, best))
864 // User-defined Implicit conversions
866 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
867 Type target, Location loc)
869 return UserDefinedConversion (ec, source, target, loc, false);
873 // User-defined Explicit conversions
875 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
876 Type target, Location loc)
878 return UserDefinedConversion (ec, source, target, loc, true);
882 // User-defined conversions
884 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
885 Type target, Location loc,
886 bool look_for_explicit)
888 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
889 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
891 MethodBase method = null;
892 Type source_type = source.Type;
896 // If we have a boolean type, we need to check for the True operator
898 // FIXME : How does the False operator come into the picture ?
899 // FIXME : This doesn't look complete and very correct !
900 if (target == TypeManager.bool_type)
903 op_name = "op_Implicit";
905 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
907 if (source_type.BaseType != null)
908 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
910 mg3 = MemberLookup (ec, target, op_name, false, loc);
912 if (target.BaseType != null)
913 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
915 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
916 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
918 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
920 MethodGroupExpr union4 = null;
922 if (look_for_explicit) {
924 op_name = "op_Explicit";
926 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
928 if (source_type.BaseType != null)
929 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
931 mg7 = MemberLookup (ec, target, op_name, false, loc);
933 if (target.BaseType != null)
934 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
936 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
937 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
939 union4 = Invocation.MakeUnionSet (union5, union6);
942 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
946 Type most_specific_source, most_specific_target;
948 most_specific_source = FindMostEncompassedType (union, source_type);
949 if (most_specific_source == null)
952 most_specific_target = FindMostEncompassingType (union, target);
953 if (most_specific_target == null)
958 for (int i = union.Methods.Length; i > 0;) {
961 MethodBase mb = union.Methods [i];
962 ParameterData pd = Invocation.GetParameterData (mb);
963 MethodInfo mi = (MethodInfo) union.Methods [i];
965 if (pd.ParameterType (0) == most_specific_source &&
966 mi.ReturnType == most_specific_target) {
972 if (method == null || count > 1) {
973 Report.Error (-11, loc, "Ambiguous user defined conversion");
978 // This will do the conversion to the best match that we
979 // found. Now we need to perform an implict standard conversion
980 // if the best match was not the type that we were requested
983 if (look_for_explicit)
984 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
986 source = ConvertImplicitStandard (ec, source,
987 most_specific_source, loc);
992 e = new UserCast ((MethodInfo) method, source);
994 if (e.Type != target){
995 if (!look_for_explicit)
996 e = ConvertImplicitStandard (ec, e, target, loc);
998 e = ConvertExplicitStandard (ec, e, target, loc);
1009 // Converts implicitly the resolved expression `expr' into the
1010 // `target_type'. It returns a new expression that can be used
1011 // in a context that expects a `target_type'.
1013 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1014 Type target_type, Location loc)
1016 Type expr_type = expr.Type;
1019 if (expr_type == target_type)
1022 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1026 e = ImplicitReferenceConversion (expr, target_type);
1030 e = ImplicitUserConversion (ec, expr, target_type, loc);
1034 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1035 IntLiteral i = (IntLiteral) expr;
1038 return new EmptyCast (expr, target_type);
1046 // Attempts to apply the `Standard Implicit
1047 // Conversion' rules to the expression `expr' into
1048 // the `target_type'. It returns a new expression
1049 // that can be used in a context that expects a
1052 // This is different from `ConvertImplicit' in that the
1053 // user defined implicit conversions are excluded.
1055 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1056 Type target_type, Location loc)
1058 Type expr_type = expr.Type;
1061 if (expr_type == target_type)
1064 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1068 e = ImplicitReferenceConversion (expr, target_type);
1072 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1073 IntLiteral i = (IntLiteral) expr;
1076 return new EmptyCast (expr, target_type);
1081 // Attemps to perform an implict constant conversion of the IntLiteral
1082 // into a different data type using casts (See Implicit Constant
1083 // Expression Conversions)
1085 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1087 int value = il.Value;
1089 if (target_type == TypeManager.sbyte_type){
1090 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1092 } else if (target_type == TypeManager.byte_type){
1093 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1095 } else if (target_type == TypeManager.short_type){
1096 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1098 } else if (target_type == TypeManager.ushort_type){
1099 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1101 } else if (target_type == TypeManager.uint32_type){
1103 // we can optimize this case: a positive int32
1104 // always fits on a uint32
1108 } else if (target_type == TypeManager.uint64_type){
1110 // we can optimize this case: a positive int32
1111 // always fits on a uint64. But we need an opcode
1115 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1122 // Attemptes to implicityly convert `target' into `type', using
1123 // ConvertImplicit. If there is no implicit conversion, then
1124 // an error is signaled
1126 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1127 Type type, Location loc)
1131 e = ConvertImplicit (ec, target, type, loc);
1135 string msg = "Can not convert implicitly from `"+
1136 TypeManager.CSharpName (target.Type) + "' to `" +
1137 TypeManager.CSharpName (type) + "'";
1139 Error (29, loc, msg);
1145 // Performs the explicit numeric conversions
1147 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1150 Type expr_type = expr.Type;
1152 if (expr_type == TypeManager.sbyte_type){
1154 // From sbyte to byte, ushort, uint, ulong, char
1156 if (target_type == TypeManager.byte_type)
1157 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1158 if (target_type == TypeManager.ushort_type)
1159 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1160 if (target_type == TypeManager.uint32_type)
1161 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1162 if (target_type == TypeManager.uint64_type)
1163 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1164 if (target_type == TypeManager.char_type)
1165 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1166 } else if (expr_type == TypeManager.byte_type){
1168 // From byte to sbyte and char
1170 if (target_type == TypeManager.sbyte_type)
1171 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1172 if (target_type == TypeManager.char_type)
1173 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1174 } else if (expr_type == TypeManager.short_type){
1176 // From short to sbyte, byte, ushort, uint, ulong, char
1178 if (target_type == TypeManager.sbyte_type)
1179 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1180 if (target_type == TypeManager.byte_type)
1181 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1182 if (target_type == TypeManager.ushort_type)
1183 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1184 if (target_type == TypeManager.uint32_type)
1185 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1186 if (target_type == TypeManager.uint64_type)
1187 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1188 if (target_type == TypeManager.char_type)
1189 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1190 } else if (expr_type == TypeManager.ushort_type){
1192 // From ushort to sbyte, byte, short, char
1194 if (target_type == TypeManager.sbyte_type)
1195 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1196 if (target_type == TypeManager.byte_type)
1197 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1198 if (target_type == TypeManager.short_type)
1199 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1200 if (target_type == TypeManager.char_type)
1201 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1202 } else if (expr_type == TypeManager.int32_type){
1204 // From int to sbyte, byte, short, ushort, uint, ulong, char
1206 if (target_type == TypeManager.sbyte_type)
1207 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1208 if (target_type == TypeManager.byte_type)
1209 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1210 if (target_type == TypeManager.short_type)
1211 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1212 if (target_type == TypeManager.ushort_type)
1213 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1214 if (target_type == TypeManager.uint32_type)
1215 return new EmptyCast (expr, target_type);
1216 if (target_type == TypeManager.uint64_type)
1217 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1218 if (target_type == TypeManager.char_type)
1219 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1220 } else if (expr_type == TypeManager.uint32_type){
1222 // From uint to sbyte, byte, short, ushort, int, char
1224 if (target_type == TypeManager.sbyte_type)
1225 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1226 if (target_type == TypeManager.byte_type)
1227 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1228 if (target_type == TypeManager.short_type)
1229 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1230 if (target_type == TypeManager.ushort_type)
1231 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1232 if (target_type == TypeManager.int32_type)
1233 return new EmptyCast (expr, target_type);
1234 if (target_type == TypeManager.char_type)
1235 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1236 } else if (expr_type == TypeManager.int64_type){
1238 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1240 if (target_type == TypeManager.sbyte_type)
1241 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1242 if (target_type == TypeManager.byte_type)
1243 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1244 if (target_type == TypeManager.short_type)
1245 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1246 if (target_type == TypeManager.ushort_type)
1247 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1248 if (target_type == TypeManager.int32_type)
1249 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1250 if (target_type == TypeManager.uint32_type)
1251 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1252 if (target_type == TypeManager.uint64_type)
1253 return new EmptyCast (expr, target_type);
1254 if (target_type == TypeManager.char_type)
1255 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1256 } else if (expr_type == TypeManager.uint64_type){
1258 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1260 if (target_type == TypeManager.sbyte_type)
1261 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1262 if (target_type == TypeManager.byte_type)
1263 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1264 if (target_type == TypeManager.short_type)
1265 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1266 if (target_type == TypeManager.ushort_type)
1267 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1268 if (target_type == TypeManager.int32_type)
1269 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1270 if (target_type == TypeManager.uint32_type)
1271 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1272 if (target_type == TypeManager.int64_type)
1273 return new EmptyCast (expr, target_type);
1274 if (target_type == TypeManager.char_type)
1275 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1276 } else if (expr_type == TypeManager.char_type){
1278 // From char to sbyte, byte, short
1280 if (target_type == TypeManager.sbyte_type)
1281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1282 if (target_type == TypeManager.byte_type)
1283 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1284 if (target_type == TypeManager.short_type)
1285 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1286 } else if (expr_type == TypeManager.float_type){
1288 // From float to sbyte, byte, short,
1289 // ushort, int, uint, long, ulong, char
1292 if (target_type == TypeManager.sbyte_type)
1293 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1294 if (target_type == TypeManager.byte_type)
1295 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1296 if (target_type == TypeManager.short_type)
1297 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1298 if (target_type == TypeManager.ushort_type)
1299 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1300 if (target_type == TypeManager.int32_type)
1301 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1302 if (target_type == TypeManager.uint32_type)
1303 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1304 if (target_type == TypeManager.int64_type)
1305 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1306 if (target_type == TypeManager.uint64_type)
1307 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1308 if (target_type == TypeManager.char_type)
1309 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1310 if (target_type == TypeManager.decimal_type)
1311 return InternalTypeConstructor (ec, expr, target_type);
1312 } else if (expr_type == TypeManager.double_type){
1314 // From double to byte, byte, short,
1315 // ushort, int, uint, long, ulong,
1316 // char, float or decimal
1318 if (target_type == TypeManager.sbyte_type)
1319 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1320 if (target_type == TypeManager.byte_type)
1321 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1322 if (target_type == TypeManager.short_type)
1323 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1324 if (target_type == TypeManager.ushort_type)
1325 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1326 if (target_type == TypeManager.int32_type)
1327 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1328 if (target_type == TypeManager.uint32_type)
1329 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1330 if (target_type == TypeManager.int64_type)
1331 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1332 if (target_type == TypeManager.uint64_type)
1333 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1334 if (target_type == TypeManager.char_type)
1335 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1336 if (target_type == TypeManager.float_type)
1337 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1338 if (target_type == TypeManager.decimal_type)
1339 return InternalTypeConstructor (ec, expr, target_type);
1342 // decimal is taken care of by the op_Explicit methods.
1348 // Returns whether an explicit reference conversion can be performed
1349 // from source_type to target_type
1351 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1353 bool target_is_value_type = target_type.IsValueType;
1355 if (source_type == target_type)
1359 // From object to any reference type
1361 if (source_type == TypeManager.object_type && !target_is_value_type)
1365 // From any class S to any class-type T, provided S is a base class of T
1367 if (target_type.IsSubclassOf (source_type))
1371 // From any interface type S to any interface T provided S is not derived from T
1373 if (source_type.IsInterface && target_type.IsInterface){
1374 if (!target_type.IsSubclassOf (source_type))
1379 // From any class type S to any interface T, provides S is not sealed
1380 // and provided S does not implement T.
1382 if (target_type.IsInterface && !source_type.IsSealed &&
1383 !target_type.IsAssignableFrom (source_type))
1387 // From any interface-type S to to any class type T, provided T is not
1388 // sealed, or provided T implements S.
1390 if (source_type.IsInterface &&
1391 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1394 // From an array type S with an element type Se to an array type T with an
1395 // element type Te provided all the following are true:
1396 // * S and T differe only in element type, in other words, S and T
1397 // have the same number of dimensions.
1398 // * Both Se and Te are reference types
1399 // * An explicit referenc conversions exist from Se to Te
1401 if (source_type.IsArray && target_type.IsArray) {
1402 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1404 Type source_element_type = source_type.GetElementType ();
1405 Type target_element_type = target_type.GetElementType ();
1407 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1408 if (ExplicitReferenceConversionExists (source_element_type,
1409 target_element_type))
1415 // From System.Array to any array-type
1416 if (source_type == TypeManager.array_type &&
1417 target_type.IsSubclassOf (TypeManager.array_type)){
1422 // From System delegate to any delegate-type
1424 if (source_type == TypeManager.delegate_type &&
1425 target_type.IsSubclassOf (TypeManager.delegate_type))
1429 // From ICloneable to Array or Delegate types
1431 if (source_type == TypeManager.icloneable_type &&
1432 (target_type == TypeManager.array_type ||
1433 target_type == TypeManager.delegate_type))
1440 // Implements Explicit Reference conversions
1442 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1444 Type source_type = source.Type;
1445 bool target_is_value_type = target_type.IsValueType;
1448 // From object to any reference type
1450 if (source_type == TypeManager.object_type && !target_is_value_type)
1451 return new ClassCast (source, target_type);
1455 // From any class S to any class-type T, provided S is a base class of T
1457 if (target_type.IsSubclassOf (source_type))
1458 return new ClassCast (source, target_type);
1461 // From any interface type S to any interface T provided S is not derived from T
1463 if (source_type.IsInterface && target_type.IsInterface){
1465 if (TypeManager.ImplementsInterface (source_type, target_type))
1468 return new ClassCast (source, target_type);
1472 // From any class type S to any interface T, provides S is not sealed
1473 // and provided S does not implement T.
1475 if (target_type.IsInterface && !source_type.IsSealed) {
1477 if (TypeManager.ImplementsInterface (source_type, target_type))
1480 return new ClassCast (source, target_type);
1485 // From any interface-type S to to any class type T, provided T is not
1486 // sealed, or provided T implements S.
1488 if (source_type.IsInterface) {
1490 if (target_type.IsSealed)
1493 if (TypeManager.ImplementsInterface (target_type, source_type))
1494 return new ClassCast (source, target_type);
1499 // From an array type S with an element type Se to an array type T with an
1500 // element type Te provided all the following are true:
1501 // * S and T differe only in element type, in other words, S and T
1502 // have the same number of dimensions.
1503 // * Both Se and Te are reference types
1504 // * An explicit referenc conversions exist from Se to Te
1506 if (source_type.IsArray && target_type.IsArray) {
1507 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1509 Type source_element_type = source_type.GetElementType ();
1510 Type target_element_type = target_type.GetElementType ();
1512 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1513 if (ExplicitReferenceConversionExists (source_element_type,
1514 target_element_type))
1515 return new ClassCast (source, target_type);
1520 // From System.Array to any array-type
1521 if (source_type == TypeManager.array_type &&
1522 target_type.IsSubclassOf (TypeManager.array_type)){
1523 return new ClassCast (source, target_type);
1527 // From System delegate to any delegate-type
1529 if (source_type == TypeManager.delegate_type &&
1530 target_type.IsSubclassOf (TypeManager.delegate_type))
1531 return new ClassCast (source, target_type);
1534 // From ICloneable to Array or Delegate types
1536 if (source_type == TypeManager.icloneable_type &&
1537 (target_type == TypeManager.array_type ||
1538 target_type == TypeManager.delegate_type))
1539 return new ClassCast (source, target_type);
1545 // Performs an explicit conversion of the expression `expr' whose
1546 // type is expr.Type to `target_type'.
1548 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1549 Type target_type, Location loc)
1551 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1556 ne = ConvertNumericExplicit (ec, expr, target_type);
1560 ne = ConvertReferenceExplicit (expr, target_type);
1564 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1568 Report.Error (30, loc, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1569 + TypeManager.CSharpName (target_type) + "'");
1574 // Same as ConverExplicit, only it doesn't include user defined conversions
1576 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1577 Type target_type, Location l)
1579 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1584 ne = ConvertNumericExplicit (ec, expr, target_type);
1588 ne = ConvertReferenceExplicit (expr, target_type);
1592 Report.Error (30, l, "Cannot convert type '" +
1593 TypeManager.CSharpName (expr.Type) + "' to '" +
1594 TypeManager.CSharpName (target_type) + "'");
1598 static string ExprClassName (ExprClass c)
1601 case ExprClass.Invalid:
1603 case ExprClass.Value:
1605 case ExprClass.Variable:
1607 case ExprClass.Namespace:
1609 case ExprClass.Type:
1611 case ExprClass.MethodGroup:
1612 return "method group";
1613 case ExprClass.PropertyAccess:
1614 return "property access";
1615 case ExprClass.EventAccess:
1616 return "event access";
1617 case ExprClass.IndexerAccess:
1618 return "indexer access";
1619 case ExprClass.Nothing:
1622 throw new Exception ("Should not happen");
1626 // Reports that we were expecting `expr' to be of class `expected'
1628 protected void report118 (Location loc, Expression expr, string expected)
1630 string kind = "Unknown";
1633 kind = ExprClassName (expr.ExprClass);
1635 Error (118, loc, "Expression denotes a '" + kind +
1636 "' where an " + expected + " was expected");
1641 // This is just a base class for expressions that can
1642 // appear on statements (invocations, object creation,
1643 // assignments, post/pre increment and decrement). The idea
1644 // being that they would support an extra Emition interface that
1645 // does not leave a result on the stack.
1648 public abstract class ExpressionStatement : Expression {
1651 // Requests the expression to be emitted in a `statement'
1652 // context. This means that no new value is left on the
1653 // stack after invoking this method (constrasted with
1654 // Emit that will always leave a value on the stack).
1656 public abstract void EmitStatement (EmitContext ec);
1660 // This kind of cast is used to encapsulate the child
1661 // whose type is child.Type into an expression that is
1662 // reported to return "return_type". This is used to encapsulate
1663 // expressions which have compatible types, but need to be dealt
1664 // at higher levels with.
1666 // For example, a "byte" expression could be encapsulated in one
1667 // of these as an "unsigned int". The type for the expression
1668 // would be "unsigned int".
1672 public class EmptyCast : Expression {
1673 protected Expression child;
1675 public EmptyCast (Expression child, Type return_type)
1677 ExprClass = child.ExprClass;
1682 public override Expression DoResolve (EmitContext ec)
1684 // This should never be invoked, we are born in fully
1685 // initialized state.
1690 public override void Emit (EmitContext ec)
1697 // This kind of cast is used to encapsulate Value Types in objects.
1699 // The effect of it is to box the value type emitted by the previous
1702 public class BoxedCast : EmptyCast {
1704 public BoxedCast (Expression expr)
1705 : base (expr, TypeManager.object_type)
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)
1720 ec.ig.Emit (OpCodes.Box, child.Type);
1725 // This kind of cast is used to encapsulate a child expression
1726 // that can be trivially converted to a target type using one or
1727 // two opcodes. The opcodes are passed as arguments.
1729 public class OpcodeCast : EmptyCast {
1733 public OpcodeCast (Expression child, Type return_type, OpCode op)
1734 : base (child, return_type)
1738 second_valid = false;
1741 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1742 : base (child, return_type)
1747 second_valid = true;
1750 public override Expression DoResolve (EmitContext ec)
1752 // This should never be invoked, we are born in fully
1753 // initialized state.
1758 public override void Emit (EmitContext ec)
1770 // This kind of cast is used to encapsulate a child and cast it
1771 // to the class requested
1773 public class ClassCast : EmptyCast {
1774 public ClassCast (Expression child, Type return_type)
1775 : base (child, return_type)
1780 public override Expression DoResolve (EmitContext ec)
1782 // This should never be invoked, we are born in fully
1783 // initialized state.
1788 public override void Emit (EmitContext ec)
1792 ec.ig.Emit (OpCodes.Castclass, type);
1798 // Unary expressions.
1802 // Unary implements unary expressions. It derives from
1803 // ExpressionStatement becuase the pre/post increment/decrement
1804 // operators can be used in a statement context.
1806 public class Unary : ExpressionStatement {
1807 public enum Operator {
1808 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
1809 Indirection, AddressOf, PreIncrement,
1810 PreDecrement, PostIncrement, PostDecrement
1815 ArrayList Arguments;
1819 public Unary (Operator op, Expression expr, Location loc)
1826 public Expression Expr {
1836 public Operator Oper {
1847 // Returns a stringified representation of the Operator
1852 case Operator.UnaryPlus:
1854 case Operator.UnaryNegation:
1856 case Operator.LogicalNot:
1858 case Operator.OnesComplement:
1860 case Operator.AddressOf:
1862 case Operator.Indirection:
1864 case Operator.PreIncrement : case Operator.PostIncrement :
1866 case Operator.PreDecrement : case Operator.PostDecrement :
1870 return oper.ToString ();
1873 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1875 if (expr.Type == target_type)
1878 return ConvertImplicit (ec, expr, target_type, new Location (-1));
1881 void error23 (Type t)
1884 23, loc, "Operator " + OperName () +
1885 " cannot be applied to operand of type `" +
1886 TypeManager.CSharpName (t) + "'");
1890 // Returns whether an object of type `t' can be incremented
1891 // or decremented with add/sub (ie, basically whether we can
1892 // use pre-post incr-decr operations on it, but it is not a
1893 // System.Decimal, which we test elsewhere)
1895 static bool IsIncrementableNumber (Type t)
1897 return (t == TypeManager.sbyte_type) ||
1898 (t == TypeManager.byte_type) ||
1899 (t == TypeManager.short_type) ||
1900 (t == TypeManager.ushort_type) ||
1901 (t == TypeManager.int32_type) ||
1902 (t == TypeManager.uint32_type) ||
1903 (t == TypeManager.int64_type) ||
1904 (t == TypeManager.uint64_type) ||
1905 (t == TypeManager.char_type) ||
1906 (t.IsSubclassOf (TypeManager.enum_type)) ||
1907 (t == TypeManager.float_type) ||
1908 (t == TypeManager.double_type);
1911 Expression ResolveOperator (EmitContext ec)
1913 Type expr_type = expr.Type;
1916 // Step 1: Perform Operator Overload location
1921 if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1922 op_name = "op_Increment";
1923 else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1924 op_name = "op_Decrement";
1926 op_name = "op_" + oper;
1928 mg = MemberLookup (ec, expr_type, op_name, false, loc);
1930 if (mg == null && expr_type.BaseType != null)
1931 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
1934 Arguments = new ArrayList ();
1935 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1937 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
1939 if (method != null) {
1940 MethodInfo mi = (MethodInfo) method;
1941 type = mi.ReturnType;
1944 error23 (expr_type);
1951 // Step 2: Default operations on CLI native types.
1954 // Only perform numeric promotions on:
1957 if (expr_type == null)
1960 if (oper == Operator.LogicalNot){
1961 if (expr_type != TypeManager.bool_type) {
1962 error23 (expr.Type);
1966 type = TypeManager.bool_type;
1970 if (oper == Operator.OnesComplement) {
1971 if (!((expr_type == TypeManager.int32_type) ||
1972 (expr_type == TypeManager.uint32_type) ||
1973 (expr_type == TypeManager.int64_type) ||
1974 (expr_type == TypeManager.uint64_type) ||
1975 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1976 error23 (expr.Type);
1983 if (oper == Operator.UnaryPlus) {
1985 // A plus in front of something is just a no-op, so return the child.
1991 // Deals with -literals
1992 // int operator- (int x)
1993 // long operator- (long x)
1994 // float operator- (float f)
1995 // double operator- (double d)
1996 // decimal operator- (decimal d)
1998 if (oper == Operator.UnaryNegation){
2000 // Fold a "- Constant" into a negative constant
2003 Expression e = null;
2006 // Is this a constant?
2008 if (expr is IntLiteral)
2009 e = new IntLiteral (-((IntLiteral) expr).Value);
2010 else if (expr is LongLiteral)
2011 e = new LongLiteral (-((LongLiteral) expr).Value);
2012 else if (expr is FloatLiteral)
2013 e = new FloatLiteral (-((FloatLiteral) expr).Value);
2014 else if (expr is DoubleLiteral)
2015 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
2016 else if (expr is DecimalLiteral)
2017 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
2025 // Not a constant we can optimize, perform numeric
2026 // promotions to int, long, double.
2029 // The following is inneficient, because we call
2030 // ConvertImplicit too many times.
2032 // It is also not clear if we should convert to Float
2033 // or Double initially.
2035 if (expr_type == TypeManager.uint32_type){
2037 // FIXME: handle exception to this rule that
2038 // permits the int value -2147483648 (-2^31) to
2039 // bt written as a decimal interger literal
2041 type = TypeManager.int64_type;
2042 expr = ConvertImplicit (ec, expr, type, loc);
2046 if (expr_type == TypeManager.uint64_type){
2048 // FIXME: Handle exception of `long value'
2049 // -92233720368547758087 (-2^63) to be written as
2050 // decimal integer literal.
2052 error23 (expr_type);
2056 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
2063 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
2070 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
2077 error23 (expr_type);
2082 // The operand of the prefix/postfix increment decrement operators
2083 // should be an expression that is classified as a variable,
2084 // a property access or an indexer access
2086 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2087 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2088 if (expr.ExprClass == ExprClass.Variable){
2089 if (IsIncrementableNumber (expr_type) ||
2090 expr_type == TypeManager.decimal_type){
2094 } else if (expr.ExprClass == ExprClass.IndexerAccess){
2096 // FIXME: Verify that we have both get and set methods
2098 throw new Exception ("Implement me");
2099 } else if (expr.ExprClass == ExprClass.PropertyAccess){
2100 PropertyExpr pe = (PropertyExpr) expr;
2102 if (pe.VerifyAssignable ())
2106 report118 (loc, expr, "variable, indexer or property access");
2110 if (oper == Operator.AddressOf){
2111 if (expr.ExprClass != ExprClass.Variable){
2112 Error (211, "Cannot take the address of non-variables");
2115 type = Type.GetType (expr.Type.ToString () + "*");
2118 Error (187, "No such operator '" + OperName () + "' defined for type '" +
2119 TypeManager.CSharpName (expr_type) + "'");
2124 public override Expression DoResolve (EmitContext ec)
2126 expr = expr.Resolve (ec);
2131 eclass = ExprClass.Value;
2132 return ResolveOperator (ec);
2135 public override void Emit (EmitContext ec)
2137 ILGenerator ig = ec.ig;
2138 Type expr_type = expr.Type;
2141 if (method != null) {
2143 // Note that operators are static anyway
2145 if (Arguments != null)
2146 Invocation.EmitArguments (ec, method, Arguments);
2149 // Post increment/decrement operations need a copy at this
2152 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
2153 ig.Emit (OpCodes.Dup);
2156 ig.Emit (OpCodes.Call, (MethodInfo) method);
2159 // Pre Increment and Decrement operators
2161 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
2162 ig.Emit (OpCodes.Dup);
2166 // Increment and Decrement should store the result
2168 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2169 oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2170 ((IStackStore) expr).Store (ec);
2176 case Operator.UnaryPlus:
2177 throw new Exception ("This should be caught by Resolve");
2179 case Operator.UnaryNegation:
2181 ig.Emit (OpCodes.Neg);
2184 case Operator.LogicalNot:
2186 ig.Emit (OpCodes.Ldc_I4_0);
2187 ig.Emit (OpCodes.Ceq);
2190 case Operator.OnesComplement:
2192 ig.Emit (OpCodes.Not);
2195 case Operator.AddressOf:
2196 ((IMemoryLocation)expr).AddressOf (ec);
2199 case Operator.Indirection:
2200 throw new Exception ("Not implemented yet");
2202 case Operator.PreIncrement:
2203 case Operator.PreDecrement:
2204 if (expr.ExprClass == ExprClass.Variable){
2206 // Resolve already verified that it is an "incrementable"
2209 ig.Emit (OpCodes.Ldc_I4_1);
2211 if (oper == Operator.PreDecrement)
2212 ig.Emit (OpCodes.Sub);
2214 ig.Emit (OpCodes.Add);
2215 ig.Emit (OpCodes.Dup);
2216 ((IStackStore) expr).Store (ec);
2218 throw new Exception ("Handle Indexers and Properties here");
2222 case Operator.PostIncrement:
2223 case Operator.PostDecrement:
2224 eclass = expr.ExprClass;
2225 if (eclass == ExprClass.Variable){
2227 // Resolve already verified that it is an "incrementable"
2230 ig.Emit (OpCodes.Dup);
2231 ig.Emit (OpCodes.Ldc_I4_1);
2233 if (oper == Operator.PostDecrement)
2234 ig.Emit (OpCodes.Sub);
2236 ig.Emit (OpCodes.Add);
2237 ((IStackStore) expr).Store (ec);
2238 } else if (eclass == ExprClass.PropertyAccess){
2239 throw new Exception ("Handle Properties here");
2240 } else if (eclass == ExprClass.IndexerAccess) {
2241 throw new Exception ("Handle Indexers here");
2243 Console.WriteLine ("Unknown exprclass: " + eclass);
2248 throw new Exception ("This should not happen: Operator = "
2249 + oper.ToString ());
2254 public override void EmitStatement (EmitContext ec)
2257 // FIXME: we should rewrite this code to generate
2258 // better code for ++ and -- as we know we wont need
2259 // the values on the stack
2262 ec.ig.Emit (OpCodes.Pop);
2266 public class Probe : Expression {
2267 public readonly string ProbeType;
2268 public readonly Operator Oper;
2272 public enum Operator {
2276 public Probe (Operator oper, Expression expr, string probe_type)
2279 ProbeType = probe_type;
2283 public Expression Expr {
2289 public override Expression DoResolve (EmitContext ec)
2291 probe_type = ec.TypeContainer.LookupType (ProbeType, false);
2293 if (probe_type == null)
2296 expr = expr.Resolve (ec);
2298 type = TypeManager.bool_type;
2299 eclass = ExprClass.Value;
2304 public override void Emit (EmitContext ec)
2306 ILGenerator ig = ec.ig;
2310 if (Oper == Operator.Is){
2311 ig.Emit (OpCodes.Isinst, probe_type);
2312 ig.Emit (OpCodes.Ldnull);
2313 ig.Emit (OpCodes.Cgt_Un);
2315 ig.Emit (OpCodes.Isinst, probe_type);
2321 // This represents a typecast in the source language.
2323 // FIXME: Cast expressions have an unusual set of parsing
2324 // rules, we need to figure those out.
2326 public class Cast : Expression {
2331 public Cast (string cast_type, Expression expr, Location loc)
2333 this.target_type = cast_type;
2338 public string TargetType {
2344 public Expression Expr {
2353 public override Expression DoResolve (EmitContext ec)
2355 expr = expr.Resolve (ec);
2359 type = ec.TypeContainer.LookupType (target_type, false);
2360 eclass = ExprClass.Value;
2365 expr = ConvertExplicit (ec, expr, type, loc);
2370 public override void Emit (EmitContext ec)
2373 // This one will never happen
2375 throw new Exception ("Should not happen");
2379 public class Binary : Expression {
2380 public enum Operator {
2381 Multiply, Division, Modulus,
2382 Addition, Subtraction,
2383 LeftShift, RightShift,
2384 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
2385 Equality, Inequality,
2394 Expression left, right;
2396 ArrayList Arguments;
2400 public Binary (Operator oper, Expression left, Expression right, Location loc)
2408 public Operator Oper {
2417 public Expression Left {
2426 public Expression Right {
2437 // Returns a stringified representation of the Operator
2442 case Operator.Multiply:
2444 case Operator.Division:
2446 case Operator.Modulus:
2448 case Operator.Addition:
2450 case Operator.Subtraction:
2452 case Operator.LeftShift:
2454 case Operator.RightShift:
2456 case Operator.LessThan:
2458 case Operator.GreaterThan:
2460 case Operator.LessThanOrEqual:
2462 case Operator.GreaterThanOrEqual:
2464 case Operator.Equality:
2466 case Operator.Inequality:
2468 case Operator.BitwiseAnd:
2470 case Operator.BitwiseOr:
2472 case Operator.ExclusiveOr:
2474 case Operator.LogicalOr:
2476 case Operator.LogicalAnd:
2480 return oper.ToString ();
2483 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
2485 if (expr.Type == target_type)
2488 return ConvertImplicit (ec, expr, target_type, new Location (-1));
2492 // Note that handling the case l == Decimal || r == Decimal
2493 // is taken care of by the Step 1 Operator Overload resolution.
2495 void DoNumericPromotions (EmitContext ec, Type l, Type r)
2497 if (l == TypeManager.double_type || r == TypeManager.double_type){
2499 // If either operand is of type double, the other operand is
2500 // conveted to type double.
2502 if (r != TypeManager.double_type)
2503 right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
2504 if (l != TypeManager.double_type)
2505 left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
2507 type = TypeManager.double_type;
2508 } else if (l == TypeManager.float_type || r == TypeManager.float_type){
2510 // if either operand is of type float, th eother operand is
2511 // converd to type float.
2513 if (r != TypeManager.double_type)
2514 right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
2515 if (l != TypeManager.double_type)
2516 left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
2517 type = TypeManager.float_type;
2518 } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
2522 // If either operand is of type ulong, the other operand is
2523 // converted to type ulong. or an error ocurrs if the other
2524 // operand is of type sbyte, short, int or long
2527 if (l == TypeManager.uint64_type){
2528 if (r != TypeManager.uint64_type && right is IntLiteral){
2529 e = TryImplicitIntConversion (l, (IntLiteral) right);
2535 if (left is IntLiteral){
2536 e = TryImplicitIntConversion (r, (IntLiteral) left);
2543 if ((other == TypeManager.sbyte_type) ||
2544 (other == TypeManager.short_type) ||
2545 (other == TypeManager.int32_type) ||
2546 (other == TypeManager.int64_type)){
2547 string oper = OperName ();
2549 Error (34, loc, "Operator `" + OperName ()
2550 + "' is ambiguous on operands of type `"
2551 + TypeManager.CSharpName (l) + "' "
2552 + "and `" + TypeManager.CSharpName (r)
2555 type = TypeManager.uint64_type;
2556 } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
2558 // If either operand is of type long, the other operand is converted
2561 if (l != TypeManager.int64_type)
2562 left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
2563 if (r != TypeManager.int64_type)
2564 right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
2566 type = TypeManager.int64_type;
2567 } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
2569 // If either operand is of type uint, and the other
2570 // operand is of type sbyte, short or int, othe operands are
2571 // converted to type long.
2575 if (l == TypeManager.uint32_type)
2577 else if (r == TypeManager.uint32_type)
2580 if ((other == TypeManager.sbyte_type) ||
2581 (other == TypeManager.short_type) ||
2582 (other == TypeManager.int32_type)){
2583 left = ForceConversion (ec, left, TypeManager.int64_type);
2584 right = ForceConversion (ec, right, TypeManager.int64_type);
2585 type = TypeManager.int64_type;
2588 // if either operand is of type uint, the other
2589 // operand is converd to type uint
2591 left = ForceConversion (ec, left, TypeManager.uint32_type);
2592 right = ForceConversion (ec, right, TypeManager.uint32_type);
2593 type = TypeManager.uint32_type;
2595 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2596 if (l != TypeManager.decimal_type)
2597 left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
2598 if (r != TypeManager.decimal_type)
2599 right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
2601 type = TypeManager.decimal_type;
2603 Expression l_tmp, r_tmp;
2605 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
2606 if (l_tmp == null) {
2612 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
2613 if (r_tmp == null) {
2619 type = TypeManager.int32_type;
2626 "Operator " + OperName () + " cannot be applied to operands of type `" +
2627 TypeManager.CSharpName (left.Type) + "' and `" +
2628 TypeManager.CSharpName (right.Type) + "'");
2632 Expression CheckShiftArguments (EmitContext ec)
2636 Type r = right.Type;
2638 e = ForceConversion (ec, right, TypeManager.int32_type);
2645 if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
2646 ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
2647 ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
2648 ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
2658 Expression ResolveOperator (EmitContext ec)
2661 Type r = right.Type;
2664 // Step 1: Perform Operator Overload location
2666 Expression left_expr, right_expr;
2668 string op = "op_" + oper;
2670 left_expr = MemberLookup (ec, l, op, false, loc);
2671 if (left_expr == null && l.BaseType != null)
2672 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
2674 right_expr = MemberLookup (ec, r, op, false, loc);
2675 if (right_expr == null && r.BaseType != null)
2676 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
2678 MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2680 if (union != null) {
2681 Arguments = new ArrayList ();
2682 Arguments.Add (new Argument (left, Argument.AType.Expression));
2683 Arguments.Add (new Argument (right, Argument.AType.Expression));
2685 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
2686 if (method != null) {
2687 MethodInfo mi = (MethodInfo) method;
2688 type = mi.ReturnType;
2697 // Step 2: Default operations on CLI native types.
2700 // Only perform numeric promotions on:
2701 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2703 if (oper == Operator.Addition){
2705 // If any of the arguments is a string, cast to string
2707 if (l == TypeManager.string_type){
2708 if (r == TypeManager.string_type){
2710 method = TypeManager.string_concat_string_string;
2713 method = TypeManager.string_concat_object_object;
2714 right = ConvertImplicit (ec, right,
2715 TypeManager.object_type, loc);
2717 type = TypeManager.string_type;
2719 Arguments = new ArrayList ();
2720 Arguments.Add (new Argument (left, Argument.AType.Expression));
2721 Arguments.Add (new Argument (right, Argument.AType.Expression));
2725 } else if (r == TypeManager.string_type){
2727 method = TypeManager.string_concat_object_object;
2728 Arguments = new ArrayList ();
2729 Arguments.Add (new Argument (left, Argument.AType.Expression));
2730 Arguments.Add (new Argument (right, Argument.AType.Expression));
2732 left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
2733 type = TypeManager.string_type;
2739 // FIXME: is Delegate operator + (D x, D y) handled?
2743 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2744 return CheckShiftArguments (ec);
2746 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2747 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2750 type = TypeManager.bool_type;
2755 // We are dealing with numbers
2758 DoNumericPromotions (ec, l, r);
2760 if (left == null || right == null)
2764 if (oper == Operator.BitwiseAnd ||
2765 oper == Operator.BitwiseOr ||
2766 oper == Operator.ExclusiveOr){
2767 if (!((l == TypeManager.int32_type) ||
2768 (l == TypeManager.uint32_type) ||
2769 (l == TypeManager.int64_type) ||
2770 (l == TypeManager.uint64_type))){
2777 if (oper == Operator.Equality ||
2778 oper == Operator.Inequality ||
2779 oper == Operator.LessThanOrEqual ||
2780 oper == Operator.LessThan ||
2781 oper == Operator.GreaterThanOrEqual ||
2782 oper == Operator.GreaterThan){
2783 type = TypeManager.bool_type;
2789 public override Expression DoResolve (EmitContext ec)
2791 left = left.Resolve (ec);
2792 right = right.Resolve (ec);
2794 if (left == null || right == null)
2797 if (left.Type == null)
2798 throw new Exception (
2799 "Resolve returned non null, but did not set the type! (" +
2801 if (right.Type == null)
2802 throw new Exception (
2803 "Resolve returned non null, but did not set the type! (" +
2806 eclass = ExprClass.Value;
2808 return ResolveOperator (ec);
2811 public bool IsBranchable ()
2813 if (oper == Operator.Equality ||
2814 oper == Operator.Inequality ||
2815 oper == Operator.LessThan ||
2816 oper == Operator.GreaterThan ||
2817 oper == Operator.LessThanOrEqual ||
2818 oper == Operator.GreaterThanOrEqual){
2825 // This entry point is used by routines that might want
2826 // to emit a brfalse/brtrue after an expression, and instead
2827 // they could use a more compact notation.
2829 // Typically the code would generate l.emit/r.emit, followed
2830 // by the comparission and then a brtrue/brfalse. The comparissions
2831 // are sometimes inneficient (there are not as complete as the branches
2832 // look for the hacks in Emit using double ceqs).
2834 // So for those cases we provide EmitBranchable that can emit the
2835 // branch with the test
2837 public void EmitBranchable (EmitContext ec, int target)
2840 bool close_target = false;
2846 case Operator.Equality:
2848 opcode = OpCodes.Beq_S;
2850 opcode = OpCodes.Beq;
2853 case Operator.Inequality:
2855 opcode = OpCodes.Bne_Un_S;
2857 opcode = OpCodes.Bne_Un;
2860 case Operator.LessThan:
2862 opcode = OpCodes.Blt_S;
2864 opcode = OpCodes.Blt;
2867 case Operator.GreaterThan:
2869 opcode = OpCodes.Bgt_S;
2871 opcode = OpCodes.Bgt;
2874 case Operator.LessThanOrEqual:
2876 opcode = OpCodes.Ble_S;
2878 opcode = OpCodes.Ble;
2881 case Operator.GreaterThanOrEqual:
2883 opcode = OpCodes.Bge_S;
2885 opcode = OpCodes.Ble;
2889 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2890 + oper.ToString ());
2893 ec.ig.Emit (opcode, target);
2896 public override void Emit (EmitContext ec)
2898 ILGenerator ig = ec.ig;
2900 Type r = right.Type;
2903 if (method != null) {
2905 // Note that operators are static anyway
2907 if (Arguments != null)
2908 Invocation.EmitArguments (ec, method, Arguments);
2910 if (method is MethodInfo)
2911 ig.Emit (OpCodes.Call, (MethodInfo) method);
2913 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2922 case Operator.Multiply:
2924 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2925 opcode = OpCodes.Mul_Ovf;
2926 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2927 opcode = OpCodes.Mul_Ovf_Un;
2929 opcode = OpCodes.Mul;
2931 opcode = OpCodes.Mul;
2935 case Operator.Division:
2936 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2937 opcode = OpCodes.Div_Un;
2939 opcode = OpCodes.Div;
2942 case Operator.Modulus:
2943 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2944 opcode = OpCodes.Rem_Un;
2946 opcode = OpCodes.Rem;
2949 case Operator.Addition:
2951 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2952 opcode = OpCodes.Add_Ovf;
2953 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2954 opcode = OpCodes.Add_Ovf_Un;
2956 opcode = OpCodes.Mul;
2958 opcode = OpCodes.Add;
2961 case Operator.Subtraction:
2963 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2964 opcode = OpCodes.Sub_Ovf;
2965 else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2966 opcode = OpCodes.Sub_Ovf_Un;
2968 opcode = OpCodes.Sub;
2970 opcode = OpCodes.Sub;
2973 case Operator.RightShift:
2974 opcode = OpCodes.Shr;
2977 case Operator.LeftShift:
2978 opcode = OpCodes.Shl;
2981 case Operator.Equality:
2982 opcode = OpCodes.Ceq;
2985 case Operator.Inequality:
2986 ec.ig.Emit (OpCodes.Ceq);
2987 ec.ig.Emit (OpCodes.Ldc_I4_0);
2989 opcode = OpCodes.Ceq;
2992 case Operator.LessThan:
2993 opcode = OpCodes.Clt;
2996 case Operator.GreaterThan:
2997 opcode = OpCodes.Cgt;
3000 case Operator.LessThanOrEqual:
3001 ec.ig.Emit (OpCodes.Cgt);
3002 ec.ig.Emit (OpCodes.Ldc_I4_0);
3004 opcode = OpCodes.Ceq;
3007 case Operator.GreaterThanOrEqual:
3008 ec.ig.Emit (OpCodes.Clt);
3009 ec.ig.Emit (OpCodes.Ldc_I4_1);
3011 opcode = OpCodes.Sub;
3014 case Operator.LogicalOr:
3015 case Operator.BitwiseOr:
3016 opcode = OpCodes.Or;
3019 case Operator.LogicalAnd:
3020 case Operator.BitwiseAnd:
3021 opcode = OpCodes.And;
3024 case Operator.ExclusiveOr:
3025 opcode = OpCodes.Xor;
3029 throw new Exception ("This should not happen: Operator = "
3030 + oper.ToString ());
3037 public class Conditional : Expression {
3038 Expression expr, trueExpr, falseExpr;
3041 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3044 this.trueExpr = trueExpr;
3045 this.falseExpr = falseExpr;
3049 public Expression Expr {
3055 public Expression TrueExpr {
3061 public Expression FalseExpr {
3067 public override Expression DoResolve (EmitContext ec)
3069 expr = expr.Resolve (ec);
3071 if (expr.Type != TypeManager.bool_type)
3072 expr = Expression.ConvertImplicitRequired (
3073 ec, expr, TypeManager.bool_type, loc);
3075 trueExpr = trueExpr.Resolve (ec);
3076 falseExpr = falseExpr.Resolve (ec);
3078 if (expr == null || trueExpr == null || falseExpr == null)
3081 if (trueExpr.Type == falseExpr.Type)
3082 type = trueExpr.Type;
3087 // First, if an implicit conversion exists from trueExpr
3088 // to falseExpr, then the result type is of type falseExpr.Type
3090 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
3092 type = falseExpr.Type;
3094 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
3095 type = trueExpr.Type;
3098 Error (173, loc, "The type of the conditional expression can " +
3099 "not be computed because there is no implicit conversion" +
3100 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3101 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3106 eclass = ExprClass.Value;
3110 public override void Emit (EmitContext ec)
3112 ILGenerator ig = ec.ig;
3113 Label false_target = ig.DefineLabel ();
3114 Label end_target = ig.DefineLabel ();
3117 ig.Emit (OpCodes.Brfalse, false_target);
3119 ig.Emit (OpCodes.Br, end_target);
3120 ig.MarkLabel (false_target);
3121 falseExpr.Emit (ec);
3122 ig.MarkLabel (end_target);
3127 // SimpleName expressions are initially formed of a single
3128 // word and it only happens at the beginning of the expression.
3130 // The expression will try to be bound to a Field, a Method
3131 // group or a Property. If those fail we pass the name to our
3132 // caller and the SimpleName is compounded to perform a type
3133 // lookup. The idea behind this process is that we want to avoid
3134 // creating a namespace map from the assemblies, as that requires
3135 // the GetExportedTypes function to be called and a hashtable to
3136 // be constructed which reduces startup time. If later we find
3137 // that this is slower, we should create a `NamespaceExpr' expression
3138 // that fully participates in the resolution process.
3140 // For example `System.Console.WriteLine' is decomposed into
3141 // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3143 // The first SimpleName wont produce a match on its own, so it will
3145 // MemberAccess (SimpleName ("System.Console"), "WriteLine").
3147 // System.Console will produce a TypeExpr match.
3149 // The downside of this is that we might be hitting `LookupType' too many
3150 // times with this scheme.
3152 public class SimpleName : Expression {
3153 public readonly string Name;
3154 public readonly Location Location;
3156 public SimpleName (string name, Location l)
3162 public static void Error120 (Location l, string name)
3166 "An object reference is required " +
3167 "for the non-static field `"+name+"'");
3171 // Checks whether we are trying to access an instance
3172 // property, method or field from a static body.
3174 Expression MemberStaticCheck (Expression e)
3176 if (e is FieldExpr){
3177 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3180 Error120 (Location, Name);
3183 } else if (e is MethodGroupExpr){
3184 MethodGroupExpr mg = (MethodGroupExpr) e;
3186 if (!mg.RemoveInstanceMethods ()){
3187 Error120 (Location, mg.Methods [0].Name);
3191 } else if (e is PropertyExpr){
3192 if (!((PropertyExpr) e).IsStatic){
3193 Error120 (Location, Name);
3202 // 7.5.2: Simple Names.
3204 // Local Variables and Parameters are handled at
3205 // parse time, so they never occur as SimpleNames.
3207 public override Expression DoResolve (EmitContext ec)
3212 // Stage 1: Performed by the parser (binding to local or parameters).
3216 // Stage 2: Lookup members
3218 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
3221 // Stage 3: Lookup symbol in the various namespaces.
3225 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
3226 return new TypeExpr (t);
3229 // Stage 3 part b: Lookup up if we are an alias to a type
3232 // Since we are cheating: we only do the Alias lookup for
3233 // namespaces if the name does not include any dots in it
3236 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
3237 // using NamespaceExprs (dunno how that fixes the alias
3238 // per-file though).
3240 // No match, maybe our parent can compose us
3241 // into something meaningful.
3246 // Step 2, continues here.
3250 if (e is FieldExpr){
3251 FieldExpr fe = (FieldExpr) e;
3253 if (!fe.FieldInfo.IsStatic)
3254 fe.InstanceExpression = new This (Location.Null);
3258 return MemberStaticCheck (e);
3263 public override void Emit (EmitContext ec)
3266 // If this is ever reached, then we failed to
3267 // find the name as a namespace
3270 Error (103, Location, "The name `" + Name +
3271 "' does not exist in the class `" +
3272 ec.TypeContainer.Name + "'");
3276 public class LocalTemporary : Expression, IStackStore, IMemoryLocation {
3277 LocalBuilder builder;
3279 public LocalTemporary (EmitContext ec, Type t)
3282 eclass = ExprClass.Value;
3283 builder = ec.GetTemporaryStorage (t);
3286 public override Expression DoResolve (EmitContext ec)
3291 public override void Emit (EmitContext ec)
3293 ec.ig.Emit (OpCodes.Ldloc, builder);
3296 public void Store (EmitContext ec)
3298 ec.ig.Emit (OpCodes.Stloc, builder);
3301 public void AddressOf (EmitContext ec)
3303 ec.ig.Emit (OpCodes.Ldloca, builder);
3307 public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
3308 public readonly string Name;
3309 public readonly Block Block;
3311 VariableInfo variable_info;
3313 public LocalVariableReference (Block block, string name)
3317 eclass = ExprClass.Variable;
3320 public VariableInfo VariableInfo {
3322 if (variable_info == null)
3323 variable_info = Block.GetVariableInfo (Name);
3324 return variable_info;
3328 public override Expression DoResolve (EmitContext ec)
3330 VariableInfo vi = VariableInfo;
3332 type = vi.VariableType;
3336 public override void Emit (EmitContext ec)
3338 VariableInfo vi = VariableInfo;
3339 ILGenerator ig = ec.ig;
3346 ig.Emit (OpCodes.Ldloc_0);
3350 ig.Emit (OpCodes.Ldloc_1);
3354 ig.Emit (OpCodes.Ldloc_2);
3358 ig.Emit (OpCodes.Ldloc_3);
3363 ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3365 ig.Emit (OpCodes.Ldloc, idx);
3370 public static void Store (ILGenerator ig, int idx)
3374 ig.Emit (OpCodes.Stloc_0);
3378 ig.Emit (OpCodes.Stloc_1);
3382 ig.Emit (OpCodes.Stloc_2);
3386 ig.Emit (OpCodes.Stloc_3);
3391 ig.Emit (OpCodes.Stloc_S, (byte) idx);
3393 ig.Emit (OpCodes.Stloc, idx);
3398 public void Store (EmitContext ec)
3400 ILGenerator ig = ec.ig;
3401 VariableInfo vi = VariableInfo;
3405 // Funny seems the above generates optimal code for us, but
3406 // seems to take too long to generate what we need.
3407 // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3412 public void AddressOf (EmitContext ec)
3414 VariableInfo vi = VariableInfo;
3421 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3423 ec.ig.Emit (OpCodes.Ldloca, idx);
3427 public class ParameterReference : Expression, IStackStore, IMemoryLocation {
3428 public readonly Parameters Pars;
3429 public readonly String Name;
3430 public readonly int Idx;
3433 public ParameterReference (Parameters pars, int idx, string name)
3438 eclass = ExprClass.Variable;
3441 public override Expression DoResolve (EmitContext ec)
3443 Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
3454 public override void Emit (EmitContext ec)
3457 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3459 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3462 public void Store (EmitContext ec)
3465 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3467 ec.ig.Emit (OpCodes.Starg, arg_idx);
3471 public void AddressOf (EmitContext ec)
3474 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3476 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3481 // Used for arguments to New(), Invocation()
3483 public class Argument {
3490 public readonly AType Type;
3491 public Expression expr;
3493 public Argument (Expression expr, AType type)
3499 public Expression Expr {
3509 public bool Resolve (EmitContext ec)
3511 expr = expr.Resolve (ec);
3513 return expr != null;
3516 public void Emit (EmitContext ec)
3523 // Invocation of methods or delegates.
3525 public class Invocation : ExpressionStatement {
3526 public readonly ArrayList Arguments;
3527 public readonly Location Location;
3530 MethodBase method = null;
3532 static Hashtable method_parameter_cache;
3534 static Invocation ()
3536 method_parameter_cache = new Hashtable ();
3540 // arguments is an ArrayList, but we do not want to typecast,
3541 // as it might be null.
3543 // FIXME: only allow expr to be a method invocation or a
3544 // delegate invocation (7.5.5)
3546 public Invocation (Expression expr, ArrayList arguments, Location l)
3549 Arguments = arguments;
3553 public Expression Expr {
3560 // Returns the Parameters (a ParameterData interface) for the
3563 public static ParameterData GetParameterData (MethodBase mb)
3565 object pd = method_parameter_cache [mb];
3569 return (ParameterData) pd;
3572 ip = TypeContainer.LookupParametersByBuilder (mb);
3574 method_parameter_cache [mb] = ip;
3576 return (ParameterData) ip;
3578 ParameterInfo [] pi = mb.GetParameters ();
3579 ReflectionParameters rp = new ReflectionParameters (pi);
3580 method_parameter_cache [mb] = rp;
3582 return (ParameterData) rp;
3587 // Tells whether a user defined conversion from Type `from' to
3588 // Type `to' exists.
3590 // FIXME: we could implement a cache here.
3592 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
3594 // Locate user-defined implicit operators
3598 mg = MemberLookup (ec, to, "op_Implicit", false, loc);
3601 MethodGroupExpr me = (MethodGroupExpr) mg;
3603 for (int i = me.Methods.Length; i > 0;) {
3605 MethodBase mb = me.Methods [i];
3606 ParameterData pd = GetParameterData (mb);
3608 if (from == pd.ParameterType (0))
3613 mg = MemberLookup (ec, from, "op_Implicit", false, loc);
3616 MethodGroupExpr me = (MethodGroupExpr) mg;
3618 for (int i = me.Methods.Length; i > 0;) {
3620 MethodBase mb = me.Methods [i];
3621 MethodInfo mi = (MethodInfo) mb;
3623 if (mi.ReturnType == to)
3632 // Determines "better conversion" as specified in 7.4.2.3
3633 // Returns : 1 if a->p is better
3634 // 0 if a->q or neither is better
3636 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
3639 Type argument_type = a.Expr.Type;
3640 Expression argument_expr = a.Expr;
3642 if (argument_type == null)
3643 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3648 if (argument_type == p)
3651 if (argument_type == q)
3655 // Now probe whether an implicit constant expression conversion
3658 // An implicit constant expression conversion permits the following
3661 // * A constant-expression of type `int' can be converted to type
3662 // sbyte, byute, short, ushort, uint, ulong provided the value of
3663 // of the expression is withing the range of the destination type.
3665 // * A constant-expression of type long can be converted to type
3666 // ulong, provided the value of the constant expression is not negative
3668 // FIXME: Note that this assumes that constant folding has
3669 // taken place. We dont do constant folding yet.
3672 if (argument_expr is IntLiteral){
3673 IntLiteral ei = (IntLiteral) argument_expr;
3674 int value = ei.Value;
3676 if (p == TypeManager.sbyte_type){
3677 if (value >= SByte.MinValue && value <= SByte.MaxValue)
3679 } else if (p == TypeManager.byte_type){
3680 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3682 } else if (p == TypeManager.short_type){
3683 if (value >= Int16.MinValue && value <= Int16.MaxValue)
3685 } else if (p == TypeManager.ushort_type){
3686 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3688 } else if (p == TypeManager.uint32_type){
3690 // we can optimize this case: a positive int32
3691 // always fits on a uint32
3695 } else if (p == TypeManager.uint64_type){
3697 // we can optimize this case: a positive int32
3698 // always fits on a uint64
3703 } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3704 LongLiteral ll = (LongLiteral) argument_expr;
3706 if (p == TypeManager.uint64_type){
3717 tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
3719 tmp = ConvertImplicit (ec, argument_expr, p, loc);
3728 if (ConversionExists (ec, p, q, loc) == true &&
3729 ConversionExists (ec, q, p, loc) == false)
3732 if (p == TypeManager.sbyte_type)
3733 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3734 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3737 if (p == TypeManager.short_type)
3738 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3739 q == TypeManager.uint64_type)
3742 if (p == TypeManager.int32_type)
3743 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3746 if (p == TypeManager.int64_type)
3747 if (q == TypeManager.uint64_type)
3754 // Determines "Better function" and returns an integer indicating :
3755 // 0 if candidate ain't better
3756 // 1 if candidate is better than the current best match
3758 static int BetterFunction (EmitContext ec, ArrayList args,
3759 MethodBase candidate, MethodBase best,
3760 bool use_standard, Location loc)
3762 ParameterData candidate_pd = GetParameterData (candidate);
3763 ParameterData best_pd;
3769 argument_count = args.Count;
3771 if (candidate_pd.Count == 0 && argument_count == 0)
3775 if (candidate_pd.Count == argument_count) {
3777 for (int j = argument_count; j > 0;) {
3780 Argument a = (Argument) args [j];
3782 x = BetterConversion (
3783 ec, a, candidate_pd.ParameterType (j), null,
3799 best_pd = GetParameterData (best);
3801 if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3802 int rating1 = 0, rating2 = 0;
3804 for (int j = argument_count; j > 0;) {
3808 Argument a = (Argument) args [j];
3810 x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
3811 best_pd.ParameterType (j), use_standard, loc);
3812 y = BetterConversion (ec, a, best_pd.ParameterType (j),
3813 candidate_pd.ParameterType (j), use_standard,
3820 if (rating1 > rating2)
3829 public static string FullMethodDesc (MethodBase mb)
3831 StringBuilder sb = new StringBuilder (mb.Name);
3832 ParameterData pd = GetParameterData (mb);
3834 int count = pd.Count;
3837 for (int i = count; i > 0; ) {
3840 sb.Append (TypeManager.CSharpName (pd.ParameterType (count - i - 1)));
3846 return sb.ToString ();
3849 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3851 MemberInfo [] miset;
3852 MethodGroupExpr union;
3854 if (mg1 != null && mg2 != null) {
3856 MethodGroupExpr left_set = null, right_set = null;
3857 int length1 = 0, length2 = 0;
3859 left_set = (MethodGroupExpr) mg1;
3860 length1 = left_set.Methods.Length;
3862 right_set = (MethodGroupExpr) mg2;
3863 length2 = right_set.Methods.Length;
3865 ArrayList common = new ArrayList ();
3867 for (int i = 0; i < left_set.Methods.Length; i++) {
3868 for (int j = 0; j < right_set.Methods.Length; j++) {
3869 if (left_set.Methods [i] == right_set.Methods [j])
3870 common.Add (left_set.Methods [i]);
3874 miset = new MemberInfo [length1 + length2 - common.Count];
3876 left_set.Methods.CopyTo (miset, 0);
3880 for (int j = 0; j < right_set.Methods.Length; j++)
3881 if (!common.Contains (right_set.Methods [j]))
3882 miset [length1 + k++] = right_set.Methods [j];
3884 union = new MethodGroupExpr (miset);
3888 } else if (mg1 == null && mg2 != null) {
3890 MethodGroupExpr me = (MethodGroupExpr) mg2;
3892 miset = new MemberInfo [me.Methods.Length];
3893 me.Methods.CopyTo (miset, 0);
3895 union = new MethodGroupExpr (miset);
3899 } else if (mg2 == null && mg1 != null) {
3901 MethodGroupExpr me = (MethodGroupExpr) mg1;
3903 miset = new MemberInfo [me.Methods.Length];
3904 me.Methods.CopyTo (miset, 0);
3906 union = new MethodGroupExpr (miset);
3915 // Find the Applicable Function Members (7.4.2.1)
3917 // me: Method Group expression with the members to select.
3918 // it might contain constructors or methods (or anything
3919 // that maps to a method).
3921 // Arguments: ArrayList containing resolved Argument objects.
3923 // loc: The location if we want an error to be reported, or a Null
3924 // location for "probing" purposes.
3926 // inside_user_defined: controls whether OverloadResolve should use the
3927 // ConvertImplicit or ConvertImplicitStandard during overload resolution.
3929 // Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3930 // that is the best match of me on Arguments.
3933 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3934 ArrayList Arguments, Location loc,
3937 ArrayList afm = new ArrayList ();
3938 int best_match_idx = -1;
3939 MethodBase method = null;
3942 for (int i = me.Methods.Length; i > 0; ){
3944 MethodBase candidate = me.Methods [i];
3947 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
3953 method = me.Methods [best_match_idx];
3957 if (Arguments == null)
3960 argument_count = Arguments.Count;
3964 // Now we see if we can at least find a method with the same number of arguments
3965 // and then try doing implicit conversion on the arguments
3966 if (best_match_idx == -1) {
3968 for (int i = me.Methods.Length; i > 0;) {
3970 MethodBase mb = me.Methods [i];
3971 pd = GetParameterData (mb);
3973 if (pd.Count == argument_count) {
3975 method = me.Methods [best_match_idx];
3986 // And now convert implicitly, each argument to the required type
3988 pd = GetParameterData (method);
3990 for (int j = argument_count; j > 0;) {
3992 Argument a = (Argument) Arguments [j];
3993 Expression a_expr = a.Expr;
3994 Type parameter_type = pd.ParameterType (j);
3996 if (a_expr.Type != parameter_type){
4000 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
4003 conv = ConvertImplicit (ec, a_expr, parameter_type,
4007 if (!Location.IsNull (loc)) {
4009 "The best overloaded match for method '" + FullMethodDesc (method) +
4010 "' has some invalid arguments");
4012 "Argument " + (j+1) +
4013 ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
4014 + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
4019 // Update the argument with the implicit conversion
4029 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4030 ArrayList Arguments, Location loc)
4032 return OverloadResolve (ec, me, Arguments, loc, false);
4035 public override Expression DoResolve (EmitContext ec)
4037 bool IsDelegate = false;
4039 // First, resolve the expression that is used to
4040 // trigger the invocation
4042 this.expr = expr.Resolve (ec);
4043 if (this.expr == null)
4046 Type expr_type = null;
4047 if (!(this.expr is MethodGroupExpr)) {
4048 expr_type = this.expr.Type;
4049 IsDelegate = TypeManager.IsDelegateType (expr_type);
4053 return (new DelegateInvocation (this.expr, Arguments, Location)).Resolve (ec);
4056 if (!(this.expr is MethodGroupExpr)){
4057 report118 (Location, this.expr, "method group");
4062 // Next, evaluate all the expressions in the argument list
4064 if (Arguments != null){
4065 for (int i = Arguments.Count; i > 0;){
4067 Argument a = (Argument) Arguments [i];
4069 if (!a.Resolve (ec))
4074 method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
4077 if (method == null){
4078 Error (-6, Location,
4079 "Could not find any applicable function for this argument list");
4083 if (method is MethodInfo)
4084 type = ((MethodInfo)method).ReturnType;
4086 eclass = ExprClass.Value;
4090 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
4094 if (Arguments != null)
4095 top = Arguments.Count;
4099 for (int i = 0; i < top; i++){
4100 Argument a = (Argument) Arguments [i];
4106 public static void EmitCall (EmitContext ec,
4107 bool is_static, Expression instance_expr,
4108 MethodBase method, ArrayList Arguments)
4110 ILGenerator ig = ec.ig;
4111 bool struct_call = false;
4115 // If this is ourselves, push "this"
4117 if (instance_expr == null){
4118 ig.Emit (OpCodes.Ldarg_0);
4121 // Push the instance expression
4123 if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
4128 // If the expression implements IMemoryLocation, then
4129 // we can optimize and use AddressOf on the
4132 // If not we have to use some temporary storage for
4134 if (instance_expr is IMemoryLocation)
4135 ((IMemoryLocation) instance_expr).AddressOf (ec);
4137 Type t = instance_expr.Type;
4139 instance_expr.Emit (ec);
4140 LocalBuilder temp = ec.GetTemporaryStorage (t);
4141 ig.Emit (OpCodes.Stloc, temp);
4142 ig.Emit (OpCodes.Ldloca, temp);
4145 instance_expr.Emit (ec);
4149 if (Arguments != null)
4150 EmitArguments (ec, method, Arguments);
4152 if (is_static || struct_call){
4153 if (method is MethodInfo)
4154 ig.Emit (OpCodes.Call, (MethodInfo) method);
4156 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4158 if (method is MethodInfo)
4159 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
4161 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
4165 public override void Emit (EmitContext ec)
4167 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
4168 EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
4171 public override void EmitStatement (EmitContext ec)
4176 // Pop the return value if there is one
4178 if (method is MethodInfo){
4179 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
4180 ec.ig.Emit (OpCodes.Pop);
4185 public class New : ExpressionStatement {
4192 public readonly NType NewType;
4193 public readonly ArrayList Arguments;
4194 public readonly string RequestedType;
4196 // These are for the case when we have an array
4197 public readonly string Rank;
4198 public readonly ArrayList Initializers;
4201 MethodBase method = null;
4204 // If set, the new expression is for a value_target, and
4205 // we will not leave anything on the stack.
4207 Expression value_target;
4209 public New (string requested_type, ArrayList arguments, Location loc)
4211 RequestedType = requested_type;
4212 Arguments = arguments;
4213 NewType = NType.Object;
4217 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
4219 RequestedType = requested_type;
4221 Initializers = initializers;
4222 NewType = NType.Array;
4225 Arguments = new ArrayList ();
4227 foreach (Expression e in exprs)
4228 Arguments.Add (new Argument (e, Argument.AType.Expression));
4232 public static string FormLookupType (string base_type, int idx_count, string rank)
4234 StringBuilder sb = new StringBuilder (base_type);
4239 for (int i = 1; i < idx_count; i++)
4243 return sb.ToString ();
4246 public Expression ValueTypeVariable {
4248 return value_target;
4252 value_target = value;
4256 public override Expression DoResolve (EmitContext ec)
4258 if (NewType == NType.Object) {
4260 type = ec.TypeContainer.LookupType (RequestedType, false);
4265 bool IsDelegate = TypeManager.IsDelegateType (type);
4268 return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
4272 ml = MemberLookup (ec, type, ".ctor", false,
4273 MemberTypes.Constructor, AllBindingsFlags, Location);
4275 bool is_struct = false;
4276 is_struct = type.IsSubclassOf (TypeManager.value_type);
4278 if (! (ml is MethodGroupExpr)){
4280 report118 (Location, ml, "method group");
4286 if (Arguments != null){
4287 for (int i = Arguments.Count; i > 0;){
4289 Argument a = (Argument) Arguments [i];
4291 if (!a.Resolve (ec))
4296 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4297 Arguments, Location);
4301 if (method == null && !is_struct) {
4302 Error (-6, Location,
4303 "New invocation: Can not find a constructor for " +
4304 "this argument list");
4308 eclass = ExprClass.Value;
4313 if (NewType == NType.Array) {
4314 throw new Exception ("Finish array creation");
4321 // This DoEmit can be invoked in two contexts:
4322 // * As a mechanism that will leave a value on the stack (new object)
4323 // * As one that wont (init struct)
4325 // You can control whether a value is required on the stack by passing
4326 // need_value_on_stack. The code *might* leave a value on the stack
4327 // so it must be popped manually
4329 // Returns whether a value is left on the stack
4331 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4333 if (method == null){
4334 IMemoryLocation ml = (IMemoryLocation) value_target;
4338 Invocation.EmitArguments (ec, method, Arguments);
4339 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4344 // It must be a value type, sanity check
4346 if (value_target != null){
4347 ec.ig.Emit (OpCodes.Initobj, type);
4349 if (need_value_on_stack){
4350 value_target.Emit (ec);
4356 throw new Exception ("No method and no value type");
4359 public override void Emit (EmitContext ec)
4364 public override void EmitStatement (EmitContext ec)
4366 if (DoEmit (ec, false))
4367 ec.ig.Emit (OpCodes.Pop);
4372 // Represents the `this' construct
4374 public class This : Expression, IStackStore, IMemoryLocation {
4377 public This (Location loc)
4382 public override Expression DoResolve (EmitContext ec)
4384 eclass = ExprClass.Variable;
4385 type = ec.TypeContainer.TypeBuilder;
4388 Report.Error (26, loc,
4389 "Keyword this not valid in static code");
4396 public Expression DoResolveLValue (EmitContext ec)
4400 if (ec.TypeContainer is Class){
4401 Report.Error (1604, loc, "Cannot assign to `this'");
4408 public override void Emit (EmitContext ec)
4410 ec.ig.Emit (OpCodes.Ldarg_0);
4413 public void Store (EmitContext ec)
4415 ec.ig.Emit (OpCodes.Starg, 0);
4418 public void AddressOf (EmitContext ec)
4420 ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4425 // Implements the typeof operator
4427 public class TypeOf : Expression {
4428 public readonly string QueriedType;
4431 public TypeOf (string queried_type)
4433 QueriedType = queried_type;
4436 public override Expression DoResolve (EmitContext ec)
4438 typearg = ec.TypeContainer.LookupType (QueriedType, false);
4440 if (typearg == null)
4443 type = TypeManager.type_type;
4444 eclass = ExprClass.Type;
4448 public override void Emit (EmitContext ec)
4450 ec.ig.Emit (OpCodes.Ldtoken, typearg);
4451 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4455 public class SizeOf : Expression {
4456 public readonly string QueriedType;
4458 public SizeOf (string queried_type)
4460 this.QueriedType = queried_type;
4463 public override Expression DoResolve (EmitContext ec)
4465 // FIXME: Implement;
4466 throw new Exception ("Unimplemented");
4470 public override void Emit (EmitContext ec)
4472 throw new Exception ("Implement me");
4476 public class MemberAccess : Expression {
4477 public readonly string Identifier;
4479 Expression member_lookup;
4482 public MemberAccess (Expression expr, string id, Location l)
4489 public Expression Expr {
4495 void error176 (Location loc, string name)
4497 Report.Error (176, loc, "Static member `" +
4498 name + "' cannot be accessed " +
4499 "with an instance reference, qualify with a " +
4500 "type name instead");
4503 public override Expression DoResolve (EmitContext ec)
4505 expr = expr.Resolve (ec);
4510 if (expr is SimpleName){
4511 SimpleName child_expr = (SimpleName) expr;
4513 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4515 return expr.Resolve (ec);
4518 member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4523 if (member_lookup is MethodGroupExpr){
4524 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4529 if (expr is TypeExpr){
4530 if (!mg.RemoveInstanceMethods ()){
4531 SimpleName.Error120 (loc, mg.Methods [0].Name);
4535 return member_lookup;
4539 // Instance.MethodGroup
4541 if (!mg.RemoveStaticMethods ()){
4542 error176 (loc, mg.Methods [0].Name);
4546 mg.InstanceExpression = expr;
4548 return member_lookup;
4551 if (member_lookup is FieldExpr){
4552 FieldExpr fe = (FieldExpr) member_lookup;
4554 if (expr is TypeExpr){
4555 if (!fe.FieldInfo.IsStatic){
4556 error176 (loc, fe.FieldInfo.Name);
4559 return member_lookup;
4561 if (fe.FieldInfo.IsStatic){
4562 error176 (loc, fe.FieldInfo.Name);
4565 fe.InstanceExpression = expr;
4571 if (member_lookup is PropertyExpr){
4572 PropertyExpr pe = (PropertyExpr) member_lookup;
4574 if (expr is TypeExpr){
4576 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4582 error176 (loc, pe.PropertyInfo.Name);
4585 pe.InstanceExpression = expr;
4591 Console.WriteLine ("Support for " + member_lookup + " is not present yet");
4592 Environment.Exit (0);
4596 public override void Emit (EmitContext ec)
4598 throw new Exception ("Should not happen I think");
4604 // Fully resolved expression that evaluates to a type
4606 public class TypeExpr : Expression {
4607 public TypeExpr (Type t)
4610 eclass = ExprClass.Type;
4613 override public Expression DoResolve (EmitContext ec)
4618 override public void Emit (EmitContext ec)
4620 throw new Exception ("Implement me");
4625 // MethodGroup Expression.
4627 // This is a fully resolved expression that evaluates to a type
4629 public class MethodGroupExpr : Expression {
4630 public MethodBase [] Methods;
4631 Expression instance_expression = null;
4633 public MethodGroupExpr (MemberInfo [] mi)
4635 Methods = new MethodBase [mi.Length];
4636 mi.CopyTo (Methods, 0);
4637 eclass = ExprClass.MethodGroup;
4640 public MethodGroupExpr (ArrayList l)
4642 Methods = new MethodBase [l.Count];
4644 l.CopyTo (Methods, 0);
4645 eclass = ExprClass.MethodGroup;
4649 // `A method group may have associated an instance expression'
4651 public Expression InstanceExpression {
4653 return instance_expression;
4657 instance_expression = value;
4661 override public Expression DoResolve (EmitContext ec)
4666 override public void Emit (EmitContext ec)
4668 throw new Exception ("This should never be reached");
4671 bool RemoveMethods (bool keep_static)
4673 ArrayList smethods = new ArrayList ();
4674 int top = Methods.Length;
4677 for (i = 0; i < top; i++){
4678 MethodBase mb = Methods [i];
4680 if (mb.IsStatic == keep_static)
4684 if (smethods.Count == 0)
4687 Methods = new MethodBase [smethods.Count];
4688 smethods.CopyTo (Methods, 0);
4694 // Removes any instance methods from the MethodGroup, returns
4695 // false if the resulting set is empty.
4697 public bool RemoveInstanceMethods ()
4699 return RemoveMethods (true);
4703 // Removes any static methods from the MethodGroup, returns
4704 // false if the resulting set is empty.
4706 public bool RemoveStaticMethods ()
4708 return RemoveMethods (false);
4713 // Fully resolved expression that evaluates to a Field
4715 public class FieldExpr : Expression, IStackStore, IMemoryLocation {
4716 public readonly FieldInfo FieldInfo;
4717 public Expression InstanceExpression;
4720 public FieldExpr (FieldInfo fi, Location l)
4723 eclass = ExprClass.Variable;
4724 type = fi.FieldType;
4728 override public Expression DoResolve (EmitContext ec)
4730 if (!FieldInfo.IsStatic){
4731 if (InstanceExpression == null){
4732 throw new Exception ("non-static FieldExpr without instance var\n" +
4733 "You have to assign the Instance variable\n" +
4734 "Of the FieldExpr to set this\n");
4737 InstanceExpression = InstanceExpression.Resolve (ec);
4738 if (InstanceExpression == null)
4745 public Expression DoResolveLValue (EmitContext ec)
4747 if (!FieldInfo.IsInitOnly)
4751 // InitOnly fields can only be assigned in constructors
4754 if (ec.IsConstructor)
4757 Report.Error (191, loc,
4758 "Readonly field can not be assigned outside " +
4759 "of constructor or variable initializer");
4764 override public void Emit (EmitContext ec)
4766 ILGenerator ig = ec.ig;
4768 if (FieldInfo.IsStatic)
4769 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4771 InstanceExpression.Emit (ec);
4773 ig.Emit (OpCodes.Ldfld, FieldInfo);
4777 public void Store (EmitContext ec)
4779 if (FieldInfo.IsStatic)
4780 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
4782 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
4785 public void AddressOf (EmitContext ec)
4787 if (FieldInfo.IsStatic)
4788 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
4790 InstanceExpression.Emit (ec);
4791 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
4797 // Expression that evaluates to a Property. The Assign class
4798 // might set the `Value' expression if we are in an assignment.
4800 // This is not an LValue because we need to re-write the expression, we
4801 // can not take data from the stack and store it.
4803 public class PropertyExpr : ExpressionStatement {
4804 public readonly PropertyInfo PropertyInfo;
4805 public readonly bool IsStatic;
4806 MethodInfo [] Accessors;
4809 Expression instance_expr;
4812 public PropertyExpr (PropertyInfo pi, Location l)
4815 eclass = ExprClass.PropertyAccess;
4818 Accessors = TypeManager.GetAccessors (pi);
4820 if (Accessors != null)
4821 for (int i = 0; i < Accessors.Length; i++){
4822 if (Accessors [i] != null)
4823 if (Accessors [i].IsStatic)
4827 Accessors = new MethodInfo [2];
4829 type = pi.PropertyType;
4833 // Controls the Value of the PropertyExpr. If the value
4834 // is null, then the property is being used in a `read' mode.
4835 // otherwise the property is used in assignment mode.
4837 // The value is set to a fully resolved type by assign.
4839 public Expression Value {
4850 // The instance expression associated with this expression
4852 public Expression InstanceExpression {
4854 instance_expr = value;
4858 return instance_expr;
4862 public bool VerifyAssignable ()
4864 if (!PropertyInfo.CanWrite){
4865 Report.Error (200, loc,
4866 "The property `" + PropertyInfo.Name +
4867 "' can not be assigned to, as it has not set accessor");
4874 override public Expression DoResolve (EmitContext ec)
4876 if (!PropertyInfo.CanRead){
4877 Report.Error (154, loc,
4878 "The property `" + PropertyInfo.Name +
4879 "' can not be used in " +
4880 "this context because it lacks a get accessor");
4887 override public void Emit (EmitContext ec)
4890 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
4892 Argument arg = new Argument (value, Argument.AType.Expression);
4893 ArrayList args = new ArrayList ();
4896 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
4900 override public void EmitStatement (EmitContext ec)
4904 ec.ig.Emit (OpCodes.Pop);
4910 // Fully resolved expression that evaluates to a Expression
4912 public class EventExpr : Expression {
4913 public readonly EventInfo EventInfo;
4916 public EventExpr (EventInfo ei, Location loc)
4920 eclass = ExprClass.EventAccess;
4923 override public Expression DoResolve (EmitContext ec)
4925 // We are born in resolved state.
4929 override public void Emit (EmitContext ec)
4931 throw new Exception ("Implement me");
4932 // FIXME: Implement.
4936 public class CheckedExpr : Expression {
4938 public Expression Expr;
4940 public CheckedExpr (Expression e)
4945 public override Expression DoResolve (EmitContext ec)
4947 Expr = Expr.Resolve (ec);
4952 eclass = Expr.ExprClass;
4957 public override void Emit (EmitContext ec)
4959 bool last_check = ec.CheckState;
4961 ec.CheckState = true;
4963 ec.CheckState = last_check;
4968 public class UnCheckedExpr : Expression {
4970 public Expression Expr;
4972 public UnCheckedExpr (Expression e)
4977 public override Expression DoResolve (EmitContext ec)
4979 Expr = Expr.Resolve (ec);
4984 eclass = Expr.ExprClass;
4989 public override void Emit (EmitContext ec)
4991 bool last_check = ec.CheckState;
4993 ec.CheckState = false;
4995 ec.CheckState = last_check;
5000 public class ElementAccess : Expression {
5002 public ArrayList Arguments;
5003 public Expression Expr;
5004 public Location loc;
5006 public ElementAccess (Expression e, ArrayList e_list, Location l)
5010 Arguments = new ArrayList ();
5011 foreach (Expression tmp in e_list)
5012 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
5017 bool CommonResolve (EmitContext ec)
5019 Expr = Expr.Resolve (ec);
5024 if (Arguments == null)
5027 for (int i = Arguments.Count; i > 0;){
5029 Argument a = (Argument) Arguments [i];
5031 if (!a.Resolve (ec))
5038 public override Expression DoResolve (EmitContext ec)
5040 if (!CommonResolve (ec))
5044 // We perform some simple tests, and then to "split" the emit and store
5045 // code we create an instance of a different class, and return that.
5047 // I am experimenting with this pattern.
5049 if (Expr.Type == TypeManager.array_type)
5050 return (new ArrayAccess (this)).Resolve (ec);
5052 return (new IndexerAccess (this)).Resolve (ec);
5055 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5057 if (!CommonResolve (ec))
5060 if (Expr.Type == TypeManager.array_type)
5061 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
5063 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
5066 public override void Emit (EmitContext ec)
5068 throw new Exception ("Should never be reached");
5072 public class ArrayAccess : Expression, IStackStore {
5074 // Points to our "data" repository
5078 public ArrayAccess (ElementAccess ea_data)
5081 eclass = ExprClass.Variable;
5084 // FIXME: Figure out the type here
5088 Expression CommonResolve (EmitContext ec)
5093 public override Expression DoResolve (EmitContext ec)
5095 if (ea.Expr.ExprClass != ExprClass.Variable) {
5096 report118 (ea.loc, ea.Expr, "variable");
5100 throw new Exception ("Implement me");
5103 public void Store (EmitContext ec)
5105 throw new Exception ("Implement me !");
5108 public override void Emit (EmitContext ec)
5110 throw new Exception ("Implement me !");
5115 public ArrayList getters, setters;
5116 static Hashtable map;
5120 map = new Hashtable ();
5123 Indexers (MemberInfo [] mi)
5125 foreach (PropertyInfo property in mi){
5126 MethodInfo get, set;
5128 get = property.GetGetMethod (true);
5130 if (getters == null)
5131 getters = new ArrayList ();
5136 set = property.GetSetMethod (true);
5138 if (setters == null)
5139 setters = new ArrayList ();
5145 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
5147 Indexers ix = (Indexers) map [t];
5148 string p_name = TypeManager.IndexerPropertyName (t);
5153 MemberInfo [] mi = tm.FindMembers (
5154 t, MemberTypes.Property,
5155 BindingFlags.Public | BindingFlags.Instance,
5156 Type.FilterName, p_name);
5158 if (mi == null || mi.Length == 0){
5159 Report.Error (21, loc,
5160 "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5161 "any indexers defined");
5165 ix = new Indexers (mi);
5172 public class IndexerAccess : Expression {
5174 // Points to our "data" repository
5177 MethodInfo get, set;
5179 ArrayList set_arguments;
5181 public IndexerAccess (ElementAccess ea_data)
5184 eclass = ExprClass.Value;
5187 public bool VerifyAssignable (Expression source)
5189 throw new Exception ("Implement me!");
5192 public override Expression DoResolve (EmitContext ec)
5194 Type indexer_type = ea.Expr.Type;
5197 // Step 1: Query for all `Item' *properties*. Notice
5198 // that the actual methods are pointed from here.
5200 // This is a group of properties, piles of them.
5203 ilist = Indexers.GetIndexersForType (
5204 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5206 if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5207 get = (MethodInfo) Invocation.OverloadResolve (
5208 ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5211 Report.Error (154, ea.loc,
5212 "indexer can not be used in this context, because " +
5213 "it lacks a `get' accessor");
5217 type = get.ReturnType;
5218 eclass = ExprClass.Value;
5222 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5224 Type indexer_type = ea.Expr.Type;
5225 Type right_type = right_side.Type;
5228 ilist = Indexers.GetIndexersForType (
5229 indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5231 Console.WriteLine ("ilist = " + ilist);
5233 Console.WriteLine ("ilist.setters = " + ilist.setters);
5234 if (ilist.setters != null)
5235 Console.WriteLine ("count = " + ilist.setters.Count);
5237 if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5238 set_arguments = (ArrayList) ea.Arguments.Clone ();
5239 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5241 set = (MethodInfo) Invocation.OverloadResolve (
5242 ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5246 Report.Error (200, ea.loc,
5247 "indexer X.this [" + TypeManager.CSharpName (right_type) +
5248 "] lacks a `set' accessor");
5252 type = TypeManager.void_type;
5253 eclass = ExprClass.Value;
5257 public override void Emit (EmitContext ec)
5259 Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5262 public void EmitSet (EmitContext ec, Expression expr)
5264 throw new Exception ("Implement me!");
5268 public class BaseAccess : Expression {
5270 public enum BaseAccessType {
5275 public readonly BaseAccessType BAType;
5276 public readonly string Member;
5277 public readonly ArrayList Arguments;
5279 public BaseAccess (BaseAccessType t, string member, ArrayList args)
5287 public override Expression DoResolve (EmitContext ec)
5289 // FIXME: Implement;
5290 throw new Exception ("Unimplemented");
5294 public override void Emit (EmitContext ec)
5296 throw new Exception ("Unimplemented");
5301 // This class exists solely to pass the Type around and to be a dummy
5302 // that can be passed to the conversion functions (this is used by
5303 // foreach implementation to typecast the object return value from
5304 // get_Current into the proper type. All code has been generated and
5305 // we only care about the side effect conversions to be performed
5308 public class EmptyExpression : Expression {
5309 public EmptyExpression ()
5311 type = TypeManager.object_type;
5312 eclass = ExprClass.Value;
5315 public override Expression DoResolve (EmitContext ec)
5320 public override void Emit (EmitContext ec)
5322 // nothing, as we only exist to not do anything.
5326 public class UserCast : Expression {
5330 public UserCast (MethodInfo method, Expression source)
5332 this.method = method;
5333 this.source = source;
5334 type = method.ReturnType;
5335 eclass = ExprClass.Value;
5338 public override Expression DoResolve (EmitContext ec)
5341 // We are born fully resolved
5346 public override void Emit (EmitContext ec)
5348 ILGenerator ig = ec.ig;
5352 if (method is MethodInfo)
5353 ig.Emit (OpCodes.Call, (MethodInfo) method);
5355 ig.Emit (OpCodes.Call, (ConstructorInfo) method);