Convert more UniqFM's back to LazyUniqFM's
[ghc-hetmet.git] / compiler / basicTypes / Module.lhs
1 %
2 % (c) The University of Glasgow, 2004-2006
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         moduleNameSlashes,
20         mkModuleName,
21         mkModuleNameFS,
22
23         -- * The PackageId type
24         PackageId,
25         fsToPackageId,
26         packageIdFS,
27         stringToPackageId,
28         packageIdString,
29
30         -- * Wired-in PackageIds
31         basePackageId,
32         rtsPackageId,
33         haskell98PackageId,
34         thPackageId,
35         ndpPackageId,
36         mainPackageId,
37
38         -- * The Module type
39         Module,
40         modulePackageId, moduleName,
41         pprModule,
42         mkModule,
43
44         -- * The ModuleLocation type
45         ModLocation(..),
46         addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
47
48         -- * Module mappings
49         ModuleEnv,
50         elemModuleEnv, extendModuleEnv, extendModuleEnvList, 
51         extendModuleEnvList_C, plusModuleEnv_C,
52         delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
53         lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
54         moduleEnvKeys, moduleEnvElts, unitModuleEnv, isEmptyModuleEnv,
55         foldModuleEnv, extendModuleEnv_C, filterModuleEnv,
56
57         -- * ModuleName mappings
58         ModuleNameEnv,
59
60         -- * Sets of modules
61         ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
62         elemModuleSet
63     ) where
64
65 #include "HsVersions.h"
66 import Outputable
67 import qualified Pretty
68 import Unique
69 import FiniteMap
70 import LazyUniqFM
71 import FastString
72 import Binary
73
74 import System.FilePath
75 \end{code}
76
77 %************************************************************************
78 %*                                                                      *
79 \subsection{Module locations}
80 %*                                                                      *
81 %************************************************************************
82
83 \begin{code}
84 data ModLocation
85    = ModLocation {
86         ml_hs_file   :: Maybe FilePath,
87                 -- The source file, if we have one.  Package modules
88                 -- probably don't have source files.
89
90         ml_hi_file   :: FilePath,
91                 -- Where the .hi file is, whether or not it exists
92                 -- yet.  Always of form foo.hi, even if there is an
93                 -- hi-boot file (we add the -boot suffix later)
94
95         ml_obj_file  :: FilePath
96                 -- Where the .o file is, whether or not it exists yet.
97                 -- (might not exist either because the module hasn't
98                 -- been compiled yet, or because it is part of a
99                 -- package with a .a file)
100   } deriving Show
101
102 instance Outputable ModLocation where
103    ppr = text . show
104 \end{code}
105
106 For a module in another package, the hs_file and obj_file
107 components of ModLocation are undefined.  
108
109 The locations specified by a ModLocation may or may not
110 correspond to actual files yet: for example, even if the object
111 file doesn't exist, the ModLocation still contains the path to
112 where the object file will reside if/when it is created.
113
114 \begin{code}
115 addBootSuffix :: FilePath -> FilePath
116 -- Add the "-boot" suffix to .hs, .hi and .o files
117 addBootSuffix path = path ++ "-boot"
118
119 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
120 addBootSuffix_maybe is_boot path
121  | is_boot   = addBootSuffix path
122  | otherwise = path
123
124 addBootSuffixLocn :: ModLocation -> ModLocation
125 addBootSuffixLocn locn
126   = locn { ml_hs_file  = fmap addBootSuffix (ml_hs_file locn)
127          , ml_hi_file  = addBootSuffix (ml_hi_file locn)
128          , ml_obj_file = addBootSuffix (ml_obj_file locn) }
129 \end{code}
130
131
132 %************************************************************************
133 %*                                                                      *
134 \subsection{The name of a module}
135 %*                                                                      *
136 %************************************************************************
137
138 \begin{code}
139 -- | A ModuleName is a simple string, eg. @Data.List@.
140 newtype ModuleName = ModuleName FastString
141
142 instance Uniquable ModuleName where
143   getUnique (ModuleName nm) = getUnique nm
144
145 instance Eq ModuleName where
146   nm1 == nm2 = getUnique nm1 == getUnique nm2
147
148 -- Warning: gives an ordering relation based on the uniques of the
149 -- FastStrings which are the (encoded) module names.  This is _not_
150 -- a lexicographical ordering.
151 instance Ord ModuleName where
152   nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
153
154 instance Outputable ModuleName where
155   ppr = pprModuleName
156
157 instance Binary ModuleName where
158   put_ bh (ModuleName fs) = put_ bh fs
159   get bh = do fs <- get bh; return (ModuleName fs)
160
161 pprModuleName :: ModuleName -> SDoc
162 pprModuleName (ModuleName nm) = 
163     getPprStyle $ \ sty ->
164     if codeStyle sty 
165         then ftext (zEncodeFS nm)
166         else ftext nm
167
168 moduleNameFS :: ModuleName -> FastString
169 moduleNameFS (ModuleName mod) = mod
170
171 moduleNameString :: ModuleName -> String
172 moduleNameString (ModuleName mod) = unpackFS mod
173
174 mkModuleName :: String -> ModuleName
175 mkModuleName s = ModuleName (mkFastString s)
176
177 mkModuleNameFS :: FastString -> ModuleName
178 mkModuleNameFS s = ModuleName s
179
180 -- Returns the string version of the module name, with dots replaced by slashes
181 moduleNameSlashes :: ModuleName -> String
182 moduleNameSlashes = dots_to_slashes . moduleNameString
183   where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
184 \end{code}
185
186 %************************************************************************
187 %*                                                                      *
188 \subsection{A fully qualified module}
189 %*                                                                      *
190 %************************************************************************
191
192 \begin{code}
193 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
194 data Module = Module {
195    modulePackageId :: !PackageId,  -- pkg-1.0
196    moduleName      :: !ModuleName  -- A.B.C
197   }
198   deriving (Eq, Ord)
199
200 instance Outputable Module where
201   ppr = pprModule
202
203 instance Binary Module where
204   put_ bh (Module p n) = put_ bh p >> put_ bh n
205   get bh = do p <- get bh; n <- get bh; return (Module p n)
206
207 instance Uniquable PackageId where
208  getUnique pid = getUnique (packageIdFS pid)
209
210 mkModule :: PackageId -> ModuleName -> Module
211 mkModule = Module
212
213 pprModule :: Module -> SDoc
214 pprModule mod@(Module p n)  = pprPackagePrefix p mod <> pprModuleName n
215
216 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
217 pprPackagePrefix p mod = getPprStyle doc
218  where
219    doc sty
220        | codeStyle sty = 
221           if p == mainPackageId 
222                 then empty -- never qualify the main package in code
223                 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
224        | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
225                 -- the PrintUnqualified tells us which modules have to
226                 -- be qualified with package names
227        | otherwise = empty
228 \end{code}
229
230 %************************************************************************
231 %*                                                                      *
232 \subsection{PackageId}
233 %*                                                                      *
234 %************************************************************************
235
236 \begin{code}
237 newtype PackageId = PId FastString deriving( Eq, Ord )  -- includes the version
238     -- here to avoid module loops with PackageConfig
239
240 instance Outputable PackageId where
241    ppr pid = text (packageIdString pid)
242
243 instance Binary PackageId where
244   put_ bh pid = put_ bh (packageIdFS pid)
245   get bh = do { fs <- get bh; return (fsToPackageId fs) }
246
247 fsToPackageId :: FastString -> PackageId
248 fsToPackageId = PId
249
250 packageIdFS :: PackageId -> FastString
251 packageIdFS (PId fs) = fs
252
253 stringToPackageId :: String -> PackageId
254 stringToPackageId = fsToPackageId . mkFastString
255
256 packageIdString :: PackageId -> String
257 packageIdString = unpackFS . packageIdFS
258
259
260 -- -----------------------------------------------------------------------------
261 -- Package Ids that are wired in
262
263 -- Certain packages are "known" to the compiler, in that we know about certain
264 -- entities that reside in these packages, and the compiler needs to 
265 -- declare static Modules and Names that refer to these packages.  Hence
266 -- the wired-in packages can't include version numbers, since we don't want
267 -- to bake the version numbers of these packages into GHC.
268 --
269 -- So here's the plan.  Wired-in packages are still versioned as
270 -- normal in the packages database, and you can still have multiple
271 -- versions of them installed.  However, for each invocation of GHC,
272 -- only a single instance of each wired-in package will be recognised
273 -- (the desired one is selected via -package/-hide-package), and GHC
274 -- will use the unversioned PackageId below when referring to it,
275 -- including in .hi files and object file symbols.  Unselected
276 -- versions of wired-in packages will be ignored, as will any other
277 -- package that depends directly or indirectly on it (much as if you
278 -- had used -ignore-package).
279
280 basePackageId, rtsPackageId, haskell98PackageId, 
281   thPackageId, ndpPackageId, mainPackageId  :: PackageId
282 basePackageId      = fsToPackageId FSLIT("base")
283 rtsPackageId       = fsToPackageId FSLIT("rts")
284 haskell98PackageId = fsToPackageId FSLIT("haskell98")
285 thPackageId        = fsToPackageId FSLIT("template-haskell")
286 ndpPackageId       = fsToPackageId FSLIT("ndp")
287
288 -- This is the package Id for the program.  It is the default package
289 -- Id if you don't specify a package name.  We don't add this prefix
290 -- to symbol name, since there can be only one main package per program.
291 mainPackageId      = fsToPackageId FSLIT("main")
292 \end{code}
293
294 %************************************************************************
295 %*                                                                      *
296 \subsection{@ModuleEnv@s}
297 %*                                                                      *
298 %************************************************************************
299
300 \begin{code}
301 type ModuleEnv elt = FiniteMap Module elt
302
303 emptyModuleEnv       :: ModuleEnv a
304 mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
305 unitModuleEnv        :: Module -> a -> ModuleEnv a
306 extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
307 extendModuleEnv_C    :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
308 plusModuleEnv        :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
309 extendModuleEnvList  :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
310 extendModuleEnvList_C  :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
311                   
312 delModuleEnvList     :: ModuleEnv a -> [Module] -> ModuleEnv a
313 delModuleEnv         :: ModuleEnv a -> Module -> ModuleEnv a
314 plusModuleEnv_C      :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
315 mapModuleEnv         :: (a -> b) -> ModuleEnv a -> ModuleEnv b
316 moduleEnvKeys        :: ModuleEnv a -> [Module]
317 moduleEnvElts        :: ModuleEnv a -> [a]
318                   
319 isEmptyModuleEnv     :: ModuleEnv a -> Bool
320 lookupModuleEnv      :: ModuleEnv a -> Module     -> Maybe a
321 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
322 elemModuleEnv        :: Module -> ModuleEnv a -> Bool
323 foldModuleEnv        :: (a -> b -> b) -> b -> ModuleEnv a -> b
324 filterModuleEnv      :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
325
326 filterModuleEnv f   = filterFM (\_ v -> f v)
327 elemModuleEnv       = elemFM
328 extendModuleEnv     = addToFM
329 extendModuleEnv_C   = addToFM_C
330 extendModuleEnvList = addListToFM
331 extendModuleEnvList_C = addListToFM_C
332 plusModuleEnv_C     = plusFM_C
333 delModuleEnvList    = delListFromFM
334 delModuleEnv        = delFromFM
335 plusModuleEnv       = plusFM
336 lookupModuleEnv     = lookupFM
337 lookupWithDefaultModuleEnv = lookupWithDefaultFM
338 mapModuleEnv f      = mapFM (\_ v -> f v)
339 mkModuleEnv         = listToFM
340 emptyModuleEnv      = emptyFM
341 moduleEnvKeys       = keysFM
342 moduleEnvElts       = eltsFM
343 unitModuleEnv       = unitFM
344 isEmptyModuleEnv    = isEmptyFM
345 foldModuleEnv f     = foldFM (\_ v -> f v)
346 \end{code}
347
348 \begin{code}
349 type ModuleSet = FiniteMap Module ()
350 mkModuleSet     :: [Module] -> ModuleSet
351 extendModuleSet :: ModuleSet -> Module -> ModuleSet
352 emptyModuleSet  :: ModuleSet
353 moduleSetElts   :: ModuleSet -> [Module]
354 elemModuleSet   :: Module -> ModuleSet -> Bool
355
356 emptyModuleSet    = emptyFM
357 mkModuleSet ms    = listToFM [(m,()) | m <- ms ]
358 extendModuleSet s m = addToFM s m ()
359 moduleSetElts     = keysFM
360 elemModuleSet     = elemFM
361 \end{code}
362
363 A ModuleName has a Unique, so we can build mappings of these using
364 UniqFM.
365
366 \begin{code}
367 type ModuleNameEnv elt = UniqFM elt
368 \end{code}