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, ModDetails(..),
11 InstEnv, lookupTypeEnv )
14 #include "HsVersions.h"
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 )
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 )
41 import VarSet ( TyVarSet )
43 TcInstUtil ( emptyInstEnv )
44 import Panic ( panic )
47 %************************************************************************
49 \subsection{Symbol tables and Module details}
51 %************************************************************************
53 A @ModDetails@ summarises everything we know about a compiled module.
59 moduleExports :: Avails, -- What it exports
60 moduleEnv :: GlobalRdrEnv, -- Its top level environment
62 fixityEnv :: NameEnv Fixity,
63 deprecEnv :: NameEnv DeprecTxt,
66 mdInsts :: [DFunId], -- Dfun-ids for the instances in this module
67 mdRules :: RuleEnv -- Domain may include Id from other modules
70 emptyModDetails :: Module -> ModDetails
72 = ModDetails { moduleId = mod,
74 moduleEnv = emptyRdrEnv,
75 fixityEnv = emptyNameEnv,
76 deprecEnv = emptyNameEnv,
77 typeEnv = emptyNameEnv,
79 mdRules = emptyRuleEnv
83 Symbol tables map modules to ModDetails:
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
92 Simple lookups in the symbol table.
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
106 %************************************************************************
108 \subsection{Type environment stuff}
110 %************************************************************************
113 type TypeEnv = NameEnv TyThing
115 data TyThing = AnId Id
119 instance NamedThing TyThing where
120 getName (AnId id) = getName id
121 getName (ATyCon tc) = getName tc
122 getName (AClass cl) = getName cl
127 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
128 lookupTypeEnv tbl name
129 = case lookupModuleEnv tbl (nameModule name) of
130 Just details -> lookupNameEnv (typeEnv details) name
134 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
135 -- Finite map because we want the range too
137 = foldl add emptyFM things
139 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
140 add tbl thing = addToFM tbl mod new_env
143 mod = nameModule name
144 new_env = case lookupFM tbl mod of
145 Nothing -> unitNameEnv name thing
146 Just env -> extendNameEnv env name thing
148 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
149 extendTypeEnv tbl things
150 = foldFM add tbl things
153 = panic "extendTypeEnv" --extendModuleEnv mod 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}
163 %************************************************************************
165 \subsection{Auxiliary types}
167 %************************************************************************
169 These types are defined here because they are mentioned in ModDetails,
170 but they are mostly elaborated elsewhere
173 type DeprecationEnv = NameEnv DeprecTxt -- Give reason for deprecation
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
179 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
180 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
183 type RuleEnv = IdEnv [CoreRule]
185 emptyRuleEnv = emptyVarEnv
190 type Avails = [AvailInfo]
191 type AvailInfo = GenAvailInfo Name
192 type RdrAvailInfo = GenAvailInfo OccName
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, ==, /=]
201 -- Equality used when deciding if the interface has changed
203 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
207 %************************************************************************
209 \subsection{ModIface}
211 %************************************************************************
214 -- ModIFace is nearly the same as RnMonad.ParsedIface.
215 -- Right now it's identical :)
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
231 type ExportItem = (ModuleName, [RdrAvailInfo])
233 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
235 type ModVersionInfo = (Version, -- Version of the whole module
236 Version, -- Version number for all fixity decls together
237 Version) -- ...ditto all rules together
239 type WhetherHasOrphans = Bool
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.
246 type IsBootInterface = Bool
248 data WhatsImported name = NothingAtAll -- The module is below us in the
249 -- hierarchy, but we import nothing
251 | Everything Version -- The module version
253 | Specifically Version -- Module version
254 Version -- Fixity version
255 Version -- Rules version
256 [(name,Version)] -- List guaranteed non-empty
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
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
271 %************************************************************************
273 \subsection{The persistent compiler state}
275 %************************************************************************
278 data PersistentCompilerState
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
286 pcsPRS :: PersistentRenamerState
290 The @PersistentRenamerState@ persists across successive calls to the
294 * A name supply, which deals with allocating unique names to
295 (Module,OccName) original names,
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.)
306 * A "holding pen" for declarations that have been read out of
307 interface files but not yet sucked in, renamed, and typechecked
310 data PersistentRenamerState
311 = PRS { prsOrig :: OrigNameEnv,
312 prsDecls :: DeclsMap,
313 prsInsts :: IfaceInsts,
314 prsRules :: IfaceRules
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
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.
327 type IfaceInsts = Bag GatedDecl
328 type IfaceRules = Bag GatedDecl
330 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
334 %************************************************************************
336 \subsection{The result of compiling one module}
338 %************************************************************************
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
349 | CompErrs PersistentCompilerState -- updated PCS
350 (Bag ErrMsg) -- errors
351 (Bag WarnMsg) -- warnings
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.
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
368 | HscErrs PersistentCompilerState -- updated PCS
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
377 type HomeModMap = FiniteMap ModuleName Module -- domain: home mods only
378 type HomeInterfaceTable = ModuleEnv ModIFace