[project @ 2005-01-14 17:57:41 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Module.lhs
1 %
2 % (c) The University of Glasgow, 2004
3 %
4
5 Module
6 ~~~~~~~~~~
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
9 the keys.
10
11 \begin{code}
12 module Module 
13     (
14       Module,                   -- Abstract, instance of Eq, Ord, Outputable
15     , pprModule                 -- :: ModuleName -> SDoc
16
17     , ModLocation(..),
18     , showModMsg
19
20     , moduleString              -- :: ModuleName -> EncodedString
21     , moduleUserString          -- :: ModuleName -> UserString
22     , moduleFS                  -- :: ModuleName -> EncodedFS
23
24     , mkModule                  -- :: UserString -> ModuleName
25     , mkModuleFS                -- :: UserFS    -> ModuleName
26     , mkSysModuleFS             -- :: EncodedFS -> ModuleName
27  
28     , ModuleEnv,
29     , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
30     , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
31     , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
32     , moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
33     , extendModuleEnv_C
34
35     , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
36
37     ) where
38
39 #include "HsVersions.h"
40 import OccName
41 import Outputable
42 import Unique           ( Uniquable(..) )
43 import Maybes           ( expectJust )
44 import UniqFM
45 import UniqSet
46 import Binary
47 import StringBuffer     ( StringBuffer )
48 import FastString
49 \end{code}
50
51 %************************************************************************
52 %*                                                                      *
53 \subsection{Module locations}
54 %*                                                                      *
55 %************************************************************************
56
57 \begin{code}
58 data ModLocation
59    = ModLocation {
60         ml_hs_file   :: Maybe FilePath,
61                 -- the source file, if we have one.  Package modules
62                 -- probably don't have source files.
63
64         ml_hspp_file :: Maybe FilePath,
65                 -- filename of preprocessed source, if we have
66                 -- preprocessed it.
67         ml_hspp_buf  :: Maybe StringBuffer,
68                 -- the actual preprocessed source, maybe.
69
70         ml_hi_file   :: FilePath,
71                 -- Where the .hi file is, whether or not it exists
72                 -- yet.  Always of form foo.hi, even if there is an
73                 -- hi-boot file (we add the -boot suffix later)
74
75         ml_obj_file  :: FilePath
76                 -- Where the .o file is, whether or not it exists yet.
77                 -- (might not exist either because the module hasn't
78                 -- been compiled yet, or because it is part of a
79                 -- package with a .a file)
80   } deriving Show
81
82 instance Outputable ModLocation where
83    ppr = text . show
84
85 -- Rather a gruesome function to have in Module
86
87 showModMsg :: Bool -> Module -> ModLocation -> String
88 showModMsg use_object mod location =
89     mod_str ++ replicate (max 0 (16 - length mod_str)) ' '
90     ++" ( " ++ expectJust "showModMsg" (ml_hs_file location) ++ ", "
91     ++ (if use_object
92           then ml_obj_file location
93           else "interpreted")
94     ++ " )"
95  where mod_str = moduleUserString mod
96 \end{code}
97
98 For a module in another package, the hs_file and obj_file
99 components of ModLocation are undefined.  
100
101 The locations specified by a ModLocation may or may not
102 correspond to actual files yet: for example, even if the object
103 file doesn't exist, the ModLocation still contains the path to
104 where the object file will reside if/when it is created.
105
106
107 %************************************************************************
108 %*                                                                      *
109 \subsection{The name of a module}
110 %*                                                                      *
111 %************************************************************************
112
113 \begin{code}
114 newtype Module = Module EncodedFS
115         -- Haskell module names can include the quote character ',
116         -- so the module names have the z-encoding applied to them
117
118 instance Binary Module where
119    put_ bh (Module m) = put_ bh m
120    get bh = do m <- get bh; return (Module m)
121
122 instance Uniquable Module where
123   getUnique (Module nm) = getUnique nm
124
125 instance Eq Module where
126   nm1 == nm2 = getUnique nm1 == getUnique nm2
127
128 -- Warning: gives an ordering relation based on the uniques of the
129 -- FastStrings which are the (encoded) module names.  This is _not_
130 -- a lexicographical ordering.
131 instance Ord Module where
132   nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
133
134 instance Outputable Module where
135   ppr = pprModule
136
137
138 pprModule :: Module -> SDoc
139 pprModule (Module nm) = pprEncodedFS nm
140
141 moduleFS :: Module -> EncodedFS
142 moduleFS (Module mod) = mod
143
144 moduleString :: Module -> EncodedString
145 moduleString (Module mod) = unpackFS mod
146
147 moduleUserString :: Module -> UserString
148 moduleUserString (Module mod) = decode (unpackFS mod)
149
150 -- used to be called mkSrcModule
151 mkModule :: UserString -> Module
152 mkModule s = Module (mkFastString (encode s))
153
154 -- used to be called mkSrcModuleFS
155 mkModuleFS :: UserFS -> Module
156 mkModuleFS s = Module (encodeFS s)
157
158 -- used to be called mkSysModuleFS
159 mkSysModuleFS :: EncodedFS -> Module
160 mkSysModuleFS s = Module s 
161 \end{code}
162
163 %************************************************************************
164 %*                                                                      *
165 \subsection{@ModuleEnv@s}
166 %*                                                                      *
167 %************************************************************************
168
169 \begin{code}
170 type ModuleEnv elt = UniqFM elt
171
172 emptyModuleEnv       :: ModuleEnv a
173 mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
174 unitModuleEnv        :: Module -> a -> ModuleEnv a
175 extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
176 extendModuleEnv_C    :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
177 plusModuleEnv        :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
178 extendModuleEnvList  :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
179                   
180 delModuleEnvList     :: ModuleEnv a -> [Module] -> ModuleEnv a
181 delModuleEnv         :: ModuleEnv a -> Module -> ModuleEnv a
182 plusModuleEnv_C      :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
183 mapModuleEnv         :: (a -> b) -> ModuleEnv a -> ModuleEnv b
184 moduleEnvElts        :: ModuleEnv a -> [a]
185                   
186 isEmptyModuleEnv     :: ModuleEnv a -> Bool
187 lookupModuleEnv      :: ModuleEnv a -> Module     -> Maybe a
188 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
189 elemModuleEnv        :: Module -> ModuleEnv a -> Bool
190 foldModuleEnv        :: (a -> b -> b) -> b -> ModuleEnv a -> b
191
192 elemModuleEnv       = elemUFM
193 extendModuleEnv     = addToUFM
194 extendModuleEnv_C   = addToUFM_C
195 extendModuleEnvList = addListToUFM
196 plusModuleEnv_C     = plusUFM_C
197 delModuleEnvList    = delListFromUFM
198 delModuleEnv        = delFromUFM
199 plusModuleEnv       = plusUFM
200 lookupModuleEnv     = lookupUFM
201 lookupWithDefaultModuleEnv = lookupWithDefaultUFM
202 mapModuleEnv        = mapUFM
203 mkModuleEnv         = listToUFM
204 emptyModuleEnv      = emptyUFM
205 moduleEnvElts       = eltsUFM
206 unitModuleEnv       = unitUFM
207 isEmptyModuleEnv    = isNullUFM
208 foldModuleEnv       = foldUFM
209 \end{code}
210
211 \begin{code}
212 type ModuleSet = UniqSet Module
213 mkModuleSet     :: [Module] -> ModuleSet
214 extendModuleSet :: ModuleSet -> Module -> ModuleSet
215 emptyModuleSet  :: ModuleSet
216 moduleSetElts   :: ModuleSet -> [Module]
217 elemModuleSet   :: Module -> ModuleSet -> Bool
218
219 emptyModuleSet  = emptyUniqSet
220 mkModuleSet     = mkUniqSet
221 extendModuleSet = addOneToUniqSet
222 moduleSetElts   = uniqSetToList
223 elemModuleSet   = elementOfUniqSet
224 \end{code}