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