1//! Types dealing with the substitutions table.
23use super::DemangleWrite;
4use crate::ast;
5use alloc::vec::Vec;
6use core::fmt;
7use core::iter::FromIterator;
8use core::ops::Deref;
910/// An enumeration of all of the types that can end up in the substitution
11/// table.
12#[doc(hidden)]
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[allow(clippy::large_enum_variant)]
15pub enum Substitutable {
16/// An `<unscoped-template-name>` production.
17UnscopedTemplateName(ast::UnscopedTemplateName),
1819/// A `<type>` production.
20Type(ast::Type),
2122/// A `<template-template-param>` production.
23TemplateTemplateParam(ast::TemplateTemplateParam),
2425/// An `<unresolved-type>` production.
26UnresolvedType(ast::UnresolvedType),
2728/// A `<prefix>` production.
29Prefix(ast::Prefix),
30}
3132impl<'subs, W> ast::Demangle<'subs, W> for Substitutable
33where
34W: 'subs + DemangleWrite,
35{
36fn demangle<'prev, 'ctx>(
37&'subs self,
38 ctx: &'ctx mut ast::DemangleContext<'subs, W>,
39 scope: Option<ast::ArgScopeStack<'prev, 'subs>>,
40 ) -> fmt::Result {
41match *self {
42 Substitutable::UnscopedTemplateName(ref name) => name.demangle(ctx, scope),
43 Substitutable::Type(ref ty) => ty.demangle(ctx, scope),
44 Substitutable::TemplateTemplateParam(ref ttp) => ttp.demangle(ctx, scope),
45 Substitutable::UnresolvedType(ref ty) => ty.demangle(ctx, scope),
46 Substitutable::Prefix(ref prefix) => prefix.demangle(ctx, scope),
47 }
48 }
49}
5051impl<'a> ast::GetLeafName<'a> for Substitutable {
52fn get_leaf_name(&'a self, subs: &'a SubstitutionTable) -> Option<ast::LeafName<'a>> {
53match *self {
54 Substitutable::UnscopedTemplateName(ref name) => name.get_leaf_name(subs),
55 Substitutable::Prefix(ref prefix) => prefix.get_leaf_name(subs),
56 Substitutable::Type(ref ty) => ty.get_leaf_name(subs),
57_ => None,
58 }
59 }
60}
6162impl ast::IsCtorDtorConversion for Substitutable {
63fn is_ctor_dtor_conversion(&self, subs: &SubstitutionTable) -> bool {
64match *self {
65 Substitutable::Prefix(ref prefix) => prefix.is_ctor_dtor_conversion(subs),
66_ => false,
67 }
68 }
69}
7071/// The table of substitutable components that we have parsed thus far, and for
72/// which there are potential back-references.
73#[doc(hidden)]
74#[derive(Clone, Default, PartialEq, Eq)]
75pub struct SubstitutionTable {
76 substitutions: Vec<Substitutable>,
77// There are components which are typically candidates for substitution, but
78 // in some particular circumstances are not. Instances of such components
79 // which are not candidates for substitution end up in this part of the
80 // table. See `<prefix>` parsing for further details.
81non_substitutions: Vec<Substitutable>,
82}
8384impl fmt::Debug for SubstitutionTable {
85fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 f.pad("SubstitutionTable ")?;
87 f.debug_map()
88 .entries(self.substitutions.iter().enumerate())
89 .finish()?;
90 f.pad("non_substitutions ")?;
91 f.debug_map()
92 .entries(self.non_substitutions.iter().enumerate())
93 .finish()
94 }
95}
9697impl SubstitutionTable {
98/// Construct a new `SubstitutionTable`.
99pub fn new() -> SubstitutionTable {
100 Default::default()
101 }
102103/// Insert a freshly-parsed substitutable component into the table and
104 /// return the index at which it now lives.
105pub fn insert(&mut self, entity: Substitutable) -> usize {
106let idx = self.substitutions.len();
107log!("SubstitutionTable::insert @ {}: {:?}", idx, entity);
108self.substitutions.push(entity);
109 idx
110 }
111112/// Insert a an entity into the table that is not a candidate for
113 /// substitution.
114pub fn insert_non_substitution(&mut self, entity: Substitutable) -> usize {
115let idx = self.non_substitutions.len();
116self.non_substitutions.push(entity);
117 idx
118 }
119120/// Does this substitution table contain a component at the given index?
121pub fn contains(&self, idx: usize) -> bool {
122 idx < self.substitutions.len()
123 }
124125/// Get the type referenced by the given handle, or None if there is no such
126 /// entry, or there is an entry that is not a type.
127pub fn get_type(&self, handle: &ast::TypeHandle) -> Option<&ast::Type> {
128if let ast::TypeHandle::BackReference(idx) = *handle {
129self.substitutions.get(idx).and_then(|s| match *s {
130 Substitutable::Type(ref ty) => Some(ty),
131_ => None,
132 })
133 } else {
134None
135}
136 }
137138/// Remove the last entry from the substitutions table and return it, or
139 /// `None` if the table is empty.
140pub fn pop(&mut self) -> Option<Substitutable> {
141log!("SubstitutionTable::pop @ {}: {:?}", self.len(), self.last());
142self.substitutions.pop()
143 }
144145/// Get the `idx`th entity that is not a candidate for substitution. Panics
146 /// if `idx` is out of bounds.
147pub fn non_substitution(&self, idx: usize) -> &Substitutable {
148&self.non_substitutions[idx]
149 }
150151/// Get the `idx`th entity that is not a candidate for substitution. Returns
152 /// `None` if `idx` is out of bounds.
153pub fn get_non_substitution(&self, idx: usize) -> Option<&Substitutable> {
154self.non_substitutions.get(idx)
155 }
156}
157158impl FromIterator<Substitutable> for SubstitutionTable {
159fn from_iter<I: IntoIterator<Item = Substitutable>>(iter: I) -> Self {
160 SubstitutionTable {
161 substitutions: Vec::from_iter(iter),
162 non_substitutions: vec![],
163 }
164 }
165}
166167impl Deref for SubstitutionTable {
168type Target = [Substitutable];
169170fn deref(&self) -> &Self::Target {
171&self.substitutions[..]
172 }
173}