[project @ 1996-03-19 08:58:34 by partain]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1996
3 %
4 \section[Rename]{Renaming and dependency analysis passes}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module Rename ( renameModule ) where
10
11 import Ubiq{-uitous-}
12
13 import HsSyn
14 import RdrHsSyn         ( ProtoNameHsModule(..) )
15 import RnHsSyn          ( RenamedHsModule(..) )
16
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 )
23 import RnPass1
24 import RnPass2
25 import RnPass3
26 import RnPass4
27 import RnUtils          ( PreludeNameMappers(..), GlobalNameMappers(..) )
28 import UniqSupply       ( splitUniqSupply )
29 import Util             ( panic )
30 \end{code}
31
32 Here's what the renamer does, basically:
33 \begin{description}
34 \item[@RnPass1@:]
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.
40
41 %--------------------------------------------------------------------
42 \item[@RnPass2@:]
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.
48
49 %--------------------------------------------------------------------
50 \item[@RnPass3@:]
51 Find all the top-level-ish (i.e., global) entities, assign them
52 @Uniques@, and make a \tr{ProtoName -> Name} mapping for them,
53 in preparation for...
54
55 %--------------------------------------------------------------------
56 \item[@RnPass4@:]
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).
61 \end{description}
62
63 \begin{code}
64 renameModule :: PreludeNameMappers      -- lookup funs for deeply wired-in names
65              -> ProtoNameHsModule       -- input
66              -> UniqSupply
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'
72                                         -- bindings.
73                  Bag Error              -- Errors, from passes 1-4
74                 )
75
76 -- Very space-leak sensitive
77
78 renameModule gnfs@(val_pnf, tc_pnf)
79              input@(HsModule mod_name _ _ _ _ _ _ _ _ _ _ _ _)
80              uniqs
81   = let
82         use_mentioned_vars = opt_UseGetMentionedVars
83     in
84     case (initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input))
85       of { ((mod1, imported_module_names), errs1) ->
86
87     case (initRn12 mod_name (rnModule2 mod1)) of { (mod2, errs2) ->
88
89     case (splitUniqSupply uniqs) of { (us1, us2) ->
90
91     case (initRn3 (rnModule3 gnfs imported_module_names mod2) us1)
92       of { (val_space, tc_space, v_gnf, tc_gnf, errs3) ->
93
94     let
95         final_name_funs = (v_gnf, tc_gnf)
96
97         errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3
98                 -- see note below about why we consult errs at this pt
99     in
100     if not (isEmptyBag errs_so_far) then -- give up now
101         ( panic "rename", imported_module_names, final_name_funs, errs_so_far )
102     else
103         case (initRn4 final_name_funs (rnModule mod2) us2)
104           of { (mod4, errs4) ->
105
106         ( mod4, imported_module_names, final_name_funs, errs4 ) }
107     }}}}
108 \end{code}
109
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
118 panic.
119
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...