annotate src/cjr_print.sml @ 735:5ccb67665d05

Only use cookie signatures when cookies might be read
author Adam Chlipala <adamc@hcoop.net>
date Thu, 23 Apr 2009 14:10:10 -0400
parents f2a2be93331c
children 796e42c93c48
rev   line source
adamc@643 1 (* Copyright (c) 2008-2009, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 (* Pretty-printing C jr. *)
adamc@29 29
adamc@29 30 structure CjrPrint :> CJR_PRINT = struct
adamc@29 31
adamc@29 32 open Print.PD
adamc@29 33 open Print
adamc@29 34
adamc@29 35 open Cjr
adamc@29 36
adamc@269 37 val dummyt = (TRecord 0, ErrorMsg.dummySpan)
adamc@269 38
adamc@29 39 structure E = CjrEnv
adamc@29 40 structure EM = ErrorMsg
adamc@29 41
adamc@144 42 structure SK = struct
adamc@144 43 type ord_key = string
adamc@144 44 val compare = String.compare
adamc@144 45 end
adamc@144 46
adamc@144 47 structure SS = BinarySetFn(SK)
adamc@144 48 structure SM = BinaryMapFn(SK)
adamc@144 49 structure IS = IntBinarySet
adamc@144 50
adamc@144 51 structure CM = BinaryMapFn(struct
adamc@144 52 type ord_key = char
adamc@144 53 val compare = Char.compare
adamc@144 54 end)
adamc@144 55
adamc@29 56 val debug = ref false
adamc@29 57
adamc@196 58 val dummyTyp = (TDatatype (Enum, 0, ref []), ErrorMsg.dummySpan)
adamc@29 59
adamc@316 60 val ident = String.translate (fn #"'" => "PRIME"
adamc@316 61 | ch => str ch)
adamc@316 62
adamc@316 63 val p_ident = string o ident
adamc@316 64
adamc@463 65 fun isUnboxable (t : typ) =
adamc@463 66 case #1 t of
adamc@463 67 TDatatype (Default, _, _) => true
adamc@463 68 | TFfi ("Basis", "string") => true
adamc@463 69 | _ => false
adamc@463 70
adamc@29 71 fun p_typ' par env (t, loc) =
adamc@29 72 case t of
adamc@476 73 TFun (t1, t2) => (EM.errorAt loc "Function type remains";
adamc@476 74 string "<FUNCTION>")
adamc@29 75 | TRecord i => box [string "struct",
adamc@29 76 space,
adamc@311 77 string "__uws_",
adamc@29 78 string (Int.toString i)]
adamc@188 79 | TDatatype (Enum, n, _) =>
adamc@188 80 (box [string "enum",
adamc@188 81 space,
adamc@311 82 string ("__uwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
adamc@311 83 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@198 84 | TDatatype (Option, n, xncs) =>
adamc@198 85 (case ListUtil.search #3 (!xncs) of
adamc@198 86 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
adamc@198 87 | SOME t =>
adamc@198 88 case #1 t of
adamc@198 89 TDatatype _ => p_typ' par env t
adamc@199 90 | TFfi ("Basis", "string") => p_typ' par env t
adamc@198 91 | _ => box [p_typ' par env t,
adamc@198 92 string "*"])
adamc@188 93 | TDatatype (Default, n, _) =>
adamc@165 94 (box [string "struct",
adamc@165 95 space,
adamc@311 96 string ("__uwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
adamc@311 97 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@316 98 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@288 99 | TOption t =>
adamc@463 100 if isUnboxable t then
adamc@463 101 p_typ' par env t
adamc@463 102 else
adamc@463 103 box [p_typ' par env t,
adamc@463 104 string "*"]
adamc@29 105
adamc@29 106 and p_typ env = p_typ' false env
adamc@29 107
adamc@316 108 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@311 109 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 110
adamc@109 111 fun p_enamed env n =
adamc@316 112 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
adamc@311 113 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
adamc@109 114
adamc@182 115 fun p_con_named env n =
adamc@316 116 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
adamc@311 117 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
adamc@182 118
adamc@182 119 fun p_pat_preamble env (p, _) =
adamc@182 120 case p of
adamc@182 121 PWild => (box [],
adamc@182 122 env)
adamc@182 123 | PVar (x, t) => (box [p_typ env t,
adamc@182 124 space,
adamc@311 125 string "__uwr_",
adamc@316 126 p_ident x,
adamc@182 127 string "_",
adamc@182 128 string (Int.toString (E.countERels env)),
adamc@182 129 string ";",
adamc@182 130 newline],
adamc@196 131 E.pushERel env x t)
adamc@182 132 | PPrim _ => (box [], env)
adamc@188 133 | PCon (_, _, NONE) => (box [], env)
adamc@188 134 | PCon (_, _, SOME p) => p_pat_preamble env p
adamc@182 135 | PRecord xps =>
adamc@182 136 foldl (fn ((_, p, _), (pp, env)) =>
adamc@182 137 let
adamc@182 138 val (pp', env) = p_pat_preamble env p
adamc@182 139 in
adamc@182 140 (box [pp', pp], env)
adamc@182 141 end) (box [], env) xps
adamc@288 142 | PNone _ => (box [], env)
adamc@288 143 | PSome (_, p) => p_pat_preamble env p
adamc@182 144
adamc@182 145 fun p_patCon env pc =
adamc@182 146 case pc of
adamc@182 147 PConVar n => p_con_named env n
adamc@316 148 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
adamc@182 149
adamc@182 150 fun p_pat (env, exit, depth) (p, _) =
adamc@182 151 case p of
adamc@182 152 PWild =>
adamc@182 153 (box [], env)
adamc@182 154 | PVar (x, t) =>
adamc@311 155 (box [string "__uwr_",
adamc@316 156 p_ident x,
adamc@182 157 string "_",
adamc@182 158 string (Int.toString (E.countERels env)),
adamc@182 159 space,
adamc@182 160 string "=",
adamc@182 161 space,
adamc@182 162 string "disc",
adamc@182 163 string (Int.toString depth),
adamc@182 164 string ";"],
adamc@182 165 E.pushERel env x t)
adamc@182 166 | PPrim (Prim.Int n) =>
adamc@182 167 (box [string "if",
adamc@182 168 space,
adamc@182 169 string "(disc",
adamc@182 170 string (Int.toString depth),
adamc@182 171 space,
adamc@182 172 string "!=",
adamc@182 173 space,
adamc@276 174 Prim.p_t_GCC (Prim.Int n),
adamc@182 175 string ")",
adamc@182 176 space,
adamc@182 177 exit],
adamc@182 178 env)
adamc@182 179 | PPrim (Prim.String s) =>
adamc@182 180 (box [string "if",
adamc@182 181 space,
adamc@182 182 string "(strcmp(disc",
adamc@182 183 string (Int.toString depth),
adamc@182 184 string ",",
adamc@182 185 space,
adamc@276 186 Prim.p_t_GCC (Prim.String s),
adamc@182 187 string "))",
adamc@182 188 space,
adamc@182 189 exit],
adamc@182 190 env)
adamc@182 191 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
adamc@182 192
adamc@188 193 | PCon (dk, pc, po) =>
adamc@182 194 let
adamc@182 195 val (p, env) =
adamc@182 196 case po of
adamc@182 197 NONE => (box [], env)
adamc@182 198 | SOME p =>
adamc@182 199 let
adamc@182 200 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 201
adamc@182 202 val (x, to) = case pc of
adamc@182 203 PConVar n =>
adamc@182 204 let
adamc@182 205 val (x, to, _) = E.lookupConstructor env n
adamc@182 206 in
adamc@316 207 ("uw_" ^ ident x, to)
adamc@182 208 end
adamc@188 209 | PConFfi {mod = m, con, arg, ...} =>
adamc@316 210 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
adamc@182 211
adamc@182 212 val t = case to of
adamc@182 213 NONE => raise Fail "CjrPrint: Constructor mismatch"
adamc@182 214 | SOME t => t
adamc@182 215 in
adamc@182 216 (box [string "{",
adamc@182 217 newline,
adamc@182 218 p_typ env t,
adamc@182 219 space,
adamc@182 220 string "disc",
adamc@182 221 string (Int.toString (depth + 1)),
adamc@182 222 space,
adamc@182 223 string "=",
adamc@182 224 space,
adamc@198 225 case dk of
adamc@198 226 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
adamc@198 227 | Default => box [string "disc",
adamc@198 228 string (Int.toString depth),
adamc@198 229 string "->data.",
adamc@198 230 string x]
adamc@198 231 | Option =>
adamc@463 232 if isUnboxable t then
adamc@463 233 box [string "disc",
adamc@463 234 string (Int.toString depth)]
adamc@463 235 else
adamc@463 236 box [string "*disc",
adamc@463 237 string (Int.toString depth)],
adamc@182 238 string ";",
adamc@182 239 newline,
adamc@182 240 p,
adamc@182 241 newline,
adamc@182 242 string "}"],
adamc@182 243 env)
adamc@182 244 end
adamc@182 245 in
adamc@182 246 (box [string "if",
adamc@182 247 space,
adamc@182 248 string "(disc",
adamc@182 249 string (Int.toString depth),
adamc@198 250 case (dk, po) of
adamc@198 251 (Enum, _) => box [space,
adamc@198 252 string "!=",
adamc@198 253 space,
adamc@198 254 p_patCon env pc]
adamc@198 255 | (Default, _) => box [string "->tag",
adamc@198 256 space,
adamc@198 257 string "!=",
adamc@198 258 space,
adamc@198 259 p_patCon env pc]
adamc@198 260 | (Option, NONE) => box [space,
adamc@198 261 string "!=",
adamc@198 262 space,
adamc@198 263 string "NULL"]
adamc@198 264 | (Option, SOME _) => box [space,
adamc@198 265 string "==",
adamc@198 266 space,
adamc@198 267 string "NULL"],
adamc@182 268 string ")",
adamc@182 269 space,
adamc@182 270 exit,
adamc@182 271 newline,
adamc@182 272 p],
adamc@182 273 env)
adamc@182 274 end
adamc@182 275
adamc@182 276 | PRecord xps =>
adamc@182 277 let
adamc@182 278 val (xps, env) =
adamc@182 279 ListUtil.foldlMap (fn ((x, p, t), env) =>
adamc@182 280 let
adamc@182 281 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 282
adamc@182 283 val p = box [string "{",
adamc@182 284 newline,
adamc@182 285 p_typ env t,
adamc@182 286 space,
adamc@182 287 string "disc",
adamc@182 288 string (Int.toString (depth + 1)),
adamc@182 289 space,
adamc@182 290 string "=",
adamc@182 291 space,
adamc@182 292 string "disc",
adamc@182 293 string (Int.toString depth),
adamc@311 294 string ".__uwf_",
adamc@316 295 p_ident x,
adamc@182 296 string ";",
adamc@182 297 newline,
adamc@182 298 p,
adamc@182 299 newline,
adamc@182 300 string "}"]
adamc@182 301 in
adamc@182 302 (p, env)
adamc@182 303 end) env xps
adamc@182 304 in
adamc@182 305 (p_list_sep newline (fn x => x) xps,
adamc@182 306 env)
adamc@182 307 end
adamc@182 308
adamc@288 309 | PNone t =>
adamc@288 310 (box [string "if",
adamc@288 311 space,
adamc@288 312 string "(disc",
adamc@288 313 string (Int.toString depth),
adamc@288 314 space,
adamc@288 315 string "!=",
adamc@288 316 space,
adamc@288 317 string "NULL)",
adamc@288 318 space,
adamc@288 319 exit,
adamc@288 320 newline],
adamc@288 321 env)
adamc@288 322
adamc@288 323 | PSome (t, p) =>
adamc@288 324 let
adamc@288 325 val (p, env) =
adamc@288 326 let
adamc@288 327 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@288 328 in
adamc@288 329 (box [string "{",
adamc@288 330 newline,
adamc@288 331 p_typ env t,
adamc@288 332 space,
adamc@288 333 string "disc",
adamc@288 334 string (Int.toString (depth + 1)),
adamc@288 335 space,
adamc@288 336 string "=",
adamc@288 337 space,
adamc@463 338 if isUnboxable t then
adamc@463 339 box [string "disc",
adamc@463 340 string (Int.toString depth)]
adamc@463 341 else
adamc@463 342 box [string "*disc",
adamc@463 343 string (Int.toString depth)],
adamc@288 344 string ";",
adamc@288 345 newline,
adamc@288 346 p,
adamc@288 347 newline,
adamc@288 348 string "}"],
adamc@288 349 env)
adamc@288 350 end
adamc@288 351 in
adamc@288 352 (box [string "if",
adamc@288 353 space,
adamc@288 354 string "(disc",
adamc@288 355 string (Int.toString depth),
adamc@288 356 space,
adamc@288 357 string "==",
adamc@288 358 space,
adamc@288 359 string "NULL)",
adamc@288 360 space,
adamc@288 361 exit,
adamc@288 362 newline,
adamc@288 363 p],
adamc@288 364 env)
adamc@288 365 end
adamc@288 366
adamc@182 367 local
adamc@182 368 val count = ref 0
adamc@182 369 in
adamc@182 370 fun newGoto () =
adamc@182 371 let
adamc@182 372 val r = !count
adamc@182 373 in
adamc@182 374 count := r + 1;
adamc@182 375 string ("L" ^ Int.toString r)
adamc@182 376 end
adamc@182 377 end
adamc@182 378
adamc@185 379 fun patConInfo env pc =
adamc@185 380 case pc of
adamc@185 381 PConVar n =>
adamc@185 382 let
adamc@185 383 val (x, _, dn) = E.lookupConstructor env n
adamc@185 384 val (dx, _) = E.lookupDatatype env dn
adamc@185 385 in
adamc@316 386 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
adamc@316 387 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
adamc@316 388 "uw_" ^ ident x)
adamc@185 389 end
adamc@186 390 | PConFfi {mod = m, datatyp, con, ...} =>
adamc@316 391 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
adamc@316 392 "uw_" ^ ident m ^ "_" ^ ident con,
adamc@316 393 "uw_" ^ ident con)
adamc@185 394
adamc@324 395 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
adamc@278 396 case t of
adamc@311 397 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
adamc@311 398 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
adamc@324 399 | TFfi ("Basis", "string") =>
adamc@324 400 if wontLeakStrings then
adamc@324 401 e
adamc@324 402 else
adamc@324 403 box [string "uw_Basis_strdup(ctx, ", e, string ")"]
adamc@311 404 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
adamc@438 405 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
adamc@678 406 | TFfi ("Basis", "channel") => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"]
adamc@682 407 | TFfi ("Basis", "client") => box [string "uw_Basis_stringToClient_error(ctx, ", e, string ")"]
adamc@467 408
adamc@278 409 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
adamc@278 410 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@278 411 string "ERROR")
adamc@278 412
adamc@467 413 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
adamc@467 414 case t of
adamc@467 415 TOption t =>
adamc@467 416 box [string "(PQgetisnull (res, i, ",
adamc@467 417 string (Int.toString i),
adamc@467 418 string ") ? NULL : ",
adamc@467 419 case t of
adamc@467 420 (TFfi ("Basis", "string"), _) => p_getcol wontLeakStrings env t i
adamc@467 421 | _ => box [string "({",
adamc@467 422 newline,
adamc@467 423 p_typ env t,
adamc@467 424 space,
adamc@467 425 string "*tmp = uw_malloc(ctx, sizeof(",
adamc@467 426 p_typ env t,
adamc@467 427 string "));",
adamc@467 428 newline,
adamc@467 429 string "*tmp = ",
adamc@467 430 p_getcol wontLeakStrings env t i,
adamc@467 431 string ";",
adamc@467 432 newline,
adamc@467 433 string "tmp;",
adamc@467 434 newline,
adamc@467 435 string "})"],
adamc@467 436 string ")"]
adamc@467 437
adamc@467 438 | _ =>
adamc@467 439 p_unsql wontLeakStrings env tAll
adamc@467 440 (box [string "PQgetvalue(res, i, ",
adamc@467 441 string (Int.toString i),
adamc@467 442 string ")"])
adamc@467 443
adamc@282 444 datatype sql_type =
adamc@282 445 Int
adamc@282 446 | Float
adamc@282 447 | String
adamc@282 448 | Bool
adamc@439 449 | Time
adamc@678 450 | Channel
adamc@682 451 | Client
adamc@467 452 | Nullable of sql_type
adamc@282 453
adamc@467 454 fun p_sql_type' t =
adamc@467 455 case t of
adamc@467 456 Int => "uw_Basis_int"
adamc@467 457 | Float => "uw_Basis_float"
adamc@467 458 | String => "uw_Basis_string"
adamc@467 459 | Bool => "uw_Basis_bool"
adamc@467 460 | Time => "uw_Basis_time"
adamc@678 461 | Channel => "uw_Basis_channel"
adamc@682 462 | Client => "uw_Basis_client"
adamc@467 463 | Nullable String => "uw_Basis_string"
adamc@467 464 | Nullable t => p_sql_type' t ^ "*"
adamc@467 465
adamc@467 466 fun p_sql_type t = string (p_sql_type' t)
adamc@282 467
adamc@282 468 fun getPargs (e, _) =
adamc@282 469 case e of
adamc@282 470 EPrim (Prim.String _) => []
adamc@282 471 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
adamc@282 472
adamc@282 473 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
adamc@282 474 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
adamc@282 475 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
adamc@282 476 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
adamc@439 477 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
adamc@678 478 | EFfiApp ("Basis", "sqlifyChannel", [e]) => [(e, Channel)]
adamc@682 479 | EFfiApp ("Basis", "sqlifyClient", [e]) => [(e, Client)]
adamc@468 480
adamc@678 481 | ECase (e,
adamc@678 482 [((PNone _, _),
adamc@678 483 (EPrim (Prim.String "NULL"), _)),
adamc@678 484 ((PSome (_, (PVar _, _)), _),
adamc@678 485 (EFfiApp (m, x, [(ERel 0, _)]), _))],
adamc@678 486 _) => map (fn (x, y) => (x, Nullable y)) (getPargs (EFfiApp (m, x, [e]), #2 e))
adamc@468 487
adamc@491 488 | ECase (e,
adamc@491 489 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
adamc@491 490 (EPrim (Prim.String "TRUE"), _)),
adamc@491 491 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
adamc@491 492 (EPrim (Prim.String "FALSE"), _))],
adamc@491 493 _) => [(e, Bool)]
adamc@282 494
adamc@282 495 | _ => raise Fail "CjrPrint: getPargs"
adamc@282 496
adamc@282 497 fun p_ensql t e =
adamc@282 498 case t of
adamc@311 499 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
adamc@311 500 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
adamc@282 501 | String => e
adamc@295 502 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
adamc@682 503 | Time => box [string "uw_Basis_attrifyTime(ctx, ", e, string ")"]
adamc@678 504 | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"]
adamc@682 505 | Client => box [string "uw_Basis_attrifyClient(ctx, ", e, string ")"]
adamc@467 506 | Nullable String => e
adamc@467 507 | Nullable t => box [string "(",
adamc@467 508 e,
adamc@467 509 string " == NULL ? NULL : ",
adamc@467 510 p_ensql t (box [string "*", e]),
adamc@467 511 string ")"]
adamc@282 512
adamc@324 513 fun notLeaky env allowHeapAllocated =
adamc@324 514 let
adamc@638 515 fun nl ok (t, _) =
adamc@324 516 case t of
adamc@324 517 TFun _ => false
adamc@324 518 | TRecord n =>
adamc@324 519 let
adamc@324 520 val xts = E.lookupStruct env n
adamc@324 521 in
adamc@638 522 List.all (fn (_, t) => nl ok t) xts
adamc@324 523 end
adamc@638 524 | TDatatype (dk, n, ref cons) =>
adamc@638 525 IS.member (ok, n)
adamc@638 526 orelse
adamc@638 527 ((allowHeapAllocated orelse dk = Enum)
adamc@638 528 andalso
adamc@638 529 let
adamc@638 530 val ok' = IS.add (ok, n)
adamc@638 531 in
adamc@638 532 List.all (fn (_, _, to) => case to of
adamc@638 533 NONE => true
adamc@638 534 | SOME t => nl ok' t) cons
adamc@638 535 end)
adamc@324 536 | TFfi ("Basis", "string") => false
adamc@324 537 | TFfi _ => true
adamc@638 538 | TOption t => allowHeapAllocated andalso nl ok t
adamc@324 539 in
adamc@638 540 nl IS.empty
adamc@324 541 end
adamc@324 542
adamc@463 543 fun capitalize s =
adamc@463 544 if s = "" then
adamc@463 545 ""
adamc@463 546 else
adamc@463 547 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@463 548
adamc@463 549 fun unurlify env (t, loc) =
adamc@463 550 let
adamc@463 551 fun unurlify' rf t =
adamc@463 552 case t of
adamc@463 553 TFfi ("Basis", "unit") => string ("uw_unit_v")
adamc@463 554 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
adamc@463 555
adamc@463 556 | TRecord 0 => string "uw_unit_v"
adamc@463 557 | TRecord i =>
adamc@463 558 let
adamc@463 559 val xts = E.lookupStruct env i
adamc@463 560 in
adamc@463 561 box [string "({",
adamc@463 562 newline,
adamc@463 563 box (map (fn (x, t) =>
adamc@463 564 box [p_typ env t,
adamc@463 565 space,
adamc@463 566 string "uwr_",
adamc@463 567 string x,
adamc@463 568 space,
adamc@463 569 string "=",
adamc@463 570 space,
adamc@463 571 unurlify' rf (#1 t),
adamc@463 572 string ";",
adamc@463 573 newline]) xts),
adamc@463 574 string "struct",
adamc@463 575 space,
adamc@463 576 string "__uws_",
adamc@463 577 string (Int.toString i),
adamc@463 578 space,
adamc@463 579 string "tmp",
adamc@463 580 space,
adamc@463 581 string "=",
adamc@463 582 space,
adamc@463 583 string "{",
adamc@463 584 space,
adamc@463 585 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
adamc@463 586 string x]) xts,
adamc@463 587 space,
adamc@463 588 string "};",
adamc@463 589 newline,
adamc@463 590 string "tmp;",
adamc@463 591 newline,
adamc@463 592 string "})"]
adamc@463 593 end
adamc@463 594
adamc@463 595 | TDatatype (Enum, i, _) =>
adamc@463 596 let
adamc@463 597 val (x, xncs) = E.lookupDatatype env i
adamc@463 598
adamc@463 599 fun doEm xncs =
adamc@463 600 case xncs of
adamc@463 601 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 602 ^ x ^ "\"), (enum __uwe_"
adamc@463 603 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@463 604 | (x', n, to) :: rest =>
adamc@463 605 box [string "((!strncmp(request, \"",
adamc@463 606 string x',
adamc@463 607 string "\", ",
adamc@463 608 string (Int.toString (size x')),
adamc@463 609 string ") && (request[",
adamc@463 610 string (Int.toString (size x')),
adamc@463 611 string "] == 0 || request[",
adamc@463 612 string (Int.toString (size x')),
adamc@463 613 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 614 space,
adamc@463 615 string ":",
adamc@463 616 space,
adamc@463 617 doEm rest,
adamc@463 618 string ")"]
adamc@463 619 in
adamc@463 620 doEm xncs
adamc@463 621 end
adamc@463 622
adamc@463 623 | TDatatype (Option, i, xncs) =>
adamc@463 624 if IS.member (rf, i) then
adamc@463 625 box [string "unurlify_",
adamc@463 626 string (Int.toString i),
adamc@463 627 string "()"]
adamc@463 628 else
adamc@463 629 let
adamc@463 630 val (x, _) = E.lookupDatatype env i
adamc@463 631
adamc@463 632 val (no_arg, has_arg, t) =
adamc@463 633 case !xncs of
adamc@463 634 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@463 635 (no_arg, has_arg, t)
adamc@463 636 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@463 637 (no_arg, has_arg, t)
adamc@463 638 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
adamc@463 639
adamc@463 640 val rf = IS.add (rf, i)
adamc@463 641 in
adamc@463 642 box [string "({",
adamc@463 643 space,
adamc@463 644 p_typ env t,
adamc@463 645 space,
adamc@463 646 string "*unurlify_",
adamc@463 647 string (Int.toString i),
adamc@463 648 string "(void) {",
adamc@463 649 newline,
adamc@463 650 box [string "return (request[0] == '/' ? ++request : request,",
adamc@463 651 newline,
adamc@463 652 string "((!strncmp(request, \"",
adamc@463 653 string no_arg,
adamc@463 654 string "\", ",
adamc@463 655 string (Int.toString (size no_arg)),
adamc@463 656 string ") && (request[",
adamc@463 657 string (Int.toString (size no_arg)),
adamc@463 658 string "] == 0 || request[",
adamc@463 659 string (Int.toString (size no_arg)),
adamc@463 660 string "] == '/')) ? (request",
adamc@463 661 space,
adamc@463 662 string "+=",
adamc@463 663 space,
adamc@463 664 string (Int.toString (size no_arg)),
adamc@463 665 string ", NULL) : ((!strncmp(request, \"",
adamc@463 666 string has_arg,
adamc@463 667 string "\", ",
adamc@463 668 string (Int.toString (size has_arg)),
adamc@463 669 string ") && (request[",
adamc@463 670 string (Int.toString (size has_arg)),
adamc@463 671 string "] == 0 || request[",
adamc@463 672 string (Int.toString (size has_arg)),
adamc@463 673 string "] == '/')) ? (request",
adamc@463 674 space,
adamc@463 675 string "+=",
adamc@463 676 space,
adamc@463 677 string (Int.toString (size has_arg)),
adamc@463 678 string ", (request[0] == '/' ? ++request : NULL), ",
adamc@463 679 newline,
adamc@463 680
adamc@463 681 if isUnboxable t then
adamc@463 682 unurlify' rf (#1 t)
adamc@463 683 else
adamc@463 684 box [string "({",
adamc@463 685 newline,
adamc@463 686 p_typ env t,
adamc@463 687 space,
adamc@463 688 string "*tmp",
adamc@463 689 space,
adamc@463 690 string "=",
adamc@463 691 space,
adamc@463 692 string "uw_malloc(ctx, sizeof(",
adamc@463 693 p_typ env t,
adamc@463 694 string "));",
adamc@463 695 newline,
adamc@463 696 string "*tmp",
adamc@463 697 space,
adamc@463 698 string "=",
adamc@463 699 space,
adamc@463 700 unurlify' rf (#1 t),
adamc@463 701 string ";",
adamc@463 702 newline,
adamc@463 703 string "tmp;",
adamc@463 704 newline,
adamc@463 705 string "})"],
adamc@463 706 string ")",
adamc@463 707 newline,
adamc@463 708 string ":",
adamc@463 709 space,
adamc@463 710 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
adamc@463 711 ^ "\"), NULL))));"),
adamc@463 712 newline],
adamc@463 713 string "}",
adamc@463 714 newline,
adamc@463 715 newline,
adamc@463 716
adamc@463 717 string "unurlify_",
adamc@463 718 string (Int.toString i),
adamc@463 719 string "();",
adamc@463 720 newline,
adamc@463 721 string "})"]
adamc@463 722 end
adamc@463 723
adamc@463 724 | TDatatype (Default, i, _) =>
adamc@463 725 if IS.member (rf, i) then
adamc@463 726 box [string "unurlify_",
adamc@463 727 string (Int.toString i),
adamc@463 728 string "()"]
adamc@463 729 else
adamc@463 730 let
adamc@463 731 val (x, xncs) = E.lookupDatatype env i
adamc@463 732
adamc@463 733 val rf = IS.add (rf, i)
adamc@463 734
adamc@463 735 fun doEm xncs =
adamc@463 736 case xncs of
adamc@463 737 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 738 ^ x ^ "\"), NULL)")
adamc@463 739 | (x', n, to) :: rest =>
adamc@463 740 box [string "((!strncmp(request, \"",
adamc@463 741 string x',
adamc@463 742 string "\", ",
adamc@463 743 string (Int.toString (size x')),
adamc@463 744 string ") && (request[",
adamc@463 745 string (Int.toString (size x')),
adamc@463 746 string "] == 0 || request[",
adamc@463 747 string (Int.toString (size x')),
adamc@463 748 string "] == '/')) ? ({",
adamc@463 749 newline,
adamc@463 750 string "struct",
adamc@463 751 space,
adamc@463 752 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
adamc@463 753 space,
adamc@463 754 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
adamc@463 755 string x,
adamc@463 756 string "_",
adamc@463 757 string (Int.toString i),
adamc@463 758 string "));",
adamc@463 759 newline,
adamc@463 760 string "tmp->tag",
adamc@463 761 space,
adamc@463 762 string "=",
adamc@463 763 space,
adamc@463 764 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 765 string ";",
adamc@463 766 newline,
adamc@463 767 string "request",
adamc@463 768 space,
adamc@463 769 string "+=",
adamc@463 770 space,
adamc@463 771 string (Int.toString (size x')),
adamc@463 772 string ";",
adamc@463 773 newline,
adamc@463 774 string "if (request[0] == '/') ++request;",
adamc@463 775 newline,
adamc@463 776 case to of
adamc@463 777 NONE => box []
adamc@463 778 | SOME (t, _) => box [string "tmp->data.uw_",
adamc@463 779 p_ident x',
adamc@463 780 space,
adamc@463 781 string "=",
adamc@463 782 space,
adamc@463 783 unurlify' rf t,
adamc@463 784 string ";",
adamc@463 785 newline],
adamc@463 786 string "tmp;",
adamc@463 787 newline,
adamc@463 788 string "})",
adamc@463 789 space,
adamc@463 790 string ":",
adamc@463 791 space,
adamc@463 792 doEm rest,
adamc@463 793 string ")"]
adamc@463 794 in
adamc@463 795 box [string "({",
adamc@463 796 space,
adamc@463 797 p_typ env (t, ErrorMsg.dummySpan),
adamc@463 798 space,
adamc@463 799 string "unurlify_",
adamc@463 800 string (Int.toString i),
adamc@463 801 string "(void) {",
adamc@463 802 newline,
adamc@463 803 box [string "return",
adamc@463 804 space,
adamc@463 805 doEm xncs,
adamc@463 806 string ";",
adamc@463 807 newline],
adamc@463 808 string "}",
adamc@463 809 newline,
adamc@463 810 newline,
adamc@463 811
adamc@463 812 string "unurlify_",
adamc@463 813 string (Int.toString i),
adamc@463 814 string "();",
adamc@463 815 newline,
adamc@463 816 string "})"]
adamc@463 817 end
adamc@463 818
adamc@471 819 | TOption t =>
adamc@471 820 box [string "(request[0] == '/' ? ++request : request, ",
adamc@471 821 string "((!strncmp(request, \"None\", 4) ",
adamc@471 822 string "&& (request[4] == 0 || request[4] == '/')) ",
adamc@471 823 string "? (request += 4, NULL) ",
adamc@471 824 string ": ((!strncmp(request, \"Some\", 4) ",
adamc@471 825 string "&& request[4] == '/') ",
adamc@471 826 string "? (request += 5, ",
adamc@471 827 if isUnboxable t then
adamc@471 828 unurlify' rf (#1 t)
adamc@471 829 else
adamc@471 830 box [string "({",
adamc@471 831 newline,
adamc@471 832 p_typ env t,
adamc@471 833 space,
adamc@471 834 string "*tmp",
adamc@471 835 space,
adamc@471 836 string "=",
adamc@471 837 space,
adamc@471 838 string "uw_malloc(ctx, sizeof(",
adamc@471 839 p_typ env t,
adamc@471 840 string "));",
adamc@471 841 newline,
adamc@471 842 string "*tmp",
adamc@471 843 space,
adamc@471 844 string "=",
adamc@471 845 space,
adamc@471 846 unurlify' rf (#1 t),
adamc@471 847 string ";",
adamc@471 848 newline,
adamc@471 849 string "tmp;",
adamc@471 850 newline,
adamc@471 851 string "})"],
adamc@471 852 string ") :",
adamc@471 853 space,
adamc@471 854 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]
adamc@471 855
adamc@463 856 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@463 857 space)
adamc@463 858 in
adamc@463 859 unurlify' IS.empty t
adamc@463 860 end
adamc@463 861
adamc@610 862 fun urlify env t =
adamc@610 863 let
adamc@610 864 fun urlify' rf level (t as (_, loc)) =
adamc@610 865 case #1 t of
adamc@610 866 TFfi ("Basis", "unit") => box []
adamc@610 867 | TFfi (m, t) => box [string ("uw_" ^ ident m ^ "_urlify" ^ capitalize t
adamc@610 868 ^ "_w(ctx, it" ^ Int.toString level ^ ");"),
adamc@610 869 newline]
adamc@610 870
adamc@610 871 | TRecord 0 => box []
adamc@610 872 | TRecord i =>
adamc@610 873 let
adamc@611 874 fun empty (t, _) =
adamc@611 875 case t of
adamc@611 876 TFfi ("Basis", "unit") => true
adamc@611 877 | TRecord 0 => true
adamc@611 878 | TRecord j =>
adamc@611 879 List.all (fn (_, t) => empty t) (E.lookupStruct env j)
adamc@611 880 | _ => false
adamc@611 881
adamc@610 882 val xts = E.lookupStruct env i
adamc@611 883
adamc@613 884 val (blocks, _) = foldl
adamc@613 885 (fn ((x, t), (blocks, printingSinceLastSlash)) =>
adamc@613 886 let
adamc@613 887 val thisEmpty = empty t
adamc@613 888 in
adamc@613 889 if thisEmpty then
adamc@613 890 (blocks, printingSinceLastSlash)
adamc@613 891 else
adamc@613 892 (box [string "{",
adamc@613 893 newline,
adamc@613 894 p_typ env t,
adamc@613 895 space,
adamc@613 896 string ("it" ^ Int.toString (level + 1)),
adamc@613 897 space,
adamc@613 898 string "=",
adamc@613 899 space,
adamc@613 900 string ("it" ^ Int.toString level ^ ".__uwf_" ^ x ^ ";"),
adamc@613 901 newline,
adamc@613 902 box (if printingSinceLastSlash then
adamc@613 903 [string "uw_write(ctx, \"/\");",
adamc@613 904 newline]
adamc@613 905 else
adamc@613 906 []),
adamc@613 907 urlify' rf (level + 1) t,
adamc@613 908 string "}",
adamc@613 909 newline] :: blocks,
adamc@613 910 true)
adamc@613 911 end)
adamc@613 912 ([], false) xts
adamc@610 913 in
adamc@613 914 box (rev blocks)
adamc@610 915 end
adamc@610 916
adamc@638 917 | TDatatype (Enum, i, _) =>
adamc@638 918 let
adamc@610 919 val (x, xncs) = E.lookupDatatype env i
adamc@610 920
adamc@610 921 fun doEm xncs =
adamc@610 922 case xncs of
adamc@638 923 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
adamc@638 924 ^ x ^ "\");"),
adamc@638 925 newline]
adamc@610 926 | (x', n, to) :: rest =>
adamc@638 927 box [string ("if (it" ^ Int.toString level
adamc@638 928 ^ "==__uwc_" ^ ident x' ^ "_" ^ Int.toString n ^ ") {"),
adamc@638 929 newline,
adamc@638 930 box [string ("uw_write(ctx, \"" ^ x' ^ "\");"),
adamc@638 931 newline],
adamc@638 932 string "} else {",
adamc@638 933 newline,
adamc@638 934 box [doEm rest,
adamc@638 935 newline],
adamc@638 936 string "}"]
adamc@610 937 in
adamc@610 938 doEm xncs
adamc@638 939 end
adamc@610 940
adamc@639 941 | TDatatype (Option, i, xncs) =>
adamc@639 942 if IS.member (rf, i) then
adamc@639 943 box [string "urlify_",
adamc@610 944 string (Int.toString i),
adamc@639 945 string "(it",
adamc@639 946 string (Int.toString level),
adamc@639 947 string ");",
adamc@639 948 newline]
adamc@610 949 else
adamc@610 950 let
adamc@610 951 val (x, _) = E.lookupDatatype env i
adamc@610 952
adamc@610 953 val (no_arg, has_arg, t) =
adamc@610 954 case !xncs of
adamc@610 955 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@610 956 (no_arg, has_arg, t)
adamc@610 957 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@610 958 (no_arg, has_arg, t)
adamc@639 959 | _ => raise Fail "CjrPrint: urlify misclassified Option datatype"
adamc@610 960
adamc@610 961 val rf = IS.add (rf, i)
adamc@610 962 in
adamc@610 963 box [string "({",
adamc@610 964 space,
adamc@639 965 string "void",
adamc@639 966 space,
adamc@639 967 string "urlify_",
adamc@639 968 string (Int.toString i),
adamc@639 969 string "(",
adamc@610 970 p_typ env t,
adamc@610 971 space,
adamc@639 972 if isUnboxable t then
adamc@639 973 box []
adamc@639 974 else
adamc@639 975 string "*",
adamc@639 976 string "it0) {",
adamc@610 977 newline,
adamc@639 978 box [string "if (it0) {",
adamc@639 979 if isUnboxable t then
adamc@639 980 urlify' rf 0 t
adamc@610 981 else
adamc@639 982 box [p_typ env t,
adamc@610 983 space,
adamc@639 984 string "it1",
adamc@610 985 space,
adamc@610 986 string "=",
adamc@610 987 space,
adamc@639 988 string "*it0;",
adamc@610 989 newline,
adamc@639 990 string "uw_write(ctx, \"",
adamc@639 991 string has_arg,
adamc@639 992 string "/\");",
adamc@639 993 newline,
adamc@639 994 urlify' rf 1 t,
adamc@610 995 string ";",
adamc@639 996 newline],
adamc@639 997 string "} else {",
adamc@639 998 box [string "uw_write(ctx, \"",
adamc@639 999 string no_arg,
adamc@639 1000 string "\");",
adamc@639 1001 newline],
adamc@639 1002 string "}",
adamc@610 1003 newline],
adamc@610 1004 string "}",
adamc@610 1005 newline,
adamc@610 1006 newline,
adamc@610 1007
adamc@639 1008 string "urlify_",
adamc@610 1009 string (Int.toString i),
adamc@639 1010 string "(it",
adamc@639 1011 string (Int.toString level),
adamc@639 1012 string ");",
adamc@610 1013 newline,
adamc@639 1014 string "});",
adamc@639 1015 newline]
adamc@639 1016 end
adamc@610 1017
adamc@640 1018 | TDatatype (Default, i, _) =>
adamc@640 1019 if IS.member (rf, i) then
adamc@640 1020 box [string "urlify_",
adamc@610 1021 string (Int.toString i),
adamc@640 1022 string "(it",
adamc@640 1023 string (Int.toString level),
adamc@640 1024 string ");",
adamc@640 1025 newline]
adamc@610 1026 else
adamc@610 1027 let
adamc@610 1028 val (x, xncs) = E.lookupDatatype env i
adamc@610 1029
adamc@610 1030 val rf = IS.add (rf, i)
adamc@610 1031
adamc@610 1032 fun doEm xncs =
adamc@610 1033 case xncs of
adamc@640 1034 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
adamc@640 1035 ^ x ^ " (%d)\", it0->data);"),
adamc@640 1036 newline]
adamc@610 1037 | (x', n, to) :: rest =>
adamc@640 1038 box [string "if",
adamc@610 1039 space,
adamc@640 1040 string "(it0->tag==__uwc_",
adamc@640 1041 string (ident x'),
adamc@610 1042 string "_",
adamc@640 1043 string (Int.toString n),
adamc@640 1044 string ") {",
adamc@610 1045 newline,
adamc@610 1046 case to of
adamc@640 1047 NONE => box [string "uw_write(ctx, \"",
adamc@640 1048 string x',
adamc@640 1049 string "\");",
adamc@640 1050 newline]
adamc@640 1051 | SOME t => box [string "uw_write(ctx, \"",
adamc@640 1052 string x',
adamc@640 1053 string "/\");",
adamc@640 1054 newline,
adamc@640 1055 p_typ env t,
adamc@640 1056 space,
adamc@640 1057 string "it1",
adamc@640 1058 space,
adamc@640 1059 string "=",
adamc@640 1060 space,
adamc@640 1061 string "it0->data.uw_",
adamc@640 1062 string x',
adamc@640 1063 string ";",
adamc@640 1064 newline,
adamc@640 1065 urlify' rf 1 t,
adamc@640 1066 newline],
adamc@640 1067 string "} else {",
adamc@610 1068 newline,
adamc@640 1069 box [doEm rest,
adamc@640 1070 newline],
adamc@640 1071 string "}",
adamc@640 1072 newline]
adamc@610 1073 in
adamc@610 1074 box [string "({",
adamc@610 1075 space,
adamc@640 1076 string "void",
adamc@610 1077 space,
adamc@640 1078 string "urlify_",
adamc@610 1079 string (Int.toString i),
adamc@640 1080 string "(",
adamc@640 1081 p_typ env t,
adamc@640 1082 space,
adamc@640 1083 string "it0) {",
adamc@610 1084 newline,
adamc@640 1085 box [doEm xncs,
adamc@610 1086 newline],
adamc@640 1087 newline,
adamc@610 1088 string "}",
adamc@610 1089 newline,
adamc@640 1090
adamc@640 1091 string "urlify_",
adamc@640 1092 string (Int.toString i),
adamc@640 1093 string "(it",
adamc@640 1094 string (Int.toString level),
adamc@640 1095 string ");",
adamc@610 1096 newline,
adamc@640 1097 string "});",
adamc@640 1098 newline]
adamc@640 1099 end
adamc@610 1100
adamc@641 1101 | TOption t =>
adamc@641 1102 box [string "if (it",
adamc@641 1103 string (Int.toString level),
adamc@641 1104 string ") {",
adamc@641 1105 if isUnboxable t then
adamc@641 1106 box [string "uw_write(ctx, \"Some/\");",
adamc@641 1107 newline,
adamc@641 1108 urlify' rf level t]
adamc@610 1109 else
adamc@641 1110 box [p_typ env t,
adamc@610 1111 space,
adamc@641 1112 string "it",
adamc@641 1113 string (Int.toString (level + 1)),
adamc@610 1114 space,
adamc@610 1115 string "=",
adamc@610 1116 space,
adamc@641 1117 string "*it",
adamc@641 1118 string (Int.toString level),
adamc@610 1119 string ";",
adamc@610 1120 newline,
adamc@641 1121 string "uw_write(ctx, \"Some/\");",
adamc@610 1122 newline,
adamc@641 1123 urlify' rf (level + 1) t,
adamc@641 1124 string ";",
adamc@641 1125 newline],
adamc@641 1126 string "} else {",
adamc@641 1127 box [string "uw_write(ctx, \"None\");",
adamc@641 1128 newline],
adamc@641 1129 string "}",
adamc@641 1130 newline]
adamc@610 1131
adamc@610 1132 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL encoding function";
adamc@610 1133 space)
adamc@610 1134 in
adamc@610 1135 urlify' IS.empty 0 t
adamc@610 1136 end
adamc@610 1137
adamc@673 1138 val timeout = ref 0
adamc@673 1139
adamc@182 1140 fun p_exp' par env (e, loc) =
adamc@29 1141 case e of
adamc@276 1142 EPrim p => Prim.p_t_GCC p
adamc@29 1143 | ERel n => p_rel env n
adamc@109 1144 | ENamed n => p_enamed env n
adamc@188 1145 | ECon (Enum, pc, _) => p_patCon env pc
adamc@198 1146 | ECon (Option, pc, NONE) => string "NULL"
adamc@198 1147 | ECon (Option, pc, SOME e) =>
adamc@198 1148 let
adamc@198 1149 val to = case pc of
adamc@198 1150 PConVar n => #2 (E.lookupConstructor env n)
adamc@198 1151 | PConFfi {arg, ...} => arg
adamc@198 1152
adamc@198 1153 val t = case to of
adamc@198 1154 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
adamc@198 1155 | SOME t => t
adamc@198 1156 in
adamc@463 1157 if isUnboxable t then
adamc@463 1158 p_exp' par env e
adamc@463 1159 else
adamc@463 1160 box [string "({",
adamc@463 1161 newline,
adamc@463 1162 p_typ env t,
adamc@463 1163 space,
adamc@463 1164 string "*tmp",
adamc@463 1165 space,
adamc@463 1166 string "=",
adamc@463 1167 space,
adamc@463 1168 string "uw_malloc(ctx, sizeof(",
adamc@463 1169 p_typ env t,
adamc@463 1170 string "));",
adamc@463 1171 newline,
adamc@463 1172 string "*tmp",
adamc@463 1173 space,
adamc@463 1174 string "=",
adamc@463 1175 p_exp' par env e,
adamc@463 1176 string ";",
adamc@463 1177 newline,
adamc@463 1178 string "tmp;",
adamc@463 1179 newline,
adamc@463 1180 string "})"]
adamc@198 1181 end
adamc@188 1182 | ECon (Default, pc, eo) =>
adamc@181 1183 let
adamc@196 1184 val (xd, xc, xn) = patConInfo env pc
adamc@181 1185 in
adamc@182 1186 box [string "({",
adamc@181 1187 newline,
adamc@181 1188 string "struct",
adamc@181 1189 space,
adamc@185 1190 string xd,
adamc@181 1191 space,
adamc@181 1192 string "*tmp",
adamc@181 1193 space,
adamc@181 1194 string "=",
adamc@181 1195 space,
adamc@311 1196 string "uw_malloc(ctx, sizeof(struct ",
adamc@185 1197 string xd,
adamc@181 1198 string "));",
adamc@181 1199 newline,
adamc@181 1200 string "tmp->tag",
adamc@181 1201 space,
adamc@181 1202 string "=",
adamc@181 1203 space,
adamc@185 1204 string xc,
adamc@181 1205 string ";",
adamc@181 1206 newline,
adamc@181 1207 case eo of
adamc@181 1208 NONE => box []
adamc@185 1209 | SOME e => box [string "tmp->data.",
adamc@196 1210 string xn,
adamc@181 1211 space,
adamc@181 1212 string "=",
adamc@181 1213 space,
adamc@181 1214 p_exp env e,
adamc@181 1215 string ";",
adamc@181 1216 newline],
adamc@181 1217 string "tmp;",
adamc@181 1218 newline,
adamc@181 1219 string "})"]
adamc@181 1220 end
adamc@297 1221 | ENone _ => string "NULL"
adamc@290 1222 | ESome (t, e) =>
adamc@463 1223 if isUnboxable t then
adamc@463 1224 p_exp' par env e
adamc@463 1225 else
adamc@463 1226 box [string "({",
adamc@463 1227 newline,
adamc@463 1228 p_typ env t,
adamc@463 1229 space,
adamc@463 1230 string "*tmp",
adamc@463 1231 space,
adamc@463 1232 string "=",
adamc@463 1233 space,
adamc@463 1234 string "uw_malloc(ctx, sizeof(",
adamc@463 1235 p_typ env t,
adamc@463 1236 string "));",
adamc@463 1237 newline,
adamc@463 1238 string "*tmp",
adamc@463 1239 space,
adamc@463 1240 string "=",
adamc@463 1241 p_exp' par env e,
adamc@463 1242 string ";",
adamc@463 1243 newline,
adamc@463 1244 string "tmp;",
adamc@463 1245 newline,
adamc@463 1246 string "})"]
adamc@109 1247
adamc@316 1248 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@283 1249 | EError (e, t) =>
adamc@283 1250 box [string "({",
adamc@283 1251 newline,
adamc@283 1252 p_typ env t,
adamc@283 1253 space,
adamc@283 1254 string "tmp;",
adamc@283 1255 newline,
adamc@311 1256 string "uw_error(ctx, FATAL, \"",
adamc@292 1257 string (ErrorMsg.spanToString loc),
adamc@292 1258 string ": %s\", ",
adamc@283 1259 p_exp env e,
adamc@283 1260 string ");",
adamc@283 1261 newline,
adamc@283 1262 string "tmp;",
adamc@283 1263 newline,
adamc@283 1264 string "})"]
adamc@476 1265 | EApp ((EError (e, (TFun (_, ran), _)), loc), _) =>
adamc@476 1266 p_exp env (EError (e, ran), loc)
adamc@476 1267
adamc@311 1268 | EFfiApp (m, x, es) => box [string "uw_",
adamc@316 1269 p_ident m,
adamc@53 1270 string "_",
adamc@316 1271 p_ident x,
adamc@117 1272 string "(ctx, ",
adamc@53 1273 p_list (p_exp env) es,
adamc@53 1274 string ")"]
adamc@316 1275 | EApp (f, args) =>
adamc@316 1276 parenIf par (box [p_exp' true env f,
adamc@316 1277 string "(ctx,",
adamc@316 1278 space,
adamc@316 1279 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@316 1280 string ")"])
adamc@29 1281
adamc@387 1282 | EUnop (s, e1) =>
adamc@387 1283 parenIf par (box [string s,
adamc@387 1284 space,
adamc@387 1285 p_exp' true env e1])
adamc@387 1286
adamc@387 1287 | EBinop (s, e1, e2) =>
adamc@389 1288 if Char.isAlpha (String.sub (s, size s - 1)) then
adamc@389 1289 box [string s,
adamc@390 1290 string "(",
adamc@389 1291 p_exp env e1,
adamc@389 1292 string ",",
adamc@389 1293 space,
adamc@389 1294 p_exp env e2,
adamc@389 1295 string ")"]
adamc@389 1296 else
adamc@389 1297 parenIf par (box [p_exp' true env e1,
adamc@389 1298 space,
adamc@389 1299 string s,
adamc@389 1300 space,
adamc@389 1301 p_exp' true env e2])
adamc@387 1302
adamc@29 1303 | ERecord (i, xes) => box [string "({",
adamc@29 1304 space,
adamc@29 1305 string "struct",
adamc@29 1306 space,
adamc@311 1307 string ("__uws_" ^ Int.toString i),
adamc@29 1308 space,
adamc@181 1309 string "tmp",
adamc@29 1310 space,
adamc@29 1311 string "=",
adamc@29 1312 space,
adamc@29 1313 string "{",
adamc@29 1314 p_list (fn (_, e) =>
adamc@29 1315 p_exp env e) xes,
adamc@29 1316 string "};",
adamc@29 1317 space,
adamc@181 1318 string "tmp;",
adamc@29 1319 space,
adamc@29 1320 string "})" ]
adamc@29 1321 | EField (e, x) =>
adamc@29 1322 box [p_exp' true env e,
adamc@311 1323 string ".__uwf_",
adamc@316 1324 p_ident x]
adamc@29 1325
adamc@182 1326 | ECase (e, pes, {disc, result}) =>
adamc@182 1327 let
adamc@182 1328 val final = newGoto ()
adamc@182 1329
adamc@182 1330 val body = foldl (fn ((p, e), body) =>
adamc@182 1331 let
adamc@182 1332 val exit = newGoto ()
adamc@182 1333 val (pr, _) = p_pat_preamble env p
adamc@182 1334 val (p, env) = p_pat (env,
adamc@182 1335 box [string "goto",
adamc@182 1336 space,
adamc@182 1337 exit,
adamc@182 1338 string ";"],
adamc@182 1339 0) p
adamc@182 1340 in
adamc@182 1341 box [body,
adamc@182 1342 box [string "{",
adamc@182 1343 newline,
adamc@182 1344 pr,
adamc@182 1345 newline,
adamc@182 1346 p,
adamc@182 1347 newline,
adamc@182 1348 string "result",
adamc@182 1349 space,
adamc@182 1350 string "=",
adamc@182 1351 space,
adamc@182 1352 p_exp env e,
adamc@182 1353 string ";",
adamc@182 1354 newline,
adamc@182 1355 string "goto",
adamc@182 1356 space,
adamc@182 1357 final,
adamc@182 1358 string ";",
adamc@182 1359 newline,
adamc@182 1360 string "}"],
adamc@182 1361 newline,
adamc@182 1362 exit,
adamc@182 1363 string ":",
adamc@182 1364 newline]
adamc@182 1365 end) (box []) pes
adamc@182 1366 in
adamc@182 1367 box [string "({",
adamc@182 1368 newline,
adamc@182 1369 p_typ env disc,
adamc@182 1370 space,
adamc@182 1371 string "disc0",
adamc@182 1372 space,
adamc@182 1373 string "=",
adamc@182 1374 space,
adamc@182 1375 p_exp env e,
adamc@182 1376 string ";",
adamc@182 1377 newline,
adamc@182 1378 p_typ env result,
adamc@182 1379 space,
adamc@182 1380 string "result;",
adamc@182 1381 newline,
adamc@182 1382 body,
adamc@311 1383 string "uw_error(ctx, FATAL, \"",
adamc@182 1384 string (ErrorMsg.spanToString loc),
adamc@182 1385 string ": pattern match failure\");",
adamc@182 1386 newline,
adamc@182 1387 final,
adamc@182 1388 string ":",
adamc@182 1389 space,
adamc@182 1390 string "result;",
adamc@182 1391 newline,
adamc@182 1392 string "})"]
adamc@182 1393 end
adamc@181 1394
adamc@311 1395 | EWrite e => box [string "(uw_write(ctx, ",
adamc@102 1396 p_exp env e,
adamc@311 1397 string "), uw_unit_v)"]
adamc@102 1398
adamc@106 1399 | ESeq (e1, e2) => box [string "(",
adamc@106 1400 p_exp env e1,
adamc@106 1401 string ",",
adamc@106 1402 space,
adamc@106 1403 p_exp env e2,
adamc@106 1404 string ")"]
adamc@269 1405 | ELet (x, t, e1, e2) => box [string "({",
adamc@269 1406 newline,
adamc@269 1407 p_typ env t,
adamc@269 1408 space,
adamc@311 1409 string "__uwr_",
adamc@316 1410 p_ident x,
adamc@272 1411 string "_",
adamc@272 1412 string (Int.toString (E.countERels env)),
adamc@269 1413 space,
adamc@269 1414 string "=",
adamc@269 1415 space,
adamc@269 1416 p_exp env e1,
adamc@269 1417 string ";",
adamc@269 1418 newline,
adamc@269 1419 p_exp (E.pushERel env x t) e2,
adamc@269 1420 string ";",
adamc@269 1421 newline,
adamc@269 1422 string "})"]
adamc@269 1423
adamc@282 1424 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
adamc@278 1425 let
adamc@316 1426 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
adamc@278 1427 val tables = ListUtil.mapConcat (fn (x, xts) =>
adamc@316 1428 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
adamc@278 1429 tables
adamc@638 1430
adamc@278 1431 val outputs = exps @ tables
adamc@325 1432 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
adamc@324 1433
adamc@324 1434 val wontLeakStrings = notLeaky env true state
adamc@324 1435 val wontLeakAnything = notLeaky env false state
adamc@278 1436 in
adamc@640 1437 box [if wontLeakAnything then
adamc@704 1438 string "(uw_begin_region(ctx), "
adamc@324 1439 else
adamc@324 1440 box [],
adamc@324 1441 string "({",
adamc@278 1442 newline,
adamc@311 1443 string "PGconn *conn = uw_get_db(ctx);",
adamc@278 1444 newline,
adamc@640 1445 p_typ env state,
adamc@640 1446 space,
adamc@640 1447 string "acc",
adamc@640 1448 space,
adamc@640 1449 string "=",
adamc@640 1450 space,
adamc@640 1451 p_exp env initial,
adamc@640 1452 string ";",
adamc@640 1453 newline,
adamc@640 1454 string "int n, i, dummy = (uw_begin_region(ctx), 0);",
adamc@640 1455 newline,
adamc@640 1456
adamc@282 1457 case prepared of
adamc@282 1458 NONE => box [string "char *query = ",
adamc@282 1459 p_exp env query,
adamc@282 1460 string ";",
adamc@282 1461 newline]
adamc@282 1462 | SOME _ =>
adamc@282 1463 let
adamc@282 1464 val ets = getPargs query
adamc@282 1465 in
adamc@282 1466 box [p_list_sepi newline
adamc@282 1467 (fn i => fn (e, t) =>
adamc@282 1468 box [p_sql_type t,
adamc@282 1469 space,
adamc@282 1470 string "arg",
adamc@282 1471 string (Int.toString (i + 1)),
adamc@282 1472 space,
adamc@282 1473 string "=",
adamc@282 1474 space,
adamc@282 1475 p_exp env e,
adamc@282 1476 string ";"])
adamc@282 1477 ets,
adamc@282 1478 newline,
adamc@282 1479 newline,
adamc@282 1480
adamc@282 1481 string "const char *paramValues[] = { ",
adamc@282 1482 p_list_sepi (box [string ",", space])
adamc@282 1483 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@282 1484 string (Int.toString (i + 1))]))
adamc@282 1485 ets,
adamc@282 1486 string " };",
adamc@282 1487 newline,
adamc@282 1488 newline]
adamc@282 1489 end,
adamc@640 1490
adamc@282 1491 string "PGresult *res = ",
adamc@282 1492 case prepared of
adamc@295 1493 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1494 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@282 1495 string (Int.toString n),
adamc@282 1496 string "\", ",
adamc@282 1497 string (Int.toString (length (getPargs query))),
adamc@295 1498 string ", paramValues, NULL, NULL, 0);"],
adamc@278 1499 newline,
adamc@278 1500 newline,
adamc@277 1501
adamc@311 1502 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@278 1503 newline,
adamc@278 1504 newline,
adamc@277 1505
adamc@278 1506 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@278 1507 newline,
adamc@278 1508 box [string "PQclear(res);",
adamc@278 1509 newline,
adamc@311 1510 string "uw_error(ctx, FATAL, \"",
adamc@278 1511 string (ErrorMsg.spanToString loc),
adamc@282 1512 string ": Query failed:\\n%s\\n%s\", ",
adamc@282 1513 case prepared of
adamc@282 1514 NONE => string "query"
adamc@282 1515 | SOME _ => p_exp env query,
adamc@282 1516 string ", PQerrorMessage(conn));",
adamc@278 1517 newline],
adamc@278 1518 string "}",
adamc@278 1519 newline,
adamc@278 1520 newline,
adamc@277 1521
adamc@323 1522 string "uw_end_region(ctx);",
adamc@323 1523 newline,
adamc@425 1524 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
adamc@425 1525 newline,
adamc@278 1526 string "n = PQntuples(res);",
adamc@278 1527 newline,
adamc@278 1528 string "for (i = 0; i < n; ++i) {",
adamc@278 1529 newline,
adamc@278 1530 box [string "struct",
adamc@278 1531 space,
adamc@311 1532 string "__uws_",
adamc@278 1533 string (Int.toString rnum),
adamc@278 1534 space,
adamc@311 1535 string "__uwr_r_",
adamc@278 1536 string (Int.toString (E.countERels env)),
adamc@278 1537 string ";",
adamc@278 1538 newline,
adamc@278 1539 p_typ env state,
adamc@278 1540 space,
adamc@311 1541 string "__uwr_acc_",
adamc@278 1542 string (Int.toString (E.countERels env + 1)),
adamc@278 1543 space,
adamc@278 1544 string "=",
adamc@278 1545 space,
adamc@278 1546 string "acc;",
adamc@278 1547 newline,
adamc@278 1548 newline,
adamc@278 1549
adamc@278 1550 p_list_sepi (box []) (fn i =>
adamc@278 1551 fn (proj, t) =>
adamc@311 1552 box [string "__uwr_r_",
adamc@278 1553 string (Int.toString (E.countERels env)),
adamc@278 1554 string ".",
adamc@278 1555 string proj,
adamc@278 1556 space,
adamc@278 1557 string "=",
adamc@278 1558 space,
adamc@467 1559 p_getcol wontLeakStrings env t i,
adamc@278 1560 string ";",
adamc@278 1561 newline]) outputs,
adamc@278 1562
adamc@278 1563 newline,
adamc@278 1564 newline,
adamc@278 1565
adamc@278 1566 string "acc",
adamc@278 1567 space,
adamc@278 1568 string "=",
adamc@278 1569 space,
adamc@278 1570 p_exp (E.pushERel
adamc@278 1571 (E.pushERel env "r" (TRecord rnum, loc))
adamc@278 1572 "acc" state)
adamc@278 1573 body,
adamc@278 1574 string ";",
adamc@278 1575 newline],
adamc@278 1576 string "}",
adamc@278 1577 newline,
adamc@278 1578 newline,
adamc@425 1579 string "uw_pop_cleanup(ctx);",
adamc@278 1580 newline,
adamc@324 1581 if wontLeakAnything then
adamc@324 1582 box [string "uw_end_region(ctx);",
adamc@324 1583 newline]
adamc@324 1584 else
adamc@324 1585 box [],
adamc@278 1586 string "acc;",
adamc@278 1587 newline,
adamc@704 1588 string "})",
adamc@704 1589 if wontLeakAnything then
adamc@704 1590 string ")"
adamc@704 1591 else
adamc@704 1592 box []]
adamc@278 1593 end
adamc@106 1594
adamc@307 1595 | EDml {dml, prepared} =>
adamc@337 1596 box [string "(uw_begin_region(ctx), ({",
adamc@307 1597 newline,
adamc@311 1598 string "PGconn *conn = uw_get_db(ctx);",
adamc@307 1599 newline,
adamc@307 1600 case prepared of
adamc@307 1601 NONE => box [string "char *dml = ",
adamc@307 1602 p_exp env dml,
adamc@307 1603 string ";",
adamc@307 1604 newline]
adamc@307 1605 | SOME _ =>
adamc@307 1606 let
adamc@307 1607 val ets = getPargs dml
adamc@307 1608 in
adamc@307 1609 box [p_list_sepi newline
adamc@307 1610 (fn i => fn (e, t) =>
adamc@307 1611 box [p_sql_type t,
adamc@307 1612 space,
adamc@307 1613 string "arg",
adamc@307 1614 string (Int.toString (i + 1)),
adamc@307 1615 space,
adamc@307 1616 string "=",
adamc@307 1617 space,
adamc@307 1618 p_exp env e,
adamc@307 1619 string ";"])
adamc@307 1620 ets,
adamc@307 1621 newline,
adamc@307 1622 newline,
adamc@307 1623
adamc@307 1624 string "const char *paramValues[] = { ",
adamc@307 1625 p_list_sepi (box [string ",", space])
adamc@307 1626 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@307 1627 string (Int.toString (i + 1))]))
adamc@307 1628 ets,
adamc@307 1629 string " };",
adamc@307 1630 newline,
adamc@307 1631 newline]
adamc@307 1632 end,
adamc@307 1633 newline,
adamc@307 1634 newline,
adamc@307 1635 string "PGresult *res = ",
adamc@307 1636 case prepared of
adamc@307 1637 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1638 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@307 1639 string (Int.toString n),
adamc@307 1640 string "\", ",
adamc@307 1641 string (Int.toString (length (getPargs dml))),
adamc@307 1642 string ", paramValues, NULL, NULL, 0);"],
adamc@307 1643 newline,
adamc@307 1644 newline,
adamc@307 1645
adamc@311 1646 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
adamc@307 1647 newline,
adamc@307 1648 newline,
adamc@307 1649
adamc@307 1650 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@307 1651 newline,
adamc@688 1652 box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {",
adamc@688 1653 box [newline,
adamc@688 1654 string "PQclear(res);",
adamc@688 1655 newline,
adamc@688 1656 string "uw_error(ctx, UNLIMITED_RETRY, \"Serialization failure\");",
adamc@688 1657 newline],
adamc@688 1658 string "}",
adamc@688 1659 newline,
adamc@688 1660 string "PQclear(res);",
adamc@307 1661 newline,
adamc@311 1662 string "uw_error(ctx, FATAL, \"",
adamc@307 1663 string (ErrorMsg.spanToString loc),
adamc@307 1664 string ": DML failed:\\n%s\\n%s\", ",
adamc@307 1665 case prepared of
adamc@307 1666 NONE => string "dml"
adamc@307 1667 | SOME _ => p_exp env dml,
adamc@307 1668 string ", PQerrorMessage(conn));",
adamc@307 1669 newline],
adamc@307 1670 string "}",
adamc@307 1671 newline,
adamc@307 1672 newline,
adamc@307 1673
adamc@307 1674 string "PQclear(res);",
adamc@307 1675 newline,
adamc@337 1676 string "uw_end_region(ctx);",
adamc@337 1677 newline,
adamc@311 1678 string "uw_unit_v;",
adamc@307 1679 newline,
adamc@337 1680 string "}))"]
adamc@307 1681
adamc@338 1682 | ENextval {seq, prepared} =>
adamc@338 1683 let
adamc@486 1684 val query = case seq of
adamc@486 1685 (EPrim (Prim.String s), loc) =>
adamc@486 1686 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
adamc@486 1687 | _ =>
adamc@486 1688 let
adamc@486 1689 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@486 1690 in
adamc@486 1691 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
adamc@486 1692 end
adamc@338 1693 in
adamc@338 1694 box [string "(uw_begin_region(ctx), ",
adamc@338 1695 string "({",
adamc@338 1696 newline,
adamc@338 1697 string "PGconn *conn = uw_get_db(ctx);",
adamc@338 1698 newline,
adamc@338 1699 case prepared of
adamc@338 1700 NONE => box [string "char *query = ",
adamc@338 1701 p_exp env query,
adamc@338 1702 string ";",
adamc@338 1703 newline]
adamc@338 1704 | SOME _ =>
adamc@338 1705 box [],
adamc@338 1706 newline,
adamc@338 1707 string "PGresult *res = ",
adamc@338 1708 case prepared of
adamc@338 1709 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@338 1710 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@338 1711 string (Int.toString n),
adamc@338 1712 string "\", 0, NULL, NULL, NULL, 0);"],
adamc@338 1713 newline,
adamc@338 1714 string "uw_Basis_int n;",
adamc@338 1715 newline,
adamc@338 1716 newline,
adamc@338 1717
adamc@338 1718 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
adamc@338 1719 newline,
adamc@338 1720 newline,
adamc@338 1721
adamc@338 1722 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@338 1723 newline,
adamc@338 1724 box [string "PQclear(res);",
adamc@338 1725 newline,
adamc@338 1726 string "uw_error(ctx, FATAL, \"",
adamc@338 1727 string (ErrorMsg.spanToString loc),
adamc@338 1728 string ": Query failed:\\n%s\\n%s\", ",
adamc@338 1729 case prepared of
adamc@338 1730 NONE => string "query"
adamc@338 1731 | SOME _ => p_exp env query,
adamc@338 1732 string ", PQerrorMessage(conn));",
adamc@338 1733 newline],
adamc@338 1734 string "}",
adamc@338 1735 newline,
adamc@338 1736 newline,
adamc@338 1737
adamc@338 1738 string "uw_end_region(ctx);",
adamc@338 1739 newline,
adamc@338 1740 string "n = PQntuples(res);",
adamc@338 1741 newline,
adamc@338 1742 string "if (n != 1) {",
adamc@338 1743 newline,
adamc@338 1744 box [string "PQclear(res);",
adamc@338 1745 newline,
adamc@338 1746 string "uw_error(ctx, FATAL, \"",
adamc@338 1747 string (ErrorMsg.spanToString loc),
adamc@338 1748 string ": Wrong number of result rows:\\n%s\\n%s\", ",
adamc@338 1749 case prepared of
adamc@338 1750 NONE => string "query"
adamc@338 1751 | SOME _ => p_exp env query,
adamc@338 1752 string ", PQerrorMessage(conn));",
adamc@338 1753 newline],
adamc@338 1754 string "}",
adamc@338 1755 newline,
adamc@338 1756 newline,
adamc@338 1757
adamc@338 1758 string "n = ",
adamc@338 1759 p_unsql true env (TFfi ("Basis", "int"), loc)
adamc@338 1760 (string "PQgetvalue(res, 0, 0)"),
adamc@338 1761 string ";",
adamc@338 1762 newline,
adamc@338 1763 string "PQclear(res);",
adamc@338 1764 newline,
adamc@338 1765 string "n;",
adamc@338 1766 newline,
adamc@338 1767 string "}))"]
adamc@338 1768 end
adamc@338 1769
adamc@463 1770 | EUnurlify (e, t) =>
adamc@463 1771 let
adamc@463 1772 fun getIt () =
adamc@463 1773 if isUnboxable t then
adamc@463 1774 unurlify env t
adamc@463 1775 else
adamc@463 1776 box [string "({",
adamc@463 1777 newline,
adamc@463 1778 p_typ env t,
adamc@463 1779 string " *tmp = uw_malloc(ctx, sizeof(",
adamc@463 1780 p_typ env t,
adamc@463 1781 string "));",
adamc@463 1782 newline,
adamc@463 1783 string "*tmp = ",
adamc@463 1784 unurlify env t,
adamc@463 1785 string ";",
adamc@463 1786 newline,
adamc@463 1787 string "tmp;",
adamc@463 1788 newline,
adamc@463 1789 string "})"]
adamc@463 1790 in
adamc@463 1791 box [string "({",
adamc@463 1792 newline,
adamc@493 1793 string "uw_Basis_string request = uw_Basis_maybe_strdup(ctx, ",
adamc@463 1794 p_exp env e,
adamc@492 1795 string ");",
adamc@463 1796 newline,
adamc@463 1797 newline,
adamc@463 1798 string "(request ? ",
adamc@463 1799 getIt (),
adamc@463 1800 string " : NULL);",
adamc@463 1801 newline,
adamc@463 1802 string "})"]
adamc@463 1803 end
adamc@463 1804
adamc@29 1805 and p_exp env = p_exp' false env
adamc@29 1806
adamc@129 1807 fun p_fun env (fx, n, args, ran, e) =
adamc@129 1808 let
adamc@129 1809 val nargs = length args
adamc@129 1810 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 1811 in
adamc@129 1812 box [string "static",
adamc@129 1813 space,
adamc@129 1814 p_typ env ran,
adamc@129 1815 space,
adamc@316 1816 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@129 1817 string "(",
adamc@129 1818 p_list_sep (box [string ",", space]) (fn x => x)
adamc@311 1819 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 1820 box [p_typ env dom,
adamc@129 1821 space,
adamc@129 1822 p_rel env' (nargs - i - 1)]) args),
adamc@129 1823 string ")",
adamc@129 1824 space,
adamc@129 1825 string "{",
adamc@129 1826 newline,
adamc@638 1827 box [string "return(",
adamc@638 1828 p_exp env' e,
adamc@638 1829 string ");"],
adamc@129 1830 newline,
adamc@129 1831 string "}"]
adamc@129 1832 end
adamc@129 1833
adamc@129 1834 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 1835 case d of
adamc@29 1836 DStruct (n, xts) =>
adamc@196 1837 let
adamc@196 1838 val env = E.declBinds env dAll
adamc@196 1839 in
adamc@196 1840 box [string "struct",
adamc@196 1841 space,
adamc@311 1842 string ("__uws_" ^ Int.toString n),
adamc@196 1843 space,
adamc@196 1844 string "{",
adamc@196 1845 newline,
adamc@196 1846 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@196 1847 space,
adamc@311 1848 string "__uwf_",
adamc@316 1849 p_ident x,
adamc@196 1850 string ";",
adamc@196 1851 newline]) xts,
adamc@196 1852 string "};"]
adamc@196 1853 end
adamc@188 1854 | DDatatype (Enum, x, n, xncs) =>
adamc@188 1855 box [string "enum",
adamc@188 1856 space,
adamc@316 1857 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@188 1858 space,
adamc@188 1859 string "{",
adamc@188 1860 space,
adamc@316 1861 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1862 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@188 1863 space,
adamc@188 1864 string "};"]
adamc@198 1865 | DDatatype (Option, _, _, _) => box []
adamc@188 1866 | DDatatype (Default, x, n, xncs) =>
adamc@165 1867 let
adamc@165 1868 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
adamc@165 1869 | (x, n, SOME t) => SOME (x, n, t)) xncs
adamc@165 1870 in
adamc@165 1871 box [string "enum",
adamc@165 1872 space,
adamc@316 1873 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1874 space,
adamc@165 1875 string "{",
adamc@165 1876 space,
adamc@316 1877 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1878 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@165 1879 space,
adamc@165 1880 string "};",
adamc@165 1881 newline,
adamc@165 1882 newline,
adamc@165 1883 string "struct",
adamc@165 1884 space,
adamc@316 1885 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1886 space,
adamc@165 1887 string "{",
adamc@165 1888 newline,
adamc@165 1889 string "enum",
adamc@165 1890 space,
adamc@316 1891 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1892 space,
adamc@165 1893 string "tag;",
adamc@165 1894 newline,
adamc@165 1895 box (case xncsArgs of
adamc@165 1896 [] => []
adamc@165 1897 | _ => [string "union",
adamc@165 1898 space,
adamc@165 1899 string "{",
adamc@165 1900 newline,
adamc@165 1901 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
adamc@165 1902 space,
adamc@316 1903 string ("uw_" ^ ident x),
adamc@165 1904 string ";"]) xncsArgs,
adamc@165 1905 newline,
adamc@165 1906 string "}",
adamc@165 1907 space,
adamc@165 1908 string "data;",
adamc@165 1909 newline]),
adamc@165 1910 string "};"]
adamc@188 1911 end
adamc@29 1912
adamc@196 1913 | DDatatypeForward _ => box []
adamc@196 1914
adamc@29 1915 | DVal (x, n, t, e) =>
adamc@29 1916 box [p_typ env t,
adamc@29 1917 space,
adamc@316 1918 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
adamc@29 1919 space,
adamc@29 1920 string "=",
adamc@29 1921 space,
adamc@29 1922 p_exp env e,
adamc@29 1923 string ";"]
adamc@129 1924 | DFun vi => p_fun env vi
adamc@129 1925 | DFunRec vis =>
adamc@29 1926 let
adamc@129 1927 val env = E.declBinds env dAll
adamc@29 1928 in
adamc@129 1929 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 1930 box [string "static",
adamc@129 1931 space,
adamc@129 1932 p_typ env ran,
adamc@129 1933 space,
adamc@316 1934 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@311 1935 string "(uw_context,",
adamc@129 1936 space,
adamc@129 1937 p_list_sep (box [string ",", space])
adamc@129 1938 (fn (_, dom) => p_typ env dom) args,
adamc@129 1939 string ");"]) vis,
adamc@29 1940 newline,
adamc@129 1941 p_list_sep newline (p_fun env) vis,
adamc@129 1942 newline]
adamc@29 1943 end
adamc@707 1944 | DTable (x, _, pk, csts) => box [string "/* SQL table ",
adamc@707 1945 string x,
adamc@707 1946 space,
adamc@707 1947 case pk of
adamc@707 1948 "" => box []
adamc@707 1949 | _ => box [string "keys",
adamc@707 1950 space,
adamc@707 1951 string pk,
adamc@707 1952 space],
adamc@707 1953 string "constraints",
adamc@707 1954 space,
adamc@707 1955 p_list (fn (x, v) => box [string x,
adamc@707 1956 space,
adamc@707 1957 string ":",
adamc@707 1958 space,
adamc@707 1959 string v]) csts,
adamc@707 1960 space,
adamc@707 1961 string " */",
adamc@707 1962 newline]
adamc@338 1963 | DSequence x => box [string "/* SQL sequence ",
adamc@338 1964 string x,
adamc@338 1965 string " */",
adamc@338 1966 newline]
adamc@687 1967 | DDatabase {name, expunge, initialize} =>
adamc@687 1968 box [string "static void uw_db_validate(uw_context);",
adamc@687 1969 newline,
adamc@687 1970 string "static void uw_db_prepare(uw_context);",
adamc@687 1971 newline,
adamc@687 1972 newline,
adamc@687 1973 string "void uw_db_init(uw_context ctx) {",
adamc@687 1974 newline,
adamc@687 1975 string "PGconn *conn = PQconnectdb(\"",
adamc@687 1976 string (String.toString name),
adamc@687 1977 string "\");",
adamc@687 1978 newline,
adamc@687 1979 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
adamc@687 1980 string "\"libpq can't allocate a connection.\");",
adamc@687 1981 newline,
adamc@687 1982 string "if (PQstatus(conn) != CONNECTION_OK) {",
adamc@687 1983 newline,
adamc@687 1984 box [string "char msg[1024];",
adamc@687 1985 newline,
adamc@687 1986 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@687 1987 newline,
adamc@687 1988 string "msg[1023] = 0;",
adamc@687 1989 newline,
adamc@687 1990 string "PQfinish(conn);",
adamc@687 1991 newline,
adamc@687 1992 string "uw_error(ctx, BOUNDED_RETRY, ",
adamc@687 1993 string "\"Connection to Postgres server failed: %s\", msg);"],
adamc@687 1994 newline,
adamc@687 1995 string "}",
adamc@687 1996 newline,
adamc@687 1997 string "uw_set_db(ctx, conn);",
adamc@687 1998 newline,
adamc@687 1999 string "uw_db_validate(ctx);",
adamc@687 2000 newline,
adamc@687 2001 string "uw_db_prepare(ctx);",
adamc@687 2002 newline,
adamc@687 2003 string "}",
adamc@687 2004 newline,
adamc@687 2005 newline,
adamc@687 2006 string "void uw_db_close(uw_context ctx) {",
adamc@687 2007 newline,
adamc@687 2008 string "PQfinish(uw_get_db(ctx));",
adamc@687 2009 newline,
adamc@687 2010 string "}",
adamc@687 2011 newline,
adamc@687 2012 newline,
adamc@424 2013
adamc@687 2014 string "int uw_db_begin(uw_context ctx) {",
adamc@687 2015 newline,
adamc@687 2016 string "PGconn *conn = uw_get_db(ctx);",
adamc@687 2017 newline,
adamc@687 2018 string "PGresult *res = PQexec(conn, \"BEGIN ISOLATION LEVEL SERIALIZABLE\");",
adamc@687 2019 newline,
adamc@687 2020 newline,
adamc@687 2021 string "if (res == NULL) return 1;",
adamc@687 2022 newline,
adamc@687 2023 newline,
adamc@687 2024 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@687 2025 box [string "PQclear(res);",
adamc@687 2026 newline,
adamc@687 2027 string "return 1;",
adamc@687 2028 newline],
adamc@687 2029 string "}",
adamc@687 2030 newline,
adamc@687 2031 string "return 0;",
adamc@687 2032 newline,
adamc@687 2033 string "}",
adamc@687 2034 newline,
adamc@687 2035 newline,
adamc@424 2036
adamc@687 2037 string "int uw_db_commit(uw_context ctx) {",
adamc@687 2038 newline,
adamc@687 2039 string "PGconn *conn = uw_get_db(ctx);",
adamc@687 2040 newline,
adamc@687 2041 string "PGresult *res = PQexec(conn, \"COMMIT\");",
adamc@687 2042 newline,
adamc@687 2043 newline,
adamc@687 2044 string "if (res == NULL) return 1;",
adamc@687 2045 newline,
adamc@687 2046 newline,
adamc@687 2047 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@687 2048 box [string "PQclear(res);",
adamc@687 2049 newline,
adamc@687 2050 string "return 1;",
adamc@687 2051 newline],
adamc@687 2052 string "}",
adamc@687 2053 newline,
adamc@687 2054 string "return 0;",
adamc@687 2055 newline,
adamc@687 2056 string "}",
adamc@687 2057 newline,
adamc@687 2058 newline,
adamc@424 2059
adamc@687 2060 string "int uw_db_rollback(uw_context ctx) {",
adamc@687 2061 newline,
adamc@687 2062 string "PGconn *conn = uw_get_db(ctx);",
adamc@687 2063 newline,
adamc@687 2064 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
adamc@687 2065 newline,
adamc@687 2066 newline,
adamc@687 2067 string "if (res == NULL) return 1;",
adamc@687 2068 newline,
adamc@687 2069 newline,
adamc@687 2070 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@687 2071 box [string "PQclear(res);",
adamc@687 2072 newline,
adamc@687 2073 string "return 1;",
adamc@687 2074 newline],
adamc@687 2075 string "}",
adamc@687 2076 newline,
adamc@687 2077 string "return 0;",
adamc@687 2078 newline,
adamc@687 2079 string "}",
adamc@687 2080 newline,
adamc@687 2081 newline,
adamc@683 2082
adamc@687 2083 string "void uw_expunger(uw_context ctx, uw_Basis_client cli) {",
adamc@687 2084 newline,
adamc@687 2085 box [p_enamed env expunge,
adamc@687 2086 string "(ctx, cli);",
adamc@687 2087 newline],
adamc@687 2088 string "}",
adamc@687 2089 newline,
adamc@687 2090 newline,
adamc@687 2091
adamc@687 2092 string "void uw_initializer(uw_context ctx) {",
adamc@687 2093 newline,
adamc@687 2094 box [p_enamed env initialize,
adamc@687 2095 string "(ctx, uw_unit_v);",
adamc@687 2096 newline],
adamc@687 2097 string "}",
adamc@687 2098 newline]
adamc@29 2099
adamc@467 2100 | DPreparedStatements [] =>
adamc@467 2101 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@467 2102 newline,
adamc@467 2103 string "}"]
adamc@282 2104 | DPreparedStatements ss =>
adamc@311 2105 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@282 2106 newline,
adamc@311 2107 string "PGconn *conn = uw_get_db(ctx);",
adamc@282 2108 newline,
adamc@282 2109 string "PGresult *res;",
adamc@282 2110 newline,
adamc@282 2111 newline,
adamc@282 2112
adamc@282 2113 p_list_sepi newline (fn i => fn (s, n) =>
adamc@311 2114 box [string "res = PQprepare(conn, \"uw",
adamc@282 2115 string (Int.toString i),
adamc@282 2116 string "\", \"",
adamc@282 2117 string (String.toString s),
adamc@282 2118 string "\", ",
adamc@282 2119 string (Int.toString n),
adamc@282 2120 string ", NULL);",
adamc@282 2121 newline,
adamc@282 2122 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@282 2123 newline,
adamc@282 2124 box [string "char msg[1024];",
adamc@282 2125 newline,
adamc@282 2126 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@282 2127 newline,
adamc@282 2128 string "msg[1023] = 0;",
adamc@282 2129 newline,
adamc@282 2130 string "PQclear(res);",
adamc@282 2131 newline,
adamc@282 2132 string "PQfinish(conn);",
adamc@282 2133 newline,
adamc@311 2134 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
adamc@282 2135 string (String.toString s),
adamc@282 2136 string "\\n%s\", msg);",
adamc@282 2137 newline],
adamc@282 2138 string "}",
adamc@282 2139 newline,
adamc@282 2140 string "PQclear(res);",
adamc@282 2141 newline])
adamc@282 2142 ss,
adamc@282 2143
adamc@282 2144 string "}"]
adamc@282 2145
adamc@569 2146 | DJavaScript s => box [string "static char jslib[] = \"",
adamc@569 2147 string (String.toString s),
adamc@569 2148 string "\";"]
adamc@725 2149 | DCookie s => box [string "/*",
adamc@725 2150 space,
adamc@725 2151 string "cookie",
adamc@725 2152 space,
adamc@725 2153 string s,
adamc@725 2154 space,
adamc@725 2155 string "*/"]
adamc@720 2156 | DStyle s => box [string "/*",
adamc@720 2157 space,
adamc@720 2158 string "style",
adamc@720 2159 space,
adamc@720 2160 string s,
adamc@720 2161 space,
adamc@720 2162 string "*/"]
adamc@569 2163
adamc@144 2164 datatype 'a search =
adamc@144 2165 Found of 'a
adamc@144 2166 | NotFound
adamc@144 2167 | Error
adamc@120 2168
adamc@467 2169 fun p_sqltype'' env (tAll as (t, loc)) =
adamc@275 2170 case t of
adamc@275 2171 TFfi ("Basis", "int") => "int8"
adamc@275 2172 | TFfi ("Basis", "float") => "float8"
adamc@275 2173 | TFfi ("Basis", "string") => "text"
adamc@275 2174 | TFfi ("Basis", "bool") => "bool"
adamc@438 2175 | TFfi ("Basis", "time") => "timestamp"
adamc@682 2176 | TFfi ("Basis", "channel") => "int8"
adamc@682 2177 | TFfi ("Basis", "client") => "int4"
adamc@275 2178 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
adamc@275 2179 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@275 2180 "ERROR")
adamc@275 2181
adamc@467 2182 fun p_sqltype' env (tAll as (t, loc)) =
adamc@467 2183 case t of
adamc@467 2184 (TOption t, _) => p_sqltype'' env t
adamc@467 2185 | _ => p_sqltype'' env t ^ " NOT NULL"
adamc@467 2186
adamc@275 2187 fun p_sqltype env t = string (p_sqltype' env t)
adamc@101 2188
adamc@467 2189 fun p_sqltype_base' env t =
adamc@467 2190 case t of
adamc@467 2191 (TOption t, _) => p_sqltype'' env t
adamc@467 2192 | _ => p_sqltype'' env t
adamc@467 2193
adamc@467 2194 fun p_sqltype_base env t = string (p_sqltype_base' env t)
adamc@467 2195
adamc@467 2196 fun is_not_null t =
adamc@467 2197 case t of
adamc@467 2198 (TOption _, _) => false
adamc@467 2199 | _ => true
adamc@467 2200
adamc@734 2201 fun sigName fields =
adamc@734 2202 let
adamc@734 2203 fun inFields s = List.exists (fn (s', _) => s' = s) fields
adamc@734 2204
adamc@734 2205 fun getSigName n =
adamc@734 2206 let
adamc@734 2207 val s = "Sig" ^ Int.toString n
adamc@734 2208 in
adamc@734 2209 if inFields s then
adamc@734 2210 getSigName (n + 1)
adamc@734 2211 else
adamc@734 2212 s
adamc@734 2213 end
adamc@734 2214 in
adamc@734 2215 if inFields "Sig" then
adamc@734 2216 getSigName 0
adamc@734 2217 else
adamc@734 2218 "Sig"
adamc@734 2219 end
adamc@734 2220
adamc@101 2221 fun p_file env (ds, ps) =
adamc@29 2222 let
adamc@101 2223 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@638 2224 (p_decl env d,
adamc@638 2225 E.declBinds env d))
adamc@101 2226 env ds
adamc@144 2227
adamc@643 2228 val fields = foldl (fn ((ek, _, _, ts, _, _), fields) =>
adamc@144 2229 case ek of
adamc@735 2230 Link => fields
adamc@735 2231 | Rpc _ => fields
adamc@735 2232 | Action eff =>
adamc@280 2233 case List.nth (ts, length ts - 2) of
adamc@144 2234 (TRecord i, _) =>
adamc@144 2235 let
adamc@144 2236 val xts = E.lookupStruct env i
adamc@735 2237 val xts = case eff of
adamc@735 2238 ReadCookieWrite =>
adamc@735 2239 (sigName xts, (TRecord 0, ErrorMsg.dummySpan)) :: xts
adamc@735 2240 | _ => xts
adamc@144 2241 val xtsSet = SS.addList (SS.empty, map #1 xts)
adamc@144 2242 in
adamc@144 2243 foldl (fn ((x, _), fields) =>
adamc@144 2244 let
adamc@144 2245 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
adamc@144 2246 in
adamc@144 2247 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
adamc@144 2248 xtsSet'))
adamc@144 2249 end) fields xts
adamc@144 2250 end
adamc@144 2251 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
adamc@144 2252 SM.empty ps
adamc@144 2253
adamc@144 2254 val fnums = SM.foldli (fn (x, xs, fnums) =>
adamc@144 2255 let
adamc@144 2256 val unusable = SS.foldl (fn (x', unusable) =>
adamc@144 2257 case SM.find (fnums, x') of
adamc@144 2258 NONE => unusable
adamc@144 2259 | SOME n => IS.add (unusable, n))
adamc@144 2260 IS.empty xs
adamc@144 2261
adamc@144 2262 fun findAvailable n =
adamc@144 2263 if IS.member (unusable, n) then
adamc@144 2264 findAvailable (n + 1)
adamc@144 2265 else
adamc@144 2266 n
adamc@144 2267 in
adamc@144 2268 SM.insert (fnums, x, findAvailable 0)
adamc@144 2269 end)
adamc@144 2270 SM.empty fields
adamc@144 2271
adamc@734 2272 val cookies = List.mapPartial (fn (DCookie s, _) => SOME s | _ => NONE) ds
adamc@734 2273
adamc@144 2274 fun makeSwitch (fnums, i) =
adamc@144 2275 case SM.foldl (fn (n, NotFound) => Found n
adamc@144 2276 | (n, Error) => Error
adamc@144 2277 | (n, Found n') => if n = n' then
adamc@144 2278 Found n'
adamc@144 2279 else
adamc@144 2280 Error) NotFound fnums of
adamc@144 2281 NotFound => box [string "return",
adamc@144 2282 space,
adamc@144 2283 string "-1;"]
adamc@144 2284 | Found n => box [string "return",
adamc@144 2285 space,
adamc@144 2286 string (Int.toString n),
adamc@144 2287 string ";"]
adamc@144 2288 | Error =>
adamc@144 2289 let
adamc@144 2290 val cmap = SM.foldli (fn (x, n, cmap) =>
adamc@144 2291 let
adamc@144 2292 val ch = if i < size x then
adamc@144 2293 String.sub (x, i)
adamc@144 2294 else
adamc@144 2295 chr 0
adamc@144 2296
adamc@144 2297 val fnums = case CM.find (cmap, ch) of
adamc@144 2298 NONE => SM.empty
adamc@144 2299 | SOME fnums => fnums
adamc@144 2300 val fnums = SM.insert (fnums, x, n)
adamc@144 2301 in
adamc@144 2302 CM.insert (cmap, ch, fnums)
adamc@144 2303 end)
adamc@144 2304 CM.empty fnums
adamc@144 2305
adamc@144 2306 val cmap = CM.listItemsi cmap
adamc@144 2307 in
adamc@144 2308 case cmap of
adamc@144 2309 [(_, fnums)] =>
adamc@144 2310 box [string "if",
adamc@144 2311 space,
adamc@144 2312 string "(name[",
adamc@144 2313 string (Int.toString i),
adamc@144 2314 string "]",
adamc@144 2315 space,
adamc@144 2316 string "==",
adamc@144 2317 space,
adamc@144 2318 string "0)",
adamc@144 2319 space,
adamc@144 2320 string "return",
adamc@144 2321 space,
adamc@144 2322 string "-1;",
adamc@144 2323 newline,
adamc@144 2324 makeSwitch (fnums, i+1)]
adamc@144 2325 | _ =>
adamc@144 2326 box [string "switch",
adamc@144 2327 space,
adamc@144 2328 string "(name[",
adamc@144 2329 string (Int.toString i),
adamc@144 2330 string "])",
adamc@144 2331 space,
adamc@144 2332 string "{",
adamc@144 2333 newline,
adamc@144 2334 box (map (fn (ch, fnums) =>
adamc@144 2335 box [string "case",
adamc@144 2336 space,
adamc@144 2337 if ch = chr 0 then
adamc@144 2338 string "0:"
adamc@144 2339 else
adamc@144 2340 box [string "'",
adamc@144 2341 string (Char.toString ch),
adamc@144 2342 string "':"],
adamc@144 2343 newline,
adamc@144 2344 makeSwitch (fnums, i+1),
adamc@144 2345 newline]) cmap),
adamc@144 2346 string "default:",
adamc@144 2347 newline,
adamc@144 2348 string "return",
adamc@144 2349 space,
adamc@144 2350 string "-1;",
adamc@144 2351 newline,
adamc@144 2352 string "}"]
adamc@144 2353 end
adamc@144 2354
adamc@643 2355 fun p_page (ek, s, n, ts, ran, side) =
adamc@144 2356 let
adamc@734 2357 val (ts, defInputs, inputsVar, fields) =
adamc@144 2358 case ek of
adamc@734 2359 Core.Link => (List.take (ts, length ts - 1), string "", string "", NONE)
adamc@734 2360 | Core.Rpc _ => (List.take (ts, length ts - 1), string "", string "", NONE)
adamc@731 2361 | Core.Action _ =>
adamc@280 2362 case List.nth (ts, length ts - 2) of
adamc@144 2363 (TRecord i, _) =>
adamc@144 2364 let
adamc@144 2365 val xts = E.lookupStruct env i
adamc@144 2366 in
adamc@280 2367 (List.take (ts, length ts - 2),
adamc@144 2368 box [box (map (fn (x, t) => box [p_typ env t,
adamc@144 2369 space,
adamc@311 2370 string "uw_input_",
adamc@316 2371 p_ident x,
adamc@144 2372 string ";",
adamc@144 2373 newline]) xts),
adamc@144 2374 newline,
adamc@144 2375 box (map (fn (x, t) =>
adamc@144 2376 let
adamc@144 2377 val n = case SM.find (fnums, x) of
adamc@144 2378 NONE => raise Fail "CjrPrint: Can't find in fnums"
adamc@144 2379 | SOME n => n
adamc@190 2380
adamc@190 2381 val f = case t of
adamc@190 2382 (TFfi ("Basis", "bool"), _) => "optional_"
adamc@190 2383 | _ => ""
adamc@144 2384 in
adamc@311 2385 box [string "request = uw_get_",
adamc@190 2386 string f,
adamc@190 2387 string "input(ctx, ",
adamc@144 2388 string (Int.toString n),
adamc@144 2389 string ");",
adamc@144 2390 newline,
adamc@433 2391 string "if (request == NULL)",
adamc@144 2392 newline,
adamc@433 2393 box [string "uw_error(ctx, FATAL, \"Missing input ",
adamc@144 2394 string x,
adamc@433 2395 string "\");"],
adamc@144 2396 newline,
adamc@311 2397 string "uw_input_",
adamc@316 2398 p_ident x,
adamc@144 2399 space,
adamc@144 2400 string "=",
adamc@144 2401 space,
adamc@463 2402 unurlify env t,
adamc@144 2403 string ";",
adamc@144 2404 newline]
adamc@144 2405 end) xts),
adamc@311 2406 string "struct __uws_",
adamc@144 2407 string (Int.toString i),
adamc@144 2408 space,
adamc@311 2409 string "uw_inputs",
adamc@144 2410 space,
adamc@144 2411 string "= {",
adamc@144 2412 newline,
adamc@311 2413 box (map (fn (x, _) => box [string "uw_input_",
adamc@316 2414 p_ident x,
adamc@144 2415 string ",",
adamc@144 2416 newline]) xts),
adamc@144 2417 string "};",
adamc@144 2418 newline],
adamc@144 2419 box [string ",",
adamc@144 2420 space,
adamc@734 2421 string "uw_inputs"],
adamc@734 2422 SOME xts)
adamc@144 2423 end
adamc@144 2424
adamc@144 2425 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
adamc@734 2426
adamc@734 2427 fun couldWrite ek =
adamc@734 2428 case ek of
adamc@734 2429 Link => false
adamc@735 2430 | Action ef => ef = ReadCookieWrite
adamc@735 2431 | Rpc ef => ef = ReadCookieWrite
adamc@144 2432 in
adamc@735 2433 box [string "if (!strncmp(request, \"",
adamc@735 2434 string (String.toString s),
adamc@735 2435 string "\", ",
adamc@735 2436 string (Int.toString (size s)),
adamc@735 2437 string ") && (request[",
adamc@735 2438 string (Int.toString (size s)),
adamc@735 2439 string "] == 0 || request[",
adamc@735 2440 string (Int.toString (size s)),
adamc@735 2441 string "] == '/')) {",
adamc@735 2442 newline,
adamc@735 2443 string "request += ",
adamc@735 2444 string (Int.toString (size s)),
adamc@735 2445 string ";",
adamc@735 2446 newline,
adamc@735 2447 string "if (*request == '/') ++request;",
adamc@735 2448 newline,
adamc@735 2449 if couldWrite ek then
adamc@734 2450 box [string "{",
adamc@734 2451 newline,
adamc@734 2452 string "uw_Basis_string sig = ",
adamc@734 2453 case fields of
adamc@734 2454 NONE => string "uw_Basis_requestHeader(ctx, \"UrWeb-Sig\")"
adamc@734 2455 | SOME fields =>
adamc@734 2456 case SM.find (fnums, sigName fields) of
adamc@734 2457 NONE => raise Fail "CjrPrint: sig name wasn't assigned a number"
adamc@734 2458 | SOME inum =>
adamc@734 2459 string ("uw_get_input(ctx, " ^ Int.toString inum ^ ")"),
adamc@734 2460 string ";",
adamc@734 2461 newline,
adamc@734 2462 string "if (sig == NULL) uw_error(ctx, FATAL, \"Missing cookie signature\");",
adamc@734 2463 newline,
adamc@734 2464 string "if (strcmp(sig, uw_cookie_sig(ctx)))",
adamc@734 2465 newline,
adamc@734 2466 box [string "uw_error(ctx, FATAL, \"Wrong cookie signature\");",
adamc@734 2467 newline],
adamc@734 2468 string "}",
adamc@734 2469 newline]
adamc@734 2470 else
adamc@734 2471 box [],
adamc@609 2472 box (case ek of
adamc@731 2473 Core.Rpc _ => [string "uw_write_header(ctx, \"Content-type: text/plain\\r\\n\");",
adamc@731 2474 newline]
adamc@609 2475 | _ => [string "uw_write_header(ctx, \"Content-type: text/html\\r\\n\");",
adamc@609 2476 newline,
adamc@609 2477 string "uw_write_header(ctx, \"Content-script-type: text/javascript\\r\\n\");",
adamc@609 2478 newline,
adamc@609 2479 string "uw_write(ctx, \"<html>\");",
adamc@643 2480 newline,
adamc@643 2481 string "uw_set_script_header(ctx, \"",
adamc@643 2482 string (case side of
adamc@693 2483 ServerOnly => ""
adamc@693 2484 | _ => "<script src=\\\""
adamc@693 2485 ^ OS.Path.joinDirFile {dir = !Monoize.urlPrefix,
adamc@693 2486 file = "app.js"}
adamc@693 2487 ^ "\\\"></script>\\n"),
adamc@643 2488 string "\");",
adamc@693 2489 newline,
adamc@693 2490 string "uw_set_needs_push(ctx, ",
adamc@693 2491 string (case side of
adamc@693 2492 ServerAndPullAndPush => "1"
adamc@693 2493 | _ => "0"),
adamc@693 2494 string ");",
adamc@693 2495 newline,
adamc@667 2496 string "uw_set_url_prefix(ctx, \"",
adamc@667 2497 string (!Monoize.urlPrefix),
adamc@667 2498 string "\");",
adamc@609 2499 newline]),
adamc@682 2500 string "uw_login(ctx);",
adamc@682 2501 newline,
adamc@144 2502 box [string "{",
adamc@144 2503 newline,
adamc@144 2504 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@144 2505 space,
adamc@144 2506 string "arg",
adamc@144 2507 string (Int.toString i),
adamc@144 2508 space,
adamc@144 2509 string "=",
adamc@144 2510 space,
adamc@463 2511 unurlify env t,
adamc@144 2512 string ";",
adamc@144 2513 newline]) ts),
adamc@144 2514 defInputs,
adamc@609 2515 box (case ek of
adamc@731 2516 Core.Rpc _ => [p_typ env ran,
adamc@731 2517 space,
adamc@731 2518 string "it0",
adamc@731 2519 space,
adamc@731 2520 string "=",
adamc@731 2521 space]
adamc@609 2522 | _ => []),
adamc@144 2523 p_enamed env n,
adamc@144 2524 string "(",
adamc@144 2525 p_list_sep (box [string ",", space])
adamc@144 2526 (fn x => x)
adamc@272 2527 (string "ctx"
adamc@280 2528 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@144 2529 inputsVar,
adamc@311 2530 string ", uw_unit_v);",
adamc@144 2531 newline,
adamc@609 2532 box (case ek of
adamc@731 2533 Core.Rpc _ => [urlify env ran]
adamc@609 2534 | _ => [string "uw_write(ctx, \"</html>\");",
adamc@609 2535 newline]),
adamc@144 2536 string "return;",
adamc@144 2537 newline,
adamc@144 2538 string "}",
adamc@144 2539 newline,
adamc@144 2540 string "}"]
adamc@144 2541 ]
adamc@144 2542 end
adamc@144 2543
adamc@144 2544 val pds' = map p_page ps
adamc@275 2545
adamc@707 2546 val tables = List.mapPartial (fn (DTable (s, xts, _, _), _) => SOME (s, xts)
adamc@275 2547 | _ => NONE) ds
adamc@377 2548 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
adamc@377 2549 | _ => NONE) ds
adamc@275 2550
adamc@275 2551 val validate =
adamc@311 2552 box [string "static void uw_db_validate(uw_context ctx) {",
adamc@275 2553 newline,
adamc@311 2554 string "PGconn *conn = uw_get_db(ctx);",
adamc@275 2555 newline,
adamc@275 2556 string "PGresult *res;",
adamc@275 2557 newline,
adamc@275 2558 newline,
adamc@275 2559 p_list_sep newline
adamc@275 2560 (fn (s, xts) =>
adamc@275 2561 let
adamc@377 2562 val sl = CharVector.map Char.toLower s
adamc@377 2563
adamc@275 2564 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2565 ^ sl ^ "'"
adamc@275 2566
adamc@275 2567 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2568 sl,
adamc@275 2569 "') AND (",
adamc@275 2570 String.concatWith " OR "
adamc@275 2571 (map (fn (x, t) =>
adamc@311 2572 String.concat ["(attname = 'uw_",
adamc@275 2573 CharVector.map
adamc@316 2574 Char.toLower (ident x),
adamc@275 2575 "' AND atttypid = (SELECT oid FROM pg_type",
adamc@275 2576 " WHERE typname = '",
adamc@467 2577 p_sqltype_base' env t,
adamc@467 2578 "') AND attnotnull = ",
adamc@467 2579 if is_not_null t then
adamc@467 2580 "TRUE"
adamc@467 2581 else
adamc@467 2582 "FALSE",
adamc@467 2583 ")"]) xts),
adamc@275 2584 ")"]
adamc@275 2585
adamc@275 2586 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2587 sl,
adamc@325 2588 "') AND attname LIKE 'uw_%'"]
adamc@275 2589 in
adamc@275 2590 box [string "res = PQexec(conn, \"",
adamc@275 2591 string q,
adamc@275 2592 string "\");",
adamc@275 2593 newline,
adamc@275 2594 newline,
adamc@275 2595 string "if (res == NULL) {",
adamc@275 2596 newline,
adamc@275 2597 box [string "PQfinish(conn);",
adamc@275 2598 newline,
adamc@311 2599 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2600 newline],
adamc@275 2601 string "}",
adamc@275 2602 newline,
adamc@275 2603 newline,
adamc@275 2604 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2605 newline,
adamc@275 2606 box [string "char msg[1024];",
adamc@275 2607 newline,
adamc@275 2608 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2609 newline,
adamc@275 2610 string "msg[1023] = 0;",
adamc@275 2611 newline,
adamc@275 2612 string "PQclear(res);",
adamc@275 2613 newline,
adamc@275 2614 string "PQfinish(conn);",
adamc@275 2615 newline,
adamc@311 2616 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2617 string q,
adamc@275 2618 string "\\n%s\", msg);",
adamc@275 2619 newline],
adamc@275 2620 string "}",
adamc@275 2621 newline,
adamc@275 2622 newline,
adamc@275 2623 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@275 2624 newline,
adamc@275 2625 box [string "PQclear(res);",
adamc@275 2626 newline,
adamc@275 2627 string "PQfinish(conn);",
adamc@275 2628 newline,
adamc@311 2629 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2630 string s,
adamc@275 2631 string "' does not exist.\");",
adamc@275 2632 newline],
adamc@275 2633 string "}",
adamc@275 2634 newline,
adamc@275 2635 newline,
adamc@275 2636 string "PQclear(res);",
adamc@275 2637 newline,
adamc@275 2638
adamc@275 2639 string "res = PQexec(conn, \"",
adamc@275 2640 string q',
adamc@275 2641 string "\");",
adamc@275 2642 newline,
adamc@275 2643 newline,
adamc@275 2644 string "if (res == NULL) {",
adamc@275 2645 newline,
adamc@275 2646 box [string "PQfinish(conn);",
adamc@275 2647 newline,
adamc@311 2648 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2649 newline],
adamc@275 2650 string "}",
adamc@275 2651 newline,
adamc@275 2652 newline,
adamc@275 2653 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2654 newline,
adamc@275 2655 box [string "char msg[1024];",
adamc@275 2656 newline,
adamc@275 2657 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2658 newline,
adamc@275 2659 string "msg[1023] = 0;",
adamc@275 2660 newline,
adamc@275 2661 string "PQclear(res);",
adamc@275 2662 newline,
adamc@275 2663 string "PQfinish(conn);",
adamc@275 2664 newline,
adamc@311 2665 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2666 string q',
adamc@275 2667 string "\\n%s\", msg);",
adamc@275 2668 newline],
adamc@275 2669 string "}",
adamc@275 2670 newline,
adamc@275 2671 newline,
adamc@275 2672 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2673 string (Int.toString (length xts)),
adamc@275 2674 string "\")) {",
adamc@275 2675 newline,
adamc@275 2676 box [string "PQclear(res);",
adamc@275 2677 newline,
adamc@275 2678 string "PQfinish(conn);",
adamc@275 2679 newline,
adamc@311 2680 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2681 string s,
adamc@275 2682 string "' has the wrong column types.\");",
adamc@275 2683 newline],
adamc@275 2684 string "}",
adamc@275 2685 newline,
adamc@275 2686 newline,
adamc@275 2687 string "PQclear(res);",
adamc@275 2688 newline,
adamc@275 2689 newline,
adamc@275 2690
adamc@275 2691 string "res = PQexec(conn, \"",
adamc@275 2692 string q'',
adamc@275 2693 string "\");",
adamc@275 2694 newline,
adamc@275 2695 newline,
adamc@275 2696 string "if (res == NULL) {",
adamc@275 2697 newline,
adamc@275 2698 box [string "PQfinish(conn);",
adamc@275 2699 newline,
adamc@311 2700 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2701 newline],
adamc@275 2702 string "}",
adamc@275 2703 newline,
adamc@275 2704 newline,
adamc@275 2705 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2706 newline,
adamc@275 2707 box [string "char msg[1024];",
adamc@275 2708 newline,
adamc@275 2709 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2710 newline,
adamc@275 2711 string "msg[1023] = 0;",
adamc@275 2712 newline,
adamc@275 2713 string "PQclear(res);",
adamc@275 2714 newline,
adamc@275 2715 string "PQfinish(conn);",
adamc@275 2716 newline,
adamc@311 2717 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2718 string q'',
adamc@275 2719 string "\\n%s\", msg);",
adamc@275 2720 newline],
adamc@275 2721 string "}",
adamc@275 2722 newline,
adamc@275 2723 newline,
adamc@275 2724 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2725 string (Int.toString (length xts)),
adamc@275 2726 string "\")) {",
adamc@275 2727 newline,
adamc@275 2728 box [string "PQclear(res);",
adamc@275 2729 newline,
adamc@275 2730 string "PQfinish(conn);",
adamc@275 2731 newline,
adamc@311 2732 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2733 string s,
adamc@275 2734 string "' has extra columns.\");",
adamc@275 2735 newline],
adamc@275 2736 string "}",
adamc@275 2737 newline,
adamc@275 2738 newline,
adamc@275 2739 string "PQclear(res);",
adamc@275 2740 newline]
adamc@275 2741 end) tables,
adamc@377 2742
adamc@377 2743 p_list_sep newline
adamc@377 2744 (fn s =>
adamc@377 2745 let
adamc@377 2746 val sl = CharVector.map Char.toLower s
adamc@377 2747
adamc@377 2748 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2749 ^ sl ^ "' AND relkind = 'S'"
adamc@377 2750 in
adamc@377 2751 box [string "res = PQexec(conn, \"",
adamc@377 2752 string q,
adamc@377 2753 string "\");",
adamc@377 2754 newline,
adamc@377 2755 newline,
adamc@377 2756 string "if (res == NULL) {",
adamc@377 2757 newline,
adamc@377 2758 box [string "PQfinish(conn);",
adamc@377 2759 newline,
adamc@377 2760 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@377 2761 newline],
adamc@377 2762 string "}",
adamc@377 2763 newline,
adamc@377 2764 newline,
adamc@377 2765 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@377 2766 newline,
adamc@377 2767 box [string "char msg[1024];",
adamc@377 2768 newline,
adamc@377 2769 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@377 2770 newline,
adamc@377 2771 string "msg[1023] = 0;",
adamc@377 2772 newline,
adamc@377 2773 string "PQclear(res);",
adamc@377 2774 newline,
adamc@377 2775 string "PQfinish(conn);",
adamc@377 2776 newline,
adamc@377 2777 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@377 2778 string q,
adamc@377 2779 string "\\n%s\", msg);",
adamc@377 2780 newline],
adamc@377 2781 string "}",
adamc@377 2782 newline,
adamc@377 2783 newline,
adamc@377 2784 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@377 2785 newline,
adamc@377 2786 box [string "PQclear(res);",
adamc@377 2787 newline,
adamc@377 2788 string "PQfinish(conn);",
adamc@377 2789 newline,
adamc@377 2790 string "uw_error(ctx, FATAL, \"Sequence '",
adamc@377 2791 string s,
adamc@377 2792 string "' does not exist.\");",
adamc@377 2793 newline],
adamc@377 2794 string "}",
adamc@377 2795 newline,
adamc@377 2796 newline,
adamc@377 2797 string "PQclear(res);",
adamc@377 2798 newline]
adamc@377 2799 end) sequences,
adamc@377 2800
adamc@275 2801 string "}"]
adamc@432 2802
adamc@432 2803 val hasDb = List.exists (fn (DDatabase _, _) => true | _ => false) ds
adamc@734 2804
adamc@734 2805 val cookies = List.mapPartial (fn (DCookie s, _) => SOME s | _ => NONE) ds
adamc@734 2806
adamc@734 2807 val cookieCode = foldl (fn (cookie, acc) =>
adamc@734 2808 SOME (case acc of
adamc@734 2809 NONE => string ("uw_unnull(uw_Basis_get_cookie(ctx, \""
adamc@734 2810 ^ cookie ^ "\"))")
adamc@734 2811 | SOME acc => box [string ("uw_Basis_strcat(ctx, uw_unnull(uw_Basis_get_cookie(ctx, \""
adamc@734 2812 ^ cookie ^ "\")), uw_Basis_strcat(ctx, \"/\", "),
adamc@734 2813 acc,
adamc@734 2814 string "))"]))
adamc@734 2815 NONE cookies
adamc@29 2816 in
adamc@144 2817 box [string "#include <stdio.h>",
adamc@144 2818 newline,
adamc@144 2819 string "#include <stdlib.h>",
adamc@144 2820 newline,
adamc@272 2821 string "#include <string.h>",
adamc@272 2822 newline,
adamc@390 2823 string "#include <math.h>",
adamc@390 2824 newline,
adamc@432 2825 if hasDb then
adamc@432 2826 box [string "#include <postgresql/libpq-fe.h>",
adamc@432 2827 newline]
adamc@432 2828 else
adamc@432 2829 box [],
adamc@144 2830 newline,
adamc@378 2831 string "#include \"",
adamc@378 2832 string (OS.Path.joinDirFile {dir = Config.includ,
adamc@378 2833 file = "urweb.h"}),
adamc@378 2834 string "\"",
adamc@101 2835 newline,
adamc@101 2836 newline,
adamc@101 2837 p_list_sep newline (fn x => x) pds,
adamc@101 2838 newline,
adamc@311 2839 string "int uw_inputs_len = ",
adamc@144 2840 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
adamc@144 2841 string ";",
adamc@144 2842 newline,
adamc@673 2843 string "int uw_timeout = ",
adamc@673 2844 string (Int.toString (!timeout)),
adamc@673 2845 string ";",
adamc@673 2846 newline,
adamc@144 2847 newline,
adamc@311 2848 string "int uw_input_num(char *name) {",
adamc@144 2849 newline,
adamc@144 2850 makeSwitch (fnums, 0),
adamc@144 2851 string "}",
adamc@144 2852 newline,
adamc@144 2853 newline,
adamc@734 2854
adamc@734 2855 string "extern void uw_sign(const char *in, char *out);",
adamc@734 2856 newline,
adamc@734 2857 string "extern int uw_hash_blocksize;",
adamc@734 2858 newline,
adamc@734 2859 string "uw_Basis_string uw_cookie_sig(uw_context ctx) {",
adamc@734 2860 newline,
adamc@734 2861 box [string "uw_Basis_string r = uw_malloc(ctx, uw_hash_blocksize);",
adamc@734 2862 newline,
adamc@734 2863 string "uw_sign(",
adamc@734 2864 case cookieCode of
adamc@734 2865 NONE => string "\"\""
adamc@734 2866 | SOME code => code,
adamc@734 2867 string ", r);",
adamc@734 2868 newline,
adamc@734 2869 string "return uw_Basis_makeSigString(ctx, r);",
adamc@734 2870 newline],
adamc@734 2871 string "}",
adamc@734 2872 newline,
adamc@734 2873 newline,
adamc@734 2874
adamc@311 2875 string "void uw_handle(uw_context ctx, char *request) {",
adamc@101 2876 newline,
adamc@569 2877 string "if (!strcmp(request, \"/app.js\")) {",
adamc@569 2878 newline,
adamc@569 2879 box [string "uw_write_header(ctx, \"Content-type: text/javascript\\r\\n\");",
adamc@569 2880 newline,
adamc@569 2881 string "uw_write(ctx, jslib);",
adamc@569 2882 newline,
adamc@569 2883 string "return;",
adamc@569 2884 newline],
adamc@569 2885 string "}",
adamc@569 2886 newline,
adamc@101 2887 p_list_sep newline (fn x => x) pds',
adamc@101 2888 newline,
adamc@387 2889 string "uw_error(ctx, FATAL, \"Unknown page\");",
adamc@387 2890 newline,
adamc@101 2891 string "}",
adamc@275 2892 newline,
adamc@275 2893 newline,
adamc@432 2894 if hasDb then
adamc@432 2895 validate
adamc@432 2896 else
adamc@432 2897 box [],
adamc@376 2898 newline,
adamc@376 2899 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
adamc@376 2900 box []
adamc@376 2901 else
adamc@376 2902 box [newline,
adamc@376 2903 string "void uw_db_init(uw_context ctx) { };",
adamc@424 2904 newline,
adamc@424 2905 string "int uw_db_begin(uw_context ctx) { return 0; };",
adamc@424 2906 newline,
adamc@424 2907 string "int uw_db_commit(uw_context ctx) { return 0; };",
adamc@424 2908 newline,
adamc@424 2909 string "int uw_db_rollback(uw_context ctx) { return 0; };",
adamc@684 2910 newline,
adamc@684 2911 string "void uw_expunger(uw_context ctx, uw_Basis_client cli) { };",
adamc@687 2912 newline,
adamc@687 2913 string "void uw_initializer(uw_context ctx) { };",
adamc@376 2914 newline]]
adamc@29 2915 end
adamc@29 2916
adamc@274 2917 fun p_sql env (ds, _) =
adamc@274 2918 let
adamc@274 2919 val (pps, _) = ListUtil.foldlMap
adamc@274 2920 (fn (dAll as (d, _), env) =>
adamc@274 2921 let
adamc@274 2922 val pp = case d of
adamc@707 2923 DTable (s, xts, pk, csts) =>
adamc@274 2924 box [string "CREATE TABLE ",
adamc@274 2925 string s,
adamc@274 2926 string "(",
adamc@274 2927 p_list (fn (x, t) =>
adamc@311 2928 box [string "uw_",
adamc@275 2929 string (CharVector.map Char.toLower x),
adamc@274 2930 space,
adamc@467 2931 p_sqltype env (t, ErrorMsg.dummySpan)]) xts,
adamc@707 2932 case (pk, csts) of
adamc@707 2933 ("", []) => box []
adamc@707 2934 | _ => string ",",
adamc@704 2935 cut,
adamc@707 2936 case pk of
adamc@707 2937 "" => box []
adamc@707 2938 | _ => box [string "PRIMARY",
adamc@707 2939 space,
adamc@707 2940 string "KEY",
adamc@707 2941 space,
adamc@707 2942 string "(",
adamc@707 2943 string pk,
adamc@707 2944 string ")",
adamc@707 2945 case csts of
adamc@707 2946 [] => box []
adamc@707 2947 | _ => string ",",
adamc@707 2948 newline],
adamc@704 2949 p_list_sep (box [string ",", newline])
adamc@704 2950 (fn (x, c) =>
adamc@704 2951 box [string "CONSTRAINT",
adamc@704 2952 space,
adamc@704 2953 string s,
adamc@704 2954 string "_",
adamc@704 2955 string x,
adamc@704 2956 space,
adamc@704 2957 string c]) csts,
adamc@704 2958 newline,
adamc@274 2959 string ");",
adamc@274 2960 newline,
adamc@274 2961 newline]
adamc@338 2962 | DSequence s =>
adamc@338 2963 box [string "CREATE SEQUENCE ",
adamc@338 2964 string s,
adamc@338 2965 string ";",
adamc@338 2966 newline,
adamc@338 2967 newline]
adamc@274 2968 | _ => box []
adamc@274 2969 in
adamc@274 2970 (pp, E.declBinds env dAll)
adamc@274 2971 end)
adamc@274 2972 env ds
adamc@274 2973 in
adamc@274 2974 box pps
adamc@274 2975 end
adamc@274 2976
adamc@29 2977 end