P4C
The P4 Compiler
Loading...
Searching...
No Matches
expr_uses.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 MIDEND_EXPR_USES_H_
18#define MIDEND_EXPR_USES_H_
19
20#include "ir/ir.h"
21
22namespace P4 {
23
24/* Should this be a method on IR::Expression? */
25
29class exprUses : public Inspector {
30 cstring look_for;
31 const char *search_tail = nullptr; // pointer into look_for for partial match
32 bool result = false;
33 bool preorder(const IR::Path *p) override {
34 if (look_for.startsWith(p->name.name)) {
35 search_tail = look_for.c_str() + p->name.name.size();
36 if (*search_tail == 0 || *search_tail == '.' || *search_tail == '[') result = true;
37 }
38 return !result;
39 }
40 bool preorder(const IR::Primitive *p) override {
41 if (p->name == look_for) result = true;
42 return !result;
43 }
44 bool preorder(const IR::Expression *) override { return !result; }
45
46 void postorder(const IR::Member *m) override {
47 if (result && search_tail && *search_tail) {
48 if (*search_tail == '.') search_tail++;
49 if (cstring(search_tail).startsWith(m->member.name)) {
50 search_tail += m->member.name.size();
51 if (*search_tail == 0 || *search_tail == '.' || *search_tail == '[') return;
52 }
53 search_tail = nullptr;
54 if (!m->expr->type->is<IR::Type_HeaderUnion>()) {
55 result = false;
56 }
57 }
58 }
59 void postorder(const IR::ArrayIndex *m) override {
60 if (result && search_tail && *search_tail) {
61 if (*search_tail == '.' || *search_tail == '[') search_tail++;
62 if (isdigit(*search_tail)) {
63 int idx = strtol(search_tail, const_cast<char **>(&search_tail), 10);
64 if (*search_tail == ']') search_tail++;
65 if (auto k = m->right->to<IR::Constant>()) {
66 if (k->asInt() == idx) return;
67 } else {
68 return;
69 }
70 }
71 result = false;
72 search_tail = nullptr;
73 }
74 }
75 void postorder(const IR::PathExpression *) override {}
76 void postorder(const IR::Expression *) override { search_tail = nullptr; }
77
78 public:
79 exprUses(const IR::Expression *e, cstring n) : look_for(n) {
80 visitDagOnce = false;
81 e->apply(*this);
82 }
83 explicit operator bool() const { return result; }
84};
85
86} // namespace P4
87
88#endif /* MIDEND_EXPR_USES_H_ */
Definition visitor.h:400
Definition cstring.h:85
Definition expr_uses.h:29
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24