e4e8ac560c679f9819ec965d60156fd9c97a1bc4
[ghc-hetmet.git] / compiler / main / HscStats.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[GHC_Stats]{Statistics for per-module compilations}
5
6 \begin{code}
7 module HscStats ( ppSourceStats ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn
12 import Outputable
13 import SrcLoc           ( unLoc, Located(..) )
14 import Char             ( isSpace )
15 import Bag              ( bagToList )
16 import Util             ( count )
17 \end{code}
18
19 %************************************************************************
20 %*                                                                      *
21 \subsection{Statistics}
22 %*                                                                      *
23 %************************************************************************
24
25 \begin{code}
26 ppSourceStats short (L _ (HsModule _ exports imports ldecls _))
27  = (if short then hcat else vcat)
28         (map pp_val
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)
59                ])
60   where
61     decls = map unLoc ldecls
62
63     pp_val (str, 0) = empty
64     pp_val (str, n) 
65       | not short   = hcat [text str, int n]
66       | otherwise   = hcat [text (trim str), equals, int n, semi]
67     
68     trim ls     = takeWhile (not.isSpace) (dropWhile isSpace ls)
69
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
74
75     tycl_decls  = [d | TyClD d <- decls]
76     (class_ds, type_ds, data_ds, newt_ds) = countTyClDecls tycl_decls
77
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]
82
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})
86                          real_exports
87     export_ds    = n_exports - export_ms
88     export_all   = case exports of { Nothing -> 1; other -> 0 }
89
90     (val_bind_ds, fn_bind_ds)
91         = foldr add2 (0,0) (map count_bind val_decls)
92
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)
101
102     count_bind (PatBind { pat_lhs = L _ (VarPat n) }) = (1,0)
103     count_bind (PatBind {})                           = (0,1)
104     count_bind (FunBind {})                           = (0,1)
105
106     count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
107
108     sig_info (FixSig _)         = (1,0,0,0)
109     sig_info (TypeSig _ _)      = (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)
113
114     import_info (L _ (ImportDecl _ _ qual as spec))
115         = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
116     qual_info False  = 0
117     qual_info True   = 1
118     as_info Nothing  = 0
119     as_info (Just _) = 1
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)
123
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)
128
129     class_info decl@(ClassDecl {})
130         = case count_sigs (map unLoc (tcdSigs decl)) of
131             (_,classops,_,_) ->
132                (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
133     class_info other = (0,0)
134
135     inst_info (InstDecl _ inst_meths inst_sigs _)  -- !!!TODO: ATs info -=chak
136         = case count_sigs (map unLoc inst_sigs) of
137             (_,_,ss,is) ->
138                (addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList inst_meths))), ss, is)
139
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)
145
146     addpr (x,y) = x+y
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)
151 \end{code}
152
153
154
155
156
157
158
159
160