10#include "lix/libutil/json.hh"
33 std::unique_ptr<Expr> expr;
38typedef std::vector<AttrName> AttrPath;
40std::string showAttrPath(
const SymbolTable & symbols,
const AttrPath & attrPath);
41JSON printAttrPathToJson(
const SymbolTable & symbols,
const AttrPath & attrPath);
55 Symbol sub, lessThan, mul, div, or_, findFile, nixPath, body, overrides;
62 Expr & operator=(
const Expr &) =
delete;
65 virtual JSON toJSON(
const SymbolTable & symbols)
const;
66 virtual void bindVars(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env);
69 virtual void setName(
Symbol name);
70 PosIdx getPos()
const {
return pos; }
73#define COMMON_METHODS \
74 JSON toJSON(const SymbolTable & symbols) const override; \
75 void eval(EvalState & state, Env & env, Value & v) override; \
76 void bindVars(Evaluator & es, const std::shared_ptr<const StaticEnv> & env) override;
104typedef uint32_t Level;
105typedef uint32_t Displacement;
134 ExprVar(
const PosIdx & pos,
Symbol name,
bool needsRoot =
false) :
Expr(pos), name(name), needsRoot(needsRoot) { };
149 :
ExprVar(pos, {}), fromExpr(fromExpr)
153 this->fromWith =
nullptr;
156 JSON toJSON(
SymbolTable const & symbols)
const override;
157 void bindVars(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env)
override;
163 std::unique_ptr<Expr>
e;
168 std::unique_ptr<Expr>
def;
180 std::unique_ptr<Expr> e;
182 ExprOpHasAttr(
const PosIdx & pos, std::unique_ptr<Expr> e, AttrPath attrPath) :
Expr(pos), e(std::move(e)), attrPath(std::move(attrPath)) { };
207 std::unique_ptr<Expr> e;
211 : kind(kind), e(std::move(e)), pos(pos) { };
215 const T & chooseByKind(
const T & plain,
const T & inherited,
const T & inheritedFrom)
const
224 return inheritedFrom;
228 typedef std::map<Symbol, AttrDef> AttrDefs;
230 std::unique_ptr<std::vector<ref<Expr>>> inheritFromExprs;
232 std::unique_ptr<Expr> nameExpr, valueExpr;
234 DynamicAttrDef(std::unique_ptr<Expr> nameExpr, std::unique_ptr<Expr> valueExpr,
const PosIdx & pos)
235 : nameExpr(std::move(nameExpr)), valueExpr(std::move(valueExpr)), pos(pos) { };
237 typedef std::vector<DynamicAttrDef> DynamicAttrDefs;
238 DynamicAttrDefs dynamicAttrs;
240 std::shared_ptr<const StaticEnv> bindInheritSources(
241 Evaluator & es,
const std::shared_ptr<const StaticEnv> & env);
243 void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const;
247 bool recursive =
false;
249 ExprSet(
const PosIdx &pos,
bool recursive =
false) :
Expr(pos), recursive(recursive) { };
256 std::vector<std::unique_ptr<Expr>> elems;
271 virtual std::shared_ptr<const StaticEnv> buildEnv(
const StaticEnv * up) = 0;
272 virtual void bindVars(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env) = 0;
275 virtual void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const = 0;
286 virtual std::shared_ptr<const StaticEnv> buildEnv(
const StaticEnv * up)
override;
287 virtual void bindVars(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env)
override;
290 virtual void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const override;
300 std::unique_ptr<Expr> def;
303 typedef std::vector<Formal> Formals_;
307 virtual std::shared_ptr<const StaticEnv> buildEnv(
const StaticEnv * up)
override;
308 virtual void bindVars(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env)
override;
311 virtual void addBindingsToJSON(JSON & out,
const SymbolTable & symbols)
const override;
313 bool has(
Symbol arg)
const
315 auto it = std::lower_bound(formals.begin(), formals.end(), arg,
316 [] (
const Formal & f,
const Symbol & sym) { return f.name < sym; });
317 return it != formals.end() && it->name == arg;
320 std::vector<std::reference_wrapper<const Formal>> lexicographicOrder(
const SymbolTable & symbols)
const
322 std::vector<std::reference_wrapper<const Formal>> result(formals.begin(), formals.end());
323 std::sort(result.begin(), result.end(),
324 [&] (
const Formal & a,
const Formal & b) {
325 std::string_view sa = symbols[a.name], sb = symbols[b.name];
338 std::unique_ptr<Pattern> pattern;
339 std::unique_ptr<Expr> body;
340 ExprLambda(
PosIdx pos, std::unique_ptr<Pattern> pattern, std::unique_ptr<Expr> body)
341 :
Expr(pos), pattern(std::move(pattern)), body(std::move(body))
345 std::string showNamePos(
const EvalState & state)
const;
353 return symbols[this->
name];
356 return "anonymous lambda";
365 return concatStrings(
"'", symbols[this->name],
"'");
368 return "anonymous lambda";
376 std::unique_ptr<Expr> fun;
377 std::vector<std::unique_ptr<Expr>> args;
378 ExprCall(
const PosIdx & pos, std::unique_ptr<Expr> fun, std::vector<std::unique_ptr<Expr>> && args)
379 :
Expr(pos), fun(std::move(fun)), args(std::move(args))
386 std::unique_ptr<Expr> body;
392 std::unique_ptr<Expr> attrs, body;
395 ExprWith(
const PosIdx & pos, std::unique_ptr<Expr> attrs, std::unique_ptr<Expr> body) :
Expr(pos), attrs(std::move(attrs)), body(std::move(body)) { };
401 std::unique_ptr<Expr> cond, then, else_;
402 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_)) { };
408 std::unique_ptr<Expr> cond, body;
409 ExprAssert(
const PosIdx & pos, std::unique_ptr<Expr> cond, std::unique_ptr<Expr> body) :
Expr(pos), cond(std::move(cond)), body(std::move(body)) { };
415 std::unique_ptr<Expr> e;
420#define MakeBinOp(name, s) \
423 std::unique_ptr<Expr> e1, e2; \
424 name(std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : e1(std::move(e1)), e2(std::move(e2)) { }; \
425 name(const PosIdx & pos, std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : Expr(pos), e1(std::move(e1)), e2(std::move(e2)) { }; \
426 JSON toJSON(const SymbolTable & symbols) const override \
430 {"e1", e1->toJSON(symbols)}, \
431 {"e2", e2->toJSON(symbols)} \
434 void bindVars(Evaluator & es, const std::shared_ptr<const StaticEnv> & env) override \
436 e1->bindVars(es, env); e2->bindVars(es, env); \
438 void eval(EvalState & state, Env & env, Value & v) override; \
441MakeBinOp(ExprOpEq,
"==")
442MakeBinOp(ExprOpNEq, "!=")
443MakeBinOp(ExprOpAnd, "&&")
444MakeBinOp(ExprOpOr, "||")
445MakeBinOp(ExprOpImpl, "->")
446MakeBinOp(ExprOpUpdate, "
447MakeBinOp(ExprOpConcatLists, "++")
449struct ExprConcatStrings :
Expr
452 std::vector<std::pair<PosIdx, std::unique_ptr<Expr>>> es;
453 ExprConcatStrings(
const PosIdx & pos,
bool forceString, std::vector<std::pair<
PosIdx, std::unique_ptr<Expr>>> es)
454 :
Expr(pos), forceString(forceString), es(std::move(es)) { };
468 void bindVars(
Evaluator & es,
const std::shared_ptr<const StaticEnv> & env)
override {}
483 typedef std::vector<std::pair<Symbol, Displacement>> Vars;
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++;
510 Vars::const_iterator find(
Symbol name)
const
512 Vars::value_type key(name, 0);
513 auto i = std::lower_bound(vars.begin(), vars.end(), key);
514 if (i != vars.end() && i->first == name)
return i;
Definition symbol-table.hh:75
Definition symbol-table.hh:52
This file defines two main structs/classes used in nix error handling.
Definition nixexpr.hh:295
virtual Env & match(ExprLambda &lambda, EvalState &state, Env &up, Value *arg, const PosIdx pos) override
Definition eval.cc:1499
Definition nixexpr.hh:407
Definition nixexpr.hh:196
Kind
Definition nixexpr.hh:197
Definition nixexpr.hh:231
Definition nixexpr.hh:188
Definition nixexpr.hh:466
Definition nixexpr.hh:375
Definition nixexpr.hh:400
Definition nixexpr.hh:145
Definition nixexpr.hh:333
Symbol name
Definition nixexpr.hh:337
std::string getQuotedName(SymbolTable const &symbols) const
Definition nixexpr.hh:362
std::string getName(SymbolTable const &symbols) const
Definition nixexpr.hh:350
Definition nixexpr.hh:385
Definition nixexpr.hh:255
Definition nixexpr.hh:179
Definition nixexpr.hh:414
Definition nixexpr.hh:459
Definition nixexpr.hh:161
std::unique_ptr< Expr > e
Definition nixexpr.hh:163
AttrPath attrPath
Definition nixexpr.hh:171
std::unique_ptr< Expr > def
Definition nixexpr.hh:168
Definition nixexpr.hh:246
Definition nixexpr.hh:108
Definition nixexpr.hh:391
Definition nixexpr.hh:262
Symbol name
Definition nixexpr.hh:265
Definition nixexpr.hh:280
Definition nixexpr.hh:478