[project @ 2000-10-13 10:26:38 by sewardj]
[ghc-hetmet.git] / ghc / compiler / main / HscTypes.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[HscTypes]{Types for the per-module compiler}
5
6 \begin{code}
7 module HscTypes ( TyThing(..) )
8 where
9
10 #include "HsVersions.h"
11
12 import Name             ( Name, NameEnv, NamedThing,
13                           unitNameEnv, extendNameEnv, plusNameEnv, 
14                           lookupNameEnv, emptyNameEnv, getName, nameModule )
15 import Module           ( Module, ModuleName,
16                           extendModuleEnv, lookupModuleEnv )
17 import Class            ( Class )
18 import OccName          ( OccName )
19 import RdrName          ( RdrNameEnv, emptyRdrEnv )
20 import Outputable       ( SDoc )
21 import UniqFM           ( UniqFM )
22 import FiniteMap        ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
23 import Bag              ( Bag )
24 import Id               ( Id )
25 import VarEnv           ( IdEnv )
26 import BasicTypes       ( Version, Fixity, defaultFixity )
27 import TyCon            ( TyCon )
28 import ErrUtils         ( ErrMsg, WarnMsg )
29 import CmLink           ( Linkable )
30 import RdrHsSyn         ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameHsDecl,
31                           RdrNameDeprecation, RdrNameFixitySig )
32 import UniqSupply       ( UniqSupply )
33 import HsDecls          ( DeprecTxt )
34 import CoreSyn          ( CoreRule )
35 import NameSet          ( NameSet )
36 import Type             ( Type )
37 import VarSet           ( TyVarSet )
38 import {-# SOURCE #-} TcInstUtil ( emptyInstEnv )
39 import Panic            ( panic )
40 \end{code}
41
42 %************************************************************************
43 %*                                                                      *
44 \subsection{Symbol tables and Module details}
45 %*                                                                      *
46 %************************************************************************
47
48 A @ModDetails@ summarises everything we know about a compiled module.
49
50 \begin{code}
51 data ModDetails
52    = ModDetails {
53         moduleId      :: Module,
54         moduleExports :: Avails,                -- What it exports
55         moduleEnv     :: GlobalRdrEnv,          -- Its top level environment
56
57         fixityEnv     :: NameEnv Fixity,
58         deprecEnv     :: NameEnv DeprecTxt,
59         typeEnv       :: TypeEnv,
60
61         instEnv       :: InstEnv,
62         ruleEnv       :: RuleEnv                -- Domain may include Id from other modules
63      }
64
65 emptyModDetails :: Module -> ModDetails
66 emptyModDetails mod
67   = ModDetails { moduleId      = mod,
68                  moduleExports = [],
69                  moduleEnv     = emptyRdrEnv,
70                  fixityEnv     = emptyNameEnv,
71                  deprecEnv     = emptyNameEnv,
72                  typeEnv       = emptyNameEnv,
73                  instEnv       = emptyInstEnv,
74                  ruleEnv       = emptyRuleEnv
75     }           
76 emptyRuleEnv = panic "emptyRuleEnv"
77 \end{code}
78
79 Symbol tables map modules to ModDetails:
80
81 \begin{code}
82 type SymbolTable        = ModuleEnv ModDetails
83 type HomeSymbolTable    = SymbolTable   -- Domain = modules in the home package
84 type PackageSymbolTable = SymbolTable   -- Domain = modules in the some other package
85 type GlobalSymbolTable  = SymbolTable   -- Domain = all modules
86 \end{code}
87
88 Simple lookups in the symbol table.
89
90 \begin{code}
91 lookupFixityEnv :: SymbolTable -> Name -> Fixity
92         -- Returns defaultFixity if there isn't an explicit fixity
93 lookupFixityEnv tbl name
94   = case lookupModuleEnv tbl (nameModule name) of
95         Nothing      -> defaultFixity
96         Just details -> case lookupNameEnv (fixityEnv details) name of
97                                 Just fixity -> fixity
98                                 Nothing     -> defaultFixity
99 \end{code}
100
101
102 %************************************************************************
103 %*                                                                      *
104 \subsection{Type environment stuff}
105 %*                                                                      *
106 %************************************************************************
107
108 \begin{code}
109 type TypeEnv = NameEnv TyThing
110
111 data TyThing = AnId   Id
112              | ATyCon TyCon
113              | AClass Class
114
115 instance NamedThing TyThing where
116   getName (AnId id)   = getName id
117   getName (ATyCon tc) = getName tc
118   getName (AClass cl) = getName cl
119 \end{code}
120
121
122 \begin{code}
123 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
124 lookupTypeEnv tbl name
125   = case lookupModuleEnv tbl (nameModule name) of
126         Just details -> lookupNameEnv (typeEnv details) name
127         Nothing      -> Nothing
128
129
130 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
131   -- Finite map because we want the range too
132 groupTyThings things
133   = foldl add emptyFM things
134   where
135     add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
136     add tbl thing = addToFM tbl mod new_env
137                   where
138                     name    = getName thing
139                     mod     = nameModule name
140                     new_env = case lookupFM tbl mod of
141                                 Nothing  -> unitNameEnv name thing
142                                 Just env -> extendNameEnv env name thing
143                 
144 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
145 extendTypeEnv tbl things
146   = foldFM add tbl things
147   where
148     add mod type_env tbl
149         = panic "extendTypeEnv" --extendModuleEnv mod new_details
150         where
151           new_details 
152              = case lookupModuleEnv tbl mod of
153                   Nothing      -> (emptyModDetails mod) {typeEnv = type_env}
154                   Just details -> details {typeEnv = typeEnv details 
155                                                      `plusNameEnv` type_env}
156 \end{code}
157
158
159 %************************************************************************
160 %*                                                                      *
161 \subsection{Auxiliary types}
162 %*                                                                      *
163 %************************************************************************
164
165 These types are defined here because they are mentioned in ModDetails,
166 but they are mostly elaborated elsewhere
167
168 \begin{code}
169 type DeprecationEnv = NameEnv DeprecTxt         -- Give reason for deprecation
170
171 type GlobalRdrEnv = RdrNameEnv [Name]   -- The list is because there may be name clashes
172                                         -- These only get reported on lookup,
173                                         -- not on construction
174
175 type InstEnv    = UniqFM ClsInstEnv             -- Maps Class to instances for that class
176 type ClsInstEnv = [(TyVarSet, [Type], Id)]      -- The instances for a particular class
177
178 type RuleEnv    = IdEnv [CoreRule]
179 \end{code}
180
181
182 \begin{code}
183 type Avails       = [AvailInfo]
184 type AvailInfo    = GenAvailInfo Name
185 type RdrAvailInfo = GenAvailInfo OccName
186
187 data GenAvailInfo name  = Avail name     -- An ordinary identifier
188                         | AvailTC name   -- The name of the type or class
189                                   [name] -- The available pieces of type/class.
190                                          -- NB: If the type or class is itself
191                                          -- to be in scope, it must be in this list.
192                                          -- Thus, typically: AvailTC Eq [Eq, ==, /=]
193                         deriving( Eq )
194                         -- Equality used when deciding if the interface has changed
195
196 type AvailEnv     = NameEnv AvailInfo   -- Maps a Name to the AvailInfo that contains it
197 \end{code}
198
199
200 %************************************************************************
201 %*                                                                      *
202 \subsection{ModIface}
203 %*                                                                      *
204 %************************************************************************
205
206 \begin{code}
207 -- ModIFace is nearly the same as RnMonad.ParsedIface.
208 -- Right now it's identical :)
209 data ModIFace 
210    = ModIFace {
211         mi_mod       :: Module,                   -- Complete with package info
212         mi_vers      :: Version,                  -- Module version number
213         mi_orphan    :: WhetherHasOrphans,        -- Whether this module has orphans
214         mi_usages    :: [ImportVersion OccName],  -- Usages
215         mi_exports   :: [ExportItem],             -- Exports
216         mi_insts     :: [RdrNameInstDecl],        -- Local instance declarations
217         mi_decls     :: [(Version, RdrNameHsDecl)],    -- Local definitions
218         mi_fixity    :: (Version, [RdrNameFixitySig]), -- Local fixity declarations, 
219                                                        -- with their version
220         mi_rules     :: (Version, [RdrNameRuleDecl]),  -- Rules, with their version
221         mi_deprecs   :: [RdrNameDeprecation]           -- Deprecations
222      }
223
224 type ExportItem          = (ModuleName, [RdrAvailInfo])
225
226 type ImportVersion name  = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
227
228 type ModVersionInfo     = (Version,             -- Version of the whole module
229                            Version,             -- Version number for all fixity decls together
230                            Version)             -- ...ditto all rules together
231
232 type WhetherHasOrphans   = Bool
233         -- An "orphan" is 
234         --      * an instance decl in a module other than the defn module for 
235         --              one of the tycons or classes in the instance head
236         --      * a transformation rule in a module other than the one defining
237         --              the function in the head of the rule.
238
239 type IsBootInterface     = Bool
240
241 data WhatsImported name  = NothingAtAll                         -- The module is below us in the
242                                                                 -- hierarchy, but we import nothing
243
244                          | Everything Version                   -- The module version
245
246                          | Specifically Version                 -- Module version
247                                         Version                 -- Fixity version
248                                         Version                 -- Rules version
249                                         [(name,Version)]        -- List guaranteed non-empty
250                          deriving( Eq )
251         -- 'Specifically' doesn't let you say "I imported f but none of the fixities in
252         -- the module". If you use anything in the module you get its fixity and rule version
253         -- So if the fixities or rules change, you'll recompile, even if you don't use either.
254         -- This is easy to implement, and it's safer: you might not have used the rules last
255         -- time round, but if someone has added a new rule you might need it this time
256
257         -- 'Everything' means there was a "module M" in 
258         -- this module's export list, so we just have to go by M's version,
259         -- not the list of (name,version) pairs
260
261 \end{code}
262
263
264 %************************************************************************
265 %*                                                                      *
266 \subsection{The persistent compiler state}
267 %*                                                                      *
268 %************************************************************************
269
270 \begin{code}
271 data PersistentCompilerState 
272    = PCS {
273         pcsPST :: PackageSymbolTable,           -- Domain = non-home-package modules
274                                                 --   except that the InstEnv components is empty
275         pcsInsts :: InstEnv,                    -- The total InstEnv accumulated from all
276                                                 --   the non-home-package modules
277         pcsRules :: RuleEnv,                    -- Ditto RuleEnv
278
279         pcsPRS :: PersistentRenamerState
280      }
281 \end{code}
282
283 The @PersistentRenamerState@ persists across successive calls to the
284 compiler.
285
286 It contains:
287   * A name supply, which deals with allocating unique names to
288     (Module,OccName) original names, 
289  
290   * An accumulated InstEnv from all the modules in pcsPST
291     The point is that we don't want to keep recreating it whenever
292     we compile a new module.  The InstEnv component of pcPST is empty.
293     (This means we might "see" instances that we shouldn't "really" see;
294     but the Haskell Report is vague on what is meant to be visible, 
295     so we just take the easy road here.)
296
297   * Ditto for rules
298
299   * A "holding pen" for declarations that have been read out of
300     interface files but not yet sucked in, renamed, and typechecked
301
302 \begin{code}
303 data PersistentRenamerState
304   = PRS { prsOrig  :: OrigNameEnv,
305           prsDecls :: DeclsMap,
306           prsInsts :: IfaceInsts,
307           prsRules :: IfaceRules
308     }
309
310 data OrigNameEnv
311  = Orig { origNames  :: FiniteMap (Module,OccName) Name, -- Ensures that one original name gets one unique
312           origIParam :: FiniteMap OccName Name           -- Ensures that one implicit parameter name gets one unique
313    }
314
315 type DeclsMap = NameEnv (Version, AvailInfo, Bool, (Module, RdrNameHsDecl))
316                 -- A DeclsMap contains a binding for each Name in the declaration
317                 -- including the constructors of a type decl etc.
318                 -- The Bool is True just for the 'main' Name.
319
320 type IfaceInsts = Bag GatedDecl
321 type IfaceRules = Bag GatedDecl
322
323 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
324 \end{code}
325
326
327 %************************************************************************
328 %*                                                                      *
329 \subsection{The result of compiling one module}
330 %*                                                                      *
331 %************************************************************************
332
333 \begin{code}
334 data CompResult
335    = CompOK   ModDetails  -- new details (HST additions)
336               (Maybe (ModIFace, Linkable))
337                        -- summary and code; Nothing => compilation not reqd
338                        -- (old summary and code are still valid)
339               PersistentCompilerState   -- updated PCS
340               (Bag WarnMsg)             -- warnings
341
342    | CompErrs PersistentCompilerState   -- updated PCS
343               (Bag ErrMsg)              -- errors
344               (Bag WarnMsg)             -- warnings
345
346
347 -- The driver sits between 'compile' and 'hscMain', translating calls
348 -- to the former into calls to the latter, and results from the latter
349 -- into results from the former.  It does things like preprocessing
350 -- the .hs file if necessary, and compiling up the .stub_c files to
351 -- generate Linkables.
352
353 data HscResult
354    = HscOK   ModDetails                 -- new details (HomeSymbolTable additions)
355              (Maybe ModIFace)           -- new iface (if any compilation was done)
356              (Maybe String)             -- generated stub_h
357              (Maybe String)             -- generated stub_c
358              PersistentCompilerState    -- updated PCS
359              [SDoc]                     -- warnings
360
361    | HscErrs PersistentCompilerState    -- updated PCS
362              [SDoc]                     -- errors
363              [SDoc]                     -- warnings
364
365         
366 -- These two are only here to avoid recursion between CmCompile and
367 -- CompManager.  They really ought to be in the latter.
368 type ModuleEnv a = UniqFM a   -- Domain is Module
369
370 type HomeModMap         = FiniteMap ModuleName Module -- domain: home mods only
371 type HomeInterfaceTable = ModuleEnv ModIFace
372 \end{code}
373
374