vhdl_lang 0.86.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// 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) 2020, Olof Kraigher olof.kraigher@gmail.com

use super::*;
use vhdl_lang::data::error_codes::ErrorCode;

#[test]
fn secondary_units_share_root_region_and_visibility_in_extended_region() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg2 is
  constant const : natural := 0;
end package;

package pkg is
  use work.pkg2;
end package;

-- Does not work
use pkg2.const;

package body pkg is
  -- Does work, share visibility of extended region
  use pkg2.const;
end package body;
",
    );
    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![Diagnostic::new(
            code.s("pkg2", 3),
            "No declaration of 'pkg2'",
            ErrorCode::Unresolved,
        )],
    )
}

#[test]
fn immediate_region_takes_precedence_over_local_visibility() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package false_pkg is
  constant decl : natural := 0;
end package;

package pkg is
  constant decl : natural := 1;
  use work.false_pkg.decl;

  -- Should refer to constant in pkg
  constant ref : natural := decl;
end package pkg;
",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("decl", 4).start()),
        Some(code.s("decl", 2).pos())
    );
}

#[test]
fn extended_region_takes_precedence_over_local_visibility() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package false_pkg is
  constant decl : natural := 0;
end package;

package pkg is
  constant decl : natural := 1;
end package pkg;

use work.false_pkg.decl;
package body pkg is
  use work.false_pkg.decl;

  -- Should refer to constant in pkg
  constant ref : natural := decl;
end package body pkg;
",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("decl", 5).start()),
        Some(code.s("decl", 2).pos())
    );
}

#[test]
fn enclosing_region_takes_precedence_over_local_visibility() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg is
  constant decl : natural := 0;
end package;

entity tb_ent is
end entity;

architecture a of tb_ent is
  constant decl : natural := 1;
begin
  main : process
    use work.pkg.decl;
  begin
    assert decl = 1;
  end process;
end architecture;
",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("decl", 4).start()),
        Some(code.s("decl", 2).pos())
    );
}

#[test]
fn context_clause_makes_names_visible() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
-- Package will be used for testing
package usepkg is
  constant const : natural := 0;
  type enum_t is (alpha, beta);
end package;

context ctx is
  library libname;
  -- Test both used by name
  use libname.usepkg;
  -- .. as well as used by all
  use libname.usepkg.all;
end context;


context work.ctx;
use usepkg.const;

package pkg is
  constant c : enum_t := alpha;
end package;
        ",
    );

    let diagnostics = builder.analyze();

    check_no_diagnostics(&diagnostics);
}

#[test]
fn cannot_reference_potentially_visible_name_by_selection() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg2 is
  constant const1 : natural := 0;
  constant const2 : natural := 0;
end package;


use work.pkg2.const1;

package pkg is
  use work.pkg2.const2;
  constant const3 : natural := 0;
end package;

use work.pkg.const1;
use work.pkg.const2;
use work.pkg.const3;

entity ent is
end entity;
        ",
    );
    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![
            Diagnostic::new(
                code.s("const1", 3),
                "No declaration of 'const1' within package 'pkg'",
                ErrorCode::Unresolved,
            ),
            Diagnostic::new(
                code.s("const2", 3),
                "No declaration of 'const2' within package 'pkg'",
                ErrorCode::Unresolved,
            ),
        ],
    );
}

#[test]
fn duplicate_identifer_is_not_directly_visible() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg1 is
  constant name : natural := 0;
end package;

package pkg2 is
  constant name : natural := 1;
end package;

use work.pkg1.name;
use work.pkg2.name;

package user is
  constant b : natural := name;
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![hidden_error(
            &code,
            "name",
            5,
            &[
                (&code, "work.pkg1.name", 1, false),
                (&code, "name", 1, true),
                (&code, "work.pkg2.name", 1, false),
                (&code, "name", 2, true),
            ],
        )],
    );
}

#[test]
fn duplicate_identifer_visiblity_is_traced_through_context() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "lib",
        "
package pkg1 is
  constant name : natural := 0;
end package;

package pkg2 is
  constant name : natural := 1;
end package;

context ctx is
  library lib;
  use lib.pkg1.name;
end context;

context work.ctx;
use work.pkg2.name;

package user is
  constant b : natural := name;
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![hidden_error(
            &code,
            "name",
            5,
            &[
                (&code, "work.ctx", 1, false),
                (&code, "lib.pkg1.name", 1, false),
                (&code, "name", 1, true),
                (&code, "work.pkg2.name", 1, false),
                (&code, "name", 2, true),
            ],
        )],
    );
}

#[test]
fn duplicate_identifer_is_directly_visible_when_it_is_the_same_named_entitty() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
package pkg1 is
  constant name : natural := 0;
end package;

use work.pkg1.name;
use work.pkg1.name;

package user is
  constant b : natural := name;
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_no_diagnostics(&diagnostics);
}

#[test]
fn duplicate_identifer_of_parent_visibility_is_not_directly_visible() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg1 is
  constant name : natural := 0;
end package;

package pkg2 is
  constant name : natural := 1;
end package;

use work.pkg1.name;

package user is
  use work.pkg2.name;
  constant b : natural := name;
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![hidden_error(
            &code,
            "name",
            5,
            &[
                (&code, "work.pkg1.name", 1, false),
                (&code, "name", 1, true),
                (&code, "work.pkg2.name", 1, false),
                (&code, "name", 2, true),
            ],
        )],
    );
}

#[test]
fn local_is_still_visible_under_duplicate_identifer() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
package pkg1 is
  constant name : natural := 0;
end package;

package pkg2 is
  constant name : natural := 1;
end package;


package user is
  use work.pkg1.name;
  use work.pkg2.name;

  constant name : natural := 0;
  constant b : natural := name;
  procedure foo(constant b : natural := name);
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_no_diagnostics(&diagnostics);
}

#[test]
fn mixed_overloaded_names_require_disambiguation() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg is
 function fun1 return natural;
 function fun2 return natural;
end package;

package pkg2 is
  constant fun1 : boolean := false;
  constant fun2 : natural := 0;
end package;

use work.pkg.all;
use work.pkg2.all;
package user is
  -- Requires disambiguation
  constant ident : natural := fun1;
  -- Requires disambiguation
  constant ident2 : natural := fun2;
end package;
",
    );
    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![
            hidden_error(
                &code,
                "fun1",
                3,
                &[
                    (&code, "work.pkg.all", 1, false),
                    (&code, "fun1", 1, true),
                    (&code, "work.pkg2.all", 1, false),
                    (&code, "fun1", 2, true),
                ],
            ),
            hidden_error(
                &code,
                "fun2",
                3,
                &[
                    (&code, "work.pkg.all", 1, false),
                    (&code, "fun2", 1, true),
                    (&code, "work.pkg2.all", 1, false),
                    (&code, "fun2", 2, true),
                ],
            ),
        ],
    );
}

/// This was a bug during development
#[test]
fn explicit_library_std_is_not_duplicate_of_implicit() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
library std;
use std.standard.all;

package pkg1 is
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_no_diagnostics(&diagnostics);
}

/// This was a bug during development
#[test]
fn duplicate_explicit_library_is_not_duplicate() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
package pkg is
end package;

context ctx is
  library libname;
  use libname.pkg;
end context;

library libname;
context libname.ctx;
use libname.pkg;

package user is
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_no_diagnostics(&diagnostics);
}

pub fn hidden_error(
    code: &Code,
    name: &str,
    occ: usize,
    related: &[(&Code, &str, usize, bool)],
) -> Diagnostic {
    let mut error = Diagnostic::new(
        code.s(name, occ),
        format!("Name '{name}' is hidden by conflicting use clause"),
        ErrorCode::ConflictingUseClause,
    );

    for (code, substr, occ, declared) in related.iter() {
        if *declared {
            error.add_related(
                code.s(substr, *occ),
                format!("Conflicting name '{name}' declared here"),
            )
        } else {
            error.add_related(
                code.s(substr, *occ),
                format!("Conflicting name '{name}' made visible here"),
            )
        }
    }

    error
}

#[test]
fn duplicate_alias_is_not_directly_visible() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg1 is
  type enum_t is (alpha, beta);
  alias alias_t is enum_t;
end package;

package pkg2 is
  type enum_t is (alpha, beta);
  alias alias_t is enum_t;
end package;

use work.pkg1.alias_t;
use work.pkg2.alias_t;

package user is
  constant b : alias_t := alpha;
end package;

        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![hidden_error(
            &code,
            "alias_t",
            5,
            &[
                (&code, "work.pkg1.alias_t", 1, false),
                (&code, "alias_t", 1, true),
                (&code, "work.pkg2.alias_t", 1, false),
                (&code, "alias_t", 2, true),
            ],
        )],
    );
}

/// It is more convenient for a language server user to goto-declaration for
/// the alias rather than going directly to the declaration without knowing about the visible alias
/// The user can always navigate from the alias to the original declaration if she desires
#[test]
fn deepest_alias_is_visible() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg is
  type enum_t is (alpha, beta);
end package;

package pkg2 is
  alias enum_t is work.pkg.enum_t;
end package;

package pkg3 is
  alias enum_t is work.pkg2.enum_t;
end package;

use work.pkg2.enum_t;
use work.pkg3.enum_t;
use work.pkg.enum_t;

package pkg4 is
  constant c : enum_t := alpha;
end package;
",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    let ref_pos = code.s1("constant c : enum_t := alpha").s1("enum_t");
    let deepest_pos = code.s1("alias enum_t is work.pkg2.enum_t").s1("enum_t");

    assert_eq!(
        root.search_reference_pos(code.source(), ref_pos.start()),
        Some(deepest_pos.pos())
    );
}

/// Using an overloaded name should not conflict with an immediate declaration if they
/// have different signatures
#[test]
fn non_conflicting_used_names_are_still_visible_in_prescence_of_immediate() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg1 is
  type enum1_t is (alpha, beta);
end package;

use work.pkg1.enum1_t;

package user is
  type enum2_t is (alpha, beta);
  constant a : enum1_t := alpha;
  constant b : enum2_t := alpha;
end package;
",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    let alpha1_pos = code.s("alpha", 1).pos();
    let alpha1_ref = code.s("alpha", 3).pos();
    assert_eq!(
        root.search_reference_pos(code.source(), alpha1_ref.start()),
        Some(alpha1_pos.clone())
    );

    let alpha2_pos = code.s("alpha", 2).pos();
    let alpha2_ref = code.s("alpha", 4).pos();
    assert_eq!(
        root.search_reference_pos(code.source(), alpha2_ref.start()),
        Some(alpha2_pos.clone())
    );

    assert_eq_unordered(
        &root.find_all_references_pos(&alpha1_pos),
        &[alpha1_pos, alpha1_ref],
    );
    assert_eq_unordered(
        &root.find_all_references_pos(&alpha2_pos),
        &[alpha2_pos, alpha2_ref],
    );
}

#[test]
fn nested_subprogram_shadows_outer() {
    let mut builder = LibraryBuilder::new();
    let code = builder.in_declarative_region(
        "
procedure theproc(arg: character) is
begin
end procedure;

function thefun(arg: integer) return natural is
    procedure theproc(arg: integer) is
    begin
        theproc('c');
    end procedure;
begin
    theproc(arg);
    return 0;
end function;
",
    );
    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("theproc('c')").start()),
        Some(code.s1("theproc").pos())
    );

    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("theproc(arg)").start()),
        Some(code.s("theproc", 2).pos())
    );
}

#[test]
fn labels_are_visible_in_declarative_region() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent is
end entity;

architecture a of ent is
  attribute debug : boolean;
  attribute debug of main : label is true;
begin
  main: process
  begin
      wait;
  end process;
end architecture;

",
    );
    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);
    let label = root
        .search_reference(code.source(), code.sa("debug of ", "main").start())
        .unwrap();
    assert_eq!(label.decl_pos(), Some(&code.sb("main", ": process").pos()));
}

#[test]
fn sequential_labels_are_visible_in_declarative_region() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent is
end entity;

architecture a of ent is
begin
  process
    attribute debug : boolean;
    attribute debug of main : label is true;
  begin
    main: for i in 0 to 1 loop
    end loop;
    wait;
  end process;
end architecture;
",
    );
    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);
    let label = root
        .search_reference(code.source(), code.sa("debug of ", "main").start())
        .unwrap();
    assert_eq!(label.decl_pos(), Some(&code.sb("main", ": for i ").pos()));
}

#[test]
fn generics_are_visible_in_procedures_but_not_outside() {
    let mut builder = LibraryBuilder::new();
    let code = builder.in_declarative_region(
        "\
    procedure swap
      generic ( type T )
      parameter (a, b : inout T) is
      variable temp : T;
    begin
      temp := a;
      a := b;
      b := temp;
    end procedure swap;
    shared variable temp2: T;
    ",
    );
    let (_, diagnostics) = builder.get_analyzed_root();
    assert_eq!(
        diagnostics,
        vec![Diagnostic::new(
            code.s("T", 4),
            "No declaration of 'T'",
            ErrorCode::Unresolved
        )]
    )
}