/DLR_Main/Languages/IronPython/IronPython/Compiler/CompilationMode.cs

https://bitbucket.org/mdavid/dlr · C# · 176 lines · 110 code · 33 blank · 33 comment · 19 complexity · 6f77c35700fe52c39aa6a275e491f1c0 MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. 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 Apache License, Version 2.0, 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 Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !CLR2
  16. using MSAst = System.Linq.Expressions;
  17. #else
  18. using MSAst = Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Dynamic;
  23. using System.Runtime.CompilerServices;
  24. using Microsoft.Scripting;
  25. using Microsoft.Scripting.Ast;
  26. using Microsoft.Scripting.Utils;
  27. using IronPython.Compiler.Ast;
  28. using IronPython.Runtime;
  29. using System.Collections;
  30. namespace IronPython.Compiler {
  31. public delegate object LookupCompilationDelegate(CodeContext context, FunctionCode code);
  32. /// <summary>
  33. /// Specifies the compilation mode which will be used during the AST transformation
  34. /// </summary>
  35. [Serializable]
  36. internal abstract class CompilationMode {
  37. /// <summary>
  38. /// Compilation will proceed in a manner in which the resulting AST can be serialized to disk.
  39. /// </summary>
  40. public static readonly CompilationMode ToDisk = new ToDiskCompilationMode();
  41. /// <summary>
  42. /// Compilation will use a type and declare static fields for globals. The resulting type
  43. /// is uncollectible and therefore extended use of this will cause memory leaks.
  44. /// </summary>
  45. public static readonly CompilationMode Uncollectable = new UncollectableCompilationMode();
  46. /// <summary>
  47. /// Compilation will use an array for globals. The resulting code will be fully collectible
  48. /// and once all references are released will be collected.
  49. /// </summary>
  50. public static readonly CompilationMode Collectable = new CollectableCompilationMode();
  51. /// <summary>
  52. /// Compilation will force all global accesses to do a full lookup. This will also happen for
  53. /// any unbound local references. This is the slowest form of code generation and is only
  54. /// used for exec/eval code where we can run against an arbitrary dictionary.
  55. /// </summary>
  56. public static readonly CompilationMode Lookup = new LookupCompilationMode();
  57. public virtual ScriptCode MakeScriptCode(PythonAst ast) {
  58. return new RuntimeScriptCode(ast, ast.ModuleContext.GlobalContext);
  59. }
  60. public virtual MSAst.Expression GetConstant(object value) {
  61. return MSAst.Expression.Constant(value);
  62. }
  63. public virtual Type GetConstantType(object value) {
  64. if (value == null) {
  65. return typeof(object);
  66. }
  67. return value.GetType();
  68. }
  69. public virtual void PrepareScope(PythonAst ast, ReadOnlyCollectionBuilder<MSAst.ParameterExpression> locals, List<MSAst.Expression> init) {
  70. }
  71. public virtual Type DelegateType {
  72. get {
  73. return typeof(MSAst.Expression<LookupCompilationDelegate>);
  74. }
  75. }
  76. public virtual UncollectableCompilationMode.ConstantInfo GetContext() {
  77. return null;
  78. }
  79. public virtual void PublishContext(CodeContext codeContext, UncollectableCompilationMode.ConstantInfo _contextInfo) {
  80. }
  81. public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0) {
  82. if (retType == typeof(object)) {
  83. return new PythonDynamicExpression1(binder, this, arg0);
  84. } else if (retType == typeof(bool)) {
  85. return new PythonDynamicExpression1<bool>(binder, this, arg0);
  86. }
  87. return ReduceDynamic(binder, retType, arg0);
  88. }
  89. public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1) {
  90. if (retType == typeof(object)) {
  91. return new PythonDynamicExpression2(binder, this, arg0, arg1);
  92. } else if (retType == typeof(bool)) {
  93. return new PythonDynamicExpression2<bool>(binder, this, arg0, arg1);
  94. }
  95. return ReduceDynamic(binder, retType, arg0, arg1);
  96. }
  97. public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2) {
  98. if (retType == typeof(object)) {
  99. return new PythonDynamicExpression3(binder, this, arg0, arg1, arg2);
  100. }
  101. return ReduceDynamic(binder, retType, arg0, arg1, arg2);
  102. }
  103. public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2, MSAst.Expression/*!*/ arg3) {
  104. if (retType == typeof(object)) {
  105. return new PythonDynamicExpression4(binder, this, arg0, arg1, arg2, arg3);
  106. }
  107. return ReduceDynamic(binder, retType, arg0, arg1, arg2, arg3);
  108. }
  109. public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/[]/*!*/ args) {
  110. Assert.NotNull(binder, retType, args);
  111. Assert.NotNullItems(args);
  112. switch (args.Length) {
  113. case 1: return Dynamic(binder, retType, args[0]);
  114. case 2: return Dynamic(binder, retType, args[0], args[1]);
  115. case 3: return Dynamic(binder, retType, args[0], args[1], args[2]);
  116. case 4: return Dynamic(binder, retType, args[0], args[1], args[2], args[3]);
  117. }
  118. if (retType == typeof(object)) {
  119. return new PythonDynamicExpressionN(binder, this, args);
  120. }
  121. return ReduceDynamic(binder, retType, args);
  122. }
  123. public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0) {
  124. return MSAst.Expression.Dynamic(binder, retType, arg0);
  125. }
  126. public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1) {
  127. return MSAst.Expression.Dynamic(binder, retType, arg0, arg1);
  128. }
  129. public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2) {
  130. return MSAst.Expression.Dynamic(binder, retType, arg0, arg1, arg2);
  131. }
  132. public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2, MSAst.Expression/*!*/ arg3) {
  133. return MSAst.Expression.Dynamic(binder, retType, arg0, arg1, arg2, arg3);
  134. }
  135. public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/[]/*!*/ args) {
  136. Assert.NotNull(binder, retType, args);
  137. Assert.NotNullItems(args);
  138. return MSAst.Expression.Dynamic(binder, retType, args);
  139. }
  140. public abstract MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global);
  141. public abstract LightLambdaExpression ReduceAst(PythonAst instance, string name);
  142. }
  143. }