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