aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/item_tree/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/item_tree/lower.rs')
-rw-r--r--crates/hir_def/src/item_tree/lower.rs53
1 files changed, 23 insertions, 30 deletions
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index b39d7fb7a..c8f090c22 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -10,7 +10,6 @@ use syntax::{
10}; 10};
11 11
12use crate::{ 12use crate::{
13 attr::Attrs,
14 generics::{GenericParams, TypeParamData, TypeParamProvenance}, 13 generics::{GenericParams, TypeParamData, TypeParamProvenance},
15 type_ref::LifetimeRef, 14 type_ref::LifetimeRef,
16}; 15};
@@ -101,10 +100,11 @@ impl Ctx {
101 | ast::Item::ExternCrate(_) 100 | ast::Item::ExternCrate(_)
102 | ast::Item::Use(_) 101 | ast::Item::Use(_)
103 | ast::Item::MacroCall(_) 102 | ast::Item::MacroCall(_)
104 | ast::Item::MacroRules(_) => {} 103 | ast::Item::MacroRules(_)
104 | ast::Item::MacroDef(_) => {}
105 }; 105 };
106 106
107 let attrs = Attrs::new(item, &self.hygiene); 107 let attrs = RawAttrs::new(item, &self.hygiene);
108 let items = match item { 108 let items = match item {
109 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into), 109 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
110 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into), 110 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
@@ -122,6 +122,7 @@ impl Ctx {
122 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into), 122 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
123 ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into), 123 ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
124 ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into), 124 ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into),
125 ast::Item::MacroDef(ast) => self.lower_macro_def(ast).map(Into::into),
125 ast::Item::ExternBlock(ast) => { 126 ast::Item::ExternBlock(ast) => {
126 Some(ModItems(self.lower_extern_block(ast).into_iter().collect::<SmallVec<_>>())) 127 Some(ModItems(self.lower_extern_block(ast).into_iter().collect::<SmallVec<_>>()))
127 } 128 }
@@ -136,7 +137,7 @@ impl Ctx {
136 items 137 items
137 } 138 }
138 139
139 fn add_attrs(&mut self, item: AttrOwner, attrs: Attrs) { 140 fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
140 match self.tree.attrs.entry(item) { 141 match self.tree.attrs.entry(item) {
141 Entry::Occupied(mut entry) => { 142 Entry::Occupied(mut entry) => {
142 *entry.get_mut() = entry.get().merge(attrs); 143 *entry.get_mut() = entry.get().merge(attrs);
@@ -203,7 +204,7 @@ impl Ctx {
203 for field in fields.fields() { 204 for field in fields.fields() {
204 if let Some(data) = self.lower_record_field(&field) { 205 if let Some(data) = self.lower_record_field(&field) {
205 let idx = self.data().fields.alloc(data); 206 let idx = self.data().fields.alloc(data);
206 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene)); 207 self.add_attrs(idx.into(), RawAttrs::new(&field, &self.hygiene));
207 } 208 }
208 } 209 }
209 let end = self.next_field_idx(); 210 let end = self.next_field_idx();
@@ -223,7 +224,7 @@ impl Ctx {
223 for (i, field) in fields.fields().enumerate() { 224 for (i, field) in fields.fields().enumerate() {
224 let data = self.lower_tuple_field(i, &field); 225 let data = self.lower_tuple_field(i, &field);
225 let idx = self.data().fields.alloc(data); 226 let idx = self.data().fields.alloc(data);
226 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene)); 227 self.add_attrs(idx.into(), RawAttrs::new(&field, &self.hygiene));
227 } 228 }
228 let end = self.next_field_idx(); 229 let end = self.next_field_idx();
229 IdRange::new(start..end) 230 IdRange::new(start..end)
@@ -268,7 +269,7 @@ impl Ctx {
268 for variant in variants.variants() { 269 for variant in variants.variants() {
269 if let Some(data) = self.lower_variant(&variant) { 270 if let Some(data) = self.lower_variant(&variant) {
270 let idx = self.data().variants.alloc(data); 271 let idx = self.data().variants.alloc(data);
271 self.add_attrs(idx.into(), Attrs::new(&variant, &self.hygiene)); 272 self.add_attrs(idx.into(), RawAttrs::new(&variant, &self.hygiene));
272 } 273 }
273 } 274 }
274 let end = self.next_variant_idx(); 275 let end = self.next_variant_idx();
@@ -298,12 +299,12 @@ impl Ctx {
298 ast::SelfParamKind::Owned => self_type, 299 ast::SelfParamKind::Owned => self_type,
299 ast::SelfParamKind::Ref => TypeRef::Reference( 300 ast::SelfParamKind::Ref => TypeRef::Reference(
300 Box::new(self_type), 301 Box::new(self_type),
301 self_param.lifetime_token().map(LifetimeRef::from_token), 302 self_param.lifetime().as_ref().map(LifetimeRef::new),
302 Mutability::Shared, 303 Mutability::Shared,
303 ), 304 ),
304 ast::SelfParamKind::MutRef => TypeRef::Reference( 305 ast::SelfParamKind::MutRef => TypeRef::Reference(
305 Box::new(self_type), 306 Box::new(self_type),
306 self_param.lifetime_token().map(LifetimeRef::from_token), 307 self_param.lifetime().as_ref().map(LifetimeRef::new),
307 Mutability::Mut, 308 Mutability::Mut,
308 ), 309 ),
309 } 310 }
@@ -436,7 +437,7 @@ impl Ctx {
436 self.with_inherited_visibility(visibility, |this| { 437 self.with_inherited_visibility(visibility, |this| {
437 list.assoc_items() 438 list.assoc_items()
438 .filter_map(|item| { 439 .filter_map(|item| {
439 let attrs = Attrs::new(&item, &this.hygiene); 440 let attrs = RawAttrs::new(&item, &this.hygiene);
440 this.collect_inner_items(item.syntax()); 441 this.collect_inner_items(item.syntax());
441 this.lower_assoc_item(&item).map(|item| { 442 this.lower_assoc_item(&item).map(|item| {
442 this.add_attrs(ModItem::from(item).into(), attrs); 443 this.add_attrs(ModItem::from(item).into(), attrs);
@@ -473,7 +474,7 @@ impl Ctx {
473 .filter_map(|item| { 474 .filter_map(|item| {
474 self.collect_inner_items(item.syntax()); 475 self.collect_inner_items(item.syntax());
475 let assoc = self.lower_assoc_item(&item)?; 476 let assoc = self.lower_assoc_item(&item)?;
476 let attrs = Attrs::new(&item, &self.hygiene); 477 let attrs = RawAttrs::new(&item, &self.hygiene);
477 self.add_attrs(ModItem::from(assoc).into(), attrs); 478 self.add_attrs(ModItem::from(assoc).into(), attrs);
478 Some(assoc) 479 Some(assoc)
479 }) 480 })
@@ -537,28 +538,20 @@ impl Ctx {
537 538
538 fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> { 539 fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
539 let name = m.name().map(|it| it.as_name())?; 540 let name = m.name().map(|it| it.as_name())?;
540 let attrs = Attrs::new(m, &self.hygiene);
541
542 let ast_id = self.source_ast_id_map.ast_id(m); 541 let ast_id = self.source_ast_id_map.ast_id(m);
543 542
544 // FIXME: cfg_attr 543 let res = MacroRules { name, ast_id };
545 let export_attr = attrs.by_key("macro_export"); 544 Some(id(self.data().macro_rules.alloc(res)))
545 }
546 546
547 let is_export = export_attr.exists(); 547 fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
548 let is_local_inner = if is_export { 548 let name = m.name().map(|it| it.as_name())?;
549 export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
550 tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
551 ident.text.contains("local_inner_macros")
552 }
553 _ => false,
554 })
555 } else {
556 false
557 };
558 549
559 let is_builtin = attrs.by_key("rustc_builtin_macro").exists(); 550 let ast_id = self.source_ast_id_map.ast_id(m);
560 let res = MacroRules { name, is_export, is_builtin, is_local_inner, ast_id }; 551 let visibility = self.lower_visibility(m);
561 Some(id(self.data().macro_rules.alloc(res))) 552
553 let res = MacroDef { name, ast_id, visibility };
554 Some(id(self.data().macro_defs.alloc(res)))
562 } 555 }
563 556
564 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> { 557 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> {
@@ -566,7 +559,7 @@ impl Ctx {
566 list.extern_items() 559 list.extern_items()
567 .filter_map(|item| { 560 .filter_map(|item| {
568 self.collect_inner_items(item.syntax()); 561 self.collect_inner_items(item.syntax());
569 let attrs = Attrs::new(&item, &self.hygiene); 562 let attrs = RawAttrs::new(&item, &self.hygiene);
570 let id: ModItem = match item { 563 let id: ModItem = match item {
571 ast::ExternItem::Fn(ast) => { 564 ast::ExternItem::Fn(ast) => {
572 let func_id = self.lower_function(&ast)?; 565 let func_id = self.lower_function(&ast)?;