Top-level functions should be forward-declared automatically #78

Open
opened 2026-11-04 14:45:39 +01:00 by alex-eg · 0 comments
Owner

Calling a function before its definition currently depends on writing the prototype by hand. Without that, sexc emits the call and the definition in source order, and the C compiler rejects the result.

Minimal reproduction:

(include stdio.h)

(fn f () int
  (return (g)))

(fn g () int
  (return 42))

(pub fn main () int
  (printf "%d\n" (f))
  (return 0))

sexc -C emits f before g:

static int f (void) {
    return g();
}

static int g (void) {
    return 42;
}

and compiling fails:

error: implicit declaration of function 'g'
error: static declaration of 'g' follows non-static declaration

Mutual recursion fails the same way, and the failure is worse for non-int returns because C assumes an implicit int signature.

Today there are two manual workarounds:

  1. Repeat the header as a bodyless prototype:
(fn g () int)
  1. Use extern fn g () int, which then requires the later definition to be pub so the linkage does not conflict.

Both duplicate the signature and have to be kept in sync. The bodyless form at least keeps the function static, but the order dependence is still there.

This is inconsistent with ops: process-ops already registers every method and emits a prototype when one method calls another written below it (called-before? plus sex-fn-prototype). Top-level fn/pub fn are the case that was left manual.

Proposal: collect the unit's top-level function definitions and paste their prototypes before the processed bodies, automatically.

  • Private fn definitions get a static prototype, preserving internal linkage.
  • pub fn definitions get a non-static prototype.
  • Source order then stops mattering, and mutual recursion works.
  • This is essentially the "module imports itself" idea: build the unit's own function interface, including private declarations, and paste it at the top before the bodies.

Implementation notes:

  • Do it after top-level macro expansion, since a macro can generate a function definition.
  • Avoid emitting a duplicate prototype when the user already wrote a bodyless fn, an extern fn, or when the function comes from ops/realize (those already register/forward-declare).
  • Keep pub linkage external and private linkage static.
  • Preserve source locations on the generated prototypes so diagnostics still point at the definition.
  • The public interface seen by importers should still contain only pub declarations.

Acceptance:

  • a forward call and mutual recursion compile and run;
  • non-int return types work;
  • private functions stay static, pub functions stay external;
  • no duplicate/conflicting prototypes with existing manual declarations;
  • existing make check suite passes.
Calling a function before its definition currently depends on writing the prototype by hand. Without that, `sexc` emits the call and the definition in source order, and the C compiler rejects the result. Minimal reproduction: ```scheme (include stdio.h) (fn f () int (return (g))) (fn g () int (return 42)) (pub fn main () int (printf "%d\n" (f)) (return 0)) ``` `sexc -C` emits `f` before `g`: ```c static int f (void) { return g(); } static int g (void) { return 42; } ``` and compiling fails: ```text error: implicit declaration of function 'g' error: static declaration of 'g' follows non-static declaration ``` Mutual recursion fails the same way, and the failure is worse for non-`int` returns because C assumes an implicit `int` signature. Today there are two manual workarounds: 1. Repeat the header as a bodyless prototype: ```scheme (fn g () int) ``` 2. Use `extern fn g () int`, which then requires the later definition to be `pub` so the linkage does not conflict. Both duplicate the signature and have to be kept in sync. The bodyless form at least keeps the function `static`, but the order dependence is still there. This is inconsistent with `ops`: `process-ops` already registers every method and emits a prototype when one method calls another written below it (`called-before?` plus `sex-fn-prototype`). Top-level `fn`/`pub fn` are the case that was left manual. Proposal: collect the unit's top-level function definitions and paste their prototypes before the processed bodies, automatically. - Private `fn` definitions get a `static` prototype, preserving internal linkage. - `pub fn` definitions get a non-static prototype. - Source order then stops mattering, and mutual recursion works. - This is essentially the "module imports itself" idea: build the unit's own function interface, including private declarations, and paste it at the top before the bodies. Implementation notes: - Do it after top-level macro expansion, since a macro can generate a function definition. - Avoid emitting a duplicate prototype when the user already wrote a bodyless `fn`, an `extern fn`, or when the function comes from `ops`/`realize` (those already register/forward-declare). - Keep `pub` linkage external and private linkage `static`. - Preserve source locations on the generated prototypes so diagnostics still point at the definition. - The public interface seen by importers should still contain only `pub` declarations. Acceptance: - a forward call and mutual recursion compile and run; - non-`int` return types work; - private functions stay `static`, `pub` functions stay external; - no duplicate/conflicting prototypes with existing manual declarations; - existing `make check` suite passes.
alex-eg added the feature label 2026-11-04 14:45:39 +01:00
Sign in to join this conversation.