2 % (c) The University of Glasgow, 2004
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 modulePackageId, moduleName,
28 -- * The ModuleLocation type
30 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
34 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
35 extendModuleEnvList_C, plusModuleEnv_C,
36 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
37 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
38 moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv,
39 extendModuleEnv_C, filterModuleEnv,
41 -- * ModuleName mappings
45 ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
49 #include "HsVersions.h"
51 import Unique ( Uniquable(..) )
54 import PackageConfig ( PackageId, packageIdFS, mainPackageId )
59 %************************************************************************
61 \subsection{Module locations}
63 %************************************************************************
68 ml_hs_file :: Maybe FilePath,
69 -- The source file, if we have one. Package modules
70 -- probably don't have source files.
72 ml_hi_file :: FilePath,
73 -- Where the .hi file is, whether or not it exists
74 -- yet. Always of form foo.hi, even if there is an
75 -- hi-boot file (we add the -boot suffix later)
77 ml_obj_file :: FilePath
78 -- Where the .o file is, whether or not it exists yet.
79 -- (might not exist either because the module hasn't
80 -- been compiled yet, or because it is part of a
81 -- package with a .a file)
84 instance Outputable ModLocation where
88 For a module in another package, the hs_file and obj_file
89 components of ModLocation are undefined.
91 The locations specified by a ModLocation may or may not
92 correspond to actual files yet: for example, even if the object
93 file doesn't exist, the ModLocation still contains the path to
94 where the object file will reside if/when it is created.
97 addBootSuffix :: FilePath -> FilePath
98 -- Add the "-boot" suffix to .hs, .hi and .o files
99 addBootSuffix path = path ++ "-boot"
101 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
102 addBootSuffix_maybe is_boot path
103 | is_boot = addBootSuffix path
106 addBootSuffixLocn :: ModLocation -> ModLocation
107 addBootSuffixLocn locn
108 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
109 , ml_hi_file = addBootSuffix (ml_hi_file locn)
110 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
114 %************************************************************************
116 \subsection{The name of a module}
118 %************************************************************************
121 -- | A ModuleName is a simple string, eg. @Data.List@.
122 newtype ModuleName = ModuleName FastString
124 instance Uniquable ModuleName where
125 getUnique (ModuleName nm) = getUnique nm
127 instance Eq ModuleName where
128 nm1 == nm2 = getUnique nm1 == getUnique nm2
130 -- Warning: gives an ordering relation based on the uniques of the
131 -- FastStrings which are the (encoded) module names. This is _not_
132 -- a lexicographical ordering.
133 instance Ord ModuleName where
134 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
136 instance Outputable ModuleName where
139 instance Binary ModuleName where
140 put_ bh (ModuleName fs) = put_ bh fs
141 get bh = do fs <- get bh; return (ModuleName fs)
143 pprModuleName :: ModuleName -> SDoc
144 pprModuleName (ModuleName nm) =
145 getPprStyle $ \ sty ->
147 then ftext (zEncodeFS nm)
150 moduleNameFS :: ModuleName -> FastString
151 moduleNameFS (ModuleName mod) = mod
153 moduleNameString :: ModuleName -> String
154 moduleNameString (ModuleName mod) = unpackFS mod
156 mkModuleName :: String -> ModuleName
157 mkModuleName s = ModuleName (mkFastString s)
159 mkModuleNameFS :: FastString -> ModuleName
160 mkModuleNameFS s = ModuleName s
163 %************************************************************************
165 \subsection{A fully qualified module}
167 %************************************************************************
170 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
171 data Module = Module {
172 modulePackageId :: !PackageId, -- pkg-1.0
173 moduleName :: !ModuleName -- A.B.C
177 instance Outputable Module where
180 instance Binary Module where
181 put_ bh (Module p n) = put_ bh p >> put_ bh n
182 get bh = do p <- get bh; n <- get bh; return (Module p n)
184 mkModule :: PackageId -> ModuleName -> Module
187 pprModule :: Module -> SDoc
188 pprModule mod@(Module p n) = pprPackagePrefix p mod <> pprModuleName n
190 pprPackagePrefix p mod = getPprStyle doc
194 if p == mainPackageId
195 then empty -- never qualify the main package in code
196 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
197 | Just pkg <- qualModule sty mod = ftext (packageIdFS pkg) <> char ':'
198 -- the PrintUnqualified tells us which modules have to
199 -- be qualified with package names
203 %************************************************************************
205 \subsection{@ModuleEnv@s}
207 %************************************************************************
210 type ModuleEnv elt = FiniteMap Module elt
212 emptyModuleEnv :: ModuleEnv a
213 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
214 unitModuleEnv :: Module -> a -> ModuleEnv a
215 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
216 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
217 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
218 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
219 extendModuleEnvList_C :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
221 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
222 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
223 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
224 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
225 moduleEnvElts :: ModuleEnv a -> [a]
227 isEmptyModuleEnv :: ModuleEnv a -> Bool
228 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
229 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
230 elemModuleEnv :: Module -> ModuleEnv a -> Bool
231 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
232 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
234 filterModuleEnv f = filterFM (\_ v -> f v)
235 elemModuleEnv = elemFM
236 extendModuleEnv = addToFM
237 extendModuleEnv_C = addToFM_C
238 extendModuleEnvList = addListToFM
239 extendModuleEnvList_C = addListToFM_C
240 plusModuleEnv_C = plusFM_C
241 delModuleEnvList = delListFromFM
242 delModuleEnv = delFromFM
243 plusModuleEnv = plusFM
244 lookupModuleEnv = lookupFM
245 lookupWithDefaultModuleEnv = lookupWithDefaultFM
246 mapModuleEnv f = mapFM (\_ v -> f v)
247 mkModuleEnv = listToFM
248 emptyModuleEnv = emptyFM
249 moduleEnvElts = eltsFM
250 unitModuleEnv = unitFM
251 isEmptyModuleEnv = isEmptyFM
252 foldModuleEnv f = foldFM (\_ v -> f v)
256 type ModuleSet = FiniteMap Module ()
257 mkModuleSet :: [Module] -> ModuleSet
258 extendModuleSet :: ModuleSet -> Module -> ModuleSet
259 emptyModuleSet :: ModuleSet
260 moduleSetElts :: ModuleSet -> [Module]
261 elemModuleSet :: Module -> ModuleSet -> Bool
263 emptyModuleSet = emptyFM
264 mkModuleSet ms = listToFM [(m,()) | m <- ms ]
265 extendModuleSet s m = addToFM s m ()
266 moduleSetElts = keysFM
267 elemModuleSet = elemFM
270 A ModuleName has a Unique, so we can build mappings of these using
274 type ModuleNameEnv elt = UniqFM elt