2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
8 ModDetails(..), GlobalSymbolTable,
9 HomeSymbolTable, PackageSymbolTable,
11 TyThing(..), lookupTypeEnv,
13 WhetherHasOrphans, ImportVersion, ExportItem,
14 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
15 IfaceInsts, IfaceRules, DeprecationEnv, OrigNameEnv,
16 AvailEnv, AvailInfo, GenAvailInfo(..),
17 PersistentCompilerState(..),
19 InstEnv, ClsInstEnv, DFunId,
21 GlobalRdrEnv, RdrAvailInfo,
23 CompResult(..), HscResult(..),
26 Provenance(..), ImportReason(..), PrintUnqualified,
27 pprNameProvenance, hasBetterProv
31 #include "HsVersions.h"
33 import Name ( Name, NameEnv, NamedThing,
34 unitNameEnv, extendNameEnv, plusNameEnv,
35 lookupNameEnv, emptyNameEnv, getName, nameModule,
37 import Module ( Module, ModuleName,
38 extendModuleEnv, lookupModuleEnv )
39 import Class ( Class )
40 import OccName ( OccName )
41 import RdrName ( RdrNameEnv, emptyRdrEnv )
42 import Outputable ( SDoc )
43 import UniqFM ( UniqFM )
44 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
47 import VarEnv ( IdEnv, emptyVarEnv )
48 import BasicTypes ( Version, Fixity, defaultFixity )
49 import TyCon ( TyCon )
50 import ErrUtils ( ErrMsg, WarnMsg )
51 import CmLink ( Linkable )
52 import RdrHsSyn ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameHsDecl,
53 RdrNameDeprecation, RdrNameFixitySig )
54 import InterpSyn ( UnlinkedIBind )
55 import UniqSupply ( UniqSupply )
56 import HsDecls ( DeprecTxt )
57 import CoreSyn ( CoreRule )
58 import NameSet ( NameSet )
60 import VarSet ( TyVarSet )
61 import Panic ( panic )
63 import SrcLoc ( SrcLoc, isGoodSrcLoc )
66 %************************************************************************
68 \subsection{Symbol tables and Module details}
70 %************************************************************************
72 A @ModIface@ plus a @ModDetails@ summarises everything we know
73 about a compiled module. The @ModIface@ is the stuff *before* linking,
74 and can be written out to an interface file. The @ModDetails@ is after
75 linking; it is the "linked" form of the mi_decls field.
80 md_module :: Module, -- Complete with package info
81 md_version :: VersionInfo, -- Module version number
82 md_orphan :: WhetherHasOrphans, -- Whether this module has orphans
83 md_usages :: [ImportVersion Name], -- Usages
85 md_exports :: Avails, -- What it exports
86 md_globals :: GlobalRdrEnv, -- Its top level environment
88 md_fixities :: NameEnv Fixity, -- Fixities
89 md_deprecs :: NameEnv DeprecTxt, -- Deprecations
91 -- The next three fields are created by the typechecker
93 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
94 md_rules :: RuleEnv -- Domain may include Ids from other modules
97 -- ModIFace is nearly the same as RnMonad.ParsedIface.
98 -- Right now it's identical :)
101 mi_mod :: Module, -- Complete with package info
102 mi_vers :: Version, -- Module version number
103 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
104 mi_usages :: [ImportVersion OccName], -- Usages
105 mi_exports :: [ExportItem], -- Exports
106 mi_insts :: [RdrNameInstDecl], -- Local instance declarations
107 mi_decls :: [(Version, RdrNameHsDecl)], -- Local definitions
108 mi_fixity :: (Version, [RdrNameFixitySig]), -- Local fixity declarations,
109 -- with their version
110 mi_rules :: (Version, [RdrNameRuleDecl]), -- Rules, with their version
111 mi_deprecs :: [RdrNameDeprecation] -- Deprecations
117 emptyModDetails :: Module -> ModDetails
119 = ModDetails { md_module = mod,
121 md_globals = emptyRdrEnv,
122 md_fixities = emptyNameEnv,
123 md_deprecs = emptyNameEnv,
124 md_types = emptyNameEnv,
126 md_rules = emptyRuleEnv
130 Symbol tables map modules to ModDetails:
133 type SymbolTable = ModuleEnv ModDetails
134 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
135 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
136 type GlobalSymbolTable = SymbolTable -- Domain = all modules
139 Simple lookups in the symbol table.
142 lookupFixityEnv :: SymbolTable -> Name -> Maybe Fixity
143 -- Returns defaultFixity if there isn't an explicit fixity
144 lookupFixityEnv tbl name
145 = case lookupModuleEnv tbl (nameModule name) of
147 Just details -> lookupNameEnv (md_fixities details) name
151 %************************************************************************
153 \subsection{Type environment stuff}
155 %************************************************************************
158 type TypeEnv = NameEnv TyThing
160 data TyThing = AnId Id
164 instance NamedThing TyThing where
165 getName (AnId id) = getName id
166 getName (ATyCon tc) = getName tc
167 getName (AClass cl) = getName cl
172 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
173 lookupTypeEnv tbl name
174 = case lookupModuleEnv tbl (nameModule name) of
175 Just details -> lookupNameEnv (md_types details) name
179 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
180 -- Finite map because we want the range too
182 = foldl add emptyFM things
184 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
185 add tbl thing = addToFM tbl mod new_env
188 mod = nameModule name
189 new_env = case lookupFM tbl mod of
190 Nothing -> unitNameEnv name thing
191 Just env -> extendNameEnv env name thing
193 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
194 extendTypeEnv tbl things
195 = foldFM add tbl things
198 = panic "extendTypeEnv" --extendModuleEnv mod new_details
201 = case lookupModuleEnv tbl mod of
202 Nothing -> (emptyModDetails mod) {md_types = type_env}
203 Just details -> details {md_types = md_types details
204 `plusNameEnv` type_env}
208 %************************************************************************
210 \subsection{Auxiliary types}
212 %************************************************************************
214 These types are defined here because they are mentioned in ModDetails,
215 but they are mostly elaborated elsewhere
223 declVers :: NameEnv Version
226 type DeprecationEnv = NameEnv DeprecTxt -- Give reason for deprecation
228 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
229 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
232 type RuleEnv = IdEnv [CoreRule]
234 emptyRuleEnv = emptyVarEnv
239 type Avails = [AvailInfo]
240 type AvailInfo = GenAvailInfo Name
241 type RdrAvailInfo = GenAvailInfo OccName
243 data GenAvailInfo name = Avail name -- An ordinary identifier
244 | AvailTC name -- The name of the type or class
245 [name] -- The available pieces of type/class.
246 -- NB: If the type or class is itself
247 -- to be in scope, it must be in this list.
248 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
250 -- Equality used when deciding if the interface has changed
252 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
256 %************************************************************************
258 \subsection{ModIface}
260 %************************************************************************
263 type ExportItem = (ModuleName, [RdrAvailInfo])
265 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
267 type ModVersionInfo = (Version, -- Version of the whole module
268 Version, -- Version number for all fixity decls together
269 Version) -- ...ditto all rules together
271 type WhetherHasOrphans = Bool
273 -- * an instance decl in a module other than the defn module for
274 -- one of the tycons or classes in the instance head
275 -- * a transformation rule in a module other than the one defining
276 -- the function in the head of the rule.
278 type IsBootInterface = Bool
280 data WhatsImported name = NothingAtAll -- The module is below us in the
281 -- hierarchy, but we import nothing
283 | Everything Version -- The module version
285 | Specifically Version -- Module version
286 Version -- Fixity version
287 Version -- Rules version
288 [(name,Version)] -- List guaranteed non-empty
290 -- 'Specifically' doesn't let you say "I imported f but none of the fixities in
291 -- the module". If you use anything in the module you get its fixity and rule version
292 -- So if the fixities or rules change, you'll recompile, even if you don't use either.
293 -- This is easy to implement, and it's safer: you might not have used the rules last
294 -- time round, but if someone has added a new rule you might need it this time
296 -- 'Everything' means there was a "module M" in
297 -- this module's export list, so we just have to go by M's version,
298 -- not the list of (name,version) pairs
302 %************************************************************************
304 \subsection{The persistent compiler state}
306 %************************************************************************
309 data PersistentCompilerState
311 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
312 -- except that the InstEnv components is empty
313 pcs_insts :: InstEnv, -- The total InstEnv accumulated from all
314 -- the non-home-package modules
315 pcs_rules :: RuleEnv, -- Ditto RuleEnv
317 pcs_PRS :: PersistentRenamerState
321 The @PersistentRenamerState@ persists across successive calls to the
325 * A name supply, which deals with allocating unique names to
326 (Module,OccName) original names,
328 * An accumulated InstEnv from all the modules in pcs_PST
329 The point is that we don't want to keep recreating it whenever
330 we compile a new module. The InstEnv component of pcPST is empty.
331 (This means we might "see" instances that we shouldn't "really" see;
332 but the Haskell Report is vague on what is meant to be visible,
333 so we just take the easy road here.)
337 * A "holding pen" for declarations that have been read out of
338 interface files but not yet sucked in, renamed, and typechecked
341 data PersistentRenamerState
342 = PRS { prsOrig :: OrigNameEnv,
343 prsDecls :: DeclsMap,
344 prsInsts :: IfaceInsts,
345 prsRules :: IfaceRules
349 The OrigNameEnv makes sure that there is just one Unique assigned for
350 each original name; i.e. (module-name, occ-name) pair. The Name is
351 always stored as a Global, and has the SrcLoc of its binding location.
352 Actually that's not quite right. When we first encounter the original
353 name, we might not be at its binding site (e.g. we are reading an
354 interface file); so we give it 'noSrcLoc' then. Later, when we find
355 its binding site, we fix it up.
357 Exactly the same is true of the Module stored in the Name. When we first
358 encounter the occurrence, we may not know the details of the module, so
359 we just store junk. Then when we find the binding site, we fix it up.
363 = Orig { origNames :: FiniteMap (ModuleName,OccName) Name, -- Ensures that one original name gets one unique
364 origIParam :: FiniteMap OccName Name -- Ensures that one implicit parameter name gets one unique
369 A DeclsMap contains a binding for each Name in the declaration
370 including the constructors of a type decl etc. The Bool is True just
374 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameHsDecl))
376 type IfaceInsts = Bag GatedDecl
377 type IfaceRules = Bag GatedDecl
379 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
383 %************************************************************************
385 \subsection{The result of compiling one module}
387 %************************************************************************
391 = CompOK ModDetails -- new details (HST additions)
392 (Maybe (ModIFace, Linkable))
393 -- summary and code; Nothing => compilation not reqd
394 -- (old summary and code are still valid)
395 PersistentCompilerState -- updated PCS
396 (Bag WarnMsg) -- warnings
398 | CompErrs PersistentCompilerState -- updated PCS
399 (Bag ErrMsg) -- errors
400 (Bag WarnMsg) -- warnings
403 -- The driver sits between 'compile' and 'hscMain', translating calls
404 -- to the former into calls to the latter, and results from the latter
405 -- into results from the former. It does things like preprocessing
406 -- the .hs file if necessary, and compiling up the .stub_c files to
407 -- generate Linkables.
410 = HscOK ModDetails -- new details (HomeSymbolTable additions)
411 (Maybe ModIFace) -- new iface (if any compilation was done)
412 (Maybe String) -- generated stub_h filename (in /tmp)
413 (Maybe String) -- generated stub_c filename (in /tmp)
414 (Maybe [UnlinkedIBind]) -- interpreted code, if any
415 PersistentCompilerState -- updated PCS
416 (Bag WarnMsg) -- warnings
418 | HscErrs PersistentCompilerState -- updated PCS
419 (Bag ErrMsg) -- errors
420 (Bag WarnMsg) -- warnings
422 -- These two are only here to avoid recursion between CmCompile and
423 -- CompManager. They really ought to be in the latter.
424 type ModuleEnv a = UniqFM a -- Domain is Module
426 type HomeModMap = FiniteMap ModuleName Module -- domain: home mods only
427 type HomeInterfaceTable = ModuleEnv ModIFace
431 %************************************************************************
433 \subsection{Provenance and export info}
435 %************************************************************************
437 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
438 one for each module, corresponding to that module's top-level scope.
441 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
442 -- These only get reported on lookup,
443 -- not on construction
446 The "provenance" of something says how it came to be in scope.
450 = LocalDef -- Defined locally
452 | NonLocalDef -- Defined non-locally
457 Moved here from Name.
458 pp_prov (LocalDef _ Exported) = char 'x'
459 pp_prov (LocalDef _ NotExported) = char 'l'
460 pp_prov (NonLocalDef ImplicitImport _) = char 'j'
461 pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I' -- Imported by name
462 pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i' -- Imported by ..
463 pp_prov SystemProv = char 's'
467 = UserImport Module SrcLoc Bool -- Imported from module M on line L
468 -- Note the M may well not be the defining module
470 -- The Bool is true iff the thing was named *explicitly* in the import spec,
471 -- rather than being imported as part of a group; e.g.
474 -- Here, everything imported by B, and the constructors of T
475 -- are not named explicitly; only T is named explicitly.
476 -- This info is used when warning of unused names.
478 | ImplicitImport -- Imported implicitly for some other reason
481 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
482 -- in scope in this module, so print it
483 -- unqualified in error messages
487 hasBetterProv :: Provenance -> Provenance -> Bool
489 -- a local thing over an imported thing
490 -- a user-imported thing over a non-user-imported thing
491 -- an explicitly-imported thing over an implicitly imported thing
492 hasBetterProv LocalDef _ = True
493 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
494 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
495 hasBetterProv _ _ = False
497 pprNameProvenance :: Name -> Provenance -> SDoc
498 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
499 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
500 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
502 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
503 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
505 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc