2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
7 module HscTypes ( TyThing(..), GlobalSymbolTable, OrigNameEnv, AvailEnv,
8 WhetherHasOrphans, ImportVersion, ExportItem,
9 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
10 IfaceInsts, IfaceRules, DeprecationEnv )
13 #include "HsVersions.h"
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 )
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 )
40 import VarSet ( TyVarSet )
41 import {-# SOURCE #-} TcInstUtil ( emptyInstEnv )
42 import Panic ( panic )
45 %************************************************************************
47 \subsection{Symbol tables and Module details}
49 %************************************************************************
51 A @ModDetails@ summarises everything we know about a compiled module.
57 moduleExports :: Avails, -- What it exports
58 moduleEnv :: GlobalRdrEnv, -- Its top level environment
60 fixityEnv :: NameEnv Fixity,
61 deprecEnv :: NameEnv DeprecTxt,
65 ruleEnv :: RuleEnv -- Domain may include Id from other modules
68 emptyModDetails :: Module -> ModDetails
70 = ModDetails { moduleId = mod,
72 moduleEnv = emptyRdrEnv,
73 fixityEnv = emptyNameEnv,
74 deprecEnv = emptyNameEnv,
75 typeEnv = emptyNameEnv,
76 instEnv = emptyInstEnv,
77 ruleEnv = emptyRuleEnv
79 emptyRuleEnv = panic "emptyRuleEnv"
82 Symbol tables map modules to ModDetails:
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
91 Simple lookups in the symbol table.
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
105 %************************************************************************
107 \subsection{Type environment stuff}
109 %************************************************************************
112 type TypeEnv = NameEnv TyThing
114 data TyThing = AnId Id
118 instance NamedThing TyThing where
119 getName (AnId id) = getName id
120 getName (ATyCon tc) = getName tc
121 getName (AClass cl) = getName cl
126 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
127 lookupTypeEnv tbl name
128 = case lookupModuleEnv tbl (nameModule name) of
129 Just details -> lookupNameEnv (typeEnv details) name
133 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
134 -- Finite map because we want the range too
136 = foldl add emptyFM things
138 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
139 add tbl thing = addToFM tbl mod new_env
142 mod = nameModule name
143 new_env = case lookupFM tbl mod of
144 Nothing -> unitNameEnv name thing
145 Just env -> extendNameEnv env name thing
147 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
148 extendTypeEnv tbl things
149 = foldFM add tbl things
152 = panic "extendTypeEnv" --extendModuleEnv mod 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}
162 %************************************************************************
164 \subsection{Auxiliary types}
166 %************************************************************************
168 These types are defined here because they are mentioned in ModDetails,
169 but they are mostly elaborated elsewhere
172 type DeprecationEnv = NameEnv DeprecTxt -- Give reason for deprecation
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
178 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
179 type ClsInstEnv = [(TyVarSet, [Type], Id)] -- The instances for a particular class
181 type RuleEnv = IdEnv [CoreRule]
186 type Avails = [AvailInfo]
187 type AvailInfo = GenAvailInfo Name
188 type RdrAvailInfo = GenAvailInfo OccName
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, ==, /=]
197 -- Equality used when deciding if the interface has changed
199 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
203 %************************************************************************
205 \subsection{ModIface}
207 %************************************************************************
210 -- ModIFace is nearly the same as RnMonad.ParsedIface.
211 -- Right now it's identical :)
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
227 type ExportItem = (ModuleName, [RdrAvailInfo])
229 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
231 type ModVersionInfo = (Version, -- Version of the whole module
232 Version, -- Version number for all fixity decls together
233 Version) -- ...ditto all rules together
235 type WhetherHasOrphans = Bool
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.
242 type IsBootInterface = Bool
244 data WhatsImported name = NothingAtAll -- The module is below us in the
245 -- hierarchy, but we import nothing
247 | Everything Version -- The module version
249 | Specifically Version -- Module version
250 Version -- Fixity version
251 Version -- Rules version
252 [(name,Version)] -- List guaranteed non-empty
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
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
267 %************************************************************************
269 \subsection{The persistent compiler state}
271 %************************************************************************
274 data PersistentCompilerState
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
282 pcsPRS :: PersistentRenamerState
286 The @PersistentRenamerState@ persists across successive calls to the
290 * A name supply, which deals with allocating unique names to
291 (Module,OccName) original names,
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.)
302 * A "holding pen" for declarations that have been read out of
303 interface files but not yet sucked in, renamed, and typechecked
306 data PersistentRenamerState
307 = PRS { prsOrig :: OrigNameEnv,
308 prsDecls :: DeclsMap,
309 prsInsts :: IfaceInsts,
310 prsRules :: IfaceRules
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
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.
323 type IfaceInsts = Bag GatedDecl
324 type IfaceRules = Bag GatedDecl
326 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
330 %************************************************************************
332 \subsection{The result of compiling one module}
334 %************************************************************************
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
345 | CompErrs PersistentCompilerState -- updated PCS
346 (Bag ErrMsg) -- errors
347 (Bag WarnMsg) -- warnings
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.
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
364 | HscErrs PersistentCompilerState -- updated PCS
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
373 type HomeModMap = FiniteMap ModuleName Module -- domain: home mods only
374 type HomeInterfaceTable = ModuleEnv ModIFace