1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A mini version of ast::Ty, which is easier to use, and features an explicit `Self` type to use
12 //! when specifying impls to be derived.
14 pub use self::PtrTy::*;
18 use syntax::ast::{Expr, GenericParamKind, Generics, Ident, SelfKind, GenericArg};
19 use syntax::ext::base::ExtCtxt;
20 use syntax::ext::build::AstBuilder;
21 use syntax::source_map::{respan, DUMMY_SP};
24 use syntax_pos::symbol::keywords;
26 /// The types of pointers
30 Borrowed(Option<&'a str>, ast::Mutability),
35 /// A path, e.g. `::std::option::Option::<i32>` (global). Has support
36 /// for type parameters and a lifetime.
40 lifetime: Option<&'a str>,
41 params: Vec<Box<Ty<'a>>>,
53 pub fn new<'r>(path: Vec<&'r str>) -> Path<'r> {
54 Path::new_(path, None, Vec::new(), PathKind::Std)
56 pub fn new_local<'r>(path: &'r str) -> Path<'r> {
57 Path::new_(vec![path], None, Vec::new(), PathKind::Local)
59 pub fn new_<'r>(path: Vec<&'r str>,
60 lifetime: Option<&'r str>,
61 params: Vec<Box<Ty<'r>>>,
76 self_generics: &Generics)
78 cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
84 self_generics: &Generics)
86 let mut idents = self.path.iter().map(|s| cx.ident_of(*s)).collect();
87 let lt = mk_lifetimes(cx, span, &self.lifetime);
88 let tys: Vec<P<ast::Ty>> =
89 self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect();
90 let params = lt.into_iter()
91 .map(|lt| GenericArg::Lifetime(lt))
92 .chain(tys.into_iter().map(|ty| GenericArg::Type(ty)))
96 PathKind::Global => cx.path_all(span, true, idents, params, Vec::new()),
97 PathKind::Local => cx.path_all(span, false, idents, params, Vec::new()),
99 let def_site = DUMMY_SP.apply_mark(cx.current_expansion.mark);
100 idents.insert(0, Ident::new(keywords::DollarCrate.name(), def_site));
101 cx.path_all(span, false, idents, params, Vec::new())
108 /// A type. Supports pointers, Self, and literals
113 Ptr(Box<Ty<'a>>, PtrTy<'a>),
114 /// mod::mod::Type<[lifetime], [Params...]>, including a plain type
115 /// parameter, and things like `i32`
121 pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
122 Borrowed(None, ast::Mutability::Immutable)
124 pub fn borrowed<'r>(ty: Box<Ty<'r>>) -> Ty<'r> {
125 Ptr(ty, borrowed_ptrty())
128 pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
129 Some(Some(borrowed_ptrty()))
132 pub fn borrowed_self<'r>() -> Ty<'r> {
133 borrowed(Box::new(Self_))
136 pub fn nil_ty<'r>() -> Ty<'r> {
140 fn mk_lifetime(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
142 cx.lifetime(span, Ident::from_str(s))
146 fn mk_lifetimes(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
147 mk_lifetime(cx, span, lt).into_iter().collect()
155 self_generics: &Generics)
158 Ptr(ref ty, ref ptr) => {
159 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
161 Borrowed(ref lt, mutbl) => {
162 let lt = mk_lifetime(cx, span, lt);
163 cx.ty_rptr(span, raw_ty, lt, mutbl)
165 Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
168 Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
169 Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
170 Tuple(ref fields) => {
171 let ty = ast::TyKind::Tup(fields.iter()
172 .map(|f| f.to_ty(cx, span, self_ty, self_generics))
179 pub fn to_path(&self,
187 let params: Vec<_> = generics.params.iter().map(|param| match param.kind {
188 GenericParamKind::Lifetime { .. } => {
189 GenericArg::Lifetime(ast::Lifetime { id: param.id, ident: param.ident })
191 GenericParamKind::Type { .. } => {
192 GenericArg::Type(cx.ty_ident(span, param.ident))
196 cx.path_all(span, false, vec![self_ty], params, vec![])
198 Literal(ref p) => p.to_path(cx, span, self_ty, generics),
199 Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
200 Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
206 fn mk_ty_param(cx: &ExtCtxt,
209 attrs: &[ast::Attribute],
212 self_generics: &Generics)
213 -> ast::GenericParam {
214 let bounds = bounds.iter()
216 let path = b.to_path(cx, span, self_ident, self_generics);
220 cx.typaram(span, cx.ident_of(name), attrs.to_owned(), bounds, None)
223 fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
226 where_clause: ast::WhereClause {
227 id: ast::DUMMY_NODE_ID,
228 predicates: Vec::new(),
235 /// Lifetimes and bounds on type parameters
237 pub struct LifetimeBounds<'a> {
238 pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
239 pub bounds: Vec<(&'a str, Vec<Path<'a>>)>,
242 impl<'a> LifetimeBounds<'a> {
243 pub fn empty() -> LifetimeBounds<'a> {
245 lifetimes: Vec::new(),
249 pub fn to_generics(&self,
253 self_generics: &Generics)
255 let generic_params = self.lifetimes
257 .map(|&(lt, ref bounds)| {
258 let bounds = bounds.iter()
259 .map(|b| ast::GenericBound::Outlives(cx.lifetime(span, Ident::from_str(b))));
260 cx.lifetime_def(span, Ident::from_str(lt), vec![], bounds.collect())
265 let (name, ref bounds) = *t;
266 mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics)
271 mk_generics(generic_params, span)
275 pub fn get_explicit_self(cx: &ExtCtxt,
277 self_ptr: &Option<PtrTy>)
278 -> (P<Expr>, ast::ExplicitSelf) {
279 // this constructs a fresh `self` path
280 let self_path = cx.expr_self(span);
282 None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Immutable))),
287 Borrowed(ref lt, mutbl) => {
288 let lt = lt.map(|s| cx.lifetime(span, Ident::from_str(s)));
289 SelfKind::Region(lt, mutbl)
292 cx.span_bug(span, "attempted to use *self in deriving definition")
295 let self_expr = cx.expr_deref(span, self_path);