2 % (c) The GRASP Project, Glasgow University, 1992-1994
4 \section[Rename]{Renaming and dependency analysis passes}
7 #include "HsVersions.h"
13 Module, Bag, InPat, ProtoNamePat(..), RenamedPat(..), Name,
14 ProtoName, SplitUniqSupply, PreludeNameFun(..),
15 PreludeNameFuns(..), Maybe, Error(..), Pretty(..), PprStyle,
16 PrettyRep, GlobalNameFuns(..), GlobalNameFun(..),
21 import Bag ( isEmptyBag, unionBags, Bag )
22 import CmdLineOpts ( GlobalSwitch(..) )
28 import RenameAuxFuns ( PreludeNameFuns(..), GlobalNameFuns(..) )
29 --import Pretty -- ToDo: rm debugging
30 import SplitUniq ( splitUniqSupply, SplitUniqSupply )
34 Here's what the renamer does, basically:
37 Flattens out the declarations from the interfaces which this module
38 imports. The result is a new module with no imports, but with more
39 declarations. (Obviously, the imported declarations have ``funny
40 names'' [@ProtoNames@] to indicate their origin.) Handles selective
41 import, renaming, \& such.
43 %--------------------------------------------------------------------
45 Removes duplicate declarations. Duplicates can arise when two
46 imported interface have a signature (or whatever) for the same
47 thing. We check that the two are consistent and then drop one.
48 Considerable huff and puff to pick the one with the ``better''
49 pragmatic information.
51 %--------------------------------------------------------------------
53 Find all the top-level-ish (i.e., global) entities, assign them
54 @Uniques@, and make a \tr{ProtoName -> Name} mapping for them,
57 %--------------------------------------------------------------------
59 Actually prepare the ``renamed'' module. In sticking @Names@ on
60 everything, it will catch out-of-scope errors (and a couple of similar
61 type-variable-use errors). We also our initial dependency analysis of
62 the program (required before typechecking).
66 renameModule :: (GlobalSwitch -> Bool) -- to check cmd-line opts
67 -> PreludeNameFuns -- lookup funs for deeply wired-in names
68 -> ProtoNameModule -- input
70 -> (RenamedModule, -- output, after renaming
71 [FAST_STRING], -- Names of the imported modules
72 -- (profiling needs to know this)
73 GlobalNameFuns, -- final name funs; used later
74 -- to rename generated `deriving'
76 Bag Error -- Errors, from passes 1-4
79 -- Very space-leak sensitive
81 renameModule sw_chkr gnfs@(val_pnf, tc_pnf)
82 input@(Module mod_name _ _ _ _ _ _ _ _ _ _ _ _)
85 use_mentioned_vars = sw_chkr UseGetMentionedVars
89 initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input)
91 ) _TO_ ((mod1, imported_module_names), errs1) ->
95 initRn12 mod_name (rnModule2 mod1)
97 ) _TO_ (mod2, errs2) ->
99 -- pprTrace "rename2:" (ppr PprDebug mod2) (
101 BIND (splitUniqSupply uniqs) _TO_ (us1, us2) ->
105 initRn3 (rnModule3 gnfs imported_module_names mod2) us1
107 ) _TO_ (val_space, tc_space, v_gnf, tc_gnf, errs3) ->
110 final_name_funs = (v_gnf, tc_gnf)
112 errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3
113 -- see note below about why we consult errs at this pt
115 if not (isEmptyBag errs_so_far) then -- give up now
116 ( panic "rename", imported_module_names, final_name_funs, errs_so_far )
120 initRn4 sw_chkr final_name_funs (rnModule4 mod2) us2
122 ) _TO_ (mod4, errs4) ->
124 ( mod4, imported_module_names, final_name_funs, errs4 )
133 Why stop if errors in the first three passes: Suppose you're compiling
134 a module with a top-level definition named \tr{scaleFloat}. Sadly,
135 this is also a Prelude class-method name. \tr{rnModule3} will have
136 detected this error, but: it will also have picked (arbitrarily) one
137 of the two definitions for its final ``value'' name-function. If, by
138 chance, it should have picked the class-method... when it comes to pin
139 a Unique on the top-level (bogus) \tr{scaleFloat}, it will ask for the
140 class-method's Unique (!); it doesn't have one, and you will get a
143 Another way to handle this would be for the duplicate detector to
144 clobber duplicates with some ``safe'' value. Then things would be
145 fine in \tr{rnModule4}. Maybe some other time...