Skip to content

Commit

Permalink
feat(irBuilder): Support foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
byakuren-hijiri committed Jul 29, 2024
1 parent 02380a7 commit 9385648
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/internals/tactIRBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ export class AstMapper {
break;
case "statement_while":
case "statement_until":
stmt.statements.forEach((s) => this.processStmt(s));
break;
case "statement_repeat":
case "statement_foreach":
stmt.statements.forEach((s) => this.processStmt(s));
break;
default:
Expand Down Expand Up @@ -626,7 +625,8 @@ export class TactIRBuilder {
} else if (
stmt.kind == "statement_while" ||
stmt.kind == "statement_until" ||
stmt.kind == "statement_repeat"
stmt.kind == "statement_repeat" ||
stmt.kind == "statement_foreach"
) {
// Create an edge from the current node (loop condition) back to the start of the loop body,
// and from the end of the loop body back to the current node to represent the loop's cycle.
Expand Down
10 changes: 10 additions & 0 deletions test/contracts/loops-4.cfg.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
digraph "loops-4" {
node [shape=box];
subgraph "cluster_test" {
label="test";
"test_141" [label="let m: map<Int, Int> = emptyMap()"];
"test_142" [label="foreach ((k, v) of m)"];
"test_141" -> "test_142";
"test_142" -> "test_142";
}
}
44 changes: 44 additions & 0 deletions test/contracts/loops-4.cfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"projectName": "loops-4",
"functions": [
{
"name": "test",
"cfg": {
"nodes": [
{
"id": 141,
"stmtID": 29445,
"srcEdges": [],
"dstEdges": [
143
]
},
{
"id": 142,
"stmtID": 29449,
"srcEdges": [
143,
144
],
"dstEdges": [
144
]
}
],
"edges": [
{
"id": 143,
"src": 141,
"dst": 142
},
{
"id": 144,
"src": 142,
"dst": 142
}
]
}
}
],
"contracts": []
}
8 changes: 8 additions & 0 deletions test/contracts/loops-4.expected.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test/contracts/loops-4.tact:2:5:
1 | fun test() {
> 2 | let m: map<Int, Int> = emptyMap();
^
3 | foreach (k, v in m) {
Read-only variable
Help: Consider creating a constant instead
See: https://nowarp.github.io/docs/misti/docs/detectors/ReadOnlyVariables
5 changes: 5 additions & 0 deletions test/contracts/loops-4.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fun test() {
let m: map<Int, Int> = emptyMap();
foreach (k, v in m) {
}
}
16 changes: 8 additions & 8 deletions test/contracts/messages-1.cfg.dot
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
digraph "messages-1" {
node [shape=box];
subgraph "cluster_A__receive_internal_comment_31172_increment" {
label="A__receive_internal_comment_31172_increment";
"A__receive_internal_comment_31172_increment_142" [label="self.val = self.val + 1"];
"A__receive_internal_comment_31172_increment_143" [label="self.reply(M{value: self.val}.toCell())"];
"A__receive_internal_comment_31172_increment_142" -> "A__receive_internal_comment_31172_increment_143";
subgraph "cluster_A__receive_internal_comment_34250_increment" {
label="A__receive_internal_comment_34250_increment";
"A__receive_internal_comment_34250_increment_142" [label="self.val = self.val + 1"];
"A__receive_internal_comment_34250_increment_143" [label="self.reply(M{value: self.val}.toCell())"];
"A__receive_internal_comment_34250_increment_142" -> "A__receive_internal_comment_34250_increment_143";
}
subgraph "cluster_A__receive_internal_comment_31187_query" {
label="A__receive_internal_comment_31187_query";
"A__receive_internal_comment_31187_query_145" [label="self.reply(M{value: self.val}.toCell())"];
subgraph "cluster_A__receive_internal_comment_34265_query" {
label="A__receive_internal_comment_34265_query";
"A__receive_internal_comment_34265_query_145" [label="self.reply(M{value: self.val}.toCell())"];
}
}
10 changes: 5 additions & 5 deletions test/contracts/messages-1.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
"name": "A",
"methods": [
{
"name": "A.receive_internal_comment_29473_increment",
"name": "A.receive_internal_comment_32551_increment",
"cfg": {
"nodes": [
{
"id": 142,
"stmtID": 29459,
"stmtID": 32537,
"srcEdges": [],
"dstEdges": [
144
]
},
{
"id": 143,
"stmtID": 29472,
"stmtID": 32550,
"srcEdges": [
144
],
Expand All @@ -36,12 +36,12 @@
}
},
{
"name": "A.receive_internal_comment_29488_query",
"name": "A.receive_internal_comment_32566_query",
"cfg": {
"nodes": [
{
"id": 145,
"stmtID": 29487,
"stmtID": 32565,
"srcEdges": [],
"dstEdges": []
}
Expand Down
8 changes: 4 additions & 4 deletions test/contracts/never-accessed-1.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"nodes": [
{
"id": 141,
"stmtID": 32841,
"stmtID": 35919,
"srcEdges": [],
"dstEdges": [
143
]
},
{
"id": 142,
"stmtID": 32846,
"stmtID": 35924,
"srcEdges": [
143
],
Expand All @@ -25,7 +25,7 @@
},
{
"id": 144,
"stmtID": 32845,
"stmtID": 35923,
"srcEdges": [
145
],
Expand All @@ -35,7 +35,7 @@
},
{
"id": 146,
"stmtID": 32848,
"stmtID": 35926,
"srcEdges": [
147
],
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/never-accessed-2.cfg.dot
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
digraph "never-accessed-2" {
node [shape=box];
subgraph "cluster_FieldTest__init_37609" {
label="FieldTest__init_37609";
"FieldTest__init_37609_142" [label="self.f1 = sender()"];
subgraph "cluster_FieldTest__init_40687" {
label="FieldTest__init_40687";
"FieldTest__init_40687_142" [label="self.f1 = sender()"];
}
subgraph "cluster_FieldTest__use_f1" {
label="FieldTest__use_f1";
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/never-accessed-2.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"name": "FieldTest",
"methods": [
{
"name": "FieldTest.init_35939",
"name": "FieldTest.init_39017",
"cfg": {
"nodes": [
{
"id": 142,
"stmtID": 35930,
"stmtID": 39008,
"srcEdges": [],
"dstEdges": []
}
Expand All @@ -25,7 +25,7 @@
"nodes": [
{
"id": 143,
"stmtID": 35937,
"stmtID": 39015,
"srcEdges": [],
"dstEdges": []
}
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/never-accessed-3.cfg.dot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
digraph "never-accessed-3" {
node [shape=box];
subgraph "cluster_FieldTest__init_40925" {
label="FieldTest__init_40925";
"FieldTest__init_40925_141" [label="self.f1 = sender()"];
subgraph "cluster_FieldTest__init_44003" {
label="FieldTest__init_44003";
"FieldTest__init_44003_141" [label="self.f1 = sender()"];
}
}
4 changes: 2 additions & 2 deletions test/contracts/never-accessed-3.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"name": "FieldTest",
"methods": [
{
"name": "FieldTest.init_39267",
"name": "FieldTest.init_42345",
"cfg": {
"nodes": [
{
"id": 141,
"stmtID": 39265,
"stmtID": 42343,
"srcEdges": [],
"dstEdges": []
}
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/never-accessed-4.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"nodes": [
{
"id": 141,
"stmtID": 42601,
"stmtID": 45679,
"srcEdges": [],
"dstEdges": []
}
Expand Down
12 changes: 6 additions & 6 deletions test/contracts/readonly-1.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"nodes": [
{
"id": 141,
"stmtID": 45935,
"stmtID": 49013,
"srcEdges": [],
"dstEdges": [
143
]
},
{
"id": 142,
"stmtID": 45939,
"stmtID": 49017,
"srcEdges": [
143
],
Expand All @@ -25,7 +25,7 @@
},
{
"id": 144,
"stmtID": 45943,
"stmtID": 49021,
"srcEdges": [
145
],
Expand All @@ -35,7 +35,7 @@
},
{
"id": 146,
"stmtID": 45950,
"stmtID": 49028,
"srcEdges": [
147
],
Expand All @@ -45,7 +45,7 @@
},
{
"id": 148,
"stmtID": 45949,
"stmtID": 49027,
"srcEdges": [
149
],
Expand All @@ -55,7 +55,7 @@
},
{
"id": 150,
"stmtID": 45952,
"stmtID": 49030,
"srcEdges": [
151
],
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/readonly-2.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"nodes": [
{
"id": 141,
"stmtID": 49035,
"stmtID": 52113,
"srcEdges": [],
"dstEdges": [
143
]
},
{
"id": 142,
"stmtID": 49039,
"stmtID": 52117,
"srcEdges": [
143
],
Expand All @@ -25,7 +25,7 @@
},
{
"id": 144,
"stmtID": 49041,
"stmtID": 52119,
"srcEdges": [
145
],
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/traits-1.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"nodes": [
{
"id": 141,
"stmtID": 52120,
"stmtID": 55198,
"srcEdges": [],
"dstEdges": []
}
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/unbound-loop-1.cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"nodes": [
{
"id": 141,
"stmtID": 55687,
"stmtID": 58765,
"srcEdges": [],
"dstEdges": [
143
]
},
{
"id": 142,
"stmtID": 55691,
"stmtID": 58769,
"srcEdges": [
143,
144
Expand All @@ -27,7 +27,7 @@
},
{
"id": 145,
"stmtID": 55693,
"stmtID": 58771,
"srcEdges": [
146
],
Expand Down
Loading

0 comments on commit 9385648

Please sign in to comment.