diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 570a940272..255feade3f 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -136,6 +136,7 @@ function lowerStatement( const fallthrough = builder.reserve(); const terminal: ReturnTerminal = { kind: "return", + loc: stmt.node.loc ?? GeneratedSource, value, id: makeInstructionId(0), }; diff --git a/compiler/forget/src/HIR/Codegen.ts b/compiler/forget/src/HIR/Codegen.ts index a9f7ab86ef..e73d04bd15 100644 --- a/compiler/forget/src/HIR/Codegen.ts +++ b/compiler/forget/src/HIR/Codegen.ts @@ -43,6 +43,8 @@ function withLoc TNode>( }; } +const createBinaryExpression = withLoc(t.binaryExpression); +const createCallExpression = withLoc(t.callExpression); const createExpressionStatement = withLoc(t.expressionStatement); const createFunctionDeclaration = withLoc(t.functionDeclaration); const createLabelledStatement = withLoc(t.labeledStatement); @@ -192,15 +194,16 @@ class CodegenVisitor return createWhileStatement(terminal.loc, terminal.test, terminal.loop); } case "return": { + const createReturnStatement = withLoc(t.returnStatement); if (terminal.value !== null) { - return t.returnStatement(terminal.value); + return createReturnStatement(terminal.loc, terminal.value); } else if (this.depth === 1) { // A return at the top-level of a function must be the last instruction, // and functions implicitly return after the last instruction of the top-level. // Elide the return. return t.emptyStatement(); } else { - return t.returnStatement(); + return createReturnStatement(terminal.loc); } } case "throw": { @@ -253,7 +256,12 @@ function codegenInstructionValue( case "BinaryExpression": { const left = codegenPlace(temp, instrValue.left); const right = codegenPlace(temp, instrValue.right); - value = t.binaryExpression(instrValue.operator, left, right); + value = createBinaryExpression( + instrValue.loc, + instrValue.operator, + left, + right + ); break; } case "UnaryExpression": { @@ -270,7 +278,7 @@ function codegenInstructionValue( case "CallExpression": { const callee = codegenPlace(temp, instrValue.callee); const args = instrValue.args.map((arg) => codegenPlace(temp, arg)); - value = t.callExpression(callee, args); + value = createCallExpression(instrValue.loc, callee, args); break; } case "NewExpression": { diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 95055f8704..0a8f30e5bd 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -114,6 +114,7 @@ export type ThrowTerminal = { kind: "throw"; value: Place; id: InstructionId }; export type ReturnTerminal = { kind: "return"; + loc: SourceLocation; value: Place | null; id: InstructionId; }; diff --git a/compiler/forget/src/HIR/HIRBuilder.ts b/compiler/forget/src/HIR/HIRBuilder.ts index 6de0673dd8..1bf104869b 100644 --- a/compiler/forget/src/HIR/HIRBuilder.ts +++ b/compiler/forget/src/HIR/HIRBuilder.ts @@ -11,6 +11,7 @@ import { invariant } from "../CompilerError"; import { BasicBlock, BlockId, + GeneratedSource, GotoVariant, HIR, Identifier, @@ -145,7 +146,12 @@ export default class HIRBuilder { this.#completed.set(blockId, { id: blockId, instructions, - terminal: { kind: "return", value: null, id: makeInstructionId(0) }, + terminal: { + kind: "return", + loc: GeneratedSource, + value: null, + id: makeInstructionId(0), + }, preds: new Set(), phis: new Set(), }); diff --git a/compiler/forget/src/HIR/HIRTreeVisitor.ts b/compiler/forget/src/HIR/HIRTreeVisitor.ts index ef26a430c2..f1541f8030 100644 --- a/compiler/forget/src/HIR/HIRTreeVisitor.ts +++ b/compiler/forget/src/HIR/HIRTreeVisitor.ts @@ -74,6 +74,7 @@ class Driver { blockValue, this.visitor.visitTerminal({ kind: "return", + loc: terminal.loc, value, }) ); @@ -697,7 +698,7 @@ export interface Visitor { } export type BlockTerminal = - | { kind: "return"; value: TValue | null } + | { kind: "return"; loc: SourceLocation; value: TValue | null } | { kind: "throw"; value: TValue } | { kind: "if"; diff --git a/compiler/forget/src/HIR/visitors.ts b/compiler/forget/src/HIR/visitors.ts index 441a27b8ca..2377c1e350 100644 --- a/compiler/forget/src/HIR/visitors.ts +++ b/compiler/forget/src/HIR/visitors.ts @@ -193,6 +193,7 @@ export function mapTerminalSuccessors( case "return": { return { kind: "return", + loc: terminal.loc, value: terminal.value, id: makeInstructionId(0), };