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, pprNameLoc,
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 :: FastInt, -- UNPACK doesn't work, recursive type
69 --(note later when changing Int# -> FastInt: is that still true about UNPACK?)
70 n_loc :: !SrcSpan -- Definition site
73 -- NOTE: we make the n_loc field strict to eliminate some potential
74 -- (and real!) space leaks, due to the fact that we don't look at
75 -- the SrcLoc in a Name all that often.
80 | WiredIn Module TyThing BuiltInSyntax
81 -- A variant of External, for wired-in things
83 | Internal -- A user-defined Id or TyVar
84 -- defined in the module being compiled
86 | System -- A system-defined Id or TyVar. Typically the
87 -- OccName is very uninformative (like 's')
89 data BuiltInSyntax = BuiltInSyntax | UserSyntax
90 -- BuiltInSyntax is for things like (:), [], tuples etc,
91 -- which have special syntactic forms. They aren't "in scope"
95 Notes about the NameSorts:
97 1. Initially, top-level Ids (including locally-defined ones) get External names,
98 and all other local Ids get Internal names
100 2. Things with a External name are given C static labels, so they finally
101 appear in the .o file's symbol table. They appear in the symbol table
102 in the form M.n. If originally-local things have this property they
103 must be made @External@ first.
105 3. In the tidy-core phase, a External that is not visible to an importer
106 is changed to Internal, and a Internal that is visible is changed to External
108 4. A System Name differs in the following ways:
109 a) has unique attached when printing dumps
110 b) unifier eliminates sys tyvars in favour of user provs where possible
112 Before anything gets printed in interface files or output code, it's
113 fed through a 'tidy' processor, which zaps the OccNames to have
114 unique names; and converts all sys-locals to user locals
115 If any desugarer sys-locals have survived that far, they get changed to
118 Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])
120 Wired-in thing => The thing (Id, TyCon) is fully known to the compiler,
121 not read from an interface file.
122 E.g. Bool, True, Int, Float, and many others
124 All built-in syntax is for wired-in things.
127 nameUnique :: Name -> Unique
128 nameOccName :: Name -> OccName
129 nameModule :: Name -> Module
130 nameSrcLoc :: Name -> SrcLoc
131 nameSrcSpan :: Name -> SrcSpan
133 nameUnique name = mkUniqueGrimily (iBox (n_uniq name))
134 nameOccName name = n_occ name
135 nameSrcLoc name = srcSpanStart (n_loc name)
136 nameSrcSpan name = n_loc name
140 nameIsLocalOrFrom :: Module -> Name -> Bool
141 isInternalName :: Name -> Bool
142 isExternalName :: Name -> Bool
143 isSystemName :: Name -> Bool
144 isWiredInName :: Name -> Bool
146 isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
147 isWiredInName _ = False
149 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
150 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
151 wiredInNameTyThing_maybe _ = Nothing
153 isBuiltInSyntax :: Name -> Bool
154 isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
155 isBuiltInSyntax _ = False
157 isExternalName (Name {n_sort = External _}) = True
158 isExternalName (Name {n_sort = WiredIn _ _ _}) = True
159 isExternalName _ = False
161 isInternalName name = not (isExternalName name)
163 nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
164 nameModule_maybe :: Name -> Maybe Module
165 nameModule_maybe (Name { n_sort = External mod}) = Just mod
166 nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
167 nameModule_maybe _ = Nothing
169 nameIsLocalOrFrom from name
170 | isExternalName name = from == nameModule name
173 isTyVarName :: Name -> Bool
174 isTyVarName name = isTvOcc (nameOccName name)
176 isTyConName :: Name -> Bool
177 isTyConName name = isTcOcc (nameOccName name)
179 isSystemName (Name {n_sort = System}) = True
180 isSystemName _ = False
184 %************************************************************************
186 \subsection{Making names}
188 %************************************************************************
191 mkInternalName :: Unique -> OccName -> SrcSpan -> Name
192 mkInternalName uniq occ loc = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
193 -- NB: You might worry that after lots of huffing and
194 -- puffing we might end up with two local names with distinct
195 -- uniques, but the same OccName. Indeed we can, but that's ok
196 -- * the insides of the compiler don't care: they use the Unique
197 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
198 -- uniques if you get confused
199 -- * for interface files we tidyCore first, which puts the uniques
200 -- into the print name (see setNameVisibility below)
202 mkExternalName :: Unique -> Module -> OccName -> SrcSpan -> Name
203 mkExternalName uniq mod occ loc
204 = Name { n_uniq = getKeyFastInt uniq, n_sort = External mod,
205 n_occ = occ, n_loc = loc }
207 mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax
209 mkWiredInName mod occ uniq thing built_in
210 = Name { n_uniq = getKeyFastInt uniq,
211 n_sort = WiredIn mod thing built_in,
212 n_occ = occ, n_loc = wiredInSrcSpan }
214 mkSystemName :: Unique -> OccName -> Name
215 mkSystemName uniq occ = Name { n_uniq = getKeyFastInt uniq, n_sort = System,
216 n_occ = occ, n_loc = noSrcSpan }
218 mkSystemVarName :: Unique -> FastString -> Name
219 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
221 mkSysTvName :: Unique -> FastString -> Name
222 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs)
224 mkFCallName :: Unique -> String -> Name
225 -- The encoded string completely describes the ccall
226 mkFCallName uniq str = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal,
227 n_occ = mkVarOcc str, n_loc = noSrcSpan }
229 mkTickBoxOpName :: Unique -> String -> Name
230 mkTickBoxOpName uniq str
231 = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal,
232 n_occ = mkVarOcc str, n_loc = noSrcSpan }
234 mkIPName :: Unique -> OccName -> Name
236 = Name { n_uniq = getKeyFastInt uniq,
243 -- When we renumber/rename things, we need to be
244 -- able to change a Name's Unique to match the cached
245 -- one in the thing it's the name of. If you know what I mean.
246 setNameUnique :: Name -> Unique -> Name
247 setNameUnique name uniq = name {n_uniq = getKeyFastInt uniq}
249 tidyNameOcc :: Name -> OccName -> Name
250 -- We set the OccName of a Name when tidying
251 -- In doing so, we change System --> Internal, so that when we print
252 -- it we don't get the unique by default. It's tidy now!
253 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
254 tidyNameOcc name occ = name { n_occ = occ }
256 localiseName :: Name -> Name
257 localiseName n = n { n_sort = Internal }
261 %************************************************************************
263 \subsection{Predicates and selectors}
265 %************************************************************************
268 hashName :: Name -> Int -- ToDo: should really be Word
269 hashName name = getKey (nameUnique name) + 1
270 -- The +1 avoids keys with lots of zeros in the ls bits, which
271 -- interacts badly with the cheap and cheerful multiplication in
276 %************************************************************************
278 \subsection[Name-instances]{Instance declarations}
280 %************************************************************************
283 cmpName :: Name -> Name -> Ordering
284 cmpName n1 n2 = iBox (n_uniq n1) `compare` iBox (n_uniq n2)
288 instance Eq Name where
289 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
290 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
292 instance Ord Name where
293 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
294 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
295 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
296 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
297 compare a b = cmpName a b
299 instance Uniquable Name where
300 getUnique = nameUnique
302 instance NamedThing Name where
306 %************************************************************************
310 %************************************************************************
313 instance Binary Name where
315 case getUserData bh of {
316 UserData { ud_symtab_map = symtab_map_ref,
317 ud_symtab_next = symtab_next } -> do
318 symtab_map <- readIORef symtab_map_ref
319 case lookupUFM symtab_map name of
320 Just (off,_) -> put_ bh off
322 off <- readFastMutInt symtab_next
323 writeFastMutInt symtab_next (off+1)
324 writeIORef symtab_map_ref
325 $! addToUFM symtab_map name (off,name)
331 return $! (ud_symtab (getUserData bh) ! i)
334 %************************************************************************
336 \subsection{Pretty printing}
338 %************************************************************************
341 instance Outputable Name where
342 ppr name = pprName name
344 instance OutputableBndr Name where
345 pprBndr _ name = pprName name
347 pprName :: Name -> SDoc
348 pprName (Name {n_sort = sort, n_uniq = u, n_occ = occ})
349 = getPprStyle $ \ sty ->
351 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
352 External mod -> pprExternal sty uniq mod occ False UserSyntax
353 System -> pprSystem sty uniq occ
354 Internal -> pprInternal sty uniq occ
355 where uniq = mkUniqueGrimily (iBox u)
357 pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
358 pprExternal sty uniq mod occ is_wired is_builtin
359 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
360 -- In code style, always qualify
361 -- ToDo: maybe we could print all wired-in things unqualified
362 -- in code style, to reduce symbol table bloat?
363 | debugStyle sty = ppr mod <> dot <> ppr_occ_name occ
364 <> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
365 pprNameSpaceBrief (occNameSpace occ),
367 | BuiltInSyntax <- is_builtin = ppr_occ_name occ
368 -- never qualify builtin syntax
369 | NameQual modname <- qual_name = ppr modname <> dot <> ppr_occ_name occ
370 -- see HscTypes.mkPrintUnqualified and Outputable.QualifyName:
371 | NameNotInScope1 <- qual_name = ppr mod <> dot <> ppr_occ_name occ
372 | NameNotInScope2 <- qual_name = ppr (modulePackageId mod) <> char ':' <>
373 ppr (moduleName mod) <> dot <> ppr_occ_name occ
374 | otherwise = ppr_occ_name occ
375 where qual_name = qualName sty mod occ
377 pprInternal :: PprStyle -> Unique -> OccName -> SDoc
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 :: PprStyle -> Unique -> OccName -> SDoc
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 :: OccName -> SDoc
399 ppr_occ_name occ = ftext (occNameFS occ)
400 -- Don't use pprOccName; instead, just print the string of the OccName;
401 -- we print the namespace in the debug stuff above
403 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
404 -- cached behind the scenes in the FastString implementation.
405 ppr_z_occ_name :: OccName -> SDoc
406 ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
408 -- Prints (if mod information is available) "Defined at <loc>" or
409 -- "Defined in <mod>" information for a Name.
410 pprNameLoc :: Name -> SDoc
412 | isGoodSrcSpan loc = pprDefnLoc loc
413 | isInternalName name || isSystemName name
414 = ptext SLIT("<no location info>")
415 | otherwise = ptext SLIT("Defined in ") <> ppr (nameModule name)
416 where loc = nameSrcSpan name
419 %************************************************************************
421 \subsection{Overloaded functions related to Names}
423 %************************************************************************
426 class NamedThing a where
427 getOccName :: a -> OccName
430 getOccName n = nameOccName (getName n) -- Default method
434 getSrcLoc :: NamedThing a => a -> SrcLoc
435 getSrcSpan :: NamedThing a => a -> SrcSpan
436 getOccString :: NamedThing a => a -> String
438 getSrcLoc = nameSrcLoc . getName
439 getSrcSpan = nameSrcSpan . getName
440 getOccString = occNameString . getOccName