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