P4C
The P4 Compiler
Loading...
Searching...
No Matches
referenceMap.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_
9#define FRONTENDS_COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_
10
11#include "absl/container/flat_hash_map.h"
12#include "absl/container/flat_hash_set.h"
13#include "frontends/common/programMap.h"
14#include "ir/ir.h"
15#include "ir/visitor.h"
16#include "lib/cstring.h"
17
18namespace P4 {
19
21 public:
22 virtual cstring newName(std::string_view base) = 0;
23 virtual ~NameGenerator() = default;
24};
25
26// replacement for ReferenceMap NameGenerator to make it easier to remove uses of refMap
27class MinimalNameGenerator : public NameGenerator, public Inspector {
30 absl::flat_hash_map<cstring, int, Util::Hash> usedNames;
31 void postorder(const IR::Path *p) override { usedName(p->name.name); }
32 void postorder(const IR::Type_Declaration *t) override { usedName(t->name.name); }
33 void postorder(const IR::Declaration *d) override { usedName(d->name.name); }
34
35 public:
36 MinimalNameGenerator();
37 void usedName(cstring name) { usedNames.emplace(name, 0); }
38 explicit MinimalNameGenerator(const IR::Node *root) : MinimalNameGenerator() {
39 root->apply(*this);
40 }
41
43 cstring newName(std::string_view base) override;
44};
45
46// FIXME -- temp common base class to allow use of ReferenceMap or ResolutionContext
47// interchangeably when looking up declarations. This should go away once the refMap does
49 public:
50 virtual ~DeclarationLookup() = default;
51 virtual const IR::IDeclaration *getDeclaration(const IR::Path *,
52 bool notNull = false) const = 0;
53 virtual const IR::IDeclaration *getDeclaration(const IR::This *,
54 bool notNull = false) const = 0;
55};
56
58class ReferenceMap final : public ProgramMap, public NameGenerator, public DeclarationLookup {
61 bool isv1;
62
64 absl::flat_hash_map<const IR::Path *, const IR::IDeclaration *, Util::Hash> pathToDeclaration;
65
67 absl::flat_hash_set<const IR::IDeclaration *, Util::Hash> used;
68
70 absl::flat_hash_map<const IR::This *, const IR::IDeclaration *, Util::Hash> thisToDeclaration;
71
74 absl::flat_hash_map<cstring, int, Util::Hash> usedNames;
75
76 public:
77 ReferenceMap();
80 const IR::IDeclaration *getDeclaration(const IR::Path *path,
81 bool notNull = false) const override;
82
84 void setDeclaration(const IR::Path *path, const IR::IDeclaration *decl);
85
88 const IR::IDeclaration *getDeclaration(const IR::This *pointer,
89 bool notNull = false) const override;
90
92 void setDeclaration(const IR::This *pointer, const IR::IDeclaration *decl);
93
94 void dbprint(std::ostream &cout) const override;
95
97 void setIsV1(bool isv1) { this->isv1 = isv1; }
98 void setAnyOrder(bool anyOrder) { this->isv1 = anyOrder; }
99
101 cstring newName(std::string_view base) override;
102
104 void clear();
105
107 bool isV1() const { return isv1; }
108
110 bool isUsed(const IR::IDeclaration *decl) const { return used.count(decl) > 0; }
111
113 void usedName(cstring name) { usedNames.emplace(name, 0); }
114};
115
116} // namespace P4
117
118#endif /* FRONTENDS_COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_ */
Definition referenceMap.h:48
The Declaration interface, representing objects with names.
Definition declaration.h:17
Definition node.h:44
Definition visitor.h:409
cstring newName(std::string_view base) override
Generate a name from base that does not appear in usedNames.
Definition referenceMap.cpp:107
Definition referenceMap.h:20
const IR::IDeclaration * getDeclaration(const IR::Path *path, bool notNull=false) const override
Definition referenceMap.cpp:67
void setDeclaration(const IR::Path *path, const IR::IDeclaration *decl)
Sets declaration for path to decl.
Definition referenceMap.cpp:31
void clear()
Clear the reference map.
Definition referenceMap.cpp:20
void usedName(cstring name)
Indicate that name is used in the program.
Definition referenceMap.h:113
cstring newName(std::string_view base) override
Generate a name from base that fresh for the program.
Definition referenceMap.cpp:85
void setIsV1(bool isv1)
Set boolean indicating whether map is for a P4_14 program to isV1.
Definition referenceMap.h:97
bool isUsed(const IR::IDeclaration *decl) const
Definition referenceMap.h:110
bool isV1() const
Definition referenceMap.h:107
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13