let sizeof take a type that looks like an expression
(* char) is a pointer type and (* p) a dereference, and only the names in scope tell them apart, so semen decides and marks a type for the writer. Before, both were walked as expressions: sizeof(*char). Fixes #52.
This commit is contained in:
@@ -202,6 +202,9 @@ forms, and what remains."
|
|||||||
;; name `->' itself being the threading macro
|
;; name `->' itself being the threading macro
|
||||||
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
||||||
(('var . _) (walk-var form))
|
(('var . _) (walk-var form))
|
||||||
|
;; semen has told a type from an expression; only it knows the names
|
||||||
|
;; in scope that make `(* p)' one or the other
|
||||||
|
(('sizeof ('%type type)) `(%sizeof-type ,(walk-type type)))
|
||||||
;; semen has already put the target first, the way C writes it
|
;; semen has already put the target first, the way C writes it
|
||||||
(('typedef type name)
|
(('typedef type name)
|
||||||
`(typedef ,(walk-type type) ,(atom-to-fmt-c name)))
|
`(typedef ,(walk-type type) ,(atom-to-fmt-c name)))
|
||||||
|
|||||||
25
semen.scm
25
semen.scm
@@ -661,6 +661,14 @@
|
|||||||
|
|
||||||
((dot-access) (walk-member-path form env))
|
((dot-access) (walk-member-path form env))
|
||||||
|
|
||||||
|
((sizeof)
|
||||||
|
(if (and (= 2 (length form)) (written-type? (second form) env))
|
||||||
|
(copy-form-source!
|
||||||
|
form
|
||||||
|
`(sizeof (%type ,(resolve-closure-types
|
||||||
|
(expand-type (second form) env)))))
|
||||||
|
(walk-parts form env)))
|
||||||
|
|
||||||
(else
|
(else
|
||||||
(cond
|
(cond
|
||||||
((method-head? (car form)) (walk-method-call form env))
|
((method-head? (car form)) (walk-method-call form env))
|
||||||
@@ -763,6 +771,23 @@
|
|||||||
(append (drop-right parts (length elements))
|
(append (drop-right parts (length elements))
|
||||||
(map (lambda (element) (walk-operand element env)) elements)))))
|
(map (lambda (element) (walk-operand element env)) elements)))))
|
||||||
|
|
||||||
|
;;; Is the operand of `sizeof' a type? `(* x)' and `(¤ x i)' are
|
||||||
|
;;; expressions when x is: a name in scope, or a list that is not
|
||||||
|
;;; itself a type. A name nothing declared is one only C knows of, and
|
||||||
|
;;; there that is a type:
|
||||||
|
;;;
|
||||||
|
;;; (* char) (* const char) (* size-t) (unsigned int) types
|
||||||
|
;;; (* p) (* (g)) (¤ xs 0) expressions
|
||||||
|
(define (written-type? form env)
|
||||||
|
(and (pair? form)
|
||||||
|
;; the length a dereference and a subscript have
|
||||||
|
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
|
||||||
|
(let ((operand (second form)))
|
||||||
|
(if (symbol? operand)
|
||||||
|
(not (or (lookup-name env operand) (get-name-type operand)))
|
||||||
|
(written-type? operand env)))
|
||||||
|
(type-head? form))))
|
||||||
|
|
||||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||||
(define (resolve-wildcard form env)
|
(define (resolve-wildcard form env)
|
||||||
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
||||||
|
|||||||
@@ -288,6 +288,7 @@
|
|||||||
((%comment) ((apply c-comment (cdr x)) st))
|
((%comment) ((apply c-comment (cdr x)) st))
|
||||||
((:) ((apply c-label (cdr x)) st))
|
((:) ((apply c-label (cdr x)) st))
|
||||||
((%cast) ((apply c-cast (cdr x)) st))
|
((%cast) ((apply c-cast (cdr x)) st))
|
||||||
|
((%sizeof-type) ((cat "sizeof(" (c-type (cadr x)) ")") st))
|
||||||
((%compound) ((apply c-compound (cdr x)) st))
|
((%compound) ((apply c-compound (cdr x)) st))
|
||||||
((%designate) ((apply c-designate (cdr x)) st))
|
((%designate) ((apply c-designate (cdr x)) st))
|
||||||
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
||||||
|
|||||||
@@ -703,7 +703,29 @@ compiles."
|
|||||||
(test-assert "nor one behind a pointer"
|
(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 define's body was pasted unwalked, and unparenthesised: `cast'
|
;; `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
|
||||||
|
(test-group "sizeof"
|
||||||
|
(test-assert "a pointer type"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* char)))") "sizeof(char *)"))
|
||||||
|
(test-assert "a qualified one"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* const char)))") "sizeof(const char *)"))
|
||||||
|
(test-assert "to a type only C knows of"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* size-t)))") "sizeof(size_t *)"))
|
||||||
|
(test-assert "a multi-word type"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (unsigned int)))") "sizeof(unsigned int)"))
|
||||||
|
(test-assert "an array type"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (¤ int 4)))") "sizeof(int [4])"))
|
||||||
|
(test-assert "a dereference of a local"
|
||||||
|
(emits? (in-fn "(var p (* int) 0) (var n size-t (sizeof (* p)))") "sizeof(*p)"))
|
||||||
|
(test-assert "of a parameter"
|
||||||
|
(emits? "(fn f ((p (* int))) void (var n size-t (sizeof (* p))))" "sizeof(*p)"))
|
||||||
|
(test-assert "of a call"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* (g))))") "sizeof(*g())"))
|
||||||
|
(test-assert "a subscript"
|
||||||
|
(emits? (in-fn "(var xs (¤ int 4)) (var n size-t (sizeof (¤ xs 0)))") "sizeof(xs[0])")))
|
||||||
|
|
||||||
|
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||||
;; stayed a call, and (* x D) regrouped with D's own operator
|
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||||
(test-group "defines"
|
(test-group "defines"
|
||||||
(test-assert "an atom is left bare"
|
(test-assert "an atom is left bare"
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
"unsized of a pointer: 5"
|
"unsized of a pointer: 5"
|
||||||
"named bound: 2 4 b"
|
"named bound: 2 4 b"
|
||||||
"unnamed parameters: 7 -1 2"
|
"unnamed parameters: 7 -1 2"
|
||||||
"typedefs: hi 4 2")
|
"typedefs: hi 4 2"
|
||||||
|
"sizeof a pointer type: 1 1")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Three questions about a written type that used to be answered in
|
;;; Three questions about a written type that used to be answered in
|
||||||
@@ -89,4 +90,9 @@
|
|||||||
(var v vec4 #(1.0 2.0 3.0 4.0))
|
(var v vec4 #(1.0 2.0 3.0 4.0))
|
||||||
(var op unary twice)
|
(var op unary twice)
|
||||||
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof (¤ v 0))) (op 1))
|
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof (¤ v 0))) (op 1))
|
||||||
|
|
||||||
|
;; `(* T)' is a type or a dereference, and only what is in scope says
|
||||||
|
(printf "sizeof a pointer type: %d %d\n"
|
||||||
|
(== (sizeof (* const char)) (sizeof greeting))
|
||||||
|
(== (sizeof (* greeting)) 1))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
Reference in New Issue
Block a user