P4C
The P4 Compiler
Loading...
Searching...
No Matches
has_side_effects.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_HAS_SIDE_EFFECTS_H_
18#define MIDEND_HAS_SIDE_EFFECTS_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/methodInstance.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
26/* Should this be a method on IR::Expression? Maybe after the refMap/typeMap go away */
27
28class hasSideEffects : public Inspector {
29 P4::ReferenceMap *refMap = nullptr;
30 P4::TypeMap *typeMap = nullptr;
31
32 bool result = false;
33 bool preorder(const IR::AssignmentStatement *) override {
34 result = true;
35 return false;
36 }
37 bool preorder(const IR::MethodCallExpression *mc) override {
38 if (result) {
39 return false;
40 }
41 /* assume has side effects if we can't look it up */
42 if (refMap && typeMap) {
43 auto *mi = P4::MethodInstance::resolve(mc, refMap, typeMap, true);
44 if (auto *bm = mi->to<P4::BuiltInMethod>()) {
45 if (bm->name == "isValid") {
46 return true;
47 }
48 }
49 if (auto *em = mi->to<P4::ExternMethod>()) {
50 if (em->method->getAnnotation(IR::Annotation::noSideEffectsAnnotation)) return true;
51 }
52 }
53 result = true;
54 return false;
55 }
56
57 bool preorder(const IR::Primitive *) override {
58 result = true;
59 return false;
60 }
61 bool preorder(const IR::Expression *) override { return !result; }
62
63 public:
64 explicit hasSideEffects(const IR::Expression *e) { e->apply(*this); }
65 hasSideEffects(P4::ReferenceMap *rm, P4::TypeMap *tm) : refMap(rm), typeMap(tm) {}
66 hasSideEffects(P4::ReferenceMap *rm, P4::TypeMap *tm, const IR::Expression *e)
67 : refMap(rm), typeMap(tm) {
68 e->apply(*this);
69 }
70 bool operator()(const IR::Expression *e) {
71 result = false;
72 e->apply(*this);
73 return result;
74 }
75 explicit operator bool() { return result; }
76};
77
78} // namespace P4
79
80#endif /* MIDEND_HAS_SIDE_EFFECTS_H_ */
Definition methodInstance.h:260
Definition methodInstance.h:168
Definition visitor.h:400
static MethodInstance * resolve(const IR::MethodCallExpression *mce, const DeclarationLookup *refMap, TypeMap *typeMap, bool useExpressionType=false, const Visitor::Context *ctxt=nullptr, bool incomplete=false)
Definition methodInstance.cpp:27
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition has_side_effects.h:28
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24