P4C
The P4 Compiler
Loading...
Searching...
No Matches
parameterSubstitution.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_P4_PARAMETERSUBSTITUTION_H_
9#define FRONTENDS_P4_PARAMETERSUBSTITUTION_H_
10
11#include <iostream>
12
13#include "ir/ir.h"
14#include "lib/cstring.h"
15#include "lib/enumerator.h"
16
17namespace P4 {
18
19/* Maps Parameters to Arguments via their name. Note that
20 parameter identity is not important, but the parameter name is. */
21class ParameterSubstitution : public IHasDbPrint {
22 protected:
23 bool containsName(cstring name) const {
24 return parameterValues.find(name) != parameterValues.end();
25 }
26
27 public:
28 ParameterSubstitution() = default;
29 ParameterSubstitution(const ParameterSubstitution &other) = default;
30
31 void add(const IR::Parameter *parameter, const IR::Argument *value);
32 const IR::Argument *lookupByName(cstring name) const { return get(parameterValues, name); }
33
34 const IR::Argument *lookup(const IR::Parameter *param) const {
35 return lookupByName(param->name.name);
36 }
37
38 bool contains(const IR::Parameter *param) const {
39 if (!containsName(param->name.name)) return false;
40 return true;
41 }
42
43 const IR::Parameter *findParameter(const IR::Argument *argument) const {
44 for (auto p : *getParametersInOrder())
45 if (lookup(p) == argument) return p;
46 return nullptr;
47 }
48
49 bool empty() const { return parameterValues.empty(); }
50
53 // For actions only some parameters may be bound.
54 void populate(const IR::ParameterList *params, const IR::Vector<IR::Argument> *args);
55
58 return Util::enumerate(parameters);
59 }
60
64 if (paramList == nullptr) return new Util::EmptyEnumerator<const IR::Parameter *>;
65 return paramList->getEnumerator();
66 }
67
68 void dbprint(std::ostream &out) const override {
69 bool brief = (DBPrint::dbgetflags(out) & DBPrint::Brief);
70 if (paramList != nullptr) {
71 if (!brief) out << "paramList:" << Log::endl;
72 for (auto s : *paramList->getEnumerator()) {
73 out << dbp(s) << "=>" << dbp(lookup(s));
74 if (!brief) out << " " << lookup(s);
75 out << Log::endl;
76 }
77 } else {
78 if (!brief) out << "parametersByName:" << Log::endl;
79 for (auto s : parametersByName) {
80 out << dbp(s.second) << "=>" << dbp(lookupByName(s.first));
81 if (!brief) out << " " << lookupByName(s.first);
82 out << Log::endl;
83 }
84 }
85 }
86
87 private:
88 // Parameter names are unique for a procedure, so each name
89 // should show up only once.
90 absl::flat_hash_map<cstring, const IR::Argument *> parameterValues;
92 absl::flat_hash_map<cstring, const IR::Parameter *> parametersByName;
94 std::vector<const IR::Parameter *> parameters;
96 const IR::ParameterList *paramList = nullptr;
97};
98
99} // namespace P4
100
101#endif /* FRONTENDS_P4_PARAMETERSUBSTITUTION_H_ */
Definition stringify.h:24
Definition ir/vector.h:50
void populate(const IR::ParameterList *params, const IR::Vector< IR::Argument > *args)
Definition parameterSubstitution.cpp:23
Util::Enumerator< const IR::Parameter * > * getParametersInOrder() const
Definition parameterSubstitution.h:63
Util::Enumerator< const IR::Parameter * > * getParametersInArgumentOrder() const
Returns parameters in the order they were added.
Definition parameterSubstitution.h:57
Always empty iterator (equivalent to end())
Definition enumerator.h:290
Type-erased Enumerator interface.
Definition enumerator.h:60
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13