P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
removeParameters.h
1/*
2Copyright 2016 VMware, 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_REMOVEPARAMETERS_H_
18#define FRONTENDS_P4_REMOVEPARAMETERS_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "frontends/p4/typeMap.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
33 std::map<const IR::P4Action *, const IR::MethodCallExpression *> invocations;
34 std::set<const IR::P4Action *> all; // for these actions remove all parameters
35 std::set<const IR::MethodCallExpression *> calls;
37 std::map<const IR::MethodCallExpression *, unsigned> defaultActions;
38
39 public:
40 void bind(const IR::P4Action *action, const IR::MethodCallExpression *invocation,
41 bool allParams) {
42 CHECK_NULL(action);
43 BUG_CHECK(invocations.find(action) == invocations.end(), "%1%: action called twice",
44 action);
45 invocations.emplace(action, invocation);
46 if (allParams) all.emplace(action);
47 calls.emplace(invocation);
48 }
49 void bindDefaultAction(const IR::P4Action *action,
50 const IR::MethodCallExpression *defaultInvocation) {
51 // We must have a binding for this action already.
52 auto actionCallInvocation = ::get(invocations, action);
53 CHECK_NULL(actionCallInvocation);
54 // We must remove all arguments which are bound in the action list.
55 unsigned boundArgs = actionCallInvocation->arguments->size();
56 defaultActions.emplace(defaultInvocation, boundArgs);
57 }
58 const IR::MethodCallExpression *get(const IR::P4Action *action) const {
59 return ::get(invocations, action);
60 }
61 bool removeAllParameters(const IR::P4Action *action) const {
62 return all.find(action) != all.end();
63 }
64 bool isCall(const IR::MethodCallExpression *expression) const {
65 return calls.find(expression) != calls.end();
66 }
67 unsigned argsToRemove(const IR::MethodCallExpression *defaultCall) const {
68 if (defaultActions.find(defaultCall) == defaultActions.end()) return 0;
69 return ::get(defaultActions, defaultCall);
70 }
71};
72
73class FindActionParameters : public Inspector {
74 ReferenceMap *refMap;
75 TypeMap *typeMap;
76 ActionInvocation *invocations;
77
78 public:
79 FindActionParameters(ReferenceMap *refMap, TypeMap *typeMap, ActionInvocation *invocations)
80 : refMap(refMap), typeMap(typeMap), invocations(invocations) {
81 CHECK_NULL(refMap);
82 CHECK_NULL(invocations);
83 CHECK_NULL(typeMap);
84 setName("FindActionParameters");
85 }
86
87 void postorder(const IR::ActionListElement *element) override;
88 void postorder(const IR::MethodCallExpression *expression) override;
89};
90
117class DoRemoveActionParameters : public Transform {
118 ActionInvocation *invocations;
119
120 public:
121 explicit DoRemoveActionParameters(ActionInvocation *invocations) : invocations(invocations) {
122 CHECK_NULL(invocations);
123 setName("DoRemoveActionParameters");
124 }
125
126 const IR::Node *postorder(IR::P4Action *table) override;
127 const IR::Node *postorder(IR::ActionListElement *element) override;
128 const IR::Node *postorder(IR::MethodCallExpression *expression) override;
129 ActionInvocation *getInvocations() { return invocations; }
130};
131
132class RemoveActionParameters : public PassManager {
133 public:
135 TypeChecking *typeChecking = nullptr);
136};
137
138} // namespace P4
139
140#endif /* FRONTENDS_P4_REMOVEPARAMETERS_H_ */
Definition removeParameters.h:32
Definition removeParameters.h:117
Definition removeParameters.h:73
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition removeParameters.h:132
Definition typeChecker.h:60
Definition typeMap.h:42
Definition applyOptionsPragmas.cpp:24