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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
// This Source Code Form is subject to the terms of the Mozilla Public
// Lic// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// This Source Code Form is subject to the terms of the Mozilla Public
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2022, Olof Kraigher olof.kraigher@gmail.com
use super::*;
use crate::VHDLStandard::VHDL2019;
use pretty_assertions::assert_eq;
use vhdl_lang::data::error_codes::ErrorCode;

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

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
   signal sig : boolean;
begin
   ent: entity work.ent_inst
      port map (missing => sig);
end architecture;
        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![Diagnostic::new(
            code.s1("missing"),
            "No declaration of 'missing'",
            ErrorCode::Unresolved,
        )],
    );
}

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

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
   signal sig : boolean;
begin
   ent: entity work.ent_inst
      generic map (missing => sig);
end architecture;
        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![Diagnostic::new(
            code.s1("missing"),
            "No declaration of 'missing'",
            ErrorCode::Unresolved,
        )],
    );
}

#[test]
fn resolve_port_name() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent_inst is
    port (
        theport : in boolean 
    );
end entity;

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
   signal sig : boolean;
begin
   ent: entity work.ent_inst
      port map (theport => sig);
end architecture;
        ",
    );

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

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("theport", 2).end()),
        Some(code.s1("theport").pos())
    );
}

#[test]
fn resolve_generic_name() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent_inst is
    generic (
        thegeneric : boolean 
    );
end entity;

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
begin
   ent: entity work.ent_inst
      generic map (thegeneric => false);
end architecture;
        ",
    );

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

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("thegeneric", 2).end()),
        Some(code.s1("thegeneric").pos())
    );
}

#[test]
fn does_not_mixup_ports_and_generics() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent_inst is
    generic (
        thegeneric : boolean 
    );
    port (
        theport : in boolean 
    );
end entity;

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
   signal sig : boolean;
begin
   ent: entity work.ent_inst
       generic map (theport => sig)
       port map (thegeneric => 0);
end architecture;
        ",
    );

    let diagnostics = builder.analyze();
    check_diagnostics(
        diagnostics,
        vec![
            Diagnostic::new(
                code.s("theport", 2),
                "No declaration of 'theport'",
                ErrorCode::Unresolved,
            ),
            Diagnostic::new(
                code.s1("work.ent_inst"),
                "No association of port 'theport' : in",
                ErrorCode::Unassociated,
            )
            .related(code.s1("theport"), "Defined here"),
            Diagnostic::new(
                code.s("thegeneric", 2),
                "No declaration of 'thegeneric'",
                ErrorCode::Unresolved,
            ),
            Diagnostic::new(
                code.s1("work.ent_inst"),
                "No association of generic 'thegeneric'",
                ErrorCode::Unassociated,
            )
            .related(code.s1("thegeneric"), "Defined here"),
        ],
    );
}

#[test]
fn resolve_port_and_surrounding_name() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent_inst is
    port (
        theport : in integer_vector(0 to 0) 
    );
end entity;

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
    constant const0 : natural := 0;
begin
   ent: entity work.ent_inst
      port map (theport(const0) => 0);
end architecture;
        ",
    );

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

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("theport", 2).end()),
        Some(code.s1("theport").pos())
    );

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("const0", 2).end()),
        Some(code.s1("const0").pos())
    );
}

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

architecture a of ent is
begin
end architecture;

entity ent2 is
end entity;

architecture a of ent2 is    
    function fun1(arg : natural) return natural is
    begin
        return arg;
    end function;

    signal sig : natural;
begin
    inst: entity work.ent
        port map (
        fun1(theport) => sig);
end architecture;
        ",
    );

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

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("fun1", 2).end()),
        Some(code.s1("fun1").pos())
    );

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("theport", 2).end()),
        Some(code.s1("theport").pos())
    );
}

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

architecture a of ent is
begin
end architecture;

entity ent2 is
end entity;

architecture a of ent2 is
    signal sig : real;
begin
    inst: entity work.ent
        port map (
        real(theport) => sig);
end architecture;
        ",
    );

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

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("theport", 2).end()),
        Some(code.s1("theport").pos())
    );
}

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

architecture a of ent is
begin
end architecture;

entity ent2 is
end entity;

architecture a of ent2 is    
    function fun1(arg : natural; arg2: natural) return natural is
    begin
        return arg;
    end function;

    signal sig : natural;
begin
    inst: entity work.ent
        port map (
        fun1(theport, 2) => sig);
end architecture;
        ",
    );

    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("fun1(theport, 2)"),
            "Invalid formal conversion",
            ErrorCode::InvalidFormalConversion,
        )],
    );
}

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

architecture a of ent is
begin
end architecture;

entity ent2 is
end entity;

architecture a of ent2 is    
    function fun1(arg : natural) return natural is
    begin
        return arg;
    end function;

    signal sig : natural;
begin
    inst: entity work.ent
        port map (
        fun1(arg => theport) => sig);
end architecture;
        ",
    );

    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("fun1(arg => theport)"),
            "Invalid formal conversion",
            ErrorCode::InvalidFormalConversion,
        )],
    );
}

#[test]
fn conversion_of_sliced_formal() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity module is
  port (
    theport : out bit_vector(0 to 5)
  );
end;

architecture a of module is
begin
end architecture;

entity ent is
end;

architecture behav of ent is
    signal data0 : bit_vector(0 to 5);
    signal data1 : bit_vector(0 to 5);
  begin

  inst0: entity work.module
    port map (
      bit_vector(theport(data0'range)) => data0
    );

  inst1: entity work.module
    port map (
      bit_vector(theport(0 to 5)) => data1
    );
end;",
    );

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

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("theport", 2).end()),
        Some(code.s1("theport").pos())
    );

    assert_eq!(
        root.search_reference_pos(code.source(), code.s("theport", 3).end()),
        Some(code.s1("theport").pos())
    );
}

#[test]
fn output_ports_may_be_left_open() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent2 is
port (
    signal inport: in natural;
    signal outport: out natural  );
end entity;

architecture a of ent2 is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
    signal sig : natural;
begin
    inst: entity work.ent2
        port map (
        inport => sig
        );
end architecture;
    ",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);
    // Still resolves references when missing output port
    assert!(root
        .search_reference(code.source(), code.s1("inport => sig").s1("sig").start())
        .is_some())
}

#[test]
fn does_not_stop_on_first_error() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent_inst is
  port (
    prt0 : in boolean;
    prt1 : in boolean
  );
end entity;

architecture a of ent_inst is
begin
end architecture;

entity ent is
end entity;

architecture a of ent is
   signal sig0, sig1 : boolean;
begin
   ent: entity work.ent_inst
      port map (
        missing => sig0,
        prt1 => sig1);
end architecture;
        ",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_diagnostics(
        diagnostics,
        vec![
            Diagnostic::new(
                code.s1("missing"),
                "No declaration of 'missing'",
                ErrorCode::Unresolved,
            ),
            Diagnostic::new(
                code.s1("work.ent_inst"),
                "No association of port 'prt0' : in",
                ErrorCode::Unassociated,
            )
            .related(code.s1("prt0"), "Defined here"),
        ],
    );

    // Still sets the reference even if it fails
    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("=> sig0").s1("sig0").pos().start())
            .unwrap(),
        code.s1("sig0").pos()
    );
    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("=> sig1").s1("sig1").pos().start())
            .unwrap(),
        code.s1("sig1").pos()
    );
    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("prt1 => ").s1("prt1").pos().start())
            .unwrap(),
        code.s1("prt1").pos()
    );
}

#[test]
fn view_array_with_explicit_type() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "
package test_pkg is
    type test_t is record
        foo : bit;
    end record;
    
    view vone of test_t is
        foo : in;
    end view;

    type test_array is array (natural range <>) of test_t;
end package;

use work.test_pkg.all;

entity test_sub_entity is
    port (
        my_array_if: view (vone) of test_array(1 downto 0)
    );
end entity;

architecture arch of test_sub_entity is
begin
    my_array_if(0).foo <= '1';
end architecture;
        ",
    );
    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("test_array(1 downto 0)").start())
            .unwrap(),
        code.s1("test_array").pos()
    );

    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("my_array_if(0).foo").end())
            .unwrap(),
        code.s1("foo").pos()
    );
}

#[test]
fn view_array_without_explicit_type() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "
package test_pkg is
    type test_t is record
        foo : bit;
    end record;
    
    view vone of test_t is
        foo : in;
    end view;
end package;

use work.test_pkg.all;

entity test_sub_entity is
    port (
        my_array_if: view (vone)
    );
end entity;

architecture arch of test_sub_entity is
begin
    my_array_if(0).foo <= '1';
end architecture;
        ",
    );
    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);

    assert_eq!(
        root.search_reference_pos(code.source(), code.s1("my_array_if(0).foo").end())
            .unwrap(),
        code.s1("foo").pos()
    );
}

#[test]
fn view_array_with_non_array_type() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "
package test_pkg is
    type test_t is record
        foo : natural;
    end record;
    
    view vone of test_t is
        foo : in;
    end view;
end package;

use work.test_pkg.all;

entity test_sub_entity is
    port (
        my_array_if: view (vone) of bit
    );
end entity;
        ",
    );
    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("bit"),
            "Subtype must be an array",
            ErrorCode::TypeMismatch,
        )],
    );
}

#[test]
fn view_array_with_mismatched_element_type() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "
package test_pkg is
    type test_t is record
        foo : natural;
    end record;

    view vone of test_t is
        foo : in;
    end view;
end package;

use work.test_pkg.all;

entity test_sub_entity is
    port (
        my_array_if: view (vone) of bit_vector
    );
end entity;
        ",
    );
    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("bit_vector"),
            "Array element type 'BIT' must match record type 'test_t' declared for the view",
            ErrorCode::TypeMismatch,
        )],
    );
}

#[test]
fn generic_function_in_entity_instantiation() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
entity generic_signal_entity is
    generic (
        function active_edge(signal s: bit) return boolean
    );
end entity;

architecture behavioural of generic_signal_entity is
begin
end architecture;

entity test is
end entity;

architecture behavioural of test is
    component generic_signal_component is
    generic (
        function active_edge(signal s: bit) return boolean
    );
    end component;
begin
    generic_signal_entity_inst: entity work.generic_signal_entity
        generic map (
            active_edge => rising_edge
        );

    generic_signal_component_inst: generic_signal_component
        generic map (
            active_edge => rising_edge
        );
end architecture;
        ",
    );
    check_no_diagnostics(&builder.analyze());
}

#[test]
fn generic_type_from_port_in_map() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
entity foo is
    generic (
        type t
    );
    port (
        clk: in t
    );
end entity foo;

architecture rtl of foo is
begin
end architecture;

entity test is
end entity test;

architecture rtl of test is
begin
    foo_inst: entity work.foo
    generic map(
        t => bit
    )
    port map(
        clk => '1'
    );
end architecture;
        ",
    );
    check_no_diagnostics(&builder.analyze());
}