|
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,8 @@ 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 String qualName; |
190 | 195 |
|
191 | 196 | // Immutable after construction |
192 | 197 | private final HashMap<String, Integer> varnames; |
@@ -215,6 +220,7 @@ public RootNodeCompiler(BytecodeDSLCompilerContext ctx, RootNodeCompiler parent, |
215 | 220 | this.startNode = rootNode; |
216 | 221 | this.scope = ctx.scopeEnvironment.lookupScope(scopeKey); |
217 | 222 | this.scopeType = getScopeType(scope, scopeKey); |
| 223 | + this.parent = parent; |
218 | 224 | if (privateName != null) { |
219 | 225 | this.privateName = privateName; |
220 | 226 | } else if (scopeType == Class) { |
@@ -319,7 +325,33 @@ private static TruffleString[] orderedTruffleStringArray(HashMap<String, Integer |
319 | 325 | return orderedKeys(map, new TruffleString[0], PythonUtils::toTruffleStringUncached); |
320 | 326 | } |
321 | 327 |
|
| 328 | + private String getNewScopeQualName(String name, CompilationScope scopeType) { |
| 329 | + RootNodeCompiler parent = this.parent; |
| 330 | + if (parent != null && parent.parent != null) { |
| 331 | + if (parent.scopeType == TypeParams && parent.parent != null && parent.parent.parent != null) { |
| 332 | + parent = parent.parent; |
| 333 | + if (parent.parent != null && parent.parent.parent == null) { |
| 334 | + // if there are exactly two parents/ancestros, then return the name |
| 335 | + return name; |
| 336 | + } |
| 337 | + } |
| 338 | + if (!(EnumSet.of(CompilationScope.Function, AsyncFunction, Class).contains(scopeType) && |
| 339 | + parent.scope.getUseOfName(ScopeEnvironment.mangle(parent.privateName, name)).contains(Scope.DefUse.GlobalExplicit))) { |
| 340 | + String base; |
| 341 | + if (EnumSet.of(CompilationScope.Function, AsyncFunction, CompilationScope.Lambda).contains(parent.scopeType)) { |
| 342 | + base = parent.qualName + ".<locals>"; |
| 343 | + } else { |
| 344 | + base = parent.qualName; |
| 345 | + } |
| 346 | + return base + "." + name; |
| 347 | + } |
| 348 | + } |
| 349 | + return name; |
| 350 | + } |
| 351 | + |
322 | 352 | private BytecodeDSLCompilerResult compileRootNode(String name, ArgumentInfo argumentInfo, SourceRange sourceRange, BytecodeParser<Builder> parser) { |
| 353 | + qualName = getNewScopeQualName(name, scopeType); |
| 354 | + |
323 | 355 | BytecodeRootNodes<PBytecodeDSLRootNode> nodes = PBytecodeDSLRootNodeGen.create(ctx.language, BytecodeConfig.WITH_SOURCE, parser); |
324 | 356 | List<PBytecodeDSLRootNode> nodeList = nodes.getNodes(); |
325 | 357 | assert nodeList.size() == 1; |
@@ -358,7 +390,7 @@ private BytecodeDSLCompilerResult compileRootNode(String name, ArgumentInfo argu |
358 | 390 | } |
359 | 391 | } |
360 | 392 |
|
361 | | - BytecodeDSLCodeUnit codeUnit = new BytecodeDSLCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(ctx.getQualifiedName(scope)), |
| 393 | + BytecodeDSLCodeUnit codeUnit = new BytecodeDSLCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(qualName), |
362 | 394 | argumentInfo.argCount, argumentInfo.kwOnlyArgCount, argumentInfo.positionalOnlyArgCount, |
363 | 395 | flags, orderedTruffleStringArray(names), |
364 | 396 | orderedTruffleStringArray(varnames), |
@@ -893,7 +925,7 @@ public BytecodeDSLCompilerResult compileClassDefBody(StmtTy.ClassDef node) { |
893 | 925 | endStoreLocal("__module__", b); |
894 | 926 |
|
895 | 927 | beginStoreLocal("__qualname__", b); |
896 | | - emitPythonConstant(toTruffleStringUncached(ctx.getQualifiedName(scope)), b); |
| 928 | + emitPythonConstant(toTruffleStringUncached(this.qualName), b); |
897 | 929 | endStoreLocal("__qualname__", b); |
898 | 930 |
|
899 | 931 | if (node.isGeneric()) { |
@@ -1108,7 +1140,7 @@ public BytecodeDSLCompilerResult visit(ExprTy.SetComp node) { |
1108 | 1140 |
|
1109 | 1141 | @Override |
1110 | 1142 | public BytecodeDSLCompilerResult visit(ExprTy.GeneratorExp node) { |
1111 | | - return buildComprehensionCodeUnit(node, node.generators, "<generator>", |
| 1143 | + return buildComprehensionCodeUnit(node, node.generators, "<genexpr>", |
1112 | 1144 | null, |
1113 | 1145 | (statementCompiler, collection) -> emitYield((statementCompiler_) -> node.element.accept(statementCompiler_), statementCompiler)); |
1114 | 1146 | } |
@@ -2101,7 +2133,7 @@ public Void visit(ExprTy.GeneratorExp node) { |
2101 | 2133 | boolean newStatement = beginSourceSection(node, b); |
2102 | 2134 |
|
2103 | 2135 | b.beginCallUnaryMethod(); |
2104 | | - emitMakeFunction(node, "<generator>", COMPREHENSION_ARGS); |
| 2136 | + emitMakeFunction(node, "<genexpr>", COMPREHENSION_ARGS); |
2105 | 2137 | node.generators[0].iter.accept(this); |
2106 | 2138 | b.endCallUnaryMethod(); |
2107 | 2139 |
|
@@ -3672,7 +3704,7 @@ private void emitMakeFunction(BytecodeDSLCodeUnit codeUnit, Object scopeKey, Str |
3672 | 3704 | ArgumentsTy argsForDefaults, List<ParamAnnotation> annotations) { |
3673 | 3705 | TruffleString functionName = toTruffleStringUncached(name); |
3674 | 3706 | Scope targetScope = ctx.scopeEnvironment.lookupScope(scopeKey); |
3675 | | - TruffleString qualifiedName = toTruffleStringUncached(ctx.getQualifiedName(targetScope)); |
| 3707 | + TruffleString qualifiedName = codeUnit.qualname; |
3676 | 3708 |
|
3677 | 3709 | // Register these in the Python constants list. |
3678 | 3710 | addConstant(qualifiedName); |
|
0 commit comments