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
// 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 self::fnv::FnvHashMap;
use crate::analysis::{Analyzer, DesignRoot};
use crate::ast::DesignFile;
use crate::config::Config;
use crate::diagnostic::Diagnostic;
use crate::latin_1::Latin1String;
use crate::message::Message;
use crate::parser::{FileToParse, ParserError, VHDLParser};
use crate::source::{Position, Source, SrcPos};
use crate::symbol_table::Symbol;
use fnv;
use std::collections::hash_map::Entry;
use std::io;

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

impl Project {
    pub fn new() -> Project {
        Project {
            parser: VHDLParser::new(),
            root: DesignRoot::new(),
            files: FnvHashMap::default(),
            empty_libraries: Vec::new(),
        }
    }

    pub fn from_config(
        config: &Config,
        num_threads: usize,
        messages: &mut Vec<Message>,
    ) -> Project {
        let mut project = Project::new();
        let mut files_to_parse: FnvHashMap<String, LibraryFileToParse> = FnvHashMap::default();

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

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

                match files_to_parse.entry(file_name.clone()) {
                    Entry::Occupied(mut entry) => {
                        entry.get_mut().library_names.push(library_name.clone());
                    }
                    Entry::Vacant(entry) => {
                        let file_to_parse = LibraryFileToParse {
                            library_names: vec![library_name.clone()],
                            file_name: file_name.clone(),
                        };

                        entry.insert(file_to_parse);
                    }
                }
            }

            if empty_library {
                project.empty_libraries.push(library_name)
            }
        }

        let files_to_parse = files_to_parse.drain().map(|(_, v)| v).collect();

        for (file_to_parse, mut parser_diagnostics, design_file) in project
            .parser
            .parse_design_files(files_to_parse, num_threads)
        {
            let design_file = match design_file {
                Ok(design_file) => Some(design_file),
                Err(ParserError::Diagnostic(diagnostic)) => {
                    parser_diagnostics.push(diagnostic);
                    None
                }
                Err(ParserError::IOError(err)) => {
                    messages.push(Message::file_error(
                        err.to_string(),
                        file_to_parse.file_name,
                    ));
                    continue;
                }
            };

            project.files.insert(
                file_to_parse.file_name,
                SourceFile {
                    library_names: file_to_parse.library_names,
                    parser_diagnostics,
                    design_file,
                },
            );
        }

        project
    }

    pub fn update_source(&mut self, source: &Source) -> io::Result<()> {
        let mut source_file = {
            if let Some(source_file) = self.files.remove(source.file_name()) {
                for library_name in source_file.library_names.iter() {
                    self.root
                        .ensure_library(library_name.clone())
                        .remove_source(source);
                }
                source_file
            } else {
                SourceFile {
                    library_names: vec![],
                    parser_diagnostics: vec![],
                    design_file: None,
                }
            }
        };
        source_file.design_file = None;
        source_file.parser_diagnostics.clear();

        let design_file = self
            .parser
            .parse_design_source(source, &mut source_file.parser_diagnostics);

        let result = match design_file {
            Ok(design_file) => {
                source_file.design_file = Some(design_file);
                Ok(())
            }
            Err(ParserError::Diagnostic(diagnostic)) => {
                source_file.parser_diagnostics.push(diagnostic);
                Ok(())
            }
            Err(ParserError::IOError(err)) => {
                // @TODO convert to soft error and push to diagnostics
                Err(err)
            }
        };

        self.files
            .insert(source.file_name().to_owned(), source_file);

        result
    }

    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 library = self.root.ensure_library(library_name.clone());
                if let Some(design_file) = design_files.pop().unwrap() {
                    library.add_design_file(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());
        }

        for library in self.root.iter_libraries_mut() {
            library.refresh(&mut diagnostics);
        }

        Analyzer::new(&mut self.root, &self.parser.symtab.clone()).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(&self, source: &Source, cursor: Position) -> Option<SrcPos> {
        self.root.search_reference(source, cursor)
    }

    pub fn find_all_references(&self, decl_pos: &SrcPos) -> Vec<SrcPos> {
        self.root.find_all_references(decl_pos)
    }
}

/// 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()
    }
}

struct LibraryFileToParse {
    library_names: Vec<Symbol>,
    file_name: String,
}

impl FileToParse for LibraryFileToParse {
    fn file_name(&self) -> &str {
        &self.file_name
    }
}

struct SourceFile {
    library_names: Vec<Symbol>,
    design_file: Option<DesignFile>,
    parser_diagnostics: Vec<Diagnostic>,
}

impl SourceFile {
    fn take_design_file(&mut self) -> Option<DesignFile> {
        std::mem::replace(&mut self.design_file, None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_util::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, 1, &mut messages);
        assert_eq!(messages, vec![]);
        check_no_diagnostics(&project.analyse());
    }

    /// 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, 1, &mut messages);
        assert_eq!(messages, vec![]);
        check_no_diagnostics(&project.analyse());
    }

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

    /// Test that the same file can be added to several libraries
    #[test]
    fn test_re_analyze_after_update() {
        let root = tempfile::tempdir().unwrap();
        let path1 = root.path().join("file1.vhd");
        let source1 = Source::from_file(path1.to_str().unwrap());

        let path2 = root.path().join("file2.vhd");
        let source2 = Source::from_file(path2.to_str().unwrap());

        std::fs::write(
            &path1,
            "
package pkg is
end package;
        ",
        )
        .unwrap();

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

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

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

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

        // Add syntax error
        update(
            &mut project,
            &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,
            &source1,
            "
package pkg is
end package;
        ",
        );
        check_no_diagnostics(&project.analyse());

        // Add analysis error
        update(
            &mut project,
            &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,
            &source2,
            "
package pkg is
end package;
        ",
        );
        check_no_diagnostics(&project.analyse());
    }
}