adamc@134
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@134
|
2 * All rights reserved.
|
adamc@134
|
3 *
|
adamc@134
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@134
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@134
|
6 *
|
adamc@134
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@134
|
8 * this list of conditions and the following disclaimer.
|
adamc@134
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@134
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@134
|
11 * and/or other materials provided with the distribution.
|
adamc@134
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@134
|
13 * derived from this software without specific prior written permission.
|
adamc@134
|
14 *
|
adamc@134
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@134
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@134
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@134
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@134
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@134
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@134
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@134
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@134
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@134
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@134
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@134
|
26 *)
|
adamc@134
|
27
|
adamc@134
|
28 (* Remove unused definitions from a file *)
|
adamc@134
|
29
|
adamc@134
|
30 structure MonoShake :> MONO_SHAKE = struct
|
adamc@134
|
31
|
adamc@134
|
32 open Mono
|
adamc@134
|
33
|
adamc@134
|
34 structure U = MonoUtil
|
adamc@134
|
35
|
adamc@134
|
36 structure IS = IntBinarySet
|
adamc@134
|
37 structure IM = IntBinaryMap
|
adamc@134
|
38
|
adamc@134
|
39 type free = {
|
adamc@134
|
40 con : IS.set,
|
adamc@134
|
41 exp : IS.set
|
adamc@134
|
42 }
|
adamc@134
|
43
|
adamc@134
|
44 fun shake file =
|
adamc@134
|
45 let
|
adamc@1073
|
46 val usedVars = U.Exp.fold {typ = fn (c, st as (cs, es)) =>
|
adamc@1073
|
47 case c of
|
adamc@1073
|
48 TDatatype (n, _) => (IS.add (cs, n), es)
|
adamc@1073
|
49 | _ => st,
|
adamc@1073
|
50 exp = fn (e, st as (cs, es)) =>
|
adamc@1073
|
51 case e of
|
adamc@1073
|
52 ENamed n => (cs, IS.add (es, n))
|
adamc@1073
|
53 | _ => st}
|
adamc@1073
|
54
|
adamc@1073
|
55 val (page_cs, page_es) =
|
adamc@1073
|
56 List.foldl
|
adamc@1104
|
57 (fn ((DExport (_, _, n, _, _, _), _), (page_cs, page_es)) => (page_cs, IS.add (page_es, n))
|
adamc@1073
|
58 | ((DDatabase {expunge = n1, initialize = n2, ...}, _), (page_cs, page_es)) =>
|
adamc@1073
|
59 (page_cs, IS.addList (page_es, [n1, n2]))
|
adamc@1075
|
60 | ((DTask (e1, e2), _), st) => usedVars (usedVars st e2) e1
|
adamc@1199
|
61 | ((DPolicy pol, _), st) =>
|
adamc@1199
|
62 let
|
adamc@1199
|
63 val e1 = case pol of
|
adamc@1214
|
64 PolClient e1 => e1
|
adamc@1220
|
65 | PolInsert e1 => e1
|
adamc@1199
|
66 in
|
adamc@1199
|
67 usedVars st e1
|
adamc@1199
|
68 end
|
adamc@1073
|
69 | (_, st) => st) (IS.empty, IS.empty) file
|
adamc@134
|
70
|
adamc@808
|
71 val (cdef, edef) = foldl (fn ((DDatatype dts, _), (cdef, edef)) =>
|
adamc@808
|
72 (foldl (fn ((_, n, xncs), cdef) => IM.insert (cdef, n, xncs)) cdef dts, edef)
|
adamc@178
|
73 | ((DVal (_, n, t, e, _), _), (cdef, edef)) =>
|
adamc@178
|
74 (cdef, IM.insert (edef, n, (t, e)))
|
adamc@134
|
75 | ((DValRec vis, _), (cdef, edef)) =>
|
adamc@134
|
76 (cdef, foldl (fn ((_, n, t, e, _), edef) => IM.insert (edef, n, (t, e))) edef vis)
|
adamc@271
|
77 | ((DExport _, _), acc) => acc
|
adamc@273
|
78 | ((DTable _, _), acc) => acc
|
adamc@338
|
79 | ((DSequence _, _), acc) => acc
|
adamc@754
|
80 | ((DView _, _), acc) => acc
|
adamc@569
|
81 | ((DDatabase _, _), acc) => acc
|
adamc@718
|
82 | ((DJavaScript _, _), acc) => acc
|
adamc@725
|
83 | ((DCookie _, _), acc) => acc
|
adamc@1073
|
84 | ((DStyle _, _), acc) => acc
|
adamc@1199
|
85 | ((DTask _, _), acc) => acc
|
adamc@1199
|
86 | ((DPolicy _, _), acc) => acc)
|
adamc@134
|
87 (IM.empty, IM.empty) file
|
adamc@134
|
88
|
adamc@164
|
89 fun typ (c, s) =
|
adamc@164
|
90 case c of
|
adamc@196
|
91 TDatatype (n, _) =>
|
adamc@164
|
92 if IS.member (#con s, n) then
|
adamc@164
|
93 s
|
adamc@164
|
94 else
|
adamc@178
|
95 let
|
adamc@178
|
96 val s' = {exp = #exp s,
|
adamc@178
|
97 con = IS.add (#con s, n)}
|
adamc@178
|
98 in
|
adamc@178
|
99 case IM.find (cdef, n) of
|
adamc@178
|
100 NONE => s'
|
adamc@178
|
101 | SOME xncs => foldl (fn ((_, _, to), s) =>
|
adamc@178
|
102 case to of
|
adamc@178
|
103 NONE => s
|
adamc@178
|
104 | SOME t => shakeTyp s t)
|
adamc@178
|
105 s' xncs
|
adamc@178
|
106 end
|
adamc@164
|
107 | _ => s
|
adamc@134
|
108
|
adamc@178
|
109 and shakeTyp s = U.Typ.fold typ s
|
adamc@178
|
110
|
adamc@134
|
111 fun exp (e, s) =
|
adamc@134
|
112 case e of
|
adamc@134
|
113 ENamed n =>
|
adamc@134
|
114 if IS.member (#exp s, n) then
|
adamc@134
|
115 s
|
adamc@134
|
116 else
|
adamc@134
|
117 let
|
adamc@134
|
118 val s' = {exp = IS.add (#exp s, n),
|
adamc@134
|
119 con = #con s}
|
adamc@134
|
120 in
|
adamc@134
|
121 case IM.find (edef, n) of
|
adamc@134
|
122 NONE => s'
|
adamc@134
|
123 | SOME (t, e) => shakeExp s' e
|
adamc@134
|
124 end
|
adamc@134
|
125 | _ => s
|
adamc@134
|
126
|
adamc@134
|
127 and shakeExp s = U.Exp.fold {typ = typ, exp = exp} s
|
adamc@134
|
128
|
adamc@1073
|
129 val s = {con = page_cs, exp = page_es}
|
adamc@134
|
130
|
adamc@1073
|
131 val s = IS.foldl (fn (n, s) =>
|
adamc@1073
|
132 case IM.find (cdef, n) of
|
adamc@1073
|
133 NONE => raise Fail "MonoShake: Couldn't find 'datatype'"
|
adamc@1073
|
134 | SOME xncs => foldl (fn ((_, _, SOME c), s) => shakeTyp s c
|
adamc@1073
|
135 | _ => s) s xncs) s page_cs
|
adamc@1073
|
136
|
adamc@1073
|
137 val s = IS.foldl (fn (n, s) =>
|
adamc@1073
|
138 case IM.find (edef, n) of
|
adamc@1073
|
139 NONE => raise Fail "MonoShake: Couldn't find 'val'"
|
adamc@1073
|
140 | SOME (t, e) => shakeExp s e) s page_es
|
adamc@134
|
141 in
|
adamc@808
|
142 List.filter (fn (DDatatype dts, _) => List.exists (fn (_, n, _) => IS.member (#con s, n)) dts
|
adamc@164
|
143 | (DVal (_, n, _, _, _), _) => IS.member (#exp s, n)
|
adamc@134
|
144 | (DValRec vis, _) => List.exists (fn (_, n, _, _, _) => IS.member (#exp s, n)) vis
|
adamc@271
|
145 | (DExport _, _) => true
|
adamc@273
|
146 | (DTable _, _) => true
|
adamc@338
|
147 | (DSequence _, _) => true
|
adamc@754
|
148 | (DView _, _) => true
|
adamc@569
|
149 | (DDatabase _, _) => true
|
adamc@718
|
150 | (DJavaScript _, _) => true
|
adamc@725
|
151 | (DCookie _, _) => true
|
adamc@1073
|
152 | (DStyle _, _) => true
|
adamc@1199
|
153 | (DTask _, _) => true
|
adamc@1199
|
154 | (DPolicy _, _) => true) file
|
adamc@134
|
155 end
|
adamc@134
|
156
|
adamc@134
|
157 end
|