Name.lhs 13.2 KB
Newer Older
1
%
2
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 4 5 6 7
%
\section[Name]{@Name@: to transmit name info from renamer to typechecker}

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

11 12
	-- The Name type
	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,
sof's avatar
sof committed
23

24
	nameSrcLoc, nameParent, nameParent_maybe, isImplicitName, 
25

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

36 37
#include "HsVersions.h"

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

40
import OccName		-- All of it
Simon Marlow's avatar
Simon Marlow committed
41
import Module		( Module )
42
import SrcLoc		( noSrcLoc, wiredInSrcLoc, SrcLoc )
43 44
import Unique		( Unique, Uniquable(..), getKey, pprUnique,
                          mkUniqueGrimily, getKey# )
45
import Maybes		( orElse, isJust )
46
import FastString	( FastString, zEncodeFS )
47
import Outputable
48 49

import GLAEXTS          ( Int#, Int(..) )
50 51
\end{code}

52 53
%************************************************************************
%*									*
54
\subsection[Name-datatype]{The @Name@ datatype, and name construction}
55 56
%*									*
%************************************************************************
57
 
58
\begin{code}
59 60
data Name = Name {
		n_sort :: NameSort,	-- What sort of name it is
61
		n_occ  :: !OccName,	-- Its occurrence name
62
		n_uniq :: Int#,         -- UNPACK doesn't work, recursive type
63
		n_loc  :: !SrcLoc	-- Definition site
64 65
	    }

66 67 68 69
-- 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.

70
data NameSort
71 72 73 74 75
  = External Module (Maybe Name)
	-- (Just parent) => this Name is a subordinate name of 'parent'
	-- e.g. data constructor of a data type, method of a class
	-- Nothing => not a subordinate
 
76
  | WiredIn Module (Maybe Name) 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 127
\begin{code}
nameUnique		:: Name -> Unique
nameOccName		:: Name -> OccName 
nameModule		:: Name -> Module
nameSrcLoc		:: Name -> SrcLoc

128
nameUnique  name = mkUniqueGrimily (I# (n_uniq name))
129 130 131 132 133
nameOccName name = n_occ  name
nameSrcLoc  name = n_loc  name
\end{code}

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

140 141
isWiredInName (Name {n_sort = WiredIn _ _ _ _}) = True
isWiredInName other			        = False
142

143
wiredInNameTyThing_maybe :: Name -> Maybe TyThing
144 145
wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ _ thing _}) = Just thing
wiredInNameTyThing_maybe other				       = Nothing
146

147 148 149 150 151 152
isBuiltInSyntax (Name {n_sort = WiredIn _ _ _ BuiltInSyntax}) = True
isBuiltInSyntax other			 		      = False

isExternalName (Name {n_sort = External _ _})    = True
isExternalName (Name {n_sort = WiredIn _ _ _ _}) = True
isExternalName other	                         = False
153

154
isInternalName name = not (isExternalName name)
155

156
nameParent_maybe :: Name -> Maybe Name
157 158 159
nameParent_maybe (Name {n_sort = External _ p})    = p
nameParent_maybe (Name {n_sort = WiredIn _ p _ _}) = p
nameParent_maybe other	                           = Nothing
160 161 162 163 164 165

nameParent :: Name -> Name
nameParent name = case nameParent_maybe name of
			Just parent -> parent
			Nothing     -> name

166 167
isImplicitName :: Name -> Bool
-- An Implicit Name is one has a parent; that is, one whose definition
168
-- derives from the parent thing
169 170
isImplicitName name = isJust (nameParent_maybe name)

171
nameModule name = nameModule_maybe name `orElse` pprPanic "nameModule" (ppr name)
172 173 174
nameModule_maybe (Name { n_sort = External mod _})    = Just mod
nameModule_maybe (Name { n_sort = WiredIn mod _ _ _}) = Just mod
nameModule_maybe name				      = Nothing
175

176 177 178 179
nameIsLocalOrFrom from name
  | isExternalName name = from == nameModule name
  | otherwise		= True

180 181
isTyVarName :: Name -> Bool
isTyVarName name = isTvOcc (nameOccName name)
182 183 184 185 186 187 188 189 190 191 192

isSystemName (Name {n_sort = System}) = True
isSystemName other		      = False
\end{code}


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

194
\begin{code}
195
mkInternalName :: Unique -> OccName -> SrcLoc -> Name
196
mkInternalName uniq occ loc = Name { n_uniq = getKey# uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
197 198 199 200 201 202 203 204
	-- 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)
205

206 207
mkExternalName :: Unique -> Module -> OccName -> Maybe Name -> SrcLoc -> Name
mkExternalName uniq mod occ mb_parent loc 
208
  = Name { n_uniq = getKey# uniq, n_sort = External mod mb_parent,
209
           n_occ = occ, n_loc = loc }
210

211 212 213
mkWiredInName :: Module -> OccName -> Unique 
	      -> Maybe Name -> TyThing -> BuiltInSyntax -> Name
mkWiredInName mod occ uniq mb_parent thing built_in
214
  = Name { n_uniq = getKey# uniq,
215
	   n_sort = WiredIn mod mb_parent thing built_in,
216
	   n_occ = occ, n_loc = wiredInSrcLoc }
217

218
mkSystemName :: Unique -> OccName -> Name
219
mkSystemName uniq occ = Name { n_uniq = getKey# uniq, n_sort = System, 
220 221
			       n_occ = occ, n_loc = noSrcLoc }

222 223
mkSystemVarName :: Unique -> FastString -> Name
mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
224

225 226
mkSysTvName :: Unique -> FastString -> Name
mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs) 
227

228
mkFCallName :: Unique -> String -> Name
229
	-- The encoded string completely describes the ccall
230
mkFCallName uniq str =  Name { n_uniq = getKey# uniq, n_sort = Internal, 
231
			       n_occ = mkVarOcc str, n_loc = noSrcLoc }
232

233 234
mkIPName :: Unique -> OccName -> Name
mkIPName uniq occ
235
  = Name { n_uniq = getKey# uniq,
236
	   n_sort = Internal,
237
	   n_occ  = occ,
238
	   n_loc = noSrcLoc }
239 240 241
\end{code}

\begin{code}
242 243 244
-- 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.
245 246
setNameUnique :: Name -> Unique -> Name
setNameUnique name uniq = name {n_uniq = getKey# uniq}
247

248 249 250 251 252 253
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 }
254

255
localiseName :: Name -> Name
256
localiseName n = n { n_sort = Internal }
sof's avatar
sof committed
257 258 259
\end{code}


260 261 262 263 264 265 266
%************************************************************************
%*									*
\subsection{Predicates and selectors}
%*									*
%************************************************************************

\begin{code}
267
hashName :: Name -> Int
268
hashName name = getKey (nameUnique name)
269 270
\end{code}

271

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

\begin{code}
279
cmpName n1 n2 = I# (n_uniq n1) `compare` I# (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 301
\end{code}


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

\begin{code}
instance Outputable Name where
310 311
    ppr name = pprName name

312 313 314
instance OutputableBndr Name where
    pprBndr _ name = pprName name

315
pprName name@(Name {n_sort = sort, n_uniq = u#, n_occ = occ})
316
  = getPprStyle $ \ sty ->
317
    case sort of
318 319 320 321
      WiredIn mod _ _ builtin -> pprExternal sty uniq mod occ True  builtin
      External mod _  	      -> pprExternal sty uniq mod occ False UserSyntax
      System   		      -> pprSystem sty uniq occ
      Internal    	      -> pprInternal sty uniq occ
322
  where uniq = mkUniqueGrimily (I# u#)
323

324
pprExternal sty uniq mod occ is_wired is_builtin
Simon Marlow's avatar
Simon Marlow committed
325
  | codeStyle sty        = ppr mod <> char '_' <> ppr_z_occ_name occ
326 327 328
	-- 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
329 330 331 332
 | debugStyle sty       = ppr mod <> dot <> ppr_occ_name occ
		<> braces (hsep [if is_wired then ptext SLIT("(w)") else empty,
				 pprNameSpaceBrief (occNameSpace occ), 
		 		 pprUnique uniq])
333
  | BuiltInSyntax <- is_builtin  = ppr_occ_name occ
334
	-- never qualify builtin syntax
Simon Marlow's avatar
Simon Marlow committed
335 336 337
  | Just mod <- qualName sty mod occ = ppr mod <> dot <> ppr_occ_name occ
        -- the PrintUnqualified tells us how to qualify this Name, if at all
  | otherwise		          = ppr_occ_name occ
338

339
pprInternal sty uniq occ
340
  | codeStyle sty  = pprUnique uniq
341
  | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ), 
342
				 		       pprUnique uniq])
343 344 345
  | 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.
346
  | otherwise      = ppr_occ_name occ	-- User style
347

348 349
-- Like Internal, except that we only omit the unique in Iface style
pprSystem sty uniq occ
350
  | codeStyle sty  = pprUnique uniq
351
  | debugStyle sty = ppr_occ_name occ <> char '_' <> pprUnique uniq
352
		     <> braces (pprNameSpaceBrief (occNameSpace occ))
353
  | otherwise	   = ppr_occ_name occ <> char '_' <> pprUnique uniq
354 355 356
				-- If the tidy phase hasn't run, the OccName
				-- is unlikely to be informative (like 's'),
				-- so print the unique
357

358
ppr_occ_name occ = ftext (occNameFS occ)
359 360
	-- Don't use pprOccName; instead, just print the string of the OccName; 
	-- we print the namespace in the debug stuff above
361 362 363 364

-- In code style, we Z-encode the strings.  The results of Z-encoding each FastString are
-- cached behind the scenes in the FastString implementation.
ppr_z_occ_name occ = ftext (zEncodeFS (occNameFS occ))
365
\end{code}
366

367 368 369 370 371 372 373 374
%************************************************************************
%*									*
\subsection{Overloaded functions related to Names}
%*									*
%************************************************************************

\begin{code}
class NamedThing a where
375
    getOccName :: a -> OccName
376 377 378
    getName    :: a -> Name

    getOccName n = nameOccName (getName n)	-- Default method
379 380 381 382
\end{code}

\begin{code}
getSrcLoc	    :: NamedThing a => a -> SrcLoc
383
getOccString	    :: NamedThing a => a -> String
384

385
getSrcLoc	    = nameSrcLoc	   . getName
386
getOccString 	    = occNameString	   . getOccName
387 388
\end{code}