2 % (c) The University of Glasgow, 2004
7 Simply the name of a module, represented as a Z-encoded FastString.
8 These are Uniquable, hence we can build FiniteMaps with ModuleNames as
14 Module, -- Abstract, instance of Eq, Ord, Outputable
15 , pprModule -- :: ModuleName -> SDoc
18 , addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
20 , moduleString -- :: ModuleName -> EncodedString
21 , moduleUserString -- :: ModuleName -> UserString
22 , moduleFS -- :: ModuleName -> EncodedFS
24 , mkModule -- :: UserString -> ModuleName
25 , mkModuleFS -- :: UserFS -> ModuleName
26 , mkSysModuleFS -- :: EncodedFS -> ModuleName
29 , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
30 , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
31 , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
32 , moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
33 , extendModuleEnv_C, filterModuleEnv,
35 , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
39 #include "HsVersions.h"
42 import Unique ( Uniquable(..) )
49 %************************************************************************
51 \subsection{Module locations}
53 %************************************************************************
58 ml_hs_file :: Maybe FilePath,
59 -- The source file, if we have one. Package modules
60 -- probably don't have source files.
62 ml_hi_file :: FilePath,
63 -- Where the .hi file is, whether or not it exists
64 -- yet. Always of form foo.hi, even if there is an
65 -- hi-boot file (we add the -boot suffix later)
67 ml_obj_file :: FilePath
68 -- Where the .o file is, whether or not it exists yet.
69 -- (might not exist either because the module hasn't
70 -- been compiled yet, or because it is part of a
71 -- package with a .a file)
74 instance Outputable ModLocation where
78 For a module in another package, the hs_file and obj_file
79 components of ModLocation are undefined.
81 The locations specified by a ModLocation may or may not
82 correspond to actual files yet: for example, even if the object
83 file doesn't exist, the ModLocation still contains the path to
84 where the object file will reside if/when it is created.
87 addBootSuffix :: FilePath -> FilePath
88 -- Add the "-boot" suffix to .hs, .hi and .o files
89 addBootSuffix path = path ++ "-boot"
91 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
92 addBootSuffix_maybe is_boot path
93 | is_boot = addBootSuffix path
96 addBootSuffixLocn :: ModLocation -> ModLocation
97 addBootSuffixLocn locn
98 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
99 , ml_hi_file = addBootSuffix (ml_hi_file locn)
100 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
104 %************************************************************************
106 \subsection{The name of a module}
108 %************************************************************************
111 newtype Module = Module EncodedFS
112 -- Haskell module names can include the quote character ',
113 -- so the module names have the z-encoding applied to them
115 instance Binary Module where
116 put_ bh (Module m) = put_ bh m
117 get bh = do m <- get bh; return (Module m)
119 instance Uniquable Module where
120 getUnique (Module nm) = getUnique nm
122 instance Eq Module where
123 nm1 == nm2 = getUnique nm1 == getUnique nm2
125 -- Warning: gives an ordering relation based on the uniques of the
126 -- FastStrings which are the (encoded) module names. This is _not_
127 -- a lexicographical ordering.
128 instance Ord Module where
129 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
131 instance Outputable Module where
135 pprModule :: Module -> SDoc
136 pprModule (Module nm) = pprEncodedFS nm
138 moduleFS :: Module -> EncodedFS
139 moduleFS (Module mod) = mod
141 moduleString :: Module -> EncodedString
142 moduleString (Module mod) = unpackFS mod
144 moduleUserString :: Module -> UserString
145 moduleUserString (Module mod) = decode (unpackFS mod)
147 -- used to be called mkSrcModule
148 mkModule :: UserString -> Module
149 mkModule s = Module (mkFastString (encode s))
151 -- used to be called mkSrcModuleFS
152 mkModuleFS :: UserFS -> Module
153 mkModuleFS s = Module (encodeFS s)
155 -- used to be called mkSysModuleFS
156 mkSysModuleFS :: EncodedFS -> Module
157 mkSysModuleFS s = Module s
160 %************************************************************************
162 \subsection{@ModuleEnv@s}
164 %************************************************************************
167 type ModuleEnv elt = UniqFM elt
169 emptyModuleEnv :: ModuleEnv a
170 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
171 unitModuleEnv :: Module -> a -> ModuleEnv a
172 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
173 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
174 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
175 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
177 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
178 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
179 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
180 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
181 moduleEnvElts :: ModuleEnv a -> [a]
183 isEmptyModuleEnv :: ModuleEnv a -> Bool
184 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
185 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
186 elemModuleEnv :: Module -> ModuleEnv a -> Bool
187 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
188 filterModuleEnv :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
190 filterModuleEnv = filterUFM
191 elemModuleEnv = elemUFM
192 extendModuleEnv = addToUFM
193 extendModuleEnv_C = addToUFM_C
194 extendModuleEnvList = addListToUFM
195 plusModuleEnv_C = plusUFM_C
196 delModuleEnvList = delListFromUFM
197 delModuleEnv = delFromUFM
198 plusModuleEnv = plusUFM
199 lookupModuleEnv = lookupUFM
200 lookupWithDefaultModuleEnv = lookupWithDefaultUFM
201 mapModuleEnv = mapUFM
202 mkModuleEnv = listToUFM
203 emptyModuleEnv = emptyUFM
204 moduleEnvElts = eltsUFM
205 unitModuleEnv = unitUFM
206 isEmptyModuleEnv = isNullUFM
207 foldModuleEnv = foldUFM
211 type ModuleSet = UniqSet Module
212 mkModuleSet :: [Module] -> ModuleSet
213 extendModuleSet :: ModuleSet -> Module -> ModuleSet
214 emptyModuleSet :: ModuleSet
215 moduleSetElts :: ModuleSet -> [Module]
216 elemModuleSet :: Module -> ModuleSet -> Bool
218 emptyModuleSet = emptyUniqSet
219 mkModuleSet = mkUniqSet
220 extendModuleSet = addOneToUniqSet
221 moduleSetElts = uniqSetToList
222 elemModuleSet = elementOfUniqSet