P4C
The P4 Compiler
Loading...
Searching...
No Matches
programMap.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_COMMON_PROGRAMMAP_H_
9#define FRONTENDS_COMMON_PROGRAMMAP_H_
10
11#include "ir/ir.h"
12#include "lib/log.h"
13
14namespace P4 {
15
16// Base class for various maps.
17// A map is computed on a certain P4Program.
18// If the program has not changed, the map is up-to-date.
19class ProgramMap : public IHasDbPrint {
20 protected:
21 const IR::P4Program *program = nullptr;
22 cstring mapKind;
23 explicit ProgramMap(std::string_view kind) : mapKind(kind) {}
24 virtual ~ProgramMap() {}
25
26 public:
27 // Check if map is up-to-date for the specified node; return true if it is
28 bool checkMap(const IR::Node *node) const {
29 if (node == program) {
30 // program has not changed
31 LOG2(mapKind << " is up-to-date");
32 return true;
33 } else {
34 LOG2("Program has changed from " << dbp(program) << " to " << dbp(node));
35 }
36 return false;
37 }
38 void validateMap(const IR::Node *node) const {
39 if (program != node)
40 BUG("Invalid map %1%: computed for %2%, used for %3%", mapKind, dbp(program),
41 dbp(node));
42 }
43 void updateMap(const IR::Node *node) {
44 if (node == nullptr || !node->is<IR::P4Program>()) return;
45 program = node ? node->to<IR::P4Program>() : nullptr;
46 LOG2(mapKind << " updated to " << dbp(node));
47 }
48 void clear() { program = nullptr; }
49};
50
51} // namespace P4
52
53#endif /* FRONTENDS_COMMON_PROGRAMMAP_H_ */
Definition stringify.h:24
Definition node.h:44
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
T * to() noexcept
Definition rtti.h:226
bool is() const noexcept
Definition rtti.h:216