use ast;
use std::fmt;
use std::io;
use std::iter::FromIterator;
use std::ops::Deref;
#[doc(hidden)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Substitutable {
UnscopedTemplateName(ast::UnscopedTemplateName),
Type(ast::Type),
TemplateTemplateParam(ast::TemplateTemplateParam),
UnresolvedType(ast::UnresolvedType),
Prefix(ast::Prefix),
}
impl<'subs, W> ast::Demangle<'subs, W> for Substitutable
where W: 'subs + io::Write
{
fn demangle<'prev, 'ctx>(&'subs self,
ctx: &'ctx mut ast::DemangleContext<'subs, W>,
stack: Option<ast::ArgScopeStack<'prev, 'subs>>)
-> io::Result<()> {
match *self {
Substitutable::UnscopedTemplateName(ref name) => name.demangle(ctx, stack),
Substitutable::Type(ref ty) => ty.demangle(ctx, stack),
Substitutable::TemplateTemplateParam(ref ttp) => ttp.demangle(ctx, stack),
Substitutable::UnresolvedType(ref ty) => ty.demangle(ctx, stack),
Substitutable::Prefix(ref prefix) => prefix.demangle(ctx, stack),
}
}
}
#[doc(hidden)]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct SubstitutionTable(Vec<Substitutable>);
impl fmt::Debug for SubstitutionTable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.pad("SubstitutionTable "));
f.debug_map().entries(self.0.iter().enumerate()).finish()
}
}
impl SubstitutionTable {
pub fn new() -> SubstitutionTable {
Default::default()
}
pub fn insert(&mut self, entity: Substitutable) -> usize {
let idx = self.0.len();
log!("SubstitutionTable::insert @ {}: {:?}", idx, entity);
self.0.push(entity);
idx
}
pub fn contains(&self, idx: usize) -> bool {
idx < self.0.len()
}
pub fn get_type(&self, handle: &ast::TypeHandle) -> Option<&ast::Type> {
if let ast::TypeHandle::BackReference(idx) = *handle {
self.0.get(idx).and_then(|s| match *s {
Substitutable::Type(ref ty) => Some(ty),
_ => None,
})
} else {
None
}
}
}
impl FromIterator<Substitutable> for SubstitutionTable {
fn from_iter<I: IntoIterator<Item = Substitutable>>(iter: I) -> Self {
SubstitutionTable(Vec::from_iter(iter))
}
}
impl Deref for SubstitutionTable {
type Target = [Substitutable];
fn deref(&self) -> &Self::Target {
&self.0[..]
}
}