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 namespace path emit #459

Merged
merged 3 commits into from
Jan 4, 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
55 changes: 43 additions & 12 deletions crates/emitter/src/emitter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::aligner::{Aligner, Location};
use veryl_analyzer::namespace::Namespace;
use veryl_analyzer::symbol::SymbolKind;
use veryl_analyzer::symbol_table;
use veryl_analyzer::symbol_table::{self, SymbolPath};
use veryl_analyzer::{msb_table, namespace_table};
use veryl_metadata::{Build, BuiltinType, ClockType, Format, Metadata, ResetType};
use veryl_parser::resource_table;
Expand Down Expand Up @@ -284,13 +285,43 @@ impl Emitter {
.contains(&BuiltinType::Type)
}

fn namespace(&mut self, namespace: &Namespace) {
let mut ret = String::from("");
let mut resolve_namespace = Namespace::new();
for (i, path) in namespace.paths.iter().enumerate() {
if i > 0 {
let symbol_path = SymbolPath::new(&[*path]);
if let Ok(ref symbol) = symbol_table::get(&symbol_path, &resolve_namespace) {
if let Some(ref symbol) = symbol.found {
let separator = match symbol.kind {
SymbolKind::Package => "::",
SymbolKind::Interface(_) => ".",
_ => "_",
};
ret.push_str(&format!("{}{}", path, separator));
} else {
return self.str(&format!("{}", namespace));
}
} else {
return self.str(&format!("{}", namespace));
}
} else {
// top level namespace is always `_`
ret.push_str(&format!("{}_", path));
}

resolve_namespace.push(*path);
}
self.str(&ret);
}

fn path_identifier(&mut self, arg: &[Identifier]) {
if let Ok(ref symbol) = symbol_table::resolve(arg) {
if let Some(ref symbol) = symbol.found {
match symbol.kind {
SymbolKind::Module(_) | SymbolKind::Interface(_) | SymbolKind::Package => {
self.str(&format!("{}", symbol.namespace).replace("::", "_"));
self.str(&format!("_{}", symbol.token.text));
self.namespace(&symbol.namespace);
self.str(&format!("{}", symbol.token.text));
}
SymbolKind::Parameter(_)
| SymbolKind::Function(_)
Expand All @@ -299,25 +330,25 @@ impl Emitter {
| SymbolKind::TypeDef(_)
| SymbolKind::Enum(_) => {
if arg.len() > 1 {
self.str(&format!("{}", symbol.namespace).replace("::", "_"));
self.str(&format!("::{}", symbol.token.text));
self.namespace(&symbol.namespace);
self.str(&format!("{}", symbol.token.text));
} else {
self.identifier(&arg[0]);
}
}
SymbolKind::EnumMember(_) => {
if arg.len() > 2 {
self.str(&format!("{}", symbol.namespace).replace("::", "_"));
self.str(&format!("_{}", symbol.token.text));
self.namespace(&symbol.namespace);
self.str(&format!("{}", symbol.token.text));
} else {
self.identifier(&arg[0]);
self.str("_");
self.identifier(&arg[1]);
}
}
SymbolKind::Modport(_) => {
self.str(&format!("{}", symbol.namespace).replace("::", "_"));
self.str(&format!(".{}", symbol.token.text));
self.namespace(&symbol.namespace);
self.str(&format!("{}", symbol.token.text));
}
SymbolKind::Port(_)
| SymbolKind::Variable(_)
Expand Down Expand Up @@ -1992,7 +2023,7 @@ impl VerylWalker for Emitter {
self.space(1);
if let Ok(symbol) = symbol_table::resolve(arg.identifier.as_ref()) {
if let Some(symbol) = symbol.found {
self.str(&format!("{}_", symbol.namespace).replace("::", "_"));
self.namespace(&symbol.namespace);
}
}
self.identifier(&arg.identifier);
Expand Down Expand Up @@ -2177,7 +2208,7 @@ impl VerylWalker for Emitter {
self.space(1);
if let Ok(symbol) = symbol_table::resolve(arg.identifier.as_ref()) {
if let Some(symbol) = symbol.found {
self.str(&format!("{}_", symbol.namespace).replace("::", "_"));
self.namespace(&symbol.namespace);
}
}
self.identifier(&arg.identifier);
Expand Down Expand Up @@ -2358,7 +2389,7 @@ impl VerylWalker for Emitter {
self.space(1);
if let Ok(symbol) = symbol_table::resolve(arg.identifier.as_ref()) {
if let Some(symbol) = symbol.found {
self.str(&format!("{}_", symbol.namespace).replace("::", "_"));
self.namespace(&symbol.namespace);
}
}
self.identifier(&arg.identifier);
Expand Down
12 changes: 12 additions & 0 deletions testcases/sv/37_package_ref.sv
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
module veryl_testcase_Module37;
int unsigned _a;
assign _a = veryl_testcase_Package37::A;
int unsigned _b;
assign _b = veryl_testcase_Package37::B_C;
int unsigned _c;
assign _c = veryl_testcase_Package37::X();
endmodule
package veryl_testcase_Package37;
localparam int unsigned A = 1;
typedef
enum logic {
B_C
} B;

function automatic int unsigned X;
return 0;
endfunction
endpackage
10 changes: 10 additions & 0 deletions testcases/vl/37_package_ref.vl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
module Module37 {
var _a: u32 = Package37::A;
var _b: u32 = Package37::B::C;
var _c: u32 = Package37::X();
}
package Package37 {
localparam A: u32 = 1;

enum B: logic {
C,
}

function X -> u32 {
return 0;
}
}
Loading