{-
(c) The University of Glasgow 2006
(c) The AQUA Project, Glasgow University, 1993-1998

-}

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | Typechecking annotations
module GHC.Tc.Gen.Annotation ( tcAnnotations, annCtxt ) where

import GHC.Prelude

import GHC.Driver.Session
import GHC.Driver.Env

import {-# SOURCE #-} GHC.Tc.Gen.Splice ( runAnnotation )
import GHC.Tc.Utils.Monad

import GHC.Unit.Module

import GHC.Hs

import GHC.Utils.Outputable

import GHC.Types.Name
import GHC.Types.Annotations
import GHC.Types.SrcLoc

import Control.Monad ( when )

-- Some platforms don't support the interpreter, and compilation on those
-- platforms shouldn't fail just due to annotations
tcAnnotations :: [LAnnDecl GhcRn] -> TcM [Annotation]
tcAnnotations :: [LAnnDecl GhcRn] -> TcM [Annotation]
tcAnnotations [LAnnDecl GhcRn]
anns = do
  HscEnv
hsc_env <- forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
  case HscEnv -> Maybe Interp
hsc_interp HscEnv
hsc_env of
    Just Interp
_  -> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LAnnDecl GhcRn -> TcM Annotation
tcAnnotation [LAnnDecl GhcRn]
anns
    Maybe Interp
Nothing -> [LAnnDecl GhcRn] -> TcM [Annotation]
warnAnns [LAnnDecl GhcRn]
anns

warnAnns :: [LAnnDecl GhcRn] -> TcM [Annotation]
--- No GHCI; emit a warning (not an error) and ignore. cf #4268
warnAnns :: [LAnnDecl GhcRn] -> TcM [Annotation]
warnAnns [] = forall (m :: * -> *) a. Monad m => a -> m a
return []
warnAnns anns :: [LAnnDecl GhcRn]
anns@(L SrcSpanAnn' (EpAnn AnnListItem)
loc AnnDecl GhcRn
_ : [LAnnDecl GhcRn]
_)
  = do { forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnn' (EpAnn AnnListItem)
loc forall a b. (a -> b) -> a -> b
$ WarnReason -> SDoc -> TcRn ()
addWarnTc WarnReason
NoReason forall a b. (a -> b) -> a -> b
$
             (String -> SDoc
text String
"Ignoring ANN annotation" SDoc -> SDoc -> SDoc
<> forall a. [a] -> SDoc
plural [LAnnDecl GhcRn]
anns SDoc -> SDoc -> SDoc
<> SDoc
comma
             SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"because this is a stage-1 compiler without -fexternal-interpreter or doesn't support GHCi")
       ; forall (m :: * -> *) a. Monad m => a -> m a
return [] }

tcAnnotation :: LAnnDecl GhcRn -> TcM Annotation
tcAnnotation :: LAnnDecl GhcRn -> TcM Annotation
tcAnnotation (L SrcSpanAnn' (EpAnn AnnListItem)
loc ann :: AnnDecl GhcRn
ann@(HsAnnotation XHsAnnotation GhcRn
_ SourceText
_ AnnProvenance GhcRn
provenance XRec GhcRn (HsExpr GhcRn)
expr)) = do
    -- Work out what the full target of this annotation was
    Module
mod <- forall (m :: * -> *). HasModule m => m Module
getModule
    let target :: AnnTarget Name
target = Module -> AnnProvenance GhcRn -> AnnTarget Name
annProvenanceToTarget Module
mod AnnProvenance GhcRn
provenance

    -- Run that annotation and construct the full Annotation data structure
    forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnn' (EpAnn AnnListItem)
loc forall a b. (a -> b) -> a -> b
$ forall a. SDoc -> TcM a -> TcM a
addErrCtxt (forall (p :: Pass).
OutputableBndrId p =>
AnnDecl (GhcPass p) -> SDoc
annCtxt AnnDecl GhcRn
ann) forall a b. (a -> b) -> a -> b
$ do
      -- See #10826 -- Annotations allow one to bypass Safe Haskell.
      DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DynFlags -> Bool
safeLanguageOn DynFlags
dflags) forall a b. (a -> b) -> a -> b
$ forall a. SDoc -> TcM a
failWithTc SDoc
safeHsErr
      AnnTarget Name -> XRec GhcRn (HsExpr GhcRn) -> TcM Annotation
runAnnotation AnnTarget Name
target XRec GhcRn (HsExpr GhcRn)
expr
    where
      safeHsErr :: SDoc
safeHsErr = [SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Annotations are not compatible with Safe Haskell."
                  , String -> SDoc
text String
"See https://gitlab.haskell.org/ghc/ghc/issues/10826" ]

annProvenanceToTarget :: Module -> AnnProvenance GhcRn
                      -> AnnTarget Name
annProvenanceToTarget :: Module -> AnnProvenance GhcRn -> AnnTarget Name
annProvenanceToTarget Module
_   (ValueAnnProvenance (L SrcSpanAnnN
_ Name
name)) = forall name. name -> AnnTarget name
NamedTarget Name
name
annProvenanceToTarget Module
_   (TypeAnnProvenance (L SrcSpanAnnN
_ Name
name))  = forall name. name -> AnnTarget name
NamedTarget Name
name
annProvenanceToTarget Module
mod AnnProvenance GhcRn
ModuleAnnProvenance             = forall name. Module -> AnnTarget name
ModuleTarget Module
mod

annCtxt :: (OutputableBndrId p) => AnnDecl (GhcPass p) -> SDoc
annCtxt :: forall (p :: Pass).
OutputableBndrId p =>
AnnDecl (GhcPass p) -> SDoc
annCtxt AnnDecl (GhcPass p)
ann
  = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"In the annotation:") Int
2 (forall a. Outputable a => a -> SDoc
ppr AnnDecl (GhcPass p)
ann)