2020-07-05 22:35:52 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const DocumentStore = @import("document_store.zig");
|
|
|
|
const analysis = @import("analysis.zig");
|
|
|
|
const types = @import("types.zig");
|
|
|
|
const offsets = @import("offsets.zig");
|
2020-08-14 11:41:34 +01:00
|
|
|
const log = std.log.scoped(.references);
|
2021-03-26 19:46:49 +00:00
|
|
|
usingnamespace @import("ast.zig");
|
2020-07-05 22:35:52 +01:00
|
|
|
|
|
|
|
const ast = std.zig.ast;
|
|
|
|
|
|
|
|
fn tokenReference(
|
|
|
|
handle: *DocumentStore.Handle,
|
|
|
|
tok: ast.TokenIndex,
|
|
|
|
encoding: offsets.Encoding,
|
2020-07-12 20:12:09 +01:00
|
|
|
context: anytype,
|
|
|
|
comptime handler: anytype,
|
2020-07-05 22:35:52 +01:00
|
|
|
) !void {
|
2021-03-07 13:51:47 +00:00
|
|
|
const loc = offsets.tokenRelativeLocation(handle.tree, 0, handle.tree.tokens.items(.start)[tok], encoding) catch return;
|
2020-07-05 22:35:52 +01:00
|
|
|
try handler(context, types.Location{
|
|
|
|
.uri = handle.uri(),
|
|
|
|
.range = .{
|
|
|
|
.start = .{
|
2020-11-06 08:08:20 +00:00
|
|
|
.line = @intCast(i64, loc.line),
|
|
|
|
.character = @intCast(i64, loc.column),
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
|
|
|
.end = .{
|
2020-11-06 08:08:20 +00:00
|
|
|
.line = @intCast(i64, loc.line),
|
|
|
|
.character = @intCast(i64, loc.column + offsets.tokenLength(handle.tree, tok, encoding)),
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn labelReferences(
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
decl: analysis.DeclWithHandle,
|
|
|
|
encoding: offsets.Encoding,
|
|
|
|
include_decl: bool,
|
2020-07-12 20:12:09 +01:00
|
|
|
context: anytype,
|
|
|
|
comptime handler: anytype,
|
2020-07-05 22:35:52 +01:00
|
|
|
) !void {
|
|
|
|
std.debug.assert(decl.decl.* == .label_decl);
|
|
|
|
const handle = decl.handle;
|
2021-03-01 13:32:19 +00:00
|
|
|
const tree = handle.tree;
|
|
|
|
const token_tags = tree.tokens.items(.tag);
|
2020-07-05 22:35:52 +01:00
|
|
|
|
|
|
|
// Find while / for / block from label -> iterate over children nodes, find break and continues, change their labels if they match.
|
|
|
|
// This case can be implemented just by scanning tokens.
|
2021-03-01 13:32:19 +00:00
|
|
|
const first_tok = tree.firstToken(decl.decl.label_decl);
|
|
|
|
const last_tok = tree.firstToken(decl.decl.label_decl);
|
2020-07-05 22:35:52 +01:00
|
|
|
|
|
|
|
if (include_decl) {
|
|
|
|
// The first token is always going to be the label
|
|
|
|
try tokenReference(handle, first_tok, encoding, context, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
var curr_tok = first_tok + 1;
|
|
|
|
while (curr_tok < last_tok - 2) : (curr_tok += 1) {
|
2021-03-01 13:32:19 +00:00
|
|
|
const curr_id = token_tags[curr_tok];
|
|
|
|
if ((curr_id == .keyword_break or curr_id == .keyword_continue) and token_tags[curr_tok + 1] == .colon and
|
|
|
|
token_tags[curr_tok + 2] == .identifier)
|
2020-07-05 22:35:52 +01:00
|
|
|
{
|
2021-03-01 13:32:19 +00:00
|
|
|
if (std.mem.eql(u8, tree.tokenSlice(curr_tok + 2), tree.tokenSlice(first_tok))) {
|
2020-07-05 22:35:52 +01:00
|
|
|
try tokenReference(handle, first_tok, encoding, context, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn symbolReferencesInternal(
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
store: *DocumentStore,
|
|
|
|
node_handle: analysis.NodeWithHandle,
|
|
|
|
decl: analysis.DeclWithHandle,
|
|
|
|
encoding: offsets.Encoding,
|
2020-07-12 20:12:09 +01:00
|
|
|
context: anytype,
|
|
|
|
comptime handler: anytype,
|
2020-07-05 22:35:52 +01:00
|
|
|
) error{OutOfMemory}!void {
|
|
|
|
const node = node_handle.node;
|
|
|
|
const handle = node_handle.handle;
|
2021-03-01 18:34:28 +00:00
|
|
|
const tree = handle.tree;
|
2021-03-03 16:45:42 +00:00
|
|
|
if (node > tree.nodes.len) return;
|
2021-03-01 18:34:28 +00:00
|
|
|
const node_tags = tree.nodes.items(.tag);
|
|
|
|
const datas = tree.nodes.items(.data);
|
|
|
|
const main_tokens = tree.nodes.items(.main_token);
|
|
|
|
const starts = tree.tokens.items(.start);
|
2020-07-05 22:35:52 +01:00
|
|
|
|
2021-03-01 18:34:28 +00:00
|
|
|
switch (node_tags[node]) {
|
|
|
|
.block, .block_semicolon, .block_two, .block_two_semicolon => {
|
|
|
|
const statements: []const ast.Node.Index = switch (node_tags[node]) {
|
|
|
|
.block, .block_semicolon => tree.extra_data[datas[node].lhs..datas[node].rhs],
|
|
|
|
.block_two, .block_two_semicolon => blk: {
|
|
|
|
const statements = &[_]ast.Node.Index{ datas[node].lhs, datas[node].rhs };
|
|
|
|
const len: usize = if (datas[node].lhs == 0)
|
|
|
|
@as(usize, 0)
|
|
|
|
else if (datas[node].rhs == 0)
|
|
|
|
@as(usize, 1)
|
|
|
|
else
|
|
|
|
@as(usize, 2);
|
|
|
|
break :blk statements[0..len];
|
|
|
|
},
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
for (statements) |stmt|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = stmt, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.container_decl,
|
|
|
|
.container_decl_trailing,
|
|
|
|
.container_decl_arg,
|
|
|
|
.container_decl_arg_trailing,
|
|
|
|
.container_decl_two,
|
|
|
|
.container_decl_two_trailing,
|
|
|
|
.tagged_union,
|
|
|
|
.tagged_union_trailing,
|
|
|
|
.tagged_union_two,
|
|
|
|
.tagged_union_two_trailing,
|
|
|
|
.tagged_union_enum_tag,
|
|
|
|
.tagged_union_enum_tag_trailing,
|
|
|
|
.root,
|
|
|
|
.error_set_decl,
|
|
|
|
=> {
|
|
|
|
var buf: [2]ast.Node.Index = undefined;
|
2021-03-30 18:59:58 +01:00
|
|
|
for (declMembers(tree, node, &buf)) |member|
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = member, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.global_var_decl,
|
|
|
|
.local_var_decl,
|
|
|
|
.simple_var_decl,
|
|
|
|
.aligned_var_decl,
|
|
|
|
=> {
|
2021-03-30 18:59:58 +01:00
|
|
|
const var_decl = varDecl(tree, node).?;
|
2021-03-01 18:34:28 +00:00
|
|
|
if (var_decl.ast.type_node != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = var_decl.ast.type_node, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
}
|
|
|
|
if (var_decl.ast.init_node != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = var_decl.ast.init_node, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
}
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.@"usingnamespace" => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.container_field,
|
|
|
|
.container_field_align,
|
|
|
|
.container_field_init,
|
|
|
|
=> {
|
2021-03-30 18:59:58 +01:00
|
|
|
const field = containerField(tree, node).?;
|
2021-03-01 18:34:28 +00:00
|
|
|
if (field.ast.type_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = field.ast.type_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
}
|
|
|
|
if (field.ast.value_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = field.ast.value_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.identifier => {
|
2021-03-01 21:18:38 +00:00
|
|
|
if (try analysis.lookupSymbolGlobal(store, arena, handle, tree.getNodeSource(node), starts[main_tokens[node]])) |child| {
|
2020-07-05 22:35:52 +01:00
|
|
|
if (std.meta.eql(decl, child)) {
|
2021-03-01 18:34:28 +00:00
|
|
|
try tokenReference(handle, main_tokens[node], encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.fn_proto,
|
|
|
|
.fn_proto_multi,
|
|
|
|
.fn_proto_one,
|
|
|
|
.fn_proto_simple,
|
|
|
|
.fn_decl,
|
|
|
|
=> {
|
2021-03-01 18:34:28 +00:00
|
|
|
var buf: [1]ast.Node.Index = undefined;
|
2021-03-30 18:59:58 +01:00
|
|
|
const fn_proto = fnProto(tree, node, &buf).?;
|
2021-03-01 18:34:28 +00:00
|
|
|
var it = fn_proto.iterate(tree);
|
|
|
|
while (it.next()) |param| {
|
|
|
|
if (param.type_expr != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = param.type_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
|
|
|
|
if (fn_proto.ast.return_type != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = fn_proto.ast.return_type, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
if (fn_proto.ast.align_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = fn_proto.ast.align_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
if (fn_proto.ast.section_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = fn_proto.ast.section_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
if (fn_proto.ast.callconv_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = fn_proto.ast.callconv_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
if (node_tags[node] == .fn_decl) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.anyframe_type => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.@"defer" => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.@"comptime" => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.@"nosuspend" => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.@"switch",
|
|
|
|
.switch_comma,
|
|
|
|
=> {
|
2020-07-05 22:35:52 +01:00
|
|
|
// TODO When renaming a union(enum) field, also rename switch items that refer to it.
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
const extra = tree.extraData(datas[node].rhs, ast.Node.SubRange);
|
|
|
|
const cases = tree.extra_data[extra.start..extra.end];
|
|
|
|
for (cases) |case| {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = case, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.switch_case_one => {
|
|
|
|
const case_one = tree.switchCaseOne(node);
|
2021-03-03 15:34:24 +00:00
|
|
|
if (case_one.ast.target_expr != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = case_one.ast.target_expr, .handle = handle }, decl, encoding, context, handler);
|
2021-03-01 18:34:28 +00:00
|
|
|
for (case_one.ast.values) |val|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = val, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.switch_case => {
|
|
|
|
const case = tree.switchCase(node);
|
2021-03-03 15:34:24 +00:00
|
|
|
if (case.ast.target_expr != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = case.ast.target_expr, .handle = handle }, decl, encoding, context, handler);
|
2021-03-01 21:18:38 +00:00
|
|
|
for (case.ast.values) |val|
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = val, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.@"while",
|
|
|
|
.while_simple,
|
|
|
|
.while_cont,
|
|
|
|
.for_simple,
|
|
|
|
.@"for",
|
|
|
|
=> {
|
2021-03-30 18:59:58 +01:00
|
|
|
const loop = whileAst(tree, node).?;
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = loop.ast.cond_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
if (loop.ast.cont_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = loop.ast.cont_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = loop.ast.then_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
if (loop.ast.else_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = loop.ast.else_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.@"if",
|
|
|
|
.if_simple,
|
|
|
|
=> {
|
2021-03-30 18:59:58 +01:00
|
|
|
const if_node: ast.full.If = if (node_tags[node] == .@"if") ifFull(tree, node) else ifSimple(tree, node);
|
2021-03-01 18:34:28 +00:00
|
|
|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = if_node.ast.cond_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = if_node.ast.then_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
if (if_node.ast.else_expr != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = if_node.ast.else_expr, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.array_type,
|
|
|
|
.array_type_sentinel,
|
|
|
|
=> {
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.ptr_type,
|
|
|
|
.ptr_type_aligned,
|
|
|
|
.ptr_type_bit_range,
|
|
|
|
.ptr_type_sentinel,
|
|
|
|
=> {
|
2021-03-30 18:59:58 +01:00
|
|
|
const ptr_type = ptrType(tree, node).?;
|
2020-07-05 22:35:52 +01:00
|
|
|
|
2021-03-01 18:34:28 +00:00
|
|
|
if (ptr_type.ast.align_node != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = ptr_type.ast.align_node, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
if (node_tags[node] == .ptr_type_bit_range) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{
|
|
|
|
.node = ptr_type.ast.bit_range_start,
|
|
|
|
.handle = handle,
|
|
|
|
}, decl, encoding, context, handler);
|
|
|
|
try symbolReferencesInternal(arena, store, .{
|
|
|
|
.node = ptr_type.ast.bit_range_end,
|
|
|
|
.handle = handle,
|
|
|
|
}, decl, encoding, context, handler);
|
2020-07-16 20:02:30 +01:00
|
|
|
}
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
if (ptr_type.ast.sentinel != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = ptr_type.ast.sentinel, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
2021-03-01 18:34:28 +00:00
|
|
|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = ptr_type.ast.child_type, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.address_of, .@"await", .bit_not, .bool_not, .optional_type, .negation, .negation_wrap, .@"resume", .@"try" => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.array_init,
|
|
|
|
.array_init_comma,
|
|
|
|
.array_init_dot,
|
|
|
|
.array_init_dot_comma,
|
|
|
|
.array_init_one,
|
|
|
|
.array_init_one_comma,
|
|
|
|
.array_init_dot_two,
|
|
|
|
.array_init_dot_two_comma,
|
|
|
|
=> |n| {
|
|
|
|
var buf: [2]ast.Node.Index = undefined;
|
|
|
|
const array_init = switch (n) {
|
|
|
|
.array_init, .array_init_comma => tree.arrayInit(node),
|
|
|
|
.array_init_dot, .array_init_dot_comma => tree.arrayInitDot(node),
|
2021-03-01 21:18:38 +00:00
|
|
|
.array_init_one, .array_init_one_comma => tree.arrayInitOne(buf[0..1], node),
|
2021-03-01 18:34:28 +00:00
|
|
|
.array_init_dot_two, .array_init_dot_two_comma => tree.arrayInitDotTwo(&buf, node),
|
2020-07-23 20:24:06 +01:00
|
|
|
else => unreachable,
|
2021-03-01 18:34:28 +00:00
|
|
|
};
|
|
|
|
if (array_init.ast.type_expr != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = array_init.ast.type_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
for (array_init.ast.elements) |e|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = e, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.struct_init,
|
|
|
|
.struct_init_comma,
|
|
|
|
.struct_init_dot,
|
|
|
|
.struct_init_dot_comma,
|
|
|
|
.struct_init_dot_two,
|
|
|
|
.struct_init_dot_two_comma,
|
|
|
|
.struct_init_one,
|
|
|
|
.struct_init_one_comma,
|
|
|
|
=> |n| {
|
|
|
|
var buf: [2]ast.Node.Index = undefined;
|
|
|
|
const struct_init: ast.full.StructInit = switch (n) {
|
|
|
|
.struct_init, .struct_init_comma => tree.structInit(node),
|
|
|
|
.struct_init_dot, .struct_init_dot_comma => tree.structInitDot(node),
|
2021-03-01 21:18:38 +00:00
|
|
|
.struct_init_one, .struct_init_one_comma => tree.structInitOne(buf[0..1], node),
|
2021-03-01 18:34:28 +00:00
|
|
|
.struct_init_dot_two, .struct_init_dot_two_comma => tree.structInitDotTwo(&buf, node),
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
if (struct_init.ast.type_expr != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = struct_init.ast.type_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
for (struct_init.ast.fields) |field|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = field, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.call,
|
|
|
|
.call_comma,
|
|
|
|
.call_one,
|
|
|
|
.call_one_comma,
|
|
|
|
.async_call,
|
|
|
|
.async_call_comma,
|
|
|
|
.async_call_one,
|
|
|
|
.async_call_one_comma,
|
|
|
|
=> |c| {
|
|
|
|
var buf: [1]ast.Node.Index = undefined;
|
|
|
|
const call: ast.full.Call = switch (c) {
|
|
|
|
.call, .call_comma, .async_call, .async_call_comma => tree.callFull(node),
|
|
|
|
.call_one, .call_one_comma, .async_call_one, .async_call_one_comma => tree.callOne(&buf, node),
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
if (call.ast.fn_expr != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = call.ast.fn_expr, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
|
|
|
|
for (call.ast.params) |param| {
|
2020-07-05 22:35:52 +01:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = param, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
}
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.slice,
|
|
|
|
.slice_sentinel,
|
|
|
|
.slice_open,
|
|
|
|
=> |s| {
|
2021-03-01 18:34:28 +00:00
|
|
|
const slice: ast.full.Slice = switch (s) {
|
|
|
|
.slice => tree.slice(node),
|
|
|
|
.slice_open => tree.sliceOpen(node),
|
|
|
|
.slice_sentinel => tree.sliceSentinel(node),
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = slice.ast.sliced, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = slice.ast.start, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
if (slice.ast.end != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = slice.ast.end, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
if (slice.ast.sentinel != 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = slice.ast.sentinel, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.array_access => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-23 17:06:39 +01:00
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.deref,
|
|
|
|
.unwrap_optional,
|
|
|
|
=> {
|
2021-03-01 18:34:28 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-23 17:06:39 +01:00
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.grouped_expression => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.@"return",
|
|
|
|
.@"break",
|
|
|
|
.@"continue",
|
|
|
|
=> {
|
2021-03-01 18:34:28 +00:00
|
|
|
if (datas[node].lhs != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.@"suspend" => {
|
|
|
|
if (datas[node].lhs != 0) {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.builtin_call,
|
|
|
|
.builtin_call_comma,
|
|
|
|
.builtin_call_two,
|
|
|
|
.builtin_call_two_comma,
|
2021-03-02 14:32:38 +00:00
|
|
|
=> |builtin_tag| {
|
|
|
|
const data = datas[node];
|
|
|
|
const params = switch (builtin_tag) {
|
|
|
|
.builtin_call, .builtin_call_comma => tree.extra_data[data.lhs..data.rhs],
|
|
|
|
.builtin_call_two, .builtin_call_two_comma => if (data.lhs == 0)
|
|
|
|
&[_]ast.Node.Index{}
|
|
|
|
else if (data.rhs == 0)
|
|
|
|
&[_]ast.Node.Index{data.lhs}
|
|
|
|
else
|
|
|
|
&[_]ast.Node.Index{ data.lhs, data.rhs },
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (params) |param|
|
2020-07-05 22:35:52 +01:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = param, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.@"asm",
|
|
|
|
.asm_simple,
|
|
|
|
=> |a| {
|
2021-03-01 18:34:28 +00:00
|
|
|
const _asm: ast.full.Asm = if (a == .@"asm") tree.asmFull(node) else tree.asmSimple(node);
|
|
|
|
if (_asm.ast.items.len == 0)
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = _asm.ast.template, .handle = handle }, decl, encoding, context, handler);
|
2020-07-16 20:02:30 +01:00
|
|
|
|
2021-03-01 18:34:28 +00:00
|
|
|
for (_asm.inputs) |input|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = input, .handle = handle }, decl, encoding, context, handler);
|
2020-07-16 20:02:30 +01:00
|
|
|
|
2021-03-01 18:34:28 +00:00
|
|
|
for (_asm.outputs) |output|
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = output, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.test_decl => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
},
|
|
|
|
.field_access => {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
|
2021-03-03 16:45:42 +00:00
|
|
|
const rhs_str = tree.tokenSlice(datas[node].rhs);
|
2020-07-16 20:19:08 +01:00
|
|
|
var bound_type_params = analysis.BoundTypeParams.init(&arena.allocator);
|
|
|
|
const left_type = try analysis.resolveFieldAccessLhsType(
|
|
|
|
store,
|
|
|
|
arena,
|
|
|
|
(try analysis.resolveTypeOfNodeInternal(store, arena, .{
|
2021-03-01 18:34:28 +00:00
|
|
|
.node = datas[node].lhs,
|
2020-07-16 20:19:08 +01:00
|
|
|
.handle = handle,
|
|
|
|
}, &bound_type_params)) orelse return,
|
|
|
|
&bound_type_params,
|
|
|
|
);
|
2020-07-16 20:02:30 +01:00
|
|
|
|
2020-07-16 20:19:08 +01:00
|
|
|
const left_type_node = switch (left_type.type.data) {
|
|
|
|
.other => |n| n,
|
|
|
|
else => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (try analysis.lookupSymbolContainer(
|
|
|
|
store,
|
|
|
|
arena,
|
|
|
|
.{ .node = left_type_node, .handle = left_type.handle },
|
|
|
|
rhs_str,
|
|
|
|
!left_type.type.is_type_val,
|
|
|
|
)) |child| {
|
|
|
|
if (std.meta.eql(child, decl)) {
|
2021-03-03 15:34:24 +00:00
|
|
|
try tokenReference(handle, datas[node].rhs, encoding, context, handler);
|
2020-07-16 20:19:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-03-01 18:34:28 +00:00
|
|
|
.add,
|
|
|
|
.add_wrap,
|
|
|
|
.array_cat,
|
|
|
|
.array_mult,
|
|
|
|
.assign,
|
|
|
|
.assign_bit_and,
|
|
|
|
.assign_bit_or,
|
|
|
|
.assign_bit_shift_left,
|
|
|
|
.assign_bit_shift_right,
|
|
|
|
.assign_bit_xor,
|
|
|
|
.assign_div,
|
|
|
|
.assign_sub,
|
|
|
|
.assign_sub_wrap,
|
|
|
|
.assign_mod,
|
|
|
|
.assign_add,
|
|
|
|
.assign_add_wrap,
|
|
|
|
.assign_mul,
|
|
|
|
.assign_mul_wrap,
|
|
|
|
.bang_equal,
|
|
|
|
.bit_and,
|
|
|
|
.bit_or,
|
|
|
|
.bit_shift_left,
|
|
|
|
.bit_shift_right,
|
|
|
|
.bit_xor,
|
|
|
|
.bool_or,
|
|
|
|
.div,
|
|
|
|
.equal_equal,
|
|
|
|
.error_union,
|
|
|
|
.greater_or_equal,
|
|
|
|
.greater_than,
|
|
|
|
.less_or_equal,
|
|
|
|
.less_than,
|
|
|
|
.merge_error_sets,
|
|
|
|
.mod,
|
|
|
|
.mul,
|
|
|
|
.mul_wrap,
|
2021-03-01 21:18:38 +00:00
|
|
|
.switch_range,
|
2021-03-01 18:34:28 +00:00
|
|
|
.sub,
|
|
|
|
.sub_wrap,
|
|
|
|
.@"orelse",
|
|
|
|
=> {
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].lhs, .handle = handle }, decl, encoding, context, handler);
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = datas[node].rhs, .handle = handle }, decl, encoding, context, handler);
|
2020-07-16 20:02:30 +01:00
|
|
|
},
|
2020-07-16 20:19:08 +01:00
|
|
|
else => {},
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn symbolReferences(
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
store: *DocumentStore,
|
|
|
|
decl_handle: analysis.DeclWithHandle,
|
|
|
|
encoding: offsets.Encoding,
|
|
|
|
include_decl: bool,
|
2020-07-12 20:12:09 +01:00
|
|
|
context: anytype,
|
|
|
|
comptime handler: anytype,
|
2021-03-06 19:55:59 +00:00
|
|
|
skip_std_references: bool,
|
2020-07-05 22:35:52 +01:00
|
|
|
) !void {
|
|
|
|
std.debug.assert(decl_handle.decl.* != .label_decl);
|
|
|
|
const curr_handle = decl_handle.handle;
|
2021-03-28 16:02:04 +01:00
|
|
|
if (include_decl) {
|
|
|
|
try tokenReference(curr_handle, decl_handle.nameToken(), encoding, context, handler);
|
|
|
|
}
|
2020-07-05 22:35:52 +01:00
|
|
|
|
|
|
|
switch (decl_handle.decl.*) {
|
|
|
|
.ast_node => |decl_node| {
|
2021-03-28 16:02:04 +01:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = 0, .handle = curr_handle }, decl_handle, encoding, context, handler);
|
2021-03-30 13:41:59 +01:00
|
|
|
|
2021-03-28 16:02:04 +01:00
|
|
|
var imports = std.ArrayList(*DocumentStore.Handle).init(&arena.allocator);
|
|
|
|
|
2020-07-05 22:35:52 +01:00
|
|
|
var handle_it = store.handles.iterator();
|
|
|
|
while (handle_it.next()) |entry| {
|
2021-03-06 19:55:59 +00:00
|
|
|
if (skip_std_references and std.mem.indexOf(u8, entry.key, "std") != null) {
|
|
|
|
if (!include_decl or entry.value != curr_handle)
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-05 22:35:52 +01:00
|
|
|
|
2021-03-28 16:02:04 +01:00
|
|
|
// Check entry's transitive imports
|
|
|
|
try imports.append(entry.value);
|
|
|
|
var i: usize = 0;
|
|
|
|
blk: while (i < imports.items.len) : (i += 1) {
|
|
|
|
const import = imports.items[i];
|
2021-03-30 13:41:59 +01:00
|
|
|
for (import.imports_used.items) |uri| {
|
2021-03-28 16:02:04 +01:00
|
|
|
const h = store.getHandle(uri) orelse break;
|
|
|
|
|
|
|
|
if (h == curr_handle) {
|
|
|
|
// entry does import curr_handle
|
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = 0, .handle = entry.value }, decl_handle, encoding, context, handler);
|
|
|
|
break :blk;
|
|
|
|
}
|
|
|
|
|
|
|
|
select: {
|
|
|
|
for (imports.items) |item| {
|
|
|
|
if (item == h) {
|
|
|
|
// already checked this import
|
|
|
|
break :select;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try imports.append(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try imports.resize(0);
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.param_decl => |param| {
|
|
|
|
// Rename the param tok.
|
2021-03-01 13:32:19 +00:00
|
|
|
const fn_node: ast.full.FnProto = loop: for (curr_handle.document_scope.scopes) |scope| {
|
2020-07-05 22:35:52 +01:00
|
|
|
switch (scope.data) {
|
|
|
|
.function => |proto| {
|
2021-03-01 13:32:19 +00:00
|
|
|
var buf: [1]ast.Node.Index = undefined;
|
2021-03-30 18:59:58 +01:00
|
|
|
const fn_proto = fnProto(curr_handle.tree, proto, &buf).?;
|
2021-03-01 13:32:19 +00:00
|
|
|
var it = fn_proto.iterate(curr_handle.tree);
|
|
|
|
while (it.next()) |candidate| {
|
|
|
|
if (std.meta.eql(candidate, param)) {
|
|
|
|
if (curr_handle.tree.nodes.items(.tag)[proto] == .fn_decl) {
|
|
|
|
try symbolReferencesInternal(
|
|
|
|
arena,
|
|
|
|
store,
|
|
|
|
.{ .node = curr_handle.tree.nodes.items(.data)[proto].rhs, .handle = curr_handle },
|
|
|
|
decl_handle,
|
|
|
|
encoding,
|
|
|
|
context,
|
|
|
|
handler,
|
|
|
|
);
|
|
|
|
}
|
2020-07-05 22:35:52 +01:00
|
|
|
break :loop fn_proto;
|
2021-03-01 13:32:19 +00:00
|
|
|
}
|
2020-07-05 22:35:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-14 11:41:34 +01:00
|
|
|
log.warn("Could not find param decl's function", .{});
|
2020-07-05 22:35:52 +01:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
},
|
2021-03-09 18:53:59 +00:00
|
|
|
.pointer_payload, .switch_payload, .array_payload, .array_index => {
|
2021-03-01 13:32:19 +00:00
|
|
|
try symbolReferencesInternal(arena, store, .{ .node = 0, .handle = curr_handle }, decl_handle, encoding, context, handler);
|
2020-07-05 22:35:52 +01:00
|
|
|
},
|
|
|
|
.label_decl => unreachable,
|
|
|
|
}
|
|
|
|
}
|