bbe84e3aaced5f0246a2216b632cadb0b84c18ca
[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 module HsImpExp where
10
11 #include "HsVersions.h"
12
13 import Module           ( ModuleName )
14 import HsDoc            ( HsDoc )
15
16 import Outputable
17 import FastString
18 import SrcLoc           ( Located(..) )
19 import Char             ( isAlpha )
20 \end{code}
21
22 %************************************************************************
23 %*                                                                      *
24 \subsection{Import and export declaration lists}
25 %*                                                                      *
26 %************************************************************************
27
28 One per \tr{import} declaration in a module.
29 \begin{code}
30 type LImportDecl name = Located (ImportDecl name)
31
32 data ImportDecl name
33   = ImportDecl    (Located ModuleName)          -- module name
34                   Bool                          -- True <=> {-# SOURCE #-} import
35                   Bool                          -- True => qualified
36                   (Maybe ModuleName)            -- as Module
37                   (Maybe (Bool, [LIE name]))    -- (True => hiding, names)
38 \end{code}
39
40 \begin{code}
41 instance (Outputable name) => Outputable (ImportDecl name) where
42     ppr (ImportDecl mod from qual as spec)
43       = hang (hsep [ptext SLIT("import"), ppr_imp from, 
44                     pp_qual qual, ppr mod, pp_as as])
45              4 (pp_spec spec)
46       where
47         pp_qual False   = empty
48         pp_qual True    = ptext SLIT("qualified")
49
50         pp_as Nothing   = empty
51         pp_as (Just a)  = ptext SLIT("as ") <+> ppr a
52
53         ppr_imp True  = ptext SLIT("{-# SOURCE #-}")
54         ppr_imp False = empty
55
56         pp_spec Nothing = empty
57         pp_spec (Just (False, spec))
58                         = parens (interpp'SP spec)
59         pp_spec (Just (True, spec))
60                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
61
62 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
63 \end{code}
64
65 %************************************************************************
66 %*                                                                      *
67 \subsection{Imported and exported entities}
68 %*                                                                      *
69 %************************************************************************
70
71 \begin{code}
72 type LIE name = Located (IE name)
73
74 data IE name
75   = IEVar               name
76   | IEThingAbs          name             -- Class/Type (can't tell)
77   | IEThingAll          name             -- Class/Type plus all methods/constructors
78   | IEThingWith         name [name]      -- Class/Type plus some methods/constructors
79   | IEModuleContents    ModuleName       -- (Export Only)
80   | IEGroup             Int (HsDoc name) -- Doc section heading
81   | IEDoc               (HsDoc name)     -- Some documentation
82   | IEDocNamed          String           -- Reference to named doc
83 \end{code}
84
85 \begin{code}
86 ieName :: IE name -> name
87 ieName (IEVar n)         = n
88 ieName (IEThingAbs  n)   = n
89 ieName (IEThingWith n _) = n
90 ieName (IEThingAll  n)   = n
91
92 ieNames :: IE a -> [a]
93 ieNames (IEVar            n   ) = [n]
94 ieNames (IEThingAbs       n   ) = [n]
95 ieNames (IEThingAll       n   ) = [n]
96 ieNames (IEThingWith      n ns) = n:ns
97 ieNames (IEModuleContents _   ) = []
98 ieNames (IEGroup          _ _ ) = []
99 ieNames (IEDoc            _   ) = []
100 ieNames (IEDocNamed       _   ) = []        
101 \end{code}
102
103 \begin{code}
104 instance (Outputable name) => Outputable (IE name) where
105     ppr (IEVar          var)    = pprHsVar var
106     ppr (IEThingAbs     thing)  = ppr thing
107     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
108     ppr (IEThingWith thing withs)
109         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
110     ppr (IEModuleContents mod)
111         = ptext SLIT("module") <+> ppr mod
112     ppr (IEGroup n doc)         = text ("<IEGroup: " ++ (show n) ++ ">") 
113     ppr (IEDoc doc)             = ppr doc
114     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
115 \end{code}
116
117 \begin{code}
118 pprHsVar :: Outputable name => name -> SDoc
119 pprHsVar v | isOperator ppr_v = parens ppr_v
120            | otherwise        = ppr_v
121            where
122              ppr_v = ppr v
123
124 isOperator :: SDoc -> Bool
125 isOperator ppr_v 
126   = case showSDocUnqual ppr_v of
127         ('(':s)   -> False              -- (), (,) etc
128         ('[':s)   -> False              -- []
129         ('$':c:s) -> not (isAlpha c)    -- Don't treat $d as an operator
130         (':':c:s) -> not (isAlpha c)    -- Don't treat :T as an operator
131         ('_':s)   -> False              -- Not an operator
132         (c:s)     -> not (isAlpha c)    -- Starts with non-alpha
133         other     -> False
134     -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
135     -- that we don't need NamedThing in the context of all these functions.
136     -- Gruesome, but simple.
137 \end{code}
138