]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/item_tree/lower.rs
Merge #9908
[rust.git] / crates / hir_def / src / item_tree / lower.rs
1 //! AST -> `ItemTree` lowering code.
2
3 use std::{collections::hash_map::Entry, mem, sync::Arc};
4
5 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
6 use syntax::{
7     ast::{self, ModuleItemOwner},
8     SyntaxNode, WalkEvent,
9 };
10
11 use crate::{
12     generics::{GenericParams, TypeParamData, TypeParamProvenance},
13     type_ref::{LifetimeRef, TraitBoundModifier, TraitRef},
14 };
15
16 use super::*;
17
18 fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
19     FileItemTreeId { index, _p: PhantomData }
20 }
21
22 pub(super) struct Ctx<'a> {
23     db: &'a dyn DefDatabase,
24     tree: ItemTree,
25     hygiene: Hygiene,
26     source_ast_id_map: Arc<AstIdMap>,
27     body_ctx: crate::body::LowerCtx<'a>,
28     forced_visibility: Option<RawVisibilityId>,
29 }
30
31 impl<'a> Ctx<'a> {
32     pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
33         Self {
34             db,
35             tree: ItemTree::default(),
36             hygiene,
37             source_ast_id_map: db.ast_id_map(file),
38             body_ctx: crate::body::LowerCtx::new(db, file),
39             forced_visibility: None,
40         }
41     }
42
43     pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
44         self.tree.top_level =
45             item_owner.items().flat_map(|item| self.lower_mod_item(&item, false)).collect();
46         self.tree
47     }
48
49     pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
50         self.tree.top_level = stmts
51             .statements()
52             .filter_map(|stmt| match stmt {
53                 ast::Stmt::Item(item) => Some(item),
54                 // Macro calls can be both items and expressions. The syntax library always treats
55                 // them as expressions here, so we undo that.
56                 ast::Stmt::ExprStmt(es) => match es.expr()? {
57                     ast::Expr::MacroCall(call) => {
58                         cov_mark::hit!(macro_call_in_macro_stmts_is_added_to_item_tree);
59                         Some(call.into())
60                     }
61                     _ => None,
62                 },
63                 _ => None,
64             })
65             .flat_map(|item| self.lower_mod_item(&item, false))
66             .collect();
67
68         // Non-items need to have their inner items collected.
69         for stmt in stmts.statements() {
70             match stmt {
71                 ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => {
72                     self.collect_inner_items(stmt.syntax())
73                 }
74                 _ => {}
75             }
76         }
77         if let Some(expr) = stmts.expr() {
78             self.collect_inner_items(expr.syntax());
79         }
80         self.tree
81     }
82
83     pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
84         self.collect_inner_items(within);
85         self.tree
86     }
87
88     fn data(&mut self) -> &mut ItemTreeData {
89         self.tree.data_mut()
90     }
91
92     fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItem> {
93         // Collect inner items for 1-to-1-lowered items.
94         match item {
95             ast::Item::Struct(_)
96             | ast::Item::Union(_)
97             | ast::Item::Enum(_)
98             | ast::Item::Fn(_)
99             | ast::Item::TypeAlias(_)
100             | ast::Item::Const(_)
101             | ast::Item::Static(_) => {
102                 // Skip this if we're already collecting inner items. We'll descend into all nodes
103                 // already.
104                 if !inner {
105                     self.collect_inner_items(item.syntax());
106                 }
107             }
108
109             // These are handled in their respective `lower_X` method (since we can't just blindly
110             // walk them).
111             ast::Item::Trait(_) | ast::Item::Impl(_) | ast::Item::ExternBlock(_) => {}
112
113             // These don't have inner items.
114             ast::Item::Module(_)
115             | ast::Item::ExternCrate(_)
116             | ast::Item::Use(_)
117             | ast::Item::MacroCall(_)
118             | ast::Item::MacroRules(_)
119             | ast::Item::MacroDef(_) => {}
120         };
121
122         let attrs = RawAttrs::new(self.db, item, &self.hygiene);
123         let item: ModItem = match item {
124             ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
125             ast::Item::Union(ast) => self.lower_union(ast)?.into(),
126             ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
127             ast::Item::Fn(ast) => self.lower_function(ast)?.into(),
128             ast::Item::TypeAlias(ast) => self.lower_type_alias(ast)?.into(),
129             ast::Item::Static(ast) => self.lower_static(ast)?.into(),
130             ast::Item::Const(ast) => self.lower_const(ast).into(),
131             ast::Item::Module(ast) => self.lower_module(ast)?.into(),
132             ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
133             ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
134             ast::Item::Use(ast) => self.lower_use(ast)?.into(),
135             ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
136             ast::Item::MacroCall(ast) => self.lower_macro_call(ast)?.into(),
137             ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
138             ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
139             ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
140         };
141
142         self.add_attrs(item.into(), attrs);
143
144         Some(item)
145     }
146
147     fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
148         match self.tree.attrs.entry(item) {
149             Entry::Occupied(mut entry) => {
150                 *entry.get_mut() = entry.get().merge(attrs);
151             }
152             Entry::Vacant(entry) => {
153                 entry.insert(attrs);
154             }
155         }
156     }
157
158     fn collect_inner_items(&mut self, container: &SyntaxNode) {
159         let forced_vis = self.forced_visibility.take();
160
161         let mut block_stack = Vec::new();
162
163         // if container itself is block, add it to the stack
164         if let Some(block) = ast::BlockExpr::cast(container.clone()) {
165             block_stack.push(self.source_ast_id_map.ast_id(&block));
166         }
167
168         for event in container.preorder().skip(1) {
169             match event {
170                 WalkEvent::Enter(node) => {
171                     match_ast! {
172                         match node {
173                             ast::BlockExpr(block) => {
174                                 block_stack.push(self.source_ast_id_map.ast_id(&block));
175                             },
176                             ast::Item(item) => {
177                                 // FIXME: This triggers for macro calls in expression/pattern/type position
178                                 let mod_item = self.lower_mod_item(&item, true);
179                                 let current_block = block_stack.last();
180                                 if let (Some(mod_item), Some(block)) = (mod_item, current_block) {
181                                         self.data().inner_items.entry(*block).or_default().push(mod_item);
182                                 }
183                             },
184                             _ => {}
185                         }
186                     }
187                 }
188                 WalkEvent::Leave(node) => {
189                     if ast::BlockExpr::cast(node).is_some() {
190                         block_stack.pop();
191                     }
192                 }
193             }
194         }
195
196         self.forced_visibility = forced_vis;
197     }
198
199     fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem> {
200         match item {
201             ast::AssocItem::Fn(ast) => self.lower_function(ast).map(Into::into),
202             ast::AssocItem::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
203             ast::AssocItem::Const(ast) => Some(self.lower_const(ast).into()),
204             ast::AssocItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
205         }
206     }
207
208     fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
209         let visibility = self.lower_visibility(strukt);
210         let name = strukt.name()?.as_name();
211         let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
212         let fields = self.lower_fields(&strukt.kind());
213         let ast_id = self.source_ast_id_map.ast_id(strukt);
214         let res = Struct { name, visibility, generic_params, fields, ast_id };
215         Some(id(self.data().structs.alloc(res)))
216     }
217
218     fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
219         match strukt_kind {
220             ast::StructKind::Record(it) => {
221                 let range = self.lower_record_fields(it);
222                 Fields::Record(range)
223             }
224             ast::StructKind::Tuple(it) => {
225                 let range = self.lower_tuple_fields(it);
226                 Fields::Tuple(range)
227             }
228             ast::StructKind::Unit => Fields::Unit,
229         }
230     }
231
232     fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdRange<Field> {
233         let start = self.next_field_idx();
234         for field in fields.fields() {
235             if let Some(data) = self.lower_record_field(&field) {
236                 let idx = self.data().fields.alloc(data);
237                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
238             }
239         }
240         let end = self.next_field_idx();
241         IdRange::new(start..end)
242     }
243
244     fn lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field> {
245         let name = field.name()?.as_name();
246         let visibility = self.lower_visibility(field);
247         let type_ref = self.lower_type_ref_opt(field.ty());
248         let res = Field { name, type_ref, visibility };
249         Some(res)
250     }
251
252     fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdRange<Field> {
253         let start = self.next_field_idx();
254         for (i, field) in fields.fields().enumerate() {
255             let data = self.lower_tuple_field(i, &field);
256             let idx = self.data().fields.alloc(data);
257             self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
258         }
259         let end = self.next_field_idx();
260         IdRange::new(start..end)
261     }
262
263     fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
264         let name = Name::new_tuple_field(idx);
265         let visibility = self.lower_visibility(field);
266         let type_ref = self.lower_type_ref_opt(field.ty());
267         let res = Field { name, type_ref, visibility };
268         res
269     }
270
271     fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
272         let visibility = self.lower_visibility(union);
273         let name = union.name()?.as_name();
274         let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
275         let fields = match union.record_field_list() {
276             Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
277             None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())),
278         };
279         let ast_id = self.source_ast_id_map.ast_id(union);
280         let res = Union { name, visibility, generic_params, fields, ast_id };
281         Some(id(self.data().unions.alloc(res)))
282     }
283
284     fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
285         let visibility = self.lower_visibility(enum_);
286         let name = enum_.name()?.as_name();
287         let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
288         let variants =
289             self.with_inherited_visibility(visibility, |this| match &enum_.variant_list() {
290                 Some(variant_list) => this.lower_variants(variant_list),
291                 None => IdRange::new(this.next_variant_idx()..this.next_variant_idx()),
292             });
293         let ast_id = self.source_ast_id_map.ast_id(enum_);
294         let res = Enum { name, visibility, generic_params, variants, ast_id };
295         Some(id(self.data().enums.alloc(res)))
296     }
297
298     fn lower_variants(&mut self, variants: &ast::VariantList) -> IdRange<Variant> {
299         let start = self.next_variant_idx();
300         for variant in variants.variants() {
301             if let Some(data) = self.lower_variant(&variant) {
302                 let idx = self.data().variants.alloc(data);
303                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &variant, &self.hygiene));
304             }
305         }
306         let end = self.next_variant_idx();
307         IdRange::new(start..end)
308     }
309
310     fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
311         let name = variant.name()?.as_name();
312         let fields = self.lower_fields(&variant.kind());
313         let res = Variant { name, fields };
314         Some(res)
315     }
316
317     fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
318         let visibility = self.lower_visibility(func);
319         let name = func.name()?.as_name();
320
321         let mut has_self_param = false;
322         let start_param = self.next_param_idx();
323         if let Some(param_list) = func.param_list() {
324             if let Some(self_param) = param_list.self_param() {
325                 let self_type = match self_param.ty() {
326                     Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
327                     None => {
328                         let self_type = TypeRef::Path(name![Self].into());
329                         match self_param.kind() {
330                             ast::SelfParamKind::Owned => self_type,
331                             ast::SelfParamKind::Ref => TypeRef::Reference(
332                                 Box::new(self_type),
333                                 self_param.lifetime().as_ref().map(LifetimeRef::new),
334                                 Mutability::Shared,
335                             ),
336                             ast::SelfParamKind::MutRef => TypeRef::Reference(
337                                 Box::new(self_type),
338                                 self_param.lifetime().as_ref().map(LifetimeRef::new),
339                                 Mutability::Mut,
340                             ),
341                         }
342                     }
343                 };
344                 let ty = Interned::new(self_type);
345                 let idx = self.data().params.alloc(Param::Normal(ty));
346                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &self_param, &self.hygiene));
347                 has_self_param = true;
348             }
349             for param in param_list.params() {
350                 let idx = match param.dotdotdot_token() {
351                     Some(_) => self.data().params.alloc(Param::Varargs),
352                     None => {
353                         let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
354                         let ty = Interned::new(type_ref);
355                         self.data().params.alloc(Param::Normal(ty))
356                     }
357                 };
358                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &param, &self.hygiene));
359             }
360         }
361         let end_param = self.next_param_idx();
362         let params = IdRange::new(start_param..end_param);
363
364         let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
365             Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
366             _ => TypeRef::unit(),
367         };
368
369         let (ret_type, async_ret_type) = if func.async_token().is_some() {
370             let async_ret_type = ret_type.clone();
371             let future_impl = desugar_future_path(ret_type);
372             let ty_bound = Interned::new(TypeBound::Path(future_impl, TraitBoundModifier::None));
373             (TypeRef::ImplTrait(vec![ty_bound]), Some(async_ret_type))
374         } else {
375             (ret_type, None)
376         };
377
378         let abi = func.abi().map(lower_abi);
379
380         let ast_id = self.source_ast_id_map.ast_id(func);
381
382         let mut flags = FnFlags::default();
383         if func.body().is_some() {
384             flags.bits |= FnFlags::HAS_BODY;
385         }
386         if has_self_param {
387             flags.bits |= FnFlags::HAS_SELF_PARAM;
388         }
389         if func.default_token().is_some() {
390             flags.bits |= FnFlags::IS_DEFAULT;
391         }
392         if func.const_token().is_some() {
393             flags.bits |= FnFlags::IS_CONST;
394         }
395         if func.async_token().is_some() {
396             flags.bits |= FnFlags::IS_ASYNC;
397         }
398         if func.unsafe_token().is_some() {
399             flags.bits |= FnFlags::IS_UNSAFE;
400         }
401
402         let mut res = Function {
403             name,
404             visibility,
405             generic_params: Interned::new(GenericParams::default()),
406             abi,
407             params,
408             ret_type: Interned::new(ret_type),
409             async_ret_type: async_ret_type.map(Interned::new),
410             ast_id,
411             flags,
412         };
413         res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
414
415         Some(id(self.data().functions.alloc(res)))
416     }
417
418     fn lower_type_alias(
419         &mut self,
420         type_alias: &ast::TypeAlias,
421     ) -> Option<FileItemTreeId<TypeAlias>> {
422         let name = type_alias.name()?.as_name();
423         let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
424         let visibility = self.lower_visibility(type_alias);
425         let bounds = self.lower_type_bounds(type_alias);
426         let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
427         let ast_id = self.source_ast_id_map.ast_id(type_alias);
428         let res = TypeAlias {
429             name,
430             visibility,
431             bounds: bounds.into_boxed_slice(),
432             generic_params,
433             type_ref,
434             ast_id,
435             is_extern: false,
436         };
437         Some(id(self.data().type_aliases.alloc(res)))
438     }
439
440     fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>> {
441         let name = static_.name()?.as_name();
442         let type_ref = self.lower_type_ref_opt(static_.ty());
443         let visibility = self.lower_visibility(static_);
444         let mutable = static_.mut_token().is_some();
445         let ast_id = self.source_ast_id_map.ast_id(static_);
446         let res = Static { name, visibility, mutable, type_ref, ast_id, is_extern: false };
447         Some(id(self.data().statics.alloc(res)))
448     }
449
450     fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
451         let mut name = konst.name().map(|it| it.as_name());
452         if name.as_ref().map_or(false, |n| n.to_string().starts_with("_DERIVE_")) {
453             // FIXME: this is a hack to treat consts generated by synstructure as unnamed
454             // remove this some time in the future
455             name = None;
456         }
457         let type_ref = self.lower_type_ref_opt(konst.ty());
458         let visibility = self.lower_visibility(konst);
459         let ast_id = self.source_ast_id_map.ast_id(konst);
460         let res = Const { name, visibility, type_ref, ast_id };
461         id(self.data().consts.alloc(res))
462     }
463
464     fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
465         let name = module.name()?.as_name();
466         let visibility = self.lower_visibility(module);
467         let kind = if module.semicolon_token().is_some() {
468             ModKind::Outline {}
469         } else {
470             ModKind::Inline {
471                 items: module
472                     .item_list()
473                     .map(|list| {
474                         list.items().flat_map(|item| self.lower_mod_item(&item, false)).collect()
475                     })
476                     .unwrap_or_else(|| {
477                         cov_mark::hit!(name_res_works_for_broken_modules);
478                         Box::new([]) as Box<[_]>
479                     }),
480             }
481         };
482         let ast_id = self.source_ast_id_map.ast_id(module);
483         let res = Mod { name, visibility, kind, ast_id };
484         Some(id(self.data().mods.alloc(res)))
485     }
486
487     fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
488         let name = trait_def.name()?.as_name();
489         let visibility = self.lower_visibility(trait_def);
490         let generic_params =
491             self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
492         let is_auto = trait_def.auto_token().is_some();
493         let is_unsafe = trait_def.unsafe_token().is_some();
494         let items = trait_def.assoc_item_list().map(|list| {
495             let db = self.db;
496             self.with_inherited_visibility(visibility, |this| {
497                 list.assoc_items()
498                     .filter_map(|item| {
499                         let attrs = RawAttrs::new(db, &item, &this.hygiene);
500                         this.collect_inner_items(item.syntax());
501                         this.lower_assoc_item(&item).map(|item| {
502                             this.add_attrs(ModItem::from(item).into(), attrs);
503                             item
504                         })
505                     })
506                     .collect()
507             })
508         });
509         let ast_id = self.source_ast_id_map.ast_id(trait_def);
510         let res = Trait {
511             name,
512             visibility,
513             generic_params,
514             is_auto,
515             is_unsafe,
516             items: items.unwrap_or_default(),
517             ast_id,
518         };
519         Some(id(self.data().traits.alloc(res)))
520     }
521
522     fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
523         let generic_params =
524             self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
525         // FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
526         // as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
527         // equals itself.
528         let target_trait = impl_def.trait_().and_then(|tr| self.lower_trait_ref(&tr));
529         let self_ty = self.lower_type_ref(&impl_def.self_ty()?);
530         let is_negative = impl_def.excl_token().is_some();
531
532         // We cannot use `assoc_items()` here as that does not include macro calls.
533         let items = impl_def
534             .assoc_item_list()
535             .into_iter()
536             .flat_map(|it| it.assoc_items())
537             .filter_map(|item| {
538                 self.collect_inner_items(item.syntax());
539                 let assoc = self.lower_assoc_item(&item)?;
540                 let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
541                 self.add_attrs(ModItem::from(assoc).into(), attrs);
542                 Some(assoc)
543             })
544             .collect();
545         let ast_id = self.source_ast_id_map.ast_id(impl_def);
546         let res = Impl { generic_params, target_trait, self_ty, is_negative, items, ast_id };
547         Some(id(self.data().impls.alloc(res)))
548     }
549
550     fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>> {
551         let visibility = self.lower_visibility(use_item);
552         let ast_id = self.source_ast_id_map.ast_id(use_item);
553         let (use_tree, _) = lower_use_tree(self.db, &self.hygiene, use_item.use_tree()?)?;
554
555         let res = Import { visibility, ast_id, use_tree };
556         Some(id(self.data().imports.alloc(res)))
557     }
558
559     fn lower_extern_crate(
560         &mut self,
561         extern_crate: &ast::ExternCrate,
562     ) -> Option<FileItemTreeId<ExternCrate>> {
563         let name = extern_crate.name_ref()?.as_name();
564         let alias = extern_crate.rename().map(|a| {
565             a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
566         });
567         let visibility = self.lower_visibility(extern_crate);
568         let ast_id = self.source_ast_id_map.ast_id(extern_crate);
569
570         let res = ExternCrate { name, alias, visibility, ast_id };
571         Some(id(self.data().extern_crates.alloc(res)))
572     }
573
574     fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
575         let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
576         let ast_id = self.source_ast_id_map.ast_id(m);
577         let fragment = hir_expand::to_fragment_kind(m);
578         let res = MacroCall { path, ast_id, fragment };
579         Some(id(self.data().macro_calls.alloc(res)))
580     }
581
582     fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
583         let name = m.name().map(|it| it.as_name())?;
584         let ast_id = self.source_ast_id_map.ast_id(m);
585
586         let res = MacroRules { name, ast_id };
587         Some(id(self.data().macro_rules.alloc(res)))
588     }
589
590     fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
591         let name = m.name().map(|it| it.as_name())?;
592
593         let ast_id = self.source_ast_id_map.ast_id(m);
594         let visibility = self.lower_visibility(m);
595
596         let res = MacroDef { name, ast_id, visibility };
597         Some(id(self.data().macro_defs.alloc(res)))
598     }
599
600     fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock> {
601         let ast_id = self.source_ast_id_map.ast_id(block);
602         let abi = block.abi().map(lower_abi);
603         let children: Box<[_]> = block.extern_item_list().map_or(Box::new([]), |list| {
604             list.extern_items()
605                 .filter_map(|item| {
606                     self.collect_inner_items(item.syntax());
607                     let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
608                     let id: ModItem = match item {
609                         ast::ExternItem::Fn(ast) => {
610                             let func_id = self.lower_function(&ast)?;
611                             let func = &mut self.data().functions[func_id.index];
612                             if is_intrinsic_fn_unsafe(&func.name) {
613                                 func.flags.bits |= FnFlags::IS_UNSAFE;
614                             }
615                             func.flags.bits |= FnFlags::IS_IN_EXTERN_BLOCK;
616                             func_id.into()
617                         }
618                         ast::ExternItem::Static(ast) => {
619                             let statik = self.lower_static(&ast)?;
620                             self.data().statics[statik.index].is_extern = true;
621                             statik.into()
622                         }
623                         ast::ExternItem::TypeAlias(ty) => {
624                             let foreign_ty = self.lower_type_alias(&ty)?;
625                             self.data().type_aliases[foreign_ty.index].is_extern = true;
626                             foreign_ty.into()
627                         }
628                         ast::ExternItem::MacroCall(call) => {
629                             // FIXME: we need some way of tracking that the macro call is in an
630                             // extern block
631                             self.lower_macro_call(&call)?.into()
632                         }
633                     };
634                     self.add_attrs(id.into(), attrs);
635                     Some(id)
636                 })
637                 .collect()
638         });
639
640         let res = ExternBlock { abi, ast_id, children };
641         id(self.data().extern_blocks.alloc(res))
642     }
643
644     /// Lowers generics defined on `node` and collects inner items defined within.
645     fn lower_generic_params_and_inner_items(
646         &mut self,
647         owner: GenericsOwner<'_>,
648         node: &impl ast::GenericParamsOwner,
649     ) -> Interned<GenericParams> {
650         // Generics are part of item headers and may contain inner items we need to collect.
651         if let Some(params) = node.generic_param_list() {
652             self.collect_inner_items(params.syntax());
653         }
654         if let Some(clause) = node.where_clause() {
655             self.collect_inner_items(clause.syntax());
656         }
657
658         self.lower_generic_params(owner, node)
659     }
660
661     fn lower_generic_params(
662         &mut self,
663         owner: GenericsOwner<'_>,
664         node: &impl ast::GenericParamsOwner,
665     ) -> Interned<GenericParams> {
666         let mut sm = &mut Default::default();
667         let mut generics = GenericParams::default();
668         match owner {
669             GenericsOwner::Function(func) => {
670                 generics.fill(&self.body_ctx, sm, node);
671                 // lower `impl Trait` in arguments
672                 for id in func.params.clone() {
673                     if let Param::Normal(ty) = &self.data().params[id] {
674                         generics.fill_implicit_impl_trait_args(ty);
675                     }
676                 }
677             }
678             GenericsOwner::Struct
679             | GenericsOwner::Enum
680             | GenericsOwner::Union
681             | GenericsOwner::TypeAlias => {
682                 generics.fill(&self.body_ctx, sm, node);
683             }
684             GenericsOwner::Trait(trait_def) => {
685                 // traits get the Self type as an implicit first type parameter
686                 let self_param_id = generics.types.alloc(TypeParamData {
687                     name: Some(name![Self]),
688                     default: None,
689                     provenance: TypeParamProvenance::TraitSelf,
690                 });
691                 sm.type_params.insert(self_param_id, Either::Right(trait_def.clone()));
692                 // add super traits as bounds on Self
693                 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
694                 let self_param = TypeRef::Path(name![Self].into());
695                 generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
696                 generics.fill(&self.body_ctx, &mut sm, node);
697             }
698             GenericsOwner::Impl => {
699                 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
700                 // type-parameter, but rather is a type-alias for impl's target
701                 // type, so this is handled by the resolver.
702                 generics.fill(&self.body_ctx, &mut sm, node);
703             }
704         }
705
706         generics.shrink_to_fit();
707         Interned::new(generics)
708     }
709
710     fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<Interned<TypeBound>> {
711         match node.type_bound_list() {
712             Some(bound_list) => bound_list
713                 .bounds()
714                 .map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it)))
715                 .collect(),
716             None => Vec::new(),
717         }
718     }
719
720     fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
721         let vis = match self.forced_visibility {
722             Some(vis) => return vis,
723             None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
724         };
725
726         self.data().vis.alloc(vis)
727     }
728
729     fn lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>> {
730         let trait_ref = TraitRef::from_ast(&self.body_ctx, trait_ref.clone())?;
731         Some(Interned::new(trait_ref))
732     }
733
734     fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef> {
735         let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone());
736         Interned::new(tyref)
737     }
738
739     fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef> {
740         match type_ref.map(|ty| self.lower_type_ref(&ty)) {
741             Some(it) => it,
742             None => Interned::new(TypeRef::Error),
743         }
744     }
745
746     /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
747     fn with_inherited_visibility<R>(
748         &mut self,
749         vis: RawVisibilityId,
750         f: impl FnOnce(&mut Self) -> R,
751     ) -> R {
752         let old = mem::replace(&mut self.forced_visibility, Some(vis));
753         let res = f(self);
754         self.forced_visibility = old;
755         res
756     }
757
758     fn next_field_idx(&self) -> Idx<Field> {
759         Idx::from_raw(RawIdx::from(
760             self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
761         ))
762     }
763     fn next_variant_idx(&self) -> Idx<Variant> {
764         Idx::from_raw(RawIdx::from(
765             self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
766         ))
767     }
768     fn next_param_idx(&self) -> Idx<Param> {
769         Idx::from_raw(RawIdx::from(
770             self.tree.data.as_ref().map_or(0, |data| data.params.len() as u32),
771         ))
772     }
773 }
774
775 fn desugar_future_path(orig: TypeRef) -> Path {
776     let path = path![core::future::Future];
777     let mut generic_args: Vec<_> =
778         std::iter::repeat(None).take(path.segments().len() - 1).collect();
779     let mut last = GenericArgs::empty();
780     let binding =
781         AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() };
782     last.bindings.push(binding);
783     generic_args.push(Some(Interned::new(last)));
784
785     Path::from_known_path(path, generic_args)
786 }
787
788 enum GenericsOwner<'a> {
789     /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
790     /// position.
791     Function(&'a Function),
792     Struct,
793     Enum,
794     Union,
795     /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
796     Trait(&'a ast::Trait),
797     TypeAlias,
798     Impl,
799 }
800
801 /// Returns `true` if the given intrinsic is unsafe to call, or false otherwise.
802 fn is_intrinsic_fn_unsafe(name: &Name) -> bool {
803     // Should be kept in sync with https://github.com/rust-lang/rust/blob/0cd0709f19d316c4796fa71c5f52c8612a5f3771/compiler/rustc_typeck/src/check/intrinsic.rs#L72-L105
804     ![
805         known::abort,
806         known::add_with_overflow,
807         known::bitreverse,
808         known::bswap,
809         known::caller_location,
810         known::ctlz,
811         known::ctpop,
812         known::cttz,
813         known::discriminant_value,
814         known::forget,
815         known::likely,
816         known::maxnumf32,
817         known::maxnumf64,
818         known::min_align_of,
819         known::minnumf32,
820         known::minnumf64,
821         known::mul_with_overflow,
822         known::needs_drop,
823         known::ptr_guaranteed_eq,
824         known::ptr_guaranteed_ne,
825         known::rotate_left,
826         known::rotate_right,
827         known::rustc_peek,
828         known::saturating_add,
829         known::saturating_sub,
830         known::size_of,
831         known::sub_with_overflow,
832         known::type_id,
833         known::type_name,
834         known::unlikely,
835         known::variant_count,
836         known::wrapping_add,
837         known::wrapping_mul,
838         known::wrapping_sub,
839     ]
840     .contains(name)
841 }
842
843 fn lower_abi(abi: ast::Abi) -> Interned<str> {
844     // FIXME: Abi::abi() -> Option<SyntaxToken>?
845     match abi.syntax().last_token() {
846         Some(tok) if tok.kind() == SyntaxKind::STRING => {
847             // FIXME: Better way to unescape?
848             Interned::new_str(tok.text().trim_matches('"'))
849         }
850         _ => {
851             // `extern` default to be `extern "C"`.
852             Interned::new_str("C")
853         }
854     }
855 }
856
857 struct UseTreeLowering<'a> {
858     db: &'a dyn DefDatabase,
859     hygiene: &'a Hygiene,
860     mapping: Arena<ast::UseTree>,
861 }
862
863 impl UseTreeLowering<'_> {
864     fn lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree> {
865         if let Some(use_tree_list) = tree.use_tree_list() {
866             let prefix = match tree.path() {
867                 // E.g. use something::{{{inner}}};
868                 None => None,
869                 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
870                 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
871                 Some(path) => {
872                     match ModPath::from_src(self.db, path, self.hygiene) {
873                         Some(it) => Some(it),
874                         None => return None, // FIXME: report errors somewhere
875                     }
876                 }
877             };
878
879             let list =
880                 use_tree_list.use_trees().filter_map(|tree| self.lower_use_tree(tree)).collect();
881
882             Some(
883                 self.use_tree(
884                     UseTreeKind::Prefixed { prefix: prefix.map(Interned::new), list },
885                     tree,
886                 ),
887             )
888         } else {
889             let is_glob = tree.star_token().is_some();
890             let path = match tree.path() {
891                 Some(path) => Some(ModPath::from_src(self.db, path, self.hygiene)?),
892                 None => None,
893             };
894             let alias = tree.rename().map(|a| {
895                 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
896             });
897             if alias.is_some() && is_glob {
898                 return None;
899             }
900
901             match (path, alias, is_glob) {
902                 (path, None, true) => {
903                     if path.is_none() {
904                         cov_mark::hit!(glob_enum_group);
905                     }
906                     Some(self.use_tree(UseTreeKind::Glob { path: path.map(Interned::new) }, tree))
907                 }
908                 // Globs can't be renamed
909                 (_, Some(_), true) | (None, None, false) => None,
910                 // `bla::{ as Name}` is invalid
911                 (None, Some(_), false) => None,
912                 (Some(path), alias, false) => Some(
913                     self.use_tree(UseTreeKind::Single { path: Interned::new(path), alias }, tree),
914                 ),
915             }
916         }
917     }
918
919     fn use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree {
920         let index = self.mapping.alloc(ast);
921         UseTree { index, kind }
922     }
923 }
924
925 pub(super) fn lower_use_tree(
926     db: &dyn DefDatabase,
927     hygiene: &Hygiene,
928     tree: ast::UseTree,
929 ) -> Option<(UseTree, Arena<ast::UseTree>)> {
930     let mut lowering = UseTreeLowering { db, hygiene, mapping: Arena::new() };
931     let tree = lowering.lower_use_tree(tree)?;
932     Some((tree, lowering.mapping))
933 }