adamc@732
|
1 (* Copyright (c) 2009, Adam Chlipala
|
adamc@732
|
2 * All rights reserved.
|
adamc@732
|
3 *
|
adamc@732
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@732
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@732
|
6 *
|
adamc@732
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@732
|
8 * this list of conditions and the following disclaimer.
|
adamc@732
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@732
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@732
|
11 * and/or other materials provided with the distribution.
|
adamc@732
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@732
|
13 * derived from this software without specific prior written permission.
|
adamc@732
|
14 *
|
adamc@732
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@732
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@732
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@732
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@732
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@732
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@732
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@732
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@732
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@732
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@732
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@732
|
26 *)
|
adamc@732
|
27
|
adamc@732
|
28 structure Effective :> EFFECTIZE = struct
|
adamc@732
|
29
|
adamc@732
|
30 open Core
|
adamc@732
|
31
|
adamc@732
|
32 structure U = CoreUtil
|
adamc@732
|
33
|
adamc@732
|
34 structure IM = IntBinaryMap
|
adamc@732
|
35 structure SS = BinarySetFn(struct
|
adamc@732
|
36 type ord_key = string
|
adamc@732
|
37 val compare = String.compare
|
adamc@732
|
38 end)
|
adamc@732
|
39
|
adamc@765
|
40 fun effectful x = Settings.isEffectful x andalso not (Settings.isClientOnly x)
|
adamc@732
|
41
|
adamc@732
|
42 fun effectize file =
|
adamc@732
|
43 let
|
adamc@732
|
44 fun exp evs e =
|
adamc@732
|
45 case e of
|
adamc@765
|
46 EFfi f => effectful f
|
adamc@765
|
47 | EFfiApp (m, x, _) => effectful (m, x)
|
adamc@732
|
48 | ENamed n => IM.inDomain (evs, n)
|
adamc@732
|
49 | EServerCall (n, _, _, _) => IM.inDomain (evs, n)
|
adamc@732
|
50 | _ => false
|
adamc@732
|
51
|
adamc@732
|
52 fun couldWrite evs = U.Exp.exists {kind = fn _ => false,
|
adamc@732
|
53 con = fn _ => false,
|
adamc@732
|
54 exp = exp evs}
|
adamc@732
|
55
|
adamc@735
|
56 fun exp evs e =
|
adamc@735
|
57 case e of
|
adamc@735
|
58 EFfi ("Basis", "getCookie") => true
|
adamc@735
|
59 | ENamed n => IM.inDomain (evs, n)
|
adamc@735
|
60 | EServerCall (n, _, _, _) => IM.inDomain (evs, n)
|
adamc@735
|
61 | _ => false
|
adamc@735
|
62
|
adamc@735
|
63 fun couldReadCookie evs = U.Exp.exists {kind = fn _ => false,
|
adamc@735
|
64 con = fn _ => false,
|
adamc@735
|
65 exp = exp evs}
|
adamc@735
|
66
|
adamc@735
|
67 fun doDecl (d, evs as (writers, readers)) =
|
adamc@732
|
68 case #1 d of
|
adamc@732
|
69 DVal (x, n, t, e, s) =>
|
adamc@735
|
70 (d, (if couldWrite writers e then
|
adamc@735
|
71 IM.insert (writers, n, (#2 d, s))
|
adamc@735
|
72 else
|
adamc@735
|
73 writers,
|
adamc@735
|
74 if couldReadCookie readers e then
|
adamc@735
|
75 IM.insert (readers, n, (#2 d, s))
|
adamc@735
|
76 else
|
adamc@735
|
77 readers))
|
adamc@732
|
78 | DValRec vis =>
|
adamc@732
|
79 let
|
adamc@732
|
80 fun oneRound evs =
|
adamc@735
|
81 foldl (fn ((_, n, _, e, s), (changed, (writers, readers))) =>
|
adamc@735
|
82 let
|
adamc@735
|
83 val (changed, writers) =
|
adamc@735
|
84 if couldWrite writers e andalso not (IM.inDomain (writers, n)) then
|
adamc@735
|
85 (true, IM.insert (writers, n, (#2 d, s)))
|
adamc@735
|
86 else
|
adamc@735
|
87 (changed, writers)
|
adamc@735
|
88
|
adamc@735
|
89 val (changed, readers) =
|
adamc@735
|
90 if couldReadCookie readers e andalso not (IM.inDomain (readers, n)) then
|
adamc@735
|
91 (true, IM.insert (readers, n, (#2 d, s)))
|
adamc@735
|
92 else
|
adamc@735
|
93 (changed, readers)
|
adamc@735
|
94 in
|
adamc@735
|
95 (changed, (writers, readers))
|
adamc@735
|
96 end) (false, evs) vis
|
adamc@732
|
97
|
adamc@732
|
98 fun loop evs =
|
adamc@732
|
99 let
|
adamc@732
|
100 val (b, evs) = oneRound evs
|
adamc@732
|
101 in
|
adamc@732
|
102 if b then
|
adamc@732
|
103 loop evs
|
adamc@732
|
104 else
|
adamc@732
|
105 evs
|
adamc@732
|
106 end
|
adamc@732
|
107 in
|
adamc@735
|
108 (d, loop (writers, readers))
|
adamc@732
|
109 end
|
adamc@732
|
110 | DExport (Link, n) =>
|
adamc@735
|
111 (case IM.find (writers, n) of
|
adamc@732
|
112 NONE => ()
|
adamc@732
|
113 | SOME (loc, s) => ErrorMsg.errorAt loc ("A link (" ^ s ^ ") could cause side effects; try implementing it with a form instead");
|
adamc@732
|
114 (d, evs))
|
adamc@732
|
115 | DExport (Action _, n) =>
|
adamc@735
|
116 ((DExport (Action (if IM.inDomain (writers, n) then
|
adamc@735
|
117 if IM.inDomain (readers, n) then
|
adamc@735
|
118 ReadCookieWrite
|
adamc@735
|
119 else
|
adamc@735
|
120 ReadWrite
|
adamc@732
|
121 else
|
adamc@732
|
122 ReadOnly), n), #2 d),
|
adamc@732
|
123 evs)
|
adamc@732
|
124 | DExport (Rpc _, n) =>
|
adamc@735
|
125 ((DExport (Rpc (if IM.inDomain (writers, n) then
|
adamc@735
|
126 if IM.inDomain (readers, n) then
|
adamc@735
|
127 ReadCookieWrite
|
adamc@735
|
128 else
|
adamc@735
|
129 ReadWrite
|
adamc@732
|
130 else
|
adamc@732
|
131 ReadOnly), n), #2 d),
|
adamc@732
|
132 evs)
|
adamc@732
|
133 | _ => (d, evs)
|
adamc@732
|
134
|
adamc@735
|
135 val (file, _) = ListUtil.foldlMap doDecl (IM.empty, IM.empty) file
|
adamc@732
|
136 in
|
adamc@732
|
137 file
|
adamc@732
|
138 end
|
adamc@732
|
139
|
adamc@732
|
140 end
|