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