vhdl_lang 0.63.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
//! 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) 2023, Olof Kraigher olof.kraigher@gmail.com

use super::*;
use crate::EntHierarchy;
use crate::Source;
use pretty_assertions::assert_eq;

#[test]
fn entity_architecture() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
entity ent is
  generic (
    g0 : natural
  );
  port (
    p0 : bit
  );
end entity;

architecture a of ent is 
  signal s0 : natural;
begin
  block
    begin
    process
        variable v0 : natural;
    begin
        loop0: loop
        end loop;

        if false then
        end if;
    end process;
  end block;
end architecture;
      ",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);
    assert_eq!(
        get_hierarchy(&root, "libname", code.source()),
        vec![nested(
            "ent",
            vec![
                single("g0"),
                single("p0"),
                nested(
                    "a",
                    vec![
                        single("s0"),
                        // @TODO add tree for anonymous block and process
                        single("v0"),
                        single("loop0"),
                    ]
                ),
            ]
        )]
    );
}

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


package body pkg is
    function fun0(arg : natural) return natural is
        variable v0 : natural;
    begin
    end function;
end package body;
      ",
    );

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

    assert_eq!(
        get_hierarchy(&root, "libname", code.source()),
        vec![
            nested("pkg", vec![nested("fun0", vec![single("arg"),]),]),
            nested(
                "pkg",
                vec![nested("fun0", vec![single("arg"), single("v0")]),]
            )
        ]
    );
}

#[test]
fn generic_package() {
    let mut builder = LibraryBuilder::new();
    let code = builder.code(
        "libname",
        "
package pkg is
   generic (
     type type_t;
     value: type_t
   );

   constant c0 : type_t := value;
   function fun0(arg: type_t) return boolean;
end package;

package body pkg is
    function fun0(arg: type_t) return boolean is
    begin
        return arg = value;
    end function;
end package body;

package ipkg is new work.pkg generic map(type_t => integer, value => 0);
      ",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);
    assert_eq!(
        get_hierarchy(&root, "libname", code.source()),
        vec![
            nested(
                "pkg",
                vec![
                    single("type_t"),
                    single("value"),
                    single("c0"),
                    nested("fun0", vec![single("arg"),]),
                ]
            ),
            nested("pkg", vec![nested("fun0", vec![single("arg")]),]),
            single("ipkg"),
        ]
    );

    let ipkg = root
        .search_reference(code.source(), code.s1("ipkg").start())
        .unwrap();

    let instances: Vec<_> = if let AnyEntKind::Design(Design::PackageInstance(region)) = ipkg.kind()
    {
        let mut symbols: Vec<_> = region.immediates().collect();
        symbols.sort_by_key(|ent| ent.decl_pos());
        symbols.into_iter().map(|ent| ent.path_name()).collect()
    } else {
        panic!("Expected instantiated package");
    };

    assert_eq!(instances, vec!["libname.ipkg.c0", "libname.ipkg.fun0"]);
}

#[test]
fn public_symbols() {
    let mut builder = LibraryBuilder::new();
    builder.code(
        "libname",
        "
package pkg is
   type type_t is (alpha, beta);
   constant const0 : type_t := alpha;
   function fun0(arg: type_t) return boolean;
   function \"+\"(arg: type_t) return boolean;

   type prot_t is protected
       procedure proc0(arg: type_t);
   end protected;
end package;

package body pkg is
    type prot_t is protected body
        procedure proc0(arg: type_t) is
        begin
        end;
    end protected body;
end package body;

entity ent is
  generic (
    g0 : natural
  );
  port (
    p0 : natural
  );
end entity;

architecture a of ent is
  signal not_public : bit;
begin
  main: process
  begin
  end process;
end architecture;
      ",
    );

    let (root, diagnostics) = builder.get_analyzed_root();
    check_no_diagnostics(&diagnostics);
    let mut symbols: Vec<_> = root
        .public_symbols()
        .filter(|ent| ent.library_name() == Some(&root.symbol_utf8("libname")))
        .collect();
    symbols.sort_by_key(|ent| ent.decl_pos());

    assert_eq!(
        symbols
            .into_iter()
            .map(|ent| ent.path_name())
            .collect::<Vec<_>>(),
        vec![
            "libname",
            "libname.pkg",
            "libname.pkg.type_t",
            "libname.pkg.type_t.alpha",
            "libname.pkg.type_t.beta",
            "libname.pkg.const0",
            "libname.pkg.fun0",
            "libname.pkg.\"+\"",
            "libname.pkg.prot_t",
            "libname.pkg.prot_t.proc0",
            "libname.pkg",
            "libname.ent",
            "libname.ent.g0",
            "libname.ent.p0",
            "libname.ent.a",
        ]
    );
}

#[derive(PartialEq, Debug)]
struct NameHierarchy {
    name: String,
    children: Vec<NameHierarchy>,
}

/// For compact test data creation
fn nested(name: &str, children: Vec<NameHierarchy>) -> NameHierarchy {
    NameHierarchy {
        name: name.to_owned(),
        children,
    }
}
/// For compact test data creation
fn single(name: &str) -> NameHierarchy {
    NameHierarchy {
        name: name.to_owned(),
        children: Vec::new(),
    }
}

impl<'a> From<EntHierarchy<'a>> for NameHierarchy {
    fn from(ent: EntHierarchy) -> Self {
        NameHierarchy {
            name: ent.ent.designator().to_string(),
            children: ent.children.into_iter().map(NameHierarchy::from).collect(),
        }
    }
}

fn get_hierarchy(root: &DesignRoot, libname: &str, source: &Source) -> Vec<NameHierarchy> {
    root.document_symbols(&root.symbol_utf8(libname), source)
        .into_iter()
        .map(NameHierarchy::from)
        .collect()
}

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

architecture a of ent0 is
begin
end architecture;

entity ent1 is
end entity;

architecture a of ent1 is
  component ent0 is
  end component;
begin
  inst: ent0;
end architecture;

      ",
    );

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

    let ent = root
        .search_reference(code.source(), code.s1("ent0").start())
        .unwrap();
    let comp = root
        .search_reference(code.source(), code.sa("component ", "ent0").start())
        .unwrap();

    assert_eq!(root.find_implementation(ent), vec![comp]);
    assert_eq!(root.find_implementation(comp), vec![ent]);
}

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

architecture a of ent is
begin
  process
  begin
    exit;
    next;

    loop
        exit;
    end loop;

    loop
        next;
    end loop;
  end process;
end architecture;
      ",
    );

    let (_, diagnostics) = builder.get_analyzed_root();
    check_diagnostics(
        diagnostics,
        vec![
            Diagnostic::error(code.s1("exit;"), "Exit can only be used inside a loop"),
            Diagnostic::error(code.s1("next;"), "Next can only be used inside a loop"),
        ],
    );
}

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

architecture a of ent is
begin
  main: process
  begin
    good0: loop
        good1: loop
            exit good0;
        end loop;
    end loop;

    bad0: loop
        exit;
    end loop;

    l1: loop
        exit bad0;
    end loop;

    l0: loop
        next bad0; 
    end loop;
  end process;
end architecture;
      ",
    );

    let (_, diagnostics) = builder.get_analyzed_root();
    check_diagnostics(
        diagnostics,
        vec![
            Diagnostic::error(
                code.sa("exit ", "bad0"),
                "Cannot be used outside of loop 'bad0'",
            ),
            Diagnostic::error(
                code.sa("next ", "bad0"),
                "Cannot be used outside of loop 'bad0'",
            ),
        ],
    );
}