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 -- The above warning supression flag is a temporary kludge.
10 -- While working on this module you are encouraged to remove it and fix
11 -- any warnings in the module. See
12 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 -- Re-export the OccName stuff
22 mkInternalName, mkSystemName,
23 mkSystemVarName, mkSysTvName,
24 mkFCallName, mkIPName,
26 mkExternalName, mkWiredInName,
28 nameUnique, setNameUnique,
29 nameOccName, nameModule, nameModule_maybe,
31 hashName, localiseName,
33 nameSrcLoc, nameSrcSpan, pprNameLoc,
35 isSystemName, isInternalName, isExternalName,
36 isTyVarName, isTyConName, isWiredInName, isBuiltInSyntax,
37 wiredInNameTyThing_maybe,
40 -- Class NamedThing and overloaded friends
42 getSrcLoc, getSrcSpan, getOccString
45 #include "HsVersions.h"
47 import {-# SOURCE #-} TypeRep( TyThing )
65 %************************************************************************
67 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
69 %************************************************************************
73 n_sort :: NameSort, -- What sort of name it is
74 n_occ :: !OccName, -- Its occurrence name
75 n_uniq :: Int#, -- UNPACK doesn't work, recursive type
76 n_loc :: !SrcSpan -- Definition site
79 -- NOTE: we make the n_loc field strict to eliminate some potential
80 -- (and real!) space leaks, due to the fact that we don't look at
81 -- the SrcLoc in a Name all that often.
86 | WiredIn Module TyThing BuiltInSyntax
87 -- A variant of External, for wired-in things
89 | Internal -- A user-defined Id or TyVar
90 -- defined in the module being compiled
92 | System -- A system-defined Id or TyVar. Typically the
93 -- OccName is very uninformative (like 's')
95 data BuiltInSyntax = BuiltInSyntax | UserSyntax
96 -- BuiltInSyntax is for things like (:), [], tuples etc,
97 -- which have special syntactic forms. They aren't "in scope"
101 Notes about the NameSorts:
103 1. Initially, top-level Ids (including locally-defined ones) get External names,
104 and all other local Ids get Internal names
106 2. Things with a External name are given C static labels, so they finally
107 appear in the .o file's symbol table. They appear in the symbol table
108 in the form M.n. If originally-local things have this property they
109 must be made @External@ first.
111 3. In the tidy-core phase, a External that is not visible to an importer
112 is changed to Internal, and a Internal that is visible is changed to External
114 4. A System Name differs in the following ways:
115 a) has unique attached when printing dumps
116 b) unifier eliminates sys tyvars in favour of user provs where possible
118 Before anything gets printed in interface files or output code, it's
119 fed through a 'tidy' processor, which zaps the OccNames to have
120 unique names; and converts all sys-locals to user locals
121 If any desugarer sys-locals have survived that far, they get changed to
124 Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])
126 Wired-in thing => The thing (Id, TyCon) is fully known to the compiler,
127 not read from an interface file.
128 E.g. Bool, True, Int, Float, and many others
130 All built-in syntax is for wired-in things.
133 nameUnique :: Name -> Unique
134 nameOccName :: Name -> OccName
135 nameModule :: Name -> Module
136 nameSrcLoc :: Name -> SrcLoc
137 nameSrcSpan :: Name -> SrcSpan
139 nameUnique name = mkUniqueGrimily (I# (n_uniq name))
140 nameOccName name = n_occ name
141 nameSrcLoc name = srcSpanStart (n_loc name)
142 nameSrcSpan name = n_loc name
146 nameIsLocalOrFrom :: Module -> Name -> Bool
147 isInternalName :: Name -> Bool
148 isExternalName :: Name -> Bool
149 isSystemName :: Name -> Bool
150 isWiredInName :: Name -> Bool
152 isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
153 isWiredInName other = False
155 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
156 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
157 wiredInNameTyThing_maybe other = Nothing
159 isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
160 isBuiltInSyntax other = False
162 isExternalName (Name {n_sort = External _}) = True
163 isExternalName (Name {n_sort = WiredIn _ _ _}) = True
164 isExternalName other = False
166 isInternalName name = not (isExternalName name)
168 nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
169 nameModule_maybe (Name { n_sort = External mod}) = Just mod
170 nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
171 nameModule_maybe name = Nothing
173 nameIsLocalOrFrom from name
174 | isExternalName name = from == nameModule name
177 isTyVarName :: Name -> Bool
178 isTyVarName name = isTvOcc (nameOccName name)
180 isTyConName :: Name -> Bool
181 isTyConName name = isTcOcc (nameOccName name)
183 isSystemName (Name {n_sort = System}) = True
184 isSystemName other = False
188 %************************************************************************
190 \subsection{Making names}
192 %************************************************************************
195 mkInternalName :: Unique -> OccName -> SrcSpan -> Name
196 mkInternalName uniq occ loc = Name { n_uniq = getKey# uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
197 -- NB: You might worry that after lots of huffing and
198 -- puffing we might end up with two local names with distinct
199 -- uniques, but the same OccName. Indeed we can, but that's ok
200 -- * the insides of the compiler don't care: they use the Unique
201 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
202 -- uniques if you get confused
203 -- * for interface files we tidyCore first, which puts the uniques
204 -- into the print name (see setNameVisibility below)
206 mkExternalName :: Unique -> Module -> OccName -> SrcSpan -> Name
207 mkExternalName uniq mod occ loc
208 = Name { n_uniq = getKey# uniq, n_sort = External mod,
209 n_occ = occ, n_loc = loc }
211 mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax
213 mkWiredInName mod occ uniq thing built_in
214 = Name { n_uniq = getKey# uniq,
215 n_sort = WiredIn mod thing built_in,
216 n_occ = occ, n_loc = wiredInSrcSpan }
218 mkSystemName :: Unique -> OccName -> Name
219 mkSystemName uniq occ = Name { n_uniq = getKey# uniq, n_sort = System,
220 n_occ = occ, n_loc = noSrcSpan }
222 mkSystemVarName :: Unique -> FastString -> Name
223 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
225 mkSysTvName :: Unique -> FastString -> Name
226 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs)
228 mkFCallName :: Unique -> String -> Name
229 -- The encoded string completely describes the ccall
230 mkFCallName uniq str = Name { n_uniq = getKey# uniq, n_sort = Internal,
231 n_occ = mkVarOcc str, n_loc = noSrcSpan }
233 mkTickBoxOpName :: Unique -> String -> Name
234 mkTickBoxOpName uniq str
235 = Name { n_uniq = getKey# uniq, n_sort = Internal,
236 n_occ = mkVarOcc str, n_loc = noSrcSpan }
238 mkIPName :: Unique -> OccName -> Name
240 = Name { n_uniq = getKey# uniq,
247 -- When we renumber/rename things, we need to be
248 -- able to change a Name's Unique to match the cached
249 -- one in the thing it's the name of. If you know what I mean.
250 setNameUnique :: Name -> Unique -> Name
251 setNameUnique name uniq = name {n_uniq = getKey# uniq}
253 tidyNameOcc :: Name -> OccName -> Name
254 -- We set the OccName of a Name when tidying
255 -- In doing so, we change System --> Internal, so that when we print
256 -- it we don't get the unique by default. It's tidy now!
257 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
258 tidyNameOcc name occ = name { n_occ = occ }
260 localiseName :: Name -> Name
261 localiseName n = n { n_sort = Internal }
265 %************************************************************************
267 \subsection{Predicates and selectors}
269 %************************************************************************
272 hashName :: Name -> Int -- ToDo: should really be Word
273 hashName name = getKey (nameUnique name) + 1
274 -- The +1 avoids keys with lots of zeros in the ls bits, which
275 -- interacts badly with the cheap and cheerful multiplication in
280 %************************************************************************
282 \subsection[Name-instances]{Instance declarations}
284 %************************************************************************
287 cmpName n1 n2 = I# (n_uniq n1) `compare` I# (n_uniq n2)
291 instance Eq Name where
292 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
293 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
295 instance Ord Name where
296 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
297 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
298 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
299 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
300 compare a b = cmpName a b
302 instance Uniquable Name where
303 getUnique = nameUnique
305 instance NamedThing Name where
309 %************************************************************************
313 %************************************************************************
316 instance Binary Name where
318 case getUserData bh of {
319 UserData { ud_symtab_map = symtab_map_ref,
320 ud_symtab_next = symtab_next } -> do
321 symtab_map <- readIORef symtab_map_ref
322 case lookupUFM symtab_map name of
323 Just (off,_) -> put_ bh off
325 off <- readFastMutInt symtab_next
326 writeFastMutInt symtab_next (off+1)
327 writeIORef symtab_map_ref
328 $! addToUFM symtab_map name (off,name)
334 return $! (ud_symtab (getUserData bh) ! i)
337 %************************************************************************
339 \subsection{Pretty printing}
341 %************************************************************************
344 instance Outputable Name where
345 ppr name = pprName name
347 instance OutputableBndr Name where
348 pprBndr _ name = pprName name
350 pprName name@(Name {n_sort = sort, n_uniq = u#, n_occ = occ})
351 = getPprStyle $ \ sty ->
353 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
354 External mod -> pprExternal sty uniq mod occ False UserSyntax
355 System -> pprSystem sty uniq occ
356 Internal -> pprInternal sty uniq occ
357 where uniq = mkUniqueGrimily (I# u#)
359 pprExternal sty uniq mod occ is_wired is_builtin
360 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
361 -- In code style, always qualify
362 -- ToDo: maybe we could print all wired-in things unqualified
363 -- in code style, to reduce symbol table bloat?
364 | debugStyle sty = ppr mod <> dot <> ppr_occ_name occ
365 <> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
366 pprNameSpaceBrief (occNameSpace occ),
368 | BuiltInSyntax <- is_builtin = ppr_occ_name occ
369 -- never qualify builtin syntax
370 | NameQual modname <- qual_name = ppr modname <> dot <> ppr_occ_name occ
371 -- see HscTypes.mkPrintUnqualified and Outputable.QualifyName:
372 | NameNotInScope1 <- qual_name = ppr mod <> dot <> ppr_occ_name occ
373 | NameNotInScope2 <- qual_name = ppr (modulePackageId mod) <> char ':' <>
374 ppr (moduleName mod) <> dot <> ppr_occ_name occ
375 | otherwise = ppr_occ_name occ
376 where qual_name = qualName sty mod occ
378 pprInternal sty uniq occ
379 | codeStyle sty = pprUnique uniq
380 | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ),
382 | dumpStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
383 -- For debug dumps, we're not necessarily dumping
384 -- tidied code, so we need to print the uniques.
385 | otherwise = ppr_occ_name occ -- User style
387 -- Like Internal, except that we only omit the unique in Iface style
388 pprSystem sty uniq occ
389 | codeStyle sty = pprUnique uniq
390 | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
391 <> braces (pprNameSpaceBrief (occNameSpace occ))
392 | otherwise = ppr_occ_name occ <> char '_' <> pprUnique uniq
393 -- If the tidy phase hasn't run, the OccName
394 -- is unlikely to be informative (like 's'),
395 -- so print the unique
397 ppr_occ_name occ = ftext (occNameFS occ)
398 -- Don't use pprOccName; instead, just print the string of the OccName;
399 -- we print the namespace in the debug stuff above
401 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
402 -- cached behind the scenes in the FastString implementation.
403 ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
405 -- Prints (if mod information is available) "Defined at <loc>" or
406 -- "Defined in <mod>" information for a Name.
407 pprNameLoc :: Name -> SDoc
409 | isGoodSrcSpan loc = pprDefnLoc loc
410 | isInternalName name || isSystemName name
411 = ptext SLIT("<no location info>")
412 | otherwise = ptext SLIT("Defined in ") <> ppr (nameModule name)
413 where loc = nameSrcSpan name
416 %************************************************************************
418 \subsection{Overloaded functions related to Names}
420 %************************************************************************
423 class NamedThing a where
424 getOccName :: a -> OccName
427 getOccName n = nameOccName (getName n) -- Default method
431 getSrcLoc :: NamedThing a => a -> SrcLoc
432 getSrcSpan :: NamedThing a => a -> SrcSpan
433 getOccString :: NamedThing a => a -> String
435 getSrcLoc = nameSrcLoc . getName
436 getSrcSpan = nameSrcSpan . getName
437 getOccString = occNameString . getOccName