1use super::super::codegen::MacroTypeVariation;
4use super::context::{BindgenContext, TypeId};
5use super::dot::DotAttributes;
6use super::function::cursor_mangling;
7use super::int::IntKind;
8use super::item::Item;
9use super::ty::{FloatKind, TypeKind};
10use crate::callbacks::{ItemInfo, ItemKind, MacroParsingBehavior};
11use crate::clang;
12use crate::clang::ClangToken;
13use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
14
15use std::io;
16use std::num::Wrapping;
17
18#[derive(Debug)]
20pub(crate) enum VarType {
21 Bool(bool),
23 Int(i64),
25 Float(f64),
27 Char(u8),
29 String(Vec<u8>),
31}
32
33#[derive(Debug)]
35pub(crate) struct Var {
36 name: String,
38 mangled_name: Option<String>,
40 link_name: Option<String>,
42 ty: TypeId,
44 val: Option<VarType>,
46 is_const: bool,
48}
49
50impl Var {
51 pub(crate) fn new(
53 name: String,
54 mangled_name: Option<String>,
55 link_name: Option<String>,
56 ty: TypeId,
57 val: Option<VarType>,
58 is_const: bool,
59 ) -> Var {
60 assert!(!name.is_empty());
61 Var {
62 name,
63 mangled_name,
64 link_name,
65 ty,
66 val,
67 is_const,
68 }
69 }
70
71 pub(crate) fn is_const(&self) -> bool {
73 self.is_const
74 }
75
76 pub(crate) fn val(&self) -> Option<&VarType> {
78 self.val.as_ref()
79 }
80
81 pub(crate) fn ty(&self) -> TypeId {
83 self.ty
84 }
85
86 pub(crate) fn name(&self) -> &str {
88 &self.name
89 }
90
91 pub(crate) fn mangled_name(&self) -> Option<&str> {
93 self.mangled_name.as_deref()
94 }
95
96 pub fn link_name(&self) -> Option<&str> {
98 self.link_name.as_deref()
99 }
100}
101
102impl DotAttributes for Var {
103 fn dot_attributes<W>(
104 &self,
105 _ctx: &BindgenContext,
106 out: &mut W,
107 ) -> io::Result<()>
108 where
109 W: io::Write,
110 {
111 if self.is_const {
112 writeln!(out, "<tr><td>const</td><td>true</td></tr>")?;
113 }
114
115 if let Some(ref mangled) = self.mangled_name {
116 writeln!(out, "<tr><td>mangled name</td><td>{mangled}</td></tr>")?;
117 }
118
119 Ok(())
120 }
121}
122
123fn default_macro_constant_type(ctx: &BindgenContext, value: i64) -> IntKind {
124 if value < 0 ||
125 ctx.options().default_macro_constant_type ==
126 MacroTypeVariation::Signed
127 {
128 if value < i64::from(i32::MIN) || value > i64::from(i32::MAX) {
129 IntKind::I64
130 } else if !ctx.options().fit_macro_constants ||
131 value < i64::from(i16::MIN) ||
132 value > i64::from(i16::MAX)
133 {
134 IntKind::I32
135 } else if value < i64::from(i8::MIN) || value > i64::from(i8::MAX) {
136 IntKind::I16
137 } else {
138 IntKind::I8
139 }
140 } else if value > i64::from(u32::MAX) {
141 IntKind::U64
142 } else if !ctx.options().fit_macro_constants || value > i64::from(u16::MAX)
143 {
144 IntKind::U32
145 } else if value > i64::from(u8::MAX) {
146 IntKind::U16
147 } else {
148 IntKind::U8
149 }
150}
151
152fn handle_function_macro(
155 cursor: &clang::Cursor,
156 callbacks: &dyn crate::callbacks::ParseCallbacks,
157) {
158 let is_closing_paren = |t: &ClangToken| {
159 t.kind == clang_sys::CXToken_Punctuation && t.spelling() == b")"
161 };
162 let tokens: Vec<_> = cursor.tokens().iter().collect();
163 if let Some(boundary) = tokens.iter().position(is_closing_paren) {
164 let mut spelled = tokens.iter().map(ClangToken::spelling);
165 let left = spelled.by_ref().take(boundary + 1);
167 let left = left.collect::<Vec<_>>().concat();
168 if let Ok(left) = String::from_utf8(left) {
169 let right: Vec<_> = spelled.collect();
170 callbacks.func_macro(&left, &right);
171 }
172 }
173}
174
175impl ClangSubItemParser for Var {
176 fn parse(
177 cursor: clang::Cursor,
178 ctx: &mut BindgenContext,
179 ) -> Result<ParseResult<Self>, ParseError> {
180 use cexpr::expr::EvalResult;
181 use cexpr::literal::CChar;
182 use clang_sys::*;
183 match cursor.kind() {
184 CXCursor_MacroDefinition => {
185 for callbacks in &ctx.options().parse_callbacks {
186 match callbacks.will_parse_macro(&cursor.spelling()) {
187 MacroParsingBehavior::Ignore => {
188 return Err(ParseError::Continue);
189 }
190 MacroParsingBehavior::Default => {}
191 }
192
193 if cursor.is_macro_function_like() {
194 handle_function_macro(&cursor, callbacks.as_ref());
195 return Err(ParseError::Continue);
197 }
198 }
199
200 let value = parse_macro(ctx, &cursor);
201
202 let Some((id, value)) = value else {
203 return Err(ParseError::Continue);
204 };
205
206 assert!(!id.is_empty(), "Empty macro name?");
207
208 let previously_defined = ctx.parsed_macro(&id);
209
210 ctx.note_parsed_macro(id.clone(), value.clone());
214
215 if previously_defined {
216 let name = String::from_utf8(id).unwrap();
217 duplicated_macro_diagnostic(&name, cursor.location(), ctx);
218 return Err(ParseError::Continue);
219 }
220
221 let name = String::from_utf8(id).unwrap();
226 let (type_kind, val) = match value {
227 EvalResult::Invalid => return Err(ParseError::Continue),
228 EvalResult::Float(f) => {
229 (TypeKind::Float(FloatKind::Double), VarType::Float(f))
230 }
231 EvalResult::Char(c) => {
232 let c = match c {
233 CChar::Char(c) => {
234 assert_eq!(c.len_utf8(), 1);
235 c as u8
236 }
237 CChar::Raw(c) => {
238 assert!(c <= u64::from(u8::MAX));
239 c as u8
240 }
241 };
242
243 (TypeKind::Int(IntKind::U8), VarType::Char(c))
244 }
245 EvalResult::Str(val) => {
246 let char_ty = Item::builtin_type(
247 TypeKind::Int(IntKind::U8),
248 true,
249 ctx,
250 );
251 for callbacks in &ctx.options().parse_callbacks {
252 callbacks.str_macro(&name, &val);
253 }
254 (TypeKind::Pointer(char_ty), VarType::String(val))
255 }
256 EvalResult::Int(Wrapping(value)) => {
257 let kind = ctx
258 .options()
259 .last_callback(|c| c.int_macro(&name, value))
260 .unwrap_or_else(|| {
261 default_macro_constant_type(ctx, value)
262 });
263
264 (TypeKind::Int(kind), VarType::Int(value))
265 }
266 };
267
268 let ty = Item::builtin_type(type_kind, true, ctx);
269
270 Ok(ParseResult::New(
271 Var::new(name, None, None, ty, Some(val), true),
272 Some(cursor),
273 ))
274 }
275 CXCursor_VarDecl => {
276 let mut name = cursor.spelling();
277 if cursor.linkage() == CXLinkage_External {
278 if let Some(nm) = ctx.options().last_callback(|callbacks| {
279 callbacks.generated_name_override(ItemInfo {
280 name: name.as_str(),
281 kind: ItemKind::Var,
282 })
283 }) {
284 name = nm;
285 }
286 }
287 let name = name;
289
290 if name.is_empty() {
291 warn!("Empty constant name?");
292 return Err(ParseError::Continue);
293 }
294
295 let link_name = ctx.options().last_callback(|callbacks| {
296 callbacks.generated_link_name_override(ItemInfo {
297 name: name.as_str(),
298 kind: ItemKind::Var,
299 })
300 });
301
302 let ty = cursor.cur_type();
303
304 let is_const = ty.is_const() ||
307 ([CXType_ConstantArray, CXType_IncompleteArray]
308 .contains(&ty.kind()) &&
309 ty.elem_type()
310 .is_some_and(|element| element.is_const()));
311
312 let ty = match Item::from_ty(&ty, cursor, None, ctx) {
313 Ok(ty) => ty,
314 Err(e) => {
315 assert!(
316 matches!(ty.kind(), CXType_Auto | CXType_Unexposed),
317 "Couldn't resolve constant type, and it \
318 wasn't an nondeductible auto type or unexposed \
319 type: {ty:?}"
320 );
321 return Err(e);
322 }
323 };
324
325 let canonical_ty = ctx
330 .safe_resolve_type(ty)
331 .and_then(|t| t.safe_canonical_type(ctx));
332
333 let is_integer = canonical_ty.is_some_and(|t| t.is_integer());
334 let is_float = canonical_ty.is_some_and(|t| t.is_float());
335
336 let value = if is_integer {
341 let TypeKind::Int(kind) = *canonical_ty.unwrap().kind()
342 else {
343 unreachable!()
344 };
345
346 let mut val = cursor.evaluate().and_then(|v| v.as_int());
347 if val.is_none() || !kind.signedness_matches(val.unwrap()) {
348 val = get_integer_literal_from_cursor(&cursor);
349 }
350
351 val.map(|val| {
352 if kind == IntKind::Bool {
353 VarType::Bool(val != 0)
354 } else {
355 VarType::Int(val)
356 }
357 })
358 } else if is_float {
359 cursor
360 .evaluate()
361 .and_then(|v| v.as_double())
362 .map(VarType::Float)
363 } else {
364 cursor
365 .evaluate()
366 .and_then(|v| v.as_literal_string())
367 .map(VarType::String)
368 };
369
370 let mangling = cursor_mangling(ctx, &cursor);
371 let var =
372 Var::new(name, mangling, link_name, ty, value, is_const);
373
374 Ok(ParseResult::New(var, Some(cursor)))
375 }
376 _ => {
377 Err(ParseError::Continue)
379 }
380 }
381 }
382}
383
384fn parse_macro_clang_fallback(
394 ctx: &mut BindgenContext,
395 cursor: &clang::Cursor,
396) -> Option<(Vec<u8>, cexpr::expr::EvalResult)> {
397 if !ctx.options().clang_macro_fallback {
398 return None;
399 }
400
401 let ftu = ctx.try_ensure_fallback_translation_unit()?;
402 let contents = format!("int main() {{ {}; }}", cursor.spelling());
403 ftu.reparse(&contents).ok()?;
404 let root_children = ftu.translation_unit().cursor().collect_children();
406 let main_func = root_children.last()?;
409 let all_stmts = main_func.collect_children();
411 let macro_stmt = all_stmts.first()?;
414 let paren_exprs = macro_stmt.collect_children();
416 let paren = paren_exprs.first()?;
419
420 Some((
421 cursor.spelling().into_bytes(),
422 cexpr::expr::EvalResult::Int(Wrapping(paren.evaluate()?.as_int()?)),
423 ))
424}
425
426fn parse_macro(
428 ctx: &mut BindgenContext,
429 cursor: &clang::Cursor,
430) -> Option<(Vec<u8>, cexpr::expr::EvalResult)> {
431 use cexpr::expr;
432
433 let cexpr_tokens = cursor.cexpr_tokens();
434
435 let parser = expr::IdentifierParser::new(ctx.parsed_macros());
436
437 match parser.macro_definition(&cexpr_tokens) {
438 Ok((_, (id, val))) => Some((id.into(), val)),
439 _ => parse_macro_clang_fallback(ctx, cursor),
440 }
441}
442
443fn parse_int_literal_tokens(cursor: &clang::Cursor) -> Option<i64> {
444 use cexpr::expr;
445 use cexpr::expr::EvalResult;
446
447 let cexpr_tokens = cursor.cexpr_tokens();
448
449 match expr::expr(&cexpr_tokens) {
451 Ok((_, EvalResult::Int(Wrapping(val)))) => Some(val),
452 _ => None,
453 }
454}
455
456fn get_integer_literal_from_cursor(cursor: &clang::Cursor) -> Option<i64> {
457 use clang_sys::*;
458 let mut value = None;
459 cursor.visit(|c| {
460 match c.kind() {
461 CXCursor_IntegerLiteral | CXCursor_UnaryOperator => {
462 value = parse_int_literal_tokens(&c);
463 }
464 CXCursor_UnexposedExpr => {
465 value = get_integer_literal_from_cursor(&c);
466 }
467 _ => (),
468 }
469 if value.is_some() {
470 CXChildVisit_Break
471 } else {
472 CXChildVisit_Continue
473 }
474 });
475 value
476}
477
478fn duplicated_macro_diagnostic(
479 macro_name: &str,
480 _location: clang::SourceLocation,
481 _ctx: &BindgenContext,
482) {
483 warn!("Duplicated macro definition: {macro_name}");
484
485 #[cfg(feature = "experimental")]
486 #[allow(clippy::overly_complex_bool_expr)]
499 if false && _ctx.options().emit_diagnostics {
500 use crate::diagnostics::{get_line, Diagnostic, Level, Slice};
501 use std::borrow::Cow;
502
503 let mut slice = Slice::default();
504 let mut source = Cow::from(macro_name);
505
506 let (file, line, col, _) = _location.location();
507 if let Some(filename) = file.name() {
508 if let Ok(Some(code)) = get_line(&filename, line) {
509 source = code.into();
510 }
511 slice.with_location(filename, line, col);
512 }
513
514 slice.with_source(source);
515
516 Diagnostic::default()
517 .with_title("Duplicated macro definition.", Level::Warning)
518 .add_slice(slice)
519 .add_annotation("This macro had a duplicate.", Level::Note)
520 .display();
521 }
522}