Fix scoped type variables for expression type signatures
[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                 ("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                 ("InstType         ", inst_type_ds),
53                 ("InstData         ", inst_data_ds),
54                 ("TypeSigs         ", bind_tys),
55                 ("ValBinds         ", val_bind_ds),
56                 ("FunBinds         ", fn_bind_ds),
57                 ("InlineMeths      ", method_inlines),
58                 ("InlineBinds      ", bind_inlines),
59 --              ("SpecialisedData  ", data_specs),
60 --              ("SpecialisedInsts ", inst_specs),
61                 ("SpecialisedMeths ", method_specs),
62                 ("SpecialisedBinds ", bind_specs)
63                ])
64   where
65     decls = map unLoc ldecls
66
67     pp_val (str, 0) = empty
68     pp_val (str, n) 
69       | not short   = hcat [text str, int n]
70       | otherwise   = hcat [text (trim str), equals, int n, semi]
71     
72     trim ls     = takeWhile (not.isSpace) (dropWhile isSpace ls)
73
74     (fixity_sigs, bind_tys, bind_specs, bind_inlines) 
75         = count_sigs [d | SigD d <- decls]
76                 -- NB: this omits fixity decls on local bindings and
77                 -- in class decls.  ToDo
78
79     tycl_decls  = [d | TyClD d <- decls]
80     (class_ds, type_ds, type_fun_ds, type_equs, data_ds, newt_ds) = 
81       countTyClDecls tycl_decls
82
83     inst_decls  = [d | InstD d <- decls]
84     inst_ds     = length inst_decls
85     default_ds  = count (\ x -> case x of { DefD{} -> True; _ -> False}) decls
86     val_decls   = [d | ValD d <- decls]
87
88     real_exports = case exports of { Nothing -> []; Just es -> es }
89     n_exports    = length real_exports
90     export_ms    = count (\ e -> case unLoc e of { IEModuleContents{} -> True;_ -> False})
91                          real_exports
92     export_ds    = n_exports - export_ms
93     export_all   = case exports of { Nothing -> 1; other -> 0 }
94
95     (val_bind_ds, fn_bind_ds)
96         = foldr add2 (0,0) (map count_bind val_decls)
97
98     (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
99         = foldr add6 (0,0,0,0,0,0) (map import_info imports)
100     (data_constrs, data_derivs)
101         = foldr add2 (0,0) (map data_info tycl_decls)
102     (class_method_ds, default_method_ds)
103         = foldr add2 (0,0) (map class_info tycl_decls)
104     (inst_method_ds, method_specs, method_inlines, inst_type_ds, inst_data_ds)
105         = foldr add5 (0,0,0,0,0) (map inst_info inst_decls)
106
107     count_bind (PatBind { pat_lhs = L _ (VarPat n) }) = (1,0)
108     count_bind (PatBind {})                           = (0,1)
109     count_bind (FunBind {})                           = (0,1)
110
111     count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
112
113     sig_info (FixSig _)         = (1,0,0,0)
114     sig_info (TypeSig _ _)      = (0,1,0,0)
115     sig_info (SpecSig _ _ _)    = (0,0,1,0)
116     sig_info (InlineSig _ _)    = (0,0,0,1)
117     sig_info _                  = (0,0,0,0)
118
119     import_info (L _ (ImportDecl _ _ qual as spec))
120         = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
121     qual_info False  = 0
122     qual_info True   = 1
123     as_info Nothing  = 0
124     as_info (Just _) = 1
125     spec_info Nothing           = (0,0,0,1,0,0)
126     spec_info (Just (False, _)) = (0,0,0,0,1,0)
127     spec_info (Just (True, _))  = (0,0,0,0,0,1)
128
129     data_info (TyData {tcdCons = cs, tcdDerivs = derivs})
130         = (length cs, case derivs of Nothing -> 0
131                                      Just ds -> length ds)
132     data_info other = (0,0)
133
134     class_info decl@(ClassDecl {})
135         = case count_sigs (map unLoc (tcdSigs decl)) of
136             (_,classops,_,_) ->
137                (classops, addpr (foldr add2 (0,0) (map (count_bind.unLoc) (bagToList (tcdMeths decl)))))
138     class_info other = (0,0)
139
140     inst_info (InstDecl _ inst_meths inst_sigs ats)
141         = case count_sigs (map unLoc inst_sigs) of
142             (_,_,ss,is) ->
143               case foldr add2 (0, 0) (map (countATDecl . unLoc) ats) of
144                 (tyDecl, dtDecl) ->
145                   (addpr (foldr add2 (0,0) 
146                            (map (count_bind.unLoc) (bagToList inst_meths))), 
147                    ss, is, tyDecl, dtDecl)
148         where
149           countATDecl (TyData    {}) = (0, 1)
150           countATDecl (TySynonym {}) = (1, 0)
151
152     addpr :: (Int,Int) -> Int
153     add2  :: (Int,Int) -> (Int,Int) -> (Int, Int)
154     add3  :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
155     add4  :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
156     add5  :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
157     add6  :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
158
159     addpr (x,y) = x+y
160     add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
161     add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
162     add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
163     add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
164     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)
165 \end{code}
166
167
168
169
170
171
172
173
174