2015-07-14 17:18:56 +00:00
|
|
|
#pragma once
|
2023-04-01 03:18:41 +00:00
|
|
|
///@file
|
2015-07-14 17:18:56 +00:00
|
|
|
|
|
|
|
#include "nixexpr.hh"
|
|
|
|
#include "symbol-table.hh"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-11-26 18:57:20 +00:00
|
|
|
#include <optional>
|
2015-07-14 17:18:56 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
class EvalState;
|
|
|
|
struct Value;
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* Map one attribute name to its value.
|
|
|
|
*/
|
2015-07-14 17:18:56 +00:00
|
|
|
struct Attr
|
|
|
|
{
|
2022-03-08 14:26:17 +00:00
|
|
|
/* the placement of `name` and `pos` in this struct is important.
|
|
|
|
both of them are uint32 wrappers, they are next to each other
|
|
|
|
to make sure that Attr has no padding on 64 bit machines. that
|
|
|
|
way we keep Attr size at two words with no wasted space. */
|
2022-04-22 19:45:39 +00:00
|
|
|
Symbol name;
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2022-03-08 14:26:17 +00:00
|
|
|
Value * value;
|
2022-04-22 19:45:39 +00:00
|
|
|
Attr(Symbol name, Value * value, PosIdx pos = noPos)
|
2022-03-08 14:26:17 +00:00
|
|
|
: name(name), pos(pos), value(value) { };
|
2022-03-04 18:31:59 +00:00
|
|
|
Attr() { };
|
2015-07-14 17:18:56 +00:00
|
|
|
bool operator < (const Attr & a) const
|
|
|
|
{
|
|
|
|
return name < a.name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-08 14:26:17 +00:00
|
|
|
static_assert(sizeof(Attr) == 2 * sizeof(uint32_t) + sizeof(Value *),
|
|
|
|
"performance of the evaluator is highly sensitive to the size of Attr. "
|
|
|
|
"avoid introducing any padding into Attr if at all possible, and do not "
|
|
|
|
"introduce new fields that need not be present for almost every instance.");
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* Bindings contains all the attributes of an attribute set. It is defined
|
|
|
|
* by its size and its capacity, the capacity being the number of Attr
|
|
|
|
* elements allocated after this structure, while the size corresponds to
|
|
|
|
* the number of elements already inserted in this structure.
|
|
|
|
*/
|
2015-07-14 17:18:56 +00:00
|
|
|
class Bindings
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef uint32_t size_t;
|
2022-03-04 18:31:59 +00:00
|
|
|
PosIdx pos;
|
2015-07-14 17:18:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t size_, capacity_;
|
|
|
|
Attr attrs[0];
|
|
|
|
|
2022-03-04 18:31:59 +00:00
|
|
|
Bindings(size_t capacity) : size_(0), capacity_(capacity) { }
|
2015-07-14 17:18:56 +00:00
|
|
|
Bindings(const Bindings & bindings) = delete;
|
|
|
|
|
|
|
|
public:
|
|
|
|
size_t size() const { return size_; }
|
|
|
|
|
|
|
|
bool empty() const { return !size_; }
|
|
|
|
|
|
|
|
typedef Attr * iterator;
|
|
|
|
|
|
|
|
void push_back(const Attr & attr)
|
|
|
|
{
|
|
|
|
assert(size_ < capacity_);
|
|
|
|
attrs[size_++] = attr;
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:23:32 +00:00
|
|
|
iterator find(Symbol name)
|
2015-07-14 17:18:56 +00:00
|
|
|
{
|
|
|
|
Attr key(name, 0);
|
|
|
|
iterator i = std::lower_bound(begin(), end(), key);
|
|
|
|
if (i != end() && i->name == name) return i;
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:23:32 +00:00
|
|
|
Attr * get(Symbol name)
|
2018-11-26 18:57:20 +00:00
|
|
|
{
|
|
|
|
Attr key(name, 0);
|
|
|
|
iterator i = std::lower_bound(begin(), end(), key);
|
|
|
|
if (i != end() && i->name == name) return &*i;
|
2020-02-13 16:15:05 +00:00
|
|
|
return nullptr;
|
2018-11-26 18:57:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 17:18:56 +00:00
|
|
|
iterator begin() { return &attrs[0]; }
|
|
|
|
iterator end() { return &attrs[size_]; }
|
|
|
|
|
|
|
|
Attr & operator[](size_t pos)
|
|
|
|
{
|
|
|
|
return attrs[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
void sort();
|
|
|
|
|
|
|
|
size_t capacity() { return capacity_; }
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the attributes in lexicographically sorted order.
|
|
|
|
*/
|
2022-03-05 13:40:24 +00:00
|
|
|
std::vector<const Attr *> lexicographicOrder(const SymbolTable & symbols) const
|
2017-01-25 15:06:50 +00:00
|
|
|
{
|
|
|
|
std::vector<const Attr *> res;
|
|
|
|
res.reserve(size_);
|
|
|
|
for (size_t n = 0; n < size_; n++)
|
|
|
|
res.emplace_back(&attrs[n]);
|
2022-03-05 13:40:24 +00:00
|
|
|
std::sort(res.begin(), res.end(), [&](const Attr * a, const Attr * b) {
|
|
|
|
std::string_view sa = symbols[a->name], sb = symbols[b->name];
|
|
|
|
return sa < sb;
|
2017-01-25 15:06:50 +00:00
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:18:56 +00:00
|
|
|
friend class EvalState;
|
|
|
|
};
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* A wrapper around Bindings that ensures that its always in sorted
|
|
|
|
* order at the end. The only way to consume a BindingsBuilder is to
|
|
|
|
* call finish(), which sorts the bindings.
|
|
|
|
*/
|
2022-01-04 16:39:16 +00:00
|
|
|
class BindingsBuilder
|
|
|
|
{
|
|
|
|
Bindings * bindings;
|
|
|
|
|
|
|
|
public:
|
2022-01-01 23:41:21 +00:00
|
|
|
// needed by std::back_inserter
|
|
|
|
using value_type = Attr;
|
2022-01-04 16:39:16 +00:00
|
|
|
|
2022-01-04 19:29:17 +00:00
|
|
|
EvalState & state;
|
|
|
|
|
2022-01-04 16:39:16 +00:00
|
|
|
BindingsBuilder(EvalState & state, Bindings * bindings)
|
2022-01-04 19:29:17 +00:00
|
|
|
: bindings(bindings), state(state)
|
2022-01-04 16:39:16 +00:00
|
|
|
{ }
|
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
void insert(Symbol name, Value * value, PosIdx pos = noPos)
|
2022-01-04 16:39:16 +00:00
|
|
|
{
|
|
|
|
insert(Attr(name, value, pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(const Attr & attr)
|
2022-01-01 23:41:21 +00:00
|
|
|
{
|
|
|
|
push_back(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(const Attr & attr)
|
2022-01-04 16:39:16 +00:00
|
|
|
{
|
|
|
|
bindings->push_back(attr);
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:23:32 +00:00
|
|
|
Value & alloc(Symbol name, PosIdx pos = noPos);
|
2022-01-04 16:39:16 +00:00
|
|
|
|
2022-03-04 18:31:59 +00:00
|
|
|
Value & alloc(std::string_view name, PosIdx pos = noPos);
|
2022-01-04 16:39:16 +00:00
|
|
|
|
|
|
|
Bindings * finish()
|
|
|
|
{
|
|
|
|
bindings->sort();
|
|
|
|
return bindings;
|
|
|
|
}
|
2022-01-04 19:29:17 +00:00
|
|
|
|
|
|
|
Bindings * alreadySorted()
|
|
|
|
{
|
|
|
|
return bindings;
|
|
|
|
}
|
2022-01-04 16:39:16 +00:00
|
|
|
};
|
2015-07-14 17:18:56 +00:00
|
|
|
|
|
|
|
}
|