From 6e150aaaf1b1bbb29e5b31673f7fba862385dc59 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Thu, 15 Oct 2026 14:48:25 +0300 Subject: [PATCH] take a name after a qualified type as the bound [int const N] read N as the type a trailing const was waiting for, and wrote int const N b[]; a qualifier waits for a name only when no type came before it. --- tests/codegen.scm | 9 ++++++++- types.scm | 17 +++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/codegen.scm b/tests/codegen.scm index 48cefd4..6ba436a 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -775,7 +775,14 @@ whitespace aside, THEN?" (test-assert "nor is an aggregate tag" (emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]")) (test-assert "nor one behind a pointer" - (emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]"))) + (emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]")) + ;; a qualifier waits for a type name only when none came before it + (test-assert "a name after a qualified type is a bound" + (emits? (in-fn "(var b [int const N])") "int const b[N]")) + (test-assert "after a qualified name too" + (emits? (in-fn "(var b [mytype const N])") "mytype const b[N]")) + (test-assert "but a qualifier alone still waits for the type" + (emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]"))) ;; `sizeof' takes a type or an expression, and `(* x)' is either. ;; What is in scope says which: a name C alone knows is a type diff --git a/types.scm b/types.scm index 2fa6098..182ddac 100644 --- a/types.scm +++ b/types.scm @@ -410,14 +410,14 @@ arglist)) ;;; The last element is a bound unless what precedes it is still -;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C -;;; spells a type with one name at most, so a name after a name, or -;;; after a grouped type, can only be the bound: +;;; waiting for a type name -- a `*', a tag's keyword, a qualifier with +;;; no type before it. C spells a type with one name at most, so a name +;;; after a name, or after a grouped type, can only be the bound: ;;; ;;; (¤ int 4) four of int (¤ unsigned int) unsized ;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized ;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized -;;; (¤ * size-t) unsized +;;; (¤ int const N) N of const int (¤ * size-t) unsized ;;; ;;; TYPE is the whole `(¤ ...)' form. (define (array-bound? type) @@ -427,8 +427,13 @@ (cond ((not (symbol? bound)) #t) ((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f) - (else (not (or (memq preceding '(* struct union enum)) - (memq preceding +c-qualifiers+)))))))) + ((memq preceding '(* struct union enum)) #f) + ((memq preceding +c-qualifiers+) + (any (lambda (word) + (not (or (memq word '(* struct union enum)) + (memq word +c-qualifiers+)))) + (drop-right (cdr type) 2))) + (else #t))))) ;;; What one element of a written array type is: ;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)