diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index a337250..fb87132 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -202,6 +202,9 @@ forms, and what remains." ;; name `->' itself being the threading macro (('%-> . rest) (cons '%-> (map walk-expr rest))) (('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 (('typedef type name) `(typedef ,(walk-type type) ,(atom-to-fmt-c name))) diff --git a/semen.scm b/semen.scm index 87423d7..bfcb44e 100644 --- a/semen.scm +++ b/semen.scm @@ -661,6 +661,14 @@ ((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 (cond ((method-head? (car form)) (walk-method-call form env)) @@ -763,6 +771,23 @@ (append (drop-right parts (length 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))'. (define (resolve-wildcard form env) (if (and (>= (length form) 3) (wildcard-type? (third form))) diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index c9ef004..e9ae69a 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -288,6 +288,7 @@ ((%comment) ((apply c-comment (cdr x)) st)) ((:) ((apply c-label (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)) ((%designate) ((apply c-designate (cdr x)) st)) ((+ - & * / % ! ~ ^ && < > <= >= == != << >> diff --git a/tests/codegen.scm b/tests/codegen.scm index 2d4e7e9..6155234 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -703,7 +703,29 @@ compiles." (test-assert "nor one behind a pointer" (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 (test-group "defines" (test-assert "an atom is left bare" diff --git a/tests/sex-programs/type-shapes.sex b/tests/sex-programs/type-shapes.sex index 38d1672..68446e6 100644 --- a/tests/sex-programs/type-shapes.sex +++ b/tests/sex-programs/type-shapes.sex @@ -7,7 +7,8 @@ "unsized of a pointer: 5" "named bound: 2 4 b" "unnamed parameters: 7 -1 2" - "typedefs: hi 4 2") + "typedefs: hi 4 2" + "sizeof a pointer type: 1 1") (return 0) ;;; 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 op unary twice) (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))