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 :: FastInt, -- UNPACK doesn't work, recursive type
76 --(note later when changing Int# -> FastInt: is that still true about UNPACK?)
77 n_loc :: !SrcSpan -- Definition site
80 -- NOTE: we make the n_loc field strict to eliminate some potential
81 -- (and real!) space leaks, due to the fact that we don't look at
82 -- the SrcLoc in a Name all that often.
87 | WiredIn Module TyThing BuiltInSyntax
88 -- A variant of External, for wired-in things
90 | Internal -- A user-defined Id or TyVar
91 -- defined in the module being compiled
93 | System -- A system-defined Id or TyVar. Typically the
94 -- OccName is very uninformative (like 's')
96 data BuiltInSyntax = BuiltInSyntax | UserSyntax
97 -- BuiltInSyntax is for things like (:), [], tuples etc,
98 -- which have special syntactic forms. They aren't "in scope"
102 Notes about the NameSorts:
104 1. Initially, top-level Ids (including locally-defined ones) get External names,
105 and all other local Ids get Internal names
107 2. Things with a External name are given C static labels, so they finally
108 appear in the .o file's symbol table. They appear in the symbol table
109 in the form M.n. If originally-local things have this property they
110 must be made @External@ first.
112 3. In the tidy-core phase, a External that is not visible to an importer
113 is changed to Internal, and a Internal that is visible is changed to External
115 4. A System Name differs in the following ways:
116 a) has unique attached when printing dumps
117 b) unifier eliminates sys tyvars in favour of user provs where possible
119 Before anything gets printed in interface files or output code, it's
120 fed through a 'tidy' processor, which zaps the OccNames to have
121 unique names; and converts all sys-locals to user locals
122 If any desugarer sys-locals have survived that far, they get changed to
125 Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])
127 Wired-in thing => The thing (Id, TyCon) is fully known to the compiler,
128 not read from an interface file.
129 E.g. Bool, True, Int, Float, and many others
131 All built-in syntax is for wired-in things.
134 nameUnique :: Name -> Unique
135 nameOccName :: Name -> OccName
136 nameModule :: Name -> Module
137 nameSrcLoc :: Name -> SrcLoc
138 nameSrcSpan :: Name -> SrcSpan
140 nameUnique name = mkUniqueGrimily (iBox (n_uniq name))
141 nameOccName name = n_occ name
142 nameSrcLoc name = srcSpanStart (n_loc name)
143 nameSrcSpan name = n_loc name
147 nameIsLocalOrFrom :: Module -> Name -> Bool
148 isInternalName :: Name -> Bool
149 isExternalName :: Name -> Bool
150 isSystemName :: Name -> Bool
151 isWiredInName :: Name -> Bool
153 isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
154 isWiredInName other = False
156 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
157 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
158 wiredInNameTyThing_maybe other = Nothing
160 isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
161 isBuiltInSyntax other = False
163 isExternalName (Name {n_sort = External _}) = True
164 isExternalName (Name {n_sort = WiredIn _ _ _}) = True
165 isExternalName other = False
167 isInternalName name = not (isExternalName name)
169 nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
170 nameModule_maybe (Name { n_sort = External mod}) = Just mod
171 nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
172 nameModule_maybe name = Nothing
174 nameIsLocalOrFrom from name
175 | isExternalName name = from == nameModule name
178 isTyVarName :: Name -> Bool
179 isTyVarName name = isTvOcc (nameOccName name)
181 isTyConName :: Name -> Bool
182 isTyConName name = isTcOcc (nameOccName name)
184 isSystemName (Name {n_sort = System}) = True
185 isSystemName other = False
189 %************************************************************************
191 \subsection{Making names}
193 %************************************************************************
196 mkInternalName :: Unique -> OccName -> SrcSpan -> Name
197 mkInternalName uniq occ loc = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
198 -- NB: You might worry that after lots of huffing and
199 -- puffing we might end up with two local names with distinct
200 -- uniques, but the same OccName. Indeed we can, but that's ok
201 -- * the insides of the compiler don't care: they use the Unique
202 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
203 -- uniques if you get confused
204 -- * for interface files we tidyCore first, which puts the uniques
205 -- into the print name (see setNameVisibility below)
207 mkExternalName :: Unique -> Module -> OccName -> SrcSpan -> Name
208 mkExternalName uniq mod occ loc
209 = Name { n_uniq = getKeyFastInt uniq, n_sort = External mod,
210 n_occ = occ, n_loc = loc }
212 mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax
214 mkWiredInName mod occ uniq thing built_in
215 = Name { n_uniq = getKeyFastInt uniq,
216 n_sort = WiredIn mod thing built_in,
217 n_occ = occ, n_loc = wiredInSrcSpan }
219 mkSystemName :: Unique -> OccName -> Name
220 mkSystemName uniq occ = Name { n_uniq = getKeyFastInt uniq, n_sort = System,
221 n_occ = occ, n_loc = noSrcSpan }
223 mkSystemVarName :: Unique -> FastString -> Name
224 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
226 mkSysTvName :: Unique -> FastString -> Name
227 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs)
229 mkFCallName :: Unique -> String -> Name
230 -- The encoded string completely describes the ccall
231 mkFCallName uniq str = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal,
232 n_occ = mkVarOcc str, n_loc = noSrcSpan }
234 mkTickBoxOpName :: Unique -> String -> Name
235 mkTickBoxOpName uniq str
236 = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal,
237 n_occ = mkVarOcc str, n_loc = noSrcSpan }
239 mkIPName :: Unique -> OccName -> Name
241 = Name { n_uniq = getKeyFastInt uniq,
248 -- When we renumber/rename things, we need to be
249 -- able to change a Name's Unique to match the cached
250 -- one in the thing it's the name of. If you know what I mean.
251 setNameUnique :: Name -> Unique -> Name
252 setNameUnique name uniq = name {n_uniq = getKeyFastInt uniq}
254 tidyNameOcc :: Name -> OccName -> Name
255 -- We set the OccName of a Name when tidying
256 -- In doing so, we change System --> Internal, so that when we print
257 -- it we don't get the unique by default. It's tidy now!
258 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
259 tidyNameOcc name occ = name { n_occ = occ }
261 localiseName :: Name -> Name
262 localiseName n = n { n_sort = Internal }
266 %************************************************************************
268 \subsection{Predicates and selectors}
270 %************************************************************************
273 hashName :: Name -> Int -- ToDo: should really be Word
274 hashName name = getKey (nameUnique name) + 1
275 -- The +1 avoids keys with lots of zeros in the ls bits, which
276 -- interacts badly with the cheap and cheerful multiplication in
281 %************************************************************************
283 \subsection[Name-instances]{Instance declarations}
285 %************************************************************************
288 cmpName n1 n2 = iBox (n_uniq n1) `compare` iBox (n_uniq n2)
292 instance Eq Name where
293 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
294 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
296 instance Ord Name where
297 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
298 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
299 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
300 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
301 compare a b = cmpName a b
303 instance Uniquable Name where
304 getUnique = nameUnique
306 instance NamedThing Name where
310 %************************************************************************
314 %************************************************************************
317 instance Binary Name where
319 case getUserData bh of {
320 UserData { ud_symtab_map = symtab_map_ref,
321 ud_symtab_next = symtab_next } -> do
322 symtab_map <- readIORef symtab_map_ref
323 case lookupUFM symtab_map name of
324 Just (off,_) -> put_ bh off
326 off <- readFastMutInt symtab_next
327 writeFastMutInt symtab_next (off+1)
328 writeIORef symtab_map_ref
329 $! addToUFM symtab_map name (off,name)
335 return $! (ud_symtab (getUserData bh) ! i)
338 %************************************************************************
340 \subsection{Pretty printing}
342 %************************************************************************
345 instance Outputable Name where
346 ppr name = pprName name
348 instance OutputableBndr Name where
349 pprBndr _ name = pprName name
351 pprName name@(Name {n_sort = sort, n_uniq = u, n_occ = occ})
352 = getPprStyle $ \ sty ->
354 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
355 External mod -> pprExternal sty uniq mod occ False UserSyntax
356 System -> pprSystem sty uniq occ
357 Internal -> pprInternal sty uniq occ
358 where uniq = mkUniqueGrimily (iBox u)
360 pprExternal sty uniq mod occ is_wired is_builtin
361 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
362 -- In code style, always qualify
363 -- ToDo: maybe we could print all wired-in things unqualified
364 -- in code style, to reduce symbol table bloat?
365 | debugStyle sty = ppr mod <> dot <> ppr_occ_name occ
366 <> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
367 pprNameSpaceBrief (occNameSpace occ),
369 | BuiltInSyntax <- is_builtin = ppr_occ_name occ
370 -- never qualify builtin syntax
371 | NameQual modname <- qual_name = ppr modname <> dot <> ppr_occ_name occ
372 -- see HscTypes.mkPrintUnqualified and Outputable.QualifyName:
373 | NameNotInScope1 <- qual_name = ppr mod <> dot <> ppr_occ_name occ
374 | NameNotInScope2 <- qual_name = ppr (modulePackageId mod) <> char ':' <>
375 ppr (moduleName mod) <> dot <> ppr_occ_name occ
376 | otherwise = ppr_occ_name occ
377 where qual_name = qualName sty mod occ
379 pprInternal sty uniq occ
380 | codeStyle sty = pprUnique uniq
381 | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ),
383 | dumpStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
384 -- For debug dumps, we're not necessarily dumping
385 -- tidied code, so we need to print the uniques.
386 | otherwise = ppr_occ_name occ -- User style
388 -- Like Internal, except that we only omit the unique in Iface style
389 pprSystem sty uniq occ
390 | codeStyle sty = pprUnique uniq
391 | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
392 <> braces (pprNameSpaceBrief (occNameSpace occ))
393 | otherwise = ppr_occ_name occ <> char '_' <> pprUnique uniq
394 -- If the tidy phase hasn't run, the OccName
395 -- is unlikely to be informative (like 's'),
396 -- so print the unique
398 ppr_occ_name occ = ftext (occNameFS occ)
399 -- Don't use pprOccName; instead, just print the string of the OccName;
400 -- we print the namespace in the debug stuff above
402 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
403 -- cached behind the scenes in the FastString implementation.
404 ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
406 -- Prints (if mod information is available) "Defined at <loc>" or
407 -- "Defined in <mod>" information for a Name.
408 pprNameLoc :: Name -> SDoc
410 | isGoodSrcSpan loc = pprDefnLoc loc
411 | isInternalName name || isSystemName name
412 = ptext SLIT("<no location info>")
413 | otherwise = ptext SLIT("Defined in ") <> ppr (nameModule name)
414 where loc = nameSrcSpan name
417 %************************************************************************
419 \subsection{Overloaded functions related to Names}
421 %************************************************************************
424 class NamedThing a where
425 getOccName :: a -> OccName
428 getOccName n = nameOccName (getName n) -- Default method
432 getSrcLoc :: NamedThing a => a -> SrcLoc
433 getSrcSpan :: NamedThing a => a -> SrcSpan
434 getOccString :: NamedThing a => a -> String
436 getSrcLoc = nameSrcLoc . getName
437 getSrcSpan = nameSrcSpan . getName
438 getOccString = occNameString . getOccName