blob: 80da53a64d33c759823e25f3c649bef5542cc814 [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 Slee6e536442006-06-30 18:28:50 +000037#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000038#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000039#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000040#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000041#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000042#include "generate/t_erl_generator.h"
Christopher Pirob97b89d2007-09-18 00:07:42 +000043#include "generate/t_ocaml_generator.h"
iproctorff8eb922007-07-25 19:06:13 +000044#include "generate/t_hs_generator.h"
Mark Slee7e9eea42007-09-10 21:00:23 +000045#include "generate/t_cocoa_generator.h"
Mark Sleeefd37f12007-11-20 05:13:09 +000046#include "generate/t_st_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000047#include "generate/t_csharp_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000048
49using namespace std;
50
Mark Sleef5377b32006-10-10 01:42:59 +000051/**
52 * Global program tree
53 */
Mark Slee31985722006-05-24 21:45:31 +000054t_program* g_program;
55
Mark Sleef5377b32006-10-10 01:42:59 +000056/**
Mark Sleef0712dc2006-10-25 19:03:57 +000057 * Global types
58 */
59
60t_type* g_type_void;
61t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000062t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000063t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000064t_type* g_type_bool;
65t_type* g_type_byte;
66t_type* g_type_i16;
67t_type* g_type_i32;
68t_type* g_type_i64;
69t_type* g_type_double;
70
71/**
72 * Global scope
73 */
74t_scope* g_scope;
75
76/**
77 * Parent scope to also parse types
78 */
79t_scope* g_parent_scope;
80
81/**
82 * Prefix for putting types in parent scope
83 */
84string g_parent_prefix;
85
86/**
87 * Parsing pass
88 */
89PARSE_MODE g_parse_mode;
90
91/**
92 * Current directory of file being parsed
93 */
94string g_curdir;
95
96/**
97 * Current file being parsed
98 */
99string g_curpath;
100
101/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000102 * Search path for inclusions
103 */
Mark Slee2329a832006-11-09 00:23:30 +0000104vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000105
106/**
kholst76f2c882008-01-16 02:47:41 +0000107 * Should C++ include statements use path prefixes for other thrift-generated
108 * header files
109 */
110bool g_cpp_use_include_prefix = false;
111
112/**
Mark Sleef5377b32006-10-10 01:42:59 +0000113 * Global debug state
114 */
Mark Slee31985722006-05-24 21:45:31 +0000115int g_debug = 0;
116
Mark Sleef5377b32006-10-10 01:42:59 +0000117/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000118 * Warning level
119 */
120int g_warn = 1;
121
122/**
123 * Verbose output
124 */
125int g_verbose = 0;
126
127/**
Mark Sleef5377b32006-10-10 01:42:59 +0000128 * Global time string
129 */
Mark Slee31985722006-05-24 21:45:31 +0000130char* g_time_str;
131
Mark Slee31985722006-05-24 21:45:31 +0000132/**
David Reisscbd4bac2007-08-14 17:12:33 +0000133 * The last parsed doctext comment.
134 */
135char* g_doctext;
136
137/**
138 * The location of the last parsed doctext comment.
139 */
140int g_doctext_lineno;
141
142/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000143 * Flags to control code generation
144 */
145bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000146bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000147bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000148bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000149bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000150bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000151bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000152bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000153bool gen_php = false;
154bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000155bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000156bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000157bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000158bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000159bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000160bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000161bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000162bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000163bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000164bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000165bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000166bool gen_recurse = false;
167
168/**
David Reiss204420f2008-01-11 20:59:03 +0000169 * MinGW doesn't have realpath, so use fallback implementation in that case,
170 * otherwise this just calls through to realpath
171 */
172char *saferealpath(const char *path, char *resolved_path) {
173#ifdef MINGW
174 char buf[MAX_PATH];
175 char* basename;
176 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
177 if (len == 0 || len > MAX_PATH - 1){
178 strcpy(resolved_path, path);
179 } else {
180 CharLowerBuff(buf, len);
181 strcpy(resolved_path, buf);
182 }
183 return resolved_path;
184#else
185 return realpath(path, resolved_path);
186#endif
187}
188
189
190/**
Mark Slee31985722006-05-24 21:45:31 +0000191 * Report an error to the user. This is called yyerror for historical
192 * reasons (lex and yacc expect the error reporting routine to be called
193 * this). Call this function to report any errors to the user.
194 * yyerror takes printf style arguments.
195 *
196 * @param fmt C format string followed by additional arguments
197 */
198void yyerror(char* fmt, ...) {
199 va_list args;
200 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000201 "[ERROR:%s:%d] (last token was '%s')\n",
202 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000203 yylineno,
204 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000205
206 va_start(args, fmt);
207 vfprintf(stderr, fmt, args);
208 va_end(args);
209
210 fprintf(stderr, "\n");
211}
212
213/**
214 * Prints a debug message from the parser.
215 *
216 * @param fmt C format string followed by additional arguments
217 */
218void pdebug(char* fmt, ...) {
219 if (g_debug == 0) {
220 return;
221 }
222 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000223 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000224 va_start(args, fmt);
225 vprintf(fmt, args);
226 va_end(args);
227 printf("\n");
228}
229
230/**
231 * Prints a verbose output mode message
232 *
233 * @param fmt C format string followed by additional arguments
234 */
235void pverbose(char* fmt, ...) {
236 if (g_verbose == 0) {
237 return;
238 }
239 va_list args;
240 va_start(args, fmt);
241 vprintf(fmt, args);
242 va_end(args);
243}
244
245/**
246 * Prints a warning message
247 *
248 * @param fmt C format string followed by additional arguments
249 */
250void pwarning(int level, char* fmt, ...) {
251 if (g_warn < level) {
252 return;
253 }
254 va_list args;
255 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000256 va_start(args, fmt);
257 vprintf(fmt, args);
258 va_end(args);
259 printf("\n");
260}
261
262/**
263 * Prints a failure message and exits
264 *
265 * @param fmt C format string followed by additional arguments
266 */
Mark Slee30152872006-11-28 01:24:07 +0000267void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000268 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000269 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000270 va_start(args, fmt);
271 vfprintf(stderr, fmt, args);
272 va_end(args);
273 printf("\n");
274 exit(1);
275}
276
277/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000278 * Converts a string filename into a thrift program name
279 */
280string program_name(string filename) {
281 string::size_type slash = filename.rfind("/");
282 if (slash != string::npos) {
283 filename = filename.substr(slash+1);
284 }
285 string::size_type dot = filename.rfind(".");
286 if (dot != string::npos) {
287 filename = filename.substr(0, dot);
288 }
289 return filename;
290}
291
292/**
293 * Gets the directory path of a filename
294 */
295string directory_name(string filename) {
296 string::size_type slash = filename.rfind("/");
297 // No slash, just use the current directory
298 if (slash == string::npos) {
299 return ".";
300 }
301 return filename.substr(0, slash);
302}
303
304/**
305 * Finds the appropriate file path for the given filename
306 */
307string include_file(string filename) {
308 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000309 if (filename[0] == '/') {
310 // Realpath!
311 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000312 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000313 pwarning(0, "Cannot open include file %s\n", filename.c_str());
314 return std::string();
315 }
Mark Slee2c44d202007-05-16 02:18:07 +0000316
317 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000318 struct stat finfo;
319 if (stat(rp, &finfo) == 0) {
320 return rp;
321 }
322 } else { // relative path, start searching
323 // new search path with current dir global
324 vector<string> sp = g_incl_searchpath;
325 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000326
Martin Kraemer32c66e12006-11-09 00:06:36 +0000327 // iterate through paths
328 vector<string>::iterator it;
329 for (it = sp.begin(); it != sp.end(); it++) {
330 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000331
Martin Kraemer32c66e12006-11-09 00:06:36 +0000332 // Realpath!
333 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000334 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000335 continue;
336 }
Mark Slee2c44d202007-05-16 02:18:07 +0000337
Martin Kraemer32c66e12006-11-09 00:06:36 +0000338 // Stat this files
339 struct stat finfo;
340 if (stat(rp, &finfo) == 0) {
341 return rp;
342 }
343 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000344 }
Mark Slee2c44d202007-05-16 02:18:07 +0000345
Mark Sleef0712dc2006-10-25 19:03:57 +0000346 // Uh oh
347 pwarning(0, "Could not find include file %s\n", filename.c_str());
348 return std::string();
349}
350
351/**
David Reisscbd4bac2007-08-14 17:12:33 +0000352 * Clears any previously stored doctext string.
353 * Also prints a warning if we are discarding information.
354 */
355void clear_doctext() {
356 if (g_doctext != NULL) {
357 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
358 }
359 free(g_doctext);
360 g_doctext = NULL;
361}
362
363/**
David Reiss1ac05802007-07-30 22:00:27 +0000364 * Cleans up text commonly found in doxygen-like comments
365 *
366 * Warning: if you mix tabs and spaces in a non-uniform way,
367 * you will get what you deserve.
368 */
369char* clean_up_doctext(char* doctext) {
370 // Convert to C++ string, and remove Windows's carriage returns.
371 string docstring = doctext;
372 docstring.erase(
373 remove(docstring.begin(), docstring.end(), '\r'),
374 docstring.end());
375
376 // Separate into lines.
377 vector<string> lines;
378 string::size_type pos = string::npos;
379 string::size_type last;
380 while (true) {
381 last = (pos == string::npos) ? 0 : pos+1;
382 pos = docstring.find('\n', last);
383 if (pos == string::npos) {
384 // First bit of cleaning. If the last line is only whitespace, drop it.
385 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
386 if (nonwhite != string::npos) {
387 lines.push_back(docstring.substr(last));
388 }
389 break;
390 }
391 lines.push_back(docstring.substr(last, pos-last));
392 }
393
394 // A very profound docstring.
395 if (lines.empty()) {
396 return NULL;
397 }
398
399 // Clear leading whitespace from the first line.
400 pos = lines.front().find_first_not_of(" \t");
401 lines.front().erase(0, pos);
402
403 // If every nonblank line after the first has the same number of spaces/tabs,
404 // then a star, remove them.
405 bool have_prefix = true;
406 bool found_prefix = false;
407 string::size_type prefix_len = 0;
408 vector<string>::iterator l_iter;
409 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
410 if (l_iter->empty()) {
411 continue;
412 }
413
414 pos = l_iter->find_first_not_of(" \t");
415 if (!found_prefix) {
416 if (pos != string::npos) {
417 if (l_iter->at(pos) == '*') {
418 found_prefix = true;
419 prefix_len = pos;
420 } else {
421 have_prefix = false;
422 break;
423 }
424 } else {
425 // Whitespace-only line. Truncate it.
426 l_iter->clear();
427 }
428 } else if (l_iter->size() > pos
429 && l_iter->at(pos) == '*'
430 && pos == prefix_len) {
431 // Business as usual.
432 } else if (pos == string::npos) {
433 // Whitespace-only line. Let's truncate it for them.
434 l_iter->clear();
435 } else {
436 // The pattern has been broken.
437 have_prefix = false;
438 break;
439 }
440 }
441
442 // If our prefix survived, delete it from every line.
443 if (have_prefix) {
444 // Get the star too.
445 prefix_len++;
446 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
447 l_iter->erase(0, prefix_len);
448 }
449 }
450
451 // Now delete the minimum amount of leading whitespace from each line.
452 prefix_len = string::npos;
453 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
454 if (l_iter->empty()) {
455 continue;
456 }
457 pos = l_iter->find_first_not_of(" \t");
458 if (pos != string::npos
459 && (prefix_len == string::npos || pos < prefix_len)) {
460 prefix_len = pos;
461 }
462 }
463
464 // If our prefix survived, delete it from every line.
465 if (prefix_len != string::npos) {
466 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
467 l_iter->erase(0, prefix_len);
468 }
469 }
470
471 // Remove trailing whitespace from every line.
472 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
473 pos = l_iter->find_last_not_of(" \t");
474 if (pos != string::npos && pos != l_iter->length()-1) {
475 l_iter->erase(pos+1);
476 }
477 }
478
479 // If the first line is empty, remove it.
480 // Don't do this earlier because a lot of steps skip the first line.
481 if (lines.front().empty()) {
482 lines.erase(lines.begin());
483 }
484
485 // Now rejoin the lines and copy them back into doctext.
486 docstring.clear();
487 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
488 docstring += *l_iter;
489 docstring += '\n';
490 }
491
492 assert(docstring.length() <= strlen(doctext));
493 strcpy(doctext, docstring.c_str());
494 return doctext;
495}
496
497/** Set to true to debug docstring parsing */
498static bool dump_docs = false;
499
500/**
501 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000502 * Only works for top-level definitions and the whole program doc
503 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000504 */
505void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000506 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000507 if (!progdoc.empty()) {
508 printf("Whole program doc:\n%s\n", progdoc.c_str());
509 }
David Reiss1ac05802007-07-30 22:00:27 +0000510 const vector<t_typedef*>& typedefs = program->get_typedefs();
511 vector<t_typedef*>::const_iterator t_iter;
512 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
513 t_typedef* td = *t_iter;
514 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000515 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
516 }
517 }
518 const vector<t_enum*>& enums = program->get_enums();
519 vector<t_enum*>::const_iterator e_iter;
520 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
521 t_enum* en = *e_iter;
522 if (en->has_doc()) {
523 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
524 }
525 }
526 const vector<t_const*>& consts = program->get_consts();
527 vector<t_const*>::const_iterator c_iter;
528 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
529 t_const* co = *c_iter;
530 if (co->has_doc()) {
531 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
532 }
533 }
534 const vector<t_struct*>& structs = program->get_structs();
535 vector<t_struct*>::const_iterator s_iter;
536 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
537 t_struct* st = *s_iter;
538 if (st->has_doc()) {
539 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
540 }
541 }
542 const vector<t_struct*>& xceptions = program->get_xceptions();
543 vector<t_struct*>::const_iterator x_iter;
544 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
545 t_struct* xn = *x_iter;
546 if (xn->has_doc()) {
547 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
548 }
549 }
550 const vector<t_service*>& services = program->get_services();
551 vector<t_service*>::const_iterator v_iter;
552 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
553 t_service* sv = *v_iter;
554 if (sv->has_doc()) {
555 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000556 }
557 }
558}
559
560/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000561 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000562 */
563void generate_all_fingerprints(t_program* program) {
564 const vector<t_struct*>& structs = program->get_structs();
565 vector<t_struct*>::const_iterator s_iter;
566 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
567 t_struct* st = *s_iter;
568 st->generate_fingerprint();
569 }
570
David Reissd779cbe2007-08-31 01:42:55 +0000571 const vector<t_struct*>& xceptions = program->get_xceptions();
572 vector<t_struct*>::const_iterator x_iter;
573 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
574 t_struct* st = *x_iter;
575 st->generate_fingerprint();
576 }
577
David Reiss3c5d2fd2008-02-08 21:58:06 +0000578 const vector<t_enum*>& enums = program->get_enums();
579 vector<t_enum*>::const_iterator e_iter;
580 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
581 t_enum* e = *e_iter;
582 e->generate_fingerprint();
583 }
584
David Reiss47557bc2007-09-04 21:31:04 +0000585 g_type_void->generate_fingerprint();
586
David Reiss18bf22d2007-08-28 20:49:17 +0000587 // If you want to generate fingerprints for implicit structures, start here.
588 /*
589 const vector<t_service*>& services = program->get_services();
590 vector<t_service*>::const_iterator v_iter;
591 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
592 t_service* sv = *v_iter;
593 }
594 */
595}
596
597/**
Mark Slee31985722006-05-24 21:45:31 +0000598 * Diplays the usage message and then exits with an error code.
599 */
600void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000601 fprintf(stderr, "Usage: thrift [options] file\n");
602 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000603 fprintf(stderr, " -php Generate PHP output files\n");
604 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000605 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
606 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000607 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000608 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000609 fprintf(stderr, " -py Generate Python output files\n");
David Reiss6495adb2007-12-18 03:37:30 +0000610 fprintf(stderr, " -pyns Generate Python new-style classes (with -py)\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000611 fprintf(stderr, " -rb Generate Ruby output files\n");
612 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000613 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000614 fprintf(stderr, " -erl Generate Erlang output files\n");
Christopher Pirob97b89d2007-09-18 00:07:42 +0000615 fprintf(stderr, " -ocaml Generate OCaml output files\n");
iproctorff8eb922007-07-25 19:06:13 +0000616 fprintf(stderr, " -hs Generate Haskell output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000617 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
David Reiss7f42bcf2008-01-11 20:59:12 +0000618 fprintf(stderr, " -csharp Generate C# output files\n");
Mark Sleeefd37f12007-11-20 05:13:09 +0000619 fprintf(stderr, " -st Generate Squeak/Smalltalk output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000620 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
621 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000622 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000623 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000624 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000625 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
626 fprintf(stderr, " -strict Strict compiler warnings on\n");
627 fprintf(stderr, " -v[erbose] Verbose mode\n");
628 fprintf(stderr, " -r[ecurse] Also generate included files\n");
629 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000630 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
631 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
632 fprintf(stderr, " Keys and values are options passed to the generator.\n");
633 fprintf(stderr, " Many options will not require values.\n");
634 fprintf(stderr, "\n");
635 fprintf(stderr, "Available generators (and options):\n");
636
637 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
638 t_generator_registry::gen_map_t::iterator iter;
639 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
640 fprintf(stderr, " %s (%s):\n",
641 iter->second->get_short_name().c_str(),
642 iter->second->get_long_name().c_str());
643 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
644 }
Mark Slee31985722006-05-24 21:45:31 +0000645 exit(1);
646}
647
648/**
Mark Slee30152872006-11-28 01:24:07 +0000649 * You know, when I started working on Thrift I really thought it wasn't going
650 * to become a programming language because it was just a generator and it
651 * wouldn't need runtime type information and all that jazz. But then we
652 * decided to add constants, and all of a sudden that means runtime type
653 * validation and inference, except the "runtime" is the code generator
654 * runtime. Shit. I've been had.
655 */
656void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
657 if (type->is_void()) {
658 throw "type error: cannot declare a void const: " + name;
659 }
660
661 if (type->is_base_type()) {
662 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
663 switch (tbase) {
664 case t_base_type::TYPE_STRING:
665 if (value->get_type() != t_const_value::CV_STRING) {
666 throw "type error: const \"" + name + "\" was declared as string";
667 }
668 break;
669 case t_base_type::TYPE_BOOL:
670 if (value->get_type() != t_const_value::CV_INTEGER) {
671 throw "type error: const \"" + name + "\" was declared as bool";
672 }
673 break;
674 case t_base_type::TYPE_BYTE:
675 if (value->get_type() != t_const_value::CV_INTEGER) {
676 throw "type error: const \"" + name + "\" was declared as byte";
677 }
678 break;
679 case t_base_type::TYPE_I16:
680 if (value->get_type() != t_const_value::CV_INTEGER) {
681 throw "type error: const \"" + name + "\" was declared as i16";
682 }
683 break;
684 case t_base_type::TYPE_I32:
685 if (value->get_type() != t_const_value::CV_INTEGER) {
686 throw "type error: const \"" + name + "\" was declared as i32";
687 }
688 break;
689 case t_base_type::TYPE_I64:
690 if (value->get_type() != t_const_value::CV_INTEGER) {
691 throw "type error: const \"" + name + "\" was declared as i64";
692 }
693 break;
694 case t_base_type::TYPE_DOUBLE:
695 if (value->get_type() != t_const_value::CV_INTEGER &&
696 value->get_type() != t_const_value::CV_DOUBLE) {
697 throw "type error: const \"" + name + "\" was declared as double";
698 }
699 break;
700 default:
David Reissdd7796f2007-08-28 21:09:06 +0000701 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000702 }
703 } else if (type->is_enum()) {
704 if (value->get_type() != t_const_value::CV_INTEGER) {
705 throw "type error: const \"" + name + "\" was declared as enum";
706 }
707 } else if (type->is_struct() || type->is_xception()) {
708 if (value->get_type() != t_const_value::CV_MAP) {
709 throw "type error: const \"" + name + "\" was declared as struct/xception";
710 }
711 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
712 vector<t_field*>::const_iterator f_iter;
713
714 const map<t_const_value*, t_const_value*>& val = value->get_map();
715 map<t_const_value*, t_const_value*>::const_iterator v_iter;
716 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
717 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
718 throw "type error: " + name + " struct key must be string";
719 }
720 t_type* field_type = NULL;
721 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
722 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
723 field_type = (*f_iter)->get_type();
724 }
725 }
726 if (field_type == NULL) {
727 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
728 }
729
730 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
731 }
732 } else if (type->is_map()) {
733 t_type* k_type = ((t_map*)type)->get_key_type();
734 t_type* v_type = ((t_map*)type)->get_val_type();
735 const map<t_const_value*, t_const_value*>& val = value->get_map();
736 map<t_const_value*, t_const_value*>::const_iterator v_iter;
737 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
738 validate_const_rec(name + "<key>", k_type, v_iter->first);
739 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000740 }
Mark Slee30152872006-11-28 01:24:07 +0000741 } else if (type->is_list() || type->is_set()) {
742 t_type* e_type;
743 if (type->is_list()) {
744 e_type = ((t_list*)type)->get_elem_type();
745 } else {
746 e_type = ((t_set*)type)->get_elem_type();
747 }
748 const vector<t_const_value*>& val = value->get_list();
749 vector<t_const_value*>::const_iterator v_iter;
750 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
751 validate_const_rec(name + "<elem>", e_type, *v_iter);
752 }
753 }
754}
755
756/**
757 * Check the type of the parsed const information against its declared type
758 */
759void validate_const_type(t_const* c) {
760 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
761}
762
763/**
Mark Slee7ff32452007-02-01 05:26:18 +0000764 * Check the type of a default value assigned to a field.
765 */
766void validate_field_value(t_field* field, t_const_value* cv) {
767 validate_const_rec(field->get_name(), field->get_type(), cv);
768}
769
770/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000771 * Check that all the elements of a throws block are actually exceptions.
772 */
773bool validate_throws(t_struct* throws) {
774 const vector<t_field*>& members = throws->get_members();
775 vector<t_field*>::const_iterator m_iter;
776 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
777 if (!(*m_iter)->get_type()->is_xception()) {
778 return false;
779 }
780 }
781 return true;
782}
783
784/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000785 * Parses a program
786 */
Mark Slee2c44d202007-05-16 02:18:07 +0000787void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000788 // Get scope file path
789 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000790
Mark Sleef0712dc2006-10-25 19:03:57 +0000791 // Set current dir global, which is used in the include_file function
792 g_curdir = directory_name(path);
793 g_curpath = path;
794
795 // Open the file
796 yyin = fopen(path.c_str(), "r");
797 if (yyin == 0) {
798 failure("Could not open input file: \"%s\"", path.c_str());
799 }
800
801 // Create new scope and scan for includes
802 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000803 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000804 g_program = program;
805 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000806 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000807 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000808 if (yyparse() != 0) {
809 failure("Parser error during include pass.");
810 }
811 } catch (string x) {
812 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000813 }
814 fclose(yyin);
815
816 // Recursively parse all the include programs
817 vector<t_program*>& includes = program->get_includes();
818 vector<t_program*>::iterator iter;
819 for (iter = includes.begin(); iter != includes.end(); ++iter) {
820 parse(*iter, program);
821 }
822
David Reiss204420f2008-01-11 20:59:03 +0000823 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000824 g_parse_mode = PROGRAM;
825 g_program = program;
826 g_scope = program->scope();
827 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
828 g_parent_prefix = program->get_name() + ".";
829 g_curpath = path;
830 yyin = fopen(path.c_str(), "r");
831 if (yyin == 0) {
832 failure("Could not open input file: \"%s\"", path.c_str());
833 }
834 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000835 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000836 try {
837 if (yyparse() != 0) {
838 failure("Parser error during types pass.");
839 }
840 } catch (string x) {
841 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000842 }
843 fclose(yyin);
844}
845
846/**
847 * Generate code
848 */
David Reissbd0db882008-02-27 01:54:51 +0000849void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 // Oooohh, recursive code generation, hot!!
851 if (gen_recurse) {
852 const vector<t_program*>& includes = program->get_includes();
853 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000854 // Propogate output path from parent to child programs
855 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000856
David Reissbd0db882008-02-27 01:54:51 +0000857 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000858 }
859 }
860
861 // Generate code!
862 try {
863 pverbose("Program: %s\n", program->get_path().c_str());
864
David Reiss18bf22d2007-08-28 20:49:17 +0000865 // Compute fingerprints.
866 generate_all_fingerprints(program);
867
Mark Sleef0712dc2006-10-25 19:03:57 +0000868 if (gen_php) {
869 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000870 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 +0000871 php->generate_program();
872 delete php;
873 }
874
875 if (gen_phpi) {
876 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000877 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000878 phpi->generate_program();
879 delete phpi;
880 }
881
882 if (gen_py) {
883 pverbose("Generating Python\n");
David Reiss6495adb2007-12-18 03:37:30 +0000884 t_py_generator* py = new t_py_generator(program, gen_py_newstyle);
Mark Sleef0712dc2006-10-25 19:03:57 +0000885 py->generate_program();
886 delete py;
887 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000888
Mark Slee6d7d5952007-01-27 01:44:22 +0000889 if (gen_rb) {
890 pverbose("Generating Ruby\n");
891 t_rb_generator* rb = new t_rb_generator(program);
892 rb->generate_program();
893 delete rb;
894 }
895
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000896 if (gen_xsd) {
897 pverbose("Generating XSD\n");
898 t_xsd_generator* xsd = new t_xsd_generator(program);
899 xsd->generate_program();
900 delete xsd;
901 }
902
Mark Slee2c44d202007-05-16 02:18:07 +0000903 if (gen_perl) {
904 pverbose("Generating PERL\n");
905 t_perl_generator* perl = new t_perl_generator(program);
906 perl->generate_program();
907 delete perl;
908 }
909
Christopher Pirob97b89d2007-09-18 00:07:42 +0000910 if (gen_erl) {
911 pverbose("Generating Erlang\n");
912 t_erl_generator* erl = new t_erl_generator(program);
913 erl->generate_program();
914 delete erl;
915 }
916
iproctor9a41a0c2007-07-16 21:59:24 +0000917 if (gen_ocaml) {
918 pverbose("Generating OCaml\n");
919 t_ocaml_generator* ocaml = new t_ocaml_generator(program);
920 ocaml->generate_program();
921 delete ocaml;
922 }
923
iproctorff8eb922007-07-25 19:06:13 +0000924 if (gen_hs) {
925 pverbose("Generating Haskell\n");
926 t_hs_generator* hs = new t_hs_generator(program);
927 hs->generate_program();
928 delete hs;
929 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000930
Mark Slee7e9eea42007-09-10 21:00:23 +0000931 if (gen_cocoa) {
932 pverbose("Generating Cocoa/Objective-C\n");
933 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
934 cocoa->generate_program();
935 delete cocoa;
936 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000937
Mark Sleeefd37f12007-11-20 05:13:09 +0000938 if (gen_st) {
939 pverbose("Generating Smalltalk/Squeak\n");
940 t_st_generator* st = new t_st_generator(program);
941 st->generate_program();
942 delete st;
943 }
944
David Reiss7f42bcf2008-01-11 20:59:12 +0000945 if (gen_csharp) {
946 pverbose("Generating C#\n");
947 t_csharp_generator* csharp = new t_csharp_generator(program);
948 csharp->generate_program();
949 delete csharp;
950 }
kholst76f2c882008-01-16 02:47:41 +0000951
David Reiss1ac05802007-07-30 22:00:27 +0000952 if (dump_docs) {
953 dump_docstrings(program);
954 }
David Reissbd0db882008-02-27 01:54:51 +0000955
956 vector<string>::const_iterator iter;
957 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
958 t_generator* generator = t_generator_registry::get_generator(program, *iter);
959
960 if (generator == NULL) {
961 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
962 } else {
963 pverbose("Generating \"%s\"\n", iter->c_str());
964 generator->generate_program();
965 }
966 }
967
Mark Sleef0712dc2006-10-25 19:03:57 +0000968 } catch (string s) {
969 printf("Error: %s\n", s.c_str());
970 } catch (const char* exc) {
971 printf("Error: %s\n", exc);
972 }
973
974}
975
976/**
Mark Sleef5377b32006-10-10 01:42:59 +0000977 * Parse it up.. then spit it back out, in pretty much every language. Alright
978 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000979 */
980int main(int argc, char** argv) {
981 int i;
dweatherford65b70752007-10-31 02:18:14 +0000982 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000983
Mark Sleeb15a68b2006-06-07 06:46:24 +0000984 // Setup time string
985 time_t now = time(NULL);
986 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000987
Mark Sleef0712dc2006-10-25 19:03:57 +0000988 // Check for necessary arguments, you gotta have at least a filename and
989 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000990 if (argc < 2) {
991 usage();
992 }
Mark Slee31985722006-05-24 21:45:31 +0000993
David Reissbd0db882008-02-27 01:54:51 +0000994 vector<string> generator_strings;
995
David Reiss9cc2c132008-02-27 01:54:47 +0000996 // Set the current path to a dummy value to make warning messages clearer.
997 g_curpath = "arguments";
998
Mark Sleef5377b32006-10-10 01:42:59 +0000999 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +00001000 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001001 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001002
Mark Sleefdbee812006-09-27 18:50:48 +00001003 arg = strtok(argv[i], " ");
1004 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001005 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001006 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001007 ++arg;
1008 }
1009
1010 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001011 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001012 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001013 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001014 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001015 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001016 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001017 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001018 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001019 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +00001020 } else if (strcmp(arg, "-gen") == 0) {
1021 arg = argv[++i];
1022 if (arg == NULL) {
1023 fprintf(stderr, "!!! Missing generator specification");
1024 usage();
1025 }
1026 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +00001027 } else if (strcmp(arg, "-dense") == 0) {
1028 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +00001029 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001030 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +00001031 } else if (strcmp(arg, "-javabean") == 0) {
1032 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +00001033 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001034 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001035 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001036 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001037 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001038 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001039 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001040 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001041 gen_phps = true;
1042 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001043 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001044 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001045 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001046 gen_php = true;
1047 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001048 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001049 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001050 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001051 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001052 } else if (strcmp(arg, "-rest") == 0) {
1053 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001054 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001055 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001056 } else if (strcmp(arg, "-pyns") == 0) {
1057 gen_py = true;
1058 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001059 } else if (strcmp(arg, "-rb") == 0) {
1060 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001061 } else if (strcmp(arg, "-xsd") == 0) {
1062 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001063 } else if (strcmp(arg, "-perl") == 0) {
1064 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001065 } else if (strcmp(arg, "-erl") == 0) {
1066 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001067 } else if (strcmp(arg, "-ocaml") == 0) {
1068 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001069 } else if (strcmp(arg, "-hs") == 0) {
1070 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001071 } else if (strcmp(arg, "-cocoa") == 0) {
1072 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001073 } else if (strcmp(arg, "-st") == 0) {
1074 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001075 } else if (strcmp(arg, "-csharp") == 0) {
1076 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001077 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1078 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001079 } else if (strcmp(arg, "-I") == 0) {
1080 // An argument of "-I\ asdf" is invalid and has unknown results
1081 arg = argv[++i];
1082
1083 if (arg == NULL) {
1084 fprintf(stderr, "!!! Missing Include directory");
1085 usage();
1086 }
1087 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001088 } else if (strcmp(arg, "-o") == 0) {
1089 arg = argv[++i];
1090 if (arg == NULL) {
1091 fprintf(stderr, "-o: missing output directory");
1092 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001093 }
dweatherford65b70752007-10-31 02:18:14 +00001094 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001095
1096#ifdef MINGW
1097 //strip out trailing \ on Windows
1098 int last = out_path.length()-1;
1099 if (out_path[last] == '\\')
1100 {
1101 out_path.erase(last);
1102 }
1103#endif
1104
dweatherford65b70752007-10-31 02:18:14 +00001105 struct stat sb;
1106 if (stat(out_path.c_str(), &sb) < 0) {
1107 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1108 return -1;
1109 }
1110 if (! S_ISDIR(sb.st_mode)) {
1111 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1112 return -1;
1113 }
Mark Sleefdbee812006-09-27 18:50:48 +00001114 } else {
1115 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1116 usage();
1117 }
1118
1119 // Tokenize more
1120 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001121 }
1122 }
Mark Slee2c44d202007-05-16 02:18:07 +00001123
David Reiss400cd4b2008-02-27 01:54:55 +00001124 // TODO(dreiss): Delete these when everyone is using the new hotness.
1125 if (gen_cpp) {
1126 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1127 string gen_string = "cpp:";
1128 if (gen_dense) {
1129 gen_string.append("dense,");
1130 }
1131 if (g_cpp_use_include_prefix) {
1132 gen_string.append("include_prefix,");
1133 }
1134 generator_strings.push_back(gen_string);
1135 }
David Reisse1404d22008-02-27 01:55:05 +00001136 if (gen_java) {
1137 pwarning(1, "-java is deprecated. Use --gen java");
1138 generator_strings.push_back("java");
1139 }
1140 if (gen_javabean) {
1141 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1142 generator_strings.push_back("java:beans");
1143 }
1144
Mark Sleef0712dc2006-10-25 19:03:57 +00001145 // You gotta generate something!
David Reisse1404d22008-02-27 01:55:05 +00001146 if (!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 +00001147 fprintf(stderr, "!!! No output language(s) specified\n\n");
1148 usage();
1149 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001150
1151 // Real-pathify it
1152 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +00001153 if (saferealpath(argv[i], rp) == NULL) {
1154 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001155 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001156 string input_file(rp);
1157
Mark Sleef5377b32006-10-10 01:42:59 +00001158 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001159 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001160 if (out_path.size()) {
1161 program->set_out_path(out_path);
1162 }
kholst76f2c882008-01-16 02:47:41 +00001163
David Reiss4b6a3c72008-02-27 22:28:12 +00001164 // Compute the cpp include prefix.
1165 // infer this from the filename passed in
1166 string input_filename = argv[i];
1167 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001168
David Reiss4b6a3c72008-02-27 22:28:12 +00001169 string::size_type last_slash = string::npos;
1170 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1171 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001172 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001173
David Reiss4b6a3c72008-02-27 22:28:12 +00001174 program->set_include_prefix(include_prefix);
1175
Mark Sleef0712dc2006-10-25 19:03:57 +00001176 // Initialize global types
1177 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1178 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001179 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1180 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001181 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1182 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001183 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1184 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1185 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1186 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1187 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1188 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001189
Mark Sleef5377b32006-10-10 01:42:59 +00001190 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001191 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001192
David Reiss9cc2c132008-02-27 01:54:47 +00001193 // The current path is not really relevant when we are doing generation.
1194 // Reset the variable to make warning messages clearer.
1195 g_curpath = "generation";
1196 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1197 // That is what shows up during argument parsing.
1198 yylineno = 1;
1199
Mark Sleef0712dc2006-10-25 19:03:57 +00001200 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001201 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001202
Mark Sleef0712dc2006-10-25 19:03:57 +00001203 // Clean up. Who am I kidding... this program probably orphans heap memory
1204 // all over the place, but who cares because it is about to exit and it is
1205 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001206
Mark Sleef0712dc2006-10-25 19:03:57 +00001207 delete program;
1208 delete g_type_void;
1209 delete g_type_string;
1210 delete g_type_bool;
1211 delete g_type_byte;
1212 delete g_type_i16;
1213 delete g_type_i32;
1214 delete g_type_i64;
1215 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001216
1217 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001218 return 0;
1219}