2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
10 ModDetails(..), ModIface(..), GlobalSymbolTable,
11 HomeSymbolTable, PackageSymbolTable,
12 HomeIfaceTable, PackageIfaceTable,
13 lookupTable, lookupTableByModName,
17 VersionInfo(..), initialVersionInfo,
19 TyThing(..), groupTyThings, isTyClThing,
21 TypeEnv, extendTypeEnv, lookupTypeEnv,
22 typeEnvClasses, typeEnvTyCons,
24 WhetherHasOrphans, ImportVersion, WhatsImported(..),
25 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
26 IfaceInsts, IfaceRules, GatedDecl,
27 OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
28 AvailEnv, AvailInfo, GenAvailInfo(..),
29 PersistentCompilerState(..),
31 Deprecations(..), lookupDeprec,
33 InstEnv, ClsInstEnv, DFunId,
34 PackageInstEnv, PackageRuleBase,
36 GlobalRdrEnv, RdrAvailInfo,
39 Provenance(..), ImportReason(..), PrintUnqualified,
40 pprNameProvenance, hasBetterProv
44 #include "HsVersions.h"
46 import RdrName ( RdrNameEnv, emptyRdrEnv )
47 import Name ( Name, NameEnv, NamedThing,
48 emptyNameEnv, unitNameEnv, extendNameEnv, plusNameEnv,
49 lookupNameEnv, emptyNameEnv, getName, nameModule,
50 nameSrcLoc, nameEnvElts )
51 import NameSet ( NameSet )
52 import OccName ( OccName )
53 import Module ( Module, ModuleName, ModuleEnv,
54 extendModuleEnv, lookupModuleEnv, lookupModuleEnvByName
56 import Rules ( RuleBase )
57 import VarSet ( TyVarSet )
59 import Class ( Class )
60 import TyCon ( TyCon )
62 import BasicTypes ( Version, initialVersion, Fixity )
64 import HsSyn ( DeprecTxt )
65 import RdrHsSyn ( RdrNameHsDecl, RdrNameTyClDecl )
66 import RnHsSyn ( RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl )
68 import CoreSyn ( CoreRule, IdCoreRule )
71 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
73 import Maybes ( seqMaybe )
74 import UniqFM ( UniqFM )
76 import SrcLoc ( SrcLoc, isGoodSrcLoc )
77 import Util ( thenCmp )
78 import UniqSupply ( UniqSupply )
81 %************************************************************************
83 \subsection{Module locations}
85 %************************************************************************
96 For a module in another package, the hs_file and obj_file
97 components of ModuleLocation are undefined.
99 The locations specified by a ModuleLocation may or may not
100 correspond to actual files yet: for example, even if the object
101 file doesn't exist, the ModuleLocation still contains the path to
102 where the object file will reside if/when it is created.
105 %************************************************************************
107 \subsection{Symbol tables and Module details}
109 %************************************************************************
111 A @ModIface@ plus a @ModDetails@ summarises everything we know
112 about a compiled module. The @ModIface@ is the stuff *before* linking,
113 and can be written out to an interface file. The @ModDetails@ is after
114 linking; it is the "linked" form of the mi_decls field.
119 mi_module :: Module, -- Complete with package info
120 mi_version :: VersionInfo, -- Module version number
121 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
123 mi_usages :: [ImportVersion Name], -- Usages; kept sorted so that it's easy
124 -- to decide whether to write a new iface file
125 -- (changing usages doesn't affect the version of
128 mi_exports :: [(ModuleName,Avails)], -- What it exports
129 -- Kept sorted by (mod,occ),
130 -- to make version comparisons easier
132 mi_globals :: GlobalRdrEnv, -- Its top level environment
134 mi_fixities :: NameEnv Fixity, -- Fixities
135 mi_deprecs :: Deprecations, -- Deprecations
137 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
140 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
141 dcl_rules :: [RenamedRuleDecl], -- Sorted
142 dcl_insts :: [RenamedInstDecl] } -- Unsorted
144 -- typechecker should only look at this, not ModIface
145 -- Should be able to construct ModDetails from mi_decls in ModIface
148 -- The next three fields are created by the typechecker
150 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
151 md_rules :: [IdCoreRule] -- Domain may include Ids from other modules
156 emptyModDetails :: ModDetails
158 = ModDetails { md_types = emptyTypeEnv,
163 emptyModIface :: Module -> ModIface
165 = ModIface { mi_module = mod,
167 mi_globals = emptyRdrEnv,
168 mi_deprecs = NoDeprecs
172 Symbol tables map modules to ModDetails:
175 type SymbolTable = ModuleEnv ModDetails
176 type IfaceTable = ModuleEnv ModIface
178 type HomeIfaceTable = IfaceTable
179 type PackageIfaceTable = IfaceTable
181 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
182 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
183 type GlobalSymbolTable = SymbolTable -- Domain = all modules
186 Simple lookups in the symbol table.
189 lookupTable :: ModuleEnv a -> ModuleEnv a -> Name -> Maybe a
190 -- We often have two Symbol- or IfaceTables, and want to do a lookup
191 lookupTable ht pt name
192 = lookupModuleEnv ht mod `seqMaybe` lookupModuleEnv pt mod
194 mod = nameModule name
196 lookupTableByModName :: ModuleEnv a -> ModuleEnv a -> ModuleName -> Maybe a
197 -- We often have two Symbol- or IfaceTables, and want to do a lookup
198 lookupTableByModName ht pt mod
199 = lookupModuleEnvByName ht mod `seqMaybe` lookupModuleEnvByName pt mod
203 %************************************************************************
205 \subsection{Type environment stuff}
207 %************************************************************************
210 type TypeEnv = NameEnv TyThing
211 emptyTypeEnv = emptyNameEnv
213 data TyThing = AnId Id
217 isTyClThing :: TyThing -> Bool
218 isTyClThing (ATyCon _) = True
219 isTyClThing (AClass _) = True
220 isTyClThing (AnId _) = False
222 instance NamedThing TyThing where
223 getName (AnId id) = getName id
224 getName (ATyCon tc) = getName tc
225 getName (AClass cl) = getName cl
227 typeEnvClasses env = [cl | AClass cl <- nameEnvElts env]
228 typeEnvTyCons env = [tc | ATyCon tc <- nameEnvElts env]
234 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
235 lookupTypeEnv tbl name
236 = case lookupModuleEnv tbl (nameModule name) of
237 Just details -> lookupNameEnv (md_types details) name
241 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
242 -- Finite map because we want the range too
244 = foldl add emptyFM things
246 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
247 add tbl thing = addToFM tbl mod new_env
250 mod = nameModule name
251 new_env = case lookupFM tbl mod of
252 Nothing -> unitNameEnv name thing
253 Just env -> extendNameEnv env name thing
255 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
256 extendTypeEnv tbl things
257 = foldFM add tbl things
260 = extendModuleEnv tbl mod new_details
263 = case lookupModuleEnv tbl mod of
264 Nothing -> emptyModDetails {md_types = type_env}
265 Just details -> details {md_types = md_types details
266 `plusNameEnv` type_env}
270 %************************************************************************
272 \subsection{Auxiliary types}
274 %************************************************************************
276 These types are defined here because they are mentioned in ModDetails,
277 but they are mostly elaborated elsewhere
282 vers_module :: Version, -- Changes when anything changes
283 vers_exports :: Version, -- Changes when export list changes
284 vers_rules :: Version, -- Changes when any rule changes
285 vers_decls :: NameEnv Version
286 -- Versions for "big" names only (not data constructors, class ops)
287 -- The version of an Id changes if its fixity changes
288 -- Ditto data constructors, class operations, except that the version of
289 -- the parent class/tycon changes
292 initialVersionInfo :: VersionInfo
293 initialVersionInfo = VersionInfo { vers_module = initialVersion,
294 vers_exports = initialVersion,
295 vers_rules = initialVersion,
296 vers_decls = emptyNameEnv }
298 data Deprecations = NoDeprecs
299 | DeprecAll DeprecTxt -- Whole module deprecated
300 | DeprecSome (NameEnv (Name,DeprecTxt)) -- Some things deprecated
302 -- We keep the Name in the range, so we can print them out
304 lookupDeprec :: ModIface -> Name -> Maybe DeprecTxt
305 lookupDeprec iface name
306 = case mi_deprecs iface of
308 DeprecAll txt -> Just txt
309 DeprecSome env -> case lookupNameEnv env name of
310 Just (_, txt) -> Just txt
313 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
315 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
321 type Avails = [AvailInfo]
322 type AvailInfo = GenAvailInfo Name
323 type RdrAvailInfo = GenAvailInfo OccName
325 data GenAvailInfo name = Avail name -- An ordinary identifier
326 | AvailTC name -- The name of the type or class
327 [name] -- The available pieces of type/class.
328 -- NB: If the type or class is itself
329 -- to be in scope, it must be in this list.
330 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
332 -- Equality used when deciding if the interface has changed
334 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
338 %************************************************************************
340 \subsection{ModIface}
342 %************************************************************************
345 type WhetherHasOrphans = Bool
347 -- * an instance decl in a module other than the defn module for
348 -- one of the tycons or classes in the instance head
349 -- * a transformation rule in a module other than the one defining
350 -- the function in the head of the rule.
352 type IsBootInterface = Bool
354 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
356 data WhatsImported name = NothingAtAll -- The module is below us in the
357 -- hierarchy, but we import nothing
359 | Everything Version -- Used for modules from other packages;
360 -- we record only the module's version number
363 Version -- Module version
364 (Maybe Version) -- Export-list version, if we depend on it
365 [(name,Version)] -- List guaranteed non-empty
366 Version -- Rules version
369 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
370 -- the module". If you use anything in the module you get its rule version
371 -- So if the rules change, you'll recompile, even if you don't use them.
372 -- This is easy to implement, and it's safer: you might not have used the rules last
373 -- time round, but if someone has added a new rule you might need it this time
375 -- The export list field is (Just v) if we depend on the export list:
376 -- we imported the module without saying exactly what we imported
377 -- We need to recompile if the module exports changes, because we might
378 -- now have a name clash in the importing module.
382 %************************************************************************
384 \subsection{The persistent compiler state}
386 %************************************************************************
389 data PersistentCompilerState
391 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
392 -- the mi_decls component is empty
394 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
395 -- except that the InstEnv components is empty
397 pcs_insts :: PackageInstEnv, -- The total InstEnv accumulated from all
398 -- the non-home-package modules
400 pcs_rules :: PackageRuleBase, -- Ditto RuleEnv
402 pcs_PRS :: PersistentRenamerState
407 The @PersistentRenamerState@ persists across successive calls to the
411 * A name supply, which deals with allocating unique names to
412 (Module,OccName) original names,
414 * An accumulated InstEnv from all the modules in pcs_PST
415 The point is that we don't want to keep recreating it whenever
416 we compile a new module. The InstEnv component of pcPST is empty.
417 (This means we might "see" instances that we shouldn't "really" see;
418 but the Haskell Report is vague on what is meant to be visible,
419 so we just take the easy road here.)
423 * A "holding pen" for declarations that have been read out of
424 interface files but not yet sucked in, renamed, and typechecked
427 type PackageRuleBase = RuleBase
428 type PackageInstEnv = InstEnv
430 data PersistentRenamerState
431 = PRS { prsOrig :: OrigNameEnv,
432 prsDecls :: DeclsMap,
433 prsInsts :: IfaceInsts,
434 prsRules :: IfaceRules,
439 The OrigNameEnv makes sure that there is just one Unique assigned for
440 each original name; i.e. (module-name, occ-name) pair. The Name is
441 always stored as a Global, and has the SrcLoc of its binding location.
442 Actually that's not quite right. When we first encounter the original
443 name, we might not be at its binding site (e.g. we are reading an
444 interface file); so we give it 'noSrcLoc' then. Later, when we find
445 its binding site, we fix it up.
447 Exactly the same is true of the Module stored in the Name. When we first
448 encounter the occurrence, we may not know the details of the module, so
449 we just store junk. Then when we find the binding site, we fix it up.
453 = Orig { origNames :: OrigNameNameEnv,
454 -- Ensures that one original name gets one unique
455 origIParam :: OrigNameIParamEnv
456 -- Ensures that one implicit parameter name gets one unique
459 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
460 type OrigNameIParamEnv = FiniteMap OccName Name
464 A DeclsMap contains a binding for each Name in the declaration
465 including the constructors of a type decl etc. The Bool is True just
469 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameTyClDecl))
471 type IfaceInsts = Bag GatedDecl
472 type IfaceRules = Bag GatedDecl
474 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
478 %************************************************************************
480 \subsection{Provenance and export info}
482 %************************************************************************
484 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
485 one for each module, corresponding to that module's top-level scope.
488 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
489 -- These only get reported on lookup,
490 -- not on construction
493 The "provenance" of something says how it came to be in scope.
497 = LocalDef -- Defined locally
499 | NonLocalDef -- Defined non-locally
503 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
504 instance Eq Provenance where
505 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
507 instance Eq ImportReason where
508 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
510 instance Ord Provenance where
511 compare LocalDef LocalDef = EQ
512 compare LocalDef (NonLocalDef _ _) = LT
513 compare (NonLocalDef _ _) LocalDef = GT
515 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
516 = compare reason1 reason2
518 instance Ord ImportReason where
519 compare ImplicitImport ImplicitImport = EQ
520 compare ImplicitImport (UserImport _ _ _) = LT
521 compare (UserImport _ _ _) ImplicitImport = GT
522 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
523 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
527 = UserImport Module SrcLoc Bool -- Imported from module M on line L
528 -- Note the M may well not be the defining module
530 -- The Bool is true iff the thing was named *explicitly* in the import spec,
531 -- rather than being imported as part of a group; e.g.
534 -- Here, everything imported by B, and the constructors of T
535 -- are not named explicitly; only T is named explicitly.
536 -- This info is used when warning of unused names.
538 | ImplicitImport -- Imported implicitly for some other reason
541 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
542 -- in scope in this module, so print it
543 -- unqualified in error messages
547 hasBetterProv :: Provenance -> Provenance -> Bool
549 -- a local thing over an imported thing
550 -- a user-imported thing over a non-user-imported thing
551 -- an explicitly-imported thing over an implicitly imported thing
552 hasBetterProv LocalDef _ = True
553 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
554 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
555 hasBetterProv _ _ = False
557 pprNameProvenance :: Name -> Provenance -> SDoc
558 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
559 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
560 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
562 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
563 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
565 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc