blob: 9e92f1f56dbbe5e6995d4f800c907175985cf41e [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;
Jens Geyer813749d2014-01-31 23:42:57 +0100409 pdebug("%s","program doctext set to INVALID");
Jens Geyere8379b52014-01-25 00:59:45 +0100410}
411
412/**
413 * We are sure the program doctext candidate is really the program doctext.
414 */
415void declare_valid_program_doctext() {
416 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
417 g_program_doctext_status = ABSOLUTELY_SURE;
Jens Geyer813749d2014-01-31 23:42:57 +0100418 pdebug("%s","program doctext set to ABSOLUTELY_SURE");
419 } else {
420 g_program_doctext_status = NO_PROGRAM_DOCTEXT;
421 pdebug("%s","program doctext set to NO_PROGRAM_DOCTEXT");
Jens Geyere8379b52014-01-25 00:59:45 +0100422 }
423}
424
425/**
David Reiss1ac05802007-07-30 22:00:27 +0000426 * Cleans up text commonly found in doxygen-like comments
427 *
428 * Warning: if you mix tabs and spaces in a non-uniform way,
429 * you will get what you deserve.
430 */
431char* clean_up_doctext(char* doctext) {
432 // Convert to C++ string, and remove Windows's carriage returns.
433 string docstring = doctext;
434 docstring.erase(
435 remove(docstring.begin(), docstring.end(), '\r'),
436 docstring.end());
437
438 // Separate into lines.
439 vector<string> lines;
440 string::size_type pos = string::npos;
441 string::size_type last;
442 while (true) {
443 last = (pos == string::npos) ? 0 : pos+1;
444 pos = docstring.find('\n', last);
445 if (pos == string::npos) {
446 // First bit of cleaning. If the last line is only whitespace, drop it.
447 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
448 if (nonwhite != string::npos) {
449 lines.push_back(docstring.substr(last));
450 }
451 break;
452 }
453 lines.push_back(docstring.substr(last, pos-last));
454 }
455
456 // A very profound docstring.
457 if (lines.empty()) {
458 return NULL;
459 }
460
461 // Clear leading whitespace from the first line.
462 pos = lines.front().find_first_not_of(" \t");
463 lines.front().erase(0, pos);
464
465 // If every nonblank line after the first has the same number of spaces/tabs,
466 // then a star, remove them.
467 bool have_prefix = true;
468 bool found_prefix = false;
469 string::size_type prefix_len = 0;
470 vector<string>::iterator l_iter;
471 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
472 if (l_iter->empty()) {
473 continue;
474 }
475
476 pos = l_iter->find_first_not_of(" \t");
477 if (!found_prefix) {
478 if (pos != string::npos) {
479 if (l_iter->at(pos) == '*') {
480 found_prefix = true;
481 prefix_len = pos;
482 } else {
483 have_prefix = false;
484 break;
485 }
486 } else {
487 // Whitespace-only line. Truncate it.
488 l_iter->clear();
489 }
490 } else if (l_iter->size() > pos
491 && l_iter->at(pos) == '*'
492 && pos == prefix_len) {
493 // Business as usual.
494 } else if (pos == string::npos) {
495 // Whitespace-only line. Let's truncate it for them.
496 l_iter->clear();
497 } else {
498 // The pattern has been broken.
499 have_prefix = false;
500 break;
501 }
502 }
503
504 // If our prefix survived, delete it from every line.
505 if (have_prefix) {
506 // Get the star too.
507 prefix_len++;
508 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
509 l_iter->erase(0, prefix_len);
510 }
511 }
512
513 // Now delete the minimum amount of leading whitespace from each line.
514 prefix_len = string::npos;
515 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
516 if (l_iter->empty()) {
517 continue;
518 }
519 pos = l_iter->find_first_not_of(" \t");
520 if (pos != string::npos
521 && (prefix_len == string::npos || pos < prefix_len)) {
522 prefix_len = pos;
523 }
524 }
525
526 // If our prefix survived, delete it from every line.
527 if (prefix_len != string::npos) {
528 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
529 l_iter->erase(0, prefix_len);
530 }
531 }
532
533 // Remove trailing whitespace from every line.
534 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
535 pos = l_iter->find_last_not_of(" \t");
536 if (pos != string::npos && pos != l_iter->length()-1) {
537 l_iter->erase(pos+1);
538 }
539 }
540
541 // If the first line is empty, remove it.
542 // Don't do this earlier because a lot of steps skip the first line.
543 if (lines.front().empty()) {
544 lines.erase(lines.begin());
545 }
546
547 // Now rejoin the lines and copy them back into doctext.
548 docstring.clear();
549 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
550 docstring += *l_iter;
551 docstring += '\n';
552 }
553
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200554 //assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
555 if(docstring.length() <= strlen(doctext)) {
556 strcpy(doctext, docstring.c_str());
557 } else {
558 free(doctext); // too short
559 doctext = strdup(docstring.c_str());
560 }
David Reiss1ac05802007-07-30 22:00:27 +0000561 return doctext;
562}
563
564/** Set to true to debug docstring parsing */
565static bool dump_docs = false;
566
567/**
568 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000569 * Only works for top-level definitions and the whole program doc
570 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000571 */
572void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000573 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000574 if (!progdoc.empty()) {
575 printf("Whole program doc:\n%s\n", progdoc.c_str());
576 }
David Reiss1ac05802007-07-30 22:00:27 +0000577 const vector<t_typedef*>& typedefs = program->get_typedefs();
578 vector<t_typedef*>::const_iterator t_iter;
579 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
580 t_typedef* td = *t_iter;
581 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000582 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
583 }
584 }
585 const vector<t_enum*>& enums = program->get_enums();
586 vector<t_enum*>::const_iterator e_iter;
587 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
588 t_enum* en = *e_iter;
589 if (en->has_doc()) {
590 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
591 }
592 }
593 const vector<t_const*>& consts = program->get_consts();
594 vector<t_const*>::const_iterator c_iter;
595 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
596 t_const* co = *c_iter;
597 if (co->has_doc()) {
598 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
599 }
600 }
601 const vector<t_struct*>& structs = program->get_structs();
602 vector<t_struct*>::const_iterator s_iter;
603 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
604 t_struct* st = *s_iter;
605 if (st->has_doc()) {
606 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
607 }
608 }
609 const vector<t_struct*>& xceptions = program->get_xceptions();
610 vector<t_struct*>::const_iterator x_iter;
611 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
612 t_struct* xn = *x_iter;
613 if (xn->has_doc()) {
614 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
615 }
616 }
617 const vector<t_service*>& services = program->get_services();
618 vector<t_service*>::const_iterator v_iter;
619 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
620 t_service* sv = *v_iter;
621 if (sv->has_doc()) {
622 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000623 }
624 }
625}
626
627/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000628 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000629 */
630void generate_all_fingerprints(t_program* program) {
631 const vector<t_struct*>& structs = program->get_structs();
632 vector<t_struct*>::const_iterator s_iter;
633 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
634 t_struct* st = *s_iter;
635 st->generate_fingerprint();
636 }
637
David Reissd779cbe2007-08-31 01:42:55 +0000638 const vector<t_struct*>& xceptions = program->get_xceptions();
639 vector<t_struct*>::const_iterator x_iter;
640 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
641 t_struct* st = *x_iter;
642 st->generate_fingerprint();
643 }
644
David Reiss3c5d2fd2008-02-08 21:58:06 +0000645 const vector<t_enum*>& enums = program->get_enums();
646 vector<t_enum*>::const_iterator e_iter;
647 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
648 t_enum* e = *e_iter;
649 e->generate_fingerprint();
650 }
651
David Reiss47557bc2007-09-04 21:31:04 +0000652 g_type_void->generate_fingerprint();
653
David Reiss18bf22d2007-08-28 20:49:17 +0000654 // If you want to generate fingerprints for implicit structures, start here.
655 /*
656 const vector<t_service*>& services = program->get_services();
657 vector<t_service*>::const_iterator v_iter;
658 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
659 t_service* sv = *v_iter;
660 }
661 */
662}
663
Jens Geyer6fe77e82014-03-16 16:48:53 +0200664
665/**
666 * Emits a warning on list<byte>, binary type is typically a much better choice.
667 */
668void check_for_list_of_bytes(t_type* list_elem_type) {
669 if((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
670 t_base_type* tbase = (t_base_type*)list_elem_type;
671 if(tbase->get_base() == t_base_type::TYPE_BYTE) {
672 pwarning(1,"Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
673 }
674 }
675}
676
677
David Reiss18bf22d2007-08-28 20:49:17 +0000678/**
David Reissdd08f6d2008-06-30 20:24:24 +0000679 * Prints the version number
680 */
681void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000682 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000683}
684
685/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000686 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000687 */
688void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000689 fprintf(stderr, "Usage: thrift [options] file\n\n");
690 fprintf(stderr, "Use thrift -help for a list of options\n");
691 exit(1);
692}
693
694/**
695 * Diplays the help message and then exits with an error code.
696 */
697void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000698 fprintf(stderr, "Usage: thrift [options] file\n");
699 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000700 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000701 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
702 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000703 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
704 fprintf(stderr," (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000705 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000706 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000707 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
708 fprintf(stderr, " -strict Strict compiler warnings on\n");
709 fprintf(stderr, " -v[erbose] Verbose mode\n");
710 fprintf(stderr, " -r[ecurse] Also generate included files\n");
711 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000712 fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
713 "preserve protocol\n");
714 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000715 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000716 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200717 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000718 fprintf(stderr, " Keys and values are options passed to the generator.\n");
719 fprintf(stderr, " Many options will not require values.\n");
720 fprintf(stderr, "\n");
721 fprintf(stderr, "Available generators (and options):\n");
722
723 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
724 t_generator_registry::gen_map_t::iterator iter;
725 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
726 fprintf(stderr, " %s (%s):\n",
727 iter->second->get_short_name().c_str(),
728 iter->second->get_long_name().c_str());
729 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
730 }
Mark Slee31985722006-05-24 21:45:31 +0000731 exit(1);
732}
733
734/**
Mark Slee30152872006-11-28 01:24:07 +0000735 * You know, when I started working on Thrift I really thought it wasn't going
736 * to become a programming language because it was just a generator and it
737 * wouldn't need runtime type information and all that jazz. But then we
738 * decided to add constants, and all of a sudden that means runtime type
739 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000740 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000741 */
742void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
743 if (type->is_void()) {
744 throw "type error: cannot declare a void const: " + name;
745 }
746
747 if (type->is_base_type()) {
748 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
749 switch (tbase) {
750 case t_base_type::TYPE_STRING:
751 if (value->get_type() != t_const_value::CV_STRING) {
752 throw "type error: const \"" + name + "\" was declared as string";
753 }
754 break;
755 case t_base_type::TYPE_BOOL:
756 if (value->get_type() != t_const_value::CV_INTEGER) {
757 throw "type error: const \"" + name + "\" was declared as bool";
758 }
759 break;
760 case t_base_type::TYPE_BYTE:
761 if (value->get_type() != t_const_value::CV_INTEGER) {
762 throw "type error: const \"" + name + "\" was declared as byte";
763 }
764 break;
765 case t_base_type::TYPE_I16:
766 if (value->get_type() != t_const_value::CV_INTEGER) {
767 throw "type error: const \"" + name + "\" was declared as i16";
768 }
769 break;
770 case t_base_type::TYPE_I32:
771 if (value->get_type() != t_const_value::CV_INTEGER) {
772 throw "type error: const \"" + name + "\" was declared as i32";
773 }
774 break;
775 case t_base_type::TYPE_I64:
776 if (value->get_type() != t_const_value::CV_INTEGER) {
777 throw "type error: const \"" + name + "\" was declared as i64";
778 }
779 break;
780 case t_base_type::TYPE_DOUBLE:
781 if (value->get_type() != t_const_value::CV_INTEGER &&
782 value->get_type() != t_const_value::CV_DOUBLE) {
783 throw "type error: const \"" + name + "\" was declared as double";
784 }
785 break;
786 default:
David Reissdd7796f2007-08-28 21:09:06 +0000787 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000788 }
789 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000790 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000791 throw "type error: const \"" + name + "\" was declared as enum";
792 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000793
Bryan Duxbury1606f252010-11-24 00:25:57 +0000794 // see if there's a dot in the identifier
795 std::string name_portion = value->get_identifier_name();
796
Bryan Duxbury2d804702009-12-18 19:41:11 +0000797 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
798 vector<t_enum_value*>::const_iterator c_iter;
799 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000800
Bryan Duxbury1606f252010-11-24 00:25:57 +0000801 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000802 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000803 found = true;
804 break;
805 }
806 }
807 if (!found) {
Ben Craige9576752013-10-11 08:19:16 -0500808 throw "type error: const " + name + " was declared as type "
809 + type->get_name() + " which is an enum, but "
Bryan Duxbury2d804702009-12-18 19:41:11 +0000810 + value->get_identifier() + " is not a valid value for that enum";
811 }
Mark Slee30152872006-11-28 01:24:07 +0000812 } else if (type->is_struct() || type->is_xception()) {
813 if (value->get_type() != t_const_value::CV_MAP) {
814 throw "type error: const \"" + name + "\" was declared as struct/xception";
815 }
816 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
817 vector<t_field*>::const_iterator f_iter;
818
819 const map<t_const_value*, t_const_value*>& val = value->get_map();
820 map<t_const_value*, t_const_value*>::const_iterator v_iter;
821 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
822 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
823 throw "type error: " + name + " struct key must be string";
824 }
825 t_type* field_type = NULL;
826 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
827 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
828 field_type = (*f_iter)->get_type();
829 }
830 }
831 if (field_type == NULL) {
832 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
833 }
834
835 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
836 }
837 } else if (type->is_map()) {
838 t_type* k_type = ((t_map*)type)->get_key_type();
839 t_type* v_type = ((t_map*)type)->get_val_type();
840 const map<t_const_value*, t_const_value*>& val = value->get_map();
841 map<t_const_value*, t_const_value*>::const_iterator v_iter;
842 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
843 validate_const_rec(name + "<key>", k_type, v_iter->first);
844 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000845 }
Mark Slee30152872006-11-28 01:24:07 +0000846 } else if (type->is_list() || type->is_set()) {
847 t_type* e_type;
848 if (type->is_list()) {
849 e_type = ((t_list*)type)->get_elem_type();
850 } else {
851 e_type = ((t_set*)type)->get_elem_type();
852 }
853 const vector<t_const_value*>& val = value->get_list();
854 vector<t_const_value*>::const_iterator v_iter;
855 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
856 validate_const_rec(name + "<elem>", e_type, *v_iter);
857 }
858 }
859}
860
861/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200862 * Check simple identifier names
863 * It's easier to do it this way instead of rewriting the whole grammar etc.
864 */
865void validate_simple_identifier(const char* identifier) {
866 string name( identifier);
867 if( name.find(".") != string::npos) {
868 yyerror("Identifier %s can't have a dot.", identifier);
869 exit(1);
870 }
871}
872
873/**
Mark Slee30152872006-11-28 01:24:07 +0000874 * Check the type of the parsed const information against its declared type
875 */
876void validate_const_type(t_const* c) {
877 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
878}
879
880/**
Mark Slee7ff32452007-02-01 05:26:18 +0000881 * Check the type of a default value assigned to a field.
882 */
883void validate_field_value(t_field* field, t_const_value* cv) {
884 validate_const_rec(field->get_name(), field->get_type(), cv);
885}
886
887/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000888 * Check that all the elements of a throws block are actually exceptions.
889 */
890bool validate_throws(t_struct* throws) {
891 const vector<t_field*>& members = throws->get_members();
892 vector<t_field*>::const_iterator m_iter;
893 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000894 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000895 return false;
896 }
897 }
898 return true;
899}
900
901/**
Jens Geyer03d49442013-09-04 22:34:41 +0200902 * Skips UTF-8 BOM if there is one
903 */
904bool skip_utf8_bom(FILE* f) {
905
906 // pretty straightforward, but works
907 if( fgetc(f) == 0xEF) {
908 if( fgetc(f) == 0xBB) {
909 if( fgetc(f) == 0xBF) {
910 return true;
911 }
912 }
913 }
914
915 rewind(f);
916 return false;
917}
918
919/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000920 * Parses a program
921 */
Mark Slee2c44d202007-05-16 02:18:07 +0000922void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000923 // Get scope file path
924 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000925
Mark Sleef0712dc2006-10-25 19:03:57 +0000926 // Set current dir global, which is used in the include_file function
927 g_curdir = directory_name(path);
928 g_curpath = path;
929
930 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200931 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000932 yyin = fopen(path.c_str(), "r");
933 if (yyin == 0) {
934 failure("Could not open input file: \"%s\"", path.c_str());
935 }
Jens Geyer03d49442013-09-04 22:34:41 +0200936 if( skip_utf8_bom( yyin))
937 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
938
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 // Create new scope and scan for includes
940 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000941 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000942 g_program = program;
943 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000944 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000945 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000946 if (yyparse() != 0) {
947 failure("Parser error during include pass.");
948 }
949 } catch (string x) {
950 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000951 }
952 fclose(yyin);
953
954 // Recursively parse all the include programs
955 vector<t_program*>& includes = program->get_includes();
956 vector<t_program*>::iterator iter;
957 for (iter = includes.begin(); iter != includes.end(); ++iter) {
958 parse(*iter, program);
959 }
960
Jens Geyere8379b52014-01-25 00:59:45 +0100961 // reset program doctext status before parsing a new file
962 reset_program_doctext_info();
963
David Reiss204420f2008-01-11 20:59:03 +0000964 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000965 g_parse_mode = PROGRAM;
966 g_program = program;
967 g_scope = program->scope();
968 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
969 g_parent_prefix = program->get_name() + ".";
970 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200971
972 // Open the file
973 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000974 yyin = fopen(path.c_str(), "r");
975 if (yyin == 0) {
976 failure("Could not open input file: \"%s\"", path.c_str());
977 }
Jens Geyer03d49442013-09-04 22:34:41 +0200978 if( skip_utf8_bom( yyin))
979 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
980
Mark Sleef0712dc2006-10-25 19:03:57 +0000981 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000982 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000983 try {
984 if (yyparse() != 0) {
985 failure("Parser error during types pass.");
986 }
987 } catch (string x) {
988 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000989 }
990 fclose(yyin);
991}
992
993/**
994 * Generate code
995 */
David Reissbd0db882008-02-27 01:54:51 +0000996void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000997 // Oooohh, recursive code generation, hot!!
998 if (gen_recurse) {
999 const vector<t_program*>& includes = program->get_includes();
1000 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +00001001 // Propogate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001002 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +00001003
David Reissbd0db882008-02-27 01:54:51 +00001004 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +00001005 }
1006 }
1007
1008 // Generate code!
1009 try {
1010 pverbose("Program: %s\n", program->get_path().c_str());
1011
Jens Geyer83767a72013-09-23 22:09:12 +02001012 // Compute fingerprints. - not anymore, we do it on the fly now
1013 //generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +00001014
David Reiss1ac05802007-07-30 22:00:27 +00001015 if (dump_docs) {
1016 dump_docstrings(program);
1017 }
David Reissbd0db882008-02-27 01:54:51 +00001018
1019 vector<string>::const_iterator iter;
1020 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1021 t_generator* generator = t_generator_registry::get_generator(program, *iter);
1022
1023 if (generator == NULL) {
1024 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1025 } else {
1026 pverbose("Generating \"%s\"\n", iter->c_str());
1027 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001028 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001029 }
1030 }
1031
Mark Sleef0712dc2006-10-25 19:03:57 +00001032 } catch (string s) {
1033 printf("Error: %s\n", s.c_str());
1034 } catch (const char* exc) {
1035 printf("Error: %s\n", exc);
1036 }
1037
1038}
1039
1040/**
Mark Sleef5377b32006-10-10 01:42:59 +00001041 * Parse it up.. then spit it back out, in pretty much every language. Alright
1042 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001043 */
1044int main(int argc, char** argv) {
1045 int i;
dweatherford65b70752007-10-31 02:18:14 +00001046 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001047 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001048
Mark Sleeb15a68b2006-06-07 06:46:24 +00001049 // Setup time string
1050 time_t now = time(NULL);
1051 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001052
Mark Sleef0712dc2006-10-25 19:03:57 +00001053 // Check for necessary arguments, you gotta have at least a filename and
1054 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001055 if (argc < 2) {
1056 usage();
1057 }
Mark Slee31985722006-05-24 21:45:31 +00001058
David Reissbd0db882008-02-27 01:54:51 +00001059 vector<string> generator_strings;
1060
David Reiss9cc2c132008-02-27 01:54:47 +00001061 // Set the current path to a dummy value to make warning messages clearer.
1062 g_curpath = "arguments";
1063
Mark Sleef5377b32006-10-10 01:42:59 +00001064 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +00001065 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001066 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001067
Mark Sleefdbee812006-09-27 18:50:48 +00001068 arg = strtok(argv[i], " ");
1069 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001070 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001071 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001072 ++arg;
1073 }
1074
Jake Farrell2fd8a152012-09-29 00:26:36 +00001075 if (strcmp(arg, "-help") == 0) {
1076 help();
1077 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001078 version();
jfarrell70969422013-09-09 20:33:38 -04001079 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001080 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001081 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001082 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001083 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001084 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001085 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001086 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001087 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001088 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001089 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001090 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001091 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1092 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001093 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1094 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001095 } else if (strcmp(arg, "-gen") == 0) {
1096 arg = argv[++i];
1097 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001098 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001099 usage();
1100 }
1101 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001102 } else if (strcmp(arg, "-I") == 0) {
1103 // An argument of "-I\ asdf" is invalid and has unknown results
1104 arg = argv[++i];
1105
1106 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001107 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001108 usage();
1109 }
1110 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001111 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1112 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001113 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001114 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001115 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001116 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001117 }
dweatherford65b70752007-10-31 02:18:14 +00001118 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001119
Ben Craige9576752013-10-11 08:19:16 -05001120#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +00001121 //strip out trailing \ on Windows
1122 int last = out_path.length()-1;
1123 if (out_path[last] == '\\')
1124 {
1125 out_path.erase(last);
1126 }
1127#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001128 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001129 return -1;
Mark Sleefdbee812006-09-27 18:50:48 +00001130 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001131 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001132 usage();
1133 }
1134
1135 // Tokenize more
1136 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001137 }
1138 }
Mark Slee2c44d202007-05-16 02:18:07 +00001139
Jake Farrell2fd8a152012-09-29 00:26:36 +00001140 // display help
1141 if ((strcmp(argv[argc-1], "-help") == 0) || (strcmp(argv[argc-1], "--help") == 0)) {
1142 help();
1143 }
1144
David Reissdd08f6d2008-06-30 20:24:24 +00001145 // if you're asking for version, you have a right not to pass a file
Jake Farrell2fd8a152012-09-29 00:26:36 +00001146 if ((strcmp(argv[argc-1], "-version") == 0) || (strcmp(argv[argc-1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001147 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001148 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001149 }
1150
Mark Sleef0712dc2006-10-25 19:03:57 +00001151 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001152 if (generator_strings.empty()) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001153 fprintf(stderr, "No output language(s) specified\n");
Mark Sleeb15a68b2006-06-07 06:46:24 +00001154 usage();
1155 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001156
1157 // Real-pathify it
Ben Craige9576752013-10-11 08:19:16 -05001158 char rp[THRIFT_PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001159 if (argv[i] == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001160 fprintf(stderr, "Missing file name\n");
David Reiss5245f402008-06-10 22:56:26 +00001161 usage();
1162 }
David Reiss204420f2008-01-11 20:59:03 +00001163 if (saferealpath(argv[i], rp) == NULL) {
1164 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001165 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001166 string input_file(rp);
1167
Mark Sleef5377b32006-10-10 01:42:59 +00001168 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001169 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001170 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001171 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001172 }
kholst76f2c882008-01-16 02:47:41 +00001173
David Reiss4b6a3c72008-02-27 22:28:12 +00001174 // Compute the cpp include prefix.
1175 // infer this from the filename passed in
1176 string input_filename = argv[i];
1177 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001178
David Reiss4b6a3c72008-02-27 22:28:12 +00001179 string::size_type last_slash = string::npos;
1180 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1181 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001182 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001183
David Reiss4b6a3c72008-02-27 22:28:12 +00001184 program->set_include_prefix(include_prefix);
1185
Mark Sleef0712dc2006-10-25 19:03:57 +00001186 // Initialize global types
1187 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1188 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001189 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1190 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001191 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1192 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001193 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1194 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1195 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1196 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1197 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1198 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001199
Mark Sleef5377b32006-10-10 01:42:59 +00001200 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001201 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001202
David Reiss9cc2c132008-02-27 01:54:47 +00001203 // The current path is not really relevant when we are doing generation.
1204 // Reset the variable to make warning messages clearer.
1205 g_curpath = "generation";
1206 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1207 // That is what shows up during argument parsing.
1208 yylineno = 1;
1209
Mark Sleef0712dc2006-10-25 19:03:57 +00001210 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001211 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001212
Mark Sleef0712dc2006-10-25 19:03:57 +00001213 // Clean up. Who am I kidding... this program probably orphans heap memory
1214 // all over the place, but who cares because it is about to exit and it is
1215 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001216
Mark Sleef0712dc2006-10-25 19:03:57 +00001217 delete program;
1218 delete g_type_void;
1219 delete g_type_string;
1220 delete g_type_bool;
1221 delete g_type_byte;
1222 delete g_type_i16;
1223 delete g_type_i32;
1224 delete g_type_i64;
1225 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001226
1227 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001228 return 0;
1229}