@@ -177,7 +177,6 @@ struct cctx_S {
177177 compiletype_T ctx_compile_type ;
178178
179179 garray_T ctx_locals ; // currently visible local variables
180- int ctx_locals_count ; // total number of local variables
181180
182181 int ctx_has_closure ; // set to one if a closures was created in
183182 // the function
@@ -574,6 +573,22 @@ generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
574573 return isn ;
575574}
576575
576+ /*
577+ * Generate an ISN_DEBUG instruction.
578+ */
579+ static isn_T *
580+ generate_instr_debug (cctx_T * cctx )
581+ {
582+ isn_T * isn ;
583+ dfunc_T * dfunc = ((dfunc_T * )def_functions .ga_data )
584+ + cctx -> ctx_ufunc -> uf_dfunc_idx ;
585+
586+ if ((isn = generate_instr (cctx , ISN_DEBUG )) == NULL )
587+ return NULL ;
588+ isn -> isn_arg .number = dfunc -> df_var_names .ga_len ;
589+ return isn ;
590+ }
591+
577592/*
578593 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
579594 * But only for simple types.
@@ -2365,6 +2380,7 @@ reserve_local(
23652380 type_T * type )
23662381{
23672382 lvar_T * lvar ;
2383+ dfunc_T * dfunc ;
23682384
23692385 if (arg_exists (name , len , NULL , NULL , NULL , cctx ) == OK )
23702386 {
@@ -2381,12 +2397,20 @@ reserve_local(
23812397 // the last ones when leaving a scope, but then variables used in a closure
23822398 // might get overwritten. To keep things simple do not re-use stack
23832399 // entries. This is less efficient, but memory is cheap these days.
2384- lvar -> lv_idx = cctx -> ctx_locals_count ++ ;
2400+ dfunc = ((dfunc_T * )def_functions .ga_data ) + cctx -> ctx_ufunc -> uf_dfunc_idx ;
2401+ lvar -> lv_idx = dfunc -> df_var_names .ga_len ;
23852402
23862403 lvar -> lv_name = vim_strnsave (name , len == 0 ? STRLEN (name ) : len );
23872404 lvar -> lv_const = isConst ;
23882405 lvar -> lv_type = type ;
23892406
2407+ // Remember the name for debugging.
2408+ if (ga_grow (& dfunc -> df_var_names , 1 ) == FAIL )
2409+ return NULL ;
2410+ ((char_u * * )dfunc -> df_var_names .ga_data )[lvar -> lv_idx ] =
2411+ vim_strsave (lvar -> lv_name );
2412+ ++ dfunc -> df_var_names .ga_len ;
2413+
23902414 return lvar ;
23912415}
23922416
@@ -7486,7 +7510,7 @@ compile_elseif(char_u *arg, cctx_T *cctx)
74867510 if (cctx -> ctx_compile_type == CT_DEBUG )
74877511 {
74887512 // the previous block was skipped, may want to debug this line
7489- generate_instr (cctx , ISN_DEBUG );
7513+ generate_instr_debug (cctx );
74907514 instr_count = instr -> ga_len ;
74917515 }
74927516 }
@@ -8239,7 +8263,7 @@ compile_catch(char_u *arg, cctx_T *cctx UNUSED)
82398263 }
82408264#endif
82418265 if (cctx -> ctx_compile_type == CT_DEBUG )
8242- generate_instr (cctx , ISN_DEBUG );
8266+ generate_instr_debug (cctx );
82438267 }
82448268
82458269 p = skipwhite (arg );
@@ -8976,6 +9000,7 @@ add_def_function(ufunc_T *ufunc)
89769000 ufunc -> uf_dfunc_idx = dfunc -> df_idx ;
89779001 dfunc -> df_ufunc = ufunc ;
89789002 dfunc -> df_name = vim_strsave (ufunc -> uf_name );
9003+ ga_init2 (& dfunc -> df_var_names , sizeof (char_u * ), 10 );
89799004 ++ dfunc -> df_refcount ;
89809005 ++ def_functions .ga_len ;
89819006 return OK ;
@@ -9026,7 +9051,21 @@ compile_def_function(
90269051 {
90279052 dfunc_T * dfunc = ((dfunc_T * )def_functions .ga_data )
90289053 + ufunc -> uf_dfunc_idx ;
9029- delete_def_function_contents (dfunc , FALSE);
9054+ isn_T * instr_dest ;
9055+
9056+ switch (compile_type )
9057+ {
9058+ case CT_PROFILE :
9059+ #ifdef FEAT_PROFILE
9060+ instr_dest = dfunc -> df_instr_prof ; break ;
9061+ #endif
9062+ case CT_NONE : instr_dest = dfunc -> df_instr ; break ;
9063+ case CT_DEBUG : instr_dest = dfunc -> df_instr_debug ; break ;
9064+ }
9065+ if (instr_dest != NULL )
9066+ // Was compiled in this mode before: Free old instructions.
9067+ delete_def_function_contents (dfunc , FALSE);
9068+ ga_clear_strings (& dfunc -> df_var_names );
90309069 }
90319070 else
90329071 {
@@ -9202,7 +9241,7 @@ compile_def_function(
92029241 && cctx .ctx_skip != SKIP_YES )
92039242 {
92049243 debug_lnum = cctx .ctx_lnum ;
9205- generate_instr (& cctx , ISN_DEBUG );
9244+ generate_instr_debug (& cctx );
92069245 }
92079246
92089247 // Some things can be recognized by the first character.
@@ -9670,7 +9709,7 @@ compile_def_function(
96709709 dfunc -> df_instr = instr -> ga_data ;
96719710 dfunc -> df_instr_count = instr -> ga_len ;
96729711 }
9673- dfunc -> df_varcount = cctx . ctx_locals_count ;
9712+ dfunc -> df_varcount = dfunc -> df_var_names . ga_len ;
96749713 dfunc -> df_has_closure = cctx .ctx_has_closure ;
96759714 if (cctx .ctx_outer_used )
96769715 ufunc -> uf_flags |= FC_CLOSURE ;
@@ -10037,6 +10076,7 @@ delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
1003710076 int idx ;
1003810077
1003910078 ga_clear (& dfunc -> df_def_args_isn );
10079+ ga_clear_strings (& dfunc -> df_var_names );
1004010080
1004110081 if (dfunc -> df_instr != NULL )
1004210082 {
0 commit comments