blob: e3295797f69af8e490f907b81785692fc27acb0f [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
2 * thrift - a lightweight cross-language rpc/serialization tool
3 *
4 * This file contains the main compiler engine for Thrift, which invokes the
5 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +00006 * code for each language lives in a file by the language name under the
7 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +00008 *
9 * @author Mark Slee <mcslee@facebook.com>
10 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdarg.h>
15#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000016#include <sys/types.h>
17#include <sys/stat.h>
Mark Slee31985722006-05-24 21:45:31 +000018
Mark Sleef0712dc2006-10-25 19:03:57 +000019// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000020#include "globals.h"
21
22#include "main.h"
23#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000024#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000025#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000026#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000027#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000028#include "generate/t_py_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000029
30using namespace std;
31
Mark Sleef5377b32006-10-10 01:42:59 +000032/**
33 * Global program tree
34 */
Mark Slee31985722006-05-24 21:45:31 +000035t_program* g_program;
36
Mark Sleef5377b32006-10-10 01:42:59 +000037/**
Mark Sleef0712dc2006-10-25 19:03:57 +000038 * Global types
39 */
40
41t_type* g_type_void;
42t_type* g_type_string;
43t_type* g_type_bool;
44t_type* g_type_byte;
45t_type* g_type_i16;
46t_type* g_type_i32;
47t_type* g_type_i64;
48t_type* g_type_double;
49
50/**
51 * Global scope
52 */
53t_scope* g_scope;
54
55/**
56 * Parent scope to also parse types
57 */
58t_scope* g_parent_scope;
59
60/**
61 * Prefix for putting types in parent scope
62 */
63string g_parent_prefix;
64
65/**
66 * Parsing pass
67 */
68PARSE_MODE g_parse_mode;
69
70/**
71 * Current directory of file being parsed
72 */
73string g_curdir;
74
75/**
76 * Current file being parsed
77 */
78string g_curpath;
79
80/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000081 * Search path for inclusions
82 */
Mark Slee2329a832006-11-09 00:23:30 +000083vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000084
85/**
Mark Sleef5377b32006-10-10 01:42:59 +000086 * Global debug state
87 */
Mark Slee31985722006-05-24 21:45:31 +000088int g_debug = 0;
89
Mark Sleef5377b32006-10-10 01:42:59 +000090/**
Mark Sleef0712dc2006-10-25 19:03:57 +000091 * Warning level
92 */
93int g_warn = 1;
94
95/**
96 * Verbose output
97 */
98int g_verbose = 0;
99
100/**
Mark Sleef5377b32006-10-10 01:42:59 +0000101 * Global time string
102 */
Mark Slee31985722006-05-24 21:45:31 +0000103char* g_time_str;
104
Mark Slee31985722006-05-24 21:45:31 +0000105/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 * Flags to control code generation
107 */
108bool gen_cpp = false;
109bool gen_java = false;
110bool gen_py = false;
111bool gen_php = false;
112bool gen_phpi = false;
113bool gen_recurse = false;
114
115/**
Mark Slee31985722006-05-24 21:45:31 +0000116 * Report an error to the user. This is called yyerror for historical
117 * reasons (lex and yacc expect the error reporting routine to be called
118 * this). Call this function to report any errors to the user.
119 * yyerror takes printf style arguments.
120 *
121 * @param fmt C format string followed by additional arguments
122 */
123void yyerror(char* fmt, ...) {
124 va_list args;
125 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000126 "[ERROR:%s:%d] (last token was '%s')\n",
127 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000128 yylineno,
129 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000130
131 va_start(args, fmt);
132 vfprintf(stderr, fmt, args);
133 va_end(args);
134
135 fprintf(stderr, "\n");
136}
137
138/**
139 * Prints a debug message from the parser.
140 *
141 * @param fmt C format string followed by additional arguments
142 */
143void pdebug(char* fmt, ...) {
144 if (g_debug == 0) {
145 return;
146 }
147 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000148 printf("[PARSE] ");
149 va_start(args, fmt);
150 vprintf(fmt, args);
151 va_end(args);
152 printf("\n");
153}
154
155/**
156 * Prints a verbose output mode message
157 *
158 * @param fmt C format string followed by additional arguments
159 */
160void pverbose(char* fmt, ...) {
161 if (g_verbose == 0) {
162 return;
163 }
164 va_list args;
165 va_start(args, fmt);
166 vprintf(fmt, args);
167 va_end(args);
168}
169
170/**
171 * Prints a warning message
172 *
173 * @param fmt C format string followed by additional arguments
174 */
175void pwarning(int level, char* fmt, ...) {
176 if (g_warn < level) {
177 return;
178 }
179 va_list args;
180 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000181 va_start(args, fmt);
182 vprintf(fmt, args);
183 va_end(args);
184 printf("\n");
185}
186
187/**
188 * Prints a failure message and exits
189 *
190 * @param fmt C format string followed by additional arguments
191 */
192void failure(char* fmt, ...) {
193 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000194 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000195 va_start(args, fmt);
196 vfprintf(stderr, fmt, args);
197 va_end(args);
198 printf("\n");
199 exit(1);
200}
201
202/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000203 * Converts a string filename into a thrift program name
204 */
205string program_name(string filename) {
206 string::size_type slash = filename.rfind("/");
207 if (slash != string::npos) {
208 filename = filename.substr(slash+1);
209 }
210 string::size_type dot = filename.rfind(".");
211 if (dot != string::npos) {
212 filename = filename.substr(0, dot);
213 }
214 return filename;
215}
216
217/**
218 * Gets the directory path of a filename
219 */
220string directory_name(string filename) {
221 string::size_type slash = filename.rfind("/");
222 // No slash, just use the current directory
223 if (slash == string::npos) {
224 return ".";
225 }
226 return filename.substr(0, slash);
227}
228
229/**
230 * Finds the appropriate file path for the given filename
231 */
232string include_file(string filename) {
233 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000234 if (filename[0] == '/') {
235 // Realpath!
236 char rp[PATH_MAX];
237 if (realpath(filename.c_str(), rp) == NULL) {
238 pwarning(0, "Cannot open include file %s\n", filename.c_str());
239 return std::string();
240 }
241
242 // Stat this files
243 struct stat finfo;
244 if (stat(rp, &finfo) == 0) {
245 return rp;
246 }
247 } else { // relative path, start searching
248 // new search path with current dir global
249 vector<string> sp = g_incl_searchpath;
250 sp.insert(sp.begin(), g_curdir);
251
252 // iterate through paths
253 vector<string>::iterator it;
254 for (it = sp.begin(); it != sp.end(); it++) {
255 string sfilename = *(it) + "/" + filename;
256
257 // Realpath!
258 char rp[PATH_MAX];
259 if (realpath(sfilename.c_str(), rp) == NULL) {
260 continue;
261 }
262
263 // Stat this files
264 struct stat finfo;
265 if (stat(rp, &finfo) == 0) {
266 return rp;
267 }
268 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000269 }
270
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 // Uh oh
272 pwarning(0, "Could not find include file %s\n", filename.c_str());
273 return std::string();
274}
275
276/**
Mark Slee31985722006-05-24 21:45:31 +0000277 * Diplays the usage message and then exits with an error code.
278 */
279void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000280 fprintf(stderr, "Usage: thrift [options] file\n");
281 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000282 fprintf(stderr, " -cpp Generate C++ output files\n");
283 fprintf(stderr, " -java Generate Java output files\n");
284 fprintf(stderr, " -php Generate PHP output files\n");
285 fprintf(stderr, " -phpi Generate PHP inlined files\n");
286 fprintf(stderr, " -py Generate Python output files\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +0000287 fprintf(stderr, " -I dir Add a directory to the list of directories \n");
288 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000289 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
290 fprintf(stderr, " -strict Strict compiler warnings on\n");
291 fprintf(stderr, " -v[erbose] Verbose mode\n");
292 fprintf(stderr, " -r[ecurse] Also generate included files\n");
293 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000294 exit(1);
295}
296
297/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000298 * Parses a program
299 */
300void parse(t_program* program, t_program* parent_program) {
301 // Get scope file path
302 string path = program->get_path();
303
304 // Set current dir global, which is used in the include_file function
305 g_curdir = directory_name(path);
306 g_curpath = path;
307
308 // Open the file
309 yyin = fopen(path.c_str(), "r");
310 if (yyin == 0) {
311 failure("Could not open input file: \"%s\"", path.c_str());
312 }
313
314 // Create new scope and scan for includes
315 pverbose("Scanning %s for includes\n", path.c_str());
316 g_parse_mode = INCLUDES;
317 g_program = program;
318 g_scope = program->scope();
319 if (yyparse() != 0) {
320 failure("Parser error during include pass.");
321 }
322 fclose(yyin);
323
324 // Recursively parse all the include programs
325 vector<t_program*>& includes = program->get_includes();
326 vector<t_program*>::iterator iter;
327 for (iter = includes.begin(); iter != includes.end(); ++iter) {
328 parse(*iter, program);
329 }
330
331 // Parse the program the file
332 g_parse_mode = PROGRAM;
333 g_program = program;
334 g_scope = program->scope();
335 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
336 g_parent_prefix = program->get_name() + ".";
337 g_curpath = path;
338 yyin = fopen(path.c_str(), "r");
339 if (yyin == 0) {
340 failure("Could not open input file: \"%s\"", path.c_str());
341 }
342 pverbose("Parsing %s for types\n", path.c_str());
343 if (yyparse() != 0) {
344 failure("Parser error during types pass.");
345 }
346 fclose(yyin);
347}
348
349/**
350 * Generate code
351 */
352void generate(t_program* program) {
353 // Oooohh, recursive code generation, hot!!
354 if (gen_recurse) {
355 const vector<t_program*>& includes = program->get_includes();
356 for (size_t i = 0; i < includes.size(); ++i) {
357 generate(includes[i]);
358 }
359 }
360
361 // Generate code!
362 try {
363 pverbose("Program: %s\n", program->get_path().c_str());
364
365 if (gen_cpp) {
366 pverbose("Generating C++\n");
367 t_cpp_generator* cpp = new t_cpp_generator(program);
368 cpp->generate_program();
369 delete cpp;
370 }
371
372 if (gen_java) {
373 pverbose("Generating Java\n");
374 t_java_generator* java = new t_java_generator(program);
375 java->generate_program();
376 delete java;
377 }
378
379 if (gen_php) {
380 pverbose("Generating PHP\n");
381 t_php_generator* php = new t_php_generator(program, false);
382 php->generate_program();
383 delete php;
384 }
385
386 if (gen_phpi) {
387 pverbose("Generating PHP-inline\n");
388 t_php_generator* phpi = new t_php_generator(program, true);
389 phpi->generate_program();
390 delete phpi;
391 }
392
393 if (gen_py) {
394 pverbose("Generating Python\n");
395 t_py_generator* py = new t_py_generator(program);
396 py->generate_program();
397 delete py;
398 }
399 } catch (string s) {
400 printf("Error: %s\n", s.c_str());
401 } catch (const char* exc) {
402 printf("Error: %s\n", exc);
403 }
404
405}
406
407/**
Mark Sleef5377b32006-10-10 01:42:59 +0000408 * Parse it up.. then spit it back out, in pretty much every language. Alright
409 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000410 */
411int main(int argc, char** argv) {
412 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000413
Mark Sleeb15a68b2006-06-07 06:46:24 +0000414 // Setup time string
415 time_t now = time(NULL);
416 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000417
Mark Sleef0712dc2006-10-25 19:03:57 +0000418 // Check for necessary arguments, you gotta have at least a filename and
419 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000420 if (argc < 2) {
421 usage();
422 }
Mark Slee31985722006-05-24 21:45:31 +0000423
Mark Sleef5377b32006-10-10 01:42:59 +0000424 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000425 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000426 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000427
Mark Sleefdbee812006-09-27 18:50:48 +0000428 arg = strtok(argv[i], " ");
429 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000430 // Treat double dashes as single dashes
431 if (false && arg[0] == '-') {
432 ++arg;
433 }
434
435 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000436 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000437 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000438 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000439 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000440 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000441 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000442 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000443 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000444 gen_recurse = true;
Mark Slee2329a832006-11-09 00:23:30 +0000445 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000446 gen_cpp = true;
Mark Slee2329a832006-11-09 00:23:30 +0000447 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000448 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000449 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000450 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000451 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000452 gen_phpi = true;
Mark Slee2329a832006-11-09 00:23:30 +0000453 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000454 gen_py = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000455 } else if (strcmp(arg, "-I") == 0) {
456 // An argument of "-I\ asdf" is invalid and has unknown results
457 arg = argv[++i];
458
459 if (arg == NULL) {
460 fprintf(stderr, "!!! Missing Include directory");
461 usage();
462 }
463 g_incl_searchpath.push_back(arg);
Mark Sleefdbee812006-09-27 18:50:48 +0000464 } else {
465 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
466 usage();
467 }
468
469 // Tokenize more
470 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000471 }
472 }
473
Mark Sleef0712dc2006-10-25 19:03:57 +0000474 // You gotta generate something!
Mark Sleef5377b32006-10-10 01:42:59 +0000475 if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000476 fprintf(stderr, "!!! No output language(s) specified\n\n");
477 usage();
478 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000479
480 // Real-pathify it
481 char rp[PATH_MAX];
482 if (realpath(argv[i], rp) == NULL) {
483 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000484 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000485 string input_file(rp);
486
Mark Sleef5377b32006-10-10 01:42:59 +0000487 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000488 t_program* program = new t_program(input_file);
489
490 // Initialize global types
491 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
492 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
493 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
494 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
495 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
496 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
497 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
498 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +0000499
Mark Sleef5377b32006-10-10 01:42:59 +0000500 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +0000501 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +0000502
Mark Sleef0712dc2006-10-25 19:03:57 +0000503 // Generate it!
504 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +0000505
Mark Sleef0712dc2006-10-25 19:03:57 +0000506 // Clean up. Who am I kidding... this program probably orphans heap memory
507 // all over the place, but who cares because it is about to exit and it is
508 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +0000509
Mark Sleef0712dc2006-10-25 19:03:57 +0000510 delete program;
511 delete g_type_void;
512 delete g_type_string;
513 delete g_type_bool;
514 delete g_type_byte;
515 delete g_type_i16;
516 delete g_type_i32;
517 delete g_type_i64;
518 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +0000519
520 // Finished
Mark Slee31985722006-05-24 21:45:31 +0000521 return 0;
522}