+ -- The rhs is already tidied
+
+ = ((orig_env', occ_env', subst_env'), id')
+ where
+ (orig_env', occ_env', name') = tidyTopName mod ns2 occ_env2
+ is_external
+ (idName id)
+ ty' = tidyTopType (idType id)
+ idinfo = tidyTopIdInfo rec_tidy_env is_external
+ (idInfo id) unfold_info arity
+ (lookupCgInfo cg_info_env name')
+
+ id' = mkVanillaGlobal name' ty' idinfo
+
+ subst_env' = extendVarEnv subst_env2 id id'
+
+ maybe_external = lookupVarEnv ext_ids id
+ is_external = isJust maybe_external
+
+ -- Expose an unfolding if ext_ids tells us to
+ -- Remember that ext_ids maps an Id to a Bool:
+ -- True to show the unfolding, False to hide it
+ show_unfold = maybe_external `orElse` False
+ unfold_info | show_unfold = mkTopUnfolding tidy_rhs
+ | otherwise = noUnfolding
+
+ -- Usually the Id will have an accurate arity on it, because
+ -- the simplifier has just run, but not always.
+ -- One case I found was when the last thing the simplifier
+ -- did was to let-bind a non-atomic argument and then float
+ -- it to the top level. So it seems more robust just to
+ -- fix it here.
+ arity = exprArity rhs
+
+
+
+-- tidyTopIdInfo creates the final IdInfo for top-level
+-- binders. There are two delicate pieces:
+--
+-- * Arity. After CoreTidy, this arity must not change any more.
+-- Indeed, CorePrep must eta expand where necessary to make
+-- the manifest arity equal to the claimed arity.
+--
+-- * CAF info, which comes from the CoreToStg pass via a knot.
+-- The CAF info will not be looked at by the downstream stuff:
+-- it *generates* it, and knot-ties it back. It will only be
+-- looked at by (a) MkIface when generating an interface file
+-- (b) In GHCi, importing modules
+-- Nevertheless, we add the info here so that it propagates to all
+-- occurrences of the binders in RHSs, and hence to occurrences in
+-- unfoldings, which are inside Ids imported by GHCi. Ditto RULES.
+--
+-- An alterative would be to do a second pass over the unfoldings
+-- of Ids, and rules, right at the top, but that would be a pain.
+
+tidyTopIdInfo tidy_env is_external idinfo unfold_info arity cg_info
+ | opt_OmitInterfacePragmas || not is_external
+ -- Only basic info if the Id isn't external, or if we don't have -O
+ = basic_info
+
+ | otherwise -- Add extra optimisation info
+ = basic_info
+ `setInlinePragInfo` inlinePragInfo idinfo
+ `setUnfoldingInfo` unfold_info
+ `setWorkerInfo` tidyWorker tidy_env (workerInfo idinfo)
+ -- NB: we throw away the Rules
+ -- They have already been extracted by findExternalRules
+
+ where
+ -- baasic_info is attached to every top-level binder
+ basic_info = vanillaIdInfo
+ `setCgInfo` cg_info
+ `setArityInfo` arity
+ `setAllStrictnessInfo` newStrictnessInfo idinfo
+
+-- This is where we set names to local/global based on whether they really are
+-- externally visible (see comment at the top of this module). If the name
+-- was previously local, we have to give it a unique occurrence name if
+-- we intend to globalise it.
+tidyTopName mod ns occ_env external name
+ | global && internal = (ns, occ_env, localiseName name)
+
+ | global && external = (ns, occ_env, name)
+ -- Global names are assumed to have been allocated by the renamer,
+ -- so they already have the "right" unique
+ -- And it's a system-wide unique too
+
+ | local && internal = (ns_w_local, occ_env', new_local_name)
+ -- Even local, internal names must get a unique occurrence, because
+ -- if we do -split-objs we globalise the name later, in the code generator
+ --
+ -- Similarly, we must make sure it has a system-wide Unique, because
+ -- the byte-code generator builds a system-wide Name->BCO symbol table
+
+ | local && external = case lookupFM ns_names key of
+ Just orig -> (ns, occ_env', orig)
+ Nothing -> (ns_w_global, occ_env', new_global_name)
+ -- If we want to globalise a currently-local name, check
+ -- whether we have already assigned a unique for it.
+ -- If so, use it; if not, extend the table (ns_w_global).
+ -- This is needed when *re*-compiling a module in GHCi; we want to
+ -- use the same name for externally-visible things as we did before.