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
44 modulePackageId, moduleName,
49 -- * The ModuleLocation type
51 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
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,
62 -- * ModuleName mappings
66 ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
71 import qualified Pretty
79 import System.FilePath
82 %************************************************************************
84 \subsection{Module locations}
86 %************************************************************************
91 ml_hs_file :: Maybe FilePath,
92 -- The source file, if we have one. Package modules
93 -- probably don't have source files.
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)
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)
107 instance Outputable ModLocation where
111 For a module in another package, the hs_file and obj_file
112 components of ModLocation are undefined.
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.
120 addBootSuffix :: FilePath -> FilePath
121 -- Add the "-boot" suffix to .hs, .hi and .o files
122 addBootSuffix path = path ++ "-boot"
124 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
125 addBootSuffix_maybe is_boot path
126 | is_boot = addBootSuffix path
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) }
137 %************************************************************************
139 \subsection{The name of a module}
141 %************************************************************************
144 -- | A ModuleName is a simple string, eg. @Data.List@.
145 newtype ModuleName = ModuleName FastString
147 instance Uniquable ModuleName where
148 getUnique (ModuleName nm) = getUnique nm
150 instance Eq ModuleName where
151 nm1 == nm2 = getUnique nm1 == getUnique nm2
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
159 instance Outputable ModuleName where
162 instance Binary ModuleName where
163 put_ bh (ModuleName fs) = put_ bh fs
164 get bh = do fs <- get bh; return (ModuleName fs)
166 stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
167 -- Compare lexically, not by unique
168 stableModuleNameCmp n1 n2 = moduleNameFS n1 `compare` moduleNameFS n2
170 pprModuleName :: ModuleName -> SDoc
171 pprModuleName (ModuleName nm) =
172 getPprStyle $ \ sty ->
174 then ftext (zEncodeFS nm)
177 moduleNameFS :: ModuleName -> FastString
178 moduleNameFS (ModuleName mod) = mod
180 moduleNameString :: ModuleName -> String
181 moduleNameString (ModuleName mod) = unpackFS mod
183 mkModuleName :: String -> ModuleName
184 mkModuleName s = ModuleName (mkFastString s)
186 mkModuleNameFS :: FastString -> ModuleName
187 mkModuleNameFS s = ModuleName s
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)
195 %************************************************************************
197 \subsection{A fully qualified module}
199 %************************************************************************
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
209 instance Outputable Module where
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)
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)
224 mkModule :: PackageId -> ModuleName -> Module
227 pprModule :: Module -> SDoc
228 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
230 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
231 pprPackagePrefix p mod = getPprStyle doc
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
244 %************************************************************************
246 \subsection{PackageId}
248 %************************************************************************
251 newtype PackageId = PId FastString deriving( Eq ) -- includes the version
252 -- here to avoid module loops with PackageConfig
254 instance Uniquable PackageId where
255 getUnique pid = getUnique (packageIdFS pid)
257 -- Note: *not* a stable lexicographic ordering, a faster unique-based
259 instance Ord PackageId where
260 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
262 stablePackageIdCmp :: PackageId -> PackageId -> Ordering
263 stablePackageIdCmp p1 p2 = packageIdFS p1 `compare` packageIdFS p2
265 instance Outputable PackageId where
266 ppr pid = text (packageIdString pid)
268 instance Binary PackageId where
269 put_ bh pid = put_ bh (packageIdFS pid)
270 get bh = do { fs <- get bh; return (fsToPackageId fs) }
272 fsToPackageId :: FastString -> PackageId
275 packageIdFS :: PackageId -> FastString
276 packageIdFS (PId fs) = fs
278 stringToPackageId :: String -> PackageId
279 stringToPackageId = fsToPackageId . mkFastString
281 packageIdString :: PackageId -> String
282 packageIdString = unpackFS . packageIdFS
285 -- -----------------------------------------------------------------------------
286 -- Package Ids that are wired in
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.
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).
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")
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")
322 %************************************************************************
324 \subsection{@ModuleEnv@s}
326 %************************************************************************
329 type ModuleEnv elt = FiniteMap Module elt
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
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]
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
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)
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
384 emptyModuleSet = emptyFM
385 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
386 extendModuleSet s m = addToFM s m ()
387 moduleSetElts = keysFM
388 elemModuleSet = elemFM
391 A ModuleName has a Unique, so we can build mappings of these using
395 type ModuleNameEnv elt = UniqFM elt