720c51f163e507633187bf7dab736a8f56e67f4e
[ghc-hetmet.git] / compiler / basicTypes / Module.lhs
1 %
2 % (c) The University of Glasgow, 2004
3 %
4
5 Module
6 ~~~~~~~~~~
7 Simply the name of a module, represented as a FastString.
8 These are Uniquable, hence we can build FiniteMaps with Modules as
9 the keys.
10
11 \begin{code}
12 module Module 
13     (
14         -- * The ModuleName type
15         ModuleName,
16         pprModuleName,
17         moduleNameFS,
18         moduleNameString,
19         mkModuleName,
20         mkModuleNameFS,
21
22         -- * The Module type
23         Module,
24         modulePackageId, moduleName,
25         pprModule,
26         mkModule,
27
28         -- * The ModuleLocation type
29         ModLocation(..),
30         addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
31
32         -- * Module mappings
33         ModuleEnv,
34         elemModuleEnv, extendModuleEnv, extendModuleEnvList, 
35         extendModuleEnvList_C, plusModuleEnv_C,
36         delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
37         lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
38         moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv,
39         extendModuleEnv_C, filterModuleEnv,
40
41         -- * ModuleName mappings
42         ModuleNameEnv,
43
44         -- * Sets of modules
45         ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
46         elemModuleSet
47     ) where
48
49 #include "HsVersions.h"
50 import Outputable
51 import Unique           ( Uniquable(..) )
52 import FiniteMap
53 import UniqFM
54 import PackageConfig    ( PackageId, packageIdFS, mainPackageId )
55 import FastString
56 import Binary
57 \end{code}
58
59 %************************************************************************
60 %*                                                                      *
61 \subsection{Module locations}
62 %*                                                                      *
63 %************************************************************************
64
65 \begin{code}
66 data ModLocation
67    = ModLocation {
68         ml_hs_file   :: Maybe FilePath,
69                 -- The source file, if we have one.  Package modules
70                 -- probably don't have source files.
71
72         ml_hi_file   :: FilePath,
73                 -- Where the .hi file is, whether or not it exists
74                 -- yet.  Always of form foo.hi, even if there is an
75                 -- hi-boot file (we add the -boot suffix later)
76
77         ml_obj_file  :: FilePath
78                 -- Where the .o file is, whether or not it exists yet.
79                 -- (might not exist either because the module hasn't
80                 -- been compiled yet, or because it is part of a
81                 -- package with a .a file)
82   } deriving Show
83
84 instance Outputable ModLocation where
85    ppr = text . show
86 \end{code}
87
88 For a module in another package, the hs_file and obj_file
89 components of ModLocation are undefined.  
90
91 The locations specified by a ModLocation may or may not
92 correspond to actual files yet: for example, even if the object
93 file doesn't exist, the ModLocation still contains the path to
94 where the object file will reside if/when it is created.
95
96 \begin{code}
97 addBootSuffix :: FilePath -> FilePath
98 -- Add the "-boot" suffix to .hs, .hi and .o files
99 addBootSuffix path = path ++ "-boot"
100
101 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
102 addBootSuffix_maybe is_boot path
103  | is_boot   = addBootSuffix path
104  | otherwise = path
105
106 addBootSuffixLocn :: ModLocation -> ModLocation
107 addBootSuffixLocn locn
108   = locn { ml_hs_file  = fmap addBootSuffix (ml_hs_file locn)
109          , ml_hi_file  = addBootSuffix (ml_hi_file locn)
110          , ml_obj_file = addBootSuffix (ml_obj_file locn) }
111 \end{code}
112
113
114 %************************************************************************
115 %*                                                                      *
116 \subsection{The name of a module}
117 %*                                                                      *
118 %************************************************************************
119
120 \begin{code}
121 -- | A ModuleName is a simple string, eg. @Data.List@.
122 newtype ModuleName = ModuleName FastString
123
124 instance Uniquable ModuleName where
125   getUnique (ModuleName nm) = getUnique nm
126
127 instance Eq ModuleName where
128   nm1 == nm2 = getUnique nm1 == getUnique nm2
129
130 -- Warning: gives an ordering relation based on the uniques of the
131 -- FastStrings which are the (encoded) module names.  This is _not_
132 -- a lexicographical ordering.
133 instance Ord ModuleName where
134   nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
135
136 instance Outputable ModuleName where
137   ppr = pprModuleName
138
139 instance Binary ModuleName where
140   put_ bh (ModuleName fs) = put_ bh fs
141   get bh = do fs <- get bh; return (ModuleName fs)
142
143 pprModuleName :: ModuleName -> SDoc
144 pprModuleName (ModuleName nm) = 
145     getPprStyle $ \ sty ->
146     if codeStyle sty 
147         then ftext (zEncodeFS nm)
148         else ftext nm
149
150 moduleNameFS :: ModuleName -> FastString
151 moduleNameFS (ModuleName mod) = mod
152
153 moduleNameString :: ModuleName -> String
154 moduleNameString (ModuleName mod) = unpackFS mod
155
156 mkModuleName :: String -> ModuleName
157 mkModuleName s = ModuleName (mkFastString s)
158
159 mkModuleNameFS :: FastString -> ModuleName
160 mkModuleNameFS s = ModuleName s
161 \end{code}
162
163 %************************************************************************
164 %*                                                                      *
165 \subsection{A fully qualified module}
166 %*                                                                      *
167 %************************************************************************
168
169 \begin{code}
170 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
171 data Module = Module {
172    modulePackageId :: !PackageId,  -- pkg-1.0
173    moduleName      :: !ModuleName  -- A.B.C
174   }
175   deriving (Eq, Ord)
176
177 instance Outputable Module where
178   ppr = pprModule
179
180 instance Binary Module where
181   put_ bh (Module p n) = put_ bh p >> put_ bh n
182   get bh = do p <- get bh; n <- get bh; return (Module p n)
183
184 mkModule :: PackageId -> ModuleName -> Module
185 mkModule = Module
186
187 pprModule :: Module -> SDoc
188 pprModule mod@(Module p n)  = pprPackagePrefix p mod <> pprModuleName n
189
190 pprPackagePrefix p mod = getPprStyle doc
191  where
192    doc sty
193        | codeStyle sty = 
194           if p == mainPackageId 
195                 then empty -- never qualify the main package in code
196                 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
197        | Just pkg <- qualModule sty mod = ftext (packageIdFS pkg) <> char ':'
198                 -- the PrintUnqualified tells us which modules have to
199                 -- be qualified with package names
200        | otherwise = empty
201 \end{code}
202
203 %************************************************************************
204 %*                                                                      *
205 \subsection{@ModuleEnv@s}
206 %*                                                                      *
207 %************************************************************************
208
209 \begin{code}
210 type ModuleEnv elt = FiniteMap Module elt
211
212 emptyModuleEnv       :: ModuleEnv a
213 mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
214 unitModuleEnv        :: Module -> a -> ModuleEnv a
215 extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
216 extendModuleEnv_C    :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
217 plusModuleEnv        :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
218 extendModuleEnvList  :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
219 extendModuleEnvList_C  :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
220                   
221 delModuleEnvList     :: ModuleEnv a -> [Module] -> ModuleEnv a
222 delModuleEnv         :: ModuleEnv a -> Module -> ModuleEnv a
223 plusModuleEnv_C      :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
224 mapModuleEnv         :: (a -> b) -> ModuleEnv a -> ModuleEnv b
225 moduleEnvElts        :: ModuleEnv a -> [a]
226                   
227 isEmptyModuleEnv     :: ModuleEnv a -> Bool
228 lookupModuleEnv      :: ModuleEnv a -> Module     -> Maybe a
229 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
230 elemModuleEnv        :: Module -> ModuleEnv a -> Bool
231 foldModuleEnv        :: (a -> b -> b) -> b -> ModuleEnv a -> b
232 filterModuleEnv      :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
233
234 filterModuleEnv f   = filterFM (\_ v -> f v)
235 elemModuleEnv       = elemFM
236 extendModuleEnv     = addToFM
237 extendModuleEnv_C   = addToFM_C
238 extendModuleEnvList = addListToFM
239 extendModuleEnvList_C = addListToFM_C
240 plusModuleEnv_C     = plusFM_C
241 delModuleEnvList    = delListFromFM
242 delModuleEnv        = delFromFM
243 plusModuleEnv       = plusFM
244 lookupModuleEnv     = lookupFM
245 lookupWithDefaultModuleEnv = lookupWithDefaultFM
246 mapModuleEnv f      = mapFM (\_ v -> f v)
247 mkModuleEnv         = listToFM
248 emptyModuleEnv      = emptyFM
249 moduleEnvElts       = eltsFM
250 unitModuleEnv       = unitFM
251 isEmptyModuleEnv    = isEmptyFM
252 foldModuleEnv f     = foldFM (\_ v -> f v)
253 \end{code}
254
255 \begin{code}
256 type ModuleSet = FiniteMap Module ()
257 mkModuleSet     :: [Module] -> ModuleSet
258 extendModuleSet :: ModuleSet -> Module -> ModuleSet
259 emptyModuleSet  :: ModuleSet
260 moduleSetElts   :: ModuleSet -> [Module]
261 elemModuleSet   :: Module -> ModuleSet -> Bool
262
263 emptyModuleSet    = emptyFM
264 mkModuleSet ms    = listToFM [(m,()) | m <- ms ]
265 extendModuleSet s m = addToFM s m ()
266 moduleSetElts     = keysFM
267 elemModuleSet     = elemFM
268 \end{code}
269
270 A ModuleName has a Unique, so we can build mappings of these using
271 UniqFM.
272
273 \begin{code}
274 type ModuleNameEnv elt = UniqFM elt
275 \end{code}