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