From 7a27f0d80c5ce9d596b2b43699e131ab387e1317 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 4 May 2021 14:40:59 -0700 Subject: Sema: restore the extern lib name functionality --- src/Module.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 3f88efd717..f32909cabe 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1603,6 +1603,34 @@ pub const SrcLoc = struct { const token_starts = tree.tokens.items(.start); return token_starts[tok_index]; }, + + .node_offset_lib_name => |node_off| { + const tree = try src_loc.file_scope.getTree(gpa); + const node_datas = tree.nodes.items(.data); + const node_tags = tree.nodes.items(.tag); + const parent_node = src_loc.declRelativeToNodeIndex(node_off); + var params: [1]ast.Node.Index = undefined; + const full = switch (node_tags[parent_node]) { + .fn_proto_simple => tree.fnProtoSimple(¶ms, parent_node), + .fn_proto_multi => tree.fnProtoMulti(parent_node), + .fn_proto_one => tree.fnProtoOne(¶ms, parent_node), + .fn_proto => tree.fnProto(parent_node), + .fn_decl => blk: { + const fn_proto = node_datas[parent_node].lhs; + break :blk switch (node_tags[fn_proto]) { + .fn_proto_simple => tree.fnProtoSimple(¶ms, fn_proto), + .fn_proto_multi => tree.fnProtoMulti(fn_proto), + .fn_proto_one => tree.fnProtoOne(¶ms, fn_proto), + .fn_proto => tree.fnProto(fn_proto), + else => unreachable, + }; + }, + else => unreachable, + }; + const tok_index = full.lib_name.?; + const token_starts = tree.tokens.items(.start); + return token_starts[tok_index]; + }, } } }; @@ -1768,6 +1796,12 @@ pub const LazySrcLoc = union(enum) { /// to the type expression. /// The Decl is determined contextually. node_offset_anyframe_type: i32, + /// The source location points to the string literal of `extern "foo"`, found + /// by taking this AST node index offset from the containing + /// Decl AST node, which points to a function prototype or variable declaration + /// expression AST node. Next, navigate to the string literal of the `extern "foo"`. + /// The Decl is determined contextually. + node_offset_lib_name: i32, /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope. pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc { @@ -1808,6 +1842,7 @@ pub const LazySrcLoc = union(enum) { .node_offset_fn_type_cc, .node_offset_fn_type_ret_ty, .node_offset_anyframe_type, + .node_offset_lib_name, => .{ .file_scope = scope.getFileScope(), .parent_decl_node = scope.srcDecl().?.src_node, @@ -1855,6 +1890,7 @@ pub const LazySrcLoc = union(enum) { .node_offset_fn_type_cc, .node_offset_fn_type_ret_ty, .node_offset_anyframe_type, + .node_offset_lib_name, => .{ .file_scope = decl.getFileScope(), .parent_decl_node = decl.src_node, -- cgit v1.2.3