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,
27 maybeUserImportedFrom,
28 nameSrcLoc, isLocallyDefinedName, isDllName,
30 isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
34 Provenance(..), ImportReason(..), pprProvenance,
35 ExportFlag(..), PrintUnqualified,
36 pprNameProvenance, hasBetterProv,
38 -- Class NamedThing and overloaded friends
40 getSrcLoc, isLocallyDefined, getOccString, toRdrName
43 #include "HsVersions.h"
45 import {-# SOURCE #-} Var ( Id, setIdName )
46 import {-# SOURCE #-} TyCon ( TyCon, setTyConName )
48 import OccName -- All of it
49 import Module ( Module, moduleName, pprModule, mkVanillaModule, isLocalModule )
50 import RdrName ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc, rdrNameModule )
51 import CmdLineOpts ( opt_Static, opt_PprStyle_NoPrags, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
53 import SrcLoc ( noSrcLoc, mkBuiltinSrcLoc, SrcLoc )
54 import Unique ( pprUnique, Unique, Uniquable(..), unboundKey, u2i )
60 %************************************************************************
62 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
64 %************************************************************************
68 n_sort :: NameSort, -- What sort of name it is
70 n_occ :: OccName, -- Its occurrence name
71 n_prov :: Provenance -- How it was made
78 | WiredInTyCon Module TyCon
81 Things with a @Global@ name are given C static labels, so they finally
82 appear in the .o file's symbol table. They appear in the symbol table
83 in the form M.n. If originally-local things have this property they
84 must be made @Global@ first.
87 mkLocalName :: Unique -> OccName -> SrcLoc -> Name
88 mkLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ,
89 n_prov = LocalDef loc NotExported }
90 -- NB: You might worry that after lots of huffing and
91 -- puffing we might end up with two local names with distinct
92 -- uniques, but the same OccName. Indeed we can, but that's ok
93 -- * the insides of the compiler don't care: they use the Unique
94 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
95 -- uniques if you get confused
96 -- * for interface files we tidyCore first, which puts the uniques
97 -- into the print name (see setNameVisibility below)
99 mkImportedLocalName :: Unique -> OccName -> SrcLoc -> Name
100 -- Just the same as mkLocalName, except the provenance is different
101 -- Reason: this flags the name as one that came in from an interface file.
102 -- This is useful when trying to decide which of two type variables
103 -- should 'win' when unifying them.
104 -- NB: this is only for non-top-level names, so we use ImplicitImport
105 mkImportedLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ,
106 n_prov = NonLocalDef ImplicitImport True }
109 mkGlobalName :: Unique -> Module -> OccName -> Provenance -> Name
110 mkGlobalName uniq mod occ prov = Name { n_uniq = uniq, n_sort = Global mod,
111 n_occ = occ, n_prov = prov }
114 mkKnownKeyGlobal :: (RdrName, Unique) -> Name
115 mkKnownKeyGlobal (rdr_name, uniq)
116 = mkGlobalName uniq (mkVanillaModule (rdrNameModule rdr_name))
117 (rdrNameOcc rdr_name)
120 mkSysLocalName :: Unique -> UserFS -> Name
121 mkSysLocalName uniq fs = Name { n_uniq = uniq, n_sort = Local,
122 n_occ = mkSrcVarOcc fs, n_prov = systemProvenance }
124 mkCCallName :: Unique -> EncodedString -> Name
125 -- The encoded string completely describes the ccall
126 mkCCallName uniq str = Name { n_uniq = uniq, n_sort = Local,
127 n_occ = mkCCallOcc str,
128 n_prov = NonLocalDef ImplicitImport True }
130 mkTopName :: Unique -> Module -> FAST_STRING -> Name
131 -- Make a top-level name; make it Global if top-level
132 -- things should be externally visible; Local otherwise
133 -- This chap is only used *after* the tidyCore phase
134 -- Notably, it is used during STG lambda lifting
136 -- We have to make sure that the name is globally unique
137 -- and we don't have tidyCore to help us. So we append
138 -- the unique. Hack! Hack!
139 mkTopName uniq mod fs
140 = Name { n_uniq = uniq,
141 n_sort = mk_top_sort mod,
142 n_occ = mkSrcVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
143 n_prov = LocalDef noSrcLoc NotExported }
145 mkIPName :: Unique -> OccName -> Name
147 = Name { n_uniq = uniq,
150 -- ZZ is this an appropriate provinence?
151 n_prov = SystemProv }
153 ------------------------- Wired in names -------------------------
155 mkWiredInIdName :: Unique -> Module -> OccName -> Id -> Name
156 mkWiredInIdName uniq mod occ id = Name { n_uniq = uniq, n_sort = WiredInId mod id,
157 n_occ = occ, n_prov = SystemProv }
159 -- mkWiredInTyConName takes a FAST_STRING instead of
160 -- an OccName, which is a bit yukky but that's what the
161 -- clients find easiest.
162 mkWiredInTyConName :: Unique -> Module -> FAST_STRING -> TyCon -> Name
163 mkWiredInTyConName uniq mod fs tycon
164 = Name { n_uniq = uniq, n_sort = WiredInTyCon mod tycon,
165 n_occ = mkSrcOccFS tcName fs, n_prov = SystemProv }
168 ---------------------------------------------------------------------
169 mkDerivedName :: (OccName -> OccName)
171 -> Unique -- New unique
172 -> Name -- Result is always a value name
174 mkDerivedName f name uniq = name {n_uniq = uniq, n_occ = f (n_occ name)}
176 -- mkUnboundName makes a place-holder Name; it shouldn't be looked at except possibly
177 -- during compiler debugging.
178 mkUnboundName :: RdrName -> Name
179 mkUnboundName rdr_name = mkLocalName unboundKey (rdrNameOcc rdr_name) noSrcLoc
181 isUnboundName :: Name -> Bool
182 isUnboundName name = getUnique name == unboundKey
186 -- When we renumber/rename things, we need to be
187 -- able to change a Name's Unique to match the cached
188 -- one in the thing it's the name of. If you know what I mean.
189 setNameUnique name uniq = name {n_uniq = uniq}
191 setNameOcc :: Name -> OccName -> Name
192 -- Give the thing a new OccName, *and*
193 -- record that it's no longer a sys-local
194 -- This is used by the tidy-up pass
195 setNameOcc name occ = name {n_occ = occ}
197 setNameModule :: Name -> Module -> Name
198 setNameModule name mod = name {n_sort = set (n_sort name)}
200 set (Global _) = Global mod
201 set (WiredInId _ id) = WiredInId mod id
202 set (WiredInTyCon _ tycon) = WiredInTyCon mod tycon
206 %************************************************************************
208 \subsection{Setting provenance and visibility
210 %************************************************************************
212 tidyTopName is applied to top-level names in the final program
214 For top-level things, it globalises Local names
215 (if all top-level things should be visible)
216 and localises non-exported Global names
217 (if only exported things should be visible)
219 In all cases except an exported global, it gives it a new occurrence name.
221 The "visibility" here concerns whether the .o file's symbol table
222 mentions the thing; if so, it needs a module name in its symbol.
223 The Global things are "visible" and the Local ones are not
225 Why should things be "visible"? Certainly they must be if they
226 are exported. But also:
228 (a) In certain (prelude only) modules we split up the .hc file into
229 lots of separate little files, which are separately compiled by the C
230 compiler. That gives lots of little .o files. The idea is that if
231 you happen to mention one of them you don't necessarily pull them all
232 in. (Pulling in a piece you don't need can be v bad, because it may
233 mention other pieces you don't need either, and so on.)
235 Sadly, splitting up .hc files means that local names (like s234) are
236 now globally visible, which can lead to clashes between two .hc
237 files. So unlocaliseWhatnot goes through making all the local things
238 into global things, essentially by giving them full names so when they
239 are printed they'll have their module name too. Pretty revolting
242 (b) When optimisation is on we want to make all the internal
243 top-level defns externally visible
246 tidyTopName :: Module -> TidyOccEnv -> Name -> (TidyOccEnv, Name)
247 tidyTopName mod env name
250 (env', occ') = tidyOccName env (n_occ name)
252 name' = Name { n_uniq = n_uniq name, n_sort = mk_top_sort mod,
253 n_occ = occ', n_prov = LocalDef noSrcLoc NotExported }
255 mk_top_sort mod | all_toplev_ids_visible = Global mod
258 all_toplev_ids_visible =
259 not opt_OmitInterfacePragmas || -- Pragmas can make them visible
260 opt_EnsureSplittableC -- Splitting requires visiblilty
265 setNameProvenance :: Name -> Provenance -> Name
266 -- setNameProvenance used to only change the provenance of
267 -- Implicit-provenance things, but that gives bad error messages
268 -- for names defined twice in the same module, so I changed it to
269 -- set the provenance of *any* global (SLPJ Jun 97)
270 setNameProvenance name prov = name {n_prov = prov}
272 getNameProvenance :: Name -> Provenance
273 getNameProvenance name = n_prov name
275 setNameImportReason :: Name -> ImportReason -> Name
276 setNameImportReason name reason
277 = name { n_prov = new_prov }
279 -- It's important that we don't do the pattern matching
280 -- in the top-level clause, else we get a black hole in
281 -- the renamer. Rather a yukky constraint. There's only
282 -- one call, in RnNames
283 old_prov = n_prov name
284 new_prov = case old_prov of
285 NonLocalDef _ omit -> NonLocalDef reason omit
290 %************************************************************************
292 \subsection{Provenance and export info}
294 %************************************************************************
298 = LocalDef -- Defined locally
300 ExportFlag -- Whether it's exported
302 | NonLocalDef -- Defined non-locally
306 | SystemProv -- Either (a) a system-generated local with
307 -- a v short name OccName
308 -- or (b) a known-key global which should have a proper
309 -- provenance attached by the renamer
312 Sys-provs are only used internally. When the compiler generates (say)
313 a fresh desguar variable it always calls it "ds", and of course it gets
314 a fresh unique. But when printing -ddump-xx dumps, we must print it with
315 its unique, because there'll be a lot of "ds" variables.
317 Names with SystemProv differ in the following ways:
318 a) locals have unique attached when printing dumps
319 b) unifier eliminates sys tyvars in favour of user provs where possible
320 c) renamer replaces SystemProv with a better one
322 Before anything gets printed in interface files or output code, it's
323 fed through a 'tidy' processor, which zaps the OccNames to have
324 unique names; and converts all sys-locals to user locals
325 If any desugarer sys-locals have survived that far, they get changed to
330 = UserImport Module SrcLoc Bool -- Imported from module M on line L
331 -- Note the M may well not be the defining module
333 -- The Bool is true iff the thing was named *explicitly* in the import spec,
334 -- rather than being imported as part of a group; e.g.
337 -- Here, everything imported by B, and the constructors of T
338 -- are not named explicitly; only T is named explicitly.
339 -- This info is used when warning of unused names.
341 | ImplicitImport -- Imported implicitly for some other reason
344 type PrintUnqualified = Bool -- True <=> the unqualified name of this thing is
345 -- in scope in this module, so print it
346 -- unqualified in error messages
348 data ExportFlag = Exported | NotExported
351 Something is "Exported" if it may be mentioned by another module without
352 warning. The crucial thing about Exported things is that they must
353 never be dropped as dead code, even if they aren't used in this module.
354 Furthermore, being Exported means that we can't see all call sites of the thing.
356 Exported things include:
358 - explicitly exported Ids, including data constructors,
359 class method selectors
361 - dfuns from instance decls
363 Being Exported is *not* the same as finally appearing in the .o file's
364 symbol table. For example, a local Id may be mentioned in an Exported
365 Id's unfolding in the interface file, in which case the local Id goes
370 systemProvenance :: Provenance
371 systemProvenance = SystemProv
373 -- pprNameProvenance is used in error messages to say where a name came from
374 pprNameProvenance :: Name -> SDoc
375 pprNameProvenance name = pprProvenance (getNameProvenance name)
377 pprProvenance :: Provenance -> SDoc
378 pprProvenance SystemProv = ptext SLIT("System")
379 pprProvenance (LocalDef loc _) = ptext SLIT("defined at") <+> ppr loc
380 pprProvenance (NonLocalDef ImplicitImport _)
381 = ptext SLIT("implicitly imported")
382 pprProvenance (NonLocalDef (UserImport mod loc _) _)
383 = ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
387 %************************************************************************
389 \subsection{Predicates and selectors}
391 %************************************************************************
394 nameUnique :: Name -> Unique
395 nameOccName :: Name -> OccName
396 nameModule :: Name -> Module
397 nameSrcLoc :: Name -> SrcLoc
398 isLocallyDefinedName :: Name -> Bool
399 isUserExportedName :: Name -> Bool
400 isWiredInName :: Name -> Bool
401 isLocalName :: Name -> Bool
402 isGlobalName :: Name -> Bool
403 isExternallyVisibleName :: Name -> Bool
407 hashName :: Name -> Int
408 hashName name = IBOX( u2i (nameUnique name) )
410 nameUnique name = n_uniq name
411 nameOccName name = n_occ name
415 Local -> pprPanic "nameModule" (ppr name)
416 x -> nameSortModule x
418 nameSortModule (Global mod) = mod
419 nameSortModule (WiredInId mod _) = mod
420 nameSortModule (WiredInTyCon mod _) = mod
422 nameRdrName :: Name -> RdrName
423 nameRdrName (Name { n_sort = Local, n_occ = occ }) = mkRdrUnqual occ
424 nameRdrName (Name { n_sort = sort, n_occ = occ }) = mkRdrQual (moduleName (nameSortModule sort)) occ
426 ifaceNameRdrName :: Name -> RdrName
427 -- Makes a qualified naem for imported things,
428 -- and an unqualified one for local things
429 ifaceNameRdrName n | isLocallyDefined n = mkRdrUnqual (nameOccName n)
430 | otherwise = mkRdrQual (moduleName (nameModule n)) (nameOccName n)
432 isUserExportedName (Name { n_prov = LocalDef _ Exported }) = True
433 isUserExportedName other = False
435 isUserImportedExplicitlyName (Name { n_prov = NonLocalDef (UserImport _ _ explicit) _ }) = explicit
436 isUserImportedExplicitlyName other = False
438 isUserImportedName (Name { n_prov = NonLocalDef (UserImport _ _ _) _ }) = True
439 isUserImportedName other = False
441 maybeUserImportedFrom (Name { n_prov = NonLocalDef (UserImport m _ _) _ }) = Just m
442 maybeUserImportedFrom other = Nothing
444 isDllName :: Name -> Bool
445 -- Does this name refer to something in a different DLL?
446 isDllName nm = not opt_Static &&
447 not (isLocallyDefinedName nm) &&
448 not (isLocalModule (nameModule nm))
450 nameSrcLoc name = provSrcLoc (n_prov name)
452 provSrcLoc (LocalDef loc _) = loc
453 provSrcLoc (NonLocalDef (UserImport _ loc _) _) = loc
454 provSrcLoc other = noSrcLoc
456 isLocallyDefinedName (Name {n_sort = Local}) = True -- Local (might have SystemProv)
457 isLocallyDefinedName (Name {n_prov = LocalDef _ _}) = True -- Global, but defined here
458 isLocallyDefinedName other = False -- Other
460 -- Things the compiler "knows about" are in some sense
461 -- "imported". When we are compiling the module where
462 -- the entities are defined, we need to be able to pick
463 -- them out, often in combination with isLocallyDefined.
464 isWiredInName (Name {n_sort = WiredInTyCon _ _}) = True
465 isWiredInName (Name {n_sort = WiredInId _ _}) = True
466 isWiredInName _ = False
468 maybeWiredInIdName :: Name -> Maybe Id
469 maybeWiredInIdName (Name {n_sort = WiredInId _ id}) = Just id
470 maybeWiredInIdName other = Nothing
472 maybeWiredInTyConName :: Name -> Maybe TyCon
473 maybeWiredInTyConName (Name {n_sort = WiredInTyCon _ tc}) = Just tc
474 maybeWiredInTyConName other = Nothing
477 isLocalName (Name {n_sort = Local}) = True
478 isLocalName _ = False
480 isGlobalName (Name {n_sort = Local}) = False
481 isGlobalName other = True
483 -- Global names are by definition those that are visible
484 -- outside the module, *as seen by the linker*. Externally visible
485 -- does not mean visible at the source level (that's isExported).
486 isExternallyVisibleName name = isGlobalName name
488 hasBetterProv :: Name -> Name -> Bool
489 hasBetterProv name1 name2
490 = case n_prov name1 of
493 NonLocalDef _ _ -> case n_prov name2 of
494 LocalDef _ _ -> False
497 isSystemName (Name {n_prov = SystemProv}) = True
498 isSystemName other = False
502 %************************************************************************
504 \subsection[Name-instances]{Instance declarations}
506 %************************************************************************
509 cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
513 instance Eq Name where
514 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
515 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
517 instance Ord Name where
518 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
519 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
520 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
521 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
522 compare a b = cmpName a b
524 instance Uniquable Name where
525 getUnique = nameUnique
527 instance NamedThing Name where
532 %************************************************************************
534 \subsection{Pretty printing}
536 %************************************************************************
539 instance Outputable Name where
540 -- When printing interfaces, all Locals have been given nice print-names
541 ppr name = pprName name
543 pprName (Name {n_sort = Local, n_uniq = uniq, n_occ = occ, n_prov = prov})
545 = getPprStyle $ \ sty ->
546 if codeStyle sty then
547 pprUnique uniq -- When printing in code we required all names to
548 -- be globally unique; for example, we use this identifier
549 -- for the closure name. So we just print the unique alone.
551 pprOccName occ <> pp_local_extra sty uniq
553 sys_local = case prov of
557 pp_local_extra sty uniq
558 | sys_local = underscore <> pprUnique uniq -- Must print uniques for sys_locals
559 | debugStyle sty = text "{-" <> pprUnique uniq <> text "-}"
563 pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ, n_prov = prov})
564 -- Globals, and wired in things
565 = getPprStyle $ \ sty ->
566 if codeStyle sty then
567 ppr mod <> underscore <> ppr occ
569 pp_mod_dot sty <> ppr occ <> pp_global_debug sty uniq prov
571 mod = nameSortModule sort
575 SystemProv -> pp_qual mod user_sty
576 -- Hack alert! Omit the qualifier on SystemProv things in user style
577 -- I claim such SystemProv things will also be WiredIn things.
578 -- We can't get the omit flag right
579 -- on wired in tycons etc (sigh) so we just leave it out in user style,
580 -- and hope that leaving it out isn't too consfusing.
581 -- (e.g. if the programmer hides Bool and redefines it. If so, use -dppr-debug.)
583 LocalDef _ _ -> pp_qual mod (user_sty || iface_sty)
585 NonLocalDef (UserImport imp_mod _ _) omit
586 | user_sty -> pp_qual imp_mod omit
587 | otherwise -> pp_qual mod False
588 NonLocalDef ImplicitImport omit -> pp_qual mod (user_sty && omit)
590 user_sty = userStyle sty
591 iface_sty = ifaceStyle sty
593 pp_qual mod omit_qual
595 | otherwise = pprModule mod <> dot
597 pp_global_debug sty uniq prov
598 | debugStyle sty = hcat [text "{-", pprUnique uniq, prov_p prov, text "-}"]
601 prov_p prov | opt_PprStyle_NoPrags = empty
602 | otherwise = comma <> pp_prov prov
604 pp_prov (LocalDef _ Exported) = char 'x'
605 pp_prov (LocalDef _ NotExported) = char 'l'
606 pp_prov (NonLocalDef ImplicitImport _) = char 'j'
607 pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I' -- Imported by name
608 pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i' -- Imported by ..
609 pp_prov SystemProv = char 's'
613 %************************************************************************
615 \subsection{Overloaded functions related to Names}
617 %************************************************************************
620 class NamedThing a where
621 getOccName :: a -> OccName
624 getOccName n = nameOccName (getName n) -- Default method
628 getSrcLoc :: NamedThing a => a -> SrcLoc
629 isLocallyDefined :: NamedThing a => a -> Bool
630 getOccString :: NamedThing a => a -> String
631 toRdrName :: NamedThing a => a -> RdrName
633 getSrcLoc = nameSrcLoc . getName
634 isLocallyDefined = isLocallyDefinedName . getName
635 getOccString x = occNameString (getOccName x)
636 toRdrName = ifaceNameRdrName . getName
640 {-# SPECIALIZE isLocallyDefined :: Name -> Bool #-}