8#include <memory_resource>
11#include "nix/expr/gc-small-vector.hh"
14#include "nix/expr/eval-error.hh"
17#include "nix/expr/counter.hh"
70 return static_cast<bool>(
begin);
73 std::string getInnerText(
const PosTable & positions)
const;
82 Expr * expr =
nullptr;
89static_assert(std::is_trivially_copy_constructible_v<AttrName>);
91typedef std::vector<AttrName> AttrSelectionPath;
93std::string showAttrSelectionPath(
const SymbolTable & symbols, std::span<const AttrName> attrPath);
95using UpdateQueue = SmallTemporaryValueVector<conservativeStackReservation>;
103 Symbol sub, lessThan, mul, div, or_, findFile, nixPath, body;
114 virtual void show(
const SymbolTable & symbols, std::ostream & str)
const;
115 virtual void bindVars(EvalState & es,
const std::shared_ptr<const StaticEnv> & env);
118 virtual void eval(EvalState & state, Env & env, Value & v);
121 void eval(EvalState & state, Env & env, Value & v, std::string_view errorCtx);
129 virtual Value *
maybeThunk(EvalState & state, Env & env);
137 virtual void evalForUpdate(EvalState & state, Env & env, UpdateQueue & q, std::string_view errorCtx);
138 virtual void setName(Symbol name);
139 virtual void setDocComment(DocComment docComment) {};
141 virtual PosIdx getPos()
const
147 virtual void resetCursedOr() {};
148 virtual void warnIfCursedOr(
const SymbolTable & symbols,
const PosTable & positions) {};
151#define COMMON_METHODS \
152 void show(const SymbolTable & symbols, std::ostream & str) const override; \
153 void eval(EvalState & state, Env & env, Value & v) override; \
154 void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override;
165 ExprInt(NixInt::Inner n)
174struct ExprFloat : Expr
178 ExprFloat(NixFloat nf)
200 ExprString(std::pmr::polymorphic_allocator<char> & alloc, std::string_view sv)
202 if (sv.size() == 0) {
203 v.mkStringNoCopy(
""_sds);
209 Value *
maybeThunk(EvalState & state, Env & env)
override;
213struct ExprPath : Expr
218 ExprPath(std::pmr::polymorphic_allocator<char> & alloc,
ref<SourceAccessor> accessor, std::string_view sv)
228typedef uint32_t Level;
229typedef uint32_t Displacement;
250 Displacement displ = 0;
259 PosIdx getPos()
const override
272struct ExprInheritFrom : ExprVar
274 ExprInheritFrom(
PosIdx pos, Displacement displ)
279 this->fromWith =
nullptr;
282 void bindVars(
EvalState & es,
const std::shared_ptr<const StaticEnv> & env)
override;
285struct ExprSelect : Expr
293 std::pmr::polymorphic_allocator<char> & alloc,
296 std::span<const AttrName> attrPath,
299 , nAttrPath(attrPath.size())
302 , attrPathStart(alloc.allocate_object<
AttrName>(nAttrPath))
304 std::ranges::copy(attrPath, attrPathStart);
307 ExprSelect(std::pmr::polymorphic_allocator<char> & alloc,
const PosIdx & pos, Expr * e,
Symbol name)
312 , attrPathStart((alloc.allocate_object<
AttrName>()))
317 PosIdx getPos()
const override
327 return {attrPathStart, nAttrPath};
344struct ExprOpHasAttr : Expr
347 std::span<AttrName> attrPath;
349 ExprOpHasAttr(std::pmr::polymorphic_allocator<char> & alloc, Expr * e, std::span<AttrName> attrPath)
351 , attrPath({alloc.allocate_object<
AttrName>(attrPath.size()), attrPath.size()})
353 std::ranges::copy(attrPath, this->attrPath.begin());
356 PosIdx getPos()
const override
364struct ExprAttrs : Expr
383 Displacement displ = 0;
391 const T & chooseByKind(
const T & plain,
const T & inherited,
const T & inheritedFrom)
const
400 return inheritedFrom;
405 typedef std::pmr::map<Symbol, AttrDef> AttrDefs;
411 std::unique_ptr<std::pmr::vector<Expr *>> inheritFromExprs;
413 struct DynamicAttrDef
415 Expr *nameExpr, *valueExpr;
417 DynamicAttrDef(Expr * nameExpr, Expr * valueExpr,
const PosIdx & pos)
419 , valueExpr(valueExpr)
423 typedef std::pmr::vector<DynamicAttrDef> DynamicAttrDefs;
428 ExprAttrs(
const PosIdx & pos)
438 PosIdx getPos()
const override
445 std::shared_ptr<const StaticEnv> bindInheritSources(EvalState & es,
const std::shared_ptr<const StaticEnv> & env);
446 Env * buildInheritFromEnv(EvalState & state, Env & up);
447 void showBindings(
const SymbolTable & symbols, std::ostream & str)
const;
448 void moveDataToAllocator(std::pmr::polymorphic_allocator<char> & alloc);
451struct ExprList : Expr
453 std::span<Expr *> elems;
455 ExprList(std::pmr::polymorphic_allocator<char> & alloc, std::span<Expr *> exprs)
456 : elems({alloc.allocate_object<Expr *>(exprs.size()), exprs.size()})
458 std::ranges::copy(exprs, elems.begin());
464 PosIdx getPos()
const override
466 return elems.empty() ? noPos : elems.front()->getPos();
479 typedef std::vector<Formal> Formals_;
486 bool has(
Symbol arg)
const
488 auto it = std::lower_bound(
490 return it !=
formals.end() && it->name == arg;
496 std::span<Formal> formals;
499 Formals(std::span<Formal> formals,
bool ellipsis)
501 , ellipsis(ellipsis) {};
503 bool has(
Symbol arg)
const
505 auto it = std::lower_bound(
506 formals.begin(), formals.end(), arg, [](
const Formal & f,
const Symbol & sym) { return f.name < sym; });
507 return it != formals.end() && it->name == arg;
510 std::vector<Formal> lexicographicOrder(
const SymbolTable & symbols)
const
512 std::vector<Formal> result(formals.begin(), formals.end());
513 std::sort(result.begin(), result.end(), [&](
const Formal & a,
const Formal & b) {
514 std::string_view sa = symbols[a.name], sb = symbols[b.name];
521struct ExprLambda : Expr
534 std::optional<Formals> getFormals()
const
537 return Formals{{formalsStart, nFormals}, ellipsis};
547 std::pmr::polymorphic_allocator<char> & alloc,
555 , ellipsis(formals.ellipsis)
556 , nFormals(formals.
formals.size())
557 , formalsStart(alloc.allocate_object<
Formal>(nFormals))
560 if (formals.
formals.size() > nFormals) [[unlikely]] {
562 "too many formal arguments, implementation supports at most %1%",
563 std::numeric_limits<
decltype(nFormals)>::max());
565 err.atPos(positions[pos]);
566 throw std::move(err);
568 std::uninitialized_copy_n(formals.
formals.begin(), nFormals, formalsStart);
577 , formalsStart(
nullptr)
582 std::pmr::polymorphic_allocator<char> & alloc,
586 : ExprLambda(positions, alloc, pos,
Symbol(), formals, body) {};
588 void setName(
Symbol name)
override;
589 std::string showNamePos(
const EvalState & state)
const;
591 PosIdx getPos()
const override
596 virtual void setDocComment(
DocComment docComment)
override;
600struct ExprCall : Expr
606 std::optional<std::pmr::vector<Expr *>>
args;
608 std::optional<PosIdx> cursedOrEndPos;
610 ExprCall(
const PosIdx & pos, Expr * fun, std::pmr::vector<Expr *> &&
args)
618 ExprCall(
const PosIdx & pos, Expr * fun, std::pmr::vector<Expr *> &&
args, PosIdx && cursedOrEndPos)
622 , cursedOrEndPos(cursedOrEndPos)
626 PosIdx getPos()
const override
631 virtual void resetCursedOr()
override;
632 virtual void warnIfCursedOr(
const SymbolTable & symbols,
const PosTable & positions)
override;
633 void moveDataToAllocator(std::pmr::polymorphic_allocator<char> & alloc);
647struct ExprWith : Expr
652 ExprWith * parentWith;
653 ExprWith(
const PosIdx & pos, Expr * attrs, Expr * body)
658 PosIdx getPos()
const override
669 Expr *cond, *then, *else_;
670 ExprIf(
const PosIdx & pos, Expr * cond, Expr * then, Expr * else_)
676 PosIdx getPos()
const override
684struct ExprAssert : Expr
688 ExprAssert(
const PosIdx & pos, Expr * cond, Expr * body)
693 PosIdx getPos()
const override
701struct ExprOpNot : Expr
707 PosIdx getPos()
const override
715#define MakeBinOpMembers(name, s) \
718 name(Expr * e1, Expr * e2) \
721 name(const PosIdx & pos, Expr * e1, Expr * e2) \
725 void show(const SymbolTable & symbols, std::ostream & str) const override \
728 e1->show(symbols, str); \
730 e2->show(symbols, str); \
733 void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override \
735 e1->bindVars(es, env); \
736 e2->bindVars(es, env); \
738 void eval(EvalState & state, Env & env, Value & v) override; \
739 PosIdx getPos() const override \
744#define MakeBinOp(name, s) \
747 MakeBinOpMembers(name, s) \
750MakeBinOp(ExprOpEq,
"==");
751MakeBinOp(ExprOpNEq,
"!=");
752MakeBinOp(ExprOpAnd,
"&&");
753MakeBinOp(ExprOpOr,
"||");
754MakeBinOp(ExprOpImpl,
"->");
755MakeBinOp(ExprOpConcatLists,
"++");
757struct ExprOpUpdate : Expr
762 void evalForUpdate(
EvalState & state,
Env & env, UpdateQueue & q);
765 MakeBinOpMembers(ExprOpUpdate,
"//");
766 virtual void evalForUpdate(
EvalState & state,
Env & env, UpdateQueue & q, std::string_view errorCtx)
override;
769struct ExprConcatStrings : Expr
773 std::span<std::pair<PosIdx, Expr *>> es;
776 std::pmr::polymorphic_allocator<char> & alloc,
779 std::span<std::pair<PosIdx, Expr *>> es)
781 , forceString(forceString)
782 , es({alloc.allocate_object<std::pair<PosIdx, Expr *>>(es.size()), es.size()})
784 std::ranges::copy(es, this->es.begin());
788 std::pmr::polymorphic_allocator<char> & alloc,
791 std::initializer_list<std::pair<PosIdx, Expr *>> es)
793 , forceString(forceString)
794 , es({alloc.allocate_object<std::pair<PosIdx, Expr *>>(es.size()), es.size()})
796 std::ranges::copy(es, this->es.begin());
799 PosIdx getPos()
const override
810 ExprPos(
const PosIdx & pos)
813 PosIdx getPos()
const override
824 void show(
const SymbolTable & symbols, std::ostream & str)
const override {}
828 void bindVars(
EvalState & es,
const std::shared_ptr<const StaticEnv> & env)
override {}
830 [[noreturn]]
static void throwInfiniteRecursionError(
EvalState & state,
Value & v);
838 std::pmr::synchronized_pool_resource fallbackResource;
842 std::pmr::polymorphic_allocator<char> alloc{&buffer};
845 [[gnu::always_inline]]
846 C * add(
auto &&... args)
848 return alloc.new_object<C>(std::forward<decltype(args)>(args)...);
853 [[gnu::always_inline]]
854 C * add(
const PosIdx & pos,
Expr *
fun, std::pmr::vector<Expr *> && args)
855 requires(std::same_as<C, ExprCall>)
857 return alloc.new_object<C>(pos,
fun, std::move(args));
861 [[gnu::always_inline]]
862 C * add(
const PosIdx & pos,
Expr *
fun, std::pmr::vector<Expr *> && args,
PosIdx && cursedOrEndPos)
863 requires(std::same_as<C, ExprCall>)
865 return alloc.new_object<C>(pos,
fun, std::move(args), std::move(cursedOrEndPos));
869 [[gnu::always_inline]]
871 add(std::pmr::polymorphic_allocator<char> & alloc,
874 std::span<std::pair<PosIdx, Expr *>> es)
875 requires(std::same_as<C, ExprConcatStrings>)
877 return alloc.new_object<C>(alloc, pos, forceString, es);
881 [[gnu::always_inline]]
883 add(std::pmr::polymorphic_allocator<char> & alloc,
886 std::initializer_list<std::pair<PosIdx, Expr *>> es)
887 requires(std::same_as<C, ExprConcatStrings>)
889 return alloc.new_object<C>(alloc, pos, forceString, es);
899 std::shared_ptr<const StaticEnv> up;
902 typedef std::vector<std::pair<Symbol, Displacement>> Vars;
905 StaticEnv(
ExprWith * isWith, std::shared_ptr<const StaticEnv> up,
size_t expectedSize = 0)
909 vars.reserve(expectedSize);
914 std::stable_sort(vars.begin(), vars.end(), [](
const Vars::value_type & a,
const Vars::value_type & b) {
915 return a.first < b.first;
921 auto it = vars.begin(), jt = it, end = vars.end();
924 while (jt != end && it->first == jt->first)
931 Vars::const_iterator find(
Symbol name)
const
933 Vars::value_type key(name, 0);
934 auto i = std::lower_bound(vars.begin(), vars.end(), key);
935 if (i != vars.end() && i->first == name)
Definition bump-memory-resource.hh:15
Definition nixexpr.hh:836
Definition pos-table.hh:15
static const StringData & make(EvalMemory &mem, std::string_view s)
Definition eval.cc:98
Definition symbol-table.hh:266
Definition symbol-table.hh:24
This file defines two main structs/classes used in nix error handling.
Definition nixexpr.hh:370
Kind
Definition nixexpr.hh:371
@ Plain
Definition nixexpr.hh:373
@ Inherited
Definition nixexpr.hh:375
@ InheritedFrom
Definition nixexpr.hh:377
Definition nixexpr.hh:365
std::optional< DynamicAttrDefs > dynamicAttrs
Definition nixexpr.hh:427
std::optional< AttrDefs > attrs
Definition nixexpr.hh:410
Definition nixexpr.hh:823
void eval(EvalState &state, Env &env, Value &v) override
Definition eval.cc:2239
std::optional< std::pmr::vector< Expr * > > args
Definition nixexpr.hh:606
Value * maybeThunk(EvalState &state, Env &env) override
Definition eval.cc:1103
Value * maybeThunk(EvalState &state, Env &env) override
Definition eval.cc:1097
Value * maybeThunk(EvalState &state, Env &env) override
Definition eval.cc:1428
Value * maybeThunk(EvalState &state, Env &env) override
Definition eval.cc:1109
std::span< const AttrName > getAttrPath() const
Definition nixexpr.hh:325
Symbol evalExceptFinalSelect(EvalState &state, Env &env, Value &attrs)
Definition eval.cc:1537
Value * maybeThunk(EvalState &state, Env &env) override
Definition eval.cc:1091
ExprString(const StringData &s)
Definition nixexpr.hh:195
Value * maybeThunk(EvalState &state, Env &env) override
Definition eval.cc:1079
Definition nixexpr.hh:648
Definition nixexpr.hh:102
Definition nixexpr.hh:100
virtual void eval(EvalState &state, Env &env, Value &v)
Definition eval.cc:1258
virtual void evalForUpdate(EvalState &state, Env &env, UpdateQueue &q, std::string_view errorCtx)
Definition eval.cc:2072
virtual Value * maybeThunk(EvalState &state, Env &env)
Definition eval.cc:1072
Definition nixexpr.hh:897