Skip to content

Allow aliases and template aliases to be considered for replacement #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,27 @@ impl<'ctx> BindgenContext<'ctx> {
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,
};

// canonical_name calls are expensive.
let ci = match ty.as_comp() {
Some(ci) => ci,
None => continue,
};

if ci.is_template_specialization() {
continue;
match *ty.kind() {
TypeKind::Comp(ref ci) if !ci.is_template_specialization() => {}
TypeKind::TemplateAlias(_, _) |
TypeKind::Alias(_, _) => {}
_ => continue,
}

if let Some(replacement) = self.replacements
.get(&item.canonical_name(self)) {
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.
Expand Down
10 changes: 5 additions & 5 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,11 @@ impl Item {
///
/// This name should be derived from the immutable state contained in the
/// type and the parent chain, since it should be consistent.
fn real_canonical_name(&self,
ctx: &BindgenContext,
count_namespaces: bool,
for_name_checking: bool)
-> String {
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() {
Expand Down
15 changes: 15 additions & 0 deletions tests/expectations/replace_template_alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Rooted<T> {
pub ptr: MaybeWrapped<T>,
}
/// But the replacement type does use T!
///
/// <div rustbindgen replaces="MaybeWrapped" />
pub type MaybeWrapped<T> = T;
23 changes: 23 additions & 0 deletions tests/headers/replace_template_alias.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// bindgen-flags: -- --std=c++14

namespace JS {
namespace detail {

/// Notice how this doesn't use T.
template <typename T>
using MaybeWrapped = int;

}

template <typename T>
class Rooted {
detail::MaybeWrapped<T> ptr;
};

}

/// But the replacement type does use T!
///
/// <div rustbindgen replaces="MaybeWrapped" />
template <typename T>
using replaces_MaybeWrapped = T;