Skip to content

clang::Type::template_args return Option<TypeTemplateArgIterator> #175

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 1 commit into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 34 additions & 10 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,25 +644,22 @@ impl Type {
Ok(Layout::new(size, align))
}

/// If this type is a class template specialization, return its number of
/// If this type is a class template specialization, return its
/// template arguments. Otherwise, return None.
pub fn num_template_args(&self) -> Option<u32> {
pub fn template_args(&self) -> Option<TypeTemplateArgIterator> {
let n = unsafe { clang_Type_getNumTemplateArguments(self.x) };
if n >= 0 {
Some(n as u32)
Some(TypeTemplateArgIterator {
x: self.x,
length: n as u32,
index: 0
})
} else {
debug_assert_eq!(n, -1);
None
}
}

/// Get the type of the `i`th template argument for this template
/// specialization.
pub fn template_arg_type(&self, i: u32) -> Type {
let n = i as c_int;
Type { x: unsafe { clang_Type_getTemplateArgumentAsType(self.x, n) } }
}

/// Given that this type is a pointer type, return the type that it points
/// to.
pub fn pointee_type(&self) -> Type {
Expand Down Expand Up @@ -747,6 +744,33 @@ impl Type {
}
}

/// An iterator for a type's template arguments.
pub struct TypeTemplateArgIterator {
x: CXType,
length: u32,
index: u32
}

impl Iterator for TypeTemplateArgIterator {
type Item = Type;
fn next(&mut self) -> Option<Type> {
if self.index < self.length {
let idx = self.index as c_int;
self.index += 1;
Some(Type { x: unsafe { clang_Type_getTemplateArgumentAsType(self.x, idx) } })
} else {
None
}
}
}

impl ExactSizeIterator for TypeTemplateArgIterator {
fn len(&self) -> usize {
assert!(self.index <= self.length);
(self.length - self.index) as usize
}
}

/// A `SourceLocation` is a file, line, column, and byte offset location for
/// some source text.
pub struct SourceLocation {
Expand Down
28 changes: 13 additions & 15 deletions src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,26 +489,24 @@ impl CompInfo {

let mut ci = CompInfo::new(kind);
ci.is_anonymous = cursor.is_anonymous();
ci.template_args = match ty.num_template_args() {
ci.template_args = match ty.template_args() {
// In forward declarations and not specializations, etc, they are in
// the ast, we'll meet them in CXCursor_TemplateTypeParameter
None => vec![],
Some(len) => {
let mut list = Vec::with_capacity(len as usize);
for i in 0..len {
let arg_type = ty.template_arg_type(i);
if arg_type.kind() != CXType_Invalid {
let type_id =
Item::from_ty_or_ref(arg_type, None, None, ctx);

list.push(type_id);
} else {
ci.has_non_type_template_params = true;
warn!("warning: Template parameter is not a type");
}
Some(arg_types) => {
let num_arg_types = arg_types.len();

let args = arg_types
.filter(|t| t.kind() != CXType_Invalid)
.map(|t| Item::from_ty_or_ref(t, None, None, ctx))
.collect::<Vec<_>>();
Copy link
Member

Choose a reason for hiding this comment

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

So much nicer :)


if args.len() != num_arg_types {
ci.has_non_type_template_params = true;
warn!("warning: Template parameter is not a type");
}

list
args
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ impl Type {
TypeKind::Function(signature)
// Same here, with template specialisations we can safely assume
// this is a Comp(..)
} else if ty.num_template_args().unwrap_or(0) > 0 {
} else if ty.template_args().map_or(false, |x| x.len() > 0) {
debug!("Template specialization: {:?}", ty);
let complex =
CompInfo::from_ty(potential_id, ty, location, ctx)
Expand Down