blob: 88d9bc41c507b1b2b36513110b4078b916654f7c [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>
David Reissab55ed52008-06-11 01:17:00 +000027#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000028
David Reiss204420f2008-01-11 20:59:03 +000029#ifdef MINGW
30# include <windows.h> /* for GetFullPathName */
David Reiss204420f2008-01-11 20:59:03 +000031#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"
David Reissbbbbe882009-02-17 20:27:48 +000039#include "generate/t_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000040#include "generate/t_php_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000041
David Reissdd08f6d2008-06-30 20:24:24 +000042#include "version.h"
43
Mark Slee31985722006-05-24 21:45:31 +000044using 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;
David Reiss9f2a5d72008-06-11 01:15:45 +0000155bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000156bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000157bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000158bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000159bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000160bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000161bool gen_recurse = false;
162
163/**
David Reiss204420f2008-01-11 20:59:03 +0000164 * MinGW doesn't have realpath, so use fallback implementation in that case,
165 * otherwise this just calls through to realpath
166 */
167char *saferealpath(const char *path, char *resolved_path) {
168#ifdef MINGW
169 char buf[MAX_PATH];
170 char* basename;
171 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
172 if (len == 0 || len > MAX_PATH - 1){
173 strcpy(resolved_path, path);
174 } else {
175 CharLowerBuff(buf, len);
176 strcpy(resolved_path, buf);
177 }
178 return resolved_path;
179#else
180 return realpath(path, resolved_path);
181#endif
182}
183
184
185/**
Mark Slee31985722006-05-24 21:45:31 +0000186 * Report an error to the user. This is called yyerror for historical
187 * reasons (lex and yacc expect the error reporting routine to be called
188 * this). Call this function to report any errors to the user.
189 * yyerror takes printf style arguments.
190 *
191 * @param fmt C format string followed by additional arguments
192 */
David Reiss0babe402008-06-10 22:56:12 +0000193void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000194 va_list args;
195 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000196 "[ERROR:%s:%d] (last token was '%s')\n",
197 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000198 yylineno,
199 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000200
201 va_start(args, fmt);
202 vfprintf(stderr, fmt, args);
203 va_end(args);
204
205 fprintf(stderr, "\n");
206}
207
208/**
209 * Prints a debug message from the parser.
210 *
211 * @param fmt C format string followed by additional arguments
212 */
David Reiss0babe402008-06-10 22:56:12 +0000213void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000214 if (g_debug == 0) {
215 return;
216 }
217 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000218 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000219 va_start(args, fmt);
220 vprintf(fmt, args);
221 va_end(args);
222 printf("\n");
223}
224
225/**
226 * Prints a verbose output mode message
227 *
228 * @param fmt C format string followed by additional arguments
229 */
David Reiss0babe402008-06-10 22:56:12 +0000230void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000231 if (g_verbose == 0) {
232 return;
233 }
234 va_list args;
235 va_start(args, fmt);
236 vprintf(fmt, args);
237 va_end(args);
238}
239
240/**
241 * Prints a warning message
242 *
243 * @param fmt C format string followed by additional arguments
244 */
David Reiss0babe402008-06-10 22:56:12 +0000245void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000246 if (g_warn < level) {
247 return;
248 }
249 va_list args;
250 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000251 va_start(args, fmt);
252 vprintf(fmt, args);
253 va_end(args);
254 printf("\n");
255}
256
257/**
258 * Prints a failure message and exits
259 *
260 * @param fmt C format string followed by additional arguments
261 */
Mark Slee30152872006-11-28 01:24:07 +0000262void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000263 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000264 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000265 va_start(args, fmt);
266 vfprintf(stderr, fmt, args);
267 va_end(args);
268 printf("\n");
269 exit(1);
270}
271
272/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000273 * Converts a string filename into a thrift program name
274 */
275string program_name(string filename) {
276 string::size_type slash = filename.rfind("/");
277 if (slash != string::npos) {
278 filename = filename.substr(slash+1);
279 }
280 string::size_type dot = filename.rfind(".");
281 if (dot != string::npos) {
282 filename = filename.substr(0, dot);
283 }
284 return filename;
285}
286
287/**
288 * Gets the directory path of a filename
289 */
290string directory_name(string filename) {
291 string::size_type slash = filename.rfind("/");
292 // No slash, just use the current directory
293 if (slash == string::npos) {
294 return ".";
295 }
296 return filename.substr(0, slash);
297}
298
299/**
300 * Finds the appropriate file path for the given filename
301 */
302string include_file(string filename) {
303 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000304 if (filename[0] == '/') {
305 // Realpath!
306 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000307 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000308 pwarning(0, "Cannot open include file %s\n", filename.c_str());
309 return std::string();
310 }
Mark Slee2c44d202007-05-16 02:18:07 +0000311
312 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000313 struct stat finfo;
314 if (stat(rp, &finfo) == 0) {
315 return rp;
316 }
317 } else { // relative path, start searching
318 // new search path with current dir global
319 vector<string> sp = g_incl_searchpath;
320 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000321
Martin Kraemer32c66e12006-11-09 00:06:36 +0000322 // iterate through paths
323 vector<string>::iterator it;
324 for (it = sp.begin(); it != sp.end(); it++) {
325 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000326
Martin Kraemer32c66e12006-11-09 00:06:36 +0000327 // Realpath!
328 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000329 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000330 continue;
331 }
Mark Slee2c44d202007-05-16 02:18:07 +0000332
Martin Kraemer32c66e12006-11-09 00:06:36 +0000333 // Stat this files
334 struct stat finfo;
335 if (stat(rp, &finfo) == 0) {
336 return rp;
337 }
338 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000339 }
Mark Slee2c44d202007-05-16 02:18:07 +0000340
Mark Sleef0712dc2006-10-25 19:03:57 +0000341 // Uh oh
342 pwarning(0, "Could not find include file %s\n", filename.c_str());
343 return std::string();
344}
345
346/**
David Reisscbd4bac2007-08-14 17:12:33 +0000347 * Clears any previously stored doctext string.
348 * Also prints a warning if we are discarding information.
349 */
350void clear_doctext() {
351 if (g_doctext != NULL) {
352 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
353 }
354 free(g_doctext);
355 g_doctext = NULL;
356}
357
358/**
David Reiss1ac05802007-07-30 22:00:27 +0000359 * Cleans up text commonly found in doxygen-like comments
360 *
361 * Warning: if you mix tabs and spaces in a non-uniform way,
362 * you will get what you deserve.
363 */
364char* clean_up_doctext(char* doctext) {
365 // Convert to C++ string, and remove Windows's carriage returns.
366 string docstring = doctext;
367 docstring.erase(
368 remove(docstring.begin(), docstring.end(), '\r'),
369 docstring.end());
370
371 // Separate into lines.
372 vector<string> lines;
373 string::size_type pos = string::npos;
374 string::size_type last;
375 while (true) {
376 last = (pos == string::npos) ? 0 : pos+1;
377 pos = docstring.find('\n', last);
378 if (pos == string::npos) {
379 // First bit of cleaning. If the last line is only whitespace, drop it.
380 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
381 if (nonwhite != string::npos) {
382 lines.push_back(docstring.substr(last));
383 }
384 break;
385 }
386 lines.push_back(docstring.substr(last, pos-last));
387 }
388
389 // A very profound docstring.
390 if (lines.empty()) {
391 return NULL;
392 }
393
394 // Clear leading whitespace from the first line.
395 pos = lines.front().find_first_not_of(" \t");
396 lines.front().erase(0, pos);
397
398 // If every nonblank line after the first has the same number of spaces/tabs,
399 // then a star, remove them.
400 bool have_prefix = true;
401 bool found_prefix = false;
402 string::size_type prefix_len = 0;
403 vector<string>::iterator l_iter;
404 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
405 if (l_iter->empty()) {
406 continue;
407 }
408
409 pos = l_iter->find_first_not_of(" \t");
410 if (!found_prefix) {
411 if (pos != string::npos) {
412 if (l_iter->at(pos) == '*') {
413 found_prefix = true;
414 prefix_len = pos;
415 } else {
416 have_prefix = false;
417 break;
418 }
419 } else {
420 // Whitespace-only line. Truncate it.
421 l_iter->clear();
422 }
423 } else if (l_iter->size() > pos
424 && l_iter->at(pos) == '*'
425 && pos == prefix_len) {
426 // Business as usual.
427 } else if (pos == string::npos) {
428 // Whitespace-only line. Let's truncate it for them.
429 l_iter->clear();
430 } else {
431 // The pattern has been broken.
432 have_prefix = false;
433 break;
434 }
435 }
436
437 // If our prefix survived, delete it from every line.
438 if (have_prefix) {
439 // Get the star too.
440 prefix_len++;
441 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
442 l_iter->erase(0, prefix_len);
443 }
444 }
445
446 // Now delete the minimum amount of leading whitespace from each line.
447 prefix_len = string::npos;
448 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
449 if (l_iter->empty()) {
450 continue;
451 }
452 pos = l_iter->find_first_not_of(" \t");
453 if (pos != string::npos
454 && (prefix_len == string::npos || pos < prefix_len)) {
455 prefix_len = pos;
456 }
457 }
458
459 // If our prefix survived, delete it from every line.
460 if (prefix_len != string::npos) {
461 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
462 l_iter->erase(0, prefix_len);
463 }
464 }
465
466 // Remove trailing whitespace from every line.
467 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
468 pos = l_iter->find_last_not_of(" \t");
469 if (pos != string::npos && pos != l_iter->length()-1) {
470 l_iter->erase(pos+1);
471 }
472 }
473
474 // If the first line is empty, remove it.
475 // Don't do this earlier because a lot of steps skip the first line.
476 if (lines.front().empty()) {
477 lines.erase(lines.begin());
478 }
479
480 // Now rejoin the lines and copy them back into doctext.
481 docstring.clear();
482 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
483 docstring += *l_iter;
484 docstring += '\n';
485 }
486
487 assert(docstring.length() <= strlen(doctext));
488 strcpy(doctext, docstring.c_str());
489 return doctext;
490}
491
492/** Set to true to debug docstring parsing */
493static bool dump_docs = false;
494
495/**
496 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000497 * Only works for top-level definitions and the whole program doc
498 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000499 */
500void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000501 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000502 if (!progdoc.empty()) {
503 printf("Whole program doc:\n%s\n", progdoc.c_str());
504 }
David Reiss1ac05802007-07-30 22:00:27 +0000505 const vector<t_typedef*>& typedefs = program->get_typedefs();
506 vector<t_typedef*>::const_iterator t_iter;
507 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
508 t_typedef* td = *t_iter;
509 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000510 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
511 }
512 }
513 const vector<t_enum*>& enums = program->get_enums();
514 vector<t_enum*>::const_iterator e_iter;
515 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
516 t_enum* en = *e_iter;
517 if (en->has_doc()) {
518 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
519 }
520 }
521 const vector<t_const*>& consts = program->get_consts();
522 vector<t_const*>::const_iterator c_iter;
523 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
524 t_const* co = *c_iter;
525 if (co->has_doc()) {
526 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
527 }
528 }
529 const vector<t_struct*>& structs = program->get_structs();
530 vector<t_struct*>::const_iterator s_iter;
531 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
532 t_struct* st = *s_iter;
533 if (st->has_doc()) {
534 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
535 }
536 }
537 const vector<t_struct*>& xceptions = program->get_xceptions();
538 vector<t_struct*>::const_iterator x_iter;
539 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
540 t_struct* xn = *x_iter;
541 if (xn->has_doc()) {
542 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
543 }
544 }
545 const vector<t_service*>& services = program->get_services();
546 vector<t_service*>::const_iterator v_iter;
547 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
548 t_service* sv = *v_iter;
549 if (sv->has_doc()) {
550 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000551 }
552 }
553}
554
555/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000556 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000557 */
558void generate_all_fingerprints(t_program* program) {
559 const vector<t_struct*>& structs = program->get_structs();
560 vector<t_struct*>::const_iterator s_iter;
561 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
562 t_struct* st = *s_iter;
563 st->generate_fingerprint();
564 }
565
David Reissd779cbe2007-08-31 01:42:55 +0000566 const vector<t_struct*>& xceptions = program->get_xceptions();
567 vector<t_struct*>::const_iterator x_iter;
568 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
569 t_struct* st = *x_iter;
570 st->generate_fingerprint();
571 }
572
David Reiss3c5d2fd2008-02-08 21:58:06 +0000573 const vector<t_enum*>& enums = program->get_enums();
574 vector<t_enum*>::const_iterator e_iter;
575 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
576 t_enum* e = *e_iter;
577 e->generate_fingerprint();
578 }
579
David Reiss47557bc2007-09-04 21:31:04 +0000580 g_type_void->generate_fingerprint();
581
David Reiss18bf22d2007-08-28 20:49:17 +0000582 // If you want to generate fingerprints for implicit structures, start here.
583 /*
584 const vector<t_service*>& services = program->get_services();
585 vector<t_service*>::const_iterator v_iter;
586 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
587 t_service* sv = *v_iter;
588 }
589 */
590}
591
592/**
David Reissdd08f6d2008-06-30 20:24:24 +0000593 * Prints the version number
594 */
595void version() {
596 printf("Thrift version %s-%s\n", THRIFT_VERSION, THRIFT_REVISION);
597}
598
599/**
Mark Slee31985722006-05-24 21:45:31 +0000600 * Diplays the usage message and then exits with an error code.
601 */
602void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000603 fprintf(stderr, "Usage: thrift [options] file\n");
604 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000605 fprintf(stderr, " -version Print the compiler version\n");
Mark Slee2329a832006-11-09 00:23:30 +0000606 fprintf(stderr, " -php Generate PHP output files\n");
607 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000608 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
609 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000610 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000611 fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
dweatherford65b70752007-10-31 02:18:14 +0000612 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
613 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000614 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000615 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000616 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000617 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
618 fprintf(stderr, " -strict Strict compiler warnings on\n");
619 fprintf(stderr, " -v[erbose] Verbose mode\n");
620 fprintf(stderr, " -r[ecurse] Also generate included files\n");
621 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000622 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
623 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
624 fprintf(stderr, " Keys and values are options passed to the generator.\n");
625 fprintf(stderr, " Many options will not require values.\n");
626 fprintf(stderr, "\n");
627 fprintf(stderr, "Available generators (and options):\n");
628
629 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
630 t_generator_registry::gen_map_t::iterator iter;
631 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
632 fprintf(stderr, " %s (%s):\n",
633 iter->second->get_short_name().c_str(),
634 iter->second->get_long_name().c_str());
635 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
636 }
Mark Slee31985722006-05-24 21:45:31 +0000637 exit(1);
638}
639
640/**
Mark Slee30152872006-11-28 01:24:07 +0000641 * You know, when I started working on Thrift I really thought it wasn't going
642 * to become a programming language because it was just a generator and it
643 * wouldn't need runtime type information and all that jazz. But then we
644 * decided to add constants, and all of a sudden that means runtime type
645 * validation and inference, except the "runtime" is the code generator
646 * runtime. Shit. I've been had.
647 */
648void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
649 if (type->is_void()) {
650 throw "type error: cannot declare a void const: " + name;
651 }
652
653 if (type->is_base_type()) {
654 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
655 switch (tbase) {
656 case t_base_type::TYPE_STRING:
657 if (value->get_type() != t_const_value::CV_STRING) {
658 throw "type error: const \"" + name + "\" was declared as string";
659 }
660 break;
661 case t_base_type::TYPE_BOOL:
662 if (value->get_type() != t_const_value::CV_INTEGER) {
663 throw "type error: const \"" + name + "\" was declared as bool";
664 }
665 break;
666 case t_base_type::TYPE_BYTE:
667 if (value->get_type() != t_const_value::CV_INTEGER) {
668 throw "type error: const \"" + name + "\" was declared as byte";
669 }
670 break;
671 case t_base_type::TYPE_I16:
672 if (value->get_type() != t_const_value::CV_INTEGER) {
673 throw "type error: const \"" + name + "\" was declared as i16";
674 }
675 break;
676 case t_base_type::TYPE_I32:
677 if (value->get_type() != t_const_value::CV_INTEGER) {
678 throw "type error: const \"" + name + "\" was declared as i32";
679 }
680 break;
681 case t_base_type::TYPE_I64:
682 if (value->get_type() != t_const_value::CV_INTEGER) {
683 throw "type error: const \"" + name + "\" was declared as i64";
684 }
685 break;
686 case t_base_type::TYPE_DOUBLE:
687 if (value->get_type() != t_const_value::CV_INTEGER &&
688 value->get_type() != t_const_value::CV_DOUBLE) {
689 throw "type error: const \"" + name + "\" was declared as double";
690 }
691 break;
692 default:
David Reissdd7796f2007-08-28 21:09:06 +0000693 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000694 }
695 } else if (type->is_enum()) {
696 if (value->get_type() != t_const_value::CV_INTEGER) {
697 throw "type error: const \"" + name + "\" was declared as enum";
698 }
699 } else if (type->is_struct() || type->is_xception()) {
700 if (value->get_type() != t_const_value::CV_MAP) {
701 throw "type error: const \"" + name + "\" was declared as struct/xception";
702 }
703 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
704 vector<t_field*>::const_iterator f_iter;
705
706 const map<t_const_value*, t_const_value*>& val = value->get_map();
707 map<t_const_value*, t_const_value*>::const_iterator v_iter;
708 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
709 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
710 throw "type error: " + name + " struct key must be string";
711 }
712 t_type* field_type = NULL;
713 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
714 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
715 field_type = (*f_iter)->get_type();
716 }
717 }
718 if (field_type == NULL) {
719 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
720 }
721
722 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
723 }
724 } else if (type->is_map()) {
725 t_type* k_type = ((t_map*)type)->get_key_type();
726 t_type* v_type = ((t_map*)type)->get_val_type();
727 const map<t_const_value*, t_const_value*>& val = value->get_map();
728 map<t_const_value*, t_const_value*>::const_iterator v_iter;
729 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
730 validate_const_rec(name + "<key>", k_type, v_iter->first);
731 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000732 }
Mark Slee30152872006-11-28 01:24:07 +0000733 } else if (type->is_list() || type->is_set()) {
734 t_type* e_type;
735 if (type->is_list()) {
736 e_type = ((t_list*)type)->get_elem_type();
737 } else {
738 e_type = ((t_set*)type)->get_elem_type();
739 }
740 const vector<t_const_value*>& val = value->get_list();
741 vector<t_const_value*>::const_iterator v_iter;
742 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
743 validate_const_rec(name + "<elem>", e_type, *v_iter);
744 }
745 }
746}
747
748/**
749 * Check the type of the parsed const information against its declared type
750 */
751void validate_const_type(t_const* c) {
752 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
753}
754
755/**
Mark Slee7ff32452007-02-01 05:26:18 +0000756 * Check the type of a default value assigned to a field.
757 */
758void validate_field_value(t_field* field, t_const_value* cv) {
759 validate_const_rec(field->get_name(), field->get_type(), cv);
760}
761
762/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000763 * Check that all the elements of a throws block are actually exceptions.
764 */
765bool validate_throws(t_struct* throws) {
766 const vector<t_field*>& members = throws->get_members();
767 vector<t_field*>::const_iterator m_iter;
768 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
769 if (!(*m_iter)->get_type()->is_xception()) {
770 return false;
771 }
772 }
773 return true;
774}
775
776/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000777 * Parses a program
778 */
Mark Slee2c44d202007-05-16 02:18:07 +0000779void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000780 // Get scope file path
781 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000782
Mark Sleef0712dc2006-10-25 19:03:57 +0000783 // Set current dir global, which is used in the include_file function
784 g_curdir = directory_name(path);
785 g_curpath = path;
786
787 // Open the file
788 yyin = fopen(path.c_str(), "r");
789 if (yyin == 0) {
790 failure("Could not open input file: \"%s\"", path.c_str());
791 }
792
793 // Create new scope and scan for includes
794 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000795 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000796 g_program = program;
797 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000798 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000799 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000800 if (yyparse() != 0) {
801 failure("Parser error during include pass.");
802 }
803 } catch (string x) {
804 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000805 }
806 fclose(yyin);
807
808 // Recursively parse all the include programs
809 vector<t_program*>& includes = program->get_includes();
810 vector<t_program*>::iterator iter;
811 for (iter = includes.begin(); iter != includes.end(); ++iter) {
812 parse(*iter, program);
813 }
814
David Reiss204420f2008-01-11 20:59:03 +0000815 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000816 g_parse_mode = PROGRAM;
817 g_program = program;
818 g_scope = program->scope();
819 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
820 g_parent_prefix = program->get_name() + ".";
821 g_curpath = path;
822 yyin = fopen(path.c_str(), "r");
823 if (yyin == 0) {
824 failure("Could not open input file: \"%s\"", path.c_str());
825 }
826 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000827 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000828 try {
829 if (yyparse() != 0) {
830 failure("Parser error during types pass.");
831 }
832 } catch (string x) {
833 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000834 }
835 fclose(yyin);
836}
837
838/**
839 * Generate code
840 */
David Reissbd0db882008-02-27 01:54:51 +0000841void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000842 // Oooohh, recursive code generation, hot!!
843 if (gen_recurse) {
844 const vector<t_program*>& includes = program->get_includes();
845 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000846 // Propogate output path from parent to child programs
847 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000848
David Reissbd0db882008-02-27 01:54:51 +0000849 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 }
851 }
852
853 // Generate code!
854 try {
855 pverbose("Program: %s\n", program->get_path().c_str());
856
David Reiss18bf22d2007-08-28 20:49:17 +0000857 // Compute fingerprints.
858 generate_all_fingerprints(program);
859
Mark Sleef0712dc2006-10-25 19:03:57 +0000860 if (gen_php) {
861 pverbose("Generating PHP\n");
Mark Sleeb22df7c2007-11-28 02:39:59 +0000862 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 +0000863 php->generate_program();
864 delete php;
865 }
866
867 if (gen_phpi) {
868 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000869 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000870 phpi->generate_program();
871 delete phpi;
872 }
873
David Reiss1ac05802007-07-30 22:00:27 +0000874 if (dump_docs) {
875 dump_docstrings(program);
876 }
David Reissbd0db882008-02-27 01:54:51 +0000877
878 vector<string>::const_iterator iter;
879 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
880 t_generator* generator = t_generator_registry::get_generator(program, *iter);
881
882 if (generator == NULL) {
883 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
884 } else {
885 pverbose("Generating \"%s\"\n", iter->c_str());
886 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000887 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000888 }
889 }
890
Mark Sleef0712dc2006-10-25 19:03:57 +0000891 } catch (string s) {
892 printf("Error: %s\n", s.c_str());
893 } catch (const char* exc) {
894 printf("Error: %s\n", exc);
895 }
896
897}
898
899/**
Mark Sleef5377b32006-10-10 01:42:59 +0000900 * Parse it up.. then spit it back out, in pretty much every language. Alright
901 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000902 */
903int main(int argc, char** argv) {
904 int i;
dweatherford65b70752007-10-31 02:18:14 +0000905 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000906
Mark Sleeb15a68b2006-06-07 06:46:24 +0000907 // Setup time string
908 time_t now = time(NULL);
909 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000910
Mark Sleef0712dc2006-10-25 19:03:57 +0000911 // Check for necessary arguments, you gotta have at least a filename and
912 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000913 if (argc < 2) {
914 usage();
915 }
Mark Slee31985722006-05-24 21:45:31 +0000916
David Reissbd0db882008-02-27 01:54:51 +0000917 vector<string> generator_strings;
918
David Reiss9cc2c132008-02-27 01:54:47 +0000919 // Set the current path to a dummy value to make warning messages clearer.
920 g_curpath = "arguments";
921
Mark Sleef5377b32006-10-10 01:42:59 +0000922 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000923 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000924 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000925
Mark Sleefdbee812006-09-27 18:50:48 +0000926 arg = strtok(argv[i], " ");
927 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000928 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000929 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000930 ++arg;
931 }
932
David Reissdd08f6d2008-06-30 20:24:24 +0000933 if (strcmp(arg, "-version") == 0) {
934 version();
935 exit(1);
936 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000937 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000938 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000940 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000941 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000942 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000943 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000944 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000945 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000946 } else if (strcmp(arg, "-gen") == 0) {
947 arg = argv[++i];
948 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000949 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000950 usage();
951 }
952 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000953 } else if (strcmp(arg, "-dense") == 0) {
954 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000955 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000956 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000957 } else if (strcmp(arg, "-javabean") == 0) {
958 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000959 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000960 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000961 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000962 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000963 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000964 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000965 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000966 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000967 gen_phps = true;
968 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000969 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000970 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000971 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000972 gen_php = true;
973 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000974 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000975 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000976 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000977 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000978 } else if (strcmp(arg, "-rest") == 0) {
979 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000980 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000981 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +0000982 } else if (strcmp(arg, "-pyns") == 0) {
983 gen_py = true;
984 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000985 } else if (strcmp(arg, "-rb") == 0) {
986 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000987 } else if (strcmp(arg, "-xsd") == 0) {
988 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000989 } else if (strcmp(arg, "-perl") == 0) {
990 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +0000991 } else if (strcmp(arg, "-erl") == 0) {
992 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000993 } else if (strcmp(arg, "-ocaml") == 0) {
994 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000995 } else if (strcmp(arg, "-hs") == 0) {
996 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000997 } else if (strcmp(arg, "-cocoa") == 0) {
998 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +0000999 } else if (strcmp(arg, "-st") == 0) {
1000 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001001 } else if (strcmp(arg, "-csharp") == 0) {
1002 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001003 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1004 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001005 } else if (strcmp(arg, "-I") == 0) {
1006 // An argument of "-I\ asdf" is invalid and has unknown results
1007 arg = argv[++i];
1008
1009 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001010 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001011 usage();
1012 }
1013 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001014 } else if (strcmp(arg, "-o") == 0) {
1015 arg = argv[++i];
1016 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001017 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001018 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001019 }
dweatherford65b70752007-10-31 02:18:14 +00001020 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001021
1022#ifdef MINGW
1023 //strip out trailing \ on Windows
1024 int last = out_path.length()-1;
1025 if (out_path[last] == '\\')
1026 {
1027 out_path.erase(last);
1028 }
1029#endif
1030
dweatherford65b70752007-10-31 02:18:14 +00001031 struct stat sb;
1032 if (stat(out_path.c_str(), &sb) < 0) {
1033 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1034 return -1;
1035 }
1036 if (! S_ISDIR(sb.st_mode)) {
1037 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1038 return -1;
1039 }
Mark Sleefdbee812006-09-27 18:50:48 +00001040 } else {
1041 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1042 usage();
1043 }
1044
1045 // Tokenize more
1046 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001047 }
1048 }
Mark Slee2c44d202007-05-16 02:18:07 +00001049
David Reissdd08f6d2008-06-30 20:24:24 +00001050 // if you're asking for version, you have a right not to pass a file
1051 if (strcmp(argv[argc-1], "-version") == 0) {
1052 version();
1053 exit(1);
1054 }
1055
David Reiss400cd4b2008-02-27 01:54:55 +00001056 // TODO(dreiss): Delete these when everyone is using the new hotness.
1057 if (gen_cpp) {
1058 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1059 string gen_string = "cpp:";
1060 if (gen_dense) {
1061 gen_string.append("dense,");
1062 }
1063 if (g_cpp_use_include_prefix) {
1064 gen_string.append("include_prefix,");
1065 }
1066 generator_strings.push_back(gen_string);
1067 }
David Reisse1404d22008-02-27 01:55:05 +00001068 if (gen_java) {
1069 pwarning(1, "-java is deprecated. Use --gen java");
1070 generator_strings.push_back("java");
1071 }
1072 if (gen_javabean) {
1073 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1074 generator_strings.push_back("java:beans");
1075 }
David Reiss861869b2008-03-27 21:41:23 +00001076 if (gen_csharp) {
1077 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1078 generator_strings.push_back("csharp");
1079 }
David Reiss558e3992008-03-27 21:41:40 +00001080 if (gen_py) {
1081 pwarning(1, "-py is deprecated. Use --gen py");
1082 generator_strings.push_back("py");
1083 }
David Reissa640cea2008-03-27 21:42:01 +00001084 if (gen_rb) {
1085 pwarning(1, "-rb is deprecated. Use --gen rb");
1086 generator_strings.push_back("rb");
1087 }
David Reiss2b386c52008-03-27 21:42:23 +00001088 if (gen_perl) {
1089 pwarning(1, "-perl is deprecated. Use --gen perl");
1090 generator_strings.push_back("perl");
1091 }
David Reissd01c64d2008-03-27 21:40:55 +00001092 if (gen_cocoa) {
1093 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1094 generator_strings.push_back("cocoa");
1095 }
David Reissa2047832009-02-17 20:27:54 +00001096 if (gen_erl) {
1097 pwarning(1, "-erl is deprecated. Use --gen erl");
1098 generator_strings.push_back("erl");
1099 }
David Reissa890b572008-03-27 21:40:35 +00001100 if (gen_st) {
1101 pwarning(1, "-st is deprecated. Use --gen st");
1102 generator_strings.push_back("st");
1103 }
David Reissf0521b12008-03-27 21:40:05 +00001104 if (gen_ocaml) {
1105 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1106 generator_strings.push_back("ocaml");
1107 }
David Reissaf3ab262008-03-27 21:40:16 +00001108 if (gen_hs) {
1109 pwarning(1, "-hs is deprecated. Use --gen hs");
1110 generator_strings.push_back("hs");
1111 }
David Reiss4ba67102009-02-17 20:28:06 +00001112 if (gen_xsd) {
1113 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1114 generator_strings.push_back("xsd");
1115 }
David Reisse1404d22008-02-27 01:55:05 +00001116
Mark Sleef0712dc2006-10-25 19:03:57 +00001117 // You gotta generate something!
David Reiss4ba67102009-02-17 20:28:06 +00001118 if (!gen_php && !gen_phpi && generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001119 fprintf(stderr, "!!! No output language(s) specified\n\n");
1120 usage();
1121 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001122
1123 // Real-pathify it
1124 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001125 if (argv[i] == NULL) {
1126 fprintf(stderr, "!!! Missing file name\n");
1127 usage();
1128 }
David Reiss204420f2008-01-11 20:59:03 +00001129 if (saferealpath(argv[i], rp) == NULL) {
1130 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001131 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001132 string input_file(rp);
1133
Mark Sleef5377b32006-10-10 01:42:59 +00001134 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001135 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001136 if (out_path.size()) {
1137 program->set_out_path(out_path);
1138 }
kholst76f2c882008-01-16 02:47:41 +00001139
David Reiss4b6a3c72008-02-27 22:28:12 +00001140 // Compute the cpp include prefix.
1141 // infer this from the filename passed in
1142 string input_filename = argv[i];
1143 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001144
David Reiss4b6a3c72008-02-27 22:28:12 +00001145 string::size_type last_slash = string::npos;
1146 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1147 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001148 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001149
David Reiss4b6a3c72008-02-27 22:28:12 +00001150 program->set_include_prefix(include_prefix);
1151
Mark Sleef0712dc2006-10-25 19:03:57 +00001152 // Initialize global types
1153 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1154 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001155 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1156 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001157 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1158 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001159 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1160 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1161 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1162 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1163 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1164 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001165
Mark Sleef5377b32006-10-10 01:42:59 +00001166 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001167 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001168
David Reiss9cc2c132008-02-27 01:54:47 +00001169 // The current path is not really relevant when we are doing generation.
1170 // Reset the variable to make warning messages clearer.
1171 g_curpath = "generation";
1172 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1173 // That is what shows up during argument parsing.
1174 yylineno = 1;
1175
Mark Sleef0712dc2006-10-25 19:03:57 +00001176 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001177 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001178
Mark Sleef0712dc2006-10-25 19:03:57 +00001179 // Clean up. Who am I kidding... this program probably orphans heap memory
1180 // all over the place, but who cares because it is about to exit and it is
1181 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001182
Mark Sleef0712dc2006-10-25 19:03:57 +00001183 delete program;
1184 delete g_type_void;
1185 delete g_type_string;
1186 delete g_type_bool;
1187 delete g_type_byte;
1188 delete g_type_i16;
1189 delete g_type_i32;
1190 delete g_type_i64;
1191 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001192
1193 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001194 return 0;
1195}