Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1 | /** |
| 2 | * thrift - a lightweight cross-language rpc/serialization tool |
| 3 | * |
| 4 | * This file contains the main compiler engine for Thrift, which invokes the |
| 5 | * scanner/parser to build the thrift object tree. The interface generation |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 6 | * code for each language lives in a file by the language name under the |
| 7 | * generate/ folder, and all parse structures live in parse/ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 8 | * |
| 9 | * @author Mark Slee <mcslee@facebook.com> |
| 10 | */ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <string> |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 18 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 19 | // Careful: must include globals first for extern definitions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | #include "globals.h" |
| 21 | |
| 22 | #include "main.h" |
| 23 | #include "parse/t_program.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 24 | #include "parse/t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 25 | #include "generate/t_cpp_generator.h" |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 26 | #include "generate/t_java_generator.h" |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 27 | #include "generate/t_php_generator.h" |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 28 | #include "generate/t_py_generator.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace std; |
| 31 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Global program tree |
| 34 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 35 | t_program* g_program; |
| 36 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 37 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 38 | * Global types |
| 39 | */ |
| 40 | |
| 41 | t_type* g_type_void; |
| 42 | t_type* g_type_string; |
| 43 | t_type* g_type_bool; |
| 44 | t_type* g_type_byte; |
| 45 | t_type* g_type_i16; |
| 46 | t_type* g_type_i32; |
| 47 | t_type* g_type_i64; |
| 48 | t_type* g_type_double; |
| 49 | |
| 50 | /** |
| 51 | * Global scope |
| 52 | */ |
| 53 | t_scope* g_scope; |
| 54 | |
| 55 | /** |
| 56 | * Parent scope to also parse types |
| 57 | */ |
| 58 | t_scope* g_parent_scope; |
| 59 | |
| 60 | /** |
| 61 | * Prefix for putting types in parent scope |
| 62 | */ |
| 63 | string g_parent_prefix; |
| 64 | |
| 65 | /** |
| 66 | * Parsing pass |
| 67 | */ |
| 68 | PARSE_MODE g_parse_mode; |
| 69 | |
| 70 | /** |
| 71 | * Current directory of file being parsed |
| 72 | */ |
| 73 | string g_curdir; |
| 74 | |
| 75 | /** |
| 76 | * Current file being parsed |
| 77 | */ |
| 78 | string g_curpath; |
| 79 | |
| 80 | /** |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 81 | * Search path for inclusions |
| 82 | */ |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 83 | vector<string> g_incl_searchpath; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 86 | * Global debug state |
| 87 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 88 | int g_debug = 0; |
| 89 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 90 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 91 | * Warning level |
| 92 | */ |
| 93 | int g_warn = 1; |
| 94 | |
| 95 | /** |
| 96 | * Verbose output |
| 97 | */ |
| 98 | int g_verbose = 0; |
| 99 | |
| 100 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 101 | * Global time string |
| 102 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 103 | char* g_time_str; |
| 104 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 105 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 106 | * Flags to control code generation |
| 107 | */ |
| 108 | bool gen_cpp = false; |
| 109 | bool gen_java = false; |
| 110 | bool gen_py = false; |
| 111 | bool gen_php = false; |
| 112 | bool gen_phpi = false; |
| 113 | bool gen_recurse = false; |
| 114 | |
| 115 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 116 | * Report an error to the user. This is called yyerror for historical |
| 117 | * reasons (lex and yacc expect the error reporting routine to be called |
| 118 | * this). Call this function to report any errors to the user. |
| 119 | * yyerror takes printf style arguments. |
| 120 | * |
| 121 | * @param fmt C format string followed by additional arguments |
| 122 | */ |
| 123 | void yyerror(char* fmt, ...) { |
| 124 | va_list args; |
| 125 | fprintf(stderr, |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 126 | "[ERROR:%s:%d] (last token was '%s')\n", |
| 127 | g_curpath.c_str(), |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 128 | yylineno, |
| 129 | yytext); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 130 | |
| 131 | va_start(args, fmt); |
| 132 | vfprintf(stderr, fmt, args); |
| 133 | va_end(args); |
| 134 | |
| 135 | fprintf(stderr, "\n"); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Prints a debug message from the parser. |
| 140 | * |
| 141 | * @param fmt C format string followed by additional arguments |
| 142 | */ |
| 143 | void pdebug(char* fmt, ...) { |
| 144 | if (g_debug == 0) { |
| 145 | return; |
| 146 | } |
| 147 | va_list args; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 148 | printf("[PARSE:%d] ", yylineno); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 149 | va_start(args, fmt); |
| 150 | vprintf(fmt, args); |
| 151 | va_end(args); |
| 152 | printf("\n"); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Prints a verbose output mode message |
| 157 | * |
| 158 | * @param fmt C format string followed by additional arguments |
| 159 | */ |
| 160 | void pverbose(char* fmt, ...) { |
| 161 | if (g_verbose == 0) { |
| 162 | return; |
| 163 | } |
| 164 | va_list args; |
| 165 | va_start(args, fmt); |
| 166 | vprintf(fmt, args); |
| 167 | va_end(args); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Prints a warning message |
| 172 | * |
| 173 | * @param fmt C format string followed by additional arguments |
| 174 | */ |
| 175 | void pwarning(int level, char* fmt, ...) { |
| 176 | if (g_warn < level) { |
| 177 | return; |
| 178 | } |
| 179 | va_list args; |
| 180 | printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 181 | va_start(args, fmt); |
| 182 | vprintf(fmt, args); |
| 183 | va_end(args); |
| 184 | printf("\n"); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Prints a failure message and exits |
| 189 | * |
| 190 | * @param fmt C format string followed by additional arguments |
| 191 | */ |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 192 | void failure(const char* fmt, ...) { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 193 | va_list args; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 194 | fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 195 | va_start(args, fmt); |
| 196 | vfprintf(stderr, fmt, args); |
| 197 | va_end(args); |
| 198 | printf("\n"); |
| 199 | exit(1); |
| 200 | } |
| 201 | |
| 202 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 203 | * Converts a string filename into a thrift program name |
| 204 | */ |
| 205 | string program_name(string filename) { |
| 206 | string::size_type slash = filename.rfind("/"); |
| 207 | if (slash != string::npos) { |
| 208 | filename = filename.substr(slash+1); |
| 209 | } |
| 210 | string::size_type dot = filename.rfind("."); |
| 211 | if (dot != string::npos) { |
| 212 | filename = filename.substr(0, dot); |
| 213 | } |
| 214 | return filename; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets the directory path of a filename |
| 219 | */ |
| 220 | string directory_name(string filename) { |
| 221 | string::size_type slash = filename.rfind("/"); |
| 222 | // No slash, just use the current directory |
| 223 | if (slash == string::npos) { |
| 224 | return "."; |
| 225 | } |
| 226 | return filename.substr(0, slash); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Finds the appropriate file path for the given filename |
| 231 | */ |
| 232 | string include_file(string filename) { |
| 233 | // Absolute path? Just try that |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 234 | if (filename[0] == '/') { |
| 235 | // Realpath! |
| 236 | char rp[PATH_MAX]; |
| 237 | if (realpath(filename.c_str(), rp) == NULL) { |
| 238 | pwarning(0, "Cannot open include file %s\n", filename.c_str()); |
| 239 | return std::string(); |
| 240 | } |
| 241 | |
| 242 | // Stat this files |
| 243 | struct stat finfo; |
| 244 | if (stat(rp, &finfo) == 0) { |
| 245 | return rp; |
| 246 | } |
| 247 | } else { // relative path, start searching |
| 248 | // new search path with current dir global |
| 249 | vector<string> sp = g_incl_searchpath; |
| 250 | sp.insert(sp.begin(), g_curdir); |
| 251 | |
| 252 | // iterate through paths |
| 253 | vector<string>::iterator it; |
| 254 | for (it = sp.begin(); it != sp.end(); it++) { |
| 255 | string sfilename = *(it) + "/" + filename; |
| 256 | |
| 257 | // Realpath! |
| 258 | char rp[PATH_MAX]; |
| 259 | if (realpath(sfilename.c_str(), rp) == NULL) { |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | // Stat this files |
| 264 | struct stat finfo; |
| 265 | if (stat(rp, &finfo) == 0) { |
| 266 | return rp; |
| 267 | } |
| 268 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 271 | // Uh oh |
| 272 | pwarning(0, "Could not find include file %s\n", filename.c_str()); |
| 273 | return std::string(); |
| 274 | } |
| 275 | |
| 276 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 277 | * Diplays the usage message and then exits with an error code. |
| 278 | */ |
| 279 | void usage() { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 280 | fprintf(stderr, "Usage: thrift [options] file\n"); |
| 281 | fprintf(stderr, "Options:\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 282 | fprintf(stderr, " -cpp Generate C++ output files\n"); |
| 283 | fprintf(stderr, " -java Generate Java output files\n"); |
| 284 | fprintf(stderr, " -php Generate PHP output files\n"); |
| 285 | fprintf(stderr, " -phpi Generate PHP inlined files\n"); |
| 286 | fprintf(stderr, " -py Generate Python output files\n"); |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 287 | fprintf(stderr, " -I dir Add a directory to the list of directories \n"); |
| 288 | fprintf(stderr, " searched for include directives\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 289 | fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n"); |
| 290 | fprintf(stderr, " -strict Strict compiler warnings on\n"); |
| 291 | fprintf(stderr, " -v[erbose] Verbose mode\n"); |
| 292 | fprintf(stderr, " -r[ecurse] Also generate included files\n"); |
| 293 | fprintf(stderr, " -debug Parse debug trace to stdout\n"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 294 | exit(1); |
| 295 | } |
| 296 | |
| 297 | /** |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 298 | * You know, when I started working on Thrift I really thought it wasn't going |
| 299 | * to become a programming language because it was just a generator and it |
| 300 | * wouldn't need runtime type information and all that jazz. But then we |
| 301 | * decided to add constants, and all of a sudden that means runtime type |
| 302 | * validation and inference, except the "runtime" is the code generator |
| 303 | * runtime. Shit. I've been had. |
| 304 | */ |
| 305 | void validate_const_rec(std::string name, t_type* type, t_const_value* value) { |
| 306 | if (type->is_void()) { |
| 307 | throw "type error: cannot declare a void const: " + name; |
| 308 | } |
| 309 | |
| 310 | if (type->is_base_type()) { |
| 311 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 312 | switch (tbase) { |
| 313 | case t_base_type::TYPE_STRING: |
| 314 | if (value->get_type() != t_const_value::CV_STRING) { |
| 315 | throw "type error: const \"" + name + "\" was declared as string"; |
| 316 | } |
| 317 | break; |
| 318 | case t_base_type::TYPE_BOOL: |
| 319 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 320 | throw "type error: const \"" + name + "\" was declared as bool"; |
| 321 | } |
| 322 | break; |
| 323 | case t_base_type::TYPE_BYTE: |
| 324 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 325 | throw "type error: const \"" + name + "\" was declared as byte"; |
| 326 | } |
| 327 | break; |
| 328 | case t_base_type::TYPE_I16: |
| 329 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 330 | throw "type error: const \"" + name + "\" was declared as i16"; |
| 331 | } |
| 332 | break; |
| 333 | case t_base_type::TYPE_I32: |
| 334 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 335 | throw "type error: const \"" + name + "\" was declared as i32"; |
| 336 | } |
| 337 | break; |
| 338 | case t_base_type::TYPE_I64: |
| 339 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 340 | throw "type error: const \"" + name + "\" was declared as i64"; |
| 341 | } |
| 342 | break; |
| 343 | case t_base_type::TYPE_DOUBLE: |
| 344 | if (value->get_type() != t_const_value::CV_INTEGER && |
| 345 | value->get_type() != t_const_value::CV_DOUBLE) { |
| 346 | throw "type error: const \"" + name + "\" was declared as double"; |
| 347 | } |
| 348 | break; |
| 349 | default: |
| 350 | throw "compiler error: no const of base type " + tbase + name; |
| 351 | } |
| 352 | } else if (type->is_enum()) { |
| 353 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 354 | throw "type error: const \"" + name + "\" was declared as enum"; |
| 355 | } |
| 356 | } else if (type->is_struct() || type->is_xception()) { |
| 357 | if (value->get_type() != t_const_value::CV_MAP) { |
| 358 | throw "type error: const \"" + name + "\" was declared as struct/xception"; |
| 359 | } |
| 360 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); |
| 361 | vector<t_field*>::const_iterator f_iter; |
| 362 | |
| 363 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 364 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 365 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 366 | if (v_iter->first->get_type() != t_const_value::CV_STRING) { |
| 367 | throw "type error: " + name + " struct key must be string"; |
| 368 | } |
| 369 | t_type* field_type = NULL; |
| 370 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 371 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { |
| 372 | field_type = (*f_iter)->get_type(); |
| 373 | } |
| 374 | } |
| 375 | if (field_type == NULL) { |
| 376 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); |
| 377 | } |
| 378 | |
| 379 | validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second); |
| 380 | } |
| 381 | } else if (type->is_map()) { |
| 382 | t_type* k_type = ((t_map*)type)->get_key_type(); |
| 383 | t_type* v_type = ((t_map*)type)->get_val_type(); |
| 384 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 385 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 386 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 387 | validate_const_rec(name + "<key>", k_type, v_iter->first); |
| 388 | validate_const_rec(name + "<val>", v_type, v_iter->second); |
| 389 | } |
| 390 | } else if (type->is_list() || type->is_set()) { |
| 391 | t_type* e_type; |
| 392 | if (type->is_list()) { |
| 393 | e_type = ((t_list*)type)->get_elem_type(); |
| 394 | } else { |
| 395 | e_type = ((t_set*)type)->get_elem_type(); |
| 396 | } |
| 397 | const vector<t_const_value*>& val = value->get_list(); |
| 398 | vector<t_const_value*>::const_iterator v_iter; |
| 399 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 400 | validate_const_rec(name + "<elem>", e_type, *v_iter); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Check the type of the parsed const information against its declared type |
| 407 | */ |
| 408 | void validate_const_type(t_const* c) { |
| 409 | validate_const_rec(c->get_name(), c->get_type(), c->get_value()); |
| 410 | } |
| 411 | |
| 412 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 413 | * Parses a program |
| 414 | */ |
| 415 | void parse(t_program* program, t_program* parent_program) { |
| 416 | // Get scope file path |
| 417 | string path = program->get_path(); |
| 418 | |
| 419 | // Set current dir global, which is used in the include_file function |
| 420 | g_curdir = directory_name(path); |
| 421 | g_curpath = path; |
| 422 | |
| 423 | // Open the file |
| 424 | yyin = fopen(path.c_str(), "r"); |
| 425 | if (yyin == 0) { |
| 426 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 427 | } |
| 428 | |
| 429 | // Create new scope and scan for includes |
| 430 | pverbose("Scanning %s for includes\n", path.c_str()); |
| 431 | g_parse_mode = INCLUDES; |
| 432 | g_program = program; |
| 433 | g_scope = program->scope(); |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 434 | try { |
| 435 | if (yyparse() != 0) { |
| 436 | failure("Parser error during include pass."); |
| 437 | } |
| 438 | } catch (string x) { |
| 439 | failure(x.c_str()); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 440 | } |
| 441 | fclose(yyin); |
| 442 | |
| 443 | // Recursively parse all the include programs |
| 444 | vector<t_program*>& includes = program->get_includes(); |
| 445 | vector<t_program*>::iterator iter; |
| 446 | for (iter = includes.begin(); iter != includes.end(); ++iter) { |
| 447 | parse(*iter, program); |
| 448 | } |
| 449 | |
| 450 | // Parse the program the file |
| 451 | g_parse_mode = PROGRAM; |
| 452 | g_program = program; |
| 453 | g_scope = program->scope(); |
| 454 | g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL; |
| 455 | g_parent_prefix = program->get_name() + "."; |
| 456 | g_curpath = path; |
| 457 | yyin = fopen(path.c_str(), "r"); |
| 458 | if (yyin == 0) { |
| 459 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 460 | } |
| 461 | pverbose("Parsing %s for types\n", path.c_str()); |
| 462 | if (yyparse() != 0) { |
| 463 | failure("Parser error during types pass."); |
| 464 | } |
| 465 | fclose(yyin); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Generate code |
| 470 | */ |
| 471 | void generate(t_program* program) { |
| 472 | // Oooohh, recursive code generation, hot!! |
| 473 | if (gen_recurse) { |
| 474 | const vector<t_program*>& includes = program->get_includes(); |
| 475 | for (size_t i = 0; i < includes.size(); ++i) { |
| 476 | generate(includes[i]); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // Generate code! |
| 481 | try { |
| 482 | pverbose("Program: %s\n", program->get_path().c_str()); |
| 483 | |
| 484 | if (gen_cpp) { |
| 485 | pverbose("Generating C++\n"); |
| 486 | t_cpp_generator* cpp = new t_cpp_generator(program); |
| 487 | cpp->generate_program(); |
| 488 | delete cpp; |
| 489 | } |
| 490 | |
| 491 | if (gen_java) { |
| 492 | pverbose("Generating Java\n"); |
| 493 | t_java_generator* java = new t_java_generator(program); |
| 494 | java->generate_program(); |
| 495 | delete java; |
| 496 | } |
| 497 | |
| 498 | if (gen_php) { |
| 499 | pverbose("Generating PHP\n"); |
| 500 | t_php_generator* php = new t_php_generator(program, false); |
| 501 | php->generate_program(); |
| 502 | delete php; |
| 503 | } |
| 504 | |
| 505 | if (gen_phpi) { |
| 506 | pverbose("Generating PHP-inline\n"); |
| 507 | t_php_generator* phpi = new t_php_generator(program, true); |
| 508 | phpi->generate_program(); |
| 509 | delete phpi; |
| 510 | } |
| 511 | |
| 512 | if (gen_py) { |
| 513 | pverbose("Generating Python\n"); |
| 514 | t_py_generator* py = new t_py_generator(program); |
| 515 | py->generate_program(); |
| 516 | delete py; |
| 517 | } |
| 518 | } catch (string s) { |
| 519 | printf("Error: %s\n", s.c_str()); |
| 520 | } catch (const char* exc) { |
| 521 | printf("Error: %s\n", exc); |
| 522 | } |
| 523 | |
| 524 | } |
| 525 | |
| 526 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 527 | * Parse it up.. then spit it back out, in pretty much every language. Alright |
| 528 | * not that many languages, but the cool ones that we care about. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 529 | */ |
| 530 | int main(int argc, char** argv) { |
| 531 | int i; |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 532 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 533 | // Setup time string |
| 534 | time_t now = time(NULL); |
| 535 | g_time_str = ctime(&now); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 536 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 537 | // Check for necessary arguments, you gotta have at least a filename and |
| 538 | // an output language flag |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 539 | if (argc < 2) { |
| 540 | usage(); |
| 541 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 542 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 543 | // Hacky parameter handling... I didn't feel like using a library sorry! |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 544 | for (i = 1; i < argc-1; i++) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 545 | char* arg; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 546 | |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 547 | arg = strtok(argv[i], " "); |
| 548 | while (arg != NULL) { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 549 | // Treat double dashes as single dashes |
Mark Slee | 52cb223 | 2006-11-10 22:32:07 +0000 | [diff] [blame] | 550 | if (arg[0] == '-' && arg[1] == '-') { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 551 | ++arg; |
| 552 | } |
| 553 | |
| 554 | if (strcmp(arg, "-debug") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 555 | g_debug = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 556 | } else if (strcmp(arg, "-nowarn") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 557 | g_warn = 0; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 558 | } else if (strcmp(arg, "-strict") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 559 | g_warn = 2; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 560 | } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 561 | g_verbose = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 562 | } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 563 | gen_recurse = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 564 | } else if (strcmp(arg, "-cpp") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 565 | gen_cpp = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 566 | } else if (strcmp(arg, "-java") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 567 | gen_java = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 568 | } else if (strcmp(arg, "-php") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 569 | gen_php = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 570 | } else if (strcmp(arg, "-phpi") == 0) { |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 571 | gen_phpi = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 572 | } else if (strcmp(arg, "-py") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 573 | gen_py = true; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 574 | } else if (strcmp(arg, "-I") == 0) { |
| 575 | // An argument of "-I\ asdf" is invalid and has unknown results |
| 576 | arg = argv[++i]; |
| 577 | |
| 578 | if (arg == NULL) { |
| 579 | fprintf(stderr, "!!! Missing Include directory"); |
| 580 | usage(); |
| 581 | } |
| 582 | g_incl_searchpath.push_back(arg); |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 583 | } else { |
| 584 | fprintf(stderr, "!!! Unrecognized option: %s\n", arg); |
| 585 | usage(); |
| 586 | } |
| 587 | |
| 588 | // Tokenize more |
| 589 | arg = strtok(NULL, " "); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 593 | // You gotta generate something! |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 594 | if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 595 | fprintf(stderr, "!!! No output language(s) specified\n\n"); |
| 596 | usage(); |
| 597 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 598 | |
| 599 | // Real-pathify it |
| 600 | char rp[PATH_MAX]; |
| 601 | if (realpath(argv[i], rp) == NULL) { |
| 602 | failure("Could not open input file: %s", argv[i]); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 603 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 604 | string input_file(rp); |
| 605 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 606 | // Instance of the global parse tree |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 607 | t_program* program = new t_program(input_file); |
| 608 | |
| 609 | // Initialize global types |
| 610 | g_type_void = new t_base_type("void", t_base_type::TYPE_VOID); |
| 611 | g_type_string = new t_base_type("string", t_base_type::TYPE_STRING); |
| 612 | g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL); |
| 613 | g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE); |
| 614 | g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16); |
| 615 | g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32); |
| 616 | g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64); |
| 617 | 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] | 618 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 619 | // Parse it! |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 620 | parse(program, NULL); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 621 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 622 | // Generate it! |
| 623 | generate(program); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 624 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 625 | // Clean up. Who am I kidding... this program probably orphans heap memory |
| 626 | // all over the place, but who cares because it is about to exit and it is |
| 627 | // 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] | 628 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 629 | delete program; |
| 630 | delete g_type_void; |
| 631 | delete g_type_string; |
| 632 | delete g_type_bool; |
| 633 | delete g_type_byte; |
| 634 | delete g_type_i16; |
| 635 | delete g_type_i32; |
| 636 | delete g_type_i64; |
| 637 | delete g_type_double; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 638 | |
| 639 | // Finished |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 640 | return 0; |
| 641 | } |