+cvt_top (ForeignD (ExportF callconv as nm typ))
+ = let e = CExport (CExportStatic (mkFastString as) callconv')
+ in Left $ ForD (ForeignExport (noLoc (vName nm)) (cvtType typ) e False)
+ where callconv' = case callconv of
+ CCall -> CCallConv
+ StdCall -> StdCallConv
+
+parse_ccall_impent :: String -> String -> Maybe (FastString, CImportSpec)
+parse_ccall_impent nm s
+ = case lex_ccall_impent s of
+ Just ["dynamic"] -> Just (nilFS, CFunction DynamicTarget)
+ Just ["wrapper"] -> Just (nilFS, CWrapper)
+ Just ("static":ts) -> parse_ccall_impent_static nm ts
+ Just ts -> parse_ccall_impent_static nm ts
+ Nothing -> Nothing
+
+parse_ccall_impent_static :: String
+ -> [String]
+ -> Maybe (FastString, CImportSpec)
+parse_ccall_impent_static nm ts
+ = let ts' = case ts of
+ [ "&", cid] -> [ cid]
+ [fname, "&" ] -> [fname ]
+ [fname, "&", cid] -> [fname, cid]
+ _ -> ts
+ in case ts' of
+ [ cid] | is_cid cid -> Just (nilFS, mk_cid cid)
+ [fname, cid] | is_cid cid -> Just (mkFastString fname, mk_cid cid)
+ [ ] -> Just (nilFS, mk_cid nm)
+ [fname ] -> Just (mkFastString fname, mk_cid nm)
+ _ -> Nothing
+ where is_cid :: String -> Bool
+ is_cid x = all (/= '.') x && (isAlpha (head x) || head x == '_')
+ mk_cid :: String -> CImportSpec
+ mk_cid = CFunction . StaticTarget . mkFastString
+
+lex_ccall_impent :: String -> Maybe [String]
+lex_ccall_impent "" = Just []
+lex_ccall_impent ('&':xs) = fmap ("&":) $ lex_ccall_impent xs
+lex_ccall_impent (' ':xs) = lex_ccall_impent xs
+lex_ccall_impent ('\t':xs) = lex_ccall_impent xs
+lex_ccall_impent xs = case span is_valid xs of
+ ("", _) -> Nothing
+ (t, xs') -> fmap (t:) $ lex_ccall_impent xs'
+ where is_valid :: Char -> Bool
+ is_valid c = isAscii c && (isAlphaNum c || c `elem` "._")
+
+noContext = noLoc []