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

Support default value of generic parameter #726

Merged
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
23 changes: 23 additions & 0 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ pub enum AnalyzerError {
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(missing_default_argument),
help("give default argument"),
url("")
)]
#[error("missing default argument for parameter {identifier}")]
MissingDefaultArgument {
identifier: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(mismatch_function_arity),
Expand Down Expand Up @@ -840,6 +855,14 @@ impl AnalyzerError {
}
}

pub fn missing_default_argument(identifier: &str, source: &str, token: &TokenRange) -> Self {
AnalyzerError::MissingDefaultArgument {
identifier: identifier.into(),
input: AnalyzerError::named_source(source, token),
error_location: token.into(),
}
}

pub fn mismatch_function_arity(
name: &str,
arity: usize,
Expand Down
2 changes: 1 addition & 1 deletion crates/analyzer/src/handlers/check_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'a> VerylGrammarTrait for CheckInstance<'a> {
}
SymbolKind::Interface(_) => (),
SymbolKind::SystemVerilog => (),
SymbolKind::GenericParameter => (),
SymbolKind::GenericParameter(_) => (),
_ => {
self.errors.push(AnalyzerError::mismatch_type(
name,
Expand Down
32 changes: 23 additions & 9 deletions crates/analyzer/src/handlers/create_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,38 @@ impl<'a> CreateReference<'a> {
symbol_table::add_reference(symbol.found.id, &path.paths[0].base);

// Check number of arguments
let params = symbol.found.generic_parameters().len();
let args = path.paths[i].arguments.len();
if params != args {
let params = symbol.found.generic_parameters();
let n_args = path.paths[i].arguments.len();
let match_artiy = if params.len() > n_args {
params[n_args].1.is_some()
} else {
params.len() == n_args
};

if !match_artiy {
self.errors.push(AnalyzerError::mismatch_generics_arity(
&path.paths[i].base.to_string(),
params,
args,
params.len(),
n_args,
self.text,
&path.range,
));
} else if let Some((token, new_symbol)) =
path.get_generic_instance(i, &symbol.found)
{
continue;
}

let mut path = path.paths[i].clone();

for param in params.iter().skip(n_args) {
// apply default value
path.arguments.push(param.1.as_ref().unwrap().clone());
}

if let Some((token, new_symbol)) = path.get_generic_instance(&symbol.found) {
if let Some(ref x) = symbol_table::insert(&token, new_symbol) {
symbol_table::add_generic_instance(symbol.found.id, *x);
}

let table = symbol.found.generic_table(&path.paths[i].arguments);
let table = symbol.found.generic_table(&path.arguments);
let mut references = symbol.found.generic_references();
for path in &mut references {
path.apply_map(&table);
Expand Down
53 changes: 43 additions & 10 deletions crates/analyzer/src/handlers/create_symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::namespace_table;
use crate::symbol::Direction as SymDirection;
use crate::symbol::Type as SymType;
use crate::symbol::{
DocComment, EnumMemberProperty, EnumProperty, FunctionProperty, InstanceProperty,
InterfaceProperty, ModportMemberProperty, ModportProperty, ModuleProperty, PackageProperty,
ParameterProperty, ParameterScope, ParameterValue, PortProperty, StructMemberProperty,
StructProperty, Symbol, SymbolId, SymbolKind, TypeDefProperty, TypeKind, UnionMemberProperty,
UnionProperty, VariableAffiniation, VariableProperty,
DocComment, EnumMemberProperty, EnumProperty, FunctionProperty, GenericParameterProperty,
InstanceProperty, InterfaceProperty, ModportMemberProperty, ModportProperty, ModuleProperty,
PackageProperty, ParameterProperty, ParameterScope, ParameterValue, PortProperty,
StructMemberProperty, StructProperty, Symbol, SymbolId, SymbolKind, TypeDefProperty, TypeKind,
UnionMemberProperty, UnionProperty, VariableAffiniation, VariableProperty,
};
use crate::symbol_path::{GenericSymbolPath, SymbolPath};
use crate::symbol_table;
Expand Down Expand Up @@ -42,6 +42,7 @@ pub struct CreateSymbolTable<'a> {
connect_targets: Vec<Vec<StrId>>,
connects: HashMap<Token, Vec<Vec<StrId>>>,
generic_parameters: Vec<SymbolId>,
needs_default_generic_argument: bool,
generic_references: Vec<GenericSymbolPath>,
default_clock_candidates: Vec<SymbolId>,
defualt_reset_candidates: Vec<SymbolId>,
Expand Down Expand Up @@ -608,16 +609,48 @@ impl<'a> VerylGrammarTrait for CreateSymbolTable<'a> {
Ok(())
}

fn with_generic_parameter_list(
&mut self,
_arg: &WithGenericParameterList,
) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
self.needs_default_generic_argument = false;
}
Ok(())
}

fn with_generic_parameter_item(
&mut self,
arg: &WithGenericParameterItem,
) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
let kind = SymbolKind::GenericParameter;
if let Some(id) =
self.insert_symbol(&arg.identifier.identifier_token.token, kind, false)
{
self.generic_parameters.push(id);
let default_value: Option<GenericSymbolPath> =
if let Some(ref x) = arg.with_generic_parameter_item_opt {
self.needs_default_generic_argument = true;
match &*x.with_generic_argument_item {
WithGenericArgumentItem::ScopedIdentifier(x) => {
Some(x.scoped_identifier.as_ref().into())
}
WithGenericArgumentItem::Number(x) => Some(x.number.as_ref().into()),
}
} else {
None
};

if !self.needs_default_generic_argument || default_value.is_some() {
let property = GenericParameterProperty { default_value };
let kind = SymbolKind::GenericParameter(property);
if let Some(id) =
self.insert_symbol(&arg.identifier.identifier_token.token, kind, false)
{
self.generic_parameters.push(id);
}
} else {
self.errors.push(AnalyzerError::missing_default_argument(
&arg.identifier.identifier_token.token.to_string(),
self.text,
&arg.identifier.as_ref().into(),
));
}
}
Ok(())
Expand Down
40 changes: 29 additions & 11 deletions crates/analyzer/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,45 +170,58 @@ impl Symbol {
let mut ret = HashMap::new();

for (i, arg) in arguments.iter().enumerate() {
if let Some(p) = generic_parameters.get(i) {
if let Some((p, _)) = generic_parameters.get(i) {
ret.insert(*p, arg.clone());
}
}

for param in generic_parameters.iter().skip(arguments.len()) {
ret.insert(param.0, param.1.as_ref().unwrap().clone());
}

ret
}

pub fn generic_parameters(&self) -> Vec<StrId> {
pub fn generic_parameters(&self) -> Vec<(StrId, Option<GenericSymbolPath>)> {
fn get_generic_parameter(id: SymbolId) -> (StrId, Option<GenericSymbolPath>) {
let symbol = symbol_table::get(id).unwrap();
if let SymbolKind::GenericParameter(x) = symbol.kind {
(symbol.token.text, x.default_value)
} else {
unreachable!()
}
}

match &self.kind {
SymbolKind::Function(x) => x
.generic_parameters
.iter()
.map(|x| symbol_table::get(*x).unwrap().token.text)
.map(|x| get_generic_parameter(*x))
.collect(),
SymbolKind::Module(x) => x
.generic_parameters
.iter()
.map(|x| symbol_table::get(*x).unwrap().token.text)
.map(|x| get_generic_parameter(*x))
.collect(),
SymbolKind::Interface(x) => x
.generic_parameters
.iter()
.map(|x| symbol_table::get(*x).unwrap().token.text)
.map(|x| get_generic_parameter(*x))
.collect(),
SymbolKind::Package(x) => x
.generic_parameters
.iter()
.map(|x| symbol_table::get(*x).unwrap().token.text)
.map(|x| get_generic_parameter(*x))
.collect(),
SymbolKind::Struct(x) => x
.generic_parameters
.iter()
.map(|x| symbol_table::get(*x).unwrap().token.text)
.map(|x| get_generic_parameter(*x))
.collect(),
SymbolKind::Union(x) => x
.generic_parameters
.iter()
.map(|x| symbol_table::get(*x).unwrap().token.text)
.map(|x| get_generic_parameter(*x))
.collect(),
_ => Vec::new(),
}
Expand Down Expand Up @@ -251,7 +264,7 @@ pub enum SymbolKind {
SystemVerilog,
Namespace,
SystemFunction,
GenericParameter,
GenericParameter(GenericParameterProperty),
GenericInstance(GenericInstanceProperty),
}

Expand Down Expand Up @@ -280,7 +293,7 @@ impl SymbolKind {
SymbolKind::SystemVerilog => "systemverilog item".to_string(),
SymbolKind::Namespace => "namespace".to_string(),
SymbolKind::SystemFunction => "system function".to_string(),
SymbolKind::GenericParameter => "generic parameter".to_string(),
SymbolKind::GenericParameter(_) => "generic parameter".to_string(),
SymbolKind::GenericInstance(_) => "generic instance".to_string(),
}
}
Expand Down Expand Up @@ -381,7 +394,7 @@ impl fmt::Display for SymbolKind {
SymbolKind::SystemVerilog => "systemverilog item".to_string(),
SymbolKind::Namespace => "namespace".to_string(),
SymbolKind::SystemFunction => "system function".to_string(),
SymbolKind::GenericParameter => "generic parameter".to_string(),
SymbolKind::GenericParameter(_) => "generic parameter".to_string(),
SymbolKind::GenericInstance(_) => "generic instance".to_string(),
};
text.fmt(f)
Expand Down Expand Up @@ -853,6 +866,11 @@ pub struct ModportMemberProperty {
pub direction: Direction,
}

#[derive(Debug, Clone)]
pub struct GenericParameterProperty {
pub default_value: Option<GenericSymbolPath>,
}

#[derive(Debug, Clone)]
pub struct GenericInstanceProperty {
pub base: SymbolId,
Expand Down
Loading
Loading