adamc@732: (* Copyright (c) 2009, Adam Chlipala adamc@732: * All rights reserved. adamc@732: * adamc@732: * Redistribution and use in source and binary forms, with or without adamc@732: * modification, are permitted provided that the following conditions are met: adamc@732: * adamc@732: * - Redistributions of source code must retain the above copyright notice, adamc@732: * this list of conditions and the following disclaimer. adamc@732: * - Redistributions in binary form must reproduce the above copyright notice, adamc@732: * this list of conditions and the following disclaimer in the documentation adamc@732: * and/or other materials provided with the distribution. adamc@732: * - The names of contributors may not be used to endorse or promote products adamc@732: * derived from this software without specific prior written permission. adamc@732: * adamc@732: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@732: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@732: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@732: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@732: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@732: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@732: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@732: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@732: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@732: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@732: * POSSIBILITY OF SUCH DAMAGE. adamc@732: *) adamc@732: adamc@732: structure Effective :> EFFECTIZE = struct adamc@732: adamc@732: open Core adamc@732: adamc@732: structure U = CoreUtil adamc@732: adamc@732: structure IM = IntBinaryMap adamc@732: structure SS = BinarySetFn(struct adamc@732: type ord_key = string adamc@732: val compare = String.compare adamc@732: end) adamc@732: adamc@732: val effectful = ["dml", "nextval", "send"] adamc@732: val effectful = SS.addList (SS.empty, effectful) adamc@732: adamc@732: fun effectize file = adamc@732: let adamc@732: fun exp evs e = adamc@732: case e of adamc@732: EFfi ("Basis", s) => SS.member (effectful, s) adamc@732: | EFfiApp ("Basis", s, _) => SS.member (effectful, s) adamc@732: | ENamed n => IM.inDomain (evs, n) adamc@732: | EServerCall (n, _, _, _) => IM.inDomain (evs, n) adamc@732: | _ => false adamc@732: adamc@732: fun couldWrite evs = U.Exp.exists {kind = fn _ => false, adamc@732: con = fn _ => false, adamc@732: exp = exp evs} adamc@732: adamc@732: fun doDecl (d, evs) = adamc@732: case #1 d of adamc@732: DVal (x, n, t, e, s) => adamc@732: (d, if couldWrite evs e then adamc@732: IM.insert (evs, n, (#2 d, s)) adamc@732: else adamc@732: evs) adamc@732: | DValRec vis => adamc@732: let adamc@732: fun oneRound evs = adamc@732: foldl (fn ((_, n, _, e, s), (changed, evs)) => adamc@732: if couldWrite evs e andalso not (IM.inDomain (evs, n)) then adamc@732: (true, IM.insert (evs, n, (#2 d, s))) adamc@732: else adamc@732: (changed, evs)) (false, evs) vis adamc@732: adamc@732: fun loop evs = adamc@732: let adamc@732: val (b, evs) = oneRound evs adamc@732: in adamc@732: if b then adamc@732: loop evs adamc@732: else adamc@732: evs adamc@732: end adamc@732: in adamc@732: (d, loop evs) adamc@732: end adamc@732: | DExport (Link, n) => adamc@732: (case IM.find (evs, n) of adamc@732: NONE => () adamc@732: | SOME (loc, s) => ErrorMsg.errorAt loc ("A link (" ^ s ^ ") could cause side effects; try implementing it with a form instead"); adamc@732: (d, evs)) adamc@732: | DExport (Action _, n) => adamc@732: ((DExport (Action (if IM.inDomain (evs, n) then adamc@732: ReadWrite adamc@732: else adamc@732: ReadOnly), n), #2 d), adamc@732: evs) adamc@732: | DExport (Rpc _, n) => adamc@732: ((DExport (Rpc (if IM.inDomain (evs, n) then adamc@732: ReadWrite adamc@732: else adamc@732: ReadOnly), n), #2 d), adamc@732: evs) adamc@732: | _ => (d, evs) adamc@732: adamc@732: val (file, _) = ListUtil.foldlMap doDecl IM.empty file adamc@732: in adamc@732: file adamc@732: end adamc@732: adamc@732: end