2 % (c) The University of Glasgow, 2000
4 \section[CmSummarise]{Module summariser for GHCI}
7 module CmSummarise ( ModImport(..), mimp_name,
8 ModSummary(..), summarise, ms_get_imports,
9 name_of_summary, deps_of_summary,
13 #include "HsVersions.h"
16 import Char ( ord, isAlphaNum )
17 import Util ( unJust )
18 import HscTypes ( ModuleLocation(..) )
28 -- The Module contains the original source filename of the module.
29 -- The ms_ppsource field contains another filename, which is intended to
30 -- be the cleaned-up source file after all preprocessing has happened to
31 -- it. The point is that the summariser will have to cpp/unlit/whatever
32 -- all files anyway, and there's no point in doing this twice -- just
33 -- park the result in a temp file, put the name of it in ms_ppsource,
34 -- and let @compile@ read from that file on the way back up.
37 ms_mod :: Module, -- name, package
38 ms_location :: ModuleLocation, -- location
39 ms_ppsource :: (Maybe (FilePath, Fingerprint)), -- preprocessed and sig if .hs
40 ms_imports :: (Maybe [ModImport]) -- imports if .hs or .hi
43 instance Outputable ModSummary where
45 = sep [text "ModSummary {",
46 nest 3 (sep [text "ms_mod =" <+> ppr (ms_mod ms),
47 text "ms_ppsource =" <+> fooble (ms_ppsource ms),
48 text "ms_imports=" <+> ppr (ms_imports ms)]),
52 fooble Nothing = text "Nothing"
53 fooble (Just (cppd_source_name,fp))
54 = text "(fp =" <+> int fp <> text ","
55 <+> text (show cppd_source_name) <> text ")"
58 = MINormal ModuleName | MISource ModuleName
61 instance Outputable ModImport where
62 ppr (MINormal nm) = ppr nm
63 ppr (MISource nm) = text "{-# SOURCE #-}" <+> ppr nm
66 mimp_name (MINormal nm) = nm
67 mimp_name (MISource nm) = nm
69 name_of_summary :: ModSummary -> ModuleName
70 name_of_summary = moduleName . ms_mod
72 deps_of_summary :: ModSummary -> [ModuleName]
73 deps_of_summary = map mimp_name . ms_get_imports
75 ms_get_imports :: ModSummary -> [ModImport]
77 = case ms_imports summ of { Just is -> is; Nothing -> [] }
79 type Fingerprint = Int
81 summarise :: Module -> ModuleLocation -> IO ModSummary
82 summarise mod location
83 = if isModuleInThisPackage mod
85 let source_fn = unJust (ml_hspp_file location) "summarise"
86 modsrc <- readFile source_fn
87 let imps = getImports modsrc
88 fp = fingerprint modsrc
89 return (ModSummary mod location (Just (source_fn,fp)) (Just imps))
91 return (ModSummary mod location Nothing Nothing)
93 fingerprint :: String -> Int
95 = dofp s (_ILIT 3) (_ILIT 3)
97 -- Copied from hash() in Hugs' storage.c.
98 dofp :: String -> FastInt -> FastInt -> Int
99 dofp [] m fp = iBox fp
100 dofp (c:cs) m fp = dofp cs (m +# _ILIT 1)
101 (iabs (fp +# m *# iUnbox (ord c)))
103 iabs :: FastInt -> FastInt
104 iabs n = if n <# _ILIT 0 then (_ILIT 0) -# n else n
107 Collect up the imports from a Haskell source module. This is
108 approximate: we don't parse the module, but we do eliminate comments
109 and strings. Doesn't currently know how to unlit or cppify the module
114 getImports :: String -> [ModImport]
115 getImports = nub . gmiBase . clean
117 -- really get the imports from a de-litted, cpp'd, de-literal'd string
118 gmiBase :: String -> [ModImport]
122 f ("foreign" : "import" : ws) = f ws
123 f ("import" : "{-#" : "SOURCE" : "#-}" : "qualified" : m : ws)
124 = MISource (mkMN m) : f ws
125 f ("import" : "{-#" : "SOURCE" : "#-}" : m : ws)
126 = MISource (mkMN m) : f ws
127 f ("import" : "qualified" : m : ws)
128 = MINormal (mkMN m) : f ws
129 f ("import" : m : ws)
130 = MINormal (mkMN m) : f ws
134 mkMN str = mkModuleName (takeWhile isModId str)
135 isModId c = isAlphaNum c || c `elem` "'_"
137 -- remove literals and comments from a string
138 clean :: String -> String
142 -- running through text we want to keep
144 keep ('"':cs) = dquote cs
145 -- try to eliminate single quotes when they're part of
147 keep (c:'\'':cs) | isAlphaNum c || c == '_' = keep (dropWhile (=='\'') cs)
148 keep ('\'':cs) = squote cs
149 keep ('-':'-':cs) = linecomment cs
150 keep ('{':'-':'#':' ':cs) = "{-# " ++ keep cs
151 keep ('{':'-':cs) = runcomment cs
152 keep (c:cs) = c : keep cs
154 -- in a double-quoted string
156 dquote ('\\':'\"':cs) = dquote cs
157 dquote ('\\':'\\':cs) = dquote cs
158 dquote ('\"':cs) = keep cs
159 dquote (c:cs) = dquote cs
161 -- in a single-quoted string
163 squote ('\\':'\'':cs) = squote cs
164 squote ('\\':'\\':cs) = squote cs
165 squote ('\'':cs) = keep cs
166 squote (c:cs) = squote cs
170 linecomment ('\n':cs) = '\n':keep cs
171 linecomment (c:cs) = linecomment cs
173 -- in a running comment
175 runcomment ('-':'}':cs) = keep cs
176 runcomment (c:cs) = runcomment cs