implements PLN-0137 (7/7) call-hierarchy

Call hierarchy walks CallExpr nodes for functions and methods. It does
not use the references index. A cursor that is not on a callable is
empty, and type hierarchy stays unadvertised.

Housekeep DSC-0066 with LSN-0074.
This commit is contained in:
2026-09-22 09:20:28 +01:00
parent e456bb8d7a
commit 1fcecdb04d
24 changed files with 995 additions and 333 deletions
@@ -0,0 +1,73 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PbsCallHierarchyTest {
private static final String SOURCE = """
fn helper() -> int {
return 1;
}
declare struct Box() {
fn blend(dx: int) -> int {
return dx;
}
}
fn main(box: Box) -> int {
box.blend(1);
return helper();
}
""";
@Test
void functionAndMethodCallsComeFromTheAst() {
final PbsAst.File ast = parse(SOURCE);
final int mainOffset = byteOffset(SOURCE, "fn main");
final var prepared = PbsCallHierarchy.prepare(ast, mainOffset);
assertEquals(1, prepared.size());
assertEquals("main", prepared.getFirst().name());
assertEquals(false, prepared.getFirst().method());
final var outgoing = PbsCallHierarchy.outgoingCalls(ast, prepared.getFirst());
assertTrue(outgoing.stream().anyMatch(call -> call.callee().name().equals("helper") && !call.callee().method()));
assertTrue(outgoing.stream().anyMatch(call -> call.callee().name().equals("blend") && call.callee().method()));
final int helperOffset = byteOffset(SOURCE, "fn helper");
final var helper = PbsCallHierarchy.prepare(ast, helperOffset).getFirst();
final var incoming = PbsCallHierarchy.incomingCalls(ast, helper);
assertEquals(1, incoming.size());
assertEquals("main", incoming.getFirst().caller().name());
}
@Test
void aStructNameIsNotACallable() {
final PbsAst.File ast = parse(SOURCE);
assertTrue(PbsCallHierarchy.prepare(ast, byteOffset(SOURCE, "struct Box")).isEmpty());
}
private static PbsAst.File parse(final String source) {
return PbsParser.parse(
PbsLexer.lex(source, FileId.none(), DiagnosticSink.empty()),
FileId.none(),
DiagnosticSink.empty());
}
private static int byteOffset(final String source, final String marker) {
final int index = source.indexOf(marker);
if (index < 0) {
throw new IllegalArgumentException(marker);
}
return source.substring(0, index + 1).getBytes(StandardCharsets.UTF_8).length;
}
}