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