P4C
The P4 Compiler
Loading...
Searching...
No Matches
referenceMap.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 COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_
18#define COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_
19
20#include "absl/container/flat_hash_map.h"
21#include "absl/container/flat_hash_set.h"
22#include "frontends/common/programMap.h"
23#include "ir/ir.h"
24#include "ir/visitor.h"
25#include "lib/cstring.h"
26
27namespace P4 {
28
30 public:
31 virtual cstring newName(std::string_view base) = 0;
32 virtual ~NameGenerator() = default;
33};
34
35// replacement for ReferenceMap NameGenerator to make it easier to remove uses of refMap
36class MinimalNameGenerator : public NameGenerator, public Inspector {
39 absl::flat_hash_map<cstring, int, Util::Hash> usedNames;
40 void postorder(const IR::Path *p) override { usedName(p->name.name); }
41 void postorder(const IR::Type_Declaration *t) override { usedName(t->name.name); }
42 void postorder(const IR::Declaration *d) override { usedName(d->name.name); }
43
44 public:
45 MinimalNameGenerator();
46 void usedName(cstring name) { usedNames.emplace(name, 0); }
47 explicit MinimalNameGenerator(const IR::Node *root) : MinimalNameGenerator() {
48 root->apply(*this);
49 }
50
52 cstring newName(std::string_view base) override;
53};
54
55// FIXME -- temp common base class to allow use of ReferenceMap or ResolutionContext
56// interchangeably when looking up declarations. This should go away once the refMap does
58 public:
59 virtual ~DeclarationLookup() = default;
60 virtual const IR::IDeclaration *getDeclaration(const IR::Path *,
61 bool notNull = false) const = 0;
62 virtual const IR::IDeclaration *getDeclaration(const IR::This *,
63 bool notNull = false) const = 0;
64};
65
67class ReferenceMap final : public ProgramMap, public NameGenerator, public DeclarationLookup {
70 bool isv1;
71
73 absl::flat_hash_map<const IR::Path *, const IR::IDeclaration *, Util::Hash> pathToDeclaration;
74
76 absl::flat_hash_set<const IR::IDeclaration *, Util::Hash> used;
77
79 absl::flat_hash_map<const IR::This *, const IR::IDeclaration *, Util::Hash> thisToDeclaration;
80
83 absl::flat_hash_map<cstring, int, Util::Hash> usedNames;
84
85 public:
86 ReferenceMap();
89 const IR::IDeclaration *getDeclaration(const IR::Path *path,
90 bool notNull = false) const override;
91
93 void setDeclaration(const IR::Path *path, const IR::IDeclaration *decl);
94
97 const IR::IDeclaration *getDeclaration(const IR::This *pointer,
98 bool notNull = false) const override;
99
101 void setDeclaration(const IR::This *pointer, const IR::IDeclaration *decl);
102
103 void dbprint(std::ostream &cout) const override;
104
106 void setIsV1(bool isv1) { this->isv1 = isv1; }
107 void setAnyOrder(bool anyOrder) { this->isv1 = anyOrder; }
108
110 cstring newName(std::string_view base) override;
111
113 void clear();
114
116 bool isV1() const { return isv1; }
117
119 bool isUsed(const IR::IDeclaration *decl) const { return used.count(decl) > 0; }
120
122 void usedName(cstring name) { usedNames.emplace(name, 0); }
123};
124
125} // namespace P4
126
127#endif /* COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_ */
Definition referenceMap.h:57
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition node.h:94
Definition visitor.h:413
cstring newName(std::string_view base) override
Generate a name from base that does not appear in usedNames.
Definition referenceMap.cpp:118
Definition referenceMap.h:29
const IR::IDeclaration * getDeclaration(const IR::Path *path, bool notNull=false) const override
Definition referenceMap.cpp:78
void setDeclaration(const IR::Path *path, const IR::IDeclaration *decl)
Sets declaration for path to decl.
Definition referenceMap.cpp:42
void clear()
Clear the reference map.
Definition referenceMap.cpp:31
void usedName(cstring name)
Indicate that name is used in the program.
Definition referenceMap.h:122
cstring newName(std::string_view base) override
Generate a name from base that fresh for the program.
Definition referenceMap.cpp:96
void setIsV1(bool isv1)
Set boolean indicating whether map is for a P4_14 program to isV1.
Definition referenceMap.h:106
bool isUsed(const IR::IDeclaration *decl) const
Definition referenceMap.h:119
bool isV1() const
Definition referenceMap.h:116
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24