2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
8 Finder, ModuleLocation(..),
10 ModDetails(..), ModIface(..), GlobalSymbolTable,
11 HomeSymbolTable, PackageSymbolTable,
12 HomeIfaceTable, PackageIfaceTable,
17 VersionInfo(..), initialVersionInfo,
19 TyThing(..), groupTyThings,
21 TypeEnv, extendTypeEnv, lookupTypeEnv,
23 WhetherHasOrphans, ImportVersion, WhatsImported(..),
24 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
25 IfaceInsts, IfaceRules, GatedDecl,
26 OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
27 AvailEnv, AvailInfo, GenAvailInfo(..),
28 PersistentCompilerState(..),
30 Deprecations(..), lookupDeprec,
32 InstEnv, ClsInstEnv, DFunId,
34 GlobalRdrEnv, RdrAvailInfo,
37 Provenance(..), ImportReason(..), PrintUnqualified,
38 pprNameProvenance, hasBetterProv
42 #include "HsVersions.h"
44 import RdrName ( RdrNameEnv, emptyRdrEnv )
45 import Name ( Name, NameEnv, NamedThing,
46 emptyNameEnv, unitNameEnv, extendNameEnv, plusNameEnv,
47 lookupNameEnv, emptyNameEnv, getName, nameModule,
49 import NameSet ( NameSet )
50 import OccName ( OccName )
51 import Module ( Module, ModuleName, ModuleEnv,
53 import VarSet ( TyVarSet )
54 import VarEnv ( emptyVarEnv )
56 import Class ( Class )
57 import TyCon ( TyCon )
59 import BasicTypes ( Version, initialVersion, Fixity )
61 import HsSyn ( DeprecTxt )
62 import RdrHsSyn ( RdrNameHsDecl )
63 import RnHsSyn ( RenamedTyClDecl, RenamedIfaceSig, RenamedRuleDecl, RenamedInstDecl )
65 import CoreSyn ( CoreRule )
68 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
70 import Maybes ( seqMaybe )
71 import UniqFM ( UniqFM )
73 import SrcLoc ( SrcLoc, isGoodSrcLoc )
74 import Util ( thenCmp )
77 %************************************************************************
79 \subsection{The Finder type}
81 %************************************************************************
84 type Finder = ModuleName -> IO (Maybe (Module, ModuleLocation))
94 For a module in another package, the hs_file and obj_file
95 components of ModuleLocation are undefined.
97 The locations specified by a ModuleLocation may or may not
98 correspond to actual files yet: for example, even if the object
99 file doesn't exist, the ModuleLocation still contains the path to
100 where the object file will reside if/when it is created.
103 %************************************************************************
105 \subsection{Symbol tables and Module details}
107 %************************************************************************
109 A @ModIface@ plus a @ModDetails@ summarises everything we know
110 about a compiled module. The @ModIface@ is the stuff *before* linking,
111 and can be written out to an interface file. The @ModDetails@ is after
112 linking; it is the "linked" form of the mi_decls field.
117 mi_module :: Module, -- Complete with package info
118 mi_version :: VersionInfo, -- Module version number
119 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
121 mi_usages :: [ImportVersion Name], -- Usages; kept sorted so that it's easy
122 -- to decide whether to write a new iface file
123 -- (changing usages doesn't affect the version of
126 mi_exports :: Avails, -- What it exports
127 -- Kept sorted by (mod,occ),
128 -- to make version comparisons easier
130 mi_globals :: GlobalRdrEnv, -- Its top level environment
132 mi_fixities :: NameEnv Fixity, -- Fixities
133 mi_deprecs :: Deprecations, -- Deprecations
135 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
138 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
139 dcl_sigs :: [RenamedIfaceSig], -- Sorted
140 dcl_rules :: [RenamedRuleDecl], -- Sorted
141 dcl_insts :: [RenamedInstDecl] } -- Unsorted
143 -- typechecker should only look at this, not ModIface
144 -- Should be able to construct ModDetails from mi_decls in ModIface
147 -- The next three fields are created by the typechecker
149 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
150 md_rules :: RuleEnv -- Domain may include Ids from other modules
155 emptyModDetails :: ModDetails
157 = ModDetails { md_types = emptyTypeEnv,
159 md_rules = emptyRuleEnv
162 emptyModIface :: Module -> ModIface
164 = ModIface { mi_module = mod,
166 mi_globals = emptyRdrEnv,
167 mi_deprecs = NoDeprecs
171 Symbol tables map modules to ModDetails:
174 type SymbolTable = ModuleEnv ModDetails
175 type IfaceTable = ModuleEnv ModIface
177 type HomeIfaceTable = IfaceTable
178 type PackageIfaceTable = IfaceTable
180 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
181 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
182 type GlobalSymbolTable = SymbolTable -- Domain = all modules
185 Simple lookups in the symbol table.
188 lookupTable :: ModuleEnv a -> ModuleEnv a -> Name -> Maybe a
189 -- We often have two Symbol- or IfaceTables, and want to do a lookup
190 lookupTable ht pt name
191 = lookupModuleEnv ht mod `seqMaybe` lookupModuleEnv pt mod
193 mod = nameModule name
197 %************************************************************************
199 \subsection{Type environment stuff}
201 %************************************************************************
204 type TypeEnv = NameEnv TyThing
205 emptyTypeEnv = emptyNameEnv
207 data TyThing = AnId Id
211 instance NamedThing TyThing where
212 getName (AnId id) = getName id
213 getName (ATyCon tc) = getName tc
214 getName (AClass cl) = getName cl
219 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
220 lookupTypeEnv tbl name
221 = case lookupModuleEnv tbl (nameModule name) of
222 Just details -> lookupNameEnv (md_types details) name
226 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
227 -- Finite map because we want the range too
229 = foldl add emptyFM things
231 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
232 add tbl thing = addToFM tbl mod new_env
235 mod = nameModule name
236 new_env = case lookupFM tbl mod of
237 Nothing -> unitNameEnv name thing
238 Just env -> extendNameEnv env name thing
240 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
241 extendTypeEnv tbl things
242 = foldFM add tbl things
245 = panic "extendTypeEnv" --extendModuleEnv mod new_details
248 = case lookupModuleEnv tbl mod of
249 Nothing -> emptyModDetails {md_types = type_env}
250 Just details -> details {md_types = md_types details
251 `plusNameEnv` type_env}
255 %************************************************************************
257 \subsection{Auxiliary types}
259 %************************************************************************
261 These types are defined here because they are mentioned in ModDetails,
262 but they are mostly elaborated elsewhere
267 vers_module :: Version, -- Changes when anything changes
268 vers_exports :: Version, -- Changes when export list changes
269 vers_rules :: Version, -- Changes when any rule changes
270 vers_decls :: NameEnv Version
271 -- Versions for "big" names only (not data constructors, class ops)
272 -- The version of an Id changes if its fixity changes
273 -- Ditto data constructors, class operations, except that the version of
274 -- the parent class/tycon changes
277 initialVersionInfo :: VersionInfo
278 initialVersionInfo = VersionInfo { vers_module = initialVersion,
279 vers_exports = initialVersion,
280 vers_rules = initialVersion,
281 vers_decls = emptyNameEnv }
283 data Deprecations = NoDeprecs
284 | DeprecAll DeprecTxt -- Whole module deprecated
285 | DeprecSome (NameEnv DeprecTxt) -- Some things deprecated
288 lookupDeprec :: ModIface -> Name -> Maybe DeprecTxt
289 lookupDeprec iface name
290 = case mi_deprecs iface of
292 DeprecAll txt -> Just txt
293 DeprecSome env -> lookupNameEnv env name
295 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
296 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
299 type RuleEnv = NameEnv [CoreRule]
301 emptyRuleEnv = emptyVarEnv
306 type Avails = [AvailInfo]
307 type AvailInfo = GenAvailInfo Name
308 type RdrAvailInfo = GenAvailInfo OccName
310 data GenAvailInfo name = Avail name -- An ordinary identifier
311 | AvailTC name -- The name of the type or class
312 [name] -- The available pieces of type/class.
313 -- NB: If the type or class is itself
314 -- to be in scope, it must be in this list.
315 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
317 -- Equality used when deciding if the interface has changed
319 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
323 %************************************************************************
325 \subsection{ModIface}
327 %************************************************************************
330 type WhetherHasOrphans = Bool
332 -- * an instance decl in a module other than the defn module for
333 -- one of the tycons or classes in the instance head
334 -- * a transformation rule in a module other than the one defining
335 -- the function in the head of the rule.
337 type IsBootInterface = Bool
339 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
341 data WhatsImported name = NothingAtAll -- The module is below us in the
342 -- hierarchy, but we import nothing
344 | Everything Version -- Used for modules from other packages;
345 -- we record only the module's version number
348 Version -- Module version
349 (Maybe Version) -- Export-list version, if we depend on it
350 [(name,Version)] -- List guaranteed non-empty
351 Version -- Rules version
354 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
355 -- the module". If you use anything in the module you get its rule version
356 -- So if the rules change, you'll recompile, even if you don't use them.
357 -- This is easy to implement, and it's safer: you might not have used the rules last
358 -- time round, but if someone has added a new rule you might need it this time
360 -- The export list field is (Just v) if we depend on the export list:
361 -- we imported the module without saying exactly what we imported
362 -- We need to recompile if the module exports changes, because we might
363 -- now have a name clash in the importing module.
367 %************************************************************************
369 \subsection{The persistent compiler state}
371 %************************************************************************
374 data PersistentCompilerState
376 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
377 -- the mi_decls component is empty
378 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
379 -- except that the InstEnv components is empty
380 pcs_insts :: InstEnv, -- The total InstEnv accumulated from all
381 -- the non-home-package modules
382 pcs_rules :: RuleEnv, -- Ditto RuleEnv
384 pcs_PRS :: PersistentRenamerState
388 The @PersistentRenamerState@ persists across successive calls to the
392 * A name supply, which deals with allocating unique names to
393 (Module,OccName) original names,
395 * An accumulated InstEnv from all the modules in pcs_PST
396 The point is that we don't want to keep recreating it whenever
397 we compile a new module. The InstEnv component of pcPST is empty.
398 (This means we might "see" instances that we shouldn't "really" see;
399 but the Haskell Report is vague on what is meant to be visible,
400 so we just take the easy road here.)
404 * A "holding pen" for declarations that have been read out of
405 interface files but not yet sucked in, renamed, and typechecked
408 data PersistentRenamerState
409 = PRS { prsOrig :: OrigNameEnv,
410 prsDecls :: DeclsMap,
411 prsInsts :: IfaceInsts,
412 prsRules :: IfaceRules
416 The OrigNameEnv makes sure that there is just one Unique assigned for
417 each original name; i.e. (module-name, occ-name) pair. The Name is
418 always stored as a Global, and has the SrcLoc of its binding location.
419 Actually that's not quite right. When we first encounter the original
420 name, we might not be at its binding site (e.g. we are reading an
421 interface file); so we give it 'noSrcLoc' then. Later, when we find
422 its binding site, we fix it up.
424 Exactly the same is true of the Module stored in the Name. When we first
425 encounter the occurrence, we may not know the details of the module, so
426 we just store junk. Then when we find the binding site, we fix it up.
430 = Orig { origNames :: OrigNameNameEnv,
431 -- Ensures that one original name gets one unique
432 origIParam :: OrigNameIParamEnv
433 -- Ensures that one implicit parameter name gets one unique
436 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
437 type OrigNameIParamEnv = FiniteMap OccName Name
441 A DeclsMap contains a binding for each Name in the declaration
442 including the constructors of a type decl etc. The Bool is True just
446 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameHsDecl))
448 type IfaceInsts = Bag GatedDecl
449 type IfaceRules = Bag GatedDecl
451 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
455 %************************************************************************
457 \subsection{Provenance and export info}
459 %************************************************************************
461 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
462 one for each module, corresponding to that module's top-level scope.
465 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
466 -- These only get reported on lookup,
467 -- not on construction
470 The "provenance" of something says how it came to be in scope.
474 = LocalDef -- Defined locally
476 | NonLocalDef -- Defined non-locally
480 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
481 instance Eq Provenance where
482 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
484 instance Eq ImportReason where
485 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
487 instance Ord Provenance where
488 compare LocalDef LocalDef = EQ
489 compare LocalDef (NonLocalDef _ _) = LT
490 compare (NonLocalDef _ _) LocalDef = GT
492 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
493 = compare reason1 reason2
495 instance Ord ImportReason where
496 compare ImplicitImport ImplicitImport = EQ
497 compare ImplicitImport (UserImport _ _ _) = LT
498 compare (UserImport _ _ _) ImplicitImport = GT
499 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
500 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
504 = UserImport Module SrcLoc Bool -- Imported from module M on line L
505 -- Note the M may well not be the defining module
507 -- The Bool is true iff the thing was named *explicitly* in the import spec,
508 -- rather than being imported as part of a group; e.g.
511 -- Here, everything imported by B, and the constructors of T
512 -- are not named explicitly; only T is named explicitly.
513 -- This info is used when warning of unused names.
515 | ImplicitImport -- Imported implicitly for some other reason
518 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
519 -- in scope in this module, so print it
520 -- unqualified in error messages
524 hasBetterProv :: Provenance -> Provenance -> Bool
526 -- a local thing over an imported thing
527 -- a user-imported thing over a non-user-imported thing
528 -- an explicitly-imported thing over an implicitly imported thing
529 hasBetterProv LocalDef _ = True
530 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
531 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
532 hasBetterProv _ _ = False
534 pprNameProvenance :: Name -> Provenance -> SDoc
535 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
536 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
537 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
539 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
540 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
542 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc