2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
8 -- Re-export the OccName stuff
13 mkLocalName, mkSysLocalName, mkCCallName,
15 mkDerivedName, mkGlobalName, mkKnownKeyGlobal, mkWiredInName,
17 nameUnique, setNameUnique,
19 nameOccName, nameModule, nameModule_maybe,
20 setNameOcc, nameRdrName, setNameModuleAndLoc,
23 nameSrcLoc, nameIsLocallyDefined, isDllName, nameIsFrom, nameIsLocalOrFrom,
25 isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
30 emptyNameEnv, unitNameEnv, nameEnvElts,
31 extendNameEnv_C, extendNameEnv, foldNameEnv,
32 plusNameEnv, plusNameEnv_C, extendNameEnv, extendNameEnvList,
33 lookupNameEnv, lookupNameEnv_NF, delFromNameEnv, elemNameEnv,
36 -- Class NamedThing and overloaded friends
38 getSrcLoc, getOccString, toRdrName,
42 #include "HsVersions.h"
44 import OccName -- All of it
45 import Module ( Module, moduleName, mkVanillaModule, isHomeModule )
46 import RdrName ( RdrName, mkRdrOrig, mkRdrUnqual, rdrNameOcc, rdrNameModule )
47 import CmdLineOpts ( opt_Static, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
48 import SrcLoc ( builtinSrcLoc, noSrcLoc, SrcLoc )
49 import Unique ( Unique, Uniquable(..), u2i, pprUnique, pprUnique10 )
51 import Maybes ( expectJust )
56 %************************************************************************
58 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
60 %************************************************************************
64 n_sort :: NameSort, -- What sort of name it is
65 n_occ :: OccName, -- Its occurrence name
67 n_loc :: SrcLoc -- Definition site
71 = Global Module -- (a) TyCon, Class, their derived Ids, dfun Id
73 -- (c) Top-level Id in the original source, even if
76 | Local -- A user-defined Id or TyVar
77 -- defined in the module being compiled
79 | System -- A system-defined Id or TyVar. Typically the
80 -- OccName is very uninformative (like 's')
83 Notes about the NameSorts:
85 1. Initially, top-level Ids (including locally-defined ones) get Global names,
86 and all other local Ids get Local names
88 2. Things with a @Global@ name are given C static labels, so they finally
89 appear in the .o file's symbol table. They appear in the symbol table
90 in the form M.n. If originally-local things have this property they
91 must be made @Global@ first.
93 3. In the tidy-core phase, a Global that is not visible to an importer
94 is changed to Local, and a Local that is visible is changed to Global
96 4. A System Name differs in the following ways:
97 a) has unique attached when printing dumps
98 b) unifier eliminates sys tyvars in favour of user provs where possible
100 Before anything gets printed in interface files or output code, it's
101 fed through a 'tidy' processor, which zaps the OccNames to have
102 unique names; and converts all sys-locals to user locals
103 If any desugarer sys-locals have survived that far, they get changed to
107 nameUnique :: Name -> Unique
108 nameOccName :: Name -> OccName
109 nameModule :: Name -> Module
110 nameSrcLoc :: Name -> SrcLoc
112 nameUnique name = n_uniq name
113 nameOccName name = n_occ name
114 nameSrcLoc name = n_loc name
116 nameModule (Name { n_sort = Global mod }) = mod
117 nameModule name = pprPanic "nameModule" (ppr name)
119 nameModule_maybe (Name { n_sort = Global mod }) = Just mod
120 nameModule_maybe name = Nothing
124 nameIsLocallyDefined :: Name -> Bool
125 nameIsFrom :: Module -> Name -> Bool
126 nameIsLocalOrFrom :: Module -> Name -> Bool
127 isLocalName :: Name -> Bool -- Not globals
128 isGlobalName :: Name -> Bool
129 isSystemName :: Name -> Bool
130 isExternallyVisibleName :: Name -> Bool
132 isGlobalName (Name {n_sort = Global _}) = True
133 isGlobalName other = False
135 isLocalName name = not (isGlobalName name)
137 nameIsLocallyDefined name = isLocalName name
139 nameIsLocalOrFrom from (Name {n_sort = Global mod}) = mod == from
140 nameIsLocalOrFrom from other = True
142 nameIsFrom from (Name {n_sort = Global mod}) = mod == from
143 nameIsFrom from other = pprPanic "nameIsFrom" (ppr other)
145 -- Global names are by definition those that are visible
146 -- outside the module, *as seen by the linker*. Externally visible
147 -- does not mean visible at the source level
148 isExternallyVisibleName name = isGlobalName name
150 isSystemName (Name {n_sort = System}) = True
151 isSystemName other = False
155 %************************************************************************
157 \subsection{Making names}
159 %************************************************************************
162 mkLocalName :: Unique -> OccName -> SrcLoc -> Name
163 mkLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ, n_loc = loc }
164 -- NB: You might worry that after lots of huffing and
165 -- puffing we might end up with two local names with distinct
166 -- uniques, but the same OccName. Indeed we can, but that's ok
167 -- * the insides of the compiler don't care: they use the Unique
168 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
169 -- uniques if you get confused
170 -- * for interface files we tidyCore first, which puts the uniques
171 -- into the print name (see setNameVisibility below)
173 mkGlobalName :: Unique -> Module -> OccName -> SrcLoc -> Name
174 mkGlobalName uniq mod occ loc = Name { n_uniq = uniq, n_sort = Global mod,
175 n_occ = occ, n_loc = loc }
178 mkKnownKeyGlobal :: RdrName -> Unique -> Name
179 mkKnownKeyGlobal rdr_name uniq
180 = mkGlobalName uniq (mkVanillaModule (rdrNameModule rdr_name))
181 (rdrNameOcc rdr_name)
184 mkWiredInName :: Module -> OccName -> Unique -> Name
185 mkWiredInName mod occ uniq = mkGlobalName uniq mod occ builtinSrcLoc
187 mkSysLocalName :: Unique -> UserFS -> Name
188 mkSysLocalName uniq fs = Name { n_uniq = uniq, n_sort = System,
189 n_occ = mkVarOcc fs, n_loc = noSrcLoc }
191 mkCCallName :: Unique -> EncodedString -> Name
192 -- The encoded string completely describes the ccall
193 mkCCallName uniq str = Name { n_uniq = uniq, n_sort = Local,
194 n_occ = mkCCallOcc str, n_loc = noSrcLoc }
196 mkIPName :: Unique -> OccName -> Name
198 = Name { n_uniq = uniq,
203 ---------------------------------------------------------------------
204 mkDerivedName :: (OccName -> OccName)
206 -> Unique -- New unique
207 -> Name -- Result is always a value name
209 mkDerivedName f name uniq = name {n_uniq = uniq, n_occ = f (n_occ name)}
213 -- When we renumber/rename things, we need to be
214 -- able to change a Name's Unique to match the cached
215 -- one in the thing it's the name of. If you know what I mean.
216 setNameUnique name uniq = name {n_uniq = uniq}
218 setNameOcc :: Name -> OccName -> Name
219 -- Give the thing a new OccName, *and*
220 -- record that it's no longer a sys-local
221 -- This is used by the tidy-up pass
222 setNameOcc name occ = name {n_occ = occ}
224 setNameModuleAndLoc :: Name -> Module -> SrcLoc -> Name
225 setNameModuleAndLoc name mod loc = name {n_sort = set (n_sort name), n_loc = loc}
227 set (Global _) = Global mod
231 %************************************************************************
233 \subsection{Tidying a name}
235 %************************************************************************
237 tidyTopName is applied to top-level names in the final program
239 For top-level things,
240 it globalises Local names
241 (if all top-level things should be visible)
242 and localises non-exported Global names
243 (if only exported things should be visible)
245 In all cases except an exported global, it gives it a new occurrence name.
247 The "visibility" here concerns whether the .o file's symbol table
248 mentions the thing; if so, it needs a module name in its symbol.
249 The Global things are "visible" and the Local ones are not
251 Why should things be "visible"? Certainly they must be if they
252 are exported. But also:
254 (a) In certain (prelude only) modules we split up the .hc file into
255 lots of separate little files, which are separately compiled by the C
256 compiler. That gives lots of little .o files. The idea is that if
257 you happen to mention one of them you don't necessarily pull them all
258 in. (Pulling in a piece you don't need can be v bad, because it may
259 mention other pieces you don't need either, and so on.)
261 Sadly, splitting up .hc files means that local names (like s234) are
262 now globally visible, which can lead to clashes between two .hc
263 files. So unlocaliseWhatnot goes through making all the local things
264 into global things, essentially by giving them full names so when they
265 are printed they'll have their module name too. Pretty revolting
268 (b) When optimisation is on we want to make all the internal
269 top-level defns externally visible
272 tidyTopName :: Module -> TidyOccEnv -> Bool -> Name -> (TidyOccEnv, Name)
273 tidyTopName mod env is_exported
274 name@(Name { n_occ = occ, n_sort = sort, n_uniq = uniq, n_loc = loc })
276 Global _ | is_exported -> (env, name)
277 | otherwise -> (env, name { n_sort = new_sort })
278 -- Don't change the occurrnce names of globals, because many of them
279 -- are bound by either a class declaration or a data declaration
280 -- or an explicit user export.
282 other | is_exported -> (env', name { n_sort = Global mod, n_occ = occ' })
283 | otherwise -> (env', name { n_sort = new_sort, n_occ = occ' })
285 (env', occ') = tidyOccName env occ
286 new_sort = mkLocalTopSort mod
288 mkTopName :: Unique -> Module -> FAST_STRING -> Name
289 -- Make a top-level name; make it Global if top-level
290 -- things should be externally visible; Local otherwise
291 -- This chap is only used *after* the tidyCore phase
292 -- Notably, it is used during STG lambda lifting
294 -- We have to make sure that the name is globally unique
295 -- and we don't have tidyCore to help us. So we append
296 -- the unique. Hack! Hack!
297 -- (Used only by the STG lambda lifter.)
298 mkTopName uniq mod fs
299 = Name { n_uniq = uniq,
300 n_sort = mkLocalTopSort mod,
301 n_occ = mkVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
304 mkLocalTopSort :: Module -> NameSort
306 | all_toplev_ids_visible = Global mod
309 all_toplev_ids_visible
310 = not opt_OmitInterfacePragmas || -- Pragmas can make them visible
311 opt_EnsureSplittableC -- Splitting requires visiblilty
316 %************************************************************************
318 \subsection{Predicates and selectors}
320 %************************************************************************
323 hashName :: Name -> Int
324 hashName name = iBox (u2i (nameUnique name))
327 nameRdrName :: Name -> RdrName
328 -- Makes a qualified name for top-level (Global) names, whether locally defined or not
329 -- and an unqualified name just for Locals
330 nameRdrName (Name { n_occ = occ, n_sort = Global mod }) = mkRdrOrig (moduleName mod) occ
331 nameRdrName (Name { n_occ = occ }) = mkRdrUnqual occ
333 isDllName :: Name -> Bool
334 -- Does this name refer to something in a different DLL?
335 isDllName nm = not opt_Static &&
336 not (isLocalName nm) && -- isLocalName test needed 'cos
337 not (isHomeModule (nameModule nm)) -- nameModule won't work on local names
341 isTyVarName :: Name -> Bool
342 isTyVarName name = isTvOcc (nameOccName name)
347 %************************************************************************
349 \subsection[Name-instances]{Instance declarations}
351 %************************************************************************
354 cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
358 instance Eq Name where
359 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
360 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
362 instance Ord Name where
363 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
364 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
365 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
366 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
367 compare a b = cmpName a b
369 instance Uniquable Name where
370 getUnique = nameUnique
372 instance NamedThing Name where
377 %************************************************************************
379 \subsection{Name environment}
381 %************************************************************************
384 type NameEnv a = UniqFM a -- Domain is Name
386 emptyNameEnv :: NameEnv a
387 mkNameEnv :: [(Name,a)] -> NameEnv a
388 nameEnvElts :: NameEnv a -> [a]
389 extendNameEnv_C :: (a->a->a) -> NameEnv a -> Name -> a -> NameEnv a
390 extendNameEnv :: NameEnv a -> Name -> a -> NameEnv a
391 plusNameEnv :: NameEnv a -> NameEnv a -> NameEnv a
392 plusNameEnv_C :: (a->a->a) -> NameEnv a -> NameEnv a -> NameEnv a
393 extendNameEnvList:: NameEnv a -> [(Name,a)] -> NameEnv a
394 delFromNameEnv :: NameEnv a -> Name -> NameEnv a
395 elemNameEnv :: Name -> NameEnv a -> Bool
396 unitNameEnv :: Name -> a -> NameEnv a
397 lookupNameEnv :: NameEnv a -> Name -> Maybe a
398 lookupNameEnv_NF :: NameEnv a -> Name -> a
399 mapNameEnv :: (a->b) -> NameEnv a -> NameEnv b
400 foldNameEnv :: (a -> b -> b) -> b -> NameEnv a -> b
402 emptyNameEnv = emptyUFM
403 foldNameEnv = foldUFM
404 mkNameEnv = listToUFM
405 nameEnvElts = eltsUFM
406 extendNameEnv_C = addToUFM_C
407 extendNameEnv = addToUFM
408 plusNameEnv = plusUFM
409 plusNameEnv_C = plusUFM_C
410 extendNameEnvList= addListToUFM
411 delFromNameEnv = delFromUFM
412 elemNameEnv = elemUFM
414 unitNameEnv = unitUFM
416 lookupNameEnv = lookupUFM
417 lookupNameEnv_NF env n = expectJust "lookupNameEnv_NF" (lookupUFM env n)
421 %************************************************************************
423 \subsection{Pretty printing}
425 %************************************************************************
428 instance Outputable Name where
429 -- When printing interfaces, all Locals have been given nice print-names
430 ppr name = pprName name
432 pprName name@(Name {n_sort = sort, n_uniq = uniq, n_occ = occ})
433 = getPprStyle $ \ sty ->
435 Global mod -> pprGlobal sty name uniq mod occ
436 System -> pprSysLocal sty uniq occ
437 Local -> pprLocal sty uniq occ
439 pprLocal sty uniq occ
440 | codeStyle sty = pprUnique uniq
441 | debugStyle sty = pprOccName occ <>
442 text "{-" <> pprUnique10 uniq <> text "-}"
443 | otherwise = pprOccName occ
445 pprGlobal sty name uniq mod occ
446 | codeStyle sty = ppr (moduleName mod) <> char '_' <> pprOccName occ
448 | debugStyle sty = ppr (moduleName mod) <> dot <> pprOccName occ <>
449 text "{-" <> pprUnique10 uniq <> text "-}"
451 | unqualStyle sty name = pprOccName occ
452 | otherwise = ppr (moduleName mod) <> dot <> pprOccName occ
454 pprSysLocal sty uniq occ
455 | codeStyle sty = pprUnique uniq
456 | otherwise = pprOccName occ <> char '_' <> pprUnique uniq
460 %************************************************************************
462 \subsection{Overloaded functions related to Names}
464 %************************************************************************
467 class NamedThing a where
468 getOccName :: a -> OccName
471 getOccName n = nameOccName (getName n) -- Default method
475 getSrcLoc :: NamedThing a => a -> SrcLoc
476 getOccString :: NamedThing a => a -> String
477 toRdrName :: NamedThing a => a -> RdrName
478 isFrom :: NamedThing a => Module -> a -> Bool
479 isLocalOrFrom :: NamedThing a => Module -> a -> Bool
481 getSrcLoc = nameSrcLoc . getName
482 getOccString = occNameString . getOccName
483 toRdrName = nameRdrName . getName
484 isFrom mod x = nameIsFrom mod (getName x)
485 isLocalOrFrom mod x = nameIsLocalOrFrom mod ( getName x)