2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
8 ModDetails(..), GlobalSymbolTable,
9 HomeSymbolTable, PackageSymbolTable,
11 TyThing(..), groupTyThings,
13 TypeEnv, extendTypeEnv, lookupTypeEnv,
17 WhetherHasOrphans, ImportVersion, ExportItem, WhatsImported(..),
18 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
19 IfaceInsts, IfaceRules, DeprecationEnv,
20 OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
21 AvailEnv, AvailInfo, GenAvailInfo(..),
22 PersistentCompilerState(..),
24 InstEnv, ClsInstEnv, DFunId,
26 GlobalRdrEnv, RdrAvailInfo,
28 CompResult(..), HscResult(..),
31 Provenance(..), ImportReason(..), PrintUnqualified,
32 pprNameProvenance, hasBetterProv
36 #include "HsVersions.h"
38 import Name ( Name, NameEnv, NamedThing,
39 unitNameEnv, extendNameEnv, plusNameEnv,
40 lookupNameEnv, emptyNameEnv, getName, nameModule,
42 import Module ( Module, ModuleName,
43 extendModuleEnv, lookupModuleEnv )
44 import Class ( Class )
45 import OccName ( OccName )
46 import RdrName ( RdrNameEnv, emptyRdrEnv )
47 import Outputable ( SDoc )
48 import UniqFM ( UniqFM )
49 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
52 import VarEnv ( IdEnv, emptyVarEnv )
53 import BasicTypes ( Version, Fixity, defaultFixity )
54 import TyCon ( TyCon )
55 import ErrUtils ( ErrMsg, WarnMsg )
56 import CmLink ( Linkable )
57 import RdrHsSyn ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameHsDecl,
58 RdrNameDeprecation, RdrNameFixitySig )
59 import InterpSyn ( UnlinkedIBind )
60 import UniqSupply ( UniqSupply )
61 import HsDecls ( DeprecTxt )
62 import CoreSyn ( CoreRule )
63 import NameSet ( NameSet )
65 import VarSet ( TyVarSet )
66 import Panic ( panic )
68 import SrcLoc ( SrcLoc, isGoodSrcLoc )
69 import Util ( thenCmp )
72 %************************************************************************
74 \subsection{Symbol tables and Module details}
76 %************************************************************************
78 A @ModIface@ plus a @ModDetails@ summarises everything we know
79 about a compiled module. The @ModIface@ is the stuff *before* linking,
80 and can be written out to an interface file. The @ModDetails@ is after
81 linking; it is the "linked" form of the mi_decls field.
86 md_module :: Module, -- Complete with package info
87 md_version :: VersionInfo, -- Module version number
88 md_orphan :: WhetherHasOrphans, -- Whether this module has orphans
89 md_usages :: [ImportVersion Name], -- Usages
91 md_exports :: Avails, -- What it exports
92 md_globals :: GlobalRdrEnv, -- Its top level environment
94 md_fixities :: NameEnv Fixity, -- Fixities
95 md_deprecs :: NameEnv DeprecTxt, -- Deprecations
97 -- The next three fields are created by the typechecker
99 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
100 md_rules :: RuleEnv -- Domain may include Ids from other modules
103 -- ModIFace is nearly the same as RnMonad.ParsedIface.
104 -- Right now it's identical :)
107 mi_mod :: Module, -- Complete with package info
108 mi_vers :: Version, -- Module version number
109 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
110 mi_usages :: [ImportVersion OccName], -- Usages
111 mi_exports :: [ExportItem], -- Exports
112 mi_insts :: [RdrNameInstDecl], -- Local instance declarations
113 mi_decls :: [(Version, RdrNameHsDecl)], -- Local definitions
114 mi_fixity :: (Version, [RdrNameFixitySig]), -- Local fixity declarations,
115 -- with their version
116 mi_rules :: (Version, [RdrNameRuleDecl]), -- Rules, with their version
117 mi_deprecs :: [RdrNameDeprecation] -- Deprecations
123 emptyModDetails :: Module -> ModDetails
125 = ModDetails { md_module = mod,
127 md_globals = emptyRdrEnv,
128 md_fixities = emptyNameEnv,
129 md_deprecs = emptyNameEnv,
130 md_types = emptyNameEnv,
132 md_rules = emptyRuleEnv
136 Symbol tables map modules to ModDetails:
139 type SymbolTable = ModuleEnv ModDetails
140 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
141 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
142 type GlobalSymbolTable = SymbolTable -- Domain = all modules
145 Simple lookups in the symbol table.
148 lookupFixityEnv :: SymbolTable -> Name -> Maybe Fixity
149 -- Returns defaultFixity if there isn't an explicit fixity
150 lookupFixityEnv tbl name
151 = case lookupModuleEnv tbl (nameModule name) of
153 Just details -> lookupNameEnv (md_fixities details) name
157 %************************************************************************
159 \subsection{Type environment stuff}
161 %************************************************************************
164 type TypeEnv = NameEnv TyThing
166 data TyThing = AnId Id
170 instance NamedThing TyThing where
171 getName (AnId id) = getName id
172 getName (ATyCon tc) = getName tc
173 getName (AClass cl) = getName cl
178 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
179 lookupTypeEnv tbl name
180 = case lookupModuleEnv tbl (nameModule name) of
181 Just details -> lookupNameEnv (md_types details) name
185 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
186 -- Finite map because we want the range too
188 = foldl add emptyFM things
190 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
191 add tbl thing = addToFM tbl mod new_env
194 mod = nameModule name
195 new_env = case lookupFM tbl mod of
196 Nothing -> unitNameEnv name thing
197 Just env -> extendNameEnv env name thing
199 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
200 extendTypeEnv tbl things
201 = foldFM add tbl things
204 = panic "extendTypeEnv" --extendModuleEnv mod new_details
207 = case lookupModuleEnv tbl mod of
208 Nothing -> (emptyModDetails mod) {md_types = type_env}
209 Just details -> details {md_types = md_types details
210 `plusNameEnv` type_env}
214 %************************************************************************
216 \subsection{Auxiliary types}
218 %************************************************************************
220 These types are defined here because they are mentioned in ModDetails,
221 but they are mostly elaborated elsewhere
229 declVers :: NameEnv Version
232 type DeprecationEnv = NameEnv DeprecTxt -- Give reason for deprecation
234 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
235 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
238 type RuleEnv = IdEnv [CoreRule]
240 emptyRuleEnv = emptyVarEnv
245 type Avails = [AvailInfo]
246 type AvailInfo = GenAvailInfo Name
247 type RdrAvailInfo = GenAvailInfo OccName
249 data GenAvailInfo name = Avail name -- An ordinary identifier
250 | AvailTC name -- The name of the type or class
251 [name] -- The available pieces of type/class.
252 -- NB: If the type or class is itself
253 -- to be in scope, it must be in this list.
254 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
256 -- Equality used when deciding if the interface has changed
258 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
262 %************************************************************************
264 \subsection{ModIface}
266 %************************************************************************
269 type ExportItem = (ModuleName, [RdrAvailInfo])
271 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
273 type ModVersionInfo = (Version, -- Version of the whole module
274 Version, -- Version number for all fixity decls together
275 Version) -- ...ditto all rules together
277 type WhetherHasOrphans = Bool
279 -- * an instance decl in a module other than the defn module for
280 -- one of the tycons or classes in the instance head
281 -- * a transformation rule in a module other than the one defining
282 -- the function in the head of the rule.
284 type IsBootInterface = Bool
286 data WhatsImported name = NothingAtAll -- The module is below us in the
287 -- hierarchy, but we import nothing
289 | Everything Version -- The module version
291 | Specifically Version -- Module version
292 Version -- Fixity version
293 Version -- Rules version
294 [(name,Version)] -- List guaranteed non-empty
296 -- 'Specifically' doesn't let you say "I imported f but none of the fixities in
297 -- the module". If you use anything in the module you get its fixity and rule version
298 -- So if the fixities or rules change, you'll recompile, even if you don't use either.
299 -- This is easy to implement, and it's safer: you might not have used the rules last
300 -- time round, but if someone has added a new rule you might need it this time
302 -- 'Everything' means there was a "module M" in
303 -- this module's export list, so we just have to go by M's version,
304 -- not the list of (name,version) pairs
308 %************************************************************************
310 \subsection{The persistent compiler state}
312 %************************************************************************
315 data PersistentCompilerState
317 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
318 -- except that the InstEnv components is empty
319 pcs_insts :: InstEnv, -- The total InstEnv accumulated from all
320 -- the non-home-package modules
321 pcs_rules :: RuleEnv, -- Ditto RuleEnv
323 pcs_PRS :: PersistentRenamerState
327 The @PersistentRenamerState@ persists across successive calls to the
331 * A name supply, which deals with allocating unique names to
332 (Module,OccName) original names,
334 * An accumulated InstEnv from all the modules in pcs_PST
335 The point is that we don't want to keep recreating it whenever
336 we compile a new module. The InstEnv component of pcPST is empty.
337 (This means we might "see" instances that we shouldn't "really" see;
338 but the Haskell Report is vague on what is meant to be visible,
339 so we just take the easy road here.)
343 * A "holding pen" for declarations that have been read out of
344 interface files but not yet sucked in, renamed, and typechecked
347 data PersistentRenamerState
348 = PRS { prsOrig :: OrigNameEnv,
349 prsDecls :: DeclsMap,
350 prsInsts :: IfaceInsts,
351 prsRules :: IfaceRules
355 The OrigNameEnv makes sure that there is just one Unique assigned for
356 each original name; i.e. (module-name, occ-name) pair. The Name is
357 always stored as a Global, and has the SrcLoc of its binding location.
358 Actually that's not quite right. When we first encounter the original
359 name, we might not be at its binding site (e.g. we are reading an
360 interface file); so we give it 'noSrcLoc' then. Later, when we find
361 its binding site, we fix it up.
363 Exactly the same is true of the Module stored in the Name. When we first
364 encounter the occurrence, we may not know the details of the module, so
365 we just store junk. Then when we find the binding site, we fix it up.
369 = Orig { origNames :: OrigNameNameEnv,
370 -- Ensures that one original name gets one unique
371 origIParam :: OrigNameIParamEnv
372 -- Ensures that one implicit parameter name gets one unique
375 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
376 type OrigNameIParamEnv = FiniteMap OccName Name
380 A DeclsMap contains a binding for each Name in the declaration
381 including the constructors of a type decl etc. The Bool is True just
385 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameHsDecl))
387 type IfaceInsts = Bag GatedDecl
388 type IfaceRules = Bag GatedDecl
390 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
394 %************************************************************************
396 \subsection{The result of compiling one module}
398 %************************************************************************
402 = CompOK ModDetails -- new details (HST additions)
403 (Maybe (ModIFace, Linkable))
404 -- summary and code; Nothing => compilation not reqd
405 -- (old summary and code are still valid)
406 PersistentCompilerState -- updated PCS
407 (Bag WarnMsg) -- warnings
409 | CompErrs PersistentCompilerState -- updated PCS
410 (Bag ErrMsg) -- errors
411 (Bag WarnMsg) -- warnings
414 -- The driver sits between 'compile' and 'hscMain', translating calls
415 -- to the former into calls to the latter, and results from the latter
416 -- into results from the former. It does things like preprocessing
417 -- the .hs file if necessary, and compiling up the .stub_c files to
418 -- generate Linkables.
421 = HscOK ModDetails -- new details (HomeSymbolTable additions)
422 (Maybe ModIFace) -- new iface (if any compilation was done)
423 (Maybe String) -- generated stub_h filename (in /tmp)
424 (Maybe String) -- generated stub_c filename (in /tmp)
425 (Maybe [UnlinkedIBind]) -- interpreted code, if any
426 PersistentCompilerState -- updated PCS
427 (Bag WarnMsg) -- warnings
429 | HscErrs PersistentCompilerState -- updated PCS
430 (Bag ErrMsg) -- errors
431 (Bag WarnMsg) -- warnings
433 -- These two are only here to avoid recursion between CmCompile and
434 -- CompManager. They really ought to be in the latter.
435 type ModuleEnv a = UniqFM a -- Domain is Module
437 type HomeModMap = FiniteMap ModuleName Module -- domain: home mods only
438 type HomeInterfaceTable = ModuleEnv ModIFace
442 %************************************************************************
444 \subsection{Provenance and export info}
446 %************************************************************************
448 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
449 one for each module, corresponding to that module's top-level scope.
452 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
453 -- These only get reported on lookup,
454 -- not on construction
457 The "provenance" of something says how it came to be in scope.
461 = LocalDef -- Defined locally
463 | NonLocalDef -- Defined non-locally
467 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
468 instance Eq Provenance where
469 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
471 instance Eq ImportReason where
472 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
474 instance Ord Provenance where
475 compare LocalDef LocalDef = EQ
476 compare LocalDef (NonLocalDef _ _) = LT
477 compare (NonLocalDef _ _) LocalDef = GT
479 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
480 = compare reason1 reason2
482 instance Ord ImportReason where
483 compare ImplicitImport ImplicitImport = EQ
484 compare ImplicitImport (UserImport _ _ _) = LT
485 compare (UserImport _ _ _) ImplicitImport = GT
486 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
487 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
491 Moved here from Name.
492 pp_prov (LocalDef _ Exported) = char 'x'
493 pp_prov (LocalDef _ NotExported) = char 'l'
494 pp_prov (NonLocalDef ImplicitImport _) = char 'j'
495 pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I' -- Imported by name
496 pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i' -- Imported by ..
497 pp_prov SystemProv = char 's'
501 = UserImport Module SrcLoc Bool -- Imported from module M on line L
502 -- Note the M may well not be the defining module
504 -- The Bool is true iff the thing was named *explicitly* in the import spec,
505 -- rather than being imported as part of a group; e.g.
508 -- Here, everything imported by B, and the constructors of T
509 -- are not named explicitly; only T is named explicitly.
510 -- This info is used when warning of unused names.
512 | ImplicitImport -- Imported implicitly for some other reason
515 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
516 -- in scope in this module, so print it
517 -- unqualified in error messages
521 hasBetterProv :: Provenance -> Provenance -> Bool
523 -- a local thing over an imported thing
524 -- a user-imported thing over a non-user-imported thing
525 -- an explicitly-imported thing over an implicitly imported thing
526 hasBetterProv LocalDef _ = True
527 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
528 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
529 hasBetterProv _ _ = False
531 pprNameProvenance :: Name -> Provenance -> SDoc
532 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
533 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
534 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
536 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
537 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
539 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc