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
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
21 -- * The ModuleName type
31 modulePackageId, moduleName,
35 -- * The ModuleLocation type
37 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
41 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
42 extendModuleEnvList_C, plusModuleEnv_C,
43 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
44 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
45 moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv,
46 extendModuleEnv_C, filterModuleEnv,
48 -- * ModuleName mappings
52 ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
56 #include "HsVersions.h"
66 %************************************************************************
68 \subsection{Module locations}
70 %************************************************************************
75 ml_hs_file :: Maybe FilePath,
76 -- The source file, if we have one. Package modules
77 -- probably don't have source files.
79 ml_hi_file :: FilePath,
80 -- Where the .hi file is, whether or not it exists
81 -- yet. Always of form foo.hi, even if there is an
82 -- hi-boot file (we add the -boot suffix later)
84 ml_obj_file :: FilePath
85 -- Where the .o file is, whether or not it exists yet.
86 -- (might not exist either because the module hasn't
87 -- been compiled yet, or because it is part of a
88 -- package with a .a file)
91 instance Outputable ModLocation where
95 For a module in another package, the hs_file and obj_file
96 components of ModLocation are undefined.
98 The locations specified by a ModLocation may or may not
99 correspond to actual files yet: for example, even if the object
100 file doesn't exist, the ModLocation still contains the path to
101 where the object file will reside if/when it is created.
104 addBootSuffix :: FilePath -> FilePath
105 -- Add the "-boot" suffix to .hs, .hi and .o files
106 addBootSuffix path = path ++ "-boot"
108 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
109 addBootSuffix_maybe is_boot path
110 | is_boot = addBootSuffix path
113 addBootSuffixLocn :: ModLocation -> ModLocation
114 addBootSuffixLocn locn
115 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
116 , ml_hi_file = addBootSuffix (ml_hi_file locn)
117 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
121 %************************************************************************
123 \subsection{The name of a module}
125 %************************************************************************
128 -- | A ModuleName is a simple string, eg. @Data.List@.
129 newtype ModuleName = ModuleName FastString
131 instance Uniquable ModuleName where
132 getUnique (ModuleName nm) = getUnique nm
134 instance Eq ModuleName where
135 nm1 == nm2 = getUnique nm1 == getUnique nm2
137 -- Warning: gives an ordering relation based on the uniques of the
138 -- FastStrings which are the (encoded) module names. This is _not_
139 -- a lexicographical ordering.
140 instance Ord ModuleName where
141 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
143 instance Outputable ModuleName where
146 instance Binary ModuleName where
147 put_ bh (ModuleName fs) = put_ bh fs
148 get bh = do fs <- get bh; return (ModuleName fs)
150 pprModuleName :: ModuleName -> SDoc
151 pprModuleName (ModuleName nm) =
152 getPprStyle $ \ sty ->
154 then ftext (zEncodeFS nm)
157 moduleNameFS :: ModuleName -> FastString
158 moduleNameFS (ModuleName mod) = mod
160 moduleNameString :: ModuleName -> String
161 moduleNameString (ModuleName mod) = unpackFS mod
163 mkModuleName :: String -> ModuleName
164 mkModuleName s = ModuleName (mkFastString s)
166 mkModuleNameFS :: FastString -> ModuleName
167 mkModuleNameFS s = ModuleName s
170 %************************************************************************
172 \subsection{A fully qualified module}
174 %************************************************************************
177 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
178 data Module = Module {
179 modulePackageId :: !PackageId, -- pkg-1.0
180 moduleName :: !ModuleName -- A.B.C
184 instance Outputable Module where
187 instance Binary Module where
188 put_ bh (Module p n) = put_ bh p >> put_ bh n
189 get bh = do p <- get bh; n <- get bh; return (Module p n)
191 mkModule :: PackageId -> ModuleName -> Module
194 pprModule :: Module -> SDoc
195 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
197 pprPackagePrefix p mod = getPprStyle doc
201 if p == mainPackageId
202 then empty -- never qualify the main package in code
203 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
204 | Just pkg <- qualModule sty mod = ftext (packageIdFS pkg) <> char ':'
205 -- the PrintUnqualified tells us which modules have to
206 -- be qualified with package names
210 %************************************************************************
212 \subsection{@ModuleEnv@s}
214 %************************************************************************
217 type ModuleEnv elt = FiniteMap Module elt
219 emptyModuleEnv :: ModuleEnv a
220 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
221 unitModuleEnv :: Module -> a -> ModuleEnv a
222 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
223 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
224 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
225 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
226 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
228 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
229 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
230 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
231 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
232 moduleEnvElts :: ModuleEnv a -> [a]
234 isEmptyModuleEnv :: ModuleEnv a -> Bool
235 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
236 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
237 elemModuleEnv :: Module -> ModuleEnv a -> Bool
238 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
239 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
241 filterModuleEnv f = filterFM (\_ v -> f v)
242 elemModuleEnv = elemFM
243 extendModuleEnv = addToFM
244 extendModuleEnv_C = addToFM_C
245 extendModuleEnvList = addListToFM
246 extendModuleEnvList_C = addListToFM_C
247 plusModuleEnv_C = plusFM_C
248 delModuleEnvList = delListFromFM
249 delModuleEnv = delFromFM
250 plusModuleEnv = plusFM
251 lookupModuleEnv = lookupFM
252 lookupWithDefaultModuleEnv = lookupWithDefaultFM
253 mapModuleEnv f = mapFM (\_ v -> f v)
254 mkModuleEnv = listToFM
255 emptyModuleEnv = emptyFM
256 moduleEnvElts = eltsFM
257 unitModuleEnv = unitFM
258 isEmptyModuleEnv = isEmptyFM
259 foldModuleEnv f = foldFM (\_ v -> f v)
263 type ModuleSet = FiniteMap Module ()
264 mkModuleSet :: [Module] -> ModuleSet
265 extendModuleSet :: ModuleSet -> Module -> ModuleSet
266 emptyModuleSet :: ModuleSet
267 moduleSetElts :: ModuleSet -> [Module]
268 elemModuleSet :: Module -> ModuleSet -> Bool
270 emptyModuleSet = emptyFM
271 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
272 extendModuleSet s m = addToFM s m ()
273 moduleSetElts = keysFM
274 elemModuleSet = elemFM
277 A ModuleName has a Unique, so we can build mappings of these using
281 type ModuleNameEnv elt = UniqFM elt