blob: c0166a1a34ec689a1dff6c4cd28510a7b58da170 [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
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043#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
Roger Meier4f4b15b2014-11-05 16:51:04 +0100149/**
Jens Geyere8379b52014-01-25 00:59:45 +0100150 * The First doctext comment
151 */
152char* g_program_doctext_candidate;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100153int g_program_doctext_lineno = 0;
154PROGDOCTEXT_STATUS g_program_doctext_status = INVALID;
Jens Geyere8379b52014-01-25 00:59:45 +0100155
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 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100175char* 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);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100180 if (len == 0 || len > MAX_PATH - 1) {
David Reiss204420f2008-01-11 20:59:03 +0000181 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100200bool 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);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100203 if (attributes == INVALID_FILE_ATTRIBUTES) {
204 fprintf(stderr,
205 "Output directory %s is unusable: GetLastError() = %ld\n",
206 dir_name,
207 GetLastError());
Roger Meier061d4a22012-10-07 11:51:00 +0000208 return false;
209 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100210 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
Roger Meier061d4a22012-10-07 11:51:00 +0000211 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
212 return false;
213 }
214 return true;
215#else
216 struct stat sb;
217 if (stat(dir_name, &sb) < 0) {
218 fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
219 return false;
220 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100221 if (!S_ISDIR(sb.st_mode)) {
Roger Meier061d4a22012-10-07 11:51:00 +0000222 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
223 return false;
224 }
225 return true;
226#endif
227}
David Reiss204420f2008-01-11 20:59:03 +0000228
229/**
Mark Slee31985722006-05-24 21:45:31 +0000230 * Report an error to the user. This is called yyerror for historical
231 * reasons (lex and yacc expect the error reporting routine to be called
232 * this). Call this function to report any errors to the user.
233 * yyerror takes printf style arguments.
234 *
235 * @param fmt C format string followed by additional arguments
236 */
David Reiss0babe402008-06-10 22:56:12 +0000237void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000238 va_list args;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100239 fprintf(stderr, "[ERROR:%s:%d] (last token was '%s')\n", g_curpath.c_str(), yylineno, yytext);
Mark Slee31985722006-05-24 21:45:31 +0000240
241 va_start(args, fmt);
242 vfprintf(stderr, fmt, args);
243 va_end(args);
244
245 fprintf(stderr, "\n");
246}
247
248/**
249 * Prints a debug message from the parser.
250 *
251 * @param fmt C format string followed by additional arguments
252 */
David Reiss0babe402008-06-10 22:56:12 +0000253void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000254 if (g_debug == 0) {
255 return;
256 }
257 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000258 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000259 va_start(args, fmt);
260 vprintf(fmt, args);
261 va_end(args);
262 printf("\n");
263}
264
265/**
266 * Prints a verbose output mode message
267 *
268 * @param fmt C format string followed by additional arguments
269 */
David Reiss0babe402008-06-10 22:56:12 +0000270void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 if (g_verbose == 0) {
272 return;
273 }
274 va_list args;
275 va_start(args, fmt);
276 vprintf(fmt, args);
277 va_end(args);
278}
279
280/**
281 * Prints a warning message
282 *
283 * @param fmt C format string followed by additional arguments
284 */
David Reiss0babe402008-06-10 22:56:12 +0000285void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000286 if (g_warn < level) {
287 return;
288 }
289 va_list args;
290 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000291 va_start(args, fmt);
292 vprintf(fmt, args);
293 va_end(args);
294 printf("\n");
295}
296
297/**
298 * Prints a failure message and exits
299 *
300 * @param fmt C format string followed by additional arguments
301 */
Mark Slee30152872006-11-28 01:24:07 +0000302void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000303 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000304 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000305 va_start(args, fmt);
306 vfprintf(stderr, fmt, args);
307 va_end(args);
308 printf("\n");
309 exit(1);
310}
311
312/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000313 * Converts a string filename into a thrift program name
314 */
315string program_name(string filename) {
316 string::size_type slash = filename.rfind("/");
317 if (slash != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100318 filename = filename.substr(slash + 1);
Mark Sleef0712dc2006-10-25 19:03:57 +0000319 }
320 string::size_type dot = filename.rfind(".");
321 if (dot != string::npos) {
322 filename = filename.substr(0, dot);
323 }
324 return filename;
325}
326
327/**
328 * Gets the directory path of a filename
329 */
330string directory_name(string filename) {
331 string::size_type slash = filename.rfind("/");
332 // No slash, just use the current directory
333 if (slash == string::npos) {
334 return ".";
335 }
336 return filename.substr(0, slash);
337}
338
339/**
340 * Finds the appropriate file path for the given filename
341 */
342string include_file(string filename) {
343 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000344 if (filename[0] == '/') {
345 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500346 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000347 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000348 pwarning(0, "Cannot open include file %s\n", filename.c_str());
349 return std::string();
350 }
Mark Slee2c44d202007-05-16 02:18:07 +0000351
352 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000353 struct stat finfo;
354 if (stat(rp, &finfo) == 0) {
355 return rp;
356 }
357 } else { // relative path, start searching
358 // new search path with current dir global
359 vector<string> sp = g_incl_searchpath;
360 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000361
Martin Kraemer32c66e12006-11-09 00:06:36 +0000362 // iterate through paths
363 vector<string>::iterator it;
364 for (it = sp.begin(); it != sp.end(); it++) {
365 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000366
Martin Kraemer32c66e12006-11-09 00:06:36 +0000367 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500368 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000369 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000370 continue;
371 }
Mark Slee2c44d202007-05-16 02:18:07 +0000372
Martin Kraemer32c66e12006-11-09 00:06:36 +0000373 // Stat this files
374 struct stat finfo;
375 if (stat(rp, &finfo) == 0) {
376 return rp;
377 }
378 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000379 }
Mark Slee2c44d202007-05-16 02:18:07 +0000380
Mark Sleef0712dc2006-10-25 19:03:57 +0000381 // Uh oh
382 pwarning(0, "Could not find include file %s\n", filename.c_str());
383 return std::string();
384}
385
386/**
David Reisscbd4bac2007-08-14 17:12:33 +0000387 * Clears any previously stored doctext string.
388 * Also prints a warning if we are discarding information.
389 */
390void clear_doctext() {
391 if (g_doctext != NULL) {
392 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
393 }
394 free(g_doctext);
395 g_doctext = NULL;
396}
397
398/**
Jens Geyere8379b52014-01-25 00:59:45 +0100399 * Reset program doctext information after processing a file
400 */
401void reset_program_doctext_info() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100402 if (g_program_doctext_candidate != NULL) {
Jens Geyere8379b52014-01-25 00:59:45 +0100403 free(g_program_doctext_candidate);
404 g_program_doctext_candidate = NULL;
405 }
406 g_program_doctext_lineno = 0;
407 g_program_doctext_status = INVALID;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100408 pdebug("%s", "program doctext set to INVALID");
Jens Geyere8379b52014-01-25 00:59:45 +0100409}
410
411/**
412 * We are sure the program doctext candidate is really the program doctext.
413 */
414void declare_valid_program_doctext() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100415 if ((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100416 g_program_doctext_status = ABSOLUTELY_SURE;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100417 pdebug("%s", "program doctext set to ABSOLUTELY_SURE");
Jens Geyer813749d2014-01-31 23:42:57 +0100418 } else {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100419 g_program_doctext_status = NO_PROGRAM_DOCTEXT;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100420 pdebug("%s", "program doctext set to NO_PROGRAM_DOCTEXT");
Jens Geyere8379b52014-01-25 00:59:45 +0100421 }
422}
423
424/**
David Reiss1ac05802007-07-30 22:00:27 +0000425 * Cleans up text commonly found in doxygen-like comments
426 *
427 * Warning: if you mix tabs and spaces in a non-uniform way,
428 * you will get what you deserve.
429 */
430char* clean_up_doctext(char* doctext) {
431 // Convert to C++ string, and remove Windows's carriage returns.
432 string docstring = doctext;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100433 docstring.erase(remove(docstring.begin(), docstring.end(), '\r'), docstring.end());
David Reiss1ac05802007-07-30 22:00:27 +0000434
435 // Separate into lines.
436 vector<string> lines;
437 string::size_type pos = string::npos;
438 string::size_type last;
439 while (true) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100440 last = (pos == string::npos) ? 0 : pos + 1;
David Reiss1ac05802007-07-30 22:00:27 +0000441 pos = docstring.find('\n', last);
442 if (pos == string::npos) {
443 // First bit of cleaning. If the last line is only whitespace, drop it.
444 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
445 if (nonwhite != string::npos) {
446 lines.push_back(docstring.substr(last));
447 }
448 break;
449 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100450 lines.push_back(docstring.substr(last, pos - last));
David Reiss1ac05802007-07-30 22:00:27 +0000451 }
452
453 // A very profound docstring.
454 if (lines.empty()) {
455 return NULL;
456 }
457
458 // Clear leading whitespace from the first line.
459 pos = lines.front().find_first_not_of(" \t");
460 lines.front().erase(0, pos);
461
462 // If every nonblank line after the first has the same number of spaces/tabs,
463 // then a star, remove them.
464 bool have_prefix = true;
465 bool found_prefix = false;
466 string::size_type prefix_len = 0;
467 vector<string>::iterator l_iter;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100468 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000469 if (l_iter->empty()) {
470 continue;
471 }
472
473 pos = l_iter->find_first_not_of(" \t");
474 if (!found_prefix) {
475 if (pos != string::npos) {
476 if (l_iter->at(pos) == '*') {
477 found_prefix = true;
478 prefix_len = pos;
479 } else {
480 have_prefix = false;
481 break;
482 }
483 } else {
484 // Whitespace-only line. Truncate it.
485 l_iter->clear();
486 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100487 } else if (l_iter->size() > pos && l_iter->at(pos) == '*' && pos == prefix_len) {
David Reiss1ac05802007-07-30 22:00:27 +0000488 // 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++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100503 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000504 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;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100510 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000511 if (l_iter->empty()) {
512 continue;
513 }
514 pos = l_iter->find_first_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100515 if (pos != string::npos && (prefix_len == string::npos || pos < prefix_len)) {
David Reiss1ac05802007-07-30 22:00:27 +0000516 prefix_len = pos;
517 }
518 }
519
520 // If our prefix survived, delete it from every line.
521 if (prefix_len != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100522 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000523 l_iter->erase(0, prefix_len);
524 }
525 }
526
527 // Remove trailing whitespace from every line.
528 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
529 pos = l_iter->find_last_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100530 if (pos != string::npos && pos != l_iter->length() - 1) {
531 l_iter->erase(pos + 1);
David Reiss1ac05802007-07-30 22:00:27 +0000532 }
533 }
534
535 // If the first line is empty, remove it.
536 // Don't do this earlier because a lot of steps skip the first line.
537 if (lines.front().empty()) {
538 lines.erase(lines.begin());
539 }
540
541 // Now rejoin the lines and copy them back into doctext.
542 docstring.clear();
543 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
544 docstring += *l_iter;
545 docstring += '\n';
546 }
547
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100548 // assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
549 if (docstring.length() <= strlen(doctext)) {
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200550 strcpy(doctext, docstring.c_str());
551 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100552 free(doctext); // too short
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200553 doctext = strdup(docstring.c_str());
554 }
David Reiss1ac05802007-07-30 22:00:27 +0000555 return doctext;
556}
557
558/** Set to true to debug docstring parsing */
559static bool dump_docs = false;
560
561/**
562 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000563 * Only works for top-level definitions and the whole program doc
564 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000565 */
566void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000567 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000568 if (!progdoc.empty()) {
569 printf("Whole program doc:\n%s\n", progdoc.c_str());
570 }
David Reiss1ac05802007-07-30 22:00:27 +0000571 const vector<t_typedef*>& typedefs = program->get_typedefs();
572 vector<t_typedef*>::const_iterator t_iter;
573 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
574 t_typedef* td = *t_iter;
575 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000576 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
577 }
578 }
579 const vector<t_enum*>& enums = program->get_enums();
580 vector<t_enum*>::const_iterator e_iter;
581 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
582 t_enum* en = *e_iter;
583 if (en->has_doc()) {
584 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
585 }
586 }
587 const vector<t_const*>& consts = program->get_consts();
588 vector<t_const*>::const_iterator c_iter;
589 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
590 t_const* co = *c_iter;
591 if (co->has_doc()) {
592 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
593 }
594 }
595 const vector<t_struct*>& structs = program->get_structs();
596 vector<t_struct*>::const_iterator s_iter;
597 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
598 t_struct* st = *s_iter;
599 if (st->has_doc()) {
600 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
601 }
602 }
603 const vector<t_struct*>& xceptions = program->get_xceptions();
604 vector<t_struct*>::const_iterator x_iter;
605 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
606 t_struct* xn = *x_iter;
607 if (xn->has_doc()) {
608 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
609 }
610 }
611 const vector<t_service*>& services = program->get_services();
612 vector<t_service*>::const_iterator v_iter;
613 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
614 t_service* sv = *v_iter;
615 if (sv->has_doc()) {
616 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000617 }
618 }
619}
620
621/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000622 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000623 */
624void generate_all_fingerprints(t_program* program) {
625 const vector<t_struct*>& structs = program->get_structs();
626 vector<t_struct*>::const_iterator s_iter;
627 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
628 t_struct* st = *s_iter;
629 st->generate_fingerprint();
630 }
631
David Reissd779cbe2007-08-31 01:42:55 +0000632 const vector<t_struct*>& xceptions = program->get_xceptions();
633 vector<t_struct*>::const_iterator x_iter;
634 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
635 t_struct* st = *x_iter;
636 st->generate_fingerprint();
637 }
638
David Reiss3c5d2fd2008-02-08 21:58:06 +0000639 const vector<t_enum*>& enums = program->get_enums();
640 vector<t_enum*>::const_iterator e_iter;
641 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
642 t_enum* e = *e_iter;
643 e->generate_fingerprint();
644 }
645
David Reiss47557bc2007-09-04 21:31:04 +0000646 g_type_void->generate_fingerprint();
647
David Reiss18bf22d2007-08-28 20:49:17 +0000648 // If you want to generate fingerprints for implicit structures, start here.
649 /*
650 const vector<t_service*>& services = program->get_services();
651 vector<t_service*>::const_iterator v_iter;
652 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
653 t_service* sv = *v_iter;
654 }
655 */
656}
657
Jens Geyer6fe77e82014-03-16 16:48:53 +0200658/**
659 * Emits a warning on list<byte>, binary type is typically a much better choice.
660 */
661void check_for_list_of_bytes(t_type* list_elem_type) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100662 if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
Jens Geyer6fe77e82014-03-16 16:48:53 +0200663 t_base_type* tbase = (t_base_type*)list_elem_type;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100664 if (tbase->get_base() == t_base_type::TYPE_BYTE) {
665 pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
Jens Geyer6fe77e82014-03-16 16:48:53 +0200666 }
667 }
668}
669
David Reiss18bf22d2007-08-28 20:49:17 +0000670/**
David Reissdd08f6d2008-06-30 20:24:24 +0000671 * Prints the version number
672 */
673void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000674 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000675}
676
677/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000678 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000679 */
680void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000681 fprintf(stderr, "Usage: thrift [options] file\n\n");
682 fprintf(stderr, "Use thrift -help for a list of options\n");
683 exit(1);
684}
685
686/**
687 * Diplays the help message and then exits with an error code.
688 */
689void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000690 fprintf(stderr, "Usage: thrift [options] file\n");
691 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000692 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000693 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
694 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000695 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100696 fprintf(stderr, " (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000697 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000698 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000699 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
700 fprintf(stderr, " -strict Strict compiler warnings on\n");
701 fprintf(stderr, " -v[erbose] Verbose mode\n");
702 fprintf(stderr, " -r[ecurse] Also generate included files\n");
703 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100704 fprintf(stderr,
705 " --allow-neg-keys Allow negative field keys (Used to "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000706 "preserve protocol\n");
707 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000708 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000709 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200710 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000711 fprintf(stderr, " Keys and values are options passed to the generator.\n");
712 fprintf(stderr, " Many options will not require values.\n");
713 fprintf(stderr, "\n");
714 fprintf(stderr, "Available generators (and options):\n");
715
716 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
717 t_generator_registry::gen_map_t::iterator iter;
718 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100719 fprintf(stderr,
720 " %s (%s):\n",
721 iter->second->get_short_name().c_str(),
722 iter->second->get_long_name().c_str());
David Reissbd0db882008-02-27 01:54:51 +0000723 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
724 }
Mark Slee31985722006-05-24 21:45:31 +0000725 exit(1);
726}
727
728/**
Mark Slee30152872006-11-28 01:24:07 +0000729 * You know, when I started working on Thrift I really thought it wasn't going
730 * to become a programming language because it was just a generator and it
731 * wouldn't need runtime type information and all that jazz. But then we
732 * decided to add constants, and all of a sudden that means runtime type
733 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000734 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000735 */
736void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
737 if (type->is_void()) {
738 throw "type error: cannot declare a void const: " + name;
739 }
740
741 if (type->is_base_type()) {
742 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
743 switch (tbase) {
744 case t_base_type::TYPE_STRING:
745 if (value->get_type() != t_const_value::CV_STRING) {
746 throw "type error: const \"" + name + "\" was declared as string";
747 }
748 break;
749 case t_base_type::TYPE_BOOL:
750 if (value->get_type() != t_const_value::CV_INTEGER) {
751 throw "type error: const \"" + name + "\" was declared as bool";
752 }
753 break;
754 case t_base_type::TYPE_BYTE:
755 if (value->get_type() != t_const_value::CV_INTEGER) {
756 throw "type error: const \"" + name + "\" was declared as byte";
757 }
758 break;
759 case t_base_type::TYPE_I16:
760 if (value->get_type() != t_const_value::CV_INTEGER) {
761 throw "type error: const \"" + name + "\" was declared as i16";
762 }
763 break;
764 case t_base_type::TYPE_I32:
765 if (value->get_type() != t_const_value::CV_INTEGER) {
766 throw "type error: const \"" + name + "\" was declared as i32";
767 }
768 break;
769 case t_base_type::TYPE_I64:
770 if (value->get_type() != t_const_value::CV_INTEGER) {
771 throw "type error: const \"" + name + "\" was declared as i64";
772 }
773 break;
774 case t_base_type::TYPE_DOUBLE:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100775 if (value->get_type() != t_const_value::CV_INTEGER
776 && value->get_type() != t_const_value::CV_DOUBLE) {
Mark Slee30152872006-11-28 01:24:07 +0000777 throw "type error: const \"" + name + "\" was declared as double";
778 }
779 break;
780 default:
David Reissdd7796f2007-08-28 21:09:06 +0000781 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000782 }
783 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000784 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000785 throw "type error: const \"" + name + "\" was declared as enum";
786 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000787
Bryan Duxbury1606f252010-11-24 00:25:57 +0000788 // see if there's a dot in the identifier
789 std::string name_portion = value->get_identifier_name();
790
Bryan Duxbury2d804702009-12-18 19:41:11 +0000791 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
792 vector<t_enum_value*>::const_iterator c_iter;
793 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000794
Bryan Duxbury1606f252010-11-24 00:25:57 +0000795 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000796 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000797 found = true;
798 break;
799 }
800 }
801 if (!found) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100802 throw "type error: const " + name + " was declared as type " + type->get_name()
803 + " which is an enum, but " + value->get_identifier()
804 + " is not a valid value for that enum";
Bryan Duxbury2d804702009-12-18 19:41:11 +0000805 }
Mark Slee30152872006-11-28 01:24:07 +0000806 } else if (type->is_struct() || type->is_xception()) {
807 if (value->get_type() != t_const_value::CV_MAP) {
808 throw "type error: const \"" + name + "\" was declared as struct/xception";
809 }
810 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
811 vector<t_field*>::const_iterator f_iter;
812
813 const map<t_const_value*, t_const_value*>& val = value->get_map();
814 map<t_const_value*, t_const_value*>::const_iterator v_iter;
815 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
816 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
817 throw "type error: " + name + " struct key must be string";
818 }
819 t_type* field_type = NULL;
820 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
821 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
822 field_type = (*f_iter)->get_type();
823 }
824 }
825 if (field_type == NULL) {
826 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
827 }
828
829 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
830 }
831 } else if (type->is_map()) {
832 t_type* k_type = ((t_map*)type)->get_key_type();
833 t_type* v_type = ((t_map*)type)->get_val_type();
834 const map<t_const_value*, t_const_value*>& val = value->get_map();
835 map<t_const_value*, t_const_value*>::const_iterator v_iter;
836 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
837 validate_const_rec(name + "<key>", k_type, v_iter->first);
838 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000839 }
Mark Slee30152872006-11-28 01:24:07 +0000840 } else if (type->is_list() || type->is_set()) {
841 t_type* e_type;
842 if (type->is_list()) {
843 e_type = ((t_list*)type)->get_elem_type();
844 } else {
845 e_type = ((t_set*)type)->get_elem_type();
846 }
847 const vector<t_const_value*>& val = value->get_list();
848 vector<t_const_value*>::const_iterator v_iter;
849 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
850 validate_const_rec(name + "<elem>", e_type, *v_iter);
851 }
852 }
853}
854
855/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200856 * Check simple identifier names
857 * It's easier to do it this way instead of rewriting the whole grammar etc.
858 */
859void validate_simple_identifier(const char* identifier) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100860 string name(identifier);
861 if (name.find(".") != string::npos) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200862 yyerror("Identifier %s can't have a dot.", identifier);
863 exit(1);
864 }
865}
866
867/**
Mark Slee30152872006-11-28 01:24:07 +0000868 * Check the type of the parsed const information against its declared type
869 */
870void validate_const_type(t_const* c) {
871 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
872}
873
874/**
Mark Slee7ff32452007-02-01 05:26:18 +0000875 * Check the type of a default value assigned to a field.
876 */
877void validate_field_value(t_field* field, t_const_value* cv) {
878 validate_const_rec(field->get_name(), field->get_type(), cv);
879}
880
881/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000882 * Check that all the elements of a throws block are actually exceptions.
883 */
884bool validate_throws(t_struct* throws) {
885 const vector<t_field*>& members = throws->get_members();
886 vector<t_field*>::const_iterator m_iter;
887 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000888 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000889 return false;
890 }
891 }
892 return true;
893}
894
895/**
Jens Geyer03d49442013-09-04 22:34:41 +0200896 * Skips UTF-8 BOM if there is one
897 */
898bool skip_utf8_bom(FILE* f) {
899
900 // pretty straightforward, but works
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100901 if (fgetc(f) == 0xEF) {
902 if (fgetc(f) == 0xBB) {
903 if (fgetc(f) == 0xBF) {
Jens Geyer03d49442013-09-04 22:34:41 +0200904 return true;
Roger Meier4f4b15b2014-11-05 16:51:04 +0100905 }
906 }
907 }
908
909 rewind(f);
Jens Geyer03d49442013-09-04 22:34:41 +0200910 return false;
911}
912
913/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000914 * Parses a program
915 */
Mark Slee2c44d202007-05-16 02:18:07 +0000916void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000917 // Get scope file path
918 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000919
Mark Sleef0712dc2006-10-25 19:03:57 +0000920 // Set current dir global, which is used in the include_file function
921 g_curdir = directory_name(path);
922 g_curpath = path;
923
924 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200925 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000926 yyin = fopen(path.c_str(), "r");
927 if (yyin == 0) {
928 failure("Could not open input file: \"%s\"", path.c_str());
929 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100930 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200931 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100932
Mark Sleef0712dc2006-10-25 19:03:57 +0000933 // Create new scope and scan for includes
934 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000935 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000936 g_program = program;
937 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000938 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000939 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000940 if (yyparse() != 0) {
941 failure("Parser error during include pass.");
942 }
943 } catch (string x) {
944 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000945 }
946 fclose(yyin);
947
948 // Recursively parse all the include programs
949 vector<t_program*>& includes = program->get_includes();
950 vector<t_program*>::iterator iter;
951 for (iter = includes.begin(); iter != includes.end(); ++iter) {
952 parse(*iter, program);
953 }
954
Jens Geyere8379b52014-01-25 00:59:45 +0100955 // reset program doctext status before parsing a new file
956 reset_program_doctext_info();
957
David Reiss204420f2008-01-11 20:59:03 +0000958 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000959 g_parse_mode = PROGRAM;
960 g_program = program;
961 g_scope = program->scope();
962 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
963 g_parent_prefix = program->get_name() + ".";
964 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200965
966 // Open the file
967 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000968 yyin = fopen(path.c_str(), "r");
969 if (yyin == 0) {
970 failure("Could not open input file: \"%s\"", path.c_str());
971 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100972 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200973 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100974
Mark Sleef0712dc2006-10-25 19:03:57 +0000975 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000976 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000977 try {
978 if (yyparse() != 0) {
979 failure("Parser error during types pass.");
980 }
981 } catch (string x) {
982 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000983 }
984 fclose(yyin);
985}
986
987/**
988 * Generate code
989 */
David Reissbd0db882008-02-27 01:54:51 +0000990void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000991 // Oooohh, recursive code generation, hot!!
992 if (gen_recurse) {
993 const vector<t_program*>& includes = program->get_includes();
994 for (size_t i = 0; i < includes.size(); ++i) {
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100995 // Propagate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000996 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000997
David Reissbd0db882008-02-27 01:54:51 +0000998 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000999 }
1000 }
1001
1002 // Generate code!
1003 try {
1004 pverbose("Program: %s\n", program->get_path().c_str());
1005
Jens Geyer83767a72013-09-23 22:09:12 +02001006 // Compute fingerprints. - not anymore, we do it on the fly now
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001007 // generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +00001008
David Reiss1ac05802007-07-30 22:00:27 +00001009 if (dump_docs) {
1010 dump_docstrings(program);
1011 }
David Reissbd0db882008-02-27 01:54:51 +00001012
1013 vector<string>::const_iterator iter;
1014 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1015 t_generator* generator = t_generator_registry::get_generator(program, *iter);
1016
1017 if (generator == NULL) {
1018 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1019 } else {
1020 pverbose("Generating \"%s\"\n", iter->c_str());
1021 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001022 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001023 }
1024 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001025 } catch (string s) {
1026 printf("Error: %s\n", s.c_str());
1027 } catch (const char* exc) {
1028 printf("Error: %s\n", exc);
1029 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001030}
1031
1032/**
Mark Sleef5377b32006-10-10 01:42:59 +00001033 * Parse it up.. then spit it back out, in pretty much every language. Alright
1034 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001035 */
1036int main(int argc, char** argv) {
1037 int i;
dweatherford65b70752007-10-31 02:18:14 +00001038 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001039 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001040
Mark Sleeb15a68b2006-06-07 06:46:24 +00001041 // Setup time string
1042 time_t now = time(NULL);
1043 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001044
Mark Sleef0712dc2006-10-25 19:03:57 +00001045 // Check for necessary arguments, you gotta have at least a filename and
1046 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001047 if (argc < 2) {
1048 usage();
1049 }
Mark Slee31985722006-05-24 21:45:31 +00001050
David Reissbd0db882008-02-27 01:54:51 +00001051 vector<string> generator_strings;
1052
David Reiss9cc2c132008-02-27 01:54:47 +00001053 // Set the current path to a dummy value to make warning messages clearer.
1054 g_curpath = "arguments";
1055
Mark Sleef5377b32006-10-10 01:42:59 +00001056 // Hacky parameter handling... I didn't feel like using a library sorry!
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001057 for (i = 1; i < argc - 1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001058 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001059
Mark Sleefdbee812006-09-27 18:50:48 +00001060 arg = strtok(argv[i], " ");
1061 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001062 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001063 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001064 ++arg;
1065 }
1066
Jake Farrell2fd8a152012-09-29 00:26:36 +00001067 if (strcmp(arg, "-help") == 0) {
1068 help();
1069 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001070 version();
jfarrell70969422013-09-09 20:33:38 -04001071 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001072 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001073 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001074 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001075 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001076 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001077 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001078 g_warn = 2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001079 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001080 g_verbose = 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001081 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001082 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001083 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1084 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001085 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1086 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001087 } else if (strcmp(arg, "-gen") == 0) {
1088 arg = argv[++i];
1089 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001090 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001091 usage();
1092 }
1093 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001094 } else if (strcmp(arg, "-I") == 0) {
1095 // An argument of "-I\ asdf" is invalid and has unknown results
1096 arg = argv[++i];
1097
1098 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001099 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001100 usage();
1101 }
1102 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001103 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1104 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001105 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001106 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001107 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001108 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001109 }
dweatherford65b70752007-10-31 02:18:14 +00001110 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001111
Ben Craige9576752013-10-11 08:19:16 -05001112#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001113 // strip out trailing \ on Windows
1114 int last = out_path.length() - 1;
1115 if (out_path[last] == '\\') {
David Reiss204420f2008-01-11 20:59:03 +00001116 out_path.erase(last);
1117 }
1118#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001119 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001120 return -1;
Mark Sleefdbee812006-09-27 18:50:48 +00001121 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001122 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001123 usage();
1124 }
1125
1126 // Tokenize more
1127 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001128 }
1129 }
Mark Slee2c44d202007-05-16 02:18:07 +00001130
Jake Farrell2fd8a152012-09-29 00:26:36 +00001131 // display help
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001132 if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001133 help();
1134 }
1135
David Reissdd08f6d2008-06-30 20:24:24 +00001136 // if you're asking for version, you have a right not to pass a file
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001137 if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001138 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001139 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001140 }
1141
Mark Sleef0712dc2006-10-25 19:03:57 +00001142 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001143 if (generator_strings.empty()) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001144 fprintf(stderr, "No output language(s) specified\n");
Mark Sleeb15a68b2006-06-07 06:46:24 +00001145 usage();
1146 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001147
1148 // Real-pathify it
Ben Craige9576752013-10-11 08:19:16 -05001149 char rp[THRIFT_PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001150 if (argv[i] == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001151 fprintf(stderr, "Missing file name\n");
David Reiss5245f402008-06-10 22:56:26 +00001152 usage();
1153 }
David Reiss204420f2008-01-11 20:59:03 +00001154 if (saferealpath(argv[i], rp) == NULL) {
1155 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001156 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001157 string input_file(rp);
1158
Mark Sleef5377b32006-10-10 01:42:59 +00001159 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001160 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001161 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001162 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001163 }
kholst76f2c882008-01-16 02:47:41 +00001164
David Reiss4b6a3c72008-02-27 22:28:12 +00001165 // Compute the cpp include prefix.
1166 // infer this from the filename passed in
1167 string input_filename = argv[i];
1168 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001169
David Reiss4b6a3c72008-02-27 22:28:12 +00001170 string::size_type last_slash = string::npos;
1171 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1172 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001173 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001174
David Reiss4b6a3c72008-02-27 22:28:12 +00001175 program->set_include_prefix(include_prefix);
1176
Mark Sleef0712dc2006-10-25 19:03:57 +00001177 // Initialize global types
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001178 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
Mark Sleef0712dc2006-10-25 19:03:57 +00001179 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001180 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1181 ((t_base_type*)g_type_binary)->set_binary(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001182 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Sleeb6200d82007-01-19 19:14:36 +00001183 ((t_base_type*)g_type_slist)->set_string_list(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001184 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1185 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1186 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1187 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1188 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Sleef0712dc2006-10-25 19:03:57 +00001189 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001190
Mark Sleef5377b32006-10-10 01:42:59 +00001191 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001192 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001193
David Reiss9cc2c132008-02-27 01:54:47 +00001194 // The current path is not really relevant when we are doing generation.
1195 // Reset the variable to make warning messages clearer.
1196 g_curpath = "generation";
1197 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1198 // That is what shows up during argument parsing.
1199 yylineno = 1;
1200
Mark Sleef0712dc2006-10-25 19:03:57 +00001201 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001202 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001203
Mark Sleef0712dc2006-10-25 19:03:57 +00001204 // Clean up. Who am I kidding... this program probably orphans heap memory
1205 // all over the place, but who cares because it is about to exit and it is
1206 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001207
Mark Sleef0712dc2006-10-25 19:03:57 +00001208 delete program;
1209 delete g_type_void;
1210 delete g_type_string;
1211 delete g_type_bool;
1212 delete g_type_byte;
1213 delete g_type_i16;
1214 delete g_type_i32;
1215 delete g_type_i64;
1216 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001217
1218 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001219 return 0;
1220}