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