blob: 4ce22b464d3f9602c2cf73c4056f25b1419f003f [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
Ben Craig282e4402013-10-08 16:02:06 -050042#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +000043# 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
Ben Craig282e4402013-10-08 16:02:06 -050049#include "platform.h"
Mark Slee31985722006-05-24 21:45:31 +000050#include "main.h"
51#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000052#include "parse/t_scope.h"
David Reissbbbbe882009-02-17 20:27:48 +000053#include "generate/t_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000054
David Reissdd08f6d2008-06-30 20:24:24 +000055#include "version.h"
56
Mark Slee31985722006-05-24 21:45:31 +000057using namespace std;
58
Mark Sleef5377b32006-10-10 01:42:59 +000059/**
60 * Global program tree
61 */
Mark Slee31985722006-05-24 21:45:31 +000062t_program* g_program;
63
Mark Sleef5377b32006-10-10 01:42:59 +000064/**
Mark Sleef0712dc2006-10-25 19:03:57 +000065 * Global types
66 */
67
68t_type* g_type_void;
69t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000070t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000071t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000072t_type* g_type_bool;
73t_type* g_type_byte;
74t_type* g_type_i16;
75t_type* g_type_i32;
76t_type* g_type_i64;
77t_type* g_type_double;
78
79/**
80 * Global scope
81 */
82t_scope* g_scope;
83
84/**
85 * Parent scope to also parse types
86 */
87t_scope* g_parent_scope;
88
89/**
90 * Prefix for putting types in parent scope
91 */
92string g_parent_prefix;
93
94/**
95 * Parsing pass
96 */
97PARSE_MODE g_parse_mode;
98
99/**
100 * Current directory of file being parsed
101 */
102string g_curdir;
103
104/**
105 * Current file being parsed
106 */
107string g_curpath;
108
109/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000110 * Search path for inclusions
111 */
Mark Slee2329a832006-11-09 00:23:30 +0000112vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000113
114/**
Mark Sleef5377b32006-10-10 01:42:59 +0000115 * Global debug state
116 */
Mark Slee31985722006-05-24 21:45:31 +0000117int g_debug = 0;
118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000120 * Strictness level
121 */
122int g_strict = 127;
123
124/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000125 * Warning level
126 */
127int g_warn = 1;
128
129/**
130 * Verbose output
131 */
132int g_verbose = 0;
133
134/**
Mark Sleef5377b32006-10-10 01:42:59 +0000135 * Global time string
136 */
Mark Slee31985722006-05-24 21:45:31 +0000137char* g_time_str;
138
Mark Slee31985722006-05-24 21:45:31 +0000139/**
David Reisscbd4bac2007-08-14 17:12:33 +0000140 * The last parsed doctext comment.
141 */
142char* g_doctext;
143
144/**
145 * The location of the last parsed doctext comment.
146 */
147int g_doctext_lineno;
148
149/**
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000150 * Whether or not negative field keys are accepted.
151 */
152int g_allow_neg_field_keys;
153
154/**
Roger Meier887ff752011-08-19 11:25:39 +0000155 * Whether or not 64-bit constants will generate a warning.
156 */
157int g_allow_64bit_consts = 0;
158
159/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000160 * Flags to control code generation
161 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000162bool gen_recurse = false;
163
164/**
Ben Craig282e4402013-10-08 16:02:06 -0500165 * Win32 doesn't have realpath, so use fallback implementation in that case,
David Reiss204420f2008-01-11 20:59:03 +0000166 * otherwise this just calls through to realpath
167 */
168char *saferealpath(const char *path, char *resolved_path) {
Ben Craig282e4402013-10-08 16:02:06 -0500169#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +0000170 char buf[MAX_PATH];
171 char* basename;
172 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
173 if (len == 0 || len > MAX_PATH - 1){
174 strcpy(resolved_path, path);
175 } else {
David Reiss204420f2008-01-11 20:59:03 +0000176 strcpy(resolved_path, buf);
177 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000178
179 // Replace backslashes with forward slashes so the
180 // rest of the code behaves correctly.
181 size_t resolved_len = strlen(resolved_path);
182 for (size_t i = 0; i < resolved_len; i++) {
183 if (resolved_path[i] == '\\') {
184 resolved_path[i] = '/';
185 }
186 }
David Reiss204420f2008-01-11 20:59:03 +0000187 return resolved_path;
188#else
189 return realpath(path, resolved_path);
190#endif
191}
192
Roger Meier061d4a22012-10-07 11:51:00 +0000193bool check_is_directory(const char *dir_name) {
Ben Craig282e4402013-10-08 16:02:06 -0500194#ifdef _WIN32
Roger Meier061d4a22012-10-07 11:51:00 +0000195 DWORD attributes = ::GetFileAttributesA(dir_name);
196 if(attributes == INVALID_FILE_ATTRIBUTES) {
197 fprintf(stderr, "Output directory %s is unusable: GetLastError() = %ld\n", dir_name, GetLastError());
198 return false;
199 }
200 if((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
201 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
202 return false;
203 }
204 return true;
205#else
206 struct stat sb;
207 if (stat(dir_name, &sb) < 0) {
208 fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
209 return false;
210 }
211 if (! S_ISDIR(sb.st_mode)) {
212 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
213 return false;
214 }
215 return true;
216#endif
217}
David Reiss204420f2008-01-11 20:59:03 +0000218
219/**
Mark Slee31985722006-05-24 21:45:31 +0000220 * Report an error to the user. This is called yyerror for historical
221 * reasons (lex and yacc expect the error reporting routine to be called
222 * this). Call this function to report any errors to the user.
223 * yyerror takes printf style arguments.
224 *
225 * @param fmt C format string followed by additional arguments
226 */
David Reiss0babe402008-06-10 22:56:12 +0000227void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000228 va_list args;
229 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000230 "[ERROR:%s:%d] (last token was '%s')\n",
231 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000232 yylineno,
233 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000234
235 va_start(args, fmt);
236 vfprintf(stderr, fmt, args);
237 va_end(args);
238
239 fprintf(stderr, "\n");
240}
241
242/**
243 * Prints a debug message from the parser.
244 *
245 * @param fmt C format string followed by additional arguments
246 */
David Reiss0babe402008-06-10 22:56:12 +0000247void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000248 if (g_debug == 0) {
249 return;
250 }
251 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000252 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000253 va_start(args, fmt);
254 vprintf(fmt, args);
255 va_end(args);
256 printf("\n");
257}
258
259/**
260 * Prints a verbose output mode message
261 *
262 * @param fmt C format string followed by additional arguments
263 */
David Reiss0babe402008-06-10 22:56:12 +0000264void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000265 if (g_verbose == 0) {
266 return;
267 }
268 va_list args;
269 va_start(args, fmt);
270 vprintf(fmt, args);
271 va_end(args);
272}
273
274/**
275 * Prints a warning message
276 *
277 * @param fmt C format string followed by additional arguments
278 */
David Reiss0babe402008-06-10 22:56:12 +0000279void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000280 if (g_warn < level) {
281 return;
282 }
283 va_list args;
284 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000285 va_start(args, fmt);
286 vprintf(fmt, args);
287 va_end(args);
288 printf("\n");
289}
290
291/**
292 * Prints a failure message and exits
293 *
294 * @param fmt C format string followed by additional arguments
295 */
Mark Slee30152872006-11-28 01:24:07 +0000296void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000297 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000298 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000299 va_start(args, fmt);
300 vfprintf(stderr, fmt, args);
301 va_end(args);
302 printf("\n");
303 exit(1);
304}
305
306/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000307 * Converts a string filename into a thrift program name
308 */
309string program_name(string filename) {
310 string::size_type slash = filename.rfind("/");
311 if (slash != string::npos) {
312 filename = filename.substr(slash+1);
313 }
314 string::size_type dot = filename.rfind(".");
315 if (dot != string::npos) {
316 filename = filename.substr(0, dot);
317 }
318 return filename;
319}
320
321/**
322 * Gets the directory path of a filename
323 */
324string directory_name(string filename) {
325 string::size_type slash = filename.rfind("/");
326 // No slash, just use the current directory
327 if (slash == string::npos) {
328 return ".";
329 }
330 return filename.substr(0, slash);
331}
332
333/**
334 * Finds the appropriate file path for the given filename
335 */
336string include_file(string filename) {
337 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000338 if (filename[0] == '/') {
339 // Realpath!
Ben Craig282e4402013-10-08 16:02:06 -0500340 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000341 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000342 pwarning(0, "Cannot open include file %s\n", filename.c_str());
343 return std::string();
344 }
Mark Slee2c44d202007-05-16 02:18:07 +0000345
346 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000347 struct stat finfo;
348 if (stat(rp, &finfo) == 0) {
349 return rp;
350 }
351 } else { // relative path, start searching
352 // new search path with current dir global
353 vector<string> sp = g_incl_searchpath;
354 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000355
Martin Kraemer32c66e12006-11-09 00:06:36 +0000356 // iterate through paths
357 vector<string>::iterator it;
358 for (it = sp.begin(); it != sp.end(); it++) {
359 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000360
Martin Kraemer32c66e12006-11-09 00:06:36 +0000361 // Realpath!
Ben Craig282e4402013-10-08 16:02:06 -0500362 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000363 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000364 continue;
365 }
Mark Slee2c44d202007-05-16 02:18:07 +0000366
Martin Kraemer32c66e12006-11-09 00:06:36 +0000367 // Stat this files
368 struct stat finfo;
369 if (stat(rp, &finfo) == 0) {
370 return rp;
371 }
372 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000373 }
Mark Slee2c44d202007-05-16 02:18:07 +0000374
Mark Sleef0712dc2006-10-25 19:03:57 +0000375 // Uh oh
376 pwarning(0, "Could not find include file %s\n", filename.c_str());
377 return std::string();
378}
379
380/**
David Reisscbd4bac2007-08-14 17:12:33 +0000381 * Clears any previously stored doctext string.
382 * Also prints a warning if we are discarding information.
383 */
384void clear_doctext() {
385 if (g_doctext != NULL) {
386 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
387 }
388 free(g_doctext);
389 g_doctext = NULL;
390}
391
392/**
David Reiss1ac05802007-07-30 22:00:27 +0000393 * Cleans up text commonly found in doxygen-like comments
394 *
395 * Warning: if you mix tabs and spaces in a non-uniform way,
396 * you will get what you deserve.
397 */
398char* clean_up_doctext(char* doctext) {
399 // Convert to C++ string, and remove Windows's carriage returns.
400 string docstring = doctext;
401 docstring.erase(
402 remove(docstring.begin(), docstring.end(), '\r'),
403 docstring.end());
404
405 // Separate into lines.
406 vector<string> lines;
407 string::size_type pos = string::npos;
408 string::size_type last;
409 while (true) {
410 last = (pos == string::npos) ? 0 : pos+1;
411 pos = docstring.find('\n', last);
412 if (pos == string::npos) {
413 // First bit of cleaning. If the last line is only whitespace, drop it.
414 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
415 if (nonwhite != string::npos) {
416 lines.push_back(docstring.substr(last));
417 }
418 break;
419 }
420 lines.push_back(docstring.substr(last, pos-last));
421 }
422
423 // A very profound docstring.
424 if (lines.empty()) {
425 return NULL;
426 }
427
428 // Clear leading whitespace from the first line.
429 pos = lines.front().find_first_not_of(" \t");
430 lines.front().erase(0, pos);
431
432 // If every nonblank line after the first has the same number of spaces/tabs,
433 // then a star, remove them.
434 bool have_prefix = true;
435 bool found_prefix = false;
436 string::size_type prefix_len = 0;
437 vector<string>::iterator l_iter;
438 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
439 if (l_iter->empty()) {
440 continue;
441 }
442
443 pos = l_iter->find_first_not_of(" \t");
444 if (!found_prefix) {
445 if (pos != string::npos) {
446 if (l_iter->at(pos) == '*') {
447 found_prefix = true;
448 prefix_len = pos;
449 } else {
450 have_prefix = false;
451 break;
452 }
453 } else {
454 // Whitespace-only line. Truncate it.
455 l_iter->clear();
456 }
457 } else if (l_iter->size() > pos
458 && l_iter->at(pos) == '*'
459 && pos == prefix_len) {
460 // Business as usual.
461 } else if (pos == string::npos) {
462 // Whitespace-only line. Let's truncate it for them.
463 l_iter->clear();
464 } else {
465 // The pattern has been broken.
466 have_prefix = false;
467 break;
468 }
469 }
470
471 // If our prefix survived, delete it from every line.
472 if (have_prefix) {
473 // Get the star too.
474 prefix_len++;
475 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
476 l_iter->erase(0, prefix_len);
477 }
478 }
479
480 // Now delete the minimum amount of leading whitespace from each line.
481 prefix_len = string::npos;
482 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
483 if (l_iter->empty()) {
484 continue;
485 }
486 pos = l_iter->find_first_not_of(" \t");
487 if (pos != string::npos
488 && (prefix_len == string::npos || pos < prefix_len)) {
489 prefix_len = pos;
490 }
491 }
492
493 // If our prefix survived, delete it from every line.
494 if (prefix_len != string::npos) {
495 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
496 l_iter->erase(0, prefix_len);
497 }
498 }
499
500 // Remove trailing whitespace from every line.
501 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
502 pos = l_iter->find_last_not_of(" \t");
503 if (pos != string::npos && pos != l_iter->length()-1) {
504 l_iter->erase(pos+1);
505 }
506 }
507
508 // If the first line is empty, remove it.
509 // Don't do this earlier because a lot of steps skip the first line.
510 if (lines.front().empty()) {
511 lines.erase(lines.begin());
512 }
513
514 // Now rejoin the lines and copy them back into doctext.
515 docstring.clear();
516 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
517 docstring += *l_iter;
518 docstring += '\n';
519 }
520
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200521 //assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
522 if(docstring.length() <= strlen(doctext)) {
523 strcpy(doctext, docstring.c_str());
524 } else {
525 free(doctext); // too short
526 doctext = strdup(docstring.c_str());
527 }
David Reiss1ac05802007-07-30 22:00:27 +0000528 return doctext;
529}
530
531/** Set to true to debug docstring parsing */
532static bool dump_docs = false;
533
534/**
535 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000536 * Only works for top-level definitions and the whole program doc
537 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000538 */
539void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000540 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000541 if (!progdoc.empty()) {
542 printf("Whole program doc:\n%s\n", progdoc.c_str());
543 }
David Reiss1ac05802007-07-30 22:00:27 +0000544 const vector<t_typedef*>& typedefs = program->get_typedefs();
545 vector<t_typedef*>::const_iterator t_iter;
546 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
547 t_typedef* td = *t_iter;
548 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000549 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
550 }
551 }
552 const vector<t_enum*>& enums = program->get_enums();
553 vector<t_enum*>::const_iterator e_iter;
554 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
555 t_enum* en = *e_iter;
556 if (en->has_doc()) {
557 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
558 }
559 }
560 const vector<t_const*>& consts = program->get_consts();
561 vector<t_const*>::const_iterator c_iter;
562 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
563 t_const* co = *c_iter;
564 if (co->has_doc()) {
565 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
566 }
567 }
568 const vector<t_struct*>& structs = program->get_structs();
569 vector<t_struct*>::const_iterator s_iter;
570 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
571 t_struct* st = *s_iter;
572 if (st->has_doc()) {
573 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
574 }
575 }
576 const vector<t_struct*>& xceptions = program->get_xceptions();
577 vector<t_struct*>::const_iterator x_iter;
578 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
579 t_struct* xn = *x_iter;
580 if (xn->has_doc()) {
581 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
582 }
583 }
584 const vector<t_service*>& services = program->get_services();
585 vector<t_service*>::const_iterator v_iter;
586 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
587 t_service* sv = *v_iter;
588 if (sv->has_doc()) {
589 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000590 }
591 }
592}
593
594/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000595 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000596 */
597void generate_all_fingerprints(t_program* program) {
598 const vector<t_struct*>& structs = program->get_structs();
599 vector<t_struct*>::const_iterator s_iter;
600 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
601 t_struct* st = *s_iter;
602 st->generate_fingerprint();
603 }
604
David Reissd779cbe2007-08-31 01:42:55 +0000605 const vector<t_struct*>& xceptions = program->get_xceptions();
606 vector<t_struct*>::const_iterator x_iter;
607 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
608 t_struct* st = *x_iter;
609 st->generate_fingerprint();
610 }
611
David Reiss3c5d2fd2008-02-08 21:58:06 +0000612 const vector<t_enum*>& enums = program->get_enums();
613 vector<t_enum*>::const_iterator e_iter;
614 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
615 t_enum* e = *e_iter;
616 e->generate_fingerprint();
617 }
618
David Reiss47557bc2007-09-04 21:31:04 +0000619 g_type_void->generate_fingerprint();
620
David Reiss18bf22d2007-08-28 20:49:17 +0000621 // If you want to generate fingerprints for implicit structures, start here.
622 /*
623 const vector<t_service*>& services = program->get_services();
624 vector<t_service*>::const_iterator v_iter;
625 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
626 t_service* sv = *v_iter;
627 }
628 */
629}
630
631/**
David Reissdd08f6d2008-06-30 20:24:24 +0000632 * Prints the version number
633 */
634void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000635 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000636}
637
638/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000639 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000640 */
641void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000642 fprintf(stderr, "Usage: thrift [options] file\n\n");
643 fprintf(stderr, "Use thrift -help for a list of options\n");
644 exit(1);
645}
646
647/**
648 * Diplays the help message and then exits with an error code.
649 */
650void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000651 fprintf(stderr, "Usage: thrift [options] file\n");
652 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000653 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000654 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
655 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000656 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
657 fprintf(stderr," (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000658 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000659 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000660 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
661 fprintf(stderr, " -strict Strict compiler warnings on\n");
662 fprintf(stderr, " -v[erbose] Verbose mode\n");
663 fprintf(stderr, " -r[ecurse] Also generate included files\n");
664 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000665 fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
666 "preserve protocol\n");
667 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000668 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000669 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
670 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
671 fprintf(stderr, " Keys and values are options passed to the generator.\n");
672 fprintf(stderr, " Many options will not require values.\n");
673 fprintf(stderr, "\n");
674 fprintf(stderr, "Available generators (and options):\n");
675
676 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
677 t_generator_registry::gen_map_t::iterator iter;
678 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
679 fprintf(stderr, " %s (%s):\n",
680 iter->second->get_short_name().c_str(),
681 iter->second->get_long_name().c_str());
682 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
683 }
Mark Slee31985722006-05-24 21:45:31 +0000684 exit(1);
685}
686
687/**
Mark Slee30152872006-11-28 01:24:07 +0000688 * You know, when I started working on Thrift I really thought it wasn't going
689 * to become a programming language because it was just a generator and it
690 * wouldn't need runtime type information and all that jazz. But then we
691 * decided to add constants, and all of a sudden that means runtime type
692 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000693 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000694 */
695void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
696 if (type->is_void()) {
697 throw "type error: cannot declare a void const: " + name;
698 }
699
700 if (type->is_base_type()) {
701 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
702 switch (tbase) {
703 case t_base_type::TYPE_STRING:
704 if (value->get_type() != t_const_value::CV_STRING) {
705 throw "type error: const \"" + name + "\" was declared as string";
706 }
707 break;
708 case t_base_type::TYPE_BOOL:
709 if (value->get_type() != t_const_value::CV_INTEGER) {
710 throw "type error: const \"" + name + "\" was declared as bool";
711 }
712 break;
713 case t_base_type::TYPE_BYTE:
714 if (value->get_type() != t_const_value::CV_INTEGER) {
715 throw "type error: const \"" + name + "\" was declared as byte";
716 }
717 break;
718 case t_base_type::TYPE_I16:
719 if (value->get_type() != t_const_value::CV_INTEGER) {
720 throw "type error: const \"" + name + "\" was declared as i16";
721 }
722 break;
723 case t_base_type::TYPE_I32:
724 if (value->get_type() != t_const_value::CV_INTEGER) {
725 throw "type error: const \"" + name + "\" was declared as i32";
726 }
727 break;
728 case t_base_type::TYPE_I64:
729 if (value->get_type() != t_const_value::CV_INTEGER) {
730 throw "type error: const \"" + name + "\" was declared as i64";
731 }
732 break;
733 case t_base_type::TYPE_DOUBLE:
734 if (value->get_type() != t_const_value::CV_INTEGER &&
735 value->get_type() != t_const_value::CV_DOUBLE) {
736 throw "type error: const \"" + name + "\" was declared as double";
737 }
738 break;
739 default:
David Reissdd7796f2007-08-28 21:09:06 +0000740 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000741 }
742 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000743 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000744 throw "type error: const \"" + name + "\" was declared as enum";
745 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000746
Bryan Duxbury1606f252010-11-24 00:25:57 +0000747 // see if there's a dot in the identifier
748 std::string name_portion = value->get_identifier_name();
749
Bryan Duxbury2d804702009-12-18 19:41:11 +0000750 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
751 vector<t_enum_value*>::const_iterator c_iter;
752 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000753
Bryan Duxbury1606f252010-11-24 00:25:57 +0000754 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000755 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000756 found = true;
757 break;
758 }
759 }
760 if (!found) {
Ben Craig282e4402013-10-08 16:02:06 -0500761 throw "type error: const " + name + " was declared as type "
762 + type->get_name() + " which is an enum, but "
Bryan Duxbury2d804702009-12-18 19:41:11 +0000763 + value->get_identifier() + " is not a valid value for that enum";
764 }
Mark Slee30152872006-11-28 01:24:07 +0000765 } else if (type->is_struct() || type->is_xception()) {
766 if (value->get_type() != t_const_value::CV_MAP) {
767 throw "type error: const \"" + name + "\" was declared as struct/xception";
768 }
769 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
770 vector<t_field*>::const_iterator f_iter;
771
772 const map<t_const_value*, t_const_value*>& val = value->get_map();
773 map<t_const_value*, t_const_value*>::const_iterator v_iter;
774 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
775 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
776 throw "type error: " + name + " struct key must be string";
777 }
778 t_type* field_type = NULL;
779 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
780 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
781 field_type = (*f_iter)->get_type();
782 }
783 }
784 if (field_type == NULL) {
785 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
786 }
787
788 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
789 }
790 } else if (type->is_map()) {
791 t_type* k_type = ((t_map*)type)->get_key_type();
792 t_type* v_type = ((t_map*)type)->get_val_type();
793 const map<t_const_value*, t_const_value*>& val = value->get_map();
794 map<t_const_value*, t_const_value*>::const_iterator v_iter;
795 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
796 validate_const_rec(name + "<key>", k_type, v_iter->first);
797 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000798 }
Mark Slee30152872006-11-28 01:24:07 +0000799 } else if (type->is_list() || type->is_set()) {
800 t_type* e_type;
801 if (type->is_list()) {
802 e_type = ((t_list*)type)->get_elem_type();
803 } else {
804 e_type = ((t_set*)type)->get_elem_type();
805 }
806 const vector<t_const_value*>& val = value->get_list();
807 vector<t_const_value*>::const_iterator v_iter;
808 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
809 validate_const_rec(name + "<elem>", e_type, *v_iter);
810 }
811 }
812}
813
814/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200815 * Check simple identifier names
816 * It's easier to do it this way instead of rewriting the whole grammar etc.
817 */
818void validate_simple_identifier(const char* identifier) {
819 string name( identifier);
820 if( name.find(".") != string::npos) {
821 yyerror("Identifier %s can't have a dot.", identifier);
822 exit(1);
823 }
824}
825
826/**
Mark Slee30152872006-11-28 01:24:07 +0000827 * Check the type of the parsed const information against its declared type
828 */
829void validate_const_type(t_const* c) {
830 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
831}
832
833/**
Mark Slee7ff32452007-02-01 05:26:18 +0000834 * Check the type of a default value assigned to a field.
835 */
836void validate_field_value(t_field* field, t_const_value* cv) {
837 validate_const_rec(field->get_name(), field->get_type(), cv);
838}
839
840/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000841 * Check that all the elements of a throws block are actually exceptions.
842 */
843bool validate_throws(t_struct* throws) {
844 const vector<t_field*>& members = throws->get_members();
845 vector<t_field*>::const_iterator m_iter;
846 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000847 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000848 return false;
849 }
850 }
851 return true;
852}
853
854/**
Jens Geyer03d49442013-09-04 22:34:41 +0200855 * Skips UTF-8 BOM if there is one
856 */
857bool skip_utf8_bom(FILE* f) {
858
859 // pretty straightforward, but works
860 if( fgetc(f) == 0xEF) {
861 if( fgetc(f) == 0xBB) {
862 if( fgetc(f) == 0xBF) {
863 return true;
864 }
865 }
866 }
867
868 rewind(f);
869 return false;
870}
871
872/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000873 * Parses a program
874 */
Mark Slee2c44d202007-05-16 02:18:07 +0000875void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000876 // Get scope file path
877 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000878
Mark Sleef0712dc2006-10-25 19:03:57 +0000879 // Set current dir global, which is used in the include_file function
880 g_curdir = directory_name(path);
881 g_curpath = path;
882
883 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200884 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000885 yyin = fopen(path.c_str(), "r");
886 if (yyin == 0) {
887 failure("Could not open input file: \"%s\"", path.c_str());
888 }
Jens Geyer03d49442013-09-04 22:34:41 +0200889 if( skip_utf8_bom( yyin))
890 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
891
Mark Sleef0712dc2006-10-25 19:03:57 +0000892 // Create new scope and scan for includes
893 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000894 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000895 g_program = program;
896 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000897 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000898 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000899 if (yyparse() != 0) {
900 failure("Parser error during include pass.");
901 }
902 } catch (string x) {
903 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000904 }
905 fclose(yyin);
906
907 // Recursively parse all the include programs
908 vector<t_program*>& includes = program->get_includes();
909 vector<t_program*>::iterator iter;
910 for (iter = includes.begin(); iter != includes.end(); ++iter) {
911 parse(*iter, program);
912 }
913
David Reiss204420f2008-01-11 20:59:03 +0000914 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000915 g_parse_mode = PROGRAM;
916 g_program = program;
917 g_scope = program->scope();
918 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
919 g_parent_prefix = program->get_name() + ".";
920 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200921
922 // Open the file
923 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000924 yyin = fopen(path.c_str(), "r");
925 if (yyin == 0) {
926 failure("Could not open input file: \"%s\"", path.c_str());
927 }
Jens Geyer03d49442013-09-04 22:34:41 +0200928 if( skip_utf8_bom( yyin))
929 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
930
Mark Sleef0712dc2006-10-25 19:03:57 +0000931 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000932 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000933 try {
934 if (yyparse() != 0) {
935 failure("Parser error during types pass.");
936 }
937 } catch (string x) {
938 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 }
940 fclose(yyin);
941}
942
943/**
944 * Generate code
945 */
David Reissbd0db882008-02-27 01:54:51 +0000946void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000947 // Oooohh, recursive code generation, hot!!
948 if (gen_recurse) {
949 const vector<t_program*>& includes = program->get_includes();
950 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000951 // Propogate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000952 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000953
David Reissbd0db882008-02-27 01:54:51 +0000954 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000955 }
956 }
957
958 // Generate code!
959 try {
960 pverbose("Program: %s\n", program->get_path().c_str());
961
Jens Geyer83767a72013-09-23 22:09:12 +0200962 // Compute fingerprints. - not anymore, we do it on the fly now
963 //generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +0000964
David Reiss1ac05802007-07-30 22:00:27 +0000965 if (dump_docs) {
966 dump_docstrings(program);
967 }
David Reissbd0db882008-02-27 01:54:51 +0000968
969 vector<string>::const_iterator iter;
970 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
971 t_generator* generator = t_generator_registry::get_generator(program, *iter);
972
973 if (generator == NULL) {
974 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
975 } else {
976 pverbose("Generating \"%s\"\n", iter->c_str());
977 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000978 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000979 }
980 }
981
Mark Sleef0712dc2006-10-25 19:03:57 +0000982 } catch (string s) {
983 printf("Error: %s\n", s.c_str());
984 } catch (const char* exc) {
985 printf("Error: %s\n", exc);
986 }
987
988}
989
990/**
Mark Sleef5377b32006-10-10 01:42:59 +0000991 * Parse it up.. then spit it back out, in pretty much every language. Alright
992 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000993 */
994int main(int argc, char** argv) {
995 int i;
dweatherford65b70752007-10-31 02:18:14 +0000996 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000997 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +0000998
Mark Sleeb15a68b2006-06-07 06:46:24 +0000999 // Setup time string
1000 time_t now = time(NULL);
1001 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001002
Mark Sleef0712dc2006-10-25 19:03:57 +00001003 // Check for necessary arguments, you gotta have at least a filename and
1004 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001005 if (argc < 2) {
1006 usage();
1007 }
Mark Slee31985722006-05-24 21:45:31 +00001008
David Reissbd0db882008-02-27 01:54:51 +00001009 vector<string> generator_strings;
1010
David Reiss9cc2c132008-02-27 01:54:47 +00001011 // Set the current path to a dummy value to make warning messages clearer.
1012 g_curpath = "arguments";
1013
Mark Sleef5377b32006-10-10 01:42:59 +00001014 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +00001015 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001016 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001017
Mark Sleefdbee812006-09-27 18:50:48 +00001018 arg = strtok(argv[i], " ");
1019 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001020 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001021 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001022 ++arg;
1023 }
1024
Jake Farrell2fd8a152012-09-29 00:26:36 +00001025 if (strcmp(arg, "-help") == 0) {
1026 help();
1027 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001028 version();
jfarrell70969422013-09-09 20:33:38 -04001029 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001030 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001031 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001032 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001033 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001034 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001035 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001036 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001037 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001038 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001039 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001040 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001041 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1042 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001043 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1044 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001045 } else if (strcmp(arg, "-gen") == 0) {
1046 arg = argv[++i];
1047 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001048 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001049 usage();
1050 }
1051 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001052 } else if (strcmp(arg, "-I") == 0) {
1053 // An argument of "-I\ asdf" is invalid and has unknown results
1054 arg = argv[++i];
1055
1056 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001057 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001058 usage();
1059 }
1060 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001061 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1062 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001063 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001064 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001065 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001066 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001067 }
dweatherford65b70752007-10-31 02:18:14 +00001068 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001069
Ben Craig282e4402013-10-08 16:02:06 -05001070#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +00001071 //strip out trailing \ on Windows
1072 int last = out_path.length()-1;
1073 if (out_path[last] == '\\')
1074 {
1075 out_path.erase(last);
1076 }
1077#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001078 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001079 return -1;
Mark Sleefdbee812006-09-27 18:50:48 +00001080 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001081 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001082 usage();
1083 }
1084
1085 // Tokenize more
1086 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001087 }
1088 }
Mark Slee2c44d202007-05-16 02:18:07 +00001089
Jake Farrell2fd8a152012-09-29 00:26:36 +00001090 // display help
1091 if ((strcmp(argv[argc-1], "-help") == 0) || (strcmp(argv[argc-1], "--help") == 0)) {
1092 help();
1093 }
1094
David Reissdd08f6d2008-06-30 20:24:24 +00001095 // if you're asking for version, you have a right not to pass a file
Jake Farrell2fd8a152012-09-29 00:26:36 +00001096 if ((strcmp(argv[argc-1], "-version") == 0) || (strcmp(argv[argc-1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001097 version();
1098 exit(1);
1099 }
1100
Mark Sleef0712dc2006-10-25 19:03:57 +00001101 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001102 if (generator_strings.empty()) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001103 fprintf(stderr, "No output language(s) specified\n");
Mark Sleeb15a68b2006-06-07 06:46:24 +00001104 usage();
1105 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001106
1107 // Real-pathify it
Ben Craig282e4402013-10-08 16:02:06 -05001108 char rp[THRIFT_PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001109 if (argv[i] == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001110 fprintf(stderr, "Missing file name\n");
David Reiss5245f402008-06-10 22:56:26 +00001111 usage();
1112 }
David Reiss204420f2008-01-11 20:59:03 +00001113 if (saferealpath(argv[i], rp) == NULL) {
1114 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001115 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001116 string input_file(rp);
1117
Mark Sleef5377b32006-10-10 01:42:59 +00001118 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001119 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001120 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001121 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001122 }
kholst76f2c882008-01-16 02:47:41 +00001123
David Reiss4b6a3c72008-02-27 22:28:12 +00001124 // Compute the cpp include prefix.
1125 // infer this from the filename passed in
1126 string input_filename = argv[i];
1127 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001128
David Reiss4b6a3c72008-02-27 22:28:12 +00001129 string::size_type last_slash = string::npos;
1130 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1131 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001132 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001133
David Reiss4b6a3c72008-02-27 22:28:12 +00001134 program->set_include_prefix(include_prefix);
1135
Mark Sleef0712dc2006-10-25 19:03:57 +00001136 // Initialize global types
1137 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1138 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001139 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1140 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001141 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1142 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001143 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1144 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1145 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1146 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1147 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1148 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001149
Mark Sleef5377b32006-10-10 01:42:59 +00001150 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001151 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001152
David Reiss9cc2c132008-02-27 01:54:47 +00001153 // The current path is not really relevant when we are doing generation.
1154 // Reset the variable to make warning messages clearer.
1155 g_curpath = "generation";
1156 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1157 // That is what shows up during argument parsing.
1158 yylineno = 1;
1159
Mark Sleef0712dc2006-10-25 19:03:57 +00001160 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001161 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001162
Mark Sleef0712dc2006-10-25 19:03:57 +00001163 // Clean up. Who am I kidding... this program probably orphans heap memory
1164 // all over the place, but who cares because it is about to exit and it is
1165 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001166
Mark Sleef0712dc2006-10-25 19:03:57 +00001167 delete program;
1168 delete g_type_void;
1169 delete g_type_string;
1170 delete g_type_bool;
1171 delete g_type_byte;
1172 delete g_type_i16;
1173 delete g_type_i32;
1174 delete g_type_i64;
1175 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001176
1177 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001178 return 0;
1179}