diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2025-06-28 19:54:32 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-28 19:54:32 +0200 |
| commit | fb1cabed13bbb199ec88a84deb1e670324f922f9 (patch) | |
| tree | 85464157e3691b127fc240d4614d88cd5c14458c /src | |
| parent | 91d4bc816f63f96a69ac0dd9a36358214c655d68 (diff) | |
| parent | ffb24d236028a1ee74b6044ed4fe8b49d938dbba (diff) | |
Merge pull request #685 from BradLewis/fix/references-in-switch
Improvements to local variable references
Diffstat (limited to 'src')
| -rw-r--r-- | src/server/analysis.odin | 66 | ||||
| -rw-r--r-- | src/server/completion.odin | 2 | ||||
| -rw-r--r-- | src/server/file_resolve.odin | 8 |
3 files changed, 28 insertions, 48 deletions
diff --git a/src/server/analysis.odin b/src/server/analysis.odin index 9703ece..b487c76 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -74,7 +74,6 @@ DocumentLocal :: struct { offset: int, resolved_global: bool, //Some locals have already been resolved and are now in global space local_global: bool, //Some locals act like globals, i.e. functions defined inside functions. - id: int, //Id that can used to connect the local to something, i.e. for stmt begin offset pkg: string, variable: bool, parameter: bool, @@ -82,8 +81,10 @@ DocumentLocal :: struct { DeferredDepth :: 35 +LocalGroup :: map[string][dynamic]DocumentLocal + AstContext :: struct { - locals: map[int]map[string][dynamic]DocumentLocal, //locals all the way to the document position + locals: [dynamic]LocalGroup, //locals all the way to the document position globals: map[string]GlobalExpr, recursion_map: map[rawptr]bool, usings: [dynamic]string, @@ -95,7 +96,6 @@ AstContext :: struct { deferred_package: [DeferredDepth]string, //When a package change happens when resolving deferred_count: int, use_locals: bool, - local_id: int, call: ^ast.Call_Expr, //used to determine the types for generics and the correct function for overloaded functions value_decl: ^ast.Value_Decl, field_name: ast.Ident, @@ -115,7 +115,7 @@ make_ast_context :: proc( allocator := context.temp_allocator, ) -> AstContext { ast_context := AstContext { - locals = make(map[int]map[string][dynamic]DocumentLocal, 0, allocator), + locals = make([dynamic]map[string][dynamic]DocumentLocal, 0, allocator), globals = make(map[string]GlobalExpr, 0, allocator), usings = make([dynamic]string, allocator), recursion_map = make(map[rawptr]bool, 0, allocator), @@ -129,7 +129,7 @@ make_ast_context :: proc( allocator = allocator, } - add_local_group(&ast_context, 0) + add_local_group(&ast_context) return ast_context } @@ -1193,19 +1193,18 @@ store_local :: proc( rhs: ^ast.Expr, offset: int, name: string, - id: int, local_global: bool, resolved_global: bool, variable: bool, pkg: string, parameter: bool, ) { - local_stack := &ast_context.locals[id][name] + local_group := get_local_group(ast_context) + local_stack := &local_group[name] if local_stack == nil { - locals := &ast_context.locals[id] - locals[name] = make([dynamic]DocumentLocal, ast_context.allocator) - local_stack = &locals[name] + local_group[name] = make([dynamic]DocumentLocal, ast_context.allocator) + local_stack = &local_group[name] } append( @@ -1214,7 +1213,6 @@ store_local :: proc( lhs = lhs, rhs = rhs, offset = offset, - id = id, resolved_global = resolved_global, local_global = local_global, pkg = pkg, @@ -1224,16 +1222,23 @@ store_local :: proc( ) } -add_local_group :: proc(ast_context: ^AstContext, id: int) { - ast_context.locals[id] = make(map[string][dynamic]DocumentLocal, 100, ast_context.allocator) +add_local_group :: proc(ast_context: ^AstContext) { + append(&ast_context.locals, make(LocalGroup, 100, ast_context.allocator)) +} + +pop_local_group :: proc(ast_context: ^AstContext) { + pop(&ast_context.locals) } -clear_local_group :: proc(ast_context: ^AstContext, id: int) { - ast_context.locals[id] = {} +get_local_group :: proc(ast_context: ^AstContext) -> ^LocalGroup { + if len(ast_context.locals) == 0 { + add_local_group(ast_context) + } + return &ast_context.locals[len(ast_context.locals) - 1] } get_local :: proc(ast_context: AstContext, ident: ast.Ident) -> (DocumentLocal, bool) { - for _, locals in ast_context.locals { + #reverse for locals in ast_context.locals { local_stack := locals[ident.name] or_continue #reverse for local in local_stack { @@ -1260,14 +1265,14 @@ get_local :: proc(ast_context: AstContext, ident: ast.Ident) -> (DocumentLocal, } get_local_offset :: proc(ast_context: ^AstContext, offset: int, name: string) -> int { - for _, locals in &ast_context.locals { + #reverse for locals in &ast_context.locals { if local_stack, ok := locals[name]; ok { #reverse for local, i in local_stack { - if local_stack[i].offset <= offset || local_stack[i].local_global { + if local.offset <= offset || local.local_global { if i < 0 { return -1 } else { - return local_stack[i].offset + return local.offset } } } @@ -2973,7 +2978,6 @@ get_locals_value_decl :: proc(file: ast.File, value_decl: ast.Value_Decl, ast_co value_decl.type, value_decl.end.offset, str, - ast_context.local_id, ast_context.non_mutable_only, false, value_decl.is_mutable, @@ -3013,7 +3017,6 @@ get_locals_value_decl :: proc(file: ast.File, value_decl: ast.Value_Decl, ast_co results[result_i], value_decl.end.offset, str, - ast_context.local_id, ast_context.non_mutable_only, calls[result_i] or_else false, value_decl.is_mutable, @@ -3157,7 +3160,6 @@ get_locals_using :: proc(expr: ^ast.Expr, ast_context: ^AstContext) { selector, 0, name, - ast_context.local_id, false, ast_context.non_mutable_only, true, @@ -3177,7 +3179,6 @@ get_locals_using :: proc(expr: ^ast.Expr, ast_context: ^AstContext) { selector, 0, name, - ast_context.local_id, false, ast_context.non_mutable_only, true, @@ -3221,7 +3222,6 @@ get_locals_assign_stmt :: proc(file: ast.File, stmt: ast.Assign_Stmt, ast_contex results[i], ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3275,7 +3275,6 @@ get_locals_for_range_stmt :: proc( make_int_ast(ast_context, ident.pos, ident.end), ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3321,7 +3320,6 @@ get_locals_for_range_stmt :: proc( expr, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3341,7 +3339,6 @@ get_locals_for_range_stmt :: proc( make_rune_ast(ast_context, ident.pos, ident.end), ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3361,7 +3358,6 @@ get_locals_for_range_stmt :: proc( make_rune_ast(ast_context, ident.pos, ident.end), ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3380,7 +3376,6 @@ get_locals_for_range_stmt :: proc( v.key, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3397,7 +3392,6 @@ get_locals_for_range_stmt :: proc( v.value, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3415,7 +3409,6 @@ get_locals_for_range_stmt :: proc( v.expr, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3432,7 +3425,6 @@ get_locals_for_range_stmt :: proc( make_int_ast(ast_context, ident.pos, ident.end), ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3450,7 +3442,6 @@ get_locals_for_range_stmt :: proc( v.expr, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3471,7 +3462,6 @@ get_locals_for_range_stmt :: proc( v.len, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3486,7 +3476,6 @@ get_locals_for_range_stmt :: proc( make_int_ast(ast_context, ident.pos, ident.end), ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3505,7 +3494,6 @@ get_locals_for_range_stmt :: proc( v.expr, ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3522,7 +3510,6 @@ get_locals_for_range_stmt :: proc( make_int_ast(ast_context, ident.pos, ident.end), ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3594,7 +3581,6 @@ get_locals_type_switch_stmt :: proc( cause.list[0], ident.pos.offset, ident.name, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3634,7 +3620,6 @@ get_locals_proc_param_and_results :: proc( arg.type, name.pos.offset, str, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3656,7 +3641,6 @@ get_locals_proc_param_and_results :: proc( arg.default_value, name.pos.offset, str, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3679,7 +3663,6 @@ get_locals_proc_param_and_results :: proc( result.type, name.pos.offset, str, - ast_context.local_id, ast_context.non_mutable_only, false, true, @@ -3694,7 +3677,6 @@ get_locals_proc_param_and_results :: proc( result.default_value, name.pos.offset, str, - ast_context.local_id, ast_context.non_mutable_only, false, true, diff --git a/src/server/completion.odin b/src/server/completion.odin index 4b8fd35..12c4205 100644 --- a/src/server/completion.odin +++ b/src/server/completion.odin @@ -1287,7 +1287,7 @@ get_identifier_completion :: proc( } } - for _, local in ast_context.locals { + #reverse for local in ast_context.locals { for k, v in local { if position_context.global_lhs_stmt { break diff --git a/src/server/file_resolve.odin b/src/server/file_resolve.odin index 18f4fae..41d9da5 100644 --- a/src/server/file_resolve.odin +++ b/src/server/file_resolve.odin @@ -103,8 +103,7 @@ resolve_decl :: proc( @(private = "file") local_scope_deferred :: proc(data: ^FileResolveData, stmt: ^ast.Stmt) { - clear_local_group(data.ast_context, data.ast_context.local_id) - data.ast_context.local_id -= 1 + pop_local_group(data.ast_context) } @(deferred_in = local_scope_deferred) @@ -114,9 +113,7 @@ local_scope :: proc(data: ^FileResolveData, stmt: ^ast.Stmt) { return } - data.ast_context.local_id += 1 - - add_local_group(data.ast_context, data.ast_context.local_id) + add_local_group(data.ast_context) data.position_context.position = stmt.end.offset data.position_context.nested_position = data.position_context.position @@ -391,6 +388,7 @@ resolve_node :: proc(node: ^ast.Node, data: ^FileResolveData) { case ^Defer_Stmt: resolve_node(n.stmt, data) case ^Case_Clause: + local_scope(data, n) resolve_nodes(n.list, data) resolve_nodes(n.body, data) case ^Type_Switch_Stmt: |