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
42 modulePackageId, moduleName,
46 -- * The ModuleLocation type
48 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
52 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
53 extendModuleEnvList_C, plusModuleEnv_C,
54 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
55 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
56 moduleEnvKeys, moduleEnvElts, unitModuleEnv, isEmptyModuleEnv,
57 foldModuleEnv, extendModuleEnv_C, filterModuleEnv,
59 -- * ModuleName mappings
63 ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
68 import qualified Pretty
75 import System.FilePath
78 %************************************************************************
80 \subsection{Module locations}
82 %************************************************************************
87 ml_hs_file :: Maybe FilePath,
88 -- The source file, if we have one. Package modules
89 -- probably don't have source files.
91 ml_hi_file :: FilePath,
92 -- Where the .hi file is, whether or not it exists
93 -- yet. Always of form foo.hi, even if there is an
94 -- hi-boot file (we add the -boot suffix later)
96 ml_obj_file :: FilePath
97 -- Where the .o file is, whether or not it exists yet.
98 -- (might not exist either because the module hasn't
99 -- been compiled yet, or because it is part of a
100 -- package with a .a file)
103 instance Outputable ModLocation where
107 For a module in another package, the hs_file and obj_file
108 components of ModLocation are undefined.
110 The locations specified by a ModLocation may or may not
111 correspond to actual files yet: for example, even if the object
112 file doesn't exist, the ModLocation still contains the path to
113 where the object file will reside if/when it is created.
116 addBootSuffix :: FilePath -> FilePath
117 -- Add the "-boot" suffix to .hs, .hi and .o files
118 addBootSuffix path = path ++ "-boot"
120 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
121 addBootSuffix_maybe is_boot path
122 | is_boot = addBootSuffix path
125 addBootSuffixLocn :: ModLocation -> ModLocation
126 addBootSuffixLocn locn
127 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
128 , ml_hi_file = addBootSuffix (ml_hi_file locn)
129 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
133 %************************************************************************
135 \subsection{The name of a module}
137 %************************************************************************
140 -- | A ModuleName is a simple string, eg. @Data.List@.
141 newtype ModuleName = ModuleName FastString
143 instance Uniquable ModuleName where
144 getUnique (ModuleName nm) = getUnique nm
146 instance Eq ModuleName where
147 nm1 == nm2 = getUnique nm1 == getUnique nm2
149 -- Warning: gives an ordering relation based on the uniques of the
150 -- FastStrings which are the (encoded) module names. This is _not_
151 -- a lexicographical ordering.
152 instance Ord ModuleName where
153 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
155 instance Outputable ModuleName where
158 instance Binary ModuleName where
159 put_ bh (ModuleName fs) = put_ bh fs
160 get bh = do fs <- get bh; return (ModuleName fs)
162 pprModuleName :: ModuleName -> SDoc
163 pprModuleName (ModuleName nm) =
164 getPprStyle $ \ sty ->
166 then ftext (zEncodeFS nm)
169 moduleNameFS :: ModuleName -> FastString
170 moduleNameFS (ModuleName mod) = mod
172 moduleNameString :: ModuleName -> String
173 moduleNameString (ModuleName mod) = unpackFS mod
175 mkModuleName :: String -> ModuleName
176 mkModuleName s = ModuleName (mkFastString s)
178 mkModuleNameFS :: FastString -> ModuleName
179 mkModuleNameFS s = ModuleName s
181 -- Returns the string version of the module name, with dots replaced by slashes
182 moduleNameSlashes :: ModuleName -> String
183 moduleNameSlashes = dots_to_slashes . moduleNameString
184 where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
187 %************************************************************************
189 \subsection{A fully qualified module}
191 %************************************************************************
194 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
195 data Module = Module {
196 modulePackageId :: !PackageId, -- pkg-1.0
197 moduleName :: !ModuleName -- A.B.C
201 instance Outputable Module where
204 instance Binary Module where
205 put_ bh (Module p n) = put_ bh p >> put_ bh n
206 get bh = do p <- get bh; n <- get bh; return (Module p n)
208 instance Uniquable PackageId where
209 getUnique pid = getUnique (packageIdFS pid)
211 mkModule :: PackageId -> ModuleName -> Module
214 pprModule :: Module -> SDoc
215 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
217 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
218 pprPackagePrefix p mod = getPprStyle doc
222 if p == mainPackageId
223 then empty -- never qualify the main package in code
224 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
225 | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
226 -- the PrintUnqualified tells us which modules have to
227 -- be qualified with package names
231 %************************************************************************
233 \subsection{PackageId}
235 %************************************************************************
238 newtype PackageId = PId FastString deriving( Eq, Ord ) -- includes the version
239 -- here to avoid module loops with PackageConfig
241 instance Outputable PackageId where
242 ppr pid = text (packageIdString pid)
244 instance Binary PackageId where
245 put_ bh pid = put_ bh (packageIdFS pid)
246 get bh = do { fs <- get bh; return (fsToPackageId fs) }
248 fsToPackageId :: FastString -> PackageId
251 packageIdFS :: PackageId -> FastString
252 packageIdFS (PId fs) = fs
254 stringToPackageId :: String -> PackageId
255 stringToPackageId = fsToPackageId . mkFastString
257 packageIdString :: PackageId -> String
258 packageIdString = unpackFS . packageIdFS
261 -- -----------------------------------------------------------------------------
262 -- Package Ids that are wired in
264 -- Certain packages are "known" to the compiler, in that we know about certain
265 -- entities that reside in these packages, and the compiler needs to
266 -- declare static Modules and Names that refer to these packages. Hence
267 -- the wired-in packages can't include version numbers, since we don't want
268 -- to bake the version numbers of these packages into GHC.
270 -- So here's the plan. Wired-in packages are still versioned as
271 -- normal in the packages database, and you can still have multiple
272 -- versions of them installed. However, for each invocation of GHC,
273 -- only a single instance of each wired-in package will be recognised
274 -- (the desired one is selected via -package/-hide-package), and GHC
275 -- will use the unversioned PackageId below when referring to it,
276 -- including in .hi files and object file symbols. Unselected
277 -- versions of wired-in packages will be ignored, as will any other
278 -- package that depends directly or indirectly on it (much as if you
279 -- had used -ignore-package).
281 integerPackageId, primPackageId,
282 basePackageId, rtsPackageId, haskell98PackageId,
283 thPackageId, ndpPackageId, mainPackageId :: PackageId
284 primPackageId = fsToPackageId (fsLit "ghc-prim")
285 integerPackageId = fsToPackageId (fsLit "integer")
286 basePackageId = fsToPackageId (fsLit "base")
287 rtsPackageId = fsToPackageId (fsLit "rts")
288 haskell98PackageId = fsToPackageId (fsLit "haskell98")
289 thPackageId = fsToPackageId (fsLit "template-haskell")
290 ndpPackageId = fsToPackageId (fsLit "ndp")
292 -- This is the package Id for the program. It is the default package
293 -- Id if you don't specify a package name. We don't add this prefix
294 -- to symbol name, since there can be only one main package per program.
295 mainPackageId = fsToPackageId (fsLit "main")
298 %************************************************************************
300 \subsection{@ModuleEnv@s}
302 %************************************************************************
305 type ModuleEnv elt = FiniteMap Module elt
307 emptyModuleEnv :: ModuleEnv a
308 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
309 unitModuleEnv :: Module -> a -> ModuleEnv a
310 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
311 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
312 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
313 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
314 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
316 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
317 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
318 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
319 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
320 moduleEnvKeys :: ModuleEnv a -> [Module]
321 moduleEnvElts :: ModuleEnv a -> [a]
323 isEmptyModuleEnv :: ModuleEnv a -> Bool
324 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
325 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
326 elemModuleEnv :: Module -> ModuleEnv a -> Bool
327 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
328 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
330 filterModuleEnv f = filterFM (\_ v -> f v)
331 elemModuleEnv = elemFM
332 extendModuleEnv = addToFM
333 extendModuleEnv_C = addToFM_C
334 extendModuleEnvList = addListToFM
335 extendModuleEnvList_C = addListToFM_C
336 plusModuleEnv_C = plusFM_C
337 delModuleEnvList = delListFromFM
338 delModuleEnv = delFromFM
339 plusModuleEnv = plusFM
340 lookupModuleEnv = lookupFM
341 lookupWithDefaultModuleEnv = lookupWithDefaultFM
342 mapModuleEnv f = mapFM (\_ v -> f v)
343 mkModuleEnv = listToFM
344 emptyModuleEnv = emptyFM
345 moduleEnvKeys = keysFM
346 moduleEnvElts = eltsFM
347 unitModuleEnv = unitFM
348 isEmptyModuleEnv = isEmptyFM
349 foldModuleEnv f = foldFM (\_ v -> f v)
353 type ModuleSet = FiniteMap Module ()
354 mkModuleSet :: [Module] -> ModuleSet
355 extendModuleSet :: ModuleSet -> Module -> ModuleSet
356 emptyModuleSet :: ModuleSet
357 moduleSetElts :: ModuleSet -> [Module]
358 elemModuleSet :: Module -> ModuleSet -> Bool
360 emptyModuleSet = emptyFM
361 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
362 extendModuleSet s m = addToFM s m ()
363 moduleSetElts = keysFM
364 elemModuleSet = elemFM
367 A ModuleName has a Unique, so we can build mappings of these using
371 type ModuleNameEnv elt = UniqFM elt