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