Skip to content

Commit f9ec65e

Browse files
committed
always pass std.zig.Ast by reference
1 parent a93032b commit f9ec65e

23 files changed

+359
-359
lines changed

src/DocumentScope.zig

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ global_enum_set: IdentifierSet,
2828
pub const IdentifierSet = std.ArrayHashMapUnmanaged(Ast.TokenIndex, void, IdentifierTokenContext, true);
2929

3030
pub const IdentifierTokenContext = struct {
31-
tree: Ast,
31+
tree: *const Ast,
3232

3333
pub fn eql(self: @This(), a: Ast.TokenIndex, b: Ast.TokenIndex, b_index: usize) bool {
3434
_ = b_index;
@@ -137,11 +137,11 @@ pub const Declaration = union(enum) {
137137
param_index: u16,
138138
func: Ast.Node.Index,
139139

140-
pub fn get(self: Param, tree: Ast) ?Ast.full.FnProto.Param {
140+
pub fn get(self: Param, tree: *const Ast) ?Ast.full.FnProto.Param {
141141
var buffer: [1]Ast.Node.Index = undefined;
142142
const func = tree.fullFnProto(&buffer, self.func).?;
143143
var param_index: u16 = 0;
144-
var it = func.iterate(&tree);
144+
var it = func.iterate(tree);
145145
while (ast.nextFnParam(&it)) |param| : (param_index += 1) {
146146
if (self.param_index == param_index) return param;
147147
}
@@ -154,12 +154,12 @@ pub const Declaration = union(enum) {
154154
node: Ast.Node.Index,
155155
index: u32,
156156

157-
pub fn getVarDeclNode(self: AssignDestructure, tree: Ast) Ast.Node.Index {
157+
pub fn getVarDeclNode(self: AssignDestructure, tree: *const Ast) Ast.Node.Index {
158158
const extra_index = tree.nodeData(self.node).extra_and_node[0];
159159
return @enumFromInt(tree.extra_data[@intFromEnum(extra_index) + 1 ..][self.index]);
160160
}
161161

162-
pub fn getFullVarDecl(self: AssignDestructure, tree: Ast) Ast.full.VarDecl {
162+
pub fn getFullVarDecl(self: AssignDestructure, tree: *const Ast) Ast.full.VarDecl {
163163
return tree.fullVarDecl(self.getVarDeclNode(tree)).?;
164164
}
165165
};
@@ -170,7 +170,7 @@ pub const Declaration = union(enum) {
170170
/// is guaranteed to have a payload_token
171171
case_index: u32,
172172

173-
pub fn getCase(self: Switch, tree: Ast) Ast.full.SwitchCase {
173+
pub fn getCase(self: Switch, tree: *const Ast) Ast.full.SwitchCase {
174174
const extra_index = tree.nodeData(self.node).node_and_extra[1];
175175
const cases = tree.extraDataSlice(tree.extraData(extra_index, Ast.Node.SubRange), Ast.Node.Index);
176176
return tree.fullSwitchCase(cases[self.case_index]).?;
@@ -200,7 +200,7 @@ pub const Declaration = union(enum) {
200200
}
201201

202202
/// Returns a `.identifier` or `.builtin` token.
203-
pub fn nameToken(decl: Declaration, tree: Ast) Ast.TokenIndex {
203+
pub fn nameToken(decl: Declaration, tree: *const Ast) Ast.TokenIndex {
204204
return switch (decl) {
205205
.ast_node => |node| {
206206
var buffer: [1]Ast.Node.Index = undefined;
@@ -339,7 +339,7 @@ pub const Scope = struct {
339339

340340
const ScopeContext = struct {
341341
allocator: std.mem.Allocator,
342-
tree: Ast,
342+
tree: *const Ast,
343343
doc_scope: *DocumentScope,
344344

345345
current_scope: Scope.OptionalIndex = .none,
@@ -515,7 +515,7 @@ const ScopeContext = struct {
515515
}
516516
};
517517

518-
pub fn init(allocator: std.mem.Allocator, tree: Ast) error{OutOfMemory}!DocumentScope {
518+
pub fn init(allocator: std.mem.Allocator, tree: *const Ast) error{OutOfMemory}!DocumentScope {
519519
const tracy_zone = tracy.trace(@src());
520520
defer tracy_zone.end();
521521

@@ -574,7 +574,7 @@ fn locToSmallLoc(loc: offsets.Loc) Scope.SmallLoc {
574574
/// Caller must finalize the scope
575575
fn walkNodeEnsureScope(
576576
context: *ScopeContext,
577-
tree: Ast,
577+
tree: *const Ast,
578578
node_idx: Ast.Node.Index,
579579
start_token: Ast.TokenIndex,
580580
) error{OutOfMemory}!ScopeContext.PushedScope {
@@ -602,7 +602,7 @@ fn walkNodeEnsureScope(
602602

603603
fn walkNode(
604604
context: *ScopeContext,
605-
tree: Ast,
605+
tree: *const Ast,
606606
node_idx: Ast.Node.Index,
607607
) error{OutOfMemory}!void {
608608
const tag = tree.nodeTag(node_idx);
@@ -799,7 +799,7 @@ fn walkNode(
799799

800800
noinline fn walkContainerDecl(
801801
context: *ScopeContext,
802-
tree: Ast,
802+
tree: *const Ast,
803803
node_idx: Ast.Node.Index,
804804
) error{OutOfMemory}!void {
805805
const tracy_zone = tracy.trace(@src());
@@ -840,7 +840,7 @@ noinline fn walkContainerDecl(
840840
var container_field = tree.fullContainerField(decl).?;
841841
if (is_struct and container_field.ast.tuple_like) continue;
842842

843-
container_field.convertToNonTupleLike(&tree);
843+
container_field.convertToNonTupleLike(tree);
844844
if (container_field.ast.tuple_like) continue;
845845
const main_token = container_field.ast.main_token;
846846
if (tree.tokenTag(main_token) != .identifier) continue;
@@ -891,7 +891,7 @@ noinline fn walkContainerDecl(
891891

892892
noinline fn walkErrorSetNode(
893893
context: *ScopeContext,
894-
tree: Ast,
894+
tree: *const Ast,
895895
node_idx: Ast.Node.Index,
896896
) error{OutOfMemory}!void {
897897
const scope = try context.startScope(
@@ -922,7 +922,7 @@ noinline fn walkErrorSetNode(
922922

923923
noinline fn walkFuncNode(
924924
context: *ScopeContext,
925-
tree: Ast,
925+
tree: *const Ast,
926926
node_idx: Ast.Node.Index,
927927
) error{OutOfMemory}!void {
928928
var buf: [1]Ast.Node.Index = undefined;
@@ -935,7 +935,7 @@ noinline fn walkFuncNode(
935935
);
936936

937937
var param_index: u16 = 0;
938-
var it = func.iterate(&tree);
938+
var it = func.iterate(tree);
939939
while (ast.nextFnParam(&it)) |param| : (param_index += 1) {
940940
if (param.name_token) |name_token| {
941941
try scope.pushDeclaration(
@@ -963,7 +963,7 @@ noinline fn walkFuncNode(
963963

964964
noinline fn walkBlockNode(
965965
context: *ScopeContext,
966-
tree: Ast,
966+
tree: *const Ast,
967967
node_idx: Ast.Node.Index,
968968
) error{OutOfMemory}!void {
969969
const pushed_scope = try walkBlockNodeKeepOpen(context, tree, node_idx, tree.firstToken(node_idx));
@@ -972,7 +972,7 @@ noinline fn walkBlockNode(
972972

973973
fn walkBlockNodeKeepOpen(
974974
context: *ScopeContext,
975-
tree: Ast,
975+
tree: *const Ast,
976976
node_idx: Ast.Node.Index,
977977
start_token: Ast.TokenIndex,
978978
) error{OutOfMemory}!ScopeContext.PushedScope {
@@ -1029,7 +1029,7 @@ fn walkBlockNodeKeepOpen(
10291029

10301030
noinline fn walkIfNode(
10311031
context: *ScopeContext,
1032-
tree: Ast,
1032+
tree: *const Ast,
10331033
node_idx: Ast.Node.Index,
10341034
) error{OutOfMemory}!void {
10351035
const if_node = ast.fullIf(tree, node_idx).?;
@@ -1068,7 +1068,7 @@ noinline fn walkIfNode(
10681068

10691069
noinline fn walkCatchNode(
10701070
context: *ScopeContext,
1071-
tree: Ast,
1071+
tree: *const Ast,
10721072
node_idx: Ast.Node.Index,
10731073
) error{OutOfMemory}!void {
10741074
const lhs, const rhs = tree.nodeData(node_idx).node_and_node;
@@ -1094,7 +1094,7 @@ noinline fn walkCatchNode(
10941094
/// label_token: inline_token while (cond_expr) |payload_token| : (cont_expr) then_expr else |error_token| else_expr
10951095
noinline fn walkWhileNode(
10961096
context: *ScopeContext,
1097-
tree: Ast,
1097+
tree: *const Ast,
10981098
node_idx: Ast.Node.Index,
10991099
) error{OutOfMemory}!void {
11001100
const while_node = ast.fullWhile(tree, node_idx).?;
@@ -1172,7 +1172,7 @@ noinline fn walkWhileNode(
11721172
/// label_token: inline_token for (inputs) |capture_tokens| then_expr else else_expr
11731173
noinline fn walkForNode(
11741174
context: *ScopeContext,
1175-
tree: Ast,
1175+
tree: *const Ast,
11761176
node_idx: Ast.Node.Index,
11771177
) error{OutOfMemory}!void {
11781178
const for_node = ast.fullFor(tree, node_idx).?;
@@ -1225,7 +1225,7 @@ noinline fn walkForNode(
12251225

12261226
noinline fn walkSwitchNode(
12271227
context: *ScopeContext,
1228-
tree: Ast,
1228+
tree: *const Ast,
12291229
node_idx: Ast.Node.Index,
12301230
) error{OutOfMemory}!void {
12311231
const full = tree.fullSwitch(node_idx).?;
@@ -1282,7 +1282,7 @@ noinline fn walkSwitchNode(
12821282

12831283
noinline fn walkErrdeferNode(
12841284
context: *ScopeContext,
1285-
tree: Ast,
1285+
tree: *const Ast,
12861286
node_idx: Ast.Node.Index,
12871287
) error{OutOfMemory}!void {
12881288
const opt_payload_token, const rhs = tree.nodeData(node_idx).opt_token_and_node;
@@ -1300,31 +1300,31 @@ noinline fn walkErrdeferNode(
13001300
}
13011301
}
13021302

1303-
noinline fn walkUnaryOpNode(context: *ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
1303+
noinline fn walkUnaryOpNode(context: *ScopeContext, tree: *const Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
13041304
try walkNode(context, tree, tree.nodeData(node_idx).node);
13051305
}
13061306

1307-
noinline fn walkBinOpNode(context: *ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
1307+
noinline fn walkBinOpNode(context: *ScopeContext, tree: *const Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
13081308
const lhs, const rhs = tree.nodeData(node_idx).node_and_node;
13091309
try walkNode(context, tree, lhs);
13101310
try walkNode(context, tree, rhs);
13111311
}
13121312

1313-
noinline fn walkOptNodeAndOptNode(context: *ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
1313+
noinline fn walkOptNodeAndOptNode(context: *ScopeContext, tree: *const Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
13141314
const opt_lhs, const opt_rhs = tree.nodeData(node_idx).opt_node_and_opt_node;
13151315
if (opt_lhs.unwrap()) |lhs| try walkNode(context, tree, lhs);
13161316
if (opt_rhs.unwrap()) |rhs| try walkNode(context, tree, rhs);
13171317
}
13181318

1319-
noinline fn walkNodeAndOptNode(context: *ScopeContext, tree: Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
1319+
noinline fn walkNodeAndOptNode(context: *ScopeContext, tree: *const Ast, node_idx: Ast.Node.Index) error{OutOfMemory}!void {
13201320
const lhs, const opt_rhs = tree.nodeData(node_idx).node_and_opt_node;
13211321
try walkNode(context, tree, lhs);
13221322
if (opt_rhs.unwrap()) |rhs| try walkNode(context, tree, rhs);
13231323
}
13241324

13251325
noinline fn walkOtherNode(
13261326
context: *ScopeContext,
1327-
tree: Ast,
1327+
tree: *const Ast,
13281328
node_idx: Ast.Node.Index,
13291329
) error{OutOfMemory}!void {
13301330
try ast.iterateChildren(tree, node_idx, context, error{OutOfMemory}, walkNode);

src/DocumentStore.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub const Handle = struct {
257257
var tree = try parseTree(allocator, text, mode);
258258
errdefer tree.deinit(allocator);
259259

260-
var cimports = try collectCIncludes(allocator, tree);
260+
var cimports = try collectCIncludes(allocator, &tree);
261261
errdefer cimports.deinit(allocator);
262262

263263
return .{
@@ -314,7 +314,7 @@ pub const Handle = struct {
314314

315315
if (self.impl.import_uris) |import_uris| return import_uris;
316316

317-
var imports = try analysis.collectImports(allocator, self.tree);
317+
var imports = try analysis.collectImports(allocator, &self.tree);
318318
defer imports.deinit(allocator);
319319

320320
const base_path = self.uri.toFsPath(allocator) catch |err| switch (err) {
@@ -345,7 +345,7 @@ pub const Handle = struct {
345345
if (self.getStatus().has_document_scope) return self.impl.document_scope;
346346
return try self.getLazy(DocumentScope, "document_scope", struct {
347347
fn create(handle: *Handle, allocator: std.mem.Allocator) error{OutOfMemory}!DocumentScope {
348-
var document_scope: DocumentScope = try .init(allocator, handle.tree);
348+
var document_scope: DocumentScope = try .init(allocator, &handle.tree);
349349
errdefer document_scope.deinit(allocator);
350350

351351
// remove unused capacity
@@ -1396,7 +1396,7 @@ pub const CImportHandle = struct {
13961396

13971397
/// Collects all `@cImport` nodes and converts them into c source code if possible
13981398
/// Caller owns returned memory.
1399-
fn collectCIncludes(allocator: std.mem.Allocator, tree: Ast) error{OutOfMemory}!std.MultiArrayList(CImportHandle) {
1399+
fn collectCIncludes(allocator: std.mem.Allocator, tree: *const Ast) error{OutOfMemory}!std.MultiArrayList(CImportHandle) {
14001400
const tracy_zone = tracy.trace(@src());
14011401
defer tracy_zone.end();
14021402

@@ -1661,7 +1661,7 @@ fn publishCimportDiagnostics(self: *DocumentStore, handle: *Handle) !void {
16611661

16621662
if (error_bundle.errorMessageCount() == 0) continue;
16631663

1664-
const loc = offsets.nodeToLoc(handle.tree, node);
1664+
const loc = offsets.nodeToLoc(&handle.tree, node);
16651665
const source_loc = std.zig.findLineColumn(handle.tree.source, loc.start);
16661666

16671667
comptime std.debug.assert(max_document_size <= std.math.maxInt(u32));

src/Server.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ fn documentSymbolsHandler(server: *Server, arena: std.mem.Allocator, request: ty
14321432
const handle = server.document_store.getHandle(document_uri) orelse return null;
14331433
if (handle.tree.mode == .zon) return null;
14341434
return .{
1435-
.array_of_DocumentSymbol = try document_symbol.getDocumentSymbols(arena, handle.tree, server.offset_encoding),
1435+
.array_of_DocumentSymbol = try document_symbol.getDocumentSymbols(arena, &handle.tree, server.offset_encoding),
14361436
};
14371437
}
14381438

@@ -1465,7 +1465,7 @@ fn prepareRenameHandler(server: *Server, arena: std.mem.Allocator, request: type
14651465
};
14661466
const handle = server.document_store.getHandle(document_uri) orelse return null;
14671467
const source_index = offsets.positionToIndex(handle.tree.source, request.position, server.offset_encoding);
1468-
const name_loc = Analyser.identifierLocFromIndex(handle.tree, source_index) orelse return null;
1468+
const name_loc = Analyser.identifierLocFromIndex(&handle.tree, source_index) orelse return null;
14691469
const name = offsets.locToSlice(handle.tree.source, name_loc);
14701470
return .{
14711471
.literal_1 = .{
@@ -1563,7 +1563,7 @@ fn foldingRangeHandler(server: *Server, arena: std.mem.Allocator, request: types
15631563
};
15641564
const handle = server.document_store.getHandle(document_uri) orelse return null;
15651565

1566-
return try folding_range.generateFoldingRanges(arena, handle.tree, server.offset_encoding);
1566+
return try folding_range.generateFoldingRanges(arena, &handle.tree, server.offset_encoding);
15671567
}
15681568

15691569
fn selectionRangeHandler(server: *Server, arena: std.mem.Allocator, request: types.SelectionRangeParams) Error!?[]types.SelectionRange {

0 commit comments

Comments
 (0)