Add basic bultin macro infrastructure
This commit is contained in:
parent
defc7ad772
commit
c46768d13d
@ -6,8 +6,8 @@ use crate::{
|
||||
adt::VariantDef,
|
||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||
ids::AstItemDef,
|
||||
Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module,
|
||||
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||
Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef,
|
||||
MacroDefId, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||
};
|
||||
|
||||
pub use hir_expand::Source;
|
||||
@ -140,10 +140,15 @@ 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> {
|
||||
Source { file_id: self.id.ast_id.file_id(), ast: self.id.ast_id.to_node(db) }
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -200,6 +200,7 @@ pub(super) struct MacroData {
|
||||
pub(super) path: Path,
|
||||
pub(super) name: Option<Name>,
|
||||
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));
|
||||
}
|
||||
|
||||
|
26
crates/ra_hir_expand/src/builtin_macro.rs
Normal file
26
crates/ra_hir_expand/src/builtin_macro.rs
Normal file
@ -0,0 +1,26 @@
|
||||
//! Builtin macro
|
||||
use crate::{ast, name, AstId, BuiltinMacro, CrateId, MacroDefId};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinExpander {
|
||||
Line
|
||||
}
|
||||
|
||||
impl BuiltinExpander {
|
||||
pub fn expand(&self, _tt: &tt::Subtree) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
Err(mbe::ExpandError::UnexpectedToken)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_builtin_macro(
|
||||
ident: &name::Name,
|
||||
krate: CrateId,
|
||||
ast_id: AstId<ast::MacroCall>,
|
||||
) -> Option<MacroDefId> {
|
||||
// FIXME: Better registering method
|
||||
if ident == &name::LINE {
|
||||
Some(MacroDefId::BuiltinMacro(BuiltinMacro { expander: BuiltinExpander::Line, krate, ast_id }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
@ -9,10 +9,37 @@ use ra_prof::profile;
|
||||
use ra_syntax::{AstNode, Parse, SyntaxNode};
|
||||
|
||||
use crate::{
|
||||
ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId,
|
||||
MacroFile, MacroFileKind,
|
||||
ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc,
|
||||
MacroDefId, MacroFile, MacroFileKind,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub enum TokenExpander {
|
||||
MacroRules(mbe::MacroRules),
|
||||
Builtin(BuiltinExpander),
|
||||
}
|
||||
|
||||
impl TokenExpander {
|
||||
pub fn expand(
|
||||
&self,
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroCallId,
|
||||
tt: &tt::Subtree,
|
||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
match self {
|
||||
TokenExpander::MacroRules(it) => it.expand(tt),
|
||||
TokenExpander::Builtin(it) => it.expand(tt),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shift(&self) -> u32 {
|
||||
match self {
|
||||
TokenExpander::MacroRules(it) => it.shift(),
|
||||
TokenExpander::Builtin(_) => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: rename to ExpandDatabase
|
||||
#[salsa::query_group(AstDatabaseStorage)]
|
||||
pub trait AstDatabase: SourceDatabase {
|
||||
@ -24,7 +51,7 @@ pub trait AstDatabase: SourceDatabase {
|
||||
#[salsa::interned]
|
||||
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
|
||||
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
|
||||
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>>;
|
||||
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
|
||||
fn parse_macro(
|
||||
&self,
|
||||
macro_file: MacroFile,
|
||||
@ -41,18 +68,25 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdM
|
||||
pub(crate) fn macro_def(
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroDefId,
|
||||
) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>> {
|
||||
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);
|
||||
None
|
||||
})?;
|
||||
let rules = MacroRules::parse(&tt).ok().or_else(|| {
|
||||
log::warn!("fail on macro_def parse: {:#?}", tt);
|
||||
None
|
||||
})?;
|
||||
Some(Arc::new((rules, tmap)))
|
||||
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
|
||||
match id {
|
||||
MacroDefId::DeclarativeMacro(it) => {
|
||||
let macro_call = it.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);
|
||||
None
|
||||
})?;
|
||||
let rules = MacroRules::parse(&tt).ok().or_else(|| {
|
||||
log::warn!("fail on macro_def parse: {:#?}", tt);
|
||||
None
|
||||
})?;
|
||||
Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
|
||||
}
|
||||
MacroDefId::BuiltinMacro(it) => {
|
||||
Some(Arc::new((TokenExpander::Builtin(it.expander.clone()), mbe::TokenMap::default())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn macro_arg(
|
||||
@ -74,7 +108,7 @@ pub(crate) fn macro_expand(
|
||||
let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
|
||||
|
||||
let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
|
||||
let tt = macro_rules.0.expand(¯o_arg.0).map_err(|err| format!("{:?}", err))?;
|
||||
let tt = macro_rules.0.expand(db, id, ¯o_arg.0).map_err(|err| format!("{:?}", err))?;
|
||||
// Set a hard limit for the expanded tt
|
||||
let count = tt.count();
|
||||
if count > 65536 {
|
||||
|
@ -9,7 +9,7 @@ use crate::{
|
||||
db::AstDatabase,
|
||||
either::Either,
|
||||
name::{AsName, Name},
|
||||
HirFileId, HirFileIdRepr,
|
||||
HirFileId, HirFileIdRepr, MacroDefId,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -24,7 +24,10 @@ impl Hygiene {
|
||||
HirFileIdRepr::FileId(_) => None,
|
||||
HirFileIdRepr::MacroFile(macro_file) => {
|
||||
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
|
||||
Some(loc.def.krate)
|
||||
match loc.def {
|
||||
MacroDefId::DeclarativeMacro(it) => Some(it.krate),
|
||||
MacroDefId::BuiltinMacro(_) => None,
|
||||
}
|
||||
}
|
||||
};
|
||||
Hygiene { def_crate }
|
||||
|
@ -10,6 +10,7 @@ pub mod either;
|
||||
pub mod name;
|
||||
pub mod hygiene;
|
||||
pub mod diagnostics;
|
||||
pub mod builtin_macro;
|
||||
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::sync::Arc;
|
||||
@ -21,6 +22,7 @@ use ra_syntax::{
|
||||
};
|
||||
|
||||
use crate::ast_id_map::FileAstId;
|
||||
use crate::builtin_macro::BuiltinExpander;
|
||||
|
||||
/// Input to the analyzer is a set of files, where each file is identified by
|
||||
/// `FileId` and contains source code. However, another source of source code in
|
||||
@ -75,9 +77,15 @@ 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 =
|
||||
loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start();
|
||||
macro_decl.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();
|
||||
@ -85,7 +93,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 = (loc.def.ast_id.file_id, def_start);
|
||||
let def_start = (macro_decl.ast_id.file_id, def_start);
|
||||
|
||||
Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift })
|
||||
}
|
||||
@ -119,11 +127,24 @@ impl salsa::InternKey for MacroCallId {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct MacroDefId {
|
||||
pub enum MacroDefId {
|
||||
DeclarativeMacro(DeclarativeMacro),
|
||||
BuiltinMacro(BuiltinMacro),
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MacroCallLoc {
|
||||
pub def: MacroDefId,
|
||||
@ -144,7 +165,7 @@ pub struct ExpansionInfo {
|
||||
pub(crate) def_start: (HirFileId, TextUnit),
|
||||
pub(crate) shift: u32,
|
||||
|
||||
pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>,
|
||||
pub(crate) macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>,
|
||||
pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
|
||||
pub(crate) exp_map: Arc<mbe::RevTokenMap>,
|
||||
}
|
||||
|
@ -140,3 +140,6 @@ pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result");
|
||||
pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
|
||||
pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
|
||||
pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
|
||||
|
||||
// Builtin Macros
|
||||
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
|
||||
|
Loading…
x
Reference in New Issue
Block a user