From 5dff9437aaa04dc1f2e489eee39b9a46d6809509 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Mon, 8 Apr 2024 11:04:53 +0800 Subject: [PATCH 1/4] libnixt: add AST deserialization --- libnixt/include/nixt/BinOps.inc | 11 ++ libnixt/lib/ASTReader.cpp | 300 ++++++++++++++++++++++++++++++-- libnixt/lib/ASTReader.h | 53 +++++- 3 files changed, 349 insertions(+), 15 deletions(-) create mode 100644 libnixt/include/nixt/BinOps.inc diff --git a/libnixt/include/nixt/BinOps.inc b/libnixt/include/nixt/BinOps.inc new file mode 100644 index 000000000..efadec5b9 --- /dev/null +++ b/libnixt/include/nixt/BinOps.inc @@ -0,0 +1,11 @@ +#ifdef BinOp + +BinOp(OpEq, "==") +BinOp(OpNEq, "!=") +BinOp(OpAnd, "&&") +BinOp(OpOr, "||") +BinOp(OpImpl, "->") +BinOp(OpUpdate, "//") +BinOp(OpConcatLists, "++") + +#endif // BinOp diff --git a/libnixt/lib/ASTReader.cpp b/libnixt/lib/ASTReader.cpp index 876b0d636..6cef29569 100644 --- a/libnixt/lib/ASTReader.cpp +++ b/libnixt/lib/ASTReader.cpp @@ -11,34 +11,314 @@ #include #include +#include namespace nixt { using namespace nixbc; -nix::Expr *ASTDeserializer::eatHookable(std::string_view &Data, ValueMap &VMap, - EnvMap &EMap) { +nix::Expr *ASTDeserializer::eatHookable(std::string_view &Data) { +#pragma GCC diagnostic push +#pragma GCC diagnostic error "-Wimplicit-fallthrough" + +#define HOOKEXPR(TYPE, EXPR) HookExpr##TYPE(EXPR, VMap, EMap, Hdr.Handle) auto Hdr = eat(Data); + nix::PosIdx Pos = Ctx.PTable.add(Ctx.Origin, Hdr.BeginLine, Hdr.BeginColumn); + nix::Expr *NewExpr = nullptr; switch (Hdr.Kind) { - case EK_Int: { - return Pool.record( - new HookExprInt(eatExprInt(Data), VMap, EMap, Hdr.Handle)); + case EK_Null: + return nullptr; + case EK_Int: + NewExpr = new HOOKEXPR(Int, eatExprInt(Data)); + break; + case EK_Float: + NewExpr = new HOOKEXPR(Float, eatExprFloat(Data)); + break; + case EK_String: { + auto S = bc::eat(Data); + NewExpr = new HOOKEXPR(String, nix::ExprString(std::move(S))); + break; + } + case EK_Path: + NewExpr = new HOOKEXPR(Path, eatExprPath(Data)); + break; + case EK_Var: { + NewExpr = new HOOKEXPR(Var, eatExprVar(Pos, Data)); + break; } + case EK_Select: + NewExpr = new HOOKEXPR(Select, eatExprSelect(Pos, Data)); + break; + case EK_OpHasAttr: + NewExpr = new HOOKEXPR(OpHasAttr, eatExprOpHasAttr(Data)); + break; + case EK_Attrs: + NewExpr = new HOOKEXPR(Attrs, eatExprAttrs(Pos, Data)); + break; + case EK_List: + NewExpr = new HOOKEXPR(List, eatExprList(Data)); + break; + case EK_Lambda: + NewExpr = new HOOKEXPR(Lambda, eatExprLambnda(Pos, Data)); + break; + case EK_Call: + NewExpr = new HOOKEXPR(Call, eatExprCall(Pos, Data)); + break; + case EK_Let: + NewExpr = new HOOKEXPR(Let, eatExprLet(Data)); + break; + case EK_With: + NewExpr = new HOOKEXPR(With, eatExprWith(Pos, Data)); + break; + case EK_If: + NewExpr = new HOOKEXPR(If, eatExprIf(Pos, Data)); + break; + case EK_Assert: + NewExpr = new HOOKEXPR(Assert, eatExprAssert(Pos, Data)); + break; + case EK_ConcatStrings: + NewExpr = new HOOKEXPR(ConcatStrings, eatExprConcatStrings(Pos, Data)); + break; + case EK_OpNot: + NewExpr = new HOOKEXPR(OpNot, eatExprOpNot(Data)); + break; +#define BinOp(OP, SYM) \ + case EK_##OP: \ + NewExpr = new HOOKEXPR(OP, eatExpr##OP(Pos, Data)); \ + break; +#include "nixt/BinOps.inc" +#undef BinOp default: break; } - assert(false && "Unknown hookable kind"); - __builtin_unreachable(); - return nullptr; +#pragma GCC diagnostic pop + + assert(NewExpr && "Unknown hookable kind!"); + Pool.record(NewExpr); + return NewExpr; } nix::Symbol ASTDeserializer::eatSymbol(std::string_view &Data) { return Ctx.STable.create(eat(Data)); } +nix::PosIdx ASTDeserializer::eatPos(std::string_view &Data) { + auto Line = eat(Data); + auto Column = eat(Data); + return Ctx.PTable.add(Ctx.Origin, Line, Column); +} + nix::ExprInt ASTDeserializer::eatExprInt(std::string_view &Data) { return {eat(Data)}; } +nix::ExprFloat ASTDeserializer::eatExprFloat(std::string_view &Data) { + return {eat(Data)}; +} + +nix::ExprPath ASTDeserializer::eatExprPath(std::string_view &Data) { + return {Ctx.RootFS, bc::eat(Data)}; +} + +nix::ExprVar ASTDeserializer::eatExprVar(nix::PosIdx Pos, + std::string_view &Data) { + auto Name = eat(Data); + nix::Symbol Sym = Ctx.STable.create(Name); + return {Pos, Sym}; +} + +nix::AttrPath ASTDeserializer::eatAttrPath(std::string_view &Data) { + auto Size = eat(Data); + + nix::AttrPath Result; + for (std::size_t I = 0; I < Size; I++) { + // It could be a symbol, or an expression. + // The convention is, if it is a symbol, read an "ExprString" + // Otherwise interpret it as an expression (needs to be evaluated). + nix::Expr *Expr = eatHookable(Data); + if (const auto *String = dynamic_cast(Expr)) { + // Create a symbol + Result.emplace_back(Ctx.STable.create(String->s)); + } else { + Result.emplace_back(Expr); + } + } + + return Result; +} + +nix::ExprSelect ASTDeserializer::eatExprSelect(nix::PosIdx Pos, + std::string_view &Data) { + nix::Expr *E = eatHookable(Data); + auto AttrPath = eatAttrPath(Data); + nix::Expr *Default = eatHookable(Data); + return {Pos, E, std::move(AttrPath), Default}; +} + +nix::ExprOpHasAttr ASTDeserializer::eatExprOpHasAttr(std::string_view &Data) { + nix::Expr *E = eatHookable(Data); + auto AttrPath = eatAttrPath(Data); + return {E, std::move(AttrPath)}; +} + +nix::ExprAttrs ASTDeserializer::eatExprAttrs(nix::PosIdx Pos, + std::string_view &Data) { + nix::ExprAttrs Result(Pos); + + Result.recursive = eat(Data); + + auto NStaticDefs = eat(Data); + for (std::size_t I = 0; I < NStaticDefs; I++) { + // Read static attr defs + auto Name = eatSymbol(Data); + + // AttrDef + nix::Expr *E = eatHookable(Data); + nix::PosIdx Pos = eatPos(Data); + auto Inherited = eat(Data); + Result.attrs.insert({Name, {E, Pos, Inherited}}); + } + + auto NDynamicDefs = eat(Data); + for (std::size_t I = 0; I < NDynamicDefs; I++) { + nix::Expr *Name = eatHookable(Data); + nix::Expr *Value = eatHookable(Data); + nix::PosIdx Pos = eatPos(Data); + Result.dynamicAttrs.emplace_back(Name, Value, Pos); + } + + return Result; +} + +nix::ExprList ASTDeserializer::eatExprList(std::string_view &Data) { + nix::ExprList Result; + + auto N = eat(Data); + + for (std::size_t I = 0; I < N; I++) { + nix::Expr *E = eatHookable(Data); + Result.elems.emplace_back(E); + } + + return Result; +} + +nix::ExprLambda ASTDeserializer::eatExprLambnda(nix::PosIdx Pos, + std::string_view &Data) { + // Does this lambda have a unified argument? + // a: a + 1 + // ^<-------- this symbol + std::optional Arg; + auto HasArg = eat(Data); + + if (HasArg) + Arg = eatSymbol(Data); + + // Does this lambda have formal arguments? + // { a, b }: a + b + auto HasFormals = eat(Data); + nix::Formals *Formals = nullptr; + + if (HasFormals) { + // Decode the formals + // FIXME: !! Apparently leaks here. + Formals = new nix::Formals; + Formals->ellipsis = eat(Data); + + auto N = eat(Data); + for (std::size_t I = 0; I < N; I++) { + nix::PosIdx Pos = eatPos(Data); + nix::Symbol Name = eatSymbol(Data); + nix::Expr *Def = eatHookable(Data); + Formals->formals.emplace_back(Pos, Name, Def); + } + } + + nix::Expr *Body = eatHookable(Data); + + if (HasArg) + return {Pos, *Arg, Formals, Body}; + return {Pos, Formals, Body}; +} + +nix::ExprCall ASTDeserializer::eatExprCall(nix::PosIdx Pos, + std::string_view &Data) { + nix::Expr *Fn = eatHookable(Data); + + std::vector Args; + auto N = eat(Data); + for (std::size_t I = 0; I < N; I++) { + nix::Expr *Arg = eatHookable(Data); + Args.emplace_back(Arg); + } + + return {Pos, Fn, std::move(Args)}; +} + +nix::ExprLet ASTDeserializer::eatExprLet(std::string_view &Data) { + auto *Attrs = dynamic_cast(eatHookable(Data)); + if (!Attrs) + Attrs = Pool.record(new nix::ExprAttrs()); + nix::Expr *Body = eatHookable(Data); + return {Attrs, Body}; +} + +nix::ExprWith ASTDeserializer::eatExprWith(nix::PosIdx Pos, + std::string_view &Data) { + nix::Expr *Attrs = eatHookable(Data); + nix::Expr *Body = eatHookable(Data); + + return {Pos, Attrs, Body}; +} + +nix::ExprIf ASTDeserializer::eatExprIf(nix::PosIdx Pos, + std::string_view &Data) { + nix::Expr *Cond = eatHookable(Data); + nix::Expr *Then = eatHookable(Data); + nix::Expr *Else = eatHookable(Data); + + return {Pos, Cond, Then, Else}; +} + +nix::ExprAssert ASTDeserializer::eatExprAssert(nix::PosIdx Pos, + std::string_view &Data) { + nix::Expr *Cond = eatHookable(Data); + nix::Expr *Body = eatHookable(Data); + + return {Pos, Cond, Body}; +} + +nix::ExprOpNot ASTDeserializer::eatExprOpNot(std::string_view &Data) { + return {eatHookable(Data)}; +} + +#define BinOp(OP, SYM) \ + nix::Expr##OP ASTDeserializer::eatExpr##OP(nix::PosIdx Pos, \ + std::string_view &Data) { \ + nix::Expr *LHS = eatHookable(Data); \ + nix::Expr *RHS = eatHookable(Data); \ + return {Pos, LHS, RHS}; \ + } +#include "nixt/BinOps.inc" +#undef BinOp + +nix::ExprConcatStrings +ASTDeserializer::eatExprConcatStrings(nix::PosIdx Pos, std::string_view &Data) { + bool ForceString = eat(Data); + + // FIXME: apparently this LEAKS. + // However in libnixt nix itself leaks many things so who cares this one?? + auto *Expressions = new std::vector>; + + auto Size = eat(Data); + + for (std::size_t I = 0; I < Size; I++) { + nix::Expr *E = eatHookable(Data); + Expressions->emplace_back(E->getPos(), E); + } + + return {Pos, ForceString, Expressions}; +} + DeserializeContext getDeserializeContext(nix::EvalState &State, std::string_view BasePath, const nix::Pos::Origin &Origin) { @@ -58,8 +338,8 @@ nix::Expr *deserializeHookable(std::string_view &Data, DeserializeContext &Ctx, PtrPool &Pool, ValueMap &VMap, EnvMap &EMap) { // Deserialize "Hookable" - ASTDeserializer D(Ctx, Pool); - return D.eatHookable(Data, VMap, EMap); + ASTDeserializer D(Ctx, Pool, VMap, EMap); + return D.eatHookable(Data); } } // namespace nixt diff --git a/libnixt/lib/ASTReader.h b/libnixt/lib/ASTReader.h index 32d9b416d..d9d5bd8e0 100644 --- a/libnixt/lib/ASTReader.h +++ b/libnixt/lib/ASTReader.h @@ -13,18 +13,61 @@ namespace nixt { class ASTDeserializer { DeserializeContext &Ctx; PtrPool &Pool; - - template T eat(std::string_view &Data) { return bc::eat(Data); } + ValueMap &VMap; + EnvMap &EMap; public: - ASTDeserializer(DeserializeContext &Ctx, PtrPool &Pool) - : Ctx(Ctx), Pool(Pool) {} + ASTDeserializer(DeserializeContext &Ctx, PtrPool &Pool, + ValueMap &VMap, EnvMap &EMap) + : Ctx(Ctx), Pool(Pool), VMap(VMap), EMap(EMap) {} + + nix::Expr *eatHookable(std::string_view &Data); - nix::Expr *eatHookable(std::string_view &Data, ValueMap &VMap, EnvMap &EMap); + template T eat(std::string_view &Data) { return bc::eat(Data); } nix::Symbol eatSymbol(std::string_view &Data); + nix::PosIdx eatPos(std::string_view &Data); + nix::ExprInt eatExprInt(std::string_view &Data); + + nix::ExprFloat eatExprFloat(std::string_view &Data); + + nix::ExprPath eatExprPath(std::string_view &Data); + + nix::ExprVar eatExprVar(nix::PosIdx Pos, std::string_view &Data); + + nix::AttrPath eatAttrPath(std::string_view &Data); + + nix::ExprSelect eatExprSelect(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprOpHasAttr eatExprOpHasAttr(std::string_view &Data); + + nix::ExprAttrs eatExprAttrs(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprList eatExprList(std::string_view &Data); + + nix::ExprLambda eatExprLambnda(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprCall eatExprCall(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprLet eatExprLet(std::string_view &Data); + + nix::ExprWith eatExprWith(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprIf eatExprIf(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprAssert eatExprAssert(nix::PosIdx Pos, std::string_view &Data); + + nix::ExprOpNot eatExprOpNot(std::string_view &Data); + +#define BinOp(OP, SYM) \ + nix::Expr##OP eatExpr##OP(nix::PosIdx Pos, std::string_view &Data); +#include "nixt/BinOps.inc" +#undef BinOp + + nix::ExprConcatStrings eatExprConcatStrings(nix::PosIdx Pos, + std::string_view &Data); }; } // namespace nixt From 3d1e5eb1b83d5aa7f1f087755f054ad7e74d74ff Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Sun, 7 Apr 2024 17:16:10 +0800 Subject: [PATCH 2/4] libnixt: consider nullability about AST pointers --- libnixt/lib/ASTReader.cpp | 39 +++++++++++++++++++++++++-------------- libnixt/lib/ASTReader.h | 7 +++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/libnixt/lib/ASTReader.cpp b/libnixt/lib/ASTReader.cpp index 6cef29569..209a0d5da 100644 --- a/libnixt/lib/ASTReader.cpp +++ b/libnixt/lib/ASTReader.cpp @@ -135,6 +135,8 @@ nix::AttrPath ASTDeserializer::eatAttrPath(std::string_view &Data) { // The convention is, if it is a symbol, read an "ExprString" // Otherwise interpret it as an expression (needs to be evaluated). nix::Expr *Expr = eatHookable(Data); + if (!Expr) + continue; if (const auto *String = dynamic_cast(Expr)) { // Create a symbol Result.emplace_back(Ctx.STable.create(String->s)); @@ -149,13 +151,15 @@ nix::AttrPath ASTDeserializer::eatAttrPath(std::string_view &Data) { nix::ExprSelect ASTDeserializer::eatExprSelect(nix::PosIdx Pos, std::string_view &Data) { nix::Expr *E = eatHookable(Data); + assert(E); auto AttrPath = eatAttrPath(Data); - nix::Expr *Default = eatHookable(Data); + nix::Expr *Default = /*nullable*/ eatHookable(Data); return {Pos, E, std::move(AttrPath), Default}; } nix::ExprOpHasAttr ASTDeserializer::eatExprOpHasAttr(std::string_view &Data) { nix::Expr *E = eatHookable(Data); + assert(E); auto AttrPath = eatAttrPath(Data); return {E, std::move(AttrPath)}; } @@ -173,6 +177,7 @@ nix::ExprAttrs ASTDeserializer::eatExprAttrs(nix::PosIdx Pos, // AttrDef nix::Expr *E = eatHookable(Data); + assert(E); nix::PosIdx Pos = eatPos(Data); auto Inherited = eat(Data); Result.attrs.insert({Name, {E, Pos, Inherited}}); @@ -182,6 +187,7 @@ nix::ExprAttrs ASTDeserializer::eatExprAttrs(nix::PosIdx Pos, for (std::size_t I = 0; I < NDynamicDefs; I++) { nix::Expr *Name = eatHookable(Data); nix::Expr *Value = eatHookable(Data); + assert(Name && Value); nix::PosIdx Pos = eatPos(Data); Result.dynamicAttrs.emplace_back(Name, Value, Pos); } @@ -196,6 +202,7 @@ nix::ExprList ASTDeserializer::eatExprList(std::string_view &Data) { for (std::size_t I = 0; I < N; I++) { nix::Expr *E = eatHookable(Data); + assert(E); Result.elems.emplace_back(E); } @@ -228,12 +235,13 @@ nix::ExprLambda ASTDeserializer::eatExprLambnda(nix::PosIdx Pos, for (std::size_t I = 0; I < N; I++) { nix::PosIdx Pos = eatPos(Data); nix::Symbol Name = eatSymbol(Data); - nix::Expr *Def = eatHookable(Data); + // explicitly nullable, nullptr means there is no default + nix::Expr *Def = /*nullable*/ eatHookable(Data); Formals->formals.emplace_back(Pos, Name, Def); } } - nix::Expr *Body = eatHookable(Data); + nix::Expr *Body = eatNonNullHookable(Data); if (HasArg) return {Pos, *Arg, Formals, Body}; @@ -243,11 +251,13 @@ nix::ExprLambda ASTDeserializer::eatExprLambnda(nix::PosIdx Pos, nix::ExprCall ASTDeserializer::eatExprCall(nix::PosIdx Pos, std::string_view &Data) { nix::Expr *Fn = eatHookable(Data); + assert(Fn); std::vector Args; auto N = eat(Data); for (std::size_t I = 0; I < N; I++) { nix::Expr *Arg = eatHookable(Data); + assert(Arg); Args.emplace_back(Arg); } @@ -258,44 +268,44 @@ nix::ExprLet ASTDeserializer::eatExprLet(std::string_view &Data) { auto *Attrs = dynamic_cast(eatHookable(Data)); if (!Attrs) Attrs = Pool.record(new nix::ExprAttrs()); - nix::Expr *Body = eatHookable(Data); + nix::Expr *Body = eatNonNullHookable(Data); return {Attrs, Body}; } nix::ExprWith ASTDeserializer::eatExprWith(nix::PosIdx Pos, std::string_view &Data) { - nix::Expr *Attrs = eatHookable(Data); - nix::Expr *Body = eatHookable(Data); + nix::Expr *Attrs = eatNonNullHookable(Data); + nix::Expr *Body = eatNonNullHookable(Data); return {Pos, Attrs, Body}; } nix::ExprIf ASTDeserializer::eatExprIf(nix::PosIdx Pos, std::string_view &Data) { - nix::Expr *Cond = eatHookable(Data); - nix::Expr *Then = eatHookable(Data); - nix::Expr *Else = eatHookable(Data); + nix::Expr *Cond = eatNonNullHookable(Data); + nix::Expr *Then = eatNonNullHookable(Data); + nix::Expr *Else = eatNonNullHookable(Data); return {Pos, Cond, Then, Else}; } nix::ExprAssert ASTDeserializer::eatExprAssert(nix::PosIdx Pos, std::string_view &Data) { - nix::Expr *Cond = eatHookable(Data); - nix::Expr *Body = eatHookable(Data); + nix::Expr *Cond = eatNonNullHookable(Data); + nix::Expr *Body = eatNonNullHookable(Data); return {Pos, Cond, Body}; } nix::ExprOpNot ASTDeserializer::eatExprOpNot(std::string_view &Data) { - return {eatHookable(Data)}; + return {eatNonNullHookable(Data)}; } #define BinOp(OP, SYM) \ nix::Expr##OP ASTDeserializer::eatExpr##OP(nix::PosIdx Pos, \ std::string_view &Data) { \ - nix::Expr *LHS = eatHookable(Data); \ - nix::Expr *RHS = eatHookable(Data); \ + nix::Expr *LHS = eatNonNullHookable(Data); \ + nix::Expr *RHS = eatNonNullHookable(Data); \ return {Pos, LHS, RHS}; \ } #include "nixt/BinOps.inc" @@ -313,6 +323,7 @@ ASTDeserializer::eatExprConcatStrings(nix::PosIdx Pos, std::string_view &Data) { for (std::size_t I = 0; I < Size; I++) { nix::Expr *E = eatHookable(Data); + assert(E); Expressions->emplace_back(E->getPos(), E); } diff --git a/libnixt/lib/ASTReader.h b/libnixt/lib/ASTReader.h index d9d5bd8e0..0fe94b955 100644 --- a/libnixt/lib/ASTReader.h +++ b/libnixt/lib/ASTReader.h @@ -23,6 +23,13 @@ class ASTDeserializer { nix::Expr *eatHookable(std::string_view &Data); + nix::Expr *eatNonNullHookable(std::string_view &Data) { + nix::Expr *E = eatHookable(Data); + if (E) + return E; + return Pool.record(new nix::ExprVar(Ctx.STable.create("null"))); + } + template T eat(std::string_view &Data) { return bc::eat(Data); } nix::Symbol eatSymbol(std::string_view &Data); From db03e8a1f71964e81f4591668681431f354a81fa Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Mon, 8 Apr 2024 12:48:06 +0800 Subject: [PATCH 3/4] libnixt: add unit tests for ASTReader --- libnixt/meson.build | 11 +- libnixt/test/ASTReader.cpp | 35 +++++ libnixt/test/InitEval.cpp | 9 ++ libnixt/test/ReaderData/AllGrammars.cpp | 192 ++++++++++++++++++++++++ libnixt/test/ReaderData/ReaderData.h | 8 + 5 files changed, 252 insertions(+), 3 deletions(-) create mode 100644 libnixt/test/ASTReader.cpp create mode 100644 libnixt/test/InitEval.cpp create mode 100644 libnixt/test/ReaderData/AllGrammars.cpp create mode 100644 libnixt/test/ReaderData/ReaderData.h diff --git a/libnixt/meson.build b/libnixt/meson.build index 101562fb0..143fa7b89 100644 --- a/libnixt/meson.build +++ b/libnixt/meson.build @@ -31,8 +31,13 @@ nixt = declare_dependency(include_directories: libnixdInc, link_with: libnixt ) -libnixt_test_exe = executable('test-libnixt', - 'test/Kinds.cpp', - dependencies: [ gtest_main, nixt ]) +libnixt_test_exe = executable( + 'test-libnixt', + 'test/ReaderData/AllGrammars.cpp', + 'test/ASTReader.cpp', + 'test/InitEval.cpp', + 'test/Kinds.cpp', + dependencies: [ gtest_main, nixt ] +) test('unit/libnixt', libnixt_test_exe) diff --git a/libnixt/test/ASTReader.cpp b/libnixt/test/ASTReader.cpp new file mode 100644 index 000000000..b062dad90 --- /dev/null +++ b/libnixt/test/ASTReader.cpp @@ -0,0 +1,35 @@ +#include + +#include "ReaderData/ReaderData.h" + +#include "nixt/Deserialize.h" +#include "nixt/InitEval.h" + +#include +#include + +using namespace nixt; + +namespace { + +struct ReaderTest : testing::Test { + std::unique_ptr State; + nix::Pos::Origin O = nix::Pos::none_tag{}; + DeserializeContext Ctx; + ValueMap VMap; + EnvMap EMap; + PtrPool Pool; + ReaderTest() + : State(new nix::EvalState{{}, nix::openStore("dummy://")}), + Ctx(getDeserializeContext(*State, ".", O)) {} +}; + +TEST_F(ReaderTest, AllGrammars) { + std::string_view Data(reinterpret_cast(AllGrammars), + AllGrammarsLen); + nix::Expr *AST = deserializeHookable(Data, Ctx, Pool, VMap, EMap); + + ASSERT_TRUE(dynamic_cast(AST)); +} + +} // namespace diff --git a/libnixt/test/InitEval.cpp b/libnixt/test/InitEval.cpp new file mode 100644 index 000000000..3a363fd0f --- /dev/null +++ b/libnixt/test/InitEval.cpp @@ -0,0 +1,9 @@ +#include "nixt/InitEval.h" + +using namespace nixt; + +namespace { + +__attribute__((constructor)) void init() { initEval(); } + +} // namespace diff --git a/libnixt/test/ReaderData/AllGrammars.cpp b/libnixt/test/ReaderData/AllGrammars.cpp new file mode 100644 index 000000000..7013d681f --- /dev/null +++ b/libnixt/test/ReaderData/AllGrammars.cpp @@ -0,0 +1,192 @@ +#include "ReaderData.h" + +const unsigned char nixt::AllGrammars[] = { + 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x77, 0x6c, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x23, 0x6c, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x60, 0x21, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x20, 0x77, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x41, 0x64, 0x64, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x50, 0x3f, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x34, 0x7f, 0x00, + 0x00, 0x30, 0x3e, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, + 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, + 0x3f, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x3f, 0xf0, 0x3e, 0x6c, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x08, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x68, 0x61, 0x73, 0x41, 0x74, 0x74, 0x72, 0x0e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x2f, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x2c, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x6f, 0x6d, 0x65, 0x41, 0x74, 0x74, + 0x72, 0x53, 0x65, 0x74, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x17, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x30, 0x2e, 0x6c, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x05, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x69, 0x66, 0x45, 0x6c, 0x73, 0x65, 0x06, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xb0, 0x50, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, + 0xfc, 0x7f, 0x00, 0x00, 0x90, 0x4f, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xf0, 0x4f, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, + 0x15, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x50, 0x6c, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x41, 0x6e, 0x64, 0x0b, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3b, 0x6c, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x07, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3a, 0x6c, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x20, 0x3b, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, + 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x61, 0x6d, 0x62, + 0x64, 0x61, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x6b, + 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0b, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x6f, 0x70, 0x45, 0x71, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x30, 0x59, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x10, 0x58, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x58, 0x6c, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x6f, 0x70, 0x49, 0x6d, 0x70, 0x6c, 0x0f, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x50, 0x61, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0e, + 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x30, 0x60, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0e, + 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, + 0x60, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x10, + 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, + 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x6f, 0x70, 0x4e, 0x45, 0x71, 0x10, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x5d, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x5c, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x20, 0x5d, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, + 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x0d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x70, 0x4e, 0x6f, 0x74, 0x11, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x65, 0x6c, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x18, + 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0xe0, 0x64, 0x6c, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x05, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x61, 0x6c, 0x73, 0x65, + 0x0f, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x70, 0x4f, 0x72, 0x12, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x69, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x67, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xb0, 0x68, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, + 0x00, 0x0e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x70, 0x73, 0x04, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x4c, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xfc, 0x7f, + 0x00, 0x00, 0x40, 0x4a, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, + 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0xfc, 0x7f, + 0x00, 0x00, 0x40, 0x4a, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, + 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x5f, 0x5f, 0x73, 0x75, 0x62, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, + 0x44, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x09, + 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x10, + 0x44, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x09, + 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x10, + 0x44, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x09, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, + 0x5f, 0x6c, 0x65, 0x73, 0x73, 0x54, 0x68, 0x61, 0x6e, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xf0, 0x42, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, + 0x00, 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43, 0x6c, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x00, 0xfc, 0x7f, 0x00, 0x00, 0xb0, 0x49, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x09, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, + 0x00, 0xfc, 0x7f, 0x00, 0x00, 0xb0, 0x49, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x09, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x6d, 0x75, 0x6c, 0x02, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x70, 0x47, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, + 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xfc, 0x7f, + 0x00, 0x00, 0x70, 0x47, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, + 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0xfc, 0x7f, + 0x00, 0x00, 0x70, 0x47, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, + 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x5f, 0x5f, 0x6c, 0x65, 0x73, 0x73, 0x54, 0x68, 0x61, 0x6e, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x10, 0x47, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x50, 0x46, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0x50, 0x49, 0x6c, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0xfc, 0x7f, 0x00, 0x00, 0x50, 0x4c, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0xfc, 0x7f, 0x00, 0x00, 0x50, 0x4c, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x64, 0x69, 0x76, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x30, 0x4b, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, + 0x00, 0x22, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x4b, 0x6c, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x70, 0x61, 0x74, 0x68, 0x14, 0x00, 0x00, 0x00, 0xfc, 0x7f, + 0x00, 0x00, 0x50, 0x29, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x2e, 0x2f, 0x2e, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x6f, + 0x6d, 0x65, 0x41, 0x74, 0x74, 0x72, 0x53, 0x65, 0x74, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x37, 0x6c, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x61, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, + 0x35, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, + 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x70, 0x64, + 0x61, 0x74, 0x65, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, + 0x55, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x0b, + 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, + 0x53, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x0b, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x90, 0x54, 0x6c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x0b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +unsigned int nixt::AllGrammarsLen = 2238; diff --git a/libnixt/test/ReaderData/ReaderData.h b/libnixt/test/ReaderData/ReaderData.h new file mode 100644 index 000000000..d93f1507b --- /dev/null +++ b/libnixt/test/ReaderData/ReaderData.h @@ -0,0 +1,8 @@ +#pragma once + +namespace nixt { + +extern const unsigned char AllGrammars[]; +extern unsigned int AllGrammarsLen; + +} // namespace nixt From ccf2d1f6f22e4bbbb294a0d2e689a08462f61e3e Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Mon, 8 Apr 2024 13:05:03 +0800 Subject: [PATCH 4/4] libnixt: fix uninitialized use --- libnixt/include/nixt/HookExpr.h | 293 ++++++++++++++++++++++++++++++-- libnixt/lib/ASTReader.cpp | 4 +- 2 files changed, 283 insertions(+), 14 deletions(-) diff --git a/libnixt/include/nixt/HookExpr.h b/libnixt/include/nixt/HookExpr.h index c252719ba..d5a2b6148 100644 --- a/libnixt/include/nixt/HookExpr.h +++ b/libnixt/include/nixt/HookExpr.h @@ -9,18 +9,285 @@ namespace nixt { using ValueMap = std::map; using EnvMap = std::map; -#define NIX_EXPR(EXPR) \ - struct Hook##EXPR : nix::EXPR { \ - ValueMap &VMap; \ - EnvMap &EMap; \ - std::uintptr_t Handle; \ - Hook##EXPR(nix::EXPR E, ValueMap &VMap, EnvMap &EMap, \ - std::uintptr_t Handle) \ - : nix::EXPR(E), VMap(VMap), EMap(EMap), Handle(Handle) {} \ - void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; \ - std::string getName(); \ - }; -#include "Nodes.inc" -#undef NIX_EXPR +struct HookExprAssert : nix::ExprAssert { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprAssert(nix::ExprAssert E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprAssert(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; +struct HookExprAttrs : nix::ExprAttrs { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprAttrs(nix::ExprAttrs E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprAttrs(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprCall : nix::ExprCall { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprCall(nix::ExprCall E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprCall(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprConcatStrings : nix::ExprConcatStrings { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprConcatStrings(nix::ExprConcatStrings E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprConcatStrings(std::move(E)), VMap(VMap), EMap(EMap), + Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprFloat : nix::ExprFloat { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprFloat(nix::ExprFloat E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprFloat(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprIf : nix::ExprIf { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprIf(nix::ExprIf E, ValueMap &VMap, EnvMap &EMap, std::uintptr_t Handle) + : nix::ExprIf(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprInt : nix::ExprInt { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprInt(nix::ExprInt E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprInt(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprLambda : nix::ExprLambda { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprLambda(nix::ExprLambda E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprLambda(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprLet : nix::ExprLet { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprLet(nix::ExprLet E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprLet(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprList : nix::ExprList { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprList(nix::ExprList E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprList(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpAnd : nix::ExprOpAnd { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpAnd(nix::ExprOpAnd E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpAnd(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpConcatLists : nix::ExprOpConcatLists { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpConcatLists(nix::ExprOpConcatLists E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpConcatLists(std::move(E)), VMap(VMap), EMap(EMap), + Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpEq : nix::ExprOpEq { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpEq(nix::ExprOpEq E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpEq(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpHasAttr : nix::ExprOpHasAttr { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpHasAttr(nix::ExprOpHasAttr E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpHasAttr(std::move(E)), VMap(VMap), EMap(EMap), + Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpImpl : nix::ExprOpImpl { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpImpl(nix::ExprOpImpl E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpImpl(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpNEq : nix::ExprOpNEq { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpNEq(nix::ExprOpNEq E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpNEq(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpNot : nix::ExprOpNot { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpNot(nix::ExprOpNot E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpNot(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpOr : nix::ExprOpOr { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpOr(nix::ExprOpOr E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpOr(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprOpUpdate : nix::ExprOpUpdate { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprOpUpdate(nix::ExprOpUpdate E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprOpUpdate(std::move(E)), VMap(VMap), EMap(EMap), + Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprPath : nix::ExprPath { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprPath(nix::ExprPath E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprPath(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) { + v.mkPath(&*accessor, s.c_str()); + } + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprPos : nix::ExprPos { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprPos(nix::ExprPos E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprPos(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprSelect : nix::ExprSelect { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprSelect(nix::ExprSelect E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprSelect(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprString : nix::ExprString { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprString(nix::ExprString E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprString(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) { + v.mkString(s.data()); + } + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprVar : nix::ExprVar { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprVar(nix::ExprVar E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprVar(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; + +struct HookExprWith : nix::ExprWith { + ValueMap &VMap; + EnvMap &EMap; + std::uintptr_t Handle; + HookExprWith(nix::ExprWith E, ValueMap &VMap, EnvMap &EMap, + std::uintptr_t Handle) + : nix::ExprWith(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {} + void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; + std::string getName(); +}; } // namespace nixt diff --git a/libnixt/lib/ASTReader.cpp b/libnixt/lib/ASTReader.cpp index 209a0d5da..66abeeb8f 100644 --- a/libnixt/lib/ASTReader.cpp +++ b/libnixt/lib/ASTReader.cpp @@ -123,7 +123,9 @@ nix::ExprVar ASTDeserializer::eatExprVar(nix::PosIdx Pos, std::string_view &Data) { auto Name = eat(Data); nix::Symbol Sym = Ctx.STable.create(Name); - return {Pos, Sym}; + nix::ExprVar Ret{Pos, Sym}; + Ret.fromWith = false; // nix does not initialized this field! + return Ret; } nix::AttrPath ASTDeserializer::eatAttrPath(std::string_view &Data) {