3

I was wondering how do you determine the ModuleName of the file you've just loaded in ghc using the API.

If you look at the Documentation of the API they always assume you know the module name before you load the file in.

I have tried top sorting the dependency graph and picking the last module there, but that doesn't seem to always return the file that was loaded in.

The way I have been able to do it previously was to modify the GHC source code to store this information in the HscEnv.

It would be nice if the call to load would return it, But there surely must be a way to do it already since GHCi knows this when it loads a file.

Phyx
  • 2,697
  • 1
  • 20
  • 35
  • Perhaps a new function is needed ```loadTargetSet``` which returns ```m (Maybe [ModuleName])``` where the position of each corresponds to what's returned from ```getTargets```. – Sledge Aug 24 '21 at 17:08

1 Answers1

1

The haskell-src-exts package has a parseFile function with type parseFile :: FilePath -> IO (ParseResult Module). The Module type has a field for the module's name.

Daniel
  • 26,899
  • 12
  • 60
  • 88
  • 1
    That's quite an overhead I would imagine. Having to parse the module twice, since I still need ghc to parse, desugar and typecheck. This is also done quite often. I could dig around and see how ghci does it, but I figured someone must know. :/ the last time I looked at it was for ghc 7.0.1, I would imagine the compilation pipeline looks quite different now... – Phyx Aug 20 '12 at 19:46