17#ifndef P4_UNUSEDDECLARATIONS_H_
18#define P4_UNUSEDDECLARATIONS_H_
20#include "../common/resolveReferences/resolveReferences.h"
22#include "ir/pass_manager.h"
23#include "lib/stringify.h"
27class RemoveUnusedDeclarations;
31 absl::flat_hash_set<const IR::IDeclaration *, Util::Hash> usedDecls;
34 bool setUsed(
const IR::IDeclaration *decl) {
return usedDecls.emplace(decl).second; }
36 [[nodiscard]]
auto begin()
const {
return usedDecls.begin(); }
38 [[nodiscard]]
auto end()
const {
return usedDecls.end(); }
40 void clear() { usedDecls.clear(); }
42 void dbprint(std::ostream &cout)
const override;
53 bool warn =
false)
const;
66 auto rv = Inspector::init_apply(node);
72 bool preorder(
const IR::KeyElement *ke)
override;
73 bool preorder(
const IR::PathExpression *path)
override;
74 bool preorder(
const IR::Type_Name *type)
override;
127 : used(used),
warned(warn ? new
std::set<const
IR::Node *> : nullptr) {
128 setName(
"RemoveUnusedDeclarations");
132 using Transform::init_apply;
133 using Transform::postorder;
134 using Transform::preorder;
138 const IR::Node *preorder(IR::P4Control *cont)
override;
139 const IR::Node *preorder(IR::P4Parser *cont)
override;
140 const IR::Node *preorder(IR::P4Table *cont)
override;
141 const IR::Node *preorder(IR::ParserState *state)
override;
142 const IR::Node *preorder(IR::Type_Enum *type)
override;
143 const IR::Node *preorder(IR::Type_SerEnum *type)
override;
145 const IR::Node *preorder(IR::Declaration_Instance *decl)
override;
146 const IR::Node *preorder(IR::Method *decl)
override;
149 const IR::Node *preorder(IR::Type_Error *type)
override {
153 const IR::Node *preorder(IR::Declaration_MatchKind *decl)
override {
157 const IR::Node *preorder(IR::Type_StructLike *type)
override {
158 visit(type->typeParameters);
162 const IR::Node *preorder(IR::Type_Extern *type)
override {
163 visit(type->typeParameters);
167 const IR::Node *preorder(IR::Type_Method *type)
override {
168 visit(type->typeParameters);
172 const IR::Node *preorder(IR::Parameter *param)
override;
173 const IR::Node *preorder(IR::NamedExpression *ne)
override {
return ne; }
174 const IR::Node *preorder(IR::Type_Var *p)
override {
176 return warnIfUnused(p);
179 const IR::Node *preorder(IR::Declaration_Variable *decl)
override;
180 const IR::Node *preorder(IR::Declaration *decl)
override {
return process(decl); }
181 const IR::Node *preorder(IR::Type_Declaration *decl)
override {
return process(decl); }
196 setName(
"RemoveAllUnusedDeclarations");
197 setStopOnError(
true);
Collects all used declarations into @used set.
Definition unusedDeclarations.h:57
Definition stringify.h:33
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition ir/pass_manager.h:40
Definition ir/pass_manager.h:145
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:189
Removes unused declarations.
Definition unusedDeclarations.h:99
std::set< const IR::Node * > * warned
Definition unusedDeclarations.h:111
bool giveWarning(const IR::Node *node)
Definition unusedDeclarations.cpp:63
Definition unusedDeclarations.h:48
virtual RemoveUnusedDeclarations * getRemoveUnusedDeclarationsPass(const UsedDeclSet &used, bool warn=false) const
Definition unusedDeclarations.cpp:23
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition unusedDeclarations.h:29
bool isUsed(const IR::IDeclaration *decl) const
Definition unusedDeclarations.h:45
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24