@@ -929,6 +929,45 @@ impl<W: Write> Writer<W> {
929929 Ok ( ( ) )
930930 }
931931
932+ /// Writes the local variables of the given function.
933+ fn put_locals ( & mut self , context : & ExpressionContext ) -> BackendResult {
934+ for ( name_key, ty, init) in context
935+ . function
936+ . local_variables
937+ . iter ( )
938+ . map ( |( local_handle, local) | {
939+ let name_key = NameKey :: local ( context. origin , local_handle) ;
940+ ( name_key, local. ty , local. init )
941+ } )
942+ {
943+ let ty_name = TypeContext {
944+ handle : ty,
945+ gctx : context. module . to_ctx ( ) ,
946+ names : & self . names ,
947+ access : crate :: StorageAccess :: empty ( ) ,
948+ first_time : false ,
949+ } ;
950+ write ! (
951+ self . out,
952+ "{}{} {}" ,
953+ back:: INDENT ,
954+ ty_name,
955+ self . names[ & name_key]
956+ ) ?;
957+ match init {
958+ Some ( value) => {
959+ write ! ( self . out, " = " ) ?;
960+ self . put_expression ( value, context, true ) ?;
961+ }
962+ None => {
963+ write ! ( self . out, " = {{}}" ) ?;
964+ }
965+ } ;
966+ writeln ! ( self . out, ";" ) ?;
967+ }
968+ Ok ( ( ) )
969+ }
970+
932971 fn put_level_of_detail (
933972 & mut self ,
934973 level : LevelOfDetail ,
@@ -5683,28 +5722,7 @@ template <typename A>
56835722 result_struct : None ,
56845723 } ;
56855724
5686- for ( local_handle, local) in fun. local_variables . iter ( ) {
5687- let ty_name = TypeContext {
5688- handle : local. ty ,
5689- gctx : module. to_ctx ( ) ,
5690- names : & self . names ,
5691- access : crate :: StorageAccess :: empty ( ) ,
5692- first_time : false ,
5693- } ;
5694- let local_name = & self . names [ & NameKey :: FunctionLocal ( fun_handle, local_handle) ] ;
5695- write ! ( self . out, "{}{} {}" , back:: INDENT , ty_name, local_name) ?;
5696- match local. init {
5697- Some ( value) => {
5698- write ! ( self . out, " = " ) ?;
5699- self . put_expression ( value, & context. expression , true ) ?;
5700- }
5701- None => {
5702- write ! ( self . out, " = {{}}" ) ?;
5703- }
5704- } ;
5705- writeln ! ( self . out, ";" ) ?;
5706- }
5707-
5725+ self . put_locals ( & context. expression ) ?;
57085726 self . update_expressions_to_bake ( fun, fun_info, & context. expression ) ;
57095727 self . put_block ( back:: Level ( 1 ) , & fun. body , & context) ?;
57105728 writeln ! ( self . out, "}}" ) ?;
@@ -6616,28 +6634,7 @@ template <typename A>
66166634
66176635 // Finally, declare all the local variables that we need
66186636 //TODO: we can postpone this till the relevant expressions are emitted
6619- for ( local_handle, local) in fun. local_variables . iter ( ) {
6620- let name = & self . names [ & NameKey :: EntryPointLocal ( ep_index as _ , local_handle) ] ;
6621- let ty_name = TypeContext {
6622- handle : local. ty ,
6623- gctx : module. to_ctx ( ) ,
6624- names : & self . names ,
6625- access : crate :: StorageAccess :: empty ( ) ,
6626- first_time : false ,
6627- } ;
6628- write ! ( self . out, "{}{} {}" , back:: INDENT , ty_name, name) ?;
6629- match local. init {
6630- Some ( value) => {
6631- write ! ( self . out, " = " ) ?;
6632- self . put_expression ( value, & context. expression , true ) ?;
6633- }
6634- None => {
6635- write ! ( self . out, " = {{}}" ) ?;
6636- }
6637- } ;
6638- writeln ! ( self . out, ";" ) ?;
6639- }
6640-
6637+ self . put_locals ( & context. expression ) ?;
66416638 self . update_expressions_to_bake ( fun, fun_info, & context. expression ) ;
66426639 self . put_block ( back:: Level ( 1 ) , & fun. body , & context) ?;
66436640 writeln ! ( self . out, "}}" ) ?;
0 commit comments