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#include "lib/exceptions.h"
26
27namespace P4 {
28
29/* Maps Parameters to Arguments via their name. Note that
30 parameter identity is not important, but the parameter name is. */
32 protected:
33 // Parameter names are unique for a procedure, so each name
34 // should show up only once.
35 std::map<cstring, const IR::Argument *> parameterValues;
37 std::map<cstring, const IR::Parameter *> parametersByName;
39 std::vector<const IR::Parameter *> parameters;
41 const IR::ParameterList *paramList = nullptr;
42
43 bool containsName(cstring name) const {
44 return parameterValues.find(name) != parameterValues.end();
45 }
46
47 public:
48 ParameterSubstitution() = default;
49 ParameterSubstitution(const ParameterSubstitution &other) = default;
50
51 void add(const IR::Parameter *parameter, const IR::Argument *value);
52 const IR::Argument *lookupByName(cstring name) const { return get(parameterValues, name); }
53
54 const IR::Argument *lookup(const IR::Parameter *param) const {
55 return lookupByName(param->name.name);
56 }
57
58 bool contains(const IR::Parameter *param) const {
59 if (!containsName(param->name.name)) return false;
60 return true;
61 }
62
63 const IR::Parameter *findParameter(const IR::Argument *argument) const {
64 for (auto p : *getParametersInOrder())
65 if (lookup(p) == argument) return p;
66 return nullptr;
67 }
68
69 bool empty() const { return parameterValues.empty(); }
70
73 // For actions only some parameters may be bound.
74 void populate(const IR::ParameterList *params, const IR::Vector<IR::Argument> *args);
75
80
87
88 void dbprint(std::ostream &out) const {
89 if (paramList != nullptr) {
90 for (auto s : *paramList->getEnumerator())
91 out << dbp(s) << "=>" << dbp(lookup(s)) << std::endl;
92 } else {
93 for (auto s : parametersByName)
94 out << dbp(s.second) << "=>" << dbp(lookupByName(s.first)) << std::endl;
95 }
96 }
97};
98
99} // namespace P4
100
101#endif /* FRONTENDS_P4_PARAMETERSUBSTITUTION_H_ */
Definition source_file.h:38
Definition externInstance.h:33
Definition parameterSubstitution.h:31
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:83
const IR::ParameterList * paramList
If created using populate this is non-null.
Definition parameterSubstitution.h:41
std::map< cstring, const IR::Parameter * > parametersByName
Map from parameter name to parameter.
Definition parameterSubstitution.h:37
Util::Enumerator< const IR::Parameter * > * getParametersInArgumentOrder() const
Returns parameters in the order they were added.
Definition parameterSubstitution.h:77
std::vector< const IR::Parameter * > parameters
Parameters in the order they were added.
Definition parameterSubstitution.h:39
Always empty iterator (equivalent to end())
Definition enumerator.h:261
Type-erased Enumerator interface.
Definition enumerator.h:68
Definition cstring.h:72
Definition applyOptionsPragmas.cpp:24
STL namespace.