blob: a4c1dd7602a87d965e14294eaf4b1091ee03dac4 [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"
Mark Sleeefd37f12007-11-20 05:13:09 +000045#include "generate/t_st_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000046#include "generate/t_csharp_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000047
48using namespace std;
49
Mark Sleef5377b32006-10-10 01:42:59 +000050/**
51 * Global program tree
52 */
Mark Slee31985722006-05-24 21:45:31 +000053t_program* g_program;
54
Mark Sleef5377b32006-10-10 01:42:59 +000055/**
Mark Sleef0712dc2006-10-25 19:03:57 +000056 * Global types
57 */
58
59t_type* g_type_void;
60t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000061t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000062t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000063t_type* g_type_bool;
64t_type* g_type_byte;
65t_type* g_type_i16;
66t_type* g_type_i32;
67t_type* g_type_i64;
68t_type* g_type_double;
69
70/**
71 * Global scope
72 */
73t_scope* g_scope;
74
75/**
76 * Parent scope to also parse types
77 */
78t_scope* g_parent_scope;
79
80/**
81 * Prefix for putting types in parent scope
82 */
83string g_parent_prefix;
84
85/**
86 * Parsing pass
87 */
88PARSE_MODE g_parse_mode;
89
90/**
91 * Current directory of file being parsed
92 */
93string g_curdir;
94
95/**
96 * Current file being parsed
97 */
98string g_curpath;
99
100/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000101 * Search path for inclusions
102 */
Mark Slee2329a832006-11-09 00:23:30 +0000103vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000104
105/**
kholst76f2c882008-01-16 02:47:41 +0000106 * Should C++ include statements use path prefixes for other thrift-generated
107 * header files
108 */
109bool g_cpp_use_include_prefix = false;
110
111/**
Mark Sleef5377b32006-10-10 01:42:59 +0000112 * Global debug state
113 */
Mark Slee31985722006-05-24 21:45:31 +0000114int g_debug = 0;
115
Mark Sleef5377b32006-10-10 01:42:59 +0000116/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000117 * Warning level
118 */
119int g_warn = 1;
120
121/**
122 * Verbose output
123 */
124int g_verbose = 0;
125
126/**
Mark Sleef5377b32006-10-10 01:42:59 +0000127 * Global time string
128 */
Mark Slee31985722006-05-24 21:45:31 +0000129char* g_time_str;
130
Mark Slee31985722006-05-24 21:45:31 +0000131/**
David Reisscbd4bac2007-08-14 17:12:33 +0000132 * The last parsed doctext comment.
133 */
134char* g_doctext;
135
136/**
137 * The location of the last parsed doctext comment.
138 */
139int g_doctext_lineno;
140
141/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000142 * Flags to control code generation
143 */
144bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000145bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000146bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000147bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000148bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000149bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000150bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000151bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000152bool gen_php = false;
153bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000154bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000155bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000156bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000157bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000158bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000159bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000160bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000161bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000162bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000163bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000164bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000165bool gen_recurse = false;
166
167/**
David Reiss204420f2008-01-11 20:59:03 +0000168 * MinGW doesn't have realpath, so use fallback implementation in that case,
169 * otherwise this just calls through to realpath
170 */
171char *saferealpath(const char *path, char *resolved_path) {
172#ifdef MINGW
173 char buf[MAX_PATH];
174 char* basename;
175 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
176 if (len == 0 || len > MAX_PATH - 1){
177 strcpy(resolved_path, path);
178 } else {
179 CharLowerBuff(buf, len);
180 strcpy(resolved_path, buf);
181 }
182 return resolved_path;
183#else
184 return realpath(path, resolved_path);
185#endif
186}
187
188
189/**
Mark Slee31985722006-05-24 21:45:31 +0000190 * Report an error to the user. This is called yyerror for historical
191 * reasons (lex and yacc expect the error reporting routine to be called
192 * this). Call this function to report any errors to the user.
193 * yyerror takes printf style arguments.
194 *
195 * @param fmt C format string followed by additional arguments
196 */
197void yyerror(char* fmt, ...) {
198 va_list args;
199 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000200 "[ERROR:%s:%d] (last token was '%s')\n",
201 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000202 yylineno,
203 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000204
205 va_start(args, fmt);
206 vfprintf(stderr, fmt, args);
207 va_end(args);
208
209 fprintf(stderr, "\n");
210}
211
212/**
213 * Prints a debug message from the parser.
214 *
215 * @param fmt C format string followed by additional arguments
216 */
217void pdebug(char* fmt, ...) {
218 if (g_debug == 0) {
219 return;
220 }
221 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000222 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000223 va_start(args, fmt);
224 vprintf(fmt, args);
225 va_end(args);
226 printf("\n");
227}
228
229/**
230 * Prints a verbose output mode message
231 *
232 * @param fmt C format string followed by additional arguments
233 */
234void pverbose(char* fmt, ...) {
235 if (g_verbose == 0) {
236 return;
237 }
238 va_list args;
239 va_start(args, fmt);
240 vprintf(fmt, args);
241 va_end(args);
242}
243
244/**
245 * Prints a warning message
246 *
247 * @param fmt C format string followed by additional arguments
248 */
249void pwarning(int level, char* fmt, ...) {
250 if (g_warn < level) {
251 return;
252 }
253 va_list args;
254 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000255 va_start(args, fmt);
256 vprintf(fmt, args);
257 va_end(args);
258 printf("\n");
259}
260
261/**
262 * Prints a failure message and exits
263 *
264 * @param fmt C format string followed by additional arguments
265 */
Mark Slee30152872006-11-28 01:24:07 +0000266void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000267 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000268 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000269 va_start(args, fmt);
270 vfprintf(stderr, fmt, args);
271 va_end(args);
272 printf("\n");
273 exit(1);
274}
275
276/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000277 * Converts a string filename into a thrift program name
278 */
279string program_name(string filename) {
280 string::size_type slash = filename.rfind("/");
281 if (slash != string::npos) {
282 filename = filename.substr(slash+1);
283 }
284 string::size_type dot = filename.rfind(".");
285 if (dot != string::npos) {
286 filename = filename.substr(0, dot);
287 }
288 return filename;
289}
290
291/**
292 * Gets the directory path of a filename
293 */
294string directory_name(string filename) {
295 string::size_type slash = filename.rfind("/");
296 // No slash, just use the current directory
297 if (slash == string::npos) {
298 return ".";
299 }
300 return filename.substr(0, slash);
301}
302
303/**
304 * Finds the appropriate file path for the given filename
305 */
306string include_file(string filename) {
307 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000308 if (filename[0] == '/') {
309 // Realpath!
310 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000311 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000312 pwarning(0, "Cannot open include file %s\n", filename.c_str());
313 return std::string();
314 }
Mark Slee2c44d202007-05-16 02:18:07 +0000315
316 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000317 struct stat finfo;
318 if (stat(rp, &finfo) == 0) {
319 return rp;
320 }
321 } else { // relative path, start searching
322 // new search path with current dir global
323 vector<string> sp = g_incl_searchpath;
324 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000325
Martin Kraemer32c66e12006-11-09 00:06:36 +0000326 // iterate through paths
327 vector<string>::iterator it;
328 for (it = sp.begin(); it != sp.end(); it++) {
329 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000330
Martin Kraemer32c66e12006-11-09 00:06:36 +0000331 // Realpath!
332 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000333 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000334 continue;
335 }
Mark Slee2c44d202007-05-16 02:18:07 +0000336
Martin Kraemer32c66e12006-11-09 00:06:36 +0000337 // Stat this files
338 struct stat finfo;
339 if (stat(rp, &finfo) == 0) {
340 return rp;
341 }
342 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000343 }
Mark Slee2c44d202007-05-16 02:18:07 +0000344
Mark Sleef0712dc2006-10-25 19:03:57 +0000345 // Uh oh
346 pwarning(0, "Could not find include file %s\n", filename.c_str());
347 return std::string();
348}
349
350/**
David Reisscbd4bac2007-08-14 17:12:33 +0000351 * Clears any previously stored doctext string.
352 * Also prints a warning if we are discarding information.
353 */
354void clear_doctext() {
355 if (g_doctext != NULL) {
356 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
357 }
358 free(g_doctext);
359 g_doctext = NULL;
360}
361
362/**
David Reiss1ac05802007-07-30 22:00:27 +0000363 * Cleans up text commonly found in doxygen-like comments
364 *
365 * Warning: if you mix tabs and spaces in a non-uniform way,
366 * you will get what you deserve.
367 */
368char* clean_up_doctext(char* doctext) {
369 // Convert to C++ string, and remove Windows's carriage returns.
370 string docstring = doctext;
371 docstring.erase(
372 remove(docstring.begin(), docstring.end(), '\r'),
373 docstring.end());
374
375 // Separate into lines.
376 vector<string> lines;
377 string::size_type pos = string::npos;
378 string::size_type last;
379 while (true) {
380 last = (pos == string::npos) ? 0 : pos+1;
381 pos = docstring.find('\n', last);
382 if (pos == string::npos) {
383 // First bit of cleaning. If the last line is only whitespace, drop it.
384 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
385 if (nonwhite != string::npos) {
386 lines.push_back(docstring.substr(last));
387 }
388 break;
389 }
390 lines.push_back(docstring.substr(last, pos-last));
391 }
392
393 // A very profound docstring.
394 if (lines.empty()) {
395 return NULL;
396 }
397
398 // Clear leading whitespace from the first line.
399 pos = lines.front().find_first_not_of(" \t");
400 lines.front().erase(0, pos);
401
402 // If every nonblank line after the first has the same number of spaces/tabs,
403 // then a star, remove them.
404 bool have_prefix = true;
405 bool found_prefix = false;
406 string::size_type prefix_len = 0;
407 vector<string>::iterator l_iter;
408 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
409 if (l_iter->empty()) {
410 continue;
411 }
412
413 pos = l_iter->find_first_not_of(" \t");
414 if (!found_prefix) {
415 if (pos != string::npos) {
416 if (l_iter->at(pos) == '*') {
417 found_prefix = true;
418 prefix_len = pos;
419 } else {
420 have_prefix = false;
421 break;
422 }
423 } else {
424 // Whitespace-only line. Truncate it.
425 l_iter->clear();
426 }
427 } else if (l_iter->size() > pos
428 && l_iter->at(pos) == '*'
429 && pos == prefix_len) {
430 // Business as usual.
431 } else if (pos == string::npos) {
432 // Whitespace-only line. Let's truncate it for them.
433 l_iter->clear();
434 } else {
435 // The pattern has been broken.
436 have_prefix = false;
437 break;
438 }
439 }
440
441 // If our prefix survived, delete it from every line.
442 if (have_prefix) {
443 // Get the star too.
444 prefix_len++;
445 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
446 l_iter->erase(0, prefix_len);
447 }
448 }
449
450 // Now delete the minimum amount of leading whitespace from each line.
451 prefix_len = string::npos;
452 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
453 if (l_iter->empty()) {
454 continue;
455 }
456 pos = l_iter->find_first_not_of(" \t");
457 if (pos != string::npos
458 && (prefix_len == string::npos || pos < prefix_len)) {
459 prefix_len = pos;
460 }
461 }
462
463 // If our prefix survived, delete it from every line.
464 if (prefix_len != string::npos) {
465 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
466 l_iter->erase(0, prefix_len);
467 }
468 }
469
470 // Remove trailing whitespace from every line.
471 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
472 pos = l_iter->find_last_not_of(" \t");
473 if (pos != string::npos && pos != l_iter->length()-1) {
474 l_iter->erase(pos+1);
475 }
476 }
477
478 // If the first line is empty, remove it.
479 // Don't do this earlier because a lot of steps skip the first line.
480 if (lines.front().empty()) {
481 lines.erase(lines.begin());
482 }
483
484 // Now rejoin the lines and copy them back into doctext.
485 docstring.clear();
486 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
487 docstring += *l_iter;
488 docstring += '\n';
489 }
490
491 assert(docstring.length() <= strlen(doctext));
492 strcpy(doctext, docstring.c_str());
493 return doctext;
494}
495
496/** Set to true to debug docstring parsing */
497static bool dump_docs = false;
498
499/**
500 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000501 * Only works for top-level definitions and the whole program doc
502 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000503 */
504void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000505 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000506 if (!progdoc.empty()) {
507 printf("Whole program doc:\n%s\n", progdoc.c_str());
508 }
David Reiss1ac05802007-07-30 22:00:27 +0000509 const vector<t_typedef*>& typedefs = program->get_typedefs();
510 vector<t_typedef*>::const_iterator t_iter;
511 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
512 t_typedef* td = *t_iter;
513 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000514 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
515 }
516 }
517 const vector<t_enum*>& enums = program->get_enums();
518 vector<t_enum*>::const_iterator e_iter;
519 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
520 t_enum* en = *e_iter;
521 if (en->has_doc()) {
522 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
523 }
524 }
525 const vector<t_const*>& consts = program->get_consts();
526 vector<t_const*>::const_iterator c_iter;
527 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
528 t_const* co = *c_iter;
529 if (co->has_doc()) {
530 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
531 }
532 }
533 const vector<t_struct*>& structs = program->get_structs();
534 vector<t_struct*>::const_iterator s_iter;
535 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
536 t_struct* st = *s_iter;
537 if (st->has_doc()) {
538 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
539 }
540 }
541 const vector<t_struct*>& xceptions = program->get_xceptions();
542 vector<t_struct*>::const_iterator x_iter;
543 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
544 t_struct* xn = *x_iter;
545 if (xn->has_doc()) {
546 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
547 }
548 }
549 const vector<t_service*>& services = program->get_services();
550 vector<t_service*>::const_iterator v_iter;
551 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
552 t_service* sv = *v_iter;
553 if (sv->has_doc()) {
554 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000555 }
556 }
557}
558
559/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000560 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000561 */
562void generate_all_fingerprints(t_program* program) {
563 const vector<t_struct*>& structs = program->get_structs();
564 vector<t_struct*>::const_iterator s_iter;
565 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
566 t_struct* st = *s_iter;
567 st->generate_fingerprint();
568 }
569
David Reissd779cbe2007-08-31 01:42:55 +0000570 const vector<t_struct*>& xceptions = program->get_xceptions();
571 vector<t_struct*>::const_iterator x_iter;
572 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
573 t_struct* st = *x_iter;
574 st->generate_fingerprint();
575 }
576
David Reiss3c5d2fd2008-02-08 21:58:06 +0000577 const vector<t_enum*>& enums = program->get_enums();
578 vector<t_enum*>::const_iterator e_iter;
579 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
580 t_enum* e = *e_iter;
581 e->generate_fingerprint();
582 }
583
David Reiss47557bc2007-09-04 21:31:04 +0000584 g_type_void->generate_fingerprint();
585
David Reiss18bf22d2007-08-28 20:49:17 +0000586 // If you want to generate fingerprints for implicit structures, start here.
587 /*
588 const vector<t_service*>& services = program->get_services();
589 vector<t_service*>::const_iterator v_iter;
590 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
591 t_service* sv = *v_iter;
592 }
593 */
594}
595
596/**
Mark Slee31985722006-05-24 21:45:31 +0000597 * Diplays the usage message and then exits with an error code.
598 */
599void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000600 fprintf(stderr, "Usage: thrift [options] file\n");
601 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000602 fprintf(stderr, " -php Generate PHP output files\n");
603 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000604 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
605 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000606 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000607 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000608 fprintf(stderr, " -py Generate Python output files\n");
David Reiss6495adb2007-12-18 03:37:30 +0000609 fprintf(stderr, " -pyns Generate Python new-style classes (with -py)\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000610 fprintf(stderr, " -rb Generate Ruby output files\n");
611 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000612 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000613 fprintf(stderr, " -erl Generate Erlang output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000614 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
David Reiss7f42bcf2008-01-11 20:59:12 +0000615 fprintf(stderr, " -csharp Generate C# output files\n");
Mark Sleeefd37f12007-11-20 05:13:09 +0000616 fprintf(stderr, " -st Generate Squeak/Smalltalk output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000617 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
618 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000619 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000620 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000621 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000622 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
623 fprintf(stderr, " -strict Strict compiler warnings on\n");
624 fprintf(stderr, " -v[erbose] Verbose mode\n");
625 fprintf(stderr, " -r[ecurse] Also generate included files\n");
626 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000627 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
628 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
629 fprintf(stderr, " Keys and values are options passed to the generator.\n");
630 fprintf(stderr, " Many options will not require values.\n");
631 fprintf(stderr, "\n");
632 fprintf(stderr, "Available generators (and options):\n");
633
634 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
635 t_generator_registry::gen_map_t::iterator iter;
636 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
637 fprintf(stderr, " %s (%s):\n",
638 iter->second->get_short_name().c_str(),
639 iter->second->get_long_name().c_str());
640 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
641 }
Mark Slee31985722006-05-24 21:45:31 +0000642 exit(1);
643}
644
645/**
Mark Slee30152872006-11-28 01:24:07 +0000646 * You know, when I started working on Thrift I really thought it wasn't going
647 * to become a programming language because it was just a generator and it
648 * wouldn't need runtime type information and all that jazz. But then we
649 * decided to add constants, and all of a sudden that means runtime type
650 * validation and inference, except the "runtime" is the code generator
651 * runtime. Shit. I've been had.
652 */
653void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
654 if (type->is_void()) {
655 throw "type error: cannot declare a void const: " + name;
656 }
657
658 if (type->is_base_type()) {
659 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
660 switch (tbase) {
661 case t_base_type::TYPE_STRING:
662 if (value->get_type() != t_const_value::CV_STRING) {
663 throw "type error: const \"" + name + "\" was declared as string";
664 }
665 break;
666 case t_base_type::TYPE_BOOL:
667 if (value->get_type() != t_const_value::CV_INTEGER) {
668 throw "type error: const \"" + name + "\" was declared as bool";
669 }
670 break;
671 case t_base_type::TYPE_BYTE:
672 if (value->get_type() != t_const_value::CV_INTEGER) {
673 throw "type error: const \"" + name + "\" was declared as byte";
674 }
675 break;
676 case t_base_type::TYPE_I16:
677 if (value->get_type() != t_const_value::CV_INTEGER) {
678 throw "type error: const \"" + name + "\" was declared as i16";
679 }
680 break;
681 case t_base_type::TYPE_I32:
682 if (value->get_type() != t_const_value::CV_INTEGER) {
683 throw "type error: const \"" + name + "\" was declared as i32";
684 }
685 break;
686 case t_base_type::TYPE_I64:
687 if (value->get_type() != t_const_value::CV_INTEGER) {
688 throw "type error: const \"" + name + "\" was declared as i64";
689 }
690 break;
691 case t_base_type::TYPE_DOUBLE:
692 if (value->get_type() != t_const_value::CV_INTEGER &&
693 value->get_type() != t_const_value::CV_DOUBLE) {
694 throw "type error: const \"" + name + "\" was declared as double";
695 }
696 break;
697 default:
David Reissdd7796f2007-08-28 21:09:06 +0000698 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000699 }
700 } else if (type->is_enum()) {
701 if (value->get_type() != t_const_value::CV_INTEGER) {
702 throw "type error: const \"" + name + "\" was declared as enum";
703 }
704 } else if (type->is_struct() || type->is_xception()) {
705 if (value->get_type() != t_const_value::CV_MAP) {
706 throw "type error: const \"" + name + "\" was declared as struct/xception";
707 }
708 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
709 vector<t_field*>::const_iterator f_iter;
710
711 const map<t_const_value*, t_const_value*>& val = value->get_map();
712 map<t_const_value*, t_const_value*>::const_iterator v_iter;
713 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
714 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
715 throw "type error: " + name + " struct key must be string";
716 }
717 t_type* field_type = NULL;
718 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
719 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
720 field_type = (*f_iter)->get_type();
721 }
722 }
723 if (field_type == NULL) {
724 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
725 }
726
727 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
728 }
729 } else if (type->is_map()) {
730 t_type* k_type = ((t_map*)type)->get_key_type();
731 t_type* v_type = ((t_map*)type)->get_val_type();
732 const map<t_const_value*, t_const_value*>& val = value->get_map();
733 map<t_const_value*, t_const_value*>::const_iterator v_iter;
734 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
735 validate_const_rec(name + "<key>", k_type, v_iter->first);
736 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000737 }
Mark Slee30152872006-11-28 01:24:07 +0000738 } else if (type->is_list() || type->is_set()) {
739 t_type* e_type;
740 if (type->is_list()) {
741 e_type = ((t_list*)type)->get_elem_type();
742 } else {
743 e_type = ((t_set*)type)->get_elem_type();
744 }
745 const vector<t_const_value*>& val = value->get_list();
746 vector<t_const_value*>::const_iterator v_iter;
747 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
748 validate_const_rec(name + "<elem>", e_type, *v_iter);
749 }
750 }
751}
752
753/**
754 * Check the type of the parsed const information against its declared type
755 */
756void validate_const_type(t_const* c) {
757 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
758}
759
760/**
Mark Slee7ff32452007-02-01 05:26:18 +0000761 * Check the type of a default value assigned to a field.
762 */
763void validate_field_value(t_field* field, t_const_value* cv) {
764 validate_const_rec(field->get_name(), field->get_type(), cv);
765}
766
767/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000768 * Check that all the elements of a throws block are actually exceptions.
769 */
770bool validate_throws(t_struct* throws) {
771 const vector<t_field*>& members = throws->get_members();
772 vector<t_field*>::const_iterator m_iter;
773 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
774 if (!(*m_iter)->get_type()->is_xception()) {
775 return false;
776 }
777 }
778 return true;
779}
780
781/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000782 * Parses a program
783 */
Mark Slee2c44d202007-05-16 02:18:07 +0000784void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000785 // Get scope file path
786 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000787
Mark Sleef0712dc2006-10-25 19:03:57 +0000788 // Set current dir global, which is used in the include_file function
789 g_curdir = directory_name(path);
790 g_curpath = path;
791
792 // Open the file
793 yyin = fopen(path.c_str(), "r");
794 if (yyin == 0) {
795 failure("Could not open input file: \"%s\"", path.c_str());
796 }
797
798 // Create new scope and scan for includes
799 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000800 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000801 g_program = program;
802 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000803 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000804 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000805 if (yyparse() != 0) {
806 failure("Parser error during include pass.");
807 }
808 } catch (string x) {
809 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000810 }
811 fclose(yyin);
812
813 // Recursively parse all the include programs
814 vector<t_program*>& includes = program->get_includes();
815 vector<t_program*>::iterator iter;
816 for (iter = includes.begin(); iter != includes.end(); ++iter) {
817 parse(*iter, program);
818 }
819
David Reiss204420f2008-01-11 20:59:03 +0000820 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000821 g_parse_mode = PROGRAM;
822 g_program = program;
823 g_scope = program->scope();
824 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
825 g_parent_prefix = program->get_name() + ".";
826 g_curpath = path;
827 yyin = fopen(path.c_str(), "r");
828 if (yyin == 0) {
829 failure("Could not open input file: \"%s\"", path.c_str());
830 }
831 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000832 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000833 try {
834 if (yyparse() != 0) {
835 failure("Parser error during types pass.");
836 }
837 } catch (string x) {
838 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000839 }
840 fclose(yyin);
841}
842
843/**
844 * Generate code
845 */
David Reissbd0db882008-02-27 01:54:51 +0000846void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000847 // Oooohh, recursive code generation, hot!!
848 if (gen_recurse) {
849 const vector<t_program*>& includes = program->get_includes();
850 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000851 // Propogate output path from parent to child programs
852 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000853
David Reissbd0db882008-02-27 01:54:51 +0000854 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000855 }
856 }
857
858 // Generate code!
859 try {
860 pverbose("Program: %s\n", program->get_path().c_str());
861
David Reiss18bf22d2007-08-28 20:49:17 +0000862 // Compute fingerprints.
863 generate_all_fingerprints(program);
864
Mark Sleef0712dc2006-10-25 19:03:57 +0000865 if (gen_php) {
866 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000867 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 +0000868 php->generate_program();
869 delete php;
870 }
871
872 if (gen_phpi) {
873 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000874 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000875 phpi->generate_program();
876 delete phpi;
877 }
878
879 if (gen_py) {
880 pverbose("Generating Python\n");
David Reiss6495adb2007-12-18 03:37:30 +0000881 t_py_generator* py = new t_py_generator(program, gen_py_newstyle);
Mark Sleef0712dc2006-10-25 19:03:57 +0000882 py->generate_program();
883 delete py;
884 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000885
Mark Slee6d7d5952007-01-27 01:44:22 +0000886 if (gen_rb) {
887 pverbose("Generating Ruby\n");
888 t_rb_generator* rb = new t_rb_generator(program);
889 rb->generate_program();
890 delete rb;
891 }
892
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000893 if (gen_xsd) {
894 pverbose("Generating XSD\n");
895 t_xsd_generator* xsd = new t_xsd_generator(program);
896 xsd->generate_program();
897 delete xsd;
898 }
899
Mark Slee2c44d202007-05-16 02:18:07 +0000900 if (gen_perl) {
901 pverbose("Generating PERL\n");
902 t_perl_generator* perl = new t_perl_generator(program);
903 perl->generate_program();
904 delete perl;
905 }
906
Christopher Pirob97b89d2007-09-18 00:07:42 +0000907 if (gen_erl) {
908 pverbose("Generating Erlang\n");
909 t_erl_generator* erl = new t_erl_generator(program);
910 erl->generate_program();
911 delete erl;
912 }
913
Mark Slee7e9eea42007-09-10 21:00:23 +0000914 if (gen_cocoa) {
915 pverbose("Generating Cocoa/Objective-C\n");
916 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
917 cocoa->generate_program();
918 delete cocoa;
919 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000920
Mark Sleeefd37f12007-11-20 05:13:09 +0000921 if (gen_st) {
922 pverbose("Generating Smalltalk/Squeak\n");
923 t_st_generator* st = new t_st_generator(program);
924 st->generate_program();
925 delete st;
926 }
927
David Reiss7f42bcf2008-01-11 20:59:12 +0000928 if (gen_csharp) {
929 pverbose("Generating C#\n");
930 t_csharp_generator* csharp = new t_csharp_generator(program);
931 csharp->generate_program();
932 delete csharp;
933 }
kholst76f2c882008-01-16 02:47:41 +0000934
David Reiss1ac05802007-07-30 22:00:27 +0000935 if (dump_docs) {
936 dump_docstrings(program);
937 }
David Reissbd0db882008-02-27 01:54:51 +0000938
939 vector<string>::const_iterator iter;
940 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
941 t_generator* generator = t_generator_registry::get_generator(program, *iter);
942
943 if (generator == NULL) {
944 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
945 } else {
946 pverbose("Generating \"%s\"\n", iter->c_str());
947 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000948 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000949 }
950 }
951
Mark Sleef0712dc2006-10-25 19:03:57 +0000952 } catch (string s) {
953 printf("Error: %s\n", s.c_str());
954 } catch (const char* exc) {
955 printf("Error: %s\n", exc);
956 }
957
958}
959
960/**
Mark Sleef5377b32006-10-10 01:42:59 +0000961 * Parse it up.. then spit it back out, in pretty much every language. Alright
962 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000963 */
964int main(int argc, char** argv) {
965 int i;
dweatherford65b70752007-10-31 02:18:14 +0000966 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000967
Mark Sleeb15a68b2006-06-07 06:46:24 +0000968 // Setup time string
969 time_t now = time(NULL);
970 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000971
Mark Sleef0712dc2006-10-25 19:03:57 +0000972 // Check for necessary arguments, you gotta have at least a filename and
973 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000974 if (argc < 2) {
975 usage();
976 }
Mark Slee31985722006-05-24 21:45:31 +0000977
David Reissbd0db882008-02-27 01:54:51 +0000978 vector<string> generator_strings;
979
David Reiss9cc2c132008-02-27 01:54:47 +0000980 // Set the current path to a dummy value to make warning messages clearer.
981 g_curpath = "arguments";
982
Mark Sleef5377b32006-10-10 01:42:59 +0000983 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000984 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000985 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000986
Mark Sleefdbee812006-09-27 18:50:48 +0000987 arg = strtok(argv[i], " ");
988 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000989 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000990 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000991 ++arg;
992 }
993
994 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000995 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000996 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000997 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000998 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000999 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001000 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001001 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001002 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001003 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +00001004 } else if (strcmp(arg, "-gen") == 0) {
1005 arg = argv[++i];
1006 if (arg == NULL) {
1007 fprintf(stderr, "!!! Missing generator specification");
1008 usage();
1009 }
1010 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +00001011 } else if (strcmp(arg, "-dense") == 0) {
1012 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +00001013 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001014 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +00001015 } else if (strcmp(arg, "-javabean") == 0) {
1016 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +00001017 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001018 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001019 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001020 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001021 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001022 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001023 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001024 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001025 gen_phps = true;
1026 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001027 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001028 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001029 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001030 gen_php = true;
1031 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001032 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001033 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001034 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001035 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001036 } else if (strcmp(arg, "-rest") == 0) {
1037 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001038 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001039 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001040 } else if (strcmp(arg, "-pyns") == 0) {
1041 gen_py = true;
1042 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001043 } else if (strcmp(arg, "-rb") == 0) {
1044 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001045 } else if (strcmp(arg, "-xsd") == 0) {
1046 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001047 } else if (strcmp(arg, "-perl") == 0) {
1048 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001049 } else if (strcmp(arg, "-erl") == 0) {
1050 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001051 } else if (strcmp(arg, "-ocaml") == 0) {
1052 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001053 } else if (strcmp(arg, "-hs") == 0) {
1054 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001055 } else if (strcmp(arg, "-cocoa") == 0) {
1056 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001057 } else if (strcmp(arg, "-st") == 0) {
1058 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001059 } else if (strcmp(arg, "-csharp") == 0) {
1060 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001061 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1062 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001063 } else if (strcmp(arg, "-I") == 0) {
1064 // An argument of "-I\ asdf" is invalid and has unknown results
1065 arg = argv[++i];
1066
1067 if (arg == NULL) {
1068 fprintf(stderr, "!!! Missing Include directory");
1069 usage();
1070 }
1071 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001072 } else if (strcmp(arg, "-o") == 0) {
1073 arg = argv[++i];
1074 if (arg == NULL) {
1075 fprintf(stderr, "-o: missing output directory");
1076 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001077 }
dweatherford65b70752007-10-31 02:18:14 +00001078 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001079
1080#ifdef MINGW
1081 //strip out trailing \ on Windows
1082 int last = out_path.length()-1;
1083 if (out_path[last] == '\\')
1084 {
1085 out_path.erase(last);
1086 }
1087#endif
1088
dweatherford65b70752007-10-31 02:18:14 +00001089 struct stat sb;
1090 if (stat(out_path.c_str(), &sb) < 0) {
1091 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1092 return -1;
1093 }
1094 if (! S_ISDIR(sb.st_mode)) {
1095 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1096 return -1;
1097 }
Mark Sleefdbee812006-09-27 18:50:48 +00001098 } else {
1099 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1100 usage();
1101 }
1102
1103 // Tokenize more
1104 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001105 }
1106 }
Mark Slee2c44d202007-05-16 02:18:07 +00001107
David Reiss400cd4b2008-02-27 01:54:55 +00001108 // TODO(dreiss): Delete these when everyone is using the new hotness.
1109 if (gen_cpp) {
1110 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1111 string gen_string = "cpp:";
1112 if (gen_dense) {
1113 gen_string.append("dense,");
1114 }
1115 if (g_cpp_use_include_prefix) {
1116 gen_string.append("include_prefix,");
1117 }
1118 generator_strings.push_back(gen_string);
1119 }
David Reisse1404d22008-02-27 01:55:05 +00001120 if (gen_java) {
1121 pwarning(1, "-java is deprecated. Use --gen java");
1122 generator_strings.push_back("java");
1123 }
1124 if (gen_javabean) {
1125 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1126 generator_strings.push_back("java:beans");
1127 }
David Reissf0521b12008-03-27 21:40:05 +00001128 if (gen_ocaml) {
1129 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1130 generator_strings.push_back("ocaml");
1131 }
David Reissaf3ab262008-03-27 21:40:16 +00001132 if (gen_hs) {
1133 pwarning(1, "-hs is deprecated. Use --gen hs");
1134 generator_strings.push_back("hs");
1135 }
David Reisse1404d22008-02-27 01:55:05 +00001136
Mark Sleef0712dc2006-10-25 19:03:57 +00001137 // You gotta generate something!
David Reissaf3ab262008-03-27 21:40:16 +00001138 if (!gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_erl && !gen_cocoa && !gen_st && !gen_csharp && generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001139 fprintf(stderr, "!!! No output language(s) specified\n\n");
1140 usage();
1141 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001142
1143 // Real-pathify it
1144 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +00001145 if (saferealpath(argv[i], rp) == NULL) {
1146 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001147 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001148 string input_file(rp);
1149
Mark Sleef5377b32006-10-10 01:42:59 +00001150 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001151 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001152 if (out_path.size()) {
1153 program->set_out_path(out_path);
1154 }
kholst76f2c882008-01-16 02:47:41 +00001155
David Reiss4b6a3c72008-02-27 22:28:12 +00001156 // Compute the cpp include prefix.
1157 // infer this from the filename passed in
1158 string input_filename = argv[i];
1159 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001160
David Reiss4b6a3c72008-02-27 22:28:12 +00001161 string::size_type last_slash = string::npos;
1162 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1163 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001164 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001165
David Reiss4b6a3c72008-02-27 22:28:12 +00001166 program->set_include_prefix(include_prefix);
1167
Mark Sleef0712dc2006-10-25 19:03:57 +00001168 // Initialize global types
1169 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1170 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001171 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1172 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001173 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1174 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001175 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1176 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1177 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1178 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1179 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1180 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001181
Mark Sleef5377b32006-10-10 01:42:59 +00001182 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001183 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001184
David Reiss9cc2c132008-02-27 01:54:47 +00001185 // The current path is not really relevant when we are doing generation.
1186 // Reset the variable to make warning messages clearer.
1187 g_curpath = "generation";
1188 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1189 // That is what shows up during argument parsing.
1190 yylineno = 1;
1191
Mark Sleef0712dc2006-10-25 19:03:57 +00001192 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001193 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001194
Mark Sleef0712dc2006-10-25 19:03:57 +00001195 // Clean up. Who am I kidding... this program probably orphans heap memory
1196 // all over the place, but who cares because it is about to exit and it is
1197 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001198
Mark Sleef0712dc2006-10-25 19:03:57 +00001199 delete program;
1200 delete g_type_void;
1201 delete g_type_string;
1202 delete g_type_bool;
1203 delete g_type_byte;
1204 delete g_type_i16;
1205 delete g_type_i32;
1206 delete g_type_i64;
1207 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001208
1209 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001210 return 0;
1211}