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
47 modulePackageId, moduleName,
52 -- * The ModuleLocation type
54 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
58 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
59 extendModuleEnvList_C, plusModuleEnv_C,
60 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
61 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
62 moduleEnvKeys, moduleEnvElts, unitModuleEnv, isEmptyModuleEnv,
63 foldModuleEnv, extendModuleEnv_C, filterModuleEnv,
65 -- * ModuleName mappings
70 emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
74 import qualified Pretty
82 import System.FilePath
85 %************************************************************************
87 \subsection{Module locations}
89 %************************************************************************
92 -- | Where a module lives on the file system: the actual locations
93 -- of the .hs, .hi and .o files, if we have them
96 ml_hs_file :: Maybe FilePath,
97 -- The source file, if we have one. Package modules
98 -- probably don't have source files.
100 ml_hi_file :: FilePath,
101 -- Where the .hi file is, whether or not it exists
102 -- yet. Always of form foo.hi, even if there is an
103 -- hi-boot file (we add the -boot suffix later)
105 ml_obj_file :: FilePath
106 -- Where the .o file is, whether or not it exists yet.
107 -- (might not exist either because the module hasn't
108 -- been compiled yet, or because it is part of a
109 -- package with a .a file)
112 instance Outputable ModLocation where
116 For a module in another package, the hs_file and obj_file
117 components of ModLocation are undefined.
119 The locations specified by a ModLocation may or may not
120 correspond to actual files yet: for example, even if the object
121 file doesn't exist, the ModLocation still contains the path to
122 where the object file will reside if/when it is created.
125 addBootSuffix :: FilePath -> FilePath
126 -- ^ Add the @-boot@ suffix to .hs, .hi and .o files
127 addBootSuffix path = path ++ "-boot"
129 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
130 -- ^ Add the @-boot@ suffix if the @Bool@ argument is @True@
131 addBootSuffix_maybe is_boot path
132 | is_boot = addBootSuffix path
135 addBootSuffixLocn :: ModLocation -> ModLocation
136 -- ^ Add the @-boot@ suffix to all file paths associated with the module
137 addBootSuffixLocn locn
138 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
139 , ml_hi_file = addBootSuffix (ml_hi_file locn)
140 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
144 %************************************************************************
146 \subsection{The name of a module}
148 %************************************************************************
151 -- | A ModuleName is essentially a simple string, e.g. @Data.List@.
152 newtype ModuleName = ModuleName FastString
154 instance Uniquable ModuleName where
155 getUnique (ModuleName nm) = getUnique nm
157 instance Eq ModuleName where
158 nm1 == nm2 = getUnique nm1 == getUnique nm2
160 -- Warning: gives an ordering relation based on the uniques of the
161 -- FastStrings which are the (encoded) module names. This is _not_
162 -- a lexicographical ordering.
163 instance Ord ModuleName where
164 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
166 instance Outputable ModuleName where
169 instance Binary ModuleName where
170 put_ bh (ModuleName fs) = put_ bh fs
171 get bh = do fs <- get bh; return (ModuleName fs)
173 stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
174 -- ^ Compares module names lexically, rather than by their 'Unique's
175 stableModuleNameCmp n1 n2 = moduleNameFS n1 `compare` moduleNameFS n2
177 pprModuleName :: ModuleName -> SDoc
178 pprModuleName (ModuleName nm) =
179 getPprStyle $ \ sty ->
181 then ftext (zEncodeFS nm)
184 moduleNameFS :: ModuleName -> FastString
185 moduleNameFS (ModuleName mod) = mod
187 moduleNameString :: ModuleName -> String
188 moduleNameString (ModuleName mod) = unpackFS mod
190 mkModuleName :: String -> ModuleName
191 mkModuleName s = ModuleName (mkFastString s)
193 mkModuleNameFS :: FastString -> ModuleName
194 mkModuleNameFS s = ModuleName s
196 -- | Returns the string version of the module name, with dots replaced by slashes
197 moduleNameSlashes :: ModuleName -> String
198 moduleNameSlashes = dots_to_slashes . moduleNameString
199 where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
202 %************************************************************************
204 \subsection{A fully qualified module}
206 %************************************************************************
209 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
210 data Module = Module {
211 modulePackageId :: !PackageId, -- pkg-1.0
212 moduleName :: !ModuleName -- A.B.C
216 instance Uniquable Module where
217 getUnique (Module p n) = getUnique (packageIdFS p `appendFS` moduleNameFS n)
219 instance Outputable Module where
222 instance Binary Module where
223 put_ bh (Module p n) = put_ bh p >> put_ bh n
224 get bh = do p <- get bh; n <- get bh; return (Module p n)
226 -- | This gives a stable ordering, as opposed to the Ord instance which
227 -- gives an ordering based on the 'Unique's of the components, which may
228 -- not be stable from run to run of the compiler.
229 stableModuleCmp :: Module -> Module -> Ordering
230 stableModuleCmp (Module p1 n1) (Module p2 n2)
231 = (p1 `stablePackageIdCmp` p2) `thenCmp`
232 (n1 `stableModuleNameCmp` n2)
234 mkModule :: PackageId -> ModuleName -> Module
237 pprModule :: Module -> SDoc
238 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
240 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
241 pprPackagePrefix p mod = getPprStyle doc
245 if p == mainPackageId
246 then empty -- never qualify the main package in code
247 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
248 | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
249 -- the PrintUnqualified tells us which modules have to
250 -- be qualified with package names
254 %************************************************************************
256 \subsection{PackageId}
258 %************************************************************************
261 -- | Essentially just a string identifying a package, including the version: e.g. parsec-1.0
262 newtype PackageId = PId FastString deriving( Eq )
263 -- here to avoid module loops with PackageConfig
265 instance Uniquable PackageId where
266 getUnique pid = getUnique (packageIdFS pid)
268 -- Note: *not* a stable lexicographic ordering, a faster unique-based
270 instance Ord PackageId where
271 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
273 stablePackageIdCmp :: PackageId -> PackageId -> Ordering
274 -- ^ Compares package ids lexically, rather than by their 'Unique's
275 stablePackageIdCmp p1 p2 = packageIdFS p1 `compare` packageIdFS p2
277 instance Outputable PackageId where
278 ppr pid = text (packageIdString pid)
280 instance Binary PackageId where
281 put_ bh pid = put_ bh (packageIdFS pid)
282 get bh = do { fs <- get bh; return (fsToPackageId fs) }
284 fsToPackageId :: FastString -> PackageId
287 packageIdFS :: PackageId -> FastString
288 packageIdFS (PId fs) = fs
290 stringToPackageId :: String -> PackageId
291 stringToPackageId = fsToPackageId . mkFastString
293 packageIdString :: PackageId -> String
294 packageIdString = unpackFS . packageIdFS
297 -- -----------------------------------------------------------------------------
298 -- $wired_in_packages
299 -- Certain packages are known to the compiler, in that we know about certain
300 -- entities that reside in these packages, and the compiler needs to
301 -- declare static Modules and Names that refer to these packages. Hence
302 -- the wired-in packages can't include version numbers, since we don't want
303 -- to bake the version numbers of these packages into GHC.
305 -- So here's the plan. Wired-in packages are still versioned as
306 -- normal in the packages database, and you can still have multiple
307 -- versions of them installed. However, for each invocation of GHC,
308 -- only a single instance of each wired-in package will be recognised
309 -- (the desired one is selected via @-package@\/@-hide-package@), and GHC
310 -- will use the unversioned 'PackageId' below when referring to it,
311 -- including in .hi files and object file symbols. Unselected
312 -- versions of wired-in packages will be ignored, as will any other
313 -- package that depends directly or indirectly on it (much as if you
314 -- had used @-ignore-package@).
316 -- Make sure you change 'Packages.findWiredInPackages' if you add an entry here
318 integerPackageId, primPackageId,
319 basePackageId, rtsPackageId, haskell98PackageId,
320 thPackageId, ndpPackageId, dphSeqPackageId, dphParPackageId,
321 mainPackageId :: PackageId
322 primPackageId = fsToPackageId (fsLit "ghc-prim")
323 integerPackageId = fsToPackageId (fsLit "integer")
324 basePackageId = fsToPackageId (fsLit "base")
325 rtsPackageId = fsToPackageId (fsLit "rts")
326 haskell98PackageId = fsToPackageId (fsLit "haskell98")
327 thPackageId = fsToPackageId (fsLit "template-haskell")
328 ndpPackageId = fsToPackageId (fsLit "ndp")
329 dphSeqPackageId = fsToPackageId (fsLit "dph-seq")
330 dphParPackageId = fsToPackageId (fsLit "dph-par")
332 -- | This is the package Id for the current program. It is the default
333 -- package Id if you don't specify a package name. We don't add this prefix
334 -- to symbol names, since there can be only one main package per program.
335 mainPackageId = fsToPackageId (fsLit "main")
338 %************************************************************************
340 \subsection{@ModuleEnv@s}
342 %************************************************************************
345 -- | A map keyed off of 'Module's
346 type ModuleEnv elt = FiniteMap Module elt
348 emptyModuleEnv :: ModuleEnv a
349 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
350 unitModuleEnv :: Module -> a -> ModuleEnv a
351 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
352 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
353 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
354 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
355 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
357 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
358 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
359 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
360 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
361 moduleEnvKeys :: ModuleEnv a -> [Module]
362 moduleEnvElts :: ModuleEnv a -> [a]
364 isEmptyModuleEnv :: ModuleEnv a -> Bool
365 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
366 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
367 elemModuleEnv :: Module -> ModuleEnv a -> Bool
368 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
369 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
371 filterModuleEnv f = filterFM (\_ v -> f v)
372 elemModuleEnv = elemFM
373 extendModuleEnv = addToFM
374 extendModuleEnv_C = addToFM_C
375 extendModuleEnvList = addListToFM
376 extendModuleEnvList_C = addListToFM_C
377 plusModuleEnv_C = plusFM_C
378 delModuleEnvList = delListFromFM
379 delModuleEnv = delFromFM
380 plusModuleEnv = plusFM
381 lookupModuleEnv = lookupFM
382 lookupWithDefaultModuleEnv = lookupWithDefaultFM
383 mapModuleEnv f = mapFM (\_ v -> f v)
384 mkModuleEnv = listToFM
385 emptyModuleEnv = emptyFM
386 moduleEnvKeys = keysFM
387 moduleEnvElts = eltsFM
388 unitModuleEnv = unitFM
389 isEmptyModuleEnv = isEmptyFM
390 foldModuleEnv f = foldFM (\_ v -> f v)
394 -- | A set of 'Module's
395 type ModuleSet = FiniteMap Module ()
397 mkModuleSet :: [Module] -> ModuleSet
398 extendModuleSet :: ModuleSet -> Module -> ModuleSet
399 emptyModuleSet :: ModuleSet
400 moduleSetElts :: ModuleSet -> [Module]
401 elemModuleSet :: Module -> ModuleSet -> Bool
403 emptyModuleSet = emptyFM
404 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
405 extendModuleSet s m = addToFM s m ()
406 moduleSetElts = keysFM
407 elemModuleSet = elemFM
410 A ModuleName has a Unique, so we can build mappings of these using
414 -- | A map keyed off of 'ModuleName's (actually, their 'Unique's)
415 type ModuleNameEnv elt = UniqFM elt