Struct SymbolTable
pub struct SymbolTable { /* private fields */ }Expand description
The Symbol Table records metadata about identifiers.
The parser and/or sema stage can query the Symbol Table and emit diagnostics such as when a variable, function or type alias is incorrectly redeclared.
In addition, it helps us solve the “typedef-name: identifier” problem when there is grammar ambiguity between a type alias and a variable identifier. One example is:
typedef int AA;
AA aa; // Declare variable 'aa' of type AA (alias of int).
float AA; // Declare variable 'AA' of type float.Implementations§
§impl SymbolTable
impl SymbolTable
pub fn new() -> Self
pub fn new() -> Self
Creates a new symbol table.
pub fn add<T: AsRef<AstUniqueName>>(
&mut self,
unique_name: T,
data_type: AstType,
attrs: SymbolAttributes,
) -> Result<(), &Symbol>
pub fn add<T: AsRef<AstUniqueName>>( &mut self, unique_name: T, data_type: AstType, attrs: SymbolAttributes, ) -> Result<(), &Symbol>
Adds a symbol to the table.
Returns Ok if the symbol was added, or Err(&Symbol) with the existing symbol if a symbol already exists
for the given unique_name.
pub fn set_definition<T: AsRef<AstUniqueName>>(
&mut self,
unique_name: T,
definition: Definition,
) -> Result<(), SetDefinitionError>
pub fn set_definition<T: AsRef<AstUniqueName>>( &mut self, unique_name: T, definition: Definition, ) -> Result<(), SetDefinitionError>
Sets that a symbol is defined.
A symbol’s definition can be changed from undefined to either Definition::Tentative or Definition::Defined,
but it’s not possible to change a defined symbol back to undefined, or to change a defined symbol to tentative,
or vice versa.
Returns Ok if a symbol exists for the given unique_name and if its previous definition was undefined, or
Err otherwise.
pub fn get<T: AsRef<AstUniqueName>>(&self, unique_name: T) -> Option<&Symbol>
pub fn get<T: AsRef<AstUniqueName>>(&self, unique_name: T) -> Option<&Symbol>
Returns the symbol for the given unique identifier name, or returns None.
pub fn set_symbol_used(&mut self, unique_name: &AstUniqueName)
pub fn set_symbol_used(&mut self, unique_name: &AstUniqueName)
Sets that the symbol has been used.
pub fn get_unused_symbols(&self) -> Vec<(String, SymbolKind, SourceLocation)>
pub fn get_unused_symbols(&self) -> Vec<(String, SymbolKind, SourceLocation)>
Returns a vector of all unused variable and function symbols, excluding ones with external linkage, and unused local typedefs. (Do not warn for unused file-scope typedefs, since they could come from included headers.)