annotate src/cjr_print.sml @ 1617:8611da277df4

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