Top-level functions should be forward-declared automatically #78
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Calling a function before its definition currently depends on writing the prototype by hand. Without that,
sexcemits the call and the definition in source order, and the C compiler rejects the result.Minimal reproduction:
sexc -Cemitsfbeforeg:and compiling fails:
Mutual recursion fails the same way, and the failure is worse for non-
intreturns because C assumes an implicitintsignature.Today there are two manual workarounds:
extern fn g () int, which then requires the later definition to bepubso 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-opsalready registers every method and emits a prototype when one method calls another written below it (called-before?plussex-fn-prototype). Top-levelfn/pub fnare the case that was left manual.Proposal: collect the unit's top-level function definitions and paste their prototypes before the processed bodies, automatically.
fndefinitions get astaticprototype, preserving internal linkage.pub fndefinitions get a non-static prototype.Implementation notes:
fn, anextern fn, or when the function comes fromops/realize(those already register/forward-declare).publinkage external and private linkagestatic.pubdeclarations.Acceptance:
intreturn types work;static,pubfunctions stay external;make checksuite passes.