|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
1 | 41 | package com.oracle.graal.python.compiler.bytecode_dsl;
|
2 | 42 |
|
3 | 43 | import static com.oracle.graal.python.compiler.CompilationScope.Class;
|
4 |
| -import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached; |
5 | 44 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___CLASS__;
|
| 45 | +import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached; |
6 | 46 |
|
7 | 47 | import java.util.ArrayList;
|
8 | 48 | import java.util.Arrays;
|
|
24 | 64 | import com.oracle.graal.python.compiler.Compiler;
|
25 | 65 | import com.oracle.graal.python.compiler.Compiler.ConstantCollection;
|
26 | 66 | import com.oracle.graal.python.compiler.OpCodes.CollectionBits;
|
| 67 | +import com.oracle.graal.python.compiler.Unparser; |
27 | 68 | import com.oracle.graal.python.compiler.bytecode_dsl.BytecodeDSLCompiler.BytecodeDSLCompilerContext;
|
28 | 69 | import com.oracle.graal.python.compiler.bytecode_dsl.BytecodeDSLCompiler.BytecodeDSLCompilerResult;
|
29 |
| -import com.oracle.graal.python.compiler.Unparser; |
30 | 70 | import com.oracle.graal.python.nodes.StringLiterals;
|
31 | 71 | import com.oracle.graal.python.nodes.bytecode_dsl.BytecodeDSLCodeUnit;
|
32 | 72 | import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNode;
|
33 | 73 | import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNodeGen;
|
34 | 74 | import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNodeGen.Builder;
|
35 |
| -import com.oracle.graal.python.pegparser.FutureFeature; |
36 | 75 | import com.oracle.graal.python.pegparser.ErrorCallback.ErrorType;
|
37 | 76 | import com.oracle.graal.python.pegparser.ErrorCallback.WarningType;
|
| 77 | +import com.oracle.graal.python.pegparser.FutureFeature; |
38 | 78 | import com.oracle.graal.python.pegparser.scope.Scope;
|
39 | 79 | import com.oracle.graal.python.pegparser.scope.Scope.DefUse;
|
40 | 80 | import com.oracle.graal.python.pegparser.sst.AliasTy;
|
|
48 | 88 | import com.oracle.graal.python.pegparser.sst.ExceptHandlerTy;
|
49 | 89 | import com.oracle.graal.python.pegparser.sst.ExprContextTy;
|
50 | 90 | import com.oracle.graal.python.pegparser.sst.ExprTy;
|
| 91 | +import com.oracle.graal.python.pegparser.sst.ExprTy.Constant; |
| 92 | +import com.oracle.graal.python.pegparser.sst.ExprTy.DictComp; |
| 93 | +import com.oracle.graal.python.pegparser.sst.ExprTy.GeneratorExp; |
| 94 | +import com.oracle.graal.python.pegparser.sst.ExprTy.Lambda; |
| 95 | +import com.oracle.graal.python.pegparser.sst.ExprTy.ListComp; |
| 96 | +import com.oracle.graal.python.pegparser.sst.ExprTy.SetComp; |
51 | 97 | import com.oracle.graal.python.pegparser.sst.KeywordTy;
|
52 | 98 | import com.oracle.graal.python.pegparser.sst.MatchCaseTy;
|
53 | 99 | import com.oracle.graal.python.pegparser.sst.ModTy;
|
54 | 100 | import com.oracle.graal.python.pegparser.sst.OperatorTy;
|
55 | 101 | import com.oracle.graal.python.pegparser.sst.PatternTy;
|
56 | 102 | import com.oracle.graal.python.pegparser.sst.SSTNode;
|
57 | 103 | import com.oracle.graal.python.pegparser.sst.StmtTy;
|
| 104 | +import com.oracle.graal.python.pegparser.sst.StmtTy.AsyncFunctionDef; |
58 | 105 | import com.oracle.graal.python.pegparser.sst.UnaryOpTy;
|
59 | 106 | import com.oracle.graal.python.pegparser.sst.WithItemTy;
|
60 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.Constant; |
61 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.DictComp; |
62 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.GeneratorExp; |
63 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.Lambda; |
64 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.ListComp; |
65 |
| -import com.oracle.graal.python.pegparser.sst.ExprTy.SetComp; |
66 |
| -import com.oracle.graal.python.pegparser.sst.StmtTy.AsyncFunctionDef; |
67 | 107 | import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
|
68 | 108 | import com.oracle.graal.python.util.PythonUtils;
|
69 | 109 | import com.oracle.truffle.api.CompilerDirectives;
|
70 | 110 | import com.oracle.truffle.api.bytecode.BytecodeConfig;
|
71 | 111 | import com.oracle.truffle.api.bytecode.BytecodeLabel;
|
72 | 112 | import com.oracle.truffle.api.bytecode.BytecodeLocal;
|
| 113 | +import com.oracle.truffle.api.bytecode.BytecodeParser; |
73 | 114 | import com.oracle.truffle.api.bytecode.BytecodeRootNodes;
|
74 | 115 | import com.oracle.truffle.api.instrumentation.StandardTags.StatementTag;
|
75 |
| -import com.oracle.truffle.api.bytecode.BytecodeParser; |
76 | 116 | import com.oracle.truffle.api.strings.TruffleString;
|
77 | 117 |
|
78 | 118 | /**
|
|
82 | 122 | * This visitor is a small wrapper that calls into another visitor, {@link StatementCompiler}, to
|
83 | 123 | * produce bytecode for the various statements/expressions within the AST.
|
84 | 124 | */
|
85 |
| -public class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDSLCompilerResult> { |
| 125 | +public final class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDSLCompilerResult> { |
86 | 126 | /**
|
87 | 127 | * Because a {@link RootNodeCompiler} instance gets reused on reparse, it should be idempotent.
|
88 | 128 | * Consequently, most of its fields are final and immutable/not mutated after construction. For
|
@@ -303,41 +343,45 @@ private boolean nonEmpty() {
|
303 | 343 | }
|
304 | 344 | }
|
305 | 345 |
|
306 |
| - protected final void checkForbiddenName(String id, NameOperation context) { |
| 346 | + private void checkForbiddenName(String id, NameOperation context) { |
| 347 | + checkForbiddenName(id, context, currentLocation); |
| 348 | + } |
| 349 | + |
| 350 | + private void checkForbiddenName(String id, NameOperation context, SourceRange location) { |
307 | 351 | if (context == NameOperation.BeginWrite) {
|
308 | 352 | if (id.equals("__debug__")) {
|
309 |
| - ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "cannot assign to __debug__"); |
| 353 | + ctx.errorCallback.onError(ErrorType.Syntax, location, "cannot assign to __debug__"); |
310 | 354 | }
|
311 | 355 | }
|
312 | 356 | if (context == NameOperation.Delete) {
|
313 | 357 | if (id.equals("__debug__")) {
|
314 |
| - ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "cannot delete __debug__"); |
| 358 | + ctx.errorCallback.onError(ErrorType.Syntax, location, "cannot delete __debug__"); |
315 | 359 | }
|
316 | 360 | }
|
317 | 361 | }
|
318 | 362 |
|
319 |
| - private void checkForbiddenArgs(ArgumentsTy args) { |
| 363 | + private void checkForbiddenArgs(ArgumentsTy args, SourceRange location) { |
320 | 364 | if (args != null) {
|
321 | 365 | if (args.posOnlyArgs != null) {
|
322 | 366 | for (ArgTy arg : args.posOnlyArgs) {
|
323 |
| - checkForbiddenName(arg.arg, NameOperation.BeginWrite); |
| 367 | + checkForbiddenName(arg.arg, NameOperation.BeginWrite, location); |
324 | 368 | }
|
325 | 369 | }
|
326 | 370 | if (args.args != null) {
|
327 | 371 | for (ArgTy arg : args.args) {
|
328 |
| - checkForbiddenName(arg.arg, NameOperation.BeginWrite); |
| 372 | + checkForbiddenName(arg.arg, NameOperation.BeginWrite, location); |
329 | 373 | }
|
330 | 374 | }
|
331 | 375 | if (args.kwOnlyArgs != null) {
|
332 | 376 | for (ArgTy arg : args.kwOnlyArgs) {
|
333 |
| - checkForbiddenName(arg.arg, NameOperation.BeginWrite); |
| 377 | + checkForbiddenName(arg.arg, NameOperation.BeginWrite, location); |
334 | 378 | }
|
335 | 379 | }
|
336 | 380 | if (args.varArg != null) {
|
337 |
| - checkForbiddenName(args.varArg.arg, NameOperation.BeginWrite); |
| 381 | + checkForbiddenName(args.varArg.arg, NameOperation.BeginWrite, location); |
338 | 382 | }
|
339 | 383 | if (args.kwArg != null) {
|
340 |
| - checkForbiddenName(args.kwArg.arg, NameOperation.BeginWrite); |
| 384 | + checkForbiddenName(args.kwArg.arg, NameOperation.BeginWrite, location); |
341 | 385 | }
|
342 | 386 | }
|
343 | 387 | }
|
@@ -436,7 +480,7 @@ void beginRootNode(SSTNode node, ArgumentsTy args, Builder b) {
|
436 | 480 |
|
437 | 481 | b.beginRoot();
|
438 | 482 |
|
439 |
| - checkForbiddenArgs(args); |
| 483 | + checkForbiddenArgs(args, node.getSourceRange()); |
440 | 484 | setUpFrame(args, b);
|
441 | 485 |
|
442 | 486 | b.emitTraceOrProfileCall();
|
@@ -4041,10 +4085,10 @@ public Void visit(StmtTy.Raise node) {
|
4041 | 4085 |
|
4042 | 4086 | @Override
|
4043 | 4087 | public Void visit(StmtTy.Return node) {
|
| 4088 | + boolean newStatement = beginSourceSection(node, b); |
4044 | 4089 | if (!scope.isFunction()) {
|
4045 | 4090 | ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "'return' outside function");
|
4046 | 4091 | }
|
4047 |
| - boolean newStatement = beginSourceSection(node, b); |
4048 | 4092 | beginReturn(b);
|
4049 | 4093 | if (node.value != null) {
|
4050 | 4094 | node.value.accept(this);
|
@@ -4538,21 +4582,21 @@ public Void visit(WithItemTy node) {
|
4538 | 4582 |
|
4539 | 4583 | @Override
|
4540 | 4584 | public Void visit(StmtTy.Break aThis) {
|
| 4585 | + boolean newStatement = beginSourceSection(aThis, b); |
4541 | 4586 | if (breakLabel == null) {
|
4542 | 4587 | ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "'break' outside loop");
|
4543 | 4588 | }
|
4544 |
| - boolean newStatement = beginSourceSection(aThis, b); |
4545 | 4589 | b.emitBranch(breakLabel);
|
4546 | 4590 | endSourceSection(b, newStatement);
|
4547 | 4591 | return null;
|
4548 | 4592 | }
|
4549 | 4593 |
|
4550 | 4594 | @Override
|
4551 | 4595 | public Void visit(StmtTy.Continue aThis) {
|
| 4596 | + boolean newStatement = beginSourceSection(aThis, b); |
4552 | 4597 | if (continueLabel == null) {
|
4553 | 4598 | ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "'continue' not properly in loop");
|
4554 | 4599 | }
|
4555 |
| - boolean newStatement = beginSourceSection(aThis, b); |
4556 | 4600 | b.emitBranch(continueLabel);
|
4557 | 4601 | endSourceSection(b, newStatement);
|
4558 | 4602 | return null;
|
|
0 commit comments