Fix the bug part of Trac #1930
[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 data ImportDecl name
37   = ImportDecl    (Located ModuleName)          -- module name
38                   Bool                          -- True <=> {-# SOURCE #-} import
39                   Bool                          -- True => qualified
40                   (Maybe ModuleName)            -- as Module
41                   (Maybe (Bool, [LIE name]))    -- (True => hiding, names)
42 \end{code}
43
44 \begin{code}
45 instance (Outputable name) => Outputable (ImportDecl name) where
46     ppr (ImportDecl mod from qual as spec)
47       = hang (hsep [ptext (sLit "import"), ppr_imp from, 
48                     pp_qual qual, ppr mod, pp_as as])
49              4 (pp_spec spec)
50       where
51         pp_qual False   = empty
52         pp_qual True    = ptext (sLit "qualified")
53
54         pp_as Nothing   = empty
55         pp_as (Just a)  = ptext (sLit "as ") <+> ppr a
56
57         ppr_imp True  = ptext (sLit "{-# SOURCE #-}")
58         ppr_imp False = empty
59
60         pp_spec Nothing = empty
61         pp_spec (Just (False, spec))
62                         = parens (interpp'SP spec)
63         pp_spec (Just (True, spec))
64                         = ptext (sLit "hiding") <+> parens (interpp'SP spec)
65
66 ideclName :: ImportDecl name -> Located ModuleName
67 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
68 \end{code}
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection{Imported and exported entities}
73 %*                                                                      *
74 %************************************************************************
75
76 \begin{code}
77 type LIE name = Located (IE name)
78
79 data IE name
80   = IEVar               name
81   | IEThingAbs          name             -- Class/Type (can't tell)
82   | IEThingAll          name             -- Class/Type plus all methods/constructors
83   | IEThingWith         name [name]      -- Class/Type plus some methods/constructors
84   | IEModuleContents    ModuleName       -- (Export Only)
85   | IEGroup             Int (HsDoc name) -- Doc section heading
86   | IEDoc               (HsDoc name)     -- Some documentation
87   | IEDocNamed          String           -- Reference to named doc
88 \end{code}
89
90 \begin{code}
91 ieName :: IE name -> name
92 ieName (IEVar n)         = n
93 ieName (IEThingAbs  n)   = n
94 ieName (IEThingWith n _) = n
95 ieName (IEThingAll  n)   = n
96
97 ieNames :: IE a -> [a]
98 ieNames (IEVar            n   ) = [n]
99 ieNames (IEThingAbs       n   ) = [n]
100 ieNames (IEThingAll       n   ) = [n]
101 ieNames (IEThingWith      n ns) = n:ns
102 ieNames (IEModuleContents _   ) = []
103 ieNames (IEGroup          _ _ ) = []
104 ieNames (IEDoc            _   ) = []
105 ieNames (IEDocNamed       _   ) = []        
106 \end{code}
107
108 \begin{code}
109 instance (Outputable name) => Outputable (IE name) where
110     ppr (IEVar          var)    = pprHsVar var
111     ppr (IEThingAbs     thing)  = ppr thing
112     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
113     ppr (IEThingWith thing withs)
114         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
115     ppr (IEModuleContents mod)
116         = ptext (sLit "module") <+> ppr mod
117     ppr (IEGroup n _)           = text ("<IEGroup: " ++ (show n) ++ ">")
118     ppr (IEDoc doc)             = ppr doc
119     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
120 \end{code}
121
122