{-# LANGUAGE CPP                    #-}
{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs                  #-}
{-# LANGUAGE InstanceSigs           #-}
{-# LANGUAGE MultiWayIf             #-}
{-# LANGUAGE ScopedTypeVariables    #-}
{-# LANGUAGE TupleSections          #-}
{-# LANGUAGE TypeFamilies           #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
{-# LANGUAGE NamedFieldPuns #-}

{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

-}

-- | Template Haskell splices
module GHC.Tc.Gen.Splice(
     tcTypedSplice, tcTypedBracket, tcUntypedBracket,
     runAnnotation,

     runMetaE, runMetaP, runMetaT, runMetaD, runQuasi,
     tcTopSpliceExpr, lookupThName_maybe,
     defaultRunMeta, runMeta', runRemoteModFinalizers,
     finishTH, runTopSplice
      ) where

import GHC.Prelude

import GHC.Driver.Errors
import GHC.Driver.Plugins
import GHC.Driver.Main
import GHC.Driver.Session
import GHC.Driver.Env
import GHC.Driver.Hooks
import GHC.Driver.Config.Diagnostic
import GHC.Driver.Config.Finder

import GHC.Hs

import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.TcType
import GHC.Tc.Gen.Expr
import GHC.Tc.Utils.Unify
import GHC.Tc.Utils.Env
import GHC.Tc.Types.Origin
import GHC.Tc.Types.Evidence
import GHC.Tc.Utils.Zonk
import GHC.Tc.Solver
import GHC.Tc.Utils.TcMType
import GHC.Tc.Gen.HsType
import GHC.Tc.Instance.Family
import GHC.Tc.Utils.Instantiate

import GHC.Core.Multiplicity
import GHC.Core.Coercion( etaExpandCoAxBranch )
import GHC.Core.Type as Type
import GHC.Core.TyCo.Rep as TyCoRep
import GHC.Core.FamInstEnv
import GHC.Core.InstEnv as InstEnv

import GHC.Builtin.Names.TH
import GHC.Builtin.Names
import GHC.Builtin.Types

import GHC.ThToHs
import GHC.HsToCore.Docs
import GHC.HsToCore.Expr
import GHC.HsToCore.Monad
import GHC.IfaceToCore
import GHC.Iface.Load

import GHCi.Message
import GHCi.RemoteTypes
import GHC.Runtime.Interpreter

import GHC.Rename.Splice( traceSplice, SpliceInfo(..))
import GHC.Rename.Expr
import GHC.Rename.Env
import GHC.Rename.Fixity ( lookupFixityRn_help )
import GHC.Rename.HsType

import GHC.Core.Class
import GHC.Core.TyCon
import GHC.Core.Coercion.Axiom
import GHC.Core.PatSyn
import GHC.Core.ConLike
import GHC.Core.DataCon as DataCon

import GHC.Types.FieldLabel
import GHC.Types.SrcLoc
import GHC.Types.Name.Env
import GHC.Types.Name.Set
import GHC.Types.Name.Reader
import GHC.Types.Name.Occurrence as OccName
import GHC.Types.Var
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Unique
import GHC.Types.Var.Set
import GHC.Types.Meta
import GHC.Types.Basic hiding( SuccessFlag(..) )
import GHC.Types.Error
import GHC.Types.Fixity as Hs
import GHC.Types.Annotations
import GHC.Types.Name
import GHC.Types.Unique.Map
import GHC.Serialized

import GHC.Unit.Finder
import GHC.Unit.Module
import GHC.Unit.Module.ModIface
import GHC.Unit.Module.Deps

import GHC.Utils.Misc
import GHC.Utils.Panic as Panic
import GHC.Utils.Panic.Plain
import GHC.Utils.Lexeme
import GHC.Utils.Outputable
import GHC.Utils.Logger
import GHC.Utils.Exception (throwIO, ErrorCall(..), SomeException(..))

import GHC.Utils.TmpFs ( newTempName, TempFileLifetime(..) )

import GHC.Data.FastString
import GHC.Data.Maybe( MaybeErr(..) )
import qualified GHC.Data.EnumSet as EnumSet

import Language.Haskell.Syntax.Basic (FieldLabelString(..))
import qualified Language.Haskell.TH as TH
-- THSyntax gives access to internal functions and data types
import qualified Language.Haskell.TH.Syntax as TH

#if defined(HAVE_INTERNAL_INTERPRETER)
-- Because GHC.Desugar might not be in the base library of the bootstrapping compiler
import GHC.Desugar      ( AnnotationWrapper(..) )
import Unsafe.Coerce    ( unsafeCoerce )
#endif

import Control.Monad
import Data.Binary
import Data.Binary.Get
import Data.List        ( find )
import Data.Maybe
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.Dynamic  ( fromDynamic, toDyn )
import qualified Data.IntMap as IntMap
import qualified Data.Map as Map
import Data.Typeable ( typeOf, Typeable, TypeRep, typeRep )
import Data.Data (Data)
import Data.Proxy    ( Proxy (..) )
import GHC.Parser.HaddockLex (lexHsDoc)
import GHC.Parser (parseIdentifier)
import GHC.Rename.Doc (rnHsDoc)



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

     Start here
         |
         V
      -----------     $      ------------   $
      |  Comp   | ---------> |  Splice  | -----|
      |   1     |            |    0     | <----|
      -----------            ------------
        ^     |                ^      |
      $ |     | [||]         $ |      | [||]
        |     v                |      v
   --------------          ----------------
   | Brack Comp |          | Brack Splice |
   |     2      |          |      1       |
   --------------          ----------------

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

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

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

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

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

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

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

* Variables are bound at the "current level"

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

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

* When a variable is used, checkWellStaged compares
        bind:  binding level, and
        use:   current level at usage site

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

        bind = use      Always OK (bound same stage as used)
                        [| \x -> $(f [| x |]) |]

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

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

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

  For example:
           f = ...
           g1 = $(map ...)         is OK
           g2 = $(f ...)           is not OK; because we haven't compiled f yet


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
     GHC.Hs syntax 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

     (The last step is different for decls, because they can *only* be
      top-level: we return the result of step 2.)

Note [Warnings for TH splices]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We only produce warnings for TH splices when the user requests so
(-fenable-th-splice-warnings). There are multiple reasons:

  * It's not clear that the user that compiles a splice is the author of the code
    that produces the warning. Think of the situation where they just splice in
    code from a third-party library that produces incomplete pattern matches.
    In this scenario, the user isn't even able to fix that warning.
  * Gathering information for producing the warnings (pattern-match check
    warnings in particular) is costly. There's no point in doing so if the user
    is not interested in those warnings.

That's why we store Origin flags in the Haskell AST. The functions from ThToHs
take such a flag and depending on whether TH splice warnings were enabled or
not, we pass FromSource (if the user requests warnings) or Generated
(otherwise). This is implemented in getThSpliceOrigin.

For correct pattern-match warnings it's crucial that we annotate the Origin
consistently (#17270). In the future we could offer the Origin as part of the
TH AST. That would enable us to give quotes from the current module get
FromSource origin, and/or third library authors to tag certain parts of
generated code as FromSource to enable warnings.
That effort is tracked in #14838.

Note [The life cycle of a TH quotation]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When desugaring a bracket (aka quotation), we want to produce Core
code that, when run, will produce the TH syntax tree for the quotation.
To that end, we want to desugar /renamed/ but not /typechecked/ code;
the latter is cluttered with the typechecker's elaboration that should
not appear in the TH syntax tree. So in (HsExpr GhcTc) tree, we must
have a (HsExpr GhcRn) for the quotation itself.

As such, when typechecking both typed and untyped brackets,
we keep a /renamed/ bracket in the extension field.

The HsBracketTc, the GhcTc ext field for both typed and untyped
brackets, contains:
  - The renamed quote :: HsQuote GhcRn -- for the desugarer
  - [PendingTcSplice]
  - The type of the quote
  - Maybe QuoteWrapper

Note that HsBracketTc stores the untyped (HsQuote GhcRn) for both typed and
untyped brackets. They are treated uniformly by the desugarer, and we can
easily construct untyped brackets from typed ones (with ExpBr).

See Note [Desugaring of brackets].

------------
Typed quotes
------------
Here is the life cycle of a /typed/ quote [|| e ||], whose datacon is
  HsTypedBracket (XTypedBracket p) (LHsExpr p)

  In pass p   (XTypedBracket p)       (LHsExpr p)
  -------------------------------------------
  GhcPs       Annotations only        LHsExpr GhcPs
  GhcRn       Annotations only        LHsExpr GhcRn
  GhcTc       HsBracketTc             LHsExpr GhcTc: unused!

Note that in the GhcTc tree, the second field (HsExpr GhcTc)
is entirely unused; the desugarer uses the (HsExpr GhcRn) from the
first field.

--------------
Untyped quotes
--------------
Here is the life cycle of an /untyped/ quote, whose datacon is
   HsUntypedBracket (XUntypedBracket p) (HsQuote p)

Here HsQuote is a sum-type of expressions [| e |], patterns [| p |],
types [| t |] etc.

  In pass p   (XUntypedBracket p)          (HsQuote p)
  -------------------------------------------------------
  GhcPs   Annotations only                 HsQuote GhcPs
  GhcRn   Annotations, [PendingRnSplice]   HsQuote GhcRn
  GhcTc   HsBracketTc                      HsQuote GhcTc: unused!

The difficulty is: the typechecker does not typecheck the body of an
untyped quote, so how do we make a (HsQuote GhcTc) to put in the
second field?

Answer: we use the extension constructor of HsQuote, namely XQuote,
and make all the other constructors into DataConCantHappen.  That is,
the only non-bottom value of type (HsQuote GhcTc) is (XQuote noExtField).
Hence the instances

  type instance XExpBr GhcTc = DataConCantHappen
  ...etc...

See the related Note [How brackets and nested splices are handled]

Note [Typechecking Overloaded Quotes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The main function for typechecking untyped quotations is `tcUntypedBracket`.

Consider an expression quote, `[| e |]`, its type is `forall m . Quote m => m Exp`.
Note carefully that this is overloaded: its type is not `Q Exp` for some fixed Q.

When we typecheck it we therefore create a template of a metavariable
`m` applied to `Exp` and emit a constraint `Quote m`. All this is done
in the `brackTy` function.  `brackTy` also selects the correct
contents type for the quotation (Exp, Type, Decs etc).

The meta variable and the constraint evidence variable are
returned together in a `QuoteWrapper` and then passed along to two further places
during compilation:

1. Typechecking nested splices (immediately in tcPendingSplice)
2. Desugaring quotations (see GHC.HsToCore.Quote)

`tcPendingSplice` takes the `m` type variable as an argument and
checks each nested splice against this variable `m`. During this
process the variable `m` can either be fixed to a specific value or
further constrained by the nested splices.

Once we have checked all the nested splices, the quote type is checked against
the expected return type.

The process is very simple and like typechecking a list where the quotation is
like the container and the splices are the elements of the list which must have
a specific type.

After the typechecking process is completed, the evidence variable for `Quote m`
and the type `m` is stored in a `QuoteWrapper` which is passed through the pipeline
and used when desugaring quotations.

Typechecking typed quotations is a similar idea but the `QuoteWrapper` is stored
in the `PendingStuff` as the nested splices are gathered up in a different way
to untyped splices. Untyped splices are found in the renamer but typed splices are
not typechecked and extracted until during typechecking.

Note [Lifecycle of an untyped splice, and PendingRnSplice]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Untyped splices $(f x) and quasiquotes [p| stuff |] have the following
life cycle. Remember, quasi-quotes are very like splices; see Note [Quasi-quote overview]).

The type structure is

  data HsExpr p = ...
    | HsUntypedSplice (XUntypedSplice p) (HsUntypedSplice p)

  data HsUntypedSplice p
    = HsUntypedSpliceExpr (XUntypedSpliceExpr p) (LHsExpr p)
    | HsQuasiQuote (XQuasiQuote p) (IdP id) (XRec p FastString)

Remember that untyped splices can occur in expressions, patterns,
types, and declarations.  So we have a HsUntypedSplice data
constructor in all four of these types.

Untyped splices never occur in (HsExpr GhcTc), and similarly
patterns etc. So we have

   type instance XUntypedSplice GhcTc = DataConCantHappen

Top-level and nested splices are handled differently.

-------------------------------------
Nested untyped splices/quasiquotes
----------------------------------
When we rename an /untyped/ bracket, such as
     [| f $(g x) |]
we name and lift out all the nested splices, so that when the
typechecker hits the bracket, it can typecheck those nested splices
without having to walk over the untyped bracket code.  Our example
[| f $(g x) |] parses as

    HsUntypedBracket _
       (HsApp (HsVar "f")
              (HsUntypedSplice _ (HsUntypedSpliceExpr _ (g x :: LHsExpr GhcPs)))

RENAMER (rnUntypedBracket):

* Set the ThStage to (Brack s (RnPendingUntyped ps_var))

* Rename the body

* Nested splices (which must be untyped) are renamed (rnUntypedSplice),
  and the results accumulated in ps_var. Each gets a fresh
  SplicePointName, 'spn'

* The SplicePointName connects the `PendingRnSplice` with the particular point
  in the syntax tree where that expression should be spliced in.  That point
  in the tree is identified by `(HsUntypedSpliceNested spn)`.  It is used by
  the desugarer, so that we ultimately generate something like
       let spn = g x
       in App (Var "f") spn

The result is
    HsUntypedBracket
        [PendingRnSplice UntypedExpSplice spn (g x  :: LHsExpr GHcRn)]
        (HsApp (HsVar f) (HsUntypedSplice (HsUntypedSpliceNested spn)
                                          (HsUntypedSpliceExpr _ (g x :: LHsExpr GhcRn))))

Note that a nested splice, such as the `$(g x)` now appears twice:
  - In the PendingRnSplice: this is the version that will later be typechecked
  - In the HsUntypedSpliceExpr in the body of the bracket. This copy is used
    only for pretty printing.

NB: a single untyped bracket can contain many splices, each of a different
`UntypedSpliceFlavour`. For example

   [| let $e0 in (f :: $e1) $e2 (\ $e -> body ) |] + 1

Here $e0 is a declaration splice, $e1 is a type splice, $e2 is an
expression splice, and $e3 is a pattern splice.  The `PendingRnSplice`
keeps track of which is which through its `UntypedSpliceFlavour`
field.

TYPECHECKER (tcUntypedBracket): see also Note [Typechecking Overloaded Quotes]

* Typecheck the [PendingRnSplice] individually, to give [PendingTcSplice]
  So PendingTcSplice is used for both typed and untyped splices.

* 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 (m Pat)

* Stash the whole lot inside a HsBracketTc

Result is:
    HsUntypedBracket
        (HsBracketTc { hsb_splices = [PendingTcSplice spn (g x  :: LHsExpr GHcTc)]
                     , hsb_quote = HsApp (HsVar f)
                                         (HsUntypedSplice (HsUntypedSpliceNested spn)
                                            (HsUntypedSpliceExpr _ (g x :: LHsExpr GhcRn)))
                     })
        (XQuote noExtField)

NB in the typechecker output, the original payload (which would now
have type (HsQuote GhcTc) is stubbed off with (XQuote noExtField). The payload
is now in the hsb_quote field of the HsBracketTc.


-------------------------------------
Top-level untyped splices/quasiquotes
-------------------------------------
A top-level splice (not inside a bracket) does not need a SpliceName,
nor does a top-level splice ever end up inside a PendingRnSplice;
hence HsUntypedSpliceTop does not have a SplicePointName field.

Example $(g x).  This is parsed as

  HsUntypedSplice _ (HsUntypedSpliceExpr _ ((g x) :: LHsExpr GhcPs))

Renamer: the renamer runs the splice, so the output of the renamer looks like

  HsUntypedSplice (HsUntypedSpliceTop fins (e2 :: LHsExpr GhcRn))
                  (HsUntypedSpliceExpr ((g x) :: LHsExpr GhcRn))

where 'e2' is the result of running (g x) to
             produce the syntax tree for 'e2'
      'fins' is a bunch of TH finalisers, to be run later.

Typechecker: the typechecker simply adds the finalisers, and
typechecks e2, discarding the HsUntypedSplice altogether.


Note [Lifecycle of an typed splice, and PendingTcSplice]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

----------------------
Nested, typed splices
----------------------
When we typecheck a /typed/ bracket, we lift nested splices out as
`PendingTcSplice`, very similar to Note [PendingRnSplice]. Again, the
splice needs a SplicePointName, for the desguarer to use to connect
the splice expression with the point in the syntax tree where it is
used.  Example:
     [||  f $$(g 2)||]

Parser: this is parsed as

    HsTypedBracket _ (HsApp (HsVar "f")
                            (HsTypedSplice _ (g 2 :: LHsExpr GhcPs)))

RENAMER (rnTypedSplice): the renamer adds a SplicePointName, spn:

    HsTypedBracket _ (HsApp (HsVar "f")
                            (HsTypedSplice spn (g x :: LHsExpr GhcRn)))

TYPECHECKER (tcTypedBracket):

* Set the ThStage to (Brack s (TcPending ps_var lie_var))

* Typecheck the body, and keep the elaborated result (despite never using it!)

* Nested splices (which must be typed) are typechecked by tcNestedSplice, and
  the results accumulated in ps_var; their constraints accumulate in lie_var

* Result is a HsTypedBracket (HsBracketTc rn_brack ty quote_wrapper pending_splices) tc_brack
  where rn_brack is the untyped renamed exp quote constructed from the typed renamed expression :: HsQuote GhcRn

Just like untyped brackets, dump the output into a HsBracketTc.

    HsTypedBracket
        (HsBracketTc { hsb_splices = [PendingTcSplice spn (g x  :: LHsExpr GHcTc)]
                     , hsb_quote = HsApp (HsVar f)
                                         (HsUntypedSplice (HsUntypedSpliceNested spn)
                                            (HsUntypedSpliceExpr _ (g x :: LHsExpr GhcRn)))
                     })
        (panic "should never be looked at")

NB: we never need to represent typed /nested/ splices in phase GhcTc.

There are only typed expression splices so `PendingTcSplice` doesn't have a
flavour field.


--------------------------------
Top-level, typed splices $$(f x)
--------------------------------
Typed splices are renamed and typechecked, but only actually run in
the zonker, after typechecking. See Note [Running typed splices in the zonker]

* Output of parser:
  HsTypedSplice _ (e :: HsExpr GhcPs)

* Output of renamer:
  HsTypedSplice (n :: SplicePointName) (e :: HsExpr GhcRn)

* Output of typechecker: (top-level splices only)
  HsTypedSplice (del_splice :: DelayedSplice) (e :: HsExpr GhcTc)
  where 'del_splice' is something the zonker can run to produce
           the syntax tree to splice in.
           See Note [Running typed splices in the zonker]

Note [Desugaring of brackets]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In both cases, desugaring happens like this:
  * Hs*Bracket is desugared by GHC.HsToCore.Quote.dsBracket using the renamed
    expression held in `HsBracketTc` (`type instance X*Bracket GhcTc = HsBracketTc`). It

      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
         is why we leave the *renamed* expression attached to the bracket:
         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 GHC.HsToCore.Quote (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 (GHC.HsToCore.Quote.repSplice).

Example:
    Source:       f = [| Just $(g 3) |]
      The [| |] part is a HsUntypedBracket GhcPs

    Typechecked:  f = [| Just ${s7}(g 3) |]{s7 = g Int 3}
      The [| |] part is a HsUntypedBracket GhcTc, containing *renamed*
        (not typechecked) expression (see Note [The life cycle of a TH quotation])
      The "s7" is the "splice point"; the (g Int 3) part
        is a typechecked expression

    Desugared:    f = do { s7 <- g Int 3
                         ; return (ConE "Data.Maybe.Just" s7) }

-}

{-
************************************************************************
*                                                                      *
\subsection{Main interface + stubs for the non-GHCI case
*                                                                      *
************************************************************************
-}

tcTypedBracket    :: HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcUntypedBracket  :: HsExpr GhcRn -> HsQuote GhcRn -> [PendingRnSplice] -> ExpRhoType
                  -> TcM (HsExpr GhcTc)
tcTypedSplice :: Name -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
        -- None of these functions add constraints to the LIE

runAnnotation     :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
{-
************************************************************************
*                                                                      *
\subsection{Quoting an expression}
*                                                                      *
************************************************************************
-}

-- See Note [How brackets and nested splices are handled]
tcTypedBracket :: HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTypedBracket HsExpr GhcRn
rn_expr LHsExpr GhcRn
expr ExpRhoType
res_ty
  = SDoc -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (LHsExpr GhcRn -> SDoc
quotationCtxtDoc LHsExpr GhcRn
expr) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
    do { ThStage
cur_stage <- TcM ThStage
getStage
       ; IORef [PendingTcSplice]
ps_ref <- [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) (IORef [PendingTcSplice])
forall a env. a -> IOEnv env (IORef a)
newMutVar []
       ; TcRef WantedConstraints
lie_var <- TcM (TcRef WantedConstraints)
getConstraintVar   -- Any constraints arising from nested splices
                                       -- should get thrown into the constraint set
                                       -- from outside the bracket

       -- Make a new type variable for the type of the overall quote
       ; Type
m_var <- Id -> Type
mkTyVarTy (Id -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Id
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) Id
mkMetaTyVar
       -- Make sure the type variable satisfies Quote
       ; Id
ev_var <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitQuoteWanted Type
m_var
       -- Bundle them together so they can be used in GHC.HsToCore.Quote for desugaring
       -- brackets.
       ; let wrapper :: QuoteWrapper
wrapper = Id -> Type -> QuoteWrapper
QuoteWrapper Id
ev_var Type
m_var
       -- Typecheck expr to make sure it is valid.
       -- The typechecked expression won't be used, so we just discard it
       --   (See Note [The life cycle of a TH quotation] in GHC.Hs.Expr)
       -- We'll typecheck it again when we splice it in somewhere
       ; (GenLocated SrcSpanAnnA (HsExpr GhcTc)
tc_expr, Type
expr_ty) <- ThStage
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), Type)
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), Type)
forall a. ThStage -> TcM a -> TcM a
setStage (ThStage -> PendingStuff -> ThStage
Brack ThStage
cur_stage (IORef [PendingTcSplice]
-> TcRef WantedConstraints -> QuoteWrapper -> PendingStuff
TcPending IORef [PendingTcSplice]
ps_ref TcRef WantedConstraints
lie_var QuoteWrapper
wrapper)) (TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), Type)
 -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), Type))
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), Type)
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), Type)
forall a b. (a -> b) -> a -> b
$
                                Type -> TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type)
forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
ManyTy (TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type))
-> TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type)
forall a b. (a -> b) -> a -> b
$
                                -- Scale by Many, TH lifting is currently nonlinear (#18465)
                                LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRhoNC LHsExpr GhcRn
expr
                                -- NC for no context; tcBracket does that
       ; let rep :: Type
rep = (() :: Constraint) => Type -> Type
Type -> Type
getRuntimeRep Type
expr_ty
       ; Type
meta_ty <- Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
m_var Type
expr_ty
       ; [PendingTcSplice]
ps' <- IORef [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a env. IORef a -> IOEnv env a
readMutVar IORef [PendingTcSplice]
ps_ref
       ; Id
codeco <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Id
tcLookupId Name
unsafeCodeCoerceName
       ; Type
bracket_ty <- Type -> Type -> Type
mkAppTy Type
m_var (Type -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Type
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
expTyConName
       ; let brack_tc :: HsBracketTc
brack_tc = HsBracketTc { hsb_quote :: HsQuote GhcRn
hsb_quote = XExpBr GhcRn -> LHsExpr GhcRn -> HsQuote GhcRn
forall p. XExpBr p -> LHsExpr p -> HsQuote p
ExpBr XExpBr GhcRn
NoExtField
noExtField LHsExpr GhcRn
expr, hsb_ty :: Type
hsb_ty = Type
bracket_ty
                                    , hsb_wrap :: Maybe QuoteWrapper
hsb_wrap  = QuoteWrapper -> Maybe QuoteWrapper
forall a. a -> Maybe a
Just QuoteWrapper
wrapper, hsb_splices :: [PendingTcSplice]
hsb_splices = [PendingTcSplice]
ps' }
             -- The tc_expr is stored here so that the expression can be used in HIE files.
             brack_expr :: HsExpr GhcTc
brack_expr = XTypedBracket GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XTypedBracket p -> LHsExpr p -> HsExpr p
HsTypedBracket XTypedBracket GhcTc
HsBracketTc
brack_tc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
tc_expr
       ; CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTc
-> Type
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcWrapResultO (String -> CtOrigin
Shouldn'tHappenOrigin String
"TH typed bracket expression")
                       HsExpr GhcRn
rn_expr
                       (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc (LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc
forall (id :: Pass).
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkHsApp (HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap (QuoteWrapper -> HsWrapper
applyQuoteWrapper QuoteWrapper
wrapper)
                                                  (Id -> [Type] -> LHsExpr GhcTc
nlHsTyApp Id
codeco [Type
rep, Type
expr_ty]))
                                      (HsExpr GhcTc -> GenLocated SrcSpanAnnA (HsExpr GhcTc)
forall a an. a -> LocatedAn an a
noLocA HsExpr GhcTc
brack_expr)))
                       Type
meta_ty ExpRhoType
res_ty }

-- See Note [Typechecking Overloaded Quotes]
tcUntypedBracket :: HsExpr GhcRn
-> HsQuote GhcRn
-> [PendingRnSplice]
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcUntypedBracket HsExpr GhcRn
rn_expr HsQuote GhcRn
brack [PendingRnSplice]
ps ExpRhoType
res_ty
  = do { String -> SDoc -> TcRn ()
traceTc String
"tc_bracket untyped" (HsQuote GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsQuote GhcRn
brack SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [PendingRnSplice] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PendingRnSplice]
ps)

       -- Create the type m Exp for expression bracket, m Type for a type
       -- bracket and so on. The brack_info is a Maybe because the
       -- VarBracket ('a) isn't overloaded, but also shouldn't contain any
       -- splices.
       ; (Maybe QuoteWrapper
brack_info, Type
expected_type) <- HsQuote GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy HsQuote GhcRn
brack

       -- Match the expected type with the type of all the internal
       -- splices. They might have further constrained types and if they do
       -- we want to reflect that in the overall type of the bracket.
       ; [PendingTcSplice]
ps' <- case QuoteWrapper -> Type
quoteWrapperTyVarTy (QuoteWrapper -> Type) -> Maybe QuoteWrapper -> DFunInstType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe QuoteWrapper
brack_info of
                  Just Type
m_var -> (PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice)
-> [PendingRnSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type
-> PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice Type
m_var) [PendingRnSplice]
ps
                  DFunInstType
Nothing -> Bool
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a. HasCallStack => Bool -> a -> a
assert ([PendingRnSplice] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PendingRnSplice]
ps) (IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
 -> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice])
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a b. (a -> b) -> a -> b
$ [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []

       -- Notice that we don't attempt to typecheck the body
       -- of the bracket, which is in brack.
       ; String -> SDoc -> TcRn ()
traceTc String
"tc_bracket done untyped" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
expected_type)

       -- Unify the overall type of the bracket with the expected result type
       ; CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTc
-> Type
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcWrapResultO CtOrigin
BracketOrigin HsExpr GhcRn
rn_expr
            (XUntypedBracket GhcTc -> HsQuote GhcTc -> HsExpr GhcTc
forall p. XUntypedBracket p -> HsQuote p -> HsExpr p
HsUntypedBracket (HsBracketTc { hsb_quote :: HsQuote GhcRn
hsb_quote = HsQuote GhcRn
brack, hsb_ty :: Type
hsb_ty = Type
expected_type
                                           , hsb_wrap :: Maybe QuoteWrapper
hsb_wrap = Maybe QuoteWrapper
brack_info, hsb_splices :: [PendingTcSplice]
hsb_splices = [PendingTcSplice]
ps' })
                              (XXQuote GhcTc -> HsQuote GhcTc
forall p. XXQuote p -> HsQuote p
XQuote XXQuote GhcTc
NoExtField
noExtField))
                -- (XQuote noExtField): see Note [The life cycle of a TH quotation] in GHC.Hs.Expr
            Type
expected_type ExpRhoType
res_ty

       }

-- | A type variable with kind * -> * named "m"
mkMetaTyVar :: TcM TyVar
mkMetaTyVar :: IOEnv (Env TcGblEnv TcLclEnv) Id
mkMetaTyVar =
  FastString -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
newNamedFlexiTyVar (String -> FastString
fsLit String
"m") ((() :: Constraint) => Type -> Type -> Type
Type -> Type -> Type
mkVisFunTyMany Type
liftedTypeKind Type
liftedTypeKind)


-- | For a type 'm', emit the constraint 'Quote m'.
emitQuoteWanted :: Type -> TcM EvVar
emitQuoteWanted :: Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitQuoteWanted Type
m_var =  do
        TyCon
quote_con <- Name -> TcM TyCon
tcLookupTyCon Name
quoteClassName
        CtOrigin -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitWantedEvVar CtOrigin
BracketOrigin (Type -> IOEnv (Env TcGblEnv TcLclEnv) Id)
-> Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
forall a b. (a -> b) -> a -> b
$
          TyCon -> [Type] -> Type
mkTyConApp TyCon
quote_con [Type
m_var]

---------------
-- | Compute the expected type of a quotation, and also the QuoteWrapper in
-- the case where it is an overloaded quotation. All quotation forms are
-- overloaded aprt from Variable quotations ('foo)
brackTy :: HsQuote GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy :: HsQuote GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy HsQuote GhcRn
b =
  let mkTy :: Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
n = do
        -- New polymorphic type variable for the bracket
        Type
m_var <- Id -> Type
mkTyVarTy (Id -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Id
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) Id
mkMetaTyVar
        -- Emit a Quote constraint for the bracket
        Id
ev_var <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitQuoteWanted Type
m_var
        -- Construct the final expected type of the quote, for example
        -- m Exp or m Type
        Type
final_ty <- Type -> Type -> Type
mkAppTy Type
m_var (Type -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Type
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
n
        -- Return the evidence variable and metavariable to be used during
        -- desugaring.
        let wrapper :: QuoteWrapper
wrapper = Id -> Type -> QuoteWrapper
QuoteWrapper Id
ev_var Type
m_var
        (Maybe QuoteWrapper, Type) -> TcM (Maybe QuoteWrapper, Type)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (QuoteWrapper -> Maybe QuoteWrapper
forall a. a -> Maybe a
Just QuoteWrapper
wrapper, Type
final_ty)
  in
  case HsQuote GhcRn
b of
    (VarBr {}) -> (Maybe QuoteWrapper
forall a. Maybe a
Nothing,) (Type -> (Maybe QuoteWrapper, Type))
-> IOEnv (Env TcGblEnv TcLclEnv) Type
-> TcM (Maybe QuoteWrapper, Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
nameTyConName
                                           -- Result type is Var (not Quote-monadic)
    (ExpBr {})  -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
expTyConName  -- Result type is m Exp
    (TypBr {})  -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
typeTyConName -- Result type is m Type
    (DecBrG {}) -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
decsTyConName -- Result type is m [Dec]
    (PatBr {})  -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
patTyConName  -- Result type is m Pat
    (DecBrL {}) -> String -> TcM (Maybe QuoteWrapper, Type)
forall a. HasCallStack => String -> a
panic String
"tcBrackTy: Unexpected DecBrL"

---------------
-- | Typechecking a pending splice from a untyped bracket
tcPendingSplice :: TcType -- Metavariable for the expected overall type of the
                          -- quotation.
                -> PendingRnSplice
                -> TcM PendingTcSplice
tcPendingSplice :: Type
-> PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice Type
m_var (PendingRnSplice UntypedSpliceFlavour
flavour Name
splice_name LHsExpr GhcRn
expr)
  -- See Note [Typechecking Overloaded Quotes]
  = do { Type
meta_ty <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
meta_ty_name
         -- Expected type of splice, e.g. m Exp
       ; let expected_type :: Type
expected_type = Type -> Type -> Type
mkAppTy Type
m_var Type
meta_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- Type -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
ManyTy (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr Type
expected_type
                  -- Scale by Many, TH lifting is currently nonlinear (#18465)
       ; PendingTcSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> LHsExpr GhcTc -> PendingTcSplice
PendingTcSplice Name
splice_name LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') }
  where
     meta_ty_name :: Name
meta_ty_name = case UntypedSpliceFlavour
flavour of
                       UntypedSpliceFlavour
UntypedExpSplice  -> Name
expTyConName
                       UntypedSpliceFlavour
UntypedPatSplice  -> Name
patTyConName
                       UntypedSpliceFlavour
UntypedTypeSplice -> Name
typeTyConName
                       UntypedSpliceFlavour
UntypedDeclSplice -> Name
decsTyConName

---------------
-- Takes a m and tau and returns the type m (TExp tau)
tcTExpTy :: TcType -> TcType -> TcM TcType
tcTExpTy :: Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
m_ty Type
exp_ty
  = do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Type -> Bool
isTauTy Type
exp_ty) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErr (Type -> TcRnMessage
TcRnTypedTHWithPolyType Type
exp_ty)
       ; TyCon
codeCon <- Name -> TcM TyCon
tcLookupTyCon Name
codeTyConName
       ; let rep :: Type
rep = (() :: Constraint) => Type -> Type
Type -> Type
getRuntimeRep Type
exp_ty
       ; Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCon -> [Type] -> Type
mkTyConApp TyCon
codeCon [Type
rep, Type
m_ty, Type
exp_ty]) }

quotationCtxtDoc :: LHsExpr GhcRn -> SDoc
quotationCtxtDoc :: LHsExpr GhcRn -> SDoc
quotationCtxtDoc LHsExpr GhcRn
br_body
  = SDoc -> SumArity -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the Template Haskell quotation")
         SumArity
2 (SDoc -> SDoc
thTyBrackets (SDoc -> SDoc) -> (LHsExpr GhcRn -> SDoc) -> LHsExpr GhcRn -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsExpr GhcRn -> SDoc
GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr (LHsExpr GhcRn -> SDoc) -> LHsExpr GhcRn -> SDoc
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn
br_body)


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


{-
************************************************************************
*                                                                      *
\subsection{Splicing an expression}
*                                                                      *
************************************************************************
-}

tcTypedSplice :: Name -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTypedSplice Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
  = SDoc -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (Name -> LHsExpr GhcRn -> SDoc
typedSpliceCtxtDoc Name
splice_name LHsExpr GhcRn
expr) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
    SrcSpan -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr)    (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ do
    { ThStage
stage <- TcM ThStage
getStage
    ; case ThStage
stage of
          Splice {}            -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
          Brack ThStage
pop_stage PendingStuff
pend -> ThStage
-> PendingStuff
-> Name
-> LHsExpr GhcRn
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcNestedSplice ThStage
pop_stage PendingStuff
pend Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
          RunSplice TcRef [ForeignRef (Q ())]
_          ->
            -- See Note [RunSplice ThLevel] in "GHC.Tc.Types".
            String -> SDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> SDoc -> a
pprPanic (String
"tcSpliceExpr: attempted to typecheck a splice when " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                      String
"running another splice") (Maybe Name -> LHsExpr GhcRn -> SDoc
forall (p :: Pass).
OutputableBndrId p =>
Maybe Name -> LHsExpr (GhcPass p) -> SDoc
pprTypedSplice (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
splice_name) LHsExpr GhcRn
expr)
          ThStage
Comp                 -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
    }

{- 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
GHC.Rename.Splice). 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').

-}

------------------
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
  = do { -- Typecheck the expression,
         -- making sure it has type Q (T res_ty)
         Type
res_ty <- ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Type
expTypeToType ExpRhoType
res_ty
       ; Type
q_type <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
qTyConName
       -- Top level splices must still be of type Q (TExp a)
       ; Type
meta_exp_ty <- Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
q_type Type
res_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
q_expr <- SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr SpliceType
Typed (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
                   LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
expr Type
meta_exp_ty
       ; TcLclEnv
lcl_env <- TcRnIf TcGblEnv TcLclEnv TcLclEnv
forall gbl lcl. TcRnIf gbl lcl lcl
getLclEnv
       ; let delayed_splice :: DelayedSplice
delayed_splice
              = TcLclEnv -> LHsExpr GhcRn -> Type -> LHsExpr GhcTc -> DelayedSplice
DelayedSplice TcLclEnv
lcl_env LHsExpr GhcRn
expr Type
res_ty LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
q_expr
       ; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XTypedSplice GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XTypedSplice p -> LHsExpr p -> HsExpr p
HsTypedSplice XTypedSplice GhcTc
DelayedSplice
delayed_splice LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
q_expr)

       }

-------------------
tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
-- Note [How top-level splices are handled]
-- Type check an expression that is the body of a top-level splice
--   (the caller will compile and run it)
-- Note that set the level to Splice, regardless of the original level,
-- before typechecking the expression.  For example:
--      f x = $( ...$(g 3) ... )
-- The recursive call to tcCheckPolyExpr will simply expand the
-- inner escape before dealing with the outer one

tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr SpliceType
isTypedSplice TcM (LHsExpr GhcTc)
tc_action
  = TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall r. TcM r -> TcM r
checkNoErrs (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$  -- checkNoErrs: must not try to run the thing
                   -- if the type checker fails!
    ThStage -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. ThStage -> TcM a -> TcM a
setStage (SpliceType -> ThStage
Splice SpliceType
isTypedSplice) (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
    do {    -- Typecheck the expression
         (Maybe (GenLocated SrcSpanAnnA (HsExpr GhcTc))
mb_expr', WantedConstraints
wanted) <- IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> TcM
     (Maybe (GenLocated SrcSpanAnnA (HsExpr GhcTc)), WantedConstraints)
forall a. TcM a -> TcM (Maybe a, WantedConstraints)
tryCaptureConstraints TcM (LHsExpr GhcTc)
IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
tc_action
             -- If tc_action fails (perhaps because of insoluble constraints)
             -- we want to capture and report those constraints, else we may
             -- just get a silent failure (#20179). Hence the 'try' part.

       ; Bag EvBind
const_binds <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wanted

       ; case Maybe (GenLocated SrcSpanAnnA (HsExpr GhcTc))
mb_expr' of
            Maybe (GenLocated SrcSpanAnnA (HsExpr GhcTc))
Nothing    -> IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall env a. IOEnv env a
failM   -- In this case simplifyTop should have
                                  -- reported some errors
            Just GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsExpr GhcTc -> TcM (LHsExpr GhcTc))
-> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ TcEvBinds -> LHsExpr GhcTc -> LHsExpr GhcTc
mkHsDictLet (Bag EvBind -> TcEvBinds
EvBinds Bag EvBind
const_binds) LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' }

------------------
tcNestedSplice :: ThStage -> PendingStuff -> Name
                -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    -- See Note [How brackets and nested splices are handled]
    -- A splice inside brackets
tcNestedSplice :: ThStage
-> PendingStuff
-> Name
-> LHsExpr GhcRn
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcNestedSplice ThStage
pop_stage (TcPending IORef [PendingTcSplice]
ps_var TcRef WantedConstraints
lie_var q :: QuoteWrapper
q@(QuoteWrapper Id
_ Type
m_var)) Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
  = do { Type
res_ty <- ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Type
expTypeToType ExpRhoType
res_ty
       ; let rep :: Type
rep = (() :: Constraint) => Type -> Type
Type -> Type
getRuntimeRep Type
res_ty
       ; Type
meta_exp_ty <- Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
m_var Type
res_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- ThStage
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall a. ThStage -> TcM a -> TcM a
setStage ThStage
pop_stage (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall a b. (a -> b) -> a -> b
$
                  TcRef WantedConstraints
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. TcRef WantedConstraints -> TcM a -> TcM a
setConstraintVar TcRef WantedConstraints
lie_var (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
                  LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
expr Type
meta_exp_ty
       ; Id
untype_code <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Id
tcLookupId Name
unTypeCodeName
       ; let expr'' :: LHsExpr GhcTc
expr'' = LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc
forall (id :: Pass).
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkHsApp
                        (HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap (QuoteWrapper -> HsWrapper
applyQuoteWrapper QuoteWrapper
q)
                          (Id -> [Type] -> LHsExpr GhcTc
nlHsTyApp Id
untype_code [Type
rep, Type
res_ty])) LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr'
       ; [PendingTcSplice]
ps <- IORef [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a env. IORef a -> IOEnv env a
readMutVar IORef [PendingTcSplice]
ps_var
       ; IORef [PendingTcSplice] -> [PendingTcSplice] -> TcRn ()
forall a env. IORef a -> a -> IOEnv env ()
writeMutVar IORef [PendingTcSplice]
ps_var (Name -> LHsExpr GhcTc -> PendingTcSplice
PendingTcSplice Name
splice_name LHsExpr GhcTc
expr'' PendingTcSplice -> [PendingTcSplice] -> [PendingTcSplice]
forall a. a -> [a] -> [a]
: [PendingTcSplice]
ps)

       -- The returned expression is ignored; it's in the pending splices
       ; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return HsExpr GhcTc
stubNestedSplice }

tcNestedSplice ThStage
_ PendingStuff
_ Name
splice_name LHsExpr GhcRn
_ ExpRhoType
_
  = String -> SDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcNestedSplice: rename stage found" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
splice_name)


------------------
-- This is called in the zonker
-- See Note [Running typed splices in the zonker]
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTc)
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTc)
runTopSplice (DelayedSplice TcLclEnv
lcl_env LHsExpr GhcRn
orig_expr Type
res_ty LHsExpr GhcTc
q_expr)
  = TcLclEnv -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall gbl a.
TcLclEnv -> TcRnIf gbl TcLclEnv a -> TcRnIf gbl TcLclEnv a
restoreLclEnv TcLclEnv
lcl_env (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
    do { Type
zonked_ty <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcType Type
res_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
zonked_q_expr <- LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
zonkTopLExpr LHsExpr GhcTc
q_expr
        -- See Note [Collecting modFinalizers in typed splices].
       ; TcRef [ForeignRef (Q ())]
modfinalizers_ref <- [ForeignRef (Q ())]
-> TcRnIf TcGblEnv TcLclEnv (TcRef [ForeignRef (Q ())])
forall a gbl lcl. a -> TcRnIf gbl lcl (TcRef a)
newTcRef []
         -- Run the expression
       ; GenLocated SrcSpanAnnA (HsExpr GhcPs)
expr2 <- ThStage -> TcM (LHsExpr GhcPs) -> TcM (LHsExpr GhcPs)
forall a. ThStage -> TcM a -> TcM a
setStage (TcRef [ForeignRef (Q ())] -> ThStage
RunSplice TcRef [ForeignRef (Q ())]
modfinalizers_ref) (TcM (LHsExpr GhcPs) -> TcM (LHsExpr GhcPs))
-> TcM (LHsExpr GhcPs) -> TcM (LHsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$
                    LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
runMetaE LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
zonked_q_expr
       ; [ForeignRef (Q ())]
mod_finalizers <- TcRef [ForeignRef (Q ())]
-> TcRnIf TcGblEnv TcLclEnv [ForeignRef (Q ())]
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef [ForeignRef (Q ())]
modfinalizers_ref
       ; ThModFinalizers -> TcRn ()
addModFinalizersWithLclEnv (ThModFinalizers -> TcRn ()) -> ThModFinalizers -> TcRn ()
forall a b. (a -> b) -> a -> b
$ [ForeignRef (Q ())] -> ThModFinalizers
ThModFinalizers [ForeignRef (Q ())]
mod_finalizers
       -- We use orig_expr here and not q_expr when tracing as a call to
       -- unsafeCodeCoerce is added to the original expression by the
       -- typechecker when typed quotes are type checked.
       ; SpliceInfo -> TcRn ()
traceSplice (SpliceInfo { spliceDescription :: String
spliceDescription = String
"expression"
                                 , spliceIsDecl :: Bool
spliceIsDecl      = Bool
False
                                 , spliceSource :: Maybe (LHsExpr GhcRn)
spliceSource      = GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> Maybe (GenLocated SrcSpanAnnA (HsExpr GhcRn))
forall a. a -> Maybe a
Just LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
orig_expr
                                 , spliceGenerated :: SDoc
spliceGenerated   = GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr GenLocated SrcSpanAnnA (HsExpr GhcPs)
expr2 })
        -- Rename and typecheck the spliced-in expression,
        -- making sure it has type res_ty
        -- These steps should never fail; this is a *typed* splice
       ; (GenLocated SrcSpanAnnA (HsExpr GhcTc)
res, WantedConstraints
wcs) <-
            IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
 -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), WantedConstraints))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), WantedConstraints)
forall a b. (a -> b) -> a -> b
$
              SDoc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (LHsExpr GhcTc -> SDoc
spliceResultDoc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
zonked_q_expr) (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall a b. (a -> b) -> a -> b
$ do
                { (GenLocated SrcSpanAnnA (HsExpr GhcRn)
exp3, FreeVars
_fvs) <- LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr LHsExpr GhcPs
GenLocated SrcSpanAnnA (HsExpr GhcPs)
expr2
                ; LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
exp3 Type
zonked_ty }
       ; Bag EvBind
ev <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wcs
       ; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc -> TcM (HsExpr GhcTc))
-> HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (HsExpr GhcTc) -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc (TcEvBinds -> LHsExpr GhcTc -> LHsExpr GhcTc
mkHsDictLet (Bag EvBind -> TcEvBinds
EvBinds Bag EvBind
ev) LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
res)
       }


{-
************************************************************************
*                                                                      *

*                                                                      *
************************************************************************
-}

typedSpliceCtxtDoc :: SplicePointName -> LHsExpr GhcRn -> SDoc
typedSpliceCtxtDoc :: Name -> LHsExpr GhcRn -> SDoc
typedSpliceCtxtDoc Name
n LHsExpr GhcRn
splice
  = SDoc -> SumArity -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the Template Haskell splice")
         SumArity
2 (Maybe Name -> LHsExpr GhcRn -> SDoc
forall (p :: Pass).
OutputableBndrId p =>
Maybe Name -> LHsExpr (GhcPass p) -> SDoc
pprTypedSplice (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
n) LHsExpr GhcRn
splice)

spliceResultDoc :: LHsExpr GhcTc -> SDoc
spliceResultDoc :: LHsExpr GhcTc -> SDoc
spliceResultDoc LHsExpr GhcTc
expr
  = [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the result of the splice:"
        , SumArity -> SDoc -> SDoc
nest SumArity
2 (Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'$' SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> GenLocated SrcSpanAnnA (HsExpr GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr)
        , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"To see what the splice expanded to, use -ddump-splices"]

stubNestedSplice :: HsExpr GhcTc
-- Used when we need a (LHsExpr GhcTc) that we are never going
-- to look at.  We could use "panic" but that's confusing if we ever
-- do a debug-print.  The warning is because this should never happen
-- /except/ when doing debug prints.
stubNestedSplice :: HsExpr GhcTc
stubNestedSplice = Bool -> String -> SDoc -> HsExpr GhcTc -> HsExpr GhcTc
forall a. HasCallStack => Bool -> String -> SDoc -> a -> a
warnPprTrace Bool
True String
"stubNestedSplice" SDoc
forall doc. IsOutput doc => doc
empty (HsExpr GhcTc -> HsExpr GhcTc) -> HsExpr GhcTc -> HsExpr GhcTc
forall a b. (a -> b) -> a -> b
$
                   XLitE GhcTc -> HsLit GhcTc -> HsExpr GhcTc
forall p. XLitE p -> HsLit p -> HsExpr p
HsLit XLitE GhcTc
EpAnnCO
noComments (String -> HsLit GhcTc
forall (p :: Pass). String -> HsLit (GhcPass p)
mkHsString String
"stubNestedSplice")


{-
************************************************************************
*                                                                      *
        Annotations
*                                                                      *
************************************************************************
-}

runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
runAnnotation CoreAnnTarget
target LHsExpr GhcRn
expr = do
    -- Find the classes we want instances for in order to call toAnnotationWrapper
    SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
    Class
data_class <- Name -> TcM Class
tcLookupClass Name
dataClassName
    Id
to_annotation_wrapper_id <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Id
tcLookupId Name
toAnnotationWrapperName

    -- 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
    GenLocated SrcSpanAnnA (HsExpr GhcTc)
zonked_wrapped_expr' <- LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
zonkTopLExpr (GenLocated SrcSpanAnnA (HsExpr GhcTc)
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr SpliceType
Untyped (
           do { (GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr', Type
expr_ty) <- LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRhoNC LHsExpr GhcRn
expr
                -- 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
                -- and hence ensures the appropriate dictionary is bound by const_binds
              ; HsWrapper
wrapper <- CtOrigin -> [Type] -> [Type] -> TcM HsWrapper
instCall CtOrigin
AnnOrigin [Type
expr_ty] [Class -> [Type] -> Type
mkClassPred Class
data_class [Type
expr_ty]]
              ; let loc' :: SrcSpanAnnA
loc' = SrcSpan -> SrcSpanAnnA
forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc
              ; let specialised_to_annotation_wrapper_expr :: GenLocated SrcSpanAnnA (HsExpr GhcTc)
specialised_to_annotation_wrapper_expr
                      = SrcSpanAnnA
-> HsExpr GhcTc -> GenLocated SrcSpanAnnA (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrapper
                                 (XVar GhcTc -> LIdP GhcTc -> HsExpr GhcTc
forall p. XVar p -> LIdP p -> HsExpr p
HsVar XVar GhcTc
NoExtField
noExtField (SrcSpanAnnN -> Id -> GenLocated SrcSpanAnnN Id
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnN
forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) Id
to_annotation_wrapper_id)))
              ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnA
-> HsExpr GhcTc -> GenLocated SrcSpanAnnA (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' (XApp GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp XApp GhcTc
EpAnnCO
noComments
                                LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
specialised_to_annotation_wrapper_expr LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr'))
                                })

    -- 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
    Serialized
serialized <- LHsExpr GhcTc -> TcM Serialized
runMetaAW LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
zonked_wrapped_expr'
    Annotation -> TcM Annotation
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Annotation {
               ann_target :: CoreAnnTarget
ann_target = CoreAnnTarget
target,
               ann_value :: Serialized
ann_value = Serialized
serialized
           }

convertAnnotationWrapper :: ForeignHValue -> TcM Serialized
convertAnnotationWrapper :: ForeignHValue -> TcM Serialized
convertAnnotationWrapper ForeignHValue
fhv = do
  Interp
interp <- TcM Interp
tcGetInterp
  case Interp -> InterpInstance
interpInstance Interp
interp of
    ExternalInterp {} -> THResultType -> ForeignHValue -> TcM Serialized
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THAnnWrapper ForeignHValue
fhv
#if defined(HAVE_INTERNAL_INTERPRETER)
    InterpInstance
InternalInterp    -> do
      HValue
annotation_wrapper <- IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue)
-> IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a b. (a -> b) -> a -> b
$ Interp -> ForeignHValue -> IO HValue
forall a. Interp -> ForeignRef a -> IO a
wormhole Interp
interp ForeignHValue
fhv
      Serialized -> TcM Serialized
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Serialized -> TcM Serialized) -> Serialized -> TcM Serialized
forall a b. (a -> b) -> a -> b
$
        case HValue -> AnnotationWrapper
forall a b. a -> b
unsafeCoerce HValue
annotation_wrapper of
           AnnotationWrapper a
value | let serialized :: Serialized
serialized = (a -> [Word8]) -> a -> Serialized
forall a. Typeable a => (a -> [Word8]) -> a -> Serialized
toSerialized a -> [Word8]
forall a. Data a => a -> [Word8]
serializeWithData a
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
               Serialized -> ()
seqSerialized Serialized
serialized () -> Serialized -> Serialized
forall a b. a -> b -> b
`seq` Serialized
serialized

-- | Force the contents of the Serialized value so weknow it doesn't contain any bottoms
seqSerialized :: Serialized -> ()
seqSerialized :: Serialized -> ()
seqSerialized (Serialized TypeRep
the_type [Word8]
bytes) = TypeRep
the_type TypeRep -> () -> ()
forall a b. a -> b -> b
`seq` [Word8]
bytes [Word8] -> () -> ()
forall a b. [a] -> b -> b
`seqList` ()

#endif

{-
************************************************************************
*                                                                      *
\subsection{Running an expression}
*                                                                      *
************************************************************************
-}

runQuasi :: TH.Q a -> TcM a
runQuasi :: forall a. Q a -> TcM a
runQuasi Q a
act = Q a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Quasi m => Q a -> m a
TH.runQ Q a
act

runRemoteModFinalizers :: ThModFinalizers -> TcM ()
runRemoteModFinalizers :: ThModFinalizers -> TcRn ()
runRemoteModFinalizers (ThModFinalizers [ForeignRef (Q ())]
finRefs) = do
  let withForeignRefs :: [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [] [RemoteRef a] -> IO b
f = [RemoteRef a] -> IO b
f []
      withForeignRefs (ForeignRef a
x : [ForeignRef a]
xs) [RemoteRef a] -> IO b
f = ForeignRef a -> (RemoteRef a -> IO b) -> IO b
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef a
x ((RemoteRef a -> IO b) -> IO b) -> (RemoteRef a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \RemoteRef a
r ->
        [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef a]
xs (([RemoteRef a] -> IO b) -> IO b)
-> ([RemoteRef a] -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \[RemoteRef a]
rs -> [RemoteRef a] -> IO b
f (RemoteRef a
r RemoteRef a -> [RemoteRef a] -> [RemoteRef a]
forall a. a -> [a] -> [a]
: [RemoteRef a]
rs)
  Interp
interp <- TcM Interp
tcGetInterp
  case Interp -> InterpInstance
interpInstance Interp
interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
    InterpInstance
InternalInterp -> do
      [Q ()]
qs <- IO [Q ()] -> IOEnv (Env TcGblEnv TcLclEnv) [Q ()]
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([ForeignRef (Q ())]
-> ([RemoteRef (Q ())] -> IO [Q ()]) -> IO [Q ()]
forall {a} {b}. [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef (Q ())]
finRefs (([RemoteRef (Q ())] -> IO [Q ()]) -> IO [Q ()])
-> ([RemoteRef (Q ())] -> IO [Q ()]) -> IO [Q ()]
forall a b. (a -> b) -> a -> b
$ (RemoteRef (Q ()) -> IO (Q ())) -> [RemoteRef (Q ())] -> IO [Q ()]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM RemoteRef (Q ()) -> IO (Q ())
forall a. RemoteRef a -> IO a
localRef)
      Q () -> TcRn ()
forall a. Q a -> TcM a
runQuasi (Q () -> TcRn ()) -> Q () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ [Q ()] -> Q ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [Q ()]
qs
#endif

    ExternalInterp IServConfig
conf IServ
iserv -> IServConfig -> IServ -> (IServInstance -> TcRn ()) -> TcRn ()
forall (m :: * -> *) a.
(MonadIO m, ExceptionMonad m) =>
IServConfig -> IServ -> (IServInstance -> m a) -> m a
withIServ_ IServConfig
conf IServ
iserv ((IServInstance -> TcRn ()) -> TcRn ())
-> (IServInstance -> TcRn ()) -> TcRn ()
forall a b. (a -> b) -> a -> b
$ \IServInstance
i -> do
      TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
      Maybe (ForeignRef (IORef QState))
th_state <- TcRef (Maybe (ForeignRef (IORef QState)))
-> TcRnIf TcGblEnv TcLclEnv (Maybe (ForeignRef (IORef QState)))
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg)
      case Maybe (ForeignRef (IORef QState))
th_state of
        Maybe (ForeignRef (IORef QState))
Nothing -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return () -- TH was not started, nothing to do
        Just ForeignRef (IORef QState)
fhv -> do
          IO () -> TcRn ()
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ ForeignRef (IORef QState)
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef (IORef QState)
fhv ((RemoteRef (IORef QState) -> IO ()) -> IO ())
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRef (IORef QState)
st ->
            [ForeignRef (Q ())] -> ([RemoteRef (Q ())] -> IO ()) -> IO ()
forall {a} {b}. [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef (Q ())]
finRefs (([RemoteRef (Q ())] -> IO ()) -> IO ())
-> ([RemoteRef (Q ())] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[RemoteRef (Q ())]
qrefs ->
              IServInstance -> Put -> IO ()
writeIServ IServInstance
i (Message (QResult ()) -> Put
forall a. Message a -> Put
putMessage (RemoteRef (IORef QState)
-> [RemoteRef (Q ())] -> Message (QResult ())
RunModFinalizers RemoteRef (IORef QState)
st [RemoteRef (Q ())]
qrefs))
          () <- IServInstance -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH IServInstance
i []
          IServInstance -> TcRn ()
forall a. Binary a => IServInstance -> TcM a
readQResult IServInstance
i

runQResult
  :: (a -> String)
  -> (Origin -> SrcSpan -> a -> b)
  -> (ForeignHValue -> TcM a)
  -> SrcSpan
  -> ForeignHValue {- TH.Q a -}
  -> TcM b
runQResult :: forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult a -> String
show_th Origin -> SrcSpan -> a -> b
f ForeignHValue -> TcM a
runQ SrcSpan
expr_span ForeignHValue
hval
  = do { a
th_result <- ForeignHValue -> TcM a
runQ ForeignHValue
hval
       ; Origin
th_origin <- TcM Origin
getThSpliceOrigin
       ; String -> SDoc -> TcRn ()
traceTc String
"Got TH result:" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (a -> String
show_th a
th_result))
       ; b -> TcM b
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Origin -> SrcSpan -> a -> b
f Origin
th_origin SrcSpan
expr_span a
th_result) }


-----------------
runMeta :: (MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
        -> LHsExpr GhcTc
        -> TcM hs_syn
runMeta :: forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn
unwrap LHsExpr GhcTc
e = do
    Hooks
hooks <- IOEnv (Env TcGblEnv TcLclEnv) Hooks
forall (m :: * -> *). HasHooks m => m Hooks
getHooks
    case Hooks -> Maybe (MetaHook (IOEnv (Env TcGblEnv TcLclEnv)))
runMetaHook Hooks
hooks of
        Maybe (MetaHook (IOEnv (Env TcGblEnv TcLclEnv)))
Nothing -> MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn
unwrap MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
defaultRunMeta LHsExpr GhcTc
e
        Just MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
h  -> MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn
unwrap MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
h LHsExpr GhcTc
e

defaultRunMeta :: MetaHook TcM
defaultRunMeta :: MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
defaultRunMeta (MetaE LHsExpr GhcPs -> MetaResult
r)
  = (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> MetaResult)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsExpr GhcPs -> MetaResult
GenLocated SrcSpanAnnA (HsExpr GhcPs) -> MetaResult
r (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
 -> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SDoc)
-> (SrcSpan
    -> ForeignHValue
    -> TcM
         (Either
            RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((Exp -> String)
-> (Origin
    -> SrcSpan
    -> Exp
    -> Either
         RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> (ForeignHValue -> TcM Exp)
-> SrcSpan
-> ForeignHValue
-> TcM
     (Either
        RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Exp -> String
forall a. Ppr a => a -> String
TH.pprint Origin
-> SrcSpan -> Exp -> Either RunSpliceFailReason (LHsExpr GhcPs)
Origin
-> SrcSpan
-> Exp
-> Either
     RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs))
convertToHsExpr ForeignHValue -> TcM Exp
runTHExp)
defaultRunMeta (MetaP LPat GhcPs -> MetaResult
r)
  = (GenLocated SrcSpanAnnA (Pat GhcPs) -> MetaResult)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LPat GhcPs -> MetaResult
GenLocated SrcSpanAnnA (Pat GhcPs) -> MetaResult
r (IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
 -> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (GenLocated SrcSpanAnnA (Pat GhcPs) -> SDoc)
-> (SrcSpan
    -> ForeignHValue
    -> TcM
         (Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs))))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True GenLocated SrcSpanAnnA (Pat GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((Pat -> String)
-> (Origin
    -> SrcSpan
    -> Pat
    -> Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs)))
-> (ForeignHValue -> TcM Pat)
-> SrcSpan
-> ForeignHValue
-> TcM
     (Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs)))
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Pat -> String
forall a. Ppr a => a -> String
TH.pprint Origin -> SrcSpan -> Pat -> Either RunSpliceFailReason (LPat GhcPs)
Origin
-> SrcSpan
-> Pat
-> Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs))
convertToPat ForeignHValue -> TcM Pat
runTHPat)
defaultRunMeta (MetaT LHsType GhcPs -> MetaResult
r)
  = (GenLocated SrcSpanAnnA (HsType GhcPs) -> MetaResult)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsType GhcPs -> MetaResult
GenLocated SrcSpanAnnA (HsType GhcPs) -> MetaResult
r (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
 -> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (GenLocated SrcSpanAnnA (HsType GhcPs) -> SDoc)
-> (SrcSpan
    -> ForeignHValue
    -> TcM
         (Either
            RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs))))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True GenLocated SrcSpanAnnA (HsType GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((Type -> String)
-> (Origin
    -> SrcSpan
    -> Type
    -> Either
         RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs)))
-> (ForeignHValue -> TcM Type)
-> SrcSpan
-> ForeignHValue
-> TcM
     (Either
        RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs)))
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Type -> String
forall a. Ppr a => a -> String
TH.pprint Origin
-> SrcSpan -> Type -> Either RunSpliceFailReason (LHsType GhcPs)
Origin
-> SrcSpan
-> Type
-> Either
     RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs))
convertToHsType ForeignHValue -> TcM Type
runTHType)
defaultRunMeta (MetaD [LHsDecl GhcPs] -> MetaResult
r)
  = ([GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> MetaResult)
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [LHsDecl GhcPs] -> MetaResult
[GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> MetaResult
r (IOEnv
   (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
 -> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
    -> IOEnv
         (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ([GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> SDoc)
-> (SrcSpan
    -> ForeignHValue
    -> TcM
         (Either
            RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)]))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True [GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr (([Dec] -> String)
-> (Origin
    -> SrcSpan
    -> [Dec]
    -> Either
         RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> (ForeignHValue -> TcM [Dec])
-> SrcSpan
-> ForeignHValue
-> TcM
     (Either
        RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult [Dec] -> String
forall a. Ppr a => a -> String
TH.pprint Origin
-> SrcSpan -> [Dec] -> Either RunSpliceFailReason [LHsDecl GhcPs]
Origin
-> SrcSpan
-> [Dec]
-> Either
     RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
convertToHsDecls ForeignHValue -> TcM [Dec]
runTHDec)
defaultRunMeta (MetaAW Serialized -> MetaResult
r)
  = (Serialized -> MetaResult) -> TcM Serialized -> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Serialized -> MetaResult
r (TcM Serialized -> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> TcM Serialized)
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (Serialized -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> LHsExpr GhcTc
-> TcM Serialized
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
False (SDoc -> Serialized -> SDoc
forall a b. a -> b -> a
const SDoc
forall doc. IsOutput doc => doc
empty) ((ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> SrcSpan
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized)
forall a b. a -> b -> a
const ((ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
 -> SrcSpan
 -> ForeignHValue
 -> TcM (Either RunSpliceFailReason Serialized))
-> (ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> SrcSpan
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized)
forall a b. (a -> b) -> a -> b
$ (Serialized -> Either RunSpliceFailReason Serialized)
-> TcM Serialized -> TcM (Either RunSpliceFailReason Serialized)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Serialized -> Either RunSpliceFailReason Serialized
forall a b. b -> Either a b
Right (TcM Serialized -> TcM (Either RunSpliceFailReason Serialized))
-> (ForeignHValue -> TcM Serialized)
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForeignHValue -> TcM Serialized
convertAnnotationWrapper)
    -- We turn off showing the code in meta-level exceptions because doing so exposes
    -- the toAnnotationWrapper function that we slap around the user's code

----------------
runMetaAW :: LHsExpr GhcTc         -- Of type AnnotationWrapper
          -> TcM Serialized
runMetaAW :: LHsExpr GhcTc -> TcM Serialized
runMetaAW = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM Serialized)
-> LHsExpr GhcTc -> TcM Serialized
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM Serialized
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f Serialized
metaRequestAW

runMetaE :: LHsExpr GhcTc          -- Of type (Q Exp)
         -> TcM (LHsExpr GhcPs)
runMetaE :: LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
runMetaE = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LHsExpr GhcPs)
metaRequestE

runMetaP :: LHsExpr GhcTc          -- Of type (Q Pat)
         -> TcM (LPat GhcPs)
runMetaP :: LHsExpr GhcTc -> TcM (LPat GhcPs)
runMetaP = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs)))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM (LPat GhcPs)
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LPat GhcPs)
metaRequestP

runMetaT :: LHsExpr GhcTc          -- Of type (Q Type)
         -> TcM (LHsType GhcPs)
runMetaT :: LHsExpr GhcTc -> TcM (LHsType GhcPs)
runMetaT = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs)))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM (LHsType GhcPs)
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LHsType GhcPs)
metaRequestT

runMetaD :: LHsExpr GhcTc          -- Of type Q [Dec]
         -> TcM [LHsDecl GhcPs]
runMetaD :: LHsExpr GhcTc -> TcM [LHsDecl GhcPs]
runMetaD = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc
 -> IOEnv
      (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
 -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM [LHsDecl GhcPs]
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f [LHsDecl GhcPs]
metaRequestD

{- Note [Errors in desugaring a splice]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What should we do if there are errors when desugaring a splice? We should
abort. There are several cases to consider:

(a) The desugarer hits an unrecoverable error and fails in the monad.
(b) The desugarer hits a recoverable error, reports it, and continues.
(c) The desugarer reports a fatal warning (with -Werror), reports it, and continues.
(d) The desugarer reports a non-fatal warning, and continues.

Each case is tested in th/T19709[abcd].

General principle: we wish to report all messages from dealing with a splice
eagerly, as these messages arise during an earlier stage than type-checking
generally. It's also likely that a compile-time warning from spliced code
will be easier to understand then an error that arises from processing the
code the splice produces. (Rationale: the warning will be about the code the
user actually wrote, not what is generated.)

Case (a): We have no choice but to abort here, but we must make sure that
the messages are printed or logged before aborting. Logging them is annoying,
because we're in the type-checker, and the messages are DsMessages, from the
desugarer. So we report and then fail in the monad. This case is detected
by the fact that initDsTc returns Nothing.

Case (b): We detect this case by looking for errors in the messages returned
from initDsTc and aborting if we spot any (after printing, of course). Note
that initDsTc will return a Just ds_expr in this case, but we don't wish to
use the (likely very bogus) expression.

Case (c): This is functionally the same as (b), except that the expression
isn't bogus. We still don't wish to use it, as the user's request for -Werror
tells us not to.

Case (d): We report the warnings and then carry on with the expression.
This might result in warnings printed out of source order, but this is
appropriate, as the warnings from the splice arise from an earlier stage
of compilation.

Previously, we failed to abort in cases (b) and (c), leading to #19709.
-}

---------------
runMeta' :: Bool                 -- Whether code should be printed in the exception message
         -> (hs_syn -> SDoc)                                    -- how to print the code
         -> (SrcSpan -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn)) -- How to run x
         -> LHsExpr GhcTc        -- Of type x; typically x = Q TH.Exp, or
                                 --    something like that
         -> TcM hs_syn           -- Of type t
runMeta' :: forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
    -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
show_code hs_syn -> SDoc
ppr_hs SrcSpan -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn)
run_and_convert LHsExpr GhcTc
expr
  = do  { String -> SDoc -> TcRn ()
traceTc String
"About to run" (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr)
        ; TcRn ()
recordThSpliceUse -- seems to be the best place to do this,
                            -- we catch all kinds of splices and annotations.

        -- 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 on.  Mind you, the staging restrictions mean we won't
        -- actually run f, but it still seems wrong. And, more concretely,
        -- see #5358 for an example that fell over when trying to
        -- reify a function with an unlifted kind in it.  (These don't occur
        -- in type-correct programs.)
        ; TcRn ()
failIfErrsM

        -- run plugins
        ; HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- Plugins
-> PluginOperation
     (IOEnv (Env TcGblEnv TcLclEnv))
     (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall (m :: * -> *) a.
Monad m =>
Plugins -> PluginOperation m a -> a -> m a
withPlugins (HscEnv -> Plugins
hsc_plugins HscEnv
hsc_env) Plugin -> [String] -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
PluginOperation
  (IOEnv (Env TcGblEnv TcLclEnv))
  (GenLocated SrcSpanAnnA (HsExpr GhcTc))
spliceRunAction LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr

        -- Desugar
        ; (Messages DsMessage
ds_msgs, Maybe CoreExpr
mb_ds_expr) <- DsM CoreExpr -> TcM (Messages DsMessage, Maybe CoreExpr)
forall a. DsM a -> TcM (Messages DsMessage, Maybe a)
initDsTc (LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr')

        -- Print any messages (even warnings) eagerly: they might be helpful if anything
        -- goes wrong. See Note [Errors in desugaring a splice]. This happens in all
        -- cases.
        ; Logger
logger <- IOEnv (Env TcGblEnv TcLclEnv) Logger
forall (m :: * -> *). HasLogger m => m Logger
getLogger
        ; DiagOpts
diag_opts <- DynFlags -> DiagOpts
initDiagOpts (DynFlags -> DiagOpts)
-> IOEnv (Env TcGblEnv TcLclEnv) DynFlags
-> IOEnv (Env TcGblEnv TcLclEnv) DiagOpts
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
        ; NoDiagnosticOpts
print_config <- DynFlags -> NoDiagnosticOpts
DynFlags -> DiagnosticOpts DsMessage
initDsMessageOpts (DynFlags -> NoDiagnosticOpts)
-> IOEnv (Env TcGblEnv TcLclEnv) DynFlags
-> IOEnv (Env TcGblEnv TcLclEnv) NoDiagnosticOpts
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
        ; IO () -> TcRn ()
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ Logger
-> DiagnosticOpts DsMessage
-> DiagOpts
-> Messages DsMessage
-> IO ()
forall a.
Diagnostic a =>
Logger -> DiagnosticOpts a -> DiagOpts -> Messages a -> IO ()
printMessages Logger
logger NoDiagnosticOpts
DiagnosticOpts DsMessage
print_config DiagOpts
diag_opts Messages DsMessage
ds_msgs

        ; CoreExpr
ds_expr <- case Maybe CoreExpr
mb_ds_expr of
            Maybe CoreExpr
Nothing      -> IOEnv (Env TcGblEnv TcLclEnv) CoreExpr
forall env a. IOEnv env a
failM   -- Case (a) from Note [Errors in desugaring a splice]
            Just CoreExpr
ds_expr ->  -- There still might be a fatal warning or recoverable
                             -- Cases (b) and (c) from Note [Errors in desugaring a splice]
              do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Messages DsMessage -> Bool
forall e. Messages e -> Bool
errorsOrFatalWarningsFound Messages DsMessage
ds_msgs)
                     TcRn ()
forall env a. IOEnv env a
failM
                 ; CoreExpr -> IOEnv (Env TcGblEnv TcLclEnv) CoreExpr
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
ds_expr }

        -- Compile and link it; might fail if linking fails
        ; SrcSpan
src_span <- TcRn SrcSpan
getSrcSpanM
        ; String -> SDoc -> TcRn ()
traceTc String
"About to run (desugared)" (CoreExpr -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoreExpr
ds_expr)
        ; Either IOEnvFailure (ForeignHValue, [Linkable], PkgsLoaded)
either_hval <- IOEnv
  (Env TcGblEnv TcLclEnv) (ForeignHValue, [Linkable], PkgsLoaded)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (Either IOEnvFailure (ForeignHValue, [Linkable], PkgsLoaded))
forall env r. IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM (IOEnv
   (Env TcGblEnv TcLclEnv) (ForeignHValue, [Linkable], PkgsLoaded)
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (Either IOEnvFailure (ForeignHValue, [Linkable], PkgsLoaded)))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (ForeignHValue, [Linkable], PkgsLoaded)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (Either IOEnvFailure (ForeignHValue, [Linkable], PkgsLoaded))
forall a b. (a -> b) -> a -> b
$ IO (ForeignHValue, [Linkable], PkgsLoaded)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (ForeignHValue, [Linkable], PkgsLoaded)
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ForeignHValue, [Linkable], PkgsLoaded)
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (ForeignHValue, [Linkable], PkgsLoaded))
-> IO (ForeignHValue, [Linkable], PkgsLoaded)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (ForeignHValue, [Linkable], PkgsLoaded)
forall a b. (a -> b) -> a -> b
$
                         HscEnv
-> SrcSpan
-> CoreExpr
-> IO (ForeignHValue, [Linkable], PkgsLoaded)
GHC.Driver.Main.hscCompileCoreExpr HscEnv
hsc_env SrcSpan
src_span CoreExpr
ds_expr
        ; case Either IOEnvFailure (ForeignHValue, [Linkable], PkgsLoaded)
either_hval of {
            Left IOEnvFailure
exn   -> SplicePhase -> IOEnvFailure -> TcM hs_syn
forall e a. Exception e => SplicePhase -> e -> TcM a
fail_with_exn SplicePhase
SplicePhase_CompileAndLink IOEnvFailure
exn ;
            Right (ForeignHValue
hval, [Linkable]
needed_mods, PkgsLoaded
needed_pkgs) -> 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-catching 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 :: SrcSpan
expr_span = GenLocated SrcSpanAnnA (HsExpr GhcTc) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr
        ; [Linkable] -> PkgsLoaded -> TcRn ()
recordThNeededRuntimeDeps [Linkable]
needed_mods PkgsLoaded
needed_pkgs
        ; Either SomeException hs_syn
either_tval <- TcM hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn)
forall env r. IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM (TcM hs_syn
 -> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn))
-> TcM hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn)
forall a b. (a -> b) -> a -> b
$
                         SrcSpan -> TcM hs_syn -> TcM hs_syn
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
expr_span (TcM hs_syn -> TcM hs_syn) -> TcM hs_syn -> TcM hs_syn
forall a b. (a -> b) -> a -> b
$ -- Set the span so that qLocation can
                                                -- see where this splice is
             do { Either RunSpliceFailReason hs_syn
mb_result <- SrcSpan -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn)
run_and_convert SrcSpan
expr_span ForeignHValue
hval
                ; case Either RunSpliceFailReason hs_syn
mb_result of
                    Left RunSpliceFailReason
err     -> TcRnMessage -> TcM hs_syn
forall a. TcRnMessage -> TcM a
failWithTc (Maybe String -> RunSpliceFailReason -> TcRnMessage
TcRnRunSpliceFailure Maybe String
forall a. Maybe a
Nothing RunSpliceFailReason
err)
                    Right hs_syn
result -> do { String -> SDoc -> TcRn ()
traceTc String
"Got HsSyn result:" (hs_syn -> SDoc
ppr_hs hs_syn
result)
                                       ; hs_syn -> TcM hs_syn
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (hs_syn -> TcM hs_syn) -> hs_syn -> TcM hs_syn
forall a b. (a -> b) -> a -> b
$! hs_syn
result } }

        ; case Either SomeException hs_syn
either_tval of
            Right hs_syn
v -> hs_syn -> TcM hs_syn
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return hs_syn
v
            Left SomeException
se -> case SomeException -> Maybe IOEnvFailure
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se of
                         Just IOEnvFailure
IOEnvFailure -> TcM hs_syn
forall env a. IOEnv env a
failM -- Error already in Tc monad
                         Maybe IOEnvFailure
_ -> SplicePhase -> SomeException -> TcM hs_syn
forall e a. Exception e => SplicePhase -> e -> TcM a
fail_with_exn SplicePhase
SplicePhase_Run SomeException
se -- Exception
        }}}
  where
    -- see Note [Concealed TH exceptions]
    fail_with_exn :: Exception e => SplicePhase -> e -> TcM a
    fail_with_exn :: forall e a. Exception e => SplicePhase -> e -> TcM a
fail_with_exn SplicePhase
phase e
exn = do
        String
exn_msg <- IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> IOEnv (Env TcGblEnv TcLclEnv) String)
-> IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a b. (a -> b) -> a -> b
$ e -> IO String
forall e. Exception e => e -> IO String
Panic.safeShowException e
exn
        TcRnMessage -> TcM a
forall a. TcRnMessage -> TcM a
failWithTc
          (TcRnMessage -> TcM a) -> TcRnMessage -> TcM a
forall a b. (a -> b) -> a -> b
$ SplicePhase
-> SomeException -> String -> LHsExpr GhcTc -> Bool -> TcRnMessage
TcRnSpliceThrewException SplicePhase
phase (e -> SomeException
forall e. Exception e => e -> SomeException
SomeException e
exn) String
exn_msg LHsExpr GhcTc
expr Bool
show_code

{-
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
`GHC.Tc.Gen.Splice.runTopSplice` and the expression inserted into the AST as normal.



Note [Exceptions in TH]
~~~~~~~~~~~~~~~~~~~~~~~
Suppose we have something like this
        $( f 4 )
where
        f :: Int -> Q [Dec]
        f n | n>3       = fail "Too many declarations"
            | otherwise = ...

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
    effectively transforms (fail s) to
        qReport True s >> fail
    where 'qReport' comes from the Quasi class and fail from its monad
    superclass.

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

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

  * 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
    and then fail

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.


To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
-}

instance TH.Quasi TcM where
  qNewName :: String -> TcM Name
qNewName String
s = do { Unique
u <- TcRnIf TcGblEnv TcLclEnv Unique
forall gbl lcl. TcRnIf gbl lcl Unique
newUnique
                  ; let i :: Integer
i = SumArity -> Integer
forall a. Integral a => a -> Integer
toInteger (Unique -> SumArity
getKey Unique
u)
                  ; Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Integer -> Name
TH.mkNameU String
s Integer
i) }

  -- 'msg' is forced to ensure exceptions don't escape,
  -- see Note [Exceptions in TH]
  qReport :: Bool -> String -> TcRn ()
qReport Bool
True String
msg  = String -> TcRn () -> TcRn ()
forall a b. [a] -> b -> b
seqList String
msg (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcRnMessage
TcRnReportCustomQuasiError Bool
True String
msg
  qReport Bool
False String
msg = String -> TcRn () -> TcRn ()
forall a b. [a] -> b -> b
seqList String
msg (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addDiagnostic (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcRnMessage
TcRnReportCustomQuasiError Bool
False String
msg

  qLocation :: TcM TH.Loc
  qLocation :: TcM Loc
qLocation = do { GenModule Unit
m <- IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule
                 ; SrcSpan
l <- TcRn SrcSpan
getSrcSpanM
                 ; RealSrcSpan
r <- case SrcSpan
l of
                        UnhelpfulSpan UnhelpfulSpanReason
_ -> String -> SDoc -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"qLocation: Unhelpful location"
                                                    (SrcSpan -> SDoc
forall a. Outputable a => a -> SDoc
ppr SrcSpan
l)
                        RealSrcSpan RealSrcSpan
s Maybe BufSpan
_ -> RealSrcSpan -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return RealSrcSpan
s
                 ; Loc -> TcM Loc
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TH.Loc { loc_filename :: String
TH.loc_filename = FastString -> String
unpackFS (RealSrcSpan -> FastString
srcSpanFile RealSrcSpan
r)
                                  , loc_module :: String
TH.loc_module   = ModuleName -> String
moduleNameString (GenModule Unit -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName GenModule Unit
m)
                                  , loc_package :: String
TH.loc_package  = Unit -> String
forall u. IsUnitId u => u -> String
unitString (GenModule Unit -> Unit
forall unit. GenModule unit -> unit
moduleUnit GenModule Unit
m)
                                  , loc_start :: CharPos
TH.loc_start = (RealSrcSpan -> SumArity
srcSpanStartLine RealSrcSpan
r, RealSrcSpan -> SumArity
srcSpanStartCol RealSrcSpan
r)
                                  , loc_end :: CharPos
TH.loc_end = (RealSrcSpan -> SumArity
srcSpanEndLine   RealSrcSpan
r, RealSrcSpan -> SumArity
srcSpanEndCol   RealSrcSpan
r) }) }

  qLookupName :: Bool -> String -> TcM (Maybe Name)
qLookupName       = Bool -> String -> TcM (Maybe Name)
lookupName
  qReify :: Name -> TcM Info
qReify            = Name -> TcM Info
reify
  qReifyFixity :: Name -> TcM (Maybe Fixity)
qReifyFixity Name
nm   = Name -> TcM Name
lookupThName Name
nm TcM Name -> (Name -> TcM (Maybe Fixity)) -> TcM (Maybe Fixity)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> TcM (Maybe Fixity)
reifyFixity
  qReifyType :: Name -> TcM Type
qReifyType        = Name -> TcM Type
reifyTypeOfThing
  qReifyInstances :: Name -> [Type] -> TcM [Dec]
qReifyInstances   = Name -> [Type] -> TcM [Dec]
reifyInstances
  qReifyRoles :: Name -> TcM [Role]
qReifyRoles       = Name -> TcM [Role]
reifyRoles
  qReifyAnnotations :: forall a. Data a => AnnLookup -> TcM [a]
qReifyAnnotations = AnnLookup -> TcM [a]
forall a. Data a => AnnLookup -> TcM [a]
reifyAnnotations
  qReifyModule :: Module -> TcM ModuleInfo
qReifyModule      = Module -> TcM ModuleInfo
reifyModule
  qReifyConStrictness :: Name -> TcM [DecidedStrictness]
qReifyConStrictness Name
nm = do { Name
nm' <- Name -> TcM Name
lookupThName Name
nm
                              ; DataCon
dc  <- Name -> TcM DataCon
tcLookupDataCon Name
nm'
                              ; let bangs :: [HsImplBang]
bangs = DataCon -> [HsImplBang]
dataConImplBangs DataCon
dc
                              ; [DecidedStrictness] -> TcM [DecidedStrictness]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((HsImplBang -> DecidedStrictness)
-> [HsImplBang] -> [DecidedStrictness]
forall a b. (a -> b) -> [a] -> [b]
map HsImplBang -> DecidedStrictness
reifyDecidedStrictness [HsImplBang]
bangs) }

        -- For qRecover, discard error messages if
        -- the recovery action is chosen.  Otherwise
        -- we'll only fail higher up.
  qRecover :: forall a. TcM a -> TcM a -> TcM a
qRecover TcM a
recover TcM a
main = TcM a -> TcM a -> TcM a
forall a. TcM a -> TcM a -> TcM a
tryTcDiscardingErrs TcM a
recover TcM a
main

  qGetPackageRoot :: IOEnv (Env TcGblEnv TcLclEnv) String
qGetPackageRoot = do
    DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
    String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> IOEnv (Env TcGblEnv TcLclEnv) String)
-> String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a b. (a -> b) -> a -> b
$ String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"." (DynFlags -> Maybe String
workingDirectory DynFlags
dflags)

  qAddDependentFile :: String -> TcRn ()
qAddDependentFile String
fp = do
    TcRef [String]
ref <- (TcGblEnv -> TcRef [String])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [String])
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef [String]
tcg_dependent_files TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
    [String]
dep_files <- TcRef [String] -> TcRnIf TcGblEnv TcLclEnv [String]
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef [String]
ref
    TcRef [String] -> [String] -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef TcRef [String]
ref (String
fpString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
dep_files)

  qAddTempFile :: String -> IOEnv (Env TcGblEnv TcLclEnv) String
qAddTempFile String
suffix = do
    DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
    Logger
logger <- IOEnv (Env TcGblEnv TcLclEnv) Logger
forall (m :: * -> *). HasLogger m => m Logger
getLogger
    TmpFs
tmpfs  <- HscEnv -> TmpFs
hsc_tmpfs (HscEnv -> TmpFs)
-> TcRnIf TcGblEnv TcLclEnv HscEnv
-> IOEnv (Env TcGblEnv TcLclEnv) TmpFs
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
    IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> IOEnv (Env TcGblEnv TcLclEnv) String)
-> IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a b. (a -> b) -> a -> b
$ Logger
-> TmpFs -> TempDir -> TempFileLifetime -> String -> IO String
newTempName Logger
logger TmpFs
tmpfs (DynFlags -> TempDir
tmpDir DynFlags
dflags) TempFileLifetime
TFL_GhcSession String
suffix

  qAddTopDecls :: [Dec] -> TcRn ()
qAddTopDecls [Dec]
thds = do
      SrcSpan
l <- TcRn SrcSpan
getSrcSpanM
      Origin
th_origin <- TcM Origin
getThSpliceOrigin
      let either_hval :: Either RunSpliceFailReason [LHsDecl GhcPs]
either_hval = Origin
-> SrcSpan -> [Dec] -> Either RunSpliceFailReason [LHsDecl GhcPs]
convertToHsDecls Origin
th_origin SrcSpan
l [Dec]
thds
      [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
ds <- case Either RunSpliceFailReason [LHsDecl GhcPs]
either_hval of
              Left RunSpliceFailReason
exn -> TcRnMessage
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a. TcRnMessage -> TcM a
failWithTc
                            (TcRnMessage
 -> IOEnv
      (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> TcRnMessage
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a b. (a -> b) -> a -> b
$ Maybe String -> RunSpliceFailReason -> TcRnMessage
TcRnRunSpliceFailure (String -> Maybe String
forall a. a -> Maybe a
Just String
"addTopDecls") RunSpliceFailReason
exn
              Right [LHsDecl GhcPs]
ds -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> IOEnv
     (Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [LHsDecl GhcPs]
[GenLocated SrcSpanAnnA (HsDecl GhcPs)]
ds
      (GenLocated SrcSpanAnnA (HsDecl GhcPs) -> TcRn ())
-> [GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (HsDecl GhcPs -> TcRn ()
checkTopDecl (HsDecl GhcPs -> TcRn ())
-> (GenLocated SrcSpanAnnA (HsDecl GhcPs) -> HsDecl GhcPs)
-> GenLocated SrcSpanAnnA (HsDecl GhcPs)
-> TcRn ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (HsDecl GhcPs) -> HsDecl GhcPs
forall l e. GenLocated l e -> e
unLoc) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
ds
      TcRef [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
th_topdecls_var <- (TcGblEnv -> TcRef [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (TcRef [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef [LHsDecl GhcPs]
TcGblEnv -> TcRef [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
tcg_th_topdecls TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
      TcRef [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> ([GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
th_topdecls_var (\[GenLocated SrcSpanAnnA (HsDecl GhcPs)]
topds -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
ds [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a. [a] -> [a] -> [a]
++ [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
topds)
    where
      checkTopDecl :: HsDecl GhcPs -> TcM ()
      checkTopDecl :: HsDecl GhcPs -> TcRn ()
checkTopDecl (ValD XValD GhcPs
_ HsBind GhcPs
binds)
        = (RdrName -> TcRn ()) -> [RdrName] -> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ RdrName -> TcRn ()
bindName (CollectFlag GhcPs -> HsBind GhcPs -> [IdP GhcPs]
forall p idR.
CollectPass p =>
CollectFlag p -> HsBindLR p idR -> [IdP p]
collectHsBindBinders CollectFlag GhcPs
forall p. CollectFlag p
CollNoDictBinders HsBind GhcPs
binds)
      checkTopDecl (SigD XSigD GhcPs
_ Sig GhcPs
_)
        = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      checkTopDecl (AnnD XAnnD GhcPs
_ AnnDecl GhcPs
_)
        = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      checkTopDecl (ForD XForD GhcPs
_ (ForeignImport { fd_name :: forall pass. ForeignDecl pass -> LIdP pass
fd_name = L SrcSpanAnnN
_ RdrName
name }))
        = RdrName -> TcRn ()
bindName RdrName
name
      checkTopDecl HsDecl GhcPs
d
        = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ HsDecl GhcPs -> TcRnMessage
TcRnInvalidTopDecl HsDecl GhcPs
d

      bindName :: RdrName -> TcM ()
      bindName :: RdrName -> TcRn ()
bindName (Exact Name
n)
        = do { TcRef FreeVars
th_topnames_var <- (TcGblEnv -> TcRef FreeVars)
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef FreeVars)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef FreeVars
tcg_th_topnames TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
             ; TcRef FreeVars -> (FreeVars -> FreeVars) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef FreeVars
th_topnames_var (\FreeVars
ns -> FreeVars -> Name -> FreeVars
extendNameSet FreeVars
ns Name
n)
             }

      bindName RdrName
name = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ RdrName -> TcRnMessage
TcRnNonExactName RdrName
name

  qAddForeignFilePath :: ForeignSrcLang -> String -> TcRn ()
qAddForeignFilePath ForeignSrcLang
lang String
fp = do
    TcRef [(ForeignSrcLang, String)]
var <- (TcGblEnv -> TcRef [(ForeignSrcLang, String)])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [(ForeignSrcLang, String)])
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef [(ForeignSrcLang, String)]
tcg_th_foreign_files TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
    TcRef [(ForeignSrcLang, String)]
-> ([(ForeignSrcLang, String)] -> [(ForeignSrcLang, String)])
-> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [(ForeignSrcLang, String)]
var ((ForeignSrcLang
lang, String
fp) (ForeignSrcLang, String)
-> [(ForeignSrcLang, String)] -> [(ForeignSrcLang, String)]
forall a. a -> [a] -> [a]
:)

  qAddModFinalizer :: Q () -> TcRn ()
qAddModFinalizer Q ()
fin = do
      RemoteRef (Q ())
r <- IO (RemoteRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ()))
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RemoteRef (Q ()))
 -> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ())))
-> IO (RemoteRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ()))
forall a b. (a -> b) -> a -> b
$ Q () -> IO (RemoteRef (Q ()))
forall a. a -> IO (RemoteRef a)
mkRemoteRef Q ()
fin
      ForeignRef (Q ())
fref <- IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ForeignRef (Q ()))
 -> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ())))
-> IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
forall a b. (a -> b) -> a -> b
$ RemoteRef (Q ()) -> IO () -> IO (ForeignRef (Q ()))
forall a. RemoteRef a -> IO () -> IO (ForeignRef a)
mkForeignRef RemoteRef (Q ())
r (RemoteRef (Q ()) -> IO ()
forall a. RemoteRef a -> IO ()
freeRemoteRef RemoteRef (Q ())
r)
      ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef ForeignRef (Q ())
fref

  qAddCorePlugin :: String -> TcRn ()
qAddCorePlugin String
plugin = do
      HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
      let fc :: FinderCache
fc        = HscEnv -> FinderCache
hsc_FC HscEnv
hsc_env
      let home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
      let dflags :: DynFlags
dflags    = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
      let fopts :: FinderOpts
fopts     = DynFlags -> FinderOpts
initFinderOpts DynFlags
dflags
      FindResult
r <- IO FindResult -> IOEnv (Env TcGblEnv TcLclEnv) FindResult
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FindResult -> IOEnv (Env TcGblEnv TcLclEnv) FindResult)
-> IO FindResult -> IOEnv (Env TcGblEnv TcLclEnv) FindResult
forall a b. (a -> b) -> a -> b
$ FinderCache
-> FinderOpts -> HomeUnit -> ModuleName -> IO FindResult
findHomeModule FinderCache
fc FinderOpts
fopts HomeUnit
home_unit (String -> ModuleName
mkModuleName String
plugin)
      let err :: TcRnMessage
err = String -> TcRnMessage
TcRnAddInvalidCorePlugin String
plugin
      case FindResult
r of
        Found {} -> TcRnMessage -> TcRn ()
addErr TcRnMessage
err
        FoundMultiple {} -> TcRnMessage -> TcRn ()
addErr TcRnMessage
err
        FindResult
_ -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      TcRef [String]
th_coreplugins_var <- TcGblEnv -> TcRef [String]
tcg_th_coreplugins (TcGblEnv -> TcRef [String])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
      TcRef [String] -> ([String] -> [String]) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [String]
th_coreplugins_var (String
pluginString -> [String] -> [String]
forall a. a -> [a] -> [a]
:)

  qGetQ :: forall a. Typeable a => TcM (Maybe a)
  qGetQ :: forall a. Typeable a => TcM (Maybe a)
qGetQ = do
      TcRef (Map TypeRep Dynamic)
th_state_var <- (TcGblEnv -> TcRef (Map TypeRep Dynamic))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef (Map TypeRep Dynamic))
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef (Map TypeRep Dynamic)
tcg_th_state TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
      Map TypeRep Dynamic
th_state <- TcRef (Map TypeRep Dynamic)
-> TcRnIf TcGblEnv TcLclEnv (Map TypeRep Dynamic)
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef (Map TypeRep Dynamic)
th_state_var
      -- See #10596 for why we use a scoped type variable here.
      Maybe a -> TcM (Maybe a)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeRep -> Map TypeRep Dynamic -> Maybe Dynamic
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a)) Map TypeRep Dynamic
th_state Maybe Dynamic -> (Dynamic -> Maybe a) -> Maybe a
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Dynamic -> Maybe a
forall a. Typeable a => Dynamic -> Maybe a
fromDynamic)

  qPutQ :: forall a. Typeable a => a -> TcRn ()
qPutQ a
x = do
      TcRef (Map TypeRep Dynamic)
th_state_var <- (TcGblEnv -> TcRef (Map TypeRep Dynamic))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef (Map TypeRep Dynamic))
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef (Map TypeRep Dynamic)
tcg_th_state TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
      TcRef (Map TypeRep Dynamic)
-> (Map TypeRep Dynamic -> Map TypeRep Dynamic) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef (Map TypeRep Dynamic)
th_state_var (\Map TypeRep Dynamic
m -> TypeRep -> Dynamic -> Map TypeRep Dynamic -> Map TypeRep Dynamic
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (a -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf a
x) (a -> Dynamic
forall a. Typeable a => a -> Dynamic
toDyn a
x) Map TypeRep Dynamic
m)

  qIsExtEnabled :: Extension -> TcM Bool
qIsExtEnabled = Extension -> TcM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM

  qExtsEnabled :: TcM [Extension]
qExtsEnabled =
    EnumSet Extension -> [Extension]
forall a. Enum a => EnumSet a -> [a]
EnumSet.toList (EnumSet Extension -> [Extension])
-> (HscEnv -> EnumSet Extension) -> HscEnv -> [Extension]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> EnumSet Extension
extensionFlags (DynFlags -> EnumSet Extension)
-> (HscEnv -> DynFlags) -> HscEnv -> EnumSet Extension
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HscEnv -> DynFlags
hsc_dflags (HscEnv -> [Extension])
-> TcRnIf TcGblEnv TcLclEnv HscEnv -> TcM [Extension]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv

  qPutDoc :: DocLoc -> String -> TcRn ()
qPutDoc DocLoc
doc_loc String
s = do
    TcRef THDocs
th_doc_var <- TcGblEnv -> TcRef THDocs
tcg_th_docs (TcGblEnv -> TcRef THDocs)
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef THDocs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
    DocLoc
resolved_doc_loc <- DocLoc -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
resolve_loc DocLoc
doc_loc
    Bool
is_local <- DocLoc -> TcM Bool
forall {f :: * -> *}.
(Applicative f, HasModule f) =>
DocLoc -> f Bool
checkLocalName DocLoc
resolved_doc_loc
    Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
is_local (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ DocLoc -> TcRnMessage
TcRnAddDocToNonLocalDefn DocLoc
doc_loc
    let ds :: HsDocString
ds = String -> HsDocString
mkGeneratedHsDocString String
s
        hd :: HsDoc GhcPs
hd = P (GenLocated SrcSpanAnnN RdrName) -> HsDocString -> HsDoc GhcPs
lexHsDoc P (GenLocated SrcSpanAnnN RdrName)
parseIdentifier HsDocString
ds
    WithHsDocIdentifiers HsDocString GhcRn
hd' <- HsDoc GhcPs -> RnM (WithHsDocIdentifiers HsDocString GhcRn)
forall a.
WithHsDocIdentifiers a GhcPs -> RnM (WithHsDocIdentifiers a GhcRn)
rnHsDoc HsDoc GhcPs
hd
    TcRef THDocs -> (THDocs -> THDocs) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef THDocs
th_doc_var (DocLoc
-> WithHsDocIdentifiers HsDocString GhcRn -> THDocs -> THDocs
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert DocLoc
resolved_doc_loc WithHsDocIdentifiers HsDocString GhcRn
hd')
    where
      resolve_loc :: DocLoc -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
resolve_loc (TH.DeclDoc Name
n) = Name -> DocLoc
DeclDoc (Name -> DocLoc)
-> TcM Name -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcM Name
lookupThName Name
n
      resolve_loc (TH.ArgDoc Name
n SumArity
i) = Name -> SumArity -> DocLoc
ArgDoc (Name -> SumArity -> DocLoc)
-> TcM Name -> IOEnv (Env TcGblEnv TcLclEnv) (SumArity -> DocLoc)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcM Name
lookupThName Name
n IOEnv (Env TcGblEnv TcLclEnv) (SumArity -> DocLoc)
-> IOEnv (Env TcGblEnv TcLclEnv) SumArity
-> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) (a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SumArity -> IOEnv (Env TcGblEnv TcLclEnv) SumArity
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SumArity
i
      resolve_loc (TH.InstDoc Type
t) = Name -> DocLoc
InstDoc (Name -> DocLoc)
-> TcM Name -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Name -> Name) -> TcM Name -> TcM Name
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> Name
forall a. NamedThing a => a -> Name
getName (Type -> TcM Name
lookupThInstName Type
t)
      resolve_loc DocLoc
TH.ModuleDoc = DocLoc -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DocLoc
ModuleDoc

      -- It doesn't make sense to add documentation to something not inside
      -- the current module. So check for it!
      checkLocalName :: DocLoc -> f Bool
checkLocalName (DeclDoc Name
n) = GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> f (GenModule Unit) -> f (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule f (Name -> Bool) -> f Name -> f Bool
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> f Name
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
      checkLocalName (ArgDoc Name
n SumArity
_) = GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> f (GenModule Unit) -> f (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule f (Name -> Bool) -> f Name -> f Bool
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> f Name
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
      checkLocalName (InstDoc Name
n) = GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> f (GenModule Unit) -> f (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule f (Name -> Bool) -> f Name -> f Bool
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> f Name
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
      checkLocalName DocLoc
ModuleDoc = Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True


  qGetDoc :: DocLoc -> TcM (Maybe String)
qGetDoc (TH.DeclDoc Name
n) = Name -> TcM Name
lookupThName Name
n TcM Name -> (Name -> TcM (Maybe String)) -> TcM (Maybe String)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> TcM (Maybe String)
lookupDeclDoc
  qGetDoc (TH.InstDoc Type
t) = Type -> TcM Name
lookupThInstName Type
t TcM Name -> (Name -> TcM (Maybe String)) -> TcM (Maybe String)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> TcM (Maybe String)
lookupDeclDoc
  qGetDoc (TH.ArgDoc Name
n SumArity
i) = Name -> TcM Name
lookupThName Name
n TcM Name -> (Name -> TcM (Maybe String)) -> TcM (Maybe String)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SumArity -> Name -> TcM (Maybe String)
lookupArgDoc SumArity
i
  qGetDoc DocLoc
TH.ModuleDoc = do
    DynFlags
df <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
    Maybe Docs
docs <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> (TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DynFlags -> TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
forall (m :: * -> *).
MonadIO m =>
DynFlags -> TcGblEnv -> m (Maybe Docs)
extractDocs DynFlags
df
    Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsDocString -> String
renderHsDocString (HsDocString -> String)
-> (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> WithHsDocIdentifiers HsDocString GhcRn
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString (WithHsDocIdentifiers HsDocString GhcRn -> String)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Docs -> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
docs_mod_hdr (Docs -> Maybe (WithHsDocIdentifiers HsDocString GhcRn))
-> Maybe Docs -> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe Docs
docs))

-- | Looks up documentation for a declaration in first the current module,
-- otherwise tries to find it in another module via 'hscGetModuleInterface'.
lookupDeclDoc :: Name -> TcM (Maybe String)
lookupDeclDoc :: Name -> TcM (Maybe String)
lookupDeclDoc Name
nm = do
  DynFlags
df <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
  Docs{UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
docs_decls :: UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
docs_decls :: Docs -> UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
docs_decls} <- (Maybe Docs -> Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) Docs
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Docs -> Maybe Docs -> Docs
forall a. a -> Maybe a -> a
fromMaybe Docs
emptyDocs) (IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
 -> IOEnv (Env TcGblEnv TcLclEnv) Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) Docs
forall a b. (a -> b) -> a -> b
$ TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> (TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DynFlags -> TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
forall (m :: * -> *).
MonadIO m =>
DynFlags -> TcGblEnv -> m (Maybe Docs)
extractDocs DynFlags
df
  case UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
-> Name -> Maybe [WithHsDocIdentifiers HsDocString GhcRn]
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
docs_decls Name
nm of
    Just [WithHsDocIdentifiers HsDocString GhcRn]
doc -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just ([HsDocString] -> String
renderHsDocStrings ([HsDocString] -> String) -> [HsDocString] -> String
forall a b. (a -> b) -> a -> b
$ (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> [WithHsDocIdentifiers HsDocString GhcRn] -> [HsDocString]
forall a b. (a -> b) -> [a] -> [b]
map WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString [WithHsDocIdentifiers HsDocString GhcRn]
doc)
    Maybe [WithHsDocIdentifiers HsDocString GhcRn]
Nothing -> do
      -- Wasn't in the current module. Try searching other external ones!
      Maybe ModIface
mIface <- Name -> TcM (Maybe ModIface)
getExternalModIface Name
nm
      case Maybe ModIface
mIface of
        Just ModIface { mi_docs :: forall (phase :: ModIfacePhase). ModIface_ phase -> Maybe Docs
mi_docs = Just Docs{docs_decls :: Docs -> UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
docs_decls = UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
dmap} } ->
          Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ [HsDocString] -> String
renderHsDocStrings ([HsDocString] -> String)
-> ([WithHsDocIdentifiers HsDocString GhcRn] -> [HsDocString])
-> [WithHsDocIdentifiers HsDocString GhcRn]
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> [WithHsDocIdentifiers HsDocString GhcRn] -> [HsDocString]
forall a b. (a -> b) -> [a] -> [b]
map WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString ([WithHsDocIdentifiers HsDocString GhcRn] -> String)
-> Maybe [WithHsDocIdentifiers HsDocString GhcRn] -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
-> Name -> Maybe [WithHsDocIdentifiers HsDocString GhcRn]
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
dmap Name
nm
        Maybe ModIface
_ -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe String
forall a. Maybe a
Nothing

-- | Like 'lookupDeclDoc', looks up documentation for a function argument. If
-- it can't find any documentation for a function in this module, it tries to
-- find it in another module.
lookupArgDoc :: Int -> Name -> TcM (Maybe String)
lookupArgDoc :: SumArity -> Name -> TcM (Maybe String)
lookupArgDoc SumArity
i Name
nm = do
  DynFlags
df <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
  Docs{docs_args :: Docs
-> UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
docs_args = UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
argDocs} <- (Maybe Docs -> Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) Docs
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Docs -> Maybe Docs -> Docs
forall a. a -> Maybe a -> a
fromMaybe Docs
emptyDocs) (IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
 -> IOEnv (Env TcGblEnv TcLclEnv) Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
-> IOEnv (Env TcGblEnv TcLclEnv) Docs
forall a b. (a -> b) -> a -> b
$ TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> (TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DynFlags -> TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Docs)
forall (m :: * -> *).
MonadIO m =>
DynFlags -> TcGblEnv -> m (Maybe Docs)
extractDocs DynFlags
df
  case UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
-> Name -> Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
argDocs Name
nm of
    Just IntMap (WithHsDocIdentifiers HsDocString GhcRn)
m -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ HsDocString -> String
renderHsDocString (HsDocString -> String)
-> (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> WithHsDocIdentifiers HsDocString GhcRn
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString (WithHsDocIdentifiers HsDocString GhcRn -> String)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SumArity
-> IntMap (WithHsDocIdentifiers HsDocString GhcRn)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall a. SumArity -> IntMap a -> Maybe a
IntMap.lookup SumArity
i IntMap (WithHsDocIdentifiers HsDocString GhcRn)
m
    Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
Nothing -> do
      Maybe ModIface
mIface <- Name -> TcM (Maybe ModIface)
getExternalModIface Name
nm
      case Maybe ModIface
mIface of
        Just ModIface { mi_docs :: forall (phase :: ModIfacePhase). ModIface_ phase -> Maybe Docs
mi_docs = Just Docs{docs_args :: Docs
-> UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
docs_args = UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
amap} } ->
          Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ HsDocString -> String
renderHsDocString (HsDocString -> String)
-> (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> WithHsDocIdentifiers HsDocString GhcRn
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString (WithHsDocIdentifiers HsDocString GhcRn -> String)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
-> Name -> Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
amap Name
nm Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
-> (IntMap (WithHsDocIdentifiers HsDocString GhcRn)
    -> Maybe (WithHsDocIdentifiers HsDocString GhcRn))
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SumArity
-> IntMap (WithHsDocIdentifiers HsDocString GhcRn)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall a. SumArity -> IntMap a -> Maybe a
IntMap.lookup SumArity
i)
        Maybe ModIface
_ -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe String
forall a. Maybe a
Nothing

-- | Returns the module a Name belongs to, if it is isn't local.
getExternalModIface :: Name -> TcM (Maybe ModIface)
getExternalModIface :: Name -> TcM (Maybe ModIface)
getExternalModIface Name
nm = do
  Bool
isLocal <- GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
-> IOEnv (Env TcGblEnv TcLclEnv) (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule IOEnv (Env TcGblEnv TcLclEnv) (Name -> Bool)
-> TcM Name -> TcM Bool
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) (a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
nm
  if Bool
isLocal
    then Maybe ModIface -> TcM (Maybe ModIface)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ModIface
forall a. Maybe a
Nothing
    else case Name -> Maybe (GenModule Unit)
nameModule_maybe Name
nm of
          Maybe (GenModule Unit)
Nothing -> Maybe ModIface -> TcM (Maybe ModIface)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ModIface
forall a. Maybe a
Nothing
          Just GenModule Unit
modNm -> do
            HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
            ModIface
iface <- IO ModIface -> IOEnv (Env TcGblEnv TcLclEnv) ModIface
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ModIface -> IOEnv (Env TcGblEnv TcLclEnv) ModIface)
-> IO ModIface -> IOEnv (Env TcGblEnv TcLclEnv) ModIface
forall a b. (a -> b) -> a -> b
$ HscEnv -> GenModule Unit -> IO ModIface
hscGetModuleInterface HscEnv
hsc_env GenModule Unit
modNm
            Maybe ModIface -> TcM (Maybe ModIface)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ModIface -> Maybe ModIface
forall a. a -> Maybe a
Just ModIface
iface)

-- | Find the GHC name of the first instance that matches the TH type
lookupThInstName :: TH.Type -> TcM Name
lookupThInstName :: Type -> TcM Name
lookupThInstName Type
th_type = do
  Name
cls_name <- Type -> TcM Name
inst_cls_name Type
th_type
  Either (Class, [ClsInst]) (TyCon, [FamInst])
insts <- Name
-> [Type] -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
reifyInstances' Name
cls_name (Type -> [Type]
inst_arg_types Type
th_type)
  case Either (Class, [ClsInst]) (TyCon, [FamInst])
insts of   -- This expands any type synonyms
    Left  (Class
_, (ClsInst
inst:[ClsInst]
_)) -> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> TcM Name) -> Name -> TcM Name
forall a b. (a -> b) -> a -> b
$ ClsInst -> Name
forall a. NamedThing a => a -> Name
getName ClsInst
inst
    Left  (Class
_, [])       -> TcM Name
noMatches
    Right (TyCon
_, (FamInst
inst:[FamInst]
_)) -> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> TcM Name) -> Name -> TcM Name
forall a b. (a -> b) -> a -> b
$ FamInst -> Name
forall a. NamedThing a => a -> Name
getName FamInst
inst
    Right (TyCon
_, [])       -> TcM Name
noMatches
  where
    noMatches :: TcM Name
noMatches = TcRnMessage -> TcM Name
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcM Name) -> TcRnMessage -> TcM Name
forall a b. (a -> b) -> a -> b
$
      Type -> LookupTHInstNameErrReason -> TcRnMessage
TcRnFailedToLookupThInstName Type
th_type LookupTHInstNameErrReason
NoMatchesFound

    -- Get the name of the class for the instance we are documenting
    -- > inst_cls_name (Monad Maybe) == Monad
    -- > inst_cls_name C = C
    inst_cls_name :: TH.Type -> TcM TH.Name
    inst_cls_name :: Type -> TcM Name
inst_cls_name (TH.AppT Type
t Type
_)              = Type -> TcM Name
inst_cls_name Type
t
    inst_cls_name (TH.SigT Type
n Type
_)              = Type -> TcM Name
inst_cls_name Type
n
    inst_cls_name (TH.VarT Name
n)                = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.ConT Name
n)                = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.PromotedT Name
n)           = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.InfixT Type
_ Name
n Type
_)          = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.UInfixT Type
_ Name
n Type
_)         = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.PromotedInfixT Type
_ Name
n Type
_)  = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.PromotedUInfixT Type
_ Name
n Type
_) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
    inst_cls_name (TH.ParensT Type
t)             = Type -> TcM Name
inst_cls_name Type
t

    inst_cls_name (TH.ForallT [TyVarBndr Specificity]
_ [Type]
_ Type
_)         = TcM Name
inst_cls_name_err
    inst_cls_name (TH.ForallVisT [TyVarBndr ()]
_ Type
_)        = TcM Name
inst_cls_name_err
    inst_cls_name (TH.AppKindT Type
_ Type
_)          = TcM Name
inst_cls_name_err
    inst_cls_name (TH.TupleT SumArity
_)              = TcM Name
inst_cls_name_err
    inst_cls_name (TH.UnboxedTupleT SumArity
_)       = TcM Name
inst_cls_name_err
    inst_cls_name (TH.UnboxedSumT SumArity
_)         = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.ArrowT                  = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.MulArrowT               = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.EqualityT               = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.ListT                   = TcM Name
inst_cls_name_err
    inst_cls_name (TH.PromotedTupleT SumArity
_)      = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.PromotedNilT            = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.PromotedConsT           = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.StarT                   = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.ConstraintT             = TcM Name
inst_cls_name_err
    inst_cls_name (TH.LitT TyLit
_)                = TcM Name
inst_cls_name_err
    inst_cls_name Type
TH.WildCardT               = TcM Name
inst_cls_name_err
    inst_cls_name (TH.ImplicitParamT String
_ Type
_)    = TcM Name
inst_cls_name_err

    inst_cls_name_err :: TcM Name
inst_cls_name_err = TcRnMessage -> TcM Name
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcM Name) -> TcRnMessage -> TcM Name
forall a b. (a -> b) -> a -> b
$
      Type -> LookupTHInstNameErrReason -> TcRnMessage
TcRnFailedToLookupThInstName Type
th_type LookupTHInstNameErrReason
CouldNotDetermineInstance

    -- Basically does the opposite of 'mkThAppTs'
    -- > inst_arg_types (Monad Maybe) == [Maybe]
    -- > inst_arg_types C == []
    inst_arg_types :: TH.Type -> [TH.Type]
    inst_arg_types :: Type -> [Type]
inst_arg_types (TH.AppT Type
_ Type
args) =
      let go :: Type -> [Type]
go (TH.AppT Type
t Type
ts) = Type
tType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:Type -> [Type]
go Type
ts
          go Type
t = [Type
t]
        in Type -> [Type]
go Type
args
    inst_arg_types Type
_ = []

-- | Adds a mod finalizer reference to the local environment.
addModFinalizerRef :: ForeignRef (TH.Q ()) -> TcM ()
addModFinalizerRef :: ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef ForeignRef (Q ())
finRef = do
    ThStage
th_stage <- TcM ThStage
getStage
    case ThStage
th_stage of
      RunSplice TcRef [ForeignRef (Q ())]
th_modfinalizers_var -> TcRef [ForeignRef (Q ())]
-> ([ForeignRef (Q ())] -> [ForeignRef (Q ())]) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [ForeignRef (Q ())]
th_modfinalizers_var (ForeignRef (Q ())
finRef ForeignRef (Q ()) -> [ForeignRef (Q ())] -> [ForeignRef (Q ())]
forall a. a -> [a] -> [a]
:)
      -- This case happens only if a splice is executed and the caller does
      -- not set the 'ThStage' to 'RunSplice' to collect finalizers.
      -- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
      ThStage
_ ->
        String -> SDoc -> TcRn ()
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"addModFinalizer was called when no finalizers were collected"
                 (ThStage -> SDoc
forall a. Outputable a => a -> SDoc
ppr ThStage
th_stage)

-- | Releases the external interpreter state.
finishTH :: TcM ()
finishTH :: TcRn ()
finishTH = do
  HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
  case Interp -> InterpInstance
interpInstance (Interp -> InterpInstance) -> Maybe Interp -> Maybe InterpInstance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HscEnv -> Maybe Interp
hsc_interp HscEnv
hsc_env of
    Maybe InterpInstance
Nothing                  -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
#if defined(HAVE_INTERNAL_INTERPRETER)
    Just InterpInstance
InternalInterp      -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
#endif
    Just (ExternalInterp {}) -> do
      TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
      TcRef (Maybe (ForeignRef (IORef QState)))
-> Maybe (ForeignRef (IORef QState)) -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg) Maybe (ForeignRef (IORef QState))
forall a. Maybe a
Nothing


runTHExp :: ForeignHValue -> TcM TH.Exp
runTHExp :: ForeignHValue -> TcM Exp
runTHExp = THResultType -> ForeignHValue -> TcM Exp
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THExp

runTHPat :: ForeignHValue -> TcM TH.Pat
runTHPat :: ForeignHValue -> TcM Pat
runTHPat = THResultType -> ForeignHValue -> TcM Pat
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THPat

runTHType :: ForeignHValue -> TcM TH.Type
runTHType :: ForeignHValue -> TcM Type
runTHType = THResultType -> ForeignHValue -> TcM Type
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THType

runTHDec :: ForeignHValue -> TcM [TH.Dec]
runTHDec :: ForeignHValue -> TcM [Dec]
runTHDec = THResultType -> ForeignHValue -> TcM [Dec]
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THDec

runTH :: Binary a => THResultType -> ForeignHValue -> TcM a
runTH :: forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
ty ForeignHValue
fhv = do
  Interp
interp <- TcM Interp
tcGetInterp
  case Interp -> InterpInstance
interpInstance Interp
interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
    InterpInstance
InternalInterp -> do
       -- Run it in the local TcM
      HValue
hv <- IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue)
-> IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a b. (a -> b) -> a -> b
$ Interp -> ForeignHValue -> IO HValue
forall a. Interp -> ForeignRef a -> IO a
wormhole Interp
interp ForeignHValue
fhv
      a
r <- Q a -> TcM a
forall a. Q a -> TcM a
runQuasi (HValue -> Q a
forall a b. a -> b
unsafeCoerce HValue
hv :: TH.Q a)
      a -> TcM a
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
#endif

    ExternalInterp IServConfig
conf IServ
iserv ->
      -- Run it on the server.  For an overview of how TH works with
      -- Remote GHCi, see Note [Remote Template Haskell] in
      -- libraries/ghci/GHCi/TH.hs.
      IServConfig -> IServ -> (IServInstance -> TcM a) -> TcM a
forall (m :: * -> *) a.
(MonadIO m, ExceptionMonad m) =>
IServConfig -> IServ -> (IServInstance -> m a) -> m a
withIServ_ IServConfig
conf IServ
iserv ((IServInstance -> TcM a) -> TcM a)
-> (IServInstance -> TcM a) -> TcM a
forall a b. (a -> b) -> a -> b
$ \IServInstance
i -> do
        ForeignRef (IORef QState)
rstate <- IServInstance -> TcM (ForeignRef (IORef QState))
getTHState IServInstance
i
        Loc
loc <- TcM Loc
forall (m :: * -> *). Quasi m => m Loc
TH.qLocation
        IO () -> TcRn ()
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
          ForeignRef (IORef QState)
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef (IORef QState)
rstate ((RemoteRef (IORef QState) -> IO ()) -> IO ())
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRef (IORef QState)
state_hv ->
          ForeignHValue -> (RemoteRef HValue -> IO ()) -> IO ()
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignHValue
fhv ((RemoteRef HValue -> IO ()) -> IO ())
-> (RemoteRef HValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRef HValue
q_hv ->
            IServInstance -> Put -> IO ()
writeIServ IServInstance
i (Message (QResult ByteString) -> Put
forall a. Message a -> Put
putMessage (RemoteRef (IORef QState)
-> RemoteRef HValue
-> THResultType
-> Maybe Loc
-> Message (QResult ByteString)
RunTH RemoteRef (IORef QState)
state_hv RemoteRef HValue
q_hv THResultType
ty (Loc -> Maybe Loc
forall a. a -> Maybe a
Just Loc
loc)))
        IServInstance -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH IServInstance
i []
        ByteString
bs <- IServInstance -> TcM ByteString
forall a. Binary a => IServInstance -> TcM a
readQResult IServInstance
i
        a -> TcM a
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> TcM a) -> a -> TcM a
forall a b. (a -> b) -> a -> b
$! Get a -> ByteString -> a
forall a. Get a -> ByteString -> a
runGet Get a
forall t. Binary t => Get t
get (ByteString -> ByteString
LB.fromStrict ByteString
bs)


-- | communicate with a remotely-running TH computation until it finishes.
-- See Note [Remote Template Haskell] in libraries/ghci/GHCi/TH.hs.
runRemoteTH
  :: IServInstance
  -> [Messages TcRnMessage]   --  saved from nested calls to qRecover
  -> TcM ()
runRemoteTH :: IServInstance -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH IServInstance
iserv [Messages TcRnMessage]
recovers = do
  THMsg THMessage a
msg <- IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg)
-> IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg
forall a b. (a -> b) -> a -> b
$ IServInstance -> Get THMsg -> IO THMsg
forall a. IServInstance -> Get a -> IO a
readIServ IServInstance
iserv Get THMsg
getTHMessage
  case THMessage a
msg of
    THMessage a
RunTHDone -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    THMessage a
StartRecover -> do -- Note [TH recover with -fexternal-interpreter]
      TcRef (Messages TcRnMessage)
v <- TcRn (TcRef (Messages TcRnMessage))
getErrsVar
      Messages TcRnMessage
msgs <- TcRef (Messages TcRnMessage)
-> TcRnIf TcGblEnv TcLclEnv (Messages TcRnMessage)
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef (Messages TcRnMessage)
v
      TcRef (Messages TcRnMessage) -> Messages TcRnMessage -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef TcRef (Messages TcRnMessage)
v Messages TcRnMessage
forall e. Messages e
emptyMessages
      IServInstance -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH IServInstance
iserv (Messages TcRnMessage
msgs Messages TcRnMessage
-> [Messages TcRnMessage] -> [Messages TcRnMessage]
forall a. a -> [a] -> [a]
: [Messages TcRnMessage]
recovers)
    EndRecover Bool
caught_error -> do
      let (Messages TcRnMessage
prev_msgs, [Messages TcRnMessage]
rest) = case [Messages TcRnMessage]
recovers of
             [] -> String -> (Messages TcRnMessage, [Messages TcRnMessage])
forall a. HasCallStack => String -> a
panic String
"EndRecover"
             Messages TcRnMessage
a : [Messages TcRnMessage]
b -> (Messages TcRnMessage
a,[Messages TcRnMessage]
b)
      TcRef (Messages TcRnMessage)
v <- TcRn (TcRef (Messages TcRnMessage))
getErrsVar
      Bag (MsgEnvelope TcRnMessage)
warn_msgs <- Messages TcRnMessage -> Bag (MsgEnvelope TcRnMessage)
forall e. Diagnostic e => Messages e -> Bag (MsgEnvelope e)
getWarningMessages (Messages TcRnMessage -> Bag (MsgEnvelope TcRnMessage))
-> TcRnIf TcGblEnv TcLclEnv (Messages TcRnMessage)
-> IOEnv (Env TcGblEnv TcLclEnv) (Bag (MsgEnvelope TcRnMessage))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRef (Messages TcRnMessage)
-> TcRnIf TcGblEnv TcLclEnv (Messages TcRnMessage)
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef (Messages TcRnMessage)
v
      -- keep the warnings only if there were no errors
      TcRef (Messages TcRnMessage) -> Messages TcRnMessage -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef TcRef (Messages TcRnMessage)
v (Messages TcRnMessage -> TcRn ())
-> Messages TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ if Bool
caught_error
        then Messages TcRnMessage
prev_msgs
        else Bag (MsgEnvelope TcRnMessage) -> Messages TcRnMessage
forall e. Bag (MsgEnvelope e) -> Messages e
mkMessages Bag (MsgEnvelope TcRnMessage)
warn_msgs Messages TcRnMessage
-> Messages TcRnMessage -> Messages TcRnMessage
forall e. Messages e -> Messages e -> Messages e
`unionMessages` Messages TcRnMessage
prev_msgs
      IServInstance -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH IServInstance
iserv [Messages TcRnMessage]
rest
    THMessage a
_other -> do
      a
r <- THMessage a -> TcM a
forall a. THMessage a -> TcM a
handleTHMessage THMessage a
msg
      IO () -> TcRn ()
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ IServInstance -> Put -> IO ()
writeIServ IServInstance
iserv (a -> Put
forall t. Binary t => t -> Put
put a
r)
      IServInstance -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH IServInstance
iserv [Messages TcRnMessage]
recovers

-- | Read a value of type QResult from the iserv
readQResult :: Binary a => IServInstance -> TcM a
readQResult :: forall a. Binary a => IServInstance -> TcM a
readQResult IServInstance
i = do
  QResult a
qr <- IO (QResult a) -> IOEnv (Env TcGblEnv TcLclEnv) (QResult a)
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (QResult a) -> IOEnv (Env TcGblEnv TcLclEnv) (QResult a))
-> IO (QResult a) -> IOEnv (Env TcGblEnv TcLclEnv) (QResult a)
forall a b. (a -> b) -> a -> b
$ IServInstance -> Get (QResult a) -> IO (QResult a)
forall a. IServInstance -> Get a -> IO a
readIServ IServInstance
i Get (QResult a)
forall t. Binary t => Get t
get
  case QResult a
qr of
    QDone a
a -> a -> TcM a
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
    QException String
str -> IO a -> TcM a
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> TcM a) -> IO a -> TcM a
forall a b. (a -> b) -> a -> b
$ ErrorCall -> IO a
forall e a. Exception e => e -> IO a
throwIO (String -> ErrorCall
ErrorCall String
str)
    QFail String
str -> String -> TcM a
forall a. String -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
str

{- Note [TH recover with -fexternal-interpreter]
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Recover is slightly tricky to implement.

The meaning of "recover a b" is
 - Do a
   - If it finished with no errors, then keep the warnings it generated
   - If it failed, discard any messages it generated, and do b

Note that "failed" here can mean either
  (1) threw an exception (failTc)
  (2) generated an error message (addErrTcM)

The messages are managed by GHC in the TcM monad, whereas the
exception-handling is done in the ghc-iserv process, so we have to
coordinate between the two.

On the server:
  - emit a StartRecover message
  - run "a; FailIfErrs" inside a try
  - emit an (EndRecover x) message, where x = True if "a; FailIfErrs" failed
  - if "a; FailIfErrs" failed, run "b"

Back in GHC, when we receive:

  FailIfErrrs
    failTc if there are any error messages (= failIfErrsM)
  StartRecover
    save the current messages and start with an empty set.
  EndRecover caught_error
    Restore the previous messages,
    and merge in the new messages if caught_error is false.
-}

-- | Retrieve (or create, if it hasn't been created already), the
-- remote TH state.  The TH state is a remote reference to an IORef
-- QState living on the server, and we have to pass this to each RunTH
-- call we make.
--
-- The TH state is stored in tcg_th_remote_state in the TcGblEnv.
--
getTHState :: IServInstance -> TcM (ForeignRef (IORef QState))
getTHState :: IServInstance -> TcM (ForeignRef (IORef QState))
getTHState IServInstance
i = do
  TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
  Maybe (ForeignRef (IORef QState))
th_state <- TcRef (Maybe (ForeignRef (IORef QState)))
-> TcRnIf TcGblEnv TcLclEnv (Maybe (ForeignRef (IORef QState)))
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg)
  case Maybe (ForeignRef (IORef QState))
th_state of
    Just ForeignRef (IORef QState)
rhv -> ForeignRef (IORef QState) -> TcM (ForeignRef (IORef QState))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignRef (IORef QState)
rhv
    Maybe (ForeignRef (IORef QState))
Nothing -> do
      Interp
interp <- TcM Interp
tcGetInterp
      ForeignRef (IORef QState)
fhv <- IO (ForeignRef (IORef QState)) -> TcM (ForeignRef (IORef QState))
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ForeignRef (IORef QState)) -> TcM (ForeignRef (IORef QState)))
-> IO (ForeignRef (IORef QState))
-> TcM (ForeignRef (IORef QState))
forall a b. (a -> b) -> a -> b
$ Interp
-> RemoteRef (IORef QState) -> IO (ForeignRef (IORef QState))
forall a. Interp -> RemoteRef a -> IO (ForeignRef a)
mkFinalizedHValue Interp
interp (RemoteRef (IORef QState) -> IO (ForeignRef (IORef QState)))
-> IO (RemoteRef (IORef QState)) -> IO (ForeignRef (IORef QState))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IServInstance
-> Message (RemoteRef (IORef QState))
-> IO (RemoteRef (IORef QState))
forall a. Binary a => IServInstance -> Message a -> IO a
iservCall IServInstance
i Message (RemoteRef (IORef QState))
StartTH
      TcRef (Maybe (ForeignRef (IORef QState)))
-> Maybe (ForeignRef (IORef QState)) -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg) (ForeignRef (IORef QState) -> Maybe (ForeignRef (IORef QState))
forall a. a -> Maybe a
Just ForeignRef (IORef QState)
fhv)
      ForeignRef (IORef QState) -> TcM (ForeignRef (IORef QState))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignRef (IORef QState)
fhv

wrapTHResult :: TcM a -> TcM (THResult a)
wrapTHResult :: forall a. TcM a -> TcM (THResult a)
wrapTHResult TcM a
tcm = do
  Either IOEnvFailure a
e <- TcM a -> IOEnv (Env TcGblEnv TcLclEnv) (Either IOEnvFailure a)
forall env r. IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM TcM a
tcm   -- only catch 'fail', treat everything else as catastrophic
  case Either IOEnvFailure a
e of
    Left IOEnvFailure
e -> THResult a -> TcM (THResult a)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> THResult a
forall a. String -> THResult a
THException (IOEnvFailure -> String
forall a. Show a => a -> String
show IOEnvFailure
e))
    Right a
a -> THResult a -> TcM (THResult a)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> THResult a
forall a. a -> THResult a
THComplete a
a)

handleTHMessage :: THMessage a -> TcM a
handleTHMessage :: forall a. THMessage a -> TcM a
handleTHMessage THMessage a
msg = case THMessage a
msg of
  NewName String
a -> TcM Name -> TcM (THResult Name)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Name -> TcM (THResult Name))
-> TcM Name -> TcM (THResult Name)
forall a b. (a -> b) -> a -> b
$ String -> TcM Name
forall (m :: * -> *). Quasi m => String -> m Name
TH.qNewName String
a
  Report Bool
b String
str -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcRn ()
forall (m :: * -> *). Quasi m => Bool -> String -> m ()
TH.qReport Bool
b String
str
  LookupName Bool
b String
str -> TcM (Maybe Name) -> TcM (THResult (Maybe Name))
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe Name) -> TcM (THResult (Maybe Name)))
-> TcM (Maybe Name) -> TcM (THResult (Maybe Name))
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcM (Maybe Name)
forall (m :: * -> *). Quasi m => Bool -> String -> m (Maybe Name)
TH.qLookupName Bool
b String
str
  Reify Name
n -> TcM Info -> TcM (THResult Info)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Info -> TcM (THResult Info))
-> TcM Info -> TcM (THResult Info)
forall a b. (a -> b) -> a -> b
$ Name -> TcM Info
forall (m :: * -> *). Quasi m => Name -> m Info
TH.qReify Name
n
  ReifyFixity Name
n -> TcM (Maybe Fixity) -> TcM (THResult (Maybe Fixity))
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe Fixity) -> TcM (THResult (Maybe Fixity)))
-> TcM (Maybe Fixity) -> TcM (THResult (Maybe Fixity))
forall a b. (a -> b) -> a -> b
$ Name -> TcM (Maybe Fixity)
forall (m :: * -> *). Quasi m => Name -> m (Maybe Fixity)
TH.qReifyFixity Name
n
  ReifyType Name
n -> TcM Type -> TcM (THResult Type)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Type -> TcM (THResult Type))
-> TcM Type -> TcM (THResult Type)
forall a b. (a -> b) -> a -> b
$ Name -> TcM Type
forall (m :: * -> *). Quasi m => Name -> m Type
TH.qReifyType Name
n
  ReifyInstances Name
n [Type]
ts -> TcM [Dec] -> TcM (THResult [Dec])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Dec] -> TcM (THResult [Dec]))
-> TcM [Dec] -> TcM (THResult [Dec])
forall a b. (a -> b) -> a -> b
$ Name -> [Type] -> TcM [Dec]
forall (m :: * -> *). Quasi m => Name -> [Type] -> m [Dec]
TH.qReifyInstances Name
n [Type]
ts
  ReifyRoles Name
n -> TcM [Role] -> TcM (THResult [Role])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Role] -> TcM (THResult [Role]))
-> TcM [Role] -> TcM (THResult [Role])
forall a b. (a -> b) -> a -> b
$ Name -> TcM [Role]
forall (m :: * -> *). Quasi m => Name -> m [Role]
TH.qReifyRoles Name
n
  ReifyAnnotations AnnLookup
lookup TypeRep
tyrep ->
    TcM [ByteString] -> TcM (THResult [ByteString])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [ByteString] -> TcM (THResult [ByteString]))
-> TcM [ByteString] -> TcM (THResult [ByteString])
forall a b. (a -> b) -> a -> b
$ (([Word8] -> ByteString) -> [[Word8]] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map [Word8] -> ByteString
B.pack ([[Word8]] -> [ByteString])
-> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]] -> TcM [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnnLookup -> TypeRep -> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]]
getAnnotationsByTypeRep AnnLookup
lookup TypeRep
tyrep)
  ReifyModule Module
m -> TcM ModuleInfo -> TcM (THResult ModuleInfo)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM ModuleInfo -> TcM (THResult ModuleInfo))
-> TcM ModuleInfo -> TcM (THResult ModuleInfo)
forall a b. (a -> b) -> a -> b
$ Module -> TcM ModuleInfo
forall (m :: * -> *). Quasi m => Module -> m ModuleInfo
TH.qReifyModule Module
m
  ReifyConStrictness Name
nm -> TcM [DecidedStrictness] -> TcM (THResult [DecidedStrictness])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [DecidedStrictness] -> TcM (THResult [DecidedStrictness]))
-> TcM [DecidedStrictness] -> TcM (THResult [DecidedStrictness])
forall a b. (a -> b) -> a -> b
$ Name -> TcM [DecidedStrictness]
forall (m :: * -> *). Quasi m => Name -> m [DecidedStrictness]
TH.qReifyConStrictness Name
nm
  THMessage a
GetPackageRoot -> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String))
-> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a b. (a -> b) -> a -> b
$ IOEnv (Env TcGblEnv TcLclEnv) String
forall (m :: * -> *). Quasi m => m String
TH.qGetPackageRoot
  AddDependentFile String
f -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ String -> TcRn ()
forall (m :: * -> *). Quasi m => String -> m ()
TH.qAddDependentFile String
f
  AddTempFile String
s -> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String))
-> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a b. (a -> b) -> a -> b
$ String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall (m :: * -> *). Quasi m => String -> m String
TH.qAddTempFile String
s
  AddModFinalizer RemoteRef (Q ())
r -> do
    Interp
interp <- HscEnv -> Interp
hscInterp (HscEnv -> Interp) -> TcRnIf TcGblEnv TcLclEnv HscEnv -> TcM Interp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
    TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Interp -> RemoteRef (Q ()) -> IO (ForeignRef (Q ()))
forall a. Interp -> RemoteRef a -> IO (ForeignRef a)
mkFinalizedHValue Interp
interp RemoteRef (Q ())
r) IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
-> (ForeignRef (Q ()) -> TcRn ()) -> TcRn ()
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef
  AddCorePlugin String
str -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ String -> TcRn ()
forall (m :: * -> *). Quasi m => String -> m ()
TH.qAddCorePlugin String
str
  AddTopDecls [Dec]
decs -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ [Dec] -> TcRn ()
forall (m :: * -> *). Quasi m => [Dec] -> m ()
TH.qAddTopDecls [Dec]
decs
  AddForeignFilePath ForeignSrcLang
lang String
str -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ ForeignSrcLang -> String -> TcRn ()
forall (m :: * -> *). Quasi m => ForeignSrcLang -> String -> m ()
TH.qAddForeignFilePath ForeignSrcLang
lang String
str
  IsExtEnabled Extension
ext -> TcM Bool -> TcM (THResult Bool)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Bool -> TcM (THResult Bool))
-> TcM Bool -> TcM (THResult Bool)
forall a b. (a -> b) -> a -> b
$ Extension -> TcM Bool
forall (m :: * -> *). Quasi m => Extension -> m Bool
TH.qIsExtEnabled Extension
ext
  THMessage a
ExtsEnabled -> TcM [Extension] -> TcM (THResult [Extension])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Extension] -> TcM (THResult [Extension]))
-> TcM [Extension] -> TcM (THResult [Extension])
forall a b. (a -> b) -> a -> b
$ TcM [Extension]
forall (m :: * -> *). Quasi m => m [Extension]
TH.qExtsEnabled
  PutDoc DocLoc
l String
s -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ DocLoc -> String -> TcRn ()
forall (m :: * -> *). Quasi m => DocLoc -> String -> m ()
TH.qPutDoc DocLoc
l String
s
  GetDoc DocLoc
l -> TcM (Maybe String) -> TcM (THResult (Maybe String))
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe String) -> TcM (THResult (Maybe String)))
-> TcM (Maybe String) -> TcM (THResult (Maybe String))
forall a b. (a -> b) -> a -> b
$ DocLoc -> TcM (Maybe String)
forall (m :: * -> *). Quasi m => DocLoc -> m (Maybe String)
TH.qGetDoc DocLoc
l
  THMessage a
FailIfErrs -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult TcRn ()
failIfErrsM
  THMessage a
_ -> String -> TcM a
forall a. HasCallStack => String -> a
panic (String
"handleTHMessage: unexpected message " String -> String -> String
forall a. [a] -> [a] -> [a]
++ THMessage a -> String
forall a. Show a => a -> String
show THMessage a
msg)

getAnnotationsByTypeRep :: TH.AnnLookup -> TypeRep -> TcM [[Word8]]
getAnnotationsByTypeRep :: AnnLookup -> TypeRep -> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]]
getAnnotationsByTypeRep AnnLookup
th_name TypeRep
tyrep
  = do { CoreAnnTarget
name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
       ; HscEnv
topEnv <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
       ; AnnEnv
epsHptAnns <- IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv)
-> IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv
forall a b. (a -> b) -> a -> b
$ HscEnv -> Maybe ModGuts -> IO AnnEnv
prepareAnnotations HscEnv
topEnv Maybe ModGuts
forall a. Maybe a
Nothing
       ; TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
       ; let selectedEpsHptAnns :: [[Word8]]
selectedEpsHptAnns = AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
findAnnsByTypeRep AnnEnv
epsHptAnns CoreAnnTarget
name TypeRep
tyrep
       ; let selectedTcgAnns :: [[Word8]]
selectedTcgAnns = AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
findAnnsByTypeRep (TcGblEnv -> AnnEnv
tcg_ann_env TcGblEnv
tcg) CoreAnnTarget
name TypeRep
tyrep
       ; [[Word8]] -> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([[Word8]]
selectedEpsHptAnns [[Word8]] -> [[Word8]] -> [[Word8]]
forall a. [a] -> [a] -> [a]
++ [[Word8]]
selectedTcgAnns) }

{-
************************************************************************
*                                                                      *
            Instance Testing
*                                                                      *
************************************************************************
-}

reifyInstances :: TH.Name -> [TH.Type] -> TcM [TH.Dec]
reifyInstances :: Name -> [Type] -> TcM [Dec]
reifyInstances Name
th_nm [Type]
th_tys
  = do { Either (Class, [ClsInst]) (TyCon, [FamInst])
insts <- Name
-> [Type] -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
reifyInstances' Name
th_nm [Type]
th_tys
       ; case Either (Class, [ClsInst]) (TyCon, [FamInst])
insts of
           Left (Class
cls, [ClsInst]
cls_insts) ->
             Class -> [ClsInst] -> TcM [Dec]
reifyClassInstances Class
cls [ClsInst]
cls_insts
           Right (TyCon
tc, [FamInst]
fam_insts) ->
             TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc [FamInst]
fam_insts }

reifyInstances' :: TH.Name
                -> [TH.Type]
                -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
                -- ^ Returns 'Left' in the case that the instances were found to
                -- be class instances, or 'Right' if they are family instances.
reifyInstances' :: Name
-> [Type] -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
reifyInstances' Name
th_nm [Type]
th_tys
   = SDoc
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the argument of reifyInstances:"
                 SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Name -> SDoc
forall a. Ppr a => a -> SDoc
ppr_th Name
th_nm SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep ((Type -> SDoc) -> [Type] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Type -> SDoc
forall a. Ppr a => a -> SDoc
ppr_th [Type]
th_tys)) (TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
 -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst])))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a b. (a -> b) -> a -> b
$
     do { SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
        ; Origin
th_origin <- TcM Origin
getThSpliceOrigin
        ; GenLocated SrcSpanAnnA (HsType GhcPs)
rdr_ty <- Origin -> SrcSpan -> Type -> TcM (LHsType GhcPs)
cvt Origin
th_origin SrcSpan
loc (Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT Name
th_nm) [Type]
th_tys)
          -- #9262 says to bring vars into scope, like in HsForAllTy case
          -- of rnHsTyKi
        ; let tv_rdrs :: FreeKiTyVars
tv_rdrs = LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
rdr_ty
          -- Rename  to HsType Name
        ; (([Name]
tv_names, GenLocated SrcSpanAnnA (HsType GhcRn)
rn_ty), FreeVars
_fvs)
            <- TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
-> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
forall r. TcM r -> TcM r
checkNoErrs (TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
 -> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars))
-> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
-> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
forall a b. (a -> b) -> a -> b
$ -- If there are out-of-scope Names here, then we
                             -- must error before proceeding to typecheck the
                             -- renamed type, as that will result in GHC
                             -- internal errors (#13837).
               Maybe Any
-> FreeKiTyVars
-> ([Name]
    -> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars))
-> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
forall assoc a.
Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvOccs Maybe Any
forall a. Maybe a
Nothing FreeKiTyVars
tv_rdrs (([Name]
  -> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars))
 -> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars))
-> ([Name]
    -> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars))
-> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [Name]
tv_names ->
               do { (GenLocated SrcSpanAnnA (HsType GhcRn)
rn_ty, FreeVars
fvs) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
doc LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
rdr_ty
                  ; (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
-> TcM (([Name], GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (([Name]
tv_names, GenLocated SrcSpanAnnA (HsType GhcRn)
rn_ty), FreeVars
fvs) }
        ; SkolemInfo
skol_info <- SkolemInfoAnon -> IOEnv (Env TcGblEnv TcLclEnv) SkolemInfo
forall (m :: * -> *). MonadIO m => SkolemInfoAnon -> m SkolemInfo
mkSkolemInfo SkolemInfoAnon
ReifySkol
        ; (TcLevel
tclvl, WantedConstraints
wanted, ([Id]
tvs, Type
ty))
            <- String
-> TcM ([Id], Type)
-> TcM (TcLevel, WantedConstraints, ([Id], Type))
forall a. String -> TcM a -> TcM (TcLevel, WantedConstraints, a)
pushLevelAndSolveEqualitiesX String
"reifyInstances"  (TcM ([Id], Type)
 -> TcM (TcLevel, WantedConstraints, ([Id], Type)))
-> TcM ([Id], Type)
-> TcM (TcLevel, WantedConstraints, ([Id], Type))
forall a b. (a -> b) -> a -> b
$
               SkolemInfo
-> [Name] -> IOEnv (Env TcGblEnv TcLclEnv) Type -> TcM ([Id], Type)
forall a. SkolemInfo -> [Name] -> TcM a -> TcM ([Id], a)
bindImplicitTKBndrs_Skol SkolemInfo
skol_info [Name]
tv_names              (IOEnv (Env TcGblEnv TcLclEnv) Type -> TcM ([Id], Type))
-> IOEnv (Env TcGblEnv TcLclEnv) Type -> TcM ([Id], Type)
forall a b. (a -> b) -> a -> b
$
               LHsType GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcInferLHsType LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
rn_ty

        ; [Id]
tvs <- [Id] -> TcM [Id]
zonkAndScopedSort [Id]
tvs

        -- Avoid error cascade if there are unsolved
        ; SkolemInfo -> [Id] -> TcLevel -> WantedConstraints -> TcRn ()
reportUnsolvedEqualities SkolemInfo
skol_info [Id]
tvs TcLevel
tclvl WantedConstraints
wanted

        ; Type
ty <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcTypeToType Type
ty
                -- Substitute out the meta type variables
                -- In particular, the type might have kind
                -- variables inside it (#7477)

        ; String -> SDoc -> TcRn ()
traceTc String
"reifyInstances'" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((() :: Constraint) => Type -> Type
Type -> Type
typeKind Type
ty))
        ; case (() :: Constraint) => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of   -- This expands any type synonyms
            Just (TyCon
tc, [Type]
tys)                 -- See #7910
               | Just Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
               -> do { InstEnvs
inst_envs <- TcM InstEnvs
tcGetInstEnvs
                     ; let ([InstMatch]
matches, PotentialUnifiers
unifies, [InstMatch]
_) = Bool
-> InstEnvs
-> Class
-> [Type]
-> ([InstMatch], PotentialUnifiers, [InstMatch])
lookupInstEnv Bool
False InstEnvs
inst_envs Class
cls [Type]
tys
                     ; String -> SDoc -> TcRn ()
traceTc String
"reifyInstances'1" ([InstMatch] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [InstMatch]
matches)
                     ; Either (Class, [ClsInst]) (TyCon, [FamInst])
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either (Class, [ClsInst]) (TyCon, [FamInst])
 -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst])))
-> Either (Class, [ClsInst]) (TyCon, [FamInst])
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a b. (a -> b) -> a -> b
$ (Class, [ClsInst]) -> Either (Class, [ClsInst]) (TyCon, [FamInst])
forall a b. a -> Either a b
Left (Class
cls, (InstMatch -> ClsInst) -> [InstMatch] -> [ClsInst]
forall a b. (a -> b) -> [a] -> [b]
map InstMatch -> ClsInst
forall a b. (a, b) -> a
fst [InstMatch]
matches [ClsInst] -> [ClsInst] -> [ClsInst]
forall a. [a] -> [a] -> [a]
++ PotentialUnifiers -> [ClsInst]
getPotentialUnifiers PotentialUnifiers
unifies) }
               | TyCon -> Bool
isOpenFamilyTyCon TyCon
tc
               -> do { FamInstEnvs
inst_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
                     ; let matches :: [FamInstMatch]
matches = FamInstEnvs -> TyCon -> [Type] -> [FamInstMatch]
lookupFamInstEnv FamInstEnvs
inst_envs TyCon
tc [Type]
tys
                     ; String -> SDoc -> TcRn ()
traceTc String
"reifyInstances'2" ([FamInstMatch] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [FamInstMatch]
matches)
                     ; Either (Class, [ClsInst]) (TyCon, [FamInst])
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either (Class, [ClsInst]) (TyCon, [FamInst])
 -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst])))
-> Either (Class, [ClsInst]) (TyCon, [FamInst])
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a b. (a -> b) -> a -> b
$ (TyCon, [FamInst]) -> Either (Class, [ClsInst]) (TyCon, [FamInst])
forall a b. b -> Either a b
Right (TyCon
tc, (FamInstMatch -> FamInst) -> [FamInstMatch] -> [FamInst]
forall a b. (a -> b) -> [a] -> [b]
map FamInstMatch -> FamInst
fim_instance [FamInstMatch]
matches) }
            Maybe (TyCon, [Type])
_  -> TcRnMessage -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a. TcRnMessage -> TcM a
bale_out (TcRnMessage -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst])))
-> TcRnMessage
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a b. (a -> b) -> a -> b
$ Type -> TcRnMessage
TcRnCannotReifyInstance Type
ty }
  where
    doc :: HsDocContext
doc = HsDocContext
ClassInstanceCtx
    bale_out :: TcRnMessage -> TcM a
bale_out TcRnMessage
msg = TcRnMessage -> TcM a
forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
msg

    cvt :: Origin -> SrcSpan -> TH.Type -> TcM (LHsType GhcPs)
    cvt :: Origin -> SrcSpan -> Type -> TcM (LHsType GhcPs)
cvt Origin
origin SrcSpan
loc Type
th_ty = case Origin
-> SrcSpan -> Type -> Either RunSpliceFailReason (LHsType GhcPs)
convertToHsType Origin
origin SrcSpan
loc Type
th_ty of
      Left RunSpliceFailReason
msg -> TcRnMessage
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall a. TcRnMessage -> TcM a
failWithTc (Maybe String -> RunSpliceFailReason -> TcRnMessage
TcRnRunSpliceFailure Maybe String
forall a. Maybe a
Nothing RunSpliceFailReason
msg)
      Right LHsType GhcPs
ty -> GenLocated SrcSpanAnnA (HsType GhcPs)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
ty

{-
************************************************************************
*                                                                      *
                        Reification
*                                                                      *
************************************************************************
-}

lookupName :: Bool      -- True  <=> type namespace
                        -- False <=> value namespace
           -> String -> TcM (Maybe TH.Name)
lookupName :: Bool -> String -> TcM (Maybe Name)
lookupName Bool
is_type_name String
s
  = do { Maybe Name
mb_nm <- RdrName -> RnM (Maybe Name)
lookupOccRn_maybe RdrName
rdr_name
       ; Maybe Name -> TcM (Maybe Name)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Name -> Name) -> Maybe Name -> Maybe Name
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> Name
forall n. NamedThing n => n -> Name
reifyName Maybe Name
mb_nm) }
  where
    th_name :: Name
th_name = String -> Name
TH.mkName String
s       -- Parses M.x into a base of 'x' and a module of 'M'

    occ_fs :: FastString
    occ_fs :: FastString
occ_fs = String -> FastString
mkFastString (Name -> String
TH.nameBase Name
th_name)

    occ :: OccName
    occ :: OccName
occ | Bool
is_type_name
        = if FastString -> Bool
isLexVarSym FastString
occ_fs Bool -> Bool -> Bool
|| FastString -> Bool
isLexCon FastString
occ_fs
                             then FastString -> OccName
mkTcOccFS    FastString
occ_fs
                             else FastString -> OccName
mkTyVarOccFS FastString
occ_fs
        | Bool
otherwise
        = if FastString -> Bool
isLexCon FastString
occ_fs then FastString -> OccName
mkDataOccFS FastString
occ_fs
                             else FastString -> OccName
mkVarOccFS  FastString
occ_fs

    rdr_name :: RdrName
rdr_name = case Name -> Maybe String
TH.nameModule Name
th_name of
                 Maybe String
Nothing  -> OccName -> RdrName
mkRdrUnqual OccName
occ
                 Just String
mod -> ModuleName -> OccName -> RdrName
mkRdrQual (String -> ModuleName
mkModuleName String
mod) OccName
occ

-- | We only want to produce warnings for TH-splices if the user requests so.
-- See Note [Warnings for TH splices].
getThSpliceOrigin :: TcM Origin
getThSpliceOrigin :: TcM Origin
getThSpliceOrigin = do
  Bool
warn <- GeneralFlag -> TcM Bool
forall gbl lcl. GeneralFlag -> TcRnIf gbl lcl Bool
goptM GeneralFlag
Opt_EnableThSpliceWarnings
  if Bool
warn then Origin -> TcM Origin
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Origin
FromSource else Origin -> TcM Origin
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Origin
Generated


getThing :: TH.Name -> TcM TcTyThing
getThing :: Name -> TcM TcTyThing
getThing Name
th_name
  = do  { Name
name <- Name -> TcM Name
lookupThName Name
th_name
        ; SDoc -> TcRn ()
forall m n. SDoc -> TcRnIf m n ()
traceIf (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"reify" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> SDoc
forall doc. IsLine doc => String -> doc
text (Name -> String
forall a. Show a => a -> String
show Name
th_name) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
brackets (Name -> SDoc
forall {doc}. IsLine doc => Name -> doc
ppr_ns Name
th_name) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name)
        ; Name -> TcM TcTyThing
tcLookupTh Name
name }
        -- ToDo: this tcLookup could fail, which would give a
        --       rather unhelpful error message
  where
    ppr_ns :: Name -> doc
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.DataName  PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"data"
    ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.TcClsName PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"tc"
    ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.VarName   PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"var"
    ppr_ns Name
_ = String -> doc
forall a. HasCallStack => String -> a
panic String
"reify/ppr_ns"

reify :: TH.Name -> TcM TH.Info
reify :: Name -> TcM Info
reify Name
th_name
  = do  { String -> SDoc -> TcRn ()
traceTc String
"reify 1" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (Name -> String
TH.showName Name
th_name))
        ; TcTyThing
thing <- Name -> TcM TcTyThing
getThing Name
th_name
        ; String -> SDoc -> TcRn ()
traceTc String
"reify 2" (TcTyThing -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyThing
thing)
        ; TcTyThing -> TcM Info
reifyThing TcTyThing
thing }

lookupThName :: TH.Name -> TcM Name
lookupThName :: Name -> TcM Name
lookupThName Name
th_name = do
    Maybe Name
mb_name <- Name -> RnM (Maybe Name)
lookupThName_maybe Name
th_name
    case Maybe Name
mb_name of
        Maybe Name
Nothing   -> TcRnMessage -> TcM Name
forall a. TcRnMessage -> TcM a
failWithTc (Name -> TcRnMessage
notInScope Name
th_name)
        Just Name
name -> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Name
name

lookupThName_maybe :: TH.Name -> TcM (Maybe Name)
lookupThName_maybe :: Name -> RnM (Maybe Name)
lookupThName_maybe Name
th_name
  =  do { [Name]
names <- (RdrName -> RnM (Maybe Name))
-> [RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) [Name]
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM RdrName -> RnM (Maybe Name)
lookupOccRn_maybe (Name -> [RdrName]
thRdrNameGuesses Name
th_name)
          -- Pick the first that works
          -- E.g. reify (mkName "A") will pick the class A in preference to the data constructor A
        ; Maybe Name -> RnM (Maybe Name)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Name] -> Maybe Name
forall a. [a] -> Maybe a
listToMaybe [Name]
names) }

tcLookupTh :: Name -> TcM TcTyThing
-- This is a specialised version of GHC.Tc.Utils.Env.tcLookup; specialised mainly in that
-- it gives a reify-related error message on failure, whereas in the normal
-- tcLookup, failure is a bug.
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh Name
name
  = do  { (TcGblEnv
gbl_env, TcLclEnv
lcl_env) <- TcRnIf TcGblEnv TcLclEnv (TcGblEnv, TcLclEnv)
forall gbl lcl. TcRnIf gbl lcl (gbl, lcl)
getEnvs
        ; case NameEnv TcTyThing -> Name -> Maybe TcTyThing
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv (TcLclEnv -> NameEnv TcTyThing
tcl_env TcLclEnv
lcl_env) Name
name of {
                Just TcTyThing
thing -> TcTyThing -> TcM TcTyThing
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return TcTyThing
thing;
                Maybe TcTyThing
Nothing    ->

          case NameEnv TyThing -> Name -> Maybe TyThing
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv (TcGblEnv -> NameEnv TyThing
tcg_type_env TcGblEnv
gbl_env) Name
name of {
                Just TyThing
thing -> TcTyThing -> TcM TcTyThing
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing);
                Maybe TyThing
Nothing    ->

          -- EZY: I don't think this choice matters, no TH in signatures!
          if GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (TcGblEnv -> GenModule Unit
tcg_semantic_mod TcGblEnv
gbl_env) Name
name
          then  -- It's defined in this module
                TcRnMessage -> TcM TcTyThing
forall a. TcRnMessage -> TcM a
failWithTc (Name -> TcRnMessage
notInEnv Name
name)

          else
     do { MaybeErr SDoc TyThing
mb_thing <- Name -> TcM (MaybeErr SDoc TyThing)
tcLookupImported_maybe Name
name
        ; case MaybeErr SDoc TyThing
mb_thing of
            Succeeded TyThing
thing -> TcTyThing -> TcM TcTyThing
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing)
            Failed SDoc
msg      -> TcRnMessage -> TcM TcTyThing
forall a. TcRnMessage -> TcM a
failWithTc (Name -> SDoc -> TcRnMessage
TcRnInterfaceLookupError Name
name SDoc
msg)
    }}}}

notInScope :: TH.Name -> TcRnMessage
notInScope :: Name -> TcRnMessage
notInScope Name
th_name =
  Name -> TcRnMessage
TcRnCannotReifyOutOfScopeThing Name
th_name

notInEnv :: Name -> TcRnMessage
notInEnv :: Name -> TcRnMessage
notInEnv Name
name = Name -> TcRnMessage
TcRnCannotReifyThingNotInTypeEnv Name
name

------------------------------
reifyRoles :: TH.Name -> TcM [TH.Role]
reifyRoles :: Name -> TcM [Role]
reifyRoles Name
th_name
  = do { TcTyThing
thing <- Name -> TcM TcTyThing
getThing Name
th_name
       ; case TcTyThing
thing of
           AGlobal (ATyCon TyCon
tc) -> [Role] -> TcM [Role]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Role -> Role) -> [Role] -> [Role]
forall a b. (a -> b) -> [a] -> [b]
map Role -> Role
reify_role (TyCon -> [Role]
tyConRoles TyCon
tc))
           TcTyThing
_ -> TcRnMessage -> TcM [Role]
forall a. TcRnMessage -> TcM a
failWithTc (TcTyThing -> TcRnMessage
TcRnNoRolesAssociatedWithThing TcTyThing
thing)
       }
  where
    reify_role :: Role -> Role
reify_role Role
Nominal          = Role
TH.NominalR
    reify_role Role
Representational = Role
TH.RepresentationalR
    reify_role Role
Phantom          = Role
TH.PhantomR

------------------------------
reifyThing :: TcTyThing -> TcM TH.Info
-- The only reason this is monadic is for error reporting,
-- which in turn is mainly for the case when TH can't express
-- some random GHC extension

reifyThing :: TcTyThing -> TcM Info
reifyThing (AGlobal (AnId Id
id))
  = do  { Type
ty <- Type -> TcM Type
reifyType (Id -> Type
idType Id
id)
        ; let v :: Name
v = Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
id
        ; case Id -> IdDetails
idDetails Id
id of
            ClassOpId Class
cls -> Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Name -> Info
TH.ClassOpI Name
v Type
ty (Class -> Name
forall n. NamedThing n => n -> Name
reifyName Class
cls))
            RecSelId{sel_tycon :: IdDetails -> RecSelParent
sel_tycon=RecSelData TyCon
tc}
                          -> Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Maybe Dec -> Info
TH.VarI (Id -> TyCon -> Name
reifySelector Id
id TyCon
tc) Type
ty Maybe Dec
forall a. Maybe a
Nothing)
            IdDetails
_             -> Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Maybe Dec -> Info
TH.VarI     Name
v Type
ty Maybe Dec
forall a. Maybe a
Nothing)
    }

reifyThing (AGlobal (ATyCon TyCon
tc))   = TyCon -> TcM Info
reifyTyCon TyCon
tc
reifyThing (AGlobal (AConLike (RealDataCon DataCon
dc)))
  = DataCon -> TcM Info
mkDataConI DataCon
dc

reifyThing (AGlobal (AConLike (PatSynCon PatSyn
ps)))
  = do { let name :: Name
name = PatSyn -> Name
forall n. NamedThing n => n -> Name
reifyName PatSyn
ps
       ; Type
ty <- ([InvisTVBinder], [Type], [InvisTVBinder], [Type], [Scaled Type],
 Type)
-> TcM Type
reifyPatSynType (PatSyn
-> ([InvisTVBinder], [Type], [InvisTVBinder], [Type],
    [Scaled Type], Type)
patSynSigBndr PatSyn
ps)
       ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Info
TH.PatSynI Name
name Type
ty) }

reifyThing (ATcId {tct_id :: TcTyThing -> Id
tct_id = Id
id})
  = do  { Type
ty1 <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcType (Id -> Type
idType Id
id) -- Make use of all the info we have, even
                                        -- though it may be incomplete
        ; Type
ty2 <- Type -> TcM Type
reifyType Type
ty1
        ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Maybe Dec -> Info
TH.VarI (Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
id) Type
ty2 Maybe Dec
forall a. Maybe a
Nothing) }

reifyThing (ATyVar Name
tv Id
tv1)
  = do { Type
ty1 <- Id -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcTyVar Id
tv1
       ; Type
ty2 <- Type -> TcM Type
reifyType Type
ty1
       ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Info
TH.TyVarI (Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
tv) Type
ty2) }

reifyThing TcTyThing
thing = String -> SDoc -> TcM Info
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"reifyThing" (TcTyThing -> SDoc
pprTcTyThingCategory TcTyThing
thing)

-------------------------------------------
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TH.TySynEqn
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TySynEqn
reifyAxBranch TyCon
fam_tc (CoAxBranch { cab_tvs :: CoAxBranch -> [Id]
cab_tvs = [Id]
tvs
                                 , cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs
                                 , cab_rhs :: CoAxBranch -> Type
cab_rhs = Type
rhs })
            -- remove kind patterns (#8884)
  = do { Maybe [TyVarBndr ()]
tvs' <- [Id] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [Id]
tvs
       ; let lhs_types_only :: [Type]
lhs_types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
lhs
       ; [Type]
lhs' <- [Type] -> TcM [Type]
reifyTypes [Type]
lhs_types_only
       ; [Type]
annot_th_lhs <- (Bool -> Type -> Type -> TcM Type)
-> [Bool] -> [Type] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Type -> Type -> TcM Type
annotThType (TyCon -> [Bool]
tyConArgsPolyKinded TyCon
fam_tc)
                                   [Type]
lhs_types_only [Type]
lhs'
       ; let lhs_type :: Type
lhs_type = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT (Name -> Type) -> Name -> Type
forall a b. (a -> b) -> a -> b
$ TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
fam_tc) [Type]
annot_th_lhs
       ; Type
rhs'  <- Type -> TcM Type
reifyType Type
rhs
       ; TySynEqn -> TcM TySynEqn
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe [TyVarBndr ()] -> Type -> Type -> TySynEqn
TH.TySynEqn Maybe [TyVarBndr ()]
tvs' Type
lhs_type Type
rhs') }

reifyTyCon :: TyCon -> TcM TH.Info
reifyTyCon :: TyCon -> TcM Info
reifyTyCon TyCon
tc
  | Just Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
  = Class -> TcM Info
reifyClass Class
cls

{-  Seems to be just a short cut for the next equation -- omit
  | tc `hasKey` fUNTyConKey -- I'm not quite sure what is happening here
  = return (TH.PrimTyConI (reifyName tc) 2 False)
-}

  | TyCon -> Bool
isPrimTyCon TyCon
tc
  = Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> SumArity -> Bool -> Info
TH.PrimTyConI (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) ([Id] -> SumArity
forall a. [a] -> SumArity
forall (t :: * -> *) a. Foldable t => t a -> SumArity
length (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc))
                          (Type -> Bool
isUnliftedTypeKind (TyCon -> Type
tyConResKind TyCon
tc)))

  | TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
  = do { let tvs :: [Id]
tvs      = TyCon -> [Id]
tyConTyVars TyCon
tc
             res_kind :: Type
res_kind = TyCon -> Type
tyConResKind TyCon
tc
             resVar :: Maybe Name
resVar   = TyCon -> Maybe Name
tyConFamilyResVar_maybe TyCon
tc

       ; Type
kind' <- Type -> TcM Type
reifyKind Type
res_kind
       ; let (FamilyResultSig
resultSig, Maybe InjectivityAnn
injectivity) =
                 case Maybe Name
resVar of
                   Maybe Name
Nothing   -> (Type -> FamilyResultSig
TH.KindSig Type
kind', Maybe InjectivityAnn
forall a. Maybe a
Nothing)
                   Just Name
name ->
                     let thName :: Name
thName   = Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
name
                         injAnnot :: Injectivity
injAnnot = TyCon -> Injectivity
tyConInjectivityInfo TyCon
tc
                         sig :: FamilyResultSig
sig = TyVarBndr () -> FamilyResultSig
TH.TyVarSig (Name -> () -> Type -> TyVarBndr ()
forall flag. Name -> flag -> Type -> TyVarBndr flag
TH.KindedTV Name
thName () Type
kind')
                         inj :: Maybe InjectivityAnn
inj = case Injectivity
injAnnot of
                                 Injectivity
NotInjective -> Maybe InjectivityAnn
forall a. Maybe a
Nothing
                                 Injective [Bool]
ms ->
                                     InjectivityAnn -> Maybe InjectivityAnn
forall a. a -> Maybe a
Just (Name -> [Name] -> InjectivityAnn
TH.InjectivityAnn Name
thName [Name]
injRHS)
                                   where
                                     injRHS :: [Name]
injRHS = (Id -> Name) -> [Id] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Name
forall n. NamedThing n => n -> Name
reifyName (Name -> Name) -> (Id -> Name) -> Id -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Name
tyVarName)
                                                  ([Bool] -> [Id] -> [Id]
forall a. [Bool] -> [a] -> [a]
filterByList [Bool]
ms [Id]
tvs)
                     in (FamilyResultSig
sig, Maybe InjectivityAnn
inj)
       ; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
       ; let tfHead :: TypeFamilyHead
tfHead =
               Name
-> [TyVarBndr ()]
-> FamilyResultSig
-> Maybe InjectivityAnn
-> TypeFamilyHead
TH.TypeFamilyHead (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr ()]
tvs' FamilyResultSig
resultSig Maybe InjectivityAnn
injectivity
       ; if TyCon -> Bool
isOpenTypeFamilyTyCon TyCon
tc
         then do { FamInstEnvs
fam_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
                 ; [Dec]
instances <- TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc
                                  (FamInstEnvs -> TyCon -> [FamInst]
familyInstances FamInstEnvs
fam_envs TyCon
tc)
                 ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.FamilyI (TypeFamilyHead -> Dec
TH.OpenTypeFamilyD TypeFamilyHead
tfHead) [Dec]
instances) }
         else do { [TySynEqn]
eqns <-
                     case TyCon -> Maybe (CoAxiom Branched)
isClosedSynFamilyTyConWithAxiom_maybe TyCon
tc of
                       Just CoAxiom Branched
ax -> (CoAxBranch -> TcM TySynEqn)
-> [CoAxBranch] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (TyCon -> CoAxBranch -> TcM TySynEqn
reifyAxBranch TyCon
tc) ([CoAxBranch] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn])
-> [CoAxBranch] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn]
forall a b. (a -> b) -> a -> b
$
                                  Branches Branched -> [CoAxBranch]
forall (br :: BranchFlag). Branches br -> [CoAxBranch]
fromBranches (Branches Branched -> [CoAxBranch])
-> Branches Branched -> [CoAxBranch]
forall a b. (a -> b) -> a -> b
$ CoAxiom Branched -> Branches Branched
forall (br :: BranchFlag). CoAxiom br -> Branches br
coAxiomBranches CoAxiom Branched
ax
                       Maybe (CoAxiom Branched)
Nothing -> [TySynEqn] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
                 ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.FamilyI (TypeFamilyHead -> [TySynEqn] -> Dec
TH.ClosedTypeFamilyD TypeFamilyHead
tfHead [TySynEqn]
eqns)
                      []) } }

  | TyCon -> Bool
isDataFamilyTyCon TyCon
tc
  = do { let res_kind :: Type
res_kind = TyCon -> Type
tyConResKind TyCon
tc

       ; Maybe Type
kind' <- (Type -> Maybe Type)
-> TcM Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type -> Maybe Type
forall a. a -> Maybe a
Just (Type -> TcM Type
reifyKind Type
res_kind)

       ; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
       ; FamInstEnvs
fam_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
       ; [Dec]
instances <- TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc (FamInstEnvs -> TyCon -> [FamInst]
familyInstances FamInstEnvs
fam_envs TyCon
tc)
       ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.FamilyI
                       (Name -> [TyVarBndr ()] -> Maybe Type -> Dec
TH.DataFamilyD (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr ()]
tvs' Maybe Type
kind') [Dec]
instances) }

  | Just ([Id]
_, Type
rhs) <- TyCon -> Maybe ([Id], Type)
synTyConDefn_maybe TyCon
tc  -- Vanilla type synonym
  = do { Type
rhs' <- Type -> TcM Type
reifyType Type
rhs
       ; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
       ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> Info
TH.TyConI
                   (Name -> [TyVarBndr ()] -> Type -> Dec
TH.TySynD (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr ()]
tvs' Type
rhs'))
       }

  -- Special case for `type data` data constructors, which are reified as
  -- `ATyCon`s rather than `ADataCon`s (#22818).
  -- See Note [Type data declarations] in GHC.Rename.Module.
  | Just DataCon
dc <- TyCon -> Maybe DataCon
isPromotedDataCon_maybe TyCon
tc
  , DataCon -> Bool
isTypeDataCon DataCon
dc
  = DataCon -> TcM Info
mkDataConI DataCon
dc

  | Bool
otherwise
  = do  { [Type]
cxt <- [Type] -> TcM [Type]
reifyCxt (TyCon -> [Type]
tyConStupidTheta TyCon
tc)
        ; let tvs :: [Id]
tvs      = TyCon -> [Id]
tyConTyVars TyCon
tc
              dataCons :: [DataCon]
dataCons = TyCon -> [DataCon]
tyConDataCons TyCon
tc
              isGadt :: Bool
isGadt   = TyCon -> Bool
isGadtSyntaxTyCon TyCon
tc
        ; [Con]
cons <- (DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> [DataCon] -> IOEnv (Env TcGblEnv TcLclEnv) [Con]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Bool -> [Type] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadt ([Id] -> [Type]
mkTyVarTys [Id]
tvs)) [DataCon]
dataCons
        ; [TyVarBndr ()]
r_tvs <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
        ; let name :: Name
name = TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc
              deriv :: [a]
deriv = []        -- Don't know about deriving
              decl :: Dec
decl | TyCon -> Bool
isTypeDataTyCon TyCon
tc =
                       -- `type data` declarations have a special `Dec`,
                       -- separate from other `DataD`s. See
                       -- [Type data declarations] in GHC.Rename.Module.
                       Name -> [TyVarBndr ()] -> Maybe Type -> [Con] -> Dec
TH.TypeDataD Name
name [TyVarBndr ()]
r_tvs Maybe Type
forall a. Maybe a
Nothing [Con]
cons
                   | TyCon -> Bool
isNewTyCon TyCon
tc =
                       [Type]
-> Name
-> [TyVarBndr ()]
-> Maybe Type
-> Con
-> [DerivClause]
-> Dec
TH.NewtypeD [Type]
cxt Name
name [TyVarBndr ()]
r_tvs Maybe Type
forall a. Maybe a
Nothing ([Con] -> Con
forall a. HasCallStack => [a] -> a
head [Con]
cons) [DerivClause]
forall a. [a]
deriv
                   | Bool
otherwise     =
                       [Type]
-> Name
-> [TyVarBndr ()]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
TH.DataD    [Type]
cxt Name
name [TyVarBndr ()]
r_tvs Maybe Type
forall a. Maybe a
Nothing       [Con]
cons  [DerivClause]
forall a. [a]
deriv
        ; Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> Info
TH.TyConI Dec
decl) }

reifyDataCon :: Bool -> [Type] -> DataCon -> TcM TH.Con
reifyDataCon :: Bool -> [Type] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadtDataCon [Type]
tys DataCon
dc
  = do { let -- used for H98 data constructors
             ([Id]
ex_tvs, [Type]
theta, [Type]
arg_tys)
                 = DataCon -> [Type] -> ([Id], [Type], [Type])
dataConInstSig DataCon
dc [Type]
tys
             -- used for GADTs data constructors
             g_user_tvs' :: [InvisTVBinder]
g_user_tvs' = DataCon -> [InvisTVBinder]
dataConUserTyVarBinders DataCon
dc
             ([Id]
g_univ_tvs, [Id]
_, [EqSpec]
g_eq_spec, [Type]
g_theta', [Scaled Type]
g_arg_tys', Type
g_res_ty')
                 = DataCon -> ([Id], [Id], [EqSpec], [Type], [Scaled Type], Type)
dataConFullSig DataCon
dc
             ([SourceUnpackedness]
srcUnpks, [SourceStrictness]
srcStricts)
                 = (HsSrcBang -> (SourceUnpackedness, SourceStrictness))
-> [HsSrcBang] -> ([SourceUnpackedness], [SourceStrictness])
forall a b c. (a -> (b, c)) -> [a] -> ([b], [c])
mapAndUnzip HsSrcBang -> (SourceUnpackedness, SourceStrictness)
reifySourceBang (DataCon -> [HsSrcBang]
dataConSrcBangs DataCon
dc)
             dcdBangs :: [Bang]
dcdBangs  = (SourceUnpackedness -> SourceStrictness -> Bang)
-> [SourceUnpackedness] -> [SourceStrictness] -> [Bang]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith SourceUnpackedness -> SourceStrictness -> Bang
TH.Bang [SourceUnpackedness]
srcUnpks [SourceStrictness]
srcStricts
             fields :: [FieldLabel]
fields    = DataCon -> [FieldLabel]
dataConFieldLabels DataCon
dc
             name :: Name
name      = DataCon -> Name
forall n. NamedThing n => n -> Name
reifyName DataCon
dc
             -- Universal tvs present in eq_spec need to be filtered out, as
             -- they will not appear anywhere in the type.
             eq_spec_tvs :: VarSet
eq_spec_tvs = [Id] -> VarSet
mkVarSet ((EqSpec -> Id) -> [EqSpec] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map EqSpec -> Id
eqSpecTyVar [EqSpec]
g_eq_spec)

       ; (Subst
univ_subst, [Id]
_)
              -- See Note [Freshen reified GADT constructors' universal tyvars]
           <- [Id] -> TcM (Subst, [Id])
freshenTyVarBndrs ([Id] -> TcM (Subst, [Id])) -> [Id] -> TcM (Subst, [Id])
forall a b. (a -> b) -> a -> b
$
              (Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (Id -> VarSet -> Bool
`elemVarSet` VarSet
eq_spec_tvs) [Id]
g_univ_tvs
       ; let (Subst
tvb_subst, [InvisTVBinder]
g_user_tvs) = Subst -> [InvisTVBinder] -> (Subst, [InvisTVBinder])
forall {argf}.
Subst -> [VarBndr Id argf] -> (Subst, [VarBndr Id argf])
subst_tv_binders Subst
univ_subst [InvisTVBinder]
g_user_tvs'
             g_theta :: [Type]
g_theta   = (() :: Constraint) => Subst -> [Type] -> [Type]
Subst -> [Type] -> [Type]
substTys Subst
tvb_subst [Type]
g_theta'
             g_arg_tys :: [Type]
g_arg_tys = (() :: Constraint) => Subst -> [Type] -> [Type]
Subst -> [Type] -> [Type]
substTys Subst
tvb_subst ((Scaled Type -> Type) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Scaled Type -> Type
forall a. Scaled a -> a
scaledThing [Scaled Type]
g_arg_tys')
             g_res_ty :: Type
g_res_ty  = (() :: Constraint) => Subst -> Type -> Type
Subst -> Type -> Type
substTy  Subst
tvb_subst Type
g_res_ty'

       ; [Type]
r_arg_tys <- [Type] -> TcM [Type]
reifyTypes (if Bool
isGadtDataCon then [Type]
g_arg_tys else [Type]
arg_tys)

       ; Con
main_con <-
           if | Bool -> Bool
not ([FieldLabel] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
fields) Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isGadtDataCon ->
                  Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ Name -> [VarBangType] -> Con
TH.RecC Name
name ([Name] -> [Bang] -> [Type] -> [VarBangType]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 ((FieldLabel -> Name) -> [FieldLabel] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> Name
reifyFieldLabel [FieldLabel]
fields)
                                         [Bang]
dcdBangs [Type]
r_arg_tys)
              | Bool -> Bool
not ([FieldLabel] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
fields) -> do
                  { Type
res_ty <- Type -> TcM Type
reifyType Type
g_res_ty
                  ; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ [Name] -> [VarBangType] -> Type -> Con
TH.RecGadtC [Name
name]
                                     ([Name] -> [Bang] -> [Type] -> [VarBangType]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 ((FieldLabel -> Name) -> [FieldLabel] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Name
forall n. NamedThing n => n -> Name
reifyName (Name -> Name) -> (FieldLabel -> Name) -> FieldLabel -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldLabel -> Name
flSelector) [FieldLabel]
fields)
                                      [Bang]
dcdBangs [Type]
r_arg_tys) Type
res_ty }
                -- We need to check not isGadtDataCon here because GADT
                -- constructors can be declared infix.
                -- See Note [Infix GADT constructors] in GHC.Tc.TyCl.
              | DataCon -> Bool
dataConIsInfix DataCon
dc Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isGadtDataCon ->
                  Bool
-> IOEnv (Env TcGblEnv TcLclEnv) Con
-> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. HasCallStack => Bool -> a -> a
assert ([Type]
r_arg_tys [Type] -> SumArity -> Bool
forall a. [a] -> SumArity -> Bool
`lengthIs` SumArity
2) (IOEnv (Env TcGblEnv TcLclEnv) Con
 -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> IOEnv (Env TcGblEnv TcLclEnv) Con
-> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ do
                  { let [Type
r_a1, Type
r_a2] = [Type]
r_arg_tys
                        [Bang
s1,   Bang
s2]   = [Bang]
dcdBangs
                  ; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ BangType -> Name -> BangType -> Con
TH.InfixC (Bang
s1,Type
r_a1) Name
name (Bang
s2,Type
r_a2) }
              | Bool
isGadtDataCon -> do
                  { Type
res_ty <- Type -> TcM Type
reifyType Type
g_res_ty
                  ; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ [Name] -> [BangType] -> Type -> Con
TH.GadtC [Name
name] ([Bang]
dcdBangs [Bang] -> [Type] -> [BangType]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
r_arg_tys) Type
res_ty }
              | Bool
otherwise ->
                  Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ Name -> [BangType] -> Con
TH.NormalC Name
name ([Bang]
dcdBangs [Bang] -> [Type] -> [BangType]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
r_arg_tys)

       ; let ([InvisTVBinder]
ex_tvs', [Type]
theta') | Bool
isGadtDataCon = ([InvisTVBinder]
g_user_tvs, [Type]
g_theta)
                               | Bool
otherwise     = Bool -> ([InvisTVBinder], [Type]) -> ([InvisTVBinder], [Type])
forall a. HasCallStack => Bool -> a -> a
assert ((Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool