annotate src/cjr_print.sml @ 1017:34ba25d6af3b

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