2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
10 ModDetails(..), ModIface(..),
11 HomeSymbolTable, PackageTypeEnv,
12 HomeIfaceTable, PackageIfaceTable, emptyIfaceTable,
13 lookupTable, lookupTableByModName,
18 VersionInfo(..), initialVersionInfo,
20 TyThing(..), isTyClThing,
22 TypeEnv, lookupType, mkTypeEnv, extendTypeEnvList,
23 typeEnvClasses, typeEnvTyCons,
25 WhetherHasOrphans, ImportVersion, WhatsImported(..),
26 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
27 IfaceInsts, IfaceRules, GatedDecl,
28 OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
29 AvailEnv, AvailInfo, GenAvailInfo(..),
30 PersistentCompilerState(..),
32 Deprecations(..), lookupDeprec,
34 InstEnv, ClsInstEnv, DFunId,
35 PackageInstEnv, PackageRuleBase,
37 GlobalRdrEnv, RdrAvailInfo,
40 Provenance(..), ImportReason(..), PrintUnqualified,
41 pprNameProvenance, hasBetterProv
45 #include "HsVersions.h"
47 import RdrName ( RdrNameEnv, emptyRdrEnv )
48 import Name ( Name, NameEnv, NamedThing,
49 emptyNameEnv, extendNameEnv,
50 lookupNameEnv, emptyNameEnv, getName, nameModule,
51 nameSrcLoc, nameEnvElts )
52 import NameSet ( NameSet )
53 import OccName ( OccName )
54 import Module ( Module, ModuleName, ModuleEnv,
55 lookupModuleEnv, lookupModuleEnvByName
57 import Rules ( RuleBase )
58 import VarSet ( TyVarSet )
60 import Class ( Class )
61 import TyCon ( TyCon )
63 import BasicTypes ( Version, initialVersion, Fixity )
65 import HsSyn ( DeprecTxt )
66 import RdrHsSyn ( RdrNameHsDecl, RdrNameTyClDecl )
67 import RnHsSyn ( RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl )
69 import CoreSyn ( IdCoreRule )
72 import FiniteMap ( FiniteMap )
74 import Maybes ( seqMaybe )
75 import UniqFM ( UniqFM, emptyUFM )
77 import SrcLoc ( SrcLoc, isGoodSrcLoc )
78 import Util ( thenCmp )
79 import UniqSupply ( UniqSupply )
82 %************************************************************************
84 \subsection{Module locations}
86 %************************************************************************
91 ml_hs_file :: Maybe FilePath,
92 ml_hspp_file :: Maybe FilePath, -- path of preprocessed source
93 ml_hi_file :: Maybe FilePath,
94 ml_obj_file :: Maybe FilePath
98 instance Outputable ModuleLocation where
102 For a module in another package, the hs_file and obj_file
103 components of ModuleLocation are undefined.
105 The locations specified by a ModuleLocation may or may not
106 correspond to actual files yet: for example, even if the object
107 file doesn't exist, the ModuleLocation still contains the path to
108 where the object file will reside if/when it is created.
111 %************************************************************************
113 \subsection{Symbol tables and Module details}
115 %************************************************************************
117 A @ModIface@ plus a @ModDetails@ summarises everything we know
118 about a compiled module. The @ModIface@ is the stuff *before* linking,
119 and can be written out to an interface file. The @ModDetails@ is after
120 linking; it is the "linked" form of the mi_decls field.
125 mi_module :: Module, -- Complete with package info
126 mi_version :: VersionInfo, -- Module version number
127 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
128 mi_boot :: IsBootInterface, -- Whether this interface was read from an hi-boot file
130 mi_usages :: [ImportVersion Name], -- Usages; kept sorted so that it's easy
131 -- to decide whether to write a new iface file
132 -- (changing usages doesn't affect the version of
135 mi_exports :: [(ModuleName,Avails)], -- What it exports
136 -- Kept sorted by (mod,occ),
137 -- to make version comparisons easier
139 mi_globals :: GlobalRdrEnv, -- Its top level environment
141 mi_fixities :: NameEnv Fixity, -- Fixities
142 mi_deprecs :: Deprecations, -- Deprecations
144 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
147 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
148 dcl_rules :: [RenamedRuleDecl], -- Sorted
149 dcl_insts :: [RenamedInstDecl] } -- Unsorted
151 -- typechecker should only look at this, not ModIface
152 -- Should be able to construct ModDetails from mi_decls in ModIface
155 -- The next three fields are created by the typechecker
157 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
158 md_rules :: [IdCoreRule] -- Domain may include Ids from other modules
163 emptyModDetails :: ModDetails
165 = ModDetails { md_types = emptyTypeEnv,
170 emptyModIface :: Module -> ModIface
172 = ModIface { mi_module = mod,
173 mi_version = initialVersionInfo,
178 mi_fixities = emptyNameEnv,
179 mi_globals = emptyRdrEnv,
180 mi_deprecs = NoDeprecs,
181 mi_decls = panic "emptyModIface: decls"
185 Symbol tables map modules to ModDetails:
188 type SymbolTable = ModuleEnv ModDetails
189 type IfaceTable = ModuleEnv ModIface
191 type HomeIfaceTable = IfaceTable
192 type PackageIfaceTable = IfaceTable
194 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
196 emptyIfaceTable :: IfaceTable
197 emptyIfaceTable = emptyUFM
200 Simple lookups in the symbol table.
203 lookupTable :: ModuleEnv a -> ModuleEnv a -> Name -> Maybe a
204 -- We often have two Symbol- or IfaceTables, and want to do a lookup
205 lookupTable ht pt name
206 = lookupModuleEnv ht mod `seqMaybe` lookupModuleEnv pt mod
208 mod = nameModule name
210 lookupTableByModName :: ModuleEnv a -> ModuleEnv a -> ModuleName -> Maybe a
211 -- We often have two Symbol- or IfaceTables, and want to do a lookup
212 lookupTableByModName ht pt mod
213 = lookupModuleEnvByName ht mod `seqMaybe` lookupModuleEnvByName pt mod
217 %************************************************************************
219 \subsection{Type environment stuff}
221 %************************************************************************
224 data TyThing = AnId Id
228 isTyClThing :: TyThing -> Bool
229 isTyClThing (ATyCon _) = True
230 isTyClThing (AClass _) = True
231 isTyClThing (AnId _) = False
233 instance NamedThing TyThing where
234 getName (AnId id) = getName id
235 getName (ATyCon tc) = getName tc
236 getName (AClass cl) = getName cl
238 typeEnvClasses env = [cl | AClass cl <- nameEnvElts env]
239 typeEnvTyCons env = [tc | ATyCon tc <- nameEnvElts env]
245 type TypeEnv = NameEnv TyThing
247 emptyTypeEnv = emptyNameEnv
249 mkTypeEnv :: [TyThing] -> TypeEnv
250 mkTypeEnv things = extendTypeEnvList emptyTypeEnv things
252 extendTypeEnvList :: TypeEnv -> [TyThing] -> TypeEnv
253 extendTypeEnvList env things
254 = foldl add_thing env things
256 add_thing :: TypeEnv -> TyThing -> TypeEnv
257 add_thing env thing = extendNameEnv env (getName thing) thing
261 lookupType :: HomeSymbolTable -> PackageTypeEnv -> Name -> Maybe TyThing
262 lookupType hst pte name
263 = case lookupModuleEnv hst (nameModule name) of
264 Just details -> lookupNameEnv (md_types details) name
265 Nothing -> lookupNameEnv pte name
268 %************************************************************************
270 \subsection{Auxiliary types}
272 %************************************************************************
274 These types are defined here because they are mentioned in ModDetails,
275 but they are mostly elaborated elsewhere
280 vers_module :: Version, -- Changes when anything changes
281 vers_exports :: Version, -- Changes when export list changes
282 vers_rules :: Version, -- Changes when any rule changes
283 vers_decls :: NameEnv Version
284 -- Versions for "big" names only (not data constructors, class ops)
285 -- The version of an Id changes if its fixity changes
286 -- Ditto data constructors, class operations, except that the version of
287 -- the parent class/tycon changes
290 initialVersionInfo :: VersionInfo
291 initialVersionInfo = VersionInfo { vers_module = initialVersion,
292 vers_exports = initialVersion,
293 vers_rules = initialVersion,
294 vers_decls = emptyNameEnv }
296 data Deprecations = NoDeprecs
297 | DeprecAll DeprecTxt -- Whole module deprecated
298 | DeprecSome (NameEnv (Name,DeprecTxt)) -- Some things deprecated
300 -- We keep the Name in the range, so we can print them out
302 lookupDeprec :: Deprecations -> Name -> Maybe DeprecTxt
303 lookupDeprec NoDeprecs name = Nothing
304 lookupDeprec (DeprecAll txt) name = Just txt
305 lookupDeprec (DeprecSome env) name = case lookupNameEnv env name of
306 Just (_, txt) -> Just txt
309 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
311 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
317 type Avails = [AvailInfo]
318 type AvailInfo = GenAvailInfo Name
319 type RdrAvailInfo = GenAvailInfo OccName
321 data GenAvailInfo name = Avail name -- An ordinary identifier
322 | AvailTC name -- The name of the type or class
323 [name] -- The available pieces of type/class.
324 -- NB: If the type or class is itself
325 -- to be in scope, it must be in this list.
326 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
328 -- Equality used when deciding if the interface has changed
330 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
334 %************************************************************************
336 \subsection{ModIface}
338 %************************************************************************
341 type WhetherHasOrphans = Bool
343 -- * an instance decl in a module other than the defn module for
344 -- one of the tycons or classes in the instance head
345 -- * a transformation rule in a module other than the one defining
346 -- the function in the head of the rule.
348 type IsBootInterface = Bool
350 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
352 data WhatsImported name = NothingAtAll -- The module is below us in the
353 -- hierarchy, but we import nothing
355 | Everything Version -- Used for modules from other packages;
356 -- we record only the module's version number
359 Version -- Module version
360 (Maybe Version) -- Export-list version, if we depend on it
361 [(name,Version)] -- List guaranteed non-empty
362 Version -- Rules version
365 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
366 -- the module". If you use anything in the module you get its rule version
367 -- So if the rules change, you'll recompile, even if you don't use them.
368 -- This is easy to implement, and it's safer: you might not have used the rules last
369 -- time round, but if someone has added a new rule you might need it this time
371 -- The export list field is (Just v) if we depend on the export list:
372 -- we imported the module without saying exactly what we imported
373 -- We need to recompile if the module exports changes, because we might
374 -- now have a name clash in the importing module.
378 %************************************************************************
380 \subsection{The persistent compiler state}
382 %************************************************************************
385 data PersistentCompilerState
387 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
388 -- the mi_decls component is empty
390 pcs_PTE :: PackageTypeEnv, -- Domain = non-home-package modules
391 -- except that the InstEnv components is empty
393 pcs_insts :: PackageInstEnv, -- The total InstEnv accumulated from all
394 -- the non-home-package modules
396 pcs_rules :: PackageRuleBase, -- Ditto RuleEnv
398 pcs_PRS :: PersistentRenamerState
403 The @PersistentRenamerState@ persists across successive calls to the
407 * A name supply, which deals with allocating unique names to
408 (Module,OccName) original names,
410 * An accumulated TypeEnv from all the modules in imported packages
412 * An accumulated InstEnv from all the modules in imported packages
413 The point is that we don't want to keep recreating it whenever
414 we compile a new module. The InstEnv component of pcPST is empty.
415 (This means we might "see" instances that we shouldn't "really" see;
416 but the Haskell Report is vague on what is meant to be visible,
417 so we just take the easy road here.)
421 * A "holding pen" for declarations that have been read out of
422 interface files but not yet sucked in, renamed, and typechecked
425 type PackageTypeEnv = TypeEnv
426 type PackageRuleBase = RuleBase
427 type PackageInstEnv = InstEnv
429 data PersistentRenamerState
430 = PRS { prsOrig :: OrigNameEnv,
431 prsDecls :: DeclsMap,
432 prsInsts :: IfaceInsts,
433 prsRules :: IfaceRules,
438 The OrigNameEnv makes sure that there is just one Unique assigned for
439 each original name; i.e. (module-name, occ-name) pair. The Name is
440 always stored as a Global, and has the SrcLoc of its binding location.
441 Actually that's not quite right. When we first encounter the original
442 name, we might not be at its binding site (e.g. we are reading an
443 interface file); so we give it 'noSrcLoc' then. Later, when we find
444 its binding site, we fix it up.
446 Exactly the same is true of the Module stored in the Name. When we first
447 encounter the occurrence, we may not know the details of the module, so
448 we just store junk. Then when we find the binding site, we fix it up.
452 = Orig { origNames :: OrigNameNameEnv,
453 -- Ensures that one original name gets one unique
454 origIParam :: OrigNameIParamEnv
455 -- Ensures that one implicit parameter name gets one unique
458 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
459 type OrigNameIParamEnv = FiniteMap OccName Name
463 A DeclsMap contains a binding for each Name in the declaration
464 including the constructors of a type decl etc. The Bool is True just
468 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameTyClDecl))
470 type IfaceInsts = Bag GatedDecl
471 type IfaceRules = Bag GatedDecl
473 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
477 %************************************************************************
479 \subsection{Provenance and export info}
481 %************************************************************************
483 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
484 one for each module, corresponding to that module's top-level scope.
487 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
488 -- These only get reported on lookup,
489 -- not on construction
492 The "provenance" of something says how it came to be in scope.
496 = LocalDef -- Defined locally
498 | NonLocalDef -- Defined non-locally
502 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
503 instance Eq Provenance where
504 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
506 instance Eq ImportReason where
507 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
509 instance Ord Provenance where
510 compare LocalDef LocalDef = EQ
511 compare LocalDef (NonLocalDef _ _) = LT
512 compare (NonLocalDef _ _) LocalDef = GT
514 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
515 = compare reason1 reason2
517 instance Ord ImportReason where
518 compare ImplicitImport ImplicitImport = EQ
519 compare ImplicitImport (UserImport _ _ _) = LT
520 compare (UserImport _ _ _) ImplicitImport = GT
521 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
522 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
526 = UserImport Module SrcLoc Bool -- Imported from module M on line L
527 -- Note the M may well not be the defining module
529 -- The Bool is true iff the thing was named *explicitly* in the import spec,
530 -- rather than being imported as part of a group; e.g.
533 -- Here, everything imported by B, and the constructors of T
534 -- are not named explicitly; only T is named explicitly.
535 -- This info is used when warning of unused names.
537 | ImplicitImport -- Imported implicitly for some other reason
540 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
541 -- in scope in this module, so print it
542 -- unqualified in error messages
546 hasBetterProv :: Provenance -> Provenance -> Bool
548 -- a local thing over an imported thing
549 -- a user-imported thing over a non-user-imported thing
550 -- an explicitly-imported thing over an implicitly imported thing
551 hasBetterProv LocalDef _ = True
552 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
553 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
554 hasBetterProv _ _ = False
556 pprNameProvenance :: Name -> Provenance -> SDoc
557 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
558 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
559 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
561 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
562 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
564 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc