Skip to content

Commit

Permalink
WIP: Basic libclang bindings
Browse files Browse the repository at this point in the history
This adds all bindings required to translate the `libclang` tutorial to
Haskell.
  • Loading branch information
edsko committed Aug 6, 2024
1 parent 83e323f commit 87715a7
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
dist-newstyle/
unversioned
cabal.project.local
.vscode/
25 changes: 25 additions & 0 deletions hs-bindgen/cbits/clang_wrappers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdlib.h>
#include <string.h>

#include <clang-c/Index.h>

#include "clang_wrappers.h"

/**
* Cursor manipulations
*/

CXCursor* wrap_malloc_getTranslationUnitCursor (CXTranslationUnit unit) {
CXCursor result = clang_getTranslationUnitCursor(unit);
CXCursor *wrapped = malloc(sizeof(CXCursor));
memcpy(wrapped, &result, sizeof(CXCursor));
return wrapped;
}

/**
* Traversing the AST with cursors
*/

unsigned wrap_visitChildren(CXCursor* parent, CXCursorVisitor visitor, CXClientData client_data) {
return clang_visitChildren(*parent, visitor, client_data);
}
19 changes: 19 additions & 0 deletions hs-bindgen/cbits/clang_wrappers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Wrappers for clang functions that take structs, or return them, by value.
*
* For functions that return structs by value, we instead allocate memory and
* return a pointer. It is the responsibility of the caller to attach a
* finalizer to these pointers; to remind the caller to do so, these functions
* are prefixed with @wrap_malloc_@; the other functions are prefixed with
* @wrap_@ (in both cases this prefixes replaces the @clang_@ prefix).
*/

#include <clang-c/Index.h>

// Cursor manipulations

CXCursor* wrap_malloc_getTranslationUnitCursor (CXTranslationUnit unit);

// Traversing the AST with cursors

unsigned wrap_visitChildren(CXCursor* parent, CXCursorVisitor visitor, CXClientData);
18 changes: 18 additions & 0 deletions hs-bindgen/hs-bindgen.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ category: Development
build-type: Simple
synopsis: Generate Haskell bindings from C headers
extra-doc-files: CHANGELOG.md
extra-source-files: cbits/*.c
cbits/*.h
tested-with: , GHC==9.2.8
, GHC==9.4.8
, GHC==9.6.6
Expand All @@ -29,10 +31,14 @@ common lang
default-extensions:
DerivingStrategies
DisambiguateRecordFields
RoleAnnotations
TypeFamilies

library
import:
lang
other-extensions:
CApiFFI
exposed-modules:
HsBindgen
HsBindgen.Preprocessor
Expand All @@ -42,6 +48,9 @@ library
HsBindgen.Annotation
HsBindgen.C
HsBindgen.Clang
HsBindgen.Clang.LowLevel
HsBindgen.Clang.Tutorial
HsBindgen.Clang.Util
HsBindgen.Spec.Resolved
hs-source-dirs:
src
Expand All @@ -50,10 +59,19 @@ library
, haskell-src-meta >= 0.8 && < 0.9
, template-haskell >= 2.18 && < 2.23

-- @libclang@
--
-- TODO: <https://github.com/llvm/llvm-project/issues/9777>
-- It seems @libclang@ does not support @pkg-config@.

extra-libraries:
clang
c-sources:
cbits/clang_wrappers.c
include-dirs:
cbits
cc-options:
-Wall

executable hs-bindgen
import:
Expand Down
7 changes: 7 additions & 0 deletions hs-bindgen/src/HsBindgen/Annotation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ module HsBindgen.Annotation (
-------------------------------------------------------------------------------}

-- | Syntax tree annotation
--
-- TODO: <https://github.com/well-typed/hs-bindgen/issues/23>
-- We should use this explain tool decisions (when generating high-level API).
--
-- TODO: <https://github.com/well-typed/hs-bindgen/issues/74>
-- We should reference the relevant part of the C header here (including line
-- numbers).
data Ann = Ann {
}

Expand Down
5 changes: 1 addition & 4 deletions hs-bindgen/src/HsBindgen/Clang.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- | Bindings to @libclang@
--
-- The goal of this module is not to be a complete set of bindings for all of
-- @libclang@, but rather only to the parts that we need.
-- | High-level bindings to @libclang@
--
-- Intended for qualified import.
--
Expand Down
Loading

0 comments on commit 87715a7

Please sign in to comment.