39 typedef MAP<cstring, const T *, COMP, ALLOC> map_t;
43 auto match_name(
cstring name,
const U *obj) ->
decltype(name == obj->name) {
44 return name == obj->name;
46 bool match_name(
cstring,
const void *) {
return true; }
48 auto obj_name(
const U *obj) ->
decltype(obj->name) {
51 cstring obj_name(
const void *) {
return nullptr; }
60 typedef typename map_t::value_type value_type;
61 typedef typename map_t::iterator iterator;
62 typedef typename map_t::const_iterator const_iterator;
63 typedef typename map_t::reverse_iterator reverse_iterator;
64 typedef typename map_t::const_reverse_iterator const_reverse_iterator;
71 const T *operator=(
const T *v)
const {
72 if (!self.match_name(name, v)) BUG(
"Inserting into NameMap with incorrect name");
73 return self.symbols[name] = v;
75 operator const T *()
const {
return self.count(name) ? self.at(name) :
nullptr; }
79 const_iterator begin()
const {
return symbols.begin(); }
80 const_iterator end()
const {
return symbols.end(); }
81 iterator begin() {
return symbols.begin(); }
82 iterator end() {
return symbols.end(); }
84 std::pair<const_iterator, const_iterator> equal_range(
cstring key)
const {
85 return symbols.equal_range(key);
87 const_reverse_iterator rbegin()
const {
return symbols.rbegin(); }
88 const_reverse_iterator rend()
const {
return symbols.rend(); }
89 reverse_iterator rbegin() {
return symbols.rbegin(); }
90 reverse_iterator rend() {
return symbols.rend(); }
91 size_t count(
cstring name)
const {
return symbols.count(name); }
92 void clear() { symbols.clear(); }
93 size_t size()
const {
return symbols.size(); }
94 bool empty()
const {
return symbols.empty(); }
95 size_t erase(
cstring n) {
return symbols.erase(n); }
97 iterator erase(iterator p) {
return symbols.erase(p); }
98 iterator erase(iterator f, iterator l) {
return symbols.erase(f, l); }
99 const_iterator find(
cstring name)
const {
return symbols.find(name); }
101 const U *get(
cstring name)
const {
102 for (
auto it = symbols.find(name); it != symbols.end() && it->first == name; it++)
103 if (
auto rv = it->second->template
to<U>())
return rv;
106 void add(
cstring name,
const T *n) {
107 if (!match_name(name, n)) BUG(
"Inserting into NameMap with incorrect name");
108 symbols.emplace(std::move(name), std::move(n));
112 void addUnique(
cstring name,
const T *n) {
113 auto prev = symbols.find(name);
114 if (prev != symbols.end())
115 ::P4::error(ErrorType::ERR_DUPLICATE,
"%1%: duplicated name (%2% is previous instance)",
117 symbols.emplace(std::move(name), std::move(n));
121 const T *getUnique(
cstring name)
const {
122 auto it = symbols.find(name);
123 if (it == symbols.end())
return nullptr;
126 elem_ref operator[](
cstring name) {
return elem_ref(*
this, name); }
127 const T *operator[](
cstring name)
const {
return count(name) ? at(name) :
nullptr; }
128 const T *&at(
cstring name) {
return symbols.at(name); }
129 const T *
const &at(
cstring name)
const {
return symbols.at(name); }
130 void check_null()
const {
131 for (
auto &e : symbols) CHECK_NULL(e.second);
135 bool operator==(
const Node &a)
const override {
return a.operator==(*this); }
136 bool operator==(
const NameMap &a)
const {
return symbols == a.symbols; }
137 bool equiv(
const Node &a_)
const override {
138 if (
static_cast<const Node *
>(
this) == &a_)
return true;
141 if (size() != a.size())
return false;
143 for (
auto &el : *
this)
144 if (el.first != it->first || !el.second->equiv(*(it++)->second))
return false;
147 cstring node_type_name()
const override {
return "NameMap<" + T::static_type_name() +
">"; }
148 static cstring static_type_name() {
return "NameMap<" + T::static_type_name() +
">"; }
149 void visit_children(
Visitor &v)
override;
150 void visit_children(
Visitor &v)
const override;
155 return Util::enumerate(Values(symbols));
157 template <
typename S>
160 [](
const T *d) {
return d !=
nullptr; });
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51