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