slang-netlist  0.9.0
Loading...
Searching...
No Matches
SymbolReference.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4#include <mutex>
5#include <string>
6#include <string_view>
7#include <utility>
8
10
11#include "slang/util/ConcurrentMap.h"
12
13namespace slang::netlist {
14
17 std::string name;
18 std::string hierarchicalPath;
20
21 SymbolReference() = default;
22 SymbolReference(std::string name, std::string hierarchicalPath,
24 : name(std::move(name)), hierarchicalPath(std::move(hierarchicalPath)),
26
27 auto empty() const -> bool { return name.empty(); }
28};
29
35 // Stable storage: std::deque guarantees addresses survive insertion.
36 std::deque<SymbolReference> entries;
37 // Keys are string_views into entries[i].hierarchicalPath.
38 concurrent_map<std::string_view, SymbolReference const *> indexMap;
39 mutable std::mutex insertMutex;
40
41public:
44 auto intern(std::string_view name, std::string_view hierarchicalPath,
45 TextLocation location) -> SymbolReference const * {
46 SymbolReference const *result = nullptr;
47 if (indexMap.visit(hierarchicalPath,
48 [&](auto const &kv) { result = kv.second; })) {
49 return result;
50 }
51 std::lock_guard lock(insertMutex);
52 if (indexMap.visit(hierarchicalPath,
53 [&](auto const &kv) { result = kv.second; })) {
54 return result;
55 }
56 auto &stored = entries.emplace_back(
57 std::string(name), std::string(hierarchicalPath), location);
58 auto *ptr = &stored;
59 indexMap.emplace(std::string_view(stored.hierarchicalPath), ptr);
60 return ptr;
61 }
62
64 auto intern(SymbolReference const &ref) -> SymbolReference const * {
65 return intern(ref.name, ref.hierarchicalPath, ref.location);
66 }
67
69 auto size() const -> size_t { return entries.size(); }
70};
71
72} // namespace slang::netlist
Definition SymbolReference.hpp:34
auto intern(SymbolReference const &ref) -> SymbolReference const *
Convenience: intern by copying from an existing SymbolReference value.
Definition SymbolReference.hpp:64
auto size() const -> size_t
Number of unique symbol entries currently interned.
Definition SymbolReference.hpp:69
auto intern(std::string_view name, std::string_view hierarchicalPath, TextLocation location) -> SymbolReference const *
Definition SymbolReference.hpp:44
Definition Utilities.hpp:16
Extracted identity of an AST symbol, decoupled from the slang AST.
Definition SymbolReference.hpp:16
TextLocation location
Definition SymbolReference.hpp:19
SymbolReference(std::string name, std::string hierarchicalPath, TextLocation location)
Definition SymbolReference.hpp:22
std::string hierarchicalPath
Definition SymbolReference.hpp:18
auto empty() const -> bool
Definition SymbolReference.hpp:27
std::string name
Definition SymbolReference.hpp:17
Definition TextLocation.hpp:71