Add 'packageDbModules' function to GHC API.
[ghc-hetmet.git] / compiler / main / GHC.hs
index 2aa7b47..ce3fe76 100644 (file)
@@ -78,6 +78,9 @@ module GHC (
        findGlobalAnns,
         mkPrintUnqualifiedForModule,
 
+        -- * Querying the environment
+        packageDbModules,
+
        -- * Printing
        PrintUnqualified, alwaysQualify,
 
@@ -283,6 +286,7 @@ import SysTools     ( initSysTools, cleanTempFiles, cleanTempFilesExcept,
 import Annotations
 import Module
 import LazyUniqFM
+import qualified UniqFM as UFM
 import UniqSet
 import Unique
 import FiniteMap
@@ -2428,6 +2432,23 @@ getGRE = withSession $ \hsc_env-> return $ ic_rn_gbl_env (hsc_IC hsc_env)
 #endif
 
 -- -----------------------------------------------------------------------------
+
+-- | Return all /external/ modules available in the package database.
+-- Modules from the current session (i.e., from the 'HomePackageTable') are
+-- not included.
+packageDbModules :: GhcMonad m =>
+                    Bool  -- ^ Only consider exposed packages.
+                 -> m [Module]
+packageDbModules only_exposed = do
+   dflags <- getSessionDynFlags
+   let pkgs = UFM.eltsUFM (pkgIdMap (pkgState dflags))
+   return $
+     [ mkModule pid modname | p <- pkgs
+                            , not only_exposed || exposed p
+                            , pid <- [mkPackageId (package p)]
+                            , modname <- exposedModules p ]
+
+-- -----------------------------------------------------------------------------
 -- Misc exported utils
 
 dataConType :: DataCon -> Type