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