@@ -13,6 +13,7 @@ use crate::parse::{
13
13
} ;
14
14
use std:: io;
15
15
use std:: collections:: HashMap ;
16
+ use rcc:: { InternedStr , Literal } ;
16
17
17
18
/// The type for a constant variable.
18
19
#[ derive( Debug ) ]
@@ -134,7 +135,6 @@ impl ClangSubItemParser for Var {
134
135
cursor : clang:: Cursor ,
135
136
ctx : & mut BindgenContext ,
136
137
) -> Result < ParseResult < Self > , ParseError > {
137
- use rcc:: Literal ;
138
138
use clang_sys:: * ;
139
139
match cursor. kind ( ) {
140
140
CXCursor_MacroDefinition => {
@@ -156,29 +156,22 @@ impl ClangSubItemParser for Var {
156
156
157
157
assert ! ( !id. is_empty( ) , "Empty macro name?" ) ;
158
158
159
- let previously_defined = ctx. parsed_macro ( & id) ;
159
+ let previously_defined = ctx. parsed_macro ( id) ;
160
160
161
161
// NB: It's important to "note" the macro even if the result is
162
162
// not an integer, otherwise we might lose other kind of
163
163
// derived macros.
164
- ctx. note_parsed_macro ( id. clone ( ) , value. clone ( ) ) ;
164
+ ctx. note_parsed_macro ( id, value. clone ( ) ) ;
165
165
166
166
if previously_defined {
167
- let name = String :: from_utf8 ( id) . unwrap ( ) ;
168
- warn ! ( "Duplicated macro definition: {}" , name) ;
167
+ warn ! ( "Duplicated macro definition: {}" , id) ;
169
168
return Err ( ParseError :: Continue ) ;
170
169
}
171
170
172
- // NOTE: Unwrapping, here and above, is safe, because the
173
- // identifier of a token comes straight from clang, and we
174
- // enforce utf8 there, so we should have already panicked at
175
- // this point.
176
- let name = String :: from_utf8 ( id) . unwrap ( ) ;
177
-
178
171
let parse_int = |value| {
179
172
let kind = ctx
180
173
. parse_callbacks ( )
181
- . and_then ( |c| c. int_macro ( & name , value) )
174
+ . and_then ( |c| c. int_macro ( rcc :: get_str! ( id ) , value) )
182
175
. unwrap_or_else ( || {
183
176
default_macro_constant_type ( value)
184
177
} ) ;
@@ -200,7 +193,7 @@ impl ClangSubItemParser for Var {
200
193
ctx,
201
194
) ;
202
195
if let Some ( callbacks) = ctx. parse_callbacks ( ) {
203
- callbacks. str_macro ( & name , & val) ;
196
+ callbacks. str_macro ( rcc :: get_str! ( id ) , & val) ;
204
197
}
205
198
( TypeKind :: Pointer ( char_ty) , VarType :: String ( val) )
206
199
}
@@ -211,7 +204,7 @@ impl ClangSubItemParser for Var {
211
204
let ty = Item :: builtin_type ( type_kind, true , ctx) ;
212
205
213
206
Ok ( ParseResult :: New (
214
- Var :: new ( name , None , ty, Some ( val) , true ) ,
207
+ Var :: new ( id . resolve_and_clone ( ) , None , ty, Some ( val) , true ) ,
215
208
Some ( cursor) ,
216
209
) )
217
210
}
@@ -309,7 +302,7 @@ impl ClangSubItemParser for Var {
309
302
fn parse_macro (
310
303
ctx : & BindgenContext ,
311
304
cursor : & clang:: Cursor ,
312
- ) -> Option < ( Vec < u8 > , rcc :: Literal ) > {
305
+ ) -> Option < ( InternedStr , Literal ) > {
313
306
use rcc:: Token ;
314
307
315
308
// TODO: pass this in to rcc somehow
@@ -318,15 +311,15 @@ fn parse_macro(
318
311
// TODO: remove(0) is expensive
319
312
let ident = rcc_tokens. remove ( 0 ) ;
320
313
let ident_str = match ident. data {
321
- Token :: Id ( id) => id. resolve_and_clone ( ) . into_bytes ( ) ,
314
+ Token :: Id ( id) => id,
322
315
_ => return None ,
323
316
} ;
324
317
if ident_str. is_empty ( ) {
325
318
return None ;
326
319
}
327
320
328
321
// TODO: remove this clone (will need changes in rcc)
329
- if let Some ( literal) = rcc_expr ( rcc_tokens. clone ( ) . into_iter ( ) , & parsed_macros) {
322
+ if let Some ( literal) = rcc_expr ( rcc_tokens. clone ( ) . into_iter ( ) , parsed_macros) {
330
323
return Some ( ( ident_str, literal) ) ;
331
324
}
332
325
@@ -337,37 +330,22 @@ fn parse_macro(
337
330
// https://bugs.llvm.org//show_bug.cgi?id=9069
338
331
// https://reviews.llvm.org/D26446
339
332
rcc_tokens. pop ( ) ?;
340
- if let Some ( literal) = rcc_expr ( rcc_tokens. into_iter ( ) , & parsed_macros) {
333
+ if let Some ( literal) = rcc_expr ( rcc_tokens. into_iter ( ) , parsed_macros) {
341
334
Some ( ( ident_str, literal) )
342
335
} else {
343
336
None
344
337
}
345
338
}
346
339
347
- fn rcc_expr ( rcc_tokens : impl Iterator < Item = rcc:: Locatable < rcc:: Token > > , definitions : & HashMap < Vec < u8 > , rcc:: Literal > ) -> Option < rcc :: Literal > {
340
+ fn rcc_expr ( rcc_tokens : impl Iterator < Item = rcc:: Locatable < rcc:: Token > > , definitions : & HashMap < InternedStr , rcc:: Definition > ) -> Option < Literal > {
348
341
use rcc:: PreProcessor ;
349
342
350
- //let mut replacer = MacroReplacer::with_definitions(std::iter::empty().peekable(), &definitions);
351
343
let mut rcc_tokens = rcc_tokens. peekable ( ) ;
352
344
let location = rcc_tokens. peek ( ) ?. location ;
353
- let map = definitions. iter ( ) . map ( |( k, v) | {
354
- ( String :: from_utf8_lossy ( k) . to_string ( ) . into ( ) , rcc:: Definition :: Object ( vec ! [ rcc:: Token :: Literal ( v. clone( ) ) ] ) )
355
- } ) . collect ( ) ;
356
- PreProcessor :: cpp_expr ( & map, rcc_tokens, location) . ok ( ) ?. const_fold ( ) . ok ( ) ?. into_literal ( ) . ok ( )
357
- /*
358
- let first = rcc_tokens.next()?;
359
-
360
- let mut parser = Parser::new(first, rcc_tokens.map(Result::<_, CompileError>::Ok), false);
361
- let expr = parser.expr().ok()?;
362
- let mut analyzer = PureAnalyzer::new();
363
-
364
- analyzer.expr(expr).const_fold().ok()?.into_literal().ok()
365
- */
345
+ PreProcessor :: cpp_expr ( definitions, rcc_tokens, location) . ok ( ) ?. const_fold ( ) . ok ( ) ?. into_literal ( ) . ok ( )
366
346
}
367
347
368
348
fn parse_int_literal_tokens ( cursor : & clang:: Cursor ) -> Option < i64 > {
369
- use rcc:: Literal ;
370
-
371
349
let rcc_tokens = cursor. rcc_tokens ( ) . into_iter ( ) ;
372
350
373
351
// TODO(emilio): We can try to parse other kinds of literals.
0 commit comments