9#include "eval-error.hh"
56 operator bool()
const {
return static_cast<bool>(
begin); }
58 std::string getInnerText(
const PosTable & positions)
const;
70 AttrName(
Expr * e) : expr(e) {};
73typedef std::vector<AttrName> AttrPath;
83 Symbol sub, lessThan, mul, div, or_, findFile, nixPath, body;
87 static unsigned long nrExprs;
93 virtual void bindVars(
EvalState & es,
const std::shared_ptr<const StaticEnv> & env);
94 virtual void eval(
EvalState & state, Env & env, Value & v);
95 virtual Value * maybeThunk(
EvalState & state, Env & env);
96 virtual void setName(Symbol
name);
97 virtual void setDocComment(DocComment docComment) { };
98 virtual PosIdx getPos()
const {
return noPos; }
101 virtual void resetCursedOr() { };
102 virtual void warnIfCursedOr(
const SymbolTable &
symbols,
const PosTable & positions) { };
105#define COMMON_METHODS \
106 void show(const SymbolTable & symbols, std::ostream & str) const override; \
107 void eval(EvalState & state, Env & env, Value & v) override; \
108 void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override;
113 ExprInt(NixInt n) { v.mkInt(n); };
114 ExprInt(NixInt::Inner n) { v.mkInt(n); };
119struct ExprFloat : Expr
122 ExprFloat(NixFloat nf) { v.mkFloat(nf); };
127struct ExprString : Expr
131 ExprString(std::string &&s) : s(std::move(s)) { v.mkString(this->s.data()); };
136struct ExprPath : Expr
141 ExprPath(
ref<SourceAccessor> accessor, std::string s) : accessor(accessor), s(std::move(s))
143 v.mkPath(&*accessor, this->s.c_str());
149typedef uint32_t Level;
150typedef uint32_t Displacement;
171 Displacement displ = 0;
173 ExprVar(
Symbol name) : name(name) { };
174 ExprVar(
const PosIdx & pos,
Symbol name) : pos(pos), name(name) { };
176 PosIdx getPos()
const override {
return pos; }
185struct ExprInheritFrom : ExprVar
187 ExprInheritFrom(
PosIdx pos, Displacement displ): ExprVar(pos, {})
191 this->fromWith =
nullptr;
194 void bindVars(
EvalState & es,
const std::shared_ptr<const StaticEnv> & env)
override;
197struct ExprSelect : Expr
202 ExprSelect(
const PosIdx & pos, Expr * e, AttrPath attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(std::move(attrPath)) { };
204 PosIdx getPos()
const override {
return pos; }
219struct ExprOpHasAttr : Expr
223 ExprOpHasAttr(Expr * e, AttrPath attrPath) : e(e), attrPath(std::move(attrPath)) { };
224 PosIdx getPos()
const override {
return e->getPos(); }
228struct ExprAttrs : Expr
245 Displacement displ = 0;
247 : kind(kind), e(e), pos(pos) { };
251 const T & chooseByKind(
const T & plain,
const T & inherited,
const T & inheritedFrom)
const
260 return inheritedFrom;
264 typedef std::map<Symbol, AttrDef> AttrDefs;
266 std::unique_ptr<std::vector<Expr *>> inheritFromExprs;
267 struct DynamicAttrDef {
268 Expr * nameExpr, * valueExpr;
270 DynamicAttrDef(Expr * nameExpr, Expr * valueExpr,
const PosIdx & pos)
271 : nameExpr(nameExpr), valueExpr(valueExpr), pos(pos) { };
273 typedef std::vector<DynamicAttrDef> DynamicAttrDefs;
274 DynamicAttrDefs dynamicAttrs;
275 ExprAttrs(
const PosIdx &pos) : recursive(false), pos(pos) { };
277 PosIdx getPos()
const override {
return pos; }
280 std::shared_ptr<const StaticEnv> bindInheritSources(
281 EvalState & es,
const std::shared_ptr<const StaticEnv> & env);
282 Env * buildInheritFromEnv(
EvalState & state, Env & up);
286struct ExprList : Expr
288 std::vector<Expr *> elems;
293 PosIdx getPos()
const override
295 return elems.empty() ? noPos : elems.front()->getPos();
308 typedef std::vector<Formal> Formals_;
312 bool has(
Symbol arg)
const
314 auto it = std::lower_bound(formals.begin(), formals.end(), arg,
315 [] (
const Formal &
f,
const Symbol & sym) { return f.name < sym; });
316 return it != formals.end() && it->name == arg;
321 std::vector<Formal> result(formals.begin(), formals.end());
322 std::sort(result.begin(), result.end(),
324 std::string_view sa = symbols[a.name], sb = symbols[b.name];
331struct ExprLambda : Expr
341 : pos(pos), arg(arg), formals(formals), body(body)
345 : pos(pos), formals(formals), body(body)
348 void setName(
Symbol name)
override;
349 std::string showNamePos(
const EvalState & state)
const;
350 inline bool hasFormals()
const {
return formals !=
nullptr; }
351 PosIdx getPos()
const override {
return pos; }
352 virtual void setDocComment(
DocComment docComment)
override;
356struct ExprCall : Expr
359 std::vector<Expr *> args;
361 std::optional<PosIdx> cursedOrEndPos;
362 ExprCall(
const PosIdx & pos, Expr * fun, std::vector<Expr *> && args)
363 : fun(fun), args(args), pos(pos), cursedOrEndPos({})
365 ExprCall(
const PosIdx & pos, Expr * fun, std::vector<Expr *> && args,
PosIdx && cursedOrEndPos)
366 : fun(fun), args(args), pos(pos), cursedOrEndPos(cursedOrEndPos)
368 PosIdx getPos()
const override {
return pos; }
369 virtual void resetCursedOr()
override;
378 ExprLet(
ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
382struct ExprWith : Expr
385 Expr * attrs, * body;
387 ExprWith * parentWith;
388 ExprWith(
const PosIdx & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
389 PosIdx getPos()
const override {
return pos; }
396 Expr * cond, * then, * else_;
397 ExprIf(
const PosIdx & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { };
398 PosIdx getPos()
const override {
return pos; }
402struct ExprAssert : Expr
406 ExprAssert(
const PosIdx & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
407 PosIdx getPos()
const override {
return pos; }
411struct ExprOpNot : Expr
414 ExprOpNot(Expr * e) : e(e) { };
415 PosIdx getPos()
const override {
return e->getPos(); }
419#define MakeBinOp(name, s) \
424 name(Expr * e1, Expr * e2) : e1(e1), e2(e2) { }; \
425 name(const PosIdx & pos, Expr * e1, Expr * e2) : pos(pos), e1(e1), e2(e2) { }; \
426 void show(const SymbolTable & symbols, std::ostream & str) const override \
428 str << "("; e1->show(symbols, str); str << " " s " "; e2->show(symbols, str); str << ")"; \
430 void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override \
432 e1->bindVars(es, env); e2->bindVars(es, env); \
434 void eval(EvalState & state, Env & env, Value & v) override; \
435 PosIdx getPos() const override { return pos; } \
438MakeBinOp(ExprOpEq,
"==")
439MakeBinOp(ExprOpNEq,
"!=")
440MakeBinOp(ExprOpAnd,
"&&")
441MakeBinOp(ExprOpOr,
"||")
442MakeBinOp(ExprOpImpl,
"->")
443MakeBinOp(ExprOpUpdate,
"//")
444MakeBinOp(ExprOpConcatLists,
"++")
446struct ExprConcatStrings : Expr
450 std::vector<std::pair<PosIdx, Expr *>> * es;
451 ExprConcatStrings(
const PosIdx & pos,
bool forceString, std::vector<std::pair<PosIdx, Expr *>> * es)
452 : pos(pos), forceString(forceString), es(es) { };
453 PosIdx getPos()
const override {
return pos; }
460 ExprPos(
const PosIdx & pos) : pos(pos) { };
461 PosIdx getPos()
const override {
return pos; }
470 void bindVars(
EvalState & es,
const std::shared_ptr<const StaticEnv> & env)
override {}
471 [[noreturn]]
static void throwInfiniteRecursionError(
EvalState & state,
Value & v);
483 const StaticEnv * up;
486 typedef std::vector<std::pair<Symbol, Displacement>> Vars;
489 StaticEnv(
ExprWith * isWith,
const StaticEnv * up,
size_t expectedSize = 0) : isWith(isWith), up(up) {
490 vars.reserve(expectedSize);
495 std::stable_sort(vars.begin(), vars.end(),
496 [](
const Vars::value_type & a,
const Vars::value_type & b) { return a.first < b.first; });
501 auto it = vars.begin(), jt = it,
end = vars.end();
504 while (jt !=
end && it->first == jt->first) *it = *jt++;
513 auto i = std::lower_bound(vars.begin(), vars.end(),
key);
514 if (
i != vars.end() &&
i->first ==
name)
return i;
Definition pos-table.hh:13
Definition symbol-table.hh:82
Definition symbol-table.hh:58
PosIdx end
Definition lexer.l:5814
friend class EvalState
Definition lexer.l:5728
const T::key_type & key
Definition lexer.l:2763
friend class SymbolTable
Definition lexer.l:947
auto i
Definition lexer.l:2745
return s
Definition lexer.l:459
std::ostream & str
Definition lexer.l:1728
const std::string_view & name
Definition lexer.l:1709
std::unordered_map< std::string_view, std::pair< const std::string *, uint32_t > > symbols
Definition lexer.l:1010
Definition nixexpr.hh:232
Kind
Definition nixexpr.hh:233
@ Plain
Definition nixexpr.hh:235
@ Inherited
Definition nixexpr.hh:237
@ InheritedFrom
Definition nixexpr.hh:239
Definition nixexpr.hh:229
Definition nixexpr.hh:467
Symbol evalExceptFinalSelect(EvalState &state, Env &env, Value &attrs)
Definition eval.cc:1436
Definition nixexpr.hh:383
Definition nixexpr.hh:481