2 % (c) The GRASP Project, Glasgow University, 1992-1996
4 \section[Rename]{Renaming and dependency analysis passes}
7 #include "HsVersions.h"
9 module Rename ( renameModule ) where
14 import RdrHsSyn ( ProtoNameHsModule(..) )
15 import RnHsSyn ( RenamedHsModule(..) )
17 import Bag ( isEmptyBag, unionBags )
18 import CmdLineOpts ( opt_UseGetMentionedVars )
19 import ErrUtils ( Error(..) )
20 import Pretty ( Pretty(..){-ToDo:rm?-} )
21 import RnMonad12 ( initRn12 )
22 import RnMonad4 ( initRn4 )
27 import RnUtils ( PreludeNameMappers(..), GlobalNameMappers(..) )
28 import UniqSupply ( splitUniqSupply )
32 Here's what the renamer does, basically:
35 Flattens out the declarations from the interfaces which this module
36 imports. The result is a new module with no imports, but with more
37 declarations. (Obviously, the imported declarations have ``funny
38 names'' [@ProtoNames@] to indicate their origin.) Handles selective
39 import, renaming, \& such.
41 %--------------------------------------------------------------------
43 Removes duplicate declarations. Duplicates can arise when two
44 imported interface have a signature (or whatever) for the same
45 thing. We check that the two are consistent and then drop one.
46 Considerable huff and puff to pick the one with the ``better''
47 pragmatic information.
49 %--------------------------------------------------------------------
51 Find all the top-level-ish (i.e., global) entities, assign them
52 @Uniques@, and make a \tr{ProtoName -> Name} mapping for them,
55 %--------------------------------------------------------------------
57 Actually prepare the ``renamed'' module. In sticking @Names@ on
58 everything, it will catch out-of-scope errors (and a couple of similar
59 type-variable-use errors). We also our initial dependency analysis of
60 the program (required before typechecking).
64 renameModule :: PreludeNameMappers -- lookup funs for deeply wired-in names
65 -> ProtoNameHsModule -- input
67 -> (RenamedHsModule, -- output, after renaming
68 Bag FAST_STRING, -- Names of the imported modules
69 -- (profiling needs to know this)
70 GlobalNameMappers, -- final name funs; used later
71 -- to rename generated `deriving'
73 Bag Error -- Errors, from passes 1-4
76 -- Very space-leak sensitive
78 renameModule gnfs@(val_pnf, tc_pnf)
79 input@(HsModule mod_name _ _ _ _ _ _ _ _ _ _ _ _)
82 use_mentioned_vars = opt_UseGetMentionedVars
84 case (initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input))
85 of { ((mod1, imported_module_names), errs1) ->
87 case (initRn12 mod_name (rnModule2 mod1)) of { (mod2, errs2) ->
89 case (splitUniqSupply uniqs) of { (us1, us2) ->
91 case (initRn3 (rnModule3 gnfs imported_module_names mod2) us1)
92 of { (val_space, tc_space, v_gnf, tc_gnf, errs3) ->
95 final_name_funs = (v_gnf, tc_gnf)
97 errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3
98 -- see note below about why we consult errs at this pt
100 if not (isEmptyBag errs_so_far) then -- give up now
101 ( panic "rename", imported_module_names, final_name_funs, errs_so_far )
103 case (initRn4 final_name_funs (rnModule mod2) us2)
104 of { (mod4, errs4) ->
106 ( mod4, imported_module_names, final_name_funs, errs4 ) }
110 Why stop if errors in the first three passes: Suppose you're compiling
111 a module with a top-level definition named \tr{scaleFloat}. Sadly,
112 this is also a Prelude class-method name. \tr{rnModule3} will have
113 detected this error, but: it will also have picked (arbitrarily) one
114 of the two definitions for its final ``value'' name-function. If, by
115 chance, it should have picked the class-method... when it comes to pin
116 a Unique on the top-level (bogus) \tr{scaleFloat}, it will ask for the
117 class-method's Unique (!); it doesn't have one, and you will get a
120 Another way to handle this would be for the duplicate detector to
121 clobber duplicates with some ``safe'' value. Then things would be
122 fine in \tr{rnModule}. Maybe some other time...