@@ -86,6 +86,7 @@ AstContext :: struct {
86
86
imports: []Package, // imports for the current document
87
87
current_package: string ,
88
88
document_package: string ,
89
+ saved_package: string ,
89
90
use_locals: bool ,
90
91
local_id: int ,
91
92
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(
128
129
return ast_context
129
130
}
130
131
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
+
131
172
reset_ast_context :: proc (ast_context: ^AstContext) {
132
173
ast_context.use_locals = true
133
174
clear (&ast_context.recursion_map)
@@ -154,10 +195,7 @@ resolve_type_comp_literal :: proc(
154
195
}
155
196
156
197
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)
161
199
162
200
for elem, element_index in current_comp_lit.elems {
163
201
if !position_in_node (elem, position_context.position) {
@@ -737,7 +775,7 @@ resolve_basic_directive :: proc(
737
775
ast_context.allocator,
738
776
)
739
777
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)
741
779
return internal_resolve_type_identifier (ast_context, ident^)
742
780
}
743
781
@@ -781,11 +819,7 @@ internal_resolve_type_expression :: proc(
781
819
return {}, false
782
820
}
783
821
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)
789
823
790
824
if check_node_recursion (ast_context, node) {
791
825
return {}, false
@@ -979,7 +1013,7 @@ internal_resolve_type_expression :: proc(
979
1013
return {}, false
980
1014
}
981
1015
982
- ast_context.current_package = indexed.pkg
1016
+ set_ast_package_set_scoped ( ast_context, indexed.pkg)
983
1017
984
1018
symbol: Symbol
985
1019
@@ -1027,11 +1061,7 @@ internal_resolve_type_expression :: proc(
1027
1061
); ok {
1028
1062
ast_context.use_locals = false
1029
1063
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)
1035
1065
1036
1066
#partial switch s in selector.value {
1037
1067
case SymbolProcedureValue:
@@ -1052,11 +1082,7 @@ internal_resolve_type_expression :: proc(
1052
1082
); ok {
1053
1083
ast_context.use_locals = false
1054
1084
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)
1060
1086
1061
1087
#partial switch s in selector.value {
1062
1088
case SymbolFixedArrayValue:
@@ -1079,12 +1105,8 @@ internal_resolve_type_expression :: proc(
1079
1105
}
1080
1106
1081
1107
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
+
1088
1110
symbol, ok := internal_resolve_type_expression (
1089
1111
ast_context,
1090
1112
s.expr,
@@ -1288,11 +1310,7 @@ internal_resolve_type_identifier :: proc(
1288
1310
return {}, false
1289
1311
}
1290
1312
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)
1296
1314
1297
1315
if v, ok := common.keyword_map[node.name]; ok {
1298
1316
// keywords
@@ -1679,7 +1697,7 @@ expand_struct_usings :: proc(
1679
1697
ranges := slice.to_dynamic (value.ranges, ast_context.allocator)
1680
1698
1681
1699
for k, v in value.usings {
1682
- ast_context.current_package = symbol.pkg
1700
+ set_ast_package_set_scoped ( ast_context, symbol.pkg)
1683
1701
1684
1702
field_expr: ^ast.Expr
1685
1703
@@ -1809,8 +1827,7 @@ resolve_comp_literal :: proc(
1809
1827
}
1810
1828
}
1811
1829
1812
- old_package := ast_context.current_package
1813
- ast_context.current_package = symbol.pkg
1830
+ set_ast_package_set_scoped (ast_context, symbol.pkg)
1814
1831
1815
1832
symbol, _ = resolve_type_comp_literal (
1816
1833
ast_context,
@@ -1819,8 +1836,6 @@ resolve_comp_literal :: proc(
1819
1836
position_context.parent_comp_lit,
1820
1837
) or_return
1821
1838
1822
- ast_context.current_package = old_package
1823
-
1824
1839
return symbol, true
1825
1840
}
1826
1841
@@ -1964,7 +1979,7 @@ resolve_implicit_selector :: proc(
1964
1979
position_context.parent_comp_lit,
1965
1980
); ok {
1966
1981
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)
1968
1983
1969
1984
// We can either have the final
1970
1985
elem_index := -1
@@ -1993,7 +2008,7 @@ resolve_implicit_selector :: proc(
1993
2008
return resolve_type_expression (ast_context, type)
1994
2009
} else if s, ok := comp_symbol.value.(SymbolBitFieldValue);
1995
2010
ok {
1996
- ast_context.current_package = comp_symbol.pkg
2011
+ set_ast_package_set_scoped ( ast_context, comp_symbol.pkg)
1997
2012
1998
2013
// We can either have the final
1999
2014
elem_index := -1
@@ -2140,7 +2155,8 @@ resolve_unresolved_symbol :: proc(
2140
2155
case SymbolBitSetValue:
2141
2156
symbol.type = .Enum
2142
2157
case SymbolGenericValue:
2143
- ast_context.current_package = symbol.pkg
2158
+ set_ast_package_set_scoped (ast_context, symbol.pkg)
2159
+
2144
2160
if ret, ok := resolve_type_expression (ast_context, v.expr); ok {
2145
2161
symbol.type = ret.type
2146
2162
symbol.signature = ret.signature
@@ -2203,7 +2219,7 @@ resolve_location_comp_lit_field :: proc(
2203
2219
) {
2204
2220
reset_ast_context (ast_context)
2205
2221
2206
- ast_context.current_package = ast_context.document_package
2222
+ set_ast_package_set_scoped ( ast_context, ast_context.document_package)
2207
2223
2208
2224
symbol = resolve_comp_literal (ast_context, position_context) or_return
2209
2225
@@ -2236,7 +2252,7 @@ resolve_location_implicit_selector :: proc(
2236
2252
) {
2237
2253
reset_ast_context (ast_context)
2238
2254
2239
- ast_context.current_package = ast_context.document_package
2255
+ set_ast_package_set_scoped ( ast_context, ast_context.document_package)
2240
2256
2241
2257
symbol = resolve_implicit_selector (
2242
2258
ast_context,
@@ -2264,7 +2280,8 @@ resolve_location_selector :: proc(
2264
2280
ok: bool ,
2265
2281
) {
2266
2282
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)
2268
2285
2269
2286
symbol = resolve_type_expression (ast_context, selector.expr) or_return
2270
2287
@@ -3166,7 +3183,8 @@ get_locals_stmt :: proc(
3166
3183
save_assign := false ,
3167
3184
) {
3168
3185
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)
3170
3188
3171
3189
using ast
3172
3190
@@ -3817,7 +3835,7 @@ resolve_entire_file :: proc(
3817
3835
3818
3836
get_globals (document.ast, &ast_context)
3819
3837
3820
- ast_context.current_package = ast_context.document_package
3838
+ set_ast_package_set_scoped (& ast_context, ast_context.document_package)
3821
3839
3822
3840
symbols := make (map [uintptr ]SymbolAndNode, 10000 , allocator)
3823
3841
@@ -4214,6 +4232,7 @@ unwrap_union :: proc(
4214
4232
bool ,
4215
4233
) {
4216
4234
if union_symbol, ok := resolve_type_expression (ast_context, node); ok {
4235
+ // TODO: This current package is sus, it probably shouldn't be there.
4217
4236
ast_context.current_package = union_symbol.pkg
4218
4237
if union_value, ok := union_symbol.value.(SymbolUnionValue); ok {
4219
4238
return union_value, true
0 commit comments