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