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,
28 isSystemName, isInternalName, isExternalName,
29 isTyVarName, isTyConName, isWiredInName, isBuiltInSyntax,
30 wiredInNameTyThing_maybe,
33 -- Class NamedThing and overloaded friends
35 getSrcLoc, 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 :: !SrcLoc -- 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
131 nameUnique name = mkUniqueGrimily (I# (n_uniq name))
132 nameOccName name = n_occ name
133 nameSrcLoc name = n_loc name
137 nameIsLocalOrFrom :: Module -> Name -> Bool
138 isInternalName :: Name -> Bool
139 isExternalName :: Name -> Bool
140 isSystemName :: Name -> Bool
141 isWiredInName :: Name -> Bool
143 isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
144 isWiredInName other = False
146 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
147 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
148 wiredInNameTyThing_maybe other = Nothing
150 isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
151 isBuiltInSyntax other = False
153 isExternalName (Name {n_sort = External _}) = True
154 isExternalName (Name {n_sort = WiredIn _ _ _}) = True
155 isExternalName other = False
157 isInternalName name = not (isExternalName name)
159 nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
160 nameModule_maybe (Name { n_sort = External mod}) = Just mod
161 nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
162 nameModule_maybe name = Nothing
164 nameIsLocalOrFrom from name
165 | isExternalName name = from == nameModule name
168 isTyVarName :: Name -> Bool
169 isTyVarName name = isTvOcc (nameOccName name)
171 isTyConName :: Name -> Bool
172 isTyConName name = isTcOcc (nameOccName name)
174 isSystemName (Name {n_sort = System}) = True
175 isSystemName other = False
179 %************************************************************************
181 \subsection{Making names}
183 %************************************************************************
186 mkInternalName :: Unique -> OccName -> SrcLoc -> Name
187 mkInternalName uniq occ loc = Name { n_uniq = getKey# uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
188 -- NB: You might worry that after lots of huffing and
189 -- puffing we might end up with two local names with distinct
190 -- uniques, but the same OccName. Indeed we can, but that's ok
191 -- * the insides of the compiler don't care: they use the Unique
192 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
193 -- uniques if you get confused
194 -- * for interface files we tidyCore first, which puts the uniques
195 -- into the print name (see setNameVisibility below)
197 mkExternalName :: Unique -> Module -> OccName -> SrcLoc -> Name
198 mkExternalName uniq mod occ loc
199 = Name { n_uniq = getKey# uniq, n_sort = External mod,
200 n_occ = occ, n_loc = loc }
202 mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax
204 mkWiredInName mod occ uniq thing built_in
205 = Name { n_uniq = getKey# uniq,
206 n_sort = WiredIn mod thing built_in,
207 n_occ = occ, n_loc = wiredInSrcLoc }
209 mkSystemName :: Unique -> OccName -> Name
210 mkSystemName uniq occ = Name { n_uniq = getKey# uniq, n_sort = System,
211 n_occ = occ, n_loc = noSrcLoc }
213 mkSystemVarName :: Unique -> FastString -> Name
214 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
216 mkSysTvName :: Unique -> FastString -> Name
217 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs)
219 mkFCallName :: Unique -> String -> Name
220 -- The encoded string completely describes the ccall
221 mkFCallName uniq str = Name { n_uniq = getKey# uniq, n_sort = Internal,
222 n_occ = mkVarOcc str, n_loc = noSrcLoc }
224 mkTickBoxOpName :: Unique -> String -> Name
225 mkTickBoxOpName uniq str
226 = Name { n_uniq = getKey# uniq, n_sort = Internal,
227 n_occ = mkVarOcc str, n_loc = noSrcLoc }
229 mkIPName :: Unique -> OccName -> Name
231 = Name { n_uniq = getKey# uniq,
238 -- When we renumber/rename things, we need to be
239 -- able to change a Name's Unique to match the cached
240 -- one in the thing it's the name of. If you know what I mean.
241 setNameUnique :: Name -> Unique -> Name
242 setNameUnique name uniq = name {n_uniq = getKey# uniq}
244 tidyNameOcc :: Name -> OccName -> Name
245 -- We set the OccName of a Name when tidying
246 -- In doing so, we change System --> Internal, so that when we print
247 -- it we don't get the unique by default. It's tidy now!
248 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
249 tidyNameOcc name occ = name { n_occ = occ }
251 localiseName :: Name -> Name
252 localiseName n = n { n_sort = Internal }
256 %************************************************************************
258 \subsection{Predicates and selectors}
260 %************************************************************************
263 hashName :: Name -> Int -- ToDo: should really be Word
264 hashName name = getKey (nameUnique name) + 1
265 -- The +1 avoids keys with lots of zeros in the ls bits, which
266 -- interacts badly with the cheap and cheerful multiplication in
271 %************************************************************************
273 \subsection[Name-instances]{Instance declarations}
275 %************************************************************************
278 cmpName n1 n2 = I# (n_uniq n1) `compare` I# (n_uniq n2)
282 instance Eq Name where
283 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
284 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
286 instance Ord Name where
287 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
288 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
289 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
290 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
291 compare a b = cmpName a b
293 instance Uniquable Name where
294 getUnique = nameUnique
296 instance NamedThing Name where
300 %************************************************************************
304 %************************************************************************
307 instance Binary Name where
309 case getUserData bh of {
310 UserData { ud_symtab_map = symtab_map_ref,
311 ud_symtab_next = symtab_next } -> do
312 symtab_map <- readIORef symtab_map_ref
313 case lookupUFM symtab_map name of
314 Just (off,_) -> put_ bh off
316 off <- readFastMutInt symtab_next
317 writeFastMutInt symtab_next (off+1)
318 writeIORef symtab_map_ref
319 $! addToUFM symtab_map name (off,name)
325 return $! (ud_symtab (getUserData bh) ! i)
328 %************************************************************************
330 \subsection{Pretty printing}
332 %************************************************************************
335 instance Outputable Name where
336 ppr name = pprName name
338 instance OutputableBndr Name where
339 pprBndr _ name = pprName name
341 pprName name@(Name {n_sort = sort, n_uniq = u#, n_occ = occ})
342 = getPprStyle $ \ sty ->
344 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
345 External mod -> pprExternal sty uniq mod occ False UserSyntax
346 System -> pprSystem sty uniq occ
347 Internal -> pprInternal sty uniq occ
348 where uniq = mkUniqueGrimily (I# u#)
350 pprExternal sty uniq mod occ is_wired is_builtin
351 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
352 -- In code style, always qualify
353 -- ToDo: maybe we could print all wired-in things unqualified
354 -- in code style, to reduce symbol table bloat?
355 | debugStyle sty = ppr mod <> dot <> ppr_occ_name occ
356 <> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
357 pprNameSpaceBrief (occNameSpace occ),
359 | BuiltInSyntax <- is_builtin = ppr_occ_name occ
360 -- never qualify builtin syntax
361 | Just mod <- qualName sty mod occ = ppr mod <> dot <> ppr_occ_name occ
362 -- the PrintUnqualified tells us how to qualify this Name, if at all
363 | otherwise = ppr_occ_name occ
365 pprInternal sty uniq occ
366 | codeStyle sty = pprUnique uniq
367 | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ),
369 | dumpStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
370 -- For debug dumps, we're not necessarily dumping
371 -- tidied code, so we need to print the uniques.
372 | otherwise = ppr_occ_name occ -- User style
374 -- Like Internal, except that we only omit the unique in Iface style
375 pprSystem sty uniq occ
376 | codeStyle sty = pprUnique uniq
377 | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
378 <> braces (pprNameSpaceBrief (occNameSpace occ))
379 | otherwise = ppr_occ_name occ <> char '_' <> pprUnique uniq
380 -- If the tidy phase hasn't run, the OccName
381 -- is unlikely to be informative (like 's'),
382 -- so print the unique
384 ppr_occ_name occ = ftext (occNameFS occ)
385 -- Don't use pprOccName; instead, just print the string of the OccName;
386 -- we print the namespace in the debug stuff above
388 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
389 -- cached behind the scenes in the FastString implementation.
390 ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
393 %************************************************************************
395 \subsection{Overloaded functions related to Names}
397 %************************************************************************
400 class NamedThing a where
401 getOccName :: a -> OccName
404 getOccName n = nameOccName (getName n) -- Default method
408 getSrcLoc :: NamedThing a => a -> SrcLoc
409 getOccString :: NamedThing a => a -> String
411 getSrcLoc = nameSrcLoc . getName
412 getOccString = occNameString . getOccName