blob: e0ff21fbeb6097e94762d089cfa31c197a13be5f [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"
iproctor9a41a0c2007-07-16 21:59:24 +000038#include "generate/t_ocaml_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000039#include "generate/t_erl_generator.h"
iproctorff8eb922007-07-25 19:06:13 +000040#include "generate/t_hs_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000041
42using namespace std;
43
Mark Sleef5377b32006-10-10 01:42:59 +000044/**
45 * Global program tree
46 */
Mark Slee31985722006-05-24 21:45:31 +000047t_program* g_program;
48
Mark Sleef5377b32006-10-10 01:42:59 +000049/**
Mark Sleef0712dc2006-10-25 19:03:57 +000050 * Global types
51 */
52
53t_type* g_type_void;
54t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000055t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000056t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000057t_type* g_type_bool;
58t_type* g_type_byte;
59t_type* g_type_i16;
60t_type* g_type_i32;
61t_type* g_type_i64;
62t_type* g_type_double;
63
64/**
65 * Global scope
66 */
67t_scope* g_scope;
68
69/**
70 * Parent scope to also parse types
71 */
72t_scope* g_parent_scope;
73
74/**
75 * Prefix for putting types in parent scope
76 */
77string g_parent_prefix;
78
79/**
80 * Parsing pass
81 */
82PARSE_MODE g_parse_mode;
83
84/**
85 * Current directory of file being parsed
86 */
87string g_curdir;
88
89/**
90 * Current file being parsed
91 */
92string g_curpath;
93
94/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000095 * Search path for inclusions
96 */
Mark Slee2329a832006-11-09 00:23:30 +000097vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000098
99/**
Mark Sleef5377b32006-10-10 01:42:59 +0000100 * Global debug state
101 */
Mark Slee31985722006-05-24 21:45:31 +0000102int g_debug = 0;
103
Mark Sleef5377b32006-10-10 01:42:59 +0000104/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000105 * Warning level
106 */
107int g_warn = 1;
108
109/**
110 * Verbose output
111 */
112int g_verbose = 0;
113
114/**
Mark Sleef5377b32006-10-10 01:42:59 +0000115 * Global time string
116 */
Mark Slee31985722006-05-24 21:45:31 +0000117char* g_time_str;
118
Mark Slee31985722006-05-24 21:45:31 +0000119/**
David Reisscbd4bac2007-08-14 17:12:33 +0000120 * The last parsed doctext comment.
121 */
122char* g_doctext;
123
124/**
125 * The location of the last parsed doctext comment.
126 */
127int g_doctext_lineno;
128
129/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000130 * Flags to control code generation
131 */
132bool gen_cpp = false;
133bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000134bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000135bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000136bool gen_py = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000137bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000138bool gen_php = false;
139bool gen_phpi = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000140bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000141bool gen_perl = false;
iproctor9a41a0c2007-07-16 21:59:24 +0000142bool gen_ocaml = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000143bool gen_erl = false;
iproctorff8eb922007-07-25 19:06:13 +0000144bool gen_hs = false;
David Reissd779cbe2007-08-31 01:42:55 +0000145bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000146bool gen_recurse = false;
147
148/**
Mark Slee31985722006-05-24 21:45:31 +0000149 * Report an error to the user. This is called yyerror for historical
150 * reasons (lex and yacc expect the error reporting routine to be called
151 * this). Call this function to report any errors to the user.
152 * yyerror takes printf style arguments.
153 *
154 * @param fmt C format string followed by additional arguments
155 */
156void yyerror(char* fmt, ...) {
157 va_list args;
158 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000159 "[ERROR:%s:%d] (last token was '%s')\n",
160 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000161 yylineno,
162 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000163
164 va_start(args, fmt);
165 vfprintf(stderr, fmt, args);
166 va_end(args);
167
168 fprintf(stderr, "\n");
169}
170
171/**
172 * Prints a debug message from the parser.
173 *
174 * @param fmt C format string followed by additional arguments
175 */
176void pdebug(char* fmt, ...) {
177 if (g_debug == 0) {
178 return;
179 }
180 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000181 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000182 va_start(args, fmt);
183 vprintf(fmt, args);
184 va_end(args);
185 printf("\n");
186}
187
188/**
189 * Prints a verbose output mode message
190 *
191 * @param fmt C format string followed by additional arguments
192 */
193void pverbose(char* fmt, ...) {
194 if (g_verbose == 0) {
195 return;
196 }
197 va_list args;
198 va_start(args, fmt);
199 vprintf(fmt, args);
200 va_end(args);
201}
202
203/**
204 * Prints a warning message
205 *
206 * @param fmt C format string followed by additional arguments
207 */
208void pwarning(int level, char* fmt, ...) {
209 if (g_warn < level) {
210 return;
211 }
212 va_list args;
213 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000214 va_start(args, fmt);
215 vprintf(fmt, args);
216 va_end(args);
217 printf("\n");
218}
219
220/**
221 * Prints a failure message and exits
222 *
223 * @param fmt C format string followed by additional arguments
224 */
Mark Slee30152872006-11-28 01:24:07 +0000225void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000226 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000227 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000228 va_start(args, fmt);
229 vfprintf(stderr, fmt, args);
230 va_end(args);
231 printf("\n");
232 exit(1);
233}
234
235/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000236 * Converts a string filename into a thrift program name
237 */
238string program_name(string filename) {
239 string::size_type slash = filename.rfind("/");
240 if (slash != string::npos) {
241 filename = filename.substr(slash+1);
242 }
243 string::size_type dot = filename.rfind(".");
244 if (dot != string::npos) {
245 filename = filename.substr(0, dot);
246 }
247 return filename;
248}
249
250/**
251 * Gets the directory path of a filename
252 */
253string directory_name(string filename) {
254 string::size_type slash = filename.rfind("/");
255 // No slash, just use the current directory
256 if (slash == string::npos) {
257 return ".";
258 }
259 return filename.substr(0, slash);
260}
261
262/**
263 * Finds the appropriate file path for the given filename
264 */
265string include_file(string filename) {
266 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000267 if (filename[0] == '/') {
268 // Realpath!
269 char rp[PATH_MAX];
270 if (realpath(filename.c_str(), rp) == NULL) {
271 pwarning(0, "Cannot open include file %s\n", filename.c_str());
272 return std::string();
273 }
Mark Slee2c44d202007-05-16 02:18:07 +0000274
275 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000276 struct stat finfo;
277 if (stat(rp, &finfo) == 0) {
278 return rp;
279 }
280 } else { // relative path, start searching
281 // new search path with current dir global
282 vector<string> sp = g_incl_searchpath;
283 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000284
Martin Kraemer32c66e12006-11-09 00:06:36 +0000285 // iterate through paths
286 vector<string>::iterator it;
287 for (it = sp.begin(); it != sp.end(); it++) {
288 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000289
Martin Kraemer32c66e12006-11-09 00:06:36 +0000290 // Realpath!
291 char rp[PATH_MAX];
292 if (realpath(sfilename.c_str(), rp) == NULL) {
293 continue;
294 }
Mark Slee2c44d202007-05-16 02:18:07 +0000295
Martin Kraemer32c66e12006-11-09 00:06:36 +0000296 // Stat this files
297 struct stat finfo;
298 if (stat(rp, &finfo) == 0) {
299 return rp;
300 }
301 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000302 }
Mark Slee2c44d202007-05-16 02:18:07 +0000303
Mark Sleef0712dc2006-10-25 19:03:57 +0000304 // Uh oh
305 pwarning(0, "Could not find include file %s\n", filename.c_str());
306 return std::string();
307}
308
309/**
David Reisscbd4bac2007-08-14 17:12:33 +0000310 * Clears any previously stored doctext string.
311 * Also prints a warning if we are discarding information.
312 */
313void clear_doctext() {
314 if (g_doctext != NULL) {
315 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
316 }
317 free(g_doctext);
318 g_doctext = NULL;
319}
320
321/**
David Reiss1ac05802007-07-30 22:00:27 +0000322 * Cleans up text commonly found in doxygen-like comments
323 *
324 * Warning: if you mix tabs and spaces in a non-uniform way,
325 * you will get what you deserve.
326 */
327char* clean_up_doctext(char* doctext) {
328 // Convert to C++ string, and remove Windows's carriage returns.
329 string docstring = doctext;
330 docstring.erase(
331 remove(docstring.begin(), docstring.end(), '\r'),
332 docstring.end());
333
334 // Separate into lines.
335 vector<string> lines;
336 string::size_type pos = string::npos;
337 string::size_type last;
338 while (true) {
339 last = (pos == string::npos) ? 0 : pos+1;
340 pos = docstring.find('\n', last);
341 if (pos == string::npos) {
342 // First bit of cleaning. If the last line is only whitespace, drop it.
343 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
344 if (nonwhite != string::npos) {
345 lines.push_back(docstring.substr(last));
346 }
347 break;
348 }
349 lines.push_back(docstring.substr(last, pos-last));
350 }
351
352 // A very profound docstring.
353 if (lines.empty()) {
354 return NULL;
355 }
356
357 // Clear leading whitespace from the first line.
358 pos = lines.front().find_first_not_of(" \t");
359 lines.front().erase(0, pos);
360
361 // If every nonblank line after the first has the same number of spaces/tabs,
362 // then a star, remove them.
363 bool have_prefix = true;
364 bool found_prefix = false;
365 string::size_type prefix_len = 0;
366 vector<string>::iterator l_iter;
367 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
368 if (l_iter->empty()) {
369 continue;
370 }
371
372 pos = l_iter->find_first_not_of(" \t");
373 if (!found_prefix) {
374 if (pos != string::npos) {
375 if (l_iter->at(pos) == '*') {
376 found_prefix = true;
377 prefix_len = pos;
378 } else {
379 have_prefix = false;
380 break;
381 }
382 } else {
383 // Whitespace-only line. Truncate it.
384 l_iter->clear();
385 }
386 } else if (l_iter->size() > pos
387 && l_iter->at(pos) == '*'
388 && pos == prefix_len) {
389 // Business as usual.
390 } else if (pos == string::npos) {
391 // Whitespace-only line. Let's truncate it for them.
392 l_iter->clear();
393 } else {
394 // The pattern has been broken.
395 have_prefix = false;
396 break;
397 }
398 }
399
400 // If our prefix survived, delete it from every line.
401 if (have_prefix) {
402 // Get the star too.
403 prefix_len++;
404 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
405 l_iter->erase(0, prefix_len);
406 }
407 }
408
409 // Now delete the minimum amount of leading whitespace from each line.
410 prefix_len = string::npos;
411 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
412 if (l_iter->empty()) {
413 continue;
414 }
415 pos = l_iter->find_first_not_of(" \t");
416 if (pos != string::npos
417 && (prefix_len == string::npos || pos < prefix_len)) {
418 prefix_len = pos;
419 }
420 }
421
422 // If our prefix survived, delete it from every line.
423 if (prefix_len != string::npos) {
424 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
425 l_iter->erase(0, prefix_len);
426 }
427 }
428
429 // Remove trailing whitespace from every line.
430 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
431 pos = l_iter->find_last_not_of(" \t");
432 if (pos != string::npos && pos != l_iter->length()-1) {
433 l_iter->erase(pos+1);
434 }
435 }
436
437 // If the first line is empty, remove it.
438 // Don't do this earlier because a lot of steps skip the first line.
439 if (lines.front().empty()) {
440 lines.erase(lines.begin());
441 }
442
443 // Now rejoin the lines and copy them back into doctext.
444 docstring.clear();
445 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
446 docstring += *l_iter;
447 docstring += '\n';
448 }
449
450 assert(docstring.length() <= strlen(doctext));
451 strcpy(doctext, docstring.c_str());
452 return doctext;
453}
454
455/** Set to true to debug docstring parsing */
456static bool dump_docs = false;
457
458/**
459 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000460 * Only works for top-level definitions and the whole program doc
461 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000462 */
463void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000464 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000465 if (!progdoc.empty()) {
466 printf("Whole program doc:\n%s\n", progdoc.c_str());
467 }
David Reiss1ac05802007-07-30 22:00:27 +0000468 const vector<t_typedef*>& typedefs = program->get_typedefs();
469 vector<t_typedef*>::const_iterator t_iter;
470 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
471 t_typedef* td = *t_iter;
472 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000473 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
474 }
475 }
476 const vector<t_enum*>& enums = program->get_enums();
477 vector<t_enum*>::const_iterator e_iter;
478 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
479 t_enum* en = *e_iter;
480 if (en->has_doc()) {
481 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
482 }
483 }
484 const vector<t_const*>& consts = program->get_consts();
485 vector<t_const*>::const_iterator c_iter;
486 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
487 t_const* co = *c_iter;
488 if (co->has_doc()) {
489 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
490 }
491 }
492 const vector<t_struct*>& structs = program->get_structs();
493 vector<t_struct*>::const_iterator s_iter;
494 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
495 t_struct* st = *s_iter;
496 if (st->has_doc()) {
497 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
498 }
499 }
500 const vector<t_struct*>& xceptions = program->get_xceptions();
501 vector<t_struct*>::const_iterator x_iter;
502 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
503 t_struct* xn = *x_iter;
504 if (xn->has_doc()) {
505 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
506 }
507 }
508 const vector<t_service*>& services = program->get_services();
509 vector<t_service*>::const_iterator v_iter;
510 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
511 t_service* sv = *v_iter;
512 if (sv->has_doc()) {
513 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000514 }
515 }
516}
517
518/**
David Reiss18bf22d2007-08-28 20:49:17 +0000519 * Call generate_fingerprint for every structure.
520 */
521void generate_all_fingerprints(t_program* program) {
522 const vector<t_struct*>& structs = program->get_structs();
523 vector<t_struct*>::const_iterator s_iter;
524 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
525 t_struct* st = *s_iter;
526 st->generate_fingerprint();
527 }
528
David Reissd779cbe2007-08-31 01:42:55 +0000529 const vector<t_struct*>& xceptions = program->get_xceptions();
530 vector<t_struct*>::const_iterator x_iter;
531 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
532 t_struct* st = *x_iter;
533 st->generate_fingerprint();
534 }
535
David Reiss18bf22d2007-08-28 20:49:17 +0000536 // If you want to generate fingerprints for implicit structures, start here.
537 /*
538 const vector<t_service*>& services = program->get_services();
539 vector<t_service*>::const_iterator v_iter;
540 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
541 t_service* sv = *v_iter;
542 }
543 */
544}
545
546/**
Mark Slee31985722006-05-24 21:45:31 +0000547 * Diplays the usage message and then exits with an error code.
548 */
549void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000550 fprintf(stderr, "Usage: thrift [options] file\n");
551 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000552 fprintf(stderr, " -cpp Generate C++ output files\n");
553 fprintf(stderr, " -java Generate Java output files\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000554 fprintf(stderr, " -javabean Generate Java bean-style output files\n");
Mark Slee2329a832006-11-09 00:23:30 +0000555 fprintf(stderr, " -php Generate PHP output files\n");
556 fprintf(stderr, " -phpi Generate PHP inlined files\n");
557 fprintf(stderr, " -py Generate Python output files\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000558 fprintf(stderr, " -rb Generate Ruby output files\n");
559 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000560 fprintf(stderr, " -perl Generate Perl output files\n");
iproctor9a41a0c2007-07-16 21:59:24 +0000561 fprintf(stderr, " -ocaml Generate OCaml output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000562 fprintf(stderr, " -erl Generate Erlang output files\n");
iproctorff8eb922007-07-25 19:06:13 +0000563 fprintf(stderr, " -hs Generate Haskell output files\n");
David Reissd779cbe2007-08-31 01:42:55 +0000564 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000565 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000566 fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n");
567 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000568 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
569 fprintf(stderr, " -strict Strict compiler warnings on\n");
570 fprintf(stderr, " -v[erbose] Verbose mode\n");
571 fprintf(stderr, " -r[ecurse] Also generate included files\n");
572 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000573 exit(1);
574}
575
576/**
Mark Slee30152872006-11-28 01:24:07 +0000577 * You know, when I started working on Thrift I really thought it wasn't going
578 * to become a programming language because it was just a generator and it
579 * wouldn't need runtime type information and all that jazz. But then we
580 * decided to add constants, and all of a sudden that means runtime type
581 * validation and inference, except the "runtime" is the code generator
582 * runtime. Shit. I've been had.
583 */
584void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
585 if (type->is_void()) {
586 throw "type error: cannot declare a void const: " + name;
587 }
588
589 if (type->is_base_type()) {
590 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
591 switch (tbase) {
592 case t_base_type::TYPE_STRING:
593 if (value->get_type() != t_const_value::CV_STRING) {
594 throw "type error: const \"" + name + "\" was declared as string";
595 }
596 break;
597 case t_base_type::TYPE_BOOL:
598 if (value->get_type() != t_const_value::CV_INTEGER) {
599 throw "type error: const \"" + name + "\" was declared as bool";
600 }
601 break;
602 case t_base_type::TYPE_BYTE:
603 if (value->get_type() != t_const_value::CV_INTEGER) {
604 throw "type error: const \"" + name + "\" was declared as byte";
605 }
606 break;
607 case t_base_type::TYPE_I16:
608 if (value->get_type() != t_const_value::CV_INTEGER) {
609 throw "type error: const \"" + name + "\" was declared as i16";
610 }
611 break;
612 case t_base_type::TYPE_I32:
613 if (value->get_type() != t_const_value::CV_INTEGER) {
614 throw "type error: const \"" + name + "\" was declared as i32";
615 }
616 break;
617 case t_base_type::TYPE_I64:
618 if (value->get_type() != t_const_value::CV_INTEGER) {
619 throw "type error: const \"" + name + "\" was declared as i64";
620 }
621 break;
622 case t_base_type::TYPE_DOUBLE:
623 if (value->get_type() != t_const_value::CV_INTEGER &&
624 value->get_type() != t_const_value::CV_DOUBLE) {
625 throw "type error: const \"" + name + "\" was declared as double";
626 }
627 break;
628 default:
David Reissdd7796f2007-08-28 21:09:06 +0000629 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000630 }
631 } else if (type->is_enum()) {
632 if (value->get_type() != t_const_value::CV_INTEGER) {
633 throw "type error: const \"" + name + "\" was declared as enum";
634 }
635 } else if (type->is_struct() || type->is_xception()) {
636 if (value->get_type() != t_const_value::CV_MAP) {
637 throw "type error: const \"" + name + "\" was declared as struct/xception";
638 }
639 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
640 vector<t_field*>::const_iterator f_iter;
641
642 const map<t_const_value*, t_const_value*>& val = value->get_map();
643 map<t_const_value*, t_const_value*>::const_iterator v_iter;
644 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
645 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
646 throw "type error: " + name + " struct key must be string";
647 }
648 t_type* field_type = NULL;
649 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
650 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
651 field_type = (*f_iter)->get_type();
652 }
653 }
654 if (field_type == NULL) {
655 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
656 }
657
658 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
659 }
660 } else if (type->is_map()) {
661 t_type* k_type = ((t_map*)type)->get_key_type();
662 t_type* v_type = ((t_map*)type)->get_val_type();
663 const map<t_const_value*, t_const_value*>& val = value->get_map();
664 map<t_const_value*, t_const_value*>::const_iterator v_iter;
665 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
666 validate_const_rec(name + "<key>", k_type, v_iter->first);
667 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000668 }
Mark Slee30152872006-11-28 01:24:07 +0000669 } else if (type->is_list() || type->is_set()) {
670 t_type* e_type;
671 if (type->is_list()) {
672 e_type = ((t_list*)type)->get_elem_type();
673 } else {
674 e_type = ((t_set*)type)->get_elem_type();
675 }
676 const vector<t_const_value*>& val = value->get_list();
677 vector<t_const_value*>::const_iterator v_iter;
678 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
679 validate_const_rec(name + "<elem>", e_type, *v_iter);
680 }
681 }
682}
683
684/**
685 * Check the type of the parsed const information against its declared type
686 */
687void validate_const_type(t_const* c) {
688 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
689}
690
691/**
Mark Slee7ff32452007-02-01 05:26:18 +0000692 * Check the type of a default value assigned to a field.
693 */
694void validate_field_value(t_field* field, t_const_value* cv) {
695 validate_const_rec(field->get_name(), field->get_type(), cv);
696}
697
698/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000699 * Parses a program
700 */
Mark Slee2c44d202007-05-16 02:18:07 +0000701void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000702 // Get scope file path
703 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000704
Mark Sleef0712dc2006-10-25 19:03:57 +0000705 // Set current dir global, which is used in the include_file function
706 g_curdir = directory_name(path);
707 g_curpath = path;
708
709 // Open the file
710 yyin = fopen(path.c_str(), "r");
711 if (yyin == 0) {
712 failure("Could not open input file: \"%s\"", path.c_str());
713 }
714
715 // Create new scope and scan for includes
716 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000717 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000718 g_program = program;
719 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000720 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000721 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000722 if (yyparse() != 0) {
723 failure("Parser error during include pass.");
724 }
725 } catch (string x) {
726 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000727 }
728 fclose(yyin);
729
730 // Recursively parse all the include programs
731 vector<t_program*>& includes = program->get_includes();
732 vector<t_program*>::iterator iter;
733 for (iter = includes.begin(); iter != includes.end(); ++iter) {
734 parse(*iter, program);
735 }
736
737 // Parse the program the file
738 g_parse_mode = PROGRAM;
739 g_program = program;
740 g_scope = program->scope();
741 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
742 g_parent_prefix = program->get_name() + ".";
743 g_curpath = path;
744 yyin = fopen(path.c_str(), "r");
745 if (yyin == 0) {
746 failure("Could not open input file: \"%s\"", path.c_str());
747 }
748 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000749 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000750 try {
751 if (yyparse() != 0) {
752 failure("Parser error during types pass.");
753 }
754 } catch (string x) {
755 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000756 }
757 fclose(yyin);
758}
759
760/**
761 * Generate code
762 */
763void generate(t_program* program) {
764 // Oooohh, recursive code generation, hot!!
765 if (gen_recurse) {
766 const vector<t_program*>& includes = program->get_includes();
767 for (size_t i = 0; i < includes.size(); ++i) {
768 generate(includes[i]);
769 }
770 }
771
772 // Generate code!
773 try {
774 pverbose("Program: %s\n", program->get_path().c_str());
775
David Reiss18bf22d2007-08-28 20:49:17 +0000776 // Compute fingerprints.
777 generate_all_fingerprints(program);
778
Mark Sleef0712dc2006-10-25 19:03:57 +0000779 if (gen_cpp) {
780 pverbose("Generating C++\n");
David Reissd779cbe2007-08-31 01:42:55 +0000781 t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense);
Mark Sleef0712dc2006-10-25 19:03:57 +0000782 cpp->generate_program();
783 delete cpp;
784 }
785
786 if (gen_java) {
787 pverbose("Generating Java\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000788 t_java_generator* java = new t_java_generator(program, false);
789 java->generate_program();
790 delete java;
791 }
792
793 if (gen_javabean) {
794 pverbose("Generating Java Beans\n");
795 t_java_generator* java = new t_java_generator(program, true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000796 java->generate_program();
797 delete java;
798 }
799
800 if (gen_php) {
801 pverbose("Generating PHP\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000802 t_php_generator* php = new t_php_generator(program, false, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000803 php->generate_program();
804 delete php;
805 }
806
807 if (gen_phpi) {
808 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000809 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000810 phpi->generate_program();
811 delete phpi;
812 }
813
814 if (gen_py) {
815 pverbose("Generating Python\n");
816 t_py_generator* py = new t_py_generator(program);
817 py->generate_program();
818 delete py;
819 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000820
Mark Slee6d7d5952007-01-27 01:44:22 +0000821 if (gen_rb) {
822 pverbose("Generating Ruby\n");
823 t_rb_generator* rb = new t_rb_generator(program);
824 rb->generate_program();
825 delete rb;
826 }
827
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000828 if (gen_xsd) {
829 pverbose("Generating XSD\n");
830 t_xsd_generator* xsd = new t_xsd_generator(program);
831 xsd->generate_program();
832 delete xsd;
833 }
834
Mark Slee2c44d202007-05-16 02:18:07 +0000835 if (gen_perl) {
836 pverbose("Generating PERL\n");
837 t_perl_generator* perl = new t_perl_generator(program);
838 perl->generate_program();
839 delete perl;
840 }
841
iproctor9a41a0c2007-07-16 21:59:24 +0000842 if (gen_ocaml) {
843 pverbose("Generating OCaml\n");
844 t_ocaml_generator* ocaml = new t_ocaml_generator(program);
845 ocaml->generate_program();
846 delete ocaml;
847 }
848
Christopher Piro2f5afce2007-06-29 07:17:33 +0000849 if (gen_erl) {
850 pverbose("Generating Erlang\n");
851 t_erl_generator* erl = new t_erl_generator(program);
852 erl->generate_program();
853 delete erl;
854 }
iproctorff8eb922007-07-25 19:06:13 +0000855 if (gen_hs) {
856 pverbose("Generating Haskell\n");
857 t_hs_generator* hs = new t_hs_generator(program);
858 hs->generate_program();
859 delete hs;
860 }
David Reiss1ac05802007-07-30 22:00:27 +0000861 if (dump_docs) {
862 dump_docstrings(program);
863 }
iproctor9a41a0c2007-07-16 21:59:24 +0000864
Mark Sleef0712dc2006-10-25 19:03:57 +0000865 } catch (string s) {
866 printf("Error: %s\n", s.c_str());
867 } catch (const char* exc) {
868 printf("Error: %s\n", exc);
869 }
870
871}
872
873/**
Mark Sleef5377b32006-10-10 01:42:59 +0000874 * Parse it up.. then spit it back out, in pretty much every language. Alright
875 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000876 */
877int main(int argc, char** argv) {
878 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000879
Mark Sleeb15a68b2006-06-07 06:46:24 +0000880 // Setup time string
881 time_t now = time(NULL);
882 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000883
Mark Sleef0712dc2006-10-25 19:03:57 +0000884 // Check for necessary arguments, you gotta have at least a filename and
885 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000886 if (argc < 2) {
887 usage();
888 }
Mark Slee31985722006-05-24 21:45:31 +0000889
Mark Sleef5377b32006-10-10 01:42:59 +0000890 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000891 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000892 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000893
Mark Sleefdbee812006-09-27 18:50:48 +0000894 arg = strtok(argv[i], " ");
895 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000896 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000897 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000898 ++arg;
899 }
900
901 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000902 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000903 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000904 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000905 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000906 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000907 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000908 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000909 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000910 gen_recurse = true;
David Reissd779cbe2007-08-31 01:42:55 +0000911 } else if (strcmp(arg, "-dense") == 0) {
912 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000913 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000914 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000915 } else if (strcmp(arg, "-javabean") == 0) {
916 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000917 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000918 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000919 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000920 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000921 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000922 gen_phpi = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000923 } else if (strcmp(arg, "-rest") == 0) {
924 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000925 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000926 gen_py = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000927 } else if (strcmp(arg, "-rb") == 0) {
928 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000929 } else if (strcmp(arg, "-xsd") == 0) {
930 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000931 } else if (strcmp(arg, "-perl") == 0) {
932 gen_perl = true;
iproctor9a41a0c2007-07-16 21:59:24 +0000933 } else if (strcmp(arg, "-ocaml") == 0) {
934 gen_ocaml = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000935 } else if (strcmp(arg, "-erl") == 0) {
936 gen_erl = true;
iproctorff8eb922007-07-25 19:06:13 +0000937 } else if (strcmp(arg, "-hs") == 0) {
938 gen_hs = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000939 } else if (strcmp(arg, "-I") == 0) {
940 // An argument of "-I\ asdf" is invalid and has unknown results
941 arg = argv[++i];
942
943 if (arg == NULL) {
944 fprintf(stderr, "!!! Missing Include directory");
945 usage();
946 }
947 g_incl_searchpath.push_back(arg);
Mark Sleefdbee812006-09-27 18:50:48 +0000948 } else {
949 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
950 usage();
951 }
952
953 // Tokenize more
954 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000955 }
956 }
Mark Slee2c44d202007-05-16 02:18:07 +0000957
Mark Sleef0712dc2006-10-25 19:03:57 +0000958 // You gotta generate something!
Mark Slee01a9f882007-08-31 00:55:28 +0000959 if (!gen_cpp && !gen_java && !gen_javabean && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_ocaml && !gen_erl && !gen_hs) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000960 fprintf(stderr, "!!! No output language(s) specified\n\n");
961 usage();
962 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000963
964 // Real-pathify it
965 char rp[PATH_MAX];
966 if (realpath(argv[i], rp) == NULL) {
967 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000968 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000969 string input_file(rp);
970
Mark Sleef5377b32006-10-10 01:42:59 +0000971 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000972 t_program* program = new t_program(input_file);
973
974 // Initialize global types
975 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
976 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +0000977 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
978 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +0000979 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
980 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000981 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
982 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
983 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
984 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
985 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
986 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +0000987
Mark Sleef5377b32006-10-10 01:42:59 +0000988 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +0000989 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +0000990
Mark Sleef0712dc2006-10-25 19:03:57 +0000991 // Generate it!
992 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +0000993
Mark Sleef0712dc2006-10-25 19:03:57 +0000994 // Clean up. Who am I kidding... this program probably orphans heap memory
995 // all over the place, but who cares because it is about to exit and it is
996 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +0000997
Mark Sleef0712dc2006-10-25 19:03:57 +0000998 delete program;
999 delete g_type_void;
1000 delete g_type_string;
1001 delete g_type_bool;
1002 delete g_type_byte;
1003 delete g_type_i16;
1004 delete g_type_i32;
1005 delete g_type_i64;
1006 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001007
1008 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001009 return 0;
1010}