2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
8 -- Re-export the OccName stuff
13 mkLocalName, mkImportedLocalName, mkSysLocalName, mkCCallName,
15 mkDerivedName, mkGlobalName, mkKnownKeyGlobal,
16 mkWiredInIdName, mkWiredInTyConName,
17 mkUnboundName, isUnboundName,
19 maybeWiredInIdName, maybeWiredInTyConName,
20 isWiredInName, hashName,
22 nameUnique, setNameUnique, setNameProvenance, getNameProvenance, setNameImportReason,
24 nameOccName, nameModule, setNameOcc, nameRdrName, setNameModule,
26 isUserExportedName, isUserImportedName, isUserImportedExplicitlyName, nameSrcLoc,
27 isLocallyDefinedName, isDynName,
29 isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
33 Provenance(..), ImportReason(..), pprProvenance,
34 ExportFlag(..), PrintUnqualified,
35 pprNameProvenance, hasBetterProv,
37 -- Class NamedThing and overloaded friends
39 getSrcLoc, isLocallyDefined, getOccString
42 #include "HsVersions.h"
44 import {-# SOURCE #-} Var ( Id, setIdName )
45 import {-# SOURCE #-} TyCon ( TyCon, setTyConName )
47 import OccName -- All of it
48 import Module ( Module, moduleName, pprModule, mkVanillaModule, isDynamicModule )
49 import RdrName ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc, rdrNameModule )
50 import CmdLineOpts ( opt_PprStyle_NoPrags, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
52 import SrcLoc ( noSrcLoc, mkBuiltinSrcLoc, SrcLoc )
53 import Unique ( pprUnique, Unique, Uniquable(..), unboundKey, u2i )
59 %************************************************************************
61 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
63 %************************************************************************
67 n_sort :: NameSort, -- What sort of name it is
69 n_occ :: OccName, -- Its occurrence name
70 n_prov :: Provenance -- How it was made
77 | WiredInTyCon Module TyCon
80 Things with a @Global@ name are given C static labels, so they finally
81 appear in the .o file's symbol table. They appear in the symbol table
82 in the form M.n. If originally-local things have this property they
83 must be made @Global@ first.
86 mkLocalName :: Unique -> OccName -> SrcLoc -> Name
87 mkLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ,
88 n_prov = LocalDef loc NotExported }
89 -- NB: You might worry that after lots of huffing and
90 -- puffing we might end up with two local names with distinct
91 -- uniques, but the same OccName. Indeed we can, but that's ok
92 -- * the insides of the compiler don't care: they use the Unique
93 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
94 -- uniques if you get confused
95 -- * for interface files we tidyCore first, which puts the uniques
96 -- into the print name (see setNameVisibility below)
98 mkImportedLocalName :: Unique -> OccName -> SrcLoc -> Name
99 -- Just the same as mkLocalName, except the provenance is different
100 -- Reason: this flags the name as one that came in from an interface file.
101 -- This is useful when trying to decide which of two type variables
102 -- should 'win' when unifying them.
103 -- NB: this is only for non-top-level names, so we use ImplicitImport
104 mkImportedLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ,
105 n_prov = NonLocalDef ImplicitImport True }
108 mkGlobalName :: Unique -> Module -> OccName -> Provenance -> Name
109 mkGlobalName uniq mod occ prov = Name { n_uniq = uniq, n_sort = Global mod,
110 n_occ = occ, n_prov = prov }
113 mkKnownKeyGlobal :: (RdrName, Unique) -> Name
114 mkKnownKeyGlobal (rdr_name, uniq)
115 = mkGlobalName uniq (mkVanillaModule (rdrNameModule rdr_name))
116 (rdrNameOcc rdr_name)
119 mkSysLocalName :: Unique -> UserFS -> Name
120 mkSysLocalName uniq fs = Name { n_uniq = uniq, n_sort = Local,
121 n_occ = mkSrcVarOcc fs, n_prov = systemProvenance }
123 mkCCallName :: Unique -> EncodedString -> Name
124 -- The encoded string completely describes the ccall
125 mkCCallName uniq str = Name { n_uniq = uniq, n_sort = Local,
126 n_occ = mkCCallOcc str,
127 n_prov = NonLocalDef ImplicitImport True }
129 mkTopName :: Unique -> Module -> FAST_STRING -> Name
130 -- Make a top-level name; make it Global if top-level
131 -- things should be externally visible; Local otherwise
132 -- This chap is only used *after* the tidyCore phase
133 -- Notably, it is used during STG lambda lifting
135 -- We have to make sure that the name is globally unique
136 -- and we don't have tidyCore to help us. So we append
137 -- the unique. Hack! Hack!
138 mkTopName uniq mod fs
139 = Name { n_uniq = uniq,
140 n_sort = mk_top_sort mod,
141 n_occ = mkSrcVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
142 n_prov = LocalDef noSrcLoc NotExported }
144 mkIPName :: Unique -> OccName -> Name
146 = Name { n_uniq = uniq,
149 -- ZZ is this an appropriate provinence?
150 n_prov = SystemProv }
152 ------------------------- Wired in names -------------------------
154 mkWiredInIdName :: Unique -> Module -> OccName -> Id -> Name
155 mkWiredInIdName uniq mod occ id = Name { n_uniq = uniq, n_sort = WiredInId mod id,
156 n_occ = occ, n_prov = SystemProv }
158 -- mkWiredInTyConName takes a FAST_STRING instead of
159 -- an OccName, which is a bit yukky but that's what the
160 -- clients find easiest.
161 mkWiredInTyConName :: Unique -> Module -> FAST_STRING -> TyCon -> Name
162 mkWiredInTyConName uniq mod fs tycon
163 = Name { n_uniq = uniq, n_sort = WiredInTyCon mod tycon,
164 n_occ = mkSrcOccFS tcName fs, n_prov = SystemProv }
167 ---------------------------------------------------------------------
168 mkDerivedName :: (OccName -> OccName)
170 -> Unique -- New unique
171 -> Name -- Result is always a value name
173 mkDerivedName f name uniq = name {n_uniq = uniq, n_occ = f (n_occ name)}
175 -- mkUnboundName makes a place-holder Name; it shouldn't be looked at except possibly
176 -- during compiler debugging.
177 mkUnboundName :: RdrName -> Name
178 mkUnboundName rdr_name = mkLocalName unboundKey (rdrNameOcc rdr_name) noSrcLoc
180 isUnboundName :: Name -> Bool
181 isUnboundName name = getUnique name == unboundKey
185 -- When we renumber/rename things, we need to be
186 -- able to change a Name's Unique to match the cached
187 -- one in the thing it's the name of. If you know what I mean.
188 setNameUnique name uniq = name {n_uniq = uniq}
190 setNameOcc :: Name -> OccName -> Name
191 -- Give the thing a new OccName, *and*
192 -- record that it's no longer a sys-local
193 -- This is used by the tidy-up pass
194 setNameOcc name occ = name {n_occ = occ}
196 setNameModule :: Name -> Module -> Name
197 setNameModule name mod = name {n_sort = set (n_sort name)}
199 set (Global _) = Global mod
200 set (WiredInId _ id) = WiredInId mod id
201 set (WiredInTyCon _ tycon) = WiredInTyCon mod tycon
205 %************************************************************************
207 \subsection{Setting provenance and visibility
209 %************************************************************************
211 tidyTopName is applied to top-level names in the final program
213 For top-level things, it globalises Local names
214 (if all top-level things should be visible)
215 and localises non-exported Global names
216 (if only exported things should be visible)
218 In all cases except an exported global, it gives it a new occurrence name.
220 The "visibility" here concerns whether the .o file's symbol table
221 mentions the thing; if so, it needs a module name in its symbol.
222 The Global things are "visible" and the Local ones are not
224 Why should things be "visible"? Certainly they must be if they
225 are exported. But also:
227 (a) In certain (prelude only) modules we split up the .hc file into
228 lots of separate little files, which are separately compiled by the C
229 compiler. That gives lots of little .o files. The idea is that if
230 you happen to mention one of them you don't necessarily pull them all
231 in. (Pulling in a piece you don't need can be v bad, because it may
232 mention other pieces you don't need either, and so on.)
234 Sadly, splitting up .hc files means that local names (like s234) are
235 now globally visible, which can lead to clashes between two .hc
236 files. So unlocaliseWhatnot goes through making all the local things
237 into global things, essentially by giving them full names so when they
238 are printed they'll have their module name too. Pretty revolting
241 (b) When optimisation is on we want to make all the internal
242 top-level defns externally visible
245 tidyTopName :: Module -> TidyOccEnv -> Name -> (TidyOccEnv, Name)
246 tidyTopName mod env name
249 (env', occ') = tidyOccName env (n_occ name)
251 name' = Name { n_uniq = n_uniq name, n_sort = mk_top_sort mod,
252 n_occ = occ', n_prov = LocalDef noSrcLoc NotExported }
254 mk_top_sort mod | all_toplev_ids_visible = Global mod
257 all_toplev_ids_visible =
258 not opt_OmitInterfacePragmas || -- Pragmas can make them visible
259 opt_EnsureSplittableC -- Splitting requires visiblilty
264 setNameProvenance :: Name -> Provenance -> Name
265 -- setNameProvenance used to only change the provenance of
266 -- Implicit-provenance things, but that gives bad error messages
267 -- for names defined twice in the same module, so I changed it to
268 -- set the provenance of *any* global (SLPJ Jun 97)
269 setNameProvenance name prov = name {n_prov = prov}
271 getNameProvenance :: Name -> Provenance
272 getNameProvenance name = n_prov name
274 setNameImportReason :: Name -> ImportReason -> Name
275 setNameImportReason name reason
276 = name { n_prov = new_prov }
278 -- It's important that we don't do the pattern matching
279 -- in the top-level clause, else we get a black hole in
280 -- the renamer. Rather a yukky constraint. There's only
281 -- one call, in RnNames
282 old_prov = n_prov name
283 new_prov = case old_prov of
284 NonLocalDef _ omit -> NonLocalDef reason omit
289 %************************************************************************
291 \subsection{Provenance and export info}
293 %************************************************************************
297 = LocalDef -- Defined locally
299 ExportFlag -- Whether it's exported
301 | NonLocalDef -- Defined non-locally
305 | SystemProv -- Either (a) a system-generated local with
306 -- a v short name OccName
307 -- or (b) a known-key global which should have a proper
308 -- provenance attached by the renamer
311 Sys-provs are only used internally. When the compiler generates (say)
312 a fresh desguar variable it always calls it "ds", and of course it gets
313 a fresh unique. But when printing -ddump-xx dumps, we must print it with
314 its unique, because there'll be a lot of "ds" variables.
316 Names with SystemProv differ in the following ways:
317 a) locals have unique attached when printing dumps
318 b) unifier eliminates sys tyvars in favour of user provs where possible
319 c) renamer replaces SystemProv with a better one
321 Before anything gets printed in interface files or output code, it's
322 fed through a 'tidy' processor, which zaps the OccNames to have
323 unique names; and converts all sys-locals to user locals
324 If any desugarer sys-locals have survived that far, they get changed to
329 = UserImport Module SrcLoc Bool -- Imported from module M on line L
330 -- Note the M may well not be the defining module
332 -- The Bool is true iff the thing was named *explicitly* in the import spec,
333 -- rather than being imported as part of a group; e.g.
336 -- Here, everything imported by B, and the constructors of T
337 -- are not named explicitly; only T is named explicitly.
338 -- This info is used when warning of unused names.
340 | ImplicitImport -- Imported implicitly for some other reason
343 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
344 -- in scope in this module, so print it
345 -- unqualified in error messages
347 data ExportFlag = Exported | NotExported
350 Something is "Exported" if it may be mentioned by another module without
351 warning. The crucial thing about Exported things is that they must
352 never be dropped as dead code, even if they aren't used in this module.
353 Furthermore, being Exported means that we can't see all call sites of the thing.
355 Exported things include:
357 - explicitly exported Ids, including data constructors,
358 class method selectors
360 - dfuns from instance decls
362 Being Exported is *not* the same as finally appearing in the .o file's
363 symbol table. For example, a local Id may be mentioned in an Exported
364 Id's unfolding in the interface file, in which case the local Id goes
369 systemProvenance :: Provenance
370 systemProvenance = SystemProv
372 -- pprNameProvenance is used in error messages to say where a name came from
373 pprNameProvenance :: Name -> SDoc
374 pprNameProvenance name = pprProvenance (getNameProvenance name)
376 pprProvenance :: Provenance -> SDoc
377 pprProvenance SystemProv = ptext SLIT("System")
378 pprProvenance (LocalDef loc _) = ptext SLIT("defined at") <+> ppr loc
379 pprProvenance (NonLocalDef ImplicitImport _)
380 = ptext SLIT("implicitly imported")
381 pprProvenance (NonLocalDef (UserImport mod loc _) _)
382 = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
386 %************************************************************************
388 \subsection{Predicates and selectors}
390 %************************************************************************
393 nameUnique :: Name -> Unique
394 nameOccName :: Name -> OccName
395 nameModule :: Name -> Module
396 nameSrcLoc :: Name -> SrcLoc
397 isLocallyDefinedName :: Name -> Bool
398 isUserExportedName :: Name -> Bool
399 isWiredInName :: Name -> Bool
400 isLocalName :: Name -> Bool
401 isGlobalName :: Name -> Bool
402 isExternallyVisibleName :: Name -> Bool
406 hashName :: Name -> Int
407 hashName name = IBOX( u2i (nameUnique name) )
409 nameUnique name = n_uniq name
410 nameOccName name = n_occ name
414 Local -> pprPanic "nameModule" (ppr name)
415 x -> nameSortModule x
417 nameSortModule (Global mod) = mod
418 nameSortModule (WiredInId mod _) = mod
419 nameSortModule (WiredInTyCon mod _) = mod
421 nameRdrName :: Name -> RdrName
422 nameRdrName (Name { n_sort = Local, n_occ = occ }) = mkRdrUnqual occ
423 nameRdrName (Name { n_sort = sort, n_occ = occ }) = mkRdrQual (moduleName (nameSortModule sort)) occ
425 isUserExportedName (Name { n_prov = LocalDef _ Exported }) = True
426 isUserExportedName other = False
428 isUserImportedExplicitlyName (Name { n_prov = NonLocalDef (UserImport _ _ explicit) _ }) = explicit
429 isUserImportedExplicitlyName other = False
431 isUserImportedName (Name { n_prov = NonLocalDef (UserImport _ _ _) _ }) = True
432 isUserImportedName other = False
434 isDynName :: Name -> Bool
435 -- Does this name come from a DLL?
436 isDynName nm = not (isLocallyDefinedName nm) &&
437 isDynamicModule (nameModule nm)
439 nameSrcLoc name = provSrcLoc (n_prov name)
441 provSrcLoc (LocalDef loc _) = loc
442 provSrcLoc (NonLocalDef (UserImport _ loc _) _) = loc
443 provSrcLoc other = noSrcLoc
445 isLocallyDefinedName (Name {n_sort = Local}) = True -- Local (might have SystemProv)
446 isLocallyDefinedName (Name {n_prov = LocalDef _ _}) = True -- Global, but defined here
447 isLocallyDefinedName other = False -- Other
449 -- Things the compiler "knows about" are in some sense
450 -- "imported". When we are compiling the module where
451 -- the entities are defined, we need to be able to pick
452 -- them out, often in combination with isLocallyDefined.
453 isWiredInName (Name {n_sort = WiredInTyCon _ _}) = True
454 isWiredInName (Name {n_sort = WiredInId _ _}) = True
455 isWiredInName _ = False
457 maybeWiredInIdName :: Name -> Maybe Id
458 maybeWiredInIdName (Name {n_sort = WiredInId _ id}) = Just id
459 maybeWiredInIdName other = Nothing
461 maybeWiredInTyConName :: Name -> Maybe TyCon
462 maybeWiredInTyConName (Name {n_sort = WiredInTyCon _ tc}) = Just tc
463 maybeWiredInTyConName other = Nothing
466 isLocalName (Name {n_sort = Local}) = True
467 isLocalName _ = False
469 isGlobalName (Name {n_sort = Local}) = False
470 isGlobalName other = True
472 -- Global names are by definition those that are visible
473 -- outside the module, *as seen by the linker*. Externally visible
474 -- does not mean visible at the source level (that's isExported).
475 isExternallyVisibleName name = isGlobalName name
477 hasBetterProv :: Name -> Name -> Bool
478 hasBetterProv name1 name2
479 = case n_prov name1 of
482 NonLocalDef _ _ -> case n_prov name2 of
483 LocalDef _ _ -> False
486 isSystemName (Name {n_prov = SystemProv}) = True
487 isSystemName other = False
491 %************************************************************************
493 \subsection[Name-instances]{Instance declarations}
495 %************************************************************************
498 cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
502 instance Eq Name where
503 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
504 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
506 instance Ord Name where
507 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
508 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
509 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
510 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
511 compare a b = cmpName a b
513 instance Uniquable Name where
514 getUnique = nameUnique
516 instance NamedThing Name where
521 %************************************************************************
523 \subsection{Pretty printing}
525 %************************************************************************
528 instance Outputable Name where
529 -- When printing interfaces, all Locals have been given nice print-names
530 ppr name = pprName name
532 pprName (Name {n_sort = Local, n_uniq = uniq, n_occ = occ, n_prov = prov})
534 = getPprStyle $ \ sty ->
535 if codeStyle sty then
536 pprUnique uniq -- When printing in code we required all names to
537 -- be globally unique; for example, we use this identifier
538 -- for the closure name. So we just print the unique alone.
540 pprOccName occ <> pp_local_extra sty uniq
542 sys_local = case prov of
546 pp_local_extra sty uniq
547 | sys_local = underscore <> pprUnique uniq -- Must print uniques for sys_locals
548 | debugStyle sty = text "{-" <> pprUnique uniq <> text "-}"
552 pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ, n_prov = prov})
553 -- Globals, and wired in things
554 = getPprStyle $ \ sty ->
555 if codeStyle sty then
556 ppr mod <> underscore <> ppr occ
558 pp_mod_dot sty <> ppr occ <> pp_global_debug sty uniq prov
560 mod = nameSortModule sort
564 SystemProv -> pp_qual mod user_sty
565 -- Hack alert! Omit the qualifier on SystemProv things in user style
566 -- I claim such SystemProv things will also be WiredIn things.
567 -- We can't get the omit flag right
568 -- on wired in tycons etc (sigh) so we just leave it out in user style,
569 -- and hope that leaving it out isn't too consfusing.
570 -- (e.g. if the programmer hides Bool and redefines it. If so, use -dppr-debug.)
572 LocalDef _ _ -> pp_qual mod (user_sty || iface_sty)
574 NonLocalDef (UserImport imp_mod _ _) omit
575 | user_sty -> pp_qual imp_mod omit
576 | otherwise -> pp_qual mod False
577 NonLocalDef ImplicitImport omit -> pp_qual mod (user_sty && omit)
579 user_sty = userStyle sty
580 iface_sty = ifaceStyle sty
582 pp_qual mod omit_qual
584 | otherwise = pprModule mod <> dot
586 pp_global_debug sty uniq prov
587 | debugStyle sty = hcat [text "{-", pprUnique uniq, prov_p prov, text "-}"]
590 prov_p prov | opt_PprStyle_NoPrags = empty
591 | otherwise = comma <> pp_prov prov
593 pp_prov (LocalDef _ Exported) = char 'x'
594 pp_prov (LocalDef _ NotExported) = char 'l'
595 pp_prov (NonLocalDef ImplicitImport _) = char 'j'
596 pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I' -- Imported by name
597 pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i' -- Imported by ..
598 pp_prov SystemProv = char 's'
602 %************************************************************************
604 \subsection{Overloaded functions related to Names}
606 %************************************************************************
609 class NamedThing a where
610 getOccName :: a -> OccName
613 getOccName n = nameOccName (getName n) -- Default method
617 getSrcLoc :: NamedThing a => a -> SrcLoc
618 isLocallyDefined :: NamedThing a => a -> Bool
619 getOccString :: NamedThing a => a -> String
621 getSrcLoc = nameSrcLoc . getName
622 isLocallyDefined = isLocallyDefinedName . getName
623 getOccString x = occNameString (getOccName x)
627 {-# SPECIALIZE isLocallyDefined :: Name -> Bool #-}