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
57 Util::EnumeratorPtr<const IR::Parameter *> getParametersInArgumentOrder() const {
58 return Util::enumerate(parameters);
59 }
60
63 Util::EnumeratorPtr<const IR::Parameter *> getParametersInOrder() const {
64 if (paramList == nullptr)
65 return std::make_shared<Util::EmptyEnumerator<const IR::Parameter *>>();
66 return paramList->getEnumerator();
67 }
68
69 void dbprint(std::ostream &out) const override {
70 bool brief = (DBPrint::dbgetflags(out) & DBPrint::Brief);
71 if (paramList != nullptr) {
72 if (!brief) out << "paramList:" << Log::endl;
73 for (auto s : paramList->getEnumerator()) {
74 out << dbp(s) << "=>" << dbp(lookup(s));
75 if (!brief) out << " " << lookup(s);
76 out << Log::endl;
77 }
78 } else {
79 if (!brief) out << "parametersByName:" << Log::endl;
80 for (auto s : parametersByName) {
81 out << dbp(s.second) << "=>" << dbp(lookupByName(s.first));
82 if (!brief) out << " " << lookupByName(s.first);
83 out << Log::endl;
84 }
85 }
86 }
87
88 private:
89 // Parameter names are unique for a procedure, so each name
90 // should show up only once.
91 absl::flat_hash_map<cstring, const IR::Argument *> parameterValues;
93 absl::flat_hash_map<cstring, const IR::Parameter *> parametersByName;
95 std::vector<const IR::Parameter *> parameters;
97 const IR::ParameterList *paramList = nullptr;
98};
99
100} // namespace P4
101
102#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::EnumeratorPtr< const IR::Parameter * > getParametersInArgumentOrder() const
Returns parameters in the order they were added.
Definition parameterSubstitution.h:57
Util::EnumeratorPtr< const IR::Parameter * > getParametersInOrder() const
Definition parameterSubstitution.h:63
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13