feat(selfhost): Expr/Stmt/Block sourceFile for multi-file and macros
Stamp AST trees from Decl path; lower prefers node sourceFile over func-level path so grafted macro nodes keep their origin for #line maps.
This commit is contained in:
+102
-2
@@ -138,6 +138,8 @@ module Ast {
|
||||
kind: int,
|
||||
line: uint32,
|
||||
column: uint32,
|
||||
// Source file for multi-file #line / future macros (empty = inherit Decl)
|
||||
sourceFile: String,
|
||||
// Common fields
|
||||
strValue: String, // ident name, path segments, field name, callee
|
||||
intValue: int, // operator kind, intrinsic kind
|
||||
@@ -193,6 +195,7 @@ module Ast {
|
||||
struct Block {
|
||||
line: uint32,
|
||||
column: uint32,
|
||||
sourceFile: String, // multi-file #line (empty = inherit Decl)
|
||||
stmtCount: int,
|
||||
firstStmt: *Stmt,
|
||||
lastStmt: *Stmt,
|
||||
@@ -227,6 +230,8 @@ module Ast {
|
||||
kind: int,
|
||||
line: uint32,
|
||||
column: uint32,
|
||||
// Source file for multi-file #line / future macros (empty = inherit Decl)
|
||||
sourceFile: String,
|
||||
// Common fields
|
||||
strValue: String, // let name, pattern ident, label, for var
|
||||
boolValue: bool, // let mutable
|
||||
@@ -372,7 +377,7 @@ module Ast {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func Ast_MakeExpr(kind: int, line: uint32, col: uint32) -> Expr {
|
||||
return Expr { kind: kind, line: line, column: col,
|
||||
return Expr { kind: kind, line: line, column: col, sourceFile: "",
|
||||
strValue: "", intValue: 0, boolValue: false,
|
||||
tokKind: 0, tokText: "",
|
||||
child1: null as *Expr, child2: null as *Expr, child3: null as *Expr,
|
||||
@@ -410,7 +415,7 @@ module Ast {
|
||||
}
|
||||
|
||||
func Ast_MakeStmt(kind: int, line: uint32, col: uint32) -> Stmt {
|
||||
return Stmt { kind: kind, line: line, column: col,
|
||||
return Stmt { kind: kind, line: line, column: col, sourceFile: "",
|
||||
strValue: "", boolValue: false,
|
||||
child1: null as *Expr, child2: null as *Expr, child3: null as *Expr,
|
||||
refStmtType: null as *TypeExpr, refStmtPattern: null as *Pattern,
|
||||
@@ -418,6 +423,101 @@ module Ast {
|
||||
elseIfCount: 0 };
|
||||
}
|
||||
|
||||
/// Stamp sourceFile on an expression tree (empty slots only — keeps grafted macro nodes).
|
||||
func Ast_StampExprFile(e: *Expr, path: String) {
|
||||
if e == null as *Expr { return; }
|
||||
if path == null as String || String_Eq(path, "") { return; }
|
||||
if e.sourceFile == null as String || String_Eq(e.sourceFile, "") {
|
||||
e.sourceFile = path;
|
||||
}
|
||||
Ast_StampExprFile(e.child1, path);
|
||||
Ast_StampExprFile(e.child2, path);
|
||||
Ast_StampExprFile(e.child3, path);
|
||||
if e.refBlock != null as *Block {
|
||||
Ast_StampBlockFile(e.refBlock, path);
|
||||
}
|
||||
var args: *ExprList = e.callArgs;
|
||||
while args != null as *ExprList {
|
||||
Ast_StampExprFile(args.expr, path);
|
||||
args = args.next;
|
||||
}
|
||||
// Match arms (guard lives on Pattern as patGuardExpr when pkGuarded)
|
||||
var arm: *MatchArm = e.matchArms;
|
||||
while arm != null as *MatchArm {
|
||||
Ast_StampExprFile(arm.body, path);
|
||||
if arm.pattern != null as *Pattern {
|
||||
Ast_StampPatternFile(arm.pattern, path);
|
||||
}
|
||||
arm = arm.next;
|
||||
}
|
||||
// Closure default param exprs
|
||||
if e.closureParams != null as *Decl {
|
||||
var pi: int = 0;
|
||||
while pi < e.closureParams.paramCount {
|
||||
var p: *Param = null as *Param;
|
||||
if pi == 0 { p = &e.closureParams.param0; }
|
||||
else if pi == 1 { p = &e.closureParams.param1; }
|
||||
else if pi == 2 { p = &e.closureParams.param2; }
|
||||
else if pi == 3 { p = &e.closureParams.param3; }
|
||||
else if pi == 4 { p = &e.closureParams.param4; }
|
||||
else if pi == 5 { p = &e.closureParams.param5; }
|
||||
else if pi == 6 { p = &e.closureParams.param6; }
|
||||
else if pi == 7 { p = &e.closureParams.param7; }
|
||||
else if pi == 8 { p = &e.closureParams.param8; }
|
||||
if p != null as *Param && p.defaultExpr != null as *Expr {
|
||||
Ast_StampExprFile(p.defaultExpr, path);
|
||||
}
|
||||
pi = pi + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Ast_StampPatternFile(pat: *Pattern, path: String) {
|
||||
if pat == null as *Pattern { return; }
|
||||
if path == null as String || String_Eq(path, "") { return; }
|
||||
if pat.patGuardExpr != null as *Expr {
|
||||
Ast_StampExprFile(pat.patGuardExpr, path);
|
||||
}
|
||||
Ast_StampPatternFile(pat.patChild1, path);
|
||||
Ast_StampPatternFile(pat.patChild2, path);
|
||||
Ast_StampPatternFile(pat.patArgs, path);
|
||||
Ast_StampPatternFile(pat.patNext, path);
|
||||
}
|
||||
|
||||
func Ast_StampBlockFile(b: *Block, path: String) {
|
||||
if b == null as *Block { return; }
|
||||
if path == null as String || String_Eq(path, "") { return; }
|
||||
if b.sourceFile == null as String || String_Eq(b.sourceFile, "") {
|
||||
b.sourceFile = path;
|
||||
}
|
||||
var s: *Stmt = b.firstStmt;
|
||||
while s != null as *Stmt {
|
||||
Ast_StampStmtFile(s, path);
|
||||
s = s.nextStmt;
|
||||
}
|
||||
}
|
||||
|
||||
func Ast_StampStmtFile(s: *Stmt, path: String) {
|
||||
if s == null as *Stmt { return; }
|
||||
if path == null as String || String_Eq(path, "") { return; }
|
||||
if s.sourceFile == null as String || String_Eq(s.sourceFile, "") {
|
||||
s.sourceFile = path;
|
||||
}
|
||||
Ast_StampExprFile(s.child1, path);
|
||||
Ast_StampExprFile(s.child2, path);
|
||||
Ast_StampExprFile(s.child3, path);
|
||||
if s.refStmtBlock != null as *Block {
|
||||
Ast_StampBlockFile(s.refStmtBlock, path);
|
||||
}
|
||||
if s.refStmtElse != null as *Block {
|
||||
Ast_StampBlockFile(s.refStmtElse, path);
|
||||
}
|
||||
if s.refStmtDecl != null as *Decl {
|
||||
// nested decl inside stmt — body stamp via Decl stamp
|
||||
discard;
|
||||
}
|
||||
}
|
||||
|
||||
func Ast_MakeDecl(kind: int, line: uint32, col: uint32) -> Decl {
|
||||
return Decl { kind: kind, line: line, column: col, isPublic: false,
|
||||
sourceFile: "",
|
||||
|
||||
+28
-1
@@ -611,10 +611,19 @@ func Cli_CollectStdlibImports(mod: *Module, outPaths: *String, maxCount: int, st
|
||||
return count;
|
||||
}
|
||||
|
||||
/// Stamp sourceFile on a decl and nested module items (for multi-file #line).
|
||||
/// Stamp sourceFile on a decl, nested items, and body Expr/Stmt/Block trees.
|
||||
/// Prepares multi-file #line and future macro / cross-file grafts (empty slots only).
|
||||
func Cli_StampSourceFile(decl: *Decl, path: String) {
|
||||
if decl == null as *Decl { return; }
|
||||
decl.sourceFile = path;
|
||||
// Function / method body
|
||||
if decl.refBody != null as *Block {
|
||||
Ast_StampBlockFile(decl.refBody, path);
|
||||
}
|
||||
// Const / type alias init exprs if present
|
||||
if decl.constValue != null as *Expr {
|
||||
Ast_StampExprFile(decl.constValue, path);
|
||||
}
|
||||
if decl.kind == dkModule {
|
||||
var inner: *Decl = decl.childDecl1;
|
||||
while inner != null as *Decl {
|
||||
@@ -630,6 +639,24 @@ func Cli_StampSourceFile(decl: *Decl, path: String) {
|
||||
m = m.childDecl2;
|
||||
}
|
||||
}
|
||||
// Default param values
|
||||
var pi: int = 0;
|
||||
while pi < decl.paramCount {
|
||||
var p: *Param = null as *Param;
|
||||
if pi == 0 { p = &decl.param0; }
|
||||
else if pi == 1 { p = &decl.param1; }
|
||||
else if pi == 2 { p = &decl.param2; }
|
||||
else if pi == 3 { p = &decl.param3; }
|
||||
else if pi == 4 { p = &decl.param4; }
|
||||
else if pi == 5 { p = &decl.param5; }
|
||||
else if pi == 6 { p = &decl.param6; }
|
||||
else if pi == 7 { p = &decl.param7; }
|
||||
else if pi == 8 { p = &decl.param8; }
|
||||
if p != null as *Param && p.defaultExpr != null as *Expr {
|
||||
Ast_StampExprFile(p.defaultExpr, path);
|
||||
}
|
||||
pi = pi + 1;
|
||||
}
|
||||
}
|
||||
|
||||
func Cli_MergeFileInto(target: *Module, path: String, skipNames: *String, skipCount: int) -> int {
|
||||
|
||||
+27
-10
@@ -81,6 +81,28 @@ module HirLower {
|
||||
}
|
||||
}
|
||||
|
||||
/// Prefer Expr/Stmt.sourceFile (macro / cross-file graft); else enclosing Decl path.
|
||||
func Lcx_SourceFileFor(ctx: *LowerCtx, nodeFile: String) -> String {
|
||||
if nodeFile != null as String && !String_Eq(nodeFile, "") {
|
||||
return nodeFile;
|
||||
}
|
||||
if ctx.currentSourceFile != null as String && !String_Eq(ctx.currentSourceFile, "") {
|
||||
return ctx.currentSourceFile;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// Set line/column/sourceFile on a freshly allocated HirNode from AST location.
|
||||
func Lcx_SetNodeLoc(ctx: *LowerCtx, n: *HirNode, line: uint32, col: uint32, nodeFile: String) {
|
||||
if n == null as *HirNode { return; }
|
||||
n.line = line;
|
||||
n.column = col;
|
||||
let f: String = Lcx_SourceFileFor(ctx, nodeFile);
|
||||
if f != null as String && !String_Eq(f, "") {
|
||||
n.sourceFile = f;
|
||||
}
|
||||
}
|
||||
|
||||
func Lcx_PatLookup(ctx: *LowerCtx, src: String) -> String {
|
||||
// Most recent rename wins (scan from end)
|
||||
var i: int = ctx.patMapCount - 1;
|
||||
@@ -1000,15 +1022,13 @@ module HirLower {
|
||||
// Alloca result + found flag
|
||||
let allocaNode: *HirNode = bux_alloc(sizeof(HirNode)) as *HirNode;
|
||||
allocaNode.kind = hAlloca;
|
||||
allocaNode.line = line;
|
||||
allocaNode.column = col;
|
||||
Lcx_SetNodeLoc(ctx, allocaNode, line, col, expr.sourceFile);
|
||||
allocaNode.strValue = resultName;
|
||||
allocaNode.typeName = typeName;
|
||||
|
||||
let foundAlloca: *HirNode = bux_alloc(sizeof(HirNode)) as *HirNode;
|
||||
foundAlloca.kind = hAlloca;
|
||||
foundAlloca.line = line;
|
||||
foundAlloca.column = col;
|
||||
Lcx_SetNodeLoc(ctx, foundAlloca, line, col, expr.sourceFile);
|
||||
foundAlloca.strValue = foundName;
|
||||
foundAlloca.typeName = "bool";
|
||||
allocaNode.child3 = foundAlloca;
|
||||
@@ -1175,8 +1195,7 @@ module HirLower {
|
||||
let col: uint32 = expr.column;
|
||||
let n: *HirNode = bux_alloc(sizeof(HirNode)) as *HirNode;
|
||||
n.kind = hBlock;
|
||||
n.line = line;
|
||||
n.column = col;
|
||||
Lcx_SetNodeLoc(ctx, n, line, col, expr.sourceFile);
|
||||
|
||||
let kind: int = expr.kind;
|
||||
|
||||
@@ -2387,8 +2406,7 @@ module HirLower {
|
||||
let col: uint32 = stmt.column;
|
||||
let n: *HirNode = bux_alloc(sizeof(HirNode)) as *HirNode;
|
||||
n.kind = hBlock;
|
||||
n.line = line;
|
||||
n.column = col;
|
||||
Lcx_SetNodeLoc(ctx, n, line, col, stmt.sourceFile);
|
||||
|
||||
let kind: int = stmt.kind;
|
||||
|
||||
@@ -3397,8 +3415,7 @@ module HirLower {
|
||||
// Wrap in an hBlock node with child1 = first statement in chain
|
||||
let n: *HirNode = bux_alloc(sizeof(HirNode)) as *HirNode;
|
||||
n.kind = hBlock;
|
||||
n.line = block.line;
|
||||
n.column = block.column;
|
||||
Lcx_SetNodeLoc(ctx, n, block.line, block.column, block.sourceFile);
|
||||
n.boolValue = true;
|
||||
n.child1 = firstNode;
|
||||
if asExpr && !String_Eq(yieldName, "") {
|
||||
|
||||
Reference in New Issue
Block a user