blob: 0b904ea4032721566d90da350466ac6b87280f40 [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>
dweatherford65b70752007-10-31 02:18:14 +000024#include <errno.h>
Mark Slee31985722006-05-24 21:45:31 +000025
Mark Sleef0712dc2006-10-25 19:03:57 +000026// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000027#include "globals.h"
28
29#include "main.h"
30#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000031#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000032#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000033#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000034#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000035#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000036#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000037#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000038#include "generate/t_perl_generator.h"
Christopher Piro2f5afce2007-06-29 07:17:33 +000039#include "generate/t_erl_generator.h"
Christopher Pirob97b89d2007-09-18 00:07:42 +000040#include "generate/t_ocaml_generator.h"
iproctorff8eb922007-07-25 19:06:13 +000041#include "generate/t_hs_generator.h"
Mark Slee7e9eea42007-09-10 21:00:23 +000042#include "generate/t_cocoa_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000043
44using namespace std;
45
Mark Sleef5377b32006-10-10 01:42:59 +000046/**
47 * Global program tree
48 */
Mark Slee31985722006-05-24 21:45:31 +000049t_program* g_program;
50
Mark Sleef5377b32006-10-10 01:42:59 +000051/**
Mark Sleef0712dc2006-10-25 19:03:57 +000052 * Global types
53 */
54
55t_type* g_type_void;
56t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000057t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000058t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000059t_type* g_type_bool;
60t_type* g_type_byte;
61t_type* g_type_i16;
62t_type* g_type_i32;
63t_type* g_type_i64;
64t_type* g_type_double;
65
66/**
67 * Global scope
68 */
69t_scope* g_scope;
70
71/**
72 * Parent scope to also parse types
73 */
74t_scope* g_parent_scope;
75
76/**
77 * Prefix for putting types in parent scope
78 */
79string g_parent_prefix;
80
81/**
82 * Parsing pass
83 */
84PARSE_MODE g_parse_mode;
85
86/**
87 * Current directory of file being parsed
88 */
89string g_curdir;
90
91/**
92 * Current file being parsed
93 */
94string g_curpath;
95
96/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000097 * Search path for inclusions
98 */
Mark Slee2329a832006-11-09 00:23:30 +000099vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000100
101/**
Mark Sleef5377b32006-10-10 01:42:59 +0000102 * Global debug state
103 */
Mark Slee31985722006-05-24 21:45:31 +0000104int g_debug = 0;
105
Mark Sleef5377b32006-10-10 01:42:59 +0000106/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000107 * Warning level
108 */
109int g_warn = 1;
110
111/**
112 * Verbose output
113 */
114int g_verbose = 0;
115
116/**
Mark Sleef5377b32006-10-10 01:42:59 +0000117 * Global time string
118 */
Mark Slee31985722006-05-24 21:45:31 +0000119char* g_time_str;
120
Mark Slee31985722006-05-24 21:45:31 +0000121/**
David Reisscbd4bac2007-08-14 17:12:33 +0000122 * The last parsed doctext comment.
123 */
124char* g_doctext;
125
126/**
127 * The location of the last parsed doctext comment.
128 */
129int g_doctext_lineno;
130
131/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000132 * Flags to control code generation
133 */
134bool gen_cpp = false;
135bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000136bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000137bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000138bool gen_py = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000139bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000140bool gen_php = false;
141bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000142bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000143bool gen_phpa = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000144bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000145bool gen_perl = false;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000146bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000147bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000148bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000149bool gen_cocoa = false;
David Reissd779cbe2007-08-31 01:42:55 +0000150bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000151bool gen_recurse = false;
152
153/**
Mark Slee31985722006-05-24 21:45:31 +0000154 * Report an error to the user. This is called yyerror for historical
155 * reasons (lex and yacc expect the error reporting routine to be called
156 * this). Call this function to report any errors to the user.
157 * yyerror takes printf style arguments.
158 *
159 * @param fmt C format string followed by additional arguments
160 */
161void yyerror(char* fmt, ...) {
162 va_list args;
163 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000164 "[ERROR:%s:%d] (last token was '%s')\n",
165 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000166 yylineno,
167 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000168
169 va_start(args, fmt);
170 vfprintf(stderr, fmt, args);
171 va_end(args);
172
173 fprintf(stderr, "\n");
174}
175
176/**
177 * Prints a debug message from the parser.
178 *
179 * @param fmt C format string followed by additional arguments
180 */
181void pdebug(char* fmt, ...) {
182 if (g_debug == 0) {
183 return;
184 }
185 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000186 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000187 va_start(args, fmt);
188 vprintf(fmt, args);
189 va_end(args);
190 printf("\n");
191}
192
193/**
194 * Prints a verbose output mode message
195 *
196 * @param fmt C format string followed by additional arguments
197 */
198void pverbose(char* fmt, ...) {
199 if (g_verbose == 0) {
200 return;
201 }
202 va_list args;
203 va_start(args, fmt);
204 vprintf(fmt, args);
205 va_end(args);
206}
207
208/**
209 * Prints a warning message
210 *
211 * @param fmt C format string followed by additional arguments
212 */
213void pwarning(int level, char* fmt, ...) {
214 if (g_warn < level) {
215 return;
216 }
217 va_list args;
218 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000219 va_start(args, fmt);
220 vprintf(fmt, args);
221 va_end(args);
222 printf("\n");
223}
224
225/**
226 * Prints a failure message and exits
227 *
228 * @param fmt C format string followed by additional arguments
229 */
Mark Slee30152872006-11-28 01:24:07 +0000230void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000231 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000233 va_start(args, fmt);
234 vfprintf(stderr, fmt, args);
235 va_end(args);
236 printf("\n");
237 exit(1);
238}
239
240/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000241 * Converts a string filename into a thrift program name
242 */
243string program_name(string filename) {
244 string::size_type slash = filename.rfind("/");
245 if (slash != string::npos) {
246 filename = filename.substr(slash+1);
247 }
248 string::size_type dot = filename.rfind(".");
249 if (dot != string::npos) {
250 filename = filename.substr(0, dot);
251 }
252 return filename;
253}
254
255/**
256 * Gets the directory path of a filename
257 */
258string directory_name(string filename) {
259 string::size_type slash = filename.rfind("/");
260 // No slash, just use the current directory
261 if (slash == string::npos) {
262 return ".";
263 }
264 return filename.substr(0, slash);
265}
266
267/**
268 * Finds the appropriate file path for the given filename
269 */
270string include_file(string filename) {
271 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000272 if (filename[0] == '/') {
273 // Realpath!
274 char rp[PATH_MAX];
275 if (realpath(filename.c_str(), rp) == NULL) {
276 pwarning(0, "Cannot open include file %s\n", filename.c_str());
277 return std::string();
278 }
Mark Slee2c44d202007-05-16 02:18:07 +0000279
280 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000281 struct stat finfo;
282 if (stat(rp, &finfo) == 0) {
283 return rp;
284 }
285 } else { // relative path, start searching
286 // new search path with current dir global
287 vector<string> sp = g_incl_searchpath;
288 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000289
Martin Kraemer32c66e12006-11-09 00:06:36 +0000290 // iterate through paths
291 vector<string>::iterator it;
292 for (it = sp.begin(); it != sp.end(); it++) {
293 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000294
Martin Kraemer32c66e12006-11-09 00:06:36 +0000295 // Realpath!
296 char rp[PATH_MAX];
297 if (realpath(sfilename.c_str(), rp) == NULL) {
298 continue;
299 }
Mark Slee2c44d202007-05-16 02:18:07 +0000300
Martin Kraemer32c66e12006-11-09 00:06:36 +0000301 // Stat this files
302 struct stat finfo;
303 if (stat(rp, &finfo) == 0) {
304 return rp;
305 }
306 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000307 }
Mark Slee2c44d202007-05-16 02:18:07 +0000308
Mark Sleef0712dc2006-10-25 19:03:57 +0000309 // Uh oh
310 pwarning(0, "Could not find include file %s\n", filename.c_str());
311 return std::string();
312}
313
314/**
David Reisscbd4bac2007-08-14 17:12:33 +0000315 * Clears any previously stored doctext string.
316 * Also prints a warning if we are discarding information.
317 */
318void clear_doctext() {
319 if (g_doctext != NULL) {
320 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
321 }
322 free(g_doctext);
323 g_doctext = NULL;
324}
325
326/**
David Reiss1ac05802007-07-30 22:00:27 +0000327 * Cleans up text commonly found in doxygen-like comments
328 *
329 * Warning: if you mix tabs and spaces in a non-uniform way,
330 * you will get what you deserve.
331 */
332char* clean_up_doctext(char* doctext) {
333 // Convert to C++ string, and remove Windows's carriage returns.
334 string docstring = doctext;
335 docstring.erase(
336 remove(docstring.begin(), docstring.end(), '\r'),
337 docstring.end());
338
339 // Separate into lines.
340 vector<string> lines;
341 string::size_type pos = string::npos;
342 string::size_type last;
343 while (true) {
344 last = (pos == string::npos) ? 0 : pos+1;
345 pos = docstring.find('\n', last);
346 if (pos == string::npos) {
347 // First bit of cleaning. If the last line is only whitespace, drop it.
348 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
349 if (nonwhite != string::npos) {
350 lines.push_back(docstring.substr(last));
351 }
352 break;
353 }
354 lines.push_back(docstring.substr(last, pos-last));
355 }
356
357 // A very profound docstring.
358 if (lines.empty()) {
359 return NULL;
360 }
361
362 // Clear leading whitespace from the first line.
363 pos = lines.front().find_first_not_of(" \t");
364 lines.front().erase(0, pos);
365
366 // If every nonblank line after the first has the same number of spaces/tabs,
367 // then a star, remove them.
368 bool have_prefix = true;
369 bool found_prefix = false;
370 string::size_type prefix_len = 0;
371 vector<string>::iterator l_iter;
372 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
373 if (l_iter->empty()) {
374 continue;
375 }
376
377 pos = l_iter->find_first_not_of(" \t");
378 if (!found_prefix) {
379 if (pos != string::npos) {
380 if (l_iter->at(pos) == '*') {
381 found_prefix = true;
382 prefix_len = pos;
383 } else {
384 have_prefix = false;
385 break;
386 }
387 } else {
388 // Whitespace-only line. Truncate it.
389 l_iter->clear();
390 }
391 } else if (l_iter->size() > pos
392 && l_iter->at(pos) == '*'
393 && pos == prefix_len) {
394 // Business as usual.
395 } else if (pos == string::npos) {
396 // Whitespace-only line. Let's truncate it for them.
397 l_iter->clear();
398 } else {
399 // The pattern has been broken.
400 have_prefix = false;
401 break;
402 }
403 }
404
405 // If our prefix survived, delete it from every line.
406 if (have_prefix) {
407 // Get the star too.
408 prefix_len++;
409 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
410 l_iter->erase(0, prefix_len);
411 }
412 }
413
414 // Now delete the minimum amount of leading whitespace from each line.
415 prefix_len = string::npos;
416 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
417 if (l_iter->empty()) {
418 continue;
419 }
420 pos = l_iter->find_first_not_of(" \t");
421 if (pos != string::npos
422 && (prefix_len == string::npos || pos < prefix_len)) {
423 prefix_len = pos;
424 }
425 }
426
427 // If our prefix survived, delete it from every line.
428 if (prefix_len != string::npos) {
429 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
430 l_iter->erase(0, prefix_len);
431 }
432 }
433
434 // Remove trailing whitespace from every line.
435 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
436 pos = l_iter->find_last_not_of(" \t");
437 if (pos != string::npos && pos != l_iter->length()-1) {
438 l_iter->erase(pos+1);
439 }
440 }
441
442 // If the first line is empty, remove it.
443 // Don't do this earlier because a lot of steps skip the first line.
444 if (lines.front().empty()) {
445 lines.erase(lines.begin());
446 }
447
448 // Now rejoin the lines and copy them back into doctext.
449 docstring.clear();
450 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
451 docstring += *l_iter;
452 docstring += '\n';
453 }
454
455 assert(docstring.length() <= strlen(doctext));
456 strcpy(doctext, docstring.c_str());
457 return doctext;
458}
459
460/** Set to true to debug docstring parsing */
461static bool dump_docs = false;
462
463/**
464 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000465 * Only works for top-level definitions and the whole program doc
466 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000467 */
468void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000469 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000470 if (!progdoc.empty()) {
471 printf("Whole program doc:\n%s\n", progdoc.c_str());
472 }
David Reiss1ac05802007-07-30 22:00:27 +0000473 const vector<t_typedef*>& typedefs = program->get_typedefs();
474 vector<t_typedef*>::const_iterator t_iter;
475 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
476 t_typedef* td = *t_iter;
477 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000478 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
479 }
480 }
481 const vector<t_enum*>& enums = program->get_enums();
482 vector<t_enum*>::const_iterator e_iter;
483 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
484 t_enum* en = *e_iter;
485 if (en->has_doc()) {
486 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
487 }
488 }
489 const vector<t_const*>& consts = program->get_consts();
490 vector<t_const*>::const_iterator c_iter;
491 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
492 t_const* co = *c_iter;
493 if (co->has_doc()) {
494 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
495 }
496 }
497 const vector<t_struct*>& structs = program->get_structs();
498 vector<t_struct*>::const_iterator s_iter;
499 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
500 t_struct* st = *s_iter;
501 if (st->has_doc()) {
502 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
503 }
504 }
505 const vector<t_struct*>& xceptions = program->get_xceptions();
506 vector<t_struct*>::const_iterator x_iter;
507 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
508 t_struct* xn = *x_iter;
509 if (xn->has_doc()) {
510 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
511 }
512 }
513 const vector<t_service*>& services = program->get_services();
514 vector<t_service*>::const_iterator v_iter;
515 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
516 t_service* sv = *v_iter;
517 if (sv->has_doc()) {
518 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000519 }
520 }
521}
522
523/**
David Reiss18bf22d2007-08-28 20:49:17 +0000524 * Call generate_fingerprint for every structure.
525 */
526void generate_all_fingerprints(t_program* program) {
527 const vector<t_struct*>& structs = program->get_structs();
528 vector<t_struct*>::const_iterator s_iter;
529 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
530 t_struct* st = *s_iter;
531 st->generate_fingerprint();
532 }
533
David Reissd779cbe2007-08-31 01:42:55 +0000534 const vector<t_struct*>& xceptions = program->get_xceptions();
535 vector<t_struct*>::const_iterator x_iter;
536 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
537 t_struct* st = *x_iter;
538 st->generate_fingerprint();
539 }
540
David Reiss47557bc2007-09-04 21:31:04 +0000541 g_type_void->generate_fingerprint();
542
David Reiss18bf22d2007-08-28 20:49:17 +0000543 // If you want to generate fingerprints for implicit structures, start here.
544 /*
545 const vector<t_service*>& services = program->get_services();
546 vector<t_service*>::const_iterator v_iter;
547 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
548 t_service* sv = *v_iter;
549 }
550 */
551}
552
553/**
Mark Slee31985722006-05-24 21:45:31 +0000554 * Diplays the usage message and then exits with an error code.
555 */
556void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000557 fprintf(stderr, "Usage: thrift [options] file\n");
558 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000559 fprintf(stderr, " -cpp Generate C++ output files\n");
560 fprintf(stderr, " -java Generate Java output files\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000561 fprintf(stderr, " -javabean Generate Java bean-style output files\n");
Mark Slee2329a832006-11-09 00:23:30 +0000562 fprintf(stderr, " -php Generate PHP output files\n");
563 fprintf(stderr, " -phpi Generate PHP inlined files\n");
Mark Slee5b743072007-11-13 04:00:29 +0000564 fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
565 fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000566 fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000567 fprintf(stderr, " -py Generate Python output files\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000568 fprintf(stderr, " -rb Generate Ruby output files\n");
569 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000570 fprintf(stderr, " -perl Generate Perl output files\n");
Christopher Piro2f5afce2007-06-29 07:17:33 +0000571 fprintf(stderr, " -erl Generate Erlang output files\n");
Christopher Pirob97b89d2007-09-18 00:07:42 +0000572 fprintf(stderr, " -ocaml Generate OCaml output files\n");
iproctorff8eb922007-07-25 19:06:13 +0000573 fprintf(stderr, " -hs Generate Haskell output files\n");
Mark Slee7e9eea42007-09-10 21:00:23 +0000574 fprintf(stderr, " -cocoa Generate Cocoa/Objective-C output files\n");
dweatherford65b70752007-10-31 02:18:14 +0000575 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
576 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000577 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000578 fprintf(stderr, " searched for include directives\n");
David Reissd779cbe2007-08-31 01:42:55 +0000579 fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n");
580 fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
Mark Slee2329a832006-11-09 00:23:30 +0000581 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
582 fprintf(stderr, " -strict Strict compiler warnings on\n");
583 fprintf(stderr, " -v[erbose] Verbose mode\n");
584 fprintf(stderr, " -r[ecurse] Also generate included files\n");
585 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000586 exit(1);
587}
588
589/**
Mark Slee30152872006-11-28 01:24:07 +0000590 * You know, when I started working on Thrift I really thought it wasn't going
591 * to become a programming language because it was just a generator and it
592 * wouldn't need runtime type information and all that jazz. But then we
593 * decided to add constants, and all of a sudden that means runtime type
594 * validation and inference, except the "runtime" is the code generator
595 * runtime. Shit. I've been had.
596 */
597void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
598 if (type->is_void()) {
599 throw "type error: cannot declare a void const: " + name;
600 }
601
602 if (type->is_base_type()) {
603 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
604 switch (tbase) {
605 case t_base_type::TYPE_STRING:
606 if (value->get_type() != t_const_value::CV_STRING) {
607 throw "type error: const \"" + name + "\" was declared as string";
608 }
609 break;
610 case t_base_type::TYPE_BOOL:
611 if (value->get_type() != t_const_value::CV_INTEGER) {
612 throw "type error: const \"" + name + "\" was declared as bool";
613 }
614 break;
615 case t_base_type::TYPE_BYTE:
616 if (value->get_type() != t_const_value::CV_INTEGER) {
617 throw "type error: const \"" + name + "\" was declared as byte";
618 }
619 break;
620 case t_base_type::TYPE_I16:
621 if (value->get_type() != t_const_value::CV_INTEGER) {
622 throw "type error: const \"" + name + "\" was declared as i16";
623 }
624 break;
625 case t_base_type::TYPE_I32:
626 if (value->get_type() != t_const_value::CV_INTEGER) {
627 throw "type error: const \"" + name + "\" was declared as i32";
628 }
629 break;
630 case t_base_type::TYPE_I64:
631 if (value->get_type() != t_const_value::CV_INTEGER) {
632 throw "type error: const \"" + name + "\" was declared as i64";
633 }
634 break;
635 case t_base_type::TYPE_DOUBLE:
636 if (value->get_type() != t_const_value::CV_INTEGER &&
637 value->get_type() != t_const_value::CV_DOUBLE) {
638 throw "type error: const \"" + name + "\" was declared as double";
639 }
640 break;
641 default:
David Reissdd7796f2007-08-28 21:09:06 +0000642 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000643 }
644 } else if (type->is_enum()) {
645 if (value->get_type() != t_const_value::CV_INTEGER) {
646 throw "type error: const \"" + name + "\" was declared as enum";
647 }
648 } else if (type->is_struct() || type->is_xception()) {
649 if (value->get_type() != t_const_value::CV_MAP) {
650 throw "type error: const \"" + name + "\" was declared as struct/xception";
651 }
652 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
653 vector<t_field*>::const_iterator f_iter;
654
655 const map<t_const_value*, t_const_value*>& val = value->get_map();
656 map<t_const_value*, t_const_value*>::const_iterator v_iter;
657 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
658 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
659 throw "type error: " + name + " struct key must be string";
660 }
661 t_type* field_type = NULL;
662 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
663 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
664 field_type = (*f_iter)->get_type();
665 }
666 }
667 if (field_type == NULL) {
668 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
669 }
670
671 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
672 }
673 } else if (type->is_map()) {
674 t_type* k_type = ((t_map*)type)->get_key_type();
675 t_type* v_type = ((t_map*)type)->get_val_type();
676 const map<t_const_value*, t_const_value*>& val = value->get_map();
677 map<t_const_value*, t_const_value*>::const_iterator v_iter;
678 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
679 validate_const_rec(name + "<key>", k_type, v_iter->first);
680 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000681 }
Mark Slee30152872006-11-28 01:24:07 +0000682 } else if (type->is_list() || type->is_set()) {
683 t_type* e_type;
684 if (type->is_list()) {
685 e_type = ((t_list*)type)->get_elem_type();
686 } else {
687 e_type = ((t_set*)type)->get_elem_type();
688 }
689 const vector<t_const_value*>& val = value->get_list();
690 vector<t_const_value*>::const_iterator v_iter;
691 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
692 validate_const_rec(name + "<elem>", e_type, *v_iter);
693 }
694 }
695}
696
697/**
698 * Check the type of the parsed const information against its declared type
699 */
700void validate_const_type(t_const* c) {
701 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
702}
703
704/**
Mark Slee7ff32452007-02-01 05:26:18 +0000705 * Check the type of a default value assigned to a field.
706 */
707void validate_field_value(t_field* field, t_const_value* cv) {
708 validate_const_rec(field->get_name(), field->get_type(), cv);
709}
710
711/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000712 * Parses a program
713 */
Mark Slee2c44d202007-05-16 02:18:07 +0000714void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000715 // Get scope file path
716 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000717
Mark Sleef0712dc2006-10-25 19:03:57 +0000718 // Set current dir global, which is used in the include_file function
719 g_curdir = directory_name(path);
720 g_curpath = path;
721
722 // Open the file
723 yyin = fopen(path.c_str(), "r");
724 if (yyin == 0) {
725 failure("Could not open input file: \"%s\"", path.c_str());
726 }
727
728 // Create new scope and scan for includes
729 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000730 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000731 g_program = program;
732 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000733 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000734 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000735 if (yyparse() != 0) {
736 failure("Parser error during include pass.");
737 }
738 } catch (string x) {
739 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000740 }
741 fclose(yyin);
742
743 // Recursively parse all the include programs
744 vector<t_program*>& includes = program->get_includes();
745 vector<t_program*>::iterator iter;
746 for (iter = includes.begin(); iter != includes.end(); ++iter) {
747 parse(*iter, program);
748 }
749
750 // Parse the program the file
751 g_parse_mode = PROGRAM;
752 g_program = program;
753 g_scope = program->scope();
754 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
755 g_parent_prefix = program->get_name() + ".";
756 g_curpath = path;
757 yyin = fopen(path.c_str(), "r");
758 if (yyin == 0) {
759 failure("Could not open input file: \"%s\"", path.c_str());
760 }
761 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000762 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000763 try {
764 if (yyparse() != 0) {
765 failure("Parser error during types pass.");
766 }
767 } catch (string x) {
768 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000769 }
770 fclose(yyin);
771}
772
773/**
774 * Generate code
775 */
776void generate(t_program* program) {
777 // Oooohh, recursive code generation, hot!!
778 if (gen_recurse) {
779 const vector<t_program*>& includes = program->get_includes();
780 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000781 // Propogate output path from parent to child programs
782 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000783
Mark Sleef0712dc2006-10-25 19:03:57 +0000784 generate(includes[i]);
785 }
786 }
787
788 // Generate code!
789 try {
790 pverbose("Program: %s\n", program->get_path().c_str());
791
David Reiss18bf22d2007-08-28 20:49:17 +0000792 // Compute fingerprints.
793 generate_all_fingerprints(program);
794
Mark Sleef0712dc2006-10-25 19:03:57 +0000795 if (gen_cpp) {
796 pverbose("Generating C++\n");
David Reissd779cbe2007-08-31 01:42:55 +0000797 t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense);
Mark Sleef0712dc2006-10-25 19:03:57 +0000798 cpp->generate_program();
799 delete cpp;
800 }
801
802 if (gen_java) {
803 pverbose("Generating Java\n");
Mark Slee01a9f882007-08-31 00:55:28 +0000804 t_java_generator* java = new t_java_generator(program, false);
805 java->generate_program();
806 delete java;
807 }
808
809 if (gen_javabean) {
810 pverbose("Generating Java Beans\n");
811 t_java_generator* java = new t_java_generator(program, true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000812 java->generate_program();
813 delete java;
814 }
815
816 if (gen_php) {
817 pverbose("Generating PHP\n");
Mark Slee09f69e02007-11-17 00:32:36 +0000818 t_php_generator* php = new t_php_generator(program, false, gen_rest, gen_phps, gen_phpa);
Mark Sleef0712dc2006-10-25 19:03:57 +0000819 php->generate_program();
820 delete php;
821 }
822
823 if (gen_phpi) {
824 pverbose("Generating PHP-inline\n");
Mark Slee756b1d12007-07-06 00:30:21 +0000825 t_php_generator* phpi = new t_php_generator(program, true, gen_rest);
Mark Sleef0712dc2006-10-25 19:03:57 +0000826 phpi->generate_program();
827 delete phpi;
828 }
829
830 if (gen_py) {
831 pverbose("Generating Python\n");
832 t_py_generator* py = new t_py_generator(program);
833 py->generate_program();
834 delete py;
835 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000836
Mark Slee6d7d5952007-01-27 01:44:22 +0000837 if (gen_rb) {
838 pverbose("Generating Ruby\n");
839 t_rb_generator* rb = new t_rb_generator(program);
840 rb->generate_program();
841 delete rb;
842 }
843
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000844 if (gen_xsd) {
845 pverbose("Generating XSD\n");
846 t_xsd_generator* xsd = new t_xsd_generator(program);
847 xsd->generate_program();
848 delete xsd;
849 }
850
Mark Slee2c44d202007-05-16 02:18:07 +0000851 if (gen_perl) {
852 pverbose("Generating PERL\n");
853 t_perl_generator* perl = new t_perl_generator(program);
854 perl->generate_program();
855 delete perl;
856 }
857
Christopher Pirob97b89d2007-09-18 00:07:42 +0000858 if (gen_erl) {
859 pverbose("Generating Erlang\n");
860 t_erl_generator* erl = new t_erl_generator(program);
861 erl->generate_program();
862 delete erl;
863 }
864
iproctor9a41a0c2007-07-16 21:59:24 +0000865 if (gen_ocaml) {
866 pverbose("Generating OCaml\n");
867 t_ocaml_generator* ocaml = new t_ocaml_generator(program);
868 ocaml->generate_program();
869 delete ocaml;
870 }
871
iproctorff8eb922007-07-25 19:06:13 +0000872 if (gen_hs) {
873 pverbose("Generating Haskell\n");
874 t_hs_generator* hs = new t_hs_generator(program);
875 hs->generate_program();
876 delete hs;
877 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000878
Mark Slee7e9eea42007-09-10 21:00:23 +0000879 if (gen_cocoa) {
880 pverbose("Generating Cocoa/Objective-C\n");
881 t_cocoa_generator* cocoa = new t_cocoa_generator(program);
882 cocoa->generate_program();
883 delete cocoa;
884 }
Christopher Pirob97b89d2007-09-18 00:07:42 +0000885
David Reiss1ac05802007-07-30 22:00:27 +0000886 if (dump_docs) {
887 dump_docstrings(program);
888 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000889 } catch (string s) {
890 printf("Error: %s\n", s.c_str());
891 } catch (const char* exc) {
892 printf("Error: %s\n", exc);
893 }
894
895}
896
897/**
Mark Sleef5377b32006-10-10 01:42:59 +0000898 * Parse it up.. then spit it back out, in pretty much every language. Alright
899 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000900 */
901int main(int argc, char** argv) {
902 int i;
dweatherford65b70752007-10-31 02:18:14 +0000903 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000904
Mark Sleeb15a68b2006-06-07 06:46:24 +0000905 // Setup time string
906 time_t now = time(NULL);
907 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000908
Mark Sleef0712dc2006-10-25 19:03:57 +0000909 // Check for necessary arguments, you gotta have at least a filename and
910 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000911 if (argc < 2) {
912 usage();
913 }
Mark Slee31985722006-05-24 21:45:31 +0000914
Mark Sleef5377b32006-10-10 01:42:59 +0000915 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000916 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000917 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000918
Mark Sleefdbee812006-09-27 18:50:48 +0000919 arg = strtok(argv[i], " ");
920 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000921 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000922 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000923 ++arg;
924 }
925
926 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000927 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000928 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000929 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000930 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000931 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000932 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000933 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000934 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000935 gen_recurse = true;
David Reissd779cbe2007-08-31 01:42:55 +0000936 } else if (strcmp(arg, "-dense") == 0) {
937 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000938 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000939 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000940 } else if (strcmp(arg, "-javabean") == 0) {
941 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000942 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000943 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000944 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000945 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000946 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000947 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000948 } else if (strcmp(arg, "-phps") == 0) {
949 if (!gen_php) {
950 gen_php = true;
951 }
952 gen_phps = true;
953 } else if (strcmp(arg, "-phpl") == 0) {
954 if (!gen_php) {
955 gen_php = true;
956 }
957 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000958 } else if (strcmp(arg, "-phpa") == 0) {
959 if (!gen_php) {
960 gen_php = true;
961 gen_phps = false;
962 }
963 gen_phpa = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000964 } else if (strcmp(arg, "-rest") == 0) {
965 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000966 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000967 gen_py = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000968 } else if (strcmp(arg, "-rb") == 0) {
969 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000970 } else if (strcmp(arg, "-xsd") == 0) {
971 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000972 } else if (strcmp(arg, "-perl") == 0) {
973 gen_perl = true;
Christopher Piro2f5afce2007-06-29 07:17:33 +0000974 } else if (strcmp(arg, "-erl") == 0) {
975 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000976 } else if (strcmp(arg, "-ocaml") == 0) {
977 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000978 } else if (strcmp(arg, "-hs") == 0) {
979 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000980 } else if (strcmp(arg, "-cocoa") == 0) {
981 gen_cocoa = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000982 } else if (strcmp(arg, "-I") == 0) {
983 // An argument of "-I\ asdf" is invalid and has unknown results
984 arg = argv[++i];
985
986 if (arg == NULL) {
987 fprintf(stderr, "!!! Missing Include directory");
988 usage();
989 }
990 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +0000991 } else if (strcmp(arg, "-o") == 0) {
992 arg = argv[++i];
993 if (arg == NULL) {
994 fprintf(stderr, "-o: missing output directory");
995 usage();
Mark Slee5b743072007-11-13 04:00:29 +0000996 }
dweatherford65b70752007-10-31 02:18:14 +0000997 out_path = arg;
998 struct stat sb;
999 if (stat(out_path.c_str(), &sb) < 0) {
1000 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1001 return -1;
1002 }
1003 if (! S_ISDIR(sb.st_mode)) {
1004 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1005 return -1;
1006 }
Mark Sleefdbee812006-09-27 18:50:48 +00001007 } else {
1008 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1009 usage();
1010 }
1011
1012 // Tokenize more
1013 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001014 }
1015 }
Mark Slee2c44d202007-05-16 02:18:07 +00001016
Mark Sleef0712dc2006-10-25 19:03:57 +00001017 // You gotta generate something!
Christopher Pirob97b89d2007-09-18 00:07:42 +00001018 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 +00001019 fprintf(stderr, "!!! No output language(s) specified\n\n");
1020 usage();
1021 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001022
1023 // Real-pathify it
1024 char rp[PATH_MAX];
1025 if (realpath(argv[i], rp) == NULL) {
1026 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001027 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001028 string input_file(rp);
1029
Mark Sleef5377b32006-10-10 01:42:59 +00001030 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001031 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001032 if (out_path.size()) {
1033 program->set_out_path(out_path);
1034 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001035
1036 // Initialize global types
1037 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1038 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001039 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1040 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001041 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1042 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001043 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1044 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1045 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1046 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1047 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1048 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001049
Mark Sleef5377b32006-10-10 01:42:59 +00001050 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001051 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001052
Mark Sleef0712dc2006-10-25 19:03:57 +00001053 // Generate it!
1054 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001055
Mark Sleef0712dc2006-10-25 19:03:57 +00001056 // Clean up. Who am I kidding... this program probably orphans heap memory
1057 // all over the place, but who cares because it is about to exit and it is
1058 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001059
Mark Sleef0712dc2006-10-25 19:03:57 +00001060 delete program;
1061 delete g_type_void;
1062 delete g_type_string;
1063 delete g_type_bool;
1064 delete g_type_byte;
1065 delete g_type_i16;
1066 delete g_type_i32;
1067 delete g_type_i64;
1068 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001069
1070 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001071 return 0;
1072}