2003-11-18 14:06:07 +02:00
|
|
|
#include "nixexpr.hh"
|
2022-03-05 15:40:24 +02:00
|
|
|
#include "eval.hh"
|
|
|
|
#include "symbol-table.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "util.hh"
|
2023-04-16 14:10:45 +03:00
|
|
|
#include "print.hh"
|
2004-10-27 01:54:26 +03:00
|
|
|
|
2008-05-21 14:17:31 +03:00
|
|
|
#include <cstdlib>
|
Remove 100s of CPU time (10%) from build times (1465s -> 1302s)
Result's from Mic92's framework 13th Gen Intel Core i7-1360P:
Before: 3595.92s user 183.01s system 1360% cpu 4:37.74 total
After: 3486.07s user 168.93s system 1354% cpu 4:29.79 total
I saw that boost/lexical_cast was costing about 100s in CPU time on our
compiles. We can fix this trivially by doing explicit template
instantiation in exactly one place and eliminating all other includes of
it, which is a code improvement anyway by hiding the boost.
Before:
```
lix/lix2 » ClangBuildAnalyzer --analyze buildtimeold.bin
Analyzing build trace from 'buildtimeold.bin'...
**** Time summary:
Compilation (551 times):
Parsing (frontend): 1465.3 s
Codegen & opts (backend): 1110.9 s
<snip>
**** Expensive headers:
178153 ms: ../src/libcmd/installable-value.hh (included 52 times, avg 3426 ms), included via:
40x: command.hh
5x: command-installable-value.hh
3x: installable-flake.hh
2x: <direct include>
2x: installable-attr-path.hh
176217 ms: ../src/libutil/error.hh (included 246 times, avg 716 ms), included via:
36x: command.hh installable-value.hh installables.hh derived-path.hh config.hh experimental-features.hh
12x: globals.hh config.hh experimental-features.hh
11x: file-system.hh file-descriptor.hh
6x: serialise.hh strings.hh
6x: <direct include>
6x: archive.hh serialise.hh strings.hh
...
173243 ms: ../src/libstore/store-api.hh (included 152 times, avg 1139 ms), included via:
55x: <direct include>
39x: command.hh installable-value.hh installables.hh
7x: libexpr.hh
4x: local-store.hh
4x: command-installable-value.hh installable-value.hh installables.hh
3x: binary-cache-store.hh
...
170482 ms: ../src/libutil/serialise.hh (included 201 times, avg 848 ms), included via:
37x: command.hh installable-value.hh installables.hh built-path.hh realisation.hh hash.hh
14x: store-api.hh nar-info.hh hash.hh
11x: <direct include>
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh
7x: libexpr.hh value.hh source-path.hh archive.hh
6x: fetchers.hh hash.hh
...
169397 ms: ../src/libcmd/installables.hh (included 53 times, avg 3196 ms), included via:
40x: command.hh installable-value.hh
5x: command-installable-value.hh installable-value.hh
3x: installable-flake.hh installable-value.hh
2x: <direct include>
1x: installable-derived-path.hh
1x: installable-value.hh
...
159740 ms: ../src/libutil/strings.hh (included 221 times, avg 722 ms), included via:
37x: command.hh installable-value.hh installables.hh built-path.hh realisation.hh hash.hh serialise.hh
19x: <direct include>
14x: store-api.hh nar-info.hh hash.hh serialise.hh
11x: serialise.hh
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh serialise.hh
7x: libexpr.hh value.hh source-path.hh archive.hh serialise.hh
...
156796 ms: ../src/libcmd/command.hh (included 51 times, avg 3074 ms), included via:
42x: <direct include>
7x: command-installable-value.hh
2x: installable-attr-path.hh
150392 ms: ../src/libutil/types.hh (included 251 times, avg 599 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh
11x: file-system.hh
10x: globals.hh
6x: fetchers.hh
6x: serialise.hh strings.hh error.hh
5x: archive.hh
...
133101 ms: /nix/store/644b90j1vms44nr18yw3520pzkrg4dd1-boost-1.81.0-dev/include/boost/lexical_cast.hpp (included 226 times, avg 588 ms), included via
:
37x: command.hh installable-value.hh installables.hh built-path.hh realisation.hh hash.hh serialise.hh strings.hh
19x: file-system.hh
11x: store-api.hh nar-info.hh hash.hh serialise.hh strings.hh
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh serialise.hh strings.hh
7x: libexpr.hh value.hh source-path.hh archive.hh serialise.hh strings.hh
6x: eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh serialise.hh strings.hh
...
132887 ms: /nix/store/h2abv2l8irqj942i5rq9wbrj42kbsh5y-gcc-12.3.0/include/c++/12.3.0/memory (included 262 times, avg 507 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh types.hh ref.hh
16x: gtest.h
11x: file-system.hh types.hh ref.hh
10x: globals.hh types.hh ref.hh
10x: json.hpp
6x: serialise.hh
...
done in 0.6s.
```
After:
```
lix/lix2 » maintainers/buildtime_report.sh build
Processing all files and saving to '/home/jade/lix/lix2/maintainers/../buildtime.bin'...
done in 0.6s. Run 'ClangBuildAnalyzer --analyze /home/jade/lix/lix2/maintainers/../buildtime.bin' to analyze it.
Analyzing build trace from '/home/jade/lix/lix2/maintainers/../buildtime.bin'...
**** Time summary:
Compilation (551 times):
Parsing (frontend): 1302.1 s
Codegen & opts (backend): 956.3 s
<snip>
**** Expensive headers:
178145 ms: ../src/libutil/error.hh (included 246 times, avg 724 ms), included via:
36x: command.hh installable-value.hh installables.hh derived-path.hh config.hh experimental-features.hh
12x: globals.hh config.hh experimental-features.hh
11x: file-system.hh file-descriptor.hh
6x: <direct include>
6x: serialise.hh strings.hh
6x: fetchers.hh hash.hh serialise.hh strings.hh
...
154043 ms: ../src/libcmd/installable-value.hh (included 52 times, avg 2962 ms), included via:
40x: command.hh
5x: command-installable-value.hh
3x: installable-flake.hh
2x: <direct include>
2x: installable-attr-path.hh
153593 ms: ../src/libstore/store-api.hh (included 152 times, avg 1010 ms), included via:
55x: <direct include>
39x: command.hh installable-value.hh installables.hh
7x: libexpr.hh
4x: local-store.hh
4x: command-installable-value.hh installable-value.hh installables.hh
3x: binary-cache-store.hh
...
149948 ms: ../src/libutil/types.hh (included 251 times, avg 597 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh
11x: file-system.hh
10x: globals.hh
6x: fetchers.hh
6x: serialise.hh strings.hh error.hh
5x: archive.hh
...
144560 ms: ../src/libcmd/installables.hh (included 53 times, avg 2727 ms), included via:
40x: command.hh installable-value.hh
5x: command-installable-value.hh installable-value.hh
3x: installable-flake.hh installable-value.hh
2x: <direct include>
1x: installable-value.hh
1x: installable-derived-path.hh
...
136585 ms: ../src/libcmd/command.hh (included 51 times, avg 2678 ms), included via:
42x: <direct include>
7x: command-installable-value.hh
2x: installable-attr-path.hh
133394 ms: /nix/store/h2abv2l8irqj942i5rq9wbrj42kbsh5y-gcc-12.3.0/include/c++/12.3.0/memory (included 262 times, avg 509 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh types.hh ref.hh
16x: gtest.h
11x: file-system.hh types.hh ref.hh
10x: globals.hh types.hh ref.hh
10x: json.hpp
6x: serialise.hh
...
89315 ms: ../src/libstore/derived-path.hh (included 178 times, avg 501 ms), included via:
37x: command.hh installable-value.hh installables.hh
25x: store-api.hh realisation.hh
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh context.hh
6x: eval.hh attr-set.hh nixexpr.hh value.hh context.hh
6x: libexpr.hh value.hh context.hh
6x: shared.hh
...
87347 ms: /nix/store/h2abv2l8irqj942i5rq9wbrj42kbsh5y-gcc-12.3.0/include/c++/12.3.0/ostream (included 273 times, avg 319 ms), included via:
35x: command.hh installable-value.hh installables.hh path.hh types.hh ref.hh memory unique_ptr.h
12x: regex sstream istream
10x: file-system.hh types.hh ref.hh memory unique_ptr.h
10x: gtest.h memory unique_ptr.h
10x: globals.hh types.hh ref.hh memory unique_ptr.h
6x: fetchers.hh types.hh ref.hh memory unique_ptr.h
...
85249 ms: ../src/libutil/config.hh (included 213 times, avg 400 ms), included via:
37x: command.hh installable-value.hh installables.hh derived-path.hh
20x: globals.hh
20x: logging.hh
16x: store-api.hh logging.hh
6x: <direct include>
6x: eval.hh attr-set.hh nixexpr.hh value.hh context.hh derived-path.hh
...
done in 0.5s.
```
Adapated from https://git.lix.systems/lix-project/lix/commit/18aa3e1d570b4ecbb9962376e5fba5757dad8da9
2024-05-30 07:12:34 +03:00
|
|
|
#include <sstream>
|
2004-10-27 01:54:26 +03:00
|
|
|
|
2024-07-14 13:19:55 +03:00
|
|
|
#include "strings-inline.hh"
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
|
|
|
|
2024-01-15 17:52:18 +02:00
|
|
|
unsigned long Expr::nrExprs = 0;
|
|
|
|
|
2023-12-11 17:23:08 +02:00
|
|
|
ExprBlackHole eBlackHole;
|
|
|
|
|
2023-04-16 15:07:35 +03:00
|
|
|
// FIXME: remove, because *symbols* are abstract and do not have a single
|
|
|
|
// textual representation; see printIdentifier()
|
2022-04-22 22:45:39 +03:00
|
|
|
std::ostream & operator <<(std::ostream & str, const SymbolStr & symbol)
|
2014-10-20 09:44:32 +03:00
|
|
|
{
|
2022-04-22 22:45:39 +03:00
|
|
|
std::string_view s = symbol;
|
2023-04-16 15:07:35 +03:00
|
|
|
return printIdentifier(str, s);
|
2014-10-20 09:44:32 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void Expr::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-13 01:03:27 +03:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprInt::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2024-03-25 19:20:18 +02:00
|
|
|
str << v.integer();
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprFloat::show(const SymbolTable & symbols, std::ostream & str) const
|
2016-01-05 01:40:40 +02:00
|
|
|
{
|
2024-03-25 19:20:18 +02:00
|
|
|
str << v.fpoint();
|
2016-01-05 01:40:40 +02:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprString::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2023-04-16 13:56:31 +03:00
|
|
|
printLiteralString(str, s);
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprPath::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
|
|
|
str << s;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprVar::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << symbols[name];
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprSelect::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "(";
|
|
|
|
e->show(symbols, str);
|
|
|
|
str << ")." << showAttrPath(symbols, attrPath);
|
|
|
|
if (def) {
|
|
|
|
str << " or (";
|
|
|
|
def->show(symbols, str);
|
|
|
|
str << ")";
|
|
|
|
}
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprOpHasAttr::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-13 00:21:24 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "((";
|
|
|
|
e->show(symbols, str);
|
|
|
|
str << ") ? " << showAttrPath(symbols, attrPath) << ")";
|
2010-04-13 00:21:24 +03:00
|
|
|
}
|
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
void ExprAttrs::showBindings(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2022-02-25 10:21:04 +02:00
|
|
|
typedef const decltype(attrs)::value_type * Attr;
|
|
|
|
std::vector<Attr> sorted;
|
|
|
|
for (auto & i : attrs) sorted.push_back(&i);
|
2022-03-05 15:40:24 +02:00
|
|
|
std::sort(sorted.begin(), sorted.end(), [&](Attr a, Attr b) {
|
|
|
|
std::string_view sa = symbols[a->first], sb = symbols[b->first];
|
|
|
|
return sa < sb;
|
|
|
|
});
|
2024-01-27 17:33:34 +02:00
|
|
|
std::vector<Symbol> inherits;
|
2024-01-27 17:33:34 +02:00
|
|
|
std::map<ExprInheritFrom *, std::vector<Symbol>> inheritsFrom;
|
2022-02-25 10:21:04 +02:00
|
|
|
for (auto & i : sorted) {
|
2024-01-27 17:33:34 +02:00
|
|
|
switch (i->second.kind) {
|
|
|
|
case AttrDef::Kind::Plain:
|
|
|
|
break;
|
|
|
|
case AttrDef::Kind::Inherited:
|
|
|
|
inherits.push_back(i->first);
|
|
|
|
break;
|
|
|
|
case AttrDef::Kind::InheritedFrom: {
|
|
|
|
auto & select = dynamic_cast<ExprSelect &>(*i->second.e);
|
2024-01-27 17:33:34 +02:00
|
|
|
auto & from = dynamic_cast<ExprInheritFrom &>(*select.e);
|
|
|
|
inheritsFrom[&from].push_back(i->first);
|
2024-01-27 17:33:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inherits.empty()) {
|
|
|
|
str << "inherit";
|
|
|
|
for (auto sym : inherits) str << " " << symbols[sym];
|
|
|
|
str << "; ";
|
|
|
|
}
|
|
|
|
for (const auto & [from, syms] : inheritsFrom) {
|
|
|
|
str << "inherit (";
|
2024-01-27 17:33:34 +02:00
|
|
|
(*inheritFromExprs)[from->displ]->show(symbols, str);
|
2024-01-27 17:33:34 +02:00
|
|
|
str << ")";
|
|
|
|
for (auto sym : syms) str << " " << symbols[sym];
|
|
|
|
str << "; ";
|
|
|
|
}
|
|
|
|
for (auto & i : sorted) {
|
|
|
|
if (i->second.kind == AttrDef::Kind::Plain) {
|
2022-03-05 15:40:24 +02:00
|
|
|
str << symbols[i->first] << " = ";
|
|
|
|
i->second.e->show(symbols, str);
|
|
|
|
str << "; ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto & i : dynamicAttrs) {
|
|
|
|
str << "\"${";
|
|
|
|
i.nameExpr->show(symbols, str);
|
|
|
|
str << "}\" = ";
|
|
|
|
i.valueExpr->show(symbols, str);
|
|
|
|
str << "; ";
|
2022-02-25 10:21:04 +02:00
|
|
|
}
|
2024-01-27 17:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExprAttrs::show(const SymbolTable & symbols, std::ostream & str) const
|
|
|
|
{
|
|
|
|
if (recursive) str << "rec ";
|
|
|
|
str << "{ ";
|
|
|
|
showBindings(symbols, str);
|
2010-04-12 21:30:11 +03:00
|
|
|
str << "}";
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprList::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
|
|
|
str << "[ ";
|
2022-03-05 15:40:24 +02:00
|
|
|
for (auto & i : elems) {
|
|
|
|
str << "(";
|
|
|
|
i->show(symbols, str);
|
|
|
|
str << ") ";
|
|
|
|
}
|
2010-04-12 21:30:11 +03:00
|
|
|
str << "]";
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprLambda::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
|
|
|
str << "(";
|
2021-10-06 18:08:08 +03:00
|
|
|
if (hasFormals()) {
|
2010-04-12 21:30:11 +03:00
|
|
|
str << "{ ";
|
|
|
|
bool first = true;
|
2024-01-29 07:19:23 +02:00
|
|
|
// the natural Symbol ordering is by creation time, which can lead to the
|
|
|
|
// same expression being printed in two different ways depending on its
|
|
|
|
// context. always use lexicographic ordering to avoid this.
|
|
|
|
for (auto & i : formals->lexicographicOrder(symbols)) {
|
2010-04-12 21:30:11 +03:00
|
|
|
if (first) first = false; else str << ", ";
|
2022-03-05 15:40:24 +02:00
|
|
|
str << symbols[i.name];
|
|
|
|
if (i.def) {
|
|
|
|
str << " ? ";
|
|
|
|
i.def->show(symbols, str);
|
|
|
|
}
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
2014-10-20 09:44:32 +03:00
|
|
|
if (formals->ellipsis) {
|
|
|
|
if (!first) str << ", ";
|
|
|
|
str << "...";
|
|
|
|
}
|
2010-04-12 21:30:11 +03:00
|
|
|
str << " }";
|
2022-03-05 15:40:24 +02:00
|
|
|
if (arg) str << " @ ";
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
2022-03-05 15:40:24 +02:00
|
|
|
if (arg) str << symbols[arg];
|
|
|
|
str << ": ";
|
|
|
|
body->show(symbols, str);
|
|
|
|
str << ")";
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprCall::show(const SymbolTable & symbols, std::ostream & str) const
|
2020-02-24 02:32:01 +02:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << '(';
|
|
|
|
fun->show(symbols, str);
|
2020-02-24 02:32:01 +02:00
|
|
|
for (auto e : args) {
|
|
|
|
str << ' ';
|
2022-03-05 15:40:24 +02:00
|
|
|
e->show(symbols, str);
|
2020-02-24 02:32:01 +02:00
|
|
|
}
|
|
|
|
str << ')';
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprLet::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-13 16:42:25 +03:00
|
|
|
{
|
2014-10-20 09:44:32 +03:00
|
|
|
str << "(let ";
|
2024-01-27 17:33:34 +02:00
|
|
|
attrs->showBindings(symbols, str);
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "in ";
|
|
|
|
body->show(symbols, str);
|
|
|
|
str << ")";
|
2010-04-13 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprWith::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "(with ";
|
|
|
|
attrs->show(symbols, str);
|
|
|
|
str << "; ";
|
|
|
|
body->show(symbols, str);
|
|
|
|
str << ")";
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprIf::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "(if ";
|
|
|
|
cond->show(symbols, str);
|
|
|
|
str << " then ";
|
|
|
|
then->show(symbols, str);
|
|
|
|
str << " else ";
|
|
|
|
else_->show(symbols, str);
|
|
|
|
str << ")";
|
2010-04-12 21:30:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprAssert::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-13 00:21:24 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "assert ";
|
|
|
|
cond->show(symbols, str);
|
|
|
|
str << "; ";
|
|
|
|
body->show(symbols, str);
|
2010-04-13 00:21:24 +03:00
|
|
|
}
|
2010-04-12 21:30:11 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprOpNot::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-13 00:21:24 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "(! ";
|
|
|
|
e->show(symbols, str);
|
|
|
|
str << ")";
|
2010-04-13 00:21:24 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprConcatStrings::show(const SymbolTable & symbols, std::ostream & str) const
|
2010-04-13 00:21:24 +03:00
|
|
|
{
|
|
|
|
bool first = true;
|
2014-10-20 09:44:32 +03:00
|
|
|
str << "(";
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : *es) {
|
2010-04-13 00:21:24 +03:00
|
|
|
if (first) first = false; else str << " + ";
|
2022-03-05 15:40:24 +02:00
|
|
|
i.second->show(symbols, str);
|
2010-04-13 00:21:24 +03:00
|
|
|
}
|
2014-10-20 09:44:32 +03:00
|
|
|
str << ")";
|
2010-04-13 00:21:24 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
void ExprPos::show(const SymbolTable & symbols, std::ostream & str) const
|
2013-11-18 21:14:54 +02:00
|
|
|
{
|
|
|
|
str << "__curPos";
|
|
|
|
}
|
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
std::string showAttrPath(const SymbolTable & symbols, const AttrPath & attrPath)
|
2011-07-06 13:58:17 +03:00
|
|
|
{
|
2014-01-01 01:56:26 +02:00
|
|
|
std::ostringstream out;
|
|
|
|
bool first = true;
|
2015-03-06 15:24:08 +02:00
|
|
|
for (auto & i : attrPath) {
|
|
|
|
if (!first) out << '.'; else first = false;
|
2022-03-05 15:40:24 +02:00
|
|
|
if (i.symbol)
|
|
|
|
out << symbols[i.symbol];
|
|
|
|
else {
|
|
|
|
out << "\"${";
|
|
|
|
i.expr->show(symbols, out);
|
|
|
|
out << "}\"";
|
|
|
|
}
|
2011-07-06 13:58:17 +03:00
|
|
|
}
|
2014-01-01 01:56:26 +02:00
|
|
|
return out.str();
|
2011-07-06 13:58:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-14 17:42:32 +03:00
|
|
|
/* Computing levels/displacements for variables. */
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void Expr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprInt::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
|
|
|
}
|
2010-04-14 17:42:32 +03:00
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprFloat::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2016-01-05 01:40:40 +02:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2016-01-05 01:40:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprString::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprPath::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprVar::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2023-12-22 19:19:53 +02:00
|
|
|
fromWith = nullptr;
|
|
|
|
|
2010-04-14 17:42:32 +03:00
|
|
|
/* Check whether the variable appears in the environment. If so,
|
|
|
|
set its level and displacement. */
|
2021-10-22 23:49:58 +03:00
|
|
|
const StaticEnv * curEnv;
|
2020-02-24 15:33:01 +02:00
|
|
|
Level level;
|
2021-10-22 23:49:58 +03:00
|
|
|
int withLevel = -1;
|
|
|
|
for (curEnv = env.get(), level = 0; curEnv; curEnv = curEnv->up, level++) {
|
|
|
|
if (curEnv->isWith) {
|
|
|
|
if (withLevel == -1) withLevel = level;
|
|
|
|
} else {
|
2020-02-21 19:31:16 +02:00
|
|
|
auto i = curEnv->find(name);
|
2021-10-22 23:49:58 +03:00
|
|
|
if (i != curEnv->vars.end()) {
|
|
|
|
this->level = level;
|
|
|
|
displ = i->second;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 22:02:39 +03:00
|
|
|
}
|
|
|
|
|
2021-10-22 23:49:58 +03:00
|
|
|
/* Otherwise, the variable must be obtained from the nearest
|
|
|
|
enclosing `with'. If there is no `with', then we can issue an
|
|
|
|
"undefined variable" error now. */
|
2022-04-29 20:24:54 +03:00
|
|
|
if (withLevel == -1)
|
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`.
2024-01-23 03:08:29 +02:00
|
|
|
es.error<UndefinedVarError>(
|
|
|
|
"undefined variable '%1%'",
|
|
|
|
es.symbols[name]
|
|
|
|
).atPos(pos).debugThrow();
|
2023-12-22 19:19:53 +02:00
|
|
|
for (auto * e = env.get(); e && !fromWith; e = e->up)
|
|
|
|
fromWith = e->isWith;
|
2021-10-22 23:49:58 +03:00
|
|
|
this->level = withLevel;
|
2008-08-14 13:04:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
void ExprInheritFrom::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
|
|
|
{
|
|
|
|
if (es.debugRepl)
|
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprSelect::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
e->bindVars(es, env);
|
|
|
|
if (def) def->bindVars(es, env);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : attrPath)
|
2022-03-05 15:40:24 +02:00
|
|
|
if (!i.symbol)
|
|
|
|
i.expr->bindVars(es, env);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
2008-08-14 13:04:22 +03:00
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprOpHasAttr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2004-02-03 16:45:34 +02:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
e->bindVars(es, env);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : attrPath)
|
2022-03-05 15:40:24 +02:00
|
|
|
if (!i.symbol)
|
|
|
|
i.expr->bindVars(es, env);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
std::shared_ptr<const StaticEnv> ExprAttrs::bindInheritSources(
|
|
|
|
EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
|
|
|
{
|
|
|
|
if (!inheritFromExprs)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// the inherit (from) source values are inserted into an env of its own, which
|
|
|
|
// does not introduce any variable names.
|
|
|
|
// analysis must see an empty env, or an env that contains only entries with
|
|
|
|
// otherwise unused names to not interfere with regular names. the parser
|
|
|
|
// has already filled all exprs that access this env with appropriate level
|
|
|
|
// and displacement, and nothing else is allowed to access it. ideally we'd
|
|
|
|
// not even *have* an expr that grabs anything from this env since it's fully
|
|
|
|
// invisible, but the evaluator does not allow for this yet.
|
|
|
|
auto inner = std::make_shared<StaticEnv>(nullptr, env.get(), 0);
|
|
|
|
for (auto from : *inheritFromExprs)
|
|
|
|
from->bindVars(es, env);
|
|
|
|
|
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprAttrs::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2010-04-14 17:42:32 +03:00
|
|
|
if (recursive) {
|
2024-01-27 17:33:34 +02:00
|
|
|
auto newEnv = [&] () -> std::shared_ptr<const StaticEnv> {
|
|
|
|
auto newEnv = std::make_shared<StaticEnv>(nullptr, env.get(), attrs.size());
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
Displacement displ = 0;
|
|
|
|
for (auto & i : attrs)
|
|
|
|
newEnv->vars.emplace_back(i.first, i.second.displ = displ++);
|
|
|
|
return newEnv;
|
|
|
|
}();
|
2020-02-21 19:31:16 +02:00
|
|
|
|
|
|
|
// No need to sort newEnv since attrs is in sorted order.
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
auto inheritFromEnv = bindInheritSources(es, newEnv);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : attrs)
|
2024-01-27 17:33:34 +02:00
|
|
|
i.second.e->bindVars(es, i.second.chooseByKind(newEnv, env, inheritFromEnv));
|
2021-09-16 01:16:53 +03:00
|
|
|
|
|
|
|
for (auto & i : dynamicAttrs) {
|
2022-04-28 21:32:57 +03:00
|
|
|
i.nameExpr->bindVars(es, newEnv);
|
|
|
|
i.valueExpr->bindVars(es, newEnv);
|
2021-09-16 01:16:53 +03:00
|
|
|
}
|
2004-10-25 19:54:56 +03:00
|
|
|
}
|
2021-09-16 01:16:53 +03:00
|
|
|
else {
|
2024-01-27 17:33:34 +02:00
|
|
|
auto inheritFromEnv = bindInheritSources(es, env);
|
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : attrs)
|
2024-01-27 17:33:34 +02:00
|
|
|
i.second.e->bindVars(es, i.second.chooseByKind(env, env, inheritFromEnv));
|
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 06:25:30 +03:00
|
|
|
|
2021-09-16 01:16:53 +03:00
|
|
|
for (auto & i : dynamicAttrs) {
|
2022-04-28 21:32:57 +03:00
|
|
|
i.nameExpr->bindVars(es, env);
|
|
|
|
i.valueExpr->bindVars(es, env);
|
2021-09-16 01:16:53 +03:00
|
|
|
}
|
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 06:25:30 +03:00
|
|
|
}
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprList::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems)
|
2022-03-05 15:40:24 +02:00
|
|
|
i->bindVars(es, env);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprLambda::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-05-05 13:29:14 +03:00
|
|
|
auto newEnv = std::make_shared<StaticEnv>(
|
2023-12-22 19:19:53 +02:00
|
|
|
nullptr, env.get(),
|
2022-05-05 13:29:14 +03:00
|
|
|
(hasFormals() ? formals->formals.size() : 0) +
|
|
|
|
(!arg ? 0 : 1));
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2020-02-24 15:33:01 +02:00
|
|
|
Displacement displ = 0;
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2022-04-29 19:02:17 +03:00
|
|
|
if (arg) newEnv->vars.emplace_back(arg, displ++);
|
2010-04-14 17:42:32 +03:00
|
|
|
|
2021-10-06 18:08:08 +03:00
|
|
|
if (hasFormals()) {
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : formals->formals)
|
2021-11-30 23:15:02 +02:00
|
|
|
newEnv->vars.emplace_back(i.name, displ++);
|
2020-02-21 19:31:16 +02:00
|
|
|
|
2021-11-30 23:15:02 +02:00
|
|
|
newEnv->sort();
|
2010-04-14 17:42:32 +03:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : formals->formals)
|
2022-03-05 15:40:24 +02:00
|
|
|
if (i.def) i.def->bindVars(es, newEnv);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
body->bindVars(es, newEnv);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprCall::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
fun->bindVars(es, env);
|
2020-02-24 02:32:01 +02:00
|
|
|
for (auto e : args)
|
2022-03-05 15:40:24 +02:00
|
|
|
e->bindVars(es, env);
|
2020-02-24 02:32:01 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprLet::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2024-01-27 17:33:34 +02:00
|
|
|
auto newEnv = [&] () -> std::shared_ptr<const StaticEnv> {
|
|
|
|
auto newEnv = std::make_shared<StaticEnv>(nullptr, env.get(), attrs->attrs.size());
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
Displacement displ = 0;
|
|
|
|
for (auto & i : attrs->attrs)
|
|
|
|
newEnv->vars.emplace_back(i.first, i.second.displ = displ++);
|
|
|
|
return newEnv;
|
|
|
|
}();
|
2020-02-21 19:31:16 +02:00
|
|
|
|
|
|
|
// No need to sort newEnv since attrs->attrs is in sorted order.
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
auto inheritFromEnv = attrs->bindInheritSources(es, newEnv);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : attrs->attrs)
|
2024-01-27 17:33:34 +02:00
|
|
|
i.second.e->bindVars(es, i.second.chooseByKind(newEnv, env, inheritFromEnv));
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2024-02-03 05:31:18 +02:00
|
|
|
if (es.debugRepl)
|
|
|
|
es.exprEnvs.insert(std::make_pair(this, newEnv));
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
body->bindVars(es, newEnv);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprWith::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2023-12-22 19:19:53 +02:00
|
|
|
parentWith = nullptr;
|
|
|
|
for (auto * e = env.get(); e && !parentWith; e = e->up)
|
|
|
|
parentWith = e->isWith;
|
|
|
|
|
2010-04-14 18:01:04 +03:00
|
|
|
/* Does this `with' have an enclosing `with'? If so, record its
|
2010-04-22 18:08:09 +03:00
|
|
|
level so that `lookupVar' can look up variables in the previous
|
|
|
|
`with' if this one doesn't contain the desired attribute. */
|
2010-04-14 18:01:04 +03:00
|
|
|
const StaticEnv * curEnv;
|
2020-02-24 15:33:01 +02:00
|
|
|
Level level;
|
2010-04-22 18:08:09 +03:00
|
|
|
prevWith = 0;
|
2021-09-14 19:49:22 +03:00
|
|
|
for (curEnv = env.get(), level = 1; curEnv; curEnv = curEnv->up, level++)
|
2010-04-14 18:01:04 +03:00
|
|
|
if (curEnv->isWith) {
|
|
|
|
prevWith = level;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
attrs->bindVars(es, env);
|
2023-12-22 19:19:53 +02:00
|
|
|
auto newEnv = std::make_shared<StaticEnv>(this, env.get());
|
2022-03-05 15:40:24 +02:00
|
|
|
body->bindVars(es, newEnv);
|
2005-11-04 17:17:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprIf::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
cond->bindVars(es, env);
|
|
|
|
then->bindVars(es, env);
|
|
|
|
else_->bindVars(es, env);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprAssert::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
cond->bindVars(es, env);
|
|
|
|
body->bindVars(es, env);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprOpNot::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2010-04-14 17:42:32 +03:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
e->bindVars(es, env);
|
2010-04-14 17:42:32 +03:00
|
|
|
}
|
2005-11-04 17:17:05 +02:00
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprConcatStrings::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2005-11-04 17:17:05 +02:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2021-10-02 22:47:36 +03:00
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
for (auto & i : *this->es)
|
|
|
|
i.second->bindVars(es, env);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
2006-10-16 18:55:34 +03:00
|
|
|
|
2022-05-19 19:48:10 +03:00
|
|
|
void ExprPos::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
|
2013-11-18 21:14:54 +02:00
|
|
|
{
|
2022-05-23 06:45:24 +03:00
|
|
|
if (es.debugRepl)
|
2022-05-19 19:48:10 +03:00
|
|
|
es.exprEnvs.insert(std::make_pair(this, env));
|
2013-11-18 21:14:54 +02:00
|
|
|
}
|
|
|
|
|
2006-10-16 18:55:34 +03:00
|
|
|
|
2013-05-16 20:08:02 +03:00
|
|
|
/* Storing function names. */
|
|
|
|
|
2022-04-22 22:45:39 +03:00
|
|
|
void Expr::setName(Symbol name)
|
2013-05-16 20:08:02 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-22 22:45:39 +03:00
|
|
|
void ExprLambda::setName(Symbol name)
|
2013-05-16 20:08:02 +03:00
|
|
|
{
|
|
|
|
this->name = name;
|
|
|
|
body->setName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
std::string ExprLambda::showNamePos(const EvalState & state) const
|
2013-05-16 20:08:02 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
std::string id(name
|
|
|
|
? concatStrings("'", state.symbols[name], "'")
|
|
|
|
: "anonymous function");
|
|
|
|
return fmt("%1% at %2%", id, state.positions[pos]);
|
2013-05-16 20:08:02 +03:00
|
|
|
}
|
|
|
|
|
2024-07-08 18:39:26 +03:00
|
|
|
void ExprLambda::setDocComment(DocComment docComment) {
|
2024-07-15 15:27:09 +03:00
|
|
|
// RFC 145 specifies that the innermost doc comment wins.
|
|
|
|
// See https://github.com/NixOS/rfcs/blob/master/rfcs/0145-doc-strings.md#ambiguous-placement
|
2024-07-08 18:39:26 +03:00
|
|
|
if (!this->docComment) {
|
|
|
|
this->docComment = docComment;
|
|
|
|
|
|
|
|
// Curried functions are defined by putting a function directly
|
|
|
|
// in the body of another function. To render docs for those, we
|
|
|
|
// need to propagate the doc comment to the innermost function.
|
|
|
|
//
|
|
|
|
// If we have our own comment, we've already propagated it, so this
|
|
|
|
// belongs in the same conditional.
|
|
|
|
body->setDocComment(docComment);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 20:08:02 +03:00
|
|
|
|
2013-10-08 16:34:57 +03:00
|
|
|
|
2024-01-29 07:19:23 +02:00
|
|
|
/* Position table. */
|
|
|
|
|
|
|
|
Pos PosTable::operator[](PosIdx p) const
|
|
|
|
{
|
|
|
|
auto origin = resolve(p);
|
|
|
|
if (!origin)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto offset = origin->offsetOf(p);
|
|
|
|
|
|
|
|
Pos result{0, 0, origin->origin};
|
|
|
|
auto lines = this->lines.lock();
|
|
|
|
auto linesForInput = (*lines)[origin->offset];
|
|
|
|
|
|
|
|
if (linesForInput.empty()) {
|
|
|
|
auto source = result.getSource().value_or("");
|
|
|
|
const char * begin = source.data();
|
|
|
|
for (Pos::LinesIterator it(source), end; it != end; it++)
|
|
|
|
linesForInput.push_back(it->data() - begin);
|
|
|
|
if (linesForInput.empty())
|
|
|
|
linesForInput.push_back(0);
|
|
|
|
}
|
|
|
|
// as above: the first line starts at byte 0 and is always present
|
|
|
|
auto lineStartOffset = std::prev(
|
|
|
|
std::upper_bound(linesForInput.begin(), linesForInput.end(), offset));
|
|
|
|
|
|
|
|
result.line = 1 + (lineStartOffset - linesForInput.begin());
|
|
|
|
result.column = 1 + (offset - *lineStartOffset);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-08 16:34:57 +03:00
|
|
|
/* Symbol table. */
|
|
|
|
|
|
|
|
size_t SymbolTable::totalSize() const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
2022-04-22 22:45:39 +03:00
|
|
|
dump([&] (const std::string & s) { n += s.size(); });
|
2013-10-08 16:34:57 +03:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2024-07-09 20:26:22 +03:00
|
|
|
std::string DocComment::getInnerText(const PosTable & positions) const {
|
|
|
|
auto beginPos = positions[begin];
|
|
|
|
auto endPos = positions[end];
|
2024-07-15 21:08:41 +03:00
|
|
|
auto docCommentStr = beginPos.getSnippetUpTo(endPos).value_or("");
|
2024-07-09 20:26:22 +03:00
|
|
|
|
|
|
|
// Strip "/**" and "*/"
|
|
|
|
constexpr size_t prefixLen = 3;
|
|
|
|
constexpr size_t suffixLen = 2;
|
|
|
|
std::string docStr = docCommentStr.substr(prefixLen, docCommentStr.size() - prefixLen - suffixLen);
|
|
|
|
if (docStr.empty())
|
|
|
|
return {};
|
|
|
|
// Turn the now missing "/**" into indentation
|
|
|
|
docStr = " " + docStr;
|
|
|
|
// Strip indentation (for the whole, potentially multi-line string)
|
|
|
|
docStr = stripIndentation(docStr);
|
|
|
|
return docStr;
|
|
|
|
}
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|