474630b789f765818f6bb8b00340a6597d2feaa9
[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
10 module TcAnnotations ( tcAnnotations, annCtxt ) where
11
12 #ifdef GHCI
13 import {-# SOURCE #-} TcSplice ( runAnnotation )
14 import Module
15 #endif
16
17 import HsSyn
18 import Annotations
19 import Name
20 import TcRnMonad
21 import SrcLoc
22 import Outputable
23
24 import FastString
25
26 #ifndef GHCI
27
28 tcAnnotations :: [LAnnDecl Name] -> TcM [Annotation]
29 -- No GHCI; emit a warning (not an error) and ignore. cf Trac #4268
30 tcAnnotations [] = return []
31 tcAnnotations anns@(L loc _ : _)
32 = do { setSrcSpan loc $ addWarnTc $
33 (ptext (sLit "Ignoring ANN annotation") <> plural anns <> comma
34 <+> ptext (sLit "because this is a stage-1 compiler or doesn't support GHCi"))
35 ; return [] }
36
37 #else
38
39 tcAnnotations :: [LAnnDecl Name] -> TcM [Annotation]
40 -- GHCI exists, typecheck the annotations
41 tcAnnotations anns = mapM tcAnnotation anns
42
43 tcAnnotation :: LAnnDecl Name -> TcM Annotation
44 tcAnnotation (L loc ann@(HsAnnotation _ provenance expr)) = do
45 -- Work out what the full target of this annotation was
46 mod <- getModule
47 let target = annProvenanceToTarget mod provenance
48
49 -- Run that annotation and construct the full Annotation data structure
50 setSrcSpan loc $ addErrCtxt (annCtxt ann) $ runAnnotation target expr
51
52 annProvenanceToTarget :: Module -> AnnProvenance Name -> AnnTarget Name
53 annProvenanceToTarget _ (ValueAnnProvenance (L _ name)) = NamedTarget name
54 annProvenanceToTarget _ (TypeAnnProvenance (L _ name)) = NamedTarget name
55 annProvenanceToTarget mod ModuleAnnProvenance = ModuleTarget mod
56 #endif
57
58 annCtxt :: OutputableBndr id => AnnDecl id -> SDoc
59 annCtxt ann
60 = hang (ptext (sLit "In the annotation:")) 2 (ppr ann)