/
validateParsedProgram.cpp
267 lines (237 loc) · 11.3 KB
/
validateParsedProgram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "validateParsedProgram.h"
namespace P4 {
/// Check that the type of a constant is either bit<>, int<> or int
void ValidateParsedProgram::postorder(const IR::Constant *c) {
if (c->type != nullptr && !c->type->is<IR::Type_Unknown>() && !c->type->is<IR::Type_Bits>() &&
!c->type->is<IR::Type_InfInt>())
BUG("Invalid type %1% for constant %2%", c->type, c);
}
/// Check that underscore is not a method name
/// Check that constructors do not have a return type
/// Check that extern constructor names match the enclosing extern
void ValidateParsedProgram::postorder(const IR::Method *m) {
if (m->name.isDontCare())
::error(ErrorType::ERR_INVALID, "%1%: invalid method/function name.", m->name);
if (auto ext = findContext<IR::Type_Extern>()) {
if (m->name == ext->name && m->type->returnType != nullptr)
::error(ErrorType::ERR_INVALID, "%1%: invalid constructor; cannot have a return type.",
m);
if (m->type->returnType == nullptr) {
if (m->name != ext->name) {
::error(ErrorType::ERR_INVALID, "%1%: invalid type; method has no return type.", m);
return;
}
for (auto p : *m->type->parameters)
if (p->direction != IR::Direction::None)
::error(ErrorType::ERR_INVALID,
"%1%: Constructor parameters cannot have a direction.", p);
}
}
}
/// Structured annotations cannot reuse names
void ValidateParsedProgram::postorder(const IR::Annotations *annotations) {
std::multiset<cstring> namesUsed;
for (auto a : annotations->annotations) namesUsed.emplace(a->name);
for (auto a : annotations->annotations) {
if (!a->structured) continue;
if (namesUsed.count(a->name) > 1)
::error(ErrorType::ERR_DUPLICATE, "%1%: duplicate name for structured annotation", a);
}
}
/// Struct field names cannot be underscore
void ValidateParsedProgram::postorder(const IR::StructField *f) {
if (f->name.isDontCare()) ::error(ErrorType::ERR_INVALID, "%1%: invalid field name", f->name);
}
/// Width of a bit<> or int<> type is greater than 0
void ValidateParsedProgram::postorder(const IR::Type_Bits *type) {
if (type->expression)
// cannot validate yet
return;
if (type->size < 0) ::error(ErrorType::ERR_INVALID, "%1%: invalid type size", type);
if (type->size == 0 && type->isSigned)
::error(ErrorType::ERR_INVALID, "%1%: invalid type size", type);
}
void ValidateParsedProgram::postorder(const IR::Type_Varbits *type) {
if (type->expression)
// cannot validate yet
return;
if (type->size < 0) ::error(ErrorType::ERR_INVALID, "%1%: invalid type size", type);
}
/// The accept and reject states cannot be implemented
void ValidateParsedProgram::postorder(const IR::ParserState *s) {
if (s->name == IR::ParserState::accept || s->name == IR::ParserState::reject)
::error(ErrorType::ERR_INVALID,
"Invalid parser state: %1% should not be implemented, it is built-in", s->name);
}
/// All parameters of a constructor must be directionless.
/// This only checks controls, parsers and packages
void ValidateParsedProgram::container(const IR::IContainer *type) {
for (auto p : type->getConstructorParameters()->parameters)
if (p->direction != IR::Direction::None)
::error(ErrorType::ERR_INVALID,
"%1%: invalid direction. Constructor parameters cannot have a direction", p);
}
/// Tables must have an 'actions' property.
void ValidateParsedProgram::postorder(const IR::P4Table *t) {
auto ac = t->getActionList();
if (ac == nullptr)
::error(ErrorType::ERR_EXPECTED, "%1%: expected '%2%' property", t->name,
IR::TableProperties::actionsPropertyName);
}
/// Checks that the names of the three parameter lists for some constructs
/// are all distinct (type parameters, apply parameters, constructor parameters)
void ValidateParsedProgram::distinctParameters(const IR::TypeParameters *typeParams,
const IR::ParameterList *apply,
const IR::ParameterList *constr) {
std::map<cstring, const IR::Node *> found;
for (auto p : typeParams->parameters) found.emplace(p->getName(), p);
for (auto p : apply->parameters) {
auto it = found.find(p->getName());
if (it != found.end())
::error(ErrorType::ERR_DUPLICATE, "%1% duplicates %2%.", it->second, p);
else
found.emplace(p->getName(), p);
}
for (auto p : constr->parameters) {
auto it = found.find(p->getName());
if (it != found.end())
::error(ErrorType::ERR_DUPLICATE, "%1% duplicates %2%.", it->second, p);
}
}
/// Cannot invoke constructors in actions
void ValidateParsedProgram::postorder(const IR::ConstructorCallExpression *expression) {
auto inAction = findContext<IR::P4Action>();
if (inAction != nullptr)
::error(ErrorType::ERR_INVALID,
"%1%: invalid call. Constructor calls not allowed in actions.", expression);
}
/// Variable names cannot be underscore
void ValidateParsedProgram::postorder(const IR::Declaration_Variable *decl) {
if (decl->name.isDontCare())
::error(ErrorType::ERR_INVALID, "%1%: invalid variable name.", decl);
}
/// Instance names cannot be don't care
/// Do not declare instances in apply {} blocks, parser states or actions
void ValidateParsedProgram::postorder(const IR::Declaration_Instance *decl) {
if (!decl->type->is<IR::Type_Name>() && !decl->type->is<IR::Type_Specialized>() &&
!decl->type->is<IR::Type_Extern>()) // P4_14 only?
::error(ErrorType::ERR_INVALID, "%1%: invalid type for declaration", decl->type);
if (decl->name.isDontCare())
::error(ErrorType::ERR_INVALID, "%1%: invalid instance name.", decl);
if (findContext<IR::BlockStatement>() && // we're looking for the apply block
findContext<IR::P4Control>() && // of a control
!findContext<IR::Declaration_Instance>()) { // but not in an instance initializer
::error(ErrorType::ERR_INVALID,
"%1%: invalid declaration. Instantiations cannot be in a control 'apply' block.",
decl);
}
if (findContext<IR::ParserState>())
::error(ErrorType::ERR_INVALID,
"%1%: invalid declaration. Instantiations cannot be in a parser state.", decl);
if (findContext<IR::Function>() || findContext<IR::Method>())
::error(ErrorType::ERR_INVALID,
"%1%: invalid declaration. Instantiations cannot be in a function or method.",
decl);
auto inAction = findContext<IR::P4Action>();
if (inAction != nullptr)
::error(ErrorType::ERR_INVALID, "%1%: declaration. Instantiations not allowed in actions.",
decl);
}
/// Constant names cannot be underscore
void ValidateParsedProgram::postorder(const IR::Declaration_Constant *decl) {
if (decl->name.isDontCare())
::error(ErrorType::ERR_INVALID, "%1%: invalid constant name.", decl);
}
void ValidateParsedProgram::postorder(const IR::EntriesList *l) {
auto table = findContext<IR::P4Table>();
if (table == nullptr) {
::error(ErrorType::ERR_INVALID,
"%1%: invalid entries list. Table entries must belong to a table.", l);
return;
}
}
/// Default label in switch statement is always the last one.
void ValidateParsedProgram::postorder(const IR::SwitchStatement *statement) {
const IR::SwitchCase *defaultFound = nullptr;
for (auto c : statement->cases) {
if (defaultFound != nullptr) {
if (c->label->is<IR::DefaultExpression>())
::error(ErrorType::ERR_INVALID, "%1% has multiple 'default' labels: %2% and %3%.",
statement, defaultFound->label, c->label);
else
warn(ErrorType::WARN_ORDERING, "%1%: label following 'default' %2% label.",
c->label, defaultFound->label);
break;
}
if (c->label->is<IR::DefaultExpression>()) defaultFound = c;
}
}
/// Return statements are not allowed in parsers
void ValidateParsedProgram::postorder(const IR::ReturnStatement *statement) {
if (!findContext<IR::Function>()) {
auto inParser = findContext<IR::P4Parser>();
if (inParser != nullptr)
::error(ErrorType::ERR_INVALID,
"%1%: invalid statement. 'return' statements not allowed in parsers.",
statement);
}
}
/// Exit statements are not allowed in parsers or functions
void ValidateParsedProgram::postorder(const IR::ExitStatement *statement) {
auto inParser = findContext<IR::P4Parser>();
if (inParser != nullptr)
::error(ErrorType::ERR_INVALID,
"%1%: invalid statement. 'exit' statements not allowed in parsers.", statement);
if (findContext<IR::Function>())
::error(ErrorType::ERR_INVALID,
"%1% invalid statement. 'exit' statements not allowed in functions.", statement);
}
void ValidateParsedProgram::postorder(const IR::P4Program *program) {
IR::IndexedVector<IR::Node> declarations;
for (auto decl : *program->getDeclarations()) {
cstring name = decl->getName();
auto existing = declarations.getDeclaration(name);
if (existing != nullptr) {
if (!existing->is<IR::IFunctional>() || !decl->is<IR::IFunctional>() ||
existing->typeId() != decl->typeId() || decl->is<IR::P4Action>()) {
::error(ErrorType::ERR_DUPLICATE, "%1% duplicates %2%.", decl->getName(),
existing->getName());
}
} else {
declarations.push_back(decl->getNode());
}
}
}
void ValidateParsedProgram::postorder(const IR::Dots *dots) {
auto context = getContext();
CHECK_NULL(context);
if (context->node->is<IR::ListExpression>()) {
auto list = context->node->to<IR::ListExpression>();
if (context->child_index != (int)list->size() - 1)
::error(ErrorType::ERR_INVALID, "%1% must be the last element", dots);
}
}
/// Check that continue and break statements are only used in the context of a for statement.
void ValidateParsedProgram::postorder(const IR::BreakStatement *s) {
if (!findContext<IR::ForStatement>() && !findContext<IR::ForInStatement>())
::error(ErrorType::ERR_INVALID,
"%1%: break statement must be used in the context of a for statement.", s);
}
void ValidateParsedProgram::postorder(const IR::ContinueStatement *s) {
if (!findContext<IR::ForStatement>() && !findContext<IR::ForInStatement>())
::error(ErrorType::ERR_INVALID,
"%1%: continue statement must be used in the context of a for statement.", s);
}
} // namespace P4