mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 03:38:29 +00:00
Add basic bultin macro infrastructure
This commit is contained in:
parent
defc7ad772
commit
c46768d13d
@ -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, Module,
|
Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef,
|
||||||
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
MacroDefId, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir_expand::Source;
|
pub use hir_expand::Source;
|
||||||
@ -140,10 +140,15 @@ 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> {
|
||||||
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
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
|
builtin_macro::find_builtin_macro,
|
||||||
name::{self, AsName, Name},
|
name::{self, AsName, Name},
|
||||||
HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind,
|
DeclarativeMacro, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind,
|
||||||
};
|
};
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_db::{CrateId, FileId};
|
use ra_db::{CrateId, FileId};
|
||||||
@ -688,11 +689,32 @@ where
|
|||||||
fn collect_macro(&mut self, mac: &raw::MacroData) {
|
fn collect_macro(&mut self, mac: &raw::MacroData) {
|
||||||
let ast_id = AstId::new(self.file_id, mac.ast_id);
|
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
|
// 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 = MacroDefId { ast_id, krate: self.def_collector.def_map.krate };
|
let macro_id = DeclarativeMacro { ast_id, krate: self.def_collector.def_map.krate };
|
||||||
self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export);
|
self.def_collector.define_macro(
|
||||||
|
self.module_id,
|
||||||
|
name.clone(),
|
||||||
|
MacroDefId::DeclarativeMacro(macro_id),
|
||||||
|
mac.export,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -200,6 +200,7 @@ pub(super) struct MacroData {
|
|||||||
pub(super) path: Path,
|
pub(super) path: Path,
|
||||||
pub(super) name: Option<Name>,
|
pub(super) name: Option<Name>,
|
||||||
pub(super) export: bool,
|
pub(super) export: bool,
|
||||||
|
pub(super) builtin: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RawItemsCollector {
|
struct RawItemsCollector {
|
||||||
@ -367,7 +368,11 @@ impl RawItemsCollector {
|
|||||||
// FIXME: cfg_attr
|
// FIXME: cfg_attr
|
||||||
let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
|
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));
|
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 ra_syntax::{AstNode, Parse, SyntaxNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId,
|
ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc,
|
||||||
MacroFile, MacroFileKind,
|
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
|
// FIXME: rename to ExpandDatabase
|
||||||
#[salsa::query_group(AstDatabaseStorage)]
|
#[salsa::query_group(AstDatabaseStorage)]
|
||||||
pub trait AstDatabase: SourceDatabase {
|
pub trait AstDatabase: SourceDatabase {
|
||||||
@ -24,7 +51,7 @@ pub trait AstDatabase: SourceDatabase {
|
|||||||
#[salsa::interned]
|
#[salsa::interned]
|
||||||
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
|
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
|
||||||
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
|
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(
|
fn parse_macro(
|
||||||
&self,
|
&self,
|
||||||
macro_file: MacroFile,
|
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(
|
pub(crate) fn macro_def(
|
||||||
db: &dyn AstDatabase,
|
db: &dyn AstDatabase,
|
||||||
id: MacroDefId,
|
id: MacroDefId,
|
||||||
) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>> {
|
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
|
||||||
let macro_call = id.ast_id.to_node(db);
|
match id {
|
||||||
let arg = macro_call.token_tree()?;
|
MacroDefId::DeclarativeMacro(it) => {
|
||||||
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
|
let macro_call = it.ast_id.to_node(db);
|
||||||
log::warn!("fail on macro_def to token tree: {:#?}", arg);
|
let arg = macro_call.token_tree()?;
|
||||||
None
|
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
|
||||||
})?;
|
log::warn!("fail on macro_def to token tree: {:#?}", arg);
|
||||||
let rules = MacroRules::parse(&tt).ok().or_else(|| {
|
None
|
||||||
log::warn!("fail on macro_def parse: {:#?}", tt);
|
})?;
|
||||||
None
|
let rules = MacroRules::parse(&tt).ok().or_else(|| {
|
||||||
})?;
|
log::warn!("fail on macro_def parse: {:#?}", tt);
|
||||||
Some(Arc::new((rules, tmap)))
|
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(
|
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_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 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
|
// Set a hard limit for the expanded tt
|
||||||
let count = tt.count();
|
let count = tt.count();
|
||||||
if count > 65536 {
|
if count > 65536 {
|
||||||
|
@ -9,7 +9,7 @@ use crate::{
|
|||||||
db::AstDatabase,
|
db::AstDatabase,
|
||||||
either::Either,
|
either::Either,
|
||||||
name::{AsName, Name},
|
name::{AsName, Name},
|
||||||
HirFileId, HirFileIdRepr,
|
HirFileId, HirFileIdRepr, MacroDefId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -24,7 +24,10 @@ 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);
|
||||||
Some(loc.def.krate)
|
match loc.def {
|
||||||
|
MacroDefId::DeclarativeMacro(it) => Some(it.krate),
|
||||||
|
MacroDefId::BuiltinMacro(_) => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Hygiene { def_crate }
|
Hygiene { def_crate }
|
||||||
|
@ -10,6 +10,7 @@ pub mod either;
|
|||||||
pub mod name;
|
pub mod name;
|
||||||
pub mod hygiene;
|
pub mod hygiene;
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
|
pub mod builtin_macro;
|
||||||
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -21,6 +22,7 @@ use ra_syntax::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::ast_id_map::FileAstId;
|
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
|
/// 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
|
/// `FileId` and contains source code. However, another source of source code in
|
||||||
@ -75,9 +77,15 @@ 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 =
|
||||||
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 macro_def = db.macro_def(loc.def)?;
|
||||||
let shift = macro_def.0.shift();
|
let shift = macro_def.0.shift();
|
||||||
@ -85,7 +93,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 = (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 })
|
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)]
|
#[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 krate: CrateId,
|
||||||
pub ast_id: AstId<ast::MacroCall>,
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct MacroCallLoc {
|
pub struct MacroCallLoc {
|
||||||
pub def: MacroDefId,
|
pub def: MacroDefId,
|
||||||
@ -144,7 +165,7 @@ pub struct ExpansionInfo {
|
|||||||
pub(crate) def_start: (HirFileId, TextUnit),
|
pub(crate) def_start: (HirFileId, TextUnit),
|
||||||
pub(crate) shift: u32,
|
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) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
|
||||||
pub(crate) exp_map: Arc<mbe::RevTokenMap>,
|
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 OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
|
||||||
pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
|
pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
|
||||||
pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
|
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…
Reference in New Issue
Block a user