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