2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
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 {
|
|
|
|
|
|
|
|
/* Symbol table used by the parser and evaluator to represent and look
|
2013-10-24 14:41:04 +00:00
|
|
|
up identifiers and attributes efficiently. SymbolTable::create()
|
|
|
|
converts a string into a symbol. Symbols have the property that
|
|
|
|
they can be compared efficiently (using a pointer equality test),
|
|
|
|
because the symbol table stores only one copy of each string. */
|
2010-04-13 12:25:42 +00:00
|
|
|
|
|
|
|
class Symbol
|
|
|
|
{
|
|
|
|
friend class SymbolTable;
|
2022-03-05 13:40:24 +00:00
|
|
|
private:
|
|
|
|
std::string s;
|
2010-04-13 12:25:42 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-05 13:40:24 +00:00
|
|
|
Symbol(std::string_view s) : s(s) { }
|
2013-05-16 17:08:02 +00:00
|
|
|
|
2020-04-16 23:02:29 +00:00
|
|
|
// FIXME: remove
|
|
|
|
bool operator == (std::string_view s2) const
|
|
|
|
{
|
2022-03-05 13:40:24 +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-03-05 13:40:24 +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
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-04-13 12:25:42 +00:00
|
|
|
friend std::ostream & operator << (std::ostream & str, const Symbol & sym);
|
|
|
|
};
|
|
|
|
|
2022-03-05 13:40:24 +00:00
|
|
|
class SymbolIdx
|
|
|
|
{
|
|
|
|
friend class SymbolTable;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
explicit SymbolIdx(uint32_t id): id(id) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
SymbolIdx() : id(0) {}
|
|
|
|
|
|
|
|
explicit operator bool() const { return id > 0; }
|
|
|
|
|
|
|
|
bool operator<(const SymbolIdx other) const { return id < other.id; }
|
|
|
|
bool operator==(const SymbolIdx other) const { return id == other.id; }
|
|
|
|
bool operator!=(const SymbolIdx other) const { return id != other.id; }
|
|
|
|
};
|
|
|
|
|
2010-04-13 12:25:42 +00:00
|
|
|
class SymbolTable
|
|
|
|
{
|
|
|
|
private:
|
2022-03-05 13:40:24 +00:00
|
|
|
std::unordered_map<std::string_view, std::pair<const Symbol *, uint32_t>> symbols;
|
|
|
|
ChunkedVector<Symbol, 8192> store{16};
|
2010-04-13 12:25:42 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-05 13:40:24 +00:00
|
|
|
SymbolIdx 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
|
|
|
|
auto it = symbols.find(s);
|
2022-03-05 13:40:24 +00:00
|
|
|
if (it != symbols.end()) return SymbolIdx(it->second.second + 1);
|
2021-12-20 10:29:14 +00:00
|
|
|
|
2022-03-05 13:40:24 +00:00
|
|
|
const auto & [rawSym, idx] = store.add(s);
|
|
|
|
symbols.emplace(rawSym, std::make_pair(&rawSym, idx));
|
|
|
|
return SymbolIdx(idx + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Symbol & operator[](SymbolIdx s) const
|
|
|
|
{
|
|
|
|
if (s.id == 0 || s.id > store.size())
|
|
|
|
abort();
|
|
|
|
return 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|