2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 %************************************************************************
6 \section[NameTypes]{@NameTypes@: The flavours of names that we stick on things}
8 %************************************************************************
11 #include "HsVersions.h"
14 ShortName, FullName, -- abstract types
21 mkFullName, mkPrivateFullName, mkPreludeCoreName,
25 unlocaliseFullName, unlocaliseShortName,
27 -- and to make the interface self-sufficient....
28 ExportFlag, Unique, SrcLoc
31 CHK_Ubiq() -- debugging consistency check
32 import PrelLoop -- for paranoia checking
34 import PrelMods ( pRELUDE, pRELUDE_CORE ) -- NB: naughty import
36 import CStrings ( identToC, cSEP )
39 import PprStyle ( PprStyle(..), codeStyle )
41 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
42 import Unique ( showUnique, Unique )
46 %************************************************************************
48 \subsection[NameTypes-flavours]{Datatypes for names}
50 %************************************************************************
52 Here are the types; see the notes that follow.
55 = ShortName FAST_STRING -- entity's name in this module
56 SrcLoc -- defining location (only one possible)
59 = FullName FAST_STRING -- original module name
60 FAST_STRING -- entity's name in original module
61 Provenance -- where this thing came from
62 -- (also records its local name, if any)
63 ExportFlag -- where this thing is going (from here)
64 Bool -- True <=> invisible to the user
65 SrcLoc -- defining location (just one)
67 (@FullNames@ don't have fast-comparison keys; the things with
71 %----------------------------------------------------------------------
74 These are used for entities local to the module being compiled; for
75 example, function parameters, where- and let-bound things. These are
76 @TyVars@ (ToDo: what if imported???) and local @Ids@. They have
77 @Uniques@ for fast comparison.
79 %----------------------------------------------------------------------
81 These are used for things that either have, or may be required to
82 have, full-blown original names. All @Classes@ and @TyCons@ have full
83 names. All data-constructor and top-level @Ids@ (things that were
84 top-level in the original source) have fullnames.
87 %************************************************************************
89 \subsection[NameTypes-Provenance]{Where a name(d thing) came from}
91 %************************************************************************
93 The ``provenance'' of a name says something about where it came from.
97 to decide whether to generate the code fragments for constructors
98 (only done for @ThisModule@).
100 to detect when a thing is from @PreludeCore@, in which case we
101 use shorter target-code names.
108 | InventedInThisModule -- for workers/wrappers, specialized
109 -- versions, etc: anything "conjured up"
110 -- on the compiler's initiative.
112 | ExportedByPreludeCore -- these are the immutable, unrenamable
113 -- things the compiler knows about
115 | OtherPrelude FAST_STRING -- the FullName gave the *original*
116 -- name; this says what it was renamed
117 -- to (if anything); really just for
120 | OtherModule FAST_STRING -- as for OtherPrelude, just the occurrence
122 [FAST_STRING]-- The modules from whose interface we
123 -- got the information about this thing
125 | HereInPreludeCore -- used when compiling PreludeCore bits:
126 -- == ThisModule + ExportedByPreludeCore
128 | OtherInstance -- For imported instances.
129 FAST_STRING -- The module where this instance supposedly
130 -- was declared; "" if we don't know.
131 [FAST_STRING] -- The modules whose interface told us about
135 %************************************************************************
137 \subsection[NameTypes-access-fns]{Access functions for names}
139 %************************************************************************
143 mkShortName = ShortName
145 mkFullName m n p e l = FullName m n p e False{-not invisible-} l
147 mkPrivateFullName m n p e l = FullName m n p e True{-invisible-} l
149 mkPreludeCoreName mod name
150 = FullName mod name ExportedByPreludeCore ExportAll False mkBuiltinSrcLoc
151 -- Mark them as Exported; mkInterface may decide against it
152 -- later. (Easier than marking them NotExported, then later
153 -- deciding it would be a good idea...)
157 unlocaliseShortName :: FAST_STRING -> Unique -> ShortName -> FullName
159 {- We now elucidate Simon's favourite piece of code:
161 When we are told to "unlocalise" a ShortName, we really really want
162 the resulting monster to be unique (across the entire universe).
163 We can't count on the module name being printed (for Prelude
164 things, it isn't), so we brutally force the module-name into the
165 regular-name component.
167 We change the provenance to InventedInThisModule, because
170 unlocaliseShortName mod u (ShortName nm loc)
172 (mod _APPEND_ nm _APPEND_ (showUnique u))
176 -- FullNames really can't be mangled; someone out there
177 -- *expects* the thing to have this name.
178 -- We only change the export status.
180 unlocaliseFullName (FullName m n p _ i l)
181 = FullName m n p ExportAll i l
184 %************************************************************************
186 \subsection[NameTypes-instances]{Instance declarations for various names}
188 %************************************************************************
190 We don't have equality and ordering; that's defined for the things
191 that have @ShortNames@ and @FullNames@ in them.
194 instance NamedThing ShortName where
195 getExportFlag a = NotExported
196 isLocallyDefined a = True
197 getOrigName (ShortName s l) = (panic "NamedThing.ShortName.getOrigName", s)
198 getOccurrenceName (ShortName s l) = s
199 getSrcLoc (ShortName s l) = l
200 fromPreludeCore _ = False
202 getItsUnique (ShortName s l) = panic "NamedThing.ShortName.getItsUnique"
203 getInformingModules a = panic "NamedThing.ShortName.getInformingModule"
208 instance NamedThing FullName where
210 getExportFlag (FullName m s p e i l) = e
211 getOrigName (FullName m s p e i l) = (m, s)
212 getSrcLoc (FullName m s p e i l) = l
214 isLocallyDefined (FullName m s p e i l)
217 InventedInThisModule -> True
218 HereInPreludeCore -> True
221 getOccurrenceName (FullName _ s p _ _ _)
227 fromPreludeCore (FullName _ _ p _ _ _)
229 ExportedByPreludeCore -> True
230 HereInPreludeCore -> True
233 getInformingModules (FullName _ _ p _ _ _)
235 ThisModule -> [] -- Urgh. ToDo
236 InventedInThisModule -> []
237 OtherModule _ ms -> ms
238 OtherInstance _ ms -> ms
239 ExportedByPreludeCore -> [pRELUDE_CORE]
240 HereInPreludeCore -> [pRELUDE_CORE]
241 OtherPrelude _ -> [pRELUDE]
244 getItsUnique = panic "NamedThing.FullName.getItsUnique"
250 fromPrelude :: FAST_STRING -> Bool
252 fromPrelude s = (_SUBSTR_ s 0 6 == SLIT("Prelude"))
254 invisibleFullName (FullName m s p e i l) = i
257 Forcing and printing:
259 instance Outputable ShortName where
260 ppr sty (ShortName s loc) = ppPStr s
262 instance Outputable FullName where
263 ppr sty name@(FullName m s p e i l)
265 ppBeside (if fromPreludeCore name
270 PprInterface -> ppNil
271 PprUnfolding -> ppNil -- ToDo: something diff later?
272 PprForC -> ppBeside (identToC m) (ppPStr cSEP)
273 PprForAsm False _ -> ppBeside (identToC m) (ppPStr cSEP)
274 PprForAsm True _ -> ppBesides [ppPStr cSEP, identToC m, ppPStr cSEP]
275 _ -> ppBeside (ppPStr m) (ppChar '.'))
279 PprInterface -> pp_local_name s p
280 PprForUser -> pp_local_name s p
283 pp_debug = ppBeside pp_name (pp_occur_name s p)
286 PprShowAll -> ppBesides [pp_debug, pp_exp e] -- (ppr sty loc)
288 PprUnfolding -> pp_debug
291 pp_exp NotExported = ppNil
292 pp_exp ExportAll = ppPStr SLIT("/EXP(..)")
293 pp_exp ExportAbs = ppPStr SLIT("/EXP")
295 -- little utility gizmos...
296 pp_occur_name, pp_local_name :: FAST_STRING -> Provenance -> Pretty
298 pp_occur_name s (OtherPrelude o) | s /= o = ppBesides [ppChar '{', ppPStr o, ppChar '}']
299 pp_occur_name s (OtherModule o ms)| s /= o = ppBesides [ppChar '{', ppPStr o, ppChar '}']
300 -- ToDo: print the "informant modules"?
301 pp_occur_name _ _ = ppNil
303 pp_local_name s (OtherPrelude o) | s /= o = ppPStr o
304 pp_local_name s (OtherModule o ms)| s /= o = ppPStr o
305 pp_local_name s _ = ppPStr s