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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
extern crate syntex_syntax;
extern crate syntex_pos;
extern crate syntex_errors;
extern crate syntex;
extern crate petgraph;
#[macro_use]
extern crate log;
#[cfg(test)]
extern crate env_logger;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate bitflags;
macro_rules! unwrap_presult {
($presult_epxr:expr) => {
match $presult_epxr {
Ok(x) => x,
Err(mut err) => {
err.emit();
panic!("rust_swig fatal error, see above");
}
}
};
($presult_epxr:expr, $conv_map:expr) => {
match $presult_epxr {
Ok(x) => x,
Err(mut err) => {
debug!("{}", $conv_map);
err.emit();
panic!("rust_swig fatal error, see above");
}
}
};
}
mod types_conv_map;
mod java_jni;
mod errors;
mod parsing;
mod my_ast;
use std::path::PathBuf;
use std::cell::RefCell;
use std::rc::Rc;
use syntex_syntax::parse::ParseSess;
use syntex_syntax::codemap::Span;
use syntex::Registry;
use syntex_syntax::tokenstream::TokenTree;
use syntex_syntax::ext::base::{ExtCtxt, MacEager, MacResult, TTMacroExpander};
use syntex_syntax::parse::PResult;
use syntex_syntax::ptr::P;
use syntex_syntax::ast;
use syntex_pos::DUMMY_SP;
use syntex_syntax::symbol::Symbol;
use syntex_syntax::util::small_vector::SmallVector;
use types_conv_map::TypesConvMap;
use errors::fatal_error;
use parsing::parse_foreigner_class;
pub enum LanguageConfig {
Java {
output_dir: PathBuf,
package_name: String,
},
}
pub struct Generator {
data: Rc<RefCell<GeneratorData>>,
}
struct GeneratorData {
init_done: bool,
config: LanguageConfig,
conv_map: TypesConvMap,
conv_map_source: Vec<TypesConvMapCode>,
}
struct TypesConvMapCode {
id_of_code: &'static str,
code: &'static str,
}
#[derive(PartialEq, Clone, Copy, Debug)]
enum SelfTypeVariant {
RptrMut,
Rptr,
Mut,
Default,
}
#[derive(PartialEq, Clone, Copy, Debug)]
enum MethodVariant {
Constructor,
Method(SelfTypeVariant),
StaticMethod,
}
#[derive(Debug, Clone)]
struct ForeignerMethod {
variant: MethodVariant,
rust_id: ast::Path,
fn_decl: P<ast::FnDecl>,
name_alias: Option<Symbol>,
may_return_error: bool,
foreigner_private: bool,
doc_comments: Vec<Symbol>,
}
impl ForeignerMethod {
pub(crate) fn short_name(&self) -> Symbol {
if let Some(name) = self.name_alias {
name
} else {
match self.rust_id.segments.len() {
0 => Symbol::intern(""),
n => self.rust_id.segments[n - 1].identifier.name,
}
}
}
pub(crate) fn span(&self) -> Span {
self.rust_id.span
}
}
#[derive(Debug, Clone)]
pub(crate) struct ForeignerClassInfo {
name: Symbol,
methods: Vec<ForeignerMethod>,
self_type: ast::Path,
this_type_for_method: Option<ast::Ty>,
foreigner_code: String,
constructor_ret_type: Option<ast::Ty>,
span: Span,
doc_comments: Vec<Symbol>,
}
impl Generator {
pub fn new(config: LanguageConfig) -> Generator {
let mut conv_map_source = Vec::new();
match config {
LanguageConfig::Java { .. } => {
conv_map_source.push(TypesConvMapCode {
id_of_code: "jni-include.rs",
code: include_str!("java_jni/jni-include.rs"),
});
}
}
Generator {
data: Rc::new(RefCell::new(GeneratorData {
init_done: false,
config,
conv_map: TypesConvMap::default(),
conv_map_source,
})),
}
}
pub fn register(self, registry: &mut Registry) {
registry.add_macro("foreigner_class", self);
}
}
impl TTMacroExpander for Generator {
fn expand<'a>(
&self,
cx: &'a mut ExtCtxt,
_: Span,
tokens: &[TokenTree],
) -> Box<MacResult + 'a> {
self.data.borrow_mut().expand_foreigner_class(cx, tokens)
}
}
impl GeneratorData {
fn expand_foreigner_class<'a>(
&mut self,
cx: &'a mut ExtCtxt,
tokens: &[TokenTree],
) -> Box<MacResult + 'a> {
let mut items = unwrap_presult!(self.init_types_map(cx.parse_sess()), self.conv_map);
let foreigner_class = match parse_foreigner_class(cx, tokens) {
Ok(x) => x,
Err(_) => {
panic!("Can not parse foreigner_class");
}
};
self.conv_map.register_foreigner_class(&foreigner_class);
match self.config {
LanguageConfig::Java {
ref output_dir,
ref package_name,
} => {
let mut gen_items = unwrap_presult!(
java_jni::generate(
cx.parse_sess(),
&mut self.conv_map,
output_dir,
package_name,
&foreigner_class,
),
self.conv_map
);
items.append(&mut gen_items);
MacEager::items(SmallVector::many(items))
}
}
}
fn init_types_map<'a>(&mut self, sess: &'a ParseSess) -> PResult<'a, Vec<P<ast::Item>>> {
if self.init_done {
return Ok(vec![]);
}
self.init_done = true;
for code in &self.conv_map_source {
self.conv_map.merge(sess, code.id_of_code, code.code)?;
}
if self.conv_map.is_empty() {
return Err(fatal_error(
sess,
DUMMY_SP,
"After merge all types maps with have no convertion code",
));
}
Ok(self.conv_map.take_utils_code())
}
}