Cache reserved keywords (#7338)

This commit is contained in:
Connor Fitzgerald authored and GitHub committed 2025-03-18 14:26:45 -04:00
1 parent 5ac906645b
commit da7deb4381
10 files changed
+106 -16

No files matched your search

+19 -7
View File
@@ -5,6 +5,7 @@ use alloc::{
vec::Vec,
};
use core::hash::{Hash, Hasher};
use hashbrown::HashSet;
use crate::{arena::Handle, FastHashMap, FastHashSet};
@@ -27,15 +28,29 @@ pub enum NameKey {
/// This processor assigns names to all the things in a module
/// that may need identifiers in a textual backend.
#[derive(Default)]
pub struct Namer {
/// The last numeric suffix used for each base name. Zero means "no suffix".
unique: FastHashMap<String, u32>,
keywords: FastHashSet<&'static str>,
keywords: &'static HashSet<&'static str>,
keywords_case_insensitive: FastHashSet<AsciiUniCase<&'static str>>,
reserved_prefixes: Vec<&'static str>,
}
#[cfg(any(wgsl_out, glsl_out, msl_out, hlsl_out, test))]
impl Default for Namer {
fn default() -> Self {
use std::sync::LazyLock;
static DEFAULT_KEYWORDS: LazyLock<HashSet<&'static str>> = LazyLock::new(HashSet::default);
Self {
unique: Default::default(),
keywords: &DEFAULT_KEYWORDS,
keywords_case_insensitive: Default::default(),
reserved_prefixes: Default::default(),
}
}
}
impl Namer {
/// Return a form of `string` suitable for use as the base of an identifier.
///
@@ -157,8 +172,7 @@ impl Namer {
pub fn reset(
&mut self,
module: &crate::Module,
reserved_keywords: &[&'static str],
extra_reserved_keywords: &[&'static str],
reserved_keywords: &'static HashSet<&'static str>,
reserved_keywords_case_insensitive: &[&'static str],
reserved_prefixes: &[&'static str],
output: &mut FastHashMap<NameKey, String>,
@@ -167,9 +181,7 @@ impl Namer {
self.reserved_prefixes.extend(reserved_prefixes.iter());
self.unique.clear();
self.keywords.clear();
self.keywords.extend(reserved_keywords.iter());
self.keywords.extend(extra_reserved_keywords.iter());
self.keywords = reserved_keywords;
debug_assert!(reserved_keywords_case_insensitive
.iter()