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