blob: 9f3eb7312b63cb27b54fdad9376dd13cefd8207e [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee31985722006-05-24 21:45:31 +00007/**
8 * thrift - a lightweight cross-language rpc/serialization tool
9 *
10 * This file contains the main compiler engine for Thrift, which invokes the
11 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +000012 * code for each language lives in a file by the language name under the
13 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +000014 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <stdarg.h>
21#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000022#include <sys/types.h>
23#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000024#include <errno.h>
Mark Slee31985722006-05-24 21:45:31 +000025
David Reiss204420f2008-01-11 20:59:03 +000026#ifdef MINGW
27# include <windows.h> /* for GetFullPathName */
28# include <limits.h>
29#endif
30
Mark Sleef0712dc2006-10-25 19:03:57 +000031// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000032#include "globals.h"
33
34#include "main.h"
35#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000036#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000037#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000038#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000039#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000040#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000041#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000042#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000043#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000044#include "generate/t_erl_generator.h"
Christopher Pirob97b89d2007-09-18 00:07:42 +000045#include "generate/t_ocaml_generator.h"
iproctorff8eb922007-07-25 19:06:13 +000046#include "generate/t_hs_generator.h"
Mark Slee7e9eea42007-09-10 21:00:23 +000047#include "generate/t_cocoa_generator.h"
Mark Sleeefd37f12007-11-20 05:13:09 +000048#include "generate/t_st_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000049#include "generate/t_csharp_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000050
51using namespace std;
52
Mark Sleef5377b32006-10-10 01:42:59 +000053/**
54 * Global program tree
55 */
Mark Slee31985722006-05-24 21:45:31 +000056t_program* g_program;
57
Mark Sleef5377b32006-10-10 01:42:59 +000058/**
Mark Sleef0712dc2006-10-25 19:03:57 +000059 * Global types
60 */
61
62t_type* g_type_void;
63t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000064t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000065t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000066t_type* g_type_bool;
67t_type* g_type_byte;
68t_type* g_type_i16;
69t_type* g_type_i32;
70t_type* g_type_i64;
71t_type* g_type_double;
72
73/**
74 * Global scope
75 */
76t_scope* g_scope;
77
78/**
79 * Parent scope to also parse types
80 */
81t_scope* g_parent_scope;
82
83/**
84 * Prefix for putting types in parent scope
85 */
86string g_parent_prefix;
87
88/**
89 * Parsing pass
90 */
91PARSE_MODE g_parse_mode;
92
93/**
94 * Current directory of file being parsed
95 */
96string g_curdir;
97
98/**
99 * Current file being parsed
100 */
101string g_curpath;
102
103/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000104 * Search path for inclusions
105 */
Mark Slee2329a832006-11-09 00:23:30 +0000106vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000107
108/**
kholst76f2c882008-01-16 02:47:41 +0000109 * Should C++ include statements use path prefixes for other thrift-generated
110 * header files
111 */
112bool g_cpp_use_include_prefix = false;
113
114/**
Mark Sleef5377b32006-10-10 01:42:59 +0000115 * Global debug state
116 */
Mark Slee31985722006-05-24 21:45:31 +0000117int g_debug = 0;
118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000120 * Warning level
121 */
122int g_warn = 1;
123
124/**
125 * Verbose output
126 */
127int g_verbose = 0;
128
129/**
Mark Sleef5377b32006-10-10 01:42:59 +0000130 * Global time string
131 */
Mark Slee31985722006-05-24 21:45:31 +0000132char* g_time_str;
133
Mark Slee31985722006-05-24 21:45:31 +0000134/**
David Reisscbd4bac2007-08-14 17:12:33 +0000135 * The last parsed doctext comment.
136 */
137char* g_doctext;
138
139/**
140 * The location of the last parsed doctext comment.
141 */
142int g_doctext_lineno;
143
144/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000145 * Flags to control code generation
146 */
147bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000148bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000149bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000150bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000151bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000152bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000153bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000154bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000155bool gen_php = false;
156bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000157bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000158bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000159bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000160bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000161bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000162bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000163bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000164bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000165bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000166bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000167bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000168bool gen_recurse = false;
169
170/**
David Reiss204420f2008-01-11 20:59:03 +0000171 * MinGW doesn't have realpath, so use fallback implementation in that case,
172 * otherwise this just calls through to realpath
173 */
174char *saferealpath(const char *path, char *resolved_path) {
175#ifdef MINGW
176 char buf[MAX_PATH];
177 char* basename;
178 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
179 if (len == 0 || len > MAX_PATH - 1){
180 strcpy(resolved_path, path);
181 } else {
182 CharLowerBuff(buf, len);
183 strcpy(resolved_path, buf);
184 }
185 return resolved_path;
186#else
187 return realpath(path, resolved_path);
188#endif
189}
190
191
192/**
Mark Slee31985722006-05-24 21:45:31 +0000193 * Report an error to the user. This is called yyerror for historical
194 * reasons (lex and yacc expect the error reporting routine to be called
195 * this). Call this function to report any errors to the user.
196 * yyerror takes printf style arguments.
197 *
198 * @param fmt C format string followed by additional arguments
199 */
200void yyerror(char* fmt, ...) {
201 va_list args;
202 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000203 "[ERROR:%s:%d] (last token was '%s')\n",
204 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000205 yylineno,
206 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000207
208 va_start(args, fmt);
209 vfprintf(stderr, fmt, args);
210 va_end(args);
211
212 fprintf(stderr, "\n");
213}
214
215/**
216 * Prints a debug message from the parser.
217 *
218 * @param fmt C format string followed by additional arguments
219 */
220void pdebug(char* fmt, ...) {
221 if (g_debug == 0) {
222 return;
223 }
224 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000225 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000226 va_start(args, fmt);
227 vprintf(fmt, args);
228 va_end(args);
229 printf("\n");
230}
231
232/**
233 * Prints a verbose output mode message
234 *
235 * @param fmt C format string followed by additional arguments
236 */
237void pverbose(char* fmt, ...) {
238 if (g_verbose == 0) {
239 return;
240 }
241 va_list args;
242 va_start(args, fmt);
243 vprintf(fmt, args);
244 va_end(args);
245}
246
247/**
248 * Prints a warning message
249 *
250 * @param fmt C format string followed by additional arguments
251 */
252void pwarning(int level, char* fmt, ...) {
253 if (g_warn < level) {
254 return;
255 }
256 va_list args;
257 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000258 va_start(args, fmt);
259 vprintf(fmt, args);
260 va_end(args);
261 printf("\n");
262}
263
264/**
265 * Prints a failure message and exits
266 *
267 * @param fmt C format string followed by additional arguments
268 */
Mark Slee30152872006-11-28 01:24:07 +0000269void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000270 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000272 va_start(args, fmt);
273 vfprintf(stderr, fmt, args);
274 va_end(args);
275 printf("\n");
276 exit(1);
277}
278
279/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000280 * Converts a string filename into a thrift program name
281 */
282string program_name(string filename) {
283 string::size_type slash = filename.rfind("/");
284 if (slash != string::npos) {
285 filename = filename.substr(slash+1);
286 }
287 string::size_type dot = filename.rfind(".");
288 if (dot != string::npos) {
289 filename = filename.substr(0, dot);
290 }
291 return filename;
292}
293
294/**
295 * Gets the directory path of a filename
296 */
297string directory_name(string filename) {
298 string::size_type slash = filename.rfind("/");
299 // No slash, just use the current directory
300 if (slash == string::npos) {
301 return ".";
302 }
303 return filename.substr(0, slash);
304}
305
306/**
307 * Finds the appropriate file path for the given filename
308 */
309string include_file(string filename) {
310 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000311 if (filename[0] == '/') {
312 // Realpath!
313 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000314 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000315 pwarning(0, "Cannot open include file %s\n", filename.c_str());
316 return std::string();
317 }
Mark Slee2c44d202007-05-16 02:18:07 +0000318
319 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000320 struct stat finfo;
321 if (stat(rp, &finfo) == 0) {
322 return rp;
323 }
324 } else { // relative path, start searching
325 // new search path with current dir global
326 vector<string> sp = g_incl_searchpath;
327 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000328
Martin Kraemer32c66e12006-11-09 00:06:36 +0000329 // iterate through paths
330 vector<string>::iterator it;
331 for (it = sp.begin(); it != sp.end(); it++) {
332 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000333
Martin Kraemer32c66e12006-11-09 00:06:36 +0000334 // Realpath!
335 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000336 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000337 continue;
338 }
Mark Slee2c44d202007-05-16 02:18:07 +0000339
Martin Kraemer32c66e12006-11-09 00:06:36 +0000340 // Stat this files
341 struct stat finfo;
342 if (stat(rp, &finfo) == 0) {
343 return rp;
344 }
345 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000346 }
Mark Slee2c44d202007-05-16 02:18:07 +0000347
Mark Sleef0712dc2006-10-25 19:03:57 +0000348 // Uh oh
349 pwarning(0, "Could not find include file %s\n", filename.c_str());
350 return std::string();
351}
352
353/**
David Reisscbd4bac2007-08-14 17:12:33 +0000354 * Clears any previously stored doctext string.
355 * Also prints a warning if we are discarding information.
356 */
357void clear_doctext() {
358 if (g_doctext != NULL) {
359 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
360 }
361 free(g_doctext);
362 g_doctext = NULL;
363}
364
365/**
David Reiss1ac05802007-07-30 22:00:27 +0000366 * Cleans up text commonly found in doxygen-like comments
367 *
368 * Warning: if you mix tabs and spaces in a non-uniform way,
369 * you will get what you deserve.
370 */
371char* clean_up_doctext(char* doctext) {
372 // Convert to C++ string, and remove Windows's carriage returns.
373 string docstring = doctext;
374 docstring.erase(
375 remove(docstring.begin(), docstring.end(), '\r'),
376 docstring.end());
377
378 // Separate into lines.
379 vector<string> lines;
380 string::size_type pos = string::npos;
381 string::size_type last;
382 while (true) {
383 last = (pos == string::npos) ? 0 : pos+1;
384 pos = docstring.find('\n', last);
385 if (pos == string::npos) {
386 // First bit of cleaning. If the last line is only whitespace, drop it.
387 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
388 if (nonwhite != string::npos) {
389 lines.push_back(docstring.substr(last));
390 }
391 break;
392 }
393 lines.push_back(docstring.substr(last, pos-last));
394 }
395
396 // A very profound docstring.
397 if (lines.empty()) {
398 return NULL;
399 }
400
401 // Clear leading whitespace from the first line.
402 pos = lines.front().find_first_not_of(" \t");
403 lines.front().erase(0, pos);
404
405 // If every nonblank line after the first has the same number of spaces/tabs,
406 // then a star, remove them.
407 bool have_prefix = true;
408 bool found_prefix = false;
409 string::size_type prefix_len = 0;
410 vector<string>::iterator l_iter;
411 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
412 if (l_iter->empty()) {
413 continue;
414 }
415
416 pos = l_iter->find_first_not_of(" \t");
417 if (!found_prefix) {
418 if (pos != string::npos) {
419 if (l_iter->at(pos) == '*') {
420 found_prefix = true;
421 prefix_len = pos;
422 } else {
423 have_prefix = false;
424 break;
425 }
426 } else {
427 // Whitespace-only line. Truncate it.
428 l_iter->clear();
429 }
430 } else if (l_iter->size() > pos
431 && l_iter->at(pos) == '*'
432 && pos == prefix_len) {
433 // Business as usual.
434 } else if (pos == string::npos) {
435 // Whitespace-only line. Let's truncate it for them.
436 l_iter->clear();
437 } else {
438 // The pattern has been broken.
439 have_prefix = false;
440 break;
441 }
442 }
443
444 // If our prefix survived, delete it from every line.
445 if (have_prefix) {
446 // Get the star too.
447 prefix_len++;
448 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
449 l_iter->erase(0, prefix_len);
450 }
451 }
452
453 // Now delete the minimum amount of leading whitespace from each line.
454 prefix_len = string::npos;
455 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
456 if (l_iter->empty()) {
457 continue;
458 }
459 pos = l_iter->find_first_not_of(" \t");
460 if (pos != string::npos
461 && (prefix_len == string::npos || pos < prefix_len)) {
462 prefix_len = pos;
463 }
464 }
465
466 // If our prefix survived, delete it from every line.
467 if (prefix_len != string::npos) {
468 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
469 l_iter->erase(0, prefix_len);
470 }
471 }
472
473 // Remove trailing whitespace from every line.
474 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
475 pos = l_iter->find_last_not_of(" \t");
476 if (pos != string::npos && pos != l_iter->length()-1) {
477 l_iter->erase(pos+1);
478 }
479 }
480
481 // If the first line is empty, remove it.
482 // Don't do this earlier because a lot of steps skip the first line.
483 if (lines.front().empty()) {
484 lines.erase(lines.begin());
485 }
486
487 // Now rejoin the lines and copy them back into doctext.
488 docstring.clear();
489 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
490 docstring += *l_iter;
491 docstring += '\n';
492 }
493
494 assert(docstring.length() <= strlen(doctext));
495 strcpy(doctext, docstring.c_str());
496 return doctext;
497}
498
499/** Set to true to debug docstring parsing */
500static bool dump_docs = false;
501
502/**
503 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000504 * Only works for top-level definitions and the whole program doc
505 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000506 */
507void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000508 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000509 if (!progdoc.empty()) {
510 printf("Whole program doc:\n%s\n", progdoc.c_str());
511 }
David Reiss1ac05802007-07-30 22:00:27 +0000512 const vector<t_typedef*>& typedefs = program->get_typedefs();
513 vector<t_typedef*>::const_iterator t_iter;
514 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
515 t_typedef* td = *t_iter;
516 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000517 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
518 }
519 }
520 const vector<t_enum*>& enums = program->get_enums();
521 vector<t_enum*>::const_iterator e_iter;
522 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
523 t_enum* en = *e_iter;
524 if (en->has_doc()) {
525 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
526 }
527 }
528 const vector<t_const*>& consts = program->get_consts();
529 vector<t_const*>::const_iterator c_iter;
530 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
531 t_const* co = *c_iter;
532 if (co->has_doc()) {
533 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
534 }
535 }
536 const vector<t_struct*>& structs = program->get_structs();
537 vector<t_struct*>::const_iterator s_iter;
538 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
539 t_struct* st = *s_iter;
540 if (st->has_doc()) {
541 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
542 }
543 }
544 const vector<t_struct*>& xceptions = program->get_xceptions();
545 vector<t_struct*>::const_iterator x_iter;
546 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
547 t_struct* xn = *x_iter;
548 if (xn->has_doc()) {
549 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
550 }
551 }
552 const vector<t_service*>& services = program->get_services();
553 vector<t_service*>::const_iterator v_iter;
554 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
555 t_service* sv = *v_iter;
556 if (sv->has_doc()) {
557 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000558 }
559 }
560}
561
562/**
David Reiss18bf22d2007-08-28 20:49:17 +0000563 * Call generate_fingerprint for every structure.
564 */
565void generate_all_fingerprints(t_program* program) {
566 const vector<t_struct*>& structs = program->get_structs();
567 vector<t_struct*>::const_iterator s_iter;
568 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
569 t_struct* st = *s_iter;
570 st->generate_fingerprint();
571 }
572
David Reissd779cbe2007-08-31 01:42:55 +0000573 const vector<t_struct*>& xceptions = program->get_xceptions();
574 vector<t_struct*>::const_iterator x_iter;
575 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
576 t_struct* st = *x_iter;
577 st->generate_fingerprint();
578 }
579
David Reiss47557bc2007-09-04 21:31:04 +0000580 g_type_void->generate_fingerprint();
581
David Reiss18bf22d2007-08-28 20:49:17 +0000582 // If you want to generate fingerprints for implicit structures, start here.
583 /*
584 const vector<t_service*>& services = program->get_services();
585 vector<t_service*>::const_iterator v_iter;
586 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
587 t_service* sv = *v_iter;
588 }
589 */
590}
591
592/**
Mark Slee31985722006-05-24 21:45:31 +0000593 * Diplays the usage message and then exits with an error code.
594 */
595void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000596 fprintf(stderr, "Usage: thrift [options] file\n");
597 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000598 fprintf(stderr, " -cpp Generate C++ output files\n");
599 fprintf(stderr, " -java Generate Java output files\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000600 fprintf(stderr, " -javabean Generate Java bean-style output files\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");
Christopher Pirob97b89d2007-09-18 00:07:42 +0000613 fprintf(stderr, " -ocaml Generate OCaml output files\n");
iproctorff8eb922007-07-25 19:06:13 +0000614 fprintf(stderr, " -hs Generate Haskell output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000615 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
David Reiss7f42bcf2008-01-11 20:59:12 +0000616 fprintf(stderr, " -csharp Generate C# output files\n");
Mark Sleeefd37f12007-11-20 05:13:09 +0000617 fprintf(stderr, " -st Generate Squeak/Smalltalk output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000618 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
619 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000620 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000621 fprintf(stderr, " searched for include directives\n");
kholst76f2c882008-01-16 02:47:41 +0000622 fprintf(stderr, " -cpp_use_include_prefix\n");
623 fprintf(stderr, " Make C++ include statements use path prefixes\n");
David Reissd779cbe2007-08-31 01:42:55 +0000624 fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n");
625 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000626 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
627 fprintf(stderr, " -strict Strict compiler warnings on\n");
628 fprintf(stderr, " -v[erbose] Verbose mode\n");
629 fprintf(stderr, " -r[ecurse] Also generate included files\n");
630 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000631 exit(1);
632}
633
634/**
Mark Slee30152872006-11-28 01:24:07 +0000635 * You know, when I started working on Thrift I really thought it wasn't going
636 * to become a programming language because it was just a generator and it
637 * wouldn't need runtime type information and all that jazz. But then we
638 * decided to add constants, and all of a sudden that means runtime type
639 * validation and inference, except the "runtime" is the code generator
640 * runtime. Shit. I've been had.
641 */
642void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
643 if (type->is_void()) {
644 throw "type error: cannot declare a void const: " + name;
645 }
646
647 if (type->is_base_type()) {
648 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
649 switch (tbase) {
650 case t_base_type::TYPE_STRING:
651 if (value->get_type() != t_const_value::CV_STRING) {
652 throw "type error: const \"" + name + "\" was declared as string";
653 }
654 break;
655 case t_base_type::TYPE_BOOL:
656 if (value->get_type() != t_const_value::CV_INTEGER) {
657 throw "type error: const \"" + name + "\" was declared as bool";
658 }
659 break;
660 case t_base_type::TYPE_BYTE:
661 if (value->get_type() != t_const_value::CV_INTEGER) {
662 throw "type error: const \"" + name + "\" was declared as byte";
663 }
664 break;
665 case t_base_type::TYPE_I16:
666 if (value->get_type() != t_const_value::CV_INTEGER) {
667 throw "type error: const \"" + name + "\" was declared as i16";
668 }
669 break;
670 case t_base_type::TYPE_I32:
671 if (value->get_type() != t_const_value::CV_INTEGER) {
672 throw "type error: const \"" + name + "\" was declared as i32";
673 }
674 break;
675 case t_base_type::TYPE_I64:
676 if (value->get_type() != t_const_value::CV_INTEGER) {
677 throw "type error: const \"" + name + "\" was declared as i64";
678 }
679 break;
680 case t_base_type::TYPE_DOUBLE:
681 if (value->get_type() != t_const_value::CV_INTEGER &&
682 value->get_type() != t_const_value::CV_DOUBLE) {
683 throw "type error: const \"" + name + "\" was declared as double";
684 }
685 break;
686 default:
David Reissdd7796f2007-08-28 21:09:06 +0000687 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000688 }
689 } else if (type->is_enum()) {
690 if (value->get_type() != t_const_value::CV_INTEGER) {
691 throw "type error: const \"" + name + "\" was declared as enum";
692 }
693 } else if (type->is_struct() || type->is_xception()) {
694 if (value->get_type() != t_const_value::CV_MAP) {
695 throw "type error: const \"" + name + "\" was declared as struct/xception";
696 }
697 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
698 vector<t_field*>::const_iterator f_iter;
699
700 const map<t_const_value*, t_const_value*>& val = value->get_map();
701 map<t_const_value*, t_const_value*>::const_iterator v_iter;
702 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
703 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
704 throw "type error: " + name + " struct key must be string";
705 }
706 t_type* field_type = NULL;
707 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
708 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
709 field_type = (*f_iter)->get_type();
710 }
711 }
712 if (field_type == NULL) {
713 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
714 }
715
716 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
717 }
718 } else if (type->is_map()) {
719 t_type* k_type = ((t_map*)type)->get_key_type();
720 t_type* v_type = ((t_map*)type)->get_val_type();
721 const map<t_const_value*, t_const_value*>& val = value->get_map();
722 map<t_const_value*, t_const_value*>::const_iterator v_iter;
723 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
724 validate_const_rec(name + "<key>", k_type, v_iter->first);
725 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000726 }
Mark Slee30152872006-11-28 01:24:07 +0000727 } else if (type->is_list() || type->is_set()) {
728 t_type* e_type;
729 if (type->is_list()) {
730 e_type = ((t_list*)type)->get_elem_type();
731 } else {
732 e_type = ((t_set*)type)->get_elem_type();
733 }
734 const vector<t_const_value*>& val = value->get_list();
735 vector<t_const_value*>::const_iterator v_iter;
736 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
737 validate_const_rec(name + "<elem>", e_type, *v_iter);
738 }
739 }
740}
741
742/**
743 * Check the type of the parsed const information against its declared type
744 */
745void validate_const_type(t_const* c) {
746 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
747}
748
749/**
Mark Slee7ff32452007-02-01 05:26:18 +0000750 * Check the type of a default value assigned to a field.
751 */
752void validate_field_value(t_field* field, t_const_value* cv) {
753 validate_const_rec(field->get_name(), field->get_type(), cv);
754}
755
756/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000757 * Check that all the elements of a throws block are actually exceptions.
758 */
759bool validate_throws(t_struct* throws) {
760 const vector<t_field*>& members = throws->get_members();
761 vector<t_field*>::const_iterator m_iter;
762 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
763 if (!(*m_iter)->get_type()->is_xception()) {
764 return false;
765 }
766 }
767 return true;
768}
769
770/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000771 * Parses a program
772 */
Mark Slee2c44d202007-05-16 02:18:07 +0000773void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000774 // Get scope file path
775 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000776
Mark Sleef0712dc2006-10-25 19:03:57 +0000777 // Set current dir global, which is used in the include_file function
778 g_curdir = directory_name(path);
779 g_curpath = path;
780
781 // Open the file
782 yyin = fopen(path.c_str(), "r");
783 if (yyin == 0) {
784 failure("Could not open input file: \"%s\"", path.c_str());
785 }
786
787 // Create new scope and scan for includes
788 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000789 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000790 g_program = program;
791 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000792 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000793 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000794 if (yyparse() != 0) {
795 failure("Parser error during include pass.");
796 }
797 } catch (string x) {
798 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 }
800 fclose(yyin);
801
802 // Recursively parse all the include programs
803 vector<t_program*>& includes = program->get_includes();
804 vector<t_program*>::iterator iter;
805 for (iter = includes.begin(); iter != includes.end(); ++iter) {
806 parse(*iter, program);
807 }
808
David Reiss204420f2008-01-11 20:59:03 +0000809 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000810 g_parse_mode = PROGRAM;
811 g_program = program;
812 g_scope = program->scope();
813 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
814 g_parent_prefix = program->get_name() + ".";
815 g_curpath = path;
816 yyin = fopen(path.c_str(), "r");
817 if (yyin == 0) {
818 failure("Could not open input file: \"%s\"", path.c_str());
819 }
820 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000821 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000822 try {
823 if (yyparse() != 0) {
824 failure("Parser error during types pass.");
825 }
826 } catch (string x) {
827 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000828 }
829 fclose(yyin);
830}
831
832/**
833 * Generate code
834 */
835void generate(t_program* program) {
836 // Oooohh, recursive code generation, hot!!
837 if (gen_recurse) {
838 const vector<t_program*>& includes = program->get_includes();
839 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000840 // Propogate output path from parent to child programs
841 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000842
Mark Sleef0712dc2006-10-25 19:03:57 +0000843 generate(includes[i]);
844 }
845 }
846
847 // Generate code!
848 try {
849 pverbose("Program: %s\n", program->get_path().c_str());
850
David Reiss18bf22d2007-08-28 20:49:17 +0000851 // Compute fingerprints.
852 generate_all_fingerprints(program);
853
Mark Sleef0712dc2006-10-25 19:03:57 +0000854 if (gen_cpp) {
855 pverbose("Generating C++\n");
David Reissd779cbe2007-08-31 01:42:55 +0000856 t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense);
kholst76f2c882008-01-16 02:47:41 +0000857 cpp->set_use_include_prefix(g_cpp_use_include_prefix);
Mark Sleef0712dc2006-10-25 19:03:57 +0000858 cpp->generate_program();
859 delete cpp;
860 }
861
862 if (gen_java) {
863 pverbose("Generating Java\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000864 t_java_generator* java = new t_java_generator(program, false);
865 java->generate_program();
866 delete java;
867 }
868
869 if (gen_javabean) {
870 pverbose("Generating Java Beans\n");
871 t_java_generator* java = new t_java_generator(program, true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000872 java->generate_program();
873 delete java;
874 }
875
876 if (gen_php) {
877 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000878 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 +0000879 php->generate_program();
880 delete php;
881 }
882
883 if (gen_phpi) {
884 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000885 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000886 phpi->generate_program();
887 delete phpi;
888 }
889
890 if (gen_py) {
891 pverbose("Generating Python\n");
David Reiss6495adb2007-12-18 03:37:30 +0000892 t_py_generator* py = new t_py_generator(program, gen_py_newstyle);
Mark Sleef0712dc2006-10-25 19:03:57 +0000893 py->generate_program();
894 delete py;
895 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000896
Mark Slee6d7d5952007-01-27 01:44:22 +0000897 if (gen_rb) {
898 pverbose("Generating Ruby\n");
899 t_rb_generator* rb = new t_rb_generator(program);
900 rb->generate_program();
901 delete rb;
902 }
903
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000904 if (gen_xsd) {
905 pverbose("Generating XSD\n");
906 t_xsd_generator* xsd = new t_xsd_generator(program);
907 xsd->generate_program();
908 delete xsd;
909 }
910
Mark Slee2c44d202007-05-16 02:18:07 +0000911 if (gen_perl) {
912 pverbose("Generating PERL\n");
913 t_perl_generator* perl = new t_perl_generator(program);
914 perl->generate_program();
915 delete perl;
916 }
917
Christopher Pirob97b89d2007-09-18 00:07:42 +0000918 if (gen_erl) {
919 pverbose("Generating Erlang\n");
920 t_erl_generator* erl = new t_erl_generator(program);
921 erl->generate_program();
922 delete erl;
923 }
924
iproctor9a41a0c2007-07-16 21:59:24 +0000925 if (gen_ocaml) {
926 pverbose("Generating OCaml\n");
927 t_ocaml_generator* ocaml = new t_ocaml_generator(program);
928 ocaml->generate_program();
929 delete ocaml;
930 }
931
iproctorff8eb922007-07-25 19:06:13 +0000932 if (gen_hs) {
933 pverbose("Generating Haskell\n");
934 t_hs_generator* hs = new t_hs_generator(program);
935 hs->generate_program();
936 delete hs;
937 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000938
Mark Slee7e9eea42007-09-10 21:00:23 +0000939 if (gen_cocoa) {
940 pverbose("Generating Cocoa/Objective-C\n");
941 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
942 cocoa->generate_program();
943 delete cocoa;
944 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000945
Mark Sleeefd37f12007-11-20 05:13:09 +0000946 if (gen_st) {
947 pverbose("Generating Smalltalk/Squeak\n");
948 t_st_generator* st = new t_st_generator(program);
949 st->generate_program();
950 delete st;
951 }
952
David Reiss7f42bcf2008-01-11 20:59:12 +0000953 if (gen_csharp) {
954 pverbose("Generating C#\n");
955 t_csharp_generator* csharp = new t_csharp_generator(program);
956 csharp->generate_program();
957 delete csharp;
958 }
kholst76f2c882008-01-16 02:47:41 +0000959
David Reiss1ac05802007-07-30 22:00:27 +0000960 if (dump_docs) {
961 dump_docstrings(program);
962 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000963 } catch (string s) {
964 printf("Error: %s\n", s.c_str());
965 } catch (const char* exc) {
966 printf("Error: %s\n", exc);
967 }
968
969}
970
971/**
Mark Sleef5377b32006-10-10 01:42:59 +0000972 * Parse it up.. then spit it back out, in pretty much every language. Alright
973 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000974 */
975int main(int argc, char** argv) {
976 int i;
dweatherford65b70752007-10-31 02:18:14 +0000977 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000978
Mark Sleeb15a68b2006-06-07 06:46:24 +0000979 // Setup time string
980 time_t now = time(NULL);
981 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000982
Mark Sleef0712dc2006-10-25 19:03:57 +0000983 // Check for necessary arguments, you gotta have at least a filename and
984 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000985 if (argc < 2) {
986 usage();
987 }
Mark Slee31985722006-05-24 21:45:31 +0000988
Mark Sleef5377b32006-10-10 01:42:59 +0000989 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000990 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000991 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000992
Mark Sleefdbee812006-09-27 18:50:48 +0000993 arg = strtok(argv[i], " ");
994 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000995 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000996 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000997 ++arg;
998 }
999
1000 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001001 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001002 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001003 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001004 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001005 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001006 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001007 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001008 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001009 gen_recurse = true;
David Reissd779cbe2007-08-31 01:42:55 +00001010 } else if (strcmp(arg, "-dense") == 0) {
1011 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +00001012 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001013 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +00001014 } else if (strcmp(arg, "-javabean") == 0) {
1015 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +00001016 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001017 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001018 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001019 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001020 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001021 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001022 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001023 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001024 gen_phps = true;
1025 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001026 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001027 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001028 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001029 gen_php = true;
1030 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001031 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001032 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001033 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001034 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001035 } else if (strcmp(arg, "-rest") == 0) {
1036 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001037 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001038 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001039 } else if (strcmp(arg, "-pyns") == 0) {
1040 gen_py = true;
1041 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001042 } else if (strcmp(arg, "-rb") == 0) {
1043 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001044 } else if (strcmp(arg, "-xsd") == 0) {
1045 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001046 } else if (strcmp(arg, "-perl") == 0) {
1047 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001048 } else if (strcmp(arg, "-erl") == 0) {
1049 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001050 } else if (strcmp(arg, "-ocaml") == 0) {
1051 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001052 } else if (strcmp(arg, "-hs") == 0) {
1053 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001054 } else if (strcmp(arg, "-cocoa") == 0) {
1055 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001056 } else if (strcmp(arg, "-st") == 0) {
1057 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001058 } else if (strcmp(arg, "-csharp") == 0) {
1059 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001060 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1061 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001062 } else if (strcmp(arg, "-I") == 0) {
1063 // An argument of "-I\ asdf" is invalid and has unknown results
1064 arg = argv[++i];
1065
1066 if (arg == NULL) {
1067 fprintf(stderr, "!!! Missing Include directory");
1068 usage();
1069 }
1070 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001071 } else if (strcmp(arg, "-o") == 0) {
1072 arg = argv[++i];
1073 if (arg == NULL) {
1074 fprintf(stderr, "-o: missing output directory");
1075 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001076 }
dweatherford65b70752007-10-31 02:18:14 +00001077 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001078
1079#ifdef MINGW
1080 //strip out trailing \ on Windows
1081 int last = out_path.length()-1;
1082 if (out_path[last] == '\\')
1083 {
1084 out_path.erase(last);
1085 }
1086#endif
1087
dweatherford65b70752007-10-31 02:18:14 +00001088 struct stat sb;
1089 if (stat(out_path.c_str(), &sb) < 0) {
1090 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1091 return -1;
1092 }
1093 if (! S_ISDIR(sb.st_mode)) {
1094 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1095 return -1;
1096 }
Mark Sleefdbee812006-09-27 18:50:48 +00001097 } else {
1098 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1099 usage();
1100 }
1101
1102 // Tokenize more
1103 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001104 }
1105 }
Mark Slee2c44d202007-05-16 02:18:07 +00001106
Mark Sleef0712dc2006-10-25 19:03:57 +00001107 // You gotta generate something!
David Reiss7f42bcf2008-01-11 20:59:12 +00001108 if (!gen_cpp && !gen_java && !gen_javabean && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_erl && !gen_ocaml && !gen_hs && !gen_cocoa && !gen_st && !gen_csharp) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001109 fprintf(stderr, "!!! No output language(s) specified\n\n");
1110 usage();
1111 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001112
1113 // Real-pathify it
1114 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +00001115 if (saferealpath(argv[i], rp) == NULL) {
1116 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001117 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001118 string input_file(rp);
1119
Mark Sleef5377b32006-10-10 01:42:59 +00001120 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001121 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001122 if (out_path.size()) {
1123 program->set_out_path(out_path);
1124 }
kholst76f2c882008-01-16 02:47:41 +00001125 if (g_cpp_use_include_prefix) {
1126 // infer this from the filename passed in
1127 string input_filename = argv[i];
1128 string include_prefix;
1129
1130 string::size_type last_slash = string::npos;
1131 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1132 include_prefix = input_filename.substr(0, last_slash);
1133 }
1134
1135 program->set_include_prefix(include_prefix);
1136 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001137
1138 // Initialize global types
1139 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1140 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001141 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1142 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001143 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1144 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001145 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1146 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1147 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1148 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1149 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1150 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001151
Mark Sleef5377b32006-10-10 01:42:59 +00001152 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001153 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001154
Mark Sleef0712dc2006-10-25 19:03:57 +00001155 // Generate it!
1156 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001157
Mark Sleef0712dc2006-10-25 19:03:57 +00001158 // Clean up. Who am I kidding... this program probably orphans heap memory
1159 // all over the place, but who cares because it is about to exit and it is
1160 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001161
Mark Sleef0712dc2006-10-25 19:03:57 +00001162 delete program;
1163 delete g_type_void;
1164 delete g_type_string;
1165 delete g_type_bool;
1166 delete g_type_byte;
1167 delete g_type_i16;
1168 delete g_type_i32;
1169 delete g_type_i64;
1170 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001171
1172 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001173 return 0;
1174}