summaryrefslogtreecommitdiff
path: root/libbindgen/src/ir
diff options
context:
space:
mode:
Diffstat (limited to 'libbindgen/src/ir')
-rw-r--r--libbindgen/src/ir/annotations.rs157
-rw-r--r--libbindgen/src/ir/comp.rs871
-rw-r--r--libbindgen/src/ir/context.rs1055
-rw-r--r--libbindgen/src/ir/enum_ty.rs137
-rw-r--r--libbindgen/src/ir/function.rs282
-rw-r--r--libbindgen/src/ir/int.rs93
-rw-r--r--libbindgen/src/ir/item.rs1172
-rw-r--r--libbindgen/src/ir/item_kind.rs114
-rw-r--r--libbindgen/src/ir/layout.rs34
-rw-r--r--libbindgen/src/ir/mod.rs18
-rw-r--r--libbindgen/src/ir/module.rs61
-rw-r--r--libbindgen/src/ir/ty.rs869
-rw-r--r--libbindgen/src/ir/type_collector.rs22
-rw-r--r--libbindgen/src/ir/var.rs246
14 files changed, 5131 insertions, 0 deletions
diff --git a/libbindgen/src/ir/annotations.rs b/libbindgen/src/ir/annotations.rs
new file mode 100644
index 00000000..58308d6d
--- /dev/null
+++ b/libbindgen/src/ir/annotations.rs
@@ -0,0 +1,157 @@
+//! Types and functions related to bindgen annotation comments.
+//!
+//! Users can add annotations in doc comments to types that they would like to
+//! replace other types with, mark as opaque, etc. This module deals with all of
+//! that stuff.
+
+use clang;
+
+/// What kind of accessor should we provide for a field?
+#[derive(Copy, PartialEq, Clone, Debug)]
+pub enum FieldAccessorKind {
+ /// No accessor.
+ None,
+ /// Plain accessor.
+ Regular,
+ /// Unsafe accessor.
+ Unsafe,
+ /// Immutable accessor.
+ Immutable,
+}
+
+/// Annotations for a given item, or a field.
+#[derive(Clone, PartialEq, Debug)]
+pub struct Annotations {
+ /// Whether this item is marked as opaque. Only applies to types.
+ opaque: bool,
+ /// Whether this item should be hidden from the output. Only applies to
+ /// types.
+ hide: bool,
+ /// Whether this type should be replaced by another. The name must be the
+ /// canonical name that that type would get.
+ use_instead_of: Option<String>,
+ /// Manually disable deriving copy/clone on this type. Only applies to
+ /// struct or union types.
+ disallow_copy: bool,
+ /// Whether fields should be marked as private or not. You can set this on
+ /// structs (it will apply to all the fields), or individual fields.
+ private_fields: Option<bool>,
+ /// The kind of accessor this field will have. Also can be applied to
+ /// structs so all the fields inside share it by default.
+ accessor_kind: Option<FieldAccessorKind>,
+}
+
+fn parse_accessor(s: &str) -> FieldAccessorKind {
+ match s {
+ "false" => FieldAccessorKind::None,
+ "unsafe" => FieldAccessorKind::Unsafe,
+ "immutable" => FieldAccessorKind::Immutable,
+ _ => FieldAccessorKind::Regular,
+ }
+}
+
+impl Default for Annotations {
+ fn default() -> Self {
+ Annotations {
+ opaque: false,
+ hide: false,
+ use_instead_of: None,
+ disallow_copy: false,
+ private_fields: None,
+ accessor_kind: None,
+ }
+ }
+}
+
+impl Annotations {
+ /// Construct new annotations for the given cursor and its bindgen comments
+ /// (if any).
+ pub fn new(cursor: &clang::Cursor) -> Option<Annotations> {
+ let mut anno = Annotations::default();
+ let mut matched_one = false;
+ anno.parse(&cursor.comment(), &mut matched_one);
+
+ if matched_one { Some(anno) } else { None }
+ }
+
+ /// Should this type be hidden?
+ pub fn hide(&self) -> bool {
+ self.hide
+ }
+
+ /// Should this type be opaque?
+ pub fn opaque(&self) -> bool {
+ self.opaque
+ }
+
+ /// For a given type, indicates the type it should replace.
+ ///
+ /// For example, in the following code:
+ ///
+ /// ```cpp
+ ///
+ /// /** <div rustbindgen replaces="Bar"></div> */
+ /// struct Foo { int x; };
+ ///
+ /// struct Bar { char foo; };
+ /// ```
+ ///
+ /// the generated code would look something like:
+ ///
+ /// ```
+ /// /** <div rustbindgen replaces="Bar"></div> */
+ /// struct Bar {
+ /// x: ::std::os::raw::c_int,
+ /// };
+ /// ```
+ ///
+ /// That is, code for `Foo` is used to generate `Bar`.
+ pub fn use_instead_of(&self) -> Option<&str> {
+ self.use_instead_of.as_ref().map(|s| &**s)
+ }
+
+ /// Should we avoid implementing the `Copy` trait?
+ pub fn disallow_copy(&self) -> bool {
+ self.disallow_copy
+ }
+
+ /// Should the fields be private?
+ pub fn private_fields(&self) -> Option<bool> {
+ self.private_fields
+ }
+
+ /// What kind of accessors should we provide for this type's fields?
+ pub fn accessor_kind(&self) -> Option<FieldAccessorKind> {
+ self.accessor_kind
+ }
+
+ fn parse(&mut self, comment: &clang::Comment, matched: &mut bool) {
+ use clangll::CXComment_HTMLStartTag;
+ if comment.kind() == CXComment_HTMLStartTag &&
+ comment.get_tag_name() == "div" &&
+ comment.get_tag_attrs()
+ .next()
+ .map_or(false, |attr| attr.name == "rustbindgen") {
+ *matched = true;
+ for attr in comment.get_tag_attrs() {
+ match attr.name.as_str() {
+ "opaque" => self.opaque = true,
+ "hide" => self.hide = true,
+ "nocopy" => self.disallow_copy = true,
+ "replaces" => self.use_instead_of = Some(attr.value),
+ "private" => {
+ self.private_fields = Some(attr.value != "false")
+ }
+ "accessor" => {
+ self.accessor_kind = Some(parse_accessor(&attr.value))
+ }
+ _ => {}
+ }
+ }
+ }
+
+ for child in comment.get_children() {
+ self.parse(&child, matched);
+ }
+ }
+}
diff --git a/libbindgen/src/ir/comp.rs b/libbindgen/src/ir/comp.rs
new file mode 100644
index 00000000..d19d1209
--- /dev/null
+++ b/libbindgen/src/ir/comp.rs
@@ -0,0 +1,871 @@
+//! Compound types (unions and structs) in our intermediate representation.
+
+use clang;
+use parse::{ClangItemParser, ParseError};
+use std::cell::Cell;
+use std::cmp;
+use super::annotations::Annotations;
+use super::context::{BindgenContext, ItemId};
+use super::item::Item;
+use super::layout::Layout;
+use super::ty::{RUST_DERIVE_IN_ARRAY_LIMIT, Type};
+use super::type_collector::{ItemSet, TypeCollector};
+
+/// The kind of compound type.
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub enum CompKind {
+ /// A struct.
+ Struct,
+ /// A union.
+ Union,
+}
+
+/// The kind of C++ method.
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub enum MethodKind {
+ /// A static method.
+ Static,
+ /// A normal method.
+ Normal,
+ /// A virtual method.
+ Virtual,
+}
+
+/// A struct representing a C++ method, either static, normal, or virtual.
+#[derive(Debug)]
+pub struct Method {
+ kind: MethodKind,
+ /// The signature of the method. Take into account this is not a `Type`
+ /// item, but a `Function` one.
+ ///
+ /// This is tricky and probably this field should be renamed.
+ signature: ItemId,
+ is_const: bool,
+}
+
+impl Method {
+ /// Construct a new `Method`.
+ fn new(kind: MethodKind, signature: ItemId, is_const: bool) -> Self {
+ Method {
+ kind: kind,
+ signature: signature,
+ is_const: is_const,
+ }
+ }
+
+ /// What kind of method is this?
+ pub fn kind(&self) -> MethodKind {
+ self.kind
+ }
+
+ /// Is this a virtual method?
+ pub fn is_virtual(&self) -> bool {
+ self.kind == MethodKind::Virtual
+ }
+
+ /// Is this a static method?
+ pub fn is_static(&self) -> bool {
+ self.kind == MethodKind::Static
+ }
+
+ /// Get the `ItemId` for the `Function` signature for this method.
+ pub fn signature(&self) -> ItemId {
+ self.signature
+ }
+
+ /// Is this a const qualified method?
+ pub fn is_const(&self) -> bool {
+ self.is_const
+ }
+}
+
+/// A struct representing a C++ field.
+#[derive(Clone, Debug)]
+pub struct Field {
+ /// The name of the field, empty if it's an unnamed bitfield width.
+ name: Option<String>,
+ /// The inner type.
+ ty: ItemId,
+ /// The doc comment on the field if any.
+ comment: Option<String>,
+ /// Annotations for this field, or the default.
+ annotations: Annotations,
+ /// If this field is a bitfield, and how many bits does it contain if it is.
+ bitfield: Option<u32>,
+ /// If the C++ field is marked as `mutable`
+ mutable: bool,
+}
+
+impl Field {
+ /// Construct a new `Field`.
+ pub fn new(name: Option<String>,
+ ty: ItemId,
+ comment: Option<String>,
+ annotations: Option<Annotations>,
+ bitfield: Option<u32>,
+ mutable: bool)
+ -> Field {
+ Field {
+ name: name,
+ ty: ty,
+ comment: comment,
+ annotations: annotations.unwrap_or_default(),
+ bitfield: bitfield,
+ mutable: mutable,
+ }
+ }
+
+ /// Get the name of this field.
+ pub fn name(&self) -> Option<&str> {
+ self.name.as_ref().map(|n| &**n)
+ }
+
+ /// Get the type of this field.
+ pub fn ty(&self) -> ItemId {
+ self.ty
+ }
+
+ /// Get the comment for this field.
+ pub fn comment(&self) -> Option<&str> {
+ self.comment.as_ref().map(|c| &**c)
+ }
+
+ /// If this is a bitfield, how many bits does it need?
+ pub fn bitfield(&self) -> Option<u32> {
+ self.bitfield
+ }
+
+ /// Is this field marked as `mutable`?
+ pub fn is_mutable(&self) -> bool {
+ self.mutable
+ }
+
+ /// Get the annotations for this field.
+ pub fn annotations(&self) -> &Annotations {
+ &self.annotations
+ }
+}
+
+/// A compound type.
+///
+/// Either a struct or union, a compound type is built up from the combination
+/// of fields which also are associated with their own (potentially compound)
+/// type.
+#[derive(Debug)]
+pub struct CompInfo {
+ /// Whether this is a struct or a union.
+ kind: CompKind,
+
+ /// The members of this struct or union.
+ fields: Vec<Field>,
+
+ /// The template parameters of this class. These are non-concrete, and
+ /// should always be a Type(TypeKind::Named(name)), but still they need to
+ /// be registered with an unique type id in the context.
+ template_args: Vec<ItemId>,
+
+ /// The method declarations inside this class, if in C++ mode.
+ methods: Vec<Method>,
+
+ /// Vector of classes this one inherits from.
+ base_members: Vec<ItemId>,
+
+ /// The parent reference template if any.
+ ref_template: Option<ItemId>,
+
+ /// The inner types that were declared inside this class, in something like:
+ ///
+ /// class Foo {
+ /// typedef int FooTy;
+ /// struct Bar {
+ /// int baz;
+ /// };
+ /// }
+ ///
+ /// static Foo::Bar const = {3};
+ inner_types: Vec<ItemId>,
+
+ /// Set of static constants declared inside this class.
+ inner_vars: Vec<ItemId>,
+
+ /// Whether this type should generate an vtable (TODO: Should be able to
+ /// look at the virtual methods and ditch this field).
+ has_vtable: bool,
+
+ /// Whether this type has destructor.
+ has_destructor: bool,
+
+ /// Whether this type has a base type with more than one member.
+ ///
+ /// TODO: We should be able to compute this.
+ has_nonempty_base: bool,
+
+ /// If this type has a template parameter which is not a type (e.g.: a
+ /// size_t)
+ has_non_type_template_params: bool,
+
+ /// Whether this struct layout is packed.
+ packed: bool,
+
+ /// Whether this struct is anonymous.
+ is_anonymous: bool,
+
+ /// Used to know if we've found an opaque attribute that could cause us to
+ /// generate a type with invalid layout. This is explicitly used to avoid us
+ /// generating bad alignments when parsing types like max_align_t.
+ ///
+ /// It's not clear what the behavior should be here, if generating the item
+ /// and pray, or behave as an opaque type.
+ found_unknown_attr: bool,
+
+ /// Used to detect if we've run in a can_derive_debug cycle while cycling
+ /// around the template arguments.
+ detect_derive_debug_cycle: Cell<bool>,
+
+ /// Used to detect if we've run in a has_destructor cycle while cycling
+ /// around the template arguments.
+ detect_has_destructor_cycle: Cell<bool>,
+}
+
+impl CompInfo {
+ /// Construct a new compound type.
+ pub fn new(kind: CompKind) -> Self {
+ CompInfo {
+ kind: kind,
+ fields: vec![],
+ template_args: vec![],
+ methods: vec![],
+ base_members: vec![],
+ ref_template: None,
+ inner_types: vec![],
+ inner_vars: vec![],
+ has_vtable: false,
+ has_destructor: false,
+ has_nonempty_base: false,
+ has_non_type_template_params: false,
+ packed: false,
+ is_anonymous: false,
+ found_unknown_attr: false,
+ detect_derive_debug_cycle: Cell::new(false),
+ detect_has_destructor_cycle: Cell::new(false),
+ }
+ }
+
+ /// Can we derive the `Debug` trait for this compound type?
+ pub fn can_derive_debug(&self,
+ ctx: &BindgenContext,
+ layout: Option<Layout>)
+ -> bool {
+ // We can reach here recursively via template parameters of a member,
+ // for example.
+ if self.detect_derive_debug_cycle.get() {
+ warn!("Derive debug cycle detected!");
+ return true;
+ }
+
+ if self.kind == CompKind::Union {
+ if ctx.options().unstable_rust {
+ return false;
+ }
+
+ let layout = layout.unwrap_or_else(Layout::zero);
+ let size_divisor = cmp::max(1, layout.align);
+ return layout.size / size_divisor <= RUST_DERIVE_IN_ARRAY_LIMIT;
+ }
+
+ self.detect_derive_debug_cycle.set(true);
+
+ let can_derive_debug = {
+ self.base_members
+ .iter()
+ .all(|ty| ctx.resolve_type(*ty).can_derive_debug(ctx)) &&
+ self.template_args
+ .iter()
+ .all(|ty| ctx.resolve_type(*ty).can_derive_debug(ctx)) &&
+ self.fields
+ .iter()
+ .all(|f| ctx.resolve_type(f.ty).can_derive_debug(ctx)) &&
+ self.ref_template.map_or(true, |template| {
+ ctx.resolve_type(template).can_derive_debug(ctx)
+ })
+ };
+
+ self.detect_derive_debug_cycle.set(false);
+
+ can_derive_debug
+ }
+
+ /// Is this compound type unsized?
+ pub fn is_unsized(&self, ctx: &BindgenContext) -> bool {
+ !self.has_vtable(ctx) && self.fields.is_empty() &&
+ self.base_members.iter().all(|base| {
+ ctx.resolve_type(*base).canonical_type(ctx).is_unsized(ctx)
+ }) &&
+ self.ref_template
+ .map_or(true, |template| ctx.resolve_type(template).is_unsized(ctx))
+ }
+
+ /// Does this compound type have a destructor?
+ pub fn has_destructor(&self, ctx: &BindgenContext) -> bool {
+ if self.detect_has_destructor_cycle.get() {
+ warn!("Cycle detected looking for destructors");
+ // Assume no destructor, since we don't have an explicit one.
+ return false;
+ }
+
+ self.detect_has_destructor_cycle.set(true);
+
+ let has_destructor = self.has_destructor ||
+ match self.kind {
+ CompKind::Union => false,
+ CompKind::Struct => {
+ // NB: We can't rely on a type with type parameters
+ // not having destructor.
+ //
+ // This is unfortunate, but...
+ self.ref_template.as_ref().map_or(false, |t| {
+ ctx.resolve_type(*t).has_destructor(ctx)
+ }) ||
+ self.template_args
+ .iter()
+ .any(|t| ctx.resolve_type(*t).has_destructor(ctx)) ||
+ self.base_members
+ .iter()
+ .any(|t| ctx.resolve_type(*t).has_destructor(ctx)) ||
+ self.fields.iter().any(|field| {
+ ctx.resolve_type(field.ty)
+ .has_destructor(ctx)
+ })
+ }
+ };
+
+ self.detect_has_destructor_cycle.set(false);
+
+ has_destructor
+ }
+
+ /// Can we derive the `Copy` trait for this type?
+ pub fn can_derive_copy(&self, ctx: &BindgenContext, item: &Item) -> bool {
+ // NOTE: Take into account that while unions in C and C++ are copied by
+ // default, the may have an explicit destructor in C++, so we can't
+ // defer this check just for the union case.
+ if self.has_destructor(ctx) {
+ return false;
+ }
+
+ if self.kind == CompKind::Union {
+ if !ctx.options().unstable_rust {
+ return true;
+ }
+
+ // https://github.com/rust-lang/rust/issues/36640
+ if !self.template_args.is_empty() || self.ref_template.is_some() ||
+ !item.applicable_template_args(ctx).is_empty() {
+ return false;
+ }
+ }
+
+ // With template args, use a safe subset of the types,
+ // since copyability depends on the types itself.
+ self.ref_template
+ .as_ref()
+ .map_or(true, |t| ctx.resolve_item(*t).can_derive_copy(ctx)) &&
+ self.base_members
+ .iter()
+ .all(|t| ctx.resolve_item(*t).can_derive_copy(ctx)) &&
+ self.fields.iter().all(|field| {
+ ctx.resolve_item(field.ty)
+ .can_derive_copy(ctx)
+ })
+ }
+
+ /// Is this type a template specialization?
+ pub fn is_template_specialization(&self) -> bool {
+ self.ref_template.is_some()
+ }
+
+ /// Get the template declaration this specialization is specializing.
+ pub fn specialized_template(&self) -> Option<ItemId> {
+ self.ref_template
+ }
+
+ /// Compute the layout of this type.
+ ///
+ /// This is called as a fallback under some circumstances where LLVM doesn't
+ /// give us the correct layout.
+ ///
+ /// If we're a union without known layout, we try to compute it from our
+ /// members. This is not ideal, but clang fails to report the size for these
+ /// kind of unions, see test/headers/template_union.hpp
+ pub fn layout(&self, ctx: &BindgenContext) -> Option<Layout> {
+ use std::cmp;
+
+ // We can't do better than clang here, sorry.
+ if self.kind == CompKind::Struct {
+ return None;
+ }
+
+ let mut max_size = 0;
+ let mut max_align = 0;
+ for field in &self.fields {
+ let field_layout = ctx.resolve_type(field.ty)
+ .layout(ctx);
+
+ if let Some(layout) = field_layout {
+ max_size = cmp::max(max_size, layout.size);
+ max_align = cmp::max(max_align, layout.align);
+ }
+ }
+
+ Some(Layout::new(max_size, max_align))
+ }
+
+ /// Get this type's set of fields.
+ pub fn fields(&self) -> &[Field] {
+ &self.fields
+ }
+
+ /// Get this type's set of free template arguments. Empty if this is not a
+ /// template.
+ pub fn template_args(&self) -> &[ItemId] {
+ &self.template_args
+ }
+
+ /// Does this type have any template parameters that aren't types
+ /// (e.g. int)?
+ pub fn has_non_type_template_params(&self) -> bool {
+ self.has_non_type_template_params
+ }
+
+ /// Does this type have a virtual table?
+ pub fn has_vtable(&self, ctx: &BindgenContext) -> bool {
+ self.has_vtable ||
+ self.base_members().iter().any(|base| {
+ ctx.resolve_type(*base)
+ .has_vtable(ctx)
+ }) ||
+ self.ref_template.map_or(false, |template| {
+ ctx.resolve_type(template).has_vtable(ctx)
+ })
+ }
+
+ /// Get this type's set of methods.
+ pub fn methods(&self) -> &[Method] {
+ &self.methods
+ }
+
+ /// What kind of compound type is this?
+ pub fn kind(&self) -> CompKind {
+ self.kind
+ }
+
+ /// The set of types that this one inherits from.
+ pub fn base_members(&self) -> &[ItemId] {
+ &self.base_members
+ }
+
+ /// Construct a new compound type from a Clang type.
+ pub fn from_ty(potential_id: ItemId,
+ ty: &clang::Type,
+ location: Option<clang::Cursor>,
+ ctx: &mut BindgenContext)
+ -> Result<Self, ParseError> {
+ use clangll::*;
+ // Sigh... For class templates we want the location, for
+ // specialisations, we want the declaration... So just try both.
+ //
+ // TODO: Yeah, this code reads really bad.
+ let mut cursor = ty.declaration();
+ let mut kind = Self::kind_from_cursor(&cursor);
+ if kind.is_err() {
+ if let Some(location) = location {
+ kind = Self::kind_from_cursor(&location);
+ cursor = location;
+ }
+ }
+
+ let kind = try!(kind);
+
+ debug!("CompInfo::from_ty({:?}, {:?})", kind, cursor);
+
+ let mut ci = CompInfo::new(kind);
+ ci.is_anonymous = cursor.is_anonymous();
+ ci.template_args = match ty.template_args() {
+ // In forward declarations and not specializations,
+ // etc, they are in
+ // the ast, we'll meet them in
+ // CXCursor_TemplateTypeParameter
+ None => vec![],
+ Some(arg_types) => {
+ let num_arg_types = arg_types.len();
+
+ let args = arg_types.filter(|t| t.kind() != CXType_Invalid)
+ .map(|t| Item::from_ty_or_ref(t, None, None, ctx))
+ .collect::<Vec<_>>();
+
+ if args.len() != num_arg_types {
+ ci.has_non_type_template_params = true;
+ warn!("warning: Template parameter is not a type");
+ }
+
+ args
+ }
+ };
+
+ ci.ref_template = cursor.specialized()
+ .and_then(|c| Item::parse(c, None, ctx).ok());
+
+ let mut maybe_anonymous_struct_field = None;
+ cursor.visit(|cur| {
+ if cur.kind() != CXCursor_FieldDecl {
+ if let Some((ty, _)) = maybe_anonymous_struct_field {
+ let field = Field::new(None, ty, None, None, None, false);
+ ci.fields.push(field);
+ }
+ maybe_anonymous_struct_field = None;
+ }
+
+ match cur.kind() {
+ CXCursor_FieldDecl => {
+ match maybe_anonymous_struct_field.take() {
+ Some((ty, clang_ty)) => {
+ let mut used = false;
+ cur.visit(|child| {
+ if child.cur_type() == clang_ty {
+ used = true;
+ }
+ CXChildVisit_Continue
+ });
+ if !used {
+ let field = Field::new(None,
+ ty,
+ None,
+ None,
+ None,
+ false);
+ ci.fields.push(field);
+ }
+ }
+ None => {}
+ }
+
+ let bit_width = cur.bit_width();
+ let field_type = Item::from_ty_or_ref(cur.cur_type(),
+ Some(cur),
+ Some(potential_id),
+ ctx);
+
+ let comment = cur.raw_comment();
+ let annotations = Annotations::new(&cur);
+ let name = cur.spelling();
+ let is_mutable = cursor.is_mutable_field();
+
+ // Name can be empty if there are bitfields, for example,
+ // see tests/headers/struct_with_bitfields.h
+ assert!(!name.is_empty() || bit_width.is_some(),
+ "Empty field name?");
+
+ let name = if name.is_empty() { None } else { Some(name) };
+
+ let field = Field::new(name,
+ field_type,
+ comment,
+ annotations,
+ bit_width,
+ is_mutable);
+ ci.fields.push(field);
+
+ // No we look for things like attributes and stuff.
+ cur.visit(|cur| {
+ if cur.kind() == CXCursor_UnexposedAttr {
+ ci.found_unknown_attr = true;
+ }
+ CXChildVisit_Continue
+ });
+
+ }
+ CXCursor_UnexposedAttr => {
+ ci.found_unknown_attr = true;
+ }
+ CXCursor_EnumDecl |
+ CXCursor_TypeAliasDecl |
+ CXCursor_TypedefDecl |
+ CXCursor_StructDecl |
+ CXCursor_UnionDecl |
+ CXCursor_ClassTemplate |
+ CXCursor_ClassDecl => {
+ let inner = Item::parse(cur, Some(potential_id), ctx)
+ .expect("Inner ClassDecl");
+ if !ci.inner_types.contains(&inner) {
+ ci.inner_types.push(inner);
+ }
+ // A declaration of an union or a struct without name could
+ // also be an unnamed field, unfortunately.
+ if cur.spelling().is_empty() &&
+ cur.kind() != CXCursor_EnumDecl {
+ let ty = cur.cur_type();
+ maybe_anonymous_struct_field = Some((inner, ty));
+ }
+ }
+ CXCursor_PackedAttr => {
+ ci.packed = true;
+ }
+ CXCursor_TemplateTypeParameter => {
+ // Yes! You can arrive here with an empty template parameter
+ // name! Awesome, isn't it?
+ //
+ // see tests/headers/empty_template_param_name.hpp
+ if cur.spelling().is_empty() {
+ return CXChildVisit_Continue;
+ }
+
+ let default_type = Item::from_ty(&cur.cur_type(),
+ Some(cur),
+ Some(potential_id),
+ ctx)
+ .ok();
+ let param = Item::named_type(cur.spelling(),
+ default_type,
+ potential_id,
+ ctx);
+ ci.template_args.push(param);
+ }
+ CXCursor_CXXBaseSpecifier => {
+ if !ci.has_vtable {
+ ci.has_vtable = cur.is_virtual_base();
+ }
+ let type_id =
+ Item::from_ty(&cur.cur_type(), Some(cur), None, ctx)
+ .expect("BaseSpecifier");
+ ci.base_members.push(type_id);
+ }
+ CXCursor_CXXMethod => {
+ let is_virtual = cur.method_is_virtual();
+ let is_static = cur.method_is_static();
+ debug_assert!(!(is_static && is_virtual), "How?");
+
+ if !ci.has_vtable {
+ ci.has_vtable = is_virtual;
+ }
+
+ let linkage = cur.linkage();
+ if linkage != CXLinkage_External {
+ return CXChildVisit_Continue;
+ }
+
+ if cur.access_specifier() == CX_CXXPrivate {
+ return CXChildVisit_Continue;
+ }
+
+ let visibility = cur.visibility();
+ if visibility != CXVisibility_Default {
+ return CXChildVisit_Continue;
+ }
+
+ if cur.is_inlined_function() {
+ return CXChildVisit_Continue;
+ }
+
+ let spelling = cur.spelling();
+ if spelling.starts_with("operator") {
+ return CXChildVisit_Continue;
+ }
+
+ // This used to not be here, but then I tried generating
+ // stylo bindings with this (without path filters), and
+ // cried a lot with a method in gfx/Point.h
+ // (ToUnknownPoint), that somehow was causing the same type
+ // to be inserted in the map two times.
+ //
+ // I couldn't make a reduced test case, but anyway...
+ // Methods of template functions not only use to be inlined,
+ // but also instantiated, and we wouldn't be able to call
+ // them, so just bail out.
+ if !ci.template_args.is_empty() {
+ return CXChildVisit_Continue;
+ }
+
+ // NB: This gets us an owned `Function`, not a
+ // `FunctionSig`.
+ let method_signature =
+ Item::parse(cur, Some(potential_id), ctx)
+ .expect("CXXMethod");
+
+ let is_const = cur.method_is_const();
+ let method_kind = if is_static {
+ MethodKind::Static
+ } else if is_virtual {
+ MethodKind::Virtual
+ } else {
+ MethodKind::Normal
+ };
+
+ let method =
+ Method::new(method_kind, method_signature, is_const);
+
+ ci.methods.push(method);
+ }
+ CXCursor_Destructor => {
+ if cur.method_is_virtual() {
+ // FIXME: Push to the method list?
+ ci.has_vtable = true;
+ }
+ ci.has_destructor = true;
+ }
+ CXCursor_NonTypeTemplateParameter => {
+ ci.has_non_type_template_params = true;
+ }
+ CXCursor_VarDecl => {
+ let linkage = cur.linkage();
+ if linkage != CXLinkage_External &&
+ linkage != CXLinkage_UniqueExternal {
+ return CXChildVisit_Continue;
+ }
+
+ let visibility = cur.visibility();
+ if visibility != CXVisibility_Default {
+ return CXChildVisit_Continue;
+ }
+
+ let item = Item::parse(cur, Some(potential_id), ctx)
+ .expect("VarDecl");
+ ci.inner_vars.push(item);
+ }
+ // Intentionally not handled
+ CXCursor_CXXAccessSpecifier |
+ CXCursor_CXXFinalAttr |
+ CXCursor_Constructor |
+ CXCursor_FunctionTemplate |
+ CXCursor_ConversionFunction => {}
+ _ => {
+ warn!("unhandled comp member `{}` (kind {}) in `{}` ({})",
+ cur.spelling(),
+ cur.kind(),
+ cursor.spelling(),
+ cur.location());
+ }
+ }
+ CXChildVisit_Continue
+ });
+
+ if let Some((ty, _)) = maybe_anonymous_struct_field {
+ let field = Field::new(None, ty, None, None, None, false);
+ ci.fields.push(field);
+ }
+
+ Ok(ci)
+ }
+
+ fn kind_from_cursor(cursor: &clang::Cursor)
+ -> Result<CompKind, ParseError> {
+ use clangll::*;
+ Ok(match cursor.kind() {
+ CXCursor_UnionDecl => CompKind::Union,
+ CXCursor_ClassDecl |
+ CXCursor_StructDecl => CompKind::Struct,
+ CXCursor_CXXBaseSpecifier |
+ CXCursor_ClassTemplatePartialSpecialization |
+ CXCursor_ClassTemplate => {
+ match cursor.template_kind() {
+ CXCursor_UnionDecl => CompKind::Union,
+ _ => CompKind::Struct,
+ }
+ }
+ _ => {
+ warn!("Unknown kind for comp type: {:?}", cursor);
+ return Err(ParseError::Continue);
+ }
+ })
+ }
+
+ /// Do any of the types that participate in this type's "signature" use the
+ /// named type `ty`?
+ ///
+ /// See also documentation for `ir::Item::signature_contains_named_type`.
+ pub fn signature_contains_named_type(&self,
+ ctx: &BindgenContext,
+ ty: &Type)
+ -> bool {
+ // We don't generate these, so rather don't make the codegen step to
+ // think we got it covered.
+ if self.has_non_type_template_params() {
+ return false;
+ }
+ self.template_args.iter().any(|arg| {
+ ctx.resolve_type(*arg)
+ .signature_contains_named_type(ctx, ty)
+ })
+ }
+
+ /// Get the set of types that were declared within this compound type
+ /// (e.g. nested class definitions).
+ pub fn inner_types(&self) -> &[ItemId] {
+ &self.inner_types
+ }
+
+ /// Get the set of static variables declared within this compound type.
+ pub fn inner_vars(&self) -> &[ItemId] {
+ &self.inner_vars
+ }
+
+ /// Have we found a field with an opaque type that could potentially mess up
+ /// the layout of this compound type?
+ pub fn found_unknown_attr(&self) -> bool {
+ self.found_unknown_attr
+ }
+
+ /// Is this compound type packed?
+ pub fn packed(&self) -> bool {
+ self.packed
+ }
+
+ /// Returns whether this type needs an explicit vtable because it has
+ /// virtual methods and none of its base classes has already a vtable.
+ pub fn needs_explicit_vtable(&self, ctx: &BindgenContext) -> bool {
+ self.has_vtable(ctx) &&
+ !self.base_members.iter().any(|base| {
+ // NB: Ideally, we could rely in all these types being `comp`, and
+ // life would be beautiful.
+ //
+ // Unfortunately, given the way we implement --match-pat, and also
+ // that you can inherit from templated types, we need to handle
+ // other cases here too.
+ ctx.resolve_type(*base)
+ .canonical_type(ctx)
+ .as_comp()
+ .map_or(false, |ci| ci.has_vtable(ctx))
+ })
+ }
+}
+
+impl TypeCollector for CompInfo {
+ type Extra = Item;
+
+ fn collect_types(&self,
+ context: &BindgenContext,
+ types: &mut ItemSet,
+ item: &Item) {
+ if let Some(template) = self.specialized_template() {
+ types.insert(template);
+ }
+
+ let applicable_template_args = item.applicable_template_args(context);
+ for arg in applicable_template_args {
+ types.insert(arg);
+ }
+
+ for &base in self.base_members() {
+ types.insert(base);
+ }
+
+ for field in self.fields() {
+ types.insert(field.ty());
+ }
+
+ for &ty in self.inner_types() {
+ types.insert(ty);
+ }
+
+ // FIXME(emilio): Methods, VTable?
+ }
+}
diff --git a/libbindgen/src/ir/context.rs b/libbindgen/src/ir/context.rs
new file mode 100644
index 00000000..85721978
--- /dev/null
+++ b/libbindgen/src/ir/context.rs
@@ -0,0 +1,1055 @@
+//! Common context that is passed around during parsing and codegen.
+
+use BindgenOptions;
+use cexpr;
+use clang::{self, Cursor};
+use parse::ClangItemParser;
+use std::borrow::Cow;
+use std::cell::Cell;
+use std::collections::{HashMap, hash_map};
+use std::collections::btree_map::{self, BTreeMap};
+use std::fmt;
+use super::int::IntKind;
+use super::item::{Item, ItemCanonicalName};
+use super::item_kind::ItemKind;
+use super::module::Module;
+use super::ty::{FloatKind, Type, TypeKind};
+use super::type_collector::{ItemSet, TypeCollector};
+use syntax::ast::Ident;
+use syntax::codemap::{DUMMY_SP, Span};
+use syntax::ext::base::ExtCtxt;
+
+/// A single identifier for an item.
+///
+/// TODO: Build stronger abstractions on top of this, like TypeId(ItemId)?
+#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct ItemId(usize);
+
+impl ItemId {
+ /// Get a numeric representation of this id.
+ pub fn as_usize(&self) -> usize {
+ self.0
+ }
+}
+
+/// A key used to index a resolved type, so we only process it once.
+///
+/// This is almost always a USR string (an unique identifier generated by
+/// clang), but it can also be the canonical declaration if the type is unnamed,
+/// in which case clang may generate the same USR for multiple nested unnamed
+/// types.
+#[derive(Eq, PartialEq, Hash, Debug)]
+enum TypeKey {
+ USR(String),
+ Declaration(Cursor),
+}
+
+// This is just convenience to avoid creating a manual debug impl for the
+// context.
+struct GenContext<'ctx>(ExtCtxt<'ctx>);
+
+impl<'ctx> fmt::Debug for GenContext<'ctx> {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ write!(fmt, "GenContext {{ ... }}")
+ }
+}
+
+/// A context used during parsing and generation of structs.
+#[derive(Debug)]
+pub struct BindgenContext<'ctx> {
+ /// The map of all the items parsed so far.
+ ///
+ /// It's a BTreeMap because we want the keys to be sorted to have consistent
+ /// output.
+ items: BTreeMap<ItemId, Item>,
+
+ /// The next item id to use during this bindings regeneration.
+ next_item_id: ItemId,
+
+ /// Clang USR to type map. This is needed to be able to associate types with
+ /// item ids during parsing.
+ types: HashMap<TypeKey, ItemId>,
+
+ /// A cursor to module map. Similar reason than above.
+ modules: HashMap<Cursor, ItemId>,
+
+ /// The root module, this is guaranteed to be an item of kind Module.
+ root_module: ItemId,
+
+ /// Current module being traversed.
+ current_module: ItemId,
+
+ /// A stack with the current type declarations and types we're parsing. This
+ /// is needed to avoid infinite recursion when parsing a type like:
+ ///
+ /// struct c { struct c* next; };
+ ///
+ /// This means effectively, that a type has a potential ID before knowing if
+ /// it's a correct type. But that's not important in practice.
+ ///
+ /// We could also use the `types` HashMap, but my intention with it is that
+ /// only valid types and declarations end up there, and this could
+ /// potentially break that assumption.
+ ///
+ /// FIXME: Should not be public, though... meh.
+ pub currently_parsed_types: Vec<(Cursor, ItemId)>,
+
+ /// A HashSet with all the already parsed macro names. This is done to avoid
+ /// hard errors while parsing duplicated macros, as well to allow macro
+ /// expression parsing.
+ parsed_macros: HashMap<Vec<u8>, cexpr::expr::EvalResult>,
+
+ /// The active replacements collected from replaces="xxx" annotations.
+ replacements: HashMap<String, ItemId>,
+
+ collected_typerefs: bool,
+
+ /// Dummy structures for code generation.
+ gen_ctx: Option<&'ctx GenContext<'ctx>>,
+ span: Span,
+
+ /// The clang index for parsing.
+ index: clang::Index,
+
+ /// The translation unit for parsing.
+ translation_unit: clang::TranslationUnit,
+
+ /// The options given by the user via cli or other medium.
+ options: BindgenOptions,
+
+ /// Whether a bindgen complex was generated
+ generated_bindegen_complex: Cell<bool>,
+}
+
+impl<'ctx> BindgenContext<'ctx> {
+ /// Construct the context for the given `options`.
+ pub fn new(options: BindgenOptions) -> Self {
+ use clangll;
+
+ let index = clang::Index::new(false, true);
+
+ let parse_options =
+ clangll::CXTranslationUnit_DetailedPreprocessingRecord;
+ let translation_unit =
+ clang::TranslationUnit::parse(&index,
+ "",
+ &options.clang_args,
+ &[],
+ parse_options)
+ .expect("TranslationUnit::parse");
+
+ let root_module = Self::build_root_module(ItemId(0));
+ let mut me = BindgenContext {
+ items: Default::default(),
+ types: Default::default(),
+ modules: Default::default(),
+ next_item_id: ItemId(1),
+ root_module: root_module.id(),
+ current_module: root_module.id(),
+ currently_parsed_types: vec![],
+ parsed_macros: Default::default(),
+ replacements: Default::default(),
+ collected_typerefs: false,
+ gen_ctx: None,
+ span: DUMMY_SP,
+ index: index,
+ translation_unit: translation_unit,
+ options: options,
+ generated_bindegen_complex: Cell::new(false),
+ };
+
+ me.add_item(root_module, None, None);
+
+ me
+ }
+
+ /// Define a new item.
+ ///
+ /// This inserts it into the internal items set, and its type into the
+ /// internal types set.
+ pub fn add_item(&mut self,
+ item: Item,
+ declaration: Option<Cursor>,
+ location: Option<Cursor>) {
+ use clangll::{CXCursor_ClassTemplate,
+ CXCursor_ClassTemplatePartialSpecialization};
+ debug!("BindgenContext::add_item({:?}, declaration: {:?}, loc: {:?}",
+ item,
+ declaration,
+ location);
+ debug_assert!(declaration.is_some() || !item.kind().is_type() ||
+ item.kind().expect_type().is_builtin_or_named(),
+ "Adding a type without declaration?");
+
+ let id = item.id();
+ let is_type = item.kind().is_type();
+ let is_unnamed = is_type && item.expect_type().name().is_none();
+ let old_item = self.items.insert(id, item);
+ assert!(old_item.is_none(), "Inserted type twice?");
+
+ // Unnamed items can have an USR, but they can't be referenced from
+ // other sites explicitly and the USR can match if the unnamed items are
+ // nested, so don't bother tracking them.
+ if is_type && declaration.is_some() {
+ let mut declaration = declaration.unwrap();
+ if !declaration.is_valid() {
+ if let Some(location) = location {
+ if location.kind() == CXCursor_ClassTemplate ||
+ location.kind() ==
+ CXCursor_ClassTemplatePartialSpecialization {
+ declaration = location;
+ }
+ }
+ }
+ declaration = declaration.canonical();
+ if !declaration.is_valid() {
+ // This could happen, for example, with types like `int*` or
+ // similar.
+ //
+ // Fortunately, we don't care about those types being
+ // duplicated, so we can just ignore them.
+ debug!("Invalid declaration {:?} found for type {:?}",
+ declaration,
+ self.items.get(&id).unwrap().kind().expect_type());
+ return;
+ }
+
+ let key = if is_unnamed {
+ TypeKey::Declaration(declaration)
+ } else if let Some(usr) = declaration.usr() {
+ TypeKey::USR(usr)
+ } else {
+ error!("Valid declaration with no USR: {:?}, {:?}",
+ declaration,
+ location);
+ return;
+ };
+
+ let old = self.types.insert(key, id);
+ debug_assert_eq!(old, None);
+ }
+ }
+
+ // TODO: Move all this syntax crap to other part of the code.
+
+ /// Given that we are in the codegen phase, get the syntex context.
+ pub fn ext_cx(&self) -> &ExtCtxt<'ctx> {
+ &self.gen_ctx.expect("Not in gen phase").0
+ }
+
+ /// Given that we are in the codegen phase, get the current syntex span.
+ pub fn span(&self) -> Span {
+ self.span
+ }
+
+ /// Mangles a name so it doesn't conflict with any keyword.
+ pub fn rust_mangle<'a>(&self, name: &'a str) -> Cow<'a, str> {
+ use syntax::parse::token;
+ let ident = self.rust_ident_raw(name);
+ let token = token::Ident(ident);
+ if token.is_any_keyword() || name.contains("@") ||
+ name.contains("?") || name.contains("$") ||
+ "bool" == name {
+ let mut s = name.to_owned();
+ s = s.replace("@", "_");
+ s = s.replace("?", "_");
+ s = s.replace("$", "_");
+ s.push_str("_");
+ return Cow::Owned(s);
+ }
+ Cow::Borrowed(name)
+ }
+
+ /// Returns a mangled name as a rust identifier.
+ pub fn rust_ident(&self, name: &str) -> Ident {
+ self.rust_ident_raw(&self.rust_mangle(name))
+ }
+
+ /// Returns a mangled name as a rust identifier.
+ pub fn rust_ident_raw(&self, name: &str) -> Ident {
+ self.ext_cx().ident_of(name)
+ }
+
+ /// Iterate over all items that have been defined.
+ pub fn items<'a>(&'a self) -> btree_map::Iter<'a, ItemId, Item> {
+ self.items.iter()
+ }
+
+ /// Have we collected all unresolved type references yet?
+ pub fn collected_typerefs(&self) -> bool {
+ self.collected_typerefs
+ }
+
+ /// Gather all the unresolved type references.
+ fn collect_typerefs
+ (&mut self)
+ -> Vec<(ItemId, clang::Type, Option<clang::Cursor>, Option<ItemId>)> {
+ debug_assert!(!self.collected_typerefs);
+ self.collected_typerefs = true;
+ let mut typerefs = vec![];
+ for (id, ref mut item) in &mut self.items {
+ let kind = item.kind();
+ let ty = match kind.as_type() {
+ Some(ty) => ty,
+ None => continue,
+ };
+
+ match *ty.kind() {
+ TypeKind::UnresolvedTypeRef(ref ty, loc, parent_id) => {
+ typerefs.push((*id, ty.clone(), loc, parent_id));
+ }
+ _ => {}
+ };
+ }
+ typerefs
+ }
+
+ /// Collect all of our unresolved type references and resolve them.
+ fn resolve_typerefs(&mut self) {
+ let typerefs = self.collect_typerefs();
+
+ for (id, ty, loc, parent_id) in typerefs {
+ let _resolved = {
+ let resolved = Item::from_ty(&ty, loc, parent_id, self)
+ .expect("What happened?");
+ let mut item = self.items.get_mut(&id).unwrap();
+
+ *item.kind_mut().as_type_mut().unwrap().kind_mut() =
+ TypeKind::ResolvedTypeRef(resolved);
+ resolved
+ };
+
+ // Something in the STL is trolling me. I don't need this assertion
+ // right now, but worth investigating properly once this lands.
+ //
+ // debug_assert!(self.items.get(&resolved).is_some(), "How?");
+ }
+ }
+
+ /// Iterate over all items and replace any item that has been named in a
+ /// `replaces="SomeType"` annotation with the replacement type.
+ fn process_replacements(&mut self) {
+ if self.replacements.is_empty() {
+ debug!("No replacements to process");
+ return;
+ }
+
+ // FIXME: This is linear, but the replaces="xxx" annotation was already
+ // there, and for better or worse it's useful, sigh...
+ //
+ // We leverage the ResolvedTypeRef thing, though, which is cool :P.
+
+ let mut replacements = vec![];
+
+ for (id, item) in self.items.iter() {
+ // Calls to `canonical_name` are expensive, so eagerly filter out
+ // items that cannot be replaced.
+ let ty = match item.kind().as_type() {
+ Some(ty) => ty,
+ None => continue,
+ };
+
+ match *ty.kind() {
+ TypeKind::Comp(ref ci) if !ci.is_template_specialization() => {}
+ TypeKind::TemplateAlias(_, _) |
+ TypeKind::Alias(_, _) => {}
+ _ => continue,
+ }
+
+ let name = item.real_canonical_name(self,
+ self.options()
+ .enable_cxx_namespaces,
+ true);
+ let replacement = self.replacements.get(&name);
+
+ if let Some(replacement) = replacement {
+ if replacement != id {
+ // We set this just after parsing the annotation. It's
+ // very unlikely, but this can happen.
+ if self.items.get(replacement).is_some() {
+ replacements.push((*id, *replacement));
+ }
+ }
+ }
+ }
+
+ for (id, replacement) in replacements {
+ debug!("Replacing {:?} with {:?}", id, replacement);
+
+ let mut item = self.items.get_mut(&id).unwrap();
+ *item.kind_mut().as_type_mut().unwrap().kind_mut() =
+ TypeKind::ResolvedTypeRef(replacement);
+ }
+ }
+
+ /// Enter the code generation phase, invoke the given callback `cb`, and
+ /// leave the code generation phase.
+ pub fn gen<F, Out>(&mut self, cb: F) -> Out
+ where F: FnOnce(&Self) -> Out,
+ {
+ use syntax::ext::expand::ExpansionConfig;
+ use syntax::codemap::{ExpnInfo, MacroBang, NameAndSpan};
+ use syntax::ext::base;
+ use syntax::parse;
+ use std::mem;
+
+ let cfg = ExpansionConfig::default("xxx".to_owned());
+ let sess = parse::ParseSess::new();
+ let mut loader = base::DummyResolver;
+ let mut ctx =
+ GenContext(base::ExtCtxt::new(&sess, vec![], cfg, &mut loader));
+
+ ctx.0.bt_push(ExpnInfo {
+ call_site: self.span,
+ callee: NameAndSpan {
+ format: MacroBang(parse::token::intern("")),
+ allow_internal_unstable: false,
+ span: None,
+ },
+ });
+
+ // FIXME: This is evil, we should move code generation to use a wrapper
+ // of BindgenContext instead, I guess. Even though we know it's fine
+ // because we remove it before the end of this function.
+ self.gen_ctx = Some(unsafe { mem::transmute(&ctx) });
+
+ if !self.collected_typerefs() {
+ self.resolve_typerefs();
+ self.process_replacements();
+ }
+
+ let ret = cb(self);
+ self.gen_ctx = None;
+ ret
+ }
+
+ // This deserves a comment. Builtin types don't get a valid declaration, so
+ // we can't add it to the cursor->type map.
+ //
+ // That being said, they're not generated anyway, and are few, so the
+ // duplication and special-casing is fine.
+ //
+ // If at some point we care about the memory here, probably a map TypeKind
+ // -> builtin type ItemId would be the best to improve that.
+ fn add_builtin_item(&mut self, item: Item) {
+ debug!("add_builtin_item: item = {:?}", item);
+ debug_assert!(item.kind().is_type());
+ let id = item.id();
+ let old_item = self.items.insert(id, item);
+ assert!(old_item.is_none(), "Inserted type twice?");
+ }
+
+ fn build_root_module(id: ItemId) -> Item {
+ let module = Module::new(Some("root".into()));
+ Item::new(id, None, None, id, ItemKind::Module(module))
+ }
+
+ /// Get the root module.
+ pub fn root_module(&self) -> ItemId {
+ self.root_module
+ }
+
+ /// Resolve the given `ItemId` as a type.
+ ///
+ /// Panics if there is no item for the given `ItemId` or if the resolved
+ /// item is not a `Type`.
+ pub fn resolve_type(&self, type_id: ItemId) -> &Type {
+ self.items.get(&type_id).unwrap().kind().expect_type()
+ }
+
+ /// Resolve the given `ItemId` as a type, or `None` if there is no item with
+ /// the given id.
+ ///
+ /// Panics if the id resolves to an item that is not a type.
+ pub fn safe_resolve_type(&self, type_id: ItemId) -> Option<&Type> {
+ self.items.get(&type_id).map(|t| t.kind().expect_type())
+ }
+
+ /// Resolve the given `ItemId` into an `Item`, or `None` if no such item
+ /// exists.
+ pub fn resolve_item_fallible(&self, item_id: ItemId) -> Option<&Item> {
+ self.items.get(&item_id)
+ }
+
+ /// Resolve the given `ItemId` into an `Item`.
+ ///
+ /// Panics if the given id does not resolve to any item.
+ pub fn resolve_item(&self, item_id: ItemId) -> &Item {
+ match self.items.get(&item_id) {
+ Some(item) => item,
+ None => panic!("Not an item: {:?}", item_id),
+ }
+ }
+
+ /// Get the current module.
+ pub fn current_module(&self) -> ItemId {
+ self.current_module
+ }
+
+ /// This is one of the hackiest methods in all the parsing code. This method
+ /// is used to allow having templates with another argument names instead of
+ /// the canonical ones.
+ ///
+ /// This is surprisingly difficult to do with libclang, due to the fact that
+ /// partial template specializations don't provide explicit template
+ /// argument information.
+ ///
+ /// The only way to do this as far as I know, is inspecting manually the
+ /// AST, looking for TypeRefs inside. This, unfortunately, doesn't work for
+ /// more complex cases, see the comment on the assertion below.
+ ///
+ /// To see an example of what this handles:
+ ///
+ /// ```c++
+ /// template<typename T>
+ /// class Incomplete {
+ /// T p;
+ /// };
+ ///
+ /// template<typename U>
+ /// class Foo {
+ /// Incomplete<U> bar;
+ /// };
+ /// ```
+ fn build_template_wrapper(&mut self,
+ with_id: ItemId,
+ wrapping: ItemId,
+ parent_id: ItemId,
+ ty: &clang::Type,
+ location: clang::Cursor,
+ declaration: clang::Cursor)
+ -> ItemId {
+ use clangll::*;
+ let mut args = vec![];
+ location.visit(|c| {
+ if c.kind() == CXCursor_TypeRef {
+ // The `with_id` id will potentially end up unused if we give up
+ // on this type (for example, its a tricky partial template
+ // specialization), so if we pass `with_id` as the parent, it is
+ // potentially a dangling reference. Instead, use the canonical
+ // template declaration as the parent. It is already parsed and
+ // has a known-resolvable `ItemId`.
+ let new_ty = Item::from_ty_or_ref(c.cur_type(),
+ Some(c),
+ Some(wrapping),
+ self);
+ args.push(new_ty);
+ }
+ CXChildVisit_Continue
+ });
+
+ let item = {
+ let wrapping_type = self.resolve_type(wrapping);
+ if let TypeKind::Comp(ref ci) = *wrapping_type.kind() {
+ let old_args = ci.template_args();
+
+ // The following assertion actually fails with partial template
+ // specialization. But as far as I know there's no way at all to
+ // grab the specialized types from neither the AST or libclang,
+ // which sucks. The same happens for specialized type alias
+ // template declarations, where we have that ugly hack up there.
+ //
+ // This flaw was already on the old parser, but I now think it
+ // has no clear solution (apart from patching libclang to
+ // somehow expose them, of course).
+ //
+ // For an easy example in which there's no way at all of getting
+ // the `int` type, except manually parsing the spelling:
+ //
+ // template<typename T, typename U>
+ // class Incomplete {
+ // T d;
+ // U p;
+ // };
+ //
+ // template<typename U>
+ // class Foo {
+ // Incomplete<U, int> bar;
+ // };
+ //
+ // debug_assert_eq!(old_args.len(), args.len());
+ //
+ // That being said, this is not so common, so just error! and
+ // hope for the best, returning the previous type, who knows.
+ if old_args.len() != args.len() {
+ error!("Found partial template specialization, \
+ expect dragons!");
+ return wrapping;
+ }
+ } else {
+ assert_eq!(declaration.kind(),
+ ::clangll::CXCursor_TypeAliasTemplateDecl,
+ "Expected wrappable type");
+ }
+
+ let type_kind = TypeKind::TemplateRef(wrapping, args);
+ let name = ty.spelling();
+ let name = if name.is_empty() { None } else { Some(name) };
+ let ty = Type::new(name,
+ ty.fallible_layout().ok(),
+ type_kind,
+ ty.is_const());
+ Item::new(with_id, None, None, parent_id, ItemKind::Type(ty))
+ };
+
+ // Bypass all the validations in add_item explicitly.
+ debug!("build_template_wrapper: inserting item: {:?}", item);
+ debug_assert!(with_id == item.id());
+ self.items.insert(with_id, item);
+ with_id
+ }
+
+ /// Looks up for an already resolved type, either because it's builtin, or
+ /// because we already have it in the map.
+ pub fn builtin_or_resolved_ty(&mut self,
+ with_id: ItemId,
+ parent_id: Option<ItemId>,
+ ty: &clang::Type,
+ location: Option<clang::Cursor>)
+ -> Option<ItemId> {
+ use clangll::{CXCursor_ClassTemplate,
+ CXCursor_ClassTemplatePartialSpecialization,
+ CXCursor_TypeAliasTemplateDecl,
+ CXCursor_TypeRef};
+ debug!("builtin_or_resolved_ty: {:?}, {:?}, {:?}",
+ ty,
+ location,
+ parent_id);
+ let mut declaration = ty.declaration();
+ if !declaration.is_valid() {
+ if let Some(location) = location {
+ if location.kind() == CXCursor_ClassTemplate ||
+ location.kind() ==
+ CXCursor_ClassTemplatePartialSpecialization {
+ declaration = location;
+ }
+ }
+ }
+ let canonical_declaration = declaration.canonical();
+ if canonical_declaration.is_valid() {
+ let id = self.types
+ .get(&TypeKey::Declaration(canonical_declaration))
+ .map(|id| *id)
+ .or_else(|| {
+ canonical_declaration.usr()
+ .and_then(|usr| self.types.get(&TypeKey::USR(usr)))
+ .map(|id| *id)
+ });
+ if let Some(id) = id {
+ debug!("Already resolved ty {:?}, {:?}, {:?} {:?}",
+ id,
+ declaration,
+ ty,
+ location);
+
+ // If the declaration existed, we *might* be done, but it's not
+ // the case for class templates, where the template arguments
+ // may vary.
+ //
+ // In this case, we create a TemplateRef with the new template
+ // arguments, pointing to the canonical template.
+ //
+ // Note that we only do it if parent_id is some, and we have a
+ // location for building the new arguments, the template
+ // argument names don't matter in the global context.
+ if (declaration.kind() == CXCursor_ClassTemplate ||
+ declaration.kind() ==
+ CXCursor_ClassTemplatePartialSpecialization ||
+ declaration.kind() == CXCursor_TypeAliasTemplateDecl) &&
+ *ty != canonical_declaration.cur_type() &&
+ location.is_some() &&
+ parent_id.is_some() {
+ // For specialized type aliases, there's no way to get the
+ // template parameters as of this writing (for a struct
+ // specialization we wouldn't be in this branch anyway).
+ //
+ // Explicitly return `None` if there aren't any
+ // unspecialized parameters (contains any `TypeRef`) so we
+ // resolve the canonical type if there is one and it's
+ // exposed.
+ //
+ // This is _tricky_, I know :(
+ if declaration.kind() == CXCursor_TypeAliasTemplateDecl &&
+ !location.unwrap().contains_cursor(CXCursor_TypeRef) &&
+ ty.canonical_type().is_valid_and_exposed() {
+ return None;
+ }
+
+ return Some(self.build_template_wrapper(with_id,
+ id,
+ parent_id.unwrap(),
+ ty,
+ location.unwrap(),
+ declaration));
+ }
+
+ return Some(self.build_ty_wrapper(with_id, id, parent_id, ty));
+ }
+ }
+
+ debug!("Not resolved, maybe builtin?");
+
+ // Else, build it.
+ self.build_builtin_ty(ty, declaration)
+ }
+
+ // This is unfortunately a lot of bloat, but is needed to properly track
+ // constness et. al.
+ //
+ // We should probably make the constness tracking separate, so it doesn't
+ // bloat that much, but hey, we already bloat the heck out of builtin types.
+ fn build_ty_wrapper(&mut self,
+ with_id: ItemId,
+ wrapped_id: ItemId,
+ parent_id: Option<ItemId>,
+ ty: &clang::Type)
+ -> ItemId {
+ let spelling = ty.spelling();
+ let is_const = ty.is_const();
+ let layout = ty.fallible_layout().ok();
+ let type_kind = TypeKind::ResolvedTypeRef(wrapped_id);
+ let ty = Type::new(Some(spelling), layout, type_kind, is_const);
+ let item = Item::new(with_id,
+ None,
+ None,
+ parent_id.unwrap_or(self.current_module),
+ ItemKind::Type(ty));
+ self.add_builtin_item(item);
+ with_id
+ }
+
+ /// Returns the next item id to be used for an item.
+ pub fn next_item_id(&mut self) -> ItemId {
+ let ret = self.next_item_id;
+ self.next_item_id = ItemId(self.next_item_id.0 + 1);
+ ret
+ }
+
+ fn build_builtin_ty(&mut self,
+ ty: &clang::Type,
+ _declaration: Cursor)
+ -> Option<ItemId> {
+ use clangll::*;
+ let type_kind = match ty.kind() {
+ CXType_NullPtr => TypeKind::NullPtr,
+ CXType_Void => TypeKind::Void,
+ CXType_Bool => TypeKind::Int(IntKind::Bool),
+ CXType_Int => TypeKind::Int(IntKind::Int),
+ CXType_UInt => TypeKind::Int(IntKind::UInt),
+ CXType_SChar | CXType_Char_S => TypeKind::Int(IntKind::Char),
+ CXType_UChar | CXType_Char_U => TypeKind::Int(IntKind::UChar),
+ CXType_Short => TypeKind::Int(IntKind::Short),
+ CXType_UShort => TypeKind::Int(IntKind::UShort),
+ CXType_WChar | CXType_Char16 => TypeKind::Int(IntKind::U16),
+ CXType_Char32 => TypeKind::Int(IntKind::U32),
+ CXType_Long => TypeKind::Int(IntKind::Long),
+ CXType_ULong => TypeKind::Int(IntKind::ULong),
+ CXType_LongLong => TypeKind::Int(IntKind::LongLong),
+ CXType_ULongLong => TypeKind::Int(IntKind::ULongLong),
+ CXType_Int128 => TypeKind::Int(IntKind::I128),
+ CXType_UInt128 => TypeKind::Int(IntKind::U128),
+ CXType_Float => TypeKind::Float(FloatKind::Float),
+ CXType_Double => TypeKind::Float(FloatKind::Double),
+ CXType_LongDouble => TypeKind::Float(FloatKind::LongDouble),
+ CXType_Float128 => TypeKind::Float(FloatKind::Float128),
+ CXType_Complex => {
+ let float_type = ty.elem_type()
+ .expect("Not able to resolve complex type?");
+ let float_kind = match float_type.kind() {
+ CXType_Float => FloatKind::Float,
+ CXType_Double => FloatKind::Double,
+ CXType_LongDouble => FloatKind::LongDouble,
+ _ => panic!("Non floating-type complex?"),
+ };
+ TypeKind::Complex(float_kind)
+ }
+ _ => return None,
+ };
+
+ let spelling = ty.spelling();
+ let is_const = ty.is_const();
+ let layout = ty.fallible_layout().ok();
+ let ty = Type::new(Some(spelling), layout, type_kind, is_const);
+ let id = self.next_item_id();
+ let item =
+ Item::new(id, None, None, self.root_module, ItemKind::Type(ty));
+ self.add_builtin_item(item);
+ Some(id)
+ }
+
+ /// Get the current Clang translation unit that is being processed.
+ pub fn translation_unit(&self) -> &clang::TranslationUnit {
+ &self.translation_unit
+ }
+
+ /// Have we parsed the macro named `macro_name` already?
+ pub fn parsed_macro(&self, macro_name: &[u8]) -> bool {
+ self.parsed_macros.contains_key(macro_name)
+ }
+
+ /// Get the currently parsed macros.
+ pub fn parsed_macros(&self) -> &HashMap<Vec<u8>, cexpr::expr::EvalResult> {
+ debug_assert!(!self.in_codegen_phase());
+ &self.parsed_macros
+ }
+
+ /// Mark the macro named `macro_name` as parsed.
+ pub fn note_parsed_macro(&mut self,
+ id: Vec<u8>,
+ value: cexpr::expr::EvalResult) {
+ self.parsed_macros.insert(id, value);
+ }
+
+ /// Are we in the codegen phase?
+ pub fn in_codegen_phase(&self) -> bool {
+ self.gen_ctx.is_some()
+ }
+
+ /// Mark the type with the given `name` as replaced by the type with id
+ /// `potential_ty`.
+ ///
+ /// Replacement types are declared using the `replaces="xxx"` annotation,
+ /// and implies that the original type is hidden.
+ pub fn replace(&mut self, name: &str, potential_ty: ItemId) {
+ match self.replacements.entry(name.into()) {
+ hash_map::Entry::Vacant(entry) => {
+ debug!("Defining replacement for {} as {:?}",
+ name,
+ potential_ty);
+ entry.insert(potential_ty);
+ }
+ hash_map::Entry::Occupied(occupied) => {
+ warn!("Replacement for {} already defined as {:?}; \
+ ignoring duplicate replacement definition as {:?}}}",
+ name,
+ occupied.get(),
+ potential_ty);
+ }
+ }
+ }
+
+ /// Is the item with the given `name` hidden? Or is the item with the given
+ /// `name` and `id` replaced by another type, and effectively hidden?
+ pub fn hidden_by_name(&self, name: &str, id: ItemId) -> bool {
+ debug_assert!(self.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ self.options.hidden_types.contains(name) ||
+ self.is_replaced_type(name, id)
+ }
+
+ /// Has the item with the given `name` and `id` been replaced by another
+ /// type?
+ pub fn is_replaced_type(&self, name: &str, id: ItemId) -> bool {
+ match self.replacements.get(name) {
+ Some(replaced_by) if *replaced_by != id => true,
+ _ => false,
+ }
+ }
+
+ /// Is the type with the given `name` marked as opaque?
+ pub fn opaque_by_name(&self, name: &str) -> bool {
+ debug_assert!(self.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ self.options.opaque_types.contains(name)
+ }
+
+ /// Get the options used to configure this bindgen context.
+ pub fn options(&self) -> &BindgenOptions {
+ &self.options
+ }
+
+ /// Given a CXCursor_Namespace cursor, return the item id of the
+ /// corresponding module, or create one on the fly.
+ pub fn module(&mut self, cursor: clang::Cursor) -> ItemId {
+ use clangll::*;
+ assert!(cursor.kind() == CXCursor_Namespace, "Be a nice person");
+ let cursor = cursor.canonical();
+ if let Some(id) = self.modules.get(&cursor) {
+ return *id;
+ }
+
+ let module_id = self.next_item_id();
+ let module_name = self.translation_unit
+ .tokens(&cursor)
+ .and_then(|tokens| {
+ if tokens.len() <= 1 {
+ None
+ } else {
+ match &*tokens[1].spelling {
+ "{" => None,
+ s => Some(s.to_owned()),
+ }
+ }
+ });
+
+ let module = Module::new(module_name);
+ let module = Item::new(module_id,
+ None,
+ None,
+ self.current_module,
+ ItemKind::Module(module));
+
+ self.add_item(module, None, None);
+
+ module_id
+ }
+
+ /// Start traversing the module with the given `module_id`, invoke the
+ /// callback `cb`, and then return to traversing the original module.
+ pub fn with_module<F>(&mut self, module_id: ItemId, cb: F)
+ where F: FnOnce(&mut Self, &mut Vec<ItemId>),
+ {
+ debug_assert!(self.resolve_item(module_id).kind().is_module(), "Wat");
+
+ let previous_id = self.current_module;
+ self.current_module = module_id;
+
+ let mut children = vec![];
+ cb(self, &mut children);
+
+ self.items
+ .get_mut(&module_id)
+ .unwrap()
+ .as_module_mut()
+ .expect("Not a module?")
+ .children_mut()
+ .extend(children.into_iter());
+
+ self.current_module = previous_id;
+ }
+
+ /// Iterate over all (explicitly or transitively) whitelisted items.
+ ///
+ /// If no items are explicitly whitelisted, then all items are considered
+ /// whitelisted.
+ pub fn whitelisted_items<'me>(&'me self)
+ -> WhitelistedItemsIter<'me, 'ctx> {
+ assert!(self.in_codegen_phase());
+ assert!(self.current_module == self.root_module);
+
+ let roots = self.items()
+ .filter(|&(_, item)| {
+ // If nothing is explicitly whitelisted, then everything is fair
+ // game.
+ if self.options().whitelisted_types.is_empty() &&
+ self.options().whitelisted_functions.is_empty() &&
+ self.options().whitelisted_vars.is_empty() {
+ return true;
+ }
+
+ let name = item.canonical_name(self);
+ match *item.kind() {
+ ItemKind::Module(..) => false,
+ ItemKind::Function(_) => {
+ self.options().whitelisted_functions.matches(&name)
+ }
+ ItemKind::Var(_) => {
+ self.options().whitelisted_vars.matches(&name)
+ }
+ ItemKind::Type(ref ty) => {
+ if self.options().whitelisted_types.matches(&name) {
+ return true;
+ }
+
+ // Unnamed top-level enums are special and we whitelist
+ // them via the `whitelisted_vars` filter, since they're
+ // effectively top-level constants, and there's no way
+ // for them to be referenced consistently.
+ if let TypeKind::Enum(ref enum_) = *ty.kind() {
+ if ty.name().is_none() &&
+ enum_.variants().iter().any(|variant| {
+ self.options()
+ .whitelisted_vars
+ .matches(&variant.name())
+ }) {
+ return true;
+ }
+ }
+
+ false
+ }
+ }
+ })
+ .map(|(&id, _)| id);
+
+ let seen: ItemSet = roots.collect();
+
+ // The .rev() preserves the expected ordering traversal, resulting in
+ // more stable-ish bindgen-generated names for anonymous types (like
+ // unions).
+ let to_iterate = seen.iter().cloned().rev().collect();
+
+ WhitelistedItemsIter {
+ ctx: self,
+ seen: seen,
+ to_iterate: to_iterate,
+ }
+ }
+
+ /// Convenient method for getting the prefix to use for most traits in
+ /// codegen depending on the `use_core` option.
+ pub fn trait_prefix(&self) -> Ident {
+ if self.options().use_core {
+ self.rust_ident_raw("core")
+ } else {
+ self.rust_ident_raw("std")
+ }
+ }
+
+ /// Call if a binden complex is generated
+ pub fn generated_bindegen_complex(&self) {
+ self.generated_bindegen_complex.set(true)
+ }
+
+ /// Whether we need to generate the binden complex type
+ pub fn need_bindegen_complex_type(&self) -> bool {
+ self.generated_bindegen_complex.get()
+ }
+}
+
+/// An iterator over whitelisted items.
+///
+/// See `BindgenContext::whitelisted_items` for more information.
+pub struct WhitelistedItemsIter<'ctx, 'gen>
+ where 'gen: 'ctx,
+{
+ ctx: &'ctx BindgenContext<'gen>,
+
+ // The set of whitelisted items we have seen. If you think of traversing
+ // whitelisted items like GC tracing, this is the mark bits, and contains
+ // both black and gray items.
+ seen: ItemSet,
+
+ // The set of whitelisted items that we have seen but have yet to iterate
+ // over and collect transitive references from. To return to the GC analogy,
+ // this is the mark stack, containing the set of gray items which we have
+ // not finished tracing yet.
+ to_iterate: Vec<ItemId>,
+}
+
+impl<'ctx, 'gen> Iterator for WhitelistedItemsIter<'ctx, 'gen>
+ where 'gen: 'ctx,
+{
+ type Item = ItemId;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let id = match self.to_iterate.pop() {
+ None => return None,
+ Some(id) => id,
+ };
+
+ debug_assert!(self.seen.contains(&id));
+ debug_assert!(self.ctx.items.contains_key(&id));
+
+ let mut sub_types = ItemSet::new();
+ id.collect_types(self.ctx, &mut sub_types, &());
+
+ for id in sub_types {
+ if self.seen.insert(id) {
+ self.to_iterate.push(id);
+ }
+ }
+
+ Some(id)
+ }
+}
diff --git a/libbindgen/src/ir/enum_ty.rs b/libbindgen/src/ir/enum_ty.rs
new file mode 100644
index 00000000..6085833d
--- /dev/null
+++ b/libbindgen/src/ir/enum_ty.rs
@@ -0,0 +1,137 @@
+//! Intermediate representation for C/C++ enumerations.
+
+use clang;
+use parse::{ClangItemParser, ParseError};
+use super::context::{BindgenContext, ItemId};
+use super::item::Item;
+use super::ty::TypeKind;
+
+/// A C/C++ enumeration.
+#[derive(Debug)]
+pub struct Enum {
+ /// The representation used for this enum; it should be an `IntKind` type or
+ /// an alias to one.
+ ///
+ /// It's `None` if the enum is a forward declaration and isn't defined
+ /// anywhere else, see `tests/headers/func_ptr_in_struct.h`.
+ repr: Option<ItemId>,
+
+ /// The different variants, with explicit values.
+ variants: Vec<EnumVariant>,
+}
+
+impl Enum {
+ /// Construct a new `Enum` with the given representation and variants.
+ pub fn new(repr: Option<ItemId>, variants: Vec<EnumVariant>) -> Self {
+ Enum {
+ repr: repr,
+ variants: variants,
+ }
+ }
+
+ /// Get this enumeration's representation.
+ pub fn repr(&self) -> Option<ItemId> {
+ self.repr
+ }
+
+ /// Get this enumeration's variants.
+ pub fn variants(&self) -> &[EnumVariant] {
+ &self.variants
+ }
+
+ /// Construct an enumeration from the given Clang type.
+ pub fn from_ty(ty: &clang::Type,
+ ctx: &mut BindgenContext)
+ -> Result<Self, ParseError> {
+ use clangll::*;
+ if ty.kind() != CXType_Enum {
+ return Err(ParseError::Continue);
+ }
+
+ let declaration = ty.declaration().canonical();
+ let repr = Item::from_ty(&declaration.enum_type(), None, None, ctx)
+ .ok();
+ let mut variants = vec![];
+
+ let is_signed = match repr {
+ Some(repr) => {
+ let repr_type = ctx.resolve_type(repr);
+ match *repr_type.canonical_type(ctx).kind() {
+ TypeKind::Int(ref int_kind) => int_kind.is_signed(),
+ ref other => {
+ panic!("Since when enums can be non-integers? {:?}",
+ other)
+ }
+ }
+ }
+ // Assume signedness since the default type by the C
+ // standard is an
+ // int.
+ None => true,
+ };
+
+ declaration.visit(|cursor| {
+ if cursor.kind() == CXCursor_EnumConstantDecl {
+ let value = if is_signed {
+ cursor.enum_val_signed().map(EnumVariantValue::Signed)
+ } else {
+ cursor.enum_val_unsigned().map(EnumVariantValue::Unsigned)
+ };
+ if let Some(val) = value {
+ let name = cursor.spelling();
+ let comment = cursor.raw_comment();
+ variants.push(EnumVariant::new(name, comment, val));
+ }
+ }
+ CXChildVisit_Continue
+ });
+ Ok(Enum::new(repr, variants))
+ }
+}
+
+/// A single enum variant, to be contained only in an enum.
+#[derive(Debug)]
+pub struct EnumVariant {
+ /// The name of the variant.
+ name: String,
+
+ /// An optional doc comment.
+ comment: Option<String>,
+
+ /// The integer value of the variant.
+ val: EnumVariantValue,
+}
+
+/// A constant value assigned to an enumeration variant.
+#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub enum EnumVariantValue {
+ /// A signed constant.
+ Signed(i64),
+
+ /// An unsigned constant.
+ Unsigned(u64),
+}
+
+impl EnumVariant {
+ /// Construct a new enumeration variant from the given parts.
+ pub fn new(name: String,
+ comment: Option<String>,
+ val: EnumVariantValue)
+ -> Self {
+ EnumVariant {
+ name: name,
+ comment: comment,
+ val: val,
+ }
+ }
+
+ /// Get this variant's name.
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ /// Get this variant's value.
+ pub fn val(&self) -> EnumVariantValue {
+ self.val
+ }
+}
diff --git a/libbindgen/src/ir/function.rs b/libbindgen/src/ir/function.rs
new file mode 100644
index 00000000..eacb6c0e
--- /dev/null
+++ b/libbindgen/src/ir/function.rs
@@ -0,0 +1,282 @@
+//! Intermediate representation for C/C++ functions and methods.
+
+use clang;
+use clangll::Enum_CXCallingConv;
+use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
+use super::context::{BindgenContext, ItemId};
+use super::item::Item;
+use super::ty::TypeKind;
+use super::type_collector::{ItemSet, TypeCollector};
+use syntax::abi;
+
+/// A function declaration, with a signature, arguments, and argument names.
+///
+/// The argument names vector must be the same length as the ones in the
+/// signature.
+#[derive(Debug)]
+pub struct Function {
+ /// The name of this function.
+ name: String,
+
+ /// The mangled name, that is, the symbol.
+ mangled_name: Option<String>,
+
+ /// The id pointing to the current function signature.
+ signature: ItemId,
+
+ /// The doc comment on the function, if any.
+ comment: Option<String>,
+}
+
+impl Function {
+ /// Construct a new function.
+ pub fn new(name: String,
+ mangled_name: Option<String>,
+ sig: ItemId,
+ comment: Option<String>)
+ -> Self {
+ Function {
+ name: name,
+ mangled_name: mangled_name,
+ signature: sig,
+ comment: comment,
+ }
+ }
+
+ /// Get this function's name.
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ /// Get this function's name.
+ pub fn mangled_name(&self) -> Option<&str> {
+ self.mangled_name.as_ref().map(|n| &**n)
+ }
+
+ /// Get this function's signature.
+ pub fn signature(&self) -> ItemId {
+ self.signature
+ }
+}
+
+/// A function signature.
+#[derive(Debug)]
+pub struct FunctionSig {
+ /// The return type of the function.
+ return_type: ItemId,
+
+ /// The type of the arguments, optionally with the name of the argument when
+ /// declared.
+ argument_types: Vec<(Option<String>, ItemId)>,
+
+ /// Whether this function is variadic.
+ is_variadic: bool,
+
+ /// The ABI of this function.
+ abi: abi::Abi,
+}
+
+fn get_abi(cc: Enum_CXCallingConv) -> abi::Abi {
+ use clangll::*;
+ match cc {
+ CXCallingConv_Default => abi::Abi::C,
+ CXCallingConv_C => abi::Abi::C,
+ CXCallingConv_X86StdCall => abi::Abi::Stdcall,
+ CXCallingConv_X86FastCall => abi::Abi::Fastcall,
+ CXCallingConv_AAPCS => abi::Abi::Aapcs,
+ CXCallingConv_X86_64Win64 => abi::Abi::Win64,
+ other => panic!("unsupported calling convention: {}", other),
+ }
+}
+
+/// Get the mangled name for the cursor's referent.
+pub fn cursor_mangling(cursor: &clang::Cursor) -> Option<String> {
+ // We early return here because libclang may crash in some case
+ // if we pass in a variable inside a partial specialized template.
+ // See servo/rust-bindgen#67.
+ if cursor.is_in_non_fully_specialized_template() {
+ return None;
+ }
+
+ let mut mangling = cursor.mangling();
+ if mangling.is_empty() {
+ return None;
+ }
+
+ // Try to undo backend linkage munging (prepended _, generally)
+ if cfg!(target_os = "macos") {
+ mangling.remove(0);
+ }
+
+ Some(mangling)
+}
+
+impl FunctionSig {
+ /// Construct a new function signature.
+ pub fn new(return_type: ItemId,
+ arguments: Vec<(Option<String>, ItemId)>,
+ is_variadic: bool,
+ abi: abi::Abi)
+ -> Self {
+ FunctionSig {
+ return_type: return_type,
+ argument_types: arguments,
+ is_variadic: is_variadic,
+ abi: abi,
+ }
+ }
+
+ /// Construct a new function signature from the given Clang type.
+ pub fn from_ty(ty: &clang::Type,
+ cursor: &clang::Cursor,
+ ctx: &mut BindgenContext)
+ -> Result<Self, ParseError> {
+ use clangll::*;
+ debug!("FunctionSig::from_ty {:?} {:?}", ty, cursor);
+
+ // Don't parse operatorxx functions in C++
+ let spelling = cursor.spelling();
+ if spelling.starts_with("operator") {
+ return Err(ParseError::Continue);
+ }
+
+ let cursor = if cursor.is_valid() {
+ *cursor
+ } else {
+ ty.declaration()
+ };
+ let mut args: Vec<_> = match cursor.kind() {
+ CXCursor_FunctionDecl |
+ CXCursor_CXXMethod => {
+ // For CXCursor_FunctionDecl, cursor.args() is the reliable way
+ // to get parameter names and types.
+ cursor.args()
+ .iter()
+ .map(|arg| {
+ let arg_ty = arg.cur_type();
+ let name = arg.spelling();
+ let name =
+ if name.is_empty() { None } else { Some(name) };
+ let ty = Item::from_ty(&arg_ty, Some(*arg), None, ctx)
+ .expect("Argument?");
+ (name, ty)
+ })
+ .collect()
+ }
+ _ => {
+ // For non-CXCursor_FunctionDecl, visiting the cursor's children
+ // is the only reliable way to get parameter names.
+ let mut args = vec![];
+ cursor.visit(|c| {
+ if c.kind() == CXCursor_ParmDecl {
+ let ty =
+ Item::from_ty(&c.cur_type(), Some(c), None, ctx)
+ .expect("ParmDecl?");
+ let name = c.spelling();
+ let name =
+ if name.is_empty() { None } else { Some(name) };
+ args.push((name, ty));
+ }
+ CXChildVisit_Continue
+ });
+ args
+ }
+ };
+
+ if cursor.kind() == CXCursor_CXXMethod {
+ let is_const = cursor.method_is_const();
+ let is_virtual = cursor.method_is_virtual();
+ let is_static = cursor.method_is_static();
+ if !is_static && !is_virtual {
+ let class = Item::parse(cursor.semantic_parent(), None, ctx)
+ .expect("Expected to parse the class");
+ let ptr =
+ Item::builtin_type(TypeKind::Pointer(class), is_const, ctx);
+ args.insert(0, (Some("this".into()), ptr));
+ } else if is_virtual {
+ let void = Item::builtin_type(TypeKind::Void, false, ctx);
+ let ptr =
+ Item::builtin_type(TypeKind::Pointer(void), false, ctx);
+ args.insert(0, (Some("this".into()), ptr));
+ }
+ }
+
+ let ty_ret_type = try!(ty.ret_type().ok_or(ParseError::Continue));
+ let ret = try!(Item::from_ty(&ty_ret_type, None, None, ctx));
+ let abi = get_abi(ty.call_conv());
+
+ Ok(Self::new(ret, args, ty.is_variadic(), abi))
+ }
+
+ /// Get this function signature's return type.
+ pub fn return_type(&self) -> ItemId {
+ self.return_type
+ }
+
+ /// Get this function signature's argument (name, type) pairs.
+ pub fn argument_types(&self) -> &[(Option<String>, ItemId)] {
+ &self.argument_types
+ }
+
+ /// Get this function signature's ABI.
+ pub fn abi(&self) -> abi::Abi {
+ self.abi
+ }
+
+ /// Is this function signature variadic?
+ pub fn is_variadic(&self) -> bool {
+ // Clang reports some functions as variadic when they *might* be
+ // variadic. We do the argument check because rust doesn't codegen well
+ // variadic functions without an initial argument.
+ self.is_variadic && !self.argument_types.is_empty()
+ }
+}
+
+impl ClangSubItemParser for Function {
+ fn parse(cursor: clang::Cursor,
+ context: &mut BindgenContext)
+ -> Result<ParseResult<Self>, ParseError> {
+ use clangll::*;
+ match cursor.kind() {
+ CXCursor_FunctionDecl |
+ CXCursor_CXXMethod => {}
+ _ => return Err(ParseError::Continue),
+ };
+
+ debug!("Function::parse({:?}, {:?})", cursor, cursor.cur_type());
+
+ // Grab the signature using Item::from_ty.
+ let sig = try!(Item::from_ty(&cursor.cur_type(),
+ Some(cursor),
+ None,
+ context));
+
+ let name = cursor.spelling();
+ assert!(!name.is_empty(), "Empty function name?");
+
+ let mut mangled_name = cursor_mangling(&cursor);
+ if mangled_name.as_ref() == Some(&name) {
+ mangled_name = None;
+ }
+
+ let comment = cursor.raw_comment();
+
+ let function = Self::new(name, mangled_name, sig, comment);
+ Ok(ParseResult::New(function, Some(cursor)))
+ }
+}
+
+impl TypeCollector for FunctionSig {
+ type Extra = Item;
+
+ fn collect_types(&self,
+ _context: &BindgenContext,
+ types: &mut ItemSet,
+ _item: &Item) {
+ types.insert(self.return_type());
+
+ for &(_, ty) in self.argument_types() {
+ types.insert(ty);
+ }
+ }
+}
diff --git a/libbindgen/src/ir/int.rs b/libbindgen/src/ir/int.rs
new file mode 100644
index 00000000..2d85db83
--- /dev/null
+++ b/libbindgen/src/ir/int.rs
@@ -0,0 +1,93 @@
+//! Intermediate representation for integral types.
+
+/// Which integral type are we dealing with?
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
+pub enum IntKind {
+ /// A `bool`.
+ Bool,
+
+ /// A `char`.
+ Char,
+
+ /// An `unsigned char`.
+ UChar,
+
+ /// A `short`.
+ Short,
+
+ /// An `unsigned short`.
+ UShort,
+
+ /// An `int`.
+ Int,
+
+ /// An `unsigned int`.
+ UInt,
+
+ /// A `long`.
+ Long,
+
+ /// An `unsigned long`.
+ ULong,
+
+ /// A `long long`.
+ LongLong,
+
+ /// An `unsigned long long`.
+ ULongLong,
+
+ /// A 8-bit signed integer.
+ I8,
+
+ /// A 8-bit unsigned integer.
+ U8,
+
+ /// A 16-bit signed integer.
+ I16,
+
+ /// Either a `char16_t` or a `wchar_t`.
+ U16,
+
+ /// A 32-bit signed integer.
+ I32,
+
+ /// A 32-bit unsigned integer.
+ U32,
+
+ /// A 64-bit signed integer.
+ I64,
+
+ /// A 64-bit unsigned integer.
+ U64,
+
+ /// An `int128_t`
+ I128,
+
+ /// A `uint128_t`.
+ U128,
+
+ /// A custom integer type, used to allow custom macro types depending on
+ /// range.
+ Custom {
+ /// The name of the type, which would be used without modification.
+ name: &'static str,
+ /// Whether the type is signed or not.
+ is_signed: bool,
+ },
+}
+
+impl IntKind {
+ /// Is this integral type signed?
+ pub fn is_signed(&self) -> bool {
+ use self::IntKind::*;
+ match *self {
+ Bool | UChar | UShort | UInt | ULong | ULongLong | U8 | U16 |
+ U32 | U64 | U128 => false,
+
+ Char | Short | Int | Long | LongLong | I8 | I16 | I32 | I64 |
+ I128 => true,
+
+ Custom { is_signed, .. } => is_signed,
+ }
+ }
+}
diff --git a/libbindgen/src/ir/item.rs b/libbindgen/src/ir/item.rs
new file mode 100644
index 00000000..1f05f92f
--- /dev/null
+++ b/libbindgen/src/ir/item.rs
@@ -0,0 +1,1172 @@
+//! Bindgen's core intermediate representation type.
+
+use clang;
+use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
+use regex::Regex;
+use std::cell::{Cell, RefCell};
+use super::annotations::Annotations;
+use super::context::{BindgenContext, ItemId};
+use super::function::Function;
+use super::item_kind::ItemKind;
+use super::module::Module;
+use super::ty::{Type, TypeKind};
+use super::type_collector::{ItemSet, TypeCollector};
+
+/// A trait to get the canonical name from an item.
+///
+/// This is the trait that will eventually isolate all the logic related to name
+/// mangling and that kind of stuff.
+///
+/// This assumes no nested paths, at some point I'll have to make it a more
+/// complex thing.
+///
+/// This name is required to be safe for Rust, that is, is not expected to
+/// return any rust keyword from here.
+pub trait ItemCanonicalName {
+ /// Get the canonical name for this item.
+ fn canonical_name(&self, ctx: &BindgenContext) -> String;
+}
+
+/// The same, but specifies the path that needs to be followed to reach an item.
+///
+/// To contrast with canonical_name, here's an example:
+///
+/// ```c++
+/// namespace foo {
+/// const BAR = 3;
+/// }
+/// ```
+///
+/// For bar, the canonical path is `vec!["foo", "BAR"]`, while the canonical
+/// name is just `"BAR"`.
+pub trait ItemCanonicalPath {
+ /// Get the canonical path for this item.
+ fn canonical_path(&self, ctx: &BindgenContext) -> Vec<String>;
+}
+
+/// A trait for iterating over an item and its parents and up its ancestor chain
+/// up to (but not including) the implicit root module.
+pub trait ItemAncestors {
+ /// Get an iterable over this item's ancestors.
+ fn ancestors<'a, 'b>(&self,
+ ctx: &'a BindgenContext<'b>)
+ -> ItemAncestorsIter<'a, 'b>;
+}
+
+/// An iterator over an item and its ancestors.
+pub struct ItemAncestorsIter<'a, 'b>
+ where 'b: 'a,
+{
+ item: ItemId,
+ ctx: &'a BindgenContext<'b>,
+}
+
+impl<'a, 'b> Iterator for ItemAncestorsIter<'a, 'b>
+ where 'b: 'a,
+{
+ type Item = ItemId;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let item = self.ctx.resolve_item(self.item);
+ if item.parent_id() == self.item {
+ None
+ } else {
+ self.item = item.parent_id();
+ Some(item.id())
+ }
+ }
+}
+
+// Pure convenience
+impl ItemCanonicalName for ItemId {
+ fn canonical_name(&self, ctx: &BindgenContext) -> String {
+ debug_assert!(ctx.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ ctx.resolve_item(*self).canonical_name(ctx)
+ }
+}
+
+impl ItemCanonicalPath for ItemId {
+ fn canonical_path(&self, ctx: &BindgenContext) -> Vec<String> {
+ debug_assert!(ctx.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ ctx.resolve_item(*self).canonical_path(ctx)
+ }
+}
+
+impl ItemAncestors for ItemId {
+ fn ancestors<'a, 'b>(&self,
+ ctx: &'a BindgenContext<'b>)
+ -> ItemAncestorsIter<'a, 'b> {
+ ItemAncestorsIter {
+ item: *self,
+ ctx: ctx,
+ }
+ }
+}
+
+impl ItemAncestors for Item {
+ fn ancestors<'a, 'b>(&self,
+ ctx: &'a BindgenContext<'b>)
+ -> ItemAncestorsIter<'a, 'b> {
+ self.id().ancestors(ctx)
+ }
+}
+
+impl TypeCollector for ItemId {
+ type Extra = ();
+
+ fn collect_types(&self,
+ ctx: &BindgenContext,
+ types: &mut ItemSet,
+ extra: &()) {
+ ctx.resolve_item(*self).collect_types(ctx, types, extra);
+ }
+}
+
+impl TypeCollector for Item {
+ type Extra = ();
+
+ fn collect_types(&self,
+ ctx: &BindgenContext,
+ types: &mut ItemSet,
+ _extra: &()) {
+ if self.is_hidden(ctx) || types.contains(&self.id()) {
+ return;
+ }
+
+ match *self.kind() {
+ ItemKind::Type(ref ty) => {
+ if !self.is_opaque(ctx) {
+ ty.collect_types(ctx, types, self);
+ }
+ }
+ _ => {} // FIXME.
+ }
+ }
+}
+
+/// An item is the base of the bindgen representation, it can be either a
+/// module, a type, a function, or a variable (see `ItemKind` for more
+/// information).
+///
+/// Items refer to each other by `ItemId`. Every item has its parent's
+/// id. Depending on the kind of item this is, it may also refer to other items,
+/// such as a compound type item referring to other types. Collectively, these
+/// references form a graph.
+///
+/// The entry-point to this graph is the "root module": a meta-item used to hold
+/// all top-level items.
+///
+/// An item may have a comment, and annotations (see the `annotations` module).
+///
+/// Note that even though we parse all the types of annotations in comments, not
+/// all of them apply to every item. Those rules are described in the
+/// `annotations` module.
+#[derive(Debug)]
+pub struct Item {
+ /// This item's id.
+ id: ItemId,
+
+ /// The item's local id, unique only amongst its siblings. Only used for
+ /// anonymous items.
+ ///
+ /// Lazily initialized in local_id().
+ ///
+ /// Note that only structs, unions, and enums get a local type id. In any
+ /// case this is an implementation detail.
+ local_id: Cell<Option<usize>>,
+
+ /// The next local id to use for a child..
+ next_child_local_id: Cell<usize>,
+
+ /// A cached copy of the canonical name, as returned by `canonical_name`.
+ ///
+ /// This is a fairly used operation during codegen so this makes bindgen
+ /// considerably faster in those cases.
+ canonical_name_cache: RefCell<Option<String>>,
+
+ /// A doc comment over the item, if any.
+ comment: Option<String>,
+ /// Annotations extracted from the doc comment, or the default ones
+ /// otherwise.
+ annotations: Annotations,
+ /// An item's parent id. This will most likely be a class where this item
+ /// was declared, or a module, etc.
+ ///
+ /// All the items have a parent, except the root module, in which case the
+ /// parent id is its own id.
+ parent_id: ItemId,
+ /// The item kind.
+ kind: ItemKind,
+}
+
+impl Item {
+ /// Construct a new `Item`.
+ pub fn new(id: ItemId,
+ comment: Option<String>,
+ annotations: Option<Annotations>,
+ parent_id: ItemId,
+ kind: ItemKind)
+ -> Self {
+ debug_assert!(id != parent_id || kind.is_module());
+ Item {
+ id: id,
+ local_id: Cell::new(None),
+ next_child_local_id: Cell::new(1),
+ canonical_name_cache: RefCell::new(None),
+ parent_id: parent_id,
+ comment: comment,
+ annotations: annotations.unwrap_or_default(),
+ kind: kind,
+ }
+ }
+
+ /// Get this `Item`'s identifier.
+ pub fn id(&self) -> ItemId {
+ self.id
+ }
+
+ /// Get this `Item`'s parent's identifier.
+ ///
+ /// For the root module, the parent's ID is its own ID.
+ pub fn parent_id(&self) -> ItemId {
+ self.parent_id
+ }
+
+ /// Get this `Item`'s comment, if it has any.
+ pub fn comment(&self) -> Option<&str> {
+ self.comment.as_ref().map(|c| &**c)
+ }
+
+ /// What kind of item is this?
+ pub fn kind(&self) -> &ItemKind {
+ &self.kind
+ }
+
+ /// Get a mutable reference to this item's kind.
+ pub fn kind_mut(&mut self) -> &mut ItemKind {
+ &mut self.kind
+ }
+
+ /// Get an identifier that differentiates this item from its siblings.
+ ///
+ /// This should stay relatively stable in the face of code motion outside or
+ /// below this item's lexical scope, meaning that this can be useful for
+ /// generating relatively stable identifiers within a scope.
+ pub fn local_id(&self, ctx: &BindgenContext) -> usize {
+ if self.local_id.get().is_none() {
+ let parent = ctx.resolve_item(self.parent_id);
+ let local_id = parent.next_child_local_id.get();
+ parent.next_child_local_id.set(local_id + 1);
+ self.local_id.set(Some(local_id));
+ }
+ self.local_id.get().unwrap()
+ }
+
+ /// Returns whether this item is a top-level item, from the point of view of
+ /// bindgen.
+ ///
+ /// This point of view changes depending on whether namespaces are enabled
+ /// or not. That way, in the following example:
+ ///
+ /// ```c++
+ /// namespace foo {
+ /// static int var;
+ /// }
+ /// ```
+ ///
+ /// `var` would be a toplevel item if namespaces are disabled, but won't if
+ /// they aren't.
+ ///
+ /// This function is used to determine when the codegen phase should call
+ /// `codegen` on an item, since any item that is not top-level will be
+ /// generated by its parent.
+ pub fn is_toplevel(&self, ctx: &BindgenContext) -> bool {
+ // FIXME: Workaround for some types falling behind when parsing weird
+ // stl classes, for example.
+ if ctx.options().enable_cxx_namespaces && self.kind().is_module() &&
+ self.id() != ctx.root_module() {
+ return false;
+ }
+
+ let mut parent = self.parent_id;
+ loop {
+ let parent_item = match ctx.resolve_item_fallible(parent) {
+ Some(item) => item,
+ None => return false,
+ };
+
+ if parent_item.id() == ctx.root_module() {
+ return true;
+ } else if ctx.options().enable_cxx_namespaces ||
+ !parent_item.kind().is_module() {
+ return false;
+ }
+
+ parent = parent_item.parent_id();
+ }
+ }
+
+ /// Get a reference to this item's underlying `Type`. Panic if this is some
+ /// other kind of item.
+ pub fn expect_type(&self) -> &Type {
+ self.kind().expect_type()
+ }
+
+ /// Get a reference to this item's underlying `Type`, or `None` if this is
+ /// some other kind of item.
+ pub fn as_type(&self) -> Option<&Type> {
+ self.kind().as_type()
+ }
+
+ /// Get a reference to this item's underlying `Function`. Panic if this is
+ /// some other kind of item.
+ pub fn expect_function(&self) -> &Function {
+ self.kind().expect_function()
+ }
+
+ /// Checks whether an item contains in its "type signature" some named type.
+ ///
+ /// This function is used to avoid unused template parameter errors in Rust
+ /// when generating typedef declarations, and also to know whether we need
+ /// to generate a `PhantomData` member for a template parameter.
+ ///
+ /// For example, in code like the following:
+ ///
+ /// ```c++
+ /// template<typename T, typename U>
+ /// struct Foo {
+ /// T bar;
+ ///
+ /// struct Baz {
+ /// U bas;
+ /// };
+ /// };
+ /// ```
+ ///
+ /// Both `Foo` and `Baz` contain both `T` and `U` template parameters in
+ /// their signature:
+ ///
+ /// * `Foo<T, U>`
+ /// * `Bar<T, U>`
+ ///
+ /// But the Rust structure for `Foo` would look like:
+ ///
+ /// ```rust
+ /// struct Foo<T, U> {
+ /// bar: T,
+ /// _phantom0: ::std::marker::PhantomData<U>,
+ /// }
+ /// ```
+ ///
+ /// because none of its member fields contained the `U` type in the
+ /// signature. Similarly, `Bar` would contain a `PhantomData<T>` type, for
+ /// the same reason.
+ ///
+ /// Note that this is somewhat similar to `applicable_template_args`, but
+ /// this also takes into account other kind of types, like arrays,
+ /// (`[T; 40]`), pointers: `*mut T`, etc...
+ ///
+ /// Normally we could do this check just in the `Type` kind, but we also
+ /// need to check the `applicable_template_args` more generally, since we
+ /// could need a type transitively from our parent, see the test added in
+ /// commit 2a3f93074dd2898669dbbce6e97e5cc4405d7cb1.
+ ///
+ /// It's kind of unfortunate (in the sense that it's a sort of complex
+ /// process), but I think it should get all the cases.
+ fn signature_contains_named_type(&self,
+ ctx: &BindgenContext,
+ ty: &Type)
+ -> bool {
+ debug_assert!(ty.is_named());
+ self.expect_type().signature_contains_named_type(ctx, ty) ||
+ self.applicable_template_args(ctx).iter().any(|template| {
+ ctx.resolve_type(*template).signature_contains_named_type(ctx, ty)
+ })
+ }
+
+ /// Returns the template arguments that apply to a struct. This is a concept
+ /// needed because of type declarations inside templates, for example:
+ ///
+ /// ```c++
+ /// template<typename T>
+ /// class Foo {
+ /// typedef T element_type;
+ /// typedef int Bar;
+ ///
+ /// template<typename U>
+ /// class Baz {
+ /// };
+ /// };
+ /// ```
+ ///
+ /// In this case, the applicable template arguments for the different types
+ /// would be:
+ ///
+ /// * `Foo`: [`T`]
+ /// * `Foo::element_type`: [`T`]
+ /// * `Foo::Bar`: [`T`]
+ /// * `Foo::Baz`: [`T`, `U`]
+ ///
+ /// You might notice that we can't generate something like:
+ ///
+ /// ```rust,ignore
+ /// type Foo_Bar<T> = ::std::os::raw::c_int;
+ /// ```
+ ///
+ /// since that would be invalid Rust. Still, conceptually, `Bar` *could* use
+ /// the template parameter type `T`, and that's exactly what this method
+ /// represents. The unused template parameters get stripped in the
+ /// `signature_contains_named_type` check.
+ pub fn applicable_template_args(&self,
+ ctx: &BindgenContext)
+ -> Vec<ItemId> {
+ let ty = match *self.kind() {
+ ItemKind::Type(ref ty) => ty,
+ _ => return vec![],
+ };
+
+ fn parent_contains(ctx: &BindgenContext,
+ parent_template_args: &[ItemId],
+ item: ItemId)
+ -> bool {
+ let item_ty = ctx.resolve_type(item);
+ parent_template_args.iter().any(|parent_item| {
+ let parent_ty = ctx.resolve_type(*parent_item);
+ match (parent_ty.kind(), item_ty.kind()) {
+ (&TypeKind::Named(ref n, _),
+ &TypeKind::Named(ref i, _)) => n == i,
+ _ => false,
+ }
+ })
+ }
+
+ match *ty.kind() {
+ TypeKind::Named(..) => vec![self.id()],
+ TypeKind::Array(inner, _) |
+ TypeKind::Pointer(inner) |
+ TypeKind::Reference(inner) |
+ TypeKind::ResolvedTypeRef(inner) => {
+ ctx.resolve_item(inner).applicable_template_args(ctx)
+ }
+ TypeKind::Alias(_, inner) => {
+ let parent_args = ctx.resolve_item(self.parent_id())
+ .applicable_template_args(ctx);
+ let inner = ctx.resolve_item(inner);
+
+ // Avoid unused type parameters, sigh.
+ parent_args.iter()
+ .cloned()
+ .filter(|arg| {
+ let arg = ctx.resolve_type(*arg);
+ arg.is_named() &&
+ inner.signature_contains_named_type(ctx, arg)
+ })
+ .collect()
+ }
+ // XXX Is this completely correct? Partial template specialization
+ // is hard anyways, sigh...
+ TypeKind::TemplateAlias(_, ref args) |
+ TypeKind::TemplateRef(_, ref args) => args.clone(),
+ // In a template specialization we've got all we want.
+ TypeKind::Comp(ref ci) if ci.is_template_specialization() => {
+ ci.template_args().iter().cloned().collect()
+ }
+ TypeKind::Comp(ref ci) => {
+ let mut parent_template_args =
+ ctx.resolve_item(self.parent_id())
+ .applicable_template_args(ctx);
+
+ for ty in ci.template_args() {
+ if !parent_contains(ctx, &parent_template_args, *ty) {
+ parent_template_args.push(*ty);
+ }
+ }
+
+ parent_template_args
+ }
+ _ => vec![],
+ }
+ }
+
+ fn is_module(&self) -> bool {
+ match self.kind {
+ ItemKind::Module(..) => true,
+ _ => false,
+ }
+ }
+
+ /// Get this item's annotations.
+ pub fn annotations(&self) -> &Annotations {
+ &self.annotations
+ }
+
+ /// Whether this item should be hidden.
+ ///
+ /// This may be due to either annotations or to other kind of configuration.
+ pub fn is_hidden(&self, ctx: &BindgenContext) -> bool {
+ debug_assert!(ctx.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ self.annotations.hide() ||
+ ctx.hidden_by_name(&self.real_canonical_name(ctx, false, true), self.id)
+ }
+
+ /// Is this item opaque?
+ pub fn is_opaque(&self, ctx: &BindgenContext) -> bool {
+ debug_assert!(ctx.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ self.annotations.opaque() ||
+ ctx.opaque_by_name(&self.real_canonical_name(ctx, false, true))
+ }
+
+ /// Is this a reference to another type?
+ pub fn is_type_ref(&self) -> bool {
+ self.as_type().map_or(false, |ty| ty.is_type_ref())
+ }
+
+ /// Get the canonical name without taking into account the replaces
+ /// annotation.
+ ///
+ /// This is the base logic used to implement hiding and replacing via
+ /// annotations, and also to implement proper name mangling.
+ ///
+ /// The idea is that each generated type in the same "level" (read: module
+ /// or namespace) has a unique canonical name.
+ ///
+ /// This name should be derived from the immutable state contained in the
+ /// type and the parent chain, since it should be consistent.
+ pub fn real_canonical_name(&self,
+ ctx: &BindgenContext,
+ count_namespaces: bool,
+ for_name_checking: bool)
+ -> String {
+ let base_name = match *self.kind() {
+ ItemKind::Type(ref ty) => {
+ match *ty.kind() {
+ // If we're a template specialization, our name is our
+ // parent's.
+ TypeKind::Comp(ref ci)
+ if ci.is_template_specialization() => {
+ return ci.specialized_template().unwrap()
+ .canonical_name(ctx);
+ },
+ // Same as above
+ TypeKind::ResolvedTypeRef(inner) |
+ TypeKind::TemplateRef(inner, _) => {
+ return inner.canonical_name(ctx);
+ }
+ // If we're a named type, we don't need to mangle it, and we
+ // should be able to assert we're not top level.
+ TypeKind::Named(ref name, _) => {
+ return name.to_owned();
+ }
+ // We call codegen on the inner type, but we do not want
+ // this alias's name to appear in the canonical name just
+ // because it is in the inner type's parent chain, so we use
+ // an empty base name.
+ //
+ // Note that this would be incorrect if this type could be
+ // referenced from, let's say, a member variable, but in
+ // that case the referenced type is the inner alias, so
+ // we're good there. If we wouldn't, a more complex solution
+ // would be needed.
+ TypeKind::TemplateAlias(inner, _) => {
+ if for_name_checking {
+ return ctx.resolve_item(inner)
+ .real_canonical_name(ctx,
+ count_namespaces,
+ false);
+ }
+ Some("")
+ }
+ // Else use the proper name, or fallback to a name with an
+ // id.
+ _ => {
+ ty.name()
+ }
+ }.map(ToOwned::to_owned)
+ }
+ ItemKind::Function(ref fun) => {
+ let mut base = fun.name().to_owned();
+
+ // We might need to deduplicate if we're a method.
+ let parent = ctx.resolve_item(self.parent_id());
+ if let ItemKind::Type(ref ty) = *parent.kind() {
+ if let TypeKind::Comp(ref ci) = *ty.kind() {
+ let mut count = 0;
+ let mut found = false;
+ for method in ci.methods() {
+ if method.signature() == self.id() {
+ found = true;
+ break;
+ }
+ let fun = ctx.resolve_item(method.signature())
+ .expect_function();
+ if fun.name() == base {
+ count += 1;
+ }
+ }
+
+ assert!(found, "Method not found?");
+ if count != 0 {
+ base.push_str(&count.to_string());
+ }
+ }
+ }
+ Some(base)
+ }
+ ItemKind::Var(ref var) => Some(var.name().to_owned()),
+ ItemKind::Module(ref module) => {
+ module.name().map(ToOwned::to_owned)
+ }
+ };
+
+ let parent = ctx.resolve_item(self.parent_id());
+ let parent_is_namespace = parent.is_module();
+
+ if self.is_toplevel(ctx) || (parent_is_namespace && count_namespaces) {
+ let base_name = self.make_exposed_name(None, base_name, ctx);
+ return ctx.rust_mangle(&base_name).into_owned();
+ }
+
+ // TODO: allow modification of the mangling functions, maybe even per
+ // item type?
+ let parent_name = parent.canonical_name(ctx);
+ self.make_exposed_name(Some(parent_name), base_name, ctx)
+ }
+
+ fn exposed_id(&self, ctx: &BindgenContext) -> String {
+ // Only use local ids for enums, classes, structs and union types. All
+ // other items use their global id.
+ let ty_kind = self.kind().as_type().map(|t| t.kind());
+ if let Some(ty_kind) = ty_kind {
+ match *ty_kind {
+ TypeKind::Comp(..) |
+ TypeKind::Enum(..) => return self.local_id(ctx).to_string(),
+ _ => {}
+ }
+ }
+
+ // Note that this `id_` prefix prevents (really unlikely) collisions
+ // between the global id and the local id of an item with the same
+ // parent.
+ format!("id_{}", self.id().as_usize())
+ }
+
+ fn make_exposed_name(&self,
+ parent_name: Option<String>,
+ base_name: Option<String>,
+ ctx: &BindgenContext)
+ -> String {
+ lazy_static! {
+ static ref RE_ENDS_WITH_BINDGEN_TY: Regex =
+ Regex::new(r"_bindgen_ty(_\d+)+$").unwrap();
+
+ static ref RE_ENDS_WITH_BINDGEN_MOD: Regex =
+ Regex::new(r"_bindgen_mod(_\d+)+$").unwrap();
+ }
+
+ let (re, kind) = match *self.kind() {
+ ItemKind::Module(..) => (&*RE_ENDS_WITH_BINDGEN_MOD, "mod"),
+ _ => (&*RE_ENDS_WITH_BINDGEN_TY, "ty"),
+ };
+
+ let parent_name =
+ parent_name.and_then(|n| if n.is_empty() { None } else { Some(n) });
+ match (parent_name, base_name) {
+ (Some(parent), Some(base)) => format!("{}_{}", parent, base),
+ (Some(parent), None) => {
+ if re.is_match(parent.as_str()) {
+ format!("{}_{}", parent, self.exposed_id(ctx))
+ } else {
+ format!("{}__bindgen_{}_{}",
+ parent,
+ kind,
+ self.exposed_id(ctx))
+ }
+ }
+ (None, Some(base)) => base,
+ (None, None) => {
+ format!("_bindgen_{}_{}", kind, self.exposed_id(ctx))
+ }
+ }
+ }
+
+ /// Get a mutable reference to this item's `Module`, or `None` if this is
+ /// not a `Module` item.
+ pub fn as_module_mut(&mut self) -> Option<&mut Module> {
+ match self.kind {
+ ItemKind::Module(ref mut module) => Some(module),
+ _ => None,
+ }
+ }
+
+ /// Can we derive an implementation of the `Copy` trait for this type?
+ pub fn can_derive_copy(&self, ctx: &BindgenContext) -> bool {
+ self.expect_type().can_derive_copy(ctx, self)
+ }
+
+ /// Can we derive an implementation of the `Copy` trait for an array of this
+ /// type?
+ ///
+ /// See `Type::can_derive_copy_in_array` for details.
+ pub fn can_derive_copy_in_array(&self, ctx: &BindgenContext) -> bool {
+ self.expect_type().can_derive_copy_in_array(ctx, self)
+ }
+}
+
+impl ClangItemParser for Item {
+ fn builtin_type(kind: TypeKind,
+ is_const: bool,
+ ctx: &mut BindgenContext)
+ -> ItemId {
+ // Feel free to add more here, I'm just lazy.
+ match kind {
+ TypeKind::Void |
+ TypeKind::Int(..) |
+ TypeKind::Pointer(..) |
+ TypeKind::Float(..) => {}
+ _ => panic!("Unsupported builtin type"),
+ }
+
+ let ty = Type::new(None, None, kind, is_const);
+ let id = ctx.next_item_id();
+ let module = ctx.root_module();
+ ctx.add_item(Item::new(id, None, None, module, ItemKind::Type(ty)),
+ None,
+ None);
+ id
+ }
+
+
+ fn parse(cursor: clang::Cursor,
+ parent_id: Option<ItemId>,
+ ctx: &mut BindgenContext)
+ -> Result<ItemId, ParseError> {
+ use ir::function::Function;
+ use ir::module::Module;
+ use ir::var::Var;
+ use clangll::*;
+
+ if !cursor.is_valid() {
+ return Err(ParseError::Continue);
+ }
+
+ let comment = cursor.raw_comment();
+ let annotations = Annotations::new(&cursor);
+
+ let current_module = ctx.current_module();
+ let relevant_parent_id = parent_id.unwrap_or(current_module);
+
+ macro_rules! try_parse {
+ ($what:ident) => {
+ match $what::parse(cursor, ctx) {
+ Ok(ParseResult::New(item, declaration)) => {
+ let id = ctx.next_item_id();
+
+ ctx.add_item(Item::new(id, comment, annotations,
+ relevant_parent_id,
+ ItemKind::$what(item)),
+ declaration,
+ Some(cursor));
+ return Ok(id);
+ }
+ Ok(ParseResult::AlreadyResolved(id)) => {
+ return Ok(id);
+ }
+ Err(ParseError::Recurse) => return Err(ParseError::Recurse),
+ Err(ParseError::Continue) => {},
+ }
+ }
+ }
+
+ try_parse!(Module);
+
+ // NOTE: Is extremely important to parse functions and vars **before**
+ // types. Otherwise we can parse a function declaration as a type
+ // (which is legal), and lose functions to generate.
+ //
+ // In general, I'm not totally confident this split between
+ // ItemKind::Function and TypeKind::FunctionSig is totally worth it, but
+ // I guess we can try.
+ try_parse!(Function);
+ try_parse!(Var);
+
+ // Types are sort of special, so to avoid parsing template classes
+ // twice, handle them separately.
+ {
+ let applicable_cursor = cursor.definition().unwrap_or(cursor);
+ match Self::from_ty(&applicable_cursor.cur_type(),
+ Some(applicable_cursor),
+ parent_id,
+ ctx) {
+ Ok(ty) => return Ok(ty),
+ Err(ParseError::Recurse) => return Err(ParseError::Recurse),
+ Err(ParseError::Continue) => {}
+ }
+ }
+
+ // Guess how does clang treat extern "C" blocks?
+ if cursor.kind() == CXCursor_UnexposedDecl {
+ Err(ParseError::Recurse)
+ } else {
+ // We whitelist cursors here known to be unhandled, to prevent being
+ // too noisy about this.
+ match cursor.kind() {
+ CXCursor_MacroDefinition |
+ CXCursor_MacroExpansion |
+ CXCursor_UsingDeclaration |
+ CXCursor_StaticAssert |
+ CXCursor_InclusionDirective => {
+ debug!("Unhandled cursor kind {:?}: {:?}",
+ cursor.kind(),
+ cursor);
+ }
+ _ => {
+ error!("Unhandled cursor kind {:?}: {:?}",
+ cursor.kind(),
+ cursor);
+ }
+ }
+
+ Err(ParseError::Continue)
+ }
+ }
+
+ fn from_ty_or_ref(ty: clang::Type,
+ location: Option<clang::Cursor>,
+ parent_id: Option<ItemId>,
+ ctx: &mut BindgenContext)
+ -> ItemId {
+ let id = ctx.next_item_id();
+ Self::from_ty_or_ref_with_id(id,
+ ty,
+ location,
+ parent_id,
+ ctx)
+ }
+
+ /// Parse a C++ type. If we find a reference to a type that has not been
+ /// defined yet, use `UnresolvedTypeRef` as a placeholder.
+ ///
+ /// This logic is needed to avoid parsing items with the incorrect parent
+ /// and it's sort of complex to explain, so I'll just point to
+ /// `tests/headers/typeref.hpp` to see the kind of constructs that forced
+ /// this.
+ ///
+ /// Typerefs are resolved once parsing is completely done, see
+ /// `BindgenContext::resolve_typerefs`.
+ fn from_ty_or_ref_with_id(potential_id: ItemId,
+ ty: clang::Type,
+ location: Option<clang::Cursor>,
+ parent_id: Option<ItemId>,
+ ctx: &mut BindgenContext)
+ -> ItemId {
+ debug!("from_ty_or_ref_with_id: {:?} {:?}, {:?}, {:?}",
+ potential_id,
+ ty,
+ location,
+ parent_id);
+
+ if ctx.collected_typerefs() {
+ debug!("refs already collected, resolving directly");
+ return Self::from_ty_with_id(potential_id,
+ &ty,
+ location,
+ parent_id,
+ ctx)
+ .expect("Unable to resolve type");
+ }
+
+ if let Some(ty) = ctx.builtin_or_resolved_ty(potential_id,
+ parent_id, &ty,
+ location) {
+ debug!("{:?} already resolved: {:?}", ty, location);
+ return ty;
+ }
+
+ debug!("New unresolved type reference: {:?}, {:?}", ty, location);
+
+ let is_const = ty.is_const();
+ let kind = TypeKind::UnresolvedTypeRef(ty, location, parent_id);
+ let current_module = ctx.current_module();
+ ctx.add_item(Item::new(potential_id,
+ None,
+ None,
+ parent_id.unwrap_or(current_module),
+ ItemKind::Type(Type::new(None,
+ None,
+ kind,
+ is_const))),
+ Some(clang::Cursor::null()),
+ None);
+ potential_id
+ }
+
+
+ fn from_ty(ty: &clang::Type,
+ location: Option<clang::Cursor>,
+ parent_id: Option<ItemId>,
+ ctx: &mut BindgenContext)
+ -> Result<ItemId, ParseError> {
+ let id = ctx.next_item_id();
+ Self::from_ty_with_id(id, ty, location, parent_id, ctx)
+ }
+
+ /// This is one of the trickiest methods you'll find (probably along with
+ /// some of the ones that handle templates in `BindgenContext`).
+ ///
+ /// This method parses a type, given the potential id of that type (if
+ /// parsing it was correct), an optional location we're scanning, which is
+ /// critical some times to obtain information, an optional parent item id,
+ /// that will, if it's `None`, become the current module id, and the
+ /// context.
+ fn from_ty_with_id(id: ItemId,
+ ty: &clang::Type,
+ location: Option<clang::Cursor>,
+ parent_id: Option<ItemId>,
+ ctx: &mut BindgenContext)
+ -> Result<ItemId, ParseError> {
+ use clangll::*;
+
+ let decl = {
+ let decl = ty.declaration();
+ decl.definition().unwrap_or(decl)
+ };
+
+ let comment = decl.raw_comment()
+ .or_else(|| location.as_ref().and_then(|l| l.raw_comment()));
+ let annotations = Annotations::new(&decl)
+ .or_else(|| location.as_ref().and_then(|l| Annotations::new(l)));
+
+ if let Some(ref annotations) = annotations {
+ if let Some(ref replaced) = annotations.use_instead_of() {
+ ctx.replace(replaced, id);
+ }
+ }
+
+ if let Some(ty) =
+ ctx.builtin_or_resolved_ty(id, parent_id, ty, location) {
+ return Ok(ty);
+ }
+
+ // First, check we're not recursing.
+ let mut valid_decl = decl.kind() != CXCursor_NoDeclFound;
+ let declaration_to_look_for = if valid_decl {
+ decl.canonical()
+ } else if location.is_some() &&
+ location.unwrap().kind() ==
+ CXCursor_ClassTemplate {
+ valid_decl = true;
+ location.unwrap()
+ } else {
+ decl
+ };
+
+ if valid_decl {
+ if let Some(&(_, item_id)) = ctx.currently_parsed_types
+ .iter()
+ .find(|&&(d, _)| d == declaration_to_look_for) {
+ debug!("Avoiding recursion parsing type: {:?}", ty);
+ return Ok(item_id);
+ }
+ }
+
+ let current_module = ctx.current_module();
+ if valid_decl {
+ ctx.currently_parsed_types.push((declaration_to_look_for, id));
+ }
+
+ let result = Type::from_clang_ty(id, ty, location, parent_id, ctx);
+ let relevant_parent_id = parent_id.unwrap_or(current_module);
+ let ret = match result {
+ Ok(ParseResult::AlreadyResolved(ty)) => Ok(ty),
+ Ok(ParseResult::New(item, declaration)) => {
+ ctx.add_item(Item::new(id,
+ comment,
+ annotations,
+ relevant_parent_id,
+ ItemKind::Type(item)),
+ declaration,
+ location);
+ Ok(id)
+ }
+ Err(ParseError::Continue) => Err(ParseError::Continue),
+ Err(ParseError::Recurse) => {
+ debug!("Item::from_ty recursing in the ast");
+ let mut result = Err(ParseError::Recurse);
+ if let Some(ref location) = location {
+ // Need to pop here, otherwise we'll get stuck.
+ //
+ // TODO: Find a nicer interface, really. Also, the
+ // declaration_to_look_for suspiciously shares a lot of
+ // logic with ir::context, so we should refactor that.
+ if valid_decl {
+ let (popped_decl, _) =
+ ctx.currently_parsed_types.pop().unwrap();
+ assert_eq!(popped_decl, declaration_to_look_for);
+ }
+
+ location.visit(|cur| {
+ use clangll::*;
+ result = Item::from_ty_with_id(id,
+ ty,
+ Some(cur),
+ parent_id,
+ ctx);
+ match result {
+ Ok(..) => CXChildVisit_Break,
+ Err(ParseError::Recurse) => CXChildVisit_Recurse,
+ Err(ParseError::Continue) => CXChildVisit_Continue,
+ }
+ });
+
+ if valid_decl {
+ ctx.currently_parsed_types
+ .push((declaration_to_look_for, id));
+ }
+ }
+ // If we have recursed into the AST all we know, and we still
+ // haven't found what we've got, let's just make a named type.
+ //
+ // This is what happens with some template members, for example.
+ //
+ // FIXME: Maybe we should restrict this to things with parent?
+ // It's harmless, but if we restrict that, then
+ // tests/headers/nsStyleAutoArray.hpp crashes.
+ if let Err(ParseError::Recurse) = result {
+ warn!("Unknown type, assuming named template type: id = {:?}; spelling = {}",
+ id,
+ ty.spelling());
+ Ok(Self::named_type_with_id(id,
+ ty.spelling(),
+ None,
+ relevant_parent_id,
+ ctx))
+ } else {
+ result
+ }
+ }
+ };
+
+ if valid_decl {
+ let (popped_decl, _) = ctx.currently_parsed_types.pop().unwrap();
+ assert_eq!(popped_decl, declaration_to_look_for);
+ }
+
+ ret
+ }
+
+ /// A named type is a template parameter, e.g., the "T" in Foo<T>. They're
+ /// always local so it's the only exception when there's no declaration for
+ /// a type.
+ ///
+ /// It must have an id, and must not be the current module id. Ideally we
+ /// could assert the parent id is a Comp(..) type, but that info isn't
+ /// available yet.
+ fn named_type_with_id<S>(id: ItemId,
+ name: S,
+ default: Option<ItemId>,
+ parent_id: ItemId,
+ ctx: &mut BindgenContext)
+ -> ItemId
+ where S: Into<String>,
+ {
+ // see tests/headers/const_tparam.hpp
+ // and tests/headers/variadic_tname.hpp
+ let name = name.into().replace("const ", "").replace(".", "");
+
+ ctx.add_item(Item::new(id,
+ None,
+ None,
+ parent_id,
+ ItemKind::Type(Type::named(name, default))),
+ None,
+ None);
+
+ id
+ }
+
+ fn named_type<S>(name: S,
+ default: Option<ItemId>,
+ parent_id: ItemId,
+ ctx: &mut BindgenContext)
+ -> ItemId
+ where S: Into<String>,
+ {
+ let id = ctx.next_item_id();
+ Self::named_type_with_id(id, name, default, parent_id, ctx)
+ }
+}
+
+impl ItemCanonicalName for Item {
+ fn canonical_name(&self, ctx: &BindgenContext) -> String {
+ debug_assert!(ctx.in_codegen_phase(),
+ "You're not supposed to call this yet");
+ if let Some(other_canon_type) = self.annotations.use_instead_of() {
+ return other_canon_type.to_owned();
+ }
+ if self.canonical_name_cache.borrow().is_none() {
+ *self.canonical_name_cache.borrow_mut() =
+ Some(self.real_canonical_name(ctx,
+ ctx.options()
+ .enable_cxx_namespaces,
+ false));
+ }
+ return self.canonical_name_cache.borrow().as_ref().unwrap().clone();
+ }
+}
+
+impl ItemCanonicalPath for Item {
+ fn canonical_path(&self, ctx: &BindgenContext) -> Vec<String> {
+ if !ctx.options().enable_cxx_namespaces {
+ return vec![self.canonical_name(ctx)];
+ }
+
+ if self.id() == ctx.root_module() {
+ match self.kind {
+ ItemKind::Module(ref module) => {
+ return vec![module.name().unwrap().into()]
+ }
+ _ => panic!("Something has wrong horribly wrong"),
+ }
+ }
+
+ // TODO: This duplicates too much logic with real_canonical_name.
+ if let ItemKind::Type(ref ty) = *self.kind() {
+ match *ty.kind() {
+ TypeKind::Comp(ref ci) if ci.is_template_specialization() => {
+ return ci.specialized_template()
+ .unwrap()
+ .canonical_path(ctx);
+ }
+ TypeKind::ResolvedTypeRef(inner) |
+ TypeKind::TemplateRef(inner, _) => {
+ return inner.canonical_path(ctx);
+ }
+ TypeKind::Named(ref name, _) => {
+ return vec![name.clone()];
+ }
+ _ => {}
+ }
+ }
+
+ let mut parent_path = self.parent_id().canonical_path(&ctx);
+ if parent_path.last()
+ .map_or(false, |parent_name| parent_name.is_empty()) {
+ // This only happens (or should only happen) when we're an alias,
+ // and our parent is a templated alias, in which case the last
+ // component of the path will be empty.
+ let is_alias = match *self.expect_type().kind() {
+ TypeKind::Alias(..) => true,
+ _ => false,
+ };
+ debug_assert!(is_alias, "How can this ever happen?");
+ parent_path.pop().unwrap();
+ }
+ parent_path.push(self.real_canonical_name(ctx, true, false));
+
+ parent_path
+ }
+}
diff --git a/libbindgen/src/ir/item_kind.rs b/libbindgen/src/ir/item_kind.rs
new file mode 100644
index 00000000..d9e4690c
--- /dev/null
+++ b/libbindgen/src/ir/item_kind.rs
@@ -0,0 +1,114 @@
+//! Different variants of an `Item` in our intermediate representation.
+
+use super::function::Function;
+use super::module::Module;
+use super::ty::Type;
+use super::var::Var;
+
+/// A item we parse and translate.
+#[derive(Debug)]
+pub enum ItemKind {
+ /// A module, created implicitly once (the root module), or via C++
+ /// namespaces.
+ Module(Module),
+
+ /// A type declared in any of the multiple ways it can be declared.
+ Type(Type),
+
+ /// A function or method declaration.
+ Function(Function),
+
+ /// A variable declaration, most likely a static.
+ Var(Var),
+}
+
+impl ItemKind {
+ /// Get a reference to this `ItemKind`'s underying `Module`, or `None` if it
+ /// is some other kind.
+ pub fn as_module(&self) -> Option<&Module> {
+ match *self {
+ ItemKind::Module(ref module) => Some(module),
+ _ => None,
+ }
+ }
+
+ /// Is this a module?
+ pub fn is_module(&self) -> bool {
+ self.as_module().is_some()
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Module`, or panic if it
+ /// is some other kind.
+ pub fn expect_module(&self) -> &Module {
+ self.as_module().expect("Not a module")
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Function`, or `None` if
+ /// it is some other kind.
+ pub fn as_function(&self) -> Option<&Function> {
+ match *self {
+ ItemKind::Function(ref func) => Some(func),
+ _ => None,
+ }
+ }
+
+ /// Is this a function?
+ pub fn is_function(&self) -> bool {
+ self.as_function().is_some()
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Function`, or panic if
+ /// it is some other kind.
+ pub fn expect_function(&self) -> &Function {
+ self.as_function().expect("Not a function")
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Type`, or `None` if
+ /// it is some other kind.
+ pub fn as_type(&self) -> Option<&Type> {
+ match *self {
+ ItemKind::Type(ref ty) => Some(ty),
+ _ => None,
+ }
+ }
+
+ /// Get a mutable reference to this `ItemKind`'s underying `Type`, or `None`
+ /// if it is some other kind.
+ pub fn as_type_mut(&mut self) -> Option<&mut Type> {
+ match *self {
+ ItemKind::Type(ref mut ty) => Some(ty),
+ _ => None,
+ }
+ }
+
+ /// Is this a type?
+ pub fn is_type(&self) -> bool {
+ self.as_type().is_some()
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Type`, or panic if it is
+ /// some other kind.
+ pub fn expect_type(&self) -> &Type {
+ self.as_type().expect("Not a type")
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Var`, or `None` if it is
+ /// some other kind.
+ pub fn as_var(&self) -> Option<&Var> {
+ match *self {
+ ItemKind::Var(ref v) => Some(v),
+ _ => None,
+ }
+ }
+
+ /// Is this a variable?
+ pub fn is_var(&self) -> bool {
+ self.as_var().is_some()
+ }
+
+ /// Get a reference to this `ItemKind`'s underying `Var`, or panic if it is
+ /// some other kind.
+ pub fn expect_var(&self) -> &Var {
+ self.as_var().expect("Not a var")
+ }
+}
diff --git a/libbindgen/src/ir/layout.rs b/libbindgen/src/ir/layout.rs
new file mode 100644
index 00000000..3ac4a5f4
--- /dev/null
+++ b/libbindgen/src/ir/layout.rs
@@ -0,0 +1,34 @@
+//! Intermediate representation for the physical layout of some type.
+
+/// A type that represents the struct layout of a type.
+#[derive(Debug, Clone, Copy)]
+pub struct Layout {
+ /// The size (in bytes) of this layout.
+ pub size: usize,
+ /// The alignment (in bytes) of this layout.
+ pub align: usize,
+ /// Whether this layout's members are packed or not.
+ pub packed: bool,
+}
+
+impl Layout {
+ /// Construct a new `Layout` with the given `size` and `align`. It is not
+ /// packed.
+ pub fn new(size: usize, align: usize) -> Self {
+ Layout {
+ size: size,
+ align: align,
+ packed: false,
+ }
+ }
+
+ /// Is this a zero-sized layout?
+ pub fn is_zero(&self) -> bool {
+ self.size == 0 && self.align == 0
+ }
+
+ /// Construct a zero-sized layout.
+ pub fn zero() -> Self {
+ Self::new(0, 0)
+ }
+}
diff --git a/libbindgen/src/ir/mod.rs b/libbindgen/src/ir/mod.rs
new file mode 100644
index 00000000..3c658a4a
--- /dev/null
+++ b/libbindgen/src/ir/mod.rs
@@ -0,0 +1,18 @@
+//! The ir module defines bindgen's intermediate representation.
+//!
+//! Parsing C/C++ generates the IR, while code generation outputs Rust code from
+//! the IR.
+
+pub mod annotations;
+pub mod comp;
+pub mod context;
+pub mod enum_ty;
+pub mod function;
+pub mod int;
+pub mod item;
+pub mod item_kind;
+pub mod layout;
+pub mod module;
+pub mod ty;
+pub mod type_collector;
+pub mod var;
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),
+ }
+ }
+}
diff --git a/libbindgen/src/ir/ty.rs b/libbindgen/src/ir/ty.rs
new file mode 100644
index 00000000..34af2db5
--- /dev/null
+++ b/libbindgen/src/ir/ty.rs
@@ -0,0 +1,869 @@
+//! Everything related to types in our intermediate representation.
+
+use clang::{self, Cursor};
+use parse::{ClangItemParser, ParseError, ParseResult};
+use super::comp::CompInfo;
+use super::context::{BindgenContext, ItemId};
+use super::enum_ty::Enum;
+use super::function::FunctionSig;
+use super::int::IntKind;
+use super::item::Item;
+use super::layout::Layout;
+use super::type_collector::{ItemSet, TypeCollector};
+
+/// The base representation of a type in bindgen.
+///
+/// A type has an optional name, which if present cannot be empty, a `layout`
+/// (size, alignment and packedness) if known, a `Kind`, which determines which
+/// kind of type it is, and whether the type is const.
+#[derive(Debug)]
+pub struct Type {
+ /// The name of the type, or None if it was an unnamed struct or union.
+ name: Option<String>,
+ /// The layout of the type, if known.
+ layout: Option<Layout>,
+ /// The inner kind of the type
+ kind: TypeKind,
+ /// Whether this type is const-qualified.
+ is_const: bool,
+}
+
+/// The maximum number of items in an array for which Rust implements common
+/// traits, and so if we have a type containing an array with more than this
+/// many items, we won't be able to derive common traits on that type.
+///
+/// We need type-level integers yesterday :'(
+pub const RUST_DERIVE_IN_ARRAY_LIMIT: usize = 32;
+
+impl Type {
+ /// Get the underlying `CompInfo` for this type, or `None` if this is some
+ /// other kind of type.
+ pub fn as_comp(&self) -> Option<&CompInfo> {
+ match self.kind {
+ TypeKind::Comp(ref ci) => Some(ci),
+ _ => None,
+ }
+ }
+
+ /// Construct a new `Type`.
+ pub fn new(name: Option<String>,
+ layout: Option<Layout>,
+ kind: TypeKind,
+ is_const: bool)
+ -> Self {
+ Type {
+ name: name,
+ layout: layout,
+ kind: kind,
+ is_const: is_const,
+ }
+ }
+
+ /// Which kind of type is this?
+ pub fn kind(&self) -> &TypeKind {
+ &self.kind
+ }
+
+ /// Get a mutable reference to this type's kind.
+ pub fn kind_mut(&mut self) -> &mut TypeKind {
+ &mut self.kind
+ }
+
+ /// Get this type's name.
+ pub fn name(&self) -> Option<&str> {
+ self.name.as_ref().map(|name| &**name)
+ }
+
+ /// Is this a compound type?
+ pub fn is_comp(&self) -> bool {
+ match self.kind {
+ TypeKind::Comp(..) => true,
+ _ => false,
+ }
+ }
+
+ /// Is this a named type?
+ pub fn is_named(&self) -> bool {
+ match self.kind {
+ TypeKind::Named(..) => true,
+ _ => false,
+ }
+ }
+
+ /// Is this a function type?
+ pub fn is_function(&self) -> bool {
+ match self.kind {
+ TypeKind::Function(..) => true,
+ _ => false,
+ }
+ }
+
+ /// Is this either a builtin or named type?
+ pub fn is_builtin_or_named(&self) -> bool {
+ match self.kind {
+ TypeKind::Void |
+ TypeKind::NullPtr |
+ TypeKind::Function(..) |
+ TypeKind::Array(..) |
+ TypeKind::Reference(..) |
+ TypeKind::Pointer(..) |
+ TypeKind::BlockPointer |
+ TypeKind::Int(..) |
+ TypeKind::Float(..) |
+ TypeKind::Named(..) => true,
+ _ => false,
+ }
+ }
+
+ /// Creates a new named type, with name `name`.
+ pub fn named(name: String, default: Option<ItemId>) -> Self {
+ assert!(!name.is_empty());
+ // TODO: stop duplicating the name, it's stupid.
+ let kind = TypeKind::Named(name.clone(), default);
+ Self::new(Some(name), None, kind, false)
+ }
+
+ /// Is this an integer type?
+ pub fn is_integer(&self) -> bool {
+ match self.kind {
+ TypeKind::Int(..) => true,
+ _ => false,
+ }
+ }
+
+ /// Is this a `const` qualified type?
+ pub fn is_const(&self) -> bool {
+ self.is_const
+ }
+
+ /// Is this a reference to another type?
+ pub fn is_type_ref(&self) -> bool {
+ match self.kind {
+ TypeKind::ResolvedTypeRef(_) |
+ TypeKind::UnresolvedTypeRef(_, _, _) => true,
+ _ => false,
+ }
+ }
+
+ /// What is the layout of this type?
+ pub fn layout(&self, ctx: &BindgenContext) -> Option<Layout> {
+ use std::mem;
+
+ self.layout.or_else(|| {
+ match self.kind {
+ TypeKind::Comp(ref ci) => ci.layout(ctx),
+ // FIXME(emilio): This is a hack for anonymous union templates.
+ // Use the actual pointer size!
+ TypeKind::Pointer(..) |
+ TypeKind::BlockPointer => {
+ Some(Layout::new(mem::size_of::<*mut ()>(),
+ mem::align_of::<*mut ()>()))
+ }
+ TypeKind::ResolvedTypeRef(inner) => {
+ ctx.resolve_type(inner).layout(ctx)
+ }
+ _ => None,
+ }
+ })
+ }
+
+ /// Wether we can derive rust's `Debug` annotation in Rust. This should
+ /// ideally be a no-op that just returns `true`, but instead needs to be a
+ /// recursive method that checks whether all the proper members can derive
+ /// debug or not, because of the limit rust has on 32 items as max in the
+ /// array.
+ pub fn can_derive_debug(&self, ctx: &BindgenContext) -> bool {
+ match self.kind {
+ TypeKind::Array(t, len) => {
+ len <= RUST_DERIVE_IN_ARRAY_LIMIT &&
+ ctx.resolve_type(t).can_derive_debug(ctx)
+ }
+ TypeKind::ResolvedTypeRef(t) |
+ TypeKind::TemplateAlias(t, _) |
+ TypeKind::Alias(_, t) => ctx.resolve_type(t).can_derive_debug(ctx),
+ TypeKind::Comp(ref info) => {
+ info.can_derive_debug(ctx, self.layout(ctx))
+ }
+ _ => true,
+ }
+ }
+
+ /// For some reason, deriving copies of an array of a type that is not known
+ /// to be copy is a compile error. e.g.:
+ ///
+ /// ```rust
+ /// #[derive(Copy, Clone)]
+ /// struct A<T> {
+ /// member: T,
+ /// }
+ /// ```
+ ///
+ /// is fine, while:
+ ///
+ /// ```rust,ignore
+ /// #[derive(Copy, Clone)]
+ /// struct A<T> {
+ /// member: [T; 1],
+ /// }
+ /// ```
+ ///
+ /// is an error.
+ ///
+ /// That's the whole point of the existence of `can_derive_copy_in_array`.
+ pub fn can_derive_copy_in_array(&self,
+ ctx: &BindgenContext,
+ item: &Item)
+ -> bool {
+ match self.kind {
+ TypeKind::ResolvedTypeRef(t) |
+ TypeKind::TemplateAlias(t, _) |
+ TypeKind::Alias(_, t) |
+ TypeKind::Array(t, _) => {
+ ctx.resolve_item(t)
+ .can_derive_copy_in_array(ctx)
+ }
+ TypeKind::Named(..) => false,
+ _ => self.can_derive_copy(ctx, item),
+ }
+ }
+
+ /// Wether we'd be able to derive the `Copy` trait in Rust or not. Same
+ /// rationale than `can_derive_debug`.
+ pub fn can_derive_copy(&self, ctx: &BindgenContext, item: &Item) -> bool {
+ match self.kind {
+ TypeKind::Array(t, len) => {
+ len <= RUST_DERIVE_IN_ARRAY_LIMIT &&
+ ctx.resolve_item(t).can_derive_copy_in_array(ctx)
+ }
+ TypeKind::ResolvedTypeRef(t) |
+ TypeKind::TemplateAlias(t, _) |
+ TypeKind::TemplateRef(t, _) |
+ TypeKind::Alias(_, t) => ctx.resolve_item(t).can_derive_copy(ctx),
+ TypeKind::Comp(ref info) => info.can_derive_copy(ctx, item),
+ _ => true,
+ }
+ }
+
+ /// Whether this type has a vtable.
+ pub fn has_vtable(&self, ctx: &BindgenContext) -> bool {
+ // FIXME: Can we do something about template parameters? Huh...
+ match self.kind {
+ TypeKind::TemplateRef(t, _) |
+ TypeKind::TemplateAlias(t, _) |
+ TypeKind::Alias(_, t) |
+ TypeKind::ResolvedTypeRef(t) => ctx.resolve_type(t).has_vtable(ctx),
+ TypeKind::Comp(ref info) => info.has_vtable(ctx),
+ _ => false,
+ }
+
+ }
+
+ /// Returns whether this type has a destructor.
+ pub fn has_destructor(&self, ctx: &BindgenContext) -> bool {
+ match self.kind {
+ TypeKind::TemplateRef(t, _) |
+ TypeKind::TemplateAlias(t, _) |
+ TypeKind::Alias(_, t) |
+ TypeKind::ResolvedTypeRef(t) => {
+ ctx.resolve_type(t).has_destructor(ctx)
+ }
+ TypeKind::Comp(ref info) => info.has_destructor(ctx),
+ _ => false,
+ }
+ }
+
+ /// See the comment in `Item::signature_contains_named_type`.
+ pub fn signature_contains_named_type(&self,
+ ctx: &BindgenContext,
+ ty: &Type)
+ -> bool {
+ debug_assert!(ty.is_named());
+ let name = match *ty.kind() {
+ TypeKind::Named(ref name, _) => name,
+ _ => unreachable!(),
+ };
+
+ match self.kind {
+ TypeKind::Named(ref this_name, _) => this_name == name,
+ TypeKind::ResolvedTypeRef(t) |
+ TypeKind::Array(t, _) |
+ TypeKind::Pointer(t) |
+ TypeKind::Alias(_, t) => {
+ ctx.resolve_type(t)
+ .signature_contains_named_type(ctx, ty)
+ }
+ TypeKind::Function(ref sig) => {
+ sig.argument_types().iter().any(|&(_, arg)| {
+ ctx.resolve_type(arg)
+ .signature_contains_named_type(ctx, ty)
+ }) ||
+ ctx.resolve_type(sig.return_type())
+ .signature_contains_named_type(ctx, ty)
+ }
+ TypeKind::TemplateAlias(_, ref template_args) |
+ TypeKind::TemplateRef(_, ref template_args) => {
+ template_args.iter().any(|arg| {
+ ctx.resolve_type(*arg)
+ .signature_contains_named_type(ctx, ty)
+ })
+ }
+ TypeKind::Comp(ref ci) => ci.signature_contains_named_type(ctx, ty),
+ _ => false,
+ }
+ }
+
+ /// 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. 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(..) |
+ TypeKind::Comp(..) |
+ TypeKind::Int(..) |
+ TypeKind::Float(..) |
+ TypeKind::Complex(..) |
+ TypeKind::Function(..) |
+ TypeKind::Enum(..) |
+ TypeKind::Reference(..) |
+ TypeKind::Void |
+ TypeKind::NullPtr |
+ TypeKind::BlockPointer |
+ TypeKind::Pointer(..) => Some(self),
+
+ TypeKind::ResolvedTypeRef(inner) |
+ TypeKind::Alias(_, inner) |
+ TypeKind::TemplateAlias(inner, _) |
+ TypeKind::TemplateRef(inner, _) => {
+ ctx.resolve_type(inner).safe_canonical_type(ctx)
+ }
+
+ TypeKind::UnresolvedTypeRef(..) => None,
+ }
+ }
+}
+
+/// The kind of float this type represents.
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub enum FloatKind {
+ /// A `float`.
+ Float,
+ /// A `double`.
+ Double,
+ /// A `long double`.
+ LongDouble,
+ /// A `__float128`.
+ Float128,
+}
+
+/// The different kinds of types that we can parse.
+#[derive(Debug)]
+pub enum TypeKind {
+ /// The void type.
+ Void,
+
+ /// The `nullptr_t` type.
+ NullPtr,
+
+ /// A compound type, that is, a class, struct, or union.
+ Comp(CompInfo),
+
+ /// An integer type, of a given kind. `bool` and `char` are also considered
+ /// integers.
+ Int(IntKind),
+
+ /// A floating point type.
+ Float(FloatKind),
+
+ /// A complex floating point type.
+ Complex(FloatKind),
+
+ /// A type alias, with a name, that points to another type.
+ Alias(String, ItemId),
+
+ /// A templated alias, pointing to an inner `Alias` type, with template
+ /// parameters.
+ TemplateAlias(ItemId, Vec<ItemId>),
+
+ /// An array of a type and a lenght.
+ Array(ItemId, usize),
+
+ /// A function type, with a given signature.
+ Function(FunctionSig),
+
+ /// An `enum` type.
+ Enum(Enum),
+
+ /// A pointer to a type. The bool field represents whether it's const or
+ /// not.
+ Pointer(ItemId),
+
+ /// A pointer to an Apple block.
+ BlockPointer,
+
+ /// A reference to a type, as in: int& foo().
+ Reference(ItemId),
+
+ /// A reference to a template, with different template parameter names. To
+ /// see why this is needed, check out the creation of this variant in
+ /// `Type::from_clang_ty`.
+ TemplateRef(ItemId, Vec<ItemId>),
+
+ /// A reference to a yet-to-resolve type. This stores the clang cursor
+ /// itself, and postpones its resolution.
+ ///
+ /// These are gone in a phase after parsing where these are mapped to
+ /// already known types, and are converted to ResolvedTypeRef.
+ ///
+ /// see tests/headers/typeref.hpp to see somewhere where this is a problem.
+ UnresolvedTypeRef(clang::Type,
+ Option<clang::Cursor>,
+ /* parent_id */
+ Option<ItemId>),
+
+ /// An indirection to another type.
+ ///
+ /// These are generated after we resolve a forward declaration, or when we
+ /// replace one type with another.
+ ResolvedTypeRef(ItemId),
+
+ /// A named type, that is, a template parameter, with an optional default
+ /// type.
+ Named(String, Option<ItemId>),
+}
+
+impl Type {
+ /// Whether this type is unsized, that is, has no members. This is used to
+ /// derive whether we should generate a dummy `_address` field for structs,
+ /// to comply to the C and C++ layouts, that specify that every type needs
+ /// to be addressable.
+ pub fn is_unsized(&self, ctx: &BindgenContext) -> bool {
+ debug_assert!(ctx.in_codegen_phase(), "Not yet");
+
+ match self.kind {
+ TypeKind::Void => true,
+ TypeKind::Comp(ref ci) => ci.is_unsized(ctx),
+ TypeKind::Array(inner, size) => {
+ size == 0 || ctx.resolve_type(inner).is_unsized(ctx)
+ }
+ TypeKind::ResolvedTypeRef(inner) |
+ TypeKind::Alias(_, inner) |
+ TypeKind::TemplateAlias(inner, _) |
+ TypeKind::TemplateRef(inner, _) => {
+ ctx.resolve_type(inner).is_unsized(ctx)
+ }
+ TypeKind::Named(..) |
+ TypeKind::Int(..) |
+ TypeKind::Float(..) |
+ TypeKind::Complex(..) |
+ TypeKind::Function(..) |
+ TypeKind::Enum(..) |
+ TypeKind::Reference(..) |
+ TypeKind::NullPtr |
+ TypeKind::BlockPointer |
+ TypeKind::Pointer(..) => false,
+
+ TypeKind::UnresolvedTypeRef(..) => {
+ unreachable!("Should have been resolved after parsing!");
+ }
+ }
+ }
+
+ /// This is another of the nasty methods. This one is the one that takes
+ /// care of the core logic of converting a clang type to a `Type`.
+ ///
+ /// It's sort of nasty and full of special-casing, but hopefully the
+ /// comments in every special case justify why they're there.
+ pub fn from_clang_ty(potential_id: ItemId,
+ ty: &clang::Type,
+ location: Option<Cursor>,
+ parent_id: Option<ItemId>,
+ ctx: &mut BindgenContext)
+ -> Result<ParseResult<Self>, ParseError> {
+ use clangll::*;
+ {
+ let already_resolved =
+ ctx.builtin_or_resolved_ty(potential_id,
+ parent_id,
+ ty,
+ location);
+ if let Some(ty) = already_resolved {
+ debug!("{:?} already resolved: {:?}", ty, location);
+ return Ok(ParseResult::AlreadyResolved(ty));
+ }
+ }
+
+ let layout = ty.fallible_layout().ok();
+ let cursor = ty.declaration();
+ let mut name = cursor.spelling();
+
+ debug!("from_clang_ty: {:?}, ty: {:?}, loc: {:?}",
+ potential_id,
+ ty,
+ location);
+ debug!("currently_parsed_types: {:?}", ctx.currently_parsed_types);
+
+ let canonical_ty = ty.canonical_type();
+ let kind = match ty.kind() {
+ CXType_Unexposed if *ty != canonical_ty &&
+ canonical_ty.kind() != CXType_Invalid => {
+ debug!("Looking for canonical type: {:?}", canonical_ty);
+ return Self::from_clang_ty(potential_id,
+ &canonical_ty,
+ location,
+ parent_id,
+ ctx);
+ }
+ CXType_Unexposed | CXType_Invalid => {
+ // For some reason Clang doesn't give us any hint in some
+ // situations where we should generate a function pointer (see
+ // tests/headers/func_ptr_in_struct.h), so we do a guess here
+ // trying to see if it has a valid return type.
+ if ty.ret_type().is_some() {
+ let signature = try!(FunctionSig::from_ty(ty,
+ &location.unwrap_or(cursor),
+ ctx));
+ TypeKind::Function(signature)
+ // Same here, with template specialisations we can safely
+ // assume this is a Comp(..)
+ } else if ty.template_args().map_or(false, |x| x.len() > 0) {
+ debug!("Template specialization: {:?}", ty);
+ let complex =
+ CompInfo::from_ty(potential_id, ty, location, ctx)
+ .expect("C'mon");
+ TypeKind::Comp(complex)
+ } else if let Some(location) = location {
+ match location.kind() {
+ CXCursor_ClassTemplatePartialSpecialization |
+ CXCursor_CXXBaseSpecifier |
+ CXCursor_ClassTemplate => {
+ if location.kind() == CXCursor_CXXBaseSpecifier {
+ // In the case we're parsing a base specifier
+ // inside an unexposed or invalid type, it means
+ // that we're parsing one of two things:
+ //
+ // * A template parameter.
+ // * A complex class that isn't exposed.
+ //
+ // This means, unfortunately, that there's no
+ // good way to differentiate between them.
+ //
+ // Probably we could try to look at the
+ // declaration and complicate more this logic,
+ // but we'll keep it simple... if it's a valid
+ // C++ identifier, we'll consider it as a
+ // template parameter.
+ //
+ // This is because:
+ //
+ // * We expect every other base that is a
+ // proper identifier (that is, a simple
+ // struct/union declaration), to be exposed,
+ // so this path can't be reached in that
+ // case.
+ //
+ // * Quite conveniently, complex base
+ // specifiers preserve their full names (that
+ // is: Foo<T> instead of Foo). We can take
+ // advantage of this.
+ //
+ // If we find some edge case where this doesn't
+ // work (which I guess is unlikely, see the
+ // different test cases[1][2][3][4]), we'd need
+ // to find more creative ways of differentiating
+ // these two cases.
+ //
+ // [1]: inherit_named.hpp
+ // [2]: forward-inherit-struct-with-fields.hpp
+ // [3]: forward-inherit-struct.hpp
+ // [4]: inherit-namespaced.hpp
+ if location.spelling()
+ .chars()
+ .all(|c| c.is_alphanumeric() || c == '_') {
+ return Err(ParseError::Recurse);
+ }
+ } else {
+ name = location.spelling();
+ }
+ let complex = CompInfo::from_ty(potential_id,
+ ty,
+ Some(location),
+ ctx)
+ .expect("C'mon");
+ TypeKind::Comp(complex)
+ }
+ CXCursor_TypeAliasTemplateDecl => {
+ debug!("TypeAliasTemplateDecl");
+
+ // We need to manually unwind this one.
+ let mut inner = Err(ParseError::Continue);
+ let mut args = vec![];
+
+ location.visit(|cur| {
+ match cur.kind() {
+ CXCursor_TypeAliasDecl => {
+ debug_assert!(cur.cur_type().kind() ==
+ CXType_Typedef);
+ inner =
+ Item::from_ty(&cur.cur_type(),
+ Some(cur),
+ Some(potential_id),
+ ctx);
+ }
+ CXCursor_TemplateTypeParameter => {
+ // See the comment in src/ir/comp.rs
+ // about the same situation.
+ if cur.spelling().is_empty() {
+ return CXChildVisit_Continue;
+ }
+
+ let default_type =
+ Item::from_ty(&cur.cur_type(),
+ Some(cur),
+ Some(potential_id),
+ ctx)
+ .ok();
+ let param =
+ Item::named_type(cur.spelling(),
+ default_type,
+ potential_id,
+ ctx);
+ args.push(param);
+ }
+ _ => {}
+ }
+ CXChildVisit_Continue
+ });
+
+ if inner.is_err() {
+ error!("Failed to parse templated alias {:?}",
+ location);
+ return Err(ParseError::Continue);
+ }
+
+ // NB: `args` may be empty here (if for example the
+ // template parameters are constants).
+ //
+ // We can't reject it here then because inner points
+ // to `potential_id` now, so either we remove
+ // `inner` and return an error, or carry on.
+ //
+ // In this case, we just carry on, since it seems
+ // easier if than removing every possible reference
+ // to `item` from `ctx`, and it doesn't give any
+ // problems that we didn't have anyway.
+ TypeKind::TemplateAlias(inner.unwrap(), args)
+ }
+ CXCursor_TemplateRef => {
+ let referenced = location.referenced().expect("expected value, got none");
+ let referenced_ty = referenced.cur_type();
+ let referenced_declaration =
+ Some(referenced_ty.declaration());
+
+ return Self::from_clang_ty(potential_id,
+ &referenced_ty,
+ referenced_declaration,
+ parent_id,
+ ctx);
+ }
+ CXCursor_TypeRef => {
+ let referenced = location.referenced().expect("expected value, got none");
+ let referenced_ty = referenced.cur_type();
+ let referenced_declaration =
+ Some(referenced_ty.declaration());
+
+ let item =
+ Item::from_ty_or_ref_with_id(
+ potential_id,
+ referenced_ty,
+ referenced_declaration,
+ parent_id,
+ ctx);
+ return Ok(ParseResult::AlreadyResolved(item));
+ }
+ _ => {
+ if ty.kind() == CXType_Unexposed {
+ warn!("Unexposed type {:?}, recursing inside, \
+ loc: {:?}",
+ ty,
+ location);
+ return Err(ParseError::Recurse);
+ }
+
+ // If the type name is empty we're probably
+ // over-recursing to find a template parameter name
+ // or something like that, so just don't be too
+ // noisy with it since it causes confusion, see for
+ // example the discussion in:
+ //
+ // https://github.com/jamesmunns/teensy3-rs/issues/9
+ if !ty.spelling().is_empty() {
+ error!("invalid type {:?}", ty);
+ } else {
+ warn!("invalid type {:?}", ty);
+ }
+ return Err(ParseError::Continue);
+ }
+ }
+ } else {
+ // TODO: Don't duplicate this!
+ if ty.kind() == CXType_Unexposed {
+ warn!("Unexposed type {:?}, recursing inside", ty);
+ return Err(ParseError::Recurse);
+ }
+
+ if !ty.spelling().is_empty() {
+ error!("invalid type {:?}", ty);
+ } else {
+ warn!("invalid type {:?}", ty);
+ }
+ return Err(ParseError::Continue);
+ }
+ }
+ // NOTE: We don't resolve pointers eagerly because the pointee type
+ // might not have been parsed, and if it contains templates or
+ // something else we might get confused, see the comment inside
+ // TypeRef.
+ //
+ // We might need to, though, if the context is already in the
+ // process of resolving them.
+ CXType_MemberPointer |
+ CXType_Pointer => {
+ let inner = Item::from_ty_or_ref(ty.pointee_type().unwrap(),
+ location,
+ parent_id,
+ ctx);
+ TypeKind::Pointer(inner)
+ }
+ CXType_BlockPointer => TypeKind::BlockPointer,
+ // XXX: RValueReference is most likely wrong, but I don't think we
+ // can even add bindings for that, so huh.
+ CXType_RValueReference |
+ CXType_LValueReference => {
+ let inner = Item::from_ty_or_ref(ty.pointee_type().unwrap(),
+ location,
+ parent_id,
+ ctx);
+ TypeKind::Reference(inner)
+ }
+ // XXX DependentSizedArray is wrong
+ CXType_VariableArray |
+ CXType_DependentSizedArray |
+ CXType_IncompleteArray => {
+ let inner = Item::from_ty(ty.elem_type().as_ref().unwrap(),
+ location,
+ parent_id,
+ ctx)
+ .expect("Not able to resolve array element?");
+ TypeKind::Pointer(inner)
+ }
+ CXType_FunctionNoProto |
+ CXType_FunctionProto => {
+ let signature = try!(FunctionSig::from_ty(ty,
+ &location.unwrap_or(cursor),
+ ctx));
+ TypeKind::Function(signature)
+ }
+ CXType_Typedef => {
+ let inner = cursor.typedef_type();
+ let inner =
+ Item::from_ty_or_ref(inner, location, parent_id, ctx);
+ TypeKind::Alias(ty.spelling(), inner)
+ }
+ CXType_Enum => {
+ let enum_ = Enum::from_ty(ty, ctx).expect("Not an enum?");
+ TypeKind::Enum(enum_)
+ }
+ CXType_Record => {
+ let complex =
+ CompInfo::from_ty(potential_id, ty, location, ctx)
+ .expect("Not a complex type?");
+ TypeKind::Comp(complex)
+ }
+ // FIXME: We stub vectors as arrays since in 99% of the cases the
+ // layout is going to be correct, and there's no way we can generate
+ // vector types properly in Rust for now.
+ //
+ // That being said, that should be fixed eventually.
+ CXType_Vector |
+ CXType_ConstantArray => {
+ let inner = Item::from_ty(ty.elem_type().as_ref().unwrap(),
+ location,
+ parent_id,
+ ctx)
+ .expect("Not able to resolve array element?");
+ TypeKind::Array(inner, ty.num_elements().unwrap())
+ }
+ #[cfg(not(feature="llvm_stable"))]
+ CXType_Elaborated => {
+ return Self::from_clang_ty(potential_id,
+ &ty.named(),
+ location,
+ parent_id,
+ ctx);
+ }
+ _ => {
+ error!("unsupported type: kind = {:?}; ty = {:?}; at {:?}",
+ ty.kind(),
+ ty,
+ location);
+ return Err(ParseError::Continue);
+ }
+ };
+
+ let name = if name.is_empty() { None } else { Some(name) };
+ let is_const = ty.is_const();
+
+ let ty = Type::new(name, layout, kind, is_const);
+ // TODO: maybe declaration.canonical()?
+ Ok(ParseResult::New(ty, Some(cursor.canonical())))
+ }
+}
+
+impl TypeCollector for Type {
+ type Extra = Item;
+
+ fn collect_types(&self,
+ context: &BindgenContext,
+ types: &mut ItemSet,
+ item: &Item) {
+ match *self.kind() {
+ TypeKind::Pointer(inner) |
+ TypeKind::Reference(inner) |
+ TypeKind::Array(inner, _) |
+ TypeKind::TemplateAlias(inner, _) |
+ TypeKind::Alias(_, inner) |
+ TypeKind::Named(_, Some(inner)) |
+ TypeKind::ResolvedTypeRef(inner) => {
+ types.insert(inner);
+ }
+
+ TypeKind::TemplateRef(inner, ref template_args) => {
+ types.insert(inner);
+ for &item in template_args {
+ types.insert(item);
+ }
+ }
+ TypeKind::Comp(ref ci) => ci.collect_types(context, types, item),
+ TypeKind::Function(ref sig) => {
+ sig.collect_types(context, types, item)
+ }
+ // FIXME: Pending types!
+ ref other @ _ => {
+ debug!("<Type as TypeCollector>::collect_types: Ignoring: {:?}", other);
+ }
+ }
+ }
+}
diff --git a/libbindgen/src/ir/type_collector.rs b/libbindgen/src/ir/type_collector.rs
new file mode 100644
index 00000000..0f10152d
--- /dev/null
+++ b/libbindgen/src/ir/type_collector.rs
@@ -0,0 +1,22 @@
+//! Collecting type items.
+
+use std::collections::BTreeSet;
+use super::context::{BindgenContext, ItemId};
+
+/// A set of items.
+pub type ItemSet = BTreeSet<ItemId>;
+
+/// Collect all the type items referenced by this item.
+pub trait TypeCollector {
+ /// If a particular type needs extra information beyond what it has in
+ /// `self` and `context` to find its referenced type items, its
+ /// implementation can define this associated type, forcing callers to pass
+ /// the needed information through.
+ type Extra;
+
+ /// Add each type item referenced by `self` into the `types` set.
+ fn collect_types(&self,
+ context: &BindgenContext,
+ types: &mut ItemSet,
+ extra: &Self::Extra);
+}
diff --git a/libbindgen/src/ir/var.rs b/libbindgen/src/ir/var.rs
new file mode 100644
index 00000000..d0c4d9ca
--- /dev/null
+++ b/libbindgen/src/ir/var.rs
@@ -0,0 +1,246 @@
+//! Intermediate representation of variables.
+
+use cexpr;
+use clang;
+use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
+use std::num::Wrapping;
+use super::context::{BindgenContext, ItemId};
+use super::function::cursor_mangling;
+use super::int::IntKind;
+use super::item::Item;
+use super::ty::TypeKind;
+
+/// A `Var` is our intermediate representation of a variable.
+#[derive(Debug)]
+pub struct Var {
+ /// The name of the variable.
+ name: String,
+ /// The mangled name of the variable.
+ mangled_name: Option<String>,
+ /// The type of the variable.
+ ty: ItemId,
+ /// TODO: support non-integer constants?
+ /// The integer value of the variable.
+ val: Option<i64>,
+ /// Whether this variable is const.
+ is_const: bool,
+}
+
+impl Var {
+ /// Construct a new `Var`.
+ pub fn new(name: String,
+ mangled: Option<String>,
+ ty: ItemId,
+ val: Option<i64>,
+ is_const: bool)
+ -> Var {
+ assert!(!name.is_empty());
+ Var {
+ name: name,
+ mangled_name: mangled,
+ ty: ty,
+ val: val,
+ is_const: is_const,
+ }
+ }
+
+ /// Is this variable `const` qualified?
+ pub fn is_const(&self) -> bool {
+ self.is_const
+ }
+
+ /// The value of this constant variable, if any.
+ pub fn val(&self) -> Option<i64> {
+ self.val
+ }
+
+ /// Get this variable's type.
+ pub fn ty(&self) -> ItemId {
+ self.ty
+ }
+
+ /// Get this variable's name.
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ /// Get this variable's mangled name.
+ pub fn mangled_name(&self) -> Option<&str> {
+ self.mangled_name.as_ref().map(|n| &**n)
+ }
+}
+
+impl ClangSubItemParser for Var {
+ fn parse(cursor: clang::Cursor,
+ ctx: &mut BindgenContext)
+ -> Result<ParseResult<Self>, ParseError> {
+ use clangll::*;
+ use cexpr::expr::EvalResult;
+ match cursor.kind() {
+ CXCursor_MacroDefinition => {
+ let value = parse_macro(ctx, &cursor, ctx.translation_unit());
+
+ let (id, value) = match value {
+ Some(v) => v,
+ None => return Err(ParseError::Continue),
+ };
+
+ assert!(!id.is_empty(), "Empty macro name?");
+
+ let previously_defined = ctx.parsed_macro(&id);
+
+ // NB: It's important to "note" the macro even if the result is
+ // not an integer, otherwise we might loose other kind of
+ // derived macros.
+ ctx.note_parsed_macro(id.clone(), value.clone());
+
+ if previously_defined {
+ let name = String::from_utf8(id).unwrap();
+ warn!("Duplicated macro definition: {}", name);
+ return Err(ParseError::Continue);
+ }
+
+ // NOTE: Unwrapping, here and above, is safe, because the
+ // identifier of a token comes straight from clang, and we
+ // enforce utf8 there, so we should have already panicked at
+ // this point.
+ let name = String::from_utf8(id).unwrap();
+ let (int_kind, val) = match value {
+ // TODO(emilio): Handle the non-invalid ones!
+ EvalResult::Float(..) |
+ EvalResult::Char(..) |
+ EvalResult::Str(..) |
+ EvalResult::Invalid => return Err(ParseError::Continue),
+
+ EvalResult::Int(Wrapping(value)) => {
+ let kind = ctx.options()
+ .type_chooser
+ .as_ref()
+ .and_then(|c| c.int_macro(&name, value))
+ .unwrap_or_else(|| {
+ if value < 0 {
+ if value < i32::min_value() as i64 {
+ IntKind::LongLong
+ } else {
+ IntKind::Int
+ }
+ } else if value > u32::max_value() as i64 {
+ IntKind::ULongLong
+ } else {
+ IntKind::UInt
+ }
+ });
+
+ (kind, value)
+ }
+ };
+
+ let ty = Item::builtin_type(TypeKind::Int(int_kind), true, ctx);
+
+ Ok(ParseResult::New(Var::new(name, None, ty, Some(val), true),
+ Some(cursor)))
+ }
+ CXCursor_VarDecl => {
+ let name = cursor.spelling();
+ if name.is_empty() {
+ warn!("Empty constant name?");
+ return Err(ParseError::Continue);
+ }
+
+ let ty = cursor.cur_type();
+
+ // XXX this is redundant, remove!
+ let is_const = ty.is_const();
+
+ let ty = Item::from_ty(&ty, Some(cursor), None, ctx)
+ .expect("Unable to resolve constant type?");
+
+ // Note: Ty might not be totally resolved yet, see
+ // tests/headers/inner_const.hpp
+ //
+ // That's fine because in that case we know it's not a literal.
+ let value = ctx.safe_resolve_type(ty)
+ .and_then(|t| t.safe_canonical_type(ctx))
+ .and_then(|t| if t.is_integer() { Some(t) } else { None })
+ .and_then(|_| {
+ get_integer_literal_from_cursor(&cursor,
+ ctx.translation_unit())
+ });
+
+ let mangling = cursor_mangling(&cursor);
+
+ let var = Var::new(name, mangling, ty, value, is_const);
+ Ok(ParseResult::New(var, Some(cursor)))
+
+ }
+ _ => {
+ /* TODO */
+ Err(ParseError::Continue)
+ }
+ }
+ }
+}
+
+/// Try and parse a macro using all the macros parsed until now.
+fn parse_macro(ctx: &BindgenContext,
+ cursor: &clang::Cursor,
+ unit: &clang::TranslationUnit)
+ -> Option<(Vec<u8>, cexpr::expr::EvalResult)> {
+ use cexpr::{expr, nom};
+
+ let cexpr_tokens = match unit.cexpr_tokens(cursor) {
+ None => return None,
+ Some(tokens) => tokens,
+ };
+
+ let parser = expr::IdentifierParser::new(ctx.parsed_macros());
+ let result = parser.macro_definition(&cexpr_tokens);
+
+ match result {
+ nom::IResult::Done(_, (id, val)) => Some((id.into(), val)),
+ _ => None,
+ }
+}
+
+fn parse_int_literal_tokens(cursor: &clang::Cursor,
+ unit: &clang::TranslationUnit)
+ -> Option<i64> {
+ use cexpr::{expr, nom};
+ use cexpr::expr::EvalResult;
+
+ let cexpr_tokens = match unit.cexpr_tokens(cursor) {
+ None => return None,
+ Some(tokens) => tokens,
+ };
+
+ // TODO(emilio): We can try to parse other kinds of literals.
+ match expr::expr(&cexpr_tokens) {
+ nom::IResult::Done(_, EvalResult::Int(Wrapping(val))) => Some(val),
+ _ => None,
+ }
+}
+
+fn get_integer_literal_from_cursor(cursor: &clang::Cursor,
+ unit: &clang::TranslationUnit)
+ -> Option<i64> {
+ use clangll::*;
+ let mut value = None;
+ cursor.visit(|c| {
+ match c.kind() {
+ CXCursor_IntegerLiteral |
+ CXCursor_UnaryOperator => {
+ value = parse_int_literal_tokens(&c, unit);
+ }
+ CXCursor_UnexposedExpr => {
+ value = get_integer_literal_from_cursor(&c, unit);
+ }
+ _ => (),
+ }
+ if value.is_some() {
+ CXChildVisit_Break
+ } else {
+ CXChildVisit_Continue
+ }
+ });
+ value
+}