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 issue that generic_references are unexpectedly cleared #1138

Merged
merged 1 commit into from
Dec 11, 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
60 changes: 41 additions & 19 deletions crates/analyzer/src/handlers/create_symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ use veryl_parser::veryl_token::{Token, TokenRange, TokenSource};
use veryl_parser::veryl_walker::{Handler, HandlerPoint};
use veryl_parser::ParolError;

#[derive(Default)]
struct GenericContext {
parameters: Vec<Vec<SymbolId>>,
references: Vec<Vec<GenericSymbolPath>>,
}

impl GenericContext {
pub fn push(&mut self) {
self.parameters.push(Vec::new());
self.references.push(Vec::new());
}

pub fn pop(&mut self) -> (Vec<SymbolId>, Vec<GenericSymbolPath>) {
(
self.parameters.pop().unwrap(),
self.references.pop().unwrap(),
)
}

pub fn push_parameter(&mut self, id: SymbolId) {
self.parameters.last_mut().unwrap().push(id);
}

pub fn push_reference(&mut self, path: GenericSymbolPath) {
self.references.last_mut().unwrap().push(path);
}
}

#[derive(Default)]
pub struct CreateSymbolTable<'a> {
pub errors: Vec<AnalyzerError>,
Expand All @@ -54,11 +82,10 @@ pub struct CreateSymbolTable<'a> {
affiliation: Vec<VariableAffiliation>,
connect_targets: Vec<ConnectTarget>,
connects: HashMap<Token, Vec<ConnectTarget>>,
generic_parameters: Vec<Vec<SymbolId>>,
parameters: Vec<Vec<Parameter>>,
ports: Vec<Vec<Port>>,
needs_default_generic_argument: bool,
generic_references: Vec<GenericSymbolPath>,
generic_context: GenericContext,
default_clock_candidates: Vec<SymbolId>,
defualt_reset_candidates: Vec<SymbolId>,
modport_member_ids: Vec<SymbolId>,
Expand Down Expand Up @@ -433,7 +460,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
HandlerPoint::After => {
let path: GenericSymbolPath = arg.into();
if path.is_generic_reference() {
self.generic_references.push(path);
self.generic_context.push_reference(path);
}
}
}
Expand Down Expand Up @@ -790,15 +817,14 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
StructUnion::Union(_) => Some(StructOrUnion::InUnion),
};

self.generic_parameters.push(Vec::new());
self.generic_context.push();
self.namespace.push(name);
}
HandlerPoint::After => {
self.struct_or_union = None;
self.namespace.pop();

let generic_parameters: Vec<_> = self.generic_parameters.pop().unwrap();
let generic_references: Vec<_> = self.generic_references.drain(..).collect();
let (generic_parameters, generic_references) = self.generic_context.pop();

let members: Vec<_> = self.struct_union_members.drain(0..).flatten().collect();
let kind = match &*arg.struct_union {
Expand Down Expand Up @@ -980,7 +1006,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
if let Some(id) =
self.insert_symbol(&arg.identifier.identifier_token.token, kind, false)
{
self.generic_parameters.last_mut().unwrap().push(id);
self.generic_context.push_parameter(id);
}
} else {
self.errors.push(AnalyzerError::missing_default_argument(
Expand Down Expand Up @@ -1091,16 +1117,15 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
match self.point {
HandlerPoint::Before => {
self.namespace.push(name);
self.generic_parameters.push(Vec::new());
self.generic_context.push();
self.ports.push(Vec::new());
self.affiliation.push(VariableAffiliation::Function);
}
HandlerPoint::After => {
self.namespace.pop();
self.affiliation.pop();

let generic_parameters: Vec<_> = self.generic_parameters.pop().unwrap();
let generic_references: Vec<_> = self.generic_references.drain(..).collect();
let (generic_parameters, generic_references) = self.generic_context.pop();
let ports: Vec<_> = self.ports.pop().unwrap();

let ret = arg
Expand Down Expand Up @@ -1165,7 +1190,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
match self.point {
HandlerPoint::Before => {
self.namespace.push(name);
self.generic_parameters.push(Vec::new());
self.generic_context.push();
self.parameters.push(Vec::new());
self.ports.push(Vec::new());
self.affiliation.push(VariableAffiliation::Module);
Expand All @@ -1180,8 +1205,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
self.affiliation.pop();
self.module_namspace_depth = 0;

let generic_parameters: Vec<_> = self.generic_parameters.pop().unwrap();
let generic_references: Vec<_> = self.generic_references.drain(..).collect();
let (generic_parameters, generic_references) = self.generic_context.pop();
let parameters: Vec<_> = self.parameters.pop().unwrap();
let ports: Vec<_> = self.ports.pop().unwrap();

Expand Down Expand Up @@ -1296,7 +1320,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
match self.point {
HandlerPoint::Before => {
self.namespace.push(name);
self.generic_parameters.push(Vec::new());
self.generic_context.push();
self.parameters.push(Vec::new());
self.affiliation.push(VariableAffiliation::Intarface);
self.function_ids.clear();
Expand All @@ -1308,8 +1332,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
self.namespace.pop();
self.affiliation.pop();

let generic_parameters: Vec<_> = self.generic_parameters.pop().unwrap();
let generic_references: Vec<_> = self.generic_references.drain(..).collect();
let (generic_parameters, generic_references) = self.generic_context.pop();
let parameters: Vec<_> = self.parameters.pop().unwrap();

let range =
Expand Down Expand Up @@ -1350,7 +1373,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
match self.point {
HandlerPoint::Before => {
self.namespace.push(name);
self.generic_parameters.push(Vec::new());
self.generic_context.push();
self.affiliation.push(VariableAffiliation::Package);
self.function_ids.clear();

Expand All @@ -1360,8 +1383,7 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
self.namespace.pop();
self.affiliation.pop();

let generic_parameters: Vec<_> = self.generic_parameters.pop().unwrap();
let generic_references: Vec<_> = self.generic_references.drain(..).collect();
let (generic_parameters, generic_references) = self.generic_context.pop();

let range = TokenRange::new(&arg.package.package_token, &arg.r_brace.r_brace_token);

Expand Down
2 changes: 1 addition & 1 deletion testcases/map/testcases/sv/56_generic_interface.sv.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions testcases/sv/56_generic_interface.sv
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module veryl_testcase_Module56;
veryl_testcase___Interface56B__Package56A_X u3 ();
veryl_testcase___Interface56B__3 u4 ();
veryl_testcase___Interface56B__Package56A_X u5 ();
veryl_testcase___Module56Sub__1 u6 ();

logic _a;
always_comb _a = u0._a;
Expand All @@ -15,14 +16,25 @@ module veryl_testcase_Module56;
always_comb _c = u5._b;
endmodule

module veryl_testcase___Module56Sub__1;
veryl_testcase___Interface56A__1 u ();
function automatic void f() ;
endfunction
endmodule

/// Generic interface test for doc comment
interface veryl_testcase___Interface56A__Package56A_X;
logic [veryl_testcase_Package56A::X-1:0] _a;
logic [veryl_testcase_Package56A::X-1:0] _a;
endinterface

/// Generic interface test for doc comment
interface veryl_testcase___Interface56A__Package56B_X;
logic [veryl_testcase_Package56B::X-1:0] _a;
logic [veryl_testcase_Package56B::X-1:0] _a;
endinterface

/// Generic interface test for doc comment
interface veryl_testcase___Interface56A__1;
logic [1-1:0] _a;
endinterface

/// Generic interface test for doc comment
Expand Down
6 changes: 6 additions & 0 deletions testcases/veryl/56_generic_interface.veryl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ module Module56 {
inst u3: Interface56B::<Package56A::X>;
inst u4: Interface56B::<>;
inst u5: Interface56B::<X>;
inst u6: Module56Sub::<1>;

let _a: logic = u0._a;
let _b: logic = u2._b;
let _c: logic = u5._b;
}

module Module56Sub::<T: const> {
inst u: Interface56A::<T>;
function f () {}
}

/// Generic interface test for doc comment
pub interface Interface56A::<T: const> {
var _a: logic<T>;
Expand Down
Loading