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

View file

@ -3,7 +3,7 @@
use hir_expand::{
builtin_macro::find_builtin_macro,
name::{self, AsName, Name},
DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind,
HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind,
};
use ra_cfg::CfgOptions;
use ra_db::{CrateId, FileId};
@ -708,13 +708,12 @@ where
// 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 = 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,
);
let macro_id = MacroDefId {
ast_id,
krate: self.def_collector.def_map.krate,
kind: MacroDefKind::Declarative,
};
self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export);
}
return;
}

View file

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

View file

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

View file

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

View file

@ -78,15 +78,9 @@ impl HirFileId {
HirFileIdRepr::MacroFile(macro_file) => {
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 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 shift = macro_def.0.shift();
@ -94,7 +88,7 @@ impl HirFileId {
let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
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 })
}
@ -128,22 +122,16 @@ impl salsa::InternKey for MacroCallId {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroDefId {
DeclarativeMacro(DeclarativeMacro),
BuiltinMacro(BuiltinMacro),
pub struct MacroDefId {
pub krate: CrateId,
pub ast_id: AstId<ast::MacroCall>,
pub kind: MacroDefKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DeclarativeMacro {
pub krate: CrateId,
pub ast_id: AstId<ast::MacroCall>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BuiltinMacro {
pub krate: CrateId,
pub ast_id: AstId<ast::MacroCall>,
pub expander: BuiltinExpander,
pub enum MacroDefKind {
Declarative,
BuiltIn(BuiltinExpander),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]