implements PR-08.2

This commit is contained in:
2026-03-24 13:42:32 +00:00
parent 40188b67ab
commit 0490f64870
11 changed files with 520 additions and 23 deletions
@@ -19,6 +19,7 @@ import p.studio.compiler.source.identifiers.CallableShapeId;
import p.studio.compiler.source.identifiers.CallableId;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.IntrinsicId;
import p.studio.compiler.source.identifiers.ModuleId;
import p.studio.compiler.source.identifiers.NameId;
import p.studio.compiler.source.identifiers.TypeSurfaceId;
import p.studio.compiler.source.tables.CallableShapeTable;
@@ -26,6 +27,7 @@ import p.studio.compiler.source.tables.CallableSignatureRef;
import p.studio.compiler.source.tables.CallableTable;
import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.compiler.source.tables.IntrinsicTable;
import p.studio.compiler.source.tables.ModuleReference;
import p.studio.compiler.source.tables.NameTable;
import p.studio.compiler.source.tables.TypeSurfaceTable;
import p.studio.utilities.structures.ReadOnlyList;
@@ -71,7 +73,16 @@ public final class PbsFrontendCompiler {
? PbsParser.ParseMode.INTERFACE_MODULE
: PbsParser.ParseMode.ORDINARY;
final var ast = PbsParser.parse(tokens, fileId, diagnostics, parseMode);
final var irBackendFile = compileParsedFile(fileId, ast, diagnostics, sourceKind, "", hostAdmissionContext, nameTable);
final var irBackendFile = compileParsedFile(
fileId,
ast,
diagnostics,
sourceKind,
ModuleId.none(),
"",
ReadOnlyList.empty(),
hostAdmissionContext,
nameTable);
if (diagnostics.errorCount() > admissionBaseline) {
return IRBackendFile.empty(fileId);
}
@@ -99,7 +110,16 @@ public final class PbsFrontendCompiler {
final DiagnosticSink diagnostics,
final SourceKind sourceKind,
final HostAdmissionContext hostAdmissionContext) {
return compileParsedFile(fileId, ast, diagnostics, sourceKind, "", hostAdmissionContext, new NameTable());
return compileParsedFile(
fileId,
ast,
diagnostics,
sourceKind,
ModuleId.none(),
"",
ReadOnlyList.empty(),
hostAdmissionContext,
new NameTable());
}
public IRBackendFile compileParsedFile(
@@ -109,7 +129,16 @@ public final class PbsFrontendCompiler {
final SourceKind sourceKind,
final String moduleKey,
final HostAdmissionContext hostAdmissionContext) {
return compileParsedFile(fileId, ast, diagnostics, sourceKind, moduleKey, hostAdmissionContext, new NameTable());
return compileParsedFile(
fileId,
ast,
diagnostics,
sourceKind,
ModuleId.none(),
moduleKey,
ReadOnlyList.empty(),
hostAdmissionContext,
new NameTable());
}
public IRBackendFile compileParsedFile(
@@ -117,7 +146,9 @@ public final class PbsFrontendCompiler {
final PbsAst.File ast,
final DiagnosticSink diagnostics,
final SourceKind sourceKind,
final ModuleId moduleId,
final String moduleKey,
final ReadOnlyList<ModuleReference> modulePool,
final HostAdmissionContext hostAdmissionContext,
final NameTable nameTable) {
return compileParsedFile(
@@ -125,7 +156,9 @@ public final class PbsFrontendCompiler {
ast,
diagnostics,
sourceKind,
moduleId,
moduleKey,
modulePool,
hostAdmissionContext,
nameTable,
ReadOnlyList.empty(),
@@ -138,12 +171,16 @@ public final class PbsFrontendCompiler {
final PbsAst.File ast,
final DiagnosticSink diagnostics,
final SourceKind sourceKind,
final ModuleId moduleId,
final String moduleKey,
final ReadOnlyList<ModuleReference> modulePool,
final HostAdmissionContext hostAdmissionContext,
final NameTable nameTable,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final ReadOnlyList<ImportedCallableSurface> importedCallables,
final IRReservedMetadata importedReservedMetadata) {
final var effectiveModuleId = moduleId == null ? ModuleId.none() : moduleId;
final var effectiveModulePool = modulePool == null ? ReadOnlyList.<ModuleReference>empty() : modulePool;
final var effectiveNameTable = nameTable == null ? new NameTable() : nameTable;
final var effectiveSupplementalTopDecls = supplementalTopDecls == null
? ReadOnlyList.<PbsAst.TopDecl>empty()
@@ -187,6 +224,7 @@ public final class PbsFrontendCompiler {
fileId,
ast,
effectiveSupplementalTopDecls,
effectiveModuleId,
moduleKey,
effectiveReservedMetadata,
effectiveNameTable,
@@ -200,6 +238,7 @@ public final class PbsFrontendCompiler {
functions,
executableLowering.executableFunctions(),
reservedMetadata,
effectiveModulePool,
executableLowering.callableSignatures(),
executableLowering.intrinsicPool());
}
@@ -221,12 +260,14 @@ public final class PbsFrontendCompiler {
final FileId fileId,
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final ModuleId moduleId,
final String moduleKey,
final IRReservedMetadata reservedMetadata,
final NameTable nameTable,
final DiagnosticSink diagnostics,
final ReadOnlyList<ImportedCallableSurface> importedCallables) {
final var normalizedModuleKey = moduleKey == null ? "" : moduleKey;
final var normalizedModuleId = moduleId == null ? ModuleId.none() : moduleId;
final var hostByMethodName = new HashMap<NameId, List<IRReservedMetadata.HostMethodBinding>>();
for (final var hostBinding : reservedMetadata.hostMethodBindings()) {
hostByMethodName
@@ -318,6 +359,7 @@ public final class PbsFrontendCompiler {
final var declaredFn = declaredCallable.functionDecl();
final var callableShapeId = callableShapeId(declaredFn, typeSurfaceTable, callableShapeTable);
final var callableId = callableIdTable.register(
normalizedModuleId,
normalizedModuleKey,
declaredCallable.callableName(),
declaredFn.parameters().size(),
@@ -333,6 +375,7 @@ public final class PbsFrontendCompiler {
}
for (final var importedCallable : importedCallables) {
final var callableId = callableIdTable.register(
importedCallable.moduleId(),
importedCallable.moduleKey(),
importedCallable.callableName(),
importedCallable.arity(),
@@ -359,6 +402,7 @@ public final class PbsFrontendCompiler {
localSlotByNameId.put(nameTable.register(fn.parameters().get(paramIndex).name()), paramIndex);
}
final var loweringContext = new ExecutableLoweringContext(
normalizedModuleId,
normalizedModuleKey,
diagnostics,
nameTable,
@@ -401,6 +445,7 @@ public final class PbsFrontendCompiler {
final var end = safeToInt(fn.span().getEnd());
executableFunctions.add(new IRBackendExecutableFunction(
fileId,
normalizedModuleId,
normalizedModuleKey,
callable.callableName(),
functionCallableId,
@@ -921,6 +966,7 @@ public final class PbsFrontendCompiler {
final var calleeCallableId = callableCandidates.getFirst();
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
context.moduleId(),
context.moduleKey(),
calleeIdentity.primaryCallableDisplayName(),
calleeCallableId,
@@ -1292,6 +1338,7 @@ public final class PbsFrontendCompiler {
}
private static final class ExecutableLoweringContext {
private final ModuleId moduleId;
private final String moduleKey;
private final DiagnosticSink diagnostics;
private final NameTable nameTable;
@@ -1308,6 +1355,7 @@ public final class PbsFrontendCompiler {
private int nextLabelId = 0;
private ExecutableLoweringContext(
final ModuleId moduleId,
final String moduleKey,
final DiagnosticSink diagnostics,
final NameTable nameTable,
@@ -1319,6 +1367,7 @@ public final class PbsFrontendCompiler {
final Map<CallableId, Integer> returnSlotsByCallableId,
final IntrinsicTable intrinsicIdTable,
final Map<NameId, Integer> localSlotByNameId) {
this.moduleId = moduleId == null ? ModuleId.none() : moduleId;
this.moduleKey = moduleKey;
this.diagnostics = diagnostics;
this.nameTable = nameTable;
@@ -1332,6 +1381,10 @@ public final class PbsFrontendCompiler {
this.localSlotByNameId = localSlotByNameId == null ? Map.of() : localSlotByNameId;
}
private ModuleId moduleId() {
return moduleId;
}
private String moduleKey() {
return moduleKey;
}
@@ -1440,11 +1493,15 @@ public final class PbsFrontendCompiler {
}
public record ImportedCallableSurface(
ModuleId moduleId,
String moduleKey,
String callableName,
int arity,
int returnSlots,
String shapeSurface) {
public ImportedCallableSurface {
moduleId = moduleId == null ? ModuleId.none() : moduleId;
}
}
private static final class ExecutableLoweringAnalysisException extends RuntimeException {
@@ -159,6 +159,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
markModulesWithLinkingErrors(diagnostics, moduleIdByFile, failedModuleIds);
final var moduleDependencyGraph = buildModuleDependencyGraph(parsedSourceFiles, moduleTable);
final var importedSemanticContexts = buildImportedSemanticContexts(parsedSourceFiles, moduleTable);
final var canonicalModulePool = emitModulePool(moduleTable);
final var compiledSourceFiles = new ArrayList<CompiledSourceFile>(parsedSourceFiles.size());
for (final var parsedSource : parsedSourceFiles) {
@@ -175,7 +176,9 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
parsedSource.ast(),
diagnostics,
parsedSource.sourceKind(),
parsedSource.moduleId(),
renderModuleKey(moduleTable, parsedSource.moduleId()),
canonicalModulePool,
ctx.hostAdmissionContext(),
nameTable,
importedSemanticContext.supplementalTopDecls(),
@@ -431,6 +434,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
importedCallables,
importedCallableKeys,
new PbsFrontendCompiler.ImportedCallableSurface(
importedModuleId,
importedModuleKey,
localName + "." + method.name(),
method.parameters().size(),
@@ -444,6 +448,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
importedCallables,
importedCallableKeys,
new PbsFrontendCompiler.ImportedCallableSurface(
importedModuleId,
importedModuleKey,
localName,
functionDecl.parameters().size(),
@@ -793,6 +798,14 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
return moduleReference.project() + ":" + String.join("/", moduleReference.pathSegments().asList());
}
private ReadOnlyList<ModuleReference> emitModulePool(final ModuleTable moduleTable) {
final var pool = new ArrayList<ModuleReference>(moduleTable.size());
for (final var moduleId : moduleTable.identifiers()) {
pool.add(moduleTable.get(moduleId));
}
return ReadOnlyList.wrap(pool);
}
private static final class MutableModuleUnit {
private final ArrayList<PbsModuleVisibilityValidator.SourceFile> sources = new ArrayList<>();
private final ArrayList<PbsModuleVisibilityValidator.BarrelFile> barrels = new ArrayList<>();