2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2023-04-01 03:18:41 +00:00
|
|
|
///@file
|
2010-04-13 12:25:42 +00:00
|
|
|
|
2021-12-20 10:29:14 +00:00
|
|
|
#include <list>
|
2010-04-13 12:25:42 +00:00
|
|
|
#include <map>
|
2021-12-20 10:29:14 +00:00
|
|
|
#include <unordered_map>
|
2010-04-13 12:25:42 +00:00
|
|
|
|
|
|
|
#include "types.hh"
|
2022-04-22 08:01:02 +00:00
|
|
|
#include "chunked-vector.hh"
|
2010-04-13 12:25:42 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* This class mainly exists to give us an operator<< for ostreams. We could also
|
|
|
|
* return plain strings from SymbolTable, but then we'd have to wrap every
|
|
|
|
* instance of a symbol that is fmt()ed, which is inconvenient and error-prone.
|
|
|
|
*/
|
2022-04-22 19:45:39 +00:00
|
|
|
class SymbolStr
|
2010-04-13 12:25:42 +00:00
|
|
|
{
|
|
|
|
friend class SymbolTable;
|
2022-04-22 19:45:39 +00:00
|
|
|
|
2022-03-05 13:40:24 +00:00
|
|
|
private:
|
2022-04-22 19:45:39 +00:00
|
|
|
const std::string * s;
|
2010-04-13 12:25:42 +00:00
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
explicit SymbolStr(const std::string & symbol): s(&symbol) {}
|
2013-05-16 17:08:02 +00:00
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
public:
|
2020-04-16 23:02:29 +00:00
|
|
|
bool operator == (std::string_view s2) const
|
|
|
|
{
|
2022-04-22 19:45:39 +00:00
|
|
|
return *s == s2;
|
2010-04-13 12:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
operator const std::string & () const
|
|
|
|
{
|
2022-04-22 19:45:39 +00:00
|
|
|
return *s;
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
operator const std::string_view () const
|
2013-05-16 17:08:02 +00:00
|
|
|
{
|
2022-04-22 19:45:39 +00:00
|
|
|
return *s;
|
2013-05-16 17:08:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
friend std::ostream & operator <<(std::ostream & os, const SymbolStr & symbol);
|
2010-04-13 12:25:42 +00:00
|
|
|
};
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* Symbols have the property that they can be compared efficiently
|
|
|
|
* (using an equality test), because the symbol table stores only one
|
|
|
|
* copy of each string.
|
|
|
|
*/
|
2022-04-22 19:45:39 +00:00
|
|
|
class Symbol
|
2022-03-05 13:40:24 +00:00
|
|
|
{
|
|
|
|
friend class SymbolTable;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t id;
|
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
explicit Symbol(uint32_t id): id(id) {}
|
2022-03-05 13:40:24 +00:00
|
|
|
|
|
|
|
public:
|
2022-04-22 19:45:39 +00:00
|
|
|
Symbol() : id(0) {}
|
2022-03-05 13:40:24 +00:00
|
|
|
|
|
|
|
explicit operator bool() const { return id > 0; }
|
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
bool operator<(const Symbol other) const { return id < other.id; }
|
|
|
|
bool operator==(const Symbol other) const { return id == other.id; }
|
|
|
|
bool operator!=(const Symbol other) const { return id != other.id; }
|
2022-03-05 13:40:24 +00:00
|
|
|
};
|
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* Symbol table used by the parser and evaluator to represent and look
|
|
|
|
* up identifiers and attributes efficiently.
|
|
|
|
*/
|
2010-04-13 12:25:42 +00:00
|
|
|
class SymbolTable
|
|
|
|
{
|
|
|
|
private:
|
2022-04-22 19:45:39 +00:00
|
|
|
std::unordered_map<std::string_view, std::pair<const std::string *, uint32_t>> symbols;
|
|
|
|
ChunkedVector<std::string, 8192> store{16};
|
2010-04-13 12:25:42 +00:00
|
|
|
|
|
|
|
public:
|
2022-04-26 12:16:20 +00:00
|
|
|
|
2023-04-07 13:55:28 +00:00
|
|
|
/**
|
|
|
|
* converts a string into a symbol.
|
|
|
|
*/
|
2022-04-22 19:45:39 +00:00
|
|
|
Symbol create(std::string_view s)
|
2010-04-13 12:25:42 +00:00
|
|
|
{
|
2021-12-20 10:29:14 +00:00
|
|
|
// Most symbols are looked up more than once, so we trade off insertion performance
|
|
|
|
// for lookup performance.
|
|
|
|
// TODO: could probably be done more efficiently with transparent Hash and Equals
|
|
|
|
// on the original implementation using unordered_set
|
2022-04-26 12:16:20 +00:00
|
|
|
// FIXME: make this thread-safe.
|
2021-12-20 10:29:14 +00:00
|
|
|
auto it = symbols.find(s);
|
2022-04-22 19:45:39 +00:00
|
|
|
if (it != symbols.end()) return Symbol(it->second.second + 1);
|
2021-12-20 10:29:14 +00:00
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
const auto & [rawSym, idx] = store.add(std::string(s));
|
2022-03-05 13:40:24 +00:00
|
|
|
symbols.emplace(rawSym, std::make_pair(&rawSym, idx));
|
2022-04-22 19:45:39 +00:00
|
|
|
return Symbol(idx + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SymbolStr> resolve(const std::vector<Symbol> & symbols) const
|
|
|
|
{
|
|
|
|
std::vector<SymbolStr> result;
|
|
|
|
result.reserve(symbols.size());
|
|
|
|
for (auto sym : symbols)
|
|
|
|
result.push_back((*this)[sym]);
|
|
|
|
return result;
|
2022-03-05 13:40:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 19:45:39 +00:00
|
|
|
SymbolStr operator[](Symbol s) const
|
2022-03-05 13:40:24 +00:00
|
|
|
{
|
|
|
|
if (s.id == 0 || s.id > store.size())
|
|
|
|
abort();
|
2022-04-22 19:45:39 +00:00
|
|
|
return SymbolStr(store[s.id - 1]);
|
2010-04-13 12:25:42 +00:00
|
|
|
}
|
2010-04-13 14:34:11 +00:00
|
|
|
|
2018-05-02 11:56:34 +00:00
|
|
|
size_t size() const
|
2010-04-13 14:34:11 +00:00
|
|
|
{
|
2022-03-05 13:40:24 +00:00
|
|
|
return store.size();
|
2010-04-13 14:34:11 +00:00
|
|
|
}
|
2013-10-08 13:34:57 +00:00
|
|
|
|
|
|
|
size_t totalSize() const;
|
2019-04-11 21:04:13 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2022-03-05 13:40:24 +00:00
|
|
|
void dump(T callback) const
|
2019-04-11 21:04:13 +00:00
|
|
|
{
|
2022-03-05 13:40:24 +00:00
|
|
|
store.forEach(callback);
|
2019-04-11 21:04:13 +00:00
|
|
|
}
|
2010-04-13 12:25:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|