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

Enhance generic boundary #1192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 63 additions & 8 deletions crates/analyzer/src/handlers/check_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,62 @@ impl VerylGrammarTrait for CheckType<'_> {
));
}
}
GenericBoundKind::Inst(proto) => {
let proto_match = if arg.is_resolvable() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At Rust 2024 (Rust 1.85), if-let chain will be stabilized.
After it, this large if chain can be re-written like below. So TODO comment may be useful to mark for re-writing.

if arg.is_resolvable()
  && let Ok(symbol) = ...
  && let SymbolKind::Instance(x) = ...
  && let Ok(x) = ...
  && let Some(ref x) = x.found.proto() {
  ...
} else {
  false
}

if let Ok(symbol) =
symbol_table::resolve((&arg.generic_path(), &namespace))
{
if let SymbolKind::Instance(x) = symbol.found.kind {
if let Ok(x) = symbol_table::resolve((
&x.type_name.mangled_path(),
&namespace,
)) {
if let Some(ref x) = x.found.proto() {
let actual =
symbol_table::resolve((x, &namespace));
let required = symbol_table::resolve((
proto,
&defined_namespace,
));
if let (Ok(actual), Ok(required)) =
(actual, required)
{
actual.found.id == required.found.id
} else {
false
}
} else {
false
}
} else {
false
}
} else {
false
}
} else {
false
}
} else {
false
};

if !proto_match {
self.errors.push(AnalyzerError::mismatch_type(
&symbol.found.token.to_string(),
&format!("inst {proto}"),
&symbol.found.kind.to_kind_name(),
self.text,
&arg.range,
));
}
}
GenericBoundKind::Proto(proto) => {
let proto_match = if arg.is_resolvable() {
if let Ok(symbol) =
symbol_table::resolve((&arg.generic_path(), &namespace))
{
if let Some(ref x) = symbol.found.kind.proto() {
if let Some(ref x) = symbol.found.proto() {
let actual = symbol_table::resolve((x, &namespace));
let required = symbol_table::resolve((
proto,
Expand Down Expand Up @@ -327,13 +377,18 @@ impl VerylGrammarTrait for CheckType<'_> {
if let GenericBoundKind::Proto(ref x) = x.bound {
if let Ok(symbol) = symbol_table::resolve((x, &symbol.found.namespace))
{
if let SymbolKind::ProtoModule(x) = symbol.found.kind {
params.append(&mut x.parameters.clone());
ports.append(&mut x.ports.clone());
check_port_connection = true;
None
} else {
Some("module or interface")
match &symbol.found.kind {
SymbolKind::ProtoModule(x) => {
params.append(&mut x.parameters.clone());
ports.append(&mut x.ports.clone());
check_port_connection = true;
None
}
SymbolKind::Interface(x) => {
params.append(&mut x.parameters.clone());
None
}
_ => Some("module or interface"),
}
} else {
None
Expand Down
3 changes: 3 additions & 0 deletions crates/analyzer/src/handlers/create_symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,9 @@ impl VerylGrammarTrait for CreateSymbolTable<'_> {
let bound = match arg.generic_bound.as_ref() {
GenericBound::Const(_) => GenericBoundKind::Const,
GenericBound::Type(_) => GenericBoundKind::Type,
GenericBound::InstScopedIdentifier(x) => {
GenericBoundKind::Inst(x.scoped_identifier.as_ref().into())
}
GenericBound::ScopedIdentifier(x) => {
GenericBoundKind::Proto(x.scoped_identifier.as_ref().into())
}
Expand Down
25 changes: 14 additions & 11 deletions crates/analyzer/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ impl Symbol {
_ => Vec::new(),
}
}

pub fn proto(&self) -> Option<SymbolPath> {
match &self.kind {
SymbolKind::Module(x) => x.proto.clone(),
SymbolKind::Interface(_) => Some((&self.token).into()),
SymbolKind::GenericParameter(x) => match x.bound {
GenericBoundKind::Proto(ref x) => Some(x.clone()),
_ => None,
},
_ => None,
}
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -416,17 +428,6 @@ impl SymbolKind {
}
}

pub fn proto(&self) -> Option<SymbolPath> {
match self {
SymbolKind::Module(x) => x.proto.clone(),
SymbolKind::GenericParameter(x) => match x.bound {
GenericBoundKind::Proto(ref x) => Some(x.clone()),
_ => None,
},
_ => None,
}
}

pub fn get_type(&self) -> Option<&Type> {
match self {
SymbolKind::Port(x) => x.r#type.as_ref(),
Expand Down Expand Up @@ -1354,6 +1355,7 @@ pub struct ModportFunctionMemberProperty {
pub enum GenericBoundKind {
Const,
Type,
Inst(SymbolPath),
Proto(SymbolPath),
}

Expand All @@ -1362,6 +1364,7 @@ impl fmt::Display for GenericBoundKind {
let text = match self {
GenericBoundKind::Const => "const".to_string(),
GenericBoundKind::Type => "type".to_string(),
GenericBoundKind::Inst(x) => x.to_string(),
GenericBoundKind::Proto(x) => x.to_string(),
};
text.fmt(f)
Expand Down
14 changes: 10 additions & 4 deletions crates/analyzer/src/symbol_table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::evaluator::Evaluated;
use crate::namespace::Namespace;
use crate::symbol::{DocComment, Symbol, SymbolId, SymbolKind, TypeKind};
use crate::symbol::{DocComment, GenericBoundKind, Symbol, SymbolId, SymbolKind, TypeKind};
use crate::symbol_path::{SymbolPath, SymbolPathNamespace};
use crate::var_ref::{Assign, VarRef, VarRefAffiliation};
use std::cell::RefCell;
Expand Down Expand Up @@ -268,9 +268,15 @@ impl SymbolTable {
.generic_namespace_map
.insert(symbol.token.text, found.token.text);
}
SymbolKind::GenericParameter(_) => {
context.namespace = found.inner_namespace();
context.inner = true;
SymbolKind::GenericParameter(ref x) => {
if let GenericBoundKind::Inst(proto) = &x.bound {
let symbol = self.resolve(proto, &found.namespace)?;
context.namespace = symbol.found.inner_namespace();
context.inner = true;
} else {
context.namespace = found.inner_namespace();
context.inner = true;
}
}
// don't trace inner item
SymbolKind::Function(_)
Expand Down
Loading
Loading