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

Refactor mkStructField #364

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
59 changes: 27 additions & 32 deletions hs-bindgen/src/HsBindgen/C/Fold/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -388,43 +388,38 @@ mkStructField relPath unit path current = do
extent <- liftIO $ HighLevel.clang_getCursorExtent relPath current
hasMacro <- gets $ containsMacroExpansion extent

if hasMacro then liftIO $ do

tokens <- HighLevel.clang_tokenize relPath unit (multiLocExpansion <$> extent)
case reparseWith reparseFieldDecl tokens of
Left err ->
fail $ "mkStructField: " ++ show err
Right (fieldType, fieldName) -> do
fieldOffset <- fromIntegral <$> clang_Cursor_getOffsetOfField current
unless (fieldOffset `mod` 8 == 0) $
fail "bit-fields not supported yet"

return $ Just $ Right StructField {
fieldName
, fieldOffset
, fieldType
, fieldSourceLoc
}

else do
fieldName <- CName <$> liftIO (clang_getCursorDisplayName current)
ty <- liftIO (clang_getCursorType current)

case fromSimpleEnum $ cxtKind ty of
Right CXType_IncompleteArray -> do
(fieldName, fieldType, isIncompleteArray) <- if hasMacro
then liftIO $ do
tokens <- HighLevel.clang_tokenize relPath unit (multiLocExpansion <$> extent)
case reparseWith reparseFieldDecl tokens of
Left err ->
fail $ "mkStructField: " ++ show err
Right (fieldType, fieldName) -> do
-- Note: macro definitions don't work with incomplete arrays
-- This is fine as reparseWith doesn't recognise array types atm.
return (fieldName, fieldType, False)

else do
fieldName <- CName <$> liftIO (clang_getCursorDisplayName current)
ty <- liftIO (clang_getCursorType current)
case fromSimpleEnum $ cxtKind ty of
Right CXType_IncompleteArray -> do
e <- liftIO $ clang_getArrayElementType ty
fieldType <- processTypeDeclRec relPath (DeclPathField fieldName path) unit e
fieldOffset <- fromIntegral <$> liftIO (clang_Cursor_getOffsetOfField current)
return (fieldName, fieldType, True)

return $ Just $ Left StructField{fieldName, fieldOffset, fieldType, fieldSourceLoc}
_ -> do
fieldType <- processTypeDeclRec relPath (DeclPathField fieldName path) unit ty
return (fieldName, fieldType, False)

_ -> do
fieldType <- processTypeDeclRec relPath (DeclPathField fieldName path) unit ty
fieldOffset <- fromIntegral <$> liftIO (clang_Cursor_getOffsetOfField current)
fieldOffset <- fromIntegral <$> liftIO (clang_Cursor_getOffsetOfField current)
unless (fieldOffset `mod` 8 == 0) $ fail "bit-fields not supported yet"

unless (fieldOffset `mod` 8 == 0) $ fail "bit-fields not supported yet"

return $ Just $ Right StructField{fieldName, fieldOffset, fieldType, fieldSourceLoc}
if isIncompleteArray
then do
return $ Just $ Left StructField{fieldName, fieldOffset, fieldType, fieldSourceLoc}
else do
return $ Just $ Right StructField{fieldName, fieldOffset, fieldType, fieldSourceLoc}

-- inner structs, there are two approaches:
-- * process eagerly
Expand Down
Loading