2 % (c) The University of Glasgow, 2004-2006
7 Simply the name of a module, represented as a FastString.
8 These are Uniquable, hence we can build FiniteMaps with Modules as
14 -- * The ModuleName type
24 -- * The PackageId type
32 -- * Wired-in PackageIds
48 modulePackageId, moduleName,
53 -- * The ModuleLocation type
55 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
59 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
60 extendModuleEnvList_C, plusModuleEnv_C,
61 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
62 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
63 moduleEnvKeys, moduleEnvElts, unitModuleEnv, isEmptyModuleEnv,
64 foldModuleEnv, extendModuleEnv_C, filterModuleEnv,
66 -- * ModuleName mappings
71 emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
75 import qualified Pretty
83 import System.FilePath
86 %************************************************************************
88 \subsection{Module locations}
90 %************************************************************************
93 -- | Where a module lives on the file system: the actual locations
94 -- of the .hs, .hi and .o files, if we have them
97 ml_hs_file :: Maybe FilePath,
98 -- The source file, if we have one. Package modules
99 -- probably don't have source files.
101 ml_hi_file :: FilePath,
102 -- Where the .hi file is, whether or not it exists
103 -- yet. Always of form foo.hi, even if there is an
104 -- hi-boot file (we add the -boot suffix later)
106 ml_obj_file :: FilePath
107 -- Where the .o file is, whether or not it exists yet.
108 -- (might not exist either because the module hasn't
109 -- been compiled yet, or because it is part of a
110 -- package with a .a file)
113 instance Outputable ModLocation where
117 For a module in another package, the hs_file and obj_file
118 components of ModLocation are undefined.
120 The locations specified by a ModLocation may or may not
121 correspond to actual files yet: for example, even if the object
122 file doesn't exist, the ModLocation still contains the path to
123 where the object file will reside if/when it is created.
126 addBootSuffix :: FilePath -> FilePath
127 -- ^ Add the @-boot@ suffix to .hs, .hi and .o files
128 addBootSuffix path = path ++ "-boot"
130 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
131 -- ^ Add the @-boot@ suffix if the @Bool@ argument is @True@
132 addBootSuffix_maybe is_boot path
133 | is_boot = addBootSuffix path
136 addBootSuffixLocn :: ModLocation -> ModLocation
137 -- ^ Add the @-boot@ suffix to all file paths associated with the module
138 addBootSuffixLocn locn
139 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
140 , ml_hi_file = addBootSuffix (ml_hi_file locn)
141 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
145 %************************************************************************
147 \subsection{The name of a module}
149 %************************************************************************
152 -- | A ModuleName is essentially a simple string, e.g. @Data.List@.
153 newtype ModuleName = ModuleName FastString
155 instance Uniquable ModuleName where
156 getUnique (ModuleName nm) = getUnique nm
158 instance Eq ModuleName where
159 nm1 == nm2 = getUnique nm1 == getUnique nm2
161 -- Warning: gives an ordering relation based on the uniques of the
162 -- FastStrings which are the (encoded) module names. This is _not_
163 -- a lexicographical ordering.
164 instance Ord ModuleName where
165 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
167 instance Outputable ModuleName where
170 instance Binary ModuleName where
171 put_ bh (ModuleName fs) = put_ bh fs
172 get bh = do fs <- get bh; return (ModuleName fs)
174 stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
175 -- ^ Compares module names lexically, rather than by their 'Unique's
176 stableModuleNameCmp n1 n2 = moduleNameFS n1 `compare` moduleNameFS n2
178 pprModuleName :: ModuleName -> SDoc
179 pprModuleName (ModuleName nm) =
180 getPprStyle $ \ sty ->
182 then ftext (zEncodeFS nm)
185 moduleNameFS :: ModuleName -> FastString
186 moduleNameFS (ModuleName mod) = mod
188 moduleNameString :: ModuleName -> String
189 moduleNameString (ModuleName mod) = unpackFS mod
191 mkModuleName :: String -> ModuleName
192 mkModuleName s = ModuleName (mkFastString s)
194 mkModuleNameFS :: FastString -> ModuleName
195 mkModuleNameFS s = ModuleName s
197 -- | Returns the string version of the module name, with dots replaced by slashes
198 moduleNameSlashes :: ModuleName -> String
199 moduleNameSlashes = dots_to_slashes . moduleNameString
200 where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
203 %************************************************************************
205 \subsection{A fully qualified module}
207 %************************************************************************
210 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
211 data Module = Module {
212 modulePackageId :: !PackageId, -- pkg-1.0
213 moduleName :: !ModuleName -- A.B.C
217 instance Uniquable Module where
218 getUnique (Module p n) = getUnique (packageIdFS p `appendFS` moduleNameFS n)
220 instance Outputable Module where
223 instance Binary Module where
224 put_ bh (Module p n) = put_ bh p >> put_ bh n
225 get bh = do p <- get bh; n <- get bh; return (Module p n)
227 -- | This gives a stable ordering, as opposed to the Ord instance which
228 -- gives an ordering based on the 'Unique's of the components, which may
229 -- not be stable from run to run of the compiler.
230 stableModuleCmp :: Module -> Module -> Ordering
231 stableModuleCmp (Module p1 n1) (Module p2 n2)
232 = (p1 `stablePackageIdCmp` p2) `thenCmp`
233 (n1 `stableModuleNameCmp` n2)
235 mkModule :: PackageId -> ModuleName -> Module
238 pprModule :: Module -> SDoc
239 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
241 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
242 pprPackagePrefix p mod = getPprStyle doc
246 if p == mainPackageId
247 then empty -- never qualify the main package in code
248 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
249 | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
250 -- the PrintUnqualified tells us which modules have to
251 -- be qualified with package names
255 %************************************************************************
257 \subsection{PackageId}
259 %************************************************************************
262 -- | Essentially just a string identifying a package, including the version: e.g. parsec-1.0
263 newtype PackageId = PId FastString deriving( Eq )
264 -- here to avoid module loops with PackageConfig
266 instance Uniquable PackageId where
267 getUnique pid = getUnique (packageIdFS pid)
269 -- Note: *not* a stable lexicographic ordering, a faster unique-based
271 instance Ord PackageId where
272 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
274 stablePackageIdCmp :: PackageId -> PackageId -> Ordering
275 -- ^ Compares package ids lexically, rather than by their 'Unique's
276 stablePackageIdCmp p1 p2 = packageIdFS p1 `compare` packageIdFS p2
278 instance Outputable PackageId where
279 ppr pid = text (packageIdString pid)
281 instance Binary PackageId where
282 put_ bh pid = put_ bh (packageIdFS pid)
283 get bh = do { fs <- get bh; return (fsToPackageId fs) }
285 fsToPackageId :: FastString -> PackageId
288 packageIdFS :: PackageId -> FastString
289 packageIdFS (PId fs) = fs
291 stringToPackageId :: String -> PackageId
292 stringToPackageId = fsToPackageId . mkFastString
294 packageIdString :: PackageId -> String
295 packageIdString = unpackFS . packageIdFS
298 -- -----------------------------------------------------------------------------
299 -- $wired_in_packages
300 -- Certain packages are known to the compiler, in that we know about certain
301 -- entities that reside in these packages, and the compiler needs to
302 -- declare static Modules and Names that refer to these packages. Hence
303 -- the wired-in packages can't include version numbers, since we don't want
304 -- to bake the version numbers of these packages into GHC.
306 -- So here's the plan. Wired-in packages are still versioned as
307 -- normal in the packages database, and you can still have multiple
308 -- versions of them installed. However, for each invocation of GHC,
309 -- only a single instance of each wired-in package will be recognised
310 -- (the desired one is selected via @-package@\/@-hide-package@), and GHC
311 -- will use the unversioned 'PackageId' below when referring to it,
312 -- including in .hi files and object file symbols. Unselected
313 -- versions of wired-in packages will be ignored, as will any other
314 -- package that depends directly or indirectly on it (much as if you
315 -- had used @-ignore-package@).
317 -- Make sure you change 'Packages.findWiredInPackages' if you add an entry here
319 integerPackageId, primPackageId,
320 basePackageId, rtsPackageId, haskell98PackageId, sybPackageId,
321 thPackageId, ndpPackageId, dphSeqPackageId, dphParPackageId,
322 mainPackageId :: PackageId
323 primPackageId = fsToPackageId (fsLit "ghc-prim")
324 integerPackageId = fsToPackageId (fsLit "integer")
325 basePackageId = fsToPackageId (fsLit "base")
326 rtsPackageId = fsToPackageId (fsLit "rts")
327 haskell98PackageId = fsToPackageId (fsLit "haskell98")
328 sybPackageId = fsToPackageId (fsLit "syb")
329 thPackageId = fsToPackageId (fsLit "template-haskell")
330 ndpPackageId = fsToPackageId (fsLit "ndp")
331 dphSeqPackageId = fsToPackageId (fsLit "dph-seq")
332 dphParPackageId = fsToPackageId (fsLit "dph-par")
334 -- | This is the package Id for the current program. It is the default
335 -- package Id if you don't specify a package name. We don't add this prefix
336 -- to symbol names, since there can be only one main package per program.
337 mainPackageId = fsToPackageId (fsLit "main")
340 %************************************************************************
342 \subsection{@ModuleEnv@s}
344 %************************************************************************
347 -- | A map keyed off of 'Module's
348 type ModuleEnv elt = FiniteMap Module elt
350 emptyModuleEnv :: ModuleEnv a
351 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
352 unitModuleEnv :: Module -> a -> ModuleEnv a
353 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
354 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
355 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
356 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
357 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
359 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
360 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
361 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
362 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
363 moduleEnvKeys :: ModuleEnv a -> [Module]
364 moduleEnvElts :: ModuleEnv a -> [a]
366 isEmptyModuleEnv :: ModuleEnv a -> Bool
367 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
368 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
369 elemModuleEnv :: Module -> ModuleEnv a -> Bool
370 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
371 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
373 filterModuleEnv f = filterFM (\_ v -> f v)
374 elemModuleEnv = elemFM
375 extendModuleEnv = addToFM
376 extendModuleEnv_C = addToFM_C
377 extendModuleEnvList = addListToFM
378 extendModuleEnvList_C = addListToFM_C
379 plusModuleEnv_C = plusFM_C
380 delModuleEnvList = delListFromFM
381 delModuleEnv = delFromFM
382 plusModuleEnv = plusFM
383 lookupModuleEnv = lookupFM
384 lookupWithDefaultModuleEnv = lookupWithDefaultFM
385 mapModuleEnv f = mapFM (\_ v -> f v)
386 mkModuleEnv = listToFM
387 emptyModuleEnv = emptyFM
388 moduleEnvKeys = keysFM
389 moduleEnvElts = eltsFM
390 unitModuleEnv = unitFM
391 isEmptyModuleEnv = isEmptyFM
392 foldModuleEnv f = foldFM (\_ v -> f v)
396 -- | A set of 'Module's
397 type ModuleSet = FiniteMap Module ()
399 mkModuleSet :: [Module] -> ModuleSet
400 extendModuleSet :: ModuleSet -> Module -> ModuleSet
401 emptyModuleSet :: ModuleSet
402 moduleSetElts :: ModuleSet -> [Module]
403 elemModuleSet :: Module -> ModuleSet -> Bool
405 emptyModuleSet = emptyFM
406 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
407 extendModuleSet s m = addToFM s m ()
408 moduleSetElts = keysFM
409 elemModuleSet = elemFM
412 A ModuleName has a Unique, so we can build mappings of these using
416 -- | A map keyed off of 'ModuleName's (actually, their 'Unique's)
417 type ModuleNameEnv elt = UniqFM elt