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