[project @ 2000-10-12 09:56:52 by sewardj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Module.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Module]{The @Module@ module.}
5
6 Representing modules and their flavours.
7
8
9 Notes on DLLs
10 ~~~~~~~~~~~~~
11 When compiling module A, which imports module B, we need to 
12 know whether B will be in the same DLL as A.  
13         If it's in the same DLL, we refer to B_f_closure
14         If it isn't, we refer to _imp__B_f_closure
15 When compiling A, we record in B's Module value whether it's
16 in a different DLL, by setting the DLL flag.
17
18
19
20
21 \begin{code}
22 module Module 
23     (
24       Module                -- abstract, instance of Eq, Ord, Outputable
25     , ModuleName
26     , ModuleKind(..)
27     , isPackageKind
28
29     , moduleNameString          -- :: ModuleName -> EncodedString
30     , moduleNameUserString      -- :: ModuleName -> UserString
31
32     , moduleString          -- :: Module -> EncodedString
33     , moduleUserString      -- :: Module -> UserString
34     , moduleName            -- :: Module -> ModuleName
35
36 --    , mkVanillaModule     -- :: ModuleName -> Module
37 --    , mkThisModule        -- :: ModuleName -> Module
38 --    , mkPrelModule          -- :: UserString -> Module
39     , mkModule              -- :: ModuleName -> ModuleKind -> Module
40     
41 --    , isLocalModule       -- :: Module -> Bool
42
43     , mkSrcModule
44
45     , mkSrcModuleFS         -- :: UserFS    -> ModuleName
46     , mkSysModuleFS         -- :: EncodedFS -> ModuleName
47
48     , pprModule, pprModuleName
49  
50     , PackageName
51
52         -- Where to find a .hi file
53     , WhereFrom(..)
54
55     ) where
56
57 #include "HsVersions.h"
58 import OccName
59 import Outputable
60 import CmdLineOpts      ( opt_InPackage )
61 import FastString       ( FastString, uniqueOfFS )
62 import Unique           ( Uniquable(..), mkUniqueGrimily )
63 \end{code}
64
65
66 %************************************************************************
67 %*                                                                      *
68 \subsection{Interface file flavour}
69 %*                                                                      *
70 %************************************************************************
71
72 A further twist to the tale is the support for dynamically linked libraries under
73 Win32. Here, dealing with the use of global variables that's residing in a DLL
74 requires special handling at the point of use (there's an extra level of indirection,
75 i.e., (**v) to get at v's value, rather than just (*v) .) When slurping in an
76 interface file we then record whether it's coming from a .hi corresponding to a
77 module that's packaged up in a DLL or not, so that we later can emit the
78 appropriate code.
79
80 The logic for how an interface file is marked as corresponding to a module that's
81 hiding in a DLL is explained elsewhere (ToDo: give renamer href here.)
82
83 @SourceOnly@ and @ObjectCode@ indicate a module from the same package
84 as the one being compiled, i.e. a home module.  @InPackage@ means one
85 from a different package.
86
87 \begin{code}
88 data ModuleKind
89    = SourceOnly FilePath            -- .hs
90    | ObjectCode FilePath FilePath   -- .o, .hi
91    | InPackage  PackageName
92
93 isPackageKind (InPackage _) = True
94 isPackageKind _             = False
95
96 type PackageName = FastString           -- No encoding at all
97
98 preludePackage :: PackageName
99 preludePackage = SLIT("std")
100
101 instance Outputable ModuleKind where
102    ppr (SourceOnly path_hs) 
103       = text "SourceOnly" <+> text (show path_hs)
104    ppr (ObjectCode path_o path_hi)
105       = text "ObjectCode" <+> text (show path_o) <+> text (show path_hi)
106    ppr (InPackage pkgname)
107       = text "InPackage" <+> text (show pkgname)
108 \end{code}
109
110
111 %************************************************************************
112 %*                                                                      *
113 \subsection{Where from}
114 %*                                                                      *
115 %************************************************************************
116
117 The @WhereFrom@ type controls where the renamer looks for an interface file
118
119 \begin{code}
120 data WhereFrom = ImportByUser           -- Ordinary user import: look for M.hi
121                | ImportByUserSource     -- User {- SOURCE -}: look for M.hi-boot
122                | ImportBySystem         -- Non user import.  Look for M.hi if M is in
123                                         -- the module this module depends on, or is a system-ish module; 
124                                         -- M.hi-boot otherwise
125
126 instance Outputable WhereFrom where
127   ppr ImportByUser       = empty
128   ppr ImportByUserSource = ptext SLIT("{- SOURCE -}")
129   ppr ImportBySystem     = ptext SLIT("{- SYSTEM IMPORT -}")
130 \end{code}
131
132
133 %************************************************************************
134 %*                                                                      *
135 \subsection{The name of a module}
136 %*                                                                      *
137 %************************************************************************
138
139 \begin{code}
140 type ModuleName = EncodedFS
141         -- Haskell module names can include the quote character ',
142         -- so the module names have the z-encoding applied to them
143
144 pprModuleName :: ModuleName -> SDoc
145 pprModuleName nm = pprEncodedFS nm
146
147 moduleNameString :: ModuleName -> EncodedString
148 moduleNameString mod = _UNPK_ mod
149
150 moduleNameUserString :: ModuleName -> UserString
151 moduleNameUserString mod = decode (_UNPK_ mod)
152
153 mkSrcModule :: UserString -> ModuleName
154 mkSrcModule s = _PK_ (encode s)
155
156 mkSrcModuleFS :: UserFS -> ModuleName
157 mkSrcModuleFS s = encodeFS s
158
159 mkSysModuleFS :: EncodedFS -> ModuleName
160 mkSysModuleFS s = s 
161 \end{code}
162
163 \begin{code}
164 data Module 
165    = Module {
166         mod_name :: ModuleName,
167         mod_kind :: ModuleKind
168      }
169 \end{code}
170
171 \begin{code}
172 instance Outputable Module where
173   ppr = pprModule
174
175 instance Uniquable Module where
176   getUnique (Module nm _) = mkUniqueGrimily (uniqueOfFS nm)
177
178 -- Same if they have the same name.
179 instance Eq Module where
180   m1 == m2 = getUnique m1 == getUnique m2
181
182 -- Warning: gives an ordering relation based on the uniques of the
183 -- FastStrings which are the (encoded) module names.  This is _not_
184 -- a lexicographical ordering.
185 instance Ord Module where
186   m1 `compare` m2 = getUnique m1 `compare` getUnique m2
187 \end{code}
188
189
190 \begin{code}
191 pprModule :: Module -> SDoc
192 pprModule (Module mod p) = getPprStyle $ \ sty ->
193                            if debugStyle sty then
194                                 -- Print the package too
195                                 ppr p <> dot <> pprModuleName mod
196                            else
197                                 pprModuleName mod
198 \end{code}
199
200
201 \begin{code}
202 mkModule :: ModuleName -> ModuleKind -> Module
203 mkModule = Module
204 -- I don't think anybody except the Finder should ever try to create a
205 -- Module now, so this lot commented out pro tem (JRS)
206 --mkModule :: ModuleName        -- Name of the module
207 --       -> PackageName
208 --       -> Module
209 --mkModule mod_nm pack_name
210 --  = Module mod_nm pack_info
211 --  where
212 --    pack_info | pack_name == opt_InPackage = ThisPackage
213 --            | otherwise                  = AnotherPackage pack_name
214
215
216 --mkVanillaModule :: ModuleName -> Module
217 --mkVanillaModule name = Module name ThisPackage
218         -- Used temporarily when we first come across Foo.x in an interface
219         -- file, but before we've opened Foo.hi.
220         -- (Until we've opened Foo.hi we don't know what the PackageInfo is.)
221
222 --mkThisModule :: ModuleName -> Module  -- The module being compiled
223 --mkThisModule name = Module name ThisPackage
224
225 --mkPrelModule :: ModuleName -> Module
226 --mkPrelModule name = mkModule name preludePackage
227
228 moduleString :: Module -> EncodedString
229 moduleString (Module mod _) = _UNPK_ mod
230
231 moduleName :: Module -> ModuleName
232 moduleName (Module mod _) = mod
233
234 moduleUserString :: Module -> UserString
235 moduleUserString (Module mod _) = moduleNameUserString mod
236 \end{code}
237
238 \begin{code}
239 --isLocalModule :: Module -> Bool
240 --isLocalModule (Module _ ThisPackage) = True
241 --isLocalModule _                            = False
242 \end{code}