blob: 0e31bd9fe377fd1c09b3e6851e3c44e51e739ca7 [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 Slee31985722006-05-24 21:45:31 +000040
David Reissdd08f6d2008-06-30 20:24:24 +000041#include "version.h"
42
Mark Slee31985722006-05-24 21:45:31 +000043using namespace std;
44
Mark Sleef5377b32006-10-10 01:42:59 +000045/**
46 * Global program tree
47 */
Mark Slee31985722006-05-24 21:45:31 +000048t_program* g_program;
49
Mark Sleef5377b32006-10-10 01:42:59 +000050/**
Mark Sleef0712dc2006-10-25 19:03:57 +000051 * Global types
52 */
53
54t_type* g_type_void;
55t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000056t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000057t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000058t_type* g_type_bool;
59t_type* g_type_byte;
60t_type* g_type_i16;
61t_type* g_type_i32;
62t_type* g_type_i64;
63t_type* g_type_double;
64
65/**
66 * Global scope
67 */
68t_scope* g_scope;
69
70/**
71 * Parent scope to also parse types
72 */
73t_scope* g_parent_scope;
74
75/**
76 * Prefix for putting types in parent scope
77 */
78string g_parent_prefix;
79
80/**
81 * Parsing pass
82 */
83PARSE_MODE g_parse_mode;
84
85/**
86 * Current directory of file being parsed
87 */
88string g_curdir;
89
90/**
91 * Current file being parsed
92 */
93string g_curpath;
94
95/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000096 * Search path for inclusions
97 */
Mark Slee2329a832006-11-09 00:23:30 +000098vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000099
100/**
kholst76f2c882008-01-16 02:47:41 +0000101 * Should C++ include statements use path prefixes for other thrift-generated
102 * header files
103 */
104bool g_cpp_use_include_prefix = false;
105
106/**
Mark Sleef5377b32006-10-10 01:42:59 +0000107 * Global debug state
108 */
Mark Slee31985722006-05-24 21:45:31 +0000109int g_debug = 0;
110
Mark Sleef5377b32006-10-10 01:42:59 +0000111/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000112 * Warning level
113 */
114int g_warn = 1;
115
116/**
117 * Verbose output
118 */
119int g_verbose = 0;
120
121/**
Mark Sleef5377b32006-10-10 01:42:59 +0000122 * Global time string
123 */
Mark Slee31985722006-05-24 21:45:31 +0000124char* g_time_str;
125
Mark Slee31985722006-05-24 21:45:31 +0000126/**
David Reisscbd4bac2007-08-14 17:12:33 +0000127 * The last parsed doctext comment.
128 */
129char* g_doctext;
130
131/**
132 * The location of the last parsed doctext comment.
133 */
134int g_doctext_lineno;
135
136/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000137 * Flags to control code generation
138 */
139bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000140bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000141bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000142bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000143bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000144bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000145bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000146bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000147bool gen_php = false;
148bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000149bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000150bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000151bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000152bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000153bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000154bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000155bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000156bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000157bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000158bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000159bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000160bool gen_recurse = false;
161
162/**
David Reiss204420f2008-01-11 20:59:03 +0000163 * MinGW doesn't have realpath, so use fallback implementation in that case,
164 * otherwise this just calls through to realpath
165 */
166char *saferealpath(const char *path, char *resolved_path) {
167#ifdef MINGW
168 char buf[MAX_PATH];
169 char* basename;
170 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
171 if (len == 0 || len > MAX_PATH - 1){
172 strcpy(resolved_path, path);
173 } else {
174 CharLowerBuff(buf, len);
175 strcpy(resolved_path, buf);
176 }
177 return resolved_path;
178#else
179 return realpath(path, resolved_path);
180#endif
181}
182
183
184/**
Mark Slee31985722006-05-24 21:45:31 +0000185 * Report an error to the user. This is called yyerror for historical
186 * reasons (lex and yacc expect the error reporting routine to be called
187 * this). Call this function to report any errors to the user.
188 * yyerror takes printf style arguments.
189 *
190 * @param fmt C format string followed by additional arguments
191 */
David Reiss0babe402008-06-10 22:56:12 +0000192void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000193 va_list args;
194 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000195 "[ERROR:%s:%d] (last token was '%s')\n",
196 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000197 yylineno,
198 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000199
200 va_start(args, fmt);
201 vfprintf(stderr, fmt, args);
202 va_end(args);
203
204 fprintf(stderr, "\n");
205}
206
207/**
208 * Prints a debug message from the parser.
209 *
210 * @param fmt C format string followed by additional arguments
211 */
David Reiss0babe402008-06-10 22:56:12 +0000212void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000213 if (g_debug == 0) {
214 return;
215 }
216 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000217 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000218 va_start(args, fmt);
219 vprintf(fmt, args);
220 va_end(args);
221 printf("\n");
222}
223
224/**
225 * Prints a verbose output mode message
226 *
227 * @param fmt C format string followed by additional arguments
228 */
David Reiss0babe402008-06-10 22:56:12 +0000229void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000230 if (g_verbose == 0) {
231 return;
232 }
233 va_list args;
234 va_start(args, fmt);
235 vprintf(fmt, args);
236 va_end(args);
237}
238
239/**
240 * Prints a warning message
241 *
242 * @param fmt C format string followed by additional arguments
243 */
David Reiss0babe402008-06-10 22:56:12 +0000244void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000245 if (g_warn < level) {
246 return;
247 }
248 va_list args;
249 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000250 va_start(args, fmt);
251 vprintf(fmt, args);
252 va_end(args);
253 printf("\n");
254}
255
256/**
257 * Prints a failure message and exits
258 *
259 * @param fmt C format string followed by additional arguments
260 */
Mark Slee30152872006-11-28 01:24:07 +0000261void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000262 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000263 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000264 va_start(args, fmt);
265 vfprintf(stderr, fmt, args);
266 va_end(args);
267 printf("\n");
268 exit(1);
269}
270
271/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000272 * Converts a string filename into a thrift program name
273 */
274string program_name(string filename) {
275 string::size_type slash = filename.rfind("/");
276 if (slash != string::npos) {
277 filename = filename.substr(slash+1);
278 }
279 string::size_type dot = filename.rfind(".");
280 if (dot != string::npos) {
281 filename = filename.substr(0, dot);
282 }
283 return filename;
284}
285
286/**
287 * Gets the directory path of a filename
288 */
289string directory_name(string filename) {
290 string::size_type slash = filename.rfind("/");
291 // No slash, just use the current directory
292 if (slash == string::npos) {
293 return ".";
294 }
295 return filename.substr(0, slash);
296}
297
298/**
299 * Finds the appropriate file path for the given filename
300 */
301string include_file(string filename) {
302 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000303 if (filename[0] == '/') {
304 // Realpath!
305 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000306 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000307 pwarning(0, "Cannot open include file %s\n", filename.c_str());
308 return std::string();
309 }
Mark Slee2c44d202007-05-16 02:18:07 +0000310
311 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000312 struct stat finfo;
313 if (stat(rp, &finfo) == 0) {
314 return rp;
315 }
316 } else { // relative path, start searching
317 // new search path with current dir global
318 vector<string> sp = g_incl_searchpath;
319 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000320
Martin Kraemer32c66e12006-11-09 00:06:36 +0000321 // iterate through paths
322 vector<string>::iterator it;
323 for (it = sp.begin(); it != sp.end(); it++) {
324 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000325
Martin Kraemer32c66e12006-11-09 00:06:36 +0000326 // Realpath!
327 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000328 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000329 continue;
330 }
Mark Slee2c44d202007-05-16 02:18:07 +0000331
Martin Kraemer32c66e12006-11-09 00:06:36 +0000332 // Stat this files
333 struct stat finfo;
334 if (stat(rp, &finfo) == 0) {
335 return rp;
336 }
337 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000338 }
Mark Slee2c44d202007-05-16 02:18:07 +0000339
Mark Sleef0712dc2006-10-25 19:03:57 +0000340 // Uh oh
341 pwarning(0, "Could not find include file %s\n", filename.c_str());
342 return std::string();
343}
344
345/**
David Reisscbd4bac2007-08-14 17:12:33 +0000346 * Clears any previously stored doctext string.
347 * Also prints a warning if we are discarding information.
348 */
349void clear_doctext() {
350 if (g_doctext != NULL) {
351 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
352 }
353 free(g_doctext);
354 g_doctext = NULL;
355}
356
357/**
David Reiss1ac05802007-07-30 22:00:27 +0000358 * Cleans up text commonly found in doxygen-like comments
359 *
360 * Warning: if you mix tabs and spaces in a non-uniform way,
361 * you will get what you deserve.
362 */
363char* clean_up_doctext(char* doctext) {
364 // Convert to C++ string, and remove Windows's carriage returns.
365 string docstring = doctext;
366 docstring.erase(
367 remove(docstring.begin(), docstring.end(), '\r'),
368 docstring.end());
369
370 // Separate into lines.
371 vector<string> lines;
372 string::size_type pos = string::npos;
373 string::size_type last;
374 while (true) {
375 last = (pos == string::npos) ? 0 : pos+1;
376 pos = docstring.find('\n', last);
377 if (pos == string::npos) {
378 // First bit of cleaning. If the last line is only whitespace, drop it.
379 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
380 if (nonwhite != string::npos) {
381 lines.push_back(docstring.substr(last));
382 }
383 break;
384 }
385 lines.push_back(docstring.substr(last, pos-last));
386 }
387
388 // A very profound docstring.
389 if (lines.empty()) {
390 return NULL;
391 }
392
393 // Clear leading whitespace from the first line.
394 pos = lines.front().find_first_not_of(" \t");
395 lines.front().erase(0, pos);
396
397 // If every nonblank line after the first has the same number of spaces/tabs,
398 // then a star, remove them.
399 bool have_prefix = true;
400 bool found_prefix = false;
401 string::size_type prefix_len = 0;
402 vector<string>::iterator l_iter;
403 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
404 if (l_iter->empty()) {
405 continue;
406 }
407
408 pos = l_iter->find_first_not_of(" \t");
409 if (!found_prefix) {
410 if (pos != string::npos) {
411 if (l_iter->at(pos) == '*') {
412 found_prefix = true;
413 prefix_len = pos;
414 } else {
415 have_prefix = false;
416 break;
417 }
418 } else {
419 // Whitespace-only line. Truncate it.
420 l_iter->clear();
421 }
422 } else if (l_iter->size() > pos
423 && l_iter->at(pos) == '*'
424 && pos == prefix_len) {
425 // Business as usual.
426 } else if (pos == string::npos) {
427 // Whitespace-only line. Let's truncate it for them.
428 l_iter->clear();
429 } else {
430 // The pattern has been broken.
431 have_prefix = false;
432 break;
433 }
434 }
435
436 // If our prefix survived, delete it from every line.
437 if (have_prefix) {
438 // Get the star too.
439 prefix_len++;
440 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
441 l_iter->erase(0, prefix_len);
442 }
443 }
444
445 // Now delete the minimum amount of leading whitespace from each line.
446 prefix_len = string::npos;
447 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
448 if (l_iter->empty()) {
449 continue;
450 }
451 pos = l_iter->find_first_not_of(" \t");
452 if (pos != string::npos
453 && (prefix_len == string::npos || pos < prefix_len)) {
454 prefix_len = pos;
455 }
456 }
457
458 // If our prefix survived, delete it from every line.
459 if (prefix_len != string::npos) {
460 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
461 l_iter->erase(0, prefix_len);
462 }
463 }
464
465 // Remove trailing whitespace from every line.
466 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
467 pos = l_iter->find_last_not_of(" \t");
468 if (pos != string::npos && pos != l_iter->length()-1) {
469 l_iter->erase(pos+1);
470 }
471 }
472
473 // If the first line is empty, remove it.
474 // Don't do this earlier because a lot of steps skip the first line.
475 if (lines.front().empty()) {
476 lines.erase(lines.begin());
477 }
478
479 // Now rejoin the lines and copy them back into doctext.
480 docstring.clear();
481 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
482 docstring += *l_iter;
483 docstring += '\n';
484 }
485
486 assert(docstring.length() <= strlen(doctext));
487 strcpy(doctext, docstring.c_str());
488 return doctext;
489}
490
491/** Set to true to debug docstring parsing */
492static bool dump_docs = false;
493
494/**
495 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000496 * Only works for top-level definitions and the whole program doc
497 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000498 */
499void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000500 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000501 if (!progdoc.empty()) {
502 printf("Whole program doc:\n%s\n", progdoc.c_str());
503 }
David Reiss1ac05802007-07-30 22:00:27 +0000504 const vector<t_typedef*>& typedefs = program->get_typedefs();
505 vector<t_typedef*>::const_iterator t_iter;
506 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
507 t_typedef* td = *t_iter;
508 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000509 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
510 }
511 }
512 const vector<t_enum*>& enums = program->get_enums();
513 vector<t_enum*>::const_iterator e_iter;
514 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
515 t_enum* en = *e_iter;
516 if (en->has_doc()) {
517 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
518 }
519 }
520 const vector<t_const*>& consts = program->get_consts();
521 vector<t_const*>::const_iterator c_iter;
522 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
523 t_const* co = *c_iter;
524 if (co->has_doc()) {
525 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
526 }
527 }
528 const vector<t_struct*>& structs = program->get_structs();
529 vector<t_struct*>::const_iterator s_iter;
530 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
531 t_struct* st = *s_iter;
532 if (st->has_doc()) {
533 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
534 }
535 }
536 const vector<t_struct*>& xceptions = program->get_xceptions();
537 vector<t_struct*>::const_iterator x_iter;
538 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
539 t_struct* xn = *x_iter;
540 if (xn->has_doc()) {
541 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
542 }
543 }
544 const vector<t_service*>& services = program->get_services();
545 vector<t_service*>::const_iterator v_iter;
546 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
547 t_service* sv = *v_iter;
548 if (sv->has_doc()) {
549 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000550 }
551 }
552}
553
554/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000555 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000556 */
557void generate_all_fingerprints(t_program* program) {
558 const vector<t_struct*>& structs = program->get_structs();
559 vector<t_struct*>::const_iterator s_iter;
560 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
561 t_struct* st = *s_iter;
562 st->generate_fingerprint();
563 }
564
David Reissd779cbe2007-08-31 01:42:55 +0000565 const vector<t_struct*>& xceptions = program->get_xceptions();
566 vector<t_struct*>::const_iterator x_iter;
567 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
568 t_struct* st = *x_iter;
569 st->generate_fingerprint();
570 }
571
David Reiss3c5d2fd2008-02-08 21:58:06 +0000572 const vector<t_enum*>& enums = program->get_enums();
573 vector<t_enum*>::const_iterator e_iter;
574 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
575 t_enum* e = *e_iter;
576 e->generate_fingerprint();
577 }
578
David Reiss47557bc2007-09-04 21:31:04 +0000579 g_type_void->generate_fingerprint();
580
David Reiss18bf22d2007-08-28 20:49:17 +0000581 // If you want to generate fingerprints for implicit structures, start here.
582 /*
583 const vector<t_service*>& services = program->get_services();
584 vector<t_service*>::const_iterator v_iter;
585 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
586 t_service* sv = *v_iter;
587 }
588 */
589}
590
591/**
David Reissdd08f6d2008-06-30 20:24:24 +0000592 * Prints the version number
593 */
594void version() {
595 printf("Thrift version %s-%s\n", THRIFT_VERSION, THRIFT_REVISION);
596}
597
598/**
Mark Slee31985722006-05-24 21:45:31 +0000599 * Diplays the usage message and then exits with an error code.
600 */
601void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000602 fprintf(stderr, "Usage: thrift [options] file\n");
603 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000604 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000605 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
606 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000607 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000608 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000609 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
610 fprintf(stderr, " -strict Strict compiler warnings on\n");
611 fprintf(stderr, " -v[erbose] Verbose mode\n");
612 fprintf(stderr, " -r[ecurse] Also generate included files\n");
613 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000614 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
615 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
616 fprintf(stderr, " Keys and values are options passed to the generator.\n");
617 fprintf(stderr, " Many options will not require values.\n");
618 fprintf(stderr, "\n");
619 fprintf(stderr, "Available generators (and options):\n");
620
621 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
622 t_generator_registry::gen_map_t::iterator iter;
623 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
624 fprintf(stderr, " %s (%s):\n",
625 iter->second->get_short_name().c_str(),
626 iter->second->get_long_name().c_str());
627 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
628 }
Mark Slee31985722006-05-24 21:45:31 +0000629 exit(1);
630}
631
632/**
Mark Slee30152872006-11-28 01:24:07 +0000633 * You know, when I started working on Thrift I really thought it wasn't going
634 * to become a programming language because it was just a generator and it
635 * wouldn't need runtime type information and all that jazz. But then we
636 * decided to add constants, and all of a sudden that means runtime type
637 * validation and inference, except the "runtime" is the code generator
638 * runtime. Shit. I've been had.
639 */
640void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
641 if (type->is_void()) {
642 throw "type error: cannot declare a void const: " + name;
643 }
644
645 if (type->is_base_type()) {
646 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
647 switch (tbase) {
648 case t_base_type::TYPE_STRING:
649 if (value->get_type() != t_const_value::CV_STRING) {
650 throw "type error: const \"" + name + "\" was declared as string";
651 }
652 break;
653 case t_base_type::TYPE_BOOL:
654 if (value->get_type() != t_const_value::CV_INTEGER) {
655 throw "type error: const \"" + name + "\" was declared as bool";
656 }
657 break;
658 case t_base_type::TYPE_BYTE:
659 if (value->get_type() != t_const_value::CV_INTEGER) {
660 throw "type error: const \"" + name + "\" was declared as byte";
661 }
662 break;
663 case t_base_type::TYPE_I16:
664 if (value->get_type() != t_const_value::CV_INTEGER) {
665 throw "type error: const \"" + name + "\" was declared as i16";
666 }
667 break;
668 case t_base_type::TYPE_I32:
669 if (value->get_type() != t_const_value::CV_INTEGER) {
670 throw "type error: const \"" + name + "\" was declared as i32";
671 }
672 break;
673 case t_base_type::TYPE_I64:
674 if (value->get_type() != t_const_value::CV_INTEGER) {
675 throw "type error: const \"" + name + "\" was declared as i64";
676 }
677 break;
678 case t_base_type::TYPE_DOUBLE:
679 if (value->get_type() != t_const_value::CV_INTEGER &&
680 value->get_type() != t_const_value::CV_DOUBLE) {
681 throw "type error: const \"" + name + "\" was declared as double";
682 }
683 break;
684 default:
David Reissdd7796f2007-08-28 21:09:06 +0000685 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000686 }
687 } else if (type->is_enum()) {
688 if (value->get_type() != t_const_value::CV_INTEGER) {
689 throw "type error: const \"" + name + "\" was declared as enum";
690 }
691 } else if (type->is_struct() || type->is_xception()) {
692 if (value->get_type() != t_const_value::CV_MAP) {
693 throw "type error: const \"" + name + "\" was declared as struct/xception";
694 }
695 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
696 vector<t_field*>::const_iterator f_iter;
697
698 const map<t_const_value*, t_const_value*>& val = value->get_map();
699 map<t_const_value*, t_const_value*>::const_iterator v_iter;
700 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
701 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
702 throw "type error: " + name + " struct key must be string";
703 }
704 t_type* field_type = NULL;
705 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
706 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
707 field_type = (*f_iter)->get_type();
708 }
709 }
710 if (field_type == NULL) {
711 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
712 }
713
714 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
715 }
716 } else if (type->is_map()) {
717 t_type* k_type = ((t_map*)type)->get_key_type();
718 t_type* v_type = ((t_map*)type)->get_val_type();
719 const map<t_const_value*, t_const_value*>& val = value->get_map();
720 map<t_const_value*, t_const_value*>::const_iterator v_iter;
721 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
722 validate_const_rec(name + "<key>", k_type, v_iter->first);
723 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000724 }
Mark Slee30152872006-11-28 01:24:07 +0000725 } else if (type->is_list() || type->is_set()) {
726 t_type* e_type;
727 if (type->is_list()) {
728 e_type = ((t_list*)type)->get_elem_type();
729 } else {
730 e_type = ((t_set*)type)->get_elem_type();
731 }
732 const vector<t_const_value*>& val = value->get_list();
733 vector<t_const_value*>::const_iterator v_iter;
734 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
735 validate_const_rec(name + "<elem>", e_type, *v_iter);
736 }
737 }
738}
739
740/**
741 * Check the type of the parsed const information against its declared type
742 */
743void validate_const_type(t_const* c) {
744 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
745}
746
747/**
Mark Slee7ff32452007-02-01 05:26:18 +0000748 * Check the type of a default value assigned to a field.
749 */
750void validate_field_value(t_field* field, t_const_value* cv) {
751 validate_const_rec(field->get_name(), field->get_type(), cv);
752}
753
754/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000755 * Check that all the elements of a throws block are actually exceptions.
756 */
757bool validate_throws(t_struct* throws) {
758 const vector<t_field*>& members = throws->get_members();
759 vector<t_field*>::const_iterator m_iter;
760 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
761 if (!(*m_iter)->get_type()->is_xception()) {
762 return false;
763 }
764 }
765 return true;
766}
767
768/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000769 * Parses a program
770 */
Mark Slee2c44d202007-05-16 02:18:07 +0000771void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000772 // Get scope file path
773 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000774
Mark Sleef0712dc2006-10-25 19:03:57 +0000775 // Set current dir global, which is used in the include_file function
776 g_curdir = directory_name(path);
777 g_curpath = path;
778
779 // Open the file
780 yyin = fopen(path.c_str(), "r");
781 if (yyin == 0) {
782 failure("Could not open input file: \"%s\"", path.c_str());
783 }
784
785 // Create new scope and scan for includes
786 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000787 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000788 g_program = program;
789 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000790 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000791 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000792 if (yyparse() != 0) {
793 failure("Parser error during include pass.");
794 }
795 } catch (string x) {
796 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000797 }
798 fclose(yyin);
799
800 // Recursively parse all the include programs
801 vector<t_program*>& includes = program->get_includes();
802 vector<t_program*>::iterator iter;
803 for (iter = includes.begin(); iter != includes.end(); ++iter) {
804 parse(*iter, program);
805 }
806
David Reiss204420f2008-01-11 20:59:03 +0000807 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000808 g_parse_mode = PROGRAM;
809 g_program = program;
810 g_scope = program->scope();
811 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
812 g_parent_prefix = program->get_name() + ".";
813 g_curpath = path;
814 yyin = fopen(path.c_str(), "r");
815 if (yyin == 0) {
816 failure("Could not open input file: \"%s\"", path.c_str());
817 }
818 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000819 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000820 try {
821 if (yyparse() != 0) {
822 failure("Parser error during types pass.");
823 }
824 } catch (string x) {
825 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000826 }
827 fclose(yyin);
828}
829
830/**
831 * Generate code
832 */
David Reissbd0db882008-02-27 01:54:51 +0000833void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000834 // Oooohh, recursive code generation, hot!!
835 if (gen_recurse) {
836 const vector<t_program*>& includes = program->get_includes();
837 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000838 // Propogate output path from parent to child programs
839 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000840
David Reissbd0db882008-02-27 01:54:51 +0000841 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000842 }
843 }
844
845 // Generate code!
846 try {
847 pverbose("Program: %s\n", program->get_path().c_str());
848
David Reiss18bf22d2007-08-28 20:49:17 +0000849 // Compute fingerprints.
850 generate_all_fingerprints(program);
851
David Reiss1ac05802007-07-30 22:00:27 +0000852 if (dump_docs) {
853 dump_docstrings(program);
854 }
David Reissbd0db882008-02-27 01:54:51 +0000855
856 vector<string>::const_iterator iter;
857 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
858 t_generator* generator = t_generator_registry::get_generator(program, *iter);
859
860 if (generator == NULL) {
861 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
862 } else {
863 pverbose("Generating \"%s\"\n", iter->c_str());
864 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000865 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000866 }
867 }
868
Mark Sleef0712dc2006-10-25 19:03:57 +0000869 } catch (string s) {
870 printf("Error: %s\n", s.c_str());
871 } catch (const char* exc) {
872 printf("Error: %s\n", exc);
873 }
874
875}
876
877/**
Mark Sleef5377b32006-10-10 01:42:59 +0000878 * Parse it up.. then spit it back out, in pretty much every language. Alright
879 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000880 */
881int main(int argc, char** argv) {
882 int i;
dweatherford65b70752007-10-31 02:18:14 +0000883 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000884
Mark Sleeb15a68b2006-06-07 06:46:24 +0000885 // Setup time string
886 time_t now = time(NULL);
887 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000888
Mark Sleef0712dc2006-10-25 19:03:57 +0000889 // Check for necessary arguments, you gotta have at least a filename and
890 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000891 if (argc < 2) {
892 usage();
893 }
Mark Slee31985722006-05-24 21:45:31 +0000894
David Reissbd0db882008-02-27 01:54:51 +0000895 vector<string> generator_strings;
896
David Reiss9cc2c132008-02-27 01:54:47 +0000897 // Set the current path to a dummy value to make warning messages clearer.
898 g_curpath = "arguments";
899
Mark Sleef5377b32006-10-10 01:42:59 +0000900 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000901 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000902 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000903
Mark Sleefdbee812006-09-27 18:50:48 +0000904 arg = strtok(argv[i], " ");
905 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000906 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000907 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000908 ++arg;
909 }
910
David Reissdd08f6d2008-06-30 20:24:24 +0000911 if (strcmp(arg, "-version") == 0) {
912 version();
913 exit(1);
914 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000915 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000916 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000917 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000918 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000919 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000920 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000921 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000922 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000923 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000924 } else if (strcmp(arg, "-gen") == 0) {
925 arg = argv[++i];
926 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000927 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000928 usage();
929 }
930 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000931 } else if (strcmp(arg, "-dense") == 0) {
932 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000933 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000934 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000935 } else if (strcmp(arg, "-javabean") == 0) {
936 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000937 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000938 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000939 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000940 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000941 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000942 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000943 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000944 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000945 gen_phps = true;
946 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000947 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000948 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000949 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000950 gen_php = true;
951 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000952 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000953 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000954 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000955 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000956 } else if (strcmp(arg, "-rest") == 0) {
957 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000958 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000959 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +0000960 } else if (strcmp(arg, "-pyns") == 0) {
961 gen_py = true;
962 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000963 } else if (strcmp(arg, "-rb") == 0) {
964 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000965 } else if (strcmp(arg, "-xsd") == 0) {
966 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000967 } else if (strcmp(arg, "-perl") == 0) {
968 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +0000969 } else if (strcmp(arg, "-erl") == 0) {
970 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000971 } else if (strcmp(arg, "-ocaml") == 0) {
972 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000973 } else if (strcmp(arg, "-hs") == 0) {
974 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000975 } else if (strcmp(arg, "-cocoa") == 0) {
976 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +0000977 } else if (strcmp(arg, "-st") == 0) {
978 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +0000979 } else if (strcmp(arg, "-csharp") == 0) {
980 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +0000981 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
982 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000983 } else if (strcmp(arg, "-I") == 0) {
984 // An argument of "-I\ asdf" is invalid and has unknown results
985 arg = argv[++i];
986
987 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000988 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +0000989 usage();
990 }
991 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +0000992 } else if (strcmp(arg, "-o") == 0) {
993 arg = argv[++i];
994 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000995 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +0000996 usage();
Mark Slee5b743072007-11-13 04:00:29 +0000997 }
dweatherford65b70752007-10-31 02:18:14 +0000998 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +0000999
1000#ifdef MINGW
1001 //strip out trailing \ on Windows
1002 int last = out_path.length()-1;
1003 if (out_path[last] == '\\')
1004 {
1005 out_path.erase(last);
1006 }
1007#endif
1008
dweatherford65b70752007-10-31 02:18:14 +00001009 struct stat sb;
1010 if (stat(out_path.c_str(), &sb) < 0) {
1011 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1012 return -1;
1013 }
1014 if (! S_ISDIR(sb.st_mode)) {
1015 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1016 return -1;
1017 }
Mark Sleefdbee812006-09-27 18:50:48 +00001018 } else {
1019 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1020 usage();
1021 }
1022
1023 // Tokenize more
1024 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001025 }
1026 }
Mark Slee2c44d202007-05-16 02:18:07 +00001027
David Reissdd08f6d2008-06-30 20:24:24 +00001028 // if you're asking for version, you have a right not to pass a file
1029 if (strcmp(argv[argc-1], "-version") == 0) {
1030 version();
1031 exit(1);
1032 }
1033
David Reiss400cd4b2008-02-27 01:54:55 +00001034 // TODO(dreiss): Delete these when everyone is using the new hotness.
1035 if (gen_cpp) {
1036 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1037 string gen_string = "cpp:";
1038 if (gen_dense) {
1039 gen_string.append("dense,");
1040 }
1041 if (g_cpp_use_include_prefix) {
1042 gen_string.append("include_prefix,");
1043 }
1044 generator_strings.push_back(gen_string);
1045 }
David Reisse1404d22008-02-27 01:55:05 +00001046 if (gen_java) {
1047 pwarning(1, "-java is deprecated. Use --gen java");
1048 generator_strings.push_back("java");
1049 }
1050 if (gen_javabean) {
1051 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1052 generator_strings.push_back("java:beans");
1053 }
David Reiss861869b2008-03-27 21:41:23 +00001054 if (gen_csharp) {
1055 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1056 generator_strings.push_back("csharp");
1057 }
David Reiss558e3992008-03-27 21:41:40 +00001058 if (gen_py) {
1059 pwarning(1, "-py is deprecated. Use --gen py");
1060 generator_strings.push_back("py");
1061 }
David Reissa640cea2008-03-27 21:42:01 +00001062 if (gen_rb) {
1063 pwarning(1, "-rb is deprecated. Use --gen rb");
1064 generator_strings.push_back("rb");
1065 }
David Reiss2b386c52008-03-27 21:42:23 +00001066 if (gen_perl) {
1067 pwarning(1, "-perl is deprecated. Use --gen perl");
1068 generator_strings.push_back("perl");
1069 }
David Reissa9ea68b2009-02-17 20:28:24 +00001070 if (gen_php || gen_phpi) {
1071 pwarning(1, "-php is deprecated. Use --gen php");
1072 string gen_string = "php:";
1073 if (gen_phpi) {
1074 gen_string.append("inlined,");
1075 } else if(gen_phps) {
1076 gen_string.append("server,");
1077 } else if(gen_phpa) {
1078 gen_string.append("autoload,");
1079 } else if(gen_phpo) {
1080 gen_string.append("oop,");
1081 } else if(gen_rest) {
1082 gen_string.append("rest,");
1083 }
1084 generator_strings.push_back(gen_string);
1085 }
David Reissd01c64d2008-03-27 21:40:55 +00001086 if (gen_cocoa) {
1087 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1088 generator_strings.push_back("cocoa");
1089 }
David Reissa2047832009-02-17 20:27:54 +00001090 if (gen_erl) {
1091 pwarning(1, "-erl is deprecated. Use --gen erl");
1092 generator_strings.push_back("erl");
1093 }
David Reissa890b572008-03-27 21:40:35 +00001094 if (gen_st) {
1095 pwarning(1, "-st is deprecated. Use --gen st");
1096 generator_strings.push_back("st");
1097 }
David Reissf0521b12008-03-27 21:40:05 +00001098 if (gen_ocaml) {
1099 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1100 generator_strings.push_back("ocaml");
1101 }
David Reissaf3ab262008-03-27 21:40:16 +00001102 if (gen_hs) {
1103 pwarning(1, "-hs is deprecated. Use --gen hs");
1104 generator_strings.push_back("hs");
1105 }
David Reiss4ba67102009-02-17 20:28:06 +00001106 if (gen_xsd) {
1107 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1108 generator_strings.push_back("xsd");
1109 }
David Reisse1404d22008-02-27 01:55:05 +00001110
Mark Sleef0712dc2006-10-25 19:03:57 +00001111 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001112 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001113 fprintf(stderr, "!!! No output language(s) specified\n\n");
1114 usage();
1115 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001116
1117 // Real-pathify it
1118 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001119 if (argv[i] == NULL) {
1120 fprintf(stderr, "!!! Missing file name\n");
1121 usage();
1122 }
David Reiss204420f2008-01-11 20:59:03 +00001123 if (saferealpath(argv[i], rp) == NULL) {
1124 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001125 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001126 string input_file(rp);
1127
Mark Sleef5377b32006-10-10 01:42:59 +00001128 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001129 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001130 if (out_path.size()) {
1131 program->set_out_path(out_path);
1132 }
kholst76f2c882008-01-16 02:47:41 +00001133
David Reiss4b6a3c72008-02-27 22:28:12 +00001134 // Compute the cpp include prefix.
1135 // infer this from the filename passed in
1136 string input_filename = argv[i];
1137 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001138
David Reiss4b6a3c72008-02-27 22:28:12 +00001139 string::size_type last_slash = string::npos;
1140 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1141 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001142 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001143
David Reiss4b6a3c72008-02-27 22:28:12 +00001144 program->set_include_prefix(include_prefix);
1145
Mark Sleef0712dc2006-10-25 19:03:57 +00001146 // Initialize global types
1147 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1148 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001149 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1150 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001151 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1152 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001153 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1154 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1155 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1156 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1157 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1158 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001159
Mark Sleef5377b32006-10-10 01:42:59 +00001160 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001161 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001162
David Reiss9cc2c132008-02-27 01:54:47 +00001163 // The current path is not really relevant when we are doing generation.
1164 // Reset the variable to make warning messages clearer.
1165 g_curpath = "generation";
1166 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1167 // That is what shows up during argument parsing.
1168 yylineno = 1;
1169
Mark Sleef0712dc2006-10-25 19:03:57 +00001170 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001171 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001172
Mark Sleef0712dc2006-10-25 19:03:57 +00001173 // Clean up. Who am I kidding... this program probably orphans heap memory
1174 // all over the place, but who cares because it is about to exit and it is
1175 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001176
Mark Sleef0712dc2006-10-25 19:03:57 +00001177 delete program;
1178 delete g_type_void;
1179 delete g_type_string;
1180 delete g_type_bool;
1181 delete g_type_byte;
1182 delete g_type_i16;
1183 delete g_type_i32;
1184 delete g_type_i64;
1185 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001186
1187 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001188 return 0;
1189}