Skip to content

GHC API: add function to allow looking up Name for Located RdrName

A tool writer using the GHC API to do a source level change must use the ParsedSource as it most closely represents the original source code, and has API Annotations.

But the ParsedSource only has RdrNames in it, and many useful transformations need information from the renamer, the simplest example being renaming a variable in the presence of name shadowing.

So we need a way to look up the Name corresponding to each RdrName in the ParsedSource.

GHC does have a GlobalRdrEnv which is a mapping from OccName to a list of elements, each of which has the Name. But the OccName is only unique wrt its spelling, and so cannot distinguish the name shadowing case.

Fortuitously (!), every RdrName in the ParsedSource is Located, and hence has a unique SrcSpan associated with it.

So, given a TypecheckedModule, it is possible to construct a map from the SrcSpan of each Located RdrName to the corresponding Name from the RenamedSource (or TypecheckedSource for ambiguous record selectors).

Introduce this as part of the GHC API.

Note: this function has existed in HaRe for some time, and is the reason each RdrName in the ParsedSource is Located.

Trac metadata
Trac field Value
Version 8.6.2
Type Task
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component GHC API
Test case
Differential revisions
BlockedBy
Related
Blocking
CC simonpj
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information