+%************************************************************************
+%* *
+\subsection{Get global names}
+%* *
+%************************************************************************
+
+\begin{code}
+getGlobalNames :: RdrNameHsModule
+ -> RnMG (Maybe (ExportEnv,
+ RnEnv,
+ FiniteMap Name HowInScope, -- Locally defined or explicitly imported
+ Name -> PrintUnqualified))
+ -- Nothing => no need to recompile
+
+getGlobalNames m@(HsModule this_mod _ exports imports _ _ mod_loc)
+ = fixRn (\ ~(rec_exp_fn, _) ->
+
+ -- PROCESS LOCAL DECLS
+ -- Do these *first* so that the correct provenance gets
+ -- into the global name cache.
+ importsFromLocalDecls rec_exp_fn m `thenRn` \ (local_rn_env, local_mod_avails, local_info) ->
+
+ -- PROCESS IMPORT DECLS
+ mapAndUnzip3Rn importsFromImportDecl all_imports
+ `thenRn` \ (imp_rn_envs, imp_avails_s, explicit_imports_s) ->
+
+ -- COMBINE RESULTS
+ -- We put the local env second, so that a local provenance
+ -- "wins", even if a module imports itself.
+ foldlRn plusRnEnv emptyRnEnv imp_rn_envs `thenRn` \ imp_rn_env ->
+ plusRnEnv imp_rn_env local_rn_env `thenRn` \ rn_env ->
+
+ -- TRY FOR EARLY EXIT
+ -- We can't go for an early exit before this because we have to check
+ -- for name clashes. Consider:
+ --
+ -- module A where module B where
+ -- import B h = True
+ -- f = h
+ --
+ -- Suppose I've compiled everything up, and then I add a
+ -- new definition to module B, that defines "f".
+ --
+ -- Then I must detect the name clash in A before going for an early
+ -- exit. The early-exit code checks what's actually needed from B
+ -- to compile A, and of course that doesn't include B.f. That's
+ -- why we wait till after the plusRnEnv stuff to do the early-exit.
+ checkEarlyExit this_mod `thenRn` \ up_to_date ->
+ if up_to_date then
+ returnRn (error "early exit", Nothing)
+ else
+
+
+ -- PROCESS EXPORT LISTS
+ let
+ export_avails :: ExportAvails
+ export_avails = foldr plusExportAvails local_mod_avails imp_avails_s
+
+ explicit_info :: FiniteMap Name HowInScope -- Locally defined or explicitly imported
+ explicit_info = foldr plusFM local_info explicit_imports_s
+ in
+ exportsFromAvail this_mod exports export_avails rn_env
+ `thenRn` \ (export_fn, export_env) ->
+
+ -- BUILD THE "IMPORT FN". It just tells whether a name is in
+ -- scope in an unqualified form.
+ let
+ print_unqual = mkImportFn imp_rn_env
+ in
+
+ returnRn (export_fn, Just (export_env, rn_env, explicit_info, print_unqual))
+ ) `thenRn` \ (_, result) ->
+ returnRn result
+ where
+ junk_exp_fn = error "RnNames:export_fn"
+
+ all_imports = prel_imports ++ imports
+
+ -- NB: opt_NoImplicitPrelude is slightly different to import Prelude ();
+ -- because the former doesn't even look at Prelude.hi for instance declarations,
+ -- whereas the latter does.
+ prel_imports | this_mod == pRELUDE ||
+ explicit_prelude_import ||
+ opt_NoImplicitPrelude
+ = []
+
+ | otherwise = [ImportDecl pRELUDE
+ False {- Not qualified -}
+ HiFile {- Not source imported -}
+ Nothing {- No "as" -}
+ Nothing {- No import list -}
+ mod_loc]
+
+ explicit_prelude_import
+ = not (null [ () | (ImportDecl mod qual _ _ _ _) <- imports, mod == pRELUDE ])
+\end{code}
+
+\begin{code}
+checkEarlyExit mod
+ = checkErrsRn `thenRn` \ no_errs_so_far ->
+ if not no_errs_so_far then
+ -- Found errors already, so exit now
+ returnRn True
+ else
+
+ traceRn (text "Considering whether compilation is required...") `thenRn_`
+ if not opt_SourceUnchanged then
+ -- Source code changed and no errors yet... carry on
+ traceRn (nest 4 (text "source file changed or recompilation check turned off")) `thenRn_`
+ returnRn False
+ else
+
+ -- Unchanged source, and no errors yet; see if usage info
+ -- up to date, and exit if so
+ checkUpToDate mod `thenRn` \ up_to_date ->
+ putDocRn (text "Compilation" <+>
+ text (if up_to_date then "IS NOT" else "IS") <+>
+ text "required") `thenRn_`
+ returnRn up_to_date
+\end{code}
+
+\begin{code}
+importsFromImportDecl :: RdrNameImportDecl
+ -> RnMG (RnEnv,
+ ExportAvails,
+ FiniteMap Name HowInScope) -- Records the explicitly-imported things
+
+importsFromImportDecl (ImportDecl mod qual_only as_source as_mod import_spec loc)
+ = pushSrcLocRn loc $
+ getInterfaceExports mod as_source `thenRn` \ (avails, fixities) ->
+ filterImports mod import_spec avails `thenRn` \ (filtered_avails, hides, explicits) ->