blob: b24a9030a7197cbcf8e908e197dd5b7b5a3632d6 [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee31985722006-05-24 21:45:31 +00007/**
8 * thrift - a lightweight cross-language rpc/serialization tool
9 *
10 * This file contains the main compiler engine for Thrift, which invokes the
11 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +000012 * code for each language lives in a file by the language name under the
13 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +000014 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <stdarg.h>
21#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000022#include <sys/types.h>
23#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000024#include <errno.h>
Mark Slee31985722006-05-24 21:45:31 +000025
David Reiss204420f2008-01-11 20:59:03 +000026#ifdef MINGW
27# include <windows.h> /* for GetFullPathName */
28# include <limits.h>
29#endif
30
Mark Sleef0712dc2006-10-25 19:03:57 +000031// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000032#include "globals.h"
33
34#include "main.h"
35#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000036#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000037#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000038#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000039#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000040#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000041#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000042#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000043#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000044#include "generate/t_erl_generator.h"
Christopher Pirob97b89d2007-09-18 00:07:42 +000045#include "generate/t_ocaml_generator.h"
iproctorff8eb922007-07-25 19:06:13 +000046#include "generate/t_hs_generator.h"
Mark Slee7e9eea42007-09-10 21:00:23 +000047#include "generate/t_cocoa_generator.h"
Mark Sleeefd37f12007-11-20 05:13:09 +000048#include "generate/t_st_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000049#include "generate/t_csharp_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000050
51using namespace std;
52
Mark Sleef5377b32006-10-10 01:42:59 +000053/**
54 * Global program tree
55 */
Mark Slee31985722006-05-24 21:45:31 +000056t_program* g_program;
57
Mark Sleef5377b32006-10-10 01:42:59 +000058/**
Mark Sleef0712dc2006-10-25 19:03:57 +000059 * Global types
60 */
61
62t_type* g_type_void;
63t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000064t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000065t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000066t_type* g_type_bool;
67t_type* g_type_byte;
68t_type* g_type_i16;
69t_type* g_type_i32;
70t_type* g_type_i64;
71t_type* g_type_double;
72
73/**
74 * Global scope
75 */
76t_scope* g_scope;
77
78/**
79 * Parent scope to also parse types
80 */
81t_scope* g_parent_scope;
82
83/**
84 * Prefix for putting types in parent scope
85 */
86string g_parent_prefix;
87
88/**
89 * Parsing pass
90 */
91PARSE_MODE g_parse_mode;
92
93/**
94 * Current directory of file being parsed
95 */
96string g_curdir;
97
98/**
99 * Current file being parsed
100 */
101string g_curpath;
102
103/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000104 * Search path for inclusions
105 */
Mark Slee2329a832006-11-09 00:23:30 +0000106vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000107
108/**
kholst76f2c882008-01-16 02:47:41 +0000109 * Should C++ include statements use path prefixes for other thrift-generated
110 * header files
111 */
112bool g_cpp_use_include_prefix = false;
113
114/**
Mark Sleef5377b32006-10-10 01:42:59 +0000115 * Global debug state
116 */
Mark Slee31985722006-05-24 21:45:31 +0000117int g_debug = 0;
118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000120 * Warning level
121 */
122int g_warn = 1;
123
124/**
125 * Verbose output
126 */
127int g_verbose = 0;
128
129/**
Mark Sleef5377b32006-10-10 01:42:59 +0000130 * Global time string
131 */
Mark Slee31985722006-05-24 21:45:31 +0000132char* g_time_str;
133
Mark Slee31985722006-05-24 21:45:31 +0000134/**
David Reisscbd4bac2007-08-14 17:12:33 +0000135 * The last parsed doctext comment.
136 */
137char* g_doctext;
138
139/**
140 * The location of the last parsed doctext comment.
141 */
142int g_doctext_lineno;
143
144/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000145 * Flags to control code generation
146 */
147bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000148bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000149bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000150bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000151bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000152bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000153bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000154bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000155bool gen_php = false;
156bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000157bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000158bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000159bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000160bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000161bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000162bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000163bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000164bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000165bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000166bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000167bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000168bool gen_recurse = false;
169
170/**
David Reiss204420f2008-01-11 20:59:03 +0000171 * MinGW doesn't have realpath, so use fallback implementation in that case,
172 * otherwise this just calls through to realpath
173 */
174char *saferealpath(const char *path, char *resolved_path) {
175#ifdef MINGW
176 char buf[MAX_PATH];
177 char* basename;
178 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
179 if (len == 0 || len > MAX_PATH - 1){
180 strcpy(resolved_path, path);
181 } else {
182 CharLowerBuff(buf, len);
183 strcpy(resolved_path, buf);
184 }
185 return resolved_path;
186#else
187 return realpath(path, resolved_path);
188#endif
189}
190
191
192/**
Mark Slee31985722006-05-24 21:45:31 +0000193 * Report an error to the user. This is called yyerror for historical
194 * reasons (lex and yacc expect the error reporting routine to be called
195 * this). Call this function to report any errors to the user.
196 * yyerror takes printf style arguments.
197 *
198 * @param fmt C format string followed by additional arguments
199 */
200void yyerror(char* fmt, ...) {
201 va_list args;
202 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000203 "[ERROR:%s:%d] (last token was '%s')\n",
204 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000205 yylineno,
206 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000207
208 va_start(args, fmt);
209 vfprintf(stderr, fmt, args);
210 va_end(args);
211
212 fprintf(stderr, "\n");
213}
214
215/**
216 * Prints a debug message from the parser.
217 *
218 * @param fmt C format string followed by additional arguments
219 */
220void pdebug(char* fmt, ...) {
221 if (g_debug == 0) {
222 return;
223 }
224 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000225 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000226 va_start(args, fmt);
227 vprintf(fmt, args);
228 va_end(args);
229 printf("\n");
230}
231
232/**
233 * Prints a verbose output mode message
234 *
235 * @param fmt C format string followed by additional arguments
236 */
237void pverbose(char* fmt, ...) {
238 if (g_verbose == 0) {
239 return;
240 }
241 va_list args;
242 va_start(args, fmt);
243 vprintf(fmt, args);
244 va_end(args);
245}
246
247/**
248 * Prints a warning message
249 *
250 * @param fmt C format string followed by additional arguments
251 */
252void pwarning(int level, char* fmt, ...) {
253 if (g_warn < level) {
254 return;
255 }
256 va_list args;
257 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000258 va_start(args, fmt);
259 vprintf(fmt, args);
260 va_end(args);
261 printf("\n");
262}
263
264/**
265 * Prints a failure message and exits
266 *
267 * @param fmt C format string followed by additional arguments
268 */
Mark Slee30152872006-11-28 01:24:07 +0000269void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000270 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000272 va_start(args, fmt);
273 vfprintf(stderr, fmt, args);
274 va_end(args);
275 printf("\n");
276 exit(1);
277}
278
279/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000280 * Converts a string filename into a thrift program name
281 */
282string program_name(string filename) {
283 string::size_type slash = filename.rfind("/");
284 if (slash != string::npos) {
285 filename = filename.substr(slash+1);
286 }
287 string::size_type dot = filename.rfind(".");
288 if (dot != string::npos) {
289 filename = filename.substr(0, dot);
290 }
291 return filename;
292}
293
294/**
295 * Gets the directory path of a filename
296 */
297string directory_name(string filename) {
298 string::size_type slash = filename.rfind("/");
299 // No slash, just use the current directory
300 if (slash == string::npos) {
301 return ".";
302 }
303 return filename.substr(0, slash);
304}
305
306/**
307 * Finds the appropriate file path for the given filename
308 */
309string include_file(string filename) {
310 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000311 if (filename[0] == '/') {
312 // Realpath!
313 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000314 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000315 pwarning(0, "Cannot open include file %s\n", filename.c_str());
316 return std::string();
317 }
Mark Slee2c44d202007-05-16 02:18:07 +0000318
319 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000320 struct stat finfo;
321 if (stat(rp, &finfo) == 0) {
322 return rp;
323 }
324 } else { // relative path, start searching
325 // new search path with current dir global
326 vector<string> sp = g_incl_searchpath;
327 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000328
Martin Kraemer32c66e12006-11-09 00:06:36 +0000329 // iterate through paths
330 vector<string>::iterator it;
331 for (it = sp.begin(); it != sp.end(); it++) {
332 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000333
Martin Kraemer32c66e12006-11-09 00:06:36 +0000334 // Realpath!
335 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000336 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000337 continue;
338 }
Mark Slee2c44d202007-05-16 02:18:07 +0000339
Martin Kraemer32c66e12006-11-09 00:06:36 +0000340 // Stat this files
341 struct stat finfo;
342 if (stat(rp, &finfo) == 0) {
343 return rp;
344 }
345 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000346 }
Mark Slee2c44d202007-05-16 02:18:07 +0000347
Mark Sleef0712dc2006-10-25 19:03:57 +0000348 // Uh oh
349 pwarning(0, "Could not find include file %s\n", filename.c_str());
350 return std::string();
351}
352
353/**
David Reisscbd4bac2007-08-14 17:12:33 +0000354 * Clears any previously stored doctext string.
355 * Also prints a warning if we are discarding information.
356 */
357void clear_doctext() {
358 if (g_doctext != NULL) {
359 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
360 }
361 free(g_doctext);
362 g_doctext = NULL;
363}
364
365/**
David Reiss1ac05802007-07-30 22:00:27 +0000366 * Cleans up text commonly found in doxygen-like comments
367 *
368 * Warning: if you mix tabs and spaces in a non-uniform way,
369 * you will get what you deserve.
370 */
371char* clean_up_doctext(char* doctext) {
372 // Convert to C++ string, and remove Windows's carriage returns.
373 string docstring = doctext;
374 docstring.erase(
375 remove(docstring.begin(), docstring.end(), '\r'),
376 docstring.end());
377
378 // Separate into lines.
379 vector<string> lines;
380 string::size_type pos = string::npos;
381 string::size_type last;
382 while (true) {
383 last = (pos == string::npos) ? 0 : pos+1;
384 pos = docstring.find('\n', last);
385 if (pos == string::npos) {
386 // First bit of cleaning. If the last line is only whitespace, drop it.
387 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
388 if (nonwhite != string::npos) {
389 lines.push_back(docstring.substr(last));
390 }
391 break;
392 }
393 lines.push_back(docstring.substr(last, pos-last));
394 }
395
396 // A very profound docstring.
397 if (lines.empty()) {
398 return NULL;
399 }
400
401 // Clear leading whitespace from the first line.
402 pos = lines.front().find_first_not_of(" \t");
403 lines.front().erase(0, pos);
404
405 // If every nonblank line after the first has the same number of spaces/tabs,
406 // then a star, remove them.
407 bool have_prefix = true;
408 bool found_prefix = false;
409 string::size_type prefix_len = 0;
410 vector<string>::iterator l_iter;
411 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
412 if (l_iter->empty()) {
413 continue;
414 }
415
416 pos = l_iter->find_first_not_of(" \t");
417 if (!found_prefix) {
418 if (pos != string::npos) {
419 if (l_iter->at(pos) == '*') {
420 found_prefix = true;
421 prefix_len = pos;
422 } else {
423 have_prefix = false;
424 break;
425 }
426 } else {
427 // Whitespace-only line. Truncate it.
428 l_iter->clear();
429 }
430 } else if (l_iter->size() > pos
431 && l_iter->at(pos) == '*'
432 && pos == prefix_len) {
433 // Business as usual.
434 } else if (pos == string::npos) {
435 // Whitespace-only line. Let's truncate it for them.
436 l_iter->clear();
437 } else {
438 // The pattern has been broken.
439 have_prefix = false;
440 break;
441 }
442 }
443
444 // If our prefix survived, delete it from every line.
445 if (have_prefix) {
446 // Get the star too.
447 prefix_len++;
448 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
449 l_iter->erase(0, prefix_len);
450 }
451 }
452
453 // Now delete the minimum amount of leading whitespace from each line.
454 prefix_len = string::npos;
455 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
456 if (l_iter->empty()) {
457 continue;
458 }
459 pos = l_iter->find_first_not_of(" \t");
460 if (pos != string::npos
461 && (prefix_len == string::npos || pos < prefix_len)) {
462 prefix_len = pos;
463 }
464 }
465
466 // If our prefix survived, delete it from every line.
467 if (prefix_len != string::npos) {
468 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
469 l_iter->erase(0, prefix_len);
470 }
471 }
472
473 // Remove trailing whitespace from every line.
474 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
475 pos = l_iter->find_last_not_of(" \t");
476 if (pos != string::npos && pos != l_iter->length()-1) {
477 l_iter->erase(pos+1);
478 }
479 }
480
481 // If the first line is empty, remove it.
482 // Don't do this earlier because a lot of steps skip the first line.
483 if (lines.front().empty()) {
484 lines.erase(lines.begin());
485 }
486
487 // Now rejoin the lines and copy them back into doctext.
488 docstring.clear();
489 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
490 docstring += *l_iter;
491 docstring += '\n';
492 }
493
494 assert(docstring.length() <= strlen(doctext));
495 strcpy(doctext, docstring.c_str());
496 return doctext;
497}
498
499/** Set to true to debug docstring parsing */
500static bool dump_docs = false;
501
502/**
503 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000504 * Only works for top-level definitions and the whole program doc
505 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000506 */
507void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000508 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000509 if (!progdoc.empty()) {
510 printf("Whole program doc:\n%s\n", progdoc.c_str());
511 }
David Reiss1ac05802007-07-30 22:00:27 +0000512 const vector<t_typedef*>& typedefs = program->get_typedefs();
513 vector<t_typedef*>::const_iterator t_iter;
514 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
515 t_typedef* td = *t_iter;
516 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000517 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
518 }
519 }
520 const vector<t_enum*>& enums = program->get_enums();
521 vector<t_enum*>::const_iterator e_iter;
522 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
523 t_enum* en = *e_iter;
524 if (en->has_doc()) {
525 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
526 }
527 }
528 const vector<t_const*>& consts = program->get_consts();
529 vector<t_const*>::const_iterator c_iter;
530 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
531 t_const* co = *c_iter;
532 if (co->has_doc()) {
533 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
534 }
535 }
536 const vector<t_struct*>& structs = program->get_structs();
537 vector<t_struct*>::const_iterator s_iter;
538 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
539 t_struct* st = *s_iter;
540 if (st->has_doc()) {
541 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
542 }
543 }
544 const vector<t_struct*>& xceptions = program->get_xceptions();
545 vector<t_struct*>::const_iterator x_iter;
546 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
547 t_struct* xn = *x_iter;
548 if (xn->has_doc()) {
549 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
550 }
551 }
552 const vector<t_service*>& services = program->get_services();
553 vector<t_service*>::const_iterator v_iter;
554 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
555 t_service* sv = *v_iter;
556 if (sv->has_doc()) {
557 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000558 }
559 }
560}
561
562/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000563 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000564 */
565void generate_all_fingerprints(t_program* program) {
566 const vector<t_struct*>& structs = program->get_structs();
567 vector<t_struct*>::const_iterator s_iter;
568 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
569 t_struct* st = *s_iter;
570 st->generate_fingerprint();
571 }
572
David Reissd779cbe2007-08-31 01:42:55 +0000573 const vector<t_struct*>& xceptions = program->get_xceptions();
574 vector<t_struct*>::const_iterator x_iter;
575 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
576 t_struct* st = *x_iter;
577 st->generate_fingerprint();
578 }
579
David Reiss3c5d2fd2008-02-08 21:58:06 +0000580 const vector<t_enum*>& enums = program->get_enums();
581 vector<t_enum*>::const_iterator e_iter;
582 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
583 t_enum* e = *e_iter;
584 e->generate_fingerprint();
585 }
586
David Reiss47557bc2007-09-04 21:31:04 +0000587 g_type_void->generate_fingerprint();
588
David Reiss18bf22d2007-08-28 20:49:17 +0000589 // If you want to generate fingerprints for implicit structures, start here.
590 /*
591 const vector<t_service*>& services = program->get_services();
592 vector<t_service*>::const_iterator v_iter;
593 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
594 t_service* sv = *v_iter;
595 }
596 */
597}
598
599/**
Mark Slee31985722006-05-24 21:45:31 +0000600 * Diplays the usage message and then exits with an error code.
601 */
602void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000603 fprintf(stderr, "Usage: thrift [options] file\n");
604 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000605 fprintf(stderr, " -cpp Generate C++ output files\n");
606 fprintf(stderr, " -java Generate Java output files\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000607 fprintf(stderr, " -javabean Generate Java bean-style output files\n");
Mark Slee2329a832006-11-09 00:23:30 +0000608 fprintf(stderr, " -php Generate PHP output files\n");
609 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000610 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
611 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000612 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000613 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000614 fprintf(stderr, " -py Generate Python output files\n");
David Reiss6495adb2007-12-18 03:37:30 +0000615 fprintf(stderr, " -pyns Generate Python new-style classes (with -py)\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000616 fprintf(stderr, " -rb Generate Ruby output files\n");
617 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000618 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000619 fprintf(stderr, " -erl Generate Erlang output files\n");
Christopher Pirob97b89d2007-09-18 00:07:42 +0000620 fprintf(stderr, " -ocaml Generate OCaml output files\n");
iproctorff8eb922007-07-25 19:06:13 +0000621 fprintf(stderr, " -hs Generate Haskell output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000622 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
David Reiss7f42bcf2008-01-11 20:59:12 +0000623 fprintf(stderr, " -csharp Generate C# output files\n");
Mark Sleeefd37f12007-11-20 05:13:09 +0000624 fprintf(stderr, " -st Generate Squeak/Smalltalk output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000625 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
626 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000627 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000628 fprintf(stderr, " searched for include directives\n");
kholst76f2c882008-01-16 02:47:41 +0000629 fprintf(stderr, " -cpp_use_include_prefix\n");
630 fprintf(stderr, " Make C++ include statements use path prefixes\n");
David Reissd779cbe2007-08-31 01:42:55 +0000631 fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n");
632 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000633 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
634 fprintf(stderr, " -strict Strict compiler warnings on\n");
635 fprintf(stderr, " -v[erbose] Verbose mode\n");
636 fprintf(stderr, " -r[ecurse] Also generate included files\n");
637 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000638 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
639 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
640 fprintf(stderr, " Keys and values are options passed to the generator.\n");
641 fprintf(stderr, " Many options will not require values.\n");
642 fprintf(stderr, "\n");
643 fprintf(stderr, "Available generators (and options):\n");
644
645 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
646 t_generator_registry::gen_map_t::iterator iter;
647 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
648 fprintf(stderr, " %s (%s):\n",
649 iter->second->get_short_name().c_str(),
650 iter->second->get_long_name().c_str());
651 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
652 }
Mark Slee31985722006-05-24 21:45:31 +0000653 exit(1);
654}
655
656/**
Mark Slee30152872006-11-28 01:24:07 +0000657 * You know, when I started working on Thrift I really thought it wasn't going
658 * to become a programming language because it was just a generator and it
659 * wouldn't need runtime type information and all that jazz. But then we
660 * decided to add constants, and all of a sudden that means runtime type
661 * validation and inference, except the "runtime" is the code generator
662 * runtime. Shit. I've been had.
663 */
664void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
665 if (type->is_void()) {
666 throw "type error: cannot declare a void const: " + name;
667 }
668
669 if (type->is_base_type()) {
670 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
671 switch (tbase) {
672 case t_base_type::TYPE_STRING:
673 if (value->get_type() != t_const_value::CV_STRING) {
674 throw "type error: const \"" + name + "\" was declared as string";
675 }
676 break;
677 case t_base_type::TYPE_BOOL:
678 if (value->get_type() != t_const_value::CV_INTEGER) {
679 throw "type error: const \"" + name + "\" was declared as bool";
680 }
681 break;
682 case t_base_type::TYPE_BYTE:
683 if (value->get_type() != t_const_value::CV_INTEGER) {
684 throw "type error: const \"" + name + "\" was declared as byte";
685 }
686 break;
687 case t_base_type::TYPE_I16:
688 if (value->get_type() != t_const_value::CV_INTEGER) {
689 throw "type error: const \"" + name + "\" was declared as i16";
690 }
691 break;
692 case t_base_type::TYPE_I32:
693 if (value->get_type() != t_const_value::CV_INTEGER) {
694 throw "type error: const \"" + name + "\" was declared as i32";
695 }
696 break;
697 case t_base_type::TYPE_I64:
698 if (value->get_type() != t_const_value::CV_INTEGER) {
699 throw "type error: const \"" + name + "\" was declared as i64";
700 }
701 break;
702 case t_base_type::TYPE_DOUBLE:
703 if (value->get_type() != t_const_value::CV_INTEGER &&
704 value->get_type() != t_const_value::CV_DOUBLE) {
705 throw "type error: const \"" + name + "\" was declared as double";
706 }
707 break;
708 default:
David Reissdd7796f2007-08-28 21:09:06 +0000709 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000710 }
711 } else if (type->is_enum()) {
712 if (value->get_type() != t_const_value::CV_INTEGER) {
713 throw "type error: const \"" + name + "\" was declared as enum";
714 }
715 } else if (type->is_struct() || type->is_xception()) {
716 if (value->get_type() != t_const_value::CV_MAP) {
717 throw "type error: const \"" + name + "\" was declared as struct/xception";
718 }
719 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
720 vector<t_field*>::const_iterator f_iter;
721
722 const map<t_const_value*, t_const_value*>& val = value->get_map();
723 map<t_const_value*, t_const_value*>::const_iterator v_iter;
724 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
725 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
726 throw "type error: " + name + " struct key must be string";
727 }
728 t_type* field_type = NULL;
729 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
730 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
731 field_type = (*f_iter)->get_type();
732 }
733 }
734 if (field_type == NULL) {
735 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
736 }
737
738 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
739 }
740 } else if (type->is_map()) {
741 t_type* k_type = ((t_map*)type)->get_key_type();
742 t_type* v_type = ((t_map*)type)->get_val_type();
743 const map<t_const_value*, t_const_value*>& val = value->get_map();
744 map<t_const_value*, t_const_value*>::const_iterator v_iter;
745 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
746 validate_const_rec(name + "<key>", k_type, v_iter->first);
747 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000748 }
Mark Slee30152872006-11-28 01:24:07 +0000749 } else if (type->is_list() || type->is_set()) {
750 t_type* e_type;
751 if (type->is_list()) {
752 e_type = ((t_list*)type)->get_elem_type();
753 } else {
754 e_type = ((t_set*)type)->get_elem_type();
755 }
756 const vector<t_const_value*>& val = value->get_list();
757 vector<t_const_value*>::const_iterator v_iter;
758 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
759 validate_const_rec(name + "<elem>", e_type, *v_iter);
760 }
761 }
762}
763
764/**
765 * Check the type of the parsed const information against its declared type
766 */
767void validate_const_type(t_const* c) {
768 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
769}
770
771/**
Mark Slee7ff32452007-02-01 05:26:18 +0000772 * Check the type of a default value assigned to a field.
773 */
774void validate_field_value(t_field* field, t_const_value* cv) {
775 validate_const_rec(field->get_name(), field->get_type(), cv);
776}
777
778/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000779 * Check that all the elements of a throws block are actually exceptions.
780 */
781bool validate_throws(t_struct* throws) {
782 const vector<t_field*>& members = throws->get_members();
783 vector<t_field*>::const_iterator m_iter;
784 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
785 if (!(*m_iter)->get_type()->is_xception()) {
786 return false;
787 }
788 }
789 return true;
790}
791
792/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000793 * Parses a program
794 */
Mark Slee2c44d202007-05-16 02:18:07 +0000795void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000796 // Get scope file path
797 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000798
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 // Set current dir global, which is used in the include_file function
800 g_curdir = directory_name(path);
801 g_curpath = path;
802
803 // Open the file
804 yyin = fopen(path.c_str(), "r");
805 if (yyin == 0) {
806 failure("Could not open input file: \"%s\"", path.c_str());
807 }
808
809 // Create new scope and scan for includes
810 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000811 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000812 g_program = program;
813 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000814 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000815 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000816 if (yyparse() != 0) {
817 failure("Parser error during include pass.");
818 }
819 } catch (string x) {
820 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000821 }
822 fclose(yyin);
823
824 // Recursively parse all the include programs
825 vector<t_program*>& includes = program->get_includes();
826 vector<t_program*>::iterator iter;
827 for (iter = includes.begin(); iter != includes.end(); ++iter) {
828 parse(*iter, program);
829 }
830
David Reiss204420f2008-01-11 20:59:03 +0000831 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000832 g_parse_mode = PROGRAM;
833 g_program = program;
834 g_scope = program->scope();
835 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
836 g_parent_prefix = program->get_name() + ".";
837 g_curpath = path;
838 yyin = fopen(path.c_str(), "r");
839 if (yyin == 0) {
840 failure("Could not open input file: \"%s\"", path.c_str());
841 }
842 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000843 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000844 try {
845 if (yyparse() != 0) {
846 failure("Parser error during types pass.");
847 }
848 } catch (string x) {
849 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 }
851 fclose(yyin);
852}
853
854/**
855 * Generate code
856 */
David Reissbd0db882008-02-27 01:54:51 +0000857void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000858 // Oooohh, recursive code generation, hot!!
859 if (gen_recurse) {
860 const vector<t_program*>& includes = program->get_includes();
861 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000862 // Propogate output path from parent to child programs
863 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000864
David Reissbd0db882008-02-27 01:54:51 +0000865 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000866 }
867 }
868
869 // Generate code!
870 try {
871 pverbose("Program: %s\n", program->get_path().c_str());
872
David Reiss18bf22d2007-08-28 20:49:17 +0000873 // Compute fingerprints.
874 generate_all_fingerprints(program);
875
Mark Sleef0712dc2006-10-25 19:03:57 +0000876 if (gen_cpp) {
877 pverbose("Generating C++\n");
David Reissd779cbe2007-08-31 01:42:55 +0000878 t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense);
kholst76f2c882008-01-16 02:47:41 +0000879 cpp->set_use_include_prefix(g_cpp_use_include_prefix);
Mark Sleef0712dc2006-10-25 19:03:57 +0000880 cpp->generate_program();
881 delete cpp;
882 }
883
884 if (gen_java) {
885 pverbose("Generating Java\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000886 t_java_generator* java = new t_java_generator(program, false);
887 java->generate_program();
888 delete java;
889 }
890
891 if (gen_javabean) {
892 pverbose("Generating Java Beans\n");
893 t_java_generator* java = new t_java_generator(program, true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000894 java->generate_program();
895 delete java;
896 }
897
898 if (gen_php) {
899 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000900 t_php_generator* php = new t_php_generator(program, false, gen_rest, gen_phps, gen_phpa, gen_phpo);
Mark Sleef0712dc2006-10-25 19:03:57 +0000901 php->generate_program();
902 delete php;
903 }
904
905 if (gen_phpi) {
906 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000907 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000908 phpi->generate_program();
909 delete phpi;
910 }
911
912 if (gen_py) {
913 pverbose("Generating Python\n");
David Reiss6495adb2007-12-18 03:37:30 +0000914 t_py_generator* py = new t_py_generator(program, gen_py_newstyle);
Mark Sleef0712dc2006-10-25 19:03:57 +0000915 py->generate_program();
916 delete py;
917 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000918
Mark Slee6d7d5952007-01-27 01:44:22 +0000919 if (gen_rb) {
920 pverbose("Generating Ruby\n");
921 t_rb_generator* rb = new t_rb_generator(program);
922 rb->generate_program();
923 delete rb;
924 }
925
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000926 if (gen_xsd) {
927 pverbose("Generating XSD\n");
928 t_xsd_generator* xsd = new t_xsd_generator(program);
929 xsd->generate_program();
930 delete xsd;
931 }
932
Mark Slee2c44d202007-05-16 02:18:07 +0000933 if (gen_perl) {
934 pverbose("Generating PERL\n");
935 t_perl_generator* perl = new t_perl_generator(program);
936 perl->generate_program();
937 delete perl;
938 }
939
Christopher Pirob97b89d2007-09-18 00:07:42 +0000940 if (gen_erl) {
941 pverbose("Generating Erlang\n");
942 t_erl_generator* erl = new t_erl_generator(program);
943 erl->generate_program();
944 delete erl;
945 }
946
iproctor9a41a0c2007-07-16 21:59:24 +0000947 if (gen_ocaml) {
948 pverbose("Generating OCaml\n");
949 t_ocaml_generator* ocaml = new t_ocaml_generator(program);
950 ocaml->generate_program();
951 delete ocaml;
952 }
953
iproctorff8eb922007-07-25 19:06:13 +0000954 if (gen_hs) {
955 pverbose("Generating Haskell\n");
956 t_hs_generator* hs = new t_hs_generator(program);
957 hs->generate_program();
958 delete hs;
959 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000960
Mark Slee7e9eea42007-09-10 21:00:23 +0000961 if (gen_cocoa) {
962 pverbose("Generating Cocoa/Objective-C\n");
963 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
964 cocoa->generate_program();
965 delete cocoa;
966 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000967
Mark Sleeefd37f12007-11-20 05:13:09 +0000968 if (gen_st) {
969 pverbose("Generating Smalltalk/Squeak\n");
970 t_st_generator* st = new t_st_generator(program);
971 st->generate_program();
972 delete st;
973 }
974
David Reiss7f42bcf2008-01-11 20:59:12 +0000975 if (gen_csharp) {
976 pverbose("Generating C#\n");
977 t_csharp_generator* csharp = new t_csharp_generator(program);
978 csharp->generate_program();
979 delete csharp;
980 }
kholst76f2c882008-01-16 02:47:41 +0000981
David Reiss1ac05802007-07-30 22:00:27 +0000982 if (dump_docs) {
983 dump_docstrings(program);
984 }
David Reissbd0db882008-02-27 01:54:51 +0000985
986 vector<string>::const_iterator iter;
987 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
988 t_generator* generator = t_generator_registry::get_generator(program, *iter);
989
990 if (generator == NULL) {
991 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
992 } else {
993 pverbose("Generating \"%s\"\n", iter->c_str());
994 generator->generate_program();
995 }
996 }
997
Mark Sleef0712dc2006-10-25 19:03:57 +0000998 } catch (string s) {
999 printf("Error: %s\n", s.c_str());
1000 } catch (const char* exc) {
1001 printf("Error: %s\n", exc);
1002 }
1003
1004}
1005
1006/**
Mark Sleef5377b32006-10-10 01:42:59 +00001007 * Parse it up.. then spit it back out, in pretty much every language. Alright
1008 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001009 */
1010int main(int argc, char** argv) {
1011 int i;
dweatherford65b70752007-10-31 02:18:14 +00001012 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +00001013
Mark Sleeb15a68b2006-06-07 06:46:24 +00001014 // Setup time string
1015 time_t now = time(NULL);
1016 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001017
Mark Sleef0712dc2006-10-25 19:03:57 +00001018 // Check for necessary arguments, you gotta have at least a filename and
1019 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001020 if (argc < 2) {
1021 usage();
1022 }
Mark Slee31985722006-05-24 21:45:31 +00001023
David Reissbd0db882008-02-27 01:54:51 +00001024 vector<string> generator_strings;
1025
David Reiss9cc2c132008-02-27 01:54:47 +00001026 // Set the current path to a dummy value to make warning messages clearer.
1027 g_curpath = "arguments";
1028
Mark Sleef5377b32006-10-10 01:42:59 +00001029 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +00001030 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001031 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001032
Mark Sleefdbee812006-09-27 18:50:48 +00001033 arg = strtok(argv[i], " ");
1034 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001035 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001036 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001037 ++arg;
1038 }
1039
1040 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001041 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001042 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001043 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001044 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001045 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001046 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001047 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001048 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001049 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +00001050 } else if (strcmp(arg, "-gen") == 0) {
1051 arg = argv[++i];
1052 if (arg == NULL) {
1053 fprintf(stderr, "!!! Missing generator specification");
1054 usage();
1055 }
1056 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +00001057 } else if (strcmp(arg, "-dense") == 0) {
1058 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +00001059 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001060 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +00001061 } else if (strcmp(arg, "-javabean") == 0) {
1062 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +00001063 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001064 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001065 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001066 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001067 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001068 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001069 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001070 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001071 gen_phps = true;
1072 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001073 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001074 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001075 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001076 gen_php = true;
1077 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001078 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001079 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001080 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001081 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001082 } else if (strcmp(arg, "-rest") == 0) {
1083 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001084 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001085 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001086 } else if (strcmp(arg, "-pyns") == 0) {
1087 gen_py = true;
1088 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001089 } else if (strcmp(arg, "-rb") == 0) {
1090 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001091 } else if (strcmp(arg, "-xsd") == 0) {
1092 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001093 } else if (strcmp(arg, "-perl") == 0) {
1094 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001095 } else if (strcmp(arg, "-erl") == 0) {
1096 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001097 } else if (strcmp(arg, "-ocaml") == 0) {
1098 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001099 } else if (strcmp(arg, "-hs") == 0) {
1100 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001101 } else if (strcmp(arg, "-cocoa") == 0) {
1102 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001103 } else if (strcmp(arg, "-st") == 0) {
1104 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001105 } else if (strcmp(arg, "-csharp") == 0) {
1106 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001107 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1108 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001109 } else if (strcmp(arg, "-I") == 0) {
1110 // An argument of "-I\ asdf" is invalid and has unknown results
1111 arg = argv[++i];
1112
1113 if (arg == NULL) {
1114 fprintf(stderr, "!!! Missing Include directory");
1115 usage();
1116 }
1117 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001118 } else if (strcmp(arg, "-o") == 0) {
1119 arg = argv[++i];
1120 if (arg == NULL) {
1121 fprintf(stderr, "-o: missing output directory");
1122 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001123 }
dweatherford65b70752007-10-31 02:18:14 +00001124 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001125
1126#ifdef MINGW
1127 //strip out trailing \ on Windows
1128 int last = out_path.length()-1;
1129 if (out_path[last] == '\\')
1130 {
1131 out_path.erase(last);
1132 }
1133#endif
1134
dweatherford65b70752007-10-31 02:18:14 +00001135 struct stat sb;
1136 if (stat(out_path.c_str(), &sb) < 0) {
1137 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1138 return -1;
1139 }
1140 if (! S_ISDIR(sb.st_mode)) {
1141 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1142 return -1;
1143 }
Mark Sleefdbee812006-09-27 18:50:48 +00001144 } else {
1145 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1146 usage();
1147 }
1148
1149 // Tokenize more
1150 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001151 }
1152 }
Mark Slee2c44d202007-05-16 02:18:07 +00001153
Mark Sleef0712dc2006-10-25 19:03:57 +00001154 // You gotta generate something!
David Reissbd0db882008-02-27 01:54:51 +00001155 if (!gen_cpp && !gen_java && !gen_javabean && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_erl && !gen_ocaml && !gen_hs && !gen_cocoa && !gen_st && !gen_csharp && generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001156 fprintf(stderr, "!!! No output language(s) specified\n\n");
1157 usage();
1158 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001159
1160 // Real-pathify it
1161 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +00001162 if (saferealpath(argv[i], rp) == NULL) {
1163 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001164 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001165 string input_file(rp);
1166
Mark Sleef5377b32006-10-10 01:42:59 +00001167 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001168 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001169 if (out_path.size()) {
1170 program->set_out_path(out_path);
1171 }
kholst76f2c882008-01-16 02:47:41 +00001172 if (g_cpp_use_include_prefix) {
1173 // infer this from the filename passed in
1174 string input_filename = argv[i];
1175 string include_prefix;
1176
1177 string::size_type last_slash = string::npos;
1178 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1179 include_prefix = input_filename.substr(0, last_slash);
1180 }
1181
1182 program->set_include_prefix(include_prefix);
1183 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001184
1185 // Initialize global types
1186 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1187 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001188 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1189 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001190 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1191 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001192 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1193 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1194 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1195 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1196 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1197 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001198
Mark Sleef5377b32006-10-10 01:42:59 +00001199 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001200 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001201
David Reiss9cc2c132008-02-27 01:54:47 +00001202 // The current path is not really relevant when we are doing generation.
1203 // Reset the variable to make warning messages clearer.
1204 g_curpath = "generation";
1205 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1206 // That is what shows up during argument parsing.
1207 yylineno = 1;
1208
Mark Sleef0712dc2006-10-25 19:03:57 +00001209 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001210 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001211
Mark Sleef0712dc2006-10-25 19:03:57 +00001212 // Clean up. Who am I kidding... this program probably orphans heap memory
1213 // all over the place, but who cares because it is about to exit and it is
1214 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001215
Mark Sleef0712dc2006-10-25 19:03:57 +00001216 delete program;
1217 delete g_type_void;
1218 delete g_type_string;
1219 delete g_type_bool;
1220 delete g_type_byte;
1221 delete g_type_i16;
1222 delete g_type_i32;
1223 delete g_type_i64;
1224 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001225
1226 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001227 return 0;
1228}