diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 13:56:44 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 13:56:44 +0000 |
| commit | 312a19c781da1c31ff1e3dc71c319663a8be9148 (patch) | |
| tree | 80d63213c0d11b1fcf3d67ef6e51289fe4b5096e /tests | |
| parent | 5eccd90b47b327917ad0e5858c79ca8fab27cd74 (diff) | |
feat(codegen): accept pointer types to any C type in EXTERN
A pseudocode pointer type now counts as a C type, in EXTERN signatures
and as a C-struct field, when it finally points to a non-void C type or a
C struct, as in TYPE PInt = ^C_INT. A pointer cycle such as TYPE P = ^P
and a pointer to a pseudocode type stay rejected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i/extern-c-struct/expected.txt | 2 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/lib.c | 8 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/notes.md | 2 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/program.cps | 21 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/case.json | 4 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/notes.md | 2 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/program.cps | 2 |
7 files changed, 38 insertions, 3 deletions
diff --git a/tests/i/extern-c-struct/expected.txt b/tests/i/extern-c-struct/expected.txt index dee8467..816b4e3 100644 --- a/tests/i/extern-c-struct/expected.txt +++ b/tests/i/extern-c-struct/expected.txt @@ -2,3 +2,5 @@ S 4.500000 5 G 0.500000 7 9 14 +3 +G 9 diff --git a/tests/i/extern-c-struct/lib.c b/tests/i/extern-c-struct/lib.c index 65080f1..45bb30e 100644 --- a/tests/i/extern-c-struct/lib.c +++ b/tests/i/extern-c-struct/lib.c @@ -12,6 +12,14 @@ void scale_sample(struct Sample *s, int k) { s->value *= k; } struct Sample *shared_sample(void) { return &shared; } int shared_count(void) { return shared.count; } +struct Counter { + int *target; +}; + +void bump(int *p) { ++*p; } +void bump_counter(struct Counter *c) { ++*c->target; } +void get_shared(struct Sample **out) { *out = &shared; } + /* Each argument after n is a struct Sample *, read back with va_arg. */ int sum_counts(int n, ...) { va_list ap; diff --git a/tests/i/extern-c-struct/notes.md b/tests/i/extern-c-struct/notes.md index 519e835..494c510 100644 --- a/tests/i/extern-c-struct/notes.md +++ b/tests/i/extern-c-struct/notes.md @@ -4,4 +4,4 @@ Human review: pending. Expected output was independently traced by AI, not captu Basis: compiler extension (EXTERN "C", INCLUDE_OBJ of a user object); a record whose fields are all C types has the layout of the matching C struct. -Trace: the harness compiles `lib.c` with the host C compiler. Sample matches `struct Sample {char; double; int}`, including the padding after the char. S is 'S', 1.5, 5. scale_sample(^S, 3) multiplies the value through the pointer to 4.5 and leaves tag and count alone, so the first line is `S 4.500000 5`. shared_sample returns the address of the static struct in lib.c, initialised to 'G', 0.5, 7, giving `G 0.500000 7`. P^.Count <- 9 writes through that pointer into the C object, so shared_count() returns 9. sum_counts receives P and Q as variadic arguments; pointers are passed unchanged and read back with va_arg(ap, struct Sample *). P points to the shared struct, whose count is now 9, and Q points to S, whose count is 5, so the last line is 9 + 5 = 14. +Trace: the harness compiles `lib.c` with the host C compiler. Sample matches `struct Sample {char; double; int}`, including the padding after the char. S is 'S', 1.5, 5. scale_sample(^S, 3) multiplies the value through the pointer to 4.5 and leaves tag and count alone, so the first line is `S 4.500000 5`. shared_sample returns the address of the static struct in lib.c, initialised to 'G', 0.5, 7, giving `G 0.500000 7`. P^.Count <- 9 writes through that pointer into the C object, so shared_count() returns 9. sum_counts receives P and Q as variadic arguments; pointers are passed unchanged and read back with va_arg(ap, struct Sample *). P points to the shared struct, whose count is now 9, and Q points to S, whose count is 5, so the last line is 9 + 5 = 14. N starts at 1. bump(^N) receives a PInt, an int * in C, and increments N to 2. Counter matches `struct Counter {int *}`; bump_counter(^C) increments through C.Target, which points to N, giving 3. get_shared(^R) receives a struct Sample ** and stores the address of the shared struct into R, so R^ shows tag G and the count 9 written earlier: `G 9`. diff --git a/tests/i/extern-c-struct/program.cps b/tests/i/extern-c-struct/program.cps index bcb7af4..62370d3 100644 --- a/tests/i/extern-c-struct/program.cps +++ b/tests/i/extern-c-struct/program.cps @@ -1,4 +1,4 @@ -// Records whose fields are C types pass to C functions through pointer types. +// Records whose fields are C types, and pointer types to C types, pass to C functions. INCLUDE_OBJ "lib.o" TYPE Sample DECLARE Tag : C_CHAR @@ -6,13 +6,25 @@ TYPE Sample DECLARE Count : C_INT ENDTYPE TYPE PSample = ^Sample +TYPE PPSample = ^PSample +TYPE PInt = ^C_INT +TYPE Counter + DECLARE Target : PInt +ENDTYPE +TYPE PCounter = ^Counter EXTERN "C" EXTERN_PROCEDURE scale_sample(PSample, C_INT) EXTERN "C" EXTERN_FUNCTION shared_sample() RETURNS PSample EXTERN "C" EXTERN_FUNCTION shared_count() RETURNS C_INT EXTERN "C" EXTERN_FUNCTION sum_counts(C_INT, ...) RETURNS C_INT +EXTERN "C" EXTERN_PROCEDURE bump(PInt) +EXTERN "C" EXTERN_PROCEDURE bump_counter(PCounter) +EXTERN "C" EXTERN_PROCEDURE get_shared(PPSample) DECLARE S : Sample DECLARE P : PSample DECLARE Q : PSample +DECLARE R : PSample +DECLARE N : C_INT +DECLARE C : Counter S.Tag <- (C_CHAR)'S' S.Value <- (C_DOUBLE)1.5 S.Count <- (C_INT)5 @@ -24,3 +36,10 @@ P^.Count <- (C_INT)9 OUTPUT shared_count() Q <- ^S OUTPUT sum_counts((C_INT)2, P, Q) +N <- (C_INT)1 +CALL bump(^N) +C.Target <- ^N +CALL bump_counter(^C) +OUTPUT N +CALL get_shared(^R) +OUTPUT R^.Tag, " ", R^.Count diff --git a/tests/iii/extern-struct-rejected/case.json b/tests/iii/extern-struct-rejected/case.json index cd7a3dd..905d5ad 100644 --- a/tests/iii/extern-struct-rejected/case.json +++ b/tests/iii/extern-struct-rejected/case.json @@ -15,6 +15,10 @@ { "reason": "Variadic\\ argument\\ 2\\ to\\ printf\\ uses\\ record\\ Person,\\ which\\ is\\ not\\ a\\ C\\ struct:\\ field\\ Name\\ of\\ Person\\ has\\ type\\ STRING", "line": 13 + }, + { + "reason": "EXTERN\\ parameter\\ 1\\ of\\ srand\\ must\\ be\\ a\\ non\\-void\\ C\\ type\\ \\(got\\ PInteger\\)", + "line": 15 } ] } diff --git a/tests/iii/extern-struct-rejected/notes.md b/tests/iii/extern-struct-rejected/notes.md index 650b420..40e3ab8 100644 --- a/tests/iii/extern-struct-rejected/notes.md +++ b/tests/iii/extern-struct-rejected/notes.md @@ -1,5 +1,5 @@ # extern-struct-rejected -Compiler extension: an EXTERN signature accepts a record, through a pointer type or by value, only as a C struct (every field a C type, a C struct or a pointer type to one). Person has a STRING field, so a pointer to it, Person as a by-value parameter, as a by-value result and as a by-value variadic argument are all rejected and the field is named. +Compiler extension: an EXTERN signature accepts a record, through a pointer type or by value, only as a C struct (every field a C type, a C struct or a pointer type to either). Person has a STRING field, so a pointer to it, Person as a by-value parameter, as a by-value result and as a by-value variadic argument are all rejected and the field is named. A pointer type counts as a C type only when its pointee does, so a pointer to INTEGER is rejected too. Require exit 1 and each cause on its EXTERN or call line. diff --git a/tests/iii/extern-struct-rejected/program.cps b/tests/iii/extern-struct-rejected/program.cps index ced6fee..80f965f 100644 --- a/tests/iii/extern-struct-rejected/program.cps +++ b/tests/iii/extern-struct-rejected/program.cps @@ -11,3 +11,5 @@ EXTERN "C" EXTERN_FUNCTION atoi(C_STRING) RETURNS Person EXTERN "C" EXTERN_FUNCTION printf(C_STRING, ...) RETURNS C_INT DECLARE Someone : Person OUTPUT printf((C_STRING)"%d", Someone) +TYPE PInteger = ^INTEGER +EXTERN "C" EXTERN_PROCEDURE srand(PInteger) |
