-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathStaticContextVisitor.java
More file actions
422 lines (391 loc) · 17.3 KB
/
StaticContextVisitor.java
File metadata and controls
422 lines (391 loc) · 17.3 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authors: Stefan Irimescu, Can Berker Cikis
*
*/
package org.rumbledb.compiler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.rumbledb.context.BuiltinFunctionCatalogue;
import org.rumbledb.context.FunctionIdentifier;
import org.rumbledb.context.Name;
import org.rumbledb.context.StaticContext;
import org.rumbledb.exceptions.OurBadException;
import org.rumbledb.exceptions.UndeclaredVariableException;
import org.rumbledb.exceptions.VariableAlreadyExistsException;
import org.rumbledb.expressions.AbstractNodeVisitor;
import org.rumbledb.expressions.ExecutionMode;
import org.rumbledb.expressions.Expression;
import org.rumbledb.expressions.Node;
import org.rumbledb.expressions.control.TypeSwitchExpression;
import org.rumbledb.expressions.control.TypeswitchCase;
import org.rumbledb.expressions.flowr.Clause;
import org.rumbledb.expressions.flowr.CountClause;
import org.rumbledb.expressions.flowr.FlworExpression;
import org.rumbledb.expressions.flowr.GroupByVariableDeclaration;
import org.rumbledb.expressions.flowr.ForClause;
import org.rumbledb.expressions.flowr.GroupByClause;
import org.rumbledb.expressions.flowr.LetClause;
import org.rumbledb.expressions.module.FunctionDeclaration;
import org.rumbledb.expressions.module.LibraryModule;
import org.rumbledb.expressions.module.MainModule;
import org.rumbledb.expressions.module.VariableDeclaration;
import org.rumbledb.expressions.primary.FunctionCallExpression;
import org.rumbledb.expressions.primary.InlineFunctionExpression;
import org.rumbledb.expressions.primary.VariableReferenceExpression;
import org.rumbledb.types.BuiltinTypesCatalogue;
import org.rumbledb.types.SequenceType;
import org.rumbledb.types.SequenceType.Arity;
/**
* Static context visitor implements a multi-pass algorithm that enables function hoisting
*/
public class StaticContextVisitor extends AbstractNodeVisitor<StaticContext> {
private VisitorConfig visitorConfig;
private Map<String, StaticContext> importedModuleContexts;
StaticContextVisitor() {
this.visitorConfig = VisitorConfig.staticContextVisitorInitialPassConfig;
this.importedModuleContexts = new HashMap<>();
}
void setVisitorConfig(VisitorConfig visitorConfig) {
this.visitorConfig = visitorConfig;
}
@Override
protected StaticContext defaultAction(Node node, StaticContext argument) {
StaticContext generatedContext = visitDescendants(node, argument);
// initialize execution mode by visiting children and expressions first, then calling initialize methods
node.initHighestExecutionMode(this.visitorConfig);
return generatedContext;
}
@Override
public StaticContext visit(Node node, StaticContext argument) {
if (argument == null) {
throw new OurBadException("No static context provided!");
}
if (node instanceof Expression) {
((Expression) node).setStaticContext(argument);
}
if (node instanceof Clause) {
((Clause) node).setStaticContext(argument);
}
return node.accept(this, argument);
}
@Override
public StaticContext visitMainModule(MainModule mainModule, StaticContext argument) {
this.importedModuleContexts.clear();
StaticContext generatedContext = visitDescendants(mainModule, argument);
mainModule.initHighestExecutionMode(this.visitorConfig);
return generatedContext;
}
@Override
public StaticContext visitLibraryModule(LibraryModule libraryModule, StaticContext argument) {
if (!this.importedModuleContexts.containsKey(libraryModule.getNamespace())) {
StaticContext moduleContext = libraryModule.getStaticContext();
this.visit(libraryModule.getProlog(), moduleContext);
this.importedModuleContexts.put(libraryModule.getNamespace(), moduleContext);
} else {
libraryModule.getProlog().initHighestExecutionMode(this.visitorConfig);
}
libraryModule.initHighestExecutionMode(this.visitorConfig);
argument.importModuleContext(
this.importedModuleContexts.get(libraryModule.getNamespace()),
libraryModule.getNamespace()
);
return argument;
}
// region primary
@Override
public StaticContext visitVariableReference(VariableReferenceExpression expression, StaticContext argument) {
Name variableName = expression.getVariableName();
if (!argument.isInScope(variableName)) {
throw new UndeclaredVariableException(
"Uninitialized variable reference: " + variableName,
expression.getMetadata()
);
} else {
// note: sequence type can be null
expression.setType(argument.getVariableSequenceType(variableName));
ExecutionMode mode = argument.getVariableStorageMode(variableName);
if (this.visitorConfig.setUnsetToLocal() && mode.equals(ExecutionMode.UNSET)) {
mode = ExecutionMode.LOCAL;
}
expression.setHighestExecutionMode(mode);
// TODO: check staticContext available
return argument;
}
}
private void populateFunctionDeclarationStaticContext(
StaticContext functionDeclarationContext,
List<ExecutionMode> modes,
InlineFunctionExpression expression
) {
int i = 0;
for (Name name : expression.getParams().keySet()) {
ExecutionMode mode = modes.get(i);
SequenceType type = expression.getParams().get(name);
if (type.isEmptySequence()) {
mode = ExecutionMode.LOCAL;
} else if (type.getArity().equals(Arity.OneOrZero) || type.getArity().equals(Arity.One)) {
mode = ExecutionMode.LOCAL;
}
functionDeclarationContext.addVariable(
name,
expression.getParams().get(name),
expression.getMetadata(),
mode
);
++i;
}
}
@Override
public StaticContext visitFunctionDeclaration(FunctionDeclaration declaration, StaticContext argument) {
InlineFunctionExpression expression = (InlineFunctionExpression) declaration.getExpression();
// define a static context for the function body, add params to the context and visit the body expression
List<ExecutionMode> modes = argument.getUserDefinedFunctionsExecutionModes()
.getParameterExecutionMode(
expression.getFunctionIdentifier(),
expression.getMetadata()
);
StaticContext functionDeclarationContext = new StaticContext(argument);
expression.setStaticContext(argument);
populateFunctionDeclarationStaticContext(functionDeclarationContext, modes, expression);
// visit the body first to make its execution mode available while adding the function to the catalog
this.visit(expression.getBody(), functionDeclarationContext);
expression.initHighestExecutionMode(this.visitorConfig);
declaration.initHighestExecutionMode(this.visitorConfig);
expression.registerUserDefinedFunctionExecutionMode(
this.visitorConfig
);
return argument;
}
@Override
public StaticContext visitInlineFunctionExpr(InlineFunctionExpression expression, StaticContext argument) {
// define a static context for the function body, add params to the context and visit the body expression
StaticContext functionDeclarationContextLocal = new StaticContext(argument);
for (Entry<Name, SequenceType> entry : expression.getParams().entrySet()) {
functionDeclarationContextLocal.addVariable(
entry.getKey(),
entry.getValue(),
expression.getMetadata(),
ExecutionMode.LOCAL
);
}
// visit the body first to make its execution mode available while adding the function to the catalog
this.visit(expression.getBodies().get(0L), functionDeclarationContextLocal);
StaticContext functionDeclarationContextRDD = new StaticContext(argument);
boolean first = true;
for (Entry<Name, SequenceType> entry : expression.getParams().entrySet()) {
if (first) {
functionDeclarationContextRDD.addVariable(
entry.getKey(),
entry.getValue(),
expression.getMetadata(),
ExecutionMode.DATAFRAME
);
first = false;
} else {
functionDeclarationContextRDD.addVariable(
entry.getKey(),
entry.getValue(),
expression.getMetadata(),
ExecutionMode.LOCAL
);
}
}
StaticContext functionDeclarationContextDF = new StaticContext(argument);
this.visit(expression.getBodies().get(1L), functionDeclarationContextDF);
expression.initHighestExecutionMode(this.visitorConfig);
expression.registerUserDefinedFunctionExecutionMode(
this.visitorConfig
);
return argument;
}
@Override
public StaticContext visitFunctionCall(FunctionCallExpression expression, StaticContext argument) {
visitDescendants(expression, argument);
FunctionIdentifier identifier = expression.getFunctionIdentifier();
if (!BuiltinFunctionCatalogue.exists(identifier)) {
List<ExecutionMode> modes = new ArrayList<>();
if (expression.isPartialApplication()) {
for (@SuppressWarnings("unused")
Expression parameter : expression.getArguments()) {
modes.add(ExecutionMode.LOCAL);
}
} else {
for (Expression parameter : expression.getArguments()) {
modes.add(parameter.getHighestExecutionMode(this.visitorConfig));
}
}
argument.getUserDefinedFunctionsExecutionModes()
.setParameterExecutionMode(
identifier,
modes,
expression.getMetadata()
);
}
expression.initFunctionCallHighestExecutionMode(this.visitorConfig);
return argument;
}
// endregion
// region FLWOR
@Override
public StaticContext visitFlowrExpression(FlworExpression expression, StaticContext argument) {
Clause clause = expression.getReturnClause().getFirstClause();
StaticContext result = this.visit(clause, argument);
while (clause != null) {
result = this.visit(clause, result);
clause = clause.getNextClause();
}
expression.initHighestExecutionMode(this.visitorConfig);
return argument;
}
// region FLWOR vars
@Override
public StaticContext visitForClause(ForClause clause, StaticContext argument) {
// TODO visit at...
this.visit(clause.getExpression(), argument);
clause.initHighestExecutionMode(this.visitorConfig);
StaticContext result = new StaticContext(argument);
result.addVariable(
clause.getVariableName(),
clause.getActualSequenceType(),
clause.getMetadata(),
clause.getVariableHighestStorageMode(this.visitorConfig)
);
if (clause.getPositionalVariableName() != null) {
result.addVariable(
clause.getPositionalVariableName(),
new SequenceType(BuiltinTypesCatalogue.integerItem),
clause.getMetadata(),
ExecutionMode.LOCAL
);
}
return result;
}
@Override
public StaticContext visitLetClause(LetClause clause, StaticContext argument) {
this.visit(clause.getExpression(), argument);
clause.initHighestExecutionMode(this.visitorConfig);
StaticContext result = new StaticContext(argument);
result.addVariable(
clause.getVariableName(),
clause.getActualSequenceType(),
clause.getMetadata(),
clause.getVariableHighestStorageMode(this.visitorConfig)
);
return result;
}
@Override
public StaticContext visitGroupByClause(GroupByClause clause, StaticContext argument) {
StaticContext groupByClauseContext = new StaticContext(argument);
for (GroupByVariableDeclaration variable : clause.getGroupVariables()) {
if (variable.getExpression() != null) {
// if a variable declaration takes place
this.visit(variable.getExpression(), argument);
groupByClauseContext.addVariable(
variable.getVariableName(),
variable.getActualSequenceType(),
clause.getMetadata(),
ExecutionMode.LOCAL
);
} else if (!argument.isInScope(variable.getVariableName())) {
throw new UndeclaredVariableException(
"Uninitialized variable reference: " + variable.getVariableName(),
clause.getMetadata()
);
}
}
clause.initHighestExecutionMode(this.visitorConfig);
return groupByClauseContext;
}
@Override
public StaticContext visitCountClause(CountClause expression, StaticContext argument) {
expression.initHighestExecutionMode(this.visitorConfig);
StaticContext result = new StaticContext(argument);
result.addVariable(
expression.getCountVariable().getVariableName(),
new SequenceType(BuiltinTypesCatalogue.integerItem, SequenceType.Arity.One),
expression.getMetadata(),
ExecutionMode.LOCAL
);
this.visit(expression.getCountVariable(), result);
return result;
}
// endregion
// region control
@Override
public StaticContext visitTypeSwitchExpression(TypeSwitchExpression expression, StaticContext argument) {
this.visit(expression.getTestCondition(), argument);
for (TypeswitchCase c : expression.getCases()) {
StaticContext caseContext = new StaticContext(argument);
Name variableName = c.getVariableName();
if (variableName != null) {
caseContext.addVariable(
variableName,
null,
expression.getMetadata(),
ExecutionMode.LOCAL
);
}
this.visit(c.getReturnExpression(), caseContext);
}
Name defaultCaseVariableName = expression.getDefaultCase().getVariableName();
if (defaultCaseVariableName == null) {
this.visit(expression.getDefaultCase().getReturnExpression(), argument);
} else {
// add variable to child context to visit default return expression
StaticContext defaultCaseStaticContext = new StaticContext(argument);
defaultCaseStaticContext.addVariable(
defaultCaseVariableName,
null,
expression.getMetadata(),
ExecutionMode.LOCAL
);
this.visit(expression.getDefaultCase().getReturnExpression(), defaultCaseStaticContext);
}
expression.initHighestExecutionMode(this.visitorConfig);
// return the given context unchanged as defined variables go out of scope
return argument;
}
// endregion
@Override
public StaticContext visitVariableDeclaration(VariableDeclaration variableDeclaration, StaticContext argument) {
if (variableDeclaration.getExpression() != null) {
this.visit(variableDeclaration.getExpression(), argument);
}
variableDeclaration.initHighestExecutionMode(this.visitorConfig);
if (argument.hasVariable(variableDeclaration.getVariableName())) {
if (!this.visitorConfig.suppressErrorsForFunctionSignatureCollision()) {
throw new VariableAlreadyExistsException(
variableDeclaration.getVariableName(),
variableDeclaration.getMetadata()
);
}
} else {
// first pass.
argument.addVariable(
variableDeclaration.getVariableName(),
variableDeclaration.getActualSequenceType(),
variableDeclaration.getMetadata(),
variableDeclaration.getVariableHighestStorageMode(this.visitorConfig)
);
}
return argument;
}
}