Skip to content

Commit 60dd28e

Browse files
fitzgenKowasaki
authored andcommitted
Move the TemplateParameters trait from ir::ty to ir::template
Organizationally, it makes more sense.
1 parent be60d9c commit 60dd28e

File tree

8 files changed

+153
-154
lines changed

8 files changed

+153
-154
lines changed

src/codegen/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3354,8 +3354,8 @@ use ir::item_kind::ItemKind;
33543354
use ir::layout::Layout;
33553355
use ir::module::Module;
33563356
use ir::objc::{ObjCInterface, ObjCMethod};
3357-
use ir::template::{AsNamed, TemplateInstantiation};
3358-
use ir::ty::{TemplateParameters, Type, TypeKind};
3357+
use ir::template::{AsNamed, TemplateInstantiation, TemplateParameters};
3358+
use ir::ty::{Type, TypeKind};
33593359
use ir::var::Var;
33603360

33613361
use std::borrow::Cow;

src/ir/comp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
66
use super::item::Item;
77
use super::layout::Layout;
88
use super::traversal::{EdgeKind, Trace, Tracer};
9-
use super::ty::TemplateParameters;
9+
use super::template::TemplateParameters;
1010
use clang;
1111
use parse::{ClangItemParser, ParseError};
1212
use std::cell::Cell;

src/ir/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use super::item::{Item, ItemCanonicalPath, ItemSet};
66
use super::item_kind::ItemKind;
77
use super::module::{Module, ModuleKind};
88
use super::named::{UsedTemplateParameters, analyze};
9-
use super::template::TemplateInstantiation;
9+
use super::template::{TemplateInstantiation, TemplateParameters};
1010
use super::traversal::{self, Edge, ItemTraversal};
11-
use super::ty::{FloatKind, TemplateParameters, Type, TypeKind};
11+
use super::ty::{FloatKind, Type, TypeKind};
1212
use BindgenOptions;
1313
use cexpr;
1414
use callbacks::ParseCallbacks;

src/ir/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use super::function::Function;
88
use super::item_kind::ItemKind;
99
use super::layout::Opaque;
1010
use super::module::Module;
11-
use super::template::AsNamed;
11+
use super::template::{AsNamed, TemplateParameters};
1212
use super::traversal::{EdgeKind, Trace, Tracer};
13-
use super::ty::{TemplateParameters, Type, TypeKind};
13+
use super::ty::{Type, TypeKind};
1414
use clang;
1515
use clang_sys;
1616
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};

src/ir/named.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@
128128
129129
use super::context::{BindgenContext, ItemId};
130130
use super::item::{Item, ItemSet};
131-
use super::template::{AsNamed, TemplateInstantiation};
131+
use super::template::{AsNamed, TemplateInstantiation, TemplateParameters};
132132
use super::traversal::{EdgeKind, Trace};
133-
use super::ty::{TemplateParameters, TypeKind};
133+
use super::ty::TypeKind;
134134
use std::collections::{HashMap, HashSet};
135135
use std::fmt;
136136

src/ir/template.rs

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,152 @@
2929
3030
use super::context::{BindgenContext, ItemId};
3131
use super::derive::{CanDeriveCopy, CanDeriveDebug};
32-
use super::item::Item;
32+
use super::item::{Item, ItemAncestors};
3333
use super::layout::Layout;
3434
use super::traversal::{EdgeKind, Trace, Tracer};
3535
use clang;
3636
use parse::ClangItemParser;
3737

38+
/// Template declaration (and such declaration's template parameters) related
39+
/// methods.
40+
///
41+
/// This trait's methods distinguish between `None` and `Some([])` for
42+
/// declarations that are not templates and template declarations with zero
43+
/// parameters, in general.
44+
///
45+
/// Consider this example:
46+
///
47+
/// ```c++
48+
/// template <typename T, typename U>
49+
/// class Foo {
50+
/// T use_of_t;
51+
/// U use_of_u;
52+
///
53+
/// template <typename V>
54+
/// using Bar = V*;
55+
///
56+
/// class Inner {
57+
/// T x;
58+
/// U y;
59+
/// Bar<int> z;
60+
/// };
61+
///
62+
/// template <typename W>
63+
/// class Lol {
64+
/// // No use of W, but here's a use of T.
65+
/// T t;
66+
/// };
67+
///
68+
/// template <typename X>
69+
/// class Wtf {
70+
/// // X is not used because W is not used.
71+
/// Lol<X> lololol;
72+
/// };
73+
/// };
74+
///
75+
/// class Qux {
76+
/// int y;
77+
/// };
78+
/// ```
79+
///
80+
/// The following table depicts the results of each trait method when invoked on
81+
/// each of the declarations above:
82+
///
83+
/// +------+----------------------+--------------------------+------------------------+----
84+
/// |Decl. | self_template_params | num_self_template_params | all_template_parameters| ...
85+
/// +------+----------------------+--------------------------+------------------------+----
86+
/// |Foo | Some([T, U]) | Some(2) | Some([T, U]) | ...
87+
/// |Bar | Some([V]) | Some(1) | Some([T, U, V]) | ...
88+
/// |Inner | None | None | Some([T, U]) | ...
89+
/// |Lol | Some([W]) | Some(1) | Some([T, U, W]) | ...
90+
/// |Wtf | Some([X]) | Some(1) | Some([T, U, X]) | ...
91+
/// |Qux | None | None | None | ...
92+
/// +------+----------------------+--------------------------+------------------------+----
93+
///
94+
/// ----+------+-----+----------------------+
95+
/// ... |Decl. | ... | used_template_params |
96+
/// ----+------+-----+----------------------+
97+
/// ... |Foo | ... | Some([T, U]) |
98+
/// ... |Bar | ... | Some([V]) |
99+
/// ... |Inner | ... | None |
100+
/// ... |Lol | ... | Some([T]) |
101+
/// ... |Wtf | ... | Some([T]) |
102+
/// ... |Qux | ... | None |
103+
/// ----+------+-----+----------------------+
104+
pub trait TemplateParameters {
105+
/// Get the set of `ItemId`s that make up this template declaration's free
106+
/// template parameters.
107+
///
108+
/// Note that these might *not* all be named types: C++ allows
109+
/// constant-value template parameters as well as template-template
110+
/// parameters. Of course, Rust does not allow generic parameters to be
111+
/// anything but types, so we must treat them as opaque, and avoid
112+
/// instantiating them.
113+
fn self_template_params(&self,
114+
ctx: &BindgenContext)
115+
-> Option<Vec<ItemId>>;
116+
117+
/// Get the number of free template parameters this template declaration
118+
/// has.
119+
///
120+
/// Implementations *may* return `Some` from this method when
121+
/// `template_params` returns `None`. This is useful when we only have
122+
/// partial information about the template declaration, such as when we are
123+
/// in the middle of parsing it.
124+
fn num_self_template_params(&self, ctx: &BindgenContext) -> Option<usize> {
125+
self.self_template_params(ctx).map(|params| params.len())
126+
}
127+
128+
/// Get the complete set of template parameters that can affect this
129+
/// declaration.
130+
///
131+
/// Note that this item doesn't need to be a template declaration itself for
132+
/// `Some` to be returned here (in contrast to `self_template_params`). If
133+
/// this item is a member of a template declaration, then the parent's
134+
/// template parameters are included here.
135+
///
136+
/// In the example above, `Inner` depends on both of the `T` and `U` type
137+
/// parameters, even though it is not itself a template declaration and
138+
/// therefore has no type parameters itself. Perhaps it helps to think about
139+
/// how we would fully reference such a member type in C++:
140+
/// `Foo<int,char>::Inner`. `Foo` *must* be instantiated with template
141+
/// arguments before we can gain access to the `Inner` member type.
142+
fn all_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>
143+
where Self: ItemAncestors,
144+
{
145+
let each_self_params: Vec<Vec<_>> = self.ancestors(ctx)
146+
.filter_map(|id| id.self_template_params(ctx))
147+
.collect();
148+
if each_self_params.is_empty() {
149+
None
150+
} else {
151+
Some(each_self_params.into_iter()
152+
.rev()
153+
.flat_map(|params| params)
154+
.collect())
155+
}
156+
}
157+
158+
/// Get only the set of template parameters that this item uses. This is a
159+
/// subset of `all_template_params` and does not necessarily contain any of
160+
/// `self_template_params`.
161+
fn used_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>
162+
where Self: AsRef<ItemId>,
163+
{
164+
assert!(ctx.in_codegen_phase(),
165+
"template parameter usage is not computed until codegen");
166+
167+
let id = *self.as_ref();
168+
ctx.resolve_item(id)
169+
.all_template_params(ctx)
170+
.map(|all_params| {
171+
all_params.into_iter()
172+
.filter(|p| ctx.uses_template_parameter(id, *p))
173+
.collect()
174+
})
175+
}
176+
}
177+
38178
/// A trait for things which may or may not be a named template type parameter.
39179
pub trait AsNamed {
40180
/// Any extra information the implementor might need to make this decision.

src/ir/ty.rs

Lines changed: 2 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -7,158 +7,17 @@ use super::dot::DotAttributes;
77
use super::enum_ty::Enum;
88
use super::function::FunctionSig;
99
use super::int::IntKind;
10-
use super::item::{Item, ItemAncestors};
10+
use super::item::Item;
1111
use super::layout::{Layout, Opaque};
1212
use super::objc::ObjCInterface;
13-
use super::template::{AsNamed, TemplateInstantiation};
13+
use super::template::{AsNamed, TemplateInstantiation, TemplateParameters};
1414
use super::traversal::{EdgeKind, Trace, Tracer};
1515
use clang::{self, Cursor};
1616
use parse::{ClangItemParser, ParseError, ParseResult};
1717
use std::cell::Cell;
1818
use std::io;
1919
use std::mem;
2020

21-
/// Template declaration (and such declaration's template parameters) related
22-
/// methods.
23-
///
24-
/// This trait's methods distinguish between `None` and `Some([])` for
25-
/// declarations that are not templates and template declarations with zero
26-
/// parameters, in general.
27-
///
28-
/// Consider this example:
29-
///
30-
/// ```c++
31-
/// template <typename T, typename U>
32-
/// class Foo {
33-
/// T use_of_t;
34-
/// U use_of_u;
35-
///
36-
/// template <typename V>
37-
/// using Bar = V*;
38-
///
39-
/// class Inner {
40-
/// T x;
41-
/// U y;
42-
/// Bar<int> z;
43-
/// };
44-
///
45-
/// template <typename W>
46-
/// class Lol {
47-
/// // No use of W, but here's a use of T.
48-
/// T t;
49-
/// };
50-
///
51-
/// template <typename X>
52-
/// class Wtf {
53-
/// // X is not used because W is not used.
54-
/// Lol<X> lololol;
55-
/// };
56-
/// };
57-
///
58-
/// class Qux {
59-
/// int y;
60-
/// };
61-
/// ```
62-
///
63-
/// The following table depicts the results of each trait method when invoked on
64-
/// each of the declarations above:
65-
///
66-
/// +------+----------------------+--------------------------+------------------------+----
67-
/// |Decl. | self_template_params | num_self_template_params | all_template_parameters| ...
68-
/// +------+----------------------+--------------------------+------------------------+----
69-
/// |Foo | Some([T, U]) | Some(2) | Some([T, U]) | ...
70-
/// |Bar | Some([V]) | Some(1) | Some([T, U, V]) | ...
71-
/// |Inner | None | None | Some([T, U]) | ...
72-
/// |Lol | Some([W]) | Some(1) | Some([T, U, W]) | ...
73-
/// |Wtf | Some([X]) | Some(1) | Some([T, U, X]) | ...
74-
/// |Qux | None | None | None | ...
75-
/// +------+----------------------+--------------------------+------------------------+----
76-
///
77-
/// ----+------+-----+----------------------+
78-
/// ... |Decl. | ... | used_template_params |
79-
/// ----+------+-----+----------------------+
80-
/// ... |Foo | ... | Some([T, U]) |
81-
/// ... |Bar | ... | Some([V]) |
82-
/// ... |Inner | ... | None |
83-
/// ... |Lol | ... | Some([T]) |
84-
/// ... |Wtf | ... | Some([T]) |
85-
/// ... |Qux | ... | None |
86-
/// ----+------+-----+----------------------+
87-
88-
pub trait TemplateParameters {
89-
/// Get the set of `ItemId`s that make up this template declaration's free
90-
/// template parameters.
91-
///
92-
/// Note that these might *not* all be named types: C++ allows
93-
/// constant-value template parameters as well as template-template
94-
/// parameters. Of course, Rust does not allow generic parameters to be
95-
/// anything but types, so we must treat them as opaque, and avoid
96-
/// instantiating them.
97-
fn self_template_params(&self,
98-
ctx: &BindgenContext)
99-
-> Option<Vec<ItemId>>;
100-
101-
/// Get the number of free template parameters this template declaration
102-
/// has.
103-
///
104-
/// Implementations *may* return `Some` from this method when
105-
/// `template_params` returns `None`. This is useful when we only have
106-
/// partial information about the template declaration, such as when we are
107-
/// in the middle of parsing it.
108-
fn num_self_template_params(&self, ctx: &BindgenContext) -> Option<usize> {
109-
self.self_template_params(ctx).map(|params| params.len())
110-
}
111-
112-
/// Get the complete set of template parameters that can affect this
113-
/// declaration.
114-
///
115-
/// Note that this item doesn't need to be a template declaration itself for
116-
/// `Some` to be returned here (in contrast to `self_template_params`). If
117-
/// this item is a member of a template declaration, then the parent's
118-
/// template parameters are included here.
119-
///
120-
/// In the example above, `Inner` depends on both of the `T` and `U` type
121-
/// parameters, even though it is not itself a template declaration and
122-
/// therefore has no type parameters itself. Perhaps it helps to think about
123-
/// how we would fully reference such a member type in C++:
124-
/// `Foo<int,char>::Inner`. `Foo` *must* be instantiated with template
125-
/// arguments before we can gain access to the `Inner` member type.
126-
fn all_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>
127-
where Self: ItemAncestors,
128-
{
129-
let each_self_params: Vec<Vec<_>> = self.ancestors(ctx)
130-
.filter_map(|id| id.self_template_params(ctx))
131-
.collect();
132-
if each_self_params.is_empty() {
133-
None
134-
} else {
135-
Some(each_self_params.into_iter()
136-
.rev()
137-
.flat_map(|params| params)
138-
.collect())
139-
}
140-
}
141-
142-
/// Get only the set of template parameters that this item uses. This is a
143-
/// subset of `all_template_params` and does not necessarily contain any of
144-
/// `self_template_params`.
145-
fn used_template_params(&self, ctx: &BindgenContext) -> Option<Vec<ItemId>>
146-
where Self: AsRef<ItemId>,
147-
{
148-
assert!(ctx.in_codegen_phase(),
149-
"template parameter usage is not computed until codegen");
150-
151-
let id = *self.as_ref();
152-
ctx.resolve_item(id)
153-
.all_template_params(ctx)
154-
.map(|all_params| {
155-
all_params.into_iter()
156-
.filter(|p| ctx.uses_template_parameter(id, *p))
157-
.collect()
158-
})
159-
}
160-
}
161-
16221
/// The base representation of a type in bindgen.
16322
///
16423
/// A type has an optional name, which if present cannot be empty, a `layout`

src/uses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
3838
use ir::context::BindgenContext;
3939
use ir::item::{Item, ItemAncestors, ItemCanonicalName};
40-
use ir::ty::TemplateParameters;
40+
use ir::template::TemplateParameters;
4141
use std::io;
4242

4343
// Like `canonical_path`, except we always take namespaces into account, ignore

0 commit comments

Comments
 (0)