1 | // ================================================================ // |
---|
2 | // // |
---|
3 | // File : arb_proto_2_xsub.cxx // |
---|
4 | // Purpose : generate ARB.xs for perl interface // |
---|
5 | // // |
---|
6 | // ReCoded by Ralf Westram (coder@reallysoft.de) in December 2009 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // ================================================================ // |
---|
11 | |
---|
12 | #include <arbdb.h> |
---|
13 | #include <arb_strbuf.h> |
---|
14 | #include <BufferedFileReader.h> |
---|
15 | |
---|
16 | #include <string> |
---|
17 | #include <vector> |
---|
18 | #include <set> |
---|
19 | |
---|
20 | #include <cctype> |
---|
21 | #include <unistd.h> |
---|
22 | #include <arb_str.h> |
---|
23 | #include <arb_diff.h> |
---|
24 | |
---|
25 | #if defined(DEBUG) |
---|
26 | #if defined(DEVEL_RALF) |
---|
27 | // #define TRACE |
---|
28 | #endif // DEVEL_RALF |
---|
29 | #endif // DEBUG |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | // -------------------------------------------------------------------------------- |
---|
34 | |
---|
35 | #define CHAR_PTR "char *" |
---|
36 | #define CONST_CHAR_PTR "const char *" |
---|
37 | |
---|
38 | // -------------------------------------------------------------------------------- |
---|
39 | |
---|
40 | struct Error { |
---|
41 | virtual ~Error() {} |
---|
42 | virtual void print() const = 0; |
---|
43 | }; |
---|
44 | |
---|
45 | class ProgramError : public Error { |
---|
46 | string error; |
---|
47 | public: |
---|
48 | ProgramError(string message) : error(message) {} |
---|
49 | ProgramError(const char *message) : error(message) {} |
---|
50 | ~ProgramError() OVERRIDE {} |
---|
51 | |
---|
52 | void print() const OVERRIDE { |
---|
53 | fprintf(stderr, "arb_proto_2_xsub: Error: %s\n", error.c_str()); |
---|
54 | } |
---|
55 | }; |
---|
56 | |
---|
57 | class InputFileError : public Error { |
---|
58 | string located_error; |
---|
59 | public: |
---|
60 | InputFileError(LineReader& fileBuffer, string message) : located_error(fileBuffer.lineError(message)) {} |
---|
61 | InputFileError(LineReader& fileBuffer, const char *message) : located_error(fileBuffer.lineError(message)) {} |
---|
62 | ~InputFileError() OVERRIDE {} |
---|
63 | |
---|
64 | void print() const OVERRIDE { |
---|
65 | fputs(located_error.c_str(), stderr); |
---|
66 | fputc('\n', stderr); |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | // -------------------------------------------------------------------------------- |
---|
71 | |
---|
72 | class CommentSkippingFileBuffer : public BufferedFileReader { |
---|
73 | string open_comment; |
---|
74 | string close_comment; |
---|
75 | string eol_comment; |
---|
76 | |
---|
77 | void throw_error(const char *message) __ATTR__NORETURN { throw InputFileError(*this, message); } |
---|
78 | |
---|
79 | string read_till_close_comment(string curr_line, size_t comment_startLineNumber) { |
---|
80 | bool seen_end = false; |
---|
81 | while (!seen_end) { |
---|
82 | size_t close = curr_line.find(close_comment); |
---|
83 | if (close != string::npos) { |
---|
84 | curr_line = curr_line.substr(close+close_comment.length()); |
---|
85 | seen_end = true; |
---|
86 | } |
---|
87 | else { |
---|
88 | if (!BufferedFileReader::getLine(curr_line)) { |
---|
89 | setLineNumber(comment_startLineNumber); |
---|
90 | throw_error("end of file reached while skipping comment"); |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | return curr_line; |
---|
95 | } |
---|
96 | |
---|
97 | public: |
---|
98 | CommentSkippingFileBuffer(const string& filename_, |
---|
99 | FILE *in, |
---|
100 | const char *openComment, |
---|
101 | const char *closeComment, |
---|
102 | const char *eolComment) : |
---|
103 | BufferedFileReader(filename_, in), |
---|
104 | open_comment(openComment), |
---|
105 | close_comment(closeComment), |
---|
106 | eol_comment(eolComment) |
---|
107 | {} |
---|
108 | |
---|
109 | bool getLine(string& line) OVERRIDE { |
---|
110 | if (BufferedFileReader::getLine(line)) { |
---|
111 | size_t open = line.find(open_comment); |
---|
112 | size_t eol = line.find(eol_comment); |
---|
113 | |
---|
114 | if (open != eol) { // comment found |
---|
115 | if (open<eol) { |
---|
116 | if (eol != string::npos) { |
---|
117 | throw_error(GBS_global_string("'%s' inside '%s %s'", eol_comment.c_str(), open_comment.c_str(), close_comment.c_str())); |
---|
118 | } |
---|
119 | line = line.substr(0, open) + read_till_close_comment(line.substr(open+2), getLineNumber()); |
---|
120 | } |
---|
121 | else { |
---|
122 | arb_assert(eol<open); |
---|
123 | if (open != string::npos) { |
---|
124 | throw_error(GBS_global_string("'%s' behind '%s'", open_comment.c_str(), eol_comment.c_str())); |
---|
125 | } |
---|
126 | line = line.substr(0, eol); |
---|
127 | } |
---|
128 | } |
---|
129 | return true; |
---|
130 | } |
---|
131 | return false; |
---|
132 | } |
---|
133 | }; |
---|
134 | |
---|
135 | // -------------------------------------------------------------------------------- |
---|
136 | |
---|
137 | inline bool is_empty_code(const char *code) { return !code[0]; } |
---|
138 | inline bool contains_preprozessorCode(const char *code) { return strchr(code, '#'); } |
---|
139 | inline bool contains_braces(const char *code) { return strpbrk(code, "{}"); } |
---|
140 | inline bool is_typedef(const char *code) { return ARB_strBeginsWith(code, "typedef"); } |
---|
141 | inline bool is_forward_decl(const char *code) { return ARB_strBeginsWith(code, "class") || ARB_strBeginsWith(code, "struct"); } |
---|
142 | |
---|
143 | inline bool is_prototype(const char *code) { |
---|
144 | return |
---|
145 | !is_empty_code(code) && |
---|
146 | !contains_preprozessorCode(code) && |
---|
147 | !contains_braces(code) && |
---|
148 | !is_typedef(code) && |
---|
149 | !is_forward_decl(code); |
---|
150 | } |
---|
151 | |
---|
152 | inline void trace_over_braces(const char *code, int& brace_counter) { |
---|
153 | while (code) { |
---|
154 | const char *brace = strpbrk(code, "{}"); |
---|
155 | if (!brace) break; |
---|
156 | |
---|
157 | if (*brace == '{') { |
---|
158 | ++brace_counter; |
---|
159 | } |
---|
160 | else { |
---|
161 | arb_assert(*brace == '}'); |
---|
162 | --brace_counter; |
---|
163 | } |
---|
164 | code = brace+1; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | // -------------------------------------------------------------------------------- |
---|
169 | |
---|
170 | inline char *get_token_and_incr_lineno(const char*& code, const char *separator, size_t *lineno) { |
---|
171 | char *token = NULp; |
---|
172 | if (code) { |
---|
173 | const char *sep_pos = strpbrk(code, separator); |
---|
174 | |
---|
175 | if (!sep_pos) { |
---|
176 | if (!code[0]) { // end of code |
---|
177 | token = NULp; |
---|
178 | code = NULp; |
---|
179 | } |
---|
180 | else { |
---|
181 | token = ARB_strdup(code); |
---|
182 | code = NULp; |
---|
183 | } |
---|
184 | } |
---|
185 | else { |
---|
186 | token = ARB_strpartdup(code, sep_pos-1); |
---|
187 | |
---|
188 | const char *behind_sep = sep_pos + strspn(sep_pos, separator); // next non 'separator' char |
---|
189 | if (lineno) { |
---|
190 | int no_of_linefeeds = 0; |
---|
191 | while (code<behind_sep) if (*++code == '\n') ++no_of_linefeeds; |
---|
192 | |
---|
193 | *lineno += no_of_linefeeds; |
---|
194 | } |
---|
195 | else { |
---|
196 | code = behind_sep; |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | return token; |
---|
201 | } |
---|
202 | |
---|
203 | inline char *get_token(const char*& code, const char *separator) { |
---|
204 | return get_token_and_incr_lineno(code, separator, NULp); |
---|
205 | } |
---|
206 | |
---|
207 | inline bool is_ID_char(char c) { return isalnum(c) || c == '_'; } |
---|
208 | |
---|
209 | inline const char *next_closing_paren(const char *code) { |
---|
210 | const char *open_paren = strchr(code, '('); |
---|
211 | const char *close_paren = strchr(code, ')'); |
---|
212 | |
---|
213 | if (!open_paren || (close_paren && close_paren<open_paren)) return close_paren; |
---|
214 | |
---|
215 | close_paren = next_closing_paren(open_paren+1); |
---|
216 | return next_closing_paren(close_paren+1); |
---|
217 | } |
---|
218 | |
---|
219 | inline const char *next_comma_outside_parens(const char *code) { |
---|
220 | const char *comma = strchr(code, ','); |
---|
221 | if (comma) { |
---|
222 | const char *open_paren = strchr(code, '('); |
---|
223 | if (open_paren && open_paren<comma) { |
---|
224 | const char *close_paren = next_closing_paren(open_paren+1); |
---|
225 | if (!close_paren) throw "Unbalanced parenthesis"; |
---|
226 | comma = next_comma_outside_parens(close_paren+1); |
---|
227 | } |
---|
228 | } |
---|
229 | return comma; |
---|
230 | } |
---|
231 | |
---|
232 | inline bool find_open_close_paren(const char *code, size_t& opening_paren_pos, size_t& closing_paren_pos) { |
---|
233 | const char *open_paren = strchr(code, '('); |
---|
234 | if (open_paren) { |
---|
235 | const char *close_paren = next_closing_paren(open_paren+1); |
---|
236 | if (close_paren) { |
---|
237 | opening_paren_pos = open_paren-code; |
---|
238 | closing_paren_pos = close_paren-code; |
---|
239 | return true; |
---|
240 | } |
---|
241 | } |
---|
242 | return false; |
---|
243 | } |
---|
244 | |
---|
245 | inline string concat_type_and_name(const string& type, const string& name) { |
---|
246 | if (type.at(type.length()-1) == '*') return type+name; |
---|
247 | return type+' '+name; |
---|
248 | } |
---|
249 | |
---|
250 | // ---------------- |
---|
251 | // TypeMap |
---|
252 | |
---|
253 | class TypeMap { |
---|
254 | // representation of types mapped in 'typemap' file |
---|
255 | set<string> defined_types; |
---|
256 | |
---|
257 | public: |
---|
258 | TypeMap() {} |
---|
259 | |
---|
260 | void load(LineReader& typemap); |
---|
261 | bool has_definition_for(const string& type_decl) const { |
---|
262 | return defined_types.find(type_decl) != defined_types.end(); |
---|
263 | } |
---|
264 | }; |
---|
265 | |
---|
266 | // ------------- |
---|
267 | // Type |
---|
268 | |
---|
269 | enum TypeClass { |
---|
270 | INVALID_TYPE, // invalid |
---|
271 | |
---|
272 | VOID, // no parameter |
---|
273 | SIMPLE_TYPE, // simple types like int, float, double, ... |
---|
274 | CONST_CHAR, // 'const char *' |
---|
275 | HEAP_COPY, // type is 'char*' and interpreted as heap-copy |
---|
276 | CONVERSION_FUNCTION, // convert type using GBP_-conversion functions |
---|
277 | TYPEMAPPED, // type is defined in file 'typemap' |
---|
278 | |
---|
279 | CANT_HANDLE, // type cannot be used in perl interface |
---|
280 | FORBIDDEN, // usage forbidden via 'NOT4PERL' |
---|
281 | }; |
---|
282 | |
---|
283 | #if defined(TRACE) |
---|
284 | #define TypeClass2CSTR(type) case type: return #type |
---|
285 | const char *get_TypeClass_name(TypeClass type_class) { |
---|
286 | switch (type_class) { |
---|
287 | TypeClass2CSTR(INVALID_TYPE); |
---|
288 | TypeClass2CSTR(VOID); |
---|
289 | TypeClass2CSTR(SIMPLE_TYPE); |
---|
290 | TypeClass2CSTR(CONST_CHAR); |
---|
291 | TypeClass2CSTR(HEAP_COPY); |
---|
292 | TypeClass2CSTR(CONVERSION_FUNCTION); |
---|
293 | TypeClass2CSTR(TYPEMAPPED); |
---|
294 | TypeClass2CSTR(CANT_HANDLE); |
---|
295 | TypeClass2CSTR(FORBIDDEN); |
---|
296 | } |
---|
297 | return NULp; |
---|
298 | } |
---|
299 | #undef TypeClass2CSTR |
---|
300 | #endif // TRACE |
---|
301 | |
---|
302 | inline string type2id(const string& type) { |
---|
303 | char *s = GBS_string_eval(type.c_str(), |
---|
304 | "const =:" // remove const (for less ugly names) |
---|
305 | " =:" // remove spaces |
---|
306 | "\\*=Ptr"); // rename '*' |
---|
307 | |
---|
308 | string id(s); |
---|
309 | free(s); |
---|
310 | return id; |
---|
311 | } |
---|
312 | inline string conversion_function_name(const string& fromType, const string& toType) { |
---|
313 | string from = type2id(fromType); |
---|
314 | string to = type2id(toType); |
---|
315 | return string("GBP_")+from+"_2_"+to; |
---|
316 | } |
---|
317 | inline string constCastTo(const string& expr, const string& targetType) { |
---|
318 | return string("const_cast<")+targetType+">("+expr+")"; |
---|
319 | } |
---|
320 | |
---|
321 | class Type { // return- or parameter-type |
---|
322 | string c_type; |
---|
323 | string perl_type; |
---|
324 | TypeClass type_class; |
---|
325 | |
---|
326 | string unify_type_decl(const char *code) { |
---|
327 | string type_decl; |
---|
328 | enum { SPACE, STAR, ID, UNKNOWN } last = SPACE, curr; |
---|
329 | for (int i = 0; code[i]; ++i) { |
---|
330 | char c = code[i]; |
---|
331 | |
---|
332 | switch (c) { |
---|
333 | case ' ': curr = SPACE; break; |
---|
334 | case '*': curr = STAR; break; |
---|
335 | default: curr = is_ID_char(c) ? ID : UNKNOWN; break; |
---|
336 | } |
---|
337 | |
---|
338 | if (last != SPACE && curr != last) type_decl += ' '; |
---|
339 | if (curr != SPACE) type_decl += c; |
---|
340 | |
---|
341 | last = curr; |
---|
342 | } |
---|
343 | |
---|
344 | return last == SPACE |
---|
345 | ? type_decl.substr(0, type_decl.length()-1) |
---|
346 | : type_decl; |
---|
347 | } |
---|
348 | |
---|
349 | void throw_if_enum() const { |
---|
350 | size_t enum_pos = c_type.find("enum "); |
---|
351 | if (enum_pos != string::npos) { |
---|
352 | const char *enum_type = c_type.c_str()+enum_pos; |
---|
353 | const char *enum_name = enum_type+5; |
---|
354 | throw GBS_global_string("do not use '%s', simply use '%s'", enum_type, enum_name); |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | string convertExpression(const string& expr, const string& fromType, const string& toType) const { |
---|
359 | arb_assert(possible_in_xsub()); |
---|
360 | throw_if_enum(); |
---|
361 | if (get_TypeClass() == CONVERSION_FUNCTION) { |
---|
362 | string conversion_function = conversion_function_name(fromType, toType); |
---|
363 | return conversion_function+"("+expr+")"; // result is toType |
---|
364 | } |
---|
365 | return expr; |
---|
366 | } |
---|
367 | |
---|
368 | bool cant_handle(const string& type_decl) { |
---|
369 | return |
---|
370 | strpbrk(type_decl.c_str(), "().*") || // function-parameters, pointer-types not handled in ctor |
---|
371 | type_decl.find("GB_CB") != string::npos || // some typedef'ed function-parameters |
---|
372 | type_decl.find("CharPtrArray") != string::npos || |
---|
373 | type_decl.find("StrArray") != string::npos || |
---|
374 | type_decl.find("GB_Link_Follower") != string::npos; |
---|
375 | } |
---|
376 | |
---|
377 | bool is_forbidden(const string& type_decl) { |
---|
378 | return |
---|
379 | type_decl.find("NOT4PERL") != string::npos || // 'NOT4PERL' declares prototype as "FORBIDDEN" |
---|
380 | type_decl.find("GBQUARK") != string::npos || // internal information, hide from perl |
---|
381 | type_decl.find("GB_COMPRESSION_MASK") != string::npos || // internal information, hide from perl |
---|
382 | type_decl.find("GB_CBUFFER") != string::npos || // internal ARBDB buffers |
---|
383 | type_decl.find("GB_BUFFER") != string::npos; // memory managed by ARBDB |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | public: |
---|
388 | static TypeMap globalTypemap; |
---|
389 | |
---|
390 | Type() : type_class(INVALID_TYPE) {} |
---|
391 | Type(const char *code) { |
---|
392 | c_type = unify_type_decl(code); |
---|
393 | |
---|
394 | if (c_type == "void") { type_class = VOID; } |
---|
395 | else if (c_type == CONST_CHAR_PTR || |
---|
396 | c_type == "GB_ERROR" || |
---|
397 | c_type == "GB_CSTR") |
---|
398 | { |
---|
399 | type_class = CONST_CHAR; |
---|
400 | perl_type = CHAR_PTR; |
---|
401 | } |
---|
402 | else if (c_type == CHAR_PTR) { |
---|
403 | type_class = HEAP_COPY; |
---|
404 | perl_type = c_type; |
---|
405 | } |
---|
406 | // [Code-TAG: enum_type_replacement] |
---|
407 | // for each enum type converted here, you need to support a |
---|
408 | // conversion function in ../ARBDB/adperl.cxx@enum_conversion_functions |
---|
409 | else if (c_type == "GB_CASE" || |
---|
410 | c_type == "GB_CB_TYPE" || |
---|
411 | c_type == "GB_TYPES" || |
---|
412 | c_type == "GB_UNDO_TYPE" || |
---|
413 | c_type == "GB_SEARCH_TYPE" || |
---|
414 | c_type == "GB_alignment_type") |
---|
415 | { |
---|
416 | type_class = CONVERSION_FUNCTION; |
---|
417 | perl_type = CHAR_PTR; |
---|
418 | } |
---|
419 | else if (globalTypemap.has_definition_for(c_type)) { |
---|
420 | type_class = TYPEMAPPED; |
---|
421 | perl_type = c_type; |
---|
422 | } |
---|
423 | else if (cant_handle(c_type)) { type_class = CANT_HANDLE; } |
---|
424 | else if (is_forbidden(c_type)) { type_class = FORBIDDEN; } // Caution: this line catches all '*' types not handled above |
---|
425 | else { |
---|
426 | type_class = SIMPLE_TYPE; |
---|
427 | perl_type = c_type; |
---|
428 | } |
---|
429 | } |
---|
430 | |
---|
431 | const string& c_decl() const { return c_type; } |
---|
432 | const string& perl_decl() const { return perl_type; } |
---|
433 | |
---|
434 | TypeClass get_TypeClass() const { return type_class; } |
---|
435 | bool isVoid() const { return get_TypeClass() == VOID; } |
---|
436 | |
---|
437 | bool possible_in_xsub() const { return type_class != CANT_HANDLE && type_class != FORBIDDEN; } |
---|
438 | |
---|
439 | string convert_argument_for_C(const string& perl_arg) const { |
---|
440 | if (perl_decl() == CHAR_PTR) { |
---|
441 | if (c_decl() == CHAR_PTR) throw "argument of type 'char*' is forbidden"; |
---|
442 | string const_perl_arg = constCastTo(perl_arg, CONST_CHAR_PTR); // ensure C uses 'const char *' |
---|
443 | return convertExpression(const_perl_arg, CONST_CHAR_PTR, c_decl()); |
---|
444 | } |
---|
445 | return convertExpression(perl_arg, perl_decl(), c_decl()); |
---|
446 | } |
---|
447 | string convert_result_for_PERL(const string& c_expr) const { |
---|
448 | arb_assert(type_class != HEAP_COPY); |
---|
449 | if (perl_decl() == CHAR_PTR) { |
---|
450 | string const_c_expr = convertExpression(c_expr, c_decl(), CONST_CHAR_PTR); |
---|
451 | return constCastTo(const_c_expr, CHAR_PTR); |
---|
452 | } |
---|
453 | return convertExpression(c_expr, c_decl(), perl_decl()); |
---|
454 | } |
---|
455 | |
---|
456 | #if defined(TRACE) |
---|
457 | void dump_if_impossible_in_xsub(FILE *out) const { |
---|
458 | if (!possible_in_xsub()) { |
---|
459 | fprintf(out, "TRACE: - impossible type '%s' (TypeClass='%s')\n", |
---|
460 | c_type.c_str(), get_TypeClass_name(type_class)); |
---|
461 | } |
---|
462 | } |
---|
463 | #endif // TRACE |
---|
464 | |
---|
465 | }; |
---|
466 | |
---|
467 | TypeMap Type::globalTypemap; |
---|
468 | |
---|
469 | // ------------------ |
---|
470 | // Parameter |
---|
471 | |
---|
472 | class Parameter { |
---|
473 | Type type; |
---|
474 | string name; |
---|
475 | |
---|
476 | static long nonameCount; |
---|
477 | |
---|
478 | public: |
---|
479 | Parameter() {} |
---|
480 | Parameter(const char *code) { |
---|
481 | const char *last = strchr(code, 0)-1; |
---|
482 | while (last[0] == ' ') --last; |
---|
483 | |
---|
484 | const char *name_start = last; |
---|
485 | while (name_start >= code && is_ID_char(name_start[0])) --name_start; |
---|
486 | name_start++; |
---|
487 | |
---|
488 | if (name_start>code) { |
---|
489 | string type_def(code, name_start-code); |
---|
490 | name = string(name_start, last-name_start+1); |
---|
491 | type = Type(type_def.c_str()); |
---|
492 | |
---|
493 | if (type.possible_in_xsub() && !type.isVoid() && name.empty()) { |
---|
494 | name = GBS_global_string("noName%li", ++nonameCount); |
---|
495 | } |
---|
496 | } |
---|
497 | else if (strcmp(name_start, "void") == 0) { |
---|
498 | string no_type(name_start, last-name_start+1); |
---|
499 | name = ""; |
---|
500 | type = Type(no_type.c_str()); |
---|
501 | } |
---|
502 | else { |
---|
503 | throw string("can't parse '")+code+"' (expected 'type name')"; |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | const string& get_name() const { return name; } |
---|
508 | const Type& get_type() const { return type; } |
---|
509 | |
---|
510 | TypeClass get_TypeClass() const { return get_type().get_TypeClass(); } |
---|
511 | bool isVoid() const { return get_TypeClass() == VOID; } |
---|
512 | |
---|
513 | string perl_typed_param() const { return concat_type_and_name(type.perl_decl(), name); } |
---|
514 | string c_typed_param () const { return concat_type_and_name(type.c_decl (), name); } |
---|
515 | }; |
---|
516 | |
---|
517 | long Parameter::nonameCount = 0; |
---|
518 | |
---|
519 | // ------------------ |
---|
520 | // Prototype |
---|
521 | |
---|
522 | typedef vector<Parameter> Arguments; |
---|
523 | typedef Arguments::const_iterator ArgumentIter; |
---|
524 | |
---|
525 | class Prototype { |
---|
526 | Parameter function; // return-type + function_name |
---|
527 | Arguments arguments; |
---|
528 | |
---|
529 | void parse_arguments(const char *arg_list) { |
---|
530 | const char *comma = next_comma_outside_parens(arg_list); |
---|
531 | if (comma) { |
---|
532 | { |
---|
533 | char *first_param = ARB_strpartdup(arg_list, comma-1); |
---|
534 | arguments.push_back(Parameter(first_param)); |
---|
535 | free(first_param); |
---|
536 | } |
---|
537 | parse_arguments(comma+1); |
---|
538 | } |
---|
539 | else { // only one parameter |
---|
540 | arguments.push_back(Parameter(arg_list)); |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | public: |
---|
545 | Prototype(const char *code) { |
---|
546 | size_t open_paren, close_paren; |
---|
547 | if (!find_open_close_paren(code, open_paren, close_paren)) { |
---|
548 | throw "expected parenthesis"; |
---|
549 | } |
---|
550 | |
---|
551 | string return_type_and_name(code, open_paren); |
---|
552 | function = Parameter(return_type_and_name.c_str()); |
---|
553 | |
---|
554 | string arg_list(code+open_paren+1, close_paren-open_paren-1); |
---|
555 | parse_arguments(arg_list.c_str()); |
---|
556 | } |
---|
557 | |
---|
558 | const Type& get_return_type() const { return function.get_type(); } |
---|
559 | const string& get_function_name() const { return function.get_name(); } |
---|
560 | |
---|
561 | ArgumentIter args_begin() const { return arguments.begin(); } |
---|
562 | ArgumentIter args_end() const { return arguments.end(); } |
---|
563 | |
---|
564 | string argument_names_list() const { |
---|
565 | string argument_list; |
---|
566 | bool first = true; |
---|
567 | ArgumentIter arg_end = arguments.end(); |
---|
568 | |
---|
569 | for (ArgumentIter param = arguments.begin(); param != arg_end; ++param) { |
---|
570 | if (!param->isVoid()) { |
---|
571 | if (first) first = false; |
---|
572 | else argument_list += ", "; |
---|
573 | |
---|
574 | argument_list += param->get_name(); |
---|
575 | } |
---|
576 | } |
---|
577 | return argument_list; |
---|
578 | } |
---|
579 | |
---|
580 | string call_arguments() const { |
---|
581 | string argument_list; |
---|
582 | bool first = true; |
---|
583 | ArgumentIter arg_end = arguments.end(); |
---|
584 | |
---|
585 | for (ArgumentIter arg = arguments.begin(); arg != arg_end; ++arg) { |
---|
586 | if (!arg->isVoid()) { |
---|
587 | if (first) first = false; |
---|
588 | else argument_list += ", "; |
---|
589 | |
---|
590 | argument_list += arg->get_type().convert_argument_for_C(arg->get_name()); |
---|
591 | } |
---|
592 | } |
---|
593 | return argument_list; |
---|
594 | } |
---|
595 | |
---|
596 | bool possible_as_xsub() const { |
---|
597 | if (get_return_type().possible_in_xsub()) { |
---|
598 | ArgumentIter arg_end = arguments.end(); |
---|
599 | for (ArgumentIter arg = arguments.begin(); arg != arg_end; ++arg) { |
---|
600 | if (!arg->get_type().possible_in_xsub()) { |
---|
601 | return false; |
---|
602 | } |
---|
603 | } |
---|
604 | return true; |
---|
605 | } |
---|
606 | return false; |
---|
607 | } |
---|
608 | |
---|
609 | #if defined(TRACE) |
---|
610 | void dump_types_impossible_in_xsub(FILE *out) const { |
---|
611 | get_return_type().dump_if_impossible_in_xsub(out); |
---|
612 | ArgumentIter arg_end = arguments.end(); |
---|
613 | for (ArgumentIter arg = arguments.begin(); arg != arg_end; ++arg) { |
---|
614 | arg->get_type().dump_if_impossible_in_xsub(out); |
---|
615 | } |
---|
616 | } |
---|
617 | #endif // TRACE |
---|
618 | }; |
---|
619 | |
---|
620 | inline void trim(string& text) { |
---|
621 | const char *whiteSpace = " \t"; |
---|
622 | size_t leading = text.find_first_not_of(whiteSpace); |
---|
623 | size_t trailing = text.find_last_not_of(whiteSpace, leading); |
---|
624 | |
---|
625 | if (trailing != string::npos) { |
---|
626 | text = text.substr(leading, text.length()-leading-trailing); |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | void TypeMap::load(LineReader& typemap_reader) { |
---|
631 | string line; |
---|
632 | while (typemap_reader.getLine(line)) { |
---|
633 | if (line == "TYPEMAP") { |
---|
634 | while (typemap_reader.getLine(line)) { |
---|
635 | trim(line); |
---|
636 | if (!line.empty()) { |
---|
637 | Parameter typemapping(line.c_str()); |
---|
638 | const string& c_type = typemapping.get_type().c_decl(); |
---|
639 | defined_types.insert(c_type); |
---|
640 | } |
---|
641 | } |
---|
642 | return; |
---|
643 | } |
---|
644 | } |
---|
645 | |
---|
646 | throw InputFileError(typemap_reader, "Expected to see 'TYPEMAP'"); |
---|
647 | } |
---|
648 | |
---|
649 | // ---------------- |
---|
650 | // Package |
---|
651 | |
---|
652 | class Package : virtual Noncopyable { |
---|
653 | string prefix; // e.g. 'P2A' or 'P2AT' |
---|
654 | string name; // e.g. 'ARB' or 'BIO' |
---|
655 | GBS_strstruct *generated_code; |
---|
656 | GB_HASH *functions_to_skip; |
---|
657 | |
---|
658 | public: |
---|
659 | Package(const char *name_, const char *prefix_) : |
---|
660 | prefix(prefix_), |
---|
661 | name(name_) |
---|
662 | { |
---|
663 | generated_code = GBS_stropen(100000); |
---|
664 | functions_to_skip = GBS_create_hash(1000, GB_MIND_CASE); |
---|
665 | } |
---|
666 | ~Package() { |
---|
667 | GBS_free_hash(functions_to_skip); |
---|
668 | GBS_strforget(generated_code); |
---|
669 | } |
---|
670 | |
---|
671 | bool matches_package_prefix(const string& text) const { return text.find(prefix) == 0 && text.at(prefix.length()) == '_'; } |
---|
672 | |
---|
673 | void mark_function_defined(const string& function) { GBS_write_hash(functions_to_skip, function.c_str(), 1); } |
---|
674 | bool not_defined(const string& function) const { return GBS_read_hash(functions_to_skip, function.c_str()) == 0; } |
---|
675 | |
---|
676 | const string& get_prefix() const { return prefix; } |
---|
677 | |
---|
678 | void append_code(const string& code) { GBS_strncat(generated_code, code.c_str(), code.length()); } |
---|
679 | void append_code(const char *code) { append_code(string(code)); } |
---|
680 | void append_code(char code) { GBS_chrcat(generated_code, code); } |
---|
681 | |
---|
682 | void append_linefeed(size_t count = 1) { while (count--) append_code("\n"); } |
---|
683 | |
---|
684 | void print_xsubs(FILE *file) { |
---|
685 | fputs("# --------------------------------------------------------------------------------\n", file); |
---|
686 | fprintf(file, "MODULE = ARB PACKAGE = %s PREFIX = %s_\n\n", name.c_str(), prefix.c_str()); |
---|
687 | fputs(GBS_mempntr(generated_code), file); |
---|
688 | } |
---|
689 | }; |
---|
690 | |
---|
691 | // ---------------------- |
---|
692 | // xsubGenerator |
---|
693 | |
---|
694 | class xsubGenerator { |
---|
695 | Package arb; |
---|
696 | Package bio; |
---|
697 | |
---|
698 | void generate_xsub(const Prototype& prototype); |
---|
699 | public: |
---|
700 | xsubGenerator() : |
---|
701 | arb("ARB", "P2A"), |
---|
702 | bio("BIO", "P2AT") |
---|
703 | {} |
---|
704 | |
---|
705 | void mark_handcoded_functions(BufferedFileReader& handcoded) { |
---|
706 | string line; |
---|
707 | while (handcoded.getLine(line)) { |
---|
708 | Package *package = NULp; |
---|
709 | |
---|
710 | if (arb.matches_package_prefix(line)) package = &arb; |
---|
711 | else if (bio.matches_package_prefix(line)) package = &bio; |
---|
712 | |
---|
713 | if (package) { |
---|
714 | size_t open_paren = line.find('('); |
---|
715 | if (open_paren != string::npos) { |
---|
716 | package->mark_function_defined(line.substr(0, open_paren)); |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | } |
---|
721 | handcoded.rewind(); |
---|
722 | } |
---|
723 | |
---|
724 | void generate_all_xsubs(LineReader& prototype_reader); |
---|
725 | |
---|
726 | void print_xsubs(FILE *out) { |
---|
727 | arb.print_xsubs(out); |
---|
728 | bio.print_xsubs(out); |
---|
729 | } |
---|
730 | }; |
---|
731 | |
---|
732 | inline string prefix_before(const string& name, char separator) { |
---|
733 | size_t sep_offset = name.find_first_of(separator); |
---|
734 | if (sep_offset != string::npos) { |
---|
735 | return name.substr(0, sep_offset); |
---|
736 | } |
---|
737 | return ""; |
---|
738 | } |
---|
739 | |
---|
740 | inline void GBS_spaces(GBS_strstruct *out, int space_count) { |
---|
741 | const char *spaces = " "; |
---|
742 | arb_assert(space_count <= 10); |
---|
743 | GBS_strncat(out, spaces+(10-space_count), space_count); |
---|
744 | } |
---|
745 | |
---|
746 | void xsubGenerator::generate_xsub(const Prototype& prototype) { |
---|
747 | const string& c_function_name = prototype.get_function_name(); |
---|
748 | string function_prefix = prefix_before(c_function_name, '_'); |
---|
749 | Package *package = NULp; |
---|
750 | |
---|
751 | if (function_prefix == "GB" || function_prefix == "GBC") { |
---|
752 | package = &arb; |
---|
753 | } |
---|
754 | else if (function_prefix == "GBT" || function_prefix == "GEN") { |
---|
755 | package = &bio; |
---|
756 | } |
---|
757 | |
---|
758 | if (package) { |
---|
759 | string perl_function_name = package->get_prefix() + c_function_name.substr(function_prefix.length()); |
---|
760 | |
---|
761 | if (package->not_defined(perl_function_name)) { |
---|
762 | package->mark_function_defined(perl_function_name); // do NOT xsub functions twice |
---|
763 | |
---|
764 | // generate xsub function header |
---|
765 | const Type& return_type = prototype.get_return_type(); |
---|
766 | { |
---|
767 | string argument_names_list = prototype.argument_names_list(); |
---|
768 | string function_header = return_type.isVoid() ? "void" : return_type.perl_decl(); |
---|
769 | |
---|
770 | function_header += '\n'; |
---|
771 | function_header += perl_function_name+'('+argument_names_list+")\n"; |
---|
772 | |
---|
773 | ArgumentIter arg_end = prototype.args_end(); |
---|
774 | for (ArgumentIter arg = prototype.args_begin(); arg != arg_end; ++arg) { |
---|
775 | if (!arg->isVoid()) { |
---|
776 | string type_decl = string(" ") + arg->perl_typed_param() + '\n'; |
---|
777 | function_header += type_decl; |
---|
778 | } |
---|
779 | } |
---|
780 | |
---|
781 | package->append_code(function_header); |
---|
782 | package->append_linefeed(); |
---|
783 | } |
---|
784 | |
---|
785 | // generate xsub function body |
---|
786 | string call_c_function = c_function_name+'('+prototype.call_arguments()+")"; |
---|
787 | if (return_type.isVoid()) { |
---|
788 | package->append_code(" PPCODE:\n "); |
---|
789 | package->append_code(call_c_function); |
---|
790 | package->append_code(';'); |
---|
791 | } |
---|
792 | else { |
---|
793 | string assign_RETVAL = " "; |
---|
794 | |
---|
795 | switch (return_type.get_TypeClass()) { |
---|
796 | case CONST_CHAR: |
---|
797 | case CONVERSION_FUNCTION: |
---|
798 | case SIMPLE_TYPE: |
---|
799 | case TYPEMAPPED: |
---|
800 | assign_RETVAL = string(" RETVAL = ") + return_type.convert_result_for_PERL(call_c_function); |
---|
801 | break; |
---|
802 | |
---|
803 | case HEAP_COPY: |
---|
804 | // temporarily store heapcopy in static pointer |
---|
805 | // defined at ../PERL2ARB/ARB_ext.c@static_pntr |
---|
806 | assign_RETVAL = |
---|
807 | string(" freeset(static_pntr, ") + call_c_function+");\n"+ |
---|
808 | " RETVAL = static_pntr"; |
---|
809 | break; |
---|
810 | |
---|
811 | case VOID: |
---|
812 | case INVALID_TYPE: |
---|
813 | case CANT_HANDLE: |
---|
814 | case FORBIDDEN: |
---|
815 | arb_assert(0); |
---|
816 | } |
---|
817 | |
---|
818 | string body = |
---|
819 | string(" CODE:\n") + |
---|
820 | assign_RETVAL + ";\n" + |
---|
821 | "\n" + |
---|
822 | " OUTPUT:\n" + |
---|
823 | " RETVAL"; |
---|
824 | |
---|
825 | package->append_code(body); |
---|
826 | } |
---|
827 | package->append_linefeed(3); |
---|
828 | } |
---|
829 | #if defined(TRACE) |
---|
830 | else { |
---|
831 | fprintf(stderr, "TRACE: '%s' skipped\n", c_function_name.c_str()); |
---|
832 | } |
---|
833 | #endif // TRACE |
---|
834 | } |
---|
835 | #if defined(TRACE) |
---|
836 | else { |
---|
837 | fprintf(stderr, "TRACE: Skipped function: '%s' (prefix='%s')\n", c_function_name.c_str(), function_prefix.c_str()); |
---|
838 | } |
---|
839 | #endif // TRACE |
---|
840 | } |
---|
841 | |
---|
842 | static void print_prototype_parse_error(LineReader& prototype_reader, const char *err, const char *prototype) { |
---|
843 | InputFileError(prototype_reader, GBS_global_string("%s (can't xsub '%s')", err, prototype)).print(); |
---|
844 | } |
---|
845 | |
---|
846 | void xsubGenerator::generate_all_xsubs(LineReader& prototype_reader) { |
---|
847 | bool error_occurred = false; |
---|
848 | string line; |
---|
849 | int open_brace_counter = 0; |
---|
850 | |
---|
851 | while (prototype_reader.getLine(line)) { |
---|
852 | const char *lineStart = line.c_str(); |
---|
853 | size_t leading_whitespace = strspn(lineStart, " \t"); |
---|
854 | const char *prototype = lineStart+leading_whitespace; |
---|
855 | |
---|
856 | if (!open_brace_counter && is_prototype(prototype)) { |
---|
857 | try { |
---|
858 | Prototype proto(prototype); |
---|
859 | if (proto.possible_as_xsub()) { |
---|
860 | generate_xsub(proto); |
---|
861 | } |
---|
862 | #if defined(TRACE) |
---|
863 | else { |
---|
864 | fprintf(stderr, "TRACE: prototype '%s' not possible as xsub\n", prototype); |
---|
865 | proto.dump_types_impossible_in_xsub(stderr); |
---|
866 | } |
---|
867 | #endif // TRACE |
---|
868 | } |
---|
869 | catch(string& err) { |
---|
870 | print_prototype_parse_error(prototype_reader, err.c_str(), prototype); |
---|
871 | error_occurred = true; |
---|
872 | } |
---|
873 | catch(const char *err) { |
---|
874 | print_prototype_parse_error(prototype_reader, err, prototype); |
---|
875 | error_occurred = true; |
---|
876 | } |
---|
877 | catch(...) { arb_assert(0); } |
---|
878 | } |
---|
879 | else { |
---|
880 | #if defined(TRACE) |
---|
881 | fprintf(stderr, "TRACE: not a prototype: '%s'\n", prototype); |
---|
882 | #endif // TRACE |
---|
883 | trace_over_braces(prototype, open_brace_counter); |
---|
884 | } |
---|
885 | } |
---|
886 | |
---|
887 | if (error_occurred) throw ProgramError("could not generate xsubs for all prototypes"); |
---|
888 | } |
---|
889 | |
---|
890 | static void print_xs_default(BufferedFileReader& xs_default, const char *proto_filename, FILE *out) { |
---|
891 | fprintf(out, |
---|
892 | "/* This file has been generated from\n" |
---|
893 | " * %s and\n" |
---|
894 | " * %s\n */\n" |
---|
895 | "\n", |
---|
896 | xs_default.getFilename().c_str(), |
---|
897 | proto_filename); |
---|
898 | |
---|
899 | xs_default.copyTo(out); |
---|
900 | xs_default.rewind(); |
---|
901 | } |
---|
902 | |
---|
903 | static BufferedFileReader *createFileBuffer(const char *filename) { |
---|
904 | FILE *in = fopen(filename, "rt"); |
---|
905 | if (!in) { |
---|
906 | GB_export_IO_error("reading", filename); |
---|
907 | throw ProgramError(GB_await_error()); |
---|
908 | } |
---|
909 | return new BufferedFileReader(filename, in); |
---|
910 | } |
---|
911 | static BufferedFileReader *createCommentSkippingFileBuffer(const char *filename) { |
---|
912 | FILE *in = fopen(filename, "rt"); |
---|
913 | if (!in) { |
---|
914 | GB_export_IO_error("reading", filename); |
---|
915 | throw ProgramError(GB_await_error()); |
---|
916 | } |
---|
917 | return new CommentSkippingFileBuffer(filename, in, "/*", "*/", "//"); |
---|
918 | } |
---|
919 | |
---|
920 | |
---|
921 | |
---|
922 | static void loadTypemap(const char *typemap_filename) { |
---|
923 | SmartPtr<BufferedFileReader> typemap = createFileBuffer(typemap_filename); |
---|
924 | Type::globalTypemap.load(*typemap); |
---|
925 | } |
---|
926 | |
---|
927 | int ARB_main(int argc, char **argv) { |
---|
928 | bool error_occurred = false; |
---|
929 | try { |
---|
930 | if (argc != 4) { |
---|
931 | fputs("arb_proto_2_xsub converts GB_prototypes for the ARB perl interface\n" |
---|
932 | "Usage: arb_proto_2_xsub <prototypes.h> <xs-header> <typemap>\n" |
---|
933 | " <prototypes.h> contains prototypes of ARBDB library\n" |
---|
934 | " <xs-header> may contain prototypes, which will not be\n" |
---|
935 | " overwritten by generated default prototypes\n" |
---|
936 | " <typemap> contains type-conversion-definitions, which can\n" |
---|
937 | " be handled by xsubpp\n" |
---|
938 | , stderr); |
---|
939 | |
---|
940 | throw ProgramError("Wrong number of command line arguments"); |
---|
941 | } |
---|
942 | else { |
---|
943 | const char *proto_filename = argv[1]; |
---|
944 | const char *xs_default_name = argv[2]; |
---|
945 | const char *typemap_filename = argv[3]; |
---|
946 | |
---|
947 | loadTypemap(typemap_filename); |
---|
948 | |
---|
949 | // generate xsubs |
---|
950 | SmartPtr<BufferedFileReader> xs_default = createFileBuffer(xs_default_name); |
---|
951 | |
---|
952 | xsubGenerator generator; |
---|
953 | generator.mark_handcoded_functions(*xs_default); |
---|
954 | { |
---|
955 | SmartPtr<BufferedFileReader> prototypes = createCommentSkippingFileBuffer(proto_filename); |
---|
956 | generator.generate_all_xsubs(*prototypes); |
---|
957 | } |
---|
958 | |
---|
959 | // write xsubs |
---|
960 | FILE *out = stdout; |
---|
961 | print_xs_default(*xs_default, proto_filename, out); |
---|
962 | generator.print_xsubs(out); |
---|
963 | } |
---|
964 | } |
---|
965 | catch (Error& err) { |
---|
966 | err.print(); |
---|
967 | error_occurred = true; |
---|
968 | } |
---|
969 | catch (...) { |
---|
970 | ProgramError("Unexpected exception").print(); |
---|
971 | error_occurred = true; |
---|
972 | } |
---|
973 | |
---|
974 | if (error_occurred) { |
---|
975 | ProgramError("failed").print(); |
---|
976 | return EXIT_FAILURE; |
---|
977 | } |
---|
978 | return EXIT_SUCCESS; |
---|
979 | } |
---|
980 | |
---|
981 | |
---|
982 | // -------------------------------------------------------------------------------- |
---|
983 | |
---|
984 | #ifdef UNIT_TESTS |
---|
985 | #ifndef TEST_UNIT_H |
---|
986 | #include <test_unit.h> |
---|
987 | #endif |
---|
988 | #include <test_runtool.h> |
---|
989 | |
---|
990 | |
---|
991 | // #define TEST_AUTO_UPDATE // uncomment this to update expected results |
---|
992 | |
---|
993 | void TEST_arb_proto_2_xsub() { |
---|
994 | TEST_EXPECT_ZERO(chdir("xsub")); |
---|
995 | |
---|
996 | const char *outname = "ap2x.out"; |
---|
997 | const char *expected = "ap2x.out.expected"; |
---|
998 | |
---|
999 | char *cmd = GBS_global_string_copy("arb_proto_2_xsub ptype.header default.xs typemap > %s", outname); |
---|
1000 | TEST_RUN_TOOL(cmd); |
---|
1001 | |
---|
1002 | #if defined(TEST_AUTO_UPDATE) |
---|
1003 | TEST_COPY_FILE(outname, expected); |
---|
1004 | #else |
---|
1005 | TEST_EXPECT_TEXTFILE_DIFFLINES(expected, outname, 0); |
---|
1006 | #endif |
---|
1007 | TEST_EXPECT_ZERO_OR_SHOW_ERRNO(unlink(outname)); |
---|
1008 | |
---|
1009 | free(cmd); |
---|
1010 | } |
---|
1011 | |
---|
1012 | #endif // UNIT_TESTS |
---|
1013 | |
---|
1014 | // -------------------------------------------------------------------------------- |
---|