Debian Codegraph @

src/interfaces/ecpg/preproc/ecpg.header

raw
1 /* src/interfaces/ecpg/preproc/ecpg.header */
2
3 /* Copyright comment */
4 %{
5 #include "postgres_fe.h"
6
7 #include "preproc_extern.h"
8 #include "preproc.h"
9 #include "ecpg_config.h"
10 #include <unistd.h>
11
12 /* silence -Wmissing-variable-declarations */
13 extern int base_yychar;
14 extern int base_yynerrs;
15
16
17 /*
18 * The %name-prefix option below will make bison call base_yylex, but we
19 * really want it to call filtered_base_yylex (see parser.c).
20 */
21 #define base_yylex filtered_base_yylex
22
23 /*
24 * This is only here so the string gets into the POT. Bison uses it
25 * internally.
26 */
27 #define bison_gettext_dummy gettext_noop("syntax error")
28
29 /*
30 * Variables containing simple states.
31 */
32 int struct_level = 0;
33 int braces_open; /* brace level counter */
34 char *current_function;
35 int ecpg_internal_var = 0;
36 char *connection = NULL;
37 char *input_filename = NULL;
38
39 static int FoundInto = 0;
40 static int initializer = 0;
41 static int pacounter = 1;
42 static struct this_type actual_type[STRUCT_DEPTH];
43 static char *actual_startline[STRUCT_DEPTH];
44 static int varchar_counter = 1;
45 static int bytea_counter = 1;
46
47 /*
48 * We temporarily store struct members here while parsing struct declarations.
49 * The struct_member_list (at a given nesting depth) is constructed while
50 * scanning the fields within "struct { .... }", but we can't remove it upon
51 * seeing the right brace. It's kept around and copied into the variables
52 * or typedefs that follow, in order to handle cases like
53 * "struct foo { ... } foovar1, foovar2;". We recycle the storage only
54 * upon closing the current nesting level or starting the next struct
55 * declaration within the same nesting level.
56 * For cases like "struct foo foovar1, foovar2;", we copy the saved struct
57 * field list for the typedef or struct tag into the struct_member_list
58 * global variable, and then copy it again to the newly-declared variables.
59 */
60 struct ECPGstruct_member *struct_member_list[STRUCT_DEPTH] = {NULL};
61
62 /* also store struct type so we can do a sizeof() later */
63 static char *ECPGstruct_sizeof = NULL;
64
65 /* for forward declarations we have to store some data as well */
66 static char *forward_name = NULL;
67
68 struct ECPGtype ecpg_no_indicator = {ECPGt_NO_INDICATOR, NULL, NULL, NULL, {NULL}, 0};
69 struct variable no_indicator = {"no_indicator", &ecpg_no_indicator, 0, NULL};
70
71 static struct ECPGtype ecpg_query = {ECPGt_char_variable, NULL, NULL, NULL, {NULL}, 0};
72
73 static bool check_declared_list(const char *name);
74 static void update_connection(const char *newconn);
75
76
77 /*
78 * "Location tracking" support. We commandeer Bison's location tracking
79 * mechanism to manage the output string for productions that ordinarily would
80 * return a <str> result. This allows the majority of those productions to
81 * have default semantic actions, reducing the size of the parser, and also
82 * greatly reducing its compilation time on some versions of clang.
83 *
84 * To do this, we make YYLTYPE be a pointer to a malloc'd string, and then
85 * merge the location strings of the input tokens in the default YYLLOC
86 * computation. Productions that are okay with the standard merge need not
87 * do anything more; otherwise, they can override it by assigning to @$.
88 */
89 #define YYLLOC_DEFAULT(Current, Rhs, N) yylloc_default(&(Current), Rhs, N)
90
91 static void
92 yylloc_default(YYLTYPE *target, YYLTYPE *rhs, int N)
93 {
94 if (N > 1)
95 {
96 /* Concatenate non-empty inputs with one space between them */
97 char *result,
98 *ptr;
99 size_t needed = 0;
100
101 for (int i = 1; i <= N; i++)
102 {
103 size_t thislen = strlen(rhs[i]);
104
105 if (needed > 0 && thislen > 0)
106 needed++;
107 needed += thislen;
108 }
109 result = (char *) loc_alloc(needed + 1);
110 ptr = result;
111 for (int i = 1; i <= N; i++)
112 {
113 size_t thislen = strlen(rhs[i]);
114
115 if (ptr > result && thislen > 0)
116 *ptr++ = ' ';
117 memcpy(ptr, rhs[i], thislen);
118 ptr += thislen;
119 }
120 *ptr = '\0';
121 *target = result;
122 }
123 else if (N == 1)
124 {
125 /* Just re-use the single input */
126 *target = rhs[1];
127 }
128 else
129 {
130 /* No need to allocate any space */
131 *target = "";
132 }
133 }
134
135 /* and the rest */
136 static char *
137 create_questionmarks(const char *name, bool array)
138 {
139 struct variable *p = find_variable(name);
140 int count;
141 char *result = "";
142
143 /*
144 * In case we have a struct, we have to print as many "?" as there are
145 * attributes in the struct
146 *
147 * An array is only allowed together with an element argument
148 *
149 * This is essentially only used for inserts, but using a struct as input
150 * parameter is an error anywhere else so we don't have to worry here.
151 */
152
153 if (p->type->type == ECPGt_struct || (array && p->type->type == ECPGt_array && p->type->u.element->type == ECPGt_struct))
154 {
155 struct ECPGstruct_member *m;
156
157 if (p->type->type == ECPGt_struct)
158 m = p->type->u.members;
159 else
160 m = p->type->u.element->u.members;
161
162 for (count = 0; m != NULL; m = m->next, count++);
163 }
164 else
165 count = 1;
166
167 for (; count > 0; count--)
168 {
169 char buf[32];
170
171 snprintf(buf, sizeof(buf), "$%d", pacounter++);
172 result = cat_str(3, result, buf, " , ");
173 }
174
175 /* remove the trailing " ," */
176 result[strlen(result) - 3] = '\0';
177 return result;
178 }
179
180 static char *
181 adjust_outofscope_cursor_vars(struct cursor *cur)
182 {
183 /*
184 * Informix accepts DECLARE with variables that are out of scope when OPEN
185 * is called. For instance you can DECLARE a cursor in one function, and
186 * OPEN/FETCH/CLOSE it in another functions. This is very useful for e.g.
187 * event-driver programming, but may also lead to dangerous programming.
188 * The limitation when this is allowed and doesn't cause problems have to
189 * be documented, like the allocated variables must not be realloc()'ed.
190 *
191 * We have to change the variables to our own struct and just store the
192 * pointer instead of the variable. Do it only for local variables, not
193 * for globals.
194 */
195 char *result = "";
196 int insert;
197
198 for (insert = 1; insert >= 0; insert--)
199 {
200 struct arguments *list;
201 struct arguments *ptr;
202 struct arguments *newlist = NULL;
203 struct variable *newvar,
204 *newind;
205
206 list = (insert ? cur->argsinsert : cur->argsresult);
207
208 for (ptr = list; ptr != NULL; ptr = ptr->next)
209 {
210 char var_text[20];
211 char *original_var;
212 bool skip_set_var = false;
213 bool var_ptr = false;
214
215 /* change variable name to "ECPGget_var(<counter>)" */
216 original_var = ptr->variable->name;
217 snprintf(var_text, sizeof(var_text), "%d))", ecpg_internal_var);
218
219 /* Don't emit ECPGset_var() calls for global variables */
220 if (ptr->variable->brace_level == 0)
221 {
222 newvar = ptr->variable;
223 skip_set_var = true;
224 }
225 else if ((ptr->variable->type->type == ECPGt_char_variable)
226 && (strncmp(ptr->variable->name, "ECPGprepared_statement", strlen("ECPGprepared_statement")) == 0))
227 {
228 newvar = ptr->variable;
229 skip_set_var = true;
230 }
231 else if ((ptr->variable->type->type != ECPGt_varchar
232 && ptr->variable->type->type != ECPGt_char
233 && ptr->variable->type->type != ECPGt_unsigned_char
234 && ptr->variable->type->type != ECPGt_string
235 && ptr->variable->type->type != ECPGt_bytea)
236 && atoi(ptr->variable->type->size) > 1)
237 {
238 newvar = new_variable(cat_str(4, "(",
239 ecpg_type_name(ptr->variable->type->u.element->type),
240 " *)(ECPGget_var(",
241 var_text),
242 ECPGmake_array_type(ECPGmake_simple_type(ptr->variable->type->u.element->type,
243 "1",
244 ptr->variable->type->u.element->counter),
245 ptr->variable->type->size),
246 0);
247 }
248 else if ((ptr->variable->type->type == ECPGt_varchar
249 || ptr->variable->type->type == ECPGt_char
250 || ptr->variable->type->type == ECPGt_unsigned_char
251 || ptr->variable->type->type == ECPGt_string
252 || ptr->variable->type->type == ECPGt_bytea)
253 && atoi(ptr->variable->type->size) > 1)
254 {
255 newvar = new_variable(cat_str(4, "(",
256 ecpg_type_name(ptr->variable->type->type),
257 " *)(ECPGget_var(",
258 var_text),
259 ECPGmake_simple_type(ptr->variable->type->type,
260 ptr->variable->type->size,
261 ptr->variable->type->counter),
262 0);
263 if (ptr->variable->type->type == ECPGt_varchar ||
264 ptr->variable->type->type == ECPGt_bytea)
265 var_ptr = true;
266 }
267 else if (ptr->variable->type->type == ECPGt_struct
268 || ptr->variable->type->type == ECPGt_union)
269 {
270 newvar = new_variable(cat_str(5, "(*(",
271 ptr->variable->type->type_name,
272 " *)(ECPGget_var(",
273 var_text,
274 ")"),
275 ECPGmake_struct_type(ptr->variable->type->u.members,
276 ptr->variable->type->type,
277 ptr->variable->type->type_name,
278 ptr->variable->type->struct_sizeof),
279 0);
280 var_ptr = true;
281 }
282 else if (ptr->variable->type->type == ECPGt_array)
283 {
284 if (ptr->variable->type->u.element->type == ECPGt_struct
285 || ptr->variable->type->u.element->type == ECPGt_union)
286 {
287 newvar = new_variable(cat_str(5, "(*(",
288 ptr->variable->type->u.element->type_name,
289 " *)(ECPGget_var(",
290 var_text,
291 ")"),
292 ECPGmake_struct_type(ptr->variable->type->u.element->u.members,
293 ptr->variable->type->u.element->type,
294 ptr->variable->type->u.element->type_name,
295 ptr->variable->type->u.element->struct_sizeof),
296 0);
297 }
298 else
299 {
300 newvar = new_variable(cat_str(4, "(",
301 ecpg_type_name(ptr->variable->type->u.element->type),
302 " *)(ECPGget_var(",
303 var_text),
304 ECPGmake_array_type(ECPGmake_simple_type(ptr->variable->type->u.element->type,
305 ptr->variable->type->u.element->size,
306 ptr->variable->type->u.element->counter),
307 ptr->variable->type->size),
308 0);
309 var_ptr = true;
310 }
311 }
312 else
313 {
314 newvar = new_variable(cat_str(4, "*(",
315 ecpg_type_name(ptr->variable->type->type),
316 " *)(ECPGget_var(",
317 var_text),
318 ECPGmake_simple_type(ptr->variable->type->type,
319 ptr->variable->type->size,
320 ptr->variable->type->counter),
321 0);
322 var_ptr = true;
323 }
324
325 /*
326 * create call to "ECPGset_var(<counter>, <connection>, <pointer>.
327 * <line number>)"
328 */
329 if (!skip_set_var)
330 {
331 snprintf(var_text, sizeof(var_text), "%d, %s",
332 ecpg_internal_var++, var_ptr ? "&(" : "(");
333 result = cat_str(5, result, "ECPGset_var(",
334 var_text, original_var,
335 "), __LINE__);\n");
336 }
337
338 /*
339 * now the indicator if there is one and it's not a global
340 * variable
341 */
342 if ((ptr->indicator->type->type == ECPGt_NO_INDICATOR) || (ptr->indicator->brace_level == 0))
343 {
344 newind = ptr->indicator;
345 }
346 else
347 {
348 /* change variable name to "ECPGget_var(<counter>)" */
349 original_var = ptr->indicator->name;
350 snprintf(var_text, sizeof(var_text), "%d))", ecpg_internal_var);
351 var_ptr = false;
352
353 if (ptr->indicator->type->type == ECPGt_struct
354 || ptr->indicator->type->type == ECPGt_union)
355 {
356 newind = new_variable(cat_str(5, "(*(",
357 ptr->indicator->type->type_name,
358 " *)(ECPGget_var(",
359 var_text,
360 ")"),
361 ECPGmake_struct_type(ptr->indicator->type->u.members,
362 ptr->indicator->type->type,
363 ptr->indicator->type->type_name,
364 ptr->indicator->type->struct_sizeof),
365 0);
366 var_ptr = true;
367 }
368 else if (ptr->indicator->type->type == ECPGt_array)
369 {
370 if (ptr->indicator->type->u.element->type == ECPGt_struct
371 || ptr->indicator->type->u.element->type == ECPGt_union)
372 {
373 newind = new_variable(cat_str(5, "(*(",
374 ptr->indicator->type->u.element->type_name,
375 " *)(ECPGget_var(",
376 var_text,
377 ")"),
378 ECPGmake_struct_type(ptr->indicator->type->u.element->u.members,
379 ptr->indicator->type->u.element->type,
380 ptr->indicator->type->u.element->type_name,
381 ptr->indicator->type->u.element->struct_sizeof),
382 0);
383 }
384 else
385 {
386 newind = new_variable(cat_str(4, "(",
387 ecpg_type_name(ptr->indicator->type->u.element->type),
388 " *)(ECPGget_var(",
389 var_text),
390 ECPGmake_array_type(ECPGmake_simple_type(ptr->indicator->type->u.element->type,
391 ptr->indicator->type->u.element->size,
392 ptr->indicator->type->u.element->counter),
393 ptr->indicator->type->size),
394 0);
395 var_ptr = true;
396 }
397 }
398 else if (atoi(ptr->indicator->type->size) > 1)
399 {
400 newind = new_variable(cat_str(4, "(",
401 ecpg_type_name(ptr->indicator->type->type),
402 " *)(ECPGget_var(",
403 var_text),
404 ECPGmake_simple_type(ptr->indicator->type->type,
405 ptr->indicator->type->size,
406 ptr->variable->type->counter),
407 0);
408 }
409 else
410 {
411 newind = new_variable(cat_str(4, "*(",
412 ecpg_type_name(ptr->indicator->type->type),
413 " *)(ECPGget_var(",
414 var_text),
415 ECPGmake_simple_type(ptr->indicator->type->type,
416 ptr->indicator->type->size,
417 ptr->variable->type->counter),
418 0);
419 var_ptr = true;
420 }
421
422 /*
423 * create call to "ECPGset_var(<counter>, <pointer>. <line
424 * number>)"
425 */
426 snprintf(var_text, sizeof(var_text), "%d, %s",
427 ecpg_internal_var++, var_ptr ? "&(" : "(");
428 result = cat_str(5, result, "ECPGset_var(",
429 var_text, original_var,
430 "), __LINE__);\n");
431 }
432
433 add_variable_to_tail(&newlist, newvar, newind);
434 }
435
436 if (insert)
437 cur->argsinsert_oos = newlist;
438 else
439 cur->argsresult_oos = newlist;
440 }
441
442 return result;
443 }
444
445 /* This tests whether the cursor was declared and opened in the same function. */
446 #define SAMEFUNC(cur) \
447 ((cur->function == NULL) || \
448 (cur->function != NULL && current_function != NULL && \
449 strcmp(cur->function, current_function) == 0))
450
451 static struct cursor *
452 add_additional_variables(const char *name, bool insert)
453 {
454 struct cursor *ptr;
455 struct arguments *p;
456 int (*strcmp_fn) (const char *, const char *) = ((name[0] == ':' || name[0] == '"') ? strcmp : pg_strcasecmp);
457
458 for (ptr = cur; ptr != NULL; ptr = ptr->next)
459 {
460 if (strcmp_fn(ptr->name, name) == 0)
461 break;
462 }
463
464 if (ptr == NULL)
465 {
466 mmerror(PARSE_ERROR, ET_ERROR, "cursor \"%s\" does not exist", name);
467 return NULL;
468 }
469
470 if (insert)
471 {
472 /*
473 * add all those input variables that were given earlier
474 *
475 * note that we have to append here but have to keep the existing
476 * order
477 */
478 for (p = (SAMEFUNC(ptr) ? ptr->argsinsert : ptr->argsinsert_oos); p; p = p->next)
479 add_variable_to_tail(&argsinsert, p->variable, p->indicator);
480 }
481
482 /* add all those output variables that were given earlier */
483 for (p = (SAMEFUNC(ptr) ? ptr->argsresult : ptr->argsresult_oos); p; p = p->next)
484 add_variable_to_tail(&argsresult, p->variable, p->indicator);
485
486 return ptr;
487 }
488
489 static void
490 add_typedef(const char *name, const char *dimension, const char *length,
491 enum ECPGttype type_enum,
492 const char *type_dimension, const char *type_index,
493 int initializer, int array)
494 {
495 /* add entry to list */
496 struct typedefs *ptr,
497 *this;
498
499 if ((type_enum == ECPGt_struct ||
500 type_enum == ECPGt_union) &&
501 initializer == 1)
502 mmerror(PARSE_ERROR, ET_ERROR, "initializer not allowed in type definition");
503 else if (INFORMIX_MODE && strcmp(name, "string") == 0)
504 mmerror(PARSE_ERROR, ET_ERROR, "type name \"string\" is reserved in Informix mode");
505 else
506 {
507 for (ptr = types; ptr != NULL; ptr = ptr->next)
508 {
509 if (strcmp(name, ptr->name) == 0)
510 /* re-definition is a bug */
511 mmerror(PARSE_ERROR, ET_ERROR, "type \"%s\" is already defined", name);
512 }
513 adjust_array(type_enum, &dimension, &length,
514 type_dimension, type_index, array, true);
515
516 this = (struct typedefs *) mm_alloc(sizeof(struct typedefs));
517
518 /* initial definition */
519 this->next = types;
520 this->name = mm_strdup(name);
521 this->brace_level = braces_open;
522 this->type = (struct this_type *) mm_alloc(sizeof(struct this_type));
523 this->type->type_storage = NULL;
524 this->type->type_enum = type_enum;
525 this->type->type_str = mm_strdup(name);
526 this->type->type_dimension = mm_strdup(dimension); /* dimension of array */
527 this->type->type_index = mm_strdup(length); /* length of string */
528 this->type->type_sizeof = ECPGstruct_sizeof ? mm_strdup(ECPGstruct_sizeof) : NULL;
529 this->struct_member_list = (type_enum == ECPGt_struct || type_enum == ECPGt_union) ?
530 ECPGstruct_member_dup(struct_member_list[struct_level]) : NULL;
531
532 if (type_enum != ECPGt_varchar &&
533 type_enum != ECPGt_bytea &&
534 type_enum != ECPGt_char &&
535 type_enum != ECPGt_unsigned_char &&
536 type_enum != ECPGt_string &&
537 atoi(this->type->type_index) >= 0)
538 mmerror(PARSE_ERROR, ET_ERROR, "multidimensional arrays for simple data types are not supported");
539
540 types = this;
541 }
542 }
543
544 /*
545 * check an SQL identifier is declared or not.
546 * If it is already declared, the global variable
547 * connection will be changed to the related connection.
548 */
549 static bool
550 check_declared_list(const char *name)
551 {
552 struct declared_list *ptr = NULL;
553
554 for (ptr = g_declared_list; ptr != NULL; ptr = ptr->next)
555 {
556 if (!ptr->connection)
557 continue;
558 if (strcmp(name, ptr->name) == 0)
559 {
560 if (connection && strcmp(ptr->connection, connection) != 0)
561 mmerror(PARSE_ERROR, ET_WARNING, "connection %s is overwritten with %s by DECLARE statement %s", connection, ptr->connection, name);
562 update_connection(ptr->connection);
563 return true;
564 }
565 }
566 return false;
567 }
568
569 /*
570 * If newconn isn't NULL, update the global "connection" variable to that;
571 * otherwise do nothing.
572 */
573 static void
574 update_connection(const char *newconn)
575 {
576 if (newconn)
577 {
578 free(connection);
579 connection = mm_strdup(newconn);
580 }
581 }
582 %}
583
584 %expect 0
585 %name-prefix="base_yy"
586 %locations
587
588 %union {
589 double dval;
590 char *str;
591 int ival;
592 struct when action;
593 struct index index;
594 int tagname;
595 struct this_type type;
596 enum ECPGttype type_enum;
597 enum ECPGdtype dtype_enum;
598 struct fetch_desc descriptor;
599 struct su_symbol struct_union;
600 struct prep prep;
601 struct exec exec;
602 struct describe describe;
603 }