1 //! HIR for references to types. Paths in these are not yet resolved. They can
2 //! be directly created from an ast::TypeRef, without further queries.
4 use hir_expand::{name::Name, AstId, InFile};
7 use crate::{body::LowerCtx, path::Path};
9 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
16 pub fn from_mutable(mutable: bool) -> Mutability {
24 pub fn as_keyword_for_ref(self) -> &'static str {
26 Mutability::Shared => "",
27 Mutability::Mut => "mut ",
31 pub fn as_keyword_for_ptr(self) -> &'static str {
33 Mutability::Shared => "const ",
34 Mutability::Mut => "mut ",
39 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
46 pub fn from_raw(is_raw: bool) -> Rawness {
55 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
61 /// Converts an `ast::PathType` to a `hir::TraitRef`.
62 pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Option<Self> {
63 // FIXME: Use `Path::from_src`
65 ast::Type::PathType(path) => {
66 path.path().and_then(|it| ctx.lower_path(it)).map(|path| TraitRef { path })
74 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
80 RawPtr(Box<TypeRef>, Mutability),
81 Reference(Box<TypeRef>, Option<LifetimeRef>, Mutability),
82 Array(Box<TypeRef> /*, Expr*/),
84 /// A fn pointer. Last element of the vector is the return type.
85 Fn(Vec<TypeRef>, bool /*varargs*/),
87 ImplTrait(Vec<TypeBound>),
88 DynTrait(Vec<TypeBound>),
89 Macro(AstId<ast::MacroCall>),
93 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
94 pub struct LifetimeRef {
99 pub(crate) fn new_name(name: Name) -> Self {
103 pub(crate) fn new(lifetime: &ast::Lifetime) -> Self {
104 LifetimeRef { name: Name::new_lifetime(lifetime) }
107 pub fn missing() -> LifetimeRef {
108 LifetimeRef { name: Name::missing() }
112 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
115 // ForLifetime(Vec<LifetimeRef>, Path), FIXME ForLifetime
116 Lifetime(LifetimeRef),
121 /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
122 pub fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self {
124 ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
125 ast::Type::TupleType(inner) => {
126 TypeRef::Tuple(inner.fields().map(|it| TypeRef::from_ast(ctx, it)).collect())
128 ast::Type::NeverType(..) => TypeRef::Never,
129 ast::Type::PathType(inner) => {
130 // FIXME: Use `Path::from_src`
133 .and_then(|it| ctx.lower_path(it))
135 .unwrap_or(TypeRef::Error)
137 ast::Type::PtrType(inner) => {
138 let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
139 let mutability = Mutability::from_mutable(inner.mut_token().is_some());
140 TypeRef::RawPtr(Box::new(inner_ty), mutability)
142 ast::Type::ArrayType(inner) => {
143 TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())))
145 ast::Type::SliceType(inner) => {
146 TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())))
148 ast::Type::RefType(inner) => {
149 let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
150 let lifetime = inner.lifetime().map(|lt| LifetimeRef::new(<));
151 let mutability = Mutability::from_mutable(inner.mut_token().is_some());
152 TypeRef::Reference(Box::new(inner_ty), lifetime, mutability)
154 ast::Type::InferType(_inner) => TypeRef::Placeholder,
155 ast::Type::FnPtrType(inner) => {
158 .and_then(|rt| rt.ty())
159 .map(|it| TypeRef::from_ast(ctx, it))
160 .unwrap_or_else(|| TypeRef::Tuple(Vec::new()));
161 let mut is_varargs = false;
162 let mut params = if let Some(pl) = inner.param_list() {
163 if let Some(param) = pl.params().last() {
164 is_varargs = param.dotdotdot_token().is_some();
167 pl.params().map(|p| p.ty()).map(|it| TypeRef::from_ast_opt(&ctx, it)).collect()
172 TypeRef::Fn(params, is_varargs)
174 // for types are close enough for our purposes to the inner type for now...
175 ast::Type::ForType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
176 ast::Type::ImplTraitType(inner) => {
177 TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
179 ast::Type::DynTraitType(inner) => {
180 TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
182 ast::Type::MacroType(mt) => match mt.macro_call() {
185 .map(|mc| TypeRef::Macro(InFile::new(ctx.file_id(), mc)))
186 .unwrap_or(TypeRef::Error),
187 None => TypeRef::Error,
192 pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self {
193 if let Some(node) = node {
194 TypeRef::from_ast(ctx, node)
200 pub(crate) fn unit() -> TypeRef {
201 TypeRef::Tuple(Vec::new())
204 pub fn walk(&self, f: &mut impl FnMut(&TypeRef)) {
207 fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) {
210 TypeRef::Fn(types, _) | TypeRef::Tuple(types) => {
211 types.iter().for_each(|t| go(t, f))
213 TypeRef::RawPtr(type_ref, _)
214 | TypeRef::Reference(type_ref, ..)
215 | TypeRef::Array(type_ref)
216 | TypeRef::Slice(type_ref) => go(&type_ref, f),
217 TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => {
218 for bound in bounds {
220 TypeBound::Path(path) => go_path(path, f),
221 TypeBound::Lifetime(_) | TypeBound::Error => (),
225 TypeRef::Path(path) => go_path(path, f),
226 TypeRef::Never | TypeRef::Placeholder | TypeRef::Macro(_) | TypeRef::Error => {}
230 fn go_path(path: &Path, f: &mut impl FnMut(&TypeRef)) {
231 if let Some(type_ref) = path.type_anchor() {
234 for segment in path.segments().iter() {
235 if let Some(args_and_bindings) = segment.args_and_bindings {
236 for arg in &args_and_bindings.args {
238 crate::path::GenericArg::Type(type_ref) => {
241 crate::path::GenericArg::Lifetime(_) => {}
244 for binding in &args_and_bindings.bindings {
245 if let Some(type_ref) = &binding.type_ref {
248 for bound in &binding.bounds {
250 TypeBound::Path(path) => go_path(path, f),
251 TypeBound::Lifetime(_) | TypeBound::Error => (),
261 pub(crate) fn type_bounds_from_ast(
262 lower_ctx: &LowerCtx,
263 type_bounds_opt: Option<ast::TypeBoundList>,
264 ) -> Vec<TypeBound> {
265 if let Some(type_bounds) = type_bounds_opt {
266 type_bounds.bounds().map(|it| TypeBound::from_ast(lower_ctx, it)).collect()
273 pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::TypeBound) -> Self {
275 ast::TypeBoundKind::PathType(path_type) => {
276 let path = match path_type.path() {
278 None => return TypeBound::Error,
281 let path = match ctx.lower_path(path) {
283 None => return TypeBound::Error,
285 TypeBound::Path(path)
287 ast::TypeBoundKind::ForType(_) => TypeBound::Error, // FIXME ForType
288 ast::TypeBoundKind::Lifetime(lifetime) => {
289 TypeBound::Lifetime(LifetimeRef::new(&lifetime))
294 pub fn as_path(&self) -> Option<&Path> {
296 TypeBound::Path(p) => Some(p),