| ... | @@ -1,1431 +0,0 @@ |
| 1 | /* |
| 2 | * Copyright (c) 2019 Andrew Kelley |
| 3 | * |
| 4 | * This file is part of zig, which is MIT licensed. |
| 5 | * See http://opensource.org/licenses/MIT |
| 6 | */ |
| 7 | |
| 8 | #include "dump_analysis.hpp" |
| 9 | #include "analyze.hpp" |
| 10 | #include "ir.hpp" |
| 11 | #include "codegen.hpp" |
| 12 | #include "os.hpp" |
| 13 | |
| 14 | enum JsonWriterState { |
| 15 | JsonWriterStateInvalid, |
| 16 | JsonWriterStateValue, |
| 17 | JsonWriterStateArrayStart, |
| 18 | JsonWriterStateArray, |
| 19 | JsonWriterStateObjectStart, |
| 20 | JsonWriterStateObject, |
| 21 | }; |
| 22 | |
| 23 | #define JSON_MAX_DEPTH 10 |
| 24 | |
| 25 | struct JsonWriter { |
| 26 | size_t state_index; |
| 27 | FILE *f; |
| 28 | const char *one_indent; |
| 29 | const char *nl; |
| 30 | JsonWriterState state[JSON_MAX_DEPTH]; |
| 31 | }; |
| 32 | |
| 33 | static void jw_init(JsonWriter *jw, FILE *f, const char *one_indent, const char *nl) { |
| 34 | jw->state_index = 1; |
| 35 | jw->f = f; |
| 36 | jw->one_indent = one_indent; |
| 37 | jw->nl = nl; |
| 38 | jw->state[0] = JsonWriterStateInvalid; |
| 39 | jw->state[1] = JsonWriterStateValue; |
| 40 | } |
| 41 | |
| 42 | static void jw_nl_indent(JsonWriter *jw) { |
| 43 | assert(jw->state_index >= 1); |
| 44 | fprintf(jw->f, "%s", jw->nl); |
| 45 | for (size_t i = 0; i < jw->state_index - 1; i += 1) { |
| 46 | fprintf(jw->f, "%s", jw->one_indent); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void jw_push_state(JsonWriter *jw, JsonWriterState state) { |
| 51 | jw->state_index += 1; |
| 52 | assert(jw->state_index < JSON_MAX_DEPTH); |
| 53 | jw->state[jw->state_index] = state; |
| 54 | } |
| 55 | |
| 56 | static void jw_pop_state(JsonWriter *jw) { |
| 57 | assert(jw->state_index != 0); |
| 58 | jw->state_index -= 1; |
| 59 | } |
| 60 | |
| 61 | static void jw_begin_array(JsonWriter *jw) { |
| 62 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 63 | fprintf(jw->f, "["); |
| 64 | jw->state[jw->state_index] = JsonWriterStateArrayStart; |
| 65 | } |
| 66 | |
| 67 | static void jw_begin_object(JsonWriter *jw) { |
| 68 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 69 | fprintf(jw->f, "{"); |
| 70 | jw->state[jw->state_index] = JsonWriterStateObjectStart; |
| 71 | } |
| 72 | |
| 73 | static void jw_array_elem(JsonWriter *jw) { |
| 74 | switch (jw->state[jw->state_index]) { |
| 75 | case JsonWriterStateInvalid: |
| 76 | case JsonWriterStateValue: |
| 77 | case JsonWriterStateObjectStart: |
| 78 | case JsonWriterStateObject: |
| 79 | zig_unreachable(); |
| 80 | case JsonWriterStateArray: |
| 81 | fprintf(jw->f, ","); |
| 82 | ZIG_FALLTHROUGH; |
| 83 | case JsonWriterStateArrayStart: |
| 84 | jw->state[jw->state_index] = JsonWriterStateArray; |
| 85 | jw_push_state(jw, JsonWriterStateValue); |
| 86 | jw_nl_indent(jw); |
| 87 | return; |
| 88 | } |
| 89 | zig_unreachable(); |
| 90 | } |
| 91 | |
| 92 | static void jw_write_escaped_string(JsonWriter *jw, const char *s) { |
| 93 | fprintf(jw->f, "\""); |
| 94 | for (;; s += 1) { |
| 95 | switch (*s) { |
| 96 | case 0: |
| 97 | fprintf(jw->f, "\""); |
| 98 | return; |
| 99 | case '"': |
| 100 | fprintf(jw->f, "\\\""); |
| 101 | continue; |
| 102 | case '\t': |
| 103 | fprintf(jw->f, "\\t"); |
| 104 | continue; |
| 105 | case '\r': |
| 106 | fprintf(jw->f, "\\r"); |
| 107 | continue; |
| 108 | case '\n': |
| 109 | fprintf(jw->f, "\\n"); |
| 110 | continue; |
| 111 | case '\b': |
| 112 | fprintf(jw->f, "\\b"); |
| 113 | continue; |
| 114 | case '\f': |
| 115 | fprintf(jw->f, "\\f"); |
| 116 | continue; |
| 117 | case '\\': |
| 118 | fprintf(jw->f, "\\\\"); |
| 119 | continue; |
| 120 | default: |
| 121 | fprintf(jw->f, "%c", *s); |
| 122 | continue; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static void jw_object_field(JsonWriter *jw, const char *name) { |
| 128 | switch (jw->state[jw->state_index]) { |
| 129 | case JsonWriterStateInvalid: |
| 130 | case JsonWriterStateValue: |
| 131 | case JsonWriterStateArray: |
| 132 | case JsonWriterStateArrayStart: |
| 133 | zig_unreachable(); |
| 134 | case JsonWriterStateObject: |
| 135 | fprintf(jw->f, ","); |
| 136 | ZIG_FALLTHROUGH; |
| 137 | case JsonWriterStateObjectStart: |
| 138 | jw->state[jw->state_index] = JsonWriterStateObject; |
| 139 | jw_push_state(jw, JsonWriterStateValue); |
| 140 | jw_nl_indent(jw); |
| 141 | jw_write_escaped_string(jw, name); |
| 142 | fprintf(jw->f, ": "); |
| 143 | return; |
| 144 | } |
| 145 | zig_unreachable(); |
| 146 | } |
| 147 | |
| 148 | static void jw_end_array(JsonWriter *jw) { |
| 149 | switch (jw->state[jw->state_index]) { |
| 150 | case JsonWriterStateInvalid: |
| 151 | case JsonWriterStateValue: |
| 152 | case JsonWriterStateObjectStart: |
| 153 | case JsonWriterStateObject: |
| 154 | zig_unreachable(); |
| 155 | case JsonWriterStateArrayStart: |
| 156 | fprintf(jw->f, "]"); |
| 157 | jw_pop_state(jw); |
| 158 | return; |
| 159 | case JsonWriterStateArray: |
| 160 | jw_nl_indent(jw); |
| 161 | jw_pop_state(jw); |
| 162 | fprintf(jw->f, "]"); |
| 163 | return; |
| 164 | } |
| 165 | zig_unreachable(); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | static void jw_end_object(JsonWriter *jw) { |
| 170 | switch (jw->state[jw->state_index]) { |
| 171 | case JsonWriterStateInvalid: |
| 172 | zig_unreachable(); |
| 173 | case JsonWriterStateValue: |
| 174 | zig_unreachable(); |
| 175 | case JsonWriterStateArray: |
| 176 | zig_unreachable(); |
| 177 | case JsonWriterStateArrayStart: |
| 178 | zig_unreachable(); |
| 179 | case JsonWriterStateObjectStart: |
| 180 | fprintf(jw->f, "}"); |
| 181 | jw_pop_state(jw); |
| 182 | return; |
| 183 | case JsonWriterStateObject: |
| 184 | jw_nl_indent(jw); |
| 185 | jw_pop_state(jw); |
| 186 | fprintf(jw->f, "}"); |
| 187 | return; |
| 188 | } |
| 189 | zig_unreachable(); |
| 190 | } |
| 191 | |
| 192 | static void jw_null(JsonWriter *jw) { |
| 193 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 194 | fprintf(jw->f, "null"); |
| 195 | jw_pop_state(jw); |
| 196 | } |
| 197 | |
| 198 | static void jw_bool(JsonWriter *jw, bool x) { |
| 199 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 200 | if (x) { |
| 201 | fprintf(jw->f, "true"); |
| 202 | } else { |
| 203 | fprintf(jw->f, "false"); |
| 204 | } |
| 205 | jw_pop_state(jw); |
| 206 | } |
| 207 | |
| 208 | static void jw_int(JsonWriter *jw, int64_t x) { |
| 209 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 210 | if (x > 4503599627370496 || x < -4503599627370496) { |
| 211 | fprintf(jw->f, "\"%" ZIG_PRI_i64 "\"", x); |
| 212 | } else { |
| 213 | fprintf(jw->f, "%" ZIG_PRI_i64, x); |
| 214 | } |
| 215 | jw_pop_state(jw); |
| 216 | } |
| 217 | |
| 218 | static void jw_bigint(JsonWriter *jw, const BigInt *x) { |
| 219 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 220 | Buf *str = buf_alloc(); |
| 221 | bigint_append_buf(str, x, 10); |
| 222 | |
| 223 | if (bigint_fits_in_bits(x, 52, true)) { |
| 224 | fprintf(jw->f, "%s", buf_ptr(str)); |
| 225 | } else { |
| 226 | fprintf(jw->f, "\"%s\"", buf_ptr(str)); |
| 227 | } |
| 228 | jw_pop_state(jw); |
| 229 | |
| 230 | buf_destroy(str); |
| 231 | } |
| 232 | |
| 233 | static void jw_string(JsonWriter *jw, const char *s) { |
| 234 | assert(jw->state[jw->state_index] == JsonWriterStateValue); |
| 235 | jw_write_escaped_string(jw, s); |
| 236 | jw_pop_state(jw); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | static void tree_print(FILE *f, ZigType *ty, size_t indent); |
| 241 | |
| 242 | static int compare_type_abi_sizes_desc(const void *a, const void *b) { |
| 243 | uint64_t size_a = (*(ZigType * const*)(a))->abi_size; |
| 244 | uint64_t size_b = (*(ZigType * const*)(b))->abi_size; |
| 245 | if (size_a > size_b) |
| 246 | return -1; |
| 247 | if (size_a < size_b) |
| 248 | return 1; |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static void start_child(FILE *f, size_t indent) { |
| 253 | fprintf(f, "\n"); |
| 254 | for (size_t i = 0; i < indent; i += 1) { |
| 255 | fprintf(f, " "); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static void start_peer(FILE *f, size_t indent) { |
| 260 | fprintf(f, ",\n"); |
| 261 | for (size_t i = 0; i < indent; i += 1) { |
| 262 | fprintf(f, " "); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) { |
| 267 | ZigList<ZigType *> children = {}; |
| 268 | uint64_t sum_from_fields = 0; |
| 269 | for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) { |
| 270 | TypeStructField *field = struct_type->data.structure.fields[i]; |
| 271 | children.append(field->type_entry); |
| 272 | sum_from_fields += field->type_entry->abi_size; |
| 273 | } |
| 274 | qsort(children.items, children.length, sizeof(ZigType *), compare_type_abi_sizes_desc); |
| 275 | |
| 276 | start_peer(f, indent); |
| 277 | fprintf(f, "\"padding\": \"%" ZIG_PRI_u64 "\"", struct_type->abi_size - sum_from_fields); |
| 278 | |
| 279 | start_peer(f, indent); |
| 280 | fprintf(f, "\"fields\": ["); |
| 281 | |
| 282 | for (size_t i = 0; i < children.length; i += 1) { |
| 283 | if (i == 0) { |
| 284 | start_child(f, indent + 1); |
| 285 | } else { |
| 286 | start_peer(f, indent + 1); |
| 287 | } |
| 288 | fprintf(f, "{"); |
| 289 | |
| 290 | ZigType *child_type = children.at(i); |
| 291 | tree_print(f, child_type, indent + 2); |
| 292 | |
| 293 | start_child(f, indent + 1); |
| 294 | fprintf(f, "}"); |
| 295 | } |
| 296 | |
| 297 | start_child(f, indent); |
| 298 | fprintf(f, "]"); |
| 299 | } |
| 300 | |
| 301 | static void tree_print(FILE *f, ZigType *ty, size_t indent) { |
| 302 | start_child(f, indent); |
| 303 | fprintf(f, "\"type\": \"%s\"", buf_ptr(&ty->name)); |
| 304 | |
| 305 | start_peer(f, indent); |
| 306 | fprintf(f, "\"sizef\": \""); |
| 307 | zig_pretty_print_bytes(f, ty->abi_size); |
| 308 | fprintf(f, "\""); |
| 309 | |
| 310 | start_peer(f, indent); |
| 311 | fprintf(f, "\"size\": \"%" ZIG_PRI_usize "\"", ty->abi_size); |
| 312 | |
| 313 | switch (ty->id) { |
| 314 | case ZigTypeIdFnFrame: |
| 315 | return tree_print_struct(f, ty->data.frame.locals_struct, indent); |
| 316 | case ZigTypeIdStruct: |
| 317 | return tree_print_struct(f, ty, indent); |
| 318 | default: |
| 319 | start_child(f, indent); |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void zig_print_stack_report(CodeGen *g, FILE *f) { |
| 325 | if (g->largest_frame_fn == nullptr) { |
| 326 | fprintf(f, "{\"error\": \"No async function frames in entire compilation.\"}\n"); |
| 327 | return; |
| 328 | } |
| 329 | fprintf(f, "{"); |
| 330 | tree_print(f, g->largest_frame_fn->frame_type, 1); |
| 331 | |
| 332 | start_child(f, 0); |
| 333 | fprintf(f, "}\n"); |
| 334 | } |
| 335 | |
| 336 | struct AnalDumpCtx { |
| 337 | CodeGen *g; |
| 338 | JsonWriter jw; |
| 339 | |
| 340 | ZigList<ZigType *> type_list; |
| 341 | HashMap<const ZigType *, uint32_t, type_ptr_hash, type_ptr_eql> type_map; |
| 342 | |
| 343 | ZigList<ZigPackage *> pkg_list; |
| 344 | HashMap<const ZigPackage *, uint32_t, pkg_ptr_hash, pkg_ptr_eql> pkg_map; |
| 345 | |
| 346 | ZigList<Buf *> file_list; |
| 347 | HashMap<Buf *, uint32_t, buf_hash, buf_eql_buf> file_map; |
| 348 | |
| 349 | ZigList<Tld *> decl_list; |
| 350 | HashMap<const Tld *, uint32_t, tld_ptr_hash, tld_ptr_eql> decl_map; |
| 351 | |
| 352 | ZigList<ZigFn *> fn_list; |
| 353 | HashMap<const ZigFn *, uint32_t, fn_ptr_hash, fn_ptr_eql> fn_map; |
| 354 | HashMap<const ZigFn *, uint32_t, fn_ptr_hash, fn_ptr_eql> fn_decl_map; |
| 355 | |
| 356 | ZigList<AstNode *> node_list; |
| 357 | HashMap<const AstNode *, uint32_t, node_ptr_hash, node_ptr_eql> node_map; |
| 358 | |
| 359 | ZigList<ErrorTableEntry *> err_list; |
| 360 | HashMap<const ErrorTableEntry *, uint32_t, err_ptr_hash, err_ptr_eql> err_map; |
| 361 | }; |
| 362 | |
| 363 | static uint32_t anal_dump_get_type_id(AnalDumpCtx *ctx, ZigType *ty); |
| 364 | static void anal_dump_value(AnalDumpCtx *ctx, AstNode *source_node, ZigType *ty, ZigValue *value); |
| 365 | |
| 366 | static void anal_dump_poke_value(AnalDumpCtx *ctx, AstNode *source_node, ZigType *ty, ZigValue *value) { |
| 367 | Error err; |
| 368 | if (value->type != ty) { |
| 369 | return; |
| 370 | } |
| 371 | if ((err = ir_resolve_lazy(ctx->g, source_node, value))) { |
| 372 | codegen_report_errors_and_exit(ctx->g); |
| 373 | } |
| 374 | if (value->special == ConstValSpecialUndef) { |
| 375 | return; |
| 376 | } |
| 377 | if (value->special == ConstValSpecialRuntime) { |
| 378 | return; |
| 379 | } |
| 380 | switch (ty->id) { |
| 381 | case ZigTypeIdMetaType: { |
| 382 | ZigType *val_ty = value->data.x_type; |
| 383 | (void)anal_dump_get_type_id(ctx, val_ty); |
| 384 | return; |
| 385 | } |
| 386 | default: |
| 387 | return; |
| 388 | } |
| 389 | zig_unreachable(); |
| 390 | } |
| 391 | |
| 392 | static uint32_t anal_dump_get_type_id(AnalDumpCtx *ctx, ZigType *ty) { |
| 393 | uint32_t type_id = ctx->type_list.length; |
| 394 | auto existing_entry = ctx->type_map.put_unique(ty, type_id); |
| 395 | if (existing_entry == nullptr) { |
| 396 | ctx->type_list.append(ty); |
| 397 | } else { |
| 398 | type_id = existing_entry->value; |
| 399 | } |
| 400 | return type_id; |
| 401 | } |
| 402 | |
| 403 | static uint32_t anal_dump_get_pkg_id(AnalDumpCtx *ctx, ZigPackage *pkg) { |
| 404 | assert(pkg != nullptr); |
| 405 | uint32_t pkg_id = ctx->pkg_list.length; |
| 406 | auto existing_entry = ctx->pkg_map.put_unique(pkg, pkg_id); |
| 407 | if (existing_entry == nullptr) { |
| 408 | ctx->pkg_list.append(pkg); |
| 409 | } else { |
| 410 | pkg_id = existing_entry->value; |
| 411 | } |
| 412 | return pkg_id; |
| 413 | } |
| 414 | |
| 415 | static uint32_t anal_dump_get_file_id(AnalDumpCtx *ctx, Buf *file) { |
| 416 | uint32_t file_id = ctx->file_list.length; |
| 417 | auto existing_entry = ctx->file_map.put_unique(file, file_id); |
| 418 | if (existing_entry == nullptr) { |
| 419 | ctx->file_list.append(file); |
| 420 | } else { |
| 421 | file_id = existing_entry->value; |
| 422 | } |
| 423 | return file_id; |
| 424 | } |
| 425 | |
| 426 | static uint32_t anal_dump_get_node_id(AnalDumpCtx *ctx, AstNode *node) { |
| 427 | uint32_t node_id = ctx->node_list.length; |
| 428 | auto existing_entry = ctx->node_map.put_unique(node, node_id); |
| 429 | if (existing_entry == nullptr) { |
| 430 | ctx->node_list.append(node); |
| 431 | } else { |
| 432 | node_id = existing_entry->value; |
| 433 | } |
| 434 | return node_id; |
| 435 | } |
| 436 | |
| 437 | static uint32_t anal_dump_get_fn_id(AnalDumpCtx *ctx, ZigFn *fn) { |
| 438 | uint32_t fn_id = ctx->fn_list.length; |
| 439 | auto existing_entry = ctx->fn_map.put_unique(fn, fn_id); |
| 440 | if (existing_entry == nullptr) { |
| 441 | ctx->fn_list.append(fn); |
| 442 | |
| 443 | // poke the fn |
| 444 | (void)anal_dump_get_type_id(ctx, fn->type_entry); |
| 445 | (void)anal_dump_get_node_id(ctx, fn->proto_node); |
| 446 | } else { |
| 447 | fn_id = existing_entry->value; |
| 448 | } |
| 449 | return fn_id; |
| 450 | } |
| 451 | |
| 452 | static uint32_t anal_dump_get_err_id(AnalDumpCtx *ctx, ErrorTableEntry *err) { |
| 453 | uint32_t err_id = ctx->err_list.length; |
| 454 | auto existing_entry = ctx->err_map.put_unique(err, err_id); |
| 455 | if (existing_entry == nullptr) { |
| 456 | ctx->err_list.append(err); |
| 457 | } else { |
| 458 | err_id = existing_entry->value; |
| 459 | } |
| 460 | return err_id; |
| 461 | } |
| 462 | |
| 463 | static uint32_t anal_dump_get_decl_id(AnalDumpCtx *ctx, Tld *tld) { |
| 464 | uint32_t decl_id = ctx->decl_list.length; |
| 465 | auto existing_entry = ctx->decl_map.put_unique(tld, decl_id); |
| 466 | if (existing_entry == nullptr) { |
| 467 | ctx->decl_list.append(tld); |
| 468 | |
| 469 | if (tld->import != nullptr) { |
| 470 | (void)anal_dump_get_type_id(ctx, tld->import); |
| 471 | } |
| 472 | |
| 473 | // poke the types |
| 474 | switch (tld->id) { |
| 475 | case TldIdVar: { |
| 476 | TldVar *tld_var = reinterpret_cast<TldVar *>(tld); |
| 477 | ZigVar *var = tld_var->var; |
| 478 | |
| 479 | if (var != nullptr) { |
| 480 | (void)anal_dump_get_type_id(ctx, var->var_type); |
| 481 | |
| 482 | if (var->const_value != nullptr) { |
| 483 | anal_dump_poke_value(ctx, var->decl_node, var->var_type, var->const_value); |
| 484 | } |
| 485 | } |
| 486 | break; |
| 487 | } |
| 488 | case TldIdFn: { |
| 489 | TldFn *tld_fn = reinterpret_cast<TldFn *>(tld); |
| 490 | ZigFn *fn = tld_fn->fn_entry; |
| 491 | |
| 492 | if (fn != nullptr) { |
| 493 | (void)anal_dump_get_type_id(ctx, fn->type_entry); |
| 494 | ctx->fn_decl_map.put_unique(fn, decl_id); |
| 495 | } |
| 496 | break; |
| 497 | } |
| 498 | default: |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | } else { |
| 503 | decl_id = existing_entry->value; |
| 504 | } |
| 505 | return decl_id; |
| 506 | } |
| 507 | |
| 508 | static void anal_dump_type_ref(AnalDumpCtx *ctx, ZigType *ty) { |
| 509 | uint32_t type_id = anal_dump_get_type_id(ctx, ty); |
| 510 | jw_int(&ctx->jw, type_id); |
| 511 | } |
| 512 | |
| 513 | static void anal_dump_pkg_ref(AnalDumpCtx *ctx, ZigPackage *pkg) { |
| 514 | uint32_t pkg_id = anal_dump_get_pkg_id(ctx, pkg); |
| 515 | jw_int(&ctx->jw, pkg_id); |
| 516 | } |
| 517 | |
| 518 | static void anal_dump_file_ref(AnalDumpCtx *ctx, Buf *file) { |
| 519 | uint32_t file_id = anal_dump_get_file_id(ctx, file); |
| 520 | jw_int(&ctx->jw, file_id); |
| 521 | } |
| 522 | |
| 523 | static void anal_dump_node_ref(AnalDumpCtx *ctx, AstNode *node) { |
| 524 | uint32_t node_id = anal_dump_get_node_id(ctx, node); |
| 525 | jw_int(&ctx->jw, node_id); |
| 526 | } |
| 527 | |
| 528 | static void anal_dump_fn_ref(AnalDumpCtx *ctx, ZigFn *fn) { |
| 529 | uint32_t fn_id = anal_dump_get_fn_id(ctx, fn); |
| 530 | jw_int(&ctx->jw, fn_id); |
| 531 | } |
| 532 | |
| 533 | static void anal_dump_err_ref(AnalDumpCtx *ctx, ErrorTableEntry *err) { |
| 534 | uint32_t err_id = anal_dump_get_err_id(ctx, err); |
| 535 | jw_int(&ctx->jw, err_id); |
| 536 | } |
| 537 | |
| 538 | static void anal_dump_decl_ref(AnalDumpCtx *ctx, Tld *tld) { |
| 539 | uint32_t decl_id = anal_dump_get_decl_id(ctx, tld); |
| 540 | jw_int(&ctx->jw, decl_id); |
| 541 | } |
| 542 | |
| 543 | static void anal_dump_pkg(AnalDumpCtx *ctx, ZigPackage *pkg) { |
| 544 | JsonWriter *jw = &ctx->jw; |
| 545 | |
| 546 | Buf full_path_buf = BUF_INIT; |
| 547 | os_path_join(&pkg->root_src_dir, &pkg->root_src_path, &full_path_buf); |
| 548 | Buf *resolve_paths[] = { &full_path_buf, }; |
| 549 | Buf *resolved_path = buf_alloc(); |
| 550 | *resolved_path = os_path_resolve(resolve_paths, 1); |
| 551 | |
| 552 | auto import_entry = ctx->g->import_table.maybe_get(resolved_path); |
| 553 | if (!import_entry) { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | jw_array_elem(jw); |
| 558 | jw_begin_object(jw); |
| 559 | |
| 560 | jw_object_field(jw, "name"); |
| 561 | jw_string(jw, buf_ptr(&pkg->pkg_path)); |
| 562 | |
| 563 | jw_object_field(jw, "file"); |
| 564 | anal_dump_file_ref(ctx, resolved_path); |
| 565 | |
| 566 | jw_object_field(jw, "main"); |
| 567 | anal_dump_type_ref(ctx, import_entry->value); |
| 568 | |
| 569 | jw_object_field(jw, "table"); |
| 570 | jw_begin_object(jw); |
| 571 | auto it = pkg->package_table.entry_iterator(); |
| 572 | for (;;) { |
| 573 | auto *entry = it.next(); |
| 574 | if (!entry) |
| 575 | break; |
| 576 | |
| 577 | ZigPackage *child_pkg = entry->value; |
| 578 | if (child_pkg != nullptr) { |
| 579 | jw_object_field(jw, buf_ptr(entry->key)); |
| 580 | anal_dump_pkg_ref(ctx, child_pkg); |
| 581 | } |
| 582 | } |
| 583 | jw_end_object(jw); |
| 584 | |
| 585 | jw_end_object(jw); |
| 586 | } |
| 587 | |
| 588 | static void anal_dump_decl(AnalDumpCtx *ctx, Tld *tld) { |
| 589 | JsonWriter *jw = &ctx->jw; |
| 590 | |
| 591 | bool make_obj = tld->id == TldIdVar || tld->id == TldIdFn; |
| 592 | if (make_obj) { |
| 593 | jw_array_elem(jw); |
| 594 | jw_begin_object(jw); |
| 595 | |
| 596 | jw_object_field(jw, "import"); |
| 597 | anal_dump_type_ref(ctx, tld->import); |
| 598 | |
| 599 | jw_object_field(jw, "src"); |
| 600 | anal_dump_node_ref(ctx, tld->source_node); |
| 601 | |
| 602 | jw_object_field(jw, "name"); |
| 603 | jw_string(jw, buf_ptr(tld->name)); |
| 604 | } |
| 605 | |
| 606 | switch (tld->id) { |
| 607 | case TldIdVar: { |
| 608 | TldVar *tld_var = reinterpret_cast<TldVar *>(tld); |
| 609 | ZigVar *var = tld_var->var; |
| 610 | |
| 611 | if (var != nullptr) { |
| 612 | jw_object_field(jw, "kind"); |
| 613 | if (var->src_is_const) { |
| 614 | jw_string(jw, "const"); |
| 615 | } else { |
| 616 | jw_string(jw, "var"); |
| 617 | } |
| 618 | |
| 619 | if (var->is_thread_local) { |
| 620 | jw_object_field(jw, "threadlocal"); |
| 621 | jw_bool(jw, true); |
| 622 | } |
| 623 | |
| 624 | jw_object_field(jw, "type"); |
| 625 | anal_dump_type_ref(ctx, var->var_type); |
| 626 | |
| 627 | if (var->const_value != nullptr) { |
| 628 | jw_object_field(jw, "value"); |
| 629 | anal_dump_value(ctx, var->decl_node, var->var_type, var->const_value); |
| 630 | } |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | case TldIdFn: { |
| 635 | TldFn *tld_fn = reinterpret_cast<TldFn *>(tld); |
| 636 | ZigFn *fn = tld_fn->fn_entry; |
| 637 | |
| 638 | if (fn != nullptr) { |
| 639 | jw_object_field(jw, "kind"); |
| 640 | jw_string(jw, "const"); |
| 641 | |
| 642 | jw_object_field(jw, "type"); |
| 643 | anal_dump_type_ref(ctx, fn->type_entry); |
| 644 | |
| 645 | jw_object_field(jw, "value"); |
| 646 | anal_dump_fn_ref(ctx, fn); |
| 647 | } |
| 648 | break; |
| 649 | } |
| 650 | default: |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | if (make_obj) { |
| 655 | jw_end_object(jw); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | static void anal_dump_file(AnalDumpCtx *ctx, Buf *file) { |
| 660 | JsonWriter *jw = &ctx->jw; |
| 661 | jw_string(jw, buf_ptr(file)); |
| 662 | } |
| 663 | |
| 664 | static void anal_dump_value(AnalDumpCtx *ctx, AstNode *source_node, ZigType *ty, ZigValue *value) { |
| 665 | Error err; |
| 666 | |
| 667 | if (value->type != ty) { |
| 668 | jw_null(&ctx->jw); |
| 669 | return; |
| 670 | } |
| 671 | if ((err = ir_resolve_lazy(ctx->g, source_node, value))) { |
| 672 | codegen_report_errors_and_exit(ctx->g); |
| 673 | } |
| 674 | if (value->special == ConstValSpecialUndef) { |
| 675 | jw_string(&ctx->jw, "undefined"); |
| 676 | return; |
| 677 | } |
| 678 | if (value->special == ConstValSpecialRuntime) { |
| 679 | jw_null(&ctx->jw); |
| 680 | return; |
| 681 | } |
| 682 | switch (ty->id) { |
| 683 | case ZigTypeIdMetaType: { |
| 684 | ZigType *val_ty = value->data.x_type; |
| 685 | anal_dump_type_ref(ctx, val_ty); |
| 686 | return; |
| 687 | } |
| 688 | case ZigTypeIdFn: { |
| 689 | if (value->data.x_ptr.special == ConstPtrSpecialFunction) { |
| 690 | ZigFn *val_fn = value->data.x_ptr.data.fn.fn_entry; |
| 691 | anal_dump_fn_ref(ctx, val_fn); |
| 692 | } else { |
| 693 | jw_null(&ctx->jw); |
| 694 | } |
| 695 | return; |
| 696 | } |
| 697 | case ZigTypeIdOptional: { |
| 698 | if(optional_value_is_null(value)){ |
| 699 | jw_string(&ctx->jw, "null"); |
| 700 | } else { |
| 701 | jw_null(&ctx->jw); |
| 702 | } |
| 703 | return; |
| 704 | } |
| 705 | case ZigTypeIdBool: { |
| 706 | jw_string(&ctx->jw, value->data.x_bool ? "true" : "false"); |
| 707 | return; |
| 708 | } |
| 709 | case ZigTypeIdInt: { |
| 710 | jw_bigint(&ctx->jw, &value->data.x_bigint); |
| 711 | return; |
| 712 | } |
| 713 | default: |
| 714 | jw_null(&ctx->jw); |
| 715 | return; |
| 716 | } |
| 717 | zig_unreachable(); |
| 718 | } |
| 719 | |
| 720 | static void anal_dump_pointer_attrs(AnalDumpCtx *ctx, ZigType *ty) { |
| 721 | JsonWriter *jw = &ctx->jw; |
| 722 | if (ty->data.pointer.explicit_alignment != 0) { |
| 723 | jw_object_field(jw, "align"); |
| 724 | jw_int(jw, ty->data.pointer.explicit_alignment); |
| 725 | } |
| 726 | if (ty->data.pointer.is_const) { |
| 727 | jw_object_field(jw, "const"); |
| 728 | jw_bool(jw, true); |
| 729 | } |
| 730 | if (ty->data.pointer.is_volatile) { |
| 731 | jw_object_field(jw, "volatile"); |
| 732 | jw_bool(jw, true); |
| 733 | } |
| 734 | if (ty->data.pointer.allow_zero) { |
| 735 | jw_object_field(jw, "allowZero"); |
| 736 | jw_bool(jw, true); |
| 737 | } |
| 738 | if (ty->data.pointer.host_int_bytes != 0) { |
| 739 | jw_object_field(jw, "hostIntBytes"); |
| 740 | jw_int(jw, ty->data.pointer.host_int_bytes); |
| 741 | |
| 742 | jw_object_field(jw, "bitOffsetInHost"); |
| 743 | jw_int(jw, ty->data.pointer.bit_offset_in_host); |
| 744 | } |
| 745 | |
| 746 | jw_object_field(jw, "elem"); |
| 747 | anal_dump_type_ref(ctx, ty->data.pointer.child_type); |
| 748 | } |
| 749 | |
| 750 | static void anal_dump_type(AnalDumpCtx *ctx, ZigType *ty) { |
| 751 | JsonWriter *jw = &ctx->jw; |
| 752 | jw_array_elem(jw); |
| 753 | jw_begin_object(jw); |
| 754 | |
| 755 | jw_object_field(jw, "kind"); |
| 756 | jw_int(jw, type_id_index(ty)); |
| 757 | |
| 758 | switch (ty->id) { |
| 759 | case ZigTypeIdMetaType: |
| 760 | case ZigTypeIdBool: |
| 761 | case ZigTypeIdEnumLiteral: |
| 762 | break; |
| 763 | case ZigTypeIdStruct: { |
| 764 | if (ty->data.structure.special == StructSpecialSlice) { |
| 765 | jw_object_field(jw, "len"); |
| 766 | jw_int(jw, 2); |
| 767 | anal_dump_pointer_attrs(ctx, ty->data.structure.fields[slice_ptr_index]->type_entry); |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | jw_object_field(jw, "name"); |
| 772 | jw_string(jw, buf_ptr(&ty->name)); |
| 773 | |
| 774 | jw_object_field(jw, "src"); |
| 775 | anal_dump_node_ref(ctx, ty->data.structure.decl_node); |
| 776 | |
| 777 | { |
| 778 | jw_object_field(jw, "pubDecls"); |
| 779 | jw_begin_array(jw); |
| 780 | |
| 781 | ScopeDecls *decls_scope = ty->data.structure.decls_scope; |
| 782 | auto it = decls_scope->decl_table.entry_iterator(); |
| 783 | for (;;) { |
| 784 | auto *entry = it.next(); |
| 785 | if (!entry) |
| 786 | break; |
| 787 | |
| 788 | Tld *tld = entry->value; |
| 789 | if (tld->visib_mod == VisibModPub) { |
| 790 | jw_array_elem(jw); |
| 791 | anal_dump_decl_ref(ctx, tld); |
| 792 | } |
| 793 | } |
| 794 | jw_end_array(jw); |
| 795 | } |
| 796 | |
| 797 | { |
| 798 | jw_object_field(jw, "privDecls"); |
| 799 | jw_begin_array(jw); |
| 800 | |
| 801 | ScopeDecls *decls_scope = ty->data.structure.decls_scope; |
| 802 | auto it = decls_scope->decl_table.entry_iterator(); |
| 803 | for (;;) { |
| 804 | auto *entry = it.next(); |
| 805 | if (!entry) |
| 806 | break; |
| 807 | |
| 808 | Tld *tld = entry->value; |
| 809 | if (tld->visib_mod == VisibModPrivate) { |
| 810 | jw_array_elem(jw); |
| 811 | anal_dump_decl_ref(ctx, tld); |
| 812 | } |
| 813 | } |
| 814 | jw_end_array(jw); |
| 815 | } |
| 816 | |
| 817 | if (ty->data.structure.src_field_count != 0) { |
| 818 | jw_object_field(jw, "fields"); |
| 819 | jw_begin_array(jw); |
| 820 | |
| 821 | for(size_t i = 0; i < ty->data.structure.src_field_count; i += 1) { |
| 822 | jw_array_elem(jw); |
| 823 | anal_dump_type_ref(ctx, ty->data.structure.fields[i]->type_entry); |
| 824 | } |
| 825 | jw_end_array(jw); |
| 826 | } |
| 827 | |
| 828 | if (ty->data.structure.root_struct != nullptr) { |
| 829 | Buf *path_buf = ty->data.structure.root_struct->path; |
| 830 | |
| 831 | jw_object_field(jw, "file"); |
| 832 | anal_dump_file_ref(ctx, path_buf); |
| 833 | } |
| 834 | break; |
| 835 | } |
| 836 | case ZigTypeIdUnion: { |
| 837 | jw_object_field(jw, "name"); |
| 838 | jw_string(jw, buf_ptr(&ty->name)); |
| 839 | |
| 840 | jw_object_field(jw, "src"); |
| 841 | anal_dump_node_ref(ctx, ty->data.unionation.decl_node); |
| 842 | |
| 843 | { |
| 844 | jw_object_field(jw, "pubDecls"); |
| 845 | jw_begin_array(jw); |
| 846 | |
| 847 | ScopeDecls *decls_scope = ty->data.unionation.decls_scope; |
| 848 | auto it = decls_scope->decl_table.entry_iterator(); |
| 849 | for (;;) { |
| 850 | auto *entry = it.next(); |
| 851 | if (!entry) |
| 852 | break; |
| 853 | |
| 854 | Tld *tld = entry->value; |
| 855 | if (tld->visib_mod == VisibModPub) { |
| 856 | jw_array_elem(jw); |
| 857 | anal_dump_decl_ref(ctx, tld); |
| 858 | } |
| 859 | } |
| 860 | jw_end_array(jw); |
| 861 | } |
| 862 | |
| 863 | { |
| 864 | jw_object_field(jw, "privDecls"); |
| 865 | jw_begin_array(jw); |
| 866 | |
| 867 | ScopeDecls *decls_scope = ty->data.unionation.decls_scope; |
| 868 | auto it = decls_scope->decl_table.entry_iterator(); |
| 869 | for (;;) { |
| 870 | auto *entry = it.next(); |
| 871 | if (!entry) |
| 872 | break; |
| 873 | |
| 874 | Tld *tld = entry->value; |
| 875 | if (tld->visib_mod == VisibModPrivate) { |
| 876 | jw_array_elem(jw); |
| 877 | anal_dump_decl_ref(ctx, tld); |
| 878 | } |
| 879 | } |
| 880 | jw_end_array(jw); |
| 881 | } |
| 882 | |
| 883 | if (ty->data.unionation.src_field_count != 0) { |
| 884 | jw_object_field(jw, "fields"); |
| 885 | jw_begin_array(jw); |
| 886 | |
| 887 | for(size_t i = 0; i < ty->data.unionation.src_field_count; i += 1) { |
| 888 | jw_array_elem(jw); |
| 889 | anal_dump_type_ref(ctx, ty->data.unionation.fields[i].type_entry); |
| 890 | } |
| 891 | jw_end_array(jw); |
| 892 | } |
| 893 | break; |
| 894 | } |
| 895 | case ZigTypeIdEnum: { |
| 896 | jw_object_field(jw, "name"); |
| 897 | jw_string(jw, buf_ptr(&ty->name)); |
| 898 | |
| 899 | jw_object_field(jw, "src"); |
| 900 | anal_dump_node_ref(ctx, ty->data.enumeration.decl_node); |
| 901 | |
| 902 | { |
| 903 | jw_object_field(jw, "pubDecls"); |
| 904 | jw_begin_array(jw); |
| 905 | |
| 906 | ScopeDecls *decls_scope = ty->data.enumeration.decls_scope; |
| 907 | auto it = decls_scope->decl_table.entry_iterator(); |
| 908 | for (;;) { |
| 909 | auto *entry = it.next(); |
| 910 | if (!entry) |
| 911 | break; |
| 912 | |
| 913 | Tld *tld = entry->value; |
| 914 | if (tld->visib_mod == VisibModPub) { |
| 915 | jw_array_elem(jw); |
| 916 | anal_dump_decl_ref(ctx, tld); |
| 917 | } |
| 918 | } |
| 919 | jw_end_array(jw); |
| 920 | } |
| 921 | |
| 922 | { |
| 923 | jw_object_field(jw, "privDecls"); |
| 924 | jw_begin_array(jw); |
| 925 | |
| 926 | ScopeDecls *decls_scope = ty->data.enumeration.decls_scope; |
| 927 | auto it = decls_scope->decl_table.entry_iterator(); |
| 928 | for (;;) { |
| 929 | auto *entry = it.next(); |
| 930 | if (!entry) |
| 931 | break; |
| 932 | |
| 933 | Tld *tld = entry->value; |
| 934 | if (tld->visib_mod == VisibModPrivate) { |
| 935 | jw_array_elem(jw); |
| 936 | anal_dump_decl_ref(ctx, tld); |
| 937 | } |
| 938 | } |
| 939 | jw_end_array(jw); |
| 940 | } |
| 941 | |
| 942 | if (ty->data.enumeration.src_field_count != 0) { |
| 943 | jw_object_field(jw, "fields"); |
| 944 | jw_begin_array(jw); |
| 945 | |
| 946 | for(size_t i = 0; i < ty->data.enumeration.src_field_count; i += 1) { |
| 947 | jw_array_elem(jw); |
| 948 | jw_bigint(jw, &ty->data.enumeration.fields[i].value); |
| 949 | } |
| 950 | jw_end_array(jw); |
| 951 | } |
| 952 | break; |
| 953 | } |
| 954 | case ZigTypeIdFloat: { |
| 955 | jw_object_field(jw, "bits"); |
| 956 | jw_int(jw, ty->data.floating.bit_count); |
| 957 | break; |
| 958 | } |
| 959 | case ZigTypeIdInt: { |
| 960 | if (ty->data.integral.is_signed) { |
| 961 | jw_object_field(jw, "i"); |
| 962 | } else { |
| 963 | jw_object_field(jw, "u"); |
| 964 | } |
| 965 | jw_int(jw, ty->data.integral.bit_count); |
| 966 | break; |
| 967 | } |
| 968 | case ZigTypeIdFn: { |
| 969 | jw_object_field(jw, "name"); |
| 970 | jw_string(jw, buf_ptr(&ty->name)); |
| 971 | |
| 972 | jw_object_field(jw, "generic"); |
| 973 | jw_bool(jw, ty->data.fn.is_generic); |
| 974 | |
| 975 | if (ty->data.fn.fn_type_id.return_type != nullptr) { |
| 976 | jw_object_field(jw, "ret"); |
| 977 | anal_dump_type_ref(ctx, ty->data.fn.fn_type_id.return_type); |
| 978 | } |
| 979 | |
| 980 | if (ty->data.fn.fn_type_id.param_count != 0) { |
| 981 | jw_object_field(jw, "args"); |
| 982 | jw_begin_array(jw); |
| 983 | for (size_t i = 0; i < ty->data.fn.fn_type_id.param_count; i += 1) { |
| 984 | jw_array_elem(jw); |
| 985 | if (ty->data.fn.fn_type_id.param_info[i].type != nullptr) { |
| 986 | anal_dump_type_ref(ctx, ty->data.fn.fn_type_id.param_info[i].type); |
| 987 | } else { |
| 988 | jw_null(jw); |
| 989 | } |
| 990 | } |
| 991 | jw_end_array(jw); |
| 992 | } |
| 993 | break; |
| 994 | } |
| 995 | case ZigTypeIdOptional: { |
| 996 | jw_object_field(jw, "child"); |
| 997 | anal_dump_type_ref(ctx, ty->data.maybe.child_type); |
| 998 | break; |
| 999 | } |
| 1000 | case ZigTypeIdPointer: { |
| 1001 | switch (ty->data.pointer.ptr_len) { |
| 1002 | case PtrLenSingle: |
| 1003 | break; |
| 1004 | case PtrLenUnknown: |
| 1005 | jw_object_field(jw, "len"); |
| 1006 | jw_int(jw, 1); |
| 1007 | break; |
| 1008 | case PtrLenC: |
| 1009 | jw_object_field(jw, "len"); |
| 1010 | jw_int(jw, 3); |
| 1011 | break; |
| 1012 | } |
| 1013 | anal_dump_pointer_attrs(ctx, ty); |
| 1014 | break; |
| 1015 | } |
| 1016 | case ZigTypeIdErrorSet: { |
| 1017 | if (type_is_global_error_set(ty)) { |
| 1018 | break; |
| 1019 | } |
| 1020 | jw_object_field(jw, "name"); |
| 1021 | jw_string(jw, buf_ptr(&ty->name)); |
| 1022 | |
| 1023 | if (ty->data.error_set.infer_fn != nullptr) { |
| 1024 | jw_object_field(jw, "fn"); |
| 1025 | anal_dump_fn_ref(ctx, ty->data.error_set.infer_fn); |
| 1026 | } |
| 1027 | jw_object_field(jw, "errors"); |
| 1028 | jw_begin_array(jw); |
| 1029 | for (uint32_t i = 0; i < ty->data.error_set.err_count; i += 1) { |
| 1030 | jw_array_elem(jw); |
| 1031 | ErrorTableEntry *err = ty->data.error_set.errors[i]; |
| 1032 | anal_dump_err_ref(ctx, err); |
| 1033 | } |
| 1034 | jw_end_array(jw); |
| 1035 | break; |
| 1036 | } |
| 1037 | case ZigTypeIdErrorUnion: { |
| 1038 | jw_object_field(jw, "err"); |
| 1039 | anal_dump_type_ref(ctx, ty->data.error_union.err_set_type); |
| 1040 | |
| 1041 | jw_object_field(jw, "payload"); |
| 1042 | anal_dump_type_ref(ctx, ty->data.error_union.payload_type); |
| 1043 | |
| 1044 | break; |
| 1045 | } |
| 1046 | case ZigTypeIdArray: { |
| 1047 | jw_object_field(jw, "len"); |
| 1048 | jw_int(jw, ty->data.array.len); |
| 1049 | |
| 1050 | jw_object_field(jw, "elem"); |
| 1051 | anal_dump_type_ref(ctx, ty->data.array.child_type); |
| 1052 | break; |
| 1053 | } |
| 1054 | case ZigTypeIdVector: { |
| 1055 | jw_object_field(jw, "len"); |
| 1056 | jw_int(jw, ty->data.vector.len); |
| 1057 | |
| 1058 | jw_object_field(jw, "elem"); |
| 1059 | anal_dump_type_ref(ctx, ty->data.vector.elem_type); |
| 1060 | break; |
| 1061 | } |
| 1062 | case ZigTypeIdAnyFrame: { |
| 1063 | if (ty->data.any_frame.result_type != nullptr) { |
| 1064 | jw_object_field(jw, "result"); |
| 1065 | anal_dump_type_ref(ctx, ty->data.any_frame.result_type); |
| 1066 | } |
| 1067 | break; |
| 1068 | } |
| 1069 | case ZigTypeIdFnFrame: { |
| 1070 | jw_object_field(jw, "fnName"); |
| 1071 | jw_string(jw, buf_ptr(&ty->data.frame.fn->symbol_name)); |
| 1072 | |
| 1073 | jw_object_field(jw, "fn"); |
| 1074 | anal_dump_fn_ref(ctx, ty->data.frame.fn); |
| 1075 | break; |
| 1076 | } |
| 1077 | case ZigTypeIdInvalid: |
| 1078 | zig_unreachable(); |
| 1079 | default: |
| 1080 | jw_object_field(jw, "name"); |
| 1081 | jw_string(jw, buf_ptr(&ty->name)); |
| 1082 | break; |
| 1083 | } |
| 1084 | jw_end_object(jw); |
| 1085 | } |
| 1086 | |
| 1087 | static Buf *collect_doc_comments(RootStruct *root_struct, TokenIndex first_token) { |
| 1088 | if (first_token == 0) |
| 1089 | return nullptr; |
| 1090 | |
| 1091 | TokenId *token_ids = root_struct->token_ids; |
| 1092 | TokenLoc *token_locs = root_struct->token_locs; |
| 1093 | Buf *str = buf_alloc(); |
| 1094 | const char *source = buf_ptr(root_struct->source_code); |
| 1095 | TokenIndex doc_token = first_token; |
| 1096 | for (;token_ids[doc_token] == TokenIdDocComment; doc_token += 1) { |
| 1097 | // chops off '///' but leaves '\n' |
| 1098 | uint32_t start_pos = token_locs[doc_token].offset; |
| 1099 | uint32_t token_len = 0; |
| 1100 | while (source[start_pos + token_len] != '\n' && |
| 1101 | source[start_pos + token_len] != 0) |
| 1102 | { |
| 1103 | token_len += 1; |
| 1104 | } |
| 1105 | buf_append_mem(str, source + start_pos + 3, token_len - 3); |
| 1106 | } |
| 1107 | return str; |
| 1108 | } |
| 1109 | |
| 1110 | static void anal_dump_node(AnalDumpCtx *ctx, const AstNode *node) { |
| 1111 | JsonWriter *jw = &ctx->jw; |
| 1112 | |
| 1113 | jw_begin_object(jw); |
| 1114 | |
| 1115 | jw_object_field(jw, "file"); |
| 1116 | RootStruct *root_struct = node->owner->data.structure.root_struct; |
| 1117 | anal_dump_file_ref(ctx, root_struct->path); |
| 1118 | |
| 1119 | jw_object_field(jw, "line"); |
| 1120 | jw_int(jw, root_struct->token_locs[node->main_token].line); |
| 1121 | |
| 1122 | jw_object_field(jw, "col"); |
| 1123 | jw_int(jw, root_struct->token_locs[node->main_token].column); |
| 1124 | |
| 1125 | const Buf *doc_comments_buf = nullptr; |
| 1126 | const Buf *name_buf = nullptr; |
| 1127 | const ZigList<AstNode *> *field_nodes = nullptr; |
| 1128 | bool is_var_args = false; |
| 1129 | bool is_noalias = false; |
| 1130 | bool is_comptime = false; |
| 1131 | |
| 1132 | switch (node->type) { |
| 1133 | case NodeTypeParamDecl: |
| 1134 | doc_comments_buf = collect_doc_comments(root_struct, node->data.param_decl.doc_comments); |
| 1135 | name_buf = node->data.param_decl.name; |
| 1136 | is_var_args = node->data.param_decl.is_var_args; |
| 1137 | is_noalias = node->data.param_decl.is_noalias; |
| 1138 | is_comptime = node->data.param_decl.is_comptime; |
| 1139 | break; |
| 1140 | case NodeTypeFnProto: |
| 1141 | doc_comments_buf = collect_doc_comments(root_struct, node->data.fn_proto.doc_comments); |
| 1142 | field_nodes = &node->data.fn_proto.params; |
| 1143 | is_var_args = node->data.fn_proto.is_var_args; |
| 1144 | break; |
| 1145 | case NodeTypeVariableDeclaration: |
| 1146 | doc_comments_buf = collect_doc_comments(root_struct, node->data.variable_declaration.doc_comments); |
| 1147 | break; |
| 1148 | case NodeTypeErrorSetField: |
| 1149 | doc_comments_buf = collect_doc_comments(root_struct, node->data.err_set_field.doc_comments); |
| 1150 | break; |
| 1151 | case NodeTypeStructField: |
| 1152 | doc_comments_buf = collect_doc_comments(root_struct, node->data.struct_field.doc_comments); |
| 1153 | name_buf = node->data.struct_field.name; |
| 1154 | break; |
| 1155 | case NodeTypeContainerDecl: |
| 1156 | field_nodes = &node->data.container_decl.fields; |
| 1157 | doc_comments_buf = collect_doc_comments(root_struct, node->data.container_decl.doc_comments); |
| 1158 | break; |
| 1159 | default: |
| 1160 | break; |
| 1161 | } |
| 1162 | |
| 1163 | if (doc_comments_buf != nullptr && doc_comments_buf->list.length != 0) { |
| 1164 | jw_object_field(jw, "docs"); |
| 1165 | jw_string(jw, buf_ptr(doc_comments_buf)); |
| 1166 | } |
| 1167 | |
| 1168 | if (name_buf != nullptr) { |
| 1169 | jw_object_field(jw, "name"); |
| 1170 | jw_string(jw, buf_ptr(name_buf)); |
| 1171 | } |
| 1172 | |
| 1173 | if (field_nodes != nullptr) { |
| 1174 | jw_object_field(jw, "fields"); |
| 1175 | jw_begin_array(jw); |
| 1176 | for (size_t i = 0; i < field_nodes->length; i += 1) { |
| 1177 | jw_array_elem(jw); |
| 1178 | anal_dump_node_ref(ctx, field_nodes->at(i)); |
| 1179 | } |
| 1180 | jw_end_array(jw); |
| 1181 | } |
| 1182 | |
| 1183 | if (is_var_args) { |
| 1184 | jw_object_field(jw, "varArgs"); |
| 1185 | jw_bool(jw, true); |
| 1186 | } |
| 1187 | |
| 1188 | if (is_comptime) { |
| 1189 | jw_object_field(jw, "comptime"); |
| 1190 | jw_bool(jw, true); |
| 1191 | } |
| 1192 | |
| 1193 | if (is_noalias) { |
| 1194 | jw_object_field(jw, "noalias"); |
| 1195 | jw_bool(jw, true); |
| 1196 | } |
| 1197 | |
| 1198 | jw_end_object(jw); |
| 1199 | } |
| 1200 | |
| 1201 | static void anal_dump_err(AnalDumpCtx *ctx, const ErrorTableEntry *err) { |
| 1202 | JsonWriter *jw = &ctx->jw; |
| 1203 | |
| 1204 | jw_begin_object(jw); |
| 1205 | |
| 1206 | jw_object_field(jw, "src"); |
| 1207 | anal_dump_node_ref(ctx, err->decl_node); |
| 1208 | |
| 1209 | jw_object_field(jw, "name"); |
| 1210 | jw_string(jw, buf_ptr(&err->name)); |
| 1211 | |
| 1212 | jw_end_object(jw); |
| 1213 | } |
| 1214 | |
| 1215 | static void anal_dump_fn(AnalDumpCtx *ctx, ZigFn *fn) { |
| 1216 | JsonWriter *jw = &ctx->jw; |
| 1217 | |
| 1218 | jw_begin_object(jw); |
| 1219 | |
| 1220 | jw_object_field(jw, "src"); |
| 1221 | anal_dump_node_ref(ctx, fn->proto_node); |
| 1222 | |
| 1223 | jw_object_field(jw, "type"); |
| 1224 | anal_dump_type_ref(ctx, fn->type_entry); |
| 1225 | |
| 1226 | auto entry = ctx->fn_decl_map.maybe_get(fn); |
| 1227 | if (entry != nullptr) { |
| 1228 | jw_object_field(jw, "decl"); |
| 1229 | jw_int(jw, entry->value); |
| 1230 | } |
| 1231 | |
| 1232 | jw_end_object(jw); |
| 1233 | } |
| 1234 | |
| 1235 | void zig_print_analysis_dump(CodeGen *g, FILE *f, const char *one_indent, const char *nl) { |
| 1236 | AnalDumpCtx ctx = {}; |
| 1237 | ctx.g = g; |
| 1238 | JsonWriter *jw = &ctx.jw; |
| 1239 | jw_init(jw, f, one_indent, nl); |
| 1240 | ctx.type_map.init(16); |
| 1241 | ctx.pkg_map.init(16); |
| 1242 | ctx.file_map.init(16); |
| 1243 | ctx.decl_map.init(16); |
| 1244 | ctx.node_map.init(16); |
| 1245 | ctx.fn_map.init(16); |
| 1246 | ctx.fn_decl_map.init(16); |
| 1247 | ctx.err_map.init(16); |
| 1248 | |
| 1249 | jw_begin_object(jw); |
| 1250 | |
| 1251 | jw_object_field(jw, "typeKinds"); |
| 1252 | jw_begin_array(jw); |
| 1253 | for (size_t i = 0; i < type_id_len(); i += 1) { |
| 1254 | jw_array_elem(jw); |
| 1255 | jw_string(jw, type_id_name(type_id_at_index(i))); |
| 1256 | } |
| 1257 | jw_end_array(jw); |
| 1258 | |
| 1259 | jw_object_field(jw, "params"); |
| 1260 | jw_begin_object(jw); |
| 1261 | { |
| 1262 | jw_object_field(jw, "zigVersion"); |
| 1263 | jw_string(jw, stage2_version_string()); |
| 1264 | |
| 1265 | jw_object_field(jw, "builds"); |
| 1266 | jw_begin_array(jw); |
| 1267 | jw_array_elem(jw); |
| 1268 | jw_begin_object(jw); |
| 1269 | jw_object_field(jw, "target"); |
| 1270 | Buf triple_buf = BUF_INIT; |
| 1271 | target_triple_zig(&triple_buf, g->zig_target); |
| 1272 | jw_string(jw, buf_ptr(&triple_buf)); |
| 1273 | jw_end_object(jw); |
| 1274 | jw_end_array(jw); |
| 1275 | |
| 1276 | jw_object_field(jw, "rootName"); |
| 1277 | jw_string(jw, buf_ptr(g->root_out_name)); |
| 1278 | } |
| 1279 | jw_end_object(jw); |
| 1280 | |
| 1281 | jw_object_field(jw, "rootPkg"); |
| 1282 | anal_dump_pkg_ref(&ctx, g->main_pkg); |
| 1283 | |
| 1284 | // FIXME: Remove this ugly workaround. |
| 1285 | // Right now the code in docs/main.js relies on the root of the main package being itself. |
| 1286 | g->main_pkg->package_table.put(buf_create_from_str("root"), g->main_pkg); |
| 1287 | |
| 1288 | // Poke the functions |
| 1289 | for (size_t i = 0; i < g->fn_defs.length; i += 1) { |
| 1290 | ZigFn *fn = g->fn_defs.at(i); |
| 1291 | (void)anal_dump_get_fn_id(&ctx, fn); |
| 1292 | } |
| 1293 | |
| 1294 | jw_object_field(jw, "calls"); |
| 1295 | jw_begin_array(jw); |
| 1296 | { |
| 1297 | ZigList<ZigVar *> var_stack = {}; |
| 1298 | |
| 1299 | auto it = g->memoized_fn_eval_table.entry_iterator(); |
| 1300 | for (;;) { |
| 1301 | auto *entry = it.next(); |
| 1302 | if (!entry) |
| 1303 | break; |
| 1304 | |
| 1305 | var_stack.resize(0); |
| 1306 | ZigFn *fn = nullptr; |
| 1307 | |
| 1308 | Scope *scope = entry->key; |
| 1309 | while (scope != nullptr) { |
| 1310 | if (scope->id == ScopeIdVarDecl) { |
| 1311 | ZigVar *var = reinterpret_cast<ScopeVarDecl *>(scope)->var; |
| 1312 | var_stack.append(var); |
| 1313 | } else if (scope->id == ScopeIdFnDef) { |
| 1314 | fn = reinterpret_cast<ScopeFnDef *>(scope)->fn_entry; |
| 1315 | break; |
| 1316 | } |
| 1317 | scope = scope->parent; |
| 1318 | } |
| 1319 | ZigValue *result = entry->value; |
| 1320 | |
| 1321 | assert(fn != nullptr); |
| 1322 | |
| 1323 | jw_array_elem(jw); |
| 1324 | jw_begin_object(jw); |
| 1325 | |
| 1326 | jw_object_field(jw, "fn"); |
| 1327 | anal_dump_fn_ref(&ctx, fn); |
| 1328 | |
| 1329 | jw_object_field(jw, "result"); |
| 1330 | { |
| 1331 | jw_begin_object(jw); |
| 1332 | |
| 1333 | jw_object_field(jw, "type"); |
| 1334 | anal_dump_type_ref(&ctx, result->type); |
| 1335 | |
| 1336 | jw_object_field(jw, "value"); |
| 1337 | anal_dump_value(&ctx, scope->source_node, result->type, result); |
| 1338 | |
| 1339 | jw_end_object(jw); |
| 1340 | } |
| 1341 | |
| 1342 | if (var_stack.length != 0) { |
| 1343 | jw_object_field(jw, "args"); |
| 1344 | jw_begin_array(jw); |
| 1345 | |
| 1346 | while (var_stack.length != 0) { |
| 1347 | ZigVar *var = var_stack.pop(); |
| 1348 | |
| 1349 | jw_array_elem(jw); |
| 1350 | jw_begin_object(jw); |
| 1351 | |
| 1352 | jw_object_field(jw, "type"); |
| 1353 | anal_dump_type_ref(&ctx, var->var_type); |
| 1354 | |
| 1355 | jw_object_field(jw, "value"); |
| 1356 | anal_dump_value(&ctx, scope->source_node, var->var_type, var->const_value); |
| 1357 | |
| 1358 | jw_end_object(jw); |
| 1359 | } |
| 1360 | jw_end_array(jw); |
| 1361 | } |
| 1362 | |
| 1363 | jw_end_object(jw); |
| 1364 | } |
| 1365 | |
| 1366 | var_stack.deinit(); |
| 1367 | } |
| 1368 | jw_end_array(jw); |
| 1369 | |
| 1370 | jw_object_field(jw, "packages"); |
| 1371 | jw_begin_array(jw); |
| 1372 | for (uint32_t i = 0; i < ctx.pkg_list.length; i += 1) { |
| 1373 | anal_dump_pkg(&ctx, ctx.pkg_list.at(i)); |
| 1374 | } |
| 1375 | jw_end_array(jw); |
| 1376 | |
| 1377 | jw_object_field(jw, "types"); |
| 1378 | jw_begin_array(jw); |
| 1379 | |
| 1380 | for (uint32_t i = 0; i < ctx.type_list.length; i += 1) { |
| 1381 | ZigType *ty = ctx.type_list.at(i); |
| 1382 | anal_dump_type(&ctx, ty); |
| 1383 | } |
| 1384 | jw_end_array(jw); |
| 1385 | |
| 1386 | jw_object_field(jw, "decls"); |
| 1387 | jw_begin_array(jw); |
| 1388 | for (uint32_t i = 0; i < ctx.decl_list.length; i += 1) { |
| 1389 | Tld *decl = ctx.decl_list.at(i); |
| 1390 | anal_dump_decl(&ctx, decl); |
| 1391 | } |
| 1392 | jw_end_array(jw); |
| 1393 | |
| 1394 | jw_object_field(jw, "fns"); |
| 1395 | jw_begin_array(jw); |
| 1396 | for (uint32_t i = 0; i < ctx.fn_list.length; i += 1) { |
| 1397 | ZigFn *fn = ctx.fn_list.at(i); |
| 1398 | jw_array_elem(jw); |
| 1399 | anal_dump_fn(&ctx, fn); |
| 1400 | } |
| 1401 | jw_end_array(jw); |
| 1402 | |
| 1403 | jw_object_field(jw, "errors"); |
| 1404 | jw_begin_array(jw); |
| 1405 | for (uint32_t i = 0; i < ctx.err_list.length; i += 1) { |
| 1406 | const ErrorTableEntry *err = ctx.err_list.at(i); |
| 1407 | jw_array_elem(jw); |
| 1408 | anal_dump_err(&ctx, err); |
| 1409 | } |
| 1410 | jw_end_array(jw); |
| 1411 | |
| 1412 | jw_object_field(jw, "astNodes"); |
| 1413 | jw_begin_array(jw); |
| 1414 | for (uint32_t i = 0; i < ctx.node_list.length; i += 1) { |
| 1415 | const AstNode *node = ctx.node_list.at(i); |
| 1416 | jw_array_elem(jw); |
| 1417 | anal_dump_node(&ctx, node); |
| 1418 | } |
| 1419 | jw_end_array(jw); |
| 1420 | |
| 1421 | jw_object_field(jw, "files"); |
| 1422 | jw_begin_array(jw); |
| 1423 | for (uint32_t i = 0; i < ctx.file_list.length; i += 1) { |
| 1424 | Buf *file = ctx.file_list.at(i); |
| 1425 | jw_array_elem(jw); |
| 1426 | anal_dump_file(&ctx, file); |
| 1427 | } |
| 1428 | jw_end_array(jw); |
| 1429 | |
| 1430 | jw_end_object(jw); |
| 1431 | } |