[project @ 2002-11-06 13:10:46 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / Convert.lhs
index 2e8b83a..24d34f0 100644 (file)
@@ -14,9 +14,9 @@ import Language.Haskell.THSyntax as Meta
 
 import HsSyn as Hs
        (       HsExpr(..), HsLit(..), ArithSeqInfo(..), 
-               HsDoContext(..), 
+               HsStmtContext(..), 
                Match(..), GRHSs(..), GRHS(..), HsPred(..),
-               HsDecl(..), TyClDecl(..), InstDecl(..), ConDecl(..),
+               HsDecl(..), InstDecl(..), ConDecl(..),
                Stmt(..), HsBinds(..), MonoBinds(..), Sig(..),
                Pat(..), HsConDetails(..), HsOverLit, BangType(..),
                placeHolderType, HsType(..), HsTupCon(..),
@@ -26,14 +26,16 @@ import HsSyn as Hs
 
 import RdrName ( RdrName, mkRdrUnqual, mkRdrQual, mkOrig )
 import Module   ( mkModuleName )
-import RdrHsSyn        ( mkHsIntegral, mkClassDecl, mkTyData )
+import RdrHsSyn        ( mkHsIntegral, mkHsFractional, mkClassDecl, mkTyData )
 import OccName
 import SrcLoc  ( SrcLoc, generatedSrcLoc )
 import TyCon   ( DataConDetails(..) )
 import Type    ( Type )
 import BasicTypes( Boxity(..), RecFlag(Recursive), 
                   NewOrData(..), StrictnessMark(..) )
-import FastString( mkFastString )
+import ForeignCall ( Safety(..), CCallConv(..), CCallTarget(..) )
+import HsDecls ( CImportSpec(..), ForeignImport(..), ForeignDecl(..) )
+import FastString( mkFastString, nilFS )
 import Char    ( ord, isAlphaNum )
 import List    ( partition )
 import Outputable
@@ -41,11 +43,12 @@ import Outputable
 
 -------------------------------------------------------------------
 convertToHsDecls :: [Meta.Dec] -> [HsDecl RdrName]
-convertToHsDecls ds 
-  = ValD (cvtdecs binds_and_sigs) : map cvt_top top_decls
-  where
-    (binds_and_sigs, top_decls) = partition sigOrBindP ds
+convertToHsDecls ds = map cvt_top ds
+
 
+cvt_top d@(Val _ _ _) = ValD (cvtd d)
+cvt_top d@(Fun _ _)   = ValD (cvtd d)
 cvt_top (Data tc tvs constrs derivs)
   = TyClD (mkTyData DataType 
                    (noContext, tconName tc, cvt_tvs tvs)
@@ -76,6 +79,16 @@ cvt_top (Instance tys ty decs)
                         (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      = []
 noExistentials = []
 noFunDeps      = []
@@ -104,7 +117,7 @@ cvt (Infix (Just x) s (Just y)) = OpApp (cvt x) (HsVar(vName s)) undefined (cvt
 cvt (Infix Nothing  s (Just y)) = SectionR (HsVar(vName s)) (cvt y)
 cvt (Infix (Just x) s Nothing ) = SectionL (cvt x) (HsVar(vName s))
 cvt (Infix Nothing  s Nothing ) = HsVar(vName s) -- Can I indicate this is an infix thing?
-
+cvt (SigExp e t)               = ExprWithTySig (cvt e) (cvtType t)
 
 cvtdecs :: [Meta.Dec] -> HsBinds RdrName
 cvtdecs [] = EmptyBinds
@@ -170,12 +183,14 @@ cvtpair (x,y) = GRHS [BindStmt truePat (cvt x) loc0,
                      ResultStmt (cvt y) loc0] loc0
 
 cvtOverLit :: Lit -> HsOverLit
-cvtOverLit (Int i) = mkHsIntegral (fromInt i)
+cvtOverLit (Int i)      = mkHsIntegral (fromInt i)
+cvtOverLit (Rational r) = mkHsFractional r
 -- An Int is like an an (overloaded) '3' in a Haskell source program
+-- Similarly 3.5 for fractionals
 
 cvtLit :: Lit -> HsLit
-cvtLit (Char c)              = HsChar (ord c)
-cvtLit (CrossStage s) = error "What do we do about crossStage constants?"
+cvtLit (Char c)          = HsChar (ord c)
+cvtLit (String s) = HsString (mkFastString s)
 
 cvtp :: Meta.Pat -> Hs.Pat RdrName
 cvtp (Plit l)
@@ -196,7 +211,7 @@ cvtp Pwild        = WildPat void
 cvt_tvs :: [String] -> [HsTyVarBndr RdrName]
 cvt_tvs tvs = map (UserTyVar . tName) tvs
 
-cvt_context :: Context -> HsContext RdrName 
+cvt_context :: Cxt -> HsContext RdrName 
 cvt_context tys = map cvt_pred tys
 
 cvt_pred :: Typ -> HsPred RdrName
@@ -205,15 +220,23 @@ cvt_pred ty = case split_ty_app ty of
                other -> panic "Malformed predicate"
 
 cvtType :: Meta.Typ -> HsType RdrName
-cvtType (Tvar nm)  = HsTyVar(tName nm)
-cvtType (Tapp x y) = trans (root x [y])
-  where root (Tapp a b) zs = root a (b:zs)
-        root t zs = (t,zs)
-        trans (Tcon (Tuple n),args) = HsTupleTy (HsTupCon Boxed n) (map cvtType args)
-        trans (Tcon Arrow,[x,y])    =  HsFunTy (cvtType x) (cvtType y)
-        trans (Tcon List,[x])      = HsListTy (cvtType x)
-        trans (Tcon (Name nm),args) = HsTyVar(tconName nm)
-        trans (t,args)             = panic "bad type application"
+cvtType ty = trans (root ty [])
+  where root (Tapp a b) zs = root a (cvtType b : zs)
+        root t zs         = (t,zs)
+
+        trans (Tcon (Tuple n),args) | length args == n
+                                   = HsTupleTy (HsTupCon Boxed n) args
+        trans (Tcon Arrow,   [x,y]) = HsFunTy x y
+        trans (Tcon List,    [x])   = HsListTy x
+
+       trans (Tvar nm, args)       = foldl HsAppTy (HsTyVar (tName nm)) args
+        trans (Tcon tc, args)       = foldl HsAppTy (HsTyVar (tc_name tc)) args
+
+       tc_name (TconName nm) = tconName nm
+       tc_name Arrow         = tconName "->"
+       tc_name List          = tconName "[]"
+       tc_name (Tuple 0)     = tconName "()"
+       tc_name (Tuple n)     = tconName ("(" ++ replicate (n-1) ',' ++ ")")
 
 split_ty_app :: Typ -> (Typ, [Typ])
 split_ty_app ty = go ty []
@@ -226,12 +249,6 @@ sigP :: Dec -> Bool
 sigP (Proto _ _) = True
 sigP other      = False
 
-sigOrBindP :: Dec -> Bool
-sigOrBindP (Proto _ _) = True
-sigOrBindP (Val _ _ _) = True
-sigOrBindP (Fun _ _)   = True
-sigOrBindP other       = False
-
 
 -----------------------------------------------------------
 -- some useful things