Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
49686007
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
49686007
编写于
10月 19, 2016
作者:
J
Jeffrey Seyfried
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor away `metadata::creader::Macros`.
上级
53de24bb
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
30 addition
and
58 deletion
+30
-58
src/librustc_metadata/creader.rs
src/librustc_metadata/creader.rs
+30
-58
未找到文件。
src/librustc_metadata/creader.rs
浏览文件 @
49686007
...
...
@@ -148,17 +148,6 @@ enum LoadResult {
Loaded
(
Library
),
}
pub
struct
Macros
{
pub
macro_rules
:
Vec
<
ast
::
MacroDef
>
,
/// An array of pairs where the first element is the name of the custom
/// derive (e.g. the trait being derived) and the second element is the
/// index of the definition.
pub
custom_derive_registrar
:
Option
<
DefIndex
>
,
pub
svh
:
Svh
,
pub
dylib
:
Option
<
PathBuf
>
,
}
impl
<
'a
>
CrateLoader
<
'a
>
{
pub
fn
new
(
sess
:
&
'a
Session
,
cstore
:
&
'a
CStore
,
...
...
@@ -554,18 +543,13 @@ fn read_extension_crate(&mut self, span: Span, info: &ExternCrateInfo) -> Extens
}
}
pub
fn
read_macros
(
&
mut
self
,
item
:
&
ast
::
Item
)
->
Macros
{
pub
fn
read_macros
(
&
mut
self
,
item
:
&
ast
::
Item
)
->
Loaded
Macros
{
let
ci
=
self
.extract_crate_info
(
item
)
.unwrap
();
let
ekrate
=
self
.read_extension_crate
(
item
.span
,
&
ci
);
let
root
=
ekrate
.metadata
.get_root
();
let
source_name
=
format!
(
"<{} macros>"
,
item
.ident
);
let
mut
ret
=
Macros
{
macro_rules
:
Vec
::
new
(),
custom_derive_registrar
:
None
,
svh
:
root
.hash
,
dylib
:
None
,
};
let
mut
macro_rules
=
Vec
::
new
();
for
def
in
root
.macro_defs
.decode
(
&*
ekrate
.metadata
)
{
// NB: Don't use parse::parse_tts_from_source_str because it parses with
// quote_depth > 0.
...
...
@@ -589,7 +573,7 @@ pub fn read_macros(&mut self, item: &ast::Item) -> Macros {
attr
::
mark_used
(
attr
);
}
ret
.
macro_rules
.push
(
ast
::
MacroDef
{
macro_rules
.push
(
ast
::
MacroDef
{
ident
:
ast
::
Ident
::
with_empty_ctxt
(
def
.name
),
id
:
ast
::
DUMMY_NODE_ID
,
span
:
local_span
,
...
...
@@ -602,9 +586,24 @@ pub fn read_macros(&mut self, item: &ast::Item) -> Macros {
.insert
(
local_span
,
(
def
.name
.as_str
()
.to_string
(),
def
.span
));
}
match
root
.macro_derive_registrar
{
Some
(
id
)
=>
ret
.custom_derive_registrar
=
Some
(
id
),
if
let
Some
(
id
)
=
root
.macro_derive_registrar
{
let
dylib
=
match
ekrate
.dylib
.clone
()
{
Some
(
dylib
)
=>
dylib
,
None
=>
span_bug!
(
item
.span
,
"proc-macro crate not dylib"
),
};
if
ekrate
.target_only
{
let
message
=
format!
(
"proc-macro crate is not available for
\
triple `{}` (only found {})"
,
config
::
host_triple
(),
self
.sess.opts.target_triple
);
self
.sess
.span_fatal
(
item
.span
,
&
message
);
}
// custom derive crates currently should not have any macro_rules!
// exported macros, enforced elsewhere
assert_eq!
(
macro_rules
.len
(),
0
);
LoadedMacros
::
ProcMacros
(
self
.load_derive_macros
(
item
,
id
,
root
.hash
,
dylib
))
}
else
{
// If this crate is not a proc-macro crate then we might be able to
// register it with the local crate store to prevent loading the
// metadata twice.
...
...
@@ -612,29 +611,11 @@ pub fn read_macros(&mut self, item: &ast::Item) -> Macros {
// If it's a proc-macro crate, though, then we definitely don't
// want to register it with the local crate store as we're just
// going to use it as we would a plugin.
None
=>
{
ekrate
.register
(
self
);
return
ret
LoadedMacros
::
MacroRules
(
macro_rules
)
}
}
self
.cstore
.add_used_for_derive_macros
(
item
);
ret
.dylib
=
ekrate
.dylib
.clone
();
if
ret
.dylib
.is_none
()
{
span_bug!
(
item
.span
,
"proc-macro crate not dylib"
);
}
if
ekrate
.target_only
{
let
message
=
format!
(
"proc-macro crate is not available for
\
triple `{}` (only found {})"
,
config
::
host_triple
(),
self
.sess.opts.target_triple
);
self
.sess
.span_fatal
(
item
.span
,
&
message
);
}
return
ret
}
/// Load custom derive macros.
///
/// Note that this is intentionally similar to how we load plugins today,
...
...
@@ -642,7 +623,7 @@ pub fn read_macros(&mut self, item: &ast::Item) -> Macros {
/// implemented as dynamic libraries, but we have a possible future where
/// custom derive (and other macro-1.1 style features) are implemented via
/// executables and custom IPC.
fn
load_derive_macros
(
&
mut
self
,
span
:
Span
,
macros
:
&
Macros
,
index
:
DefIndex
)
fn
load_derive_macros
(
&
mut
self
,
item
:
&
ast
::
Item
,
index
:
DefIndex
,
svh
:
Svh
,
path
:
PathBuf
)
->
Vec
<
(
ast
::
Name
,
SyntaxExtension
)
>
{
use
std
::{
env
,
mem
};
use
proc_macro
::
TokenStream
;
...
...
@@ -650,19 +631,20 @@ fn load_derive_macros(&mut self, span: Span, macros: &Macros, index: DefIndex)
use
rustc_back
::
dynamic_lib
::
DynamicLibrary
;
use
syntax_ext
::
deriving
::
custom
::
CustomDerive
;
self
.cstore
.add_used_for_derive_macros
(
item
);
// Make sure the path contains a / or the linker will search for it.
let
path
=
macros
.dylib
.as_ref
()
.unwrap
();
let
path
=
env
::
current_dir
()
.unwrap
()
.join
(
path
);
let
lib
=
match
DynamicLibrary
::
open
(
Some
(
&
path
))
{
Ok
(
lib
)
=>
lib
,
Err
(
err
)
=>
self
.sess
.span_fatal
(
span
,
&
err
),
Err
(
err
)
=>
self
.sess
.span_fatal
(
item
.
span
,
&
err
),
};
let
sym
=
self
.sess
.generate_derive_registrar_symbol
(
&
macros
.
svh
,
index
);
let
sym
=
self
.sess
.generate_derive_registrar_symbol
(
&
svh
,
index
);
let
registrar
=
unsafe
{
let
sym
=
match
lib
.symbol
(
&
sym
)
{
Ok
(
f
)
=>
f
,
Err
(
err
)
=>
self
.sess
.span_fatal
(
span
,
&
err
),
Err
(
err
)
=>
self
.sess
.span_fatal
(
item
.
span
,
&
err
),
};
mem
::
transmute
::
<*
mut
u8
,
fn
(
&
mut
Registry
)
>
(
sym
)
};
...
...
@@ -1079,16 +1061,6 @@ fn process_item(&mut self, item: &ast::Item, definitions: &hir_map::Definitions)
}
fn
load_macros
(
&
mut
self
,
extern_crate
:
&
ast
::
Item
)
->
LoadedMacros
{
let
macros
=
self
.read_macros
(
extern_crate
);
if
let
Some
(
index
)
=
macros
.custom_derive_registrar
{
// custom derive crates currently should not have any macro_rules!
// exported macros, enforced elsewhere
assert_eq!
(
macros
.macro_rules
.len
(),
0
);
let
custom_derives
=
self
.load_derive_macros
(
extern_crate
.span
,
&
macros
,
index
);
LoadedMacros
::
ProcMacros
(
custom_derives
)
}
else
{
LoadedMacros
::
MacroRules
(
macros
.macro_rules
)
}
self
.read_macros
(
extern_crate
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录