implements PR-19.7 explicit backend globals and synthetic callable model

This commit is contained in:
2026-03-26 19:31:21 +00:00
parent 7fc38010a2
commit c61b9db786
8 changed files with 238 additions and 0 deletions
@@ -3,7 +3,11 @@ package p.studio.compiler.pbs;
import p.studio.compiler.messages.HostAdmissionContext;
import p.studio.compiler.models.IRBackendFile;
import p.studio.compiler.models.IRFunction;
import p.studio.compiler.models.IRGlobal;
import p.studio.compiler.models.IRReservedMetadata;
import p.studio.compiler.models.IRSyntheticCallableKind;
import p.studio.compiler.models.IRSyntheticFunction;
import p.studio.compiler.models.IRSyntheticOrigin;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
@@ -149,6 +153,12 @@ public final class PbsFrontendCompiler {
final ReadOnlyList<IRFunction> functions = sourceKind == SourceKind.SDK_INTERFACE
? ReadOnlyList.empty()
: lowerFunctions(fileId, ast);
final ReadOnlyList<IRSyntheticFunction> syntheticFunctions = sourceKind == SourceKind.SDK_INTERFACE
? ReadOnlyList.empty()
: lowerSyntheticFunctions(fileId, effectiveModuleId, ast);
final ReadOnlyList<IRGlobal> globals = sourceKind == SourceKind.SDK_INTERFACE
? ReadOnlyList.empty()
: lowerGlobals(fileId, effectiveModuleId, ast);
final var loweringErrorBaseline = diagnostics.errorCount();
final var executableLowering = executableLoweringService.lower(
fileId,
@@ -165,6 +175,8 @@ public final class PbsFrontendCompiler {
return new IRBackendFile(
fileId,
functions,
syntheticFunctions,
globals,
executableLowering.executableFunctions(),
reservedMetadata,
effectiveModulePool,
@@ -210,6 +222,78 @@ public final class PbsFrontendCompiler {
return ReadOnlyList.wrap(functions);
}
private ReadOnlyList<IRGlobal> lowerGlobals(
final FileId fileId,
final ModuleId moduleId,
final PbsAst.File ast) {
final var globals = new ArrayList<IRGlobal>();
for (final var topDecl : ast.topDecls()) {
if (!(topDecl instanceof PbsAst.GlobalDecl globalDecl)) {
continue;
}
globals.add(new IRGlobal(
fileId,
moduleId,
globalDecl.name(),
PbsReservedMetadataExtractor.typeSurfaceKey(globalDecl.explicitType()),
globalDecl.span()));
}
return ReadOnlyList.wrap(globals);
}
private ReadOnlyList<IRSyntheticFunction> lowerSyntheticFunctions(
final FileId fileId,
final ModuleId moduleId,
final PbsAst.File ast) {
final var syntheticFunctions = new ArrayList<IRSyntheticFunction>();
PbsAst.FunctionDecl initDecl = null;
PbsAst.FunctionDecl frameDecl = null;
PbsAst.GlobalDecl firstGlobalDecl = null;
for (final var topDecl : ast.topDecls()) {
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
if (functionDecl.lifecycleMarker() == PbsAst.LifecycleMarker.INIT && initDecl == null) {
initDecl = functionDecl;
}
if (functionDecl.lifecycleMarker() == PbsAst.LifecycleMarker.FRAME && frameDecl == null) {
frameDecl = functionDecl;
}
}
if (topDecl instanceof PbsAst.GlobalDecl globalDecl && firstGlobalDecl == null) {
firstGlobalDecl = globalDecl;
}
}
final var fileInitAnchorSpan = initDecl != null ? initDecl.span() : firstGlobalDecl == null ? null : firstGlobalDecl.span();
final var fileInitAnchorName = initDecl != null ? initDecl.name() : firstGlobalDecl == null ? "" : firstGlobalDecl.name();
if (fileInitAnchorSpan != null) {
syntheticFunctions.add(new IRSyntheticFunction(
moduleId,
"__pbs.file_init",
IRSyntheticCallableKind.FILE_INIT_FRAGMENT,
new IRSyntheticOrigin(fileId, fileInitAnchorName, fileInitAnchorSpan)));
syntheticFunctions.add(new IRSyntheticFunction(
moduleId,
"__pbs.module_init",
IRSyntheticCallableKind.MODULE_INIT,
new IRSyntheticOrigin(fileId, fileInitAnchorName, fileInitAnchorSpan)));
}
if (initDecl != null && frameDecl != null) {
syntheticFunctions.add(new IRSyntheticFunction(
moduleId,
"__pbs.project_init",
IRSyntheticCallableKind.PROJECT_INIT,
new IRSyntheticOrigin(fileId, initDecl.name(), initDecl.span())));
}
if (frameDecl != null) {
syntheticFunctions.add(new IRSyntheticFunction(
moduleId,
"__pbs.frame_wrapper",
IRSyntheticCallableKind.PUBLISHED_FRAME_WRAPPER,
new IRSyntheticOrigin(fileId, frameDecl.name(), frameDecl.span())));
}
return ReadOnlyList.wrap(syntheticFunctions);
}
private IRReservedMetadata mergeReservedMetadata(
final IRReservedMetadata primary,
final IRReservedMetadata imported) {
@@ -2,6 +2,7 @@ package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.messages.HostAdmissionContext;
import p.studio.compiler.models.IRSyntheticCallableKind;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.lexer.LexErrors;
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
@@ -40,6 +41,33 @@ class PbsFrontendCompilerTest {
assertEquals(1, functions.get(1).parameterCount());
}
@Test
void shouldExposeGlobalsAndSyntheticLifecycleArtifactsInBackendFile() {
final var source = """
declare global SCORE: int = 0;
[Init]
fn boot() -> void { return; }
[Frame]
fn frame() -> void { return; }
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(1), source, diagnostics);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(1, fileBackend.globals().size());
assertEquals("SCORE", fileBackend.globals().getFirst().name());
assertEquals(4, fileBackend.syntheticFunctions().size());
assertTrue(fileBackend.syntheticFunctions().stream().anyMatch(fn -> fn.kind() == IRSyntheticCallableKind.FILE_INIT_FRAGMENT));
assertTrue(fileBackend.syntheticFunctions().stream().anyMatch(fn -> fn.kind() == IRSyntheticCallableKind.MODULE_INIT));
assertTrue(fileBackend.syntheticFunctions().stream().anyMatch(fn -> fn.kind() == IRSyntheticCallableKind.PROJECT_INIT));
assertTrue(fileBackend.syntheticFunctions().stream().anyMatch(fn -> fn.kind() == IRSyntheticCallableKind.PUBLISHED_FRAME_WRAPPER));
assertTrue(fileBackend.syntheticFunctions().stream().allMatch(fn -> !fn.origin().anchorCallableName().isBlank()));
}
@Test
void shouldReportDuplicateFunctionNames() {
final var source = """