-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVariableScopeVisitor.java
More file actions
976 lines (860 loc) · 34.6 KB
/
VariableScopeVisitor.java
File metadata and controls
976 lines (860 loc) · 34.6 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
* Copyright 2024, Seqera Labs
*
* Licensed 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.
*/
package nextflow.lsp.services.script;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import groovy.json.JsonSlurper;
import nextflow.lsp.compiler.FutureWarning;
import nextflow.lsp.compiler.PhaseAware;
import nextflow.lsp.compiler.Phases;
import nextflow.lsp.compiler.RelatedInformationAware;
import nextflow.script.dsl.Constant;
import nextflow.script.dsl.EntryWorkflowDsl;
import nextflow.script.dsl.FeatureFlag;
import nextflow.script.dsl.FeatureFlagDsl;
import nextflow.script.dsl.Function;
import nextflow.script.dsl.OutputDsl;
import nextflow.script.dsl.ParamsMap;
import nextflow.script.dsl.ProcessDsl;
import nextflow.script.dsl.ProcessDirectiveDsl;
import nextflow.script.dsl.ProcessInputDsl;
import nextflow.script.dsl.ProcessOutputDsl;
import nextflow.script.dsl.ScriptDsl;
import nextflow.script.dsl.WorkflowDsl;
import nextflow.script.v2.AssignmentExpression;
import nextflow.script.v2.FeatureFlagNode;
import nextflow.script.v2.FunctionNode;
import nextflow.script.v2.IncludeNode;
import nextflow.script.v2.IncludeVariable;
import nextflow.script.v2.OutputNode;
import nextflow.script.v2.ProcessNode;
import nextflow.script.v2.ScriptNode;
import nextflow.script.v2.ScriptVisitorSupport;
import nextflow.script.v2.WorkflowNode;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.DynamicVariable;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.ast.VariableScope;
import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.EmptyExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MapEntryExpression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.CatchStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.control.messages.WarningMessage;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.syntax.Token;
import org.codehaus.groovy.syntax.Types;
import static nextflow.script.v2.ASTHelpers.*;
/**
* Initialize the variable scopes for an AST.
*
* See: org.codehaus.groovy.classgen.VariableScopeVisitor
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
public class VariableScopeVisitor extends ScriptVisitorSupport {
private SourceUnit sourceUnit;
private Map<String,Variable> includes = new HashMap<>();
private MethodNode currentDefinition;
private VariableScope currentScope;
private ClassNode paramsType;
private Set<Variable> declaredVariables = Collections.newSetFromMap(new IdentityHashMap<>());
public VariableScopeVisitor(SourceUnit sourceUnit) {
this.sourceUnit = sourceUnit;
this.currentScope = new VariableScope();
this.currentScope.setClassScope(new ClassNode(ScriptDsl.class));
}
@Override
protected SourceUnit getSourceUnit() {
return sourceUnit;
}
public void declare() {
var moduleNode = sourceUnit.getAST();
if( moduleNode instanceof ScriptNode sn ) {
for( var includeNode : sn.getIncludes() )
declareInclude(includeNode);
for( var workflowNode : sn.getWorkflows() ) {
if( !workflowNode.isEntry() )
declareMethod(workflowNode);
}
for( var processNode : sn.getProcesses() )
declareMethod(processNode);
for( var functionNode : sn.getFunctions() )
declareMethod(functionNode);
}
}
private void declareInclude(IncludeNode node) {
for( var module : node.modules ) {
var name = module.getName();
var otherInclude = includes.get(name);
if( otherInclude != null )
addError("`" + name + "` is already included", node, "First included here", (ASTNode) otherInclude);
includes.put(name, module);
declaredVariables.add(module);
}
}
private void declareMethod(MethodNode mn) {
var cn = currentScope.getClassScope();
var name = mn.getName();
var otherInclude = includes.get(name);
if( otherInclude != null ) {
addError("`" + name + "` is already included", mn, "First included here", (ASTNode) otherInclude);
}
var otherMethods = cn.getDeclaredMethods(name);
if( otherMethods.size() > 0 ) {
var other = otherMethods.get(0);
var first = mn.getLineNumber() < other.getLineNumber() ? mn : other;
var second = mn.getLineNumber() < other.getLineNumber() ? other : mn;
addError("`" + name + "` is already declared", second, "First declared here", first);
return;
}
cn.addMethod(mn);
}
public void visit() {
var moduleNode = sourceUnit.getAST();
if( moduleNode instanceof ScriptNode sn ) {
// visit top-level definitions
super.visit(sn);
// warn about any unused local variables
for( var variable : declaredVariables ) {
if( variable instanceof ASTNode node && !variable.getName().startsWith("_") ) {
var message = variable instanceof IncludeVariable
? "Include was not used"
: "Variable was declared but not used";
sourceUnit.addWarning(message, node);
}
}
}
}
@Override
public void visitFeatureFlag(FeatureFlagNode node) {
var cn = ClassHelper.makeCached(FeatureFlagDsl.class);
var result = cn.getFields().stream()
.filter(fn ->
findAnnotation(fn, FeatureFlag.class)
.map(an -> an.getMember("name").getText())
.map(name -> name.equals(node.name))
.orElse(false)
)
.findFirst();
if( result.isPresent() )
node.target = result.get();
else
addError("Unrecognized feature flag '" + node.name + "'", node);
}
private boolean inWorkflowEmit;
@Override
public void visitWorkflow(WorkflowNode node) {
if( node.isEntry() )
declareParameters();
pushState(node.isEntry() ? EntryWorkflowDsl.class : WorkflowDsl.class);
currentDefinition = node;
node.setVariableScope(currentScope);
declareWorkflowInputs(node.takes);
visit(node.main);
if( node.main instanceof BlockStatement block )
copyVariableScope(block.getVariableScope());
visitWorkflowEmits(node.emits);
visit(node.publishers);
currentDefinition = null;
popState();
if( node.isEntry() )
this.paramsType = null;
}
private void declareParameters() {
// load parameter schema
var uri = sourceUnit.getSource().getURI();
var schemaPath = Path.of(uri).getParent().resolve("nextflow_schema.json");
if( !Files.exists(schemaPath) )
return;
var schemaJson = getParameterSchema(schemaPath);
var defs = Optional.ofNullable(schemaJson)
.flatMap(json -> asMap(json))
.flatMap(json ->
json.containsKey("$defs")
? asMap(json.get("$defs")) :
json.containsKey("defs")
? asMap(json.get("defs")) :
json.containsKey("definitions")
? asMap(json.get("definitions"))
: Optional.empty()
)
.orElse(Collections.emptyMap());
var entries = (List<Map.Entry>) defs.values().stream()
.filter(defn -> defn instanceof Map)
.map(defn -> ((Map) defn).get("properties"))
.filter(props -> props instanceof Map)
.flatMap(props -> ((Map) props).entrySet().stream())
.collect(Collectors.toList());
if( entries.isEmpty() )
return;
// create synthetic params type
var cn = new ClassNode(ParamsMap.class);
for( var entry : entries ) {
var name = (String) entry.getKey();
var attrs = asMap(entry.getValue()).orElse(null);
if( attrs == null )
continue;
var type = getTypeClassFromString((String) attrs.get("type"));
var description = (String) attrs.get("description");
var fn = new FieldNode(name, Modifier.PUBLIC, type, cn, null);
fn.setHasNoRealSourcePosition(true);
fn.setDeclaringClass(cn);
fn.setSynthetic(true);
var an = new AnnotationNode(ClassHelper.makeCached(Constant.class));
an.addMember("value", new ConstantExpression(description));
fn.addAnnotation(an);
cn.addField(fn);
}
this.paramsType = cn;
}
private Object getParameterSchema(Path schemaPath) {
try {
var schemaText = IOGroovyMethods.getText(Files.newInputStream(schemaPath));
return new JsonSlurper().parseText(schemaText);
}
catch( IOException e ) {
System.err.println("Failed to read parameter schema: " + e.toString());
return null;
}
}
private static Optional<Map> asMap(Object value) {
return value instanceof Map
? Optional.of((Map) value)
: Optional.empty();
}
private ClassNode getTypeClassFromString(String type) {
if( "boolean".equals(type) )
return ClassHelper.boolean_TYPE;
if( "integer".equals(type) )
return ClassHelper.long_TYPE;
if( "number".equals(type) )
return ClassHelper.double_TYPE;
if( "string".equals(type) )
return ClassHelper.STRING_TYPE;
return ClassHelper.dynamicType();
}
private void declareWorkflowInputs(Statement takes) {
for( var stmt : asBlockStatements(takes) ) {
var varX = asVarX(stmt);
if( varX == null )
continue;
declare(varX);
}
}
private void copyVariableScope(VariableScope source) {
for( var it = source.getDeclaredVariablesIterator(); it.hasNext(); ) {
var variable = it.next();
currentScope.putDeclaredVariable(variable);
}
}
private void visitWorkflowEmits(Statement emits) {
var declaredEmits = new HashMap<String,ASTNode>();
for( var stmt : asBlockStatements(emits) ) {
var stmtX = (ExpressionStatement)stmt;
var emit = stmtX.getExpression();
if( emit instanceof AssignmentExpression assign ) {
visit(assign.getRightExpression());
var target = (VariableExpression)assign.getLeftExpression();
var name = target.getName();
var other = declaredEmits.get(name);
if( other != null )
addError("Workflow emit `" + name + "` is already declared", target, "First declared here", other);
else
declaredEmits.put(name, target);
}
else {
visit(emit);
}
}
}
@Override
public void visitProcess(ProcessNode node) {
pushState(ProcessDsl.class);
currentDefinition = node;
node.setVariableScope(currentScope);
declareProcessInputs(node.inputs);
pushState(ProcessInputDsl.class);
visitDirectives(node.inputs, "process input qualifier", false);
popState();
if( !(node.when instanceof EmptyExpression) )
addFutureWarning("Process `when` section will not be supported in a future version", node.when);
visit(node.when);
visit(node.exec);
visit(node.stub);
pushState(ProcessDirectiveDsl.class);
visitDirectives(node.directives, "process directive", false);
popState();
pushState(ProcessOutputDsl.class);
visitDirectives(node.outputs, "process output qualifier", false);
popState();
currentDefinition = null;
popState();
}
private void declareProcessInputs(Statement inputs) {
for( var stmt : asBlockStatements(inputs) ) {
var call = asMethodCallX(stmt);
if( call == null )
continue;
if( "tuple".equals(call.getMethodAsString()) ) {
for( var arg : asMethodCallArguments(call) ) {
if( arg instanceof MethodCallExpression mce )
declareProcessInput(mce);
}
}
else if( "each".equals(call.getMethodAsString()) ) {
var args = asMethodCallArguments(call);
if( args.size() != 1 )
continue;
var firstArg = args.get(0);
if( firstArg instanceof MethodCallExpression mce )
declareProcessInput(mce);
else if( firstArg instanceof VariableExpression ve )
declare(ve);
}
else {
declareProcessInput(call);
}
}
}
private static final List<String> DECLARING_INPUT_TYPES = List.of("val", "file", "path");
private void declareProcessInput(MethodCallExpression call) {
if( !DECLARING_INPUT_TYPES.contains(call.getMethodAsString()) )
return;
var args = asMethodCallArguments(call);
if( args.isEmpty() )
return;
if( args.get(args.size() - 1) instanceof VariableExpression ve )
declare(ve);
}
private void visitDirectives(Statement node, String typeLabel, boolean checkSyntaxErrors) {
if( node instanceof BlockStatement block )
block.setVariableScope(currentScope);
for( var stmt : asBlockStatements(node) ) {
var call = checkDirective(stmt, typeLabel, checkSyntaxErrors);
if( call != null )
super.visitMethodCallExpression(call);
}
}
private MethodCallExpression checkDirective(Statement node, String typeLabel, boolean checkSyntaxErrors) {
var call = asMethodCallX(node);
if( call == null ) {
if( checkSyntaxErrors )
addSyntaxError("Invalid " + typeLabel, node);
return null;
}
var name = call.getMethodAsString();
var variable = findClassMember(currentScope.getClassScope(), name, call.getMethod());
if( variable != null )
currentScope.putReferencedClassVariable(variable);
else
addError("Invalid " + typeLabel + " `" + name + "`", node);
return call;
}
private static final List<String> EMIT_AND_TOPIC = List.of("emit", "topic");
@Override
public void visitMapEntryExpression(MapEntryExpression node) {
var classScope = currentScope.getClassScope();
if( classScope != null && classScope.getTypeClass() == ProcessOutputDsl.class ) {
var key = node.getKeyExpression();
if( key instanceof ConstantExpression && EMIT_AND_TOPIC.contains(key.getText()) )
return;
}
super.visitMapEntryExpression(node);
}
@Override
public void visitFunction(FunctionNode node) {
pushState();
currentDefinition = node;
node.setVariableScope(currentScope);
for( var parameter : node.getParameters() ) {
if( parameter.hasInitialExpression() )
visit(parameter.getInitialExpression());
declare(parameter, node);
}
visit(node.getCode());
currentDefinition = null;
popState();
}
@Override
public void visitOutput(OutputNode node) {
if( node.body instanceof BlockStatement block )
visitOutputBody(block);
}
private void visitOutputBody(BlockStatement block) {
pushState(OutputDsl.class);
block.setVariableScope(currentScope);
asDirectives(block).forEach((call) -> {
var code = asDslBlock(call, 1);
if( code != null )
visitTargetBody(code);
});
popState();
}
private void visitTargetBody(BlockStatement block) {
pushState(OutputDsl.TargetDsl.class);
block.setVariableScope(currentScope);
asBlockStatements(block).forEach((stmt) -> {
// validate target directive
var call = checkDirective(stmt, "output target directive", true);
if( call == null )
return;
// treat as index definition
var name = call.getMethodAsString();
if( "index".equals(name) ) {
var code = asDslBlock(call, 1);
if( code != null ) {
pushState(OutputDsl.IndexDsl.class);
visitDirectives(code, "output index directive", true);
popState();
return;
}
}
// treat as regular directive
super.visitMethodCallExpression(call);
});
popState();
}
// statements
@Override
public void visitBlockStatement(BlockStatement node) {
var newScope = node.getVariableScope() != null;
if( newScope ) pushState();
node.setVariableScope(currentScope);
super.visitBlockStatement(node);
if( newScope ) popState();
}
@Override
public void visitCatchStatement(CatchStatement node) {
pushState();
declare(node.getVariable(), node);
super.visitCatchStatement(node);
popState();
}
// statements
private static final List<String> KEYWORDS = List.of(
"case",
"for",
"switch",
"while"
);
@Override
public void visitMethodCallExpression(MethodCallExpression node) {
if( currentDefinition instanceof WorkflowNode ) {
visitAssignmentOperator(node);
}
if( node.isImplicitThis() && node.getMethod() instanceof ConstantExpression ) {
var name = node.getMethodAsString();
var variable = findVariableDeclaration(name, node);
if( variable == null ) {
if( !KEYWORDS.contains(name) )
addError("`" + name + "` is not defined", node.getMethod());
}
}
super.visitMethodCallExpression(node);
}
/**
* Treat `set` operator as an assignment.
*/
private void visitAssignmentOperator(MethodCallExpression node) {
var name = node.getMethodAsString();
if( !("set".equals(name) || "tap".equals(name)) )
return;
var code = asDslBlock(node, 1);
if( code == null || code.getStatements().size() != 1 )
return;
var varX = asVarX(code.getStatements().get(0));
if( varX == null )
return;
currentScope.putDeclaredVariable(varX);
}
@Override
public void visitBinaryExpression(BinaryExpression node) {
if( node instanceof AssignmentExpression ) {
visit(node.getRightExpression());
visitAssignmentTarget(node.getLeftExpression());
}
else {
super.visitBinaryExpression(node);
}
}
/**
* In processes and workflows, variables can be declared without `def`
* and are treated as variables scoped to the process or workflow.
*
* @param node
*/
private void visitAssignmentTarget(Expression node) {
if( node instanceof TupleExpression te ) {
for( var el : te.getExpressions() )
declareAssignedVariable((VariableExpression) el);
}
else if( node instanceof VariableExpression ve ) {
declareAssignedVariable(ve);
}
else {
visitMutatedVariable(node);
visit(node);
}
}
private void declareAssignedVariable(VariableExpression ve) {
var variable = findVariableDeclaration(ve.getName(), ve);
if( variable != null ) {
if( variable instanceof FieldNode fn && findAnnotation(fn, Constant.class).isPresent() )
addError("Built-in variable cannot be re-assigned", ve);
else
checkExternalWriteInClosure(ve, variable);
}
else if( currentDefinition instanceof ProcessNode || currentDefinition instanceof WorkflowNode ) {
if( currentClosure != null )
addError("Variables in a closure should be declared with `def`", ve);
else if( !(currentDefinition instanceof ProcessNode) )
addFutureWarning("Variables should be declared with `def`", ve);
var scope = currentScope;
currentScope = currentDefinition.getVariableScope();
declare(ve);
currentScope = scope;
}
else {
addError("`" + ve.getName() + "` was assigned but not declared", ve);
}
}
private void visitMutatedVariable(Expression node) {
VariableExpression target = null;
while( true ) {
// e.g. obj.prop = 123
if( node instanceof PropertyExpression pe ) {
node = pe.getObjectExpression();
}
// e.g. list[1] = 123 OR map['a'] = 123
else if( node instanceof BinaryExpression be && be.getOperation().getType() == Types.LEFT_SQUARE_BRACKET ) {
node = be.getLeftExpression();
}
else {
if( node instanceof VariableExpression ve )
target = ve;
break;
}
}
if( target == null )
return;
var variable = findVariableDeclaration(target.getName(), target);
if( variable instanceof FieldNode fn && findAnnotation(fn, Constant.class).isPresent() ) {
if( "params".equals(variable.getName()) )
sourceUnit.addWarning("Params should be declared at the top-level (i.e. outside the workflow)", target);
// TODO: re-enable after workflow.onComplete bug is fixed
// else
// addError("Built-in variable cannot be mutated", target);
}
else if( variable != null ) {
checkExternalWriteInClosure(target, variable);
}
}
private void checkExternalWriteInClosure(VariableExpression target, Variable variable) {
if( currentClosure == null )
return;
var scope = currentClosure.getVariableScope();
var name = variable.getName();
if( scope.isReferencedLocalVariable(name) && scope.getDeclaredVariable(name) == null )
addFutureWarning("Mutating an external variable in a closure may lead to a race condition", target, "External variable declared here", (ASTNode) variable);
}
@Override
public void visitDeclarationExpression(DeclarationExpression node) {
visit(node.getRightExpression());
if( node.isMultipleAssignmentDeclaration() ) {
for( var el : node.getTupleExpression() )
declare((VariableExpression) el);
}
else {
declare(node.getVariableExpression());
}
}
private ClosureExpression currentClosure;
@Override
public void visitClosureExpression(ClosureExpression node) {
var cl = currentClosure;
currentClosure = node;
pushState();
node.setVariableScope(currentScope);
if( node.getParameters() != null ) {
for( var parameter : node.getParameters() ) {
declare(parameter, parameter);
if( parameter.hasInitialExpression() )
visit(parameter.getInitialExpression());
}
}
super.visitClosureExpression(node);
for( var it = currentScope.getReferencedLocalVariablesIterator(); it.hasNext(); ) {
var variable = it.next();
variable.setClosureSharedVariable(true);
}
popState();
currentClosure = cl;
}
@Override
public void visitPropertyExpression(PropertyExpression node) {
super.visitPropertyExpression(node);
// validate parameter against schema if applicable
// NOTE: should be incorporated into type-checking visitor
if( paramsType == null )
return;
if( !(node.getObjectExpression() instanceof VariableExpression) )
return;
var varX = (VariableExpression) node.getObjectExpression();
if( !"params".equals(varX.getName()) )
return;
var property = node.getPropertyAsString();
if( findClassMember(paramsType, property, node) == null ) {
addError("Unrecognized parameter `" + property + "`", node);
return;
}
var variable = varX.getAccessedVariable();
if( variable instanceof FieldNode fn )
fn.setType(paramsType);
}
@Override
public void visitVariableExpression(VariableExpression node) {
var name = node.getName();
Variable variable = findVariableDeclaration(name, node);
if( variable == null ) {
if( "it".equals(name) ) {
addFutureWarning("Implicit variable `it` in closure will not be supported in a future version", node);
}
else if( "args".equals(name) ) {
addFutureWarning("The use of `args` outside the entry workflow will not be supported in a future version", node);
}
else if( "params".equals(name) ) {
addFutureWarning("The use of `params` outside the entry workflow will not be supported in a future version", node);
}
else {
variable = new DynamicVariable(name, false);
}
}
if( variable != null ) {
checkGlobalVariableInProcess(variable, node);
node.setAccessedVariable(variable);
}
}
private static final List<String> WARN_GLOBALS = List.of(
"baseDir",
"launchDir",
"projectDir",
"workDir"
);
private void checkGlobalVariableInProcess(Variable variable, ASTNode context) {
if( !(currentDefinition instanceof ProcessNode) )
return;
if( variable instanceof FieldNode fn && fn.getDeclaringClass().getTypeClass() == ScriptDsl.class ) {
if( WARN_GLOBALS.contains(variable.getName()) )
sourceUnit.addWarning("The use of `" + variable.getName() + "` in a process is discouraged -- input files should be provided as process inputs", context);
}
}
// helpers
private void pushState(Class classScope) {
currentScope = new VariableScope(currentScope);
if( classScope != null )
currentScope.setClassScope(ClassHelper.makeCached(classScope));
}
private void pushState() {
pushState(null);
}
private void popState() {
currentScope = currentScope.getParent();
}
private void declare(VariableExpression variable) {
declare(variable, variable);
variable.setAccessedVariable(variable);
}
private void declare(Variable variable, ASTNode context) {
var name = variable.getName();
for( var scope = currentScope; scope != null; scope = scope.getParent() ) {
var other = scope.getDeclaredVariable(name);
if( other != null ) {
addError("`" + name + "` is already declared", context, "First declared here", (ASTNode) other);
break;
}
}
currentScope.putDeclaredVariable(variable);
declaredVariables.add(variable);
}
/**
* Find the declaration of a given variable.
*
* @param name
* @param node
*/
private Variable findVariableDeclaration(String name, ASTNode node) {
Variable variable = null;
VariableScope scope = currentScope;
boolean isClassVariable = false;
while( scope != null ) {
variable = scope.getDeclaredVariable(name);
if( variable != null )
break;
variable = scope.getReferencedLocalVariable(name);
if( variable != null )
break;
variable = scope.getReferencedClassVariable(name);
if( variable != null ) {
isClassVariable = true;
break;
}
variable = findClassMember(scope.getClassScope(), name, node);
if( variable != null ) {
isClassVariable = true;
break;
}
variable = includes.get(name);
if( variable != null ) {
isClassVariable = true;
break;
}
scope = scope.getParent();
}
if( variable == null )
return null;
VariableScope end = scope;
scope = currentScope;
while( true ) {
if( isClassVariable )
scope.putReferencedClassVariable(variable);
else
scope.putReferencedLocalVariable(variable);
if( scope == end )
break;
scope = scope.getParent();
}
declaredVariables.remove(variable);
return variable;
}
private Variable findClassMember(ClassNode cn, String name, ASTNode node) {
while( cn != null && !ClassHelper.isObjectType(cn) ) {
var fn = cn.getDeclaredField(name);
if( fn != null && findAnnotation(fn, Constant.class).isPresent() ) {
if( findAnnotation(fn, Deprecated.class).isPresent() )
addFutureWarning("`" + name + "` is deprecated and will be removed in a future version", node);
return fn;
}
var methods = cn.getDeclaredMethods(name);
var mn = methods.size() > 0 ? methods.get(0) : null;
if( mn != null ) {
if( mn instanceof FunctionNode || mn instanceof ProcessNode || mn instanceof WorkflowNode ) {
return wrapMethodAsVariable(mn, cn);
}
if( findAnnotation(mn, Function.class).isPresent() ) {
if( findAnnotation(mn, Deprecated.class).isPresent() )
addFutureWarning("`" + name + "` is deprecated and will be removed in a future version", node);
return wrapMethodAsVariable(mn, cn);
}
}
cn = cn.getSuperClass();
}
return null;
}
private Variable wrapMethodAsVariable(MethodNode mn, ClassNode cn) {
var fn = new FieldNode(mn.getName(), mn.getModifiers() & 0xF, ClassHelper.dynamicType(), cn, null);
fn.setHasNoRealSourcePosition(true);
fn.setDeclaringClass(cn);
fn.setSynthetic(true);
var pn = new PropertyNode(fn, fn.getModifiers(), null, null);
pn.putNodeMetaData("access.method", mn);
pn.setDeclaringClass(cn);
return pn;
}
protected void addSyntaxError(String message, ASTNode node) {
var cause = new SyntaxException(message, node);
var errorMessage = new SyntaxErrorMessage(cause, sourceUnit);
sourceUnit.getErrorCollector().addErrorAndContinue(errorMessage);
}
protected void addFutureWarning(String message, ASTNode node, String otherMessage, ASTNode otherNode) {
var token = new Token(0, "", node.getLineNumber(), node.getColumnNumber()); // ASTNode to CSTNode
var warning = new FutureWarning(WarningMessage.POSSIBLE_ERRORS, message, token, sourceUnit);
if( otherNode != null )
warning.setRelatedInformation(otherMessage, otherNode);
sourceUnit.getErrorCollector().addWarning(warning);
}
protected void addFutureWarning(String message, ASTNode node) {
addFutureWarning(message, node, null, null);
}
@Override
public void addError(String message, ASTNode node) {
addError(new VariableScopeError(message, node));
}
protected void addError(String message, ASTNode node, String otherMessage, ASTNode otherNode) {
var cause = new VariableScopeError(message, node);
if( otherNode != null )
cause.setRelatedInformation(otherMessage, otherNode);
addError(cause);
}
protected void addError(SyntaxException cause) {
var errorMessage = new SyntaxErrorMessage(cause, sourceUnit);
sourceUnit.getErrorCollector().addErrorAndContinue(errorMessage);
}
private class VariableScopeError extends SyntaxException implements PhaseAware, RelatedInformationAware {
private String otherMessage;
private ASTNode otherNode;
public VariableScopeError(String message, ASTNode node) {
super(message, node);
}
public void setRelatedInformation(String otherMessage, ASTNode otherNode) {
this.otherMessage = otherMessage;
this.otherNode = otherNode;
}
@Override
public int getPhase() {
return Phases.NAME_RESOLUTION;
}
@Override
public String getOtherMessage() {
return otherMessage;
}
@Override
public ASTNode getOtherNode() {
return otherNode;
}
}
}