new test main with code directly (fixes)

This commit is contained in:
2026-03-24 13:42:32 +00:00
parent 14f9b5dd26
commit 5630f8f119
63 changed files with 1677 additions and 198 deletions
@@ -9,5 +9,6 @@ public class PBSDefinitions {
.languageId("pbs")
.allowedExtensions(ReadOnlySet.from("pbs", "barrel"))
.sourceRoots(ReadOnlySet.from("src"))
.entryPointCallableName("frame")
.build();
}
@@ -120,10 +120,43 @@ public final class PbsFrontendCompiler {
final String moduleKey,
final HostAdmissionContext hostAdmissionContext,
final NameTable nameTable) {
return compileParsedFile(
fileId,
ast,
diagnostics,
sourceKind,
moduleKey,
hostAdmissionContext,
nameTable,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
IRReservedMetadata.empty());
}
public IRBackendFile compileParsedFile(
final FileId fileId,
final PbsAst.File ast,
final DiagnosticSink diagnostics,
final SourceKind sourceKind,
final String moduleKey,
final HostAdmissionContext hostAdmissionContext,
final NameTable nameTable,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final ReadOnlyList<ImportedCallableSurface> importedCallables,
final IRReservedMetadata importedReservedMetadata) {
final var effectiveNameTable = nameTable == null ? new NameTable() : nameTable;
final var effectiveSupplementalTopDecls = supplementalTopDecls == null
? ReadOnlyList.<PbsAst.TopDecl>empty()
: supplementalTopDecls;
final var effectiveImportedCallables = importedCallables == null
? ReadOnlyList.<ImportedCallableSurface>empty()
: importedCallables;
final var effectiveImportedReservedMetadata = importedReservedMetadata == null
? IRReservedMetadata.empty()
: importedReservedMetadata;
final var semanticsErrorBaseline = diagnostics.errorCount();
new PbsDeclarationSemanticsValidator(effectiveNameTable).validate(ast, sourceKind, diagnostics);
flowSemanticsValidator.validate(ast, diagnostics);
flowSemanticsValidator.validate(ast, effectiveSupplementalTopDecls, diagnostics);
if (diagnostics.errorCount() > semanticsErrorBaseline) {
return IRBackendFile.empty(fileId);
}
@@ -142,14 +175,22 @@ public final class PbsFrontendCompiler {
extractedReservedMetadata.builtinTypeSurfaces(),
extractedReservedMetadata.builtinConstSurfaces(),
requiredCapabilities);
final var effectiveReservedMetadata = mergeReservedMetadata(
reservedMetadata,
effectiveImportedReservedMetadata);
final ReadOnlyList<IRFunction> functions = sourceKind == SourceKind.SDK_INTERFACE
? ReadOnlyList.empty()
: lowerFunctions(fileId, ast);
final var loweringErrorBaseline = diagnostics.errorCount();
final var executableLowering = sourceKind == SourceKind.SDK_INTERFACE
? new ExecutableLoweringResult(ReadOnlyList.empty(), ReadOnlyList.empty(), ReadOnlyList.empty())
: lowerExecutableFunctions(fileId, ast, moduleKey, reservedMetadata, effectiveNameTable, diagnostics);
final var executableLowering = lowerExecutableFunctions(
fileId,
ast,
moduleKey,
effectiveReservedMetadata,
effectiveNameTable,
diagnostics,
effectiveImportedCallables);
if (diagnostics.errorCount() > loweringErrorBaseline) {
return IRBackendFile.empty(fileId);
}
@@ -181,7 +222,8 @@ public final class PbsFrontendCompiler {
final String moduleKey,
final IRReservedMetadata reservedMetadata,
final NameTable nameTable,
final DiagnosticSink diagnostics) {
final DiagnosticSink diagnostics,
final ReadOnlyList<ImportedCallableSurface> importedCallables) {
final var normalizedModuleKey = moduleKey == null ? "" : moduleKey;
final var hostByMethodName = new HashMap<NameId, List<IRReservedMetadata.HostMethodBinding>>();
for (final var hostBinding : reservedMetadata.hostMethodBindings()) {
@@ -199,31 +241,49 @@ public final class PbsFrontendCompiler {
}
final var callableIdTable = new CallableTable();
final var callableIdsByNameAndArity = new HashMap<CallableResolutionKey, List<CallableId>>();
final var callableIdByDeclaration = new HashMap<PbsAst.FunctionDecl, CallableId>();
final var callableIdByDeclaration = new HashMap<LowerableCallable, CallableId>();
final var returnSlotsByCallableId = new HashMap<CallableId, Integer>();
final var intrinsicIdTable = new IntrinsicTable();
final var typeSurfaceTable = new TypeSurfaceTable();
final var callableShapeTable = new CallableShapeTable();
for (final var declaredFn : ast.functions()) {
final var localCallables = collectLocalLowerableCallables(ast);
for (final var declaredCallable : localCallables) {
final var declaredFn = declaredCallable.functionDecl();
final var callableShapeId = callableShapeId(declaredFn, typeSurfaceTable, callableShapeTable);
final var callableId = callableIdTable.register(
normalizedModuleKey,
declaredFn.name(),
declaredCallable.callableName(),
declaredFn.parameters().size(),
callableShapeSurface(callableShapeId, typeSurfaceTable, callableShapeTable));
callableIdByDeclaration.put(declaredFn, callableId);
callableIdByDeclaration.put(declaredCallable, callableId);
callableIdsByNameAndArity
.computeIfAbsent(
new CallableResolutionKey(nameTable.register(declaredFn.name()), declaredFn.parameters().size()),
new CallableResolutionKey(nameTable.register(declaredCallable.callableName()), declaredFn.parameters().size()),
ignored -> new ArrayList<>())
.add(callableId);
final var retSlots = returnSlotsFor(declaredFn);
returnSlotsByCallableId.put(callableId, retSlots);
}
for (final var importedCallable : importedCallables) {
final var callableId = callableIdTable.register(
importedCallable.moduleKey(),
importedCallable.callableName(),
importedCallable.arity(),
importedCallable.shapeSurface());
callableIdsByNameAndArity
.computeIfAbsent(
new CallableResolutionKey(
nameTable.register(importedCallable.callableName()),
importedCallable.arity()),
ignored -> new ArrayList<>())
.add(callableId);
returnSlotsByCallableId.put(callableId, importedCallable.returnSlots());
}
final var executableFunctions = new ArrayList<IRBackendExecutableFunction>(ast.functions().size());
for (final var fn : ast.functions()) {
final var functionCallableId = callableIdByDeclaration.get(fn);
final var executableFunctions = new ArrayList<IRBackendExecutableFunction>(localCallables.size());
for (final var callable : localCallables) {
final var fn = callable.functionDecl();
final var functionCallableId = callableIdByDeclaration.get(callable);
if (functionCallableId == null) {
continue;
}
@@ -268,7 +328,7 @@ public final class PbsFrontendCompiler {
executableFunctions.add(new IRBackendExecutableFunction(
fileId,
normalizedModuleKey,
fn.name(),
callable.callableName(),
functionCallableId,
start,
end,
@@ -300,6 +360,48 @@ public final class PbsFrontendCompiler {
};
}
private ReadOnlyList<LowerableCallable> collectLocalLowerableCallables(final PbsAst.File ast) {
final var callables = new ArrayList<LowerableCallable>();
for (final var functionDecl : ast.functions()) {
callables.add(new LowerableCallable(functionDecl.name(), functionDecl));
}
for (final var topDecl : ast.topDecls()) {
if (!(topDecl instanceof PbsAst.ServiceDecl serviceDecl)) {
continue;
}
for (final var method : serviceDecl.methods()) {
callables.add(new LowerableCallable(serviceDecl.name() + "." + method.name(), method));
}
}
return ReadOnlyList.wrap(callables);
}
private IRReservedMetadata mergeReservedMetadata(
final IRReservedMetadata primary,
final IRReservedMetadata imported) {
final var hostBindings = new ArrayList<IRReservedMetadata.HostMethodBinding>();
hostBindings.addAll(primary.hostMethodBindings().asList());
hostBindings.addAll(imported.hostMethodBindings().asList());
final var builtinTypeSurfaces = new ArrayList<IRReservedMetadata.BuiltinTypeSurface>();
builtinTypeSurfaces.addAll(primary.builtinTypeSurfaces().asList());
builtinTypeSurfaces.addAll(imported.builtinTypeSurfaces().asList());
final var builtinConstSurfaces = new ArrayList<IRReservedMetadata.BuiltinConstSurface>();
builtinConstSurfaces.addAll(primary.builtinConstSurfaces().asList());
builtinConstSurfaces.addAll(imported.builtinConstSurfaces().asList());
final var requiredCapabilities = new HashSet<String>();
requiredCapabilities.addAll(primary.requiredCapabilities().asList());
requiredCapabilities.addAll(imported.requiredCapabilities().asList());
return new IRReservedMetadata(
ReadOnlyList.wrap(hostBindings),
ReadOnlyList.wrap(builtinTypeSurfaces),
ReadOnlyList.wrap(builtinConstSurfaces),
ReadOnlyList.wrap(requiredCapabilities.stream().toList()));
}
private CallableShapeId callableShapeId(
final PbsAst.FunctionDecl functionDecl,
final TypeSurfaceTable typeSurfaceTable,
@@ -315,6 +417,13 @@ public final class PbsFrontendCompiler {
return callableShapeTable.register(ReadOnlyList.wrap(parameterSurfaceIds), outputSurfaceId);
}
public String callableShapeSurfaceOf(final PbsAst.FunctionDecl functionDecl) {
final var typeSurfaceTable = new TypeSurfaceTable();
final var callableShapeTable = new CallableShapeTable();
final var callableShapeId = callableShapeId(functionDecl, typeSurfaceTable, callableShapeTable);
return callableShapeSurface(callableShapeId, typeSurfaceTable, callableShapeTable);
}
private String callableShapeSurface(
final CallableShapeId callableShapeId,
final TypeSurfaceTable typeSurfaceTable,
@@ -600,10 +709,26 @@ public final class PbsFrontendCompiler {
reportUnsupportedLowering("executable lowering requires resolvable callee identity", callExpr.span(), context);
return;
}
final var callableCandidates = context.callableIdsByNameAndArity().get(
new CallableResolutionKey(calleeIdentity.nameId(), callExpr.arguments().size()));
final var hostCandidates = context.hostByMethodName().getOrDefault(calleeIdentity.nameId(), List.of());
final var intrinsicCandidates = context.intrinsicByMethodName().getOrDefault(calleeIdentity.nameId(), List.of());
final var callableCandidates = new ArrayList<CallableId>();
final var primaryCallableCandidates = context.callableIdsByNameAndArity().get(
new CallableResolutionKey(calleeIdentity.primaryCallableNameId(), callExpr.arguments().size()));
if (primaryCallableCandidates != null) {
callableCandidates.addAll(primaryCallableCandidates);
}
if (calleeIdentity.secondaryCallableNameId() != null) {
final var secondaryCallableCandidates = context.callableIdsByNameAndArity().get(
new CallableResolutionKey(calleeIdentity.secondaryCallableNameId(), callExpr.arguments().size()));
if (secondaryCallableCandidates != null) {
for (final var candidate : secondaryCallableCandidates) {
if (!callableCandidates.contains(candidate)) {
callableCandidates.add(candidate);
}
}
}
}
final var hostCandidates = context.hostByMethodName().getOrDefault(calleeIdentity.memberNameId(), List.of());
final var intrinsicCandidates = context.intrinsicByMethodName().getOrDefault(calleeIdentity.memberNameId(), List.of());
var categoryCount = 0;
if (callableCandidates != null && !callableCandidates.isEmpty()) {
@@ -651,6 +776,7 @@ public final class PbsFrontendCompiler {
return;
}
final var intrinsic = intrinsicCandidates.getFirst();
final var effectiveArgSlots = intrinsic.argSlots() + implicitReceiverArgSlots(callExpr.callee());
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
"",
@@ -660,13 +786,13 @@ public final class PbsFrontendCompiler {
intrinsic.canonicalName(),
intrinsic.canonicalVersion(),
context.intrinsicIdTable().register(intrinsic.canonicalName(), intrinsic.canonicalVersion())),
intrinsic.argSlots(),
effectiveArgSlots,
intrinsic.retSlots(),
callExpr.span()));
return;
}
if (callableCandidates == null || callableCandidates.isEmpty()) {
if (callableCandidates.isEmpty()) {
reportUnsupportedLowering("executable lowering requires resolvable callable identity", callExpr.span(), context);
return;
}
@@ -678,7 +804,7 @@ public final class PbsFrontendCompiler {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
context.moduleKey(),
calleeIdentity.displayName(),
calleeIdentity.primaryCallableDisplayName(),
calleeCallableId,
null,
null,
@@ -692,16 +818,71 @@ public final class PbsFrontendCompiler {
final NameTable nameTable) {
return switch (callee) {
case PbsAst.IdentifierExpr identifierExpr ->
new CalleeIdentity(nameTable.register(identifierExpr.name()), identifierExpr.name());
// Member access does not carry executable callsite identity in v1 lowering.
case PbsAst.MemberExpr ignored -> null;
new CalleeIdentity(
nameTable.register(identifierExpr.name()),
identifierExpr.name(),
null,
"",
nameTable.register(identifierExpr.name()));
case PbsAst.MemberExpr memberExpr -> {
final var memberName = memberExpr.memberName();
final var rootName = memberRootName(memberExpr.receiver());
final var qualified = rootName == null || rootName.isBlank()
? memberName
: rootName + "." + memberName;
final NameId secondaryNameId;
final String secondaryDisplayName;
if (qualified.equals(memberName)) {
secondaryNameId = null;
secondaryDisplayName = "";
} else {
secondaryNameId = nameTable.register(memberName);
secondaryDisplayName = memberName;
}
yield new CalleeIdentity(
nameTable.register(qualified),
qualified,
secondaryNameId,
secondaryDisplayName,
nameTable.register(memberName));
}
case PbsAst.BindExpr bindExpr ->
new CalleeIdentity(nameTable.register(bindExpr.functionName()), bindExpr.functionName());
new CalleeIdentity(
nameTable.register(bindExpr.functionName()),
bindExpr.functionName(),
null,
"",
nameTable.register(bindExpr.functionName()));
case PbsAst.GroupExpr groupExpr -> resolveCalleeIdentity(groupExpr.expression(), nameTable);
default -> null;
};
}
private String memberRootName(final PbsAst.Expression expression) {
return switch (expression) {
case PbsAst.IdentifierExpr identifierExpr -> identifierExpr.name();
case PbsAst.MemberExpr memberExpr -> memberRootName(memberExpr.receiver());
case PbsAst.CallExpr callExpr -> memberRootName(callExpr.callee());
case PbsAst.GroupExpr groupExpr -> memberRootName(groupExpr.expression());
default -> null;
};
}
private int implicitReceiverArgSlots(final PbsAst.Expression callee) {
if (!(callee instanceof PbsAst.MemberExpr memberExpr)) {
return 0;
}
return receiverProducesRuntimeValue(memberExpr.receiver()) ? 1 : 0;
}
private boolean receiverProducesRuntimeValue(final PbsAst.Expression receiver) {
return switch (receiver) {
case PbsAst.CallExpr ignored -> true;
case PbsAst.GroupExpr groupExpr -> receiverProducesRuntimeValue(groupExpr.expression());
default -> false;
};
}
private void emitJump(
final IRBackendExecutableFunction.InstructionKind jumpKind,
final String targetLabel,
@@ -785,7 +966,8 @@ public final class PbsFrontendCompiler {
switch (instruction.kind()) {
case CALL_FUNC -> outHeight += instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
case CALL_HOST -> outHeight += instruction.hostCall() == null ? 0 : instruction.hostCall().retSlots();
case CALL_INTRINSIC, HALT, LABEL, JMP, RET -> {
case CALL_INTRINSIC -> outHeight += instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
case HALT, LABEL, JMP, RET -> {
}
case JMP_IF_TRUE, JMP_IF_FALSE -> {
if (outHeight <= 0) {
@@ -947,14 +1129,30 @@ public final class PbsFrontendCompiler {
ReadOnlyList<IntrinsicReference> intrinsicPool) {
}
private record LowerableCallable(
String callableName,
PbsAst.FunctionDecl functionDecl) {
}
private record CallableResolutionKey(
NameId callableNameId,
int arity) {
}
private record CalleeIdentity(
NameId nameId,
String displayName) {
NameId primaryCallableNameId,
String primaryCallableDisplayName,
NameId secondaryCallableNameId,
String secondaryCallableDisplayName,
NameId memberNameId) {
}
public record ImportedCallableSurface(
String moduleKey,
String callableName,
int arity,
int returnSlots,
String shapeSurface) {
}
private static final class ExecutableLoweringAnalysisException extends RuntimeException {
@@ -119,9 +119,10 @@ public final class PbsReservedMetadataExtractor {
continue;
}
final var intrinsicMetadata = intrinsicAttribute.get();
final var intrinsicName = stringArgument(intrinsicMetadata, "name").orElse(signature.name());
intrinsics.add(new IRReservedMetadata.IntrinsicSurface(
signature.name(),
stringArgument(intrinsicMetadata, "name").orElse(signature.name()),
canonicalIntrinsicName(canonicalTypeName, intrinsicName),
longArgument(intrinsicMetadata, "version").orElse(canonicalVersion),
signature.parameters().size(),
switch (signature.returnKind()) {
@@ -215,6 +216,23 @@ public final class PbsReservedMetadataExtractor {
return rawValue;
}
private String canonicalIntrinsicName(
final String canonicalTypeName,
final String intrinsicName) {
final var normalizedTypeName = canonicalTypeName == null ? "" : canonicalTypeName.trim();
final var normalizedIntrinsicName = intrinsicName == null ? "" : intrinsicName.trim();
if (normalizedIntrinsicName.isBlank()) {
return normalizedTypeName;
}
if (normalizedIntrinsicName.contains(".")) {
return normalizedIntrinsicName;
}
if (normalizedTypeName.isBlank()) {
return normalizedIntrinsicName;
}
return normalizedTypeName + "." + normalizedIntrinsicName;
}
private String typeSurfaceKey(final PbsAst.TypeRef typeRef) {
if (typeRef == null) {
return "unit";
@@ -285,7 +285,7 @@ final class PbsExprParser {
}
if (cursor.match(PbsTokenKind.DOT)) {
final var member = consume(PbsTokenKind.IDENTIFIER, "Expected member name after '.'");
final var member = consumeMemberName("Expected member name after '.'");
expression = new PbsAst.MemberExpr(
expression,
member.lexeme(),
@@ -631,6 +631,18 @@ final class PbsExprParser {
return token;
}
private PbsToken consumeMemberName(final String message) {
if (cursor.check(PbsTokenKind.IDENTIFIER) || cursor.check(PbsTokenKind.ERROR)) {
return cursor.advance();
}
final var token = cursor.peek();
report(token, ParseErrors.E_PARSE_EXPECTED_TOKEN, message + ", found " + token.kind());
if (!cursor.isAtEnd()) {
return cursor.advance();
}
return token;
}
private Span span(final long start, final long end) {
return new Span(fileId, start, end);
}
@@ -730,7 +730,7 @@ public final class PbsParser {
}
private PbsAst.FunctionDecl parseFunctionLike(final PbsToken fnToken) {
final var name = consume(PbsTokenKind.IDENTIFIER, "Expected function name");
final var name = consumeCallableName("Expected function name");
consume(PbsTokenKind.LEFT_PAREN, "Expected '(' after function name");
final var parameters = parseParametersUntilRightParen();
final var rightParen = consume(PbsTokenKind.RIGHT_PAREN, "Expected ')' after parameter list");
@@ -752,7 +752,7 @@ public final class PbsParser {
final PbsToken fnToken,
final boolean requireSemicolon,
final ReadOnlyList<PbsAst.Attribute> attributes) {
final var name = consume(PbsTokenKind.IDENTIFIER, "Expected function name");
final var name = consumeCallableName("Expected function name");
consume(PbsTokenKind.LEFT_PAREN, "Expected '(' after function name");
final var parameters = parseParametersUntilRightParen();
final var rightParen = consume(PbsTokenKind.RIGHT_PAREN, "Expected ')' after parameter list");
@@ -774,6 +774,18 @@ public final class PbsParser {
span(fnToken.start(), end));
}
private PbsToken consumeCallableName(final String message) {
if (cursor.check(PbsTokenKind.IDENTIFIER) || cursor.check(PbsTokenKind.ERROR)) {
return cursor.advance();
}
final var token = cursor.peek();
report(token, ParseErrors.E_PARSE_EXPECTED_TOKEN, message + ", found " + token.kind());
if (!cursor.isAtEnd()) {
return cursor.advance();
}
return token;
}
/**
* Parses `declare callback` declaration.
*/
@@ -88,12 +88,6 @@ public final class PbsDeclarationSemanticsValidator {
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
binder.registerType(serviceDecl.name(), serviceDecl.span(), "service");
binder.registerValue(serviceDecl.name(), serviceDecl.span(), "service singleton");
if (interfaceModule) {
reportInterfaceNonDeclarativeDecl(
serviceDecl.span(),
"Interface modules must not declare executable service bodies",
diagnostics);
}
validateServiceDeclaration(serviceDecl, binder, rules);
continue;
}
@@ -21,7 +21,14 @@ final class PbsFlowBodyAnalyzer {
}
public void validate(final PbsAst.File ast, final DiagnosticSink diagnostics) {
final var model = Model.from(ast);
validate(ast, ReadOnlyList.empty(), diagnostics);
}
public void validate(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final DiagnosticSink diagnostics) {
final var model = Model.from(ast, supplementalTopDecls);
for (final var topDecl : ast.topDecls()) {
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
@@ -169,108 +169,208 @@ final class PbsFlowSemanticSupport {
final Map<String, Set<String>> errors = new HashMap<>();
final Map<String, TypeView> constTypes = new HashMap<>();
final Map<String, TypeView> serviceSingletons = new HashMap<>();
final Set<String> knownStructNames = new HashSet<>();
final Set<String> knownServiceNames = new HashSet<>();
final Set<String> knownContractNames = new HashSet<>();
final Set<String> knownEnumNames = new HashSet<>();
final Set<String> knownErrorNames = new HashSet<>();
final Set<String> knownCallbackNames = new HashSet<>();
static Model from(final PbsAst.File ast) {
return from(ast, ReadOnlyList.empty());
}
static Model from(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls) {
final var model = new Model();
for (final var topDecl : ast.topDecls()) {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
final var fields = new HashMap<String, StructFieldInfo>();
for (final var field : structDecl.fields()) {
fields.put(
field.name(),
new StructFieldInfo(
model.typeFrom(field.typeRef()),
field.isPublic(),
field.isMutable()));
}
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var method : structDecl.methods()) {
methods.computeIfAbsent(method.name(), ignored -> new ArrayList<>())
.add(model.callableFrom(
method.name(),
method.parameters(),
method.returnKind(),
method.returnType(),
method.resultErrorType(),
method.span()));
}
model.structs.put(structDecl.name(), new StructInfo(fields, methods));
continue;
}
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var method : serviceDecl.methods()) {
methods.computeIfAbsent(method.name(), ignored -> new ArrayList<>())
.add(model.callableFrom(
method.name(),
method.parameters(),
method.returnKind(),
method.returnType(),
method.resultErrorType(),
method.span()));
}
model.services.put(serviceDecl.name(), new ServiceInfo(methods));
model.serviceSingletons.put(serviceDecl.name(), TypeView.service(serviceDecl.name()));
continue;
}
if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var signature : contractDecl.signatures()) {
methods.computeIfAbsent(signature.name(), ignored -> new ArrayList<>())
.add(model.callableFrom(
signature.name(),
signature.parameters(),
signature.returnKind(),
signature.returnType(),
signature.resultErrorType(),
signature.span()));
}
model.contracts.put(contractDecl.name(), new ContractInfo(methods));
continue;
}
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
model.topLevelCallables.computeIfAbsent(functionDecl.name(), ignored -> new ArrayList<>())
.add(model.callableFrom(
functionDecl.name(),
functionDecl.parameters(),
functionDecl.returnKind(),
functionDecl.returnType(),
functionDecl.resultErrorType(),
functionDecl.span()));
continue;
}
if (topDecl instanceof PbsAst.CallbackDecl(
String name, ReadOnlyList<PbsAst.Parameter> parameters, PbsAst.ReturnKind returnKind,
PbsAst.TypeRef returnType, PbsAst.TypeRef resultErrorType, Span span
)) {
final var symbol = model.callableFrom(
name,
parameters,
returnKind,
returnType,
resultErrorType,
span);
model.callbacks.put(name, new CallbackSignature(symbol.inputTypes(), symbol.outputType()));
continue;
}
if (topDecl instanceof PbsAst.EnumDecl enumDecl) {
final var cases = new HashSet<String>();
for (final var enumCase : enumDecl.cases()) {
cases.add(enumCase.name());
}
model.enums.put(enumDecl.name(), cases);
continue;
}
if (topDecl instanceof PbsAst.ErrorDecl errorDecl) {
model.errors.put(errorDecl.name(), new HashSet<>(errorDecl.cases().asList()));
continue;
}
if (topDecl instanceof PbsAst.ConstDecl constDecl && constDecl.explicitType() != null) {
model.constTypes.put(constDecl.name(), model.typeFrom(constDecl.explicitType()));
}
model.registerKnownTopDecl(topDecl);
}
for (final var topDecl : supplementalTopDecls) {
model.registerKnownTopDecl(topDecl);
}
for (final var topDecl : ast.topDecls()) {
model.ingestTopDecl(topDecl);
}
for (final var topDecl : supplementalTopDecls) {
model.ingestTopDecl(topDecl);
}
return model;
}
private void registerKnownTopDecl(final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
knownStructNames.add(structDecl.name());
return;
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
knownStructNames.add(builtinTypeDecl.name());
return;
}
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
knownServiceNames.add(serviceDecl.name());
return;
}
if (topDecl instanceof PbsAst.HostDecl hostDecl) {
knownServiceNames.add(hostDecl.name());
return;
}
if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
knownContractNames.add(contractDecl.name());
return;
}
if (topDecl instanceof PbsAst.EnumDecl enumDecl) {
knownEnumNames.add(enumDecl.name());
return;
}
if (topDecl instanceof PbsAst.ErrorDecl errorDecl) {
knownErrorNames.add(errorDecl.name());
return;
}
if (topDecl instanceof PbsAst.CallbackDecl callbackDecl) {
knownCallbackNames.add(callbackDecl.name());
}
}
private void ingestTopDecl(final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
final var fields = new HashMap<String, StructFieldInfo>();
for (final var field : structDecl.fields()) {
fields.put(
field.name(),
new StructFieldInfo(
typeFrom(field.typeRef()),
field.isPublic(),
field.isMutable()));
}
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var method : structDecl.methods()) {
methods.computeIfAbsent(method.name(), ignored -> new ArrayList<>())
.add(callableFrom(
method.name(),
method.parameters(),
method.returnKind(),
method.returnType(),
method.resultErrorType(),
method.span()));
}
structs.put(structDecl.name(), new StructInfo(fields, methods));
return;
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
final var fields = new HashMap<String, StructFieldInfo>();
for (final var field : builtinTypeDecl.fields()) {
fields.put(
field.name(),
new StructFieldInfo(
typeFrom(field.typeRef()),
true,
false));
}
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var signature : builtinTypeDecl.signatures()) {
methods.computeIfAbsent(signature.name(), ignored -> new ArrayList<>())
.add(callableFrom(
signature.name(),
signature.parameters(),
signature.returnKind(),
signature.returnType(),
signature.resultErrorType(),
signature.span()));
}
structs.put(builtinTypeDecl.name(), new StructInfo(fields, methods));
return;
}
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var method : serviceDecl.methods()) {
methods.computeIfAbsent(method.name(), ignored -> new ArrayList<>())
.add(callableFrom(
method.name(),
method.parameters(),
method.returnKind(),
method.returnType(),
method.resultErrorType(),
method.span()));
}
services.put(serviceDecl.name(), new ServiceInfo(methods));
serviceSingletons.put(serviceDecl.name(), TypeView.service(serviceDecl.name()));
return;
}
if (topDecl instanceof PbsAst.HostDecl hostDecl) {
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var signature : hostDecl.signatures()) {
methods.computeIfAbsent(signature.name(), ignored -> new ArrayList<>())
.add(callableFrom(
signature.name(),
signature.parameters(),
signature.returnKind(),
signature.returnType(),
signature.resultErrorType(),
signature.span()));
}
// Host owners are value singletons with callable members, same access shape as services.
services.put(hostDecl.name(), new ServiceInfo(methods));
serviceSingletons.put(hostDecl.name(), TypeView.service(hostDecl.name()));
return;
}
if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
final var methods = new HashMap<String, List<CallableSymbol>>();
for (final var signature : contractDecl.signatures()) {
methods.computeIfAbsent(signature.name(), ignored -> new ArrayList<>())
.add(callableFrom(
signature.name(),
signature.parameters(),
signature.returnKind(),
signature.returnType(),
signature.resultErrorType(),
signature.span()));
}
contracts.put(contractDecl.name(), new ContractInfo(methods));
return;
}
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
topLevelCallables.computeIfAbsent(functionDecl.name(), ignored -> new ArrayList<>())
.add(callableFrom(
functionDecl.name(),
functionDecl.parameters(),
functionDecl.returnKind(),
functionDecl.returnType(),
functionDecl.resultErrorType(),
functionDecl.span()));
return;
}
if (topDecl instanceof PbsAst.CallbackDecl(
String name, ReadOnlyList<PbsAst.Parameter> parameters, PbsAst.ReturnKind returnKind,
PbsAst.TypeRef returnType, PbsAst.TypeRef resultErrorType, Span span
)) {
final var symbol = callableFrom(
name,
parameters,
returnKind,
returnType,
resultErrorType,
span);
callbacks.put(name, new CallbackSignature(symbol.inputTypes(), symbol.outputType()));
return;
}
if (topDecl instanceof PbsAst.EnumDecl enumDecl) {
final var cases = new HashSet<String>();
for (final var enumCase : enumDecl.cases()) {
cases.add(enumCase.name());
}
enums.put(enumDecl.name(), cases);
return;
}
if (topDecl instanceof PbsAst.ErrorDecl errorDecl) {
errors.put(errorDecl.name(), new HashSet<>(errorDecl.cases().asList()));
return;
}
if (topDecl instanceof PbsAst.ConstDecl constDecl && constDecl.explicitType() != null) {
constTypes.put(constDecl.name(), typeFrom(constDecl.explicitType()));
}
}
private CallableSymbol callableFrom(
final String name,
@@ -324,25 +424,28 @@ final class PbsFlowSemanticSupport {
if ("str".equals(typeRef.name())) {
yield TypeView.str();
}
if (structs.containsKey(typeRef.name())) {
if (structs.containsKey(typeRef.name()) || knownStructNames.contains(typeRef.name())) {
yield TypeView.struct(typeRef.name());
}
if (services.containsKey(typeRef.name())) {
if (services.containsKey(typeRef.name()) || knownServiceNames.contains(typeRef.name())) {
yield TypeView.service(typeRef.name());
}
if (contracts.containsKey(typeRef.name())) {
if (contracts.containsKey(typeRef.name()) || knownContractNames.contains(typeRef.name())) {
yield TypeView.contract(typeRef.name());
}
if (enums.containsKey(typeRef.name())) {
if (enums.containsKey(typeRef.name()) || knownEnumNames.contains(typeRef.name())) {
yield TypeView.enumType(typeRef.name());
}
if (errors.containsKey(typeRef.name())) {
if (errors.containsKey(typeRef.name()) || knownErrorNames.contains(typeRef.name())) {
yield TypeView.error(typeRef.name());
}
final var callbackSignature = callbacks.get(typeRef.name());
if (callbackSignature != null) {
yield TypeView.callback(typeRef.name(), callbackSignature.inputTypes(), callbackSignature.outputType());
}
if (knownCallbackNames.contains(typeRef.name())) {
yield TypeView.callback(typeRef.name(), List.of(), TypeView.unknown());
}
yield TypeView.unknown();
}
case OPTIONAL -> TypeView.optional(typeFrom(typeRef.inner()));
@@ -2,11 +2,19 @@ package p.studio.compiler.pbs.semantics;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.utilities.structures.ReadOnlyList;
public final class PbsFlowSemanticsValidator {
private final PbsFlowBodyAnalyzer flowBodyAnalyzer = new PbsFlowBodyAnalyzer();
public void validate(final PbsAst.File ast, final DiagnosticSink diagnostics) {
flowBodyAnalyzer.validate(ast, diagnostics);
flowBodyAnalyzer.validate(ast, ReadOnlyList.empty(), diagnostics);
}
public void validate(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final DiagnosticSink diagnostics) {
flowBodyAnalyzer.validate(ast, supplementalTopDecls, diagnostics);
}
}
@@ -1,14 +1,17 @@
package p.studio.compiler.services;
import lombok.extern.slf4j.Slf4j;
import p.studio.compiler.PBSDefinitions;
import p.studio.compiler.messages.BuildingIssueSink;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.models.IRReservedMetadata;
import p.studio.compiler.models.ProjectDescriptor;
import p.studio.compiler.models.SourceHandle;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.metadata.PbsReservedMetadataExtractor;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.linking.PbsLinkErrors;
import p.studio.compiler.pbs.linking.PbsModuleVisibilityValidator;
@@ -42,6 +45,7 @@ import java.util.Set;
@Slf4j
public class PBSFrontendPhaseService implements FrontendPhaseService {
private final PbsFrontendCompiler frontendCompiler = new PbsFrontendCompiler();
private final PbsReservedMetadataExtractor reservedMetadataExtractor = new PbsReservedMetadataExtractor();
private final PbsModuleVisibilityValidator moduleVisibilityValidator = new PbsModuleVisibilityValidator();
private final StdlibEnvironmentResolver stdlibEnvironmentResolver;
private final InterfaceModuleLoader interfaceModuleLoader;
@@ -154,6 +158,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
moduleVisibilityValidator.validate(ReadOnlyList.wrap(modules), nameTable, diagnostics);
markModulesWithLinkingErrors(diagnostics, moduleIdByFile, failedModuleIds);
final var moduleDependencyGraph = buildModuleDependencyGraph(parsedSourceFiles, moduleTable);
final var importedSemanticContexts = buildImportedSemanticContexts(parsedSourceFiles, moduleTable);
final var compiledSourceFiles = new ArrayList<CompiledSourceFile>(parsedSourceFiles.size());
for (final var parsedSource : parsedSourceFiles) {
@@ -161,6 +166,9 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
if (blockedModuleIds.contains(parsedSource.moduleId())) {
continue;
}
final var importedSemanticContext = importedSemanticContexts.getOrDefault(
parsedSource.fileId(),
ImportedSemanticContext.empty());
final var compileErrorBaseline = diagnostics.errorCount();
final var irBackendFile = frontendCompiler.compileParsedFile(
parsedSource.fileId(),
@@ -169,7 +177,10 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
parsedSource.sourceKind(),
renderModuleKey(moduleTable, parsedSource.moduleId()),
ctx.hostAdmissionContext(),
nameTable);
nameTable,
importedSemanticContext.supplementalTopDecls(),
importedSemanticContext.importedCallables(),
importedSemanticContext.importedReservedMetadata());
if (diagnostics.errorCount() > compileErrorBaseline) {
failedModuleIds.add(parsedSource.moduleId());
}
@@ -183,6 +194,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
}
irBackendAggregator.merge(compiledSource.irBackendFile());
}
irBackendAggregator.entryPointCallableName(PBSDefinitions.PBS.getEntryPointCallableName());
final var irBackend = irBackendAggregator.emit();
logs.using(log).debug("PBS frontend lowered to IR BE:\n%s".formatted(irBackend));
@@ -358,6 +370,383 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
return dependenciesByModule;
}
private Map<FileId, ImportedSemanticContext> buildImportedSemanticContexts(
final ArrayList<ParsedSourceFile> parsedSourceFiles,
final ModuleTable moduleTable) {
final Map<ModuleId, ArrayList<ParsedSourceFile>> sourcesByModule = new HashMap<>();
for (final var parsedSource : parsedSourceFiles) {
sourcesByModule
.computeIfAbsent(parsedSource.moduleId(), ignored -> new ArrayList<>())
.add(parsedSource);
}
final Map<ModuleId, IRReservedMetadata> reservedMetadataByModule = new HashMap<>();
for (final var entry : sourcesByModule.entrySet()) {
var merged = IRReservedMetadata.empty();
for (final var source : entry.getValue()) {
final var extracted = reservedMetadataExtractor.extract(source.ast(), source.sourceKind());
merged = mergeReservedMetadata(merged, extracted);
}
reservedMetadataByModule.put(entry.getKey(), merged);
}
final Map<ModuleId, Map<String, ArrayList<PbsAst.TopDecl>>> topDeclsByNameByModule = new HashMap<>();
for (final var entry : sourcesByModule.entrySet()) {
topDeclsByNameByModule.put(entry.getKey(), indexTopDeclsByName(entry.getValue()));
}
final Map<FileId, ImportedSemanticContext> contexts = new HashMap<>();
for (final var parsedSource : parsedSourceFiles) {
final var supplementalTopDecls = new ArrayList<PbsAst.TopDecl>();
final var importedCallables = new ArrayList<PbsFrontendCompiler.ImportedCallableSurface>();
final var importedCallableKeys = new HashSet<String>();
final var supplementalKeys = new HashSet<String>();
var importedReservedMetadata = IRReservedMetadata.empty();
for (final var importDecl : parsedSource.ast().imports()) {
final var moduleRef = importDecl.moduleRef();
final var importedModuleId = moduleId(moduleTable, moduleRef.project(), moduleRef.pathSegments());
final var importedSources = sourcesByModule.get(importedModuleId);
if (importedSources == null || importedSources.isEmpty()) {
continue;
}
final var importedTopDeclsByName = topDeclsByNameByModule.get(importedModuleId);
if (importedTopDeclsByName == null) {
continue;
}
final var importedModuleKey = renderModuleKey(moduleTable, importedModuleId);
importedReservedMetadata = mergeReservedMetadata(
importedReservedMetadata,
reservedMetadataByModule.getOrDefault(importedModuleId, IRReservedMetadata.empty()));
for (final var importItem : importDecl.items()) {
final var importName = importItem.name();
final var localName = importItemLocalName(importItem);
final var directDecls = importedTopDeclsByName.getOrDefault(importName, new ArrayList<>());
for (final var topDecl : directDecls) {
appendSupplementalTopDecl(topDecl, supplementalTopDecls, supplementalKeys);
appendTypeDependencyDecls(topDecl, importedTopDeclsByName, supplementalTopDecls, supplementalKeys);
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
for (final var method : serviceDecl.methods()) {
appendImportedCallable(
importedCallables,
importedCallableKeys,
new PbsFrontendCompiler.ImportedCallableSurface(
importedModuleKey,
localName + "." + method.name(),
method.parameters().size(),
returnSlotsFor(method),
frontendCompiler.callableShapeSurfaceOf(method)));
}
continue;
}
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
appendImportedCallable(
importedCallables,
importedCallableKeys,
new PbsFrontendCompiler.ImportedCallableSurface(
importedModuleKey,
localName,
functionDecl.parameters().size(),
returnSlotsFor(functionDecl),
frontendCompiler.callableShapeSurfaceOf(functionDecl)));
}
}
}
}
contexts.put(parsedSource.fileId(), new ImportedSemanticContext(
ReadOnlyList.wrap(supplementalTopDecls),
ReadOnlyList.wrap(importedCallables),
importedReservedMetadata));
}
return contexts;
}
private Map<String, ArrayList<PbsAst.TopDecl>> indexTopDeclsByName(
final ArrayList<ParsedSourceFile> moduleSources) {
final Map<String, ArrayList<PbsAst.TopDecl>> index = new HashMap<>();
for (final var source : moduleSources) {
for (final var topDecl : source.ast().topDecls()) {
final var declName = topDeclName(topDecl);
if (declName == null || declName.isBlank()) {
continue;
}
index.computeIfAbsent(declName, ignored -> new ArrayList<>()).add(topDecl);
}
}
return index;
}
private void appendTypeDependencyDecls(
final PbsAst.TopDecl rootDecl,
final Map<String, ArrayList<PbsAst.TopDecl>> topDeclsByName,
final ArrayList<PbsAst.TopDecl> supplementalTopDecls,
final Set<String> supplementalKeys) {
final var pendingTypeNames = new ArrayDeque<String>();
final var visitedTypeNames = new HashSet<String>();
collectReferencedTypeNames(rootDecl, pendingTypeNames);
while (!pendingTypeNames.isEmpty()) {
final var typeName = pendingTypeNames.removeFirst();
if (!visitedTypeNames.add(typeName)) {
continue;
}
final var candidates = topDeclsByName.get(typeName);
if (candidates == null) {
continue;
}
for (final var candidate : candidates) {
if (!isTypeDecl(candidate)) {
continue;
}
appendSupplementalTopDecl(candidate, supplementalTopDecls, supplementalKeys);
collectReferencedTypeNames(candidate, pendingTypeNames);
}
}
}
private void collectReferencedTypeNames(
final PbsAst.TopDecl topDecl,
final ArrayDeque<String> sink) {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
for (final var field : structDecl.fields()) {
collectReferencedTypeNames(field.typeRef(), sink);
}
for (final var method : structDecl.methods()) {
collectReferencedTypeNames(method, sink);
}
for (final var ctor : structDecl.ctors()) {
for (final var parameter : ctor.parameters()) {
collectReferencedTypeNames(parameter.typeRef(), sink);
}
}
return;
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
for (final var field : builtinTypeDecl.fields()) {
collectReferencedTypeNames(field.typeRef(), sink);
}
for (final var signature : builtinTypeDecl.signatures()) {
collectReferencedTypeNames(signature, sink);
}
return;
}
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
for (final var method : serviceDecl.methods()) {
collectReferencedTypeNames(method, sink);
}
return;
}
if (topDecl instanceof PbsAst.HostDecl hostDecl) {
for (final var signature : hostDecl.signatures()) {
collectReferencedTypeNames(signature, sink);
}
return;
}
if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
for (final var signature : contractDecl.signatures()) {
collectReferencedTypeNames(signature, sink);
}
return;
}
if (topDecl instanceof PbsAst.CallbackDecl callbackDecl) {
collectReferencedTypeNames(callbackDecl.parameters(), callbackDecl.returnType(), callbackDecl.resultErrorType(), sink);
return;
}
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
collectReferencedTypeNames(functionDecl, sink);
return;
}
if (topDecl instanceof PbsAst.ConstDecl constDecl && constDecl.explicitType() != null) {
collectReferencedTypeNames(constDecl.explicitType(), sink);
}
}
private void collectReferencedTypeNames(
final PbsAst.FunctionDecl functionDecl,
final ArrayDeque<String> sink) {
collectReferencedTypeNames(
functionDecl.parameters(),
functionDecl.returnType(),
functionDecl.resultErrorType(),
sink);
}
private void collectReferencedTypeNames(
final PbsAst.FunctionSignature functionSignature,
final ArrayDeque<String> sink) {
collectReferencedTypeNames(
functionSignature.parameters(),
functionSignature.returnType(),
functionSignature.resultErrorType(),
sink);
}
private void collectReferencedTypeNames(
final ReadOnlyList<PbsAst.Parameter> parameters,
final PbsAst.TypeRef returnType,
final PbsAst.TypeRef resultErrorType,
final ArrayDeque<String> sink) {
for (final var parameter : parameters) {
collectReferencedTypeNames(parameter.typeRef(), sink);
}
collectReferencedTypeNames(returnType, sink);
collectReferencedTypeNames(resultErrorType, sink);
}
private void collectReferencedTypeNames(
final PbsAst.TypeRef typeRef,
final ArrayDeque<String> sink) {
if (typeRef == null) {
return;
}
switch (typeRef.kind()) {
case SIMPLE -> {
final var simpleName = typeRef.name();
if (!isPrimitiveTypeName(simpleName)) {
sink.addLast(simpleName);
}
}
case OPTIONAL, GROUP -> collectReferencedTypeNames(typeRef.inner(), sink);
case NAMED_TUPLE -> {
for (final var field : typeRef.fields()) {
collectReferencedTypeNames(field.typeRef(), sink);
}
}
case UNIT, SELF, ERROR -> {
}
}
}
private boolean isPrimitiveTypeName(final String typeName) {
return "int".equals(typeName)
|| "float".equals(typeName)
|| "bool".equals(typeName)
|| "str".equals(typeName)
|| "unit".equals(typeName);
}
private boolean isTypeDecl(final PbsAst.TopDecl topDecl) {
return topDecl instanceof PbsAst.StructDecl
|| topDecl instanceof PbsAst.BuiltinTypeDecl
|| topDecl instanceof PbsAst.ServiceDecl
|| topDecl instanceof PbsAst.HostDecl
|| topDecl instanceof PbsAst.ContractDecl
|| topDecl instanceof PbsAst.CallbackDecl
|| topDecl instanceof PbsAst.EnumDecl
|| topDecl instanceof PbsAst.ErrorDecl;
}
private void appendSupplementalTopDecl(
final PbsAst.TopDecl topDecl,
final ArrayList<PbsAst.TopDecl> supplementalTopDecls,
final Set<String> supplementalKeys) {
final var key = topDeclKey(topDecl);
if (key == null || key.isBlank()) {
return;
}
if (supplementalKeys.add(key)) {
supplementalTopDecls.add(topDecl);
}
}
private void appendImportedCallable(
final ArrayList<PbsFrontendCompiler.ImportedCallableSurface> importedCallables,
final Set<String> importedCallableKeys,
final PbsFrontendCompiler.ImportedCallableSurface importedCallableSurface) {
final var callableKey = importedCallableSurface.moduleKey()
+ "#"
+ importedCallableSurface.callableName()
+ "#"
+ importedCallableSurface.arity()
+ "#"
+ importedCallableSurface.shapeSurface();
if (importedCallableKeys.add(callableKey)) {
importedCallables.add(importedCallableSurface);
}
}
private String topDeclKey(final PbsAst.TopDecl topDecl) {
final var declName = topDeclName(topDecl);
if (declName == null || declName.isBlank()) {
return null;
}
return topDecl.getClass().getSimpleName() + ":" + declName;
}
private String topDeclName(final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
return structDecl.name();
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
return builtinTypeDecl.name();
}
if (topDecl instanceof PbsAst.ConstDecl constDecl) {
return constDecl.name();
}
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
return serviceDecl.name();
}
if (topDecl instanceof PbsAst.HostDecl hostDecl) {
return hostDecl.name();
}
if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
return contractDecl.name();
}
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
return functionDecl.name();
}
if (topDecl instanceof PbsAst.CallbackDecl callbackDecl) {
return callbackDecl.name();
}
if (topDecl instanceof PbsAst.EnumDecl enumDecl) {
return enumDecl.name();
}
if (topDecl instanceof PbsAst.ErrorDecl errorDecl) {
return errorDecl.name();
}
return null;
}
private IRReservedMetadata mergeReservedMetadata(
final IRReservedMetadata left,
final IRReservedMetadata right) {
final var hostBindings = new ArrayList<IRReservedMetadata.HostMethodBinding>();
hostBindings.addAll(left.hostMethodBindings().asList());
hostBindings.addAll(right.hostMethodBindings().asList());
final var builtinTypeSurfaces = new ArrayList<IRReservedMetadata.BuiltinTypeSurface>();
builtinTypeSurfaces.addAll(left.builtinTypeSurfaces().asList());
builtinTypeSurfaces.addAll(right.builtinTypeSurfaces().asList());
final var builtinConstSurfaces = new ArrayList<IRReservedMetadata.BuiltinConstSurface>();
builtinConstSurfaces.addAll(left.builtinConstSurfaces().asList());
builtinConstSurfaces.addAll(right.builtinConstSurfaces().asList());
final var requiredCapabilities = new HashSet<String>();
requiredCapabilities.addAll(left.requiredCapabilities().asList());
requiredCapabilities.addAll(right.requiredCapabilities().asList());
return new IRReservedMetadata(
ReadOnlyList.wrap(hostBindings),
ReadOnlyList.wrap(builtinTypeSurfaces),
ReadOnlyList.wrap(builtinConstSurfaces),
ReadOnlyList.wrap(requiredCapabilities.stream().toList()));
}
private int returnSlotsFor(final PbsAst.FunctionDecl functionDecl) {
return switch (functionDecl.returnKind()) {
case INFERRED_UNIT, EXPLICIT_UNIT -> 0;
case PLAIN, RESULT -> 1;
};
}
private String importItemLocalName(final PbsAst.ImportItem importItem) {
if (importItem.alias() == null || importItem.alias().isBlank()) {
return importItem.name();
}
return importItem.alias();
}
private Set<ModuleId> blockedModulesByDependency(
final Set<ModuleId> failedModuleIds,
final Map<ModuleId, Set<ModuleId>> dependenciesByModule) {
@@ -420,4 +809,13 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
ModuleId moduleId,
p.studio.compiler.models.IRBackendFile irBackendFile) {
}
private record ImportedSemanticContext(
ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
ReadOnlyList<PbsFrontendCompiler.ImportedCallableSurface> importedCallables,
IRReservedMetadata importedReservedMetadata) {
private static ImportedSemanticContext empty() {
return new ImportedSemanticContext(ReadOnlyList.empty(), ReadOnlyList.empty(), IRReservedMetadata.empty());
}
}
}
@@ -0,0 +1,62 @@
declare host LowLog {
[Host(module = "log", name = "write", version = 1)]
[Capability(name = "log")]
fn write(level: int, message: str) -> void;
[Host(module = "log", name = "write_tag", version = 1)]
[Capability(name = "log")]
fn write_tag(level: int, tag: int, message: str) -> void;
}
declare service Log
{
fn trace(msg: str) -> void
{
LowLog.write(0, msg);
}
fn debug(msg: str) -> void
{
LowLog.write(1, msg);
}
fn info(msg: str) -> void
{
LowLog.write(2, msg);
}
fn warn(msg: str) -> void
{
LowLog.write(3, msg);
}
fn error(msg: str) -> void
{
LowLog.write(4, msg);
}
fn trace(tag: int, msg: str) -> void
{
LowLog.write_tag(0, tag, msg);
}
fn debug(tag: int, msg: str) -> void
{
LowLog.write_tag(1, tag, msg);
}
fn info(tag: int, msg: str) -> void
{
LowLog.write_tag(2, tag, msg);
}
fn warn(tag: int, msg: str) -> void
{
LowLog.write_tag(3, tag, msg);
}
fn error(tag: int, msg: str) -> void
{
LowLog.write_tag(4, tag, msg);
}
}
@@ -0,0 +1,2 @@
mod host LowLog;
pub service Log;
@@ -95,9 +95,10 @@ class PbsGateUSdkInterfaceConformanceTest {
import { Color } from @core:color;
import { Gfx } from @sdk:gfx;
import { Input, InputPad, InputButton } from @sdk:input;
import { Log } from @sdk:log;
declare contract Renderer {
fn render(gfx: Gfx, color: Color, input: Input, pad: InputPad, button: InputButton) -> void;
fn render(gfx: Gfx, color: Color, input: Input, pad: InputPad, button: InputButton, log: Log) -> void;
}
""",
"pub contract Renderer;",
@@ -113,9 +114,19 @@ class PbsGateUSdkInterfaceConformanceTest {
.anyMatch(t -> t.sourceTypeName().equals("Input") && t.canonicalTypeName().equals("input")));
assertTrue(positive.irBackend().getReservedMetadata().builtinTypeSurfaces().stream()
.anyMatch(t -> t.sourceTypeName().equals("InputButton")
&& t.intrinsics().stream().anyMatch(i -> i.canonicalName().equals("hold"))));
&& t.intrinsics().stream().anyMatch(i -> i.canonicalName().equals("input.button.hold"))));
assertTrue(positive.irBackend().getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("Gfx")));
assertTrue(positive.irBackend().getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowLog")
&& h.abiModule().equals("log")
&& h.abiMethod().equals("write")
&& h.abiVersion() == 1));
assertTrue(positive.irBackend().getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowLog")
&& h.abiModule().equals("log")
&& h.abiMethod().equals("write_tag")
&& h.abiVersion() == 1));
final var negative = compileWorkspaceModule(
tempDir.resolve("gate-u-reserved-import-negative"),
@@ -129,6 +140,19 @@ class PbsGateUSdkInterfaceConformanceTest {
final var missingModule = firstDiagnostic(negative.diagnostics(),
d -> d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_MODULE_NOT_FOUND.name()));
assertStableDiagnosticIdentity(missingModule, PbsLinkErrors.E_LINK_IMPORT_MODULE_NOT_FOUND.name(), DiagnosticPhase.LINKING);
final var lowLogNotPublic = compileWorkspaceModule(
tempDir.resolve("gate-u-reserved-import-lowlog-hidden"),
"""
import { LowLog } from @sdk:log;
fn run() -> int { return 1; }
""",
"pub fn run() -> int;",
1,
d -> d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_NOT_PUBLIC.name()));
final var notPublic = firstDiagnostic(lowLogNotPublic.diagnostics(),
d -> d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_NOT_PUBLIC.name()));
assertStableDiagnosticIdentity(notPublic, PbsLinkErrors.E_LINK_IMPORT_SYMBOL_NOT_PUBLIC.name(), DiagnosticPhase.LINKING);
}
@Test
@@ -142,6 +142,31 @@ class PbsParserTest {
assertTrue(diagnostics.stream().anyMatch(d -> d.getCode().equals(ParseErrors.E_PARSE_ATTRIBUTES_NOT_ALLOWED.name())));
}
@Test
void shouldParseServiceAndMemberNamesUsingErrorKeyword() {
final var source = """
declare service Log {
fn error(msg: str) -> void { return; }
fn error(tag: int, msg: str) -> void { return; }
}
fn run() -> void {
Log.error("oops");
Log.error(7, "oops");
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var fileId = new FileId(0);
final PbsAst.File ast = PbsParser.parse(PbsLexer.lex(source, fileId, diagnostics), fileId, diagnostics);
assertTrue(diagnostics.isEmpty(), "Parser should accept 'error' as callable/member name");
final var service = assertInstanceOf(PbsAst.ServiceDecl.class, ast.topDecls().getFirst());
assertEquals(2, service.methods().size());
assertEquals("error", service.methods().getFirst().name());
}
@Test
void shouldAcceptStructFieldTrailingComma() {
final var source = "declare struct S(a: int,);";
@@ -81,10 +81,6 @@ class PbsInterfaceModuleSemanticsTest {
final var source = """
fn run() -> int { return 1; }
declare service Game {
fn tick() -> int { return 1; }
}
declare contract C { fn run() -> int; }
declare struct S(v: int);
implements C for S using s {
@@ -98,7 +94,30 @@ class PbsInterfaceModuleSemanticsTest {
final var nonDeclarativeCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name()))
.count();
assertTrue(nonDeclarativeCount >= 3);
assertTrue(nonDeclarativeCount >= 2);
}
@Test
void shouldAllowDeclarativeServiceFacadesInInterfaceModule() {
final var source = """
declare host LowLog {
[Host(module = "log", name = "write", version = 1)]
[Capability(name = "log")]
fn write(level: int, message: str) -> void;
}
declare service Log {
fn trace(message: str) -> void {
LowLog.write(0, message);
}
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(3), source, diagnostics, SourceKind.SDK_INTERFACE);
assertFalse(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name())));
}
@Test
@@ -181,6 +181,7 @@ class PBSFrontendPhaseServiceTest {
BuildingIssueSink.empty());
assertTrue(diagnostics.isEmpty());
assertEquals("frame", irBackend.getEntryPointCallableName());
assertEquals(2, irBackend.getFunctions().size());
}
@@ -783,6 +784,89 @@ class PBSFrontendPhaseServiceTest {
assertEquals("independent", irBackend.getFunctions().getFirst().name());
}
@Test
void shouldResolveImportedBuiltinTypeChainsInExpressionFlowSemantics() throws IOException {
final var projectRoot = tempDir.resolve("project-builtin-chain");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
final var sourceFile = sourceRoot.resolve("main.pbs");
final var modBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { Log } from @sdk:log;
import { Input } from @sdk:input;
fn frame() -> void
{
if (Input.pad().a().pressed())
{
Log.debug("Hello World!");
}
}
""");
Files.writeString(modBarrel, "pub fn frame() -> void;");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("main")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, sourceFile, fileTable);
registerFile(projectId, projectRoot, modBarrel, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
1);
final var diagnostics = DiagnosticSink.empty();
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_MEMBER_ACCESS.name())));
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_APPLY_NON_CALLABLE_TARGET.name())));
final var frameExecutableOptional = irBackend.getExecutableFunctions().stream()
.filter(function -> "frame".equals(function.callableName()))
.findFirst();
final var executableNames = irBackend.getExecutableFunctions().stream()
.map(function -> function.callableName() + "@" + function.moduleKey())
.toList();
assertTrue(
frameExecutableOptional.isPresent(),
"diagnostics="
+ diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList()
+ " executableNames="
+ executableNames);
final var frameExecutable = frameExecutableOptional.orElseThrow();
final var intrinsicCalls = frameExecutable.instructions().stream()
.filter(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC)
.toList();
assertTrue(intrinsicCalls.stream().anyMatch(instruction -> Integer.valueOf(1).equals(instruction.expectedArgSlots())));
assertTrue(intrinsicCalls.stream().anyMatch(instruction ->
instruction.intrinsicCall() != null && "input.pad".equals(instruction.intrinsicCall().canonicalName())));
assertTrue(intrinsicCalls.stream().anyMatch(instruction ->
instruction.intrinsicCall() != null && "input.pad.a".equals(instruction.intrinsicCall().canonicalName())));
assertTrue(intrinsicCalls.stream().anyMatch(instruction ->
instruction.intrinsicCall() != null && "input.button.pressed".equals(instruction.intrinsicCall().canonicalName())));
assertTrue(
irBackend.getExecutableFunctions().stream().anyMatch(function -> "frame".equals(function.callableName())),
"diagnostics="
+ diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList()
+ " executableNames="
+ executableNames);
}
private void registerFile(
final ProjectId projectId,
final Path projectRoot,