[project @ 2000-10-17 11:25:35 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / CmSummarise.lhs
index 7ef80a9..f68ca48 100644 (file)
 %
-% (c) The AQUA Project, Glasgow University, 1993-2000
+% (c) The University of Glasgow, 2000
 %
 \section[CmSummarise]{Module summariser for GHCI}
 
 \begin{code}
 module CmSummarise ( ModImport(..), mi_name,
-                     ModSummary(..), summarise, ms_get_imports )
+                     ModSummary(..), summarise, ms_get_imports,
+                    name_of_summary, deps_of_summary,
+                    getImports )
 where
 
 #include "HsVersions.h"
 
 import List            ( nub )
 import Char            ( ord, isAlphaNum )
+import Finder
+import FastTypes
 
-import CmFind          ( ModName, ModLocation(..) )
-import Outputable      ( pprPanic, text )
+import Module
+import Outputable
 \end{code}
 
 \begin{code}
 
 
+-- The Module contains the original source filename of the module.
+-- The ms_ppsource field contains another filename, which is intended to
+-- be the cleaned-up source file after all preprocessing has happened to
+-- it.  The point is that the summariser will have to cpp/unlit/whatever
+-- all files anyway, and there's no point in doing this twice -- just 
+-- park the result in a temp file, put the name of it in ms_ppsource,
+-- and let @compile@ read from that file on the way back up.
 data ModSummary
    = ModSummary {
-        ms_loc     :: ModLocation,                   -- location and kind
-        ms_source  :: (Maybe (String, Fingerprint)), -- source and sig if .hs
-        ms_imports :: (Maybe [ModImport])            -- imports if .hs or .hi
+        ms_mod      :: Module,                          -- name, package
+       ms_location :: ModuleLocation,                  -- location
+        ms_ppsource :: (Maybe (FilePath, Fingerprint)), -- preprocessed and sig if .hs
+        ms_imports  :: (Maybe [ModImport])              -- imports if .hs or .hi
      }
-     deriving Show
+
+instance Outputable ModSummary where
+   ppr ms
+      = sep [text "ModSummary {",
+             nest 3 (sep [text "ms_mod =" <+> ppr (ms_mod ms),
+             text "ms_ppsource =" <+> fooble (ms_ppsource ms),
+             text "ms_imports=" <+> ppr (ms_imports ms)]),
+             char '}'
+            ]
+        where
+           fooble Nothing = text "Nothing"
+           fooble (Just (cppd_source_name,fp)) 
+              = text "(fp =" <+> int fp <> text "," 
+                <+> text (show cppd_source_name) <> text ")"
 
 data ModImport
-   = MINormal ModName | MISource ModName
-     deriving (Eq, Show)
+   = MINormal ModuleName | MISource ModuleName
+     deriving Eq
+
+instance Outputable ModImport where
+   ppr (MINormal nm) = ppr nm
+   ppr (MISource nm) = text "{-# SOURCE #-}" <+> ppr nm
+
 
 mi_name (MINormal nm) = nm
 mi_name (MISource nm) = nm
 
+name_of_summary :: ModSummary -> ModuleName
+name_of_summary = moduleName . ms_mod
+
+deps_of_summary :: ModSummary -> [ModuleName]
+deps_of_summary = map mi_name . ms_get_imports
+
 ms_get_imports :: ModSummary -> [ModImport]
 ms_get_imports summ
    = case ms_imports summ of { Just is -> is; Nothing -> [] }
 
 type Fingerprint = Int
 
-summarise :: ModLocation -> IO ModSummary
-
-summarise loc
-   = case loc of
-        InPackage mod path -- if in a package, investigate no further
-           -> return (ModSummary loc Nothing Nothing)
-        SourceOnly mod path -- source; read, cache and get imports
-           -> readFile path >>= \ modsrc ->
-              let imps = getImports modsrc
-                  fp   = fingerprint modsrc
-              in  return (ModSummary loc (Just (modsrc,fp)) (Just imps))
-        ObjectCode mod oPath hiPath -- can we get away with the src summariser
-                                    -- for interface files?
-           -> readFile hiPath >>= \ hisrc ->
-              let imps = getImports hisrc
-              in  return (ModSummary loc Nothing (Just imps))
-        NotFound
-           -> pprPanic "summarise:NotFound" (text (show loc))
-
+summarise :: Module -> ModuleLocation -> IO ModSummary
+summarise mod location
+   = if isModuleInThisPackage mod
+       then do 
+           let source_fn = hs_file location
+           -- ToDo:
+           -- ppsource_fn <- preprocess source_fn
+           modsrc <- readFile source_fn
+            let imps = getImports modsrc
+                fp   = fingerprint modsrc
+            return (ModSummary mod location (Just (source_fn,fp)) (Just imps))
+       else
+           return (ModSummary mod location Nothing Nothing)
+       
 fingerprint :: String -> Int
 fingerprint s
-   = dofp s 3 3
+   = dofp s (_ILIT 3) (_ILIT 3)
      where
         -- Copied from hash() in Hugs' storage.c.
-        dofp :: String -> Int -> Int -> Int
-        dofp []     m fp = fp
-        dofp (c:cs) m fp = dofp cs (m+1) (iabs (fp + m * ord c))
-        iabs :: Int -> Int
-        iabs n = if n < 0 then -n else n
+        dofp :: String -> FastInt -> FastInt -> Int
+        dofp []     m fp = iBox fp
+        dofp (c:cs) m fp = dofp cs (m +# _ILIT 1) 
+                               (iabs (fp +# m *# iUnbox (ord c)))
+
+        iabs :: FastInt -> FastInt
+        iabs n = if n <# _ILIT 0 then (_ILIT 0) -# n else n
 \end{code}
 
 Collect up the imports from a Haskell source module.  This is
@@ -89,17 +122,18 @@ gmiBase s
      where
        f ("foreign" : "import" : ws) = f ws
         f ("import" : "{-#" : "SOURCE" : "#-}" : "qualified" : m : ws) 
-           = MISource (takeWhile isModId m) : f ws
+           = MISource (mkMN m) : f ws
         f ("import" : "{-#" : "SOURCE" : "#-}" : m : ws) 
-           = MISource (takeWhile isModId m) : f ws
+           = MISource (mkMN m) : f ws
         f ("import" : "qualified" : m : ws) 
-           = MINormal (takeWhile isModId m) : f ws
+           = MINormal (mkMN m) : f ws
         f ("import" : m : ws) 
-           = MINormal (takeWhile isModId m) : f ws
+           = MINormal (mkMN m) : f ws
         f (w:ws) = f ws
         f [] = []
 
-isModId c = isAlphaNum c || c `elem` "'_"
+        mkMN str = mkModuleName (takeWhile isModId str)
+        isModId c = isAlphaNum c || c `elem` "'_"
 
 -- remove literals and comments from a string
 clean :: String -> String
@@ -141,4 +175,4 @@ clean s
         runcomment []           = []
         runcomment ('-':'}':cs) = keep cs
         runcomment (c:cs)       = runcomment cs
-\end{code}
\ No newline at end of file
+\end{code}