+\subsection{Implicit free vars and sugar names}
+%* *
+%*********************************************************
+
+@addImplicitFVs@ forces the renamer to slurp in some things which aren't
+mentioned explicitly, but which might be needed by the type checker.
+
+\begin{code}
+addImplicitFVs :: GlobalRdrEnv
+ -> Maybe (ModuleName, [RenamedHsDecl]) -- Nothing when compling an expression
+ -> FreeVars -- Free in the source
+ -> RnMG (FreeVars, SyntaxMap) -- Augmented source free vars
+
+addImplicitFVs gbl_env maybe_mod source_fvs
+ = -- Find out what re-bindable names to use for desugaring
+ rnSyntaxNames gbl_env source_fvs `thenRn` \ (source_fvs1, sugar_map) ->
+
+ -- Find implicit FVs thade
+ extra_implicits maybe_mod `thenRn` \ extra_fvs ->
+
+ let
+ implicit_fvs = ubiquitousNames `plusFV` extra_fvs
+ slurp_fvs = implicit_fvs `plusFV` source_fvs1
+ -- It's important to do the "plus" this way round, so that
+ -- when compiling the prelude, locally-defined (), Bool, etc
+ -- override the implicit ones.
+ in
+ returnRn (slurp_fvs, sugar_map)
+
+ where
+ extra_implicits Nothing -- Compiling a statement
+ = returnRn (mkFVs [printName, bindIOName, returnIOName, failIOName])
+ -- These are all needed implicitly when compiling a statement
+ -- See TcModule.tc_stmts
+
+ extra_implicits (Just (mod_name, decls)) -- Compiling a module
+ = lookupOrigNames deriv_occs `thenRn` \ deriving_names ->
+ returnRn (deriving_names `plusFV` implicit_main)
+ where
+ -- Add occurrences for IO or PrimIO
+ implicit_main | mod_name == mAIN_Name
+ || mod_name == pREL_MAIN_Name = unitFV ioTyConName
+ | otherwise = emptyFVs
+
+ deriv_occs = [occ | TyClD (TyData {tcdDerivs = Just deriv_classes}) <- decls,
+ cls <- deriv_classes,
+ occ <- lookupWithDefaultUFM derivingOccurrences [] cls ]
+
+-- ubiquitous_names are loaded regardless, because
+-- they are needed in virtually every program
+ubiquitousNames
+ = mkFVs [unpackCStringName, unpackCStringFoldrName,
+ unpackCStringUtf8Name, eqStringName]
+ -- Virtually every program has error messages in it somewhere
+
+ `plusFV`
+ mkFVs [getName unitTyCon, funTyConName, boolTyConName, intTyConName]
+ -- Add occurrences for very frequently used types.
+ -- (e.g. we don't want to be bothered with making funTyCon a
+ -- free var at every function application!)
+\end{code}
+
+\begin{code}
+implicitGates :: Name -> FreeVars
+-- If we load class Num, add Integer to the gates
+-- This takes account of the fact that Integer might be needed for
+-- defaulting, but we don't want to load Integer (and all its baggage)
+-- if there's no numeric stuff needed.
+-- Similarly for class Fractional and Double
+--
+-- NB: If we load (say) Floating, we'll end up loading Fractional too,
+-- since Fractional is a superclass of Floating
+implicitGates cls | cls `hasKey` numClassKey = unitFV integerTyConName
+ | cls `hasKey` fractionalClassKey = unitFV doubleTyConName
+ | otherwise = emptyFVs
+\end{code}
+
+\begin{code}
+rnSyntaxNames :: GlobalRdrEnv -> FreeVars -> RnMG (FreeVars, SyntaxMap)
+-- Look up the re-bindable syntactic sugar names
+-- Any errors arising from these lookups may surprise the
+-- programmer, since they aren't explicitly mentioned, and
+-- the src line will be unhelpful (ToDo)
+
+rnSyntaxNames gbl_env source_fvs
+ = doptRn Opt_NoImplicitPrelude `thenRn` \ no_prelude ->
+ if not no_prelude then
+ returnRn (source_fvs, vanillaSyntaxMap)
+ else
+
+ -- There's a -fno-implicit-prelude flag,
+ -- so build the re-mapping function
+ let
+ reqd_syntax_list = filter is_reqd syntaxList
+ is_reqd (n,_) = n `elemNameSet` source_fvs
+ lookup (n,rn) = lookupSrcName gbl_env rn `thenRn` \ rn' ->
+ returnRn (n,rn')
+ in
+ mapRn lookup reqd_syntax_list `thenRn` \ rn_syntax_list ->
+ let
+ -- Delete the proxies and add the actuals
+ proxies = map fst rn_syntax_list
+ actuals = map snd rn_syntax_list
+ new_source_fvs = (proxies `delFVs` source_fvs) `plusFV` mkFVs actuals
+
+ syntax_env = mkNameEnv rn_syntax_list
+ syntax_map n = lookupNameEnv syntax_env n `orElse` n
+ in
+ returnRn (new_source_fvs, syntax_map)
+\end{code}
+
+
+%*********************************************************
+%* *