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, HasModuleItem},
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
id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N>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> {
new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self32 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
lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree43 pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> 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
lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree49 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
lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree83 pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
84 self.collect_inner_items(within);
85 self.tree
86 }
87
data(&mut self) -> &mut ItemTreeData88 fn data(&mut self) -> &mut ItemTreeData {
89 self.tree.data_mut()
90 }
91
lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItem>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
add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs)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
collect_inner_items(&mut self, container: &SyntaxNode)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
lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem>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
lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>>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
lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields218 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
lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdxRange<Field>232 fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdxRange<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 IdxRange::new(start..end)
242 }
243
lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field>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
lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdxRange<Field>252 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdxRange<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 IdxRange::new(start..end)
261 }
262
lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field263 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 Field { name, type_ref, visibility }
268 }
269
lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>>270 fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
271 let visibility = self.lower_visibility(union);
272 let name = union.name()?.as_name();
273 let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
274 let fields = match union.record_field_list() {
275 Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
276 None => Fields::Record(IdxRange::new(self.next_field_idx()..self.next_field_idx())),
277 };
278 let ast_id = self.source_ast_id_map.ast_id(union);
279 let res = Union { name, visibility, generic_params, fields, ast_id };
280 Some(id(self.data().unions.alloc(res)))
281 }
282
lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>>283 fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
284 let visibility = self.lower_visibility(enum_);
285 let name = enum_.name()?.as_name();
286 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
287 let variants =
288 self.with_inherited_visibility(visibility, |this| match &enum_.variant_list() {
289 Some(variant_list) => this.lower_variants(variant_list),
290 None => IdxRange::new(this.next_variant_idx()..this.next_variant_idx()),
291 });
292 let ast_id = self.source_ast_id_map.ast_id(enum_);
293 let res = Enum { name, visibility, generic_params, variants, ast_id };
294 Some(id(self.data().enums.alloc(res)))
295 }
296
lower_variants(&mut self, variants: &ast::VariantList) -> IdxRange<Variant>297 fn lower_variants(&mut self, variants: &ast::VariantList) -> IdxRange<Variant> {
298 let start = self.next_variant_idx();
299 for variant in variants.variants() {
300 if let Some(data) = self.lower_variant(&variant) {
301 let idx = self.data().variants.alloc(data);
302 self.add_attrs(idx.into(), RawAttrs::new(self.db, &variant, &self.hygiene));
303 }
304 }
305 let end = self.next_variant_idx();
306 IdxRange::new(start..end)
307 }
308
lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant>309 fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
310 let name = variant.name()?.as_name();
311 let fields = self.lower_fields(&variant.kind());
312 let res = Variant { name, fields };
313 Some(res)
314 }
315
lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>316 fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
317 let visibility = self.lower_visibility(func);
318 let name = func.name()?.as_name();
319
320 let mut has_self_param = false;
321 let start_param = self.next_param_idx();
322 if let Some(param_list) = func.param_list() {
323 if let Some(self_param) = param_list.self_param() {
324 let self_type = match self_param.ty() {
325 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
326 None => {
327 let self_type = TypeRef::Path(name![Self].into());
328 match self_param.kind() {
329 ast::SelfParamKind::Owned => self_type,
330 ast::SelfParamKind::Ref => TypeRef::Reference(
331 Box::new(self_type),
332 self_param.lifetime().as_ref().map(LifetimeRef::new),
333 Mutability::Shared,
334 ),
335 ast::SelfParamKind::MutRef => TypeRef::Reference(
336 Box::new(self_type),
337 self_param.lifetime().as_ref().map(LifetimeRef::new),
338 Mutability::Mut,
339 ),
340 }
341 }
342 };
343 let ty = Interned::new(self_type);
344 let idx = self.data().params.alloc(Param::Normal(ty));
345 self.add_attrs(idx.into(), RawAttrs::new(self.db, &self_param, &self.hygiene));
346 has_self_param = true;
347 }
348 for param in param_list.params() {
349 let idx = match param.dotdotdot_token() {
350 Some(_) => self.data().params.alloc(Param::Varargs),
351 None => {
352 let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
353 let ty = Interned::new(type_ref);
354 self.data().params.alloc(Param::Normal(ty))
355 }
356 };
357 self.add_attrs(idx.into(), RawAttrs::new(self.db, ¶m, &self.hygiene));
358 }
359 }
360 let end_param = self.next_param_idx();
361 let params = IdxRange::new(start_param..end_param);
362
363 let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
364 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
365 _ => TypeRef::unit(),
366 };
367
368 let (ret_type, async_ret_type) = if func.async_token().is_some() {
369 let async_ret_type = ret_type.clone();
370 let future_impl = desugar_future_path(ret_type);
371 let ty_bound = Interned::new(TypeBound::Path(future_impl, TraitBoundModifier::None));
372 (TypeRef::ImplTrait(vec![ty_bound]), Some(async_ret_type))
373 } else {
374 (ret_type, None)
375 };
376
377 let abi = func.abi().map(lower_abi);
378
379 let ast_id = self.source_ast_id_map.ast_id(func);
380
381 let mut flags = FnFlags::default();
382 if func.body().is_some() {
383 flags.bits |= FnFlags::HAS_BODY;
384 }
385 if has_self_param {
386 flags.bits |= FnFlags::HAS_SELF_PARAM;
387 }
388 if func.default_token().is_some() {
389 flags.bits |= FnFlags::IS_DEFAULT;
390 }
391 if func.const_token().is_some() {
392 flags.bits |= FnFlags::IS_CONST;
393 }
394 if func.async_token().is_some() {
395 flags.bits |= FnFlags::IS_ASYNC;
396 }
397 if func.unsafe_token().is_some() {
398 flags.bits |= FnFlags::IS_UNSAFE;
399 }
400
401 let mut res = Function {
402 name,
403 visibility,
404 explicit_generic_params: Interned::new(GenericParams::default()),
405 abi,
406 params,
407 ret_type: Interned::new(ret_type),
408 async_ret_type: async_ret_type.map(Interned::new),
409 ast_id,
410 flags,
411 };
412 res.explicit_generic_params =
413 self.lower_generic_params(GenericsOwner::Function(&res), func);
414
415 Some(id(self.data().functions.alloc(res)))
416 }
417
lower_type_alias( &mut self, type_alias: &ast::TypeAlias, ) -> Option<FileItemTreeId<TypeAlias>>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
lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>>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
lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const>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_smol_str().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
lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>>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
lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>>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
lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>>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
lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>>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
lower_extern_crate( &mut self, extern_crate: &ast::ExternCrate, ) -> Option<FileItemTreeId<ExternCrate>>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
lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>>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 expand_to = hir_expand::ExpandTo::from_call_site(m);
578 let res = MacroCall { path, ast_id, expand_to };
579 Some(id(self.data().macro_calls.alloc(res)))
580 }
581
lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>>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
lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>>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
lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock>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.
lower_generic_params_and_inner_items( &mut self, owner: GenericsOwner<'_>, node: &dyn ast::HasGenericParams, ) -> Interned<GenericParams>645 fn lower_generic_params_and_inner_items(
646 &mut self,
647 owner: GenericsOwner<'_>,
648 node: &dyn ast::HasGenericParams,
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
lower_generic_params( &mut self, owner: GenericsOwner<'_>, node: &dyn ast::HasGenericParams, ) -> Interned<GenericParams>661 fn lower_generic_params(
662 &mut self,
663 owner: GenericsOwner<'_>,
664 node: &dyn ast::HasGenericParams,
665 ) -> Interned<GenericParams> {
666 let mut generics = GenericParams::default();
667 match owner {
668 GenericsOwner::Function(_)
669 | GenericsOwner::Struct
670 | GenericsOwner::Enum
671 | GenericsOwner::Union
672 | GenericsOwner::TypeAlias => {
673 generics.fill(&self.body_ctx, node);
674 }
675 GenericsOwner::Trait(trait_def) => {
676 // traits get the Self type as an implicit first type parameter
677 generics.types.alloc(TypeParamData {
678 name: Some(name![Self]),
679 default: None,
680 provenance: TypeParamProvenance::TraitSelf,
681 });
682 // add super traits as bounds on Self
683 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
684 let self_param = TypeRef::Path(name![Self].into());
685 generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
686 generics.fill(&self.body_ctx, node);
687 }
688 GenericsOwner::Impl => {
689 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
690 // type-parameter, but rather is a type-alias for impl's target
691 // type, so this is handled by the resolver.
692 generics.fill(&self.body_ctx, node);
693 }
694 }
695
696 generics.shrink_to_fit();
697 Interned::new(generics)
698 }
699
lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>>700 fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>> {
701 match node.type_bound_list() {
702 Some(bound_list) => bound_list
703 .bounds()
704 .map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it)))
705 .collect(),
706 None => Vec::new(),
707 }
708 }
709
lower_visibility(&mut self, item: &dyn ast::HasVisibility) -> RawVisibilityId710 fn lower_visibility(&mut self, item: &dyn ast::HasVisibility) -> RawVisibilityId {
711 let vis = match self.forced_visibility {
712 Some(vis) => return vis,
713 None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
714 };
715
716 self.data().vis.alloc(vis)
717 }
718
lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>>719 fn lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>> {
720 let trait_ref = TraitRef::from_ast(&self.body_ctx, trait_ref.clone())?;
721 Some(Interned::new(trait_ref))
722 }
723
lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef>724 fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef> {
725 let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone());
726 Interned::new(tyref)
727 }
728
lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef>729 fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef> {
730 match type_ref.map(|ty| self.lower_type_ref(&ty)) {
731 Some(it) => it,
732 None => Interned::new(TypeRef::Error),
733 }
734 }
735
736 /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
with_inherited_visibility<R>( &mut self, vis: RawVisibilityId, f: impl FnOnce(&mut Self) -> R, ) -> R737 fn with_inherited_visibility<R>(
738 &mut self,
739 vis: RawVisibilityId,
740 f: impl FnOnce(&mut Self) -> R,
741 ) -> R {
742 let old = mem::replace(&mut self.forced_visibility, Some(vis));
743 let res = f(self);
744 self.forced_visibility = old;
745 res
746 }
747
next_field_idx(&self) -> Idx<Field>748 fn next_field_idx(&self) -> Idx<Field> {
749 Idx::from_raw(RawIdx::from(
750 self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
751 ))
752 }
next_variant_idx(&self) -> Idx<Variant>753 fn next_variant_idx(&self) -> Idx<Variant> {
754 Idx::from_raw(RawIdx::from(
755 self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
756 ))
757 }
next_param_idx(&self) -> Idx<Param>758 fn next_param_idx(&self) -> Idx<Param> {
759 Idx::from_raw(RawIdx::from(
760 self.tree.data.as_ref().map_or(0, |data| data.params.len() as u32),
761 ))
762 }
763 }
764
desugar_future_path(orig: TypeRef) -> Path765 fn desugar_future_path(orig: TypeRef) -> Path {
766 let path = path![core::future::Future];
767 let mut generic_args: Vec<_> =
768 std::iter::repeat(None).take(path.segments().len() - 1).collect();
769 let mut last = GenericArgs::empty();
770 let binding =
771 AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() };
772 last.bindings.push(binding);
773 generic_args.push(Some(Interned::new(last)));
774
775 Path::from_known_path(path, generic_args)
776 }
777
778 enum GenericsOwner<'a> {
779 /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
780 /// position.
781 Function(&'a Function),
782 Struct,
783 Enum,
784 Union,
785 /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
786 Trait(&'a ast::Trait),
787 TypeAlias,
788 Impl,
789 }
790
791 /// Returns `true` if the given intrinsic is unsafe to call, or false otherwise.
is_intrinsic_fn_unsafe(name: &Name) -> bool792 fn is_intrinsic_fn_unsafe(name: &Name) -> bool {
793 // Should be kept in sync with https://github.com/rust-lang/rust/blob/0cd0709f19d316c4796fa71c5f52c8612a5f3771/compiler/rustc_typeck/src/check/intrinsic.rs#L72-L105
794 ![
795 known::abort,
796 known::add_with_overflow,
797 known::bitreverse,
798 known::bswap,
799 known::caller_location,
800 known::ctlz,
801 known::ctpop,
802 known::cttz,
803 known::discriminant_value,
804 known::forget,
805 known::likely,
806 known::maxnumf32,
807 known::maxnumf64,
808 known::min_align_of,
809 known::minnumf32,
810 known::minnumf64,
811 known::mul_with_overflow,
812 known::needs_drop,
813 known::ptr_guaranteed_eq,
814 known::ptr_guaranteed_ne,
815 known::rotate_left,
816 known::rotate_right,
817 known::rustc_peek,
818 known::saturating_add,
819 known::saturating_sub,
820 known::size_of,
821 known::sub_with_overflow,
822 known::type_id,
823 known::type_name,
824 known::unlikely,
825 known::variant_count,
826 known::wrapping_add,
827 known::wrapping_mul,
828 known::wrapping_sub,
829 ]
830 .contains(name)
831 }
832
lower_abi(abi: ast::Abi) -> Interned<str>833 fn lower_abi(abi: ast::Abi) -> Interned<str> {
834 // FIXME: Abi::abi() -> Option<SyntaxToken>?
835 match abi.syntax().last_token() {
836 Some(tok) if tok.kind() == SyntaxKind::STRING => {
837 // FIXME: Better way to unescape?
838 Interned::new_str(tok.text().trim_matches('"'))
839 }
840 _ => {
841 // `extern` default to be `extern "C"`.
842 Interned::new_str("C")
843 }
844 }
845 }
846
847 struct UseTreeLowering<'a> {
848 db: &'a dyn DefDatabase,
849 hygiene: &'a Hygiene,
850 mapping: Arena<ast::UseTree>,
851 }
852
853 impl UseTreeLowering<'_> {
lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree>854 fn lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree> {
855 if let Some(use_tree_list) = tree.use_tree_list() {
856 let prefix = match tree.path() {
857 // E.g. use something::{{{inner}}};
858 None => None,
859 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
860 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
861 Some(path) => {
862 match ModPath::from_src(self.db, path, self.hygiene) {
863 Some(it) => Some(it),
864 None => return None, // FIXME: report errors somewhere
865 }
866 }
867 };
868
869 let list =
870 use_tree_list.use_trees().filter_map(|tree| self.lower_use_tree(tree)).collect();
871
872 Some(
873 self.use_tree(
874 UseTreeKind::Prefixed { prefix: prefix.map(Interned::new), list },
875 tree,
876 ),
877 )
878 } else {
879 let is_glob = tree.star_token().is_some();
880 let path = match tree.path() {
881 Some(path) => Some(ModPath::from_src(self.db, path, self.hygiene)?),
882 None => None,
883 };
884 let alias = tree.rename().map(|a| {
885 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
886 });
887 if alias.is_some() && is_glob {
888 return None;
889 }
890
891 match (path, alias, is_glob) {
892 (path, None, true) => {
893 if path.is_none() {
894 cov_mark::hit!(glob_enum_group);
895 }
896 Some(self.use_tree(UseTreeKind::Glob { path: path.map(Interned::new) }, tree))
897 }
898 // Globs can't be renamed
899 (_, Some(_), true) | (None, None, false) => None,
900 // `bla::{ as Name}` is invalid
901 (None, Some(_), false) => None,
902 (Some(path), alias, false) => Some(
903 self.use_tree(UseTreeKind::Single { path: Interned::new(path), alias }, tree),
904 ),
905 }
906 }
907 }
908
use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree909 fn use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree {
910 let index = self.mapping.alloc(ast);
911 UseTree { index, kind }
912 }
913 }
914
lower_use_tree( db: &dyn DefDatabase, hygiene: &Hygiene, tree: ast::UseTree, ) -> Option<(UseTree, Arena<ast::UseTree>)>915 pub(super) fn lower_use_tree(
916 db: &dyn DefDatabase,
917 hygiene: &Hygiene,
918 tree: ast::UseTree,
919 ) -> Option<(UseTree, Arena<ast::UseTree>)> {
920 let mut lowering = UseTreeLowering { db, hygiene, mapping: Arena::new() };
921 let tree = lowering.lower_use_tree(tree)?;
922 Some((tree, lowering.mapping))
923 }
924