2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2023-04-01 03:18:41 +00:00
|
|
|
///@file
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2022-03-04 18:31:59 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-01-07 17:26:33 +00:00
|
|
|
#include "value.hh"
|
2010-04-13 12:25:42 +00:00
|
|
|
#include "symbol-table.hh"
|
2020-05-11 21:52:15 +00:00
|
|
|
#include "error.hh"
|
2024-03-06 04:24:35 +00:00
|
|
|
#include "position.hh"
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
#include "eval-error.hh"
|
2024-03-08 05:55:47 +00:00
|
|
|
#include "pos-idx.hh"
|
|
|
|
#include "pos-table.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
2003-10-30 16:11:24 +00:00
|
|
|
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Env;
|
|
|
|
struct Value;
|
2014-01-21 17:29:55 +00:00
|
|
|
class EvalState;
|
2024-03-04 06:37:45 +00:00
|
|
|
struct ExprWith;
|
2010-04-14 14:42:32 +00:00
|
|
|
struct StaticEnv;
|
|
|
|
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* An attribute path is a sequence of attribute names.
|
|
|
|
*/
|
2013-12-31 23:56:26 +00:00
|
|
|
struct AttrName
|
|
|
|
{
|
|
|
|
Symbol symbol;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> expr;
|
2022-04-26 11:23:32 +00:00
|
|
|
AttrName(Symbol s) : symbol(s) {};
|
2024-06-16 21:10:09 +00:00
|
|
|
AttrName(std::unique_ptr<Expr> e) : expr(std::move(e)) {};
|
2013-12-31 23:56:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<AttrName> AttrPath;
|
2011-07-06 10:58:17 +00:00
|
|
|
|
2022-03-05 13:40:24 +00:00
|
|
|
std::string showAttrPath(const SymbolTable & symbols, const AttrPath & attrPath);
|
2011-07-06 10:58:17 +00:00
|
|
|
|
|
|
|
|
2010-04-14 14:42:32 +00:00
|
|
|
/* Abstract syntax of Nix expressions. */
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
struct Expr
|
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
protected:
|
|
|
|
Expr(Expr &&) = default;
|
|
|
|
Expr & operator=(Expr &&) = default;
|
|
|
|
|
|
|
|
public:
|
2024-03-08 04:37:19 +00:00
|
|
|
struct AstSymbols {
|
|
|
|
Symbol sub, lessThan, mul, div, or_, findFile, nixPath, body;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
Expr() = default;
|
|
|
|
Expr(const Expr &) = delete;
|
|
|
|
Expr & operator=(const Expr &) = delete;
|
2014-01-21 17:29:55 +00:00
|
|
|
virtual ~Expr() { };
|
2024-06-16 21:10:09 +00:00
|
|
|
|
2022-03-05 13:40:24 +00:00
|
|
|
virtual void show(const SymbolTable & symbols, std::ostream & str) const;
|
2022-05-19 16:48:10 +00:00
|
|
|
virtual void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
|
2010-04-12 22:03:27 +00:00
|
|
|
virtual void eval(EvalState & state, Env & env, Value & v);
|
2012-01-04 21:24:11 +00:00
|
|
|
virtual Value * maybeThunk(EvalState & state, Env & env);
|
2022-04-22 19:45:39 +00:00
|
|
|
virtual void setName(Symbol name);
|
2022-05-05 10:29:14 +00:00
|
|
|
virtual PosIdx getPos() const { return noPos; }
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define COMMON_METHODS \
|
2022-06-02 14:55:28 +00:00
|
|
|
void show(const SymbolTable & symbols, std::ostream & str) const override; \
|
|
|
|
void eval(EvalState & state, Env & env, Value & v) override; \
|
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override;
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
struct ExprInt : Expr
|
|
|
|
{
|
2013-08-19 10:35:03 +00:00
|
|
|
NixInt n;
|
2012-01-07 17:26:33 +00:00
|
|
|
Value v;
|
2022-01-04 17:40:39 +00:00
|
|
|
ExprInt(NixInt n) : n(n) { v.mkInt(n); };
|
2022-06-02 14:55:28 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2016-01-04 23:40:40 +00:00
|
|
|
struct ExprFloat : Expr
|
|
|
|
{
|
|
|
|
NixFloat nf;
|
|
|
|
Value v;
|
2022-01-04 17:40:39 +00:00
|
|
|
ExprFloat(NixFloat nf) : nf(nf) { v.mkFloat(nf); };
|
2022-06-02 14:55:28 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2016-01-04 23:40:40 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprString : Expr
|
|
|
|
{
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string s;
|
2012-01-07 17:26:33 +00:00
|
|
|
Value v;
|
2023-02-10 23:34:31 +00:00
|
|
|
ExprString(std::string &&s) : s(std::move(s)) { v.mkString(this->s.data()); };
|
2022-06-02 14:55:28 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprPath : Expr
|
|
|
|
{
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string s;
|
2012-01-07 17:26:33 +00:00
|
|
|
Value v;
|
2022-02-25 15:00:00 +00:00
|
|
|
ExprPath(std::string s) : s(std::move(s)) { v.mkPath(this->s.c_str()); };
|
2022-06-02 14:55:28 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2020-02-24 13:33:01 +00:00
|
|
|
typedef uint32_t Level;
|
|
|
|
typedef uint32_t Displacement;
|
|
|
|
|
2013-10-08 12:24:53 +00:00
|
|
|
struct ExprVar : Expr
|
2010-04-12 18:30:11 +00:00
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol name;
|
2010-04-14 14:42:32 +00:00
|
|
|
|
|
|
|
/* Whether the variable comes from an environment (e.g. a rec, let
|
2024-03-04 06:37:45 +00:00
|
|
|
or function argument) or from a "with".
|
|
|
|
|
|
|
|
`nullptr`: Not from a `with`.
|
|
|
|
Valid pointer: the nearest, innermost `with` expression to query first. */
|
|
|
|
ExprWith * fromWith;
|
2013-09-02 14:29:15 +00:00
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/* In the former case, the value is obtained by going `level`
|
2010-04-14 14:42:32 +00:00
|
|
|
levels up from the current environment and getting the
|
2023-04-07 13:55:28 +00:00
|
|
|
`displ`th value in that environment. In the latter case, the
|
|
|
|
value is obtained by getting the attribute named `name` from
|
|
|
|
the set stored in the environment that is `level` levels up
|
2013-10-24 14:41:04 +00:00
|
|
|
from the current one.*/
|
2020-02-24 13:33:01 +00:00
|
|
|
Level level;
|
|
|
|
Displacement displ;
|
2010-04-14 15:14:23 +00:00
|
|
|
|
2022-04-26 11:23:32 +00:00
|
|
|
ExprVar(Symbol name) : name(name) { };
|
|
|
|
ExprVar(const PosIdx & pos, Symbol name) : pos(pos), name(name) { };
|
2022-06-02 14:55:28 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2024-03-08 08:52:42 +00:00
|
|
|
/**
|
|
|
|
* A pseudo-expression for the purpose of evaluating the `from` expression in `inherit (from)` syntax.
|
|
|
|
* Unlike normal variable references, the displacement is set during parsing, and always refers to
|
|
|
|
* `ExprAttrs::inheritFromExprs` (by itself or in `ExprLet`), whose values are put into their own `Env`.
|
|
|
|
*/
|
2024-03-08 08:52:15 +00:00
|
|
|
struct ExprInheritFrom : ExprVar
|
|
|
|
{
|
|
|
|
ExprInheritFrom(PosIdx pos, Displacement displ): ExprVar(pos, {})
|
|
|
|
{
|
|
|
|
this->level = 0;
|
|
|
|
this->displ = displ;
|
|
|
|
this->fromWith = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprSelect : Expr
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> e, def;
|
2011-07-06 12:28:57 +00:00
|
|
|
AttrPath attrPath;
|
2024-06-16 21:10:09 +00:00
|
|
|
ExprSelect(const PosIdx & pos, std::unique_ptr<Expr> e, AttrPath attrPath, std::unique_ptr<Expr> def) : pos(pos), e(std::move(e)), def(std::move(def)), attrPath(std::move(attrPath)) { };
|
|
|
|
ExprSelect(const PosIdx & pos, std::unique_ptr<Expr> e, Symbol name) : pos(pos), e(std::move(e)) { attrPath.push_back(AttrName(name)); };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprOpHasAttr : Expr
|
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> e;
|
2011-07-06 10:58:17 +00:00
|
|
|
AttrPath attrPath;
|
2024-06-16 21:10:09 +00:00
|
|
|
ExprOpHasAttr(std::unique_ptr<Expr> e, AttrPath attrPath) : e(std::move(e)), attrPath(std::move(attrPath)) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return e->getPos(); }
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprAttrs : Expr
|
|
|
|
{
|
|
|
|
bool recursive;
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2010-10-24 19:52:33 +00:00
|
|
|
struct AttrDef {
|
2024-03-08 08:51:27 +00:00
|
|
|
enum class Kind {
|
|
|
|
/** `attr = expr;` */
|
|
|
|
Plain,
|
|
|
|
/** `inherit attr1 attrn;` */
|
|
|
|
Inherited,
|
|
|
|
/** `inherit (expr) attr1 attrn;` */
|
|
|
|
InheritedFrom,
|
|
|
|
};
|
|
|
|
|
|
|
|
Kind kind;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> e;
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2020-02-24 13:33:01 +00:00
|
|
|
Displacement displ; // displacement
|
2024-06-16 21:10:09 +00:00
|
|
|
AttrDef(std::unique_ptr<Expr> e, const PosIdx & pos, Kind kind = Kind::Plain)
|
|
|
|
: kind(kind), e(std::move(e)), pos(pos) { };
|
2010-10-24 19:52:33 +00:00
|
|
|
AttrDef() { };
|
2024-03-08 08:51:27 +00:00
|
|
|
|
2024-03-08 08:51:40 +00:00
|
|
|
template<typename T>
|
|
|
|
const T & chooseByKind(const T & plain, const T & inherited, const T & inheritedFrom) const
|
|
|
|
{
|
|
|
|
switch (kind) {
|
|
|
|
case Kind::Plain:
|
|
|
|
return plain;
|
|
|
|
case Kind::Inherited:
|
|
|
|
return inherited;
|
|
|
|
default:
|
|
|
|
case Kind::InheritedFrom:
|
|
|
|
return inheritedFrom;
|
|
|
|
}
|
|
|
|
}
|
2010-10-24 19:52:33 +00:00
|
|
|
};
|
|
|
|
typedef std::map<Symbol, AttrDef> AttrDefs;
|
|
|
|
AttrDefs attrs;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<std::vector<std::unique_ptr<Expr>>> inheritFromExprs;
|
Dynamic attrs
This adds new syntax for attribute names:
* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]
Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.
For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.
Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.
Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.
This is an alternative solution to and closes #167
Signed-off-by: Shea Levy <shea@shealevy.com>
2013-09-21 03:25:30 +00:00
|
|
|
struct DynamicAttrDef {
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> nameExpr, valueExpr;
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-06-16 21:10:09 +00:00
|
|
|
DynamicAttrDef(std::unique_ptr<Expr> nameExpr, std::unique_ptr<Expr> valueExpr, const PosIdx & pos)
|
|
|
|
: nameExpr(std::move(nameExpr)), valueExpr(std::move(valueExpr)), pos(pos) { };
|
Dynamic attrs
This adds new syntax for attribute names:
* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]
Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.
For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.
Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.
Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.
This is an alternative solution to and closes #167
Signed-off-by: Shea Levy <shea@shealevy.com>
2013-09-21 03:25:30 +00:00
|
|
|
};
|
|
|
|
typedef std::vector<DynamicAttrDef> DynamicAttrDefs;
|
|
|
|
DynamicAttrDefs dynamicAttrs;
|
2022-03-04 18:31:59 +00:00
|
|
|
ExprAttrs(const PosIdx &pos) : recursive(false), pos(pos) { };
|
|
|
|
ExprAttrs() : recursive(false) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
2024-03-08 08:51:51 +00:00
|
|
|
|
2024-03-08 08:52:15 +00:00
|
|
|
std::shared_ptr<const StaticEnv> bindInheritSources(
|
|
|
|
EvalState & es, const std::shared_ptr<const StaticEnv> & env);
|
|
|
|
Env * buildInheritFromEnv(EvalState & state, Env & up);
|
2024-03-08 08:51:51 +00:00
|
|
|
void showBindings(const SymbolTable & symbols, std::ostream & str) const;
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
2004-10-26 22:54:26 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprList : Expr
|
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
std::vector<std::unique_ptr<Expr>> elems;
|
2010-04-12 18:30:11 +00:00
|
|
|
ExprList() { };
|
|
|
|
COMMON_METHODS
|
2024-03-04 06:39:12 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2022-05-05 10:29:14 +00:00
|
|
|
|
|
|
|
PosIdx getPos() const override
|
|
|
|
{
|
|
|
|
return elems.empty() ? noPos : elems.front()->getPos();
|
|
|
|
}
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Formal
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol name;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> def;
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
2004-08-04 10:59:20 +00:00
|
|
|
|
2024-03-30 01:26:40 +00:00
|
|
|
/** Attribute set destructuring in arguments of a lambda, if present */
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Formals
|
|
|
|
{
|
2022-01-19 15:49:02 +00:00
|
|
|
typedef std::vector<Formal> Formals_;
|
2010-04-12 18:30:11 +00:00
|
|
|
Formals_ formals;
|
|
|
|
bool ellipsis;
|
2022-01-19 15:49:02 +00:00
|
|
|
|
2022-04-26 11:23:32 +00:00
|
|
|
bool has(Symbol arg) const
|
|
|
|
{
|
2022-01-19 15:49:02 +00:00
|
|
|
auto it = std::lower_bound(formals.begin(), formals.end(), arg,
|
|
|
|
[] (const Formal & f, const Symbol & sym) { return f.name < sym; });
|
|
|
|
return it != formals.end() && it->name == arg;
|
|
|
|
}
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
std::vector<std::reference_wrapper<const Formal>> lexicographicOrder(const SymbolTable & symbols) const
|
2022-01-19 15:49:02 +00:00
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
std::vector<std::reference_wrapper<const Formal>> result(formals.begin(), formals.end());
|
2022-01-19 15:49:02 +00:00
|
|
|
std::sort(result.begin(), result.end(),
|
2022-03-05 13:40:24 +00:00
|
|
|
[&] (const Formal & a, const Formal & b) {
|
|
|
|
std::string_view sa = symbols[a.name], sb = symbols[b.name];
|
|
|
|
return sa < sb;
|
2022-01-19 15:49:02 +00:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
2004-08-04 10:59:20 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprLambda : Expr
|
|
|
|
{
|
2024-03-30 01:26:40 +00:00
|
|
|
/** Where the lambda is defined in Nix code. May be falsey if the
|
|
|
|
* position is not known. */
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-03-30 01:26:40 +00:00
|
|
|
/** Name of the lambda. This is set if the lambda is defined in a
|
|
|
|
* let-expression or an attribute set, such that there is a name.
|
|
|
|
* Lambdas may have a falsey symbol as the name if they are anonymous */
|
2013-05-16 17:08:02 +00:00
|
|
|
Symbol name;
|
2024-03-30 01:26:40 +00:00
|
|
|
/** The argument name of this particular lambda. Is a falsey symbol if there
|
|
|
|
* is no such argument. */
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol arg;
|
2024-03-30 01:26:40 +00:00
|
|
|
/** Formals are present when the lambda destructures an attr set as
|
|
|
|
* argument, with or without ellipsis */
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Formals> formals;
|
|
|
|
std::unique_ptr<Expr> body;
|
|
|
|
ExprLambda(PosIdx pos, Symbol arg, std::unique_ptr<Formals> formals, std::unique_ptr<Expr> body)
|
|
|
|
: pos(pos), arg(arg), formals(std::move(formals)), body(std::move(body))
|
2010-04-22 11:02:24 +00:00
|
|
|
{
|
2020-04-29 16:14:32 +00:00
|
|
|
};
|
2024-06-16 21:10:09 +00:00
|
|
|
ExprLambda(PosIdx pos, std::unique_ptr<Formals> formals, std::unique_ptr<Expr> body)
|
|
|
|
: pos(pos), formals(std::move(formals)), body(std::move(body))
|
2022-03-05 13:40:24 +00:00
|
|
|
{
|
|
|
|
}
|
2022-06-02 14:55:28 +00:00
|
|
|
void setName(Symbol name) override;
|
2022-03-05 13:40:24 +00:00
|
|
|
std::string showNamePos(const EvalState & state) const;
|
2021-10-06 15:08:08 +00:00
|
|
|
inline bool hasFormals() const { return formals != nullptr; }
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2024-05-31 19:12:14 +00:00
|
|
|
|
|
|
|
/** Returns the name of the lambda,
|
|
|
|
* or "anonymous lambda" if it doesn't have one.
|
|
|
|
*/
|
|
|
|
inline std::string getName(SymbolTable const & symbols) const
|
|
|
|
{
|
|
|
|
if (this->name) {
|
|
|
|
return symbols[this->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "anonymous lambda";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the name of the lambda in single quotes,
|
|
|
|
* or "anonymous lambda" if it doesn't have one.
|
|
|
|
*/
|
|
|
|
inline std::string getQuotedName(SymbolTable const & symbols) const
|
|
|
|
{
|
|
|
|
if (this->name) {
|
|
|
|
return concatStrings("'", symbols[this->name], "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
return "anonymous lambda";
|
|
|
|
}
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2020-02-24 00:32:01 +00:00
|
|
|
struct ExprCall : Expr
|
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> fun;
|
|
|
|
std::vector<std::unique_ptr<Expr>> args;
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-06-16 21:10:09 +00:00
|
|
|
ExprCall(const PosIdx & pos, std::unique_ptr<Expr> fun, std::vector<std::unique_ptr<Expr>> && args)
|
|
|
|
: fun(std::move(fun)), args(std::move(args)), pos(pos)
|
2020-02-24 00:32:01 +00:00
|
|
|
{ }
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2020-02-24 00:32:01 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-13 13:42:25 +00:00
|
|
|
struct ExprLet : Expr
|
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<ExprAttrs> attrs;
|
|
|
|
std::unique_ptr<Expr> body;
|
|
|
|
ExprLet(std::unique_ptr<ExprAttrs> attrs, std::unique_ptr<Expr> body) : attrs(std::move(attrs)), body(std::move(body)) { };
|
2010-04-13 13:42:25 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprWith : Expr
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> attrs, body;
|
2018-05-02 11:56:34 +00:00
|
|
|
size_t prevWith;
|
2024-03-04 06:37:45 +00:00
|
|
|
ExprWith * parentWith;
|
2024-06-16 21:10:09 +00:00
|
|
|
ExprWith(const PosIdx & pos, std::unique_ptr<Expr> attrs, std::unique_ptr<Expr> body) : pos(pos), attrs(std::move(attrs)), body(std::move(body)) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprIf : Expr
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> cond, then, else_;
|
|
|
|
ExprIf(const PosIdx & pos, std::unique_ptr<Expr> cond, std::unique_ptr<Expr> then, std::unique_ptr<Expr> else_) : pos(pos), cond(std::move(cond)), then(std::move(then)), else_(std::move(else_)) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprAssert : Expr
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> cond, body;
|
|
|
|
ExprAssert(const PosIdx & pos, std::unique_ptr<Expr> cond, std::unique_ptr<Expr> body) : pos(pos), cond(std::move(cond)), body(std::move(body)) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprOpNot : Expr
|
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> e;
|
|
|
|
ExprOpNot(std::unique_ptr<Expr> e) : e(std::move(e)) { };
|
2024-03-04 05:36:36 +00:00
|
|
|
PosIdx getPos() const override { return e->getPos(); }
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
#define MakeBinOp(name, s) \
|
2018-03-09 03:16:33 +00:00
|
|
|
struct name : Expr \
|
2010-04-12 18:30:11 +00:00
|
|
|
{ \
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos; \
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Expr> e1, e2; \
|
|
|
|
name(std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : e1(std::move(e1)), e2(std::move(e2)) { }; \
|
|
|
|
name(const PosIdx & pos, std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : pos(pos), e1(std::move(e1)), e2(std::move(e2)) { }; \
|
2022-06-02 14:55:28 +00:00
|
|
|
void show(const SymbolTable & symbols, std::ostream & str) const override \
|
2010-04-12 18:30:11 +00:00
|
|
|
{ \
|
2022-03-05 13:40:24 +00:00
|
|
|
str << "("; e1->show(symbols, str); str << " " s " "; e2->show(symbols, str); str << ")"; \
|
2010-04-12 18:30:11 +00:00
|
|
|
} \
|
2022-06-02 14:55:28 +00:00
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override \
|
2010-04-14 14:42:32 +00:00
|
|
|
{ \
|
2022-03-05 13:40:24 +00:00
|
|
|
e1->bindVars(es, env); e2->bindVars(es, env); \
|
2010-04-14 14:42:32 +00:00
|
|
|
} \
|
2022-06-02 14:55:28 +00:00
|
|
|
void eval(EvalState & state, Env & env, Value & v) override; \
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; } \
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
2018-03-09 03:16:33 +00:00
|
|
|
MakeBinOp(ExprOpEq, "==")
|
|
|
|
MakeBinOp(ExprOpNEq, "!=")
|
|
|
|
MakeBinOp(ExprOpAnd, "&&")
|
|
|
|
MakeBinOp(ExprOpOr, "||")
|
|
|
|
MakeBinOp(ExprOpImpl, "->")
|
|
|
|
MakeBinOp(ExprOpUpdate, "//")
|
|
|
|
MakeBinOp(ExprOpConcatLists, "++")
|
2010-04-12 18:30:11 +00:00
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprConcatStrings : Expr
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2013-03-08 00:24:59 +00:00
|
|
|
bool forceString;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::vector<std::pair<PosIdx, std::unique_ptr<Expr>>> es;
|
|
|
|
ExprConcatStrings(const PosIdx & pos, bool forceString, std::vector<std::pair<PosIdx, std::unique_ptr<Expr>>> es)
|
2024-06-16 21:10:09 +00:00
|
|
|
: pos(pos), forceString(forceString), es(std::move(es)) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
2004-04-05 22:27:41 +00:00
|
|
|
|
2013-11-18 19:14:54 +00:00
|
|
|
struct ExprPos : Expr
|
|
|
|
{
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
|
|
|
ExprPos(const PosIdx & pos) : pos(pos) { };
|
2022-05-05 10:29:14 +00:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2013-11-18 19:14:54 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2024-03-04 06:32:31 +00:00
|
|
|
/* only used to mark thunks as black holes. */
|
|
|
|
struct ExprBlackHole : Expr
|
|
|
|
{
|
|
|
|
void show(const SymbolTable & symbols, std::ostream & str) const override {}
|
|
|
|
void eval(EvalState & state, Env & env, Value & v) override;
|
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern ExprBlackHole eBlackHole;
|
|
|
|
|
2010-04-08 11:41:19 +00:00
|
|
|
|
2010-04-14 14:42:32 +00:00
|
|
|
/* Static environments are used to map variable names onto (level,
|
|
|
|
displacement) pairs used to obtain the value of the variable at
|
|
|
|
runtime. */
|
|
|
|
struct StaticEnv
|
|
|
|
{
|
2024-03-04 06:37:45 +00:00
|
|
|
ExprWith * isWith;
|
2010-04-14 14:42:32 +00:00
|
|
|
const StaticEnv * up;
|
2020-02-21 17:31:16 +00:00
|
|
|
|
|
|
|
// Note: these must be in sorted order.
|
2020-02-24 13:33:01 +00:00
|
|
|
typedef std::vector<std::pair<Symbol, Displacement>> Vars;
|
2010-04-14 14:42:32 +00:00
|
|
|
Vars vars;
|
2020-02-21 17:31:16 +00:00
|
|
|
|
2024-03-04 06:37:45 +00:00
|
|
|
StaticEnv(ExprWith * isWith, const StaticEnv * up, size_t expectedSize = 0) : isWith(isWith), up(up) {
|
2020-02-21 17:31:16 +00:00
|
|
|
vars.reserve(expectedSize);
|
|
|
|
};
|
|
|
|
|
|
|
|
void sort()
|
|
|
|
{
|
2022-02-04 06:36:56 +00:00
|
|
|
std::stable_sort(vars.begin(), vars.end(),
|
2020-02-21 17:31:16 +00:00
|
|
|
[](const Vars::value_type & a, const Vars::value_type & b) { return a.first < b.first; });
|
|
|
|
}
|
|
|
|
|
2021-12-27 12:18:55 +00:00
|
|
|
void deduplicate()
|
|
|
|
{
|
2022-02-04 06:36:56 +00:00
|
|
|
auto it = vars.begin(), jt = it, end = vars.end();
|
|
|
|
while (jt != end) {
|
|
|
|
*it = *jt++;
|
|
|
|
while (jt != end && it->first == jt->first) *it = *jt++;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
vars.erase(it, end);
|
2021-12-27 12:18:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 11:23:32 +00:00
|
|
|
Vars::const_iterator find(Symbol name) const
|
2020-02-21 17:31:16 +00:00
|
|
|
{
|
|
|
|
Vars::value_type key(name, 0);
|
|
|
|
auto i = std::lower_bound(vars.begin(), vars.end(), key);
|
|
|
|
if (i != vars.end() && i->first == name) return i;
|
|
|
|
return vars.end();
|
|
|
|
}
|
2010-04-14 14:42:32 +00:00
|
|
|
};
|
|
|
|
|
2006-10-16 15:55:34 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|