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 ("TypeFunDecls ", type_fun_ds),
42 ("TypeEquations ", type_equs),
43 ("DataDecls ", data_ds),
44 ("NewTypeDecls ", newt_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 ("TypeSigs ", bind_tys),
53 ("ValBinds ", val_bind_ds),
54 ("FunBinds ", fn_bind_ds),
55 ("InlineMeths ", method_inlines),
56 ("InlineBinds ", bind_inlines),
57 -- ("SpecialisedData ", data_specs),
58 -- ("SpecialisedInsts ", inst_specs),
59 ("SpecialisedMeths ", method_specs),
60 ("SpecialisedBinds ", bind_specs)
63 decls = map unLoc ldecls
65 pp_val (str, 0) = empty
67 | not short = hcat [text str, int n]
68 | otherwise = hcat [text (trim str), equals, int n, semi]
70 trim ls = takeWhile (not.isSpace) (dropWhile isSpace ls)
72 (fixity_sigs, bind_tys, bind_specs, bind_inlines)
73 = count_sigs [d | SigD d <- decls]
74 -- NB: this omits fixity decls on local bindings and
75 -- in class decls. ToDo
77 tycl_decls = [d | TyClD d <- decls]
78 (class_ds, type_ds, type_fun_ds, type_equs, data_ds, newt_ds) =
79 countTyClDecls tycl_decls
81 inst_decls = [d | InstD d <- decls]
82 inst_ds = length inst_decls
83 default_ds = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
84 val_decls = [d | ValD d <- decls]
86 real_exports = case exports of { Nothing -> []; Just es -> es }
87 n_exports = length real_exports
88 export_ms = count (\ e -> case unLoc e of { IEModuleContents{} -> True;_ -> False})
90 export_ds = n_exports - export_ms
91 export_all = case exports of { Nothing -> 1; other -> 0 }
93 (val_bind_ds, fn_bind_ds)
94 = foldr add2 (0,0) (map count_bind val_decls)
96 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
97 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
98 (data_constrs, data_derivs)
99 = foldr add2 (0,0) (map data_info tycl_decls)
100 (class_method_ds, default_method_ds)
101 = foldr add2 (0,0) (map class_info tycl_decls)
102 (inst_method_ds, method_specs, method_inlines)
103 = foldr add3 (0,0,0) (map inst_info inst_decls)
105 count_bind (PatBind { pat_lhs = L _ (VarPat n) }) = (1,0)
106 count_bind (PatBind {}) = (0,1)
107 count_bind (FunBind {}) = (0,1)
109 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
111 sig_info (FixSig _) = (1,0,0,0)
112 sig_info (TypeSig _ _) = (0,1,0,0)
113 sig_info (SpecSig _ _ _) = (0,0,1,0)
114 sig_info (InlineSig _ _) = (0,0,0,1)
115 sig_info _ = (0,0,0,0)
117 import_info (L _ (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 = cs, tcdDerivs = derivs})
128 = (length cs, case derivs of Nothing -> 0
129 Just ds -> length ds)
130 data_info other = (0,0)
132 class_info decl@(ClassDecl {})
133 = case count_sigs (map unLoc (tcdSigs decl)) of
135 (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
136 class_info other = (0,0)
138 inst_info (InstDecl _ inst_meths inst_sigs _) -- !!!TODO: ATs info -=chak
139 = case count_sigs (map unLoc inst_sigs) of
141 (addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList inst_meths))), ss, is)
143 addpr :: (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 add6 :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
150 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
151 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
152 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
153 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)