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
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ impl CompInfo {
}

impl TemplateDeclaration for CompInfo {
fn template_params(&self, _ctx: &BindgenContext) -> Option<Vec<ItemId>> {
fn self_template_params(&self, _ctx: &BindgenContext) -> Option<Vec<ItemId>> {
if self.template_args.is_empty() {
None
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ impl<'ctx> BindgenContext<'ctx> {
.and_then(|canon_decl| {
self.get_resolved_type(&canon_decl)
.and_then(|template_decl_id| {
template_decl_id.num_template_params(self)
template_decl_id.num_self_template_params(self)
.map(|num_params| {
(*canon_decl.cursor(),
template_decl_id,
Expand All @@ -658,7 +658,7 @@ impl<'ctx> BindgenContext<'ctx> {
.cloned()
})
.and_then(|template_decl| {
template_decl.num_template_params(self)
template_decl.num_self_template_params(self)
.map(|num_template_params| {
(*template_decl.decl(),
template_decl.id(),
Expand Down Expand Up @@ -706,7 +706,7 @@ impl<'ctx> BindgenContext<'ctx> {
use clang_sys;

let num_expected_args = match self.resolve_type(template)
.num_template_params(self) {
.num_self_template_params(self) {
Some(n) => n,
None => {
warn!("Tried to instantiate a template for which we could not \
Expand Down Expand Up @@ -1331,13 +1331,13 @@ impl PartialType {
}

impl TemplateDeclaration for PartialType {
fn template_params(&self, _ctx: &BindgenContext) -> Option<Vec<ItemId>> {
fn self_template_params(&self, _ctx: &BindgenContext) -> Option<Vec<ItemId>> {
// Maybe at some point we will eagerly parse named types, but for now we
// don't and this information is unavailable.
None
}

fn num_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
// Wouldn't it be nice if libclang would reliably give us this
// information‽
match self.decl().kind() {
Expand Down
12 changes: 6 additions & 6 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,22 +930,22 @@ impl DotAttributes for Item {
}

impl TemplateDeclaration for ItemId {
fn template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
fn self_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
ctx.resolve_item_fallible(*self)
.and_then(|item| item.template_params(ctx))
.and_then(|item| item.self_template_params(ctx))
}
}

impl TemplateDeclaration for Item {
fn template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
self.kind.template_params(ctx)
fn self_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
self.kind.self_template_params(ctx)
}
}

impl TemplateDeclaration for ItemKind {
fn template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
fn self_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
match *self {
ItemKind::Type(ref ty) => ty.template_params(ctx),
ItemKind::Type(ref ty) => ty.self_template_params(ctx),
// If we start emitting bindings to explicitly instantiated
// functions, then we'll need to check ItemKind::Function for
// template params.
Expand Down
4 changes: 2 additions & 2 deletions src/ir/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<'a> MonotoneFramework for UsedTemplateParameters<'a> {
.map(|ty| match ty.kind() {
&TypeKind::TemplateInstantiation(decl, ref args) => {
let decl = ctx.resolve_type(decl);
let params = decl.template_params(ctx)
let params = decl.self_template_params(ctx)
.expect("a template instantiation's referenced \
template declaration should have template \
parameters");
Expand Down Expand Up @@ -255,7 +255,7 @@ impl<'a> MonotoneFramework for UsedTemplateParameters<'a> {
// only used if the template declaration uses the
// corresponding template parameter.
let decl = self.ctx.resolve_type(decl);
let params = decl.template_params(self.ctx)
let params = decl.self_template_params(self.ctx)
.expect("a template instantiation's referenced \
template declaration should have template \
parameters");
Expand Down
90 changes: 77 additions & 13 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,16 +16,50 @@ 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.
///
/// Note that these might *not* all be named types: C++ allows
/// constant-value template parameters. Of course, Rust does not allow
/// generic parameters to be anything but types, so we must treat them as
/// opaque, and avoid instantiating them.
fn template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>;
/// constant-value template parameters as well as template-template
/// parameters. Of course, Rust does not allow generic parameters to be
/// anything but types, so we must treat them as opaque, and avoid
/// instantiating them.
fn self_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>;

/// Get the number of free template parameters this template declaration
/// has.
Expand All @@ -34,8 +68,38 @@ pub trait TemplateDeclaration {
/// `template_params` returns `None`. This is useful when we only have
/// partial information about the template declaration, such as when we are
/// in the middle of parsing it.
fn num_template_params(&self, ctx: &BindgenContext) -> Option<usize> {
self.template_params(ctx).map(|params| params.len())
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())
}
}
}

Expand Down Expand Up @@ -487,18 +551,18 @@ fn is_invalid_named_type_empty_name() {


impl TemplateDeclaration for Type {
fn template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
self.kind.template_params(ctx)
fn self_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
self.kind.self_template_params(ctx)
}
}

impl TemplateDeclaration for TypeKind {
fn template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
fn self_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>> {
match *self {
TypeKind::ResolvedTypeRef(id) => {
ctx.resolve_type(id).template_params(ctx)
ctx.resolve_type(id).self_template_params(ctx)
}
TypeKind::Comp(ref comp) => comp.template_params(ctx),
TypeKind::Comp(ref comp) => comp.self_template_params(ctx),
TypeKind::TemplateAlias(_, ref args) => Some(args.clone()),

TypeKind::TemplateInstantiation(..) |
Expand Down