blob: 56b5c75d8e76ef5bb0a79ecf1068a87c06f9b1b2 [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/**
kholst76f2c882008-01-16 02:47:41 +0000114 * Should C++ include statements use path prefixes for other thrift-generated
115 * header files
116 */
117bool g_cpp_use_include_prefix = false;
118
119/**
Mark Sleef5377b32006-10-10 01:42:59 +0000120 * Global debug state
121 */
Mark Slee31985722006-05-24 21:45:31 +0000122int g_debug = 0;
123
Mark Sleef5377b32006-10-10 01:42:59 +0000124/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000125 * Strictness level
126 */
127int g_strict = 127;
128
129/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000130 * Warning level
131 */
132int g_warn = 1;
133
134/**
135 * Verbose output
136 */
137int g_verbose = 0;
138
139/**
Mark Sleef5377b32006-10-10 01:42:59 +0000140 * Global time string
141 */
Mark Slee31985722006-05-24 21:45:31 +0000142char* g_time_str;
143
Mark Slee31985722006-05-24 21:45:31 +0000144/**
David Reisscbd4bac2007-08-14 17:12:33 +0000145 * The last parsed doctext comment.
146 */
147char* g_doctext;
148
149/**
150 * The location of the last parsed doctext comment.
151 */
152int g_doctext_lineno;
153
154/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000155 * Flags to control code generation
156 */
157bool gen_cpp = false;
David Reiss6495adb2007-12-18 03:37:30 +0000158bool gen_dense = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000159bool gen_java = false;
Mark Slee01a9f882007-08-31 00:55:28 +0000160bool gen_javabean = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000161bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000162bool gen_py = false;
David Reiss6495adb2007-12-18 03:37:30 +0000163bool gen_py_newstyle = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000164bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000165bool gen_php = false;
166bool gen_phpi = false;
Mark Slee5b743072007-11-13 04:00:29 +0000167bool gen_phps = true;
Mark Slee09f69e02007-11-17 00:32:36 +0000168bool gen_phpa = false;
Mark Sleeb22df7c2007-11-28 02:39:59 +0000169bool gen_phpo = false;
Mark Slee756b1d12007-07-06 00:30:21 +0000170bool gen_rest = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000171bool gen_perl = false;
David Reiss9f2a5d72008-06-11 01:15:45 +0000172bool gen_erl = false;
Christopher Pirob97b89d2007-09-18 00:07:42 +0000173bool gen_ocaml = false;
iproctorff8eb922007-07-25 19:06:13 +0000174bool gen_hs = false;
Mark Slee7e9eea42007-09-10 21:00:23 +0000175bool gen_cocoa = false;
David Reiss7f42bcf2008-01-11 20:59:12 +0000176bool gen_csharp = false;
Mark Sleeefd37f12007-11-20 05:13:09 +0000177bool gen_st = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000178bool gen_recurse = false;
179
180/**
David Reiss204420f2008-01-11 20:59:03 +0000181 * MinGW doesn't have realpath, so use fallback implementation in that case,
182 * otherwise this just calls through to realpath
183 */
184char *saferealpath(const char *path, char *resolved_path) {
185#ifdef MINGW
186 char buf[MAX_PATH];
187 char* basename;
188 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
189 if (len == 0 || len > MAX_PATH - 1){
190 strcpy(resolved_path, path);
191 } else {
192 CharLowerBuff(buf, len);
193 strcpy(resolved_path, buf);
194 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000195
196 // Replace backslashes with forward slashes so the
197 // rest of the code behaves correctly.
198 size_t resolved_len = strlen(resolved_path);
199 for (size_t i = 0; i < resolved_len; i++) {
200 if (resolved_path[i] == '\\') {
201 resolved_path[i] = '/';
202 }
203 }
David Reiss204420f2008-01-11 20:59:03 +0000204 return resolved_path;
205#else
206 return realpath(path, resolved_path);
207#endif
208}
209
210
211/**
Mark Slee31985722006-05-24 21:45:31 +0000212 * Report an error to the user. This is called yyerror for historical
213 * reasons (lex and yacc expect the error reporting routine to be called
214 * this). Call this function to report any errors to the user.
215 * yyerror takes printf style arguments.
216 *
217 * @param fmt C format string followed by additional arguments
218 */
David Reiss0babe402008-06-10 22:56:12 +0000219void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000220 va_list args;
221 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000222 "[ERROR:%s:%d] (last token was '%s')\n",
223 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000224 yylineno,
225 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000226
227 va_start(args, fmt);
228 vfprintf(stderr, fmt, args);
229 va_end(args);
230
231 fprintf(stderr, "\n");
232}
233
234/**
235 * Prints a debug message from the parser.
236 *
237 * @param fmt C format string followed by additional arguments
238 */
David Reiss0babe402008-06-10 22:56:12 +0000239void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000240 if (g_debug == 0) {
241 return;
242 }
243 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000244 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000245 va_start(args, fmt);
246 vprintf(fmt, args);
247 va_end(args);
248 printf("\n");
249}
250
251/**
252 * Prints a verbose output mode message
253 *
254 * @param fmt C format string followed by additional arguments
255 */
David Reiss0babe402008-06-10 22:56:12 +0000256void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000257 if (g_verbose == 0) {
258 return;
259 }
260 va_list args;
261 va_start(args, fmt);
262 vprintf(fmt, args);
263 va_end(args);
264}
265
266/**
267 * Prints a warning message
268 *
269 * @param fmt C format string followed by additional arguments
270 */
David Reiss0babe402008-06-10 22:56:12 +0000271void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000272 if (g_warn < level) {
273 return;
274 }
275 va_list args;
276 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000277 va_start(args, fmt);
278 vprintf(fmt, args);
279 va_end(args);
280 printf("\n");
281}
282
283/**
284 * Prints a failure message and exits
285 *
286 * @param fmt C format string followed by additional arguments
287 */
Mark Slee30152872006-11-28 01:24:07 +0000288void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000289 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000290 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000291 va_start(args, fmt);
292 vfprintf(stderr, fmt, args);
293 va_end(args);
294 printf("\n");
295 exit(1);
296}
297
298/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000299 * Converts a string filename into a thrift program name
300 */
301string program_name(string filename) {
302 string::size_type slash = filename.rfind("/");
303 if (slash != string::npos) {
304 filename = filename.substr(slash+1);
305 }
306 string::size_type dot = filename.rfind(".");
307 if (dot != string::npos) {
308 filename = filename.substr(0, dot);
309 }
310 return filename;
311}
312
313/**
314 * Gets the directory path of a filename
315 */
316string directory_name(string filename) {
317 string::size_type slash = filename.rfind("/");
318 // No slash, just use the current directory
319 if (slash == string::npos) {
320 return ".";
321 }
322 return filename.substr(0, slash);
323}
324
325/**
326 * Finds the appropriate file path for the given filename
327 */
328string include_file(string filename) {
329 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000330 if (filename[0] == '/') {
331 // Realpath!
332 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000333 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000334 pwarning(0, "Cannot open include file %s\n", filename.c_str());
335 return std::string();
336 }
Mark Slee2c44d202007-05-16 02:18:07 +0000337
338 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000339 struct stat finfo;
340 if (stat(rp, &finfo) == 0) {
341 return rp;
342 }
343 } else { // relative path, start searching
344 // new search path with current dir global
345 vector<string> sp = g_incl_searchpath;
346 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000347
Martin Kraemer32c66e12006-11-09 00:06:36 +0000348 // iterate through paths
349 vector<string>::iterator it;
350 for (it = sp.begin(); it != sp.end(); it++) {
351 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000352
Martin Kraemer32c66e12006-11-09 00:06:36 +0000353 // Realpath!
354 char rp[PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000355 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000356 continue;
357 }
Mark Slee2c44d202007-05-16 02:18:07 +0000358
Martin Kraemer32c66e12006-11-09 00:06:36 +0000359 // Stat this files
360 struct stat finfo;
361 if (stat(rp, &finfo) == 0) {
362 return rp;
363 }
364 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000365 }
Mark Slee2c44d202007-05-16 02:18:07 +0000366
Mark Sleef0712dc2006-10-25 19:03:57 +0000367 // Uh oh
368 pwarning(0, "Could not find include file %s\n", filename.c_str());
369 return std::string();
370}
371
372/**
David Reisscbd4bac2007-08-14 17:12:33 +0000373 * Clears any previously stored doctext string.
374 * Also prints a warning if we are discarding information.
375 */
376void clear_doctext() {
377 if (g_doctext != NULL) {
378 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
379 }
380 free(g_doctext);
381 g_doctext = NULL;
382}
383
384/**
David Reiss1ac05802007-07-30 22:00:27 +0000385 * Cleans up text commonly found in doxygen-like comments
386 *
387 * Warning: if you mix tabs and spaces in a non-uniform way,
388 * you will get what you deserve.
389 */
390char* clean_up_doctext(char* doctext) {
391 // Convert to C++ string, and remove Windows's carriage returns.
392 string docstring = doctext;
393 docstring.erase(
394 remove(docstring.begin(), docstring.end(), '\r'),
395 docstring.end());
396
397 // Separate into lines.
398 vector<string> lines;
399 string::size_type pos = string::npos;
400 string::size_type last;
401 while (true) {
402 last = (pos == string::npos) ? 0 : pos+1;
403 pos = docstring.find('\n', last);
404 if (pos == string::npos) {
405 // First bit of cleaning. If the last line is only whitespace, drop it.
406 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
407 if (nonwhite != string::npos) {
408 lines.push_back(docstring.substr(last));
409 }
410 break;
411 }
412 lines.push_back(docstring.substr(last, pos-last));
413 }
414
415 // A very profound docstring.
416 if (lines.empty()) {
417 return NULL;
418 }
419
420 // Clear leading whitespace from the first line.
421 pos = lines.front().find_first_not_of(" \t");
422 lines.front().erase(0, pos);
423
424 // If every nonblank line after the first has the same number of spaces/tabs,
425 // then a star, remove them.
426 bool have_prefix = true;
427 bool found_prefix = false;
428 string::size_type prefix_len = 0;
429 vector<string>::iterator l_iter;
430 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
431 if (l_iter->empty()) {
432 continue;
433 }
434
435 pos = l_iter->find_first_not_of(" \t");
436 if (!found_prefix) {
437 if (pos != string::npos) {
438 if (l_iter->at(pos) == '*') {
439 found_prefix = true;
440 prefix_len = pos;
441 } else {
442 have_prefix = false;
443 break;
444 }
445 } else {
446 // Whitespace-only line. Truncate it.
447 l_iter->clear();
448 }
449 } else if (l_iter->size() > pos
450 && l_iter->at(pos) == '*'
451 && pos == prefix_len) {
452 // Business as usual.
453 } else if (pos == string::npos) {
454 // Whitespace-only line. Let's truncate it for them.
455 l_iter->clear();
456 } else {
457 // The pattern has been broken.
458 have_prefix = false;
459 break;
460 }
461 }
462
463 // If our prefix survived, delete it from every line.
464 if (have_prefix) {
465 // Get the star too.
466 prefix_len++;
467 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
468 l_iter->erase(0, prefix_len);
469 }
470 }
471
472 // Now delete the minimum amount of leading whitespace from each line.
473 prefix_len = string::npos;
474 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
475 if (l_iter->empty()) {
476 continue;
477 }
478 pos = l_iter->find_first_not_of(" \t");
479 if (pos != string::npos
480 && (prefix_len == string::npos || pos < prefix_len)) {
481 prefix_len = pos;
482 }
483 }
484
485 // If our prefix survived, delete it from every line.
486 if (prefix_len != string::npos) {
487 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
488 l_iter->erase(0, prefix_len);
489 }
490 }
491
492 // Remove trailing whitespace from every line.
493 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
494 pos = l_iter->find_last_not_of(" \t");
495 if (pos != string::npos && pos != l_iter->length()-1) {
496 l_iter->erase(pos+1);
497 }
498 }
499
500 // If the first line is empty, remove it.
501 // Don't do this earlier because a lot of steps skip the first line.
502 if (lines.front().empty()) {
503 lines.erase(lines.begin());
504 }
505
506 // Now rejoin the lines and copy them back into doctext.
507 docstring.clear();
508 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
509 docstring += *l_iter;
510 docstring += '\n';
511 }
512
513 assert(docstring.length() <= strlen(doctext));
514 strcpy(doctext, docstring.c_str());
515 return doctext;
516}
517
518/** Set to true to debug docstring parsing */
519static bool dump_docs = false;
520
521/**
522 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000523 * Only works for top-level definitions and the whole program doc
524 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000525 */
526void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000527 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000528 if (!progdoc.empty()) {
529 printf("Whole program doc:\n%s\n", progdoc.c_str());
530 }
David Reiss1ac05802007-07-30 22:00:27 +0000531 const vector<t_typedef*>& typedefs = program->get_typedefs();
532 vector<t_typedef*>::const_iterator t_iter;
533 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
534 t_typedef* td = *t_iter;
535 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000536 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
537 }
538 }
539 const vector<t_enum*>& enums = program->get_enums();
540 vector<t_enum*>::const_iterator e_iter;
541 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
542 t_enum* en = *e_iter;
543 if (en->has_doc()) {
544 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
545 }
546 }
547 const vector<t_const*>& consts = program->get_consts();
548 vector<t_const*>::const_iterator c_iter;
549 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
550 t_const* co = *c_iter;
551 if (co->has_doc()) {
552 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
553 }
554 }
555 const vector<t_struct*>& structs = program->get_structs();
556 vector<t_struct*>::const_iterator s_iter;
557 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
558 t_struct* st = *s_iter;
559 if (st->has_doc()) {
560 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
561 }
562 }
563 const vector<t_struct*>& xceptions = program->get_xceptions();
564 vector<t_struct*>::const_iterator x_iter;
565 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
566 t_struct* xn = *x_iter;
567 if (xn->has_doc()) {
568 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
569 }
570 }
571 const vector<t_service*>& services = program->get_services();
572 vector<t_service*>::const_iterator v_iter;
573 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
574 t_service* sv = *v_iter;
575 if (sv->has_doc()) {
576 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000577 }
578 }
579}
580
581/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000582 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000583 */
584void generate_all_fingerprints(t_program* program) {
585 const vector<t_struct*>& structs = program->get_structs();
586 vector<t_struct*>::const_iterator s_iter;
587 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
588 t_struct* st = *s_iter;
589 st->generate_fingerprint();
590 }
591
David Reissd779cbe2007-08-31 01:42:55 +0000592 const vector<t_struct*>& xceptions = program->get_xceptions();
593 vector<t_struct*>::const_iterator x_iter;
594 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
595 t_struct* st = *x_iter;
596 st->generate_fingerprint();
597 }
598
David Reiss3c5d2fd2008-02-08 21:58:06 +0000599 const vector<t_enum*>& enums = program->get_enums();
600 vector<t_enum*>::const_iterator e_iter;
601 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
602 t_enum* e = *e_iter;
603 e->generate_fingerprint();
604 }
605
David Reiss47557bc2007-09-04 21:31:04 +0000606 g_type_void->generate_fingerprint();
607
David Reiss18bf22d2007-08-28 20:49:17 +0000608 // If you want to generate fingerprints for implicit structures, start here.
609 /*
610 const vector<t_service*>& services = program->get_services();
611 vector<t_service*>::const_iterator v_iter;
612 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
613 t_service* sv = *v_iter;
614 }
615 */
616}
617
618/**
David Reissdd08f6d2008-06-30 20:24:24 +0000619 * Prints the version number
620 */
621void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000622 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000623}
624
625/**
Mark Slee31985722006-05-24 21:45:31 +0000626 * Diplays the usage message and then exits with an error code.
627 */
628void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000629 fprintf(stderr, "Usage: thrift [options] file\n");
630 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000631 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000632 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
633 fprintf(stderr, " (default: current directory)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000634 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000635 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000636 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
637 fprintf(stderr, " -strict Strict compiler warnings on\n");
638 fprintf(stderr, " -v[erbose] Verbose mode\n");
639 fprintf(stderr, " -r[ecurse] Also generate included files\n");
640 fprintf(stderr, " -debug Parse debug trace to stdout\n");
David Reissbd0db882008-02-27 01:54:51 +0000641 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
642 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
643 fprintf(stderr, " Keys and values are options passed to the generator.\n");
644 fprintf(stderr, " Many options will not require values.\n");
645 fprintf(stderr, "\n");
646 fprintf(stderr, "Available generators (and options):\n");
647
648 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
649 t_generator_registry::gen_map_t::iterator iter;
650 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
651 fprintf(stderr, " %s (%s):\n",
652 iter->second->get_short_name().c_str(),
653 iter->second->get_long_name().c_str());
654 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
655 }
Mark Slee31985722006-05-24 21:45:31 +0000656 exit(1);
657}
658
659/**
Mark Slee30152872006-11-28 01:24:07 +0000660 * You know, when I started working on Thrift I really thought it wasn't going
661 * to become a programming language because it was just a generator and it
662 * wouldn't need runtime type information and all that jazz. But then we
663 * decided to add constants, and all of a sudden that means runtime type
664 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000665 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000666 */
667void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
668 if (type->is_void()) {
669 throw "type error: cannot declare a void const: " + name;
670 }
671
672 if (type->is_base_type()) {
673 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
674 switch (tbase) {
675 case t_base_type::TYPE_STRING:
676 if (value->get_type() != t_const_value::CV_STRING) {
677 throw "type error: const \"" + name + "\" was declared as string";
678 }
679 break;
680 case t_base_type::TYPE_BOOL:
681 if (value->get_type() != t_const_value::CV_INTEGER) {
682 throw "type error: const \"" + name + "\" was declared as bool";
683 }
684 break;
685 case t_base_type::TYPE_BYTE:
686 if (value->get_type() != t_const_value::CV_INTEGER) {
687 throw "type error: const \"" + name + "\" was declared as byte";
688 }
689 break;
690 case t_base_type::TYPE_I16:
691 if (value->get_type() != t_const_value::CV_INTEGER) {
692 throw "type error: const \"" + name + "\" was declared as i16";
693 }
694 break;
695 case t_base_type::TYPE_I32:
696 if (value->get_type() != t_const_value::CV_INTEGER) {
697 throw "type error: const \"" + name + "\" was declared as i32";
698 }
699 break;
700 case t_base_type::TYPE_I64:
701 if (value->get_type() != t_const_value::CV_INTEGER) {
702 throw "type error: const \"" + name + "\" was declared as i64";
703 }
704 break;
705 case t_base_type::TYPE_DOUBLE:
706 if (value->get_type() != t_const_value::CV_INTEGER &&
707 value->get_type() != t_const_value::CV_DOUBLE) {
708 throw "type error: const \"" + name + "\" was declared as double";
709 }
710 break;
711 default:
David Reissdd7796f2007-08-28 21:09:06 +0000712 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000713 }
714 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000715 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000716 throw "type error: const \"" + name + "\" was declared as enum";
717 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000718
719 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
720 vector<t_enum_value*>::const_iterator c_iter;
721 bool found = false;
722 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000723 size_t sub_index = value->get_identifier().find('.');
724 if (sub_index == string::npos) {
725 throw "error: identifier " + value->get_identifier() + " is unqualified!";
726 }
727 std::string name_portion = value->get_identifier().substr(sub_index+1);
728
729 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000730 found = true;
731 break;
732 }
733 }
734 if (!found) {
735 throw "type error: const " + name + " was declared as type "
736 + type->get_name() + " which is an enum, but "
737 + value->get_identifier() + " is not a valid value for that enum";
738 }
Mark Slee30152872006-11-28 01:24:07 +0000739 } else if (type->is_struct() || type->is_xception()) {
740 if (value->get_type() != t_const_value::CV_MAP) {
741 throw "type error: const \"" + name + "\" was declared as struct/xception";
742 }
743 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
744 vector<t_field*>::const_iterator f_iter;
745
746 const map<t_const_value*, t_const_value*>& val = value->get_map();
747 map<t_const_value*, t_const_value*>::const_iterator v_iter;
748 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
749 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
750 throw "type error: " + name + " struct key must be string";
751 }
752 t_type* field_type = NULL;
753 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
754 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
755 field_type = (*f_iter)->get_type();
756 }
757 }
758 if (field_type == NULL) {
759 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
760 }
761
762 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
763 }
764 } else if (type->is_map()) {
765 t_type* k_type = ((t_map*)type)->get_key_type();
766 t_type* v_type = ((t_map*)type)->get_val_type();
767 const map<t_const_value*, t_const_value*>& val = value->get_map();
768 map<t_const_value*, t_const_value*>::const_iterator v_iter;
769 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
770 validate_const_rec(name + "<key>", k_type, v_iter->first);
771 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000772 }
Mark Slee30152872006-11-28 01:24:07 +0000773 } else if (type->is_list() || type->is_set()) {
774 t_type* e_type;
775 if (type->is_list()) {
776 e_type = ((t_list*)type)->get_elem_type();
777 } else {
778 e_type = ((t_set*)type)->get_elem_type();
779 }
780 const vector<t_const_value*>& val = value->get_list();
781 vector<t_const_value*>::const_iterator v_iter;
782 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
783 validate_const_rec(name + "<elem>", e_type, *v_iter);
784 }
785 }
786}
787
788/**
789 * Check the type of the parsed const information against its declared type
790 */
791void validate_const_type(t_const* c) {
792 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
793}
794
795/**
Mark Slee7ff32452007-02-01 05:26:18 +0000796 * Check the type of a default value assigned to a field.
797 */
798void validate_field_value(t_field* field, t_const_value* cv) {
799 validate_const_rec(field->get_name(), field->get_type(), cv);
800}
801
802/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000803 * Check that all the elements of a throws block are actually exceptions.
804 */
805bool validate_throws(t_struct* throws) {
806 const vector<t_field*>& members = throws->get_members();
807 vector<t_field*>::const_iterator m_iter;
808 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
809 if (!(*m_iter)->get_type()->is_xception()) {
810 return false;
811 }
812 }
813 return true;
814}
815
816/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000817 * Parses a program
818 */
Mark Slee2c44d202007-05-16 02:18:07 +0000819void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000820 // Get scope file path
821 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000822
Mark Sleef0712dc2006-10-25 19:03:57 +0000823 // Set current dir global, which is used in the include_file function
824 g_curdir = directory_name(path);
825 g_curpath = path;
826
827 // Open the file
828 yyin = fopen(path.c_str(), "r");
829 if (yyin == 0) {
830 failure("Could not open input file: \"%s\"", path.c_str());
831 }
832
833 // Create new scope and scan for includes
834 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000835 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000836 g_program = program;
837 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000838 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000839 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000840 if (yyparse() != 0) {
841 failure("Parser error during include pass.");
842 }
843 } catch (string x) {
844 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000845 }
846 fclose(yyin);
847
848 // Recursively parse all the include programs
849 vector<t_program*>& includes = program->get_includes();
850 vector<t_program*>::iterator iter;
851 for (iter = includes.begin(); iter != includes.end(); ++iter) {
852 parse(*iter, program);
853 }
854
David Reiss204420f2008-01-11 20:59:03 +0000855 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000856 g_parse_mode = PROGRAM;
857 g_program = program;
858 g_scope = program->scope();
859 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
860 g_parent_prefix = program->get_name() + ".";
861 g_curpath = path;
862 yyin = fopen(path.c_str(), "r");
863 if (yyin == 0) {
864 failure("Could not open input file: \"%s\"", path.c_str());
865 }
866 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000867 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000868 try {
869 if (yyparse() != 0) {
870 failure("Parser error during types pass.");
871 }
872 } catch (string x) {
873 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000874 }
875 fclose(yyin);
876}
877
878/**
879 * Generate code
880 */
David Reissbd0db882008-02-27 01:54:51 +0000881void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000882 // Oooohh, recursive code generation, hot!!
883 if (gen_recurse) {
884 const vector<t_program*>& includes = program->get_includes();
885 for (size_t i = 0; i < includes.size(); ++i) {
dweatherford65b70752007-10-31 02:18:14 +0000886 // Propogate output path from parent to child programs
887 includes[i]->set_out_path(program->get_out_path());
Mark Slee5b743072007-11-13 04:00:29 +0000888
David Reissbd0db882008-02-27 01:54:51 +0000889 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000890 }
891 }
892
893 // Generate code!
894 try {
895 pverbose("Program: %s\n", program->get_path().c_str());
896
David Reiss18bf22d2007-08-28 20:49:17 +0000897 // Compute fingerprints.
898 generate_all_fingerprints(program);
899
David Reiss1ac05802007-07-30 22:00:27 +0000900 if (dump_docs) {
901 dump_docstrings(program);
902 }
David Reissbd0db882008-02-27 01:54:51 +0000903
904 vector<string>::const_iterator iter;
905 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
906 t_generator* generator = t_generator_registry::get_generator(program, *iter);
907
908 if (generator == NULL) {
909 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
910 } else {
911 pverbose("Generating \"%s\"\n", iter->c_str());
912 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +0000913 delete generator;
David Reissbd0db882008-02-27 01:54:51 +0000914 }
915 }
916
Mark Sleef0712dc2006-10-25 19:03:57 +0000917 } catch (string s) {
918 printf("Error: %s\n", s.c_str());
919 } catch (const char* exc) {
920 printf("Error: %s\n", exc);
921 }
922
923}
924
925/**
Mark Sleef5377b32006-10-10 01:42:59 +0000926 * Parse it up.. then spit it back out, in pretty much every language. Alright
927 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000928 */
929int main(int argc, char** argv) {
930 int i;
dweatherford65b70752007-10-31 02:18:14 +0000931 std::string out_path;
Mark Sleef5377b32006-10-10 01:42:59 +0000932
Mark Sleeb15a68b2006-06-07 06:46:24 +0000933 // Setup time string
934 time_t now = time(NULL);
935 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000936
Mark Sleef0712dc2006-10-25 19:03:57 +0000937 // Check for necessary arguments, you gotta have at least a filename and
938 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000939 if (argc < 2) {
940 usage();
941 }
Mark Slee31985722006-05-24 21:45:31 +0000942
David Reissbd0db882008-02-27 01:54:51 +0000943 vector<string> generator_strings;
944
David Reiss9cc2c132008-02-27 01:54:47 +0000945 // Set the current path to a dummy value to make warning messages clearer.
946 g_curpath = "arguments";
947
Mark Sleef5377b32006-10-10 01:42:59 +0000948 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000949 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000950 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000951
Mark Sleefdbee812006-09-27 18:50:48 +0000952 arg = strtok(argv[i], " ");
953 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000954 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000955 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000956 ++arg;
957 }
958
David Reissdd08f6d2008-06-30 20:24:24 +0000959 if (strcmp(arg, "-version") == 0) {
960 version();
961 exit(1);
962 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000963 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000964 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000965 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000966 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000967 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +0000968 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000969 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000970 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000971 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000972 gen_recurse = true;
David Reissbd0db882008-02-27 01:54:51 +0000973 } else if (strcmp(arg, "-gen") == 0) {
974 arg = argv[++i];
975 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +0000976 fprintf(stderr, "!!! Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +0000977 usage();
978 }
979 generator_strings.push_back(arg);
David Reissd779cbe2007-08-31 01:42:55 +0000980 } else if (strcmp(arg, "-dense") == 0) {
981 gen_dense = true;
Mark Slee2329a832006-11-09 00:23:30 +0000982 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000983 gen_cpp = true;
Mark Slee01a9f882007-08-31 00:55:28 +0000984 } else if (strcmp(arg, "-javabean") == 0) {
985 gen_javabean = true;
Mark Slee2329a832006-11-09 00:23:30 +0000986 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000987 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000988 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000989 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000990 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000991 gen_phpi = true;
Mark Slee5b743072007-11-13 04:00:29 +0000992 } else if (strcmp(arg, "-phps") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000993 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000994 gen_phps = true;
995 } else if (strcmp(arg, "-phpl") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000996 gen_php = true;
Mark Slee5b743072007-11-13 04:00:29 +0000997 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +0000998 } else if (strcmp(arg, "-phpa") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +0000999 gen_php = true;
1000 gen_phps = false;
Mark Slee09f69e02007-11-17 00:32:36 +00001001 gen_phpa = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001002 } else if (strcmp(arg, "-phpo") == 0) {
David Reiss6495adb2007-12-18 03:37:30 +00001003 gen_php = true;
Mark Sleeb22df7c2007-11-28 02:39:59 +00001004 gen_phpo = true;
Mark Slee756b1d12007-07-06 00:30:21 +00001005 } else if (strcmp(arg, "-rest") == 0) {
1006 gen_rest = true;
Mark Slee2329a832006-11-09 00:23:30 +00001007 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001008 gen_py = true;
David Reiss6495adb2007-12-18 03:37:30 +00001009 } else if (strcmp(arg, "-pyns") == 0) {
1010 gen_py = true;
1011 gen_py_newstyle = true;
Mark Slee6d7d5952007-01-27 01:44:22 +00001012 } else if (strcmp(arg, "-rb") == 0) {
1013 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +00001014 } else if (strcmp(arg, "-xsd") == 0) {
1015 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +00001016 } else if (strcmp(arg, "-perl") == 0) {
1017 gen_perl = true;
David Reiss9f2a5d72008-06-11 01:15:45 +00001018 } else if (strcmp(arg, "-erl") == 0) {
1019 gen_erl = true;
Christopher Pirob97b89d2007-09-18 00:07:42 +00001020 } else if (strcmp(arg, "-ocaml") == 0) {
1021 gen_ocaml = true;
iproctorff8eb922007-07-25 19:06:13 +00001022 } else if (strcmp(arg, "-hs") == 0) {
1023 gen_hs = true;
Mark Slee7e9eea42007-09-10 21:00:23 +00001024 } else if (strcmp(arg, "-cocoa") == 0) {
1025 gen_cocoa = true;
Mark Sleeefd37f12007-11-20 05:13:09 +00001026 } else if (strcmp(arg, "-st") == 0) {
1027 gen_st = true;
David Reiss7f42bcf2008-01-11 20:59:12 +00001028 } else if (strcmp(arg, "-csharp") == 0) {
1029 gen_csharp = true;
kholst76f2c882008-01-16 02:47:41 +00001030 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1031 g_cpp_use_include_prefix = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +00001032 } else if (strcmp(arg, "-I") == 0) {
1033 // An argument of "-I\ asdf" is invalid and has unknown results
1034 arg = argv[++i];
1035
1036 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001037 fprintf(stderr, "!!! Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001038 usage();
1039 }
1040 g_incl_searchpath.push_back(arg);
dweatherford65b70752007-10-31 02:18:14 +00001041 } else if (strcmp(arg, "-o") == 0) {
1042 arg = argv[++i];
1043 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001044 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001045 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001046 }
dweatherford65b70752007-10-31 02:18:14 +00001047 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001048
1049#ifdef MINGW
1050 //strip out trailing \ on Windows
1051 int last = out_path.length()-1;
1052 if (out_path[last] == '\\')
1053 {
1054 out_path.erase(last);
1055 }
1056#endif
1057
dweatherford65b70752007-10-31 02:18:14 +00001058 struct stat sb;
1059 if (stat(out_path.c_str(), &sb) < 0) {
1060 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1061 return -1;
1062 }
1063 if (! S_ISDIR(sb.st_mode)) {
1064 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1065 return -1;
1066 }
Mark Sleefdbee812006-09-27 18:50:48 +00001067 } else {
1068 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1069 usage();
1070 }
1071
1072 // Tokenize more
1073 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001074 }
1075 }
Mark Slee2c44d202007-05-16 02:18:07 +00001076
David Reissdd08f6d2008-06-30 20:24:24 +00001077 // if you're asking for version, you have a right not to pass a file
1078 if (strcmp(argv[argc-1], "-version") == 0) {
1079 version();
1080 exit(1);
1081 }
1082
David Reiss400cd4b2008-02-27 01:54:55 +00001083 // TODO(dreiss): Delete these when everyone is using the new hotness.
1084 if (gen_cpp) {
1085 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1086 string gen_string = "cpp:";
1087 if (gen_dense) {
1088 gen_string.append("dense,");
1089 }
1090 if (g_cpp_use_include_prefix) {
1091 gen_string.append("include_prefix,");
1092 }
1093 generator_strings.push_back(gen_string);
1094 }
David Reisse1404d22008-02-27 01:55:05 +00001095 if (gen_java) {
1096 pwarning(1, "-java is deprecated. Use --gen java");
1097 generator_strings.push_back("java");
1098 }
1099 if (gen_javabean) {
1100 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1101 generator_strings.push_back("java:beans");
1102 }
David Reiss861869b2008-03-27 21:41:23 +00001103 if (gen_csharp) {
1104 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1105 generator_strings.push_back("csharp");
1106 }
David Reiss558e3992008-03-27 21:41:40 +00001107 if (gen_py) {
1108 pwarning(1, "-py is deprecated. Use --gen py");
1109 generator_strings.push_back("py");
1110 }
David Reissa640cea2008-03-27 21:42:01 +00001111 if (gen_rb) {
1112 pwarning(1, "-rb is deprecated. Use --gen rb");
1113 generator_strings.push_back("rb");
1114 }
David Reiss2b386c52008-03-27 21:42:23 +00001115 if (gen_perl) {
1116 pwarning(1, "-perl is deprecated. Use --gen perl");
1117 generator_strings.push_back("perl");
1118 }
David Reissa9ea68b2009-02-17 20:28:24 +00001119 if (gen_php || gen_phpi) {
1120 pwarning(1, "-php is deprecated. Use --gen php");
1121 string gen_string = "php:";
1122 if (gen_phpi) {
1123 gen_string.append("inlined,");
1124 } else if(gen_phps) {
1125 gen_string.append("server,");
1126 } else if(gen_phpa) {
1127 gen_string.append("autoload,");
1128 } else if(gen_phpo) {
1129 gen_string.append("oop,");
1130 } else if(gen_rest) {
1131 gen_string.append("rest,");
1132 }
1133 generator_strings.push_back(gen_string);
1134 }
David Reissd01c64d2008-03-27 21:40:55 +00001135 if (gen_cocoa) {
1136 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1137 generator_strings.push_back("cocoa");
1138 }
David Reissa2047832009-02-17 20:27:54 +00001139 if (gen_erl) {
1140 pwarning(1, "-erl is deprecated. Use --gen erl");
1141 generator_strings.push_back("erl");
1142 }
David Reissa890b572008-03-27 21:40:35 +00001143 if (gen_st) {
1144 pwarning(1, "-st is deprecated. Use --gen st");
1145 generator_strings.push_back("st");
1146 }
David Reissf0521b12008-03-27 21:40:05 +00001147 if (gen_ocaml) {
1148 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1149 generator_strings.push_back("ocaml");
1150 }
David Reissaf3ab262008-03-27 21:40:16 +00001151 if (gen_hs) {
1152 pwarning(1, "-hs is deprecated. Use --gen hs");
1153 generator_strings.push_back("hs");
1154 }
David Reiss4ba67102009-02-17 20:28:06 +00001155 if (gen_xsd) {
1156 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1157 generator_strings.push_back("xsd");
1158 }
David Reisse1404d22008-02-27 01:55:05 +00001159
Mark Sleef0712dc2006-10-25 19:03:57 +00001160 // You gotta generate something!
David Reissa9ea68b2009-02-17 20:28:24 +00001161 if (generator_strings.empty()) {
Mark Sleeb15a68b2006-06-07 06:46:24 +00001162 fprintf(stderr, "!!! No output language(s) specified\n\n");
1163 usage();
1164 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001165
1166 // Real-pathify it
1167 char rp[PATH_MAX];
David Reiss5245f402008-06-10 22:56:26 +00001168 if (argv[i] == NULL) {
1169 fprintf(stderr, "!!! Missing file name\n");
1170 usage();
1171 }
David Reiss204420f2008-01-11 20:59:03 +00001172 if (saferealpath(argv[i], rp) == NULL) {
1173 failure("Could not open input file with realpath: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +00001174 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001175 string input_file(rp);
1176
Mark Sleef5377b32006-10-10 01:42:59 +00001177 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +00001178 t_program* program = new t_program(input_file);
dweatherford65b70752007-10-31 02:18:14 +00001179 if (out_path.size()) {
1180 program->set_out_path(out_path);
1181 }
kholst76f2c882008-01-16 02:47:41 +00001182
David Reiss4b6a3c72008-02-27 22:28:12 +00001183 // Compute the cpp include prefix.
1184 // infer this from the filename passed in
1185 string input_filename = argv[i];
1186 string include_prefix;
kholst76f2c882008-01-16 02:47:41 +00001187
David Reiss4b6a3c72008-02-27 22:28:12 +00001188 string::size_type last_slash = string::npos;
1189 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1190 include_prefix = input_filename.substr(0, last_slash);
kholst76f2c882008-01-16 02:47:41 +00001191 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001192
David Reiss4b6a3c72008-02-27 22:28:12 +00001193 program->set_include_prefix(include_prefix);
1194
Mark Sleef0712dc2006-10-25 19:03:57 +00001195 // Initialize global types
1196 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1197 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001198 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1199 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +00001200 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1201 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001202 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1203 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1204 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1205 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1206 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1207 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001208
Mark Sleef5377b32006-10-10 01:42:59 +00001209 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +00001210 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +00001211
David Reiss9cc2c132008-02-27 01:54:47 +00001212 // The current path is not really relevant when we are doing generation.
1213 // Reset the variable to make warning messages clearer.
1214 g_curpath = "generation";
1215 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1216 // That is what shows up during argument parsing.
1217 yylineno = 1;
1218
Mark Sleef0712dc2006-10-25 19:03:57 +00001219 // Generate it!
David Reissbd0db882008-02-27 01:54:51 +00001220 generate(program, generator_strings);
Mark Sleeb15a68b2006-06-07 06:46:24 +00001221
Mark Sleef0712dc2006-10-25 19:03:57 +00001222 // Clean up. Who am I kidding... this program probably orphans heap memory
1223 // all over the place, but who cares because it is about to exit and it is
1224 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001225
Mark Sleef0712dc2006-10-25 19:03:57 +00001226 delete program;
1227 delete g_type_void;
1228 delete g_type_string;
1229 delete g_type_bool;
1230 delete g_type_byte;
1231 delete g_type_i16;
1232 delete g_type_i32;
1233 delete g_type_i64;
1234 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001235
1236 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001237 return 0;
1238}