We now complete ArrayList!

This commit is contained in:
Alexandros Naskos 2020-05-24 15:24:18 +03:00
parent dd9ec8e450
commit 9ec32ac830
3 changed files with 89 additions and 34 deletions

View File

@ -234,7 +234,12 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
return null; return null;
} }
fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression { fn findReturnStatementInternal(
tree: *ast.Tree,
fn_decl: *ast.Node.FnProto,
base_node: *ast.Node,
already_found: *bool,
) ?*ast.Node.ControlFlowExpression {
var result: ?*ast.Node.ControlFlowExpression = null; var result: ?*ast.Node.ControlFlowExpression = null;
var idx: usize = 0; var idx: usize = 0;
while (base_node.iterate(idx)) |child_node| : (idx += 1) { while (base_node.iterate(idx)) |child_node| : (idx += 1) {
@ -242,6 +247,19 @@ fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast
.ControlFlowExpression => { .ControlFlowExpression => {
const cfe = child_node.cast(ast.Node.ControlFlowExpression).?; const cfe = child_node.cast(ast.Node.ControlFlowExpression).?;
if (cfe.kind == .Return) { if (cfe.kind == .Return) {
// If we are calling ourselves recursively, ignore this return.
if (cfe.rhs) |rhs| {
if (rhs.cast(ast.Node.SuffixOp)) |suffix_op| {
if (suffix_op.op == .Call) {
if (suffix_op.lhs.node.id == .Identifier) {
if (std.mem.eql(u8, getDeclName(tree, suffix_op.lhs.node).?, getDeclName(tree, &fn_decl.base).?)) {
continue;
}
}
}
}
}
if (already_found.*) return null; if (already_found.*) return null;
already_found.* = true; already_found.* = true;
result = cfe; result = cfe;
@ -251,25 +269,28 @@ fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast
else => {}, else => {},
} }
result = findReturnStatementInternal(child_node, already_found); result = findReturnStatementInternal(tree, fn_decl, child_node, already_found);
} }
return result; return result;
} }
fn findReturnStatement(base_node: *ast.Node) ?*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(base_node, &already_found); return findReturnStatementInternal(tree, fn_decl, fn_decl.body_node.?, &already_found);
} }
/// Resolves the return type of a function /// Resolves the return type of a function
fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto, current_container: *ast.Node) ?*ast.Node { fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto) ?*ast.Node {
if (isTypeFunction(analysis_ctx.tree, fn_decl) and fn_decl.body_node != null) { if (isTypeFunction(analysis_ctx.tree, fn_decl) and fn_decl.body_node != 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(fn_decl.body_node.?) orelse return null; const ret = findReturnStatement(analysis_ctx.tree, fn_decl) orelse return null;
if (ret.rhs) |rhs| if (ret.rhs) |rhs|
if (resolveTypeOfNode(analysis_ctx, rhs, current_container)) |res_rhs| switch (res_rhs.id) { if (resolveTypeOfNode(analysis_ctx, rhs)) |res_rhs| switch (res_rhs.id) {
.ContainerDecl => return res_rhs, .ContainerDecl => {
analysis_ctx.onContainer(res_rhs.cast(ast.Node.ContainerDecl).?) catch return null;
return res_rhs;
},
else => return null, else => return null,
}; };
@ -277,44 +298,44 @@ fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto,
} }
return switch (fn_decl.return_type) { return switch (fn_decl.return_type) {
.Explicit, .InferErrorSet => |return_type| resolveTypeOfNode(analysis_ctx, return_type, current_container), .Explicit, .InferErrorSet => |return_type| resolveTypeOfNode(analysis_ctx, return_type),
.Invalid => null, .Invalid => null,
}; };
} }
/// Resolves the type of a node /// Resolves the type of a node
pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, current_container: *ast.Node) ?*ast.Node { pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node {
switch (node.id) { switch (node.id) {
.VarDecl => { .VarDecl => {
const vari = node.cast(ast.Node.VarDecl).?; const vari = node.cast(ast.Node.VarDecl).?;
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?, current_container) orelse null; return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
}, },
.ParamDecl => { .ParamDecl => {
const decl = node.cast(ast.Node.ParamDecl).?; const decl = node.cast(ast.Node.ParamDecl).?;
switch (decl.param_type) { switch (decl.param_type) {
.var_type, .type_expr => |var_type| { .var_type, .type_expr => |var_type| {
return resolveTypeOfNode(analysis_ctx, var_type, current_container) orelse null; return resolveTypeOfNode(analysis_ctx, var_type) orelse null;
}, },
else => {}, else => {},
} }
}, },
.Identifier => { .Identifier => {
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
return resolveTypeOfNode(analysis_ctx, child, current_container); return resolveTypeOfNode(analysis_ctx, child);
} else return null; } else return null;
}, },
.ContainerField => { .ContainerField => {
const field = node.cast(ast.Node.ContainerField).?; const field = node.cast(ast.Node.ContainerField).?;
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null, current_container); return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
}, },
.SuffixOp => { .SuffixOp => {
const suffix_op = node.cast(ast.Node.SuffixOp).?; const suffix_op = node.cast(ast.Node.SuffixOp).?;
switch (suffix_op.op) { switch (suffix_op.op) {
.Call, .StructInitializer => { .Call, .StructInitializer => {
const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node, current_container) orelse return null; const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node) orelse return null;
return switch (decl.id) { return switch (decl.id) {
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?, current_container), .FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
else => decl, else => decl,
}; };
}, },
@ -330,9 +351,9 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren
var rhs_str = nodeToString(analysis_ctx.tree, infix_op.rhs) orelse return null; var rhs_str = nodeToString(analysis_ctx.tree, infix_op.rhs) orelse return null;
// Use the analysis context temporary arena to store the rhs string. // Use the analysis context temporary arena to store the rhs string.
rhs_str = std.mem.dupe(&analysis_ctx.arena.allocator, u8, rhs_str) catch return null; rhs_str = std.mem.dupe(&analysis_ctx.arena.allocator, u8, rhs_str) catch return null;
const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs, current_container) orelse return null; const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs) orelse return null;
const child = getChild(analysis_ctx.tree, left, rhs_str) orelse return null; const child = getChild(analysis_ctx.tree, left, rhs_str) orelse return null;
return resolveTypeOfNode(analysis_ctx, child, current_container); return resolveTypeOfNode(analysis_ctx, child);
}, },
else => {}, else => {},
} }
@ -344,13 +365,13 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren
.PtrType => { .PtrType => {
const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token); const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token);
switch (op_token.id) { switch (op_token.id) {
.Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs, current_container), .Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs),
.LBracket, .AsteriskAsterisk => return null, .LBracket, .AsteriskAsterisk => return null,
else => unreachable, else => unreachable,
} }
}, },
.Try => { .Try => {
const rhs_type = resolveTypeOfNode(analysis_ctx, prefix_op.rhs, current_container) orelse return null; const rhs_type = resolveTypeOfNode(analysis_ctx, prefix_op.rhs) orelse return null;
switch (rhs_type.id) { switch (rhs_type.id) {
.InfixOp => { .InfixOp => {
const infix_op = rhs_type.cast(ast.Node.InfixOp).?; const infix_op = rhs_type.cast(ast.Node.InfixOp).?;
@ -368,7 +389,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren
const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token); const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token);
if (std.mem.eql(u8, call_name, "@This")) { if (std.mem.eql(u8, call_name, "@This")) {
if (builtin_call.params.len != 0) return null; if (builtin_call.params.len != 0) return null;
return current_container; return analysis_ctx.in_container;
} }
if (!std.mem.eql(u8, call_name, "@import")) return null; if (!std.mem.eql(u8, call_name, "@import")) return null;
@ -383,7 +404,10 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren
break :block null; break :block null;
}; };
}, },
.ContainerDecl => return node, .ContainerDecl => {
analysis_ctx.onContainer(node.cast(ast.Node.ContainerDecl).?) catch return null;
return node;
},
.MultilineStringLiteral, .StringLiteral, .ErrorSetDecl, .FnProto => return node, .MultilineStringLiteral, .StringLiteral, .ErrorSetDecl, .FnProto => return node,
else => std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}), else => std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}),
} }
@ -437,6 +461,7 @@ pub fn getFieldAccessTypeNode(
line_length: usize, line_length: usize,
) ?*ast.Node { ) ?*ast.Node {
var current_node = analysis_ctx.in_container; var current_node = analysis_ctx.in_container;
var current_container = analysis_ctx.in_container;
while (true) { while (true) {
var next = tokenizer.next(); var next = tokenizer.next();
@ -444,7 +469,7 @@ pub fn getFieldAccessTypeNode(
.Eof => return current_node, .Eof => return current_node,
.Identifier => { .Identifier => {
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| {
if (resolveTypeOfNode(analysis_ctx, child, current_node)) |node_type| { if (resolveTypeOfNode(analysis_ctx, child)) |node_type| {
current_node = node_type; current_node = node_type;
} else return null; } else return null;
} else return null; } else return null;
@ -458,7 +483,7 @@ pub fn getFieldAccessTypeNode(
if (after_period.end == line_length) return current_node; if (after_period.end == line_length) return current_node;
if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| { if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| {
if (resolveTypeOfNode(analysis_ctx, child, current_node)) |child_type| { if (resolveTypeOfNode(analysis_ctx, child)) |child_type| {
current_node = child_type; current_node = child_type;
} else return null; } else return null;
} else return null; } else return null;
@ -468,17 +493,40 @@ pub fn getFieldAccessTypeNode(
switch (current_node.id) { switch (current_node.id) {
.FnProto => { .FnProto => {
const func = current_node.cast(ast.Node.FnProto).?; const func = current_node.cast(ast.Node.FnProto).?;
if (resolveReturnType(analysis_ctx, func, current_node)) |ret| { if (resolveReturnType(analysis_ctx, func)) |ret| {
current_node = ret; current_node = ret;
// Skip to the right paren
var paren_count: usize = 1;
next = tokenizer.next();
while (next.id != .Eof) : (next = tokenizer.next()) {
if (next.id == .RParen) {
paren_count -= 1;
if (paren_count == 0) break;
} else if (next.id == .LParen) {
paren_count += 1;
}
} else return null;
} else { } else {
return null; return null;
} }
}, },
else => {} else => {},
}
},
.Keyword_const, .Keyword_var => {
next = tokenizer.next();
if (next.id == .Identifier) {
next = tokenizer.next();
if (next.id != .Equal) return null;
continue;
} }
}, },
else => std.debug.warn("Not implemented; {}\n", .{next.id}), else => std.debug.warn("Not implemented; {}\n", .{next.id}),
} }
if (current_node.id == .ContainerDecl or current_node.id == .Root) {
current_container = current_node;
}
} }
return current_node; return current_node;

View File

@ -266,6 +266,16 @@ pub const AnalysisContext = struct {
self.in_container = &self.tree.root_node.base; self.in_container = &self.tree.root_node.base;
} }
pub fn onContainer(self: *AnalysisContext, container: *std.zig.ast.Node.ContainerDecl) !void {
if (self.in_container != &container.base) {
self.in_container = &container.base;
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&self.arena.allocator);
try analysis.addChildrenNodes(&scope_nodes, self.tree, &container.base);
self.scope_nodes = scope_nodes.items;
}
}
pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node { pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node {
const allocator = self.store.allocator; const allocator = self.store.allocator;
const final_uri = (try uriFromImportStr( const final_uri = (try uriFromImportStr(

View File

@ -183,7 +183,7 @@ fn containerToCompletion(
while (container.iterate(index)) |child_node| : (index += 1) { while (container.iterate(index)) |child_node| : (index += 1) {
// Declarations in the same file do not need to be public. // Declarations in the same file do not need to be public.
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) { if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) {
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, container, config); try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
} }
} }
} }
@ -193,7 +193,6 @@ fn nodeToCompletion(
analysis_ctx: *DocumentStore.AnalysisContext, analysis_ctx: *DocumentStore.AnalysisContext,
orig_handle: *DocumentStore.Handle, orig_handle: *DocumentStore.Handle,
node: *std.zig.ast.Node, node: *std.zig.ast.Node,
current_container: *std.zig.ast.Node,
config: Config, config: Config,
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
var doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree, node)) |doc_comments| var doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree, node)) |doc_comments|
@ -245,11 +244,11 @@ fn nodeToCompletion(
break :block var_decl.init_node.?; break :block var_decl.init_node.?;
}; };
if (analysis.resolveTypeOfNode(&child_analysis_context, child_node, current_container)) |resolved_node| { if (analysis.resolveTypeOfNode(&child_analysis_context, child_node)) |resolved_node| {
// Special case for function aliases // Special case for function aliases
// In the future it might be used to print types of values instead of their declarations // In the future it might be used to print types of values instead of their declarations
if (resolved_node.id == .FnProto) { if (resolved_node.id == .FnProto) {
try nodeToCompletion(list, &child_analysis_context, orig_handle, resolved_node, current_container, config); try nodeToCompletion(list, &child_analysis_context, orig_handle, resolved_node, config);
return; return;
} }
} }
@ -423,7 +422,7 @@ fn completeGlobal(id: i64, pos_index: usize, handle: *DocumentStore.Handle, conf
for (analysis_ctx.scope_nodes) |decl_ptr| { for (analysis_ctx.scope_nodes) |decl_ptr| {
var decl = decl_ptr.*; var decl = decl_ptr.*;
try nodeToCompletion(&completions, &analysis_ctx, handle, decl_ptr, &analysis_ctx.tree.root_node.base, config); try nodeToCompletion(&completions, &analysis_ctx, handle, decl_ptr, config);
} }
try send(types.Response{ try send(types.Response{
@ -451,7 +450,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
const line_length = line.len - line_start_idx; const line_length = line.len - line_start_idx;
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer, line_length)) |node| { if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer, line_length)) |node| {
try nodeToCompletion(&completions, &analysis_ctx, handle, node, node, config); try nodeToCompletion(&completions, &analysis_ctx, handle, node, config);
} }
try send(types.Response{ try send(types.Response{
.id = .{ .Integer = id }, .id = .{ .Integer = id },
@ -796,8 +795,6 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v
const pos_index = try handle.document.positionToIndex(pos); const pos_index = try handle.document.positionToIndex(pos);
const pos_context = documentPositionContext(handle.document, pos_index); const pos_context = documentPositionContext(handle.document, pos_index);
std.debug.warn("{}", .{pos_context});
const this_config = configFromUriOr(uri, config); const this_config = configFromUriOr(uri, config);
switch (pos_context) { switch (pos_context) {
.builtin => try send(types.Response{ .builtin => try send(types.Response{