spade-ast-lowering 0.16.0

Helper crate for https://spade-lang.org/
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use hir::{
    symbol_table::{EnumVariant, StructCallable},
    TypeExpression, WalTraceable,
};
use spade_ast::{self as ast, Module, ModuleBody};
use spade_common::{
    location_info::{Loc, WithLocation},
    name::{Identifier, Path, Visibility},
    namespace::ModuleNamespace,
};
use spade_diagnostics::{diag_anyhow, Diagnostic};
use spade_hir::WhereClause;
use spade_hir::{self as hir, symbol_table::TraitMarker};
use spade_types::meta_types::MetaType;

use crate::{
    attributes::{AttributeListExt, LocAttributeExt},
    impls::create_trait_from_unit_heads,
    types::{IsInOut, IsPort},
    visit_parameter_list, visit_trait_spec, visit_type_spec, Context, Result, TypeSpecKind,
};
use spade_hir::symbol_table::{GenericArg, Thing, TypeSymbol};

pub fn handle_external_modules(
    this_file: &str,
    inner_module: Option<&Loc<Module>>,
    module: &ast::ModuleBody,
    namespaces: &mut HashMap<Path, String>,
    ctx: &mut Context,
) -> Result<()> {
    for item in &module.members {
        match item {
            ast::Item::ExternalMod(ref em) => {
                let name = &em.name;
                if let Some(inner) = inner_module {
                    return Err(Diagnostic::error(
                        name,
                        "Modules from different files cannot be defined inside inline modules",
                    )
                    .primary_label("Module defined in inline module")
                    .secondary_label(inner, "Inline module defined here"));
                } else {
                    // If we get an external module, we need to
                    // 1. check that there is an associated file
                    // 2. Replace the loc of that module with the source code
                    let path = ctx.symtab.current_namespace().push_ident(name.clone());

                    // We're going to eagerly add the `mod` first in order to detect duplicates.
                    let name_id = ctx.symtab.add_unique_thing(
                        Path::ident(name.clone()).at_loc(&name),
                        Thing::Module(em.loc(), name.clone()),
                        Some(em.visibility.clone()),
                    )?;

                    ctx.item_list.modules.insert(
                        name_id.clone(),
                        spade_hir::Module {
                            name: name_id.at_loc(&name),
                            documentation: String::new(),
                        },
                    );

                    if namespaces.remove(&path).is_none() {
                        let name_parts = this_file.split("/").collect::<Vec<_>>();
                        if name_parts.len() > 1 {
                            let base = name_parts[0..name_parts.len() - 1].join("/");
                            let expected_name = format!("{base}/{name}.spade");
                            let expected_mod = format!("{base}/{name}/main.spade");

                            return Err(Diagnostic::error(
                                name,
                                format!("Did not find {expected_name} or {expected_mod}"),
                            )
                            .primary_label(format!("Did not find file for {name}")));
                        } else {
                            return Err(Diagnostic::error(
                                    name,
                                    format!("Did not find file for {name}"),
                                )
                                .help("Are you running Spade without swim? If so, multiple files are unsupported"));
                        };
                    }
                }
            }
            ast::Item::Module(m) => ctx.in_named_namespace(m.name.clone(), |ctx| {
                handle_external_modules(this_file, Some(m), &m.body, namespaces, ctx)
            })?,
            ast::Item::Type(_) => {}
            ast::Item::ImplBlock(_) => {}
            ast::Item::Unit(_) => {}
            ast::Item::TraitDef(_) => {}
            ast::Item::Use(_) => {}
        }
    }
    Ok(())
}

pub fn report_missing_mod_declarations(
    module_asts: &[(ModuleNamespace, Loc<ModuleBody>)],
    missing_namespace_set: &HashMap<Path, String>,
) -> Vec<Diagnostic> {
    // If we have things left in the namespace_set, we are missing a few `mod`, let's report
    // those
    let missing_namespace_set = missing_namespace_set
        .iter()
        .filter(|ns| !ns.0 .0.is_empty() && !(ns.1.ends_with("main.spade")))
        .collect::<Vec<_>>();

    missing_namespace_set
        .iter()
        .map(|(path, file)| {
            if path.0.len() == 0 {
                return diag_anyhow!(
                    ().nowhere(),
                    "Internal error: Found no mod for empty path (in {file})"
                );
            }

            let ns = path.tail();
            let parent_path = path.prelude();
            let parent_loc = module_asts.iter().find_map(|(ns, ast)| {
                if ns.namespace == parent_path {
                    Some(ast.loc())
                } else {
                    None
                }
            });

            if let Some(parent_loc) = parent_loc {
                Diagnostic::error(parent_loc, format!("Missing `mod {ns};` for {file}"))
                    .primary_label(format!("Missing `mod {ns};`"))
                    .span_suggest_insert_before(
                        format!("Consider adding `mod {ns};`"),
                        parent_loc,
                        format!("mod {ns}; "),
                    )
            } else {
                diag_anyhow!(().nowhere(), "Found {file} which is missing `mod` but the file where `mod` should go was not found")
            }
        })
        .collect()
}

#[tracing::instrument(skip_all)]
pub fn gather_types(module: &ast::ModuleBody, ctx: &mut Context) -> Result<()> {
    for item in &module.members {
        match item {
            ast::Item::Type(ref t) => {
                visit_type_declaration(t, ctx)?;
            }
            ast::Item::ExternalMod(_) => {}
            ast::Item::Module(ref m) => {
                ctx.symtab.add_unique_thing(
                    Path::ident(m.name.clone()).at_loc(&m.name),
                    Thing::Module(m.loc(), m.name.clone()),
                    Some(m.visibility.clone()),
                )?;
                ctx.in_named_namespace(m.name.clone(), |ctx| gather_types(&m.body, ctx))?
            }
            ast::Item::ImplBlock(_) => {}
            ast::Item::Unit(_) => {}
            ast::Item::TraitDef(ref r#trait) => {
                ctx.symtab.add_unique_thing(
                    Path::ident(r#trait.name.clone()).at_loc(&r#trait.name.clone()),
                    Thing::Trait(
                        TraitMarker {
                            name: r#trait.name.clone(),
                            paren_sugar: r#trait
                                .inner
                                .attributes
                                .0
                                .iter()
                                .any(|attr| attr.inner == ast::Attribute::SpadecParenSugar),
                        }
                        .at_loc(&r#trait.name),
                    ),
                    Some(r#trait.visibility.clone()),
                )?;
            }
            ast::Item::Use(us) => {
                for u in &us.inner {
                    let new_name = match &u.alias {
                        Some(name) => name.clone(),
                        None => u.path.0.last().unwrap().unwrap_named().clone(),
                    };

                    ctx.symtab.add_alias(
                        us.loc(),
                        new_name,
                        u.path.clone(),
                        u.visibility.clone(),
                    )?;
                }
            }
        }
    }
    Ok(())
}

/// Collect global symbols as a first pass before generating HIR
#[tracing::instrument(skip_all)]
pub fn gather_symbols(module: &ast::ModuleBody, ctx: &mut Context) -> Result<()> {
    for item in &module.members {
        visit_item(item, ctx)?;
    }
    Ok(())
}

#[tracing::instrument(skip_all)]
pub fn visit_item(item: &ast::Item, ctx: &mut Context) -> Result<()> {
    match item {
        ast::Item::Unit(ref e) => {
            visit_unit(&None, e, &None, &vec![], ctx)?;
        }
        ast::Item::TraitDef(ref def) => {
            let (name, _) = ctx.symtab.lookup_trait(&Path::ident(def.name.clone()).at_loc(&def.name)).map_err(|_| diag_anyhow!(def, "Did not find the trait in the trait list when looking it up during item visiting"))?;
            let mut paren_sugar = false;
            let documentation = def.attributes.merge_docs();
            let _ = def.attributes.lower(&mut |attr| match &attr.inner {
                ast::Attribute::Documentation { .. } => Ok(None),
                ast::Attribute::SpadecParenSugar => {
                    paren_sugar = true;
                    Ok(None)
                }
                ast::Attribute::VerilogAttrs { .. }
                | ast::Attribute::Optimize { .. }
                | ast::Attribute::SurferTranslator(_)
                | ast::Attribute::WalTraceable { .. }
                | ast::Attribute::NoMangle { .. }
                | ast::Attribute::Fsm { .. }
                | ast::Attribute::WalSuffix { .. }
                | ast::Attribute::WalTrace { .. } => Err(attr.report_unused("trait")),
            })?;
            create_trait_from_unit_heads(
                hir::TraitName::Named(name.at_loc(&def.name)),
                &def.type_params,
                &def.where_clauses,
                &def.methods,
                paren_sugar,
                documentation,
                ctx,
            )?;
        }
        ast::Item::ImplBlock(_) => {}
        ast::Item::Type(ref t) => {
            re_visit_type_declaration(t, ctx)?;
        }
        ast::Item::ExternalMod(_) => {}
        ast::Item::Module(m) => {
            ctx.in_named_namespace(m.name.clone(), |ctx| gather_symbols(&m.body, ctx))?
        }
        ast::Item::Use(_) => {}
    }
    Ok(())
}

#[tracing::instrument(skip_all)]
pub fn visit_unit(
    extra_path: &Option<Path>,
    unit: &Loc<ast::Unit>,
    scope_type_params: &Option<Loc<Vec<Loc<ast::TypeParam>>>>,
    scope_where_clauses: &[Loc<WhereClause>],
    ctx: &mut Context,
) -> Result<()> {
    let head = crate::unit_head(
        &unit.head,
        scope_type_params,
        scope_where_clauses,
        ctx,
        unit.inner.body.as_ref(),
    )?;

    let new_path = extra_path
        .as_ref()
        .unwrap_or(&Path(vec![]))
        .join(Path::ident(unit.head.name.clone()))
        .at_loc(&unit.head.name);

    ctx.symtab.add_unique_thing(
        new_path,
        Thing::Unit(head.at_loc(unit)),
        Some(unit.head.visibility.clone()),
    )?;

    Ok(())
}

pub fn visit_meta_type(meta: &Loc<Identifier>) -> Result<MetaType> {
    let meta = match meta.inner.as_str() {
        "int" => MetaType::Int,
        "uint" => MetaType::Uint,
        "str" => MetaType::Str,
        "type" => MetaType::Type,
        _ => {
            return Err(Diagnostic::error(meta, "{meta} is not a valid meta-type")
                .primary_label("Invalid meta-type")
                .help("Expected #int, #uint or #type"))
        }
    };
    Ok(meta)
}

pub fn visit_type_declaration(t: &Loc<ast::TypeDeclaration>, ctx: &mut Context) -> Result<()> {
    let args = t
        .generic_args
        .as_ref()
        .map(|args| &args.inner)
        .unwrap_or(&vec![])
        .iter()
        .map(|arg| {
            let result = match &arg.inner {
                ast::TypeParam::TypeName { name, traits } => {
                    let traits = traits
                        .iter()
                        .map(|t| visit_trait_spec(t, &TypeSpecKind::TraitBound, ctx))
                        .collect::<Result<Vec<_>>>()?;

                    GenericArg::TypeName {
                        name: name.inner.clone(),
                        traits,
                    }
                }
                ast::TypeParam::TypeWithMeta { name, meta } => GenericArg::TypeWithMeta {
                    name: name.inner.clone(),
                    meta: visit_meta_type(meta)?,
                },
            }
            .at_loc(&arg.loc());
            Ok(result)
        })
        .collect::<Result<_>>()?;

    let kind = match &t.kind {
        ast::TypeDeclKind::Enum(_) => hir::symbol_table::TypeDeclKind::Enum,
        ast::TypeDeclKind::Struct(s) => hir::symbol_table::TypeDeclKind::Struct {
            is_port: s.is_port(),
        },
    };

    let new_thing = t.name.clone();
    ctx.symtab.add_unique_type(
        new_thing,
        TypeSymbol::Declared(args, kind).at_loc(t),
        t.visibility.clone(),
    )?;

    Ok(())
}

/// Visit type declarations a second time, this time adding the type to the item list
/// as well as adding enum variants to the global scope.
/// This needs to happen as a separate pass since other types need to be in scope when
/// we check type declarations
pub fn re_visit_type_declaration(t: &Loc<ast::TypeDeclaration>, ctx: &mut Context) -> Result<()> {
    // Process right hand side of type declarations
    // The first visitor has already added the LHS to the symtab
    // Look up the ID
    let (declaration_id, _) = ctx
        .symtab
        .lookup_type_symbol(&Path::ident_with_loc(t.name.clone()))
        .expect("Expected type symbol to already be in symtab");
    let declaration_id = declaration_id.at_loc(&t.name);

    // Add the generic parameters to a new symtab scope
    ctx.symtab.new_scope();
    for param in t
        .generic_args
        .as_ref()
        .map(Loc::strip_ref)
        .unwrap_or(&vec![])
    {
        let (name, symbol_type) = match &param.inner {
            ast::TypeParam::TypeName { name: n, traits } => {
                let resolved_traits = traits
                    .iter()
                    .map(|t| visit_trait_spec(t, &TypeSpecKind::TraitBound, ctx))
                    .collect::<Result<Vec<_>>>()?;
                (
                    n,
                    TypeSymbol::GenericArg {
                        traits: resolved_traits,
                    },
                )
            }
            ast::TypeParam::TypeWithMeta { name, meta } => {
                (name, TypeSymbol::GenericMeta(visit_meta_type(meta)?))
            }
        };
        ctx.symtab.add_type(
            name.clone(),
            symbol_type.at_loc(param),
            t.visibility.clone(),
        );
    }

    // Generate TypeExprs and TypeParam vectors which are needed for building the
    // hir::TypeDeclaration and for enum constructors
    let mut output_type_exprs = vec![];
    let mut type_params = vec![];
    for arg in t.generic_args.as_ref().map(|l| &l.inner).unwrap_or(&vec![]) {
        let (name_id, _) = ctx
            .symtab
            .lookup_type_symbol(&Path::ident_with_loc(arg.name().clone()))
            .expect("Expected generic param to be in symtab");
        let expr = TypeExpression::TypeSpec(hir::TypeSpec::Generic(name_id.clone().at_loc(arg)))
            .at_loc(arg);
        let param = match &arg.inner {
            ast::TypeParam::TypeName { name, traits } => {
                let trait_bounds = traits
                    .iter()
                    .map(|t| visit_trait_spec(t, &TypeSpecKind::TraitBound, ctx))
                    .collect::<Result<Vec<_>>>()?;

                hir::TypeParam {
                    ident: name.clone(),
                    name_id: name_id.clone(),
                    trait_bounds,
                    meta: MetaType::Type,
                }
            }
            ast::TypeParam::TypeWithMeta { meta, name } => hir::TypeParam {
                ident: name.clone(),
                name_id: name_id.clone(),
                trait_bounds: vec![],
                meta: visit_meta_type(&meta)?,
            },
        };
        output_type_exprs.push(expr);
        type_params.push(param.at_loc(arg))
    }

    let hir_kind = match &t.inner.kind {
        ast::TypeDeclKind::Enum(e) => {
            let mut member_names = HashSet::<Loc<Identifier>>::default();
            let mut hir_options = vec![];

            // Set variant visibility based on enum visibility
            let variant_vis = match *t.visibility {
                // Implicit visibility (no visibility marker attached) is equivalent to `pub(self)`.
                // This means that an enum with implicit visibility must have enum variants with
                // `super` visibility.
                Visibility::Implicit => Visibility::AtSuper,
                Visibility::Public => Visibility::Public,
                Visibility::AtLib => Visibility::AtLib,
                Visibility::AtSelf => Visibility::AtSuper,
                Visibility::AtSuper => Visibility::AtSuperSuper,
                Visibility::AtSuperSuper => {
                    return Err(Diagnostic::bug(
                        t.loc(),
                        "Impossible visibility found while setting enum variant visibility",
                    ))
                }
            };

            for (i, variant) in e.variants.iter().enumerate() {
                if let Some(prev) = member_names.get(&variant.name) {
                    let new = &variant.name;
                    return Err(
                        Diagnostic::error(new, format!("Multiple options called {}", new))
                            .primary_label(format!("{} occurs more than once", new))
                            .secondary_label(prev, "Previously occurred here"),
                    );
                }
                member_names.insert(variant.name.clone());
                // Check the parameter list
                let parameter_list = variant
                    .args
                    .clone()
                    .map(|l| visit_parameter_list(&l, ctx, None))
                    .unwrap_or_else(|| Ok(hir::ParameterList(vec![]).nowhere()))?;

                let args = variant
                    .args
                    .clone()
                    .map(|l| {
                        if let Some(ref self_) = l.self_ {
                            Err(Diagnostic::bug(self_, "enum member contains self"))
                        } else {
                            Ok(l.args.clone())
                        }
                    })
                    .unwrap_or(Ok(vec![]))?;

                // Ensure that we don't have any port or inout types in the enum variants
                for (_, _, ty) in args {
                    let ty = visit_type_spec(&ty, &TypeSpecKind::EnumMember, ctx)?;
                    if ty.is_port(&ctx)? {
                        return Err(Diagnostic::error(ty, "Port in enum")
                            .primary_label("This is a port")
                            .secondary_label(&e.name, "This is an enum"));
                    }
                    if ty.is_inout(&ctx)? {
                        return Err(Diagnostic::error(ty, "Inout in enum")
                            .primary_label("This is an inout")
                            .secondary_label(&e.name, "This is an enum"));
                    }
                }

                let documentation = variant.attributes.merge_docs();
                let _ = variant.attributes.lower(&mut |attr| match &attr.inner {
                    ast::Attribute::Documentation { .. } => Ok(None),
                    ast::Attribute::Optimize { .. }
                    | ast::Attribute::VerilogAttrs { .. }
                    | ast::Attribute::SurferTranslator(_)
                    | ast::Attribute::SpadecParenSugar
                    | ast::Attribute::WalTraceable { .. }
                    | ast::Attribute::NoMangle { .. }
                    | ast::Attribute::Fsm { .. }
                    | ast::Attribute::WalSuffix { .. }
                    | ast::Attribute::WalTrace { .. } => Err(attr.report_unused("enum variant")),
                })?;

                let variant_thing = EnumVariant {
                    name: variant.name.clone(),
                    output_type: hir::TypeSpec::Declared(
                        declaration_id.clone(),
                        output_type_exprs.clone(),
                    )
                    .at_loc(t),
                    type_params: type_params.clone(),
                    option: i,
                    params: parameter_list.clone(),
                    documentation,
                };

                // Add option constructor to symtab at the outer scope
                let head_id = ctx.symtab.add_thing_at_offset(
                    1,
                    Path::from_idents(&[&e.name, &variant.name]),
                    Thing::EnumVariant(variant_thing.at_loc(&variant.name)),
                    Some(variant_vis.clone().at_loc(&t.visibility)),
                );

                // Add option constructor to item list
                ctx.item_list.executables.insert(
                    head_id.clone(),
                    hir::ExecutableItem::EnumInstance {
                        base_enum: declaration_id.inner.clone(),
                        variant: i,
                    },
                );

                // NOTE: it's kind of weird to push head_id here, since that's just
                // the constructor. In the future, if we move forward with enum members
                // being individual types, we should push that instead
                hir_options.push((head_id.clone().at_loc(&variant.name), parameter_list))
            }

            let documentation = e.attributes.merge_docs();
            let _ = e.attributes.lower(&mut |attr| match &attr.inner {
                ast::Attribute::Documentation { .. } => Ok(None),
                ast::Attribute::Optimize { .. }
                | ast::Attribute::VerilogAttrs { .. }
                | ast::Attribute::WalTraceable { .. }
                | ast::Attribute::SpadecParenSugar
                | ast::Attribute::NoMangle { .. }
                | ast::Attribute::Fsm { .. }
                | ast::Attribute::WalSuffix { .. }
                | ast::Attribute::SurferTranslator(_)
                | ast::Attribute::WalTrace { .. } => Err(attr.report_unused("enum")),
            })?;

            hir::TypeDeclKind::Enum(
                hir::Enum {
                    options: hir_options,
                    documentation,
                }
                .at_loc(e),
            )
        }
        ast::TypeDeclKind::Struct(s) => {
            if let Some(ref self_) = s.members.self_ {
                return Err(Diagnostic::bug(
                    self_,
                    "struct contains self member which was let through parser",
                ));
            }

            // Disallow normal arguments if the struct is a port, and port types
            // if it is not
            for (_, f, ty) in &s.members.args {
                let hir_ty = visit_type_spec(ty, &TypeSpecKind::StructMember, ctx)?;
                if hir_ty.is_inout(ctx)? {
                    return Err(Diagnostic::error(ty, "Inout in struct")
                        .primary_label("This is an inout")
                        .secondary_label(&s.name, "This is a struct"));
                }
                if s.is_port() {
                    if !hir_ty.is_port(ctx)? {
                        return Err(Diagnostic::error(ty, "Non-port in port struct")
                            .primary_label("This is not a port type")
                            .secondary_label(
                                s.port_keyword.unwrap(),
                                format!("{} is a port struct", s.name),
                            )
                            .note("All members of a port struct must be ports")
                            .span_suggest_insert_before(
                                format!("Consider making {f} a wire"),
                                ty,
                                "&",
                            ));
                    }
                } else {
                    if hir_ty.is_port(ctx)? {
                        return Err(Diagnostic::error(ty, "Port in non-port struct")
                            .primary_label("This is a port")
                            .secondary_label(&s.name, "This is not a port struct")
                            .span_suggest_insert_before(
                                format!("Consider making {} a port struct", s.name),
                                &s.name,
                                "port ",
                            ));
                    }
                }
            }

            let members = visit_parameter_list(&s.members, ctx, None)?;

            let self_type =
                hir::TypeSpec::Declared(declaration_id.clone(), output_type_exprs.clone())
                    .at_loc(s);

            ctx.symtab.add_thing_with_name_id(
                declaration_id.inner.clone(),
                Thing::Struct(
                    StructCallable {
                        name: t.name.clone(),
                        self_type,
                        params: members.clone(),
                        type_params: type_params.clone(),
                    }
                    .at_loc(s),
                ),
                Some(t.visibility.clone()),
            );

            ctx.item_list.executables.insert(
                declaration_id.inner.clone(),
                hir::ExecutableItem::StructInstance,
            );

            let mut wal_traceable = None;
            let documentation = s.attributes.merge_docs();
            let attributes = s.attributes.lower(&mut |attr| match &attr.inner {
                ast::Attribute::WalTraceable {
                    suffix,
                    uses_rst,
                    uses_clk,
                } => {
                    let suffix = if let Some(suffix) = suffix {
                        Path::ident(suffix.clone())
                    } else {
                        declaration_id.1.clone()
                    };
                    wal_traceable = Some(
                        WalTraceable {
                            suffix,
                            uses_clk: *uses_clk,
                            uses_rst: *uses_rst,
                        }
                        .at_loc(attr),
                    );
                    Ok(None)
                }
                ast::Attribute::Documentation { .. } => Ok(None),
                ast::Attribute::Optimize { .. }
                | ast::Attribute::VerilogAttrs { .. }
                | ast::Attribute::NoMangle { .. }
                | ast::Attribute::Fsm { .. }
                | ast::Attribute::WalSuffix { .. }
                | ast::Attribute::SpadecParenSugar
                | ast::Attribute::SurferTranslator(_)
                | ast::Attribute::WalTrace { .. } => Err(attr.report_unused("struct")),
            })?;

            // We don't do any special processing of structs here
            hir::TypeDeclKind::Struct(
                hir::Struct {
                    members,
                    is_port: s.is_port(),
                    attributes,
                    wal_traceable,
                    documentation,
                }
                .at_loc(s),
            )
        }
    };
    // Close the symtab scope
    ctx.symtab.close_scope();

    // Add type to item list
    let decl = hir::TypeDeclaration {
        name: declaration_id.clone(),
        kind: hir_kind,
        generic_args: type_params,
    }
    .at_loc(t);
    ctx.item_list.types.insert(declaration_id.inner, decl);

    Ok(())
}