forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilationMode.cs
More file actions
233 lines (182 loc) · 9.82 KB
/
CompilationMode.cs
File metadata and controls
233 lines (182 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using MSAst = System.Linq.Expressions;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Runtime.CompilerServices;
using Microsoft.Scripting;
using Microsoft.Scripting.Ast;
using Microsoft.Scripting.Utils;
using IronPython.Compiler.Ast;
using IronPython.Runtime;
using System.Collections;
using System.Reflection;
namespace IronPython.Compiler {
public delegate object LookupCompilationDelegate(CodeContext context, FunctionCode code);
/// <summary>
/// Specifies the compilation mode which will be used during the AST transformation
/// </summary>
[Serializable]
internal abstract class CompilationMode {
#if FEATURE_REFEMIT
/// <summary>
/// Compilation will proceed in a manner in which the resulting AST can be serialized to disk.
/// </summary>
public static readonly CompilationMode ToDisk = new ToDiskCompilationMode();
/// <summary>
/// Compilation will use a type and declare static fields for globals. The resulting type
/// is uncollectible and therefore extended use of this will cause memory leaks.
/// </summary>
public static readonly CompilationMode Uncollectable = new UncollectableCompilationMode();
#endif
/// <summary>
/// Compilation will use an array for globals. The resulting code will be fully collectible
/// and once all references are released will be collected.
/// </summary>
public static readonly CompilationMode Collectable = new CollectableCompilationMode();
/// <summary>
/// Compilation will force all global accesses to do a full lookup. This will also happen for
/// any unbound local references. This is the slowest form of code generation and is only
/// used for exec/eval code where we can run against an arbitrary dictionary.
/// </summary>
public static readonly CompilationMode Lookup = new LookupCompilationMode();
public virtual ScriptCode MakeScriptCode(PythonAst ast) {
return new RuntimeScriptCode(ast, ast.ModuleContext.GlobalContext);
}
public virtual MSAst.Expression GetConstant(object value) {
return MSAst.Expression.Constant(value);
}
public virtual Type GetConstantType(object value) {
if (value == null) {
return typeof(object);
}
return value.GetType();
}
public virtual void PrepareScope(PythonAst ast, ReadOnlyCollectionBuilder<MSAst.ParameterExpression> locals, List<MSAst.Expression> init) {
}
public virtual Type DelegateType {
get {
return typeof(MSAst.Expression<LookupCompilationDelegate>);
}
}
public virtual ConstantInfo GetContext() {
return null;
}
public virtual void PublishContext(CodeContext codeContext, ConstantInfo _contextInfo) {
}
public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0) {
if (retType == typeof(object)) {
return new PythonDynamicExpression1(binder, this, arg0);
} else if (retType == typeof(bool)) {
return new PythonDynamicExpression1<bool>(binder, this, arg0);
}
return ReduceDynamic(binder, retType, arg0);
}
public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1) {
if (retType == typeof(object)) {
return new PythonDynamicExpression2(binder, this, arg0, arg1);
} else if (retType == typeof(bool)) {
return new PythonDynamicExpression2<bool>(binder, this, arg0, arg1);
}
return ReduceDynamic(binder, retType, arg0, arg1);
}
public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2) {
if (retType == typeof(object)) {
return new PythonDynamicExpression3(binder, this, arg0, arg1, arg2);
}
return ReduceDynamic(binder, retType, arg0, arg1, arg2);
}
public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2, MSAst.Expression/*!*/ arg3) {
if (retType == typeof(object)) {
return new PythonDynamicExpression4(binder, this, arg0, arg1, arg2, arg3);
}
return ReduceDynamic(binder, retType, arg0, arg1, arg2, arg3);
}
public MSAst.Expression/*!*/ Dynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/[]/*!*/ args) {
Assert.NotNull(binder, retType, args);
Assert.NotNullItems(args);
switch (args.Length) {
case 1: return Dynamic(binder, retType, args[0]);
case 2: return Dynamic(binder, retType, args[0], args[1]);
case 3: return Dynamic(binder, retType, args[0], args[1], args[2]);
case 4: return Dynamic(binder, retType, args[0], args[1], args[2], args[3]);
}
if (retType == typeof(object)) {
return new PythonDynamicExpressionN(binder, this, args);
}
return ReduceDynamic(binder, retType, args);
}
public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0) {
return MSAst.DynamicExpression.Dynamic(binder, retType, arg0);
}
public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1) {
return MSAst.DynamicExpression.Dynamic(binder, retType, arg0, arg1);
}
public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2) {
return MSAst.DynamicExpression.Dynamic(binder, retType, arg0, arg1, arg2);
}
public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/ arg0, MSAst.Expression/*!*/ arg1, MSAst.Expression/*!*/ arg2, MSAst.Expression/*!*/ arg3) {
return MSAst.DynamicExpression.Dynamic(binder, retType, arg0, arg1, arg2, arg3);
}
public virtual MSAst.Expression/*!*/ ReduceDynamic(DynamicMetaObjectBinder/*!*/ binder, Type/*!*/ retType, MSAst.Expression/*!*/[]/*!*/ args) {
Assert.NotNull(binder, retType, args);
Assert.NotNullItems(args);
return MSAst.DynamicExpression.Dynamic(binder, retType, args);
}
public abstract MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global);
public abstract LightLambdaExpression ReduceAst(PythonAst instance, string name);
#region ConstantInfo
public class ConstantInfo {
public readonly MSAst.Expression/*!*/ Expression;
public readonly FieldInfo Field;
public readonly int Offset;
public ConstantInfo(MSAst.Expression/*!*/ expr, FieldInfo field, int offset) {
Assert.NotNull(expr);
Expression = expr;
Field = field;
Offset = offset;
}
}
public abstract class SiteInfo : ConstantInfo {
public readonly DynamicMetaObjectBinder/*!*/ Binder;
public readonly Type/*!*/ DelegateType;
protected Type/*!*/ _siteType;
public Type/*!*/ SiteType {
get {
if (_siteType != null) {
_siteType = typeof(CallSite<>).MakeGenericType(DelegateType);
}
return _siteType;
}
}
public SiteInfo(DynamicMetaObjectBinder/*!*/ binder, MSAst.Expression/*!*/ expr, FieldInfo/*!*/ field, int index, Type/*!*/ delegateType)
: base(expr, field, index) {
Assert.NotNull(binder);
Binder = binder;
DelegateType = delegateType;
}
public SiteInfo(DynamicMetaObjectBinder/*!*/ binder, MSAst.Expression/*!*/ expr, FieldInfo/*!*/ field, int index, Type/*!*/ delegateType, Type/*!*/ siteType)
: this(binder, expr, field, index, delegateType) {
_siteType = siteType;
}
public abstract CallSite/*!*/ MakeSite();
}
public class SiteInfoLarge : SiteInfo {
public SiteInfoLarge(DynamicMetaObjectBinder/*!*/ binder, MSAst.Expression/*!*/ expr, FieldInfo/*!*/ field, int index, Type/*!*/ delegateType)
: base(binder, expr, field, index, delegateType) { }
public override CallSite MakeSite() {
return CallSite.Create(DelegateType, Binder);
}
}
public class SiteInfo<T> : SiteInfo where T : class {
public SiteInfo(DynamicMetaObjectBinder/*!*/ binder, MSAst.Expression/*!*/ expr, FieldInfo/*!*/ field, int index)
: base(binder, expr, field, index, typeof(T), typeof(CallSite<T>)) { }
public override CallSite MakeSite() {
return CallSite<T>.Create(Binder);
}
}
#endregion
}
}