Generalise Package Support
[ghc-hetmet.git] / compiler / basicTypes / Name.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
5
6 \begin{code}
7 module Name (
8         -- Re-export the OccName stuff
9         module OccName,
10
11         -- The Name type
12         Name,                                   -- Abstract
13         BuiltInSyntax(..), 
14         mkInternalName, mkSystemName,
15         mkSystemVarName, mkSysTvName, 
16         mkFCallName, mkIPName,
17         mkExternalName, mkWiredInName,
18
19         nameUnique, setNameUnique,
20         nameOccName, nameModule, nameModule_maybe,
21         tidyNameOcc, 
22         hashName, localiseName,
23
24         nameSrcLoc, nameParent, nameParent_maybe, isImplicitName, 
25
26         isSystemName, isInternalName, isExternalName,
27         isTyVarName, isWiredInName, isBuiltInSyntax,
28         wiredInNameTyThing_maybe, 
29         nameIsLocalOrFrom,
30         
31         -- Class NamedThing and overloaded friends
32         NamedThing(..),
33         getSrcLoc, getOccString
34     ) where
35
36 #include "HsVersions.h"
37
38 import {-# SOURCE #-} TypeRep( TyThing )
39
40 import OccName          -- All of it
41 import Module           ( Module )
42 import SrcLoc           ( noSrcLoc, wiredInSrcLoc, SrcLoc )
43 import Unique           ( Unique, Uniquable(..), getKey, pprUnique )
44 import Maybes           ( orElse, isJust )
45 import FastString       ( FastString, zEncodeFS )
46 import Outputable
47 \end{code}
48
49 %************************************************************************
50 %*                                                                      *
51 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
52 %*                                                                      *
53 %************************************************************************
54  
55 \begin{code}
56 data Name = Name {
57                 n_sort :: NameSort,     -- What sort of name it is
58                 n_occ  :: !OccName,     -- Its occurrence name
59                 n_uniq :: {-# UNPACK #-} !Unique,
60                 n_loc  :: !SrcLoc       -- Definition site
61             }
62
63 -- NOTE: we make the n_loc field strict to eliminate some potential
64 -- (and real!) space leaks, due to the fact that we don't look at
65 -- the SrcLoc in a Name all that often.
66
67 data NameSort
68   = External Module (Maybe Name)
69         -- (Just parent) => this Name is a subordinate name of 'parent'
70         -- e.g. data constructor of a data type, method of a class
71         -- Nothing => not a subordinate
72  
73   | WiredIn Module (Maybe Name) TyThing BuiltInSyntax
74         -- A variant of External, for wired-in things
75
76   | Internal            -- A user-defined Id or TyVar
77                         -- defined in the module being compiled
78
79   | System              -- A system-defined Id or TyVar.  Typically the
80                         -- OccName is very uninformative (like 's')
81
82 data BuiltInSyntax = BuiltInSyntax | UserSyntax
83 -- BuiltInSyntax is for things like (:), [], tuples etc, 
84 -- which have special syntactic forms.  They aren't "in scope"
85 -- as such.
86 \end{code}
87
88 Notes about the NameSorts:
89
90 1.  Initially, top-level Ids (including locally-defined ones) get External names, 
91     and all other local Ids get Internal names
92
93 2.  Things with a External name are given C static labels, so they finally
94     appear in the .o file's symbol table.  They appear in the symbol table
95     in the form M.n.  If originally-local things have this property they
96     must be made @External@ first.
97
98 3.  In the tidy-core phase, a External that is not visible to an importer
99     is changed to Internal, and a Internal that is visible is changed to External
100
101 4.  A System Name differs in the following ways:
102         a) has unique attached when printing dumps
103         b) unifier eliminates sys tyvars in favour of user provs where possible
104
105     Before anything gets printed in interface files or output code, it's
106     fed through a 'tidy' processor, which zaps the OccNames to have
107     unique names; and converts all sys-locals to user locals
108     If any desugarer sys-locals have survived that far, they get changed to
109     "ds1", "ds2", etc.
110
111 Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])
112
113 Wired-in thing  => The thing (Id, TyCon) is fully known to the compiler, 
114                    not read from an interface file. 
115                    E.g. Bool, True, Int, Float, and many others
116
117 All built-in syntax is for wired-in things.
118
119 \begin{code}
120 nameUnique              :: Name -> Unique
121 nameOccName             :: Name -> OccName 
122 nameModule              :: Name -> Module
123 nameSrcLoc              :: Name -> SrcLoc
124
125 nameUnique  name = n_uniq name
126 nameOccName name = n_occ  name
127 nameSrcLoc  name = n_loc  name
128 \end{code}
129
130 \begin{code}
131 nameIsLocalOrFrom :: Module -> Name -> Bool
132 isInternalName    :: Name -> Bool
133 isExternalName    :: Name -> Bool
134 isSystemName      :: Name -> Bool
135 isWiredInName     :: Name -> Bool
136
137 isWiredInName (Name {n_sort = WiredIn _ _ _ _}) = True
138 isWiredInName other                             = False
139
140 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
141 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ _ thing _}) = Just thing
142 wiredInNameTyThing_maybe other                                 = Nothing
143
144 isBuiltInSyntax (Name {n_sort = WiredIn _ _ _ BuiltInSyntax}) = True
145 isBuiltInSyntax other                                         = False
146
147 isExternalName (Name {n_sort = External _ _})    = True
148 isExternalName (Name {n_sort = WiredIn _ _ _ _}) = True
149 isExternalName other                             = False
150
151 isInternalName name = not (isExternalName name)
152
153 nameParent_maybe :: Name -> Maybe Name
154 nameParent_maybe (Name {n_sort = External _ p})    = p
155 nameParent_maybe (Name {n_sort = WiredIn _ p _ _}) = p
156 nameParent_maybe other                             = Nothing
157
158 nameParent :: Name -> Name
159 nameParent name = case nameParent_maybe name of
160                         Just parent -> parent
161                         Nothing     -> name
162
163 isImplicitName :: Name -> Bool
164 -- An Implicit Name is one has a parent; that is, one whose definition
165 -- derives from the parent thing
166 isImplicitName name = isJust (nameParent_maybe name)
167
168 nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
169 nameModule_maybe (Name { n_sort = External mod _})    = Just mod
170 nameModule_maybe (Name { n_sort = WiredIn mod _ _ _}) = Just mod
171 nameModule_maybe name                                 = Nothing
172
173 nameIsLocalOrFrom from name
174   | isExternalName name = from == nameModule name
175   | otherwise           = True
176
177 isTyVarName :: Name -> Bool
178 isTyVarName name = isTvOcc (nameOccName name)
179
180 isSystemName (Name {n_sort = System}) = True
181 isSystemName other                    = False
182 \end{code}
183
184
185 %************************************************************************
186 %*                                                                      *
187 \subsection{Making names}
188 %*                                                                      *
189 %************************************************************************
190
191 \begin{code}
192 mkInternalName :: Unique -> OccName -> SrcLoc -> Name
193 mkInternalName uniq occ loc = Name { n_uniq = uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
194         -- NB: You might worry that after lots of huffing and
195         -- puffing we might end up with two local names with distinct
196         -- uniques, but the same OccName.  Indeed we can, but that's ok
197         --      * the insides of the compiler don't care: they use the Unique
198         --      * when printing for -ddump-xxx you can switch on -dppr-debug to get the
199         --        uniques if you get confused
200         --      * for interface files we tidyCore first, which puts the uniques
201         --        into the print name (see setNameVisibility below)
202
203 mkExternalName :: Unique -> Module -> OccName -> Maybe Name -> SrcLoc -> Name
204 mkExternalName uniq mod occ mb_parent loc 
205   = Name { n_uniq = uniq, n_sort = External mod mb_parent,
206            n_occ = occ, n_loc = loc }
207
208 mkWiredInName :: Module -> OccName -> Unique 
209               -> Maybe Name -> TyThing -> BuiltInSyntax -> Name
210 mkWiredInName mod occ uniq mb_parent thing built_in
211   = Name { n_uniq = uniq,
212            n_sort = WiredIn mod mb_parent thing built_in,
213            n_occ = occ, n_loc = wiredInSrcLoc }
214
215 mkSystemName :: Unique -> OccName -> Name
216 mkSystemName uniq occ = Name { n_uniq = uniq, n_sort = System, 
217                                n_occ = occ, n_loc = noSrcLoc }
218
219 mkSystemVarName :: Unique -> FastString -> Name
220 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
221
222 mkSysTvName :: Unique -> FastString -> Name
223 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs) 
224
225 mkFCallName :: Unique -> String -> Name
226         -- The encoded string completely describes the ccall
227 mkFCallName uniq str =  Name { n_uniq = uniq, n_sort = Internal, 
228                                n_occ = mkVarOcc str, n_loc = noSrcLoc }
229
230 mkIPName :: Unique -> OccName -> Name
231 mkIPName uniq occ
232   = Name { n_uniq = uniq,
233            n_sort = Internal,
234            n_occ  = occ,
235            n_loc = noSrcLoc }
236 \end{code}
237
238 \begin{code}
239 -- When we renumber/rename things, we need to be
240 -- able to change a Name's Unique to match the cached
241 -- one in the thing it's the name of.  If you know what I mean.
242 setNameUnique name uniq = name {n_uniq = uniq}
243
244 tidyNameOcc :: Name -> OccName -> Name
245 -- We set the OccName of a Name when tidying
246 -- In doing so, we change System --> Internal, so that when we print
247 -- it we don't get the unique by default.  It's tidy now!
248 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
249 tidyNameOcc name                            occ = name { n_occ = occ }
250
251 localiseName :: Name -> Name
252 localiseName n = n { n_sort = Internal }
253 \end{code}
254
255
256 %************************************************************************
257 %*                                                                      *
258 \subsection{Predicates and selectors}
259 %*                                                                      *
260 %************************************************************************
261
262 \begin{code}
263 hashName :: Name -> Int
264 hashName name = getKey (nameUnique name)
265 \end{code}
266
267
268 %************************************************************************
269 %*                                                                      *
270 \subsection[Name-instances]{Instance declarations}
271 %*                                                                      *
272 %************************************************************************
273
274 \begin{code}
275 cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
276 \end{code}
277
278 \begin{code}
279 instance Eq Name where
280     a == b = case (a `compare` b) of { EQ -> True;  _ -> False }
281     a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
282
283 instance Ord Name where
284     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
285     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
286     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
287     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
288     compare a b = cmpName a b
289
290 instance Uniquable Name where
291     getUnique = nameUnique
292
293 instance NamedThing Name where
294     getName n = n
295 \end{code}
296
297
298 %************************************************************************
299 %*                                                                      *
300 \subsection{Pretty printing}
301 %*                                                                      *
302 %************************************************************************
303
304 \begin{code}
305 instance Outputable Name where
306     ppr name = pprName name
307
308 instance OutputableBndr Name where
309     pprBndr _ name = pprName name
310
311 pprName name@(Name {n_sort = sort, n_uniq = uniq, n_occ = occ})
312   = getPprStyle $ \ sty ->
313     case sort of
314       WiredIn mod _ _ builtin -> pprExternal sty uniq mod occ True  builtin
315       External mod _          -> pprExternal sty uniq mod occ False UserSyntax
316       System                  -> pprSystem sty uniq occ
317       Internal                -> pprInternal sty uniq occ
318
319 pprExternal sty uniq mod occ is_wired is_builtin
320   | codeStyle sty        = ppr mod <> char '_' <> ppr_z_occ_name occ
321         -- In code style, always qualify
322         -- ToDo: maybe we could print all wired-in things unqualified
323         --       in code style, to reduce symbol table bloat?
324  | debugStyle sty       = ppr mod <> dot <> ppr_occ_name occ
325                 <> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
326                                  pprNameSpaceBrief (occNameSpace occ), 
327                                  pprUnique uniq])
328   | BuiltInSyntax <- is_builtin  = ppr_occ_name occ
329         -- never qualify builtin syntax
330   | Just mod <- qualName sty mod occ = ppr mod <> dot <> ppr_occ_name occ
331         -- the PrintUnqualified tells us how to qualify this Name, if at all
332   | otherwise                     = ppr_occ_name occ
333
334 pprInternal sty uniq occ
335   | codeStyle sty  = pprUnique uniq
336   | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ), 
337                                                        pprUnique uniq])
338   | dumpStyle sty  = ppr_occ_name occ <> char '_' <> pprUnique uniq
339                         -- For debug dumps, we're not necessarily dumping
340                         -- tidied code, so we need to print the uniques.
341   | otherwise      = ppr_occ_name occ   -- User style
342
343 -- Like Internal, except that we only omit the unique in Iface style
344 pprSystem sty uniq occ
345   | codeStyle sty  = pprUnique uniq
346   | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
347                      <> braces (pprNameSpaceBrief (occNameSpace occ))
348   | otherwise      = ppr_occ_name occ <> char '_' <> pprUnique uniq
349                                 -- If the tidy phase hasn't run, the OccName
350                                 -- is unlikely to be informative (like 's'),
351                                 -- so print the unique
352
353 ppr_occ_name occ = ftext (occNameFS occ)
354         -- Don't use pprOccName; instead, just print the string of the OccName; 
355         -- we print the namespace in the debug stuff above
356
357 -- In code style, we Z-encode the strings.  The results of Z-encoding each FastString are
358 -- cached behind the scenes in the FastString implementation.
359 ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
360 \end{code}
361
362 %************************************************************************
363 %*                                                                      *
364 \subsection{Overloaded functions related to Names}
365 %*                                                                      *
366 %************************************************************************
367
368 \begin{code}
369 class NamedThing a where
370     getOccName :: a -> OccName
371     getName    :: a -> Name
372
373     getOccName n = nameOccName (getName n)      -- Default method
374 \end{code}
375
376 \begin{code}
377 getSrcLoc           :: NamedThing a => a -> SrcLoc
378 getOccString        :: NamedThing a => a -> String
379
380 getSrcLoc           = nameSrcLoc           . getName
381 getOccString        = occNameString        . getOccName
382 \end{code}
383