TcSplice.hs 83.1 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4

5

Austin Seipp's avatar
Austin Seipp committed
6 7
TcSplice: Template Haskell splices
-}
8

9 10 11 12 13
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE InstanceSigs #-}
14 15
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RecordWildCards #-}
16
{-# LANGUAGE MultiWayIf #-}
17
{-# LANGUAGE TypeFamilies #-}
18
{-# OPTIONS_GHC -fno-warn-orphans #-}
19

20
module TcSplice(
21
     tcSpliceExpr, tcTypedBracket, tcUntypedBracket,
22 23
--     runQuasiQuoteExpr, runQuasiQuotePat,
--     runQuasiQuoteDecl, runQuasiQuoteType,
24 25 26
     runAnnotation,

     runMetaE, runMetaP, runMetaT, runMetaD, runQuasi,
27
     tcTopSpliceExpr, lookupThName_maybe,
28
     defaultRunMeta, runMeta', runRemoteModFinalizers,
29
     finishTH, runTopSplice
30
      ) where
31 32 33

#include "HsVersions.h"

34 35
import GhcPrelude

36 37
import HsSyn
import Annotations
38
import Finder
39 40 41 42
import Name
import TcRnMonad
import TcType

43 44 45
import Outputable
import TcExpr
import SrcLoc
46
import THNames
47 48
import TcUnify
import TcEnv
49
import Coercion( etaExpandCoAxBranch )
50
import FileCleanup ( newTempName, TempFileLifetime(..) )
51

52 53
import Control.Monad

54 55 56
import GHCi.Message
import GHCi.RemoteTypes
import GHCi
57
import HscMain
58 59
        -- These imports are the reason that TcSplice
        -- is very high up the module hierarchy
60
import RnSplice( traceSplice, SpliceInfo(..))
61
import RdrName
62
import HscTypes
63 64 65
import Convert
import RnExpr
import RnEnv
66 67
import RnUtils ( HsDocContext(..) )
import RnFixity ( lookupFixityRn_help )
68 69 70
import RnTypes
import TcHsSyn
import TcSimplify
batterseapower's avatar
batterseapower committed
71
import Type
72
import NameSet
73 74 75
import TcMType
import TcHsType
import TcIface
76
import TyCoRep
77 78
import FamInst
import FamInstEnv
79
import InstEnv
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
80
import Inst
81
import NameEnv
82
import PrelNames
83
import TysWiredIn
84
import OccName
85
import Hooks
86 87
import Var
import Module
88
import LoadIface
89 90
import Class
import TyCon
91
import CoAxiom
92
import PatSyn
Gergő Érdi's avatar
Gergő Érdi committed
93
import ConLike
94
import DataCon
95
import TcEvidence( TcEvBinds(..) )
96 97 98
import Id
import IdInfo
import DsExpr
99
import DsMonad
100
import GHC.Serialized
101
import ErrUtils
102
import Util
103
import Unique
104
import VarSet
Simon Peyton Jones's avatar
Simon Peyton Jones committed
105
import Data.List        ( find )
Ian Lynagh's avatar
Ian Lynagh committed
106
import Data.Maybe
107
import FastString
108 109
import BasicTypes hiding( SuccessFlag(..) )
import Maybes( MaybeErr(..) )
110
import DynFlags
111
import Panic
112
import Lexeme
113
import qualified EnumSet
114
import Plugins
115
import Bag
116

117 118 119
import qualified Language.Haskell.TH as TH
-- THSyntax gives access to internal functions and data types
import qualified Language.Haskell.TH.Syntax as TH
120

121 122
-- Because GHC.Desugar might not be in the base library of the bootstrapping compiler
import GHC.Desugar      ( AnnotationWrapper(..) )
123

124 125 126 127 128
import Control.Exception
import Data.Binary
import Data.Binary.Get
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
129
import Data.Dynamic  ( fromDynamic, toDyn )
130 131
import qualified Data.Map as Map
import Data.Typeable ( typeOf, Typeable, TypeRep, typeRep )
132
import Data.Data (Data)
133
import Data.Proxy    ( Proxy (..) )
134
import GHC.Exts         ( unsafeCoerce# )
135

Austin Seipp's avatar
Austin Seipp committed
136 137 138
{-
************************************************************************
*                                                                      *
139
\subsection{Main interface + stubs for the non-GHCI case
Austin Seipp's avatar
Austin Seipp committed
140 141 142
*                                                                      *
************************************************************************
-}
143

144 145
tcTypedBracket   :: HsExpr GhcRn -> HsBracket GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
tcUntypedBracket :: HsExpr GhcRn -> HsBracket GhcRn -> [PendingRnSplice] -> ExpRhoType
146 147
                 -> TcM (HsExpr GhcTcId)
tcSpliceExpr     :: HsSplice GhcRn  -> ExpRhoType -> TcM (HsExpr GhcTcId)
148 149
        -- None of these functions add constraints to the LIE

150 151 152 153
-- runQuasiQuoteExpr :: HsQuasiQuote RdrName -> RnM (LHsExpr RdrName)
-- runQuasiQuotePat  :: HsQuasiQuote RdrName -> RnM (LPat RdrName)
-- runQuasiQuoteType :: HsQuasiQuote RdrName -> RnM (LHsType RdrName)
-- runQuasiQuoteDecl :: HsQuasiQuote RdrName -> RnM [LHsDecl RdrName]
154

155
runAnnotation     :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
156 157 158 159 160 161 162 163 164 165
{-
************************************************************************
*                                                                      *
\subsection{Quoting an expression}
*                                                                      *
************************************************************************
-}

-- See Note [How brackets and nested splices are handled]
-- tcTypedBracket :: HsBracket Name -> TcRhoType -> TcM (HsExpr TcId)
166
tcTypedBracket rn_expr brack@(TExpBr _ expr) res_ty
167 168 169 170 171 172 173 174 175 176 177 178 179
  = addErrCtxt (quotationCtxtDoc brack) $
    do { cur_stage <- getStage
       ; ps_ref <- newMutVar []
       ; lie_var <- getConstraintVar   -- Any constraints arising from nested splices
                                       -- should get thrown into the constraint set
                                       -- from outside the bracket

       -- Typecheck expr to make sure it is valid,
       -- Throw away the typechecked expression but return its type.
       -- We'll typecheck it again when we splice it in somewhere
       ; (_tc_expr, expr_ty) <- setStage (Brack cur_stage (TcPending ps_ref lie_var)) $
                                tcInferRhoNC expr
                                -- NC for no context; tcBracket does that
180
       ; let rep = getRuntimeRep expr_ty
181 182 183 184

       ; meta_ty <- tcTExpTy expr_ty
       ; ps' <- readMutVar ps_ref
       ; texpco <- tcLookupId unsafeTExpCoerceName
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
185
       ; tcWrapResultO (Shouldn'tHappenOrigin "TExpBr")
186
                       rn_expr
187
                       (unLoc (mkHsApp (nlHsTyApp texpco [rep, expr_ty])
188
                                      (noLoc (HsTcBracketOut noExt brack ps'))))
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
189
                       meta_ty res_ty }
190
tcTypedBracket _ other_brack _
191 192
  = pprPanic "tcTypedBracket" (ppr other_brack)

193
-- tcUntypedBracket :: HsBracket Name -> [PendingRnSplice] -> ExpRhoType -> TcM (HsExpr TcId)
194
tcUntypedBracket rn_expr brack ps res_ty
195 196 197 198
  = do { traceTc "tc_bracket untyped" (ppr brack $$ ppr ps)
       ; ps' <- mapM tcPendingSplice ps
       ; meta_ty <- tcBrackTy brack
       ; traceTc "tc_bracket done untyped" (ppr meta_ty)
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
199
       ; tcWrapResultO (Shouldn'tHappenOrigin "untyped bracket")
200
                       rn_expr (HsTcBracketOut noExt brack ps') meta_ty res_ty }
201 202

---------------
203
tcBrackTy :: HsBracket GhcRn -> TcM TcType
204 205 206 207 208 209 210 211 212
tcBrackTy (VarBr {})  = tcMetaTy nameTyConName
                                           -- Result type is Var (not Q-monadic)
tcBrackTy (ExpBr {})  = tcMetaTy expQTyConName  -- Result type is ExpQ (= Q Exp)
tcBrackTy (TypBr {})  = tcMetaTy typeQTyConName -- Result type is Type (= Q Typ)
tcBrackTy (DecBrG {}) = tcMetaTy decsQTyConName -- Result type is Q [Dec]
tcBrackTy (PatBr {})  = tcMetaTy patQTyConName  -- Result type is PatQ (= Q Pat)
tcBrackTy (DecBrL {})   = panic "tcBrackTy: Unexpected DecBrL"
tcBrackTy (TExpBr {})   = panic "tcUntypedBracket: Unexpected TExpBr"
tcBrackTy (XBracket {}) = panic "tcUntypedBracket: Unexpected XBracket"
213 214 215 216 217

---------------
tcPendingSplice :: PendingRnSplice -> TcM PendingTcSplice
tcPendingSplice (PendingRnSplice flavour splice_name expr)
  = do { res_ty <- tcMetaTy meta_ty_name
218
       ; expr' <- tcMonoExpr expr (mkCheckExpType res_ty)
219 220 221 222 223 224 225 226 227
       ; return (PendingTcSplice splice_name expr') }
  where
     meta_ty_name = case flavour of
                       UntypedExpSplice  -> expQTyConName
                       UntypedPatSplice  -> patQTyConName
                       UntypedTypeSplice -> typeQTyConName
                       UntypedDeclSplice -> decsQTyConName

---------------
228
-- Takes a tau and returns the type Q (TExp tau)
229
tcTExpTy :: TcType -> TcM TcType
230 231 232
tcTExpTy exp_ty
  = do { unless (isTauTy exp_ty) $ addErr (err_msg exp_ty)
       ; q    <- tcLookupTyCon qTyConName
233
       ; texp <- tcLookupTyCon tExpTyConName
234 235
       ; let rep = getRuntimeRep exp_ty
       ; return (mkTyConApp q [mkTyConApp texp [rep, exp_ty]]) }
236 237 238 239 240
  where
    err_msg ty
      = vcat [ text "Illegal polytype:" <+> ppr ty
             , text "The type of a Typed Template Haskell expression must" <+>
               text "not have any quantification." ]
241

242
quotationCtxtDoc :: HsBracket GhcRn -> SDoc
243
quotationCtxtDoc br_body
244
  = hang (text "In the Template Haskell quotation")
245 246
         2 (ppr br_body)

247 248 249

  -- The whole of the rest of the file is the else-branch (ie stage2 only)

Austin Seipp's avatar
Austin Seipp committed
250
{-
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
Note [How top-level splices are handled]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Top-level splices (those not inside a [| .. |] quotation bracket) are handled
very straightforwardly:

  1. tcTopSpliceExpr: typecheck the body e of the splice $(e)

  2. runMetaT: desugar, compile, run it, and convert result back to
     HsSyn RdrName (of the appropriate flavour, eg HsType RdrName,
     HsExpr RdrName etc)

  3. treat the result as if that's what you saw in the first place
     e.g for HsType, rename and kind-check
         for HsExpr, rename and type-check

Gabor Greif's avatar
typos  
Gabor Greif committed
266
     (The last step is different for decls, because they can *only* be
267 268 269 270
      top-level: we return the result of step 2.)

Note [How brackets and nested splices are handled]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
Nested splices (those inside a [| .. |] quotation bracket),
are treated quite differently.

Remember, there are two forms of bracket
         typed   [|| e ||]
   and untyped   [|  e  |]

The life cycle of a typed bracket:
   * Starts as HsBracket

   * When renaming:
        * Set the ThStage to (Brack s RnPendingTyped)
        * Rename the body
        * Result is still a HsBracket

   * When typechecking:
        * Set the ThStage to (Brack s (TcPending ps_var lie_var))
        * Typecheck the body, and throw away the elaborated result
        * Nested splices (which must be typed) are typechecked, and
          the results accumulated in ps_var; their constraints
          accumulate in lie_var
        * Result is a HsTcBracketOut rn_brack pending_splices
          where rn_brack is the incoming renamed bracket

The life cycle of a un-typed bracket:
   * Starts as HsBracket

   * When renaming:
        * Set the ThStage to (Brack s (RnPendingUntyped ps_var))
        * Rename the body
        * Nested splices (which must be untyped) are renamed, and the
          results accumulated in ps_var
        * Result is still (HsRnBracketOut rn_body pending_splices)

   * When typechecking a HsRnBracketOut
        * Typecheck the pending_splices individually
        * Ignore the body of the bracket; just check that the context
          expects a bracket of that type (e.g. a [p| pat |] bracket should
          be in a context needing a (Q Pat)
        * Result is a HsTcBracketOut rn_brack pending_splices
          where rn_brack is the incoming renamed bracket


In both cases, desugaring happens like this:
  * HsTcBracketOut is desugared by DsMeta.dsBracket.  It
316 317 318 319 320 321

      a) Extends the ds_meta environment with the PendingSplices
         attached to the bracket

      b) Converts the quoted (HsExpr Name) to a CoreExpr that, when
         run, will produce a suitable TH expression/type/decl.  This
322
         is why we leave the *renamed* expression attached to the bracket:
323 324 325 326 327 328 329 330 331 332 333 334
         the quoted expression should not be decorated with all the goop
         added by the type checker

  * Each splice carries a unique Name, called a "splice point", thus
    ${n}(e).  The name is initialised to an (Unqual "splice") when the
    splice is created; the renamer gives it a unique.

  * When DsMeta (used to desugar the body of the bracket) comes across
    a splice, it looks up the splice's Name, n, in the ds_meta envt,
    to find an (HsExpr Id) that should be substituted for the splice;
    it just desugars it to get a CoreExpr (DsMeta.repSplice).

335 336
Example:
    Source:       f = [| Just $(g 3) |]
337 338 339
      The [| |] part is a HsBracket

    Typechecked:  f = [| Just ${s7}(g 3) |]{s7 = g Int 3}
340 341 342 343
      The [| |] part is a HsBracketOut, containing *renamed*
        (not typechecked) expression
      The "s7" is the "splice point"; the (g Int 3) part
        is a typechecked expression
344

345 346
    Desugared:    f = do { s7 <- g Int 3
                         ; return (ConE "Data.Maybe.Just" s7) }
347 348 349 350 351


Note [Template Haskell state diagram]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here are the ThStages, s, their corresponding level numbers
352
(the result of (thLevel s)), and their state transitions.
353
The top level of the program is stage Comp:
354

355 356 357
     Start here
         |
         V
358
      -----------     $      ------------   $
359
      |  Comp   | ---------> |  Splice  | -----|
360 361 362 363 364 365 366 367 368 369 370
      |   1     |            |    0     | <----|
      -----------            ------------
        ^     |                ^      |
      $ |     | [||]         $ |      | [||]
        |     v                |      v
   --------------          ----------------
   | Brack Comp |          | Brack Splice |
   |     2      |          |      1       |
   --------------          ----------------

* Normal top-level declarations start in state Comp
371 372 373 374
       (which has level 1).
  Annotations start in state Splice, since they are
       treated very like a splice (only without a '$')

375
* Code compiled in state Splice (and only such code)
376 377 378 379 380
  will be *run at compile time*, with the result replacing
  the splice

* The original paper used level -1 instead of 0, etc.

381 382
* The original paper did not allow a splice within a
  splice, but there is no reason not to. This is the
383 384
  $ transition in the top right.

385 386 387
Note [Template Haskell levels]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Imported things are impLevel (= 0)
388

389 390 391
* However things at level 0 are not *necessarily* imported.
      eg  $( \b -> ... )   here b is bound at level 0

392 393 394
* In GHCi, variables bound by a previous command are treated
  as impLevel, because we have bytecode for them.

395
* Variables are bound at the "current level"
396

397
* The current level starts off at outerLevel (= 1)
398

399
* The level is decremented by splicing $(..)
400 401
               incremented by brackets [| |]
               incremented by name-quoting 'f
402

403 404 405
When a variable is used, we compare
        bind:  binding level, and
        use:   current level at usage site
406 407

  Generally
408 409
        bind > use      Always error (bound later than used)
                        [| \x -> $(f x) |]
410

411 412 413 414 415 416
        bind = use      Always OK (bound same stage as used)
                        [| \x -> $(f [| x |]) |]

        bind < use      Inside brackets, it depends
                        Inside splice, OK
                        Inside neither, OK
417 418

  For (bind < use) inside brackets, there are three cases:
419 420 421 422
    - Imported things   OK      f = [| map |]
    - Top-level things  OK      g = [| f |]
    - Non-top-level     Only if there is a liftable instance
                                h = \(x:Int) -> [| x |]
423

424
  To track top-level-ness we use the ThBindEnv in TcLclEnv
425

426 427 428 429
  For example:
           f = ...
           g1 = $(map ...)         is OK
           g2 = $(f ...)           is not OK; because we havn't compiled f yet
430

Austin Seipp's avatar
Austin Seipp committed
431
-}
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
432

Austin Seipp's avatar
Austin Seipp committed
433 434 435
{-
************************************************************************
*                                                                      *
436
\subsection{Splicing an expression}
Austin Seipp's avatar
Austin Seipp committed
437 438 439
*                                                                      *
************************************************************************
-}
440

441
tcSpliceExpr splice@(HsTypedSplice _ _ name expr) res_ty
442
  = addErrCtxt (spliceCtxtDoc splice) $
443
    setSrcSpan (getLoc expr)    $ do
444
    { stage <- getStage
gmainland's avatar
gmainland committed
445
    ; case stage of
446 447 448 449 450 451 452 453
          Splice {}            -> tcTopSplice expr res_ty
          Brack pop_stage pend -> tcNestedSplice pop_stage pend name expr res_ty
          RunSplice _          ->
            -- See Note [RunSplice ThLevel] in "TcRnTypes".
            pprPanic ("tcSpliceExpr: attempted to typecheck a splice when " ++
                      "running another splice") (ppr splice)
          Comp                 -> tcTopSplice expr res_ty
    }
454 455
tcSpliceExpr splice _
  = pprPanic "tcSpliceExpr" (ppr splice)
456

457 458 459 460 461 462 463 464 465 466 467
{- Note [Collecting modFinalizers in typed splices]
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'qAddModFinalizer' of the @Quasi TcM@ instance adds finalizers in the local
environment (see Note [Delaying modFinalizers in untyped splices] in
"RnSplice"). Thus after executing the splice, we move the finalizers to the
finalizer list in the global environment and set them to use the current local
environment (with 'addModFinalizersWithLclEnv').

-}

468
tcNestedSplice :: ThStage -> PendingStuff -> Name
469
                -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
470 471
    -- See Note [How brackets and nested splices are handled]
    -- A splice inside brackets
472
tcNestedSplice pop_stage (TcPending ps_var lie_var) splice_name expr res_ty
473
  = do { res_ty <- expTypeToType res_ty
474
       ; let rep = getRuntimeRep res_ty
475
       ; meta_exp_ty <- tcTExpTy res_ty
476 477
       ; expr' <- setStage pop_stage $
                  setConstraintVar lie_var $
478
                  tcMonoExpr expr (mkCheckExpType meta_exp_ty)
479
       ; untypeq <- tcLookupId unTypeQName
480
       ; let expr'' = mkHsApp (nlHsTyApp untypeq [rep, res_ty]) expr'
481
       ; ps <- readMutVar ps_var
482
       ; writeMutVar ps_var (PendingTcSplice splice_name expr'' : ps)
483 484 485 486 487 488

       -- The returned expression is ignored; it's in the pending splices
       ; return (panic "tcSpliceExpr") }

tcNestedSplice _ _ splice_name _ _
  = pprPanic "tcNestedSplice: rename stage found" (ppr splice_name)
489

490
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
gmainland's avatar
gmainland committed
491
tcTopSplice expr res_ty
492 493
  = do { -- Typecheck the expression,
         -- making sure it has type Q (T res_ty)
494 495
         res_ty <- expTypeToType res_ty
       ; meta_exp_ty <- tcTExpTy res_ty
496
       ; q_expr <- tcTopSpliceExpr Typed $
497
                          tcMonoExpr expr (mkCheckExpType meta_exp_ty)
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
       ; lcl_env <- getLclEnv
       ; let delayed_splice
              = DelayedSplice lcl_env expr res_ty q_expr
       ; return (HsSpliceE noExt (HsSplicedT delayed_splice))

       }


-- This is called in the zonker
-- See Note [Running typed splices in the zonker]
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTc)
runTopSplice (DelayedSplice lcl_env orig_expr res_ty q_expr)
  = setLclEnv lcl_env $ do {
         zonked_ty <- zonkTcType res_ty
       ; zonked_q_expr <- zonkTopLExpr q_expr
        -- See Note [Collecting modFinalizers in typed splices].
514
       ; modfinalizers_ref <- newTcRef []
515
         -- Run the expression
516 517 518 519
       ; expr2 <- setStage (RunSplice modfinalizers_ref) $
                    runMetaE zonked_q_expr
       ; mod_finalizers <- readTcRef modfinalizers_ref
       ; addModFinalizersWithLclEnv $ ThModFinalizers mod_finalizers
520 521 522
       -- We use orig_expr here and not q_expr when tracing as a call to
       -- unsafeTExpCoerce is added to the original expression by the
       -- typechecker when typed quotes are type checked.
523 524
       ; traceSplice (SpliceInfo { spliceDescription = "expression"
                                 , spliceIsDecl      = False
525
                                 , spliceSource      = Just orig_expr
526
                                 , spliceGenerated   = ppr expr2 })
527 528 529
        -- Rename and typecheck the spliced-in expression,
        -- making sure it has type res_ty
        -- These steps should never fail; this is a *typed* splice
530 531 532 533 534 535 536 537
       ; (res, wcs) <-
            captureConstraints $
              addErrCtxt (spliceResultDoc zonked_q_expr) $ do
                { (exp3, _fvs) <- rnLExpr expr2
                ; tcMonoExpr exp3 (mkCheckExpType zonked_ty)}
       ; ev <- simplifyTop wcs
       ; return $ unLoc (mkHsDictLet (EvBinds ev) res)
       }
538

gmainland's avatar
gmainland committed
539

Austin Seipp's avatar
Austin Seipp committed
540 541 542
{-
************************************************************************
*                                                                      *
gmainland's avatar
gmainland committed
543
\subsection{Error messages}
Austin Seipp's avatar
Austin Seipp committed
544 545 546
*                                                                      *
************************************************************************
-}
547

548
spliceCtxtDoc :: HsSplice GhcRn -> SDoc
549
spliceCtxtDoc splice
550
  = hang (text "In the Template Haskell splice")
551
         2 (pprSplice splice)
552

553
spliceResultDoc :: LHsExpr GhcTc -> SDoc
554
spliceResultDoc expr
555
  = sep [ text "In the result of the splice:"
556
        , nest 2 (char '$' <> ppr expr)
557
        , text "To see what the splice expanded to, use -ddump-splices"]
558

559
-------------------
560
tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
561
-- Note [How top-level splices are handled]
562 563
-- Type check an expression that is the body of a top-level splice
--   (the caller will compile and run it)
564 565
-- Note that set the level to Splice, regardless of the original level,
-- before typechecking the expression.  For example:
566
--      f x = $( ...$(g 3) ... )
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
567
-- The recursive call to tcPolyExpr will simply expand the
568 569
-- inner escape before dealing with the outer one

570
tcTopSpliceExpr isTypedSplice tc_action
571 572
  = checkNoErrs $  -- checkNoErrs: must not try to run the thing
                   -- if the type checker fails!
ian@well-typed.com's avatar
ian@well-typed.com committed
573
    unsetGOptM Opt_DeferTypeErrors $
574 575 576 577
                   -- Don't defer type errors.  Not only are we
                   -- going to run this code, but we do an unsafe
                   -- coerce, so we get a seg-fault if, say we
                   -- splice a type into a place where an expression
578
                   -- is expected (#7276)
579
    setStage (Splice isTypedSplice) $
580
    do {    -- Typecheck the expression
581 582
         (expr', wanted) <- captureConstraints tc_action
       ; const_binds     <- simplifyTop wanted
583

584
          -- Zonk it and tie the knot of dictionary bindings
585
       ; return $ mkHsDictLet (EvBinds const_binds) expr' }
586

Austin Seipp's avatar
Austin Seipp committed
587 588 589
{-
************************************************************************
*                                                                      *
590
        Annotations
Austin Seipp's avatar
Austin Seipp committed
591 592 593
*                                                                      *
************************************************************************
-}
594

595 596
runAnnotation target expr = do
    -- Find the classes we want instances for in order to call toAnnotationWrapper
597
    loc <- getSrcSpanM
598
    data_class <- tcLookupClass dataClassName
599
    to_annotation_wrapper_id <- tcLookupId toAnnotationWrapperName
600

601 602 603
    -- Check the instances we require live in another module (we want to execute it..)
    -- and check identifiers live in other modules using TH stage checks. tcSimplifyStagedExpr
    -- also resolves the LIE constraints to detect e.g. instance ambiguity
604
    zonked_wrapped_expr' <- zonkTopLExpr =<< tcTopSpliceExpr Untyped (
605
           do { (expr', expr_ty) <- tcInferRhoNC expr
606 607 608
                -- We manually wrap the typechecked expression in a call to toAnnotationWrapper
                -- By instantiating the call >here< it gets registered in the
                -- LIE consulted by tcTopSpliceExpr
609
                -- and hence ensures the appropriate dictionary is bound by const_binds
610
              ; wrapper <- instCall AnnOrigin [expr_ty] [mkClassPred data_class [expr_ty]]
611
              ; let specialised_to_annotation_wrapper_expr
612
                      = L loc (mkHsWrap wrapper
613 614
                                 (HsVar noExt (L loc to_annotation_wrapper_id)))
              ; return (L loc (HsApp noExt
615 616
                                specialised_to_annotation_wrapper_expr expr'))
                                })
617 618 619 620 621

    -- Run the appropriately wrapped expression to get the value of
    -- the annotation and its dictionaries. The return value is of
    -- type AnnotationWrapper by construction, so this conversion is
    -- safe
Luite Stegeman's avatar
Luite Stegeman committed
622 623 624 625 626 627
    serialized <- runMetaAW zonked_wrapped_expr'
    return Annotation {
               ann_target = target,
               ann_value = serialized
           }

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
convertAnnotationWrapper :: ForeignHValue -> TcM (Either MsgDoc Serialized)
convertAnnotationWrapper fhv = do
  dflags <- getDynFlags
  if gopt Opt_ExternalInterpreter dflags
    then do
      Right <$> runTH THAnnWrapper fhv
    else do
      annotation_wrapper <- liftIO $ wormhole dflags fhv
      return $ Right $
        case unsafeCoerce# annotation_wrapper of
           AnnotationWrapper value | let serialized = toSerialized serializeWithData value ->
               -- Got the value and dictionaries: build the serialized value and
               -- call it a day. We ensure that we seq the entire serialized value
               -- in order that any errors in the user-written code for the
               -- annotation are exposed at this point.  This is also why we are
               -- doing all this stuff inside the context of runMeta: it has the
               -- facilities to deal with user error in a meta-level expression
               seqSerialized serialized `seq` serialized

-- | Force the contents of the Serialized value so weknow it doesn't contain any bottoms
seqSerialized :: Serialized -> ()
seqSerialized (Serialized the_type bytes) = the_type `seq` bytes `seqList` ()
650

651

Austin Seipp's avatar
Austin Seipp committed
652 653 654
{-
************************************************************************
*                                                                      *
655
\subsection{Running an expression}
Austin Seipp's avatar
Austin Seipp committed
656 657 658
*                                                                      *
************************************************************************
-}
659

660 661 662
runQuasi :: TH.Q a -> TcM a
runQuasi act = TH.runQ act

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
runRemoteModFinalizers :: ThModFinalizers -> TcM ()
runRemoteModFinalizers (ThModFinalizers finRefs) = do
  dflags <- getDynFlags
  let withForeignRefs [] f = f []
      withForeignRefs (x : xs) f = withForeignRef x $ \r ->
        withForeignRefs xs $ \rs -> f (r : rs)
  if gopt Opt_ExternalInterpreter dflags then do
    hsc_env <- env_top <$> getEnv
    withIServ hsc_env $ \i -> do
      tcg <- getGblEnv
      th_state <- readTcRef (tcg_th_remote_state tcg)
      case th_state of
        Nothing -> return () -- TH was not started, nothing to do
        Just fhv -> do
          liftIO $ withForeignRef fhv $ \st ->
            withForeignRefs finRefs $ \qrefs ->
              writeIServ i (putMessage (RunModFinalizers st qrefs))
          () <- runRemoteTH i []
          readQResult i
  else do
    qs <- liftIO (withForeignRefs finRefs $ mapM localRef)
    runQuasi $ sequence_ qs

686 687 688 689 690 691 692 693 694
runQResult
  :: (a -> String)
  -> (SrcSpan -> a -> b)
  -> (ForeignHValue -> TcM a)
  -> SrcSpan
  -> ForeignHValue {- TH.Q a -}
  -> TcM b
runQResult show_th f runQ expr_span hval
  = do { th_result <- runQ hval
Luite Stegeman's avatar
Luite Stegeman committed
695 696
       ; traceTc "Got TH result:" (text (show_th th_result))
       ; return (f expr_span th_result) }
697

698

Luite Stegeman's avatar
Luite Stegeman committed
699
-----------------
700 701
runMeta :: (MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
        -> LHsExpr GhcTc
Luite Stegeman's avatar
Luite Stegeman committed
702 703 704 705 706 707 708
        -> TcM hs_syn
runMeta unwrap e
  = do { h <- getHooked runMetaHook defaultRunMeta
       ; unwrap h e }

defaultRunMeta :: MetaHook TcM
defaultRunMeta (MetaE r)
709
  = fmap r . runMeta' True ppr (runQResult TH.pprint convertToHsExpr runTHExp)
Luite Stegeman's avatar
Luite Stegeman committed
710
defaultRunMeta (MetaP r)
711
  = fmap r . runMeta' True ppr (runQResult TH.pprint convertToPat runTHPat)
Luite Stegeman's avatar
Luite Stegeman committed
712
defaultRunMeta (MetaT r)
713
  = fmap r . runMeta' True ppr (runQResult TH.pprint convertToHsType runTHType)
Luite Stegeman's avatar
Luite Stegeman committed
714
defaultRunMeta (MetaD r)
715
  = fmap r . runMeta' True ppr (runQResult TH.pprint convertToHsDecls runTHDec)
Luite Stegeman's avatar
Luite Stegeman committed
716
defaultRunMeta (MetaAW r)
717
  = fmap r . runMeta' False (const empty) (const convertAnnotationWrapper)
718
    -- We turn off showing the code in meta-level exceptions because doing so exposes
Gabor Greif's avatar
Gabor Greif committed
719
    -- the toAnnotationWrapper function that we slap around the user's code
720

Luite Stegeman's avatar
Luite Stegeman committed
721
----------------
722
runMetaAW :: LHsExpr GhcTc         -- Of type AnnotationWrapper
Luite Stegeman's avatar
Luite Stegeman committed
723 724
          -> TcM Serialized
runMetaAW = runMeta metaRequestAW
725

726 727
runMetaE :: LHsExpr GhcTc          -- Of type (Q Exp)
         -> TcM (LHsExpr GhcPs)
Luite Stegeman's avatar
Luite Stegeman committed
728
runMetaE = runMeta metaRequestE
729

730 731
runMetaP :: LHsExpr GhcTc          -- Of type (Q Pat)
         -> TcM (LPat GhcPs)
Luite Stegeman's avatar
Luite Stegeman committed
732
runMetaP = runMeta metaRequestP
gmainland's avatar
gmainland committed
733

734 735
runMetaT :: LHsExpr GhcTc          -- Of type (Q Type)
         -> TcM (LHsType GhcPs)
Luite Stegeman's avatar
Luite Stegeman committed
736
runMetaT = runMeta metaRequestT
737

738 739
runMetaD :: LHsExpr GhcTc          -- Of type Q [Dec]
         -> TcM [LHsDecl GhcPs]
Luite Stegeman's avatar
Luite Stegeman committed
740
runMetaD = runMeta metaRequestD
741 742

---------------
Luite Stegeman's avatar
Luite Stegeman committed
743 744
runMeta' :: Bool                 -- Whether code should be printed in the exception message
         -> (hs_syn -> SDoc)                                    -- how to print the code
745
         -> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))        -- How to run x
746 747
         -> LHsExpr GhcTc        -- Of type x; typically x = Q TH.Exp, or
                                 --    something like that
Luite Stegeman's avatar
Luite Stegeman committed
748 749
         -> TcM hs_syn           -- Of type t
runMeta' show_code ppr_hs run_and_convert expr
750
  = do  { traceTc "About to run" (ppr expr)
751 752
        ; recordThSpliceUse -- seems to be the best place to do this,
                            -- we catch all kinds of splices and annotations.
753

754 755 756 757 758
        -- Check that we've had no errors of any sort so far.
        -- For example, if we found an error in an earlier defn f, but
        -- recovered giving it type f :: forall a.a, it'd be very dodgy
        -- to carry ont.  Mind you, the staging restrictions mean we won't
        -- actually run f, but it still seems wrong. And, more concretely,
759
        -- see #5358 for an example that fell over when trying to
760 761 762 763
        -- reify a function with a "?" kind in it.  (These don't occur
        -- in type-correct programs.
        ; failIfErrsM

764 765 766 767
        -- run plugins
        ; hsc_env <- getTopEnv
        ; expr' <- withPlugins (hsc_dflags hsc_env) spliceRunAction expr

768
        -- Desugar
769
        ; ds_expr <- initDsTc (dsLExpr expr')
770 771 772
        -- Compile and link it; might fail if linking fails
        ; src_span <- getSrcSpanM
        ; traceTc "About to run (desugared)" (ppr ds_expr)
Simon Peyton Jones's avatar
Simon Peyton Jones committed
773
        ; either_hval <- tryM $ liftIO $
774 775
                         HscMain.hscCompileCoreExpr hsc_env src_span ds_expr
        ; case either_hval of {
776
            Left exn   -> fail_with_exn "compile and link" exn ;
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
            Right hval -> do

        {       -- Coerce it to Q t, and run it

                -- Running might fail if it throws an exception of any kind (hence tryAllM)
                -- including, say, a pattern-match exception in the code we are running
                --
                -- We also do the TH -> HS syntax conversion inside the same
                -- exception-cacthing thing so that if there are any lurking
                -- exceptions in the data structure returned by hval, we'll
                -- encounter them inside the try
                --
                -- See Note [Exceptions in TH]
          let expr_span = getLoc expr
        ; either_tval <- tryAllM $
                         setSrcSpan expr_span $ -- Set the span so that qLocation can
                                                -- see where this splice is
794
             do { mb_result <- run_and_convert expr_span hval
795 796
                ; case mb_result of
                    Left err     -> failWithTc err
Luite Stegeman's avatar
Luite Stegeman committed
797
                    Right result -> do { traceTc "Got HsSyn result:" (ppr_hs result)
798
                                       ; return $! result } }
799

800 801 802 803
        ; case either_tval of
            Right v -> return v
            Left se -> case fromException se of
                         Just IOEnvFailure -> failM -- Error already in Tc monad
804
                         _ -> fail_with_exn "run" se -- Exception
805
        }}}
806
  where
807
    -- see Note [Concealed TH exceptions]
808
    fail_with_exn :: Exception e => String -> e -> TcM a
809 810 811 812
    fail_with_exn phase exn = do
        exn_msg <- liftIO $ Panic.safeShowException exn
        let msg = vcat [text "Exception when trying to" <+> text phase <+> text "compile-time code:",
                        nest 2 (text exn_msg),
Simon Peyton Jones's avatar
Simon Peyton Jones committed
813
                        if show_code then text "Code:" <+> ppr expr else empty]
814
        failWithTc msg
815

Austin Seipp's avatar
Austin Seipp committed
816
{-
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
Note [Running typed splices in the zonker]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

See #15471 for the full discussion.

For many years typed splices were run immediately after they were type checked
however, this is too early as it means to zonk some type variables before
they can be unified with type variables in the surrounding context.

For example,

```
module A where

test_foo :: forall a . Q (TExp (a -> a))
test_foo = [|| id ||]

module B where

import A

qux = $$(test_foo)
```

We would expect `qux` to have inferred type `forall a . a -> a` but if
we run the splices too early the unified variables are zonked to `Any`. The
inferred type is the unusable `Any -> Any`.

To run the splice, we must compile `test_foo` all the way to byte code.
But at the moment when the type checker is looking at the splice, test_foo
has type `Q (TExp (alpha -> alpha))` and we
certainly can't compile code involving unification variables!

We could default `alpha` to `Any` but then we infer `qux :: Any -> Any`
which definitely is not what we want.  Moreover, if we had
  qux = [$$(test_foo), (\x -> x +1::Int)]
then `alpha` would have to be `Int`.

Conclusion: we must defer taking decisions about `alpha` until the
typechecker is done; and *then* we can run the splice.  It's fine to do it
later, because we know it'll produce type-correct code.

Deferring running the splice until later, in the zonker, means that the
unification variables propagate upwards from the splice into the surrounding
context and are unified correctly.

This is implemented by storing the arguments we need for running the splice
in a `DelayedSplice`. In the zonker, the arguments are passed to
`TcSplice.runTopSplice` and the expression inserted into the AST as normal.



869 870
Note [Exceptions in TH]
~~~~~~~~~~~~~~~~~~~~~~~
Gabor Greif's avatar
Gabor Greif committed
871
Suppose we have something like this
872
        $( f 4 )
873
where
874 875 876
        f :: Int -> Q [Dec]
        f n | n>3       = fail "Too many declarations"
            | otherwise = ...
877 878 879 880 881 882

The 'fail' is a user-generated failure, and should be displayed as a
perfectly ordinary compiler error message, not a panic or anything
like that.  Here's how it's processed:

  * 'fail' is the monad fail.  The monad instance for Q in TH.Syntax
883 884
    effectively transforms (fail s) to
        qReport True s >> fail
885 886 887 888 889
    where 'qReport' comes from the Quasi class and fail from its monad
    superclass.

  * The TcM monad is an instance of Quasi (see TcSplice), and it implements
    (qReport True s) by using addErr to add an error message to the bag of errors.
890
    The 'fail' in TcM raises an IOEnvFailure exception
891

892 893
 * 'qReport' forces the message to ensure any exception hidden in unevaluated
   thunk doesn't get into the bag of errors. Otherwise the following splice
894
   will triger panic (#8987):
895 896 897
        $(fail undefined)
   See also Note [Concealed TH exceptions]

898 899 900 901
  * So, when running a splice, we catch all exceptions; then for
        - an IOEnvFailure exception, we assume the error is already
                in the error-bag (above)
        - other errors, we add an error to the bag
902 903
    and then fail

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
Note [Concealed TH exceptions]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When displaying the error message contained in an exception originated from TH
code, we need to make sure that the error message itself does not contain an
exception.  For example, when executing the following splice:

    $( error ("foo " ++ error "bar") )

the message for the outer exception is a thunk which will throw the inner
exception when evaluated.

For this reason, we display the message of a TH exception using the
'safeShowException' function, which recursively catches any exception thrown
when showing an error message.

919

920
To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
Austin Seipp's avatar
Austin Seipp committed
921
-}
922