2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[RenameMonad3]{The monad used by the third renamer pass}
7 #include "HsVersions.h"
11 initRn3, thenRn3, andRn3, returnRn3, mapRn3, fixRn3,
15 newFullNameM3, newInvisibleNameM3,
18 IE, FullName, ExportFlag, ProtoName, Unique,
20 IF_ATTACK_PRAGMAS(COMMA splitUniqSupply)
23 import AbsSyn -- including, IE, getIEStrings, ...
25 import Maybes ( Maybe(..), assocMaybe )
29 import RenameMonad4 ( GlobalNameFun(..) )
37 %************************************************************************
39 \subsection{Plain @Rename3@ monadery}
41 %************************************************************************
45 = ImExportListInfo -> FAST_STRING{-ModuleName-} -> SplitUniqSupply
48 #ifdef __GLASGOW_HASKELL__
50 {-# INLINE thenRn3 #-}
51 {-# INLINE returnRn3 #-}
54 initRn3 :: Rn3M a -> SplitUniqSupply -> a
56 initRn3 m us = m (emptyFM,emptySet) (panic "initRn3: uninitialised module name") us
58 thenRn3 :: Rn3M a -> (a -> Rn3M b) -> Rn3M b
59 andRn3 :: (a -> a -> a) -> Rn3M a -> Rn3M a -> Rn3M a
61 thenRn3 expr continuation exps mod_name uniqs
62 = case splitUniqSupply uniqs of { (s1, s2) ->
63 case (expr exps mod_name s1) of { res1 ->
64 continuation res1 exps mod_name s2 }}
66 andRn3 combiner m1 m2 exps mod_name uniqs
67 = case splitUniqSupply uniqs of { (s1, s2) ->
68 case (m1 exps mod_name s1) of { res1 ->
69 case (m2 exps mod_name s2) of { res2 ->
70 combiner res1 res2 }}}
72 returnRn3 :: a -> Rn3M a
73 returnRn3 result exps mod_name uniqs = result
75 mapRn3 :: (a -> Rn3M b) -> [a] -> Rn3M [b]
77 mapRn3 f [] = returnRn3 []
79 = f x `thenRn3` \ r ->
80 mapRn3 f xs `thenRn3` \ rs ->
83 fixRn3 :: (a -> Rn3M a) -> Rn3M a
85 fixRn3 m exps mod_name us
88 result = m result exps mod_name us
90 putInfoDownM3 :: FAST_STRING{-ModuleName-} -> [IE] -> Rn3M a -> Rn3M a
92 putInfoDownM3 mod_name exports cont _ _ uniqs
93 = cont (getIEStrings exports) mod_name uniqs
96 %************************************************************************
98 \subsection[RenameMonad3-new-names]{Making new names}
100 %************************************************************************
102 @newFullNameM3@ makes a new user-visible FullName (the usual);
103 @newInvisibleNameM3@ is the odd case. @new_name@ does all the work.
106 newFullNameM3, newInvisibleNameM3
107 :: ProtoName -- input
108 -> SrcLoc -- where it started life
109 -> Bool -- if it is "TyCon"ish (rather than "val"ish)
110 -> Maybe ExportFlag -- Just flag => force the use of that exportness
111 -> Rn3M (Unique, FullName)
113 newFullNameM3 pn src_loc is_tycon_ish frcd_exp exps mod_name uniqs
114 = new_name pn src_loc is_tycon_ish frcd_exp False{-visible-} exps mod_name uniqs
116 newInvisibleNameM3 pn src_loc is_tycon_ish frcd_exp exps mod_name uniqs
117 = new_name pn src_loc is_tycon_ish frcd_exp True{-invisible-} exps mod_name uniqs
121 new_name pn src_loc is_tycon_ish frcd_export_flag want_invisible exps mod_name uniqs
124 uniq = getSUnique uniqs
126 mk_name = if want_invisible then mkPrivateFullName else mkFullName
130 Unk s -> mk_name mod_name s
131 (if fromPrelude mod_name
132 && is_tycon_ish then -- & tycon/clas/datacon => Core
137 (case frcd_export_flag of
139 Nothing -> mk_export_flag True [mod_name] s exps)
142 -- note: the assigning of prelude-ness is most dubious (ToDo)
144 Imp m d informant_mods l
146 (if fromPrelude m then -- as above
148 ExportedByPreludeCore
151 else if m == mod_name then -- pretty dang weird... (ToDo: anything?)
154 OtherModule l informant_mods -- for Other*, we save its occurrence name
156 (case frcd_export_flag of
158 Nothing -> mk_export_flag (m==mod_name) informant_mods l exps)
161 Prel n -> panic "RenameMonad3.new_name: prelude name"
164 In deciding the ``exportness'' of something, there are these cases to
167 \item[No explicit export list:]
168 Everything defined in this module goes out.
170 \item[Matches a non-\tr{M..} item in the export list:]
171 Then it's exported as its @name_pr@ item suggests.
173 \item[Matches a \tr{M..} item in the export list:]
175 (Note: the module \tr{M} may be {\em this} module!) It's exported if
176 we got it from \tr{M}'s interface; {\em most emphatically not} the
177 same thing as ``it originally came from \tr{M}''.
184 mk_export_flag :: Bool -- True <=> originally from the module we're compiling
185 -> [FAST_STRING] -- modules that told us about this thing
186 -> FAST_STRING -- name of the thing we're looking at
188 -> ExportFlag -- result
190 mk_export_flag this_module informant_mods thing (exports_alist, dotdot_modules)
191 | isEmptyFM exports_alist && isEmptySet dotdot_modules
192 = if this_module then ExportAll else NotExported
195 = case (lookupFM exports_alist thing) of
196 Just how_to_export -> how_to_export
197 Nothing -> if (or [ im `elementOf` dotdot_modules | im <- informant_mods ])