@@ -13,14 +13,22 @@ pub const Trigram = [3]u8;
1313pub const Declaration = struct {
1414 pub const Index = enum (u32 ) { _ };
1515
16+ pub const Kind = enum {
17+ variable ,
18+ constant ,
19+ function ,
20+ test_function ,
21+ };
22+
23+ /// Either `.identifier` or `.string_literal`.
1624 name : Ast.TokenIndex ,
25+ kind : Kind ,
1726};
1827
1928has_filter : bool ,
2029filter_buckets : std .ArrayListUnmanaged (CuckooFilter .Bucket ),
2130trigram_to_declarations : std .AutoArrayHashMapUnmanaged (Trigram , std .ArrayListUnmanaged (Declaration .Index )),
2231declarations : std .MultiArrayList (Declaration ),
23- names : std .ArrayListUnmanaged (u8 ),
2432
2533pub fn init (
2634 allocator : std.mem.Allocator ,
@@ -31,7 +39,6 @@ pub fn init(
3139 .filter_buckets = .empty ,
3240 .trigram_to_declarations = .empty ,
3341 .declarations = .empty ,
34- .names = .empty ,
3542 };
3643 errdefer store .deinit (allocator );
3744
@@ -45,7 +52,6 @@ pub fn init(
4552 const old_in_function = context .in_function ;
4653 defer context .in_function = old_in_function ;
4754
48- var name_token_maybe : ? Ast.TokenIndex = null ;
4955 switch (cb_tree .nodeTag (node )) {
5056 .fn_proto ,
5157 .fn_proto_multi ,
@@ -57,7 +63,12 @@ pub fn init(
5763 const fn_token = cb_tree .nodeMainToken (node );
5864 if (cb_tree .tokenTag (fn_token + 1 ) != .identifier ) break :skip ;
5965
60- name_token_maybe = fn_token + 1 ;
66+ try context .store .appendDeclaration (
67+ context .allocator ,
68+ offsets .identifierTokenToNameSlice (cb_tree , fn_token + 1 ),
69+ fn_token + 1 ,
70+ .function ,
71+ );
6172 },
6273 .root = > unreachable ,
6374 .container_decl ,
@@ -80,17 +91,34 @@ pub fn init(
8091 .aligned_var_decl ,
8192 = > skip : {
8293 if (context .in_function ) break :skip ;
83- name_token_maybe = cb_tree .nodeMainToken (node ) + 1 ;
94+
95+ const main_token = cb_tree .nodeMainToken (node );
96+
97+ const kind : Declaration.Kind = switch (cb_tree .tokenTag (main_token )) {
98+ .keyword_var = > .variable ,
99+ .keyword_const = > .constant ,
100+ else = > unreachable ,
101+ };
102+
103+ try context .store .appendDeclaration (
104+ context .allocator ,
105+ offsets .identifierTokenToNameSlice (cb_tree , main_token + 1 ),
106+ main_token + 1 ,
107+ kind ,
108+ );
84109 },
85- else = > {},
86- }
87110
88- if (name_token_maybe ) | name_token | {
89- try context .store .appendDeclaration (
90- context .allocator ,
91- cb_tree ,
92- name_token ,
93- );
111+ .test_decl = > skip : {
112+ const test_name_token , const test_name = ast .testDeclNameAndToken (cb_tree , node ) orelse break :skip ;
113+
114+ try context .store .appendDeclaration (
115+ context .allocator ,
116+ test_name ,
117+ test_name_token ,
118+ .test_function ,
119+ );
120+ },
121+ else = > {},
94122 }
95123
96124 try ast .iterateChildren (cb_tree , node , context , Error , callback );
@@ -149,23 +177,21 @@ pub fn deinit(store: *TrigramStore, allocator: std.mem.Allocator) void {
149177 }
150178 store .trigram_to_declarations .deinit (allocator );
151179 store .declarations .deinit (allocator );
152- store .names .deinit (allocator );
153180 store .* = undefined ;
154181}
155182
156- /// Caller must not submit name.len < 3.
157183fn appendDeclaration (
158184 store : * TrigramStore ,
159185 allocator : std.mem.Allocator ,
160- tree : Ast ,
186+ name : [] const u8 ,
161187 name_token : Ast.TokenIndex ,
188+ kind : Declaration.Kind ,
162189) error {OutOfMemory }! void {
163- const loc = offsets .identifierTokenToNameLoc (tree , name_token );
164- const name = offsets .locToSlice (tree .source , loc );
165190 if (name .len < 3 ) return ;
166191
167192 try store .declarations .append (allocator , .{
168193 .name = name_token ,
194+ .kind = kind ,
169195 });
170196
171197 for (0.. name .len - 2 ) | index | {
0 commit comments