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
use crate::lang::c::{CType, Constant, Function};
use crate::patterns::LibraryPattern;
use crate::util::{ctypes_from_functions_types, extract_namespaces_from_types};
use std::collections::HashSet;
#[derive(Clone, Debug, PartialOrd, PartialEq, Default)]
pub struct Library {
functions: Vec<Function>,
ctypes: Vec<CType>,
constants: Vec<Constant>,
patterns: Vec<LibraryPattern>,
namespaces: Vec<String>,
}
impl Library {
pub fn new(functions: Vec<Function>, constants: Vec<Constant>, patterns: Vec<LibraryPattern>, extra_types: Vec<CType>) -> Self {
let mut ctypes = ctypes_from_functions_types(&functions, &extra_types);
let mut namespaces = HashSet::new();
extract_namespaces_from_types(&ctypes, &mut namespaces);
namespaces.extend(functions.iter().map(|x| x.meta().namespace().to_string()));
namespaces.extend(constants.iter().map(|x| x.meta().namespace().to_string()));
let mut namespaces = namespaces.iter().cloned().collect::<Vec<String>>();
namespaces.sort();
ctypes.sort();
Self {
functions,
ctypes,
constants,
patterns,
namespaces,
}
}
pub fn functions(&self) -> &[Function] {
&self.functions
}
pub fn ctypes(&self) -> &[CType] {
&self.ctypes
}
pub fn constants(&self) -> &[Constant] {
&self.constants
}
pub fn namespaces(&self) -> &[String] {
&self.namespaces
}
pub fn patterns(&self) -> &[LibraryPattern] {
&self.patterns
}
}
pub fn merge_libraries(libraries: &[Library]) -> Library {
let mut functions = Vec::new();
let mut constants = Vec::new();
let mut patterns = Vec::new();
let mut types = Vec::new();
for library in libraries {
functions.extend_from_slice(library.functions());
constants.extend_from_slice(library.constants());
patterns.extend_from_slice(library.patterns());
types.extend_from_slice(library.ctypes());
}
Library::new(functions, constants, patterns, types)
}