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#ifdef SUPPORT_P4_14
32 bool preorder(const IR::Primitive *p) override {
33 if (p->name == look_for) result = true;
34 return !result;
35 }
36#endif
37 bool preorder(const IR::Expression *) override { return !result; }
38
39 void postorder(const IR::Member *m) override {
40 if (result && search_tail && *search_tail) {
41 if (*search_tail == '.') search_tail++;
42 if (cstring(search_tail).startsWith(m->member.name)) {
43 search_tail += m->member.name.size();
44 if (*search_tail == 0 || *search_tail == '.' || *search_tail == '[') return;
45 }
46 search_tail = nullptr;
47 if (!m->expr->type->is<IR::Type_HeaderUnion>()) {
48 result = false;
49 }
50 }
51 }
52 void postorder(const IR::ArrayIndex *m) override {
53 if (result && search_tail && *search_tail) {
54 if (*search_tail == '.' || *search_tail == '[') search_tail++;
55 if (isdigit(*search_tail)) {
56 int idx = strtol(search_tail, const_cast<char **>(&search_tail), 10);
57 if (*search_tail == ']') search_tail++;
58 if (auto k = m->right->to<IR::Constant>()) {
59 if (k->asInt() == idx) return;
60 } else {
61 return;
62 }
63 }
64 result = false;
65 search_tail = nullptr;
66 }
67 }
68 void postorder(const IR::PathExpression *) override {}
69 void postorder(const IR::Expression *) override { search_tail = nullptr; }
70
71 public:
72 exprUses(const IR::Expression *e, cstring n) : look_for(n) {
73 visitDagOnce = false;
74 e->apply(*this);
75 }
76 explicit operator bool() const { return result; }
77};
78
79} // namespace P4
80
81#endif /* MIDEND_EXPR_USES_H_ */
Definition visitor.h:409
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13