]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/generic/ty.rs
Rollup merge of #30398 - jwworth:pull-request-1450205451, r=sanxiyn
[rust.git] / src / libsyntax_ext / deriving / generic / ty.rs
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.
4 //
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.
10
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.
13
14 pub use self::PtrTy::*;
15 pub use self::Ty::*;
16
17 use syntax::ast;
18 use syntax::ast::{Expr,Generics,Ident};
19 use syntax::ext::base::ExtCtxt;
20 use syntax::ext::build::AstBuilder;
21 use syntax::codemap::{Span,respan};
22 use syntax::owned_slice::OwnedSlice;
23 use syntax::parse::token::special_idents;
24 use syntax::ptr::P;
25
26 /// The types of pointers
27 #[derive(Clone, Eq, PartialEq)]
28 #[allow(dead_code)]
29 pub enum PtrTy<'a> {
30     /// &'lifetime mut
31     Borrowed(Option<&'a str>, ast::Mutability),
32     /// *mut
33     Raw(ast::Mutability),
34 }
35
36 /// A path, e.g. `::std::option::Option::<i32>` (global). Has support
37 /// for type parameters and a lifetime.
38 #[derive(Clone, Eq, PartialEq)]
39 pub struct Path<'a> {
40     pub path: Vec<&'a str> ,
41     pub lifetime: Option<&'a str>,
42     pub params: Vec<Box<Ty<'a>>>,
43     pub global: bool,
44 }
45
46 impl<'a> Path<'a> {
47     pub fn new<'r>(path: Vec<&'r str> ) -> Path<'r> {
48         Path::new_(path, None, Vec::new(), true)
49     }
50     pub fn new_local<'r>(path: &'r str) -> Path<'r> {
51         Path::new_(vec!( path ), None, Vec::new(), false)
52     }
53     pub fn new_<'r>(path: Vec<&'r str> ,
54                     lifetime: Option<&'r str>,
55                     params: Vec<Box<Ty<'r>>>,
56                     global: bool)
57                     -> Path<'r> {
58         Path {
59             path: path,
60             lifetime: lifetime,
61             params: params,
62             global: global
63         }
64     }
65
66     pub fn to_ty(&self,
67                  cx: &ExtCtxt,
68                  span: Span,
69                  self_ty: Ident,
70                  self_generics: &Generics)
71                  -> P<ast::Ty> {
72         cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
73     }
74     pub fn to_path(&self,
75                    cx: &ExtCtxt,
76                    span: Span,
77                    self_ty: Ident,
78                    self_generics: &Generics)
79                    -> ast::Path {
80         let idents = self.path.iter().map(|s| cx.ident_of(*s)).collect();
81         let lt = mk_lifetimes(cx, span, &self.lifetime);
82         let tys = self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect();
83
84         cx.path_all(span, self.global, idents, lt, tys, Vec::new())
85     }
86 }
87
88 /// A type. Supports pointers, Self, and literals
89 #[derive(Clone, Eq, PartialEq)]
90 pub enum Ty<'a> {
91     Self_,
92     /// &/Box/ Ty
93     Ptr(Box<Ty<'a>>, PtrTy<'a>),
94     /// mod::mod::Type<[lifetime], [Params...]>, including a plain type
95     /// parameter, and things like `i32`
96     Literal(Path<'a>),
97     /// includes unit
98     Tuple(Vec<Ty<'a>> )
99 }
100
101 pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
102     Borrowed(None, ast::MutImmutable)
103 }
104 pub fn borrowed<'r>(ty: Box<Ty<'r>>) -> Ty<'r> {
105     Ptr(ty, borrowed_ptrty())
106 }
107
108 pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
109     Some(Some(borrowed_ptrty()))
110 }
111
112 pub fn borrowed_self<'r>() -> Ty<'r> {
113     borrowed(Box::new(Self_))
114 }
115
116 pub fn nil_ty<'r>() -> Ty<'r> {
117     Tuple(Vec::new())
118 }
119
120 fn mk_lifetime(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
121     match *lt {
122         Some(ref s) => Some(cx.lifetime(span, cx.ident_of(*s).name)),
123         None => None
124     }
125 }
126
127 fn mk_lifetimes(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
128     match *lt {
129         Some(ref s) => vec!(cx.lifetime(span, cx.ident_of(*s).name)),
130         None => vec!()
131     }
132 }
133
134 impl<'a> Ty<'a> {
135     pub fn to_ty(&self,
136                  cx: &ExtCtxt,
137                  span: Span,
138                  self_ty: Ident,
139                  self_generics: &Generics)
140                  -> P<ast::Ty> {
141         match *self {
142             Ptr(ref ty, ref ptr) => {
143                 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
144                 match *ptr {
145                     Borrowed(ref lt, mutbl) => {
146                         let lt = mk_lifetime(cx, span, lt);
147                         cx.ty_rptr(span, raw_ty, lt, mutbl)
148                     }
149                     Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl)
150                 }
151             }
152             Literal(ref p) => { p.to_ty(cx, span, self_ty, self_generics) }
153             Self_  => {
154                 cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
155             }
156             Tuple(ref fields) => {
157                 let ty = ast::TyTup(fields.iter()
158                     .map(|f| f.to_ty(cx, span, self_ty, self_generics))
159                     .collect());
160                 cx.ty(span, ty)
161             }
162         }
163     }
164
165     pub fn to_path(&self,
166                    cx: &ExtCtxt,
167                    span: Span,
168                    self_ty: Ident,
169                    self_generics: &Generics)
170                    -> ast::Path {
171         match *self {
172             Self_ => {
173                 let self_params = self_generics.ty_params.map(|ty_param| {
174                     cx.ty_ident(span, ty_param.ident)
175                 });
176                 let lifetimes = self_generics.lifetimes.iter()
177                                                        .map(|d| d.lifetime)
178                                                        .collect();
179
180                 cx.path_all(span, false, vec!(self_ty), lifetimes,
181                             self_params.into_vec(), Vec::new())
182             }
183             Literal(ref p) => {
184                 p.to_path(cx, span, self_ty, self_generics)
185             }
186             Ptr(..) => { cx.span_bug(span, "pointer in a path in generic `derive`") }
187             Tuple(..) => { cx.span_bug(span, "tuple in a path in generic `derive`") }
188         }
189     }
190 }
191
192
193 fn mk_ty_param(cx: &ExtCtxt,
194                span: Span,
195                name: &str,
196                bounds: &[Path],
197                self_ident: Ident,
198                self_generics: &Generics)
199                -> ast::TyParam {
200     let bounds =
201         bounds.iter().map(|b| {
202             let path = b.to_path(cx, span, self_ident, self_generics);
203             cx.typarambound(path)
204         }).collect();
205     cx.typaram(span, cx.ident_of(name), bounds, None)
206 }
207
208 fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>)
209                -> Generics {
210     Generics {
211         lifetimes: lifetimes,
212         ty_params: OwnedSlice::from_vec(ty_params),
213         where_clause: ast::WhereClause {
214             id: ast::DUMMY_NODE_ID,
215             predicates: Vec::new(),
216         },
217     }
218 }
219
220 /// Lifetimes and bounds on type parameters
221 #[derive(Clone)]
222 pub struct LifetimeBounds<'a> {
223     pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
224     pub bounds: Vec<(&'a str, Vec<Path<'a>>)>,
225 }
226
227 impl<'a> LifetimeBounds<'a> {
228     pub fn empty() -> LifetimeBounds<'a> {
229         LifetimeBounds {
230             lifetimes: Vec::new(), bounds: Vec::new()
231         }
232     }
233     pub fn to_generics(&self,
234                        cx: &ExtCtxt,
235                        span: Span,
236                        self_ty: Ident,
237                        self_generics: &Generics)
238                        -> Generics {
239         let lifetimes = self.lifetimes.iter().map(|&(ref lt, ref bounds)| {
240             let bounds =
241                 bounds.iter().map(
242                     |b| cx.lifetime(span, cx.ident_of(*b).name)).collect();
243             cx.lifetime_def(span, cx.ident_of(*lt).name, bounds)
244         }).collect();
245         let ty_params = self.bounds.iter().map(|t| {
246             match *t {
247                 (ref name, ref bounds) => {
248                     mk_ty_param(cx,
249                                 span,
250                                 *name,
251                                 bounds,
252                                 self_ty,
253                                 self_generics)
254                 }
255             }
256         }).collect();
257         mk_generics(lifetimes, ty_params)
258     }
259 }
260
261 pub fn get_explicit_self(cx: &ExtCtxt, span: Span, self_ptr: &Option<PtrTy>)
262     -> (P<Expr>, ast::ExplicitSelf) {
263     // this constructs a fresh `self` path, which will match the fresh `self` binding
264     // created below.
265     let self_path = cx.expr_self(span);
266     match *self_ptr {
267         None => {
268             (self_path, respan(span, ast::SelfValue(special_idents::self_)))
269         }
270         Some(ref ptr) => {
271             let self_ty = respan(
272                 span,
273                 match *ptr {
274                     Borrowed(ref lt, mutbl) => {
275                         let lt = lt.map(|s| cx.lifetime(span, cx.ident_of(s).name));
276                         ast::SelfRegion(lt, mutbl, special_idents::self_)
277                     }
278                     Raw(_) => cx.span_bug(span, "attempted to use *self in deriving definition")
279                 });
280             let self_expr = cx.expr_deref(span, self_path);
281             (self_expr, self_ty)
282         }
283     }
284 }