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
23 -- * The PackageId type
30 -- * Wired-in PackageIds
40 modulePackageId, moduleName,
44 -- * The ModuleLocation type
46 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
50 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
51 extendModuleEnvList_C, plusModuleEnv_C,
52 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
53 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
54 moduleEnvKeys, moduleEnvElts, unitModuleEnv, isEmptyModuleEnv,
55 foldModuleEnv, extendModuleEnv_C, filterModuleEnv,
57 -- * ModuleName mappings
61 ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
65 #include "HsVersions.h"
67 import qualified Pretty
75 %************************************************************************
77 \subsection{Module locations}
79 %************************************************************************
84 ml_hs_file :: Maybe FilePath,
85 -- The source file, if we have one. Package modules
86 -- probably don't have source files.
88 ml_hi_file :: FilePath,
89 -- Where the .hi file is, whether or not it exists
90 -- yet. Always of form foo.hi, even if there is an
91 -- hi-boot file (we add the -boot suffix later)
93 ml_obj_file :: FilePath
94 -- Where the .o file is, whether or not it exists yet.
95 -- (might not exist either because the module hasn't
96 -- been compiled yet, or because it is part of a
97 -- package with a .a file)
100 instance Outputable ModLocation where
104 For a module in another package, the hs_file and obj_file
105 components of ModLocation are undefined.
107 The locations specified by a ModLocation may or may not
108 correspond to actual files yet: for example, even if the object
109 file doesn't exist, the ModLocation still contains the path to
110 where the object file will reside if/when it is created.
113 addBootSuffix :: FilePath -> FilePath
114 -- Add the "-boot" suffix to .hs, .hi and .o files
115 addBootSuffix path = path ++ "-boot"
117 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
118 addBootSuffix_maybe is_boot path
119 | is_boot = addBootSuffix path
122 addBootSuffixLocn :: ModLocation -> ModLocation
123 addBootSuffixLocn locn
124 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
125 , ml_hi_file = addBootSuffix (ml_hi_file locn)
126 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
130 %************************************************************************
132 \subsection{The name of a module}
134 %************************************************************************
137 -- | A ModuleName is a simple string, eg. @Data.List@.
138 newtype ModuleName = ModuleName FastString
140 instance Uniquable ModuleName where
141 getUnique (ModuleName nm) = getUnique nm
143 instance Eq ModuleName where
144 nm1 == nm2 = getUnique nm1 == getUnique nm2
146 -- Warning: gives an ordering relation based on the uniques of the
147 -- FastStrings which are the (encoded) module names. This is _not_
148 -- a lexicographical ordering.
149 instance Ord ModuleName where
150 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
152 instance Outputable ModuleName where
155 instance Binary ModuleName where
156 put_ bh (ModuleName fs) = put_ bh fs
157 get bh = do fs <- get bh; return (ModuleName fs)
159 pprModuleName :: ModuleName -> SDoc
160 pprModuleName (ModuleName nm) =
161 getPprStyle $ \ sty ->
163 then ftext (zEncodeFS nm)
166 moduleNameFS :: ModuleName -> FastString
167 moduleNameFS (ModuleName mod) = mod
169 moduleNameString :: ModuleName -> String
170 moduleNameString (ModuleName mod) = unpackFS mod
172 mkModuleName :: String -> ModuleName
173 mkModuleName s = ModuleName (mkFastString s)
175 mkModuleNameFS :: FastString -> ModuleName
176 mkModuleNameFS s = ModuleName s
178 -- Returns the string version of the module name, with dots replaced by slashes
179 moduleNameSlashes :: ModuleName -> String
180 moduleNameSlashes = dots_to_slashes . moduleNameString
181 where dots_to_slashes = map (\c -> if c == '.' then '/' else c)
184 %************************************************************************
186 \subsection{A fully qualified module}
188 %************************************************************************
191 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
192 data Module = Module {
193 modulePackageId :: !PackageId, -- pkg-1.0
194 moduleName :: !ModuleName -- A.B.C
198 instance Outputable Module where
201 instance Binary Module where
202 put_ bh (Module p n) = put_ bh p >> put_ bh n
203 get bh = do p <- get bh; n <- get bh; return (Module p n)
205 instance Uniquable PackageId where
206 getUnique pid = getUnique (packageIdFS pid)
208 mkModule :: PackageId -> ModuleName -> Module
211 pprModule :: Module -> SDoc
212 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
214 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
215 pprPackagePrefix p mod = getPprStyle doc
219 if p == mainPackageId
220 then empty -- never qualify the main package in code
221 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
222 | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
223 -- the PrintUnqualified tells us which modules have to
224 -- be qualified with package names
228 %************************************************************************
230 \subsection{PackageId}
232 %************************************************************************
235 newtype PackageId = PId FastString deriving( Eq, Ord ) -- includes the version
236 -- here to avoid module loops with PackageConfig
238 instance Outputable PackageId where
239 ppr pid = text (packageIdString pid)
241 instance Binary PackageId where
242 put_ bh pid = put_ bh (packageIdFS pid)
243 get bh = do { fs <- get bh; return (fsToPackageId fs) }
245 fsToPackageId :: FastString -> PackageId
248 packageIdFS :: PackageId -> FastString
249 packageIdFS (PId fs) = fs
251 stringToPackageId :: String -> PackageId
252 stringToPackageId = fsToPackageId . mkFastString
254 packageIdString :: PackageId -> String
255 packageIdString = unpackFS . packageIdFS
258 -- -----------------------------------------------------------------------------
259 -- Package Ids that are wired in
261 -- Certain packages are "known" to the compiler, in that we know about certain
262 -- entities that reside in these packages, and the compiler needs to
263 -- declare static Modules and Names that refer to these packages. Hence
264 -- the wired-in packages can't include version numbers, since we don't want
265 -- to bake the version numbers of these packages into GHC.
267 -- So here's the plan. Wired-in packages are still versioned as
268 -- normal in the packages database, and you can still have multiple
269 -- versions of them installed. However, for each invocation of GHC,
270 -- only a single instance of each wired-in package will be recognised
271 -- (the desired one is selected via -package/-hide-package), and GHC
272 -- will use the unversioned PackageId below when referring to it,
273 -- including in .hi files and object file symbols. Unselected
274 -- versions of wired-in packages will be ignored, as will any other
275 -- package that depends directly or indirectly on it (much as if you
276 -- had used -ignore-package).
278 basePackageId, rtsPackageId, haskell98PackageId,
279 thPackageId, ndpPackageId, mainPackageId :: PackageId
280 basePackageId = fsToPackageId FSLIT("base")
281 rtsPackageId = fsToPackageId FSLIT("rts")
282 haskell98PackageId = fsToPackageId FSLIT("haskell98")
283 thPackageId = fsToPackageId FSLIT("template-haskell")
284 ndpPackageId = fsToPackageId FSLIT("ndp")
286 -- This is the package Id for the program. It is the default package
287 -- Id if you don't specify a package name. We don't add this prefix
288 -- to symbol name, since there can be only one main package per program.
289 mainPackageId = fsToPackageId FSLIT("main")
292 %************************************************************************
294 \subsection{@ModuleEnv@s}
296 %************************************************************************
299 type ModuleEnv elt = FiniteMap Module elt
301 emptyModuleEnv :: ModuleEnv a
302 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
303 unitModuleEnv :: Module -> a -> ModuleEnv a
304 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
305 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
306 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
307 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
308 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
310 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
311 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
312 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
313 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
314 moduleEnvKeys :: ModuleEnv a -> [Module]
315 moduleEnvElts :: ModuleEnv a -> [a]
317 isEmptyModuleEnv :: ModuleEnv a -> Bool
318 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
319 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
320 elemModuleEnv :: Module -> ModuleEnv a -> Bool
321 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
322 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
324 filterModuleEnv f = filterFM (\_ v -> f v)
325 elemModuleEnv = elemFM
326 extendModuleEnv = addToFM
327 extendModuleEnv_C = addToFM_C
328 extendModuleEnvList = addListToFM
329 extendModuleEnvList_C = addListToFM_C
330 plusModuleEnv_C = plusFM_C
331 delModuleEnvList = delListFromFM
332 delModuleEnv = delFromFM
333 plusModuleEnv = plusFM
334 lookupModuleEnv = lookupFM
335 lookupWithDefaultModuleEnv = lookupWithDefaultFM
336 mapModuleEnv f = mapFM (\_ v -> f v)
337 mkModuleEnv = listToFM
338 emptyModuleEnv = emptyFM
339 moduleEnvKeys = keysFM
340 moduleEnvElts = eltsFM
341 unitModuleEnv = unitFM
342 isEmptyModuleEnv = isEmptyFM
343 foldModuleEnv f = foldFM (\_ v -> f v)
347 type ModuleSet = FiniteMap Module ()
348 mkModuleSet :: [Module] -> ModuleSet
349 extendModuleSet :: ModuleSet -> Module -> ModuleSet
350 emptyModuleSet :: ModuleSet
351 moduleSetElts :: ModuleSet -> [Module]
352 elemModuleSet :: Module -> ModuleSet -> Bool
354 emptyModuleSet = emptyFM
355 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
356 extendModuleSet s m = addToFM s m ()
357 moduleSetElts = keysFM
358 elemModuleSet = elemFM
361 A ModuleName has a Unique, so we can build mappings of these using
365 type ModuleNameEnv elt = UniqFM elt