blob: b16e6ca53b9976f218158df5ccc934d0344363a5 [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 *
Mark Slee31985722006-05-24 21:45:31 +000015 */
16
David Reissf10984b2008-03-27 21:39:52 +000017#include <cassert>
Mark Slee31985722006-05-24 21:45:31 +000018#include <stdlib.h>
19#include <stdio.h>
20#include <stdarg.h>
21#include <string>
David Reiss739cbe22008-04-15 05:44:00 +000022#include <algorithm>
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>
David Reissab55ed52008-06-11 01:17:00 +000026#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000027
David Reiss204420f2008-01-11 20:59:03 +000028#ifdef MINGW
29# include <windows.h> /* for GetFullPathName */
David Reiss204420f2008-01-11 20:59:03 +000030#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"
David Reissbbbbe882009-02-17 20:27:48 +000038#include "generate/t_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000039
David Reissdd08f6d2008-06-30 20:24:24 +000040#include "version.h"
41
Mark Slee31985722006-05-24 21:45:31 +000042using namespace std;
43
Mark Sleef5377b32006-10-10 01:42:59 +000044/**
45 * Global program tree
46 */
Mark Slee31985722006-05-24 21:45:31 +000047t_program* g_program;
48
Mark Sleef5377b32006-10-10 01:42:59 +000049/**
Mark Sleef0712dc2006-10-25 19:03:57 +000050 * Global types
51 */
52
53t_type* g_type_void;
54t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000055t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000056t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000057t_type* g_type_bool;
58t_type* g_type_byte;
59t_type* g_type_i16;
60t_type* g_type_i32;
61t_type* g_type_i64;
62t_type* g_type_double;
63
64/**
65 * Global scope
66 */
67t_scope* g_scope;
68
69/**
70 * Parent scope to also parse types
71 */
72t_scope* g_parent_scope;
73
74/**
75 * Prefix for putting types in parent scope
76 */
77string g_parent_prefix;
78
79/**
80 * Parsing pass
81 */
82PARSE_MODE g_parse_mode;
83
84/**
85 * Current directory of file being parsed
86 */
87string g_curdir;
88
89/**
90 * Current file being parsed
91 */
92string g_curpath;
93
94/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000095 * Search path for inclusions
96 */
Mark Slee2329a832006-11-09 00:23:30 +000097vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000098
99/**
kholst76f2c882008-01-16 02:47:41 +0000100 * Should C++ include statements use path prefixes for other thrift-generated
101 * header files
102 */
103bool g_cpp_use_include_prefix = false;
104
105/**
Mark Sleef5377b32006-10-10 01:42:59 +0000106 * Global debug state
107 */
Mark Slee31985722006-05-24 21:45:31 +0000108int g_debug = 0;
109
Mark Sleef5377b32006-10-10 01:42:59 +0000110/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000111 * Warning level
112 */
113int g_warn = 1;
114
115/**
116 * Verbose output
117 */
118int g_verbose = 0;
119
120/**
Mark Sleef5377b32006-10-10 01:42:59 +0000121 * Global time string
122 */
Mark Slee31985722006-05-24 21:45:31 +0000123char* g_time_str;
124
Mark Slee31985722006-05-24 21:45:31 +0000125/**
David Reisscbd4bac2007-08-14 17:12:33 +0000126 * The last parsed doctext comment.
127 */
128char* g_doctext;
129
130/**
131 * The location of the last parsed doctext comment.
132 */
133int g_doctext_lineno;
134
135/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000136 * Flags to control code generation
137 */
138bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000139bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000140bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000141bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000142bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000143bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000144bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000145bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000146bool gen_php = false;
147bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000148bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000149bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000150bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000151bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000152bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000153bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000154bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000155bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000156bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000157bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000158bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000159bool gen_recurse = false;
160
161/**
David Reiss204420f2008-01-11 20:59:03 +0000162 * MinGW doesn't have realpath, so use fallback implementation in that case,
163 * otherwise this just calls through to realpath
164 */
165char *saferealpath(const char *path, char *resolved_path) {
166#ifdef MINGW
167 char buf[MAX_PATH];
168 char* basename;
169 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
170 if (len == 0 || len > MAX_PATH - 1){
171 strcpy(resolved_path, path);
172 } else {
173 CharLowerBuff(buf, len);
174 strcpy(resolved_path, buf);
175 }
176 return resolved_path;
177#else
178 return realpath(path, resolved_path);
179#endif
180}
181
182
183/**
Mark Slee31985722006-05-24 21:45:31 +0000184 * Report an error to the user. This is called yyerror for historical
185 * reasons (lex and yacc expect the error reporting routine to be called
186 * this). Call this function to report any errors to the user.
187 * yyerror takes printf style arguments.
188 *
189 * @param fmt C format string followed by additional arguments
190 */
David Reiss0babe402008-06-10 22:56:12 +0000191void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000192 va_list args;
193 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000194 "[ERROR:%s:%d] (last token was '%s')\n",
195 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000196 yylineno,
197 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000198
199 va_start(args, fmt);
200 vfprintf(stderr, fmt, args);
201 va_end(args);
202
203 fprintf(stderr, "\n");
204}
205
206/**
207 * Prints a debug message from the parser.
208 *
209 * @param fmt C format string followed by additional arguments
210 */
David Reiss0babe402008-06-10 22:56:12 +0000211void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000212 if (g_debug == 0) {
213 return;
214 }
215 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000216 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000217 va_start(args, fmt);
218 vprintf(fmt, args);
219 va_end(args);
220 printf("\n");
221}
222
223/**
224 * Prints a verbose output mode message
225 *
226 * @param fmt C format string followed by additional arguments
227 */
David Reiss0babe402008-06-10 22:56:12 +0000228void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000229 if (g_verbose == 0) {
230 return;
231 }
232 va_list args;
233 va_start(args, fmt);
234 vprintf(fmt, args);
235 va_end(args);
236}
237
238/**
239 * Prints a warning message
240 *
241 * @param fmt C format string followed by additional arguments
242 */
David Reiss0babe402008-06-10 22:56:12 +0000243void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000244 if (g_warn < level) {
245 return;
246 }
247 va_list args;
248 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000249 va_start(args, fmt);
250 vprintf(fmt, args);
251 va_end(args);
252 printf("\n");
253}
254
255/**
256 * Prints a failure message and exits
257 *
258 * @param fmt C format string followed by additional arguments
259 */
Mark Slee30152872006-11-28 01:24:07 +0000260void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000261 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000262 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000263 va_start(args, fmt);
264 vfprintf(stderr, fmt, args);
265 va_end(args);
266 printf("\n");
267 exit(1);
268}
269
270/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 * Converts a string filename into a thrift program name
272 */
273string program_name(string filename) {
274 string::size_type slash = filename.rfind("/");
275 if (slash != string::npos) {
276 filename = filename.substr(slash+1);
277 }
278 string::size_type dot = filename.rfind(".");
279 if (dot != string::npos) {
280 filename = filename.substr(0, dot);
281 }
282 return filename;
283}
284
285/**
286 * Gets the directory path of a filename
287 */
288string directory_name(string filename) {
289 string::size_type slash = filename.rfind("/");
290 // No slash, just use the current directory
291 if (slash == string::npos) {
292 return ".";
293 }
294 return filename.substr(0, slash);
295}
296
297/**
298 * Finds the appropriate file path for the given filename
299 */
300string include_file(string filename) {
301 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000302 if (filename[0] == '/') {
303 // Realpath!
304 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000305 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000306 pwarning(0, "Cannot open include file %s\n", filename.c_str());
307 return std::string();
308 }
Mark Slee2c44d202007-05-16 02:18:07 +0000309
310 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000311 struct stat finfo;
312 if (stat(rp, &finfo) == 0) {
313 return rp;
314 }
315 } else { // relative path, start searching
316 // new search path with current dir global
317 vector<string> sp = g_incl_searchpath;
318 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000319
Martin Kraemer32c66e12006-11-09 00:06:36 +0000320 // iterate through paths
321 vector<string>::iterator it;
322 for (it = sp.begin(); it != sp.end(); it++) {
323 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000324
Martin Kraemer32c66e12006-11-09 00:06:36 +0000325 // Realpath!
326 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000327 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000328 continue;
329 }
Mark Slee2c44d202007-05-16 02:18:07 +0000330
Martin Kraemer32c66e12006-11-09 00:06:36 +0000331 // Stat this files
332 struct stat finfo;
333 if (stat(rp, &finfo) == 0) {
334 return rp;
335 }
336 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000337 }
Mark Slee2c44d202007-05-16 02:18:07 +0000338
Mark Sleef0712dc2006-10-25 19:03:57 +0000339 // Uh oh
340 pwarning(0, "Could not find include file %s\n", filename.c_str());
341 return std::string();
342}
343
344/**
David Reisscbd4bac2007-08-14 17:12:33 +0000345 * Clears any previously stored doctext string.
346 * Also prints a warning if we are discarding information.
347 */
348void clear_doctext() {
349 if (g_doctext != NULL) {
350 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
351 }
352 free(g_doctext);
353 g_doctext = NULL;
354}
355
356/**
David Reiss1ac05802007-07-30 22:00:27 +0000357 * Cleans up text commonly found in doxygen-like comments
358 *
359 * Warning: if you mix tabs and spaces in a non-uniform way,
360 * you will get what you deserve.
361 */
362char* clean_up_doctext(char* doctext) {
363 // Convert to C++ string, and remove Windows's carriage returns.
364 string docstring = doctext;
365 docstring.erase(
366 remove(docstring.begin(), docstring.end(), '\r'),
367 docstring.end());
368
369 // Separate into lines.
370 vector<string> lines;
371 string::size_type pos = string::npos;
372 string::size_type last;
373 while (true) {
374 last = (pos == string::npos) ? 0 : pos+1;
375 pos = docstring.find('\n', last);
376 if (pos == string::npos) {
377 // First bit of cleaning. If the last line is only whitespace, drop it.
378 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
379 if (nonwhite != string::npos) {
380 lines.push_back(docstring.substr(last));
381 }
382 break;
383 }
384 lines.push_back(docstring.substr(last, pos-last));
385 }
386
387 // A very profound docstring.
388 if (lines.empty()) {
389 return NULL;
390 }
391
392 // Clear leading whitespace from the first line.
393 pos = lines.front().find_first_not_of(" \t");
394 lines.front().erase(0, pos);
395
396 // If every nonblank line after the first has the same number of spaces/tabs,
397 // then a star, remove them.
398 bool have_prefix = true;
399 bool found_prefix = false;
400 string::size_type prefix_len = 0;
401 vector<string>::iterator l_iter;
402 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
403 if (l_iter->empty()) {
404 continue;
405 }
406
407 pos = l_iter->find_first_not_of(" \t");
408 if (!found_prefix) {
409 if (pos != string::npos) {
410 if (l_iter->at(pos) == '*') {
411 found_prefix = true;
412 prefix_len = pos;
413 } else {
414 have_prefix = false;
415 break;
416 }
417 } else {
418 // Whitespace-only line. Truncate it.
419 l_iter->clear();
420 }
421 } else if (l_iter->size() > pos
422 && l_iter->at(pos) == '*'
423 && pos == prefix_len) {
424 // Business as usual.
425 } else if (pos == string::npos) {
426 // Whitespace-only line. Let's truncate it for them.
427 l_iter->clear();
428 } else {
429 // The pattern has been broken.
430 have_prefix = false;
431 break;
432 }
433 }
434
435 // If our prefix survived, delete it from every line.
436 if (have_prefix) {
437 // Get the star too.
438 prefix_len++;
439 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
440 l_iter->erase(0, prefix_len);
441 }
442 }
443
444 // Now delete the minimum amount of leading whitespace from each line.
445 prefix_len = string::npos;
446 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
447 if (l_iter->empty()) {
448 continue;
449 }
450 pos = l_iter->find_first_not_of(" \t");
451 if (pos != string::npos
452 && (prefix_len == string::npos || pos < prefix_len)) {
453 prefix_len = pos;
454 }
455 }
456
457 // If our prefix survived, delete it from every line.
458 if (prefix_len != string::npos) {
459 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
460 l_iter->erase(0, prefix_len);
461 }
462 }
463
464 // Remove trailing whitespace from every line.
465 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
466 pos = l_iter->find_last_not_of(" \t");
467 if (pos != string::npos && pos != l_iter->length()-1) {
468 l_iter->erase(pos+1);
469 }
470 }
471
472 // If the first line is empty, remove it.
473 // Don't do this earlier because a lot of steps skip the first line.
474 if (lines.front().empty()) {
475 lines.erase(lines.begin());
476 }
477
478 // Now rejoin the lines and copy them back into doctext.
479 docstring.clear();
480 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
481 docstring += *l_iter;
482 docstring += '\n';
483 }
484
485 assert(docstring.length() <= strlen(doctext));
486 strcpy(doctext, docstring.c_str());
487 return doctext;
488}
489
490/** Set to true to debug docstring parsing */
491static bool dump_docs = false;
492
493/**
494 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000495 * Only works for top-level definitions and the whole program doc
496 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000497 */
498void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000499 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000500 if (!progdoc.empty()) {
501 printf("Whole program doc:\n%s\n", progdoc.c_str());
502 }
David Reiss1ac05802007-07-30 22:00:27 +0000503 const vector<t_typedef*>& typedefs = program->get_typedefs();
504 vector<t_typedef*>::const_iterator t_iter;
505 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
506 t_typedef* td = *t_iter;
507 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000508 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
509 }
510 }
511 const vector<t_enum*>& enums = program->get_enums();
512 vector<t_enum*>::const_iterator e_iter;
513 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
514 t_enum* en = *e_iter;
515 if (en->has_doc()) {
516 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
517 }
518 }
519 const vector<t_const*>& consts = program->get_consts();
520 vector<t_const*>::const_iterator c_iter;
521 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
522 t_const* co = *c_iter;
523 if (co->has_doc()) {
524 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
525 }
526 }
527 const vector<t_struct*>& structs = program->get_structs();
528 vector<t_struct*>::const_iterator s_iter;
529 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
530 t_struct* st = *s_iter;
531 if (st->has_doc()) {
532 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
533 }
534 }
535 const vector<t_struct*>& xceptions = program->get_xceptions();
536 vector<t_struct*>::const_iterator x_iter;
537 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
538 t_struct* xn = *x_iter;
539 if (xn->has_doc()) {
540 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
541 }
542 }
543 const vector<t_service*>& services = program->get_services();
544 vector<t_service*>::const_iterator v_iter;
545 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
546 t_service* sv = *v_iter;
547 if (sv->has_doc()) {
548 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000549 }
550 }
551}
552
553/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000554 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000555 */
556void generate_all_fingerprints(t_program* program) {
557 const vector<t_struct*>& structs = program->get_structs();
558 vector<t_struct*>::const_iterator s_iter;
559 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
560 t_struct* st = *s_iter;
561 st->generate_fingerprint();
562 }
563
David Reissd779cbe2007-08-31 01:42:55 +0000564 const vector<t_struct*>& xceptions = program->get_xceptions();
565 vector<t_struct*>::const_iterator x_iter;
566 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
567 t_struct* st = *x_iter;
568 st->generate_fingerprint();
569 }
570
David Reiss3c5d2fd2008-02-08 21:58:06 +0000571 const vector<t_enum*>& enums = program->get_enums();
572 vector<t_enum*>::const_iterator e_iter;
573 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
574 t_enum* e = *e_iter;
575 e->generate_fingerprint();
576 }
577
David Reiss47557bc2007-09-04 21:31:04 +0000578 g_type_void->generate_fingerprint();
579
David Reiss18bf22d2007-08-28 20:49:17 +0000580 // If you want to generate fingerprints for implicit structures, start here.
581 /*
582 const vector<t_service*>& services = program->get_services();
583 vector<t_service*>::const_iterator v_iter;
584 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
585 t_service* sv = *v_iter;
586 }
587 */
588}
589
590/**
David Reissdd08f6d2008-06-30 20:24:24 +0000591 * Prints the version number
592 */
593void version() {
594 printf("Thrift version %s-%s\n", THRIFT_VERSION, THRIFT_REVISION);
595}
596
597/**
Mark Slee31985722006-05-24 21:45:31 +0000598 * Diplays the usage message and then exits with an error code.
599 */
600void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000601 fprintf(stderr, "Usage: thrift [options] file\n");
602 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000603 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000604 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
605 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000606 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000607 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000608 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
609 fprintf(stderr, " -strict Strict compiler warnings on\n");
610 fprintf(stderr, " -v[erbose] Verbose mode\n");
611 fprintf(stderr, " -r[ecurse] Also generate included files\n");
612 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000613 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
614 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
615 fprintf(stderr, " Keys and values are options passed to the generator.\n");
616 fprintf(stderr, " Many options will not require values.\n");
617 fprintf(stderr, "\n");
618 fprintf(stderr, "Available generators (and options):\n");
619
620 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
621 t_generator_registry::gen_map_t::iterator iter;
622 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
623 fprintf(stderr, " %s (%s):\n",
624 iter->second->get_short_name().c_str(),
625 iter->second->get_long_name().c_str());
626 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
627 }
Mark Slee31985722006-05-24 21:45:31 +0000628 exit(1);
629}
630
631/**
Mark Slee30152872006-11-28 01:24:07 +0000632 * You know, when I started working on Thrift I really thought it wasn't going
633 * to become a programming language because it was just a generator and it
634 * wouldn't need runtime type information and all that jazz. But then we
635 * decided to add constants, and all of a sudden that means runtime type
636 * validation and inference, except the "runtime" is the code generator
637 * runtime. Shit. I've been had.
638 */
639void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
640 if (type->is_void()) {
641 throw "type error: cannot declare a void const: " + name;
642 }
643
644 if (type->is_base_type()) {
645 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
646 switch (tbase) {
647 case t_base_type::TYPE_STRING:
648 if (value->get_type() != t_const_value::CV_STRING) {
649 throw "type error: const \"" + name + "\" was declared as string";
650 }
651 break;
652 case t_base_type::TYPE_BOOL:
653 if (value->get_type() != t_const_value::CV_INTEGER) {
654 throw "type error: const \"" + name + "\" was declared as bool";
655 }
656 break;
657 case t_base_type::TYPE_BYTE:
658 if (value->get_type() != t_const_value::CV_INTEGER) {
659 throw "type error: const \"" + name + "\" was declared as byte";
660 }
661 break;
662 case t_base_type::TYPE_I16:
663 if (value->get_type() != t_const_value::CV_INTEGER) {
664 throw "type error: const \"" + name + "\" was declared as i16";
665 }
666 break;
667 case t_base_type::TYPE_I32:
668 if (value->get_type() != t_const_value::CV_INTEGER) {
669 throw "type error: const \"" + name + "\" was declared as i32";
670 }
671 break;
672 case t_base_type::TYPE_I64:
673 if (value->get_type() != t_const_value::CV_INTEGER) {
674 throw "type error: const \"" + name + "\" was declared as i64";
675 }
676 break;
677 case t_base_type::TYPE_DOUBLE:
678 if (value->get_type() != t_const_value::CV_INTEGER &&
679 value->get_type() != t_const_value::CV_DOUBLE) {
680 throw "type error: const \"" + name + "\" was declared as double";
681 }
682 break;
683 default:
David Reissdd7796f2007-08-28 21:09:06 +0000684 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000685 }
686 } else if (type->is_enum()) {
687 if (value->get_type() != t_const_value::CV_INTEGER) {
688 throw "type error: const \"" + name + "\" was declared as enum";
689 }
690 } else if (type->is_struct() || type->is_xception()) {
691 if (value->get_type() != t_const_value::CV_MAP) {
692 throw "type error: const \"" + name + "\" was declared as struct/xception";
693 }
694 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
695 vector<t_field*>::const_iterator f_iter;
696
697 const map<t_const_value*, t_const_value*>& val = value->get_map();
698 map<t_const_value*, t_const_value*>::const_iterator v_iter;
699 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
700 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
701 throw "type error: " + name + " struct key must be string";
702 }
703 t_type* field_type = NULL;
704 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
705 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
706 field_type = (*f_iter)->get_type();
707 }
708 }
709 if (field_type == NULL) {
710 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
711 }
712
713 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
714 }
715 } else if (type->is_map()) {
716 t_type* k_type = ((t_map*)type)->get_key_type();
717 t_type* v_type = ((t_map*)type)->get_val_type();
718 const map<t_const_value*, t_const_value*>& val = value->get_map();
719 map<t_const_value*, t_const_value*>::const_iterator v_iter;
720 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
721 validate_const_rec(name + "<key>", k_type, v_iter->first);
722 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000723 }
Mark Slee30152872006-11-28 01:24:07 +0000724 } else if (type->is_list() || type->is_set()) {
725 t_type* e_type;
726 if (type->is_list()) {
727 e_type = ((t_list*)type)->get_elem_type();
728 } else {
729 e_type = ((t_set*)type)->get_elem_type();
730 }
731 const vector<t_const_value*>& val = value->get_list();
732 vector<t_const_value*>::const_iterator v_iter;
733 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
734 validate_const_rec(name + "<elem>", e_type, *v_iter);
735 }
736 }
737}
738
739/**
740 * Check the type of the parsed const information against its declared type
741 */
742void validate_const_type(t_const* c) {
743 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
744}
745
746/**
Mark Slee7ff32452007-02-01 05:26:18 +0000747 * Check the type of a default value assigned to a field.
748 */
749void validate_field_value(t_field* field, t_const_value* cv) {
750 validate_const_rec(field->get_name(), field->get_type(), cv);
751}
752
753/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000754 * Check that all the elements of a throws block are actually exceptions.
755 */
756bool validate_throws(t_struct* throws) {
757 const vector<t_field*>& members = throws->get_members();
758 vector<t_field*>::const_iterator m_iter;
759 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
760 if (!(*m_iter)->get_type()->is_xception()) {
761 return false;
762 }
763 }
764 return true;
765}
766
767/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000768 * Parses a program
769 */
Mark Slee2c44d202007-05-16 02:18:07 +0000770void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000771 // Get scope file path
772 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000773
Mark Sleef0712dc2006-10-25 19:03:57 +0000774 // Set current dir global, which is used in the include_file function
775 g_curdir = directory_name(path);
776 g_curpath = path;
777
778 // Open the file
779 yyin = fopen(path.c_str(), "r");
780 if (yyin == 0) {
781 failure("Could not open input file: \"%s\"", path.c_str());
782 }
783
784 // Create new scope and scan for includes
785 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000786 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000787 g_program = program;
788 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000789 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000790 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000791 if (yyparse() != 0) {
792 failure("Parser error during include pass.");
793 }
794 } catch (string x) {
795 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000796 }
797 fclose(yyin);
798
799 // Recursively parse all the include programs
800 vector<t_program*>& includes = program->get_includes();
801 vector<t_program*>::iterator iter;
802 for (iter = includes.begin(); iter != includes.end(); ++iter) {
803 parse(*iter, program);
804 }
805
David Reiss204420f2008-01-11 20:59:03 +0000806 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000807 g_parse_mode = PROGRAM;
808 g_program = program;
809 g_scope = program->scope();
810 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
811 g_parent_prefix = program->get_name() + ".";
812 g_curpath = path;
813 yyin = fopen(path.c_str(), "r");
814 if (yyin == 0) {
815 failure("Could not open input file: \"%s\"", path.c_str());
816 }
817 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000818 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000819 try {
820 if (yyparse() != 0) {
821 failure("Parser error during types pass.");
822 }
823 } catch (string x) {
824 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000825 }
826 fclose(yyin);
827}
828
829/**
830 * Generate code
831 */
David Reissbd0db882008-02-27 01:54:51 +0000832void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000833 // Oooohh, recursive code generation, hot!!
834 if (gen_recurse) {
835 const vector<t_program*>& includes = program->get_includes();
836 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000837 // Propogate output path from parent to child programs
838 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000839
David Reissbd0db882008-02-27 01:54:51 +0000840 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000841 }
842 }
843
844 // Generate code!
845 try {
846 pverbose("Program: %s\n", program->get_path().c_str());
847
David Reiss18bf22d2007-08-28 20:49:17 +0000848 // Compute fingerprints.
849 generate_all_fingerprints(program);
850
David Reiss1ac05802007-07-30 22:00:27 +0000851 if (dump_docs) {
852 dump_docstrings(program);
853 }
David Reissbd0db882008-02-27 01:54:51 +0000854
855 vector<string>::const_iterator iter;
856 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
857 t_generator* generator = t_generator_registry::get_generator(program, *iter);
858
859 if (generator == NULL) {
860 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
861 } else {
862 pverbose("Generating \"%s\"\n", iter->c_str());
863 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000864 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000865 }
866 }
867
Mark Sleef0712dc2006-10-25 19:03:57 +0000868 } catch (string s) {
869 printf("Error: %s\n", s.c_str());
870 } catch (const char* exc) {
871 printf("Error: %s\n", exc);
872 }
873
874}
875
876/**
Mark Sleef5377b32006-10-10 01:42:59 +0000877 * Parse it up.. then spit it back out, in pretty much every language. Alright
878 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000879 */
880int main(int argc, char** argv) {
881 int i;
dweatherford65b70752007-10-31 02:18:14 +0000882 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000883
Mark Sleeb15a68b2006-06-07 06:46:24 +0000884 // Setup time string
885 time_t now = time(NULL);
886 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000887
Mark Sleef0712dc2006-10-25 19:03:57 +0000888 // Check for necessary arguments, you gotta have at least a filename and
889 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000890 if (argc < 2) {
891 usage();
892 }
Mark Slee31985722006-05-24 21:45:31 +0000893
David Reissbd0db882008-02-27 01:54:51 +0000894 vector<string> generator_strings;
895
David Reiss9cc2c132008-02-27 01:54:47 +0000896 // Set the current path to a dummy value to make warning messages clearer.
897 g_curpath = "arguments";
898
Mark Sleef5377b32006-10-10 01:42:59 +0000899 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000900 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000901 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000902
Mark Sleefdbee812006-09-27 18:50:48 +0000903 arg = strtok(argv[i], " ");
904 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000905 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000906 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000907 ++arg;
908 }
909
David Reissdd08f6d2008-06-30 20:24:24 +0000910 if (strcmp(arg, "-version") == 0) {
911 version();
912 exit(1);
913 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000914 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000915 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000916 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000917 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000918 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000919 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000920 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000921 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000922 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000923 } else if (strcmp(arg, "-gen") == 0) {
924 arg = argv[++i];
925 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000926 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000927 usage();
928 }
929 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000930 } else if (strcmp(arg, "-dense") == 0) {
931 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000932 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000933 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000934 } else if (strcmp(arg, "-javabean") == 0) {
935 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000936 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000937 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000938 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000939 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000940 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000941 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000942 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000943 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000944 gen_phps = true;
945 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000946 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000947 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000948 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000949 gen_php = true;
950 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000951 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000952 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000953 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000954 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000955 } else if (strcmp(arg, "-rest") == 0) {
956 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000957 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000958 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +0000959 } else if (strcmp(arg, "-pyns") == 0) {
960 gen_py = true;
961 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000962 } else if (strcmp(arg, "-rb") == 0) {
963 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000964 } else if (strcmp(arg, "-xsd") == 0) {
965 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000966 } else if (strcmp(arg, "-perl") == 0) {
967 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +0000968 } else if (strcmp(arg, "-erl") == 0) {
969 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000970 } else if (strcmp(arg, "-ocaml") == 0) {
971 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000972 } else if (strcmp(arg, "-hs") == 0) {
973 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000974 } else if (strcmp(arg, "-cocoa") == 0) {
975 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +0000976 } else if (strcmp(arg, "-st") == 0) {
977 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +0000978 } else if (strcmp(arg, "-csharp") == 0) {
979 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +0000980 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
981 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000982 } else if (strcmp(arg, "-I") == 0) {
983 // An argument of "-I\ asdf" is invalid and has unknown results
984 arg = argv[++i];
985
986 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000987 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +0000988 usage();
989 }
990 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +0000991 } else if (strcmp(arg, "-o") == 0) {
992 arg = argv[++i];
993 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000994 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +0000995 usage();
Mark Slee5b743072007-11-13 04:00:29 +0000996 }
dweatherford65b70752007-10-31 02:18:14 +0000997 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +0000998
999#ifdef MINGW
1000 //strip out trailing \ on Windows
1001 int last = out_path.length()-1;
1002 if (out_path[last] == '\\')
1003 {
1004 out_path.erase(last);
1005 }
1006#endif
1007
dweatherford65b70752007-10-31 02:18:14 +00001008 struct stat sb;
1009 if (stat(out_path.c_str(), &sb) < 0) {
1010 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1011 return -1;
1012 }
1013 if (! S_ISDIR(sb.st_mode)) {
1014 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1015 return -1;
1016 }
Mark Sleefdbee812006-09-27 18:50:48 +00001017 } else {
1018 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1019 usage();
1020 }
1021
1022 // Tokenize more
1023 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001024 }
1025 }
Mark Slee2c44d202007-05-16 02:18:07 +00001026
David Reissdd08f6d2008-06-30 20:24:24 +00001027 // if you're asking for version, you have a right not to pass a file
1028 if (strcmp(argv[argc-1], "-version") == 0) {
1029 version();
1030 exit(1);
1031 }
1032
David Reiss400cd4b2008-02-27 01:54:55 +00001033 // TODO(dreiss): Delete these when everyone is using the new hotness.
1034 if (gen_cpp) {
1035 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1036 string gen_string = "cpp:";
1037 if (gen_dense) {
1038 gen_string.append("dense,");
1039 }
1040 if (g_cpp_use_include_prefix) {
1041 gen_string.append("include_prefix,");
1042 }
1043 generator_strings.push_back(gen_string);
1044 }
David Reisse1404d22008-02-27 01:55:05 +00001045 if (gen_java) {
1046 pwarning(1, "-java is deprecated. Use --gen java");
1047 generator_strings.push_back("java");
1048 }
1049 if (gen_javabean) {
1050 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1051 generator_strings.push_back("java:beans");
1052 }
David Reiss861869b2008-03-27 21:41:23 +00001053 if (gen_csharp) {
1054 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1055 generator_strings.push_back("csharp");
1056 }
David Reiss558e3992008-03-27 21:41:40 +00001057 if (gen_py) {
1058 pwarning(1, "-py is deprecated. Use --gen py");
1059 generator_strings.push_back("py");
1060 }
David Reissa640cea2008-03-27 21:42:01 +00001061 if (gen_rb) {
1062 pwarning(1, "-rb is deprecated. Use --gen rb");
1063 generator_strings.push_back("rb");
1064 }
David Reiss2b386c52008-03-27 21:42:23 +00001065 if (gen_perl) {
1066 pwarning(1, "-perl is deprecated. Use --gen perl");
1067 generator_strings.push_back("perl");
1068 }
David Reissa9ea68b2009-02-17 20:28:24 +00001069 if (gen_php || gen_phpi) {
1070 pwarning(1, "-php is deprecated. Use --gen php");
1071 string gen_string = "php:";
1072 if (gen_phpi) {
1073 gen_string.append("inlined,");
1074 } else if(gen_phps) {
1075 gen_string.append("server,");
1076 } else if(gen_phpa) {
1077 gen_string.append("autoload,");
1078 } else if(gen_phpo) {
1079 gen_string.append("oop,");
1080 } else if(gen_rest) {
1081 gen_string.append("rest,");
1082 }
1083 generator_strings.push_back(gen_string);
1084 }
David Reissd01c64d2008-03-27 21:40:55 +00001085 if (gen_cocoa) {
1086 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1087 generator_strings.push_back("cocoa");
1088 }
David Reissa2047832009-02-17 20:27:54 +00001089 if (gen_erl) {
1090 pwarning(1, "-erl is deprecated. Use --gen erl");
1091 generator_strings.push_back("erl");
1092 }
David Reissa890b572008-03-27 21:40:35 +00001093 if (gen_st) {
1094 pwarning(1, "-st is deprecated. Use --gen st");
1095 generator_strings.push_back("st");
1096 }
David Reissf0521b12008-03-27 21:40:05 +00001097 if (gen_ocaml) {
1098 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1099 generator_strings.push_back("ocaml");
1100 }
David Reissaf3ab262008-03-27 21:40:16 +00001101 if (gen_hs) {
1102 pwarning(1, "-hs is deprecated. Use --gen hs");
1103 generator_strings.push_back("hs");
1104 }
David Reiss4ba67102009-02-17 20:28:06 +00001105 if (gen_xsd) {
1106 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1107 generator_strings.push_back("xsd");
1108 }
David Reisse1404d22008-02-27 01:55:05 +00001109
Mark Sleef0712dc2006-10-25 19:03:57 +00001110 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001111 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001112 fprintf(stderr, "!!! No output language(s) specified\n\n");
1113 usage();
1114 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001115
1116 // Real-pathify it
1117 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001118 if (argv[i] == NULL) {
1119 fprintf(stderr, "!!! Missing file name\n");
1120 usage();
1121 }
David Reiss204420f2008-01-11 20:59:03 +00001122 if (saferealpath(argv[i], rp) == NULL) {
1123 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001124 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001125 string input_file(rp);
1126
Mark Sleef5377b32006-10-10 01:42:59 +00001127 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001128 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001129 if (out_path.size()) {
1130 program->set_out_path(out_path);
1131 }
kholst76f2c882008-01-16 02:47:41 +00001132
David Reiss4b6a3c72008-02-27 22:28:12 +00001133 // Compute the cpp include prefix.
1134 // infer this from the filename passed in
1135 string input_filename = argv[i];
1136 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001137
David Reiss4b6a3c72008-02-27 22:28:12 +00001138 string::size_type last_slash = string::npos;
1139 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1140 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001141 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001142
David Reiss4b6a3c72008-02-27 22:28:12 +00001143 program->set_include_prefix(include_prefix);
1144
Mark Sleef0712dc2006-10-25 19:03:57 +00001145 // Initialize global types
1146 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1147 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001148 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1149 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001150 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1151 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001152 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1153 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1154 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1155 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1156 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1157 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001158
Mark Sleef5377b32006-10-10 01:42:59 +00001159 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001160 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001161
David Reiss9cc2c132008-02-27 01:54:47 +00001162 // The current path is not really relevant when we are doing generation.
1163 // Reset the variable to make warning messages clearer.
1164 g_curpath = "generation";
1165 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1166 // That is what shows up during argument parsing.
1167 yylineno = 1;
1168
Mark Sleef0712dc2006-10-25 19:03:57 +00001169 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001170 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001171
Mark Sleef0712dc2006-10-25 19:03:57 +00001172 // Clean up. Who am I kidding... this program probably orphans heap memory
1173 // all over the place, but who cares because it is about to exit and it is
1174 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001175
Mark Sleef0712dc2006-10-25 19:03:57 +00001176 delete program;
1177 delete g_type_void;
1178 delete g_type_string;
1179 delete g_type_bool;
1180 delete g_type_byte;
1181 delete g_type_i16;
1182 delete g_type_i32;
1183 delete g_type_i64;
1184 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001185
1186 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001187 return 0;
1188}