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