ID/Stmt: Introduce INIT_SKIP and use in ForStmt

Currently, loop vars are added to a function scope's inits and
initialized upon entering a function with default values. This
applies to vector, record and table types.

This is unnecessary for variables used in for loops as they are
guaranteed to be initialized while iterating.
This commit is contained in:
Arne Welzel 2023-09-01 19:14:54 +02:00
parent aaa81cae5d
commit cea7c0ab46
7 changed files with 287 additions and 288 deletions

View file

@ -46,6 +46,7 @@ enum InitClass
INIT_FULL,
INIT_EXTRA,
INIT_REMOVE,
INIT_SKIP,
};
enum IDScope
{

View file

@ -1263,7 +1263,7 @@ ForStmt::ForStmt(IDPList* arg_loop_vars, ExprPtr loop_expr)
else
{
add_local({NewRef{}, lv}, ind_type, INIT_NONE, nullptr, nullptr, VAR_REGULAR);
add_local({NewRef{}, lv}, ind_type, INIT_SKIP, nullptr, nullptr, VAR_REGULAR);
}
}
}
@ -1284,7 +1284,7 @@ ForStmt::ForStmt(IDPList* arg_loop_vars, ExprPtr loop_expr)
// nop
}
else if ( ! t )
add_local({NewRef{}, lv}, base_type(TYPE_COUNT), INIT_NONE, nullptr, nullptr,
add_local({NewRef{}, lv}, base_type(TYPE_COUNT), INIT_SKIP, nullptr, nullptr,
VAR_REGULAR);
else if ( ! IsIntegral(t->Tag()) )
@ -1310,7 +1310,7 @@ ForStmt::ForStmt(IDPList* arg_loop_vars, ExprPtr loop_expr)
// nop
}
else if ( ! t )
add_local({NewRef{}, (*loop_vars)[0]}, base_type(TYPE_STRING), INIT_NONE, nullptr,
add_local({NewRef{}, (*loop_vars)[0]}, base_type(TYPE_STRING), INIT_SKIP, nullptr,
nullptr, VAR_REGULAR);
else if ( t->Tag() != TYPE_STRING )
@ -1353,7 +1353,7 @@ ForStmt::ForStmt(IDPList* arg_loop_vars, ExprPtr loop_expr, IDPtr val_var)
e->Error("type clash in iteration", value_var->GetType().get());
}
else
add_local(value_var, yield_type, INIT_NONE, nullptr, nullptr, VAR_REGULAR);
add_local(value_var, yield_type, INIT_SKIP, nullptr, nullptr, VAR_REGULAR);
}
ForStmt::~ForStmt()

View file

@ -420,7 +420,9 @@ StmtPtr add_local(IDPtr id, TypePtr t, InitClass c, ExprPtr init,
else
{
if ( c != INIT_SKIP )
current_scope()->AddInit(std::move(id));
return make_intrusive<NullStmt>();
}
}

View file

@ -1,14 +1,13 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0, done parsing
0, populating table, expecting 4 my_seq() invocations
0, my_seq() invoked
1, populating table, expecting 4 my_seq() invocations
1, my_seq() invoked
2, my_seq() invoked
3, my_seq() invoked
4, my_seq() invoked
5, iterating table, expecting no my_seq() invocations
5, it, [id=4], 3
5, it, [id=5], 4
5, it, [id=3], 2
5, it, [id=2], 1
5, done
4, iterating table, expecting no my_seq() invocations
4, it, [id=1], 1
4, it, [id=4], 4
4, it, [id=3], 3
4, it, [id=2], 2
4, done

View file

@ -1,19 +1,17 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0, done parsing
0, populating table, expecting 8 my_seq() invocations
0, my_seq() invoked
1, my_seq() invoked
2, populating table, expecting 8 my_seq() invocations
2, my_seq() invoked
3, my_seq() invoked
4, my_seq() invoked
5, my_seq() invoked
6, my_seq() invoked
7, my_seq() invoked
8, my_seq() invoked
9, my_seq() invoked
10, iterating table, expecting no my_seq() invocations
10, it, [id=8], [id=7]
10, it, [id=6], [id=5]
10, it, [id=4], [id=3]
10, it, [id=10], [id=9]
10, done
8, iterating table, expecting no my_seq() invocations
8, it, [id=8], [id=7]
8, it, [id=4], [id=3]
8, it, [id=6], [id=5]
8, it, [id=2], [id=1]
8, done

View file

@ -1,14 +1,13 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0, done parsing
0, populating vector, expecting 4 my_seq() invocations
0, my_seq() invoked
1, populating vector, expecting 4 my_seq() invocations
1, my_seq() invoked
2, my_seq() invoked
3, my_seq() invoked
4, my_seq() invoked
5, iterating vector, expecting no my_seq() invocations
5, it, 0, [id=2]
5, it, 1, [id=3]
5, it, 2, [id=4]
5, it, 3, [id=5]
5, done
4, iterating vector, expecting no my_seq() invocations
4, it, 0, [id=1]
4, it, 1, [id=2]
4, it, 2, [id=3]
4, it, 3, [id=4]
4, done

File diff suppressed because one or more lines are too long