From af33f6e3a6b97fadf95c9e8fc4005a17a9d6581b Mon Sep 17 00:00:00 2001 From: Samuel Hunter Date: Mon, 7 Nov 2022 16:25:53 -0800 Subject: [PATCH] Add compiler macro FOREIGN-TYPE-SIZE The compiler macro is a first-pass that should fix most use cases without breakage. It checks to see if the foreign type is constant, and if so, expands into its foreign type size, falling back to the old form if an error is signaled. Close #343 --- src/early-types.lisp | 5 +++++ tests/misc.lisp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/early-types.lisp b/src/early-types.lisp index aa13c01..d5f21ee 100644 --- a/src/early-types.lisp +++ b/src/early-types.lisp @@ -164,6 +164,11 @@ Signals an error if FOREIGN-TYPE is undefined.")) (:documentation "Return the size in bytes of a foreign type.")) +(define-compiler-macro foreign-type-size (&whole form foreign-type) + (if (constant-form-p foreign-type) + (foreign-type-size (constant-form-value foreign-type)) + form)) + (defgeneric unparse-type (foreign-type) (:documentation "Unparse FOREIGN-TYPE to a type specification (symbol or list).")) diff --git a/tests/misc.lisp b/tests/misc.lisp index d46ca45..a57925e 100644 --- a/tests/misc.lisp +++ b/tests/misc.lisp @@ -130,3 +130,33 @@ (strcpy pointer "xpto") (return vector)))) #(120 112 116 111 0)) + +;;; Inline foreign-type-size + +(deftest foreign-type-size.inline.int + (eql (foreign-type-size :int) (locally (declare (notinline foreign-type-size)) (foreign-type-size :int))) + t) + +(deftest foreign-type-size.inline.uint32 + (eql (foreign-type-size :uint32) (locally (declare (notinline foreign-type-size)) (foreign-type-size :uint32))) + t) + +(deftest foreign-type-size.inline.ptrdiff + (eql (foreign-type-size :ptrdiff) (locally (declare (notinline foreign-type-size)) (foreign-type-size :ptrdiff))) + t) + +(deftest foreign-type-size.inline.size + (eql (foreign-type-size :size) (locally (declare (notinline foreign-type-size)) (foreign-type-size :size))) + t) + +(deftest foreign-type-size.inline.offset + (eql (foreign-type-size :offset) (locally (declare (notinline foreign-type-size)) (foreign-type-size :offset))) + t) + +(deftest foreign-type-size.inline.uintptr + (eql (foreign-type-size :uintptr) (locally (declare (notinline foreign-type-size)) (foreign-type-size :uintptr))) + t) + +(deftest foreign-type-size.inline.intptr + (eql (foreign-type-size :intptr) (locally (declare (notinline foreign-type-size)) (foreign-type-size :intptr))) + t) -- 2.11.4.GIT