| 516 | static CBM_TLS ks_slot_t ks_cache[KS_SLOTS]; |
| 517 | |
| 518 | static ks_slot_t *ks_build(const TSLanguage *lang, const char *const *types, ks_slot_t *s) { |
| 519 | s->lang = lang; |
| 520 | s->types = types; |
| 521 | s->bits = NULL; |
| 522 | s->nsyms = 0; |
| 523 | s->exact = false; |
| 524 | uint32_t nsyms = ts_language_symbol_count(lang); |
| 525 | if (nsyms == 0) { |
| 526 | return s; /* fall back to strcmp */ |
| 527 | } |
| 528 | uint64_t *bits = calloc(((size_t)nsyms + 63) / 64, sizeof(uint64_t)); |
| 529 | if (!bits) { |
| 530 | return s; |
| 531 | } |
| 532 | bool all_resolved = true; |
| 533 | for (const char *const *t = types; *t; t++) { |
| 534 | uint32_t len = (uint32_t)strlen(*t); |
| 535 | /* A name may be a named node type or an anonymous token ("for", "&&"): |
| 536 | * set whichever symbol(s) exist so ts_node_symbol matches either. */ |
| 537 | TSSymbol sn = ts_language_symbol_for_name(lang, *t, len, true); |
| 538 | TSSymbol sa = ts_language_symbol_for_name(lang, *t, len, false); |
| 539 | bool any = false; |
| 540 | if (sn != 0 && sn < nsyms) { |
| 541 | bits[sn >> 6] |= (uint64_t)1 << (sn & 63); |
| 542 | any = true; |
| 543 | } |
| 544 | if (sa != 0 && sa < nsyms) { |
| 545 | bits[sa >> 6] |= (uint64_t)1 << (sa & 63); |
| 546 | any = true; |
| 547 | } |
| 548 | if (!any) { |
| 549 | all_resolved = false; /* unknown name → can't represent exactly */ |
| 550 | } |
| 551 | } |
| 552 | if (!all_resolved) { |
| 553 | free(bits); |
| 554 | return s; /* exact stays false */ |
| 555 | } |
| 556 | s->bits = bits; |
| 557 | s->nsyms = nsyms; |
| 558 | s->exact = true; |
| 559 | return s; |
| 560 | } |
| 561 | |
| 562 | /* Find or build the cache slot for (lang, types). Returns NULL only if the |
| 563 | * thread-local table is saturated at this hash (extremely rare → strcmp). */ |