Opened 9 months ago

Last modified 8 months ago

#15896 patch task

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

Reported by: alanz Owned by: alanz
Priority: normal Milestone: 8.8.1
Component: GHC API Version: 8.6.2
Keywords: Cc: simonpj
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D5330
Wiki Page: ApiNameMap


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.

Change History (4)

comment:1 Changed 9 months ago by alanz

Owner: set to alanz

comment:2 Changed 9 months ago by alanz

Status: newpatch

comment:3 Changed 9 months ago by alanz

Wiki Page: ApiNameMap

comment:4 Changed 8 months ago by bgamari

Note: See TracTickets for help on using tickets.