annotate src/cjr_print.sml @ 757:fa2019a63ea4

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