1 //! AST -> `ItemTree` lowering code.
2
3 use std::collections::hash_map::Entry;
4
5 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId};
6 use syntax::ast::{self, HasModuleItem, HasTypeBounds};
7
8 use crate::{
9 generics::{GenericParams, TypeParamData, TypeParamProvenance},
10 type_ref::{LifetimeRef, TraitBoundModifier, TraitRef},
11 };
12
13 use super::*;
14
id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N>15 fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
16 FileItemTreeId { index, _p: PhantomData }
17 }
18
19 pub(super) struct Ctx<'a> {
20 db: &'a dyn DefDatabase,
21 tree: ItemTree,
22 source_ast_id_map: Arc<AstIdMap>,
23 body_ctx: crate::lower::LowerCtx<'a>,
24 }
25
26 impl<'a> Ctx<'a> {
new(db: &'a dyn DefDatabase, file: HirFileId) -> Self27 pub(super) fn new(db: &'a dyn DefDatabase, file: HirFileId) -> Self {
28 Self {
29 db,
30 tree: ItemTree::default(),
31 source_ast_id_map: db.ast_id_map(file),
32 body_ctx: crate::lower::LowerCtx::with_file_id(db, file),
33 }
34 }
35
hygiene(&self) -> &Hygiene36 pub(super) fn hygiene(&self) -> &Hygiene {
37 self.body_ctx.hygiene()
38 }
39
lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree40 pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree {
41 self.tree.top_level =
42 item_owner.items().flat_map(|item| self.lower_mod_item(&item)).collect();
43 self.tree
44 }
45
lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree46 pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
47 self.tree.top_level = stmts
48 .statements()
49 .filter_map(|stmt| {
50 match stmt {
51 ast::Stmt::Item(item) => Some(item),
52 // Macro calls can be both items and expressions. The syntax library always treats
53 // them as expressions here, so we undo that.
54 ast::Stmt::ExprStmt(es) => match es.expr()? {
55 ast::Expr::MacroExpr(expr) => {
56 cov_mark::hit!(macro_call_in_macro_stmts_is_added_to_item_tree);
57 Some(expr.macro_call()?.into())
58 }
59 _ => None,
60 },
61 _ => None,
62 }
63 })
64 .flat_map(|item| self.lower_mod_item(&item))
65 .collect();
66
67 if let Some(ast::Expr::MacroExpr(tail_macro)) = stmts.expr() {
68 if let Some(call) = tail_macro.macro_call() {
69 cov_mark::hit!(macro_stmt_with_trailing_macro_expr);
70 if let Some(mod_item) = self.lower_mod_item(&call.into()) {
71 self.tree.top_level.push(mod_item);
72 }
73 }
74 }
75
76 self.tree
77 }
78
lower_block(mut self, block: &ast::BlockExpr) -> ItemTree79 pub(super) fn lower_block(mut self, block: &ast::BlockExpr) -> ItemTree {
80 self.tree.top_level = block
81 .statements()
82 .filter_map(|stmt| match stmt {
83 ast::Stmt::Item(item) => self.lower_mod_item(&item),
84 // Macro calls can be both items and expressions. The syntax library always treats
85 // them as expressions here, so we undo that.
86 ast::Stmt::ExprStmt(es) => match es.expr()? {
87 ast::Expr::MacroExpr(expr) => self.lower_mod_item(&expr.macro_call()?.into()),
88 _ => None,
89 },
90 _ => None,
91 })
92 .collect();
93 if let Some(ast::Expr::MacroExpr(expr)) = block.tail_expr() {
94 if let Some(call) = expr.macro_call() {
95 if let Some(mod_item) = self.lower_mod_item(&call.into()) {
96 self.tree.top_level.push(mod_item);
97 }
98 }
99 }
100
101 self.tree
102 }
103
data(&mut self) -> &mut ItemTreeData104 fn data(&mut self) -> &mut ItemTreeData {
105 self.tree.data_mut()
106 }
107
lower_mod_item(&mut self, item: &ast::Item) -> Option<ModItem>108 fn lower_mod_item(&mut self, item: &ast::Item) -> Option<ModItem> {
109 let attrs = RawAttrs::new(self.db.upcast(), item, self.hygiene());
110 let item: ModItem = match item {
111 ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
112 ast::Item::Union(ast) => self.lower_union(ast)?.into(),
113 ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
114 ast::Item::Fn(ast) => self.lower_function(ast)?.into(),
115 ast::Item::TypeAlias(ast) => self.lower_type_alias(ast)?.into(),
116 ast::Item::Static(ast) => self.lower_static(ast)?.into(),
117 ast::Item::Const(ast) => self.lower_const(ast).into(),
118 ast::Item::Module(ast) => self.lower_module(ast)?.into(),
119 ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
120 ast::Item::TraitAlias(ast) => self.lower_trait_alias(ast)?.into(),
121 ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
122 ast::Item::Use(ast) => self.lower_use(ast)?.into(),
123 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
124 ast::Item::MacroCall(ast) => self.lower_macro_call(ast)?.into(),
125 ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
126 ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
127 ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
128 };
129
130 self.add_attrs(item.into(), attrs);
131
132 Some(item)
133 }
134
add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs)135 fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
136 match self.tree.attrs.entry(item) {
137 Entry::Occupied(mut entry) => {
138 *entry.get_mut() = entry.get().merge(attrs);
139 }
140 Entry::Vacant(entry) => {
141 entry.insert(attrs);
142 }
143 }
144 }
145
lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem>146 fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem> {
147 match item {
148 ast::AssocItem::Fn(ast) => self.lower_function(ast).map(Into::into),
149 ast::AssocItem::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
150 ast::AssocItem::Const(ast) => Some(self.lower_const(ast).into()),
151 ast::AssocItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
152 }
153 }
154
lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>>155 fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
156 let visibility = self.lower_visibility(strukt);
157 let name = strukt.name()?.as_name();
158 let generic_params = self.lower_generic_params(HasImplicitSelf::No, strukt);
159 let fields = self.lower_fields(&strukt.kind());
160 let ast_id = self.source_ast_id_map.ast_id(strukt);
161 let res = Struct { name, visibility, generic_params, fields, ast_id };
162 Some(id(self.data().structs.alloc(res)))
163 }
164
lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields165 fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
166 match strukt_kind {
167 ast::StructKind::Record(it) => {
168 let range = self.lower_record_fields(it);
169 Fields::Record(range)
170 }
171 ast::StructKind::Tuple(it) => {
172 let range = self.lower_tuple_fields(it);
173 Fields::Tuple(range)
174 }
175 ast::StructKind::Unit => Fields::Unit,
176 }
177 }
178
lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdxRange<Field>179 fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdxRange<Field> {
180 let start = self.next_field_idx();
181 for field in fields.fields() {
182 if let Some(data) = self.lower_record_field(&field) {
183 let idx = self.data().fields.alloc(data);
184 self.add_attrs(idx.into(), RawAttrs::new(self.db.upcast(), &field, self.hygiene()));
185 }
186 }
187 let end = self.next_field_idx();
188 IdxRange::new(start..end)
189 }
190
lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field>191 fn lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field> {
192 let name = field.name()?.as_name();
193 let visibility = self.lower_visibility(field);
194 let type_ref = self.lower_type_ref_opt(field.ty());
195 let ast_id = FieldAstId::Record(self.source_ast_id_map.ast_id(field));
196 let res = Field { name, type_ref, visibility, ast_id };
197 Some(res)
198 }
199
lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdxRange<Field>200 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdxRange<Field> {
201 let start = self.next_field_idx();
202 for (i, field) in fields.fields().enumerate() {
203 let data = self.lower_tuple_field(i, &field);
204 let idx = self.data().fields.alloc(data);
205 self.add_attrs(idx.into(), RawAttrs::new(self.db.upcast(), &field, self.hygiene()));
206 }
207 let end = self.next_field_idx();
208 IdxRange::new(start..end)
209 }
210
lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field211 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
212 let name = Name::new_tuple_field(idx);
213 let visibility = self.lower_visibility(field);
214 let type_ref = self.lower_type_ref_opt(field.ty());
215 let ast_id = FieldAstId::Tuple(self.source_ast_id_map.ast_id(field));
216 Field { name, type_ref, visibility, ast_id }
217 }
218
lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>>219 fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
220 let visibility = self.lower_visibility(union);
221 let name = union.name()?.as_name();
222 let generic_params = self.lower_generic_params(HasImplicitSelf::No, union);
223 let fields = match union.record_field_list() {
224 Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
225 None => Fields::Record(IdxRange::new(self.next_field_idx()..self.next_field_idx())),
226 };
227 let ast_id = self.source_ast_id_map.ast_id(union);
228 let res = Union { name, visibility, generic_params, fields, ast_id };
229 Some(id(self.data().unions.alloc(res)))
230 }
231
lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>>232 fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
233 let visibility = self.lower_visibility(enum_);
234 let name = enum_.name()?.as_name();
235 let generic_params = self.lower_generic_params(HasImplicitSelf::No, enum_);
236 let variants = match &enum_.variant_list() {
237 Some(variant_list) => self.lower_variants(variant_list),
238 None => IdxRange::new(self.next_variant_idx()..self.next_variant_idx()),
239 };
240 let ast_id = self.source_ast_id_map.ast_id(enum_);
241 let res = Enum { name, visibility, generic_params, variants, ast_id };
242 Some(id(self.data().enums.alloc(res)))
243 }
244
lower_variants(&mut self, variants: &ast::VariantList) -> IdxRange<Variant>245 fn lower_variants(&mut self, variants: &ast::VariantList) -> IdxRange<Variant> {
246 let start = self.next_variant_idx();
247 for variant in variants.variants() {
248 if let Some(data) = self.lower_variant(&variant) {
249 let idx = self.data().variants.alloc(data);
250 self.add_attrs(
251 idx.into(),
252 RawAttrs::new(self.db.upcast(), &variant, self.hygiene()),
253 );
254 }
255 }
256 let end = self.next_variant_idx();
257 IdxRange::new(start..end)
258 }
259
lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant>260 fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
261 let name = variant.name()?.as_name();
262 let fields = self.lower_fields(&variant.kind());
263 let ast_id = self.source_ast_id_map.ast_id(variant);
264 let res = Variant { name, fields, ast_id };
265 Some(res)
266 }
267
lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>268 fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
269 let visibility = self.lower_visibility(func);
270 let name = func.name()?.as_name();
271
272 let mut has_self_param = false;
273 let start_param = self.next_param_idx();
274 if let Some(param_list) = func.param_list() {
275 if let Some(self_param) = param_list.self_param() {
276 let self_type = match self_param.ty() {
277 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
278 None => {
279 let self_type = TypeRef::Path(name![Self].into());
280 match self_param.kind() {
281 ast::SelfParamKind::Owned => self_type,
282 ast::SelfParamKind::Ref => TypeRef::Reference(
283 Box::new(self_type),
284 self_param.lifetime().as_ref().map(LifetimeRef::new),
285 Mutability::Shared,
286 ),
287 ast::SelfParamKind::MutRef => TypeRef::Reference(
288 Box::new(self_type),
289 self_param.lifetime().as_ref().map(LifetimeRef::new),
290 Mutability::Mut,
291 ),
292 }
293 }
294 };
295 let ty = Interned::new(self_type);
296 let idx = self.data().params.alloc(Param::Normal(ty));
297 self.add_attrs(
298 idx.into(),
299 RawAttrs::new(self.db.upcast(), &self_param, self.hygiene()),
300 );
301 has_self_param = true;
302 }
303 for param in param_list.params() {
304 let idx = match param.dotdotdot_token() {
305 Some(_) => self.data().params.alloc(Param::Varargs),
306 None => {
307 let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
308 let ty = Interned::new(type_ref);
309 self.data().params.alloc(Param::Normal(ty))
310 }
311 };
312 self.add_attrs(idx.into(), RawAttrs::new(self.db.upcast(), ¶m, self.hygiene()));
313 }
314 }
315 let end_param = self.next_param_idx();
316 let params = IdxRange::new(start_param..end_param);
317
318 let ret_type = match func.ret_type() {
319 Some(rt) => match rt.ty() {
320 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
321 None if rt.thin_arrow_token().is_some() => TypeRef::Error,
322 None => TypeRef::unit(),
323 },
324 None => TypeRef::unit(),
325 };
326
327 let ret_type = if func.async_token().is_some() {
328 let future_impl = desugar_future_path(ret_type);
329 let ty_bound = Interned::new(TypeBound::Path(future_impl, TraitBoundModifier::None));
330 TypeRef::ImplTrait(vec![ty_bound])
331 } else {
332 ret_type
333 };
334
335 let abi = func.abi().map(lower_abi);
336
337 let ast_id = self.source_ast_id_map.ast_id(func);
338
339 let mut flags = FnFlags::default();
340 if func.body().is_some() {
341 flags |= FnFlags::HAS_BODY;
342 }
343 if has_self_param {
344 flags |= FnFlags::HAS_SELF_PARAM;
345 }
346 if func.default_token().is_some() {
347 flags |= FnFlags::HAS_DEFAULT_KW;
348 }
349 if func.const_token().is_some() {
350 flags |= FnFlags::HAS_CONST_KW;
351 }
352 if func.async_token().is_some() {
353 flags |= FnFlags::HAS_ASYNC_KW;
354 }
355 if func.unsafe_token().is_some() {
356 flags |= FnFlags::HAS_UNSAFE_KW;
357 }
358
359 let mut res = Function {
360 name,
361 visibility,
362 explicit_generic_params: Interned::new(GenericParams::default()),
363 abi,
364 params,
365 ret_type: Interned::new(ret_type),
366 ast_id,
367 flags,
368 };
369 res.explicit_generic_params = self.lower_generic_params(HasImplicitSelf::No, func);
370
371 Some(id(self.data().functions.alloc(res)))
372 }
373
lower_type_alias( &mut self, type_alias: &ast::TypeAlias, ) -> Option<FileItemTreeId<TypeAlias>>374 fn lower_type_alias(
375 &mut self,
376 type_alias: &ast::TypeAlias,
377 ) -> Option<FileItemTreeId<TypeAlias>> {
378 let name = type_alias.name()?.as_name();
379 let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
380 let visibility = self.lower_visibility(type_alias);
381 let bounds = self.lower_type_bounds(type_alias);
382 let generic_params = self.lower_generic_params(HasImplicitSelf::No, type_alias);
383 let ast_id = self.source_ast_id_map.ast_id(type_alias);
384 let res = TypeAlias {
385 name,
386 visibility,
387 bounds: bounds.into_boxed_slice(),
388 generic_params,
389 type_ref,
390 ast_id,
391 };
392 Some(id(self.data().type_aliases.alloc(res)))
393 }
394
lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>>395 fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>> {
396 let name = static_.name()?.as_name();
397 let type_ref = self.lower_type_ref_opt(static_.ty());
398 let visibility = self.lower_visibility(static_);
399 let mutable = static_.mut_token().is_some();
400 let ast_id = self.source_ast_id_map.ast_id(static_);
401 let res = Static { name, visibility, mutable, type_ref, ast_id };
402 Some(id(self.data().statics.alloc(res)))
403 }
404
lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const>405 fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
406 let name = konst.name().map(|it| it.as_name());
407 let type_ref = self.lower_type_ref_opt(konst.ty());
408 let visibility = self.lower_visibility(konst);
409 let ast_id = self.source_ast_id_map.ast_id(konst);
410 let res = Const { name, visibility, type_ref, ast_id };
411 id(self.data().consts.alloc(res))
412 }
413
lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>>414 fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
415 let name = module.name()?.as_name();
416 let visibility = self.lower_visibility(module);
417 let kind = if module.semicolon_token().is_some() {
418 ModKind::Outline
419 } else {
420 ModKind::Inline {
421 items: module
422 .item_list()
423 .map(|list| list.items().flat_map(|item| self.lower_mod_item(&item)).collect())
424 .unwrap_or_else(|| {
425 cov_mark::hit!(name_res_works_for_broken_modules);
426 Box::new([]) as Box<[_]>
427 }),
428 }
429 };
430 let ast_id = self.source_ast_id_map.ast_id(module);
431 let res = Mod { name, visibility, kind, ast_id };
432 Some(id(self.data().mods.alloc(res)))
433 }
434
lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>>435 fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
436 let name = trait_def.name()?.as_name();
437 let visibility = self.lower_visibility(trait_def);
438 let generic_params =
439 self.lower_generic_params(HasImplicitSelf::Yes(trait_def.type_bound_list()), trait_def);
440 let is_auto = trait_def.auto_token().is_some();
441 let is_unsafe = trait_def.unsafe_token().is_some();
442 let ast_id = self.source_ast_id_map.ast_id(trait_def);
443
444 let items = trait_def
445 .assoc_item_list()
446 .into_iter()
447 .flat_map(|list| list.assoc_items())
448 .filter_map(|item| {
449 let attrs = RawAttrs::new(self.db.upcast(), &item, self.hygiene());
450 self.lower_assoc_item(&item).map(|item| {
451 self.add_attrs(ModItem::from(item).into(), attrs);
452 item
453 })
454 })
455 .collect();
456
457 let def = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
458 Some(id(self.data().traits.alloc(def)))
459 }
460
lower_trait_alias( &mut self, trait_alias_def: &ast::TraitAlias, ) -> Option<FileItemTreeId<TraitAlias>>461 fn lower_trait_alias(
462 &mut self,
463 trait_alias_def: &ast::TraitAlias,
464 ) -> Option<FileItemTreeId<TraitAlias>> {
465 let name = trait_alias_def.name()?.as_name();
466 let visibility = self.lower_visibility(trait_alias_def);
467 let generic_params = self.lower_generic_params(
468 HasImplicitSelf::Yes(trait_alias_def.type_bound_list()),
469 trait_alias_def,
470 );
471 let ast_id = self.source_ast_id_map.ast_id(trait_alias_def);
472
473 let alias = TraitAlias { name, visibility, generic_params, ast_id };
474 Some(id(self.data().trait_aliases.alloc(alias)))
475 }
476
lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>>477 fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
478 // Note that trait impls don't get implicit `Self` unlike traits, because here they are a
479 // type alias rather than a type parameter, so this is handled by the resolver.
480 let generic_params = self.lower_generic_params(HasImplicitSelf::No, impl_def);
481 // FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
482 // as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
483 // equals itself.
484 let target_trait = impl_def.trait_().and_then(|tr| self.lower_trait_ref(&tr));
485 let self_ty = self.lower_type_ref(&impl_def.self_ty()?);
486 let is_negative = impl_def.excl_token().is_some();
487
488 // We cannot use `assoc_items()` here as that does not include macro calls.
489 let items = impl_def
490 .assoc_item_list()
491 .into_iter()
492 .flat_map(|it| it.assoc_items())
493 .filter_map(|item| {
494 let assoc = self.lower_assoc_item(&item)?;
495 let attrs = RawAttrs::new(self.db.upcast(), &item, self.hygiene());
496 self.add_attrs(ModItem::from(assoc).into(), attrs);
497 Some(assoc)
498 })
499 .collect();
500 let ast_id = self.source_ast_id_map.ast_id(impl_def);
501 let res = Impl { generic_params, target_trait, self_ty, is_negative, items, ast_id };
502 Some(id(self.data().impls.alloc(res)))
503 }
504
lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>>505 fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>> {
506 let visibility = self.lower_visibility(use_item);
507 let ast_id = self.source_ast_id_map.ast_id(use_item);
508 let (use_tree, _) = lower_use_tree(self.db, self.hygiene(), use_item.use_tree()?)?;
509
510 let res = Import { visibility, ast_id, use_tree };
511 Some(id(self.data().imports.alloc(res)))
512 }
513
lower_extern_crate( &mut self, extern_crate: &ast::ExternCrate, ) -> Option<FileItemTreeId<ExternCrate>>514 fn lower_extern_crate(
515 &mut self,
516 extern_crate: &ast::ExternCrate,
517 ) -> Option<FileItemTreeId<ExternCrate>> {
518 let name = extern_crate.name_ref()?.as_name();
519 let alias = extern_crate.rename().map(|a| {
520 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
521 });
522 let visibility = self.lower_visibility(extern_crate);
523 let ast_id = self.source_ast_id_map.ast_id(extern_crate);
524
525 let res = ExternCrate { name, alias, visibility, ast_id };
526 Some(id(self.data().extern_crates.alloc(res)))
527 }
528
lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>>529 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
530 let path = Interned::new(ModPath::from_src(self.db.upcast(), m.path()?, self.hygiene())?);
531 let ast_id = self.source_ast_id_map.ast_id(m);
532 let expand_to = hir_expand::ExpandTo::from_call_site(m);
533 let res = MacroCall { path, ast_id, expand_to };
534 Some(id(self.data().macro_calls.alloc(res)))
535 }
536
lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>>537 fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
538 let name = m.name().map(|it| it.as_name())?;
539 let ast_id = self.source_ast_id_map.ast_id(m);
540
541 let res = MacroRules { name, ast_id };
542 Some(id(self.data().macro_rules.alloc(res)))
543 }
544
lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>>545 fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
546 let name = m.name().map(|it| it.as_name())?;
547
548 let ast_id = self.source_ast_id_map.ast_id(m);
549 let visibility = self.lower_visibility(m);
550
551 let res = MacroDef { name, ast_id, visibility };
552 Some(id(self.data().macro_defs.alloc(res)))
553 }
554
lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock>555 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock> {
556 let ast_id = self.source_ast_id_map.ast_id(block);
557 let abi = block.abi().map(lower_abi);
558 let children: Box<[_]> = block.extern_item_list().map_or(Box::new([]), |list| {
559 list.extern_items()
560 .filter_map(|item| {
561 // Note: All items in an `extern` block need to be lowered as if they're outside of one
562 // (in other words, the knowledge that they're in an extern block must not be used).
563 // This is because an extern block can contain macros whose ItemTree's top-level items
564 // should be considered to be in an extern block too.
565 let attrs = RawAttrs::new(self.db.upcast(), &item, self.hygiene());
566 let id: ModItem = match item {
567 ast::ExternItem::Fn(ast) => self.lower_function(&ast)?.into(),
568 ast::ExternItem::Static(ast) => self.lower_static(&ast)?.into(),
569 ast::ExternItem::TypeAlias(ty) => self.lower_type_alias(&ty)?.into(),
570 ast::ExternItem::MacroCall(call) => self.lower_macro_call(&call)?.into(),
571 };
572 self.add_attrs(id.into(), attrs);
573 Some(id)
574 })
575 .collect()
576 });
577
578 let res = ExternBlock { abi, ast_id, children };
579 id(self.data().extern_blocks.alloc(res))
580 }
581
lower_generic_params( &mut self, has_implicit_self: HasImplicitSelf, node: &dyn ast::HasGenericParams, ) -> Interned<GenericParams>582 fn lower_generic_params(
583 &mut self,
584 has_implicit_self: HasImplicitSelf,
585 node: &dyn ast::HasGenericParams,
586 ) -> Interned<GenericParams> {
587 let mut generics = GenericParams::default();
588
589 if let HasImplicitSelf::Yes(bounds) = has_implicit_self {
590 // Traits and trait aliases get the Self type as an implicit first type parameter.
591 generics.type_or_consts.alloc(
592 TypeParamData {
593 name: Some(name![Self]),
594 default: None,
595 provenance: TypeParamProvenance::TraitSelf,
596 }
597 .into(),
598 );
599 // add super traits as bounds on Self
600 // i.e., `trait Foo: Bar` is equivalent to `trait Foo where Self: Bar`
601 let self_param = TypeRef::Path(name![Self].into());
602 generics.fill_bounds(&self.body_ctx, bounds, Either::Left(self_param));
603 }
604
605 generics.fill(&self.body_ctx, node);
606
607 generics.shrink_to_fit();
608 Interned::new(generics)
609 }
610
lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>>611 fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>> {
612 match node.type_bound_list() {
613 Some(bound_list) => bound_list
614 .bounds()
615 .map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it)))
616 .collect(),
617 None => Vec::new(),
618 }
619 }
620
lower_visibility(&mut self, item: &dyn ast::HasVisibility) -> RawVisibilityId621 fn lower_visibility(&mut self, item: &dyn ast::HasVisibility) -> RawVisibilityId {
622 let vis = RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), self.hygiene());
623 self.data().vis.alloc(vis)
624 }
625
lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>>626 fn lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>> {
627 let trait_ref = TraitRef::from_ast(&self.body_ctx, trait_ref.clone())?;
628 Some(Interned::new(trait_ref))
629 }
630
lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef>631 fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef> {
632 let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone());
633 Interned::new(tyref)
634 }
635
lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef>636 fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef> {
637 match type_ref.map(|ty| self.lower_type_ref(&ty)) {
638 Some(it) => it,
639 None => Interned::new(TypeRef::Error),
640 }
641 }
642
next_field_idx(&self) -> Idx<Field>643 fn next_field_idx(&self) -> Idx<Field> {
644 Idx::from_raw(RawIdx::from(
645 self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
646 ))
647 }
next_variant_idx(&self) -> Idx<Variant>648 fn next_variant_idx(&self) -> Idx<Variant> {
649 Idx::from_raw(RawIdx::from(
650 self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
651 ))
652 }
next_param_idx(&self) -> Idx<Param>653 fn next_param_idx(&self) -> Idx<Param> {
654 Idx::from_raw(RawIdx::from(
655 self.tree.data.as_ref().map_or(0, |data| data.params.len() as u32),
656 ))
657 }
658 }
659
desugar_future_path(orig: TypeRef) -> Path660 fn desugar_future_path(orig: TypeRef) -> Path {
661 let path = path![core::future::Future];
662 let mut generic_args: Vec<_> =
663 std::iter::repeat(None).take(path.segments().len() - 1).collect();
664 let binding = AssociatedTypeBinding {
665 name: name![Output],
666 args: None,
667 type_ref: Some(orig),
668 bounds: Box::default(),
669 };
670 generic_args.push(Some(Interned::new(GenericArgs {
671 bindings: Box::new([binding]),
672 ..GenericArgs::empty()
673 })));
674
675 Path::from_known_path(path, generic_args)
676 }
677
678 enum HasImplicitSelf {
679 /// Inner list is a type bound list for the implicit `Self`.
680 Yes(Option<ast::TypeBoundList>),
681 No,
682 }
683
lower_abi(abi: ast::Abi) -> Interned<str>684 fn lower_abi(abi: ast::Abi) -> Interned<str> {
685 // FIXME: Abi::abi() -> Option<SyntaxToken>?
686 match abi.syntax().last_token() {
687 Some(tok) if tok.kind() == SyntaxKind::STRING => {
688 // FIXME: Better way to unescape?
689 Interned::new_str(tok.text().trim_matches('"'))
690 }
691 _ => {
692 // `extern` default to be `extern "C"`.
693 Interned::new_str("C")
694 }
695 }
696 }
697
698 struct UseTreeLowering<'a> {
699 db: &'a dyn DefDatabase,
700 hygiene: &'a Hygiene,
701 mapping: Arena<ast::UseTree>,
702 }
703
704 impl UseTreeLowering<'_> {
lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree>705 fn lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree> {
706 if let Some(use_tree_list) = tree.use_tree_list() {
707 let prefix = match tree.path() {
708 // E.g. use something::{{{inner}}};
709 None => None,
710 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
711 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
712 Some(path) => {
713 match ModPath::from_src(self.db.upcast(), path, self.hygiene) {
714 Some(it) => Some(it),
715 None => return None, // FIXME: report errors somewhere
716 }
717 }
718 };
719
720 let list =
721 use_tree_list.use_trees().filter_map(|tree| self.lower_use_tree(tree)).collect();
722
723 Some(
724 self.use_tree(
725 UseTreeKind::Prefixed { prefix: prefix.map(Interned::new), list },
726 tree,
727 ),
728 )
729 } else {
730 let is_glob = tree.star_token().is_some();
731 let path = match tree.path() {
732 Some(path) => Some(ModPath::from_src(self.db.upcast(), path, self.hygiene)?),
733 None => None,
734 };
735 let alias = tree.rename().map(|a| {
736 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
737 });
738 if alias.is_some() && is_glob {
739 return None;
740 }
741
742 match (path, alias, is_glob) {
743 (path, None, true) => {
744 if path.is_none() {
745 cov_mark::hit!(glob_enum_group);
746 }
747 Some(self.use_tree(UseTreeKind::Glob { path: path.map(Interned::new) }, tree))
748 }
749 // Globs can't be renamed
750 (_, Some(_), true) | (None, None, false) => None,
751 // `bla::{ as Name}` is invalid
752 (None, Some(_), false) => None,
753 (Some(path), alias, false) => Some(
754 self.use_tree(UseTreeKind::Single { path: Interned::new(path), alias }, tree),
755 ),
756 }
757 }
758 }
759
use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree760 fn use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree {
761 let index = self.mapping.alloc(ast);
762 UseTree { index, kind }
763 }
764 }
765
lower_use_tree( db: &dyn DefDatabase, hygiene: &Hygiene, tree: ast::UseTree, ) -> Option<(UseTree, Arena<ast::UseTree>)>766 pub(super) fn lower_use_tree(
767 db: &dyn DefDatabase,
768 hygiene: &Hygiene,
769 tree: ast::UseTree,
770 ) -> Option<(UseTree, Arena<ast::UseTree>)> {
771 let mut lowering = UseTreeLowering { db, hygiene, mapping: Arena::new() };
772 let tree = lowering.lower_use_tree(tree)?;
773 Some((tree, lowering.mapping))
774 }
775