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 lookupIface, lookupIfaceByModName,
16 IfaceDecls, mkIfaceDecls, dcl_tycl, dcl_rules, dcl_insts,
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, NamedThing, isLocallyDefined,
49 getName, nameModule, nameSrcLoc )
51 import OccName ( OccName )
52 import Module ( Module, ModuleName, ModuleEnv,
53 lookupModuleEnv, lookupModuleEnvByName, emptyModuleEnv
55 import InstEnv ( InstEnv, ClsInstEnv, DFunId )
56 import Rules ( RuleBase )
58 import Class ( Class )
59 import TyCon ( TyCon )
61 import BasicTypes ( Version, initialVersion, Fixity )
63 import HsSyn ( DeprecTxt, tyClDeclName, ifaceRuleDeclName )
64 import RdrHsSyn ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameTyClDecl )
65 import RnHsSyn ( RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl )
67 import CoreSyn ( IdCoreRule )
69 import FiniteMap ( FiniteMap )
71 import Maybes ( seqMaybe )
73 import SrcLoc ( SrcLoc, isGoodSrcLoc )
74 import Util ( thenCmp, sortLt )
75 import UniqSupply ( UniqSupply )
78 %************************************************************************
80 \subsection{Module locations}
82 %************************************************************************
87 ml_hs_file :: Maybe FilePath,
88 ml_hspp_file :: Maybe FilePath, -- path of preprocessed source
89 ml_hi_file :: Maybe FilePath,
90 ml_obj_file :: Maybe FilePath
94 instance Outputable ModuleLocation where
98 For a module in another package, the hs_file and obj_file
99 components of ModuleLocation are undefined.
101 The locations specified by a ModuleLocation may or may not
102 correspond to actual files yet: for example, even if the object
103 file doesn't exist, the ModuleLocation still contains the path to
104 where the object file will reside if/when it is created.
107 %************************************************************************
109 \subsection{Symbol tables and Module details}
111 %************************************************************************
113 A @ModIface@ plus a @ModDetails@ summarises everything we know
114 about a compiled module. The @ModIface@ is the stuff *before* linking,
115 and can be written out to an interface file. The @ModDetails@ is after
116 linking; it is the "linked" form of the mi_decls field.
121 mi_module :: Module, -- Complete with package info
122 mi_version :: VersionInfo, -- Module version number
123 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
124 mi_boot :: IsBootInterface, -- Whether this interface was read from an hi-boot file
126 mi_usages :: [ImportVersion Name], -- Usages; kept sorted so that it's easy
127 -- to decide whether to write a new iface file
128 -- (changing usages doesn't affect the version of
131 mi_exports :: [(ModuleName,Avails)], -- What it exports
132 -- Kept sorted by (mod,occ),
133 -- to make version comparisons easier
135 mi_globals :: GlobalRdrEnv, -- Its top level environment
137 mi_fixities :: NameEnv Fixity, -- Fixities
138 mi_deprecs :: Deprecations, -- Deprecations
140 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
143 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
144 dcl_rules :: [RenamedRuleDecl], -- Sorted
145 dcl_insts :: [RenamedInstDecl] } -- Unsorted
147 mkIfaceDecls :: [RenamedTyClDecl] -> [RenamedRuleDecl] -> [RenamedInstDecl] -> IfaceDecls
148 mkIfaceDecls tycls rules insts
149 = IfaceDecls { dcl_tycl = sortLt lt_tycl tycls,
150 dcl_rules = sortLt lt_rule rules,
153 d1 `lt_tycl` d2 = nameOccName (tyClDeclName d1) < nameOccName (tyClDeclName d2)
154 r1 `lt_rule` r2 = nameOccName (ifaceRuleDeclName r1) < nameOccName (ifaceRuleDeclName r2)
156 -- I wanted to sort just by the Name, but there's a problem: we are comparing
157 -- the old version of an interface with the new version. The latter will use
158 -- local names like 'lvl23' that were constructed not by the renamer but by
159 -- the simplifier. So the unqiues aren't going to line up.
161 -- It's ok to compare by OccName because this comparison only drives the
162 -- computation of new version numbers.
164 -- Better solutions: Compare in a way that is insensitive to the name used
165 -- for local things. This would decrease the wobbles due
166 -- to 'lvl23' changing to 'lvl24'.
168 -- NB: there's a related comparision on MkIface.diffDecls!
173 -- typechecker should only look at this, not ModIface
174 -- Should be able to construct ModDetails from mi_decls in ModIface
177 -- The next three fields are created by the typechecker
179 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
180 md_rules :: [IdCoreRule] -- Domain may include Ids from other modules
185 emptyModDetails :: ModDetails
187 = ModDetails { md_types = emptyTypeEnv,
192 emptyModIface :: Module -> ModIface
194 = ModIface { mi_module = mod,
195 mi_version = initialVersionInfo,
200 mi_fixities = emptyNameEnv,
201 mi_globals = emptyRdrEnv,
202 mi_deprecs = NoDeprecs,
203 mi_decls = panic "emptyModIface: decls"
207 Symbol tables map modules to ModDetails:
210 type SymbolTable = ModuleEnv ModDetails
211 type IfaceTable = ModuleEnv ModIface
213 type HomeIfaceTable = IfaceTable
214 type PackageIfaceTable = IfaceTable
216 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
218 emptyIfaceTable :: IfaceTable
219 emptyIfaceTable = emptyModuleEnv
222 Simple lookups in the symbol table.
225 lookupIface :: HomeIfaceTable -> PackageIfaceTable
226 -> Module -> Name -- The module is to use for locally-defined names
228 -- We often have two IfaceTables, and want to do a lookup
229 lookupIface hit pit this_mod name
230 | isLocallyDefined name = lookupModuleEnv hit this_mod
231 | otherwise = lookupModuleEnv hit mod `seqMaybe` lookupModuleEnv pit mod
233 mod = nameModule name
235 lookupIfaceByModName :: ModuleEnv a -> ModuleEnv a -> ModuleName -> Maybe a
236 -- We often have two Symbol- or IfaceTables, and want to do a lookup
237 lookupIfaceByModName ht pt mod
238 = lookupModuleEnvByName ht mod `seqMaybe` lookupModuleEnvByName pt mod
242 %************************************************************************
244 \subsection{Type environment stuff}
246 %************************************************************************
249 data TyThing = AnId Id
253 isTyClThing :: TyThing -> Bool
254 isTyClThing (ATyCon _) = True
255 isTyClThing (AClass _) = True
256 isTyClThing (AnId _) = False
258 instance NamedThing TyThing where
259 getName (AnId id) = getName id
260 getName (ATyCon tc) = getName tc
261 getName (AClass cl) = getName cl
263 typeEnvClasses env = [cl | AClass cl <- nameEnvElts env]
264 typeEnvTyCons env = [tc | ATyCon tc <- nameEnvElts env]
270 type TypeEnv = NameEnv TyThing
272 emptyTypeEnv = emptyNameEnv
274 mkTypeEnv :: [TyThing] -> TypeEnv
275 mkTypeEnv things = extendTypeEnvList emptyTypeEnv things
277 extendTypeEnvList :: TypeEnv -> [TyThing] -> TypeEnv
278 extendTypeEnvList env things
279 = foldl add_thing env things
281 add_thing :: TypeEnv -> TyThing -> TypeEnv
282 add_thing env thing = extendNameEnv env (getName thing) thing
286 lookupType :: HomeSymbolTable -> PackageTypeEnv -> Name -> Maybe TyThing
287 lookupType hst pte name
288 = ASSERT2( not (isLocallyDefined name), ppr name )
289 case lookupModuleEnv hst (nameModule name) of
290 Just details -> lookupNameEnv (md_types details) name
291 Nothing -> lookupNameEnv pte name
294 %************************************************************************
296 \subsection{Auxiliary types}
298 %************************************************************************
300 These types are defined here because they are mentioned in ModDetails,
301 but they are mostly elaborated elsewhere
306 vers_module :: Version, -- Changes when anything changes
307 vers_exports :: Version, -- Changes when export list changes
308 vers_rules :: Version, -- Changes when any rule changes
309 vers_decls :: NameEnv Version
310 -- Versions for "big" names only (not data constructors, class ops)
311 -- The version of an Id changes if its fixity changes
312 -- Ditto data constructors, class operations, except that the version of
313 -- the parent class/tycon changes
316 initialVersionInfo :: VersionInfo
317 initialVersionInfo = VersionInfo { vers_module = initialVersion,
318 vers_exports = initialVersion,
319 vers_rules = initialVersion,
320 vers_decls = emptyNameEnv }
322 data Deprecations = NoDeprecs
323 | DeprecAll DeprecTxt -- Whole module deprecated
324 | DeprecSome (NameEnv (Name,DeprecTxt)) -- Some things deprecated
326 -- We keep the Name in the range, so we can print them out
328 lookupDeprec :: Deprecations -> Name -> Maybe DeprecTxt
329 lookupDeprec NoDeprecs name = Nothing
330 lookupDeprec (DeprecAll txt) name = Just txt
331 lookupDeprec (DeprecSome env) name = case lookupNameEnv env name of
332 Just (_, txt) -> Just txt
338 type Avails = [AvailInfo]
339 type AvailInfo = GenAvailInfo Name
340 type RdrAvailInfo = GenAvailInfo OccName
342 data GenAvailInfo name = Avail name -- An ordinary identifier
343 | AvailTC name -- The name of the type or class
344 [name] -- The available pieces of type/class.
345 -- NB: If the type or class is itself
346 -- to be in scope, it must be in this list.
347 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
349 -- Equality used when deciding if the interface has changed
351 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
353 instance Outputable n => Outputable (GenAvailInfo n) where
356 pprAvail :: Outputable n => GenAvailInfo n -> SDoc
357 pprAvail (AvailTC n ns) = ppr n <> case {- filter (/= n) -} ns of
359 ns' -> braces (hsep (punctuate comma (map ppr ns')))
361 pprAvail (Avail n) = ppr n
365 %************************************************************************
367 \subsection{ModIface}
369 %************************************************************************
372 type WhetherHasOrphans = Bool
374 -- * an instance decl in a module other than the defn module for
375 -- one of the tycons or classes in the instance head
376 -- * a transformation rule in a module other than the one defining
377 -- the function in the head of the rule.
379 type IsBootInterface = Bool
381 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
383 data WhatsImported name = NothingAtAll -- The module is below us in the
384 -- hierarchy, but we import nothing
386 | Everything Version -- Used for modules from other packages;
387 -- we record only the module's version number
390 Version -- Module version
391 (Maybe Version) -- Export-list version, if we depend on it
392 [(name,Version)] -- List guaranteed non-empty
393 Version -- Rules version
396 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
397 -- the module". If you use anything in the module you get its rule version
398 -- So if the rules change, you'll recompile, even if you don't use them.
399 -- This is easy to implement, and it's safer: you might not have used the rules last
400 -- time round, but if someone has added a new rule you might need it this time
402 -- The export list field is (Just v) if we depend on the export list:
403 -- we imported the module without saying exactly what we imported
404 -- We need to recompile if the module exports changes, because we might
405 -- now have a name clash in the importing module.
409 %************************************************************************
411 \subsection{The persistent compiler state}
413 %************************************************************************
416 data PersistentCompilerState
418 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
419 -- the mi_decls component is empty
421 pcs_PTE :: PackageTypeEnv, -- Domain = non-home-package modules
422 -- except that the InstEnv components is empty
424 pcs_insts :: PackageInstEnv, -- The total InstEnv accumulated from all
425 -- the non-home-package modules
427 pcs_rules :: PackageRuleBase, -- Ditto RuleEnv
429 pcs_PRS :: PersistentRenamerState
434 The @PersistentRenamerState@ persists across successive calls to the
438 * A name supply, which deals with allocating unique names to
439 (Module,OccName) original names,
441 * An accumulated TypeEnv from all the modules in imported packages
443 * An accumulated InstEnv from all the modules in imported packages
444 The point is that we don't want to keep recreating it whenever
445 we compile a new module. The InstEnv component of pcPST is empty.
446 (This means we might "see" instances that we shouldn't "really" see;
447 but the Haskell Report is vague on what is meant to be visible,
448 so we just take the easy road here.)
452 * A "holding pen" for declarations that have been read out of
453 interface files but not yet sucked in, renamed, and typechecked
456 type PackageTypeEnv = TypeEnv
457 type PackageRuleBase = RuleBase
458 type PackageInstEnv = InstEnv
460 data PersistentRenamerState
461 = PRS { prsOrig :: OrigNameEnv,
462 prsDecls :: DeclsMap,
463 prsInsts :: IfaceInsts,
464 prsRules :: IfaceRules,
469 The OrigNameEnv makes sure that there is just one Unique assigned for
470 each original name; i.e. (module-name, occ-name) pair. The Name is
471 always stored as a Global, and has the SrcLoc of its binding location.
472 Actually that's not quite right. When we first encounter the original
473 name, we might not be at its binding site (e.g. we are reading an
474 interface file); so we give it 'noSrcLoc' then. Later, when we find
475 its binding site, we fix it up.
477 Exactly the same is true of the Module stored in the Name. When we first
478 encounter the occurrence, we may not know the details of the module, so
479 we just store junk. Then when we find the binding site, we fix it up.
483 = Orig { origNames :: OrigNameNameEnv,
484 -- Ensures that one original name gets one unique
485 origIParam :: OrigNameIParamEnv
486 -- Ensures that one implicit parameter name gets one unique
489 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
490 type OrigNameIParamEnv = FiniteMap OccName Name
494 A DeclsMap contains a binding for each Name in the declaration
495 including the constructors of a type decl etc. The Bool is True just
499 type DeclsMap = (NameEnv (AvailInfo, Bool, (Module, RdrNameTyClDecl)), Int)
500 -- The Int says how many have been sucked in
502 type IfaceInsts = GatedDecls RdrNameInstDecl
503 type IfaceRules = GatedDecls RdrNameRuleDecl
505 type GatedDecls d = (Bag (GatedDecl d), Int) -- The Int says how many have been sucked in
506 type GatedDecl d = ([Name], (Module, d))
510 %************************************************************************
512 \subsection{Provenance and export info}
514 %************************************************************************
516 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
517 one for each module, corresponding to that module's top-level scope.
520 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
521 -- These only get reported on lookup,
522 -- not on construction
525 The "provenance" of something says how it came to be in scope.
529 = LocalDef -- Defined locally
531 | NonLocalDef -- Defined non-locally
535 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
536 instance Eq Provenance where
537 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
539 instance Eq ImportReason where
540 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
542 instance Ord Provenance where
543 compare LocalDef LocalDef = EQ
544 compare LocalDef (NonLocalDef _ _) = LT
545 compare (NonLocalDef _ _) LocalDef = GT
547 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
548 = compare reason1 reason2
550 instance Ord ImportReason where
551 compare ImplicitImport ImplicitImport = EQ
552 compare ImplicitImport (UserImport _ _ _) = LT
553 compare (UserImport _ _ _) ImplicitImport = GT
554 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
555 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
559 = UserImport Module SrcLoc Bool -- Imported from module M on line L
560 -- Note the M may well not be the defining module
562 -- The Bool is true iff the thing was named *explicitly* in the import spec,
563 -- rather than being imported as part of a group; e.g.
566 -- Here, everything imported by B, and the constructors of T
567 -- are not named explicitly; only T is named explicitly.
568 -- This info is used when warning of unused names.
570 | ImplicitImport -- Imported implicitly for some other reason
573 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
574 -- in scope in this module, so print it
575 -- unqualified in error messages
579 hasBetterProv :: Provenance -> Provenance -> Bool
581 -- a local thing over an imported thing
582 -- a user-imported thing over a non-user-imported thing
583 -- an explicitly-imported thing over an implicitly imported thing
584 hasBetterProv LocalDef _ = True
585 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
586 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
587 hasBetterProv _ _ = False
589 pprNameProvenance :: Name -> Provenance -> SDoc
590 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
591 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
592 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
594 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
595 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
597 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc