forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_check.cc
More file actions
586 lines (524 loc) · 21.4 KB
/
Copy pathtype_check.cc
File metadata and controls
586 lines (524 loc) · 21.4 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Copyright (c) 2015 PLUMgrid, 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 <set>
#include <algorithm>
#include "bcc_exception.h"
#include "type_check.h"
#include "lexer.h"
namespace ebpf {
namespace cc {
using std::for_each;
using std::set;
StatusTuple TypeCheck::visit_block_stmt_node(BlockStmtNode *n) {
// enter scope
if (n->scope_)
scopes_->push_var(n->scope_);
if (!n->stmts_.empty()) {
for (auto it = n->stmts_.begin(); it != n->stmts_.end(); ++it)
TRY2((*it)->accept(this));
}
if (n->scope_)
scopes_->pop_var();
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_if_stmt_node(IfStmtNode *n) {
TRY2(n->cond_->accept(this));
//if (n->cond_->typeof_ != ExprNode::INTEGER)
// return mkstatus_(n, "If condition must be a numeric type");
TRY2(n->true_block_->accept(this));
if (n->false_block_) {
TRY2(n->false_block_->accept(this));
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_onvalid_stmt_node(OnValidStmtNode *n) {
TRY2(n->cond_->accept(this));
auto sdecl = static_cast<StructVariableDeclStmtNode*>(n->cond_->decl_);
if (sdecl->storage_type_ != StructVariableDeclStmtNode::STRUCT_REFERENCE)
return mkstatus_(n, "on_valid condition must be a reference type");
TRY2(n->block_->accept(this));
if (n->else_block_) {
TRY2(n->else_block_->accept(this));
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_switch_stmt_node(SwitchStmtNode *n) {
TRY2(n->cond_->accept(this));
if (n->cond_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Switch condition must be a numeric type");
TRY2(n->block_->accept(this));
for (auto it = n->block_->stmts_.begin(); it != n->block_->stmts_.end(); ++it) {
/// @todo check for duplicates
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_case_stmt_node(CaseStmtNode *n) {
if (n->value_) {
TRY2(n->value_->accept(this));
if (n->value_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Switch condition must be a numeric type");
}
TRY2(n->block_->accept(this));
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_ident_expr_node(IdentExprNode *n) {
n->decl_ = scopes_->current_var()->lookup(n->name_, SCOPE_GLOBAL);
if (!n->decl_)
return mkstatus_(n, "Variable %s lookup failed", n->c_str());
n->typeof_ = ExprNode::UNKNOWN;
if (n->sub_name_.empty()) {
if (n->decl_->storage_type_ == VariableDeclStmtNode::INTEGER) {
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = n->decl_->bit_width_;
n->flags_[ExprNode::WRITE] = true;
} else if (n->decl_->is_struct()) {
n->typeof_ = ExprNode::STRUCT;
auto sdecl = static_cast<StructVariableDeclStmtNode*>(n->decl_);
if (sdecl->struct_id_->scope_name_ == "proto") {
n->struct_type_ = proto_scopes_->top_struct()->lookup(sdecl->struct_id_->name_, true);
n->flags_[ExprNode::PROTO] = true;
} else {
n->struct_type_ = scopes_->top_struct()->lookup(sdecl->struct_id_->name_, true);
}
if (!n->struct_type_)
return mkstatus_(n, "Type %s has not been declared", sdecl->struct_id_->full_name().c_str());
n->bit_width_ = n->struct_type_->bit_width_;
}
} else {
if (n->decl_->storage_type_ == VariableDeclStmtNode::INTEGER)
return mkstatus_(n, "Subfield access not valid for numeric types");
auto sdecl = static_cast<StructVariableDeclStmtNode*>(n->decl_);
if (sdecl->struct_id_->scope_name_ == "proto") {
n->struct_type_ = proto_scopes_->top_struct()->lookup(sdecl->struct_id_->name_, true);
n->flags_[ExprNode::PROTO] = true;
} else {
n->struct_type_ = scopes_->top_struct()->lookup(sdecl->struct_id_->name_, true);
}
if (!n->struct_type_)
return mkstatus_(n, "Type %s has not been declared", sdecl->struct_id_->full_name().c_str());
n->sub_decl_ = n->struct_type_->field(n->sub_name_);
if (!n->sub_decl_)
return mkstatus_(n, "Access to invalid subfield %s.%s", n->c_str(), n->sub_name_.c_str());
if (n->sub_decl_->storage_type_ != VariableDeclStmtNode::INTEGER)
return mkstatus_(n, "Accessing non-numeric subfield %s.%s", n->c_str(), n->sub_name_.c_str());
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = n->sub_decl_->bit_width_;
n->flags_[ExprNode::WRITE] = true;
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_assign_expr_node(AssignExprNode *n) {
/// @todo check lhs is assignable
TRY2(n->lhs_->accept(this));
if (n->lhs_->typeof_ == ExprNode::STRUCT) {
TRY2(n->rhs_->accept(this));
if (n->rhs_->typeof_ != ExprNode::STRUCT)
return mkstatus_(n, "Right-hand side of assignment must be a struct");
} else {
if (n->lhs_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Left-hand side of assignment must be a numeric type");
if (!n->lhs_->flags_[ExprNode::WRITE])
return mkstatus_(n, "Left-hand side of assignment is read-only");
TRY2(n->rhs_->accept(this));
if (n->rhs_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Right-hand side of assignment must be a numeric type");
}
n->typeof_ = ExprNode::VOID;
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_packet_expr_node(PacketExprNode *n) {
StructDeclStmtNode *struct_type = proto_scopes_->top_struct()->lookup(n->id_->name_, true);
if (!struct_type)
return mkstatus_(n, "Undefined packet header %s", n->id_->c_str());
if (n->id_->sub_name_.empty()) {
n->typeof_ = ExprNode::STRUCT;
n->struct_type_ = struct_type;
} else {
VariableDeclStmtNode *sub_decl = struct_type->field(n->id_->sub_name_);
if (!sub_decl)
return mkstatus_(n, "Access to invalid subfield %s.%s", n->id_->c_str(), n->id_->sub_name_.c_str());
n->typeof_ = ExprNode::INTEGER;
if (n->is_ref())
n->bit_width_ = 64;
else
n->bit_width_ = sub_decl->bit_width_;
}
n->flags_[ExprNode::WRITE] = true;
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_integer_expr_node(IntegerExprNode *n) {
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = n->bits_;
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_string_expr_node(StringExprNode *n) {
n->typeof_ = ExprNode::STRING;
n->flags_[ExprNode::IS_REF] = true;
n->bit_width_ = n->val_.size() << 3;
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_binop_expr_node(BinopExprNode *n) {
TRY2(n->lhs_->accept(this));
if (n->lhs_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Left-hand side of binary expression must be a numeric type");
TRY2(n->rhs_->accept(this));
if (n->rhs_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Right-hand side of binary expression must be a numeric type");
n->typeof_ = ExprNode::INTEGER;
switch(n->op_) {
case Tok::TCEQ:
case Tok::TCNE:
case Tok::TCLT:
case Tok::TCLE:
case Tok::TCGT:
case Tok::TCGE:
n->bit_width_ = 1;
default:
n->bit_width_ = std::max(n->lhs_->bit_width_, n->rhs_->bit_width_);
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_unop_expr_node(UnopExprNode *n) {
TRY2(n->expr_->accept(this));
if (n->expr_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Unary operand must be a numeric type");
n->copy_type(*n->expr_);
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_bitop_expr_node(BitopExprNode *n) {
if (n->expr_->typeof_ != ExprNode::INTEGER)
return mkstatus_(n, "Bitop [] can only operate on numeric types");
n->typeof_ = ExprNode::INTEGER;
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_goto_expr_node(GotoExprNode *n) {
//n->id_->accept(this);
n->typeof_ = ExprNode::VOID;
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_return_expr_node(ReturnExprNode *n) {
TRY2(n->expr_->accept(this));
n->typeof_ = ExprNode::VOID;
return StatusTuple(0);
}
StatusTuple TypeCheck::expect_method_arg(MethodCallExprNode *n, size_t num, size_t num_def_args = 0) {
if (num_def_args == 0) {
if (n->args_.size() != num)
return mkstatus_(n, "%s expected %d argument%s, %zu given", n->id_->sub_name_.c_str(),
num, num == 1 ? "" : "s", n->args_.size());
} else {
if (n->args_.size() < num - num_def_args || n->args_.size() > num)
return mkstatus_(n, "%s expected %d argument%s (%d default), %zu given", n->id_->sub_name_.c_str(),
num, num == 1 ? "" : "s", num_def_args, n->args_.size());
}
return StatusTuple(0);
}
StatusTuple TypeCheck::check_lookup_method(MethodCallExprNode *n) {
auto table = scopes_->top_table()->lookup(n->id_->name_);
if (!table)
return mkstatus_(n, "Unknown table name %s", n->id_->c_str());
TRY2(expect_method_arg(n, 2, 1));
if (table->type_id()->name_ == "LPM")
return mkstatus_(n, "LPM unsupported");
if (n->block_->scope_) {
auto result = make_unique<StructVariableDeclStmtNode>(table->leaf_id()->copy(), make_unique<IdentExprNode>("_result"),
VariableDeclStmtNode::STRUCT_REFERENCE);
n->block_->scope_->add("_result", result.get());
n->block_->stmts_.insert(n->block_->stmts_.begin(), move(result));
}
return StatusTuple(0);
}
StatusTuple TypeCheck::check_update_method(MethodCallExprNode *n) {
auto table = scopes_->top_table()->lookup(n->id_->name_);
if (!table)
return mkstatus_(n, "Unknown table name %s", n->id_->c_str());
if (table->type_id()->name_ == "FIXED_MATCH" || table->type_id()->name_ == "INDEXED")
TRY2(expect_method_arg(n, 2));
else if (table->type_id()->name_ == "LPM")
TRY2(expect_method_arg(n, 3));
return StatusTuple(0);
}
StatusTuple TypeCheck::check_delete_method(MethodCallExprNode *n) {
auto table = scopes_->top_table()->lookup(n->id_->name_);
if (!table)
return mkstatus_(n, "Unknown table name %s", n->id_->c_str());
if (table->type_id()->name_ == "FIXED_MATCH" || table->type_id()->name_ == "INDEXED")
TRY2(expect_method_arg(n, 1));
else if (table->type_id()->name_ == "LPM")
{}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_method_call_expr_node(MethodCallExprNode *n) {
// be sure to visit those child nodes ASAP, so their properties can
// be propagated up to this node and be ready to be used
for (auto it = n->args_.begin(); it != n->args_.end(); ++it) {
TRY2((*it)->accept(this));
}
n->typeof_ = ExprNode::VOID;
if (n->id_->sub_name_.size()) {
if (n->id_->sub_name_ == "lookup") {
TRY2(check_lookup_method(n));
} else if (n->id_->sub_name_ == "update") {
TRY2(check_update_method(n));
} else if (n->id_->sub_name_ == "delete") {
TRY2(check_delete_method(n));
} else if (n->id_->sub_name_ == "rewrite_field" && n->id_->name_ == "pkt") {
TRY2(expect_method_arg(n, 2));
n->args_[0]->flags_[ExprNode::IS_LHS] = true;
}
} else if (n->id_->name_ == "log") {
if (n->args_.size() < 1)
return mkstatus_(n, "%s expected at least 1 argument", n->id_->c_str());
if (n->args_[0]->typeof_ != ExprNode::STRING)
return mkstatus_(n, "%s expected a string for argument 1", n->id_->c_str());
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = 32;
} else if (n->id_->name_ == "atomic_add") {
TRY2(expect_method_arg(n, 2));
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = n->args_[0]->bit_width_;
n->args_[0]->flags_[ExprNode::IS_LHS] = true;
} else if (n->id_->name_ == "incr_cksum") {
TRY2(expect_method_arg(n, 4, 1));
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = 16;
} else if (n->id_->name_ == "sizeof") {
TRY2(expect_method_arg(n, 1));
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = 32;
} else if (n->id_->name_ == "get_usec_time") {
TRY2(expect_method_arg(n, 0));
n->typeof_ = ExprNode::INTEGER;
n->bit_width_ = 64;
}
if (!n->block_->stmts_.empty()) {
if (n->id_->sub_name_ != "update" && n->id_->sub_name_ != "lookup")
return mkstatus_(n, "%s does not allow trailing block statements", n->id_->full_name().c_str());
TRY2(n->block_->accept(this));
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_table_index_expr_node(TableIndexExprNode *n) {
n->table_ = scopes_->top_table()->lookup(n->id_->name_);
if (!n->table_) return mkstatus_(n, "Unknown table name %s", n->id_->c_str());
TRY2(n->index_->accept(this));
if (n->index_->struct_type_ != n->table_->key_type_)
return mkstatus_(n, "Key to table %s lookup must be of type %s", n->id_->c_str(), n->table_->key_id()->c_str());
if (n->sub_) {
n->sub_decl_ = n->table_->leaf_type_->field(n->sub_->name_);
if (!n->sub_decl_)
return mkstatus_(n, "Field %s is not a member of %s", n->sub_->c_str(), n->table_->leaf_id()->c_str());
n->typeof_ = ExprNode::INTEGER;
} else {
n->typeof_ = ExprNode::STRUCT;
n->flags_[ExprNode::IS_REF] = true;
n->struct_type_ = n->table_->leaf_type_;
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_expr_stmt_node(ExprStmtNode *n) {
TRY2(n->expr_->accept(this));
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_struct_variable_decl_stmt_node(StructVariableDeclStmtNode *n) {
//TRY2(n->struct_id_->accept(this));
//TRY2(n->id_->accept(this));
if (!n->init_.empty()) {
StructDeclStmtNode *type;
if (n->struct_id_->scope_name_ == "proto")
type = proto_scopes_->top_struct()->lookup(n->struct_id_->name_, true);
else
type = scopes_->top_struct()->lookup(n->struct_id_->name_, true);
if (!type)
return mkstatus_(n, "type %s does not exist", n->struct_id_->full_name().c_str());
// init remaining fields to 0
set<string> used;
for (auto i = n->init_.begin(); i != n->init_.end(); ++i) {
auto asn = static_cast<AssignExprNode*>(i->get());
auto id = static_cast<IdentExprNode *>(asn->lhs_.get());
used.insert(id->sub_name_);
}
for (auto f = type->stmts_.begin(); f != type->stmts_.end(); ++f) {
if (used.find((*f)->id_->name_) == used.end()) {
auto id = make_unique<IdentExprNode>(n->id_->name_);
id->append_dot((*f)->id_->name_);
n->init_.push_back(make_unique<AssignExprNode>(move(id), make_unique<IntegerExprNode>("0")));
}
}
for (auto it = n->init_.begin(); it != n->init_.end(); ++it) {
TRY2((*it)->accept(this));
}
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_integer_variable_decl_stmt_node(IntegerVariableDeclStmtNode *n) {
//TRY2(n->id_->accept(this));
if (!n->init_.empty()) {
TRY2(n->init_[0]->accept(this));
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_struct_decl_stmt_node(StructDeclStmtNode *n) {
//TRY2(n->id_->accept(this));
for (auto it = n->stmts_.begin(); it != n->stmts_.end(); ++it) {
TRY2((*it)->accept(this));
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_parser_state_stmt_node(ParserStateStmtNode *n) {
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_state_decl_stmt_node(StateDeclStmtNode *n) {
if (!n->id_) {
return StatusTuple(0);
}
auto s1 = proto_scopes_->top_state()->lookup(n->id_->name_, true);
if (s1) {
const string &name = n->id_->name_;
auto offset_var = make_unique<IntegerVariableDeclStmtNode>(make_unique<IdentExprNode>("$" + name), "64");
offset_var->init_.push_back(make_unique<AssignExprNode>(offset_var->id_->copy(), make_unique<IntegerExprNode>("0")));
scopes_->current_var()->add("$" + name, offset_var.get());
s1->subs_[0].block_->scope_->add("$" + name, offset_var.get());
n->init_.push_back(move(offset_var));
n->parser_ = ParserStateStmtNode::make(n->id_);
n->parser_->next_state_ = s1->subs_[0].block_.get();
n->parser_->scope_id_ = n->scope_id_;
auto p = proto_scopes_->top_struct()->lookup(n->id_->name_, true);
if (!p) return mkstatus_(n, "unable to find struct decl for parser state %s", n->id_->full_name().c_str());
// $proto = parsed_bytes; parsed_bytes += sizeof($proto);
auto asn1 = make_unique<AssignExprNode>(make_unique<IdentExprNode>("$" + n->id_->name_),
make_unique<IdentExprNode>("parsed_bytes"));
n->init_.push_back(make_unique<ExprStmtNode>(move(asn1)));
auto add_expr = make_unique<BinopExprNode>(make_unique<IdentExprNode>("parsed_bytes"), Tok::TPLUS,
make_unique<IntegerExprNode>(std::to_string(p->bit_width_ >> 3), 64));
auto asn2 = make_unique<AssignExprNode>(make_unique<IdentExprNode>("parsed_bytes"), move(add_expr));
n->init_.push_back(make_unique<ExprStmtNode>(move(asn2)));
}
for (auto it = n->init_.begin(); it != n->init_.end(); ++it) {
TRY2((*it)->accept(this));
}
for (auto it = n->subs_.begin(); it != n->subs_.end(); ++it) {
scopes_->push_state(it->scope_);
TRY2(it->block_->accept(this));
if (s1) {
if (it->id_->name_ == "") {
it->parser_ = ParserStateStmtNode::make(it->id_);
it->parser_->next_state_ = s1->subs_[0].block_.get();
it->parser_->scope_id_ = n->scope_id_ + n->id_->name_ + "_";
} else if (auto s2 = proto_scopes_->top_state()->lookup(it->id_->name_, true)) {
it->parser_ = ParserStateStmtNode::make(it->id_);
it->parser_->next_state_ = s2->subs_[0].block_.get();
it->parser_->scope_id_ = n->scope_id_ + n->id_->name_ + "_";
}
if (it->parser_) {
TRY2(it->parser_->accept(this));
}
}
scopes_->pop_state();
}
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_match_decl_stmt_node(MatchDeclStmtNode *n) {
//TRY2(n->id_->accept(this));
for (auto it = n->formals_.begin(); it != n->formals_.end(); ++it) {
TRY2((*it)->accept(this));
}
TRY2(n->block_->accept(this));
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_miss_decl_stmt_node(MissDeclStmtNode *n) {
//TRY2(n->id_->accept(this));
for (auto it = n->formals_.begin(); it != n->formals_.end(); ++it) {
TRY2((*it)->accept(this));
}
TRY2(n->block_->accept(this));
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_failure_decl_stmt_node(FailureDeclStmtNode *n) {
//TRY2(n->id_->accept(this));
for (auto it = n->formals_.begin(); it != n->formals_.end(); ++it) {
TRY2((*it)->accept(this));
}
TRY2(n->block_->accept(this));
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_table_decl_stmt_node(TableDeclStmtNode *n) {
n->key_type_ = scopes_->top_struct()->lookup(n->key_id()->name_, true);
if (!n->key_type_)
return mkstatus_(n, "Table key type %s undefined", n->key_id()->c_str());
n->key_id()->bit_width_ = n->key_type_->bit_width_;
n->leaf_type_ = scopes_->top_struct()->lookup(n->leaf_id()->name_, true);
if (!n->leaf_type_)
return mkstatus_(n, "Table leaf type %s undefined", n->leaf_id()->c_str());
n->leaf_id()->bit_width_ = n->leaf_type_->bit_width_;
if (n->type_id()->name_ == "INDEXED" && n->policy_id()->name_ != "AUTO") {
fprintf(stderr, "Table %s is INDEXED, policy should be AUTO\n", n->id_->c_str());
n->policy_id()->name_ = "AUTO";
}
if (n->policy_id()->name_ != "AUTO" && n->policy_id()->name_ != "NONE")
return mkstatus_(n, "Unsupported policy type %s", n->policy_id()->c_str());
return StatusTuple(0);
}
StatusTuple TypeCheck::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
for (auto it = n->formals_.begin(); it != n->formals_.end(); ++it) {
VariableDeclStmtNode *var = it->get();
TRY2(var->accept(this));
if (var->is_struct()) {
if (!var->is_pointer())
return mkstatus_(n, "Only struct references allowed in function definitions");
}
}
scopes_->push_state(n->scope_);
TRY2(n->block_->accept(this));
scopes_->pop_state();
return StatusTuple(0);
}
StatusTuple TypeCheck::visit(Node *root) {
BlockStmtNode *b = static_cast<BlockStmtNode*>(root);
scopes_->set_current(scopes_->top_state());
scopes_->set_current(scopes_->top_var());
// // packet data in bpf socket
// if (scopes_->top_struct()->lookup("_skbuff", true)) {
// return StatusTuple(-1, "_skbuff already defined");
// }
// auto skb_type = make_unique<StructDeclStmtNode>(make_unique<IdentExprNode>("_skbuff"));
// scopes_->top_struct()->add("_skbuff", skb_type.get());
// b->stmts_.push_back(move(skb_type));
// if (scopes_->current_var()->lookup("skb", true)) {
// return StatusTuple(-1, "skb already defined");
// }
// auto skb = make_unique<StructVariableDeclStmtNode>(make_unique<IdentExprNode>("_skbuff"),
// make_unique<IdentExprNode>("skb"));
// skb->storage_type_ = VariableDeclStmtNode::STRUCT_REFERENCE;
// scopes_->current_var()->add("skb", skb.get());
// b->stmts_.push_back(move(skb));
// offset counter
auto parsed_bytes = make_unique<IntegerVariableDeclStmtNode>(
make_unique<IdentExprNode>("parsed_bytes"), "64");
parsed_bytes->init_.push_back(make_unique<AssignExprNode>(parsed_bytes->id_->copy(), make_unique<IntegerExprNode>("0")));
scopes_->current_var()->add("parsed_bytes", parsed_bytes.get());
b->stmts_.push_back(move(parsed_bytes));
TRY2(b->accept(this));
if (!errors_.empty()) {
for (auto it = errors_.begin(); it != errors_.end(); ++it) {
fprintf(stderr, "%s\n", it->c_str());
}
return StatusTuple(-1, errors_.begin()->c_str());
}
return StatusTuple(0);
}
} // namespace cc
} // namespace ebpf