Commit 52b22e81
52b22e81cc1eMerged
Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): emit sp_Argv typedef before function bodies
ARGV.length / ARGV[i] may be referenced from any function
(e.g. a user-defined def main; ARGV.each {|a| ... }; end called
from the C main()), but emit_main previously emitted the
typedef ... sp_Argv; and static sp_Argv sp_argv; declarations
just before int main(...) — past every other function body in
source order — so functions that referenced sp_argv ahead of
that point compiled with an undeclared identifier.
Hoist the two declarations into emit_forward_decls so they
precede every function body. The runtime initialization
(sp_argv.len = argc - 1; ...) stays in emit_main where main()
can fill it in from argc / argv.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 52b22e81cc1e
- Parent
- 139b5d374568
spinel_codegen.rbmodifiedView file
# ---- Forward declarations ---- def emit_forward_decls # ARGV (sp_argv) is referenced from any function that uses # `ARGV.length` / `ARGV[i]`, so the declaration has to precede # all function bodies — not just main()'s. emit_main keeps the # runtime initialization (`sp_argv.len = argc - 1; ...`) where # main() can fill it in from `argc` / `argv`. emit_raw("typedef struct{const char**data;mrb_int len;}sp_Argv;") emit_raw("static sp_Argv sp_argv;") emit_raw("") # Emit block helper functions accumulated during collection if @block_funcs != "" emit_raw(@block_funcs) def emit_main stmts = get_body_stmts(@root_id) emit_raw("typedef struct{const char**data;mrb_int len;}sp_Argv;") emit_raw("static sp_Argv sp_argv;") emit_raw("") emit_raw("int main(int argc,char**argv){") emit_raw(" sp_argv.len=argc-1;sp_argv.data=(const char**)malloc(sizeof(const char*)*(argc>1?argc-1:1));{int _i;for(_i=0;_i<sp_argv.len;_i++)sp_argv.data[_i]=sp_str_dup_external(argv[_i+1]);}") if @needs_rand == 1