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 if (paramList != nullptr) {
79 for (auto s : *paramList->getEnumerator())
80 out << dbp(s) << "=>" << dbp(lookup(s)) << std::endl;
81 } else {
82 for (auto s : parametersByName)
83 out << dbp(s.second) << "=>" << dbp(lookupByName(s.first)) << std::endl;
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:33
Definition vector.h:58
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