feat(cc): function-pointer typedefs/params + positional struct initializers#492
Merged
Conversation
458c6fd to
c13b670
Compare
``typedef <ret> (*<alias>)(<args>);`` registers ``<alias>`` as a typedef-aliased spelling for the opaque ``"function_pointer"`` type cc.py already uses for local function-pointer variables and function-pointer parameters (b2ccf5d). The inner argument list is parsed and discarded — cc.py has no per-typedef callable machinery yet, so calling through a typedef'd function pointer still requires the local-variable shape; but the alias is enough for parameter types, return types, and casts, which is what ``<signal.h>`` and the ``<stdlib.h>`` qsort/bsearch/atexit prototypes need. Forcing function: ``user/libbboeos/include/signal.h`` declares ``typedef void (*sighandler_t)(int);`` and consumers use the alias as a parameter type, return type, and inside a cast (``#define SIG_DFL ((sighandler_t)0)``). Pre-patch, cc.py rejected the typedef with ``expected typedef alias name, got LPAREN``. Post-patch, the alias resolves through the existing ``typedef_aliases`` map and Cast is identity codegen for non-struct-pointer targets, so every use site Just Works. Phase 6 enablement: shrinks the libbboeos-on-cc.py gap by one header (``<signal.h>``) and removes a blocker for compiling ``user/libbboeos/stdlib.h`` and ``stdio.h`` prototypes that take callback function-pointer parameters. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
``struct T x = {a, b, c};`` is the positional form of brace
initialization — fields fill in declaration order, matching
standard C. Pre-patch, ``_parse_designated_struct_initializer``
hard-rejected any non-``.field``, non-``{0}`` shape; post-patch
it dispatches by leading token: ``DOT`` keeps the designated
path, anything else parses positional values into the existing
``StructInitializer.positional`` slot.
Codegen sides:
* Local struct VarDecl: ``_emit_struct_initializer`` already
zero-stores the slot then emits per-field MemberAssigns. The
positional branch zips field-name order from
``struct_layouts[tag]`` with the value list and reuses the
same MemberAssign synthesis.
* File-scope struct global: previously rejected because
``_constant_expression(StructInitializer)`` returns None. The
guard now lets brace initializers through for struct-typed
globals; a new ``_validate_struct_global_initializer`` checks
every field value is constant, and the global-emission loop
walks the layout to emit a width-appropriate directive per
field (``db``/``dw``/``dd`` matched to the field width).
Forcing function: ``user/libbboeos/stdio.c`` declares ``static
FILE _stderr = {0, 0, 2};`` on line 14. Phase 6 enablement —
``stdio.c`` now parses past line 14 (stops at line 451 on an
unrelated ``L`` identifier issue).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
c13b670 to
71cb6ed
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two parser/codegen additions, each Phase 6 enablement for compiling
user/libbboeos/through cc.py.Function-pointer typedef aliases (Commit 1):
typedef <ret> (*<alias>)(<args>);registers<alias>as a typedef-aliasedspelling for the opaque
"function_pointer"type cc.py alreadyuses for local function-pointer variables and function-pointer
parameters (b2ccf5d). Unblocks
<signal.h>(sighandler_t)and the cast-based
SIG_DFL/SIG_IGNmacros, plus the<stdlib.h>qsort/bsearch/atexit prototypes that use namedcallback typedefs.
Positional struct initializers (Commit 2):
struct T x = {a, b, c};is the positional form of brace initialization — fieldsfill in declaration order. Pre-patch, cc.py only accepted
{0}zero-init and the designated
{.field = expr, ...}form.Codegen extended on both sides: local VarDecl reuses the existing
zero-store + per-field MemberAssign path; file-scope struct
globals now emit per-field directives walked from the struct
layout (
db/dw/ddmatched to field width). Forcingfunction:
user/libbboeos/stdio.cline 14 declaresstatic FILE _stderr = {0, 0, 2};.Test plan
python3 -m pytest tests/unit/ tests/test_ccobj.py tests/test_ccld.py tests/test_ccar.py -x -q— 559 passedpython3 tests/test_cc_bits.py— 112/112 passcc.py user/libbboeos/stdio.cparses past line 14(stops at line 451 on an unrelated identifier-as-label issue)
cc.py user/libbboeos/stdlib.cparses past theqsort/bsearch prototypes (the file's remaining blocker is
static void (*_atexit_fns[N])(void);— an array offunction pointers at file scope, not covered by this PR)
cc.py user/libbboeos/signal.cstill hits the pre-existingstringify-operator (
#x) limitation in the preprocessor at<syscalls.h>; the typedef/cast/parameter parse cleanly inisolation, confirmed via direct
signal.hprobes./make_os.sh— full image build succeedstimeout 240 python3 tests/test_asm.py— 42 passed, 0 failed🤖 Generated with Claude Code