Skip to content

Commit b914a09

Browse files
committed
Implement ToRustTy for TemplateInstantiation
This pulls existing code out of Type's ToRustTy implementation and into an implementation of ToRustTy for TemplateInstantiation. Purely code motion.
1 parent fdea868 commit b914a09

File tree

1 file changed

+69
-61
lines changed

1 file changed

+69
-61
lines changed

src/codegen/mod.rs

+69-61
Original file line numberDiff line numberDiff line change
@@ -2265,67 +2265,7 @@ impl ToRustTy for Type {
22652265
aster::AstBuilder::new().ty().path().ids(path).build()
22662266
}
22672267
TypeKind::TemplateInstantiation(ref inst) => {
2268-
let decl = inst.template_definition();
2269-
let mut ty = decl.to_rust_ty(ctx).unwrap();
2270-
2271-
// If we gave up when making a type for the template definition,
2272-
// check if maybe we can make a better opaque blob for the
2273-
// instantiation.
2274-
if ty == aster::AstBuilder::new().ty().unit().unwrap() {
2275-
if let Some(layout) = self.layout(ctx) {
2276-
ty = BlobTyBuilder::new(layout).build().unwrap()
2277-
}
2278-
}
2279-
2280-
let decl_params = if let Some(params) =
2281-
decl.self_template_params(ctx) {
2282-
params
2283-
} else {
2284-
// This can happen if we generated an opaque type for a
2285-
// partial template specialization, in which case we just
2286-
// use the opaque type's layout. If we don't have a layout,
2287-
// we cross our fingers and hope for the best :-/
2288-
debug_assert!(ctx.resolve_type_through_type_refs(decl)
2289-
.is_opaque());
2290-
let layout = self.layout(ctx).unwrap_or(Layout::zero());
2291-
ty = BlobTyBuilder::new(layout).build().unwrap();
2292-
2293-
vec![]
2294-
};
2295-
2296-
// TODO: If the decl type is a template class/struct
2297-
// declaration's member template declaration, it could rely on
2298-
// generic template parameters from its outer template
2299-
// class/struct. When we emit bindings for it, it could require
2300-
// *more* type arguments than we have here, and we will need to
2301-
// reconstruct them somehow. We don't have any means of doing
2302-
// that reconstruction at this time.
2303-
2304-
if let ast::TyKind::Path(_, ref mut path) = ty.node {
2305-
let template_args = inst.template_arguments()
2306-
.iter()
2307-
.zip(decl_params.iter())
2308-
// Only pass type arguments for the type parameters that
2309-
// the decl uses.
2310-
.filter(|&(_, param)| ctx.uses_template_parameter(decl, *param))
2311-
.map(|(arg, _)| arg.to_rust_ty(ctx))
2312-
.collect::<Vec<_>>();
2313-
2314-
path.segments.last_mut().unwrap().parameters = if
2315-
template_args.is_empty() {
2316-
None
2317-
} else {
2318-
Some(P(ast::PathParameters::AngleBracketed(
2319-
ast::AngleBracketedParameterData {
2320-
lifetimes: vec![],
2321-
types: P::from_vec(template_args),
2322-
bindings: P::from_vec(vec![]),
2323-
}
2324-
)))
2325-
}
2326-
}
2327-
2328-
P(ty)
2268+
inst.to_rust_ty(ctx, self)
23292269
}
23302270
TypeKind::ResolvedTypeRef(inner) => inner.to_rust_ty(ctx),
23312271
TypeKind::TemplateAlias(inner, _) |
@@ -2409,6 +2349,74 @@ impl ToRustTy for Type {
24092349
}
24102350
}
24112351

2352+
impl ToRustTy for TemplateInstantiation {
2353+
type Extra = Type;
2354+
2355+
fn to_rust_ty(&self, ctx: &BindgenContext, self_ty: &Type) -> P<ast::Ty> {
2356+
let decl = self.template_definition();
2357+
let mut ty = decl.to_rust_ty(ctx).unwrap();
2358+
2359+
// If we gave up when making a type for the template definition,
2360+
// check if maybe we can make a better opaque blob for the
2361+
// instantiation.
2362+
if ty == aster::AstBuilder::new().ty().unit().unwrap() {
2363+
if let Some(layout) = self_ty.layout(ctx) {
2364+
ty = BlobTyBuilder::new(layout).build().unwrap()
2365+
}
2366+
}
2367+
2368+
let decl_params = if let Some(params) =
2369+
decl.self_template_params(ctx) {
2370+
params
2371+
} else {
2372+
// This can happen if we generated an opaque type for a
2373+
// partial template specialization, in which case we just
2374+
// use the opaque type's layout. If we don't have a layout,
2375+
// we cross our fingers and hope for the best :-/
2376+
debug_assert!(ctx.resolve_type_through_type_refs(decl)
2377+
.is_opaque());
2378+
let layout = self_ty.layout(ctx).unwrap_or(Layout::zero());
2379+
ty = BlobTyBuilder::new(layout).build().unwrap();
2380+
2381+
vec![]
2382+
};
2383+
2384+
// TODO: If the decl type is a template class/struct
2385+
// declaration's member template declaration, it could rely on
2386+
// generic template parameters from its outer template
2387+
// class/struct. When we emit bindings for it, it could require
2388+
// *more* type arguments than we have here, and we will need to
2389+
// reconstruct them somehow. We don't have any means of doing
2390+
// that reconstruction at this time.
2391+
2392+
if let ast::TyKind::Path(_, ref mut path) = ty.node {
2393+
let template_args = self.template_arguments()
2394+
.iter()
2395+
.zip(decl_params.iter())
2396+
// Only pass type arguments for the type parameters that
2397+
// the decl uses.
2398+
.filter(|&(_, param)| ctx.uses_template_parameter(decl, *param))
2399+
.map(|(arg, _)| arg.to_rust_ty(ctx))
2400+
.collect::<Vec<_>>();
2401+
2402+
path.segments.last_mut().unwrap().parameters = if
2403+
template_args.is_empty() {
2404+
None
2405+
} else {
2406+
Some(P(ast::PathParameters::AngleBracketed(
2407+
ast::AngleBracketedParameterData {
2408+
lifetimes: vec![],
2409+
types: P::from_vec(template_args),
2410+
bindings: P::from_vec(vec![]),
2411+
}
2412+
)))
2413+
}
2414+
}
2415+
2416+
P(ty)
2417+
}
2418+
}
2419+
24122420
impl ToRustTy for FunctionSig {
24132421
type Extra = Item;
24142422

0 commit comments

Comments
 (0)