@@ -11,6 +11,7 @@ use std::rc::Rc;
11
11
use clang:: { Entity , EntityKind , EntityVisitResult } ;
12
12
13
13
use crate :: class:: ClassKind ;
14
+ use crate :: settings:: Settings ;
14
15
use crate :: type_ref:: CppNameStyle ;
15
16
use crate :: {
16
17
is_opencv_path, opencv_module_from_path, settings, Class , Element , EntityWalkerExt , EntityWalkerVisitor , MemoizeMap ,
@@ -104,6 +105,7 @@ struct ExportIdx {
104
105
///
105
106
/// This is 1st pass of the analysis. It performs the collection of the necessary auxiliary data like which descendants a class has.
106
107
struct GeneratorEnvPopulator < ' tu , ' ge > {
108
+ module : & ' tu str ,
107
109
gen_env : & ' ge mut GeneratorEnv < ' tu > ,
108
110
}
109
111
@@ -133,7 +135,7 @@ impl<'tu> GeneratorEnvPopulator<'tu, '_> {
133
135
134
136
impl < ' tu > EntityWalkerVisitor < ' tu > for GeneratorEnvPopulator < ' tu , ' _ > {
135
137
fn wants_file ( & mut self , path : & Path ) -> bool {
136
- is_opencv_path ( path) || opencv_module_from_path ( path) . map_or ( false , |m| m == self . gen_env . module ( ) )
138
+ is_opencv_path ( path) || opencv_module_from_path ( path) . map_or ( false , |m| m == self . module )
137
139
}
138
140
139
141
fn visit_entity ( & mut self , entity : Entity < ' tu > ) -> ControlFlow < ( ) > {
@@ -169,8 +171,6 @@ impl<'tu> EntityWalkerVisitor<'tu> for GeneratorEnvPopulator<'tu, '_> {
169
171
/// This is partially pre-populated in an additional pass before the generation to provide some necessary data that's not available
170
172
/// at the generation moment. E.g. list of descendants of a particular class.
171
173
pub struct GeneratorEnv < ' tu > {
172
- /// The name of the module that's currently being generated
173
- module : & ' tu str ,
174
174
export_map : HashMap < ExportIdx , ExportConfig > ,
175
175
rename_map : HashMap < ExportIdx , RenameConfig > ,
176
176
pub func_names : NamePool ,
@@ -179,28 +179,40 @@ pub struct GeneratorEnv<'tu> {
179
179
/// Cache of the calculated [ClassKind]s
180
180
class_kind_cache : MemoizeMap < String , Option < ClassKind > > ,
181
181
descendants : HashMap < String , HashSet < Entity < ' tu > > > ,
182
+ pub settings : Settings ,
182
183
}
183
184
184
185
impl < ' tu > GeneratorEnv < ' tu > {
185
- pub fn new ( root_entity : Entity < ' tu > , module : & ' tu str ) -> Self {
186
+ pub fn empty ( ) -> Self {
187
+ Self {
188
+ export_map : HashMap :: new ( ) ,
189
+ rename_map : HashMap :: new ( ) ,
190
+ func_names : NamePool :: with_capacity ( 0 ) ,
191
+ func_comments : HashMap :: new ( ) ,
192
+ class_kind_cache : MemoizeMap :: new ( HashMap :: new ( ) ) ,
193
+ descendants : HashMap :: new ( ) ,
194
+ settings : Settings :: empty ( ) ,
195
+ }
196
+ }
197
+
198
+ /// [GeneratorEnv] with the global settings for the regular working mode
199
+ pub fn global ( module : & ' tu str , root_entity : Entity < ' tu > ) -> Self {
186
200
let mut out = Self {
187
- module,
188
201
export_map : HashMap :: with_capacity ( 1024 ) ,
189
202
rename_map : HashMap :: with_capacity ( 64 ) ,
190
203
func_names : NamePool :: with_capacity ( 512 ) ,
191
204
func_comments : HashMap :: with_capacity ( 2048 ) ,
192
205
class_kind_cache : MemoizeMap :: new ( HashMap :: with_capacity ( 32 ) ) ,
193
206
descendants : HashMap :: with_capacity ( 16 ) ,
207
+ settings : Settings :: for_module ( module) ,
194
208
} ;
195
- root_entity. walk_opencv_entities ( GeneratorEnvPopulator { gen_env : & mut out } ) ;
209
+ root_entity. walk_opencv_entities ( GeneratorEnvPopulator {
210
+ module,
211
+ gen_env : & mut out,
212
+ } ) ;
196
213
out
197
214
}
198
215
199
- /// The name of the module that's currently being generated
200
- pub fn module ( & self ) -> & str {
201
- self . module
202
- }
203
-
204
216
fn key ( entity : Entity ) -> ExportIdx {
205
217
let ( loc, line_offset) = if entity. get_kind ( ) == EntityKind :: MacroExpansion {
206
218
// sometimes CV_EXPORT macros are located on a separate line so for those we compensate the offset
@@ -353,11 +365,15 @@ impl fmt::Debug for GeneratorEnv<'_> {
353
365
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
354
366
f. debug_struct ( "GeneratorEnv" )
355
367
. field ( "export_map" , & format ! ( "{} elements" , self . export_map. len( ) ) )
368
+ . field ( "rename_map" , & format ! ( "{} elements" , self . rename_map. len( ) ) )
369
+ . field ( "func_names" , & format ! ( "{} elements" , self . func_names. len( ) ) )
356
370
. field ( "func_comments" , & format ! ( "{} elements" , self . func_comments. len( ) ) )
357
371
. field (
358
372
"class_kind_cache" ,
359
373
& format ! ( "{} elements" , self . class_kind_cache. borrow( ) . len( ) ) ,
360
374
)
375
+ . field ( "descendants" , & format ! ( "{} elements" , self . descendants. len( ) ) )
376
+ . field ( "settings" , & self . settings )
361
377
. finish ( )
362
378
}
363
379
}
0 commit comments