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
use crate::analysis::tests::{check_diagnostics, check_no_diagnostics, LibraryBuilder};
use crate::data::ErrorCode;
use crate::Diagnostic;
use crate::VHDLStandard::VHDL2019;
use pretty_assertions::assert_eq;

#[test]
pub fn view_mode_declaration_must_have_declared_subtype() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
view my_view of undeclared is
end view;
    ",
    );
    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("undeclared"),
            "No declaration of 'undeclared'",
            ErrorCode::Unresolved,
        )],
    )
}

#[test]
pub fn view_mode_declaration_must_have_record_as_type() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is (A, B);

view my_view of foo is
end view;
    ",
    );
    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s("foo", 2),
            "The type of a view must be a record type, not type 'foo'",
            ErrorCode::TypeMismatch,
        )
        .related(code.s1("foo"), "type 'foo' declared here")],
    );

    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    builder.in_declarative_region(
        "\
type foo is record
  x: bit;
end record;

view my_view of foo is
  x : in;
end view;
    ",
    );
    check_no_diagnostics(&builder.analyze());
}

#[test]
pub fn element_in_view_that_is_not_in_record() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is record
    bar: bit;
end record;

view my_view of foo is
    baz: in;
    bar: out;
end view;
    ",
    );
    let (_, diag) = builder.get_analyzed_root();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("baz"),
            "Not a part of record type 'foo'",
            ErrorCode::Unresolved,
        )],
    );
}

#[test]
pub fn view_reference_set_to_the_original_element() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is record
    bar: bit;
end record;

view my_view of foo is
    bar: in;
end view;
    ",
    );
    let (root, diag) = builder.get_analyzed_root();
    check_no_diagnostics(&diag);
    let record_element = root
        .search_reference(code.source(), code.s1("bar: bit").s1("bar").start())
        .unwrap();
    let view_element = root
        .search_reference(code.source(), code.s1("bar: in").s1("bar").start())
        .unwrap();
    assert_eq!(record_element, view_element)
}

#[test]
pub fn diagnostic_when_elements_are_missing() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is record
    bar: bit;
    baz: bit;
end record;

view my_view of foo is
    bar: in;
end view;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("my_view"),
            "Missing association of element 'baz'",
            ErrorCode::Unassociated,
        )],
    );

    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is record
    bar: bit;
    baz: bit;
    foobar: bit;
end record;

view my_view of foo is
    bar: in;
end view;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("my_view"),
            "Missing association of elements 'baz' and 'foobar'",
            ErrorCode::Unassociated,
        )],
    );
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is record
    bar: bit;
    baz: bit;
    foobar: bit;
    foobaz: bit;
end record;

view my_view of foo is
    bar: in;
end view;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("my_view"),
            "Missing association of elements 'baz', 'foobar' and 'foobaz'",
            ErrorCode::Unassociated,
        )],
    );

    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.in_declarative_region(
        "\
type foo is record
    a, b, c, d, e, f, g, h: bit;
end record;

view my_view of foo is
    a: in;
end view;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("my_view"),
            "Missing association of elements 'b', 'c', 'd' and 4 more",
            ErrorCode::Unassociated,
        )],
    );
}

#[test]
pub fn view_interface_declarations_must_be_views() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
entity my_ent is
port (
    foo: view not_declared
);
end entity;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("not_declared"),
            "No declaration of 'not_declared'",
            ErrorCode::Unresolved,
        )],
    );

    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
    entity my_ent is
    port (
        foo: view bit
    );
    end entity;
        ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::mismatched_kinds(
            code.s1("bit"),
            "type 'BIT' is not a view",
        )],
    );

    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    builder.code(
        "libname",
        "\
package x is
    type bar is record
        x: bit;
    end bar;
     
    view foo of bar is
        x: in;
    end view;
end x;

use work.x.all;

entity my_ent is
port (
    foo: view foo
);
end entity;
    ",
    );
    let diag = builder.analyze();
    check_no_diagnostics(&diag);
}

#[test]
pub fn view_interface_declaration_subtype_must_match_declared_subtype() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
package x is
    type bar is record
        x: bit;
    end bar;
     
    view foo of bar is
        x: in;
    end view;
end x;

use work.x.all;

entity my_ent is
port (
    foo: view foo of bit
);
end entity;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("of bit").s1("bit"),
            "Specified subtype must match the subtype declared for the view",
            ErrorCode::TypeMismatch,
        )],
    );

    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
package x is
    type bar is record
        x: bit;
    end bar;
     
    view foo of bar is
        x: in;
    end view;
end x;

use work.x.all;

entity my_ent is
port (
    x: view foo of bar
);
end entity;
    ",
    );
    let (root, diag) = builder.get_analyzed_root();
    check_no_diagnostics(&diag);
    let in_view = root
        .search_reference(
            code.source(),
            code.s1("x: view foo of bar").s1("bar").start(),
        )
        .unwrap();

    let declared = root
        .search_reference(
            code.source(),
            code.s1("type bar is record").s1("bar").start(),
        )
        .unwrap();
    assert_eq!(in_view, declared)
}

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

use work.x;

entity my_ent is
port (
    x: view x.foo
);
end entity;
    ",
    );
    let (root, diag) = builder.get_analyzed_root();
    check_no_diagnostics(&diag);
    let in_view = root
        .search_reference(code.source(), code.s1("x: view x.foo").s1("foo").start())
        .unwrap();

    let declared = root
        .search_reference(
            code.source(),
            code.s1("view foo of bar is").s1("foo").start(),
        )
        .unwrap();
    assert_eq!(in_view, declared)
}

#[test]
fn converse_attribute() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    builder.code(
        "libname",
        "\
package my_pkg is
    type bar is record
        x: bit;
    end bar;
     
    view foo of bar is
        x: in;
    end view;

    alias foo_conv is foo'converse;
end my_pkg;

use work.my_pkg;

entity my_ent is
port (
    x: view my_pkg.foo;
    y: view my_pkg.foo_conv
);
end entity;
    ",
    );
    let diag = builder.analyze();
    // The mode of views is not analyzed at the moment, so the 'converse attribute
    // should simply not show any diagnostics.
    check_no_diagnostics(&diag);
}

#[test]
#[ignore]
fn view_declaration_in_cannot_be_assigned_to() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
package my_pkg is
    type bar is record
        x: bit;
    end bar;
     
    view foo of bar is
        x: in;
    end view;
end my_pkg;

use work.my_pkg;

entity my_ent is
port ( y: view my_pkg.foo );
end entity;

architecture arch of my_ent is
begin
    y.x <= '1';
end arch;
    ",
    );
    let diag = builder.analyze();
    check_diagnostics(
        diag,
        vec![Diagnostic::new(
            code.s1("y.x"),
            "interface signal 'y' of mode in may not be the target of an assignment",
            ErrorCode::MismatchedKinds,
        )],
    );
}

// GitHub issue #324
#[test]
fn view_in_generic_package() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    builder.code(
        "libname",
        "\
package test_pkg is
    generic ( width : integer );
    type test_t is record
        a : bit;
    end record;

    view vone of test_t is
        a : in;
    end view;
    alias vtwo is vone'converse;
end package;

package w8_pkg is new work.test_pkg generic map (width => 8);

use work.w8_pkg.all;

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

use work.w8_pkg.all;

entity test_top_entity is
end entity;

architecture rtl of test_top_entity is
    signal my_sig : test_t;
begin
    my_if : entity work.test_sub_entity
        port map (
            my_if => my_sig
        );
end architecture;
    ",
    );
    check_no_diagnostics(&builder.analyze());
}

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

    view vone of test_t is
        a : 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_if : view vone;
        my_array_if: view (vone) of test_array(0 to 1)
    );
end entity;
    ",
    );
    check_no_diagnostics(&builder.analyze());
}

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

    view vone of test_t is
        a : in;
    end view;

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

use work.test_pkg.all;

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

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

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

use work.test_pkg.all;

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

#[test]
fn undefined_name_for_nested_view() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
package test is
    type my_rec_t is record
        a : bit;
     end record;
     view invalid_view of my_rec_t is
         a : view undefined_view;
     end view;
end package;
    ",
    );
    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("undefined_view"),
            "No declaration of 'undefined_view'",
            ErrorCode::Unresolved,
        )],
    )
}

#[test]
fn view_that_is_not_a_view() {
    let mut builder = LibraryBuilder::with_standard(VHDL2019);
    let code = builder.code(
        "libname",
        "\
package test is
    constant some_constant : bit := '1';
    type my_rec_t is record
        a : bit;
     end record;
     view invalid_view of my_rec_t is
         a : view some_constant;
     end view;
end package;
    ",
    );
    check_diagnostics(
        builder.analyze(),
        vec![Diagnostic::new(
            code.s1("a : view some_constant;").s1("some_constant"),
            "Expected view",
            ErrorCode::MismatchedKinds,
        )],
    )
}