annotate src/cjr_print.sml @ 913:b26823138bf8

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