1 //! AST -> `ItemTree` lowering code.
3 use std::{collections::hash_map::Entry, mem, sync::Arc};
5 use arena::map::ArenaMap;
6 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId};
7 use smallvec::SmallVec;
9 ast::{self, ModuleItemOwner},
15 generics::{GenericParams, TypeParamData, TypeParamProvenance},
20 fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
21 FileItemTreeId { index, _p: PhantomData }
24 struct ModItems(SmallVec<[ModItem; 1]>);
26 impl<T> From<T> for ModItems
30 fn from(t: T) -> Self {
31 ModItems(SmallVec::from_buf([t.into(); 1]))
35 pub(super) struct Ctx {
39 source_ast_id_map: Arc<AstIdMap>,
40 body_ctx: crate::body::LowerCtx,
41 inner_items: Vec<ModItem>,
42 forced_visibility: Option<RawVisibilityId>,
46 pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
48 tree: ItemTree::empty(),
51 source_ast_id_map: db.ast_id_map(file),
52 body_ctx: crate::body::LowerCtx::new(db, file),
53 inner_items: Vec::new(),
54 forced_visibility: None,
58 pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
59 self.tree.top_level = item_owner
61 .flat_map(|item| self.lower_mod_item(&item, false))
62 .flat_map(|items| items.0)
67 pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
68 self.collect_inner_items(within);
72 fn data(&mut self) -> &mut ItemTreeData {
76 fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItems> {
77 assert!(inner || self.inner_items.is_empty());
79 // Collect inner items for 1-to-1-lowered items.
85 | ast::Item::TypeAlias(_)
87 | ast::Item::Static(_)
88 | ast::Item::MacroCall(_) => {
89 // Skip this if we're already collecting inner items. We'll descend into all nodes
92 self.collect_inner_items(item.syntax());
96 // These are handled in their respective `lower_X` method (since we can't just blindly
98 ast::Item::Trait(_) | ast::Item::Impl(_) | ast::Item::ExternBlock(_) => {}
100 // These don't have inner items.
101 ast::Item::Module(_) | ast::Item::ExternCrate(_) | ast::Item::Use(_) => {}
104 let attrs = Attrs::new(item, &self.hygiene);
105 let items = match item {
106 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
107 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
108 ast::Item::Enum(ast) => self.lower_enum(ast).map(Into::into),
109 ast::Item::Fn(ast) => self.lower_function(ast).map(Into::into),
110 ast::Item::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
111 ast::Item::Static(ast) => self.lower_static(ast).map(Into::into),
112 ast::Item::Const(ast) => Some(self.lower_const(ast).into()),
113 ast::Item::Module(ast) => self.lower_module(ast).map(Into::into),
114 ast::Item::Trait(ast) => self.lower_trait(ast).map(Into::into),
115 ast::Item::Impl(ast) => self.lower_impl(ast).map(Into::into),
116 ast::Item::Use(ast) => Some(ModItems(
117 self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(),
119 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
120 ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
121 ast::Item::ExternBlock(ast) => {
122 Some(ModItems(self.lower_extern_block(ast).into_iter().collect::<SmallVec<_>>()))
126 if !attrs.is_empty() {
127 for item in items.iter().flat_map(|items| &items.0) {
128 self.add_attrs((*item).into(), attrs.clone());
135 fn add_attrs(&mut self, item: AttrOwner, attrs: Attrs) {
136 match self.tree.attrs.entry(item) {
137 Entry::Occupied(mut entry) => {
138 *entry.get_mut() = entry.get().merge(attrs);
140 Entry::Vacant(entry) => {
146 fn collect_inner_items(&mut self, container: &SyntaxNode) {
147 let forced_vis = self.forced_visibility.take();
148 let mut inner_items = mem::take(&mut self.tree.inner_items);
149 inner_items.extend(container.descendants().skip(1).filter_map(ast::Item::cast).filter_map(
151 let ast_id = self.source_ast_id_map.ast_id(&item);
152 Some((ast_id, self.lower_mod_item(&item, true)?.0))
155 self.tree.inner_items = inner_items;
156 self.forced_visibility = forced_vis;
159 fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem> {
161 ast::AssocItem::Fn(ast) => self.lower_function(ast).map(Into::into),
162 ast::AssocItem::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
163 ast::AssocItem::Const(ast) => Some(self.lower_const(ast).into()),
164 ast::AssocItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
168 fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
169 let visibility = self.lower_visibility(strukt);
170 let name = strukt.name()?.as_name();
171 let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
172 let fields = self.lower_fields(&strukt.kind());
173 let ast_id = self.source_ast_id_map.ast_id(strukt);
174 let kind = match strukt.kind() {
175 ast::StructKind::Record(_) => StructDefKind::Record,
176 ast::StructKind::Tuple(_) => StructDefKind::Tuple,
177 ast::StructKind::Unit => StructDefKind::Unit,
179 let res = Struct { name, visibility, generic_params, fields, ast_id, kind };
180 Some(id(self.data().structs.alloc(res)))
183 fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
185 ast::StructKind::Record(it) => {
186 let range = self.lower_record_fields(it);
187 Fields::Record(range)
189 ast::StructKind::Tuple(it) => {
190 let range = self.lower_tuple_fields(it);
193 ast::StructKind::Unit => Fields::Unit,
197 fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdRange<Field> {
198 let start = self.next_field_idx();
199 for field in fields.fields() {
200 if let Some(data) = self.lower_record_field(&field) {
201 let idx = self.data().fields.alloc(data);
202 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene));
205 let end = self.next_field_idx();
206 IdRange::new(start..end)
209 fn lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field> {
210 let name = field.name()?.as_name();
211 let visibility = self.lower_visibility(field);
212 let type_ref = self.lower_type_ref_opt(field.ty());
213 let res = Field { name, type_ref, visibility };
217 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdRange<Field> {
218 let start = self.next_field_idx();
219 for (i, field) in fields.fields().enumerate() {
220 let data = self.lower_tuple_field(i, &field);
221 let idx = self.data().fields.alloc(data);
222 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene));
224 let end = self.next_field_idx();
225 IdRange::new(start..end)
228 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
229 let name = Name::new_tuple_field(idx);
230 let visibility = self.lower_visibility(field);
231 let type_ref = self.lower_type_ref_opt(field.ty());
232 let res = Field { name, type_ref, visibility };
236 fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
237 let visibility = self.lower_visibility(union);
238 let name = union.name()?.as_name();
239 let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
240 let fields = match union.record_field_list() {
241 Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
242 None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())),
244 let ast_id = self.source_ast_id_map.ast_id(union);
245 let res = Union { name, visibility, generic_params, fields, ast_id };
246 Some(id(self.data().unions.alloc(res)))
249 fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
250 let visibility = self.lower_visibility(enum_);
251 let name = enum_.name()?.as_name();
252 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
253 let variants = match &enum_.variant_list() {
254 Some(variant_list) => self.lower_variants(variant_list),
255 None => IdRange::new(self.next_variant_idx()..self.next_variant_idx()),
257 let ast_id = self.source_ast_id_map.ast_id(enum_);
258 let res = Enum { name, visibility, generic_params, variants, ast_id };
259 Some(id(self.data().enums.alloc(res)))
262 fn lower_variants(&mut self, variants: &ast::VariantList) -> IdRange<Variant> {
263 let start = self.next_variant_idx();
264 for variant in variants.variants() {
265 if let Some(data) = self.lower_variant(&variant) {
266 let idx = self.data().variants.alloc(data);
267 self.add_attrs(idx.into(), Attrs::new(&variant, &self.hygiene));
270 let end = self.next_variant_idx();
271 IdRange::new(start..end)
274 fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
275 let name = variant.name()?.as_name();
276 let fields = self.lower_fields(&variant.kind());
277 let res = Variant { name, fields };
281 fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
282 let visibility = self.lower_visibility(func);
283 let name = func.name()?.as_name();
285 let mut params = Vec::new();
286 let mut param_names = Vec::new();
287 let mut has_self_param = false;
288 if let Some(param_list) = func.param_list() {
289 if let Some(self_param) = param_list.self_param() {
290 let self_type = match self_param.ty() {
291 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
293 let self_type = TypeRef::Path(name![Self].into());
294 match self_param.kind() {
295 ast::SelfParamKind::Owned => self_type,
296 ast::SelfParamKind::Ref => {
297 TypeRef::Reference(Box::new(self_type), Mutability::Shared)
299 ast::SelfParamKind::MutRef => {
300 TypeRef::Reference(Box::new(self_type), Mutability::Mut)
305 params.push(self_type);
306 has_self_param = true;
308 for param in param_list.params() {
309 let param_name = param
312 if let ast::Pat::IdentPat(ident) = name {
313 Some(ident.name()?.as_name())
319 param_names.push(param_name);
321 let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
322 params.push(type_ref);
326 let mut is_varargs = false;
327 if let Some(params) = func.param_list() {
328 if let Some(last) = params.params().last() {
329 is_varargs = last.dotdotdot_token().is_some();
333 let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
334 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
335 _ => TypeRef::unit(),
338 let ret_type = if func.async_token().is_some() {
339 let future_impl = desugar_future_path(ret_type);
340 let ty_bound = TypeBound::Path(future_impl);
341 TypeRef::ImplTrait(vec![ty_bound])
346 let has_body = func.body().is_some();
348 let ast_id = self.source_ast_id_map.ast_id(func);
349 let mut res = Function {
352 generic_params: GenericParamsId::EMPTY,
355 is_unsafe: func.unsafe_token().is_some(),
356 params: params.into_boxed_slice(),
357 param_names: param_names.into_boxed_slice(),
362 res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
364 Some(id(self.data().functions.alloc(res)))
369 type_alias: &ast::TypeAlias,
370 ) -> Option<FileItemTreeId<TypeAlias>> {
371 let name = type_alias.name()?.as_name();
372 let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
373 let visibility = self.lower_visibility(type_alias);
374 let bounds = self.lower_type_bounds(type_alias);
375 let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
376 let ast_id = self.source_ast_id_map.ast_id(type_alias);
377 let res = TypeAlias {
380 bounds: bounds.into_boxed_slice(),
386 Some(id(self.data().type_aliases.alloc(res)))
389 fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>> {
390 let name = static_.name()?.as_name();
391 let type_ref = self.lower_type_ref_opt(static_.ty());
392 let visibility = self.lower_visibility(static_);
393 let mutable = static_.mut_token().is_some();
394 let ast_id = self.source_ast_id_map.ast_id(static_);
395 let res = Static { name, visibility, mutable, type_ref, ast_id };
396 Some(id(self.data().statics.alloc(res)))
399 fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
400 let name = konst.name().map(|it| it.as_name());
401 let type_ref = self.lower_type_ref_opt(konst.ty());
402 let visibility = self.lower_visibility(konst);
403 let ast_id = self.source_ast_id_map.ast_id(konst);
404 let res = Const { name, visibility, type_ref, ast_id };
405 id(self.data().consts.alloc(res))
408 fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
409 let name = module.name()?.as_name();
410 let visibility = self.lower_visibility(module);
411 let kind = if module.semicolon_token().is_some() {
419 .flat_map(|item| self.lower_mod_item(&item, false))
420 .flat_map(|items| items.0)
424 mark::hit!(name_res_works_for_broken_modules);
425 Box::new([]) as Box<[_]>
429 let ast_id = self.source_ast_id_map.ast_id(module);
430 let res = Mod { name, visibility, kind, ast_id };
431 Some(id(self.data().mods.alloc(res)))
434 fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
435 let name = trait_def.name()?.as_name();
436 let visibility = self.lower_visibility(trait_def);
438 self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
439 let auto = trait_def.auto_token().is_some();
440 let items = trait_def.assoc_item_list().map(|list| {
441 self.with_inherited_visibility(visibility, |this| {
444 let attrs = Attrs::new(&item, &this.hygiene);
445 this.collect_inner_items(item.syntax());
446 this.lower_assoc_item(&item).map(|item| {
447 this.add_attrs(ModItem::from(item).into(), attrs);
454 let ast_id = self.source_ast_id_map.ast_id(trait_def);
460 items: items.unwrap_or_default(),
463 Some(id(self.data().traits.alloc(res)))
466 fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
468 self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
469 let target_trait = impl_def.trait_().map(|tr| self.lower_type_ref(&tr));
470 let target_type = self.lower_type_ref(&impl_def.self_ty()?);
471 let is_negative = impl_def.excl_token().is_some();
473 // We cannot use `assoc_items()` here as that does not include macro calls.
477 .flat_map(|it| it.assoc_items())
479 self.collect_inner_items(item.syntax());
480 let assoc = self.lower_assoc_item(&item)?;
481 let attrs = Attrs::new(&item, &self.hygiene);
482 self.add_attrs(ModItem::from(assoc).into(), attrs);
486 let ast_id = self.source_ast_id_map.ast_id(impl_def);
487 let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id };
488 Some(id(self.data().impls.alloc(res)))
491 fn lower_use(&mut self, use_item: &ast::Use) -> Vec<FileItemTreeId<Import>> {
493 let is_prelude = use_item.has_atom_attr("prelude_import");
494 let visibility = self.lower_visibility(use_item);
495 let ast_id = self.source_ast_id_map.ast_id(use_item);
497 // Every use item can expand to many `Import`s.
498 let mut imports = Vec::new();
499 let tree = self.tree.data_mut();
500 ModPath::expand_use_item(
501 InFile::new(self.file, use_item.clone()),
503 |path, _use_tree, is_glob, alias| {
504 imports.push(id(tree.imports.alloc(Import {
511 index: imports.len(),
519 fn lower_extern_crate(
521 extern_crate: &ast::ExternCrate,
522 ) -> Option<FileItemTreeId<ExternCrate>> {
523 let name = extern_crate.name_ref()?.as_name();
524 let alias = extern_crate.rename().map(|a| {
525 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
527 let visibility = self.lower_visibility(extern_crate);
528 let ast_id = self.source_ast_id_map.ast_id(extern_crate);
530 let is_macro_use = extern_crate.has_atom_attr("macro_use");
532 let res = ExternCrate { name, alias, visibility, is_macro_use, ast_id };
533 Some(id(self.data().extern_crates.alloc(res)))
536 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
537 let name = m.name().map(|it| it.as_name());
538 let attrs = Attrs::new(m, &self.hygiene);
539 let path = ModPath::from_src(m.path()?, &self.hygiene)?;
541 let ast_id = self.source_ast_id_map.ast_id(m);
544 let export_attr = attrs.by_key("macro_export");
546 let is_export = export_attr.exists();
547 let is_local_inner = if is_export {
548 export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
549 tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
550 ident.text.contains("local_inner_macros")
558 let is_builtin = attrs.by_key("rustc_builtin_macro").exists();
559 let res = MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id };
560 Some(id(self.data().macro_calls.alloc(res)))
563 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> {
564 block.extern_item_list().map_or(Vec::new(), |list| {
567 self.collect_inner_items(item.syntax());
568 let attrs = Attrs::new(&item, &self.hygiene);
569 let id: ModItem = match item {
570 ast::ExternItem::Fn(ast) => {
571 let func = self.lower_function(&ast)?;
572 self.data().functions[func.index].is_unsafe = true;
575 ast::ExternItem::Static(ast) => {
576 let statik = self.lower_static(&ast)?;
579 ast::ExternItem::TypeAlias(ty) => {
580 let foreign_ty = self.lower_type_alias(&ty)?;
581 self.data().type_aliases[foreign_ty.index].is_extern = true;
584 ast::ExternItem::MacroCall(_) => return None,
586 self.add_attrs(id.into(), attrs);
593 /// Lowers generics defined on `node` and collects inner items defined within.
594 fn lower_generic_params_and_inner_items(
596 owner: GenericsOwner<'_>,
597 node: &impl ast::GenericParamsOwner,
598 ) -> GenericParamsId {
599 // Generics are part of item headers and may contain inner items we need to collect.
600 if let Some(params) = node.generic_param_list() {
601 self.collect_inner_items(params.syntax());
603 if let Some(clause) = node.where_clause() {
604 self.collect_inner_items(clause.syntax());
607 self.lower_generic_params(owner, node)
610 fn lower_generic_params(
612 owner: GenericsOwner<'_>,
613 node: &impl ast::GenericParamsOwner,
614 ) -> GenericParamsId {
615 let mut sm = &mut ArenaMap::default();
616 let mut generics = GenericParams::default();
618 GenericsOwner::Function(func) => {
619 generics.fill(&self.body_ctx, sm, node);
620 // lower `impl Trait` in arguments
621 for param in &*func.params {
622 generics.fill_implicit_impl_trait_args(param);
625 GenericsOwner::Struct
626 | GenericsOwner::Enum
627 | GenericsOwner::Union
628 | GenericsOwner::TypeAlias => {
629 generics.fill(&self.body_ctx, sm, node);
631 GenericsOwner::Trait(trait_def) => {
632 // traits get the Self type as an implicit first type parameter
633 let self_param_id = generics.types.alloc(TypeParamData {
634 name: Some(name![Self]),
636 provenance: TypeParamProvenance::TraitSelf,
638 sm.insert(self_param_id, Either::Left(trait_def.clone()));
639 // add super traits as bounds on Self
640 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
641 let self_param = TypeRef::Path(name![Self].into());
642 generics.fill_bounds(&self.body_ctx, trait_def, self_param);
644 generics.fill(&self.body_ctx, &mut sm, node);
646 GenericsOwner::Impl => {
647 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
648 // type-parameter, but rather is a type-alias for impl's target
649 // type, so this is handled by the resolver.
650 generics.fill(&self.body_ctx, &mut sm, node);
654 self.data().generics.alloc(generics)
657 fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<TypeBound> {
658 match node.type_bound_list() {
659 Some(bound_list) => {
660 bound_list.bounds().map(|it| TypeBound::from_ast(&self.body_ctx, it)).collect()
666 fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
667 let vis = match self.forced_visibility {
668 Some(vis) => return vis,
669 None => RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene),
672 self.data().vis.alloc(vis)
675 fn lower_type_ref(&self, type_ref: &ast::Type) -> TypeRef {
676 TypeRef::from_ast(&self.body_ctx, type_ref.clone())
678 fn lower_type_ref_opt(&self, type_ref: Option<ast::Type>) -> TypeRef {
679 type_ref.map(|ty| self.lower_type_ref(&ty)).unwrap_or(TypeRef::Error)
682 /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
683 fn with_inherited_visibility<R>(
685 vis: RawVisibilityId,
686 f: impl FnOnce(&mut Self) -> R,
688 let old = mem::replace(&mut self.forced_visibility, Some(vis));
690 self.forced_visibility = old;
694 fn next_field_idx(&self) -> Idx<Field> {
695 Idx::from_raw(RawId::from(
696 self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
699 fn next_variant_idx(&self) -> Idx<Variant> {
700 Idx::from_raw(RawId::from(
701 self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
706 fn desugar_future_path(orig: TypeRef) -> Path {
707 let path = path![core::future::Future];
708 let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect();
709 let mut last = GenericArgs::empty();
711 AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() };
712 last.bindings.push(binding);
713 generic_args.push(Some(Arc::new(last)));
715 Path::from_known_path(path, generic_args)
718 enum GenericsOwner<'a> {
719 /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
721 Function(&'a Function),
725 /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
726 Trait(&'a ast::Trait),