From 775251b17d30b5b438a6fe2f32ae4dad2405db13 Mon Sep 17 00:00:00 2001 From: Louis Xu Date: Sat, 23 Nov 2019 19:14:43 +0800 Subject: [PATCH] ffi: fix missing C function declarations when compiling --- src/cmp/cmpglobals.lsp | 1 + src/cmp/cmptop.lsp | 2 ++ src/lsp/ffi.lsp | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/src/cmp/cmpglobals.lsp b/src/cmp/cmpglobals.lsp index 5f0f9afed..000b224b6 100644 --- a/src/cmp/cmpglobals.lsp +++ b/src/cmp/cmpglobals.lsp @@ -265,6 +265,7 @@ lines are inserted, but the order is preserved") (defvar *global-funs* nil) ; holds { fun }* (defvar *use-c-global* nil) ; honor si::c-global declaration (defvar *global-cfuns-array* nil) ; holds { fun }* +(defvar *external-cfuns* '()) ; holds { external-c-funs }* (defvar *local-funs* nil) ; holds { fun }* (defvar *top-level-forms* nil) ; holds { top-level-form }* (defvar *make-forms* nil) ; holds { top-level-form }* diff --git a/src/cmp/cmptop.lsp b/src/cmp/cmptop.lsp index 23fa7fb18..2e0a6a5ed 100644 --- a/src/cmp/cmptop.lsp +++ b/src/cmp/cmptop.lsp @@ -124,6 +124,8 @@ (wt-nl-h "#ifdef __cplusplus") (wt-nl-h "extern \"C\" {") (wt-nl-h "#endif") + (format *compiler-output2* "~%~{~a~%~}" *external-cfuns*) + (setq *external-cfuns* '()) ;;; Initialization function. (let* ((*opened-c-braces* 0) (*aux-closure* nil) diff --git a/src/lsp/ffi.lsp b/src/lsp/ffi.lsp index d4e289e2c..53ad7d277 100644 --- a/src/lsp/ffi.lsp +++ b/src/lsp/ffi.lsp @@ -58,6 +58,9 @@ Defines a new foreign type." (cons (%convert-to-ffi-type (first type) context) (%convert-to-ffi-type (rest type) context)))) +(defun %convert-to-c-type (type) + (compiler::rep-type-c-name (compiler::rep-type-record type))) + (defmacro %align-data (data align) `(setf ,data (* (ceiling (/ ,data ,align)) ,align))) @@ -618,7 +621,9 @@ Declares a foreign function." (lisp-to-c-name name) (let* ((arguments (mapcar #'first args)) (arg-types (mapcar #'(lambda (type) (%convert-to-arg-type (second type))) args)) + (c-types (mapcar #'(lambda (type) (%convert-to-c-type (second type))) args)) (return-type (%convert-to-return-type returning)) + (c-return-type (%convert-to-c-type returning)) (nargs (length arguments)) (c-string (produce-function-call c-name nargs)) (casting-required (not (or (member return-type '(:void :cstring)) @@ -628,6 +633,11 @@ Declares a foreign function." ,c-string :one-liner t :side-effects t))) + (push (format nil "~A ~A(~{~a~^, ~});" + c-return-type + c-name + c-types) + compiler::*external-cfuns*) (when casting-required (setf inline-form `(si::foreign-data-recast ,inline-form -- GitLab