diff options
author | Jeff Waugh <jdub@bethesignal.org> | 2016-11-15 14:37:20 +1100 |
---|---|---|
committer | Jeff Waugh <jdub@bethesignal.org> | 2016-11-16 05:31:02 +1100 |
commit | 8270a0ca766ea834032daeb67c7f32a1947ab3bd (patch) | |
tree | b3bbdb0f98e5da995f91c89fbf5b10ecb8290bde /libbindgen/src/ir/module.rs | |
parent | 6e78bb8d56d875619d20e343d0f3109e2d6b6841 (diff) |
Transition to libbindgen sub-crate
- The root crate is the `bindgen` binary
- Rust-ify the test suite, no more subprocesses!
- Update Travis config to test both crates
Diffstat (limited to 'libbindgen/src/ir/module.rs')
-rw-r--r-- | libbindgen/src/ir/module.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/libbindgen/src/ir/module.rs b/libbindgen/src/ir/module.rs new file mode 100644 index 00000000..c5d8cfa7 --- /dev/null +++ b/libbindgen/src/ir/module.rs @@ -0,0 +1,61 @@ +//! Intermediate representation for modules (AKA C++ namespaces). + +use clang; +use parse::{ClangSubItemParser, ParseError, ParseResult}; +use parse_one; +use super::context::{BindgenContext, ItemId}; + +/// A module, as in, a C++ namespace. +#[derive(Clone, Debug)] +pub struct Module { + /// The name of the module, or none if it's anonymous. + name: Option<String>, + /// The children of this module, just here for convenience. + children_ids: Vec<ItemId>, +} + +impl Module { + /// Construct a new `Module`. + pub fn new(name: Option<String>) -> Self { + Module { + name: name, + children_ids: vec![], + } + } + + /// Get this module's name. + pub fn name(&self) -> Option<&str> { + self.name.as_ref().map(|n| &**n) + } + + /// Get a mutable reference to this module's children. + pub fn children_mut(&mut self) -> &mut Vec<ItemId> { + &mut self.children_ids + } + + /// Get this module's children. + pub fn children(&self) -> &[ItemId] { + &self.children_ids + } +} + +impl ClangSubItemParser for Module { + fn parse(cursor: clang::Cursor, + ctx: &mut BindgenContext) + -> Result<ParseResult<Self>, ParseError> { + use clangll::*; + match cursor.kind() { + CXCursor_Namespace => { + let module_id = ctx.module(cursor); + ctx.with_module(module_id, |ctx, children| { + cursor.visit(|cursor| { + parse_one(ctx, cursor, Some(module_id), children) + }) + }); + + Ok(ParseResult::AlreadyResolved(module_id)) + } + _ => Err(ParseError::Continue), + } + } +} |