Module.lhs 11.4 KB
Newer Older
sof's avatar
sof committed
1
2
3
4
5
6
7
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
\section[Module]{The @Module@ module.}

Representing modules and their flavours.

8
9
10
11
12
13
14
15
16
17
18
19
20

Notes on DLLs
~~~~~~~~~~~~~
When compiling module A, which imports module B, we need to 
know whether B will be in the same DLL as A.  
	If it's in the same DLL, we refer to B_f_closure
	If it isn't, we refer to _imp__B_f_closure
When compiling A, we record in B's Module value whether it's
in a different DLL, by setting the DLL flag.




sof's avatar
sof committed
21
22
23
\begin{code}
module Module 
    (
24
25
26
27
28
29
      Module, 		   	-- Abstract, instance of Eq, Ord, Outputable

    , PackageName		-- = FastString; instance of Outputable, Uniquable
    , modulePackage		-- :: Module -> PackageName
    , preludePackage		-- :: PackageName	name of Standard Prelude package

sof's avatar
sof committed
30
    , ModuleName
31
    , pprModuleName		-- :: ModuleName -> SDoc
32
    , printModulePrefix
sof's avatar
sof committed
33

34
    , moduleName		-- :: Module -> ModuleName 
sof's avatar
sof committed
35
36
    , moduleNameString		-- :: ModuleName -> EncodedString
    , moduleNameUserString	-- :: ModuleName -> UserString
37
    , moduleNameFS		-- :: ModuleName -> EncodedFS
sof's avatar
sof committed
38

39
40
    , moduleString		-- :: Module -> EncodedString
    , moduleUserString		-- :: Module -> UserString
sof's avatar
sof committed
41

42
    , mkVanillaModule	        -- :: ModuleName -> Module
43
    , isVanillaModule		-- :: Module -> Bool
44
    , mkPrelModule		-- :: UserString -> Module
45
    , isPrelModule		-- :: Module -> Bool
46
47
    , mkModule			-- :: ModuleName -> PackageName -> Module
    , mkHomeModule		-- :: ModuleName -> Module
48
    , isHomeModule		-- :: Module -> Bool
sof's avatar
sof committed
49

50
51
52
    , mkModuleName		-- :: UserString -> ModuleName
    , mkModuleNameFS		-- :: UserFS    -> ModuleName
    , mkSysModuleNameFS		-- :: EncodedFS -> ModuleName
sof's avatar
sof committed
53

54
    , pprModule,
sof's avatar
sof committed
55
56
 
	-- Where to find a .hi file
57
    , WhereFrom(..)
sof's avatar
sof committed
58

59
60
61
62
    , ModuleEnv,
    , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
    , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
    , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
63
    , moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
64
    , lookupModuleEnvByName, extendModuleEnv_C
65

66
67
    , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet

sof's avatar
sof committed
68
69
70
71
72
    ) where

#include "HsVersions.h"
import OccName
import Outputable
73
import CmdLineOpts	( opt_InPackage )
74
75
import FastString	( FastString )
import Unique		( Uniquable(..) )
76
import UniqFM
77
import UniqSet
sof's avatar
sof committed
78
79
80
81
82
83
84
85
86
\end{code}


%************************************************************************
%*									*
\subsection{Interface file flavour}
%*									*
%************************************************************************

87
88
89
90
91
92
93
A further twist to the tale is the support for dynamically linked
libraries under Win32. Here, dealing with the use of global variables
that's residing in a DLL requires special handling at the point of use
(there's an extra level of indirection, i.e., (**v) to get at v's
value, rather than just (*v) .) When slurping in an interface file we
then record whether it's coming from a .hi corresponding to a module
that's packaged up in a DLL or not, so that we later can emit the
sof's avatar
sof committed
94
95
appropriate code.

96
97
98
The logic for how an interface file is marked as corresponding to a
module that's hiding in a DLL is explained elsewhere (ToDo: give
renamer href here.)
99

sof's avatar
sof committed
100
\begin{code}
101
data Module = Module ModuleName PackageInfo
102

103
data PackageInfo
104
105
106
  = ThisPackage				-- A module from the same package 
					-- as the one being compiled
  | AnotherPackage PackageName		-- A module from a different package
107

108
109
110
111
  | DunnoYet	-- This is used when we don't yet know
		-- Main case: we've come across Foo.x in an interface file
		-- but we havn't yet opened Foo.hi.  We need a Name for Foo.x
		-- Later on (in RnEnv.newTopBinder) we'll update the cache
112
		-- to have the right PackageName
113

114
type PackageName = FastString		-- No encoding at all
sof's avatar
sof committed
115

116
117
preludePackage :: PackageName
preludePackage = SLIT("std")
sof's avatar
sof committed
118

119
packageInfoPackage :: PackageInfo -> PackageName
120
packageInfoPackage ThisPackage        = opt_InPackage
121
122
123
packageInfoPackage DunnoYet	      = SLIT("<?>")
packageInfoPackage (AnotherPackage p) = p

124
125
instance Outputable PackageInfo where
	-- Just used in debug prints of lex tokens and in debug modde
126
   ppr pkg_info = ppr (packageInfoPackage pkg_info)
sof's avatar
sof committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
\end{code}


%************************************************************************
%*									*
\subsection{Where from}
%*									*
%************************************************************************

The @WhereFrom@ type controls where the renamer looks for an interface file

\begin{code}
data WhereFrom = ImportByUser		-- Ordinary user import: look for M.hi
	       | ImportByUserSource	-- User {- SOURCE -}: look for M.hi-boot
	       | ImportBySystem		-- Non user import.  Look for M.hi if M is in
					-- the module this module depends on, or is a system-ish module; 
					-- M.hi-boot otherwise
144
145
146
	       | ImportByCmdLine        -- The user typed a qualified name at
					-- the GHCi prompt, try to demand-load
					-- the interface.
sof's avatar
sof committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

instance Outputable WhereFrom where
  ppr ImportByUser       = empty
  ppr ImportByUserSource = ptext SLIT("{- SOURCE -}")
  ppr ImportBySystem     = ptext SLIT("{- SYSTEM IMPORT -}")
\end{code}


%************************************************************************
%*									*
\subsection{The name of a module}
%*									*
%************************************************************************

\begin{code}
162
newtype ModuleName = ModuleName EncodedFS
sof's avatar
sof committed
163
164
165
	-- Haskell module names can include the quote character ',
	-- so the module names have the z-encoding applied to them

166
instance Uniquable ModuleName where
167
  getUnique (ModuleName nm) = getUnique nm
168
169
170
171
172
173
174
175
176
177
178
179
180
181

instance Eq ModuleName where
  nm1 == nm2 = getUnique nm1 == getUnique nm2

-- Warning: gives an ordering relation based on the uniques of the
-- FastStrings which are the (encoded) module names.  This is _not_
-- a lexicographical ordering.
instance Ord ModuleName where
  nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2

instance Outputable ModuleName where
  ppr = pprModuleName


sof's avatar
sof committed
182
pprModuleName :: ModuleName -> SDoc
183
pprModuleName (ModuleName nm) = pprEncodedFS nm
sof's avatar
sof committed
184

185
186
187
moduleNameFS :: ModuleName -> EncodedFS
moduleNameFS (ModuleName mod) = mod

sof's avatar
sof committed
188
moduleNameString :: ModuleName -> EncodedString
189
moduleNameString (ModuleName mod) = _UNPK_ mod
sof's avatar
sof committed
190
191

moduleNameUserString :: ModuleName -> UserString
192
moduleNameUserString (ModuleName mod) = decode (_UNPK_ mod)
sof's avatar
sof committed
193

194
195
196
-- used to be called mkSrcModule
mkModuleName :: UserString -> ModuleName
mkModuleName s = ModuleName (_PK_ (encode s))
sof's avatar
sof committed
197

198
199
200
-- used to be called mkSrcModuleFS
mkModuleNameFS :: UserFS -> ModuleName
mkModuleNameFS s = ModuleName (encodeFS s)
sof's avatar
sof committed
201

202
203
204
-- used to be called mkSysModuleFS
mkSysModuleNameFS :: EncodedFS -> ModuleName
mkSysModuleNameFS s = ModuleName s 
sof's avatar
sof committed
205
206
207
208
209
210
\end{code}

\begin{code}
instance Outputable Module where
  ppr = pprModule

211
instance Uniquable Module where
212
  getUnique (Module nm _) = getUnique nm
213
214

-- Same if they have the same name.
sof's avatar
sof committed
215
instance Eq Module where
216
  m1 == m2 = getUnique m1 == getUnique m2
sof's avatar
sof committed
217

218
219
220
-- Warning: gives an ordering relation based on the uniques of the
-- FastStrings which are the (encoded) module names.  This is _not_
-- a lexicographical ordering.
sof's avatar
sof committed
221
instance Ord Module where
222
  m1 `compare` m2 = getUnique m1 `compare` getUnique m2
sof's avatar
sof committed
223
224
225
226
227
\end{code}


\begin{code}
pprModule :: Module -> SDoc
228
pprModule (Module mod p) = getPprStyle $ \ sty ->
229
			   if debugStyle sty then
230
				-- Print the package too
231
				ppr p <> dot <> pprModuleName mod
232
			   else
233
				pprModuleName mod
sof's avatar
sof committed
234
235
236
237
\end{code}


\begin{code}
238
239
240
241
242
243
244
245
mkModule :: ModuleName	-- Name of the module
	 -> PackageName
	 -> Module
mkModule mod_nm pack_name
  = Module mod_nm pack_info
  where
    pack_info | pack_name == opt_InPackage = ThisPackage
	      | otherwise		   = AnotherPackage pack_name
246

247
248
mkHomeModule :: ModuleName -> Module
mkHomeModule mod_nm = Module mod_nm ThisPackage
249

250
251
252
253
isHomeModule :: Module -> Bool
isHomeModule (Module nm ThisPackage) = True
isHomeModule _                       = False

254
255
-- Used temporarily when we first come across Foo.x in an interface
-- file, but before we've opened Foo.hi.
256
-- (Until we've opened Foo.hi we don't know what the Package is.)
257
mkVanillaModule :: ModuleName -> Module
258
mkVanillaModule name = Module name DunnoYet
sof's avatar
sof committed
259

260
261
262
263
isVanillaModule :: Module -> Bool
isVanillaModule (Module nm DunnoYet) = True
isVanillaModule _                       = False

264
mkPrelModule :: ModuleName -> Module
265
mkPrelModule name = mkModule name preludePackage
sof's avatar
sof committed
266

267
268
269
270
isPrelModule :: Module -> Bool
isPrelModule (Module nm (AnotherPackage p)) | p == preludePackage = True
isPrelModule _                       = False

sof's avatar
sof committed
271
moduleString :: Module -> EncodedString
272
moduleString (Module (ModuleName fs) _) = _UNPK_ fs
sof's avatar
sof committed
273

274
moduleName :: Module -> ModuleName
275
moduleName (Module mod pkg_info) = mod
276

277
278
279
modulePackage :: Module -> PackageName
modulePackage (Module mod pkg_info) = packageInfoPackage pkg_info

sof's avatar
sof committed
280
moduleUserString :: Module -> UserString
281
moduleUserString (Module mod _) = moduleNameUserString mod
sof's avatar
sof committed
282

283
284
285
286
printModulePrefix :: Module -> Bool
  -- When printing, say M.x
printModulePrefix (Module nm ThisPackage) = False
printModulePrefix _                       = True
sof's avatar
sof committed
287
\end{code}
288

289

290
291
292
293
294
295
296
297
298
299
300
301
302
%************************************************************************
%*                                                                      *
\subsection{@ModuleEnv@s}
%*                                                                      *
%************************************************************************

\begin{code}
type ModuleEnv elt = UniqFM elt

emptyModuleEnv       :: ModuleEnv a
mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
unitModuleEnv        :: Module -> a -> ModuleEnv a
extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
303
extendModuleEnv_C    :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
304
305
306
307
308
309
310
plusModuleEnv        :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
extendModuleEnvList  :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
                  
delModuleEnvList     :: ModuleEnv a -> [Module] -> ModuleEnv a
delModuleEnv         :: ModuleEnv a -> Module -> ModuleEnv a
plusModuleEnv_C      :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
mapModuleEnv         :: (a -> b) -> ModuleEnv a -> ModuleEnv b
311
moduleEnvElts        :: ModuleEnv a -> [a]
312
313
                  
isEmptyModuleEnv     :: ModuleEnv a -> Bool
314
315
lookupModuleEnv      :: ModuleEnv a -> Module     -> Maybe a
lookupModuleEnvByName:: ModuleEnv a -> ModuleName -> Maybe a
316
317
318
319
320
321
lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
elemModuleEnv        :: Module -> ModuleEnv a -> Bool
foldModuleEnv        :: (a -> b -> b) -> b -> ModuleEnv a -> b

elemModuleEnv       = elemUFM
extendModuleEnv     = addToUFM
322
extendModuleEnv_C   = addToUFM_C
323
324
325
326
327
328
extendModuleEnvList = addListToUFM
plusModuleEnv_C     = plusUFM_C
delModuleEnvList    = delListFromUFM
delModuleEnv        = delFromUFM
plusModuleEnv       = plusUFM
lookupModuleEnv     = lookupUFM
329
lookupModuleEnvByName = lookupUFM
330
331
332
333
lookupWithDefaultModuleEnv = lookupWithDefaultUFM
mapModuleEnv        = mapUFM
mkModuleEnv         = listToUFM
emptyModuleEnv      = emptyUFM
334
moduleEnvElts       = eltsUFM
335
336
337
unitModuleEnv       = unitUFM
isEmptyModuleEnv    = isNullUFM
foldModuleEnv       = foldUFM
338
\end{code}
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

\begin{code}

type ModuleSet = UniqSet Module
mkModuleSet	:: [Module] -> ModuleSet
extendModuleSet :: ModuleSet -> Module -> ModuleSet
emptyModuleSet  :: ModuleSet
moduleSetElts   :: ModuleSet -> [Module]
elemModuleSet   :: Module -> ModuleSet -> Bool

emptyModuleSet  = emptyUniqSet
mkModuleSet     = mkUniqSet
extendModuleSet = addOneToUniqSet
moduleSetElts   = uniqSetToList
elemModuleSet   = elementOfUniqSet
\end{code}