Skip to content

Commit 30625d5

Browse files
committed
Refractor ast_context.current_package now being called through defered functions
1 parent 1d2df42 commit 30625d5

File tree

5 files changed

+168
-76
lines changed

5 files changed

+168
-76
lines changed

src/server/analysis.odin

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ AstContext :: struct {
8686
imports: []Package, //imports for the current document
8787
current_package: string,
8888
document_package: string,
89+
saved_package: string,
8990
use_locals: bool,
9091
local_id: int,
9192
call: ^ast.Call_Expr, //used to determine the types for generics and the correct function for overloaded functions
@@ -128,6 +129,46 @@ make_ast_context :: proc(
128129
return ast_context
129130
}
130131

132+
set_ast_package_deferred :: proc(ast_context: ^AstContext, pkg: string) {
133+
ast_context.current_package = ast_context.saved_package
134+
}
135+
136+
@(deferred_in = set_ast_package_deferred)
137+
set_ast_package_set_scoped :: proc(ast_context: ^AstContext, pkg: string) {
138+
ast_context.saved_package = ast_context.current_package
139+
ast_context.current_package = pkg
140+
}
141+
142+
set_ast_package_none_deferred :: proc(ast_context: ^AstContext) {
143+
ast_context.current_package = ast_context.saved_package
144+
}
145+
146+
@(deferred_in = set_ast_package_none_deferred)
147+
set_ast_package_scoped :: proc(ast_context: ^AstContext) {
148+
ast_context.saved_package = ast_context.current_package
149+
}
150+
151+
set_ast_package_from_symbol_deferred :: proc(
152+
ast_context: ^AstContext,
153+
symbol: Symbol,
154+
) {
155+
ast_context.current_package = ast_context.saved_package
156+
}
157+
158+
@(deferred_in = set_ast_package_from_symbol_deferred)
159+
set_ast_package_from_symbol_scoped :: proc(
160+
ast_context: ^AstContext,
161+
symbol: Symbol,
162+
) {
163+
ast_context.saved_package = ast_context.current_package
164+
165+
if symbol.pkg != "" {
166+
ast_context.current_package = symbol.pkg
167+
} else {
168+
ast_context.current_package = ast_context.document_package
169+
}
170+
}
171+
131172
reset_ast_context :: proc(ast_context: ^AstContext) {
132173
ast_context.use_locals = true
133174
clear(&ast_context.recursion_map)
@@ -154,10 +195,7 @@ resolve_type_comp_literal :: proc(
154195
}
155196

156197

157-
prev_package := ast_context.current_package
158-
ast_context.current_package = current_symbol.pkg
159-
160-
defer ast_context.current_package = prev_package
198+
set_ast_package_set_scoped(ast_context, current_symbol.pkg)
161199

162200
for elem, element_index in current_comp_lit.elems {
163201
if !position_in_node(elem, position_context.position) {
@@ -737,7 +775,7 @@ resolve_basic_directive :: proc(
737775
ast_context.allocator,
738776
)
739777
ident.name = "Source_Code_Location"
740-
ast_context.current_package = ast_context.document_package
778+
set_ast_package_set_scoped(ast_context, ast_context.document_package)
741779
return internal_resolve_type_identifier(ast_context, ident^)
742780
}
743781

@@ -781,11 +819,7 @@ internal_resolve_type_expression :: proc(
781819
return {}, false
782820
}
783821

784-
saved_package := ast_context.current_package
785-
786-
defer {
787-
ast_context.current_package = saved_package
788-
}
822+
set_ast_package_scoped(ast_context)
789823

790824
if check_node_recursion(ast_context, node) {
791825
return {}, false
@@ -979,7 +1013,7 @@ internal_resolve_type_expression :: proc(
9791013
return {}, false
9801014
}
9811015

982-
ast_context.current_package = indexed.pkg
1016+
set_ast_package_set_scoped(ast_context, indexed.pkg)
9831017

9841018
symbol: Symbol
9851019

@@ -1027,11 +1061,7 @@ internal_resolve_type_expression :: proc(
10271061
); ok {
10281062
ast_context.use_locals = false
10291063

1030-
if selector.pkg != "" {
1031-
ast_context.current_package = selector.pkg
1032-
} else {
1033-
ast_context.current_package = ast_context.document_package
1034-
}
1064+
set_ast_package_from_symbol_scoped(ast_context, selector)
10351065

10361066
#partial switch s in selector.value {
10371067
case SymbolProcedureValue:
@@ -1052,11 +1082,7 @@ internal_resolve_type_expression :: proc(
10521082
); ok {
10531083
ast_context.use_locals = false
10541084

1055-
if selector.pkg != "" {
1056-
ast_context.current_package = selector.pkg
1057-
} else {
1058-
ast_context.current_package = ast_context.document_package
1059-
}
1085+
set_ast_package_from_symbol_scoped(ast_context, selector)
10601086

10611087
#partial switch s in selector.value {
10621088
case SymbolFixedArrayValue:
@@ -1079,12 +1105,8 @@ internal_resolve_type_expression :: proc(
10791105
}
10801106

10811107
if components_count == 1 {
1082-
if selector.pkg != "" {
1083-
ast_context.current_package = selector.pkg
1084-
} else {
1085-
ast_context.current_package =
1086-
ast_context.document_package
1087-
}
1108+
set_ast_package_from_symbol_scoped(ast_context, selector)
1109+
10881110
symbol, ok := internal_resolve_type_expression(
10891111
ast_context,
10901112
s.expr,
@@ -1288,11 +1310,7 @@ internal_resolve_type_identifier :: proc(
12881310
return {}, false
12891311
}
12901312

1291-
saved_package := ast_context.current_package
1292-
1293-
defer {
1294-
ast_context.current_package = saved_package
1295-
}
1313+
set_ast_package_scoped(ast_context)
12961314

12971315
if v, ok := common.keyword_map[node.name]; ok {
12981316
//keywords
@@ -1679,7 +1697,7 @@ expand_struct_usings :: proc(
16791697
ranges := slice.to_dynamic(value.ranges, ast_context.allocator)
16801698

16811699
for k, v in value.usings {
1682-
ast_context.current_package = symbol.pkg
1700+
set_ast_package_set_scoped(ast_context, symbol.pkg)
16831701

16841702
field_expr: ^ast.Expr
16851703

@@ -1809,8 +1827,7 @@ resolve_comp_literal :: proc(
18091827
}
18101828
}
18111829

1812-
old_package := ast_context.current_package
1813-
ast_context.current_package = symbol.pkg
1830+
set_ast_package_set_scoped(ast_context, symbol.pkg)
18141831

18151832
symbol, _ = resolve_type_comp_literal(
18161833
ast_context,
@@ -1819,8 +1836,6 @@ resolve_comp_literal :: proc(
18191836
position_context.parent_comp_lit,
18201837
) or_return
18211838

1822-
ast_context.current_package = old_package
1823-
18241839
return symbol, true
18251840
}
18261841

@@ -1964,7 +1979,7 @@ resolve_implicit_selector :: proc(
19641979
position_context.parent_comp_lit,
19651980
); ok {
19661981
if s, ok := comp_symbol.value.(SymbolStructValue); ok {
1967-
ast_context.current_package = comp_symbol.pkg
1982+
set_ast_package_set_scoped(ast_context, comp_symbol.pkg)
19681983

19691984
//We can either have the final
19701985
elem_index := -1
@@ -1993,7 +2008,7 @@ resolve_implicit_selector :: proc(
19932008
return resolve_type_expression(ast_context, type)
19942009
} else if s, ok := comp_symbol.value.(SymbolBitFieldValue);
19952010
ok {
1996-
ast_context.current_package = comp_symbol.pkg
2011+
set_ast_package_set_scoped(ast_context, comp_symbol.pkg)
19972012

19982013
//We can either have the final
19992014
elem_index := -1
@@ -2140,7 +2155,8 @@ resolve_unresolved_symbol :: proc(
21402155
case SymbolBitSetValue:
21412156
symbol.type = .Enum
21422157
case SymbolGenericValue:
2143-
ast_context.current_package = symbol.pkg
2158+
set_ast_package_set_scoped(ast_context, symbol.pkg)
2159+
21442160
if ret, ok := resolve_type_expression(ast_context, v.expr); ok {
21452161
symbol.type = ret.type
21462162
symbol.signature = ret.signature
@@ -2203,7 +2219,7 @@ resolve_location_comp_lit_field :: proc(
22032219
) {
22042220
reset_ast_context(ast_context)
22052221

2206-
ast_context.current_package = ast_context.document_package
2222+
set_ast_package_set_scoped(ast_context, ast_context.document_package)
22072223

22082224
symbol = resolve_comp_literal(ast_context, position_context) or_return
22092225

@@ -2236,7 +2252,7 @@ resolve_location_implicit_selector :: proc(
22362252
) {
22372253
reset_ast_context(ast_context)
22382254

2239-
ast_context.current_package = ast_context.document_package
2255+
set_ast_package_set_scoped(ast_context, ast_context.document_package)
22402256

22412257
symbol = resolve_implicit_selector(
22422258
ast_context,
@@ -2264,7 +2280,8 @@ resolve_location_selector :: proc(
22642280
ok: bool,
22652281
) {
22662282
reset_ast_context(ast_context)
2267-
ast_context.current_package = ast_context.document_package
2283+
2284+
set_ast_package_set_scoped(ast_context, ast_context.document_package)
22682285

22692286
symbol = resolve_type_expression(ast_context, selector.expr) or_return
22702287

@@ -3166,7 +3183,8 @@ get_locals_stmt :: proc(
31663183
save_assign := false,
31673184
) {
31683185
reset_ast_context(ast_context)
3169-
ast_context.current_package = ast_context.document_package
3186+
3187+
set_ast_package_set_scoped(ast_context, ast_context.document_package)
31703188

31713189
using ast
31723190

@@ -3817,7 +3835,7 @@ resolve_entire_file :: proc(
38173835

38183836
get_globals(document.ast, &ast_context)
38193837

3820-
ast_context.current_package = ast_context.document_package
3838+
set_ast_package_set_scoped(&ast_context, ast_context.document_package)
38213839

38223840
symbols := make(map[uintptr]SymbolAndNode, 10000, allocator)
38233841

@@ -4214,6 +4232,7 @@ unwrap_union :: proc(
42144232
bool,
42154233
) {
42164234
if union_symbol, ok := resolve_type_expression(ast_context, node); ok {
4235+
//TODO: This current package is sus, it probably shouldn't be there.
42174236
ast_context.current_package = union_symbol.pkg
42184237
if union_value, ok := union_symbol.value.(SymbolUnionValue); ok {
42194238
return union_value, true

0 commit comments

Comments
 (0)