Skip to content

Commit

Permalink
libnixt/Serialize: address self-review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Dec 23, 2023
1 parent 48badb2 commit 2cbeb87
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions libnixt/lib/Serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ struct SerializeVisitor : public RecursiveASTVisitor<SerializeVisitor> {
const nix::PosTable &PTable)
: OS(OS), STable(STable), PTable(PTable) {}

Position getPos(nix::PosIdx P);
bool visitExprAttrs(const nix::ExprAttrs *E);
bool visitExprVar(const nix::ExprVar *E);
};

Position SerializeVisitor::getPos(nix::PosIdx P) {
if (P == nix::noPos) {
Position getPos(std::ostream &OS, nix::PosIdx P, const nix::PosTable &PTable) {
if (P == nix::noPos)
return Position{};
}
const auto &Pos = PTable[P];
Position Ret;
Ret.Column = Pos.column;
Expand All @@ -48,11 +46,15 @@ Position SerializeVisitor::getPos(nix::PosIdx P) {

bool SerializeVisitor::visitExprAttrs(const nix::ExprAttrs *E) {
ExprAttrSet Expr;
Expr.Pos = place(OS, getPos(E->pos));
Expr.Pos = place(OS, getPos(OS, E->pos, PTable));
for (const auto &[K, V] : E->attrs) {
Expr.Attrs.emplace_back(AttrDefEntry{
place(OS, std::string(STable[K])),
{V.inherited, ExprMap[V.e], place(OS, getPos(V.pos)), V.displ}});
AttrDefEntry Entry;
Entry.Name = place(OS, std::string(STable[K]));
Entry.Def.Inherited = V.inherited;
Entry.Def.E = ExprMap[V.e];
Entry.Def.Pos = place(OS, getPos(OS, V.pos, PTable));
Entry.Def.Displ = V.displ;
Expr.Attrs.emplace_back(Entry);
}
ExprMap[E] = place(OS, Expr);
return true;
Expand All @@ -64,7 +66,7 @@ bool SerializeVisitor::visitExprVar(const nix::ExprVar *E) {
Expr.Body.Var.Displ = E->displ;
Expr.Body.Var.FromWidth = E->fromWith;
Expr.Body.Var.Name = place(OS, std::string(STable[E->name]));
Expr.Body.Var.Pos = place(OS, getPos(E->pos));
Expr.Body.Var.Pos = place(OS, getPos(OS, E->pos, PTable));
ExprMap[E] = place(OS, Expr);
return true;
}
Expand All @@ -73,14 +75,14 @@ bool SerializeVisitor::visitExprVar(const nix::ExprVar *E) {

std::ostream &write(std::ostream &OS, const nix::SymbolTable &STable,
const nix::PosTable &PTable, const nix::Expr *E) {
SerializeVisitor Visitor(OS, STable, PTable);
// Write the header, metadata stuff like a magic number and a version number.
ASTHeader Header;
strcpy(Header.Magic, "\x7fNixAST");
Header.Version = 1;
write(OS, Header);

// Then traverse the AST
SerializeVisitor Visitor(OS, STable, PTable);
Visitor.traverseExpr(E);
return OS;
}
Expand Down

0 comments on commit 2cbeb87

Please sign in to comment.