Skip to content

Commit d5a62f0

Browse files
committed
Shorter names for foreign imports.
1 parent 8f432d7 commit d5a62f0

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

RegistryProcessor/src/Main.hs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ printTokens api registry = do
5252
, e <- es
5353
, api `matches` enumAPI e ]
5454

55+
-- Calulate a map from compact signature to short names.
56+
signatureMap :: Registry -> M.Map String String
57+
signatureMap registry = fst $ M.foldl' step (M.empty, 0) (commands registry)
58+
where step (m,n) command = memberAndInsert (n+1) n (sig command) (dyn n) m
59+
sig = flip showSignatureFromCommand False
60+
dyn n = "dyn" ++ show n
61+
memberAndInsert notFound found key value map =
62+
(newMap, maybe notFound (const found) maybeValue)
63+
where (maybeValue, newMap) = M.insertLookupWithKey (\_ _ s -> s) key value map
64+
5565
printFunctions :: API -> Registry -> IO ()
5666
printFunctions api registry = do
5767
let comment =
@@ -80,9 +90,10 @@ printFunctions api registry = do
8090
SI.hPutStrLn h "throwIfNullFunPtr :: String -> IO (FunPtr a) -> IO (FunPtr a)"
8191
SI.hPutStrLn h "throwIfNullFunPtr = throwIf (== nullFunPtr) . const"
8292
SI.hPutStrLn h ""
83-
mapM_ (SI.hPutStrLn h) (S.toList (M.foldl' makeImportDynamic S.empty (commands registry)))
93+
let sigMap = signatureMap registry
94+
mapM_ (SI.hPutStrLn h . uncurry makeImportDynamic) (M.assocs sigMap)
8495
SI.hPutStrLn h ""
85-
mapM_ (SI.hPutStrLn h . showCommand api) (M.elems (commands registry))
96+
mapM_ (SI.hPutStrLn h . showCommand api sigMap) (M.elems (commands registry))
8697

8798
printExtensions :: API -> Registry -> IO ()
8899
printExtensions api registry = do
@@ -308,8 +319,8 @@ convertEnum e =
308319
, n ++ " = " ++ unEnumValue (enumValue e) ]
309320
where n = unEnumName . enumName $ e
310321

311-
showCommand :: API -> Command -> String
312-
showCommand api c =
322+
showCommand :: API -> M.Map String String -> Command -> String
323+
showCommand api sigMap c =
313324
showString (take 80 ("-- " ++ name ++ " " ++ repeat '-') ++ "\n\n") .
314325

315326
showString man .
@@ -326,7 +337,7 @@ showCommand api c =
326337
id $ ""
327338

328339
where name = signatureElementName (resultType c)
329-
dyn_name = getDynName compactSignature
340+
dyn_name = lookup' compactSignature sigMap
330341
ptr_name = "ptr_" ++ name
331342
str_name = show name
332343
compactSignature = signature False
@@ -340,16 +351,11 @@ showCommand api c =
340351
renderURL (u, l) = "<" ++ u ++ " " ++ l ++ ">"
341352
args = concat [" v" ++ show i | i <- [1 .. length (paramTypes c)]]
342353

343-
makeImportDynamic :: S.Set String -> Command -> S.Set String
344-
makeImportDynamic sigmap c = S.insert sig sigmap
345-
where sig = "foreign import CALLCONV \"dynamic\" " ++ dyn_name ++ "\n" ++
346-
" :: FunPtr (" ++ compactSignature ++ ")\n" ++
347-
" -> " ++ compactSignature ++ "\n"
348-
dyn_name = getDynName compactSignature
349-
compactSignature = showSignatureFromCommand c False
350-
351-
getDynName :: String -> String
352-
getDynName sig = "dyn_" ++ (map (\x -> if x == '-' then '_' else x) $ filter (\x -> x `elem` ("-" ++ ['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z'])) sig)
354+
makeImportDynamic :: String -> String -> String
355+
makeImportDynamic compactSignature dyn_name =
356+
"foreign import CALLCONV \"dynamic\" " ++ dyn_name ++ "\n" ++
357+
" :: FunPtr (" ++ compactSignature ++ ")\n" ++
358+
" -> " ++ compactSignature ++ "\n"
353359

354360
showSignatureFromCommand :: Command -> Bool -> String
355361
showSignatureFromCommand c withComment =

0 commit comments

Comments
 (0)