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

use crate::analysis::{AnyEnt, DesignRoot, EntRef};
use crate::ast::DesignFile;
use crate::config::Config;
use crate::data::*;
use crate::syntax::VHDLParser;
use fnv::{FnvHashMap, FnvHashSet};
use std::collections::hash_map::Entry;
use std::path::{Path, PathBuf};

pub struct Project {
    parser: VHDLParser,
    root: DesignRoot,
    files: FnvHashMap<PathBuf, SourceFile>,
    empty_libraries: FnvHashSet<Symbol>,
}

impl Project {
    pub fn new() -> Project {
        let parser = VHDLParser::default();
        Project {
            root: DesignRoot::new(parser.symbols.clone()),
            files: FnvHashMap::default(),
            empty_libraries: FnvHashSet::default(),
            parser,
        }
    }

    /// Create instance from given configuration.
    /// Files referred by configuration are parsed into corresponding libraries.
    pub fn from_config(config: &Config, messages: &mut dyn MessageHandler) -> Project {
        let mut project = Project::new();

        let files = project.load_files_from_config(config, messages);
        project.parse_and_add_files(files, messages);

        project
    }

    /// Replace active project configuration.
    /// The design state is reset, new files are added and parsed. Existing source files will be
    /// kept and parsed from in-memory source (required for incremental document updates).
    pub fn update_config(&mut self, config: &Config, messages: &mut dyn MessageHandler) {
        self.parser = VHDLParser::default();
        self.root = DesignRoot::new(self.parser.symbols.clone());

        // Reset library associations for known files,
        // all project files are added to the corresponding libraries later on.
        self.files
            .values_mut()
            .for_each(|source_file| source_file.library_names.clear());

        // Files might already be part of self.files, these have to be parsed
        // from in-memory source. New files can be parsed as usual.
        let (known_files, new_files) = self
            .load_files_from_config(config, messages)
            .into_iter()
            .partition(|(file_name, _library_names)| self.files.contains_key(file_name));

        for (file_name, library_names) in known_files {
            if let Some(source_file) = self.files.get_mut(&file_name) {
                source_file.parser_diagnostics.clear();
                source_file.library_names = library_names;
                source_file.design_file = self
                    .parser
                    .parse_design_source(&source_file.source, &mut source_file.parser_diagnostics);
            }
        }

        self.parse_and_add_files(new_files, messages);
    }

    fn load_files_from_config(
        &mut self,
        config: &Config,
        messages: &mut dyn MessageHandler,
    ) -> FnvHashMap<PathBuf, FnvHashSet<Symbol>> {
        let mut files: FnvHashMap<PathBuf, FnvHashSet<Symbol>> = FnvHashMap::default();
        self.empty_libraries.clear();

        for library in config.iter_libraries() {
            let library_name =
                Latin1String::from_utf8(library.name()).expect("Library name not latin-1 encoded");
            let library_name = self.parser.symbol(&library_name);

            let mut empty_library = true;
            for file_name in library.file_names(messages) {
                empty_library = false;

                match files.entry(file_name.clone()) {
                    Entry::Occupied(mut entry) => {
                        entry.get_mut().insert(library_name.clone());
                    }
                    Entry::Vacant(entry) => {
                        let mut set = FnvHashSet::default();
                        set.insert(library_name.clone());
                        entry.insert(set);
                    }
                }
            }

            if empty_library {
                self.empty_libraries.insert(library_name);
            }
        }
        files
    }

    fn parse_and_add_files(
        &mut self,
        files_to_parse: FnvHashMap<PathBuf, FnvHashSet<Symbol>>,
        messages: &mut dyn MessageHandler,
    ) {
        use rayon::prelude::*;

        let parsed: Vec<_> = files_to_parse
            .into_par_iter()
            .map_init(
                || &self.parser,
                |parser, (file_name, library_names)| {
                    let mut diagnostics = Vec::new();
                    let result = parser.parse_design_file(&file_name, &mut diagnostics);
                    (file_name, library_names, diagnostics, result)
                },
            )
            .collect();

        for (file_name, library_names, parser_diagnostics, result) in parsed.into_iter() {
            let (source, design_file) = match result {
                Ok(result) => result,
                Err(err) => {
                    messages.push(Message::file_error(err.to_string(), &file_name));
                    continue;
                }
            };

            self.files.insert(
                source.file_name().to_owned(),
                SourceFile {
                    source,
                    library_names,
                    parser_diagnostics,
                    design_file,
                },
            );
        }
    }

    pub fn get_source(&self, file_name: &Path) -> Option<Source> {
        self.files.get(file_name).map(|file| file.source.clone())
    }

    pub fn update_source(&mut self, source: &Source) {
        let mut source_file = {
            if let Some(mut source_file) = self.files.remove(source.file_name()) {
                // File is already part of the project
                for library_name in source_file.library_names.iter() {
                    self.root.remove_source(library_name.clone(), source);
                }
                source_file.source = source.clone();
                source_file
            } else {
                // File is not part of the project
                // @TODO use config wildcards to map to library

                // Add unmapped files to an anonymous library work
                // To still get some semantic analysis for unmapped files
                let mut library_names = FnvHashSet::default();
                library_names.insert(self.root.symbol_utf8("work"));

                SourceFile {
                    source: source.clone(),
                    library_names,
                    parser_diagnostics: vec![],
                    design_file: DesignFile::default(),
                }
            }
        };
        source_file.parser_diagnostics.clear();
        source_file.design_file = self
            .parser
            .parse_design_source(source, &mut source_file.parser_diagnostics);
        self.files
            .insert(source.file_name().to_owned(), source_file);
    }

    pub fn analyse(&mut self) -> Vec<Diagnostic> {
        let mut diagnostics = Vec::new();

        for source_file in self.files.values_mut() {
            let design_file = source_file.take_design_file();
            // Avoid cloning design files for single library
            let mut design_files = multiply(design_file, source_file.library_names.len());

            for library_name in source_file.library_names.iter() {
                let design_file = design_files.pop().unwrap();
                self.root.add_design_file(library_name.clone(), design_file);
            }

            for diagnostic in source_file.parser_diagnostics.iter().cloned() {
                diagnostics.push(diagnostic);
            }
        }

        for library_name in self.empty_libraries.iter() {
            self.root.ensure_library(library_name.clone());
        }

        self.root.analyze(&mut diagnostics);
        diagnostics
    }

    /// Search for reference at position
    /// Character offset on a line in a document (zero-based). Assuming that the line is
    /// represented as a string, the `character` value represents the gap between the
    /// `character` and `character + 1`.
    ///
    /// If the character value is greater than the line length it defaults back to the
    /// line length.
    pub fn search_reference<'a>(&'a self, source: &Source, cursor: Position) -> Option<EntRef<'a>> {
        self.root.search_reference(source, cursor)
    }

    /// Search for the declaration at decl_pos and format it
    pub fn format_declaration(&self, ent: &AnyEnt) -> Option<String> {
        self.root.format_declaration(ent)
    }

    /// Search for all references to the declaration at decl_pos
    pub fn find_all_references(&self, ent: &AnyEnt) -> Vec<SrcPos> {
        self.root.find_all_references(ent)
    }

    /// Get source positions that are not resolved to a declaration
    /// This is used for development to test where the language server is blind
    pub fn find_all_unresolved(&self) -> (usize, Vec<SrcPos>) {
        self.root.find_all_unresolved()
    }

    pub fn files(&self) -> impl Iterator<Item = &SourceFile> {
        self.files.values()
    }
}

/// Multiply clonable value by cloning
/// Avoid clone for n=1
fn multiply<T: Clone>(value: T, n: usize) -> Vec<T> {
    if n == 0 {
        vec![]
    } else if n == 1 {
        vec![value]
    } else {
        let mut res = Vec::with_capacity(n);
        for _ in 0..n - 1 {
            res.push(value.clone());
        }
        res.push(value);
        res
    }
}

impl Default for Project {
    fn default() -> Self {
        Self::new()
    }
}

pub struct SourceFile {
    library_names: FnvHashSet<Symbol>,
    source: Source,
    design_file: DesignFile,
    parser_diagnostics: Vec<Diagnostic>,
}

impl SourceFile {
    fn take_design_file(&mut self) -> DesignFile {
        std::mem::take(&mut self.design_file)
    }

    pub fn num_lines(&self) -> usize {
        self.source.contents().num_lines()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::syntax::test::check_no_diagnostics;

    /// Test that an empty library is created
    /// Thus test case was added when fixing a bug
    /// Where a library with no files was never added
    #[test]
    fn test_empty_library_is_defined() {
        let root = tempfile::tempdir().unwrap();
        let vhdl_file_path = root.path().join("file.vhd");
        std::fs::write(
            vhdl_file_path,
            "
library missing;

entity ent is
end entity;
        ",
        )
        .unwrap();

        let config_str = "
[libraries]
missing.files = []
lib.files = ['file.vhd']
        ";

        let config = Config::from_str(config_str, root.path()).unwrap();
        let mut messages = Vec::new();
        let mut project = Project::from_config(&config, &mut messages);
        assert_eq!(messages, vec![]);
        check_no_diagnostics(&project.analyse());
    }

    #[test]
    fn unmapped_libraries_are_analyzed() {
        let mut messages = Vec::new();
        let mut project = Project::from_config(&Config::default(), &mut messages);
        assert_eq!(messages, vec![]);
        let diagnostics = project.analyse();
        check_no_diagnostics(&diagnostics);

        let root = tempfile::tempdir().unwrap();
        let vhdl_file_path = root.path().join("file.vhd");
        std::fs::write(
            &vhdl_file_path,
            "
entity ent is
end ent;

architecture rtl of ent is
begin
end architecture;

architecture rtl of ent is
begin
end architecture;
",
        )
        .unwrap();
        let source = Source::from_latin1_file(&vhdl_file_path).unwrap();

        project.update_source(&source);
        let diagnostics = project.analyse();
        assert_eq!(diagnostics.len(), 1);
        let diag = diagnostics.first().unwrap();
        assert_eq!(diag.message, "Duplicate architecture 'rtl' of entity 'ent'")
    }

    /// Test that the same file can be added to several libraries
    #[test]
    fn test_same_file_in_multiple_libraries() {
        let root = tempfile::tempdir().unwrap();
        let vhdl_file_path1 = root.path().join("file.vhd");
        std::fs::write(
            vhdl_file_path1,
            "
package pkg is
end package;
        ",
        )
        .unwrap();

        let vhdl_file_path2 = root.path().join("use_file.vhd");
        std::fs::write(
            vhdl_file_path2,
            "
library lib1;
use lib1.pkg.all;

package use_pkg1 is
end package;

library lib2;
use lib2.pkg.all;

package use_pkg2 is
end package;
        ",
        )
        .unwrap();

        let config_str = "
[libraries]
lib1.files = ['file.vhd']
lib2.files = ['file.vhd']
use_lib.files = ['use_file.vhd']
        ";

        let config = Config::from_str(config_str, root.path()).unwrap();
        let mut messages = Vec::new();
        let mut project = Project::from_config(&config, &mut messages);
        assert_eq!(messages, vec![]);
        check_no_diagnostics(&project.analyse());
    }

    fn update(project: &mut Project, source: &mut Source, contents: &str) {
        std::fs::write(std::path::Path::new(source.file_name()), contents).unwrap();
        *source = Source::from_latin1_file(source.file_name()).unwrap();
        project.update_source(source);
    }

    /// Test that the same file can be added to several libraries
    #[test]
    fn test_re_analyze_after_update() {
        let tempdir = tempfile::tempdir().unwrap();
        let root = dunce::canonicalize(tempdir.path()).unwrap();

        let path1 = root.join("file1.vhd");
        let path2 = root.join("file2.vhd");
        std::fs::write(
            &path1,
            "
package pkg is
end package;
        ",
        )
        .unwrap();
        let mut source1 = Source::from_latin1_file(&path1).unwrap();

        std::fs::write(
            &path2,
            "
library lib1;
use lib1.pkg.all;

package pkg is
end package;
        ",
        )
        .unwrap();
        let mut source2 = Source::from_latin1_file(&path2).unwrap();

        let config_str = "
[libraries]
lib1.files = ['file1.vhd']
lib2.files = ['file2.vhd']
        ";

        let config = Config::from_str(config_str, &root).unwrap();
        let mut messages = Vec::new();
        let mut project = Project::from_config(&config, &mut messages);
        assert_eq!(messages, vec![]);
        check_no_diagnostics(&project.analyse());

        // Add syntax error
        update(
            &mut project,
            &mut source1,
            "
package is
        ",
        );
        let diagnostics = project.analyse();
        assert_eq!(diagnostics.len(), 2);
        // Syntax error comes first
        assert_eq!(diagnostics[0].pos.source, source1);
        assert_eq!(diagnostics[1].pos.source, source2);

        // Make it good again
        update(
            &mut project,
            &mut source1,
            "
package pkg is
end package;
        ",
        );
        check_no_diagnostics(&project.analyse());

        // Add analysis error
        update(
            &mut project,
            &mut source2,
            "
package pkg is
end package;

package pkg is
end package;
        ",
        );
        let diagnostics = project.analyse();
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].pos.source, source2);

        // Make it good again
        update(
            &mut project,
            &mut source2,
            "
package pkg is
end package;
        ",
        );
        check_no_diagnostics(&project.analyse());
    }

    /// Test that the configuration can be updated
    #[test]
    fn test_config_update() {
        let tempdir = tempfile::tempdir().unwrap();
        let root = dunce::canonicalize(tempdir.path()).unwrap();

        let path1 = root.join("file1.vhd");
        let path2 = root.join("file2.vhd");
        std::fs::write(
            &path1,
            "
library unkown;
use unkown.pkg.all;

package pkg is
end package;
        ",
        )
        .unwrap();
        let source1 = Source::from_latin1_file(&path1).unwrap();

        std::fs::write(
            &path2,
            "
library unkown;
use unkown.pkg.all;

package pkg is
end package;
        ",
        )
        .unwrap();
        let source2 = Source::from_latin1_file(&path2).unwrap();

        let config_str1 = "
[libraries]
lib.files = ['file1.vhd']
        ";
        let config1 = Config::from_str(config_str1, &root).unwrap();

        let config_str2 = "
[libraries]
lib.files = ['file2.vhd']
        ";
        let config2 = Config::from_str(config_str2, &root).unwrap();

        let mut messages = Vec::new();
        let mut project = Project::from_config(&config1, &mut messages);
        assert_eq!(messages, vec![]);

        // Invalid library should only be reported in source1
        let diagnostics = project.analyse();
        assert_eq!(diagnostics.len(), 2);
        assert_eq!(diagnostics[0].pos.source, source1); // No such library
        assert_eq!(diagnostics[1].pos.source, source1); // No declaration

        // Change configuration file
        project.update_config(&config2, &mut messages);
        assert_eq!(messages, vec![]);

        // Invalid library should only be reported in source2
        let diagnostics = project.analyse();
        assert_eq!(diagnostics.len(), 2);
        assert_eq!(diagnostics[0].pos.source, source2); // No such library
        assert_eq!(diagnostics[1].pos.source, source2); // No declaration
    }
}