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 ("TypeFamilyDecls ", type_fam_ds),
44 ("FamilyInstDecls ", fam_inst_ds),
45 ("DataConstrs ", data_constrs),
46 ("DataDerivings ", data_derivs),
47 ("ClassDecls ", class_ds),
48 ("ClassMethods ", class_method_ds),
49 ("DefaultMethods ", default_method_ds),
50 ("InstDecls ", inst_ds),
51 ("InstMethods ", inst_method_ds),
52 ("InstType ", inst_type_ds),
53 ("InstData ", inst_data_ds),
54 ("TypeSigs ", bind_tys),
55 ("ValBinds ", val_bind_ds),
56 ("FunBinds ", fn_bind_ds),
57 ("InlineMeths ", method_inlines),
58 ("InlineBinds ", bind_inlines),
59 -- ("SpecialisedData ", data_specs),
60 -- ("SpecialisedInsts ", inst_specs),
61 ("SpecialisedMeths ", method_specs),
62 ("SpecialisedBinds ", bind_specs)
65 decls = map unLoc ldecls
67 pp_val (str, 0) = empty
69 | not short = hcat [text str, int n]
70 | otherwise = hcat [text (trim str), equals, int n, semi]
72 trim ls = takeWhile (not.isSpace) (dropWhile isSpace ls)
74 (fixity_sigs, bind_tys, bind_specs, bind_inlines)
75 = count_sigs [d | SigD d <- decls]
76 -- NB: this omits fixity decls on local bindings and
77 -- in class decls. ToDo
79 tycl_decls = [d | TyClD d <- decls]
80 (class_ds, type_ds, data_ds, newt_ds, type_fam_ds, fam_inst_ds) =
81 countTyClDecls tycl_decls
83 inst_decls = [d | InstD d <- decls]
84 inst_ds = length inst_decls
85 default_ds = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
86 val_decls = [d | ValD d <- decls]
88 real_exports = case exports of { Nothing -> []; Just es -> es }
89 n_exports = length real_exports
90 export_ms = count (\ e -> case unLoc e of { IEModuleContents{} -> True;_ -> False})
92 export_ds = n_exports - export_ms
93 export_all = case exports of { Nothing -> 1; other -> 0 }
95 (val_bind_ds, fn_bind_ds)
96 = foldr add2 (0,0) (map count_bind val_decls)
98 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
99 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
100 (data_constrs, data_derivs)
101 = foldr add2 (0,0) (map data_info tycl_decls)
102 (class_method_ds, default_method_ds)
103 = foldr add2 (0,0) (map class_info tycl_decls)
104 (inst_method_ds, method_specs, method_inlines, inst_type_ds, inst_data_ds)
105 = foldr add5 (0,0,0,0,0) (map inst_info inst_decls)
107 count_bind (PatBind { pat_lhs = L _ (VarPat n) }) = (1,0)
108 count_bind (PatBind {}) = (0,1)
109 count_bind (FunBind {}) = (0,1)
111 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
113 sig_info (FixSig _) = (1,0,0,0)
114 sig_info (TypeSig _ _) = (0,1,0,0)
115 sig_info (SpecSig _ _ _) = (0,0,1,0)
116 sig_info (InlineSig _ _) = (0,0,0,1)
117 sig_info _ = (0,0,0,0)
119 import_info (L _ (ImportDecl _ _ qual as spec))
120 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
125 spec_info Nothing = (0,0,0,1,0,0)
126 spec_info (Just (False, _)) = (0,0,0,0,1,0)
127 spec_info (Just (True, _)) = (0,0,0,0,0,1)
129 data_info (TyData {tcdCons = cs, tcdDerivs = derivs})
130 = (length cs, case derivs of Nothing -> 0
131 Just ds -> length ds)
132 data_info other = (0,0)
134 class_info decl@(ClassDecl {})
135 = case count_sigs (map unLoc (tcdSigs decl)) of
137 (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
138 class_info other = (0,0)
140 inst_info (InstDecl _ inst_meths inst_sigs ats)
141 = case count_sigs (map unLoc inst_sigs) of
143 case foldr add2 (0, 0) (map (countATDecl . unLoc) ats) of
145 (addpr (foldr add2 (0,0)
146 (map (count_bind.unLoc) (bagToList inst_meths))),
147 ss, is, tyDecl, dtDecl)
149 countATDecl (TyData {}) = (0, 1)
150 countATDecl (TySynonym {}) = (1, 0)
152 addpr :: (Int,Int) -> Int
153 add2 :: (Int,Int) -> (Int,Int) -> (Int, Int)
154 add3 :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
155 add4 :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
156 add5 :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
157 add6 :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
160 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
161 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
162 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
163 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
164 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)