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
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:
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 const IR::IDeclaration *getDeclaration(const IR::Path *,
60 bool notNull = false) const = 0;
61 virtual const IR::IDeclaration *getDeclaration(const IR::This *,
62 bool notNull = false) const = 0;
63};
64
66class ReferenceMap final : public ProgramMap, public NameGenerator, public DeclarationLookup {
69 bool isv1;
70
72 absl::flat_hash_map<const IR::Path *, const IR::IDeclaration *, Util::Hash> pathToDeclaration;
73
75 absl::flat_hash_set<const IR::IDeclaration *, Util::Hash> used;
76
78 absl::flat_hash_map<const IR::This *, const IR::IDeclaration *, Util::Hash> thisToDeclaration;
79
82 absl::flat_hash_map<cstring, int, Util::Hash> usedNames;
83
84 public:
88 const IR::IDeclaration *getDeclaration(const IR::Path *path,
89 bool notNull = false) const override;
90
92 void setDeclaration(const IR::Path *path, const IR::IDeclaration *decl);
93
96 const IR::IDeclaration *getDeclaration(const IR::This *pointer,
97 bool notNull = false) const override;
98
100 void setDeclaration(const IR::This *pointer, const IR::IDeclaration *decl);
101
102 void dbprint(std::ostream &cout) const override;
103
105 void setIsV1(bool isv1) { this->isv1 = isv1; }
106 void setAnyOrder(bool anyOrder) { this->isv1 = anyOrder; }
107
109 cstring newName(std::string_view base) override;
110
112 void clear();
113
115 bool isV1() const { return isv1; }
116
118 bool isUsed(const IR::IDeclaration *decl) const { return used.count(decl) > 0; }
119
121 void usedName(cstring name) { usedNames.emplace(name, 0); }
122};
123
124} // namespace P4
125
126#endif /* COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_ */
Definition referenceMap.h:57
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition node.h:95
Definition visitor.h:400
Definition referenceMap.h:36
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
Definition programMap.h:28
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
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:121
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:105
bool isUsed(const IR::IDeclaration *decl) const
Definition referenceMap.h:118
bool isV1() const
Definition referenceMap.h:115
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24