Mark nested functions as requiring a static chain earlier
Fixes a bug in handling nested functions, ensuring correct static chain determination and preventing issues with static initialization.
This change corrects how GCC handles nested functions, specifically when determining if they require a static chain for proper execution. Previously, the check for DECL_STATIC_CHAIN was performed too late, after the function body was parsed, which could lead to incorrect assumptions about static initialization of variables pointing to these functions. The fix ensures that DECL_STATIC_CHAIN is set earlier, conservatively assuming a static chain is needed until the function is fully parsed. This aligns with PR128760 and PR128518, improving the correctness of nested function handling and associated static initializations.
In Details
This patch addresses issues related to nested functions and their static chain requirements in GCC's C front-end. The core problem lies in the timing of setting DECL_STATIC_CHAIN. Previously, it was set only after the function body was parsed, which was too late for correctly evaluating static initializations that might use pointers to nested functions. Nested functions, especially those used in static initializers, often require a static chain to access their non-local context. The fix modifies start_decl and start_function to conservatively set DECL_STATIC_CHAIN for nested functions…
- nested function
- A function defined inside another function. In C, nested functions are a non-standard extension supported by GCC, often implemented using a trampoline and a static chain.
- static chain
- A data structure used in some programming language implementations to manage scope and access non-local variables for nested functions or closures. It's a linked list of activation records.
- DECL_STATIC_CHAIN
- An internal GCC flag associated with a function declaration, indicating whether that function requires a static chain.
- static initialization
- The process of initializing a variable at compile time or load time, rather than at runtime. This applies to global, static, and sometimes file-scope variables.
- trampoline
- A small piece of code that acts as an intermediary, often used in implementing nested functions or closures to set up the correct execution context (like the static chain) before calling the actual function code.
- PR128760
- Bug report number 128760 in GCC's bug tracking system, related to this issue.