@@ -90,6 +90,7 @@ pub struct Namer {
9090 /// The last numeric suffix used for each base name. Zero means "no suffix".
9191 unique : FastHashMap < String , u32 > ,
9292 keywords : & ' static KeywordSet ,
93+ builtin_identifiers : & ' static KeywordSet ,
9394 keywords_case_insensitive : & ' static CaseInsensitiveKeywordSet ,
9495 reserved_prefixes : Vec < & ' static str > ,
9596}
@@ -173,6 +174,13 @@ impl Namer {
173174 /// Guarantee uniqueness by applying a numeric suffix when necessary. If `label_raw`
174175 /// itself ends with digits, separate them from the suffix with an underscore.
175176 pub fn call ( & mut self , label_raw : & str ) -> String {
177+ self . call_impl ( label_raw, false )
178+ }
179+
180+ /// See documentation of [`Self::call`]. Additionally this function allows ignoring
181+ /// `Namer::builtin_identifiers` mainly for [`NameKey::StructMember`] since struct members
182+ /// can't shadow builtin identifiers.
183+ pub fn call_impl ( & mut self , label_raw : & str , ignore_builtin_identifiers : bool ) -> String {
176184 use core:: fmt:: Write as _; // for write!-ing to Strings
177185
178186 let base = self . sanitize ( label_raw) ;
@@ -196,6 +204,8 @@ impl Namer {
196204 if base. ends_with ( char:: is_numeric)
197205 || self . keywords . contains ( base. as_ref ( ) )
198206 || self . keywords_case_insensitive . contains ( base. as_ref ( ) )
207+ || ( !ignore_builtin_identifiers
208+ && self . builtin_identifiers . contains ( base. as_ref ( ) ) )
199209 {
200210 suffixed. push ( SEPARATOR ) ;
201211 }
@@ -209,10 +219,22 @@ impl Namer {
209219 }
210220
211221 pub fn call_or ( & mut self , label : & Option < String > , fallback : & str ) -> String {
212- self . call ( match * label {
213- Some ( ref name) => name,
214- None => fallback,
215- } )
222+ self . call_or_impl ( label, fallback, false )
223+ }
224+
225+ fn call_or_impl (
226+ & mut self ,
227+ label : & Option < String > ,
228+ fallback : & str ,
229+ ignore_builtin_identifiers : bool ,
230+ ) -> String {
231+ self . call_impl (
232+ match * label {
233+ Some ( ref name) => name,
234+ None => fallback,
235+ } ,
236+ ignore_builtin_identifiers,
237+ )
216238 }
217239
218240 /// Enter a local namespace for things like structs.
@@ -231,6 +253,7 @@ impl Namer {
231253 & mut self ,
232254 module : & crate :: Module ,
233255 reserved_keywords : & ' static KeywordSet ,
256+ builtin_identifiers : & ' static KeywordSet ,
234257 reserved_keywords_case_insensitive : & ' static CaseInsensitiveKeywordSet ,
235258 reserved_prefixes : & [ & ' static str ] ,
236259 output : & mut FastHashMap < NameKey , String > ,
@@ -240,6 +263,7 @@ impl Namer {
240263
241264 self . unique . clear ( ) ;
242265 self . keywords = reserved_keywords;
266+ self . builtin_identifiers = builtin_identifiers;
243267 self . keywords_case_insensitive = reserved_keywords_case_insensitive;
244268
245269 // Choose fallback names for anonymous entry point return types.
@@ -281,7 +305,7 @@ impl Namer {
281305 // struct members have their own namespace, because access is always prefixed
282306 self . namespace ( members. len ( ) , |namer| {
283307 for ( index, member) in members. iter ( ) . enumerate ( ) {
284- let name = namer. call_or ( & member. name , "member" ) ;
308+ let name = namer. call_or_impl ( & member. name , "member" , true ) ;
285309 output. insert ( NameKey :: StructMember ( ty_handle, index as u32 ) , name) ;
286310 }
287311 } )
0 commit comments