blob: c12b31abce6633d2a13dd1dc264e7b7f34afa566 [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/**
Roger Meier887ff752011-08-19 11:25:39 +0000160 * Whether or not 64-bit constants will generate a warning.
161 */
162int g_allow_64bit_consts = 0;
163
164/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000165 * Flags to control code generation
166 */
167bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000168bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000169bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000170bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000171bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000172bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000173bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000174bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000175bool gen_php = false;
176bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000177bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000178bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000179bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000180bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000181bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000182bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000183bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000184bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000185bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000186bool gen_csharp = false;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000187bool gen_delphi = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000188bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000189bool gen_recurse = false;
190
191/**
David Reiss204420f2008-01-11 20:59:03 +0000192 * MinGW doesn't have realpath, so use fallback implementation in that case,
193 * otherwise this just calls through to realpath
194 */
195char *saferealpath(const char *path, char *resolved_path) {
196#ifdef MINGW
197 char buf[MAX_PATH];
198 char* basename;
199 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
200 if (len == 0 || len > MAX_PATH - 1){
201 strcpy(resolved_path, path);
202 } else {
David Reiss204420f2008-01-11 20:59:03 +0000203 strcpy(resolved_path, buf);
204 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000205
206 // Replace backslashes with forward slashes so the
207 // rest of the code behaves correctly.
208 size_t resolved_len = strlen(resolved_path);
209 for (size_t i = 0; i < resolved_len; i++) {
210 if (resolved_path[i] == '\\') {
211 resolved_path[i] = '/';
212 }
213 }
David Reiss204420f2008-01-11 20:59:03 +0000214 return resolved_path;
215#else
216 return realpath(path, resolved_path);
217#endif
218}
219
220
221/**
Mark Slee31985722006-05-24 21:45:31 +0000222 * Report an error to the user. This is called yyerror for historical
223 * reasons (lex and yacc expect the error reporting routine to be called
224 * this). Call this function to report any errors to the user.
225 * yyerror takes printf style arguments.
226 *
227 * @param fmt C format string followed by additional arguments
228 */
David Reiss0babe402008-06-10 22:56:12 +0000229void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000230 va_list args;
231 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 "[ERROR:%s:%d] (last token was '%s')\n",
233 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000234 yylineno,
235 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000236
237 va_start(args, fmt);
238 vfprintf(stderr, fmt, args);
239 va_end(args);
240
241 fprintf(stderr, "\n");
242}
243
244/**
245 * Prints a debug message from the parser.
246 *
247 * @param fmt C format string followed by additional arguments
248 */
David Reiss0babe402008-06-10 22:56:12 +0000249void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000250 if (g_debug == 0) {
251 return;
252 }
253 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000254 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000255 va_start(args, fmt);
256 vprintf(fmt, args);
257 va_end(args);
258 printf("\n");
259}
260
261/**
262 * Prints a verbose output mode message
263 *
264 * @param fmt C format string followed by additional arguments
265 */
David Reiss0babe402008-06-10 22:56:12 +0000266void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000267 if (g_verbose == 0) {
268 return;
269 }
270 va_list args;
271 va_start(args, fmt);
272 vprintf(fmt, args);
273 va_end(args);
274}
275
276/**
277 * Prints a warning message
278 *
279 * @param fmt C format string followed by additional arguments
280 */
David Reiss0babe402008-06-10 22:56:12 +0000281void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000282 if (g_warn < level) {
283 return;
284 }
285 va_list args;
286 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000287 va_start(args, fmt);
288 vprintf(fmt, args);
289 va_end(args);
290 printf("\n");
291}
292
293/**
294 * Prints a failure message and exits
295 *
296 * @param fmt C format string followed by additional arguments
297 */
Mark Slee30152872006-11-28 01:24:07 +0000298void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000299 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000300 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000301 va_start(args, fmt);
302 vfprintf(stderr, fmt, args);
303 va_end(args);
304 printf("\n");
305 exit(1);
306}
307
308/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000309 * Converts a string filename into a thrift program name
310 */
311string program_name(string filename) {
312 string::size_type slash = filename.rfind("/");
313 if (slash != string::npos) {
314 filename = filename.substr(slash+1);
315 }
316 string::size_type dot = filename.rfind(".");
317 if (dot != string::npos) {
318 filename = filename.substr(0, dot);
319 }
320 return filename;
321}
322
323/**
324 * Gets the directory path of a filename
325 */
326string directory_name(string filename) {
327 string::size_type slash = filename.rfind("/");
328 // No slash, just use the current directory
329 if (slash == string::npos) {
330 return ".";
331 }
332 return filename.substr(0, slash);
333}
334
335/**
336 * Finds the appropriate file path for the given filename
337 */
338string include_file(string filename) {
339 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000340 if (filename[0] == '/') {
341 // Realpath!
342 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000343 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000344 pwarning(0, "Cannot open include file %s\n", filename.c_str());
345 return std::string();
346 }
Mark Slee2c44d202007-05-16 02:18:07 +0000347
348 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000349 struct stat finfo;
350 if (stat(rp, &finfo) == 0) {
351 return rp;
352 }
353 } else { // relative path, start searching
354 // new search path with current dir global
355 vector<string> sp = g_incl_searchpath;
356 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000357
Martin Kraemer32c66e12006-11-09 00:06:36 +0000358 // iterate through paths
359 vector<string>::iterator it;
360 for (it = sp.begin(); it != sp.end(); it++) {
361 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000362
Martin Kraemer32c66e12006-11-09 00:06:36 +0000363 // Realpath!
364 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000365 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000366 continue;
367 }
Mark Slee2c44d202007-05-16 02:18:07 +0000368
Martin Kraemer32c66e12006-11-09 00:06:36 +0000369 // Stat this files
370 struct stat finfo;
371 if (stat(rp, &finfo) == 0) {
372 return rp;
373 }
374 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000375 }
Mark Slee2c44d202007-05-16 02:18:07 +0000376
Mark Sleef0712dc2006-10-25 19:03:57 +0000377 // Uh oh
378 pwarning(0, "Could not find include file %s\n", filename.c_str());
379 return std::string();
380}
381
382/**
David Reisscbd4bac2007-08-14 17:12:33 +0000383 * Clears any previously stored doctext string.
384 * Also prints a warning if we are discarding information.
385 */
386void clear_doctext() {
387 if (g_doctext != NULL) {
388 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
389 }
390 free(g_doctext);
391 g_doctext = NULL;
392}
393
394/**
David Reiss1ac05802007-07-30 22:00:27 +0000395 * Cleans up text commonly found in doxygen-like comments
396 *
397 * Warning: if you mix tabs and spaces in a non-uniform way,
398 * you will get what you deserve.
399 */
400char* clean_up_doctext(char* doctext) {
401 // Convert to C++ string, and remove Windows's carriage returns.
402 string docstring = doctext;
403 docstring.erase(
404 remove(docstring.begin(), docstring.end(), '\r'),
405 docstring.end());
406
407 // Separate into lines.
408 vector<string> lines;
409 string::size_type pos = string::npos;
410 string::size_type last;
411 while (true) {
412 last = (pos == string::npos) ? 0 : pos+1;
413 pos = docstring.find('\n', last);
414 if (pos == string::npos) {
415 // First bit of cleaning. If the last line is only whitespace, drop it.
416 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
417 if (nonwhite != string::npos) {
418 lines.push_back(docstring.substr(last));
419 }
420 break;
421 }
422 lines.push_back(docstring.substr(last, pos-last));
423 }
424
425 // A very profound docstring.
426 if (lines.empty()) {
427 return NULL;
428 }
429
430 // Clear leading whitespace from the first line.
431 pos = lines.front().find_first_not_of(" \t");
432 lines.front().erase(0, pos);
433
434 // If every nonblank line after the first has the same number of spaces/tabs,
435 // then a star, remove them.
436 bool have_prefix = true;
437 bool found_prefix = false;
438 string::size_type prefix_len = 0;
439 vector<string>::iterator l_iter;
440 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
441 if (l_iter->empty()) {
442 continue;
443 }
444
445 pos = l_iter->find_first_not_of(" \t");
446 if (!found_prefix) {
447 if (pos != string::npos) {
448 if (l_iter->at(pos) == '*') {
449 found_prefix = true;
450 prefix_len = pos;
451 } else {
452 have_prefix = false;
453 break;
454 }
455 } else {
456 // Whitespace-only line. Truncate it.
457 l_iter->clear();
458 }
459 } else if (l_iter->size() > pos
460 && l_iter->at(pos) == '*'
461 && pos == prefix_len) {
462 // Business as usual.
463 } else if (pos == string::npos) {
464 // Whitespace-only line. Let's truncate it for them.
465 l_iter->clear();
466 } else {
467 // The pattern has been broken.
468 have_prefix = false;
469 break;
470 }
471 }
472
473 // If our prefix survived, delete it from every line.
474 if (have_prefix) {
475 // Get the star too.
476 prefix_len++;
477 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
478 l_iter->erase(0, prefix_len);
479 }
480 }
481
482 // Now delete the minimum amount of leading whitespace from each line.
483 prefix_len = string::npos;
484 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
485 if (l_iter->empty()) {
486 continue;
487 }
488 pos = l_iter->find_first_not_of(" \t");
489 if (pos != string::npos
490 && (prefix_len == string::npos || pos < prefix_len)) {
491 prefix_len = pos;
492 }
493 }
494
495 // If our prefix survived, delete it from every line.
496 if (prefix_len != string::npos) {
497 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
498 l_iter->erase(0, prefix_len);
499 }
500 }
501
502 // Remove trailing whitespace from every line.
503 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
504 pos = l_iter->find_last_not_of(" \t");
505 if (pos != string::npos && pos != l_iter->length()-1) {
506 l_iter->erase(pos+1);
507 }
508 }
509
510 // If the first line is empty, remove it.
511 // Don't do this earlier because a lot of steps skip the first line.
512 if (lines.front().empty()) {
513 lines.erase(lines.begin());
514 }
515
516 // Now rejoin the lines and copy them back into doctext.
517 docstring.clear();
518 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
519 docstring += *l_iter;
520 docstring += '\n';
521 }
522
523 assert(docstring.length() <= strlen(doctext));
524 strcpy(doctext, docstring.c_str());
525 return doctext;
526}
527
528/** Set to true to debug docstring parsing */
529static bool dump_docs = false;
530
531/**
532 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000533 * Only works for top-level definitions and the whole program doc
534 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000535 */
536void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000537 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000538 if (!progdoc.empty()) {
539 printf("Whole program doc:\n%s\n", progdoc.c_str());
540 }
David Reiss1ac05802007-07-30 22:00:27 +0000541 const vector<t_typedef*>& typedefs = program->get_typedefs();
542 vector<t_typedef*>::const_iterator t_iter;
543 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
544 t_typedef* td = *t_iter;
545 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000546 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
547 }
548 }
549 const vector<t_enum*>& enums = program->get_enums();
550 vector<t_enum*>::const_iterator e_iter;
551 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
552 t_enum* en = *e_iter;
553 if (en->has_doc()) {
554 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
555 }
556 }
557 const vector<t_const*>& consts = program->get_consts();
558 vector<t_const*>::const_iterator c_iter;
559 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
560 t_const* co = *c_iter;
561 if (co->has_doc()) {
562 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
563 }
564 }
565 const vector<t_struct*>& structs = program->get_structs();
566 vector<t_struct*>::const_iterator s_iter;
567 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
568 t_struct* st = *s_iter;
569 if (st->has_doc()) {
570 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
571 }
572 }
573 const vector<t_struct*>& xceptions = program->get_xceptions();
574 vector<t_struct*>::const_iterator x_iter;
575 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
576 t_struct* xn = *x_iter;
577 if (xn->has_doc()) {
578 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
579 }
580 }
581 const vector<t_service*>& services = program->get_services();
582 vector<t_service*>::const_iterator v_iter;
583 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
584 t_service* sv = *v_iter;
585 if (sv->has_doc()) {
586 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000587 }
588 }
589}
590
591/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000592 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000593 */
594void generate_all_fingerprints(t_program* program) {
595 const vector<t_struct*>& structs = program->get_structs();
596 vector<t_struct*>::const_iterator s_iter;
597 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
598 t_struct* st = *s_iter;
599 st->generate_fingerprint();
600 }
601
David Reissd779cbe2007-08-31 01:42:55 +0000602 const vector<t_struct*>& xceptions = program->get_xceptions();
603 vector<t_struct*>::const_iterator x_iter;
604 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
605 t_struct* st = *x_iter;
606 st->generate_fingerprint();
607 }
608
David Reiss3c5d2fd2008-02-08 21:58:06 +0000609 const vector<t_enum*>& enums = program->get_enums();
610 vector<t_enum*>::const_iterator e_iter;
611 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
612 t_enum* e = *e_iter;
613 e->generate_fingerprint();
614 }
615
David Reiss47557bc2007-09-04 21:31:04 +0000616 g_type_void->generate_fingerprint();
617
David Reiss18bf22d2007-08-28 20:49:17 +0000618 // If you want to generate fingerprints for implicit structures, start here.
619 /*
620 const vector<t_service*>& services = program->get_services();
621 vector<t_service*>::const_iterator v_iter;
622 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
623 t_service* sv = *v_iter;
624 }
625 */
626}
627
628/**
David Reissdd08f6d2008-06-30 20:24:24 +0000629 * Prints the version number
630 */
631void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000632 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000633}
634
635/**
Mark Slee31985722006-05-24 21:45:31 +0000636 * Diplays the usage message and then exits with an error code.
637 */
638void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000639 fprintf(stderr, "Usage: thrift [options] file\n");
640 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000641 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000642 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
643 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000644 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
645 fprintf(stderr," (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000646 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000647 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000648 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
649 fprintf(stderr, " -strict Strict compiler warnings on\n");
650 fprintf(stderr, " -v[erbose] Verbose mode\n");
651 fprintf(stderr, " -r[ecurse] Also generate included files\n");
652 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000653 fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
654 "preserve protocol\n");
655 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000656 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000657 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
658 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
659 fprintf(stderr, " Keys and values are options passed to the generator.\n");
660 fprintf(stderr, " Many options will not require values.\n");
661 fprintf(stderr, "\n");
662 fprintf(stderr, "Available generators (and options):\n");
663
664 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
665 t_generator_registry::gen_map_t::iterator iter;
666 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
667 fprintf(stderr, " %s (%s):\n",
668 iter->second->get_short_name().c_str(),
669 iter->second->get_long_name().c_str());
670 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
671 }
Mark Slee31985722006-05-24 21:45:31 +0000672 exit(1);
673}
674
675/**
Mark Slee30152872006-11-28 01:24:07 +0000676 * You know, when I started working on Thrift I really thought it wasn't going
677 * to become a programming language because it was just a generator and it
678 * wouldn't need runtime type information and all that jazz. But then we
679 * decided to add constants, and all of a sudden that means runtime type
680 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000681 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000682 */
683void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
684 if (type->is_void()) {
685 throw "type error: cannot declare a void const: " + name;
686 }
687
688 if (type->is_base_type()) {
689 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
690 switch (tbase) {
691 case t_base_type::TYPE_STRING:
692 if (value->get_type() != t_const_value::CV_STRING) {
693 throw "type error: const \"" + name + "\" was declared as string";
694 }
695 break;
696 case t_base_type::TYPE_BOOL:
697 if (value->get_type() != t_const_value::CV_INTEGER) {
698 throw "type error: const \"" + name + "\" was declared as bool";
699 }
700 break;
701 case t_base_type::TYPE_BYTE:
702 if (value->get_type() != t_const_value::CV_INTEGER) {
703 throw "type error: const \"" + name + "\" was declared as byte";
704 }
705 break;
706 case t_base_type::TYPE_I16:
707 if (value->get_type() != t_const_value::CV_INTEGER) {
708 throw "type error: const \"" + name + "\" was declared as i16";
709 }
710 break;
711 case t_base_type::TYPE_I32:
712 if (value->get_type() != t_const_value::CV_INTEGER) {
713 throw "type error: const \"" + name + "\" was declared as i32";
714 }
715 break;
716 case t_base_type::TYPE_I64:
717 if (value->get_type() != t_const_value::CV_INTEGER) {
718 throw "type error: const \"" + name + "\" was declared as i64";
719 }
720 break;
721 case t_base_type::TYPE_DOUBLE:
722 if (value->get_type() != t_const_value::CV_INTEGER &&
723 value->get_type() != t_const_value::CV_DOUBLE) {
724 throw "type error: const \"" + name + "\" was declared as double";
725 }
726 break;
727 default:
David Reissdd7796f2007-08-28 21:09:06 +0000728 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000729 }
730 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000731 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000732 throw "type error: const \"" + name + "\" was declared as enum";
733 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000734
Bryan Duxbury1606f252010-11-24 00:25:57 +0000735 // see if there's a dot in the identifier
736 std::string name_portion = value->get_identifier_name();
737
Bryan Duxbury2d804702009-12-18 19:41:11 +0000738 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
739 vector<t_enum_value*>::const_iterator c_iter;
740 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000741
Bryan Duxbury1606f252010-11-24 00:25:57 +0000742 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000743 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000744 found = true;
745 break;
746 }
747 }
748 if (!found) {
749 throw "type error: const " + name + " was declared as type "
750 + type->get_name() + " which is an enum, but "
751 + value->get_identifier() + " is not a valid value for that enum";
752 }
Mark Slee30152872006-11-28 01:24:07 +0000753 } else if (type->is_struct() || type->is_xception()) {
754 if (value->get_type() != t_const_value::CV_MAP) {
755 throw "type error: const \"" + name + "\" was declared as struct/xception";
756 }
757 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
758 vector<t_field*>::const_iterator f_iter;
759
760 const map<t_const_value*, t_const_value*>& val = value->get_map();
761 map<t_const_value*, t_const_value*>::const_iterator v_iter;
762 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
763 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
764 throw "type error: " + name + " struct key must be string";
765 }
766 t_type* field_type = NULL;
767 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
768 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
769 field_type = (*f_iter)->get_type();
770 }
771 }
772 if (field_type == NULL) {
773 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
774 }
775
776 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
777 }
778 } else if (type->is_map()) {
779 t_type* k_type = ((t_map*)type)->get_key_type();
780 t_type* v_type = ((t_map*)type)->get_val_type();
781 const map<t_const_value*, t_const_value*>& val = value->get_map();
782 map<t_const_value*, t_const_value*>::const_iterator v_iter;
783 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
784 validate_const_rec(name + "<key>", k_type, v_iter->first);
785 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000786 }
Mark Slee30152872006-11-28 01:24:07 +0000787 } else if (type->is_list() || type->is_set()) {
788 t_type* e_type;
789 if (type->is_list()) {
790 e_type = ((t_list*)type)->get_elem_type();
791 } else {
792 e_type = ((t_set*)type)->get_elem_type();
793 }
794 const vector<t_const_value*>& val = value->get_list();
795 vector<t_const_value*>::const_iterator v_iter;
796 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
797 validate_const_rec(name + "<elem>", e_type, *v_iter);
798 }
799 }
800}
801
802/**
803 * Check the type of the parsed const information against its declared type
804 */
805void validate_const_type(t_const* c) {
806 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
807}
808
809/**
Mark Slee7ff32452007-02-01 05:26:18 +0000810 * Check the type of a default value assigned to a field.
811 */
812void validate_field_value(t_field* field, t_const_value* cv) {
813 validate_const_rec(field->get_name(), field->get_type(), cv);
814}
815
816/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000817 * Check that all the elements of a throws block are actually exceptions.
818 */
819bool validate_throws(t_struct* throws) {
820 const vector<t_field*>& members = throws->get_members();
821 vector<t_field*>::const_iterator m_iter;
822 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000823 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000824 return false;
825 }
826 }
827 return true;
828}
829
830/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000831 * Parses a program
832 */
Mark Slee2c44d202007-05-16 02:18:07 +0000833void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000834 // Get scope file path
835 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000836
Mark Sleef0712dc2006-10-25 19:03:57 +0000837 // Set current dir global, which is used in the include_file function
838 g_curdir = directory_name(path);
839 g_curpath = path;
840
841 // Open the file
842 yyin = fopen(path.c_str(), "r");
843 if (yyin == 0) {
844 failure("Could not open input file: \"%s\"", path.c_str());
845 }
846
847 // Create new scope and scan for includes
848 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000849 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 g_program = program;
851 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000852 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000853 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000854 if (yyparse() != 0) {
855 failure("Parser error during include pass.");
856 }
857 } catch (string x) {
858 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000859 }
860 fclose(yyin);
861
862 // Recursively parse all the include programs
863 vector<t_program*>& includes = program->get_includes();
864 vector<t_program*>::iterator iter;
865 for (iter = includes.begin(); iter != includes.end(); ++iter) {
866 parse(*iter, program);
867 }
868
David Reiss204420f2008-01-11 20:59:03 +0000869 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000870 g_parse_mode = PROGRAM;
871 g_program = program;
872 g_scope = program->scope();
873 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
874 g_parent_prefix = program->get_name() + ".";
875 g_curpath = path;
876 yyin = fopen(path.c_str(), "r");
877 if (yyin == 0) {
878 failure("Could not open input file: \"%s\"", path.c_str());
879 }
880 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000881 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000882 try {
883 if (yyparse() != 0) {
884 failure("Parser error during types pass.");
885 }
886 } catch (string x) {
887 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000888 }
889 fclose(yyin);
890}
891
892/**
893 * Generate code
894 */
David Reissbd0db882008-02-27 01:54:51 +0000895void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000896 // Oooohh, recursive code generation, hot!!
897 if (gen_recurse) {
898 const vector<t_program*>& includes = program->get_includes();
899 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000900 // Propogate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000901 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000902
David Reissbd0db882008-02-27 01:54:51 +0000903 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000904 }
905 }
906
907 // Generate code!
908 try {
909 pverbose("Program: %s\n", program->get_path().c_str());
910
David Reiss18bf22d2007-08-28 20:49:17 +0000911 // Compute fingerprints.
912 generate_all_fingerprints(program);
913
David Reiss1ac05802007-07-30 22:00:27 +0000914 if (dump_docs) {
915 dump_docstrings(program);
916 }
David Reissbd0db882008-02-27 01:54:51 +0000917
918 vector<string>::const_iterator iter;
919 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
920 t_generator* generator = t_generator_registry::get_generator(program, *iter);
921
922 if (generator == NULL) {
923 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
924 } else {
925 pverbose("Generating \"%s\"\n", iter->c_str());
926 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000927 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000928 }
929 }
930
Mark Sleef0712dc2006-10-25 19:03:57 +0000931 } catch (string s) {
932 printf("Error: %s\n", s.c_str());
933 } catch (const char* exc) {
934 printf("Error: %s\n", exc);
935 }
936
937}
938
939/**
Mark Sleef5377b32006-10-10 01:42:59 +0000940 * Parse it up.. then spit it back out, in pretty much every language. Alright
941 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000942 */
943int main(int argc, char** argv) {
944 int i;
dweatherford65b70752007-10-31 02:18:14 +0000945 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000946 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +0000947
Mark Sleeb15a68b2006-06-07 06:46:24 +0000948 // Setup time string
949 time_t now = time(NULL);
950 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000951
Mark Sleef0712dc2006-10-25 19:03:57 +0000952 // Check for necessary arguments, you gotta have at least a filename and
953 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000954 if (argc < 2) {
955 usage();
956 }
Mark Slee31985722006-05-24 21:45:31 +0000957
David Reissbd0db882008-02-27 01:54:51 +0000958 vector<string> generator_strings;
959
David Reiss9cc2c132008-02-27 01:54:47 +0000960 // Set the current path to a dummy value to make warning messages clearer.
961 g_curpath = "arguments";
962
Mark Sleef5377b32006-10-10 01:42:59 +0000963 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000964 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000965 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000966
Mark Sleefdbee812006-09-27 18:50:48 +0000967 arg = strtok(argv[i], " ");
968 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000969 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000970 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000971 ++arg;
972 }
973
David Reissdd08f6d2008-06-30 20:24:24 +0000974 if (strcmp(arg, "-version") == 0) {
975 version();
976 exit(1);
977 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000978 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000979 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000980 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000981 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000982 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +0000983 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000984 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000985 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000986 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000987 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000988 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
989 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +0000990 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
991 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +0000992 } else if (strcmp(arg, "-gen") == 0) {
993 arg = argv[++i];
994 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000995 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000996 usage();
997 }
998 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000999 } else if (strcmp(arg, "-dense") == 0) {
1000 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +00001001 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001002 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +00001003 } else if (strcmp(arg, "-javabean") == 0) {
1004 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +00001005 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001006 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +00001007 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001008 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +00001009 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +00001010 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +00001011 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001012 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001013 gen_phps = true;
1014 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001015 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +00001016 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001017 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001018 gen_php = true;
1019 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001020 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001021 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001022 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001023 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001024 } else if (strcmp(arg, "-rest") == 0) {
1025 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001026 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001027 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001028 } else if (strcmp(arg, "-pyns") == 0) {
1029 gen_py = true;
1030 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001031 } else if (strcmp(arg, "-rb") == 0) {
1032 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001033 } else if (strcmp(arg, "-xsd") == 0) {
1034 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001035 } else if (strcmp(arg, "-perl") == 0) {
1036 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +00001037 } else if (strcmp(arg, "-erl") == 0) {
1038 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001039 } else if (strcmp(arg, "-ocaml") == 0) {
1040 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001041 } else if (strcmp(arg, "-hs") == 0) {
1042 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001043 } else if (strcmp(arg, "-cocoa") == 0) {
1044 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001045 } else if (strcmp(arg, "-st") == 0) {
1046 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001047 } else if (strcmp(arg, "-csharp") == 0) {
1048 gen_csharp = true;
Jake Farrell7ae13e12011-10-18 14:35:26 +00001049 } else if (strcmp(arg, "-delphi") == 0) {
1050 gen_delphi = true;
kholst76f2c882008-01-16 02:47:41 +00001051 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1052 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001053 } else if (strcmp(arg, "-I") == 0) {
1054 // An argument of "-I\ asdf" is invalid and has unknown results
1055 arg = argv[++i];
1056
1057 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001058 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001059 usage();
1060 }
1061 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001062 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1063 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
1064
1065 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001066 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001067 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001068 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001069 }
dweatherford65b70752007-10-31 02:18:14 +00001070 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001071
1072#ifdef MINGW
1073 //strip out trailing \ on Windows
1074 int last = out_path.length()-1;
1075 if (out_path[last] == '\\')
1076 {
1077 out_path.erase(last);
1078 }
1079#endif
1080
dweatherford65b70752007-10-31 02:18:14 +00001081 struct stat sb;
1082 if (stat(out_path.c_str(), &sb) < 0) {
1083 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1084 return -1;
1085 }
1086 if (! S_ISDIR(sb.st_mode)) {
1087 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1088 return -1;
1089 }
Mark Sleefdbee812006-09-27 18:50:48 +00001090 } else {
1091 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1092 usage();
1093 }
1094
1095 // Tokenize more
1096 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001097 }
1098 }
Mark Slee2c44d202007-05-16 02:18:07 +00001099
David Reissdd08f6d2008-06-30 20:24:24 +00001100 // if you're asking for version, you have a right not to pass a file
1101 if (strcmp(argv[argc-1], "-version") == 0) {
1102 version();
1103 exit(1);
1104 }
1105
David Reiss400cd4b2008-02-27 01:54:55 +00001106 // TODO(dreiss): Delete these when everyone is using the new hotness.
1107 if (gen_cpp) {
1108 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1109 string gen_string = "cpp:";
1110 if (gen_dense) {
1111 gen_string.append("dense,");
1112 }
1113 if (g_cpp_use_include_prefix) {
1114 gen_string.append("include_prefix,");
1115 }
1116 generator_strings.push_back(gen_string);
1117 }
David Reisse1404d22008-02-27 01:55:05 +00001118 if (gen_java) {
1119 pwarning(1, "-java is deprecated. Use --gen java");
1120 generator_strings.push_back("java");
1121 }
1122 if (gen_javabean) {
1123 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1124 generator_strings.push_back("java:beans");
1125 }
David Reiss861869b2008-03-27 21:41:23 +00001126 if (gen_csharp) {
1127 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1128 generator_strings.push_back("csharp");
1129 }
Jake Farrell7ae13e12011-10-18 14:35:26 +00001130 if (gen_delphi) {
1131 pwarning(1, "-delphi is deprecated. Use --gen delphi");
1132 generator_strings.push_back("delphi");
1133 }
David Reiss558e3992008-03-27 21:41:40 +00001134 if (gen_py) {
1135 pwarning(1, "-py is deprecated. Use --gen py");
1136 generator_strings.push_back("py");
1137 }
David Reissa640cea2008-03-27 21:42:01 +00001138 if (gen_rb) {
1139 pwarning(1, "-rb is deprecated. Use --gen rb");
1140 generator_strings.push_back("rb");
1141 }
David Reiss2b386c52008-03-27 21:42:23 +00001142 if (gen_perl) {
1143 pwarning(1, "-perl is deprecated. Use --gen perl");
1144 generator_strings.push_back("perl");
1145 }
David Reissa9ea68b2009-02-17 20:28:24 +00001146 if (gen_php || gen_phpi) {
1147 pwarning(1, "-php is deprecated. Use --gen php");
1148 string gen_string = "php:";
1149 if (gen_phpi) {
1150 gen_string.append("inlined,");
1151 } else if(gen_phps) {
1152 gen_string.append("server,");
1153 } else if(gen_phpa) {
1154 gen_string.append("autoload,");
1155 } else if(gen_phpo) {
1156 gen_string.append("oop,");
1157 } else if(gen_rest) {
1158 gen_string.append("rest,");
1159 }
1160 generator_strings.push_back(gen_string);
1161 }
David Reissd01c64d2008-03-27 21:40:55 +00001162 if (gen_cocoa) {
1163 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1164 generator_strings.push_back("cocoa");
1165 }
David Reissa2047832009-02-17 20:27:54 +00001166 if (gen_erl) {
1167 pwarning(1, "-erl is deprecated. Use --gen erl");
1168 generator_strings.push_back("erl");
1169 }
David Reissa890b572008-03-27 21:40:35 +00001170 if (gen_st) {
1171 pwarning(1, "-st is deprecated. Use --gen st");
1172 generator_strings.push_back("st");
1173 }
David Reissf0521b12008-03-27 21:40:05 +00001174 if (gen_ocaml) {
1175 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1176 generator_strings.push_back("ocaml");
1177 }
David Reissaf3ab262008-03-27 21:40:16 +00001178 if (gen_hs) {
1179 pwarning(1, "-hs is deprecated. Use --gen hs");
1180 generator_strings.push_back("hs");
1181 }
David Reiss4ba67102009-02-17 20:28:06 +00001182 if (gen_xsd) {
1183 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1184 generator_strings.push_back("xsd");
1185 }
David Reisse1404d22008-02-27 01:55:05 +00001186
Mark Sleef0712dc2006-10-25 19:03:57 +00001187 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001188 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001189 fprintf(stderr, "!!! No output language(s) specified\n\n");
1190 usage();
1191 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001192
1193 // Real-pathify it
1194 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001195 if (argv[i] == NULL) {
1196 fprintf(stderr, "!!! Missing file name\n");
1197 usage();
1198 }
David Reiss204420f2008-01-11 20:59:03 +00001199 if (saferealpath(argv[i], rp) == NULL) {
1200 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001201 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001202 string input_file(rp);
1203
Mark Sleef5377b32006-10-10 01:42:59 +00001204 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001205 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001206 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001207 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001208 }
kholst76f2c882008-01-16 02:47:41 +00001209
David Reiss4b6a3c72008-02-27 22:28:12 +00001210 // Compute the cpp include prefix.
1211 // infer this from the filename passed in
1212 string input_filename = argv[i];
1213 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001214
David Reiss4b6a3c72008-02-27 22:28:12 +00001215 string::size_type last_slash = string::npos;
1216 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1217 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001218 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001219
David Reiss4b6a3c72008-02-27 22:28:12 +00001220 program->set_include_prefix(include_prefix);
1221
Mark Sleef0712dc2006-10-25 19:03:57 +00001222 // Initialize global types
1223 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1224 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001225 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1226 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001227 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1228 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001229 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1230 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1231 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1232 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1233 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1234 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001235
Mark Sleef5377b32006-10-10 01:42:59 +00001236 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001237 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001238
David Reiss9cc2c132008-02-27 01:54:47 +00001239 // The current path is not really relevant when we are doing generation.
1240 // Reset the variable to make warning messages clearer.
1241 g_curpath = "generation";
1242 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1243 // That is what shows up during argument parsing.
1244 yylineno = 1;
1245
Mark Sleef0712dc2006-10-25 19:03:57 +00001246 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001247 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001248
Mark Sleef0712dc2006-10-25 19:03:57 +00001249 // Clean up. Who am I kidding... this program probably orphans heap memory
1250 // all over the place, but who cares because it is about to exit and it is
1251 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001252
Mark Sleef0712dc2006-10-25 19:03:57 +00001253 delete program;
1254 delete g_type_void;
1255 delete g_type_string;
1256 delete g_type_bool;
1257 delete g_type_byte;
1258 delete g_type_i16;
1259 delete g_type_i32;
1260 delete g_type_i64;
1261 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001262
1263 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001264 return 0;
1265}