2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[GHC_Stats]{Statistics for per-module compilations}
7 module HscStats ( ppSourceStats ) where
9 #include "HsVersions.h"
12 import TyCon ( DataConDetails(..) )
14 import Char ( isSpace )
18 %************************************************************************
20 \subsection{Statistics}
22 %************************************************************************
25 ppSourceStats short (HsModule name version exports imports decls _ src_loc)
26 = (if short then hcat else vcat)
28 [("ExportAll ", export_all), -- 1 if no export list
29 ("ExportDecls ", export_ds),
30 ("ExportModules ", export_ms),
31 ("Imports ", import_no),
32 (" ImpQual ", import_qual),
33 (" ImpAs ", import_as),
34 (" ImpAll ", import_all),
35 (" ImpPartial ", import_partial),
36 (" ImpHiding ", import_hiding),
37 ("FixityDecls ", fixity_sigs),
38 ("DefaultDecls ", default_ds),
39 ("TypeDecls ", type_ds),
40 ("DataDecls ", data_ds),
41 ("NewTypeDecls ", newt_ds),
42 ("DataConstrs ", data_constrs),
43 ("DataDerivings ", data_derivs),
44 ("ClassDecls ", class_ds),
45 ("ClassMethods ", class_method_ds),
46 ("DefaultMethods ", default_method_ds),
47 ("InstDecls ", inst_ds),
48 ("InstMethods ", inst_method_ds),
49 ("TypeSigs ", bind_tys),
50 ("ValBinds ", val_bind_ds),
51 ("FunBinds ", fn_bind_ds),
52 ("InlineMeths ", method_inlines),
53 ("InlineBinds ", bind_inlines),
54 -- ("SpecialisedData ", data_specs),
55 -- ("SpecialisedInsts ", inst_specs),
56 ("SpecialisedMeths ", method_specs),
57 ("SpecialisedBinds ", bind_specs)
60 pp_val (str, 0) = empty
62 | not short = hcat [text str, int n]
63 | otherwise = hcat [text (trim str), equals, int n, semi]
65 trim ls = takeWhile (not.isSpace) (dropWhile isSpace ls)
67 (fixity_sigs, bind_tys, _, bind_specs, bind_inlines)
68 = count_sigs [d | SigD d <- decls]
69 -- NB: this omits fixity decls on local bindings and
70 -- in class decls. ToDo
72 tycl_decls = [d | TyClD d <- decls]
73 (class_ds, data_ds, newt_ds, type_ds, _) = countTyClDecls tycl_decls
75 inst_decls = [d | InstD d <- decls]
76 inst_ds = length inst_decls
77 default_ds = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
78 val_decls = [d | ValD d <- decls]
80 real_exports = case exports of { Nothing -> []; Just es -> es }
81 n_exports = length real_exports
82 export_ms = count (\ e -> case e of { IEModuleContents{} -> True;_ -> False})
84 export_ds = n_exports - export_ms
85 export_all = case exports of { Nothing -> 1; other -> 0 }
87 (val_bind_ds, fn_bind_ds)
88 = foldr add2 (0,0) (map count_monobinds val_decls)
90 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
91 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
92 (data_constrs, data_derivs)
93 = foldr add2 (0,0) (map data_info tycl_decls)
94 (class_method_ds, default_method_ds)
95 = foldr add2 (0,0) (map class_info tycl_decls)
96 (inst_method_ds, method_specs, method_inlines)
97 = foldr add3 (0,0,0) (map inst_info inst_decls)
99 count_monobinds EmptyMonoBinds = (0,0)
100 count_monobinds (AndMonoBinds b1 b2) = count_monobinds b1 `add2` count_monobinds b2
101 count_monobinds (PatMonoBind (VarPat n) r _) = (1,0)
102 count_monobinds (PatMonoBind p r _) = (0,1)
103 count_monobinds (FunMonoBind f _ m _) = (0,1)
105 count_mb_monobinds (Just mbs) = count_monobinds mbs
106 count_mb_monobinds Nothing = (0,0)
108 count_sigs sigs = foldr add5 (0,0,0,0,0) (map sig_info sigs)
110 sig_info (FixSig _) = (1,0,0,0,0)
111 sig_info (Sig _ _ _) = (0,1,0,0,0)
112 sig_info (ClassOpSig _ _ _ _) = (0,0,1,0,0)
113 sig_info (SpecSig _ _ _) = (0,0,0,1,0)
114 sig_info (InlineSig _ _ _ _) = (0,0,0,0,1)
115 sig_info _ = (0,0,0,0,0)
117 import_info (ImportDecl _ _ qual as spec _)
118 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
123 spec_info Nothing = (0,0,0,1,0,0)
124 spec_info (Just (False, _)) = (0,0,0,0,1,0)
125 spec_info (Just (True, _)) = (0,0,0,0,0,1)
127 data_info (TyData {tcdCons = DataCons cs, tcdDerivs = derivs})
128 = (length cs, case derivs of {Nothing -> 0; Just ds -> length ds})
129 data_info other = (0,0)
131 class_info decl@(ClassDecl {})
132 = case count_sigs (tcdSigs decl) of
133 (_,_,classops,_,_) ->
134 (classops, addpr (count_mb_monobinds (tcdMeths decl)))
135 class_info other = (0,0)
137 inst_info (InstDecl _ inst_meths inst_sigs _ _)
138 = case count_sigs inst_sigs of
140 (addpr (count_monobinds inst_meths), ss, is)
142 addpr :: (Int,Int) -> Int
143 add1 :: Int -> Int -> Int
144 add2 :: (Int,Int) -> (Int,Int) -> (Int, Int)
145 add3 :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
146 add4 :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
147 add5 :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
148 add6 :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
152 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
153 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
154 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
155 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
156 add6 (x1,x2,x3,x4,x5,x6) (y1,y2,y3,y4,y5,y6) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5,x6+y6)