@@ -148,45 +148,51 @@ struct cctx_S {
148148static void delete_def_function_contents (dfunc_T * dfunc );
149149
150150/*
151- * Lookup variable "name" in the local scope and return it.
152- * Return NULL if not found.
151+ * Lookup variable "name" in the local scope and return it in "lvar".
152+ * "lvar->lv_from_outer" is set accordingly.
153+ * If "lvar" is NULL only check if the variable can be found.
154+ * Return FAIL if not found.
153155 */
154- static lvar_T *
155- lookup_local (char_u * name , size_t len , cctx_T * cctx )
156+ static int
157+ lookup_local (char_u * name , size_t len , lvar_T * lvar , cctx_T * cctx )
156158{
157159 int idx ;
158- lvar_T * lvar ;
160+ lvar_T * lvp ;
159161
160162 if (len == 0 )
161- return NULL ;
163+ return FAIL ;
162164
163165 // Find local in current function scope.
164166 for (idx = 0 ; idx < cctx -> ctx_locals .ga_len ; ++ idx )
165167 {
166- lvar = ((lvar_T * )cctx -> ctx_locals .ga_data ) + idx ;
167- if (STRNCMP (name , lvar -> lv_name , len ) == 0
168- && STRLEN (lvar -> lv_name ) == len )
168+ lvp = ((lvar_T * )cctx -> ctx_locals .ga_data ) + idx ;
169+ if (STRNCMP (name , lvp -> lv_name , len ) == 0
170+ && STRLEN (lvp -> lv_name ) == len )
169171 {
170- lvar -> lv_from_outer = FALSE;
171- return lvar ;
172+ if (lvar != NULL )
173+ {
174+ * lvar = * lvp ;
175+ lvar -> lv_from_outer = FALSE;
176+ }
177+ return OK ;
172178 }
173179 }
174180
175181 // Find local in outer function scope.
176182 if (cctx -> ctx_outer != NULL )
177183 {
178- lvar = lookup_local (name , len , cctx -> ctx_outer );
179- if (lvar != NULL )
184+ if (lookup_local (name , len , lvar , cctx -> ctx_outer ) == OK )
180185 {
181- // TODO: are there situations we should not mark the outer scope as
182- // used?
183- cctx -> ctx_outer_used = TRUE;
184- lvar -> lv_from_outer = TRUE;
185- return lvar ;
186+ if (lvar != NULL )
187+ {
188+ cctx -> ctx_outer_used = TRUE;
189+ lvar -> lv_from_outer = TRUE;
190+ }
191+ return OK ;
186192 }
187193 }
188194
189- return NULL ;
195+ return FAIL ;
190196}
191197
192198/*
@@ -377,7 +383,7 @@ check_defined(char_u *p, size_t len, cctx_T *cctx)
377383 p [len ] = NUL ;
378384 if (script_var_exists (p , len , FALSE, cctx ) == OK
379385 || (cctx != NULL
380- && (lookup_local (p , len , cctx ) != NULL
386+ && (lookup_local (p , len , NULL , cctx ) == OK
381387 || arg_exists (p , len , NULL , NULL , NULL , cctx ) == OK ))
382388 || find_imported (p , len , cctx ) != NULL
383389 || (ufunc = find_func_even_dead (p , FALSE, cctx )) != NULL )
@@ -2555,13 +2561,13 @@ compile_load(
25552561 }
25562562 else
25572563 {
2558- lvar_T * lvar = lookup_local ( * arg , len , cctx ) ;
2564+ lvar_T lvar ;
25592565
2560- if (lvar != NULL )
2566+ if (lookup_local ( * arg , len , & lvar , cctx ) == OK )
25612567 {
2562- type = lvar -> lv_type ;
2563- idx = lvar -> lv_idx ;
2564- if (lvar -> lv_from_outer )
2568+ type = lvar . lv_type ;
2569+ idx = lvar . lv_idx ;
2570+ if (lvar . lv_from_outer )
25652571 gen_load_outer = TRUE;
25662572 else
25672573 gen_load = TRUE;
@@ -2763,7 +2769,7 @@ compile_call(
27632769
27642770 // An argument or local variable can be a function reference, this
27652771 // overrules a function name.
2766- if (lookup_local (namebuf , varlen , cctx ) == NULL
2772+ if (lookup_local (namebuf , varlen , NULL , cctx ) == FAIL
27672773 && arg_exists (namebuf , varlen , NULL , NULL , NULL , cctx ) != OK )
27682774 {
27692775 // If we can find the function by name generate the right call.
@@ -5366,6 +5372,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
53665372 assign_dest_T dest = dest_local ;
53675373 int opt_flags = 0 ;
53685374 int vimvaridx = -1 ;
5375+ lvar_T local_lvar ;
53695376 lvar_T * lvar = NULL ;
53705377 lvar_T arg_lvar ;
53715378 int has_type = FALSE;
@@ -5424,8 +5431,10 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
54245431 goto theend ;
54255432 }
54265433
5427- lvar = lookup_local (var_start , varlen , cctx );
5428- if (lvar == NULL )
5434+
5435+ if (lookup_local (var_start , varlen , & local_lvar , cctx ) == OK )
5436+ lvar = & local_lvar ;
5437+ else
54295438 {
54305439 CLEAR_FIELD (arg_lvar );
54315440 if (arg_exists (var_start , varlen ,
@@ -6579,8 +6588,7 @@ compile_for(char_u *arg_start, cctx_T *cctx)
65796588 }
65806589 else
65816590 {
6582- var_lvar = lookup_local (arg , varlen , cctx );
6583- if (var_lvar != NULL )
6591+ if (lookup_local (arg , varlen , NULL , cctx ) == OK )
65846592 {
65856593 semsg (_ (e_variable_already_declared ), arg );
65866594 goto failed ;
@@ -7584,7 +7592,7 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
75847592 || * ea .cmd == '$'
75857593 || * ea .cmd == '@'
75867594 || ((len ) > 2 && ea .cmd [1 ] == ':' )
7587- || lookup_local (ea .cmd , len , & cctx ) != NULL
7595+ || lookup_local (ea .cmd , len , NULL , & cctx ) == OK
75887596 || arg_exists (ea .cmd , len , NULL , NULL ,
75897597 NULL , & cctx ) == OK
75907598 || script_var_exists (ea .cmd , len ,
@@ -7637,7 +7645,7 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
76377645 }
76387646 }
76397647 p = find_ex_command (& ea , NULL , starts_with_colon ? NULL
7640- : (void * (* )(char_u * , size_t , cctx_T * ))lookup_local ,
7648+ : (int (* )(char_u * , size_t , void * , cctx_T * ))lookup_local ,
76417649 & cctx );
76427650
76437651 if (p == ea .cmd && ea .cmdidx != CMD_SIZE )
0 commit comments