Extend API for compiling to and from Core
[ghc-hetmet.git] / compiler / basicTypes / Module.lhs
1 %
2 % (c) The University of Glasgow, 2004-2006
3 %
4
5 Module
6 ~~~~~~~~~~
7 Simply the name of a module, represented as a FastString.
8 These are Uniquable, hence we can build FiniteMaps with Modules as
9 the keys.
10
11 \begin{code}
12 module Module 
13     (
14         -- * The ModuleName type
15         ModuleName,
16         pprModuleName,
17         moduleNameFS,
18         moduleNameString,
19         moduleNameSlashes,
20         mkModuleName,
21         mkModuleNameFS,
22
23         -- * The PackageId type
24         PackageId,
25         fsToPackageId,
26         packageIdFS,
27         stringToPackageId,
28         packageIdString,
29
30         -- * Wired-in PackageIds
31         basePackageId,
32         rtsPackageId,
33         haskell98PackageId,
34         thPackageId,
35         ndpPackageId,
36         mainPackageId,
37
38         -- * The Module type
39         Module,
40         modulePackageId, moduleName,
41         pprModule,
42         mkModule,
43
44         -- * The ModuleLocation type
45         ModLocation(..),
46         addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
47
48         -- * Module mappings
49         ModuleEnv,
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,
56
57         -- * ModuleName mappings
58         ModuleNameEnv,
59
60         -- * Sets of modules
61         ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet,
62         elemModuleSet
63     ) where
64
65 #include "HsVersions.h"
66 import Outputable
67 import qualified Pretty
68 import Unique
69 import FiniteMap
70 import UniqFM
71 import FastString
72 import Binary
73 \end{code}
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection{Module locations}
78 %*                                                                      *
79 %************************************************************************
80
81 \begin{code}
82 data ModLocation
83    = ModLocation {
84         ml_hs_file   :: Maybe FilePath,
85                 -- The source file, if we have one.  Package modules
86                 -- probably don't have source files.
87
88         ml_hi_file   :: FilePath,
89                 -- Where the .hi file is, whether or not it exists
90                 -- yet.  Always of form foo.hi, even if there is an
91                 -- hi-boot file (we add the -boot suffix later)
92
93         ml_obj_file  :: FilePath
94                 -- Where the .o file is, whether or not it exists yet.
95                 -- (might not exist either because the module hasn't
96                 -- been compiled yet, or because it is part of a
97                 -- package with a .a file)
98   } deriving Show
99
100 instance Outputable ModLocation where
101    ppr = text . show
102 \end{code}
103
104 For a module in another package, the hs_file and obj_file
105 components of ModLocation are undefined.  
106
107 The locations specified by a ModLocation may or may not
108 correspond to actual files yet: for example, even if the object
109 file doesn't exist, the ModLocation still contains the path to
110 where the object file will reside if/when it is created.
111
112 \begin{code}
113 addBootSuffix :: FilePath -> FilePath
114 -- Add the "-boot" suffix to .hs, .hi and .o files
115 addBootSuffix path = path ++ "-boot"
116
117 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
118 addBootSuffix_maybe is_boot path
119  | is_boot   = addBootSuffix path
120  | otherwise = path
121
122 addBootSuffixLocn :: ModLocation -> ModLocation
123 addBootSuffixLocn locn
124   = locn { ml_hs_file  = fmap addBootSuffix (ml_hs_file locn)
125          , ml_hi_file  = addBootSuffix (ml_hi_file locn)
126          , ml_obj_file = addBootSuffix (ml_obj_file locn) }
127 \end{code}
128
129
130 %************************************************************************
131 %*                                                                      *
132 \subsection{The name of a module}
133 %*                                                                      *
134 %************************************************************************
135
136 \begin{code}
137 -- | A ModuleName is a simple string, eg. @Data.List@.
138 newtype ModuleName = ModuleName FastString
139
140 instance Uniquable ModuleName where
141   getUnique (ModuleName nm) = getUnique nm
142
143 instance Eq ModuleName where
144   nm1 == nm2 = getUnique nm1 == getUnique nm2
145
146 -- Warning: gives an ordering relation based on the uniques of the
147 -- FastStrings which are the (encoded) module names.  This is _not_
148 -- a lexicographical ordering.
149 instance Ord ModuleName where
150   nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
151
152 instance Outputable ModuleName where
153   ppr = pprModuleName
154
155 instance Binary ModuleName where
156   put_ bh (ModuleName fs) = put_ bh fs
157   get bh = do fs <- get bh; return (ModuleName fs)
158
159 pprModuleName :: ModuleName -> SDoc
160 pprModuleName (ModuleName nm) = 
161     getPprStyle $ \ sty ->
162     if codeStyle sty 
163         then ftext (zEncodeFS nm)
164         else ftext nm
165
166 moduleNameFS :: ModuleName -> FastString
167 moduleNameFS (ModuleName mod) = mod
168
169 moduleNameString :: ModuleName -> String
170 moduleNameString (ModuleName mod) = unpackFS mod
171
172 mkModuleName :: String -> ModuleName
173 mkModuleName s = ModuleName (mkFastString s)
174
175 mkModuleNameFS :: FastString -> ModuleName
176 mkModuleNameFS s = ModuleName s
177
178 -- Returns the string version of the module name, with dots replaced by slashes
179 moduleNameSlashes :: ModuleName -> String
180 moduleNameSlashes = dots_to_slashes . moduleNameString
181   where dots_to_slashes = map (\c -> if c == '.' then '/' else c)
182 \end{code}
183
184 %************************************************************************
185 %*                                                                      *
186 \subsection{A fully qualified module}
187 %*                                                                      *
188 %************************************************************************
189
190 \begin{code}
191 -- | A Module is a pair of a 'PackageId' and a 'ModuleName'.
192 data Module = Module {
193    modulePackageId :: !PackageId,  -- pkg-1.0
194    moduleName      :: !ModuleName  -- A.B.C
195   }
196   deriving (Eq, Ord)
197
198 instance Outputable Module where
199   ppr = pprModule
200
201 instance Binary Module where
202   put_ bh (Module p n) = put_ bh p >> put_ bh n
203   get bh = do p <- get bh; n <- get bh; return (Module p n)
204
205 instance Uniquable PackageId where
206  getUnique pid = getUnique (packageIdFS pid)
207
208 mkModule :: PackageId -> ModuleName -> Module
209 mkModule = Module
210
211 pprModule :: Module -> SDoc
212 pprModule mod@(Module p n)  = pprPackagePrefix p mod <> pprModuleName n
213
214 pprPackagePrefix :: PackageId -> Module -> PprStyle -> Pretty.Doc
215 pprPackagePrefix p mod = getPprStyle doc
216  where
217    doc sty
218        | codeStyle sty = 
219           if p == mainPackageId 
220                 then empty -- never qualify the main package in code
221                 else ftext (zEncodeFS (packageIdFS p)) <> char '_'
222        | qualModule sty mod = ftext (packageIdFS (modulePackageId mod)) <> char ':'
223                 -- the PrintUnqualified tells us which modules have to
224                 -- be qualified with package names
225        | otherwise = empty
226 \end{code}
227
228 %************************************************************************
229 %*                                                                      *
230 \subsection{PackageId}
231 %*                                                                      *
232 %************************************************************************
233
234 \begin{code}
235 newtype PackageId = PId FastString deriving( Eq, Ord )  -- includes the version
236     -- here to avoid module loops with PackageConfig
237
238 instance Outputable PackageId where
239    ppr pid = text (packageIdString pid)
240
241 instance Binary PackageId where
242   put_ bh pid = put_ bh (packageIdFS pid)
243   get bh = do { fs <- get bh; return (fsToPackageId fs) }
244
245 fsToPackageId :: FastString -> PackageId
246 fsToPackageId = PId
247
248 packageIdFS :: PackageId -> FastString
249 packageIdFS (PId fs) = fs
250
251 stringToPackageId :: String -> PackageId
252 stringToPackageId = fsToPackageId . mkFastString
253
254 packageIdString :: PackageId -> String
255 packageIdString = unpackFS . packageIdFS
256
257
258 -- -----------------------------------------------------------------------------
259 -- Package Ids that are wired in
260
261 -- Certain packages are "known" to the compiler, in that we know about certain
262 -- entities that reside in these packages, and the compiler needs to 
263 -- declare static Modules and Names that refer to these packages.  Hence
264 -- the wired-in packages can't include version numbers, since we don't want
265 -- to bake the version numbers of these packages into GHC.
266 --
267 -- So here's the plan.  Wired-in packages are still versioned as
268 -- normal in the packages database, and you can still have multiple
269 -- versions of them installed.  However, for each invocation of GHC,
270 -- only a single instance of each wired-in package will be recognised
271 -- (the desired one is selected via -package/-hide-package), and GHC
272 -- will use the unversioned PackageId below when referring to it,
273 -- including in .hi files and object file symbols.  Unselected
274 -- versions of wired-in packages will be ignored, as will any other
275 -- package that depends directly or indirectly on it (much as if you
276 -- had used -ignore-package).
277
278 basePackageId, rtsPackageId, haskell98PackageId, 
279   thPackageId, ndpPackageId, mainPackageId  :: PackageId
280 basePackageId      = fsToPackageId FSLIT("base")
281 rtsPackageId       = fsToPackageId FSLIT("rts")
282 haskell98PackageId = fsToPackageId FSLIT("haskell98")
283 thPackageId        = fsToPackageId FSLIT("template-haskell")
284 ndpPackageId       = fsToPackageId FSLIT("ndp")
285
286 -- This is the package Id for the program.  It is the default package
287 -- Id if you don't specify a package name.  We don't add this prefix
288 -- to symbol name, since there can be only one main package per program.
289 mainPackageId      = fsToPackageId FSLIT("main")
290 \end{code}
291
292 %************************************************************************
293 %*                                                                      *
294 \subsection{@ModuleEnv@s}
295 %*                                                                      *
296 %************************************************************************
297
298 \begin{code}
299 type ModuleEnv elt = FiniteMap Module elt
300
301 emptyModuleEnv       :: ModuleEnv a
302 mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
303 unitModuleEnv        :: Module -> a -> ModuleEnv a
304 extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
305 extendModuleEnv_C    :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
306 plusModuleEnv        :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
307 extendModuleEnvList  :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
308 extendModuleEnvList_C  :: (a->a->a) -> ModuleEnv a -> [(Module, a)] -> ModuleEnv a
309                   
310 delModuleEnvList     :: ModuleEnv a -> [Module] -> ModuleEnv a
311 delModuleEnv         :: ModuleEnv a -> Module -> ModuleEnv a
312 plusModuleEnv_C      :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
313 mapModuleEnv         :: (a -> b) -> ModuleEnv a -> ModuleEnv b
314 moduleEnvKeys        :: ModuleEnv a -> [Module]
315 moduleEnvElts        :: ModuleEnv a -> [a]
316                   
317 isEmptyModuleEnv     :: ModuleEnv a -> Bool
318 lookupModuleEnv      :: ModuleEnv a -> Module     -> Maybe a
319 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
320 elemModuleEnv        :: Module -> ModuleEnv a -> Bool
321 foldModuleEnv        :: (a -> b -> b) -> b -> ModuleEnv a -> b
322 filterModuleEnv      :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
323
324 filterModuleEnv f   = filterFM (\_ v -> f v)
325 elemModuleEnv       = elemFM
326 extendModuleEnv     = addToFM
327 extendModuleEnv_C   = addToFM_C
328 extendModuleEnvList = addListToFM
329 extendModuleEnvList_C = addListToFM_C
330 plusModuleEnv_C     = plusFM_C
331 delModuleEnvList    = delListFromFM
332 delModuleEnv        = delFromFM
333 plusModuleEnv       = plusFM
334 lookupModuleEnv     = lookupFM
335 lookupWithDefaultModuleEnv = lookupWithDefaultFM
336 mapModuleEnv f      = mapFM (\_ v -> f v)
337 mkModuleEnv         = listToFM
338 emptyModuleEnv      = emptyFM
339 moduleEnvKeys       = keysFM
340 moduleEnvElts       = eltsFM
341 unitModuleEnv       = unitFM
342 isEmptyModuleEnv    = isEmptyFM
343 foldModuleEnv f     = foldFM (\_ v -> f v)
344 \end{code}
345
346 \begin{code}
347 type ModuleSet = FiniteMap Module ()
348 mkModuleSet     :: [Module] -> ModuleSet
349 extendModuleSet :: ModuleSet -> Module -> ModuleSet
350 emptyModuleSet  :: ModuleSet
351 moduleSetElts   :: ModuleSet -> [Module]
352 elemModuleSet   :: Module -> ModuleSet -> Bool
353
354 emptyModuleSet    = emptyFM
355 mkModuleSet ms    = listToFM [(m,()) | m <- ms ]
356 extendModuleSet s m = addToFM s m ()
357 moduleSetElts     = keysFM
358 elemModuleSet     = elemFM
359 \end{code}
360
361 A ModuleName has a Unique, so we can build mappings of these using
362 UniqFM.
363
364 \begin{code}
365 type ModuleNameEnv elt = UniqFM elt
366 \end{code}