blob: 7a5d2d4958dd8930f99a6c38cacb80e78ce1ce88 [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/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000124 * Strictness level
125 */
126int g_strict = 127;
127
128/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000129 * Warning level
130 */
131int g_warn = 1;
132
133/**
134 * Verbose output
135 */
136int g_verbose = 0;
137
138/**
Mark Sleef5377b32006-10-10 01:42:59 +0000139 * Global time string
140 */
Mark Slee31985722006-05-24 21:45:31 +0000141char* g_time_str;
142
Mark Slee31985722006-05-24 21:45:31 +0000143/**
David Reisscbd4bac2007-08-14 17:12:33 +0000144 * The last parsed doctext comment.
145 */
146char* g_doctext;
147
148/**
149 * The location of the last parsed doctext comment.
150 */
151int g_doctext_lineno;
152
153/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000154 * Flags to control code generation
155 */
156bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000157bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000158bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000159bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000160bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000161bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000162bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000163bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000164bool gen_php = false;
165bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000166bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000167bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000168bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000169bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000170bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000171bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000172bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000173bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000174bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000175bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000176bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000177bool gen_recurse = false;
178
179/**
David Reiss204420f2008-01-11 20:59:03 +0000180 * MinGW doesn't have realpath, so use fallback implementation in that case,
181 * otherwise this just calls through to realpath
182 */
183char *saferealpath(const char *path, char *resolved_path) {
184#ifdef MINGW
185 char buf[MAX_PATH];
186 char* basename;
187 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
188 if (len == 0 || len > MAX_PATH - 1){
189 strcpy(resolved_path, path);
190 } else {
191 CharLowerBuff(buf, len);
192 strcpy(resolved_path, buf);
193 }
194 return resolved_path;
195#else
196 return realpath(path, resolved_path);
197#endif
198}
199
200
201/**
Mark Slee31985722006-05-24 21:45:31 +0000202 * Report an error to the user. This is called yyerror for historical
203 * reasons (lex and yacc expect the error reporting routine to be called
204 * this). Call this function to report any errors to the user.
205 * yyerror takes printf style arguments.
206 *
207 * @param fmt C format string followed by additional arguments
208 */
David Reiss0babe402008-06-10 22:56:12 +0000209void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000210 va_list args;
211 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000212 "[ERROR:%s:%d] (last token was '%s')\n",
213 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000214 yylineno,
215 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000216
217 va_start(args, fmt);
218 vfprintf(stderr, fmt, args);
219 va_end(args);
220
221 fprintf(stderr, "\n");
222}
223
224/**
225 * Prints a debug message from the parser.
226 *
227 * @param fmt C format string followed by additional arguments
228 */
David Reiss0babe402008-06-10 22:56:12 +0000229void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000230 if (g_debug == 0) {
231 return;
232 }
233 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000234 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000235 va_start(args, fmt);
236 vprintf(fmt, args);
237 va_end(args);
238 printf("\n");
239}
240
241/**
242 * Prints a verbose output mode message
243 *
244 * @param fmt C format string followed by additional arguments
245 */
David Reiss0babe402008-06-10 22:56:12 +0000246void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000247 if (g_verbose == 0) {
248 return;
249 }
250 va_list args;
251 va_start(args, fmt);
252 vprintf(fmt, args);
253 va_end(args);
254}
255
256/**
257 * Prints a warning message
258 *
259 * @param fmt C format string followed by additional arguments
260 */
David Reiss0babe402008-06-10 22:56:12 +0000261void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000262 if (g_warn < level) {
263 return;
264 }
265 va_list args;
266 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000267 va_start(args, fmt);
268 vprintf(fmt, args);
269 va_end(args);
270 printf("\n");
271}
272
273/**
274 * Prints a failure message and exits
275 *
276 * @param fmt C format string followed by additional arguments
277 */
Mark Slee30152872006-11-28 01:24:07 +0000278void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000279 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000280 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000281 va_start(args, fmt);
282 vfprintf(stderr, fmt, args);
283 va_end(args);
284 printf("\n");
285 exit(1);
286}
287
288/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000289 * Converts a string filename into a thrift program name
290 */
291string program_name(string filename) {
292 string::size_type slash = filename.rfind("/");
293 if (slash != string::npos) {
294 filename = filename.substr(slash+1);
295 }
296 string::size_type dot = filename.rfind(".");
297 if (dot != string::npos) {
298 filename = filename.substr(0, dot);
299 }
300 return filename;
301}
302
303/**
304 * Gets the directory path of a filename
305 */
306string directory_name(string filename) {
307 string::size_type slash = filename.rfind("/");
308 // No slash, just use the current directory
309 if (slash == string::npos) {
310 return ".";
311 }
312 return filename.substr(0, slash);
313}
314
315/**
316 * Finds the appropriate file path for the given filename
317 */
318string include_file(string filename) {
319 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000320 if (filename[0] == '/') {
321 // Realpath!
322 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000323 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000324 pwarning(0, "Cannot open include file %s\n", filename.c_str());
325 return std::string();
326 }
Mark Slee2c44d202007-05-16 02:18:07 +0000327
328 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000329 struct stat finfo;
330 if (stat(rp, &finfo) == 0) {
331 return rp;
332 }
333 } else { // relative path, start searching
334 // new search path with current dir global
335 vector<string> sp = g_incl_searchpath;
336 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000337
Martin Kraemer32c66e12006-11-09 00:06:36 +0000338 // iterate through paths
339 vector<string>::iterator it;
340 for (it = sp.begin(); it != sp.end(); it++) {
341 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000342
Martin Kraemer32c66e12006-11-09 00:06:36 +0000343 // Realpath!
344 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000345 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000346 continue;
347 }
Mark Slee2c44d202007-05-16 02:18:07 +0000348
Martin Kraemer32c66e12006-11-09 00:06:36 +0000349 // Stat this files
350 struct stat finfo;
351 if (stat(rp, &finfo) == 0) {
352 return rp;
353 }
354 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000355 }
Mark Slee2c44d202007-05-16 02:18:07 +0000356
Mark Sleef0712dc2006-10-25 19:03:57 +0000357 // Uh oh
358 pwarning(0, "Could not find include file %s\n", filename.c_str());
359 return std::string();
360}
361
362/**
David Reisscbd4bac2007-08-14 17:12:33 +0000363 * Clears any previously stored doctext string.
364 * Also prints a warning if we are discarding information.
365 */
366void clear_doctext() {
367 if (g_doctext != NULL) {
368 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
369 }
370 free(g_doctext);
371 g_doctext = NULL;
372}
373
374/**
David Reiss1ac05802007-07-30 22:00:27 +0000375 * Cleans up text commonly found in doxygen-like comments
376 *
377 * Warning: if you mix tabs and spaces in a non-uniform way,
378 * you will get what you deserve.
379 */
380char* clean_up_doctext(char* doctext) {
381 // Convert to C++ string, and remove Windows's carriage returns.
382 string docstring = doctext;
383 docstring.erase(
384 remove(docstring.begin(), docstring.end(), '\r'),
385 docstring.end());
386
387 // Separate into lines.
388 vector<string> lines;
389 string::size_type pos = string::npos;
390 string::size_type last;
391 while (true) {
392 last = (pos == string::npos) ? 0 : pos+1;
393 pos = docstring.find('\n', last);
394 if (pos == string::npos) {
395 // First bit of cleaning. If the last line is only whitespace, drop it.
396 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
397 if (nonwhite != string::npos) {
398 lines.push_back(docstring.substr(last));
399 }
400 break;
401 }
402 lines.push_back(docstring.substr(last, pos-last));
403 }
404
405 // A very profound docstring.
406 if (lines.empty()) {
407 return NULL;
408 }
409
410 // Clear leading whitespace from the first line.
411 pos = lines.front().find_first_not_of(" \t");
412 lines.front().erase(0, pos);
413
414 // If every nonblank line after the first has the same number of spaces/tabs,
415 // then a star, remove them.
416 bool have_prefix = true;
417 bool found_prefix = false;
418 string::size_type prefix_len = 0;
419 vector<string>::iterator l_iter;
420 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
421 if (l_iter->empty()) {
422 continue;
423 }
424
425 pos = l_iter->find_first_not_of(" \t");
426 if (!found_prefix) {
427 if (pos != string::npos) {
428 if (l_iter->at(pos) == '*') {
429 found_prefix = true;
430 prefix_len = pos;
431 } else {
432 have_prefix = false;
433 break;
434 }
435 } else {
436 // Whitespace-only line. Truncate it.
437 l_iter->clear();
438 }
439 } else if (l_iter->size() > pos
440 && l_iter->at(pos) == '*'
441 && pos == prefix_len) {
442 // Business as usual.
443 } else if (pos == string::npos) {
444 // Whitespace-only line. Let's truncate it for them.
445 l_iter->clear();
446 } else {
447 // The pattern has been broken.
448 have_prefix = false;
449 break;
450 }
451 }
452
453 // If our prefix survived, delete it from every line.
454 if (have_prefix) {
455 // Get the star too.
456 prefix_len++;
457 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
458 l_iter->erase(0, prefix_len);
459 }
460 }
461
462 // Now delete the minimum amount of leading whitespace from each line.
463 prefix_len = string::npos;
464 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
465 if (l_iter->empty()) {
466 continue;
467 }
468 pos = l_iter->find_first_not_of(" \t");
469 if (pos != string::npos
470 && (prefix_len == string::npos || pos < prefix_len)) {
471 prefix_len = pos;
472 }
473 }
474
475 // If our prefix survived, delete it from every line.
476 if (prefix_len != string::npos) {
477 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
478 l_iter->erase(0, prefix_len);
479 }
480 }
481
482 // Remove trailing whitespace from every line.
483 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
484 pos = l_iter->find_last_not_of(" \t");
485 if (pos != string::npos && pos != l_iter->length()-1) {
486 l_iter->erase(pos+1);
487 }
488 }
489
490 // If the first line is empty, remove it.
491 // Don't do this earlier because a lot of steps skip the first line.
492 if (lines.front().empty()) {
493 lines.erase(lines.begin());
494 }
495
496 // Now rejoin the lines and copy them back into doctext.
497 docstring.clear();
498 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
499 docstring += *l_iter;
500 docstring += '\n';
501 }
502
503 assert(docstring.length() <= strlen(doctext));
504 strcpy(doctext, docstring.c_str());
505 return doctext;
506}
507
508/** Set to true to debug docstring parsing */
509static bool dump_docs = false;
510
511/**
512 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000513 * Only works for top-level definitions and the whole program doc
514 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000515 */
516void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000517 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000518 if (!progdoc.empty()) {
519 printf("Whole program doc:\n%s\n", progdoc.c_str());
520 }
David Reiss1ac05802007-07-30 22:00:27 +0000521 const vector<t_typedef*>& typedefs = program->get_typedefs();
522 vector<t_typedef*>::const_iterator t_iter;
523 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
524 t_typedef* td = *t_iter;
525 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000526 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
527 }
528 }
529 const vector<t_enum*>& enums = program->get_enums();
530 vector<t_enum*>::const_iterator e_iter;
531 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
532 t_enum* en = *e_iter;
533 if (en->has_doc()) {
534 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
535 }
536 }
537 const vector<t_const*>& consts = program->get_consts();
538 vector<t_const*>::const_iterator c_iter;
539 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
540 t_const* co = *c_iter;
541 if (co->has_doc()) {
542 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
543 }
544 }
545 const vector<t_struct*>& structs = program->get_structs();
546 vector<t_struct*>::const_iterator s_iter;
547 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
548 t_struct* st = *s_iter;
549 if (st->has_doc()) {
550 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
551 }
552 }
553 const vector<t_struct*>& xceptions = program->get_xceptions();
554 vector<t_struct*>::const_iterator x_iter;
555 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
556 t_struct* xn = *x_iter;
557 if (xn->has_doc()) {
558 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
559 }
560 }
561 const vector<t_service*>& services = program->get_services();
562 vector<t_service*>::const_iterator v_iter;
563 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
564 t_service* sv = *v_iter;
565 if (sv->has_doc()) {
566 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000567 }
568 }
569}
570
571/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000572 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000573 */
574void generate_all_fingerprints(t_program* program) {
575 const vector<t_struct*>& structs = program->get_structs();
576 vector<t_struct*>::const_iterator s_iter;
577 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
578 t_struct* st = *s_iter;
579 st->generate_fingerprint();
580 }
581
David Reissd779cbe2007-08-31 01:42:55 +0000582 const vector<t_struct*>& xceptions = program->get_xceptions();
583 vector<t_struct*>::const_iterator x_iter;
584 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
585 t_struct* st = *x_iter;
586 st->generate_fingerprint();
587 }
588
David Reiss3c5d2fd2008-02-08 21:58:06 +0000589 const vector<t_enum*>& enums = program->get_enums();
590 vector<t_enum*>::const_iterator e_iter;
591 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
592 t_enum* e = *e_iter;
593 e->generate_fingerprint();
594 }
595
David Reiss47557bc2007-09-04 21:31:04 +0000596 g_type_void->generate_fingerprint();
597
David Reiss18bf22d2007-08-28 20:49:17 +0000598 // If you want to generate fingerprints for implicit structures, start here.
599 /*
600 const vector<t_service*>& services = program->get_services();
601 vector<t_service*>::const_iterator v_iter;
602 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
603 t_service* sv = *v_iter;
604 }
605 */
606}
607
608/**
David Reissdd08f6d2008-06-30 20:24:24 +0000609 * Prints the version number
610 */
611void version() {
612 printf("Thrift version %s-%s\n", THRIFT_VERSION, THRIFT_REVISION);
613}
614
615/**
Mark Slee31985722006-05-24 21:45:31 +0000616 * Diplays the usage message and then exits with an error code.
617 */
618void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000619 fprintf(stderr, "Usage: thrift [options] file\n");
620 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000621 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000622 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
623 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000624 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000625 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000626 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
627 fprintf(stderr, " -strict Strict compiler warnings on\n");
628 fprintf(stderr, " -v[erbose] Verbose mode\n");
629 fprintf(stderr, " -r[ecurse] Also generate included files\n");
630 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000631 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
632 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
633 fprintf(stderr, " Keys and values are options passed to the generator.\n");
634 fprintf(stderr, " Many options will not require values.\n");
635 fprintf(stderr, "\n");
636 fprintf(stderr, "Available generators (and options):\n");
637
638 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
639 t_generator_registry::gen_map_t::iterator iter;
640 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
641 fprintf(stderr, " %s (%s):\n",
642 iter->second->get_short_name().c_str(),
643 iter->second->get_long_name().c_str());
644 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
645 }
Mark Slee31985722006-05-24 21:45:31 +0000646 exit(1);
647}
648
649/**
Mark Slee30152872006-11-28 01:24:07 +0000650 * You know, when I started working on Thrift I really thought it wasn't going
651 * to become a programming language because it was just a generator and it
652 * wouldn't need runtime type information and all that jazz. But then we
653 * decided to add constants, and all of a sudden that means runtime type
654 * validation and inference, except the "runtime" is the code generator
655 * runtime. Shit. I've been had.
656 */
657void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
658 if (type->is_void()) {
659 throw "type error: cannot declare a void const: " + name;
660 }
661
662 if (type->is_base_type()) {
663 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
664 switch (tbase) {
665 case t_base_type::TYPE_STRING:
666 if (value->get_type() != t_const_value::CV_STRING) {
667 throw "type error: const \"" + name + "\" was declared as string";
668 }
669 break;
670 case t_base_type::TYPE_BOOL:
671 if (value->get_type() != t_const_value::CV_INTEGER) {
672 throw "type error: const \"" + name + "\" was declared as bool";
673 }
674 break;
675 case t_base_type::TYPE_BYTE:
676 if (value->get_type() != t_const_value::CV_INTEGER) {
677 throw "type error: const \"" + name + "\" was declared as byte";
678 }
679 break;
680 case t_base_type::TYPE_I16:
681 if (value->get_type() != t_const_value::CV_INTEGER) {
682 throw "type error: const \"" + name + "\" was declared as i16";
683 }
684 break;
685 case t_base_type::TYPE_I32:
686 if (value->get_type() != t_const_value::CV_INTEGER) {
687 throw "type error: const \"" + name + "\" was declared as i32";
688 }
689 break;
690 case t_base_type::TYPE_I64:
691 if (value->get_type() != t_const_value::CV_INTEGER) {
692 throw "type error: const \"" + name + "\" was declared as i64";
693 }
694 break;
695 case t_base_type::TYPE_DOUBLE:
696 if (value->get_type() != t_const_value::CV_INTEGER &&
697 value->get_type() != t_const_value::CV_DOUBLE) {
698 throw "type error: const \"" + name + "\" was declared as double";
699 }
700 break;
701 default:
David Reissdd7796f2007-08-28 21:09:06 +0000702 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000703 }
704 } else if (type->is_enum()) {
705 if (value->get_type() != t_const_value::CV_INTEGER) {
706 throw "type error: const \"" + name + "\" was declared as enum";
707 }
708 } else if (type->is_struct() || type->is_xception()) {
709 if (value->get_type() != t_const_value::CV_MAP) {
710 throw "type error: const \"" + name + "\" was declared as struct/xception";
711 }
712 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
713 vector<t_field*>::const_iterator f_iter;
714
715 const map<t_const_value*, t_const_value*>& val = value->get_map();
716 map<t_const_value*, t_const_value*>::const_iterator v_iter;
717 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
718 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
719 throw "type error: " + name + " struct key must be string";
720 }
721 t_type* field_type = NULL;
722 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
723 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
724 field_type = (*f_iter)->get_type();
725 }
726 }
727 if (field_type == NULL) {
728 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
729 }
730
731 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
732 }
733 } else if (type->is_map()) {
734 t_type* k_type = ((t_map*)type)->get_key_type();
735 t_type* v_type = ((t_map*)type)->get_val_type();
736 const map<t_const_value*, t_const_value*>& val = value->get_map();
737 map<t_const_value*, t_const_value*>::const_iterator v_iter;
738 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
739 validate_const_rec(name + "<key>", k_type, v_iter->first);
740 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000741 }
Mark Slee30152872006-11-28 01:24:07 +0000742 } else if (type->is_list() || type->is_set()) {
743 t_type* e_type;
744 if (type->is_list()) {
745 e_type = ((t_list*)type)->get_elem_type();
746 } else {
747 e_type = ((t_set*)type)->get_elem_type();
748 }
749 const vector<t_const_value*>& val = value->get_list();
750 vector<t_const_value*>::const_iterator v_iter;
751 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
752 validate_const_rec(name + "<elem>", e_type, *v_iter);
753 }
754 }
755}
756
757/**
758 * Check the type of the parsed const information against its declared type
759 */
760void validate_const_type(t_const* c) {
761 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
762}
763
764/**
Mark Slee7ff32452007-02-01 05:26:18 +0000765 * Check the type of a default value assigned to a field.
766 */
767void validate_field_value(t_field* field, t_const_value* cv) {
768 validate_const_rec(field->get_name(), field->get_type(), cv);
769}
770
771/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000772 * Check that all the elements of a throws block are actually exceptions.
773 */
774bool validate_throws(t_struct* throws) {
775 const vector<t_field*>& members = throws->get_members();
776 vector<t_field*>::const_iterator m_iter;
777 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
778 if (!(*m_iter)->get_type()->is_xception()) {
779 return false;
780 }
781 }
782 return true;
783}
784
785/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000786 * Parses a program
787 */
Mark Slee2c44d202007-05-16 02:18:07 +0000788void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000789 // Get scope file path
790 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000791
Mark Sleef0712dc2006-10-25 19:03:57 +0000792 // Set current dir global, which is used in the include_file function
793 g_curdir = directory_name(path);
794 g_curpath = path;
795
796 // Open the file
797 yyin = fopen(path.c_str(), "r");
798 if (yyin == 0) {
799 failure("Could not open input file: \"%s\"", path.c_str());
800 }
801
802 // Create new scope and scan for includes
803 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000804 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000805 g_program = program;
806 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000807 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000808 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000809 if (yyparse() != 0) {
810 failure("Parser error during include pass.");
811 }
812 } catch (string x) {
813 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000814 }
815 fclose(yyin);
816
817 // Recursively parse all the include programs
818 vector<t_program*>& includes = program->get_includes();
819 vector<t_program*>::iterator iter;
820 for (iter = includes.begin(); iter != includes.end(); ++iter) {
821 parse(*iter, program);
822 }
823
David Reiss204420f2008-01-11 20:59:03 +0000824 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000825 g_parse_mode = PROGRAM;
826 g_program = program;
827 g_scope = program->scope();
828 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
829 g_parent_prefix = program->get_name() + ".";
830 g_curpath = path;
831 yyin = fopen(path.c_str(), "r");
832 if (yyin == 0) {
833 failure("Could not open input file: \"%s\"", path.c_str());
834 }
835 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000836 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000837 try {
838 if (yyparse() != 0) {
839 failure("Parser error during types pass.");
840 }
841 } catch (string x) {
842 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000843 }
844 fclose(yyin);
845}
846
847/**
848 * Generate code
849 */
David Reissbd0db882008-02-27 01:54:51 +0000850void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000851 // Oooohh, recursive code generation, hot!!
852 if (gen_recurse) {
853 const vector<t_program*>& includes = program->get_includes();
854 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000855 // Propogate output path from parent to child programs
856 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000857
David Reissbd0db882008-02-27 01:54:51 +0000858 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000859 }
860 }
861
862 // Generate code!
863 try {
864 pverbose("Program: %s\n", program->get_path().c_str());
865
David Reiss18bf22d2007-08-28 20:49:17 +0000866 // Compute fingerprints.
867 generate_all_fingerprints(program);
868
David Reiss1ac05802007-07-30 22:00:27 +0000869 if (dump_docs) {
870 dump_docstrings(program);
871 }
David Reissbd0db882008-02-27 01:54:51 +0000872
873 vector<string>::const_iterator iter;
874 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
875 t_generator* generator = t_generator_registry::get_generator(program, *iter);
876
877 if (generator == NULL) {
878 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
879 } else {
880 pverbose("Generating \"%s\"\n", iter->c_str());
881 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000882 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000883 }
884 }
885
Mark Sleef0712dc2006-10-25 19:03:57 +0000886 } catch (string s) {
887 printf("Error: %s\n", s.c_str());
888 } catch (const char* exc) {
889 printf("Error: %s\n", exc);
890 }
891
892}
893
894/**
Mark Sleef5377b32006-10-10 01:42:59 +0000895 * Parse it up.. then spit it back out, in pretty much every language. Alright
896 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000897 */
898int main(int argc, char** argv) {
899 int i;
dweatherford65b70752007-10-31 02:18:14 +0000900 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000901
Mark Sleeb15a68b2006-06-07 06:46:24 +0000902 // Setup time string
903 time_t now = time(NULL);
904 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000905
Mark Sleef0712dc2006-10-25 19:03:57 +0000906 // Check for necessary arguments, you gotta have at least a filename and
907 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000908 if (argc < 2) {
909 usage();
910 }
Mark Slee31985722006-05-24 21:45:31 +0000911
David Reissbd0db882008-02-27 01:54:51 +0000912 vector<string> generator_strings;
913
David Reiss9cc2c132008-02-27 01:54:47 +0000914 // Set the current path to a dummy value to make warning messages clearer.
915 g_curpath = "arguments";
916
Mark Sleef5377b32006-10-10 01:42:59 +0000917 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000918 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000919 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000920
Mark Sleefdbee812006-09-27 18:50:48 +0000921 arg = strtok(argv[i], " ");
922 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000923 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000924 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000925 ++arg;
926 }
927
David Reissdd08f6d2008-06-30 20:24:24 +0000928 if (strcmp(arg, "-version") == 0) {
929 version();
930 exit(1);
931 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000932 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000933 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000934 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000935 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000936 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +0000937 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000938 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000940 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000941 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000942 } else if (strcmp(arg, "-gen") == 0) {
943 arg = argv[++i];
944 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000945 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000946 usage();
947 }
948 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000949 } else if (strcmp(arg, "-dense") == 0) {
950 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000951 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000952 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000953 } else if (strcmp(arg, "-javabean") == 0) {
954 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000955 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000956 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000957 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000958 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000959 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000960 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000961 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000962 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000963 gen_phps = true;
964 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000965 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000966 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000967 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000968 gen_php = true;
969 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000970 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000971 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000972 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000973 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +0000974 } else if (strcmp(arg, "-rest") == 0) {
975 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +0000976 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000977 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +0000978 } else if (strcmp(arg, "-pyns") == 0) {
979 gen_py = true;
980 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000981 } else if (strcmp(arg, "-rb") == 0) {
982 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000983 } else if (strcmp(arg, "-xsd") == 0) {
984 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000985 } else if (strcmp(arg, "-perl") == 0) {
986 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +0000987 } else if (strcmp(arg, "-erl") == 0) {
988 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000989 } else if (strcmp(arg, "-ocaml") == 0) {
990 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +0000991 } else if (strcmp(arg, "-hs") == 0) {
992 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +0000993 } else if (strcmp(arg, "-cocoa") == 0) {
994 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +0000995 } else if (strcmp(arg, "-st") == 0) {
996 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +0000997 } else if (strcmp(arg, "-csharp") == 0) {
998 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +0000999 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1000 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001001 } else if (strcmp(arg, "-I") == 0) {
1002 // An argument of "-I\ asdf" is invalid and has unknown results
1003 arg = argv[++i];
1004
1005 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001006 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001007 usage();
1008 }
1009 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001010 } else if (strcmp(arg, "-o") == 0) {
1011 arg = argv[++i];
1012 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001013 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001014 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001015 }
dweatherford65b70752007-10-31 02:18:14 +00001016 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001017
1018#ifdef MINGW
1019 //strip out trailing \ on Windows
1020 int last = out_path.length()-1;
1021 if (out_path[last] == '\\')
1022 {
1023 out_path.erase(last);
1024 }
1025#endif
1026
dweatherford65b70752007-10-31 02:18:14 +00001027 struct stat sb;
1028 if (stat(out_path.c_str(), &sb) < 0) {
1029 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1030 return -1;
1031 }
1032 if (! S_ISDIR(sb.st_mode)) {
1033 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1034 return -1;
1035 }
Mark Sleefdbee812006-09-27 18:50:48 +00001036 } else {
1037 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1038 usage();
1039 }
1040
1041 // Tokenize more
1042 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001043 }
1044 }
Mark Slee2c44d202007-05-16 02:18:07 +00001045
David Reissdd08f6d2008-06-30 20:24:24 +00001046 // if you're asking for version, you have a right not to pass a file
1047 if (strcmp(argv[argc-1], "-version") == 0) {
1048 version();
1049 exit(1);
1050 }
1051
David Reiss400cd4b2008-02-27 01:54:55 +00001052 // TODO(dreiss): Delete these when everyone is using the new hotness.
1053 if (gen_cpp) {
1054 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1055 string gen_string = "cpp:";
1056 if (gen_dense) {
1057 gen_string.append("dense,");
1058 }
1059 if (g_cpp_use_include_prefix) {
1060 gen_string.append("include_prefix,");
1061 }
1062 generator_strings.push_back(gen_string);
1063 }
David Reisse1404d22008-02-27 01:55:05 +00001064 if (gen_java) {
1065 pwarning(1, "-java is deprecated. Use --gen java");
1066 generator_strings.push_back("java");
1067 }
1068 if (gen_javabean) {
1069 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1070 generator_strings.push_back("java:beans");
1071 }
David Reiss861869b2008-03-27 21:41:23 +00001072 if (gen_csharp) {
1073 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1074 generator_strings.push_back("csharp");
1075 }
David Reiss558e3992008-03-27 21:41:40 +00001076 if (gen_py) {
1077 pwarning(1, "-py is deprecated. Use --gen py");
1078 generator_strings.push_back("py");
1079 }
David Reissa640cea2008-03-27 21:42:01 +00001080 if (gen_rb) {
1081 pwarning(1, "-rb is deprecated. Use --gen rb");
1082 generator_strings.push_back("rb");
1083 }
David Reiss2b386c52008-03-27 21:42:23 +00001084 if (gen_perl) {
1085 pwarning(1, "-perl is deprecated. Use --gen perl");
1086 generator_strings.push_back("perl");
1087 }
David Reissa9ea68b2009-02-17 20:28:24 +00001088 if (gen_php || gen_phpi) {
1089 pwarning(1, "-php is deprecated. Use --gen php");
1090 string gen_string = "php:";
1091 if (gen_phpi) {
1092 gen_string.append("inlined,");
1093 } else if(gen_phps) {
1094 gen_string.append("server,");
1095 } else if(gen_phpa) {
1096 gen_string.append("autoload,");
1097 } else if(gen_phpo) {
1098 gen_string.append("oop,");
1099 } else if(gen_rest) {
1100 gen_string.append("rest,");
1101 }
1102 generator_strings.push_back(gen_string);
1103 }
David Reissd01c64d2008-03-27 21:40:55 +00001104 if (gen_cocoa) {
1105 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1106 generator_strings.push_back("cocoa");
1107 }
David Reissa2047832009-02-17 20:27:54 +00001108 if (gen_erl) {
1109 pwarning(1, "-erl is deprecated. Use --gen erl");
1110 generator_strings.push_back("erl");
1111 }
David Reissa890b572008-03-27 21:40:35 +00001112 if (gen_st) {
1113 pwarning(1, "-st is deprecated. Use --gen st");
1114 generator_strings.push_back("st");
1115 }
David Reissf0521b12008-03-27 21:40:05 +00001116 if (gen_ocaml) {
1117 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1118 generator_strings.push_back("ocaml");
1119 }
David Reissaf3ab262008-03-27 21:40:16 +00001120 if (gen_hs) {
1121 pwarning(1, "-hs is deprecated. Use --gen hs");
1122 generator_strings.push_back("hs");
1123 }
David Reiss4ba67102009-02-17 20:28:06 +00001124 if (gen_xsd) {
1125 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1126 generator_strings.push_back("xsd");
1127 }
David Reisse1404d22008-02-27 01:55:05 +00001128
Mark Sleef0712dc2006-10-25 19:03:57 +00001129 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001130 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001131 fprintf(stderr, "!!! No output language(s) specified\n\n");
1132 usage();
1133 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001134
1135 // Real-pathify it
1136 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001137 if (argv[i] == NULL) {
1138 fprintf(stderr, "!!! Missing file name\n");
1139 usage();
1140 }
David Reiss204420f2008-01-11 20:59:03 +00001141 if (saferealpath(argv[i], rp) == NULL) {
1142 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001143 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001144 string input_file(rp);
1145
Mark Sleef5377b32006-10-10 01:42:59 +00001146 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001147 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001148 if (out_path.size()) {
1149 program->set_out_path(out_path);
1150 }
kholst76f2c882008-01-16 02:47:41 +00001151
David Reiss4b6a3c72008-02-27 22:28:12 +00001152 // Compute the cpp include prefix.
1153 // infer this from the filename passed in
1154 string input_filename = argv[i];
1155 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001156
David Reiss4b6a3c72008-02-27 22:28:12 +00001157 string::size_type last_slash = string::npos;
1158 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1159 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001160 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001161
David Reiss4b6a3c72008-02-27 22:28:12 +00001162 program->set_include_prefix(include_prefix);
1163
Mark Sleef0712dc2006-10-25 19:03:57 +00001164 // Initialize global types
1165 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1166 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001167 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1168 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001169 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1170 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001171 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1172 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1173 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1174 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1175 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1176 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001177
Mark Sleef5377b32006-10-10 01:42:59 +00001178 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001179 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001180
David Reiss9cc2c132008-02-27 01:54:47 +00001181 // The current path is not really relevant when we are doing generation.
1182 // Reset the variable to make warning messages clearer.
1183 g_curpath = "generation";
1184 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1185 // That is what shows up during argument parsing.
1186 yylineno = 1;
1187
Mark Sleef0712dc2006-10-25 19:03:57 +00001188 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001189 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001190
Mark Sleef0712dc2006-10-25 19:03:57 +00001191 // Clean up. Who am I kidding... this program probably orphans heap memory
1192 // all over the place, but who cares because it is about to exit and it is
1193 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001194
Mark Sleef0712dc2006-10-25 19:03:57 +00001195 delete program;
1196 delete g_type_void;
1197 delete g_type_string;
1198 delete g_type_bool;
1199 delete g_type_byte;
1200 delete g_type_i16;
1201 delete g_type_i32;
1202 delete g_type_i64;
1203 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001204
1205 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001206 return 0;
1207}