@@ -26,18 +26,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2626 bytes_in_context ( self , bytes)
2727 }
2828
29- fn const_cstr ( & self , symbol : Symbol , _null_terminated : bool ) -> LValue < ' gcc > {
30- // TODO(antoyo): handle null_terminated.
31- if let Some ( & value) = self . const_cstr_cache . borrow ( ) . get ( & symbol) {
32- return value;
33- }
34-
35- let global = self . global_string ( symbol. as_str ( ) ) ;
36-
37- self . const_cstr_cache . borrow_mut ( ) . insert ( symbol, global) ;
38- global
39- }
40-
4129 fn global_string ( & self , string : & str ) -> LValue < ' gcc > {
4230 // TODO(antoyo): handle non-null-terminated strings.
4331 let string = self . context . new_string_literal ( & * string) ;
@@ -171,8 +159,12 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
171159 }
172160
173161 fn const_str ( & self , s : Symbol ) -> ( RValue < ' gcc > , RValue < ' gcc > ) {
174- let len = s. as_str ( ) . len ( ) ;
175- let cs = self . const_ptrcast ( self . const_cstr ( s, false ) . get_address ( None ) ,
162+ let s_str = s. as_str ( ) ;
163+ let str_global = * self . const_str_cache . borrow_mut ( ) . entry ( s) . or_insert_with ( || {
164+ self . global_string ( s_str)
165+ } ) ;
166+ let len = s_str. len ( ) ;
167+ let cs = self . const_ptrcast ( str_global. get_address ( None ) ,
176168 self . type_ptr_to ( self . layout_of ( self . tcx . types . str_ ) . gcc_type ( self , true ) ) ,
177169 ) ;
178170 ( cs, self . const_usize ( len as u64 ) )
0 commit comments