P4C
The P4 Compiler
Loading...
Searching...
No Matches
nodemap.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef IR_NODEMAP_H_
18#define IR_NODEMAP_H_
19
20#include "ir/node.h"
21#include "lib/cstring.h"
22
23namespace P4::IR {
24
25template <class KEY, class VALUE,
26 template <class K, class V, class COMP, class ALLOC> class MAP = std::map,
27 class COMP = std::less<cstring>,
28 class ALLOC = std::allocator<std::pair<const KEY *const, const VALUE *>>>
29class NodeMap : public Node {
30 typedef MAP<const KEY *, const VALUE *, COMP, ALLOC> map_t;
31 map_t symbols;
32
33 public:
34 typedef typename map_t::value_type value_type;
35 typedef typename map_t::iterator iterator;
36 typedef typename map_t::const_iterator const_iterator;
37 typedef typename map_t::reverse_iterator reverse_iterator;
38 typedef typename map_t::const_reverse_iterator const_reverse_iterator;
39
40 private:
41 struct elem_ref {
42 NodeMap &self;
43 const KEY *key;
44 elem_ref(NodeMap &s, const KEY *k) : self(s), key(k) {}
45 const VALUE *operator=(const VALUE *v) const { return self.symbols[key] = v; }
46 operator const VALUE *() const { return self.symbols.at(key); }
47 };
48
49 public:
50 const_iterator begin() const { return symbols.begin(); }
51 const_iterator end() const { return symbols.end(); }
52 iterator begin() { return symbols.begin(); }
53 iterator end() { return symbols.end(); }
54 const_reverse_iterator rbegin() const { return symbols.rbegin(); }
55 const_reverse_iterator rend() const { return symbols.rend(); }
56 reverse_iterator rbegin() { return symbols.rbegin(); }
57 reverse_iterator rend() { return symbols.rend(); }
58 size_t count(cstring name) const { return symbols.count(name); }
59 size_t size() const { return symbols.size(); }
60 bool empty() const { return symbols.empty(); }
61 size_t erase(const KEY *k) { return symbols.erase(k); }
62 iterator erase(const_iterator p) { return symbols.erase(p); }
63 iterator erase(const_iterator f, const_iterator l) { return symbols.erase(f, l); }
64 const_iterator find(const KEY *k) const { return symbols.find(k); }
65 template <class U>
66 const U *get(const KEY *k) const {
67 for (auto it = symbols.find(k); it != symbols.end() && it->first == k; it++)
68 if (auto rv = it->second->template to<U>()) return rv;
69 return nullptr;
70 }
71 elem_ref operator[](const KEY *k) { return elem_ref(*this, k); }
72 const VALUE *operator[](const KEY *k) const { return symbols.at(k); }
73 const VALUE *&at(const KEY *k) { return symbols.at(k); }
74 const VALUE *const &at(const KEY *k) const { return symbols.at(k); }
75 IRNODE_SUBCLASS(NodeMap)
76 bool operator==(const Node &a) const override { return a == *this; }
77 bool operator==(const NodeMap &a) const { return symbols == a.symbols; }
78 bool equiv(const Node &a_) const override {
79 if (static_cast<const Node *>(this) == &a_) return true;
80 if (this->typeId() != a_.typeId()) return false;
81 auto &a = static_cast<const NodeMap<KEY, VALUE, MAP, COMP, ALLOC> &>(a_);
82 if (size() != a.size()) return false;
83 auto it = a.begin();
84 for (auto &el : *this)
85 if (el.first != it->first || !el.second->equiv(*(it++)->second)) return false;
86 return true;
87 }
88 cstring node_type_name() const override {
89 return "NodeMap<" + KEY::static_type_name() + "," + VALUE::static_type_name() + ">";
90 }
91 static cstring static_type_name() {
92 return "NodeMap<" + KEY::static_type_name() + "," + VALUE::static_type_name() + ">";
93 }
94 void visit_children(Visitor &v) override;
95 void visit_children(Visitor &v) const override;
96
97 DECLARE_TYPEINFO(NodeMap, Node);
98};
99
100} // namespace P4::IR
101
102#endif /* IR_NODEMAP_H_ */
Definition node.h:95
Definition nodemap.h:29
Definition visitor.h:75
Definition cstring.h:85
T * to() noexcept
Definition rtti.h:226
virtual TypeId typeId() const noexcept=0