1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Many files at once: the summaries, the edges between them, and the queries that need both.
//!
//! [`crate::index`] turns one file into a [`FileSummary`]. This module is what holds a project's worth of them
//! and answers the questions that cross a file boundary — which is the whole reason an index exists, and the
//! layer calls `resolve`.
//!
//! # Why the reverse map is built here and not stored
//!
//! A summary stores the includes its own file *writes* — direct edges, one per `#include`. The map from a header
//! back to the files that include it is therefore **derived**, and it is derived here, in memory, from the
//! summaries as they are loaded. Writing it to disk would be storing a conclusion: it is a fact about the graph
//! rather than about any file, so it would have no single file to go stale with, and the first inconsistency
//! between it and the summaries would be invisible. Rebuilding it costs one pass over the includes, which is
//! what the summaries are for.
//!
//! # What visible means here
//!
//! ```text
//! a.h declares Widget
//! b.h #include "a.h"
//! c.cpp #include "b.h" -> Widget is visible in c.cpp
//! d.cpp (nothing) -> Widget is not visible in d.cpp, even though it is in the project
//! ```
//!
//! So a name is visible in a file when the file **transitively includes** the file that declares it. That is a
//! graph reachability question, and it is answered without re-reading anything: the edges are in the summaries.
//!
//! # The one case this cannot decide, and what it says instead
//!
//! An `#include` written inside an `#if` is a fact about the text, not about a compilation: whether the compiler
//! took that branch depends on macros the index does not have. So a declaration reached only through a
//! **guarded** include is reported as [`Known::Unknown`] rather than as visible or invisible — the same rule the
//! rest of the crate follows, and the reason [`IncludeFact`](crate::summary::IncludeFact) carries a guard at all.
use ;
use ;
use Arc;
use CppSyntaxKind;
use ;
use crateVisibility;
use cratenormalize_path;
use crateMarked;
use crateIncludeForm;
use crate;
use crate;
use crate;
/// How many files a visibility walk will cross before giving up.
///
/// A backstop rather than a policy: real include graphs are shallow (the walker's own limit is
/// [`crate::MAX_INCLUDE_DEPTH`]), and a corpus that exceeds this is one whose summaries disagree with its
/// includes — which the visited set already handles. The number is here so that the *round* count of a
/// pathological graph cannot become an unbounded amount of work on a keystroke.
const MAX_VISIBILITY_DEPTH: usize = 128;
/// **Where an `#include` points** — the target a reader asks about by pointing at the header's name.
///
/// The fourth answer a "go to definition" can give, beside a declaration, an overload set and a macro, and the one
/// it was missing: nothing in the file *declares* `vector`, so every question the jump is built out of — the scope
/// walk, the index by name, the macro table — answers nothing at all for `#include <vector>`, and a reader who
/// ctrl-clicks it is told there is no definition to go to.
///
/// The name is the spelling between the delimiters (`vector`, `sys/types.h`), which is what the resolver searched
/// for and what a reader wrote; the delimiters are the *form*, which is why [`HeaderTarget::form`] is separate.
pub use *;
/// **One file's cooked reading, as the index keeps it** — what a compiler sees written there, and what the parse of
/// that text complained about.
///
/// The two travel together because they are one reading of one rendering: a caller that handed over the declarations
/// and dropped the errors would make the index answer "nothing is wrong with this file" for a rendering the parser
/// had something to say about, and a caller that replaced the declarations without replacing the errors would
/// publish the *old* file's errors beside the new file's names.
/// **How many cooked readings are kept** — the map is sparse by design, so this only bounds the closure of the
/// open files, not what a query can answer: a miss falls back to the file`s own reading.
const MAX_COOKED_FILES: usize = 512;
/// **What one file was read as**, beyond its own summary: the renderings of it, one per translation unit.
///
/// A file's summary is the file read **on its own** — its own text, its own closure, its own environment. A cooked
/// reading is the file read **inside a program**, and a header can be read by several programs: `a.cpp` that defines
/// `_GNU_SOURCE` before including it and `b.cpp` that does not read *different declarations* out of the same bytes.
/// So the readings are kept per program, and which one a lookup uses is decided by the **asking** file's program —
/// see [`ProjectIndex::the_reading_for`].
///
/// The id a [`Posting`] carries is **`0` for the file's own summary** (which lives in `ProjectIndex::summaries`, not
/// here), `1` for the reading it was cooked into on its own, and `2 + its position` for a program's reading — so a
/// reading keeps its id for as long as the file does and a re-read replaces it in place.
/// A project's summaries, and the queries that need more than one of them.
///
/// Built incrementally: [`ProjectIndex::insert`] takes a summary that some other layer produced, which is what
/// keeps this type free of any opinion about parsing, caching or the filesystem.
/// **What evaluating guarded `#include` conditions has cost**, in asks and nanoseconds — see the `FactGuard::Region`
/// arm and the `[visibility]` probe. Only written when `CPPLS_TRACE_TIME` is on, so a normal run does not touch it.
pub static CONDITION_ASKS: AtomicU64 =
new;
pub static CONDITION_NANOS: AtomicU64 =
new;
/// One entry of [`ProjectIndex::member_lists`]: the members, and **what the answer was made of**.
///
/// The contributors are the files whose facts the list holds — every member's own file. An entry is dropped when one
/// of them changes, which is the narrowest invalidation that is sound without recording which *bases* were walked:
/// a base's members are in the list and carry the base's file.
///
/// # A list with an `unlisted` base, and the generation it was built at
///
/// A gap — "this base is not declared anywhere I can see" — is a statement about the **files that have been read**, not
/// about the class, so such a list used to be remembered at all: every file that mentioned the class rebuilt it.
/// Measured on a real project: `std::vector<LuaSyntaxNode>` 1.7 ms from one file and **20 ms** from the next, twelve
/// lists built per pull and **zero hits**. Remembering it together with the **number of summaries at the time** is
/// sound instead: a file being read is exactly what can fill the gap, so an entry built at an older count is treated as
/// absent. A pull reads nothing new, so the count does not move and the entry is reused.
/// How many member lists [`ProjectIndex::member_lists`] may hold before it is emptied.
///
/// A list is a few hundred [`ProjectMember`]s, each with a `DeclFact`, so an entry is tens to a hundred kilobytes and
/// this is a few tens of megabytes at worst. Cleared whole rather than evicted one at a time: the sets that matter
/// are "the classes this file mentions", which are asked together.
const MAX_CACHED_MEMBER_LISTS: usize = 96;
/// One entry of [`ProjectIndex::macro_environments`]: the answer, and **the files it was made of**.
///
/// The contributor rule is [`ProjectIndex::visibility`]'s, for the same reason: a walk's answer is a function of the
/// files it read, so a re-read of one of them invalidates precisely this entry and nothing else.
/// One entry of [`ProjectIndex::macro_names`] — the same shape, for the whole-name-set walk.
/// How many macro walks each memo may hold before it is emptied.
///
/// An environment is one name's candidates (a handful of facts) and a name set is tens of thousands of short strings,
/// so a few hundred entries is tens of megabytes at worst. Cleared whole when full, like the other memos: the sets
/// that matter are "the names this file asks about", which are asked together.
const MAX_CACHED_MACRO_WALKS: usize = 256;
/// How many declaration answers [`ProjectIndex::definition_answers`] may hold before it is emptied.
///
/// A pull asks for a few dozen spellings and the whole point of the memo is that the repeats hit; the bound is
/// there so that a long-running session cannot grow it without limit, and emptying it costs one pull's lookups.
const MAX_DEFINITION_ANSWERS: usize = 4096;
/// How many guard verdicts [`ProjectIndex::a_guard_is_taken`] may hold before it is emptied.
///
/// One per guarded declaration any lookup has asked about — a few thousand on a standard-library closure, each a
/// `(path, offset, region)` and an enum.
const MAX_GUARD_ANSWERS: usize = 16_384;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// **[member_candidates] — one member name's whole overload set.**
pub use member_candidates;
pub use *;
pub use *;
pub use *;
pub use *;
/// The name index against the scan it replaced: the same questions, asked of the same index, answered both ways.
///
/// The inverted index is derived data, and derived data has one failure that no ordinary test finds — it drifts
/// from what it was derived from after some particular sequence of edits. So this builds a corpus that has every
/// kind of collision (one name in many files, one name in many scopes, a name only the cooked reading declares),
/// mutates it the way a session does (forget, re-insert edited, cook, forget the cooking), and after **every**
/// step asserts that each name query equals the brute-force answer over `summaries()`.
pub use *;