{-# LANGUAGE AllowAmbiguousTypes    #-}

{-# LANGUAGE DataKinds              #-}
{-# LANGUAGE FlexibleContexts       #-}
{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiWayIf             #-}
{-# LANGUAGE PatternSynonyms        #-}
{-# LANGUAGE RankNTypes             #-}
{-# LANGUAGE ScopedTypeVariables    #-}
{-# LANGUAGE TypeApplications       #-}
{-# LANGUAGE TypeFamilies           #-}
{-# LANGUAGE UndecidableInstances   #-}

{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

-----------------------------------------------------------------------------
--
-- (c) The University of Glasgow 2006
--
-- The purpose of this module is to transform an HsExpr into a CoreExpr which
-- when evaluated, returns a (Meta.Q Meta.Exp) computation analogous to the
-- input HsExpr. We do this in the DsM monad, which supplies access to
-- CoreExpr's of the "smart constructors" of the Meta.Exp datatype.
--
-- It also defines a bunch of knownKeyNames, in the same way as is done
-- in prelude/GHC.Builtin.Names.  It's much more convenient to do it here, because
-- otherwise we have to recompile GHC.Builtin.Names whenever we add a Name, which is
-- a Royal Pain (triggers other recompilation).
-----------------------------------------------------------------------------

module GHC.HsToCore.Quote( dsBracket ) where

import GHC.Prelude
import GHC.Platform

import GHC.Driver.DynFlags

import GHC.HsToCore.Errors.Types
import {-# SOURCE #-} GHC.HsToCore.Expr ( dsExpr )
import GHC.HsToCore.Match.Literal
import GHC.HsToCore.Monad
import GHC.HsToCore.Binds

import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH

import GHC.Hs

import GHC.Tc.Utils.TcType
import GHC.Tc.Types.Evidence

import GHC.Core.Class
import GHC.Core.DataCon
import GHC.Core.TyCon
import GHC.Core
import GHC.Core.Type( pattern ManyTy, mkFunTy )
import GHC.Core.Make
import GHC.Core.Utils

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

import GHC.Unit.Module

import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Misc
import GHC.Utils.Monad

import GHC.Data.Bag
import GHC.Data.FastString
import GHC.Data.Maybe

import GHC.Types.SrcLoc as SrcLoc
import GHC.Types.Unique
import GHC.Types.Basic
import GHC.Types.ForeignCall
import GHC.Types.Var
import GHC.Types.Id
import GHC.Types.SourceText
import GHC.Types.Fixity
import GHC.Types.TyThing
import GHC.Types.Name hiding( varName, tcName )
import GHC.Types.Name.Env

import GHC.TypeLits
import Data.Kind (Constraint)

import qualified GHC.LanguageExtensions as LangExt

import Language.Haskell.Syntax.Basic (FieldLabelString(..))

import Data.ByteString ( unpack )
import Control.Monad
import Data.List (sort, sortBy)
import Data.List.NonEmpty ( NonEmpty(..), toList )
import Data.Function
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Class
import GHC.Types.Name.Reader (RdrName(..))

data MetaWrappers = MetaWrappers {
      -- Applies its argument to a type argument `m` and dictionary `Quote m`
      MetaWrappers -> CoreExpr -> CoreExpr
quoteWrapper :: CoreExpr -> CoreExpr
      -- Apply its argument to a type argument `m` and a dictionary `Monad m`
    , MetaWrappers -> CoreExpr -> CoreExpr
monadWrapper :: CoreExpr -> CoreExpr
      -- Apply the container typed variable `m` to the argument type `T` to get `m T`.
    , MetaWrappers -> Type -> Type
metaTy :: Type -> Type
      -- Information about the wrappers which be printed to be inspected
    , MetaWrappers -> (HsWrapper, HsWrapper, Type)
_debugWrappers :: (HsWrapper, HsWrapper, Type)
    }

-- | Construct the functions which will apply the relevant part of the
-- QuoteWrapper to identifiers during desugaring.
mkMetaWrappers :: QuoteWrapper -> DsM MetaWrappers
mkMetaWrappers :: QuoteWrapper -> DsM MetaWrappers
mkMetaWrappers q :: QuoteWrapper
q@(QuoteWrapper Id
quote_var_raw Type
m_var) = do
      let quote_var :: CoreExpr
quote_var = Id -> CoreExpr
forall b. Id -> Expr b
Var Id
quote_var_raw
      -- Get the superclass selector to select the Monad dictionary, going
      -- to be used to construct the monadWrapper.
      quote_tc <- Name -> DsM TyCon
dsLookupTyCon Name
quoteClassName
      monad_tc <- dsLookupTyCon monadClassName
      let Just cls = tyConClass_maybe quote_tc
          Just monad_cls = tyConClass_maybe monad_tc
          -- Quote m -> Monad m
          monad_sel = Class -> Int -> Id
classSCSelId Class
cls Int
0

          -- Only used for the defensive assertion that the selector has
          -- the expected type
          tyvars = DataCon -> [InvisTVBinder]
dataConUserTyVarBinders (Class -> DataCon
classDataCon Class
cls)
          expected_ty = [InvisTVBinder] -> Type -> Type
mkInvisForAllTys [InvisTVBinder]
tyvars (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$
                        HasDebugCallStack => FunTyFlag -> Type -> Type -> Type -> Type
FunTyFlag -> Type -> Type -> Type -> Type
mkFunTy FunTyFlag
invisArgConstraintLike Type
ManyTy
                                (Class -> [Type] -> Type
mkClassPred Class
cls ([Id] -> [Type]
mkTyVarTys ([InvisTVBinder] -> [Id]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [InvisTVBinder]
tyvars)))
                                (Class -> [Type] -> Type
mkClassPred Class
monad_cls ([Id] -> [Type]
mkTyVarTys ([InvisTVBinder] -> [Id]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [InvisTVBinder]
tyvars)))

      massertPpr (idType monad_sel `eqType` expected_ty) (ppr monad_sel $$ ppr expected_ty)

      let m_ty = Type -> CoreExpr
forall b. Type -> Expr b
Type Type
m_var
          -- Construct the contents of MetaWrappers
          quoteWrapper = QuoteWrapper -> HsWrapper
applyQuoteWrapper QuoteWrapper
q
          monadWrapper = [EvTerm] -> HsWrapper
mkWpEvApps [CoreExpr -> EvTerm
EvExpr (CoreExpr -> EvTerm) -> CoreExpr -> EvTerm
forall a b. (a -> b) -> a -> b
$ CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreApps (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
monad_sel) [CoreExpr
m_ty, CoreExpr
quote_var]] HsWrapper -> HsWrapper -> HsWrapper
<.>
                            [Type] -> HsWrapper
mkWpTyApps [Type
m_var]
          tyWrapper Type
t = Type -> Type -> Type
mkAppTy Type
m_var Type
t
          debug = (HsWrapper
quoteWrapper, HsWrapper
monadWrapper, Type
m_var)
      dsHsWrapper quoteWrapper $ \CoreExpr -> CoreExpr
q_f -> do {
      HsWrapper
-> ((CoreExpr -> CoreExpr) -> DsM MetaWrappers) -> DsM MetaWrappers
forall a. HsWrapper -> ((CoreExpr -> CoreExpr) -> DsM a) -> DsM a
dsHsWrapper HsWrapper
monadWrapper (((CoreExpr -> CoreExpr) -> DsM MetaWrappers) -> DsM MetaWrappers)
-> ((CoreExpr -> CoreExpr) -> DsM MetaWrappers) -> DsM MetaWrappers
forall a b. (a -> b) -> a -> b
$ \CoreExpr -> CoreExpr
m_f -> do {
      MetaWrappers -> DsM MetaWrappers
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((CoreExpr -> CoreExpr)
-> (CoreExpr -> CoreExpr)
-> (Type -> Type)
-> (HsWrapper, HsWrapper, Type)
-> MetaWrappers
MetaWrappers CoreExpr -> CoreExpr
q_f CoreExpr -> CoreExpr
m_f Type -> Type
tyWrapper (HsWrapper, HsWrapper, Type)
debug) } }

-- Turn A into m A
wrapName :: Name -> MetaM Type
wrapName :: Name -> MetaM Type
wrapName Name
n = do
  t <- Name -> MetaM Type
lookupType Name
n
  wrap_fn <- asks metaTy
  return (wrap_fn t)

-- The local state is always the same, calculated from the passed in
-- wrapper
type MetaM a = ReaderT MetaWrappers DsM a

getPlatform :: MetaM Platform
getPlatform :: MetaM Platform
getPlatform = DynFlags -> Platform
targetPlatform (DynFlags -> Platform)
-> ReaderT MetaWrappers DsM DynFlags -> MetaM Platform
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT MetaWrappers DsM DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags

-----------------------------------------------------------------------------
dsBracket :: HsBracketTc -> DsM CoreExpr
-- See Note [Desugaring Brackets]
-- Returns a CoreExpr of type (M TH.Exp)
-- The quoted thing is parameterised over Name, even though it has
-- been type checked.  We don't want all those type decorations!

dsBracket :: HsBracketTc -> DsM CoreExpr
dsBracket (HsBracketTc { hsb_wrap :: HsBracketTc -> Maybe QuoteWrapper
hsb_wrap = Maybe QuoteWrapper
mb_wrap, hsb_splices :: HsBracketTc -> [PendingTcSplice]
hsb_splices = [PendingTcSplice]
splices, hsb_quote :: HsBracketTc -> HsQuote GhcRn
hsb_quote = HsQuote GhcRn
quote })
  = case HsQuote GhcRn
quote of
      VarBr XVarBr GhcRn
_ Bool
_ LIdP GhcRn
n -> do { MkC e1  <- Name -> DsM (Core Name)
lookupOccDsM (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n) ; return e1 }
      ExpBr XExpBr GhcRn
_ LHsExpr GhcRn
e   -> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
runOverloaded (ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr)
-> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ do { MkC e1  <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e     ; return e1 }
      PatBr XPatBr GhcRn
_ LPat GhcRn
p   -> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
runOverloaded (ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr)
-> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ do { MkC p1  <- LPat GhcRn -> MetaM (Core (M Pat))
repTopP LPat GhcRn
p   ; return p1 }
      TypBr XTypBr GhcRn
_ XRec GhcRn (HsType GhcRn)
t   -> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
runOverloaded (ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr)
-> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ do { MkC t1  <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
t    ; return t1 }
      DecBrG XDecBrG GhcRn
_ HsGroup GhcRn
gp -> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
runOverloaded (ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr)
-> ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ do { MkC ds1 <- HsGroup GhcRn -> MetaM (Core (M [Dec]))
repTopDs HsGroup GhcRn
gp ; return ds1 }
      DecBrL {}   -> String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dsUntypedBracket: unexpected DecBrL"
  where
    Just QuoteWrapper
wrap = Maybe QuoteWrapper
mb_wrap  -- Not used in VarBr case
      -- In the overloaded case we have to get given a wrapper, it is just
      -- the VarBr case that there is no wrapper, because they
      -- have a simple type.

    runOverloaded :: ReaderT MetaWrappers DsM CoreExpr -> DsM CoreExpr
runOverloaded ReaderT MetaWrappers DsM CoreExpr
act = do { mw <- QuoteWrapper -> DsM MetaWrappers
mkMetaWrappers QuoteWrapper
wrap
                           ; runReaderT (mapReaderT (dsExtendMetaEnv new_bit) act) mw }

    new_bit :: DsMetaEnv
new_bit = [(Name, DsMetaVal)] -> DsMetaEnv
forall a. [(Name, a)] -> NameEnv a
mkNameEnv [(Name
n, HsExpr GhcTc -> DsMetaVal
DsSplice (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
e))
                        | PendingTcSplice Name
n LHsExpr GhcTc
e <- [PendingTcSplice]
splices]

{-
Note [Desugaring Brackets]
~~~~~~~~~~~~~~~~~~~~~~~~~~

In the old days (pre Dec 2019) quotation brackets used to be monomorphic, ie
an expression bracket was of type Q Exp. This made the desugaring process simple
as there were no complicated type variables to keep consistent throughout the
whole AST. Due to the overloaded quotations proposal a quotation bracket is now
of type `Quote m => m Exp` and all the combinators defined in TH.Lib have been
generalised to work with any monad implementing a minimal interface.

https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0246-overloaded-bracket.rst

Users can rejoice at the flexibility but now there is some additional complexity in
how brackets are desugared as all these polymorphic combinators need their arguments
instantiated.

> IF YOU ARE MODIFYING THIS MODULE DO NOT USE ANYTHING SPECIFIC TO Q. INSTEAD
> USE THE `wrapName` FUNCTION TO APPLY THE `m` TYPE VARIABLE TO A TYPE CONSTRUCTOR.

What the arguments should be instantiated to is supplied by the `QuoteWrapper`
datatype which is produced by `GHC.Tc.Gen.Splice`. It is a pair of an evidence variable
for `Quote m` and a type variable `m`. All the polymorphic combinators in desugaring
need to be applied to these two type variables.

There are three important functions which do the application.

1. The default is `rep2` which takes a function name of type `Quote m => T` as an argument.
2. `rep2M` takes a function name of type `Monad m => T` as an argument
3. `rep2_nw` takes a function name without any constraints as an argument.

These functions then use the information in QuoteWrapper to apply the correct
arguments to the functions as the representation is constructed.

The `MetaM` monad carries around an environment of three functions which are
used in order to wrap the polymorphic combinators and instantiate the arguments
to the correct things.

1. quoteWrapper wraps functions of type `forall m . Quote m => T`
2. monadWrapper wraps functions of type `forall m . Monad m => T`
3. metaTy wraps a type in the polymorphic `m` variable of the whole representation.

Historical note about the implementation: At the first attempt, I attempted to
lie that the type of any quotation was `Quote m => m Exp` and then specialise it
by applying a wrapper to pass the `m` and `Quote m` arguments. This approach was
simpler to implement but didn't work because of nested splices. For example,
you might have a nested splice of a more specific type which fixes the type of
the overall quote and so all the combinators used must also be instantiated to
that specific type. Therefore you really have to use the contents of the quote
wrapper to directly apply the right type to the combinators rather than
first generate a polymorphic definition and then just apply the wrapper at the end.

-}

{- -------------- Examples --------------------

  [| \x -> x |]
====>
  gensym (unpackString "x"#) `bindQ` \ x1::String ->
  lam (pvar x1) (var x1)


  [| \x -> $(f [| x |]) |]
====>
  gensym (unpackString "x"#) `bindQ` \ x1::String ->
  lam (pvar x1) (f (var x1))
-}


-------------------------------------------------------
--                      Declarations
-------------------------------------------------------

-- Proxy for the phantom type of `Core`. All the generated fragments have
-- type something like `Quote m => m Exp` so to keep things simple we represent fragments
-- of that type as `M Exp`.
data M a

repTopP :: LPat GhcRn -> MetaM (Core (M TH.Pat))
repTopP :: LPat GhcRn -> MetaM (Core (M Pat))
repTopP LPat GhcRn
pat = do { ss <- [Name] -> MetaM [GenSymBind]
mkGenSyms (CollectFlag GhcRn -> LPat GhcRn -> [IdP GhcRn]
forall p. CollectPass p => CollectFlag p -> LPat p -> [IdP p]
collectPatBinders CollectFlag GhcRn
forall p. CollectFlag p
CollNoDictBinders LPat GhcRn
pat)
                 ; pat' <- addBinds ss (repLP pat)
                 ; wrapGenSyms ss pat' }

repTopDs :: HsGroup GhcRn -> MetaM (Core (M [TH.Dec]))
repTopDs :: HsGroup GhcRn -> MetaM (Core (M [Dec]))
repTopDs group :: HsGroup GhcRn
group@(HsGroup { hs_valds :: forall p. HsGroup p -> HsValBinds p
hs_valds   = HsValBinds GhcRn
valds
                        , hs_splcds :: forall p. HsGroup p -> [LSpliceDecl p]
hs_splcds  = [LSpliceDecl GhcRn]
splcds
                        , hs_tyclds :: forall p. HsGroup p -> [TyClGroup p]
hs_tyclds  = [TyClGroup GhcRn]
tyclds
                        , hs_derivds :: forall p. HsGroup p -> [LDerivDecl p]
hs_derivds = [LDerivDecl GhcRn]
derivds
                        , hs_fixds :: forall p. HsGroup p -> [LFixitySig p]
hs_fixds   = [LFixitySig GhcRn]
fixds
                        , hs_defds :: forall p. HsGroup p -> [LDefaultDecl p]
hs_defds   = [LDefaultDecl GhcRn]
defds
                        , hs_fords :: forall p. HsGroup p -> [LForeignDecl p]
hs_fords   = [LForeignDecl GhcRn]
fords
                        , hs_warnds :: forall p. HsGroup p -> [LWarnDecls p]
hs_warnds  = [LWarnDecls GhcRn]
warnds
                        , hs_annds :: forall p. HsGroup p -> [LAnnDecl p]
hs_annds   = [LAnnDecl GhcRn]
annds
                        , hs_ruleds :: forall p. HsGroup p -> [LRuleDecls p]
hs_ruleds  = [LRuleDecls GhcRn]
ruleds
                        , hs_docs :: forall p. HsGroup p -> [LDocDecl p]
hs_docs    = [LDocDecl GhcRn]
docs })
 = do { let { bndrs :: [Name]
bndrs  = HsValBinds GhcRn -> [Name]
hsScopedTvBinders HsValBinds GhcRn
valds
                       [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ HsGroup GhcRn -> [Name]
hsGroupBinders HsGroup GhcRn
group
                       [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ (FieldOcc GhcRn -> Name) -> [FieldOcc GhcRn] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map FieldOcc GhcRn -> XCFieldOcc GhcRn
FieldOcc GhcRn -> Name
forall pass. FieldOcc pass -> XCFieldOcc pass
foExt (HsValBinds GhcRn -> [FieldOcc GhcRn]
forall (p :: Pass).
IsPass p =>
HsValBinds (GhcPass p) -> [FieldOcc (GhcPass p)]
hsPatSynSelectors HsValBinds GhcRn
valds)
            ; instds :: [GenLocated SrcSpanAnnA (InstDecl GhcRn)]
instds = [TyClGroup GhcRn]
tyclds [TyClGroup GhcRn]
-> (TyClGroup GhcRn -> [GenLocated SrcSpanAnnA (InstDecl GhcRn)])
-> [GenLocated SrcSpanAnnA (InstDecl GhcRn)]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TyClGroup GhcRn -> [LInstDecl GhcRn]
TyClGroup GhcRn -> [GenLocated SrcSpanAnnA (InstDecl GhcRn)]
forall pass. TyClGroup pass -> [LInstDecl pass]
group_instds } ;
        ss <- [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
bndrs ;

        -- Bind all the names mainly to avoid repeated use of explicit strings.
        -- Thus we get
        --      do { t :: String <- genSym "T" ;
        --           return (Data t [] ...more t's... }
        -- The other important reason is that the output must mention
        -- only "T", not "Foo:T" where Foo is the current module

        decls <- addBinds ss (
                  do { val_ds   <- rep_val_binds valds
                     ; _        <- mapM no_splice splcds
                     ; tycl_ds  <- mapM repTyClD (tyClGroupTyClDecls tyclds)
                     ; role_ds  <- mapM repRoleD (concatMap group_roles tyclds)
                     ; kisig_ds <- mapM repKiSigD (concatMap group_kisigs tyclds)
                     ; inst_ds  <- mapM repInstD instds
                     ; deriv_ds <- mapM repStandaloneDerivD derivds
                     ; fix_ds   <- mapM repLFixD fixds
                     ; def_ds   <- mapM repDefD defds
                     ; for_ds   <- mapM repForD fords
                     ; _        <- mapM no_warn (concatMap (wd_warnings . unLoc)
                                                           warnds)
                     ; ann_ds   <- mapM repAnnD annds
                     ; rule_ds  <- mapM repRuleD (concatMap (rds_rules . unLoc)
                                                            ruleds)
                     ; _        <- mapM no_doc docs

                        -- more needed
                     ;  return (de_loc $ sort_by_loc $
                                val_ds ++ catMaybes tycl_ds ++ role_ds
                                       ++ kisig_ds
                                       ++ (concat fix_ds)
                                       ++ def_ds
                                       ++ inst_ds ++ rule_ds ++ for_ds
                                       ++ ann_ds ++ deriv_ds) }) ;

        core_list <- repListM decTyConName return decls ;

        dec_ty <- lookupType decTyConName ;
        q_decs  <- repSequenceM dec_ty core_list ;

        wrapGenSyms ss q_decs
      }
  where
    no_splice :: GenLocated a e -> MetaM a
no_splice (L a
loc e
_)
      = SrcSpan -> ThRejectionReason -> MetaM a
forall a. SrcSpan -> ThRejectionReason -> MetaM a
notHandledL (a -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA a
loc) ThRejectionReason
ThSplicesWithinDeclBrackets
    no_warn :: LWarnDecl GhcRn -> MetaM a
    no_warn :: forall a. LWarnDecl GhcRn -> MetaM a
no_warn (L SrcSpanAnnA
loc (Warning XWarning GhcRn
_ [LIdP GhcRn]
thing WarningTxt GhcRn
_))
      = SrcSpan -> ThRejectionReason -> MetaM a
forall a. SrcSpan -> ThRejectionReason -> MetaM a
notHandledL (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) ([LIdP GhcRn] -> ThRejectionReason
ThWarningAndDeprecationPragmas [LIdP GhcRn]
thing)
    no_doc :: GenLocated a e -> MetaM a
no_doc (L a
loc e
_)
      = SrcSpan -> ThRejectionReason -> MetaM a
forall a. SrcSpan -> ThRejectionReason -> MetaM a
notHandledL (a -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA a
loc) ThRejectionReason
ThHaddockDocumentation

hsScopedTvBinders :: HsValBinds GhcRn -> [Name]
-- See Note [Scoped type variables in quotes]
hsScopedTvBinders :: HsValBinds GhcRn -> [Name]
hsScopedTvBinders HsValBinds GhcRn
binds
  = (GenLocated SrcSpanAnnA (Sig GhcRn) -> [Name])
-> [GenLocated SrcSpanAnnA (Sig GhcRn)] -> [Name]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LSig GhcRn -> [Name]
GenLocated SrcSpanAnnA (Sig GhcRn) -> [Name]
get_scoped_tvs [LSig GhcRn]
[GenLocated SrcSpanAnnA (Sig GhcRn)]
sigs
  where
    sigs :: [LSig GhcRn]
sigs = case HsValBinds GhcRn
binds of
             ValBinds           XValBinds GhcRn GhcRn
_ LHsBindsLR GhcRn GhcRn
_ [LSig GhcRn]
sigs  -> [LSig GhcRn]
sigs
             XValBindsLR (NValBinds [(RecFlag, LHsBindsLR GhcRn GhcRn)]
_ [LSig GhcRn]
sigs) -> [LSig GhcRn]
sigs

get_scoped_tvs :: LSig GhcRn -> [Name]
get_scoped_tvs :: LSig GhcRn -> [Name]
get_scoped_tvs (L SrcSpanAnnA
_ Sig GhcRn
signature)
  | TypeSig XTypeSig GhcRn
_ [LIdP GhcRn]
_ LHsSigWcType GhcRn
sig <- Sig GhcRn
signature
  = LHsSigType GhcRn -> [Name]
get_scoped_tvs_from_sig (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn))
-> GenLocated SrcSpanAnnA (HsSigType GhcRn)
forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body LHsSigWcType GhcRn
HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn))
sig)
  | ClassOpSig XClassOpSig GhcRn
_ Bool
_ [LIdP GhcRn]
_ LHsSigType GhcRn
sig <- Sig GhcRn
signature
  = LHsSigType GhcRn -> [Name]
get_scoped_tvs_from_sig LHsSigType GhcRn
sig
  | PatSynSig XPatSynSig GhcRn
_ [LIdP GhcRn]
_ LHsSigType GhcRn
sig <- Sig GhcRn
signature
  = LHsSigType GhcRn -> [Name]
get_scoped_tvs_from_sig LHsSigType GhcRn
sig
  | Bool
otherwise
  = []

get_scoped_tvs_from_sig :: LHsSigType GhcRn -> [Name]
  -- Collect both implicit and explicit quantified variables, since
  -- the types in instance heads, as well as `via` types in DerivingVia, can
  -- bring implicitly quantified type variables into scope, e.g.,
  --
  --   instance Foo [a] where
  --     m = n @a
  --
  -- See also Note [Scoped type variables in quotes]
get_scoped_tvs_from_sig :: LHsSigType GhcRn -> [Name]
get_scoped_tvs_from_sig (L SrcSpanAnnA
_ (HsSig{sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcRn
outer_bndrs})) =
  HsOuterSigTyVarBndrs GhcRn -> [Name]
forall flag. HsOuterTyVarBndrs flag GhcRn -> [Name]
hsOuterTyVarNames HsOuterSigTyVarBndrs GhcRn
outer_bndrs

{- Notes

Note [Scoped type variables in quotes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoting declarations with scoped type variables requires some care. Consider:

  $([d| f :: forall a. a -> a
        f x = x::a
      |])

Here, the `forall a` brings `a` into scope over the binding group. This has
ramifications when desugaring the quote, as we must ensure that that the
desugared code binds `a` with `Language.Haskell.TH.newName` and refers to the
bound `a` type variable in the type signature and in the body of `f`. As a
result, the call to `newName` must occur before any part of the declaration for
`f` is processed. To achieve this, we:

 (a) Gensym a binding for `a` at the same time as we do one for `f`,
     collecting the relevant binders with the hsScopedTvBinders family of
     functions.

 (b) Use `addBinds` to bring these gensymmed bindings into scope over any
     part of the code where the type variables scope. In the `f` example,
     above, that means the type signature and the body of `f`.

 (c) When processing the `forall`, /don't/ gensym the type variables. We have
     already brought the type variables into scope in part (b), after all, so
     gensymming them again would lead to shadowing. We use the rep_ty_sig
     family of functions for processing types without gensymming the type
     variables again.

 (d) Finally, we use wrapGenSyms to generate the Core for these scoped type
     variables:

       newName "a" >>= \a ->
         ... -- process the type signature and body of `f`

The relevant places are signposted with references to this Note.

Note [Binders and occurrences]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When we desugar [d| data T = MkT |]
we want to get
        Data "T" [] [Con "MkT" []] []
and *not*
        Data "Foo:T" [] [Con "Foo:MkT" []] []
That is, the new data decl should fit into whatever new module it is
asked to fit in.   We do *not* clone, though; no need for this:
        Data "T79" ....

But if we see this:
        data T = MkT
        foo = reifyDecl T

then we must desugar to
        foo = Data "Foo:T" [] [Con "Foo:MkT" []] []

So in repTopDs we bring the binders into scope with mkGenSyms and addBinds.
And we use lookupOcc, rather than lookupBinder
in repTyClD and repC.

Note [Don't quantify implicit type variables in quotes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you're not careful, it's surprisingly easy to take this quoted declaration:

  [d| id :: a -> a
      id x = x
    |]

and have Template Haskell turn it into this:

  id :: forall a. a -> a
  id x = x

Notice that we explicitly quantified the variable `a`! The latter declaration
isn't what the user wrote in the first place.

Usually, the culprit behind these bugs is taking implicitly quantified type
variables (often from the hsib_vars field of HsImplicitBinders) and putting
them into a `ForallT` or `ForallC`. Doing so caused #13018 and #13123.
-}

-- represent associated family instances
--
repTyClD :: LTyClDecl GhcRn -> MetaM (Maybe (SrcSpan, Core (M TH.Dec)))

repTyClD :: LTyClDecl GhcRn
-> ReaderT MetaWrappers DsM (Maybe (SrcSpan, Core (M Dec)))
repTyClD (L SrcSpanAnnA
loc (FamDecl { tcdFam :: forall pass. TyClDecl pass -> FamilyDecl pass
tcdFam = FamilyDecl GhcRn
fam })) = ((SrcSpan, Core (M Dec)) -> Maybe (SrcSpan, Core (M Dec)))
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
-> ReaderT MetaWrappers DsM (Maybe (SrcSpan, Core (M Dec)))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (SrcSpan, Core (M Dec)) -> Maybe (SrcSpan, Core (M Dec))
forall a. a -> Maybe a
Just (ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
 -> ReaderT MetaWrappers DsM (Maybe (SrcSpan, Core (M Dec))))
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
-> ReaderT MetaWrappers DsM (Maybe (SrcSpan, Core (M Dec)))
forall a b. (a -> b) -> a -> b
$
                                              LFamilyDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repFamilyDecl (SrcSpanAnnA
-> FamilyDecl GhcRn -> GenLocated SrcSpanAnnA (FamilyDecl GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc FamilyDecl GhcRn
fam)

repTyClD (L SrcSpanAnnA
loc (SynDecl { tcdLName :: forall pass. TyClDecl pass -> LIdP pass
tcdLName = LIdP GhcRn
tc, tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
tcdTyVars = LHsQTyVars GhcRn
tvs, tcdRhs :: forall pass. TyClDecl pass -> LHsType pass
tcdRhs = XRec GhcRn (HsType GhcRn)
rhs }))
  = do { tc1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
tc           -- See Note [Binders and occurrences]
       ; dec <- addQTyVarBinds ReuseBoundNames tvs $ \Core [M (TyVarBndr BndrVis)]
bndrs ->
                Core Name
-> Core [M (TyVarBndr BndrVis)]
-> XRec GhcRn (HsType GhcRn)
-> MetaM (Core (M Dec))
repSynDecl Core Name
tc1 Core [M (TyVarBndr BndrVis)]
bndrs XRec GhcRn (HsType GhcRn)
rhs
       ; return (Just (locA loc, dec)) }

repTyClD (L SrcSpanAnnA
loc (DataDecl { tcdLName :: forall pass. TyClDecl pass -> LIdP pass
tcdLName = LIdP GhcRn
tc
                          , tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
tcdTyVars = LHsQTyVars GhcRn
tvs
                          , tcdDataDefn :: forall pass. TyClDecl pass -> HsDataDefn pass
tcdDataDefn = HsDataDefn GhcRn
defn }))
  = do { tc1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
tc           -- See Note [Binders and occurrences]
       ; dec <- addQTyVarBinds ReuseBoundNames tvs $ \Core [M (TyVarBndr BndrVis)]
bndrs ->
                Core Name
-> Either
     (Core [M (TyVarBndr BndrVis)])
     (Core (Maybe [M (TyVarBndr ())]), Core (M Type))
-> HsDataDefn GhcRn
-> MetaM (Core (M Dec))
repDataDefn Core Name
tc1 (Core [M (TyVarBndr BndrVis)]
-> Either
     (Core [M (TyVarBndr BndrVis)])
     (Core (Maybe [M (TyVarBndr ())]), Core (M Type))
forall a b. a -> Either a b
Left Core [M (TyVarBndr BndrVis)]
bndrs) HsDataDefn GhcRn
defn
       ; return (Just (locA loc, dec)) }

repTyClD (L SrcSpanAnnA
loc (ClassDecl { tcdCtxt :: forall pass. TyClDecl pass -> Maybe (LHsContext pass)
tcdCtxt = Maybe (LHsContext GhcRn)
cxt, tcdLName :: forall pass. TyClDecl pass -> LIdP pass
tcdLName = LIdP GhcRn
cls,
                             tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
tcdTyVars = LHsQTyVars GhcRn
tvs, tcdFDs :: forall pass. TyClDecl pass -> [LHsFunDep pass]
tcdFDs = [LHsFunDep GhcRn]
fds,
                             tcdSigs :: forall pass. TyClDecl pass -> [LSig pass]
tcdSigs = [LSig GhcRn]
sigs, tcdMeths :: forall pass. TyClDecl pass -> LHsBinds pass
tcdMeths = LHsBindsLR GhcRn GhcRn
meth_binds,
                             tcdATs :: forall pass. TyClDecl pass -> [LFamilyDecl pass]
tcdATs = [LFamilyDecl GhcRn]
ats, tcdATDefs :: forall pass. TyClDecl pass -> [LTyFamDefltDecl pass]
tcdATDefs = [LTyFamDefltDecl GhcRn]
atds }))
  = do { cls1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
cls         -- See Note [Binders and occurrences]
       ; dec  <- addQTyVarBinds FreshNamesOnly tvs $ \Core [M (TyVarBndr BndrVis)]
bndrs ->
           do { cxt1   <- Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
cxt
          -- See Note [Scoped type variables in quotes]
              ; (ss, sigs_binds) <- rep_meth_sigs_binds sigs meth_binds
              ; fds1   <- repLFunDeps fds
              ; ats1   <- repFamilyDecls ats
              ; atds1  <- mapM (repAssocTyFamDefaultD . unLoc) atds
              ; decls1 <- repListM decTyConName return (ats1 ++ atds1 ++ sigs_binds)
              ; decls2 <- repClass cxt1 cls1 bndrs fds1 decls1
              ; wrapGenSyms ss decls2 }
       ; return $ Just (locA loc, dec)
       }

-------------------------
repRoleD :: LRoleAnnotDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repRoleD :: LRoleAnnotDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repRoleD (L SrcSpanAnnA
loc (RoleAnnotDecl XCRoleAnnotDecl GhcRn
_ LIdP GhcRn
tycon [XRec GhcRn (Maybe Role)]
roles))
  = do { tycon1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
tycon
       ; roles1 <- mapM repRole roles
       ; roles2 <- coreList roleTyConName roles1
       ; dec <- repRoleAnnotD tycon1 roles2
       ; return (locA loc, dec) }

-------------------------
repKiSigD :: LStandaloneKindSig GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repKiSigD :: LStandaloneKindSig GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repKiSigD (L SrcSpanAnnA
loc StandaloneKindSig GhcRn
kisig) =
  case StandaloneKindSig GhcRn
kisig of
    StandaloneKindSig XStandaloneKindSig GhcRn
_ LIdP GhcRn
v LHsSigType GhcRn
ki -> do
      MkC th_v  <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
v
      MkC th_ki <- repHsSigType ki
      dec       <- rep2 kiSigDName [th_v, th_ki]
      pure (locA loc, dec)

-------------------------
repDataDefn :: Core TH.Name
            -> Either (Core [(M (TH.TyVarBndr TH.BndrVis))])
                        -- the repTyClD case
                      (Core (Maybe [(M (TH.TyVarBndr ()))]), Core (M TH.Type))
                        -- the repDataFamInstD case
            -> HsDataDefn GhcRn
            -> MetaM (Core (M TH.Dec))
repDataDefn :: Core Name
-> Either
     (Core [M (TyVarBndr BndrVis)])
     (Core (Maybe [M (TyVarBndr ())]), Core (M Type))
-> HsDataDefn GhcRn
-> MetaM (Core (M Dec))
repDataDefn Core Name
tc Either
  (Core [M (TyVarBndr BndrVis)])
  (Core (Maybe [M (TyVarBndr ())]), Core (M Type))
opts
          (HsDataDefn { dd_ctxt :: forall pass. HsDataDefn pass -> Maybe (LHsContext pass)
dd_ctxt = Maybe (LHsContext GhcRn)
cxt, dd_kindSig :: forall pass. HsDataDefn pass -> Maybe (LHsKind pass)
dd_kindSig = Maybe (XRec GhcRn (HsType GhcRn))
ksig
                      , dd_cons :: forall pass. HsDataDefn pass -> DataDefnCons (LConDecl pass)
dd_cons = DataDefnCons (LConDecl GhcRn)
cons, dd_derivs :: forall pass. HsDataDefn pass -> HsDeriving pass
dd_derivs = HsDeriving GhcRn
mb_derivs })
  = do { cxt1     <- Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
cxt
       ; derivs1  <- repDerivs mb_derivs
       ; case cons of
           NewTypeCon LConDecl GhcRn
con  -> do { con'  <- LConDecl GhcRn -> MetaM (Core (M Con))
repC LConDecl GhcRn
con
                                   ; ksig' <- repMaybeLTy ksig
                                   ; repNewtype cxt1 tc opts ksig' con'
                                                derivs1 }
           DataTypeCons Bool
type_data [LConDecl GhcRn]
cons -> do { ksig' <- Maybe (XRec GhcRn (HsType GhcRn)) -> MetaM (Core (Maybe (M Type)))
repMaybeLTy Maybe (XRec GhcRn (HsType GhcRn))
ksig
                               ; consL <- mapM repC cons
                               ; cons1 <- coreListM conTyConName consL
                               ; repData type_data cxt1 tc opts ksig' cons1
                                         derivs1 }
       }

repSynDecl :: Core TH.Name -> Core [(M (TH.TyVarBndr TH.BndrVis))]
           -> LHsType GhcRn
           -> MetaM (Core (M TH.Dec))
repSynDecl :: Core Name
-> Core [M (TyVarBndr BndrVis)]
-> XRec GhcRn (HsType GhcRn)
-> MetaM (Core (M Dec))
repSynDecl Core Name
tc Core [M (TyVarBndr BndrVis)]
bndrs XRec GhcRn (HsType GhcRn)
ty
  = do { ty1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
ty
       ; repTySyn tc bndrs ty1 }

repFamilyDecl :: LFamilyDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repFamilyDecl :: LFamilyDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repFamilyDecl decl :: LFamilyDecl GhcRn
decl@(L SrcSpanAnnA
loc (FamilyDecl { fdInfo :: forall pass. FamilyDecl pass -> FamilyInfo pass
fdInfo      = FamilyInfo GhcRn
info
                                      , fdLName :: forall pass. FamilyDecl pass -> LIdP pass
fdLName     = LIdP GhcRn
tc
                                      , fdTyVars :: forall pass. FamilyDecl pass -> LHsQTyVars pass
fdTyVars    = LHsQTyVars GhcRn
tvs
                                      , fdResultSig :: forall pass. FamilyDecl pass -> LFamilyResultSig pass
fdResultSig = L EpAnnCO
_ FamilyResultSig GhcRn
resultSig
                                      , fdInjectivityAnn :: forall pass. FamilyDecl pass -> Maybe (LInjectivityAnn pass)
fdInjectivityAnn = Maybe (LInjectivityAnn GhcRn)
injectivity }))
  = do { tc1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
tc           -- See Note [Binders and occurrences]
       ; let resTyVar = case FamilyResultSig GhcRn
resultSig of
                     TyVarSig XTyVarSig GhcRn
_ LHsTyVarBndr () GhcRn
bndr -> [LHsTyVarBndr () GhcRn -> IdP GhcRn
forall flag (p :: Pass).
LHsTyVarBndr flag (GhcPass p) -> IdP (GhcPass p)
hsLTyVarName LHsTyVarBndr () GhcRn
bndr]
                     FamilyResultSig GhcRn
_               -> []
       ; dec <- addQTyVarBinds ReuseBoundNames tvs $ \Core [M (TyVarBndr BndrVis)]
bndrs ->
                FreshOrReuse
-> [Name] -> MetaM (Core (M Dec)) -> MetaM (Core (M Dec))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
ReuseBoundNames [Name]
resTyVar (MetaM (Core (M Dec)) -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec)) -> MetaM (Core (M Dec))
forall a b. (a -> b) -> a -> b
$
           case FamilyInfo GhcRn
info of
             ClosedTypeFamily Maybe [LTyFamInstEqn GhcRn]
Nothing ->
                 ThRejectionReason -> MetaM (Core (M Dec))
forall a. ThRejectionReason -> MetaM a
notHandled (LFamilyDecl GhcRn -> ThRejectionReason
ThAbstractClosedTypeFamily LFamilyDecl GhcRn
decl)
             ClosedTypeFamily (Just [LTyFamInstEqn GhcRn]
eqns) ->
               do { eqns1  <- (GenLocated
   SrcSpanAnnA (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))
 -> ReaderT MetaWrappers DsM (Core (M TySynEqn)))
-> [GenLocated
      SrcSpanAnnA (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))]
-> ReaderT MetaWrappers DsM [Core (M 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 (TyFamInstEqn GhcRn -> ReaderT MetaWrappers DsM (Core (M TySynEqn))
FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
-> ReaderT MetaWrappers DsM (Core (M TySynEqn))
repTyFamEqn (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
 -> ReaderT MetaWrappers DsM (Core (M TySynEqn)))
-> (GenLocated
      SrcSpanAnnA (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))
    -> FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))
-> GenLocated
     SrcSpanAnnA (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))
-> ReaderT MetaWrappers DsM (Core (M TySynEqn))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated
  SrcSpanAnnA (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))
-> FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
forall l e. GenLocated l e -> e
unLoc) [LTyFamInstEqn GhcRn]
[GenLocated
   SrcSpanAnnA (FamEqn GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)))]
eqns
                  ; eqns2  <- coreListM tySynEqnTyConName eqns1
                  ; result <- repFamilyResultSig resultSig
                  ; inj    <- repInjectivityAnn injectivity
                  ; repClosedFamilyD tc1 bndrs result inj eqns2 }
             FamilyInfo GhcRn
OpenTypeFamily ->
               do { result <- FamilyResultSig GhcRn -> MetaM (Core (M FamilyResultSig))
repFamilyResultSig FamilyResultSig GhcRn
resultSig
                  ; inj    <- repInjectivityAnn injectivity
                  ; repOpenFamilyD tc1 bndrs result inj }
             FamilyInfo GhcRn
DataFamily ->
               do { kind <- FamilyResultSig GhcRn -> MetaM (Core (Maybe (M Type)))
repFamilyResultSigToMaybeKind FamilyResultSig GhcRn
resultSig
                  ; repDataFamilyD tc1 bndrs kind }
       ; return (locA loc, dec)
       }

-- | Represent result signature of a type family
repFamilyResultSig :: FamilyResultSig GhcRn -> MetaM (Core (M TH.FamilyResultSig))
repFamilyResultSig :: FamilyResultSig GhcRn -> MetaM (Core (M FamilyResultSig))
repFamilyResultSig (NoSig XNoSig GhcRn
_)         = MetaM (Core (M FamilyResultSig))
repNoSig
repFamilyResultSig (KindSig XCKindSig GhcRn
_ XRec GhcRn (HsType GhcRn)
ki)    = do { ki' <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
ki
                                          ; repKindSig ki' }
repFamilyResultSig (TyVarSig XTyVarSig GhcRn
_ LHsTyVarBndr () GhcRn
bndr) = do { bndr' <- LHsTyVarBndr () GhcRn -> MetaM (Core (M (TyVarBndr ())))
forall flag flag'.
RepTV flag flag' =>
LHsTyVarBndr flag GhcRn -> MetaM (Core (M (TyVarBndr flag')))
repTyVarBndr LHsTyVarBndr () GhcRn
bndr
                                          ; repTyVarSig bndr' }

-- | Represent result signature using a Maybe Kind. Used with data families,
-- where the result signature can be either missing or a kind but never a named
-- result variable.
repFamilyResultSigToMaybeKind :: FamilyResultSig GhcRn
                              -> MetaM (Core (Maybe (M TH.Kind)))
repFamilyResultSigToMaybeKind :: FamilyResultSig GhcRn -> MetaM (Core (Maybe (M Type)))
repFamilyResultSigToMaybeKind (NoSig XNoSig GhcRn
_) =
    Name -> MetaM (Core (Maybe (M Type)))
forall a. Name -> MetaM (Core (Maybe a))
coreNothingM Name
kindTyConName
repFamilyResultSigToMaybeKind (KindSig XCKindSig GhcRn
_ XRec GhcRn (HsType GhcRn)
ki) =
    Name -> Core (M Type) -> MetaM (Core (Maybe (M Type)))
forall a. Name -> Core a -> MetaM (Core (Maybe a))
coreJustM Name
kindTyConName (Core (M Type) -> MetaM (Core (Maybe (M Type))))
-> MetaM (Core (M Type)) -> MetaM (Core (Maybe (M Type)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
ki
repFamilyResultSigToMaybeKind TyVarSig{} =
    String -> MetaM (Core (Maybe (M Type)))
forall a. HasCallStack => String -> a
panic String
"repFamilyResultSigToMaybeKind: unexpected TyVarSig"

-- | Represent injectivity annotation of a type family
repInjectivityAnn :: Maybe (LInjectivityAnn GhcRn)
                  -> MetaM (Core (Maybe TH.InjectivityAnn))
repInjectivityAnn :: Maybe (LInjectivityAnn GhcRn)
-> MetaM (Core (Maybe InjectivityAnn))
repInjectivityAnn Maybe (LInjectivityAnn GhcRn)
Nothing =
    Name -> MetaM (Core (Maybe InjectivityAnn))
forall a. Name -> MetaM (Core (Maybe a))
coreNothing Name
injAnnTyConName
repInjectivityAnn (Just (L EpAnnCO
_ (InjectivityAnn XCInjectivityAnn GhcRn
_ LIdP GhcRn
lhs [LIdP GhcRn]
rhs))) =
    do { lhs'   <- Name -> MetaM (Core Name)
lookupBinder (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
lhs)
       ; rhs1   <- mapM (lookupBinder . unLoc) rhs
       ; rhs2   <- coreList nameTyConName rhs1
       ; injAnn <- rep2_nw injectivityAnnName [unC lhs', unC rhs2]
       ; coreJust injAnnTyConName injAnn }

repFamilyDecls :: [LFamilyDecl GhcRn] -> MetaM [Core (M TH.Dec)]
repFamilyDecls :: [LFamilyDecl GhcRn] -> MetaM [Core (M Dec)]
repFamilyDecls [LFamilyDecl GhcRn]
fds = ([(SrcSpan, Core (M Dec))] -> [Core (M Dec)])
-> MetaM [(SrcSpan, Core (M Dec))] -> MetaM [Core (M Dec)]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [(SrcSpan, Core (M Dec))] -> [Core (M Dec)]
forall a b. [(a, b)] -> [b]
de_loc ((GenLocated SrcSpanAnnA (FamilyDecl GhcRn)
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnA (FamilyDecl GhcRn)]
-> MetaM [(SrcSpan, Core (M Dec))]
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 LFamilyDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
GenLocated SrcSpanAnnA (FamilyDecl GhcRn)
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repFamilyDecl [LFamilyDecl GhcRn]
[GenLocated SrcSpanAnnA (FamilyDecl GhcRn)]
fds)

repAssocTyFamDefaultD :: TyFamDefltDecl GhcRn -> MetaM (Core (M TH.Dec))
repAssocTyFamDefaultD :: TyFamDefltDecl GhcRn -> MetaM (Core (M Dec))
repAssocTyFamDefaultD = TyFamDefltDecl GhcRn -> MetaM (Core (M Dec))
repTyFamInstD

-------------------------
-- represent fundeps
--
repLFunDeps :: [LHsFunDep GhcRn] -> MetaM (Core [TH.FunDep])
repLFunDeps :: [LHsFunDep GhcRn] -> MetaM (Core [FunDep])
repLFunDeps [LHsFunDep GhcRn]
fds = Name
-> (GenLocated SrcSpanAnnA (FunDep GhcRn) -> MetaM (Core FunDep))
-> [GenLocated SrcSpanAnnA (FunDep GhcRn)]
-> MetaM (Core [FunDep])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repList Name
funDepTyConName LHsFunDep GhcRn -> MetaM (Core FunDep)
GenLocated SrcSpanAnnA (FunDep GhcRn) -> MetaM (Core FunDep)
repLFunDep [LHsFunDep GhcRn]
[GenLocated SrcSpanAnnA (FunDep GhcRn)]
fds

repLFunDep :: LHsFunDep GhcRn -> MetaM (Core TH.FunDep)
repLFunDep :: LHsFunDep GhcRn -> MetaM (Core FunDep)
repLFunDep (L SrcSpanAnnA
_ (FunDep XCFunDep GhcRn
_ [LIdP GhcRn]
xs [LIdP GhcRn]
ys))
   = do xs' <- Name
-> (GenLocated SrcSpanAnnN Name -> MetaM (Core Name))
-> [GenLocated SrcSpanAnnN Name]
-> MetaM (Core [Name])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repList Name
nameTyConName (Name -> MetaM (Core Name)
lookupBinder (Name -> MetaM (Core Name))
-> (GenLocated SrcSpanAnnN Name -> Name)
-> GenLocated SrcSpanAnnN Name
-> MetaM (Core Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc) [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
xs
        ys' <- repList nameTyConName (lookupBinder . unLoc) ys
        repFunDep xs' ys'

-- Represent instance declarations
--
repInstD :: LInstDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repInstD :: LInstDecl GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repInstD (L SrcSpanAnnA
loc (TyFamInstD { tfid_inst :: forall pass. InstDecl pass -> TyFamInstDecl pass
tfid_inst = TyFamDefltDecl GhcRn
fi_decl }))
  = do { dec <- TyFamDefltDecl GhcRn -> MetaM (Core (M Dec))
repTyFamInstD TyFamDefltDecl GhcRn
fi_decl
       ; return (locA loc, dec) }
repInstD (L SrcSpanAnnA
loc (DataFamInstD { dfid_inst :: forall pass. InstDecl pass -> DataFamInstDecl pass
dfid_inst = DataFamInstDecl GhcRn
fi_decl }))
  = do { dec <- DataFamInstDecl GhcRn -> MetaM (Core (M Dec))
repDataFamInstD DataFamInstDecl GhcRn
fi_decl
       ; return (locA loc, dec) }
repInstD (L SrcSpanAnnA
loc (ClsInstD { cid_inst :: forall pass. InstDecl pass -> ClsInstDecl pass
cid_inst = ClsInstDecl GhcRn
cls_decl }))
  = do { dec <- ClsInstDecl GhcRn -> MetaM (Core (M Dec))
repClsInstD ClsInstDecl GhcRn
cls_decl
       ; return (locA loc, dec) }

repClsInstD :: ClsInstDecl GhcRn -> MetaM (Core (M TH.Dec))
repClsInstD :: ClsInstDecl GhcRn -> MetaM (Core (M Dec))
repClsInstD (ClsInstDecl { cid_poly_ty :: forall pass. ClsInstDecl pass -> LHsSigType pass
cid_poly_ty = LHsSigType GhcRn
ty, cid_binds :: forall pass. ClsInstDecl pass -> LHsBinds pass
cid_binds = LHsBindsLR GhcRn GhcRn
binds
                         , cid_sigs :: forall pass. ClsInstDecl pass -> [LSig pass]
cid_sigs = [LSig GhcRn]
sigs, cid_tyfam_insts :: forall pass. ClsInstDecl pass -> [LTyFamInstDecl pass]
cid_tyfam_insts = [LTyFamDefltDecl GhcRn]
ats
                         , cid_datafam_insts :: forall pass. ClsInstDecl pass -> [LDataFamInstDecl pass]
cid_datafam_insts = [LDataFamInstDecl GhcRn]
adts
                         , cid_overlap_mode :: forall pass. ClsInstDecl pass -> Maybe (XRec pass OverlapMode)
cid_overlap_mode = Maybe (XRec GhcRn OverlapMode)
overlap
                         })
  = FreshOrReuse
-> [Name] -> MetaM (Core (M Dec)) -> MetaM (Core (M Dec))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
FreshNamesOnly [Name]
tvs (MetaM (Core (M Dec)) -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec)) -> MetaM (Core (M Dec))
forall a b. (a -> b) -> a -> b
$
            -- We must bring the type variables into scope, so their
            -- occurrences don't fail, even though the binders don't
            -- appear in the resulting data structure
            --
            -- But we do NOT bring the binders of 'binds' into scope
            -- because they are properly regarded as occurrences
            -- For example, the method names should be bound to
            -- the selector Ids, not to fresh names (#5410)
            --
            do { cxt1     <- Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
cxt
               ; inst_ty1 <- repLTy inst_ty
          -- See Note [Scoped type variables in quotes]
               ; (ss, sigs_binds) <- rep_meth_sigs_binds sigs binds
               ; ats1   <- mapM (repTyFamInstD . unLoc) ats
               ; adts1  <- mapM (repDataFamInstD . unLoc) adts
               ; decls1 <- coreListM decTyConName (ats1 ++ adts1 ++ sigs_binds)
               ; rOver  <- repOverlap (fmap unLoc overlap)
               ; decls2 <- repInst rOver cxt1 inst_ty1 decls1
               ; wrapGenSyms ss decls2 }
 where
   ([Name]
tvs, Maybe (LHsContext GhcRn)
cxt, XRec GhcRn (HsType GhcRn)
inst_ty) = LHsSigType GhcRn
-> ([Name], Maybe (LHsContext GhcRn), XRec GhcRn (HsType GhcRn))
splitLHsInstDeclTy LHsSigType GhcRn
ty

repStandaloneDerivD :: LDerivDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repStandaloneDerivD :: LDerivDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repStandaloneDerivD (L SrcSpanAnnA
loc (DerivDecl { deriv_strategy :: forall pass. DerivDecl pass -> Maybe (LDerivStrategy pass)
deriv_strategy = Maybe (LDerivStrategy GhcRn)
strat
                                       , deriv_type :: forall pass. DerivDecl pass -> LHsSigWcType pass
deriv_type     = LHsSigWcType GhcRn
ty }))
  = do { dec <- Maybe (LDerivStrategy GhcRn)
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec))
forall {k} (a :: k).
Maybe (LDerivStrategy GhcRn)
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> MetaM (Core (M a))
repDerivStrategy Maybe (LDerivStrategy GhcRn)
strat  ((Core (Maybe (M DerivStrategy)) -> MetaM (Core (M Dec)))
 -> MetaM (Core (M Dec)))
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec))
forall a b. (a -> b) -> a -> b
$ \Core (Maybe (M DerivStrategy))
strat' ->
                FreshOrReuse
-> [Name] -> MetaM (Core (M Dec)) -> MetaM (Core (M Dec))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
FreshNamesOnly [Name]
tvs (MetaM (Core (M Dec)) -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec)) -> MetaM (Core (M Dec))
forall a b. (a -> b) -> a -> b
$
                do { cxt'     <- Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
cxt
                   ; inst_ty' <- repLTy inst_ty
                   ; repDeriv strat' cxt' inst_ty' }
       ; return (locA loc, dec) }
  where
    ([Name]
tvs, Maybe (LHsContext GhcRn)
cxt, XRec GhcRn (HsType GhcRn)
inst_ty) = LHsSigType GhcRn
-> ([Name], Maybe (LHsContext GhcRn), XRec GhcRn (HsType GhcRn))
splitLHsInstDeclTy (LHsSigWcType GhcRn -> LHsSigType GhcRn
forall pass. LHsSigWcType pass -> LHsSigType pass
dropWildCards LHsSigWcType GhcRn
ty)

repTyFamInstD :: TyFamInstDecl GhcRn -> MetaM (Core (M TH.Dec))
repTyFamInstD :: TyFamDefltDecl GhcRn -> MetaM (Core (M Dec))
repTyFamInstD (TyFamInstDecl { tfid_eqn :: forall pass. TyFamInstDecl pass -> TyFamInstEqn pass
tfid_eqn = TyFamInstEqn GhcRn
eqn })
  = do { eqn1 <- TyFamInstEqn GhcRn -> ReaderT MetaWrappers DsM (Core (M TySynEqn))
repTyFamEqn TyFamInstEqn GhcRn
eqn
       ; repTySynInst eqn1 }

repTyFamEqn :: TyFamInstEqn GhcRn -> MetaM (Core (M TH.TySynEqn))
repTyFamEqn :: TyFamInstEqn GhcRn -> ReaderT MetaWrappers DsM (Core (M TySynEqn))
repTyFamEqn (FamEqn { feqn_tycon :: forall pass rhs. FamEqn pass rhs -> LIdP pass
feqn_tycon = LIdP GhcRn
tc_name
                    , feqn_bndrs :: forall pass rhs. FamEqn pass rhs -> HsOuterFamEqnTyVarBndrs pass
feqn_bndrs = HsOuterFamEqnTyVarBndrs GhcRn
outer_bndrs
                    , feqn_pats :: forall pass rhs. FamEqn pass rhs -> HsFamEqnPats pass
feqn_pats = HsFamEqnPats GhcRn
tys
                    , feqn_fixity :: forall pass rhs. FamEqn pass rhs -> LexicalFixity
feqn_fixity = LexicalFixity
fixity
                    , feqn_rhs :: forall pass rhs. FamEqn pass rhs -> rhs
feqn_rhs  = XRec GhcRn (HsType GhcRn)
rhs })
  = do { tc <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
tc_name     -- See Note [Binders and occurrences]
       ; addHsOuterFamEqnTyVarBinds outer_bndrs $ \Core (Maybe [M (TyVarBndr ())])
mb_exp_bndrs ->
         do { tys1 <- case LexicalFixity
fixity of
                        LexicalFixity
Prefix -> MetaM (Core (M Type))
-> HsFamEqnPats GhcRn -> MetaM (Core (M Type))
repTyArgs (Core Name -> MetaM (Core (M Type))
repNamedTyCon Core Name
tc) HsFamEqnPats GhcRn
tys
                        LexicalFixity
Infix  -> do { (HsValArg _ t1: HsValArg _ t2: args) <- HsFamEqnPats GhcRn -> MetaM (HsFamEqnPats GhcRn)
checkTys HsFamEqnPats GhcRn
tys
                                     ; t1' <- repLTy t1
                                     ; t2'  <- repLTy t2
                                     ; repTyArgs (repTInfix t1' tc t2') args }
            ; rhs1 <- repLTy rhs
            ; repTySynEqn mb_exp_bndrs tys1 rhs1 } }
     where checkTys :: [LHsTypeArg GhcRn] -> MetaM [LHsTypeArg GhcRn]
           checkTys :: HsFamEqnPats GhcRn -> MetaM (HsFamEqnPats GhcRn)
checkTys tys :: HsFamEqnPats GhcRn
tys@(HsValArg XValArg GhcRn
_ XRec GhcRn (HsType GhcRn)
_:HsValArg XValArg GhcRn
_ XRec GhcRn (HsType GhcRn)
_:HsFamEqnPats GhcRn
_) = [HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn))]
-> ReaderT
     MetaWrappers
     DsM
     [HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn))]
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return HsFamEqnPats GhcRn
[HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn))]
tys
           checkTys HsFamEqnPats GhcRn
_ = String
-> ReaderT
     MetaWrappers
     DsM
     [HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn))]
forall a. HasCallStack => String -> a
panic String
"repTyFamEqn:checkTys"

repTyArgs :: MetaM (Core (M TH.Type)) -> [LHsTypeArg GhcRn] -> MetaM (Core (M TH.Type))
repTyArgs :: MetaM (Core (M Type))
-> HsFamEqnPats GhcRn -> MetaM (Core (M Type))
repTyArgs MetaM (Core (M Type))
f [] = MetaM (Core (M Type))
f
repTyArgs MetaM (Core (M Type))
f (HsValArg XValArg GhcRn
_ XRec GhcRn (HsType GhcRn)
ty : HsFamEqnPats GhcRn
as)  = do { f' <- MetaM (Core (M Type))
f
                                       ; ty' <- repLTy ty
                                       ; repTyArgs (repTapp f' ty') as }
repTyArgs MetaM (Core (M Type))
f (HsTypeArg XTypeArg GhcRn
_ XRec GhcRn (HsType GhcRn)
ki : HsFamEqnPats GhcRn
as) = do { f' <- MetaM (Core (M Type))
f
                                       ; ki' <- repLTy ki
                                       ; repTyArgs (repTappKind f' ki') as }
repTyArgs MetaM (Core (M Type))
f (HsArgPar XArgPar GhcRn
_ : HsFamEqnPats GhcRn
as) = MetaM (Core (M Type))
-> HsFamEqnPats GhcRn -> MetaM (Core (M Type))
repTyArgs MetaM (Core (M Type))
f HsFamEqnPats GhcRn
as

repDataFamInstD :: DataFamInstDecl GhcRn -> MetaM (Core (M TH.Dec))
repDataFamInstD :: DataFamInstDecl GhcRn -> MetaM (Core (M Dec))
repDataFamInstD (DataFamInstDecl { dfid_eqn :: forall pass. DataFamInstDecl pass -> FamEqn pass (HsDataDefn pass)
dfid_eqn =
                                      FamEqn { feqn_tycon :: forall pass rhs. FamEqn pass rhs -> LIdP pass
feqn_tycon = LIdP GhcRn
tc_name
                                             , feqn_bndrs :: forall pass rhs. FamEqn pass rhs -> HsOuterFamEqnTyVarBndrs pass
feqn_bndrs = HsOuterFamEqnTyVarBndrs GhcRn
outer_bndrs
                                             , feqn_pats :: forall pass rhs. FamEqn pass rhs -> HsFamEqnPats pass
feqn_pats  = HsFamEqnPats GhcRn
tys
                                             , feqn_fixity :: forall pass rhs. FamEqn pass rhs -> LexicalFixity
feqn_fixity = LexicalFixity
fixity
                                             , feqn_rhs :: forall pass rhs. FamEqn pass rhs -> rhs
feqn_rhs   = HsDataDefn GhcRn
defn }})
  = do { tc <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
tc_name         -- See Note [Binders and occurrences]
       ; addHsOuterFamEqnTyVarBinds outer_bndrs $ \Core (Maybe [M (TyVarBndr ())])
mb_exp_bndrs ->
         do { tys1 <- case LexicalFixity
fixity of
                        LexicalFixity
Prefix -> MetaM (Core (M Type))
-> HsFamEqnPats GhcRn -> MetaM (Core (M Type))
repTyArgs (Core Name -> MetaM (Core (M Type))
repNamedTyCon Core Name
tc) HsFamEqnPats GhcRn
tys
                        LexicalFixity
Infix  -> do { (HsValArg _ t1: HsValArg _ t2: args) <- HsFamEqnPats GhcRn -> MetaM (HsFamEqnPats GhcRn)
checkTys HsFamEqnPats GhcRn
tys
                                     ; t1' <- repLTy t1
                                     ; t2'  <- repLTy t2
                                     ; repTyArgs (repTInfix t1' tc t2') args }
            ; repDataDefn tc (Right (mb_exp_bndrs, tys1)) defn } }

      where checkTys :: [LHsTypeArg GhcRn] -> MetaM [LHsTypeArg GhcRn]
            checkTys :: HsFamEqnPats GhcRn -> MetaM (HsFamEqnPats GhcRn)
checkTys tys :: HsFamEqnPats GhcRn
tys@(HsValArg XValArg GhcRn
_ XRec GhcRn (HsType GhcRn)
_: HsValArg XValArg GhcRn
_ XRec GhcRn (HsType GhcRn)
_: HsFamEqnPats GhcRn
_) = [HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn))]
-> ReaderT
     MetaWrappers
     DsM
     [HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn))]
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return HsFamEqnPats GhcRn
[HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn))]
tys
            checkTys HsFamEqnPats GhcRn
_ = String
-> ReaderT
     MetaWrappers
     DsM
     [HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn))]
forall a. HasCallStack => String -> a
panic String
"repDataFamInstD:checkTys"

repForD :: LForeignDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repForD :: LForeignDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repForD (L SrcSpanAnnA
loc (ForeignImport { fd_name :: forall pass. ForeignDecl pass -> LIdP pass
fd_name = LIdP GhcRn
name, fd_sig_ty :: forall pass. ForeignDecl pass -> LHsSigType pass
fd_sig_ty = LHsSigType GhcRn
typ
                                  , fd_fi :: forall pass. ForeignDecl pass -> ForeignImport pass
fd_fi = CImport XCImport GhcRn
_ (L EpaLocation
_ CCallConv
cc)
                                                    (L EpaLocation
_ Safety
s) Maybe Header
mch CImportSpec
cis }))
 = do MkC name' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
name
      MkC typ' <- repHsSigType typ
      MkC cc' <- repCCallConv cc
      MkC s' <- repSafety s
      cis' <- conv_cimportspec cis
      MkC str <- coreStringLit (mkFastString (static ++ chStr ++ cis'))
      dec <- rep2 forImpDName [cc', s', str, name', typ']
      return (locA loc, dec)
 where
    conv_cimportspec :: CImportSpec -> MetaM String
conv_cimportspec (CLabel FastString
cls)
      = ThRejectionReason -> MetaM String
forall a. ThRejectionReason -> MetaM a
notHandled (FastString -> ThRejectionReason
ThForeignLabel FastString
cls)
    conv_cimportspec (CFunction CCallTarget
DynamicTarget) = String -> MetaM String
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return String
"dynamic"
    conv_cimportspec (CFunction (StaticTarget SourceText
_ FastString
fs Maybe Unit
_ Bool
True))
                            = String -> MetaM String
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return (FastString -> String
unpackFS FastString
fs)
    conv_cimportspec (CFunction (StaticTarget SourceText
_ FastString
_  Maybe Unit
_ Bool
False))
                            = String -> MetaM String
forall a. HasCallStack => String -> a
panic String
"conv_cimportspec: values not supported yet"
    conv_cimportspec CImportSpec
CWrapper = String -> MetaM String
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return String
"wrapper"
    -- these calling conventions do not support headers and the static keyword
    raw_cconv :: Bool
raw_cconv = CCallConv
cc CCallConv -> CCallConv -> Bool
forall a. Eq a => a -> a -> Bool
== CCallConv
PrimCallConv Bool -> Bool -> Bool
|| CCallConv
cc CCallConv -> CCallConv -> Bool
forall a. Eq a => a -> a -> Bool
== CCallConv
JavaScriptCallConv
    static :: String
static = case CImportSpec
cis of
                 CFunction (StaticTarget SourceText
_ FastString
_ Maybe Unit
_ Bool
_) | Bool -> Bool
not Bool
raw_cconv -> String
"static "
                 CImportSpec
_ -> String
""
    chStr :: String
chStr = case Maybe Header
mch of
            Just (Header SourceText
_ FastString
h) | Bool -> Bool
not Bool
raw_cconv -> FastString -> String
unpackFS FastString
h String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" "
            Maybe Header
_ -> String
""
repForD decl :: LForeignDecl GhcRn
decl@(L SrcSpanAnnA
_ ForeignExport{}) = ThRejectionReason
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
forall a. ThRejectionReason -> MetaM a
notHandled (LForeignDecl GhcRn -> ThRejectionReason
ThForeignExport LForeignDecl GhcRn
decl)

repCCallConv :: CCallConv -> MetaM (Core TH.Callconv)
repCCallConv :: CCallConv -> MetaM (Core Callconv)
repCCallConv CCallConv
CCallConv          = Name -> [CoreExpr] -> MetaM (Core Callconv)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
cCallName []
repCCallConv CCallConv
StdCallConv        = Name -> [CoreExpr] -> MetaM (Core Callconv)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
stdCallName []
repCCallConv CCallConv
CApiConv           = Name -> [CoreExpr] -> MetaM (Core Callconv)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
cApiCallName []
repCCallConv CCallConv
PrimCallConv       = Name -> [CoreExpr] -> MetaM (Core Callconv)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
primCallName []
repCCallConv CCallConv
JavaScriptCallConv = Name -> [CoreExpr] -> MetaM (Core Callconv)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
javaScriptCallName []

repSafety :: Safety -> MetaM (Core TH.Safety)
repSafety :: Safety -> MetaM (Core Safety)
repSafety Safety
PlayRisky = Name -> [CoreExpr] -> MetaM (Core Safety)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
unsafeName []
repSafety Safety
PlayInterruptible = Name -> [CoreExpr] -> MetaM (Core Safety)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
interruptibleName []
repSafety Safety
PlaySafe = Name -> [CoreExpr] -> MetaM (Core Safety)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
safeName []

repLFixD :: LFixitySig GhcRn -> MetaM [(SrcSpan, Core (M TH.Dec))]
repLFixD :: LFixitySig GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
repLFixD (L SrcSpanAnnA
loc FixitySig GhcRn
fix_sig) = SrcSpan -> FixitySig GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_fix_d (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) FixitySig GhcRn
fix_sig

rep_fix_d :: SrcSpan -> FixitySig GhcRn -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_fix_d :: SrcSpan -> FixitySig GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_fix_d SrcSpan
loc (FixitySig XFixitySig GhcRn
ns_spec [LIdP GhcRn]
names (Fixity SourceText
_ Int
prec FixityDirection
dir))
  = do { MkC prec' <- Int -> MetaM (Core Int)
coreIntLit Int
prec
       ; let rep_fn = case FixityDirection
dir of
                        FixityDirection
InfixL -> Name
infixLWithSpecDName
                        FixityDirection
InfixR -> Name
infixRWithSpecDName
                        FixityDirection
InfixN -> Name
infixNWithSpecDName
       ; let do_one GenLocated SrcSpanAnnN Name
name
              = do { MkC name' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
name
                   ; MkC ns_spec' <- repNamespaceSpecifier ns_spec
                   ; dec <- rep2 rep_fn [prec', ns_spec', name']
                   ; return (loc,dec) }
       ; mapM do_one names }

repDefD :: LDefaultDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repDefD :: LDefaultDecl GhcRn
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repDefD (L SrcSpanAnnA
loc (DefaultDecl XCDefaultDecl GhcRn
_ [XRec GhcRn (HsType GhcRn)]
tys)) = do { tys1 <- [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys
                                         ; MkC tys2 <- coreListM typeTyConName tys1
                                         ; dec <- rep2 defaultDName [tys2]
                                         ; return (locA loc, dec)}

repRuleD :: LRuleDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repRuleD :: LRuleDecl GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repRuleD (L SrcSpanAnnA
loc (HsRule { rd_name :: forall pass. RuleDecl pass -> XRec pass FastString
rd_name = XRec GhcRn FastString
n
                        , rd_act :: forall pass. RuleDecl pass -> Activation
rd_act = Activation
act
                        , rd_tyvs :: forall pass.
RuleDecl pass -> Maybe [LHsTyVarBndr () (NoGhcTc pass)]
rd_tyvs = Maybe [LHsTyVarBndr () (NoGhcTc GhcRn)]
m_ty_bndrs
                        , rd_tmvs :: forall pass. RuleDecl pass -> [LRuleBndr pass]
rd_tmvs = [LRuleBndr GhcRn]
tm_bndrs
                        , rd_lhs :: forall pass. RuleDecl pass -> XRec pass (HsExpr pass)
rd_lhs = LHsExpr GhcRn
lhs
                        , rd_rhs :: forall pass. RuleDecl pass -> XRec pass (HsExpr pass)
rd_rhs = LHsExpr GhcRn
rhs }))
  = do { let ty_bndrs :: [GenLocated SrcSpanAnnA (HsTyVarBndr () GhcRn)]
ty_bndrs = [GenLocated SrcSpanAnnA (HsTyVarBndr () GhcRn)]
-> Maybe [GenLocated SrcSpanAnnA (HsTyVarBndr () GhcRn)]
-> [GenLocated SrcSpanAnnA (HsTyVarBndr () GhcRn)]
forall a. a -> Maybe a -> a
fromMaybe [] Maybe [LHsTyVarBndr () (NoGhcTc GhcRn)]
Maybe [GenLocated SrcSpanAnnA (HsTyVarBndr () GhcRn)]
m_ty_bndrs
       ; rule <- FreshOrReuse
-> [LHsTyVarBndr () GhcRn]
-> (Core [M (TyVarBndr ())] -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
FreshNamesOnly [LHsTyVarBndr () GhcRn]
[GenLocated SrcSpanAnnA (HsTyVarBndr () GhcRn)]
ty_bndrs ((Core [M (TyVarBndr ())] -> MetaM (Core (M Dec)))
 -> MetaM (Core (M Dec)))
-> (Core [M (TyVarBndr ())] -> MetaM (Core (M Dec)))
-> MetaM (Core (M Dec))
forall a b. (a -> b) -> a -> b
$ \ Core [M (TyVarBndr ())]
ex_bndrs ->
         do { let tm_bndr_names :: [Name]
tm_bndr_names = (GenLocated EpAnnCO (RuleBndr GhcRn) -> [Name])
-> [GenLocated EpAnnCO (RuleBndr GhcRn)] -> [Name]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LRuleBndr GhcRn -> [Name]
GenLocated EpAnnCO (RuleBndr GhcRn) -> [Name]
ruleBndrNames [LRuleBndr GhcRn]
[GenLocated EpAnnCO (RuleBndr GhcRn)]
tm_bndrs
            ; ss <- [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
tm_bndr_names
            ; rule <- addBinds ss $
                      do { elt_ty <- wrapName tyVarBndrUnitTyConName
                         ; ty_bndrs' <- return $ case m_ty_bndrs of
                             Maybe [LHsTyVarBndr () (NoGhcTc GhcRn)]
Nothing -> Type -> Core (Maybe [M (TyVarBndr ())])
forall a. Type -> Core (Maybe a)
coreNothing' (Type -> Type
mkListTy Type
elt_ty)
                             Just [LHsTyVarBndr () (NoGhcTc GhcRn)]
_  -> Type -> Core [M (TyVarBndr ())] -> Core (Maybe [M (TyVarBndr ())])
forall a. Type -> Core a -> Core (Maybe a)
coreJust' (Type -> Type
mkListTy Type
elt_ty) Core [M (TyVarBndr ())]
ex_bndrs
                         ; tm_bndrs' <- repListM ruleBndrTyConName
                                                repRuleBndr
                                                tm_bndrs
                         ; n'   <- coreStringLit $ unLoc n
                         ; act' <- repPhases act
                         ; lhs' <- repLE lhs
                         ; rhs' <- repLE rhs
                         ; repPragRule n' ty_bndrs' tm_bndrs' lhs' rhs' act' }
           ; wrapGenSyms ss rule  }
       ; return (locA loc, rule) }

ruleBndrNames :: LRuleBndr GhcRn -> [Name]
ruleBndrNames :: LRuleBndr GhcRn -> [Name]
ruleBndrNames (L EpAnnCO
_ (RuleBndr XCRuleBndr GhcRn
_ LIdP GhcRn
n))      = [GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n]
ruleBndrNames (L EpAnnCO
_ (RuleBndrSig XRuleBndrSig GhcRn
_ LIdP GhcRn
n HsPatSigType GhcRn
sig))
  | HsPS { hsps_ext :: forall pass. HsPatSigType pass -> XHsPS pass
hsps_ext = HsPSRn { hsps_imp_tvs :: HsPSRn -> [Name]
hsps_imp_tvs = [Name]
vars }} <- HsPatSigType GhcRn
sig
  = GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
vars

repRuleBndr :: LRuleBndr GhcRn -> MetaM (Core (M TH.RuleBndr))
repRuleBndr :: LRuleBndr GhcRn -> MetaM (Core (M RuleBndr))
repRuleBndr (L EpAnnCO
_ (RuleBndr XCRuleBndr GhcRn
_ LIdP GhcRn
n))
  = do { MkC n' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
lookupNBinder LIdP GhcRn
GenLocated SrcSpanAnnN Name
n
       ; rep2 ruleVarName [n'] }
repRuleBndr (L EpAnnCO
_ (RuleBndrSig XRuleBndrSig GhcRn
_ LIdP GhcRn
n HsPatSigType GhcRn
sig))
  = do { MkC n'  <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
lookupNBinder LIdP GhcRn
GenLocated SrcSpanAnnN Name
n
       ; MkC ty' <- repLTy (hsPatSigType sig)
       ; rep2 typedRuleVarName [n', ty'] }

repAnnD :: LAnnDecl GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
repAnnD :: LAnnDecl GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
repAnnD (L SrcSpanAnnA
loc (HsAnnotation XHsAnnotation GhcRn
_ AnnProvenance GhcRn
ann_prov (L SrcSpanAnnA
_ HsExpr GhcRn
exp)))
  = do { target <- AnnProvenance GhcRn -> MetaM (Core AnnTarget)
repAnnProv AnnProvenance GhcRn
ann_prov
       ; exp'   <- repE exp
       ; dec    <- repPragAnn target exp'
       ; return (locA loc, dec) }

repAnnProv :: AnnProvenance GhcRn -> MetaM (Core TH.AnnTarget)
repAnnProv :: AnnProvenance GhcRn -> MetaM (Core AnnTarget)
repAnnProv (ValueAnnProvenance LIdP GhcRn
n)
  = do { -- An ANN references an identifier bound elsewhere in the module, so
         -- we must look it up using lookupLOcc (#19377).
         -- Similarly for TypeAnnProvenance (`ANN type`) below.
         MkC n' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n
       ; rep2_nw valueAnnotationName [ n' ] }
repAnnProv (TypeAnnProvenance LIdP GhcRn
n)
  = do { MkC n' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n
       ; rep2_nw typeAnnotationName [ n' ] }
repAnnProv AnnProvenance GhcRn
ModuleAnnProvenance
  = Name -> [CoreExpr] -> MetaM (Core AnnTarget)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
moduleAnnotationName []

-------------------------------------------------------
--                      Constructors
-------------------------------------------------------

repC :: LConDecl GhcRn -> MetaM (Core (M TH.Con))
repC :: LConDecl GhcRn -> MetaM (Core (M Con))
repC (L SrcSpanAnnA
_ (ConDeclH98 { con_name :: forall pass. ConDecl pass -> LIdP pass
con_name   = LIdP GhcRn
con
                      , con_forall :: forall pass. ConDecl pass -> Bool
con_forall = Bool
False
                      , con_mb_cxt :: forall pass. ConDecl pass -> Maybe (LHsContext pass)
con_mb_cxt = Maybe (LHsContext GhcRn)
Nothing
                      , con_args :: forall pass. ConDecl pass -> HsConDeclH98Details pass
con_args   = HsConDeclH98Details GhcRn
args }))
  = GenLocated SrcSpanAnnN Name
-> HsConDeclH98Details GhcRn -> MetaM (Core (M Con))
repH98DataCon LIdP GhcRn
GenLocated SrcSpanAnnN Name
con HsConDeclH98Details GhcRn
args

repC (L SrcSpanAnnA
_ (ConDeclH98 { con_name :: forall pass. ConDecl pass -> LIdP pass
con_name = LIdP GhcRn
con
                      , con_forall :: forall pass. ConDecl pass -> Bool
con_forall = Bool
is_existential
                      , con_ex_tvs :: forall pass. ConDecl pass -> [LHsTyVarBndr Specificity pass]
con_ex_tvs = [LHsTyVarBndr Specificity GhcRn]
con_tvs
                      , con_mb_cxt :: forall pass. ConDecl pass -> Maybe (LHsContext pass)
con_mb_cxt = Maybe (LHsContext GhcRn)
mcxt
                      , con_args :: forall pass. ConDecl pass -> HsConDeclH98Details pass
con_args = HsConDeclH98Details GhcRn
args }))
  = FreshOrReuse
-> [LHsTyVarBndr Specificity GhcRn]
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Con)))
-> MetaM (Core (M Con))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
FreshNamesOnly [LHsTyVarBndr Specificity GhcRn]
con_tvs ((Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Con)))
 -> MetaM (Core (M Con)))
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Con)))
-> MetaM (Core (M Con))
forall a b. (a -> b) -> a -> b
$ \ Core [M (TyVarBndr Specificity)]
ex_bndrs ->
         do { c'    <- GenLocated SrcSpanAnnN Name
-> HsConDeclH98Details GhcRn -> MetaM (Core (M Con))
repH98DataCon LIdP GhcRn
GenLocated SrcSpanAnnN Name
con HsConDeclH98Details GhcRn
args
            ; ctxt' <- repMbContext mcxt
            ; if not is_existential && isNothing mcxt
              then return c'
              else rep2 forallCName ([unC ex_bndrs, unC ctxt', unC c'])
            }

repC (L SrcSpanAnnA
_ (ConDeclGADT { con_names :: forall pass. ConDecl pass -> NonEmpty (LIdP pass)
con_names  = NonEmpty (LIdP GhcRn)
cons
                       , con_bndrs :: forall pass. ConDecl pass -> XRec pass (HsOuterSigTyVarBndrs pass)
con_bndrs  = L SrcSpanAnnA
_ HsOuterSigTyVarBndrs GhcRn
outer_bndrs
                       , con_mb_cxt :: forall pass. ConDecl pass -> Maybe (LHsContext pass)
con_mb_cxt = Maybe (LHsContext GhcRn)
mcxt
                       , con_g_args :: forall pass. ConDecl pass -> HsConDeclGADTDetails pass
con_g_args = HsConDeclGADTDetails GhcRn
args
                       , con_res_ty :: forall pass. ConDecl pass -> LHsType pass
con_res_ty = XRec GhcRn (HsType GhcRn)
res_ty }))
  | Bool
null_outer_imp_tvs Bool -> Bool -> Bool
&& Bool
null_outer_exp_tvs
                                 -- No implicit or explicit variables
  , Maybe (LHsContext GhcRn)
Nothing <- Maybe (LHsContext GhcRn)
mcxt              -- No context
                                 -- ==> no need for a forall
  = NonEmpty (GenLocated SrcSpanAnnN Name)
-> HsConDeclGADTDetails GhcRn
-> XRec GhcRn (HsType GhcRn)
-> MetaM (Core (M Con))
repGadtDataCons NonEmpty (LIdP GhcRn)
NonEmpty (GenLocated SrcSpanAnnN Name)
cons HsConDeclGADTDetails GhcRn
args XRec GhcRn (HsType GhcRn)
res_ty

  | Bool
otherwise
  = HsOuterSigTyVarBndrs GhcRn
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Con)))
-> MetaM (Core (M Con))
forall {k} (a :: k).
HsOuterSigTyVarBndrs GhcRn
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsOuterSigTyVarBinds HsOuterSigTyVarBndrs GhcRn
outer_bndrs ((Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Con)))
 -> MetaM (Core (M Con)))
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Con)))
-> MetaM (Core (M Con))
forall a b. (a -> b) -> a -> b
$ \ Core [M (TyVarBndr Specificity)]
outer_bndrs' ->
             -- See Note [Don't quantify implicit type variables in quotes]
    do { c'    <- NonEmpty (GenLocated SrcSpanAnnN Name)
-> HsConDeclGADTDetails GhcRn
-> XRec GhcRn (HsType GhcRn)
-> MetaM (Core (M Con))
repGadtDataCons NonEmpty (LIdP GhcRn)
NonEmpty (GenLocated SrcSpanAnnN Name)
cons HsConDeclGADTDetails GhcRn
args XRec GhcRn (HsType GhcRn)
res_ty
       ; ctxt' <- repMbContext mcxt
       ; if null_outer_exp_tvs && isNothing mcxt
         then return c'
         else rep2 forallCName ([unC outer_bndrs', unC ctxt', unC c']) }
  where
    null_outer_imp_tvs :: Bool
null_outer_imp_tvs = HsOuterSigTyVarBndrs GhcRn -> Bool
nullOuterImplicit HsOuterSigTyVarBndrs GhcRn
outer_bndrs
    null_outer_exp_tvs :: Bool
null_outer_exp_tvs = HsOuterSigTyVarBndrs GhcRn -> Bool
nullOuterExplicit HsOuterSigTyVarBndrs GhcRn
outer_bndrs

repMbContext :: Maybe (LHsContext GhcRn) -> MetaM (Core (M TH.Cxt))
repMbContext :: Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repMbContext Maybe (LHsContext GhcRn)
Nothing          = [XRec GhcRn (HsType GhcRn)] -> MetaM (Core (M Cxt))
repContext []
repMbContext (Just (L SrcSpanAnnC
_ [GenLocated SrcSpanAnnA (HsType GhcRn)]
cxt)) = [XRec GhcRn (HsType GhcRn)] -> MetaM (Core (M Cxt))
repContext [XRec GhcRn (HsType GhcRn)]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
cxt

repSrcUnpackedness :: SrcUnpackedness -> MetaM (Core (M TH.SourceUnpackedness))
repSrcUnpackedness :: SrcUnpackedness -> MetaM (Core (M SourceUnpackedness))
repSrcUnpackedness SrcUnpackedness
SrcUnpack   = Name -> [CoreExpr] -> MetaM (Core (M SourceUnpackedness))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sourceUnpackName         []
repSrcUnpackedness SrcUnpackedness
SrcNoUnpack = Name -> [CoreExpr] -> MetaM (Core (M SourceUnpackedness))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sourceNoUnpackName       []
repSrcUnpackedness SrcUnpackedness
NoSrcUnpack = Name -> [CoreExpr] -> MetaM (Core (M SourceUnpackedness))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
noSourceUnpackednessName []

repSrcStrictness :: SrcStrictness -> MetaM (Core (M TH.SourceStrictness))
repSrcStrictness :: SrcStrictness -> MetaM (Core (M SourceStrictness))
repSrcStrictness SrcStrictness
SrcLazy     = Name -> [CoreExpr] -> MetaM (Core (M SourceStrictness))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sourceLazyName         []
repSrcStrictness SrcStrictness
SrcStrict   = Name -> [CoreExpr] -> MetaM (Core (M SourceStrictness))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sourceStrictName       []
repSrcStrictness SrcStrictness
NoSrcStrict = Name -> [CoreExpr] -> MetaM (Core (M SourceStrictness))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
noSourceStrictnessName []

repBangTy :: LBangType GhcRn -> MetaM (Core (M TH.BangType))
repBangTy :: XRec GhcRn (HsType GhcRn) -> MetaM (Core (M BangType))
repBangTy XRec GhcRn (HsType GhcRn)
ty = do
  MkC u <- SrcUnpackedness -> MetaM (Core (M SourceUnpackedness))
repSrcUnpackedness SrcUnpackedness
su'
  MkC s <- repSrcStrictness ss'
  MkC b <- rep2 bangName [u, s]
  MkC t <- repLTy ty'
  rep2 bangTypeName [b, t]
  where
    (SrcUnpackedness
su', SrcStrictness
ss', GenLocated SrcSpanAnnA (HsType GhcRn)
ty') = case GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
forall l e. GenLocated l e -> e
unLoc XRec GhcRn (HsType GhcRn)
GenLocated SrcSpanAnnA (HsType GhcRn)
ty of
            HsBangTy XBangTy GhcRn
_ (HsSrcBang SourceText
_ SrcUnpackedness
su SrcStrictness
ss) XRec GhcRn (HsType GhcRn)
ty -> (SrcUnpackedness
su, SrcStrictness
ss, XRec GhcRn (HsType GhcRn)
GenLocated SrcSpanAnnA (HsType GhcRn)
ty)
            HsType GhcRn
_ -> (SrcUnpackedness
NoSrcUnpack, SrcStrictness
NoSrcStrict, XRec GhcRn (HsType GhcRn)
GenLocated SrcSpanAnnA (HsType GhcRn)
ty)

-------------------------------------------------------
--                      Deriving clauses
-------------------------------------------------------

repDerivs :: HsDeriving GhcRn -> MetaM (Core [M TH.DerivClause])
repDerivs :: HsDeriving GhcRn -> MetaM (Core [M DerivClause])
repDerivs HsDeriving GhcRn
clauses
  = Name
-> (GenLocated EpAnnCO (HsDerivingClause GhcRn)
    -> MetaM (Core (M DerivClause)))
-> [GenLocated EpAnnCO (HsDerivingClause GhcRn)]
-> MetaM (Core [M DerivClause])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
derivClauseTyConName LHsDerivingClause GhcRn -> MetaM (Core (M DerivClause))
GenLocated EpAnnCO (HsDerivingClause GhcRn)
-> MetaM (Core (M DerivClause))
repDerivClause HsDeriving GhcRn
[GenLocated EpAnnCO (HsDerivingClause GhcRn)]
clauses

repDerivClause :: LHsDerivingClause GhcRn
               -> MetaM (Core (M TH.DerivClause))
repDerivClause :: LHsDerivingClause GhcRn -> MetaM (Core (M DerivClause))
repDerivClause (L EpAnnCO
_ (HsDerivingClause
                          { deriv_clause_strategy :: forall pass. HsDerivingClause pass -> Maybe (LDerivStrategy pass)
deriv_clause_strategy = Maybe (LDerivStrategy GhcRn)
dcs
                          , deriv_clause_tys :: forall pass. HsDerivingClause pass -> LDerivClauseTys pass
deriv_clause_tys      = LDerivClauseTys GhcRn
dct }))
  = Maybe (LDerivStrategy GhcRn)
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M DerivClause)))
-> MetaM (Core (M DerivClause))
forall {k} (a :: k).
Maybe (LDerivStrategy GhcRn)
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> MetaM (Core (M a))
repDerivStrategy Maybe (LDerivStrategy GhcRn)
dcs ((Core (Maybe (M DerivStrategy)) -> MetaM (Core (M DerivClause)))
 -> MetaM (Core (M DerivClause)))
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M DerivClause)))
-> MetaM (Core (M DerivClause))
forall a b. (a -> b) -> a -> b
$ \(MkC CoreExpr
dcs') ->
    do MkC dct' <- LDerivClauseTys GhcRn -> MetaM (Core [M Type])
rep_deriv_clause_tys LDerivClauseTys GhcRn
dct
       rep2 derivClauseName [dcs',dct']
  where
    rep_deriv_clause_tys :: LDerivClauseTys GhcRn -> MetaM (Core [M TH.Type])
    rep_deriv_clause_tys :: LDerivClauseTys GhcRn -> MetaM (Core [M Type])
rep_deriv_clause_tys (L SrcSpanAnnC
_ DerivClauseTys GhcRn
dct) = case DerivClauseTys GhcRn
dct of
      DctSingle XDctSingle GhcRn
_ LHsSigType GhcRn
ty -> [LHsSigType GhcRn] -> MetaM (Core [M Type])
rep_deriv_tys [LHsSigType GhcRn
ty]
      DctMulti XDctMulti GhcRn
_ [LHsSigType GhcRn]
tys -> [LHsSigType GhcRn] -> MetaM (Core [M Type])
rep_deriv_tys [LHsSigType GhcRn]
tys

    rep_deriv_tys :: [LHsSigType GhcRn] -> MetaM (Core [M TH.Type])
    rep_deriv_tys :: [LHsSigType GhcRn] -> MetaM (Core [M Type])
rep_deriv_tys = Name
-> (GenLocated SrcSpanAnnA (HsSigType GhcRn)
    -> MetaM (Core (M Type)))
-> [GenLocated SrcSpanAnnA (HsSigType GhcRn)]
-> MetaM (Core [M Type])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
typeTyConName LHsSigType GhcRn -> MetaM (Core (M Type))
GenLocated SrcSpanAnnA (HsSigType GhcRn) -> MetaM (Core (M Type))
repHsSigType

rep_meth_sigs_binds :: [LSig GhcRn] -> LHsBinds GhcRn
                    -> MetaM ([GenSymBind], [Core (M TH.Dec)])
-- Represent signatures and methods in class/instance declarations.
-- See Note [Scoped type variables in quotes]
--
-- Why not use 'repBinds': we have already created symbols for methods in
-- 'repTopDs' via 'hsGroupBinders'. However in 'repBinds', we recreate
-- these fun_id via 'collectHsValBinders decs', which would lead to the
-- instance declarations failing in TH.
rep_meth_sigs_binds :: [LSig GhcRn]
-> LHsBindsLR GhcRn GhcRn -> MetaM ([GenSymBind], [Core (M Dec)])
rep_meth_sigs_binds [LSig GhcRn]
sigs LHsBindsLR GhcRn GhcRn
binds
  = do { let tvs :: [Name]
tvs = (GenLocated SrcSpanAnnA (Sig GhcRn) -> [Name])
-> [GenLocated SrcSpanAnnA (Sig GhcRn)] -> [Name]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LSig GhcRn -> [Name]
GenLocated SrcSpanAnnA (Sig GhcRn) -> [Name]
get_scoped_tvs [LSig GhcRn]
[GenLocated SrcSpanAnnA (Sig GhcRn)]
sigs
       ; ss <- [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
tvs
       ; sigs1 <- addBinds ss $ rep_sigs sigs
       ; binds1 <- addBinds ss $ rep_binds binds
       ; return (ss, de_loc (sort_by_loc (sigs1 ++ binds1))) }

-------------------------------------------------------
--   Signatures in a class decl, or a group of bindings
-------------------------------------------------------

rep_sigs :: [LSig GhcRn] -> MetaM [(SrcSpan, Core (M TH.Dec))]
        -- We silently ignore ones we don't recognise
rep_sigs :: [LSig GhcRn] -> MetaM [(SrcSpan, Core (M Dec))]
rep_sigs = (GenLocated SrcSpanAnnA (Sig GhcRn)
 -> MetaM [(SrcSpan, Core (M Dec))])
-> [GenLocated SrcSpanAnnA (Sig GhcRn)]
-> MetaM [(SrcSpan, Core (M Dec))]
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable f) =>
(a -> m [b]) -> f a -> m [b]
concatMapM LSig GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
GenLocated SrcSpanAnnA (Sig GhcRn)
-> MetaM [(SrcSpan, Core (M Dec))]
rep_sig

rep_sig :: LSig GhcRn -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_sig :: LSig GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_sig (L SrcSpanAnnA
loc (TypeSig XTypeSig GhcRn
_ [LIdP GhcRn]
nms LHsSigWcType GhcRn
ty))
  = (GenLocated SrcSpanAnnN Name
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnN Name] -> MetaM [(SrcSpan, Core (M Dec))]
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 (Name
-> SrcSpan
-> LHsSigWcType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_wc_ty_sig Name
sigDName (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) LHsSigWcType GhcRn
ty) [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
nms
rep_sig (L SrcSpanAnnA
loc (PatSynSig XPatSynSig GhcRn
_ [LIdP GhcRn]
nms LHsSigType GhcRn
ty))
  = (GenLocated SrcSpanAnnN Name
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnN Name] -> MetaM [(SrcSpan, Core (M Dec))]
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 (SrcSpan
-> LHsSigType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_patsyn_ty_sig (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) LHsSigType GhcRn
ty) [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
nms
rep_sig (L SrcSpanAnnA
loc (ClassOpSig XClassOpSig GhcRn
_ Bool
is_deflt [LIdP GhcRn]
nms LHsSigType GhcRn
ty))
  | Bool
is_deflt     = (GenLocated SrcSpanAnnN Name
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnN Name] -> MetaM [(SrcSpan, Core (M Dec))]
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 (Name
-> SrcSpan
-> LHsSigType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_ty_sig Name
defaultSigDName (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) LHsSigType GhcRn
ty) [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
nms
  | Bool
otherwise    = (GenLocated SrcSpanAnnN Name
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnN Name] -> MetaM [(SrcSpan, Core (M Dec))]
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 (Name
-> SrcSpan
-> LHsSigType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_ty_sig Name
sigDName (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) LHsSigType GhcRn
ty) [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
nms
rep_sig (L SrcSpanAnnA
loc (FixSig XFixSig GhcRn
_ FixitySig GhcRn
fix_sig))   = SrcSpan -> FixitySig GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_fix_d (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc) FixitySig GhcRn
fix_sig
rep_sig (L SrcSpanAnnA
loc (InlineSig XInlineSig GhcRn
_ LIdP GhcRn
nm InlinePragma
ispec))= GenLocated SrcSpanAnnN Name
-> InlinePragma -> SrcSpan -> MetaM [(SrcSpan, Core (M Dec))]
rep_inline LIdP GhcRn
GenLocated SrcSpanAnnN Name
nm InlinePragma
ispec (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)
rep_sig (L SrcSpanAnnA
loc (SpecSig XSpecSig GhcRn
_ LIdP GhcRn
nm [LHsSigType GhcRn]
tys InlinePragma
ispec))
  = (GenLocated SrcSpanAnnA (HsSigType GhcRn)
 -> MetaM [(SrcSpan, Core (M Dec))])
-> [GenLocated SrcSpanAnnA (HsSigType GhcRn)]
-> MetaM [(SrcSpan, Core (M Dec))]
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable f) =>
(a -> m [b]) -> f a -> m [b]
concatMapM (\GenLocated SrcSpanAnnA (HsSigType GhcRn)
t -> GenLocated SrcSpanAnnN Name
-> LHsSigType GhcRn
-> InlinePragma
-> SrcSpan
-> MetaM [(SrcSpan, Core (M Dec))]
rep_specialise LIdP GhcRn
GenLocated SrcSpanAnnN Name
nm LHsSigType GhcRn
GenLocated SrcSpanAnnA (HsSigType GhcRn)
t InlinePragma
ispec (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)) [LHsSigType GhcRn]
[GenLocated SrcSpanAnnA (HsSigType GhcRn)]
tys
rep_sig (L SrcSpanAnnA
loc (SpecInstSig XSpecInstSig GhcRn
_ LHsSigType GhcRn
ty))  = LHsSigType GhcRn -> SrcSpan -> MetaM [(SrcSpan, Core (M Dec))]
rep_specialiseInst LHsSigType GhcRn
ty (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)
rep_sig (L SrcSpanAnnA
_   (MinimalSig {}))       = ThRejectionReason -> MetaM [(SrcSpan, Core (M Dec))]
forall a. ThRejectionReason -> MetaM a
notHandled ThRejectionReason
ThMinimalPragmas
rep_sig (L SrcSpanAnnA
loc (SCCFunSig XSCCFunSig GhcRn
_ LIdP GhcRn
nm Maybe (XRec GhcRn StringLiteral)
str)) = GenLocated SrcSpanAnnN Name
-> Maybe (XRec GhcRn StringLiteral)
-> SrcSpan
-> MetaM [(SrcSpan, Core (M Dec))]
rep_sccFun LIdP GhcRn
GenLocated SrcSpanAnnN Name
nm Maybe (XRec GhcRn StringLiteral)
str (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)
rep_sig (L SrcSpanAnnA
loc (CompleteMatchSig XCompleteMatchSig GhcRn
_ [LIdP GhcRn]
cls Maybe (LIdP GhcRn)
mty))
  = [GenLocated SrcSpanAnnN Name]
-> Maybe (GenLocated SrcSpanAnnN Name)
-> SrcSpan
-> MetaM [(SrcSpan, Core (M Dec))]
rep_complete_sig [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
cls Maybe (LIdP GhcRn)
Maybe (GenLocated SrcSpanAnnN Name)
mty (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)
rep_sig d :: LSig GhcRn
d@(L SrcSpanAnnA
_ (XSig {}))             = String -> SDoc -> MetaM [(SrcSpan, Core (M Dec))]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rep_sig IdSig" (GenLocated SrcSpanAnnA (Sig GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LSig GhcRn
GenLocated SrcSpanAnnA (Sig GhcRn)
d)

-- Desugar the explicit type variable binders in an 'LHsSigType', making
-- sure not to gensym them.
-- See Note [Scoped type variables in quotes]
-- and Note [Don't quantify implicit type variables in quotes]
rep_ty_sig_tvs :: [LHsTyVarBndr Specificity GhcRn]
               -> MetaM (Core [M TH.TyVarBndrSpec])
rep_ty_sig_tvs :: [LHsTyVarBndr Specificity GhcRn]
-> MetaM (Core [M (TyVarBndr Specificity)])
rep_ty_sig_tvs [LHsTyVarBndr Specificity GhcRn]
explicit_tvs
  = Name
-> (GenLocated SrcSpanAnnA (HsTyVarBndr Specificity GhcRn)
    -> MetaM (Core (M (TyVarBndr Specificity))))
-> [GenLocated SrcSpanAnnA (HsTyVarBndr Specificity GhcRn)]
-> MetaM (Core [M (TyVarBndr Specificity)])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
tyVarBndrSpecTyConName LHsTyVarBndr Specificity GhcRn
-> MetaM (Core (M (TyVarBndr Specificity)))
GenLocated SrcSpanAnnA (HsTyVarBndr Specificity GhcRn)
-> MetaM (Core (M (TyVarBndr Specificity)))
forall flag flag'.
RepTV flag flag' =>
LHsTyVarBndr flag GhcRn -> MetaM (Core (M (TyVarBndr flag')))
repTyVarBndr
             [LHsTyVarBndr Specificity GhcRn]
[GenLocated SrcSpanAnnA (HsTyVarBndr Specificity GhcRn)]
explicit_tvs

-- Desugar the outer type variable binders in an 'LHsSigType', making
-- sure not to gensym them.
-- See Note [Scoped type variables in quotes]
-- and Note [Don't quantify implicit type variables in quotes]
rep_ty_sig_outer_tvs :: HsOuterSigTyVarBndrs GhcRn
                     -> MetaM (Core [M TH.TyVarBndrSpec])
rep_ty_sig_outer_tvs :: HsOuterSigTyVarBndrs GhcRn
-> MetaM (Core [M (TyVarBndr Specificity)])
rep_ty_sig_outer_tvs (HsOuterImplicit{}) =
  Name
-> [Core (M (TyVarBndr Specificity))]
-> MetaM (Core [M (TyVarBndr Specificity)])
forall a. Name -> [Core a] -> MetaM (Core [a])
coreListM Name
tyVarBndrSpecTyConName []
rep_ty_sig_outer_tvs (HsOuterExplicit{hso_bndrs :: forall flag pass.
HsOuterTyVarBndrs flag pass -> [LHsTyVarBndr flag (NoGhcTc pass)]
hso_bndrs = [LHsTyVarBndr Specificity (NoGhcTc GhcRn)]
explicit_tvs}) =
  [LHsTyVarBndr Specificity GhcRn]
-> MetaM (Core [M (TyVarBndr Specificity)])
rep_ty_sig_tvs [LHsTyVarBndr Specificity (NoGhcTc GhcRn)]
[LHsTyVarBndr Specificity GhcRn]
explicit_tvs

-- Desugar a top-level type signature. Unlike 'repHsSigType', this
-- deliberately avoids gensymming the type variables.
-- See Note [Scoped type variables in quotes]
-- and Note [Don't quantify implicit type variables in quotes]
rep_ty_sig :: Name -> SrcSpan -> LHsSigType GhcRn -> LocatedN Name
           -> MetaM (SrcSpan, Core (M TH.Dec))
rep_ty_sig :: Name
-> SrcSpan
-> LHsSigType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_ty_sig Name
mk_sig SrcSpan
loc LHsSigType GhcRn
sig_ty GenLocated SrcSpanAnnN Name
nm
  = do { nm1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
       ; ty1 <- rep_ty_sig' sig_ty
       ; sig <- repProto mk_sig nm1 ty1
       ; return (loc, sig) }

-- Desugar an 'LHsSigType', making sure not to gensym the type variables at
-- the front of the type signature.
-- See Note [Scoped type variables in quotes]
-- and Note [Don't quantify implicit type variables in quotes]
rep_ty_sig' :: LHsSigType GhcRn
            -> MetaM (Core (M TH.Type))
rep_ty_sig' :: LHsSigType GhcRn -> MetaM (Core (M Type))
rep_ty_sig' (L SrcSpanAnnA
_ (HsSig{sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcRn
outer_bndrs, sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body = XRec GhcRn (HsType GhcRn)
body}))
  | (Maybe (LHsContext GhcRn)
ctxt, XRec GhcRn (HsType GhcRn)
tau) <- XRec GhcRn (HsType GhcRn)
-> (Maybe (LHsContext GhcRn), XRec GhcRn (HsType GhcRn))
forall (pass :: Pass).
LHsType (GhcPass pass)
-> (Maybe (LHsContext (GhcPass pass)), LHsType (GhcPass pass))
splitLHsQualTy XRec GhcRn (HsType GhcRn)
body
  = do { th_explicit_tvs <- HsOuterSigTyVarBndrs GhcRn
-> MetaM (Core [M (TyVarBndr Specificity)])
rep_ty_sig_outer_tvs HsOuterSigTyVarBndrs GhcRn
outer_bndrs
       ; th_ctxt <- repLContext ctxt
       ; th_tau  <- repLTy tau
       ; if nullOuterExplicit outer_bndrs && null (fromMaybeContext ctxt)
            then return th_tau
            else repTForall th_explicit_tvs th_ctxt th_tau }

rep_patsyn_ty_sig :: SrcSpan -> LHsSigType GhcRn -> LocatedN Name
                  -> MetaM (SrcSpan, Core (M TH.Dec))
-- represents a pattern synonym type signature;
-- see Note [Pattern synonym type signatures and Template Haskell] in "GHC.ThToHs"
--
-- Don't create the implicit and explicit variables when desugaring signatures,
-- see Note [Scoped type variables in quotes]
-- and Note [Don't quantify implicit type variables in quotes]
rep_patsyn_ty_sig :: SrcSpan
-> LHsSigType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_patsyn_ty_sig SrcSpan
loc LHsSigType GhcRn
sig_ty GenLocated SrcSpanAnnN Name
nm
  | ([LHsTyVarBndr Specificity (GhcPass (NoGhcTcPass 'Renamed))]
univs, Maybe (LHsContext GhcRn)
reqs, [LHsTyVarBndr Specificity GhcRn]
exis, Maybe (LHsContext GhcRn)
provs, XRec GhcRn (HsType GhcRn)
ty) <- LHsSigType GhcRn
-> ([LHsTyVarBndr Specificity (GhcPass (NoGhcTcPass 'Renamed))],
    Maybe (LHsContext GhcRn), [LHsTyVarBndr Specificity GhcRn],
    Maybe (LHsContext GhcRn), XRec GhcRn (HsType GhcRn))
forall (p :: Pass).
LHsSigType (GhcPass p)
-> ([LHsTyVarBndr Specificity (GhcPass (NoGhcTcPass p))],
    Maybe (LHsContext (GhcPass p)),
    [LHsTyVarBndr Specificity (GhcPass p)],
    Maybe (LHsContext (GhcPass p)), LHsType (GhcPass p))
splitLHsPatSynTy LHsSigType GhcRn
sig_ty
  = do { nm1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
       ; th_univs <- rep_ty_sig_tvs univs
       ; th_exis  <- rep_ty_sig_tvs exis

       ; th_reqs  <- repLContext reqs
       ; th_provs <- repLContext provs
       ; th_ty    <- repLTy ty
       ; ty1      <- repTForall th_univs th_reqs =<<
                       repTForall th_exis th_provs th_ty
       ; sig      <- repProto patSynSigDName nm1 ty1
       ; return (loc, sig) }

rep_wc_ty_sig :: Name -> SrcSpan -> LHsSigWcType GhcRn -> LocatedN Name
              -> MetaM (SrcSpan, Core (M TH.Dec))
rep_wc_ty_sig :: Name
-> SrcSpan
-> LHsSigWcType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_wc_ty_sig Name
mk_sig SrcSpan
loc LHsSigWcType GhcRn
sig_ty GenLocated SrcSpanAnnN Name
nm
  = Name
-> SrcSpan
-> LHsSigType GhcRn
-> GenLocated SrcSpanAnnN Name
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_ty_sig Name
mk_sig SrcSpan
loc (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn))
-> GenLocated SrcSpanAnnA (HsSigType GhcRn)
forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body LHsSigWcType GhcRn
HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn))
sig_ty) GenLocated SrcSpanAnnN Name
nm

rep_inline :: LocatedN Name
           -> InlinePragma      -- Never defaultInlinePragma
           -> SrcSpan
           -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_inline :: GenLocated SrcSpanAnnN Name
-> InlinePragma -> SrcSpan -> MetaM [(SrcSpan, Core (M Dec))]
rep_inline GenLocated SrcSpanAnnN Name
nm InlinePragma
ispec SrcSpan
loc
  | Opaque {} <- InlinePragma -> InlineSpec
inl_inline InlinePragma
ispec
  = do { nm1    <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
       ; opq <- repPragOpaque nm1
       ; return [(loc, opq)]
       }

rep_inline GenLocated SrcSpanAnnN Name
nm InlinePragma
ispec SrcSpan
loc
  = do { nm1    <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
       ; inline <- repInline $ inl_inline ispec
       ; rm     <- repRuleMatch $ inl_rule ispec
       ; phases <- repPhases $ inl_act ispec
       ; pragma <- repPragInl nm1 inline rm phases
       ; return [(loc, pragma)]
       }

rep_specialise :: LocatedN Name -> LHsSigType GhcRn -> InlinePragma
               -> SrcSpan
               -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_specialise :: GenLocated SrcSpanAnnN Name
-> LHsSigType GhcRn
-> InlinePragma
-> SrcSpan
-> MetaM [(SrcSpan, Core (M Dec))]
rep_specialise GenLocated SrcSpanAnnN Name
nm LHsSigType GhcRn
ty InlinePragma
ispec SrcSpan
loc
  = do { nm1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
       ; ty1 <- repHsSigType ty
       ; phases <- repPhases $ inl_act ispec
       ; let inline = InlinePragma -> InlineSpec
inl_inline InlinePragma
ispec
       ; pragma <- if noUserInlineSpec inline
                   then -- SPECIALISE
                     repPragSpec nm1 ty1 phases
                   else -- SPECIALISE INLINE
                     do { inline1 <- repInline inline
                        ; repPragSpecInl nm1 ty1 inline1 phases }
       ; return [(loc, pragma)]
       }

rep_specialiseInst :: LHsSigType GhcRn -> SrcSpan
                   -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_specialiseInst :: LHsSigType GhcRn -> SrcSpan -> MetaM [(SrcSpan, Core (M Dec))]
rep_specialiseInst LHsSigType GhcRn
ty SrcSpan
loc
  = do { ty1    <- LHsSigType GhcRn -> MetaM (Core (M Type))
repHsSigType LHsSigType GhcRn
ty
       ; pragma <- repPragSpecInst ty1
       ; return [(loc, pragma)] }

rep_sccFun :: LocatedN Name
        -> Maybe (XRec GhcRn StringLiteral)
        -> SrcSpan
        -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_sccFun :: GenLocated SrcSpanAnnN Name
-> Maybe (XRec GhcRn StringLiteral)
-> SrcSpan
-> MetaM [(SrcSpan, Core (M Dec))]
rep_sccFun GenLocated SrcSpanAnnN Name
nm Maybe (XRec GhcRn StringLiteral)
Nothing SrcSpan
loc = do
  nm1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
  scc <- repPragSCCFun nm1
  return [(loc, scc)]

rep_sccFun GenLocated SrcSpanAnnN Name
nm (Just (L EpAnnCO
_ StringLiteral
str)) SrcSpan
loc = do
  nm1 <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
nm
  str1 <- coreStringLit (sl_fs str)
  scc <- repPragSCCFunNamed nm1 str1
  return [(loc, scc)]

repInline :: InlineSpec -> MetaM (Core TH.Inline)
repInline :: InlineSpec -> MetaM (Core Inline)
repInline (NoInline          SourceText
_ )   = Name -> MetaM (Core Inline)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
noInlineDataConName
-- There is a mismatch between the TH and GHC representation because
-- OPAQUE pragmas can't have phase activation annotations (which is
-- enforced by the TH API), therefore they are desugared to OpaqueP rather than
-- InlineP, see special case in rep_inline.
repInline (Opaque            SourceText
_ )   = String -> MetaM (Core Inline)
forall a. HasCallStack => String -> a
panic String
"repInline: Opaque"
repInline (Inline            SourceText
_ )   = Name -> MetaM (Core Inline)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
inlineDataConName
repInline (Inlinable         SourceText
_ )   = Name -> MetaM (Core Inline)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
inlinableDataConName
repInline InlineSpec
NoUserInlinePrag        = ThRejectionReason -> MetaM (Core Inline)
forall a. ThRejectionReason -> MetaM a
notHandled ThRejectionReason
ThNoUserInline

repRuleMatch :: RuleMatchInfo -> MetaM (Core TH.RuleMatch)
repRuleMatch :: RuleMatchInfo -> MetaM (Core RuleMatch)
repRuleMatch RuleMatchInfo
ConLike = Name -> MetaM (Core RuleMatch)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
conLikeDataConName
repRuleMatch RuleMatchInfo
FunLike = Name -> MetaM (Core RuleMatch)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
funLikeDataConName

repPhases :: Activation -> MetaM (Core TH.Phases)
repPhases :: Activation -> MetaM (Core Phases)
repPhases (ActiveBefore SourceText
_ Int
i) = do { MkC arg <- Int -> MetaM (Core Int)
coreIntLit Int
i
                                  ; dataCon' beforePhaseDataConName [arg] }
repPhases (ActiveAfter SourceText
_ Int
i)  = do { MkC arg <- Int -> MetaM (Core Int)
coreIntLit Int
i
                                  ; dataCon' fromPhaseDataConName [arg] }
repPhases Activation
_                  = Name -> MetaM (Core Phases)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
allPhasesDataConName

rep_complete_sig :: [LocatedN Name]
                 -> Maybe (LocatedN Name)
                 -> SrcSpan
                 -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_complete_sig :: [GenLocated SrcSpanAnnN Name]
-> Maybe (GenLocated SrcSpanAnnN Name)
-> SrcSpan
-> MetaM [(SrcSpan, Core (M Dec))]
rep_complete_sig [GenLocated SrcSpanAnnN Name]
cls Maybe (GenLocated SrcSpanAnnN Name)
mty SrcSpan
loc
  = do { mty' <- Name
-> (GenLocated SrcSpanAnnN Name -> MetaM (Core Name))
-> Maybe (GenLocated SrcSpanAnnN Name)
-> MetaM (Core (Maybe Name))
forall a b.
Name -> (a -> MetaM (Core b)) -> Maybe a -> MetaM (Core (Maybe b))
repMaybe Name
nameTyConName GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc Maybe (GenLocated SrcSpanAnnN Name)
mty
       ; cls' <- repList nameTyConName lookupLOcc cls
       ; sig <- repPragComplete cls' mty'
       ; return [(loc, sig)] }

-------------------------------------------------------
--                      Types
-------------------------------------------------------

class RepTV flag flag' | flag -> flag' where
    tyVarBndrName :: Name
    repPlainTV  :: Core TH.Name -> flag -> MetaM (Core (M (TH.TyVarBndr flag')))
    repKindedTV :: Core TH.Name -> flag -> Core (M TH.Kind)
                -> MetaM (Core (M (TH.TyVarBndr flag')))

instance RepTV () () where
    tyVarBndrName :: Name
tyVarBndrName = Name
tyVarBndrUnitTyConName
    repPlainTV :: Core Name -> () -> MetaM (Core (M (TyVarBndr ())))
repPlainTV  (MkC CoreExpr
nm) ()          = Name -> [CoreExpr] -> MetaM (Core (M (TyVarBndr ())))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
plainTVName  [CoreExpr
nm]
    repKindedTV :: Core Name -> () -> Core (M Type) -> MetaM (Core (M (TyVarBndr ())))
repKindedTV (MkC CoreExpr
nm) () (MkC CoreExpr
ki) = Name -> [CoreExpr] -> MetaM (Core (M (TyVarBndr ())))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
kindedTVName [CoreExpr
nm, CoreExpr
ki]

instance RepTV Specificity TH.Specificity where
    tyVarBndrName :: Name
tyVarBndrName = Name
tyVarBndrSpecTyConName
    repPlainTV :: Core Name
-> Specificity -> MetaM (Core (M (TyVarBndr Specificity)))
repPlainTV  (MkC CoreExpr
nm) Specificity
spec          = do { (MkC spec') <- Specificity -> MetaM (Core Specificity)
rep_flag Specificity
spec
                                            ; rep2 plainInvisTVName  [nm, spec'] }
    repKindedTV :: Core Name
-> Specificity
-> Core (M Type)
-> MetaM (Core (M (TyVarBndr Specificity)))
repKindedTV (MkC CoreExpr
nm) Specificity
spec (MkC CoreExpr
ki) = do { (MkC spec') <- Specificity -> MetaM (Core Specificity)
rep_flag Specificity
spec
                                            ; rep2 kindedInvisTVName [nm, spec', ki] }

rep_flag :: Specificity -> MetaM (Core TH.Specificity)
rep_flag :: Specificity -> MetaM (Core Specificity)
rep_flag Specificity
SpecifiedSpec = Name -> [CoreExpr] -> MetaM (Core Specificity)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
specifiedSpecName []
rep_flag Specificity
InferredSpec  = Name -> [CoreExpr] -> MetaM (Core Specificity)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
inferredSpecName []

instance RepTV (HsBndrVis GhcRn) TH.BndrVis where
    tyVarBndrName :: Name
tyVarBndrName = Name
tyVarBndrVisTyConName
    repPlainTV :: Core Name
-> HsBndrVis GhcRn -> MetaM (Core (M (TyVarBndr BndrVis)))
repPlainTV  (MkC CoreExpr
nm) HsBndrVis GhcRn
vis          = do { (MkC vis') <- HsBndrVis GhcRn -> MetaM (Core BndrVis)
rep_bndr_vis HsBndrVis GhcRn
vis
                                           ; rep2 plainBndrTVName  [nm, vis'] }
    repKindedTV :: Core Name
-> HsBndrVis GhcRn
-> Core (M Type)
-> MetaM (Core (M (TyVarBndr BndrVis)))
repKindedTV (MkC CoreExpr
nm) HsBndrVis GhcRn
vis (MkC CoreExpr
ki) = do { (MkC vis') <- HsBndrVis GhcRn -> MetaM (Core BndrVis)
rep_bndr_vis HsBndrVis GhcRn
vis
                                           ; rep2 kindedBndrTVName [nm, vis', ki] }

rep_bndr_vis :: HsBndrVis GhcRn -> MetaM (Core TH.BndrVis)
rep_bndr_vis :: HsBndrVis GhcRn -> MetaM (Core BndrVis)
rep_bndr_vis (HsBndrRequired XBndrRequired GhcRn
_)  = Name -> [CoreExpr] -> MetaM (Core BndrVis)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
bndrReqName []
rep_bndr_vis (HsBndrInvisible XBndrInvisible GhcRn
_) = Name -> [CoreExpr] -> MetaM (Core BndrVis)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
bndrInvisName []

addHsOuterFamEqnTyVarBinds ::
     HsOuterFamEqnTyVarBndrs GhcRn
  -> (Core (Maybe [M TH.TyVarBndrUnit]) -> MetaM (Core (M a)))
  -> MetaM (Core (M a))
addHsOuterFamEqnTyVarBinds :: forall {k} (a :: k).
HsOuterFamEqnTyVarBndrs GhcRn
-> (Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsOuterFamEqnTyVarBinds HsOuterFamEqnTyVarBndrs GhcRn
outer_bndrs Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a))
thing_inside = do
  elt_ty <- Name -> MetaM Type
wrapName Name
tyVarBndrUnitTyConName
  case outer_bndrs of
    HsOuterImplicit{hso_ximplicit :: forall flag pass.
HsOuterTyVarBndrs flag pass -> XHsOuterImplicit pass
hso_ximplicit = XHsOuterImplicit GhcRn
imp_tvs} ->
      FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
ReuseBoundNames [Name]
XHsOuterImplicit GhcRn
imp_tvs (MetaM (Core (M a)) -> MetaM (Core (M a)))
-> MetaM (Core (M a)) -> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$
      Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a))
thing_inside (Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a)))
-> Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$ Type -> Core (Maybe [M (TyVarBndr ())])
forall a. Type -> Core (Maybe [a])
coreNothingList Type
elt_ty
    HsOuterExplicit{hso_bndrs :: forall flag pass.
HsOuterTyVarBndrs flag pass -> [LHsTyVarBndr flag (NoGhcTc pass)]
hso_bndrs = [LHsTyVarBndr () (NoGhcTc GhcRn)]
exp_bndrs} ->
      FreshOrReuse
-> [LHsTyVarBndr () GhcRn]
-> (Core [M (TyVarBndr ())] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
FreshNamesOnly [LHsTyVarBndr () (NoGhcTc GhcRn)]
[LHsTyVarBndr () GhcRn]
exp_bndrs ((Core [M (TyVarBndr ())] -> MetaM (Core (M a)))
 -> MetaM (Core (M a)))
-> (Core [M (TyVarBndr ())] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$ \Core [M (TyVarBndr ())]
th_exp_bndrs ->
      Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a))
thing_inside (Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a)))
-> Core (Maybe [M (TyVarBndr ())]) -> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$ Type -> Core [M (TyVarBndr ())] -> Core (Maybe [M (TyVarBndr ())])
forall a. Type -> Core [a] -> Core (Maybe [a])
coreJustList Type
elt_ty Core [M (TyVarBndr ())]
th_exp_bndrs

addHsOuterSigTyVarBinds ::
     HsOuterSigTyVarBndrs GhcRn
  -> (Core [M TH.TyVarBndrSpec] -> MetaM (Core (M a)))
  -> MetaM (Core (M a))
addHsOuterSigTyVarBinds :: forall {k} (a :: k).
HsOuterSigTyVarBndrs GhcRn
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsOuterSigTyVarBinds HsOuterSigTyVarBndrs GhcRn
outer_bndrs Core [M (TyVarBndr Specificity)] -> MetaM (Core (M a))
thing_inside = case HsOuterSigTyVarBndrs GhcRn
outer_bndrs of
  HsOuterImplicit{hso_ximplicit :: forall flag pass.
HsOuterTyVarBndrs flag pass -> XHsOuterImplicit pass
hso_ximplicit = XHsOuterImplicit GhcRn
imp_tvs} ->
    do th_nil <- Name
-> [Core (M (TyVarBndr Specificity))]
-> MetaM (Core [M (TyVarBndr Specificity)])
forall a. Name -> [Core a] -> MetaM (Core [a])
coreListM Name
tyVarBndrSpecTyConName []
       addSimpleTyVarBinds FreshNamesOnly imp_tvs $ thing_inside th_nil
  HsOuterExplicit{hso_bndrs :: forall flag pass.
HsOuterTyVarBndrs flag pass -> [LHsTyVarBndr flag (NoGhcTc pass)]
hso_bndrs = [LHsTyVarBndr Specificity (NoGhcTc GhcRn)]
exp_bndrs} ->
    FreshOrReuse
-> [LHsTyVarBndr Specificity GhcRn]
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
FreshNamesOnly [LHsTyVarBndr Specificity (NoGhcTc GhcRn)]
[LHsTyVarBndr Specificity GhcRn]
exp_bndrs Core [M (TyVarBndr Specificity)] -> MetaM (Core (M a))
thing_inside

-- | If a type implicitly quantifies its outermost type variables, return
-- 'True' if the list of implicitly bound type variables is empty. If a type
-- explicitly quantifies its outermost type variables, always return 'True'.
--
-- This is used in various places to determine if a Template Haskell 'Type'
-- should be headed by a 'ForallT' or not.
nullOuterImplicit :: HsOuterSigTyVarBndrs GhcRn -> Bool
nullOuterImplicit :: HsOuterSigTyVarBndrs GhcRn -> Bool
nullOuterImplicit (HsOuterImplicit{hso_ximplicit :: forall flag pass.
HsOuterTyVarBndrs flag pass -> XHsOuterImplicit pass
hso_ximplicit = XHsOuterImplicit GhcRn
imp_tvs}) = [Name] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
XHsOuterImplicit GhcRn
imp_tvs
nullOuterImplicit (HsOuterExplicit{})                        = Bool
True
  -- Vacuously true, as there is no implicit quantification

-- | If a type explicitly quantifies its outermost type variables, return
-- 'True' if the list of explicitly bound type variables is empty. If a type
-- implicitly quantifies its outermost type variables, always return 'True'.
--
-- This is used in various places to determine if a Template Haskell 'Type'
-- should be headed by a 'ForallT' or not.
nullOuterExplicit :: HsOuterSigTyVarBndrs GhcRn -> Bool
nullOuterExplicit :: HsOuterSigTyVarBndrs GhcRn -> Bool
nullOuterExplicit (HsOuterExplicit{hso_bndrs :: forall flag pass.
HsOuterTyVarBndrs flag pass -> [LHsTyVarBndr flag (NoGhcTc pass)]
hso_bndrs = [LHsTyVarBndr Specificity (NoGhcTc GhcRn)]
exp_bndrs}) = [GenLocated SrcSpanAnnA (HsTyVarBndr Specificity GhcRn)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsTyVarBndr Specificity (NoGhcTc GhcRn)]
[GenLocated SrcSpanAnnA (HsTyVarBndr Specificity GhcRn)]
exp_bndrs
nullOuterExplicit (HsOuterImplicit{})                      = Bool
True
  -- Vacuously true, as there is no outermost explicit quantification

-- Do we want to generate fresh names for type variables
-- or reuse the ones that are already in scope?
data FreshOrReuse
  = FreshNamesOnly
    -- Generate fresh names for all type variables, regardless of existing
    -- variables in the MetaEnv.
    --
    -- This is the default strategy.

  | ReuseBoundNames
    -- Generate fresh names for type variables not in the MetaEnv.
    -- Where a name is already bound in the MetaEnv, use that existing binding;
    -- do not create a new one with a fresh name.
    --
    -- This is the strategy used for data/newtype declarations and type family
    -- instances, so that the nested type variables work right:
    --
    --     class C a where
    --       type W a b
    --     instance C (T a) where
    --       type W (T a) b = blah
    --
    -- The 'a' in the type instance is the one bound by the instance decl
    --
    -- Test cases: TH_reifyExplicitForAllFams T9081 T9199 T10811

mkGenSyms' :: FreshOrReuse -> [Name] -> MetaM [GenSymBind]
mkGenSyms' :: FreshOrReuse -> [Name] -> MetaM [GenSymBind]
mkGenSyms' FreshOrReuse
FreshNamesOnly  [Name]
names = [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
names
mkGenSyms' FreshOrReuse
ReuseBoundNames [Name]
names =
  -- Make fresh names for the ones that are not already in scope
  -- This makes things work for associated types
  do { env <- DsM DsMetaEnv -> ReaderT MetaWrappers DsM DsMetaEnv
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift DsM DsMetaEnv
dsGetMetaEnv
     ; mkGenSyms (filterOut (`elemNameEnv` env) names) }

addSimpleTyVarBinds :: FreshOrReuse
                    -> [Name]             -- the binders to be added
                    -> MetaM (Core (M a)) -- action in the ext env
                    -> MetaM (Core (M a))
addSimpleTyVarBinds :: forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
fresh_or_reuse [Name]
names MetaM (Core (M a))
thing_inside
  = do { fresh_names <- FreshOrReuse -> [Name] -> MetaM [GenSymBind]
mkGenSyms' FreshOrReuse
fresh_or_reuse [Name]
names
       ; term <- addBinds fresh_names thing_inside
       ; wrapGenSyms fresh_names term }

addHsTyVarBinds :: forall flag flag' a. RepTV flag flag'
                => FreshOrReuse
                -> [LHsTyVarBndr flag GhcRn] -- the binders to be added
                -> (Core [(M (TH.TyVarBndr flag'))] -> MetaM (Core (M a))) -- action in the ext env
                -> MetaM (Core (M a))
addHsTyVarBinds :: forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
fresh_or_reuse [LHsTyVarBndr flag GhcRn]
exp_tvs Core [M (TyVarBndr flag')] -> MetaM (Core (M a))
thing_inside
  = do { fresh_exp_names <- FreshOrReuse -> [Name] -> MetaM [GenSymBind]
mkGenSyms' FreshOrReuse
fresh_or_reuse ([LHsTyVarBndr flag GhcRn] -> [IdP GhcRn]
forall flag (p :: Pass).
[LHsTyVarBndr flag (GhcPass p)] -> [IdP (GhcPass p)]
hsLTyVarNames [LHsTyVarBndr flag GhcRn]
exp_tvs)
       ; term <- addBinds fresh_exp_names $
                 do { kbs <- repListM (tyVarBndrName @flag @flag') repTyVarBndr
                                      exp_tvs
                    ; thing_inside kbs }
       ; wrapGenSyms fresh_exp_names term }

addQTyVarBinds :: FreshOrReuse
               -> LHsQTyVars GhcRn -- the binders to be added
               -> (Core [(M (TH.TyVarBndr TH.BndrVis))] -> MetaM (Core (M a))) -- action in the ext env
               -> MetaM (Core (M a))
addQTyVarBinds :: forall {k} (a :: k).
FreshOrReuse
-> LHsQTyVars GhcRn
-> (Core [M (TyVarBndr BndrVis)] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addQTyVarBinds FreshOrReuse
fresh_or_reuse LHsQTyVars GhcRn
qtvs Core [M (TyVarBndr BndrVis)] -> MetaM (Core (M a))
thing_inside =
  let HsQTvs { hsq_ext :: forall pass. LHsQTyVars pass -> XHsQTvs pass
hsq_ext      = XHsQTvs GhcRn
imp_tvs
             , hsq_explicit :: forall pass.
LHsQTyVars pass -> [LHsTyVarBndr (HsBndrVis pass) pass]
hsq_explicit = [LHsTyVarBndr (HsBndrVis GhcRn) GhcRn]
exp_tvs }
        = LHsQTyVars GhcRn
qtvs
  in FreshOrReuse
-> [LHsTyVarBndr (HsBndrVis GhcRn) GhcRn]
-> [Name]
-> (Core [M (TyVarBndr BndrVis)] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> [Name]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addTyVarBinds FreshOrReuse
fresh_or_reuse [LHsTyVarBndr (HsBndrVis GhcRn) GhcRn]
exp_tvs [Name]
XHsQTvs GhcRn
imp_tvs Core [M (TyVarBndr BndrVis)] -> MetaM (Core (M a))
thing_inside

addTyVarBinds :: RepTV flag flag'
              => FreshOrReuse
              -> [LHsTyVarBndr flag GhcRn] -- the binders to be added
              -> [Name]
              -> (Core [(M (TH.TyVarBndr flag'))] -> MetaM (Core (M a))) -- action in the ext env
              -> MetaM (Core (M a))
-- gensym a list of type variables and enter them into the meta environment;
-- the computations passed as the second argument is executed in that extended
-- meta environment and gets the *new* names on Core-level as an argument
addTyVarBinds :: forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> [Name]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addTyVarBinds FreshOrReuse
fresh_or_reuse [LHsTyVarBndr flag GhcRn]
exp_tvs [Name]
imp_tvs Core [M (TyVarBndr flag')] -> MetaM (Core (M a))
thing_inside
  = FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
fresh_or_reuse [Name]
imp_tvs (MetaM (Core (M a)) -> MetaM (Core (M a)))
-> MetaM (Core (M a)) -> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$
    FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
fresh_or_reuse [LHsTyVarBndr flag GhcRn]
exp_tvs ((Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
 -> MetaM (Core (M a)))
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$
    Core [M (TyVarBndr flag')] -> MetaM (Core (M a))
thing_inside

-- | Represent a type variable binder
repTyVarBndr :: RepTV flag flag'
             => LHsTyVarBndr flag GhcRn -> MetaM (Core (M (TH.TyVarBndr flag')))
repTyVarBndr :: forall flag flag'.
RepTV flag flag' =>
LHsTyVarBndr flag GhcRn -> MetaM (Core (M (TyVarBndr flag')))
repTyVarBndr (L SrcSpanAnnA
_ (UserTyVar XUserTyVar GhcRn
_ flag
fl (L SrcSpanAnnN
_ Name
nm)) )
  = do { nm' <- Name -> MetaM (Core Name)
lookupBinder Name
nm
       ; repPlainTV nm' fl }
repTyVarBndr (L SrcSpanAnnA
_ (KindedTyVar XKindedTyVar GhcRn
_ flag
fl (L SrcSpanAnnN
_ Name
nm) XRec GhcRn (HsType GhcRn)
ki))
  = do { nm' <- Name -> MetaM (Core Name)
lookupBinder Name
nm
       ; ki' <- repLTy ki
       ; repKindedTV nm' fl ki' }

-- represent a type context
--
repLContext :: Maybe (LHsContext GhcRn) -> MetaM (Core (M TH.Cxt))
repLContext :: Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
Nothing = [XRec GhcRn (HsType GhcRn)] -> MetaM (Core (M Cxt))
repContext []
repLContext (Just LHsContext GhcRn
ctxt) = [XRec GhcRn (HsType GhcRn)] -> MetaM (Core (M Cxt))
repContext (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
forall l e. GenLocated l e -> e
unLoc LHsContext GhcRn
GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
ctxt)

repContext :: HsContext GhcRn -> MetaM (Core (M TH.Cxt))
repContext :: [XRec GhcRn (HsType GhcRn)] -> MetaM (Core (M Cxt))
repContext [XRec GhcRn (HsType GhcRn)]
ctxt = do preds <- Name
-> (GenLocated SrcSpanAnnA (HsType GhcRn) -> MetaM (Core (M Type)))
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> MetaM (Core [M Type])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
typeTyConName XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
GenLocated SrcSpanAnnA (HsType GhcRn) -> MetaM (Core (M Type))
repLTy [XRec GhcRn (HsType GhcRn)]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
ctxt
                     repCtxt preds

repHsSigType :: LHsSigType GhcRn -> MetaM (Core (M TH.Type))
repHsSigType :: LHsSigType GhcRn -> MetaM (Core (M Type))
repHsSigType (L SrcSpanAnnA
_ (HsSig { sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcRn
outer_bndrs, sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body = XRec GhcRn (HsType GhcRn)
body }))
  | (Maybe (LHsContext GhcRn)
ctxt, XRec GhcRn (HsType GhcRn)
tau) <- XRec GhcRn (HsType GhcRn)
-> (Maybe (LHsContext GhcRn), XRec GhcRn (HsType GhcRn))
forall (pass :: Pass).
LHsType (GhcPass pass)
-> (Maybe (LHsContext (GhcPass pass)), LHsType (GhcPass pass))
splitLHsQualTy XRec GhcRn (HsType GhcRn)
body
  = HsOuterSigTyVarBndrs GhcRn
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Type)))
-> MetaM (Core (M Type))
forall {k} (a :: k).
HsOuterSigTyVarBndrs GhcRn
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsOuterSigTyVarBinds HsOuterSigTyVarBndrs GhcRn
outer_bndrs ((Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Type)))
 -> MetaM (Core (M Type)))
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Type)))
-> MetaM (Core (M Type))
forall a b. (a -> b) -> a -> b
$ \ Core [M (TyVarBndr Specificity)]
th_outer_bndrs ->
    do { th_ctxt <- Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
ctxt
       ; th_tau  <- repLTy tau
       ; if nullOuterExplicit outer_bndrs && null (fromMaybeContext ctxt)
         then pure th_tau
         else repTForall th_outer_bndrs th_ctxt th_tau }

-- yield the representation of a list of types
repLTys :: [LHsType GhcRn] -> MetaM [Core (M TH.Type)]
repLTys :: [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys = (GenLocated SrcSpanAnnA (HsType GhcRn) -> MetaM (Core (M Type)))
-> [GenLocated SrcSpanAnnA (HsType GhcRn)] -> MetaM [Core (M Type)]
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 XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
GenLocated SrcSpanAnnA (HsType GhcRn) -> MetaM (Core (M Type))
repLTy [XRec GhcRn (HsType GhcRn)]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
tys

-- represent a type
repLTy :: LHsType GhcRn -> MetaM (Core (M TH.Type))
repLTy :: XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
ty = HsType GhcRn -> MetaM (Core (M Type))
repTy (GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
forall l e. GenLocated l e -> e
unLoc XRec GhcRn (HsType GhcRn)
GenLocated SrcSpanAnnA (HsType GhcRn)
ty)

-- Desugar a type headed by an invisible forall (e.g., @forall a. a@) or
-- a context (e.g., @Show a => a@) into a ForallT from L.H.TH.Syntax.
-- In other words, the argument to this function is always an
-- @HsForAllTy HsForAllInvis{}@ or @HsQualTy@.
-- Types headed by visible foralls (which are desugared to ForallVisT) are
-- handled separately in repTy.
repForallT :: HsType GhcRn -> MetaM (Core (M TH.Type))
repForallT :: HsType GhcRn -> MetaM (Core (M Type))
repForallT HsType GhcRn
ty
 | ([LHsTyVarBndr Specificity GhcRn]
tvs, Maybe (LHsContext GhcRn)
ctxt, XRec GhcRn (HsType GhcRn)
tau) <- XRec GhcRn (HsType GhcRn)
-> ([LHsTyVarBndr Specificity GhcRn], Maybe (LHsContext GhcRn),
    XRec GhcRn (HsType GhcRn))
forall (p :: Pass).
LHsType (GhcPass p)
-> ([LHsTyVarBndr Specificity (GhcPass p)],
    Maybe (LHsContext (GhcPass p)), LHsType (GhcPass p))
splitLHsSigmaTyInvis (HsType GhcRn -> GenLocated SrcSpanAnnA (HsType GhcRn)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA HsType GhcRn
ty)
 = FreshOrReuse
-> [LHsTyVarBndr Specificity GhcRn]
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Type)))
-> MetaM (Core (M Type))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
FreshNamesOnly [LHsTyVarBndr Specificity GhcRn]
tvs ((Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Type)))
 -> MetaM (Core (M Type)))
-> (Core [M (TyVarBndr Specificity)] -> MetaM (Core (M Type)))
-> MetaM (Core (M Type))
forall a b. (a -> b) -> a -> b
$ \Core [M (TyVarBndr Specificity)]
bndrs ->
   do { ctxt1  <- Maybe (LHsContext GhcRn) -> MetaM (Core (M Cxt))
repLContext Maybe (LHsContext GhcRn)
ctxt
      ; tau1   <- repLTy tau
      ; repTForall bndrs ctxt1 tau1 -- forall a. C a => {...}
      }

repTy :: HsType GhcRn -> MetaM (Core (M TH.Type))
repTy :: HsType GhcRn -> MetaM (Core (M Type))
repTy ty :: HsType GhcRn
ty@(HsForAllTy { hst_tele :: forall pass. HsType pass -> HsForAllTelescope pass
hst_tele = HsForAllTelescope GhcRn
tele, hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = XRec GhcRn (HsType GhcRn)
body }) =
  case HsForAllTelescope GhcRn
tele of
    HsForAllInvis{} -> HsType GhcRn -> MetaM (Core (M Type))
repForallT HsType GhcRn
ty
    HsForAllVis { hsf_vis_bndrs :: forall pass. HsForAllTelescope pass -> [LHsTyVarBndr () pass]
hsf_vis_bndrs = [LHsTyVarBndr () GhcRn]
tvs } ->
      FreshOrReuse
-> [LHsTyVarBndr () GhcRn]
-> (Core [M (TyVarBndr ())] -> MetaM (Core (M Type)))
-> MetaM (Core (M Type))
forall {k} flag flag' (a :: k).
RepTV flag flag' =>
FreshOrReuse
-> [LHsTyVarBndr flag GhcRn]
-> (Core [M (TyVarBndr flag')] -> MetaM (Core (M a)))
-> MetaM (Core (M a))
addHsTyVarBinds FreshOrReuse
FreshNamesOnly [LHsTyVarBndr () GhcRn]
tvs ((Core [M (TyVarBndr ())] -> MetaM (Core (M Type)))
 -> MetaM (Core (M Type)))
-> (Core [M (TyVarBndr ())] -> MetaM (Core (M Type)))
-> MetaM (Core (M Type))
forall a b. (a -> b) -> a -> b
$ \Core [M (TyVarBndr ())]
bndrs ->
      do body1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
body
         repTForallVis bndrs body1
repTy ty :: HsType GhcRn
ty@(HsQualTy {}) = HsType GhcRn -> MetaM (Core (M Type))
repForallT HsType GhcRn
ty

repTy (HsTyVar XTyVar GhcRn
_ PromotionFlag
_ (L SrcSpanAnnN
_ Name
n))
  | Name
n Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
liftedTypeKindTyConKey  = MetaM (Core (M Type))
repTStar
  | Name
n Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
constraintKindTyConKey  = MetaM (Core (M Type))
repTConstraint
  | Name
n Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
unrestrictedFunTyConKey = MetaM (Core (M Type))
repArrowTyCon
  | Name
n Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
fUNTyConKey             = MetaM (Core (M Type))
repMulArrowTyCon
  | Name
n Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqTyConKey              = MetaM (Core (M Type))
repTequality
  | NameSpace -> Bool
isVarNameSpace     NameSpace
ns = do tv1 <- Name -> MetaM (Core Name)
lookupOcc Name
n
                               repTvar tv1
  | NameSpace -> Bool
isDataConNameSpace NameSpace
ns = do dc1 <- Name -> MetaM (Core Name)
lookupOcc Name
n
                               repPromotedDataCon dc1
  | NameSpace -> Bool
isTcClsNameSpace   NameSpace
ns = do tc1 <- Name -> MetaM (Core Name)
lookupOcc Name
n
                               repNamedTyCon tc1
  | Bool
otherwise = String -> MetaM (Core (M Type))
forall a. HasCallStack => String -> a
panic String
"repTy: HsTyVar: unknown namespace"
  where
    occ :: OccName
occ = Name -> OccName
nameOccName Name
n
    ns :: NameSpace
ns  = OccName -> NameSpace
occNameSpace OccName
occ

repTy (HsAppTy XAppTy GhcRn
_ XRec GhcRn (HsType GhcRn)
f XRec GhcRn (HsType GhcRn)
a)       = do
                                f1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
f
                                a1 <- repLTy a
                                repTapp f1 a1
repTy (HsAppKindTy XAppKindTy GhcRn
_ XRec GhcRn (HsType GhcRn)
ty XRec GhcRn (HsType GhcRn)
ki) = do
                                ty1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
ty
                                ki1 <- repLTy ki
                                repTappKind ty1 ki1
repTy (HsFunTy XFunTy GhcRn
_ HsArrow GhcRn
w XRec GhcRn (HsType GhcRn)
f XRec GhcRn (HsType GhcRn)
a) | HsArrow GhcRn -> Bool
isUnrestricted HsArrow GhcRn
w = do
                                f1   <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
f
                                a1   <- repLTy a
                                tcon <- repArrowTyCon
                                repTapps tcon [f1, a1]
repTy (HsFunTy XFunTy GhcRn
_ HsArrow GhcRn
w XRec GhcRn (HsType GhcRn)
f XRec GhcRn (HsType GhcRn)
a) = do w1   <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy (HsArrow GhcRn -> XRec GhcRn (HsType GhcRn)
arrowToHsType HsArrow GhcRn
w)
                             f1   <- repLTy f
                             a1   <- repLTy a
                             tcon <- repMulArrowTyCon
                             repTapps tcon [w1, f1, a1]
repTy (HsListTy XListTy GhcRn
_ XRec GhcRn (HsType GhcRn)
t)        = do
                                t1   <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
t
                                tcon <- repListTyCon
                                repTapp tcon t1
repTy (HsTupleTy XTupleTy GhcRn
_ HsTupleSort
HsUnboxedTuple [XRec GhcRn (HsType GhcRn)]
tys) = do
                                tys1 <- [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys
                                tcon <- repUnboxedTupleTyCon (length tys)
                                repTapps tcon tys1
repTy (HsTupleTy XTupleTy GhcRn
_ HsTupleSort
_ [XRec GhcRn (HsType GhcRn)]
tys)   = do tys1 <- [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys
                                 tcon <- repTupleTyCon (length tys)
                                 repTapps tcon tys1
repTy (HsSumTy XSumTy GhcRn
_ [XRec GhcRn (HsType GhcRn)]
tys)       = do tys1 <- [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys
                                 tcon <- repUnboxedSumTyCon (length tys)
                                 repTapps tcon tys1
repTy (HsOpTy XOpTy GhcRn
_ PromotionFlag
prom XRec GhcRn (HsType GhcRn)
ty1 LIdP GhcRn
n XRec GhcRn (HsType GhcRn)
ty2) = XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy ((PromotionFlag -> IdP GhcRn -> XRec GhcRn (HsType GhcRn)
forall (p :: Pass) a.
IsSrcSpanAnn p a =>
PromotionFlag -> IdP (GhcPass p) -> LHsType (GhcPass p)
nlHsTyVar PromotionFlag
prom (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n) XRec GhcRn (HsType GhcRn)
-> XRec GhcRn (HsType GhcRn) -> XRec GhcRn (HsType GhcRn)
forall (p :: Pass).
LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
`nlHsAppTy` XRec GhcRn (HsType GhcRn)
ty1)
                                   XRec GhcRn (HsType GhcRn)
-> XRec GhcRn (HsType GhcRn) -> XRec GhcRn (HsType GhcRn)
forall (p :: Pass).
LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
`nlHsAppTy` XRec GhcRn (HsType GhcRn)
ty2)
repTy (HsParTy XParTy GhcRn
_ XRec GhcRn (HsType GhcRn)
t)         = XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
t
repTy (HsStarTy XStarTy GhcRn
_ Bool
_) =  MetaM (Core (M Type))
repTStar
repTy (HsKindSig XKindSig GhcRn
_ XRec GhcRn (HsType GhcRn)
t XRec GhcRn (HsType GhcRn)
k)     = do
                                t1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy XRec GhcRn (HsType GhcRn)
t
                                k1 <- repLTy k
                                repTSig t1 k1
repTy (HsSpliceTy (HsUntypedSpliceNested Name
n) HsUntypedSplice GhcRn
_) = Name -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> MetaM (Core a)
rep_splice Name
n
repTy t :: HsType GhcRn
t@(HsSpliceTy (HsUntypedSpliceTop ThModFinalizers
_ GenLocated SrcSpanAnnA (HsType GhcRn)
_) HsUntypedSplice GhcRn
_) = String -> SDoc -> MetaM (Core (M Type))
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"repTy: top level splice" (HsType GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsType GhcRn
t)
repTy (HsExplicitListTy XExplicitListTy GhcRn
_ PromotionFlag
_ [XRec GhcRn (HsType GhcRn)]
tys) = do
                                    tys1 <- [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys
                                    repTPromotedList tys1
repTy (HsExplicitTupleTy XExplicitTupleTy GhcRn
_ [XRec GhcRn (HsType GhcRn)]
tys) = do
                                    tys1 <- [XRec GhcRn (HsType GhcRn)] -> MetaM [Core (M Type)]
repLTys [XRec GhcRn (HsType GhcRn)]
tys
                                    tcon <- repPromotedTupleTyCon (length tys)
                                    repTapps tcon tys1
repTy (HsTyLit XTyLit GhcRn
_ HsTyLit GhcRn
lit) = do
                          lit' <- HsTyLit GhcRn -> MetaM (Core (M TyLit))
forall (p :: Pass). HsTyLit (GhcPass p) -> MetaM (Core (M TyLit))
repTyLit HsTyLit GhcRn
lit
                          repTLit lit'
repTy (HsWildCardTy XWildCardTy GhcRn
_) = MetaM (Core (M Type))
repTWildCard
repTy (HsIParamTy XIParamTy GhcRn
_ XRec GhcRn HsIPName
n XRec GhcRn (HsType GhcRn)
t) = do
                             n' <- HsIPName -> ReaderT MetaWrappers DsM (Core String)
rep_implicit_param_name (GenLocated EpAnnCO HsIPName -> HsIPName
forall l e. GenLocated l e -> e
unLoc XRec GhcRn HsIPName
GenLocated EpAnnCO HsIPName
n)
                             t' <- repLTy t
                             repTImplicitParam n' t'

repTy HsType GhcRn
ty                      = ThRejectionReason -> MetaM (Core (M Type))
forall a. ThRejectionReason -> MetaM a
notHandled (HsType GhcRn -> ThRejectionReason
ThExoticFormOfType HsType GhcRn
ty)

repTyLit :: HsTyLit (GhcPass p) -> MetaM (Core (M TH.TyLit))
repTyLit :: forall (p :: Pass). HsTyLit (GhcPass p) -> MetaM (Core (M TyLit))
repTyLit (HsNumTy XNumTy (GhcPass p)
_ Integer
i) = do
                         platform <- MetaM Platform
getPlatform
                         rep2 numTyLitName [mkIntegerExpr platform i]
repTyLit (HsStrTy XStrTy (GhcPass p)
_ FastString
s) = do { s' <- FastString -> ReaderT MetaWrappers DsM CoreExpr
forall (m :: * -> *). MonadThings m => FastString -> m CoreExpr
mkStringExprFS FastString
s
                            ; rep2 strTyLitName [s']
                            }
repTyLit (HsCharTy XCharTy (GhcPass p)
_ Char
c) = do { c' <- CoreExpr -> ReaderT MetaWrappers DsM CoreExpr
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Char -> CoreExpr
mkCharExpr Char
c)
                             ; rep2 charTyLitName [c']
                             }

-- | Represent a type wrapped in a Maybe
repMaybeLTy :: Maybe (LHsKind GhcRn)
            -> MetaM (Core (Maybe (M TH.Type)))
repMaybeLTy :: Maybe (XRec GhcRn (HsType GhcRn)) -> MetaM (Core (Maybe (M Type)))
repMaybeLTy Maybe (XRec GhcRn (HsType GhcRn))
m = do
  k_ty <- Name -> MetaM Type
wrapName Name
kindTyConName
  repMaybeT k_ty repLTy m

repRole :: LocatedAn NoEpAnns (Maybe Role) -> MetaM (Core TH.Role)
repRole :: LocatedAn NoEpAnns (Maybe Role)
-> ReaderT MetaWrappers DsM (Core Role)
repRole (L EpAnnCO
_ (Just Role
Nominal))          = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core Role)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
nominalRName []
repRole (L EpAnnCO
_ (Just Role
Representational)) = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core Role)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
representationalRName []
repRole (L EpAnnCO
_ (Just Role
Phantom))          = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core Role)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
phantomRName []
repRole (L EpAnnCO
_ Maybe Role
Nothing)                 = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core Role)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
inferRName []

-----------------------------------------------------------------------------
--              Splices
-----------------------------------------------------------------------------

-- See Note [How brackets and nested splices are handled] in GHC.Tc.Gen.Splice
-- We return a CoreExpr of any old type; the context should know

rep_splice :: Name -> MetaM (Core a)
rep_splice :: forall {k} (a :: k). Name -> MetaM (Core a)
rep_splice Name
splice_name
 = do { mb_val <- IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
-> ReaderT MetaWrappers DsM (Maybe DsMetaVal)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
 -> ReaderT MetaWrappers DsM (Maybe DsMetaVal))
-> IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
-> ReaderT MetaWrappers DsM (Maybe DsMetaVal)
forall a b. (a -> b) -> a -> b
$ Name -> IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
dsLookupMetaEnv Name
splice_name
       ; case mb_val of
           Just (DsSplice HsExpr GhcTc
e) -> do { e' <- DsM CoreExpr -> ReaderT MetaWrappers DsM CoreExpr
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM CoreExpr -> ReaderT MetaWrappers DsM CoreExpr)
-> DsM CoreExpr -> ReaderT MetaWrappers DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
e
                                   ; return (MkC e') }
           Maybe DsMetaVal
_ -> String -> SDoc -> ReaderT MetaWrappers DsM (Core a)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"HsSplice" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
splice_name) }
                        -- Should not happen; statically checked

-----------------------------------------------------------------------------
--              Expressions
-----------------------------------------------------------------------------

repLEs :: [LHsExpr GhcRn] -> MetaM (Core [(M TH.Exp)])
repLEs :: [LHsExpr GhcRn] -> MetaM (Core [M Exp])
repLEs [LHsExpr GhcRn]
es = Name
-> (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> MetaM (Core (M Exp)))
-> [GenLocated SrcSpanAnnA (HsExpr GhcRn)]
-> MetaM (Core [M Exp])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
expTyConName LHsExpr GhcRn -> MetaM (Core (M Exp))
GenLocated SrcSpanAnnA (HsExpr GhcRn) -> MetaM (Core (M Exp))
repLE [LHsExpr GhcRn]
[GenLocated SrcSpanAnnA (HsExpr GhcRn)]
es

-- FIXME: some of these panics should be converted into proper error messages
--        unless we can make sure that constructs, which are plainly not
--        supported in TH already lead to error messages at an earlier stage
repLE :: LHsExpr GhcRn -> MetaM (Core (M TH.Exp))
repLE :: LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE (L SrcSpanAnnA
loc HsExpr GhcRn
e) = (IOEnv (Env DsGblEnv DsLclEnv) (Core (M Exp))
 -> IOEnv (Env DsGblEnv DsLclEnv) (Core (M Exp)))
-> MetaM (Core (M Exp)) -> MetaM (Core (M Exp))
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT (SrcSpan
-> IOEnv (Env DsGblEnv DsLclEnv) (Core (M Exp))
-> IOEnv (Env DsGblEnv DsLclEnv) (Core (M Exp))
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)) (HsExpr GhcRn -> MetaM (Core (M Exp))
repE HsExpr GhcRn
e)

repE :: HsExpr GhcRn -> MetaM (Core (M TH.Exp))
repE :: HsExpr GhcRn -> MetaM (Core (M Exp))
repE (HsVar XVar GhcRn
_ (L SrcSpanAnnN
_ Name
x)) =
  do { mb_val <- IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
-> ReaderT MetaWrappers DsM (Maybe DsMetaVal)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
 -> ReaderT MetaWrappers DsM (Maybe DsMetaVal))
-> IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
-> ReaderT MetaWrappers DsM (Maybe DsMetaVal)
forall a b. (a -> b) -> a -> b
$ Name -> IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
dsLookupMetaEnv Name
x
     ; case mb_val of
        Maybe DsMetaVal
Nothing            -> do { str <- DsM (Core Name) -> MetaM (Core Name)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM (Core Name) -> MetaM (Core Name))
-> DsM (Core Name) -> MetaM (Core Name)
forall a b. (a -> b) -> a -> b
$ Name -> DsM (Core Name)
globalVar Name
x
                                 ; repVarOrCon x str }
        Just (DsBound Id
y)   -> Name -> Core Name -> MetaM (Core (M Exp))
repVarOrCon Name
x (Id -> Core Name
coreVar Id
y)
        Just (DsSplice HsExpr GhcTc
e)  -> do { e' <- DsM CoreExpr -> ReaderT MetaWrappers DsM CoreExpr
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM CoreExpr -> ReaderT MetaWrappers DsM CoreExpr)
-> DsM CoreExpr -> ReaderT MetaWrappers DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
e
                                 ; return (MkC e') } }
repE (HsIPVar XIPVar GhcRn
_ HsIPName
n) = HsIPName -> ReaderT MetaWrappers DsM (Core String)
rep_implicit_param_name HsIPName
n ReaderT MetaWrappers DsM (Core String)
-> (Core String -> MetaM (Core (M Exp))) -> MetaM (Core (M Exp))
forall a b.
ReaderT MetaWrappers DsM a
-> (a -> ReaderT MetaWrappers DsM b) -> ReaderT MetaWrappers DsM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Core String -> MetaM (Core (M Exp))
repImplicitParamVar
repE (HsOverLabel XOverLabel GhcRn
_ SourceText
_ FastString
s) = FastString -> MetaM (Core (M Exp))
repOverLabel FastString
s

repE (HsRecSel XRecSel GhcRn
_ (FieldOcc XCFieldOcc GhcRn
x XRec GhcRn RdrName
_)) = HsExpr GhcRn -> MetaM (Core (M Exp))
repE (XVar GhcRn -> LIdP GhcRn -> HsExpr GhcRn
forall p. XVar p -> LIdP p -> HsExpr p
HsVar XVar GhcRn
NoExtField
noExtField (Name -> GenLocated SrcSpanAnnN Name
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA XCFieldOcc GhcRn
Name
x))

        -- Remember, we're desugaring renamer output here, so
        -- HsOverlit can definitely occur
repE (HsOverLit XOverLitE GhcRn
_ HsOverLit GhcRn
l) = do { a <- HsOverLit GhcRn -> MetaM (Core Lit)
repOverloadedLiteral HsOverLit GhcRn
l; repLit a }
repE (HsLit XLitE GhcRn
_ HsLit GhcRn
l)     = do { a <- HsLit GhcRn -> MetaM (Core Lit)
repLiteral HsLit GhcRn
l;           repLit a }
repE (HsLam XLam GhcRn
_ HsLamVariant
LamSingle (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
_ [GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
m] })) = LMatch GhcRn (LHsExpr GhcRn) -> MetaM (Core (M Exp))
repLambda LMatch GhcRn (LHsExpr GhcRn)
GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
m
repE e :: HsExpr GhcRn
e@(HsLam XLam GhcRn
_ HsLamVariant
LamSingle (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
_ })) = String -> SDoc -> MetaM (Core (M Exp))
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"repE: HsLam with multiple alternatives" (HsExpr GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcRn
e)
repE (HsLam XLam GhcRn
_ HsLamVariant
LamCase (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms }))
                   = do { ms' <- (GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> ReaderT MetaWrappers DsM (Core (M Match)))
-> [GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> ReaderT MetaWrappers DsM [Core (M Match)]
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 LMatch GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM (Core (M Match))
GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> ReaderT MetaWrappers DsM (Core (M Match))
repMatchTup [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms
                        ; core_ms <- coreListM matchTyConName ms'
                        ; repLamCase core_ms }
repE (HsLam XLam GhcRn
_ HsLamVariant
LamCases (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = (L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms) }))
                   = do { ms' <- (GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> ReaderT MetaWrappers DsM (Core (M Clause)))
-> [GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> ReaderT MetaWrappers DsM [Core (M Clause)]
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 LMatch GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM (Core (M Clause))
GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> ReaderT MetaWrappers DsM (Core (M Clause))
repClauseTup [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms
                        ; core_ms <- coreListM matchTyConName ms'
                        ; repLamCases core_ms }
repE (HsApp XApp GhcRn
_ LHsExpr GhcRn
x LHsExpr GhcRn
y)   = do {a <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
x; b <- repLE y; repApp a b}
repE (HsAppType XAppTypeE GhcRn
_ LHsExpr GhcRn
e LHsWcType (NoGhcTc GhcRn)
t) = do { a <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
                            ; s <- repLTy (hswc_body t)
                            ; repAppType a s }

repE (OpApp XOpApp GhcRn
_ LHsExpr GhcRn
e1 LHsExpr GhcRn
op LHsExpr GhcRn
e2) =
  do { arg1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e1;
       arg2 <- repLE e2;
       the_op <- repLE op ;
       repInfixApp arg1 the_op arg2 }
repE (NegApp XNegApp GhcRn
_ LHsExpr GhcRn
x SyntaxExpr GhcRn
_)      = do
                              a         <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
x
                              negateVar <- lookupOcc negateName >>= repVar
                              negateVar `repApp` a
repE (HsPar XPar GhcRn
_ LHsExpr GhcRn
x)            = LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
x
repE (SectionL XSectionL GhcRn
_ LHsExpr GhcRn
x LHsExpr GhcRn
y)       = do { a <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
x; b <- repLE y; repSectionL a b }
repE (SectionR XSectionR GhcRn
_ LHsExpr GhcRn
x LHsExpr GhcRn
y)       = do { a <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
x; b <- repLE y; repSectionR a b }
repE (HsCase XCase GhcRn
_ LHsExpr GhcRn
e (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = (L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms) }))
                          = do { arg <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
                               ; ms2 <- mapM repMatchTup ms
                               ; core_ms2 <- coreListM matchTyConName ms2
                               ; repCaseE arg core_ms2 }
repE (HsIf XIf GhcRn
_ LHsExpr GhcRn
x LHsExpr GhcRn
y LHsExpr GhcRn
z)       = do
                            a <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
x
                            b <- repLE y
                            c <- repLE z
                            repCond a b c
repE (HsMultiIf XMultiIf GhcRn
_ [LGRHS GhcRn (LHsExpr GhcRn)]
alts)
  = do { (binds, alts') <- ([([GenSymBind], Core (M (Guard, Exp)))]
 -> ([[GenSymBind]], [Core (M (Guard, Exp))]))
-> ReaderT MetaWrappers DsM [([GenSymBind], Core (M (Guard, Exp)))]
-> ReaderT
     MetaWrappers DsM ([[GenSymBind]], [Core (M (Guard, Exp))])
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [([GenSymBind], Core (M (Guard, Exp)))]
-> ([[GenSymBind]], [Core (M (Guard, Exp))])
forall a b. [(a, b)] -> ([a], [b])
unzip (ReaderT MetaWrappers DsM [([GenSymBind], Core (M (Guard, Exp)))]
 -> ReaderT
      MetaWrappers DsM ([[GenSymBind]], [Core (M (Guard, Exp))]))
-> ReaderT MetaWrappers DsM [([GenSymBind], Core (M (Guard, Exp)))]
-> ReaderT
     MetaWrappers DsM ([[GenSymBind]], [Core (M (Guard, Exp))])
forall a b. (a -> b) -> a -> b
$ (GenLocated
   EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp))))
-> [GenLocated
      EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> ReaderT MetaWrappers DsM [([GenSymBind], Core (M (Guard, Exp)))]
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 LGRHS GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp)))
GenLocated
  EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp)))
repLGRHS [LGRHS GhcRn (LHsExpr GhcRn)]
[GenLocated
   EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
alts
       ; expr' <- repMultiIf (nonEmptyCoreList alts')
       ; wrapGenSyms (concat binds) expr' }
repE (HsLet XLet GhcRn
_ HsLocalBinds GhcRn
bs LHsExpr GhcRn
e)       = do { (ss,ds) <- HsLocalBinds GhcRn -> MetaM ([GenSymBind], Core [M Dec])
repBinds HsLocalBinds GhcRn
bs
                               ; e2 <- addBinds ss (repLE e)
                               ; z <- repLetE ds e2
                               ; wrapGenSyms ss z }

-- FIXME: I haven't got the types here right yet
repE e :: HsExpr GhcRn
e@(HsDo XDo GhcRn
_ HsDoFlavour
ctxt (L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
sts))
 | Just Maybe ModuleName
maybeModuleName <- case HsDoFlavour
ctxt of
     { DoExpr Maybe ModuleName
m -> Maybe ModuleName -> Maybe (Maybe ModuleName)
forall a. a -> Maybe a
Just Maybe ModuleName
m; HsDoFlavour
GhciStmtCtxt -> Maybe ModuleName -> Maybe (Maybe ModuleName)
forall a. a -> Maybe a
Just Maybe ModuleName
forall a. Maybe a
Nothing; HsDoFlavour
_ -> Maybe (Maybe ModuleName)
forall a. Maybe a
Nothing }
 = do { (ss,zs) <- [ExprLStmt GhcRn] -> MetaM ([GenSymBind], [Core (M Stmt)])
repLSts [ExprLStmt GhcRn]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
sts;
        e'      <- repDoE maybeModuleName (nonEmptyCoreList zs);
        wrapGenSyms ss e' }

 | HsDoFlavour
ListComp <- HsDoFlavour
ctxt
 = do { (ss,zs) <- [ExprLStmt GhcRn] -> MetaM ([GenSymBind], [Core (M Stmt)])
repLSts [ExprLStmt GhcRn]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
sts;
        e'      <- repComp (nonEmptyCoreList zs);
        wrapGenSyms ss e' }

 | MDoExpr Maybe ModuleName
maybeModuleName <- HsDoFlavour
ctxt
 = do { (ss,zs) <- [ExprLStmt GhcRn] -> MetaM ([GenSymBind], [Core (M Stmt)])
repLSts [ExprLStmt GhcRn]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
sts;
        e'      <- repMDoE maybeModuleName (nonEmptyCoreList zs);
        wrapGenSyms ss e' }

  | Bool
otherwise
  = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsExpr GhcRn -> ThRejectionReason
ThMonadComprehensionSyntax HsExpr GhcRn
e)

repE (ExplicitList XExplicitList GhcRn
_ [LHsExpr GhcRn]
es) = do { xs <- [LHsExpr GhcRn] -> MetaM (Core [M Exp])
repLEs [LHsExpr GhcRn]
es; repListExp xs }
repE (ExplicitTuple XExplicitTuple GhcRn
_ [HsTupArg GhcRn]
es Boxity
boxity) =
  let tupArgToCoreExp :: HsTupArg GhcRn -> MetaM (Core (Maybe (M TH.Exp)))
      tupArgToCoreExp :: HsTupArg GhcRn -> MetaM (Core (Maybe (M Exp)))
tupArgToCoreExp HsTupArg GhcRn
a
        | (Present XPresent GhcRn
_ LHsExpr GhcRn
e) <- HsTupArg GhcRn
a = do { e' <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
                                  ; coreJustM expTyConName e' }
        | Bool
otherwise = Name -> MetaM (Core (Maybe (M Exp)))
forall a. Name -> MetaM (Core (Maybe a))
coreNothingM Name
expTyConName

  in do { args <- (HsTupArg GhcRn -> MetaM (Core (Maybe (M Exp))))
-> [HsTupArg GhcRn]
-> ReaderT MetaWrappers DsM [Core (Maybe (M Exp))]
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 HsTupArg GhcRn -> MetaM (Core (Maybe (M Exp)))
tupArgToCoreExp [HsTupArg GhcRn]
es
        ; expTy <- wrapName  expTyConName
        ; let maybeExpQTy = TyCon -> [Type] -> Type
mkTyConApp TyCon
maybeTyCon [Type
expTy]
              listArg = Type -> [Core (Maybe (M Exp))] -> Core [Maybe (M Exp)]
forall a. Type -> [Core a] -> Core [a]
coreList' Type
maybeExpQTy [Core (Maybe (M Exp))]
args
        ; if isBoxed boxity
          then repTup listArg
          else repUnboxedTup listArg }

repE (ExplicitSum XExplicitSum GhcRn
_ Int
alt Int
arity LHsExpr GhcRn
e)
 = do { e1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
      ; repUnboxedSum e1 alt arity }

repE (RecordCon { rcon_con :: forall p. HsExpr p -> XRec p (ConLikeP p)
rcon_con = XRec GhcRn (ConLikeP GhcRn)
c, rcon_flds :: forall p. HsExpr p -> HsRecordBinds p
rcon_flds = HsRecordBinds GhcRn
flds })
 = do { x <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
c;
        fs <- repFields flds;
        repRecCon x fs }
repE (RecordUpd { rupd_expr :: forall p. HsExpr p -> LHsExpr p
rupd_expr = LHsExpr GhcRn
e, rupd_flds :: forall p. HsExpr p -> LHsRecUpdFields p
rupd_flds = RegularRecUpdFields { recUpdFields :: forall p. LHsRecUpdFields p -> [LHsRecUpdField p p]
recUpdFields = [LHsRecUpdField GhcRn GhcRn]
flds } })
 = do { x <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e;
        fs <- repUpdFields flds;
        repRecUpd x fs }
repE e :: HsExpr GhcRn
e@(RecordUpd { rupd_flds :: forall p. HsExpr p -> LHsRecUpdFields p
rupd_flds = OverloadedRecUpdFields {} })
  = do
      -- Not possible due to elimination in the renamer. See Note
      -- [Handling overloaded and rebindable constructs]
      String -> SDoc -> MetaM (Core (M Exp))
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"repE: unexpected overloaded record update" (SDoc -> MetaM (Core (M Exp))) -> SDoc -> MetaM (Core (M Exp))
forall a b. (a -> b) -> a -> b
$ HsExpr GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcRn
e

repE (ExprWithTySig XExprWithTySig GhcRn
_ LHsExpr GhcRn
e LHsSigWcType (NoGhcTc GhcRn)
wc_ty)
  = FreshOrReuse
-> [Name] -> MetaM (Core (M Exp)) -> MetaM (Core (M Exp))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
FreshNamesOnly (LHsSigType GhcRn -> [Name]
get_scoped_tvs_from_sig LHsSigType GhcRn
sig_ty) (MetaM (Core (M Exp)) -> MetaM (Core (M Exp)))
-> MetaM (Core (M Exp)) -> MetaM (Core (M Exp))
forall a b. (a -> b) -> a -> b
$
    do { e1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
       ; t1 <- rep_ty_sig' sig_ty
       ; repSigExp e1 t1 }
  where
    sig_ty :: LHsSigType GhcRn
sig_ty = LHsSigWcType GhcRn -> LHsSigType GhcRn
forall pass. LHsSigWcType pass -> LHsSigType pass
dropWildCards LHsSigWcType (NoGhcTc GhcRn)
LHsSigWcType GhcRn
wc_ty

repE (ArithSeq XArithSeq GhcRn
_ Maybe (SyntaxExpr GhcRn)
_ ArithSeqInfo GhcRn
aseq) =
  case ArithSeqInfo GhcRn
aseq of
    From LHsExpr GhcRn
e              -> do { ds1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e; repFrom ds1 }
    FromThen LHsExpr GhcRn
e1 LHsExpr GhcRn
e2      -> do
                             ds1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e1
                             ds2 <- repLE e2
                             repFromThen ds1 ds2
    FromTo   LHsExpr GhcRn
e1 LHsExpr GhcRn
e2      -> do
                             ds1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e1
                             ds2 <- repLE e2
                             repFromTo ds1 ds2
    FromThenTo LHsExpr GhcRn
e1 LHsExpr GhcRn
e2 LHsExpr GhcRn
e3 -> do
                             ds1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e1
                             ds2 <- repLE e2
                             ds3 <- repLE e3
                             repFromThenTo ds1 ds2 ds3

repE (HsTypedSplice XTypedSplice GhcRn
n LHsExpr GhcRn
_) = Name -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> MetaM (Core a)
rep_splice XTypedSplice GhcRn
Name
n
repE (HsUntypedSplice (HsUntypedSpliceNested Name
n) HsUntypedSplice GhcRn
_)  = Name -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> MetaM (Core a)
rep_splice Name
n
repE e :: HsExpr GhcRn
e@(HsUntypedSplice (HsUntypedSpliceTop ThModFinalizers
_ HsExpr GhcRn
_) HsUntypedSplice GhcRn
_) = String -> SDoc -> MetaM (Core (M Exp))
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"repE: top level splice" (HsExpr GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcRn
e)
repE (HsStatic XStatic GhcRn
_ LHsExpr GhcRn
e)        = LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e MetaM (Core (M Exp))
-> (Core (M Exp) -> MetaM (Core (M Exp))) -> MetaM (Core (M Exp))
forall a b.
ReaderT MetaWrappers DsM a
-> (a -> ReaderT MetaWrappers DsM b) -> ReaderT MetaWrappers DsM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
staticEName ([CoreExpr] -> MetaM (Core (M Exp)))
-> (Core (M Exp) -> [CoreExpr])
-> Core (M Exp)
-> MetaM (Core (M Exp))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
:[]) (CoreExpr -> [CoreExpr])
-> (Core (M Exp) -> CoreExpr) -> Core (M Exp) -> [CoreExpr]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Core (M Exp) -> CoreExpr
forall {k} (a :: k). Core a -> CoreExpr
unC
repE (HsUnboundVar XUnboundVar GhcRn
_ RdrName
uv)   = do
                               name <- RdrName -> MetaM (Core Name)
repRdrName RdrName
uv
                               repUnboundVar name
repE (HsGetField XGetField GhcRn
_ LHsExpr GhcRn
e (L EpAnnCO
_ (DotFieldOcc XCDotFieldOcc GhcRn
_ (L SrcSpanAnnN
_ (FieldLabelString FastString
f))))) = do
  e1 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
  repGetField e1 f
repE (HsProjection XProjection GhcRn
_ NonEmpty (XRec GhcRn (DotFieldOcc GhcRn))
xs) = NonEmpty FastString -> MetaM (Core (M Exp))
repProjection ((GenLocated EpAnnCO (DotFieldOcc GhcRn) -> FastString)
-> NonEmpty (GenLocated EpAnnCO (DotFieldOcc GhcRn))
-> NonEmpty FastString
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FieldLabelString -> FastString
field_label (FieldLabelString -> FastString)
-> (GenLocated EpAnnCO (DotFieldOcc GhcRn) -> FieldLabelString)
-> GenLocated EpAnnCO (DotFieldOcc GhcRn)
-> FastString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnN FieldLabelString -> FieldLabelString
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpanAnnN FieldLabelString -> FieldLabelString)
-> (GenLocated EpAnnCO (DotFieldOcc GhcRn)
    -> GenLocated SrcSpanAnnN FieldLabelString)
-> GenLocated EpAnnCO (DotFieldOcc GhcRn)
-> FieldLabelString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DotFieldOcc GhcRn -> XRec GhcRn FieldLabelString
DotFieldOcc GhcRn -> GenLocated SrcSpanAnnN FieldLabelString
forall p. DotFieldOcc p -> XRec p FieldLabelString
dfoLabel (DotFieldOcc GhcRn -> GenLocated SrcSpanAnnN FieldLabelString)
-> (GenLocated EpAnnCO (DotFieldOcc GhcRn) -> DotFieldOcc GhcRn)
-> GenLocated EpAnnCO (DotFieldOcc GhcRn)
-> GenLocated SrcSpanAnnN FieldLabelString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated EpAnnCO (DotFieldOcc GhcRn) -> DotFieldOcc GhcRn
forall l e. GenLocated l e -> e
unLoc) NonEmpty (XRec GhcRn (DotFieldOcc GhcRn))
NonEmpty (GenLocated EpAnnCO (DotFieldOcc GhcRn))
xs)
repE (HsEmbTy XEmbTy GhcRn
_ LHsWcType (NoGhcTc GhcRn)
t) = do
  t1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
-> GenLocated SrcSpanAnnA (HsType GhcRn)
forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body LHsWcType (NoGhcTc GhcRn)
HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
t)
  rep2 typeEName [unC t1]
repE e :: HsExpr GhcRn
e@(XExpr (ExpandedThingRn HsThingRn
o HsExpr GhcRn
x))
  | OrigExpr HsExpr GhcRn
e <- HsThingRn
o
  = do { rebindable_on <- DsM Bool -> ReaderT MetaWrappers DsM Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM Bool -> ReaderT MetaWrappers DsM Bool)
-> DsM Bool -> ReaderT MetaWrappers DsM Bool
forall a b. (a -> b) -> a -> b
$ Extension -> DsM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RebindableSyntax
       ; if rebindable_on  -- See Note [Quotation and rebindable syntax]
         then repE x
         else repE e }
  | Bool
otherwise
  = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsExpr GhcRn -> ThRejectionReason
ThExpressionForm HsExpr GhcRn
e)

repE (XExpr (PopErrCtxt (L SrcSpanAnnA
_ HsExpr GhcRn
e))) = HsExpr GhcRn -> MetaM (Core (M Exp))
repE HsExpr GhcRn
e
repE e :: HsExpr GhcRn
e@(HsPragE XPragE GhcRn
_ (HsPragSCC {}) LHsExpr GhcRn
_) = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsExpr GhcRn -> ThRejectionReason
ThCostCentres HsExpr GhcRn
e)
repE e :: HsExpr GhcRn
e@(HsTypedBracket{})   = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsExpr GhcRn -> ThRejectionReason
ThExpressionForm HsExpr GhcRn
e)
repE e :: HsExpr GhcRn
e@(HsUntypedBracket{}) = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsExpr GhcRn -> ThRejectionReason
ThExpressionForm HsExpr GhcRn
e)
repE e :: HsExpr GhcRn
e@(HsProc{}) = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsExpr GhcRn -> ThRejectionReason
ThExpressionForm HsExpr GhcRn
e)

{- Note [Quotation and rebindable syntax]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  f = [| (* 3) |]

Because of Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr,
the renamer will expand (* 3) to (rightSection (*) 3), regardless of RebindableSyntax.
Then, concerning the TH quotation,

* If RebindableSyntax is off, we want the TH quote to generate the section (* 3),
  as the user originally wrote.

* If RebindableSyntax is on, we perhaps want the TH quote to generate
  (rightSection (*) 3), using whatever 'rightSection' is in scope, because
  (a) RebindableSyntax might not be on in the splicing context
  (b) Even if it is, 'rightSection' might not be in scope
  (c) At least in the case of Typed Template Haskell we should never get
      a type error from the splice.

We consult the module-wide RebindableSyntax flag here. We could instead record
the choice in ExpandedThingRn, but it seems simpler to consult the flag (again).
-}

-----------------------------------------------------------------------------
-- Building representations of auxiliary structures like Match, Clause, Stmt,

repMatchTup ::  LMatch GhcRn (LHsExpr GhcRn) -> MetaM (Core (M TH.Match))
repMatchTup :: LMatch GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM (Core (M Match))
repMatchTup (L SrcSpanAnnA
_ (Match { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = [LPat GhcRn
p]
                        , m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs XCGRHSs GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [LGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
guards HsLocalBinds GhcRn
wheres })) =
  do { ss1 <- [Name] -> MetaM [GenSymBind]
mkGenSyms (CollectFlag GhcRn -> LPat GhcRn -> [IdP GhcRn]
forall p. CollectPass p => CollectFlag p -> LPat p -> [IdP p]
collectPatBinders CollectFlag GhcRn
forall p. CollectFlag p
CollNoDictBinders LPat GhcRn
p)
     ; addBinds ss1 $ do {
     ; p1 <- repLP p
     ; (ss2,ds) <- repBinds wheres
     ; addBinds ss2 $ do {
     ; gs    <- repGuards guards
     ; match <- repMatch p1 gs ds
     ; wrapGenSyms (ss1++ss2) match }}}
repMatchTup LMatch GhcRn (LHsExpr GhcRn)
_ = String -> ReaderT MetaWrappers DsM (Core (M Match))
forall a. HasCallStack => String -> a
panic String
"repMatchTup: case alt with more than one arg or with invisible pattern"

repClauseTup ::  LMatch GhcRn (LHsExpr GhcRn) -> MetaM (Core (M TH.Clause))
repClauseTup :: LMatch GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM (Core (M Clause))
repClauseTup (L SrcSpanAnnA
_ (Match { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = [LPat GhcRn]
ps
                         , m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs XCGRHSs GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [LGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
guards  HsLocalBinds GhcRn
wheres })) =
  do { ss1 <- [Name] -> MetaM [GenSymBind]
mkGenSyms (CollectFlag GhcRn -> [LPat GhcRn] -> [IdP GhcRn]
forall p. CollectPass p => CollectFlag p -> [LPat p] -> [IdP p]
collectPatsBinders CollectFlag GhcRn
forall p. CollectFlag p
CollNoDictBinders [LPat GhcRn]
ps)
     ; addBinds ss1 $ do {
       ps1 <- repLPs ps
     ; (ss2,ds) <- repBinds wheres
     ; addBinds ss2 $ do {
       gs <- repGuards guards
     ; clause <- repClause ps1 gs ds
     ; wrapGenSyms (ss1++ss2) clause }}}

repGuards ::  [LGRHS GhcRn (LHsExpr GhcRn)] ->  MetaM (Core (M TH.Body))
repGuards :: [LGRHS GhcRn (LHsExpr GhcRn)] -> MetaM (Core (M Body))
repGuards [L EpAnnCO
_ (GRHS XCGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [] GenLocated SrcSpanAnnA (HsExpr GhcRn)
e)]
  = do {a <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e; repNormal a }
repGuards [LGRHS GhcRn (LHsExpr GhcRn)]
other
  = do { zs <- (GenLocated
   EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp))))
-> [GenLocated
      EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> ReaderT MetaWrappers DsM [([GenSymBind], Core (M (Guard, Exp)))]
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 LGRHS GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp)))
GenLocated
  EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp)))
repLGRHS [LGRHS GhcRn (LHsExpr GhcRn)]
[GenLocated
   EpAnnCO (GRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
other
       ; let (xs, ys) = unzip zs
       ; gd <- repGuarded (nonEmptyCoreList ys)
       ; wrapGenSyms (concat xs) gd }

repLGRHS :: LGRHS GhcRn (LHsExpr GhcRn)
         -> MetaM ([GenSymBind], (Core (M (TH.Guard, TH.Exp))))
repLGRHS :: LGRHS GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM ([GenSymBind], Core (M (Guard, Exp)))
repLGRHS (L EpAnnCO
_ (GRHS XCGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [L SrcSpanAnnA
_ (BodyStmt XBodyStmt GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ GenLocated SrcSpanAnnA (HsExpr GhcRn)
e1 SyntaxExpr GhcRn
_ SyntaxExpr GhcRn
_)] GenLocated SrcSpanAnnA (HsExpr GhcRn)
e2))
  = do { guarded <- LHsExpr GhcRn -> LHsExpr GhcRn -> MetaM (Core (M (Guard, Exp)))
repLNormalGE LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e1 LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e2
       ; return ([], guarded) }
repLGRHS (L EpAnnCO
_ (GRHS XCGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [ExprLStmt GhcRn]
ss GenLocated SrcSpanAnnA (HsExpr GhcRn)
rhs))
  = do { (gs, ss') <- [ExprLStmt GhcRn] -> MetaM ([GenSymBind], [Core (M Stmt)])
repLSts [ExprLStmt GhcRn]
ss
       ; rhs' <- addBinds gs $ repLE rhs
       ; guarded <- repPatGE (nonEmptyCoreList ss') rhs'
       ; return (gs, guarded) }

repFields :: HsRecordBinds GhcRn -> MetaM (Core [M TH.FieldExp])
repFields :: HsRecordBinds GhcRn -> MetaM (Core [M FieldExp])
repFields (HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcRn (LHsExpr GhcRn)]
flds })
  = Name
-> (GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
    -> MetaM (Core (M FieldExp)))
-> [GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> MetaM (Core [M FieldExp])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
fieldExpTyConName LHsRecField GhcRn (LHsExpr GhcRn) -> MetaM (Core (M FieldExp))
GenLocated
  SrcSpanAnnA
  (HsFieldBind
     (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
     (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> MetaM (Core (M FieldExp))
rep_fld [LHsRecField GhcRn (LHsExpr GhcRn)]
[GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
flds
  where
    rep_fld :: LHsRecField GhcRn (LHsExpr GhcRn)
            -> MetaM (Core (M TH.FieldExp))
    rep_fld :: LHsRecField GhcRn (LHsExpr GhcRn) -> MetaM (Core (M FieldExp))
rep_fld (L SrcSpanAnnA
_ HsFieldBind
  (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld) = do { fn <- Name -> MetaM (Core Name)
lookupOcc (HsRecField GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
-> XCFieldOcc GhcRn
forall p arg. UnXRec p => HsRecField p arg -> XCFieldOcc p
hsRecFieldSel HsRecField GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
HsFieldBind
  (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld)
                           ; e  <- repLE (hfbRHS fld)
                           ; repFieldExp fn e }

repUpdFields :: [LHsRecUpdField GhcRn GhcRn] -> MetaM (Core [M TH.FieldExp])
repUpdFields :: [LHsRecUpdField GhcRn GhcRn] -> MetaM (Core [M FieldExp])
repUpdFields = Name
-> (GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
    -> MetaM (Core (M FieldExp)))
-> [GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> MetaM (Core [M FieldExp])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
fieldExpTyConName LHsRecUpdField GhcRn GhcRn -> MetaM (Core (M FieldExp))
GenLocated
  SrcSpanAnnA
  (HsFieldBind
     (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
     (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> MetaM (Core (M FieldExp))
rep_fld
  where
    rep_fld :: LHsRecUpdField GhcRn GhcRn -> MetaM (Core (M TH.FieldExp))
    rep_fld :: LHsRecUpdField GhcRn GhcRn -> MetaM (Core (M FieldExp))
rep_fld (L SrcSpanAnnA
l HsFieldBind
  (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld) = case GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn)
-> AmbiguousFieldOcc GhcRn
forall l e. GenLocated l e -> e
unLoc (HsFieldBind
  (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
-> GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn)
forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS HsFieldBind
  (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld) of
      Unambiguous XUnambiguous GhcRn
sel_name XRec GhcRn RdrName
_ -> do { fn <- GenLocated SrcSpanAnnA Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc (SrcSpanAnnA -> Name -> GenLocated SrcSpanAnnA Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l XUnambiguous GhcRn
Name
sel_name)
                                   ; e  <- repLE (hfbRHS fld)
                                   ; repFieldExp fn e }
      Ambiguous{}            -> ThRejectionReason -> MetaM (Core (M FieldExp))
forall a. ThRejectionReason -> MetaM a
notHandled (HsRecUpdField GhcRn GhcRn -> ThRejectionReason
ThAmbiguousRecordUpdates HsRecUpdField GhcRn GhcRn
HsFieldBind
  (GenLocated SrcSpanAnnA (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld)



-----------------------------------------------------------------------------
-- Representing Stmt's is tricky, especially if bound variables
-- shadow each other. Consider:  [| do { x <- f 1; x <- f x; g x } |]
-- First gensym new names for every variable in any of the patterns.
-- both static (x'1 and x'2), and dynamic ((gensym "x") and (gensym "y"))
-- if variables didn't shadow, the static gensym wouldn't be necessary
-- and we could reuse the original names (x and x).
--
-- do { x'1 <- gensym "x"
--    ; x'2 <- gensym "x"
--    ; doE Nothing
--          [ BindSt (pvar x'1) [| f 1 |]
--          , BindSt (pvar x'2) [| f x |]
--          , NoBindSt [| g x |]
--          ]
--    }

-- The strategy is to translate a whole list of do-bindings by building a
-- bigger environment, and a bigger set of meta bindings
-- (like:  x'1 <- gensym "x" ) and then combining these with the translations
-- of the expressions within the Do

-----------------------------------------------------------------------------
-- The helper function repSts computes the translation of each sub expression
-- and a bunch of prefix bindings denoting the dynamic renaming.

repLSts :: [LStmt GhcRn (LHsExpr GhcRn)] -> MetaM ([GenSymBind], [Core (M TH.Stmt)])
repLSts :: [ExprLStmt GhcRn] -> MetaM ([GenSymBind], [Core (M Stmt)])
repLSts [ExprLStmt GhcRn]
stmts = [Stmt GhcRn (LHsExpr GhcRn)]
-> MetaM ([GenSymBind], [Core (M Stmt)])
repSts ((GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> [GenLocated
      SrcSpanAnnA
      (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> [StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated
  SrcSpanAnnA
  (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
forall l e. GenLocated l e -> e
unLoc [ExprLStmt GhcRn]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
stmts)

repSts :: [Stmt GhcRn (LHsExpr GhcRn)] -> MetaM ([GenSymBind], [Core (M TH.Stmt)])
repSts :: [Stmt GhcRn (LHsExpr GhcRn)]
-> MetaM ([GenSymBind], [Core (M Stmt)])
repSts (BindStmt XBindStmt GhcRn GhcRn (LHsExpr GhcRn)
_ LPat GhcRn
p LHsExpr GhcRn
e : [Stmt GhcRn (LHsExpr GhcRn)]
ss) =
   do { e2 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
      ; ss1 <- mkGenSyms (collectPatBinders CollNoDictBinders p)
      ; addBinds ss1 $ do {
      ; p1 <- repLP p;
      ; (ss2,zs) <- repSts ss
      ; z <- repBindSt p1 e2
      ; return (ss1++ss2, z : zs) }}
repSts (LetStmt XLetStmt GhcRn GhcRn (LHsExpr GhcRn)
_ HsLocalBinds GhcRn
bs : [Stmt GhcRn (LHsExpr GhcRn)]
ss) =
   do { (ss1,ds) <- HsLocalBinds GhcRn -> MetaM ([GenSymBind], Core [M Dec])
repBinds HsLocalBinds GhcRn
bs
      ; z <- repLetSt ds
      ; (ss2,zs) <- addBinds ss1 (repSts ss)
      ; return (ss1++ss2, z : zs) }
repSts (BodyStmt XBodyStmt GhcRn GhcRn (LHsExpr GhcRn)
_ LHsExpr GhcRn
e SyntaxExpr GhcRn
_ SyntaxExpr GhcRn
_ : [Stmt GhcRn (LHsExpr GhcRn)]
ss) =
   do { e2 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
      ; z <- repNoBindSt e2
      ; (ss2,zs) <- repSts ss
      ; return (ss2, z : zs) }
repSts (ParStmt XParStmt GhcRn GhcRn (LHsExpr GhcRn)
_ [ParStmtBlock GhcRn GhcRn]
stmt_blocks HsExpr GhcRn
_ SyntaxExpr GhcRn
_ : [Stmt GhcRn (LHsExpr GhcRn)]
ss) =
   do { (ss_s, stmt_blocks1) <- (ParStmtBlock GhcRn GhcRn
 -> ReaderT MetaWrappers DsM ([GenSymBind], Core [M Stmt]))
-> [ParStmtBlock GhcRn GhcRn]
-> ReaderT MetaWrappers DsM ([[GenSymBind]], [Core [M Stmt]])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM ParStmtBlock GhcRn GhcRn
-> ReaderT MetaWrappers DsM ([GenSymBind], Core [M Stmt])
rep_stmt_block [ParStmtBlock GhcRn GhcRn]
stmt_blocks
      ; let stmt_blocks2 = [Core [M Stmt]] -> Core [[M Stmt]]
forall a. [Core a] -> Core [a]
nonEmptyCoreList [Core [M Stmt]]
stmt_blocks1
            ss1 = [[GenSymBind]] -> [GenSymBind]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[GenSymBind]]
ss_s
      ; z <- repParSt stmt_blocks2
      ; (ss2, zs) <- addBinds ss1 (repSts ss)
      ; return (ss1++ss2, z : zs) }
   where
     rep_stmt_block :: ParStmtBlock GhcRn GhcRn
                    -> MetaM ([GenSymBind], Core [(M TH.Stmt)])
     rep_stmt_block :: ParStmtBlock GhcRn GhcRn
-> ReaderT MetaWrappers DsM ([GenSymBind], Core [M Stmt])
rep_stmt_block (ParStmtBlock XParStmtBlock GhcRn GhcRn
_ [ExprLStmt GhcRn]
stmts [IdP GhcRn]
_ SyntaxExpr GhcRn
_) =
       do { (ss1, zs) <- [Stmt GhcRn (LHsExpr GhcRn)]
-> MetaM ([GenSymBind], [Core (M Stmt)])
repSts ((GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> [GenLocated
      SrcSpanAnnA
      (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> [StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated
  SrcSpanAnnA
  (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
forall l e. GenLocated l e -> e
unLoc [ExprLStmt GhcRn]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
stmts)
          ; zs1 <- coreListM stmtTyConName zs
          ; return (ss1, zs1) }
repSts [LastStmt XLastStmt GhcRn GhcRn (LHsExpr GhcRn)
_ LHsExpr GhcRn
e Maybe Bool
_ SyntaxExpr GhcRn
_]
  = do { e2 <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e
       ; z <- repNoBindSt e2
       ; return ([], [z]) }
repSts (stmt :: Stmt GhcRn (LHsExpr GhcRn)
stmt@RecStmt{} : [Stmt GhcRn (LHsExpr GhcRn)]
ss)
  = do { let binders :: [IdP GhcRn]
binders = CollectFlag GhcRn
-> [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
-> [IdP GhcRn]
forall (idL :: Pass) (idR :: Pass) body.
CollectPass (GhcPass idL) =>
CollectFlag (GhcPass idL)
-> [LStmtLR (GhcPass idL) (GhcPass idR) body]
-> [IdP (GhcPass idL)]
collectLStmtsBinders CollectFlag GhcRn
forall p. CollectFlag p
CollNoDictBinders (GenLocated
  (Anno
     [GenLocated
        (Anno (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))))
        (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))])
  [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
-> [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
forall l e. GenLocated l e -> e
unLoc (GenLocated
   (Anno
      [GenLocated
         (Anno (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))))
         (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))])
   [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
 -> [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))])
-> GenLocated
     (Anno
        [GenLocated
           (Anno (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))))
           (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))])
     [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
-> [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
forall a b. (a -> b) -> a -> b
$ StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
-> XRec
     GhcRn [LStmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
forall idL idR body.
StmtLR idL idR body -> XRec idR [LStmtLR idL idR body]
recS_stmts Stmt GhcRn (LHsExpr GhcRn)
StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
stmt)
       ; ss1 <- [Name] -> MetaM [GenSymBind]
mkGenSyms [IdP GhcRn]
[Name]
binders
       -- Bring all of binders in the recursive group into scope for the
       -- whole group.
       ; (ss1_other,rss) <- addBinds ss1 $ repSts (map unLoc (unLoc $ recS_stmts stmt))
       ; massert (sort ss1 == sort ss1_other)
       ; z <- repRecSt (nonEmptyCoreList rss)
       ; (ss2,zs) <- addBinds ss1 (repSts ss)
       ; return (ss1++ss2, z : zs) }
repSts []    = ([GenSymBind], [Core (M Stmt)])
-> MetaM ([GenSymBind], [Core (M Stmt)])
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return ([],[])
repSts [Stmt GhcRn (LHsExpr GhcRn)]
other = ThRejectionReason -> MetaM ([GenSymBind], [Core (M Stmt)])
forall a. ThRejectionReason -> MetaM a
notHandled ([Stmt GhcRn (LHsExpr GhcRn)] -> ThRejectionReason
ThExoticStatement [Stmt GhcRn (LHsExpr GhcRn)]
other)


-----------------------------------------------------------
--                      Bindings
-----------------------------------------------------------

repBinds :: HsLocalBinds GhcRn -> MetaM ([GenSymBind], Core [(M TH.Dec)])
repBinds :: HsLocalBinds GhcRn -> MetaM ([GenSymBind], Core [M Dec])
repBinds (EmptyLocalBinds XEmptyLocalBinds GhcRn GhcRn
_)
  = do  { core_list <- Name -> [Core (M Dec)] -> MetaM (Core [M Dec])
forall a. Name -> [Core a] -> MetaM (Core [a])
coreListM Name
decTyConName []
        ; return ([], core_list) }

repBinds (HsIPBinds XHsIPBinds GhcRn GhcRn
_ (IPBinds XIPBinds GhcRn
_ [LIPBind GhcRn]
decs))
 = do   { ips <- (GenLocated SrcSpanAnnA (IPBind GhcRn)
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnA (IPBind GhcRn)]
-> MetaM [(SrcSpan, Core (M Dec))]
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 LIPBind GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
GenLocated SrcSpanAnnA (IPBind GhcRn)
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_implicit_param_bind [LIPBind GhcRn]
[GenLocated SrcSpanAnnA (IPBind GhcRn)]
decs
        ; core_list <- coreListM decTyConName
                                (de_loc (sort_by_loc ips))
        ; return ([], core_list)
        }

repBinds (HsValBinds XHsValBinds GhcRn GhcRn
_ HsValBinds GhcRn
decs)
 = do   { let { bndrs :: [Name]
bndrs = HsValBinds GhcRn -> [Name]
hsScopedTvBinders HsValBinds GhcRn
decs [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ CollectFlag GhcRn -> HsValBinds GhcRn -> [IdP GhcRn]
forall (idL :: Pass) idR.
CollectPass (GhcPass idL) =>
CollectFlag (GhcPass idL)
-> HsValBindsLR (GhcPass idL) idR -> [IdP (GhcPass idL)]
collectHsValBinders CollectFlag GhcRn
forall p. CollectFlag p
CollNoDictBinders HsValBinds GhcRn
decs }
                -- No need to worry about detailed scopes within
                -- the binding group, because we are talking Names
                -- here, so we can safely treat it as a mutually
                -- recursive group
                -- For hsScopedTvBinders see Note [Scoped type variables in quotes]
        ; ss        <- [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
bndrs
        ; prs       <- addBinds ss (rep_val_binds decs)
        ; core_list <- coreListM decTyConName
                                (de_loc (sort_by_loc prs))
        ; return (ss, core_list) }

rep_implicit_param_bind :: LIPBind GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
rep_implicit_param_bind :: LIPBind GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_implicit_param_bind (L SrcSpanAnnA
loc (IPBind XCIPBind GhcRn
_ (L EpAnnCO
_ HsIPName
n) (L SrcSpanAnnA
_ HsExpr GhcRn
rhs)))
 = do { name <- HsIPName -> ReaderT MetaWrappers DsM (Core String)
rep_implicit_param_name HsIPName
n
      ; rhs' <- repE rhs
      ; ipb <- repImplicitParamBind name rhs'
      ; return (locA loc, ipb) }

rep_implicit_param_name :: HsIPName -> MetaM (Core String)
rep_implicit_param_name :: HsIPName -> ReaderT MetaWrappers DsM (Core String)
rep_implicit_param_name (HsIPName FastString
name) = FastString -> ReaderT MetaWrappers DsM (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit FastString
name

rep_val_binds :: HsValBinds GhcRn -> MetaM [(SrcSpan, Core (M TH.Dec))]
-- Assumes: all the binders of the binding are already in the meta-env
rep_val_binds :: HsValBinds GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_val_binds (XValBindsLR (NValBinds [(RecFlag, LHsBindsLR GhcRn GhcRn)]
binds [LSig GhcRn]
sigs))
 = do { core1 <- LHsBindsLR GhcRn GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_binds ([Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))]
-> Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
forall a. [Bag a] -> Bag a
unionManyBags (((RecFlag, Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)))
 -> Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)))
-> [(RecFlag, Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)))]
-> [Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))]
forall a b. (a -> b) -> [a] -> [b]
map (RecFlag, Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)))
-> Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
forall a b. (a, b) -> b
snd [(RecFlag, LHsBindsLR GhcRn GhcRn)]
[(RecFlag, Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)))]
binds))
      ; core2 <- rep_sigs sigs
      ; return (core1 ++ core2) }
rep_val_binds (ValBinds XValBinds GhcRn GhcRn
_ LHsBindsLR GhcRn GhcRn
_ [LSig GhcRn]
_)
 = String -> MetaM [(SrcSpan, Core (M Dec))]
forall a. HasCallStack => String -> a
panic String
"rep_val_binds: ValBinds"

rep_binds :: LHsBinds GhcRn -> MetaM [(SrcSpan, Core (M TH.Dec))]
rep_binds :: LHsBindsLR GhcRn GhcRn -> MetaM [(SrcSpan, Core (M Dec))]
rep_binds = (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)
 -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec)))
-> [GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)]
-> MetaM [(SrcSpan, Core (M Dec))]
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 LHsBind GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_bind ([GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)]
 -> MetaM [(SrcSpan, Core (M Dec))])
-> (Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
    -> [GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)])
-> Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
-> MetaM [(SrcSpan, Core (M Dec))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bag (GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn))
-> [GenLocated SrcSpanAnnA (HsBindLR GhcRn GhcRn)]
forall a. Bag a -> [a]
bagToList

rep_bind :: LHsBind GhcRn -> MetaM (SrcSpan, Core (M TH.Dec))
-- Assumes: all the binders of the binding are already in the meta-env

-- Note GHC treats declarations of a variable (not a pattern)
-- e.g.  x = g 5 as a Fun MonoBinds. This is indicated by a single match
-- with an empty list of patterns
rep_bind :: LHsBind GhcRn -> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
rep_bind (L SrcSpanAnnA
loc (FunBind
                 { fun_id :: forall idL idR. HsBindLR idL idR -> LIdP idL
fun_id = LIdP GhcRn
fn,
                   fun_matches :: forall idL idR. HsBindLR idL idR -> MatchGroup idR (LHsExpr idR)
fun_matches = MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts
                           = (L SrcSpanAnnL
_ [L SrcSpanAnnA
_ (Match
                                   { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = []
                                   , m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs XCGRHSs GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [LGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
guards HsLocalBinds GhcRn
wheres
                                   -- For a variable declaration I'm pretty
                                   -- sure we always have a FunRhs
                                   , m_ctxt :: forall p body. Match p body -> HsMatchContext (LIdP (NoGhcTc p))
m_ctxt = FunRhs { mc_strictness :: forall fn. HsMatchContext fn -> SrcStrictness
mc_strictness = SrcStrictness
strictessAnn }
                                   } )]) } }))
 = do { (ss,wherecore) <- HsLocalBinds GhcRn -> MetaM ([GenSymBind], Core [M Dec])
repBinds HsLocalBinds GhcRn
wheres
        ; guardcore <- addBinds ss (repGuards guards)
        ; fn'  <- lookupNBinder fn
        ; p    <- repPvar fn' >>= case strictessAnn of
                                    SrcStrictness
SrcLazy -> Core (M Pat) -> MetaM (Core (M Pat))
repPtilde
                                    SrcStrictness
SrcStrict -> Core (M Pat) -> MetaM (Core (M Pat))
repPbang
                                    SrcStrictness
NoSrcStrict -> Core (M Pat) -> MetaM (Core (M Pat))
forall a. a -> ReaderT MetaWrappers DsM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        ; ans  <- repVal p guardcore wherecore
        ; ans' <- wrapGenSyms ss ans
        ; return (locA loc, ans') }

rep_bind (L SrcSpanAnnA
loc (FunBind { fun_id :: forall idL idR. HsBindLR idL idR -> LIdP idL
fun_id = LIdP GhcRn
fn
                         , fun_matches :: forall idL idR. HsBindLR idL idR -> MatchGroup idR (LHsExpr idR)
fun_matches = MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms } }))
 =   do { ms1 <- (GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> ReaderT MetaWrappers DsM (Core (M Clause)))
-> [GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> ReaderT MetaWrappers DsM [Core (M Clause)]
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 LMatch GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM (Core (M Clause))
GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> ReaderT MetaWrappers DsM (Core (M Clause))
repClauseTup [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
ms
        ; fn' <- lookupNBinder fn
        ; ans <- repFun fn' (nonEmptyCoreList ms1)
        ; return (locA loc, ans) }

rep_bind (L SrcSpanAnnA
loc (PatBind { pat_lhs :: forall idL idR. HsBindLR idL idR -> LPat idL
pat_lhs = LPat GhcRn
pat
                         , pat_rhs :: forall idL idR. HsBindLR idL idR -> GRHSs idR (LHsExpr idR)
pat_rhs = GRHSs XCGRHSs GhcRn (LHsExpr GhcRn)
_ [LGRHS GhcRn (LHsExpr GhcRn)]
guards HsLocalBinds GhcRn
wheres }))
 =   do { patcore <- LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
pat
        ; (ss,wherecore) <- repBinds wheres
        ; guardcore <- addBinds ss (repGuards guards)
        ; ans  <- repVal patcore guardcore wherecore
        ; ans' <- wrapGenSyms ss ans
        ; return (locA loc, ans') }

rep_bind (L SrcSpanAnnA
loc (PatSynBind XPatSynBind GhcRn GhcRn
_ (PSB { psb_id :: forall idL idR. PatSynBind idL idR -> LIdP idL
psb_id   = LIdP GhcRn
syn
                                   , psb_args :: forall idL idR. PatSynBind idL idR -> HsPatSynDetails idR
psb_args = HsPatSynDetails GhcRn
args
                                   , psb_def :: forall idL idR. PatSynBind idL idR -> LPat idR
psb_def  = LPat GhcRn
pat
                                   , psb_dir :: forall idL idR. PatSynBind idL idR -> HsPatSynDir idR
psb_dir  = HsPatSynDir GhcRn
dir })))
  = do { syn'      <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
lookupNBinder LIdP GhcRn
GenLocated SrcSpanAnnN Name
syn
       ; dir'      <- repPatSynDir dir
       ; ss        <- mkGenArgSyms args
       ; patSynD'  <- addBinds ss (
         do { args'  <- repPatSynArgs args
            ; pat'   <- repLP pat
            ; repPatSynD syn' args' dir' pat' })
       ; patSynD'' <- wrapGenArgSyms args ss patSynD'
       ; return (locA loc, patSynD'') }
  where
    mkGenArgSyms :: HsPatSynDetails GhcRn -> MetaM [GenSymBind]
    -- for Record Pattern Synonyms we want to conflate the selector
    -- and the pattern-only names in order to provide a nicer TH
    -- API. Whereas inside GHC, record pattern synonym selectors and
    -- their pattern-only bound right hand sides have different names,
    -- we want to treat them the same in TH. This is the reason why we
    -- need an adjusted mkGenArgSyms in the `RecCon` case below.
    mkGenArgSyms :: HsPatSynDetails GhcRn -> MetaM [GenSymBind]
mkGenArgSyms (PrefixCon [Void]
_ [LIdP GhcRn]
args)   = [Name] -> MetaM [GenSymBind]
mkGenSyms ((GenLocated SrcSpanAnnN Name -> Name)
-> [GenLocated SrcSpanAnnN Name] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
args)
    mkGenArgSyms (InfixCon LIdP GhcRn
arg1 LIdP GhcRn
arg2) = [Name] -> MetaM [GenSymBind]
mkGenSyms [GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
arg1, GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
arg2]
    mkGenArgSyms (RecCon [RecordPatSynField GhcRn]
fields)
      = do { let pats :: [Name]
pats = (RecordPatSynField GhcRn -> Name)
-> [RecordPatSynField GhcRn] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpanAnnN Name -> Name)
-> (RecordPatSynField GhcRn -> GenLocated SrcSpanAnnN Name)
-> RecordPatSynField GhcRn
-> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecordPatSynField GhcRn -> LIdP GhcRn
RecordPatSynField GhcRn -> GenLocated SrcSpanAnnN Name
forall pass. RecordPatSynField pass -> LIdP pass
recordPatSynPatVar) [RecordPatSynField GhcRn]
fields
                 sels :: [Name]
sels = (RecordPatSynField GhcRn -> Name)
-> [RecordPatSynField GhcRn] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (FieldOcc GhcRn -> XCFieldOcc GhcRn
FieldOcc GhcRn -> Name
forall pass. FieldOcc pass -> XCFieldOcc pass
foExt (FieldOcc GhcRn -> Name)
-> (RecordPatSynField GhcRn -> FieldOcc GhcRn)
-> RecordPatSynField GhcRn
-> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecordPatSynField GhcRn -> FieldOcc GhcRn
forall pass. RecordPatSynField pass -> FieldOcc pass
recordPatSynField) [RecordPatSynField GhcRn]
fields
           ; ss <- [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
sels
           ; return $ replaceNames (zip sels pats) ss }

    replaceNames :: [(a, a)] -> [(a, b)] -> [(a, b)]
replaceNames [(a, a)]
selsPats [(a, b)]
genSyms
      = [ (a
pat, b
id) | (a
sel, b
id) <- [(a, b)]
genSyms, (a
sel', a
pat) <- [(a, a)]
selsPats
                    , a
sel a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
sel' ]

    wrapGenArgSyms :: HsPatSynDetails GhcRn
                   -> [GenSymBind] -> Core (M TH.Dec) -> MetaM (Core (M TH.Dec))
    wrapGenArgSyms :: HsPatSynDetails GhcRn
-> [GenSymBind] -> Core (M Dec) -> MetaM (Core (M Dec))
wrapGenArgSyms (RecCon [RecordPatSynField GhcRn]
_) [GenSymBind]
_  Core (M Dec)
dec = Core (M Dec) -> MetaM (Core (M Dec))
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return Core (M Dec)
dec
    wrapGenArgSyms HsPatSynDetails GhcRn
_          [GenSymBind]
ss Core (M Dec)
dec = [GenSymBind] -> Core (M Dec) -> MetaM (Core (M Dec))
forall {k} (a :: k).
[GenSymBind] -> Core (M a) -> MetaM (Core (M a))
wrapGenSyms [GenSymBind]
ss Core (M Dec)
dec

rep_bind (L SrcSpanAnnA
_ (VarBind { var_ext :: forall idL idR. HsBindLR idL idR -> XVarBind idL idR
var_ext = XVarBind GhcRn GhcRn
x })) = DataConCantHappen
-> ReaderT MetaWrappers DsM (SrcSpan, Core (M Dec))
forall a. DataConCantHappen -> a
dataConCantHappen XVarBind GhcRn GhcRn
DataConCantHappen
x

repPatSynD :: Core TH.Name
           -> Core (M TH.PatSynArgs)
           -> Core (M TH.PatSynDir)
           -> Core (M TH.Pat)
           -> MetaM (Core (M TH.Dec))
repPatSynD :: Core Name
-> Core (M PatSynArgs)
-> Core (M PatSynDir)
-> Core (M Pat)
-> MetaM (Core (M Dec))
repPatSynD (MkC CoreExpr
syn) (MkC CoreExpr
args) (MkC CoreExpr
dir) (MkC CoreExpr
pat)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
patSynDName [CoreExpr
syn, CoreExpr
args, CoreExpr
dir, CoreExpr
pat]

repPatSynArgs :: HsPatSynDetails GhcRn -> MetaM (Core (M TH.PatSynArgs))
repPatSynArgs :: HsPatSynDetails GhcRn -> MetaM (Core (M PatSynArgs))
repPatSynArgs (PrefixCon [Void]
_ [LIdP GhcRn]
args)
  = do { args' <- Name
-> (GenLocated SrcSpanAnnN Name -> MetaM (Core Name))
-> [GenLocated SrcSpanAnnN Name]
-> MetaM (Core [Name])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repList Name
nameTyConName GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc [LIdP GhcRn]
[GenLocated SrcSpanAnnN Name]
args
       ; repPrefixPatSynArgs args' }
repPatSynArgs (InfixCon LIdP GhcRn
arg1 LIdP GhcRn
arg2)
  = do { arg1' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc LIdP GhcRn
GenLocated SrcSpanAnnN Name
arg1
       ; arg2' <- lookupLOcc arg2
       ; repInfixPatSynArgs arg1' arg2' }
repPatSynArgs (RecCon [RecordPatSynField GhcRn]
fields)
  = do { sels' <- Name
-> (FieldOcc GhcRn -> MetaM (Core Name))
-> [FieldOcc GhcRn]
-> MetaM (Core [Name])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repList Name
nameTyConName (Name -> MetaM (Core Name)
lookupOcc (Name -> MetaM (Core Name))
-> (FieldOcc GhcRn -> Name) -> FieldOcc GhcRn -> MetaM (Core Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldOcc GhcRn -> XCFieldOcc GhcRn
FieldOcc GhcRn -> Name
forall pass. FieldOcc pass -> XCFieldOcc pass
foExt) [FieldOcc GhcRn]
sels
       ; repRecordPatSynArgs sels' }
  where sels :: [FieldOcc GhcRn]
sels = (RecordPatSynField GhcRn -> FieldOcc GhcRn)
-> [RecordPatSynField GhcRn] -> [FieldOcc GhcRn]
forall a b. (a -> b) -> [a] -> [b]
map RecordPatSynField GhcRn -> FieldOcc GhcRn
forall pass. RecordPatSynField pass -> FieldOcc pass
recordPatSynField [RecordPatSynField GhcRn]
fields

repPrefixPatSynArgs :: Core [TH.Name] -> MetaM (Core (M TH.PatSynArgs))
repPrefixPatSynArgs :: Core [Name] -> MetaM (Core (M PatSynArgs))
repPrefixPatSynArgs (MkC CoreExpr
nms) = Name -> [CoreExpr] -> MetaM (Core (M PatSynArgs))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
prefixPatSynName [CoreExpr
nms]

repInfixPatSynArgs :: Core TH.Name -> Core TH.Name -> MetaM (Core (M TH.PatSynArgs))
repInfixPatSynArgs :: Core Name -> Core Name -> MetaM (Core (M PatSynArgs))
repInfixPatSynArgs (MkC CoreExpr
nm1) (MkC CoreExpr
nm2) = Name -> [CoreExpr] -> MetaM (Core (M PatSynArgs))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
infixPatSynName [CoreExpr
nm1, CoreExpr
nm2]

repRecordPatSynArgs :: Core [TH.Name]
                    -> MetaM (Core (M TH.PatSynArgs))
repRecordPatSynArgs :: Core [Name] -> MetaM (Core (M PatSynArgs))
repRecordPatSynArgs (MkC CoreExpr
sels) = Name -> [CoreExpr] -> MetaM (Core (M PatSynArgs))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
recordPatSynName [CoreExpr
sels]

repPatSynDir :: HsPatSynDir GhcRn -> MetaM (Core (M TH.PatSynDir))
repPatSynDir :: HsPatSynDir GhcRn -> MetaM (Core (M PatSynDir))
repPatSynDir HsPatSynDir GhcRn
Unidirectional        = Name -> [CoreExpr] -> MetaM (Core (M PatSynDir))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
unidirPatSynName []
repPatSynDir HsPatSynDir GhcRn
ImplicitBidirectional = Name -> [CoreExpr] -> MetaM (Core (M PatSynDir))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
implBidirPatSynName []
repPatSynDir (ExplicitBidirectional (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = (L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
clauses) }))
  = do { clauses' <- (GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
 -> ReaderT MetaWrappers DsM (Core (M Clause)))
-> [GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
-> ReaderT MetaWrappers DsM [Core (M Clause)]
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 LMatch GhcRn (LHsExpr GhcRn)
-> ReaderT MetaWrappers DsM (Core (M Clause))
GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
-> ReaderT MetaWrappers DsM (Core (M Clause))
repClauseTup [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
clauses
       ; repExplBidirPatSynDir (nonEmptyCoreList clauses') }

repExplBidirPatSynDir :: Core [(M TH.Clause)] -> MetaM (Core (M TH.PatSynDir))
repExplBidirPatSynDir :: Core [M Clause] -> MetaM (Core (M PatSynDir))
repExplBidirPatSynDir (MkC CoreExpr
cls) = Name -> [CoreExpr] -> MetaM (Core (M PatSynDir))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
explBidirPatSynName [CoreExpr
cls]


-----------------------------------------------------------------------------
-- Since everything in a Bind is mutually recursive we need rename all
-- all the variables simultaneously. For example:
-- [| AndMonoBinds (f x = x + g 2) (g x = f 1 + 2) |] would translate to
-- do { f'1 <- gensym "f"
--    ; g'2 <- gensym "g"
--    ; [ do { x'3 <- gensym "x"; fun f'1 [pvar x'3] [| x + g2 |]},
--        do { x'4 <- gensym "x"; fun g'2 [pvar x'4] [| f 1 + 2 |]}
--      ]}
-- This requires collecting the bindings (f'1 <- gensym "f"), and the
-- environment ( f |-> f'1 ) from each binding, and then unioning them
-- together. As we do this we collect GenSymBinds's which represent the renamed
-- variables bound by the Bindings. In order not to lose track of these
-- representations we build a shadow datatype MB with the same structure as
-- MonoBinds, but which has slots for the representations


-----------------------------------------------------------------------------
-- GHC allows a more general form of lambda abstraction than specified
-- by Haskell 98. In particular it allows guarded lambda's like :
-- (\  x | even x -> 0 | odd x -> 1) at the moment we can't represent this in
-- Haskell Template's Meta.Exp type so we punt if it isn't a simple thing like
-- (\ p1 .. pn -> exp) by causing an error.

repLambda :: LMatch GhcRn (LHsExpr GhcRn) -> MetaM (Core (M TH.Exp))
repLambda :: LMatch GhcRn (LHsExpr GhcRn) -> MetaM (Core (M Exp))
repLambda (L SrcSpanAnnA
_ (Match { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = [LPat GhcRn]
ps
                      , m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs XCGRHSs GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [L EpAnnCO
_ (GRHS XCGRHS GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
_ [] GenLocated SrcSpanAnnA (HsExpr GhcRn)
e)]
                                              (EmptyLocalBinds XEmptyLocalBinds GhcRn GhcRn
_) } ))
 = do { let bndrs :: [IdP GhcRn]
bndrs = CollectFlag GhcRn -> [LPat GhcRn] -> [IdP GhcRn]
forall p. CollectPass p => CollectFlag p -> [LPat p] -> [IdP p]
collectPatsBinders CollectFlag GhcRn
forall p. CollectFlag p
CollNoDictBinders [LPat GhcRn]
ps ;
      ; ss  <- [Name] -> MetaM [GenSymBind]
mkGenSyms [IdP GhcRn]
[Name]
bndrs
      ; lam <- addBinds ss (
                do { xs <- repLPs ps; body <- repLE e; repLam xs body })
      ; wrapGenSyms ss lam }

repLambda (L SrcSpanAnnA
_ Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
m) = ThRejectionReason -> MetaM (Core (M Exp))
forall a. ThRejectionReason -> MetaM a
notHandled (Match GhcRn (LHsExpr GhcRn) -> ThRejectionReason
ThGuardedLambdas Match GhcRn (LHsExpr GhcRn)
Match GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
m)


-----------------------------------------------------------------------------
--                      Patterns
-- repP deals with patterns.  It assumes that we have already
-- walked over the pattern(s) once to collect the binders, and
-- have extended the environment.  So every pattern-bound
-- variable should already appear in the environment.

-- Process a list of patterns
repLPs :: [LPat GhcRn] -> MetaM (Core [(M TH.Pat)])
repLPs :: [LPat GhcRn] -> MetaM (Core [M Pat])
repLPs [LPat GhcRn]
ps = Name
-> (GenLocated SrcSpanAnnA (Pat GhcRn) -> MetaM (Core (M Pat)))
-> [GenLocated SrcSpanAnnA (Pat GhcRn)]
-> MetaM (Core [M Pat])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
patTyConName LPat GhcRn -> MetaM (Core (M Pat))
GenLocated SrcSpanAnnA (Pat GhcRn) -> MetaM (Core (M Pat))
repLP [LPat GhcRn]
[GenLocated SrcSpanAnnA (Pat GhcRn)]
ps

repLP :: LPat GhcRn -> MetaM (Core (M TH.Pat))
repLP :: LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p = Pat GhcRn -> MetaM (Core (M Pat))
repP (GenLocated SrcSpanAnnA (Pat GhcRn) -> Pat GhcRn
forall l e. GenLocated l e -> e
unLoc LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p)

repP :: Pat GhcRn -> MetaM (Core (M TH.Pat))
repP :: Pat GhcRn -> MetaM (Core (M Pat))
repP (WildPat XWildPat GhcRn
_)        = MetaM (Core (M Pat))
repPwild
repP (LitPat XLitPat GhcRn
_ HsLit GhcRn
l)       = do { l2 <- HsLit GhcRn -> MetaM (Core Lit)
repLiteral HsLit GhcRn
l; repPlit l2 }
repP (VarPat XVarPat GhcRn
_ LIdP GhcRn
x)       = do { x' <- Name -> MetaM (Core Name)
lookupBinder (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
x); repPvar x' }
repP (LazyPat XLazyPat GhcRn
_ LPat GhcRn
p)      = do { p1 <- LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p; repPtilde p1 }
repP (BangPat XBangPat GhcRn
_ LPat GhcRn
p)      = do { p1 <- LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p; repPbang p1 }
repP (AsPat XAsPat GhcRn
_ LIdP GhcRn
x LPat GhcRn
p)      = do { x' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
lookupNBinder LIdP GhcRn
GenLocated SrcSpanAnnN Name
x; p1 <- repLP p
                             ; repPaspat x' p1 }
repP (ParPat XParPat GhcRn
_ LPat GhcRn
p)       = LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p
repP (ListPat XListPat GhcRn
_ [LPat GhcRn]
ps)     = do { qs <- [LPat GhcRn] -> MetaM (Core [M Pat])
repLPs [LPat GhcRn]
ps; repPlist qs }
repP (TuplePat XTuplePat GhcRn
_ [LPat GhcRn]
ps Boxity
boxed)
  | Boxity -> Bool
isBoxed Boxity
boxed       = do { qs <- [LPat GhcRn] -> MetaM (Core [M Pat])
repLPs [LPat GhcRn]
ps; repPtup qs }
  | Bool
otherwise           = do { qs <- [LPat GhcRn] -> MetaM (Core [M Pat])
repLPs [LPat GhcRn]
ps; repPunboxedTup qs }
repP (SumPat XSumPat GhcRn
_ LPat GhcRn
p Int
alt Int
arity) = do { p1 <- LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p
                                 ; repPunboxedSum p1 alt arity }
repP (ConPat XConPat GhcRn
NoExtField
NoExtField XRec GhcRn (ConLikeP GhcRn)
dc HsConPatDetails GhcRn
details)
 = do { con_str <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
dc
      ; case details of
         PrefixCon [HsConPatTyArg (NoGhcTc GhcRn)]
tyargs [LPat GhcRn]
ps -> do { qs <- [LPat GhcRn] -> MetaM (Core [M Pat])
repLPs [LPat GhcRn]
ps
                                   ; let unwrapTyArg (HsConPatTyArg XConPatTyArg pass
_ HsTyPat pass
t) = GenLocated l (HsType pass) -> HsType pass
forall l e. GenLocated l e -> e
unLoc (HsTyPat pass -> XRec pass (HsType pass)
forall pass. HsTyPat pass -> LHsType pass
hstp_body HsTyPat pass
t)
                                   ; ts <- repListM typeTyConName (repTy . unwrapTyArg) tyargs
                                   ; repPcon con_str ts qs }
         RecCon HsRecFields GhcRn (LPat GhcRn)
rec   -> do { fps <- Name
-> (GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (Pat GhcRn)))
    -> MetaM (Core (M (Name, Pat))))
-> [GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (Pat GhcRn)))]
-> MetaM (Core [M (Name, Pat)])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
fieldPatTyConName LHsRecField GhcRn (LPat GhcRn) -> MetaM (Core (M (Name, Pat)))
GenLocated
  SrcSpanAnnA
  (HsFieldBind
     (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
     (GenLocated SrcSpanAnnA (Pat GhcRn)))
-> MetaM (Core (M (Name, Pat)))
rep_fld (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))
-> [LHsRecField GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields GhcRn (LPat GhcRn)
HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))
rec)
                            ; repPrec con_str fps }
         InfixCon LPat GhcRn
p1 LPat GhcRn
p2 -> do { p1' <- LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p1;
                                p2' <- repLP p2;
                                repPinfix p1' con_str p2' }
   }
 where
   rep_fld :: LHsRecField GhcRn (LPat GhcRn) -> MetaM (Core (M (TH.Name, TH.Pat)))
   rep_fld :: LHsRecField GhcRn (LPat GhcRn) -> MetaM (Core (M (Name, Pat)))
rep_fld (L SrcSpanAnnA
_ HsFieldBind
  (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (Pat GhcRn))
fld) = do { MkC v <- Name -> MetaM (Core Name)
lookupOcc (HsRecField GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))
-> XCFieldOcc GhcRn
forall p arg. UnXRec p => HsRecField p arg -> XCFieldOcc p
hsRecFieldSel HsRecField GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))
HsFieldBind
  (GenLocated SrcSpanAnnA (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (Pat GhcRn))
fld)
                          ; MkC p <- repLP (hfbRHS fld)
                          ; rep2 fieldPatName [v,p] }
repP (NPat XNPat GhcRn
_ (L EpAnnCO
_ HsOverLit GhcRn
l) Maybe (SyntaxExpr GhcRn)
Nothing SyntaxExpr GhcRn
_) = do { a <- HsOverLit GhcRn -> MetaM (Core Lit)
repOverloadedLiteral HsOverLit GhcRn
l
                                     ; repPlit a }
repP (ViewPat XViewPat GhcRn
_ LHsExpr GhcRn
e LPat GhcRn
p) = do { e' <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
e; p' <- repLP p; repPview e' p' }
repP p :: Pat GhcRn
p@(NPat XNPat GhcRn
_ (L EpAnnCO
_ HsOverLit GhcRn
l) (Just SyntaxExpr GhcRn
_) SyntaxExpr GhcRn
_)
  | OverLitRn Bool
rebindable LIdP GhcRn
_ <- HsOverLit GhcRn -> XOverLit GhcRn
forall p. HsOverLit p -> XOverLit p
ol_ext HsOverLit GhcRn
l
  , Bool
rebindable = ThRejectionReason -> MetaM (Core (M Pat))
forall a. ThRejectionReason -> MetaM a
notHandled (Pat GhcRn -> ThRejectionReason
ThNegativeOverloadedPatterns Pat GhcRn
p)
  | HsIntegral IntegralLit
i <- HsOverLit GhcRn -> OverLitVal
forall p. HsOverLit p -> OverLitVal
ol_val HsOverLit GhcRn
l = do { a <- HsOverLit GhcRn -> MetaM (Core Lit)
repOverloadedLiteral HsOverLit GhcRn
l{ol_val = HsIntegral (negateIntegralLit i)}
                                  ; repPlit a }
  | HsFractional FractionalLit
i <- HsOverLit GhcRn -> OverLitVal
forall p. HsOverLit p -> OverLitVal
ol_val HsOverLit GhcRn
l = do { a <- HsOverLit GhcRn -> MetaM (Core Lit)
repOverloadedLiteral HsOverLit GhcRn
l{ol_val = HsFractional (negateFractionalLit i)}
                                  ; repPlit a }
  | Bool
otherwise = ThRejectionReason -> MetaM (Core (M Pat))
forall a. ThRejectionReason -> MetaM a
notHandled (Pat GhcRn -> ThRejectionReason
ThExoticPattern Pat GhcRn
p)
repP (SigPat XSigPat GhcRn
_ LPat GhcRn
p HsPatSigType (NoGhcTc GhcRn)
t) = do { p' <- LPat GhcRn -> MetaM (Core (M Pat))
repLP LPat GhcRn
p
                         ; t' <- repLTy (hsPatSigType t)
                         ; repPsig p' t' }
repP (EmbTyPat XEmbTyPat GhcRn
_ HsTyPat (NoGhcTc GhcRn)
t) = do { t' <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy (HsTyPat GhcRn -> XRec GhcRn (HsType GhcRn)
forall pass. HsTyPat pass -> LHsType pass
hstp_body HsTyPat (NoGhcTc GhcRn)
HsTyPat GhcRn
t)
                         ; repPtype t' }
repP (InvisPat XInvisPat GhcRn
_ HsTyPat (NoGhcTc GhcRn)
t) = do { t' <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M Type))
repLTy (HsTyPat GhcRn -> XRec GhcRn (HsType GhcRn)
forall pass. HsTyPat pass -> LHsType pass
hstp_body HsTyPat (NoGhcTc GhcRn)
HsTyPat GhcRn
t)
                         ; repPinvis t' }
repP (SplicePat (HsUntypedSpliceNested Name
n) HsUntypedSplice GhcRn
_) = Name -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> MetaM (Core a)
rep_splice Name
n
repP p :: Pat GhcRn
p@(SplicePat (HsUntypedSpliceTop ThModFinalizers
_ Pat GhcRn
_) HsUntypedSplice GhcRn
_) = String -> SDoc -> MetaM (Core (M Pat))
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"repP: top level splice" (Pat GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcRn
p)
repP Pat GhcRn
other = ThRejectionReason -> MetaM (Core (M Pat))
forall a. ThRejectionReason -> MetaM a
notHandled (Pat GhcRn -> ThRejectionReason
ThExoticPattern Pat GhcRn
other)

----------------------------------------------------------
-- Declaration ordering helpers

sort_by_loc :: [(SrcSpan, a)] -> [(SrcSpan, a)]
sort_by_loc :: forall a. [(SrcSpan, a)] -> [(SrcSpan, a)]
sort_by_loc = ((SrcSpan, a) -> (SrcSpan, a) -> Ordering)
-> [(SrcSpan, a)] -> [(SrcSpan, a)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (SrcSpan -> SrcSpan -> Ordering
SrcLoc.leftmost_smallest (SrcSpan -> SrcSpan -> Ordering)
-> ((SrcSpan, a) -> SrcSpan)
-> (SrcSpan, a)
-> (SrcSpan, a)
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (SrcSpan, a) -> SrcSpan
forall a b. (a, b) -> a
fst)

de_loc :: [(a, b)] -> [b]
de_loc :: forall a b. [(a, b)] -> [b]
de_loc = ((a, b) -> b) -> [(a, b)] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map (a, b) -> b
forall a b. (a, b) -> b
snd

----------------------------------------------------------
--      The meta-environment

-- A name/identifier association for fresh names of locally bound entities
type GenSymBind = (Name, Id)    -- Gensym the string and bind it to the Id
                                -- I.e.         (x, x_id) means
                                --      let x_id = gensym "x" in ...

-- Generate a fresh name for a locally bound entity

mkGenSyms :: [Name] -> MetaM [GenSymBind]
-- We can use the existing name.  For example:
--      [| \x_77 -> x_77 + x_77 |]
-- desugars to
--      do { x_77 <- genSym "x"; .... }
-- We use the same x_77 in the desugared program, but with the type Bndr
-- instead of Int
--
-- We do make it an Internal name, though (hence localiseName)
--
-- Nevertheless, it's monadic because we have to generate nameTy
mkGenSyms :: [Name] -> MetaM [GenSymBind]
mkGenSyms [Name]
ns = do { var_ty <- Name -> MetaM Type
lookupType Name
nameTyConName
                  ; return [ (nm, mkLocalId (localiseName nm) ManyTy var_ty)
                           | nm <- ns] }


addBinds :: [GenSymBind] -> MetaM a -> MetaM a
-- Add a list of fresh names for locally bound entities to the
-- meta environment (which is part of the state carried around
-- by the desugarer monad)
addBinds :: forall a. [GenSymBind] -> MetaM a -> MetaM a
addBinds [GenSymBind]
bs MetaM a
m = (IOEnv (Env DsGblEnv DsLclEnv) a
 -> IOEnv (Env DsGblEnv DsLclEnv) a)
-> MetaM a -> MetaM a
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT (DsMetaEnv
-> IOEnv (Env DsGblEnv DsLclEnv) a
-> IOEnv (Env DsGblEnv DsLclEnv) a
forall a. DsMetaEnv -> DsM a -> DsM a
dsExtendMetaEnv ([(Name, DsMetaVal)] -> DsMetaEnv
forall a. [(Name, a)] -> NameEnv a
mkNameEnv [(Name
n,Id -> DsMetaVal
DsBound Id
id) | (Name
n,Id
id) <- [GenSymBind]
bs])) MetaM a
m

-- Look up a locally bound name
--
lookupNBinder :: LocatedN Name -> MetaM (Core TH.Name)
lookupNBinder :: GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
lookupNBinder GenLocated SrcSpanAnnN Name
n = Name -> MetaM (Core Name)
lookupBinder (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN Name
n)

lookupBinder :: Name -> MetaM (Core TH.Name)
lookupBinder :: Name -> MetaM (Core Name)
lookupBinder = Name -> MetaM (Core Name)
lookupOcc
  -- Binders are brought into scope before the pattern or what-not is
  -- desugared.  Moreover, in instance declaration the binder of a method
  -- will be the selector Id and hence a global; so we need the
  -- globalVar case of lookupOcc

-- Look up a name that is either locally bound or a global name
--
--  * If it is a global name, generate the "original name" representation (ie,
--   the <module>:<name> form) for the associated entity
--
lookupLOcc :: GenLocated l Name -> MetaM (Core TH.Name)
-- Lookup an occurrence; it can't be a splice.
-- Use the in-scope bindings if they exist
lookupLOcc :: forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated l Name
n = Name -> MetaM (Core Name)
lookupOcc (GenLocated l Name -> Name
forall l e. GenLocated l e -> e
unLoc GenLocated l Name
n)

lookupOcc :: Name -> MetaM (Core TH.Name)
lookupOcc :: Name -> MetaM (Core Name)
lookupOcc = DsM (Core Name) -> MetaM (Core Name)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM (Core Name) -> MetaM (Core Name))
-> (Name -> DsM (Core Name)) -> Name -> MetaM (Core Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> DsM (Core Name)
lookupOccDsM

lookupOccDsM :: Name -> DsM (Core TH.Name)
lookupOccDsM :: Name -> DsM (Core Name)
lookupOccDsM Name
n
  = do {  mb_val <- Name -> IOEnv (Env DsGblEnv DsLclEnv) (Maybe DsMetaVal)
dsLookupMetaEnv Name
n ;
          case mb_val of
                Maybe DsMetaVal
Nothing           -> Name -> DsM (Core Name)
globalVar Name
n
                Just (DsBound Id
x)  -> Core Name -> DsM (Core Name)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Core Name
coreVar Id
x)
                Just (DsSplice HsExpr GhcTc
_) -> String -> SDoc -> DsM (Core Name)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"repE:lookupOcc" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
n)
    }


-- Not bound by the meta-env
-- Could be top-level; or could be local
--      f x = $(g [| x |])
-- Here the x will be local
globalVar :: Name -> DsM (Core TH.Name)
globalVar :: Name -> DsM (Core Name)
globalVar Name
n =
  case Name -> Maybe Module
nameModule_maybe Name
n of
    Just Module
m -> Module -> OccName -> DsM (Core Name)
globalVarExternal Module
m (Name -> OccName
forall a. NamedThing a => a -> OccName
getOccName Name
n)
    Maybe Module
Nothing -> Unique -> OccName -> DsM (Core Name)
globalVarLocal (Name -> Unique
forall a. Uniquable a => a -> Unique
getUnique Name
n) (Name -> OccName
forall a. NamedThing a => a -> OccName
getOccName Name
n)

globalVarLocal :: Unique -> OccName -> DsM (Core TH.Name)
globalVarLocal :: Unique -> OccName -> DsM (Core Name)
globalVarLocal Unique
unique OccName
name
  = do  { MkC occ <- OccName -> IOEnv (Env DsGblEnv DsLclEnv) (Core String)
forall (m :: * -> *). MonadThings m => OccName -> m (Core String)
occNameLit OccName
name
        ; platform <- targetPlatform <$> getDynFlags
        ; let uni = Platform -> Integer -> CoreExpr
mkIntegerExpr Platform
platform (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ Unique -> Word64
getKey Unique
unique)
        ; rep2_nwDsM mkNameLName [occ,uni] }

globalVarExternal :: Module -> OccName -> DsM (Core TH.Name)
globalVarExternal :: Module -> OccName -> DsM (Core Name)
globalVarExternal Module
mod OccName
name_occ
  = do  { MkC mod <- FastString -> IOEnv (Env DsGblEnv DsLclEnv) (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit FastString
name_mod
        ; MkC pkg <- coreStringLit name_pkg
        ; MkC occ <- occNameLit name_occ
        ; if | isDataOcc name_occ
             -> rep2_nwDsM mkNameG_dName [pkg,mod,occ]
             | isVarOcc  name_occ
             -> rep2_nwDsM mkNameG_vName [pkg,mod,occ]
             | isTcOcc   name_occ
             -> rep2_nwDsM mkNameG_tcName [pkg,mod,occ]
             | Just con_fs <- fieldOcc_maybe name_occ
             -> do { MkC con <- coreStringLit con_fs
                   ; rep2_nwDsM mkNameG_fldName [pkg,mod,con,occ] }
             | otherwise
             -> pprPanic "GHC.HsToCore.Quote.globalVar" (ppr name_occ)
        }
  where
    name_mod :: FastString
name_mod = ModuleName -> FastString
moduleNameFS (Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName Module
mod)
    name_pkg :: FastString
name_pkg = Unit -> FastString
forall u. IsUnitId u => u -> FastString
unitFS (Module -> Unit
forall unit. GenModule unit -> unit
moduleUnit Module
mod)

lookupType :: Name      -- Name of type constructor (e.g. (M TH.Exp))
           -> MetaM Type  -- The type
lookupType :: Name -> MetaM Type
lookupType Name
tc_name = do { tc <- DsM TyCon -> ReaderT MetaWrappers DsM TyCon
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM TyCon -> ReaderT MetaWrappers DsM TyCon)
-> DsM TyCon -> ReaderT MetaWrappers DsM TyCon
forall a b. (a -> b) -> a -> b
$ Name -> DsM TyCon
dsLookupTyCon Name
tc_name ;
                          return (mkTyConApp tc []) }

wrapGenSyms :: [GenSymBind]
            -> Core (M a) -> MetaM (Core (M a))
-- wrapGenSyms [(nm1,id1), (nm2,id2)] y
--      --> bindQ (gensym nm1) (\ id1 ->
--          bindQ (gensym nm2 (\ id2 ->
--          y))

wrapGenSyms :: forall {k} (a :: k).
[GenSymBind] -> Core (M a) -> MetaM (Core (M a))
wrapGenSyms [GenSymBind]
binds body :: Core (M a)
body@(MkC CoreExpr
b)
  = do  { var_ty <- Name -> MetaM Type
lookupType Name
nameTyConName
        ; go var_ty binds }
  where
    (Type
_, Type
elt_ty) = Type -> (Type, Type)
tcSplitAppTy (HasDebugCallStack => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
b)
        -- b :: m a, so we can get the type 'a' by looking at the
        -- argument type. Need to use `tcSplitAppTy` here as since
        -- the overloaded quotations patch the type of the expression can
        -- be something more complicated than just `Q a`.
        -- See #17839 for when this went wrong with the type `WriterT () m a`

    go :: Type -> [GenSymBind] -> ReaderT MetaWrappers DsM (Core (M a))
go Type
_ [] = Core (M a) -> ReaderT MetaWrappers DsM (Core (M a))
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return Core (M a)
body
    go Type
var_ty ((Name
name,Id
id) : [GenSymBind]
binds)
      = do { MkC body'  <- Type -> [GenSymBind] -> ReaderT MetaWrappers DsM (Core (M a))
go Type
var_ty [GenSymBind]
binds
           ; lit_str    <- occNameLit (occName name)
           ; gensym_app <- repGensym lit_str
           ; repBindM var_ty elt_ty
                      gensym_app (MkC (Lam id body')) }

occNameLit :: MonadThings m => OccName -> m (Core String)
occNameLit :: forall (m :: * -> *). MonadThings m => OccName -> m (Core String)
occNameLit OccName
name = FastString -> m (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit (OccName -> FastString
occNameFS OccName
name)


-- %*********************************************************************
-- %*                                                                   *
--              Constructing code
-- %*                                                                   *
-- %*********************************************************************

-----------------------------------------------------------------------------
-- PHANTOM TYPES for consistency. In order to make sure we do this correct
-- we invent a new datatype which uses phantom types.

newtype Core a = MkC CoreExpr
unC :: Core a -> CoreExpr
unC :: forall {k} (a :: k). Core a -> CoreExpr
unC (MkC CoreExpr
x) = CoreExpr
x

type family NotM a where
  NotM (M _) = TypeError ('Text ("rep2_nw must not produce something of overloaded type"))
  NotM _other = (() :: Constraint)

rep2M :: Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 :: Name -> [CoreExpr] -> MetaM (Core (M a))
rep2_nw :: NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nwDsM :: NotM a => Name -> [CoreExpr] -> DsM (Core a)
rep2 :: forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 = (forall z. DsM z -> ReaderT MetaWrappers DsM z)
-> ReaderT MetaWrappers DsM (CoreExpr -> CoreExpr)
-> Name
-> [CoreExpr]
-> ReaderT MetaWrappers DsM (Core (M a))
forall {k} (m :: * -> *) (a :: k).
Monad m =>
(forall z. DsM z -> m z)
-> m (CoreExpr -> CoreExpr) -> Name -> [CoreExpr] -> m (Core a)
rep2X IOEnv (Env DsGblEnv DsLclEnv) z -> ReaderT MetaWrappers DsM z
forall z. DsM z -> ReaderT MetaWrappers DsM z
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((MetaWrappers -> CoreExpr -> CoreExpr)
-> ReaderT MetaWrappers DsM (CoreExpr -> CoreExpr)
forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks MetaWrappers -> CoreExpr -> CoreExpr
quoteWrapper)
rep2M :: forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2M = (forall z. DsM z -> ReaderT MetaWrappers DsM z)
-> ReaderT MetaWrappers DsM (CoreExpr -> CoreExpr)
-> Name
-> [CoreExpr]
-> ReaderT MetaWrappers DsM (Core (M a))
forall {k} (m :: * -> *) (a :: k).
Monad m =>
(forall z. DsM z -> m z)
-> m (CoreExpr -> CoreExpr) -> Name -> [CoreExpr] -> m (Core a)
rep2X IOEnv (Env DsGblEnv DsLclEnv) z -> ReaderT MetaWrappers DsM z
forall z. DsM z -> ReaderT MetaWrappers DsM z
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((MetaWrappers -> CoreExpr -> CoreExpr)
-> ReaderT MetaWrappers DsM (CoreExpr -> CoreExpr)
forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks MetaWrappers -> CoreExpr -> CoreExpr
monadWrapper)
rep2_nw :: forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
n [CoreExpr]
xs = DsM (Core a) -> ReaderT MetaWrappers DsM (Core a)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Name -> [CoreExpr] -> DsM (Core a)
forall a. NotM a => Name -> [CoreExpr] -> DsM (Core a)
rep2_nwDsM Name
n [CoreExpr]
xs)
rep2_nwDsM :: forall a. NotM a => Name -> [CoreExpr] -> DsM (Core a)
rep2_nwDsM = (forall z. DsM z -> DsM z)
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr)
-> Name
-> [CoreExpr]
-> IOEnv (Env DsGblEnv DsLclEnv) (Core a)
forall {k} (m :: * -> *) (a :: k).
Monad m =>
(forall z. DsM z -> m z)
-> m (CoreExpr -> CoreExpr) -> Name -> [CoreExpr] -> m (Core a)
rep2X DsM z -> DsM z
forall a. a -> a
forall z. DsM z -> DsM z
id ((CoreExpr -> CoreExpr)
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr -> CoreExpr
forall a. a -> a
id)

rep2X :: Monad m => (forall z . DsM z -> m z)
      -> m (CoreExpr -> CoreExpr)
      -> Name
      -> [ CoreExpr ]
      -> m (Core a)
rep2X :: forall {k} (m :: * -> *) (a :: k).
Monad m =>
(forall z. DsM z -> m z)
-> m (CoreExpr -> CoreExpr) -> Name -> [CoreExpr] -> m (Core a)
rep2X forall z. DsM z -> m z
lift_dsm m (CoreExpr -> CoreExpr)
get_wrap Name
n [CoreExpr]
xs = do
  { rep_id <- DsM Id -> m Id
forall z. DsM z -> m z
lift_dsm (DsM Id -> m Id) -> DsM Id -> m Id
forall a b. (a -> b) -> a -> b
$ Name -> DsM Id
dsLookupGlobalId Name
n
  ; wrap <- get_wrap
  ; return (MkC $ (foldl' App (wrap (Var rep_id)) xs)) }


dataCon' :: Name -> [CoreExpr] -> MetaM (Core a)
dataCon' :: forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core a)
dataCon' Name
n [CoreExpr]
args = do { id <- IOEnv (Env DsGblEnv DsLclEnv) DataCon
-> ReaderT MetaWrappers DsM DataCon
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IOEnv (Env DsGblEnv DsLclEnv) DataCon
 -> ReaderT MetaWrappers DsM DataCon)
-> IOEnv (Env DsGblEnv DsLclEnv) DataCon
-> ReaderT MetaWrappers DsM DataCon
forall a b. (a -> b) -> a -> b
$ Name -> IOEnv (Env DsGblEnv DsLclEnv) DataCon
dsLookupDataCon Name
n
                     ; return $ MkC $ mkCoreConApps id args }

dataCon :: Name -> MetaM (Core a)
dataCon :: forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
n = Name -> [CoreExpr] -> MetaM (Core a)
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core a)
dataCon' Name
n []


-- %*********************************************************************
-- %*                                                                   *
--              The 'smart constructors'
-- %*                                                                   *
-- %*********************************************************************

--------------- Patterns -----------------
repPlit   :: Core TH.Lit -> MetaM (Core (M TH.Pat))
repPlit :: Core Lit -> MetaM (Core (M Pat))
repPlit (MkC CoreExpr
l) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
litPName [CoreExpr
l]

repPvar :: Core TH.Name -> MetaM (Core (M TH.Pat))
repPvar :: Core Name -> MetaM (Core (M Pat))
repPvar (MkC CoreExpr
s) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
varPName [CoreExpr
s]

repPtup :: Core [(M TH.Pat)] -> MetaM (Core (M TH.Pat))
repPtup :: Core [M Pat] -> MetaM (Core (M Pat))
repPtup (MkC CoreExpr
ps) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tupPName [CoreExpr
ps]

repPunboxedTup :: Core [(M TH.Pat)] -> MetaM (Core (M TH.Pat))
repPunboxedTup :: Core [M Pat] -> MetaM (Core (M Pat))
repPunboxedTup (MkC CoreExpr
ps) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
unboxedTupPName [CoreExpr
ps]

repPunboxedSum :: Core (M TH.Pat) -> TH.SumAlt -> TH.SumArity -> MetaM (Core (M TH.Pat))
-- Note: not Core TH.SumAlt or Core TH.SumArity; it's easier to be direct here
repPunboxedSum :: Core (M Pat) -> Int -> Int -> MetaM (Core (M Pat))
repPunboxedSum (MkC CoreExpr
p) Int
alt Int
arity
 = do { platform <- MetaM Platform
getPlatform
      ; rep2 unboxedSumPName [ p
                             , mkIntExprInt platform alt
                             , mkIntExprInt platform arity ] }

repPcon   :: Core TH.Name -> Core [(M TH.Type)] -> Core [(M TH.Pat)] -> MetaM (Core (M TH.Pat))
repPcon :: Core Name -> Core [M Type] -> Core [M Pat] -> MetaM (Core (M Pat))
repPcon (MkC CoreExpr
s) (MkC CoreExpr
ts) (MkC CoreExpr
ps) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
conPName [CoreExpr
s, CoreExpr
ts, CoreExpr
ps]

repPrec   :: Core TH.Name -> Core [M (TH.Name, TH.Pat)] -> MetaM (Core (M TH.Pat))
repPrec :: Core Name -> Core [M (Name, Pat)] -> MetaM (Core (M Pat))
repPrec (MkC CoreExpr
c) (MkC CoreExpr
rps) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
recPName [CoreExpr
c,CoreExpr
rps]

repPinfix :: Core (M TH.Pat) -> Core TH.Name -> Core (M TH.Pat) -> MetaM (Core (M TH.Pat))
repPinfix :: Core (M Pat) -> Core Name -> Core (M Pat) -> MetaM (Core (M Pat))
repPinfix (MkC CoreExpr
p1) (MkC CoreExpr
n) (MkC CoreExpr
p2) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
infixPName [CoreExpr
p1, CoreExpr
n, CoreExpr
p2]

repPtilde :: Core (M TH.Pat) -> MetaM (Core (M TH.Pat))
repPtilde :: Core (M Pat) -> MetaM (Core (M Pat))
repPtilde (MkC CoreExpr
p) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tildePName [CoreExpr
p]

repPbang :: Core (M TH.Pat) -> MetaM (Core (M TH.Pat))
repPbang :: Core (M Pat) -> MetaM (Core (M Pat))
repPbang (MkC CoreExpr
p) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
bangPName [CoreExpr
p]

repPaspat :: Core TH.Name -> Core (M TH.Pat) -> MetaM (Core (M TH.Pat))
repPaspat :: Core Name -> Core (M Pat) -> MetaM (Core (M Pat))
repPaspat (MkC CoreExpr
s) (MkC CoreExpr
p) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
asPName [CoreExpr
s, CoreExpr
p]

repPwild  :: MetaM (Core (M TH.Pat))
repPwild :: MetaM (Core (M Pat))
repPwild = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
wildPName []

repPlist :: Core [(M TH.Pat)] -> MetaM (Core (M TH.Pat))
repPlist :: Core [M Pat] -> MetaM (Core (M Pat))
repPlist (MkC CoreExpr
ps) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
listPName [CoreExpr
ps]

repPview :: Core (M TH.Exp) -> Core (M TH.Pat) -> MetaM (Core (M TH.Pat))
repPview :: Core (M Exp) -> Core (M Pat) -> MetaM (Core (M Pat))
repPview (MkC CoreExpr
e) (MkC CoreExpr
p) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
viewPName [CoreExpr
e,CoreExpr
p]

repPsig :: Core (M TH.Pat) -> Core (M TH.Type) -> MetaM (Core (M TH.Pat))
repPsig :: Core (M Pat) -> Core (M Type) -> MetaM (Core (M Pat))
repPsig (MkC CoreExpr
p) (MkC CoreExpr
t) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sigPName [CoreExpr
p, CoreExpr
t]

repPtype :: Core (M TH.Type) -> MetaM (Core (M TH.Pat))
repPtype :: Core (M Type) -> MetaM (Core (M Pat))
repPtype (MkC CoreExpr
t) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
typePName [CoreExpr
t]

repPinvis :: Core (M TH.Type) -> MetaM (Core (M TH.Pat))
repPinvis :: Core (M Type) -> MetaM (Core (M Pat))
repPinvis (MkC CoreExpr
t) = Name -> [CoreExpr] -> MetaM (Core (M Pat))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
invisPName [CoreExpr
t]

--------------- Expressions -----------------
repVarOrCon :: Name -> Core TH.Name -> MetaM (Core (M TH.Exp))
repVarOrCon :: Name -> Core Name -> MetaM (Core (M Exp))
repVarOrCon Name
vc Core Name
str
    | NameSpace -> Bool
isVarNameSpace NameSpace
ns = Core Name -> MetaM (Core (M Exp))
repVar Core Name
str  -- Both type and term variables (#18740)
    | Bool
otherwise         = Core Name -> MetaM (Core (M Exp))
repCon Core Name
str
  where
    ns :: NameSpace
ns = Name -> NameSpace
nameNameSpace Name
vc

repVar :: Core TH.Name -> MetaM (Core (M TH.Exp))
repVar :: Core Name -> MetaM (Core (M Exp))
repVar (MkC CoreExpr
s) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
varEName [CoreExpr
s]

repCon :: Core TH.Name -> MetaM (Core (M TH.Exp))
repCon :: Core Name -> MetaM (Core (M Exp))
repCon (MkC CoreExpr
s) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
conEName [CoreExpr
s]

repLit :: Core TH.Lit -> MetaM (Core (M TH.Exp))
repLit :: Core Lit -> MetaM (Core (M Exp))
repLit (MkC CoreExpr
c) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
litEName [CoreExpr
c]

repApp :: Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repApp :: Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repApp (MkC CoreExpr
x) (MkC CoreExpr
y) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
appEName [CoreExpr
x,CoreExpr
y]

repAppType :: Core (M TH.Exp) -> Core (M TH.Type) -> MetaM (Core (M TH.Exp))
repAppType :: Core (M Exp) -> Core (M Type) -> MetaM (Core (M Exp))
repAppType (MkC CoreExpr
x) (MkC CoreExpr
y) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
appTypeEName [CoreExpr
x,CoreExpr
y]

repLam :: Core [(M TH.Pat)] -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repLam :: Core [M Pat] -> Core (M Exp) -> MetaM (Core (M Exp))
repLam (MkC CoreExpr
ps) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
lamEName [CoreExpr
ps, CoreExpr
e]

repLamCase :: Core [(M TH.Match)] -> MetaM (Core (M TH.Exp))
repLamCase :: Core [M Match] -> MetaM (Core (M Exp))
repLamCase (MkC CoreExpr
ms) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
lamCaseEName [CoreExpr
ms]

repLamCases :: Core [(M TH.Clause)] -> MetaM (Core (M TH.Exp))
repLamCases :: Core [M Clause] -> MetaM (Core (M Exp))
repLamCases (MkC CoreExpr
ms) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
lamCasesEName [CoreExpr
ms]

repTup :: Core [Maybe (M TH.Exp)] -> MetaM (Core (M TH.Exp))
repTup :: Core [Maybe (M Exp)] -> MetaM (Core (M Exp))
repTup (MkC CoreExpr
es) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tupEName [CoreExpr
es]

repUnboxedTup :: Core [Maybe (M TH.Exp)] -> MetaM (Core (M TH.Exp))
repUnboxedTup :: Core [Maybe (M Exp)] -> MetaM (Core (M Exp))
repUnboxedTup (MkC CoreExpr
es) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
unboxedTupEName [CoreExpr
es]

repUnboxedSum :: Core (M TH.Exp) -> TH.SumAlt -> TH.SumArity -> MetaM (Core (M TH.Exp))
-- Note: not Core TH.SumAlt or Core TH.SumArity; it's easier to be direct here
repUnboxedSum :: Core (M Exp) -> Int -> Int -> MetaM (Core (M Exp))
repUnboxedSum (MkC CoreExpr
e) Int
alt Int
arity
 = do { platform <- MetaM Platform
getPlatform
      ; rep2 unboxedSumEName [ e
                             , mkIntExprInt platform alt
                             , mkIntExprInt platform arity ] }

repCond :: Core (M TH.Exp) -> Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repCond :: Core (M Exp)
-> Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repCond (MkC CoreExpr
x) (MkC CoreExpr
y) (MkC CoreExpr
z) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
condEName [CoreExpr
x,CoreExpr
y,CoreExpr
z]

repMultiIf :: Core [M (TH.Guard, TH.Exp)] -> MetaM (Core (M TH.Exp))
repMultiIf :: Core [M (Guard, Exp)] -> MetaM (Core (M Exp))
repMultiIf (MkC CoreExpr
alts) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
multiIfEName [CoreExpr
alts]

repLetE :: Core [(M TH.Dec)] -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repLetE :: Core [M Dec] -> Core (M Exp) -> MetaM (Core (M Exp))
repLetE (MkC CoreExpr
ds) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
letEName [CoreExpr
ds, CoreExpr
e]

repCaseE :: Core (M TH.Exp) -> Core [(M TH.Match)] -> MetaM (Core (M TH.Exp))
repCaseE :: Core (M Exp) -> Core [M Match] -> MetaM (Core (M Exp))
repCaseE (MkC CoreExpr
e) (MkC CoreExpr
ms) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
caseEName [CoreExpr
e, CoreExpr
ms]

repDoE :: Maybe ModuleName -> Core [(M TH.Stmt)] -> MetaM (Core (M TH.Exp))
repDoE :: Maybe ModuleName -> Core [M Stmt] -> MetaM (Core (M Exp))
repDoE = Name -> Maybe ModuleName -> Core [M Stmt] -> MetaM (Core (M Exp))
repDoBlock Name
doEName

repMDoE :: Maybe ModuleName -> Core [(M TH.Stmt)] -> MetaM (Core (M TH.Exp))
repMDoE :: Maybe ModuleName -> Core [M Stmt] -> MetaM (Core (M Exp))
repMDoE = Name -> Maybe ModuleName -> Core [M Stmt] -> MetaM (Core (M Exp))
repDoBlock Name
mdoEName

repDoBlock :: Name -> Maybe ModuleName -> Core [(M TH.Stmt)] -> MetaM (Core (M TH.Exp))
repDoBlock :: Name -> Maybe ModuleName -> Core [M Stmt] -> MetaM (Core (M Exp))
repDoBlock Name
doName Maybe ModuleName
maybeModName (MkC CoreExpr
ss) = do
    MkC coreModName <- MetaM (Core (Maybe ModName))
coreModNameM
    rep2 doName [coreModName, ss]
  where
    coreModNameM :: MetaM (Core (Maybe TH.ModName))
    coreModNameM :: MetaM (Core (Maybe ModName))
coreModNameM = case Maybe ModuleName
maybeModName of
      Just ModuleName
m -> do
        MkC s <- FastString -> ReaderT MetaWrappers DsM (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit (ModuleName -> FastString
moduleNameFS ModuleName
m)
        mName <- rep2_nw mkModNameName [s]
        coreJust modNameTyConName mName
      Maybe ModuleName
_ -> Name -> MetaM (Core (Maybe ModName))
forall a. Name -> MetaM (Core (Maybe a))
coreNothing Name
modNameTyConName

repComp :: Core [(M TH.Stmt)] -> MetaM (Core (M TH.Exp))
repComp :: Core [M Stmt] -> MetaM (Core (M Exp))
repComp (MkC CoreExpr
ss) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
compEName [CoreExpr
ss]

repListExp :: Core [(M TH.Exp)] -> MetaM (Core (M TH.Exp))
repListExp :: Core [M Exp] -> MetaM (Core (M Exp))
repListExp (MkC CoreExpr
es) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
listEName [CoreExpr
es]

repSigExp :: Core (M TH.Exp) -> Core (M TH.Type) -> MetaM (Core (M TH.Exp))
repSigExp :: Core (M Exp) -> Core (M Type) -> MetaM (Core (M Exp))
repSigExp (MkC CoreExpr
e) (MkC CoreExpr
t) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sigEName [CoreExpr
e,CoreExpr
t]

repRecCon :: Core TH.Name -> Core [M TH.FieldExp]-> MetaM (Core (M TH.Exp))
repRecCon :: Core Name -> Core [M FieldExp] -> MetaM (Core (M Exp))
repRecCon (MkC CoreExpr
c) (MkC CoreExpr
fs) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
recConEName [CoreExpr
c,CoreExpr
fs]

repRecUpd :: Core (M TH.Exp) -> Core [M TH.FieldExp] -> MetaM (Core (M TH.Exp))
repRecUpd :: Core (M Exp) -> Core [M FieldExp] -> MetaM (Core (M Exp))
repRecUpd (MkC CoreExpr
e) (MkC CoreExpr
fs) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
recUpdEName [CoreExpr
e,CoreExpr
fs]

repFieldExp :: Core TH.Name -> Core (M TH.Exp) -> MetaM (Core (M TH.FieldExp))
repFieldExp :: Core Name -> Core (M Exp) -> MetaM (Core (M FieldExp))
repFieldExp (MkC CoreExpr
n) (MkC CoreExpr
x) = Name -> [CoreExpr] -> MetaM (Core (M FieldExp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
fieldExpName [CoreExpr
n,CoreExpr
x]

repInfixApp :: Core (M TH.Exp) -> Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repInfixApp :: Core (M Exp)
-> Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repInfixApp (MkC CoreExpr
x) (MkC CoreExpr
y) (MkC CoreExpr
z) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
infixAppName [CoreExpr
x,CoreExpr
y,CoreExpr
z]

repSectionL :: Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repSectionL :: Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repSectionL (MkC CoreExpr
x) (MkC CoreExpr
y) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sectionLName [CoreExpr
x,CoreExpr
y]

repSectionR :: Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repSectionR :: Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repSectionR (MkC CoreExpr
x) (MkC CoreExpr
y) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sectionRName [CoreExpr
x,CoreExpr
y]

repImplicitParamVar :: Core String -> MetaM (Core (M TH.Exp))
repImplicitParamVar :: Core String -> MetaM (Core (M Exp))
repImplicitParamVar (MkC CoreExpr
x) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
implicitParamVarEName [CoreExpr
x]

------------ Right hand sides (guarded expressions) ----
repGuarded :: Core [M (TH.Guard, TH.Exp)] -> MetaM (Core (M TH.Body))
repGuarded :: Core [M (Guard, Exp)] -> MetaM (Core (M Body))
repGuarded (MkC CoreExpr
pairs) = Name -> [CoreExpr] -> MetaM (Core (M Body))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
guardedBName [CoreExpr
pairs]

repNormal :: Core (M TH.Exp) -> MetaM (Core (M TH.Body))
repNormal :: Core (M Exp) -> MetaM (Core (M Body))
repNormal (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Body))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
normalBName [CoreExpr
e]

------------ Guards ----
repLNormalGE :: LHsExpr GhcRn -> LHsExpr GhcRn
             -> MetaM (Core (M (TH.Guard, TH.Exp)))
repLNormalGE :: LHsExpr GhcRn -> LHsExpr GhcRn -> MetaM (Core (M (Guard, Exp)))
repLNormalGE LHsExpr GhcRn
g LHsExpr GhcRn
e = do g' <- LHsExpr GhcRn -> MetaM (Core (M Exp))
repLE LHsExpr GhcRn
g
                      e' <- repLE e
                      repNormalGE g' e'

repNormalGE :: Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M (TH.Guard, TH.Exp)))
repNormalGE :: Core (M Exp) -> Core (M Exp) -> MetaM (Core (M (Guard, Exp)))
repNormalGE (MkC CoreExpr
g) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M (Guard, Exp)))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
normalGEName [CoreExpr
g, CoreExpr
e]

repPatGE :: Core [(M TH.Stmt)] -> Core (M TH.Exp) -> MetaM (Core (M (TH.Guard, TH.Exp)))
repPatGE :: Core [M Stmt] -> Core (M Exp) -> MetaM (Core (M (Guard, Exp)))
repPatGE (MkC CoreExpr
ss) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M (Guard, Exp)))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
patGEName [CoreExpr
ss, CoreExpr
e]

------------- Stmts -------------------
repBindSt :: Core (M TH.Pat) -> Core (M TH.Exp) -> MetaM (Core (M TH.Stmt))
repBindSt :: Core (M Pat) -> Core (M Exp) -> MetaM (Core (M Stmt))
repBindSt (MkC CoreExpr
p) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Stmt))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
bindSName [CoreExpr
p,CoreExpr
e]

repLetSt :: Core [(M TH.Dec)] -> MetaM (Core (M TH.Stmt))
repLetSt :: Core [M Dec] -> MetaM (Core (M Stmt))
repLetSt (MkC CoreExpr
ds) = Name -> [CoreExpr] -> MetaM (Core (M Stmt))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
letSName [CoreExpr
ds]

repNoBindSt :: Core (M TH.Exp) -> MetaM (Core (M TH.Stmt))
repNoBindSt :: Core (M Exp) -> MetaM (Core (M Stmt))
repNoBindSt (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Stmt))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
noBindSName [CoreExpr
e]

repParSt :: Core [[(M TH.Stmt)]] -> MetaM (Core (M TH.Stmt))
repParSt :: Core [[M Stmt]] -> MetaM (Core (M Stmt))
repParSt (MkC CoreExpr
sss) = Name -> [CoreExpr] -> MetaM (Core (M Stmt))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
parSName [CoreExpr
sss]

repRecSt :: Core [(M TH.Stmt)] -> MetaM (Core (M TH.Stmt))
repRecSt :: Core [M Stmt] -> MetaM (Core (M Stmt))
repRecSt (MkC CoreExpr
ss) = Name -> [CoreExpr] -> MetaM (Core (M Stmt))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
recSName [CoreExpr
ss]

-------------- Range (Arithmetic sequences) -----------
repFrom :: Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repFrom :: Core (M Exp) -> MetaM (Core (M Exp))
repFrom (MkC CoreExpr
x) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
fromEName [CoreExpr
x]

repFromThen :: Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repFromThen :: Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repFromThen (MkC CoreExpr
x) (MkC CoreExpr
y) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
fromThenEName [CoreExpr
x,CoreExpr
y]

repFromTo :: Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repFromTo :: Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repFromTo (MkC CoreExpr
x) (MkC CoreExpr
y) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
fromToEName [CoreExpr
x,CoreExpr
y]

repFromThenTo :: Core (M TH.Exp) -> Core (M TH.Exp) -> Core (M TH.Exp) -> MetaM (Core (M TH.Exp))
repFromThenTo :: Core (M Exp)
-> Core (M Exp) -> Core (M Exp) -> MetaM (Core (M Exp))
repFromThenTo (MkC CoreExpr
x) (MkC CoreExpr
y) (MkC CoreExpr
z) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
fromThenToEName [CoreExpr
x,CoreExpr
y,CoreExpr
z]

------------ Match and Clause Tuples -----------
repMatch :: Core (M TH.Pat) -> Core (M TH.Body) -> Core [(M TH.Dec)] -> MetaM (Core (M TH.Match))
repMatch :: Core (M Pat)
-> Core (M Body)
-> Core [M Dec]
-> ReaderT MetaWrappers DsM (Core (M Match))
repMatch (MkC CoreExpr
p) (MkC CoreExpr
bod) (MkC CoreExpr
ds) = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M Match))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
matchName [CoreExpr
p, CoreExpr
bod, CoreExpr
ds]

repClause :: Core [(M TH.Pat)] -> Core (M TH.Body) -> Core [(M TH.Dec)] -> MetaM (Core (M TH.Clause))
repClause :: Core [M Pat]
-> Core (M Body)
-> Core [M Dec]
-> ReaderT MetaWrappers DsM (Core (M Clause))
repClause (MkC CoreExpr
ps) (MkC CoreExpr
bod) (MkC CoreExpr
ds) = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M Clause))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
clauseName [CoreExpr
ps, CoreExpr
bod, CoreExpr
ds]

-------------- Dec -----------------------------
repVal :: Core (M TH.Pat) -> Core (M TH.Body) -> Core [(M TH.Dec)] -> MetaM (Core (M TH.Dec))
repVal :: Core (M Pat)
-> Core (M Body) -> Core [M Dec] -> MetaM (Core (M Dec))
repVal (MkC CoreExpr
p) (MkC CoreExpr
b) (MkC CoreExpr
ds) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
valDName [CoreExpr
p, CoreExpr
b, CoreExpr
ds]

repFun :: Core TH.Name -> Core [(M TH.Clause)] -> MetaM (Core (M TH.Dec))
repFun :: Core Name -> Core [M Clause] -> MetaM (Core (M Dec))
repFun (MkC CoreExpr
nm) (MkC CoreExpr
b) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
funDName [CoreExpr
nm, CoreExpr
b]

repData :: Bool -- ^ @True@ for a @type data@ declaration.
                -- See Note [Type data declarations] in GHC.Rename.Module
        -> Core (M TH.Cxt) -> Core TH.Name
        -> Either (Core [(M (TH.TyVarBndr TH.BndrVis))])
                  (Core (Maybe [(M (TH.TyVarBndr ()))]), Core (M TH.Type))
        -> Core (Maybe (M TH.Kind)) -> Core [(M TH.Con)] -> Core [M TH.DerivClause]
        -> MetaM (Core (M TH.Dec))
repData :: Bool
-> Core (M Cxt)
-> Core Name
-> Either
     (Core [M (TyVarBndr BndrVis)])
     (Core (Maybe [M (TyVarBndr ())]), Core (M Type))
-> Core (Maybe (M Type))
-> Core [M Con]
-> Core [M DerivClause]
-> MetaM (Core (M Dec))
repData Bool
type_data (MkC CoreExpr
cxt) (MkC CoreExpr
nm) (Left (MkC CoreExpr
tvs)) (MkC CoreExpr
ksig) (MkC CoreExpr
cons) (MkC CoreExpr
derivs)
  | Bool
type_data = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
typeDataDName [CoreExpr
nm, CoreExpr
tvs, CoreExpr
ksig, CoreExpr
cons]
  | Bool
otherwise = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
dataDName [CoreExpr
cxt, CoreExpr
nm, CoreExpr
tvs, CoreExpr
ksig, CoreExpr
cons, CoreExpr
derivs]
repData Bool
_ (MkC CoreExpr
cxt) (MkC CoreExpr
_) (Right (MkC CoreExpr
mb_bndrs, MkC CoreExpr
ty)) (MkC CoreExpr
ksig) (MkC CoreExpr
cons)
        (MkC CoreExpr
derivs)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
dataInstDName [CoreExpr
cxt, CoreExpr
mb_bndrs, CoreExpr
ty, CoreExpr
ksig, CoreExpr
cons, CoreExpr
derivs]

repNewtype :: Core (M TH.Cxt) -> Core TH.Name
           -> Either (Core [(M (TH.TyVarBndr TH.BndrVis))])
                     (Core (Maybe [(M (TH.TyVarBndr ()))]), Core (M TH.Type))
           -> Core (Maybe (M TH.Kind)) -> Core (M TH.Con) -> Core [M TH.DerivClause]
           -> MetaM (Core (M TH.Dec))
repNewtype :: Core (M Cxt)
-> Core Name
-> Either
     (Core [M (TyVarBndr BndrVis)])
     (Core (Maybe [M (TyVarBndr ())]), Core (M Type))
-> Core (Maybe (M Type))
-> Core (M Con)
-> Core [M DerivClause]
-> MetaM (Core (M Dec))
repNewtype (MkC CoreExpr
cxt) (MkC CoreExpr
nm) (Left (MkC CoreExpr
tvs)) (MkC CoreExpr
ksig) (MkC CoreExpr
con)
           (MkC CoreExpr
derivs)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
newtypeDName [CoreExpr
cxt, CoreExpr
nm, CoreExpr
tvs, CoreExpr
ksig, CoreExpr
con, CoreExpr
derivs]
repNewtype (MkC CoreExpr
cxt) (MkC CoreExpr
_) (Right (MkC CoreExpr
mb_bndrs, MkC CoreExpr
ty)) (MkC CoreExpr
ksig) (MkC CoreExpr
con)
           (MkC CoreExpr
derivs)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
newtypeInstDName [CoreExpr
cxt, CoreExpr
mb_bndrs, CoreExpr
ty, CoreExpr
ksig, CoreExpr
con, CoreExpr
derivs]

repTySyn :: Core TH.Name -> Core [(M (TH.TyVarBndr TH.BndrVis))]
         -> Core (M TH.Type) -> MetaM (Core (M TH.Dec))
repTySyn :: Core Name
-> Core [M (TyVarBndr BndrVis)]
-> Core (M Type)
-> MetaM (Core (M Dec))
repTySyn (MkC CoreExpr
nm) (MkC CoreExpr
tvs) (MkC CoreExpr
rhs)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tySynDName [CoreExpr
nm, CoreExpr
tvs, CoreExpr
rhs]

repInst :: Core (Maybe TH.Overlap) ->
           Core (M TH.Cxt) -> Core (M TH.Type) -> Core [(M TH.Dec)] -> MetaM (Core (M TH.Dec))
repInst :: Core (Maybe Overlap)
-> Core (M Cxt)
-> Core (M Type)
-> Core [M Dec]
-> MetaM (Core (M Dec))
repInst (MkC CoreExpr
o) (MkC CoreExpr
cxt) (MkC CoreExpr
ty) (MkC CoreExpr
ds) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
instanceWithOverlapDName
                                                              [CoreExpr
o, CoreExpr
cxt, CoreExpr
ty, CoreExpr
ds]

repDerivStrategy :: Maybe (LDerivStrategy GhcRn)
                 -> (Core (Maybe (M TH.DerivStrategy)) -> MetaM (Core (M a)))
                 -> MetaM (Core (M a))
repDerivStrategy :: forall {k} (a :: k).
Maybe (LDerivStrategy GhcRn)
-> (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> MetaM (Core (M a))
repDerivStrategy Maybe (LDerivStrategy GhcRn)
mds Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a))
thing_inside =
  case Maybe (LDerivStrategy GhcRn)
mds of
    Maybe (LDerivStrategy GhcRn)
Nothing -> Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a))
thing_inside (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
-> MetaM (Core (M a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall {a}. MetaM (Core (Maybe a))
nothing
    Just LDerivStrategy GhcRn
ds ->
      case GenLocated EpAnnCO (DerivStrategy GhcRn) -> DerivStrategy GhcRn
forall l e. GenLocated l e -> e
unLoc LDerivStrategy GhcRn
GenLocated EpAnnCO (DerivStrategy GhcRn)
ds of
        StockStrategy    XStockStrategy GhcRn
_ -> Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a))
thing_inside (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
-> MetaM (Core (M a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Core (M DerivStrategy)
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core (M DerivStrategy)
 -> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy))))
-> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repStockStrategy
        AnyclassStrategy XAnyClassStrategy GhcRn
_ -> Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a))
thing_inside (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
-> MetaM (Core (M a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Core (M DerivStrategy)
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core (M DerivStrategy)
 -> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy))))
-> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repAnyclassStrategy
        NewtypeStrategy  XNewtypeStrategy GhcRn
_ -> Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a))
thing_inside (Core (Maybe (M DerivStrategy)) -> MetaM (Core (M a)))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
-> MetaM (Core (M a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Core (M DerivStrategy)
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core (M DerivStrategy)
 -> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy))))
-> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
-> ReaderT MetaWrappers DsM (Core (Maybe (M DerivStrategy)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repNewtypeStrategy
        ViaStrategy XViaStrategy GhcRn
ty     -> FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
forall {k} (a :: k).
FreshOrReuse -> [Name] -> MetaM (Core (M a)) -> MetaM (Core (M a))
addSimpleTyVarBinds FreshOrReuse
FreshNamesOnly (LHsSigType GhcRn -> [Name]
get_scoped_tvs_from_sig XViaStrategy GhcRn
LHsSigType GhcRn
ty) (MetaM (Core (M a)) -> MetaM (Core (M a)))
-> MetaM (Core (M a)) -> MetaM (Core (M a))
forall a b. (a -> b) -> a -> b
$
                              do ty' <- LHsSigType GhcRn -> MetaM (Core (M Type))
rep_ty_sig' XViaStrategy GhcRn
LHsSigType GhcRn
ty
                                 via_strat <- repViaStrategy ty'
                                 m_via_strat <- just via_strat
                                 thing_inside m_via_strat
  where
  nothing :: MetaM (Core (Maybe a))
nothing = Name -> MetaM (Core (Maybe a))
forall a. Name -> MetaM (Core (Maybe a))
coreNothingM Name
derivStrategyTyConName
  just :: Core a -> MetaM (Core (Maybe a))
just    = Name -> Core a -> MetaM (Core (Maybe a))
forall a. Name -> Core a -> MetaM (Core (Maybe a))
coreJustM    Name
derivStrategyTyConName

repStockStrategy :: MetaM (Core (M TH.DerivStrategy))
repStockStrategy :: ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repStockStrategy = Name
-> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
stockStrategyName []

repAnyclassStrategy :: MetaM (Core (M TH.DerivStrategy))
repAnyclassStrategy :: ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repAnyclassStrategy = Name
-> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
anyclassStrategyName []

repNewtypeStrategy :: MetaM (Core (M TH.DerivStrategy))
repNewtypeStrategy :: ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repNewtypeStrategy = Name
-> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
newtypeStrategyName []

repViaStrategy :: Core (M TH.Type) -> MetaM (Core (M TH.DerivStrategy))
repViaStrategy :: Core (M Type) -> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
repViaStrategy (MkC CoreExpr
t) = Name
-> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M DerivStrategy))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
viaStrategyName [CoreExpr
t]

repOverlap :: Maybe OverlapMode -> MetaM (Core (Maybe TH.Overlap))
repOverlap :: Maybe OverlapMode -> MetaM (Core (Maybe Overlap))
repOverlap Maybe OverlapMode
mb =
  case Maybe OverlapMode
mb of
    Maybe OverlapMode
Nothing -> MetaM (Core (Maybe Overlap))
forall {a}. MetaM (Core (Maybe a))
nothing
    Just OverlapMode
o ->
      case OverlapMode
o of
        NoOverlap SourceText
_    -> MetaM (Core (Maybe Overlap))
forall {a}. MetaM (Core (Maybe a))
nothing
        Overlappable SourceText
_ -> Core Overlap -> MetaM (Core (Maybe Overlap))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core Overlap -> MetaM (Core (Maybe Overlap)))
-> ReaderT MetaWrappers DsM (Core Overlap)
-> MetaM (Core (Maybe Overlap))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Name -> ReaderT MetaWrappers DsM (Core Overlap)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
overlappableDataConName
        Overlapping SourceText
_  -> Core Overlap -> MetaM (Core (Maybe Overlap))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core Overlap -> MetaM (Core (Maybe Overlap)))
-> ReaderT MetaWrappers DsM (Core Overlap)
-> MetaM (Core (Maybe Overlap))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Name -> ReaderT MetaWrappers DsM (Core Overlap)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
overlappingDataConName
        Overlaps SourceText
_     -> Core Overlap -> MetaM (Core (Maybe Overlap))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core Overlap -> MetaM (Core (Maybe Overlap)))
-> ReaderT MetaWrappers DsM (Core Overlap)
-> MetaM (Core (Maybe Overlap))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Name -> ReaderT MetaWrappers DsM (Core Overlap)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
overlapsDataConName
        Incoherent SourceText
_   -> Core Overlap -> MetaM (Core (Maybe Overlap))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core Overlap -> MetaM (Core (Maybe Overlap)))
-> ReaderT MetaWrappers DsM (Core Overlap)
-> MetaM (Core (Maybe Overlap))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Name -> ReaderT MetaWrappers DsM (Core Overlap)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
incoherentDataConName
        NonCanonical SourceText
_ -> Core Overlap -> MetaM (Core (Maybe Overlap))
forall {a}. Core a -> MetaM (Core (Maybe a))
just (Core Overlap -> MetaM (Core (Maybe Overlap)))
-> ReaderT MetaWrappers DsM (Core Overlap)
-> MetaM (Core (Maybe Overlap))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Name -> ReaderT MetaWrappers DsM (Core Overlap)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
incoherentDataConName
  where
  nothing :: MetaM (Core (Maybe a))
nothing = Name -> MetaM (Core (Maybe a))
forall a. Name -> MetaM (Core (Maybe a))
coreNothing Name
overlapTyConName
  just :: Core a -> MetaM (Core (Maybe a))
just    = Name -> Core a -> MetaM (Core (Maybe a))
forall a. Name -> Core a -> MetaM (Core (Maybe a))
coreJust Name
overlapTyConName


repNamespaceSpecifier :: NamespaceSpecifier -> MetaM (Core (TH.NamespaceSpecifier))
repNamespaceSpecifier :: NamespaceSpecifier -> MetaM (Core NamespaceSpecifier)
repNamespaceSpecifier NamespaceSpecifier
ns_spec = case NamespaceSpecifier
ns_spec of
  NoNamespaceSpecifier{} -> Name -> MetaM (Core NamespaceSpecifier)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
noNamespaceSpecifierDataConName
  TypeNamespaceSpecifier{} -> Name -> MetaM (Core NamespaceSpecifier)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
typeNamespaceSpecifierDataConName
  DataNamespaceSpecifier{} -> Name -> MetaM (Core NamespaceSpecifier)
forall {k} (a :: k). Name -> MetaM (Core a)
dataCon Name
dataNamespaceSpecifierDataConName

repClass :: Core (M TH.Cxt) -> Core TH.Name -> Core [(M (TH.TyVarBndr TH.BndrVis))]
         -> Core [TH.FunDep] -> Core [(M TH.Dec)]
         -> MetaM (Core (M TH.Dec))
repClass :: Core (M Cxt)
-> Core Name
-> Core [M (TyVarBndr BndrVis)]
-> Core [FunDep]
-> Core [M Dec]
-> MetaM (Core (M Dec))
repClass (MkC CoreExpr
cxt) (MkC CoreExpr
cls) (MkC CoreExpr
tvs) (MkC CoreExpr
fds) (MkC CoreExpr
ds)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
classDName [CoreExpr
cxt, CoreExpr
cls, CoreExpr
tvs, CoreExpr
fds, CoreExpr
ds]

repDeriv :: Core (Maybe (M TH.DerivStrategy))
         -> Core (M TH.Cxt) -> Core (M TH.Type)
         -> MetaM (Core (M TH.Dec))
repDeriv :: Core (Maybe (M DerivStrategy))
-> Core (M Cxt) -> Core (M Type) -> MetaM (Core (M Dec))
repDeriv (MkC CoreExpr
ds) (MkC CoreExpr
cxt) (MkC CoreExpr
ty)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
standaloneDerivWithStrategyDName [CoreExpr
ds, CoreExpr
cxt, CoreExpr
ty]

repPragInl :: Core TH.Name -> Core TH.Inline -> Core TH.RuleMatch
           -> Core TH.Phases -> MetaM (Core (M TH.Dec))
repPragInl :: Core Name
-> Core Inline
-> Core RuleMatch
-> Core Phases
-> MetaM (Core (M Dec))
repPragInl (MkC CoreExpr
nm) (MkC CoreExpr
inline) (MkC CoreExpr
rm) (MkC CoreExpr
phases)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragInlDName [CoreExpr
nm, CoreExpr
inline, CoreExpr
rm, CoreExpr
phases]

repPragOpaque :: Core TH.Name -> MetaM (Core (M TH.Dec))
repPragOpaque :: Core Name -> MetaM (Core (M Dec))
repPragOpaque (MkC CoreExpr
nm) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragOpaqueDName [CoreExpr
nm]

repPragSpec :: Core TH.Name -> Core (M TH.Type) -> Core TH.Phases
            -> MetaM (Core (M TH.Dec))
repPragSpec :: Core Name -> Core (M Type) -> Core Phases -> MetaM (Core (M Dec))
repPragSpec (MkC CoreExpr
nm) (MkC CoreExpr
ty) (MkC CoreExpr
phases)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragSpecDName [CoreExpr
nm, CoreExpr
ty, CoreExpr
phases]

repPragSpecInl :: Core TH.Name -> Core (M TH.Type) -> Core TH.Inline
               -> Core TH.Phases -> MetaM (Core (M TH.Dec))
repPragSpecInl :: Core Name
-> Core (M Type)
-> Core Inline
-> Core Phases
-> MetaM (Core (M Dec))
repPragSpecInl (MkC CoreExpr
nm) (MkC CoreExpr
ty) (MkC CoreExpr
inline) (MkC CoreExpr
phases)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragSpecInlDName [CoreExpr
nm, CoreExpr
ty, CoreExpr
inline, CoreExpr
phases]

repPragSpecInst :: Core (M TH.Type) -> MetaM (Core (M TH.Dec))
repPragSpecInst :: Core (M Type) -> MetaM (Core (M Dec))
repPragSpecInst (MkC CoreExpr
ty) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragSpecInstDName [CoreExpr
ty]

repPragComplete :: Core [TH.Name] -> Core (Maybe TH.Name) -> MetaM (Core (M TH.Dec))
repPragComplete :: Core [Name] -> Core (Maybe Name) -> MetaM (Core (M Dec))
repPragComplete (MkC CoreExpr
cls) (MkC CoreExpr
mty) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragCompleteDName [CoreExpr
cls, CoreExpr
mty]

repPragRule :: Core String -> Core (Maybe [(M (TH.TyVarBndr ()))])
            -> Core [(M TH.RuleBndr)] -> Core (M TH.Exp) -> Core (M TH.Exp)
            -> Core TH.Phases -> MetaM (Core (M TH.Dec))
repPragRule :: Core String
-> Core (Maybe [M (TyVarBndr ())])
-> Core [M RuleBndr]
-> Core (M Exp)
-> Core (M Exp)
-> Core Phases
-> MetaM (Core (M Dec))
repPragRule (MkC CoreExpr
nm) (MkC CoreExpr
ty_bndrs) (MkC CoreExpr
tm_bndrs) (MkC CoreExpr
lhs) (MkC CoreExpr
rhs) (MkC CoreExpr
phases)
  = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragRuleDName [CoreExpr
nm, CoreExpr
ty_bndrs, CoreExpr
tm_bndrs, CoreExpr
lhs, CoreExpr
rhs, CoreExpr
phases]

repPragAnn :: Core TH.AnnTarget -> Core (M TH.Exp) -> MetaM (Core (M TH.Dec))
repPragAnn :: Core AnnTarget -> Core (M Exp) -> MetaM (Core (M Dec))
repPragAnn (MkC CoreExpr
targ) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragAnnDName [CoreExpr
targ, CoreExpr
e]

repPragSCCFun :: Core TH.Name -> MetaM (Core (M TH.Dec))
repPragSCCFun :: Core Name -> MetaM (Core (M Dec))
repPragSCCFun (MkC CoreExpr
nm) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragSCCFunDName [CoreExpr
nm]

repPragSCCFunNamed :: Core TH.Name -> Core String -> MetaM (Core (M TH.Dec))
repPragSCCFunNamed :: Core Name -> Core String -> MetaM (Core (M Dec))
repPragSCCFunNamed (MkC CoreExpr
nm) (MkC CoreExpr
str) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
pragSCCFunNamedDName [CoreExpr
nm, CoreExpr
str]

repTySynInst :: Core (M TH.TySynEqn) -> MetaM (Core (M TH.Dec))
repTySynInst :: Core (M TySynEqn) -> MetaM (Core (M Dec))
repTySynInst (MkC CoreExpr
eqn)
    = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tySynInstDName [CoreExpr
eqn]

repDataFamilyD :: Core TH.Name -> Core [(M (TH.TyVarBndr TH.BndrVis))]
               -> Core (Maybe (M TH.Kind)) -> MetaM (Core (M TH.Dec))
repDataFamilyD :: Core Name
-> Core [M (TyVarBndr BndrVis)]
-> Core (Maybe (M Type))
-> MetaM (Core (M Dec))
repDataFamilyD (MkC CoreExpr
nm) (MkC CoreExpr
tvs) (MkC CoreExpr
kind)
    = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
dataFamilyDName [CoreExpr
nm, CoreExpr
tvs, CoreExpr
kind]

repOpenFamilyD :: Core TH.Name
               -> Core [(M (TH.TyVarBndr TH.BndrVis))]
               -> Core (M TH.FamilyResultSig)
               -> Core (Maybe TH.InjectivityAnn)
               -> MetaM (Core (M TH.Dec))
repOpenFamilyD :: Core Name
-> Core [M (TyVarBndr BndrVis)]
-> Core (M FamilyResultSig)
-> Core (Maybe InjectivityAnn)
-> MetaM (Core (M Dec))
repOpenFamilyD (MkC CoreExpr
nm) (MkC CoreExpr
tvs) (MkC CoreExpr
result) (MkC CoreExpr
inj)
    = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
openTypeFamilyDName [CoreExpr
nm, CoreExpr
tvs, CoreExpr
result, CoreExpr
inj]

repClosedFamilyD :: Core TH.Name
                 -> Core [(M (TH.TyVarBndr TH.BndrVis))]
                 -> Core (M TH.FamilyResultSig)
                 -> Core (Maybe TH.InjectivityAnn)
                 -> Core [(M TH.TySynEqn)]
                 -> MetaM (Core (M TH.Dec))
repClosedFamilyD :: Core Name
-> Core [M (TyVarBndr BndrVis)]
-> Core (M FamilyResultSig)
-> Core (Maybe InjectivityAnn)
-> Core [M TySynEqn]
-> MetaM (Core (M Dec))
repClosedFamilyD (MkC CoreExpr
nm) (MkC CoreExpr
tvs) (MkC CoreExpr
res) (MkC CoreExpr
inj) (MkC CoreExpr
eqns)
    = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
closedTypeFamilyDName [CoreExpr
nm, CoreExpr
tvs, CoreExpr
res, CoreExpr
inj, CoreExpr
eqns]

repTySynEqn :: Core (Maybe [(M (TH.TyVarBndr ()))]) ->
               Core (M TH.Type) -> Core (M TH.Type) -> MetaM (Core (M TH.TySynEqn))
repTySynEqn :: Core (Maybe [M (TyVarBndr ())])
-> Core (M Type)
-> Core (M Type)
-> ReaderT MetaWrappers DsM (Core (M TySynEqn))
repTySynEqn (MkC CoreExpr
mb_bndrs) (MkC CoreExpr
lhs) (MkC CoreExpr
rhs)
  = Name -> [CoreExpr] -> ReaderT MetaWrappers DsM (Core (M TySynEqn))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tySynEqnName [CoreExpr
mb_bndrs, CoreExpr
lhs, CoreExpr
rhs]

repRoleAnnotD :: Core TH.Name -> Core [TH.Role] -> MetaM (Core (M TH.Dec))
repRoleAnnotD :: Core Name -> Core [Role] -> MetaM (Core (M Dec))
repRoleAnnotD (MkC CoreExpr
n) (MkC CoreExpr
roles) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
roleAnnotDName [CoreExpr
n, CoreExpr
roles]

repFunDep :: Core [TH.Name] -> Core [TH.Name] -> MetaM (Core TH.FunDep)
repFunDep :: Core [Name] -> Core [Name] -> MetaM (Core FunDep)
repFunDep (MkC CoreExpr
xs) (MkC CoreExpr
ys) = Name -> [CoreExpr] -> MetaM (Core FunDep)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
funDepName [CoreExpr
xs, CoreExpr
ys]

repProto :: Name -> Core TH.Name -> Core (M TH.Type) -> MetaM (Core (M TH.Dec))
repProto :: Name -> Core Name -> Core (M Type) -> MetaM (Core (M Dec))
repProto Name
mk_sig (MkC CoreExpr
s) (MkC CoreExpr
ty) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
mk_sig [CoreExpr
s, CoreExpr
ty]

repImplicitParamBind :: Core String -> Core (M TH.Exp) -> MetaM (Core (M TH.Dec))
repImplicitParamBind :: Core String -> Core (M Exp) -> MetaM (Core (M Dec))
repImplicitParamBind (MkC CoreExpr
n) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Dec))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
implicitParamBindDName [CoreExpr
n, CoreExpr
e]

repCtxt :: Core [(M TH.Pred)] -> MetaM (Core (M TH.Cxt))
repCtxt :: Core [M Type] -> MetaM (Core (M Cxt))
repCtxt (MkC CoreExpr
tys) = Name -> [CoreExpr] -> MetaM (Core (M Cxt))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
cxtName [CoreExpr
tys]

repH98DataCon :: LocatedN Name
              -> HsConDeclH98Details GhcRn
              -> MetaM (Core (M TH.Con))
repH98DataCon :: GenLocated SrcSpanAnnN Name
-> HsConDeclH98Details GhcRn -> MetaM (Core (M Con))
repH98DataCon GenLocated SrcSpanAnnN Name
con HsConDeclH98Details GhcRn
details
    = do con' <- GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc GenLocated SrcSpanAnnN Name
con -- See Note [Binders and occurrences]
         case details of
           PrefixCon [Void]
_ [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps -> do
             arg_tys <- [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
-> MetaM (Core [M BangType])
repPrefixConArgs [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps
             rep2 normalCName [unC con', unC arg_tys]
           InfixCon HsScaled GhcRn (XRec GhcRn (HsType GhcRn))
st1 HsScaled GhcRn (XRec GhcRn (HsType GhcRn))
st2 -> do
             [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
-> ReaderT MetaWrappers DsM ()
verifyLinearFields [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))
st1, HsScaled GhcRn (XRec GhcRn (HsType GhcRn))
st2]
             arg1 <- XRec GhcRn (HsType GhcRn) -> MetaM (Core (M BangType))
repBangTy (HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
-> GenLocated SrcSpanAnnA (HsType GhcRn)
forall pass a. HsScaled pass a -> a
hsScaledThing HsScaled GhcRn (XRec GhcRn (HsType GhcRn))
HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
st1)
             arg2 <- repBangTy (hsScaledThing st2)
             rep2 infixCName [unC arg1, unC con', unC arg2]
           RecCon XRec GhcRn [LConDeclField GhcRn]
ips -> do
             arg_vtys <- LocatedL [LConDeclField GhcRn] -> MetaM (Core [M VarBangType])
repRecConArgs XRec GhcRn [LConDeclField GhcRn]
LocatedL [LConDeclField GhcRn]
ips
             rep2 recCName [unC con', unC arg_vtys]

repGadtDataCons :: NonEmpty (LocatedN Name)
                -> HsConDeclGADTDetails GhcRn
                -> LHsType GhcRn
                -> MetaM (Core (M TH.Con))
repGadtDataCons :: NonEmpty (GenLocated SrcSpanAnnN Name)
-> HsConDeclGADTDetails GhcRn
-> XRec GhcRn (HsType GhcRn)
-> MetaM (Core (M Con))
repGadtDataCons NonEmpty (GenLocated SrcSpanAnnN Name)
cons HsConDeclGADTDetails GhcRn
details XRec GhcRn (HsType GhcRn)
res_ty
    = do cons' <- (GenLocated SrcSpanAnnN Name -> MetaM (Core Name))
-> NonEmpty (GenLocated SrcSpanAnnN Name)
-> ReaderT MetaWrappers DsM (NonEmpty (Core Name))
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) -> NonEmpty a -> m (NonEmpty b)
mapM GenLocated SrcSpanAnnN Name -> MetaM (Core Name)
forall l. GenLocated l Name -> MetaM (Core Name)
lookupLOcc NonEmpty (GenLocated SrcSpanAnnN Name)
cons -- See Note [Binders and occurrences]
         case details of
           PrefixConGADT XPrefixConGADT GhcRn
_ [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps -> do
             arg_tys <- [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
-> MetaM (Core [M BangType])
repPrefixConArgs [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps
             res_ty' <- repLTy res_ty
             rep2 gadtCName [ unC (nonEmptyCoreList' cons'), unC arg_tys, unC res_ty']
           RecConGADT XRecConGADT GhcRn
_ XRec GhcRn [LConDeclField GhcRn]
ips -> do
             arg_vtys <- LocatedL [LConDeclField GhcRn] -> MetaM (Core [M VarBangType])
repRecConArgs XRec GhcRn [LConDeclField GhcRn]
LocatedL [LConDeclField GhcRn]
ips
             res_ty'  <- repLTy res_ty
             rep2 recGadtCName [unC (nonEmptyCoreList' cons'), unC arg_vtys,
                                unC res_ty']

-- TH currently only supports linear constructors.
-- We also accept the (->) arrow when -XLinearTypes is off, because this
-- denotes a linear field.
-- This check is not performed in repRecConArgs, since the GADT record
-- syntax currently does not have a way to mark fields as nonlinear.
verifyLinearFields :: [HsScaled GhcRn (LHsType GhcRn)] -> MetaM ()
verifyLinearFields :: [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
-> ReaderT MetaWrappers DsM ()
verifyLinearFields [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps = do
  linear <- DsM Bool -> ReaderT MetaWrappers DsM Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM Bool -> ReaderT MetaWrappers DsM Bool)
-> DsM Bool -> ReaderT MetaWrappers DsM Bool
forall a b. (a -> b) -> a -> b
$ Extension -> DsM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.LinearTypes
  let allGood = (HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)) -> Bool)
-> [HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
st -> case HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
-> HsArrow GhcRn
forall pass a. HsScaled pass a -> HsArrow pass
hsMult HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
st of
                              HsUnrestrictedArrow XUnrestrictedArrow GhcRn
_ -> Bool -> Bool
not Bool
linear
                              HsLinearArrow XLinearArrow GhcRn
_       -> Bool
True
                              HsArrow GhcRn
_                     -> Bool
False) [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
[HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))]
ps
  unless allGood $ notHandled ThNonLinearDataCon

-- Desugar the arguments in a data constructor declared with prefix syntax.
repPrefixConArgs :: [HsScaled GhcRn (LHsType GhcRn)]
                 -> MetaM (Core [M TH.BangType])
repPrefixConArgs :: [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
-> MetaM (Core [M BangType])
repPrefixConArgs [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps = do
  [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
-> ReaderT MetaWrappers DsM ()
verifyLinearFields [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
ps
  Name
-> (GenLocated SrcSpanAnnA (HsType GhcRn)
    -> MetaM (Core (M BangType)))
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> MetaM (Core [M BangType])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
bangTypeTyConName XRec GhcRn (HsType GhcRn) -> MetaM (Core (M BangType))
GenLocated SrcSpanAnnA (HsType GhcRn) -> MetaM (Core (M BangType))
repBangTy ((HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
 -> GenLocated SrcSpanAnnA (HsType GhcRn))
-> [HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))]
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
forall a b. (a -> b) -> [a] -> [b]
map HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
-> GenLocated SrcSpanAnnA (HsType GhcRn)
forall pass a. HsScaled pass a -> a
hsScaledThing [HsScaled GhcRn (XRec GhcRn (HsType GhcRn))]
[HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))]
ps)

-- Desugar the arguments in a data constructor declared with record syntax.
repRecConArgs :: LocatedL [LConDeclField GhcRn]
              -> MetaM (Core [M TH.VarBangType])
repRecConArgs :: LocatedL [LConDeclField GhcRn] -> MetaM (Core [M VarBangType])
repRecConArgs LocatedL [LConDeclField GhcRn]
ips = do
  args     <- (GenLocated SrcSpanAnnA (ConDeclField GhcRn)
 -> ReaderT MetaWrappers DsM [Core (M VarBangType)])
-> [GenLocated SrcSpanAnnA (ConDeclField GhcRn)]
-> ReaderT MetaWrappers DsM [Core (M VarBangType)]
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable f) =>
(a -> m [b]) -> f a -> m [b]
concatMapM GenLocated SrcSpanAnnA (ConDeclField GhcRn)
-> ReaderT MetaWrappers DsM [Core (M VarBangType)]
forall {l}.
GenLocated l (ConDeclField GhcRn)
-> ReaderT MetaWrappers DsM [Core (M VarBangType)]
rep_ip (GenLocated
  SrcSpanAnnL [GenLocated SrcSpanAnnA (ConDeclField GhcRn)]
-> [GenLocated SrcSpanAnnA (ConDeclField GhcRn)]
forall l e. GenLocated l e -> e
unLoc LocatedL [LConDeclField GhcRn]
GenLocated
  SrcSpanAnnL [GenLocated SrcSpanAnnA (ConDeclField GhcRn)]
ips)
  coreListM varBangTypeTyConName args
    where
      rep_ip :: GenLocated l (ConDeclField GhcRn)
-> ReaderT MetaWrappers DsM [Core (M VarBangType)]
rep_ip (L l
_ ConDeclField GhcRn
ip) = (GenLocated SrcSpanAnnA (FieldOcc GhcRn)
 -> ReaderT MetaWrappers DsM (Core (M VarBangType)))
-> [GenLocated SrcSpanAnnA (FieldOcc GhcRn)]
-> ReaderT MetaWrappers DsM [Core (M VarBangType)]
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 (XRec GhcRn (HsType GhcRn)
-> LFieldOcc GhcRn
-> ReaderT MetaWrappers DsM (Core (M VarBangType))
rep_one_ip (ConDeclField GhcRn -> XRec GhcRn (HsType GhcRn)
forall pass. ConDeclField pass -> LBangType pass
cd_fld_type ConDeclField GhcRn
ip)) (ConDeclField GhcRn -> [LFieldOcc GhcRn]
forall pass. ConDeclField pass -> [LFieldOcc pass]
cd_fld_names ConDeclField GhcRn
ip)

      rep_one_ip :: LBangType GhcRn -> LFieldOcc GhcRn -> MetaM (Core (M TH.VarBangType))
      rep_one_ip :: XRec GhcRn (HsType GhcRn)
-> LFieldOcc GhcRn
-> ReaderT MetaWrappers DsM (Core (M VarBangType))
rep_one_ip XRec GhcRn (HsType GhcRn)
t LFieldOcc GhcRn
n = do { MkC v  <- Name -> MetaM (Core Name)
lookupOcc (FieldOcc GhcRn -> XCFieldOcc GhcRn
forall pass. FieldOcc pass -> XCFieldOcc pass
foExt (FieldOcc GhcRn -> XCFieldOcc GhcRn)
-> FieldOcc GhcRn -> XCFieldOcc GhcRn
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (FieldOcc GhcRn) -> FieldOcc GhcRn
forall l e. GenLocated l e -> e
unLoc LFieldOcc GhcRn
GenLocated SrcSpanAnnA (FieldOcc GhcRn)
n)
                          ; MkC ty <- repBangTy  t
                          ; rep2 varBangTypeName [v,ty] }

------------ Types -------------------

repTForall :: Core [(M (TH.TyVarBndr TH.Specificity))] -> Core (M TH.Cxt) -> Core (M TH.Type)
           -> MetaM (Core (M TH.Type))
repTForall :: Core [M (TyVarBndr Specificity)]
-> Core (M Cxt) -> Core (M Type) -> MetaM (Core (M Type))
repTForall (MkC CoreExpr
tvars) (MkC CoreExpr
ctxt) (MkC CoreExpr
ty)
    = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
forallTName [CoreExpr
tvars, CoreExpr
ctxt, CoreExpr
ty]

repTForallVis :: Core [(M (TH.TyVarBndr ()))] -> Core (M TH.Type)
              -> MetaM (Core (M TH.Type))
repTForallVis :: Core [M (TyVarBndr ())] -> Core (M Type) -> MetaM (Core (M Type))
repTForallVis (MkC CoreExpr
tvars) (MkC CoreExpr
ty) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
forallVisTName [CoreExpr
tvars, CoreExpr
ty]

repTvar :: Core TH.Name -> MetaM (Core (M TH.Type))
repTvar :: Core Name -> MetaM (Core (M Type))
repTvar (MkC CoreExpr
s) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
varTName [CoreExpr
s]

repTapp :: Core (M TH.Type) -> Core (M TH.Type) -> MetaM (Core (M TH.Type))
repTapp :: Core (M Type) -> Core (M Type) -> MetaM (Core (M Type))
repTapp (MkC CoreExpr
t1) (MkC CoreExpr
t2) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
appTName [CoreExpr
t1, CoreExpr
t2]

repTappKind :: Core (M TH.Type) -> Core (M TH.Kind) -> MetaM (Core (M TH.Type))
repTappKind :: Core (M Type) -> Core (M Type) -> MetaM (Core (M Type))
repTappKind (MkC CoreExpr
ty) (MkC CoreExpr
ki) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
appKindTName [CoreExpr
ty,CoreExpr
ki]

repTapps :: Core (M TH.Type) -> [Core (M TH.Type)] -> MetaM (Core (M TH.Type))
repTapps :: Core (M Type) -> [Core (M Type)] -> MetaM (Core (M Type))
repTapps Core (M Type)
f []     = Core (M Type) -> MetaM (Core (M Type))
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return Core (M Type)
f
repTapps Core (M Type)
f (Core (M Type)
t:[Core (M Type)]
ts) = do { f1 <- Core (M Type) -> Core (M Type) -> MetaM (Core (M Type))
repTapp Core (M Type)
f Core (M Type)
t; repTapps f1 ts }

repTSig :: Core (M TH.Type) -> Core (M TH.Kind) -> MetaM (Core (M TH.Type))
repTSig :: Core (M Type) -> Core (M Type) -> MetaM (Core (M Type))
repTSig (MkC CoreExpr
ty) (MkC CoreExpr
ki) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
sigTName [CoreExpr
ty, CoreExpr
ki]

repTequality :: MetaM (Core (M TH.Type))
repTequality :: MetaM (Core (M Type))
repTequality = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
equalityTName []

repTPromotedList :: [Core (M TH.Type)] -> MetaM (Core (M TH.Type))
repTPromotedList :: [Core (M Type)] -> MetaM (Core (M Type))
repTPromotedList []     = MetaM (Core (M Type))
repPromotedNilTyCon
repTPromotedList (Core (M Type)
t:[Core (M Type)]
ts) = do  { tcon <- MetaM (Core (M Type))
repPromotedConsTyCon
                              ; f <- repTapp tcon t
                              ; t' <- repTPromotedList ts
                              ; repTapp f t'
                              }

repTLit :: Core (M TH.TyLit) -> MetaM (Core (M TH.Type))
repTLit :: Core (M TyLit) -> MetaM (Core (M Type))
repTLit (MkC CoreExpr
lit) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
litTName [CoreExpr
lit]

repTWildCard :: MetaM (Core (M TH.Type))
repTWildCard :: MetaM (Core (M Type))
repTWildCard = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
wildCardTName []

repTImplicitParam :: Core String -> Core (M TH.Type) -> MetaM (Core (M TH.Type))
repTImplicitParam :: Core String -> Core (M Type) -> MetaM (Core (M Type))
repTImplicitParam (MkC CoreExpr
n) (MkC CoreExpr
e) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
implicitParamTName [CoreExpr
n, CoreExpr
e]

repTStar :: MetaM (Core (M TH.Type))
repTStar :: MetaM (Core (M Type))
repTStar = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
starKName []

repTConstraint :: MetaM (Core (M TH.Type))
repTConstraint :: MetaM (Core (M Type))
repTConstraint = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
constraintKName []

--------- Type constructors --------------

repNamedTyCon :: Core TH.Name -> MetaM (Core (M TH.Type))
repNamedTyCon :: Core Name -> MetaM (Core (M Type))
repNamedTyCon (MkC CoreExpr
s) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
conTName [CoreExpr
s]

repTInfix :: Core (M TH.Type) -> Core TH.Name -> Core (M TH.Type)
             -> MetaM (Core (M TH.Type))
repTInfix :: Core (M Type)
-> Core Name -> Core (M Type) -> MetaM (Core (M Type))
repTInfix (MkC CoreExpr
t1) (MkC CoreExpr
name) (MkC CoreExpr
t2) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
infixTName [CoreExpr
t1,CoreExpr
name,CoreExpr
t2]

repTupleTyCon :: Int -> MetaM (Core (M TH.Type))
-- Note: not Core Int; it's easier to be direct here
repTupleTyCon :: Int -> MetaM (Core (M Type))
repTupleTyCon Int
i = do platform <- MetaM Platform
getPlatform
                     rep2 tupleTName [mkIntExprInt platform i]

repUnboxedTupleTyCon :: Int -> MetaM (Core (M TH.Type))
-- Note: not Core Int; it's easier to be direct here
repUnboxedTupleTyCon :: Int -> MetaM (Core (M Type))
repUnboxedTupleTyCon Int
i = do platform <- MetaM Platform
getPlatform
                            rep2 unboxedTupleTName [mkIntExprInt platform i]

repUnboxedSumTyCon :: TH.SumArity -> MetaM (Core (M TH.Type))
-- Note: not Core TH.SumArity; it's easier to be direct here
repUnboxedSumTyCon :: Int -> MetaM (Core (M Type))
repUnboxedSumTyCon Int
arity = do platform <- MetaM Platform
getPlatform
                              rep2 unboxedSumTName [mkIntExprInt platform arity]

repArrowTyCon :: MetaM (Core (M TH.Type))
repArrowTyCon :: MetaM (Core (M Type))
repArrowTyCon = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
arrowTName []

repMulArrowTyCon :: MetaM (Core (M TH.Type))
repMulArrowTyCon :: MetaM (Core (M Type))
repMulArrowTyCon = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
mulArrowTName []

repListTyCon :: MetaM (Core (M TH.Type))
repListTyCon :: MetaM (Core (M Type))
repListTyCon = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
listTName []

repPromotedDataCon :: Core TH.Name -> MetaM (Core (M TH.Type))
repPromotedDataCon :: Core Name -> MetaM (Core (M Type))
repPromotedDataCon (MkC CoreExpr
s) = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
promotedTName [CoreExpr
s]

repPromotedTupleTyCon :: Int -> MetaM (Core (M TH.Type))
repPromotedTupleTyCon :: Int -> MetaM (Core (M Type))
repPromotedTupleTyCon Int
i = do platform <- MetaM Platform
getPlatform
                             rep2 promotedTupleTName [mkIntExprInt platform i]

repPromotedNilTyCon :: MetaM (Core (M TH.Type))
repPromotedNilTyCon :: MetaM (Core (M Type))
repPromotedNilTyCon = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
promotedNilTName []

repPromotedConsTyCon :: MetaM (Core (M TH.Type))
repPromotedConsTyCon :: MetaM (Core (M Type))
repPromotedConsTyCon = Name -> [CoreExpr] -> MetaM (Core (M Type))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
promotedConsTName []

----------------------------------------------------------
--       Type family result signature

repNoSig :: MetaM (Core (M TH.FamilyResultSig))
repNoSig :: MetaM (Core (M FamilyResultSig))
repNoSig = Name -> [CoreExpr] -> MetaM (Core (M FamilyResultSig))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
noSigName []

repKindSig :: Core (M TH.Kind) -> MetaM (Core (M TH.FamilyResultSig))
repKindSig :: Core (M Type) -> MetaM (Core (M FamilyResultSig))
repKindSig (MkC CoreExpr
ki) = Name -> [CoreExpr] -> MetaM (Core (M FamilyResultSig))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
kindSigName [CoreExpr
ki]

repTyVarSig :: Core (M (TH.TyVarBndr ())) -> MetaM (Core (M TH.FamilyResultSig))
repTyVarSig :: Core (M (TyVarBndr ())) -> MetaM (Core (M FamilyResultSig))
repTyVarSig (MkC CoreExpr
bndr) = Name -> [CoreExpr] -> MetaM (Core (M FamilyResultSig))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
tyVarSigName [CoreExpr
bndr]

----------------------------------------------------------
--              Literals

repLiteral :: HsLit GhcRn -> MetaM (Core TH.Lit)
repLiteral :: HsLit GhcRn -> MetaM (Core Lit)
repLiteral (HsStringPrim XHsStringPrim GhcRn
_ ByteString
bs)
  = do word8_ty <- Name -> MetaM Type
lookupType Name
word8TyConName
       let w8s = ByteString -> [Word8]
unpack ByteString
bs
           w8s_expr = (Word8 -> CoreExpr) -> [Word8] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map (\Word8
w8 -> DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
word8DataCon
                                  [Integer -> CoreExpr
forall b. Integer -> Expr b
mkWord8Lit (Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
w8)]) [Word8]
w8s
       rep2_nw stringPrimLName [mkListExpr word8_ty w8s_expr]
repLiteral HsLit GhcRn
lit
  = do lit' <- case HsLit GhcRn
lit of
                   HsIntPrim XHsIntPrim GhcRn
_ Integer
i    -> Integer -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_integer Integer
i
                   HsWordPrim XHsWordPrim GhcRn
_ Integer
w   -> Integer -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_integer Integer
w
                   HsInt XHsInt GhcRn
_ IntegralLit
i        -> Integer -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_integer (IntegralLit -> Integer
il_value IntegralLit
i)
                   HsFloatPrim XHsFloatPrim GhcRn
_ FractionalLit
r  -> FractionalLit -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_rational FractionalLit
r
                   HsDoublePrim XHsDoublePrim GhcRn
_ FractionalLit
r -> FractionalLit -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_rational FractionalLit
r
                   HsCharPrim XHsCharPrim GhcRn
_ Char
c   -> Char -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_char Char
c
                   HsLit GhcRn
_ -> HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return HsLit GhcRn
lit
       lit_expr <- lift $ dsLit lit'
       case mb_lit_name of
          Just Name
lit_name -> Name -> [CoreExpr] -> MetaM (Core Lit)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
lit_name [CoreExpr
lit_expr]
          Maybe Name
Nothing -> ThRejectionReason -> MetaM (Core Lit)
forall a. ThRejectionReason -> MetaM a
notHandled (HsLit GhcRn -> ThRejectionReason
ThExoticLiteral HsLit GhcRn
lit)
  where
    mb_lit_name :: Maybe Name
mb_lit_name = case HsLit GhcRn
lit of
                 HsInteger XHsInteger GhcRn
_ Integer
_ Type
_  -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
integerLName
                 HsInt XHsInt GhcRn
_ IntegralLit
_        -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
integerLName
                 HsIntPrim XHsIntPrim GhcRn
_ Integer
_    -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
intPrimLName
                 HsWordPrim XHsWordPrim GhcRn
_ Integer
_   -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
wordPrimLName
                 HsFloatPrim XHsFloatPrim GhcRn
_ FractionalLit
_  -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
floatPrimLName
                 HsDoublePrim XHsDoublePrim GhcRn
_ FractionalLit
_ -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
doublePrimLName
                 HsChar XHsChar GhcRn
_ Char
_       -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
charLName
                 HsCharPrim XHsCharPrim GhcRn
_ Char
_   -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
charPrimLName
                 HsString XHsString GhcRn
_ FastString
_     -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
stringLName
                 HsRat XHsRat GhcRn
_ FractionalLit
_ Type
_      -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
rationalLName
                 HsLit GhcRn
_                -> Maybe Name
forall a. Maybe a
Nothing

mk_integer :: Integer -> MetaM (HsLit GhcRn)
mk_integer :: Integer -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_integer  Integer
i = HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn))
-> HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a b. (a -> b) -> a -> b
$ XHsInteger GhcRn -> Integer -> Type -> HsLit GhcRn
forall x. XHsInteger x -> Integer -> Type -> HsLit x
HsInteger XHsInteger GhcRn
SourceText
NoSourceText Integer
i Type
integerTy

mk_rational :: FractionalLit -> MetaM (HsLit GhcRn)
mk_rational :: FractionalLit -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_rational FractionalLit
r = do rat_ty <- Name -> MetaM Type
lookupType Name
rationalTyConName
                   return $ HsRat noExtField r rat_ty
mk_string :: FastString -> MetaM (HsLit GhcRn)
mk_string :: FastString -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_string FastString
s = HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn))
-> HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a b. (a -> b) -> a -> b
$ XHsString GhcRn -> FastString -> HsLit GhcRn
forall x. XHsString x -> FastString -> HsLit x
HsString XHsString GhcRn
SourceText
NoSourceText FastString
s

mk_char :: Char -> MetaM (HsLit GhcRn)
mk_char :: Char -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_char Char
c = HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn))
-> HsLit GhcRn -> ReaderT MetaWrappers DsM (HsLit GhcRn)
forall a b. (a -> b) -> a -> b
$ XHsChar GhcRn -> Char -> HsLit GhcRn
forall x. XHsChar x -> Char -> HsLit x
HsChar XHsChar GhcRn
SourceText
NoSourceText Char
c

repOverloadedLiteral :: HsOverLit GhcRn -> MetaM (Core TH.Lit)
repOverloadedLiteral :: HsOverLit GhcRn -> MetaM (Core Lit)
repOverloadedLiteral (OverLit { ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val = OverLitVal
val})
  = do { lit <- OverLitVal -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_lit OverLitVal
val; repLiteral lit }
        -- The type Rational will be in the environment, because
        -- the smart constructor 'TH.Syntax.rationalL' uses it in its type,
        -- and rationalL is sucked in when any TH stuff is used

mk_lit :: OverLitVal -> MetaM (HsLit GhcRn)
mk_lit :: OverLitVal -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_lit (HsIntegral IntegralLit
i)     = Integer -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_integer  (IntegralLit -> Integer
il_value IntegralLit
i)
mk_lit (HsFractional FractionalLit
f)   = FractionalLit -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_rational FractionalLit
f
mk_lit (HsIsString SourceText
_ FastString
s)   = FastString -> ReaderT MetaWrappers DsM (HsLit GhcRn)
mk_string   FastString
s

repRdrName :: RdrName -> MetaM (Core TH.Name)
repRdrName :: RdrName -> MetaM (Core Name)
repRdrName RdrName
rdr_name = do
  case RdrName
rdr_name of
    Unqual OccName
occ ->
      Core String -> MetaM (Core Name)
repNameS (Core String -> MetaM (Core Name))
-> ReaderT MetaWrappers DsM (Core String) -> MetaM (Core Name)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< OccName -> ReaderT MetaWrappers DsM (Core String)
forall (m :: * -> *). MonadThings m => OccName -> m (Core String)
occNameLit OccName
occ
    Qual ModuleName
mn OccName
occ -> do
      let name_mod :: FastString
name_mod = ModuleName -> FastString
moduleNameFS ModuleName
mn
      mod <- FastString -> ReaderT MetaWrappers DsM (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit FastString
name_mod
      occ <- occNameLit occ
      repNameQ mod occ
    Orig Module
m OccName
n -> DsM (Core Name) -> MetaM (Core Name)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM (Core Name) -> MetaM (Core Name))
-> DsM (Core Name) -> MetaM (Core Name)
forall a b. (a -> b) -> a -> b
$ Module -> OccName -> DsM (Core Name)
globalVarExternal Module
m OccName
n
    Exact Name
n -> DsM (Core Name) -> MetaM (Core Name)
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM (Core Name) -> MetaM (Core Name))
-> DsM (Core Name) -> MetaM (Core Name)
forall a b. (a -> b) -> a -> b
$ Name -> DsM (Core Name)
globalVar Name
n

repNameS :: Core String -> MetaM (Core TH.Name)
repNameS :: Core String -> MetaM (Core Name)
repNameS (MkC CoreExpr
name) = Name -> [CoreExpr] -> MetaM (Core Name)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
mkNameSName [CoreExpr
name]

repNameQ :: Core String -> Core String -> MetaM (Core TH.Name)
repNameQ :: Core String -> Core String -> MetaM (Core Name)
repNameQ (MkC CoreExpr
mn) (MkC CoreExpr
name) = Name -> [CoreExpr] -> MetaM (Core Name)
forall a. NotM a => Name -> [CoreExpr] -> MetaM (Core a)
rep2_nw Name
mkNameQName [CoreExpr
mn, CoreExpr
name]

--------------- Miscellaneous -------------------

repGensym :: Core String -> MetaM (Core (M TH.Name))
repGensym :: Core String -> MetaM (Core (M Name))
repGensym (MkC CoreExpr
lit_str) = Name -> [CoreExpr] -> MetaM (Core (M Name))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
newNameName [CoreExpr
lit_str]

repBindM :: Type -> Type        -- a and b
         -> Core (M a) -> Core (a -> M b) -> MetaM (Core (M b))
repBindM :: forall {k} a (b :: k).
Type -> Type -> Core (M a) -> Core (a -> M b) -> MetaM (Core (M b))
repBindM Type
ty_a Type
ty_b (MkC CoreExpr
x) (MkC CoreExpr
y)
  = Name -> [CoreExpr] -> MetaM (Core (M b))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2M Name
bindMName [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty_a, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty_b, CoreExpr
x, CoreExpr
y]

repSequenceM :: Type -> Core [M a] -> MetaM (Core (M [a]))
repSequenceM :: forall a. Type -> Core [M a] -> MetaM (Core (M [a]))
repSequenceM Type
ty_a (MkC CoreExpr
list)
  = Name -> [CoreExpr] -> MetaM (Core (M [a]))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2M Name
sequenceQName [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty_a, CoreExpr
list]

repUnboundVar :: Core TH.Name -> MetaM (Core (M TH.Exp))
repUnboundVar :: Core Name -> MetaM (Core (M Exp))
repUnboundVar (MkC CoreExpr
name) = Name -> [CoreExpr] -> MetaM (Core (M Exp))
forall {k} (a :: k). Name -> [CoreExpr] -> MetaM (Core (M a))
rep2 Name
unboundVarEName [CoreExpr
name]

repOverLabel :: FastString -> MetaM (Core (M TH.Exp))
repOverLabel :: FastString -> MetaM (Core (M Exp))
repOverLabel FastString
fs = do
                    MkC s <- FastString -> ReaderT MetaWrappers DsM (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit FastString
fs
                    rep2 labelEName [s]

repGetField :: Core (M TH.Exp) -> FastString -> MetaM (Core (M TH.Exp))
repGetField :: Core (M Exp) -> FastString -> MetaM (Core (M Exp))
repGetField (MkC CoreExpr
exp) FastString
fs = do
  MkC s <- FastString -> ReaderT MetaWrappers DsM (Core String)
forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit FastString
fs
  rep2 getFieldEName [exp,s]

repProjection :: NonEmpty FastString -> MetaM (Core (M TH.Exp))
repProjection :: NonEmpty FastString -> MetaM (Core (M Exp))
repProjection NonEmpty FastString
fs = do
  ne_tycon <- DsM TyCon -> ReaderT MetaWrappers DsM TyCon
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM TyCon -> ReaderT MetaWrappers DsM TyCon)
-> DsM TyCon -> ReaderT MetaWrappers DsM TyCon
forall a b. (a -> b) -> a -> b
$ Name -> DsM TyCon
dsLookupTyCon Name
nonEmptyTyConName
  MkC xs <- coreListNonEmpty ne_tycon stringTy <$>
            mapM coreStringLit fs
  rep2 projectionEName [xs]

------------ Lists -------------------
-- turn a list of patterns into a single pattern matching a list

repList :: Name -> (a  -> MetaM (Core b))
                    -> [a] -> MetaM (Core [b])
repList :: forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repList Name
tc_name a -> MetaM (Core b)
f [a]
args
  = do { args1 <- (a -> MetaM (Core b)) -> [a] -> ReaderT MetaWrappers DsM [Core b]
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 a -> MetaM (Core b)
f [a]
args
       ; coreList tc_name args1 }

-- Create a list of m a values
repListM :: Name -> (a  -> MetaM (Core b))
                    -> [a] -> MetaM (Core [b])
repListM :: forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
tc_name a -> MetaM (Core b)
f [a]
args
  = do { ty <- Name -> MetaM Type
wrapName Name
tc_name
       ; args1 <- mapM f args
       ; return $ coreList' ty args1 }

coreListM :: Name -> [Core a] -> MetaM (Core [a])
coreListM :: forall a. Name -> [Core a] -> MetaM (Core [a])
coreListM Name
tc [Core a]
as = Name -> (Core a -> MetaM (Core a)) -> [Core a] -> MetaM (Core [a])
forall a b.
Name -> (a -> MetaM (Core b)) -> [a] -> MetaM (Core [b])
repListM Name
tc Core a -> MetaM (Core a)
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return [Core a]
as

coreList :: Name    -- Of the TyCon of the element type
         -> [Core a] -> MetaM (Core [a])
coreList :: forall a. Name -> [Core a] -> MetaM (Core [a])
coreList Name
tc_name [Core a]
es
  = do { elt_ty <- Name -> MetaM Type
lookupType Name
tc_name; return (coreList' elt_ty es) }

coreList' :: Type       -- The element type
          -> [Core a] -> Core [a]
coreList' :: forall a. Type -> [Core a] -> Core [a]
coreList' Type
elt_ty [Core a]
es = CoreExpr -> Core [a]
forall {k} (a :: k). CoreExpr -> Core a
MkC (Type -> [CoreExpr] -> CoreExpr
mkListExpr Type
elt_ty ((Core a -> CoreExpr) -> [Core a] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Core a -> CoreExpr
forall {k} (a :: k). Core a -> CoreExpr
unC [Core a]
es ))

coreListNonEmpty :: TyCon -- TyCon for NonEmpty
                 -> Type  -- Element type
                 -> NonEmpty (Core a)
                 -> Core (NonEmpty a)
coreListNonEmpty :: forall a. TyCon -> Type -> NonEmpty (Core a) -> Core (NonEmpty a)
coreListNonEmpty TyCon
ne_tc Type
ty (MkC CoreExpr
x :| [Core a]
xs)
  = CoreExpr -> Core (NonEmpty a)
forall {k} (a :: k). CoreExpr -> Core a
MkC (CoreExpr -> Core (NonEmpty a)) -> CoreExpr -> Core (NonEmpty a)
forall a b. (a -> b) -> a -> b
$ DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps (TyCon -> DataCon
tyConSingleDataCon TyCon
ne_tc)
          [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty, CoreExpr
x, Type -> [CoreExpr] -> CoreExpr
mkListExpr Type
ty ((Core a -> CoreExpr) -> [Core a] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Core a -> CoreExpr
forall {k} (a :: k). Core a -> CoreExpr
unC [Core a]
xs)]

nonEmptyCoreList :: [Core a] -> Core [a]
  -- The list must be non-empty so we can get the element type
  -- Otherwise use coreList
nonEmptyCoreList :: forall a. [Core a] -> Core [a]
nonEmptyCoreList []           = String -> Core [a]
forall a. HasCallStack => String -> a
panic String
"coreList: empty argument"
nonEmptyCoreList xs :: [Core a]
xs@(MkC CoreExpr
x:[Core a]
_) = CoreExpr -> Core [a]
forall {k} (a :: k). CoreExpr -> Core a
MkC (Type -> [CoreExpr] -> CoreExpr
mkListExpr (HasDebugCallStack => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
x) ((Core a -> CoreExpr) -> [Core a] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Core a -> CoreExpr
forall {k} (a :: k). Core a -> CoreExpr
unC [Core a]
xs))

nonEmptyCoreList' :: NonEmpty (Core a) -> Core [a]
nonEmptyCoreList' :: forall a. NonEmpty (Core a) -> Core [a]
nonEmptyCoreList' xs :: NonEmpty (Core a)
xs@(MkC CoreExpr
x:|[Core a]
_) = CoreExpr -> Core [a]
forall {k} (a :: k). CoreExpr -> Core a
MkC (Type -> [CoreExpr] -> CoreExpr
mkListExpr (HasDebugCallStack => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
x) (NonEmpty CoreExpr -> [CoreExpr]
forall a. NonEmpty a -> [a]
toList (NonEmpty CoreExpr -> [CoreExpr])
-> NonEmpty CoreExpr -> [CoreExpr]
forall a b. (a -> b) -> a -> b
$ (Core a -> CoreExpr) -> NonEmpty (Core a) -> NonEmpty CoreExpr
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Core a -> CoreExpr
forall {k} (a :: k). Core a -> CoreExpr
unC NonEmpty (Core a)
xs))

coreStringLit :: MonadThings m => FastString -> m (Core String)
coreStringLit :: forall (m :: * -> *).
MonadThings m =>
FastString -> m (Core String)
coreStringLit FastString
s = do { z <- FastString -> m CoreExpr
forall (m :: * -> *). MonadThings m => FastString -> m CoreExpr
mkStringExprFS FastString
s; return (MkC z) }

------------------- Maybe ------------------

repMaybe :: Name -> (a -> MetaM (Core b))
                    -> Maybe a -> MetaM (Core (Maybe b))
repMaybe :: forall a b.
Name -> (a -> MetaM (Core b)) -> Maybe a -> MetaM (Core (Maybe b))
repMaybe Name
tc_name a -> MetaM (Core b)
f Maybe a
m = do
  t <- Name -> MetaM Type
lookupType Name
tc_name
  repMaybeT t f m

repMaybeT :: Type -> (a -> MetaM (Core b))
                    -> Maybe a -> MetaM (Core (Maybe b))
repMaybeT :: forall a b.
Type -> (a -> MetaM (Core b)) -> Maybe a -> MetaM (Core (Maybe b))
repMaybeT Type
ty a -> MetaM (Core b)
_ Maybe a
Nothing   = Core (Maybe b) -> ReaderT MetaWrappers DsM (Core (Maybe b))
forall a. a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Core (Maybe b) -> ReaderT MetaWrappers DsM (Core (Maybe b)))
-> Core (Maybe b) -> ReaderT MetaWrappers DsM (Core (Maybe b))
forall a b. (a -> b) -> a -> b
$ Type -> Core (Maybe b)
forall a. Type -> Core (Maybe a)
coreNothing' Type
ty
repMaybeT Type
ty a -> MetaM (Core b)
f (Just a
es) = Type -> Core b -> Core (Maybe b)
forall a. Type -> Core a -> Core (Maybe a)
coreJust' Type
ty (Core b -> Core (Maybe b))
-> MetaM (Core b) -> ReaderT MetaWrappers DsM (Core (Maybe b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> MetaM (Core b)
f a
es

-- | Construct Core expression for Nothing of a given type name
coreNothing :: Name        -- ^ Name of the TyCon of the element type
            -> MetaM (Core (Maybe a))
coreNothing :: forall a. Name -> MetaM (Core (Maybe a))
coreNothing Name
tc_name =
    do { elt_ty <- Name -> MetaM Type
lookupType Name
tc_name; return (coreNothing' elt_ty) }

coreNothingM :: Name -> MetaM (Core (Maybe a))
coreNothingM :: forall a. Name -> MetaM (Core (Maybe a))
coreNothingM Name
tc_name =
    do { elt_ty <- Name -> MetaM Type
wrapName Name
tc_name; return (coreNothing' elt_ty) }

-- | Construct Core expression for Nothing of a given type
coreNothing' :: Type       -- ^ The element type
             -> Core (Maybe a)
coreNothing' :: forall a. Type -> Core (Maybe a)
coreNothing' Type
elt_ty = CoreExpr -> Core (Maybe a)
forall {k} (a :: k). CoreExpr -> Core a
MkC (Type -> CoreExpr
mkNothingExpr Type
elt_ty)

-- | Store given Core expression in a Just of a given type name
coreJust :: Name        -- ^ Name of the TyCon of the element type
         -> Core a -> MetaM (Core (Maybe a))
coreJust :: forall a. Name -> Core a -> MetaM (Core (Maybe a))
coreJust Name
tc_name Core a
es
  = do { elt_ty <- Name -> MetaM Type
lookupType Name
tc_name; return (coreJust' elt_ty es) }

coreJustM :: Name -> Core a -> MetaM (Core (Maybe a))
coreJustM :: forall a. Name -> Core a -> MetaM (Core (Maybe a))
coreJustM Name
tc_name Core a
es = do { elt_ty <- Name -> MetaM Type
wrapName Name
tc_name; return (coreJust' elt_ty es) }

-- | Store given Core expression in a Just of a given type
coreJust' :: Type       -- ^ The element type
          -> Core a -> Core (Maybe a)
coreJust' :: forall a. Type -> Core a -> Core (Maybe a)
coreJust' Type
elt_ty Core a
es = CoreExpr -> Core (Maybe a)
forall {k} (a :: k). CoreExpr -> Core a
MkC (Type -> CoreExpr -> CoreExpr
mkJustExpr Type
elt_ty (Core a -> CoreExpr
forall {k} (a :: k). Core a -> CoreExpr
unC Core a
es))

------------------- Maybe Lists ------------------

coreJustList :: Type -> Core [a] -> Core (Maybe [a])
coreJustList :: forall a. Type -> Core [a] -> Core (Maybe [a])
coreJustList Type
elt_ty = Type -> Core [a] -> Core (Maybe [a])
forall a. Type -> Core a -> Core (Maybe a)
coreJust' (Type -> Type
mkListTy Type
elt_ty)

coreNothingList :: Type -> Core (Maybe [a])
coreNothingList :: forall a. Type -> Core (Maybe [a])
coreNothingList Type
elt_ty = Type -> Core (Maybe [a])
forall a. Type -> Core (Maybe a)
coreNothing' (Type -> Type
mkListTy Type
elt_ty)

------------ Literals & Variables -------------------

coreIntLit :: Int -> MetaM (Core Int)
coreIntLit :: Int -> MetaM (Core Int)
coreIntLit Int
i = do platform <- MetaM Platform
getPlatform
                  return (MkC (mkIntExprInt platform i))

coreVar :: Id -> Core TH.Name   -- The Id has type Name
coreVar :: Id -> Core Name
coreVar Id
id = CoreExpr -> Core Name
forall {k} (a :: k). CoreExpr -> Core a
MkC (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
id)

----------------- Failure -----------------------
notHandledL :: SrcSpan -> ThRejectionReason -> MetaM a
notHandledL :: forall a. SrcSpan -> ThRejectionReason -> MetaM a
notHandledL SrcSpan
loc ThRejectionReason
reason
  | SrcSpan -> Bool
isGoodSrcSpan SrcSpan
loc
  = (IOEnv (Env DsGblEnv DsLclEnv) a
 -> IOEnv (Env DsGblEnv DsLclEnv) a)
-> ReaderT MetaWrappers DsM a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT (SrcSpan
-> IOEnv (Env DsGblEnv DsLclEnv) a
-> IOEnv (Env DsGblEnv DsLclEnv) a
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc) (ReaderT MetaWrappers DsM a -> ReaderT MetaWrappers DsM a)
-> ReaderT MetaWrappers DsM a -> ReaderT MetaWrappers DsM a
forall a b. (a -> b) -> a -> b
$ ThRejectionReason -> ReaderT MetaWrappers DsM a
forall a. ThRejectionReason -> MetaM a
notHandled ThRejectionReason
reason
  | Bool
otherwise
  = ThRejectionReason -> ReaderT MetaWrappers DsM a
forall a. ThRejectionReason -> MetaM a
notHandled ThRejectionReason
reason

notHandled :: ThRejectionReason -> MetaM a
notHandled :: forall a. ThRejectionReason -> MetaM a
notHandled ThRejectionReason
reason = DsM a -> ReaderT MetaWrappers DsM a
forall (m :: * -> *) a. Monad m => m a -> ReaderT MetaWrappers m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DsM a -> ReaderT MetaWrappers DsM a)
-> DsM a -> ReaderT MetaWrappers DsM a
forall a b. (a -> b) -> a -> b
$ DsMessage -> DsM a
forall a. DsMessage -> DsM a
failWithDs (ThRejectionReason -> DsMessage
DsNotYetHandledByTH ThRejectionReason
reason)