diff options
author | Joe Crayne <joe@jerkface.net> | 2019-03-10 20:41:59 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-03-10 20:41:59 -0400 |
commit | 4571dcae244b81a4b6aa0acacd773f728be49772 (patch) | |
tree | 11a9879d9334403808ada7749c23e25e18a100d9 | |
parent | ef0bd9baee906ebf7c3293f0e5ec531bca0b4801 (diff) |
Signatures.
-rw-r--r-- | monkeypatch.hs | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/monkeypatch.hs b/monkeypatch.hs index ac67afa..cfa9011 100644 --- a/monkeypatch.hs +++ b/monkeypatch.hs | |||
@@ -71,6 +71,7 @@ prettyUsingInclude incs (CTranslUnit edecls _) = | |||
71 | sysfst (Left ('"':a)) (Left ('<':b)) = Prelude.GT | 71 | sysfst (Left ('"':a)) (Left ('<':b)) = Prelude.GT |
72 | sysfst _ _ = Prelude.LT | 72 | sysfst _ _ = Prelude.LT |
73 | 73 | ||
74 | includeTopLevel :: IncludeStack -> FilePath -> [Char] | ||
74 | includeTopLevel (IncludeStack incs) f = do | 75 | includeTopLevel (IncludeStack incs) f = do |
75 | stacks <- maybeToList $ Map.lookup f incs | 76 | stacks <- maybeToList $ Map.lookup f incs |
76 | stack <- take 1 stacks | 77 | stack <- take 1 stacks |
@@ -132,6 +133,7 @@ transField (CDecl [CTypeSpec (CSUType (CStruct CStructTag mctyp Nothing [] _) _) | |||
132 | 133 | ||
133 | transField _ = [] | 134 | transField _ = [] |
134 | 135 | ||
136 | transpile :: CExternalDeclaration a -> Maybe (Q Dec) | ||
135 | transpile (CDeclExt (CDecl [ CTypeSpec (CSUType | 137 | transpile (CDeclExt (CDecl [ CTypeSpec (CSUType |
136 | (CStruct CStructTag mbIdent (Just fields) [] _) | 138 | (CStruct CStructTag mbIdent (Just fields) [] _) |
137 | _) ] | 139 | _) ] |
@@ -158,6 +160,8 @@ data SymbolInformation c = SymbolInformation | |||
158 | } | 160 | } |
159 | deriving (Eq,Ord,Show,Functor) | 161 | deriving (Eq,Ord,Show,Functor) |
160 | 162 | ||
163 | symbolInformation :: SymbolInformation | ||
164 | [CExternalDeclaration NodeInfo] | ||
161 | symbolInformation = SymbolInformation | 165 | symbolInformation = SymbolInformation |
162 | { symbolLocal = False | 166 | { symbolLocal = False |
163 | , symbolStatic = False | 167 | , symbolStatic = False |
@@ -168,11 +172,16 @@ data Transpile c = Transpile | |||
168 | { syms :: Map String (SymbolInformation c) | 172 | { syms :: Map String (SymbolInformation c) |
169 | } | 173 | } |
170 | 174 | ||
175 | initTranspile :: Transpile c | ||
171 | initTranspile = Transpile | 176 | initTranspile = Transpile |
172 | { syms = Map.empty | 177 | { syms = Map.empty |
173 | } | 178 | } |
174 | 179 | ||
175 | -- grokSymbol :: CExternalDeclaration a -> String -> Maybe SymbolInformation -> Maybe SymbolInformation | 180 | -- grokSymbol :: CExternalDeclaration a -> String -> Maybe SymbolInformation -> Maybe SymbolInformation |
181 | grokSymbol :: CExternalDeclaration NodeInfo | ||
182 | -> p | ||
183 | -> Maybe (SymbolInformation [CExternalDeclaration NodeInfo]) | ||
184 | -> Maybe (SymbolInformation [CExternalDeclaration NodeInfo]) | ||
176 | grokSymbol d k msi = | 185 | grokSymbol d k msi = |
177 | let si = fromMaybe symbolInformation msi | 186 | let si = fromMaybe symbolInformation msi |
178 | in Just $ si | 187 | in Just $ si |
@@ -200,6 +209,7 @@ hsMkName str = HS.UnQual () (foo () str) | |||
200 | foo = HS.Ident -- alternative: HS.Symbol | 209 | foo = HS.Ident -- alternative: HS.Symbol |
201 | 210 | ||
202 | 211 | ||
212 | notKnown :: String -> Bool | ||
203 | notKnown "Word8" = False | 213 | notKnown "Word8" = False |
204 | notKnown "Word16" = False | 214 | notKnown "Word16" = False |
205 | notKnown "Word32" = False | 215 | notKnown "Word32" = False |
@@ -292,6 +302,7 @@ transField (CDecl [CTypeSpec (CSUType (CStruct CStructTag mctyp Nothing [] _) _) | |||
292 | hsTransField _ _ = [] | 302 | hsTransField _ _ = [] |
293 | -} | 303 | -} |
294 | 304 | ||
305 | extractType :: Decl () -> HS.Type () | ||
295 | extractType (HS.TypeDecl _ _ ftyp) = ftyp | 306 | extractType (HS.TypeDecl _ _ ftyp) = ftyp |
296 | extractType (HS.TypeSig _ _ ftyp) = ftyp | 307 | extractType (HS.TypeSig _ _ ftyp) = ftyp |
297 | extractType _ = TyCon () (Special () (UnitCon ())) | 308 | extractType _ = TyCon () (Special () (UnitCon ())) |
@@ -326,6 +337,7 @@ tname :: HS.Type () -> String | |||
326 | tname (HS.TyCon () (HS.UnQual () (HS.Ident () str))) = str | 337 | tname (HS.TyCon () (HS.UnQual () (HS.Ident () str))) = str |
327 | tname _ = "_unkonwn" | 338 | tname _ = "_unkonwn" |
328 | 339 | ||
340 | getPtrType :: HS.Type l -> Maybe (HS.Type l) | ||
329 | getPtrType (HS.TyApp _ (HS.TyCon _ (HS.UnQual _ (HS.Ident _ "Ptr"))) x) = Just x | 341 | getPtrType (HS.TyApp _ (HS.TyCon _ (HS.UnQual _ (HS.Ident _ "Ptr"))) x) = Just x |
330 | getPtrType _ = Nothing | 342 | getPtrType _ = Nothing |
331 | 343 | ||
@@ -368,9 +380,11 @@ sigf f d = f (getReturnValue d) $ do | |||
368 | return $ CDeclr s [arg] Nothing [] (node d) | 380 | return $ CDeclr s [arg] Nothing [] (node d) |
369 | -} | 381 | -} |
370 | 382 | ||
383 | body0 :: CExternalDeclaration a -> Maybe (CStatement a) | ||
371 | body0 (CFDefExt (CFunDef rs cdeclr [] bdy _)) = Just bdy | 384 | body0 (CFDefExt (CFunDef rs cdeclr [] bdy _)) = Just bdy |
372 | body0 _ = Nothing | 385 | body0 _ = Nothing |
373 | 386 | ||
387 | body :: CExternalDeclaration a -> [CCompoundBlockItem a] | ||
374 | body (CFDefExt (CFunDef rs cdeclr [] (CCompound [] bdy _) _)) = bdy | 388 | body (CFDefExt (CFunDef rs cdeclr [] (CCompound [] bdy _) _)) = bdy |
375 | body _ = [] | 389 | body _ = [] |
376 | 390 | ||
@@ -379,6 +393,7 @@ data SideEffect = PointerWrite | FunctionCall | |||
379 | calls :: Data t => t -> [CExpression NodeInfo] | 393 | calls :: Data t => t -> [CExpression NodeInfo] |
380 | calls = everything (++) (mkQ [] (\case { cc@C.CCall {} -> [cc] ; _ -> [] })) | 394 | calls = everything (++) (mkQ [] (\case { cc@C.CCall {} -> [cc] ; _ -> [] })) |
381 | 395 | ||
396 | mutations1 :: CExpression a -> [CExpression a] | ||
382 | mutations1 e@(CAssign {}) = [e] | 397 | mutations1 e@(CAssign {}) = [e] |
383 | mutations1 e@(CUnary CPreIncOp _ _) = [e] | 398 | mutations1 e@(CUnary CPreIncOp _ _) = [e] |
384 | mutations1 e@(CUnary CPreDecOp _ _) = [e] | 399 | mutations1 e@(CUnary CPreDecOp _ _) = [e] |
@@ -410,6 +425,7 @@ data C2HaskellOptions = C2HaskellOptions | |||
410 | , preprocess :: Bool | 425 | , preprocess :: Bool |
411 | } | 426 | } |
412 | 427 | ||
428 | defopts :: C2HaskellOptions | ||
413 | defopts = C2HaskellOptions | 429 | defopts = C2HaskellOptions |
414 | { selectFunction = Nothing | 430 | { selectFunction = Nothing |
415 | , prettyC = False | 431 | , prettyC = False |
@@ -418,6 +434,7 @@ defopts = C2HaskellOptions | |||
418 | , preprocess = False | 434 | , preprocess = False |
419 | } | 435 | } |
420 | 436 | ||
437 | parseOptions :: [String] -> C2HaskellOptions -> C2HaskellOptions | ||
421 | parseOptions [] opts = opts | 438 | parseOptions [] opts = opts |
422 | parseOptions ("-f":f:args) opts = parseOptions args opts | 439 | parseOptions ("-f":f:args) opts = parseOptions args opts |
423 | { selectFunction = Just f | 440 | { selectFunction = Just f |
@@ -436,6 +453,8 @@ parseOptions ("-v":args) opts = parseOptions args opts | |||
436 | } | 453 | } |
437 | parseOptions as x = error (show as) | 454 | parseOptions as x = error (show as) |
438 | 455 | ||
456 | tnames :: Show b => | ||
457 | CExternalDeclaration b -> [(String, Maybe String)] | ||
439 | tnames d = filter (notKnown . fst) $ map (first $ tname . unpointer) $ concatMap (\(t,c) -> map (,c) (types t)) $ sigf hsTransSig d | 458 | tnames d = filter (notKnown . fst) $ map (first $ tname . unpointer) $ concatMap (\(t,c) -> map (,c) (types t)) $ sigf hsTransSig d |
440 | 459 | ||
441 | 460 | ||
@@ -454,6 +473,7 @@ getsig (k,si) = do | |||
454 | s = sig d | 473 | s = sig d |
455 | [(ts,(k,s,d))] | 474 | [(ts,(k,s,d))] |
456 | 475 | ||
476 | isAcceptableImport :: HS.Type l -> Bool | ||
457 | isAcceptableImport (HS.TyFun _ (TyCon _ (UnQual _ (HS.Ident _ x))) xs) | not (notKnown x) = isAcceptableImport xs | 477 | isAcceptableImport (HS.TyFun _ (TyCon _ (UnQual _ (HS.Ident _ x))) xs) | not (notKnown x) = isAcceptableImport xs |
458 | isAcceptableImport (HS.TyFun _ (TyApp _ (TyCon _ (UnQual _ (HS.Ident _ "Ptr"))) x) xs) = isAcceptableImport xs | 478 | isAcceptableImport (HS.TyFun _ (TyApp _ (TyCon _ (UnQual _ (HS.Ident _ "Ptr"))) x) xs) = isAcceptableImport xs |
459 | isAcceptableImport (TyCon _ _) = True | 479 | isAcceptableImport (TyCon _ _) = True |
@@ -466,6 +486,7 @@ makeFunctionUseIO t@(TyApp a (TyCon b (UnQual c (HS.Ident d "IO"))) x) = t | |||
466 | makeFunctionUseIO t = TyApp () (TyCon () (UnQual () (HS.Ident () "IO"))) t | 486 | makeFunctionUseIO t = TyApp () (TyCon () (UnQual () (HS.Ident () "IO"))) t |
467 | 487 | ||
468 | 488 | ||
489 | makeAcceptableImport :: HS.Type l -> HS.Type l | ||
469 | makeAcceptableImport (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) xs) | not (notKnown x) | 490 | makeAcceptableImport (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) xs) | not (notKnown x) |
470 | = (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) (makeAcceptableImport xs)) | 491 | = (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) (makeAcceptableImport xs)) |
471 | makeAcceptableImport (HS.TyFun a (TyApp b (TyCon c (UnQual d (HS.Ident e "Ptr"))) x) xs) | 492 | makeAcceptableImport (HS.TyFun a (TyApp b (TyCon c (UnQual d (HS.Ident e "Ptr"))) x) xs) |
@@ -474,8 +495,10 @@ makeAcceptableImport (HS.TyFun a (TyCon c (UnQual d (HS.Ident e x))) xs) | |||
474 | = (HS.TyFun a (TyApp c (TyCon c (UnQual d (HS.Ident e "Ptr"))) (TyCon e (UnQual e (HS.Ident e x)))) (makeAcceptableImport xs)) | 495 | = (HS.TyFun a (TyApp c (TyCon c (UnQual d (HS.Ident e "Ptr"))) (TyCon e (UnQual e (HS.Ident e x)))) (makeAcceptableImport xs)) |
475 | makeAcceptableImport t = t | 496 | makeAcceptableImport t = t |
476 | 497 | ||
498 | enumCases :: CExternalDeclaration a | ||
499 | -> [(a, [(Ident, Maybe (CExpression a))])] | ||
477 | enumCases (CDeclExt (CDecl xs _ ni)) = do | 500 | enumCases (CDeclExt (CDecl xs _ ni)) = do |
478 | CTypeSpec (CEnumType (CEnum _ (Just cs))) <- xs | 501 | CTypeSpec (CEnumType (CEnum _ (Just cs) _ _) _) <- xs |
479 | return (ni,cs) | 502 | return (ni,cs) |
480 | 503 | ||
481 | 504 | ||
@@ -614,6 +637,8 @@ makeStatic :: [CDeclarationSpecifier NodeInfo] -> [CDeclarationSpecifier NodeInf | |||
614 | makeStatic xs = CStorageSpec (CStatic undefNode) : xs | 637 | makeStatic xs = CStorageSpec (CStatic undefNode) : xs |
615 | -- makeStatic xs = CStorageSpec (CStatic ()) : xs | 638 | -- makeStatic xs = CStorageSpec (CStatic ()) : xs |
616 | 639 | ||
640 | makePointer1 :: Maybe (CDeclarator NodeInfo) | ||
641 | -> Maybe (CDeclarator NodeInfo) | ||
617 | makePointer1 (Just (CDeclr a bs c d e)) | 642 | makePointer1 (Just (CDeclr a bs c d e)) |
618 | = (Just (CDeclr a (p:bs) c d e)) | 643 | = (Just (CDeclr a (p:bs) c d e)) |
619 | where | 644 | where |
@@ -627,6 +652,8 @@ makePointer ((a,b,c):zs) = (makePointer1 a,b,c):zs | |||
627 | setNull1 :: Maybe (CInitializer NodeInfo) | 652 | setNull1 :: Maybe (CInitializer NodeInfo) |
628 | setNull1 = Just (CInitExpr (CVar (C.Ident "NULL" 0 undefNode) undefNode) undefNode) | 653 | setNull1 = Just (CInitExpr (CVar (C.Ident "NULL" 0 undefNode) undefNode) undefNode) |
629 | 654 | ||
655 | setNull :: [(a, Maybe (CInitializer NodeInfo), c)] | ||
656 | -> [(a, Maybe (CInitializer NodeInfo), c)] | ||
630 | setNull ((a,_,b):zs) = (a,setNull1,b):zs | 657 | setNull ((a,_,b):zs) = (a,setNull1,b):zs |
631 | 658 | ||
632 | makeFunctionPointer :: CExternalDeclaration NodeInfo | 659 | makeFunctionPointer :: CExternalDeclaration NodeInfo |
@@ -634,27 +661,47 @@ makeFunctionPointer :: CExternalDeclaration NodeInfo | |||
634 | makeFunctionPointer d@(CDeclExt (CDecl xs ys pos)) = changeName ("f_"++) $ CDeclExt (CDecl (makeStatic xs) (setNull $ makePointer ys) pos) | 661 | makeFunctionPointer d@(CDeclExt (CDecl xs ys pos)) = changeName ("f_"++) $ CDeclExt (CDecl (makeStatic xs) (setNull $ makePointer ys) pos) |
635 | makeFunctionPointer d = d | 662 | makeFunctionPointer d = d |
636 | 663 | ||
664 | changeName2 :: (String -> String) | ||
665 | -> Maybe (CDeclarator a) -> Maybe (CDeclarator a) | ||
637 | changeName2 f (Just (CDeclr (Just (C.Ident nm n p)) bs c d e)) | 666 | changeName2 f (Just (CDeclr (Just (C.Ident nm n p)) bs c d e)) |
638 | = (Just (CDeclr (Just (C.Ident (f nm) n p)) bs c d e)) | 667 | = (Just (CDeclr (Just (C.Ident (f nm) n p)) bs c d e)) |
639 | changeName2 f d = d | 668 | changeName2 f d = d |
640 | 669 | ||
670 | changeName1 :: (String -> String) | ||
671 | -> [(Maybe (CDeclarator a), b, c)] | ||
672 | -> [(Maybe (CDeclarator a), b, c)] | ||
641 | changeName1 f ((a,b,c):zs) = (changeName2 f a,b,c):zs | 673 | changeName1 f ((a,b,c):zs) = (changeName2 f a,b,c):zs |
642 | 674 | ||
675 | changeName :: (String -> String) | ||
676 | -> CExternalDeclaration a -> CExternalDeclaration a | ||
643 | changeName f d@(CDeclExt (CDecl xs ys pos)) = CDeclExt (CDecl xs (changeName1 f ys) pos) | 677 | changeName f d@(CDeclExt (CDecl xs ys pos)) = CDeclExt (CDecl xs (changeName1 f ys) pos) |
644 | changeName f d = d | 678 | changeName f d = d |
645 | 679 | ||
680 | makeAcceptableDecl :: Decl () -> Decl () | ||
646 | makeAcceptableDecl (HS.TypeDecl a (DHead b (HS.Ident c signame)) ftyp) | 681 | makeAcceptableDecl (HS.TypeDecl a (DHead b (HS.Ident c signame)) ftyp) |
647 | = (HS.TypeDecl a (DHead b (HS.Ident c signame)) (makeFunctionUseIO $ makeAcceptableImport ftyp)) | 682 | = (HS.TypeDecl a (DHead b (HS.Ident c signame)) (makeFunctionUseIO $ makeAcceptableImport ftyp)) |
648 | makeAcceptableDecl (HS.TypeSig a b ftyp) = HS.TypeSig a b (makeFunctionUseIO $ makeAcceptableImport ftyp) | 683 | makeAcceptableDecl (HS.TypeSig a b ftyp) = HS.TypeSig a b (makeFunctionUseIO $ makeAcceptableImport ftyp) |
649 | 684 | ||
685 | makeSetter :: CExternalDeclaration NodeInfo | ||
686 | -> CExternalDeclaration NodeInfo | ||
650 | makeSetter d = -- @(CDeclExt (CDecl xs ys pos)) = | 687 | makeSetter d = -- @(CDeclExt (CDecl xs ys pos)) = |
651 | let name = concatMap identToString $ take 1 $ catMaybes $ sym d | 688 | let name = concatMap identToString $ take 1 $ catMaybes $ sym d |
652 | in setBody (setterBody ("f_"++name)) $ changeReturnValue (const voidReturnType) $ changeArgList (const voidp) $ changeName ("setf_"++) d | 689 | in setBody (setterBody ("f_"++name)) $ changeReturnValue (const voidReturnType) $ changeArgList (const voidp) $ changeName ("setf_"++) d |
653 | 690 | ||
691 | changeArgList1 :: ([CDerivedDeclarator a] | ||
692 | -> [CDerivedDeclarator a]) | ||
693 | -> CDeclarator a -> CDeclarator a | ||
654 | changeArgList1 f (CDeclr a xs b c d) = CDeclr a (f xs) b c d | 694 | changeArgList1 f (CDeclr a xs b c d) = CDeclr a (f xs) b c d |
655 | 695 | ||
696 | changeArgList2 :: ([CDerivedDeclarator a] | ||
697 | -> [CDerivedDeclarator a]) | ||
698 | -> [(Maybe (CDeclarator a), b, c)] | ||
699 | -> [(Maybe (CDeclarator a), b, c)] | ||
656 | changeArgList2 f ((a,b,c):zs) = (changeArgList3 f a,b,c):zs | 700 | changeArgList2 f ((a,b,c):zs) = (changeArgList3 f a,b,c):zs |
657 | 701 | ||
702 | changeArgList3 :: ([CDerivedDeclarator a] | ||
703 | -> [CDerivedDeclarator a]) | ||
704 | -> Maybe (CDeclarator a) -> Maybe (CDeclarator a) | ||
658 | changeArgList3 f (Just (CDeclr a x b c d)) = Just (CDeclr a (f x) b c d) | 705 | changeArgList3 f (Just (CDeclr a x b c d)) = Just (CDeclr a (f x) b c d) |
659 | 706 | ||
660 | changeArgList :: ([CDerivedDeclarator a] -> [CDerivedDeclarator a]) | 707 | changeArgList :: ([CDerivedDeclarator a] -> [CDerivedDeclarator a]) |
@@ -665,13 +712,19 @@ changeArgList f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl xs (changeArgLis | |||
665 | setPosOfNode :: Position -> NodeInfo -> NodeInfo | 712 | setPosOfNode :: Position -> NodeInfo -> NodeInfo |
666 | setPosOfNode pos n = maybe (mkNodeInfoOnlyPos pos) (mkNodeInfo pos) $ nameOfNode n | 713 | setPosOfNode pos n = maybe (mkNodeInfoOnlyPos pos) (mkNodeInfo pos) $ nameOfNode n |
667 | 714 | ||
715 | setPos :: Position | ||
716 | -> CExternalDeclaration NodeInfo -> CExternalDeclaration NodeInfo | ||
668 | setPos pos (CFDefExt (CFunDef xs ys zs c n)) = (CFDefExt (CFunDef xs ys zs c $ setPosOfNode pos n)) | 717 | setPos pos (CFDefExt (CFunDef xs ys zs c n)) = (CFDefExt (CFunDef xs ys zs c $ setPosOfNode pos n)) |
669 | setPos pos (CDeclExt (CDecl xs ys n)) = (CDeclExt (CDecl xs ys $ setPosOfNode pos n)) | 718 | setPos pos (CDeclExt (CDecl xs ys n)) = (CDeclExt (CDecl xs ys $ setPosOfNode pos n)) |
670 | 719 | ||
720 | getArgList1 :: CDeclarator a -> [CDerivedDeclarator a] | ||
671 | getArgList1 (CDeclr a xs b c d) = xs | 721 | getArgList1 (CDeclr a xs b c d) = xs |
672 | 722 | ||
723 | getArgList2 :: [(Maybe (CDeclarator a), b, c)] | ||
724 | -> [CDerivedDeclarator a] | ||
673 | getArgList2 ((a,b,c):zs) = getArgList3 a | 725 | getArgList2 ((a,b,c):zs) = getArgList3 a |
674 | 726 | ||
727 | getArgList3 :: Maybe (CDeclarator a) -> [CDerivedDeclarator a] | ||
675 | getArgList3 (Just (CDeclr a [CPtrDeclr [] _] b c d)) = [] -- struct prototype, no fields. | 728 | getArgList3 (Just (CDeclr a [CPtrDeclr [] _] b c d)) = [] -- struct prototype, no fields. |
676 | getArgList3 (Just (CDeclr a x b c d)) = x | 729 | getArgList3 (Just (CDeclr a x b c d)) = x |
677 | 730 | ||
@@ -685,20 +738,30 @@ getArgList x = let v=getArgList_ x in trace ("getArgList ("++show (u x)++") = "+ | |||
685 | u :: Functor f => f a -> f () | 738 | u :: Functor f => f a -> f () |
686 | u = fmap (const ()) | 739 | u = fmap (const ()) |
687 | 740 | ||
741 | changeReturnValue :: ([CDeclarationSpecifier a] | ||
742 | -> [CDeclarationSpecifier a]) | ||
743 | -> CExternalDeclaration a -> CExternalDeclaration a | ||
688 | changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d)) | 744 | changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d)) |
689 | changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos)) | 745 | changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos)) |
690 | 746 | ||
747 | getReturnValue :: CExternalDeclaration a | ||
748 | -> [CDeclarationSpecifier a] | ||
691 | getReturnValue (CFDefExt (CFunDef xs ys zs c d)) = xs | 749 | getReturnValue (CFDefExt (CFunDef xs ys zs c d)) = xs |
692 | getReturnValue (CDeclExt (CDecl xs ys pos)) = xs | 750 | getReturnValue (CDeclExt (CDecl xs ys pos)) = xs |
693 | 751 | ||
752 | voidReturnType :: [CDeclarationSpecifier NodeInfo] | ||
694 | voidReturnType = [ CTypeSpec (CVoidType undefNode) ] | 753 | voidReturnType = [ CTypeSpec (CVoidType undefNode) ] |
695 | 754 | ||
755 | setBody :: CStatement a | ||
756 | -> CExternalDeclaration a -> CExternalDeclaration a | ||
696 | setBody bdy (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef xs ys zs bdy d)) | 757 | setBody bdy (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef xs ys zs bdy d)) |
697 | setBody bdy (CDeclExt (CDecl xs ys pos)) = (CFDefExt (CFunDef xs v [] bdy pos)) | 758 | setBody bdy (CDeclExt (CDecl xs ys pos)) = (CFDefExt (CFunDef xs v [] bdy pos)) |
698 | where v = case ys of | 759 | where v = case ys of |
699 | (Just y,_,_):_ -> y | 760 | (Just y,_,_):_ -> y |
700 | _ -> CDeclr Nothing [] Nothing [] pos | 761 | _ -> CDeclr Nothing [] Nothing [] pos |
701 | 762 | ||
763 | makeStub :: CExternalDeclaration NodeInfo | ||
764 | -> CExternalDeclaration NodeInfo | ||
702 | makeStub d = -- @(CDeclExt (CDecl xs ys pos)) = | 765 | makeStub d = -- @(CDeclExt (CDecl xs ys pos)) = |
703 | let rval = case getReturnValue d of | 766 | let rval = case getReturnValue d of |
704 | [ CTypeSpec (CVoidType _) ] -> False -- void function. | 767 | [ CTypeSpec (CVoidType _) ] -> False -- void function. |
@@ -762,6 +825,8 @@ voidp = [ CFunDeclr | |||
762 | where n = undefNode | 825 | where n = undefNode |
763 | 826 | ||
764 | 827 | ||
828 | stubBody :: String | ||
829 | -> [CExpression NodeInfo] -> Bool -> String -> CStatement NodeInfo | ||
765 | stubBody name vs rval msg = | 830 | stubBody name vs rval msg = |
766 | CCompound [] | 831 | CCompound [] |
767 | [ CBlockStmt | 832 | [ CBlockStmt |
@@ -951,15 +1016,20 @@ newtype IncludeStack = IncludeStack | |||
951 | } | 1016 | } |
952 | deriving Show | 1017 | deriving Show |
953 | 1018 | ||
1019 | emptyIncludes :: IncludeStack | ||
954 | emptyIncludes = IncludeStack Map.empty | 1020 | emptyIncludes = IncludeStack Map.empty |
955 | 1021 | ||
1022 | openInclude :: FilePath | ||
1023 | -> [FilePath] -> IncludeStack -> IncludeStack | ||
956 | openInclude fname stack (IncludeStack m) = IncludeStack $ Map.alter go fname m | 1024 | openInclude fname stack (IncludeStack m) = IncludeStack $ Map.alter go fname m |
957 | where | 1025 | where |
958 | go Nothing = Just [stack] | 1026 | go Nothing = Just [stack] |
959 | go (Just s) = Just $ stack : s | 1027 | go (Just s) = Just $ stack : s |
960 | 1028 | ||
1029 | findQuoted :: [Char] -> [Char] | ||
961 | findQuoted xs = takeWhile (/='"') $ drop 1 $ dropWhile (/='"') xs | 1030 | findQuoted xs = takeWhile (/='"') $ drop 1 $ dropWhile (/='"') xs |
962 | 1031 | ||
1032 | includeStack :: B.ByteString -> IncludeStack | ||
963 | includeStack bs = foldr go (const emptyIncludes) incs [] | 1033 | includeStack bs = foldr go (const emptyIncludes) incs [] |
964 | where | 1034 | where |
965 | incs = filter (\b -> fmap fst (B.uncons b) == Just '#') $ B.lines bs | 1035 | incs = filter (\b -> fmap fst (B.uncons b) == Just '#') $ B.lines bs |