From c46768d13dd34bbe878cc62eca4af873ffbb7c22 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 10 Nov 2019 11:03:24 +0800 Subject: Add basic bultin macro infrastructure --- crates/ra_hir_def/src/nameres/collector.rs | 28 +++++++++++++++++++++++++--- crates/ra_hir_def/src/nameres/raw.rs | 7 ++++++- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index aacd50df8..5f18e9de3 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -1,8 +1,9 @@ //! FIXME: write short doc here use hir_expand::{ + builtin_macro::find_builtin_macro, name::{self, AsName, Name}, - HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, + DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, }; use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId}; @@ -688,11 +689,32 @@ where fn collect_macro(&mut self, mac: &raw::MacroData) { let ast_id = AstId::new(self.file_id, mac.ast_id); + // Case 0: builtin macros + if mac.builtin { + if let Some(name) = &mac.name { + let krate = self.def_collector.def_map.krate; + if let Some(macro_id) = find_builtin_macro(name, krate, ast_id) { + self.def_collector.define_macro( + self.module_id, + name.clone(), + macro_id, + mac.export, + ); + return; + } + } + } + // Case 1: macro rules, define a macro in crate-global mutable scope if is_macro_rules(&mac.path) { if let Some(name) = &mac.name { - let macro_id = MacroDefId { ast_id, krate: self.def_collector.def_map.krate }; - self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); + let macro_id = DeclarativeMacro { ast_id, krate: self.def_collector.def_map.krate }; + self.def_collector.define_macro( + self.module_id, + name.clone(), + MacroDefId::DeclarativeMacro(macro_id), + mac.export, + ); } return; } diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 369376f30..f52002bc0 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -200,6 +200,7 @@ pub(super) struct MacroData { pub(super) path: Path, pub(super) name: Option, pub(super) export: bool, + pub(super) builtin: bool, } struct RawItemsCollector { @@ -367,7 +368,11 @@ impl RawItemsCollector { // FIXME: cfg_attr let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export"); - let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); + // FIXME: cfg_attr + let builtin = + m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro"); + + let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin }); self.push_item(current_module, attrs, RawItemKind::Macro(m)); } -- cgit v1.2.3