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 )
13 import Module ( Module, ModuleName )
14 import Class ( Class )
15 import OccName ( OccName )
16 import RdrName ( RdrNameEnv )
17 import Outputable ( SDoc )
18 import UniqFM ( UniqFM )
19 import FiniteMap ( FiniteMap )
22 import VarEnv ( IdEnv )
23 import BasicTypes ( Version, Fixity )
24 import TyCon ( TyCon )
25 import ErrUtils ( ErrMsg, WarnMsg )
26 import CmLink ( Linkable )
27 import RdrHsSyn ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameHsDecl,
28 RdrNameDeprecation, RdrNameFixitySig )
29 import UniqSupply ( UniqSupply )
30 import HsDecls ( DeprecTxt )
31 import CoreSyn ( CoreRule )
32 import RnMonad ( ImportVersion, ExportItem, WhetherHasOrphans )
33 import NameSet ( NameSet )
37 %************************************************************************
39 \subsection{Symbol tables and Module details}
41 %************************************************************************
43 A @ModDetails@ summarises everything we know about a compiled module.
49 moduleExports :: Avails, -- What it exports
50 moduleEnv :: GlobalRdrEnv, -- Its top level environment
52 fixityEnv :: NameEnv Fixity,
53 deprecEnv :: NameEnv DeprecTxt,
57 ruleEnv :: RuleEnv -- Domain may include Id from other modules
60 emptyModDetails :: Module -> ModuleDetails
62 = ModDetails { moduleId = mod,
64 moduleEnv = emptyRdrEnv,
65 fixityEnv = emptyNameEnv,
66 deprecEnv = emptyNameEnv,
67 typeEnv = emptyNameEnv,
68 instEnv = emptyInstEnv,
69 ruleEnv = emptyRuleEnv
73 Symbol tables map modules to ModDetails:
76 type SymbolTable = ModuleEnv ModDetails
77 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
78 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
79 type GlobalSymbolTable = SymbolTable -- Domain = all modules
82 Simple lookups in the symbol table.
85 lookupFixityEnv :: SymbolTable -> Name -> Fixity
86 -- Returns defaultFixity if there isn't an explicit fixity
87 lookupFixityEnv tbl name
88 = case lookupModuleEnv tbl (nameModule name) of
89 Nothing -> defaultFixity
90 Just details -> case lookupNameEnv (fixityEnv details) name of
92 Nothing -> defaultFixity
96 %************************************************************************
98 \subsection{Type environment stuff}
100 %************************************************************************
103 type TypeEnv = NameEnv TyThing
105 data TyThing = AnId Id
109 instance NamedThing TyThing where
110 getName (AnId id) = getName id
111 getName (ATyCon tc) = getName tc
112 getName (AClass cl) = getName cl
117 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
118 lookupTypeEnv tbl name
119 = case lookupModuleEnv tbl (nameModule name) of
120 Just details -> lookupNameEnv (typeEnv details) name
124 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
125 -- Finite map because we want the range too
127 = foldl add emptyFM things
129 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
130 add tbl thing = addToFM tbl mod new_env
133 mod = nameModule name
134 new_env = case lookupFM tbl mod of
135 Nothing -> unitNameEnv name thing
136 Just env -> extendNameEnv env name thing
138 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
139 extendTypeEnv tbl things
140 = foldFM add tbl things
143 = extendModuleEnv mod new_details
145 new_details = case lookupModuleEnv tbl mod of
146 Nothing -> emptyModDetails mod {typeEnv = type_env}
147 Just details -> details {typeEnv = typeEnv details `plusNameEnv` type_env})
151 %************************************************************************
153 \subsection{Auxiliary types}
155 %************************************************************************
157 These types are defined here because they are mentioned in ModDetails,
158 but they are mostly elaborated elsewhere
161 type DeprecationEnv = NameEnv DeprecTxt -- Give reason for deprecation
163 type GlobalRdrEnv = RdrNameEnv [Name] -- The list is because there may be name clashes
164 -- These only get reported on lookup,
165 -- not on construction
167 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
168 type ClsInstEnv = [(TyVarSet, [Type], Id)] -- The instances for a particular class
170 type RuleEnv = IdEnv [CoreRule]
175 type Avails = [AvailInfo]
176 type AvailInfo = GenAvailInfo Name
177 type RdrAvailInfo = GenAvailInfo OccName
179 data GenAvailInfo name = Avail name -- An ordinary identifier
180 | AvailTC name -- The name of the type or class
181 [name] -- The available pieces of type/class.
182 -- NB: If the type or class is itself
183 -- to be in scope, it must be in this list.
184 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
186 -- Equality used when deciding if the interface has changed
188 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
192 %************************************************************************
194 \subsection{ModIface}
196 %************************************************************************
199 -- ModIFace is nearly the same as RnMonad.ParsedIface.
200 -- Right now it's identical :)
203 mi_mod :: Module, -- Complete with package info
204 mi_vers :: Version, -- Module version number
205 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
206 mi_usages :: [ImportVersion OccName], -- Usages
207 mi_exports :: [ExportItem], -- Exports
208 mi_insts :: [RdrNameInstDecl], -- Local instance declarations
209 mi_decls :: [(Version, RdrNameHsDecl)], -- Local definitions
210 mi_fixity :: (Version, [RdrNameFixitySig]), -- Local fixity declarations,
211 -- with their version
212 mi_rules :: (Version, [RdrNameRuleDecl]), -- Rules, with their version
213 mi_deprecs :: [RdrNameDeprecation] -- Deprecations
218 %************************************************************************
220 \subsection{The persistent compiler state}
222 %************************************************************************
225 data PersistentCompilerState
227 pcsPST :: PackageSymbolTable, -- Domain = non-home-package modules
228 -- except that the InstEnv components is empty
229 pcsInsts :: InstEnv -- The total InstEnv accumulated from all
230 -- the non-home-package modules
231 pcsRules :: RuleEnv -- Ditto RuleEnv
233 pcsPRS :: PersistentRenamerState
237 The @PersistentRenamerState@ persists across successive calls to the
241 * A name supply, which deals with allocating unique names to
242 (Module,OccName) original names,
244 * An accumulated InstEnv from all the modules in pcsPST
245 The point is that we don't want to keep recreating it whenever
246 we compile a new module. The InstEnv component of pcPST is empty.
247 (This means we might "see" instances that we shouldn't "really" see;
248 but the Haskell Report is vague on what is meant to be visible,
249 so we just take the easy road here.)
253 * A "holding pen" for declarations that have been read out of
254 interface files but not yet sucked in, renamed, and typechecked
257 data PersistentRenamerState
258 = PRS { prsOrig :: OrigNameEnv,
259 prsDecls :: DeclsMap,
260 prsInsts :: IfaceInsts,
261 prsRules :: IfaceRules
266 = NS { nsUniqs :: UniqSupply,
267 nsNames :: FiniteMap (Module,OccName) Name, -- Ensures that one original name gets one unique
268 nsIParam :: FiniteMap OccName Name -- Ensures that one implicit parameter name gets one unique
271 = Orig { origNames :: FiniteMap (Module,OccName) Name -- Ensures that one original name gets one unique
272 origIParam :: FiniteMap OccName Name -- Ensures that one implicit parameter name gets one unique
276 type DeclsMap = NameEnv (Version, AvailInfo, Bool, (Module, RdrNameHsDecl))
277 -- A DeclsMap contains a binding for each Name in the declaration
278 -- including the constructors of a type decl etc.
279 -- The Bool is True just for the 'main' Name.
281 type IfaceInsts = Bag GatedDecl
282 type IfaceRules = Bag GatedDecl
284 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
288 %************************************************************************
290 \subsection{The result of compiling one module}
292 %************************************************************************
296 = CompOK ModDetails -- new details (HST additions)
297 (Maybe (ModIFace, Linkable))
298 -- summary and code; Nothing => compilation not reqd
299 -- (old summary and code are still valid)
300 PersistentCompilerState -- updated PCS
301 (Bag WarnMsg) -- warnings
303 | CompErrs PersistentCompilerState -- updated PCS
304 (Bag ErrMsg) -- errors
305 (Bag WarnMsg) -- warnings
308 -- The driver sits between 'compile' and 'hscMain', translating calls
309 -- to the former into calls to the latter, and results from the latter
310 -- into results from the former. It does things like preprocessing
311 -- the .hs file if necessary, and compiling up the .stub_c files to
312 -- generate Linkables.
315 = HscOK ModDetails -- new details (HomeSymbolTable additions)
316 Maybe ModIFace -- new iface (if any compilation was done)
317 Maybe String -- generated stub_h
318 Maybe String -- generated stub_c
319 PersistentCompilerState -- updated PCS
322 | HscErrs PersistentCompilerState -- updated PCS
327 -- These two are only here to avoid recursion between CmCompile and
328 -- CompManager. They really ought to be in the latter.
329 type ModuleEnv a = UniqFM a -- Domain is Module
331 type HomeModMap = FiniteMap ModuleName Module -- domain: home mods only
332 type HomeInterfaceTable = ModuleEnv ModIFace