naga/front/mod.rs
1/*!
2Frontend parsers that consume binary and text shaders and load them into [`Module`](super::Module)s.
3*/
4
5mod interpolator;
6mod type_gen;
7
8#[cfg(feature = "spv-in")]
9pub mod atomic_upgrade;
10#[cfg(feature = "glsl-in")]
11pub mod glsl;
12#[cfg(feature = "spv-in")]
13pub mod spv;
14#[cfg(feature = "wgsl-in")]
15pub mod wgsl;
16
17use alloc::{boxed::Box, vec, vec::Vec};
18use core::ops;
19
20use crate::{
21 arena::{Arena, Handle, HandleVec, UniqueArena},
22 proc::{ResolveContext, ResolveError, TypeResolution},
23 FastHashMap,
24};
25
26/// A table of types for an `Arena<Expression>`.
27///
28/// A front end can use a `Typifier` to get types for an arena's expressions
29/// while it is still contributing expressions to it. At any point, you can call
30/// [`typifier.grow(expr, arena, ctx)`], where `expr` is a `Handle<Expression>`
31/// referring to something in `arena`, and the `Typifier` will resolve the types
32/// of all the expressions up to and including `expr`. Then you can write
33/// `typifier[handle]` to get the type of any handle at or before `expr`.
34///
35/// Note that `Typifier` does *not* build an `Arena<Type>` as a part of its
36/// usual operation. Ideally, a module's type arena should only contain types
37/// actually needed by `Handle<Type>`s elsewhere in the module — functions,
38/// variables, [`Compose`] expressions, other types, and so on — so we don't
39/// want every little thing that occurs as the type of some intermediate
40/// expression to show up there.
41///
42/// Instead, `Typifier` accumulates a [`TypeResolution`] for each expression,
43/// which refers to the `Arena<Type>` in the [`ResolveContext`] passed to `grow`
44/// as needed. [`TypeResolution`] is a lightweight representation for
45/// intermediate types like this; see its documentation for details.
46///
47/// If you do need to register a `Typifier`'s conclusion in an `Arena<Type>`
48/// (say, for a [`LocalVariable`] whose type you've inferred), you can use
49/// [`register_type`] to do so.
50///
51/// [`typifier.grow(expr, arena)`]: Typifier::grow
52/// [`register_type`]: Typifier::register_type
53/// [`Compose`]: crate::Expression::Compose
54/// [`LocalVariable`]: crate::LocalVariable
55#[derive(Debug, Default)]
56pub struct Typifier {
57 resolutions: HandleVec<crate::Expression, TypeResolution>,
58}
59
60impl Typifier {
61 pub const fn new() -> Self {
62 Typifier {
63 resolutions: HandleVec::new(),
64 }
65 }
66
67 pub fn reset(&mut self) {
68 self.resolutions.clear()
69 }
70
71 pub fn get<'a>(
72 &'a self,
73 expr_handle: Handle<crate::Expression>,
74 types: &'a UniqueArena<crate::Type>,
75 ) -> &'a crate::TypeInner {
76 self.resolutions[expr_handle].inner_with(types)
77 }
78
79 /// Add an expression's type to an `Arena<Type>`.
80 ///
81 /// Add the type of `expr_handle` to `types`, and return a `Handle<Type>`
82 /// referring to it.
83 ///
84 /// # Note
85 ///
86 /// If you just need a [`TypeInner`] for `expr_handle`'s type, consider
87 /// using `typifier[expression].inner_with(types)` instead. Calling
88 /// [`TypeResolution::inner_with`] often lets us avoid adding anything to
89 /// the arena, which can significantly reduce the number of types that end
90 /// up in the final module.
91 ///
92 /// [`TypeInner`]: crate::TypeInner
93 pub fn register_type(
94 &self,
95 expr_handle: Handle<crate::Expression>,
96 types: &mut UniqueArena<crate::Type>,
97 ) -> Handle<crate::Type> {
98 match self[expr_handle].clone() {
99 TypeResolution::Handle(handle) => handle,
100 TypeResolution::Value(inner) => {
101 types.insert(crate::Type { name: None, inner }, crate::Span::UNDEFINED)
102 }
103 }
104 }
105
106 /// Grow this typifier until it contains a type for `expr_handle`.
107 pub fn grow(
108 &mut self,
109 expr_handle: Handle<crate::Expression>,
110 expressions: &Arena<crate::Expression>,
111 ctx: &ResolveContext,
112 ) -> Result<(), ResolveError> {
113 if self.resolutions.len() <= expr_handle.index() {
114 for (eh, expr) in expressions.iter().skip(self.resolutions.len()) {
115 //Note: the closure can't `Err` by construction
116 let resolution = ctx.resolve(expr, |h| Ok(&self.resolutions[h]))?;
117 log::debug!("Resolving {eh:?} = {expr:?} : {resolution:?}");
118 self.resolutions.insert(eh, resolution);
119 }
120 }
121 Ok(())
122 }
123
124 /// Recompute the type resolution for `expr_handle`.
125 ///
126 /// If the type of `expr_handle` hasn't yet been calculated, call
127 /// [`grow`](Self::grow) to ensure it is covered.
128 ///
129 /// In either case, when this returns, `self[expr_handle]` should be an
130 /// updated type resolution for `expr_handle`.
131 pub fn invalidate(
132 &mut self,
133 expr_handle: Handle<crate::Expression>,
134 expressions: &Arena<crate::Expression>,
135 ctx: &ResolveContext,
136 ) -> Result<(), ResolveError> {
137 if self.resolutions.len() <= expr_handle.index() {
138 self.grow(expr_handle, expressions, ctx)
139 } else {
140 let expr = &expressions[expr_handle];
141 //Note: the closure can't `Err` by construction
142 let resolution = ctx.resolve(expr, |h| Ok(&self.resolutions[h]))?;
143 self.resolutions[expr_handle] = resolution;
144 Ok(())
145 }
146 }
147}
148
149impl ops::Index<Handle<crate::Expression>> for Typifier {
150 type Output = TypeResolution;
151 fn index(&self, handle: Handle<crate::Expression>) -> &Self::Output {
152 &self.resolutions[handle]
153 }
154}
155
156/// Type representing a lexical scope, associating a name to a single variable
157///
158/// The scope is generic over the variable representation and name representation
159/// in order to allow larger flexibility on the frontends on how they might
160/// represent them.
161type Scope<Name, Var> = FastHashMap<Name, Var>;
162
163/// Structure responsible for managing variable lookups and keeping track of
164/// lexical scopes
165///
166/// The symbol table is generic over the variable representation and its name
167/// to allow larger flexibility on the frontends on how they might represent them.
168///
169/// ```
170/// use naga::front::SymbolTable;
171///
172/// // Create a new symbol table with `u32`s representing the variable
173/// let mut symbol_table: SymbolTable<&str, u32> = SymbolTable::default();
174///
175/// // Add two variables named `var1` and `var2` with 0 and 2 respectively
176/// symbol_table.add("var1", 0);
177/// symbol_table.add("var2", 2);
178///
179/// // Check that `var1` exists and is `0`
180/// assert_eq!(symbol_table.lookup("var1"), Some(&0));
181///
182/// // Push a new scope and add a variable to it named `var1` shadowing the
183/// // variable of our previous scope
184/// symbol_table.push_scope();
185/// symbol_table.add("var1", 1);
186///
187/// // Check that `var1` now points to the new value of `1` and `var2` still
188/// // exists with its value of `2`
189/// assert_eq!(symbol_table.lookup("var1"), Some(&1));
190/// assert_eq!(symbol_table.lookup("var2"), Some(&2));
191///
192/// // Pop the scope
193/// symbol_table.pop_scope();
194///
195/// // Check that `var1` now refers to our initial variable with value `0`
196/// assert_eq!(symbol_table.lookup("var1"), Some(&0));
197/// ```
198///
199/// Scopes are ordered as a LIFO stack so a variable defined in a later scope
200/// with the same name as another variable defined in a earlier scope will take
201/// precedence in the lookup. Scopes can be added with [`push_scope`] and
202/// removed with [`pop_scope`].
203///
204/// A root scope is added when the symbol table is created and must always be
205/// present. Trying to pop it will result in a panic.
206///
207/// Variables can be added with [`add`] and looked up with [`lookup`]. Adding a
208/// variable will do so in the currently active scope and as mentioned
209/// previously a lookup will search from the current scope to the root scope.
210///
211/// [`push_scope`]: Self::push_scope
212/// [`pop_scope`]: Self::push_scope
213/// [`add`]: Self::add
214/// [`lookup`]: Self::lookup
215pub struct SymbolTable<Name, Var> {
216 /// Stack of lexical scopes. Not all scopes are active; see [`cursor`].
217 ///
218 /// [`cursor`]: Self::cursor
219 scopes: Vec<Scope<Name, Var>>,
220 /// Limit of the [`scopes`] stack (exclusive). By using a separate value for
221 /// the stack length instead of `Vec`'s own internal length, the scopes can
222 /// be reused to cache memory allocations.
223 ///
224 /// [`scopes`]: Self::scopes
225 cursor: usize,
226}
227
228impl<Name, Var> SymbolTable<Name, Var> {
229 /// Adds a new lexical scope.
230 ///
231 /// All variables declared after this point will be added to this scope
232 /// until another scope is pushed or [`pop_scope`] is called, causing this
233 /// scope to be removed along with all variables added to it.
234 ///
235 /// [`pop_scope`]: Self::pop_scope
236 pub fn push_scope(&mut self) {
237 // If the cursor is equal to the scope's stack length then we need to
238 // push another empty scope. Otherwise we can reuse the already existing
239 // scope.
240 if self.scopes.len() == self.cursor {
241 self.scopes.push(FastHashMap::default())
242 } else {
243 self.scopes[self.cursor].clear();
244 }
245
246 self.cursor += 1;
247 }
248
249 /// Removes the current lexical scope and all its variables
250 ///
251 /// # PANICS
252 /// - If the current lexical scope is the root scope
253 pub fn pop_scope(&mut self) {
254 // Despite the method title, the variables are only deleted when the
255 // scope is reused. This is because while a clear is inevitable if the
256 // scope needs to be reused, there are cases where the scope might be
257 // popped and not reused, i.e. if another scope with the same nesting
258 // level is never pushed again.
259 assert!(self.cursor != 1, "Tried to pop the root scope");
260
261 self.cursor -= 1;
262 }
263}
264
265impl<Name, Var> SymbolTable<Name, Var>
266where
267 Name: core::hash::Hash + Eq,
268{
269 /// Perform a lookup for a variable named `name`.
270 ///
271 /// As stated in the struct level documentation the lookup will proceed from
272 /// the current scope to the root scope, returning `Some` when a variable is
273 /// found or `None` if there doesn't exist a variable with `name` in any
274 /// scope.
275 pub fn lookup<Q>(&self, name: &Q) -> Option<&Var>
276 where
277 Name: core::borrow::Borrow<Q>,
278 Q: core::hash::Hash + Eq + ?Sized,
279 {
280 // Iterate backwards through the scopes and try to find the variable
281 for scope in self.scopes[..self.cursor].iter().rev() {
282 if let Some(var) = scope.get(name) {
283 return Some(var);
284 }
285 }
286
287 None
288 }
289
290 /// Adds a new variable to the current scope.
291 ///
292 /// Returns the previous variable with the same name in this scope if it
293 /// exists, so that the frontend might handle it in case variable shadowing
294 /// is disallowed.
295 pub fn add(&mut self, name: Name, var: Var) -> Option<Var> {
296 self.scopes[self.cursor - 1].insert(name, var)
297 }
298
299 /// Adds a new variable to the root scope.
300 ///
301 /// This is used in GLSL for builtins which aren't known in advance and only
302 /// when used for the first time, so there must be a way to add those
303 /// declarations to the root unconditionally from the current scope.
304 ///
305 /// Returns the previous variable with the same name in the root scope if it
306 /// exists, so that the frontend might handle it in case variable shadowing
307 /// is disallowed.
308 pub fn add_root(&mut self, name: Name, var: Var) -> Option<Var> {
309 self.scopes[0].insert(name, var)
310 }
311}
312
313impl<Name, Var> Default for SymbolTable<Name, Var> {
314 /// Constructs a new symbol table with a root scope
315 fn default() -> Self {
316 Self {
317 scopes: vec![FastHashMap::default()],
318 cursor: 1,
319 }
320 }
321}
322
323use core::fmt;
324
325impl<Name: fmt::Debug, Var: fmt::Debug> fmt::Debug for SymbolTable<Name, Var> {
326 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327 f.write_str("SymbolTable ")?;
328 f.debug_list()
329 .entries(self.scopes[..self.cursor].iter())
330 .finish()
331 }
332}
333
334impl crate::Module {
335 pub fn get_or_insert_default_doc_comments(&mut self) -> &mut Box<crate::DocComments> {
336 self.doc_comments
337 .get_or_insert_with(|| Box::new(crate::DocComments::default()))
338 }
339}