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),
24 /// A path, e.g., `::std::option::Option::<i32>` (global). Has support
25 /// for type parameters and a lifetime.
29 lifetime: Option<&'a str>,
30 params: Vec<Box<Ty<'a>>>,
42 pub fn new(path: Vec<&str>) -> Path<'_> {
43 Path::new_(path, None, Vec::new(), PathKind::Std)
45 pub fn new_local(path: &str) -> Path<'_> {
46 Path::new_(vec![path], None, Vec::new(), PathKind::Local)
48 pub fn new_<'r>(path: Vec<&'r str>,
49 lifetime: Option<&'r str>,
50 params: Vec<Box<Ty<'r>>>,
65 self_generics: &Generics)
67 cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
73 self_generics: &Generics)
75 let mut idents = self.path.iter().map(|s| cx.ident_of(*s)).collect();
76 let lt = mk_lifetimes(cx, span, &self.lifetime);
77 let tys: Vec<P<ast::Ty>> =
78 self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect();
79 let params = lt.into_iter()
80 .map(|lt| GenericArg::Lifetime(lt))
81 .chain(tys.into_iter().map(|ty| GenericArg::Type(ty)))
85 PathKind::Global => cx.path_all(span, true, idents, params, Vec::new()),
86 PathKind::Local => cx.path_all(span, false, idents, params, Vec::new()),
88 let def_site = DUMMY_SP.apply_mark(cx.current_expansion.id);
89 idents.insert(0, Ident::new(kw::DollarCrate, def_site));
90 cx.path_all(span, false, idents, params, Vec::new())
97 /// A type. Supports pointers, Self, and literals.
102 Ptr(Box<Ty<'a>>, PtrTy<'a>),
103 /// mod::mod::Type<[lifetime], [Params...]>, including a plain type
104 /// parameter, and things like `i32`
110 /// A const expression. Supports literals and blocks.
111 #[derive(Clone, Eq, PartialEq)]
117 pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
118 Borrowed(None, ast::Mutability::Immutable)
120 pub fn borrowed(ty: Box<Ty<'_>>) -> Ty<'_> {
121 Ptr(ty, borrowed_ptrty())
124 pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
125 Some(Some(borrowed_ptrty()))
128 pub fn borrowed_self<'r>() -> Ty<'r> {
129 borrowed(Box::new(Self_))
132 pub fn nil_ty<'r>() -> Ty<'r> {
136 fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
138 cx.lifetime(span, Ident::from_str(s))
142 fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
143 mk_lifetime(cx, span, lt).into_iter().collect()
151 self_generics: &Generics)
154 Ptr(ref ty, ref ptr) => {
155 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
157 Borrowed(ref lt, mutbl) => {
158 let lt = mk_lifetime(cx, span, lt);
159 cx.ty_rptr(span, raw_ty, lt, mutbl)
161 Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
164 Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
165 Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
166 Tuple(ref fields) => {
167 let ty = ast::TyKind::Tup(fields.iter()
168 .map(|f| f.to_ty(cx, span, self_ty, self_generics))
175 pub fn to_path(&self,
183 let params: Vec<_> = generics.params.iter().map(|param| match param.kind {
184 GenericParamKind::Lifetime { .. } => {
185 GenericArg::Lifetime(ast::Lifetime { id: param.id, ident: param.ident })
187 GenericParamKind::Type { .. } => {
188 GenericArg::Type(cx.ty_ident(span, param.ident))
190 GenericParamKind::Const { .. } => {
191 GenericArg::Const(cx.const_ident(span, param.ident))
195 cx.path_all(span, false, vec![self_ty], params, vec![])
197 Literal(ref p) => p.to_path(cx, span, self_ty, generics),
198 Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
199 Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
205 fn mk_ty_param(cx: &ExtCtxt<'_>,
208 attrs: &[ast::Attribute],
211 self_generics: &Generics)
212 -> ast::GenericParam {
213 let bounds = bounds.iter()
215 let path = b.to_path(cx, span, self_ident, self_generics);
219 cx.typaram(span, cx.ident_of(name), attrs.to_owned(), bounds, None)
222 fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
225 where_clause: ast::WhereClause {
226 predicates: Vec::new(),
233 /// Lifetimes and bounds on type parameters
235 pub struct LifetimeBounds<'a> {
236 pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
237 pub bounds: Vec<(&'a str, Vec<Path<'a>>)>,
240 impl<'a> LifetimeBounds<'a> {
241 pub fn empty() -> LifetimeBounds<'a> {
243 lifetimes: Vec::new(),
247 pub fn to_generics(&self,
251 self_generics: &Generics)
253 let generic_params = self.lifetimes
255 .map(|&(lt, ref bounds)| {
256 let bounds = bounds.iter()
257 .map(|b| ast::GenericBound::Outlives(cx.lifetime(span, Ident::from_str(b))));
258 cx.lifetime_def(span, Ident::from_str(lt), vec![], bounds.collect())
263 let (name, ref bounds) = *t;
264 mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics)
269 mk_generics(generic_params, span)
273 pub fn get_explicit_self(cx: &ExtCtxt<'_>,
275 self_ptr: &Option<PtrTy<'_>>)
276 -> (P<Expr>, ast::ExplicitSelf) {
277 // this constructs a fresh `self` path
278 let self_path = cx.expr_self(span);
280 None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Immutable))),
285 Borrowed(ref lt, mutbl) => {
286 let lt = lt.map(|s| cx.lifetime(span, Ident::from_str(s)));
287 SelfKind::Region(lt, mutbl)
290 cx.span_bug(span, "attempted to use *self in deriving definition")
293 let self_expr = cx.expr_deref(span, self_path);