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