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