diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 13:27:15 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 13:27:15 +0000 |
| commit | 4a70e44f06e637762eec0fd875eb94b84c3f4037 (patch) | |
| tree | b2771f76e3da335276a8c3e96ce70c2cfe99e088 /tests | |
| parent | eaad9c34403f05372c1544d7ef522c847dfea0ba (diff) | |
feat(codegen): accept pointers to C-struct records in EXTERN signatures
A TYPE record whose fields are all C types, C structs or pointer types to
C structs now crosses the EXTERN "C" boundary through a pointer type.
Records by value are rejected for now, and a pointer to a record with a
non-C field names that field.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i/extern-c-struct/case.json | 5 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/expected.txt | 3 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/lib.c | 11 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/notes.md | 7 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/program.cps | 22 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/case.json | 12 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/notes.md | 5 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/program.cps | 14 |
8 files changed, 79 insertions, 0 deletions
diff --git a/tests/i/extern-c-struct/case.json b/tests/i/extern-c-struct/case.json new file mode 100644 index 0000000..1ce5970 --- /dev/null +++ b/tests/i/extern-c-struct/case.json @@ -0,0 +1,5 @@ +{ + "objects": [ + {"source": "lib.c", "output": "lib.o"} + ] +} diff --git a/tests/i/extern-c-struct/expected.txt b/tests/i/extern-c-struct/expected.txt new file mode 100644 index 0000000..62e50ed --- /dev/null +++ b/tests/i/extern-c-struct/expected.txt @@ -0,0 +1,3 @@ +S 4.500000 5 +G 0.500000 7 +9 diff --git a/tests/i/extern-c-struct/lib.c b/tests/i/extern-c-struct/lib.c new file mode 100644 index 0000000..39380ec --- /dev/null +++ b/tests/i/extern-c-struct/lib.c @@ -0,0 +1,11 @@ +struct Sample { + char tag; + double value; + int count; +}; + +static struct Sample shared = {'G', 0.5, 7}; + +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; } diff --git a/tests/i/extern-c-struct/notes.md b/tests/i/extern-c-struct/notes.md new file mode 100644 index 0000000..1a0d215 --- /dev/null +++ b/tests/i/extern-c-struct/notes.md @@ -0,0 +1,7 @@ +# extern-c-struct + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +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. diff --git a/tests/i/extern-c-struct/program.cps b/tests/i/extern-c-struct/program.cps new file mode 100644 index 0000000..1954d29 --- /dev/null +++ b/tests/i/extern-c-struct/program.cps @@ -0,0 +1,22 @@ +// Records whose fields are C types pass to C functions through pointer types. +INCLUDE_OBJ "lib.o" +TYPE Sample + DECLARE Tag : C_CHAR + DECLARE Value : C_DOUBLE + DECLARE Count : C_INT +ENDTYPE +TYPE PSample = ^Sample +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 +DECLARE S : Sample +DECLARE P : PSample +S.Tag <- (C_CHAR)'S' +S.Value <- (C_DOUBLE)1.5 +S.Count <- (C_INT)5 +CALL scale_sample(^S, (C_INT)3) +OUTPUT S.Tag, " ", S.Value, " ", S.Count +P <- shared_sample() +OUTPUT P^.Tag, " ", P^.Value, " ", P^.Count +P^.Count <- (C_INT)9 +OUTPUT shared_count() diff --git a/tests/iii/extern-struct-rejected/case.json b/tests/iii/extern-struct-rejected/case.json new file mode 100644 index 0000000..6a4483f --- /dev/null +++ b/tests/iii/extern-struct-rejected/case.json @@ -0,0 +1,12 @@ +{ + "errors": [ + { + "reason": "EXTERN\\ parameter\\ 1\\ of\\ puts\\ uses\\ record\\ Person,\\ which\\ is\\ not\\ a\\ C\\ struct:\\ field\\ Name\\ of\\ Person\\ has\\ type\\ STRING", + "line": 12 + }, + { + "reason": "EXTERN\\ parameter\\ 1\\ of\\ abs\\ cannot\\ be\\ record\\ Point\\ by\\ value;\\ use\\ a\\ pointer\\ type\\ to\\ it", + "line": 13 + } + ] +} diff --git a/tests/iii/extern-struct-rejected/notes.md b/tests/iii/extern-struct-rejected/notes.md new file mode 100644 index 0000000..f33ef66 --- /dev/null +++ b/tests/iii/extern-struct-rejected/notes.md @@ -0,0 +1,5 @@ +# extern-struct-rejected + +Compiler extension: an EXTERN signature accepts a record only as a C struct (every field a C type, a C struct or a pointer type to one) and, for now, only through a pointer type. Person has a STRING field, so a pointer to it is rejected and the field is named. Point is a C struct, but passing it by value is not supported yet. + +Require exit 1 and each cause on its EXTERN line. diff --git a/tests/iii/extern-struct-rejected/program.cps b/tests/iii/extern-struct-rejected/program.cps new file mode 100644 index 0000000..e7f38d1 --- /dev/null +++ b/tests/iii/extern-struct-rejected/program.cps @@ -0,0 +1,14 @@ +// Records cross the EXTERN boundary only as C structs through a pointer type. +INCLUDE_OBJ "__libc" +TYPE Person + DECLARE Name : STRING + DECLARE Age : C_INT +ENDTYPE +TYPE PPerson = ^Person +TYPE Point + DECLARE X : C_INT + DECLARE Y : C_INT +ENDTYPE +EXTERN "C" EXTERN_PROCEDURE puts(PPerson) +EXTERN "C" EXTERN_FUNCTION abs(Point) RETURNS C_INT +OUTPUT 1 |
