2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2010-04-13 15:25:42 +03:00
|
|
|
|
2021-12-20 12:29:14 +02:00
|
|
|
#include <list>
|
2010-04-13 15:25:42 +03:00
|
|
|
#include <map>
|
2021-12-20 12:29:14 +02:00
|
|
|
#include <unordered_map>
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
#include "types.hh"
|
2022-04-22 11:01:02 +03:00
|
|
|
#include "chunked-vector.hh"
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/* Symbol table used by the parser and evaluator to represent and look
|
2013-10-24 17:41:04 +03: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 15:25:42 +03:00
|
|
|
|
|
|
|
class Symbol
|
|
|
|
{
|
|
|
|
friend class SymbolTable;
|
2022-03-05 15:40:24 +02:00
|
|
|
private:
|
|
|
|
std::string s;
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
public:
|
2022-03-05 15:40:24 +02:00
|
|
|
Symbol(std::string_view s) : s(s) { }
|
2013-05-16 20:08:02 +03:00
|
|
|
|
2020-04-17 02:02:29 +03:00
|
|
|
// FIXME: remove
|
|
|
|
bool operator == (std::string_view s2) const
|
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
return s == s2;
|
2010-04-13 15:25:42 +03:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
operator const std::string & () const
|
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
return s;
|
2019-12-05 20:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
operator const std::string_view () const
|
2013-05-16 20:08:02 +03:00
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-04-13 15:25:42 +03:00
|
|
|
friend std::ostream & operator << (std::ostream & str, const Symbol & sym);
|
|
|
|
};
|
|
|
|
|
2022-03-05 15:40:24 +02: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 15:25:42 +03:00
|
|
|
class SymbolTable
|
|
|
|
{
|
|
|
|
private:
|
2022-03-05 15:40:24 +02:00
|
|
|
std::unordered_map<std::string_view, std::pair<const Symbol *, uint32_t>> symbols;
|
|
|
|
ChunkedVector<Symbol, 8192> store{16};
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
public:
|
2022-03-05 15:40:24 +02:00
|
|
|
SymbolIdx create(std::string_view s)
|
2010-04-13 15:25:42 +03:00
|
|
|
{
|
2021-12-20 12:29:14 +02: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 15:40:24 +02:00
|
|
|
if (it != symbols.end()) return SymbolIdx(it->second.second + 1);
|
2021-12-20 12:29:14 +02:00
|
|
|
|
2022-03-05 15:40:24 +02: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 15:25:42 +03:00
|
|
|
}
|
2010-04-13 17:34:11 +03:00
|
|
|
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t size() const
|
2010-04-13 17:34:11 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
return store.size();
|
2010-04-13 17:34:11 +03:00
|
|
|
}
|
2013-10-08 16:34:57 +03:00
|
|
|
|
|
|
|
size_t totalSize() const;
|
2019-04-12 00:04:13 +03:00
|
|
|
|
|
|
|
template<typename T>
|
2022-03-05 15:40:24 +02:00
|
|
|
void dump(T callback) const
|
2019-04-12 00:04:13 +03:00
|
|
|
{
|
2022-03-05 15:40:24 +02:00
|
|
|
store.forEach(callback);
|
2019-04-12 00:04:13 +03:00
|
|
|
}
|
2010-04-13 15:25:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|