Name.lhs 11.3 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
14
	mkInternalName, mkSystemName, 
	mkSystemNameEncoded, mkSystemTvNameEncoded, mkFCallName,
15
	mkIPName,
16
	mkExternalName, mkKnownKeyExternalName, mkWiredInName,
17

18
	nameUnique, setNameUnique,
19
	nameOccName, nameModule, nameModule_maybe,
20
21
	setNameOcc, setNameModuleAndLoc, 
	hashName, externaliseName, localiseName,
sof's avatar
sof committed
22

23
	nameSrcLoc, eqNameByOcc,
24

25
	isSystemName, isInternalName, isExternalName,
26
	isTyVarName, isDllName, isWiredInName,
27
	nameIsLocalOrFrom, isHomePackageName,
28
	
29
30
	-- Class NamedThing and overloaded friends
	NamedThing(..),
31
	getSrcLoc, getOccString
32
    ) where
33

34
35
#include "HsVersions.h"

36
import OccName		-- All of it
37
import Module		( Module, ModuleName, moduleName, mkVanillaModule, isHomeModule )
38
import CmdLineOpts	( opt_Static )
39
import SrcLoc		( noSrcLoc, isWiredInLoc, wiredInSrcLoc, SrcLoc )
40
import Unique		( Unique, Uniquable(..), getKey, pprUnique )
41
import FastTypes
42
import Outputable
43
44
\end{code}

45
46
%************************************************************************
%*									*
47
\subsection[Name-datatype]{The @Name@ datatype, and name construction}
48
49
%*									*
%************************************************************************
50
 
51
\begin{code}
52
53
data Name = Name {
		n_sort :: NameSort,	-- What sort of name it is
54
		n_occ  :: !OccName,	-- Its occurrence name
55
		n_uniq :: Unique,
56
		n_loc  :: !SrcLoc	-- Definition site
57
58
	    }

59
60
61
62
-- 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.

63
data NameSort
64
  = External Module	-- (a) TyCon, Class, their derived Ids, dfun Id
65
66
67
			-- (b) Imported Id
			-- (c) Top-level Id in the original source, even if
			--	locally defined
68

69
  | Internal		-- A user-defined Id or TyVar
70
71
72
73
			-- defined in the module being compiled

  | System		-- A system-defined Id or TyVar.  Typically the
			-- OccName is very uninformative (like 's')
74
75
\end{code}

76
77
Notes about the NameSorts:

78
79
1.  Initially, top-level Ids (including locally-defined ones) get External names, 
    and all other local Ids get Internal names
80

81
2.  Things with a External name are given C static labels, so they finally
82
83
    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
84
    must be made @External@ first.
85

86
87
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
88
89

4.  A System Name differs in the following ways:
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	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.

\begin{code}
nameUnique		:: Name -> Unique
nameOccName		:: Name -> OccName 
nameModule		:: Name -> Module
nameSrcLoc		:: Name -> SrcLoc

nameUnique  name = n_uniq name
nameOccName name = n_occ  name
nameSrcLoc  name = n_loc  name
\end{code}

\begin{code}
111
112
113
114
115
nameIsLocalOrFrom :: Module -> Name -> Bool
isInternalName	  :: Name -> Bool
isExternalName	  :: Name -> Bool
isSystemName	  :: Name -> Bool
isHomePackageName :: Name -> Bool
116
117
118
isWiredInName	  :: Name -> Bool

isWiredInName name = isWiredInLoc (n_loc name)
119

120
isExternalName (Name {n_sort = External _}) = True
121
122
123
124
125
126
127
isExternalName other	                    = False

nameModule (Name { n_sort = External mod }) = mod
nameModule name				    = pprPanic "nameModule" (ppr name)

nameModule_maybe (Name { n_sort = External mod }) = Just mod
nameModule_maybe name				  = Nothing
128

129
isInternalName name = not (isExternalName name)
130

131
132
nameIsLocalOrFrom from (Name {n_sort = External mod}) = mod == from
nameIsLocalOrFrom from other			      = True
133

134
135
isHomePackageName (Name {n_sort = External mod}) = isHomeModule mod
isHomePackageName other			         = True 	-- Internal and system names
136
137
138
139
140
141

isDllName :: Name -> Bool	-- Does this name refer to something in a different DLL?
isDllName nm = not opt_Static && not (isHomePackageName nm)

isTyVarName :: Name -> Bool
isTyVarName name = isTvOcc (nameOccName name)
142
143
144

isSystemName (Name {n_sort = System}) = True
isSystemName other		      = False
145
146
147
148
149
150
151
152
153
154
155
156

eqNameByOcc :: Name -> Name -> Bool
-- Compare using the strings, not the unique
-- See notes with HsCore.eq_ufVar
eqNameByOcc (Name {n_sort = sort1, n_occ = occ1})
	    (Name {n_sort = sort2, n_occ = occ2})
  = sort1 `eq_sort` sort2 && occ1 == occ2
  where
    eq_sort (External m1) (External m2) = moduleName m1 == moduleName m2
    eq_sort (External _)  _		= False
    eq_sort _            (External _)   = False
    eq_sort _		 _		= True
157
158
159
160
161
162
163
164
\end{code}


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

166
\begin{code}
167
168
mkInternalName :: Unique -> OccName -> SrcLoc -> Name
mkInternalName uniq occ loc = Name { n_uniq = uniq, n_sort = Internal, n_occ = occ, n_loc = loc }
169
170
171
172
173
174
175
176
	-- 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)
177

178
179
mkExternalName :: Unique -> Module -> OccName -> SrcLoc -> Name
mkExternalName uniq mod occ loc = Name { n_uniq = uniq, n_sort = External mod,
180
				       n_occ = occ, n_loc = loc }
181

182
183
184
mkKnownKeyExternalName :: ModuleName -> OccName -> Unique -> Name
mkKnownKeyExternalName mod occ uniq
  = mkExternalName uniq (mkVanillaModule mod) occ noSrcLoc
185

186
mkWiredInName :: Module -> OccName -> Unique -> Name
187
mkWiredInName mod occ uniq = mkExternalName uniq mod occ wiredInSrcLoc
188

189
mkSystemName :: Unique -> UserFS -> Name
190
mkSystemName uniq fs = Name { n_uniq = uniq, n_sort = System, 
191
192
193
194
195
196
197
198
199
200
201
202
203
			      n_occ = mkVarOcc fs, n_loc = noSrcLoc }

-- Use this version when the string is already encoded.  Avoids duplicating
-- the string each time a new name is created.
mkSystemNameEncoded :: Unique -> EncodedFS -> Name
mkSystemNameEncoded uniq fs = Name { n_uniq = uniq, n_sort = System, 
			             n_occ = mkSysOccFS varName fs, 
				     n_loc = noSrcLoc }

mkSystemTvNameEncoded :: Unique -> EncodedFS -> Name
mkSystemTvNameEncoded uniq fs = Name { n_uniq = uniq, n_sort = System, 
			               n_occ = mkSysOccFS tvName fs, 
				       n_loc = noSrcLoc }
204

205
mkFCallName :: Unique -> EncodedString -> Name
206
	-- The encoded string completely describes the ccall
207
mkFCallName uniq str =  Name { n_uniq = uniq, n_sort = Internal, 
208
			       n_occ = mkFCallOcc str, n_loc = noSrcLoc }
209

210
211
212
mkIPName :: Unique -> OccName -> Name
mkIPName uniq occ
  = Name { n_uniq = uniq,
213
	   n_sort = Internal,
214
	   n_occ  = occ,
215
	   n_loc = noSrcLoc }
216
217
218
\end{code}

\begin{code}
219
220
221
-- 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.
222
setNameUnique name uniq = name {n_uniq = uniq}
223
224

setNameOcc :: Name -> OccName -> Name
225
226
setNameOcc name occ = name {n_occ = occ}

227
228
externaliseName :: Name -> Module -> Name
externaliseName n mod = n { n_sort = External mod }
229
230
				
localiseName :: Name -> Name
231
localiseName n = n { n_sort = Internal }
232
				
233
234
setNameModuleAndLoc :: Name -> Module -> SrcLoc -> Name
setNameModuleAndLoc name mod loc = name {n_sort = set (n_sort name), n_loc = loc}
235
		       where
236
			 set (External _) = External mod
sof's avatar
sof committed
237
238
239
\end{code}


240
241
242
243
244
245
246
%************************************************************************
%*									*
\subsection{Predicates and selectors}
%*									*
%************************************************************************

\begin{code}
247
hashName :: Name -> Int
248
hashName name = iBox (getKey (nameUnique name))
249
250
\end{code}

251

252
253
254
255
256
257
258
%************************************************************************
%*									*
\subsection[Name-instances]{Instance declarations}
%*									*
%************************************************************************

\begin{code}
259
cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
260
261
262
263
\end{code}

\begin{code}
instance Eq Name where
264
265
    a == b = case (a `compare` b) of { EQ -> True;  _ -> False }
    a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
266
267

instance Ord Name where
268
269
    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 }
270
271
272
    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
273

274
instance Uniquable Name where
275
    getUnique = nameUnique
276

277
instance NamedThing Name where
278
    getName n = n
279
280
281
\end{code}


282
283
284
285
286
%************************************************************************
%*									*
\subsection{Pretty printing}
%*									*
%************************************************************************
287
288
289

\begin{code}
instance Outputable Name where
290
	-- When printing interfaces, all Internals have been given nice print-names
291
292
    ppr name = pprName name

293
294
295
instance OutputableBndr Name where
    pprBndr _ name = pprName name

296
pprName name@(Name {n_sort = sort, n_uniq = uniq, n_occ = occ})
297
  = getPprStyle $ \ sty ->
298
    case sort of
299
300
301
      External mod -> pprExternal sty name uniq mod occ
      System       -> pprSystem sty uniq occ
      Internal     -> pprInternal sty uniq occ
302

303
pprExternal sty name uniq mod occ
304
  | codeStyle sty        = ppr (moduleName mod) <> char '_' <> pprOccName occ
305

306
  | debugStyle sty       = ppr (moduleName mod) <> dot <> pprOccName occ <> 
307
			    text "{-" <> pprUnique uniq <> text "-}"
308

309
310
  | unqualStyle sty name = pprOccName occ
  | otherwise		 = ppr (moduleName mod) <> dot <> pprOccName occ
311

312
pprInternal sty uniq occ
313
314
  | codeStyle sty  = pprUnique uniq
  | debugStyle sty = pprOccName occ <> 
315
		     text "{-" <> pprUnique uniq <> text "-}"
316
  | otherwise      = pprOccName occ	-- User style
317

318
319
-- Like Internal, except that we only omit the unique in Iface style
pprSystem sty uniq occ
320
321
  | codeStyle sty  = pprUnique uniq
  | otherwise	   = pprOccName occ <> char '_' <> pprUnique uniq
322
323
324
				-- If the tidy phase hasn't run, the OccName
				-- is unlikely to be informative (like 's'),
				-- so print the unique
325
\end{code}
326

327
328
329
330
331
332
333
334
%************************************************************************
%*									*
\subsection{Overloaded functions related to Names}
%*									*
%************************************************************************

\begin{code}
class NamedThing a where
335
    getOccName :: a -> OccName
336
337
338
    getName    :: a -> Name

    getOccName n = nameOccName (getName n)	-- Default method
339
340
341
342
\end{code}

\begin{code}
getSrcLoc	    :: NamedThing a => a -> SrcLoc
343
getOccString	    :: NamedThing a => a -> String
344

345
getSrcLoc	    = nameSrcLoc	   . getName
346
getOccString 	    = occNameString	   . getOccName
347
348
\end{code}