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/resolveReferences.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
29 P4::TypeMap *typeMap = nullptr;
30
31 bool result = false;
32 bool preorder(const IR::AssignmentStatement *) override {
33 result = true;
34 return false;
35 }
36 bool preorder(const IR::MethodCallExpression *mc) override {
37 if (result) return false;
38
39 /* assume has side effects if we can't look it up */
40 if (typeMap) {
41 auto *mi = P4::MethodInstance::resolve(mc, this, typeMap, true);
42 if (auto *bm = mi->to<P4::BuiltInMethod>()) {
43 if (bm->name == "isValid") return true;
44 }
45 if (auto *em = mi->to<P4::ExternMethod>()) {
46 if (em->method->getAnnotation(IR::Annotation::noSideEffectsAnnotation)) return true;
47 }
48 }
49 result = true;
50 return false;
51 }
52
53 bool preorder(const IR::Primitive *) override {
54 result = true;
55 return false;
56 }
57 bool preorder(const IR::Expression *) override { return !result; }
58
59 public:
60 explicit hasSideEffects(const IR::Expression *e) { e->apply(*this); }
61 hasSideEffects(P4::TypeMap *tm, const IR::Expression *e, const Visitor::Context *ctxt)
62 : typeMap(tm) {
63 e->apply(*this, ctxt);
64 }
65 bool operator()(const IR::Expression *e) {
66 result = false;
67 e->apply(*this);
68 return result;
69 }
70 explicit operator bool() { return result; }
71};
72
73} // namespace P4
74
75#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
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
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
Definition visitor.h:47