Support parsing and enforcing struct alignment and packed attributes in C header import
- Type: feature
- Status: done
- Track: C (C frontend)
- Owner: —
- Opened: 2026-06-28
Motivation
Currently, the C lexer (compiler/clexer.inc) extracts structural layout attribute modifiers such as __attribute__((packed)) and __attribute__((aligned)) and sets simple binary flags CAttrPacked and CAttrAligned. However:
- The compiler does not parse the exact alignment values (such as
aligned(16)oraligned(32)). - It does not apply these modifiers during layout calculations for structures whose fields are read or written directly in Pascal.
- This creates a risk of structural field drift and memory corruption when interoping with C structures that have custom byte alignments or are packed.
Scope
- Attribute Value Parsing:
- Update
compiler/clexer.incto extract the alignment byte boundary integer $N$ fromaligned(N). - Store $N$ in the parsed symbol metadata or structural definition in
compiler/symtab.inc.
- Update
- Structure Layout Enforcer:
- Update structure layout calculations (
compiler/parser.incor related struct offset generators) to respectpacked(offset alignment = 1) andaligned(N)(pad fields or struct size to multiples of $N$). - Ensure these alignments match the host platform's C compiler layout conventions.
- Update structure layout calculations (
Acceptance
- A structure defined with
__attribute__((packed))or__attribute__((aligned(16)))has identical field offsets and total size in Pascal and C. - Tests (e.g.
test/test_c_packed_aligned.pas) verify that field offsets and total sizes match the compiled C counterparts.
Log
- 2026-06-28 — ticket opened.
- 2026-06-29 — implemented
aligned(N)value capture in the C lexer and appliedpacked/aligned(N)in C struct layout.test/test_c_packed_aligned.pasnow verifies concrete field offsets and sizes for normal, packed, field-aligned, and type-aligned C structs. Self-host fixedpoint passes viamake compiler/pascal26.