From d4f00d676e9c27ba3ad7fdc0541205be64ecb7cb Mon Sep 17 00:00:00 2001 From: SpontanCombust <61706594+SpontanCombust@users.noreply.github.com> Date: Sun, 18 Aug 2024 00:28:11 +0200 Subject: [PATCH] rework: return concrete path type from symbols instead of opaque `&SymbolPath` --- crates/analysis/src/jobs/scan_symbols.rs | 20 +++---- .../src/jobs/workspace_symbol_analysis.rs | 2 +- .../src/symbol_analysis/symbol_table/iter.rs | 6 +-- .../symbol_analysis/symbol_table/marcher.rs | 6 +-- .../src/symbol_analysis/symbol_table/mod.rs | 20 +++---- .../symbols/annotated_symbols.rs | 25 ++++++--- .../symbols/array_type_symbol.rs | 13 +++-- .../symbol_analysis/symbols/class_symbol.rs | 9 ++-- .../symbol_analysis/symbols/enum_symbol.rs | 9 ++-- .../symbols/func_param_symbol.rs | 5 +- .../symbols/function_symbol.rs | 13 +++-- .../symbols/primitive_type_symbol.rs | 6 ++- .../symbol_analysis/symbols/state_symbol.rs | 4 +- .../symbol_analysis/symbols/struct_symbol.rs | 9 ++-- .../src/symbol_analysis/symbols/symbol.rs | 14 +++-- .../symbol_analysis/symbols/symbol_variant.rs | 2 +- .../src/symbol_analysis/symbols/var_symbol.rs | 32 ++++++++--- .../unqualified_name_lookup.rs | 54 +++++++++---------- .../src/utils/visitors/expr_evaluator.rs | 14 ++--- .../providers/common/position_resolving.rs | 2 +- crates/lsp/src/providers/document_symbols.rs | 4 +- crates/lsp/src/providers/goto.rs | 6 +-- 22 files changed, 167 insertions(+), 108 deletions(-) diff --git a/crates/analysis/src/jobs/scan_symbols.rs b/crates/analysis/src/jobs/scan_symbols.rs index 4a4403d0..4a67698e 100644 --- a/crates/analysis/src/jobs/scan_symbols.rs +++ b/crates/analysis/src/jobs/scan_symbols.rs @@ -371,7 +371,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { .filter_map(|s| StructSpecifier::try_from(s).ok()) .collect(); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); let constr_path = GlobalCallableSymbolPath::new(&struct_name); @@ -383,7 +383,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { }); constr_sym.parent_type_path = path; - self.current_constr_path = Some(constr_sym.path().to_owned()); + self.current_constr_path = Some(constr_sym.path_ref().to_owned()); self.symtab.insert_primary_symbol(constr_sym); traverse_definition = true; @@ -418,7 +418,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { label_range: name_node.range() }); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); traverse_definition = true; @@ -488,7 +488,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { if self.check_contains(&path, name_node.range(), SymbolType::MemberFunctionInjector) { let sym = MemberFunctionInjectorSymbol::new(self.parse_member_function(n, path)); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); traverse = true; @@ -500,7 +500,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { if self.check_contains(&path, name_node.range(), SymbolType::MemberFunctionReplacer) { let sym = MemberFunctionReplacerSymbol::new(self.parse_member_function(n, path)); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); traverse = true; @@ -510,7 +510,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { if self.check_contains(&path, name_node.range(), SymbolType::GlobalFunctionReplacer) { let sym = GlobalFunctionReplacerSymbol::new(self.parse_global_function(n, path)); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); traverse = true; @@ -524,7 +524,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { let wrapped_sym = WrappedMethodSymbol::new(&path); let sym = MemberFunctionWrapperSymbol::new(self.parse_member_function(n, path)); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); self.symtab.insert_symbol(wrapped_sym); @@ -538,7 +538,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { if self.check_contains(&path, name_node.range(), SymbolType::GlobalFunction) { let sym = self.parse_global_function(n, path); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_primary_symbol(sym); traverse = true; @@ -622,7 +622,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { if self.check_contains(&path, name_node.range(), SymbolType::MemberFunction) { let sym = self.parse_member_function(n, path); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_symbol(sym); traverse = true; @@ -660,7 +660,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> { label_range: name_node.range() }); - sym.path().clone_into(&mut self.current_path); + sym.path_ref().clone_into(&mut self.current_path); self.symtab.insert_symbol(sym); traverse = true; diff --git a/crates/analysis/src/jobs/workspace_symbol_analysis.rs b/crates/analysis/src/jobs/workspace_symbol_analysis.rs index 7ac0fe86..848646d2 100644 --- a/crates/analysis/src/jobs/workspace_symbol_analysis.rs +++ b/crates/analysis/src/jobs/workspace_symbol_analysis.rs @@ -15,7 +15,7 @@ pub fn workspace_symbol_analysis(target_symtab: &SymbolTable, marcher: SymbolTab .filter(|p| !matches!(p.typ(), SymbolType::MemberFunctionWrapper | SymbolType::MemberFunctionReplacer | SymbolType::GlobalFunctionReplacer)) .filter_map(|primary| { let primary_loc = primary.location().unwrap(); // primary symbols always have location - if let Err(err) = marcher.test_contains_symbol(primary.path()) { + if let Err(err) = marcher.test_contains_symbol(primary.path_ref()) { Some(LocatedDiagnostic { path: primary_loc.abs_source_path(), diagnostic: Diagnostic { diff --git a/crates/analysis/src/symbol_analysis/symbol_table/iter.rs b/crates/analysis/src/symbol_analysis/symbol_table/iter.rs index 146ed089..7de7221a 100644 --- a/crates/analysis/src/symbol_analysis/symbol_table/iter.rs +++ b/crates/analysis/src/symbol_analysis/symbol_table/iter.rs @@ -43,7 +43,7 @@ impl<'st, F> FilteredSymbolChildren<'st, F> where F: ChildrenSymbolsFilter<'st> { pub(super) fn new(symtab: &'st SymbolTable, symbol: &F) -> Self { Self { - iter: SymbolChildren::new(symtab, symbol.path()), + iter: SymbolChildren::new(symtab, symbol.path_ref()), filter_phantom: PhantomData } } @@ -312,8 +312,8 @@ impl<'st> Iterator for FileSymbols<'st> { // Their symbol paths can reference symbols from other source paths. // We have to skip over them and all of their children. if item.location().map(|loc| loc.local_source_path.as_ref() != self.local_source_path).unwrap_or(false) { - let injector_path = item.path().to_owned(); - self.iter.find(|v| !v.path().starts_with(&injector_path)) + let injector_path = item.path_ref().to_owned(); + self.iter.find(|v| !v.path_ref().starts_with(&injector_path)) } else { Some(item) } diff --git a/crates/analysis/src/symbol_analysis/symbol_table/marcher.rs b/crates/analysis/src/symbol_analysis/symbol_table/marcher.rs index b0823b02..018f5320 100644 --- a/crates/analysis/src/symbol_analysis/symbol_table/marcher.rs +++ b/crates/analysis/src/symbol_analysis/symbol_table/marcher.rs @@ -80,7 +80,7 @@ impl<'a> SymbolTableMarcher<'a> { #[inline] pub fn find_table_with_symbol(&self, symvar: &SymbolVariant) -> Option<&'a SymbolTable> { self.march(|masked| { - if masked.get_symbol(symvar.path()).filter(|v| v.location() == symvar.location()).is_some() { + if masked.get_symbol(symvar.path_ref()).filter(|v| v.location() == symvar.location()).is_some() { Some(masked.symtab) } else { None @@ -173,7 +173,7 @@ impl<'a> MaskedSymbolTable<'a> { fn test_contains_symbol(&self, path: &SymbolPath) -> Result<(), PathOccupiedError> { if let Some(occupying) = self.get_symbol(path) { Err(PathOccupiedError { - occupied_path: occupying.path().to_sympath_buf(), + occupied_path: occupying.path_ref().to_sympath_buf(), occupied_location: occupying.location().cloned(), occupied_typ: occupying.typ() }) @@ -288,7 +288,7 @@ impl<'a> Iterator for StateHierarchy<'a> { 'classes: for class in self.marcher.class_hierarchy(current_state_sym.parent_class_path()) { for state in self.marcher.class_states(class.path()) { if state.state_name() == base_state_name { - state.path().clone_into(&mut self.current_state_path); + state.path_ref().clone_into(&mut self.current_state_path); break 'classes; } } diff --git a/crates/analysis/src/symbol_analysis/symbol_table/mod.rs b/crates/analysis/src/symbol_analysis/symbol_table/mod.rs index 7635219a..c9b132bb 100644 --- a/crates/analysis/src/symbol_analysis/symbol_table/mod.rs +++ b/crates/analysis/src/symbol_analysis/symbol_table/mod.rs @@ -65,25 +65,25 @@ impl SymbolTable { pub(crate) fn insert_symbol(&mut self, sym: S) where S: Symbol + Into { - self.symbols.insert(sym.path().to_owned(), sym.into()); + self.symbols.insert(sym.path_ref().to_owned(), sym.into()); } pub(crate) fn insert_primary_symbol(&mut self, sym: S) where S: PrimarySymbol + LocatableSymbol + Into { self.source_path_assocs.entry(sym.location().local_source_path.clone()) .or_default() - .push(sym.path().to_owned()); + .push(sym.path_ref().to_owned()); - self.symbols.insert(sym.path().to_owned(), sym.into()); + self.symbols.insert(sym.path_ref().to_owned(), sym.into()); } pub(crate) fn insert_array_type_symbol(&mut self, sym: ArrayTypeSymbol, ref_local_source_path: &Path) { self.array_type_refs - .entry(sym.path().to_owned()) + .entry(sym.path_ref().to_owned()) .or_default() .insert(ref_local_source_path.to_owned()); - self.symbols.insert(sym.path().to_owned(), sym.into()); + self.symbols.insert(sym.path_ref().to_owned(), sym.into()); } @@ -97,7 +97,7 @@ impl SymbolTable { pub fn test_contains_symbol(&self, path: &SymbolPath) -> Result<(), PathOccupiedError> { if let Some(occupying) = self.symbols.get(path) { Err(PathOccupiedError { - occupied_path: occupying.path().to_sympath_buf(), + occupied_path: occupying.path_ref().to_sympath_buf(), occupied_location: occupying.location().cloned(), occupied_typ: occupying.typ() }) @@ -121,7 +121,7 @@ impl SymbolTable { pub fn remove_symbols_for_source(&mut self, local_source_path: &Path) { let for_removal: Vec<_> = self.get_symbols_for_source(local_source_path) - .map(|sym| sym.path().to_owned()) + .map(|sym| sym.path_ref().to_owned()) .collect(); for sympath in for_removal { @@ -143,7 +143,7 @@ impl SymbolTable { for (array_sympath, refs) in self.array_type_refs.iter() { if refs.is_empty() { for_removal.push(array_sympath.to_owned()); - for_removal.extend(self.get_symbol_descendants(&array_sympath).map(|v| v.path().to_owned())); + for_removal.extend(self.get_symbol_descendants(&array_sympath).map(|v| v.path_ref().to_owned())); } } @@ -212,13 +212,13 @@ impl SymbolTable { if let Some(occupying_variant) = self.symbols.get(&incoming_sympath) { incoming_sympath.clone_into(&mut sympath_to_skip); - if occupying_variant.is_array() || occupying_variant.path().has_missing() { + if occupying_variant.is_array() || occupying_variant.path_ref().has_missing() { continue; } if let Some(incoming_location) = incoming_variant.location().cloned() { errors.push(MergeConflictError { - occupied_path: occupying_variant.path().to_owned(), + occupied_path: occupying_variant.path_ref().to_owned(), occupied_typ: occupying_variant.typ(), occupied_location: occupying_variant.location().cloned(), incoming_typ: incoming_variant.typ(), diff --git a/crates/analysis/src/symbol_analysis/symbols/annotated_symbols.rs b/crates/analysis/src/symbol_analysis/symbols/annotated_symbols.rs index 49c1949e..d4a48a98 100644 --- a/crates/analysis/src/symbol_analysis/symbols/annotated_symbols.rs +++ b/crates/analysis/src/symbol_analysis/symbols/annotated_symbols.rs @@ -1,5 +1,4 @@ use witcherscript::ast::WRAPPED_METHOD_NAME; -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -10,11 +9,13 @@ pub struct MemberFunctionInjectorSymbol { } impl Symbol for MemberFunctionInjectorSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberFunctionInjector } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.backer.path() } } @@ -44,11 +45,13 @@ pub struct MemberFunctionReplacerSymbol { } impl Symbol for MemberFunctionReplacerSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberFunctionReplacer } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.backer.path() } } @@ -78,11 +81,13 @@ pub struct GlobalFunctionReplacerSymbol { } impl Symbol for GlobalFunctionReplacerSymbol { + type PathType = GlobalCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::GlobalFunctionReplacer } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.backer.path() } } @@ -112,11 +117,13 @@ pub struct MemberFunctionWrapperSymbol { } impl Symbol for MemberFunctionWrapperSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberFunctionWrapper } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.backer.path() } } @@ -146,11 +153,13 @@ pub struct WrappedMethodSymbol { } impl Symbol for WrappedMethodSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::WrappedMethod } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -177,11 +186,13 @@ pub struct MemberVarInjectorSymbol { } impl Symbol for MemberVarInjectorSymbol { + type PathType = MemberDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberVarInjector } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.backer.path() } } diff --git a/crates/analysis/src/symbol_analysis/symbols/array_type_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/array_type_symbol.rs index 27ff99eb..ed5d0574 100644 --- a/crates/analysis/src/symbol_analysis/symbols/array_type_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/array_type_symbol.rs @@ -1,4 +1,3 @@ -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -8,11 +7,13 @@ pub struct ArrayTypeSymbol { } impl Symbol for ArrayTypeSymbol { + type PathType = ArrayTypeSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Array } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -257,11 +258,13 @@ pub struct ArrayTypeFunctionSymbol { } impl Symbol for ArrayTypeFunctionSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberFunction } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -282,11 +285,13 @@ pub struct ArrayTypeFunctionParameterSymbol { } impl Symbol for ArrayTypeFunctionParameterSymbol { + type PathType = MemberDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Parameter } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/class_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/class_symbol.rs index fc2ef892..57b15192 100644 --- a/crates/analysis/src/symbol_analysis/symbols/class_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/class_symbol.rs @@ -1,5 +1,4 @@ use witcherscript::attribs::{ClassSpecifier, AutobindSpecifier}; -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -12,11 +11,13 @@ pub struct ClassSymbol { } impl Symbol for ClassSymbol { + type PathType = BasicTypeSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Class } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -57,11 +58,13 @@ pub struct AutobindSymbol { } impl Symbol for AutobindSymbol { + type PathType = MemberDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Autobind } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/enum_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/enum_symbol.rs index 2e4eb652..2152d2b5 100644 --- a/crates/analysis/src/symbol_analysis/symbols/enum_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/enum_symbol.rs @@ -1,4 +1,3 @@ -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -9,11 +8,13 @@ pub struct EnumSymbol { } impl Symbol for EnumSymbol { + type PathType = BasicTypeSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Enum } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -51,11 +52,13 @@ pub struct EnumVariantSymbol { } impl Symbol for EnumVariantSymbol { + type PathType = GlobalDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::EnumVariant } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/func_param_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/func_param_symbol.rs index ecaf240c..51eeef60 100644 --- a/crates/analysis/src/symbol_analysis/symbols/func_param_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/func_param_symbol.rs @@ -1,5 +1,4 @@ use witcherscript::attribs::FunctionParameterSpecifier; -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -13,11 +12,13 @@ pub struct FunctionParameterSymbol { } impl Symbol for FunctionParameterSymbol { + type PathType = MemberDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Parameter } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/function_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/function_symbol.rs index 960c5d29..a9dbf0e3 100644 --- a/crates/analysis/src/symbol_analysis/symbols/function_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/function_symbol.rs @@ -1,5 +1,4 @@ use witcherscript::attribs::*; -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -16,11 +15,13 @@ pub struct GlobalFunctionSymbol { } impl Symbol for GlobalFunctionSymbol { + type PathType = GlobalCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::GlobalFunction } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -63,11 +64,13 @@ pub struct MemberFunctionSymbol { } impl Symbol for MemberFunctionSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberFunction } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -103,11 +106,13 @@ pub struct EventSymbol { } impl Symbol for EventSymbol { + type PathType = MemberCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Event } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/primitive_type_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/primitive_type_symbol.rs index 7e629e6f..ae4ab3a9 100644 --- a/crates/analysis/src/symbol_analysis/symbols/primitive_type_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/primitive_type_symbol.rs @@ -1,4 +1,4 @@ -use crate::symbol_analysis::symbol_path::{SymbolPath, SymbolPathBuf}; +use crate::symbol_analysis::symbol_path::SymbolPathBuf; use super::*; @@ -12,11 +12,13 @@ pub struct PrimitiveTypeSymbol { } impl Symbol for PrimitiveTypeSymbol { + type PathType = SymbolPathBuf; + fn typ(&self) -> SymbolType { SymbolType::Type } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/state_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/state_symbol.rs index 70366760..1d7a4dcc 100644 --- a/crates/analysis/src/symbol_analysis/symbols/state_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/state_symbol.rs @@ -12,11 +12,13 @@ pub struct StateSymbol { } impl Symbol for StateSymbol { + type PathType = StateSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::State } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/struct_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/struct_symbol.rs index 95d2c3d3..d32c9079 100644 --- a/crates/analysis/src/symbol_analysis/symbols/struct_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/struct_symbol.rs @@ -1,5 +1,4 @@ use witcherscript::attribs::StructSpecifier; -use crate::symbol_analysis::symbol_path::SymbolPath; use super::*; @@ -11,11 +10,13 @@ pub struct StructSymbol { } impl Symbol for StructSymbol { + type PathType = BasicTypeSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Struct } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -50,11 +51,13 @@ pub struct ConstructorSymbol { } impl Symbol for ConstructorSymbol { + type PathType = GlobalCallableSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::Constructor } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/symbols/symbol.rs b/crates/analysis/src/symbol_analysis/symbols/symbol.rs index a334b913..7b1bfd44 100644 --- a/crates/analysis/src/symbol_analysis/symbols/symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/symbol.rs @@ -1,15 +1,23 @@ -use crate::symbol_analysis::symbol_path::SymbolPath; +use crate::symbol_analysis::symbol_path::{SymbolPath, SymbolPathBuf}; use super::SymbolLocation; pub trait Symbol { + type PathType: AsRef; + fn typ(&self) -> SymbolType; - fn path(&self) -> &SymbolPath; + fn path(&self) -> &Self::PathType; + + /// Shorthand of path().as_ref() for &SymbolPath reference + #[inline] + fn path_ref(&self) -> &SymbolPath { + self.path().as_ref() + } /// Returns name of the last path component. /// If path is empty returns empty string. fn name(&self) -> &str { - self.path().components().last().map(|c| c.name).unwrap_or("") + self.path().as_ref().components().last().map(|c| c.name).unwrap_or("") } } diff --git a/crates/analysis/src/symbol_analysis/symbols/symbol_variant.rs b/crates/analysis/src/symbol_analysis/symbols/symbol_variant.rs index 95d45bb2..48d7aef9 100644 --- a/crates/analysis/src/symbol_analysis/symbols/symbol_variant.rs +++ b/crates/analysis/src/symbol_analysis/symbols/symbol_variant.rs @@ -115,7 +115,7 @@ impl SymbolVariant { } } - pub fn path(&self) -> &SymbolPath { + pub fn path_ref(&self) -> &SymbolPath { match self { Self::Class(s) => s.path(), Self::State(s) => s.path(), diff --git a/crates/analysis/src/symbol_analysis/symbols/var_symbol.rs b/crates/analysis/src/symbol_analysis/symbols/var_symbol.rs index 2056f4fb..58eec3cd 100644 --- a/crates/analysis/src/symbol_analysis/symbols/var_symbol.rs +++ b/crates/analysis/src/symbol_analysis/symbols/var_symbol.rs @@ -13,11 +13,13 @@ pub struct MemberVarSymbol { } impl Symbol for MemberVarSymbol { + type PathType = MemberDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::MemberVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -54,11 +56,13 @@ pub struct LocalVarSymbol { } impl Symbol for LocalVarSymbol { + type PathType = MemberDataSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::LocalVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -92,11 +96,13 @@ pub struct GlobalVarSymbol { } impl Symbol for GlobalVarSymbol { + type PathType = SymbolPathBuf; + fn typ(&self) -> SymbolType { SymbolType::GlobalVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -128,11 +134,13 @@ pub struct ThisVarSymbol { } impl Symbol for ThisVarSymbol { + type PathType = ThisVarSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::ThisVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -162,11 +170,13 @@ pub struct SuperVarSymbol { } impl Symbol for SuperVarSymbol { + type PathType = SuperVarSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::SuperVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -206,11 +216,13 @@ pub struct StateSuperVarSymbol { } impl Symbol for StateSuperVarSymbol { + type PathType = SuperVarSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::SuperVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -236,11 +248,13 @@ pub struct ParentVarSymbol { } impl Symbol for ParentVarSymbol { + type PathType = ParentVarSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::ParentVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } @@ -270,11 +284,13 @@ pub struct VirtualParentVarSymbol { } impl Symbol for VirtualParentVarSymbol { + type PathType = VirtualParentVarSymbolPath; + fn typ(&self) -> SymbolType { SymbolType::VirtualParentVar } - fn path(&self) -> &SymbolPath { + fn path(&self) -> &Self::PathType { &self.path } } diff --git a/crates/analysis/src/symbol_analysis/unqualified_name_lookup.rs b/crates/analysis/src/symbol_analysis/unqualified_name_lookup.rs index ccb79db7..17e1d480 100644 --- a/crates/analysis/src/symbol_analysis/unqualified_name_lookup.rs +++ b/crates/analysis/src/symbol_analysis/unqualified_name_lookup.rs @@ -200,22 +200,22 @@ impl<'a> UnqualifiedNameLookupBuilder<'a> { for ch in class_symtab.get_symbol_children_filtered(class) { match ch { ClassSymbolChild::Var(s) => { - if class.path() == class_path || !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + if class.path_ref() == class_path || !s.specifiers.contains(AccessModifier::Private.into()) { + unl.insert(s.path_ref().to_owned()); } }, ClassSymbolChild::Autobind(s) => { - if class.path() == class_path || !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + if class.path_ref() == class_path || !s.specifiers.contains(AccessModifier::Private.into()) { + unl.insert(s.path_ref().to_owned()); } }, ClassSymbolChild::Method(s) => { - if class.path() == class_path || !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + if class.path_ref() == class_path || !s.specifiers.contains(AccessModifier::Private.into()) { + unl.insert(s.path_ref().to_owned()); } }, ClassSymbolChild::Event(s) => { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); }, // these are special reserved names, they cannot be overshadowed ClassSymbolChild::ThisVar(_) @@ -261,21 +261,21 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { match ch { ClassSymbolChild::Var(s) => { if !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } }, ClassSymbolChild::Autobind(s) => { if !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } }, ClassSymbolChild::Method(s) => { if !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } }, ClassSymbolChild::Event(s) => { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); }, // these are special reserved names, they cannot be overshadowed ClassSymbolChild::ThisVar(_) @@ -296,22 +296,22 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { for ch in state_symtab.get_symbol_children_filtered(state) { match ch { StateSymbolChild::Var(s) => { - if state.path() == &sympath_ctx.current_sympath || !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + if state.path_ref() == &sympath_ctx.current_sympath || !s.specifiers.contains(AccessModifier::Private.into()) { + unl.insert(s.path_ref().to_owned()); } }, StateSymbolChild::Autobind(s) => { - if state.path() == &sympath_ctx.current_sympath || !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + if state.path_ref() == &sympath_ctx.current_sympath || !s.specifiers.contains(AccessModifier::Private.into()) { + unl.insert(s.path_ref().to_owned()); } }, StateSymbolChild::Method(s) => { - if state.path() == &sympath_ctx.current_sympath || !s.specifiers.contains(AccessModifier::Private.into()) { - unl.insert(s.path().to_owned()); + if state.path_ref() == &sympath_ctx.current_sympath || !s.specifiers.contains(AccessModifier::Private.into()) { + unl.insert(s.path_ref().to_owned()); } }, StateSymbolChild::Event(s) => { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); }, // these are special reserved names, they cannot be overshadowed StateSymbolChild::ThisVar(_) @@ -339,7 +339,7 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { if let Some((struct_symtab, struct_symvar)) = self.symtab_marcher.get_symbol_with_table(&sympath_ctx.current_sympath) { if let Some(struct_sym) = struct_symvar.try_as_struct_ref() { for s in struct_symtab.get_symbol_children_filtered(struct_sym) { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } } } @@ -371,7 +371,7 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { if let Some(func) = func_symvar.try_as_global_func_ref() { for ch in func_symtab.get_symbol_children_filtered(func) { if let CallableSymbolChild::Param(s) = ch { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } // local vars will be pushed dynamically as a function will go on } @@ -382,7 +382,7 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { for ch in func_symtab.get_symbol_children_filtered(func) { if let CallableSymbolChild::Param(s) = ch { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } } } @@ -392,14 +392,14 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { for ch in func_symtab.get_symbol_children_filtered(func) { if let CallableSymbolChild::Param(s) = ch { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } } } else if let Some(func) = func_symvar.try_as_global_func_replacer_ref() { for ch in func_symtab.get_symbol_children_filtered(func) { if let CallableSymbolChild::Param(s) = ch { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } } } @@ -410,10 +410,10 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { for ch in func_symtab.get_symbol_children_filtered(func) { match ch { FunctionWrapperSymbolChild::Param(s) => { - unl.insert(s.path().to_owned()) + unl.insert(s.path_ref().to_owned()) }, FunctionWrapperSymbolChild::WrappedMethod(s) => { - unl.insert(s.path().to_owned()) + unl.insert(s.path_ref().to_owned()) }, _ => {} } @@ -439,7 +439,7 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { if let Some(func) = func_symvar.try_as_member_func_ref() { for ch in func_symtab.get_symbol_children_filtered(func) { if let CallableSymbolChild::Param(s) = ch { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } // local vars will be pushed dynamically as a function will go on } @@ -463,7 +463,7 @@ impl SyntaxNodeVisitor for UnqualifiedNameLookupBuilder<'_> { if let Some(event) = event_symvar.try_as_event_ref() { for ch in event_symtab.get_symbol_children_filtered(event) { if let CallableSymbolChild::Param(s) = ch { - unl.insert(s.path().to_owned()); + unl.insert(s.path_ref().to_owned()); } // local vars will be pushed dynamically as a function will go on } diff --git a/crates/analysis/src/utils/visitors/expr_evaluator.rs b/crates/analysis/src/utils/visitors/expr_evaluator.rs index 18329c1f..462b1e76 100644 --- a/crates/analysis/src/utils/visitors/expr_evaluator.rs +++ b/crates/analysis/src/utils/visitors/expr_evaluator.rs @@ -91,18 +91,18 @@ impl<'a> ExpressionEvaluator<'a> { fn produce_type(&self, path: &SymbolPath) -> SymbolPathBuf { if let Some(symvar) = self.symtab_marcher.get_symbol(path) { match symvar { - SymbolVariant::Class(s) => s.path().to_owned(), - SymbolVariant::State(s) => s.path().to_owned(), - SymbolVariant::Struct(s) => s.path().to_owned(), - SymbolVariant::Enum(s) => s.path().to_owned(), - SymbolVariant::Array(s) => s.path().to_owned(), + SymbolVariant::Class(s) => s.path_ref().to_owned(), + SymbolVariant::State(s) => s.path_ref().to_owned(), + SymbolVariant::Struct(s) => s.path_ref().to_owned(), + SymbolVariant::Enum(s) => s.path_ref().to_owned(), + SymbolVariant::Array(s) => s.path_ref().to_owned(), SymbolVariant::ArrayFunc(s) => s.return_type_path.clone().into(), SymbolVariant::ArrayFuncParam(s) => s.type_path.clone().into(), SymbolVariant::GlobalFunc(s) => s.return_type_path.clone().into(), SymbolVariant::MemberFunc(s) => s.return_type_path.clone().into(), SymbolVariant::Event(_) => BasicTypeSymbolPath::new("void").into(), // I guess?? SymbolVariant::Constructor(s) => s.parent_type_path.clone().into(), - SymbolVariant::Primitive(s) => s.path().to_owned(), + SymbolVariant::Primitive(s) => s.path_ref().to_owned(), SymbolVariant::EnumVariant(s) => s.parent_enum_path.clone().into(), SymbolVariant::FuncParam(s) => s.type_path.clone().into(), SymbolVariant::GlobalVar(s) => s.type_path().to_owned().into(), @@ -117,7 +117,7 @@ impl<'a> ExpressionEvaluator<'a> { self.symtab_marcher .state_hierarchy(state_path) .skip(1).next() - .map(|sym| sym.path().to_owned()) + .map(|sym| sym.path_ref().to_owned()) .unwrap_or(SymbolPathBuf::unknown(SymbolCategory::Type)) } else { BasicTypeSymbolPath::new(StateSymbol::DEFAULT_STATE_BASE_NAME).into() diff --git a/crates/lsp/src/providers/common/position_resolving.rs b/crates/lsp/src/providers/common/position_resolving.rs index 0a93114f..68b7749d 100644 --- a/crates/lsp/src/providers/common/position_resolving.rs +++ b/crates/lsp/src/providers/common/position_resolving.rs @@ -54,7 +54,7 @@ impl PositionTarget { let mut base_state_path = None; for state in symtab_marcher.state_hierarchy(target_state_sym.path()) { if state.state_name() == base_state_name { - base_state_path = Some(state.path().to_owned()); + base_state_path = Some(state.path_ref().to_owned()); break; } } diff --git a/crates/lsp/src/providers/document_symbols.rs b/crates/lsp/src/providers/document_symbols.rs index a1dd1aa6..13c44e92 100644 --- a/crates/lsp/src/providers/document_symbols.rs +++ b/crates/lsp/src/providers/document_symbols.rs @@ -57,13 +57,13 @@ impl Backend { for sym_variant in symtab_ref.get_symbols_for_source(content_info.source_tree_path.local()) { if let Some(doc_sym) = sym_variant.to_doc_sym() { if let Some(enum_sym) = sym_variant.try_as_enum_ref() { - doc_enums.insert(enum_sym.path().to_owned(), doc_sym); + doc_enums.insert(enum_sym.path_ref().to_owned(), doc_sym); } else if let Some(enum_variant_sym) = sym_variant.try_as_enum_variant_ref() { doc_enum_variants.push((enum_variant_sym.parent_enum_path.clone().into(), doc_sym)); } else { - let sympath = sym_variant.path(); + let sympath = sym_variant.path_ref(); // if the symbol is not primary, then we need to reduce the stack to a form // in which the top element if the parent symbol of the current if let Some(parent_sympath) = sympath.parent() { diff --git a/crates/lsp/src/providers/goto.rs b/crates/lsp/src/providers/goto.rs index 57a658b6..6784504a 100644 --- a/crates/lsp/src/providers/goto.rs +++ b/crates/lsp/src/providers/goto.rs @@ -95,7 +95,7 @@ impl Backend { // attempt to find the very first declaration of the callable // because normally you get the location of the last override of the function if symvar.is_member_func() || symvar.is_event() { - let func_path = symvar.path().to_owned(); + let func_path = symvar.path_ref().to_owned(); let mut parent_path = func_path.clone(); parent_path.pop(); @@ -132,7 +132,7 @@ impl Backend { } } else if symvar.is_global_func_replacer() || symvar.is_member_func_replacer() || symvar.is_member_func_wrapper() { - let sympath = symvar.path(); + let sympath = symvar.path_ref(); let symtabs = self.symtabs.read().await; let symtabs_marcher = self.march_symbol_tables(&symtabs, &content_path).await; @@ -258,7 +258,7 @@ impl Backend { symtabs_marcher .state_hierarchy(state_path) .skip(1).next() - .map(|sym| sym.path()) + .map(|sym| sym.path_ref()) } else { Some(default_state_base_path.as_sympath()) }