Fix warnings in main/HscStats
[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 -- XXX This define is a bit of a hack, and should be done more nicely
10 #define FAST_STRING_NOT_NEEDED 1
11 #include "HsVersions.h"
12
13 import HsSyn
14 import Outputable
15 import SrcLoc
16 import Char
17 import Bag
18 import Util
19 import Pretty ( Doc )
20 import RdrName
21 \end{code}
22
23 %************************************************************************
24 %*                                                                      *
25 \subsection{Statistics}
26 %*                                                                      *
27 %************************************************************************
28
29 \begin{code}
30 ppSourceStats :: Bool -> Located (HsModule RdrName) -> PprStyle -> Doc
31 ppSourceStats short (L _ (HsModule _ exports imports ldecls _ _ _))
32  = (if short then hcat else vcat)
33         (map pp_val
34                [("ExportAll        ", export_all), -- 1 if no export list
35                 ("ExportDecls      ", export_ds),
36                 ("ExportModules    ", export_ms),
37                 ("Imports          ", import_no),
38                 ("  ImpQual        ", import_qual),
39                 ("  ImpAs          ", import_as),
40                 ("  ImpAll         ", import_all),
41                 ("  ImpPartial     ", import_partial),
42                 ("  ImpHiding      ", import_hiding),
43                 ("FixityDecls      ", fixity_sigs),
44                 ("DefaultDecls     ", default_ds),
45                 ("TypeDecls        ", type_ds),
46                 ("DataDecls        ", data_ds),
47                 ("NewTypeDecls     ", newt_ds),
48                 ("TypeFamilyDecls  ", type_fam_ds),
49                 ("FamilyInstDecls  ", fam_inst_ds),
50                 ("DataConstrs      ", data_constrs),
51                 ("DataDerivings    ", data_derivs),
52                 ("ClassDecls       ", class_ds),
53                 ("ClassMethods     ", class_method_ds),
54                 ("DefaultMethods   ", default_method_ds),
55                 ("InstDecls        ", inst_ds),
56                 ("InstMethods      ", inst_method_ds),
57                 ("InstType         ", inst_type_ds),
58                 ("InstData         ", inst_data_ds),
59                 ("TypeSigs         ", bind_tys),
60                 ("ValBinds         ", val_bind_ds),
61                 ("FunBinds         ", fn_bind_ds),
62                 ("InlineMeths      ", method_inlines),
63                 ("InlineBinds      ", bind_inlines),
64 --              ("SpecialisedData  ", data_specs),
65 --              ("SpecialisedInsts ", inst_specs),
66                 ("SpecialisedMeths ", method_specs),
67                 ("SpecialisedBinds ", bind_specs)
68                ])
69   where
70     decls = map unLoc ldecls
71
72     pp_val (_, 0) = empty
73     pp_val (str, n) 
74       | not short   = hcat [text str, int n]
75       | otherwise   = hcat [text (trim str), equals, int n, semi]
76     
77     trim ls     = takeWhile (not.isSpace) (dropWhile isSpace ls)
78
79     (fixity_sigs, bind_tys, bind_specs, bind_inlines) 
80         = count_sigs [d | SigD d <- decls]
81                 -- NB: this omits fixity decls on local bindings and
82                 -- in class decls.  ToDo
83
84     tycl_decls  = [d | TyClD d <- decls]
85     (class_ds, type_ds, data_ds, newt_ds, type_fam_ds, fam_inst_ds) = 
86       countTyClDecls tycl_decls
87
88     inst_decls  = [d | InstD d <- decls]
89     inst_ds     = length inst_decls
90     default_ds  = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
91     val_decls   = [d | ValD d <- decls]
92
93     real_exports = case exports of { Nothing -> []; Just es -> es }
94     n_exports    = length real_exports
95     export_ms    = count (\ e -> case unLoc e of { IEModuleContents{} -> True;_ -> False})
96                          real_exports
97     export_ds    = n_exports - export_ms
98     export_all   = case exports of { Nothing -> 1; _ -> 0 }
99
100     (val_bind_ds, fn_bind_ds)
101         = foldr add2 (0,0) (map count_bind val_decls)
102
103     (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
104         = foldr add6 (0,0,0,0,0,0) (map import_info imports)
105     (data_constrs, data_derivs)
106         = foldr add2 (0,0) (map data_info tycl_decls)
107     (class_method_ds, default_method_ds)
108         = foldr add2 (0,0) (map class_info tycl_decls)
109     (inst_method_ds, method_specs, method_inlines, inst_type_ds, inst_data_ds)
110         = foldr add5 (0,0,0,0,0) (map inst_info inst_decls)
111
112     count_bind (PatBind { pat_lhs = L _ (VarPat _) }) = (1,0)
113     count_bind (PatBind {})                           = (0,1)
114     count_bind (FunBind {})                           = (0,1)
115     count_bind b = pprPanic "count_bind: Unhandled binder" (ppr b)
116
117     count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
118
119     sig_info (FixSig _)         = (1,0,0,0)
120     sig_info (TypeSig _ _)      = (0,1,0,0)
121     sig_info (SpecSig _ _ _)    = (0,0,1,0)
122     sig_info (InlineSig _ _)    = (0,0,0,1)
123     sig_info _                  = (0,0,0,0)
124
125     import_info (L _ (ImportDecl _ _ qual as spec))
126         = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
127     qual_info False  = 0
128     qual_info True   = 1
129     as_info Nothing  = 0
130     as_info (Just _) = 1
131     spec_info Nothing           = (0,0,0,1,0,0)
132     spec_info (Just (False, _)) = (0,0,0,0,1,0)
133     spec_info (Just (True, _))  = (0,0,0,0,0,1)
134
135     data_info (TyData {tcdCons = cs, tcdDerivs = derivs})
136         = (length cs, case derivs of Nothing -> 0
137                                      Just ds -> length ds)
138     data_info _ = (0,0)
139
140     class_info decl@(ClassDecl {})
141         = case count_sigs (map unLoc (tcdSigs decl)) of
142             (_,classops,_,_) ->
143                (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
144     class_info _ = (0,0)
145
146     inst_info (InstDecl _ inst_meths inst_sigs ats)
147         = case count_sigs (map unLoc inst_sigs) of
148             (_,_,ss,is) ->
149               case foldr add2 (0, 0) (map (countATDecl . unLoc) ats) of
150                 (tyDecl, dtDecl) ->
151                   (addpr (foldr add2 (0,0) 
152                            (map (count_bind.unLoc) (bagToList inst_meths))), 
153                    ss, is, tyDecl, dtDecl)
154         where
155           countATDecl (TyData    {}) = (0, 1)
156           countATDecl (TySynonym {}) = (1, 0)
157           countATDecl d              = pprPanic "countATDecl: Unhandled decl"
158                                             (ppr d)
159
160     addpr :: (Int,Int) -> Int
161     add2  :: (Int,Int) -> (Int,Int) -> (Int, Int)
162     add4  :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
163     add5  :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
164     add6  :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
165
166     addpr (x,y) = x+y
167     add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
168     add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
169     add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
170     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)
171 \end{code}
172
173
174
175
176
177
178
179
180