P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
removeReturns.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_REMOVERETURNS_H_
18#define FRONTENDS_P4_REMOVERETURNS_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/ternaryBool.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
32class HasExits : public Inspector {
33 public:
34 bool hasExits;
35 bool hasReturns;
36 HasExits() : hasExits(false), hasReturns(false) { setName("HasExits"); }
37
38 void postorder(const IR::ExitStatement *) override { hasExits = true; }
39 void postorder(const IR::ReturnStatement *) override { hasReturns = true; }
40};
41
59class DoRemoveReturns : public Transform {
60 protected:
61 P4::ReferenceMap *refMap;
62 IR::ID returnVar; // one for each context
63 IR::ID returnedValue; // only for functions that return expressions
64 cstring variableName;
65 cstring retValName;
66
67 std::vector<TernaryBool> stack;
68 void push() { stack.push_back(TernaryBool::No); }
69 void pop() { stack.pop_back(); }
70 void set(TernaryBool r) {
71 BUG_CHECK(!stack.empty(), "Empty stack");
72 stack.back() = r;
73 }
74 TernaryBool hasReturned() {
75 BUG_CHECK(!stack.empty(), "Empty stack");
76 return stack.back();
77 }
78
79 public:
80 explicit DoRemoveReturns(P4::ReferenceMap *refMap, cstring varName = "hasReturned",
81 cstring retValName = "retval")
82 : refMap(refMap), variableName(varName), retValName(retValName) {
83 visitDagOnce = false;
84 CHECK_NULL(refMap);
85 setName("DoRemoveReturns");
86 }
87
88 const IR::Node *preorder(IR::Function *function) override;
89 const IR::Node *preorder(IR::BlockStatement *statement) override;
90 const IR::Node *preorder(IR::ReturnStatement *statement) override;
91 const IR::Node *preorder(IR::ExitStatement *statement) override;
92 const IR::Node *preorder(IR::IfStatement *statement) override;
93 const IR::Node *preorder(IR::SwitchStatement *statement) override;
94
95 const IR::Node *preorder(IR::P4Action *action) override;
96 const IR::Node *preorder(IR::P4Control *control) override;
97 const IR::Node *preorder(IR::P4Parser *parser) override {
98 prune();
99 return parser;
100 }
101};
102
103class RemoveReturns : public PassManager {
104 public:
105 explicit RemoveReturns(ReferenceMap *refMap) {
106 passes.push_back(new ResolveReferences(refMap));
107 passes.push_back(new DoRemoveReturns(refMap));
108 setName("RemoveReturns");
109 }
110};
111
112} // namespace P4
113
114#endif /* FRONTENDS_P4_REMOVERETURNS_H_ */
Definition removeReturns.h:59
Definition removeReturns.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition removeReturns.h:103
Definition resolveReferences.h:119
Definition cstring.h:72
Definition applyOptionsPragmas.cpp:24