[project @ 2004-12-23 09:07:30 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcSplice.lhs
index 31dfd31..982ac91 100644 (file)
@@ -22,7 +22,7 @@ import HsSyn          ( HsBracket(..), HsExpr(..), HsSplice(..), LHsExpr, LHsDecl,
 import Convert         ( convertToHsExpr, convertToHsDecls, convertToHsType )
 import RnExpr          ( rnLExpr )
 import RnEnv           ( lookupFixityRn, lookupSrcOcc_maybe )
-import RdrName         ( RdrName, mkRdrUnqual, lookupLocalRdrEnv )
+import RdrName         ( RdrName, mkRdrQual, mkRdrUnqual, lookupLocalRdrEnv )
 import RnTypes         ( rnLHsType )
 import TcExpr          ( tcCheckRho, tcMonoExpr )
 import TcHsSyn         ( mkHsLet, zonkTopLExpr )
@@ -40,12 +40,13 @@ import NameEnv              ( lookupNameEnv )
 import HscTypes                ( lookupType, ExternalPackageState(..) )
 import OccName
 import Var             ( Id, TyVar, idType )
-import Module          ( moduleUserString, mkModuleName )
+import Module          ( moduleUserString, mkModule )
 import TcRnMonad
 import IfaceEnv                ( lookupOrig )
 import Class           ( Class, classExtraBigSig )
 import TyCon           ( TyCon, AlgTyConRhs(..), tyConTyVars, getSynTyConDefn, 
-                         isSynTyCon, isNewTyCon, tyConDataCons, algTyConRhs )
+                         isSynTyCon, isNewTyCon, tyConDataCons, algTyConRhs, isPrimTyCon, isFunTyCon,
+                         tyConArity, isUnLiftedTyCon )
 import DataCon         ( DataCon, dataConTyCon, dataConOrigArgTys, dataConStrictMarks, 
                          dataConName, dataConFieldLabels, dataConWrapId, dataConIsInfix, 
                          isVanillaDataCon )
@@ -397,7 +398,7 @@ To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
 
 \begin{code}
 instance TH.Quasi (IOEnv (Env TcGblEnv TcLclEnv)) where
-  qNewName s = do  { u <- newUnique 
+  qNewName s = do { u <- newUnique 
                  ; let i = getKey u
                  ; return (TH.mkNameU s i) }
 
@@ -456,7 +457,7 @@ reify th_name
 
 lookupThName :: TH.Name -> TcM Name
 lookupThName (TH.Name occ (TH.NameG th_ns mod))
-  = lookupOrig (mkModuleName (TH.modString mod))
+  = lookupOrig (mkModule (TH.modString mod))
               (OccName.mkOccName ghc_ns (TH.occString occ))
   where
     ghc_ns = case th_ns of
@@ -464,8 +465,18 @@ lookupThName (TH.Name occ (TH.NameG th_ns mod))
                TH.TcClsName -> tcClsName
                TH.VarName   -> varName
 
-lookupThName th_name@(TH.Name occ TH.NameS) 
-  =  do { let rdr_name = mkRdrUnqual (OccName.mkOccFS ns occ_fs)
+lookupThName (TH.Name occ (TH.NameU uniq)) 
+  = return (mkInternalName (mk_uniq uniq) (OccName.mkOccFS bogus_ns occ_fs) noSrcLoc)
+  where
+    occ_fs = mkFastString (TH.occString occ)
+    bogus_ns = OccName.varName -- Not yet recorded in the TH name
+                               -- but only the unique matters
+
+lookupThName th_name@(TH.Name occ flavour)     -- NameS or NameQ
+  =  do { let occ = OccName.mkOccFS ns occ_fs
+             rdr_name = case flavour of
+                           TH.NameS   -> mkRdrUnqual occ
+                           TH.NameQ m -> mkRdrQual (mkModule (TH.modString m)) occ
        ; rdr_env <- getLocalRdrEnv
        ; case lookupLocalRdrEnv rdr_env rdr_name of
                Just name -> return name
@@ -480,13 +491,6 @@ lookupThName th_name@(TH.Name occ TH.NameS)
        | otherwise      = OccName.varName
     occ_fs = mkFastString (TH.occString occ)
 
-lookupThName (TH.Name occ (TH.NameU uniq)) 
-  = return (mkInternalName (mk_uniq uniq) (OccName.mkOccFS bogus_ns occ_fs) noSrcLoc)
-  where
-    occ_fs = mkFastString (TH.occString occ)
-    bogus_ns = OccName.varName -- Not yet recorded in the TH name
-                               -- but only the unique matters
-
 tcLookupTh :: Name -> TcM TcTyThing
 -- This is a specialised version of TcEnv.tcLookup; specialised mainly in that
 -- it gives a reify-related error message on failure, whereas in the normal
@@ -540,8 +544,8 @@ reifyThing (AGlobal (AnId id))
            other            -> return (TH.VarI     v ty Nothing fix)
     }
 
-reifyThing (AGlobal (ATyCon tc))   = do { dec <- reifyTyCon tc;  return (TH.TyConI dec) }
-reifyThing (AGlobal (AClass cls))  = do { dec <- reifyClass cls; return (TH.ClassI dec) }
+reifyThing (AGlobal (ATyCon tc))  = reifyTyCon tc
+reifyThing (AGlobal (AClass cls)) = reifyClass cls
 reifyThing (AGlobal (ADataCon dc))
   = do { let name = dataConName dc
        ; ty <- reifyType (idType (dataConWrapId dc))
@@ -555,31 +559,33 @@ reifyThing (ATcId id _ _)
        ; fix <- reifyFixity (idName id)
        ; return (TH.VarI (reifyName id) ty2 Nothing fix) }
 
-reifyThing (ATyVar tv) 
-  = do { ty1 <- zonkTcTyVar tv
+reifyThing (ATyVar tv ty) 
+  = do { ty1 <- zonkTcType ty
        ; ty2 <- reifyType ty1
        ; return (TH.TyVarI (reifyName tv) ty2) }
 
 ------------------------------
-reifyTyCon :: TyCon -> TcM TH.Dec
+reifyTyCon :: TyCon -> TcM TH.Info
 reifyTyCon tc
+  | isFunTyCon tc  = return (TH.PrimTyConI (reifyName tc) 2              False)
+  | isPrimTyCon tc = return (TH.PrimTyConI (reifyName tc) (tyConArity tc) (isUnLiftedTyCon tc))
   | isSynTyCon tc
   = do { let (tvs, rhs) = getSynTyConDefn tc
        ; rhs' <- reifyType rhs
-       ; return (TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
+       ; return (TH.TyConI $ TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
 
 reifyTyCon tc
   = case algTyConRhs tc of
       NewTyCon data_con _ _ 
        -> do   { con <- reifyDataCon data_con
-               ; return (TH.NewtypeD [] (reifyName tc) (reifyTyVars (tyConTyVars tc))
-                                     con [{- Don't know about deriving -}]) }
+               ; return (TH.TyConI $ TH.NewtypeD [] (reifyName tc) (reifyTyVars (tyConTyVars tc))
+                                                 con [{- Don't know about deriving -}]) }
 
       DataTyCon mb_cxt cons _
        -> do   { cxt <- reifyCxt (mb_cxt `orElse` [])
                ; cons <- mapM reifyDataCon (tyConDataCons tc)
-               ; return (TH.DataD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
-                                     cons [{- Don't know about deriving -}]) }
+               ; return (TH.TyConI $ TH.DataD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
+                                              cons [{- Don't know about deriving -}]) }
 
 reifyDataCon :: DataCon -> TcM TH.Con
 reifyDataCon dc
@@ -604,11 +610,11 @@ reifyDataCon dc
                <+> quotes (ppr dc))
 
 ------------------------------
-reifyClass :: Class -> TcM TH.Dec
+reifyClass :: Class -> TcM TH.Info
 reifyClass cls 
   = do { cxt <- reifyCxt theta
        ; ops <- mapM reify_op op_stuff
-       ; return (TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) fds' ops) }
+       ; return (TH.ClassI $ TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) fds' ops) }
   where
     (tvs, fds, theta, _, op_stuff) = classExtraBigSig cls
     fds' = map reifyFunDep fds
@@ -650,6 +656,10 @@ reifyName :: NamedThing n => n -> TH.Name
 reifyName thing
   | isExternalName name = mk_varg mod occ_str
   | otherwise          = TH.mkNameU occ_str (getKey (getUnique name))
+       -- Many of the things we reify have local bindings, and 
+       -- NameL's aren't supposed to appear in binding positions, so
+       -- we use NameU.  When/if we start to reify nested things, that
+       -- have free variables, we may need to generate NameL's for them.
   where
     name    = getName thing
     mod     = moduleUserString (nameModule name)