|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.compiler.bytecode_dsl;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.compiler.CompilationScope.AsyncFunction; |
43 | 44 | import static com.oracle.graal.python.compiler.CompilationScope.Class;
|
| 45 | +import static com.oracle.graal.python.compiler.CompilationScope.TypeParams; |
44 | 46 | import static com.oracle.graal.python.compiler.SSTUtils.checkCaller;
|
45 | 47 | import static com.oracle.graal.python.compiler.SSTUtils.checkCompare;
|
46 | 48 | import static com.oracle.graal.python.compiler.SSTUtils.checkForbiddenArgs;
|
|
99 | 101 | import com.oracle.graal.python.pegparser.ParserCallbacks.WarningType;
|
100 | 102 | import com.oracle.graal.python.pegparser.scope.Scope;
|
101 | 103 | import com.oracle.graal.python.pegparser.scope.Scope.DefUse;
|
| 104 | +import com.oracle.graal.python.pegparser.scope.ScopeEnvironment; |
102 | 105 | import com.oracle.graal.python.pegparser.sst.AliasTy;
|
103 | 106 | import com.oracle.graal.python.pegparser.sst.ArgTy;
|
104 | 107 | import com.oracle.graal.python.pegparser.sst.ArgumentsTy;
|
@@ -187,6 +190,9 @@ public final class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDS
|
187 | 190 | * {@link com.oracle.graal.python.pegparser.scope.ScopeEnvironment#maybeMangle(String, Scope, String)}.
|
188 | 191 | */
|
189 | 192 | private final String privateName;
|
| 193 | + private final RootNodeCompiler parent; |
| 194 | + private final boolean isRoot; |
| 195 | + private String qualName; |
190 | 196 |
|
191 | 197 | // Immutable after construction
|
192 | 198 | private final HashMap<String, Integer> varnames;
|
@@ -215,6 +221,12 @@ public RootNodeCompiler(BytecodeDSLCompilerContext ctx, RootNodeCompiler parent,
|
215 | 221 | this.startNode = rootNode;
|
216 | 222 | this.scope = ctx.scopeEnvironment.lookupScope(scopeKey);
|
217 | 223 | this.scopeType = getScopeType(scope, scopeKey);
|
| 224 | + if (parent == null) { |
| 225 | + this.isRoot = true; |
| 226 | + } else { |
| 227 | + this.isRoot = false; |
| 228 | + } |
| 229 | + this.parent = parent; |
218 | 230 | if (privateName != null) {
|
219 | 231 | this.privateName = privateName;
|
220 | 232 | } else if (scopeType == Class) {
|
@@ -319,6 +331,29 @@ private static TruffleString[] orderedTruffleStringArray(HashMap<String, Integer
|
319 | 331 | return orderedKeys(map, new TruffleString[0], PythonUtils::toTruffleStringUncached);
|
320 | 332 | }
|
321 | 333 |
|
| 334 | + private String getNewScopeQualName(String name, CompilationScope scopeType) { |
| 335 | + RootNodeCompiler parent = this.parent; |
| 336 | + if (parent != null && !parent.isRoot) { |
| 337 | + if (parent.scopeType == TypeParams && parent.parent != null && !parent.parent.isRoot) { |
| 338 | + parent = parent.parent; |
| 339 | + if (parent.parent.parent != null && parent.parent.parent.isRoot) { |
| 340 | + return name; |
| 341 | + } |
| 342 | + } |
| 343 | + if (!(EnumSet.of(CompilationScope.Function, AsyncFunction, Class).contains(scopeType) && |
| 344 | + parent.scope.getUseOfName(ScopeEnvironment.mangle(parent.privateName, name)).contains(Scope.DefUse.GlobalExplicit))) { |
| 345 | + String base; |
| 346 | + if (EnumSet.of(CompilationScope.Function, AsyncFunction, CompilationScope.Lambda).contains(parent.scopeType)) { |
| 347 | + base = parent.qualName + ".<locals>"; |
| 348 | + } else { |
| 349 | + base = parent.qualName; |
| 350 | + } |
| 351 | + return base + "." + name; |
| 352 | + } |
| 353 | + } |
| 354 | + return name; |
| 355 | + } |
| 356 | + |
322 | 357 | private BytecodeDSLCompilerResult compileRootNode(String name, ArgumentInfo argumentInfo, SourceRange sourceRange, BytecodeParser<Builder> parser) {
|
323 | 358 | BytecodeRootNodes<PBytecodeDSLRootNode> nodes = PBytecodeDSLRootNodeGen.create(ctx.language, BytecodeConfig.WITH_SOURCE, parser);
|
324 | 359 | List<PBytecodeDSLRootNode> nodeList = nodes.getNodes();
|
@@ -358,7 +393,9 @@ private BytecodeDSLCompilerResult compileRootNode(String name, ArgumentInfo argu
|
358 | 393 | }
|
359 | 394 | }
|
360 | 395 |
|
361 |
| - BytecodeDSLCodeUnit codeUnit = new BytecodeDSLCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(ctx.getQualifiedName(name, scope)), |
| 396 | + qualName = getNewScopeQualName(name, scopeType); |
| 397 | + |
| 398 | + BytecodeDSLCodeUnit codeUnit = new BytecodeDSLCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(qualName), |
362 | 399 | argumentInfo.argCount, argumentInfo.kwOnlyArgCount, argumentInfo.positionalOnlyArgCount,
|
363 | 400 | flags, orderedTruffleStringArray(names),
|
364 | 401 | orderedTruffleStringArray(varnames),
|
@@ -3672,7 +3709,7 @@ private void emitMakeFunction(BytecodeDSLCodeUnit codeUnit, Object scopeKey, Str
|
3672 | 3709 | ArgumentsTy argsForDefaults, List<ParamAnnotation> annotations) {
|
3673 | 3710 | TruffleString functionName = toTruffleStringUncached(name);
|
3674 | 3711 | Scope targetScope = ctx.scopeEnvironment.lookupScope(scopeKey);
|
3675 |
| - TruffleString qualifiedName = toTruffleStringUncached(ctx.getQualifiedName(name, targetScope)); |
| 3712 | + TruffleString qualifiedName = codeUnit.qualname; |
3676 | 3713 |
|
3677 | 3714 | // Register these in the Python constants list.
|
3678 | 3715 | addConstant(qualifiedName);
|
|
0 commit comments