P4C
The P4 Compiler
Loading...
Searching...
No Matches
parameterSubstitution.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef FRONTENDS_P4_PARAMETERSUBSTITUTION_H_
18#define FRONTENDS_P4_PARAMETERSUBSTITUTION_H_
19
20#include <iostream>
21
22#include "ir/ir.h"
23#include "lib/cstring.h"
24#include "lib/enumerator.h"
25
26namespace P4 {
27
28/* Maps Parameters to Arguments via their name. Note that
29 parameter identity is not important, but the parameter name is. */
31 protected:
32 bool containsName(cstring name) const {
33 return parameterValues.find(name) != parameterValues.end();
34 }
35
36 public:
37 ParameterSubstitution() = default;
38 ParameterSubstitution(const ParameterSubstitution &other) = default;
39
40 void add(const IR::Parameter *parameter, const IR::Argument *value);
41 const IR::Argument *lookupByName(cstring name) const { return get(parameterValues, name); }
42
43 const IR::Argument *lookup(const IR::Parameter *param) const {
44 return lookupByName(param->name.name);
45 }
46
47 bool contains(const IR::Parameter *param) const {
48 if (!containsName(param->name.name)) return false;
49 return true;
50 }
51
52 const IR::Parameter *findParameter(const IR::Argument *argument) const {
53 for (auto p : *getParametersInOrder())
54 if (lookup(p) == argument) return p;
55 return nullptr;
56 }
57
58 bool empty() const { return parameterValues.empty(); }
59
62 // For actions only some parameters may be bound.
63 void populate(const IR::ParameterList *params, const IR::Vector<IR::Argument> *args);
64
67 return Util::enumerate(parameters);
68 }
69
73 if (paramList == nullptr) return new Util::EmptyEnumerator<const IR::Parameter *>;
74 return paramList->getEnumerator();
75 }
76
77 void dbprint(std::ostream &out) const {
78 bool brief = (DBPrint::dbgetflags(out) & DBPrint::Brief);
79 if (paramList != nullptr) {
80 if (!brief) out << "paramList:" << Log::endl;
81 for (auto s : *paramList->getEnumerator()) {
82 out << dbp(s) << "=>" << dbp(lookup(s));
83 if (!brief) out << " " << lookup(s);
84 out << Log::endl;
85 }
86 } else {
87 if (!brief) out << "parametersByName:" << Log::endl;
88 for (auto s : parametersByName) {
89 out << dbp(s.second) << "=>" << dbp(lookupByName(s.first));
90 if (!brief) out << " " << lookupByName(s.first);
91 out << Log::endl;
92 }
93 }
94 }
95
96 private:
97 // Parameter names are unique for a procedure, so each name
98 // should show up only once.
99 absl::flat_hash_map<cstring, const IR::Argument *> parameterValues;
101 absl::flat_hash_map<cstring, const IR::Parameter *> parametersByName;
103 std::vector<const IR::Parameter *> parameters;
105 const IR::ParameterList *paramList = nullptr;
106};
107
108} // namespace P4
109
110#endif /* FRONTENDS_P4_PARAMETERSUBSTITUTION_H_ */
Definition stringify.h:33
Definition vector.h:59
Definition parameterSubstitution.h:30
void populate(const IR::ParameterList *params, const IR::Vector< IR::Argument > *args)
Definition parameterSubstitution.cpp:34
Util::Enumerator< const IR::Parameter * > * getParametersInOrder() const
Definition parameterSubstitution.h:72
Util::Enumerator< const IR::Parameter * > * getParametersInArgumentOrder() const
Returns parameters in the order they were added.
Definition parameterSubstitution.h:66
Always empty iterator (equivalent to end())
Definition enumerator.h:295
Type-erased Enumerator interface.
Definition enumerator.h:68
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24