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