Name.lhs 14.5 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6
7
8
%
\section[Name]{@Name@: to transmit name info from renamer to typechecker}

\begin{code}
module Name (
9
10
	-- Re-export the OccName stuff
	module OccName,
11

12
13
	-- The Name type
	Name,					-- Abstract
14
	BuiltInSyntax(..), 
15
	mkInternalName, mkSystemName,
16
	mkSystemVarName, mkSysTvName, 
17
	mkFCallName, mkIPName,
18
        mkTickBoxOpName,
19
	mkExternalName, mkWiredInName,
20

21
	nameUnique, setNameUnique,
22
	nameOccName, nameModule, nameModule_maybe,
23
	tidyNameOcc, 
24
	hashName, localiseName,
sof's avatar
sof committed
25

26
	nameSrcLoc, nameSrcSpan, pprNameLoc,
27

28
	isSystemName, isInternalName, isExternalName,
29
	isTyVarName, isTyConName, isWiredInName, isBuiltInSyntax,
30
	wiredInNameTyThing_maybe, 
31
	nameIsLocalOrFrom,
32
	
33
34
	-- Class NamedThing and overloaded friends
	NamedThing(..),
35
	getSrcLoc, getSrcSpan, getOccString
36
    ) where
37

38
39
import {-# SOURCE #-} TypeRep( TyThing )

Simon Marlow's avatar
Simon Marlow committed
40
41
42
43
44
import OccName
import Module
import SrcLoc
import Unique
import Maybes
45
import Binary
46
import FastTypes
Simon Marlow's avatar
Simon Marlow committed
47
import FastString
48
import Outputable
49

Simon Marlow's avatar
Simon Marlow committed
50
import Data.Array
51
52
\end{code}

53
54
%************************************************************************
%*									*
55
\subsection[Name-datatype]{The @Name@ datatype, and name construction}
56
57
%*									*
%************************************************************************
58
 
59
\begin{code}
60
61
data Name = Name {
		n_sort :: NameSort,	-- What sort of name it is
62
		n_occ  :: !OccName,	-- Its occurrence name
63
64
		n_uniq :: FastInt,      -- UNPACK doesn't work, recursive type
--(note later when changing Int# -> FastInt: is that still true about UNPACK?)
65
		n_loc  :: !SrcSpan	-- Definition site
66
67
	    }

68
69
70
71
-- NOTE: we make the n_loc field strict to eliminate some potential
-- (and real!) space leaks, due to the fact that we don't look at
-- the SrcLoc in a Name all that often.

72
data NameSort
73
  = External Module
74
 
75
  | WiredIn Module TyThing BuiltInSyntax
76
	-- A variant of External, for wired-in things
77

78
  | Internal		-- A user-defined Id or TyVar
79
80
81
82
			-- defined in the module being compiled

  | System		-- A system-defined Id or TyVar.  Typically the
			-- OccName is very uninformative (like 's')
83
84
85
86
87

data BuiltInSyntax = BuiltInSyntax | UserSyntax
-- BuiltInSyntax is for things like (:), [], tuples etc, 
-- which have special syntactic forms.  They aren't "in scope"
-- as such.
88
89
\end{code}

90
91
Notes about the NameSorts:

92
93
1.  Initially, top-level Ids (including locally-defined ones) get External names, 
    and all other local Ids get Internal names
94

95
2.  Things with a External name are given C static labels, so they finally
96
97
    appear in the .o file's symbol table.  They appear in the symbol table
    in the form M.n.  If originally-local things have this property they
98
    must be made @External@ first.
99

100
101
3.  In the tidy-core phase, a External that is not visible to an importer
    is changed to Internal, and a Internal that is visible is changed to External
102
103

4.  A System Name differs in the following ways:
104
105
106
107
108
109
110
111
112
	a) has unique attached when printing dumps
	b) unifier eliminates sys tyvars in favour of user provs where possible

    Before anything gets printed in interface files or output code, it's
    fed through a 'tidy' processor, which zaps the OccNames to have
    unique names; and converts all sys-locals to user locals
    If any desugarer sys-locals have survived that far, they get changed to
    "ds1", "ds2", etc.

113
114
115
116
117
118
119
120
Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])

Wired-in thing  => The thing (Id, TyCon) is fully known to the compiler, 
		   not read from an interface file. 
		   E.g. Bool, True, Int, Float, and many others

All built-in syntax is for wired-in things.

121
122
123
124
125
\begin{code}
nameUnique		:: Name -> Unique
nameOccName		:: Name -> OccName 
nameModule		:: Name -> Module
nameSrcLoc		:: Name -> SrcLoc
126
nameSrcSpan		:: Name -> SrcSpan
127

128
nameUnique  name = mkUniqueGrimily (iBox (n_uniq name))
129
nameOccName name = n_occ  name
130
131
nameSrcLoc  name = srcSpanStart (n_loc name)
nameSrcSpan name = n_loc  name
132
133
134
\end{code}

\begin{code}
135
136
137
138
nameIsLocalOrFrom :: Module -> Name -> Bool
isInternalName	  :: Name -> Bool
isExternalName	  :: Name -> Bool
isSystemName	  :: Name -> Bool
139
140
isWiredInName	  :: Name -> Bool

141
isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
twanvl's avatar
twanvl committed
142
isWiredInName _                               = False
143

144
wiredInNameTyThing_maybe :: Name -> Maybe TyThing
145
wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
twanvl's avatar
twanvl committed
146
wiredInNameTyThing_maybe _                                   = Nothing
147

twanvl's avatar
twanvl committed
148
isBuiltInSyntax :: Name -> Bool
149
isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
twanvl's avatar
twanvl committed
150
isBuiltInSyntax _                                           = False
151

152
153
isExternalName (Name {n_sort = External _})    = True
isExternalName (Name {n_sort = WiredIn _ _ _}) = True
twanvl's avatar
twanvl committed
154
isExternalName _                               = False
155

156
isInternalName name = not (isExternalName name)
157

158
nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
twanvl's avatar
twanvl committed
159
nameModule_maybe :: Name -> Maybe Module
160
161
nameModule_maybe (Name { n_sort = External mod})    = Just mod
nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
twanvl's avatar
twanvl committed
162
nameModule_maybe _                                  = Nothing
163

164
165
166
167
nameIsLocalOrFrom from name
  | isExternalName name = from == nameModule name
  | otherwise		= True

168
169
isTyVarName :: Name -> Bool
isTyVarName name = isTvOcc (nameOccName name)
170

171
172
173
isTyConName :: Name -> Bool
isTyConName name = isTcOcc (nameOccName name)

174
isSystemName (Name {n_sort = System}) = True
twanvl's avatar
twanvl committed
175
isSystemName _                        = False
176
177
178
179
180
181
182
183
\end{code}


%************************************************************************
%*									*
\subsection{Making names}
%*									*
%************************************************************************
184

185
\begin{code}
186
mkInternalName :: Unique -> OccName -> SrcSpan -> Name
187
mkInternalName uniq occ loc = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
188
189
190
191
192
193
194
195
	-- NB: You might worry that after lots of huffing and
	-- puffing we might end up with two local names with distinct
	-- uniques, but the same OccName.  Indeed we can, but that's ok
	--	* the insides of the compiler don't care: they use the Unique
	--	* when printing for -ddump-xxx you can switch on -dppr-debug to get the
	--	  uniques if you get confused
	--	* for interface files we tidyCore first, which puts the uniques
	--	  into the print name (see setNameVisibility below)
196

197
mkExternalName :: Unique -> Module -> OccName -> SrcSpan -> Name
198
mkExternalName uniq mod occ loc 
199
  = Name { n_uniq = getKeyFastInt uniq, n_sort = External mod,
200
           n_occ = occ, n_loc = loc }
201

202
203
204
mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax
        -> Name
mkWiredInName mod occ uniq thing built_in
205
  = Name { n_uniq = getKeyFastInt uniq,
206
	   n_sort = WiredIn mod thing built_in,
207
	   n_occ = occ, n_loc = wiredInSrcSpan }
208

209
mkSystemName :: Unique -> OccName -> Name
210
mkSystemName uniq occ = Name { n_uniq = getKeyFastInt uniq, n_sort = System, 
211
			       n_occ = occ, n_loc = noSrcSpan }
212

213
214
mkSystemVarName :: Unique -> FastString -> Name
mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
215

216
217
mkSysTvName :: Unique -> FastString -> Name
mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs) 
218

219
mkFCallName :: Unique -> String -> Name
220
	-- The encoded string completely describes the ccall
221
mkFCallName uniq str =  Name { n_uniq = getKeyFastInt uniq, n_sort = Internal, 
222
			       n_occ = mkVarOcc str, n_loc = noSrcSpan }
223

224
225
mkTickBoxOpName :: Unique -> String -> Name
mkTickBoxOpName uniq str 
226
   = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal, 
227
	    n_occ = mkVarOcc str, n_loc = noSrcSpan }
228

229
230
mkIPName :: Unique -> OccName -> Name
mkIPName uniq occ
231
  = Name { n_uniq = getKeyFastInt uniq,
232
	   n_sort = Internal,
233
	   n_occ  = occ,
234
	   n_loc = noSrcSpan }
235
236
237
\end{code}

\begin{code}
238
239
240
-- When we renumber/rename things, we need to be
-- able to change a Name's Unique to match the cached
-- one in the thing it's the name of.  If you know what I mean.
241
setNameUnique :: Name -> Unique -> Name
242
setNameUnique name uniq = name {n_uniq = getKeyFastInt uniq}
243

244
245
246
247
248
249
tidyNameOcc :: Name -> OccName -> Name
-- We set the OccName of a Name when tidying
-- In doing so, we change System --> Internal, so that when we print
-- it we don't get the unique by default.  It's tidy now!
tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
tidyNameOcc name 			    occ = name { n_occ = occ }
250

251
localiseName :: Name -> Name
252
localiseName n = n { n_sort = Internal }
sof's avatar
sof committed
253
254
255
\end{code}


256
257
258
259
260
261
262
%************************************************************************
%*									*
\subsection{Predicates and selectors}
%*									*
%************************************************************************

\begin{code}
263
264
265
266
267
hashName :: Name -> Int		-- ToDo: should really be Word
hashName name = getKey (nameUnique name) + 1
	-- The +1 avoids keys with lots of zeros in the ls bits, which 
	-- interacts badly with the cheap and cheerful multiplication in
	-- hashExpr
268
269
\end{code}

270

271
272
273
274
275
276
277
%************************************************************************
%*									*
\subsection[Name-instances]{Instance declarations}
%*									*
%************************************************************************

\begin{code}
twanvl's avatar
twanvl committed
278
cmpName :: Name -> Name -> Ordering
279
cmpName n1 n2 = iBox (n_uniq n1) `compare` iBox (n_uniq n2)
280
281
282
283
\end{code}

\begin{code}
instance Eq Name where
284
285
    a == b = case (a `compare` b) of { EQ -> True;  _ -> False }
    a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
286
287

instance Ord Name where
288
289
    a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
    a <	 b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
290
291
292
    a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
    a >	 b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
    compare a b = cmpName a b
293

294
instance Uniquable Name where
295
    getUnique = nameUnique
296

297
instance NamedThing Name where
298
    getName n = n
299
300
\end{code}

301
302
303
304
305
306
307
308
%************************************************************************
%*									*
\subsection{Binary}
%*									*
%************************************************************************

\begin{code}
instance Binary Name where
309
310
311
   put_ bh name =
      case getUserData bh of 
        UserData{ ud_put_name = put_name } -> put_name bh name
312
313
314
315
316

   get bh = do
        i <- get bh
        return $! (ud_symtab (getUserData bh) ! i)
\end{code}
317

318
319
320
321
322
%************************************************************************
%*									*
\subsection{Pretty printing}
%*									*
%************************************************************************
323
324
325

\begin{code}
instance Outputable Name where
326
327
    ppr name = pprName name

328
329
330
instance OutputableBndr Name where
    pprBndr _ name = pprName name

twanvl's avatar
twanvl committed
331
332
pprName :: Name -> SDoc
pprName (Name {n_sort = sort, n_uniq = u, n_occ = occ})
333
  = getPprStyle $ \ sty ->
334
    case sort of
335
336
      WiredIn mod _ builtin   -> pprExternal sty uniq mod occ True  builtin
      External mod  	      -> pprExternal sty uniq mod occ False UserSyntax
337
338
      System   		      -> pprSystem sty uniq occ
      Internal    	      -> pprInternal sty uniq occ
339
  where uniq = mkUniqueGrimily (iBox u)
340

twanvl's avatar
twanvl committed
341
pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
342
pprExternal sty uniq mod occ is_wired is_builtin
Simon Marlow's avatar
Simon Marlow committed
343
  | codeStyle sty        = ppr mod <> char '_' <> ppr_z_occ_name occ
344
345
346
	-- In code style, always qualify
	-- ToDo: maybe we could print all wired-in things unqualified
	-- 	 in code style, to reduce symbol table bloat?
Simon Marlow's avatar
Simon Marlow committed
347
 | debugStyle sty       = ppr mod <> dot <> ppr_occ_name occ
Ian Lynagh's avatar
Ian Lynagh committed
348
		<> braces (hsep [if is_wired then ptext (sLit "(w)") else empty,
Simon Marlow's avatar
Simon Marlow committed
349
350
				 pprNameSpaceBrief (occNameSpace occ), 
		 		 pprUnique uniq])
351
  | BuiltInSyntax <- is_builtin  = ppr_occ_name occ
352
	-- never qualify builtin syntax
353
354
355
356
357
  | NameQual modname <- qual_name = ppr modname <> dot <> ppr_occ_name occ
        -- see HscTypes.mkPrintUnqualified and Outputable.QualifyName:
  | NameNotInScope1 <- qual_name  = ppr mod <> dot <> ppr_occ_name occ
  | NameNotInScope2 <- qual_name  = ppr (modulePackageId mod) <> char ':' <>
                                    ppr (moduleName mod) <> dot <> ppr_occ_name occ
Simon Marlow's avatar
Simon Marlow committed
358
  | otherwise		          = ppr_occ_name occ
359
  where qual_name = qualName sty mod occ
360

twanvl's avatar
twanvl committed
361
pprInternal :: PprStyle -> Unique -> OccName -> SDoc
362
pprInternal sty uniq occ
363
  | codeStyle sty  = pprUnique uniq
364
  | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ), 
365
				 		       pprUnique uniq])
366
367
368
  | dumpStyle sty  = ppr_occ_name occ <> char '_' <> pprUnique uniq
			-- For debug dumps, we're not necessarily dumping
			-- tidied code, so we need to print the uniques.
369
  | otherwise      = ppr_occ_name occ	-- User style
370

371
-- Like Internal, except that we only omit the unique in Iface style
twanvl's avatar
twanvl committed
372
pprSystem :: PprStyle -> Unique -> OccName -> SDoc
373
pprSystem sty uniq occ
374
  | codeStyle sty  = pprUnique uniq
375
  | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
376
		     <> braces (pprNameSpaceBrief (occNameSpace occ))
377
  | otherwise	   = ppr_occ_name occ <> char '_' <> pprUnique uniq
378
379
380
				-- If the tidy phase hasn't run, the OccName
				-- is unlikely to be informative (like 's'),
				-- so print the unique
381

twanvl's avatar
twanvl committed
382
ppr_occ_name :: OccName -> SDoc
383
ppr_occ_name occ = ftext (occNameFS occ)
384
385
	-- Don't use pprOccName; instead, just print the string of the OccName; 
	-- we print the namespace in the debug stuff above
386
387
388

-- In code style, we Z-encode the strings.  The results of Z-encoding each FastString are
-- cached behind the scenes in the FastString implementation.
twanvl's avatar
twanvl committed
389
ppr_z_occ_name :: OccName -> SDoc
390
ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
391

392
393
-- Prints (if mod information is available) "Defined at <loc>" or 
--  "Defined in <mod>" information for a Name.
394
395
396
pprNameLoc :: Name -> SDoc
pprNameLoc name
  | isGoodSrcSpan loc = pprDefnLoc loc
397
  | isInternalName name || isSystemName name 
Ian Lynagh's avatar
Ian Lynagh committed
398
399
                      = ptext (sLit "<no location info>")
  | otherwise         = ptext (sLit "Defined in ") <> ppr (nameModule name)
400
  where loc = nameSrcSpan name
401
\end{code}
402

403
404
405
406
407
408
409
410
%************************************************************************
%*									*
\subsection{Overloaded functions related to Names}
%*									*
%************************************************************************

\begin{code}
class NamedThing a where
411
    getOccName :: a -> OccName
412
413
414
    getName    :: a -> Name

    getOccName n = nameOccName (getName n)	-- Default method
415
416
417
418
\end{code}

\begin{code}
getSrcLoc	    :: NamedThing a => a -> SrcLoc
419
getSrcSpan	    :: NamedThing a => a -> SrcSpan
420
getOccString	    :: NamedThing a => a -> String
421

422
getSrcLoc	    = nameSrcLoc	   . getName
423
getSrcSpan	    = nameSrcSpan	   . getName
424
getOccString 	    = occNameString	   . getOccName
425
426
\end{code}