Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filelist order when file scope import is used #614

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions crates/analyzer/src/handlers/create_type_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::{
use veryl_parser::{
resource_table,
veryl_grammar_trait::{
EnumDeclaration, InterfaceDeclaration, ModuleDeclaration, PackageDeclaration,
ScopedIdentifier, StructUnion, StructUnionDeclaration, TypeDefDeclaration,
VerylGrammarTrait,
DescriptionItem, EnumDeclaration, InterfaceDeclaration, ModuleDeclaration,
PackageDeclaration, ScopedIdentifier, StructUnion, StructUnionDeclaration,
TypeDefDeclaration, Veryl, VerylGrammarTrait,
},
veryl_token::Token,
ParolError,
Expand All @@ -25,6 +25,7 @@ pub struct CreateTypeDag<'a> {
parent: Vec<u32>,
point: HandlerPoint,
ctx: Vec<Context>,
file_scope_import: Vec<Option<u32>>,
}

impl<'a> CreateTypeDag<'a> {
Expand Down Expand Up @@ -88,13 +89,6 @@ impl<'a> Handler for CreateTypeDag<'a> {
}

impl<'a> VerylGrammarTrait for CreateTypeDag<'a> {
fn veryl(&mut self, _arg: &veryl_parser::veryl_grammar_trait::Veryl) -> Result<(), ParolError> {
if let HandlerPoint::After = self.point {
// Evaluate DAG
}
Ok(())
}

fn struct_union_declaration(&mut self, arg: &StructUnionDeclaration) -> Result<(), ParolError> {
match self.point {
HandlerPoint::Before => {
Expand Down Expand Up @@ -182,6 +176,12 @@ impl<'a> VerylGrammarTrait for CreateTypeDag<'a> {
self.parent.push(x)
}
self.ctx.push(Context::Module);

for child in &self.file_scope_import.clone() {
if let (Some(parent), Some(child)) = (self.parent.last(), child) {
self.insert_edge(*parent, *child, *self.ctx.last().unwrap());
}
}
}
HandlerPoint::After => {
self.parent.pop();
Expand All @@ -201,6 +201,12 @@ impl<'a> VerylGrammarTrait for CreateTypeDag<'a> {
self.parent.push(x)
}
self.ctx.push(Context::Interface);

for child in &self.file_scope_import.clone() {
if let (Some(parent), Some(child)) = (self.parent.last(), child) {
self.insert_edge(*parent, *child, *self.ctx.last().unwrap());
}
}
}
HandlerPoint::After => {
self.parent.pop();
Expand All @@ -220,6 +226,12 @@ impl<'a> VerylGrammarTrait for CreateTypeDag<'a> {
self.parent.push(x)
}
self.ctx.push(Context::Package);

for child in &self.file_scope_import.clone() {
if let (Some(parent), Some(child)) = (self.parent.last(), child) {
self.insert_edge(*parent, *child, *self.ctx.last().unwrap());
}
}
}
HandlerPoint::After => {
self.parent.pop();
Expand All @@ -228,6 +240,26 @@ impl<'a> VerylGrammarTrait for CreateTypeDag<'a> {
}
Ok(())
}

fn veryl(&mut self, arg: &Veryl) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
for x in &arg.veryl_list {
let items: Vec<DescriptionItem> = x.description_group.as_ref().into();
for item in items {
if let DescriptionItem::ImportDeclaration(x) = item {
let x = &x.import_declaration.scoped_identifier;

let path: SymbolPathNamespace = x.as_ref().into();
let name = to_string(x);
let token = x.identifier.identifier_token.token;
let child = self.insert_node(&path, &name, &token);
self.file_scope_import.push(child);
}
}
}
}
Ok(())
}
}

fn to_string(sid: &ScopedIdentifier) -> String {
Expand Down
14 changes: 10 additions & 4 deletions crates/veryl/src/cmd_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::time::Instant;
use veryl_analyzer::symbol::SymbolKind;
use veryl_analyzer::{type_dag, Analyzer};
use veryl_emitter::Emitter;
use veryl_metadata::{FilelistType, Metadata, PathPair};
Expand Down Expand Up @@ -126,10 +127,15 @@ impl CmdBuild {
let mut ret = vec![];
let sorted_symbols = type_dag::toposort();
for symbol in sorted_symbols {
if let TokenSource::File(x) = symbol.token.source {
let path = PathBuf::from(format!("{}", x));
if let Some(x) = table.remove(&path) {
ret.push(x.clone());
if matches!(
symbol.kind,
SymbolKind::Module(_) | SymbolKind::Interface(_) | SymbolKind::Package(_)
) {
if let TokenSource::File(x) = symbol.token.source {
let path = PathBuf::from(format!("{}", x));
if let Some(x) = table.remove(&path) {
ret.push(x.clone());
}
}
}
}
Expand Down
Loading