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 == IR::Type_Header::isValid) return true;
44 } else if (auto *em = mi->to<P4::ExternMethod>()) {
45 if (em->method->hasAnnotation(IR::Annotation::noSideEffectsAnnotation)) return true;
46 }
47 }
48 result = true;
49 return false;
50 }
51
52 bool preorder(const IR::Primitive *) override {
53 result = true;
54 return false;
55 }
56 bool preorder(const IR::Expression *) override { return !result; }
57
58 public:
59 explicit hasSideEffects(const IR::Expression *e) { e->apply(*this); }
60 hasSideEffects(P4::TypeMap *tm, const IR::Expression *e, const Visitor::Context *ctxt)
61 : typeMap(tm) {
62 e->apply(*this, ctxt);
63 }
64 bool operator()(const IR::Expression *e) {
65 result = false;
66 e->apply(*this);
67 return result;
68 }
69 explicit operator bool() { return result; }
70};
71
72} // namespace P4
73
74#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