blob: 00780557e186a4a0b702646396a38ff4f9b55b89 [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
18#include <stdlib.h>
19#include <stdio.h>
20#include <stdarg.h>
21#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000022#include <sys/types.h>
23#include <sys/stat.h>
Mark Slee31985722006-05-24 21:45:31 +000024
Mark Sleef0712dc2006-10-25 19:03:57 +000025// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000026#include "globals.h"
27
28#include "main.h"
29#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000030#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000031#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000032#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000033#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000034#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000035#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000036#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000037#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000038#include "generate/t_erl_generator.h"
Christopher Pirob97b89d2007-09-18 00:07:42 +000039#include "generate/t_ocaml_generator.h"
iproctorff8eb922007-07-25 19:06:13 +000040#include "generate/t_hs_generator.h"
Mark Slee7e9eea42007-09-10 21:00:23 +000041#include "generate/t_cocoa_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000042
43using 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/**
Mark Sleef5377b32006-10-10 01:42:59 +0000101 * Global debug state
102 */
Mark Slee31985722006-05-24 21:45:31 +0000103int g_debug = 0;
104
Mark Sleef5377b32006-10-10 01:42:59 +0000105/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 * Warning level
107 */
108int g_warn = 1;
109
110/**
111 * Verbose output
112 */
113int g_verbose = 0;
114
115/**
Mark Sleef5377b32006-10-10 01:42:59 +0000116 * Global time string
117 */
Mark Slee31985722006-05-24 21:45:31 +0000118char* g_time_str;
119
Mark Slee31985722006-05-24 21:45:31 +0000120/**
David Reisscbd4bac2007-08-14 17:12:33 +0000121 * The last parsed doctext comment.
122 */
123char* g_doctext;
124
125/**
126 * The location of the last parsed doctext comment.
127 */
128int g_doctext_lineno;
129
130/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000131 * Flags to control code generation
132 */
133bool gen_cpp = false;
134bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000135bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000136bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000137bool gen_py = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000138bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000139bool gen_php = false;
140bool gen_phpi = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000141bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000142bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000143bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000144bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000145bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000146bool gen_cocoa = false;
David Reissd779cbe2007-08-31 01:42:55 +0000147bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000148bool gen_recurse = false;
149
150/**
Mark Slee31985722006-05-24 21:45:31 +0000151 * Report an error to the user. This is called yyerror for historical
152 * reasons (lex and yacc expect the error reporting routine to be called
153 * this). Call this function to report any errors to the user.
154 * yyerror takes printf style arguments.
155 *
156 * @param fmt C format string followed by additional arguments
157 */
158void yyerror(char* fmt, ...) {
159 va_list args;
160 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000161 "[ERROR:%s:%d] (last token was '%s')\n",
162 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000163 yylineno,
164 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000165
166 va_start(args, fmt);
167 vfprintf(stderr, fmt, args);
168 va_end(args);
169
170 fprintf(stderr, "\n");
171}
172
173/**
174 * Prints a debug message from the parser.
175 *
176 * @param fmt C format string followed by additional arguments
177 */
178void pdebug(char* fmt, ...) {
179 if (g_debug == 0) {
180 return;
181 }
182 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000183 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000184 va_start(args, fmt);
185 vprintf(fmt, args);
186 va_end(args);
187 printf("\n");
188}
189
190/**
191 * Prints a verbose output mode message
192 *
193 * @param fmt C format string followed by additional arguments
194 */
195void pverbose(char* fmt, ...) {
196 if (g_verbose == 0) {
197 return;
198 }
199 va_list args;
200 va_start(args, fmt);
201 vprintf(fmt, args);
202 va_end(args);
203}
204
205/**
206 * Prints a warning message
207 *
208 * @param fmt C format string followed by additional arguments
209 */
210void pwarning(int level, char* fmt, ...) {
211 if (g_warn < level) {
212 return;
213 }
214 va_list args;
215 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000216 va_start(args, fmt);
217 vprintf(fmt, args);
218 va_end(args);
219 printf("\n");
220}
221
222/**
223 * Prints a failure message and exits
224 *
225 * @param fmt C format string followed by additional arguments
226 */
Mark Slee30152872006-11-28 01:24:07 +0000227void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000228 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000229 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000230 va_start(args, fmt);
231 vfprintf(stderr, fmt, args);
232 va_end(args);
233 printf("\n");
234 exit(1);
235}
236
237/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000238 * Converts a string filename into a thrift program name
239 */
240string program_name(string filename) {
241 string::size_type slash = filename.rfind("/");
242 if (slash != string::npos) {
243 filename = filename.substr(slash+1);
244 }
245 string::size_type dot = filename.rfind(".");
246 if (dot != string::npos) {
247 filename = filename.substr(0, dot);
248 }
249 return filename;
250}
251
252/**
253 * Gets the directory path of a filename
254 */
255string directory_name(string filename) {
256 string::size_type slash = filename.rfind("/");
257 // No slash, just use the current directory
258 if (slash == string::npos) {
259 return ".";
260 }
261 return filename.substr(0, slash);
262}
263
264/**
265 * Finds the appropriate file path for the given filename
266 */
267string include_file(string filename) {
268 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000269 if (filename[0] == '/') {
270 // Realpath!
271 char rp[PATH_MAX];
272 if (realpath(filename.c_str(), rp) == NULL) {
273 pwarning(0, "Cannot open include file %s\n", filename.c_str());
274 return std::string();
275 }
Mark Slee2c44d202007-05-16 02:18:07 +0000276
277 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000278 struct stat finfo;
279 if (stat(rp, &finfo) == 0) {
280 return rp;
281 }
282 } else { // relative path, start searching
283 // new search path with current dir global
284 vector<string> sp = g_incl_searchpath;
285 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000286
Martin Kraemer32c66e12006-11-09 00:06:36 +0000287 // iterate through paths
288 vector<string>::iterator it;
289 for (it = sp.begin(); it != sp.end(); it++) {
290 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000291
Martin Kraemer32c66e12006-11-09 00:06:36 +0000292 // Realpath!
293 char rp[PATH_MAX];
294 if (realpath(sfilename.c_str(), rp) == NULL) {
295 continue;
296 }
Mark Slee2c44d202007-05-16 02:18:07 +0000297
Martin Kraemer32c66e12006-11-09 00:06:36 +0000298 // Stat this files
299 struct stat finfo;
300 if (stat(rp, &finfo) == 0) {
301 return rp;
302 }
303 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000304 }
Mark Slee2c44d202007-05-16 02:18:07 +0000305
Mark Sleef0712dc2006-10-25 19:03:57 +0000306 // Uh oh
307 pwarning(0, "Could not find include file %s\n", filename.c_str());
308 return std::string();
309}
310
311/**
David Reisscbd4bac2007-08-14 17:12:33 +0000312 * Clears any previously stored doctext string.
313 * Also prints a warning if we are discarding information.
314 */
315void clear_doctext() {
316 if (g_doctext != NULL) {
317 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
318 }
319 free(g_doctext);
320 g_doctext = NULL;
321}
322
323/**
David Reiss1ac05802007-07-30 22:00:27 +0000324 * Cleans up text commonly found in doxygen-like comments
325 *
326 * Warning: if you mix tabs and spaces in a non-uniform way,
327 * you will get what you deserve.
328 */
329char* clean_up_doctext(char* doctext) {
330 // Convert to C++ string, and remove Windows's carriage returns.
331 string docstring = doctext;
332 docstring.erase(
333 remove(docstring.begin(), docstring.end(), '\r'),
334 docstring.end());
335
336 // Separate into lines.
337 vector<string> lines;
338 string::size_type pos = string::npos;
339 string::size_type last;
340 while (true) {
341 last = (pos == string::npos) ? 0 : pos+1;
342 pos = docstring.find('\n', last);
343 if (pos == string::npos) {
344 // First bit of cleaning. If the last line is only whitespace, drop it.
345 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
346 if (nonwhite != string::npos) {
347 lines.push_back(docstring.substr(last));
348 }
349 break;
350 }
351 lines.push_back(docstring.substr(last, pos-last));
352 }
353
354 // A very profound docstring.
355 if (lines.empty()) {
356 return NULL;
357 }
358
359 // Clear leading whitespace from the first line.
360 pos = lines.front().find_first_not_of(" \t");
361 lines.front().erase(0, pos);
362
363 // If every nonblank line after the first has the same number of spaces/tabs,
364 // then a star, remove them.
365 bool have_prefix = true;
366 bool found_prefix = false;
367 string::size_type prefix_len = 0;
368 vector<string>::iterator l_iter;
369 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
370 if (l_iter->empty()) {
371 continue;
372 }
373
374 pos = l_iter->find_first_not_of(" \t");
375 if (!found_prefix) {
376 if (pos != string::npos) {
377 if (l_iter->at(pos) == '*') {
378 found_prefix = true;
379 prefix_len = pos;
380 } else {
381 have_prefix = false;
382 break;
383 }
384 } else {
385 // Whitespace-only line. Truncate it.
386 l_iter->clear();
387 }
388 } else if (l_iter->size() > pos
389 && l_iter->at(pos) == '*'
390 && pos == prefix_len) {
391 // Business as usual.
392 } else if (pos == string::npos) {
393 // Whitespace-only line. Let's truncate it for them.
394 l_iter->clear();
395 } else {
396 // The pattern has been broken.
397 have_prefix = false;
398 break;
399 }
400 }
401
402 // If our prefix survived, delete it from every line.
403 if (have_prefix) {
404 // Get the star too.
405 prefix_len++;
406 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
407 l_iter->erase(0, prefix_len);
408 }
409 }
410
411 // Now delete the minimum amount of leading whitespace from each line.
412 prefix_len = string::npos;
413 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
414 if (l_iter->empty()) {
415 continue;
416 }
417 pos = l_iter->find_first_not_of(" \t");
418 if (pos != string::npos
419 && (prefix_len == string::npos || pos < prefix_len)) {
420 prefix_len = pos;
421 }
422 }
423
424 // If our prefix survived, delete it from every line.
425 if (prefix_len != string::npos) {
426 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
427 l_iter->erase(0, prefix_len);
428 }
429 }
430
431 // Remove trailing whitespace from every line.
432 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
433 pos = l_iter->find_last_not_of(" \t");
434 if (pos != string::npos && pos != l_iter->length()-1) {
435 l_iter->erase(pos+1);
436 }
437 }
438
439 // If the first line is empty, remove it.
440 // Don't do this earlier because a lot of steps skip the first line.
441 if (lines.front().empty()) {
442 lines.erase(lines.begin());
443 }
444
445 // Now rejoin the lines and copy them back into doctext.
446 docstring.clear();
447 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
448 docstring += *l_iter;
449 docstring += '\n';
450 }
451
452 assert(docstring.length() <= strlen(doctext));
453 strcpy(doctext, docstring.c_str());
454 return doctext;
455}
456
457/** Set to true to debug docstring parsing */
458static bool dump_docs = false;
459
460/**
461 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000462 * Only works for top-level definitions and the whole program doc
463 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000464 */
465void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000466 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000467 if (!progdoc.empty()) {
468 printf("Whole program doc:\n%s\n", progdoc.c_str());
469 }
David Reiss1ac05802007-07-30 22:00:27 +0000470 const vector<t_typedef*>& typedefs = program->get_typedefs();
471 vector<t_typedef*>::const_iterator t_iter;
472 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
473 t_typedef* td = *t_iter;
474 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000475 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
476 }
477 }
478 const vector<t_enum*>& enums = program->get_enums();
479 vector<t_enum*>::const_iterator e_iter;
480 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
481 t_enum* en = *e_iter;
482 if (en->has_doc()) {
483 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
484 }
485 }
486 const vector<t_const*>& consts = program->get_consts();
487 vector<t_const*>::const_iterator c_iter;
488 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
489 t_const* co = *c_iter;
490 if (co->has_doc()) {
491 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
492 }
493 }
494 const vector<t_struct*>& structs = program->get_structs();
495 vector<t_struct*>::const_iterator s_iter;
496 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
497 t_struct* st = *s_iter;
498 if (st->has_doc()) {
499 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
500 }
501 }
502 const vector<t_struct*>& xceptions = program->get_xceptions();
503 vector<t_struct*>::const_iterator x_iter;
504 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
505 t_struct* xn = *x_iter;
506 if (xn->has_doc()) {
507 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
508 }
509 }
510 const vector<t_service*>& services = program->get_services();
511 vector<t_service*>::const_iterator v_iter;
512 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
513 t_service* sv = *v_iter;
514 if (sv->has_doc()) {
515 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000516 }
517 }
518}
519
520/**
David Reiss18bf22d2007-08-28 20:49:17 +0000521 * Call generate_fingerprint for every structure.
522 */
523void generate_all_fingerprints(t_program* program) {
524 const vector<t_struct*>& structs = program->get_structs();
525 vector<t_struct*>::const_iterator s_iter;
526 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
527 t_struct* st = *s_iter;
528 st->generate_fingerprint();
529 }
530
David Reissd779cbe2007-08-31 01:42:55 +0000531 const vector<t_struct*>& xceptions = program->get_xceptions();
532 vector<t_struct*>::const_iterator x_iter;
533 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
534 t_struct* st = *x_iter;
535 st->generate_fingerprint();
536 }
537
David Reiss47557bc2007-09-04 21:31:04 +0000538 g_type_void->generate_fingerprint();
539
David Reiss18bf22d2007-08-28 20:49:17 +0000540 // If you want to generate fingerprints for implicit structures, start here.
541 /*
542 const vector<t_service*>& services = program->get_services();
543 vector<t_service*>::const_iterator v_iter;
544 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
545 t_service* sv = *v_iter;
546 }
547 */
548}
549
550/**
Mark Slee31985722006-05-24 21:45:31 +0000551 * Diplays the usage message and then exits with an error code.
552 */
553void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000554 fprintf(stderr, "Usage: thrift [options] file\n");
555 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000556 fprintf(stderr, " -cpp Generate C++ output files\n");
557 fprintf(stderr, " -java Generate Java output files\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000558 fprintf(stderr, " -javabean Generate Java bean-style output files\n");
Mark Slee2329a832006-11-09 00:23:30 +0000559 fprintf(stderr, " -php Generate PHP output files\n");
560 fprintf(stderr, " -phpi Generate PHP inlined files\n");
561 fprintf(stderr, " -py Generate Python output files\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000562 fprintf(stderr, " -rb Generate Ruby output files\n");
563 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000564 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000565 fprintf(stderr, " -erl Generate Erlang output files\n");
Christopher Pirob97b89d2007-09-18 00:07:42 +0000566 fprintf(stderr, " -ocaml Generate OCaml output files\n");
iproctorff8eb922007-07-25 19:06:13 +0000567 fprintf(stderr, " -hs Generate Haskell output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000568 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
David Reissd779cbe2007-08-31 01:42:55 +0000569 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000570 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000571 fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n");
572 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000573 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
574 fprintf(stderr, " -strict Strict compiler warnings on\n");
575 fprintf(stderr, " -v[erbose] Verbose mode\n");
576 fprintf(stderr, " -r[ecurse] Also generate included files\n");
577 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000578 exit(1);
579}
580
581/**
Mark Slee30152872006-11-28 01:24:07 +0000582 * You know, when I started working on Thrift I really thought it wasn't going
583 * to become a programming language because it was just a generator and it
584 * wouldn't need runtime type information and all that jazz. But then we
585 * decided to add constants, and all of a sudden that means runtime type
586 * validation and inference, except the "runtime" is the code generator
587 * runtime. Shit. I've been had.
588 */
589void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
590 if (type->is_void()) {
591 throw "type error: cannot declare a void const: " + name;
592 }
593
594 if (type->is_base_type()) {
595 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
596 switch (tbase) {
597 case t_base_type::TYPE_STRING:
598 if (value->get_type() != t_const_value::CV_STRING) {
599 throw "type error: const \"" + name + "\" was declared as string";
600 }
601 break;
602 case t_base_type::TYPE_BOOL:
603 if (value->get_type() != t_const_value::CV_INTEGER) {
604 throw "type error: const \"" + name + "\" was declared as bool";
605 }
606 break;
607 case t_base_type::TYPE_BYTE:
608 if (value->get_type() != t_const_value::CV_INTEGER) {
609 throw "type error: const \"" + name + "\" was declared as byte";
610 }
611 break;
612 case t_base_type::TYPE_I16:
613 if (value->get_type() != t_const_value::CV_INTEGER) {
614 throw "type error: const \"" + name + "\" was declared as i16";
615 }
616 break;
617 case t_base_type::TYPE_I32:
618 if (value->get_type() != t_const_value::CV_INTEGER) {
619 throw "type error: const \"" + name + "\" was declared as i32";
620 }
621 break;
622 case t_base_type::TYPE_I64:
623 if (value->get_type() != t_const_value::CV_INTEGER) {
624 throw "type error: const \"" + name + "\" was declared as i64";
625 }
626 break;
627 case t_base_type::TYPE_DOUBLE:
628 if (value->get_type() != t_const_value::CV_INTEGER &&
629 value->get_type() != t_const_value::CV_DOUBLE) {
630 throw "type error: const \"" + name + "\" was declared as double";
631 }
632 break;
633 default:
David Reissdd7796f2007-08-28 21:09:06 +0000634 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000635 }
636 } else if (type->is_enum()) {
637 if (value->get_type() != t_const_value::CV_INTEGER) {
638 throw "type error: const \"" + name + "\" was declared as enum";
639 }
640 } else if (type->is_struct() || type->is_xception()) {
641 if (value->get_type() != t_const_value::CV_MAP) {
642 throw "type error: const \"" + name + "\" was declared as struct/xception";
643 }
644 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
645 vector<t_field*>::const_iterator f_iter;
646
647 const map<t_const_value*, t_const_value*>& val = value->get_map();
648 map<t_const_value*, t_const_value*>::const_iterator v_iter;
649 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
650 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
651 throw "type error: " + name + " struct key must be string";
652 }
653 t_type* field_type = NULL;
654 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
655 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
656 field_type = (*f_iter)->get_type();
657 }
658 }
659 if (field_type == NULL) {
660 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
661 }
662
663 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
664 }
665 } else if (type->is_map()) {
666 t_type* k_type = ((t_map*)type)->get_key_type();
667 t_type* v_type = ((t_map*)type)->get_val_type();
668 const map<t_const_value*, t_const_value*>& val = value->get_map();
669 map<t_const_value*, t_const_value*>::const_iterator v_iter;
670 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
671 validate_const_rec(name + "<key>", k_type, v_iter->first);
672 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000673 }
Mark Slee30152872006-11-28 01:24:07 +0000674 } else if (type->is_list() || type->is_set()) {
675 t_type* e_type;
676 if (type->is_list()) {
677 e_type = ((t_list*)type)->get_elem_type();
678 } else {
679 e_type = ((t_set*)type)->get_elem_type();
680 }
681 const vector<t_const_value*>& val = value->get_list();
682 vector<t_const_value*>::const_iterator v_iter;
683 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
684 validate_const_rec(name + "<elem>", e_type, *v_iter);
685 }
686 }
687}
688
689/**
690 * Check the type of the parsed const information against its declared type
691 */
692void validate_const_type(t_const* c) {
693 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
694}
695
696/**
Mark Slee7ff32452007-02-01 05:26:18 +0000697 * Check the type of a default value assigned to a field.
698 */
699void validate_field_value(t_field* field, t_const_value* cv) {
700 validate_const_rec(field->get_name(), field->get_type(), cv);
701}
702
703/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000704 * Parses a program
705 */
Mark Slee2c44d202007-05-16 02:18:07 +0000706void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000707 // Get scope file path
708 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000709
Mark Sleef0712dc2006-10-25 19:03:57 +0000710 // Set current dir global, which is used in the include_file function
711 g_curdir = directory_name(path);
712 g_curpath = path;
713
714 // Open the file
715 yyin = fopen(path.c_str(), "r");
716 if (yyin == 0) {
717 failure("Could not open input file: \"%s\"", path.c_str());
718 }
719
720 // Create new scope and scan for includes
721 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000722 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000723 g_program = program;
724 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000725 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000726 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000727 if (yyparse() != 0) {
728 failure("Parser error during include pass.");
729 }
730 } catch (string x) {
731 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000732 }
733 fclose(yyin);
734
735 // Recursively parse all the include programs
736 vector<t_program*>& includes = program->get_includes();
737 vector<t_program*>::iterator iter;
738 for (iter = includes.begin(); iter != includes.end(); ++iter) {
739 parse(*iter, program);
740 }
741
742 // Parse the program the file
743 g_parse_mode = PROGRAM;
744 g_program = program;
745 g_scope = program->scope();
746 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
747 g_parent_prefix = program->get_name() + ".";
748 g_curpath = path;
749 yyin = fopen(path.c_str(), "r");
750 if (yyin == 0) {
751 failure("Could not open input file: \"%s\"", path.c_str());
752 }
753 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000754 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000755 try {
756 if (yyparse() != 0) {
757 failure("Parser error during types pass.");
758 }
759 } catch (string x) {
760 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000761 }
762 fclose(yyin);
763}
764
765/**
766 * Generate code
767 */
768void generate(t_program* program) {
769 // Oooohh, recursive code generation, hot!!
770 if (gen_recurse) {
771 const vector<t_program*>& includes = program->get_includes();
772 for (size_t i = 0; i < includes.size(); ++i) {
773 generate(includes[i]);
774 }
775 }
776
777 // Generate code!
778 try {
779 pverbose("Program: %s\n", program->get_path().c_str());
780
David Reiss18bf22d2007-08-28 20:49:17 +0000781 // Compute fingerprints.
782 generate_all_fingerprints(program);
783
Mark Sleef0712dc2006-10-25 19:03:57 +0000784 if (gen_cpp) {
785 pverbose("Generating C++\n");
David Reissd779cbe2007-08-31 01:42:55 +0000786 t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense);
Mark Sleef0712dc2006-10-25 19:03:57 +0000787 cpp->generate_program();
788 delete cpp;
789 }
790
791 if (gen_java) {
792 pverbose("Generating Java\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000793 t_java_generator* java = new t_java_generator(program, false);
794 java->generate_program();
795 delete java;
796 }
797
798 if (gen_javabean) {
799 pverbose("Generating Java Beans\n");
800 t_java_generator* java = new t_java_generator(program, true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000801 java->generate_program();
802 delete java;
803 }
804
805 if (gen_php) {
806 pverbose("Generating PHP\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000807 t_php_generator* php = new t_php_generator(program, false, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000808 php->generate_program();
809 delete php;
810 }
811
812 if (gen_phpi) {
813 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000814 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000815 phpi->generate_program();
816 delete phpi;
817 }
818
819 if (gen_py) {
820 pverbose("Generating Python\n");
821 t_py_generator* py = new t_py_generator(program);
822 py->generate_program();
823 delete py;
824 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000825
Mark Slee6d7d5952007-01-27 01:44:22 +0000826 if (gen_rb) {
827 pverbose("Generating Ruby\n");
828 t_rb_generator* rb = new t_rb_generator(program);
829 rb->generate_program();
830 delete rb;
831 }
832
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000833 if (gen_xsd) {
834 pverbose("Generating XSD\n");
835 t_xsd_generator* xsd = new t_xsd_generator(program);
836 xsd->generate_program();
837 delete xsd;
838 }
839
Mark Slee2c44d202007-05-16 02:18:07 +0000840 if (gen_perl) {
841 pverbose("Generating PERL\n");
842 t_perl_generator* perl = new t_perl_generator(program);
843 perl->generate_program();
844 delete perl;
845 }
846
Christopher Pirob97b89d2007-09-18 00:07:42 +0000847 if (gen_erl) {
848 pverbose("Generating Erlang\n");
849 t_erl_generator* erl = new t_erl_generator(program);
850 erl->generate_program();
851 delete erl;
852 }
853
iproctor9a41a0c2007-07-16 21:59:24 +0000854 if (gen_ocaml) {
855 pverbose("Generating OCaml\n");
856 t_ocaml_generator* ocaml = new t_ocaml_generator(program);
857 ocaml->generate_program();
858 delete ocaml;
859 }
860
iproctorff8eb922007-07-25 19:06:13 +0000861 if (gen_hs) {
862 pverbose("Generating Haskell\n");
863 t_hs_generator* hs = new t_hs_generator(program);
864 hs->generate_program();
865 delete hs;
866 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000867
Mark Slee7e9eea42007-09-10 21:00:23 +0000868 if (gen_cocoa) {
869 pverbose("Generating Cocoa/Objective-C\n");
870 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
871 cocoa->generate_program();
872 delete cocoa;
873 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000874
David Reiss1ac05802007-07-30 22:00:27 +0000875 if (dump_docs) {
876 dump_docstrings(program);
877 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000878 } catch (string s) {
879 printf("Error: %s\n", s.c_str());
880 } catch (const char* exc) {
881 printf("Error: %s\n", exc);
882 }
883
884}
885
886/**
Mark Sleef5377b32006-10-10 01:42:59 +0000887 * Parse it up.. then spit it back out, in pretty much every language. Alright
888 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000889 */
890int main(int argc, char** argv) {
891 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000892
Mark Sleeb15a68b2006-06-07 06:46:24 +0000893 // Setup time string
894 time_t now = time(NULL);
895 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000896
Mark Sleef0712dc2006-10-25 19:03:57 +0000897 // Check for necessary arguments, you gotta have at least a filename and
898 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000899 if (argc < 2) {
900 usage();
901 }
Mark Slee31985722006-05-24 21:45:31 +0000902
Mark Sleef5377b32006-10-10 01:42:59 +0000903 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000904 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000905 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000906
Mark Sleefdbee812006-09-27 18:50:48 +0000907 arg = strtok(argv[i], " ");
908 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000909 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000910 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000911 ++arg;
912 }
913
914 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 Reissd779cbe2007-08-31 01:42:55 +0000924 } else if (strcmp(arg, "-dense") == 0) {
925 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000926 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000927 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000928 } else if (strcmp(arg, "-javabean") == 0) {
929 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000930 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000931 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000932 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000933 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000934 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000935 gen_phpi = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000936 } else if (strcmp(arg, "-rest") == 0) {
937 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000938 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000939 gen_py = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000940 } else if (strcmp(arg, "-rb") == 0) {
941 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000942 } else if (strcmp(arg, "-xsd") == 0) {
943 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000944 } else if (strcmp(arg, "-perl") == 0) {
945 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000946 } else if (strcmp(arg, "-erl") == 0) {
947 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000948 } else if (strcmp(arg, "-ocaml") == 0) {
949 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000950 } else if (strcmp(arg, "-hs") == 0) {
951 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000952 } else if (strcmp(arg, "-cocoa") == 0) {
953 gen_cocoa = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000954 } else if (strcmp(arg, "-I") == 0) {
955 // An argument of "-I\ asdf" is invalid and has unknown results
956 arg = argv[++i];
957
958 if (arg == NULL) {
959 fprintf(stderr, "!!! Missing Include directory");
960 usage();
961 }
962 g_incl_searchpath.push_back(arg);
Mark Sleefdbee812006-09-27 18:50:48 +0000963 } else {
964 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
965 usage();
966 }
967
968 // Tokenize more
969 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000970 }
971 }
Mark Slee2c44d202007-05-16 02:18:07 +0000972
Mark Sleef0712dc2006-10-25 19:03:57 +0000973 // You gotta generate something!
Christopher Pirob97b89d2007-09-18 00:07:42 +0000974 if (!gen_cpp && !gen_java && !gen_javabean && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_erl && !gen_ocaml && !gen_hs && !gen_cocoa) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000975 fprintf(stderr, "!!! No output language(s) specified\n\n");
976 usage();
977 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000978
979 // Real-pathify it
980 char rp[PATH_MAX];
981 if (realpath(argv[i], rp) == NULL) {
982 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000983 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000984 string input_file(rp);
985
Mark Sleef5377b32006-10-10 01:42:59 +0000986 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000987 t_program* program = new t_program(input_file);
988
989 // Initialize global types
990 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
991 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +0000992 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
993 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +0000994 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
995 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000996 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
997 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
998 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
999 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1000 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1001 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001002
Mark Sleef5377b32006-10-10 01:42:59 +00001003 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001004 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001005
Mark Sleef0712dc2006-10-25 19:03:57 +00001006 // Generate it!
1007 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001008
Mark Sleef0712dc2006-10-25 19:03:57 +00001009 // Clean up. Who am I kidding... this program probably orphans heap memory
1010 // all over the place, but who cares because it is about to exit and it is
1011 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001012
Mark Sleef0712dc2006-10-25 19:03:57 +00001013 delete program;
1014 delete g_type_void;
1015 delete g_type_string;
1016 delete g_type_bool;
1017 delete g_type_byte;
1018 delete g_type_i16;
1019 delete g_type_i32;
1020 delete g_type_i64;
1021 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001022
1023 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001024 return 0;
1025}