diff options
-rw-r--r-- | src/ir/ty.rs | 25 | ||||
-rw-r--r-- | src/ir/var.rs | 15 | ||||
-rwxr-xr-x | src/lib.rs | 2 | ||||
-rw-r--r-- | src/regex_set.rs | 8 |
4 files changed, 30 insertions, 20 deletions
diff --git a/src/ir/ty.rs b/src/ir/ty.rs index 3a17b374..bbb666bc 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -122,13 +122,10 @@ impl Type { } /// Is this an integer type? - pub fn is_integer(&self, ctx: &BindgenContext) -> bool { + pub fn is_integer(&self) -> bool { match self.kind { - TypeKind::UnresolvedTypeRef(..) => false, - _ => match self.canonical_type(ctx).kind { - TypeKind::Int(..) => true, - _ => false, - } + TypeKind::Int(..) => true, + _ => false, } } @@ -311,12 +308,17 @@ impl Type { } } + /// See safe_canonical_type. + pub fn canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) -> &'tr Type { + self.safe_canonical_type(ctx).expect("Should have been resolved after parsing!") + } + /// Returns the canonical type of this type, that is, the "inner type". /// /// For example, for a `typedef`, the canonical type would be the /// `typedef`ed type, for a template specialization, would be the template - /// its specializing, and so on. - pub fn canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) -> &'tr Type { + /// its specializing, and so on. Return None if the type is unresolved. + pub fn safe_canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) -> Option<&'tr Type> { match self.kind { TypeKind::Named(..) | TypeKind::Array(..) | @@ -329,16 +331,15 @@ impl Type { TypeKind::Void | TypeKind::NullPtr | TypeKind::BlockPointer | - TypeKind::Pointer(..) => self, + TypeKind::Pointer(..) => Some(self), TypeKind::ResolvedTypeRef(inner) | TypeKind::Alias(_, inner) | TypeKind::TemplateAlias(inner, _) | TypeKind::TemplateRef(inner, _) - => ctx.resolve_type(inner).canonical_type(ctx), + => ctx.resolve_type(inner).safe_canonical_type(ctx), - TypeKind::UnresolvedTypeRef(..) - => unreachable!("Should have been resolved after parsing!"), + TypeKind::UnresolvedTypeRef(..) => None, } } } diff --git a/src/ir/var.rs b/src/ir/var.rs index 3dd975ee..e9245e1a 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -119,13 +119,14 @@ impl ClangSubItemParser for Var { // tests/headers/inner_const.hpp // // That's fine because in that case we know it's not a literal. - let value = context.safe_resolve_type(ty).map_or(None, |t| { - if t.is_integer(context) { - get_integer_literal_from_cursor(&cursor, context.translation_unit()) - } else { - None - } - }); + let value = context.safe_resolve_type(ty) + .and_then(|t| t.safe_canonical_type(context)).and_then(|t| { + if t.is_integer() { + get_integer_literal_from_cursor(&cursor, context.translation_unit()) + } else { + None + } + }); let mangling = cursor_mangling(&cursor); @@ -56,7 +56,7 @@ mod clangll; doc_mod!(clang); doc_mod!(ir); doc_mod!(parse); -mod regex_set; +doc_mod!(regex_set); mod codegen { include!(concat!(env!("OUT_DIR"), "/codegen.rs")); diff --git a/src/regex_set.rs b/src/regex_set.rs index 20bc56bf..ff899d78 100644 --- a/src/regex_set.rs +++ b/src/regex_set.rs @@ -1,18 +1,24 @@ +//! A type that represents the union of a set of regular expressions. + use std::borrow::Borrow; use regex::Regex; // Yeah, I'm aware this is sorta crappy, should be cheaper to compile a regex // ORing all the patterns, I guess... + +/// A dynamic set of regular expressions. #[derive(Debug)] pub struct RegexSet { items: Vec<Regex> } impl RegexSet { + /// Is this set empty? pub fn is_empty(&self) -> bool { self.items.is_empty() } + /// Extend this set with every regex in the iterator. pub fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=String> { @@ -21,6 +27,7 @@ impl RegexSet { } } + /// Insert a new regex into this set. pub fn insert<S>(&mut self, string: &S) where S: Borrow<str> { @@ -35,6 +42,7 @@ impl RegexSet { } } + /// Does the given `string` match any of the regexes in this set? pub fn matches<S>(&self, string: &S) -> bool where S: Borrow<str> { |