RdrName.lhs 19 KB
Newer Older
1
%
2
3
4
5
6
7
8
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%

\section[RdrName]{@RdrName@}

\begin{code}
module RdrName (
9
	RdrName(..),	-- Constructors exported only to BinIface
10
11

	-- Construction
12
	mkRdrUnqual, mkRdrQual, 
13
	mkUnqual, mkVarUnqual, mkQual, mkOrig,
14
	nameRdrName, getRdrName, 
15
	mkDerivedRdrName, 
16
17

	-- Destruction
Simon Marlow's avatar
Simon Marlow committed
18
19
	rdrNameOcc, setRdrNameSpace,
	isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual, 
20
	isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
21
22

	-- Printing;	instance Outputable RdrName
23
24
25
26
27
28
29

	-- LocalRdrEnv
	LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv,
	lookupLocalRdrEnv, elemLocalRdrEnv,

	-- GlobalRdrEnv
	GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv, 
30
31
	lookupGlobalRdrEnv, extendGlobalRdrEnv,
	pprGlobalRdrEnv, globalRdrEnvElts,
32
	lookupGRE_RdrName, lookupGRE_Name, hideSomeUnquals,
33
34

	-- GlobalRdrElt, Provenance, ImportSpec
35
36
37
38
	GlobalRdrElt(..), isLocalGRE, unQualOK, 
	Provenance(..), pprNameProvenance,
	ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..), 
	importSpecLoc, importSpecModule
39
40
41
42
  ) where 

#include "HsVersions.h"

43
import OccName
Simon Marlow's avatar
Simon Marlow committed
44
import Module   ( ModuleName, mkModuleNameFS, Module, moduleName )
45
import Name	( Name, NamedThing(getName), nameModule, nameParent_maybe,
46
		  nameOccName, isExternalName, nameSrcLoc )
47
import Maybes	( mapCatMaybes )
48
import SrcLoc	( isGoodSrcLoc, isGoodSrcSpan, srcLocSpan, SrcSpan )
49
import FastString ( FastString )
50
51
52
53
54
55
56
57
58
59
60
import Outputable
import Util	( thenCmp )
\end{code}

%************************************************************************
%*									*
\subsection{The main data type}
%*									*
%************************************************************************

\begin{code}
61
62
63
64
data RdrName 
  = Unqual OccName
	-- Used for ordinary, unqualified occurrences 

Simon Marlow's avatar
Simon Marlow committed
65
  | Qual ModuleName OccName
66
	-- A qualified name written by the user in 
67
	--  *source* code.  The module isn't necessarily 
68
69
70
	-- the module where the thing is defined; 
	-- just the one from which it is imported

71
  | Orig Module OccName
72
73
74
75
76
77
78
79
80
	-- An original name; the module is the *defining* module.
	-- This is used when GHC generates code that will be fed
	-- into the renamer (e.g. from deriving clauses), but where
	-- we want to say "Use Prelude.map dammit".  
 
  | Exact Name
	-- We know exactly the Name. This is used 
	--  (a) when the parser parses built-in syntax like "[]" 
	--	and "(,)", but wants a RdrName from it
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
81
	--  (b) by Template Haskell, when TH has generated a unique name
82
83
84
85
86
87
88
89
90
91
92
\end{code}


%************************************************************************
%*									*
\subsection{Simple functions}
%*									*
%************************************************************************

\begin{code}
rdrNameOcc :: RdrName -> OccName
93
94
95
96
97
98
99
100
101
102
103
104
105
106
rdrNameOcc (Qual _ occ) = occ
rdrNameOcc (Unqual occ) = occ
rdrNameOcc (Orig _ occ) = occ
rdrNameOcc (Exact name) = nameOccName name

setRdrNameSpace :: RdrName -> NameSpace -> RdrName
-- This rather gruesome function is used mainly by the parser
-- When parsing		data T a = T | T1 Int
-- we parse the data constructors as *types* because of parser ambiguities,
-- so then we need to change the *type constr* to a *data constr*
--
-- The original-name case *can* occur when parsing
-- 		data [] a = [] | a : [a]
-- For the orig-name case we return an unqualified name.
107
108
109
setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
110
setRdrNameSpace (Exact n)    ns = Orig (nameModule n)
111
				       (setOccNameSpace ns (nameOccName n))
112
113
114
115
116
\end{code}

\begin{code}
	-- These two are the basic constructors
mkRdrUnqual :: OccName -> RdrName
117
mkRdrUnqual occ = Unqual occ
118

Simon Marlow's avatar
Simon Marlow committed
119
mkRdrQual :: ModuleName -> OccName -> RdrName
120
mkRdrQual mod occ = Qual mod occ
121

122
mkOrig :: Module -> OccName -> RdrName
123
mkOrig mod occ = Orig mod occ
124

125
126
127
---------------
mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
mkDerivedRdrName parent mk_occ
128
  = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
129

130
---------------
131
132
	-- These two are used when parsing source files
	-- They do encode the module and occurrence names
133
134
mkUnqual :: NameSpace -> FastString -> RdrName
mkUnqual sp n = Unqual (mkOccNameFS sp n)
135

136
137
mkVarUnqual :: FastString -> RdrName
mkVarUnqual n = Unqual (mkVarOccFS n)
138

139
mkQual :: NameSpace -> (FastString, FastString) -> RdrName
Simon Marlow's avatar
Simon Marlow committed
140
mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
141
142

getRdrName :: NamedThing thing => thing -> RdrName
143
getRdrName name = nameRdrName (getName name)
144

145
146
nameRdrName :: Name -> RdrName
nameRdrName name = Exact name
147
148
149
-- Keep the Name even for Internal names, so that the
-- unique is still there for debug printing, particularly
-- of Types (which are converted to IfaceTypes before printing)
150

151
152
nukeExact :: Name -> RdrName
nukeExact n 
153
  | isExternalName n = Orig (nameModule n) (nameOccName n)
154
  | otherwise	     = Unqual (nameOccName n)
155
156
157
\end{code}

\begin{code}
158
159
160
isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
isRdrTyVar   rn = isTvOcc   (rdrNameOcc rn)
isRdrTc      rn = isTcOcc   (rdrNameOcc rn)
161

162
163
164
165
isSrcRdrName (Unqual _) = True
isSrcRdrName (Qual _ _) = True
isSrcRdrName _		= False

166
167
isUnqual (Unqual _) = True
isUnqual other	    = False
168

169
170
isQual (Qual _ _) = True
isQual _	  = False
171

Simon Marlow's avatar
Simon Marlow committed
172
173
174
isQual_maybe (Qual m n) = Just (m,n)
isQual_maybe _	        = Nothing

175
176
177
isOrig (Orig _ _) = True
isOrig _	  = False

178
179
180
isOrig_maybe (Orig m n) = Just (m,n)
isOrig_maybe _		= Nothing

181
182
183
184
isExact (Exact _) = True
isExact other	= False

isExact_maybe (Exact n) = Just n
185
isExact_maybe other	= Nothing
186
187
188
189
190
191
192
193
194
195
196
\end{code}


%************************************************************************
%*									*
\subsection{Instances}
%*									*
%************************************************************************

\begin{code}
instance Outputable RdrName where
197
    ppr (Exact name)   = ppr name
198
199
200
    ppr (Unqual occ)   = ppr occ
    ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
    ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
201

202
203
204
205
206
instance OutputableBndr RdrName where
    pprBndr _ n 
	| isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
	| otherwise		 = ppr n

207
instance Eq RdrName where
208
209
210
211
212
213
214
215
216
    (Exact n1) 	  == (Exact n2)    = n1==n2
	-- Convert exact to orig
    (Exact n1) 	  == r2@(Orig _ _) = nukeExact n1 == r2
    r1@(Orig _ _) == (Exact n2)    = r1 == nukeExact n2

    (Orig m1 o1)  == (Orig m2 o2)  = m1==m2 && o1==o2
    (Qual m1 o1)  == (Qual m2 o2)  = m1==m2 && o1==o2
    (Unqual o1)   == (Unqual o2)   = o1==o2
    r1 == r2 = False
217
218
219
220
221
222
223

instance Ord RdrName where
    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 }
    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  }

224
225
226
227
228
229
230
231
232
233
234
235
	-- Exact < Unqual < Qual < Orig
	-- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig 
	-- 	before comparing so that Prelude.map == the exact Prelude.map, but 
	--	that meant that we reported duplicates when renaming bindings 
	--	generated by Template Haskell; e.g 
	--	do { n1 <- newName "foo"; n2 <- newName "foo"; 
	--	     <decl involving n1,n2> }
	--	I think we can do without this conversion
    compare (Exact n1) (Exact n2) = n1 `compare` n2
    compare (Exact n1) n2	  = LT

    compare (Unqual _)   (Exact _)    = GT
236
237
    compare (Unqual o1)  (Unqual  o2) = o1 `compare` o2
    compare (Unqual _)   _ 	      = LT
238
239
240
241

    compare (Qual _ _)   (Exact _)    = GT
    compare (Qual _ _)   (Unqual _)   = GT
    compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
242
    compare (Qual _ _)   (Orig _ _)   = LT
243
244
245

    compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
    compare (Orig _ _)   _	      = GT
246
247
248
249
\end{code}



250
251
%************************************************************************
%*									*
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
			LocalRdrEnv
%*									*
%************************************************************************

A LocalRdrEnv is used for local bindings (let, where, lambda, case)
It is keyed by OccName, because we never use it for qualified names.

\begin{code}
type LocalRdrEnv = OccEnv Name

emptyLocalRdrEnv = emptyOccEnv

extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
extendLocalRdrEnv env names
  = extendOccEnvList env [(nameOccName n, n) | n <- names]

lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
269
270
271
lookupLocalRdrEnv env (Exact name) = Just name
lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
lookupLocalRdrEnv env other	   = Nothing
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
elemLocalRdrEnv rdr_name env 
  | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
  | otherwise	      = False
\end{code}


%************************************************************************
%*									*
			GlobalRdrEnv
%*									*
%************************************************************************

\begin{code}
type GlobalRdrEnv = OccEnv [GlobalRdrElt]
	-- Keyed by OccName; when looking up a qualified name
	-- we look up the OccName part, and then check the Provenance
	-- to see if the appropriate qualification is valid.  This
	-- saves routinely doubling the size of the env by adding both
	-- qualified and unqualified names to the domain.
	--
	-- The list in the range is reqd because there may be name clashes
	-- These only get reported on lookup, not on construction

	-- INVARIANT: All the members of the list have distinct 
	--	      gre_name fields; that is, no duplicate Names

emptyGlobalRdrEnv = emptyOccEnv

globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
globalRdrEnvElts env = foldOccEnv (++) [] env

data GlobalRdrElt 
  = GRE { gre_name   :: Name,
307
	  gre_prov   :: Provenance	-- Why it's in scope
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    }

instance Outputable GlobalRdrElt where
  ppr gre = ppr name <+> pp_parent (nameParent_maybe name)
		<+> parens (pprNameProvenance gre)
	  where
	    name = gre_name gre
	    pp_parent (Just p) = brackets (text "parent:" <+> ppr p)
	    pp_parent Nothing  = empty

pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
pprGlobalRdrEnv env
  = vcat (map pp (occEnvElts env))
  where
    pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+> 
	      vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
		   | gre <- gres]
\end{code}

\begin{code}
lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
lookupGlobalRdrEnv env rdr_name = case lookupOccEnv env rdr_name of
					Nothing   -> []
					Just gres -> gres

333
334
335
336
337
338
extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
  where
    occ = nameOccName (gre_name gre)
    add gres _ = gre:gres

339
340
lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
lookupGRE_RdrName rdr_name env
341
342
343
  = case lookupOccEnv env (rdrNameOcc rdr_name) of
	Nothing   -> []
	Just gres -> pickGREs rdr_name gres
344
345
346
347
348
349
350

lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
lookupGRE_Name env name
  = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
	    gre_name gre == name ]


351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
-- Take a list of GREs which have the right OccName
-- Pick those GREs that are suitable for this RdrName
-- And for those, keep only only the Provenances that are suitable
-- 
-- Consider
--	 module A ( f ) where
--	 import qualified Foo( f )
--	 import Baz( f )
--	 f = undefined
-- Let's suppose that Foo.f and Baz.f are the same entity really.
-- The export of f is ambiguous because it's in scope from the local def
-- and the import.  The lookup of (Unqual f) should return a GRE for
-- the locally-defined f, and a GRE for the imported f, with a *single* 
-- provenance, namely the one for Baz(f).
pickGREs rdr_name gres
  = mapCatMaybes pick gres
  where
Simon Marlow's avatar
Simon Marlow committed
369
370
    rdr_is_unqual = isUnqual rdr_name
    rdr_is_qual   = isQual_maybe rdr_name
371
372

    pick :: GlobalRdrElt -> Maybe GlobalRdrElt
373
    pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) 	-- Local def
Simon Marlow's avatar
Simon Marlow committed
374
375
376
377
	| rdr_is_unqual		 		= Just gre
	| Just (mod,_) <- rdr_is_qual, 
	  mod == moduleName (nameModule n)	= Just gre
	| otherwise 				= Nothing
378
    pick gre@(GRE {gre_prov = Imported [is]})	-- Single import (efficiency)
Simon Marlow's avatar
Simon Marlow committed
379
380
381
382
383
	| rdr_is_unqual,
	  not (is_qual (is_decl is))		= Just gre
	| Just (mod,_) <- rdr_is_qual, 
	  mod == is_as (is_decl is)		= Just gre
	| otherwise     			= Nothing
384
385
386
387
    pick gre@(GRE {gre_prov = Imported is})	-- Multiple import
	| null filtered_is = Nothing
	| otherwise	   = Just (gre {gre_prov = Imported filtered_is})
	where
Simon Marlow's avatar
Simon Marlow committed
388
389
390
391
392
393
	  filtered_is | rdr_is_unqual
   		      = filter (not . is_qual    . is_decl) is
		      | Just (mod,_) <- rdr_is_qual 
	              = filter ((== mod) . is_as . is_decl) is
		      | otherwise
		      = []
394

395
isLocalGRE :: GlobalRdrElt -> Bool
396
397
isLocalGRE (GRE {gre_prov = LocalDef}) = True
isLocalGRE other    		       = False
398
399
400

unQualOK :: GlobalRdrElt -> Bool
-- An unqualifed version of this thing is in scope
401
unQualOK (GRE {gre_prov = LocalDef})    = True
402
unQualOK (GRE {gre_prov = Imported is}) = not (all (is_qual . is_decl) is)
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2

mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
mkGlobalRdrEnv gres
  = foldr add emptyGlobalRdrEnv gres
  where
    add gre env = extendOccEnv_C (foldr insertGRE) env 
				 (nameOccName (gre_name gre)) 
				 [gre]

insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
insertGRE new_g [] = [new_g]
insertGRE new_g (old_g : old_gs)
	| gre_name new_g == gre_name old_g
	= new_g `plusGRE` old_g : old_gs
	| otherwise
	= old_g : insertGRE new_g old_gs

plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
-- Used when the gre_name fields match
plusGRE g1 g2
426
427
  = GRE { gre_name = gre_name g1,
	  gre_prov = gre_prov g1 `plusProv` gre_prov g2 }
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
-- Hide any unqualified bindings for the specified OccNames
-- This is used in TH, when renaming a declaration bracket
--	[d| foo = ... |]
-- We want unqualified 'foo' in "..." to mean this foo, not
-- the one from the enclosing module.  But the *qualified* name
-- from the enclosing moudule must certainly still be avaialable
-- 	Seems like 5 times as much work as it deserves!
hideSomeUnquals rdr_env occs
  = foldr hide rdr_env occs
  where
    hide occ env 
	| Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
	| otherwise			    = env
    qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
	= GRE { gre_name = name, gre_prov = Imported [imp_spec] }
	where	-- Local defs get transfomed to (fake) imported things
	  mod = moduleName (nameModule name)
	  imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
	  decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod, 
				    is_qual = True, 
				    is_dloc = srcLocSpan (nameSrcLoc name) }

    qual_gre gre@(GRE { gre_prov = Imported specs })
	= gre { gre_prov = Imported (map qual_spec specs) }

    qual_spec spec@(ImpSpec { is_decl = decl_spec })
  	= spec { is_decl = decl_spec { is_qual = True } }
457
458
459
460
461
462
\end{code}


%************************************************************************
%*									*
			Provenance
463
464
465
%*									*
%************************************************************************

466
The "provenance" of something says how it came to be in scope.
467
It's quite elaborate so that we can give accurate unused-name warnings.
468

469
\begin{code}
470
471
data Provenance
  = LocalDef		-- Defined locally
472
  | Imported 		-- Imported
473
474
	[ImportSpec]	-- INVARIANT: non-empty

475
476
477
478
479
480
481
data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
			    is_item ::  ImpItemSpec }
		deriving( Eq, Ord )

data ImpDeclSpec	-- Describes a particular import declaration
			-- Shared among all the Provenaces for that decl
  = ImpDeclSpec {
Simon Marlow's avatar
Simon Marlow committed
482
	is_mod      :: ModuleName, -- 'import Muggle'
483
484
				-- Note the Muggle may well not be 
				-- the defining module for this thing!
Simon Marlow's avatar
Simon Marlow committed
485
486
487
                                -- TODO: either should be Module, or there
                                -- should be a Maybe PackageId here too.
	is_as       :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
488
	is_qual     :: Bool,	-- True <=> qualified (only)
489
490
491
492
493
494
495
496
497
498
	is_dloc     :: SrcSpan	-- Location of import declaration
    }

data ImpItemSpec  -- Describes import info a particular Name
  = ImpAll		-- The import had no import list, 
			-- or  had a hiding list

  | ImpSome {		-- The import had an import list
	is_explicit :: Bool,
	is_iloc     :: SrcSpan	-- Location of the import item
499
500
501
    }
	-- The is_explicit field is True iff the thing was named 
	-- *explicitly* in the import specs rather 
502
503
504
505
506
507
508
509
510
	-- than being imported as part of a "..." group 
	-- e.g.		import C( T(..) )
	-- Here the constructors of T are not named explicitly; 
	-- only T is named explicitly.

importSpecLoc :: ImportSpec -> SrcSpan
importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
importSpecLoc (ImpSpec _    item)   = is_iloc item

Simon Marlow's avatar
Simon Marlow committed
511
importSpecModule :: ImportSpec -> ModuleName
512
importSpecModule is = is_mod (is_decl is)
513

514
-- Note [Comparing provenance]
515
516
517
518
519
-- Comparison of provenance is just used for grouping 
-- error messages (in RnEnv.warnUnusedBinds)
instance Eq Provenance where
  p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False

520
521
522
523
instance Eq ImpDeclSpec where
  p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False

instance Eq ImpItemSpec where
524
525
526
  p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False

instance Ord Provenance where
527
528
529
   compare LocalDef      LocalDef   	 = EQ
   compare LocalDef      (Imported _) 	 = LT
   compare (Imported _ ) LocalDef	 = GT
530
531
   compare (Imported is1) (Imported is2) = compare (head is1) 
	{- See Note [Comparing provenance] -}	   (head is2)
532

533
instance Ord ImpDeclSpec where
534
   compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp` 
535
536
537
538
		     (is_dloc is1 `compare` is_dloc is2)

instance Ord ImpItemSpec where
   compare is1 is2 = is_iloc is1 `compare` is_iloc is2
539
\end{code}
540

541
\begin{code}
542
543
plusProv :: Provenance -> Provenance -> Provenance
-- Choose LocalDef over Imported
544
-- There is an obscure bug lurking here; in the presence
545
546
547
548
-- of recursive modules, something can be imported *and* locally
-- defined, and one might refer to it with a qualified name from
-- the import -- but I'm going to ignore that because it makes
-- the isLocalGRE predicate so much nicer this way
549
550
551
plusProv LocalDef 	 LocalDef        = panic "plusProv"
plusProv LocalDef	 p2		 = LocalDef
plusProv p1 		 LocalDef 	 = LocalDef
552
plusProv (Imported is1)  (Imported is2)  = Imported (is1++is2)
553
554

pprNameProvenance :: GlobalRdrElt -> SDoc
555
-- Print out the place where the name was imported
556
pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
557
  = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
558
559
560
561
pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
  = case whys of
	(why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
	[] -> panic "pprNameProvenance"
562

563
564
565
566
567
-- If we know the exact definition point (which we may do with GHCi)
-- then show that too.  But not if it's just "imported from X".
ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
	     | otherwise	= empty

568
instance Outputable ImportSpec where
569
570
571
572
573
574
   ppr imp_spec
     = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec) 
	<+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
				 else empty
     where
       loc = importSpecLoc imp_spec
575
\end{code}