-
Notifications
You must be signed in to change notification settings - Fork 745
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
bors-servo
merged 4 commits into
rust-lang:master
from
fitzgen:gotta-land-some-of-this-stuff
Feb 23, 2017
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
40fc430
Rename TemplateDeclaration::{template_params => self_template_params}
fitzgen 83c1a95
Add TemplateDeclaration::all_template_parameters
fitzgen cf3b459
Add more `EdgeKind`s
fitzgen 7e22fca
Find the set of template parameters used for any given IR node
fitzgen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -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]) | | ||
/// |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. | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh, almost magical. |
||
.rev() | ||
.flat_map(|params| params) | ||
.collect()) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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(..) | | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.