Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum values #141

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hs-bindgen-libclang/cbits/clang_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ void wrap_disposeString(CXString* string);

CXString* wrap_malloc_TargetInfo_getTriple(CXTargetInfo Info);

static inline long long wrap_getEnumConstantDeclValue(CXCursor *C) {
return clang_getEnumConstantDeclValue(*C);
}

#endif
16 changes: 15 additions & 1 deletion hs-bindgen-libclang/src/HsBindgen/Clang/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ module HsBindgen.Clang.Core (
, clang_getTranslationUnitTargetInfo
, clang_TargetInfo_dispose
, clang_TargetInfo_getTriple
-- * Enumerations
, clang_getEnumConstantDeclValue
) where

import Control.Exception
Expand Down Expand Up @@ -864,7 +866,7 @@ data CXTypeLayoutException =
deriving Exception via CollectedBacktrace CXTypeLayoutException

{-------------------------------------------------------------------------------
Exceptions
Target Info
-------------------------------------------------------------------------------}

-- | An opaque type representing target information for a given translation
Expand All @@ -884,3 +886,15 @@ foreign import capi "clang_wrappers.h wrap_malloc_TargetInfo_getTriple"
clang_TargetInfo_getTriple :: CXTargetInfo -> IO ByteString
clang_TargetInfo_getTriple info =
packCXString =<< wrap_malloc_TargetInfo_getTriple info

{-------------------------------------------------------------------------------
Enums
-------------------------------------------------------------------------------}

foreign import capi unsafe "clang_wrappers.h wrap_getEnumConstantDeclValue"
wrap_getEnumConstantDeclValue :: CXCursor_ -> IO CLLong

clang_getEnumConstantDeclValue :: CXCursor -> IO CLLong
clang_getEnumConstantDeclValue cursor =
unwrapForeignPtr cursor $ \cursor' ->
wrap_getEnumConstantDeclValue cursor'
15 changes: 15 additions & 0 deletions hs-bindgen/examples/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,19 @@ enum first {
FIRST2
};

enum second {
SECOND_A = -1,
SECOND_B,
SECOND_C
};

enum same {
SAME_A = 1,
SAME_B = 1,
};

enum packad {
PACKED_A, PACKED_B, PACKED_C
} __attribute__((packed));

#endif /* ENUMS_H */
16 changes: 16 additions & 0 deletions hs-bindgen/fixtures/enums.dump.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
"first" :: "Enum"
"FIRST1" :: "Int"
"FIRST2" :: "Int"
"second" :: "Enum"
"SECOND_A" :: "Int"
_ :: "Int"
_ :: "Int"
"SECOND_B" :: "Int"
"SECOND_C" :: "Int"
"same" :: "Enum"
"SAME_A" :: "Int"
_ :: "Int"
"SAME_B" :: "Int"
_ :: "Int"
"packad" :: "Enum"
"packed" :: "Invalid"
"PACKED_A" :: "Int"
"PACKED_B" :: "Int"
"PACKED_C" :: "Int"
50 changes: 48 additions & 2 deletions hs-bindgen/fixtures/enums.tree-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,51 @@ WrapCHeader
enumSizeof = 4,
enumAlignment = 4,
enumValues = [
EnumValue "FIRST1",
EnumValue "FIRST2"]}])
EnumValue {
valueName = "FIRST1",
valueValue = 0},
EnumValue {
valueName = "FIRST2",
valueValue = 1}]},
DeclEnum
Enu {
enumTag = Just "second",
enumSizeof = 4,
enumAlignment = 4,
enumValues = [
EnumValue {
valueName = "SECOND_A",
valueValue = `-1`},
EnumValue {
valueName = "SECOND_B",
valueValue = 0},
EnumValue {
valueName = "SECOND_C",
valueValue = 1}]},
DeclEnum
Enu {
enumTag = Just "same",
enumSizeof = 4,
enumAlignment = 4,
enumValues = [
EnumValue {
valueName = "SAME_A",
valueValue = 1},
EnumValue {
valueName = "SAME_B",
valueValue = 1}]},
DeclEnum
Enu {
enumTag = Just "packad",
enumSizeof = 1,
enumAlignment = 1,
enumValues = [
EnumValue {
valueName = "PACKED_A",
valueValue = 0},
EnumValue {
valueName = "PACKED_B",
valueValue = 1},
EnumValue {
valueName = "PACKED_C",
valueValue = 2}]}])
4 changes: 2 additions & 2 deletions hs-bindgen/src/HsBindgen/C/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ data Enu = Enu {
deriving anyclass (PrettyVal)

data EnumValue = EnumValue {
valueName :: String
-- , valueValue :: Integer
valueName :: String
, valueValue :: Integer
}
deriving stock (Show, Eq, Generic)
deriving anyclass (PrettyVal)
Expand Down
3 changes: 2 additions & 1 deletion hs-bindgen/src/HsBindgen/C/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ foldEnumValues tracer current = do
case primType $ cxtKind cursorType of
Just _fieldType -> do
valueName <- decodeString <$> clang_getCursorDisplayName current
let field = C.EnumValue{valueName}
valueValue <- toInteger <$> clang_getEnumConstantDeclValue current
let field = C.EnumValue{valueName, valueValue}
return $ Continue (Just field)
_otherwise -> do
traceWith tracer Warning $ Skipping callStack (cxtKind cursorType)
Expand Down