PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting.Core/Ast/ConstantExpression.cs

#
C# | 167 lines | 125 code | 21 blank | 21 comment | 29 complexity | c1d2d224920090aaa6b612a028ee6d42 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using Microsoft.Scripting.Utils;
  17. using System.Text;
  18. using Microsoft.Scripting;
  19. namespace Microsoft.Linq.Expressions {
  20. //CONFORMING
  21. public sealed class ConstantExpression : Expression {
  22. internal static readonly ConstantExpression TrueLiteral = new ConstantExpression(null, true, typeof(bool));
  23. internal static readonly ConstantExpression FalseLiteral = new ConstantExpression(null, false, typeof(bool));
  24. internal static readonly ConstantExpression ZeroLiteral = new ConstantExpression(null, 0, typeof(int));
  25. internal static readonly ConstantExpression NullLiteral = new ConstantExpression(null, null, typeof(object));
  26. internal static readonly ConstantExpression EmptyStringLiteral = new ConstantExpression(null, String.Empty, typeof(string));
  27. internal static readonly ConstantExpression[] IntCache = new ConstantExpression[100];
  28. private readonly object _value;
  29. internal ConstantExpression(Annotations annotations, object value, Type type)
  30. : base(ExpressionType.Constant, type, annotations) {
  31. _value = value;
  32. }
  33. public object Value {
  34. get { return _value; }
  35. }
  36. internal override void BuildString(StringBuilder builder) {
  37. ContractUtils.RequiresNotNull(builder, "builder");
  38. if (_value != null) {
  39. if (_value is string) {
  40. builder.Append("\"");
  41. builder.Append(_value);
  42. builder.Append("\"");
  43. } else if (_value.ToString() == _value.GetType().ToString()) {
  44. builder.Append("value(");
  45. builder.Append(_value);
  46. builder.Append(")");
  47. } else {
  48. builder.Append(_value);
  49. }
  50. } else {
  51. builder.Append("null");
  52. }
  53. }
  54. internal override Expression Accept(ExpressionTreeVisitor visitor) {
  55. return visitor.VisitConstant(this);
  56. }
  57. }
  58. public partial class Expression {
  59. public static ConstantExpression True() {
  60. return ConstantExpression.TrueLiteral;
  61. }
  62. public static ConstantExpression False() {
  63. return ConstantExpression.FalseLiteral;
  64. }
  65. public static ConstantExpression Zero() {
  66. return ConstantExpression.ZeroLiteral;
  67. }
  68. public static ConstantExpression Null() {
  69. return ConstantExpression.NullLiteral;
  70. }
  71. public static ConstantExpression Null(Type type) {
  72. ContractUtils.RequiresNotNull(type, "type");
  73. if (type.IsValueType && !TypeUtils.IsNullableType(type)) {
  74. throw Error.ArgumentTypesMustMatch();
  75. }
  76. return new ConstantExpression(Annotations.Empty, null, type);
  77. }
  78. //CONFORMING
  79. public static ConstantExpression Constant(object value) {
  80. if (value == null) {
  81. return Null();
  82. }
  83. Type t = value.GetType();
  84. if (!t.IsEnum) {
  85. switch (Type.GetTypeCode(t)) {
  86. case TypeCode.Boolean:
  87. if ((bool)value == true) {
  88. return True();
  89. }
  90. return False();
  91. case TypeCode.Int32:
  92. int x = (int)value;
  93. int cacheIndex = x + 2;
  94. if (cacheIndex >= 0 && cacheIndex < ConstantExpression.IntCache.Length) {
  95. ConstantExpression res;
  96. if ((res = ConstantExpression.IntCache[cacheIndex]) == null) {
  97. ConstantExpression.IntCache[cacheIndex] = res = new ConstantExpression(null, x, typeof(int));
  98. }
  99. return res;
  100. }
  101. break;
  102. case TypeCode.String:
  103. if (String.IsNullOrEmpty((string)value)) {
  104. return ConstantExpression.EmptyStringLiteral;
  105. }
  106. break;
  107. }
  108. }
  109. return new ConstantExpression(Annotations.Empty, value, value == null ? typeof(object) : value.GetType());
  110. }
  111. public static ConstantExpression Constant(object value, Type type) {
  112. return Constant(value, type, Annotations.Empty);
  113. }
  114. //CONFORMING
  115. public static ConstantExpression Constant(object value, Type type, Annotations annotations) {
  116. ContractUtils.RequiresNotNull(type, "type");
  117. if (value == null && type.IsValueType && !TypeUtils.IsNullableType(type)) {
  118. throw Error.ArgumentTypesMustMatch();
  119. }
  120. if (value != null && !TypeUtils.AreAssignable(type, value.GetType())) {
  121. throw Error.ArgumentTypesMustMatch();
  122. }
  123. return new ConstantExpression(annotations, value, type);
  124. }
  125. [Obsolete("use Expression.Constant instead")]
  126. public static ConstantExpression RuntimeConstant(object value) {
  127. return Constant(value);
  128. }
  129. [Obsolete("use Expression.Constant instead")]
  130. public static ConstantExpression RuntimeConstant(object value, Type type) {
  131. return Constant(value, type);
  132. }
  133. /// <summary>
  134. /// Wraps the given value in a WeakReference and returns a tree that will retrieve
  135. /// the value from the WeakReference.
  136. /// </summary>
  137. [Obsolete("use Utils.WeakConstant in Microsoft.Scripting instead")]
  138. public static MemberExpression WeakConstant(object value) {
  139. System.Diagnostics.Debug.Assert(!(value is Expression));
  140. return Expression.Property(
  141. Expression.Constant(new WeakReference(value)),
  142. typeof(WeakReference).GetProperty("Target")
  143. );
  144. }
  145. }
  146. }