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"
13 import SrcLoc ( unLoc, Located(..) )
14 import Char ( isSpace )
15 import Bag ( bagToList )
19 %************************************************************************
21 \subsection{Statistics}
23 %************************************************************************
26 ppSourceStats short (L _ (HsModule _ exports imports ldecls _))
27 = (if short then hcat else vcat)
29 [("ExportAll ", export_all), -- 1 if no export list
30 ("ExportDecls ", export_ds),
31 ("ExportModules ", export_ms),
32 ("Imports ", import_no),
33 (" ImpQual ", import_qual),
34 (" ImpAs ", import_as),
35 (" ImpAll ", import_all),
36 (" ImpPartial ", import_partial),
37 (" ImpHiding ", import_hiding),
38 ("FixityDecls ", fixity_sigs),
39 ("DefaultDecls ", default_ds),
40 ("TypeDecls ", type_ds),
41 ("DataDecls ", data_ds),
42 ("NewTypeDecls ", newt_ds),
43 ("DataConstrs ", data_constrs),
44 ("DataDerivings ", data_derivs),
45 ("ClassDecls ", class_ds),
46 ("ClassMethods ", class_method_ds),
47 ("DefaultMethods ", default_method_ds),
48 ("InstDecls ", inst_ds),
49 ("InstMethods ", inst_method_ds),
50 ("TypeSigs ", bind_tys),
51 ("ValBinds ", val_bind_ds),
52 ("FunBinds ", fn_bind_ds),
53 ("InlineMeths ", method_inlines),
54 ("InlineBinds ", bind_inlines),
55 -- ("SpecialisedData ", data_specs),
56 -- ("SpecialisedInsts ", inst_specs),
57 ("SpecialisedMeths ", method_specs),
58 ("SpecialisedBinds ", bind_specs)
61 decls = map unLoc ldecls
63 pp_val (str, 0) = empty
65 | not short = hcat [text str, int n]
66 | otherwise = hcat [text (trim str), equals, int n, semi]
68 trim ls = takeWhile (not.isSpace) (dropWhile isSpace ls)
70 (fixity_sigs, bind_tys, bind_specs, bind_inlines)
71 = count_sigs [d | SigD d <- decls]
72 -- NB: this omits fixity decls on local bindings and
73 -- in class decls. ToDo
75 tycl_decls = [d | TyClD d <- decls]
76 (class_ds, type_ds, data_ds, newt_ds) = countTyClDecls tycl_decls
78 inst_decls = [d | InstD d <- decls]
79 inst_ds = length inst_decls
80 default_ds = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
81 val_decls = [d | ValD d <- decls]
83 real_exports = case exports of { Nothing -> []; Just es -> es }
84 n_exports = length real_exports
85 export_ms = count (\ e -> case unLoc e of { IEModuleContents{} -> True;_ -> False})
87 export_ds = n_exports - export_ms
88 export_all = case exports of { Nothing -> 1; other -> 0 }
90 (val_bind_ds, fn_bind_ds)
91 = foldr add2 (0,0) (map count_bind val_decls)
93 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
94 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
95 (data_constrs, data_derivs)
96 = foldr add2 (0,0) (map data_info tycl_decls)
97 (class_method_ds, default_method_ds)
98 = foldr add2 (0,0) (map class_info tycl_decls)
99 (inst_method_ds, method_specs, method_inlines)
100 = foldr add3 (0,0,0) (map inst_info inst_decls)
102 count_bind (PatBind (L _ (VarPat n)) r _) = (1,0)
103 count_bind (PatBind p r _) = (0,1)
104 count_bind (FunBind f _ m) = (0,1)
106 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
108 sig_info (FixSig _) = (1,0,0,0)
109 sig_info (Sig _ _) = (0,1,0,0)
110 sig_info (SpecSig _ _) = (0,0,1,0)
111 sig_info (InlineSig _ _ _) = (0,0,0,1)
112 sig_info _ = (0,0,0,0)
114 import_info (L _ (ImportDecl _ _ qual as spec))
115 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
120 spec_info Nothing = (0,0,0,1,0,0)
121 spec_info (Just (False, _)) = (0,0,0,0,1,0)
122 spec_info (Just (True, _)) = (0,0,0,0,0,1)
124 data_info (TyData {tcdCons = cs, tcdDerivs = derivs})
125 = (length cs, case derivs of Nothing -> 0
126 Just ds -> length ds)
127 data_info other = (0,0)
129 class_info decl@(ClassDecl {})
130 = case count_sigs (map unLoc (tcdSigs decl)) of
132 (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
133 class_info other = (0,0)
135 inst_info (InstDecl _ inst_meths inst_sigs)
136 = case count_sigs (map unLoc inst_sigs) of
138 (addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList inst_meths))), ss, is)
140 addpr :: (Int,Int) -> Int
141 add2 :: (Int,Int) -> (Int,Int) -> (Int, Int)
142 add3 :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
143 add4 :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
144 add6 :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
147 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
148 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
149 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
150 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)