annotate src/cjr_print.sml @ 483:a0f47540d8ad

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