1 //! A mini version of ast::Ty, which is easier to use, and features an explicit `Self` type to use
2 //! when specifying impls to be derived.
7 use syntax::ast::{self, Expr, GenericParamKind, Generics, Ident, SelfKind, GenericArg};
8 use syntax::ext::base::ExtCtxt;
9 use syntax::ext::build::AstBuilder;
10 use syntax::source_map::{respan, DUMMY_SP};
13 use syntax_pos::symbol::kw;
15 /// The types of pointers
19 Borrowed(Option<&'a str>, ast::Mutability),
25 /// A path, e.g., `::std::option::Option::<i32>` (global). Has support
26 /// for type parameters and a lifetime.
30 lifetime: Option<&'a str>,
31 params: Vec<Box<Ty<'a>>>,
43 pub fn new(path: Vec<&str>) -> Path<'_> {
44 Path::new_(path, None, Vec::new(), PathKind::Std)
46 pub fn new_local(path: &str) -> Path<'_> {
47 Path::new_(vec![path], None, Vec::new(), PathKind::Local)
49 pub fn new_<'r>(path: Vec<&'r str>,
50 lifetime: Option<&'r str>,
51 params: Vec<Box<Ty<'r>>>,
66 self_generics: &Generics)
68 cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
74 self_generics: &Generics)
76 let mut idents = self.path.iter().map(|s| cx.ident_of(*s)).collect();
77 let lt = mk_lifetimes(cx, span, &self.lifetime);
78 let tys: Vec<P<ast::Ty>> =
79 self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect();
80 let params = lt.into_iter()
81 .map(|lt| GenericArg::Lifetime(lt))
82 .chain(tys.into_iter().map(|ty| GenericArg::Type(ty)))
86 PathKind::Global => cx.path_all(span, true, idents, params, Vec::new()),
87 PathKind::Local => cx.path_all(span, false, idents, params, Vec::new()),
89 let def_site = DUMMY_SP.apply_mark(cx.current_expansion.id);
90 idents.insert(0, Ident::new(kw::DollarCrate, def_site));
91 cx.path_all(span, false, idents, params, Vec::new())
98 /// A type. Supports pointers, Self, and literals.
103 Ptr(Box<Ty<'a>>, PtrTy<'a>),
104 /// mod::mod::Type<[lifetime], [Params...]>, including a plain type
105 /// parameter, and things like `i32`
111 pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
112 Borrowed(None, ast::Mutability::Immutable)
114 pub fn borrowed(ty: Box<Ty<'_>>) -> Ty<'_> {
115 Ptr(ty, borrowed_ptrty())
118 pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
119 Some(Some(borrowed_ptrty()))
122 pub fn borrowed_self<'r>() -> Ty<'r> {
123 borrowed(Box::new(Self_))
126 pub fn nil_ty<'r>() -> Ty<'r> {
130 fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
132 cx.lifetime(span, Ident::from_str(s))
136 fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
137 mk_lifetime(cx, span, lt).into_iter().collect()
145 self_generics: &Generics)
148 Ptr(ref ty, ref ptr) => {
149 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
151 Borrowed(ref lt, mutbl) => {
152 let lt = mk_lifetime(cx, span, lt);
153 cx.ty_rptr(span, raw_ty, lt, mutbl)
155 Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
158 Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
159 Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
160 Tuple(ref fields) => {
161 let ty = ast::TyKind::Tup(fields.iter()
162 .map(|f| f.to_ty(cx, span, self_ty, self_generics))
169 pub fn to_path(&self,
177 let params: Vec<_> = generics.params.iter().map(|param| match param.kind {
178 GenericParamKind::Lifetime { .. } => {
179 GenericArg::Lifetime(ast::Lifetime { id: param.id, ident: param.ident })
181 GenericParamKind::Type { .. } => {
182 GenericArg::Type(cx.ty_ident(span, param.ident))
184 GenericParamKind::Const { .. } => {
185 GenericArg::Const(cx.const_ident(span, param.ident))
189 cx.path_all(span, false, vec![self_ty], params, vec![])
191 Literal(ref p) => p.to_path(cx, span, self_ty, generics),
192 Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
193 Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
199 fn mk_ty_param(cx: &ExtCtxt<'_>,
202 attrs: &[ast::Attribute],
205 self_generics: &Generics)
206 -> ast::GenericParam {
207 let bounds = bounds.iter()
209 let path = b.to_path(cx, span, self_ident, self_generics);
213 cx.typaram(span, cx.ident_of(name), attrs.to_owned(), bounds, None)
216 fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
219 where_clause: ast::WhereClause {
220 predicates: Vec::new(),
227 /// Lifetimes and bounds on type parameters
229 pub struct LifetimeBounds<'a> {
230 pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
231 pub bounds: Vec<(&'a str, Vec<Path<'a>>)>,
234 impl<'a> LifetimeBounds<'a> {
235 pub fn empty() -> LifetimeBounds<'a> {
237 lifetimes: Vec::new(),
241 pub fn to_generics(&self,
245 self_generics: &Generics)
247 let generic_params = self.lifetimes
249 .map(|&(lt, ref bounds)| {
250 let bounds = bounds.iter()
251 .map(|b| ast::GenericBound::Outlives(cx.lifetime(span, Ident::from_str(b))));
252 cx.lifetime_def(span, Ident::from_str(lt), vec![], bounds.collect())
257 let (name, ref bounds) = *t;
258 mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics)
263 mk_generics(generic_params, span)
267 pub fn get_explicit_self(cx: &ExtCtxt<'_>,
269 self_ptr: &Option<PtrTy<'_>>)
270 -> (P<Expr>, ast::ExplicitSelf) {
271 // this constructs a fresh `self` path
272 let self_path = cx.expr_self(span);
274 None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Immutable))),
279 Borrowed(ref lt, mutbl) => {
280 let lt = lt.map(|s| cx.lifetime(span, Ident::from_str(s)));
281 SelfKind::Region(lt, mutbl)
284 cx.span_bug(span, "attempted to use *self in deriving definition")
287 let self_expr = cx.expr_deref(span, self_path);