bindgen/
parse.rs

1//! Common traits and types related to parsing our IR from Clang cursors.
2#![deny(clippy::missing_docs_in_private_items)]
3
4use crate::clang;
5use crate::ir::context::{BindgenContext, ItemId};
6
7/// Not so much an error in the traditional sense, but a control flow message
8/// when walking over Clang's AST with a cursor.
9#[derive(Debug)]
10pub(crate) enum ParseError {
11    /// Recurse down the current AST node's children.
12    Recurse,
13    /// Continue on to the next sibling AST node, or back up to the parent's
14    /// siblings if we've exhausted all of this node's siblings (and so on).
15    Continue,
16}
17
18/// The result of parsing a Clang AST node.
19#[derive(Debug)]
20pub(crate) enum ParseResult<T> {
21    /// We've already resolved this item before, here is the extant `ItemId` for
22    /// it.
23    AlreadyResolved(ItemId),
24
25    /// This is a newly parsed item. If the cursor is `Some`, it points to the
26    /// AST node where the new `T` was declared.
27    New(T, Option<clang::Cursor>),
28}
29
30/// An intermediate representation "sub-item" (i.e. one of the types contained
31/// inside an `ItemKind` variant) that can be parsed from a Clang cursor.
32pub(crate) trait ClangSubItemParser: Sized {
33    /// Attempt to parse this type from the given cursor.
34    ///
35    /// The fact that is a reference guarantees it's held by the context, and
36    /// allow returning already existing types.
37    fn parse(
38        cursor: clang::Cursor,
39        context: &mut BindgenContext,
40    ) -> Result<ParseResult<Self>, ParseError>;
41}