store document scope declarations in std.ArrayList

This commit is contained in:
Techatrix 2023-05-22 01:05:37 +02:00
parent 6f7f9dab9d
commit a378eb74b1

View File

@ -1032,7 +1032,8 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
const new_handle = analyser.store.getOrLoadHandle(builtin_uri) orelse return null; const new_handle = analyser.store.getOrLoadHandle(builtin_uri) orelse return null;
const root_scope_decls = new_handle.document_scope.scopes.items(.decls)[0]; const root_scope_decls = new_handle.document_scope.scopes.items(.decls)[0];
const decl = root_scope_decls.get("Type") orelse return null; const decl_index = root_scope_decls.get("Type") orelse return null;
const decl = new_handle.document_scope.decls.items[@enumToInt(decl_index)];
if (decl != .ast_node) return null; if (decl != .ast_node) return null;
const var_decl = new_handle.tree.fullVarDecl(decl.ast_node) orelse return null; const var_decl = new_handle.tree.fullVarDecl(decl.ast_node) orelse return null;
@ -1969,6 +1970,8 @@ pub const Declaration = union(enum) {
/// always an identifier /// always an identifier
error_token: Ast.Node.Index, error_token: Ast.Node.Index,
pub const Index = enum(u32) { _ };
pub fn eql(a: Declaration, b: Declaration) bool { pub fn eql(a: Declaration, b: Declaration) bool {
return std.meta.eql(a, b); return std.meta.eql(a, b);
} }
@ -2127,9 +2130,11 @@ pub const DeclWithHandle = struct {
const scope_index = findContainerScopeIndex(.{ .node = switch_expr_type.type.data.other, .handle = switch_expr_type.handle }) orelse return null; const scope_index = findContainerScopeIndex(.{ .node = switch_expr_type.type.data.other, .handle = switch_expr_type.handle }) orelse return null;
const scope_decls = switch_expr_type.handle.document_scope.scopes.items(.decls); const scope_decls = switch_expr_type.handle.document_scope.scopes.items(.decls);
const candidate = scope_decls[scope_index].getEntry(tree.tokenSlice(main_tokens[pay.items[0]])) orelse return null; const name = tree.tokenSlice(main_tokens[pay.items[0]]);
const decl_index = scope_decls[scope_index].get(name) orelse return null;
const decl = switch_expr_type.handle.document_scope.decls.items[@enumToInt(decl_index)];
switch (candidate.value_ptr.*) { switch (decl) {
.ast_node => |node| { .ast_node => |node| {
if (switch_expr_type.handle.tree.fullContainerField(node)) |container_field| { if (switch_expr_type.handle.tree.fullContainerField(node)) |container_field| {
if (container_field.ast.type_expr != 0) { if (container_field.ast.type_expr != 0) {
@ -2186,9 +2191,10 @@ fn iterateSymbolsContainerInternal(
const scope_uses = handle.document_scope.scopes.items(.uses); const scope_uses = handle.document_scope.scopes.items(.uses);
const container_scope_index = findContainerScopeIndex(container_handle) orelse return; const container_scope_index = findContainerScopeIndex(container_handle) orelse return;
var decl_it = scope_decls[container_scope_index].iterator(); var decl_it = scope_decls[container_scope_index].valueIterator();
while (decl_it.next()) |entry| { while (decl_it.next()) |decl_index| {
switch (entry.value_ptr.*) { const decl = &handle.document_scope.decls.items[@enumToInt(decl_index.*)];
switch (decl.*) {
.ast_node => |node| { .ast_node => |node| {
if (node_tags[node].isContainerField()) { if (node_tags[node].isContainerField()) {
if (!instance_access and !is_enum) continue; if (!instance_access and !is_enum) continue;
@ -2205,9 +2211,9 @@ fn iterateSymbolsContainerInternal(
else => {}, else => {},
} }
const decl = DeclWithHandle{ .decl = entry.value_ptr, .handle = handle }; const decl_with_handle = DeclWithHandle{ .decl = decl, .handle = handle };
if (handle != orig_handle and !decl.isPublic()) continue; if (handle != orig_handle and !decl_with_handle.isPublic()) continue;
try callback(context, decl); try callback(context, decl_with_handle);
} }
for (scope_uses[container_scope_index]) |use| { for (scope_uses[container_scope_index]) |use| {
@ -2285,13 +2291,11 @@ pub fn iterateLabels(handle: *const DocumentStore.Handle, source_index: usize, c
var scope_iterator = iterateEnclosingScopes(handle.document_scope, source_index); var scope_iterator = iterateEnclosingScopes(handle.document_scope, source_index);
while (scope_iterator.next()) |scope_index| { while (scope_iterator.next()) |scope_index| {
var decl_it = scope_decls[@enumToInt(scope_index)].iterator(); var decl_it = scope_decls[@enumToInt(scope_index)].valueIterator();
while (decl_it.next()) |entry| { while (decl_it.next()) |decl_index| {
switch (entry.value_ptr.*) { const decl = &handle.document_scope.decls.items[@enumToInt(decl_index.*)];
.label_decl => {}, if (decl.* != .label_decl) continue;
else => continue, try callback(context, DeclWithHandle{ .decl = decl, .handle = handle });
}
try callback(context, DeclWithHandle{ .decl = entry.value_ptr, .handle = handle });
} }
} }
} }
@ -2308,12 +2312,12 @@ fn iterateSymbolsGlobalInternal(
var scope_iterator = iterateEnclosingScopes(handle.document_scope, source_index); var scope_iterator = iterateEnclosingScopes(handle.document_scope, source_index);
while (scope_iterator.next()) |scope_index| { while (scope_iterator.next()) |scope_index| {
var decl_it = scope_decls[@enumToInt(scope_index)].iterator(); var decl_it = scope_decls[@enumToInt(scope_index)].valueIterator();
while (decl_it.next()) |entry| { while (decl_it.next()) |decl_index| {
if (entry.value_ptr.* == .ast_node and const decl = &handle.document_scope.decls.items[@enumToInt(decl_index.*)];
handle.tree.nodes.items(.tag)[entry.value_ptr.*.ast_node].isContainerField()) continue; if (decl.* == .ast_node and handle.tree.nodes.items(.tag)[decl.ast_node].isContainerField()) continue;
if (entry.value_ptr.* == .label_decl) continue; if (decl.* == .label_decl) continue;
try callback(context, DeclWithHandle{ .decl = entry.value_ptr, .handle = handle }); try callback(context, DeclWithHandle{ .decl = decl, .handle = handle });
} }
for (scope_uses[@enumToInt(scope_index)]) |use| { for (scope_uses[@enumToInt(scope_index)]) |use| {
@ -2425,16 +2429,12 @@ pub fn lookupLabel(
var scope_iterator = iterateEnclosingScopes(handle.document_scope, source_index); var scope_iterator = iterateEnclosingScopes(handle.document_scope, source_index);
while (scope_iterator.next()) |scope_index| { while (scope_iterator.next()) |scope_index| {
const candidate = scope_decls[@enumToInt(scope_index)].getEntry(symbol) orelse continue; const decl_index = scope_decls[@enumToInt(scope_index)].get(symbol) orelse continue;
const decl = &handle.document_scope.decls.items[@enumToInt(decl_index)];
switch (candidate.value_ptr.*) { if (decl.* != .label_decl) continue;
.label_decl => {},
else => continue, return DeclWithHandle{ .decl = decl, .handle = handle };
}
return DeclWithHandle{
.decl = candidate.value_ptr,
.handle = handle,
};
} }
return null; return null;
} }
@ -2449,18 +2449,16 @@ pub fn lookupSymbolGlobal(analyser: *Analyser, handle: *const DocumentStore.Hand
while (current_scope != .none) { while (current_scope != .none) {
const scope_index = @enumToInt(current_scope); const scope_index = @enumToInt(current_scope);
defer current_scope = scope_parents[scope_index]; defer current_scope = scope_parents[scope_index];
if (scope_decls[scope_index].getEntry(symbol)) |candidate| { if (scope_decls[scope_index].get(symbol)) |decl_index| {
switch (candidate.value_ptr.*) { const decl = &handle.document_scope.decls.items[@enumToInt(decl_index)];
switch (decl.*) {
.ast_node => |node| { .ast_node => |node| {
if (handle.tree.nodes.items(.tag)[node].isContainerField()) continue; if (handle.tree.nodes.items(.tag)[node].isContainerField()) continue;
}, },
.label_decl => continue, .label_decl => continue,
else => {}, else => {},
} }
return DeclWithHandle{ return DeclWithHandle{ .decl = decl, .handle = handle };
.decl = candidate.value_ptr,
.handle = handle,
};
} }
if (try analyser.resolveUse(scope_uses[scope_index], symbol, handle)) |result| return result; if (try analyser.resolveUse(scope_uses[scope_index], symbol, handle)) |result| return result;
} }
@ -2488,8 +2486,9 @@ pub fn lookupSymbolContainer(
const scope_uses = handle.document_scope.scopes.items(.uses); const scope_uses = handle.document_scope.scopes.items(.uses);
if (findContainerScopeIndex(container_handle)) |container_scope_index| { if (findContainerScopeIndex(container_handle)) |container_scope_index| {
if (scope_decls[container_scope_index].getEntry(symbol)) |candidate| { if (scope_decls[container_scope_index].get(symbol)) |decl_index| {
switch (candidate.value_ptr.*) { const decl = &handle.document_scope.decls.items[@enumToInt(decl_index)];
switch (decl.*) {
.ast_node => |node| { .ast_node => |node| {
if (node_tags[node].isContainerField()) { if (node_tags[node].isContainerField()) {
if (!instance_access and !is_enum) return null; if (!instance_access and !is_enum) return null;
@ -2499,7 +2498,7 @@ pub fn lookupSymbolContainer(
.label_decl => unreachable, .label_decl => unreachable,
else => {}, else => {},
} }
return DeclWithHandle{ .decl = candidate.value_ptr, .handle = handle }; return DeclWithHandle{ .decl = decl, .handle = handle };
} }
if (try analyser.resolveUse(scope_uses[container_scope_index], symbol, handle)) |result| return result; if (try analyser.resolveUse(scope_uses[container_scope_index], symbol, handle)) |result| return result;
@ -2532,9 +2531,10 @@ comptime {
} }
pub const DocumentScope = struct { pub const DocumentScope = struct {
scopes: std.MultiArrayList(Scope), scopes: std.MultiArrayList(Scope) = .{},
error_completions: CompletionSet, decls: std.ArrayListUnmanaged(Declaration) = .{},
enum_completions: CompletionSet, error_completions: CompletionSet = .{},
enum_completions: CompletionSet = .{},
pub fn deinit(self: *DocumentScope, allocator: std.mem.Allocator) void { pub fn deinit(self: *DocumentScope, allocator: std.mem.Allocator) void {
for ( for (
@ -2549,6 +2549,7 @@ pub const DocumentScope = struct {
allocator.free(uses); allocator.free(uses);
} }
self.scopes.deinit(allocator); self.scopes.deinit(allocator);
self.decls.deinit(allocator);
for (self.error_completions.entries.items(.key)) |item| { for (self.error_completions.entries.items(.key)) |item| {
if (item.detail) |detail| allocator.free(detail); if (item.detail) |detail| allocator.free(detail);
@ -2592,7 +2593,7 @@ pub const Scope = struct {
loc: offsets.Loc, loc: offsets.Loc,
parent: Index, parent: Index,
data: Data, data: Data,
decls: std.StringHashMapUnmanaged(Declaration) = .{}, decls: std.StringHashMapUnmanaged(Declaration.Index) = .{},
child_scopes: std.ArrayListUnmanaged(Scope.Index) = .{}, child_scopes: std.ArrayListUnmanaged(Scope.Index) = .{},
tests: []const Ast.Node.Index = &.{}, tests: []const Ast.Node.Index = &.{},
uses: []const Ast.Node.Index = &.{}, uses: []const Ast.Node.Index = &.{},
@ -2602,11 +2603,7 @@ pub fn makeDocumentScope(allocator: std.mem.Allocator, tree: Ast) !DocumentScope
const tracy_zone = tracy.trace(@src()); const tracy_zone = tracy.trace(@src());
defer tracy_zone.end(); defer tracy_zone.end();
var document_scope = DocumentScope{ var document_scope = DocumentScope{};
.scopes = .{},
.error_completions = .{},
.enum_completions = .{},
};
errdefer document_scope.deinit(allocator); errdefer document_scope.deinit(allocator);
var current_scope: Scope.Index = .none; var current_scope: Scope.Index = .none;
@ -2614,6 +2611,7 @@ pub fn makeDocumentScope(allocator: std.mem.Allocator, tree: Ast) !DocumentScope
try makeInnerScope(.{ try makeInnerScope(.{
.allocator = allocator, .allocator = allocator,
.scopes = &document_scope.scopes, .scopes = &document_scope.scopes,
.decls = &document_scope.decls,
.current_scope = &current_scope, .current_scope = &current_scope,
.errors = &document_scope.error_completions, .errors = &document_scope.error_completions,
.enums = &document_scope.enum_completions, .enums = &document_scope.enum_completions,
@ -2625,11 +2623,12 @@ pub fn makeDocumentScope(allocator: std.mem.Allocator, tree: Ast) !DocumentScope
const ScopeContext = struct { const ScopeContext = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
scopes: *std.MultiArrayList(Scope), scopes: *std.MultiArrayList(Scope),
decls: *std.ArrayListUnmanaged(Declaration),
current_scope: *Scope.Index, current_scope: *Scope.Index,
enums: *CompletionSet, enums: *CompletionSet,
errors: *CompletionSet, errors: *CompletionSet,
fn pushScope(context: ScopeContext, loc: offsets.Loc, data: Scope.Data) error{OutOfMemory}!usize { fn pushScope(context: ScopeContext, loc: offsets.Loc, data: Scope.Data) error{OutOfMemory}!Scope.Index {
try context.scopes.append(context.allocator, .{ try context.scopes.append(context.allocator, .{
.parent = context.current_scope.*, .parent = context.current_scope.*,
.loc = loc, .loc = loc,
@ -2640,13 +2639,24 @@ const ScopeContext = struct {
try context.scopes.items(.child_scopes)[@enumToInt(context.current_scope.*)].append(context.allocator, new_scope); try context.scopes.items(.child_scopes)[@enumToInt(context.current_scope.*)].append(context.allocator, new_scope);
} }
context.current_scope.* = new_scope; context.current_scope.* = new_scope;
return @enumToInt(new_scope); return new_scope;
} }
fn popScope(context: ScopeContext) void { fn popScope(context: ScopeContext) void {
const parent_scope = context.scopes.items(.parent)[@enumToInt(context.current_scope.*)]; const parent_scope = context.scopes.items(.parent)[@enumToInt(context.current_scope.*)];
context.current_scope.* = parent_scope; context.current_scope.* = parent_scope;
} }
fn putDecl(context: ScopeContext, scope: Scope.Index, name: []const u8, decl: Declaration) error{OutOfMemory}!void {
std.debug.assert(scope != .none);
try context.decls.append(context.allocator, decl);
errdefer _ = context.decls.pop();
const decl_index = @intToEnum(Declaration.Index, context.decls.items.len - 1);
try context.scopes.items(.decls)[@enumToInt(scope)].put(context.allocator, name, decl_index);
}
}; };
fn makeInnerScope(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void { fn makeInnerScope(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
@ -2688,7 +2698,7 @@ fn makeInnerScope(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) er
const name = getDeclName(tree, decl) orelse continue; const name = getDeclName(tree, decl) orelse continue;
try scopes.items(.decls)[scope_index].put(allocator, name, .{ .ast_node = decl }); try context.putDecl(scope_index, name, .{ .ast_node = decl });
if (container_decl.ast.enum_token != null) { if (container_decl.ast.enum_token != null) {
if (std.mem.eql(u8, name, "_")) return; if (std.mem.eql(u8, name, "_")) return;
@ -2708,13 +2718,13 @@ fn makeInnerScope(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) er
} }
} }
scopes.items(.tests)[scope_index] = try tests.toOwnedSlice(allocator); scopes.items(.tests)[@enumToInt(scope_index)] = try tests.toOwnedSlice(allocator);
scopes.items(.uses)[scope_index] = try uses.toOwnedSlice(allocator); scopes.items(.uses)[@enumToInt(scope_index)] = try uses.toOwnedSlice(allocator);
} }
/// If `node_idx` is a block it's scope index will be returned /// If `node_idx` is a block it's scope index will be returned
/// Otherwise, a new scope will be created that will enclose `node_idx` /// Otherwise, a new scope will be created that will enclose `node_idx`
fn makeBlockScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!?usize { fn makeBlockScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!?Scope.Index {
if (node_idx == 0) return null; if (node_idx == 0) return null;
const tags = tree.nodes.items(.tag); const tags = tree.nodes.items(.tag);
@ -2729,7 +2739,7 @@ fn makeBlockScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.I
.block_two_semicolon, .block_two_semicolon,
=> { => {
std.debug.assert(context.scopes.items(.data)[block_scope_index] == .block); std.debug.assert(context.scopes.items(.data)[block_scope_index] == .block);
return block_scope_index; return @intToEnum(Scope.Index, block_scope_index);
}, },
else => { else => {
const new_scope = try context.pushScope( const new_scope = try context.pushScope(
@ -2746,7 +2756,6 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
if (node_idx == 0) return; if (node_idx == 0) return;
const allocator = context.allocator; const allocator = context.allocator;
const scopes = context.scopes;
const tags = tree.nodes.items(.tag); const tags = tree.nodes.items(.tag);
const token_tags = tree.tokens.items(.tag); const token_tags = tree.tokens.items(.tag);
@ -2784,7 +2793,7 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
.doc_comment, .comma => {}, .doc_comment, .comma => {},
.identifier => { .identifier => {
const name = offsets.tokenToSlice(tree, tok_i); const name = offsets.tokenToSlice(tree, tok_i);
try scopes.items(.decls)[scope_index].put(allocator, name, .{ .error_token = tok_i }); try context.putDecl(scope_index, name, .{ .error_token = tok_i });
const gop = try context.errors.getOrPut(allocator, .{ const gop = try context.errors.getOrPut(allocator, .{
.label = name, .label = name,
.kind = .Constant, .kind = .Constant,
@ -2824,8 +2833,8 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
while (ast.nextFnParam(&it)) |param| { while (ast.nextFnParam(&it)) |param| {
// Add parameter decls // Add parameter decls
if (param.name_token) |name_token| { if (param.name_token) |name_token| {
try scopes.items(.decls)[scope_index].put( try context.putDecl(
allocator, scope_index,
tree.tokenSlice(name_token), tree.tokenSlice(name_token),
.{ .param_payload = .{ .param = param, .param_idx = @intCast(u16, param_index), .func = node_idx } }, .{ .param_payload = .{ .param = param, .param_idx = @intCast(u16, param_index), .func = node_idx } },
); );
@ -2873,8 +2882,8 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
// if labeled block // if labeled block
if (token_tags[first_token] == .identifier) { if (token_tags[first_token] == .identifier) {
try scopes.items(.decls)[scope_index].putNoClobber( try context.putDecl(
allocator, scope_index,
tree.tokenSlice(first_token), tree.tokenSlice(first_token),
.{ .label_decl = .{ .label = first_token, .block = node_idx } }, .{ .label_decl = .{ .label = first_token, .block = node_idx } },
); );
@ -2887,7 +2896,7 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
try makeScopeInternal(context, tree, idx); try makeScopeInternal(context, tree, idx);
if (tree.fullVarDecl(idx)) |var_decl| { if (tree.fullVarDecl(idx)) |var_decl| {
const name = tree.tokenSlice(var_decl.ast.mut_token + 1); const name = tree.tokenSlice(var_decl.ast.mut_token + 1);
try scopes.items(.decls)[scope_index].put(allocator, name, .{ .ast_node = idx }); try context.putDecl(scope_index, name, .{ .ast_node = idx });
} }
} }
}, },
@ -2903,8 +2912,8 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
std.debug.assert(token_tags[name_token] == .identifier); std.debug.assert(token_tags[name_token] == .identifier);
const name = tree.tokenSlice(name_token); const name = tree.tokenSlice(name_token);
try scopes.items(.decls)[then_scope].put( try context.putDecl(
allocator, then_scope,
name, name,
.{ .pointer_payload = .{ .name = name_token, .condition = if_node.ast.cond_expr } }, .{ .pointer_payload = .{ .name = name_token, .condition = if_node.ast.cond_expr } },
); );
@ -2914,7 +2923,7 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
const else_scope = (try makeBlockScopeInternal(context, tree, if_node.ast.else_expr)).?; const else_scope = (try makeBlockScopeInternal(context, tree, if_node.ast.else_expr)).?;
if (if_node.error_token) |err_token| { if (if_node.error_token) |err_token| {
const name = tree.tokenSlice(err_token); const name = tree.tokenSlice(err_token);
try scopes.items(.decls)[else_scope].put(allocator, name, .{ .ast_node = if_node.ast.else_expr }); try context.putDecl(else_scope, name, .{ .ast_node = if_node.ast.else_expr });
} }
} }
}, },
@ -2929,7 +2938,7 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
token_tags[catch_token] == .identifier) token_tags[catch_token] == .identifier)
{ {
const name = tree.tokenSlice(catch_token); const name = tree.tokenSlice(catch_token);
try scopes.items(.decls)[expr_scope].put(allocator, name, .{ .ast_node = data[node_idx].rhs }); try context.putDecl(expr_scope, name, .{ .ast_node = data[node_idx].rhs });
} }
}, },
.@"while", .@"while",
@ -2949,17 +2958,9 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
std.debug.assert(token_tags[label] == .identifier); std.debug.assert(token_tags[label] == .identifier);
const name = tree.tokenSlice(label); const name = tree.tokenSlice(label);
try scopes.items(.decls)[then_scope].put( try context.putDecl(then_scope, name, .{ .label_decl = .{ .label = label, .block = while_node.ast.then_expr } });
allocator,
name,
.{ .label_decl = .{ .label = label, .block = while_node.ast.then_expr } },
);
if (else_scope) |index| { if (else_scope) |index| {
try scopes.items(.decls)[index].put( try context.putDecl(index, name, .{ .label_decl = .{ .label = label, .block = while_node.ast.else_expr } });
allocator,
name,
.{ .label_decl = .{ .label = label, .block = while_node.ast.else_expr } },
);
} }
} }
@ -2975,15 +2976,15 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
}, },
}; };
if (cont_scope) |index| { if (cont_scope) |index| {
try scopes.items(.decls)[index].put(allocator, name, decl); try context.putDecl(index, name, decl);
} }
try scopes.items(.decls)[then_scope].put(allocator, name, decl); try context.putDecl(then_scope, name, decl);
} }
if (while_node.error_token) |err_token| { if (while_node.error_token) |err_token| {
std.debug.assert(token_tags[err_token] == .identifier); std.debug.assert(token_tags[err_token] == .identifier);
const name = tree.tokenSlice(err_token); const name = tree.tokenSlice(err_token);
try scopes.items(.decls)[else_scope.?].put(allocator, name, .{ .ast_node = while_node.ast.else_expr }); try context.putDecl(else_scope.?, name, .{ .ast_node = while_node.ast.else_expr });
} }
}, },
.@"for", .@"for",
@ -3006,8 +3007,8 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
const name_token = capture_token + @boolToInt(capture_is_ref); const name_token = capture_token + @boolToInt(capture_is_ref);
capture_token = name_token + 2; capture_token = name_token + 2;
try scopes.items(.decls)[then_scope].put( try context.putDecl(
allocator, then_scope,
offsets.tokenToSlice(tree, name_token), offsets.tokenToSlice(tree, name_token),
.{ .array_payload = .{ .identifier = name_token, .array_expr = input } }, .{ .array_payload = .{ .identifier = name_token, .array_expr = input } },
); );
@ -3017,14 +3018,14 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
std.debug.assert(token_tags[label] == .identifier); std.debug.assert(token_tags[label] == .identifier);
const name = tree.tokenSlice(label); const name = tree.tokenSlice(label);
try scopes.items(.decls)[then_scope].put( try context.putDecl(
allocator, then_scope,
name, name,
.{ .label_decl = .{ .label = label, .block = for_node.ast.then_expr } }, .{ .label_decl = .{ .label = label, .block = for_node.ast.then_expr } },
); );
if (else_scope) |index| { if (else_scope) |index| {
try scopes.items(.decls)[index].put( try context.putDecl(
allocator, index,
name, name,
.{ .label_decl = .{ .label = label, .block = for_node.ast.else_expr } }, .{ .label_decl = .{ .label = label, .block = for_node.ast.else_expr } },
); );
@ -3047,12 +3048,8 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
const name_token = payload + @boolToInt(token_tags[payload] == .asterisk); const name_token = payload + @boolToInt(token_tags[payload] == .asterisk);
const name = tree.tokenSlice(name_token); const name = tree.tokenSlice(name_token);
try scopes.items(.decls)[expr_index].put(allocator, name, .{ try context.putDecl(expr_index, name, .{
.switch_payload = .{ .switch_payload = .{ .node = name_token, .switch_expr = cond, .items = switch_case.ast.values },
.node = name_token,
.switch_expr = cond,
.items = switch_case.ast.values,
},
}); });
try makeScopeInternal(context, tree, switch_case.ast.target_expr); try makeScopeInternal(context, tree, switch_case.ast.target_expr);
@ -3067,7 +3064,7 @@ fn makeScopeInternal(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index)
const payload_token = data[node_idx].lhs; const payload_token = data[node_idx].lhs;
if (payload_token != 0) { if (payload_token != 0) {
const name = tree.tokenSlice(payload_token); const name = tree.tokenSlice(payload_token);
try scopes.items(.decls)[expr_scope].putNoClobber(allocator, name, .{ .ast_node = data[node_idx].rhs }); try context.putDecl(expr_scope, name, .{ .ast_node = data[node_idx].rhs });
} }
}, },
else => { else => {