TcSplice.hs 87.8 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

Sylvain Henry's avatar
Sylvain Henry committed
36
import GHC.Hs
37
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 TcOrigin
50
import Coercion( etaExpandCoAxBranch )
51
import FileCleanup ( newTempName, TempFileLifetime(..) )
52

53 54
import Control.Monad

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

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

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

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

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

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

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

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

-- See Note [How brackets and nested splices are handled]
-- tcTypedBracket :: HsBracket Name -> TcRhoType -> TcM (HsExpr TcId)
167
tcTypedBracket rn_expr brack@(TExpBr _ expr) res_ty
168 169 170 171 172 173 174 175 176 177 178 179 180
  = 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
181
       ; let rep = getRuntimeRep expr_ty
182 183 184 185

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

194
-- tcUntypedBracket :: HsBracket Name -> [PendingRnSplice] -> ExpRhoType -> TcM (HsExpr TcId)
195
tcUntypedBracket rn_expr brack ps res_ty
196 197 198 199
  = 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
200
       ; tcWrapResultO (Shouldn'tHappenOrigin "untyped bracket")
201
                       rn_expr (HsTcBracketOut noExtField brack ps') meta_ty res_ty }
202 203

---------------
204
tcBrackTy :: HsBracket GhcRn -> TcM TcType
205 206 207 208 209 210
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)
211 212 213
tcBrackTy (DecBrL {}) = panic "tcBrackTy: Unexpected DecBrL"
tcBrackTy (TExpBr {}) = panic "tcUntypedBracket: Unexpected TExpBr"
tcBrackTy (XBracket nec) = noExtCon nec
214 215 216 217 218

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

---------------
229
-- Takes a tau and returns the type Q (TExp tau)
230
tcTExpTy :: TcType -> TcM TcType
231 232 233
tcTExpTy exp_ty
  = do { unless (isTauTy exp_ty) $ addErr (err_msg exp_ty)
       ; q    <- tcLookupTyCon qTyConName
234
       ; texp <- tcLookupTyCon tExpTyConName
235 236
       ; let rep = getRuntimeRep exp_ty
       ; return (mkTyConApp q [mkTyConApp texp [rep, exp_ty]]) }
237 238 239 240 241
  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." ]
242

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

248 249 250

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

Austin Seipp's avatar
Austin Seipp committed
251
{-
252 253 254 255 256 257 258 259
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
Sylvain Henry's avatar
Sylvain Henry committed
260
     GHC.Hs syntax RdrName (of the appropriate flavour, eg HsType RdrName,
261 262 263 264 265 266
     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
267
     (The last step is different for decls, because they can *only* be
268 269 270 271
      top-level: we return the result of step 2.)

Note [How brackets and nested splices are handled]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 316
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
317 318 319 320 321 322

      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
323
         is why we leave the *renamed* expression attached to the bracket:
324 325 326 327 328 329 330 331 332 333 334 335
         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).

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

    Typechecked:  f = [| Just ${s7}(g 3) |]{s7 = g Int 3}
341 342 343 344
      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
345

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  For (bind < use) inside brackets, there are three cases:
420 421 422 423
    - 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 |]
424

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

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

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

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

442
tcSpliceExpr splice@(HsTypedSplice _ _ name expr) res_ty
443
  = addErrCtxt (spliceCtxtDoc splice) $
444
    setSrcSpan (getLoc expr)    $ do
445
    { stage <- getStage
gmainland's avatar
gmainland committed
446
    ; case stage of
447 448 449 450 451 452 453 454
          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
    }
455 456
tcSpliceExpr splice _
  = pprPanic "tcSpliceExpr" (ppr splice)
457

458 459 460 461 462 463 464 465 466 467 468
{- 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').

-}

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

       -- 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)
490

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

       }


-- 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].
515
       ; modfinalizers_ref <- newTcRef []
516
         -- Run the expression
517 518 519 520
       ; expr2 <- setStage (RunSplice modfinalizers_ref) $
                    runMetaE zonked_q_expr
       ; mod_finalizers <- readTcRef modfinalizers_ref
       ; addModFinalizersWithLclEnv $ ThModFinalizers mod_finalizers
521 522 523
       -- 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.
524 525
       ; traceSplice (SpliceInfo { spliceDescription = "expression"
                                 , spliceIsDecl      = False
526
                                 , spliceSource      = Just orig_expr
527
                                 , spliceGenerated   = ppr expr2 })
528 529 530
        -- Rename and typecheck the spliced-in expression,
        -- making sure it has type res_ty
        -- These steps should never fail; this is a *typed* splice
531 532 533 534 535 536 537 538
       ; (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)
       }
539

gmainland's avatar
gmainland committed
540

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

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

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

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

571
tcTopSpliceExpr isTypedSplice tc_action
572 573
  = 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
574
    unsetGOptM Opt_DeferTypeErrors $
575 576 577 578
                   -- 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
579
                   -- is expected (#7276)
580
    setStage (Splice isTypedSplice) $
581
    do {    -- Typecheck the expression
582 583
         (expr', wanted) <- captureConstraints tc_action
       ; const_binds     <- simplifyTop wanted
584

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

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

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

602 603 604
    -- 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
605
    zonked_wrapped_expr' <- zonkTopLExpr =<< tcTopSpliceExpr Untyped (
606
           do { (expr', expr_ty) <- tcInferRhoNC expr
607 608 609
                -- 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
610
                -- and hence ensures the appropriate dictionary is bound by const_binds
611
              ; wrapper <- instCall AnnOrigin [expr_ty] [mkClassPred data_class [expr_ty]]
612
              ; let specialised_to_annotation_wrapper_expr
613
                      = L loc (mkHsWrap wrapper
614 615
                                 (HsVar noExtField (L loc to_annotation_wrapper_id)))
              ; return (L loc (HsApp noExtField
616 617
                                specialised_to_annotation_wrapper_expr expr'))
                                })
618 619 620 621 622

    -- 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
623 624 625 626 627 628
    serialized <- runMetaAW zonked_wrapped_expr'
    return Annotation {
               ann_target = target,
               ann_value = serialized
           }

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
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` ()
651

652

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

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

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
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

687 688 689 690 691 692 693 694 695
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
696 697
       ; traceTc "Got TH result:" (text (show_th th_result))
       ; return (f expr_span th_result) }
698

699

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

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

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

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

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

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

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

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

755 756 757 758 759
        -- 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,
760
        -- see #5358 for an example that fell over when trying to
761 762 763 764
        -- reify a function with a "?" kind in it.  (These don't occur
        -- in type-correct programs.
        ; failIfErrsM

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

769
        -- Desugar
770
        ; ds_expr <- initDsTc (dsLExpr expr')
771 772 773
        -- 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
774
        ; either_hval <- tryM $ liftIO $
775 776
                         HscMain.hscCompileCoreExpr hsc_env src_span ds_expr
        ; case either_hval of {
777
            Left exn   -> fail_with_exn "compile and link" exn ;
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
            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
795
             do { mb_result <- run_and_convert expr_span hval
796 797
                ; case mb_result of
                    Left err     -> failWithTc err
Luite Stegeman's avatar
Luite Stegeman committed
798
                    Right result -> do { traceTc "Got HsSyn result:" (ppr_hs result)
799
                                       ; return $! result } }
800

801 802 803 804
        ; case either_tval of
            Right v -> return v
            Left se -> case fromException se of
                         Just IOEnvFailure -> failM -- Error already in Tc monad
805
                         _ -> fail_with_exn "run" se -- Exception
806
        }}}
807
  where
808
    -- see Note [Concealed TH exceptions]
809
    fail_with_exn :: Exception e => String -> e -> TcM a
810 811 812 813
    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
814
                        if show_code then text "Code:" <+> ppr expr else empty]
815
        failWithTc msg
816

Austin Seipp's avatar
Austin Seipp committed
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 869
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.



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

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
884 885
    effectively transforms (fail s) to
        qReport True s >> fail
886 887 888 889 890
    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.
891
    The 'fail' in TcM raises an IOEnvFailure exception
892

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

899 900 901 902
  * 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
903 904
    and then fail

905 906 907 908 909 910 911 912