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.
8 use rustc_ast::{self as ast, Expr, GenericArg, GenericParamKind, Generics, SelfKind};
9 use rustc_expand::base::ExtCtxt;
10 use rustc_span::source_map::{respan, DUMMY_SP};
11 use rustc_span::symbol::{kw, Ident, Symbol};
14 /// The types of pointers
18 Borrowed(Option<Ident>, ast::Mutability),
24 /// A path, e.g., `::std::option::Option::<i32>` (global). Has support
25 /// for type parameters and a lifetime.
29 lifetime: Option<Ident>,
42 pub fn new(path: Vec<Symbol>) -> Path {
43 Path::new_(path, None, Vec::new(), PathKind::Std)
45 pub fn new_local(path: Symbol) -> Path {
46 Path::new_(vec![path], None, Vec::new(), PathKind::Local)
50 lifetime: Option<Ident>,
54 Path { path, lifetime, params, kind }
62 self_generics: &Generics,
64 cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
71 self_generics: &Generics,
73 let mut idents = self.path.iter().map(|s| Ident::new(*s, span)).collect();
74 let lt = mk_lifetimes(cx, span, &self.lifetime);
75 let tys = self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics));
77 lt.into_iter().map(GenericArg::Lifetime).chain(tys.map(GenericArg::Type)).collect();
80 PathKind::Global => cx.path_all(span, true, idents, params),
81 PathKind::Local => cx.path_all(span, false, idents, params),
83 let def_site = cx.with_def_site_ctxt(DUMMY_SP);
84 idents.insert(0, Ident::new(kw::DollarCrate, def_site));
85 cx.path_all(span, false, idents, params)
91 /// A type. Supports pointers, Self, and literals.
97 /// `mod::mod::Type<[lifetime], [Params...]>`, including a plain type
98 /// parameter, and things like `i32`
104 pub fn borrowed_ptrty() -> PtrTy {
105 Borrowed(None, ast::Mutability::Not)
107 pub fn borrowed(ty: Box<Ty>) -> Ty {
108 Ptr(ty, borrowed_ptrty())
111 pub fn borrowed_explicit_self() -> Option<Option<PtrTy>> {
112 Some(Some(borrowed_ptrty()))
115 pub fn borrowed_self() -> Ty {
116 borrowed(Box::new(Self_))
119 pub fn nil_ty() -> Ty {
123 fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Option<ast::Lifetime> {
124 lt.map(|ident| cx.lifetime(span, ident))
127 fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Vec<ast::Lifetime> {
128 mk_lifetime(cx, span, lt).into_iter().collect()
137 self_generics: &Generics,
140 Ptr(ref ty, ref ptr) => {
141 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
143 Borrowed(ref lt, mutbl) => {
144 let lt = mk_lifetime(cx, span, lt);
145 cx.ty_rptr(span, raw_ty, lt, mutbl)
147 Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
150 Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
151 Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
152 Tuple(ref fields) => {
153 let ty = ast::TyKind::Tup(
154 fields.iter().map(|f| f.to_ty(cx, span, self_ty, self_generics)).collect(),
170 let params: Vec<_> = generics
173 .map(|param| match param.kind {
174 GenericParamKind::Lifetime { .. } => {
175 GenericArg::Lifetime(ast::Lifetime { id: param.id, ident: param.ident })
177 GenericParamKind::Type { .. } => {
178 GenericArg::Type(cx.ty_ident(span, param.ident))
180 GenericParamKind::Const { .. } => {
181 GenericArg::Const(cx.const_ident(span, param.ident))
186 cx.path_all(span, false, vec![self_ty], params)
188 Literal(ref p) => p.to_path(cx, span, self_ty, generics),
189 Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
190 Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
199 attrs: &[ast::Attribute],
202 self_generics: &Generics,
203 ) -> ast::GenericParam {
207 let path = b.to_path(cx, span, self_ident, self_generics);
211 cx.typaram(span, Ident::new(name, span), attrs.to_owned(), bounds, None)
214 fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
217 where_clause: ast::WhereClause { has_where_token: false, predicates: Vec::new(), span },
222 /// Bounds on type parameters.
225 pub bounds: Vec<(Symbol, Vec<Path>)>,
229 pub fn empty() -> Bounds {
230 Bounds { bounds: Vec::new() }
237 self_generics: &Generics,
239 let generic_params = self
243 let (name, ref bounds) = *t;
244 mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics)
248 mk_generics(generic_params, span)
252 pub fn get_explicit_self(
255 self_ptr: &Option<PtrTy>,
256 ) -> (P<Expr>, ast::ExplicitSelf) {
257 // this constructs a fresh `self` path
258 let self_path = cx.expr_self(span);
260 None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Not))),
262 let self_ty = respan(
265 Borrowed(ref lt, mutbl) => {
266 let lt = lt.map(|s| cx.lifetime(span, s));
267 SelfKind::Region(lt, mutbl)
269 Raw(_) => cx.span_bug(span, "attempted to use *self in deriving definition"),
272 let self_expr = cx.expr_deref(span, self_path);