Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
a -> notSupportedYet (a $> ()) ni
a@(C.CStaticAssert _ _ ni) -> notSupportedYet (a $> ()) ni
findStructsInSpecifier = \case
C.CTypeSpec (C.CSUType cu _) -> toStruct cu
_ow -> mempty
data Function = Function
{ funName :: !C.Ident
, funParams :: !(Maybe [Maybe CType])
, funReturns :: !(Maybe CType)
, funIsStatic :: !Bool
, funSize :: !Int
, funPosition :: !C.Position
}
deriving (Show, Eq)
findFunctions
:: (Monoid m)
=> (Function -> m)
-> Context
-> C.CExternalDeclaration C.NodeInfo
-> m
findFunctions inject ctx = \case
C.CFDefExt (C.CFunDef spec declr [] _ ni) ->
findFunctionsInDeclarator ni spec declr
-- # for now let's not anlyse function declarations.
C.CFDefExt def@(C.CFunDef{}) ->
notSupportedYet (void def) def
C.CDeclExt (C.CDecl spec items ni) -> flip foldMap items \case
C.CDeclarationItem declr Nothing Nothing ->
findFunctionsInDeclarator ni spec declr
_ow -> mempty
C.CDeclExt a@(C.CStaticAssert{}) ->
notSupportedYet (void a) a
C.CAsmExt _ _ -> mempty
where
findFunctionsInDeclarator ni spec = \case
(C.CDeclr mid (functionParameters -> Just (params, change)) Nothing [] _) -> case mid of
Just funName -> inject Function{..}
where
funParams = params <&> fmap (Just . snd) . concatMap (declarations ctx)
funReturns = change $ case ctype ctx spec of
CTAny -> Nothing
t -> Just t
funIsStatic = any (\case (C.CStorageSpec (C.CStatic _)) -> True; _ow -> False) spec
funSize = fromMaybe 0 (C.lengthOfNode ni)
funPosition = C.posOf ni
Nothing -> mempty
_ow -> mempty
-- \| Returns nothing if void is used
functionParameters
:: [C.CDerivedDeclarator C.NodeInfo]
-> Maybe (Maybe [C.CDeclaration C.NodeInfo], Maybe CType -> Maybe CType)
functionParameters = \case
(C.CFunDeclr (C.CFunParamsNew x _) _ _) : rst ->
case x of
[C.CDecl [C.CTypeSpec (C.CVoidType _)] _ _] ->
Just (Nothing, applyDerivedDeclarators rst)
params -> Just (Just params, applyDerivedDeclarators rst)
_ow -> Nothing
applyDerivedDeclarators :: [C.CDerivedDeclarator C.NodeInfo] -> Maybe CType -> Maybe CType
applyDerivedDeclarators [] ct = ct
applyDerivedDeclarators _ _ = Just CTPointer
declarations :: Context -> C.CDeclaration C.NodeInfo -> [(Maybe C.Ident, CType)]
declarations ctx = \case
C.CDecl spec items _ -> let t = ctype ctx spec in map (\i -> (name i, t)) items
a@(C.CStaticAssert _ _ n) -> notSupportedYet a n
class Named f where
name :: f a -> Maybe (C.Identifier a)
instance Named C.CDeclarator where
name (C.CDeclr idx _ _ _ _) = idx
instance Named C.CDeclarationItem where
name = \case
C.CDeclarationItem decl _ _ -> name decl
C.CDeclarationExpr _ -> Nothing
includeTypeDef :: (Monad m) => C.CExternalDeclaration C.NodeInfo -> StateT Context m ()
includeTypeDef = \case
C.CDeclExt (C.CDecl (C.CStorageSpec (C.CTypedef _) : rst) decl _) -> do
let [ids] = identifiers decl
modify (\ctx -> addTypeDefs [ids] (ctype ctx rst, ITInline rst) ctx)
_ow -> pure ()
containsStructDeclaration
:: (MonadPlus m, MonadState Context m)
=> [C.CDeclarationSpecifier C.NodeInfo]
-> m Bool
containsStructDeclaration spec =
or <$> forM spec \case
-- Is a struct definition
C.CTypeSpec (C.CSUType (C.CStruct _ mid def _ _) _) -> case mid of
Just sid -> do
-- Delete if struct is deleted.
ctx <- get
_ <- liftMaybe (lookupStruct ctx sid)
case def of
Just _ -> pure True
Nothing -> pure False
Nothing -> pure False
_ow -> pure False
filterParams
:: Context
-> [Maybe CType]
-> [C.CDeclaration C.NodeInfo]
-> ([C.CDeclaration C.NodeInfo], [(C.Ident, InlineExpr)])
filterParams ctx typefilter params = flip evalState typefilter do
(params', mapping) <- flip mapAndUnzipM params \case
decl@(C.CDecl def items l) -> do
(items', defs) <- flip mapAndUnzipM items \case
a'@(C.CDeclarationItem (C.CDeclr idx _ _ _ _) _ _) -> do
t' <- state (\(t : tps) -> (t, tps))
pure $ case t' of
Just t
| not (shouldDeleteDeclaration ctx decl) ->
([a'], [(idx', IEKeep t) | idx' <- maybeToList idx])
_ow ->
([], [(idx', IEDelete) | idx' <- maybeToList idx])
a' -> notSupportedYet a' l
case concat items' of
[] -> pure ([], concat defs)
items'' -> pure ([C.CDecl def items'' l], concat defs)
a' -> don'tHandleWithPos a'
pure (concat params', concat mapping)