P4C
The P4 Compiler
Loading...
Searching...
No Matches
inline_control_flow.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_14_INLINE_CONTROL_FLOW_H_
18#define FRONTENDS_P4_14_INLINE_CONTROL_FLOW_H_
19
20#include "frontends/p4/evaluator/evaluator.h"
21#include "ir/ir.h"
22
23namespace P4 {
24
26 public:
27 explicit InlineControlFlow(const IR::V1Program *gl) : global(gl) {
28 setName("InlineControlFlow");
29 }
30
31 private:
32 const IR::V1Program *global;
33
34 const IR::Node *preorder(IR::Apply *a) override {
35 if (global && !global->get<IR::V1Table>(a->name))
36 error("%s: No table named %s", a->srcInfo, a->name);
37 return a;
38 }
39 const IR::Node *preorder(IR::Primitive *p) override {
40 if (auto cf = global ? global->get<IR::V1Control>(p->name) : 0) {
41 const IR::V1Control *control;
42 if (auto act = findContext<IR::ActionFunction>())
43 error("%s: Trying to call control flow %s in action %s", p->srcInfo, p->name,
44 act->name);
45 else if (auto table = findContext<IR::V1Table>())
46 error("%s: Trying to call control flow %s in table %s", p->srcInfo, p->name,
47 table->name);
48 else if ((control = findContext<IR::V1Control>()) && control->name == p->name)
49 error("%s: Recursive call to control flow %s", p->srcInfo, p->name);
50 else
51 return cf->code;
52 }
53 return p;
54 }
55};
56
57} // namespace P4
58
59#endif /* FRONTENDS_P4_14_INLINE_CONTROL_FLOW_H_ */
Definition node.h:94
Definition inline_control_flow.h:25
Definition visitor.h:424
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51