Name.lhs 14.8 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 36
	getSrcLoc, getSrcSpan, getOccString,
 	pprInfixName, pprPrefixName
37
    ) where
38

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

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

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

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

69 70 71 72
-- 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.

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

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

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

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

91 92
Notes about the NameSorts:

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

96
2.  Things with a External name are given C static labels, so they finally
97 98
    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
99
    must be made @External@ first.
100

101 102
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
103 104

4.  A System Name differs in the following ways:
105 106 107 108 109 110 111 112 113
	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.

114 115 116 117 118 119 120 121
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.

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

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

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

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

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

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

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

157
isInternalName name = not (isExternalName name)
158

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

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

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

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

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


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

186
\begin{code}
187
mkInternalName :: Unique -> OccName -> SrcSpan -> Name
188
mkInternalName uniq occ loc = Name { n_uniq = getKeyFastInt uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
189 190 191 192 193 194 195 196
	-- 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)
197

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

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

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

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

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

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

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

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

\begin{code}
239 240 241
-- 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.
242
setNameUnique :: Name -> Unique -> Name
243
setNameUnique name uniq = name {n_uniq = getKeyFastInt uniq}
244

245 246 247 248 249 250
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 }
251

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


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

\begin{code}
264 265 266 267 268
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
269 270
\end{code}

271

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

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

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

instance Ord Name where
289 290
    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 }
291 292 293
    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
294

295
instance Uniquable Name where
296
    getUnique = nameUnique
297

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

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

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

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

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

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

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

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

twanvl's avatar
twanvl committed
342
pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
343
pprExternal sty uniq mod occ is_wired is_builtin
Simon Marlow's avatar
Simon Marlow committed
344
  | codeStyle sty        = ppr mod <> char '_' <> ppr_z_occ_name occ
345 346 347
	-- 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
348
 | debugStyle sty       = ppr mod <> dot <> ppr_occ_name occ
Ian Lynagh's avatar
Ian Lynagh committed
349
		<> braces (hsep [if is_wired then ptext (sLit "(w)") else empty,
Simon Marlow's avatar
Simon Marlow committed
350 351
				 pprNameSpaceBrief (occNameSpace occ), 
		 		 pprUnique uniq])
352
  | BuiltInSyntax <- is_builtin  = ppr_occ_name occ
353
	-- never qualify builtin syntax
354 355 356 357 358
  | 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
359
  | otherwise		          = ppr_occ_name occ
360
  where qual_name = qualName sty mod occ
361

twanvl's avatar
twanvl committed
362
pprInternal :: PprStyle -> Unique -> OccName -> SDoc
363
pprInternal sty uniq occ
364
  | codeStyle sty  = pprUnique uniq
365
  | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ), 
366
				 		       pprUnique uniq])
367 368 369
  | 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.
370
  | otherwise      = ppr_occ_name occ	-- User style
371

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

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

-- 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
390
ppr_z_occ_name :: OccName -> SDoc
391
ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
392

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

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

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

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

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

423
getSrcLoc	    = nameSrcLoc	   . getName
424
getSrcSpan	    = nameSrcSpan	   . getName
425
getOccString 	    = occNameString	   . getOccName
426 427 428 429 430 431

pprInfixName, pprPrefixName :: (Outputable a, NamedThing a) => a -> SDoc
-- See Outputable.pprPrefixVar, pprInfixVar; 
-- add parens or back-quotes as appropriate
pprInfixName  n = pprInfixVar  (isSymOcc (getOccName n)) (ppr n)
pprPrefixName n = pprPrefixVar (isSymOcc (getOccName n)) (ppr n)
432 433
\end{code}