P4C
The P4 Compiler
Loading...
Searching...
No Matches
validateParsedProgram.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 P4_VALIDATEPARSEDPROGRAM_H_
18#define P4_VALIDATEPARSEDPROGRAM_H_
19
20#include "ir/ir.h"
21#include "ir/visitor.h"
22
23namespace P4 {
24
25using namespace literals;
26
54class ValidateParsedProgram final : public Inspector {
55 std::unordered_map<std::pair<const IR::Node *, cstring>, bool> annNames;
56
57 void container(const IR::IContainer *type);
58 // Make sure that type, apply and constructor parameters are distinct
59 void distinctParameters(const IR::TypeParameters *typeParams, const IR::ParameterList *apply,
60 const IR::ParameterList *constr);
61
62 public:
63 ValidateParsedProgram() { setName("ValidateParsedProgram"); }
64 void end_apply(const IR::Node *) override { annNames.clear(); }
65 void postorder(const IR::Annotation *annotations) override;
66 void postorder(const IR::P4Program *program) override;
67 void postorder(const IR::Constant *c) override;
68 void postorder(const IR::SwitchStatement *statement) override;
69 void postorder(const IR::Method *t) override;
70 void postorder(const IR::StructField *f) override;
71 void postorder(const IR::ParserState *s) override;
72 void postorder(const IR::P4Table *t) override;
73 void postorder(const IR::Type_Bits *type) override;
74 void postorder(const IR::Type_Varbits *type) override;
75 void postorder(const IR::ConstructorCallExpression *expression) override;
76 void postorder(const IR::Declaration_Variable *decl) override;
77 void postorder(const IR::Declaration_Instance *inst) override;
78 void postorder(const IR::Declaration_Constant *decl) override;
79 void postorder(const IR::EntriesList *l) override;
80 void postorder(const IR::ReturnStatement *statement) override;
81 void postorder(const IR::ExitStatement *statement) override;
82 void postorder(const IR::Type_Package *package) override { container(package); }
83 void postorder(const IR::P4Control *control) override {
84 container(control);
85 distinctParameters(control->getTypeParameters(), control->getApplyParameters(),
86 control->getConstructorParameters());
87 }
88 void postorder(const IR::P4Parser *parser) override {
89 auto start = parser->states.getDeclaration(IR::ParserState::start);
90 if (!start) {
91 ::P4::error(ErrorType::ERR_INVALID, "Parser %1% has no 'start' state", parser);
92 }
93 container(parser);
94 distinctParameters(parser->getTypeParameters(), parser->getApplyParameters(),
95 parser->getConstructorParameters());
96 }
97 void postorder(const IR::Dots *dots) override;
98 void postorder(const IR::BreakStatement *s) override;
99 void postorder(const IR::ContinueStatement *s) override;
100};
101
102} // namespace P4
103
104#endif /* P4_VALIDATEPARSEDPROGRAM_H_ */
Definition node.h:94
Definition visitor.h:400
Definition validateParsedProgram.h:54
void postorder(const IR::Annotation *annotations) override
Structured annotations cannot reuse names.
Definition validateParsedProgram.cpp:53
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51