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
74 import System.FilePath
77 %************************************************************************
79 \subsection{Module locations}
81 %************************************************************************
86 ml_hs_file :: Maybe FilePath,
87 -- The source file, if we have one. Package modules
88 -- probably don't have source files.
90 ml_hi_file :: FilePath,
91 -- Where the .hi file is, whether or not it exists
92 -- yet. Always of form foo.hi, even if there is an
93 -- hi-boot file (we add the -boot suffix later)
95 ml_obj_file :: FilePath
96 -- Where the .o file is, whether or not it exists yet.
97 -- (might not exist either because the module hasn't
98 -- been compiled yet, or because it is part of a
99 -- package with a .a file)
102 instance Outputable ModLocation where
106 For a module in another package, the hs_file and obj_file
107 components of ModLocation are undefined.
109 The locations specified by a ModLocation may or may not
110 correspond to actual files yet: for example, even if the object
111 file doesn't exist, the ModLocation still contains the path to
112 where the object file will reside if/when it is created.
115 addBootSuffix :: FilePath -> FilePath
116 -- Add the "-boot" suffix to .hs, .hi and .o files
117 addBootSuffix path = path ++ "-boot"
119 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
120 addBootSuffix_maybe is_boot path
121 | is_boot = addBootSuffix path
124 addBootSuffixLocn :: ModLocation -> ModLocation
125 addBootSuffixLocn locn
126 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
127 , ml_hi_file = addBootSuffix (ml_hi_file locn)
128 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
132 %************************************************************************
134 \subsection{The name of a module}
136 %************************************************************************
139 -- | A ModuleName is a simple string, eg. @Data.List@.
140 newtype ModuleName = ModuleName FastString
142 instance Uniquable ModuleName where
143 getUnique (ModuleName nm) = getUnique nm
145 instance Eq ModuleName where
146 nm1 == nm2 = getUnique nm1 == getUnique nm2
148 -- Warning: gives an ordering relation based on the uniques of the
149 -- FastStrings which are the (encoded) module names. This is _not_
150 -- a lexicographical ordering.
151 instance Ord ModuleName where
152 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
154 instance Outputable ModuleName where
157 instance Binary ModuleName where
158 put_ bh (ModuleName fs) = put_ bh fs
159 get bh = do fs <- get bh; return (ModuleName fs)
161 pprModuleName :: ModuleName -> SDoc
162 pprModuleName (ModuleName nm) =
163 getPprStyle $ \ sty ->
165 then ftext (zEncodeFS nm)
168 moduleNameFS :: ModuleName -> FastString
169 moduleNameFS (ModuleName mod) = mod
171 moduleNameString :: ModuleName -> String
172 moduleNameString (ModuleName mod) = unpackFS mod
174 mkModuleName :: String -> ModuleName
175 mkModuleName s = ModuleName (mkFastString s)
177 mkModuleNameFS :: FastString -> ModuleName
178 mkModuleNameFS s = ModuleName s
180 -- Returns the string version of the module name, with dots replaced by slashes
181 moduleNameSlashes :: ModuleName -> String
182 moduleNameSlashes = dots_to_slashes . moduleNameString
183 where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
186 %************************************************************************
188 \subsection{A fully qualified module}
190 %************************************************************************
193 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
194 data Module = Module {
195 modulePackageId :: !PackageId, -- pkg-1.0
196 moduleName :: !ModuleName -- A.B.C
200 instance Outputable Module where
203 instance Binary Module where
204 put_ bh (Module p n) = put_ bh p >> put_ bh n
205 get bh = do p <- get bh; n <- get bh; return (Module p n)
207 instance Uniquable PackageId where
208 getUnique pid = getUnique (packageIdFS pid)
210 mkModule :: PackageId -> ModuleName -> Module
213 pprModule :: Module -> SDoc
214 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
216 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
217 pprPackagePrefix p mod = getPprStyle doc
221 if p == mainPackageId
222 then empty -- never qualify the main package in code
223 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
224 | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
225 -- the PrintUnqualified tells us which modules have to
226 -- be qualified with package names
230 %************************************************************************
232 \subsection{PackageId}
234 %************************************************************************
237 newtype PackageId = PId FastString deriving( Eq, Ord ) -- includes the version
238 -- here to avoid module loops with PackageConfig
240 instance Outputable PackageId where
241 ppr pid = text (packageIdString pid)
243 instance Binary PackageId where
244 put_ bh pid = put_ bh (packageIdFS pid)
245 get bh = do { fs <- get bh; return (fsToPackageId fs) }
247 fsToPackageId :: FastString -> PackageId
250 packageIdFS :: PackageId -> FastString
251 packageIdFS (PId fs) = fs
253 stringToPackageId :: String -> PackageId
254 stringToPackageId = fsToPackageId . mkFastString
256 packageIdString :: PackageId -> String
257 packageIdString = unpackFS . packageIdFS
260 -- -----------------------------------------------------------------------------
261 -- Package Ids that are wired in
263 -- Certain packages are "known" to the compiler, in that we know about certain
264 -- entities that reside in these packages, and the compiler needs to
265 -- declare static Modules and Names that refer to these packages. Hence
266 -- the wired-in packages can't include version numbers, since we don't want
267 -- to bake the version numbers of these packages into GHC.
269 -- So here's the plan. Wired-in packages are still versioned as
270 -- normal in the packages database, and you can still have multiple
271 -- versions of them installed. However, for each invocation of GHC,
272 -- only a single instance of each wired-in package will be recognised
273 -- (the desired one is selected via -package/-hide-package), and GHC
274 -- will use the unversioned PackageId below when referring to it,
275 -- including in .hi files and object file symbols. Unselected
276 -- versions of wired-in packages will be ignored, as will any other
277 -- package that depends directly or indirectly on it (much as if you
278 -- had used -ignore-package).
280 basePackageId, rtsPackageId, haskell98PackageId,
281 thPackageId, ndpPackageId, mainPackageId :: PackageId
282 basePackageId = fsToPackageId FSLIT("base")
283 rtsPackageId = fsToPackageId FSLIT("rts")
284 haskell98PackageId = fsToPackageId FSLIT("haskell98")
285 thPackageId = fsToPackageId FSLIT("template-haskell")
286 ndpPackageId = fsToPackageId FSLIT("ndp")
288 -- This is the package Id for the program. It is the default package
289 -- Id if you don't specify a package name. We don't add this prefix
290 -- to symbol name, since there can be only one main package per program.
291 mainPackageId = fsToPackageId FSLIT("main")
294 %************************************************************************
296 \subsection{@ModuleEnv@s}
298 %************************************************************************
301 type ModuleEnv elt = FiniteMap Module elt
303 emptyModuleEnv :: ModuleEnv a
304 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
305 unitModuleEnv :: Module -> a -> ModuleEnv a
306 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
307 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
308 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
309 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
310 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
312 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
313 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
314 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
315 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
316 moduleEnvKeys :: ModuleEnv a -> [Module]
317 moduleEnvElts :: ModuleEnv a -> [a]
319 isEmptyModuleEnv :: ModuleEnv a -> Bool
320 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
321 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
322 elemModuleEnv :: Module -> ModuleEnv a -> Bool
323 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
324 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
326 filterModuleEnv f = filterFM (\_ v -> f v)
327 elemModuleEnv = elemFM
328 extendModuleEnv = addToFM
329 extendModuleEnv_C = addToFM_C
330 extendModuleEnvList = addListToFM
331 extendModuleEnvList_C = addListToFM_C
332 plusModuleEnv_C = plusFM_C
333 delModuleEnvList = delListFromFM
334 delModuleEnv = delFromFM
335 plusModuleEnv = plusFM
336 lookupModuleEnv = lookupFM
337 lookupWithDefaultModuleEnv = lookupWithDefaultFM
338 mapModuleEnv f = mapFM (\_ v -> f v)
339 mkModuleEnv = listToFM
340 emptyModuleEnv = emptyFM
341 moduleEnvKeys = keysFM
342 moduleEnvElts = eltsFM
343 unitModuleEnv = unitFM
344 isEmptyModuleEnv = isEmptyFM
345 foldModuleEnv f = foldFM (\_ v -> f v)
349 type ModuleSet = FiniteMap Module ()
350 mkModuleSet :: [Module] -> ModuleSet
351 extendModuleSet :: ModuleSet -> Module -> ModuleSet
352 emptyModuleSet :: ModuleSet
353 moduleSetElts :: ModuleSet -> [Module]
354 elemModuleSet :: Module -> ModuleSet -> Bool
356 emptyModuleSet = emptyFM
357 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
358 extendModuleSet s m = addToFM s m ()
359 moduleSetElts = keysFM
360 elemModuleSet = elemFM
363 A ModuleName has a Unique, so we can build mappings of these using
367 type ModuleNameEnv elt = UniqFM elt