Skip to content

Itroduce gen declaration#2412

Open
taichi-ishitani wants to merge 1 commit intoveryl-lang:masterfrom
taichi-ishitani:gen_declaration
Open

Itroduce gen declaration#2412
taichi-ishitani wants to merge 1 commit intoveryl-lang:masterfrom
taichi-ishitani:gen_declaration

Conversation

@taichi-ishitani
Copy link
Copy Markdown
Contributor

@taichi-ishitani taichi-ishitani commented Mar 27, 2026

close #2349

gen declaration is a new declaration like const declaration for generics feature. This is used to define genreic const which can be given as generic asg.

By using gen declaration, you can provide result of expression and variable type declaratoin to generic arg like below.

module ModuleA::<W: u32, T: type) (
    a: output logic<W>,
    b: output T       ,
) {}
module ModuleB::<A: u32, B: u32, C: u32, D: u32> {
    gen W: u32  = A + B;
    gen T: type = logic<C, D>;
    inst u: ModuleA::<W, T> (a: _, b: _);
}

Expression in gen declaration has the same limitation with generic arg becuase generic consts are used as gneric arg. Therefore, the given expression can include following factors:

  • Literal
  • Symbol defined in package
  • Genreic parameter
  • Generic const

@taichi-ishitani taichi-ishitani marked this pull request as draft March 27, 2026 16:07
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 27, 2026

Merging this PR will not alter performance

✅ 6 untouched benchmarks


Comparing taichi-ishitani:gen_declaration (8613234) with master (8fa3ede)

Open in CodSpeed

@taichi-ishitani taichi-ishitani force-pushed the gen_declaration branch 3 times, most recently from dd18158 to 75a985f Compare March 28, 2026 00:47
Copy link
Copy Markdown
Contributor Author

@taichi-ishitani taichi-ishitani Mar 28, 2026

Choose a reason for hiding this comment

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

Checker for generic reference (check_generic_reference) is rewritten based on IR evaluation.
This is to share the checker routines with generic expression checker.

To implement this, IR evaluation needs to handle component reference and component instnace because they are given as generic args.
Module, Package and Instance are added as new enum variants of ir::TypeKind for this purpose.
https://github.com/taichi-ishitani/veryl/blob/75a985f958a8b18094cc291703a3f768cd49d88a/crates/analyzer/src/ir/comptime.rs#L757-L761
Component referecne and instance are converted into ir::Factor::Value if the expression is in gen declaration.

let namespace = self.inner_namespace();
for (name, r#const) in &consts {
let maps = vec![map.clone()];
if let Some(mut path) = Self::expr_to_generic_symbol_path(&r#const.value, &maps) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Generic consts are evaluated when resolving generic params of the generic component.
The evaluated result is converted to GenericSymbolPath and added to the resolved generic map.
Resolved generic consts acts like generic args.

pub enum GenericSymbolPathKind {
Identifier,
TypeLiteral,
VariableType(Vec<usize>),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

VariableType handles variable type declaration (e.g. logic<8>).

EnumMemberValue::UnevaluableValue => None,
};

if let Some(mut factor) = factor {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Type kind of enum member should be its parent enum. The replacement below is for this.

if x.path == member_path {
let mut comptime = expr.eval_comptime(context, None).clone();
comptime.token = token;
comptime.r#type = get_member_type(context, member_symbol)?;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

struct member is typed so type of evaluation result should be restored with the struct member type.

base: Option<&Symbol>,
token: TokenRange,
) -> Option<AnalyzerError> {
let mut paths = collect_symbol_paths(expression);
Copy link
Copy Markdown
Contributor Author

@taichi-ishitani taichi-ishitani Mar 28, 2026

Choose a reason for hiding this comment

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

Identifiers in the given expression are collected by iterating factors in the expression.
They are converted into generic symbol pashs and used to check if the expression is referable from the generic context.
At first, I implmented this check by adding a flag showing a factor can be used as generic arg to Comptime and propagting the flag during IR evaluation.
However, value of the flag is incorrect when processing the check because namespace used for the check is not provided when creating IR.
Therefore, I implemented the check based on iterating identifiers.

@taichi-ishitani taichi-ishitani marked this pull request as ready for review March 28, 2026 01:40
@taichi-ishitani taichi-ishitani requested a review from dalance March 28, 2026 01:40
@taichi-ishitani taichi-ishitani force-pushed the gen_declaration branch 3 times, most recently from 9436476 to 05197ea Compare March 29, 2026 10:36
@taichi-ishitani taichi-ishitani marked this pull request as draft March 30, 2026 00:32
@taichi-ishitani taichi-ishitani force-pushed the gen_declaration branch 2 times, most recently from 06d19d0 to 8e8a730 Compare March 30, 2026 05:32
@taichi-ishitani taichi-ishitani marked this pull request as ready for review March 30, 2026 05:32
@taichi-ishitani taichi-ishitani force-pushed the gen_declaration branch 7 times, most recently from ec1137c to e82b571 Compare April 2, 2026 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accept expression and factor type as generic args

1 participant