vhdl_parser 0.13.0

VHDL Parser
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
// 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 region::*;
use root::*;
use semantic::{uninstantiated_package_prefix_error, LookupResult};
use std::sync::Arc;

impl<'a> AnalyzeContext<'a> {
    pub fn analyze_design_unit(
        &self,
        unit: &mut AnyDesignUnit,
        root_region: &mut Region<'_>,
        region: &mut Region<'_>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        match unit {
            AnyDesignUnit::Primary(unit) => match unit {
                AnyPrimaryUnit::Entity(unit) => {
                    self.analyze_entity(unit, root_region, region, diagnostics)
                }
                AnyPrimaryUnit::Configuration(unit) => {
                    self.analyze_configuration(unit, diagnostics)
                }
                AnyPrimaryUnit::Package(unit) => {
                    self.analyze_package(unit, root_region, region, diagnostics)
                }
                AnyPrimaryUnit::PackageInstance(unit) => {
                    self.analyze_package_instance(unit, root_region, region, diagnostics)
                }
                AnyPrimaryUnit::Context(unit) => {
                    self.analyze_context(unit, root_region, region, diagnostics)
                }
            },
            AnyDesignUnit::Secondary(unit) => match unit {
                AnySecondaryUnit::Architecture(unit) => {
                    self.analyze_architecture(unit, diagnostics)
                }
                AnySecondaryUnit::PackageBody(unit) => self.analyze_package_body(unit, diagnostics),
            },
        }
    }

    fn analyze_entity(
        &self,
        unit: &mut EntityDeclaration,
        root_region: &mut Region<'_>,
        region: &mut Region<'_>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        *root_region = Region::default();
        self.add_implicit_context_clause(root_region)?;
        self.analyze_context_clause(root_region, &mut unit.context_clause, diagnostics)?;

        let mut primary_region = root_region.nested();

        if let Some(ref mut list) = unit.generic_clause {
            self.analyze_interface_list(&mut primary_region, list, diagnostics)?;
        }
        if let Some(ref mut list) = unit.port_clause {
            self.analyze_interface_list(&mut primary_region, list, diagnostics)?;
        }
        self.analyze_declarative_part(&mut primary_region, &mut unit.decl, diagnostics)?;
        self.analyze_concurrent_part(&mut primary_region, &mut unit.statements, diagnostics)?;

        primary_region.close_immediate(diagnostics);
        *region = primary_region.without_parent();

        Ok(())
    }

    fn analyze_configuration(
        &self,
        unit: &mut ConfigurationDeclaration,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        let mut root_region = Region::default();
        self.add_implicit_context_clause(&mut root_region)?;
        self.analyze_context_clause(&mut root_region, &mut unit.context_clause, diagnostics)?;

        match self.lookup_entity_for_configuration(&root_region, unit) {
            Ok((named_entity, entity_name, _)) => {
                if let Some(primary_pos) = named_entity.decl_pos() {
                    let secondary_pos = unit.pos();
                    if primary_pos.source == secondary_pos.source
                        && primary_pos.start() > secondary_pos.start()
                    {
                        diagnostics.push(Diagnostic::error(
                            secondary_pos,
                            format!(
                                "Configuration '{}' declared before entity '{}'",
                                &unit.name(),
                                entity_name
                            ),
                        ));
                    }
                }
            }
            Err(err) => {
                err.add_to(diagnostics)?;
            }
        };
        Ok(())
    }

    fn analyze_package(
        &self,
        unit: &mut PackageDeclaration,
        root_region: &mut Region<'_>,
        region: &mut Region<'_>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        *root_region = Region::default();
        self.add_implicit_context_clause(root_region)?;
        self.analyze_context_clause(root_region, &mut unit.context_clause, diagnostics)?;

        let mut primary_region = root_region.nested().in_package_declaration();

        if let Some(ref mut list) = unit.generic_clause {
            self.analyze_interface_list(&mut primary_region, list, diagnostics)?;
        }
        self.analyze_declarative_part(&mut primary_region, &mut unit.decl, diagnostics)?;

        if self.has_package_body() {
            primary_region.close_immediate(diagnostics);
        } else {
            primary_region.close_both(diagnostics);
        }

        *region = primary_region.without_parent();

        Ok(())
    }

    fn analyze_package_instance(
        &self,
        unit: &mut PackageInstantiation,
        root_region: &mut Region<'_>,
        region: &mut Region<'_>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        *root_region = Region::default();
        self.add_implicit_context_clause(root_region)?;
        self.analyze_context_clause(root_region, &mut unit.context_clause, diagnostics)?;

        match self.analyze_package_instance_name(root_region, &mut unit.package_name) {
            Ok(package_region) => {
                *region = (*package_region).clone();
                Ok(())
            }
            Err(AnalysisError::NotFatal(diagnostic)) => {
                diagnostics.push(diagnostic);
                Ok(())
            }
            Err(AnalysisError::Fatal(err)) => Err(err),
        }
    }

    fn analyze_context(
        &self,
        unit: &mut ContextDeclaration,
        root_region: &mut Region<'_>,
        region: &mut Region<'_>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        *root_region = Region::default();
        self.add_implicit_context_clause(root_region)?;
        let mut primary_region = root_region.nested();
        self.analyze_context_clause(&mut primary_region, &mut unit.items, diagnostics)?;
        *region = primary_region.without_parent();
        Ok(())
    }

    fn analyze_architecture(
        &self,
        unit: &mut ArchitectureBody,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        unit.entity_name.clear_reference();

        let entity = self.lookup_primary_unit(
            unit.primary_ident(),
            PrimaryKind::Entity,
            unit.pos(),
            diagnostics,
        )?;
        // @TODO maybe add more fatal results
        let entity = if let Some(entity) = entity {
            entity
        } else {
            return Ok(());
        };

        unit.entity_name.set_reference_pos(Some(entity.pos()));

        let mut root_region = Region::extend(&entity.result().root_region, None);
        self.analyze_context_clause(&mut root_region, &mut unit.context_clause, diagnostics)?;
        let mut region = Region::extend(&entity.result().region, Some(&root_region));

        // entity name is visible
        region.make_potentially_visible(
            entity.name().into(),
            NamedEntity::new(NamedEntityKind::Constant, Some(entity.pos())),
        );

        self.analyze_declarative_part(&mut region, &mut unit.decl, diagnostics)?;
        self.analyze_concurrent_part(&mut region, &mut unit.statements, diagnostics)?;
        region.close_both(diagnostics);
        Ok(())
    }

    fn analyze_package_body(
        &self,
        unit: &mut PackageBody,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        unit.ident.clear_reference();

        let package = self.lookup_primary_unit(
            unit.primary_ident(),
            PrimaryKind::Package,
            unit.pos(),
            diagnostics,
        )?;
        // @TODO maybe add more fatal results
        let package = if let Some(package) = package {
            package
        } else {
            return Ok(());
        };

        unit.ident.set_reference_pos(Some(package.pos()));
        // @TODO make pattern of primary/secondary extension
        let mut root_region = Region::extend(&package.result().root_region, None);
        self.analyze_context_clause(&mut root_region, &mut unit.context_clause, diagnostics)?;

        let mut region = Region::extend(&package.result().region, Some(&root_region));

        // Package name is visible in body
        region.make_potentially_visible(
            package.name().into(),
            NamedEntity::new(NamedEntityKind::Constant, Some(package.pos())),
        );

        self.analyze_declarative_part(&mut region, &mut unit.decl, diagnostics)?;
        region.close_both(diagnostics);
        Ok(())
    }

    fn lookup_primary_unit(
        &self,
        primary_ident: &Ident,
        primary_kind: PrimaryKind,
        secondary_pos: &SrcPos,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalResult<Option<UnitReadGuard<'a>>> {
        let unit = {
            if let Some(data) = self.get_primary_analysis(
                primary_ident.pos(),
                self.work_library_name(),
                primary_ident.name(),
                primary_kind,
            ) {
                data?
            } else {
                diagnostics.push(Diagnostic::error(
                    primary_ident.pos(),
                    format!(
                        "No {} '{}' within library '{}'",
                        primary_kind.describe(),
                        primary_ident.name(),
                        self.work_library_name()
                    ),
                ));
                return Ok(None);
            }
        };

        let primary_pos = unit.pos();
        if primary_pos.source == secondary_pos.source && primary_pos.start() > secondary_pos.start()
        {
            diagnostics.push(Diagnostic::error(
                secondary_pos,
                format!(
                    "{} declared before {} '{}'",
                    capitalize(&self.current_unit_id().describe()),
                    primary_kind.describe(),
                    unit.name()
                ),
            ));
        }

        Ok(Some(unit))
    }

    fn lookup_entity_for_configuration(
        &self,
        region: &Region<'_>,
        config: &mut ConfigurationDeclaration,
    ) -> AnalysisResult<(NamedEntity, Symbol, Arc<Region<'static>>)> {
        let ref mut ent_name = config.entity_name;

        let named_entity = {
            match ent_name.item {
                // Entitities are implicitly defined for configurations
                // configuration cfg of ent
                SelectedName::Designator(ref mut designator) => {
                    match self.lookup_in_library(
                        self.work_library_name(),
                        &ent_name.pos,
                        &designator.item,
                    ) {
                        Ok(decl) => {
                            designator.set_unique_reference(&decl);
                            decl
                        }
                        Err(err) => {
                            designator.clear_reference();
                            return Err(err);
                        }
                    }
                }

                // configuration cfg of lib.ent
                _ => {
                    if let Some(ent) = self.resolve_selected_name(&region, ent_name)?.as_unique() {
                        ent.clone()
                    } else {
                        return Err(AnalysisError::NotFatal(Diagnostic::error(
                            &ent_name,
                            format!("Does not denote an entity"),
                        )));
                    }
                }
            }
        };

        match named_entity.kind() {
            NamedEntityKind::Entity(ref unit_id, ref entity_region) => {
                if unit_id.library_name() != self.work_library_name() {
                    Err(Diagnostic::error(
                        &ent_name,
                        format!("Configuration must be within the same library '{}' as the corresponding entity", self.work_library_name()),
                    ))?
                } else {
                    Ok((
                        named_entity.clone(),
                        unit_id.primary_name().clone(),
                        entity_region.clone(),
                    ))
                }
            }
            _ => Err(Diagnostic::error(&ent_name, "does not denote an entity"))?,
        }
    }

    fn resolve_context_item_name(
        &self,
        region: &Region<'_>,
        name: &mut WithPos<Name>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> AnalysisResult<LookupResult> {
        self.resolve_name_pos(region, &name.pos, &mut name.item, false, diagnostics)
    }

    pub fn analyze_context_clause(
        &self,
        region: &mut Region<'_>,
        context_clause: &mut [WithPos<ContextItem>],
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        for context_item in context_clause.iter_mut() {
            match context_item.item {
                ContextItem::Library(LibraryClause { ref name_list }) => {
                    for library_name in name_list.iter() {
                        if self.work_sym == library_name.item {
                            diagnostics.push(Diagnostic::hint(
                                &library_name,
                                "Library clause not necessary for current working library",
                            ))
                        } else if self.has_library(&library_name.item) {
                            region.make_library_visible(
                                &library_name.item,
                                &library_name.item,
                                Some(library_name.pos().clone()),
                            );
                        } else {
                            diagnostics.push(Diagnostic::error(
                                &library_name,
                                format!("No such library '{}'", library_name.item),
                            ));
                        }
                    }
                }
                ContextItem::Use(ref mut use_clause) => {
                    self.analyze_use_clause(region, use_clause, &context_item.pos, diagnostics)?;
                }
                ContextItem::Context(ContextReference { ref mut name_list }) => {
                    for name in name_list.iter_mut() {
                        match name.item {
                            Name::Selected(..) => {}
                            _ => {
                                diagnostics.push(Diagnostic::error(
                                    &context_item.pos,
                                    "Context reference must be a selected name",
                                ));
                                continue;
                            }
                        }

                        match self.resolve_context_item_name(&region, name, diagnostics) {
                            Ok(LookupResult::Single(visible_decl)) => {
                                match visible_decl.first_kind() {
                                    // OK
                                    NamedEntityKind::Context(_, ref context_region) => {
                                        region.copy_visibility_from(context_region);
                                    }
                                    _ => {
                                        // @TODO maybe lookup should return the source position of the suffix
                                        if let Name::Selected(_, ref suffix) = name.item {
                                            diagnostics.push(Diagnostic::error(
                                                &suffix,
                                                format!(
                                                    "'{}' does not denote a context declaration",
                                                    suffix.designator()
                                                ),
                                            ));
                                        }
                                    }
                                }
                            }
                            Ok(LookupResult::AllWithin(..)) => {
                                // @TODO
                            }
                            Ok(LookupResult::NotSelected) => {
                                diagnostics.push(Diagnostic::error(
                                    &context_item.pos,
                                    "Context reference must be a selected name",
                                ));
                            }
                            Err(err) => {
                                err.add_to(diagnostics)?;
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    pub fn analyze_use_clause(
        &self,
        region: &mut Region<'_>,
        use_clause: &mut UseClause,
        use_pos: &SrcPos,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> FatalNullResult {
        for name in use_clause.name_list.iter_mut() {
            match name.item {
                Name::Selected(..) => {}
                Name::SelectedAll(..) => {}
                _ => {
                    diagnostics.push(Diagnostic::error(
                        &use_pos,
                        "Use clause must be a selected name",
                    ));
                    continue;
                }
            }

            match self.resolve_context_item_name(&region, name, diagnostics) {
                Ok(LookupResult::Single(visible)) => {
                    visible.make_potentially_visible_in(region);
                }
                Ok(LookupResult::AllWithin(visibility_pos, visible_decl)) => {
                    match visible_decl.first_kind() {
                        NamedEntityKind::Library(ref library_name) => {
                            self.use_all_in_library(&name.pos, library_name, region)?;
                        }
                        NamedEntityKind::UninstPackage(ref unit_id, ..) => {
                            diagnostics.push(uninstantiated_package_prefix_error(
                                &visibility_pos,
                                unit_id,
                            ));
                        }
                        NamedEntityKind::Package(_, ref package_region) => {
                            region.make_all_potentially_visible(package_region);
                        }
                        NamedEntityKind::PackageInstance(_, ref package_region) => {
                            region.make_all_potentially_visible(package_region);
                        }
                        NamedEntityKind::LocalPackageInstance(_, ref instance_region) => {
                            region.make_all_potentially_visible(&instance_region);
                        }
                        // @TODO handle others
                        _ => {}
                    }
                }
                Ok(LookupResult::NotSelected) => {
                    diagnostics.push(Diagnostic::error(
                        &use_pos,
                        "Use clause must be a selected name",
                    ));
                }
                Err(err) => {
                    err.add_to(diagnostics)?;
                }
            }
        }

        Ok(())
    }

    /// Returns a reference to the the uninstantiated package
    pub fn analyze_package_instance_name(
        &self,
        region: &Region<'_>,
        package_name: &mut WithPos<SelectedName>,
    ) -> AnalysisResult<Arc<Region<'static>>> {
        let decl = self.resolve_selected_name(region, package_name)?;

        if let NamedEntityKind::UninstPackage(_, ref package_region) = decl.first_kind() {
            Ok(package_region.clone())
        } else {
            Err(Diagnostic::error(
                &package_name.pos,
                format!(
                    "'{}' is not an uninstantiated generic package",
                    package_name
                ),
            ))?
        }
    }
}