blob: 34990356e8a9bdea2809e682d4bef834351337e7 [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee31985722006-05-24 21:45:31 +00007/**
8 * thrift - a lightweight cross-language rpc/serialization tool
9 *
10 * This file contains the main compiler engine for Thrift, which invokes the
11 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +000012 * code for each language lives in a file by the language name under the
13 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +000014 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
17
David Reissf10984b2008-03-27 21:39:52 +000018#include <cassert>
Mark Slee31985722006-05-24 21:45:31 +000019#include <stdlib.h>
20#include <stdio.h>
21#include <stdarg.h>
22#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000023#include <sys/types.h>
24#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000025#include <errno.h>
Mark Slee31985722006-05-24 21:45:31 +000026
David Reiss204420f2008-01-11 20:59:03 +000027#ifdef MINGW
28# include <windows.h> /* for GetFullPathName */
29# include <limits.h>
30#endif
31
Mark Sleef0712dc2006-10-25 19:03:57 +000032// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000033#include "globals.h"
34
35#include "main.h"
36#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000037#include "parse/t_scope.h"
Mark Slee6e536442006-06-30 18:28:50 +000038#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000039#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000040#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000041#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000042#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000043#include "generate/t_erl_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000044#include "generate/t_csharp_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000045
46using namespace std;
47
Mark Sleef5377b32006-10-10 01:42:59 +000048/**
49 * Global program tree
50 */
Mark Slee31985722006-05-24 21:45:31 +000051t_program* g_program;
52
Mark Sleef5377b32006-10-10 01:42:59 +000053/**
Mark Sleef0712dc2006-10-25 19:03:57 +000054 * Global types
55 */
56
57t_type* g_type_void;
58t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000059t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000060t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000061t_type* g_type_bool;
62t_type* g_type_byte;
63t_type* g_type_i16;
64t_type* g_type_i32;
65t_type* g_type_i64;
66t_type* g_type_double;
67
68/**
69 * Global scope
70 */
71t_scope* g_scope;
72
73/**
74 * Parent scope to also parse types
75 */
76t_scope* g_parent_scope;
77
78/**
79 * Prefix for putting types in parent scope
80 */
81string g_parent_prefix;
82
83/**
84 * Parsing pass
85 */
86PARSE_MODE g_parse_mode;
87
88/**
89 * Current directory of file being parsed
90 */
91string g_curdir;
92
93/**
94 * Current file being parsed
95 */
96string g_curpath;
97
98/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000099 * Search path for inclusions
100 */
Mark Slee2329a832006-11-09 00:23:30 +0000101vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000102
103/**
kholst76f2c882008-01-16 02:47:41 +0000104 * Should C++ include statements use path prefixes for other thrift-generated
105 * header files
106 */
107bool g_cpp_use_include_prefix = false;
108
109/**
Mark Sleef5377b32006-10-10 01:42:59 +0000110 * Global debug state
111 */
Mark Slee31985722006-05-24 21:45:31 +0000112int g_debug = 0;
113
Mark Sleef5377b32006-10-10 01:42:59 +0000114/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000115 * Warning level
116 */
117int g_warn = 1;
118
119/**
120 * Verbose output
121 */
122int g_verbose = 0;
123
124/**
Mark Sleef5377b32006-10-10 01:42:59 +0000125 * Global time string
126 */
Mark Slee31985722006-05-24 21:45:31 +0000127char* g_time_str;
128
Mark Slee31985722006-05-24 21:45:31 +0000129/**
David Reisscbd4bac2007-08-14 17:12:33 +0000130 * The last parsed doctext comment.
131 */
132char* g_doctext;
133
134/**
135 * The location of the last parsed doctext comment.
136 */
137int g_doctext_lineno;
138
139/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000140 * Flags to control code generation
141 */
142bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000143bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000144bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000145bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000146bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000147bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000148bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000149bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000150bool gen_php = false;
151bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000152bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000153bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000154bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000155bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000156bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000157bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000158bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000159bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000160bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000161bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000162bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000163bool gen_recurse = false;
164
165/**
David Reiss204420f2008-01-11 20:59:03 +0000166 * MinGW doesn't have realpath, so use fallback implementation in that case,
167 * otherwise this just calls through to realpath
168 */
169char *saferealpath(const char *path, char *resolved_path) {
170#ifdef MINGW
171 char buf[MAX_PATH];
172 char* basename;
173 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
174 if (len == 0 || len > MAX_PATH - 1){
175 strcpy(resolved_path, path);
176 } else {
177 CharLowerBuff(buf, len);
178 strcpy(resolved_path, buf);
179 }
180 return resolved_path;
181#else
182 return realpath(path, resolved_path);
183#endif
184}
185
186
187/**
Mark Slee31985722006-05-24 21:45:31 +0000188 * Report an error to the user. This is called yyerror for historical
189 * reasons (lex and yacc expect the error reporting routine to be called
190 * this). Call this function to report any errors to the user.
191 * yyerror takes printf style arguments.
192 *
193 * @param fmt C format string followed by additional arguments
194 */
195void yyerror(char* fmt, ...) {
196 va_list args;
197 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000198 "[ERROR:%s:%d] (last token was '%s')\n",
199 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000200 yylineno,
201 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000202
203 va_start(args, fmt);
204 vfprintf(stderr, fmt, args);
205 va_end(args);
206
207 fprintf(stderr, "\n");
208}
209
210/**
211 * Prints a debug message from the parser.
212 *
213 * @param fmt C format string followed by additional arguments
214 */
215void pdebug(char* fmt, ...) {
216 if (g_debug == 0) {
217 return;
218 }
219 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000220 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000221 va_start(args, fmt);
222 vprintf(fmt, args);
223 va_end(args);
224 printf("\n");
225}
226
227/**
228 * Prints a verbose output mode message
229 *
230 * @param fmt C format string followed by additional arguments
231 */
232void pverbose(char* fmt, ...) {
233 if (g_verbose == 0) {
234 return;
235 }
236 va_list args;
237 va_start(args, fmt);
238 vprintf(fmt, args);
239 va_end(args);
240}
241
242/**
243 * Prints a warning message
244 *
245 * @param fmt C format string followed by additional arguments
246 */
247void pwarning(int level, char* fmt, ...) {
248 if (g_warn < level) {
249 return;
250 }
251 va_list args;
252 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000253 va_start(args, fmt);
254 vprintf(fmt, args);
255 va_end(args);
256 printf("\n");
257}
258
259/**
260 * Prints a failure message and exits
261 *
262 * @param fmt C format string followed by additional arguments
263 */
Mark Slee30152872006-11-28 01:24:07 +0000264void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000265 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000266 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000267 va_start(args, fmt);
268 vfprintf(stderr, fmt, args);
269 va_end(args);
270 printf("\n");
271 exit(1);
272}
273
274/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000275 * Converts a string filename into a thrift program name
276 */
277string program_name(string filename) {
278 string::size_type slash = filename.rfind("/");
279 if (slash != string::npos) {
280 filename = filename.substr(slash+1);
281 }
282 string::size_type dot = filename.rfind(".");
283 if (dot != string::npos) {
284 filename = filename.substr(0, dot);
285 }
286 return filename;
287}
288
289/**
290 * Gets the directory path of a filename
291 */
292string directory_name(string filename) {
293 string::size_type slash = filename.rfind("/");
294 // No slash, just use the current directory
295 if (slash == string::npos) {
296 return ".";
297 }
298 return filename.substr(0, slash);
299}
300
301/**
302 * Finds the appropriate file path for the given filename
303 */
304string include_file(string filename) {
305 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000306 if (filename[0] == '/') {
307 // Realpath!
308 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000309 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000310 pwarning(0, "Cannot open include file %s\n", filename.c_str());
311 return std::string();
312 }
Mark Slee2c44d202007-05-16 02:18:07 +0000313
314 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000315 struct stat finfo;
316 if (stat(rp, &finfo) == 0) {
317 return rp;
318 }
319 } else { // relative path, start searching
320 // new search path with current dir global
321 vector<string> sp = g_incl_searchpath;
322 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000323
Martin Kraemer32c66e12006-11-09 00:06:36 +0000324 // iterate through paths
325 vector<string>::iterator it;
326 for (it = sp.begin(); it != sp.end(); it++) {
327 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000328
Martin Kraemer32c66e12006-11-09 00:06:36 +0000329 // Realpath!
330 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000331 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000332 continue;
333 }
Mark Slee2c44d202007-05-16 02:18:07 +0000334
Martin Kraemer32c66e12006-11-09 00:06:36 +0000335 // Stat this files
336 struct stat finfo;
337 if (stat(rp, &finfo) == 0) {
338 return rp;
339 }
340 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000341 }
Mark Slee2c44d202007-05-16 02:18:07 +0000342
Mark Sleef0712dc2006-10-25 19:03:57 +0000343 // Uh oh
344 pwarning(0, "Could not find include file %s\n", filename.c_str());
345 return std::string();
346}
347
348/**
David Reisscbd4bac2007-08-14 17:12:33 +0000349 * Clears any previously stored doctext string.
350 * Also prints a warning if we are discarding information.
351 */
352void clear_doctext() {
353 if (g_doctext != NULL) {
354 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
355 }
356 free(g_doctext);
357 g_doctext = NULL;
358}
359
360/**
David Reiss1ac05802007-07-30 22:00:27 +0000361 * Cleans up text commonly found in doxygen-like comments
362 *
363 * Warning: if you mix tabs and spaces in a non-uniform way,
364 * you will get what you deserve.
365 */
366char* clean_up_doctext(char* doctext) {
367 // Convert to C++ string, and remove Windows's carriage returns.
368 string docstring = doctext;
369 docstring.erase(
370 remove(docstring.begin(), docstring.end(), '\r'),
371 docstring.end());
372
373 // Separate into lines.
374 vector<string> lines;
375 string::size_type pos = string::npos;
376 string::size_type last;
377 while (true) {
378 last = (pos == string::npos) ? 0 : pos+1;
379 pos = docstring.find('\n', last);
380 if (pos == string::npos) {
381 // First bit of cleaning. If the last line is only whitespace, drop it.
382 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
383 if (nonwhite != string::npos) {
384 lines.push_back(docstring.substr(last));
385 }
386 break;
387 }
388 lines.push_back(docstring.substr(last, pos-last));
389 }
390
391 // A very profound docstring.
392 if (lines.empty()) {
393 return NULL;
394 }
395
396 // Clear leading whitespace from the first line.
397 pos = lines.front().find_first_not_of(" \t");
398 lines.front().erase(0, pos);
399
400 // If every nonblank line after the first has the same number of spaces/tabs,
401 // then a star, remove them.
402 bool have_prefix = true;
403 bool found_prefix = false;
404 string::size_type prefix_len = 0;
405 vector<string>::iterator l_iter;
406 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
407 if (l_iter->empty()) {
408 continue;
409 }
410
411 pos = l_iter->find_first_not_of(" \t");
412 if (!found_prefix) {
413 if (pos != string::npos) {
414 if (l_iter->at(pos) == '*') {
415 found_prefix = true;
416 prefix_len = pos;
417 } else {
418 have_prefix = false;
419 break;
420 }
421 } else {
422 // Whitespace-only line. Truncate it.
423 l_iter->clear();
424 }
425 } else if (l_iter->size() > pos
426 && l_iter->at(pos) == '*'
427 && pos == prefix_len) {
428 // Business as usual.
429 } else if (pos == string::npos) {
430 // Whitespace-only line. Let's truncate it for them.
431 l_iter->clear();
432 } else {
433 // The pattern has been broken.
434 have_prefix = false;
435 break;
436 }
437 }
438
439 // If our prefix survived, delete it from every line.
440 if (have_prefix) {
441 // Get the star too.
442 prefix_len++;
443 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
444 l_iter->erase(0, prefix_len);
445 }
446 }
447
448 // Now delete the minimum amount of leading whitespace from each line.
449 prefix_len = string::npos;
450 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
451 if (l_iter->empty()) {
452 continue;
453 }
454 pos = l_iter->find_first_not_of(" \t");
455 if (pos != string::npos
456 && (prefix_len == string::npos || pos < prefix_len)) {
457 prefix_len = pos;
458 }
459 }
460
461 // If our prefix survived, delete it from every line.
462 if (prefix_len != string::npos) {
463 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
464 l_iter->erase(0, prefix_len);
465 }
466 }
467
468 // Remove trailing whitespace from every line.
469 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
470 pos = l_iter->find_last_not_of(" \t");
471 if (pos != string::npos && pos != l_iter->length()-1) {
472 l_iter->erase(pos+1);
473 }
474 }
475
476 // If the first line is empty, remove it.
477 // Don't do this earlier because a lot of steps skip the first line.
478 if (lines.front().empty()) {
479 lines.erase(lines.begin());
480 }
481
482 // Now rejoin the lines and copy them back into doctext.
483 docstring.clear();
484 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
485 docstring += *l_iter;
486 docstring += '\n';
487 }
488
489 assert(docstring.length() <= strlen(doctext));
490 strcpy(doctext, docstring.c_str());
491 return doctext;
492}
493
494/** Set to true to debug docstring parsing */
495static bool dump_docs = false;
496
497/**
498 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000499 * Only works for top-level definitions and the whole program doc
500 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000501 */
502void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000503 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000504 if (!progdoc.empty()) {
505 printf("Whole program doc:\n%s\n", progdoc.c_str());
506 }
David Reiss1ac05802007-07-30 22:00:27 +0000507 const vector<t_typedef*>& typedefs = program->get_typedefs();
508 vector<t_typedef*>::const_iterator t_iter;
509 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
510 t_typedef* td = *t_iter;
511 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000512 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
513 }
514 }
515 const vector<t_enum*>& enums = program->get_enums();
516 vector<t_enum*>::const_iterator e_iter;
517 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
518 t_enum* en = *e_iter;
519 if (en->has_doc()) {
520 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
521 }
522 }
523 const vector<t_const*>& consts = program->get_consts();
524 vector<t_const*>::const_iterator c_iter;
525 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
526 t_const* co = *c_iter;
527 if (co->has_doc()) {
528 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
529 }
530 }
531 const vector<t_struct*>& structs = program->get_structs();
532 vector<t_struct*>::const_iterator s_iter;
533 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
534 t_struct* st = *s_iter;
535 if (st->has_doc()) {
536 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
537 }
538 }
539 const vector<t_struct*>& xceptions = program->get_xceptions();
540 vector<t_struct*>::const_iterator x_iter;
541 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
542 t_struct* xn = *x_iter;
543 if (xn->has_doc()) {
544 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
545 }
546 }
547 const vector<t_service*>& services = program->get_services();
548 vector<t_service*>::const_iterator v_iter;
549 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
550 t_service* sv = *v_iter;
551 if (sv->has_doc()) {
552 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000553 }
554 }
555}
556
557/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000558 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000559 */
560void generate_all_fingerprints(t_program* program) {
561 const vector<t_struct*>& structs = program->get_structs();
562 vector<t_struct*>::const_iterator s_iter;
563 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
564 t_struct* st = *s_iter;
565 st->generate_fingerprint();
566 }
567
David Reissd779cbe2007-08-31 01:42:55 +0000568 const vector<t_struct*>& xceptions = program->get_xceptions();
569 vector<t_struct*>::const_iterator x_iter;
570 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
571 t_struct* st = *x_iter;
572 st->generate_fingerprint();
573 }
574
David Reiss3c5d2fd2008-02-08 21:58:06 +0000575 const vector<t_enum*>& enums = program->get_enums();
576 vector<t_enum*>::const_iterator e_iter;
577 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
578 t_enum* e = *e_iter;
579 e->generate_fingerprint();
580 }
581
David Reiss47557bc2007-09-04 21:31:04 +0000582 g_type_void->generate_fingerprint();
583
David Reiss18bf22d2007-08-28 20:49:17 +0000584 // If you want to generate fingerprints for implicit structures, start here.
585 /*
586 const vector<t_service*>& services = program->get_services();
587 vector<t_service*>::const_iterator v_iter;
588 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
589 t_service* sv = *v_iter;
590 }
591 */
592}
593
594/**
Mark Slee31985722006-05-24 21:45:31 +0000595 * Diplays the usage message and then exits with an error code.
596 */
597void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000598 fprintf(stderr, "Usage: thrift [options] file\n");
599 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000600 fprintf(stderr, " -php Generate PHP output files\n");
601 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000602 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
603 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000604 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000605 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000606 fprintf(stderr, " -py Generate Python output files\n");
David Reiss6495adb2007-12-18 03:37:30 +0000607 fprintf(stderr, " -pyns Generate Python new-style classes (with -py)\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000608 fprintf(stderr, " -rb Generate Ruby output files\n");
609 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000610 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000611 fprintf(stderr, " -erl Generate Erlang output files\n");
David Reiss7f42bcf2008-01-11 20:59:12 +0000612 fprintf(stderr, " -csharp Generate C# output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000613 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
614 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000615 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000616 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000617 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000618 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
619 fprintf(stderr, " -strict Strict compiler warnings on\n");
620 fprintf(stderr, " -v[erbose] Verbose mode\n");
621 fprintf(stderr, " -r[ecurse] Also generate included files\n");
622 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000623 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
624 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
625 fprintf(stderr, " Keys and values are options passed to the generator.\n");
626 fprintf(stderr, " Many options will not require values.\n");
627 fprintf(stderr, "\n");
628 fprintf(stderr, "Available generators (and options):\n");
629
630 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
631 t_generator_registry::gen_map_t::iterator iter;
632 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
633 fprintf(stderr, " %s (%s):\n",
634 iter->second->get_short_name().c_str(),
635 iter->second->get_long_name().c_str());
636 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
637 }
Mark Slee31985722006-05-24 21:45:31 +0000638 exit(1);
639}
640
641/**
Mark Slee30152872006-11-28 01:24:07 +0000642 * You know, when I started working on Thrift I really thought it wasn't going
643 * to become a programming language because it was just a generator and it
644 * wouldn't need runtime type information and all that jazz. But then we
645 * decided to add constants, and all of a sudden that means runtime type
646 * validation and inference, except the "runtime" is the code generator
647 * runtime. Shit. I've been had.
648 */
649void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
650 if (type->is_void()) {
651 throw "type error: cannot declare a void const: " + name;
652 }
653
654 if (type->is_base_type()) {
655 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
656 switch (tbase) {
657 case t_base_type::TYPE_STRING:
658 if (value->get_type() != t_const_value::CV_STRING) {
659 throw "type error: const \"" + name + "\" was declared as string";
660 }
661 break;
662 case t_base_type::TYPE_BOOL:
663 if (value->get_type() != t_const_value::CV_INTEGER) {
664 throw "type error: const \"" + name + "\" was declared as bool";
665 }
666 break;
667 case t_base_type::TYPE_BYTE:
668 if (value->get_type() != t_const_value::CV_INTEGER) {
669 throw "type error: const \"" + name + "\" was declared as byte";
670 }
671 break;
672 case t_base_type::TYPE_I16:
673 if (value->get_type() != t_const_value::CV_INTEGER) {
674 throw "type error: const \"" + name + "\" was declared as i16";
675 }
676 break;
677 case t_base_type::TYPE_I32:
678 if (value->get_type() != t_const_value::CV_INTEGER) {
679 throw "type error: const \"" + name + "\" was declared as i32";
680 }
681 break;
682 case t_base_type::TYPE_I64:
683 if (value->get_type() != t_const_value::CV_INTEGER) {
684 throw "type error: const \"" + name + "\" was declared as i64";
685 }
686 break;
687 case t_base_type::TYPE_DOUBLE:
688 if (value->get_type() != t_const_value::CV_INTEGER &&
689 value->get_type() != t_const_value::CV_DOUBLE) {
690 throw "type error: const \"" + name + "\" was declared as double";
691 }
692 break;
693 default:
David Reissdd7796f2007-08-28 21:09:06 +0000694 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000695 }
696 } else if (type->is_enum()) {
697 if (value->get_type() != t_const_value::CV_INTEGER) {
698 throw "type error: const \"" + name + "\" was declared as enum";
699 }
700 } else if (type->is_struct() || type->is_xception()) {
701 if (value->get_type() != t_const_value::CV_MAP) {
702 throw "type error: const \"" + name + "\" was declared as struct/xception";
703 }
704 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
705 vector<t_field*>::const_iterator f_iter;
706
707 const map<t_const_value*, t_const_value*>& val = value->get_map();
708 map<t_const_value*, t_const_value*>::const_iterator v_iter;
709 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
710 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
711 throw "type error: " + name + " struct key must be string";
712 }
713 t_type* field_type = NULL;
714 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
715 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
716 field_type = (*f_iter)->get_type();
717 }
718 }
719 if (field_type == NULL) {
720 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
721 }
722
723 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
724 }
725 } else if (type->is_map()) {
726 t_type* k_type = ((t_map*)type)->get_key_type();
727 t_type* v_type = ((t_map*)type)->get_val_type();
728 const map<t_const_value*, t_const_value*>& val = value->get_map();
729 map<t_const_value*, t_const_value*>::const_iterator v_iter;
730 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
731 validate_const_rec(name + "<key>", k_type, v_iter->first);
732 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000733 }
Mark Slee30152872006-11-28 01:24:07 +0000734 } else if (type->is_list() || type->is_set()) {
735 t_type* e_type;
736 if (type->is_list()) {
737 e_type = ((t_list*)type)->get_elem_type();
738 } else {
739 e_type = ((t_set*)type)->get_elem_type();
740 }
741 const vector<t_const_value*>& val = value->get_list();
742 vector<t_const_value*>::const_iterator v_iter;
743 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
744 validate_const_rec(name + "<elem>", e_type, *v_iter);
745 }
746 }
747}
748
749/**
750 * Check the type of the parsed const information against its declared type
751 */
752void validate_const_type(t_const* c) {
753 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
754}
755
756/**
Mark Slee7ff32452007-02-01 05:26:18 +0000757 * Check the type of a default value assigned to a field.
758 */
759void validate_field_value(t_field* field, t_const_value* cv) {
760 validate_const_rec(field->get_name(), field->get_type(), cv);
761}
762
763/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000764 * Check that all the elements of a throws block are actually exceptions.
765 */
766bool validate_throws(t_struct* throws) {
767 const vector<t_field*>& members = throws->get_members();
768 vector<t_field*>::const_iterator m_iter;
769 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
770 if (!(*m_iter)->get_type()->is_xception()) {
771 return false;
772 }
773 }
774 return true;
775}
776
777/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000778 * Parses a program
779 */
Mark Slee2c44d202007-05-16 02:18:07 +0000780void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000781 // Get scope file path
782 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000783
Mark Sleef0712dc2006-10-25 19:03:57 +0000784 // Set current dir global, which is used in the include_file function
785 g_curdir = directory_name(path);
786 g_curpath = path;
787
788 // Open the file
789 yyin = fopen(path.c_str(), "r");
790 if (yyin == 0) {
791 failure("Could not open input file: \"%s\"", path.c_str());
792 }
793
794 // Create new scope and scan for includes
795 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000796 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000797 g_program = program;
798 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000799 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000800 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000801 if (yyparse() != 0) {
802 failure("Parser error during include pass.");
803 }
804 } catch (string x) {
805 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000806 }
807 fclose(yyin);
808
809 // Recursively parse all the include programs
810 vector<t_program*>& includes = program->get_includes();
811 vector<t_program*>::iterator iter;
812 for (iter = includes.begin(); iter != includes.end(); ++iter) {
813 parse(*iter, program);
814 }
815
David Reiss204420f2008-01-11 20:59:03 +0000816 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000817 g_parse_mode = PROGRAM;
818 g_program = program;
819 g_scope = program->scope();
820 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
821 g_parent_prefix = program->get_name() + ".";
822 g_curpath = path;
823 yyin = fopen(path.c_str(), "r");
824 if (yyin == 0) {
825 failure("Could not open input file: \"%s\"", path.c_str());
826 }
827 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000828 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000829 try {
830 if (yyparse() != 0) {
831 failure("Parser error during types pass.");
832 }
833 } catch (string x) {
834 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000835 }
836 fclose(yyin);
837}
838
839/**
840 * Generate code
841 */
David Reissbd0db882008-02-27 01:54:51 +0000842void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000843 // Oooohh, recursive code generation, hot!!
844 if (gen_recurse) {
845 const vector<t_program*>& includes = program->get_includes();
846 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000847 // Propogate output path from parent to child programs
848 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000849
David Reissbd0db882008-02-27 01:54:51 +0000850 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000851 }
852 }
853
854 // Generate code!
855 try {
856 pverbose("Program: %s\n", program->get_path().c_str());
857
David Reiss18bf22d2007-08-28 20:49:17 +0000858 // Compute fingerprints.
859 generate_all_fingerprints(program);
860
Mark Sleef0712dc2006-10-25 19:03:57 +0000861 if (gen_php) {
862 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000863 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 +0000864 php->generate_program();
865 delete php;
866 }
867
868 if (gen_phpi) {
869 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000870 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000871 phpi->generate_program();
872 delete phpi;
873 }
874
875 if (gen_py) {
876 pverbose("Generating Python\n");
David Reiss6495adb2007-12-18 03:37:30 +0000877 t_py_generator* py = new t_py_generator(program, gen_py_newstyle);
Mark Sleef0712dc2006-10-25 19:03:57 +0000878 py->generate_program();
879 delete py;
880 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000881
Mark Slee6d7d5952007-01-27 01:44:22 +0000882 if (gen_rb) {
883 pverbose("Generating Ruby\n");
884 t_rb_generator* rb = new t_rb_generator(program);
885 rb->generate_program();
886 delete rb;
887 }
888
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000889 if (gen_xsd) {
890 pverbose("Generating XSD\n");
891 t_xsd_generator* xsd = new t_xsd_generator(program);
892 xsd->generate_program();
893 delete xsd;
894 }
895
Mark Slee2c44d202007-05-16 02:18:07 +0000896 if (gen_perl) {
897 pverbose("Generating PERL\n");
898 t_perl_generator* perl = new t_perl_generator(program);
899 perl->generate_program();
900 delete perl;
901 }
902
Christopher Pirob97b89d2007-09-18 00:07:42 +0000903 if (gen_erl) {
904 pverbose("Generating Erlang\n");
905 t_erl_generator* erl = new t_erl_generator(program);
906 erl->generate_program();
907 delete erl;
908 }
909
David Reiss7f42bcf2008-01-11 20:59:12 +0000910 if (gen_csharp) {
911 pverbose("Generating C#\n");
912 t_csharp_generator* csharp = new t_csharp_generator(program);
913 csharp->generate_program();
914 delete csharp;
915 }
kholst76f2c882008-01-16 02:47:41 +0000916
David Reiss1ac05802007-07-30 22:00:27 +0000917 if (dump_docs) {
918 dump_docstrings(program);
919 }
David Reissbd0db882008-02-27 01:54:51 +0000920
921 vector<string>::const_iterator iter;
922 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
923 t_generator* generator = t_generator_registry::get_generator(program, *iter);
924
925 if (generator == NULL) {
926 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
927 } else {
928 pverbose("Generating \"%s\"\n", iter->c_str());
929 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000930 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000931 }
932 }
933
Mark Sleef0712dc2006-10-25 19:03:57 +0000934 } catch (string s) {
935 printf("Error: %s\n", s.c_str());
936 } catch (const char* exc) {
937 printf("Error: %s\n", exc);
938 }
939
940}
941
942/**
Mark Sleef5377b32006-10-10 01:42:59 +0000943 * Parse it up.. then spit it back out, in pretty much every language. Alright
944 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000945 */
946int main(int argc, char** argv) {
947 int i;
dweatherford65b70752007-10-31 02:18:14 +0000948 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000949
Mark Sleeb15a68b2006-06-07 06:46:24 +0000950 // Setup time string
951 time_t now = time(NULL);
952 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000953
Mark Sleef0712dc2006-10-25 19:03:57 +0000954 // Check for necessary arguments, you gotta have at least a filename and
955 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000956 if (argc < 2) {
957 usage();
958 }
Mark Slee31985722006-05-24 21:45:31 +0000959
David Reissbd0db882008-02-27 01:54:51 +0000960 vector<string> generator_strings;
961
David Reiss9cc2c132008-02-27 01:54:47 +0000962 // Set the current path to a dummy value to make warning messages clearer.
963 g_curpath = "arguments";
964
Mark Sleef5377b32006-10-10 01:42:59 +0000965 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000966 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000967 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000968
Mark Sleefdbee812006-09-27 18:50:48 +0000969 arg = strtok(argv[i], " ");
970 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000971 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000972 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000973 ++arg;
974 }
975
976 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000977 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000978 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000979 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000980 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000981 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000982 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000983 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000984 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000985 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000986 } else if (strcmp(arg, "-gen") == 0) {
987 arg = argv[++i];
988 if (arg == NULL) {
989 fprintf(stderr, "!!! Missing generator specification");
990 usage();
991 }
992 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000993 } else if (strcmp(arg, "-dense") == 0) {
994 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000995 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000996 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000997 } else if (strcmp(arg, "-javabean") == 0) {
998 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000999 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001000 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001001 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001002 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001003 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001004 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001005 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001006 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001007 gen_phps = true;
1008 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001009 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001010 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001011 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001012 gen_php = true;
1013 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001014 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001015 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001016 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001017 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001018 } else if (strcmp(arg, "-rest") == 0) {
1019 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001020 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001021 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001022 } else if (strcmp(arg, "-pyns") == 0) {
1023 gen_py = true;
1024 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001025 } else if (strcmp(arg, "-rb") == 0) {
1026 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001027 } else if (strcmp(arg, "-xsd") == 0) {
1028 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001029 } else if (strcmp(arg, "-perl") == 0) {
1030 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001031 } else if (strcmp(arg, "-erl") == 0) {
1032 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001033 } else if (strcmp(arg, "-ocaml") == 0) {
1034 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001035 } else if (strcmp(arg, "-hs") == 0) {
1036 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001037 } else if (strcmp(arg, "-cocoa") == 0) {
1038 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001039 } else if (strcmp(arg, "-st") == 0) {
1040 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001041 } else if (strcmp(arg, "-csharp") == 0) {
1042 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001043 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1044 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001045 } else if (strcmp(arg, "-I") == 0) {
1046 // An argument of "-I\ asdf" is invalid and has unknown results
1047 arg = argv[++i];
1048
1049 if (arg == NULL) {
1050 fprintf(stderr, "!!! Missing Include directory");
1051 usage();
1052 }
1053 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001054 } else if (strcmp(arg, "-o") == 0) {
1055 arg = argv[++i];
1056 if (arg == NULL) {
1057 fprintf(stderr, "-o: missing output directory");
1058 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001059 }
dweatherford65b70752007-10-31 02:18:14 +00001060 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001061
1062#ifdef MINGW
1063 //strip out trailing \ on Windows
1064 int last = out_path.length()-1;
1065 if (out_path[last] == '\\')
1066 {
1067 out_path.erase(last);
1068 }
1069#endif
1070
dweatherford65b70752007-10-31 02:18:14 +00001071 struct stat sb;
1072 if (stat(out_path.c_str(), &sb) < 0) {
1073 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1074 return -1;
1075 }
1076 if (! S_ISDIR(sb.st_mode)) {
1077 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1078 return -1;
1079 }
Mark Sleefdbee812006-09-27 18:50:48 +00001080 } else {
1081 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1082 usage();
1083 }
1084
1085 // Tokenize more
1086 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001087 }
1088 }
Mark Slee2c44d202007-05-16 02:18:07 +00001089
David Reiss400cd4b2008-02-27 01:54:55 +00001090 // TODO(dreiss): Delete these when everyone is using the new hotness.
1091 if (gen_cpp) {
1092 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1093 string gen_string = "cpp:";
1094 if (gen_dense) {
1095 gen_string.append("dense,");
1096 }
1097 if (g_cpp_use_include_prefix) {
1098 gen_string.append("include_prefix,");
1099 }
1100 generator_strings.push_back(gen_string);
1101 }
David Reisse1404d22008-02-27 01:55:05 +00001102 if (gen_java) {
1103 pwarning(1, "-java is deprecated. Use --gen java");
1104 generator_strings.push_back("java");
1105 }
1106 if (gen_javabean) {
1107 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1108 generator_strings.push_back("java:beans");
1109 }
David Reissd01c64d2008-03-27 21:40:55 +00001110 if (gen_cocoa) {
1111 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1112 generator_strings.push_back("cocoa");
1113 }
David Reissa890b572008-03-27 21:40:35 +00001114 if (gen_st) {
1115 pwarning(1, "-st is deprecated. Use --gen st");
1116 generator_strings.push_back("st");
1117 }
David Reissf0521b12008-03-27 21:40:05 +00001118 if (gen_ocaml) {
1119 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1120 generator_strings.push_back("ocaml");
1121 }
David Reissaf3ab262008-03-27 21:40:16 +00001122 if (gen_hs) {
1123 pwarning(1, "-hs is deprecated. Use --gen hs");
1124 generator_strings.push_back("hs");
1125 }
David Reisse1404d22008-02-27 01:55:05 +00001126
Mark Sleef0712dc2006-10-25 19:03:57 +00001127 // You gotta generate something!
David Reissd01c64d2008-03-27 21:40:55 +00001128 if (!gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_erl && !gen_csharp && generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001129 fprintf(stderr, "!!! No output language(s) specified\n\n");
1130 usage();
1131 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001132
1133 // Real-pathify it
1134 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +00001135 if (saferealpath(argv[i], rp) == NULL) {
1136 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001137 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001138 string input_file(rp);
1139
Mark Sleef5377b32006-10-10 01:42:59 +00001140 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001141 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001142 if (out_path.size()) {
1143 program->set_out_path(out_path);
1144 }
kholst76f2c882008-01-16 02:47:41 +00001145
David Reiss4b6a3c72008-02-27 22:28:12 +00001146 // Compute the cpp include prefix.
1147 // infer this from the filename passed in
1148 string input_filename = argv[i];
1149 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001150
David Reiss4b6a3c72008-02-27 22:28:12 +00001151 string::size_type last_slash = string::npos;
1152 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1153 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001154 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001155
David Reiss4b6a3c72008-02-27 22:28:12 +00001156 program->set_include_prefix(include_prefix);
1157
Mark Sleef0712dc2006-10-25 19:03:57 +00001158 // Initialize global types
1159 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1160 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001161 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1162 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001163 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1164 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001165 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1166 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1167 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1168 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1169 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1170 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001171
Mark Sleef5377b32006-10-10 01:42:59 +00001172 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001173 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001174
David Reiss9cc2c132008-02-27 01:54:47 +00001175 // The current path is not really relevant when we are doing generation.
1176 // Reset the variable to make warning messages clearer.
1177 g_curpath = "generation";
1178 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1179 // That is what shows up during argument parsing.
1180 yylineno = 1;
1181
Mark Sleef0712dc2006-10-25 19:03:57 +00001182 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001183 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001184
Mark Sleef0712dc2006-10-25 19:03:57 +00001185 // Clean up. Who am I kidding... this program probably orphans heap memory
1186 // all over the place, but who cares because it is about to exit and it is
1187 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001188
Mark Sleef0712dc2006-10-25 19:03:57 +00001189 delete program;
1190 delete g_type_void;
1191 delete g_type_string;
1192 delete g_type_bool;
1193 delete g_type_byte;
1194 delete g_type_i16;
1195 delete g_type_i32;
1196 delete g_type_i64;
1197 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001198
1199 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001200 return 0;
1201}