2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
7 #include "HsVersions.h"
10 -- Re-export the Module type
12 pprModule, moduleString,
16 pprOccName, occNameString, occNameFlavour,
17 isTvOcc, isTCOcc, isVarOcc, prefixOccName,
22 mkLocalName, mkSysLocalName,
24 mkCompoundName, mkGlobalName, mkInstDeclName,
26 mkWiredInIdName, mkWiredInTyConName,
27 maybeWiredInIdName, maybeWiredInTyConName,
30 nameUnique, changeUnique, setNameProvenance, getNameProvenance,
32 nameOccName, nameString, nameModule,
34 isExportedName, nameSrcLoc,
43 emptyNameSet, unitNameSet, mkNameSet, unionNameSets, unionManyNameSets,
44 minusNameSet, elemNameSet, nameSetToList, addOneToNameSet, addListToNameSet, isEmptyNameSet,
47 Provenance(..), pprProvenance,
50 -- Class NamedThing and overloaded friends
52 modAndOcc, isExported,
53 getSrcLoc, isLocallyDefined, getOccString
57 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
58 IMPORT_DELOOPER(TyLoop) ( GenId, Id(..), TyCon ) -- Used inside Names
60 import {-# SOURCE #-} Id ( Id )
61 import {-# SOURCE #-} TyCon ( TyCon )
64 import CStrings ( identToC, modnameToC, cSEP )
65 import CmdLineOpts ( opt_OmitInterfacePragmas, opt_EnsureSplittableC, all_toplev_ids_visible )
66 import BasicTypes ( SYN_IE(Module), IfaceFlavour(..), moduleString, pprModule )
68 import Outputable ( Outputable(..), PprStyle(..), codeStyle, ifaceStyle )
69 import PrelMods ( gHC__ )
71 import Lex ( isLexSym, isLexConId )
72 import SrcLoc ( noSrcLoc, SrcLoc )
73 import Usage ( SYN_IE(UVar), SYN_IE(Usage) )
74 import Unique ( pprUnique, showUnique, Unique, Uniquable(..) )
75 import UniqSet ( UniqSet(..), emptyUniqSet, unitUniqSet, unionUniqSets, uniqSetToList, isEmptyUniqSet,
76 unionManyUniqSets, minusUniqSet, mkUniqSet, elementOfUniqSet, addListToUniqSet, addOneToUniqSet )
77 import UniqFM ( UniqFM )
78 import Util ( Ord3(..), cmpPString, panic, assertPanic {-, pprTrace ToDo:rm-} )
82 %************************************************************************
84 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
86 %************************************************************************
89 data OccName = VarOcc FAST_STRING -- Variables and data constructors
90 | TvOcc FAST_STRING -- Type variables
91 | TCOcc FAST_STRING -- Type constructors and classes
93 pprOccName :: PprStyle -> OccName -> Doc
94 pprOccName sty n = if codeStyle sty
95 then identToC (occNameString n)
96 else ptext (occNameString n)
98 occNameString :: OccName -> FAST_STRING
99 occNameString (VarOcc s) = s
100 occNameString (TvOcc s) = s
101 occNameString (TCOcc s) = s
103 prefixOccName :: FAST_STRING -> OccName -> OccName
104 prefixOccName prefix (VarOcc s) = VarOcc (prefix _APPEND_ s)
105 prefixOccName prefix (TvOcc s) = TvOcc (prefix _APPEND_ s)
106 prefixOccName prefix (TCOcc s) = TCOcc (prefix _APPEND_ s)
108 -- occNameFlavour is used only to generate good error messages, so it doesn't matter
109 -- that the VarOcc case isn't mega-efficient. We could have different Occ constructors for
110 -- data constructors and values, but that makes everything else a bit more complicated.
111 occNameFlavour :: OccName -> String
112 occNameFlavour (VarOcc s) | isLexConId s = "Data constructor"
113 | otherwise = "Value"
114 occNameFlavour (TvOcc s) = "Type variable"
115 occNameFlavour (TCOcc s) = "Type constructor or class"
117 isVarOcc, isTCOcc, isTvOcc :: OccName -> Bool
118 isVarOcc (VarOcc s) = True
119 isVarOcc other = False
121 isTvOcc (TvOcc s) = True
122 isTvOcc other = False
124 isTCOcc (TCOcc s) = True
125 isTCOcc other = False
127 instance Eq OccName where
128 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
129 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
131 instance Ord OccName where
132 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
133 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
134 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
135 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
137 instance Ord3 OccName where
140 (VarOcc s1) `cmpOcc` (VarOcc s2) = s1 `_CMP_STRING_` s2
141 (VarOcc s1) `cmpOcc` other2 = LT_
143 (TvOcc s1) `cmpOcc` (VarOcc s2) = GT_
144 (TvOcc s1) `cmpOcc` (TvOcc s2) = s1 `_CMP_STRING_` s2
145 (TvOcc s1) `cmpOcc` other = LT_
147 (TCOcc s1) `cmpOcc` (TCOcc s2) = s1 `_CMP_STRING_` s2
148 (TCOcc s1) `cmpOcc` other = GT_
150 instance Outputable OccName where
155 %************************************************************************
157 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
159 %************************************************************************
168 Module -- The defining module
169 OccName -- Its name in that module
170 Provenance -- How it was defined
173 Things with a @Global@ name are given C static labels, so they finally
174 appear in the .o file's symbol table. They appear in the symbol table
175 in the form M.n. If originally-local things have this property they
176 must be made @Global@ first.
180 = LocalDef ExportFlag SrcLoc -- Locally defined
181 | Imported Module SrcLoc IfaceFlavour -- Directly imported from M;
182 -- gives name of module in import statement
183 -- and locn of import statement
184 | Implicit IfaceFlavour -- Implicitly imported
185 | WiredInTyCon TyCon -- There's a wired-in version
186 | WiredInId Id -- ...ditto...
189 Something is "Exported" if it may be mentioned by another module without
190 warning. The crucial thing about Exported things is that they must
191 never be dropped as dead code, even if they aren't used in this module.
192 Furthermore, being Exported means that we can't see all call sites of the thing.
194 Exported things include:
195 - explicitly exported Ids, including data constructors, class method selectors
196 - dfuns from instance decls
198 Being Exported is *not* the same as finally appearing in the .o file's
199 symbol table. For example, a local Id may be mentioned in an Exported
200 Id's unfolding in the interface file, in which case the local Id goes
204 data ExportFlag = Exported | NotExported
208 mkLocalName :: Unique -> OccName -> SrcLoc -> Name
211 mkGlobalName :: Unique -> Module -> OccName -> Provenance -> Name
212 mkGlobalName = Global
214 mkSysLocalName :: Unique -> FAST_STRING -> SrcLoc -> Name
215 mkSysLocalName uniq str loc = Local uniq (VarOcc str) loc
217 mkWiredInIdName :: Unique -> Module -> FAST_STRING -> Id -> Name
218 mkWiredInIdName uniq mod occ id
219 = Global uniq mod (VarOcc occ) (WiredInId id)
221 mkWiredInTyConName :: Unique -> Module -> FAST_STRING -> TyCon -> Name
222 mkWiredInTyConName uniq mod occ tycon
223 = Global uniq mod (TCOcc occ) (WiredInTyCon tycon)
226 mkCompoundName :: (FAST_STRING -> FAST_STRING) -- Occurrence-name modifier
227 -> Unique -- New unique
228 -> Name -- Base name (must be a Global)
229 -> Name -- Result is always a value name
231 mkCompoundName str_fn uniq (Global _ mod occ prov)
232 = Global uniq mod new_occ prov
234 new_occ = VarOcc (str_fn (occNameString occ)) -- Always a VarOcc
236 mkCompoundName str_fn uniq (Local _ occ loc)
237 = Local uniq (VarOcc (str_fn (occNameString occ))) loc
239 -- Rather a wierd one that's used for names generated for instance decls
240 mkInstDeclName :: Unique -> Module -> OccName -> SrcLoc -> Bool -> Name
241 mkInstDeclName uniq mod occ loc from_here
242 = Global uniq mod occ prov
244 prov | from_here = LocalDef Exported loc
245 | otherwise = Implicit HiFile -- Odd
248 setNameProvenance :: Name -> Provenance -> Name
249 -- setNameProvenance used to only change the provenance of Implicit-provenance things,
250 -- but that gives bad error messages for names defined twice in the same
251 -- module, so I changed it to set the proveance of *any* global (SLPJ Jun 97)
252 setNameProvenance (Global uniq mod occ _) prov = Global uniq mod occ prov
253 setNameProvenance other_name prov = other_name
255 getNameProvenance :: Name -> Provenance
256 getNameProvenance (Global uniq mod occ prov) = prov
257 getNameProvenance (Local uniq occ locn) = LocalDef NotExported locn
259 -- When we renumber/rename things, we need to be
260 -- able to change a Name's Unique to match the cached
261 -- one in the thing it's the name of. If you know what I mean.
262 changeUnique (Local _ n l) u = Local u n l
263 changeUnique (Global _ mod occ prov) u = Global u mod occ prov
266 setNameVisibility is applied to names in the final program
268 The Maybe Module argument is (Just mod) for top-level values,
269 and Nothing for all others (local values and type variables)
271 For top-level things, it globalises Local names
272 (if all top-level things should be visible)
273 and localises non-exported Global names
274 (if only exported things should be visible)
276 For nested things it localises Global names.
278 In all cases except an exported global, it gives it a new occurrence name.
280 The "visibility" here concerns whether the .o file's symbol table
281 mentions the thing; if so, it needs a module name in its symbol.
282 The Global things are "visible" and the Local ones are not
284 Why should things be "visible"? Certainly they must be if they
285 are exported. But also:
287 (a) In certain (prelude only) modules we split up the .hc file into
288 lots of separate little files, which are separately compiled by the C
289 compiler. That gives lots of little .o files. The idea is that if
290 you happen to mention one of them you don't necessarily pull them all
291 in. (Pulling in a piece you don't need can be v bad, because it may
292 mention other pieces you don't need either, and so on.)
294 Sadly, splitting up .hc files means that local names (like s234) are
295 now globally visible, which can lead to clashes between two .hc
296 files. So unlocaliseWhatnot goes through making all the local things
297 into global things, essentially by giving them full names so when they
298 are printed they'll have their module name too. Pretty revolting
301 (b) When optimisation is on we want to make all the internal
302 top-level defns externally visible
305 setNameVisibility :: Maybe Module -> Unique -> Name -> Name
307 setNameVisibility maybe_mod occ_uniq name@(Global uniq mod occ (LocalDef NotExported loc))
308 | not all_toplev_ids_visible || not_top_level maybe_mod
309 = Local uniq (uniqToOccName occ_uniq) loc -- Localise Global name
311 setNameVisibility maybe_mod occ_uniq name@(Global _ _ _ _)
312 = name -- Otherwise don't fiddle with Global
314 setNameVisibility (Just mod) occ_uniq (Local uniq occ loc)
315 | all_toplev_ids_visible
316 = Global uniq mod -- Globalise Local name
317 (uniqToOccName occ_uniq)
318 (LocalDef NotExported loc)
320 setNameVisibility maybe_mod occ_uniq (Local uniq occ loc)
321 = Local uniq (uniqToOccName occ_uniq) loc -- New OccName for Local
323 uniqToOccName uniq = VarOcc (_PK_ ('$':showUnique uniq))
324 -- The "$" is to make sure that this OccName is distinct from all user-defined ones
326 not_top_level (Just m) = False
327 not_top_level Nothing = True
331 %************************************************************************
333 \subsection{Predicates and selectors}
335 %************************************************************************
338 nameUnique :: Name -> Unique
339 nameModAndOcc :: Name -> (Module, OccName) -- Globals only
340 nameOccName :: Name -> OccName
341 nameModule :: Name -> Module
342 nameString :: Name -> FAST_STRING -- A.b form
343 nameSrcLoc :: Name -> SrcLoc
344 isLocallyDefinedName :: Name -> Bool
345 isExportedName :: Name -> Bool
346 isWiredInName :: Name -> Bool
347 isLocalName :: Name -> Bool
351 nameUnique (Local u _ _) = u
352 nameUnique (Global u _ _ _) = u
354 nameOccName (Local _ occ _) = occ
355 nameOccName (Global _ _ occ _) = occ
357 nameModule (Global _ mod occ _) = mod
359 nameModAndOcc (Global _ mod occ _) = (mod,occ)
361 nameString (Local _ occ _) = occNameString occ
362 nameString (Global _ mod occ _) = mod _APPEND_ SLIT(".") _APPEND_ occNameString occ
364 isExportedName (Global _ _ _ (LocalDef Exported _)) = True
365 isExportedName other = False
367 nameSrcLoc (Local _ _ loc) = loc
368 nameSrcLoc (Global _ _ _ (LocalDef _ loc)) = loc
369 nameSrcLoc (Global _ _ _ (Imported _ loc _)) = loc
370 nameSrcLoc other = noSrcLoc
372 isLocallyDefinedName (Local _ _ _) = True
373 isLocallyDefinedName (Global _ _ _ (LocalDef _ _)) = True
374 isLocallyDefinedName other = False
376 -- Things the compiler "knows about" are in some sense
377 -- "imported". When we are compiling the module where
378 -- the entities are defined, we need to be able to pick
379 -- them out, often in combination with isLocallyDefined.
380 isWiredInName (Global _ _ _ (WiredInTyCon _)) = True
381 isWiredInName (Global _ _ _ (WiredInId _)) = True
382 isWiredInName _ = False
384 maybeWiredInIdName :: Name -> Maybe Id
385 maybeWiredInIdName (Global _ _ _ (WiredInId id)) = Just id
386 maybeWiredInIdName other = Nothing
388 maybeWiredInTyConName :: Name -> Maybe TyCon
389 maybeWiredInTyConName (Global _ _ _ (WiredInTyCon tc)) = Just tc
390 maybeWiredInTyConName other = Nothing
393 isLocalName (Local _ _ _) = True
394 isLocalName _ = False
398 %************************************************************************
400 \subsection[Name-instances]{Instance declarations}
402 %************************************************************************
405 cmpName n1 n2 = c n1 n2
407 c (Local u1 _ _) (Local u2 _ _) = cmp u1 u2
408 c (Local _ _ _) _ = LT_
409 c (Global u1 _ _ _) (Global u2 _ _ _) = cmp u1 u2
410 c (Global _ _ _ _) _ = GT_
414 instance Eq Name where
415 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
416 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
418 instance Ord Name where
419 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
420 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
421 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
422 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
424 instance Ord3 Name where
427 instance Uniquable Name where
428 uniqueOf = nameUnique
430 instance NamedThing Name where
436 %************************************************************************
438 \subsection{Pretty printing}
440 %************************************************************************
443 instance Outputable Name where
444 ppr PprQuote name@(Local _ _ _) = quotes (ppr (PprForUser 1) name)
446 -- When printing interfaces, all Locals have been given nice print-names
447 ppr (PprForUser _) (Local _ n _) = ptext (occNameString n)
448 ppr PprInterface (Local _ n _) = ptext (occNameString n)
450 ppr sty (Local u n _) | codeStyle sty = pprUnique u
452 ppr sty (Local u n _) = hcat [ptext (occNameString n), ptext SLIT("_"), pprUnique u]
454 ppr PprQuote name@(Global _ _ _ _) = quotes (ppr (PprForUser 1) name)
456 ppr sty name@(Global u m n _)
458 = identToC (m _APPEND_ SLIT(".") _APPEND_ occNameString n)
460 ppr sty name@(Global u m n prov)
461 = hcat [pp_mod_dot, ptext (occNameString n), pp_debug sty name]
463 pp_mod = pprModule (PprForUser 1) m
464 pp_mod_dot = case prov of --- Omit home module qualifier
465 LocalDef _ _ -> empty
466 Imported _ _ hif -> pp_mod <> pp_dot hif
467 Implicit hif -> pp_mod <> pp_dot hif
468 other -> pp_mod <> text "."
470 pp_dot HiFile = text "." -- Vanilla case
471 pp_dot HiBootFile = text "!" -- M!t indicates a name imported from a .hi-boot interface
474 pp_debug PprDebug (Global uniq m n prov) = hcat [text "{-", pprUnique uniq, char ',',
475 pp_prov prov, text "-}"]
477 pp_prov (LocalDef Exported _) = char 'x'
478 pp_prov (LocalDef NotExported _) = char 'l'
479 pp_prov (Imported _ _ _) = char 'i'
480 pp_prov (Implicit _) = char 'p'
481 pp_prov (WiredInTyCon _) = char 'W'
482 pp_prov (WiredInId _) = char 'w'
483 pp_debug other name = empty
485 -- pprNameProvenance is used in error messages to say where a name came from
486 pprNameProvenance :: PprStyle -> Name -> Doc
487 pprNameProvenance sty (Local _ _ loc) = pprProvenance sty (LocalDef NotExported loc)
488 pprNameProvenance sty (Global _ _ _ prov) = pprProvenance sty prov
490 pprProvenance :: PprStyle -> Provenance -> Doc
491 pprProvenance sty (Imported mod loc _)
492 = sep [ptext SLIT("Imported from"), pprModule sty mod, ptext SLIT("at"), ppr sty loc]
493 pprProvenance sty (LocalDef _ loc) = sep [ptext SLIT("Defined at"), ppr sty loc]
494 pprProvenance sty (Implicit _) = panic "pprNameProvenance: Implicit"
495 pprProvenance sty (WiredInTyCon tc) = ptext SLIT("Wired-in tycon")
496 pprProvenance sty (WiredInId id) = ptext SLIT("Wired-in id")
500 %************************************************************************
502 \subsection[Sets of names}
504 %************************************************************************
507 type NameSet = UniqSet Name
508 emptyNameSet :: NameSet
509 unitNameSet :: Name -> NameSet
510 addListToNameSet :: NameSet -> [Name] -> NameSet
511 addOneToNameSet :: NameSet -> Name -> NameSet
512 mkNameSet :: [Name] -> NameSet
513 unionNameSets :: NameSet -> NameSet -> NameSet
514 unionManyNameSets :: [NameSet] -> NameSet
515 minusNameSet :: NameSet -> NameSet -> NameSet
516 elemNameSet :: Name -> NameSet -> Bool
517 nameSetToList :: NameSet -> [Name]
518 isEmptyNameSet :: NameSet -> Bool
520 isEmptyNameSet = isEmptyUniqSet
521 emptyNameSet = emptyUniqSet
522 unitNameSet = unitUniqSet
523 mkNameSet = mkUniqSet
524 addListToNameSet = addListToUniqSet
525 addOneToNameSet = addOneToUniqSet
526 unionNameSets = unionUniqSets
527 unionManyNameSets = unionManyUniqSets
528 minusNameSet = minusUniqSet
529 elemNameSet = elementOfUniqSet
530 nameSetToList = uniqSetToList
535 %************************************************************************
537 \subsection{Overloaded functions related to Names}
539 %************************************************************************
542 class NamedThing a where
543 getOccName :: a -> OccName -- Even RdrNames can do this!
546 getOccName n = nameOccName (getName n) -- Default method
550 modAndOcc :: NamedThing a => a -> (Module, OccName)
551 getModule :: NamedThing a => a -> Module
552 getSrcLoc :: NamedThing a => a -> SrcLoc
553 isLocallyDefined :: NamedThing a => a -> Bool
554 isExported :: NamedThing a => a -> Bool
555 getOccString :: NamedThing a => a -> String
557 modAndOcc = nameModAndOcc . getName
558 getModule = nameModule . getName
559 isExported = isExportedName . getName
560 getSrcLoc = nameSrcLoc . getName
561 isLocallyDefined = isLocallyDefinedName . getName
562 getOccString x = _UNPK_ (occNameString (getOccName x))
566 {-# SPECIALIZE isLocallyDefined