Skip to content

c_import: full SDL.h imports — zero-parameter macro return types, D42 math builtins unwrapped, per-import overflow helpers, local anonymous unions (#1876 #1877 #1878 #1879) - #1896

Merged
ehartford merged 4 commits into
fix-sdkfrom
fix-sdl-import
Sep 29, 2026

Conversation

@ehartford

@ehartford ehartford commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Stack #1898, layer 12. Closes #1876, #1877, #1878, #1879. With these, use c_import("SDL3/SDL.h") (SDL 3.4.14) checks ok on Darwin. Found and filed on the way: #1882 (a c_import-origin function name captures a std bundle's own calls). Not yet verified on Windows: #1876 against the real <corecrt_math.h>. Gate green on the layer before folding. Draft until the stack battery is green and posted.

@ehartford
ehartford added this pull request to stack #1898 September 29, 2026 05:03
@ehartford ehartford changed the title fix sdl import c_import: full SDL.h imports — zero-parameter macro return types, D42 math builtins unwrapped, per-import overflow helpers, local anonymous unions (#1876 #1877 #1878 #1879) Sep 29, 2026
Eric Hartford added 4 commits September 29, 2026 14:40
…type from its body (#1879)

`#define SDL_Unsupported() SDL_SetError("...")` translated as
`unsafe fn SDL_Unsupported() -> i32`: ci_translate_macros ran
ci_infer_macro_return_type_from_expr only for a macro with parameters
(CImport.w, the `else if param_count > 0` arm), so a zero-parameter
macro kept the `i32` placeholder and the raw variadic's `bool` result
failed "return type mismatch". The inference is the same for both.

Fixes #1879
…ed in unsafe (#1876)

MSVC's <corecrt_math.h> inline wrappers (acosf: `return (float)acos((double)x)`)
translated as `return unsafe { acos(x as f64) }` and were refused as a block
with no unsafe operation: ci_migrate_preamble_extern_call_requires_unsafe
(CImport.w) counted every modeled libc name, libm included, as an unsafe
extern call. Since D42 those names are the compiler's math builtins — Sema
types the call itself and an `extern fn` of the name yields to it
(SemaCheck math_extern_yields) — so the call is never an unsafe operation.

One owner: ci_is_libm_fn now reads the MathBuiltins table Sema reads
(math_fn_lookup) instead of its own list, and the unsafe rule answers
false for any row.

Fixes #1876
…erflow names (#1877)

SDL_size_mul_check_overflow's `__builtin_mul_overflow(a, b, ret)` lowered
to a call of `__with_builtin_mul_overflow_u64` that nothing defined
("undefined variable"): build_overflow_builtin_call (CImport.w) named the
migrator's preamble helper, which only a migrated module's defs.w
defines; a header import has no preamble.

The import now records each helper an inline body names and defines it
once, at the end of its own translation (ci_render_overflow_helpers_needed,
the CiMigrate renderers parametrized on the helper's name). The import's
spelling is `__c_import_<op>_overflow_<ty>` / `__c_import_u128_mul_would_overflow`,
distinct from the bundles' `__with_builtin_*` / `u128_mul_would_overflow`:
a c_import-origin name is global and the .wo bundles' shared defs
(lib/std/re/defs.w) reach every compilation, so a c_import definition of
the bundle's name captured the bundle's own calls (MIR "body index map
mismatch"; filed as #1882).

Fixes #1877
…s a With type beside the function (#1878)

SDL_SwapFloat's `union { float f; Uint32 ui32; } swapper;` translated as
`var __local_swapper: c_void` and every field access failed: the bridge
names a record with no tag only at file scope, as a member, or behind a
parameter/return (name_file_scope_anon_records, ClangBridge.w), so a
body-local one had no name and translate_type_recursive printed `c_void`;
lower_decl_stmt_structural (CImport.w) then skipped the record declaration
sitting beside the VarDecl in the DeclStmt.

The bridge now walks every function definition's locals
(name_local_anon_records) and names the record `<fn>_<var>_anon` under the
same session table, so the local's type and the record agree. The body
lowering translates that record declaration with ci_translate_anon_record_cursor
and hoists it to module level beside the function (ci_take_body_hoisted_decls,
read by ci_translate_function and the migrator's emission); a record With
has no layout for is a loud omission of the function, never an opaque local.

Fixes #1878
@ehartford
ehartford marked this pull request as ready for review September 29, 2026 18:57
@ehartford
ehartford merged commit 7e19500 into main Sep 29, 2026
@ehartford
ehartford deleted the fix-sdl-import branch September 29, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant