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::source_map::{respan, DUMMY_SP};
12 use syntax_pos::symbol::kw;
14 /// The types of pointers
18 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 pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
111 Borrowed(None, ast::Mutability::Immutable)
113 pub fn borrowed(ty: Box<Ty<'_>>) -> Ty<'_> {
114 Ptr(ty, borrowed_ptrty())
117 pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
118 Some(Some(borrowed_ptrty()))
121 pub fn borrowed_self<'r>() -> Ty<'r> {
122 borrowed(Box::new(Self_))
125 pub fn nil_ty<'r>() -> Ty<'r> {
129 fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
131 cx.lifetime(span, Ident::from_str(s))
135 fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
136 mk_lifetime(cx, span, lt).into_iter().collect()
144 self_generics: &Generics)
147 Ptr(ref ty, ref ptr) => {
148 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
150 Borrowed(ref lt, mutbl) => {
151 let lt = mk_lifetime(cx, span, lt);
152 cx.ty_rptr(span, raw_ty, lt, mutbl)
154 Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
157 Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
158 Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
159 Tuple(ref fields) => {
160 let ty = ast::TyKind::Tup(fields.iter()
161 .map(|f| f.to_ty(cx, span, self_ty, self_generics))
168 pub fn to_path(&self,
176 let params: Vec<_> = generics.params.iter().map(|param| match param.kind {
177 GenericParamKind::Lifetime { .. } => {
178 GenericArg::Lifetime(ast::Lifetime { id: param.id, ident: param.ident })
180 GenericParamKind::Type { .. } => {
181 GenericArg::Type(cx.ty_ident(span, param.ident))
183 GenericParamKind::Const { .. } => {
184 GenericArg::Const(cx.const_ident(span, param.ident))
188 cx.path_all(span, false, vec![self_ty], params, vec![])
190 Literal(ref p) => p.to_path(cx, span, self_ty, generics),
191 Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
192 Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
198 fn mk_ty_param(cx: &ExtCtxt<'_>,
201 attrs: &[ast::Attribute],
204 self_generics: &Generics)
205 -> ast::GenericParam {
206 let bounds = bounds.iter()
208 let path = b.to_path(cx, span, self_ident, self_generics);
212 cx.typaram(span, cx.ident_of(name), attrs.to_owned(), bounds, None)
215 fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
218 where_clause: ast::WhereClause {
219 predicates: Vec::new(),
226 /// Lifetimes and bounds on type parameters
228 pub struct LifetimeBounds<'a> {
229 pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
230 pub bounds: Vec<(&'a str, Vec<Path<'a>>)>,
233 impl<'a> LifetimeBounds<'a> {
234 pub fn empty() -> LifetimeBounds<'a> {
236 lifetimes: Vec::new(),
240 pub fn to_generics(&self,
244 self_generics: &Generics)
246 let generic_params = self.lifetimes
248 .map(|&(lt, ref bounds)| {
249 let bounds = bounds.iter()
250 .map(|b| ast::GenericBound::Outlives(cx.lifetime(span, Ident::from_str(b))));
251 cx.lifetime_def(span, Ident::from_str(lt), vec![], bounds.collect())
256 let (name, ref bounds) = *t;
257 mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics)
262 mk_generics(generic_params, span)
266 pub fn get_explicit_self(cx: &ExtCtxt<'_>,
268 self_ptr: &Option<PtrTy<'_>>)
269 -> (P<Expr>, ast::ExplicitSelf) {
270 // this constructs a fresh `self` path
271 let self_path = cx.expr_self(span);
273 None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Immutable))),
278 Borrowed(ref lt, mutbl) => {
279 let lt = lt.map(|s| cx.lifetime(span, Ident::from_str(s)));
280 SelfKind::Region(lt, mutbl)
283 cx.span_bug(span, "attempted to use *self in deriving definition")
286 let self_expr = cx.expr_deref(span, self_path);