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