|
| 1 | +//! Common traits and types related to parsing our IR from Clang cursors. |
| 2 | +
|
1 | 3 | use clang;
|
2 | 4 | use ir::ty::TypeKind;
|
3 | 5 | use ir::item::ItemId;
|
4 | 6 | use ir::context::BindgenContext;
|
5 | 7 |
|
| 8 | +/// Not so much an error in the traditional sense, but a control flow message |
| 9 | +/// when walking over Clang's AST with a cursor. |
6 | 10 | #[derive(Debug)]
|
7 | 11 | pub enum ParseError {
|
| 12 | + /// Recurse down the current AST node's children. |
8 | 13 | Recurse,
|
| 14 | + /// Continue on to the next sibling AST node, or back up to the parent's |
| 15 | + /// siblings if we've exhausted all of this node's siblings (and so on). |
9 | 16 | Continue,
|
10 | 17 | }
|
11 | 18 |
|
| 19 | +/// The result of parsing a Clang AST node. |
12 | 20 | #[derive(Debug)]
|
13 | 21 | pub enum ParseResult<T> {
|
| 22 | + /// We've already resolved this item before, here is the extant `ItemId` for |
| 23 | + /// it. |
14 | 24 | AlreadyResolved(ItemId),
|
| 25 | + |
| 26 | + /// This is a newly parsed item. If the cursor is `Some`, it points to the |
| 27 | + /// AST node where the new `T` was declared. |
15 | 28 | New(T, Option<clang::Cursor>),
|
16 | 29 | }
|
17 | 30 |
|
| 31 | +/// An intermediate representation "sub-item" (i.e. one of the types contained |
| 32 | +/// inside an `ItemKind` variant) that can be parsed from a Clang cursor. |
18 | 33 | pub trait ClangSubItemParser : Sized {
|
| 34 | + /// Attempt to parse this type from the given cursor. |
| 35 | + /// |
19 | 36 | /// The fact that is a reference guarantees it's held by the context, and
|
20 | 37 | /// allow returning already existing types.
|
21 | 38 | fn parse(cursor: clang::Cursor, context: &mut BindgenContext) -> Result<ParseResult<Self>, ParseError>;
|
22 | 39 | }
|
23 | 40 |
|
| 41 | +/// An intermediate representation item that can be parsed from a Clang cursor. |
24 | 42 | pub trait ClangItemParser: Sized {
|
| 43 | + /// Parse this item from the given Clang cursor. |
25 | 44 | fn parse(cursor: clang::Cursor,
|
26 | 45 | parent: Option<ItemId>,
|
27 | 46 | context: &mut BindgenContext) -> Result<ItemId, ParseError>;
|
| 47 | + |
| 48 | + /// Parse this item from the given Clang type. |
| 49 | + fn from_ty(ty: &clang::Type, |
| 50 | + location: Option<clang::Cursor>, |
| 51 | + parent: Option<ItemId>, |
| 52 | + ctx: &mut BindgenContext) -> Result<ItemId, ParseError>; |
| 53 | + |
| 54 | + /// Identical to `from_ty`, but use the given `id` as the `ItemId` for the |
| 55 | + /// newly parsed item. |
| 56 | + fn from_ty_with_id(id: ItemId, |
| 57 | + ty: &clang::Type, |
| 58 | + location: Option<clang::Cursor>, |
| 59 | + parent: Option<ItemId>, |
| 60 | + ctx: &mut BindgenContext) -> Result<ItemId, ParseError>; |
| 61 | + |
| 62 | + /// Parse this item from the given Clang type, or if we haven't resolved all |
| 63 | + /// the other items this one depends on, an unresolved reference. |
28 | 64 | fn from_ty_or_ref(ty: clang::Type,
|
29 | 65 | location: Option<clang::Cursor>,
|
30 | 66 | parent_id: Option<ItemId>,
|
31 | 67 | context: &mut BindgenContext) -> ItemId;
|
| 68 | + |
| 69 | + /// Identical to `from_ty_or_ref`, but use the given `potential_id` as the |
| 70 | + /// `ItemId` for the newly parsed item. |
32 | 71 | fn from_ty_or_ref_with_id(potential_id: ItemId,
|
33 | 72 | ty: clang::Type,
|
34 | 73 | location: Option<clang::Cursor>,
|
35 | 74 | parent_id: Option<ItemId>,
|
36 | 75 | context: &mut BindgenContext) -> ItemId;
|
37 |
| - fn from_ty_with_id(id: ItemId, |
38 |
| - ty: &clang::Type, |
39 |
| - location: Option<clang::Cursor>, |
40 |
| - parent: Option<ItemId>, |
41 |
| - ctx: &mut BindgenContext) -> Result<ItemId, ParseError>; |
42 |
| - fn from_ty(ty: &clang::Type, |
43 |
| - location: Option<clang::Cursor>, |
44 |
| - parent: Option<ItemId>, |
45 |
| - ctx: &mut BindgenContext) -> Result<ItemId, ParseError>; |
| 76 | + |
| 77 | + /// Create a named template type. |
46 | 78 | fn named_type<S>(name: S, default: Option<ItemId>, parent: ItemId,
|
47 | 79 | context: &mut BindgenContext) -> ItemId
|
48 | 80 | where S: Into<String>;
|
| 81 | + |
| 82 | + /// Identical to `named_type`, but use `id` as the resulting item's |
| 83 | + /// `ItemId`. |
49 | 84 | fn named_type_with_id<S>(id: ItemId, name: S, default: Option<ItemId>,
|
50 | 85 | parent: ItemId, context: &mut BindgenContext) -> ItemId
|
51 | 86 | where S: Into<String>;
|
| 87 | + |
| 88 | + /// Create a builtin type. |
52 | 89 | fn builtin_type(kind: TypeKind, is_const: bool, context: &mut BindgenContext) -> ItemId;
|
53 | 90 | }
|
0 commit comments