[project @ 2000-10-26 14:34:57 by sewardj]
[ghc-hetmet.git] / ghc / compiler / ghci / CmSummarise.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[CmSummarise]{Module summariser for GHCI}
5
6 \begin{code}
7 module CmSummarise ( ModImport(..), mimp_name,
8                      ModSummary(..), summarise, ms_get_imports,
9                      name_of_summary, deps_of_summary,
10                      getImports )
11 where
12
13 #include "HsVersions.h"
14
15 import List             ( nub )
16 import Char             ( ord, isAlphaNum )
17 import Finder
18 import FastTypes
19
20 import Module
21 import Outputable
22 \end{code}
23
24 \begin{code}
25
26
27 -- The Module contains the original source filename of the module.
28 -- The ms_ppsource field contains another filename, which is intended to
29 -- be the cleaned-up source file after all preprocessing has happened to
30 -- it.  The point is that the summariser will have to cpp/unlit/whatever
31 -- all files anyway, and there's no point in doing this twice -- just 
32 -- park the result in a temp file, put the name of it in ms_ppsource,
33 -- and let @compile@ read from that file on the way back up.
34 data ModSummary
35    = ModSummary {
36         ms_mod      :: Module,                          -- name, package
37         ms_location :: ModuleLocation,                  -- location
38         ms_ppsource :: (Maybe (FilePath, Fingerprint)), -- preprocessed and sig if .hs
39         ms_imports  :: (Maybe [ModImport])              -- imports if .hs or .hi
40      }
41
42 instance Outputable ModSummary where
43    ppr ms
44       = sep [text "ModSummary {",
45              nest 3 (sep [text "ms_mod =" <+> ppr (ms_mod ms),
46              text "ms_ppsource =" <+> fooble (ms_ppsource ms),
47              text "ms_imports=" <+> ppr (ms_imports ms)]),
48              char '}'
49             ]
50         where
51            fooble Nothing = text "Nothing"
52            fooble (Just (cppd_source_name,fp)) 
53               = text "(fp =" <+> int fp <> text "," 
54                 <+> text (show cppd_source_name) <> text ")"
55
56 data ModImport
57    = MINormal ModuleName | MISource ModuleName
58      deriving Eq
59
60 instance Outputable ModImport where
61    ppr (MINormal nm) = ppr nm
62    ppr (MISource nm) = text "{-# SOURCE #-}" <+> ppr nm
63
64
65 mimp_name (MINormal nm) = nm
66 mimp_name (MISource nm) = nm
67
68 name_of_summary :: ModSummary -> ModuleName
69 name_of_summary = moduleName . ms_mod
70
71 deps_of_summary :: ModSummary -> [ModuleName]
72 deps_of_summary = map mimp_name . ms_get_imports
73
74 ms_get_imports :: ModSummary -> [ModImport]
75 ms_get_imports summ
76    = case ms_imports summ of { Just is -> is; Nothing -> [] }
77
78 type Fingerprint = Int
79
80 summarise :: Module -> ModuleLocation -> IO ModSummary
81 summarise mod location
82    = if isModuleInThisPackage mod
83         then do 
84             let source_fn = hs_file location
85             -- ToDo:
86             -- ppsource_fn <- preprocess source_fn
87             modsrc <- readFile source_fn
88             let imps = getImports modsrc
89                 fp   = fingerprint modsrc
90             return (ModSummary mod location (Just (source_fn,fp)) (Just imps))
91         else
92            return (ModSummary mod location Nothing Nothing)
93         
94 fingerprint :: String -> Int
95 fingerprint s
96    = dofp s (_ILIT 3) (_ILIT 3)
97      where
98         -- Copied from hash() in Hugs' storage.c.
99         dofp :: String -> FastInt -> FastInt -> Int
100         dofp []     m fp = iBox fp
101         dofp (c:cs) m fp = dofp cs (m +# _ILIT 1) 
102                                 (iabs (fp +# m *# iUnbox (ord c)))
103
104         iabs :: FastInt -> FastInt
105         iabs n = if n <# _ILIT 0 then (_ILIT 0) -# n else n
106 \end{code}
107
108 Collect up the imports from a Haskell source module.  This is
109 approximate: we don't parse the module, but we do eliminate comments
110 and strings.  Doesn't currently know how to unlit or cppify the module
111 first.
112
113 \begin{code}
114
115 getImports :: String -> [ModImport]
116 getImports = nub . gmiBase . clean
117
118 -- really get the imports from a de-litted, cpp'd, de-literal'd string
119 gmiBase :: String -> [ModImport]
120 gmiBase s
121    = f (words s)
122      where
123         f ("foreign" : "import" : ws) = f ws
124         f ("import" : "{-#" : "SOURCE" : "#-}" : "qualified" : m : ws) 
125            = MISource (mkMN m) : f ws
126         f ("import" : "{-#" : "SOURCE" : "#-}" : m : ws) 
127            = MISource (mkMN m) : f ws
128         f ("import" : "qualified" : m : ws) 
129            = MINormal (mkMN m) : f ws
130         f ("import" : m : ws) 
131            = MINormal (mkMN m) : f ws
132         f (w:ws) = f ws
133         f [] = []
134
135         mkMN str = mkModuleName (takeWhile isModId str)
136         isModId c = isAlphaNum c || c `elem` "'_"
137
138 -- remove literals and comments from a string
139 clean :: String -> String
140 clean s
141    = keep s
142      where
143         -- running through text we want to keep
144         keep []                   = []
145         keep ('"':cs)             = dquote cs
146                 -- try to eliminate single quotes when they're part of
147                 -- an identifier...
148         keep (c:'\'':cs) | isAlphaNum c || c == '_' = keep (dropWhile (=='\'') cs)
149         keep ('\'':cs)            = squote cs
150         keep ('-':'-':cs)         = linecomment cs
151         keep ('{':'-':'#':' ':cs) = "{-# " ++ keep cs
152         keep ('{':'-':cs)         = runcomment cs
153         keep (c:cs)               = c : keep cs
154
155         -- in a double-quoted string
156         dquote []             = []
157         dquote ('\\':'\"':cs) = dquote cs
158         dquote ('\\':'\\':cs) = dquote cs
159         dquote ('\"':cs)      = keep cs
160         dquote (c:cs)         = dquote cs
161
162         -- in a single-quoted string
163         squote []             = []
164         squote ('\\':'\'':cs) = squote cs
165         squote ('\\':'\\':cs) = squote cs
166         squote ('\'':cs)      = keep cs
167         squote (c:cs)         = squote cs
168
169         -- in a line comment
170         linecomment []        = []
171         linecomment ('\n':cs) = '\n':keep cs
172         linecomment (c:cs)    = linecomment cs
173
174         -- in a running comment
175         runcomment []           = []
176         runcomment ('-':'}':cs) = keep cs
177         runcomment (c:cs)       = runcomment cs
178 \end{code}