2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
9 -- Re-export the OccName stuff
15 mkInternalName, mkSystemName,
16 mkSystemVarName, mkSysTvName,
17 mkFCallName, mkIPName,
19 mkExternalName, mkWiredInName,
21 nameUnique, setNameUnique,
22 nameOccName, nameModule, nameModule_maybe,
24 hashName, localiseName,
26 nameSrcLoc, nameSrcSpan,
28 isSystemName, isInternalName, isExternalName,
29 isTyVarName, isTyConName, isWiredInName, isBuiltInSyntax,
30 wiredInNameTyThing_maybe,
33 -- Class NamedThing and overloaded friends
35 getSrcLoc, getSrcSpan, getOccString
38 #include "HsVersions.h"
40 import {-# SOURCE #-} TypeRep( TyThing )
58 %************************************************************************
60 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
62 %************************************************************************
66 n_sort :: NameSort, -- What sort of name it is
67 n_occ :: !OccName, -- Its occurrence name
68 n_uniq :: Int#, -- UNPACK doesn't work, recursive type
69 n_loc :: !SrcSpan -- Definition site
72 -- NOTE: we make the n_loc field strict to eliminate some potential
73 -- (and real!) space leaks, due to the fact that we don't look at
74 -- the SrcLoc in a Name all that often.
79 | WiredIn Module TyThing BuiltInSyntax
80 -- A variant of External, for wired-in things
82 | Internal -- A user-defined Id or TyVar
83 -- defined in the module being compiled
85 | System -- A system-defined Id or TyVar. Typically the
86 -- OccName is very uninformative (like 's')
88 data BuiltInSyntax = BuiltInSyntax | UserSyntax
89 -- BuiltInSyntax is for things like (:), [], tuples etc,
90 -- which have special syntactic forms. They aren't "in scope"
94 Notes about the NameSorts:
96 1. Initially, top-level Ids (including locally-defined ones) get External names,
97 and all other local Ids get Internal names
99 2. Things with a External name are given C static labels, so they finally
100 appear in the .o file's symbol table. They appear in the symbol table
101 in the form M.n. If originally-local things have this property they
102 must be made @External@ first.
104 3. In the tidy-core phase, a External that is not visible to an importer
105 is changed to Internal, and a Internal that is visible is changed to External
107 4. A System Name differs in the following ways:
108 a) has unique attached when printing dumps
109 b) unifier eliminates sys tyvars in favour of user provs where possible
111 Before anything gets printed in interface files or output code, it's
112 fed through a 'tidy' processor, which zaps the OccNames to have
113 unique names; and converts all sys-locals to user locals
114 If any desugarer sys-locals have survived that far, they get changed to
117 Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])
119 Wired-in thing => The thing (Id, TyCon) is fully known to the compiler,
120 not read from an interface file.
121 E.g. Bool, True, Int, Float, and many others
123 All built-in syntax is for wired-in things.
126 nameUnique :: Name -> Unique
127 nameOccName :: Name -> OccName
128 nameModule :: Name -> Module
129 nameSrcLoc :: Name -> SrcLoc
130 nameSrcSpan :: Name -> SrcSpan
132 nameUnique name = mkUniqueGrimily (I# (n_uniq name))
133 nameOccName name = n_occ name
134 nameSrcLoc name = srcSpanStart (n_loc name)
135 nameSrcSpan name = n_loc name
139 nameIsLocalOrFrom :: Module -> Name -> Bool
140 isInternalName :: Name -> Bool
141 isExternalName :: Name -> Bool
142 isSystemName :: Name -> Bool
143 isWiredInName :: Name -> Bool
145 isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
146 isWiredInName other = False
148 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
149 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
150 wiredInNameTyThing_maybe other = Nothing
152 isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
153 isBuiltInSyntax other = False
155 isExternalName (Name {n_sort = External _}) = True
156 isExternalName (Name {n_sort = WiredIn _ _ _}) = True
157 isExternalName other = False
159 isInternalName name = not (isExternalName name)
161 nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
162 nameModule_maybe (Name { n_sort = External mod}) = Just mod
163 nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
164 nameModule_maybe name = Nothing
166 nameIsLocalOrFrom from name
167 | isExternalName name = from == nameModule name
170 isTyVarName :: Name -> Bool
171 isTyVarName name = isTvOcc (nameOccName name)
173 isTyConName :: Name -> Bool
174 isTyConName name = isTcOcc (nameOccName name)
176 isSystemName (Name {n_sort = System}) = True
177 isSystemName other = False
181 %************************************************************************
183 \subsection{Making names}
185 %************************************************************************
188 mkInternalName :: Unique -> OccName -> SrcSpan -> Name
189 mkInternalName uniq occ loc = Name { n_uniq = getKey# uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
190 -- NB: You might worry that after lots of huffing and
191 -- puffing we might end up with two local names with distinct
192 -- uniques, but the same OccName. Indeed we can, but that's ok
193 -- * the insides of the compiler don't care: they use the Unique
194 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
195 -- uniques if you get confused
196 -- * for interface files we tidyCore first, which puts the uniques
197 -- into the print name (see setNameVisibility below)
199 mkExternalName :: Unique -> Module -> OccName -> SrcSpan -> Name
200 mkExternalName uniq mod occ loc
201 = Name { n_uniq = getKey# uniq, n_sort = External mod,
202 n_occ = occ, n_loc = loc }
204 mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax
206 mkWiredInName mod occ uniq thing built_in
207 = Name { n_uniq = getKey# uniq,
208 n_sort = WiredIn mod thing built_in,
209 n_occ = occ, n_loc = wiredInSrcSpan }
211 mkSystemName :: Unique -> OccName -> Name
212 mkSystemName uniq occ = Name { n_uniq = getKey# uniq, n_sort = System,
213 n_occ = occ, n_loc = noSrcSpan }
215 mkSystemVarName :: Unique -> FastString -> Name
216 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
218 mkSysTvName :: Unique -> FastString -> Name
219 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs)
221 mkFCallName :: Unique -> String -> Name
222 -- The encoded string completely describes the ccall
223 mkFCallName uniq str = Name { n_uniq = getKey# uniq, n_sort = Internal,
224 n_occ = mkVarOcc str, n_loc = noSrcSpan }
226 mkTickBoxOpName :: Unique -> String -> Name
227 mkTickBoxOpName uniq str
228 = Name { n_uniq = getKey# uniq, n_sort = Internal,
229 n_occ = mkVarOcc str, n_loc = noSrcSpan }
231 mkIPName :: Unique -> OccName -> Name
233 = Name { n_uniq = getKey# uniq,
240 -- When we renumber/rename things, we need to be
241 -- able to change a Name's Unique to match the cached
242 -- one in the thing it's the name of. If you know what I mean.
243 setNameUnique :: Name -> Unique -> Name
244 setNameUnique name uniq = name {n_uniq = getKey# uniq}
246 tidyNameOcc :: Name -> OccName -> Name
247 -- We set the OccName of a Name when tidying
248 -- In doing so, we change System --> Internal, so that when we print
249 -- it we don't get the unique by default. It's tidy now!
250 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
251 tidyNameOcc name occ = name { n_occ = occ }
253 localiseName :: Name -> Name
254 localiseName n = n { n_sort = Internal }
258 %************************************************************************
260 \subsection{Predicates and selectors}
262 %************************************************************************
265 hashName :: Name -> Int -- ToDo: should really be Word
266 hashName name = getKey (nameUnique name) + 1
267 -- The +1 avoids keys with lots of zeros in the ls bits, which
268 -- interacts badly with the cheap and cheerful multiplication in
273 %************************************************************************
275 \subsection[Name-instances]{Instance declarations}
277 %************************************************************************
280 cmpName n1 n2 = I# (n_uniq n1) `compare` I# (n_uniq n2)
284 instance Eq Name where
285 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
286 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
288 instance Ord Name where
289 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
290 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
291 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
292 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
293 compare a b = cmpName a b
295 instance Uniquable Name where
296 getUnique = nameUnique
298 instance NamedThing Name where
302 %************************************************************************
306 %************************************************************************
309 instance Binary Name where
311 case getUserData bh of {
312 UserData { ud_symtab_map = symtab_map_ref,
313 ud_symtab_next = symtab_next } -> do
314 symtab_map <- readIORef symtab_map_ref
315 case lookupUFM symtab_map name of
316 Just (off,_) -> put_ bh off
318 off <- readFastMutInt symtab_next
319 writeFastMutInt symtab_next (off+1)
320 writeIORef symtab_map_ref
321 $! addToUFM symtab_map name (off,name)
327 return $! (ud_symtab (getUserData bh) ! i)
330 %************************************************************************
332 \subsection{Pretty printing}
334 %************************************************************************
337 instance Outputable Name where
338 ppr name = pprName name
340 instance OutputableBndr Name where
341 pprBndr _ name = pprName name
343 pprName name@(Name {n_sort = sort, n_uniq = u#, n_occ = occ})
344 = getPprStyle $ \ sty ->
346 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
347 External mod -> pprExternal sty uniq mod occ False UserSyntax
348 System -> pprSystem sty uniq occ
349 Internal -> pprInternal sty uniq occ
350 where uniq = mkUniqueGrimily (I# u#)
352 pprExternal sty uniq mod occ is_wired is_builtin
353 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
354 -- In code style, always qualify
355 -- ToDo: maybe we could print all wired-in things unqualified
356 -- in code style, to reduce symbol table bloat?
357 | debugStyle sty = ppr mod <> dot <> ppr_occ_name occ
358 <> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
359 pprNameSpaceBrief (occNameSpace occ),
361 | BuiltInSyntax <- is_builtin = ppr_occ_name occ
362 -- never qualify builtin syntax
363 | Just mod <- qualName sty mod occ = ppr mod <> dot <> ppr_occ_name occ
364 -- the PrintUnqualified tells us how to qualify this Name, if at all
365 | otherwise = ppr_occ_name occ
367 pprInternal sty uniq occ
368 | codeStyle sty = pprUnique uniq
369 | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ),
371 | dumpStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
372 -- For debug dumps, we're not necessarily dumping
373 -- tidied code, so we need to print the uniques.
374 | otherwise = ppr_occ_name occ -- User style
376 -- Like Internal, except that we only omit the unique in Iface style
377 pprSystem sty uniq occ
378 | codeStyle sty = pprUnique uniq
379 | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
380 <> braces (pprNameSpaceBrief (occNameSpace occ))
381 | otherwise = ppr_occ_name occ <> char '_' <> pprUnique uniq
382 -- If the tidy phase hasn't run, the OccName
383 -- is unlikely to be informative (like 's'),
384 -- so print the unique
386 ppr_occ_name occ = ftext (occNameFS occ)
387 -- Don't use pprOccName; instead, just print the string of the OccName;
388 -- we print the namespace in the debug stuff above
390 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
391 -- cached behind the scenes in the FastString implementation.
392 ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
395 %************************************************************************
397 \subsection{Overloaded functions related to Names}
399 %************************************************************************
402 class NamedThing a where
403 getOccName :: a -> OccName
406 getOccName n = nameOccName (getName n) -- Default method
410 getSrcLoc :: NamedThing a => a -> SrcLoc
411 getSrcSpan :: NamedThing a => a -> SrcSpan
412 getOccString :: NamedThing a => a -> String
414 getSrcLoc = nameSrcLoc . getName
415 getSrcSpan = nameSrcSpan . getName
416 getOccString = occNameString . getOccName