[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1994
3 %
4 \section[Rename]{Renaming and dependency analysis passes}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module Rename (
10         renameModule,
11
12         -- for completeness
13         Module, Bag, InPat, ProtoNamePat(..), RenamedPat(..), Name,
14         ProtoName, SplitUniqSupply, PreludeNameFun(..),
15         PreludeNameFuns(..), Maybe, Error(..), Pretty(..), PprStyle,
16         PrettyRep, GlobalNameFuns(..), GlobalNameFun(..),
17         GlobalSwitch
18     ) where
19
20 import AbsSyn
21 import Bag              ( isEmptyBag, unionBags, Bag )
22 import CmdLineOpts      ( GlobalSwitch(..) )
23 import RenameMonad12
24 import Rename1
25 import Rename2
26 import Rename3
27 import Rename4
28 import RenameAuxFuns    ( PreludeNameFuns(..), GlobalNameFuns(..) )
29 --import Pretty         -- ToDo: rm debugging
30 import SplitUniq        ( splitUniqSupply, SplitUniqSupply )
31 import Util
32 \end{code}
33
34 Here's what the renamer does, basically:
35 \begin{description}
36 \item[@Rename1@:]
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.
42
43 %--------------------------------------------------------------------
44 \item[@Rename2@:]
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.
50
51 %--------------------------------------------------------------------
52 \item[@Rename3@:]
53 Find all the top-level-ish (i.e., global) entities, assign them
54 @Uniques@, and make a \tr{ProtoName -> Name} mapping for them,
55 in preparation for...
56
57 %--------------------------------------------------------------------
58 \item[@Rename4@:]
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).
63 \end{description}
64
65 \begin{code}
66 renameModule :: (GlobalSwitch -> Bool)  -- to check cmd-line opts
67              -> PreludeNameFuns         -- lookup funs for deeply wired-in names
68              -> ProtoNameModule         -- input
69              -> SplitUniqSupply
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'
75                                         -- bindings.
76                  Bag Error              -- Errors, from passes 1-4
77                 )
78
79 -- Very space-leak sensitive
80
81 renameModule sw_chkr gnfs@(val_pnf, tc_pnf)
82              input@(Module mod_name _ _ _ _ _ _ _ _ _ _ _ _)
83              uniqs
84   = let
85         use_mentioned_vars = sw_chkr UseGetMentionedVars
86     in
87     BIND (
88     BSCC("Rename1")
89     initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input)
90     ESCC
91     )           _TO_ ((mod1, imported_module_names), errs1) ->
92
93     BIND (
94     BSCC("Rename2")
95     initRn12 mod_name (rnModule2 mod1)
96     ESCC
97     )           _TO_ (mod2, errs2) ->
98
99 --  pprTrace "rename2:" (ppr PprDebug mod2) (
100
101     BIND (splitUniqSupply uniqs) _TO_ (us1, us2) ->
102
103     BIND (
104     BSCC("Rename3")
105     initRn3 (rnModule3 gnfs imported_module_names mod2) us1
106     ESCC
107     )           _TO_ (val_space, tc_space, v_gnf, tc_gnf, errs3) ->
108
109     let
110         final_name_funs = (v_gnf, tc_gnf)
111
112         errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3
113                 -- see note below about why we consult errs at this pt
114     in
115     if not (isEmptyBag errs_so_far) then -- give up now
116         ( panic "rename", imported_module_names, final_name_funs, errs_so_far )
117     else
118         BIND (
119         BSCC("Rename4")
120         initRn4 sw_chkr final_name_funs (rnModule4 mod2) us2
121         ESCC
122         )               _TO_ (mod4, errs4) ->
123
124         ( mod4, imported_module_names, final_name_funs, errs4 )
125         BEND
126     BEND
127 --  )
128     BEND
129     BEND
130     BEND
131 \end{code}
132
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
141 panic.
142
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...