Skip to content

struct compound literal initialization - #302

Draft
lorettayao wants to merge 7 commits into
sysprog21:masterfrom
lorettayao:fix-compound-literals
Draft

struct compound literal initialization#302
lorettayao wants to merge 7 commits into
sysprog21:masterfrom
lorettayao:fix-compound-literals

Conversation

@lorettayao

@lorettayao lorettayao commented Sep 28, 2025

Copy link
Copy Markdown
Contributor

This PR is a work-in-progress for issue #299.

  • Fixes the offset bug in compound literal initialization by
    adjusting the source address by +4 bytes in parser.c.
  • Verified with simple test: x=10, y=20, z=30 now works.
  • Not final — needs feedback and further investigation.

Summary by cubic

Fix struct compound literal initialization in the parser so fields are assigned correctly instead of being shifted. Adds explicit brace-initializer emission and corrects assignment handling, addressing issue #299.

  • Bug Fixes
    • Parse (struct/union T){...} and typedef structs, allocating a temp struct and writing each field.
    • Add emit_struct_brace_initializer to compute field addresses and write values with correct sizes.
    • On assignment from a struct literal, copy from source address +4 in 4-byte chunks to avoid the offset bug.

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Consolidate git commit messages by enforcing the rules specified by How to Write a Git Commit Message carefully
  • Don't leave dead code
  • Don't mention your name (loretta) in code
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
@jserv jserv changed the title [WIP] Parser: draft fix for struct compound literal init (#299) Sep 28, 2025
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
jserv

This comment was marked as duplicate.

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase the latest master branch.

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always minimize the necessary changes.

Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimize the necessary changes.

Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
Comment thread src/parser.c Outdated
@jserv jserv changed the title Parser: fix for struct compound literal initialization (#299) Oct 5, 2025

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Investigated the bug where compound literals like
(struct point){10, 20, 30} produced shifted field values
(e.g., x got garbage, y=10, z=20). Added a temporary fix
by adjusting the source address +4 bytes in parser.c.

Tested with simple cases: x=10, y=20, z=30. This is an
experimental patch; needs review for design correctness.

Refs: sysprog21#299
Previously, array compound literals such as (int[]){100, 200, 300}
failed with an "Unexpected token" error. Struct compound literals
worked correctly, but array syntax [] was unhandled in the parser.

This change adds proper array compound literal handling, including
scalar and pointer contexts. In scalar context, the literal returns
its first element (e.g. int x = (int[]){100,200}; yields 100). In
pointer context, it allocates and initializes backing storage and
returns the address (e.g. int *p = (int[]){100,200};). Arithmetic
expressions using literals (e.g. 50 + (int[]){100}) also evaluate
correctly.

Additional fixes include:
- Consume missing '{' token for int/char compound literals
- Add return statements to prevent control flow fall-through
- Prevent segfaults in pointer assignments by allocating memory

As a result, shecc now supports array compound literals alongside
existing struct compound literals, improving C99 compatibility and
preserving self-hosting capability.

Known limitations remain: designated initializers and complex
expression combinations are still unsupported.
@lorettayao lorettayao changed the title Parser: fix for struct compound literal initialization Oct 6, 2025
@lorettayao
lorettayao force-pushed the fix-compound-literals branch from c215684 to 6c8154b Compare October 6, 2025 08:42

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check https://github.com/sysprog21/shecc/blob/master/CONTRIBUTING.md carefully and indent C source files with clang-format version 18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants