Introduce map from RdrName to Name for GHC API
Needs ReviewPublic

Authored by alanz on Nov 12 2018, 12:34 PM.

Details

Reviewers
bgamari
Trac Issues
#15896
Summary

Tools need to work with the ParsedSource as a accurate representation
of the compiled source, but sometimes need access to the actual Names
used from the renaming phase.

Introduce a function that initialises a NameMap from a TypechedModule,
for use by GHC API consumers.

Test Plan

./validate

alanz created this revision.Nov 12 2018, 12:34 PM

I have not added any tests (yet) as I first want an idea of whether this is acceptable or not. It came about
from discussions on IRC with people needing this kind of function, and not knowing that it already existed
in HaRe.

This is a direct port of the one used there.

To me it makes sense for it to be here, because

a) it continues making GHC more useful for tool writers
b) it is a claim on functionality, which can then evolve as things like TTG are merged.

alanz added a subscriber: simonpj.Tue, Nov 13, 8:36 AM

Comment on ghc-devs from @simonpj

Interesting. The GlobalRdrEnv is such a mapping.

I'm not sure what the goal is here. Would it be worth a ticket and/or wiki page to explain the problem you are trying to solve?

alanz added a comment.Tue, Nov 13, 1:43 PM

The GlobalRdrEnv is such a mapping.

I'm not sure what the goal is here. Would it be worth a ticket and/or wiki page to explain the problem you are trying to solve?

Before I do that, I would like some clarity on GlobalRdrEnv.

I have just done some digging into the GHC code, and it seems that

  1. Every individual FastString has a unique id in it
  1. An OccName contains a FastString for the actual name, and so is unique, even for the same spelling, if it comes from a different part of the source, and so has a different FastString.
  1. The function lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt] can thus be used to retrieve the Name (and provenance) of any RdrName, by using its OccName

In other words the function in this patch is unnecessary.

And the fact that every RdrName in the source is Located is not actually necessary.

Every individual FastString has a unique id in it

Yes

An OccName contains a FastString for the actual name, and so is unique, even for the same spelling, if it comes from a different part of the source, and so has a different FastString.

No: FastStrings are hash-consed, so same string = same unique id.

  1. The function lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt] can thus be used to retrieve the Name (and provenance) of any RdrName, by using its OccName

The GlobalRdrEnv is only for top-level names, not ones in nested bindings.

And you get back a *list* of GlobalRdrElt because many Names may have the same OccName.

alanz updated the Trac tickets for this revision.Wed, Nov 14, 3:02 PM
bgamari added inline comments.Sat, Nov 24, 12:33 PM
compiler/main/GHC.hs
131

Can we place this in a subheading? This module is already quite hard to navigate.

Frankly, I've sometimes wondered whether we should split it up into a hierarchy.

alanz updated this revision to Diff 18871.Sun, Nov 25, 4:50 AM
alanz marked an inline comment as done.

Update for @bgamari comment, add test

Alan, could you write a Note or wiki page or something explaining

  • The problem you are trying to solve
  • How you plan to solve it
  • The Trac ticket that tracks progress and design choices

I'm a bit lost with this patch, I'm afraid.

In D5330#148230, @alanz wrote:

Aha. That is helpful. But I still do not understand the problem precisely enough.

I have added some questions on the wiki page. Rather than answer me directly, would you consider refining the specification of the problem you are trying to solve so that the questions are answered? THanks