2 % (c) The University of Glasgow, 2000
4 \section[HscTypes]{Types for the per-module compiler}
10 ModuleLocation(..), showModMsg,
12 ModDetails(..), ModIface(..),
13 HomeSymbolTable, emptySymbolTable,
15 HomeIfaceTable, PackageIfaceTable, emptyIfaceTable,
16 lookupIface, lookupIfaceByModName, getModuleAndVersion,
19 InteractiveContext(..),
21 IfaceDecls, mkIfaceDecls, dcl_tycl, dcl_rules, dcl_insts,
23 VersionInfo(..), initialVersionInfo, lookupVersion,
24 FixityEnv, lookupFixity, collectFixities,
26 TyThing(..), isTyClThing, implicitTyThingIds,
28 TypeEnv, lookupType, mkTypeEnv, emptyTypeEnv,
29 extendTypeEnvList, extendTypeEnvWithIds,
30 typeEnvElts, typeEnvClasses, typeEnvTyCons, typeEnvIds,
32 ImportedModuleInfo, WhetherHasOrphans, ImportVersion, WhatsImported(..),
33 PersistentRenamerState(..), IsBootInterface, DeclsMap,
34 IfaceInsts, IfaceRules, GatedDecl, GatedDecls, GateFn, IsExported,
35 NameSupply(..), OrigNameCache, OrigIParamCache,
36 Avails, AvailEnv, emptyAvailEnv,
37 GenAvailInfo(..), AvailInfo, RdrAvailInfo,
38 ExportItem, RdrExportItem,
39 PersistentCompilerState(..),
41 Deprecations(..), lookupDeprec,
43 InstEnv, ClsInstEnv, DFunId,
44 PackageInstEnv, PackageRuleBase,
46 GlobalRdrEnv, GlobalRdrElt(..), pprGlobalRdrEnv,
47 LocalRdrEnv, extendLocalRdrEnv,
51 Provenance(..), ImportReason(..),
52 pprNameProvenance, hasBetterProv
56 #include "HsVersions.h"
58 import RdrName ( RdrName, RdrNameEnv, addListToRdrEnv,
59 mkRdrUnqual, rdrEnvToList )
60 import Name ( Name, NamedThing, getName, nameOccName, nameModule, nameSrcLoc )
62 import OccName ( OccName )
64 import InstEnv ( InstEnv, ClsInstEnv, DFunId )
65 import Rules ( RuleBase )
66 import CoreSyn ( CoreBind )
68 import Class ( Class, classSelIds )
69 import TyCon ( TyCon, isNewTyCon, tyConGenIds, tyConSelIds, tyConDataCons_maybe )
70 import DataCon ( dataConWorkId, dataConWrapId )
72 import BasicTypes ( Version, initialVersion, Fixity, defaultFixity, IPName )
74 import HsSyn ( DeprecTxt, TyClDecl, tyClDeclName, ifaceRuleDeclName,
76 import RdrHsSyn ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameTyClDecl )
77 import RnHsSyn ( RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl )
79 import CoreSyn ( IdCoreRule )
83 import Maybes ( seqMaybe, orElse, expectJust )
85 import SrcLoc ( SrcLoc, isGoodSrcLoc )
86 import Util ( thenCmp, sortLt )
87 import UniqSupply ( UniqSupply )
88 import Maybe ( fromJust )
91 %************************************************************************
93 \subsection{Which mode we're in
95 %************************************************************************
98 data GhciMode = Batch | Interactive | OneShot
103 %************************************************************************
105 \subsection{Module locations}
107 %************************************************************************
112 ml_hs_file :: Maybe FilePath,
113 ml_hspp_file :: Maybe FilePath, -- path of preprocessed source
114 ml_hi_file :: FilePath,
115 ml_obj_file :: Maybe FilePath
119 instance Outputable ModuleLocation where
122 -- Probably doesn't really belong here, but used in HscMain and InteractiveUI.
124 showModMsg :: Bool -> Module -> ModuleLocation -> String
125 showModMsg use_object mod location =
126 mod_str ++ replicate (max 0 (16 - length mod_str)) ' '
127 ++" ( " ++ expectJust "showModMsg" (ml_hs_file location) ++ ", "
129 then expectJust "showModMsg" (ml_obj_file location)
132 where mod_str = moduleUserString mod
135 For a module in another package, the hs_file and obj_file
136 components of ModuleLocation are undefined.
138 The locations specified by a ModuleLocation may or may not
139 correspond to actual files yet: for example, even if the object
140 file doesn't exist, the ModuleLocation still contains the path to
141 where the object file will reside if/when it is created.
144 %************************************************************************
146 \subsection{Symbol tables and Module details}
148 %************************************************************************
150 A @ModIface@ plus a @ModDetails@ summarises everything we know
151 about a compiled module. The @ModIface@ is the stuff *before* linking,
152 and can be written out to an interface file. (The @ModDetails@ is after
153 linking; it is the "linked" form of the mi_decls field.)
155 When we *read* an interface file, we also construct a @ModIface@ from it,
156 except that the mi_decls part is empty; when reading we consolidate
157 the declarations into a single indexed map in the @PersistentRenamerState@.
162 mi_module :: !Module,
163 mi_package :: !PackageName, -- Which package the module comes from
164 mi_version :: !VersionInfo, -- Module version number
166 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
167 -- NOT STRICT! we fill this field with _|_ sometimes
169 mi_boot :: !IsBootInterface, -- read from an hi-boot file?
171 mi_usages :: [ImportVersion Name],
172 -- Usages; kept sorted so that it's easy to decide
173 -- whether to write a new iface file (changing usages
174 -- doesn't affect the version of this module)
175 -- NOT STRICT! we read this field lazilly from the interface file
177 mi_exports :: ![ExportItem],
178 -- What it exports Kept sorted by (mod,occ), to make
179 -- version comparisons easier
181 mi_globals :: !(Maybe GlobalRdrEnv),
182 -- Its top level environment or Nothing if we read this
183 -- interface from a file.
185 mi_fixities :: !FixityEnv, -- Fixities
186 mi_deprecs :: Deprecations, -- Deprecations
187 -- NOT STRICT! we read this field lazilly from the interface file
189 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
190 -- NOT STRICT! we fill this field with _|_ sometimes
193 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
194 dcl_rules :: [RenamedRuleDecl], -- Sorted
195 dcl_insts :: [RenamedInstDecl] } -- Unsorted
197 mkIfaceDecls :: [RenamedTyClDecl] -> [RenamedRuleDecl] -> [RenamedInstDecl] -> IfaceDecls
198 mkIfaceDecls tycls rules insts
199 = IfaceDecls { dcl_tycl = sortLt lt_tycl tycls,
200 dcl_rules = sortLt lt_rule rules,
203 d1 `lt_tycl` d2 = tyClDeclName d1 < tyClDeclName d2
204 r1 `lt_rule` r2 = ifaceRuleDeclName r1 < ifaceRuleDeclName r2
207 -- typechecker should only look at this, not ModIface
208 -- Should be able to construct ModDetails from mi_decls in ModIface
211 -- The next three fields are created by the typechecker
212 md_types :: !TypeEnv,
213 md_insts :: ![DFunId], -- Dfun-ids for the instances in this module
214 md_rules :: ![IdCoreRule], -- Domain may include Ids from other modules
215 md_binds :: ![CoreBind]
218 -- The ModDetails takes on several slightly different forms:
220 -- After typecheck + desugar
221 -- md_types Contains TyCons, Classes, and implicit Ids
222 -- md_insts All instances from this module (incl derived ones)
223 -- md_rules All rules from this module
224 -- md_binds Desugared bindings
226 -- After simplification
227 -- md_types Same as after typecheck
229 -- md_rules Orphan rules only (local ones now attached to binds)
230 -- md_binds With rules attached
233 -- md_types Now contains Ids as well, replete with final IdInfo
234 -- The Ids are only the ones that are visible from
235 -- importing modules. Without -O that means only
236 -- exported Ids, but with -O importing modules may
237 -- see ids mentioned in unfoldings of exported Ids
239 -- md_insts Same DFunIds as before, but with final IdInfo,
240 -- and the unique might have changed; remember that
241 -- CoreTidy links up the uniques of old and new versions
243 -- md_rules All rules for exported things, substituted with final Ids
247 -- Passed back to compilation manager
248 -- Just as after CoreTidy, but with md_binds nuked
253 emptyModIface :: Module -> ModIface
255 = ModIface { mi_module = mod,
256 mi_package = preludePackage, -- XXX fully bogus
257 mi_version = initialVersionInfo,
262 mi_fixities = emptyNameEnv,
263 mi_globals = Nothing,
264 mi_deprecs = NoDeprecs,
265 mi_decls = panic "emptyModIface: decls"
269 Symbol tables map modules to ModDetails:
272 type SymbolTable = ModuleEnv ModDetails
273 type IfaceTable = ModuleEnv ModIface
275 type HomeIfaceTable = IfaceTable
276 type PackageIfaceTable = IfaceTable
278 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
280 emptySymbolTable :: SymbolTable
281 emptySymbolTable = emptyModuleEnv
283 emptyIfaceTable :: IfaceTable
284 emptyIfaceTable = emptyModuleEnv
287 Simple lookups in the symbol table.
290 lookupIface :: HomeIfaceTable -> PackageIfaceTable -> Name -> Maybe ModIface
291 -- We often have two IfaceTables, and want to do a lookup
292 lookupIface hit pit name
293 = lookupModuleEnv hit mod `seqMaybe` lookupModuleEnv pit mod
295 mod = nameModule name
297 lookupIfaceByModName :: HomeIfaceTable -> PackageIfaceTable -> ModuleName -> Maybe ModIface
298 -- We often have two IfaceTables, and want to do a lookup
299 lookupIfaceByModName hit pit mod
300 = lookupModuleEnvByName hit mod `seqMaybe` lookupModuleEnvByName pit mod
302 -- Use instead of Finder.findModule if possible: this way doesn't
303 -- require filesystem operations, and it is guaranteed not to fail
304 -- when the IfaceTables are properly populated (i.e. after the renamer).
305 getModuleAndVersion :: HomeIfaceTable -> PackageIfaceTable -> ModuleName
307 getModuleAndVersion hit pit mod
308 = ((,) $! mi_module iface) $! vers_module (mi_version iface)
309 where iface = fromJust (lookupIfaceByModName hit pit mod)
313 %************************************************************************
315 \subsection{The interactive context}
317 %************************************************************************
320 data InteractiveContext
321 = InteractiveContext {
322 ic_toplev_scope :: [Module], -- Include the "top-level" scope of
325 ic_exports :: [Module], -- Include just the exports of these
328 ic_rn_gbl_env :: GlobalRdrEnv, -- The cached GlobalRdrEnv, built from
329 -- ic_toplev_scope and ic_exports
331 ic_print_unqual :: PrintUnqualified,
332 -- cached PrintUnqualified, as above
334 ic_rn_local_env :: LocalRdrEnv, -- Lexical context for variables bound
335 -- during interaction
337 ic_type_env :: TypeEnv -- Ditto for types
342 %************************************************************************
344 \subsection{Type environment stuff}
346 %************************************************************************
349 data TyThing = AnId Id
353 isTyClThing :: TyThing -> Bool
354 isTyClThing (ATyCon _) = True
355 isTyClThing (AClass _) = True
356 isTyClThing (AnId _) = False
358 instance NamedThing TyThing where
359 getName (AnId id) = getName id
360 getName (ATyCon tc) = getName tc
361 getName (AClass cl) = getName cl
363 instance Outputable TyThing where
364 ppr (AnId id) = ptext SLIT("AnId") <+> ppr id
365 ppr (ATyCon tc) = ptext SLIT("ATyCon") <+> ppr tc
366 ppr (AClass cl) = ptext SLIT("AClass") <+> ppr cl
369 typeEnvElts :: TypeEnv -> [TyThing]
370 typeEnvClasses :: TypeEnv -> [Class]
371 typeEnvTyCons :: TypeEnv -> [TyCon]
372 typeEnvIds :: TypeEnv -> [Id]
374 typeEnvElts env = nameEnvElts env
375 typeEnvClasses env = [cl | AClass cl <- typeEnvElts env]
376 typeEnvTyCons env = [tc | ATyCon tc <- typeEnvElts env]
377 typeEnvIds env = [id | AnId id <- typeEnvElts env]
379 implicitTyThingIds :: [TyThing] -> [Id]
380 -- Add the implicit data cons and selectors etc
381 implicitTyThingIds things
382 = concat (map go things)
385 go (AClass cl) = classSelIds cl
386 go (ATyCon tc) = tyConGenIds tc ++
388 [ n | dc <- tyConDataCons_maybe tc `orElse` [],
389 n <- implicitConIds tc dc]
390 -- Synonyms return empty list of constructors and selectors
392 implicitConIds tc dc -- Newtypes have a constructor wrapper,
394 | isNewTyCon tc = [dataConWrapId dc]
395 | otherwise = [dataConWorkId dc, dataConWrapId dc]
400 type TypeEnv = NameEnv TyThing
402 emptyTypeEnv = emptyNameEnv
404 mkTypeEnv :: [TyThing] -> TypeEnv
405 mkTypeEnv things = extendTypeEnvList emptyTypeEnv things
407 extendTypeEnvList :: TypeEnv -> [TyThing] -> TypeEnv
408 extendTypeEnvList env things
409 = extendNameEnvList env [(getName thing, thing) | thing <- things]
411 extendTypeEnvWithIds :: TypeEnv -> [Id] -> TypeEnv
412 extendTypeEnvWithIds env ids
413 = extendNameEnvList env [(getName id, AnId id) | id <- ids]
417 lookupType :: HomeSymbolTable -> PackageTypeEnv -> Name -> Maybe TyThing
418 lookupType hst pte name
419 = case lookupModuleEnv hst (nameModule name) of
420 Just details -> lookupNameEnv (md_types details) name
421 Nothing -> lookupNameEnv pte name
424 %************************************************************************
426 \subsection{Auxiliary types}
428 %************************************************************************
430 These types are defined here because they are mentioned in ModDetails,
431 but they are mostly elaborated elsewhere
436 vers_module :: Version, -- Changes when anything changes
437 vers_exports :: Version, -- Changes when export list changes
438 vers_rules :: Version, -- Changes when any rule changes
439 vers_decls :: NameEnv Version
440 -- Versions for "big" names only (not data constructors, class ops)
441 -- The version of an Id changes if its fixity changes
442 -- Ditto data constructors, class operations, except that the version of
443 -- the parent class/tycon changes
445 -- If a name isn't in the map, it means 'initialVersion'
448 initialVersionInfo :: VersionInfo
449 initialVersionInfo = VersionInfo { vers_module = initialVersion,
450 vers_exports = initialVersion,
451 vers_rules = initialVersion,
452 vers_decls = emptyNameEnv
455 lookupVersion :: NameEnv Version -> Name -> Version
456 lookupVersion env name = lookupNameEnv env name `orElse` initialVersion
458 data Deprecations = NoDeprecs
459 | DeprecAll DeprecTxt -- Whole module deprecated
460 | DeprecSome (NameEnv (Name,DeprecTxt)) -- Some things deprecated
462 -- We keep the Name in the range, so we can print them out
464 lookupDeprec :: Deprecations -> Name -> Maybe DeprecTxt
465 lookupDeprec NoDeprecs name = Nothing
466 lookupDeprec (DeprecAll txt) name = Just txt
467 lookupDeprec (DeprecSome env) name = case lookupNameEnv env name of
468 Just (_, txt) -> Just txt
471 instance Eq Deprecations where
472 -- Used when checking whether we need write a new interface
473 NoDeprecs == NoDeprecs = True
474 (DeprecAll t1) == (DeprecAll t2) = t1 == t2
475 (DeprecSome e1) == (DeprecSome e2) = nameEnvElts e1 == nameEnvElts e2
481 type Avails = [AvailInfo]
482 type AvailInfo = GenAvailInfo Name
483 type RdrAvailInfo = GenAvailInfo OccName
485 data GenAvailInfo name = Avail name -- An ordinary identifier
486 | AvailTC name -- The name of the type or class
487 [name] -- The available pieces of type/class.
488 -- NB: If the type or class is itself
489 -- to be in scope, it must be in this list.
490 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
492 -- Equality used when deciding if the interface has changed
494 type RdrExportItem = (ModuleName, [RdrAvailInfo])
495 type ExportItem = (ModuleName, [AvailInfo])
497 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
499 emptyAvailEnv :: AvailEnv
500 emptyAvailEnv = emptyNameEnv
502 instance Outputable n => Outputable (GenAvailInfo n) where
505 pprAvail :: Outputable n => GenAvailInfo n -> SDoc
506 pprAvail (AvailTC n ns) = ppr n <> case {- filter (/= n) -} ns of
508 ns' -> braces (hsep (punctuate comma (map ppr ns')))
510 pprAvail (Avail n) = ppr n
514 type FixityEnv = NameEnv Fixity
516 lookupFixity :: FixityEnv -> Name -> Fixity
517 lookupFixity env n = lookupNameEnv env n `orElse` defaultFixity
519 collectFixities :: FixityEnv -> [TyClDecl Name pat] -> [(Name,Fixity)]
520 collectFixities env decls
522 | d <- decls, (n,_) <- tyClDeclNames d,
523 Just fix <- [lookupNameEnv env n]
528 %************************************************************************
530 \subsection{WhatsImported}
532 %************************************************************************
535 type WhetherHasOrphans = Bool
537 -- * an instance decl in a module other than the defn module for
538 -- one of the tycons or classes in the instance head
539 -- * a transformation rule in a module other than the one defining
540 -- the function in the head of the rule.
542 type IsBootInterface = Bool
544 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
546 data WhatsImported name = NothingAtAll -- The module is below us in the
547 -- hierarchy, but we import nothing
549 | Everything Version -- Used for modules from other packages;
550 -- we record only the module's version number
553 Version -- Module version
554 (Maybe Version) -- Export-list version, if we depend on it
555 [(name,Version)] -- List guaranteed non-empty
556 Version -- Rules version
559 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
560 -- the module". If you use anything in the module you get its rule version
561 -- So if the rules change, you'll recompile, even if you don't use them.
562 -- This is easy to implement, and it's safer: you might not have used the rules last
563 -- time round, but if someone has added a new rule you might need it this time
565 -- The export list field is (Just v) if we depend on the export list:
566 -- we imported the module without saying exactly what we imported
567 -- We need to recompile if the module exports changes, because we might
568 -- now have a name clash in the importing module.
570 type IsExported = Name -> Bool -- True for names that are exported from this module
574 %************************************************************************
576 \subsection{The persistent compiler state}
578 %************************************************************************
580 The @PersistentCompilerState@ persists across successive calls to the
583 * A ModIface for each non-home-package module
585 * An accumulated TypeEnv from all the modules in imported packages
587 * An accumulated InstEnv from all the modules in imported packages
588 The point is that we don't want to keep recreating it whenever
589 we compile a new module. The InstEnv component of pcPST is empty.
590 (This means we might "see" instances that we shouldn't "really" see;
591 but the Haskell Report is vague on what is meant to be visible,
592 so we just take the easy road here.)
596 * The persistent renamer state
599 data PersistentCompilerState
601 pcs_PIT :: !PackageIfaceTable, -- Domain = non-home-package modules
602 -- the mi_decls component is empty
604 pcs_PTE :: !PackageTypeEnv, -- Domain = non-home-package modules
605 -- except that the InstEnv components is empty
607 pcs_insts :: !PackageInstEnv, -- The total InstEnv accumulated from all
608 -- the non-home-package modules
610 pcs_rules :: !PackageRuleBase, -- Ditto RuleEnv
612 pcs_PRS :: !PersistentRenamerState
617 The persistent renamer state contains:
619 * A name supply, which deals with allocating unique names to
620 (Module,OccName) original names,
622 * A "holding pen" for declarations that have been read out of
623 interface files but not yet sucked in, renamed, and typechecked
626 type PackageTypeEnv = TypeEnv
627 type PackageRuleBase = RuleBase
628 type PackageInstEnv = InstEnv
630 data PersistentRenamerState
631 = PRS { prsOrig :: !NameSupply,
632 prsImpMods :: !ImportedModuleInfo,
634 -- Holding pens for stuff that has been read in
635 -- but not yet slurped into the renamer
636 prsDecls :: !DeclsMap,
637 prsInsts :: !IfaceInsts,
638 prsRules :: !IfaceRules
642 The NameSupply makes sure that there is just one Unique assigned for
643 each original name; i.e. (module-name, occ-name) pair. The Name is
644 always stored as a Global, and has the SrcLoc of its binding location.
645 Actually that's not quite right. When we first encounter the original
646 name, we might not be at its binding site (e.g. we are reading an
647 interface file); so we give it 'noSrcLoc' then. Later, when we find
648 its binding site, we fix it up.
650 Exactly the same is true of the Module stored in the Name. When we first
651 encounter the occurrence, we may not know the details of the module, so
652 we just store junk. Then when we find the binding site, we fix it up.
656 = NameSupply { nsUniqs :: UniqSupply,
658 nsNames :: OrigNameCache,
659 -- Ensures that one original name gets one unique
660 nsIPs :: OrigIParamCache
661 -- Ensures that one implicit parameter name gets one unique
664 type OrigNameCache = FiniteMap (ModuleName,OccName) Name
665 type OrigIParamCache = FiniteMap (IPName RdrName) (IPName Name)
668 @ImportedModuleInfo@ contains info ONLY about modules that have not yet
669 been loaded into the iPIT. These modules are mentioned in interfaces we've
670 already read, so we know a tiny bit about them, but we havn't yet looked
671 at the interface file for the module itself. It needs to persist across
672 invocations of the renamer, at least from Rename.checkOldIface to Rename.renameSource.
673 And there's no harm in it persisting across multiple compilations.
676 type ImportedModuleInfo = FiniteMap ModuleName (WhetherHasOrphans, IsBootInterface)
679 A DeclsMap contains a binding for each Name in the declaration
680 including the constructors of a type decl etc. The Bool is True just
684 type DeclsMap = (NameEnv (AvailInfo, Bool, (Module, RdrNameTyClDecl)), Int)
685 -- The Int says how many have been sucked in
687 type IfaceInsts = GatedDecls RdrNameInstDecl
688 type IfaceRules = GatedDecls RdrNameRuleDecl
690 type GatedDecls d = (Bag (GatedDecl d), Int) -- The Int says how many have been sucked in
691 type GatedDecl d = (GateFn, (Module, d))
692 type GateFn = (Name -> Bool) -> Bool -- Returns True <=> gate is open
693 -- The (Name -> Bool) fn returns True for visible Names
694 -- For example, suppose this is in an interface file
695 -- instance C T where ...
696 -- We want to slurp this decl if both C and T are "visible" in
697 -- the importing module. See "The gating story" in RnIfaces for details.
701 %************************************************************************
703 \subsection{Provenance and export info}
705 %************************************************************************
707 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
710 type LocalRdrEnv = RdrNameEnv Name
712 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
713 extendLocalRdrEnv env names
714 = addListToRdrEnv env [(mkRdrUnqual (nameOccName n), n) | n <- names]
717 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
718 one for each module, corresponding to that module's top-level scope.
721 type GlobalRdrEnv = RdrNameEnv [GlobalRdrElt]
722 -- The list is because there may be name clashes
723 -- These only get reported on lookup, not on construction
725 data GlobalRdrElt = GRE Name Provenance (Maybe DeprecTxt)
726 -- The Maybe DeprecTxt tells whether this name is deprecated
729 = vcat (map pp (rdrEnvToList env))
731 pp (rn, nps) = ppr rn <> colon <+>
732 vcat [ppr n <+> pprNameProvenance n p | (GRE n p _) <- nps]
735 The "provenance" of something says how it came to be in scope.
739 = LocalDef -- Defined locally
741 | NonLocalDef -- Defined non-locally
744 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
745 instance Eq Provenance where
746 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
748 instance Eq ImportReason where
749 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
751 instance Ord Provenance where
752 compare LocalDef LocalDef = EQ
753 compare LocalDef (NonLocalDef _) = LT
754 compare (NonLocalDef _) LocalDef = GT
756 compare (NonLocalDef reason1) (NonLocalDef reason2)
757 = compare reason1 reason2
759 instance Ord ImportReason where
760 compare ImplicitImport ImplicitImport = EQ
761 compare ImplicitImport (UserImport _ _ _) = LT
762 compare (UserImport _ _ _) ImplicitImport = GT
763 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
764 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
768 = UserImport Module SrcLoc Bool -- Imported from module M on line L
769 -- Note the M may well not be the defining module
771 -- The Bool is true iff the thing was named *explicitly* in the import spec,
772 -- rather than being imported as part of a group; e.g.
775 -- Here, everything imported by B, and the constructors of T
776 -- are not named explicitly; only T is named explicitly.
777 -- This info is used when warning of unused names.
779 | ImplicitImport -- Imported implicitly for some other reason
783 hasBetterProv :: Provenance -> Provenance -> Bool
785 -- a local thing over an imported thing
786 -- a user-imported thing over a non-user-imported thing
787 -- an explicitly-imported thing over an implicitly imported thing
788 hasBetterProv LocalDef _ = True
789 hasBetterProv (NonLocalDef (UserImport _ _ _ )) (NonLocalDef ImplicitImport) = True
790 hasBetterProv _ _ = False
792 pprNameProvenance :: Name -> Provenance -> SDoc
793 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
794 pprNameProvenance name (NonLocalDef why) = sep [ppr_reason why,
795 nest 2 (ppr_defn (nameSrcLoc name))]
797 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
798 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
800 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("at") <+> ppr loc)