+
+
+%************************************************************************
+%* *
+ Reification
+%* *
+%************************************************************************
+
+
+\begin{code}
+reify :: TH.Name -> TcM TH.Info
+reify th_name
+ = do { name <- lookupThName th_name
+ ; thing <- tcLookupTh name
+ -- ToDo: this tcLookup could fail, which would give a
+ -- rather unhelpful error message
+ ; traceIf (text "reify" <+> text (show th_name) <+> brackets (ppr_ns th_name) <+> ppr name)
+ ; reifyThing thing
+ }
+ where
+ ppr_ns (TH.Name _ (TH.NameG TH.DataName mod)) = text "data"
+ ppr_ns (TH.Name _ (TH.NameG TH.TcClsName mod)) = text "tc"
+ ppr_ns (TH.Name _ (TH.NameG TH.VarName mod)) = text "var"
+
+lookupThName :: TH.Name -> TcM Name
+lookupThName th_name@(TH.Name occ flavour)
+ = do { let rdr_name = thRdrName guessed_ns occ_str flavour
+
+ -- Repeat much of lookupOccRn, becase we want
+ -- to report errors in a TH-relevant way
+ ; rdr_env <- getLocalRdrEnv
+ ; case lookupLocalRdrEnv rdr_env rdr_name of
+ Just name -> return name
+ Nothing | not (isSrcRdrName rdr_name) -- Exact, Orig
+ -> lookupImportedName rdr_name
+ | otherwise -- Unqual, Qual
+ -> do {
+ mb_name <- lookupSrcOcc_maybe rdr_name
+ ; case mb_name of
+ Just name -> return name
+ Nothing -> failWithTc (notInScope th_name) }
+ }
+ where
+ -- guessed_ns is the name space guessed from looking at the TH name
+ guessed_ns | isLexCon (mkFastString occ_str) = OccName.dataName
+ | otherwise = OccName.varName
+ occ_str = TH.occString occ
+
+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
+-- tcLookup, failure is a bug.
+tcLookupTh name
+ = do { (gbl_env, lcl_env) <- getEnvs
+ ; case lookupNameEnv (tcl_env lcl_env) name of {
+ Just thing -> returnM thing;
+ Nothing -> do
+ { if nameIsLocalOrFrom (tcg_mod gbl_env) name
+ then -- It's defined in this module
+ case lookupNameEnv (tcg_type_env gbl_env) name of
+ Just thing -> return (AGlobal thing)
+ Nothing -> failWithTc (notInEnv name)
+
+ else do -- It's imported
+ { (eps,hpt) <- getEpsAndHpt
+ ; case lookupType hpt (eps_PTE eps) name of
+ Just thing -> return (AGlobal thing)
+ Nothing -> do { thing <- tcImportDecl name
+ ; return (AGlobal thing) }
+ -- Imported names should always be findable;
+ -- if not, we fail hard in tcImportDecl
+ }}}}
+
+notInScope :: TH.Name -> SDoc
+notInScope th_name = quotes (text (TH.pprint th_name)) <+>
+ ptext SLIT("is not in scope at a reify")
+ -- Ugh! Rather an indirect way to display the name
+
+notInEnv :: Name -> SDoc
+notInEnv name = quotes (ppr name) <+>
+ ptext SLIT("is not in the type environment at a reify")
+
+------------------------------
+reifyThing :: TcTyThing -> TcM TH.Info
+-- The only reason this is monadic is for error reporting,
+-- which in turn is mainly for the case when TH can't express
+-- some random GHC extension
+
+reifyThing (AGlobal (AnId id))
+ = do { ty <- reifyType (idType id)
+ ; fix <- reifyFixity (idName id)
+ ; let v = reifyName id
+ ; case globalIdDetails id of
+ ClassOpId cls -> return (TH.ClassOpI v ty (reifyName cls) fix)
+ other -> return (TH.VarI v ty Nothing fix)
+ }
+
+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))
+ ; fix <- reifyFixity name
+ ; return (TH.DataConI (reifyName name) ty (reifyName (dataConTyCon dc)) fix) }
+
+reifyThing (ATcId id _)
+ = do { ty1 <- zonkTcType (idType id) -- Make use of all the info we have, even
+ -- though it may be incomplete
+ ; ty2 <- reifyType ty1
+ ; fix <- reifyFixity (idName id)
+ ; return (TH.VarI (reifyName id) ty2 Nothing fix) }
+
+reifyThing (ATyVar tv ty)
+ = do { ty1 <- zonkTcType ty
+ ; ty2 <- reifyType ty1
+ ; return (TH.TyVarI (reifyName tv) ty2) }
+
+------------------------------
+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.TyConI $ TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
+
+reifyTyCon tc
+ = do { cxt <- reifyCxt (tyConStupidTheta tc)
+ ; cons <- mapM reifyDataCon (tyConDataCons tc)
+ ; let name = reifyName tc
+ tvs = reifyTyVars (tyConTyVars tc)
+ deriv = [] -- Don't know about deriving
+ decl | isNewTyCon tc = TH.NewtypeD cxt name tvs (head cons) deriv
+ | otherwise = TH.DataD cxt name tvs cons deriv
+ ; return (TH.TyConI decl) }
+
+reifyDataCon :: DataCon -> TcM TH.Con
+reifyDataCon dc
+ | isVanillaDataCon dc
+ = do { arg_tys <- reifyTypes (dataConOrigArgTys dc)
+ ; let stricts = map reifyStrict (dataConStrictMarks dc)
+ fields = dataConFieldLabels dc
+ name = reifyName dc
+ [a1,a2] = arg_tys
+ [s1,s2] = stricts
+ ; ASSERT( length arg_tys == length stricts )
+ if not (null fields) then
+ return (TH.RecC name (zip3 (map reifyName fields) stricts arg_tys))
+ else
+ if dataConIsInfix dc then
+ ASSERT( length arg_tys == 2 )
+ return (TH.InfixC (s1,a1) name (s2,a2))
+ else
+ return (TH.NormalC name (stricts `zip` arg_tys)) }
+ | otherwise
+ = failWithTc (ptext SLIT("Can't reify a non-Haskell-98 data constructor:")
+ <+> quotes (ppr dc))
+
+------------------------------
+reifyClass :: Class -> TcM TH.Info
+reifyClass cls
+ = do { cxt <- reifyCxt theta
+ ; ops <- mapM reify_op op_stuff
+ ; return (TH.ClassI $ TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) fds' ops) }
+ where
+ (tvs, fds, theta, _, op_stuff) = classExtraBigSig cls
+ fds' = map reifyFunDep fds
+ reify_op (op, _) = do { ty <- reifyType (idType op)
+ ; return (TH.SigD (reifyName op) ty) }
+
+------------------------------
+reifyType :: TypeRep.Type -> TcM TH.Type
+reifyType (TyVarTy tv) = return (TH.VarT (reifyName tv))
+reifyType (TyConApp tc tys) = reify_tc_app (reifyName tc) tys
+reifyType (NoteTy _ ty) = reifyType ty
+reifyType (AppTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (r1 `TH.AppT` r2) }
+reifyType (FunTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (TH.ArrowT `TH.AppT` r1 `TH.AppT` r2) }
+reifyType ty@(ForAllTy _ _) = do { cxt' <- reifyCxt cxt;
+ ; tau' <- reifyType tau
+ ; return (TH.ForallT (reifyTyVars tvs) cxt' tau') }
+ where
+ (tvs, cxt, tau) = tcSplitSigmaTy ty
+reifyTypes = mapM reifyType
+reifyCxt = mapM reifyPred
+
+reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
+reifyFunDep (xs, ys) = TH.FunDep (map reifyName xs) (map reifyName ys)
+
+reifyTyVars :: [TyVar] -> [TH.Name]
+reifyTyVars = map reifyName
+
+reify_tc_app :: TH.Name -> [TypeRep.Type] -> TcM TH.Type
+reify_tc_app tc tys = do { tys' <- reifyTypes tys
+ ; return (foldl TH.AppT (TH.ConT tc) tys') }
+
+reifyPred :: TypeRep.PredType -> TcM TH.Type
+reifyPred (ClassP cls tys) = reify_tc_app (reifyName cls) tys
+reifyPred p@(IParam _ _) = noTH SLIT("implicit parameters") (ppr p)
+
+
+------------------------------
+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 = moduleString (nameModule name)
+ occ_str = occNameString occ
+ occ = nameOccName name
+ mk_varg | OccName.isDataOcc occ = TH.mkNameG_d
+ | OccName.isVarOcc occ = TH.mkNameG_v
+ | OccName.isTcOcc occ = TH.mkNameG_tc
+ | otherwise = pprPanic "reifyName" (ppr name)
+
+------------------------------
+reifyFixity :: Name -> TcM TH.Fixity
+reifyFixity name
+ = do { fix <- lookupFixityRn name
+ ; return (conv_fix fix) }
+ where
+ conv_fix (BasicTypes.Fixity i d) = TH.Fixity i (conv_dir d)
+ conv_dir BasicTypes.InfixR = TH.InfixR
+ conv_dir BasicTypes.InfixL = TH.InfixL
+ conv_dir BasicTypes.InfixN = TH.InfixN
+
+reifyStrict :: BasicTypes.StrictnessMark -> TH.Strict
+reifyStrict MarkedStrict = TH.IsStrict
+reifyStrict MarkedUnboxed = TH.IsStrict
+reifyStrict NotMarkedStrict = TH.NotStrict
+
+------------------------------
+noTH :: LitString -> SDoc -> TcM a
+noTH s d = failWithTc (hsep [ptext SLIT("Can't represent") <+> ptext s <+>
+ ptext SLIT("in Template Haskell:"),
+ nest 2 d])
+\end{code}