Skip to content

Commit

Permalink
Merge pull request #1184 from veryl-lang/var_ref_include
Browse files Browse the repository at this point in the history
Improve VarRefPath::included performance
  • Loading branch information
dalance authored Jan 5, 2025
2 parents a498465 + dae03ec commit a4b37e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/analyzer/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a> AnalyzerPass3<'a> {
if symbol.token.source == self.path {
assignable_list.append(&mut traverse_assignable_symbol(
symbol.id,
&VarRefPath::new((&symbol.id).into()),
&VarRefPath::new(vec![(&symbol.id).into()]),
));
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/analyzer/src/handlers/check_var_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl VerylGrammarTrait for CheckVarRef<'_> {
let full_path = path.full_path();
let symbol = symbol_table::get(*full_path.last().unwrap()).unwrap();

if can_assign(&full_path) {
if can_assign(full_path) {
self.assign_position.push(AssignPositionType::Statement {
token,
resettable: true,
Expand Down Expand Up @@ -620,7 +620,7 @@ impl VerylGrammarTrait for CheckVarRef<'_> {
if let HandlerPoint::After = self.point {
if let Ok(path) = VarRefPath::try_from(arg.hierarchical_identifier.as_ref()) {
let full_path = path.full_path();
if can_assign(&full_path) {
if can_assign(full_path) {
self.assign_position.push(AssignPositionType::Declaration {
token: arg.assign.assign_token.token,
r#type: AssignDeclarationType::Assign,
Expand Down
43 changes: 25 additions & 18 deletions crates/analyzer/src/var_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,34 @@ impl fmt::Display for VarRefPathItem {
}

#[derive(Clone, Debug)]
pub struct VarRefPath(pub Vec<VarRefPathItem>);
pub struct VarRefPath(Vec<VarRefPathItem>, Vec<SymbolId>);

impl VarRefPath {
pub fn new(x: VarRefPathItem) -> Self {
Self(vec![x])
pub fn new(x: Vec<VarRefPathItem>) -> Self {
let mut full_path = Vec::new();

for path in &x {
if let VarRefPathItem::Identifier { symbol_id } = path {
full_path.push(*symbol_id);
}
}

Self(x, full_path)
}

pub fn push(&mut self, x: VarRefPathItem) {
if let VarRefPathItem::Identifier { symbol_id } = x {
self.1.push(symbol_id);
}
self.0.push(x)
}

pub fn pop(&mut self) -> Option<VarRefPathItem> {
self.0.pop()
let poped = self.0.pop();
if let Some(VarRefPathItem::Identifier { .. }) = poped {
self.1.pop();
}
poped
}

pub fn included(&self, x: &VarRefPath) -> bool {
Expand Down Expand Up @@ -303,16 +318,8 @@ impl VarRefPath {
true
}

pub fn full_path(&self) -> Vec<SymbolId> {
let mut ret = Vec::new();

for path in &self.0 {
if let VarRefPathItem::Identifier { symbol_id } = path {
ret.push(*symbol_id);
}
}

ret
pub fn full_path(&self) -> &[SymbolId] {
&self.1
}

pub fn is_partial(&self) -> bool {
Expand Down Expand Up @@ -342,7 +349,7 @@ impl TryFrom<&Identifier> for VarRefPath {
fn try_from(arg: &Identifier) -> Result<Self, Self::Error> {
if let Ok(symbol) = symbol_table::resolve(arg) {
let path_items = symbol.full_path.iter().map(VarRefPathItem::from).collect();
Ok(VarRefPath(path_items))
Ok(VarRefPath::new(path_items))
} else {
Err(())
}
Expand Down Expand Up @@ -378,7 +385,7 @@ impl TryFrom<&HierarchicalIdentifier> for VarRefPath {
}
}

Ok(VarRefPath(path_items))
Ok(VarRefPath::new(path_items))
}
}

Expand Down Expand Up @@ -415,7 +422,7 @@ impl TryFrom<&ExpressionIdentifier> for VarRefPath {
}
}

Ok(VarRefPath(path_items))
Ok(VarRefPath::new(path_items))
}
}

Expand All @@ -442,7 +449,7 @@ impl TryFrom<(&ConnectTarget, &Namespace)> for VarRefPath {
path_items.push(VarRefPathItem::from(select));
}
}
Ok(VarRefPath(path_items))
Ok(VarRefPath::new(path_items))
} else {
Err(())
}
Expand Down

0 comments on commit a4b37e3

Please sign in to comment.