1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![feature(collections, plugin_registrar, quote, box_syntax, rustc_private)]
#![deny(missing_copy_implementations)]
extern crate rustc;
extern crate syntax;
use syntax::{ast, attr, ext, codemap};
use syntax::parse::token;
use syntax::fold::Folder;
use syntax::ptr::P;
pub mod shader_param;
pub mod vertex_format;
#[plugin_registrar]
pub fn registrar(reg: &mut rustc::plugin::Registry) {
use syntax::parse::token::intern;
use syntax::ext::base;
reg.register_syntax_extension(intern("shader_param"),
base::Decorator(box shader_param::ShaderParam));
reg.register_syntax_extension(intern("vertex_format"),
base::Decorator(box vertex_format::VertexFormat));
}
fn find_name(cx: &mut ext::base::ExtCtxt, span: codemap::Span,
attributes: &[ast::Attribute]) -> Option<token::InternedString> {
attributes.iter().fold(None, |name, attribute| {
match attribute.node.value.node {
ast::MetaNameValue(ref attr_name, ref attr_value) => {
match (&attr_name[..], &attr_value.node) {
("name", &ast::LitStr(ref new_name, _)) => {
attr::mark_used(attribute);
name.map_or(Some(new_name.clone()), |name| {
cx.span_warn(span, &format!(
"Extra field name detected: {:?} - \
ignoring in favour of: {:?}", new_name, name
));
None
})
}
_ => None,
}
}
_ => name,
}
})
}
static EXTERN_CRATE_HACK: &'static str = "__gfx_extern_crate_hack";
fn extern_crate_hack<F>(context: &mut ext::base::ExtCtxt,
span: codemap::Span,
mut push: F) -> ast::Ident where F: FnMut(P<ast::Item>) {
use syntax::ext::build::AstBuilder;
let extern_crate_hack = token::gensym_ident(EXTERN_CRATE_HACK);
let item = context.item_mod(
span,
span,
extern_crate_hack,
vec![],
vec![
P(ast::Item {
span: span,
vis: ast::Inherited,
attrs: vec![],
node: ast::ItemExternCrate(
Some(context.ident_of("gfx").name)
),
id: ast::DUMMY_NODE_ID,
ident: token::str_to_ident("gfx_")
}),
context.item_use_simple_(
span,
ast::Public,
context.ident_of("gfx"),
context.path(span, vec![
context.ident_of("self"),
context.ident_of("gfx_")
])
),
]
);
push(item);
extern_crate_hack
}
struct ExternCrateHackFolder {
path_root: ast::Ident
}
impl Folder for ExternCrateHackFolder {
fn fold_path(&mut self, p: ast::Path) -> ast::Path {
let p = syntax::fold::noop_fold_path(p, self);
let needs_fix = (p.segments).get(0)
.map(|s| s.identifier.as_str() == EXTERN_CRATE_HACK)
.unwrap_or(false);
let needs_fix_self = (p.segments).get(0)
.map(|s| s.identifier.as_str() == "self")
.unwrap_or(false) &&
(p.segments).get(1)
.map(|s| s.identifier.as_str() == EXTERN_CRATE_HACK)
.unwrap_or(false);
if needs_fix {
let mut p = p.clone();
p.segments[0].identifier = self.path_root;
p.global = false;
p
} else if needs_fix_self {
let mut p = p.clone();
p.segments[1].identifier = self.path_root;
p.global = false;
p
} else {
p
}
}
}
fn fixup_extern_crate_paths(item: P<ast::Item>, path_root: ast::Ident) -> P<ast::Item> {
ExternCrateHackFolder {
path_root: path_root
}.fold_item(item).into_iter().next().unwrap()
}