RnSplice.hs 28.1 KB
Newer Older
1 2
{-# LANGUAGE CPP #-}

3
module RnSplice (
4 5 6
        rnTopSpliceDecls,
        rnSpliceType, rnSpliceExpr, rnSplicePat, rnSpliceDecl,
        rnBracket,
gmainland's avatar
gmainland committed
7
        checkThLocalName
8 9 10
#ifdef GHCI
        , traceSplice, SpliceInfo(..)
#endif
11 12
  ) where

13
#include "HsVersions.h"
14

15 16 17 18
import Name
import NameSet
import HsSyn
import RdrName
gmainland's avatar
gmainland committed
19
import TcRnMonad
20
import Kind
gmainland's avatar
gmainland committed
21 22

#ifdef GHCI
23
import ErrUtils         ( dumpIfSet_dyn_printer )
gmainland's avatar
gmainland committed
24 25
import Control.Monad    ( unless, when )
import DynFlags
26
import DsMeta           ( decsQTyConName, expQTyConName, patQTyConName, typeQTyConName, liftName )
gmainland's avatar
gmainland committed
27 28
import LoadIface        ( loadInterfaceForName )
import Module
29
import RnEnv
30
import RnPat            ( rnPat )
31
import RnSource         ( rnSrcDecls, findSplice )
32
import RnTypes          ( rnLHsType )
33
import PrelNames        ( isUnboundName )
34
import SrcLoc
35
import TcEnv            ( checkWellStaged, tcMetaTy )
36
import Outputable
37
import BasicTypes       ( TopLevelFlag, isTopLevel )
38
import FastString
39
import Hooks
40 41 42
import Var              ( Id )
import DsMeta           ( quoteExpName, quotePatName, quoteDecName, quoteTypeName )
import Util
43

gmainland's avatar
gmainland committed
44 45
import {-# SOURCE #-} RnExpr   ( rnLExpr )
import {-# SOURCE #-} TcExpr   ( tcMonoExpr )
46
import {-# SOURCE #-} TcSplice ( runMetaD, runMetaE, runMetaP, runMetaT, tcTopSpliceExpr )
gmainland's avatar
gmainland committed
47 48 49 50
#endif

#ifndef GHCI
rnBracket :: HsExpr RdrName -> HsBracket RdrName -> RnM (HsExpr Name, FreeVars)
51
rnBracket e _ = failTH e "Template Haskell bracket"
gmainland's avatar
gmainland committed
52

53 54
rnTopSpliceDecls :: HsSplice RdrName -> RnM ([LHsDecl RdrName], FreeVars)
rnTopSpliceDecls e = failTH e "Template Haskell top splice"
55

56 57
rnSpliceType :: HsSplice RdrName -> PostTc Name Kind
             -> RnM (HsType Name, FreeVars)
58
rnSpliceType e _ = failTH e "Template Haskell type splice"
gmainland's avatar
gmainland committed
59

60 61
rnSpliceExpr :: HsSplice RdrName -> RnM (HsExpr Name, FreeVars)
rnSpliceExpr e = failTH e "Template Haskell splice"
gmainland's avatar
gmainland committed
62

63
rnSplicePat :: HsSplice RdrName -> RnM (Either (Pat RdrName) (Pat Name), FreeVars)
64
rnSplicePat e = failTH e "Template Haskell pattern splice"
gmainland's avatar
gmainland committed
65

66
rnSpliceDecl :: SpliceDecl RdrName -> RnM (SpliceDecl Name, FreeVars)
67
rnSpliceDecl e = failTH e "Template Haskell declaration splice"
gmainland's avatar
gmainland committed
68
#else
69

Austin Seipp's avatar
Austin Seipp committed
70 71 72
{-
*********************************************************
*                                                      *
73
                Splices
Austin Seipp's avatar
Austin Seipp committed
74 75
*                                                      *
*********************************************************
76

77 78 79
Note [Free variables of typed splices]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider renaming this:
80 81 82
        f = ...
        h = ...$(thing "f")...

83 84 85 86 87 88 89 90 91
where the splice is a *typed* splice.  The splice can expand into
literally anything, so when we do dependency analysis we must assume
that it might mention 'f'.  So we simply treat all locally-defined
names as mentioned by any splice.  This is terribly brutal, but I
don't see what else to do.  For example, it'll mean that every
locally-defined thing will appear to be used, so no unused-binding
warnings.  But if we miss the dependency, then we might typecheck 'h'
before 'f', and that will crash the type checker because 'f' isn't in
scope.
92 93 94 95 96 97

Currently, I'm not treating a splice as also mentioning every import,
which is a bit inconsistent -- but there are a lot of them.  We might
thereby get some bogus unused-import warnings, but we won't crash the
type checker.  Not very satisfactory really.

98 99 100 101 102 103
Note [Renamer errors]
~~~~~~~~~~~~~~~~~~~~~
It's important to wrap renamer calls in checkNoErrs, because the
renamer does not fail for out of scope variables etc. Instead it
returns a bogus term/type, so that it can report more than one error.
We don't want the type checker to see these bogus unbound variables.
Austin Seipp's avatar
Austin Seipp committed
104
-}
105

106
rnSpliceGen :: (HsSplice Name -> RnM (a, FreeVars))     -- Outside brackets, run splice
107 108 109
            -> (HsSplice Name -> (PendingRnSplice, a))  -- Inside brackets, make it pending
            -> HsSplice RdrName
            -> RnM (a, FreeVars)
110 111
rnSpliceGen run_splice pend_splice splice
  = addErrCtxt (spliceCtxt splice) $ do
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    { stage <- getStage
    ; case stage of
        Brack pop_stage RnPendingTyped
          -> do { checkTc is_typed_splice illegalUntypedSplice
                ; (splice', fvs) <- setStage pop_stage $
                                    rnSplice splice
                ; let (_pending_splice, result) = pend_splice splice'
                ; return (result, fvs) }

        Brack pop_stage (RnPendingUntyped ps_var)
          -> do { checkTc (not is_typed_splice) illegalTypedSplice
                ; (splice', fvs) <- setStage pop_stage $
                                    rnSplice splice
                ; let (pending_splice, result) = pend_splice splice'
                ; ps <- readMutVar ps_var
                ; writeMutVar ps_var (pending_splice : ps)
                ; return (result, fvs) }

130 131
        _ ->  do { (splice', fvs1) <- checkNoErrs $
                                      setStage (Splice is_typed_splice) $
132
                                      rnSplice splice
133 134 135
                   -- checkNoErrs: don't attempt to run the splice if
                   -- renaming it failed; otherwise we get a cascade of
                   -- errors from e.g. unbound variables
136 137
                 ; (result, fvs2) <- run_splice splice'
                 ; return (result, fvs1 `plusFV` fvs2) } }
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
   where
     is_typed_splice = isTypedSplice splice

------------------
runRnSplice :: UntypedSpliceFlavour
            -> (LHsExpr Id -> TcRn res)
            -> (res -> SDoc)    -- How to pretty-print res
                                -- Usually just ppr, but not for [Decl]
            -> HsSplice Name    -- Always untyped
            -> TcRn res
runRnSplice flavour run_meta ppr_res splice
  = do { splice' <- getHooked runRnSpliceHook return >>= ($ splice)

       ; let the_expr = case splice' of
                  HsUntypedSplice _ e     ->  e
                  HsQuasiQuote _ q qs str -> mkQuasiQuoteExpr flavour q qs str
                  HsTypedSplice {}        -> pprPanic "runRnSplice" (ppr splice)

             -- Typecheck the expression
       ; meta_exp_ty   <- tcMetaTy meta_ty_name
       ; zonked_q_expr <- tcTopSpliceExpr False $
                          tcMonoExpr the_expr meta_exp_ty

             -- Run the expression
       ; result <- run_meta zonked_q_expr
       ; traceSplice (SpliceInfo { spliceDescription = what
                                 , spliceIsDecl      = is_decl
                                 , spliceSource      = Just the_expr
                                 , spliceGenerated   = ppr_res result })

       ; return result }

  where
    meta_ty_name = case flavour of
                       UntypedExpSplice  -> expQTyConName
                       UntypedPatSplice  -> patQTyConName
                       UntypedTypeSplice -> typeQTyConName
                       UntypedDeclSplice -> decsQTyConName
    what = case flavour of
                  UntypedExpSplice  -> "expression"
                  UntypedPatSplice  -> "pattern"
                  UntypedTypeSplice -> "type"
                  UntypedDeclSplice -> "declarations"
    is_decl = case flavour of
                 UntypedDeclSplice -> True
                 _                 -> False

------------------
makePending :: UntypedSpliceFlavour
            -> HsSplice Name
            -> PendingRnSplice
makePending flavour (HsUntypedSplice n e)
  = PendingRnSplice flavour n e
makePending flavour (HsQuasiQuote n quoter q_span quote)
  = PendingRnSplice flavour n (mkQuasiQuoteExpr flavour quoter q_span quote)
makePending _ splice@(HsTypedSplice {})
  = pprPanic "makePending" (ppr splice)

------------------
mkQuasiQuoteExpr :: UntypedSpliceFlavour -> Name -> SrcSpan -> FastString -> LHsExpr Name
-- Return the expression (quoter "...quote...")
-- which is what we must run in a quasi-quote
mkQuasiQuoteExpr flavour quoter q_span quote
  = L q_span $ HsApp (L q_span $
                      HsApp (L q_span (HsVar quote_selector)) quoterExpr)
                     quoteExpr
  where
    quoterExpr = L q_span $! HsVar $! quoter
    quoteExpr  = L q_span $! HsLit $! HsString "" quote
    quote_selector = case flavour of
                       UntypedExpSplice  -> quoteExpName
                       UntypedPatSplice  -> quotePatName
                       UntypedTypeSplice -> quoteTypeName
                       UntypedDeclSplice -> quoteDecName
212 213

---------------------
214
rnSplice :: HsSplice RdrName -> RnM (HsSplice Name, FreeVars)
215
-- Not exported...used for all
216 217 218 219 220 221 222 223 224
rnSplice (HsTypedSplice splice_name expr)
  = do  { checkTH expr "Template Haskell typed splice"
        ; loc  <- getSrcSpanM
        ; n' <- newLocalBndrRn (L loc splice_name)
        ; (expr', fvs) <- rnLExpr expr
        ; return (HsTypedSplice n' expr', fvs) }

rnSplice (HsUntypedSplice splice_name expr)
  = do  { checkTH expr "Template Haskell untyped splice"
225
        ; loc  <- getSrcSpanM
226
        ; n' <- newLocalBndrRn (L loc splice_name)
227
        ; (expr', fvs) <- rnLExpr expr
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        ; return (HsUntypedSplice n' expr', fvs) }

rnSplice (HsQuasiQuote splice_name quoter q_loc quote)
  = do  { checkTH quoter "Template Haskell quasi-quote"
        ; loc  <- getSrcSpanM
        ; splice_name' <- newLocalBndrRn (L loc splice_name)

          -- Drop the leading "$" from the quoter name, if present
          -- This is old-style syntax, now deprecated
          -- NB: when removing this backward-compat, remove
          --     the matching code in Lexer.x (around line 310)
        ; let occ_str = occNameString (rdrNameOcc quoter)
        ; quoter <- if ASSERT( not (null occ_str) )  -- Lexer ensures this
                       head occ_str /= '$'
                    then return quoter
                    else do { addWarn (deprecatedDollar quoter)
                            ; return (mkRdrUnqual (mkVarOcc (tail occ_str))) }

          -- Rename the quoter; akin to the HsVar case of rnExpr
        ; quoter' <- lookupOccRn quoter
        ; this_mod <- getModule
        ; when (nameIsLocalOrFrom this_mod quoter') $
          checkThLocalName quoter'

        ; return (HsQuasiQuote splice_name' quoter' q_loc quote, unitFV quoter') }

deprecatedDollar :: RdrName -> SDoc
deprecatedDollar quoter
  = hang (ptext (sLit "Deprecated syntax:"))
       2 (ptext (sLit "quasiquotes no longer need a dollar sign:")
          <+> ppr quoter)

260

261
---------------------
262 263 264
rnSpliceExpr :: HsSplice RdrName -> RnM (HsExpr Name, FreeVars)
rnSpliceExpr splice
  = rnSpliceGen run_expr_splice pend_expr_splice splice
gmainland's avatar
gmainland committed
265
  where
266
    pend_expr_splice :: HsSplice Name -> (PendingRnSplice, HsExpr Name)
267 268
    pend_expr_splice rn_splice
        = (makePending UntypedExpSplice rn_splice, HsSpliceE rn_splice)
269 270

    run_expr_splice :: HsSplice Name -> RnM (HsExpr Name, FreeVars)
271 272
    run_expr_splice rn_splice
      | isTypedSplice rn_splice   -- Run it later, in the type checker
273
      = do {  -- Ugh!  See Note [Splices] above
274
             lcl_rdr <- getLocalRdrEnv
275 276 277 278
           ; gbl_rdr <- getGlobalRdrEnv
           ; let gbl_names = mkNameSet [gre_name gre | gre <- globalRdrEnvElts gbl_rdr
                                                     , isLocalGRE gre]
                 lcl_names = mkNameSet (localRdrEnvElts lcl_rdr)
gmainland's avatar
gmainland committed
279

280
           ; return (HsSpliceE rn_splice, lcl_names `plusFV` gbl_names) }
281 282

      | otherwise  -- Run it here
283 284 285
      = do { rn_expr <- runRnSplice UntypedExpSplice runMetaE ppr rn_splice
           ; (lexpr3, fvs) <- checkNoErrs (rnLExpr rn_expr)
           ; return (HsPar lexpr3, fvs)  }
286 287

----------------------
288 289
rnSpliceType :: HsSplice RdrName -> PostTc Name Kind
             -> RnM (HsType Name, FreeVars)
290
rnSpliceType splice k
291
  = rnSpliceGen run_type_splice pend_type_splice splice
292
  where
293 294
    pend_type_splice rn_splice
       = (makePending UntypedTypeSplice rn_splice, HsSpliceTy rn_splice k)
gmainland's avatar
gmainland committed
295

296 297 298 299 300 301 302 303
    run_type_splice rn_splice
      = do { hs_ty2 <- runRnSplice UntypedTypeSplice runMetaT ppr rn_splice
           ; (hs_ty3, fvs) <- do { let doc = SpliceTypeCtx hs_ty2
                                 ; checkNoErrs $ rnLHsType doc hs_ty2 }
                                    -- checkNoErrs: see Note [Renamer errors]
           ; return (HsParTy hs_ty3, fvs) }
              -- Wrap the result of the splice in parens so that we don't
              -- lose the outermost location set by runQuasiQuote (#7918)
304

305 306 307 308 309 310 311 312 313
----------------------
-- | Rename a splice pattern. See Note [rnSplicePat]
rnSplicePat :: HsSplice RdrName -> RnM ( Either (Pat RdrName) (Pat Name)
                                       , FreeVars)
rnSplicePat splice
  = rnSpliceGen run_pat_splice pend_pat_splice splice
  where
    pend_pat_splice rn_splice
      = (makePending UntypedPatSplice rn_splice, Right (SplicePat rn_splice))
314

315 316 317 318 319
    run_pat_splice rn_splice
      = do { pat <- runRnSplice UntypedPatSplice runMetaP ppr rn_splice
           ; return (Left (ParPat pat), emptyFVs) }
              -- Wrap the result of the quasi-quoter in parens so that we don't
              -- lose the outermost location set by runQuasiQuote (#7918)
320

321 322 323 324 325 326 327
----------------------
rnSpliceDecl :: SpliceDecl RdrName -> RnM (SpliceDecl Name, FreeVars)
rnSpliceDecl (SpliceDecl (L loc splice) flg)
  = rnSpliceGen run_decl_splice pend_decl_splice splice
  where
    pend_decl_splice rn_splice
       = (makePending UntypedDeclSplice rn_splice, SpliceDecl (L loc rn_splice) flg)
328

329 330 331 332 333 334 335 336 337 338 339 340
    run_decl_splice rn_splice = pprPanic "rnSpliceDecl" (ppr rn_splice)

rnTopSpliceDecls :: HsSplice RdrName -> RnM ([LHsDecl RdrName], FreeVars)
-- Declaration splice at the very top level of the module
rnTopSpliceDecls splice
   = do  { (rn_splice, fvs) <- setStage (Splice False) $
                               rnSplice splice
         ; decls <- runRnSplice UntypedDeclSplice runMetaD ppr_decls rn_splice
         ; return (decls,fvs) }
   where
     ppr_decls :: [LHsDecl RdrName] -> SDoc
     ppr_decls ds = vcat (map ppr ds)
341

Austin Seipp's avatar
Austin Seipp committed
342
{-
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
Note [rnSplicePat]
~~~~~~~~~~~~~~~~~~
Renaming a pattern splice is a bit tricky, because we need the variables
bound in the pattern to be in scope in the RHS of the pattern. This scope
management is effectively done by using continuation-passing style in
RnPat, through the CpsRn monad. We don't wish to be in that monad here
(it would create import cycles and generally conflict with renaming other
splices), so we really want to return a (Pat RdrName) -- the result of
running the splice -- which can then be further renamed in RnPat, in
the CpsRn monad.

The problem is that if we're renaming a splice within a bracket, we
*don't* want to run the splice now. We really do just want to rename
it to an HsSplice Name. Of course, then we can't know what variables
are bound within the splice, so pattern splices within brackets aren't
all that useful.

In any case, when we're done in rnSplicePat, we'll either have a
Pat RdrName (the result of running a top-level splice) or a Pat Name
(the renamed nested splice). Thus, the awkward return type of
rnSplicePat.
Austin Seipp's avatar
Austin Seipp committed
364
-}
365

Austin Seipp's avatar
Austin Seipp committed
366 367 368
{-
************************************************************************
*                                                                      *
369
        Template Haskell brackets
Austin Seipp's avatar
Austin Seipp committed
370 371 372
*                                                                      *
************************************************************************
-}
373

gmainland's avatar
gmainland committed
374 375
rnBracket :: HsExpr RdrName -> HsBracket RdrName -> RnM (HsExpr Name, FreeVars)
rnBracket e br_body
376
  = addErrCtxt (quotationCtxtDoc br_body) $
377 378
    do { -- Check that Template Haskell is enabled and available
         thEnabled <- xoptM Opt_TemplateHaskell
gmainland's avatar
gmainland committed
379 380 381
       ; unless thEnabled $
           failWith ( vcat [ ptext (sLit "Syntax error on") <+> ppr e
                           , ptext (sLit "Perhaps you intended to use TemplateHaskell") ] )
382
       ; checkTH e "Template Haskell bracket"
383 384 385 386 387 388 389 390 391 392 393 394 395

         -- Check for nested brackets
       ; cur_stage <- getStage
       ; case cur_stage of
           { Splice True  -> checkTc (isTypedBracket br_body) illegalUntypedBracket
           ; Splice False -> checkTc (not (isTypedBracket br_body)) illegalTypedBracket
           ; Comp         -> return ()
           ; Brack {}     -> failWithTc illegalBracket
           }

         -- Brackets are desugared to code that mentions the TH package
       ; recordThUse

396 397 398 399
       ; case isTypedBracket br_body of
            True  -> do { (body', fvs_e) <- setStage (Brack cur_stage RnPendingTyped) $
                                            rn_bracket cur_stage br_body
                        ; return (HsBracket body', fvs_e) }
400

401 402 403 404 405
            False -> do { ps_var <- newMutVar []
                        ; (body', fvs_e) <- setStage (Brack cur_stage (RnPendingUntyped ps_var)) $
                                            rn_bracket cur_stage br_body
                        ; pendings <- readMutVar ps_var
                        ; return (HsRnBracketOut body' pendings, fvs_e) }
gmainland's avatar
gmainland committed
406 407
       }

408
rn_bracket :: ThStage -> HsBracket RdrName -> RnM (HsBracket Name, FreeVars)
409 410
rn_bracket outer_stage br@(VarBr flg rdr_name)
  = do { name <- lookupOccRn rdr_name
411
       ; this_mod <- getModule
412

gmainland's avatar
gmainland committed
413 414 415 416
       ; case flg of
           { -- Type variables can be quoted in TH. See #5721.
             False -> return ()
           ; True | nameIsLocalOrFrom this_mod name ->
417
                 do { mb_bind_lvl <- lookupLocalOccThLvl_maybe name
gmainland's avatar
gmainland committed
418
                    ; case mb_bind_lvl of
419
                        { Nothing -> return ()      -- Can happen for data constructors,
Gabor Greif's avatar
Gabor Greif committed
420
                                                    -- but nothing needs to be done for them
421 422 423 424 425 426 427 428

                        ; Just (top_lvl, bind_lvl)  -- See Note [Quoting names]
                             | isTopLevel top_lvl
                             -> when (isExternalName name) (keepAlive name)
                             | otherwise
                             -> do { traceRn (text "rn_bracket VarBr" <+> ppr name <+> ppr bind_lvl <+> ppr outer_stage)
                                   ; checkTc (thLevel outer_stage + 1 == bind_lvl)
                                             (quotedNameStageErr br) }
gmainland's avatar
gmainland committed
429 430
                        }
                    }
431 432 433 434 435
           ; True | otherwise ->  -- Imported thing
                 discardResult (loadInterfaceForName msg name)
                     -- Reason for loadInterface: deprecation checking
                     -- assumes that the home interface is loaded, and
                     -- this is the only way that is going to happen
gmainland's avatar
gmainland committed
436
           }
437 438 439 440
       ; return (VarBr flg name, unitFV name) }
  where
    msg = ptext (sLit "Need interface for Template Haskell quoted Name")

441 442
rn_bracket _ (ExpBr e) = do { (e', fvs) <- rnLExpr e
                            ; return (ExpBr e', fvs) }
443

444
rn_bracket _ (PatBr p) = rnPat ThPatQuote p $ \ p' -> return (PatBr p', emptyFVs)
445

446 447
rn_bracket _ (TypBr t) = do { (t', fvs) <- rnLHsType TypBrCtx t
                            ; return (TypBr t', fvs) }
448

449
rn_bracket _ (DecBrL decls)
450
  = do { group <- groupDecls decls
451 452 453 454 455 456 457 458 459 460
       ; gbl_env  <- getGblEnv
       ; let new_gbl_env = gbl_env { tcg_dus = emptyDUs }
                          -- The emptyDUs is so that we just collect uses for this
                          -- group alone in the call to rnSrcDecls below
       ; (tcg_env, group') <- setGblEnv new_gbl_env $
                              rnSrcDecls [] group
   -- The empty list is for extra dependencies coming from .hs-boot files
   -- See Note [Extra dependencies from .hs-boot files] in RnSource

              -- Discard the tcg_env; it contains only extra info about fixity
gmainland's avatar
gmainland committed
461
        ; traceRn (text "rn_bracket dec" <+> (ppr (tcg_dus tcg_env) $$
462 463
                   ppr (duUses (tcg_dus tcg_env))))
        ; return (DecBrG group', duUses (tcg_dus tcg_env)) }
464 465 466 467 468 469 470 471 472 473 474 475
  where
    groupDecls :: [LHsDecl RdrName] -> RnM (HsGroup RdrName)
    groupDecls decls
      = do { (group, mb_splice) <- findSplice decls
           ; case mb_splice of
           { Nothing -> return group
           ; Just (splice, rest) ->
               do { group' <- groupDecls rest
                  ; let group'' = appendGroups group group'
                  ; return group'' { hs_splcds = noLoc splice : hs_splcds group' }
                  }
           }}
476

477 478 479 480 481
rn_bracket _ (DecBrG _) = panic "rn_bracket: unexpected DecBrG"

rn_bracket _ (TExpBr e) = do { (e', fvs) <- rnLExpr e
                             ; return (TExpBr e', fvs) }

482 483 484 485 486 487 488 489
spliceCtxt :: HsSplice RdrName -> SDoc
spliceCtxt splice
  = hang (ptext (sLit "In the") <+> what) 2 (ppr splice)
  where
    what = case splice of
             HsUntypedSplice {} -> ptext (sLit "untyped splice:")
             HsTypedSplice   {} -> ptext (sLit "typed splice:")
             HsQuasiQuote    {} -> ptext (sLit "quasi-quotation:")
490 491 492 493

-- | The splice data to be logged
data SpliceInfo
  = SpliceInfo
494 495 496 497 498
    { spliceDescription   :: String
    , spliceSource        :: Maybe (LHsExpr Name)  -- Nothing <=> top-level decls
                                                   --        added by addTopDecls
    , spliceIsDecl        :: Bool    -- True <=> put the generate code in a file
                                     --          when -dth-dec-file is on
499 500
    , spliceGenerated     :: SDoc
    }
501 502 503 504 505
        -- Note that 'spliceSource' is *renamed* but not *typechecked*
        -- Reason (a) less typechecking crap
        --        (b) data constructors after type checking have been
        --            changed to their *wrappers*, and that makes them
        --            print always fully qualified
506 507 508 509

-- | outputs splice information for 2 flags which have different output formats:
-- `-ddump-splices` and `-dth-dec-file`
traceSplice :: SpliceInfo -> TcM ()
510 511 512 513 514 515 516 517 518 519 520
traceSplice (SpliceInfo { spliceDescription = sd, spliceSource = mb_src
                        , spliceGenerated = gen, spliceIsDecl = is_decl })
  = do { loc <- case mb_src of
                   Nothing        -> getSrcSpanM
                   Just (L loc _) -> return loc
       ; traceOptTcRn Opt_D_dump_splices (spliceDebugDoc loc)

       ; when is_decl $  -- Raw material for -dth-dec-file
         do { dflags <- getDynFlags
            ; liftIO $ dumpIfSet_dyn_printer alwaysQualify dflags Opt_D_th_dec_file
                                             (spliceCodeDoc loc) } }
521 522
  where
    -- `-ddump-splices`
523 524 525 526 527 528 529 530
    spliceDebugDoc :: SrcSpan -> SDoc
    spliceDebugDoc loc
      = let code = case mb_src of
                     Nothing -> ending
                     Just e  -> nest 2 (ppr e) : ending
            ending = [ text "======>", nest 2 gen ]
        in  hang (ppr loc <> colon <+> text "Splicing" <+> text sd)
               2 (sep code)
531 532

    -- `-dth-dec-file`
533 534 535 536
    spliceCodeDoc :: SrcSpan -> SDoc
    spliceCodeDoc loc
      = vcat [ text "--" <+> ppr loc <> colon <+> text "Splicing" <+> text sd
             , gen ]
gmainland's avatar
gmainland committed
537

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
illegalBracket :: SDoc
illegalBracket = ptext (sLit "Template Haskell brackets cannot be nested (without intervening splices)")

illegalTypedBracket :: SDoc
illegalTypedBracket = ptext (sLit "Typed brackets may only appear in typed slices.")

illegalUntypedBracket :: SDoc
illegalUntypedBracket = ptext (sLit "Untyped brackets may only appear in untyped slices.")

illegalTypedSplice :: SDoc
illegalTypedSplice = ptext (sLit "Typed splices may not appear in untyped brackets")

illegalUntypedSplice :: SDoc
illegalUntypedSplice = ptext (sLit "Untyped splices may not appear in typed brackets")

quotedNameStageErr :: HsBracket RdrName -> SDoc
quotedNameStageErr br
  = sep [ ptext (sLit "Stage error: the non-top-level quoted name") <+> ppr br
        , ptext (sLit "must be used at the same stage at which is is bound")]
557

558 559 560 561 562
quotationCtxtDoc :: HsBracket RdrName -> SDoc
quotationCtxtDoc br_body
  = hang (ptext (sLit "In the Template Haskell quotation"))
         2 (ppr br_body)

563 564 565 566 567
-- spliceResultDoc :: OutputableBndr id => LHsExpr id -> SDoc
-- spliceResultDoc expr
--  = vcat [ hang (ptext (sLit "In the splice:"))
--              2 (char '$' <> pprParendExpr expr)
--        , ptext (sLit "To see what the splice expanded to, use -ddump-splices") ]
gmainland's avatar
gmainland committed
568 569
#endif

570
checkThLocalName :: Name -> RnM ()
gmainland's avatar
gmainland committed
571 572 573
#ifndef GHCI  /* GHCI and TH is off */
--------------------------------------
-- Check for cross-stage lifting
574
checkThLocalName _name
gmainland's avatar
gmainland committed
575 576 577
  = return ()

#else         /* GHCI and TH is on */
Austin Seipp's avatar
Austin Seipp committed
578
checkThLocalName name
579 580 581 582
  | isUnboundName name   -- Do not report two errors for
  = return ()            --   $(not_in_scope args)

  | otherwise
583 584 585 586 587 588
  = do  { traceRn (text "checkThLocalName" <+> ppr name)
        ; mb_local_use <- getStageAndBindLevel name
        ; case mb_local_use of {
             Nothing -> return () ;  -- Not a locally-bound thing
             Just (top_lvl, bind_lvl, use_stage) ->
    do  { let use_lvl = thLevel use_stage
gmainland's avatar
gmainland committed
589
        ; checkWellStaged (quotes (ppr name)) bind_lvl use_lvl
590
        ; traceRn (text "checkThLocalName" <+> ppr name <+> ppr bind_lvl <+> ppr use_stage <+> ppr use_lvl)
gmainland's avatar
gmainland committed
591
        ; when (use_lvl > bind_lvl) $
592
          checkCrossStageLifting top_lvl name use_stage } } }
gmainland's avatar
gmainland committed
593 594

--------------------------------------
595
checkCrossStageLifting :: TopLevelFlag -> Name -> ThStage -> TcM ()
gmainland's avatar
gmainland committed
596 597 598 599 600
-- We are inside brackets, and (use_lvl > bind_lvl)
-- Now we must check whether there's a cross-stage lift to do
-- Examples   \x -> [| x |]
--            [| map |]

601
checkCrossStageLifting top_lvl name (Brack _ (RnPendingUntyped ps_var))
602 603
  | isTopLevel top_lvl
        -- Top-level identifiers in this module,
gmainland's avatar
gmainland committed
604 605 606 607 608 609
        -- (which have External Names)
        -- are just like the imported case:
        -- no need for the 'lifting' treatment
        -- E.g.  this is fine:
        --   f x = x
        --   g y = [| f 3 |]
610 611 612
  = when (isExternalName name) (keepAlive name)
    -- See Note [Keeping things alive for Template Haskell]

gmainland's avatar
gmainland committed
613 614 615 616 617
  | otherwise
  =     -- Nested identifiers, such as 'x' in
        -- E.g. \x -> [| h x |]
        -- We must behave as if the reference to x was
        --      h $(lift x)
618
        -- We use 'x' itself as the SplicePointName, used by
gmainland's avatar
gmainland committed
619 620
        -- the desugarer to stitch it all back together.
        -- If 'x' occurs many times we may get many identical
621
        -- bindings of the same SplicePointName, but that doesn't
gmainland's avatar
gmainland committed
622 623
        -- matter, although it's a mite untidy.
    do  { traceRn (text "checkCrossStageLifting" <+> ppr name)
624 625 626 627 628 629

          -- Construct the (lift x) expression
        ; let lift_expr   = nlHsApp (nlHsVar liftName) (nlHsVar name)
              pend_splice = PendingRnSplice UntypedExpSplice name lift_expr

          -- Update the pending splices
gmainland's avatar
gmainland committed
630
        ; ps <- readMutVar ps_var
631
        ; writeMutVar ps_var (pend_splice : ps) }
632 633

checkCrossStageLifting _ _ _ = return ()
gmainland's avatar
gmainland committed
634
#endif /* GHCI */
635

Austin Seipp's avatar
Austin Seipp committed
636
{-
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
Note [Keeping things alive for Template Haskell]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  f x = x+1
  g y = [| f 3 |]

Here 'f' is referred to from inside the bracket, which turns into data
and mentions only f's *name*, not 'f' itself. So we need some other
way to keep 'f' alive, lest it get dropped as dead code.  That's what
keepAlive does. It puts it in the keep-alive set, which subsequently
ensures that 'f' stays as a top level binding.

This must be done by the renamer, not the type checker (as of old),
because the type checker doesn't typecheck the body of untyped
brackets (Trac #8540).

A thing can have a bind_lvl of outerLevel, but have an internal name:
   foo = [d| op = 3
             bop = op + 1 |]
Here the bind_lvl of 'op' is (bogusly) outerLevel, even though it is
bound inside a bracket.  That is because we don't even even record
binding levels for top-level things; the binding levels are in the
LocalRdrEnv.

So the occurrence of 'op' in the rhs of 'bop' looks a bit like a
cross-stage thing, but it isn't really.  And in fact we never need
to do anything here for top-level bound things, so all is fine, if
a bit hacky.

For these chaps (which have Internal Names) we don't want to put
them in the keep-alive set.

Note [Quoting names]
~~~~~~~~~~~~~~~~~~~~
A quoted name 'n is a bit like a quoted expression [| n |], except that we
have no cross-stage lifting (c.f. TcExpr.thBrackId).  So, after incrementing
the use-level to account for the brackets, the cases are:

        bind > use                      Error
        bind = use+1                    OK
        bind < use
                Imported things         OK
                Top-level things        OK
                Non-top-level           Error

where 'use' is the binding level of the 'n quote. (So inside the implied
bracket the level would be use+1.)

Examples:

  f 'map        -- OK; also for top-level defns of this module

  \x. f 'x      -- Not ok (bind = 1, use = 1)
                -- (whereas \x. f [| x |] might have been ok, by
                --                               cross-stage lifting

  \y. [| \x. $(f 'y) |] -- Not ok (bind =1, use = 1)

  [| \x. $(f 'x) |]     -- OK (bind = 2, use = 1)
Austin Seipp's avatar
Austin Seipp committed
696
-}