blob: bba4cdc4eda9d814b8cef7175391c9b13b413771 [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 Craige9576752013-10-11 08:19:16 -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 Craige9576752013-10-11 08:19:16 -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
Jens Geyere8379b52014-01-25 00:59:45 +0100149/**
150 * The First doctext comment
151 */
152char* g_program_doctext_candidate;
153int g_program_doctext_lineno = 0;
154PROGDOCTEXT_STATUS g_program_doctext_status = INVALID;
155
David Reisscbd4bac2007-08-14 17:12:33 +0000156/**
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000157 * Whether or not negative field keys are accepted.
158 */
159int g_allow_neg_field_keys;
160
161/**
Roger Meier887ff752011-08-19 11:25:39 +0000162 * Whether or not 64-bit constants will generate a warning.
163 */
164int g_allow_64bit_consts = 0;
165
166/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000167 * Flags to control code generation
168 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000169bool gen_recurse = false;
170
171/**
Ben Craige9576752013-10-11 08:19:16 -0500172 * Win32 doesn't have realpath, so use fallback implementation in that case,
David Reiss204420f2008-01-11 20:59:03 +0000173 * otherwise this just calls through to realpath
174 */
175char *saferealpath(const char *path, char *resolved_path) {
Ben Craige9576752013-10-11 08:19:16 -0500176#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +0000177 char buf[MAX_PATH];
178 char* basename;
179 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
180 if (len == 0 || len > MAX_PATH - 1){
181 strcpy(resolved_path, path);
182 } else {
David Reiss204420f2008-01-11 20:59:03 +0000183 strcpy(resolved_path, buf);
184 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000185
186 // Replace backslashes with forward slashes so the
187 // rest of the code behaves correctly.
188 size_t resolved_len = strlen(resolved_path);
189 for (size_t i = 0; i < resolved_len; i++) {
190 if (resolved_path[i] == '\\') {
191 resolved_path[i] = '/';
192 }
193 }
David Reiss204420f2008-01-11 20:59:03 +0000194 return resolved_path;
195#else
196 return realpath(path, resolved_path);
197#endif
198}
199
Roger Meier061d4a22012-10-07 11:51:00 +0000200bool check_is_directory(const char *dir_name) {
Ben Craige9576752013-10-11 08:19:16 -0500201#ifdef _WIN32
Roger Meier061d4a22012-10-07 11:51:00 +0000202 DWORD attributes = ::GetFileAttributesA(dir_name);
203 if(attributes == INVALID_FILE_ATTRIBUTES) {
204 fprintf(stderr, "Output directory %s is unusable: GetLastError() = %ld\n", dir_name, GetLastError());
205 return false;
206 }
207 if((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
208 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
209 return false;
210 }
211 return true;
212#else
213 struct stat sb;
214 if (stat(dir_name, &sb) < 0) {
215 fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
216 return false;
217 }
218 if (! S_ISDIR(sb.st_mode)) {
219 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
220 return false;
221 }
222 return true;
223#endif
224}
David Reiss204420f2008-01-11 20:59:03 +0000225
226/**
Mark Slee31985722006-05-24 21:45:31 +0000227 * Report an error to the user. This is called yyerror for historical
228 * reasons (lex and yacc expect the error reporting routine to be called
229 * this). Call this function to report any errors to the user.
230 * yyerror takes printf style arguments.
231 *
232 * @param fmt C format string followed by additional arguments
233 */
David Reiss0babe402008-06-10 22:56:12 +0000234void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000235 va_list args;
236 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000237 "[ERROR:%s:%d] (last token was '%s')\n",
238 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000239 yylineno,
240 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000241
242 va_start(args, fmt);
243 vfprintf(stderr, fmt, args);
244 va_end(args);
245
246 fprintf(stderr, "\n");
247}
248
249/**
250 * Prints a debug message from the parser.
251 *
252 * @param fmt C format string followed by additional arguments
253 */
David Reiss0babe402008-06-10 22:56:12 +0000254void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000255 if (g_debug == 0) {
256 return;
257 }
258 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000259 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000260 va_start(args, fmt);
261 vprintf(fmt, args);
262 va_end(args);
263 printf("\n");
264}
265
266/**
267 * Prints a verbose output mode message
268 *
269 * @param fmt C format string followed by additional arguments
270 */
David Reiss0babe402008-06-10 22:56:12 +0000271void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000272 if (g_verbose == 0) {
273 return;
274 }
275 va_list args;
276 va_start(args, fmt);
277 vprintf(fmt, args);
278 va_end(args);
279}
280
281/**
282 * Prints a warning message
283 *
284 * @param fmt C format string followed by additional arguments
285 */
David Reiss0babe402008-06-10 22:56:12 +0000286void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000287 if (g_warn < level) {
288 return;
289 }
290 va_list args;
291 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000292 va_start(args, fmt);
293 vprintf(fmt, args);
294 va_end(args);
295 printf("\n");
296}
297
298/**
299 * Prints a failure message and exits
300 *
301 * @param fmt C format string followed by additional arguments
302 */
Mark Slee30152872006-11-28 01:24:07 +0000303void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000304 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000305 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000306 va_start(args, fmt);
307 vfprintf(stderr, fmt, args);
308 va_end(args);
309 printf("\n");
310 exit(1);
311}
312
313/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000314 * Converts a string filename into a thrift program name
315 */
316string program_name(string filename) {
317 string::size_type slash = filename.rfind("/");
318 if (slash != string::npos) {
319 filename = filename.substr(slash+1);
320 }
321 string::size_type dot = filename.rfind(".");
322 if (dot != string::npos) {
323 filename = filename.substr(0, dot);
324 }
325 return filename;
326}
327
328/**
329 * Gets the directory path of a filename
330 */
331string directory_name(string filename) {
332 string::size_type slash = filename.rfind("/");
333 // No slash, just use the current directory
334 if (slash == string::npos) {
335 return ".";
336 }
337 return filename.substr(0, slash);
338}
339
340/**
341 * Finds the appropriate file path for the given filename
342 */
343string include_file(string filename) {
344 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000345 if (filename[0] == '/') {
346 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500347 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000348 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000349 pwarning(0, "Cannot open include file %s\n", filename.c_str());
350 return std::string();
351 }
Mark Slee2c44d202007-05-16 02:18:07 +0000352
353 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000354 struct stat finfo;
355 if (stat(rp, &finfo) == 0) {
356 return rp;
357 }
358 } else { // relative path, start searching
359 // new search path with current dir global
360 vector<string> sp = g_incl_searchpath;
361 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000362
Martin Kraemer32c66e12006-11-09 00:06:36 +0000363 // iterate through paths
364 vector<string>::iterator it;
365 for (it = sp.begin(); it != sp.end(); it++) {
366 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000367
Martin Kraemer32c66e12006-11-09 00:06:36 +0000368 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500369 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000370 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000371 continue;
372 }
Mark Slee2c44d202007-05-16 02:18:07 +0000373
Martin Kraemer32c66e12006-11-09 00:06:36 +0000374 // Stat this files
375 struct stat finfo;
376 if (stat(rp, &finfo) == 0) {
377 return rp;
378 }
379 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000380 }
Mark Slee2c44d202007-05-16 02:18:07 +0000381
Mark Sleef0712dc2006-10-25 19:03:57 +0000382 // Uh oh
383 pwarning(0, "Could not find include file %s\n", filename.c_str());
384 return std::string();
385}
386
387/**
David Reisscbd4bac2007-08-14 17:12:33 +0000388 * Clears any previously stored doctext string.
389 * Also prints a warning if we are discarding information.
390 */
391void clear_doctext() {
392 if (g_doctext != NULL) {
393 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
394 }
395 free(g_doctext);
396 g_doctext = NULL;
397}
398
399/**
Jens Geyere8379b52014-01-25 00:59:45 +0100400 * Reset program doctext information after processing a file
401 */
402void reset_program_doctext_info() {
403 if(g_program_doctext_candidate != NULL) {
404 free(g_program_doctext_candidate);
405 g_program_doctext_candidate = NULL;
406 }
407 g_program_doctext_lineno = 0;
408 g_program_doctext_status = INVALID;
409}
410
411/**
412 * We are sure the program doctext candidate is really the program doctext.
413 */
414void declare_valid_program_doctext() {
415 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
416 g_program_doctext_status = ABSOLUTELY_SURE;
417 }
418}
419
420/**
David Reiss1ac05802007-07-30 22:00:27 +0000421 * Cleans up text commonly found in doxygen-like comments
422 *
423 * Warning: if you mix tabs and spaces in a non-uniform way,
424 * you will get what you deserve.
425 */
426char* clean_up_doctext(char* doctext) {
427 // Convert to C++ string, and remove Windows's carriage returns.
428 string docstring = doctext;
429 docstring.erase(
430 remove(docstring.begin(), docstring.end(), '\r'),
431 docstring.end());
432
433 // Separate into lines.
434 vector<string> lines;
435 string::size_type pos = string::npos;
436 string::size_type last;
437 while (true) {
438 last = (pos == string::npos) ? 0 : pos+1;
439 pos = docstring.find('\n', last);
440 if (pos == string::npos) {
441 // First bit of cleaning. If the last line is only whitespace, drop it.
442 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
443 if (nonwhite != string::npos) {
444 lines.push_back(docstring.substr(last));
445 }
446 break;
447 }
448 lines.push_back(docstring.substr(last, pos-last));
449 }
450
451 // A very profound docstring.
452 if (lines.empty()) {
453 return NULL;
454 }
455
456 // Clear leading whitespace from the first line.
457 pos = lines.front().find_first_not_of(" \t");
458 lines.front().erase(0, pos);
459
460 // If every nonblank line after the first has the same number of spaces/tabs,
461 // then a star, remove them.
462 bool have_prefix = true;
463 bool found_prefix = false;
464 string::size_type prefix_len = 0;
465 vector<string>::iterator l_iter;
466 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
467 if (l_iter->empty()) {
468 continue;
469 }
470
471 pos = l_iter->find_first_not_of(" \t");
472 if (!found_prefix) {
473 if (pos != string::npos) {
474 if (l_iter->at(pos) == '*') {
475 found_prefix = true;
476 prefix_len = pos;
477 } else {
478 have_prefix = false;
479 break;
480 }
481 } else {
482 // Whitespace-only line. Truncate it.
483 l_iter->clear();
484 }
485 } else if (l_iter->size() > pos
486 && l_iter->at(pos) == '*'
487 && pos == prefix_len) {
488 // Business as usual.
489 } else if (pos == string::npos) {
490 // Whitespace-only line. Let's truncate it for them.
491 l_iter->clear();
492 } else {
493 // The pattern has been broken.
494 have_prefix = false;
495 break;
496 }
497 }
498
499 // If our prefix survived, delete it from every line.
500 if (have_prefix) {
501 // Get the star too.
502 prefix_len++;
503 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
504 l_iter->erase(0, prefix_len);
505 }
506 }
507
508 // Now delete the minimum amount of leading whitespace from each line.
509 prefix_len = string::npos;
510 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
511 if (l_iter->empty()) {
512 continue;
513 }
514 pos = l_iter->find_first_not_of(" \t");
515 if (pos != string::npos
516 && (prefix_len == string::npos || pos < prefix_len)) {
517 prefix_len = pos;
518 }
519 }
520
521 // If our prefix survived, delete it from every line.
522 if (prefix_len != string::npos) {
523 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
524 l_iter->erase(0, prefix_len);
525 }
526 }
527
528 // Remove trailing whitespace from every line.
529 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
530 pos = l_iter->find_last_not_of(" \t");
531 if (pos != string::npos && pos != l_iter->length()-1) {
532 l_iter->erase(pos+1);
533 }
534 }
535
536 // If the first line is empty, remove it.
537 // Don't do this earlier because a lot of steps skip the first line.
538 if (lines.front().empty()) {
539 lines.erase(lines.begin());
540 }
541
542 // Now rejoin the lines and copy them back into doctext.
543 docstring.clear();
544 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
545 docstring += *l_iter;
546 docstring += '\n';
547 }
548
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200549 //assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
550 if(docstring.length() <= strlen(doctext)) {
551 strcpy(doctext, docstring.c_str());
552 } else {
553 free(doctext); // too short
554 doctext = strdup(docstring.c_str());
555 }
David Reiss1ac05802007-07-30 22:00:27 +0000556 return doctext;
557}
558
559/** Set to true to debug docstring parsing */
560static bool dump_docs = false;
561
562/**
563 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000564 * Only works for top-level definitions and the whole program doc
565 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000566 */
567void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000568 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000569 if (!progdoc.empty()) {
570 printf("Whole program doc:\n%s\n", progdoc.c_str());
571 }
David Reiss1ac05802007-07-30 22:00:27 +0000572 const vector<t_typedef*>& typedefs = program->get_typedefs();
573 vector<t_typedef*>::const_iterator t_iter;
574 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
575 t_typedef* td = *t_iter;
576 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000577 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
578 }
579 }
580 const vector<t_enum*>& enums = program->get_enums();
581 vector<t_enum*>::const_iterator e_iter;
582 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
583 t_enum* en = *e_iter;
584 if (en->has_doc()) {
585 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
586 }
587 }
588 const vector<t_const*>& consts = program->get_consts();
589 vector<t_const*>::const_iterator c_iter;
590 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
591 t_const* co = *c_iter;
592 if (co->has_doc()) {
593 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
594 }
595 }
596 const vector<t_struct*>& structs = program->get_structs();
597 vector<t_struct*>::const_iterator s_iter;
598 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
599 t_struct* st = *s_iter;
600 if (st->has_doc()) {
601 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
602 }
603 }
604 const vector<t_struct*>& xceptions = program->get_xceptions();
605 vector<t_struct*>::const_iterator x_iter;
606 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
607 t_struct* xn = *x_iter;
608 if (xn->has_doc()) {
609 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
610 }
611 }
612 const vector<t_service*>& services = program->get_services();
613 vector<t_service*>::const_iterator v_iter;
614 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
615 t_service* sv = *v_iter;
616 if (sv->has_doc()) {
617 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000618 }
619 }
620}
621
622/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000623 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000624 */
625void generate_all_fingerprints(t_program* program) {
626 const vector<t_struct*>& structs = program->get_structs();
627 vector<t_struct*>::const_iterator s_iter;
628 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
629 t_struct* st = *s_iter;
630 st->generate_fingerprint();
631 }
632
David Reissd779cbe2007-08-31 01:42:55 +0000633 const vector<t_struct*>& xceptions = program->get_xceptions();
634 vector<t_struct*>::const_iterator x_iter;
635 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
636 t_struct* st = *x_iter;
637 st->generate_fingerprint();
638 }
639
David Reiss3c5d2fd2008-02-08 21:58:06 +0000640 const vector<t_enum*>& enums = program->get_enums();
641 vector<t_enum*>::const_iterator e_iter;
642 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
643 t_enum* e = *e_iter;
644 e->generate_fingerprint();
645 }
646
David Reiss47557bc2007-09-04 21:31:04 +0000647 g_type_void->generate_fingerprint();
648
David Reiss18bf22d2007-08-28 20:49:17 +0000649 // If you want to generate fingerprints for implicit structures, start here.
650 /*
651 const vector<t_service*>& services = program->get_services();
652 vector<t_service*>::const_iterator v_iter;
653 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
654 t_service* sv = *v_iter;
655 }
656 */
657}
658
659/**
David Reissdd08f6d2008-06-30 20:24:24 +0000660 * Prints the version number
661 */
662void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000663 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000664}
665
666/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000667 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000668 */
669void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000670 fprintf(stderr, "Usage: thrift [options] file\n\n");
671 fprintf(stderr, "Use thrift -help for a list of options\n");
672 exit(1);
673}
674
675/**
676 * Diplays the help message and then exits with an error code.
677 */
678void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000679 fprintf(stderr, "Usage: thrift [options] file\n");
680 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000681 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000682 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
683 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000684 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
685 fprintf(stderr," (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000686 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000687 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000688 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
689 fprintf(stderr, " -strict Strict compiler warnings on\n");
690 fprintf(stderr, " -v[erbose] Verbose mode\n");
691 fprintf(stderr, " -r[ecurse] Also generate included files\n");
692 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000693 fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
694 "preserve protocol\n");
695 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000696 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000697 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
698 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
699 fprintf(stderr, " Keys and values are options passed to the generator.\n");
700 fprintf(stderr, " Many options will not require values.\n");
701 fprintf(stderr, "\n");
702 fprintf(stderr, "Available generators (and options):\n");
703
704 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
705 t_generator_registry::gen_map_t::iterator iter;
706 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
707 fprintf(stderr, " %s (%s):\n",
708 iter->second->get_short_name().c_str(),
709 iter->second->get_long_name().c_str());
710 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
711 }
Mark Slee31985722006-05-24 21:45:31 +0000712 exit(1);
713}
714
715/**
Mark Slee30152872006-11-28 01:24:07 +0000716 * You know, when I started working on Thrift I really thought it wasn't going
717 * to become a programming language because it was just a generator and it
718 * wouldn't need runtime type information and all that jazz. But then we
719 * decided to add constants, and all of a sudden that means runtime type
720 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000721 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000722 */
723void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
724 if (type->is_void()) {
725 throw "type error: cannot declare a void const: " + name;
726 }
727
728 if (type->is_base_type()) {
729 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
730 switch (tbase) {
731 case t_base_type::TYPE_STRING:
732 if (value->get_type() != t_const_value::CV_STRING) {
733 throw "type error: const \"" + name + "\" was declared as string";
734 }
735 break;
736 case t_base_type::TYPE_BOOL:
737 if (value->get_type() != t_const_value::CV_INTEGER) {
738 throw "type error: const \"" + name + "\" was declared as bool";
739 }
740 break;
741 case t_base_type::TYPE_BYTE:
742 if (value->get_type() != t_const_value::CV_INTEGER) {
743 throw "type error: const \"" + name + "\" was declared as byte";
744 }
745 break;
746 case t_base_type::TYPE_I16:
747 if (value->get_type() != t_const_value::CV_INTEGER) {
748 throw "type error: const \"" + name + "\" was declared as i16";
749 }
750 break;
751 case t_base_type::TYPE_I32:
752 if (value->get_type() != t_const_value::CV_INTEGER) {
753 throw "type error: const \"" + name + "\" was declared as i32";
754 }
755 break;
756 case t_base_type::TYPE_I64:
757 if (value->get_type() != t_const_value::CV_INTEGER) {
758 throw "type error: const \"" + name + "\" was declared as i64";
759 }
760 break;
761 case t_base_type::TYPE_DOUBLE:
762 if (value->get_type() != t_const_value::CV_INTEGER &&
763 value->get_type() != t_const_value::CV_DOUBLE) {
764 throw "type error: const \"" + name + "\" was declared as double";
765 }
766 break;
767 default:
David Reissdd7796f2007-08-28 21:09:06 +0000768 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000769 }
770 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000771 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000772 throw "type error: const \"" + name + "\" was declared as enum";
773 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000774
Bryan Duxbury1606f252010-11-24 00:25:57 +0000775 // see if there's a dot in the identifier
776 std::string name_portion = value->get_identifier_name();
777
Bryan Duxbury2d804702009-12-18 19:41:11 +0000778 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
779 vector<t_enum_value*>::const_iterator c_iter;
780 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000781
Bryan Duxbury1606f252010-11-24 00:25:57 +0000782 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000783 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000784 found = true;
785 break;
786 }
787 }
788 if (!found) {
Ben Craige9576752013-10-11 08:19:16 -0500789 throw "type error: const " + name + " was declared as type "
790 + type->get_name() + " which is an enum, but "
Bryan Duxbury2d804702009-12-18 19:41:11 +0000791 + value->get_identifier() + " is not a valid value for that enum";
792 }
Mark Slee30152872006-11-28 01:24:07 +0000793 } else if (type->is_struct() || type->is_xception()) {
794 if (value->get_type() != t_const_value::CV_MAP) {
795 throw "type error: const \"" + name + "\" was declared as struct/xception";
796 }
797 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
798 vector<t_field*>::const_iterator f_iter;
799
800 const map<t_const_value*, t_const_value*>& val = value->get_map();
801 map<t_const_value*, t_const_value*>::const_iterator v_iter;
802 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
803 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
804 throw "type error: " + name + " struct key must be string";
805 }
806 t_type* field_type = NULL;
807 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
808 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
809 field_type = (*f_iter)->get_type();
810 }
811 }
812 if (field_type == NULL) {
813 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
814 }
815
816 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
817 }
818 } else if (type->is_map()) {
819 t_type* k_type = ((t_map*)type)->get_key_type();
820 t_type* v_type = ((t_map*)type)->get_val_type();
821 const map<t_const_value*, t_const_value*>& val = value->get_map();
822 map<t_const_value*, t_const_value*>::const_iterator v_iter;
823 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
824 validate_const_rec(name + "<key>", k_type, v_iter->first);
825 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000826 }
Mark Slee30152872006-11-28 01:24:07 +0000827 } else if (type->is_list() || type->is_set()) {
828 t_type* e_type;
829 if (type->is_list()) {
830 e_type = ((t_list*)type)->get_elem_type();
831 } else {
832 e_type = ((t_set*)type)->get_elem_type();
833 }
834 const vector<t_const_value*>& val = value->get_list();
835 vector<t_const_value*>::const_iterator v_iter;
836 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
837 validate_const_rec(name + "<elem>", e_type, *v_iter);
838 }
839 }
840}
841
842/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200843 * Check simple identifier names
844 * It's easier to do it this way instead of rewriting the whole grammar etc.
845 */
846void validate_simple_identifier(const char* identifier) {
847 string name( identifier);
848 if( name.find(".") != string::npos) {
849 yyerror("Identifier %s can't have a dot.", identifier);
850 exit(1);
851 }
852}
853
854/**
Mark Slee30152872006-11-28 01:24:07 +0000855 * Check the type of the parsed const information against its declared type
856 */
857void validate_const_type(t_const* c) {
858 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
859}
860
861/**
Mark Slee7ff32452007-02-01 05:26:18 +0000862 * Check the type of a default value assigned to a field.
863 */
864void validate_field_value(t_field* field, t_const_value* cv) {
865 validate_const_rec(field->get_name(), field->get_type(), cv);
866}
867
868/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000869 * Check that all the elements of a throws block are actually exceptions.
870 */
871bool validate_throws(t_struct* throws) {
872 const vector<t_field*>& members = throws->get_members();
873 vector<t_field*>::const_iterator m_iter;
874 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000875 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000876 return false;
877 }
878 }
879 return true;
880}
881
882/**
Jens Geyer03d49442013-09-04 22:34:41 +0200883 * Skips UTF-8 BOM if there is one
884 */
885bool skip_utf8_bom(FILE* f) {
886
887 // pretty straightforward, but works
888 if( fgetc(f) == 0xEF) {
889 if( fgetc(f) == 0xBB) {
890 if( fgetc(f) == 0xBF) {
891 return true;
892 }
893 }
894 }
895
896 rewind(f);
897 return false;
898}
899
900/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000901 * Parses a program
902 */
Mark Slee2c44d202007-05-16 02:18:07 +0000903void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000904 // Get scope file path
905 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000906
Mark Sleef0712dc2006-10-25 19:03:57 +0000907 // Set current dir global, which is used in the include_file function
908 g_curdir = directory_name(path);
909 g_curpath = path;
910
911 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200912 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000913 yyin = fopen(path.c_str(), "r");
914 if (yyin == 0) {
915 failure("Could not open input file: \"%s\"", path.c_str());
916 }
Jens Geyer03d49442013-09-04 22:34:41 +0200917 if( skip_utf8_bom( yyin))
918 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
919
Mark Sleef0712dc2006-10-25 19:03:57 +0000920 // Create new scope and scan for includes
921 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000922 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000923 g_program = program;
924 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000925 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000926 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000927 if (yyparse() != 0) {
928 failure("Parser error during include pass.");
929 }
930 } catch (string x) {
931 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000932 }
933 fclose(yyin);
934
935 // Recursively parse all the include programs
936 vector<t_program*>& includes = program->get_includes();
937 vector<t_program*>::iterator iter;
938 for (iter = includes.begin(); iter != includes.end(); ++iter) {
939 parse(*iter, program);
940 }
941
Jens Geyere8379b52014-01-25 00:59:45 +0100942 // reset program doctext status before parsing a new file
943 reset_program_doctext_info();
944
David Reiss204420f2008-01-11 20:59:03 +0000945 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000946 g_parse_mode = PROGRAM;
947 g_program = program;
948 g_scope = program->scope();
949 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
950 g_parent_prefix = program->get_name() + ".";
951 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200952
953 // Open the file
954 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000955 yyin = fopen(path.c_str(), "r");
956 if (yyin == 0) {
957 failure("Could not open input file: \"%s\"", path.c_str());
958 }
Jens Geyer03d49442013-09-04 22:34:41 +0200959 if( skip_utf8_bom( yyin))
960 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
961
Mark Sleef0712dc2006-10-25 19:03:57 +0000962 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000963 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000964 try {
965 if (yyparse() != 0) {
966 failure("Parser error during types pass.");
967 }
968 } catch (string x) {
969 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000970 }
971 fclose(yyin);
972}
973
974/**
975 * Generate code
976 */
David Reissbd0db882008-02-27 01:54:51 +0000977void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000978 // Oooohh, recursive code generation, hot!!
979 if (gen_recurse) {
980 const vector<t_program*>& includes = program->get_includes();
981 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000982 // Propogate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000983 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000984
David Reissbd0db882008-02-27 01:54:51 +0000985 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000986 }
987 }
988
989 // Generate code!
990 try {
991 pverbose("Program: %s\n", program->get_path().c_str());
992
Jens Geyer83767a72013-09-23 22:09:12 +0200993 // Compute fingerprints. - not anymore, we do it on the fly now
994 //generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +0000995
David Reiss1ac05802007-07-30 22:00:27 +0000996 if (dump_docs) {
997 dump_docstrings(program);
998 }
David Reissbd0db882008-02-27 01:54:51 +0000999
1000 vector<string>::const_iterator iter;
1001 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1002 t_generator* generator = t_generator_registry::get_generator(program, *iter);
1003
1004 if (generator == NULL) {
1005 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1006 } else {
1007 pverbose("Generating \"%s\"\n", iter->c_str());
1008 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001009 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001010 }
1011 }
1012
Mark Sleef0712dc2006-10-25 19:03:57 +00001013 } catch (string s) {
1014 printf("Error: %s\n", s.c_str());
1015 } catch (const char* exc) {
1016 printf("Error: %s\n", exc);
1017 }
1018
1019}
1020
1021/**
Mark Sleef5377b32006-10-10 01:42:59 +00001022 * Parse it up.. then spit it back out, in pretty much every language. Alright
1023 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001024 */
1025int main(int argc, char** argv) {
1026 int i;
dweatherford65b70752007-10-31 02:18:14 +00001027 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001028 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001029
Mark Sleeb15a68b2006-06-07 06:46:24 +00001030 // Setup time string
1031 time_t now = time(NULL);
1032 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001033
Mark Sleef0712dc2006-10-25 19:03:57 +00001034 // Check for necessary arguments, you gotta have at least a filename and
1035 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001036 if (argc < 2) {
1037 usage();
1038 }
Mark Slee31985722006-05-24 21:45:31 +00001039
David Reissbd0db882008-02-27 01:54:51 +00001040 vector<string> generator_strings;
1041
David Reiss9cc2c132008-02-27 01:54:47 +00001042 // Set the current path to a dummy value to make warning messages clearer.
1043 g_curpath = "arguments";
1044
Mark Sleef5377b32006-10-10 01:42:59 +00001045 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +00001046 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001047 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001048
Mark Sleefdbee812006-09-27 18:50:48 +00001049 arg = strtok(argv[i], " ");
1050 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001051 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001052 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001053 ++arg;
1054 }
1055
Jake Farrell2fd8a152012-09-29 00:26:36 +00001056 if (strcmp(arg, "-help") == 0) {
1057 help();
1058 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001059 version();
jfarrell70969422013-09-09 20:33:38 -04001060 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001061 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001062 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001063 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001064 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001065 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001066 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001067 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001068 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001069 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001070 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001071 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001072 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1073 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001074 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1075 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001076 } else if (strcmp(arg, "-gen") == 0) {
1077 arg = argv[++i];
1078 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001079 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001080 usage();
1081 }
1082 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001083 } else if (strcmp(arg, "-I") == 0) {
1084 // An argument of "-I\ asdf" is invalid and has unknown results
1085 arg = argv[++i];
1086
1087 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001088 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001089 usage();
1090 }
1091 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001092 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1093 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001094 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001095 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001096 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001097 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001098 }
dweatherford65b70752007-10-31 02:18:14 +00001099 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001100
Ben Craige9576752013-10-11 08:19:16 -05001101#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +00001102 //strip out trailing \ on Windows
1103 int last = out_path.length()-1;
1104 if (out_path[last] == '\\')
1105 {
1106 out_path.erase(last);
1107 }
1108#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001109 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001110 return -1;
Mark Sleefdbee812006-09-27 18:50:48 +00001111 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001112 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001113 usage();
1114 }
1115
1116 // Tokenize more
1117 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001118 }
1119 }
Mark Slee2c44d202007-05-16 02:18:07 +00001120
Jake Farrell2fd8a152012-09-29 00:26:36 +00001121 // display help
1122 if ((strcmp(argv[argc-1], "-help") == 0) || (strcmp(argv[argc-1], "--help") == 0)) {
1123 help();
1124 }
1125
David Reissdd08f6d2008-06-30 20:24:24 +00001126 // if you're asking for version, you have a right not to pass a file
Jake Farrell2fd8a152012-09-29 00:26:36 +00001127 if ((strcmp(argv[argc-1], "-version") == 0) || (strcmp(argv[argc-1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001128 version();
1129 exit(1);
1130 }
1131
Mark Sleef0712dc2006-10-25 19:03:57 +00001132 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001133 if (generator_strings.empty()) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001134 fprintf(stderr, "No output language(s) specified\n");
Mark Sleeb15a68b2006-06-07 06:46:24 +00001135 usage();
1136 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001137
1138 // Real-pathify it
Ben Craige9576752013-10-11 08:19:16 -05001139 char rp[THRIFT_PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001140 if (argv[i] == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001141 fprintf(stderr, "Missing file name\n");
David Reiss5245f402008-06-10 22:56:26 +00001142 usage();
1143 }
David Reiss204420f2008-01-11 20:59:03 +00001144 if (saferealpath(argv[i], rp) == NULL) {
1145 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001146 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001147 string input_file(rp);
1148
Mark Sleef5377b32006-10-10 01:42:59 +00001149 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001150 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001151 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001152 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001153 }
kholst76f2c882008-01-16 02:47:41 +00001154
David Reiss4b6a3c72008-02-27 22:28:12 +00001155 // Compute the cpp include prefix.
1156 // infer this from the filename passed in
1157 string input_filename = argv[i];
1158 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001159
David Reiss4b6a3c72008-02-27 22:28:12 +00001160 string::size_type last_slash = string::npos;
1161 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1162 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001163 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001164
David Reiss4b6a3c72008-02-27 22:28:12 +00001165 program->set_include_prefix(include_prefix);
1166
Mark Sleef0712dc2006-10-25 19:03:57 +00001167 // Initialize global types
1168 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1169 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001170 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1171 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001172 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1173 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001174 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1175 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1176 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1177 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1178 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1179 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001180
Mark Sleef5377b32006-10-10 01:42:59 +00001181 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001182 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001183
David Reiss9cc2c132008-02-27 01:54:47 +00001184 // The current path is not really relevant when we are doing generation.
1185 // Reset the variable to make warning messages clearer.
1186 g_curpath = "generation";
1187 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1188 // That is what shows up during argument parsing.
1189 yylineno = 1;
1190
Mark Sleef0712dc2006-10-25 19:03:57 +00001191 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001192 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001193
Mark Sleef0712dc2006-10-25 19:03:57 +00001194 // Clean up. Who am I kidding... this program probably orphans heap memory
1195 // all over the place, but who cares because it is about to exit and it is
1196 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001197
Mark Sleef0712dc2006-10-25 19:03:57 +00001198 delete program;
1199 delete g_type_void;
1200 delete g_type_string;
1201 delete g_type_bool;
1202 delete g_type_byte;
1203 delete g_type_i16;
1204 delete g_type_i32;
1205 delete g_type_i64;
1206 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001207
1208 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001209 return 0;
1210}