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