blob: 7a5d2d4958dd8930f99a6c38cacb80e78ce1ce88 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +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 */
19
20/**
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
25 * code for each language lives in a file by the language name under the
26 * generate/ folder, and all parse structures live in parse/
27 *
28 */
29
30#include <cassert>
31#include <stdlib.h>
32#include <stdio.h>
33#include <stdarg.h>
34#include <string>
35#include <algorithm>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <errno.h>
39#include <limits.h>
40
41#ifdef MINGW
42# include <windows.h> /* for GetFullPathName */
43#endif
44
45// Careful: must include globals first for extern definitions
46#include "globals.h"
47
48#include "main.h"
49#include "parse/t_program.h"
50#include "parse/t_scope.h"
51#include "generate/t_generator.h"
52
53#include "version.h"
54
55using namespace std;
56
57/**
58 * Global program tree
59 */
60t_program* g_program;
61
62/**
63 * Global types
64 */
65
66t_type* g_type_void;
67t_type* g_type_string;
68t_type* g_type_binary;
69t_type* g_type_slist;
70t_type* g_type_bool;
71t_type* g_type_byte;
72t_type* g_type_i16;
73t_type* g_type_i32;
74t_type* g_type_i64;
75t_type* g_type_double;
76
77/**
78 * Global scope
79 */
80t_scope* g_scope;
81
82/**
83 * Parent scope to also parse types
84 */
85t_scope* g_parent_scope;
86
87/**
88 * Prefix for putting types in parent scope
89 */
90string g_parent_prefix;
91
92/**
93 * Parsing pass
94 */
95PARSE_MODE g_parse_mode;
96
97/**
98 * Current directory of file being parsed
99 */
100string g_curdir;
101
102/**
103 * Current file being parsed
104 */
105string g_curpath;
106
107/**
108 * Search path for inclusions
109 */
110vector<string> g_incl_searchpath;
111
112/**
113 * Should C++ include statements use path prefixes for other thrift-generated
114 * header files
115 */
116bool g_cpp_use_include_prefix = false;
117
118/**
119 * Global debug state
120 */
121int g_debug = 0;
122
123/**
124 * Strictness level
125 */
126int g_strict = 127;
127
128/**
129 * Warning level
130 */
131int g_warn = 1;
132
133/**
134 * Verbose output
135 */
136int g_verbose = 0;
137
138/**
139 * Global time string
140 */
141char* g_time_str;
142
143/**
144 * The last parsed doctext comment.
145 */
146char* g_doctext;
147
148/**
149 * The location of the last parsed doctext comment.
150 */
151int g_doctext_lineno;
152
153/**
154 * Flags to control code generation
155 */
156bool gen_cpp = false;
157bool gen_dense = false;
158bool gen_java = false;
159bool gen_javabean = false;
160bool gen_rb = false;
161bool gen_py = false;
162bool gen_py_newstyle = false;
163bool gen_xsd = false;
164bool gen_php = false;
165bool gen_phpi = false;
166bool gen_phps = true;
167bool gen_phpa = false;
168bool gen_phpo = false;
169bool gen_rest = false;
170bool gen_perl = false;
171bool gen_erl = false;
172bool gen_ocaml = false;
173bool gen_hs = false;
174bool gen_cocoa = false;
175bool gen_csharp = false;
176bool gen_st = false;
177bool gen_recurse = false;
178
179/**
180 * MinGW doesn't have realpath, so use fallback implementation in that case,
181 * otherwise this just calls through to realpath
182 */
183char *saferealpath(const char *path, char *resolved_path) {
184#ifdef MINGW
185 char buf[MAX_PATH];
186 char* basename;
187 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
188 if (len == 0 || len > MAX_PATH - 1){
189 strcpy(resolved_path, path);
190 } else {
191 CharLowerBuff(buf, len);
192 strcpy(resolved_path, buf);
193 }
194 return resolved_path;
195#else
196 return realpath(path, resolved_path);
197#endif
198}
199
200
201/**
202 * Report an error to the user. This is called yyerror for historical
203 * reasons (lex and yacc expect the error reporting routine to be called
204 * this). Call this function to report any errors to the user.
205 * yyerror takes printf style arguments.
206 *
207 * @param fmt C format string followed by additional arguments
208 */
209void yyerror(const char* fmt, ...) {
210 va_list args;
211 fprintf(stderr,
212 "[ERROR:%s:%d] (last token was '%s')\n",
213 g_curpath.c_str(),
214 yylineno,
215 yytext);
216
217 va_start(args, fmt);
218 vfprintf(stderr, fmt, args);
219 va_end(args);
220
221 fprintf(stderr, "\n");
222}
223
224/**
225 * Prints a debug message from the parser.
226 *
227 * @param fmt C format string followed by additional arguments
228 */
229void pdebug(const char* fmt, ...) {
230 if (g_debug == 0) {
231 return;
232 }
233 va_list args;
234 printf("[PARSE:%d] ", yylineno);
235 va_start(args, fmt);
236 vprintf(fmt, args);
237 va_end(args);
238 printf("\n");
239}
240
241/**
242 * Prints a verbose output mode message
243 *
244 * @param fmt C format string followed by additional arguments
245 */
246void pverbose(const char* fmt, ...) {
247 if (g_verbose == 0) {
248 return;
249 }
250 va_list args;
251 va_start(args, fmt);
252 vprintf(fmt, args);
253 va_end(args);
254}
255
256/**
257 * Prints a warning message
258 *
259 * @param fmt C format string followed by additional arguments
260 */
261void pwarning(int level, const char* fmt, ...) {
262 if (g_warn < level) {
263 return;
264 }
265 va_list args;
266 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
267 va_start(args, fmt);
268 vprintf(fmt, args);
269 va_end(args);
270 printf("\n");
271}
272
273/**
274 * Prints a failure message and exits
275 *
276 * @param fmt C format string followed by additional arguments
277 */
278void failure(const char* fmt, ...) {
279 va_list args;
280 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
281 va_start(args, fmt);
282 vfprintf(stderr, fmt, args);
283 va_end(args);
284 printf("\n");
285 exit(1);
286}
287
288/**
289 * Converts a string filename into a thrift program name
290 */
291string program_name(string filename) {
292 string::size_type slash = filename.rfind("/");
293 if (slash != string::npos) {
294 filename = filename.substr(slash+1);
295 }
296 string::size_type dot = filename.rfind(".");
297 if (dot != string::npos) {
298 filename = filename.substr(0, dot);
299 }
300 return filename;
301}
302
303/**
304 * Gets the directory path of a filename
305 */
306string directory_name(string filename) {
307 string::size_type slash = filename.rfind("/");
308 // No slash, just use the current directory
309 if (slash == string::npos) {
310 return ".";
311 }
312 return filename.substr(0, slash);
313}
314
315/**
316 * Finds the appropriate file path for the given filename
317 */
318string include_file(string filename) {
319 // Absolute path? Just try that
320 if (filename[0] == '/') {
321 // Realpath!
322 char rp[PATH_MAX];
323 if (saferealpath(filename.c_str(), rp) == NULL) {
324 pwarning(0, "Cannot open include file %s\n", filename.c_str());
325 return std::string();
326 }
327
328 // Stat this file
329 struct stat finfo;
330 if (stat(rp, &finfo) == 0) {
331 return rp;
332 }
333 } else { // relative path, start searching
334 // new search path with current dir global
335 vector<string> sp = g_incl_searchpath;
336 sp.insert(sp.begin(), g_curdir);
337
338 // iterate through paths
339 vector<string>::iterator it;
340 for (it = sp.begin(); it != sp.end(); it++) {
341 string sfilename = *(it) + "/" + filename;
342
343 // Realpath!
344 char rp[PATH_MAX];
345 if (saferealpath(sfilename.c_str(), rp) == NULL) {
346 continue;
347 }
348
349 // Stat this files
350 struct stat finfo;
351 if (stat(rp, &finfo) == 0) {
352 return rp;
353 }
354 }
355 }
356
357 // Uh oh
358 pwarning(0, "Could not find include file %s\n", filename.c_str());
359 return std::string();
360}
361
362/**
363 * Clears any previously stored doctext string.
364 * Also prints a warning if we are discarding information.
365 */
366void clear_doctext() {
367 if (g_doctext != NULL) {
368 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
369 }
370 free(g_doctext);
371 g_doctext = NULL;
372}
373
374/**
375 * Cleans up text commonly found in doxygen-like comments
376 *
377 * Warning: if you mix tabs and spaces in a non-uniform way,
378 * you will get what you deserve.
379 */
380char* clean_up_doctext(char* doctext) {
381 // Convert to C++ string, and remove Windows's carriage returns.
382 string docstring = doctext;
383 docstring.erase(
384 remove(docstring.begin(), docstring.end(), '\r'),
385 docstring.end());
386
387 // Separate into lines.
388 vector<string> lines;
389 string::size_type pos = string::npos;
390 string::size_type last;
391 while (true) {
392 last = (pos == string::npos) ? 0 : pos+1;
393 pos = docstring.find('\n', last);
394 if (pos == string::npos) {
395 // First bit of cleaning. If the last line is only whitespace, drop it.
396 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
397 if (nonwhite != string::npos) {
398 lines.push_back(docstring.substr(last));
399 }
400 break;
401 }
402 lines.push_back(docstring.substr(last, pos-last));
403 }
404
405 // A very profound docstring.
406 if (lines.empty()) {
407 return NULL;
408 }
409
410 // Clear leading whitespace from the first line.
411 pos = lines.front().find_first_not_of(" \t");
412 lines.front().erase(0, pos);
413
414 // If every nonblank line after the first has the same number of spaces/tabs,
415 // then a star, remove them.
416 bool have_prefix = true;
417 bool found_prefix = false;
418 string::size_type prefix_len = 0;
419 vector<string>::iterator l_iter;
420 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
421 if (l_iter->empty()) {
422 continue;
423 }
424
425 pos = l_iter->find_first_not_of(" \t");
426 if (!found_prefix) {
427 if (pos != string::npos) {
428 if (l_iter->at(pos) == '*') {
429 found_prefix = true;
430 prefix_len = pos;
431 } else {
432 have_prefix = false;
433 break;
434 }
435 } else {
436 // Whitespace-only line. Truncate it.
437 l_iter->clear();
438 }
439 } else if (l_iter->size() > pos
440 && l_iter->at(pos) == '*'
441 && pos == prefix_len) {
442 // Business as usual.
443 } else if (pos == string::npos) {
444 // Whitespace-only line. Let's truncate it for them.
445 l_iter->clear();
446 } else {
447 // The pattern has been broken.
448 have_prefix = false;
449 break;
450 }
451 }
452
453 // If our prefix survived, delete it from every line.
454 if (have_prefix) {
455 // Get the star too.
456 prefix_len++;
457 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
458 l_iter->erase(0, prefix_len);
459 }
460 }
461
462 // Now delete the minimum amount of leading whitespace from each line.
463 prefix_len = string::npos;
464 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
465 if (l_iter->empty()) {
466 continue;
467 }
468 pos = l_iter->find_first_not_of(" \t");
469 if (pos != string::npos
470 && (prefix_len == string::npos || pos < prefix_len)) {
471 prefix_len = pos;
472 }
473 }
474
475 // If our prefix survived, delete it from every line.
476 if (prefix_len != string::npos) {
477 for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
478 l_iter->erase(0, prefix_len);
479 }
480 }
481
482 // Remove trailing whitespace from every line.
483 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
484 pos = l_iter->find_last_not_of(" \t");
485 if (pos != string::npos && pos != l_iter->length()-1) {
486 l_iter->erase(pos+1);
487 }
488 }
489
490 // If the first line is empty, remove it.
491 // Don't do this earlier because a lot of steps skip the first line.
492 if (lines.front().empty()) {
493 lines.erase(lines.begin());
494 }
495
496 // Now rejoin the lines and copy them back into doctext.
497 docstring.clear();
498 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
499 docstring += *l_iter;
500 docstring += '\n';
501 }
502
503 assert(docstring.length() <= strlen(doctext));
504 strcpy(doctext, docstring.c_str());
505 return doctext;
506}
507
508/** Set to true to debug docstring parsing */
509static bool dump_docs = false;
510
511/**
512 * Dumps docstrings to stdout
513 * Only works for top-level definitions and the whole program doc
514 * (i.e., not enum constants, struct fields, or functions.
515 */
516void dump_docstrings(t_program* program) {
517 string progdoc = program->get_doc();
518 if (!progdoc.empty()) {
519 printf("Whole program doc:\n%s\n", progdoc.c_str());
520 }
521 const vector<t_typedef*>& typedefs = program->get_typedefs();
522 vector<t_typedef*>::const_iterator t_iter;
523 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
524 t_typedef* td = *t_iter;
525 if (td->has_doc()) {
526 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
527 }
528 }
529 const vector<t_enum*>& enums = program->get_enums();
530 vector<t_enum*>::const_iterator e_iter;
531 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
532 t_enum* en = *e_iter;
533 if (en->has_doc()) {
534 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
535 }
536 }
537 const vector<t_const*>& consts = program->get_consts();
538 vector<t_const*>::const_iterator c_iter;
539 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
540 t_const* co = *c_iter;
541 if (co->has_doc()) {
542 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
543 }
544 }
545 const vector<t_struct*>& structs = program->get_structs();
546 vector<t_struct*>::const_iterator s_iter;
547 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
548 t_struct* st = *s_iter;
549 if (st->has_doc()) {
550 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
551 }
552 }
553 const vector<t_struct*>& xceptions = program->get_xceptions();
554 vector<t_struct*>::const_iterator x_iter;
555 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
556 t_struct* xn = *x_iter;
557 if (xn->has_doc()) {
558 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
559 }
560 }
561 const vector<t_service*>& services = program->get_services();
562 vector<t_service*>::const_iterator v_iter;
563 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
564 t_service* sv = *v_iter;
565 if (sv->has_doc()) {
566 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
567 }
568 }
569}
570
571/**
572 * Call generate_fingerprint for every structure and enum.
573 */
574void generate_all_fingerprints(t_program* program) {
575 const vector<t_struct*>& structs = program->get_structs();
576 vector<t_struct*>::const_iterator s_iter;
577 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
578 t_struct* st = *s_iter;
579 st->generate_fingerprint();
580 }
581
582 const vector<t_struct*>& xceptions = program->get_xceptions();
583 vector<t_struct*>::const_iterator x_iter;
584 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
585 t_struct* st = *x_iter;
586 st->generate_fingerprint();
587 }
588
589 const vector<t_enum*>& enums = program->get_enums();
590 vector<t_enum*>::const_iterator e_iter;
591 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
592 t_enum* e = *e_iter;
593 e->generate_fingerprint();
594 }
595
596 g_type_void->generate_fingerprint();
597
598 // If you want to generate fingerprints for implicit structures, start here.
599 /*
600 const vector<t_service*>& services = program->get_services();
601 vector<t_service*>::const_iterator v_iter;
602 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
603 t_service* sv = *v_iter;
604 }
605 */
606}
607
608/**
609 * Prints the version number
610 */
611void version() {
612 printf("Thrift version %s-%s\n", THRIFT_VERSION, THRIFT_REVISION);
613}
614
615/**
616 * Diplays the usage message and then exits with an error code.
617 */
618void usage() {
619 fprintf(stderr, "Usage: thrift [options] file\n");
620 fprintf(stderr, "Options:\n");
621 fprintf(stderr, " -version Print the compiler version\n");
622 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
623 fprintf(stderr, " (default: current directory)\n");
624 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
625 fprintf(stderr, " searched for include directives\n");
626 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
627 fprintf(stderr, " -strict Strict compiler warnings on\n");
628 fprintf(stderr, " -v[erbose] Verbose mode\n");
629 fprintf(stderr, " -r[ecurse] Also generate included files\n");
630 fprintf(stderr, " -debug Parse debug trace to stdout\n");
631 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
632 fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
633 fprintf(stderr, " Keys and values are options passed to the generator.\n");
634 fprintf(stderr, " Many options will not require values.\n");
635 fprintf(stderr, "\n");
636 fprintf(stderr, "Available generators (and options):\n");
637
638 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
639 t_generator_registry::gen_map_t::iterator iter;
640 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
641 fprintf(stderr, " %s (%s):\n",
642 iter->second->get_short_name().c_str(),
643 iter->second->get_long_name().c_str());
644 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
645 }
646 exit(1);
647}
648
649/**
650 * You know, when I started working on Thrift I really thought it wasn't going
651 * to become a programming language because it was just a generator and it
652 * wouldn't need runtime type information and all that jazz. But then we
653 * decided to add constants, and all of a sudden that means runtime type
654 * validation and inference, except the "runtime" is the code generator
655 * runtime. Shit. I've been had.
656 */
657void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
658 if (type->is_void()) {
659 throw "type error: cannot declare a void const: " + name;
660 }
661
662 if (type->is_base_type()) {
663 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
664 switch (tbase) {
665 case t_base_type::TYPE_STRING:
666 if (value->get_type() != t_const_value::CV_STRING) {
667 throw "type error: const \"" + name + "\" was declared as string";
668 }
669 break;
670 case t_base_type::TYPE_BOOL:
671 if (value->get_type() != t_const_value::CV_INTEGER) {
672 throw "type error: const \"" + name + "\" was declared as bool";
673 }
674 break;
675 case t_base_type::TYPE_BYTE:
676 if (value->get_type() != t_const_value::CV_INTEGER) {
677 throw "type error: const \"" + name + "\" was declared as byte";
678 }
679 break;
680 case t_base_type::TYPE_I16:
681 if (value->get_type() != t_const_value::CV_INTEGER) {
682 throw "type error: const \"" + name + "\" was declared as i16";
683 }
684 break;
685 case t_base_type::TYPE_I32:
686 if (value->get_type() != t_const_value::CV_INTEGER) {
687 throw "type error: const \"" + name + "\" was declared as i32";
688 }
689 break;
690 case t_base_type::TYPE_I64:
691 if (value->get_type() != t_const_value::CV_INTEGER) {
692 throw "type error: const \"" + name + "\" was declared as i64";
693 }
694 break;
695 case t_base_type::TYPE_DOUBLE:
696 if (value->get_type() != t_const_value::CV_INTEGER &&
697 value->get_type() != t_const_value::CV_DOUBLE) {
698 throw "type error: const \"" + name + "\" was declared as double";
699 }
700 break;
701 default:
702 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
703 }
704 } else if (type->is_enum()) {
705 if (value->get_type() != t_const_value::CV_INTEGER) {
706 throw "type error: const \"" + name + "\" was declared as enum";
707 }
708 } else if (type->is_struct() || type->is_xception()) {
709 if (value->get_type() != t_const_value::CV_MAP) {
710 throw "type error: const \"" + name + "\" was declared as struct/xception";
711 }
712 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
713 vector<t_field*>::const_iterator f_iter;
714
715 const map<t_const_value*, t_const_value*>& val = value->get_map();
716 map<t_const_value*, t_const_value*>::const_iterator v_iter;
717 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
718 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
719 throw "type error: " + name + " struct key must be string";
720 }
721 t_type* field_type = NULL;
722 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
723 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
724 field_type = (*f_iter)->get_type();
725 }
726 }
727 if (field_type == NULL) {
728 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
729 }
730
731 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
732 }
733 } else if (type->is_map()) {
734 t_type* k_type = ((t_map*)type)->get_key_type();
735 t_type* v_type = ((t_map*)type)->get_val_type();
736 const map<t_const_value*, t_const_value*>& val = value->get_map();
737 map<t_const_value*, t_const_value*>::const_iterator v_iter;
738 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
739 validate_const_rec(name + "<key>", k_type, v_iter->first);
740 validate_const_rec(name + "<val>", v_type, v_iter->second);
741 }
742 } else if (type->is_list() || type->is_set()) {
743 t_type* e_type;
744 if (type->is_list()) {
745 e_type = ((t_list*)type)->get_elem_type();
746 } else {
747 e_type = ((t_set*)type)->get_elem_type();
748 }
749 const vector<t_const_value*>& val = value->get_list();
750 vector<t_const_value*>::const_iterator v_iter;
751 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
752 validate_const_rec(name + "<elem>", e_type, *v_iter);
753 }
754 }
755}
756
757/**
758 * Check the type of the parsed const information against its declared type
759 */
760void validate_const_type(t_const* c) {
761 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
762}
763
764/**
765 * Check the type of a default value assigned to a field.
766 */
767void validate_field_value(t_field* field, t_const_value* cv) {
768 validate_const_rec(field->get_name(), field->get_type(), cv);
769}
770
771/**
772 * Check that all the elements of a throws block are actually exceptions.
773 */
774bool validate_throws(t_struct* throws) {
775 const vector<t_field*>& members = throws->get_members();
776 vector<t_field*>::const_iterator m_iter;
777 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
778 if (!(*m_iter)->get_type()->is_xception()) {
779 return false;
780 }
781 }
782 return true;
783}
784
785/**
786 * Parses a program
787 */
788void parse(t_program* program, t_program* parent_program) {
789 // Get scope file path
790 string path = program->get_path();
791
792 // Set current dir global, which is used in the include_file function
793 g_curdir = directory_name(path);
794 g_curpath = path;
795
796 // Open the file
797 yyin = fopen(path.c_str(), "r");
798 if (yyin == 0) {
799 failure("Could not open input file: \"%s\"", path.c_str());
800 }
801
802 // Create new scope and scan for includes
803 pverbose("Scanning %s for includes\n", path.c_str());
804 g_parse_mode = INCLUDES;
805 g_program = program;
806 g_scope = program->scope();
807 try {
808 yylineno = 1;
809 if (yyparse() != 0) {
810 failure("Parser error during include pass.");
811 }
812 } catch (string x) {
813 failure(x.c_str());
814 }
815 fclose(yyin);
816
817 // Recursively parse all the include programs
818 vector<t_program*>& includes = program->get_includes();
819 vector<t_program*>::iterator iter;
820 for (iter = includes.begin(); iter != includes.end(); ++iter) {
821 parse(*iter, program);
822 }
823
824 // Parse the program file
825 g_parse_mode = PROGRAM;
826 g_program = program;
827 g_scope = program->scope();
828 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
829 g_parent_prefix = program->get_name() + ".";
830 g_curpath = path;
831 yyin = fopen(path.c_str(), "r");
832 if (yyin == 0) {
833 failure("Could not open input file: \"%s\"", path.c_str());
834 }
835 pverbose("Parsing %s for types\n", path.c_str());
836 yylineno = 1;
837 try {
838 if (yyparse() != 0) {
839 failure("Parser error during types pass.");
840 }
841 } catch (string x) {
842 failure(x.c_str());
843 }
844 fclose(yyin);
845}
846
847/**
848 * Generate code
849 */
850void generate(t_program* program, const vector<string>& generator_strings) {
851 // Oooohh, recursive code generation, hot!!
852 if (gen_recurse) {
853 const vector<t_program*>& includes = program->get_includes();
854 for (size_t i = 0; i < includes.size(); ++i) {
855 // Propogate output path from parent to child programs
856 includes[i]->set_out_path(program->get_out_path());
857
858 generate(includes[i], generator_strings);
859 }
860 }
861
862 // Generate code!
863 try {
864 pverbose("Program: %s\n", program->get_path().c_str());
865
866 // Compute fingerprints.
867 generate_all_fingerprints(program);
868
869 if (dump_docs) {
870 dump_docstrings(program);
871 }
872
873 vector<string>::const_iterator iter;
874 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
875 t_generator* generator = t_generator_registry::get_generator(program, *iter);
876
877 if (generator == NULL) {
878 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
879 } else {
880 pverbose("Generating \"%s\"\n", iter->c_str());
881 generator->generate_program();
882 delete generator;
883 }
884 }
885
886 } catch (string s) {
887 printf("Error: %s\n", s.c_str());
888 } catch (const char* exc) {
889 printf("Error: %s\n", exc);
890 }
891
892}
893
894/**
895 * Parse it up.. then spit it back out, in pretty much every language. Alright
896 * not that many languages, but the cool ones that we care about.
897 */
898int main(int argc, char** argv) {
899 int i;
900 std::string out_path;
901
902 // Setup time string
903 time_t now = time(NULL);
904 g_time_str = ctime(&now);
905
906 // Check for necessary arguments, you gotta have at least a filename and
907 // an output language flag
908 if (argc < 2) {
909 usage();
910 }
911
912 vector<string> generator_strings;
913
914 // Set the current path to a dummy value to make warning messages clearer.
915 g_curpath = "arguments";
916
917 // Hacky parameter handling... I didn't feel like using a library sorry!
918 for (i = 1; i < argc-1; i++) {
919 char* arg;
920
921 arg = strtok(argv[i], " ");
922 while (arg != NULL) {
923 // Treat double dashes as single dashes
924 if (arg[0] == '-' && arg[1] == '-') {
925 ++arg;
926 }
927
928 if (strcmp(arg, "-version") == 0) {
929 version();
930 exit(1);
931 } else if (strcmp(arg, "-debug") == 0) {
932 g_debug = 1;
933 } else if (strcmp(arg, "-nowarn") == 0) {
934 g_warn = 0;
935 } else if (strcmp(arg, "-strict") == 0) {
936 g_strict = 255;
937 g_warn = 2;
938 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
939 g_verbose = 1;
940 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
941 gen_recurse = true;
942 } else if (strcmp(arg, "-gen") == 0) {
943 arg = argv[++i];
944 if (arg == NULL) {
945 fprintf(stderr, "!!! Missing generator specification\n");
946 usage();
947 }
948 generator_strings.push_back(arg);
949 } else if (strcmp(arg, "-dense") == 0) {
950 gen_dense = true;
951 } else if (strcmp(arg, "-cpp") == 0) {
952 gen_cpp = true;
953 } else if (strcmp(arg, "-javabean") == 0) {
954 gen_javabean = true;
955 } else if (strcmp(arg, "-java") == 0) {
956 gen_java = true;
957 } else if (strcmp(arg, "-php") == 0) {
958 gen_php = true;
959 } else if (strcmp(arg, "-phpi") == 0) {
960 gen_phpi = true;
961 } else if (strcmp(arg, "-phps") == 0) {
962 gen_php = true;
963 gen_phps = true;
964 } else if (strcmp(arg, "-phpl") == 0) {
965 gen_php = true;
966 gen_phps = false;
967 } else if (strcmp(arg, "-phpa") == 0) {
968 gen_php = true;
969 gen_phps = false;
970 gen_phpa = true;
971 } else if (strcmp(arg, "-phpo") == 0) {
972 gen_php = true;
973 gen_phpo = true;
974 } else if (strcmp(arg, "-rest") == 0) {
975 gen_rest = true;
976 } else if (strcmp(arg, "-py") == 0) {
977 gen_py = true;
978 } else if (strcmp(arg, "-pyns") == 0) {
979 gen_py = true;
980 gen_py_newstyle = true;
981 } else if (strcmp(arg, "-rb") == 0) {
982 gen_rb = true;
983 } else if (strcmp(arg, "-xsd") == 0) {
984 gen_xsd = true;
985 } else if (strcmp(arg, "-perl") == 0) {
986 gen_perl = true;
987 } else if (strcmp(arg, "-erl") == 0) {
988 gen_erl = true;
989 } else if (strcmp(arg, "-ocaml") == 0) {
990 gen_ocaml = true;
991 } else if (strcmp(arg, "-hs") == 0) {
992 gen_hs = true;
993 } else if (strcmp(arg, "-cocoa") == 0) {
994 gen_cocoa = true;
995 } else if (strcmp(arg, "-st") == 0) {
996 gen_st = true;
997 } else if (strcmp(arg, "-csharp") == 0) {
998 gen_csharp = true;
999 } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1000 g_cpp_use_include_prefix = true;
1001 } else if (strcmp(arg, "-I") == 0) {
1002 // An argument of "-I\ asdf" is invalid and has unknown results
1003 arg = argv[++i];
1004
1005 if (arg == NULL) {
1006 fprintf(stderr, "!!! Missing Include directory\n");
1007 usage();
1008 }
1009 g_incl_searchpath.push_back(arg);
1010 } else if (strcmp(arg, "-o") == 0) {
1011 arg = argv[++i];
1012 if (arg == NULL) {
1013 fprintf(stderr, "-o: missing output directory\n");
1014 usage();
1015 }
1016 out_path = arg;
1017
1018#ifdef MINGW
1019 //strip out trailing \ on Windows
1020 int last = out_path.length()-1;
1021 if (out_path[last] == '\\')
1022 {
1023 out_path.erase(last);
1024 }
1025#endif
1026
1027 struct stat sb;
1028 if (stat(out_path.c_str(), &sb) < 0) {
1029 fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1030 return -1;
1031 }
1032 if (! S_ISDIR(sb.st_mode)) {
1033 fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1034 return -1;
1035 }
1036 } else {
1037 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1038 usage();
1039 }
1040
1041 // Tokenize more
1042 arg = strtok(NULL, " ");
1043 }
1044 }
1045
1046 // if you're asking for version, you have a right not to pass a file
1047 if (strcmp(argv[argc-1], "-version") == 0) {
1048 version();
1049 exit(1);
1050 }
1051
1052 // TODO(dreiss): Delete these when everyone is using the new hotness.
1053 if (gen_cpp) {
1054 pwarning(1, "-cpp is deprecated. Use --gen cpp");
1055 string gen_string = "cpp:";
1056 if (gen_dense) {
1057 gen_string.append("dense,");
1058 }
1059 if (g_cpp_use_include_prefix) {
1060 gen_string.append("include_prefix,");
1061 }
1062 generator_strings.push_back(gen_string);
1063 }
1064 if (gen_java) {
1065 pwarning(1, "-java is deprecated. Use --gen java");
1066 generator_strings.push_back("java");
1067 }
1068 if (gen_javabean) {
1069 pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1070 generator_strings.push_back("java:beans");
1071 }
1072 if (gen_csharp) {
1073 pwarning(1, "-csharp is deprecated. Use --gen csharp");
1074 generator_strings.push_back("csharp");
1075 }
1076 if (gen_py) {
1077 pwarning(1, "-py is deprecated. Use --gen py");
1078 generator_strings.push_back("py");
1079 }
1080 if (gen_rb) {
1081 pwarning(1, "-rb is deprecated. Use --gen rb");
1082 generator_strings.push_back("rb");
1083 }
1084 if (gen_perl) {
1085 pwarning(1, "-perl is deprecated. Use --gen perl");
1086 generator_strings.push_back("perl");
1087 }
1088 if (gen_php || gen_phpi) {
1089 pwarning(1, "-php is deprecated. Use --gen php");
1090 string gen_string = "php:";
1091 if (gen_phpi) {
1092 gen_string.append("inlined,");
1093 } else if(gen_phps) {
1094 gen_string.append("server,");
1095 } else if(gen_phpa) {
1096 gen_string.append("autoload,");
1097 } else if(gen_phpo) {
1098 gen_string.append("oop,");
1099 } else if(gen_rest) {
1100 gen_string.append("rest,");
1101 }
1102 generator_strings.push_back(gen_string);
1103 }
1104 if (gen_cocoa) {
1105 pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1106 generator_strings.push_back("cocoa");
1107 }
1108 if (gen_erl) {
1109 pwarning(1, "-erl is deprecated. Use --gen erl");
1110 generator_strings.push_back("erl");
1111 }
1112 if (gen_st) {
1113 pwarning(1, "-st is deprecated. Use --gen st");
1114 generator_strings.push_back("st");
1115 }
1116 if (gen_ocaml) {
1117 pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1118 generator_strings.push_back("ocaml");
1119 }
1120 if (gen_hs) {
1121 pwarning(1, "-hs is deprecated. Use --gen hs");
1122 generator_strings.push_back("hs");
1123 }
1124 if (gen_xsd) {
1125 pwarning(1, "-xsd is deprecated. Use --gen xsd");
1126 generator_strings.push_back("xsd");
1127 }
1128
1129 // You gotta generate something!
1130 if (generator_strings.empty()) {
1131 fprintf(stderr, "!!! No output language(s) specified\n\n");
1132 usage();
1133 }
1134
1135 // Real-pathify it
1136 char rp[PATH_MAX];
1137 if (argv[i] == NULL) {
1138 fprintf(stderr, "!!! Missing file name\n");
1139 usage();
1140 }
1141 if (saferealpath(argv[i], rp) == NULL) {
1142 failure("Could not open input file with realpath: %s", argv[i]);
1143 }
1144 string input_file(rp);
1145
1146 // Instance of the global parse tree
1147 t_program* program = new t_program(input_file);
1148 if (out_path.size()) {
1149 program->set_out_path(out_path);
1150 }
1151
1152 // Compute the cpp include prefix.
1153 // infer this from the filename passed in
1154 string input_filename = argv[i];
1155 string include_prefix;
1156
1157 string::size_type last_slash = string::npos;
1158 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1159 include_prefix = input_filename.substr(0, last_slash);
1160 }
1161
1162 program->set_include_prefix(include_prefix);
1163
1164 // Initialize global types
1165 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
1166 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
1167 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1168 ((t_base_type*)g_type_binary)->set_binary(true);
1169 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
1170 ((t_base_type*)g_type_slist)->set_string_list(true);
1171 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1172 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1173 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1174 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1175 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
1176 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
1177
1178 // Parse it!
1179 parse(program, NULL);
1180
1181 // The current path is not really relevant when we are doing generation.
1182 // Reset the variable to make warning messages clearer.
1183 g_curpath = "generation";
1184 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1185 // That is what shows up during argument parsing.
1186 yylineno = 1;
1187
1188 // Generate it!
1189 generate(program, generator_strings);
1190
1191 // Clean up. Who am I kidding... this program probably orphans heap memory
1192 // all over the place, but who cares because it is about to exit and it is
1193 // all referenced and used by this wacky parse tree up until now anyways.
1194
1195 delete program;
1196 delete g_type_void;
1197 delete g_type_string;
1198 delete g_type_bool;
1199 delete g_type_byte;
1200 delete g_type_i16;
1201 delete g_type_i32;
1202 delete g_type_i64;
1203 delete g_type_double;
1204
1205 // Finished
1206 return 0;
1207}