vhdl_lang 0.15.0

VHDL Language Frontend
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2019, Olof Kraigher olof.kraigher@gmail.com

use super::*;
use crate::ast::*;
use crate::data::*;
use analyze::*;
use fnv::FnvHashMap;
use region::*;
use std::collections::hash_map::Entry;
use std::sync::Arc;

impl<'a> AnalyzeContext<'a> {
    pub fn analyze_declarative_part(
        &self,
        region: &mut Region<'_>,
        declarations: &mut [Declaration],
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        let mut incomplete_types: FnvHashMap<Symbol, (EntityId, SrcPos)> = FnvHashMap::default();

        for i in 0..declarations.len() {
            // Handle incomplete types

            let (decl, remaining) = declarations[i..].split_first_mut().unwrap();

            match decl {
                Declaration::Type(type_decl) => match type_decl.def {
                    TypeDefinition::Incomplete(ref mut reference) => {
                        reference.clear_reference();

                        let full_definiton =
                            find_full_type_definition(type_decl.ident.name(), remaining);

                        let decl_pos = match full_definiton {
                            Some(full_decl) => {
                                reference.set_reference_pos(Some(full_decl.ident.pos()));
                                full_decl.ident.pos()
                            }
                            None => {
                                let mut error = Diagnostic::error(
                                    type_decl.ident.pos(),
                                    format!(
                                        "Missing full type declaration of incomplete type '{}'",
                                        type_decl.ident.name()
                                    ),
                                );
                                error.add_related(type_decl.ident.pos(), "The full type declaration shall occur immediately within the same declarative part");
                                diagnostics.push(error);
                                type_decl.ident.pos()
                            }
                        };

                        match incomplete_types.entry(type_decl.ident.name().clone()) {
                            Entry::Vacant(entry) => {
                                let designator =
                                    Designator::Identifier(type_decl.ident.name().clone());
                                // Set incomplete type defintion to position of full declaration
                                let ent = NamedEntity::new(
                                    designator,
                                    NamedEntityKind::IncompleteType,
                                    Some(decl_pos),
                                );
                                entry.insert((ent.id(), type_decl.ident.pos().clone()));
                                region.add_named_entity(Arc::new(ent), diagnostics);
                            }
                            Entry::Occupied(entry) => {
                                let (_, decl_pos) = entry.get();

                                diagnostics.push(duplicate_error(
                                    &type_decl.ident,
                                    type_decl.ident.pos(),
                                    Some(decl_pos),
                                ));
                            }
                        }
                    }
                    _ => {
                        let incomplete_type = incomplete_types.get(type_decl.ident.name());
                        let id = incomplete_type.map(|(id, _)| *id);
                        self.analyze_type_declaration(region, type_decl, id, diagnostics)?;
                    }
                },
                _ => {
                    self.analyze_declaration(region, &mut declarations[i], diagnostics)?;
                }
            }
        }
        Ok(())
    }

    fn analyze_declaration(
        &self,
        region: &mut Region<'_>,
        decl: &mut Declaration,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match decl {
            Declaration::Alias(alias) => {
                let AliasDeclaration {
                    designator,
                    name,
                    subtype_indication,
                    signature,
                } = alias;

                let resolved_name =
                    self.resolve_name(region, &name.pos, &mut name.item, diagnostics)?;

                if let Some(ref mut subtype_indication) = subtype_indication {
                    // Object alias
                    self.analyze_subtype_indication(region, subtype_indication, diagnostics)?;
                }

                if let Some(ref mut signature) = signature {
                    // Subprogram or enum literal alias
                    self.analyze_signature(region, signature, diagnostics)?;
                }

                let kind = {
                    if subtype_indication.is_some() {
                        NamedEntityKind::OtherAlias
                    } else if let Some(named_entities) =
                        resolved_name.and_then(|name| name.into_known())
                    {
                        let ent = match named_entities {
                            NamedEntities::Single(ent) => {
                                if let Some(signature) = signature {
                                    diagnostics.error(signature, "Alias should only have a signature for subprograms and enum literals");
                                }
                                ent
                            }
                            NamedEntities::Overloaded(overloaded) => {
                                if signature.is_none() {
                                    diagnostics.error(name, "Signature required for alias of subprogram and enum literals");
                                }
                                // @TODO use signature to determine variant
                                overloaded.first().clone()
                            }
                        };

                        region.add_implicit_declaration_aliases(
                            Some(&designator.pos),
                            &ent,
                            diagnostics,
                        );
                        NamedEntityKind::AliasOf(ent)
                    } else {
                        NamedEntityKind::OtherAlias
                    }
                };

                region.add(designator.clone(), kind, diagnostics);
            }
            Declaration::Object(ref mut object_decl) => {
                self.analyze_subtype_indication(
                    region,
                    &mut object_decl.subtype_indication,
                    diagnostics,
                )?;
                if let Some(ref mut expr) = object_decl.expression {
                    self.analyze_expression(region, expr, diagnostics)?;
                }
                region.add(
                    &object_decl.ident,
                    NamedEntityKind::from_object_declaration(object_decl),
                    diagnostics,
                );
            }
            Declaration::File(ref mut file) => {
                let FileDeclaration {
                    ident,
                    subtype_indication,
                    open_info,
                    file_name,
                } = file;
                self.analyze_subtype_indication(region, subtype_indication, diagnostics)?;
                if let Some(ref mut expr) = open_info {
                    self.analyze_expression(region, expr, diagnostics)?;
                }
                if let Some(ref mut expr) = file_name {
                    self.analyze_expression(region, expr, diagnostics)?;
                }
                region.add(ident.clone(), NamedEntityKind::File, diagnostics);
            }
            Declaration::Component(ref mut component) => {
                region.add(&component.ident, NamedEntityKind::Component, diagnostics);
                let mut region = region.nested();
                self.analyze_interface_list(&mut region, &mut component.generic_list, diagnostics)?;
                self.analyze_interface_list(&mut region, &mut component.port_list, diagnostics)?;
                region.close(diagnostics);
            }
            Declaration::Attribute(ref mut attr) => match attr {
                Attribute::Declaration(ref mut attr_decl) => {
                    if let Err(err) = self.resolve_type_mark(region, &mut attr_decl.type_mark) {
                        err.add_to(diagnostics)?;
                    }
                    region.add(&attr_decl.ident, NamedEntityKind::Attribute, diagnostics);
                }
                // @TODO Ignored for now
                Attribute::Specification(..) => {}
            },
            Declaration::SubprogramBody(ref mut body) => {
                region.add(
                    body.specification.designator(),
                    match body.specification {
                        SubprogramDeclaration::Procedure(..) => NamedEntityKind::Procedure,
                        SubprogramDeclaration::Function(..) => NamedEntityKind::Function,
                    },
                    diagnostics,
                );
                let mut subpgm_region = region.nested();

                self.analyze_subprogram_declaration(
                    &mut subpgm_region,
                    &mut body.specification,
                    diagnostics,
                )?;
                self.analyze_declarative_part(
                    &mut subpgm_region,
                    &mut body.declarations,
                    diagnostics,
                )?;
                subpgm_region.close(diagnostics);
                self.analyze_sequential_part(
                    &mut subpgm_region,
                    &mut body.statements,
                    diagnostics,
                )?;
            }
            Declaration::SubprogramDeclaration(ref mut subdecl) => {
                region.add(
                    subdecl.designator(),
                    match subdecl {
                        SubprogramDeclaration::Procedure(..) => NamedEntityKind::Procedure,
                        SubprogramDeclaration::Function(..) => NamedEntityKind::Function,
                    },
                    diagnostics,
                );

                let mut subpgm_region = region.nested();
                self.analyze_subprogram_declaration(&mut subpgm_region, subdecl, diagnostics)?;
                subpgm_region.close(diagnostics);
            }

            Declaration::Use(ref mut use_clause) => {
                self.analyze_use_clause(region, &mut use_clause.item, diagnostics)?;
            }

            Declaration::Package(ref mut instance) => {
                match self.analyze_package_instance_name(region, &mut instance.package_name) {
                    Ok(package_region) => region.add(
                        &instance.ident,
                        NamedEntityKind::LocalPackageInstance(package_region),
                        diagnostics,
                    ),
                    Err(err) => err.add_to(diagnostics)?,
                }
            }
            Declaration::Configuration(..) => {}
            Declaration::Type(..) => unreachable!("Handled elsewhere"),
        };

        Ok(())
    }

    fn analyze_type_declaration(
        &self,
        parent: &mut Region<'_>,
        type_decl: &mut TypeDeclaration,
        // Is the full type declaration of an incomplete type
        // Overwrite id when defining full type
        overwrite_id: Option<EntityId>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match type_decl.def {
            TypeDefinition::Enumeration(ref enumeration) => {
                let mut implicit = Vec::with_capacity(enumeration.len());

                for literal in enumeration.iter() {
                    let literal_ent = NamedEntity::new(
                        literal.item.clone().into_designator(),
                        NamedEntityKind::EnumLiteral,
                        Some(&literal.pos),
                    );
                    let literal_ent = Arc::new(literal_ent);
                    implicit.push(literal_ent.clone());
                    parent.add_named_entity(literal_ent, diagnostics);
                }

                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(implicit),
                    overwrite_id,
                    diagnostics,
                );
            }
            TypeDefinition::ProtectedBody(ref mut body) => {
                body.type_reference.clear_reference();

                match parent.lookup_immediate(&type_decl.ident.item.clone().into()) {
                    Some(visible) => {
                        let is_ok = match visible.clone().into_non_overloaded() {
                            Ok(ent) => {
                                if let NamedEntityKind::ProtectedType(ptype_region) = ent.kind() {
                                    body.type_reference.set_unique_reference(&ent);
                                    let mut region = Region::extend(&ptype_region, Some(parent));
                                    self.analyze_declarative_part(
                                        &mut region,
                                        &mut body.decl,
                                        diagnostics,
                                    )?;
                                    parent.add_protected_body(type_decl.ident.clone(), diagnostics);
                                    true
                                } else {
                                    false
                                }
                            }
                            _ => false,
                        };

                        if !is_ok {
                            diagnostics.push(Diagnostic::error(
                                type_decl.ident.pos(),
                                format!("'{}' is not a protected type", &type_decl.ident.item),
                            ));
                        }
                    }
                    None => {
                        diagnostics.push(Diagnostic::error(
                            type_decl.ident.pos(),
                            format!(
                                "No declaration of protected type '{}'",
                                &type_decl.ident.item
                            ),
                        ));
                    }
                };
            }
            TypeDefinition::Protected(ref mut prot_decl) => {
                // Protected type name is visible inside its declarative region
                // This will be overwritten later when the protected type region is finished
                let id = add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(Vec::new()),
                    overwrite_id,
                    diagnostics,
                );

                let mut region = parent.nested();
                for item in prot_decl.items.iter_mut() {
                    match item {
                        ProtectedTypeDeclarativeItem::Subprogram(ref mut subprogram) => {
                            region.add(
                                subprogram.designator(),
                                match subprogram {
                                    SubprogramDeclaration::Procedure(..) => {
                                        NamedEntityKind::Procedure
                                    }
                                    SubprogramDeclaration::Function(..) => {
                                        NamedEntityKind::Function
                                    }
                                },
                                diagnostics,
                            );
                            let mut subpgm_region = region.nested();
                            self.analyze_subprogram_declaration(
                                &mut subpgm_region,
                                subprogram,
                                diagnostics,
                            )?;
                            subpgm_region.close(diagnostics);
                        }
                    }
                }
                let region = region.without_parent();

                parent.overwrite(NamedEntity::new_with_id(
                    id,
                    type_decl.ident.name().clone(),
                    NamedEntityKind::ProtectedType(Arc::new(region)),
                    Some(type_decl.ident.pos()),
                ));
            }
            TypeDefinition::Record(ref mut element_decls) => {
                let mut region = Region::default();
                for elem_decl in element_decls.iter_mut() {
                    self.analyze_subtype_indication(parent, &mut elem_decl.subtype, diagnostics)?;
                    region.add(&elem_decl.ident, NamedEntityKind::RecordField, diagnostics);
                }
                region.close(diagnostics);

                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(Vec::new()),
                    overwrite_id,
                    diagnostics,
                );
            }
            TypeDefinition::Access(ref mut subtype_indication) => {
                self.analyze_subtype_indication(parent, subtype_indication, diagnostics)?;

                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(Vec::new()),
                    overwrite_id,
                    diagnostics,
                );
            }
            TypeDefinition::Array(ref mut array_indexes, ref mut subtype_indication) => {
                for index in array_indexes.iter_mut() {
                    self.analyze_array_index(parent, index, diagnostics)?;
                }
                self.analyze_subtype_indication(parent, subtype_indication, diagnostics)?;

                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(Vec::new()),
                    overwrite_id,
                    diagnostics,
                );
            }
            TypeDefinition::Subtype(ref mut subtype_indication) => {
                match self.resolve_subtype_indication(parent, subtype_indication, diagnostics) {
                    Ok(subtype) => {
                        add_or_overwrite(
                            parent,
                            &type_decl.ident,
                            NamedEntityKind::Subtype(subtype),
                            overwrite_id,
                            diagnostics,
                        );
                    }
                    Err(err) => {
                        err.add_to(diagnostics)?;
                    }
                }
            }
            TypeDefinition::Physical(ref mut physical) => {
                parent.add(
                    physical.primary_unit.clone(),
                    NamedEntityKind::PhysicalLiteral,
                    diagnostics,
                );
                for (secondary_unit_name, _) in physical.secondary_units.iter_mut() {
                    parent.add(
                        secondary_unit_name.clone(),
                        NamedEntityKind::PhysicalLiteral,
                        diagnostics,
                    )
                }

                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(Vec::new()),
                    overwrite_id,
                    diagnostics,
                );
            }
            TypeDefinition::Incomplete(..) => {
                unreachable!("Handled elsewhere");
            }

            TypeDefinition::Integer(ref mut range) => {
                self.analyze_range(parent, range, diagnostics)?;
                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(Vec::new()),
                    overwrite_id,
                    diagnostics,
                );
            }

            TypeDefinition::File(ref mut type_mark) => {
                if let Err(err) = self.resolve_type_mark(parent, type_mark) {
                    err.add_to(diagnostics)?;
                }

                let names = ["file_open", "file_close", "endfile"];
                let mut implicit = Vec::with_capacity(names.len());
                for name in names.iter() {
                    let ent = NamedEntity::new(
                        Designator::Identifier(self.symbol_utf8(name)),
                        NamedEntityKind::Procedure,
                        None,
                    );
                    implicit.push(Arc::new(ent));
                }
                add_or_overwrite(
                    parent,
                    &type_decl.ident,
                    NamedEntityKind::TypeDeclaration(implicit),
                    overwrite_id,
                    diagnostics,
                );
            }
        }

        Ok(())
    }

    pub fn analyze_signature(
        &self,
        region: &Region<'_>,
        signature: &mut WithPos<Signature>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match &mut signature.item {
            Signature::Function(ref mut args, ref mut ret) => {
                for arg in args.iter_mut() {
                    if let Err(err) = self.resolve_selected_name(region, arg) {
                        err.add_to(diagnostics)?;
                    }
                }
                if let Err(err) = self.resolve_selected_name(region, ret) {
                    err.add_to(diagnostics)?;
                }
            }
            Signature::Procedure(args) => {
                for arg in args.iter_mut() {
                    if let Err(err) = self.resolve_selected_name(region, arg) {
                        err.add_to(diagnostics)?;
                    }
                }
            }
        }
        Ok(())
    }

    fn analyze_interface_declaration(
        &self,
        region: &mut Region<'_>,
        decl: &mut InterfaceDeclaration,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match decl {
            InterfaceDeclaration::File(ref mut file_decl) => {
                self.analyze_subtype_indication(
                    region,
                    &mut file_decl.subtype_indication,
                    diagnostics,
                )?;
                region.add(
                    &file_decl.ident,
                    NamedEntityKind::InterfaceFile,
                    diagnostics,
                );
            }
            InterfaceDeclaration::Object(ref mut object_decl) => {
                let subtype = self.resolve_subtype_indication(
                    region,
                    &mut object_decl.subtype_indication,
                    diagnostics,
                );

                if let Some(ref mut expression) = object_decl.expression {
                    self.analyze_expression(region, expression, diagnostics)?
                }

                let subtype = ok_or_return!(subtype, diagnostics);

                region.add(
                    &object_decl.ident,
                    NamedEntityKind::InterfaceObject(InterfaceObject {
                        class: object_decl.class,
                        mode: object_decl.mode,
                        subtype,
                        has_default: object_decl.expression.is_some(),
                    }),
                    diagnostics,
                );
            }
            InterfaceDeclaration::Type(ref ident) => {
                region.add(ident, NamedEntityKind::InterfaceType, diagnostics);
            }
            InterfaceDeclaration::Subprogram(ref mut subpgm, ..) => {
                let mut subpgm_region = region.nested();
                self.analyze_subprogram_declaration(&mut subpgm_region, subpgm, diagnostics)?;
                subpgm_region.close(diagnostics);
                region.add(
                    subpgm.designator(),
                    match subpgm {
                        SubprogramDeclaration::Procedure(..) => NamedEntityKind::Procedure,
                        SubprogramDeclaration::Function(..) => NamedEntityKind::Function,
                    },
                    diagnostics,
                );
            }
            InterfaceDeclaration::Package(ref mut instance) => {
                match self.analyze_package_instance_name(region, &mut instance.package_name) {
                    Ok(package_region) => region.add(
                        &instance.ident,
                        NamedEntityKind::LocalPackageInstance(package_region.clone()),
                        diagnostics,
                    ),
                    Err(err) => {
                        err.add_to(diagnostics)?;
                    }
                }
            }
        };
        Ok(())
    }

    pub fn analyze_interface_list(
        &self,
        region: &mut Region<'_>,
        declarations: &mut [InterfaceDeclaration],
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        for decl in declarations.iter_mut() {
            self.analyze_interface_declaration(region, decl, diagnostics)?;
        }
        Ok(())
    }

    fn analyze_array_index(
        &self,
        region: &mut Region<'_>,
        array_index: &mut ArrayIndex,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match array_index {
            ArrayIndex::IndexSubtypeDefintion(ref mut type_mark) => {
                if let Err(err) = self.resolve_type_mark(region, type_mark) {
                    err.add_to(diagnostics)?;
                }
            }
            ArrayIndex::Discrete(ref mut drange) => {
                self.analyze_discrete_range(region, drange, diagnostics)?;
            }
        }
        Ok(())
    }
    fn analyze_element_constraint(
        &self,
        region: &Region<'_>,
        constraint: &mut ElementConstraint,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        // @TODO more
        let ElementConstraint { constraint, .. } = constraint;
        self.analyze_subtype_constraint(region, &mut constraint.item, diagnostics)
    }

    fn analyze_subtype_constraint(
        &self,
        region: &Region<'_>,
        constraint: &mut SubtypeConstraint,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match constraint {
            SubtypeConstraint::Array(ref mut dranges, ref mut constraint) => {
                for drange in dranges.iter_mut() {
                    self.analyze_discrete_range(region, drange, diagnostics)?;
                }
                if let Some(constraint) = constraint {
                    self.analyze_subtype_constraint(region, &mut constraint.item, diagnostics)?;
                }
            }
            SubtypeConstraint::Range(ref mut range) => {
                self.analyze_range(region, range, diagnostics)?;
            }
            SubtypeConstraint::Record(ref mut constraints) => {
                for constraint in constraints.iter_mut() {
                    self.analyze_element_constraint(region, constraint, diagnostics)?;
                }
            }
        }
        Ok(())
    }

    pub fn resolve_subtype_indication(
        &self,
        region: &Region<'_>,
        subtype_indication: &mut SubtypeIndication,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> AnalysisResult<Subtype> {
        // @TODO more
        let SubtypeIndication {
            type_mark,
            constraint,
            ..
        } = subtype_indication;

        let base_type = self.resolve_type_mark(region, type_mark)?;

        if let Some(constraint) = constraint {
            self.analyze_subtype_constraint(region, &mut constraint.item, diagnostics)?;
        }

        Ok(Subtype::new(base_type))
    }

    pub fn analyze_subtype_indication(
        &self,
        region: &Region<'_>,
        subtype_indication: &mut SubtypeIndication,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        if let Err(err) = self.resolve_subtype_indication(region, subtype_indication, diagnostics) {
            err.add_to(diagnostics)?;
        }
        Ok(())
    }

    fn analyze_subprogram_declaration(
        &self,
        region: &mut Region<'_>,
        subprogram: &mut SubprogramDeclaration,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match subprogram {
            SubprogramDeclaration::Function(fun) => {
                self.analyze_interface_list(region, &mut fun.parameter_list, diagnostics)?;
                if let Err(err) = self.resolve_type_mark(region, &mut fun.return_type) {
                    err.add_to(diagnostics)?
                }
            }
            SubprogramDeclaration::Procedure(procedure) => {
                self.analyze_interface_list(region, &mut procedure.parameter_list, diagnostics)?;
            }
        }
        Ok(())
    }
}

fn find_full_type_definition<'a>(
    name: &Symbol,
    decls: &'a [Declaration],
) -> Option<&'a TypeDeclaration> {
    for decl in decls.iter() {
        if let Declaration::Type(type_decl) = decl {
            match type_decl.def {
                TypeDefinition::Incomplete(..) => {
                    // ignored
                }
                _ => {
                    if type_decl.ident.name() == name {
                        return Some(type_decl);
                    }
                }
            }
        }
    }
    None
}

fn add_or_overwrite(
    region: &mut Region,
    name: &Ident,
    kind: NamedEntityKind,
    old_id: Option<EntityId>,
    diagnostics: &mut dyn DiagnosticHandler,
) -> EntityId {
    if let Some(id) = old_id {
        let ent = NamedEntity::new_with_id(id, name.name().clone(), kind, Some(name.pos()));
        region.overwrite(ent);
        id
    } else {
        let ent = NamedEntity::new(name.name().clone(), kind, Some(&name.pos));
        let id = ent.id();
        region.add_named_entity(Arc::new(ent), diagnostics);
        id
    }
}