ca021222dae6bd078c0f19729168abc15d645315
[ghc-hetmet.git] / compiler / main / TidyPgm.lhs
1
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Tidying up Core}
5
6 \begin{code}
7 module TidyPgm( mkBootModDetailsDs, mkBootModDetailsTc, tidyProgram ) where
8
9 #include "HsVersions.h"
10
11 import TcRnTypes
12 import FamInstEnv
13 import DynFlags
14 import CoreSyn
15 import CoreUnfold
16 import CoreFVs
17 import CoreTidy
18 import PprCore
19 import CoreLint
20 import CoreUtils
21 import VarEnv
22 import VarSet
23 import Var hiding( mkGlobalId )
24 import Id
25 import IdInfo
26 import InstEnv
27 import NewDemand
28 import BasicTypes
29 import Name
30 import NameSet
31 import IfaceEnv
32 import NameEnv
33 import OccName
34 import TcType
35 import DataCon
36 import TyCon
37 import Module
38 import HscTypes
39 import Maybes
40 import ErrUtils
41 import UniqSupply
42 import Outputable
43 import FastBool hiding ( fastOr )
44
45 import Data.List        ( partition )
46 import Data.Maybe       ( isJust )
47 import Data.IORef       ( IORef, readIORef, writeIORef )
48
49 _dummy :: FS.FastString
50 _dummy = FSLIT("")
51 \end{code}
52
53
54 Constructing the TypeEnv, Instances, Rules from which the ModIface is
55 constructed, and which goes on to subsequent modules in --make mode.
56
57 Most of the interface file is obtained simply by serialising the
58 TypeEnv.  One important consequence is that if the *interface file*
59 has pragma info if and only if the final TypeEnv does. This is not so
60 important for *this* module, but it's essential for ghc --make:
61 subsequent compilations must not see (e.g.) the arity if the interface
62 file does not contain arity If they do, they'll exploit the arity;
63 then the arity might change, but the iface file doesn't change =>
64 recompilation does not happen => disaster. 
65
66 For data types, the final TypeEnv will have a TyThing for the TyCon,
67 plus one for each DataCon; the interface file will contain just one
68 data type declaration, but it is de-serialised back into a collection
69 of TyThings.
70
71 %************************************************************************
72 %*                                                                      *
73                 Plan A: simpleTidyPgm
74 %*                                                                      * 
75 %************************************************************************
76
77
78 Plan A: mkBootModDetails: omit pragmas, make interfaces small
79 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80 * Ignore the bindings
81
82 * Drop all WiredIn things from the TypeEnv 
83         (we never want them in interface files)
84
85 * Retain all TyCons and Classes in the TypeEnv, to avoid
86         having to find which ones are mentioned in the
87         types of exported Ids
88
89 * Trim off the constructors of non-exported TyCons, both
90         from the TyCon and from the TypeEnv
91
92 * Drop non-exported Ids from the TypeEnv
93
94 * Tidy the types of the DFunIds of Instances, 
95   make them into GlobalIds, (they already have External Names)
96   and add them to the TypeEnv
97
98 * Tidy the types of the (exported) Ids in the TypeEnv,
99   make them into GlobalIds (they already have External Names)
100
101 * Drop rules altogether
102
103 * Tidy the bindings, to ensure that the Caf and Arity
104   information is correct for each top-level binder; the 
105   code generator needs it. And to ensure that local names have
106   distinct OccNames in case of object-file splitting
107
108 \begin{code}
109 -- This is Plan A: make a small type env when typechecking only,
110 -- or when compiling a hs-boot file, or simply when not using -O
111 --
112 -- We don't look at the bindings at all -- there aren't any
113 -- for hs-boot files
114
115 mkBootModDetailsTc :: HscEnv -> TcGblEnv -> IO ModDetails
116 mkBootModDetailsTc hsc_env 
117         TcGblEnv{ tcg_exports   = exports,
118                   tcg_type_env  = type_env,
119                   tcg_insts     = insts,
120                   tcg_fam_insts = fam_insts
121                 }
122   = mkBootModDetails hsc_env exports type_env insts fam_insts
123
124 mkBootModDetailsDs :: HscEnv -> ModGuts -> IO ModDetails
125 mkBootModDetailsDs hsc_env 
126         ModGuts{ mg_exports   = exports,
127                  mg_types     = type_env,
128                  mg_insts     = insts,
129                  mg_fam_insts = fam_insts
130                 }
131   = mkBootModDetails hsc_env exports type_env insts fam_insts
132   
133 mkBootModDetails :: HscEnv -> [AvailInfo] -> NameEnv TyThing
134                  -> [Instance] -> [FamInstEnv.FamInst] -> IO ModDetails
135 mkBootModDetails hsc_env exports type_env insts fam_insts
136   = do  { let dflags = hsc_dflags hsc_env 
137         ; showPass dflags "Tidy [hoot] type env"
138
139         ; let { insts'     = tidyInstances tidyExternalId insts
140               ; dfun_ids   = map instanceDFunId insts'
141               ; type_env1  = tidyBootTypeEnv (availsToNameSet exports) type_env
142               ; type_env'  = extendTypeEnvWithIds type_env1 dfun_ids
143               }
144         ; return (ModDetails { md_types     = type_env'
145                              , md_insts     = insts'
146                              , md_fam_insts = fam_insts
147                              , md_rules     = []
148                              , md_exports   = exports
149                              , md_vect_info = noVectInfo
150                              })
151         }
152   where
153
154 tidyBootTypeEnv :: NameSet -> TypeEnv -> TypeEnv
155 tidyBootTypeEnv exports type_env 
156   = tidyTypeEnv True exports type_env final_ids
157   where
158         -- Find the LocalIds in the type env that are exported
159         -- Make them into GlobalIds, and tidy their types
160         --
161         -- It's very important to remove the non-exported ones
162         -- because we don't tidy the OccNames, and if we don't remove
163         -- the non-exported ones we'll get many things with the
164         -- same name in the interface file, giving chaos.
165     final_ids = [ tidyExternalId id
166                 | id <- typeEnvIds type_env
167                 , isLocalId id
168                 , keep_it id ]
169
170         -- default methods have their export flag set, but everything
171         -- else doesn't (yet), because this is pre-desugaring, so we
172         -- must test both.
173     keep_it id = isExportedId id || idName id `elemNameSet` exports
174
175
176 tidyExternalId :: Id -> Id
177 -- Takes an LocalId with an External Name, 
178 -- makes it into a GlobalId with VanillaIdInfo, and tidies its type
179 -- (NB: vanillaIdInfo makes a conservative assumption about Caf-hood.)
180 tidyExternalId id 
181   = ASSERT2( isLocalId id && isExternalName (idName id), ppr id )
182     mkVanillaGlobal (idName id) (tidyTopType (idType id)) vanillaIdInfo
183 \end{code}
184
185
186 %************************************************************************
187 %*                                                                      *
188         Plan B: tidy bindings, make TypeEnv full of IdInfo
189 %*                                                                      * 
190 %************************************************************************
191
192 Plan B: include pragmas, make interfaces 
193 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 * Figure out which Ids are externally visible
195
196 * Tidy the bindings, externalising appropriate Ids
197
198 * Drop all Ids from the TypeEnv, and add all the External Ids from 
199   the bindings.  (This adds their IdInfo to the TypeEnv; and adds
200   floated-out Ids that weren't even in the TypeEnv before.)
201
202 Step 1: Figure out external Ids
203 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204 First we figure out which Ids are "external" Ids.  An
205 "external" Id is one that is visible from outside the compilation
206 unit.  These are
207         a) the user exported ones
208         b) ones mentioned in the unfoldings, workers, 
209            or rules of externally-visible ones 
210 This exercise takes a sweep of the bindings bottom to top.  Actually,
211 in Step 2 we're also going to need to know which Ids should be
212 exported with their unfoldings, so we produce not an IdSet but an
213 IdEnv Bool
214
215
216 Step 2: Tidy the program
217 ~~~~~~~~~~~~~~~~~~~~~~~~
218 Next we traverse the bindings top to bottom.  For each *top-level*
219 binder
220
221  1. Make it into a GlobalId; its IdDetails becomes VanillaGlobal, 
222     reflecting the fact that from now on we regard it as a global, 
223     not local, Id
224
225  2. Give it a system-wide Unique.
226     [Even non-exported things need system-wide Uniques because the
227     byte-code generator builds a single Name->BCO symbol table.]
228
229     We use the NameCache kept in the HscEnv as the
230     source of such system-wide uniques.
231
232     For external Ids, use the original-name cache in the NameCache
233     to ensure that the unique assigned is the same as the Id had 
234     in any previous compilation run.
235   
236  3. If it's an external Id, make it have a External Name, otherwise
237     make it have an Internal Name.
238     This is used by the code generator to decide whether
239     to make the label externally visible
240
241  4. Give external Ids a "tidy" OccName.  This means
242     we can print them in interface files without confusing 
243     "x" (unique 5) with "x" (unique 10).
244   
245  5. Give it its UTTERLY FINAL IdInfo; in ptic, 
246         * its unfolding, if it should have one
247         
248         * its arity, computed from the number of visible lambdas
249
250         * its CAF info, computed from what is free in its RHS
251
252                 
253 Finally, substitute these new top-level binders consistently
254 throughout, including in unfoldings.  We also tidy binders in
255 RHSs, so that they print nicely in interfaces.
256
257 \begin{code}
258 tidyProgram :: HscEnv -> ModGuts -> IO (CgGuts, ModDetails)
259 tidyProgram hsc_env
260                 (ModGuts {      mg_module = mod, mg_exports = exports, 
261                                 mg_types = type_env, 
262                                 mg_insts = insts, mg_fam_insts = fam_insts,
263                                 mg_binds = binds, 
264                                 mg_rules = imp_rules,
265                                 mg_vect_info = vect_info,
266                                 mg_dir_imps = dir_imps, 
267                                 mg_deps = deps, 
268                                 mg_foreign = foreign_stubs,
269                                 mg_hpc_info = hpc_info,
270                                 mg_modBreaks = modBreaks })
271
272   = do  { let dflags = hsc_dflags hsc_env
273         ; showPass dflags "Tidy Core"
274
275         ; let { omit_prags = dopt Opt_OmitInterfacePragmas dflags
276               ; ext_ids = findExternalIds omit_prags binds
277               ; ext_rules 
278                    | omit_prags = []
279                    | otherwise  = findExternalRules binds imp_rules ext_ids
280                 -- findExternalRules filters imp_rules to avoid binders that 
281                 -- aren't externally visible; but the externally-visible binders 
282                 -- are computed (by findExternalIds) assuming that all orphan
283                 -- rules are exported (they get their Exported flag set in the desugarer)
284                 -- So in fact we may export more than we need. 
285                 -- (It's a sort of mutual recursion.)
286         }
287
288         ; (tidy_env, tidy_binds) <- tidyTopBinds hsc_env mod type_env ext_ids 
289                                                  binds
290
291         ; let { export_set = availsToNameSet exports
292               ; final_ids  = [ id | id <- bindersOfBinds tidy_binds, 
293                                     isExternalName (idName id)]
294               ; tidy_type_env = tidyTypeEnv omit_prags export_set type_env 
295                                             final_ids
296               ; tidy_insts    = tidyInstances (lookup_dfun tidy_type_env) insts
297                 -- A DFunId will have a binding in tidy_binds, and so
298                 -- will now be in final_env, replete with IdInfo
299                 -- Its name will be unchanged since it was born, but
300                 -- we want Global, IdInfo-rich (or not) DFunId in the
301                 -- tidy_insts
302
303               ; tidy_rules = tidyRules tidy_env ext_rules
304                 -- You might worry that the tidy_env contains IdInfo-rich stuff
305                 -- and indeed it does, but if omit_prags is on, ext_rules is
306                 -- empty
307
308               ; alg_tycons = filter isAlgTyCon (typeEnvTyCons type_env)
309               }
310
311         ; endPass dflags "Tidy Core" Opt_D_dump_simpl tidy_binds
312         ; dumpIfSet_core dflags Opt_D_dump_simpl
313                 "Tidy Core Rules"
314                 (pprRules tidy_rules)
315
316         ; let dir_imp_mods = map fst (moduleEnvElts dir_imps)
317
318         ; return (CgGuts { cg_module   = mod, 
319                            cg_tycons   = alg_tycons,
320                            cg_binds    = tidy_binds,
321                            cg_dir_imps = dir_imp_mods,
322                            cg_foreign  = foreign_stubs,
323                            cg_dep_pkgs = dep_pkgs deps,
324                            cg_hpc_info = hpc_info,
325                            cg_modBreaks = modBreaks }, 
326
327                    ModDetails { md_types     = tidy_type_env,
328                                 md_rules     = tidy_rules,
329                                 md_insts     = tidy_insts,
330                                 md_fam_insts = fam_insts,
331                                 md_exports   = exports,
332                                 md_vect_info = vect_info    -- is already tidy
333                               })
334         }
335
336 lookup_dfun :: TypeEnv -> Var -> Id
337 lookup_dfun type_env dfun_id
338   = case lookupTypeEnv type_env (idName dfun_id) of
339         Just (AnId dfun_id') -> dfun_id'
340         _other -> pprPanic "lookup_dfun" (ppr dfun_id)
341
342 --------------------------
343 tidyTypeEnv :: Bool -> NameSet -> TypeEnv -> [Id] -> TypeEnv
344
345 -- The competed type environment is gotten from
346 --      Dropping any wired-in things, and then
347 --      a) keeping the types and classes
348 --      b) removing all Ids, 
349 --      c) adding Ids with correct IdInfo, including unfoldings,
350 --              gotten from the bindings
351 -- From (c) we keep only those Ids with External names;
352 --          the CoreTidy pass makes sure these are all and only
353 --          the externally-accessible ones
354 -- This truncates the type environment to include only the 
355 -- exported Ids and things needed from them, which saves space
356
357 tidyTypeEnv omit_prags exports type_env final_ids
358   = let type_env1 = filterNameEnv keep_it type_env
359         type_env2 = extendTypeEnvWithIds type_env1 final_ids
360         type_env3 | omit_prags = mapNameEnv (trimThing exports) type_env2
361                   | otherwise  = type_env2
362     in 
363     type_env3
364   where
365         -- We keep GlobalIds, because they won't appear 
366         -- in the bindings from which final_ids are derived!
367         -- (The bindings bind LocalIds.)
368     keep_it thing | isWiredInThing thing = False
369     keep_it (AnId id) = isGlobalId id   -- Keep GlobalIds (e.g. class ops)
370     keep_it _other    = True            -- Keep all TyCons, DataCons, and Classes
371
372 --------------------------
373 isWiredInThing :: TyThing -> Bool
374 isWiredInThing thing = isWiredInName (getName thing)
375
376 --------------------------
377 trimThing :: NameSet -> TyThing -> TyThing
378 -- Trim off inessentials, for boot files and no -O
379 trimThing exports (ATyCon tc)
380    | not (mustExposeTyCon exports tc)
381    = ATyCon (makeTyConAbstract tc)
382
383 trimThing _exports (AnId id)
384    | not (isImplicitId id) 
385    = AnId (id `setIdInfo` vanillaIdInfo)
386
387 trimThing _exports other_thing 
388   = other_thing
389
390
391 mustExposeTyCon :: NameSet      -- Exports
392                 -> TyCon        -- The tycon
393                 -> Bool         -- Can its rep be hidden?
394 -- We are compiling without -O, and thus trying to write as little as 
395 -- possible into the interface file.  But we must expose the details of
396 -- any data types whose constructors or fields are exported
397 mustExposeTyCon exports tc
398   | not (isAlgTyCon tc)         -- Synonyms
399   = True
400   | isEnumerationTyCon tc       -- For an enumeration, exposing the constructors
401   = True                        -- won't lead to the need for further exposure
402                                 -- (This includes data types with no constructors.)
403   | isOpenTyCon tc              -- Open type family
404   = True
405
406   | otherwise                   -- Newtype, datatype
407   = any exported_con (tyConDataCons tc)
408         -- Expose rep if any datacon or field is exported
409
410   || (isNewTyCon tc && isFFITy (snd (newTyConRhs tc)))
411         -- Expose the rep for newtypes if the rep is an FFI type.  
412         -- For a very annoying reason.  'Foreign import' is meant to
413         -- be able to look through newtypes transparently, but it
414         -- can only do that if it can "see" the newtype representation
415   where
416     exported_con con = any (`elemNameSet` exports) 
417                            (dataConName con : dataConFieldLabels con)
418
419 tidyInstances :: (DFunId -> DFunId) -> [Instance] -> [Instance]
420 tidyInstances tidy_dfun ispecs
421   = map tidy ispecs
422   where
423     tidy ispec = setInstanceDFunId ispec $
424                  tidy_dfun (instanceDFunId ispec)
425 \end{code}
426
427
428 %************************************************************************
429 %*                                                                      *
430 \subsection{Step 1: finding externals}
431 %*                                                                      * 
432 %************************************************************************
433
434 \begin{code}
435 findExternalIds :: Bool
436                 -> [CoreBind]
437                 -> IdEnv Bool   -- In domain => external
438                                 -- Range = True <=> show unfolding
439         -- Step 1 from the notes above
440 findExternalIds omit_prags binds
441   | omit_prags
442   = mkVarEnv [ (id,False) | id <- bindersOfBinds binds, isExportedId id ]
443
444   | otherwise
445   = foldr find emptyVarEnv binds
446   where
447     find (NonRec id rhs) needed
448         | need_id needed id = addExternal (id,rhs) needed
449         | otherwise         = needed
450     find (Rec prs) needed   = find_prs prs needed
451
452         -- For a recursive group we have to look for a fixed point
453     find_prs prs needed 
454         | null needed_prs = needed
455         | otherwise       = find_prs other_prs new_needed
456         where
457           (needed_prs, other_prs) = partition (need_pr needed) prs
458           new_needed = foldr addExternal needed needed_prs
459
460         -- The 'needed' set contains the Ids that are needed by earlier
461         -- interface file emissions.  If the Id isn't in this set, and isn't
462         -- exported, there's no need to emit anything
463     need_id needed_set id       = id `elemVarEnv` needed_set || isExportedId id 
464     need_pr needed_set (id,_)   = need_id needed_set id
465
466 addExternal :: (Id,CoreExpr) -> IdEnv Bool -> IdEnv Bool
467 -- The Id is needed; extend the needed set
468 -- with it and its dependents (free vars etc)
469 addExternal (id,rhs) needed
470   = extendVarEnv (foldVarSet add_occ needed new_needed_ids)
471                  id show_unfold
472   where
473     add_occ id needed | id `elemVarEnv` needed = needed
474                       | otherwise              = extendVarEnv needed id False
475         -- "False" because we don't know we need the Id's unfolding
476         -- Don't override existing bindings; we might have already set it to True
477
478     new_needed_ids = worker_ids `unionVarSet`
479                      unfold_ids `unionVarSet`
480                      spec_ids
481
482     idinfo         = idInfo id
483     dont_inline    = isNeverActive (inlinePragInfo idinfo)
484     loop_breaker   = isNonRuleLoopBreaker (occInfo idinfo)
485     bottoming_fn   = isBottomingSig (newStrictnessInfo idinfo `orElse` topSig)
486     spec_ids       = specInfoFreeVars (specInfo idinfo)
487     worker_info    = workerInfo idinfo
488
489         -- Stuff to do with the Id's unfolding
490         -- The simplifier has put an up-to-date unfolding
491         -- in the IdInfo, but the RHS will do just as well
492     unfolding    = unfoldingInfo idinfo
493     rhs_is_small = not (neverUnfold unfolding)
494
495         -- We leave the unfolding there even if there is a worker
496         -- In GHCI the unfolding is used by importers
497         -- When writing an interface file, we omit the unfolding 
498         -- if there is a worker
499     show_unfold = not bottoming_fn       &&     -- Not necessary
500                   not dont_inline        &&
501                   not loop_breaker       &&
502                   rhs_is_small                  -- Small enough
503
504     unfold_ids | show_unfold = exprSomeFreeVars isLocalId rhs
505                | otherwise   = emptyVarSet
506
507     worker_ids = case worker_info of
508                    HasWorker work_id _ -> unitVarSet work_id
509                    _otherwise          -> emptyVarSet
510 \end{code}
511
512
513 \begin{code}
514 findExternalRules :: [CoreBind]
515                   -> [CoreRule] -- Non-local rules (i.e. ones for imported fns)
516                   -> IdEnv a    -- Ids that are exported, so we need their rules
517                   -> [CoreRule]
518   -- The complete rules are gotten by combining
519   --    a) the non-local rules
520   --    b) rules embedded in the top-level Ids
521 findExternalRules binds non_local_rules ext_ids
522   = filter (not . internal_rule) (non_local_rules ++ local_rules)
523   where
524     local_rules  = [ rule
525                    | id <- bindersOfBinds binds,
526                      id `elemVarEnv` ext_ids,
527                      rule <- idCoreRules id
528                    ]
529
530     internal_rule rule
531         =  any internal_id (varSetElems (ruleLhsFreeIds rule))
532                 -- Don't export a rule whose LHS mentions a locally-defined
533                 --  Id that is completely internal (i.e. not visible to an
534                 -- importing module)
535
536     internal_id id = not (id `elemVarEnv` ext_ids)
537 \end{code}
538
539
540
541 %************************************************************************
542 %*                                                                      *
543 \subsection{Step 2: top-level tidying}
544 %*                                                                      *
545 %************************************************************************
546
547
548 \begin{code}
549 -- TopTidyEnv: when tidying we need to know
550 --   * nc_var: The NameCache, containing a unique supply and any pre-ordained Names.  
551 --        These may have arisen because the
552 --        renamer read in an interface file mentioning M.$wf, say,
553 --        and assigned it unique r77.  If, on this compilation, we've
554 --        invented an Id whose name is $wf (but with a different unique)
555 --        we want to rename it to have unique r77, so that we can do easy
556 --        comparisons with stuff from the interface file
557 --
558 --   * occ_env: The TidyOccEnv, which tells us which local occurrences 
559 --     are 'used'
560 --
561 --   * subst_env: A Var->Var mapping that substitutes the new Var for the old
562
563 tidyTopBinds :: HscEnv
564              -> Module
565              -> TypeEnv
566              -> IdEnv Bool      -- Domain = Ids that should be external
567                                 -- True <=> their unfolding is external too
568              -> [CoreBind]
569              -> IO (TidyEnv, [CoreBind])
570
571 tidyTopBinds hsc_env mod type_env ext_ids binds
572   = tidy init_env binds
573   where
574     nc_var = hsc_NC hsc_env 
575
576         -- We also make sure to avoid any exported binders.  Consider
577         --      f{-u1-} = 1     -- Local decl
578         --      ...
579         --      f{-u2-} = 2     -- Exported decl
580         --
581         -- The second exported decl must 'get' the name 'f', so we
582         -- have to put 'f' in the avoids list before we get to the first
583         -- decl.  tidyTopId then does a no-op on exported binders.
584     init_env = (initTidyOccEnv avoids, emptyVarEnv)
585     avoids   = [getOccName name | bndr <- typeEnvIds type_env,
586                                   let name = idName bndr,
587                                   isExternalName name]
588                 -- In computing our "avoids" list, we must include
589                 --      all implicit Ids
590                 --      all things with global names (assigned once and for
591                 --                                      all by the renamer)
592                 -- since their names are "taken".
593                 -- The type environment is a convenient source of such things.
594
595     this_pkg = thisPackage (hsc_dflags hsc_env)
596
597     tidy env []     = return (env, [])
598     tidy env (b:bs) = do { (env1, b')  <- tidyTopBind this_pkg mod nc_var ext_ids env b
599                          ; (env2, bs') <- tidy env1 bs
600                          ; return (env2, b':bs') }
601
602 ------------------------
603 tidyTopBind  :: PackageId
604              -> Module
605              -> IORef NameCache -- For allocating new unique names
606              -> IdEnv Bool      -- Domain = Ids that should be external
607                                 -- True <=> their unfolding is external too
608              -> TidyEnv -> CoreBind
609              -> IO (TidyEnv, CoreBind)
610
611 tidyTopBind this_pkg mod nc_var ext_ids (occ_env1,subst1) (NonRec bndr rhs)
612   = do  { (occ_env2, name') <- tidyTopName mod nc_var ext_ids occ_env1 bndr
613         ; let   { (bndr', rhs') = tidyTopPair ext_ids tidy_env2 caf_info name' (bndr, rhs)
614                 ; subst2        = extendVarEnv subst1 bndr bndr'
615                 ; tidy_env2     = (occ_env2, subst2) }
616         ; return (tidy_env2, NonRec bndr' rhs') }
617   where
618     caf_info = hasCafRefs this_pkg subst1 (idArity bndr) rhs
619
620 tidyTopBind this_pkg mod nc_var ext_ids (occ_env1,subst1) (Rec prs)
621   = do  { (occ_env2, names') <- tidyTopNames mod nc_var ext_ids occ_env1 bndrs
622         ; let   { prs'      = zipWith (tidyTopPair ext_ids tidy_env2 caf_info)
623                                       names' prs
624                 ; subst2    = extendVarEnvList subst1 (bndrs `zip` map fst prs')
625                 ; tidy_env2 = (occ_env2, subst2) }
626         ; return (tidy_env2, Rec prs') }
627   where
628     bndrs = map fst prs
629
630         -- the CafInfo for a recursive group says whether *any* rhs in
631         -- the group may refer indirectly to a CAF (because then, they all do).
632     caf_info 
633         | or [ mayHaveCafRefs (hasCafRefs this_pkg subst1 (idArity bndr) rhs)
634              | (bndr,rhs) <- prs ] = MayHaveCafRefs
635         | otherwise                = NoCafRefs
636
637 --------------------------------------------------------------------
638 --              tidyTopName
639 -- This is where we set names to local/global based on whether they really are 
640 -- externally visible (see comment at the top of this module).  If the name
641 -- was previously local, we have to give it a unique occurrence name if
642 -- we intend to externalise it.
643 tidyTopNames :: Module -> IORef NameCache -> VarEnv Bool -> TidyOccEnv
644              -> [Id] -> IO (TidyOccEnv, [Name])
645 tidyTopNames _mod _nc_var _ext_ids occ_env [] = return (occ_env, [])
646 tidyTopNames mod nc_var ext_ids occ_env (id:ids)
647   = do  { (occ_env1, name)  <- tidyTopName  mod nc_var ext_ids occ_env id
648         ; (occ_env2, names) <- tidyTopNames mod nc_var ext_ids occ_env1 ids
649         ; return (occ_env2, name:names) }
650
651 tidyTopName :: Module -> IORef NameCache -> VarEnv Bool -> TidyOccEnv
652             -> Id -> IO (TidyOccEnv, Name)
653 tidyTopName mod nc_var ext_ids occ_env id
654   | global && internal = return (occ_env, localiseName name)
655
656   | global && external = return (occ_env, name)
657         -- Global names are assumed to have been allocated by the renamer,
658         -- so they already have the "right" unique
659         -- And it's a system-wide unique too
660
661   -- Now we get to the real reason that all this is in the IO Monad:
662   -- we have to update the name cache in a nice atomic fashion
663
664   | local  && internal = do { nc <- readIORef nc_var
665                             ; let (nc', new_local_name) = mk_new_local nc
666                             ; writeIORef nc_var nc'
667                             ; return (occ_env', new_local_name) }
668         -- Even local, internal names must get a unique occurrence, because
669         -- if we do -split-objs we externalise the name later, in the code generator
670         --
671         -- Similarly, we must make sure it has a system-wide Unique, because
672         -- the byte-code generator builds a system-wide Name->BCO symbol table
673
674   | local  && external = do { nc <- readIORef nc_var
675                             ; let (nc', new_external_name) = mk_new_external nc
676                             ; writeIORef nc_var nc'
677                             ; return (occ_env', new_external_name) }
678
679   | otherwise = panic "tidyTopName"
680   where
681     name        = idName id
682     external    = id `elemVarEnv` ext_ids
683     global      = isExternalName name
684     local       = not global
685     internal    = not external
686     loc         = nameSrcSpan name
687
688     (occ_env', occ') = tidyOccName occ_env (nameOccName name)
689
690     mk_new_local nc = (nc { nsUniqs = us2 }, mkInternalName uniq occ' loc)
691                     where
692                       (us1, us2) = splitUniqSupply (nsUniqs nc)
693                       uniq       = uniqFromSupply us1
694
695     mk_new_external nc = allocateGlobalBinder nc mod occ' loc
696         -- If we want to externalise a currently-local name, check
697         -- whether we have already assigned a unique for it.
698         -- If so, use it; if not, extend the table.
699         -- All this is done by allcoateGlobalBinder.
700         -- This is needed when *re*-compiling a module in GHCi; we must
701         -- use the same name for externally-visible things as we did before.
702
703
704 -----------------------------------------------------------
705 tidyTopPair :: VarEnv Bool
706             -> TidyEnv  -- The TidyEnv is used to tidy the IdInfo
707                         -- It is knot-tied: don't look at it!
708             -> CafInfo
709             -> Name             -- New name
710             -> (Id, CoreExpr)   -- Binder and RHS before tidying
711             -> (Id, CoreExpr)
712         -- This function is the heart of Step 2
713         -- The rec_tidy_env is the one to use for the IdInfo
714         -- It's necessary because when we are dealing with a recursive
715         -- group, a variable late in the group might be mentioned
716         -- in the IdInfo of one early in the group
717
718 tidyTopPair ext_ids rhs_tidy_env caf_info name' (bndr, rhs)
719   = (bndr', rhs')
720   where
721     bndr' = mkGlobalId details name' ty' idinfo'
722         -- Preserve the GlobalIdDetails of existing global-ids
723     details = case globalIdDetails bndr of      
724                 NotGlobalId -> VanillaGlobal
725                 old_details -> old_details
726     ty'     = tidyTopType (idType bndr)
727     rhs'    = tidyExpr rhs_tidy_env rhs
728     idinfo  = idInfo bndr
729     idinfo' = tidyTopIdInfo (isJust maybe_external)
730                             idinfo unfold_info worker_info
731                             arity caf_info
732
733     -- Expose an unfolding if ext_ids tells us to
734     -- Remember that ext_ids maps an Id to a Bool: 
735     --  True to show the unfolding, False to hide it
736     maybe_external = lookupVarEnv ext_ids bndr
737     show_unfold = maybe_external `orElse` False
738     unfold_info | show_unfold = mkTopUnfolding rhs'
739                 | otherwise   = noUnfolding
740     worker_info = tidyWorker rhs_tidy_env show_unfold (workerInfo idinfo)
741
742     -- Usually the Id will have an accurate arity on it, because
743     -- the simplifier has just run, but not always. 
744     -- One case I found was when the last thing the simplifier
745     -- did was to let-bind a non-atomic argument and then float
746     -- it to the top level. So it seems more robust just to
747     -- fix it here.
748     arity = exprArity rhs
749
750
751 -- tidyTopIdInfo creates the final IdInfo for top-level
752 -- binders.  There are two delicate pieces:
753 --
754 --  * Arity.  After CoreTidy, this arity must not change any more.
755 --      Indeed, CorePrep must eta expand where necessary to make
756 --      the manifest arity equal to the claimed arity.
757 --
758 --  * CAF info.  This must also remain valid through to code generation.
759 --      We add the info here so that it propagates to all
760 --      occurrences of the binders in RHSs, and hence to occurrences in
761 --      unfoldings, which are inside Ids imported by GHCi. Ditto RULES.
762 --      CoreToStg makes use of this when constructing SRTs.
763 tidyTopIdInfo :: Bool -> IdInfo -> Unfolding
764               -> WorkerInfo -> ArityInfo -> CafInfo
765               -> IdInfo
766 tidyTopIdInfo is_external idinfo unfold_info worker_info arity caf_info
767   | not is_external     -- For internal Ids (not externally visible)
768   = vanillaIdInfo       -- we only need enough info for code generation
769                         -- Arity and strictness info are enough;
770                         --      c.f. CoreTidy.tidyLetBndr
771         `setCafInfo`           caf_info
772         `setArityInfo`         arity
773         `setAllStrictnessInfo` newStrictnessInfo idinfo
774
775   | otherwise           -- Externally-visible Ids get the whole lot
776   = vanillaIdInfo
777         `setCafInfo`           caf_info
778         `setArityInfo`         arity
779         `setAllStrictnessInfo` newStrictnessInfo idinfo
780         `setInlinePragInfo`    inlinePragInfo idinfo
781         `setUnfoldingInfo`     unfold_info
782         `setWorkerInfo`        worker_info
783                 -- NB: we throw away the Rules
784                 -- They have already been extracted by findExternalRules
785
786
787
788 ------------  Worker  --------------
789 tidyWorker :: TidyEnv -> Bool -> WorkerInfo -> WorkerInfo
790 tidyWorker _tidy_env _show_unfold NoWorker
791   = NoWorker
792 tidyWorker tidy_env show_unfold (HasWorker work_id wrap_arity) 
793   | show_unfold = HasWorker (tidyVarOcc tidy_env work_id) wrap_arity
794   | otherwise   = NoWorker
795     -- NB: do *not* expose the worker if show_unfold is off,
796     --     because that means this thing is a loop breaker or
797     --     marked NOINLINE or something like that
798     -- This is important: if you expose the worker for a loop-breaker
799     -- then you can make the simplifier go into an infinite loop, because
800     -- in effect the unfolding is exposed.  See Trac #1709
801     -- 
802     -- You might think that if show_unfold is False, then the thing should
803     -- not be w/w'd in the first place.  But a legitimate reason is this:
804     --    the function returns bottom
805     -- In this case, show_unfold will be false (we don't expose unfoldings
806     -- for bottoming functions), but we might still have a worker/wrapper
807     -- split (see Note [Worker-wrapper for bottoming functions] in WorkWrap.lhs
808 \end{code}
809
810 %************************************************************************
811 %*                                                                      *
812 \subsection{Figuring out CafInfo for an expression}
813 %*                                                                      *
814 %************************************************************************
815
816 hasCafRefs decides whether a top-level closure can point into the dynamic heap.
817 We mark such things as `MayHaveCafRefs' because this information is
818 used to decide whether a particular closure needs to be referenced
819 in an SRT or not.
820
821 There are two reasons for setting MayHaveCafRefs:
822         a) The RHS is a CAF: a top-level updatable thunk.
823         b) The RHS refers to something that MayHaveCafRefs
824
825 Possible improvement: In an effort to keep the number of CAFs (and 
826 hence the size of the SRTs) down, we could also look at the expression and 
827 decide whether it requires a small bounded amount of heap, so we can ignore 
828 it as a CAF.  In these cases however, we would need to use an additional
829 CAF list to keep track of non-collectable CAFs.  
830
831 \begin{code}
832 hasCafRefs  :: PackageId -> VarEnv Var -> Arity -> CoreExpr -> CafInfo
833 hasCafRefs this_pkg p arity expr 
834   | is_caf || mentions_cafs 
835                             = MayHaveCafRefs
836   | otherwise               = NoCafRefs
837  where
838   mentions_cafs = isFastTrue (cafRefs p expr)
839   is_caf = not (arity > 0 || rhsIsStatic this_pkg expr)
840
841   -- NB. we pass in the arity of the expression, which is expected
842   -- to be calculated by exprArity.  This is because exprArity
843   -- knows how much eta expansion is going to be done by 
844   -- CorePrep later on, and we don't want to duplicate that
845   -- knowledge in rhsIsStatic below.
846
847 cafRefs :: VarEnv Id -> Expr a -> FastBool
848 cafRefs p (Var id)
849         -- imported Ids first:
850   | not (isLocalId id) = fastBool (mayHaveCafRefs (idCafInfo id))
851         -- now Ids local to this module:
852   | otherwise =
853      case lookupVarEnv p id of
854         Just id' -> fastBool (mayHaveCafRefs (idCafInfo id'))
855         Nothing  -> fastBool False
856
857 cafRefs _ (Lit _)              = fastBool False
858 cafRefs p (App f a)            = fastOr (cafRefs p f) (cafRefs p) a
859 cafRefs p (Lam _ e)            = cafRefs p e
860 cafRefs p (Let b e)            = fastOr (cafRefss p (rhssOfBind b)) (cafRefs p) e
861 cafRefs p (Case e _bndr _ alts) = fastOr (cafRefs p e) (cafRefss p) (rhssOfAlts alts)
862 cafRefs p (Note _n e)          = cafRefs p e
863 cafRefs p (Cast e _co)         = cafRefs p e
864 cafRefs _ (Type _)             = fastBool False
865
866 cafRefss :: VarEnv Id -> [Expr a] -> FastBool
867 cafRefss _ []     = fastBool False
868 cafRefss p (e:es) = fastOr (cafRefs p e) (cafRefss p) es
869
870 fastOr :: FastBool -> (a -> FastBool) -> a -> FastBool
871 -- hack for lazy-or over FastBool.
872 fastOr a f x = fastBool (isFastTrue a || isFastTrue (f x))
873 \end{code}