Major patch to fix reporting of unused imports
[ghc-hetmet.git] / compiler / hsSyn / HsImpExp.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 HsImpExp: Abstract syntax: imports, exports, interfaces
7
8 \begin{code}
9 {-# OPTIONS -fno-warn-incomplete-patterns #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 -- for details
15
16 module HsImpExp where
17
18 import Module           ( ModuleName )
19 import HsDoc            ( HsDoc )
20
21 import Outputable
22 import FastString
23 import SrcLoc           ( Located(..) )
24 \end{code}
25
26 %************************************************************************
27 %*                                                                      *
28 \subsection{Import and export declaration lists}
29 %*                                                                      *
30 %************************************************************************
31
32 One per \tr{import} declaration in a module.
33 \begin{code}
34 type LImportDecl name = Located (ImportDecl name)
35
36 -- | A single Haskell @import@ declaration.
37 data ImportDecl name
38   = ImportDecl {
39       ideclName      :: Located ModuleName, -- ^ Module name.
40       ideclPkgQual   :: Maybe FastString,   -- ^ Package qualifier.
41       ideclSource    :: Bool,               -- ^ True <=> {-# SOURCE #-} import
42       ideclQualified :: Bool,               -- ^ True => qualified
43       ideclAs        :: Maybe ModuleName,   -- ^ as Module
44       ideclHiding    :: Maybe (Bool, [LIE name]) -- ^ (True => hiding, names)
45     }
46 \end{code}
47
48 \begin{code}
49 instance (Outputable name) => Outputable (ImportDecl name) where
50     ppr (ImportDecl mod pkg from qual as spec)
51       = hang (hsep [ptext (sLit "import"), ppr_imp from, 
52                     pp_qual qual, pp_pkg pkg, ppr mod, pp_as as])
53              4 (pp_spec spec)
54       where
55         pp_pkg Nothing  = empty
56         pp_pkg (Just p) = doubleQuotes (ftext p)
57
58         pp_qual False   = empty
59         pp_qual True    = ptext (sLit "qualified")
60
61         pp_as Nothing   = empty
62         pp_as (Just a)  = ptext (sLit "as") <+> ppr a
63
64         ppr_imp True  = ptext (sLit "{-# SOURCE #-}")
65         ppr_imp False = empty
66
67         pp_spec Nothing             = empty
68         pp_spec (Just (False, ies)) = ppr_ies ies
69         pp_spec (Just (True,  ies)) = ptext (sLit "hiding") <+> ppr_ies ies
70
71         ppr_ies []  = ptext (sLit "()")
72         ppr_ies ies = char '(' <+> interpp'SP ies <+> char ')'
73 \end{code}
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection{Imported and exported entities}
78 %*                                                                      *
79 %************************************************************************
80
81 \begin{code}
82 type LIE name = Located (IE name)
83
84 -- | Imported or exported entity.
85 data IE name
86   = IEVar               name
87   | IEThingAbs          name             -- ^ Class/Type (can't tell)
88   | IEThingAll          name             -- ^ Class/Type plus all methods/constructors
89   | IEThingWith         name [name]      -- ^ Class/Type plus some methods/constructors
90   | IEModuleContents    ModuleName       -- ^ (Export Only)
91   | IEGroup             Int (HsDoc name) -- ^ Doc section heading
92   | IEDoc               (HsDoc name)     -- ^ Some documentation
93   | IEDocNamed          String           -- ^ Reference to named doc
94 \end{code}
95
96 \begin{code}
97 ieName :: IE name -> name
98 ieName (IEVar n)         = n
99 ieName (IEThingAbs  n)   = n
100 ieName (IEThingWith n _) = n
101 ieName (IEThingAll  n)   = n
102
103 ieNames :: IE a -> [a]
104 ieNames (IEVar            n   ) = [n]
105 ieNames (IEThingAbs       n   ) = [n]
106 ieNames (IEThingAll       n   ) = [n]
107 ieNames (IEThingWith      n ns) = n:ns
108 ieNames (IEModuleContents _   ) = []
109 ieNames (IEGroup          _ _ ) = []
110 ieNames (IEDoc            _   ) = []
111 ieNames (IEDocNamed       _   ) = []        
112 \end{code}
113
114 \begin{code}
115 instance (Outputable name) => Outputable (IE name) where
116     ppr (IEVar          var)    = pprHsVar var
117     ppr (IEThingAbs     thing)  = ppr thing
118     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
119     ppr (IEThingWith thing withs)
120         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
121     ppr (IEModuleContents mod)
122         = ptext (sLit "module") <+> ppr mod
123     ppr (IEGroup n _)           = text ("<IEGroup: " ++ (show n) ++ ">")
124     ppr (IEDoc doc)             = ppr doc
125     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
126 \end{code}
127
128