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