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.
This commit is contained in:
2026-10-15 14:48:25 +03:00
parent f84010eda8
commit 6e150aaaf1
2 changed files with 19 additions and 7 deletions

View File

@@ -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

View File

@@ -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)