Merge pull request #155 from codehz/fix

Adopt to new ast API (fix #154)
This commit is contained in:
Alexandros Naskos 2020-09-02 07:57:19 +03:00 committed by GitHub
commit e82723c36f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 54 deletions

View File

@ -8,9 +8,9 @@ const log = std.log.scoped(.analysis);
/// Get a declaration's doc comment node /// Get a declaration's doc comment node
fn getDocCommentNode(tree: *ast.Tree, node: *ast.Node) ?*ast.Node.DocComment { fn getDocCommentNode(tree: *ast.Tree, node: *ast.Node) ?*ast.Node.DocComment {
if (node.castTag(.FnProto)) |func| { if (node.castTag(.FnProto)) |func| {
return func.getTrailer("doc_comments"); return func.getDocComments();
} else if (node.castTag(.VarDecl)) |var_decl| { } else if (node.castTag(.VarDecl)) |var_decl| {
return var_decl.getTrailer("doc_comments"); return var_decl.getDocComments();
} else if (node.castTag(.ContainerField)) |field| { } else if (node.castTag(.ContainerField)) |field| {
return field.doc_comments; return field.doc_comments;
} else if (node.castTag(.ErrorTag)) |tag| { } else if (node.castTag(.ErrorTag)) |tag| {
@ -72,7 +72,7 @@ pub fn getFunctionSignature(tree: *ast.Tree, func: *ast.Node.FnProto) []const u8
/// Gets a function snippet insert text /// Gets a function snippet insert text
pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func: *ast.Node.FnProto, skip_self_param: bool) ![]const u8 { pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func: *ast.Node.FnProto, skip_self_param: bool) ![]const u8 {
const name_tok = func.getTrailer("name_token") orelse unreachable; const name_tok = func.getNameToken() orelse unreachable;
var buffer = std.ArrayList(u8).init(allocator); var buffer = std.ArrayList(u8).init(allocator);
try buffer.ensureCapacity(128); try buffer.ensureCapacity(128);
@ -182,8 +182,7 @@ pub fn getDeclNameToken(tree: *ast.Tree, node: *ast.Node) ?ast.TokenIndex {
}, },
.FnProto => { .FnProto => {
const func = node.castTag(.FnProto).?; const func = node.castTag(.FnProto).?;
if (func.getTrailer("name_token") == null) return null; return func.getNameToken();
return func.getTrailer("name_token").?;
}, },
.ContainerField => { .ContainerField => {
const field = node.castTag(.ContainerField).?; const field = node.castTag(.ContainerField).?;
@ -274,10 +273,9 @@ pub fn resolveVarDeclAlias(store: *DocumentStore, arena: *std.heap.ArenaAllocato
const handle = decl_handle.handle; const handle = decl_handle.handle;
if (decl.castTag(.VarDecl)) |var_decl| { if (decl.castTag(.VarDecl)) |var_decl| {
if (!var_decl.trailer_flags.has("init_node")) return null; const base_expr = var_decl.getInitNode() orelse return null;
if (handle.tree.token_ids[var_decl.mut_token] != .Keyword_const) return null; if (handle.tree.token_ids[var_decl.mut_token] != .Keyword_const) return null;
const base_expr = var_decl.getTrailer("init_node").?;
if (base_expr.cast(ast.Node.SimpleInfixOp)) |infix_op| { if (base_expr.cast(ast.Node.SimpleInfixOp)) |infix_op| {
if (base_expr.tag != .Period) return null; if (base_expr.tag != .Period) return null;
const name = handle.tree.tokenSlice(infix_op.rhs.firstToken()); const name = handle.tree.tokenSlice(infix_op.rhs.firstToken());
@ -325,7 +323,7 @@ fn findReturnStatementInternal(
fn findReturnStatement(tree: *ast.Tree, fn_decl: *ast.Node.FnProto) ?*ast.Node.ControlFlowExpression { fn findReturnStatement(tree: *ast.Tree, fn_decl: *ast.Node.FnProto) ?*ast.Node.ControlFlowExpression {
var already_found = false; var already_found = false;
return findReturnStatementInternal(tree, fn_decl, fn_decl.getTrailer("body_node").?, &already_found); return findReturnStatementInternal(tree, fn_decl, fn_decl.getBodyNode().?, &already_found);
} }
/// Resolves the return type of a function /// Resolves the return type of a function
@ -336,7 +334,7 @@ fn resolveReturnType(
handle: *DocumentStore.Handle, handle: *DocumentStore.Handle,
bound_type_params: *BoundTypeParams, bound_type_params: *BoundTypeParams,
) !?TypeWithHandle { ) !?TypeWithHandle {
if (isTypeFunction(handle.tree, fn_decl) and fn_decl.trailer_flags.has("body_node")) { if (isTypeFunction(handle.tree, fn_decl) and fn_decl.getBodyNode() != null) {
// If this is a type function and it only contains a single return statement that returns // If this is a type function and it only contains a single return statement that returns
// a container declaration, we will return that declaration. // a container declaration, we will return that declaration.
const ret = findReturnStatement(handle.tree, fn_decl) orelse return null; const ret = findReturnStatement(handle.tree, fn_decl) orelse return null;
@ -549,17 +547,17 @@ pub fn resolveTypeOfNodeInternal(
switch (node.tag) { switch (node.tag) {
.VarDecl => { .VarDecl => {
const vari = node.castTag(.VarDecl).?; const vari = node.castTag(.VarDecl).?;
if (vari.getTrailer("type_node")) |type_node| block: { if (vari.getTypeNode()) |type_node| block: {
return ((try resolveTypeOfNodeInternal( return ((try resolveTypeOfNodeInternal(
store, store,
arena, arena,
.{ .node = vari.getTrailer("type_node") orelse break :block, .handle = handle }, .{ .node = type_node, .handle = handle },
bound_type_params, bound_type_params,
)) orelse break :block).instanceTypeVal(); )) orelse break :block).instanceTypeVal();
} }
if (vari.getTrailer("init_node") == null) return null; const init_node = vari.getInitNode() orelse return null;
return try resolveTypeOfNodeInternal(store, arena, .{ .node = vari.getTrailer("init_node").?, .handle = handle }, bound_type_params); return try resolveTypeOfNodeInternal(store, arena, .{ .node = init_node, .handle = handle }, bound_type_params);
}, },
.Identifier => { .Identifier => {
if (isTypeIdent(handle.tree, node.firstToken())) { if (isTypeIdent(handle.tree, node.firstToken())) {
@ -822,7 +820,7 @@ pub fn resolveTypeOfNodeInternal(
}, },
.FnProto => { .FnProto => {
// This is a function type // This is a function type
if (!node.castTag(.FnProto).?.trailer_flags.has("name_token")) { if (node.castTag(.FnProto).?.getNameToken() == null) {
return TypeWithHandle.typeVal(node_handle); return TypeWithHandle.typeVal(node_handle);
} }
return TypeWithHandle{ return TypeWithHandle{
@ -973,15 +971,15 @@ pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !
for (tree.root_node.decls()) |decl| { for (tree.root_node.decls()) |decl| {
if (decl.tag != .VarDecl) continue; if (decl.tag != .VarDecl) continue;
const var_decl = decl.castTag(.VarDecl).?; const var_decl = decl.castTag(.VarDecl).?;
if (!var_decl.trailer_flags.has("init_node")) continue; const init_node = var_decl.getInitNode() orelse continue;
switch (var_decl.getTrailer("init_node").?.tag) { switch (init_node.tag) {
.BuiltinCall => { .BuiltinCall => {
const builtin_call = var_decl.getTrailer("init_node").?.castTag(.BuiltinCall).?; const builtin_call = init_node.castTag(.BuiltinCall).?;
try maybeCollectImport(tree, builtin_call, import_arr); try maybeCollectImport(tree, builtin_call, import_arr);
}, },
.Period => { .Period => {
const infix_op = var_decl.getTrailer("init_node").?.cast(ast.Node.SimpleInfixOp).?; const infix_op = init_node.cast(ast.Node.SimpleInfixOp).?;
if (infix_op.lhs.tag != .BuiltinCall) continue; if (infix_op.lhs.tag != .BuiltinCall) continue;
try maybeCollectImport(tree, infix_op.lhs.castTag(.BuiltinCall).?, import_arr); try maybeCollectImport(tree, infix_op.lhs.castTag(.BuiltinCall).?, import_arr);
@ -1130,11 +1128,11 @@ pub fn isNodePublic(tree: *ast.Tree, node: *ast.Node) bool {
switch (node.tag) { switch (node.tag) {
.VarDecl => { .VarDecl => {
const var_decl = node.castTag(.VarDecl).?; const var_decl = node.castTag(.VarDecl).?;
return var_decl.trailer_flags.has("visib_token"); return var_decl.getVisibToken() != null;
}, },
.FnProto => { .FnProto => {
const func = node.castTag(.FnProto).?; const func = node.castTag(.FnProto).?;
return func.trailer_flags.has("visib_token"); return func.getVisibToken() != null;
}, },
else => return true, else => return true,
} }
@ -1156,7 +1154,7 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
}, },
.FnProto => { .FnProto => {
const func = node.castTag(.FnProto).?; const func = node.castTag(.FnProto).?;
if (func.getTrailer("name_token")) |name_token| { if (func.getNameToken()) |name_token| {
return tree.tokenSlice(name_token); return tree.tokenSlice(name_token);
} }
}, },
@ -2177,7 +2175,7 @@ fn makeScopeInternal(
} }
} }
if (func.getTrailer("body_node")) |body| { if (func.getBodyNode()) |body| {
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, body); try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, body);
} }
@ -2504,10 +2502,10 @@ fn makeScopeInternal(
}, },
.VarDecl => { .VarDecl => {
const var_decl = node.castTag(.VarDecl).?; const var_decl = node.castTag(.VarDecl).?;
if (var_decl.getTrailer("type_node")) |type_node| { if (var_decl.getTypeNode()) |type_node| {
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, type_node); try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, type_node);
} }
if (var_decl.getTrailer("init_node")) |init_node| { if (var_decl.getInitNode()) |init_node| {
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, init_node); try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, init_node);
} }
}, },

View File

@ -214,12 +214,12 @@ fn publishDiagnostics(arena: *std.heap.ArenaAllocator, handle: DocumentStore.Han
switch (decl.tag) { switch (decl.tag) {
.FnProto => blk: { .FnProto => blk: {
const func = decl.cast(std.zig.ast.Node.FnProto).?; const func = decl.cast(std.zig.ast.Node.FnProto).?;
const is_extern = func.trailer_flags.has("extern_export_inline_token"); const is_extern = func.getExternExportInlineToken() != null;
if (is_extern) if (is_extern)
break :blk; break :blk;
if (config.warn_style) { if (config.warn_style) {
if (func.getTrailer("name_token")) |name_token| { if (func.getNameToken()) |name_token| {
const loc = tree.tokenLocation(0, name_token); const loc = tree.tokenLocation(0, name_token);
const is_type_function = analysis.isTypeFunction(tree, func); const is_type_function = analysis.isTypeFunction(tree, func);
@ -354,7 +354,7 @@ fn nodeToCompletion(
switch (node.tag) { switch (node.tag) {
.FnProto => { .FnProto => {
const func = node.cast(std.zig.ast.Node.FnProto).?; const func = node.cast(std.zig.ast.Node.FnProto).?;
if (func.getTrailer("name_token")) |name_token| { if (func.getNameToken()) |name_token| {
const use_snippets = config.enable_snippets and client_capabilities.supports_snippets; const use_snippets = config.enable_snippets and client_capabilities.supports_snippets;
const insert_text = if (use_snippets) blk: { const insert_text = if (use_snippets) blk: {

View File

@ -85,10 +85,10 @@ fn symbolReferencesInternal(
}, },
.VarDecl => { .VarDecl => {
const var_decl = node.cast(ast.Node.VarDecl).?; const var_decl = node.cast(ast.Node.VarDecl).?;
if (var_decl.getTrailer("type_node")) |type_node| { if (var_decl.getTypeNode()) |type_node| {
try symbolReferencesInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = type_node, .handle = handle }, decl, encoding, context, handler);
} }
if (var_decl.getTrailer("init_node")) |init_node| { if (var_decl.getInitNode()) |init_node| {
try symbolReferencesInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = init_node, .handle = handle }, decl, encoding, context, handler);
} }
}, },
@ -128,16 +128,16 @@ fn symbolReferencesInternal(
}, },
else => {}, else => {},
} }
if (fn_proto.getTrailer("align_expr")) |align_expr| { if (fn_proto.getAlignExpr()) |align_expr| {
try symbolReferencesInternal(arena, store, .{ .node = align_expr, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = align_expr, .handle = handle }, decl, encoding, context, handler);
} }
if (fn_proto.getTrailer("section_expr")) |section_expr| { if (fn_proto.getSectionExpr()) |section_expr| {
try symbolReferencesInternal(arena, store, .{ .node = section_expr, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = section_expr, .handle = handle }, decl, encoding, context, handler);
} }
if (fn_proto.getTrailer("callconv_expr")) |callconv_expr| { if (fn_proto.getCallconvExpr()) |callconv_expr| {
try symbolReferencesInternal(arena, store, .{ .node = callconv_expr, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = callconv_expr, .handle = handle }, decl, encoding, context, handler);
} }
if (fn_proto.getTrailer("body_node")) |body| { if (fn_proto.getBodyNode()) |body| {
try symbolReferencesInternal(arena, store, .{ .node = body, .handle = handle }, decl, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = body, .handle = handle }, decl, encoding, context, handler);
} }
}, },
@ -411,7 +411,7 @@ pub fn symbolReferences(
log.warn("Could not find param decl's function", .{}); log.warn("Could not find param decl's function", .{});
return; return;
}; };
if (fn_node.getTrailer("body_node")) |body| { if (fn_node.getBodyNode()) |body| {
try symbolReferencesInternal(arena, store, .{ .node = body, .handle = curr_handle }, decl_handle, encoding, context, handler); try symbolReferencesInternal(arena, store, .{ .node = body, .handle = curr_handle }, decl_handle, encoding, context, handler);
} }
}, },

View File

@ -256,22 +256,22 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
}, },
.VarDecl => { .VarDecl => {
const var_decl = node.cast(ast.Node.VarDecl).?; const var_decl = node.cast(ast.Node.VarDecl).?;
if (var_decl.getTrailer("doc_comments")) |doc| try writeDocComments(builder, handle.tree, doc); if (var_decl.getDocComments()) |doc| try writeDocComments(builder, handle.tree, doc);
try writeToken(builder, var_decl.getTrailer("visib_token"), .keyword); try writeToken(builder, var_decl.getVisibToken(), .keyword);
try writeToken(builder, var_decl.getTrailer("extern_export_token"), .keyword); try writeToken(builder, var_decl.getExternExportToken(), .keyword);
try writeToken(builder, var_decl.getTrailer("thread_local_token"), .keyword); try writeToken(builder, var_decl.getThreadLocalToken(), .keyword);
try writeToken(builder, var_decl.getTrailer("comptime_token"), .keyword); try writeToken(builder, var_decl.getComptimeToken(), .keyword);
try writeToken(builder, var_decl.mut_token, .keyword); try writeToken(builder, var_decl.mut_token, .keyword);
if (try analysis.resolveTypeOfNode(store, arena, .{ .node = node, .handle = handle })) |decl_type| { if (try analysis.resolveTypeOfNode(store, arena, .{ .node = node, .handle = handle })) |decl_type| {
try colorIdentifierBasedOnType(builder, decl_type, var_decl.name_token, .{ .definition = true }); try colorIdentifierBasedOnType(builder, decl_type, var_decl.name_token, .{ .definition = true });
} else { } else {
try writeTokenMod(builder, var_decl.name_token, .variable, .{ .definition = true }); try writeTokenMod(builder, var_decl.name_token, .variable, .{ .definition = true });
} }
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getTrailer("type_node") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getTypeNode() });
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getTrailer("align_node") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getAlignNode() });
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getTrailer("section_node") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getSectionNode() });
try writeToken(builder, var_decl.getTrailer("eq_token"), .operator); try writeToken(builder, var_decl.getEqToken(), .operator);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getTrailer("init_node") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.getInitNode() });
}, },
.Use => { .Use => {
const use = node.cast(ast.Node.Use).?; const use = node.cast(ast.Node.Use).?;
@ -335,10 +335,10 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
}, },
.FnProto => { .FnProto => {
const fn_proto = node.cast(ast.Node.FnProto).?; const fn_proto = node.cast(ast.Node.FnProto).?;
if (fn_proto.getTrailer("doc_comments")) |docs| try writeDocComments(builder, handle.tree, docs); if (fn_proto.getDocComments()) |docs| try writeDocComments(builder, handle.tree, docs);
try writeToken(builder, fn_proto.getTrailer("visib_token"), .keyword); try writeToken(builder, fn_proto.getVisibToken(), .keyword);
try writeToken(builder, fn_proto.getTrailer("extern_export_inline_token"), .keyword); try writeToken(builder, fn_proto.getExternExportInlineToken(), .keyword);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getTrailer("lib_name") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getLibName() });
try writeToken(builder, fn_proto.fn_token, .keyword); try writeToken(builder, fn_proto.fn_token, .keyword);
const func_name_tok_type: TokenType = if (analysis.isTypeFunction(handle.tree, fn_proto)) const func_name_tok_type: TokenType = if (analysis.isTypeFunction(handle.tree, fn_proto))
@ -351,7 +351,7 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
else else
TokenModifiers{}; TokenModifiers{};
try writeTokenMod(builder, fn_proto.getTrailer("name_token"), func_name_tok_type, tok_mod); try writeTokenMod(builder, fn_proto.getNameToken(), func_name_tok_type, tok_mod);
for (fn_proto.paramsConst()) |param_decl| { for (fn_proto.paramsConst()) |param_decl| {
if (param_decl.doc_comments) |docs| try writeDocComments(builder, handle.tree, docs); if (param_decl.doc_comments) |docs| try writeDocComments(builder, handle.tree, docs);
@ -363,9 +363,9 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
.type_expr => |type_expr| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, type_expr }), .type_expr => |type_expr| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, type_expr }),
} }
} }
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getTrailer("align_expr") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getAlignExpr() });
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getTrailer("section_expr") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getSectionExpr() });
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getTrailer("callconv_expr") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getCallconvExpr() });
switch (fn_proto.return_type) { switch (fn_proto.return_type) {
.Explicit => |type_expr| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, type_expr }), .Explicit => |type_expr| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, type_expr }),
@ -375,7 +375,7 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
}, },
.Invalid => {}, .Invalid => {},
} }
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getTrailer("body_node") }); try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.getBodyNode() });
}, },
.AnyFrameType => { .AnyFrameType => {
const any_frame_type = node.cast(ast.Node.AnyFrameType).?; const any_frame_type = node.cast(ast.Node.AnyFrameType).?;

View File

@ -58,7 +58,7 @@ fn startZls() !*std.ChildProcess {
process.stderr_behavior = std.ChildProcess.StdIo.Inherit; process.stderr_behavior = std.ChildProcess.StdIo.Inherit;
process.spawn() catch |err| { process.spawn() catch |err| {
std.log.debug(.main, "Failed to spawn zls process, error: {}\n", .{err}); std.log.debug("Failed to spawn zls process, error: {}\n", .{err});
return err; return err;
}; };