blob: dfd5f9ede44f7f251d330efd9266f93f3cc8114b [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
David Reissf10984b2008-03-27 21:39:52 +000018#include <cassert>
Mark Slee31985722006-05-24 21:45:31 +000019#include <stdlib.h>
20#include <stdio.h>
21#include <stdarg.h>
22#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000023#include <sys/types.h>
24#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000025#include <errno.h>
Mark Slee31985722006-05-24 21:45:31 +000026
David Reiss204420f2008-01-11 20:59:03 +000027#ifdef MINGW
28# include <windows.h> /* for GetFullPathName */
29# include <limits.h>
30#endif
31
Mark Sleef0712dc2006-10-25 19:03:57 +000032// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000033#include "globals.h"
34
35#include "main.h"
36#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000037#include "parse/t_scope.h"
Mark Slee6e536442006-06-30 18:28:50 +000038#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000039#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000040#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000041#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000042#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000043#include "generate/t_erl_generator.h"
Mark Slee7e9eea42007-09-10 21:00:23 +000044#include "generate/t_cocoa_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000045#include "generate/t_csharp_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000046
47using namespace std;
48
Mark Sleef5377b32006-10-10 01:42:59 +000049/**
50 * Global program tree
51 */
Mark Slee31985722006-05-24 21:45:31 +000052t_program* g_program;
53
Mark Sleef5377b32006-10-10 01:42:59 +000054/**
Mark Sleef0712dc2006-10-25 19:03:57 +000055 * Global types
56 */
57
58t_type* g_type_void;
59t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000060t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000061t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000062t_type* g_type_bool;
63t_type* g_type_byte;
64t_type* g_type_i16;
65t_type* g_type_i32;
66t_type* g_type_i64;
67t_type* g_type_double;
68
69/**
70 * Global scope
71 */
72t_scope* g_scope;
73
74/**
75 * Parent scope to also parse types
76 */
77t_scope* g_parent_scope;
78
79/**
80 * Prefix for putting types in parent scope
81 */
82string g_parent_prefix;
83
84/**
85 * Parsing pass
86 */
87PARSE_MODE g_parse_mode;
88
89/**
90 * Current directory of file being parsed
91 */
92string g_curdir;
93
94/**
95 * Current file being parsed
96 */
97string g_curpath;
98
99/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000100 * Search path for inclusions
101 */
Mark Slee2329a832006-11-09 00:23:30 +0000102vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000103
104/**
kholst76f2c882008-01-16 02:47:41 +0000105 * Should C++ include statements use path prefixes for other thrift-generated
106 * header files
107 */
108bool g_cpp_use_include_prefix = false;
109
110/**
Mark Sleef5377b32006-10-10 01:42:59 +0000111 * Global debug state
112 */
Mark Slee31985722006-05-24 21:45:31 +0000113int g_debug = 0;
114
Mark Sleef5377b32006-10-10 01:42:59 +0000115/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000116 * Warning level
117 */
118int g_warn = 1;
119
120/**
121 * Verbose output
122 */
123int g_verbose = 0;
124
125/**
Mark Sleef5377b32006-10-10 01:42:59 +0000126 * Global time string
127 */
Mark Slee31985722006-05-24 21:45:31 +0000128char* g_time_str;
129
Mark Slee31985722006-05-24 21:45:31 +0000130/**
David Reisscbd4bac2007-08-14 17:12:33 +0000131 * The last parsed doctext comment.
132 */
133char* g_doctext;
134
135/**
136 * The location of the last parsed doctext comment.
137 */
138int g_doctext_lineno;
139
140/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000141 * Flags to control code generation
142 */
143bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000144bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000145bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000146bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000147bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000148bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000149bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000150bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000151bool gen_php = false;
152bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000153bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000154bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000155bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000156bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000157bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000158bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000159bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000160bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000161bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000162bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000163bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000164bool gen_recurse = false;
165
166/**
David Reiss204420f2008-01-11 20:59:03 +0000167 * MinGW doesn't have realpath, so use fallback implementation in that case,
168 * otherwise this just calls through to realpath
169 */
170char *saferealpath(const char *path, char *resolved_path) {
171#ifdef MINGW
172 char buf[MAX_PATH];
173 char* basename;
174 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
175 if (len == 0 || len > MAX_PATH - 1){
176 strcpy(resolved_path, path);
177 } else {
178 CharLowerBuff(buf, len);
179 strcpy(resolved_path, buf);
180 }
181 return resolved_path;
182#else
183 return realpath(path, resolved_path);
184#endif
185}
186
187
188/**
Mark Slee31985722006-05-24 21:45:31 +0000189 * Report an error to the user. This is called yyerror for historical
190 * reasons (lex and yacc expect the error reporting routine to be called
191 * this). Call this function to report any errors to the user.
192 * yyerror takes printf style arguments.
193 *
194 * @param fmt C format string followed by additional arguments
195 */
196void yyerror(char* fmt, ...) {
197 va_list args;
198 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000199 "[ERROR:%s:%d] (last token was '%s')\n",
200 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000201 yylineno,
202 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000203
204 va_start(args, fmt);
205 vfprintf(stderr, fmt, args);
206 va_end(args);
207
208 fprintf(stderr, "\n");
209}
210
211/**
212 * Prints a debug message from the parser.
213 *
214 * @param fmt C format string followed by additional arguments
215 */
216void pdebug(char* fmt, ...) {
217 if (g_debug == 0) {
218 return;
219 }
220 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000221 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000222 va_start(args, fmt);
223 vprintf(fmt, args);
224 va_end(args);
225 printf("\n");
226}
227
228/**
229 * Prints a verbose output mode message
230 *
231 * @param fmt C format string followed by additional arguments
232 */
233void pverbose(char* fmt, ...) {
234 if (g_verbose == 0) {
235 return;
236 }
237 va_list args;
238 va_start(args, fmt);
239 vprintf(fmt, args);
240 va_end(args);
241}
242
243/**
244 * Prints a warning message
245 *
246 * @param fmt C format string followed by additional arguments
247 */
248void pwarning(int level, char* fmt, ...) {
249 if (g_warn < level) {
250 return;
251 }
252 va_list args;
253 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000254 va_start(args, fmt);
255 vprintf(fmt, args);
256 va_end(args);
257 printf("\n");
258}
259
260/**
261 * Prints a failure message and exits
262 *
263 * @param fmt C format string followed by additional arguments
264 */
Mark Slee30152872006-11-28 01:24:07 +0000265void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000266 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000267 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000268 va_start(args, fmt);
269 vfprintf(stderr, fmt, args);
270 va_end(args);
271 printf("\n");
272 exit(1);
273}
274
275/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000276 * Converts a string filename into a thrift program name
277 */
278string program_name(string filename) {
279 string::size_type slash = filename.rfind("/");
280 if (slash != string::npos) {
281 filename = filename.substr(slash+1);
282 }
283 string::size_type dot = filename.rfind(".");
284 if (dot != string::npos) {
285 filename = filename.substr(0, dot);
286 }
287 return filename;
288}
289
290/**
291 * Gets the directory path of a filename
292 */
293string directory_name(string filename) {
294 string::size_type slash = filename.rfind("/");
295 // No slash, just use the current directory
296 if (slash == string::npos) {
297 return ".";
298 }
299 return filename.substr(0, slash);
300}
301
302/**
303 * Finds the appropriate file path for the given filename
304 */
305string include_file(string filename) {
306 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000307 if (filename[0] == '/') {
308 // Realpath!
309 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000310 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000311 pwarning(0, "Cannot open include file %s\n", filename.c_str());
312 return std::string();
313 }
Mark Slee2c44d202007-05-16 02:18:07 +0000314
315 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000316 struct stat finfo;
317 if (stat(rp, &finfo) == 0) {
318 return rp;
319 }
320 } else { // relative path, start searching
321 // new search path with current dir global
322 vector<string> sp = g_incl_searchpath;
323 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000324
Martin Kraemer32c66e12006-11-09 00:06:36 +0000325 // iterate through paths
326 vector<string>::iterator it;
327 for (it = sp.begin(); it != sp.end(); it++) {
328 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000329
Martin Kraemer32c66e12006-11-09 00:06:36 +0000330 // Realpath!
331 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000332 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000333 continue;
334 }
Mark Slee2c44d202007-05-16 02:18:07 +0000335
Martin Kraemer32c66e12006-11-09 00:06:36 +0000336 // Stat this files
337 struct stat finfo;
338 if (stat(rp, &finfo) == 0) {
339 return rp;
340 }
341 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000342 }
Mark Slee2c44d202007-05-16 02:18:07 +0000343
Mark Sleef0712dc2006-10-25 19:03:57 +0000344 // Uh oh
345 pwarning(0, "Could not find include file %s\n", filename.c_str());
346 return std::string();
347}
348
349/**
David Reisscbd4bac2007-08-14 17:12:33 +0000350 * Clears any previously stored doctext string.
351 * Also prints a warning if we are discarding information.
352 */
353void clear_doctext() {
354 if (g_doctext != NULL) {
355 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
356 }
357 free(g_doctext);
358 g_doctext = NULL;
359}
360
361/**
David Reiss1ac05802007-07-30 22:00:27 +0000362 * Cleans up text commonly found in doxygen-like comments
363 *
364 * Warning: if you mix tabs and spaces in a non-uniform way,
365 * you will get what you deserve.
366 */
367char* clean_up_doctext(char* doctext) {
368 // Convert to C++ string, and remove Windows's carriage returns.
369 string docstring = doctext;
370 docstring.erase(
371 remove(docstring.begin(), docstring.end(), '\r'),
372 docstring.end());
373
374 // Separate into lines.
375 vector<string> lines;
376 string::size_type pos = string::npos;
377 string::size_type last;
378 while (true) {
379 last = (pos == string::npos) ? 0 : pos+1;
380 pos = docstring.find('\n', last);
381 if (pos == string::npos) {
382 // First bit of cleaning. If the last line is only whitespace, drop it.
383 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
384 if (nonwhite != string::npos) {
385 lines.push_back(docstring.substr(last));
386 }
387 break;
388 }
389 lines.push_back(docstring.substr(last, pos-last));
390 }
391
392 // A very profound docstring.
393 if (lines.empty()) {
394 return NULL;
395 }
396
397 // Clear leading whitespace from the first line.
398 pos = lines.front().find_first_not_of(" \t");
399 lines.front().erase(0, pos);
400
401 // If every nonblank line after the first has the same number of spaces/tabs,
402 // then a star, remove them.
403 bool have_prefix = true;
404 bool found_prefix = false;
405 string::size_type prefix_len = 0;
406 vector<string>::iterator l_iter;
407 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
408 if (l_iter->empty()) {
409 continue;
410 }
411
412 pos = l_iter->find_first_not_of(" \t");
413 if (!found_prefix) {
414 if (pos != string::npos) {
415 if (l_iter->at(pos) == '*') {
416 found_prefix = true;
417 prefix_len = pos;
418 } else {
419 have_prefix = false;
420 break;
421 }
422 } else {
423 // Whitespace-only line. Truncate it.
424 l_iter->clear();
425 }
426 } else if (l_iter->size() > pos
427 && l_iter->at(pos) == '*'
428 && pos == prefix_len) {
429 // Business as usual.
430 } else if (pos == string::npos) {
431 // Whitespace-only line. Let's truncate it for them.
432 l_iter->clear();
433 } else {
434 // The pattern has been broken.
435 have_prefix = false;
436 break;
437 }
438 }
439
440 // If our prefix survived, delete it from every line.
441 if (have_prefix) {
442 // Get the star too.
443 prefix_len++;
444 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
445 l_iter->erase(0, prefix_len);
446 }
447 }
448
449 // Now delete the minimum amount of leading whitespace from each line.
450 prefix_len = string::npos;
451 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
452 if (l_iter->empty()) {
453 continue;
454 }
455 pos = l_iter->find_first_not_of(" \t");
456 if (pos != string::npos
457 && (prefix_len == string::npos || pos < prefix_len)) {
458 prefix_len = pos;
459 }
460 }
461
462 // If our prefix survived, delete it from every line.
463 if (prefix_len != string::npos) {
464 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
465 l_iter->erase(0, prefix_len);
466 }
467 }
468
469 // Remove trailing whitespace from every line.
470 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
471 pos = l_iter->find_last_not_of(" \t");
472 if (pos != string::npos && pos != l_iter->length()-1) {
473 l_iter->erase(pos+1);
474 }
475 }
476
477 // If the first line is empty, remove it.
478 // Don't do this earlier because a lot of steps skip the first line.
479 if (lines.front().empty()) {
480 lines.erase(lines.begin());
481 }
482
483 // Now rejoin the lines and copy them back into doctext.
484 docstring.clear();
485 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
486 docstring += *l_iter;
487 docstring += '\n';
488 }
489
490 assert(docstring.length() <= strlen(doctext));
491 strcpy(doctext, docstring.c_str());
492 return doctext;
493}
494
495/** Set to true to debug docstring parsing */
496static bool dump_docs = false;
497
498/**
499 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000500 * Only works for top-level definitions and the whole program doc
501 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000502 */
503void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000504 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000505 if (!progdoc.empty()) {
506 printf("Whole program doc:\n%s\n", progdoc.c_str());
507 }
David Reiss1ac05802007-07-30 22:00:27 +0000508 const vector<t_typedef*>& typedefs = program->get_typedefs();
509 vector<t_typedef*>::const_iterator t_iter;
510 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
511 t_typedef* td = *t_iter;
512 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000513 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
514 }
515 }
516 const vector<t_enum*>& enums = program->get_enums();
517 vector<t_enum*>::const_iterator e_iter;
518 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
519 t_enum* en = *e_iter;
520 if (en->has_doc()) {
521 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
522 }
523 }
524 const vector<t_const*>& consts = program->get_consts();
525 vector<t_const*>::const_iterator c_iter;
526 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
527 t_const* co = *c_iter;
528 if (co->has_doc()) {
529 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
530 }
531 }
532 const vector<t_struct*>& structs = program->get_structs();
533 vector<t_struct*>::const_iterator s_iter;
534 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
535 t_struct* st = *s_iter;
536 if (st->has_doc()) {
537 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
538 }
539 }
540 const vector<t_struct*>& xceptions = program->get_xceptions();
541 vector<t_struct*>::const_iterator x_iter;
542 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
543 t_struct* xn = *x_iter;
544 if (xn->has_doc()) {
545 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
546 }
547 }
548 const vector<t_service*>& services = program->get_services();
549 vector<t_service*>::const_iterator v_iter;
550 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
551 t_service* sv = *v_iter;
552 if (sv->has_doc()) {
553 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000554 }
555 }
556}
557
558/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000559 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000560 */
561void generate_all_fingerprints(t_program* program) {
562 const vector<t_struct*>& structs = program->get_structs();
563 vector<t_struct*>::const_iterator s_iter;
564 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
565 t_struct* st = *s_iter;
566 st->generate_fingerprint();
567 }
568
David Reissd779cbe2007-08-31 01:42:55 +0000569 const vector<t_struct*>& xceptions = program->get_xceptions();
570 vector<t_struct*>::const_iterator x_iter;
571 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
572 t_struct* st = *x_iter;
573 st->generate_fingerprint();
574 }
575
David Reiss3c5d2fd2008-02-08 21:58:06 +0000576 const vector<t_enum*>& enums = program->get_enums();
577 vector<t_enum*>::const_iterator e_iter;
578 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
579 t_enum* e = *e_iter;
580 e->generate_fingerprint();
581 }
582
David Reiss47557bc2007-09-04 21:31:04 +0000583 g_type_void->generate_fingerprint();
584
David Reiss18bf22d2007-08-28 20:49:17 +0000585 // If you want to generate fingerprints for implicit structures, start here.
586 /*
587 const vector<t_service*>& services = program->get_services();
588 vector<t_service*>::const_iterator v_iter;
589 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
590 t_service* sv = *v_iter;
591 }
592 */
593}
594
595/**
Mark Slee31985722006-05-24 21:45:31 +0000596 * Diplays the usage message and then exits with an error code.
597 */
598void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000599 fprintf(stderr, "Usage: thrift [options] file\n");
600 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000601 fprintf(stderr, " -php Generate PHP output files\n");
602 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000603 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
604 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000605 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000606 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000607 fprintf(stderr, " -py Generate Python output files\n");
David Reiss6495adb2007-12-18 03:37:30 +0000608 fprintf(stderr, " -pyns Generate Python new-style classes (with -py)\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000609 fprintf(stderr, " -rb Generate Ruby output files\n");
610 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000611 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000612 fprintf(stderr, " -erl Generate Erlang output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000613 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
David Reiss7f42bcf2008-01-11 20:59:12 +0000614 fprintf(stderr, " -csharp Generate C# output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000615 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
616 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000617 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000618 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000619 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000620 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
621 fprintf(stderr, " -strict Strict compiler warnings on\n");
622 fprintf(stderr, " -v[erbose] Verbose mode\n");
623 fprintf(stderr, " -r[ecurse] Also generate included files\n");
624 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000625 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
626 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
627 fprintf(stderr, " Keys and values are options passed to the generator.\n");
628 fprintf(stderr, " Many options will not require values.\n");
629 fprintf(stderr, "\n");
630 fprintf(stderr, "Available generators (and options):\n");
631
632 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
633 t_generator_registry::gen_map_t::iterator iter;
634 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
635 fprintf(stderr, " %s (%s):\n",
636 iter->second->get_short_name().c_str(),
637 iter->second->get_long_name().c_str());
638 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
639 }
Mark Slee31985722006-05-24 21:45:31 +0000640 exit(1);
641}
642
643/**
Mark Slee30152872006-11-28 01:24:07 +0000644 * You know, when I started working on Thrift I really thought it wasn't going
645 * to become a programming language because it was just a generator and it
646 * wouldn't need runtime type information and all that jazz. But then we
647 * decided to add constants, and all of a sudden that means runtime type
648 * validation and inference, except the "runtime" is the code generator
649 * runtime. Shit. I've been had.
650 */
651void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
652 if (type->is_void()) {
653 throw "type error: cannot declare a void const: " + name;
654 }
655
656 if (type->is_base_type()) {
657 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
658 switch (tbase) {
659 case t_base_type::TYPE_STRING:
660 if (value->get_type() != t_const_value::CV_STRING) {
661 throw "type error: const \"" + name + "\" was declared as string";
662 }
663 break;
664 case t_base_type::TYPE_BOOL:
665 if (value->get_type() != t_const_value::CV_INTEGER) {
666 throw "type error: const \"" + name + "\" was declared as bool";
667 }
668 break;
669 case t_base_type::TYPE_BYTE:
670 if (value->get_type() != t_const_value::CV_INTEGER) {
671 throw "type error: const \"" + name + "\" was declared as byte";
672 }
673 break;
674 case t_base_type::TYPE_I16:
675 if (value->get_type() != t_const_value::CV_INTEGER) {
676 throw "type error: const \"" + name + "\" was declared as i16";
677 }
678 break;
679 case t_base_type::TYPE_I32:
680 if (value->get_type() != t_const_value::CV_INTEGER) {
681 throw "type error: const \"" + name + "\" was declared as i32";
682 }
683 break;
684 case t_base_type::TYPE_I64:
685 if (value->get_type() != t_const_value::CV_INTEGER) {
686 throw "type error: const \"" + name + "\" was declared as i64";
687 }
688 break;
689 case t_base_type::TYPE_DOUBLE:
690 if (value->get_type() != t_const_value::CV_INTEGER &&
691 value->get_type() != t_const_value::CV_DOUBLE) {
692 throw "type error: const \"" + name + "\" was declared as double";
693 }
694 break;
695 default:
David Reissdd7796f2007-08-28 21:09:06 +0000696 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000697 }
698 } else if (type->is_enum()) {
699 if (value->get_type() != t_const_value::CV_INTEGER) {
700 throw "type error: const \"" + name + "\" was declared as enum";
701 }
702 } else if (type->is_struct() || type->is_xception()) {
703 if (value->get_type() != t_const_value::CV_MAP) {
704 throw "type error: const \"" + name + "\" was declared as struct/xception";
705 }
706 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
707 vector<t_field*>::const_iterator f_iter;
708
709 const map<t_const_value*, t_const_value*>& val = value->get_map();
710 map<t_const_value*, t_const_value*>::const_iterator v_iter;
711 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
712 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
713 throw "type error: " + name + " struct key must be string";
714 }
715 t_type* field_type = NULL;
716 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
717 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
718 field_type = (*f_iter)->get_type();
719 }
720 }
721 if (field_type == NULL) {
722 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
723 }
724
725 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
726 }
727 } else if (type->is_map()) {
728 t_type* k_type = ((t_map*)type)->get_key_type();
729 t_type* v_type = ((t_map*)type)->get_val_type();
730 const map<t_const_value*, t_const_value*>& val = value->get_map();
731 map<t_const_value*, t_const_value*>::const_iterator v_iter;
732 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
733 validate_const_rec(name + "<key>", k_type, v_iter->first);
734 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000735 }
Mark Slee30152872006-11-28 01:24:07 +0000736 } else if (type->is_list() || type->is_set()) {
737 t_type* e_type;
738 if (type->is_list()) {
739 e_type = ((t_list*)type)->get_elem_type();
740 } else {
741 e_type = ((t_set*)type)->get_elem_type();
742 }
743 const vector<t_const_value*>& val = value->get_list();
744 vector<t_const_value*>::const_iterator v_iter;
745 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
746 validate_const_rec(name + "<elem>", e_type, *v_iter);
747 }
748 }
749}
750
751/**
752 * Check the type of the parsed const information against its declared type
753 */
754void validate_const_type(t_const* c) {
755 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
756}
757
758/**
Mark Slee7ff32452007-02-01 05:26:18 +0000759 * Check the type of a default value assigned to a field.
760 */
761void validate_field_value(t_field* field, t_const_value* cv) {
762 validate_const_rec(field->get_name(), field->get_type(), cv);
763}
764
765/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000766 * Check that all the elements of a throws block are actually exceptions.
767 */
768bool validate_throws(t_struct* throws) {
769 const vector<t_field*>& members = throws->get_members();
770 vector<t_field*>::const_iterator m_iter;
771 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
772 if (!(*m_iter)->get_type()->is_xception()) {
773 return false;
774 }
775 }
776 return true;
777}
778
779/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000780 * Parses a program
781 */
Mark Slee2c44d202007-05-16 02:18:07 +0000782void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000783 // Get scope file path
784 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000785
Mark Sleef0712dc2006-10-25 19:03:57 +0000786 // Set current dir global, which is used in the include_file function
787 g_curdir = directory_name(path);
788 g_curpath = path;
789
790 // Open the file
791 yyin = fopen(path.c_str(), "r");
792 if (yyin == 0) {
793 failure("Could not open input file: \"%s\"", path.c_str());
794 }
795
796 // Create new scope and scan for includes
797 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000798 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 g_program = program;
800 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000801 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000802 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000803 if (yyparse() != 0) {
804 failure("Parser error during include pass.");
805 }
806 } catch (string x) {
807 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000808 }
809 fclose(yyin);
810
811 // Recursively parse all the include programs
812 vector<t_program*>& includes = program->get_includes();
813 vector<t_program*>::iterator iter;
814 for (iter = includes.begin(); iter != includes.end(); ++iter) {
815 parse(*iter, program);
816 }
817
David Reiss204420f2008-01-11 20:59:03 +0000818 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000819 g_parse_mode = PROGRAM;
820 g_program = program;
821 g_scope = program->scope();
822 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
823 g_parent_prefix = program->get_name() + ".";
824 g_curpath = path;
825 yyin = fopen(path.c_str(), "r");
826 if (yyin == 0) {
827 failure("Could not open input file: \"%s\"", path.c_str());
828 }
829 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000830 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000831 try {
832 if (yyparse() != 0) {
833 failure("Parser error during types pass.");
834 }
835 } catch (string x) {
836 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000837 }
838 fclose(yyin);
839}
840
841/**
842 * Generate code
843 */
David Reissbd0db882008-02-27 01:54:51 +0000844void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000845 // Oooohh, recursive code generation, hot!!
846 if (gen_recurse) {
847 const vector<t_program*>& includes = program->get_includes();
848 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000849 // Propogate output path from parent to child programs
850 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000851
David Reissbd0db882008-02-27 01:54:51 +0000852 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000853 }
854 }
855
856 // Generate code!
857 try {
858 pverbose("Program: %s\n", program->get_path().c_str());
859
David Reiss18bf22d2007-08-28 20:49:17 +0000860 // Compute fingerprints.
861 generate_all_fingerprints(program);
862
Mark Sleef0712dc2006-10-25 19:03:57 +0000863 if (gen_php) {
864 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000865 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 +0000866 php->generate_program();
867 delete php;
868 }
869
870 if (gen_phpi) {
871 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000872 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000873 phpi->generate_program();
874 delete phpi;
875 }
876
877 if (gen_py) {
878 pverbose("Generating Python\n");
David Reiss6495adb2007-12-18 03:37:30 +0000879 t_py_generator* py = new t_py_generator(program, gen_py_newstyle);
Mark Sleef0712dc2006-10-25 19:03:57 +0000880 py->generate_program();
881 delete py;
882 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000883
Mark Slee6d7d5952007-01-27 01:44:22 +0000884 if (gen_rb) {
885 pverbose("Generating Ruby\n");
886 t_rb_generator* rb = new t_rb_generator(program);
887 rb->generate_program();
888 delete rb;
889 }
890
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000891 if (gen_xsd) {
892 pverbose("Generating XSD\n");
893 t_xsd_generator* xsd = new t_xsd_generator(program);
894 xsd->generate_program();
895 delete xsd;
896 }
897
Mark Slee2c44d202007-05-16 02:18:07 +0000898 if (gen_perl) {
899 pverbose("Generating PERL\n");
900 t_perl_generator* perl = new t_perl_generator(program);
901 perl->generate_program();
902 delete perl;
903 }
904
Christopher Pirob97b89d2007-09-18 00:07:42 +0000905 if (gen_erl) {
906 pverbose("Generating Erlang\n");
907 t_erl_generator* erl = new t_erl_generator(program);
908 erl->generate_program();
909 delete erl;
910 }
911
Mark Slee7e9eea42007-09-10 21:00:23 +0000912 if (gen_cocoa) {
913 pverbose("Generating Cocoa/Objective-C\n");
914 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
915 cocoa->generate_program();
916 delete cocoa;
917 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000918
David Reiss7f42bcf2008-01-11 20:59:12 +0000919 if (gen_csharp) {
920 pverbose("Generating C#\n");
921 t_csharp_generator* csharp = new t_csharp_generator(program);
922 csharp->generate_program();
923 delete csharp;
924 }
kholst76f2c882008-01-16 02:47:41 +0000925
David Reiss1ac05802007-07-30 22:00:27 +0000926 if (dump_docs) {
927 dump_docstrings(program);
928 }
David Reissbd0db882008-02-27 01:54:51 +0000929
930 vector<string>::const_iterator iter;
931 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
932 t_generator* generator = t_generator_registry::get_generator(program, *iter);
933
934 if (generator == NULL) {
935 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
936 } else {
937 pverbose("Generating \"%s\"\n", iter->c_str());
938 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000939 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000940 }
941 }
942
Mark Sleef0712dc2006-10-25 19:03:57 +0000943 } catch (string s) {
944 printf("Error: %s\n", s.c_str());
945 } catch (const char* exc) {
946 printf("Error: %s\n", exc);
947 }
948
949}
950
951/**
Mark Sleef5377b32006-10-10 01:42:59 +0000952 * Parse it up.. then spit it back out, in pretty much every language. Alright
953 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000954 */
955int main(int argc, char** argv) {
956 int i;
dweatherford65b70752007-10-31 02:18:14 +0000957 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000958
Mark Sleeb15a68b2006-06-07 06:46:24 +0000959 // Setup time string
960 time_t now = time(NULL);
961 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000962
Mark Sleef0712dc2006-10-25 19:03:57 +0000963 // Check for necessary arguments, you gotta have at least a filename and
964 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000965 if (argc < 2) {
966 usage();
967 }
Mark Slee31985722006-05-24 21:45:31 +0000968
David Reissbd0db882008-02-27 01:54:51 +0000969 vector<string> generator_strings;
970
David Reiss9cc2c132008-02-27 01:54:47 +0000971 // Set the current path to a dummy value to make warning messages clearer.
972 g_curpath = "arguments";
973
Mark Sleef5377b32006-10-10 01:42:59 +0000974 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000975 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000976 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000977
Mark Sleefdbee812006-09-27 18:50:48 +0000978 arg = strtok(argv[i], " ");
979 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000980 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000981 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000982 ++arg;
983 }
984
985 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000986 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000987 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000988 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000989 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000990 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000991 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000992 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000993 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000994 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000995 } else if (strcmp(arg, "-gen") == 0) {
996 arg = argv[++i];
997 if (arg == NULL) {
998 fprintf(stderr, "!!! Missing generator specification");
999 usage();
1000 }
1001 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +00001002 } else if (strcmp(arg, "-dense") == 0) {
1003 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +00001004 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001005 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +00001006 } else if (strcmp(arg, "-javabean") == 0) {
1007 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +00001008 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001009 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001010 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001011 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001012 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001013 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001014 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001015 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001016 gen_phps = true;
1017 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001018 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001019 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001020 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001021 gen_php = true;
1022 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001023 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001024 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001025 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001026 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001027 } else if (strcmp(arg, "-rest") == 0) {
1028 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001029 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001030 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001031 } else if (strcmp(arg, "-pyns") == 0) {
1032 gen_py = true;
1033 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001034 } else if (strcmp(arg, "-rb") == 0) {
1035 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001036 } else if (strcmp(arg, "-xsd") == 0) {
1037 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001038 } else if (strcmp(arg, "-perl") == 0) {
1039 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001040 } else if (strcmp(arg, "-erl") == 0) {
1041 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001042 } else if (strcmp(arg, "-ocaml") == 0) {
1043 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001044 } else if (strcmp(arg, "-hs") == 0) {
1045 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001046 } else if (strcmp(arg, "-cocoa") == 0) {
1047 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001048 } else if (strcmp(arg, "-st") == 0) {
1049 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001050 } else if (strcmp(arg, "-csharp") == 0) {
1051 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001052 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1053 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001054 } else if (strcmp(arg, "-I") == 0) {
1055 // An argument of "-I\ asdf" is invalid and has unknown results
1056 arg = argv[++i];
1057
1058 if (arg == NULL) {
1059 fprintf(stderr, "!!! Missing Include directory");
1060 usage();
1061 }
1062 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001063 } else if (strcmp(arg, "-o") == 0) {
1064 arg = argv[++i];
1065 if (arg == NULL) {
1066 fprintf(stderr, "-o: missing output directory");
1067 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001068 }
dweatherford65b70752007-10-31 02:18:14 +00001069 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001070
1071#ifdef MINGW
1072 //strip out trailing \ on Windows
1073 int last = out_path.length()-1;
1074 if (out_path[last] == '\\')
1075 {
1076 out_path.erase(last);
1077 }
1078#endif
1079
dweatherford65b70752007-10-31 02:18:14 +00001080 struct stat sb;
1081 if (stat(out_path.c_str(), &sb) < 0) {
1082 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1083 return -1;
1084 }
1085 if (! S_ISDIR(sb.st_mode)) {
1086 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1087 return -1;
1088 }
Mark Sleefdbee812006-09-27 18:50:48 +00001089 } else {
1090 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1091 usage();
1092 }
1093
1094 // Tokenize more
1095 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001096 }
1097 }
Mark Slee2c44d202007-05-16 02:18:07 +00001098
David Reiss400cd4b2008-02-27 01:54:55 +00001099 // TODO(dreiss): Delete these when everyone is using the new hotness.
1100 if (gen_cpp) {
1101 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1102 string gen_string = "cpp:";
1103 if (gen_dense) {
1104 gen_string.append("dense,");
1105 }
1106 if (g_cpp_use_include_prefix) {
1107 gen_string.append("include_prefix,");
1108 }
1109 generator_strings.push_back(gen_string);
1110 }
David Reisse1404d22008-02-27 01:55:05 +00001111 if (gen_java) {
1112 pwarning(1, "-java is deprecated. Use --gen java");
1113 generator_strings.push_back("java");
1114 }
1115 if (gen_javabean) {
1116 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1117 generator_strings.push_back("java:beans");
1118 }
David Reissa890b572008-03-27 21:40:35 +00001119 if (gen_st) {
1120 pwarning(1, "-st is deprecated. Use --gen st");
1121 generator_strings.push_back("st");
1122 }
David Reissf0521b12008-03-27 21:40:05 +00001123 if (gen_ocaml) {
1124 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1125 generator_strings.push_back("ocaml");
1126 }
David Reissaf3ab262008-03-27 21:40:16 +00001127 if (gen_hs) {
1128 pwarning(1, "-hs is deprecated. Use --gen hs");
1129 generator_strings.push_back("hs");
1130 }
David Reisse1404d22008-02-27 01:55:05 +00001131
Mark Sleef0712dc2006-10-25 19:03:57 +00001132 // You gotta generate something!
David Reissa890b572008-03-27 21:40:35 +00001133 if (!gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_erl && !gen_cocoa && !gen_csharp && generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001134 fprintf(stderr, "!!! No output language(s) specified\n\n");
1135 usage();
1136 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001137
1138 // Real-pathify it
1139 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +00001140 if (saferealpath(argv[i], rp) == NULL) {
1141 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001142 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001143 string input_file(rp);
1144
Mark Sleef5377b32006-10-10 01:42:59 +00001145 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001146 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001147 if (out_path.size()) {
1148 program->set_out_path(out_path);
1149 }
kholst76f2c882008-01-16 02:47:41 +00001150
David Reiss4b6a3c72008-02-27 22:28:12 +00001151 // Compute the cpp include prefix.
1152 // infer this from the filename passed in
1153 string input_filename = argv[i];
1154 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001155
David Reiss4b6a3c72008-02-27 22:28:12 +00001156 string::size_type last_slash = string::npos;
1157 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1158 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001159 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001160
David Reiss4b6a3c72008-02-27 22:28:12 +00001161 program->set_include_prefix(include_prefix);
1162
Mark Sleef0712dc2006-10-25 19:03:57 +00001163 // Initialize global types
1164 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1165 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001166 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1167 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001168 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1169 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001170 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1171 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1172 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1173 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1174 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1175 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001176
Mark Sleef5377b32006-10-10 01:42:59 +00001177 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001178 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001179
David Reiss9cc2c132008-02-27 01:54:47 +00001180 // The current path is not really relevant when we are doing generation.
1181 // Reset the variable to make warning messages clearer.
1182 g_curpath = "generation";
1183 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1184 // That is what shows up during argument parsing.
1185 yylineno = 1;
1186
Mark Sleef0712dc2006-10-25 19:03:57 +00001187 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001188 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001189
Mark Sleef0712dc2006-10-25 19:03:57 +00001190 // Clean up. Who am I kidding... this program probably orphans heap memory
1191 // all over the place, but who cares because it is about to exit and it is
1192 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001193
Mark Sleef0712dc2006-10-25 19:03:57 +00001194 delete program;
1195 delete g_type_void;
1196 delete g_type_string;
1197 delete g_type_bool;
1198 delete g_type_byte;
1199 delete g_type_i16;
1200 delete g_type_i32;
1201 delete g_type_i64;
1202 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001203
1204 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001205 return 0;
1206}