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
46 modulePackageId, moduleName,
51 -- * The ModuleLocation type
53 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
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,
64 -- * ModuleName mappings
68 ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
73 import qualified Pretty
81 import System.FilePath
84 %************************************************************************
86 \subsection{Module locations}
88 %************************************************************************
93 ml_hs_file :: Maybe FilePath,
94 -- The source file, if we have one. Package modules
95 -- probably don't have source files.
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)
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)
109 instance Outputable ModLocation where
113 For a module in another package, the hs_file and obj_file
114 components of ModLocation are undefined.
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.
122 addBootSuffix :: FilePath -> FilePath
123 -- Add the "-boot" suffix to .hs, .hi and .o files
124 addBootSuffix path = path ++ "-boot"
126 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
127 addBootSuffix_maybe is_boot path
128 | is_boot = addBootSuffix path
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) }
139 %************************************************************************
141 \subsection{The name of a module}
143 %************************************************************************
146 -- | A ModuleName is a simple string, eg. @Data.List@.
147 newtype ModuleName = ModuleName FastString
149 instance Uniquable ModuleName where
150 getUnique (ModuleName nm) = getUnique nm
152 instance Eq ModuleName where
153 nm1 == nm2 = getUnique nm1 == getUnique nm2
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
161 instance Outputable ModuleName where
164 instance Binary ModuleName where
165 put_ bh (ModuleName fs) = put_ bh fs
166 get bh = do fs <- get bh; return (ModuleName fs)
168 stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
169 -- Compare lexically, not by unique
170 stableModuleNameCmp n1 n2 = moduleNameFS n1 `compare` moduleNameFS n2
172 pprModuleName :: ModuleName -> SDoc
173 pprModuleName (ModuleName nm) =
174 getPprStyle $ \ sty ->
176 then ftext (zEncodeFS nm)
179 moduleNameFS :: ModuleName -> FastString
180 moduleNameFS (ModuleName mod) = mod
182 moduleNameString :: ModuleName -> String
183 moduleNameString (ModuleName mod) = unpackFS mod
185 mkModuleName :: String -> ModuleName
186 mkModuleName s = ModuleName (mkFastString s)
188 mkModuleNameFS :: FastString -> ModuleName
189 mkModuleNameFS s = ModuleName s
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)
197 %************************************************************************
199 \subsection{A fully qualified module}
201 %************************************************************************
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
211 instance Outputable Module where
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)
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)
226 mkModule :: PackageId -> ModuleName -> Module
229 pprModule :: Module -> SDoc
230 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
232 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
233 pprPackagePrefix p mod = getPprStyle doc
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
246 %************************************************************************
248 \subsection{PackageId}
250 %************************************************************************
253 newtype PackageId = PId FastString deriving( Eq ) -- includes the version
254 -- here to avoid module loops with PackageConfig
256 instance Uniquable PackageId where
257 getUnique pid = getUnique (packageIdFS pid)
259 -- Note: *not* a stable lexicographic ordering, a faster unique-based
261 instance Ord PackageId where
262 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
264 stablePackageIdCmp :: PackageId -> PackageId -> Ordering
265 stablePackageIdCmp p1 p2 = packageIdFS p1 `compare` packageIdFS p2
267 instance Outputable PackageId where
268 ppr pid = text (packageIdString pid)
270 instance Binary PackageId where
271 put_ bh pid = put_ bh (packageIdFS pid)
272 get bh = do { fs <- get bh; return (fsToPackageId fs) }
274 fsToPackageId :: FastString -> PackageId
277 packageIdFS :: PackageId -> FastString
278 packageIdFS (PId fs) = fs
280 stringToPackageId :: String -> PackageId
281 stringToPackageId = fsToPackageId . mkFastString
283 packageIdString :: PackageId -> String
284 packageIdString = unpackFS . packageIdFS
287 -- -----------------------------------------------------------------------------
288 -- Package Ids that are wired in
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.
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).
307 integerPackageId, primPackageId,
308 basePackageId, rtsPackageId, haskell98PackageId,
309 thPackageId, ndpPackageId, dphSeqPackageId, dphParPackageId,
310 mainPackageId :: PackageId
311 primPackageId = fsToPackageId (fsLit "ghc-prim")
312 integerPackageId = fsToPackageId (fsLit "integer")
313 basePackageId = fsToPackageId (fsLit "base")
314 rtsPackageId = fsToPackageId (fsLit "rts")
315 haskell98PackageId = fsToPackageId (fsLit "haskell98")
316 thPackageId = fsToPackageId (fsLit "template-haskell")
317 ndpPackageId = fsToPackageId (fsLit "ndp")
318 dphSeqPackageId = fsToPackageId (fsLit "dph-seq")
319 dphParPackageId = fsToPackageId (fsLit "dph-par")
321 -- This is the package Id for the program. It is the default package
322 -- Id if you don't specify a package name. We don't add this prefix
323 -- to symbol name, since there can be only one main package per program.
324 mainPackageId = fsToPackageId (fsLit "main")
327 %************************************************************************
329 \subsection{@ModuleEnv@s}
331 %************************************************************************
334 type ModuleEnv elt = FiniteMap Module elt
336 emptyModuleEnv :: ModuleEnv a
337 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
338 unitModuleEnv :: Module -> a -> ModuleEnv a
339 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
340 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
341 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
342 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
343 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
345 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
346 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
347 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
348 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
349 moduleEnvKeys :: ModuleEnv a -> [Module]
350 moduleEnvElts :: ModuleEnv a -> [a]
352 isEmptyModuleEnv :: ModuleEnv a -> Bool
353 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
354 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
355 elemModuleEnv :: Module -> ModuleEnv a -> Bool
356 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
357 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
359 filterModuleEnv f = filterFM (\_ v -> f v)
360 elemModuleEnv = elemFM
361 extendModuleEnv = addToFM
362 extendModuleEnv_C = addToFM_C
363 extendModuleEnvList = addListToFM
364 extendModuleEnvList_C = addListToFM_C
365 plusModuleEnv_C = plusFM_C
366 delModuleEnvList = delListFromFM
367 delModuleEnv = delFromFM
368 plusModuleEnv = plusFM
369 lookupModuleEnv = lookupFM
370 lookupWithDefaultModuleEnv = lookupWithDefaultFM
371 mapModuleEnv f = mapFM (\_ v -> f v)
372 mkModuleEnv = listToFM
373 emptyModuleEnv = emptyFM
374 moduleEnvKeys = keysFM
375 moduleEnvElts = eltsFM
376 unitModuleEnv = unitFM
377 isEmptyModuleEnv = isEmptyFM
378 foldModuleEnv f = foldFM (\_ v -> f v)
382 type ModuleSet = FiniteMap Module ()
383 mkModuleSet :: [Module] -> ModuleSet
384 extendModuleSet :: ModuleSet -> Module -> ModuleSet
385 emptyModuleSet :: ModuleSet
386 moduleSetElts :: ModuleSet -> [Module]
387 elemModuleSet :: Module -> ModuleSet -> Bool
389 emptyModuleSet = emptyFM
390 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
391 extendModuleSet s m = addToFM s m ()
392 moduleSetElts = keysFM
393 elemModuleSet = elemFM
396 A ModuleName has a Unique, so we can build mappings of these using
400 type ModuleNameEnv elt = UniqFM elt