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 TyThing(..), groupTyThings,
18 TypeEnv, extendTypeEnv, lookupTypeEnv,
22 WhetherHasOrphans, ImportVersion, WhatsImported(..),
23 PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
24 IfaceInsts, IfaceRules, GatedDecl,
25 OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
26 AvailEnv, AvailInfo, GenAvailInfo(..),
27 PersistentCompilerState(..),
29 Deprecations(..), lookupDeprec,
31 InstEnv, ClsInstEnv, DFunId,
33 GlobalRdrEnv, RdrAvailInfo,
36 Provenance(..), ImportReason(..), PrintUnqualified,
37 pprNameProvenance, hasBetterProv
41 #include "HsVersions.h"
43 import RdrName ( RdrNameEnv, emptyRdrEnv )
44 import Name ( Name, NameEnv, NamedThing,
45 emptyNameEnv, unitNameEnv, extendNameEnv, plusNameEnv,
46 lookupNameEnv, emptyNameEnv, getName, nameModule,
48 import NameSet ( NameSet )
49 import OccName ( OccName )
50 import Module ( Module, ModuleName, ModuleEnv,
52 import VarSet ( TyVarSet )
53 import VarEnv ( IdEnv, emptyVarEnv )
55 import Class ( Class )
56 import TyCon ( TyCon )
58 import BasicTypes ( Version, Fixity )
60 import HsSyn ( DeprecTxt )
61 import RdrHsSyn ( RdrNameHsDecl )
62 import RnHsSyn ( RenamedHsDecl )
64 import CoreSyn ( CoreRule )
67 import FiniteMap ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
69 import UniqFM ( UniqFM )
71 import SrcLoc ( SrcLoc, isGoodSrcLoc )
72 import Util ( thenCmp )
75 %************************************************************************
77 \subsection{The Finder type}
79 %************************************************************************
82 type Finder = ModuleName -> IO (Maybe (Module, ModuleLocation))
92 For a module in another package, the hs_file and obj_file
93 components of ModuleLocation are undefined.
95 The locations specified by a ModuleLocation may or may not
96 correspond to actual files yet: for example, even if the object
97 file doesn't exist, the ModuleLocation still contains the path to
98 where the object file will reside if/when it is created.
101 %************************************************************************
103 \subsection{Symbol tables and Module details}
105 %************************************************************************
107 A @ModIface@ plus a @ModDetails@ summarises everything we know
108 about a compiled module. The @ModIface@ is the stuff *before* linking,
109 and can be written out to an interface file. The @ModDetails@ is after
110 linking; it is the "linked" form of the mi_decls field.
115 mi_module :: Module, -- Complete with package info
116 mi_version :: VersionInfo, -- Module version number
117 mi_orphan :: WhetherHasOrphans, -- Whether this module has orphans
119 mi_usages :: [ImportVersion Name], -- Usages
121 mi_exports :: Avails, -- What it exports; kept sorted by (mod,occ),
122 -- to make version comparisons easier
124 mi_globals :: GlobalRdrEnv, -- Its top level environment
126 mi_fixities :: NameEnv Fixity, -- Fixities
127 mi_deprecs :: Deprecations, -- Deprecations
129 mi_decls :: [RenamedHsDecl] -- types, classes
130 -- inst decls, rules, iface sigs
133 -- typechecker should only look at this, not ModIface
134 -- Should be able to construct ModDetails from mi_decls in ModIface
137 -- The next three fields are created by the typechecker
139 md_insts :: [DFunId], -- Dfun-ids for the instances in this module
140 md_rules :: RuleEnv -- Domain may include Ids from other modules
145 emptyModDetails :: ModDetails
147 = ModDetails { md_types = emptyTypeEnv,
149 md_rules = emptyRuleEnv
152 emptyModIface :: Module -> ModIface
154 = ModIface { mi_module = mod,
156 mi_globals = emptyRdrEnv,
157 mi_deprecs = NoDeprecs
161 Symbol tables map modules to ModDetails:
164 type SymbolTable = ModuleEnv ModDetails
165 type IfaceTable = ModuleEnv ModIface
167 type HomeIfaceTable = IfaceTable
168 type PackageIfaceTable = IfaceTable
170 type HomeSymbolTable = SymbolTable -- Domain = modules in the home package
171 type PackageSymbolTable = SymbolTable -- Domain = modules in the some other package
172 type GlobalSymbolTable = SymbolTable -- Domain = all modules
175 Simple lookups in the symbol table.
178 lookupFixityEnv :: IfaceTable -> Name -> Maybe Fixity
179 lookupFixityEnv tbl name
180 = case lookupModuleEnv tbl (nameModule name) of
182 Just details -> lookupNameEnv (mi_fixities details) name
186 %************************************************************************
188 \subsection{Type environment stuff}
190 %************************************************************************
193 type TypeEnv = NameEnv TyThing
194 emptyTypeEnv = emptyNameEnv
196 data TyThing = AnId Id
200 instance NamedThing TyThing where
201 getName (AnId id) = getName id
202 getName (ATyCon tc) = getName tc
203 getName (AClass cl) = getName cl
208 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
209 lookupTypeEnv tbl name
210 = case lookupModuleEnv tbl (nameModule name) of
211 Just details -> lookupNameEnv (md_types details) name
215 groupTyThings :: [TyThing] -> FiniteMap Module TypeEnv
216 -- Finite map because we want the range too
218 = foldl add emptyFM things
220 add :: FiniteMap Module TypeEnv -> TyThing -> FiniteMap Module TypeEnv
221 add tbl thing = addToFM tbl mod new_env
224 mod = nameModule name
225 new_env = case lookupFM tbl mod of
226 Nothing -> unitNameEnv name thing
227 Just env -> extendNameEnv env name thing
229 extendTypeEnv :: SymbolTable -> FiniteMap Module TypeEnv -> SymbolTable
230 extendTypeEnv tbl things
231 = foldFM add tbl things
234 = panic "extendTypeEnv" --extendModuleEnv mod new_details
237 = case lookupModuleEnv tbl mod of
238 Nothing -> emptyModDetails {md_types = type_env}
239 Just details -> details {md_types = md_types details
240 `plusNameEnv` type_env}
244 %************************************************************************
246 \subsection{Auxiliary types}
248 %************************************************************************
250 These types are defined here because they are mentioned in ModDetails,
251 but they are mostly elaborated elsewhere
256 vers_module :: Version, -- Changes when anything changes
257 vers_exports :: Version, -- Changes when export list changes
258 vers_rules :: Version, -- Changes when any rule changes
259 vers_decls :: NameEnv Version
260 -- Versions for "big" names only (not data constructors, class ops)
261 -- The version of an Id changes if its fixity changes
262 -- Ditto data constructors, class operations, except that the version of
263 -- the parent class/tycon changes
266 data Deprecations = NoDeprecs
267 | DeprecAll DeprecTxt -- Whole module deprecated
268 | DeprecSome (NameEnv DeprecTxt) -- Some things deprecated
271 lookupDeprec :: ModIface -> Name -> Maybe DeprecTxt
272 lookupDeprec iface name
273 = case mi_deprecs iface of
275 DeprecAll txt -> Just txt
276 DeprecSome env -> lookupNameEnv env name
278 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
279 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
282 type RuleEnv = NameEnv [CoreRule]
284 emptyRuleEnv = emptyVarEnv
289 type Avails = [AvailInfo]
290 type AvailInfo = GenAvailInfo Name
291 type RdrAvailInfo = GenAvailInfo OccName
293 data GenAvailInfo name = Avail name -- An ordinary identifier
294 | AvailTC name -- The name of the type or class
295 [name] -- The available pieces of type/class.
296 -- NB: If the type or class is itself
297 -- to be in scope, it must be in this list.
298 -- Thus, typically: AvailTC Eq [Eq, ==, /=]
300 -- Equality used when deciding if the interface has changed
302 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
306 %************************************************************************
308 \subsection{ModIface}
310 %************************************************************************
313 type WhetherHasOrphans = Bool
315 -- * an instance decl in a module other than the defn module for
316 -- one of the tycons or classes in the instance head
317 -- * a transformation rule in a module other than the one defining
318 -- the function in the head of the rule.
320 type IsBootInterface = Bool
322 type ImportVersion name = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
324 data WhatsImported name = NothingAtAll -- The module is below us in the
325 -- hierarchy, but we import nothing
327 | Everything Version -- Used for modules from other packages;
328 -- we record only the module's version number
331 Version -- Module version
332 (Maybe Version) -- Export-list version, if we depend on it
333 [(name,Version)] -- List guaranteed non-empty
334 Version -- Rules version
337 -- 'Specifically' doesn't let you say "I imported f but none of the rules in
338 -- the module". If you use anything in the module you get its rule version
339 -- So if the rules change, you'll recompile, even if you don't use them.
340 -- This is easy to implement, and it's safer: you might not have used the rules last
341 -- time round, but if someone has added a new rule you might need it this time
343 -- The export list field is (Just v) if we depend on the export list:
344 -- we imported the module without saying exactly what we imported
345 -- We need to recompile if the module exports changes, because we might
346 -- now have a name clash in the importing module.
350 %************************************************************************
352 \subsection{The persistent compiler state}
354 %************************************************************************
357 data PersistentCompilerState
359 pcs_PIT :: PackageIfaceTable, -- Domain = non-home-package modules
360 -- the mi_decls component is empty
361 pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
362 -- except that the InstEnv components is empty
363 pcs_insts :: InstEnv, -- The total InstEnv accumulated from all
364 -- the non-home-package modules
365 pcs_rules :: RuleEnv, -- Ditto RuleEnv
367 pcs_PRS :: PersistentRenamerState
371 The @PersistentRenamerState@ persists across successive calls to the
375 * A name supply, which deals with allocating unique names to
376 (Module,OccName) original names,
378 * An accumulated InstEnv from all the modules in pcs_PST
379 The point is that we don't want to keep recreating it whenever
380 we compile a new module. The InstEnv component of pcPST is empty.
381 (This means we might "see" instances that we shouldn't "really" see;
382 but the Haskell Report is vague on what is meant to be visible,
383 so we just take the easy road here.)
387 * A "holding pen" for declarations that have been read out of
388 interface files but not yet sucked in, renamed, and typechecked
391 data PersistentRenamerState
392 = PRS { prsOrig :: OrigNameEnv,
393 prsDecls :: DeclsMap,
394 prsInsts :: IfaceInsts,
395 prsRules :: IfaceRules
399 The OrigNameEnv makes sure that there is just one Unique assigned for
400 each original name; i.e. (module-name, occ-name) pair. The Name is
401 always stored as a Global, and has the SrcLoc of its binding location.
402 Actually that's not quite right. When we first encounter the original
403 name, we might not be at its binding site (e.g. we are reading an
404 interface file); so we give it 'noSrcLoc' then. Later, when we find
405 its binding site, we fix it up.
407 Exactly the same is true of the Module stored in the Name. When we first
408 encounter the occurrence, we may not know the details of the module, so
409 we just store junk. Then when we find the binding site, we fix it up.
413 = Orig { origNames :: OrigNameNameEnv,
414 -- Ensures that one original name gets one unique
415 origIParam :: OrigNameIParamEnv
416 -- Ensures that one implicit parameter name gets one unique
419 type OrigNameNameEnv = FiniteMap (ModuleName,OccName) Name
420 type OrigNameIParamEnv = FiniteMap OccName Name
424 A DeclsMap contains a binding for each Name in the declaration
425 including the constructors of a type decl etc. The Bool is True just
429 type DeclsMap = NameEnv (AvailInfo, Bool, (Module, RdrNameHsDecl))
431 type IfaceInsts = Bag GatedDecl
432 type IfaceRules = Bag GatedDecl
434 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
438 %************************************************************************
440 \subsection{Provenance and export info}
442 %************************************************************************
444 The GlobalRdrEnv gives maps RdrNames to Names. There is a separate
445 one for each module, corresponding to that module's top-level scope.
448 type GlobalRdrEnv = RdrNameEnv [(Name,Provenance)] -- The list is because there may be name clashes
449 -- These only get reported on lookup,
450 -- not on construction
453 The "provenance" of something says how it came to be in scope.
457 = LocalDef -- Defined locally
459 | NonLocalDef -- Defined non-locally
463 -- Just used for grouping error messages (in RnEnv.warnUnusedBinds)
464 instance Eq Provenance where
465 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
467 instance Eq ImportReason where
468 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
470 instance Ord Provenance where
471 compare LocalDef LocalDef = EQ
472 compare LocalDef (NonLocalDef _ _) = LT
473 compare (NonLocalDef _ _) LocalDef = GT
475 compare (NonLocalDef reason1 _) (NonLocalDef reason2 _)
476 = compare reason1 reason2
478 instance Ord ImportReason where
479 compare ImplicitImport ImplicitImport = EQ
480 compare ImplicitImport (UserImport _ _ _) = LT
481 compare (UserImport _ _ _) ImplicitImport = GT
482 compare (UserImport m1 loc1 _) (UserImport m2 loc2 _)
483 = (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
487 = UserImport Module SrcLoc Bool -- Imported from module M on line L
488 -- Note the M may well not be the defining module
490 -- The Bool is true iff the thing was named *explicitly* in the import spec,
491 -- rather than being imported as part of a group; e.g.
494 -- Here, everything imported by B, and the constructors of T
495 -- are not named explicitly; only T is named explicitly.
496 -- This info is used when warning of unused names.
498 | ImplicitImport -- Imported implicitly for some other reason
501 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
502 -- in scope in this module, so print it
503 -- unqualified in error messages
507 hasBetterProv :: Provenance -> Provenance -> Bool
509 -- a local thing over an imported thing
510 -- a user-imported thing over a non-user-imported thing
511 -- an explicitly-imported thing over an implicitly imported thing
512 hasBetterProv LocalDef _ = True
513 hasBetterProv (NonLocalDef (UserImport _ _ True) _) _ = True
514 hasBetterProv (NonLocalDef (UserImport _ _ _ ) _) (NonLocalDef ImplicitImport _) = True
515 hasBetterProv _ _ = False
517 pprNameProvenance :: Name -> Provenance -> SDoc
518 pprNameProvenance name LocalDef = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
519 pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why,
520 nest 2 (parens (ppr_defn (nameSrcLoc name)))]
522 ppr_reason ImplicitImport = ptext SLIT("implicitly imported")
523 ppr_reason (UserImport mod loc _) = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
525 ppr_defn loc | isGoodSrcLoc loc = ptext SLIT("at") <+> ppr loc