Skip to content

Commit

Permalink
Use Util::Hash for "internal maps". Some cleanup while there.
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl committed Jul 9, 2024
1 parent d3cd689 commit 0c85764
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
4 changes: 3 additions & 1 deletion frontends/p4/checkNamedArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ limitations under the License.

#include "checkNamedArgs.h"

#include "lib/hash.h"

namespace P4 {

bool CheckNamedArgs::checkArguments(const IR::Vector<IR::Argument> *arguments) {
bool first = true;
bool hasName = false;
absl::flat_hash_map<cstring, const IR::Argument *> found;
absl::flat_hash_map<cstring, const IR::Argument *, Util::Hash> found;

for (auto arg : *arguments) {
cstring argName = arg->name.name;
Expand Down
5 changes: 3 additions & 2 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ limitations under the License.
#include "frontends/p4/toP4/toP4.h"
#include "lib/algorithm.h"
#include "lib/cstring.h"
#include "lib/hash.h"
#include "lib/log.h"
#include "syntacticEquivalence.h"
#include "typeConstraints.h"
Expand Down Expand Up @@ -3487,7 +3488,7 @@ const IR::Expression *TypeInference::actionCall(bool inActionList,
auto params = new IR::ParameterList;

// keep track of parameters that have not been matched yet
absl::flat_hash_map<cstring, const IR::Parameter *> left;
absl::flat_hash_map<cstring, const IR::Parameter *, Util::Hash> left;
for (auto p : baseType->parameters->parameters) left.emplace(p->name, p);

auto paramIt = baseType->parameters->parameters.begin();
Expand Down Expand Up @@ -4074,7 +4075,7 @@ const IR::Node *TypeInference::postorder(IR::SwitchStatement *stat) {

if (auto ae = type->to<IR::Type_ActionEnum>()) {
// switch (table.apply(...))
absl::flat_hash_map<cstring, const IR::Node *> foundLabels;
absl::flat_hash_map<cstring, const IR::Node *, Util::Hash> foundLabels;
const IR::Node *foundDefault = nullptr;
for (auto c : stat->cases) {
if (c->label->is<IR::DefaultExpression>()) {
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/typeChecking/typeUnification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool TypeUnification::unifyCall(const BinaryConstraint *constraint) {

auto paramIt = dest->parameters->begin();
// keep track of parameters that have not been matched yet
absl::flat_hash_map<cstring, const IR::Parameter *> left;
absl::flat_hash_map<cstring, const IR::Parameter *, Util::Hash> left;
for (auto p : dest->parameters->parameters) left.emplace(p->name, p);

for (auto arg : *src->arguments) {
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/validateParsedProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void ValidateParsedProgram::postorder(const IR::P4Table *t) {
void ValidateParsedProgram::distinctParameters(const IR::TypeParameters *typeParams,
const IR::ParameterList *apply,
const IR::ParameterList *constr) {
absl::flat_hash_map<cstring, const IR::Node *> found;
absl::flat_hash_map<cstring, const IR::Node *, Util::Hash> found;

for (auto p : typeParams->parameters) found.emplace(p->getName(), p);
for (auto p : apply->parameters) {
Expand Down
9 changes: 9 additions & 0 deletions ir/id.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.

#include "lib/cstring.h"
#include "lib/exceptions.h"
#include "lib/hash.h"
#include "lib/source_file.h"

namespace IR {
Expand Down Expand Up @@ -61,4 +62,12 @@ struct ID : Util::IHasSourceInfo {
};

} // namespace IR

namespace Util {
template <>
struct Hasher<IR::ID> {
size_t operator()(const IR::ID &id) const { return Util::Hash{}(id.name); }
};
} // namespace Util

#endif /* IR_ID_H_ */
11 changes: 6 additions & 5 deletions ir/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ limitations under the License.
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "ir/declaration.h"
#include "ir/id.h"
#include "ir/indexed_vector.h"
Expand Down Expand Up @@ -87,7 +89,7 @@ Util::Enumerator<const IDeclaration *> *INestedNamespace::getDeclarations() cons

bool IFunctional::callMatches(const Vector<Argument> *arguments) const {
auto paramList = getParameters()->parameters;
absl::flat_hash_map<cstring, const IR::Parameter *> paramNames;
absl::flat_hash_map<cstring, const IR::Parameter *, Util::Hash> paramNames;
for (auto param : paramList) paramNames.emplace(param->name.name, param);

size_t index = 0;
Expand All @@ -113,14 +115,13 @@ bool IFunctional::callMatches(const Vector<Argument> *arguments) const {
}

void IGeneralNamespace::checkDuplicateDeclarations() const {
absl::flat_hash_map<cstring, ID> seen;
absl::flat_hash_set<ID, Util::Hash> seen;
for (const auto *decl : *getDeclarations()) {
IR::ID name = decl->getName();
auto [it, inserted] = seen.emplace(name.name, name);
auto [it, inserted] = seen.emplace(name);
if (!inserted) {
::error(ErrorType::ERR_DUPLICATE,
"Duplicate declaration of %1%: previous declaration at %2%", name,
it->second.srcInfo);
"Duplicate declaration of %1%: previous declaration at %2%", name, it->srcInfo);
}
}
}
Expand Down

0 comments on commit 0c85764

Please sign in to comment.