- (binds, sigs) = cvtBindsAndSigs decs
- inst_ty = HsForAllTy Nothing
- (cvt_context tys)
- (HsPredTy (cvt_pred ty))
-
-cvt_top (Proto nm typ) = SigD (Sig (vName nm) (cvtType typ) loc0)
-
-cvt_top (Foreign (Import callconv safety from nm typ))
- = ForD (ForeignImport (vName nm) (cvtType typ) fi False loc0)
- where fi = CImport CCallConv (PlaySafe True) c_header nilFS cis
- (c_header', c_func') = break (== ' ') from
- c_header = mkFastString c_header'
- c_func = tail c_func'
- cis = CFunction (StaticTarget (mkFastString c_func))
-
-noContext = []
+ -- Can't handle GADTs yet
+ mk_nlcon con = case con of
+ NormalC c strtys
+ -> ConDecl (L loc (cName c)) Explicit noExistentials (noContext loc)
+ (PrefixCon (map mk_arg strtys)) ResTyH98
+ RecC c varstrtys
+ -> ConDecl (L loc (cName c)) Explicit noExistentials (noContext loc)
+ (RecCon (map mk_id_arg varstrtys)) ResTyH98
+ InfixC st1 c st2
+ -> ConDecl (L loc (cName c)) Explicit noExistentials (noContext loc)
+ (InfixCon (mk_arg st1) (mk_arg st2)) ResTyH98
+ ForallC tvs ctxt (ForallC tvs' ctxt' con')
+ -> mk_nlcon (ForallC (tvs ++ tvs') (ctxt ++ ctxt') con')
+ ForallC tvs ctxt con' -> case mk_nlcon con' of
+ ConDecl l _ [] (L _ []) x ResTyH98 ->
+ ConDecl l Explicit (cvt_tvs loc tvs) (cvt_context loc ctxt) x ResTyH98
+ c -> panic "ForallC: Can't happen"
+ mk_arg (IsStrict, ty) = L loc $ HsBangTy HsStrict (cvtType loc ty)
+ mk_arg (NotStrict, ty) = cvtType loc ty
+
+ mk_id_arg (i, IsStrict, ty)
+ = (L loc (vName i), L loc $ HsBangTy HsStrict (cvtType loc ty))
+ mk_id_arg (i, NotStrict, ty)
+ = (L loc (vName i), cvtType loc ty)
+
+mk_derivs loc [] = Nothing
+mk_derivs loc cs = Just [L loc $ HsPredTy $ HsClassP (tconName c) [] | c <- cs]
+
+cvt_fundep :: FunDep -> Class.FunDep RdrName
+cvt_fundep (FunDep xs ys) = (map tName xs, map tName ys)
+
+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 loc = L loc []