Skip to content

Gotta land some of this stuff... #539

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 4 commits into from
Feb 23, 2017
Merged
Changes from 1 commit
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
67 changes: 65 additions & 2 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::dot::DotAttributes;
use super::enum_ty::Enum;
use super::function::FunctionSig;
use super::int::IntKind;
use super::item::Item;
use super::item::{Item, ItemAncestors};
use super::layout::Layout;
use super::objc::ObjCInterface;
use super::traversal::{Trace, Tracer};
Expand All @@ -16,7 +16,40 @@ use parse::{ClangItemParser, ParseError, ParseResult};
use std::io;
use std::mem;

/// Template declaration related methods.
/// Template declaration (and such declaration's template parameters) related
/// methods.
///
/// Consider this example:
///
/// ```c++
/// template <typename T, typename U>
/// class Foo {
/// template <typename V>
/// using Bar = V*;
///
/// class Inner {
/// T x;
/// U y;
/// Bar<int> z;
/// };
/// };
///
/// class Qux {
/// int y;
/// };
/// ```
///
/// The following table depicts the results of each trait method when invoked on
/// `Foo`, `Bar`, and `Qux`.
///
/// +------+----------------------+--------------------------+------------------------+
/// |Decl. | self_template_params | num_self_template_params | all_template_parameters|
/// +------+----------------------+--------------------------+------------------------+
/// |Foo | Some([T, U]) | Some(2) | Some([T, U]) |
/// |Bar | Some([V]) | Some(1) | Some([T, U, V]) |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me think... Do we handle shadowing? (Not that is a blocker, nor that we do now, but if we did that'd be awesome :P)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a note to add a test.

/// |Inner | None | None | Some([T, U]) |
/// |Qux | None | None | None |
/// +------+----------------------+--------------------------+------------------------+
pub trait TemplateDeclaration {
/// Get the set of `ItemId`s that make up this template declaration's free
/// template parameters.
Expand All @@ -38,6 +71,36 @@ pub trait TemplateDeclaration {
fn num_self_template_params(&self, ctx: &BindgenContext) -> Option<usize> {
self.self_template_params(ctx).map(|params| params.len())
}

/// Get the complete set of template parameters that can affect this
/// declaration.
///
/// Note that this item doesn't need to be a template declaration itself for
/// `Some` to be returned here (in contrast to `self_template_params`). If
/// this item is a member of a template declaration, then the parent's
/// template parameters are included here.
///
/// In the example above, `Inner` depends on both of the `T` and `U` type
/// parameters, even though it is not itself a template declaration and
/// therefore has no type parameters itself. Perhaps it helps to think about
/// how we would fully reference such a member type in C++:
/// `Foo<int,char>::Inner`. `Foo` *must* be instantiated with template
/// arguments before we can gain access to the `Inner` member type.
fn all_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>
where Self: ItemAncestors
{
let each_self_params: Vec<Vec<_>> = self.ancestors(ctx)
.filter_map(|id| id.self_template_params(ctx))
.collect();
if each_self_params.is_empty() {
None
} else {
Some(each_self_params.into_iter()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, almost magical.

.rev()
.flat_map(|params| params)
.collect())
}
}
}

/// The base representation of a type in bindgen.
Expand Down