10#include "lix/libutil/json.hh"
31 std::unique_ptr<Expr> expr;
33 AttrName(
PosIdx pos, std::unique_ptr<Expr> e);
36typedef std::vector<AttrName> AttrPath;
38std::string showAttrPath(
const SymbolTable & symbols,
const AttrPath & attrPath);
39JSON printAttrPathToJson(
const SymbolTable & symbols,
const AttrPath & attrPath);
67struct ExprOpConcatLists;
68struct ExprConcatStrings;
74 virtual void visit(
ExprDebugFrame & e, std::unique_ptr<Expr> & ptr) = 0;
75 virtual void visit(
ExprLiteral & e, std::unique_ptr<Expr> & ptr) = 0;
76 virtual void visit(
ExprVar & e, std::unique_ptr<Expr> & ptr) = 0;
77 virtual void visit(
ExprInheritFrom & e, std::unique_ptr<Expr> & ptr) = 0;
78 virtual void visit(
ExprSelect & e, std::unique_ptr<Expr> & ptr) = 0;
79 virtual void visit(
ExprOpHasAttr & e, std::unique_ptr<Expr> & ptr) = 0;
80 virtual void visit(
ExprSet & e, std::unique_ptr<Expr> & ptr) = 0;
81 virtual void visit(
ExprList & e, std::unique_ptr<Expr> & ptr) = 0;
82 virtual void visit(
ExprLambda & e, std::unique_ptr<Expr> & ptr) = 0;
83 virtual void visit(
ExprCall & e, std::unique_ptr<Expr> & ptr) = 0;
84 virtual void visit(
ExprLet & e, std::unique_ptr<Expr> & ptr) = 0;
85 virtual void visit(
ExprWith & e, std::unique_ptr<Expr> & ptr) = 0;
86 virtual void visit(
ExprIf & e, std::unique_ptr<Expr> & ptr) = 0;
87 virtual void visit(
ExprAssert & e, std::unique_ptr<Expr> & ptr) = 0;
88 virtual void visit(
ExprOpNot & e, std::unique_ptr<Expr> & ptr) = 0;
89 virtual void visit(ExprOpEq & e, std::unique_ptr<Expr> & ptr) = 0;
90 virtual void visit(ExprOpNEq & e, std::unique_ptr<Expr> & ptr) = 0;
91 virtual void visit(ExprOpAnd & e, std::unique_ptr<Expr> & ptr) = 0;
92 virtual void visit(ExprOpOr & e, std::unique_ptr<Expr> & ptr) = 0;
93 virtual void visit(ExprOpImpl & e, std::unique_ptr<Expr> & ptr) = 0;
94 virtual void visit(ExprOpUpdate & e, std::unique_ptr<Expr> & ptr) = 0;
95 virtual void visit(ExprOpConcatLists & e, std::unique_ptr<Expr> & ptr) = 0;
96 virtual void visit(ExprConcatStrings & e, std::unique_ptr<Expr> & ptr) = 0;
97 virtual void visit(
ExprPos & e, std::unique_ptr<Expr> & ptr) = 0;
98 virtual void visit(
ExprBlackHole & e, std::unique_ptr<Expr> & ptr) = 0;
100 void visit(std::unique_ptr<Expr> & ptr);
106 Expr(Expr &&) =
default;
107 Expr & operator=(Expr &&) =
default;
108 Expr(
const PosIdx pos) : pos(pos) {};
112 Symbol sub, lessThan, mul, div, or_, findFile, nixPath, body, overrides;
118 Expr(
const Expr &) =
delete;
119 Expr & operator=(
const Expr &) =
delete;
122 static std::unique_ptr<Expr> finalize(
123 std::unique_ptr<Expr> parsed,
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env
126 virtual JSON toJSON(
const SymbolTable & symbols)
const;
127 virtual void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr) = 0;
130 virtual void setName(
Symbol name);
131 PosIdx getPos()
const {
return pos; }
136 return dynamic_cast<E &
>(*this);
142 return dynamic_cast<E *
>(
this);
146inline void ExprVisitor::visit(std::unique_ptr<Expr> & ptr)
148 ptr->accept(*
this, ptr);
151struct ExprDebugFrame : Expr
153 std::unique_ptr<Expr> inner;
156 ExprDebugFrame(
PosIdx pos, std::unique_ptr<Expr> inner, std::string message)
158 , inner(std::move(inner))
159 , message(std::move(message))
163 JSON toJSON(
const SymbolTable & symbols)
const override
165 return inner->toJSON(symbols);
168 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
171struct ExprLiteral : Expr
175 ExprLiteral(
const PosIdx pos) : Expr(pos) {};
182 JSON toJSON(
const SymbolTable & symbols)
const override;
184 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
187struct ExprString : ExprLiteral
190 ExprString(
const PosIdx pos, std::string &&s) : ExprLiteral(pos), s(std::move(s)) { v.mkString(this->s.data()); };
193struct ExprPath : ExprLiteral
196 ExprPath(
const PosIdx pos, std::string s) : ExprLiteral(pos), s(std::move(s)) { v.mkPath(this->s.c_str()); };
199typedef uint32_t Level;
200typedef uint32_t Displacement;
228 ExprVar(
Symbol name) : name(name), needsRoot(
false) { };
229 ExprVar(
const PosIdx & pos,
Symbol name,
bool needsRoot =
false) : Expr(pos), name(name), needsRoot(needsRoot) { };
231 JSON toJSON(
const SymbolTable & symbols)
const override;
233 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
241struct ExprInheritFrom : Expr
246 ExprInheritFrom(
PosIdx pos, Displacement displ, Expr & fromExpr)
247 : Expr(pos), fromExpr(fromExpr), displ(displ)
251 JSON toJSON(
const SymbolTable & symbols)
const override;
253 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
256struct ExprSelect : Expr
259 std::unique_ptr<Expr>
e;
264 std::unique_ptr<Expr>
def;
271 JSON toJSON(
const SymbolTable & symbols)
const override;
272 void eval(EvalState & state, Env & env, Value & v)
override;
273 void accept(ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
276struct ExprOpHasAttr : Expr
278 std::unique_ptr<Expr> e;
280 ExprOpHasAttr(
const PosIdx & pos, std::unique_ptr<Expr> e, AttrPath attrPath) : Expr(pos), e(std::move(e)), attrPath(std::move(attrPath)) { };
281 JSON toJSON(
const SymbolTable & symbols)
const override;
283 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
289 ExprAttrs() =
default;
290 ExprAttrs(
const ExprAttrs &) =
delete;
291 ExprAttrs & operator=(
const ExprAttrs &) =
delete;
292 ExprAttrs(ExprAttrs &&) =
default;
293 ExprAttrs & operator=(ExprAttrs &&) =
default;
294 virtual ~ExprAttrs() =
default;
307 std::unique_ptr<Expr> e;
311 : kind(kind), e(std::move(e)), pos(pos) { };
315 T chooseByKind(
const T & plain,
const T & inherited,
const T & inheritedFrom)
const
324 return inheritedFrom;
328 typedef std::map<Symbol, AttrDef> AttrDefs;
330 std::unique_ptr<std::list<std::unique_ptr<Expr>>> inheritFromExprs;
331 struct DynamicAttrDef {
332 std::unique_ptr<Expr> nameExpr, valueExpr;
334 DynamicAttrDef(std::unique_ptr<Expr> nameExpr, std::unique_ptr<Expr> valueExpr,
const PosIdx & pos)
335 : nameExpr(std::move(nameExpr)), valueExpr(std::move(valueExpr)), pos(pos) { };
337 typedef std::vector<DynamicAttrDef> DynamicAttrDefs;
338 DynamicAttrDefs dynamicAttrs;
340 std::shared_ptr<const StaticEnv> buildRecursiveEnv(
const std::shared_ptr<const StaticEnv> & env);
343 void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const;
346struct ExprSet : Expr, ExprAttrs {
347 bool recursive =
false;
349 ExprSet(
const PosIdx &pos,
bool recursive =
false) : Expr(pos), recursive(recursive) { };
351 JSON toJSON(
const SymbolTable & symbols)
const override;
353 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
357 std::map<Symbol, std::unique_ptr<Expr>> symbols;
359 void finalize(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env) {
360 for (
auto & [_, e] : symbols)
361 e = Expr::finalize(std::move(e), es, env);
365struct ExprList : Expr
367 std::vector<std::unique_ptr<Expr>> elems;
368 ExprList(
PosIdx pos) : Expr(pos) { };
369 JSON toJSON(
const SymbolTable & symbols)
const override;
371 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
384 virtual std::shared_ptr<const StaticEnv> buildEnv(
const StaticEnv * up) = 0;
388 virtual void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const = 0;
392struct SimplePattern : Pattern
394 SimplePattern() =
default;
399 virtual std::shared_ptr<const StaticEnv> buildEnv(
const StaticEnv * up)
override;
403 virtual void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const override;
413 std::unique_ptr<Expr> def;
416 typedef std::vector<Formal> Formals_;
420 virtual std::shared_ptr<const StaticEnv> buildEnv(
const StaticEnv * up)
override;
424 virtual void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const override;
426 bool has(
Symbol arg)
const
428 auto it = std::lower_bound(formals.begin(), formals.end(), arg,
429 [] (
const Formal & f,
const Symbol & sym) { return f.name < sym; });
430 return it != formals.end() && it->name == arg;
433 std::vector<std::reference_wrapper<const Formal>> lexicographicOrder(
const SymbolTable & symbols)
const
435 std::vector<std::reference_wrapper<const Formal>> result(formals.begin(), formals.end());
436 std::sort(result.begin(), result.end(),
437 [&] (
const Formal & a,
const Formal & b) {
438 std::string_view sa = symbols[a.name], sb = symbols[b.name];
445struct ExprLambda : Expr
451 std::unique_ptr<Pattern> pattern;
452 std::unique_ptr<Expr> body;
453 ExprLambda(
PosIdx pos, std::unique_ptr<Pattern> pattern, std::unique_ptr<Expr> body)
454 : Expr(pos), pattern(std::move(pattern)), body(std::move(body))
458 std::string showNamePos(
const EvalState & state)
const;
466 return symbols[this->name];
469 return "anonymous lambda";
478 return concatStrings(
"'", symbols[this->name],
"'");
481 return "anonymous lambda";
484 JSON toJSON(
const SymbolTable & symbols)
const override;
486 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
489struct ExprCall : Expr
491 std::unique_ptr<Expr> fun;
492 std::vector<std::unique_ptr<Expr>> args;
493 ExprCall(
const PosIdx & pos, std::unique_ptr<Expr> fun, std::vector<std::unique_ptr<Expr>> && args)
494 : Expr(pos), fun(std::move(fun)), args(std::move(args))
496 JSON toJSON(
const SymbolTable & symbols)
const override;
498 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
503 std::unique_ptr<Expr> body;
504 JSON toJSON(
const SymbolTable & symbols)
const override;
506 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
509struct ExprWith : Expr
511 std::unique_ptr<Expr> attrs, body;
513 ExprWith * parentWith;
514 ExprWith(
const PosIdx & pos, std::unique_ptr<Expr> attrs, std::unique_ptr<Expr> body) : Expr(pos), attrs(std::move(attrs)), body(std::move(body)) { };
515 JSON toJSON(
const SymbolTable & symbols)
const override;
517 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
522 std::unique_ptr<Expr> cond, then, else_;
523 ExprIf(
const PosIdx & pos, std::unique_ptr<Expr> cond, std::unique_ptr<Expr> then, std::unique_ptr<Expr> else_) : Expr(pos), cond(std::move(cond)), then(std::move(then)), else_(std::move(else_)) { };
524 JSON toJSON(
const SymbolTable & symbols)
const override;
526 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
529struct ExprAssert : Expr
531 std::unique_ptr<Expr> cond, body;
532 ExprAssert(
const PosIdx & pos, std::unique_ptr<Expr> cond, std::unique_ptr<Expr> body) : Expr(pos), cond(std::move(cond)), body(std::move(body)) { };
533 JSON toJSON(
const SymbolTable & symbols)
const override;
535 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
538struct ExprOpNot : Expr
540 std::unique_ptr<Expr> e;
541 ExprOpNot(
const PosIdx & pos, std::unique_ptr<Expr> e) : Expr(pos), e(std::move(e)) { };
542 JSON toJSON(
const SymbolTable & symbols)
const override;
544 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
547#define MakeBinOp(name, s) \
550 std::unique_ptr<Expr> e1, e2; \
551 name(std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : e1(std::move(e1)), e2(std::move(e2)) { }; \
552 name(const PosIdx & pos, std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : Expr(pos), e1(std::move(e1)), e2(std::move(e2)) { }; \
553 JSON toJSON(const SymbolTable & symbols) const override \
557 {"e1", e1->toJSON(symbols)}, \
558 {"e2", e2->toJSON(symbols)} \
561 void accept(ExprVisitor & ev, std::unique_ptr<Expr> & ptr) override { ev.visit(*this, ptr); } \
562 void eval(EvalState & state, Env & env, Value & v) override; \
565MakeBinOp(ExprOpEq,
"==")
566MakeBinOp(ExprOpNEq,
"!=")
567MakeBinOp(ExprOpAnd,
"&&")
568MakeBinOp(ExprOpOr,
"||")
569MakeBinOp(ExprOpImpl,
"->")
570MakeBinOp(ExprOpUpdate,
"//")
571MakeBinOp(ExprOpConcatLists,
"++")
573struct ExprConcatStrings :
Expr
576 std::vector<std::pair<PosIdx, std::unique_ptr<Expr>>> es;
577 ExprConcatStrings(
const PosIdx & pos,
bool forceString, std::vector<std::pair<
PosIdx, std::unique_ptr<Expr>>> es)
578 :
Expr(pos), forceString(forceString), es(std::move(es)) { };
579 JSON toJSON(
const SymbolTable & symbols)
const override;
580 void eval(EvalState & state, Env & env, Value & v)
override;
581 void accept(ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
586 ExprPos(
const PosIdx & pos) : Expr(pos) { };
587 JSON toJSON(
const SymbolTable & symbols)
const override;
589 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
596 void accept(
ExprVisitor & ev, std::unique_ptr<Expr> & ptr)
override { ev.visit(*
this, ptr); }
608 const StaticEnv * up;
611 typedef std::vector<std::pair<Symbol, Displacement>> Vars;
617 StaticEnv(
ExprWith * isWith,
const StaticEnv * up,
size_t expectedSize = 0) : isWith(isWith), up(up) {
618 vars.reserve(expectedSize);
623 std::stable_sort(vars.begin(), vars.end(),
624 [](
const Vars::value_type & a,
const Vars::value_type & b) { return a.first < b.first; });
629 auto it = vars.begin(), jt = it, end = vars.end();
632 while (jt != end && it->first == jt->first) *it = *jt++;
638 Vars::const_iterator find(
Symbol name)
const
640 Vars::value_type key(name, 0);
641 auto i = std::lower_bound(vars.begin(), vars.end(), key);
642 if (i != vars.end() && i->first == name)
return i;
Definition symbol-table.hh:73
Definition symbol-table.hh:50
Definition nixexpr.hh:408
virtual Env & match(ExprLambda &lambda, EvalState &state, Env &up, Value *arg, const PosIdx pos) override
Definition eval.cc:1487
Definition nixexpr.hh:530
Definition nixexpr.hh:296
Kind
Definition nixexpr.hh:297
@ Plain
Definition nixexpr.hh:299
@ Inherited
Definition nixexpr.hh:301
@ InheritedFrom
Definition nixexpr.hh:303
Definition nixexpr.hh:594
Definition nixexpr.hh:490
Definition nixexpr.hh:152
Definition nixexpr.hh:521
Definition nixexpr.hh:242
Definition nixexpr.hh:446
Symbol name
Definition nixexpr.hh:450
std::string getQuotedName(SymbolTable const &symbols) const
Definition nixexpr.hh:475
std::string getName(SymbolTable const &symbols) const
Definition nixexpr.hh:463
Definition nixexpr.hh:502
Definition nixexpr.hh:366
Definition nixexpr.hh:172
Definition nixexpr.hh:277
Definition nixexpr.hh:539
Definition nixexpr.hh:194
Definition nixexpr.hh:585
Definition nixexpr.hh:356
Definition nixexpr.hh:257
std::unique_ptr< Expr > e
Definition nixexpr.hh:259
AttrPath attrPath
Definition nixexpr.hh:267
std::unique_ptr< Expr > def
Definition nixexpr.hh:264
Definition nixexpr.hh:346
Definition nixexpr.hh:188
Definition nixexpr.hh:203
Definition nixexpr.hh:510
Definition nixexpr.hh:111
Definition nixexpr.hh:104
Definition nixexpr.hh:375
Symbol name
Definition nixexpr.hh:378
Definition nixexpr.hh:606