Make a smart mkAppTyM
[ghc.git] / compiler / typecheck / TcAnnotations.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The AQUA Project, Glasgow University, 1993-1998
4
5 \section[TcAnnotations]{Typechecking annotations}
6 -}
7
8 {-# LANGUAGE CPP #-}
9 {-# LANGUAGE FlexibleContexts #-}
10 {-# LANGUAGE TypeFamilies #-}
11
12 module TcAnnotations ( tcAnnotations, annCtxt ) where
13
14 import GhcPrelude
15
16 import {-# SOURCE #-} TcSplice ( runAnnotation )
17 import Module
18 import DynFlags
19 import Control.Monad ( when )
20
21 import HsSyn
22 import Name
23 import Annotations
24 import TcRnMonad
25 import SrcLoc
26 import Outputable
27
28 -- Some platforms don't support the external interpreter, and
29 -- compilation on those platforms shouldn't fail just due to
30 -- annotations
31 #ifndef GHCI
32 tcAnnotations :: [LAnnDecl GhcRn] -> TcM [Annotation]
33 tcAnnotations anns = do
34 dflags <- getDynFlags
35 case gopt Opt_ExternalInterpreter dflags of
36 True -> tcAnnotations' anns
37 False -> warnAnns anns
38 warnAnns :: [LAnnDecl GhcRn] -> TcM [Annotation]
39 --- No GHCI; emit a warning (not an error) and ignore. cf Trac #4268
40 warnAnns [] = return []
41 warnAnns anns@(L loc _ : _)
42 = do { setSrcSpan loc $ addWarnTc NoReason $
43 (text "Ignoring ANN annotation" <> plural anns <> comma
44 <+> text "because this is a stage-1 compiler without -fexternal-interpreter or doesn't support GHCi")
45 ; return [] }
46 #else
47 tcAnnotations :: [LAnnDecl GhcRn] -> TcM [Annotation]
48 tcAnnotations = tcAnnotations'
49 #endif
50
51 tcAnnotations' :: [LAnnDecl GhcRn] -> TcM [Annotation]
52 tcAnnotations' anns = mapM tcAnnotation anns
53
54 tcAnnotation :: LAnnDecl GhcRn -> TcM Annotation
55 tcAnnotation (L loc ann@(HsAnnotation _ _ provenance expr)) = do
56 -- Work out what the full target of this annotation was
57 mod <- getModule
58 let target = annProvenanceToTarget mod provenance
59
60 -- Run that annotation and construct the full Annotation data structure
61 setSrcSpan loc $ addErrCtxt (annCtxt ann) $ do
62 -- See #10826 -- Annotations allow one to bypass Safe Haskell.
63 dflags <- getDynFlags
64 when (safeLanguageOn dflags) $ failWithTc safeHsErr
65 runAnnotation target expr
66 where
67 safeHsErr = vcat [ text "Annotations are not compatible with Safe Haskell."
68 , text "See https://ghc.haskell.org/trac/ghc/ticket/10826" ]
69 tcAnnotation (L _ (XAnnDecl _)) = panic "tcAnnotation"
70
71 annProvenanceToTarget :: Module -> AnnProvenance Name
72 -> AnnTarget Name
73 annProvenanceToTarget _ (ValueAnnProvenance (L _ name)) = NamedTarget name
74 annProvenanceToTarget _ (TypeAnnProvenance (L _ name)) = NamedTarget name
75 annProvenanceToTarget mod ModuleAnnProvenance = ModuleTarget mod
76
77 annCtxt :: (OutputableBndrId (GhcPass p)) => AnnDecl (GhcPass p) -> SDoc
78 annCtxt ann
79 = hang (text "In the annotation:") 2 (ppr ann)