Skip to content

Commit

Permalink
refactor Analyser.Declaration field names (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix authored Mar 1, 2024
1 parent 7e53a56 commit 7c2728f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 69 deletions.
30 changes: 15 additions & 15 deletions src/DocumentScope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ noinline fn walkFuncNode(
if (param.name_token) |name_token| {
try scope.pushDeclaration(
offsets.identifierTokenToNameSlice(tree, name_token),
.{ .param_payload = .{ .param_index = param_index, .func = node_idx } },
.{ .function_parameter = .{ .param_index = param_index, .func = node_idx } },
.other,
);
}
Expand Down Expand Up @@ -833,7 +833,7 @@ fn walkBlockNodeKeepOpen(
if (token_tags[first_token] == .identifier) {
try scope.pushDeclaration(
offsets.identifierTokenToNameSlice(tree, first_token),
.{ .label_decl = .{ .label = first_token, .block = node_idx } },
.{ .label = .{ .identifier = first_token, .block = node_idx } },
.other,
);
}
Expand Down Expand Up @@ -888,9 +888,9 @@ noinline fn walkIfNode(
const name = offsets.identifierTokenToNameSlice(tree, name_token);

const decl: Declaration = if (if_node.error_token != null)
.{ .error_union_payload = .{ .name = name_token, .condition = if_node.ast.cond_expr } }
.{ .error_union_payload = .{ .identifier = name_token, .condition = if_node.ast.cond_expr } }
else
.{ .pointer_payload = .{ .name = name_token, .condition = if_node.ast.cond_expr } };
.{ .optional_payload = .{ .identifier = name_token, .condition = if_node.ast.cond_expr } };

const then_scope = try walkNodeEnsureScope(context, tree, if_node.ast.then_expr, name_token);
try then_scope.pushDeclaration(name, decl, .other);
Expand All @@ -906,7 +906,7 @@ noinline fn walkIfNode(
const else_scope = try walkNodeEnsureScope(context, tree, if_node.ast.else_expr, error_token);
try else_scope.pushDeclaration(
name,
.{ .error_union_error = .{ .name = error_token, .condition = if_node.ast.cond_expr } },
.{ .error_union_error = .{ .identifier = error_token, .condition = if_node.ast.cond_expr } },
.other,
);
try else_scope.finalize();
Expand Down Expand Up @@ -937,7 +937,7 @@ noinline fn walkCatchNode(
const expr_scope = try walkNodeEnsureScope(context, tree, data[node_idx].rhs, catch_token);
try expr_scope.pushDeclaration(
name,
.{ .error_union_error = .{ .name = catch_token, .condition = data[node_idx].lhs } },
.{ .error_union_error = .{ .identifier = catch_token, .condition = data[node_idx].lhs } },
.other,
);
try expr_scope.finalize();
Expand Down Expand Up @@ -969,9 +969,9 @@ noinline fn walkWhileNode(
const name = offsets.identifierTokenToNameSlice(tree, name_token);

const decl: Declaration = if (while_node.error_token != null)
.{ .error_union_payload = .{ .name = name_token, .condition = while_node.ast.cond_expr } }
.{ .error_union_payload = .{ .identifier = name_token, .condition = while_node.ast.cond_expr } }
else
.{ .pointer_payload = .{ .name = name_token, .condition = while_node.ast.cond_expr } };
.{ .optional_payload = .{ .identifier = name_token, .condition = while_node.ast.cond_expr } };
break :blk .{ decl, name };
} else .{ null, null };

Expand All @@ -992,7 +992,7 @@ noinline fn walkWhileNode(
if (label_token) |label| {
try then_scope.pushDeclaration(
label_name.?,
.{ .label_decl = .{ .label = label, .block = while_node.ast.then_expr } },
.{ .label = .{ .identifier = label, .block = while_node.ast.then_expr } },
.other,
);
}
Expand All @@ -1013,7 +1013,7 @@ noinline fn walkWhileNode(
if (label_token) |label| {
try else_scope.pushDeclaration(
label_name.?,
.{ .label_decl = .{ .label = label, .block = while_node.ast.then_expr } },
.{ .label = .{ .identifier = label, .block = while_node.ast.then_expr } },
.other,
);
}
Expand All @@ -1023,7 +1023,7 @@ noinline fn walkWhileNode(

try else_scope.pushDeclaration(
name,
.{ .error_union_error = .{ .name = error_token, .condition = while_node.ast.cond_expr } },
.{ .error_union_error = .{ .identifier = error_token, .condition = while_node.ast.cond_expr } },
.other,
);
}
Expand Down Expand Up @@ -1061,7 +1061,7 @@ noinline fn walkForNode(
if (tree.tokens.items(.tag)[name_token] != .identifier) break;
try then_scope.pushDeclaration(
offsets.identifierTokenToNameSlice(tree, name_token),
.{ .array_payload = .{ .identifier = name_token, .array_expr = input } },
.{ .for_loop_payload = .{ .identifier = name_token, .condition = input } },
.other,
);
}
Expand All @@ -1074,7 +1074,7 @@ noinline fn walkForNode(
if (for_node.label_token) |label_token| {
try then_scope.pushDeclaration(
label_name.?,
.{ .label_decl = .{ .label = label_token, .block = for_node.ast.then_expr } },
.{ .label = .{ .identifier = label_token, .block = for_node.ast.then_expr } },
.other,
);
}
Expand All @@ -1086,7 +1086,7 @@ noinline fn walkForNode(
const else_scope = try walkNodeEnsureScope(context, tree, for_node.ast.else_expr, tree.firstToken(for_node.ast.else_expr));
try else_scope.pushDeclaration(
label_name.?,
.{ .label_decl = .{ .label = label_token, .block = for_node.ast.else_expr } },
.{ .label = .{ .identifier = label_token, .block = for_node.ast.else_expr } },
.other,
);
try else_scope.finalize();
Expand Down Expand Up @@ -1141,7 +1141,7 @@ noinline fn walkErrdeferNode(
const expr_scope = try walkNodeEnsureScope(context, tree, data[node_idx].rhs, payload_token);
try expr_scope.pushDeclaration(
name,
.{ .error_union_error = .{ .name = payload_token, .condition = 0 } },
.{ .error_union_error = .{ .identifier = payload_token, .condition = 0 } },
.other,
);
try expr_scope.finalize();
Expand Down
88 changes: 55 additions & 33 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3305,34 +3305,56 @@ pub const TokenWithHandle = struct {
};

pub const Declaration = union(enum) {
/// Index of the ast node
/// Index of the ast node.
/// Can have one of the following tags:
/// - `.root`
/// - `.container_decl`
/// - `.tagged_union`
/// - `.error_set_decl`
/// - `.container_field`
/// - `.fn_proto`
/// - `.fn_decl`
/// - `.var_decl`
/// - `.block`
ast_node: Ast.Node.Index,
/// Function parameter
param_payload: Param,
pointer_payload: struct {
name: Ast.TokenIndex,
function_parameter: Param,
/// - `if (condition) |identifier| {}`
/// - `while (condition) |identifier| {}`
optional_payload: struct {
identifier: Ast.TokenIndex,
condition: Ast.Node.Index,
},
/// - `for (condition) |identifier| {}`
/// - `for (..., condition, ...) |..., identifier, ...| {}`
for_loop_payload: struct {
identifier: Ast.TokenIndex,
condition: Ast.Node.Index,
},
/// - `if (condition) |identifier| {} else |_| {}`
/// - `while (condition) |identifier| {} else |_| {}`
error_union_payload: struct {
name: Ast.TokenIndex,
identifier: Ast.TokenIndex,
condition: Ast.Node.Index,
},
/// - `if (condition) |_| {} else |identifier| {}`
/// - `while (condition) |_| {} else |identifier| {}`
/// - `condition catch |identifier| {}`
/// - `errdefer |identifier| {}` (condition is 0)
error_union_error: struct {
name: Ast.TokenIndex,
identifier: Ast.TokenIndex,
/// may be 0
condition: Ast.Node.Index,
},
array_payload: struct {
identifier: Ast.TokenIndex,
array_expr: Ast.Node.Index,
},
assign_destructure: AssignDestructure,
// a switch case capture
switch_payload: Switch,
label_decl: struct {
label: Ast.TokenIndex,
label: struct {
identifier: Ast.TokenIndex,
block: Ast.Node.Index,
},
/// always an identifier
/// used as child declarations of an error set declaration
error_token: Ast.TokenIndex,

pub const Param = struct {
Expand Down Expand Up @@ -3405,13 +3427,13 @@ pub const Declaration = union(enum) {
pub fn nameToken(decl: Declaration, tree: Ast) Ast.TokenIndex {
return switch (decl) {
.ast_node => |n| getDeclNameToken(tree, n).?,
.param_payload => |pp| pp.get(tree).?.name_token.?,
.pointer_payload => |pp| pp.name,
.error_union_payload => |ep| ep.name,
.error_union_error => |ep| ep.name,
.array_payload => |ap| ap.identifier,
.label_decl => |ld| ld.label,
.error_token => |et| et,
.function_parameter => |payload| payload.get(tree).?.name_token.?,
.optional_payload => |payload| payload.identifier,
.error_union_payload => |payload| payload.identifier,
.error_union_error => |payload| payload.identifier,
.for_loop_payload => |payload| payload.identifier,
.label => |payload| payload.identifier,
.error_token => |error_token| error_token,
.assign_destructure => |payload| getDeclNameToken(tree, payload.getVarDeclNode(tree)).?,
.switch_payload => |payload| {
const case = payload.getCase(tree);
Expand Down Expand Up @@ -3483,19 +3505,19 @@ pub const DeclWithHandle = struct {
if (var_decl.ast.type_node == 0) return null;
return .{ .node = var_decl.ast.type_node, .handle = self.handle };
},
.param_payload => |payload| {
.function_parameter => |payload| {
const param = payload.get(tree).?;
if (param.type_expr == 0) return null;
return .{ .node = param.type_expr, .handle = self.handle };
},
.pointer_payload,
.optional_payload,
.error_union_payload,
.error_union_error,
.array_payload,
.for_loop_payload,
.switch_payload,
=> return null, // the payloads can't have a type specifier

.label_decl,
.label,
.error_token,
=> return null,
}
Expand All @@ -3506,7 +3528,7 @@ pub const DeclWithHandle = struct {
return switch (self.decl) {
// TODO: delete redundant `Analyser.`
.ast_node => |node| try Analyser.getDocComments(allocator, tree, node),
.param_payload => |pay| {
.function_parameter => |pay| {
const param = pay.get(tree).?;
const doc_comments = param.first_doc_comment orelse return null;
return try Analyser.collectDocComments(allocator, tree, doc_comments, false);
Expand Down Expand Up @@ -3534,7 +3556,7 @@ pub const DeclWithHandle = struct {
.ast_node => |node| try analyser.resolveTypeOfNodeInternal(
.{ .node = node, .handle = self.handle },
),
.param_payload => |pay| {
.function_parameter => |pay| {
// the `get` function never fails on declarations from the DocumentScope but
// there may be manually created Declarations with invalid parameter indicies.
const param = pay.get(tree) orelse return null;
Expand Down Expand Up @@ -3632,7 +3654,7 @@ pub const DeclWithHandle = struct {

return try param_type.instanceTypeVal(analyser);
},
.pointer_payload => |pay| {
.optional_payload => |pay| {
const ty = (try analyser.resolveTypeOfNodeInternal(.{
.node = pay.condition,
.handle = self.handle,
Expand All @@ -3653,9 +3675,9 @@ pub const DeclWithHandle = struct {
})) orelse return null,
.error_set,
),
.array_payload => |pay| try analyser.resolveBracketAccessType(
.for_loop_payload => |pay| try analyser.resolveBracketAccessType(
(try analyser.resolveTypeOfNodeInternal(.{
.node = pay.array_expr,
.node = pay.condition,
.handle = self.handle,
})) orelse return null,
.Single,
Expand All @@ -3678,7 +3700,7 @@ pub const DeclWithHandle = struct {
else => null,
};
},
.label_decl => |decl| try analyser.resolveTypeOfNodeInternal(.{
.label => |decl| try analyser.resolveTypeOfNodeInternal(.{
.node = decl.block,
.handle = self.handle,
}),
Expand Down Expand Up @@ -3783,7 +3805,7 @@ pub fn iterateSymbolsContainer(
},
else => {},
},
.label_decl => continue,
.label => continue,
else => {},
}

Expand Down Expand Up @@ -3890,7 +3912,7 @@ pub fn iterateLabels(handle: *DocumentStore.Handle, source_index: usize, comptim
while (scope_iterator.next().unwrap()) |scope_index| {
for (document_scope.getScopeDeclarationsConst(scope_index)) |decl_index| {
const decl = document_scope.declarations.get(@intFromEnum(decl_index));
if (decl != .label_decl) continue;
if (decl != .label) continue;
try callback(context, DeclWithHandle{ .decl = decl, .handle = handle });
}
}
Expand All @@ -3910,7 +3932,7 @@ fn iterateSymbolsGlobalInternal(
for (scope_decls) |decl_index| {
const decl = document_scope.declarations.get(@intFromEnum(decl_index));
if (decl == .ast_node and handle.tree.nodes.items(.tag)[decl.ast_node].isContainerField()) continue;
if (decl == .label_decl) continue;
if (decl == .label) continue;
try callback(context, DeclWithHandle{ .decl = decl, .handle = handle });
}

Expand Down Expand Up @@ -4023,7 +4045,7 @@ pub fn lookupLabel(
}).unwrap() orelse continue;
const decl = document_scope.declarations.get(@intFromEnum(decl_index));

if (decl != .label_decl) continue;
if (decl != .label) continue;

return DeclWithHandle{ .decl = decl, .handle = handle };
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn handleUnusedFunctionParameter(builder: *Builder, actions: *std.ArrayListUnman
)) orelse return;

const payload = switch (decl.decl) {
.param_payload => |pay| pay,
.function_parameter => |pay| pay,
else => return,
};

Expand Down
12 changes: 6 additions & 6 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: Analyser.Decl
context.either_descriptor,
context.doc_strings,
),
.param_payload => |pay| {
.function_parameter => |pay| {
const param = pay.get(tree).?;
const name = tree.tokenSlice(param.name_token.?);
const doc = try completionDoc(
Expand All @@ -498,19 +498,19 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: Analyser.Decl
.detail = offsets.nodeToSlice(tree, param.type_expr),
});
},
.pointer_payload,
.optional_payload,
.error_union_payload,
.error_union_error,
.array_payload,
.for_loop_payload,
.assign_destructure,
.switch_payload,
.label_decl,
.label,
=> {
const name = tree.tokenSlice(decl_handle.nameToken());

try builder.completions.append(builder.arena, .{
.label = name,
.kind = if (decl == .label_decl) .Text else .Variable,
.kind = if (decl == .label) .Text else .Variable,
});
},
.error_token => |token| {
Expand Down Expand Up @@ -1318,7 +1318,7 @@ fn collectVarAccessContainerNodes(
try node_type.getAllTypesWithHandlesArrayList(arena, types_with_handles);
return;
}
const fn_param_decl = Analyser.Declaration{ .param_payload = .{
const fn_param_decl = Analyser.Declaration{ .function_parameter = .{
.func = fn_proto_node,
.param_index = @intCast(dot_context.fn_arg_index),
} };
Expand Down
8 changes: 4 additions & 4 deletions src/features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn hoverSymbolRecursive(
break :def Analyser.nodeToString(tree, node) orelse return null;
}
},
.param_payload => |pay| def: {
.function_parameter => |pay| def: {
const param = pay.get(tree).?;

if (param.type_expr != 0) // zero for `anytype` and extern C varargs `...`
Expand All @@ -97,13 +97,13 @@ fn hoverSymbolRecursive(

break :def ast.paramSlice(tree, param);
},
.pointer_payload,
.optional_payload,
.error_union_payload,
.error_union_error,
.array_payload,
.for_loop_payload,
.assign_destructure,
.switch_payload,
.label_decl,
.label,
.error_token,
=> tree.tokenSlice(decl_handle.nameToken()),
};
Expand Down
Loading

0 comments on commit 7c2728f

Please sign in to comment.