Add MacroDefKind

This commit is contained in:
Edwin Cheng 2019-11-11 18:45:55 +08:00
parent c4aa8b63bc
commit 4f7df2aac1
6 changed files with 31 additions and 53 deletions

View file

@ -6,8 +6,8 @@ use crate::{
adt::VariantDef, adt::VariantDef,
db::{AstDatabase, DefDatabase, HirDatabase}, db::{AstDatabase, DefDatabase, HirDatabase},
ids::AstItemDef, ids::AstItemDef,
Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module,
MacroDefId, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
}; };
pub use hir_expand::Source; pub use hir_expand::Source;
@ -140,15 +140,10 @@ impl HasSource for TypeAlias {
self.id.source(db) self.id.source(db)
} }
} }
impl HasSource for MacroDef { impl HasSource for MacroDef {
type Ast = ast::MacroCall; type Ast = ast::MacroCall;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> { fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> {
let ast_id = match self.id { Source { file_id: self.id.ast_id.file_id(), ast: self.id.ast_id.to_node(db) }
MacroDefId::DeclarativeMacro(it) => it.ast_id,
MacroDefId::BuiltinMacro(it) => it.ast_id,
};
Source { file_id: ast_id.file_id(), ast: ast_id.to_node(db) }
} }
} }

View file

@ -3,7 +3,7 @@
use hir_expand::{ use hir_expand::{
builtin_macro::find_builtin_macro, builtin_macro::find_builtin_macro,
name::{self, AsName, Name}, name::{self, AsName, Name},
DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind,
}; };
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::{CrateId, FileId}; use ra_db::{CrateId, FileId};
@ -708,13 +708,12 @@ where
// Case 1: macro rules, define a macro in crate-global mutable scope // Case 1: macro rules, define a macro in crate-global mutable scope
if is_macro_rules(&mac.path) { if is_macro_rules(&mac.path) {
if let Some(name) = &mac.name { if let Some(name) = &mac.name {
let macro_id = DeclarativeMacro { ast_id, krate: self.def_collector.def_map.krate }; let macro_id = MacroDefId {
self.def_collector.define_macro( ast_id,
self.module_id, krate: self.def_collector.def_map.krate,
name.clone(), kind: MacroDefKind::Declarative,
MacroDefId::DeclarativeMacro(macro_id), };
mac.export, self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export);
);
} }
return; return;
} }

View file

@ -2,7 +2,7 @@
use crate::db::AstDatabase; use crate::db::AstDatabase;
use crate::{ use crate::{
ast::{self, AstNode}, ast::{self, AstNode},
name, AstId, BuiltinMacro, CrateId, HirFileId, MacroCallId, MacroDefId, MacroFileKind, name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
TextUnit, TextUnit,
}; };
@ -33,11 +33,7 @@ pub fn find_builtin_macro(
) -> Option<MacroDefId> { ) -> Option<MacroDefId> {
// FIXME: Better registering method // FIXME: Better registering method
if ident == &name::LINE_MACRO { if ident == &name::LINE_MACRO {
Some(MacroDefId::BuiltinMacro(BuiltinMacro { Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
expander: BuiltinExpander::Line,
krate,
ast_id,
}))
} else { } else {
None None
} }

View file

@ -10,7 +10,7 @@ use ra_syntax::{AstNode, Parse, SyntaxNode};
use crate::{ use crate::{
ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc,
MacroDefId, MacroFile, MacroFileKind, MacroDefId, MacroDefKind, MacroFile, MacroFileKind,
}; };
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
@ -69,9 +69,9 @@ pub(crate) fn macro_def(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroDefId, id: MacroDefId,
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> { ) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
match id { match id.kind {
MacroDefId::DeclarativeMacro(it) => { MacroDefKind::Declarative => {
let macro_call = it.ast_id.to_node(db); let macro_call = id.ast_id.to_node(db);
let arg = macro_call.token_tree()?; let arg = macro_call.token_tree()?;
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
log::warn!("fail on macro_def to token tree: {:#?}", arg); log::warn!("fail on macro_def to token tree: {:#?}", arg);
@ -83,8 +83,8 @@ pub(crate) fn macro_def(
})?; })?;
Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
} }
MacroDefId::BuiltinMacro(it) => { MacroDefKind::BuiltIn(expander) => {
Some(Arc::new((TokenExpander::Builtin(it.expander.clone()), mbe::TokenMap::default()))) Some(Arc::new((TokenExpander::Builtin(expander.clone()), mbe::TokenMap::default())))
} }
} }
} }

View file

@ -9,7 +9,7 @@ use crate::{
db::AstDatabase, db::AstDatabase,
either::Either, either::Either,
name::{AsName, Name}, name::{AsName, Name},
HirFileId, HirFileIdRepr, MacroDefId, HirFileId, HirFileIdRepr, MacroDefKind,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -24,9 +24,9 @@ impl Hygiene {
HirFileIdRepr::FileId(_) => None, HirFileIdRepr::FileId(_) => None,
HirFileIdRepr::MacroFile(macro_file) => { HirFileIdRepr::MacroFile(macro_file) => {
let loc = db.lookup_intern_macro(macro_file.macro_call_id); let loc = db.lookup_intern_macro(macro_file.macro_call_id);
match loc.def { match loc.def.kind {
MacroDefId::DeclarativeMacro(it) => Some(it.krate), MacroDefKind::Declarative => Some(loc.def.krate),
MacroDefId::BuiltinMacro(_) => None, MacroDefKind::BuiltIn(_) => None,
} }
} }
}; };

View file

@ -78,15 +78,9 @@ impl HirFileId {
HirFileIdRepr::MacroFile(macro_file) => { HirFileIdRepr::MacroFile(macro_file) => {
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
// FIXME: Do we support expansion information in builtin macro?
let macro_decl = match loc.def {
MacroDefId::DeclarativeMacro(it) => (it),
MacroDefId::BuiltinMacro(_) => return None,
};
let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start();
let def_start = let def_start =
macro_decl.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start();
let macro_def = db.macro_def(loc.def)?; let macro_def = db.macro_def(loc.def)?;
let shift = macro_def.0.shift(); let shift = macro_def.0.shift();
@ -94,7 +88,7 @@ impl HirFileId {
let macro_arg = db.macro_arg(macro_file.macro_call_id)?; let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
let arg_start = (loc.ast_id.file_id, arg_start); let arg_start = (loc.ast_id.file_id, arg_start);
let def_start = (macro_decl.ast_id.file_id, def_start); let def_start = (loc.def.ast_id.file_id, def_start);
Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift })
} }
@ -128,22 +122,16 @@ impl salsa::InternKey for MacroCallId {
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroDefId { pub struct MacroDefId {
DeclarativeMacro(DeclarativeMacro), pub krate: CrateId,
BuiltinMacro(BuiltinMacro), pub ast_id: AstId<ast::MacroCall>,
pub kind: MacroDefKind,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DeclarativeMacro { pub enum MacroDefKind {
pub krate: CrateId, Declarative,
pub ast_id: AstId<ast::MacroCall>, BuiltIn(BuiltinExpander),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BuiltinMacro {
pub krate: CrateId,
pub ast_id: AstId<ast::MacroCall>,
pub expander: BuiltinExpander,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]