blob: 80d39606094915591f69fe4baf36dd6ff8830af5 [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>
David Reiss739cbe22008-04-15 05:44:00 +000023#include <algorithm>
Mark Sleef0712dc2006-10-25 19:03:57 +000024#include <sys/types.h>
25#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000026#include <errno.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 */
30# include <limits.h>
31#endif
32
Mark Sleef0712dc2006-10-25 19:03:57 +000033// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000034#include "globals.h"
35
36#include "main.h"
37#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000038#include "parse/t_scope.h"
Mark Slee6e536442006-06-30 18:28:50 +000039#include "generate/t_php_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000040#include "generate/t_xsd_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000041#include "generate/t_erl_generator.h"
David Reiss76f0d112008-06-10 22:57:35 +000042#include "generate/t_alterl_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000043
44using namespace std;
45
Mark Sleef5377b32006-10-10 01:42:59 +000046/**
47 * Global program tree
48 */
Mark Slee31985722006-05-24 21:45:31 +000049t_program* g_program;
50
Mark Sleef5377b32006-10-10 01:42:59 +000051/**
Mark Sleef0712dc2006-10-25 19:03:57 +000052 * Global types
53 */
54
55t_type* g_type_void;
56t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000057t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000058t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000059t_type* g_type_bool;
60t_type* g_type_byte;
61t_type* g_type_i16;
62t_type* g_type_i32;
63t_type* g_type_i64;
64t_type* g_type_double;
65
66/**
67 * Global scope
68 */
69t_scope* g_scope;
70
71/**
72 * Parent scope to also parse types
73 */
74t_scope* g_parent_scope;
75
76/**
77 * Prefix for putting types in parent scope
78 */
79string g_parent_prefix;
80
81/**
82 * Parsing pass
83 */
84PARSE_MODE g_parse_mode;
85
86/**
87 * Current directory of file being parsed
88 */
89string g_curdir;
90
91/**
92 * Current file being parsed
93 */
94string g_curpath;
95
96/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000097 * Search path for inclusions
98 */
Mark Slee2329a832006-11-09 00:23:30 +000099vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000100
101/**
kholst76f2c882008-01-16 02:47:41 +0000102 * Should C++ include statements use path prefixes for other thrift-generated
103 * header files
104 */
105bool g_cpp_use_include_prefix = false;
106
107/**
Mark Sleef5377b32006-10-10 01:42:59 +0000108 * Global debug state
109 */
Mark Slee31985722006-05-24 21:45:31 +0000110int g_debug = 0;
111
Mark Sleef5377b32006-10-10 01:42:59 +0000112/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000113 * Warning level
114 */
115int g_warn = 1;
116
117/**
118 * Verbose output
119 */
120int g_verbose = 0;
121
122/**
Mark Sleef5377b32006-10-10 01:42:59 +0000123 * Global time string
124 */
Mark Slee31985722006-05-24 21:45:31 +0000125char* g_time_str;
126
Mark Slee31985722006-05-24 21:45:31 +0000127/**
David Reisscbd4bac2007-08-14 17:12:33 +0000128 * The last parsed doctext comment.
129 */
130char* g_doctext;
131
132/**
133 * The location of the last parsed doctext comment.
134 */
135int g_doctext_lineno;
136
137/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000138 * Flags to control code generation
139 */
140bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000141bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000142bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000143bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000144bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000145bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000146bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000147bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000148bool gen_php = false;
149bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000150bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000151bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000152bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000153bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000154bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000155bool gen_erl = false;
David Reiss76f0d112008-06-10 22:57:35 +0000156bool gen_alterl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000157bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000158bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000159bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000160bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000161bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000162bool gen_recurse = false;
163
164/**
David Reiss204420f2008-01-11 20:59:03 +0000165 * MinGW doesn't have realpath, so use fallback implementation in that case,
166 * otherwise this just calls through to realpath
167 */
168char *saferealpath(const char *path, char *resolved_path) {
169#ifdef MINGW
170 char buf[MAX_PATH];
171 char* basename;
172 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
173 if (len == 0 || len > MAX_PATH - 1){
174 strcpy(resolved_path, path);
175 } else {
176 CharLowerBuff(buf, len);
177 strcpy(resolved_path, buf);
178 }
179 return resolved_path;
180#else
181 return realpath(path, resolved_path);
182#endif
183}
184
185
186/**
Mark Slee31985722006-05-24 21:45:31 +0000187 * Report an error to the user. This is called yyerror for historical
188 * reasons (lex and yacc expect the error reporting routine to be called
189 * this). Call this function to report any errors to the user.
190 * yyerror takes printf style arguments.
191 *
192 * @param fmt C format string followed by additional arguments
193 */
David Reiss0babe402008-06-10 22:56:12 +0000194void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000195 va_list args;
196 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000197 "[ERROR:%s:%d] (last token was '%s')\n",
198 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000199 yylineno,
200 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000201
202 va_start(args, fmt);
203 vfprintf(stderr, fmt, args);
204 va_end(args);
205
206 fprintf(stderr, "\n");
207}
208
209/**
210 * Prints a debug message from the parser.
211 *
212 * @param fmt C format string followed by additional arguments
213 */
David Reiss0babe402008-06-10 22:56:12 +0000214void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000215 if (g_debug == 0) {
216 return;
217 }
218 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000219 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000220 va_start(args, fmt);
221 vprintf(fmt, args);
222 va_end(args);
223 printf("\n");
224}
225
226/**
227 * Prints a verbose output mode message
228 *
229 * @param fmt C format string followed by additional arguments
230 */
David Reiss0babe402008-06-10 22:56:12 +0000231void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 if (g_verbose == 0) {
233 return;
234 }
235 va_list args;
236 va_start(args, fmt);
237 vprintf(fmt, args);
238 va_end(args);
239}
240
241/**
242 * Prints a warning message
243 *
244 * @param fmt C format string followed by additional arguments
245 */
David Reiss0babe402008-06-10 22:56:12 +0000246void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000247 if (g_warn < level) {
248 return;
249 }
250 va_list args;
251 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000252 va_start(args, fmt);
253 vprintf(fmt, args);
254 va_end(args);
255 printf("\n");
256}
257
258/**
259 * Prints a failure message and exits
260 *
261 * @param fmt C format string followed by additional arguments
262 */
Mark Slee30152872006-11-28 01:24:07 +0000263void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000264 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000265 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000266 va_start(args, fmt);
267 vfprintf(stderr, fmt, args);
268 va_end(args);
269 printf("\n");
270 exit(1);
271}
272
273/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000274 * Converts a string filename into a thrift program name
275 */
276string program_name(string filename) {
277 string::size_type slash = filename.rfind("/");
278 if (slash != string::npos) {
279 filename = filename.substr(slash+1);
280 }
281 string::size_type dot = filename.rfind(".");
282 if (dot != string::npos) {
283 filename = filename.substr(0, dot);
284 }
285 return filename;
286}
287
288/**
289 * Gets the directory path of a filename
290 */
291string directory_name(string filename) {
292 string::size_type slash = filename.rfind("/");
293 // No slash, just use the current directory
294 if (slash == string::npos) {
295 return ".";
296 }
297 return filename.substr(0, slash);
298}
299
300/**
301 * Finds the appropriate file path for the given filename
302 */
303string include_file(string filename) {
304 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000305 if (filename[0] == '/') {
306 // Realpath!
307 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000308 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000309 pwarning(0, "Cannot open include file %s\n", filename.c_str());
310 return std::string();
311 }
Mark Slee2c44d202007-05-16 02:18:07 +0000312
313 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000314 struct stat finfo;
315 if (stat(rp, &finfo) == 0) {
316 return rp;
317 }
318 } else { // relative path, start searching
319 // new search path with current dir global
320 vector<string> sp = g_incl_searchpath;
321 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000322
Martin Kraemer32c66e12006-11-09 00:06:36 +0000323 // iterate through paths
324 vector<string>::iterator it;
325 for (it = sp.begin(); it != sp.end(); it++) {
326 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000327
Martin Kraemer32c66e12006-11-09 00:06:36 +0000328 // Realpath!
329 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000330 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000331 continue;
332 }
Mark Slee2c44d202007-05-16 02:18:07 +0000333
Martin Kraemer32c66e12006-11-09 00:06:36 +0000334 // Stat this files
335 struct stat finfo;
336 if (stat(rp, &finfo) == 0) {
337 return rp;
338 }
339 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000340 }
Mark Slee2c44d202007-05-16 02:18:07 +0000341
Mark Sleef0712dc2006-10-25 19:03:57 +0000342 // Uh oh
343 pwarning(0, "Could not find include file %s\n", filename.c_str());
344 return std::string();
345}
346
347/**
David Reisscbd4bac2007-08-14 17:12:33 +0000348 * Clears any previously stored doctext string.
349 * Also prints a warning if we are discarding information.
350 */
351void clear_doctext() {
352 if (g_doctext != NULL) {
353 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
354 }
355 free(g_doctext);
356 g_doctext = NULL;
357}
358
359/**
David Reiss1ac05802007-07-30 22:00:27 +0000360 * Cleans up text commonly found in doxygen-like comments
361 *
362 * Warning: if you mix tabs and spaces in a non-uniform way,
363 * you will get what you deserve.
364 */
365char* clean_up_doctext(char* doctext) {
366 // Convert to C++ string, and remove Windows's carriage returns.
367 string docstring = doctext;
368 docstring.erase(
369 remove(docstring.begin(), docstring.end(), '\r'),
370 docstring.end());
371
372 // Separate into lines.
373 vector<string> lines;
374 string::size_type pos = string::npos;
375 string::size_type last;
376 while (true) {
377 last = (pos == string::npos) ? 0 : pos+1;
378 pos = docstring.find('\n', last);
379 if (pos == string::npos) {
380 // First bit of cleaning. If the last line is only whitespace, drop it.
381 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
382 if (nonwhite != string::npos) {
383 lines.push_back(docstring.substr(last));
384 }
385 break;
386 }
387 lines.push_back(docstring.substr(last, pos-last));
388 }
389
390 // A very profound docstring.
391 if (lines.empty()) {
392 return NULL;
393 }
394
395 // Clear leading whitespace from the first line.
396 pos = lines.front().find_first_not_of(" \t");
397 lines.front().erase(0, pos);
398
399 // If every nonblank line after the first has the same number of spaces/tabs,
400 // then a star, remove them.
401 bool have_prefix = true;
402 bool found_prefix = false;
403 string::size_type prefix_len = 0;
404 vector<string>::iterator l_iter;
405 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
406 if (l_iter->empty()) {
407 continue;
408 }
409
410 pos = l_iter->find_first_not_of(" \t");
411 if (!found_prefix) {
412 if (pos != string::npos) {
413 if (l_iter->at(pos) == '*') {
414 found_prefix = true;
415 prefix_len = pos;
416 } else {
417 have_prefix = false;
418 break;
419 }
420 } else {
421 // Whitespace-only line. Truncate it.
422 l_iter->clear();
423 }
424 } else if (l_iter->size() > pos
425 && l_iter->at(pos) == '*'
426 && pos == prefix_len) {
427 // Business as usual.
428 } else if (pos == string::npos) {
429 // Whitespace-only line. Let's truncate it for them.
430 l_iter->clear();
431 } else {
432 // The pattern has been broken.
433 have_prefix = false;
434 break;
435 }
436 }
437
438 // If our prefix survived, delete it from every line.
439 if (have_prefix) {
440 // Get the star too.
441 prefix_len++;
442 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
443 l_iter->erase(0, prefix_len);
444 }
445 }
446
447 // Now delete the minimum amount of leading whitespace from each line.
448 prefix_len = string::npos;
449 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
450 if (l_iter->empty()) {
451 continue;
452 }
453 pos = l_iter->find_first_not_of(" \t");
454 if (pos != string::npos
455 && (prefix_len == string::npos || pos < prefix_len)) {
456 prefix_len = pos;
457 }
458 }
459
460 // If our prefix survived, delete it from every line.
461 if (prefix_len != string::npos) {
462 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
463 l_iter->erase(0, prefix_len);
464 }
465 }
466
467 // Remove trailing whitespace from every line.
468 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
469 pos = l_iter->find_last_not_of(" \t");
470 if (pos != string::npos && pos != l_iter->length()-1) {
471 l_iter->erase(pos+1);
472 }
473 }
474
475 // If the first line is empty, remove it.
476 // Don't do this earlier because a lot of steps skip the first line.
477 if (lines.front().empty()) {
478 lines.erase(lines.begin());
479 }
480
481 // Now rejoin the lines and copy them back into doctext.
482 docstring.clear();
483 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
484 docstring += *l_iter;
485 docstring += '\n';
486 }
487
488 assert(docstring.length() <= strlen(doctext));
489 strcpy(doctext, docstring.c_str());
490 return doctext;
491}
492
493/** Set to true to debug docstring parsing */
494static bool dump_docs = false;
495
496/**
497 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000498 * Only works for top-level definitions and the whole program doc
499 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000500 */
501void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000502 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000503 if (!progdoc.empty()) {
504 printf("Whole program doc:\n%s\n", progdoc.c_str());
505 }
David Reiss1ac05802007-07-30 22:00:27 +0000506 const vector<t_typedef*>& typedefs = program->get_typedefs();
507 vector<t_typedef*>::const_iterator t_iter;
508 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
509 t_typedef* td = *t_iter;
510 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000511 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
512 }
513 }
514 const vector<t_enum*>& enums = program->get_enums();
515 vector<t_enum*>::const_iterator e_iter;
516 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
517 t_enum* en = *e_iter;
518 if (en->has_doc()) {
519 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
520 }
521 }
522 const vector<t_const*>& consts = program->get_consts();
523 vector<t_const*>::const_iterator c_iter;
524 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
525 t_const* co = *c_iter;
526 if (co->has_doc()) {
527 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
528 }
529 }
530 const vector<t_struct*>& structs = program->get_structs();
531 vector<t_struct*>::const_iterator s_iter;
532 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
533 t_struct* st = *s_iter;
534 if (st->has_doc()) {
535 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
536 }
537 }
538 const vector<t_struct*>& xceptions = program->get_xceptions();
539 vector<t_struct*>::const_iterator x_iter;
540 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
541 t_struct* xn = *x_iter;
542 if (xn->has_doc()) {
543 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
544 }
545 }
546 const vector<t_service*>& services = program->get_services();
547 vector<t_service*>::const_iterator v_iter;
548 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
549 t_service* sv = *v_iter;
550 if (sv->has_doc()) {
551 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000552 }
553 }
554}
555
556/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000557 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000558 */
559void generate_all_fingerprints(t_program* program) {
560 const vector<t_struct*>& structs = program->get_structs();
561 vector<t_struct*>::const_iterator s_iter;
562 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
563 t_struct* st = *s_iter;
564 st->generate_fingerprint();
565 }
566
David Reissd779cbe2007-08-31 01:42:55 +0000567 const vector<t_struct*>& xceptions = program->get_xceptions();
568 vector<t_struct*>::const_iterator x_iter;
569 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
570 t_struct* st = *x_iter;
571 st->generate_fingerprint();
572 }
573
David Reiss3c5d2fd2008-02-08 21:58:06 +0000574 const vector<t_enum*>& enums = program->get_enums();
575 vector<t_enum*>::const_iterator e_iter;
576 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
577 t_enum* e = *e_iter;
578 e->generate_fingerprint();
579 }
580
David Reiss47557bc2007-09-04 21:31:04 +0000581 g_type_void->generate_fingerprint();
582
David Reiss18bf22d2007-08-28 20:49:17 +0000583 // If you want to generate fingerprints for implicit structures, start here.
584 /*
585 const vector<t_service*>& services = program->get_services();
586 vector<t_service*>::const_iterator v_iter;
587 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
588 t_service* sv = *v_iter;
589 }
590 */
591}
592
593/**
Mark Slee31985722006-05-24 21:45:31 +0000594 * Diplays the usage message and then exits with an error code.
595 */
596void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000597 fprintf(stderr, "Usage: thrift [options] file\n");
598 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000599 fprintf(stderr, " -php Generate PHP output files\n");
600 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000601 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
602 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000603 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000604 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000605 fprintf(stderr, " -xsd Generate XSD output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000606 fprintf(stderr, " -erl Generate Erlang output files\n");
David Reiss76f0d112008-06-10 22:57:35 +0000607 fprintf(stderr, " -alterl Generate Alternative Erlang output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000608 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
609 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000610 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000611 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000612 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000613 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
614 fprintf(stderr, " -strict Strict compiler warnings on\n");
615 fprintf(stderr, " -v[erbose] Verbose mode\n");
616 fprintf(stderr, " -r[ecurse] Also generate included files\n");
617 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000618 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
619 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
620 fprintf(stderr, " Keys and values are options passed to the generator.\n");
621 fprintf(stderr, " Many options will not require values.\n");
622 fprintf(stderr, "\n");
623 fprintf(stderr, "Available generators (and options):\n");
624
625 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
626 t_generator_registry::gen_map_t::iterator iter;
627 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
628 fprintf(stderr, " %s (%s):\n",
629 iter->second->get_short_name().c_str(),
630 iter->second->get_long_name().c_str());
631 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
632 }
Mark Slee31985722006-05-24 21:45:31 +0000633 exit(1);
634}
635
636/**
Mark Slee30152872006-11-28 01:24:07 +0000637 * You know, when I started working on Thrift I really thought it wasn't going
638 * to become a programming language because it was just a generator and it
639 * wouldn't need runtime type information and all that jazz. But then we
640 * decided to add constants, and all of a sudden that means runtime type
641 * validation and inference, except the "runtime" is the code generator
642 * runtime. Shit. I've been had.
643 */
644void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
645 if (type->is_void()) {
646 throw "type error: cannot declare a void const: " + name;
647 }
648
649 if (type->is_base_type()) {
650 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
651 switch (tbase) {
652 case t_base_type::TYPE_STRING:
653 if (value->get_type() != t_const_value::CV_STRING) {
654 throw "type error: const \"" + name + "\" was declared as string";
655 }
656 break;
657 case t_base_type::TYPE_BOOL:
658 if (value->get_type() != t_const_value::CV_INTEGER) {
659 throw "type error: const \"" + name + "\" was declared as bool";
660 }
661 break;
662 case t_base_type::TYPE_BYTE:
663 if (value->get_type() != t_const_value::CV_INTEGER) {
664 throw "type error: const \"" + name + "\" was declared as byte";
665 }
666 break;
667 case t_base_type::TYPE_I16:
668 if (value->get_type() != t_const_value::CV_INTEGER) {
669 throw "type error: const \"" + name + "\" was declared as i16";
670 }
671 break;
672 case t_base_type::TYPE_I32:
673 if (value->get_type() != t_const_value::CV_INTEGER) {
674 throw "type error: const \"" + name + "\" was declared as i32";
675 }
676 break;
677 case t_base_type::TYPE_I64:
678 if (value->get_type() != t_const_value::CV_INTEGER) {
679 throw "type error: const \"" + name + "\" was declared as i64";
680 }
681 break;
682 case t_base_type::TYPE_DOUBLE:
683 if (value->get_type() != t_const_value::CV_INTEGER &&
684 value->get_type() != t_const_value::CV_DOUBLE) {
685 throw "type error: const \"" + name + "\" was declared as double";
686 }
687 break;
688 default:
David Reissdd7796f2007-08-28 21:09:06 +0000689 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000690 }
691 } else if (type->is_enum()) {
692 if (value->get_type() != t_const_value::CV_INTEGER) {
693 throw "type error: const \"" + name + "\" was declared as enum";
694 }
695 } else if (type->is_struct() || type->is_xception()) {
696 if (value->get_type() != t_const_value::CV_MAP) {
697 throw "type error: const \"" + name + "\" was declared as struct/xception";
698 }
699 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
700 vector<t_field*>::const_iterator f_iter;
701
702 const map<t_const_value*, t_const_value*>& val = value->get_map();
703 map<t_const_value*, t_const_value*>::const_iterator v_iter;
704 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
705 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
706 throw "type error: " + name + " struct key must be string";
707 }
708 t_type* field_type = NULL;
709 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
710 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
711 field_type = (*f_iter)->get_type();
712 }
713 }
714 if (field_type == NULL) {
715 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
716 }
717
718 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
719 }
720 } else if (type->is_map()) {
721 t_type* k_type = ((t_map*)type)->get_key_type();
722 t_type* v_type = ((t_map*)type)->get_val_type();
723 const map<t_const_value*, t_const_value*>& val = value->get_map();
724 map<t_const_value*, t_const_value*>::const_iterator v_iter;
725 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
726 validate_const_rec(name + "<key>", k_type, v_iter->first);
727 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000728 }
Mark Slee30152872006-11-28 01:24:07 +0000729 } else if (type->is_list() || type->is_set()) {
730 t_type* e_type;
731 if (type->is_list()) {
732 e_type = ((t_list*)type)->get_elem_type();
733 } else {
734 e_type = ((t_set*)type)->get_elem_type();
735 }
736 const vector<t_const_value*>& val = value->get_list();
737 vector<t_const_value*>::const_iterator v_iter;
738 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
739 validate_const_rec(name + "<elem>", e_type, *v_iter);
740 }
741 }
742}
743
744/**
745 * Check the type of the parsed const information against its declared type
746 */
747void validate_const_type(t_const* c) {
748 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
749}
750
751/**
Mark Slee7ff32452007-02-01 05:26:18 +0000752 * Check the type of a default value assigned to a field.
753 */
754void validate_field_value(t_field* field, t_const_value* cv) {
755 validate_const_rec(field->get_name(), field->get_type(), cv);
756}
757
758/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000759 * Check that all the elements of a throws block are actually exceptions.
760 */
761bool validate_throws(t_struct* throws) {
762 const vector<t_field*>& members = throws->get_members();
763 vector<t_field*>::const_iterator m_iter;
764 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
765 if (!(*m_iter)->get_type()->is_xception()) {
766 return false;
767 }
768 }
769 return true;
770}
771
772/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000773 * Parses a program
774 */
Mark Slee2c44d202007-05-16 02:18:07 +0000775void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000776 // Get scope file path
777 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000778
Mark Sleef0712dc2006-10-25 19:03:57 +0000779 // Set current dir global, which is used in the include_file function
780 g_curdir = directory_name(path);
781 g_curpath = path;
782
783 // Open the file
784 yyin = fopen(path.c_str(), "r");
785 if (yyin == 0) {
786 failure("Could not open input file: \"%s\"", path.c_str());
787 }
788
789 // Create new scope and scan for includes
790 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000791 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000792 g_program = program;
793 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000794 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000795 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000796 if (yyparse() != 0) {
797 failure("Parser error during include pass.");
798 }
799 } catch (string x) {
800 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000801 }
802 fclose(yyin);
803
804 // Recursively parse all the include programs
805 vector<t_program*>& includes = program->get_includes();
806 vector<t_program*>::iterator iter;
807 for (iter = includes.begin(); iter != includes.end(); ++iter) {
808 parse(*iter, program);
809 }
810
David Reiss204420f2008-01-11 20:59:03 +0000811 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000812 g_parse_mode = PROGRAM;
813 g_program = program;
814 g_scope = program->scope();
815 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
816 g_parent_prefix = program->get_name() + ".";
817 g_curpath = path;
818 yyin = fopen(path.c_str(), "r");
819 if (yyin == 0) {
820 failure("Could not open input file: \"%s\"", path.c_str());
821 }
822 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000823 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000824 try {
825 if (yyparse() != 0) {
826 failure("Parser error during types pass.");
827 }
828 } catch (string x) {
829 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000830 }
831 fclose(yyin);
832}
833
834/**
835 * Generate code
836 */
David Reissbd0db882008-02-27 01:54:51 +0000837void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000838 // Oooohh, recursive code generation, hot!!
839 if (gen_recurse) {
840 const vector<t_program*>& includes = program->get_includes();
841 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000842 // Propogate output path from parent to child programs
843 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000844
David Reissbd0db882008-02-27 01:54:51 +0000845 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000846 }
847 }
848
849 // Generate code!
850 try {
851 pverbose("Program: %s\n", program->get_path().c_str());
852
David Reiss18bf22d2007-08-28 20:49:17 +0000853 // Compute fingerprints.
854 generate_all_fingerprints(program);
855
Mark Sleef0712dc2006-10-25 19:03:57 +0000856 if (gen_php) {
857 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000858 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 +0000859 php->generate_program();
860 delete php;
861 }
862
863 if (gen_phpi) {
864 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000865 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000866 phpi->generate_program();
867 delete phpi;
868 }
869
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000870 if (gen_xsd) {
871 pverbose("Generating XSD\n");
872 t_xsd_generator* xsd = new t_xsd_generator(program);
873 xsd->generate_program();
874 delete xsd;
875 }
876
Christopher Pirob97b89d2007-09-18 00:07:42 +0000877 if (gen_erl) {
878 pverbose("Generating Erlang\n");
879 t_erl_generator* erl = new t_erl_generator(program);
880 erl->generate_program();
881 delete erl;
882 }
883
David Reiss76f0d112008-06-10 22:57:35 +0000884 if (gen_alterl) {
885 pverbose("Generating Alternative Erlang\n");
886 t_alterl_generator* alterl = new t_alterl_generator(program);
887 alterl->generate_program();
888 delete alterl;
889 }
890
David Reiss1ac05802007-07-30 22:00:27 +0000891 if (dump_docs) {
892 dump_docstrings(program);
893 }
David Reissbd0db882008-02-27 01:54:51 +0000894
895 vector<string>::const_iterator iter;
896 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
897 t_generator* generator = t_generator_registry::get_generator(program, *iter);
898
899 if (generator == NULL) {
900 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
901 } else {
902 pverbose("Generating \"%s\"\n", iter->c_str());
903 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000904 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000905 }
906 }
907
Mark Sleef0712dc2006-10-25 19:03:57 +0000908 } catch (string s) {
909 printf("Error: %s\n", s.c_str());
910 } catch (const char* exc) {
911 printf("Error: %s\n", exc);
912 }
913
914}
915
916/**
Mark Sleef5377b32006-10-10 01:42:59 +0000917 * Parse it up.. then spit it back out, in pretty much every language. Alright
918 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000919 */
920int main(int argc, char** argv) {
921 int i;
dweatherford65b70752007-10-31 02:18:14 +0000922 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000923
Mark Sleeb15a68b2006-06-07 06:46:24 +0000924 // Setup time string
925 time_t now = time(NULL);
926 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000927
Mark Sleef0712dc2006-10-25 19:03:57 +0000928 // Check for necessary arguments, you gotta have at least a filename and
929 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000930 if (argc < 2) {
931 usage();
932 }
Mark Slee31985722006-05-24 21:45:31 +0000933
David Reissbd0db882008-02-27 01:54:51 +0000934 vector<string> generator_strings;
935
David Reiss9cc2c132008-02-27 01:54:47 +0000936 // Set the current path to a dummy value to make warning messages clearer.
937 g_curpath = "arguments";
938
Mark Sleef5377b32006-10-10 01:42:59 +0000939 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000940 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000941 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000942
Mark Sleefdbee812006-09-27 18:50:48 +0000943 arg = strtok(argv[i], " ");
944 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000945 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000946 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000947 ++arg;
948 }
949
950 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000951 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000952 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000953 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000954 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000955 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000956 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000957 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000958 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000959 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000960 } else if (strcmp(arg, "-gen") == 0) {
961 arg = argv[++i];
962 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000963 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000964 usage();
965 }
966 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000967 } else if (strcmp(arg, "-dense") == 0) {
968 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000969 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000970 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000971 } else if (strcmp(arg, "-javabean") == 0) {
972 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000973 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000974 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000975 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000976 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000977 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000978 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000979 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000980 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000981 gen_phps = true;
982 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000983 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000984 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000985 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000986 gen_php = true;
987 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000988 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000989 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000990 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000991 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000992 } else if (strcmp(arg, "-rest") == 0) {
993 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000994 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000995 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +0000996 } else if (strcmp(arg, "-pyns") == 0) {
997 gen_py = true;
998 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000999 } else if (strcmp(arg, "-rb") == 0) {
1000 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001001 } else if (strcmp(arg, "-xsd") == 0) {
1002 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001003 } else if (strcmp(arg, "-perl") == 0) {
1004 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +00001005 } else if (strcmp(arg, "-erl") == 0) {
1006 gen_erl = true;
David Reiss76f0d112008-06-10 22:57:35 +00001007 } else if (strcmp(arg, "-alterl") == 0) {
1008 gen_alterl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001009 } else if (strcmp(arg, "-ocaml") == 0) {
1010 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001011 } else if (strcmp(arg, "-hs") == 0) {
1012 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001013 } else if (strcmp(arg, "-cocoa") == 0) {
1014 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001015 } else if (strcmp(arg, "-st") == 0) {
1016 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001017 } else if (strcmp(arg, "-csharp") == 0) {
1018 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001019 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1020 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001021 } else if (strcmp(arg, "-I") == 0) {
1022 // An argument of "-I\ asdf" is invalid and has unknown results
1023 arg = argv[++i];
1024
1025 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001026 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001027 usage();
1028 }
1029 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001030 } else if (strcmp(arg, "-o") == 0) {
1031 arg = argv[++i];
1032 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001033 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001034 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001035 }
dweatherford65b70752007-10-31 02:18:14 +00001036 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001037
1038#ifdef MINGW
1039 //strip out trailing \ on Windows
1040 int last = out_path.length()-1;
1041 if (out_path[last] == '\\')
1042 {
1043 out_path.erase(last);
1044 }
1045#endif
1046
dweatherford65b70752007-10-31 02:18:14 +00001047 struct stat sb;
1048 if (stat(out_path.c_str(), &sb) < 0) {
1049 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1050 return -1;
1051 }
1052 if (! S_ISDIR(sb.st_mode)) {
1053 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1054 return -1;
1055 }
Mark Sleefdbee812006-09-27 18:50:48 +00001056 } else {
1057 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1058 usage();
1059 }
1060
1061 // Tokenize more
1062 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001063 }
1064 }
Mark Slee2c44d202007-05-16 02:18:07 +00001065
David Reiss400cd4b2008-02-27 01:54:55 +00001066 // TODO(dreiss): Delete these when everyone is using the new hotness.
1067 if (gen_cpp) {
1068 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1069 string gen_string = "cpp:";
1070 if (gen_dense) {
1071 gen_string.append("dense,");
1072 }
1073 if (g_cpp_use_include_prefix) {
1074 gen_string.append("include_prefix,");
1075 }
1076 generator_strings.push_back(gen_string);
1077 }
David Reisse1404d22008-02-27 01:55:05 +00001078 if (gen_java) {
1079 pwarning(1, "-java is deprecated. Use --gen java");
1080 generator_strings.push_back("java");
1081 }
1082 if (gen_javabean) {
1083 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1084 generator_strings.push_back("java:beans");
1085 }
David Reiss861869b2008-03-27 21:41:23 +00001086 if (gen_csharp) {
1087 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1088 generator_strings.push_back("csharp");
1089 }
David Reiss558e3992008-03-27 21:41:40 +00001090 if (gen_py) {
1091 pwarning(1, "-py is deprecated. Use --gen py");
1092 generator_strings.push_back("py");
1093 }
David Reissa640cea2008-03-27 21:42:01 +00001094 if (gen_rb) {
1095 pwarning(1, "-rb is deprecated. Use --gen rb");
1096 generator_strings.push_back("rb");
1097 }
David Reiss2b386c52008-03-27 21:42:23 +00001098 if (gen_perl) {
1099 pwarning(1, "-perl is deprecated. Use --gen perl");
1100 generator_strings.push_back("perl");
1101 }
David Reissd01c64d2008-03-27 21:40:55 +00001102 if (gen_cocoa) {
1103 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1104 generator_strings.push_back("cocoa");
1105 }
David Reissa890b572008-03-27 21:40:35 +00001106 if (gen_st) {
1107 pwarning(1, "-st is deprecated. Use --gen st");
1108 generator_strings.push_back("st");
1109 }
David Reissf0521b12008-03-27 21:40:05 +00001110 if (gen_ocaml) {
1111 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1112 generator_strings.push_back("ocaml");
1113 }
David Reissaf3ab262008-03-27 21:40:16 +00001114 if (gen_hs) {
1115 pwarning(1, "-hs is deprecated. Use --gen hs");
1116 generator_strings.push_back("hs");
1117 }
David Reisse1404d22008-02-27 01:55:05 +00001118
Mark Sleef0712dc2006-10-25 19:03:57 +00001119 // You gotta generate something!
David Reiss76f0d112008-06-10 22:57:35 +00001120 if (!gen_php && !gen_phpi && !gen_xsd && !gen_erl && !gen_alterl && generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001121 fprintf(stderr, "!!! No output language(s) specified\n\n");
1122 usage();
1123 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001124
1125 // Real-pathify it
1126 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001127 if (argv[i] == NULL) {
1128 fprintf(stderr, "!!! Missing file name\n");
1129 usage();
1130 }
David Reiss204420f2008-01-11 20:59:03 +00001131 if (saferealpath(argv[i], rp) == NULL) {
1132 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001133 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001134 string input_file(rp);
1135
Mark Sleef5377b32006-10-10 01:42:59 +00001136 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001137 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001138 if (out_path.size()) {
1139 program->set_out_path(out_path);
1140 }
kholst76f2c882008-01-16 02:47:41 +00001141
David Reiss4b6a3c72008-02-27 22:28:12 +00001142 // Compute the cpp include prefix.
1143 // infer this from the filename passed in
1144 string input_filename = argv[i];
1145 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001146
David Reiss4b6a3c72008-02-27 22:28:12 +00001147 string::size_type last_slash = string::npos;
1148 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1149 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001150 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001151
David Reiss4b6a3c72008-02-27 22:28:12 +00001152 program->set_include_prefix(include_prefix);
1153
Mark Sleef0712dc2006-10-25 19:03:57 +00001154 // Initialize global types
1155 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1156 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001157 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1158 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001159 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1160 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001161 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1162 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1163 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1164 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1165 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1166 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001167
Mark Sleef5377b32006-10-10 01:42:59 +00001168 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001169 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001170
David Reiss9cc2c132008-02-27 01:54:47 +00001171 // The current path is not really relevant when we are doing generation.
1172 // Reset the variable to make warning messages clearer.
1173 g_curpath = "generation";
1174 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1175 // That is what shows up during argument parsing.
1176 yylineno = 1;
1177
Mark Sleef0712dc2006-10-25 19:03:57 +00001178 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001179 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001180
Mark Sleef0712dc2006-10-25 19:03:57 +00001181 // Clean up. Who am I kidding... this program probably orphans heap memory
1182 // all over the place, but who cares because it is about to exit and it is
1183 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001184
Mark Sleef0712dc2006-10-25 19:03:57 +00001185 delete program;
1186 delete g_type_void;
1187 delete g_type_string;
1188 delete g_type_bool;
1189 delete g_type_byte;
1190 delete g_type_i16;
1191 delete g_type_i32;
1192 delete g_type_i64;
1193 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001194
1195 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001196 return 0;
1197}