blob: b9f7513547a79384dc76b9f26903fb0e9fbbb8bc [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Slee31985722006-05-24 21:45:31 +000020/**
21 * thrift - a lightweight cross-language rpc/serialization tool
22 *
23 * This file contains the main compiler engine for Thrift, which invokes the
24 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +000025 * code for each language lives in a file by the language name under the
26 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +000027 *
Mark Slee31985722006-05-24 21:45:31 +000028 */
29
David Reissf10984b2008-03-27 21:39:52 +000030#include <cassert>
Mark Slee31985722006-05-24 21:45:31 +000031#include <stdlib.h>
32#include <stdio.h>
33#include <stdarg.h>
David Reiss5ad12602010-08-31 16:51:30 +000034#include <time.h>
Mark Slee31985722006-05-24 21:45:31 +000035#include <string>
David Reiss739cbe22008-04-15 05:44:00 +000036#include <algorithm>
Mark Sleef0712dc2006-10-25 19:03:57 +000037#include <sys/types.h>
38#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000039#include <errno.h>
David Reissab55ed52008-06-11 01:17:00 +000040#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000041
David Reiss204420f2008-01-11 20:59:03 +000042#ifdef MINGW
43# include <windows.h> /* for GetFullPathName */
David Reiss204420f2008-01-11 20:59:03 +000044#endif
45
Mark Sleef0712dc2006-10-25 19:03:57 +000046// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000047#include "globals.h"
48
49#include "main.h"
50#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000051#include "parse/t_scope.h"
David Reissbbbbe882009-02-17 20:27:48 +000052#include "generate/t_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000053
David Reissdd08f6d2008-06-30 20:24:24 +000054#include "version.h"
55
Mark Slee31985722006-05-24 21:45:31 +000056using namespace std;
57
Mark Sleef5377b32006-10-10 01:42:59 +000058/**
59 * Global program tree
60 */
Mark Slee31985722006-05-24 21:45:31 +000061t_program* g_program;
62
Mark Sleef5377b32006-10-10 01:42:59 +000063/**
Mark Sleef0712dc2006-10-25 19:03:57 +000064 * Global types
65 */
66
67t_type* g_type_void;
68t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000069t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000070t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000071t_type* g_type_bool;
72t_type* g_type_byte;
73t_type* g_type_i16;
74t_type* g_type_i32;
75t_type* g_type_i64;
76t_type* g_type_double;
77
78/**
79 * Global scope
80 */
81t_scope* g_scope;
82
83/**
84 * Parent scope to also parse types
85 */
86t_scope* g_parent_scope;
87
88/**
89 * Prefix for putting types in parent scope
90 */
91string g_parent_prefix;
92
93/**
94 * Parsing pass
95 */
96PARSE_MODE g_parse_mode;
97
98/**
99 * Current directory of file being parsed
100 */
101string g_curdir;
102
103/**
104 * Current file being parsed
105 */
106string g_curpath;
107
108/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000109 * Search path for inclusions
110 */
Mark Slee2329a832006-11-09 00:23:30 +0000111vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000112
113/**
Mark Sleef5377b32006-10-10 01:42:59 +0000114 * Global debug state
115 */
Mark Slee31985722006-05-24 21:45:31 +0000116int g_debug = 0;
117
Mark Sleef5377b32006-10-10 01:42:59 +0000118/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000119 * Strictness level
120 */
121int g_strict = 127;
122
123/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000124 * Warning level
125 */
126int g_warn = 1;
127
128/**
129 * Verbose output
130 */
131int g_verbose = 0;
132
133/**
Mark Sleef5377b32006-10-10 01:42:59 +0000134 * Global time string
135 */
Mark Slee31985722006-05-24 21:45:31 +0000136char* g_time_str;
137
Mark Slee31985722006-05-24 21:45:31 +0000138/**
David Reisscbd4bac2007-08-14 17:12:33 +0000139 * The last parsed doctext comment.
140 */
141char* g_doctext;
142
143/**
144 * The location of the last parsed doctext comment.
145 */
146int g_doctext_lineno;
147
148/**
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000149 * Whether or not negative field keys are accepted.
150 */
151int g_allow_neg_field_keys;
152
153/**
Roger Meier887ff752011-08-19 11:25:39 +0000154 * Whether or not 64-bit constants will generate a warning.
155 */
156int g_allow_64bit_consts = 0;
157
158/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000159 * Flags to control code generation
160 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000161bool gen_recurse = false;
162
163/**
David Reiss204420f2008-01-11 20:59:03 +0000164 * MinGW doesn't have realpath, so use fallback implementation in that case,
165 * otherwise this just calls through to realpath
166 */
167char *saferealpath(const char *path, char *resolved_path) {
168#ifdef MINGW
169 char buf[MAX_PATH];
170 char* basename;
171 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
172 if (len == 0 || len > MAX_PATH - 1){
173 strcpy(resolved_path, path);
174 } else {
David Reiss204420f2008-01-11 20:59:03 +0000175 strcpy(resolved_path, buf);
176 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000177
178 // Replace backslashes with forward slashes so the
179 // rest of the code behaves correctly.
180 size_t resolved_len = strlen(resolved_path);
181 for (size_t i = 0; i < resolved_len; i++) {
182 if (resolved_path[i] == '\\') {
183 resolved_path[i] = '/';
184 }
185 }
David Reiss204420f2008-01-11 20:59:03 +0000186 return resolved_path;
187#else
188 return realpath(path, resolved_path);
189#endif
190}
191
Roger Meier061d4a22012-10-07 11:51:00 +0000192bool check_is_directory(const char *dir_name) {
193#ifdef MINGW
194 DWORD attributes = ::GetFileAttributesA(dir_name);
195 if(attributes == INVALID_FILE_ATTRIBUTES) {
196 fprintf(stderr, "Output directory %s is unusable: GetLastError() = %ld\n", dir_name, GetLastError());
197 return false;
198 }
199 if((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
200 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
201 return false;
202 }
203 return true;
204#else
205 struct stat sb;
206 if (stat(dir_name, &sb) < 0) {
207 fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
208 return false;
209 }
210 if (! S_ISDIR(sb.st_mode)) {
211 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
212 return false;
213 }
214 return true;
215#endif
216}
David Reiss204420f2008-01-11 20:59:03 +0000217
218/**
Mark Slee31985722006-05-24 21:45:31 +0000219 * Report an error to the user. This is called yyerror for historical
220 * reasons (lex and yacc expect the error reporting routine to be called
221 * this). Call this function to report any errors to the user.
222 * yyerror takes printf style arguments.
223 *
224 * @param fmt C format string followed by additional arguments
225 */
David Reiss0babe402008-06-10 22:56:12 +0000226void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000227 va_list args;
228 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000229 "[ERROR:%s:%d] (last token was '%s')\n",
230 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000231 yylineno,
232 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000233
234 va_start(args, fmt);
235 vfprintf(stderr, fmt, args);
236 va_end(args);
237
238 fprintf(stderr, "\n");
239}
240
241/**
242 * Prints a debug message from the parser.
243 *
244 * @param fmt C format string followed by additional arguments
245 */
David Reiss0babe402008-06-10 22:56:12 +0000246void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000247 if (g_debug == 0) {
248 return;
249 }
250 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000251 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000252 va_start(args, fmt);
253 vprintf(fmt, args);
254 va_end(args);
255 printf("\n");
256}
257
258/**
259 * Prints a verbose output mode message
260 *
261 * @param fmt C format string followed by additional arguments
262 */
David Reiss0babe402008-06-10 22:56:12 +0000263void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000264 if (g_verbose == 0) {
265 return;
266 }
267 va_list args;
268 va_start(args, fmt);
269 vprintf(fmt, args);
270 va_end(args);
271}
272
273/**
274 * Prints a warning message
275 *
276 * @param fmt C format string followed by additional arguments
277 */
David Reiss0babe402008-06-10 22:56:12 +0000278void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000279 if (g_warn < level) {
280 return;
281 }
282 va_list args;
283 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000284 va_start(args, fmt);
285 vprintf(fmt, args);
286 va_end(args);
287 printf("\n");
288}
289
290/**
291 * Prints a failure message and exits
292 *
293 * @param fmt C format string followed by additional arguments
294 */
Mark Slee30152872006-11-28 01:24:07 +0000295void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000296 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000297 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000298 va_start(args, fmt);
299 vfprintf(stderr, fmt, args);
300 va_end(args);
301 printf("\n");
302 exit(1);
303}
304
305/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000306 * Converts a string filename into a thrift program name
307 */
308string program_name(string filename) {
309 string::size_type slash = filename.rfind("/");
310 if (slash != string::npos) {
311 filename = filename.substr(slash+1);
312 }
313 string::size_type dot = filename.rfind(".");
314 if (dot != string::npos) {
315 filename = filename.substr(0, dot);
316 }
317 return filename;
318}
319
320/**
321 * Gets the directory path of a filename
322 */
323string directory_name(string filename) {
324 string::size_type slash = filename.rfind("/");
325 // No slash, just use the current directory
326 if (slash == string::npos) {
327 return ".";
328 }
329 return filename.substr(0, slash);
330}
331
332/**
333 * Finds the appropriate file path for the given filename
334 */
335string include_file(string filename) {
336 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000337 if (filename[0] == '/') {
338 // Realpath!
339 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000340 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000341 pwarning(0, "Cannot open include file %s\n", filename.c_str());
342 return std::string();
343 }
Mark Slee2c44d202007-05-16 02:18:07 +0000344
345 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000346 struct stat finfo;
347 if (stat(rp, &finfo) == 0) {
348 return rp;
349 }
350 } else { // relative path, start searching
351 // new search path with current dir global
352 vector<string> sp = g_incl_searchpath;
353 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000354
Martin Kraemer32c66e12006-11-09 00:06:36 +0000355 // iterate through paths
356 vector<string>::iterator it;
357 for (it = sp.begin(); it != sp.end(); it++) {
358 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000359
Martin Kraemer32c66e12006-11-09 00:06:36 +0000360 // Realpath!
361 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000362 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000363 continue;
364 }
Mark Slee2c44d202007-05-16 02:18:07 +0000365
Martin Kraemer32c66e12006-11-09 00:06:36 +0000366 // Stat this files
367 struct stat finfo;
368 if (stat(rp, &finfo) == 0) {
369 return rp;
370 }
371 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000372 }
Mark Slee2c44d202007-05-16 02:18:07 +0000373
Mark Sleef0712dc2006-10-25 19:03:57 +0000374 // Uh oh
375 pwarning(0, "Could not find include file %s\n", filename.c_str());
376 return std::string();
377}
378
379/**
David Reisscbd4bac2007-08-14 17:12:33 +0000380 * Clears any previously stored doctext string.
381 * Also prints a warning if we are discarding information.
382 */
383void clear_doctext() {
384 if (g_doctext != NULL) {
385 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
386 }
387 free(g_doctext);
388 g_doctext = NULL;
389}
390
391/**
David Reiss1ac05802007-07-30 22:00:27 +0000392 * Cleans up text commonly found in doxygen-like comments
393 *
394 * Warning: if you mix tabs and spaces in a non-uniform way,
395 * you will get what you deserve.
396 */
397char* clean_up_doctext(char* doctext) {
398 // Convert to C++ string, and remove Windows's carriage returns.
399 string docstring = doctext;
400 docstring.erase(
401 remove(docstring.begin(), docstring.end(), '\r'),
402 docstring.end());
403
404 // Separate into lines.
405 vector<string> lines;
406 string::size_type pos = string::npos;
407 string::size_type last;
408 while (true) {
409 last = (pos == string::npos) ? 0 : pos+1;
410 pos = docstring.find('\n', last);
411 if (pos == string::npos) {
412 // First bit of cleaning. If the last line is only whitespace, drop it.
413 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
414 if (nonwhite != string::npos) {
415 lines.push_back(docstring.substr(last));
416 }
417 break;
418 }
419 lines.push_back(docstring.substr(last, pos-last));
420 }
421
422 // A very profound docstring.
423 if (lines.empty()) {
424 return NULL;
425 }
426
427 // Clear leading whitespace from the first line.
428 pos = lines.front().find_first_not_of(" \t");
429 lines.front().erase(0, pos);
430
431 // If every nonblank line after the first has the same number of spaces/tabs,
432 // then a star, remove them.
433 bool have_prefix = true;
434 bool found_prefix = false;
435 string::size_type prefix_len = 0;
436 vector<string>::iterator l_iter;
437 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
438 if (l_iter->empty()) {
439 continue;
440 }
441
442 pos = l_iter->find_first_not_of(" \t");
443 if (!found_prefix) {
444 if (pos != string::npos) {
445 if (l_iter->at(pos) == '*') {
446 found_prefix = true;
447 prefix_len = pos;
448 } else {
449 have_prefix = false;
450 break;
451 }
452 } else {
453 // Whitespace-only line. Truncate it.
454 l_iter->clear();
455 }
456 } else if (l_iter->size() > pos
457 && l_iter->at(pos) == '*'
458 && pos == prefix_len) {
459 // Business as usual.
460 } else if (pos == string::npos) {
461 // Whitespace-only line. Let's truncate it for them.
462 l_iter->clear();
463 } else {
464 // The pattern has been broken.
465 have_prefix = false;
466 break;
467 }
468 }
469
470 // If our prefix survived, delete it from every line.
471 if (have_prefix) {
472 // Get the star too.
473 prefix_len++;
474 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
475 l_iter->erase(0, prefix_len);
476 }
477 }
478
479 // Now delete the minimum amount of leading whitespace from each line.
480 prefix_len = string::npos;
481 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
482 if (l_iter->empty()) {
483 continue;
484 }
485 pos = l_iter->find_first_not_of(" \t");
486 if (pos != string::npos
487 && (prefix_len == string::npos || pos < prefix_len)) {
488 prefix_len = pos;
489 }
490 }
491
492 // If our prefix survived, delete it from every line.
493 if (prefix_len != string::npos) {
494 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
495 l_iter->erase(0, prefix_len);
496 }
497 }
498
499 // Remove trailing whitespace from every line.
500 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
501 pos = l_iter->find_last_not_of(" \t");
502 if (pos != string::npos && pos != l_iter->length()-1) {
503 l_iter->erase(pos+1);
504 }
505 }
506
507 // If the first line is empty, remove it.
508 // Don't do this earlier because a lot of steps skip the first line.
509 if (lines.front().empty()) {
510 lines.erase(lines.begin());
511 }
512
513 // Now rejoin the lines and copy them back into doctext.
514 docstring.clear();
515 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
516 docstring += *l_iter;
517 docstring += '\n';
518 }
519
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200520 //assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
521 if(docstring.length() <= strlen(doctext)) {
522 strcpy(doctext, docstring.c_str());
523 } else {
524 free(doctext); // too short
525 doctext = strdup(docstring.c_str());
526 }
David Reiss1ac05802007-07-30 22:00:27 +0000527 return doctext;
528}
529
530/** Set to true to debug docstring parsing */
531static bool dump_docs = false;
532
533/**
534 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000535 * Only works for top-level definitions and the whole program doc
536 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000537 */
538void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000539 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000540 if (!progdoc.empty()) {
541 printf("Whole program doc:\n%s\n", progdoc.c_str());
542 }
David Reiss1ac05802007-07-30 22:00:27 +0000543 const vector<t_typedef*>& typedefs = program->get_typedefs();
544 vector<t_typedef*>::const_iterator t_iter;
545 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
546 t_typedef* td = *t_iter;
547 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000548 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
549 }
550 }
551 const vector<t_enum*>& enums = program->get_enums();
552 vector<t_enum*>::const_iterator e_iter;
553 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
554 t_enum* en = *e_iter;
555 if (en->has_doc()) {
556 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
557 }
558 }
559 const vector<t_const*>& consts = program->get_consts();
560 vector<t_const*>::const_iterator c_iter;
561 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
562 t_const* co = *c_iter;
563 if (co->has_doc()) {
564 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
565 }
566 }
567 const vector<t_struct*>& structs = program->get_structs();
568 vector<t_struct*>::const_iterator s_iter;
569 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
570 t_struct* st = *s_iter;
571 if (st->has_doc()) {
572 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
573 }
574 }
575 const vector<t_struct*>& xceptions = program->get_xceptions();
576 vector<t_struct*>::const_iterator x_iter;
577 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
578 t_struct* xn = *x_iter;
579 if (xn->has_doc()) {
580 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
581 }
582 }
583 const vector<t_service*>& services = program->get_services();
584 vector<t_service*>::const_iterator v_iter;
585 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
586 t_service* sv = *v_iter;
587 if (sv->has_doc()) {
588 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000589 }
590 }
591}
592
593/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000594 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000595 */
596void generate_all_fingerprints(t_program* program) {
597 const vector<t_struct*>& structs = program->get_structs();
598 vector<t_struct*>::const_iterator s_iter;
599 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
600 t_struct* st = *s_iter;
601 st->generate_fingerprint();
602 }
603
David Reissd779cbe2007-08-31 01:42:55 +0000604 const vector<t_struct*>& xceptions = program->get_xceptions();
605 vector<t_struct*>::const_iterator x_iter;
606 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
607 t_struct* st = *x_iter;
608 st->generate_fingerprint();
609 }
610
David Reiss3c5d2fd2008-02-08 21:58:06 +0000611 const vector<t_enum*>& enums = program->get_enums();
612 vector<t_enum*>::const_iterator e_iter;
613 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
614 t_enum* e = *e_iter;
615 e->generate_fingerprint();
616 }
617
David Reiss47557bc2007-09-04 21:31:04 +0000618 g_type_void->generate_fingerprint();
619
David Reiss18bf22d2007-08-28 20:49:17 +0000620 // If you want to generate fingerprints for implicit structures, start here.
621 /*
622 const vector<t_service*>& services = program->get_services();
623 vector<t_service*>::const_iterator v_iter;
624 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
625 t_service* sv = *v_iter;
626 }
627 */
628}
629
630/**
David Reissdd08f6d2008-06-30 20:24:24 +0000631 * Prints the version number
632 */
633void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000634 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000635}
636
637/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000638 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000639 */
640void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000641 fprintf(stderr, "Usage: thrift [options] file\n\n");
642 fprintf(stderr, "Use thrift -help for a list of options\n");
643 exit(1);
644}
645
646/**
647 * Diplays the help message and then exits with an error code.
648 */
649void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000650 fprintf(stderr, "Usage: thrift [options] file\n");
651 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000652 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000653 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
654 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000655 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
656 fprintf(stderr," (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000657 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000658 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000659 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
660 fprintf(stderr, " -strict Strict compiler warnings on\n");
661 fprintf(stderr, " -v[erbose] Verbose mode\n");
662 fprintf(stderr, " -r[ecurse] Also generate included files\n");
663 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000664 fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
665 "preserve protocol\n");
666 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000667 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000668 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
669 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
670 fprintf(stderr, " Keys and values are options passed to the generator.\n");
671 fprintf(stderr, " Many options will not require values.\n");
672 fprintf(stderr, "\n");
673 fprintf(stderr, "Available generators (and options):\n");
674
675 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
676 t_generator_registry::gen_map_t::iterator iter;
677 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
678 fprintf(stderr, " %s (%s):\n",
679 iter->second->get_short_name().c_str(),
680 iter->second->get_long_name().c_str());
681 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
682 }
Mark Slee31985722006-05-24 21:45:31 +0000683 exit(1);
684}
685
686/**
Mark Slee30152872006-11-28 01:24:07 +0000687 * You know, when I started working on Thrift I really thought it wasn't going
688 * to become a programming language because it was just a generator and it
689 * wouldn't need runtime type information and all that jazz. But then we
690 * decided to add constants, and all of a sudden that means runtime type
691 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000692 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000693 */
694void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
695 if (type->is_void()) {
696 throw "type error: cannot declare a void const: " + name;
697 }
698
699 if (type->is_base_type()) {
700 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
701 switch (tbase) {
702 case t_base_type::TYPE_STRING:
703 if (value->get_type() != t_const_value::CV_STRING) {
704 throw "type error: const \"" + name + "\" was declared as string";
705 }
706 break;
707 case t_base_type::TYPE_BOOL:
708 if (value->get_type() != t_const_value::CV_INTEGER) {
709 throw "type error: const \"" + name + "\" was declared as bool";
710 }
711 break;
712 case t_base_type::TYPE_BYTE:
713 if (value->get_type() != t_const_value::CV_INTEGER) {
714 throw "type error: const \"" + name + "\" was declared as byte";
715 }
716 break;
717 case t_base_type::TYPE_I16:
718 if (value->get_type() != t_const_value::CV_INTEGER) {
719 throw "type error: const \"" + name + "\" was declared as i16";
720 }
721 break;
722 case t_base_type::TYPE_I32:
723 if (value->get_type() != t_const_value::CV_INTEGER) {
724 throw "type error: const \"" + name + "\" was declared as i32";
725 }
726 break;
727 case t_base_type::TYPE_I64:
728 if (value->get_type() != t_const_value::CV_INTEGER) {
729 throw "type error: const \"" + name + "\" was declared as i64";
730 }
731 break;
732 case t_base_type::TYPE_DOUBLE:
733 if (value->get_type() != t_const_value::CV_INTEGER &&
734 value->get_type() != t_const_value::CV_DOUBLE) {
735 throw "type error: const \"" + name + "\" was declared as double";
736 }
737 break;
738 default:
David Reissdd7796f2007-08-28 21:09:06 +0000739 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000740 }
741 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000742 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000743 throw "type error: const \"" + name + "\" was declared as enum";
744 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000745
Bryan Duxbury1606f252010-11-24 00:25:57 +0000746 // see if there's a dot in the identifier
747 std::string name_portion = value->get_identifier_name();
748
Bryan Duxbury2d804702009-12-18 19:41:11 +0000749 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
750 vector<t_enum_value*>::const_iterator c_iter;
751 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000752
Bryan Duxbury1606f252010-11-24 00:25:57 +0000753 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000754 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000755 found = true;
756 break;
757 }
758 }
759 if (!found) {
760 throw "type error: const " + name + " was declared as type "
761 + type->get_name() + " which is an enum, but "
762 + value->get_identifier() + " is not a valid value for that enum";
763 }
Mark Slee30152872006-11-28 01:24:07 +0000764 } else if (type->is_struct() || type->is_xception()) {
765 if (value->get_type() != t_const_value::CV_MAP) {
766 throw "type error: const \"" + name + "\" was declared as struct/xception";
767 }
768 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
769 vector<t_field*>::const_iterator f_iter;
770
771 const map<t_const_value*, t_const_value*>& val = value->get_map();
772 map<t_const_value*, t_const_value*>::const_iterator v_iter;
773 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
774 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
775 throw "type error: " + name + " struct key must be string";
776 }
777 t_type* field_type = NULL;
778 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
779 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
780 field_type = (*f_iter)->get_type();
781 }
782 }
783 if (field_type == NULL) {
784 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
785 }
786
787 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
788 }
789 } else if (type->is_map()) {
790 t_type* k_type = ((t_map*)type)->get_key_type();
791 t_type* v_type = ((t_map*)type)->get_val_type();
792 const map<t_const_value*, t_const_value*>& val = value->get_map();
793 map<t_const_value*, t_const_value*>::const_iterator v_iter;
794 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
795 validate_const_rec(name + "<key>", k_type, v_iter->first);
796 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000797 }
Mark Slee30152872006-11-28 01:24:07 +0000798 } else if (type->is_list() || type->is_set()) {
799 t_type* e_type;
800 if (type->is_list()) {
801 e_type = ((t_list*)type)->get_elem_type();
802 } else {
803 e_type = ((t_set*)type)->get_elem_type();
804 }
805 const vector<t_const_value*>& val = value->get_list();
806 vector<t_const_value*>::const_iterator v_iter;
807 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
808 validate_const_rec(name + "<elem>", e_type, *v_iter);
809 }
810 }
811}
812
813/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200814 * Check simple identifier names
815 * It's easier to do it this way instead of rewriting the whole grammar etc.
816 */
817void validate_simple_identifier(const char* identifier) {
818 string name( identifier);
819 if( name.find(".") != string::npos) {
820 yyerror("Identifier %s can't have a dot.", identifier);
821 exit(1);
822 }
823}
824
825/**
Mark Slee30152872006-11-28 01:24:07 +0000826 * Check the type of the parsed const information against its declared type
827 */
828void validate_const_type(t_const* c) {
829 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
830}
831
832/**
Mark Slee7ff32452007-02-01 05:26:18 +0000833 * Check the type of a default value assigned to a field.
834 */
835void validate_field_value(t_field* field, t_const_value* cv) {
836 validate_const_rec(field->get_name(), field->get_type(), cv);
837}
838
839/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000840 * Check that all the elements of a throws block are actually exceptions.
841 */
842bool validate_throws(t_struct* throws) {
843 const vector<t_field*>& members = throws->get_members();
844 vector<t_field*>::const_iterator m_iter;
845 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000846 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000847 return false;
848 }
849 }
850 return true;
851}
852
853/**
Jens Geyer03d49442013-09-04 22:34:41 +0200854 * Skips UTF-8 BOM if there is one
855 */
856bool skip_utf8_bom(FILE* f) {
857
858 // pretty straightforward, but works
859 if( fgetc(f) == 0xEF) {
860 if( fgetc(f) == 0xBB) {
861 if( fgetc(f) == 0xBF) {
862 return true;
863 }
864 }
865 }
866
867 rewind(f);
868 return false;
869}
870
871/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000872 * Parses a program
873 */
Mark Slee2c44d202007-05-16 02:18:07 +0000874void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000875 // Get scope file path
876 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000877
Mark Sleef0712dc2006-10-25 19:03:57 +0000878 // Set current dir global, which is used in the include_file function
879 g_curdir = directory_name(path);
880 g_curpath = path;
881
882 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200883 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000884 yyin = fopen(path.c_str(), "r");
885 if (yyin == 0) {
886 failure("Could not open input file: \"%s\"", path.c_str());
887 }
Jens Geyer03d49442013-09-04 22:34:41 +0200888 if( skip_utf8_bom( yyin))
889 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
890
Mark Sleef0712dc2006-10-25 19:03:57 +0000891 // Create new scope and scan for includes
892 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000893 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000894 g_program = program;
895 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000896 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000897 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000898 if (yyparse() != 0) {
899 failure("Parser error during include pass.");
900 }
901 } catch (string x) {
902 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000903 }
904 fclose(yyin);
905
906 // Recursively parse all the include programs
907 vector<t_program*>& includes = program->get_includes();
908 vector<t_program*>::iterator iter;
909 for (iter = includes.begin(); iter != includes.end(); ++iter) {
910 parse(*iter, program);
911 }
912
David Reiss204420f2008-01-11 20:59:03 +0000913 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000914 g_parse_mode = PROGRAM;
915 g_program = program;
916 g_scope = program->scope();
917 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
918 g_parent_prefix = program->get_name() + ".";
919 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200920
921 // Open the file
922 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000923 yyin = fopen(path.c_str(), "r");
924 if (yyin == 0) {
925 failure("Could not open input file: \"%s\"", path.c_str());
926 }
Jens Geyer03d49442013-09-04 22:34:41 +0200927 if( skip_utf8_bom( yyin))
928 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
929
Mark Sleef0712dc2006-10-25 19:03:57 +0000930 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000931 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000932 try {
933 if (yyparse() != 0) {
934 failure("Parser error during types pass.");
935 }
936 } catch (string x) {
937 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000938 }
939 fclose(yyin);
940}
941
942/**
943 * Generate code
944 */
David Reissbd0db882008-02-27 01:54:51 +0000945void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000946 // Oooohh, recursive code generation, hot!!
947 if (gen_recurse) {
948 const vector<t_program*>& includes = program->get_includes();
949 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000950 // Propogate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000951 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000952
David Reissbd0db882008-02-27 01:54:51 +0000953 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000954 }
955 }
956
957 // Generate code!
958 try {
959 pverbose("Program: %s\n", program->get_path().c_str());
960
Jens Geyer83767a72013-09-23 22:09:12 +0200961 // Compute fingerprints. - not anymore, we do it on the fly now
962 //generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +0000963
David Reiss1ac05802007-07-30 22:00:27 +0000964 if (dump_docs) {
965 dump_docstrings(program);
966 }
David Reissbd0db882008-02-27 01:54:51 +0000967
968 vector<string>::const_iterator iter;
969 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
970 t_generator* generator = t_generator_registry::get_generator(program, *iter);
971
972 if (generator == NULL) {
973 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
974 } else {
975 pverbose("Generating \"%s\"\n", iter->c_str());
976 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000977 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000978 }
979 }
980
Mark Sleef0712dc2006-10-25 19:03:57 +0000981 } catch (string s) {
982 printf("Error: %s\n", s.c_str());
983 } catch (const char* exc) {
984 printf("Error: %s\n", exc);
985 }
986
987}
988
989/**
Mark Sleef5377b32006-10-10 01:42:59 +0000990 * Parse it up.. then spit it back out, in pretty much every language. Alright
991 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000992 */
993int main(int argc, char** argv) {
994 int i;
dweatherford65b70752007-10-31 02:18:14 +0000995 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000996 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +0000997
Mark Sleeb15a68b2006-06-07 06:46:24 +0000998 // Setup time string
999 time_t now = time(NULL);
1000 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001001
Mark Sleef0712dc2006-10-25 19:03:57 +00001002 // Check for necessary arguments, you gotta have at least a filename and
1003 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001004 if (argc < 2) {
1005 usage();
1006 }
Mark Slee31985722006-05-24 21:45:31 +00001007
David Reissbd0db882008-02-27 01:54:51 +00001008 vector<string> generator_strings;
1009
David Reiss9cc2c132008-02-27 01:54:47 +00001010 // Set the current path to a dummy value to make warning messages clearer.
1011 g_curpath = "arguments";
1012
Mark Sleef5377b32006-10-10 01:42:59 +00001013 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +00001014 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001015 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001016
Mark Sleefdbee812006-09-27 18:50:48 +00001017 arg = strtok(argv[i], " ");
1018 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001019 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001020 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001021 ++arg;
1022 }
1023
Jake Farrell2fd8a152012-09-29 00:26:36 +00001024 if (strcmp(arg, "-help") == 0) {
1025 help();
1026 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001027 version();
jfarrell70969422013-09-09 20:33:38 -04001028 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001029 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001030 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001031 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001032 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001033 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001034 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001035 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +00001036 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001037 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001038 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001039 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001040 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1041 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001042 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1043 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001044 } else if (strcmp(arg, "-gen") == 0) {
1045 arg = argv[++i];
1046 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001047 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001048 usage();
1049 }
1050 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001051 } else if (strcmp(arg, "-I") == 0) {
1052 // An argument of "-I\ asdf" is invalid and has unknown results
1053 arg = argv[++i];
1054
1055 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001056 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001057 usage();
1058 }
1059 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001060 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1061 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001062 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001063 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001064 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001065 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001066 }
dweatherford65b70752007-10-31 02:18:14 +00001067 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001068
1069#ifdef MINGW
1070 //strip out trailing \ on Windows
1071 int last = out_path.length()-1;
1072 if (out_path[last] == '\\')
1073 {
1074 out_path.erase(last);
1075 }
1076#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001077 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001078 return -1;
Mark Sleefdbee812006-09-27 18:50:48 +00001079 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001080 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001081 usage();
1082 }
1083
1084 // Tokenize more
1085 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001086 }
1087 }
Mark Slee2c44d202007-05-16 02:18:07 +00001088
Jake Farrell2fd8a152012-09-29 00:26:36 +00001089 // display help
1090 if ((strcmp(argv[argc-1], "-help") == 0) || (strcmp(argv[argc-1], "--help") == 0)) {
1091 help();
1092 }
1093
David Reissdd08f6d2008-06-30 20:24:24 +00001094 // if you're asking for version, you have a right not to pass a file
Jake Farrell2fd8a152012-09-29 00:26:36 +00001095 if ((strcmp(argv[argc-1], "-version") == 0) || (strcmp(argv[argc-1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001096 version();
1097 exit(1);
1098 }
1099
Mark Sleef0712dc2006-10-25 19:03:57 +00001100 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001101 if (generator_strings.empty()) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001102 fprintf(stderr, "No output language(s) specified\n");
Mark Sleeb15a68b2006-06-07 06:46:24 +00001103 usage();
1104 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001105
1106 // Real-pathify it
1107 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001108 if (argv[i] == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001109 fprintf(stderr, "Missing file name\n");
David Reiss5245f402008-06-10 22:56:26 +00001110 usage();
1111 }
David Reiss204420f2008-01-11 20:59:03 +00001112 if (saferealpath(argv[i], rp) == NULL) {
1113 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001114 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001115 string input_file(rp);
1116
Mark Sleef5377b32006-10-10 01:42:59 +00001117 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001118 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001119 if (out_path.size()) {
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001120 program->set_out_path(out_path, out_path_is_absolute);
dweatherford65b70752007-10-31 02:18:14 +00001121 }
kholst76f2c882008-01-16 02:47:41 +00001122
David Reiss4b6a3c72008-02-27 22:28:12 +00001123 // Compute the cpp include prefix.
1124 // infer this from the filename passed in
1125 string input_filename = argv[i];
1126 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001127
David Reiss4b6a3c72008-02-27 22:28:12 +00001128 string::size_type last_slash = string::npos;
1129 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1130 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001131 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001132
David Reiss4b6a3c72008-02-27 22:28:12 +00001133 program->set_include_prefix(include_prefix);
1134
Mark Sleef0712dc2006-10-25 19:03:57 +00001135 // Initialize global types
1136 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1137 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001138 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1139 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001140 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1141 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001142 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1143 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1144 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1145 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1146 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1147 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001148
Mark Sleef5377b32006-10-10 01:42:59 +00001149 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001150 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001151
David Reiss9cc2c132008-02-27 01:54:47 +00001152 // The current path is not really relevant when we are doing generation.
1153 // Reset the variable to make warning messages clearer.
1154 g_curpath = "generation";
1155 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1156 // That is what shows up during argument parsing.
1157 yylineno = 1;
1158
Mark Sleef0712dc2006-10-25 19:03:57 +00001159 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001160 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001161
Mark Sleef0712dc2006-10-25 19:03:57 +00001162 // Clean up. Who am I kidding... this program probably orphans heap memory
1163 // all over the place, but who cares because it is about to exit and it is
1164 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001165
Mark Sleef0712dc2006-10-25 19:03:57 +00001166 delete program;
1167 delete g_type_void;
1168 delete g_type_string;
1169 delete g_type_bool;
1170 delete g_type_byte;
1171 delete g_type_i16;
1172 delete g_type_i32;
1173 delete g_type_i64;
1174 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001175
1176 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001177 return 0;
1178}