forked from alex-eg/sex
tell an array's bound by the shape of its type
Whether a trailing name was the bound hinged on the word before it being a typedef Sex had seen, which a header's pthread_t never is, and a grouped (* T) never is either: [pthread-t N] became a two-word type. Fixes #56, #60.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
"through a pointer: 55"
|
"through a pointer: 55"
|
||||||
"unsized of a typedef: 1 2"
|
"unsized of a typedef: 1 2"
|
||||||
"unsized of a pointer: 5"
|
"unsized of a pointer: 5"
|
||||||
|
"named bound: 2 4 b"
|
||||||
"unnamed parameters: 7 -1 2")
|
"unnamed parameters: 7 -1 2")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
@@ -22,6 +23,8 @@
|
|||||||
|
|
||||||
(typedef small int)
|
(typedef small int)
|
||||||
|
|
||||||
|
(define CAP 2)
|
||||||
|
|
||||||
;;; a parameter that names nothing is a type, however many words it
|
;;; a parameter that names nothing is a type, however many words it
|
||||||
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
|
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
|
||||||
;;; `int'
|
;;; `int'
|
||||||
@@ -65,6 +68,13 @@
|
|||||||
(var w _ (¤ sizes 0))
|
(var w _ (¤ sizes 0))
|
||||||
(printf "unsized of a pointer: %d\n" (cast (* w) int))
|
(printf "unsized of a pointer: %d\n" (cast (* w) int))
|
||||||
|
|
||||||
|
;; ...but after a whole type, a name is: `size-t' is a typedef only C
|
||||||
|
;; knows of, and `(* const char)' a type grouped into one element
|
||||||
|
(var ws (¤ size-t CAP) #(3 4))
|
||||||
|
(var gs (¤ (* const char) CAP) #("a" "b"))
|
||||||
|
(printf "named bound: %zu %zu %s\n"
|
||||||
|
(/ (sizeof ws) (sizeof (¤ ws 0))) (¤ ws 1) (¤ gs 1))
|
||||||
|
|
||||||
;; the same question in type position: `(fn ((unsigned int)) int)'
|
;; the same question in type position: `(fn ((unsigned int)) int)'
|
||||||
;; takes one parameter, not two
|
;; takes one parameter, not two
|
||||||
(var fp (fn ((unsigned int)) int) width)
|
(var fp (fn ((unsigned int)) int) width)
|
||||||
|
|||||||
@@ -42,7 +42,6 @@
|
|||||||
|
|
||||||
type-head?
|
type-head?
|
||||||
named-arg?
|
named-arg?
|
||||||
typedef-name?
|
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type)
|
||||||
"../types.scm")
|
"../types.scm")
|
||||||
|
|||||||
@@ -217,4 +217,24 @@
|
|||||||
(get-name-type 't-never-declared))
|
(get-name-type 't-never-declared))
|
||||||
(test "nor a return type"
|
(test "nor a return type"
|
||||||
#f
|
#f
|
||||||
(get-return-type 't-never-declared)))
|
(get-return-type 't-never-declared))
|
||||||
|
|
||||||
|
;; Where an array's element type ends is a matter of shape, not of
|
||||||
|
;; what the database happens to hold: a typedef from a C header is
|
||||||
|
;; never in it
|
||||||
|
(test-group "array bounds"
|
||||||
|
(test-assert "a number" (array-bound? '(¤ int 4)))
|
||||||
|
(test-assert "a name after a specifier" (array-bound? '(¤ int N)))
|
||||||
|
(test-assert "a name after a type name unknown to Sex"
|
||||||
|
(array-bound? '(¤ pthread-t N)))
|
||||||
|
(test-assert "a name after a grouped type"
|
||||||
|
(array-bound? '(¤ (* t-point) N)))
|
||||||
|
(test-assert "a name after a tag" (array-bound? '(¤ struct t-point N)))
|
||||||
|
(test-assert "not a lone type" (not (array-bound? '(¤ pthread-t))))
|
||||||
|
(test-assert "not a specifier" (not (array-bound? '(¤ unsigned int))))
|
||||||
|
(test-assert "not a qualified name" (not (array-bound? '(¤ const t))))
|
||||||
|
(test-assert "not a pointer's target" (not (array-bound? '(¤ * size-t))))
|
||||||
|
(test-assert "not a tag" (not (array-bound? '(¤ struct t-point))))
|
||||||
|
(test "the element of a grouped type is the group"
|
||||||
|
'(* t-point)
|
||||||
|
(array-element-type '(¤ (* t-point) N)))))
|
||||||
|
|||||||
@@ -42,7 +42,6 @@
|
|||||||
|
|
||||||
type-head?
|
type-head?
|
||||||
named-arg?
|
named-arg?
|
||||||
typedef-name?
|
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type)
|
||||||
"types.scm")
|
"types.scm")
|
||||||
|
|||||||
30
types.scm
30
types.scm
@@ -388,22 +388,15 @@
|
|||||||
(pair? (cdr arg)) ; 1 element args are always type
|
(pair? (cdr arg)) ; 1 element args are always type
|
||||||
(not (type-head? arg))))
|
(not (type-head? arg))))
|
||||||
|
|
||||||
;;; A typedef and a `define' share +type-db+; only the typedef is part
|
;;; The last element is a bound unless what precedes it is still
|
||||||
;;; of a type:
|
;;; 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:
|
||||||
;;;
|
;;;
|
||||||
;;; (typedef small int) -> (¤ small N) is N of small
|
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
||||||
;;; (define CAP 4) -> (¤ int CAP) is CAP of int
|
;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized
|
||||||
(define (typedef-name? name)
|
;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized
|
||||||
(let ((info (and (symbol? name) (get-type-info name))))
|
;;; (¤ * size-t) unsized
|
||||||
(and info (memq (car info) '(typedef struct union enum)) #t)))
|
|
||||||
|
|
||||||
;;; The last element is a bound only where what precedes it already
|
|
||||||
;;; spells a whole type -- a specifier, a tag after its keyword, or a
|
|
||||||
;;; typedef we have seen declared:
|
|
||||||
;;;
|
|
||||||
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
|
||||||
;;; (¤ mytype CAP) CAP of mytype (¤ struct point) unsized
|
|
||||||
;;; (¤ const mytype) unsized (¤ * size-t) unsized
|
|
||||||
;;;
|
;;;
|
||||||
;;; TYPE is the whole `(¤ ...)' form.
|
;;; TYPE is the whole `(¤ ...)' form.
|
||||||
(define (array-bound? type)
|
(define (array-bound? type)
|
||||||
@@ -413,11 +406,8 @@
|
|||||||
(cond
|
(cond
|
||||||
((not (symbol? bound)) #t)
|
((not (symbol? bound)) #t)
|
||||||
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
||||||
((memq preceding +c-specifiers+) #t)
|
(else (not (or (memq preceding '(* struct union enum))
|
||||||
;; a tag always follows its keyword, so `(¤ * struct tt)' ends
|
(memq preceding +c-qualifiers+))))))))
|
||||||
;; in a name belonging to the type
|
|
||||||
((memq preceding '(struct union enum)) #f)
|
|
||||||
(else (typedef-name? preceding))))))
|
|
||||||
|
|
||||||
;;; What one element of a written array type is:
|
;;; What one element of a written array type is:
|
||||||
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)
|
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)
|
||||||
|
|||||||
Reference in New Issue
Block a user