diff --git a/src/clang.rs b/src/clang.rs index 32eab97893..0297ac0a1c 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -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 { + pub fn template_args(&self) -> Option { 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 { @@ -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 { + 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 { diff --git a/src/ir/comp.rs b/src/ir/comp.rs index b9e9ec8b66..355adb5ce5 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -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::>(); + + if args.len() != num_arg_types { + ci.has_non_type_template_params = true; + warn!("warning: Template parameter is not a type"); } - list + args } }; diff --git a/src/ir/ty.rs b/src/ir/ty.rs index b9816f6ee1..442e012289 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -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)