Mercurial > urweb
comparison src/dbmodecheck.sml @ 2056:a9159911c3ba
New phase: Dbmodecheck
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Sun, 17 Aug 2014 13:07:56 -0400 |
parents | |
children | 25874084bf1f |
comparison
equal
deleted
inserted
replaced
2055:7c2229aa22fc | 2056:a9159911c3ba |
---|---|
1 (* Copyright (c) 2014, Adam Chlipala | |
2 * All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are met: | |
6 * | |
7 * - Redistributions of source code must retain the above copyright notice, | |
8 * this list of conditions and the following disclaimer. | |
9 * - Redistributions in binary form must reproduce the above copyright notice, | |
10 * this list of conditions and the following disclaimer in the documentation | |
11 * and/or other materials provided with the distribution. | |
12 * - The names of contributors may not be used to endorse or promote products | |
13 * derived from this software without specific prior written permission. | |
14 * | |
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 * POSSIBILITY OF SUCH DAMAGE. | |
26 *) | |
27 | |
28 structure DbModeCheck :> DB_MODE_CHECK = struct | |
29 | |
30 open Mono | |
31 | |
32 structure IM = IntBinaryMap | |
33 | |
34 fun classify (ds, ps) = | |
35 let | |
36 fun mergeModes (m1, m2) = | |
37 case (m1, m2) of | |
38 (NoDb, _) => m2 | |
39 | (_, NoDb) => m1 | |
40 | _ => AnyDb | |
41 | |
42 fun modeOf modes = | |
43 MonoUtil.Exp.fold {typ = fn (_, dbm) => dbm, | |
44 exp = fn (EQuery _, dbm) => mergeModes (OneQuery, dbm) | |
45 | (EDml _, _) => AnyDb | |
46 | (ENextval _, _) => AnyDb | |
47 | (ESetval _, _) => AnyDb | |
48 | (ENamed n, dbm) => | |
49 (case IM.find (modes, n) of | |
50 NONE => dbm | |
51 | SOME dbm' => mergeModes (dbm, dbm')) | |
52 | (_, dbm) => dbm} NoDb | |
53 | |
54 fun decl ((d, _), modes) = | |
55 case d of | |
56 DVal (x, n, _, e, _) => IM.insert (modes, n, modeOf modes e) | |
57 | DValRec xes => | |
58 let | |
59 val mode = foldl (fn ((_, _, _, e, _), mode) => | |
60 let | |
61 val mode' = modeOf modes e | |
62 in | |
63 case mode' of | |
64 NoDb => mode | |
65 | _ => AnyDb | |
66 end) NoDb xes | |
67 in | |
68 foldl (fn ((_, n, _, _, _), modes) => IM.insert (modes, n, mode)) modes xes | |
69 end | |
70 | _ => modes | |
71 | |
72 val modes = foldl decl IM.empty ds | |
73 | |
74 val (ps, modes) = ListUtil.foldlMap (fn ((n, side, _), modes) => | |
75 case IM.find (modes, n) of | |
76 NONE => ((n, side, AnyDb), modes) | |
77 | SOME mode => ((n, side, mode), #1 (IM.remove (modes, n)))) | |
78 modes ps | |
79 | |
80 val ps = IM.foldli (fn (n, mode, ps) => (n, ServerOnly, mode) :: ps) ps modes | |
81 in | |
82 (ds, ps) | |
83 end | |
84 | |
85 end | |
86 |