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,
13 lookupTable, lookupTableByModName,
17 VersionInfo(..), initialVersionInfo,
19 TyThing(..), groupTyThings, isTyClThing,
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,
33 PackageInstEnv, PackageRuleBase,
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,
53 lookupModuleEnv, lookupModuleEnvByName
55 import Rules ( RuleBase )
56 import VarSet ( TyVarSet )
58 import Class ( Class )
59 import TyCon ( TyCon )
61 import BasicTypes ( Version, initialVersion, Fixity )
63 import HsSyn ( DeprecTxt )
64 import RdrHsSyn ( RdrNameHsDecl, RdrNameTyClDecl )
65 import RnHsSyn ( RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl )
67 import CoreSyn ( CoreRule, IdCoreRule )
70 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
72 import Maybes ( seqMaybe )
73 import UniqFM ( UniqFM )
75 import SrcLoc ( SrcLoc, isGoodSrcLoc )
76 import Util ( thenCmp )
77 import UniqSupply ( UniqSupply )
80 %************************************************************************
82 \subsection{The Finder type}
84 %************************************************************************
87 type Finder = ModuleName -> IO (Maybe (Module, ModuleLocation))
97 For a module in another package, the hs_file and obj_file
98 components of ModuleLocation are undefined.
100 The locations specified by a ModuleLocation may or may not
101 correspond to actual files yet: for example, even if the object
102 file doesn't exist, the ModuleLocation still contains the path to
103 where the object file will reside if/when it is created.
106 %************************************************************************
108 \subsection{Symbol tables and Module details}
110 %************************************************************************
112 A @ModIface@ plus a @ModDetails@ summarises everything we know
113 about a compiled module. The @ModIface@ is the stuff *before* linking,
114 and can be written out to an interface file. The @ModDetails@ is after
115 linking; it is the "linked" form of the mi_decls field.
120 mi_module :: Module, -- Complete with package info
121 mi_version :: VersionInfo, -- Module version number
122 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
124 mi_usages :: [ImportVersion Name], -- Usages; kept sorted so that it's easy
125 -- to decide whether to write a new iface file
126 -- (changing usages doesn't affect the version of
129 mi_exports :: [(ModuleName,Avails)], -- What it exports
130 -- Kept sorted by (mod,occ),
131 -- to make version comparisons easier
133 mi_globals :: GlobalRdrEnv, -- Its top level environment
135 mi_fixities :: NameEnv Fixity, -- Fixities
136 mi_deprecs :: Deprecations, -- Deprecations
138 mi_decls :: IfaceDecls -- The RnDecls form of ModDetails
141 data IfaceDecls = IfaceDecls { dcl_tycl :: [RenamedTyClDecl], -- Sorted
142 dcl_rules :: [RenamedRuleDecl], -- Sorted
143 dcl_insts :: [RenamedInstDecl] } -- Unsorted
145 -- typechecker should only look at this, not ModIface
146 -- Should be able to construct ModDetails from mi_decls in ModIface
149 -- The next three fields are created by the typechecker
151 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
152 md_rules :: [IdCoreRule] -- Domain may include Ids from other modules
157 emptyModDetails :: ModDetails
159 = ModDetails { md_types = emptyTypeEnv,
164 emptyModIface :: Module -> ModIface
166 = ModIface { mi_module = mod,
168 mi_globals = emptyRdrEnv,
169 mi_deprecs = NoDeprecs
173 Symbol tables map modules to ModDetails:
176 type SymbolTable = ModuleEnv ModDetails
177 type IfaceTable = ModuleEnv ModIface
179 type HomeIfaceTable = IfaceTable
180 type PackageIfaceTable = IfaceTable
182 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
183 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
184 type GlobalSymbolTable = SymbolTable -- Domain = all modules
187 Simple lookups in the symbol table.
190 lookupTable :: ModuleEnv a -> ModuleEnv a -> Name -> Maybe a
191 -- We often have two Symbol- or IfaceTables, and want to do a lookup
192 lookupTable ht pt name
193 = lookupModuleEnv ht mod `seqMaybe` lookupModuleEnv pt mod
195 mod = nameModule name
197 lookupTableByModName :: ModuleEnv a -> ModuleEnv a -> ModuleName -> Maybe a
198 -- We often have two Symbol- or IfaceTables, and want to do a lookup
199 lookupTableByModName ht pt mod
200 = lookupModuleEnvByName ht mod `seqMaybe` lookupModuleEnvByName pt mod
204 %************************************************************************
206 \subsection{Type environment stuff}
208 %************************************************************************
211 type TypeEnv = NameEnv TyThing
212 emptyTypeEnv = emptyNameEnv
214 data TyThing = AnId Id
218 isTyClThing :: TyThing -> Bool
219 isTyClThing (ATyCon _) = True
220 isTyClThing (AClass _) = True
221 isTyClThing (AnId _) = False
223 instance NamedThing TyThing where
224 getName (AnId id) = getName id
225 getName (ATyCon tc) = getName tc
226 getName (AClass cl) = getName cl
231 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
232 lookupTypeEnv tbl name
233 = case lookupModuleEnv tbl (nameModule name) of
234 Just details -> lookupNameEnv (md_types details) name
238 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
239 -- Finite map because we want the range too
241 = foldl add emptyFM things
243 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
244 add tbl thing = addToFM tbl mod new_env
247 mod = nameModule name
248 new_env = case lookupFM tbl mod of
249 Nothing -> unitNameEnv name thing
250 Just env -> extendNameEnv env name thing
252 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
253 extendTypeEnv tbl things
254 = foldFM add tbl things
257 = panic "extendTypeEnv" --extendModuleEnv mod new_details
260 = case lookupModuleEnv tbl mod of
261 Nothing -> emptyModDetails {md_types = type_env}
262 Just details -> details {md_types = md_types details
263 `plusNameEnv` type_env}
267 %************************************************************************
269 \subsection{Auxiliary types}
271 %************************************************************************
273 These types are defined here because they are mentioned in ModDetails,
274 but they are mostly elaborated elsewhere
279 vers_module :: Version, -- Changes when anything changes
280 vers_exports :: Version, -- Changes when export list changes
281 vers_rules :: Version, -- Changes when any rule changes
282 vers_decls :: NameEnv Version
283 -- Versions for "big" names only (not data constructors, class ops)
284 -- The version of an Id changes if its fixity changes
285 -- Ditto data constructors, class operations, except that the version of
286 -- the parent class/tycon changes
289 initialVersionInfo :: VersionInfo
290 initialVersionInfo = VersionInfo { vers_module = initialVersion,
291 vers_exports = initialVersion,
292 vers_rules = initialVersion,
293 vers_decls = emptyNameEnv }
295 data Deprecations = NoDeprecs
296 | DeprecAll DeprecTxt -- Whole module deprecated
297 | DeprecSome (NameEnv (Name,DeprecTxt)) -- Some things deprecated
299 -- We keep the Name in the range, so we can print them out
301 lookupDeprec :: ModIface -> Name -> Maybe DeprecTxt
302 lookupDeprec iface name
303 = case mi_deprecs iface of
305 DeprecAll txt -> Just txt
306 DeprecSome env -> case lookupNameEnv env name of
307 Just (_, txt) -> Just txt
310 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
312 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
318 type Avails = [AvailInfo]
319 type AvailInfo = GenAvailInfo Name
320 type RdrAvailInfo = GenAvailInfo OccName
322 data GenAvailInfo name = Avail name -- An ordinary identifier
323 | AvailTC name -- The name of the type or class
324 [name] -- The available pieces of type/class.
325 -- NB: If the type or class is itself
326 -- to be in scope, it must be in this list.
327 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
329 -- Equality used when deciding if the interface has changed
331 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
335 %************************************************************************
337 \subsection{ModIface}
339 %************************************************************************
342 type WhetherHasOrphans = Bool
344 -- * an instance decl in a module other than the defn module for
345 -- one of the tycons or classes in the instance head
346 -- * a transformation rule in a module other than the one defining
347 -- the function in the head of the rule.
349 type IsBootInterface = Bool
351 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
353 data WhatsImported name = NothingAtAll -- The module is below us in the
354 -- hierarchy, but we import nothing
356 | Everything Version -- Used for modules from other packages;
357 -- we record only the module's version number
360 Version -- Module version
361 (Maybe Version) -- Export-list version, if we depend on it
362 [(name,Version)] -- List guaranteed non-empty
363 Version -- Rules version
366 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
367 -- the module". If you use anything in the module you get its rule version
368 -- So if the rules change, you'll recompile, even if you don't use them.
369 -- This is easy to implement, and it's safer: you might not have used the rules last
370 -- time round, but if someone has added a new rule you might need it this time
372 -- The export list field is (Just v) if we depend on the export list:
373 -- we imported the module without saying exactly what we imported
374 -- We need to recompile if the module exports changes, because we might
375 -- now have a name clash in the importing module.
379 %************************************************************************
381 \subsection{The persistent compiler state}
383 %************************************************************************
386 data PersistentCompilerState
388 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
389 -- the mi_decls component is empty
391 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
392 -- except that the InstEnv components is empty
394 pcs_insts :: PackageInstEnv, -- The total InstEnv accumulated from all
395 -- the non-home-package modules
397 pcs_rules :: PackageRuleBase, -- Ditto RuleEnv
399 pcs_PRS :: PersistentRenamerState
404 The @PersistentRenamerState@ persists across successive calls to the
408 * A name supply, which deals with allocating unique names to
409 (Module,OccName) original names,
411 * An accumulated InstEnv from all the modules in pcs_PST
412 The point is that we don't want to keep recreating it whenever
413 we compile a new module. The InstEnv component of pcPST is empty.
414 (This means we might "see" instances that we shouldn't "really" see;
415 but the Haskell Report is vague on what is meant to be visible,
416 so we just take the easy road here.)
420 * A "holding pen" for declarations that have been read out of
421 interface files but not yet sucked in, renamed, and typechecked
424 type PackageRuleBase = RuleBase
425 type PackageInstEnv = InstEnv
427 data PersistentRenamerState
428 = PRS { prsOrig :: OrigNameEnv,
429 prsDecls :: DeclsMap,
430 prsInsts :: IfaceInsts,
431 prsRules :: IfaceRules,
436 The OrigNameEnv makes sure that there is just one Unique assigned for
437 each original name; i.e. (module-name, occ-name) pair. The Name is
438 always stored as a Global, and has the SrcLoc of its binding location.
439 Actually that's not quite right. When we first encounter the original
440 name, we might not be at its binding site (e.g. we are reading an
441 interface file); so we give it 'noSrcLoc' then. Later, when we find
442 its binding site, we fix it up.
444 Exactly the same is true of the Module stored in the Name. When we first
445 encounter the occurrence, we may not know the details of the module, so
446 we just store junk. Then when we find the binding site, we fix it up.
450 = Orig { origNames :: OrigNameNameEnv,
451 -- Ensures that one original name gets one unique
452 origIParam :: OrigNameIParamEnv
453 -- Ensures that one implicit parameter name gets one unique
456 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
457 type OrigNameIParamEnv = FiniteMap OccName Name
461 A DeclsMap contains a binding for each Name in the declaration
462 including the constructors of a type decl etc. The Bool is True just
466 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameTyClDecl))
468 type IfaceInsts = Bag GatedDecl
469 type IfaceRules = Bag GatedDecl
471 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
475 %************************************************************************
477 \subsection{Provenance and export info}
479 %************************************************************************
481 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
482 one for each module, corresponding to that module's top-level scope.
485 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
486 -- These only get reported on lookup,
487 -- not on construction
490 The "provenance" of something says how it came to be in scope.
494 = LocalDef -- Defined locally
496 | NonLocalDef -- Defined non-locally
500 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
501 instance Eq Provenance where
502 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
504 instance Eq ImportReason where
505 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
507 instance Ord Provenance where
508 compare LocalDef LocalDef = EQ
509 compare LocalDef (NonLocalDef _ _) = LT
510 compare (NonLocalDef _ _) LocalDef = GT
512 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
513 = compare reason1 reason2
515 instance Ord ImportReason where
516 compare ImplicitImport ImplicitImport = EQ
517 compare ImplicitImport (UserImport _ _ _) = LT
518 compare (UserImport _ _ _) ImplicitImport = GT
519 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
520 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
524 = UserImport Module SrcLoc Bool -- Imported from module M on line L
525 -- Note the M may well not be the defining module
527 -- The Bool is true iff the thing was named *explicitly* in the import spec,
528 -- rather than being imported as part of a group; e.g.
531 -- Here, everything imported by B, and the constructors of T
532 -- are not named explicitly; only T is named explicitly.
533 -- This info is used when warning of unused names.
535 | ImplicitImport -- Imported implicitly for some other reason
538 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
539 -- in scope in this module, so print it
540 -- unqualified in error messages
544 hasBetterProv :: Provenance -> Provenance -> Bool
546 -- a local thing over an imported thing
547 -- a user-imported thing over a non-user-imported thing
548 -- an explicitly-imported thing over an implicitly imported thing
549 hasBetterProv LocalDef _ = True
550 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
551 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
552 hasBetterProv _ _ = False
554 pprNameProvenance :: Name -> Provenance -> SDoc
555 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
556 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
557 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
559 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
560 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
562 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc