blob: da86d0d89a7ba64a670ac69848eb24fa6a7f0cea [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>
David Reiss5ad12602010-08-31 16:51:30 +000034#include <time.h>
Mark Slee31985722006-05-24 21:45:31 +000035#include <string>
David Reiss739cbe22008-04-15 05:44:00 +000036#include <algorithm>
Mark Sleef0712dc2006-10-25 19:03:57 +000037#include <sys/types.h>
38#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000039#include <errno.h>
David Reissab55ed52008-06-11 01:17:00 +000040#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000041
David Reiss204420f2008-01-11 20:59:03 +000042#ifdef MINGW
43# include <windows.h> /* for GetFullPathName */
David Reiss204420f2008-01-11 20:59:03 +000044#endif
45
Mark Sleef0712dc2006-10-25 19:03:57 +000046// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000047#include "globals.h"
48
49#include "main.h"
50#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000051#include "parse/t_scope.h"
David Reissbbbbe882009-02-17 20:27:48 +000052#include "generate/t_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000053
David Reissdd08f6d2008-06-30 20:24:24 +000054#include "version.h"
55
Mark Slee31985722006-05-24 21:45:31 +000056using namespace std;
57
Mark Sleef5377b32006-10-10 01:42:59 +000058/**
59 * Global program tree
60 */
Mark Slee31985722006-05-24 21:45:31 +000061t_program* g_program;
62
Mark Sleef5377b32006-10-10 01:42:59 +000063/**
Mark Sleef0712dc2006-10-25 19:03:57 +000064 * Global types
65 */
66
67t_type* g_type_void;
68t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000069t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000070t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000071t_type* g_type_bool;
72t_type* g_type_byte;
73t_type* g_type_i16;
74t_type* g_type_i32;
75t_type* g_type_i64;
76t_type* g_type_double;
77
78/**
79 * Global scope
80 */
81t_scope* g_scope;
82
83/**
84 * Parent scope to also parse types
85 */
86t_scope* g_parent_scope;
87
88/**
89 * Prefix for putting types in parent scope
90 */
91string g_parent_prefix;
92
93/**
94 * Parsing pass
95 */
96PARSE_MODE g_parse_mode;
97
98/**
99 * Current directory of file being parsed
100 */
101string g_curdir;
102
103/**
104 * Current file being parsed
105 */
106string g_curpath;
107
108/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000109 * Search path for inclusions
110 */
Mark Slee2329a832006-11-09 00:23:30 +0000111vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000112
113/**
kholst76f2c882008-01-16 02:47:41 +0000114 * Should C++ include statements use path prefixes for other thrift-generated
115 * header files
116 */
117bool g_cpp_use_include_prefix = false;
118
119/**
Mark Sleef5377b32006-10-10 01:42:59 +0000120 * Global debug state
121 */
Mark Slee31985722006-05-24 21:45:31 +0000122int g_debug = 0;
123
Mark Sleef5377b32006-10-10 01:42:59 +0000124/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000125 * Strictness level
126 */
127int g_strict = 127;
128
129/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000130 * Warning level
131 */
132int g_warn = 1;
133
134/**
135 * Verbose output
136 */
137int g_verbose = 0;
138
139/**
Mark Sleef5377b32006-10-10 01:42:59 +0000140 * Global time string
141 */
Mark Slee31985722006-05-24 21:45:31 +0000142char* g_time_str;
143
Mark Slee31985722006-05-24 21:45:31 +0000144/**
David Reisscbd4bac2007-08-14 17:12:33 +0000145 * The last parsed doctext comment.
146 */
147char* g_doctext;
148
149/**
150 * The location of the last parsed doctext comment.
151 */
152int g_doctext_lineno;
153
154/**
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000155 * Whether or not negative field keys are accepted.
156 */
157int g_allow_neg_field_keys;
158
159/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000160 * Flags to control code generation
161 */
162bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000163bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000164bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000165bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000166bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000167bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000168bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000169bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000170bool gen_php = false;
171bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000172bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000173bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000174bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000175bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000176bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000177bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000178bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000179bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000180bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000181bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000182bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000183bool gen_recurse = false;
184
185/**
David Reiss204420f2008-01-11 20:59:03 +0000186 * MinGW doesn't have realpath, so use fallback implementation in that case,
187 * otherwise this just calls through to realpath
188 */
189char *saferealpath(const char *path, char *resolved_path) {
190#ifdef MINGW
191 char buf[MAX_PATH];
192 char* basename;
193 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
194 if (len == 0 || len > MAX_PATH - 1){
195 strcpy(resolved_path, path);
196 } else {
David Reiss204420f2008-01-11 20:59:03 +0000197 strcpy(resolved_path, buf);
198 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000199
200 // Replace backslashes with forward slashes so the
201 // rest of the code behaves correctly.
202 size_t resolved_len = strlen(resolved_path);
203 for (size_t i = 0; i < resolved_len; i++) {
204 if (resolved_path[i] == '\\') {
205 resolved_path[i] = '/';
206 }
207 }
David Reiss204420f2008-01-11 20:59:03 +0000208 return resolved_path;
209#else
210 return realpath(path, resolved_path);
211#endif
212}
213
214
215/**
Mark Slee31985722006-05-24 21:45:31 +0000216 * Report an error to the user. This is called yyerror for historical
217 * reasons (lex and yacc expect the error reporting routine to be called
218 * this). Call this function to report any errors to the user.
219 * yyerror takes printf style arguments.
220 *
221 * @param fmt C format string followed by additional arguments
222 */
David Reiss0babe402008-06-10 22:56:12 +0000223void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000224 va_list args;
225 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000226 "[ERROR:%s:%d] (last token was '%s')\n",
227 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000228 yylineno,
229 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000230
231 va_start(args, fmt);
232 vfprintf(stderr, fmt, args);
233 va_end(args);
234
235 fprintf(stderr, "\n");
236}
237
238/**
239 * Prints a debug message from the parser.
240 *
241 * @param fmt C format string followed by additional arguments
242 */
David Reiss0babe402008-06-10 22:56:12 +0000243void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000244 if (g_debug == 0) {
245 return;
246 }
247 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000248 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000249 va_start(args, fmt);
250 vprintf(fmt, args);
251 va_end(args);
252 printf("\n");
253}
254
255/**
256 * Prints a verbose output mode message
257 *
258 * @param fmt C format string followed by additional arguments
259 */
David Reiss0babe402008-06-10 22:56:12 +0000260void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000261 if (g_verbose == 0) {
262 return;
263 }
264 va_list args;
265 va_start(args, fmt);
266 vprintf(fmt, args);
267 va_end(args);
268}
269
270/**
271 * Prints a warning message
272 *
273 * @param fmt C format string followed by additional arguments
274 */
David Reiss0babe402008-06-10 22:56:12 +0000275void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000276 if (g_warn < level) {
277 return;
278 }
279 va_list args;
280 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000281 va_start(args, fmt);
282 vprintf(fmt, args);
283 va_end(args);
284 printf("\n");
285}
286
287/**
288 * Prints a failure message and exits
289 *
290 * @param fmt C format string followed by additional arguments
291 */
Mark Slee30152872006-11-28 01:24:07 +0000292void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000293 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000294 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000295 va_start(args, fmt);
296 vfprintf(stderr, fmt, args);
297 va_end(args);
298 printf("\n");
299 exit(1);
300}
301
302/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000303 * Converts a string filename into a thrift program name
304 */
305string program_name(string filename) {
306 string::size_type slash = filename.rfind("/");
307 if (slash != string::npos) {
308 filename = filename.substr(slash+1);
309 }
310 string::size_type dot = filename.rfind(".");
311 if (dot != string::npos) {
312 filename = filename.substr(0, dot);
313 }
314 return filename;
315}
316
317/**
318 * Gets the directory path of a filename
319 */
320string directory_name(string filename) {
321 string::size_type slash = filename.rfind("/");
322 // No slash, just use the current directory
323 if (slash == string::npos) {
324 return ".";
325 }
326 return filename.substr(0, slash);
327}
328
329/**
330 * Finds the appropriate file path for the given filename
331 */
332string include_file(string filename) {
333 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000334 if (filename[0] == '/') {
335 // Realpath!
336 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000337 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000338 pwarning(0, "Cannot open include file %s\n", filename.c_str());
339 return std::string();
340 }
Mark Slee2c44d202007-05-16 02:18:07 +0000341
342 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000343 struct stat finfo;
344 if (stat(rp, &finfo) == 0) {
345 return rp;
346 }
347 } else { // relative path, start searching
348 // new search path with current dir global
349 vector<string> sp = g_incl_searchpath;
350 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000351
Martin Kraemer32c66e12006-11-09 00:06:36 +0000352 // iterate through paths
353 vector<string>::iterator it;
354 for (it = sp.begin(); it != sp.end(); it++) {
355 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000356
Martin Kraemer32c66e12006-11-09 00:06:36 +0000357 // Realpath!
358 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000359 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000360 continue;
361 }
Mark Slee2c44d202007-05-16 02:18:07 +0000362
Martin Kraemer32c66e12006-11-09 00:06:36 +0000363 // Stat this files
364 struct stat finfo;
365 if (stat(rp, &finfo) == 0) {
366 return rp;
367 }
368 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000369 }
Mark Slee2c44d202007-05-16 02:18:07 +0000370
Mark Sleef0712dc2006-10-25 19:03:57 +0000371 // Uh oh
372 pwarning(0, "Could not find include file %s\n", filename.c_str());
373 return std::string();
374}
375
376/**
David Reisscbd4bac2007-08-14 17:12:33 +0000377 * Clears any previously stored doctext string.
378 * Also prints a warning if we are discarding information.
379 */
380void clear_doctext() {
381 if (g_doctext != NULL) {
382 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
383 }
384 free(g_doctext);
385 g_doctext = NULL;
386}
387
388/**
David Reiss1ac05802007-07-30 22:00:27 +0000389 * Cleans up text commonly found in doxygen-like comments
390 *
391 * Warning: if you mix tabs and spaces in a non-uniform way,
392 * you will get what you deserve.
393 */
394char* clean_up_doctext(char* doctext) {
395 // Convert to C++ string, and remove Windows's carriage returns.
396 string docstring = doctext;
397 docstring.erase(
398 remove(docstring.begin(), docstring.end(), '\r'),
399 docstring.end());
400
401 // Separate into lines.
402 vector<string> lines;
403 string::size_type pos = string::npos;
404 string::size_type last;
405 while (true) {
406 last = (pos == string::npos) ? 0 : pos+1;
407 pos = docstring.find('\n', last);
408 if (pos == string::npos) {
409 // First bit of cleaning. If the last line is only whitespace, drop it.
410 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
411 if (nonwhite != string::npos) {
412 lines.push_back(docstring.substr(last));
413 }
414 break;
415 }
416 lines.push_back(docstring.substr(last, pos-last));
417 }
418
419 // A very profound docstring.
420 if (lines.empty()) {
421 return NULL;
422 }
423
424 // Clear leading whitespace from the first line.
425 pos = lines.front().find_first_not_of(" \t");
426 lines.front().erase(0, pos);
427
428 // If every nonblank line after the first has the same number of spaces/tabs,
429 // then a star, remove them.
430 bool have_prefix = true;
431 bool found_prefix = false;
432 string::size_type prefix_len = 0;
433 vector<string>::iterator l_iter;
434 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
435 if (l_iter->empty()) {
436 continue;
437 }
438
439 pos = l_iter->find_first_not_of(" \t");
440 if (!found_prefix) {
441 if (pos != string::npos) {
442 if (l_iter->at(pos) == '*') {
443 found_prefix = true;
444 prefix_len = pos;
445 } else {
446 have_prefix = false;
447 break;
448 }
449 } else {
450 // Whitespace-only line. Truncate it.
451 l_iter->clear();
452 }
453 } else if (l_iter->size() > pos
454 && l_iter->at(pos) == '*'
455 && pos == prefix_len) {
456 // Business as usual.
457 } else if (pos == string::npos) {
458 // Whitespace-only line. Let's truncate it for them.
459 l_iter->clear();
460 } else {
461 // The pattern has been broken.
462 have_prefix = false;
463 break;
464 }
465 }
466
467 // If our prefix survived, delete it from every line.
468 if (have_prefix) {
469 // Get the star too.
470 prefix_len++;
471 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
472 l_iter->erase(0, prefix_len);
473 }
474 }
475
476 // Now delete the minimum amount of leading whitespace from each line.
477 prefix_len = string::npos;
478 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
479 if (l_iter->empty()) {
480 continue;
481 }
482 pos = l_iter->find_first_not_of(" \t");
483 if (pos != string::npos
484 && (prefix_len == string::npos || pos < prefix_len)) {
485 prefix_len = pos;
486 }
487 }
488
489 // If our prefix survived, delete it from every line.
490 if (prefix_len != string::npos) {
491 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
492 l_iter->erase(0, prefix_len);
493 }
494 }
495
496 // Remove trailing whitespace from every line.
497 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
498 pos = l_iter->find_last_not_of(" \t");
499 if (pos != string::npos && pos != l_iter->length()-1) {
500 l_iter->erase(pos+1);
501 }
502 }
503
504 // If the first line is empty, remove it.
505 // Don't do this earlier because a lot of steps skip the first line.
506 if (lines.front().empty()) {
507 lines.erase(lines.begin());
508 }
509
510 // Now rejoin the lines and copy them back into doctext.
511 docstring.clear();
512 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
513 docstring += *l_iter;
514 docstring += '\n';
515 }
516
517 assert(docstring.length() <= strlen(doctext));
518 strcpy(doctext, docstring.c_str());
519 return doctext;
520}
521
522/** Set to true to debug docstring parsing */
523static bool dump_docs = false;
524
525/**
526 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000527 * Only works for top-level definitions and the whole program doc
528 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000529 */
530void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000531 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000532 if (!progdoc.empty()) {
533 printf("Whole program doc:\n%s\n", progdoc.c_str());
534 }
David Reiss1ac05802007-07-30 22:00:27 +0000535 const vector<t_typedef*>& typedefs = program->get_typedefs();
536 vector<t_typedef*>::const_iterator t_iter;
537 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
538 t_typedef* td = *t_iter;
539 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000540 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
541 }
542 }
543 const vector<t_enum*>& enums = program->get_enums();
544 vector<t_enum*>::const_iterator e_iter;
545 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
546 t_enum* en = *e_iter;
547 if (en->has_doc()) {
548 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
549 }
550 }
551 const vector<t_const*>& consts = program->get_consts();
552 vector<t_const*>::const_iterator c_iter;
553 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
554 t_const* co = *c_iter;
555 if (co->has_doc()) {
556 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
557 }
558 }
559 const vector<t_struct*>& structs = program->get_structs();
560 vector<t_struct*>::const_iterator s_iter;
561 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
562 t_struct* st = *s_iter;
563 if (st->has_doc()) {
564 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
565 }
566 }
567 const vector<t_struct*>& xceptions = program->get_xceptions();
568 vector<t_struct*>::const_iterator x_iter;
569 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
570 t_struct* xn = *x_iter;
571 if (xn->has_doc()) {
572 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
573 }
574 }
575 const vector<t_service*>& services = program->get_services();
576 vector<t_service*>::const_iterator v_iter;
577 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
578 t_service* sv = *v_iter;
579 if (sv->has_doc()) {
580 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000581 }
582 }
583}
584
585/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000586 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000587 */
588void generate_all_fingerprints(t_program* program) {
589 const vector<t_struct*>& structs = program->get_structs();
590 vector<t_struct*>::const_iterator s_iter;
591 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
592 t_struct* st = *s_iter;
593 st->generate_fingerprint();
594 }
595
David Reissd779cbe2007-08-31 01:42:55 +0000596 const vector<t_struct*>& xceptions = program->get_xceptions();
597 vector<t_struct*>::const_iterator x_iter;
598 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
599 t_struct* st = *x_iter;
600 st->generate_fingerprint();
601 }
602
David Reiss3c5d2fd2008-02-08 21:58:06 +0000603 const vector<t_enum*>& enums = program->get_enums();
604 vector<t_enum*>::const_iterator e_iter;
605 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
606 t_enum* e = *e_iter;
607 e->generate_fingerprint();
608 }
609
David Reiss47557bc2007-09-04 21:31:04 +0000610 g_type_void->generate_fingerprint();
611
David Reiss18bf22d2007-08-28 20:49:17 +0000612 // If you want to generate fingerprints for implicit structures, start here.
613 /*
614 const vector<t_service*>& services = program->get_services();
615 vector<t_service*>::const_iterator v_iter;
616 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
617 t_service* sv = *v_iter;
618 }
619 */
620}
621
622/**
David Reissdd08f6d2008-06-30 20:24:24 +0000623 * Prints the version number
624 */
625void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000626 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000627}
628
629/**
Mark Slee31985722006-05-24 21:45:31 +0000630 * Diplays the usage message and then exits with an error code.
631 */
632void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000633 fprintf(stderr, "Usage: thrift [options] file\n");
634 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000635 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000636 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
637 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000638 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
639 fprintf(stderr," (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000640 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000641 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000642 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
643 fprintf(stderr, " -strict Strict compiler warnings on\n");
644 fprintf(stderr, " -v[erbose] Verbose mode\n");
645 fprintf(stderr, " -r[ecurse] Also generate included files\n");
646 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000647 fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
648 "preserve protocol\n");
649 fprintf(stderr, " compatibility with older .thrift files)\n");
David Reissbd0db882008-02-27 01:54:51 +0000650 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
651 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
652 fprintf(stderr, " Keys and values are options passed to the generator.\n");
653 fprintf(stderr, " Many options will not require values.\n");
654 fprintf(stderr, "\n");
655 fprintf(stderr, "Available generators (and options):\n");
656
657 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
658 t_generator_registry::gen_map_t::iterator iter;
659 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
660 fprintf(stderr, " %s (%s):\n",
661 iter->second->get_short_name().c_str(),
662 iter->second->get_long_name().c_str());
663 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
664 }
Mark Slee31985722006-05-24 21:45:31 +0000665 exit(1);
666}
667
668/**
Mark Slee30152872006-11-28 01:24:07 +0000669 * You know, when I started working on Thrift I really thought it wasn't going
670 * to become a programming language because it was just a generator and it
671 * wouldn't need runtime type information and all that jazz. But then we
672 * decided to add constants, and all of a sudden that means runtime type
673 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000674 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000675 */
676void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
677 if (type->is_void()) {
678 throw "type error: cannot declare a void const: " + name;
679 }
680
681 if (type->is_base_type()) {
682 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
683 switch (tbase) {
684 case t_base_type::TYPE_STRING:
685 if (value->get_type() != t_const_value::CV_STRING) {
686 throw "type error: const \"" + name + "\" was declared as string";
687 }
688 break;
689 case t_base_type::TYPE_BOOL:
690 if (value->get_type() != t_const_value::CV_INTEGER) {
691 throw "type error: const \"" + name + "\" was declared as bool";
692 }
693 break;
694 case t_base_type::TYPE_BYTE:
695 if (value->get_type() != t_const_value::CV_INTEGER) {
696 throw "type error: const \"" + name + "\" was declared as byte";
697 }
698 break;
699 case t_base_type::TYPE_I16:
700 if (value->get_type() != t_const_value::CV_INTEGER) {
701 throw "type error: const \"" + name + "\" was declared as i16";
702 }
703 break;
704 case t_base_type::TYPE_I32:
705 if (value->get_type() != t_const_value::CV_INTEGER) {
706 throw "type error: const \"" + name + "\" was declared as i32";
707 }
708 break;
709 case t_base_type::TYPE_I64:
710 if (value->get_type() != t_const_value::CV_INTEGER) {
711 throw "type error: const \"" + name + "\" was declared as i64";
712 }
713 break;
714 case t_base_type::TYPE_DOUBLE:
715 if (value->get_type() != t_const_value::CV_INTEGER &&
716 value->get_type() != t_const_value::CV_DOUBLE) {
717 throw "type error: const \"" + name + "\" was declared as double";
718 }
719 break;
720 default:
David Reissdd7796f2007-08-28 21:09:06 +0000721 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000722 }
723 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000724 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000725 throw "type error: const \"" + name + "\" was declared as enum";
726 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000727
Bryan Duxbury1606f252010-11-24 00:25:57 +0000728 // see if there's a dot in the identifier
729 std::string name_portion = value->get_identifier_name();
730
Bryan Duxbury2d804702009-12-18 19:41:11 +0000731 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
732 vector<t_enum_value*>::const_iterator c_iter;
733 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000734
Bryan Duxbury1606f252010-11-24 00:25:57 +0000735 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000736 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000737 found = true;
738 break;
739 }
740 }
741 if (!found) {
742 throw "type error: const " + name + " was declared as type "
743 + type->get_name() + " which is an enum, but "
744 + value->get_identifier() + " is not a valid value for that enum";
745 }
Mark Slee30152872006-11-28 01:24:07 +0000746 } else if (type->is_struct() || type->is_xception()) {
747 if (value->get_type() != t_const_value::CV_MAP) {
748 throw "type error: const \"" + name + "\" was declared as struct/xception";
749 }
750 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
751 vector<t_field*>::const_iterator f_iter;
752
753 const map<t_const_value*, t_const_value*>& val = value->get_map();
754 map<t_const_value*, t_const_value*>::const_iterator v_iter;
755 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
756 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
757 throw "type error: " + name + " struct key must be string";
758 }
759 t_type* field_type = NULL;
760 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
761 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
762 field_type = (*f_iter)->get_type();
763 }
764 }
765 if (field_type == NULL) {
766 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
767 }
768
769 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
770 }
771 } else if (type->is_map()) {
772 t_type* k_type = ((t_map*)type)->get_key_type();
773 t_type* v_type = ((t_map*)type)->get_val_type();
774 const map<t_const_value*, t_const_value*>& val = value->get_map();
775 map<t_const_value*, t_const_value*>::const_iterator v_iter;
776 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
777 validate_const_rec(name + "<key>", k_type, v_iter->first);
778 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000779 }
Mark Slee30152872006-11-28 01:24:07 +0000780 } else if (type->is_list() || type->is_set()) {
781 t_type* e_type;
782 if (type->is_list()) {
783 e_type = ((t_list*)type)->get_elem_type();
784 } else {
785 e_type = ((t_set*)type)->get_elem_type();
786 }
787 const vector<t_const_value*>& val = value->get_list();
788 vector<t_const_value*>::const_iterator v_iter;
789 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
790 validate_const_rec(name + "<elem>", e_type, *v_iter);
791 }
792 }
793}
794
795/**
796 * Check the type of the parsed const information against its declared type
797 */
798void validate_const_type(t_const* c) {
799 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
800}
801
802/**
Mark Slee7ff32452007-02-01 05:26:18 +0000803 * Check the type of a default value assigned to a field.
804 */
805void validate_field_value(t_field* field, t_const_value* cv) {
806 validate_const_rec(field->get_name(), field->get_type(), cv);
807}
808
809/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000810 * Check that all the elements of a throws block are actually exceptions.
811 */
812bool validate_throws(t_struct* throws) {
813 const vector<t_field*>& members = throws->get_members();
814 vector<t_field*>::const_iterator m_iter;
815 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
816 if (!(*m_iter)->get_type()->is_xception()) {
817 return false;
818 }
819 }
820 return true;
821}
822
823/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000824 * Parses a program
825 */
Mark Slee2c44d202007-05-16 02:18:07 +0000826void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000827 // Get scope file path
828 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000829
Mark Sleef0712dc2006-10-25 19:03:57 +0000830 // Set current dir global, which is used in the include_file function
831 g_curdir = directory_name(path);
832 g_curpath = path;
833
834 // Open the file
835 yyin = fopen(path.c_str(), "r");
836 if (yyin == 0) {
837 failure("Could not open input file: \"%s\"", path.c_str());
838 }
839
840 // Create new scope and scan for includes
841 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000842 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000843 g_program = program;
844 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000845 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000846 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000847 if (yyparse() != 0) {
848 failure("Parser error during include pass.");
849 }
850 } catch (string x) {
851 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000852 }
853 fclose(yyin);
854
855 // Recursively parse all the include programs
856 vector<t_program*>& includes = program->get_includes();
857 vector<t_program*>::iterator iter;
858 for (iter = includes.begin(); iter != includes.end(); ++iter) {
859 parse(*iter, program);
860 }
861
David Reiss204420f2008-01-11 20:59:03 +0000862 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000863 g_parse_mode = PROGRAM;
864 g_program = program;
865 g_scope = program->scope();
866 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
867 g_parent_prefix = program->get_name() + ".";
868 g_curpath = path;
869 yyin = fopen(path.c_str(), "r");
870 if (yyin == 0) {
871 failure("Could not open input file: \"%s\"", path.c_str());
872 }
873 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000874 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000875 try {
876 if (yyparse() != 0) {
877 failure("Parser error during types pass.");
878 }
879 } catch (string x) {
880 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000881 }
882 fclose(yyin);
883}
884
885/**
886 * Generate code
887 */
David Reissbd0db882008-02-27 01:54:51 +0000888void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000889 // Oooohh, recursive code generation, hot!!
890 if (gen_recurse) {
891 const vector<t_program*>& includes = program->get_includes();
892 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000893 // Propogate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000894 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000895
David Reissbd0db882008-02-27 01:54:51 +0000896 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000897 }
898 }
899
900 // Generate code!
901 try {
902 pverbose("Program: %s\n", program->get_path().c_str());
903
David Reiss18bf22d2007-08-28 20:49:17 +0000904 // Compute fingerprints.
905 generate_all_fingerprints(program);
906
David Reiss1ac05802007-07-30 22:00:27 +0000907 if (dump_docs) {
908 dump_docstrings(program);
909 }
David Reissbd0db882008-02-27 01:54:51 +0000910
911 vector<string>::const_iterator iter;
912 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
913 t_generator* generator = t_generator_registry::get_generator(program, *iter);
914
915 if (generator == NULL) {
916 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
917 } else {
918 pverbose("Generating \"%s\"\n", iter->c_str());
919 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000920 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000921 }
922 }
923
Mark Sleef0712dc2006-10-25 19:03:57 +0000924 } catch (string s) {
925 printf("Error: %s\n", s.c_str());
926 } catch (const char* exc) {
927 printf("Error: %s\n", exc);
928 }
929
930}
931
932/**
Mark Sleef5377b32006-10-10 01:42:59 +0000933 * Parse it up.. then spit it back out, in pretty much every language. Alright
934 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000935 */
936int main(int argc, char** argv) {
937 int i;
dweatherford65b70752007-10-31 02:18:14 +0000938 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000939 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +0000940
Mark Sleeb15a68b2006-06-07 06:46:24 +0000941 // Setup time string
942 time_t now = time(NULL);
943 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000944
Mark Sleef0712dc2006-10-25 19:03:57 +0000945 // Check for necessary arguments, you gotta have at least a filename and
946 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000947 if (argc < 2) {
948 usage();
949 }
Mark Slee31985722006-05-24 21:45:31 +0000950
David Reissbd0db882008-02-27 01:54:51 +0000951 vector<string> generator_strings;
952
David Reiss9cc2c132008-02-27 01:54:47 +0000953 // Set the current path to a dummy value to make warning messages clearer.
954 g_curpath = "arguments";
955
Mark Sleef5377b32006-10-10 01:42:59 +0000956 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000957 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000958 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000959
Mark Sleefdbee812006-09-27 18:50:48 +0000960 arg = strtok(argv[i], " ");
961 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000962 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000963 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000964 ++arg;
965 }
966
David Reissdd08f6d2008-06-30 20:24:24 +0000967 if (strcmp(arg, "-version") == 0) {
968 version();
969 exit(1);
970 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000971 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000972 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000973 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000974 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000975 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +0000976 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000977 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000978 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000979 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000980 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000981 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
982 g_allow_neg_field_keys = true;
David Reissbd0db882008-02-27 01:54:51 +0000983 } else if (strcmp(arg, "-gen") == 0) {
984 arg = argv[++i];
985 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000986 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000987 usage();
988 }
989 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000990 } else if (strcmp(arg, "-dense") == 0) {
991 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000992 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000993 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000994 } else if (strcmp(arg, "-javabean") == 0) {
995 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000996 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000997 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000998 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000999 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001000 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001001 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001002 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001003 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001004 gen_phps = true;
1005 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001006 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001007 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001008 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001009 gen_php = true;
1010 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001011 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001012 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001013 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001014 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001015 } else if (strcmp(arg, "-rest") == 0) {
1016 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001017 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001018 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001019 } else if (strcmp(arg, "-pyns") == 0) {
1020 gen_py = true;
1021 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001022 } else if (strcmp(arg, "-rb") == 0) {
1023 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001024 } else if (strcmp(arg, "-xsd") == 0) {
1025 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001026 } else if (strcmp(arg, "-perl") == 0) {
1027 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +00001028 } else if (strcmp(arg, "-erl") == 0) {
1029 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001030 } else if (strcmp(arg, "-ocaml") == 0) {
1031 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001032 } else if (strcmp(arg, "-hs") == 0) {
1033 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001034 } else if (strcmp(arg, "-cocoa") == 0) {
1035 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001036 } else if (strcmp(arg, "-st") == 0) {
1037 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001038 } else if (strcmp(arg, "-csharp") == 0) {
1039 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001040 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1041 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001042 } else if (strcmp(arg, "-I") == 0) {
1043 // An argument of "-I\ asdf" is invalid and has unknown results
1044 arg = argv[++i];
1045
1046 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001047 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001048 usage();
1049 }
1050 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001051 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1052 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
1053
1054 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001055 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001056 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001057 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001058 }
dweatherford65b70752007-10-31 02:18:14 +00001059 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001060
1061#ifdef MINGW
1062 //strip out trailing \ on Windows
1063 int last = out_path.length()-1;
1064 if (out_path[last] == '\\')
1065 {
1066 out_path.erase(last);
1067 }
1068#endif
1069
dweatherford65b70752007-10-31 02:18:14 +00001070 struct stat sb;
1071 if (stat(out_path.c_str(), &sb) < 0) {
1072 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1073 return -1;
1074 }
1075 if (! S_ISDIR(sb.st_mode)) {
1076 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1077 return -1;
1078 }
Mark Sleefdbee812006-09-27 18:50:48 +00001079 } else {
1080 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1081 usage();
1082 }
1083
1084 // Tokenize more
1085 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001086 }
1087 }
Mark Slee2c44d202007-05-16 02:18:07 +00001088
David Reissdd08f6d2008-06-30 20:24:24 +00001089 // if you're asking for version, you have a right not to pass a file
1090 if (strcmp(argv[argc-1], "-version") == 0) {
1091 version();
1092 exit(1);
1093 }
1094
David Reiss400cd4b2008-02-27 01:54:55 +00001095 // TODO(dreiss): Delete these when everyone is using the new hotness.
1096 if (gen_cpp) {
1097 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1098 string gen_string = "cpp:";
1099 if (gen_dense) {
1100 gen_string.append("dense,");
1101 }
1102 if (g_cpp_use_include_prefix) {
1103 gen_string.append("include_prefix,");
1104 }
1105 generator_strings.push_back(gen_string);
1106 }
David Reisse1404d22008-02-27 01:55:05 +00001107 if (gen_java) {
1108 pwarning(1, "-java is deprecated. Use --gen java");
1109 generator_strings.push_back("java");
1110 }
1111 if (gen_javabean) {
1112 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1113 generator_strings.push_back("java:beans");
1114 }
David Reiss861869b2008-03-27 21:41:23 +00001115 if (gen_csharp) {
1116 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1117 generator_strings.push_back("csharp");
1118 }
David Reiss558e3992008-03-27 21:41:40 +00001119 if (gen_py) {
1120 pwarning(1, "-py is deprecated. Use --gen py");
1121 generator_strings.push_back("py");
1122 }
David Reissa640cea2008-03-27 21:42:01 +00001123 if (gen_rb) {
1124 pwarning(1, "-rb is deprecated. Use --gen rb");
1125 generator_strings.push_back("rb");
1126 }
David Reiss2b386c52008-03-27 21:42:23 +00001127 if (gen_perl) {
1128 pwarning(1, "-perl is deprecated. Use --gen perl");
1129 generator_strings.push_back("perl");
1130 }
David Reissa9ea68b2009-02-17 20:28:24 +00001131 if (gen_php || gen_phpi) {
1132 pwarning(1, "-php is deprecated. Use --gen php");
1133 string gen_string = "php:";
1134 if (gen_phpi) {
1135 gen_string.append("inlined,");
1136 } else if(gen_phps) {
1137 gen_string.append("server,");
1138 } else if(gen_phpa) {
1139 gen_string.append("autoload,");
1140 } else if(gen_phpo) {
1141 gen_string.append("oop,");
1142 } else if(gen_rest) {
1143 gen_string.append("rest,");
1144 }
1145 generator_strings.push_back(gen_string);
1146 }
David Reissd01c64d2008-03-27 21:40:55 +00001147 if (gen_cocoa) {
1148 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1149 generator_strings.push_back("cocoa");
1150 }
David Reissa2047832009-02-17 20:27:54 +00001151 if (gen_erl) {
1152 pwarning(1, "-erl is deprecated. Use --gen erl");
1153 generator_strings.push_back("erl");
1154 }
David Reissa890b572008-03-27 21:40:35 +00001155 if (gen_st) {
1156 pwarning(1, "-st is deprecated. Use --gen st");
1157 generator_strings.push_back("st");
1158 }
David Reissf0521b12008-03-27 21:40:05 +00001159 if (gen_ocaml) {
1160 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1161 generator_strings.push_back("ocaml");
1162 }
David Reissaf3ab262008-03-27 21:40:16 +00001163 if (gen_hs) {
1164 pwarning(1, "-hs is deprecated. Use --gen hs");
1165 generator_strings.push_back("hs");
1166 }
David Reiss4ba67102009-02-17 20:28:06 +00001167 if (gen_xsd) {
1168 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1169 generator_strings.push_back("xsd");
1170 }
David Reisse1404d22008-02-27 01:55:05 +00001171
Mark Sleef0712dc2006-10-25 19:03:57 +00001172 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001173 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001174 fprintf(stderr, "!!! No output language(s) specified\n\n");
1175 usage();
1176 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001177
1178 // Real-pathify it
1179 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001180 if (argv[i] == NULL) {
1181 fprintf(stderr, "!!! Missing file name\n");
1182 usage();
1183 }
David Reiss204420f2008-01-11 20:59:03 +00001184 if (saferealpath(argv[i], rp) == NULL) {
1185 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001186 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001187 string input_file(rp);
1188
Mark Sleef5377b32006-10-10 01:42:59 +00001189 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001190 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001191 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001192 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001193 }
kholst76f2c882008-01-16 02:47:41 +00001194
David Reiss4b6a3c72008-02-27 22:28:12 +00001195 // Compute the cpp include prefix.
1196 // infer this from the filename passed in
1197 string input_filename = argv[i];
1198 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001199
David Reiss4b6a3c72008-02-27 22:28:12 +00001200 string::size_type last_slash = string::npos;
1201 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1202 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001203 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001204
David Reiss4b6a3c72008-02-27 22:28:12 +00001205 program->set_include_prefix(include_prefix);
1206
Mark Sleef0712dc2006-10-25 19:03:57 +00001207 // Initialize global types
1208 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1209 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001210 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1211 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001212 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1213 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001214 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1215 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1216 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1217 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1218 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1219 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001220
Mark Sleef5377b32006-10-10 01:42:59 +00001221 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001222 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001223
David Reiss9cc2c132008-02-27 01:54:47 +00001224 // The current path is not really relevant when we are doing generation.
1225 // Reset the variable to make warning messages clearer.
1226 g_curpath = "generation";
1227 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1228 // That is what shows up during argument parsing.
1229 yylineno = 1;
1230
Mark Sleef0712dc2006-10-25 19:03:57 +00001231 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001232 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001233
Mark Sleef0712dc2006-10-25 19:03:57 +00001234 // Clean up. Who am I kidding... this program probably orphans heap memory
1235 // all over the place, but who cares because it is about to exit and it is
1236 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001237
Mark Sleef0712dc2006-10-25 19:03:57 +00001238 delete program;
1239 delete g_type_void;
1240 delete g_type_string;
1241 delete g_type_bool;
1242 delete g_type_byte;
1243 delete g_type_i16;
1244 delete g_type_i32;
1245 delete g_type_i64;
1246 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001247
1248 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001249 return 0;
1250}