promote the prelude's integer types like C's own

u8 and i32 are typedefs of <stdint.h> names nothing parses, so they had
no rank: (+ a a) over two u8 200s was a u8 holding 144, and (+ n 1) over
an i32 could not be typed at all.
This commit is contained in:
2026-10-16 13:54:41 +03:00
parent 6661dd82b4
commit c7c1f155c3
3 changed files with 25 additions and 3 deletions

View File

@@ -11,6 +11,7 @@
"partial: 1 2.5"
"joined: 43.5 84 49 1"
"promoted: 200 60000 3705032704"
"prelude: 400 6"
"from elements: 4 9 0")
(return 0)
@@ -105,6 +106,14 @@
(var unpromoted _ (+ kept kept))
(printf "promoted: %d %d %u\n" narrow narrower unpromoted)
;; the prelude's widths are C's own types under other names, and
;; promote the way those do
(var b8 u8 200)
(var w32 i32 5)
(var bytes _ (+ b8 b8))
(var next _ (+ w32 1))
(printf "prelude: %d %d\n" bytes next)
;; a brace initializer has no type of its own, but its elements solve
;; the hole in the array type around it -- and the length stays as
;; written, whether or not every slot is initialized

View File

@@ -42,14 +42,17 @@
#f
(get-fields 't-color))
(add-typedef 't-u8 '(typedef t-u8 uint8-t))
(add-typedef 't-u8 '(typedef t-u8 gl-ubyte))
(test "a typedef resolves to its target"
'uint8-t
'gl-ubyte
(get-underlying-type 't-u8))
(add-typedef 't-byte '(typedef t-byte t-u8))
(test "and chains are followed to the end"
'uint8-t
'gl-ubyte
(get-underlying-type 't-byte))
(test "stdint.h's exact-width types are C's own"
'(unsigned char)
(get-underlying-type 'uint8-t))
(test "a struct is not a typedef"
#f
(get-underlying-type 't-point))

View File

@@ -125,6 +125,16 @@
(hash-table-set! +type-db+ name
(list 'typedef name (last (strip-pub form)))))
;;; <stdint.h>, which nothing parses, as far as arithmetic asks: the
;;; prelude's `u8' is a `uint8-t', so (+ a a) over two of them is an
;;; `int', promoted like any narrow type. The 64-bit pair is `long' on
;;; some hosts; it ranks above the narrower ones either way.
(for-each (lambda (entry) (add-typedef (car entry) (cons 'typedef entry)))
'((int8-t (signed char)) (uint8-t (unsigned char))
(int16-t short) (uint16-t (unsigned short))
(int32-t int) (uint32-t (unsigned int))
(int64-t (long long)) (uint64-t (unsigned long long))))
;;; (define name value ...) -- a C #define, kept so a macro can read a
;;; compile-time constant rather than re-parse the source.
(define (add-define name form)