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,
16 VersionInfo(..), initialVersionInfo,
18 TyThing(..), groupTyThings,
20 TypeEnv, extendTypeEnv, lookupTypeEnv,
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,
35 GlobalRdrEnv, RdrAvailInfo,
38 Provenance(..), ImportReason(..), PrintUnqualified,
39 pprNameProvenance, hasBetterProv
43 #include "HsVersions.h"
45 import RdrName ( RdrNameEnv, emptyRdrEnv )
46 import Name ( Name, NameEnv, NamedThing,
47 emptyNameEnv, unitNameEnv, extendNameEnv, plusNameEnv,
48 lookupNameEnv, emptyNameEnv, getName, nameModule,
50 import NameSet ( NameSet )
51 import OccName ( OccName )
52 import Module ( Module, ModuleName, ModuleEnv,
54 import VarSet ( TyVarSet )
55 import VarEnv ( emptyVarEnv )
57 import Class ( Class )
58 import TyCon ( TyCon )
60 import BasicTypes ( Version, initialVersion, Fixity )
62 import HsSyn ( DeprecTxt )
63 import RdrHsSyn ( RdrNameHsDecl )
64 import RnHsSyn ( RenamedTyClDecl, RenamedIfaceSig, RenamedRuleDecl, RenamedInstDecl )
66 import CoreSyn ( CoreRule )
69 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
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
123 mi_exports :: Avails, -- What it exports
124 -- Kept sorted by (mod,occ),
125 -- to make version comparisons easier
127 mi_globals :: GlobalRdrEnv, -- Its top level environment
129 mi_fixities :: NameEnv Fixity, -- Fixities
130 mi_deprecs :: Deprecations, -- Deprecations
132 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
135 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
136 dcl_sigs :: [RenamedIfaceSig], -- Sorted
137 dcl_rules :: [RenamedRuleDecl], -- Sorted
138 dcl_insts :: [RenamedInstDecl] } -- Unsorted
140 -- typechecker should only look at this, not ModIface
141 -- Should be able to construct ModDetails from mi_decls in ModIface
144 -- The next three fields are created by the typechecker
146 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
147 md_rules :: RuleEnv -- Domain may include Ids from other modules
152 emptyModDetails :: ModDetails
154 = ModDetails { md_types = emptyTypeEnv,
156 md_rules = emptyRuleEnv
159 emptyModIface :: Module -> ModIface
161 = ModIface { mi_module = mod,
163 mi_globals = emptyRdrEnv,
164 mi_deprecs = NoDeprecs
168 Symbol tables map modules to ModDetails:
171 type SymbolTable = ModuleEnv ModDetails
172 type IfaceTable = ModuleEnv ModIface
174 type HomeIfaceTable = IfaceTable
175 type PackageIfaceTable = IfaceTable
177 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
178 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
179 type GlobalSymbolTable = SymbolTable -- Domain = all modules
182 Simple lookups in the symbol table.
185 lookupFixityEnv :: IfaceTable -> Name -> Maybe Fixity
186 lookupFixityEnv tbl name
187 = case lookupModuleEnv tbl (nameModule name) of
189 Just details -> lookupNameEnv (mi_fixities details) name
193 %************************************************************************
195 \subsection{Type environment stuff}
197 %************************************************************************
200 type TypeEnv = NameEnv TyThing
201 emptyTypeEnv = emptyNameEnv
203 data TyThing = AnId Id
207 instance NamedThing TyThing where
208 getName (AnId id) = getName id
209 getName (ATyCon tc) = getName tc
210 getName (AClass cl) = getName cl
215 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
216 lookupTypeEnv tbl name
217 = case lookupModuleEnv tbl (nameModule name) of
218 Just details -> lookupNameEnv (md_types details) name
222 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
223 -- Finite map because we want the range too
225 = foldl add emptyFM things
227 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
228 add tbl thing = addToFM tbl mod new_env
231 mod = nameModule name
232 new_env = case lookupFM tbl mod of
233 Nothing -> unitNameEnv name thing
234 Just env -> extendNameEnv env name thing
236 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
237 extendTypeEnv tbl things
238 = foldFM add tbl things
241 = panic "extendTypeEnv" --extendModuleEnv mod new_details
244 = case lookupModuleEnv tbl mod of
245 Nothing -> emptyModDetails {md_types = type_env}
246 Just details -> details {md_types = md_types details
247 `plusNameEnv` type_env}
251 %************************************************************************
253 \subsection{Auxiliary types}
255 %************************************************************************
257 These types are defined here because they are mentioned in ModDetails,
258 but they are mostly elaborated elsewhere
263 vers_module :: Version, -- Changes when anything changes
264 vers_exports :: Version, -- Changes when export list changes
265 vers_rules :: Version, -- Changes when any rule changes
266 vers_decls :: NameEnv Version
267 -- Versions for "big" names only (not data constructors, class ops)
268 -- The version of an Id changes if its fixity changes
269 -- Ditto data constructors, class operations, except that the version of
270 -- the parent class/tycon changes
273 initialVersionInfo :: VersionInfo
274 initialVersionInfo = VersionInfo { vers_module = initialVersion,
275 vers_exports = initialVersion,
276 vers_rules = initialVersion,
277 vers_decls = emptyNameEnv }
279 data Deprecations = NoDeprecs
280 | DeprecAll DeprecTxt -- Whole module deprecated
281 | DeprecSome (NameEnv DeprecTxt) -- Some things deprecated
284 lookupDeprec :: ModIface -> Name -> Maybe DeprecTxt
285 lookupDeprec iface name
286 = case mi_deprecs iface of
288 DeprecAll txt -> Just txt
289 DeprecSome env -> lookupNameEnv env name
291 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
292 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
295 type RuleEnv = NameEnv [CoreRule]
297 emptyRuleEnv = emptyVarEnv
302 type Avails = [AvailInfo]
303 type AvailInfo = GenAvailInfo Name
304 type RdrAvailInfo = GenAvailInfo OccName
306 data GenAvailInfo name = Avail name -- An ordinary identifier
307 | AvailTC name -- The name of the type or class
308 [name] -- The available pieces of type/class.
309 -- NB: If the type or class is itself
310 -- to be in scope, it must be in this list.
311 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
313 -- Equality used when deciding if the interface has changed
315 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
319 %************************************************************************
321 \subsection{ModIface}
323 %************************************************************************
326 type WhetherHasOrphans = Bool
328 -- * an instance decl in a module other than the defn module for
329 -- one of the tycons or classes in the instance head
330 -- * a transformation rule in a module other than the one defining
331 -- the function in the head of the rule.
333 type IsBootInterface = Bool
335 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
337 data WhatsImported name = NothingAtAll -- The module is below us in the
338 -- hierarchy, but we import nothing
340 | Everything Version -- Used for modules from other packages;
341 -- we record only the module's version number
344 Version -- Module version
345 (Maybe Version) -- Export-list version, if we depend on it
346 [(name,Version)] -- List guaranteed non-empty
347 Version -- Rules version
350 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
351 -- the module". If you use anything in the module you get its rule version
352 -- So if the rules change, you'll recompile, even if you don't use them.
353 -- This is easy to implement, and it's safer: you might not have used the rules last
354 -- time round, but if someone has added a new rule you might need it this time
356 -- The export list field is (Just v) if we depend on the export list:
357 -- we imported the module without saying exactly what we imported
358 -- We need to recompile if the module exports changes, because we might
359 -- now have a name clash in the importing module.
363 %************************************************************************
365 \subsection{The persistent compiler state}
367 %************************************************************************
370 data PersistentCompilerState
372 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
373 -- the mi_decls component is empty
374 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
375 -- except that the InstEnv components is empty
376 pcs_insts :: InstEnv, -- The total InstEnv accumulated from all
377 -- the non-home-package modules
378 pcs_rules :: RuleEnv, -- Ditto RuleEnv
380 pcs_PRS :: PersistentRenamerState
384 The @PersistentRenamerState@ persists across successive calls to the
388 * A name supply, which deals with allocating unique names to
389 (Module,OccName) original names,
391 * An accumulated InstEnv from all the modules in pcs_PST
392 The point is that we don't want to keep recreating it whenever
393 we compile a new module. The InstEnv component of pcPST is empty.
394 (This means we might "see" instances that we shouldn't "really" see;
395 but the Haskell Report is vague on what is meant to be visible,
396 so we just take the easy road here.)
400 * A "holding pen" for declarations that have been read out of
401 interface files but not yet sucked in, renamed, and typechecked
404 data PersistentRenamerState
405 = PRS { prsOrig :: OrigNameEnv,
406 prsDecls :: DeclsMap,
407 prsInsts :: IfaceInsts,
408 prsRules :: IfaceRules
412 The OrigNameEnv makes sure that there is just one Unique assigned for
413 each original name; i.e. (module-name, occ-name) pair. The Name is
414 always stored as a Global, and has the SrcLoc of its binding location.
415 Actually that's not quite right. When we first encounter the original
416 name, we might not be at its binding site (e.g. we are reading an
417 interface file); so we give it 'noSrcLoc' then. Later, when we find
418 its binding site, we fix it up.
420 Exactly the same is true of the Module stored in the Name. When we first
421 encounter the occurrence, we may not know the details of the module, so
422 we just store junk. Then when we find the binding site, we fix it up.
426 = Orig { origNames :: OrigNameNameEnv,
427 -- Ensures that one original name gets one unique
428 origIParam :: OrigNameIParamEnv
429 -- Ensures that one implicit parameter name gets one unique
432 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
433 type OrigNameIParamEnv = FiniteMap OccName Name
437 A DeclsMap contains a binding for each Name in the declaration
438 including the constructors of a type decl etc. The Bool is True just
442 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameHsDecl))
444 type IfaceInsts = Bag GatedDecl
445 type IfaceRules = Bag GatedDecl
447 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
451 %************************************************************************
453 \subsection{Provenance and export info}
455 %************************************************************************
457 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
458 one for each module, corresponding to that module's top-level scope.
461 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
462 -- These only get reported on lookup,
463 -- not on construction
466 The "provenance" of something says how it came to be in scope.
470 = LocalDef -- Defined locally
472 | NonLocalDef -- Defined non-locally
476 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
477 instance Eq Provenance where
478 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
480 instance Eq ImportReason where
481 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
483 instance Ord Provenance where
484 compare LocalDef LocalDef = EQ
485 compare LocalDef (NonLocalDef _ _) = LT
486 compare (NonLocalDef _ _) LocalDef = GT
488 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
489 = compare reason1 reason2
491 instance Ord ImportReason where
492 compare ImplicitImport ImplicitImport = EQ
493 compare ImplicitImport (UserImport _ _ _) = LT
494 compare (UserImport _ _ _) ImplicitImport = GT
495 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
496 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
500 = UserImport Module SrcLoc Bool -- Imported from module M on line L
501 -- Note the M may well not be the defining module
503 -- The Bool is true iff the thing was named *explicitly* in the import spec,
504 -- rather than being imported as part of a group; e.g.
507 -- Here, everything imported by B, and the constructors of T
508 -- are not named explicitly; only T is named explicitly.
509 -- This info is used when warning of unused names.
511 | ImplicitImport -- Imported implicitly for some other reason
514 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
515 -- in scope in this module, so print it
516 -- unqualified in error messages
520 hasBetterProv :: Provenance -> Provenance -> Bool
522 -- a local thing over an imported thing
523 -- a user-imported thing over a non-user-imported thing
524 -- an explicitly-imported thing over an implicitly imported thing
525 hasBetterProv LocalDef _ = True
526 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
527 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
528 hasBetterProv _ _ = False
530 pprNameProvenance :: Name -> Provenance -> SDoc
531 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
532 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
533 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
535 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
536 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
538 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc