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,
67 ruleEnv :: 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,
78 instEnv = emptyInstEnv,
79 ruleEnv = emptyRuleEnv
81 emptyRuleEnv = panic "emptyRuleEnv"
84 Symbol tables map modules to ModDetails:
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
93 Simple lookups in the symbol table.
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
107 %************************************************************************
109 \subsection{Type environment stuff}
111 %************************************************************************
114 type TypeEnv = NameEnv TyThing
116 data TyThing = AnId Id
120 instance NamedThing TyThing where
121 getName (AnId id) = getName id
122 getName (ATyCon tc) = getName tc
123 getName (AClass cl) = getName cl
128 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
129 lookupTypeEnv tbl name
130 = case lookupModuleEnv tbl (nameModule name) of
131 Just details -> lookupNameEnv (typeEnv details) name
135 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
136 -- Finite map because we want the range too
138 = foldl add emptyFM things
140 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
141 add tbl thing = addToFM tbl mod new_env
144 mod = nameModule name
145 new_env = case lookupFM tbl mod of
146 Nothing -> unitNameEnv name thing
147 Just env -> extendNameEnv env name thing
149 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
150 extendTypeEnv tbl things
151 = foldFM add tbl things
154 = panic "extendTypeEnv" --extendModuleEnv mod 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}
164 %************************************************************************
166 \subsection{Auxiliary types}
168 %************************************************************************
170 These types are defined here because they are mentioned in ModDetails,
171 but they are mostly elaborated elsewhere
174 type DeprecationEnv = NameEnv DeprecTxt -- Give reason for deprecation
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
180 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
181 type ClsInstEnv = [(TyVarSet, [Type], Id)] -- The instances for a particular class
183 type RuleEnv = IdEnv [CoreRule]
188 type Avails = [AvailInfo]
189 type AvailInfo = GenAvailInfo Name
190 type RdrAvailInfo = GenAvailInfo OccName
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, ==, /=]
199 -- Equality used when deciding if the interface has changed
201 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
205 %************************************************************************
207 \subsection{ModIface}
209 %************************************************************************
212 -- ModIFace is nearly the same as RnMonad.ParsedIface.
213 -- Right now it's identical :)
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
229 type ExportItem = (ModuleName, [RdrAvailInfo])
231 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
233 type ModVersionInfo = (Version, -- Version of the whole module
234 Version, -- Version number for all fixity decls together
235 Version) -- ...ditto all rules together
237 type WhetherHasOrphans = Bool
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.
244 type IsBootInterface = Bool
246 data WhatsImported name = NothingAtAll -- The module is below us in the
247 -- hierarchy, but we import nothing
249 | Everything Version -- The module version
251 | Specifically Version -- Module version
252 Version -- Fixity version
253 Version -- Rules version
254 [(name,Version)] -- List guaranteed non-empty
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
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
269 %************************************************************************
271 \subsection{The persistent compiler state}
273 %************************************************************************
276 data PersistentCompilerState
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
284 pcsPRS :: PersistentRenamerState
288 The @PersistentRenamerState@ persists across successive calls to the
292 * A name supply, which deals with allocating unique names to
293 (Module,OccName) original names,
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.)
304 * A "holding pen" for declarations that have been read out of
305 interface files but not yet sucked in, renamed, and typechecked
308 data PersistentRenamerState
309 = PRS { prsOrig :: OrigNameEnv,
310 prsDecls :: DeclsMap,
311 prsInsts :: IfaceInsts,
312 prsRules :: IfaceRules
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
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.
325 type IfaceInsts = Bag GatedDecl
326 type IfaceRules = Bag GatedDecl
328 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
332 %************************************************************************
334 \subsection{The result of compiling one module}
336 %************************************************************************
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
347 | CompErrs PersistentCompilerState -- updated PCS
348 (Bag ErrMsg) -- errors
349 (Bag WarnMsg) -- warnings
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.
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
366 | HscErrs PersistentCompilerState -- updated PCS
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
375 type HomeModMap = FiniteMap ModuleName Module -- domain: home mods only
376 type HomeInterfaceTable = ModuleEnv ModIFace