diff --git a/src/front/mod.rs b/src/front/mod.rs index 245839d3e..09836c676 100644 --- a/src/front/mod.rs +++ b/src/front/mod.rs @@ -254,7 +254,7 @@ where /// the current scope to the root scope, returning `Some` when a variable is /// found or `None` if there doesn't exist a variable with `name` in any /// scope. - pub fn lookup(&mut self, name: &Q) -> Option<&Var> + pub fn lookup(&self, name: &Q) -> Option<&Var> where Name: std::borrow::Borrow, Q: std::hash::Hash + Eq, @@ -277,6 +277,19 @@ where pub fn add(&mut self, name: Name, var: Var) -> Option { self.scopes[self.cursor - 1].insert(name, var) } + + /// Adds a new variable to the root scope. + /// + /// This is used in GLSL for builtins which aren't known in advance and only + /// when used for the first time, so there must be a way to add those + /// declarations to the root unconditionally from the current scope. + /// + /// Returns the previous variable with the same name in the root scope if it + /// exists, so that the frontend might handle it in case variable shadowing + /// is disallowed. + pub fn add_root(&mut self, name: Name, var: Var) -> Option { + self.scopes[0].insert(name, var) + } } impl Default for SymbolTable { @@ -288,3 +301,14 @@ impl Default for SymbolTable { } } } + +use std::fmt; + +impl fmt::Debug for SymbolTable { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("SymbolTable ")?; + f.debug_list() + .entries(self.scopes[..self.cursor].iter()) + .finish() + } +}