annotate src/rpcify.sml @ 669:f68eee90dbcf

Press a button to send a message
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Mar 2009 15:16:34 -0400
parents b0c1a46b1f15
children f73913d97a40
rev   line source
adamc@607 1 (* Copyright (c) 2009, Adam Chlipala
adamc@607 2 * All rights reserved.
adamc@607 3 *
adamc@607 4 * Redistribution and use in source and binary forms, with or without
adamc@607 5 * modification, are permitted provided that the following conditions are met:
adamc@607 6 *
adamc@607 7 * - Redistributions of source code must retain the above copyright notice,
adamc@607 8 * this list of conditions and the following disclaimer.
adamc@607 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@607 10 * this list of conditions and the following disclaimer in the documentation
adamc@607 11 * and/or other materials provided with the distribution.
adamc@607 12 * - The names of contributors may not be used to endorse or promote products
adamc@607 13 * derived from this software without specific prior written permission.
adamc@607 14 *
adamc@607 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@607 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@607 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@607 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@607 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@607 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@607 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@607 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@607 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@607 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@607 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@607 26 *)
adamc@607 27
adamc@607 28 structure Rpcify :> RPCIFY = struct
adamc@607 29
adamc@607 30 open Core
adamc@607 31
adamc@607 32 structure U = CoreUtil
adamc@607 33 structure E = CoreEnv
adamc@607 34
adamc@607 35 structure IS = IntBinarySet
adamc@607 36 structure IM = IntBinaryMap
adamc@607 37
adamc@607 38 structure SS = BinarySetFn(struct
adamc@607 39 type ord_key = string
adamc@607 40 val compare = String.compare
adamc@607 41 end)
adamc@607 42
adamc@642 43 fun multiLiftExpInExp n e =
adamc@642 44 if n = 0 then
adamc@642 45 e
adamc@642 46 else
adamc@642 47 multiLiftExpInExp (n - 1) (E.liftExpInExp 0 e)
adamc@642 48
adamc@607 49 val ssBasis = SS.addList (SS.empty,
adamc@607 50 ["requestHeader",
adamc@607 51 "query",
adamc@607 52 "dml",
adamc@668 53 "nextval",
adamc@668 54 "new_channel",
adamc@668 55 "subscribe",
adamc@668 56 "send"])
adamc@607 57
adamc@607 58 val csBasis = SS.addList (SS.empty,
adamc@607 59 ["source",
adamc@607 60 "get",
adamc@607 61 "set",
adamc@607 62 "alert"])
adamc@607 63
adamc@607 64 type state = {
adamc@608 65 cpsed : int IM.map,
adamc@642 66 cpsed_range : con IM.map,
adamc@608 67 cps_decls : (string * int * con * exp * string) list,
adamc@608 68
adamc@608 69 exported : IS.set,
adamc@642 70 export_decls : decl list,
adamc@642 71
adamc@642 72 maxName : int
adamc@607 73 }
adamc@607 74
adamc@607 75 fun frob file =
adamc@607 76 let
adamc@650 77 fun sideish (basis, ssids) e =
adamc@650 78 case #1 e of
adamc@650 79 ERecord _ => false
adamc@650 80 | _ =>
adamc@650 81 U.Exp.exists {kind = fn _ => false,
adamc@650 82 con = fn _ => false,
adamc@650 83 exp = fn ENamed n => IS.member (ssids, n)
adamc@650 84 | EFfi ("Basis", x) => SS.member (basis, x)
adamc@650 85 | EFfiApp ("Basis", x, _) => SS.member (basis, x)
adamc@650 86 | _ => false} e
adamc@607 87
adamc@607 88 fun whichIds basis =
adamc@607 89 let
adamc@607 90 fun decl ((d, _), ssids) =
adamc@607 91 let
adamc@607 92 val impure = sideish (basis, ssids)
adamc@607 93 in
adamc@607 94 case d of
adamc@607 95 DVal (_, n, _, e, _) => if impure e then
adamc@607 96 IS.add (ssids, n)
adamc@607 97 else
adamc@607 98 ssids
adamc@607 99 | DValRec xes => if List.exists (fn (_, _, _, e, _) => impure e) xes then
adamc@607 100 foldl (fn ((_, n, _, _, _), ssids) => IS.add (ssids, n))
adamc@607 101 ssids xes
adamc@607 102 else
adamc@607 103 ssids
adamc@607 104 | _ => ssids
adamc@607 105 end
adamc@607 106 in
adamc@607 107 foldl decl IS.empty file
adamc@607 108 end
adamc@607 109
adamc@607 110 val ssids = whichIds ssBasis
adamc@607 111 val csids = whichIds csBasis
adamc@607 112
adamc@642 113 fun sideish' (basis, ids) extra =
adamc@642 114 sideish (basis, IM.foldli (fn (id, _, ids) => IS.add (ids, id)) ids extra)
adamc@642 115
adamc@642 116 val serverSide = sideish' (ssBasis, ssids)
adamc@642 117 val clientSide = sideish' (csBasis, csids)
adamc@607 118
adamc@609 119 val tfuncs = foldl
adamc@609 120 (fn ((d, _), tfuncs) =>
adamc@609 121 let
adamc@642 122 fun doOne ((x, n, t, e, _), tfuncs) =
adamc@609 123 let
adamc@642 124 val loc = #2 e
adamc@642 125
adamc@642 126 fun crawl (t, e, args) =
adamc@642 127 case (#1 t, #1 e) of
adamc@642 128 (CApp (_, ran), _) =>
adamc@642 129 SOME (x, rev args, ran, e)
adamc@642 130 | (TFun (arg, rest), EAbs (x, _, _, e)) =>
adamc@642 131 crawl (rest, e, (x, arg) :: args)
adamc@642 132 | (TFun (arg, rest), _) =>
adamc@642 133 crawl (rest, (EApp (e, (ERel (length args), loc)), loc), ("x", arg) :: args)
adamc@609 134 | _ => NONE
adamc@609 135 in
adamc@642 136 case crawl (t, e, []) of
adamc@609 137 NONE => tfuncs
adamc@609 138 | SOME sg => IM.insert (tfuncs, n, sg)
adamc@609 139 end
adamc@609 140 in
adamc@609 141 case d of
adamc@609 142 DVal vi => doOne (vi, tfuncs)
adamc@609 143 | DValRec vis => foldl doOne tfuncs vis
adamc@609 144 | _ => tfuncs
adamc@609 145 end)
adamc@609 146 IM.empty file
adamc@609 147
adamc@607 148 fun exp (e, st) =
adamc@649 149 let
adamc@649 150 fun getApp (e', args) =
adamc@649 151 let
adamc@649 152 val loc = #2 e'
adamc@649 153 in
adamc@642 154 case #1 e' of
adamc@642 155 ENamed n => (n, args)
adamc@642 156 | EApp (e1, e2) => getApp (e1, e2 :: args)
adamc@642 157 | _ => (ErrorMsg.errorAt loc "Mixed client/server code doesn't use a named function for server part";
adamc@642 158 Print.prefaces "Bad" [("e", CorePrint.p_exp CoreEnv.empty (e, ErrorMsg.dummySpan))];
adamc@642 159 (0, []))
adamc@649 160 end
adamc@642 161
adamc@649 162 fun newRpc (trans1, trans2, st : state) =
adamc@649 163 let
adamc@649 164 val loc = #2 trans1
adamc@642 165
adamc@649 166 val (n, args) = getApp (trans1, [])
adamc@642 167
adamc@649 168 val (exported, export_decls) =
adamc@649 169 if IS.member (#exported st, n) then
adamc@649 170 (#exported st, #export_decls st)
adamc@649 171 else
adamc@649 172 (IS.add (#exported st, n),
adamc@649 173 (DExport (Rpc, n), loc) :: #export_decls st)
adamc@642 174
adamc@649 175 val st = {cpsed = #cpsed st,
adamc@649 176 cpsed_range = #cpsed_range st,
adamc@649 177 cps_decls = #cps_decls st,
adamc@642 178
adamc@649 179 exported = exported,
adamc@649 180 export_decls = export_decls,
adamc@642 181
adamc@649 182 maxName = #maxName st}
adamc@642 183
adamc@649 184 val ran =
adamc@649 185 case IM.find (tfuncs, n) of
adamc@649 186 NONE => (Print.prefaces "BAD" [("e", CorePrint.p_exp CoreEnv.empty (e, loc))];
adamc@649 187 raise Fail ("Rpcify: Undetected transaction function " ^ Int.toString n))
adamc@649 188 | SOME (_, _, ran, _) => ran
adamc@649 189
adamc@649 190 val e' = EServerCall (n, args, trans2, ran)
adamc@649 191 in
adamc@649 192 (e', st)
adamc@649 193 end
adamc@651 194
adamc@651 195 fun newCps (t1, t2, trans1, trans2, st) =
adamc@651 196 let
adamc@651 197 val loc = #2 trans1
adamc@651 198
adamc@651 199 val (n, args) = getApp (trans1, [])
adamc@651 200
adamc@651 201 fun makeCall n' =
adamc@651 202 let
adamc@651 203 val e = (ENamed n', loc)
adamc@651 204 val e = (EApp (e, trans2), loc)
adamc@651 205 in
adamc@651 206 #1 (foldl (fn (arg, e) => (EApp (e, arg), loc)) e args)
adamc@651 207 end
adamc@651 208 in
adamc@651 209 case IM.find (#cpsed_range st, n) of
adamc@651 210 SOME kdom =>
adamc@651 211 (case args of
adamc@651 212 [] => raise Fail "Rpcify: cps'd function lacks first argument"
adamc@651 213 | ke :: args =>
adamc@651 214 let
adamc@651 215 val ke' = (EFfi ("Basis", "bind"), loc)
adamc@651 216 val ke' = (ECApp (ke', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@651 217 val ke' = (ECApp (ke', kdom), loc)
adamc@651 218 val ke' = (ECApp (ke', t2), loc)
adamc@651 219 val ke' = (EApp (ke', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@651 220 val ke' = (EApp (ke', (EApp (E.liftExpInExp 0 ke, (ERel 0, loc)), loc)), loc)
adamc@651 221 val ke' = (EApp (ke', E.liftExpInExp 0 trans2), loc)
adamc@651 222 val ke' = (EAbs ("x", kdom,
adamc@651 223 (CApp ((CFfi ("Basis", "transaction"), loc), t2), loc),
adamc@651 224 ke'), loc)
adamc@651 225
adamc@651 226 val e' = (ENamed n, loc)
adamc@651 227 val e' = (EApp (e', ke'), loc)
adamc@651 228 val e' = foldl (fn (arg, e') => (EApp (e', arg), loc)) e' args
adamc@651 229 val (e', st) = doExp (e', st)
adamc@651 230 in
adamc@651 231 (#1 e', st)
adamc@651 232 end)
adamc@651 233 | NONE =>
adamc@651 234 case IM.find (#cpsed st, n) of
adamc@651 235 SOME n' => (makeCall n', st)
adamc@651 236 | NONE =>
adamc@651 237 let
adamc@651 238 val (name, fargs, ran, e) =
adamc@651 239 case IM.find (tfuncs, n) of
adamc@651 240 NONE => (Print.prefaces "BAD" [("e",
adamc@651 241 CorePrint.p_exp CoreEnv.empty (e, loc))];
adamc@651 242 raise Fail "Rpcify: Undetected transaction function [2]")
adamc@651 243 | SOME x => x
adamc@651 244
adamc@651 245 val n' = #maxName st
adamc@651 246
adamc@651 247 val st = {cpsed = IM.insert (#cpsed st, n, n'),
adamc@651 248 cpsed_range = IM.insert (#cpsed_range st, n', ran),
adamc@651 249 cps_decls = #cps_decls st,
adamc@651 250 exported = #exported st,
adamc@651 251 export_decls = #export_decls st,
adamc@651 252 maxName = n' + 1}
adamc@651 253
adamc@651 254 val unit = (TRecord (CRecord ((KType, loc), []), loc), loc)
adamc@651 255 val body = (EFfi ("Basis", "bind"), loc)
adamc@651 256 val body = (ECApp (body, (CFfi ("Basis", "transaction"), loc)), loc)
adamc@651 257 val body = (ECApp (body, t1), loc)
adamc@651 258 val body = (ECApp (body, unit), loc)
adamc@651 259 val body = (EApp (body, (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@651 260 val body = (EApp (body, e), loc)
adamc@651 261 val body = (EApp (body, (ERel (length args), loc)), loc)
adamc@651 262 val bt = (CApp ((CFfi ("Basis", "transaction"), loc), unit), loc)
adamc@651 263 val (body, bt) = foldr (fn ((x, t), (body, bt)) =>
adamc@651 264 ((EAbs (x, t, bt, body), loc),
adamc@651 265 (TFun (t, bt), loc)))
adamc@651 266 (body, bt) fargs
adamc@651 267 val kt = (TFun (ran, (CApp ((CFfi ("Basis", "transaction"), loc),
adamc@651 268 unit),
adamc@651 269 loc)), loc)
adamc@651 270 val body = (EAbs ("k", kt, bt, body), loc)
adamc@651 271 val bt = (TFun (kt, bt), loc)
adamc@651 272
adamc@651 273 val (body, st) = doExp (body, st)
adamc@651 274
adamc@651 275 val vi = (name ^ "_cps",
adamc@651 276 n',
adamc@651 277 bt,
adamc@651 278 body,
adamc@651 279 "")
adamc@651 280
adamc@651 281 val st = {cpsed = #cpsed st,
adamc@651 282 cpsed_range = #cpsed_range st,
adamc@651 283 cps_decls = vi :: #cps_decls st,
adamc@651 284 exported = #exported st,
adamc@651 285 export_decls = #export_decls st,
adamc@651 286 maxName = #maxName st}
adamc@651 287 in
adamc@651 288 (makeCall n', st)
adamc@651 289 end
adamc@651 290 end
adamc@651 291
adamc@651 292 fun dummyK loc =
adamc@651 293 let
adamc@651 294 val unit = (TRecord (CRecord ((KType, loc), []), loc), loc)
adamc@651 295
adamc@651 296 val k = (EFfi ("Basis", "return"), loc)
adamc@651 297 val k = (ECApp (k, (CFfi ("Basis", "transaction"), loc)), loc)
adamc@651 298 val k = (ECApp (k, unit), loc)
adamc@651 299 val k = (EApp (k, (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@651 300 val k = (EApp (k, (ERecord [], loc)), loc)
adamc@651 301 in
adamc@651 302 (EAbs ("_", unit, unit, k), loc)
adamc@651 303 end
adamc@649 304 in
adamc@649 305 case e of
adamc@649 306 EApp (
adamc@649 307 (EApp
adamc@649 308 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1), _), t2), _),
adamc@649 309 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@649 310 (ECase (ed, pes, {disc, ...}), _)), _),
adamc@649 311 trans2) =>
adamc@649 312 let
adamc@649 313 val e' = (EFfi ("Basis", "bind"), loc)
adamc@649 314 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@649 315 val e' = (ECApp (e', t1), loc)
adamc@649 316 val e' = (ECApp (e', t2), loc)
adamc@649 317 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@649 318
adamc@649 319 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
adamc@649 320 let
adamc@649 321 val e' = (EApp (e', e), loc)
adamc@649 322 val e' = (EApp (e',
adamc@649 323 multiLiftExpInExp (E.patBindsN p)
adamc@649 324 trans2), loc)
adamc@649 325 val (e', st) = doExp (e', st)
adamc@649 326 in
adamc@649 327 ((p, e'), st)
adamc@649 328 end) st pes
adamc@649 329 in
adamc@649 330 (ECase (ed, pes, {disc = disc,
adamc@649 331 result = (CApp ((CFfi ("Basis", "transaction"), loc), t2), loc)}),
adamc@649 332 st)
adamc@649 333 end
adamc@649 334
adamc@649 335 | EApp (
adamc@649 336 (EApp
adamc@649 337 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1), _), t2), _),
adamc@649 338 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@649 339 (EServerCall (n, es, ke, t), _)), _),
adamc@649 340 trans2) =>
adamc@649 341 let
adamc@649 342 val e' = (EFfi ("Basis", "bind"), loc)
adamc@649 343 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@649 344 val e' = (ECApp (e', t), loc)
adamc@649 345 val e' = (ECApp (e', t2), loc)
adamc@649 346 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@649 347 val e' = (EApp (e', (EApp (E.liftExpInExp 0 ke, (ERel 0, loc)), loc)), loc)
adamc@649 348 val e' = (EApp (e', E.liftExpInExp 0 trans2), loc)
adamc@649 349 val e' = (EAbs ("x", t, t2, e'), loc)
adamc@649 350 val e' = (EServerCall (n, es, e', t), loc)
adamc@649 351 val (e', st) = doExp (e', st)
adamc@649 352 in
adamc@649 353 (#1 e', st)
adamc@649 354 end
adamc@649 355
adamc@649 356 | EApp (
adamc@649 357 (EApp
adamc@649 358 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), _), _), t3), _),
adamc@649 359 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@649 360 (EApp ((EApp
adamc@649 361 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@649 362 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@649 363 trans1), _), trans2), _)), _),
adamc@649 364 trans3) =>
adamc@649 365 let
adamc@649 366 val e'' = (EFfi ("Basis", "bind"), loc)
adamc@649 367 val e'' = (ECApp (e'', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@649 368 val e'' = (ECApp (e'', t2), loc)
adamc@649 369 val e'' = (ECApp (e'', t3), loc)
adamc@649 370 val e'' = (EApp (e'', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@649 371 val e'' = (EApp (e'', (EApp (E.liftExpInExp 0 trans2, (ERel 0, loc)), loc)), loc)
adamc@649 372 val e'' = (EApp (e'', E.liftExpInExp 0 trans3), loc)
adamc@649 373 val e'' = (EAbs ("x", t1, (CApp ((CFfi ("Basis", "transaction"), loc), t3), loc), e''), loc)
adamc@649 374
adamc@649 375 val e' = (EFfi ("Basis", "bind"), loc)
adamc@649 376 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@649 377 val e' = (ECApp (e', t1), loc)
adamc@649 378 val e' = (ECApp (e', t3), loc)
adamc@649 379 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@649 380 val e' = (EApp (e', trans1), loc)
adamc@649 381 val e' = (EApp (e', e''), loc)
adamc@649 382 val (e', st) = doExp (e', st)
adamc@649 383 in
adamc@649 384 (#1 e', st)
adamc@649 385 end
adamc@649 386
adamc@649 387 | EApp (
adamc@649 388 (EApp
adamc@649 389 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), _), _), _), _), _), _), _),
adamc@649 390 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@649 391 _), loc),
adamc@649 392 (EAbs (_, _, _, (EWrite _, _)), _)) => (e, st)
adamc@649 393
adamc@649 394 | EApp (
adamc@649 395 (EApp
adamc@649 396 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@649 397 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@649 398 trans1), loc),
adamc@649 399 trans2) =>
adamc@649 400 (case (serverSide (#cpsed_range st) trans1, clientSide (#cpsed_range st) trans1,
adamc@649 401 serverSide (#cpsed_range st) trans2, clientSide (#cpsed_range st) trans2) of
adamc@649 402 (true, false, _, true) => newRpc (trans1, trans2, st)
adamc@651 403 | (_, true, true, false) =>
adamc@651 404 (case #1 trans2 of
adamc@651 405 EAbs (x, dom, ran, trans2) =>
adamc@651 406 let
adamc@651 407 val (trans2, st) = newRpc (trans2, dummyK loc, st)
adamc@651 408 val trans2 = (EAbs (x, dom, ran, (trans2, loc)), loc)
adamc@649 409
adamc@651 410 val e = (EFfi ("Basis", "bind"), loc)
adamc@651 411 val e = (ECApp (e, (CFfi ("Basis", "transaction"), loc)), loc)
adamc@651 412 val e = (ECApp (e, t1), loc)
adamc@651 413 val e = (ECApp (e, t2), loc)
adamc@651 414 val e = (EApp (e, (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@651 415 val e = (EApp (e, trans1), loc)
adamc@651 416 val e = EApp (e, trans2)
adamc@651 417 in
adamc@651 418 (e, st)
adamc@651 419 end
adamc@651 420 | _ => (e, st))
adamc@651 421 | (true, true, _, _) => newCps (t1, t2, trans1, trans2, st)
adamc@649 422
adamc@649 423 | _ => (e, st))
adamc@642 424
adamc@649 425 | ERecord xes =>
adamc@649 426 let
adamc@649 427 val loc = case xes of
adamc@649 428 [] => ErrorMsg.dummySpan
adamc@649 429 | (_, (_, loc), _) :: _ => loc
adamc@642 430
adamc@649 431 fun candidate (x, e) =
adamc@649 432 String.isPrefix "On" x
adamc@649 433 andalso serverSide (#cpsed_range st) e
adamc@649 434 andalso not (clientSide (#cpsed_range st) e)
adamc@649 435 in
adamc@649 436 if List.exists (fn ((CName x, _), e, _) => candidate (x, e)
adamc@649 437 | _ => false) xes then
adamc@649 438 let
adamc@649 439 val (xes, st) = ListUtil.foldlMap
adamc@649 440 (fn (y as (nm as (CName x, _), e, t), st) =>
adamc@649 441 if candidate (x, e) then
adamc@649 442 let
adamc@651 443 val (e, st) = newRpc (e, dummyK loc, st)
adamc@649 444 in
adamc@649 445 ((nm, (e, loc), t), st)
adamc@649 446 end
adamc@649 447 else
adamc@649 448 (y, st)
adamc@649 449 | y => y)
adamc@649 450 st xes
adamc@649 451 in
adamc@649 452 (ERecord xes, st)
adamc@649 453 end
adamc@649 454 else
adamc@649 455 (e, st)
adamc@649 456 end
adamc@642 457
adamc@649 458 | _ => (e, st)
adamc@649 459 end
adamc@607 460
adamc@642 461 and doExp (e, st) = U.Exp.foldMap {kind = fn x => x,
adamc@642 462 con = fn x => x,
adamc@642 463 exp = exp} st (ReduceLocal.reduceExp e)
adamc@642 464
adamc@607 465 fun decl (d, st : state) =
adamc@607 466 let
adamc@607 467 val (d, st) = U.Decl.foldMap {kind = fn x => x,
adamc@607 468 con = fn x => x,
adamc@607 469 exp = exp,
adamc@607 470 decl = fn x => x}
adamc@607 471 st d
adamc@607 472 in
adamc@608 473 (List.revAppend (case #cps_decls st of
adamc@608 474 [] => [d]
adamc@608 475 | ds =>
adamc@608 476 case d of
adamc@608 477 (DValRec vis, loc) => [(DValRec (ds @ vis), loc)]
adamc@608 478 | (_, loc) => [d, (DValRec ds, loc)],
adamc@608 479 #export_decls st),
adamc@608 480 {cpsed = #cpsed st,
adamc@642 481 cpsed_range = #cpsed_range st,
adamc@608 482 cps_decls = [],
adamc@608 483
adamc@608 484 exported = #exported st,
adamc@642 485 export_decls = [],
adamc@642 486
adamc@642 487 maxName = #maxName st})
adamc@607 488 end
adamc@607 489
adamc@607 490 val (file, _) = ListUtil.foldlMapConcat decl
adamc@608 491 {cpsed = IM.empty,
adamc@642 492 cpsed_range = IM.empty,
adamc@608 493 cps_decls = [],
adamc@608 494
adamc@608 495 exported = IS.empty,
adamc@642 496 export_decls = [],
adamc@642 497
adamc@642 498 maxName = U.File.maxName file + 1}
adamc@607 499 file
adamc@607 500 in
adamc@607 501 file
adamc@607 502 end
adamc@607 503
adamc@607 504 end