blob: 8a0bd68f610ca36bffb830288eaf7613f0587d1d [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Slee31985722006-05-24 21:45:31 +000020/**
21 * thrift - a lightweight cross-language rpc/serialization tool
22 *
23 * This file contains the main compiler engine for Thrift, which invokes the
24 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +000025 * code for each language lives in a file by the language name under the
26 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +000027 *
Mark Slee31985722006-05-24 21:45:31 +000028 */
29
David Reissf10984b2008-03-27 21:39:52 +000030#include <cassert>
Mark Slee31985722006-05-24 21:45:31 +000031#include <stdlib.h>
32#include <stdio.h>
33#include <stdarg.h>
34#include <string>
David Reiss739cbe22008-04-15 05:44:00 +000035#include <algorithm>
Mark Sleef0712dc2006-10-25 19:03:57 +000036#include <sys/types.h>
37#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000038#include <errno.h>
David Reissab55ed52008-06-11 01:17:00 +000039#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000040
David Reiss204420f2008-01-11 20:59:03 +000041#ifdef MINGW
42# include <windows.h> /* for GetFullPathName */
David Reiss204420f2008-01-11 20:59:03 +000043#endif
44
Mark Sleef0712dc2006-10-25 19:03:57 +000045// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000046#include "globals.h"
47
48#include "main.h"
49#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000050#include "parse/t_scope.h"
David Reissbbbbe882009-02-17 20:27:48 +000051#include "generate/t_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000052
David Reissdd08f6d2008-06-30 20:24:24 +000053#include "version.h"
54
Mark Slee31985722006-05-24 21:45:31 +000055using namespace std;
56
Mark Sleef5377b32006-10-10 01:42:59 +000057/**
58 * Global program tree
59 */
Mark Slee31985722006-05-24 21:45:31 +000060t_program* g_program;
61
Mark Sleef5377b32006-10-10 01:42:59 +000062/**
Mark Sleef0712dc2006-10-25 19:03:57 +000063 * Global types
64 */
65
66t_type* g_type_void;
67t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000068t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000069t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000070t_type* g_type_bool;
71t_type* g_type_byte;
72t_type* g_type_i16;
73t_type* g_type_i32;
74t_type* g_type_i64;
75t_type* g_type_double;
76
77/**
78 * Global scope
79 */
80t_scope* g_scope;
81
82/**
83 * Parent scope to also parse types
84 */
85t_scope* g_parent_scope;
86
87/**
88 * Prefix for putting types in parent scope
89 */
90string g_parent_prefix;
91
92/**
93 * Parsing pass
94 */
95PARSE_MODE g_parse_mode;
96
97/**
98 * Current directory of file being parsed
99 */
100string g_curdir;
101
102/**
103 * Current file being parsed
104 */
105string g_curpath;
106
107/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000108 * Search path for inclusions
109 */
Mark Slee2329a832006-11-09 00:23:30 +0000110vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000111
112/**
kholst76f2c882008-01-16 02:47:41 +0000113 * Should C++ include statements use path prefixes for other thrift-generated
114 * header files
115 */
116bool g_cpp_use_include_prefix = false;
117
118/**
Mark Sleef5377b32006-10-10 01:42:59 +0000119 * Global debug state
120 */
Mark Slee31985722006-05-24 21:45:31 +0000121int g_debug = 0;
122
Mark Sleef5377b32006-10-10 01:42:59 +0000123/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000124 * Warning level
125 */
126int g_warn = 1;
127
128/**
129 * Verbose output
130 */
131int g_verbose = 0;
132
133/**
Mark Sleef5377b32006-10-10 01:42:59 +0000134 * Global time string
135 */
Mark Slee31985722006-05-24 21:45:31 +0000136char* g_time_str;
137
Mark Slee31985722006-05-24 21:45:31 +0000138/**
David Reisscbd4bac2007-08-14 17:12:33 +0000139 * The last parsed doctext comment.
140 */
141char* g_doctext;
142
143/**
144 * The location of the last parsed doctext comment.
145 */
146int g_doctext_lineno;
147
148/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000149 * Flags to control code generation
150 */
151bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000152bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000153bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000154bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000155bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000156bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000157bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000158bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000159bool gen_php = false;
160bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000161bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000162bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000163bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000164bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000165bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000166bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000167bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000168bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000169bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000170bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000171bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000172bool gen_recurse = false;
173
174/**
David Reiss204420f2008-01-11 20:59:03 +0000175 * MinGW doesn't have realpath, so use fallback implementation in that case,
176 * otherwise this just calls through to realpath
177 */
178char *saferealpath(const char *path, char *resolved_path) {
179#ifdef MINGW
180 char buf[MAX_PATH];
181 char* basename;
182 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
183 if (len == 0 || len > MAX_PATH - 1){
184 strcpy(resolved_path, path);
185 } else {
186 CharLowerBuff(buf, len);
187 strcpy(resolved_path, buf);
188 }
189 return resolved_path;
190#else
191 return realpath(path, resolved_path);
192#endif
193}
194
195
196/**
Mark Slee31985722006-05-24 21:45:31 +0000197 * Report an error to the user. This is called yyerror for historical
198 * reasons (lex and yacc expect the error reporting routine to be called
199 * this). Call this function to report any errors to the user.
200 * yyerror takes printf style arguments.
201 *
202 * @param fmt C format string followed by additional arguments
203 */
David Reiss0babe402008-06-10 22:56:12 +0000204void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000205 va_list args;
206 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000207 "[ERROR:%s:%d] (last token was '%s')\n",
208 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000209 yylineno,
210 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000211
212 va_start(args, fmt);
213 vfprintf(stderr, fmt, args);
214 va_end(args);
215
216 fprintf(stderr, "\n");
217}
218
219/**
220 * Prints a debug message from the parser.
221 *
222 * @param fmt C format string followed by additional arguments
223 */
David Reiss0babe402008-06-10 22:56:12 +0000224void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000225 if (g_debug == 0) {
226 return;
227 }
228 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000229 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000230 va_start(args, fmt);
231 vprintf(fmt, args);
232 va_end(args);
233 printf("\n");
234}
235
236/**
237 * Prints a verbose output mode message
238 *
239 * @param fmt C format string followed by additional arguments
240 */
David Reiss0babe402008-06-10 22:56:12 +0000241void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000242 if (g_verbose == 0) {
243 return;
244 }
245 va_list args;
246 va_start(args, fmt);
247 vprintf(fmt, args);
248 va_end(args);
249}
250
251/**
252 * Prints a warning message
253 *
254 * @param fmt C format string followed by additional arguments
255 */
David Reiss0babe402008-06-10 22:56:12 +0000256void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000257 if (g_warn < level) {
258 return;
259 }
260 va_list args;
261 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000262 va_start(args, fmt);
263 vprintf(fmt, args);
264 va_end(args);
265 printf("\n");
266}
267
268/**
269 * Prints a failure message and exits
270 *
271 * @param fmt C format string followed by additional arguments
272 */
Mark Slee30152872006-11-28 01:24:07 +0000273void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000274 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000275 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000276 va_start(args, fmt);
277 vfprintf(stderr, fmt, args);
278 va_end(args);
279 printf("\n");
280 exit(1);
281}
282
283/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000284 * Converts a string filename into a thrift program name
285 */
286string program_name(string filename) {
287 string::size_type slash = filename.rfind("/");
288 if (slash != string::npos) {
289 filename = filename.substr(slash+1);
290 }
291 string::size_type dot = filename.rfind(".");
292 if (dot != string::npos) {
293 filename = filename.substr(0, dot);
294 }
295 return filename;
296}
297
298/**
299 * Gets the directory path of a filename
300 */
301string directory_name(string filename) {
302 string::size_type slash = filename.rfind("/");
303 // No slash, just use the current directory
304 if (slash == string::npos) {
305 return ".";
306 }
307 return filename.substr(0, slash);
308}
309
310/**
311 * Finds the appropriate file path for the given filename
312 */
313string include_file(string filename) {
314 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000315 if (filename[0] == '/') {
316 // Realpath!
317 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000318 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000319 pwarning(0, "Cannot open include file %s\n", filename.c_str());
320 return std::string();
321 }
Mark Slee2c44d202007-05-16 02:18:07 +0000322
323 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000324 struct stat finfo;
325 if (stat(rp, &finfo) == 0) {
326 return rp;
327 }
328 } else { // relative path, start searching
329 // new search path with current dir global
330 vector<string> sp = g_incl_searchpath;
331 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000332
Martin Kraemer32c66e12006-11-09 00:06:36 +0000333 // iterate through paths
334 vector<string>::iterator it;
335 for (it = sp.begin(); it != sp.end(); it++) {
336 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000337
Martin Kraemer32c66e12006-11-09 00:06:36 +0000338 // Realpath!
339 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000340 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000341 continue;
342 }
Mark Slee2c44d202007-05-16 02:18:07 +0000343
Martin Kraemer32c66e12006-11-09 00:06:36 +0000344 // Stat this files
345 struct stat finfo;
346 if (stat(rp, &finfo) == 0) {
347 return rp;
348 }
349 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000350 }
Mark Slee2c44d202007-05-16 02:18:07 +0000351
Mark Sleef0712dc2006-10-25 19:03:57 +0000352 // Uh oh
353 pwarning(0, "Could not find include file %s\n", filename.c_str());
354 return std::string();
355}
356
357/**
David Reisscbd4bac2007-08-14 17:12:33 +0000358 * Clears any previously stored doctext string.
359 * Also prints a warning if we are discarding information.
360 */
361void clear_doctext() {
362 if (g_doctext != NULL) {
363 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
364 }
365 free(g_doctext);
366 g_doctext = NULL;
367}
368
369/**
David Reiss1ac05802007-07-30 22:00:27 +0000370 * Cleans up text commonly found in doxygen-like comments
371 *
372 * Warning: if you mix tabs and spaces in a non-uniform way,
373 * you will get what you deserve.
374 */
375char* clean_up_doctext(char* doctext) {
376 // Convert to C++ string, and remove Windows's carriage returns.
377 string docstring = doctext;
378 docstring.erase(
379 remove(docstring.begin(), docstring.end(), '\r'),
380 docstring.end());
381
382 // Separate into lines.
383 vector<string> lines;
384 string::size_type pos = string::npos;
385 string::size_type last;
386 while (true) {
387 last = (pos == string::npos) ? 0 : pos+1;
388 pos = docstring.find('\n', last);
389 if (pos == string::npos) {
390 // First bit of cleaning. If the last line is only whitespace, drop it.
391 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
392 if (nonwhite != string::npos) {
393 lines.push_back(docstring.substr(last));
394 }
395 break;
396 }
397 lines.push_back(docstring.substr(last, pos-last));
398 }
399
400 // A very profound docstring.
401 if (lines.empty()) {
402 return NULL;
403 }
404
405 // Clear leading whitespace from the first line.
406 pos = lines.front().find_first_not_of(" \t");
407 lines.front().erase(0, pos);
408
409 // If every nonblank line after the first has the same number of spaces/tabs,
410 // then a star, remove them.
411 bool have_prefix = true;
412 bool found_prefix = false;
413 string::size_type prefix_len = 0;
414 vector<string>::iterator l_iter;
415 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
416 if (l_iter->empty()) {
417 continue;
418 }
419
420 pos = l_iter->find_first_not_of(" \t");
421 if (!found_prefix) {
422 if (pos != string::npos) {
423 if (l_iter->at(pos) == '*') {
424 found_prefix = true;
425 prefix_len = pos;
426 } else {
427 have_prefix = false;
428 break;
429 }
430 } else {
431 // Whitespace-only line. Truncate it.
432 l_iter->clear();
433 }
434 } else if (l_iter->size() > pos
435 && l_iter->at(pos) == '*'
436 && pos == prefix_len) {
437 // Business as usual.
438 } else if (pos == string::npos) {
439 // Whitespace-only line. Let's truncate it for them.
440 l_iter->clear();
441 } else {
442 // The pattern has been broken.
443 have_prefix = false;
444 break;
445 }
446 }
447
448 // If our prefix survived, delete it from every line.
449 if (have_prefix) {
450 // Get the star too.
451 prefix_len++;
452 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
453 l_iter->erase(0, prefix_len);
454 }
455 }
456
457 // Now delete the minimum amount of leading whitespace from each line.
458 prefix_len = string::npos;
459 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
460 if (l_iter->empty()) {
461 continue;
462 }
463 pos = l_iter->find_first_not_of(" \t");
464 if (pos != string::npos
465 && (prefix_len == string::npos || pos < prefix_len)) {
466 prefix_len = pos;
467 }
468 }
469
470 // If our prefix survived, delete it from every line.
471 if (prefix_len != string::npos) {
472 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
473 l_iter->erase(0, prefix_len);
474 }
475 }
476
477 // Remove trailing whitespace from every line.
478 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
479 pos = l_iter->find_last_not_of(" \t");
480 if (pos != string::npos && pos != l_iter->length()-1) {
481 l_iter->erase(pos+1);
482 }
483 }
484
485 // If the first line is empty, remove it.
486 // Don't do this earlier because a lot of steps skip the first line.
487 if (lines.front().empty()) {
488 lines.erase(lines.begin());
489 }
490
491 // Now rejoin the lines and copy them back into doctext.
492 docstring.clear();
493 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
494 docstring += *l_iter;
495 docstring += '\n';
496 }
497
498 assert(docstring.length() <= strlen(doctext));
499 strcpy(doctext, docstring.c_str());
500 return doctext;
501}
502
503/** Set to true to debug docstring parsing */
504static bool dump_docs = false;
505
506/**
507 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000508 * Only works for top-level definitions and the whole program doc
509 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000510 */
511void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000512 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000513 if (!progdoc.empty()) {
514 printf("Whole program doc:\n%s\n", progdoc.c_str());
515 }
David Reiss1ac05802007-07-30 22:00:27 +0000516 const vector<t_typedef*>& typedefs = program->get_typedefs();
517 vector<t_typedef*>::const_iterator t_iter;
518 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
519 t_typedef* td = *t_iter;
520 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000521 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
522 }
523 }
524 const vector<t_enum*>& enums = program->get_enums();
525 vector<t_enum*>::const_iterator e_iter;
526 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
527 t_enum* en = *e_iter;
528 if (en->has_doc()) {
529 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
530 }
531 }
532 const vector<t_const*>& consts = program->get_consts();
533 vector<t_const*>::const_iterator c_iter;
534 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
535 t_const* co = *c_iter;
536 if (co->has_doc()) {
537 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
538 }
539 }
540 const vector<t_struct*>& structs = program->get_structs();
541 vector<t_struct*>::const_iterator s_iter;
542 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
543 t_struct* st = *s_iter;
544 if (st->has_doc()) {
545 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
546 }
547 }
548 const vector<t_struct*>& xceptions = program->get_xceptions();
549 vector<t_struct*>::const_iterator x_iter;
550 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
551 t_struct* xn = *x_iter;
552 if (xn->has_doc()) {
553 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
554 }
555 }
556 const vector<t_service*>& services = program->get_services();
557 vector<t_service*>::const_iterator v_iter;
558 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
559 t_service* sv = *v_iter;
560 if (sv->has_doc()) {
561 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000562 }
563 }
564}
565
566/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000567 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000568 */
569void generate_all_fingerprints(t_program* program) {
570 const vector<t_struct*>& structs = program->get_structs();
571 vector<t_struct*>::const_iterator s_iter;
572 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
573 t_struct* st = *s_iter;
574 st->generate_fingerprint();
575 }
576
David Reissd779cbe2007-08-31 01:42:55 +0000577 const vector<t_struct*>& xceptions = program->get_xceptions();
578 vector<t_struct*>::const_iterator x_iter;
579 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
580 t_struct* st = *x_iter;
581 st->generate_fingerprint();
582 }
583
David Reiss3c5d2fd2008-02-08 21:58:06 +0000584 const vector<t_enum*>& enums = program->get_enums();
585 vector<t_enum*>::const_iterator e_iter;
586 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
587 t_enum* e = *e_iter;
588 e->generate_fingerprint();
589 }
590
David Reiss47557bc2007-09-04 21:31:04 +0000591 g_type_void->generate_fingerprint();
592
David Reiss18bf22d2007-08-28 20:49:17 +0000593 // If you want to generate fingerprints for implicit structures, start here.
594 /*
595 const vector<t_service*>& services = program->get_services();
596 vector<t_service*>::const_iterator v_iter;
597 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
598 t_service* sv = *v_iter;
599 }
600 */
601}
602
603/**
David Reissdd08f6d2008-06-30 20:24:24 +0000604 * Prints the version number
605 */
606void version() {
607 printf("Thrift version %s-%s\n", THRIFT_VERSION, THRIFT_REVISION);
608}
609
610/**
Mark Slee31985722006-05-24 21:45:31 +0000611 * Diplays the usage message and then exits with an error code.
612 */
613void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000614 fprintf(stderr, "Usage: thrift [options] file\n");
615 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000616 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000617 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
618 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000619 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000620 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000621 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
622 fprintf(stderr, " -strict Strict compiler warnings on\n");
623 fprintf(stderr, " -v[erbose] Verbose mode\n");
624 fprintf(stderr, " -r[ecurse] Also generate included files\n");
625 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000626 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
627 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
628 fprintf(stderr, " Keys and values are options passed to the generator.\n");
629 fprintf(stderr, " Many options will not require values.\n");
630 fprintf(stderr, "\n");
631 fprintf(stderr, "Available generators (and options):\n");
632
633 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
634 t_generator_registry::gen_map_t::iterator iter;
635 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
636 fprintf(stderr, " %s (%s):\n",
637 iter->second->get_short_name().c_str(),
638 iter->second->get_long_name().c_str());
639 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
640 }
Mark Slee31985722006-05-24 21:45:31 +0000641 exit(1);
642}
643
644/**
Mark Slee30152872006-11-28 01:24:07 +0000645 * You know, when I started working on Thrift I really thought it wasn't going
646 * to become a programming language because it was just a generator and it
647 * wouldn't need runtime type information and all that jazz. But then we
648 * decided to add constants, and all of a sudden that means runtime type
649 * validation and inference, except the "runtime" is the code generator
650 * runtime. Shit. I've been had.
651 */
652void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
653 if (type->is_void()) {
654 throw "type error: cannot declare a void const: " + name;
655 }
656
657 if (type->is_base_type()) {
658 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
659 switch (tbase) {
660 case t_base_type::TYPE_STRING:
661 if (value->get_type() != t_const_value::CV_STRING) {
662 throw "type error: const \"" + name + "\" was declared as string";
663 }
664 break;
665 case t_base_type::TYPE_BOOL:
666 if (value->get_type() != t_const_value::CV_INTEGER) {
667 throw "type error: const \"" + name + "\" was declared as bool";
668 }
669 break;
670 case t_base_type::TYPE_BYTE:
671 if (value->get_type() != t_const_value::CV_INTEGER) {
672 throw "type error: const \"" + name + "\" was declared as byte";
673 }
674 break;
675 case t_base_type::TYPE_I16:
676 if (value->get_type() != t_const_value::CV_INTEGER) {
677 throw "type error: const \"" + name + "\" was declared as i16";
678 }
679 break;
680 case t_base_type::TYPE_I32:
681 if (value->get_type() != t_const_value::CV_INTEGER) {
682 throw "type error: const \"" + name + "\" was declared as i32";
683 }
684 break;
685 case t_base_type::TYPE_I64:
686 if (value->get_type() != t_const_value::CV_INTEGER) {
687 throw "type error: const \"" + name + "\" was declared as i64";
688 }
689 break;
690 case t_base_type::TYPE_DOUBLE:
691 if (value->get_type() != t_const_value::CV_INTEGER &&
692 value->get_type() != t_const_value::CV_DOUBLE) {
693 throw "type error: const \"" + name + "\" was declared as double";
694 }
695 break;
696 default:
David Reissdd7796f2007-08-28 21:09:06 +0000697 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000698 }
699 } else if (type->is_enum()) {
700 if (value->get_type() != t_const_value::CV_INTEGER) {
701 throw "type error: const \"" + name + "\" was declared as enum";
702 }
703 } else if (type->is_struct() || type->is_xception()) {
704 if (value->get_type() != t_const_value::CV_MAP) {
705 throw "type error: const \"" + name + "\" was declared as struct/xception";
706 }
707 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
708 vector<t_field*>::const_iterator f_iter;
709
710 const map<t_const_value*, t_const_value*>& val = value->get_map();
711 map<t_const_value*, t_const_value*>::const_iterator v_iter;
712 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
713 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
714 throw "type error: " + name + " struct key must be string";
715 }
716 t_type* field_type = NULL;
717 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
718 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
719 field_type = (*f_iter)->get_type();
720 }
721 }
722 if (field_type == NULL) {
723 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
724 }
725
726 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
727 }
728 } else if (type->is_map()) {
729 t_type* k_type = ((t_map*)type)->get_key_type();
730 t_type* v_type = ((t_map*)type)->get_val_type();
731 const map<t_const_value*, t_const_value*>& val = value->get_map();
732 map<t_const_value*, t_const_value*>::const_iterator v_iter;
733 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
734 validate_const_rec(name + "<key>", k_type, v_iter->first);
735 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000736 }
Mark Slee30152872006-11-28 01:24:07 +0000737 } else if (type->is_list() || type->is_set()) {
738 t_type* e_type;
739 if (type->is_list()) {
740 e_type = ((t_list*)type)->get_elem_type();
741 } else {
742 e_type = ((t_set*)type)->get_elem_type();
743 }
744 const vector<t_const_value*>& val = value->get_list();
745 vector<t_const_value*>::const_iterator v_iter;
746 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
747 validate_const_rec(name + "<elem>", e_type, *v_iter);
748 }
749 }
750}
751
752/**
753 * Check the type of the parsed const information against its declared type
754 */
755void validate_const_type(t_const* c) {
756 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
757}
758
759/**
Mark Slee7ff32452007-02-01 05:26:18 +0000760 * Check the type of a default value assigned to a field.
761 */
762void validate_field_value(t_field* field, t_const_value* cv) {
763 validate_const_rec(field->get_name(), field->get_type(), cv);
764}
765
766/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000767 * Check that all the elements of a throws block are actually exceptions.
768 */
769bool validate_throws(t_struct* throws) {
770 const vector<t_field*>& members = throws->get_members();
771 vector<t_field*>::const_iterator m_iter;
772 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
773 if (!(*m_iter)->get_type()->is_xception()) {
774 return false;
775 }
776 }
777 return true;
778}
779
780/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000781 * Parses a program
782 */
Mark Slee2c44d202007-05-16 02:18:07 +0000783void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000784 // Get scope file path
785 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000786
Mark Sleef0712dc2006-10-25 19:03:57 +0000787 // Set current dir global, which is used in the include_file function
788 g_curdir = directory_name(path);
789 g_curpath = path;
790
791 // Open the file
792 yyin = fopen(path.c_str(), "r");
793 if (yyin == 0) {
794 failure("Could not open input file: \"%s\"", path.c_str());
795 }
796
797 // Create new scope and scan for includes
798 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000799 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000800 g_program = program;
801 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000802 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000803 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000804 if (yyparse() != 0) {
805 failure("Parser error during include pass.");
806 }
807 } catch (string x) {
808 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000809 }
810 fclose(yyin);
811
812 // Recursively parse all the include programs
813 vector<t_program*>& includes = program->get_includes();
814 vector<t_program*>::iterator iter;
815 for (iter = includes.begin(); iter != includes.end(); ++iter) {
816 parse(*iter, program);
817 }
818
David Reiss204420f2008-01-11 20:59:03 +0000819 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000820 g_parse_mode = PROGRAM;
821 g_program = program;
822 g_scope = program->scope();
823 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
824 g_parent_prefix = program->get_name() + ".";
825 g_curpath = path;
826 yyin = fopen(path.c_str(), "r");
827 if (yyin == 0) {
828 failure("Could not open input file: \"%s\"", path.c_str());
829 }
830 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000831 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000832 try {
833 if (yyparse() != 0) {
834 failure("Parser error during types pass.");
835 }
836 } catch (string x) {
837 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000838 }
839 fclose(yyin);
840}
841
842/**
843 * Generate code
844 */
David Reissbd0db882008-02-27 01:54:51 +0000845void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000846 // Oooohh, recursive code generation, hot!!
847 if (gen_recurse) {
848 const vector<t_program*>& includes = program->get_includes();
849 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000850 // Propogate output path from parent to child programs
851 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000852
David Reissbd0db882008-02-27 01:54:51 +0000853 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000854 }
855 }
856
857 // Generate code!
858 try {
859 pverbose("Program: %s\n", program->get_path().c_str());
860
David Reiss18bf22d2007-08-28 20:49:17 +0000861 // Compute fingerprints.
862 generate_all_fingerprints(program);
863
David Reiss1ac05802007-07-30 22:00:27 +0000864 if (dump_docs) {
865 dump_docstrings(program);
866 }
David Reissbd0db882008-02-27 01:54:51 +0000867
868 vector<string>::const_iterator iter;
869 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
870 t_generator* generator = t_generator_registry::get_generator(program, *iter);
871
872 if (generator == NULL) {
873 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
874 } else {
875 pverbose("Generating \"%s\"\n", iter->c_str());
876 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000877 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000878 }
879 }
880
Mark Sleef0712dc2006-10-25 19:03:57 +0000881 } catch (string s) {
882 printf("Error: %s\n", s.c_str());
883 } catch (const char* exc) {
884 printf("Error: %s\n", exc);
885 }
886
887}
888
889/**
Mark Sleef5377b32006-10-10 01:42:59 +0000890 * Parse it up.. then spit it back out, in pretty much every language. Alright
891 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000892 */
893int main(int argc, char** argv) {
894 int i;
dweatherford65b70752007-10-31 02:18:14 +0000895 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000896
Mark Sleeb15a68b2006-06-07 06:46:24 +0000897 // Setup time string
898 time_t now = time(NULL);
899 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000900
Mark Sleef0712dc2006-10-25 19:03:57 +0000901 // Check for necessary arguments, you gotta have at least a filename and
902 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000903 if (argc < 2) {
904 usage();
905 }
Mark Slee31985722006-05-24 21:45:31 +0000906
David Reissbd0db882008-02-27 01:54:51 +0000907 vector<string> generator_strings;
908
David Reiss9cc2c132008-02-27 01:54:47 +0000909 // Set the current path to a dummy value to make warning messages clearer.
910 g_curpath = "arguments";
911
Mark Sleef5377b32006-10-10 01:42:59 +0000912 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000913 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000914 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000915
Mark Sleefdbee812006-09-27 18:50:48 +0000916 arg = strtok(argv[i], " ");
917 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000918 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000919 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000920 ++arg;
921 }
922
David Reissdd08f6d2008-06-30 20:24:24 +0000923 if (strcmp(arg, "-version") == 0) {
924 version();
925 exit(1);
926 } else 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 Reissbd0db882008-02-27 01:54:51 +0000936 } else if (strcmp(arg, "-gen") == 0) {
937 arg = argv[++i];
938 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000939 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000940 usage();
941 }
942 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000943 } else if (strcmp(arg, "-dense") == 0) {
944 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000945 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000946 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000947 } else if (strcmp(arg, "-javabean") == 0) {
948 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000949 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000950 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000951 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000952 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000953 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000954 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000955 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000956 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000957 gen_phps = true;
958 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000959 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000960 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000961 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000962 gen_php = true;
963 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000964 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000965 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000966 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000967 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000968 } else if (strcmp(arg, "-rest") == 0) {
969 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000970 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000971 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +0000972 } else if (strcmp(arg, "-pyns") == 0) {
973 gen_py = true;
974 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000975 } else if (strcmp(arg, "-rb") == 0) {
976 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000977 } else if (strcmp(arg, "-xsd") == 0) {
978 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000979 } else if (strcmp(arg, "-perl") == 0) {
980 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +0000981 } else if (strcmp(arg, "-erl") == 0) {
982 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000983 } else if (strcmp(arg, "-ocaml") == 0) {
984 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000985 } else if (strcmp(arg, "-hs") == 0) {
986 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000987 } else if (strcmp(arg, "-cocoa") == 0) {
988 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +0000989 } else if (strcmp(arg, "-st") == 0) {
990 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +0000991 } else if (strcmp(arg, "-csharp") == 0) {
992 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +0000993 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
994 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000995 } else if (strcmp(arg, "-I") == 0) {
996 // An argument of "-I\ asdf" is invalid and has unknown results
997 arg = argv[++i];
998
999 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001000 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001001 usage();
1002 }
1003 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001004 } else if (strcmp(arg, "-o") == 0) {
1005 arg = argv[++i];
1006 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001007 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001008 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001009 }
dweatherford65b70752007-10-31 02:18:14 +00001010 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001011
1012#ifdef MINGW
1013 //strip out trailing \ on Windows
1014 int last = out_path.length()-1;
1015 if (out_path[last] == '\\')
1016 {
1017 out_path.erase(last);
1018 }
1019#endif
1020
dweatherford65b70752007-10-31 02:18:14 +00001021 struct stat sb;
1022 if (stat(out_path.c_str(), &sb) < 0) {
1023 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1024 return -1;
1025 }
1026 if (! S_ISDIR(sb.st_mode)) {
1027 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1028 return -1;
1029 }
Mark Sleefdbee812006-09-27 18:50:48 +00001030 } else {
1031 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1032 usage();
1033 }
1034
1035 // Tokenize more
1036 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001037 }
1038 }
Mark Slee2c44d202007-05-16 02:18:07 +00001039
David Reissdd08f6d2008-06-30 20:24:24 +00001040 // if you're asking for version, you have a right not to pass a file
1041 if (strcmp(argv[argc-1], "-version") == 0) {
1042 version();
1043 exit(1);
1044 }
1045
David Reiss400cd4b2008-02-27 01:54:55 +00001046 // TODO(dreiss): Delete these when everyone is using the new hotness.
1047 if (gen_cpp) {
1048 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1049 string gen_string = "cpp:";
1050 if (gen_dense) {
1051 gen_string.append("dense,");
1052 }
1053 if (g_cpp_use_include_prefix) {
1054 gen_string.append("include_prefix,");
1055 }
1056 generator_strings.push_back(gen_string);
1057 }
David Reisse1404d22008-02-27 01:55:05 +00001058 if (gen_java) {
1059 pwarning(1, "-java is deprecated. Use --gen java");
1060 generator_strings.push_back("java");
1061 }
1062 if (gen_javabean) {
1063 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1064 generator_strings.push_back("java:beans");
1065 }
David Reiss861869b2008-03-27 21:41:23 +00001066 if (gen_csharp) {
1067 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1068 generator_strings.push_back("csharp");
1069 }
David Reiss558e3992008-03-27 21:41:40 +00001070 if (gen_py) {
1071 pwarning(1, "-py is deprecated. Use --gen py");
1072 generator_strings.push_back("py");
1073 }
David Reissa640cea2008-03-27 21:42:01 +00001074 if (gen_rb) {
1075 pwarning(1, "-rb is deprecated. Use --gen rb");
1076 generator_strings.push_back("rb");
1077 }
David Reiss2b386c52008-03-27 21:42:23 +00001078 if (gen_perl) {
1079 pwarning(1, "-perl is deprecated. Use --gen perl");
1080 generator_strings.push_back("perl");
1081 }
David Reissa9ea68b2009-02-17 20:28:24 +00001082 if (gen_php || gen_phpi) {
1083 pwarning(1, "-php is deprecated. Use --gen php");
1084 string gen_string = "php:";
1085 if (gen_phpi) {
1086 gen_string.append("inlined,");
1087 } else if(gen_phps) {
1088 gen_string.append("server,");
1089 } else if(gen_phpa) {
1090 gen_string.append("autoload,");
1091 } else if(gen_phpo) {
1092 gen_string.append("oop,");
1093 } else if(gen_rest) {
1094 gen_string.append("rest,");
1095 }
1096 generator_strings.push_back(gen_string);
1097 }
David Reissd01c64d2008-03-27 21:40:55 +00001098 if (gen_cocoa) {
1099 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1100 generator_strings.push_back("cocoa");
1101 }
David Reissa2047832009-02-17 20:27:54 +00001102 if (gen_erl) {
1103 pwarning(1, "-erl is deprecated. Use --gen erl");
1104 generator_strings.push_back("erl");
1105 }
David Reissa890b572008-03-27 21:40:35 +00001106 if (gen_st) {
1107 pwarning(1, "-st is deprecated. Use --gen st");
1108 generator_strings.push_back("st");
1109 }
David Reissf0521b12008-03-27 21:40:05 +00001110 if (gen_ocaml) {
1111 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1112 generator_strings.push_back("ocaml");
1113 }
David Reissaf3ab262008-03-27 21:40:16 +00001114 if (gen_hs) {
1115 pwarning(1, "-hs is deprecated. Use --gen hs");
1116 generator_strings.push_back("hs");
1117 }
David Reiss4ba67102009-02-17 20:28:06 +00001118 if (gen_xsd) {
1119 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1120 generator_strings.push_back("xsd");
1121 }
David Reisse1404d22008-02-27 01:55:05 +00001122
Mark Sleef0712dc2006-10-25 19:03:57 +00001123 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001124 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001125 fprintf(stderr, "!!! No output language(s) specified\n\n");
1126 usage();
1127 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001128
1129 // Real-pathify it
1130 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001131 if (argv[i] == NULL) {
1132 fprintf(stderr, "!!! Missing file name\n");
1133 usage();
1134 }
David Reiss204420f2008-01-11 20:59:03 +00001135 if (saferealpath(argv[i], rp) == NULL) {
1136 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001137 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001138 string input_file(rp);
1139
Mark Sleef5377b32006-10-10 01:42:59 +00001140 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001141 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001142 if (out_path.size()) {
1143 program->set_out_path(out_path);
1144 }
kholst76f2c882008-01-16 02:47:41 +00001145
David Reiss4b6a3c72008-02-27 22:28:12 +00001146 // Compute the cpp include prefix.
1147 // infer this from the filename passed in
1148 string input_filename = argv[i];
1149 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001150
David Reiss4b6a3c72008-02-27 22:28:12 +00001151 string::size_type last_slash = string::npos;
1152 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1153 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001154 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001155
David Reiss4b6a3c72008-02-27 22:28:12 +00001156 program->set_include_prefix(include_prefix);
1157
Mark Sleef0712dc2006-10-25 19:03:57 +00001158 // Initialize global types
1159 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1160 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001161 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1162 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001163 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1164 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001165 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1166 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1167 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1168 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1169 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1170 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001171
Mark Sleef5377b32006-10-10 01:42:59 +00001172 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001173 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001174
David Reiss9cc2c132008-02-27 01:54:47 +00001175 // The current path is not really relevant when we are doing generation.
1176 // Reset the variable to make warning messages clearer.
1177 g_curpath = "generation";
1178 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1179 // That is what shows up during argument parsing.
1180 yylineno = 1;
1181
Mark Sleef0712dc2006-10-25 19:03:57 +00001182 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001183 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001184
Mark Sleef0712dc2006-10-25 19:03:57 +00001185 // Clean up. Who am I kidding... this program probably orphans heap memory
1186 // all over the place, but who cares because it is about to exit and it is
1187 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001188
Mark Sleef0712dc2006-10-25 19:03:57 +00001189 delete program;
1190 delete g_type_void;
1191 delete g_type_string;
1192 delete g_type_bool;
1193 delete g_type_byte;
1194 delete g_type_i16;
1195 delete g_type_i32;
1196 delete g_type_i64;
1197 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001198
1199 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001200 return 0;
1201}