annotate src/cjr_print.sml @ 867:e7f80d78075b

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