8#ifndef FRONTENDS_COMMON_RESOLVEREFERENCES_RESOLVEREFERENCES_H_
9#define FRONTENDS_COMMON_RESOLVEREFERENCES_RESOLVEREFERENCES_H_
11#include "absl/container/flat_hash_map.h"
12#include "absl/container/inlined_vector.h"
13#include "frontends/common/parser_options.h"
15#include "ir/pass_manager.h"
16#include "lib/cstring.h"
17#include "lib/iterator_range.h"
18#include "referenceMap.h"
30 const std::vector<const IR::IDeclaration *> &memoizeDeclarations(
31 const IR::INamespace *ns)
const;
33 using DeclsVector = absl::InlinedVector<const IR::IDeclaration *, 2>;
34 using NamespaceDeclsByName = absl::flat_hash_map<cstring, DeclsVector, Util::Hash>;
38 NamespaceDeclsByName &memoizeDeclsByName(
const IR::INamespace *ns)
const;
40 mutable absl::flat_hash_map<const IR::INamespace *, std::vector<const IR::IDeclaration *>,
43 mutable absl::flat_hash_map<const IR::INamespace *, NamespaceDeclsByName, Util::Hash>
49 std::vector<const IR::IDeclaration *> lookup(
const IR::INamespace *ns,
const IR::ID &name,
55 std::vector<const IR::IDeclaration *> lookupMatchKind(
const IR::ID &name)
const;
61 bool anyOrder =
false;
64 explicit ResolutionContext(
bool ao) : anyOrder(ao) {}
75 const IR::INamespace * =
nullptr)
const;
82 const IR::Type *
resolveType(
const IR::Type *type)
const;
85 bool notNull =
false)
const override;
86 const IR::IDeclaration *getDeclaration(
const IR::This *,
bool notNull =
false)
const override;
90 auto nsIt = namespaceDecls.find(ns);
91 const auto &decls = nsIt != namespaceDecls.end() ? nsIt->second : memoizeDeclarations(ns);
97 auto nsIt = namespaceDeclNames.find(ns);
98 const auto &namesToDecls =
99 nsIt != namespaceDeclNames.end() ? nsIt->second : memoizeDeclsByName(ns);
101 auto decls = namesToDecls.find(name);
102 if (decls == namesToDecls.end())
103 return Util::Enumerator<const IR::IDeclaration *>::emptyEnumerator();
104 return Util::enumerate(decls->second);
115class ResolveReferences :
public Inspector,
private ResolutionContext {
125 const IR::IDeclaration *resolvePath(
const IR::Path *path,
bool isType)
const override;
128 explicit ResolveReferences(
P4::ReferenceMap *refMap,
bool checkShadow =
false);
131 void end_apply(
const IR::Node *node)
override;
133 bool preorder(
const IR::Type_Name *type)
override;
134 bool preorder(
const IR::PathExpression *path)
override;
135 bool preorder(
const IR::KeyElement *path)
override;
136 bool preorder(
const IR::This *pointer)
override;
137 bool preorder(
const IR::Declaration_Instance *decl)
override;
139 bool preorder(
const IR::P4Program *t)
override;
140 void postorder(
const IR::P4Program *t)
override;
141 bool preorder(
const IR::P4Control *t)
override;
142 bool preorder(
const IR::P4Parser *t)
override;
143 bool preorder(
const IR::P4Action *t)
override;
144 bool preorder(
const IR::Function *t)
override;
145 bool preorder(
const IR::TableProperties *t)
override;
146 bool preorder(
const IR::Type_Method *t)
override;
147 bool preorder(
const IR::ParserState *t)
override;
148 bool preorder(
const IR::Type_Extern *t)
override;
149 bool preorder(
const IR::Type_ArchBlock *t)
override;
150 void postorder(
const IR::Type_ArchBlock *t)
override;
151 bool preorder(
const IR::Type_StructLike *t)
override;
152 bool preorder(
const IR::BlockStatement *t)
override;
154 bool preorder(
const IR::P4Table *table)
override;
155 bool preorder(
const IR::Declaration *d)
override {
156 refMap->usedName(d->getName().name);
159 bool preorder(
const IR::Type_Declaration *d)
override {
160 refMap->usedName(d->getName().name);
164 void checkShadowing(
const IR::INamespace *ns)
const;
167class CheckShadowing :
public PassManager {
175 setName(
"CheckShadowing");
Definition referenceMap.h:48
The Declaration interface, representing objects with names.
Definition declaration.h:17
Definition ir/vector.h:50
static P4CContext & get()
Definition parser_options.cpp:523
Class used to encode maps from paths to declarations.
Definition referenceMap.h:58
void setIsV1(bool isv1)
Set boolean indicating whether map is for a P4_14 program to isV1.
Definition referenceMap.h:97
std::vector< const IR::IDeclaration * > resolve(const IR::ID &name, ResolutionType type) const
Resolve references for name, restricted to type declarations.
Definition resolveReferences.cpp:37
auto getDeclsByName(const IR::INamespace *ns, cstring name) const
Returns the set of decls with the given name that exist in the given namespace.
Definition resolveReferences.h:96
auto getDeclarations(const IR::INamespace *ns) const
Returns the set of decls that exist in the given namespace.
Definition resolveReferences.h:89
virtual const IR::IDeclaration * resolvePath(const IR::Path *path, bool isType) const
Definition resolveReferences.cpp:301
const IR::Vector< IR::Argument > * methodArguments(cstring name) const
We are resolving a method call. Find the arguments from the context.
Definition resolveReferences.cpp:173
const IR::Type * resolveType(const IR::Type *type) const
Resolve a refrence to a type type.
Definition resolveReferences.cpp:288
const IR::IDeclaration * resolveUnique(const IR::ID &name, ResolutionType type, const IR::INamespace *=nullptr) const
Resolve reference for name, restricted to type declarations, and expect one result.
Definition resolveReferences.cpp:213
Definition resolveReferences.h:115
Definition iterator_range.h:44
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
ResolutionType
Helper class to indicate types of nodes that may be returned during resolution.
Definition resolveReferences.h:23