2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 /// This interface is implemented by variables
42 public interface IMemoryLocation {
44 /// The AddressOf method should generate code that loads
45 /// the address of the object and leaves it on the stack
47 void AddressOf (EmitContext ec);
51 /// Base class for expressions
53 public abstract class Expression {
54 protected ExprClass eclass;
67 public ExprClass ExprClass {
78 /// Utility wrapper routine for Error, just to beautify the code
80 static protected void Error (int error, string s)
82 Report.Error (error, s);
85 static protected void Error (int error, Location loc, string s)
87 Report.Error (error, loc, s);
91 /// Utility wrapper routine for Warning, just to beautify the code
93 static protected void Warning (int warning, string s)
95 Report.Warning (warning, s);
98 static public void error30 (Location loc, Type source, Type target)
100 Report.Error (30, loc, "Cannot convert type '" +
101 TypeManager.CSharpName (source) + "' to '" +
102 TypeManager.CSharpName (target) + "'");
106 /// Performs semantic analysis on the Expression
110 /// The Resolve method is invoked to perform the semantic analysis
113 /// The return value is an expression (it can be the
114 /// same expression in some cases) or a new
115 /// expression that better represents this node.
117 /// For example, optimizations of Unary (LiteralInt)
118 /// would return a new LiteralInt with a negated
121 /// If there is an error during semantic analysis,
122 /// then an error should be reported (using Report)
123 /// and a null value should be returned.
125 /// There are two side effects expected from calling
126 /// Resolve(): the the field variable "eclass" should
127 /// be set to any value of the enumeration
128 /// `ExprClass' and the type variable should be set
129 /// to a valid type (this is the type of the
132 public abstract Expression DoResolve (EmitContext ec);
134 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
136 return DoResolve (ec);
140 /// Resolves an expression and performs semantic analysis on it.
144 /// Currently Resolve wraps DoResolve to perform sanity
145 /// checking and assertion checking on what we expect from Resolve.
147 public Expression Resolve (EmitContext ec)
149 Expression e = DoResolve (ec);
152 if (e is SimpleName){
153 SimpleName s = (SimpleName) e;
157 "The name `" + s.Name + "' could not be found in `" +
158 ec.TypeContainer.Name + "'");
162 if (e.ExprClass == ExprClass.Invalid)
163 throw new Exception ("Expression " + e +
164 " ExprClass is Invalid after resolve");
166 if (e.ExprClass != ExprClass.MethodGroup)
168 throw new Exception ("Expression " + e +
169 " did not set its type after Resolve");
176 /// Performs expression resolution and semantic analysis, but
177 /// allows SimpleNames to be returned.
181 /// This is used by MemberAccess to construct long names that can not be
182 /// partially resolved (namespace-qualified names for example).
184 public Expression ResolveWithSimpleName (EmitContext ec)
186 Expression e = DoResolve (ec);
192 if (e.ExprClass == ExprClass.Invalid)
193 throw new Exception ("Expression " + e +
194 " ExprClass is Invalid after resolve");
196 if (e.ExprClass != ExprClass.MethodGroup)
198 throw new Exception ("Expression " + e +
199 " did not set its type after Resolve");
206 /// Resolves an expression for LValue assignment
210 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
211 /// checking and assertion checking on what we expect from Resolve
213 public Expression ResolveLValue (EmitContext ec, Expression right_side)
215 Expression e = DoResolveLValue (ec, right_side);
218 if (e is SimpleName){
219 SimpleName s = (SimpleName) e;
223 "The name `" + s.Name + "' could not be found in `" +
224 ec.TypeContainer.Name + "'");
228 if (e.ExprClass == ExprClass.Invalid)
229 throw new Exception ("Expression " + e +
230 " ExprClass is Invalid after resolve");
232 if (e.ExprClass != ExprClass.MethodGroup)
234 throw new Exception ("Expression " + e +
235 " did not set its type after Resolve");
242 /// Emits the code for the expression
246 /// The Emit method is invoked to generate the code
247 /// for the expression.
249 public abstract void Emit (EmitContext ec);
252 /// This method should perform a reduction of the expression. This should
253 /// never return null.
255 public virtual Expression Reduce (EmitContext ec)
261 /// Protected constructor. Only derivate types should
262 /// be able to be created
265 protected Expression ()
267 eclass = ExprClass.Invalid;
272 /// Returns a literalized version of a literal FieldInfo
276 /// The possible return values are:
277 /// IntLiteral, UIntLiteral
278 /// LongLiteral, ULongLiteral
279 /// FloatLiteral, DoubleLiteral
282 public static Expression Literalize (object v, Type t)
284 if (t == TypeManager.int32_type)
285 return new IntLiteral ((int) v);
286 else if (t == TypeManager.uint32_type)
287 return new UIntLiteral ((uint) v);
288 else if (t == TypeManager.int64_type)
289 return new LongLiteral ((long) v);
290 else if (t == TypeManager.uint64_type)
291 return new ULongLiteral ((ulong) v);
292 else if (t == TypeManager.float_type)
293 return new FloatLiteral ((float) v);
294 else if (t == TypeManager.double_type)
295 return new DoubleLiteral ((double) v);
296 else if (t == TypeManager.string_type)
297 return new StringLiteral ((string) v);
298 else if (t == TypeManager.short_type)
299 return new IntLiteral ((int) ((short)v));
300 else if (t == TypeManager.ushort_type)
301 return new IntLiteral ((int) ((ushort)v));
302 else if (t == TypeManager.sbyte_type)
303 return new IntLiteral ((int) ((sbyte)v));
304 else if (t == TypeManager.byte_type)
305 return new IntLiteral ((int) ((byte)v));
306 else if (t == TypeManager.char_type)
307 return new IntLiteral ((int) ((char)v));
309 throw new Exception ("Unknown type for literal (" + t +
314 /// Returns a fully formed expression after a MemberLookup
316 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
319 return new EventExpr ((EventInfo) mi, loc);
320 else if (mi is FieldInfo)
321 return new FieldExpr ((FieldInfo) mi, loc);
322 else if (mi is PropertyInfo)
323 return new PropertyExpr ((PropertyInfo) mi, loc);
325 return new TypeExpr ((Type) mi);
331 // FIXME: Probably implement a cache for (t,name,current_access_set)?
333 // FIXME: We need to cope with access permissions here, or this wont
336 // This code could use some optimizations, but we need to do some
337 // measurements. For example, we could use a delegate to `flag' when
338 // something can not any longer be a method-group (because it is something
342 // If the return value is an Array, then it is an array of
345 // If the return value is an MemberInfo, it is anything, but a Method
349 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
350 // the arguments here and have MemberLookup return only the methods that
351 // match the argument count/type, unlike we are doing now (we delay this
354 // This is so we can catch correctly attempts to invoke instance methods
355 // from a static body (scan for error 120 in ResolveSimpleName).
357 public static Expression MemberLookup (EmitContext ec, Type t, string name,
358 bool same_type, MemberTypes mt,
359 BindingFlags bf, Location loc)
362 bf |= BindingFlags.NonPublic;
364 MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
365 t, mt, bf, Type.FilterName, name);
371 // This arises when we use FindMembers from SRE. Our code prefers to send a null back
375 if (mi.Length == 1 && !(mi [0] is MethodBase))
376 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
378 for (int i = 0; i < mi.Length; i++)
379 if (!(mi [i] is MethodBase)){
380 Error (-5, "Do not know how to reproduce this case: " +
381 "Methods and non-Method with the same name, " +
382 "report this please");
384 Console.WriteLine (name);
385 for (i = 0; i < mi.Length; i++){
386 Type tt = mi [i].GetType ();
388 Console.WriteLine (i + ": " + mi [i]);
389 while (tt != TypeManager.object_type){
390 Console.WriteLine (tt);
396 return new MethodGroupExpr (mi);
399 public const MemberTypes AllMemberTypes =
400 MemberTypes.Constructor |
404 MemberTypes.NestedType |
405 MemberTypes.Property;
407 public const BindingFlags AllBindingsFlags =
408 BindingFlags.Public |
409 BindingFlags.Static |
410 BindingFlags.Instance;
412 public static Expression MemberLookup (EmitContext ec, Type t, string name,
413 bool same_type, Location loc)
415 return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
418 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
420 Type expr_type = expr.Type;
422 if (target_type == TypeManager.object_type) {
423 if (expr_type.IsClass)
424 return new EmptyCast (expr, target_type);
425 if (expr_type.IsValueType)
426 return new BoxedCast (expr);
427 } else if (expr_type.IsSubclassOf (target_type)) {
428 return new EmptyCast (expr, target_type);
430 // from any class-type S to any interface-type T.
431 if (expr_type.IsClass && target_type.IsInterface) {
433 if (TypeManager.ImplementsInterface (expr_type, target_type))
434 return new EmptyCast (expr, target_type);
439 // from any interface type S to interface-type T.
440 if (expr_type.IsInterface && target_type.IsInterface) {
442 if (TypeManager.ImplementsInterface (expr_type, target_type))
443 return new EmptyCast (expr, target_type);
448 // from an array-type S to an array-type of type T
449 if (expr_type.IsArray && target_type.IsArray) {
450 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
452 Type expr_element_type = expr_type.GetElementType ();
453 Type target_element_type = target_type.GetElementType ();
455 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
456 if (StandardConversionExists (expr_element_type,
457 target_element_type))
458 return new EmptyCast (expr, target_type);
463 // from an array-type to System.Array
464 if (expr_type.IsArray && target_type == TypeManager.array_type)
465 return new EmptyCast (expr, target_type);
467 // from any delegate type to System.Delegate
468 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
469 target_type == TypeManager.delegate_type)
470 return new EmptyCast (expr, target_type);
472 // from any array-type or delegate type into System.ICloneable.
473 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
474 if (target_type == TypeManager.icloneable_type)
475 return new EmptyCast (expr, target_type);
477 // from the null type to any reference-type.
478 if (expr is NullLiteral)
479 return new EmptyCast (expr, target_type);
489 /// Handles expressions like this: decimal d; d = 1;
490 /// and changes them into: decimal d; d = new System.Decimal (1);
492 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
494 ArrayList args = new ArrayList ();
496 args.Add (new Argument (expr, Argument.AType.Expression));
498 Expression ne = new New (target.FullName, args,
501 return ne.Resolve (ec);
505 /// Implicit Numeric Conversions.
507 /// expr is the expression to convert, returns a new expression of type
508 /// target_type or null if an implicit conversion is not possible.
510 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
511 Type target_type, Location loc)
513 Type expr_type = expr.Type;
516 // Attempt to do the implicit constant expression conversions
518 if (expr is IntLiteral){
521 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
525 } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
527 // Try the implicit constant expression conversion
528 // from long to ulong, instead of a nice routine,
531 if (((LongLiteral) expr).Value > 0)
532 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
535 if (expr_type == TypeManager.sbyte_type){
537 // From sbyte to short, int, long, float, double.
539 if (target_type == TypeManager.int32_type)
540 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
541 if (target_type == TypeManager.int64_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
543 if (target_type == TypeManager.double_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
545 if (target_type == TypeManager.float_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
547 if (target_type == TypeManager.short_type)
548 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
549 if (target_type == TypeManager.decimal_type)
550 return InternalTypeConstructor (ec, expr, target_type);
551 } else if (expr_type == TypeManager.byte_type){
553 // From byte to short, ushort, int, uint, long, ulong, float, double
555 if ((target_type == TypeManager.short_type) ||
556 (target_type == TypeManager.ushort_type) ||
557 (target_type == TypeManager.int32_type) ||
558 (target_type == TypeManager.uint32_type))
559 return new EmptyCast (expr, target_type);
561 if (target_type == TypeManager.uint64_type)
562 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
563 if (target_type == TypeManager.int64_type)
564 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
566 if (target_type == TypeManager.float_type)
567 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
568 if (target_type == TypeManager.double_type)
569 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
570 if (target_type == TypeManager.decimal_type)
571 return InternalTypeConstructor (ec, expr, target_type);
572 } else if (expr_type == TypeManager.short_type){
574 // From short to int, long, float, double
576 if (target_type == TypeManager.int32_type)
577 return new EmptyCast (expr, target_type);
578 if (target_type == TypeManager.int64_type)
579 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
580 if (target_type == TypeManager.double_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
582 if (target_type == TypeManager.float_type)
583 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
584 if (target_type == TypeManager.decimal_type)
585 return InternalTypeConstructor (ec, expr, target_type);
586 } else if (expr_type == TypeManager.ushort_type){
588 // From ushort to int, uint, long, ulong, float, double
590 if (target_type == TypeManager.uint32_type)
591 return new EmptyCast (expr, target_type);
593 if (target_type == TypeManager.uint64_type)
594 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
595 if (target_type == TypeManager.int32_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
597 if (target_type == TypeManager.int64_type)
598 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
599 if (target_type == TypeManager.double_type)
600 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
601 if (target_type == TypeManager.float_type)
602 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
603 if (target_type == TypeManager.decimal_type)
604 return InternalTypeConstructor (ec, expr, target_type);
605 } else if (expr_type == TypeManager.int32_type){
607 // From int to long, float, double
609 if (target_type == TypeManager.int64_type)
610 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
611 if (target_type == TypeManager.double_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
613 if (target_type == TypeManager.float_type)
614 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
615 if (target_type == TypeManager.decimal_type)
616 return InternalTypeConstructor (ec, expr, target_type);
617 } else if (expr_type == TypeManager.uint32_type){
619 // From uint to long, ulong, float, double
621 if (target_type == TypeManager.int64_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
623 if (target_type == TypeManager.uint64_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
625 if (target_type == TypeManager.double_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
628 if (target_type == TypeManager.float_type)
629 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
631 if (target_type == TypeManager.decimal_type)
632 return InternalTypeConstructor (ec, expr, target_type);
633 } else if ((expr_type == TypeManager.uint64_type) ||
634 (expr_type == TypeManager.int64_type)){
636 // From long/ulong to float, double
638 if (target_type == TypeManager.double_type)
639 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
641 if (target_type == TypeManager.float_type)
642 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
644 if (target_type == TypeManager.decimal_type)
645 return InternalTypeConstructor (ec, expr, target_type);
646 } else if (expr_type == TypeManager.char_type){
648 // From char to ushort, int, uint, long, ulong, float, double
650 if ((target_type == TypeManager.ushort_type) ||
651 (target_type == TypeManager.int32_type) ||
652 (target_type == TypeManager.uint32_type))
653 return new EmptyCast (expr, target_type);
654 if (target_type == TypeManager.uint64_type)
655 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
656 if (target_type == TypeManager.int64_type)
657 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
658 if (target_type == TypeManager.float_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
660 if (target_type == TypeManager.double_type)
661 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
662 if (target_type == TypeManager.decimal_type)
663 return InternalTypeConstructor (ec, expr, target_type);
664 } else if (expr_type == TypeManager.float_type){
668 if (target_type == TypeManager.double_type)
669 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
676 /// Determines if a standard implicit conversion exists from
677 /// expr_type to target_type
679 public static bool StandardConversionExists (Type expr_type, Type target_type)
681 if (expr_type == target_type)
684 // First numeric conversions
686 if (expr_type == TypeManager.sbyte_type){
688 // From sbyte to short, int, long, float, double.
690 if ((target_type == TypeManager.int32_type) ||
691 (target_type == TypeManager.int64_type) ||
692 (target_type == TypeManager.double_type) ||
693 (target_type == TypeManager.float_type) ||
694 (target_type == TypeManager.short_type) ||
695 (target_type == TypeManager.decimal_type))
698 } else if (expr_type == TypeManager.byte_type){
700 // From byte to short, ushort, int, uint, long, ulong, float, double
702 if ((target_type == TypeManager.short_type) ||
703 (target_type == TypeManager.ushort_type) ||
704 (target_type == TypeManager.int32_type) ||
705 (target_type == TypeManager.uint32_type) ||
706 (target_type == TypeManager.uint64_type) ||
707 (target_type == TypeManager.int64_type) ||
708 (target_type == TypeManager.float_type) ||
709 (target_type == TypeManager.double_type) ||
710 (target_type == TypeManager.decimal_type))
713 } else if (expr_type == TypeManager.short_type){
715 // From short to int, long, float, double
717 if ((target_type == TypeManager.int32_type) ||
718 (target_type == TypeManager.int64_type) ||
719 (target_type == TypeManager.double_type) ||
720 (target_type == TypeManager.float_type) ||
721 (target_type == TypeManager.decimal_type))
724 } else if (expr_type == TypeManager.ushort_type){
726 // From ushort to int, uint, long, ulong, float, double
728 if ((target_type == TypeManager.uint32_type) ||
729 (target_type == TypeManager.uint64_type) ||
730 (target_type == TypeManager.int32_type) ||
731 (target_type == TypeManager.int64_type) ||
732 (target_type == TypeManager.double_type) ||
733 (target_type == TypeManager.float_type) ||
734 (target_type == TypeManager.decimal_type))
737 } else if (expr_type == TypeManager.int32_type){
739 // From int to long, float, double
741 if ((target_type == TypeManager.int64_type) ||
742 (target_type == TypeManager.double_type) ||
743 (target_type == TypeManager.float_type) ||
744 (target_type == TypeManager.decimal_type))
747 } else if (expr_type == TypeManager.uint32_type){
749 // From uint to long, ulong, float, double
751 if ((target_type == TypeManager.int64_type) ||
752 (target_type == TypeManager.uint64_type) ||
753 (target_type == TypeManager.double_type) ||
754 (target_type == TypeManager.float_type) ||
755 (target_type == TypeManager.decimal_type))
758 } else if ((expr_type == TypeManager.uint64_type) ||
759 (expr_type == TypeManager.int64_type)) {
761 // From long/ulong to float, double
763 if ((target_type == TypeManager.double_type) ||
764 (target_type == TypeManager.float_type) ||
765 (target_type == TypeManager.decimal_type))
768 } else if (expr_type == TypeManager.char_type){
770 // From char to ushort, int, uint, long, ulong, float, double
772 if ((target_type == TypeManager.ushort_type) ||
773 (target_type == TypeManager.int32_type) ||
774 (target_type == TypeManager.uint32_type) ||
775 (target_type == TypeManager.uint64_type) ||
776 (target_type == TypeManager.int64_type) ||
777 (target_type == TypeManager.float_type) ||
778 (target_type == TypeManager.double_type) ||
779 (target_type == TypeManager.decimal_type))
782 } else if (expr_type == TypeManager.float_type){
786 if (target_type == TypeManager.double_type)
790 // Next reference conversions
792 if (target_type == TypeManager.object_type) {
793 if ((expr_type.IsClass) ||
794 (expr_type.IsValueType))
797 } else if (expr_type.IsSubclassOf (target_type)) {
801 // from any class-type S to any interface-type T.
802 if (expr_type.IsClass && target_type.IsInterface)
805 // from any interface type S to interface-type T.
806 // FIXME : Is it right to use IsAssignableFrom ?
807 if (expr_type.IsInterface && target_type.IsInterface)
808 if (target_type.IsAssignableFrom (expr_type))
811 // from an array-type S to an array-type of type T
812 if (expr_type.IsArray && target_type.IsArray) {
813 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
815 Type expr_element_type = expr_type.GetElementType ();
816 Type target_element_type = target_type.GetElementType ();
818 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
819 if (StandardConversionExists (expr_element_type,
820 target_element_type))
825 // from an array-type to System.Array
826 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
829 // from any delegate type to System.Delegate
830 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
831 target_type == TypeManager.delegate_type)
832 if (target_type.IsAssignableFrom (expr_type))
835 // from any array-type or delegate type into System.ICloneable.
836 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
837 if (target_type == TypeManager.icloneable_type)
840 // from the null type to any reference-type.
841 // FIXME : How do we do this ?
849 /// Finds "most encompassed type" according to the spec (13.4.2)
850 /// amongst the methods in the MethodGroupExpr which convert from a
851 /// type encompassing source_type
853 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
857 for (int i = me.Methods.Length; i > 0; ) {
860 MethodBase mb = me.Methods [i];
861 ParameterData pd = Invocation.GetParameterData (mb);
862 Type param_type = pd.ParameterType (0);
864 if (StandardConversionExists (source_type, param_type)) {
868 if (StandardConversionExists (param_type, best))
877 /// Finds "most encompassing type" according to the spec (13.4.2)
878 /// amongst the methods in the MethodGroupExpr which convert to a
879 /// type encompassed by target_type
881 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
885 for (int i = me.Methods.Length; i > 0; ) {
888 MethodInfo mi = (MethodInfo) me.Methods [i];
889 Type ret_type = mi.ReturnType;
891 if (StandardConversionExists (ret_type, target)) {
895 if (!StandardConversionExists (ret_type, best))
907 /// User-defined Implicit conversions
909 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
910 Type target, Location loc)
912 return UserDefinedConversion (ec, source, target, loc, false);
916 /// User-defined Explicit conversions
918 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
919 Type target, Location loc)
921 return UserDefinedConversion (ec, source, target, loc, true);
925 /// User-defined conversions
927 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
928 Type target, Location loc,
929 bool look_for_explicit)
931 Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
932 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
934 MethodBase method = null;
935 Type source_type = source.Type;
939 // If we have a boolean type, we need to check for the True operator
941 // FIXME : How does the False operator come into the picture ?
942 // FIXME : This doesn't look complete and very correct !
943 if (target == TypeManager.bool_type)
946 op_name = "op_Implicit";
948 mg1 = MemberLookup (ec, source_type, op_name, false, loc);
950 if (source_type.BaseType != null)
951 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
953 mg3 = MemberLookup (ec, target, op_name, false, loc);
955 if (target.BaseType != null)
956 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
958 MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
959 MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
961 MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
963 MethodGroupExpr union4 = null;
965 if (look_for_explicit) {
967 op_name = "op_Explicit";
969 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
971 if (source_type.BaseType != null)
972 mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
974 mg7 = MemberLookup (ec, target, op_name, false, loc);
976 if (target.BaseType != null)
977 mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
979 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
980 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
982 union4 = Invocation.MakeUnionSet (union5, union6);
985 MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
989 Type most_specific_source, most_specific_target;
991 most_specific_source = FindMostEncompassedType (union, source_type);
992 if (most_specific_source == null)
995 most_specific_target = FindMostEncompassingType (union, target);
996 if (most_specific_target == null)
1001 for (int i = union.Methods.Length; i > 0;) {
1004 MethodBase mb = union.Methods [i];
1005 ParameterData pd = Invocation.GetParameterData (mb);
1006 MethodInfo mi = (MethodInfo) union.Methods [i];
1008 if (pd.ParameterType (0) == most_specific_source &&
1009 mi.ReturnType == most_specific_target) {
1015 if (method == null || count > 1) {
1016 Report.Error (-11, loc, "Ambiguous user defined conversion");
1021 // This will do the conversion to the best match that we
1022 // found. Now we need to perform an implict standard conversion
1023 // if the best match was not the type that we were requested
1026 if (look_for_explicit)
1027 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1029 source = ConvertImplicitStandard (ec, source,
1030 most_specific_source, loc);
1035 e = new UserCast ((MethodInfo) method, source);
1037 if (e.Type != target){
1038 if (!look_for_explicit)
1039 e = ConvertImplicitStandard (ec, e, target, loc);
1041 e = ConvertExplicitStandard (ec, e, target, loc);
1052 /// Converts implicitly the resolved expression `expr' into the
1053 /// `target_type'. It returns a new expression that can be used
1054 /// in a context that expects a `target_type'.
1056 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1057 Type target_type, Location loc)
1059 Type expr_type = expr.Type;
1062 if (expr_type == target_type)
1065 if (target_type == null)
1066 throw new Exception ("Target type is null");
1068 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1072 e = ImplicitReferenceConversion (expr, target_type);
1076 e = ImplicitUserConversion (ec, expr, target_type, loc);
1080 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1081 IntLiteral i = (IntLiteral) expr;
1084 return new EmptyCast (expr, target_type);
1092 /// Attempts to apply the `Standard Implicit
1093 /// Conversion' rules to the expression `expr' into
1094 /// the `target_type'. It returns a new expression
1095 /// that can be used in a context that expects a
1098 /// This is different from `ConvertImplicit' in that the
1099 /// user defined implicit conversions are excluded.
1101 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1102 Type target_type, Location loc)
1104 Type expr_type = expr.Type;
1107 if (expr_type == target_type)
1110 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1114 e = ImplicitReferenceConversion (expr, target_type);
1118 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1119 IntLiteral i = (IntLiteral) expr;
1122 return new EmptyCast (expr, target_type);
1128 /// Attemps to perform an implict constant conversion of the IntLiteral
1129 /// into a different data type using casts (See Implicit Constant
1130 /// Expression Conversions)
1132 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1134 int value = il.Value;
1136 if (target_type == TypeManager.sbyte_type){
1137 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1138 return new EmptyCast (il, target_type);
1139 } else if (target_type == TypeManager.byte_type){
1140 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1141 return new EmptyCast (il, target_type);
1142 } else if (target_type == TypeManager.short_type){
1143 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1144 return new EmptyCast (il, target_type);
1145 } else if (target_type == TypeManager.ushort_type){
1146 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1147 return new EmptyCast (il, target_type);
1148 } else if (target_type == TypeManager.uint32_type){
1150 // we can optimize this case: a positive int32
1151 // always fits on a uint32
1154 return new EmptyCast (il, target_type);
1155 } else if (target_type == TypeManager.uint64_type){
1157 // we can optimize this case: a positive int32
1158 // always fits on a uint64. But we need an opcode
1162 return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1169 /// Attemptes to implicityly convert `target' into `type', using
1170 /// ConvertImplicit. If there is no implicit conversion, then
1171 /// an error is signaled
1173 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1174 Type target_type, Location loc)
1178 e = ConvertImplicit (ec, source, target_type, loc);
1182 string msg = "Cannot convert implicitly from `"+
1183 TypeManager.CSharpName (source.Type) + "' to `" +
1184 TypeManager.CSharpName (target_type) + "'";
1186 Error (29, loc, msg);
1192 /// Performs the explicit numeric conversions
1194 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1197 Type expr_type = expr.Type;
1199 if (expr_type == TypeManager.sbyte_type){
1201 // From sbyte to byte, ushort, uint, ulong, char
1203 if (target_type == TypeManager.byte_type)
1204 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1205 if (target_type == TypeManager.ushort_type)
1206 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1207 if (target_type == TypeManager.uint32_type)
1208 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1209 if (target_type == TypeManager.uint64_type)
1210 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1211 if (target_type == TypeManager.char_type)
1212 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1213 } else if (expr_type == TypeManager.byte_type){
1215 // From byte to sbyte and char
1217 if (target_type == TypeManager.sbyte_type)
1218 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1219 if (target_type == TypeManager.char_type)
1220 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1221 } else if (expr_type == TypeManager.short_type){
1223 // From short to sbyte, byte, ushort, uint, ulong, char
1225 if (target_type == TypeManager.sbyte_type)
1226 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1227 if (target_type == TypeManager.byte_type)
1228 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1229 if (target_type == TypeManager.ushort_type)
1230 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1231 if (target_type == TypeManager.uint32_type)
1232 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1233 if (target_type == TypeManager.uint64_type)
1234 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1235 if (target_type == TypeManager.char_type)
1236 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1237 } else if (expr_type == TypeManager.ushort_type){
1239 // From ushort to sbyte, byte, short, char
1241 if (target_type == TypeManager.sbyte_type)
1242 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1243 if (target_type == TypeManager.byte_type)
1244 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1245 if (target_type == TypeManager.short_type)
1246 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1247 if (target_type == TypeManager.char_type)
1248 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1249 } else if (expr_type == TypeManager.int32_type){
1251 // From int to sbyte, byte, short, ushort, uint, ulong, char
1253 if (target_type == TypeManager.sbyte_type)
1254 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1255 if (target_type == TypeManager.byte_type)
1256 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1257 if (target_type == TypeManager.short_type)
1258 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1259 if (target_type == TypeManager.ushort_type)
1260 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1261 if (target_type == TypeManager.uint32_type)
1262 return new EmptyCast (expr, target_type);
1263 if (target_type == TypeManager.uint64_type)
1264 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1265 if (target_type == TypeManager.char_type)
1266 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1267 } else if (expr_type == TypeManager.uint32_type){
1269 // From uint to sbyte, byte, short, ushort, int, char
1271 if (target_type == TypeManager.sbyte_type)
1272 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1273 if (target_type == TypeManager.byte_type)
1274 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1275 if (target_type == TypeManager.short_type)
1276 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1277 if (target_type == TypeManager.ushort_type)
1278 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1279 if (target_type == TypeManager.int32_type)
1280 return new EmptyCast (expr, target_type);
1281 if (target_type == TypeManager.char_type)
1282 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1283 } else if (expr_type == TypeManager.int64_type){
1285 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1287 if (target_type == TypeManager.sbyte_type)
1288 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1289 if (target_type == TypeManager.byte_type)
1290 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1291 if (target_type == TypeManager.short_type)
1292 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1293 if (target_type == TypeManager.ushort_type)
1294 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1295 if (target_type == TypeManager.int32_type)
1296 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1297 if (target_type == TypeManager.uint32_type)
1298 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1299 if (target_type == TypeManager.uint64_type)
1300 return new EmptyCast (expr, target_type);
1301 if (target_type == TypeManager.char_type)
1302 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1303 } else if (expr_type == TypeManager.uint64_type){
1305 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1307 if (target_type == TypeManager.sbyte_type)
1308 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1309 if (target_type == TypeManager.byte_type)
1310 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1311 if (target_type == TypeManager.short_type)
1312 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1313 if (target_type == TypeManager.ushort_type)
1314 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1315 if (target_type == TypeManager.int32_type)
1316 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1317 if (target_type == TypeManager.uint32_type)
1318 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1319 if (target_type == TypeManager.int64_type)
1320 return new EmptyCast (expr, target_type);
1321 if (target_type == TypeManager.char_type)
1322 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1323 } else if (expr_type == TypeManager.char_type){
1325 // From char to sbyte, byte, short
1327 if (target_type == TypeManager.sbyte_type)
1328 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1329 if (target_type == TypeManager.byte_type)
1330 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1331 if (target_type == TypeManager.short_type)
1332 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1333 } else if (expr_type == TypeManager.float_type){
1335 // From float to sbyte, byte, short,
1336 // ushort, int, uint, long, ulong, char
1339 if (target_type == TypeManager.sbyte_type)
1340 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1341 if (target_type == TypeManager.byte_type)
1342 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1343 if (target_type == TypeManager.short_type)
1344 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1345 if (target_type == TypeManager.ushort_type)
1346 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1347 if (target_type == TypeManager.int32_type)
1348 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1349 if (target_type == TypeManager.uint32_type)
1350 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1351 if (target_type == TypeManager.int64_type)
1352 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1353 if (target_type == TypeManager.uint64_type)
1354 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1355 if (target_type == TypeManager.char_type)
1356 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1357 if (target_type == TypeManager.decimal_type)
1358 return InternalTypeConstructor (ec, expr, target_type);
1359 } else if (expr_type == TypeManager.double_type){
1361 // From double to byte, byte, short,
1362 // ushort, int, uint, long, ulong,
1363 // char, float or decimal
1365 if (target_type == TypeManager.sbyte_type)
1366 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1367 if (target_type == TypeManager.byte_type)
1368 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1369 if (target_type == TypeManager.short_type)
1370 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1371 if (target_type == TypeManager.ushort_type)
1372 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1373 if (target_type == TypeManager.int32_type)
1374 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1375 if (target_type == TypeManager.uint32_type)
1376 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1377 if (target_type == TypeManager.int64_type)
1378 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1379 if (target_type == TypeManager.uint64_type)
1380 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1381 if (target_type == TypeManager.char_type)
1382 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1383 if (target_type == TypeManager.float_type)
1384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1385 if (target_type == TypeManager.decimal_type)
1386 return InternalTypeConstructor (ec, expr, target_type);
1389 // decimal is taken care of by the op_Explicit methods.
1395 /// Returns whether an explicit reference conversion can be performed
1396 /// from source_type to target_type
1398 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1400 bool target_is_value_type = target_type.IsValueType;
1402 if (source_type == target_type)
1406 // From object to any reference type
1408 if (source_type == TypeManager.object_type && !target_is_value_type)
1412 // From any class S to any class-type T, provided S is a base class of T
1414 if (target_type.IsSubclassOf (source_type))
1418 // From any interface type S to any interface T provided S is not derived from T
1420 if (source_type.IsInterface && target_type.IsInterface){
1421 if (!target_type.IsSubclassOf (source_type))
1426 // From any class type S to any interface T, provides S is not sealed
1427 // and provided S does not implement T.
1429 if (target_type.IsInterface && !source_type.IsSealed &&
1430 !target_type.IsAssignableFrom (source_type))
1434 // From any interface-type S to to any class type T, provided T is not
1435 // sealed, or provided T implements S.
1437 if (source_type.IsInterface &&
1438 (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1441 // From an array type S with an element type Se to an array type T with an
1442 // element type Te provided all the following are true:
1443 // * S and T differe only in element type, in other words, S and T
1444 // have the same number of dimensions.
1445 // * Both Se and Te are reference types
1446 // * An explicit referenc conversions exist from Se to Te
1448 if (source_type.IsArray && target_type.IsArray) {
1449 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1451 Type source_element_type = source_type.GetElementType ();
1452 Type target_element_type = target_type.GetElementType ();
1454 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1455 if (ExplicitReferenceConversionExists (source_element_type,
1456 target_element_type))
1462 // From System.Array to any array-type
1463 if (source_type == TypeManager.array_type &&
1464 target_type.IsSubclassOf (TypeManager.array_type)){
1469 // From System delegate to any delegate-type
1471 if (source_type == TypeManager.delegate_type &&
1472 target_type.IsSubclassOf (TypeManager.delegate_type))
1476 // From ICloneable to Array or Delegate types
1478 if (source_type == TypeManager.icloneable_type &&
1479 (target_type == TypeManager.array_type ||
1480 target_type == TypeManager.delegate_type))
1487 /// Implements Explicit Reference conversions
1489 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1491 Type source_type = source.Type;
1492 bool target_is_value_type = target_type.IsValueType;
1495 // From object to any reference type
1497 if (source_type == TypeManager.object_type && !target_is_value_type)
1498 return new ClassCast (source, target_type);
1502 // From any class S to any class-type T, provided S is a base class of T
1504 if (target_type.IsSubclassOf (source_type))
1505 return new ClassCast (source, target_type);
1508 // From any interface type S to any interface T provided S is not derived from T
1510 if (source_type.IsInterface && target_type.IsInterface){
1512 Type [] ifaces = source_type.GetInterfaces ();
1514 if (TypeManager.ImplementsInterface (source_type, target_type))
1517 return new ClassCast (source, target_type);
1521 // From any class type S to any interface T, provides S is not sealed
1522 // and provided S does not implement T.
1524 if (target_type.IsInterface && !source_type.IsSealed) {
1526 if (TypeManager.ImplementsInterface (source_type, target_type))
1529 return new ClassCast (source, target_type);
1534 // From any interface-type S to to any class type T, provided T is not
1535 // sealed, or provided T implements S.
1537 if (source_type.IsInterface) {
1539 if (target_type.IsSealed)
1542 if (TypeManager.ImplementsInterface (target_type, source_type))
1543 return new ClassCast (source, target_type);
1548 // From an array type S with an element type Se to an array type T with an
1549 // element type Te provided all the following are true:
1550 // * S and T differe only in element type, in other words, S and T
1551 // have the same number of dimensions.
1552 // * Both Se and Te are reference types
1553 // * An explicit referenc conversions exist from Se to Te
1555 if (source_type.IsArray && target_type.IsArray) {
1556 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1558 Type source_element_type = source_type.GetElementType ();
1559 Type target_element_type = target_type.GetElementType ();
1561 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1562 if (ExplicitReferenceConversionExists (source_element_type,
1563 target_element_type))
1564 return new ClassCast (source, target_type);
1569 // From System.Array to any array-type
1570 if (source_type == TypeManager.array_type &&
1571 target_type.IsSubclassOf (TypeManager.array_type)){
1572 return new ClassCast (source, target_type);
1576 // From System delegate to any delegate-type
1578 if (source_type == TypeManager.delegate_type &&
1579 target_type.IsSubclassOf (TypeManager.delegate_type))
1580 return new ClassCast (source, target_type);
1583 // From ICloneable to Array or Delegate types
1585 if (source_type == TypeManager.icloneable_type &&
1586 (target_type == TypeManager.array_type ||
1587 target_type == TypeManager.delegate_type))
1588 return new ClassCast (source, target_type);
1594 /// Performs an explicit conversion of the expression `expr' whose
1595 /// type is expr.Type to `target_type'.
1597 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1598 Type target_type, Location loc)
1600 Type expr_type = expr.Type;
1601 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1606 ne = ConvertNumericExplicit (ec, expr, target_type);
1611 // Unboxing conversion.
1613 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1614 return new UnboxCast (expr, target_type);
1619 if (expr is EnumLiteral) {
1620 Expression e = ((EnumLiteral) expr).Child;
1622 return ConvertImplicit (ec, e, target_type, loc);
1625 ne = ConvertReferenceExplicit (expr, target_type);
1629 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1633 error30 (loc, expr_type, target_type);
1638 /// Same as ConverExplicit, only it doesn't include user defined conversions
1640 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1641 Type target_type, Location l)
1643 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1648 ne = ConvertNumericExplicit (ec, expr, target_type);
1652 ne = ConvertReferenceExplicit (expr, target_type);
1656 error30 (l, expr.Type, target_type);
1660 static string ExprClassName (ExprClass c)
1663 case ExprClass.Invalid:
1665 case ExprClass.Value:
1667 case ExprClass.Variable:
1669 case ExprClass.Namespace:
1671 case ExprClass.Type:
1673 case ExprClass.MethodGroup:
1674 return "method group";
1675 case ExprClass.PropertyAccess:
1676 return "property access";
1677 case ExprClass.EventAccess:
1678 return "event access";
1679 case ExprClass.IndexerAccess:
1680 return "indexer access";
1681 case ExprClass.Nothing:
1684 throw new Exception ("Should not happen");
1688 /// Reports that we were expecting `expr' to be of class `expected'
1690 protected void report118 (Location loc, Expression expr, string expected)
1692 string kind = "Unknown";
1695 kind = ExprClassName (expr.ExprClass);
1697 Error (118, loc, "Expression denotes a `" + kind +
1698 "' where a `" + expected + "' was expected");
1702 /// This function tries to reduce the expression performing
1703 /// constant folding and common subexpression elimination
1705 static public Expression Reduce (EmitContext ec, Expression e)
1707 //Console.WriteLine ("Calling reduce");
1708 return e.Reduce (ec);
1711 static void error31 (Location l, string val, Type t)
1713 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1714 TypeManager.CSharpName (t));
1718 /// Converts the IntLiteral, UIntLiteral, LongLiteral or
1719 /// ULongLiteral into the integral target_type.
1721 /// This is used by the switch statement, so the domain
1722 /// of work is restricted to the literals above, and the
1723 /// targets are int32, uint32, char, byte, sbyte, ushort,
1724 /// short, uint64 and int64
1726 public static Literal ConvertIntLiteral (Literal l, Type target_type, Location loc)
1730 if (l.Type == target_type)
1734 // Make into one of the literals we handle, we dont really care
1735 // about this value as we will just return a few limited types
1737 if (l is EnumLiteral)
1738 l = ((EnumLiteral)l).WidenToCompilerLiteral ();
1740 if (l is IntLiteral){
1741 int v = ((IntLiteral) l).Value;
1743 if (target_type == TypeManager.uint32_type){
1745 return new UIntLiteral ((uint) v);
1746 } else if (target_type == TypeManager.char_type){
1747 if (v >= Char.MinValue && v <= Char.MaxValue)
1749 } else if (target_type == TypeManager.byte_type){
1750 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1752 } else if (target_type == TypeManager.sbyte_type){
1753 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1755 } else if (target_type == TypeManager.short_type){
1756 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1758 } else if (target_type == TypeManager.ushort_type){
1759 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1761 } else if (target_type == TypeManager.int64_type)
1762 return new LongLiteral (v);
1763 else if (target_type == TypeManager.uint64_type){
1765 return new ULongLiteral ((ulong) v);
1769 } else if (l is UIntLiteral){
1770 uint v = ((UIntLiteral) l).Value;
1772 if (target_type == TypeManager.int32_type){
1773 if (v <= Int32.MaxValue)
1774 return new IntLiteral ((int) v);
1775 } else if (target_type == TypeManager.char_type){
1776 if (v >= Char.MinValue && v <= Char.MaxValue)
1778 } else if (target_type == TypeManager.byte_type){
1779 if (v <= Byte.MaxValue)
1781 } else if (target_type == TypeManager.sbyte_type){
1782 if (v <= SByte.MaxValue)
1784 } else if (target_type == TypeManager.short_type){
1785 if (v <= UInt16.MaxValue)
1787 } else if (target_type == TypeManager.ushort_type){
1788 if (v <= UInt16.MaxValue)
1790 } else if (target_type == TypeManager.int64_type)
1791 return new LongLiteral (v);
1792 else if (target_type == TypeManager.uint64_type)
1793 return new ULongLiteral (v);
1795 } else if (l is LongLiteral){
1796 long v = ((LongLiteral) l).Value;
1798 if (target_type == TypeManager.int32_type){
1799 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1800 return new IntLiteral ((int) v);
1801 } else if (target_type == TypeManager.uint32_type){
1802 if (v >= 0 && v <= UInt32.MaxValue)
1803 return new UIntLiteral ((uint) v);
1804 } else if (target_type == TypeManager.char_type){
1805 if (v >= Char.MinValue && v <= Char.MaxValue)
1806 return new IntLiteral ((int) v);
1807 } else if (target_type == TypeManager.byte_type){
1808 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1809 return new IntLiteral ((int) v);
1810 } else if (target_type == TypeManager.sbyte_type){
1811 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1812 return new IntLiteral ((int) v);
1813 } else if (target_type == TypeManager.short_type){
1814 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1815 return new IntLiteral ((int) v);
1816 } else if (target_type == TypeManager.ushort_type){
1817 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1818 return new IntLiteral ((int) v);
1819 } else if (target_type == TypeManager.uint64_type){
1821 return new ULongLiteral ((ulong) v);
1824 } else if (l is ULongLiteral){
1825 ulong v = ((ULongLiteral) l).Value;
1827 if (target_type == TypeManager.int32_type){
1828 if (v <= Int32.MaxValue)
1829 return new IntLiteral ((int) v);
1830 } else if (target_type == TypeManager.uint32_type){
1831 if (v <= UInt32.MaxValue)
1832 return new UIntLiteral ((uint) v);
1833 } else if (target_type == TypeManager.char_type){
1834 if (v >= Char.MinValue && v <= Char.MaxValue)
1835 return new IntLiteral ((int) v);
1836 } else if (target_type == TypeManager.byte_type){
1837 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1838 return new IntLiteral ((int) v);
1839 } else if (target_type == TypeManager.sbyte_type){
1840 if (v <= (int) SByte.MaxValue)
1841 return new IntLiteral ((int) v);
1842 } else if (target_type == TypeManager.short_type){
1843 if (v <= UInt16.MaxValue)
1844 return new IntLiteral ((int) v);
1845 } else if (target_type == TypeManager.ushort_type){
1846 if (v <= UInt16.MaxValue)
1847 return new IntLiteral ((int) v);
1848 } else if (target_type == TypeManager.int64_type){
1849 if (v <= Int64.MaxValue)
1850 return new LongLiteral ((long) v);
1855 error31 (loc, s, target_type);
1862 /// This is just a base class for expressions that can
1863 /// appear on statements (invocations, object creation,
1864 /// assignments, post/pre increment and decrement). The idea
1865 /// being that they would support an extra Emition interface that
1866 /// does not leave a result on the stack.
1868 public abstract class ExpressionStatement : Expression {
1871 /// Requests the expression to be emitted in a `statement'
1872 /// context. This means that no new value is left on the
1873 /// stack after invoking this method (constrasted with
1874 /// Emit that will always leave a value on the stack).
1876 public abstract void EmitStatement (EmitContext ec);
1880 /// This kind of cast is used to encapsulate the child
1881 /// whose type is child.Type into an expression that is
1882 /// reported to return "return_type". This is used to encapsulate
1883 /// expressions which have compatible types, but need to be dealt
1884 /// at higher levels with.
1886 /// For example, a "byte" expression could be encapsulated in one
1887 /// of these as an "unsigned int". The type for the expression
1888 /// would be "unsigned int".
1891 public class EmptyCast : Expression {
1892 protected Expression child;
1894 public EmptyCast (Expression child, Type return_type)
1896 ExprClass = child.ExprClass;
1901 public override Expression DoResolve (EmitContext ec)
1903 // This should never be invoked, we are born in fully
1904 // initialized state.
1909 public override void Emit (EmitContext ec)
1917 /// This class is used to wrap literals which belong inside Enums
1919 public class EnumLiteral : Literal {
1920 public Expression Child;
1922 public EnumLiteral (Expression child, Type enum_type)
1924 ExprClass = child.ExprClass;
1929 public override Expression DoResolve (EmitContext ec)
1931 // This should never be invoked, we are born in fully
1932 // initialized state.
1937 public override void Emit (EmitContext ec)
1942 public override object GetValue ()
1944 return ((Literal) Child).GetValue ();
1948 // Converts from one of the valid underlying types for an enumeration
1949 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1950 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1952 public Literal WidenToCompilerLiteral ()
1954 Type t = Child.Type.UnderlyingSystemType;
1955 object v = ((Literal) Child).GetValue ();;
1957 if (t == TypeManager.int32_type)
1958 return new IntLiteral ((int) v);
1959 if (t == TypeManager.uint32_type)
1960 return new UIntLiteral ((uint) v);
1961 if (t == TypeManager.int64_type)
1962 return new LongLiteral ((long) v);
1963 if (t == TypeManager.uint64_type)
1964 return new ULongLiteral ((ulong) v);
1965 if (t == TypeManager.short_type)
1966 return new IntLiteral ((short) v);
1967 if (t == TypeManager.ushort_type)
1968 return new UIntLiteral ((ushort) v);
1969 if (t == TypeManager.byte_type)
1970 return new UIntLiteral ((byte) v);
1971 if (t == TypeManager.sbyte_type)
1972 return new IntLiteral ((sbyte) v);
1974 throw new Exception ("Invalid enumeration underlying type: " + t);
1977 public override string AsString ()
1979 return ((Literal) Child).AsString ();
1984 /// This kind of cast is used to encapsulate Value Types in objects.
1986 /// The effect of it is to box the value type emitted by the previous
1989 public class BoxedCast : EmptyCast {
1991 public BoxedCast (Expression expr)
1992 : base (expr, TypeManager.object_type)
1996 public override Expression DoResolve (EmitContext ec)
1998 // This should never be invoked, we are born in fully
1999 // initialized state.
2004 public override void Emit (EmitContext ec)
2007 ec.ig.Emit (OpCodes.Box, child.Type);
2011 public class UnboxCast : EmptyCast {
2012 public UnboxCast (Expression expr, Type return_type)
2013 : base (expr, return_type)
2017 public override Expression DoResolve (EmitContext ec)
2019 // This should never be invoked, we are born in fully
2020 // initialized state.
2025 public override void Emit (EmitContext ec)
2028 ILGenerator ig = ec.ig;
2031 ig.Emit (OpCodes.Unbox, t);
2034 // Load the object from the pointer
2036 if (t == TypeManager.int32_type)
2037 ig.Emit (OpCodes.Ldind_I4);
2038 else if (t == TypeManager.uint32_type)
2039 ig.Emit (OpCodes.Ldind_U4);
2040 else if (t == TypeManager.short_type)
2041 ig.Emit (OpCodes.Ldind_I2);
2042 else if (t == TypeManager.ushort_type)
2043 ig.Emit (OpCodes.Ldind_U2);
2044 else if (t == TypeManager.char_type)
2045 ig.Emit (OpCodes.Ldind_U2);
2046 else if (t == TypeManager.byte_type)
2047 ig.Emit (OpCodes.Ldind_U1);
2048 else if (t == TypeManager.sbyte_type)
2049 ig.Emit (OpCodes.Ldind_I1);
2050 else if (t == TypeManager.uint64_type)
2051 ig.Emit (OpCodes.Ldind_I8);
2052 else if (t == TypeManager.int64_type)
2053 ig.Emit (OpCodes.Ldind_I8);
2054 else if (t == TypeManager.float_type)
2055 ig.Emit (OpCodes.Ldind_R4);
2056 else if (t == TypeManager.double_type)
2057 ig.Emit (OpCodes.Ldind_R8);
2058 else if (t == TypeManager.bool_type)
2059 ig.Emit (OpCodes.Ldind_I1);
2060 else if (t == TypeManager.intptr_type)
2061 ig.Emit (OpCodes.Ldind_I);
2063 ig.Emit (OpCodes.Ldobj, t);
2068 /// This kind of cast is used to encapsulate a child expression
2069 /// that can be trivially converted to a target type using one or
2070 /// two opcodes. The opcodes are passed as arguments.
2072 public class OpcodeCast : EmptyCast {
2076 public OpcodeCast (Expression child, Type return_type, OpCode op)
2077 : base (child, return_type)
2081 second_valid = false;
2084 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2085 : base (child, return_type)
2090 second_valid = true;
2093 public override Expression DoResolve (EmitContext ec)
2095 // This should never be invoked, we are born in fully
2096 // initialized state.
2101 public override void Emit (EmitContext ec)
2113 /// This kind of cast is used to encapsulate a child and cast it
2114 /// to the class requested
2116 public class ClassCast : EmptyCast {
2117 public ClassCast (Expression child, Type return_type)
2118 : base (child, return_type)
2123 public override Expression DoResolve (EmitContext ec)
2125 // This should never be invoked, we are born in fully
2126 // initialized state.
2131 public override void Emit (EmitContext ec)
2135 ec.ig.Emit (OpCodes.Castclass, type);
2141 /// SimpleName expressions are initially formed of a single
2142 /// word and it only happens at the beginning of the expression.
2146 /// The expression will try to be bound to a Field, a Method
2147 /// group or a Property. If those fail we pass the name to our
2148 /// caller and the SimpleName is compounded to perform a type
2149 /// lookup. The idea behind this process is that we want to avoid
2150 /// creating a namespace map from the assemblies, as that requires
2151 /// the GetExportedTypes function to be called and a hashtable to
2152 /// be constructed which reduces startup time. If later we find
2153 /// that this is slower, we should create a `NamespaceExpr' expression
2154 /// that fully participates in the resolution process.
2156 /// For example `System.Console.WriteLine' is decomposed into
2157 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2159 /// The first SimpleName wont produce a match on its own, so it will
2161 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
2163 /// System.Console will produce a TypeExpr match.
2165 /// The downside of this is that we might be hitting `LookupType' too many
2166 /// times with this scheme.
2168 public class SimpleName : Expression {
2169 public readonly string Name;
2170 public readonly Location Location;
2172 public SimpleName (string name, Location l)
2178 public static void Error120 (Location l, string name)
2182 "An object reference is required " +
2183 "for the non-static field `"+name+"'");
2187 // Checks whether we are trying to access an instance
2188 // property, method or field from a static body.
2190 Expression MemberStaticCheck (Expression e)
2192 if (e is FieldExpr){
2193 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2196 Error120 (Location, Name);
2199 } else if (e is MethodGroupExpr){
2200 MethodGroupExpr mg = (MethodGroupExpr) e;
2202 if (!mg.RemoveInstanceMethods ()){
2203 Error120 (Location, mg.Methods [0].Name);
2207 } else if (e is PropertyExpr){
2208 if (!((PropertyExpr) e).IsStatic){
2209 Error120 (Location, Name);
2218 // 7.5.2: Simple Names.
2220 // Local Variables and Parameters are handled at
2221 // parse time, so they never occur as SimpleNames.
2223 public override Expression DoResolve (EmitContext ec)
2228 // Stage 1: Performed by the parser (binding to locals or parameters).
2232 // Stage 2: Lookup members
2234 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2237 // Stage 3: Lookup symbol in the various namespaces.
2241 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
2242 return new TypeExpr (t);
2245 // Stage 3 part b: Lookup up if we are an alias to a type
2248 // Since we are cheating: we only do the Alias lookup for
2249 // namespaces if the name does not include any dots in it
2252 // IMPLEMENT ME. Read mcs/mcs/TODO for ideas, or rewrite
2253 // using NamespaceExprs (dunno how that fixes the alias
2254 // per-file though).
2256 // No match, maybe our parent can compose us
2257 // into something meaningful.
2262 // Step 2, continues here.
2266 if (e is FieldExpr){
2267 FieldExpr fe = (FieldExpr) e;
2269 if (!fe.FieldInfo.IsStatic){
2270 This t = new This (Location.Null);
2272 fe.InstanceExpression = t.DoResolve (ec);
2275 FieldInfo fi = fe.FieldInfo;
2277 if (fi is FieldBuilder) {
2278 Constant c = TypeManager.LookupConstant ((FieldBuilder) fi);
2281 object o = c.LookupConstantValue (ec);
2282 Expression l = Literalize (o, fi.FieldType);
2284 return ((Literal) l);
2290 return MemberStaticCheck (e);
2295 public override void Emit (EmitContext ec)
2298 // If this is ever reached, then we failed to
2299 // find the name as a namespace
2302 Error (103, Location, "The name `" + Name +
2303 "' does not exist in the class `" +
2304 ec.TypeContainer.Name + "'");
2309 /// Fully resolved expression that evaluates to a type
2311 public class TypeExpr : Expression {
2312 public TypeExpr (Type t)
2315 eclass = ExprClass.Type;
2318 override public Expression DoResolve (EmitContext ec)
2323 override public void Emit (EmitContext ec)
2325 throw new Exception ("Implement me");
2330 /// MethodGroup Expression.
2332 /// This is a fully resolved expression that evaluates to a type
2334 public class MethodGroupExpr : Expression {
2335 public MethodBase [] Methods;
2336 Expression instance_expression = null;
2338 public MethodGroupExpr (MemberInfo [] mi)
2340 Methods = new MethodBase [mi.Length];
2341 mi.CopyTo (Methods, 0);
2342 eclass = ExprClass.MethodGroup;
2345 public MethodGroupExpr (ArrayList l)
2347 Methods = new MethodBase [l.Count];
2349 l.CopyTo (Methods, 0);
2350 eclass = ExprClass.MethodGroup;
2354 // `A method group may have associated an instance expression'
2356 public Expression InstanceExpression {
2358 return instance_expression;
2362 instance_expression = value;
2366 override public Expression DoResolve (EmitContext ec)
2371 override public void Emit (EmitContext ec)
2373 throw new Exception ("This should never be reached");
2376 bool RemoveMethods (bool keep_static)
2378 ArrayList smethods = new ArrayList ();
2379 int top = Methods.Length;
2382 for (i = 0; i < top; i++){
2383 MethodBase mb = Methods [i];
2385 if (mb.IsStatic == keep_static)
2389 if (smethods.Count == 0)
2392 Methods = new MethodBase [smethods.Count];
2393 smethods.CopyTo (Methods, 0);
2399 /// Removes any instance methods from the MethodGroup, returns
2400 /// false if the resulting set is empty.
2402 public bool RemoveInstanceMethods ()
2404 return RemoveMethods (true);
2408 /// Removes any static methods from the MethodGroup, returns
2409 /// false if the resulting set is empty.
2411 public bool RemoveStaticMethods ()
2413 return RemoveMethods (false);
2418 /// Fully resolved expression that evaluates to a Field
2420 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2421 public readonly FieldInfo FieldInfo;
2422 public Expression InstanceExpression;
2425 public FieldExpr (FieldInfo fi, Location l)
2428 eclass = ExprClass.Variable;
2429 type = fi.FieldType;
2433 override public Expression DoResolve (EmitContext ec)
2435 if (!FieldInfo.IsStatic){
2436 if (InstanceExpression == null){
2437 throw new Exception ("non-static FieldExpr without instance var\n" +
2438 "You have to assign the Instance variable\n" +
2439 "Of the FieldExpr to set this\n");
2442 InstanceExpression = InstanceExpression.Resolve (ec);
2443 if (InstanceExpression == null)
2451 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2453 Expression e = DoResolve (ec);
2458 if (!FieldInfo.IsInitOnly)
2462 // InitOnly fields can only be assigned in constructors
2465 if (ec.IsConstructor)
2468 Report.Error (191, loc,
2469 "Readonly field can not be assigned outside " +
2470 "of constructor or variable initializer");
2475 override public void Emit (EmitContext ec)
2477 ILGenerator ig = ec.ig;
2479 if (FieldInfo.IsStatic)
2480 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2482 InstanceExpression.Emit (ec);
2484 ig.Emit (OpCodes.Ldfld, FieldInfo);
2488 public void EmitAssign (EmitContext ec, Expression source)
2490 bool is_static = FieldInfo.IsStatic;
2493 Expression instance = InstanceExpression;
2495 if (instance.Type.IsValueType){
2496 if (instance is IMemoryLocation){
2497 IMemoryLocation ml = (IMemoryLocation) instance;
2501 throw new Exception ("The " + instance + " of type " + Type+
2502 "represents a ValueType and does not " +
2503 "implement IMemoryLocation");
2510 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2512 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2516 public void AddressOf (EmitContext ec)
2518 if (FieldInfo.IsStatic)
2519 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2521 InstanceExpression.Emit (ec);
2522 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2528 /// Expression that evaluates to a Property. The Assign class
2529 /// might set the `Value' expression if we are in an assignment.
2531 /// This is not an LValue because we need to re-write the expression, we
2532 /// can not take data from the stack and store it.
2534 public class PropertyExpr : ExpressionStatement, IAssignMethod {
2535 public readonly PropertyInfo PropertyInfo;
2536 public readonly bool IsStatic;
2537 MethodInfo [] Accessors;
2540 Expression instance_expr;
2542 public PropertyExpr (PropertyInfo pi, Location l)
2545 eclass = ExprClass.PropertyAccess;
2548 Accessors = TypeManager.GetAccessors (pi);
2550 if (Accessors != null)
2551 for (int i = 0; i < Accessors.Length; i++){
2552 if (Accessors [i] != null)
2553 if (Accessors [i].IsStatic)
2557 Accessors = new MethodInfo [2];
2559 type = pi.PropertyType;
2563 // The instance expression associated with this expression
2565 public Expression InstanceExpression {
2567 instance_expr = value;
2571 return instance_expr;
2575 public bool VerifyAssignable ()
2577 if (!PropertyInfo.CanWrite){
2578 Report.Error (200, loc,
2579 "The property `" + PropertyInfo.Name +
2580 "' can not be assigned to, as it has not set accessor");
2587 override public Expression DoResolve (EmitContext ec)
2589 if (!PropertyInfo.CanRead){
2590 Report.Error (154, loc,
2591 "The property `" + PropertyInfo.Name +
2592 "' can not be used in " +
2593 "this context because it lacks a get accessor");
2600 override public void Emit (EmitContext ec)
2602 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2607 // Implements the IAssignMethod interface for assignments
2609 public void EmitAssign (EmitContext ec, Expression source)
2611 Argument arg = new Argument (source, Argument.AType.Expression);
2612 ArrayList args = new ArrayList ();
2615 Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2618 override public void EmitStatement (EmitContext ec)
2621 ec.ig.Emit (OpCodes.Pop);
2626 /// Fully resolved expression that evaluates to a Expression
2628 public class EventExpr : Expression {
2629 public readonly EventInfo EventInfo;
2632 public EventExpr (EventInfo ei, Location loc)
2636 eclass = ExprClass.EventAccess;
2639 override public Expression DoResolve (EmitContext ec)
2641 // We are born in resolved state.
2645 override public void Emit (EmitContext ec)
2647 throw new Exception ("Implement me");
2648 // FIXME: Implement.