annotate src/cjr_print.sml @ 756:8ce31c052dce

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