From 6fe52d95b35b56d84cd063aee5b73310bb16a371 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Tue, 19 May 2020 18:53:01 +0300 Subject: [PATCH] Added @This() support --- src/analysis.zig | 20 +++++++++++++++++--- src/document_store.zig | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index c9f6e8b..9a19487 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -263,7 +263,10 @@ fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto) // a container declaration, we will return that declaration. const ret = findReturnStatement(fn_decl.body_node.?) orelse return null; if (ret.rhs) |rhs| 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, }; @@ -295,6 +298,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast. }, .Identifier => { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| { + std.debug.warn("Found node {}\n", .{child}); return resolveTypeOfNode(analysis_ctx, child); } else return null; }, @@ -347,7 +351,13 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast. }, .BuiltinCall => { const builtin_call = node.cast(ast.Node.BuiltinCall).?; - if (!std.mem.eql(u8, analysis_ctx.tree.tokenSlice(builtin_call.builtin_token), "@import")) return null; + const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token); + if (std.mem.eql(u8, call_name, "@This")) { + if (builtin_call.params.len != 0) return null; + return analysis_ctx.last_this_node; + } + + if (!std.mem.eql(u8, call_name, "@import")) return null; if (builtin_call.params.len > 1) return null; const import_param = builtin_call.params.at(0).*; @@ -359,7 +369,11 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast. break :block null; }; }, - .MultilineStringLiteral, .StringLiteral, .ContainerDecl, .ErrorSetDecl, .FnProto => return node, + .ContainerDecl => { + analysis_ctx.onContainer(node.cast(ast.Node.ContainerDecl).?) catch return null; + return node; + }, + .MultilineStringLiteral, .StringLiteral, .ErrorSetDecl, .FnProto => return node, else => std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}), } return null; diff --git a/src/document_store.zig b/src/document_store.zig index 084968c..c42459f 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -258,11 +258,13 @@ pub const AnalysisContext = struct { arena: *std.heap.ArenaAllocator, tree: *std.zig.ast.Tree, scope_nodes: []*std.zig.ast.Node, + last_this_node: *std.zig.ast.Node, fn refreshScopeNodes(self: *AnalysisContext) !void { var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&self.arena.allocator); try analysis.addChildrenNodes(&scope_nodes, self.tree, &self.tree.root_node.base); self.scope_nodes = scope_nodes.items; + self.last_this_node = &self.tree.root_node.base; } pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node { @@ -348,9 +350,20 @@ pub const AnalysisContext = struct { .arena = self.arena, .tree = tree, .scope_nodes = self.scope_nodes, + .last_this_node = &tree.root_node.base, }; } + pub fn onContainer(self: *AnalysisContext, container: *std.zig.ast.Node.ContainerDecl) !void { + if (self.last_this_node != &container.base) { + self.last_this_node = &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 deinit(self: *AnalysisContext) void { self.tree.deinit(); } @@ -368,6 +381,7 @@ pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.A .arena = arena, .tree = tree, .scope_nodes = scope_nodes.items, + .last_this_node = &tree.root_node.base, }; }