PostgreSQL Source Code  git master
variable.c
Go to the documentation of this file.
1 /* src/interfaces/ecpg/preproc/variable.c */
2 
3 #include "postgres_fe.h"
4 
5 #include "preproc_extern.h"
6 
7 static struct variable *allvariables = NULL;
8 
9 struct variable *
10 new_variable(const char *name, struct ECPGtype *type, int brace_level)
11 {
12  struct variable *p = (struct variable *) mm_alloc(sizeof(struct variable));
13 
14  p->name = mm_strdup(name);
15  p->type = type;
17 
18  p->next = allvariables;
19  allvariables = p;
20 
21  return p;
22 }
23 
24 static struct variable *
25 find_struct_member(char *name, char *str, struct ECPGstruct_member *members, int brace_level)
26 {
27  char *next = strpbrk(++str, ".-["),
28  *end,
29  c = '\0';
30 
31  if (next != NULL)
32  {
33  c = *next;
34  *next = '\0';
35  }
36 
37  for (; members; members = members->next)
38  {
39  if (strcmp(members->name, str) == 0)
40  {
41  if (next == NULL)
42  {
43  /* found the end */
44  switch (members->type->type)
45  {
46  case ECPGt_array:
48  case ECPGt_struct:
49  case ECPGt_union:
50  return new_variable(name, ECPGmake_struct_type(members->type->u.members, members->type->type, members->type->type_name, members->type->struct_sizeof), brace_level);
51  default:
52  return new_variable(name, ECPGmake_simple_type(members->type->type, members->type->size, members->type->counter), brace_level);
53  }
54  }
55  else
56  {
57  *next = c;
58  if (c == '[')
59  {
60  int count;
61 
62  /*
63  * We don't care about what's inside the array braces so
64  * just eat up the character
65  */
66  for (count = 1, end = next + 1; count; end++)
67  {
68  switch (*end)
69  {
70  case '[':
71  count++;
72  break;
73  case ']':
74  count--;
75  break;
76  default:
77  break;
78  }
79  }
80  }
81  else
82  end = next;
83 
84  switch (*end)
85  {
86  case '\0': /* found the end, but this time it has to be
87  * an array element */
88  if (members->type->type != ECPGt_array)
89  mmfatal(PARSE_ERROR, "incorrectly formed variable \"%s\"", name);
90 
91  switch (members->type->u.element->type)
92  {
93  case ECPGt_array:
95  case ECPGt_struct:
96  case ECPGt_union:
98  default:
99  return new_variable(name, ECPGmake_simple_type(members->type->u.element->type, members->type->u.element->size, members->type->u.element->counter), brace_level);
100  }
101  break;
102  case '-':
103  if (members->type->type == ECPGt_array)
104  return find_struct_member(name, ++end, members->type->u.element->u.members, brace_level);
105  else
106  return find_struct_member(name, ++end, members->type->u.members, brace_level);
107  break;
108  case '.':
109  if (members->type->type == ECPGt_array)
110  return find_struct_member(name, end, members->type->u.element->u.members, brace_level);
111  else
112  return find_struct_member(name, end, members->type->u.members, brace_level);
113  break;
114  default:
115  mmfatal(PARSE_ERROR, "incorrectly formed variable \"%s\"", name);
116  break;
117  }
118  }
119  }
120  }
121 
122  return NULL;
123 }
124 
125 static struct variable *
126 find_struct(char *name, char *next, char *end)
127 {
128  struct variable *p;
129  char c = *next;
130 
131  /* first get the mother structure entry */
132  *next = '\0';
133  p = find_variable(name);
134 
135  if (c == '-')
136  {
137  if (p->type->type != ECPGt_array)
138  mmfatal(PARSE_ERROR, "variable \"%s\" is not a pointer", name);
139 
140  if (p->type->u.element->type != ECPGt_struct && p->type->u.element->type != ECPGt_union)
141  mmfatal(PARSE_ERROR, "variable \"%s\" is not a pointer to a structure or a union", name);
142 
143  /* restore the name, we will need it later */
144  *next = c;
145 
146  return find_struct_member(name, ++end, p->type->u.element->u.members, p->brace_level);
147  }
148  else
149  {
150  if (next == end)
151  {
152  if (p->type->type != ECPGt_struct && p->type->type != ECPGt_union)
153  mmfatal(PARSE_ERROR, "variable \"%s\" is neither a structure nor a union", name);
154 
155  /* restore the name, we will need it later */
156  *next = c;
157 
158  return find_struct_member(name, end, p->type->u.members, p->brace_level);
159  }
160  else
161  {
162  if (p->type->type != ECPGt_array)
163  mmfatal(PARSE_ERROR, "variable \"%s\" is not an array", name);
164 
165  if (p->type->u.element->type != ECPGt_struct && p->type->u.element->type != ECPGt_union)
166  mmfatal(PARSE_ERROR, "variable \"%s\" is not a pointer to a structure or a union", name);
167 
168  /* restore the name, we will need it later */
169  *next = c;
170 
171  return find_struct_member(name, end, p->type->u.element->u.members, p->brace_level);
172  }
173  }
174 }
175 
176 static struct variable *
178 {
179  struct variable *p;
180 
181  for (p = allvariables; p; p = p->next)
182  {
183  if (strcmp(p->name, name) == 0)
184  return p;
185  }
186 
187  return NULL;
188 }
189 
190 /* Note that this function will end the program in case of an unknown */
191 /* variable */
192 struct variable *
194 {
195  char *next,
196  *end;
197  struct variable *p;
198  int count;
199 
200  next = strpbrk(name, ".[-");
201  if (next)
202  {
203  if (*next == '[')
204  {
205  /*
206  * We don't care about what's inside the array braces so just eat
207  * up the characters
208  */
209  for (count = 1, end = next + 1; count; end++)
210  {
211  switch (*end)
212  {
213  case '[':
214  count++;
215  break;
216  case ']':
217  count--;
218  break;
219  default:
220  break;
221  }
222  }
223  if (*end == '.')
224  p = find_struct(name, next, end);
225  else
226  {
227  char c = *next;
228 
229  *next = '\0';
230  p = find_simple(name);
231  if (p == NULL)
232  mmfatal(PARSE_ERROR, "variable \"%s\" is not declared", name);
233 
234  *next = c;
235  switch (p->type->u.element->type)
236  {
237  case ECPGt_array:
238  return new_variable(name, ECPGmake_array_type(ECPGmake_simple_type(p->type->u.element->u.element->type, p->type->u.element->u.element->size, p->type->u.element->u.element->counter), p->type->u.element->size), p->brace_level);
239  case ECPGt_struct:
240  case ECPGt_union:
241  return new_variable(name, ECPGmake_struct_type(p->type->u.element->u.members, p->type->u.element->type, p->type->u.element->type_name, p->type->u.element->struct_sizeof), p->brace_level);
242  default:
243  return new_variable(name, ECPGmake_simple_type(p->type->u.element->type, p->type->u.element->size, p->type->u.element->counter), p->brace_level);
244  }
245  }
246  }
247  else
248  p = find_struct(name, next, next);
249  }
250  else
251  p = find_simple(name);
252 
253  if (p == NULL)
254  mmfatal(PARSE_ERROR, "variable \"%s\" is not declared", name);
255 
256  return p;
257 }
258 
259 void
261 {
262  struct typedefs *p,
263  *prev;
264 
265  for (p = prev = types; p;)
266  {
267  if (p->brace_level >= brace_level)
268  {
269  /* remove it */
270  if (p == types)
271  prev = types = p->next;
272  else
273  prev->next = p->next;
274 
275  if (p->type->type_enum == ECPGt_struct || p->type->type_enum == ECPGt_union)
277  free(p->type);
278  free(p->name);
279  free(p);
280  if (prev == types)
281  p = types;
282  else
283  p = prev ? prev->next : NULL;
284  }
285  else
286  {
287  prev = p;
288  p = prev->next;
289  }
290  }
291 }
292 
293 void
295 {
296  struct variable *p,
297  *prev;
298 
299  for (p = prev = allvariables; p;)
300  {
301  if (p->brace_level >= brace_level)
302  {
303  /* is it still referenced by a cursor? */
304  struct cursor *ptr;
305 
306  for (ptr = cur; ptr != NULL; ptr = ptr->next)
307  {
308  struct arguments *varptr,
309  *prevvar;
310 
311  for (varptr = prevvar = ptr->argsinsert; varptr != NULL; varptr = varptr->next)
312  {
313  if (p == varptr->variable)
314  {
315  /* remove from list */
316  if (varptr == ptr->argsinsert)
317  ptr->argsinsert = varptr->next;
318  else
319  prevvar->next = varptr->next;
320  }
321  }
322  for (varptr = prevvar = ptr->argsresult; varptr != NULL; varptr = varptr->next)
323  {
324  if (p == varptr->variable)
325  {
326  /* remove from list */
327  if (varptr == ptr->argsresult)
328  ptr->argsresult = varptr->next;
329  else
330  prevvar->next = varptr->next;
331  }
332  }
333  }
334 
335  /* remove it */
336  if (p == allvariables)
337  prev = allvariables = p->next;
338  else
339  prev->next = p->next;
340 
341  ECPGfree_type(p->type);
342  free(p->name);
343  free(p);
344  if (prev == allvariables)
345  p = allvariables;
346  else
347  p = prev ? prev->next : NULL;
348  }
349  else
350  {
351  prev = p;
352  p = prev->next;
353  }
354  }
355 }
356 
357 
358 /*
359  * Here are the variables that need to be handled on every request.
360  * These are of two kinds: input and output.
361  * I will make two lists for them.
362  */
363 
364 struct arguments *argsinsert = NULL;
365 struct arguments *argsresult = NULL;
366 
367 void
369 {
370  argsinsert = NULL;
371  argsresult = NULL;
372 }
373 
374 /* Insert a new variable into our request list.
375  * Note: The list is dumped from the end,
376  * so we have to add new entries at the beginning */
377 void
378 add_variable_to_head(struct arguments **list, struct variable *var, struct variable *ind)
379 {
380  struct arguments *p = (struct arguments *) mm_alloc(sizeof(struct arguments));
381 
382  p->variable = var;
383  p->indicator = ind;
384  p->next = *list;
385  *list = p;
386 }
387 
388 /* Append a new variable to our request list. */
389 void
390 add_variable_to_tail(struct arguments **list, struct variable *var, struct variable *ind)
391 {
392  struct arguments *p,
393  *new = (struct arguments *) mm_alloc(sizeof(struct arguments));
394 
395  for (p = *list; p && p->next; p = p->next);
396 
397  new->variable = var;
398  new->indicator = ind;
399  new->next = NULL;
400 
401  if (p)
402  p->next = new;
403  else
404  *list = new;
405 }
406 
407 void
409 {
410  struct arguments *p,
411  *prev = NULL;
412  bool found = false;
413 
414  for (p = *list; p; p = p->next)
415  {
416  if (p->variable == var)
417  {
418  found = true;
419  break;
420  }
421  prev = p;
422  }
423  if (found)
424  {
425  if (prev)
426  prev->next = p->next;
427  else
428  *list = p->next;
429  }
430 }
431 
432 /* Dump out a list of all the variable on this list.
433  This is a recursive function that works from the end of the list and
434  deletes the list as we go on.
435  */
436 void
438 {
439  char *str_zero;
440 
441  if (list == NULL)
442  return;
443 
444  str_zero = mm_strdup("0");
445 
446  /*
447  * The list is build up from the beginning so lets first dump the end of
448  * the list:
449  */
450 
451  dump_variables(list->next, mode);
452 
453  /* Then the current element and its indicator */
454  ECPGdump_a_type(base_yyout, list->variable->name, list->variable->type, list->variable->brace_level,
455  list->indicator->name, list->indicator->type, list->indicator->brace_level,
456  NULL, NULL, str_zero, NULL, NULL);
457 
458  /* Then release the list element. */
459  if (mode != 0)
460  free(list);
461 
462  free(str_zero);
463 }
464 
465 void
467 {
468  /* make sure this is a valid indicator variable */
469  switch (var->type)
470  {
471  struct ECPGstruct_member *p;
472 
473  case ECPGt_short:
474  case ECPGt_int:
475  case ECPGt_long:
476  case ECPGt_long_long:
478  case ECPGt_unsigned_int:
479  case ECPGt_unsigned_long:
481  break;
482 
483  case ECPGt_struct:
484  case ECPGt_union:
485  for (p = var->u.members; p; p = p->next)
486  check_indicator(p->type);
487  break;
488 
489  case ECPGt_array:
490  check_indicator(var->u.element);
491  break;
492  default:
493  mmerror(PARSE_ERROR, ET_ERROR, "indicator variable must have an integer type");
494  break;
495  }
496 }
497 
498 struct typedefs *
499 get_typedef(const char *name, bool noerror)
500 {
501  struct typedefs *this;
502 
503  for (this = types; this != NULL; this = this->next)
504  {
505  if (strcmp(this->name, name) == 0)
506  return this;
507  }
508 
509  if (!noerror)
510  mmfatal(PARSE_ERROR, "unrecognized data type name \"%s\"", name);
511 
512  return NULL;
513 }
514 
515 void
516 adjust_array(enum ECPGttype type_enum, char **dimension, char **length, char *type_dimension, char *type_index, int pointer_len, bool type_definition)
517 {
518  if (atoi(type_index) >= 0)
519  {
520  if (atoi(*length) >= 0)
521  mmfatal(PARSE_ERROR, "multidimensional arrays are not supported");
522 
523  *length = type_index;
524  }
525 
526  if (atoi(type_dimension) >= 0)
527  {
528  if (atoi(*dimension) >= 0 && atoi(*length) >= 0)
529  mmfatal(PARSE_ERROR, "multidimensional arrays are not supported");
530 
531  if (atoi(*dimension) >= 0)
532  *length = *dimension;
533 
534  *dimension = type_dimension;
535  }
536 
537  if (pointer_len > 2)
538  mmfatal(PARSE_ERROR, ngettext("multilevel pointers (more than 2 levels) are not supported; found %d level",
539  "multilevel pointers (more than 2 levels) are not supported; found %d levels", pointer_len),
540  pointer_len);
541 
542  if (pointer_len > 1 && type_enum != ECPGt_char && type_enum != ECPGt_unsigned_char && type_enum != ECPGt_string)
543  mmfatal(PARSE_ERROR, "pointer to pointer is not supported for this data type");
544 
545  if (pointer_len > 1 && (atoi(*length) >= 0 || atoi(*dimension) >= 0))
546  mmfatal(PARSE_ERROR, "multidimensional arrays are not supported");
547 
548  if (atoi(*length) >= 0 && atoi(*dimension) >= 0 && pointer_len)
549  mmfatal(PARSE_ERROR, "multidimensional arrays are not supported");
550 
551  switch (type_enum)
552  {
553  case ECPGt_struct:
554  case ECPGt_union:
555  /* pointer has to get dimension 0 */
556  if (pointer_len)
557  {
558  *length = *dimension;
559  *dimension = mm_strdup("0");
560  }
561 
562  if (atoi(*length) >= 0)
563  mmfatal(PARSE_ERROR, "multidimensional arrays for structures are not supported");
564 
565  break;
566  case ECPGt_varchar:
567  case ECPGt_bytea:
568  /* pointer has to get dimension 0 */
569  if (pointer_len)
570  *dimension = mm_strdup("0");
571 
572  /* one index is the string length */
573  if (atoi(*length) < 0)
574  {
575  *length = *dimension;
576  *dimension = mm_strdup("-1");
577  }
578 
579  break;
580  case ECPGt_char:
581  case ECPGt_unsigned_char:
582  case ECPGt_string:
583  /* char ** */
584  if (pointer_len == 2)
585  {
586  *length = *dimension = mm_strdup("0");
587  break;
588  }
589 
590  /* pointer has to get length 0 */
591  if (pointer_len == 1)
592  *length = mm_strdup("0");
593 
594  /* one index is the string length */
595  if (atoi(*length) < 0)
596  {
597  /*
598  * make sure we return length = -1 for arrays without given
599  * bounds
600  */
601  if (atoi(*dimension) < 0 && !type_definition)
602 
603  /*
604  * do not change this for typedefs since it will be
605  * changed later on when the variable is defined
606  */
607  *length = mm_strdup("1");
608  else if (strcmp(*dimension, "0") == 0)
609  *length = mm_strdup("-1");
610  else
611  *length = *dimension;
612 
613  *dimension = mm_strdup("-1");
614  }
615  break;
616  default:
617  /* a pointer has dimension = 0 */
618  if (pointer_len)
619  {
620  *length = *dimension;
621  *dimension = mm_strdup("0");
622  }
623 
624  if (atoi(*length) >= 0)
625  mmfatal(PARSE_ERROR, "multidimensional arrays for simple data types are not supported");
626 
627  break;
628  }
629 }
static int32 next
Definition: blutils.c:221
#define ngettext(s, p, n)
Definition: c.h:1181
struct typedefs * types
Definition: ecpg.c:29
struct cursor * cur
Definition: ecpg.c:28
ECPGttype
Definition: ecpgtype.h:42
@ ECPGt_long_long
Definition: ecpgtype.h:45
@ ECPGt_short
Definition: ecpgtype.h:43
@ ECPGt_bytea
Definition: ecpgtype.h:67
@ ECPGt_union
Definition: ecpgtype.h:58
@ ECPGt_varchar
Definition: ecpgtype.h:48
@ ECPGt_struct
Definition: ecpgtype.h:57
@ ECPGt_unsigned_short
Definition: ecpgtype.h:43
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_long
Definition: ecpgtype.h:44
@ ECPGt_unsigned_char
Definition: ecpgtype.h:43
@ ECPGt_array
Definition: ecpgtype.h:56
@ ECPGt_unsigned_long
Definition: ecpgtype.h:44
@ ECPGt_unsigned_long_long
Definition: ecpgtype.h:45
@ ECPGt_unsigned_int
Definition: ecpgtype.h:44
@ ECPGt_char
Definition: ecpgtype.h:43
@ ECPGt_string
Definition: ecpgtype.h:65
const char * str
#define free(a)
Definition: header.h:65
static struct variable * allvariables
Definition: variable.c:7
struct variable * find_variable(char *name)
Definition: variable.c:193
void add_variable_to_tail(struct arguments **list, struct variable *var, struct variable *ind)
Definition: variable.c:390
void dump_variables(struct arguments *list, int mode)
Definition: variable.c:437
void adjust_array(enum ECPGttype type_enum, char **dimension, char **length, char *type_dimension, char *type_index, int pointer_len, bool type_definition)
Definition: variable.c:516
static struct variable * find_struct(char *name, char *next, char *end)
Definition: variable.c:126
void reset_variables(void)
Definition: variable.c:368
static struct variable * find_simple(char *name)
Definition: variable.c:177
void remove_variables(int brace_level)
Definition: variable.c:294
struct arguments * argsresult
Definition: variable.c:365
void remove_variable_from_list(struct arguments **list, struct variable *var)
Definition: variable.c:408
static struct variable * find_struct_member(char *name, char *str, struct ECPGstruct_member *members, int brace_level)
Definition: variable.c:25
void add_variable_to_head(struct arguments **list, struct variable *var, struct variable *ind)
Definition: variable.c:378
struct arguments * argsinsert
Definition: variable.c:364
struct typedefs * get_typedef(const char *name, bool noerror)
Definition: variable.c:499
void remove_typedefs(int brace_level)
Definition: variable.c:260
void check_indicator(struct ECPGtype *var)
Definition: variable.c:466
struct variable * new_variable(const char *name, struct ECPGtype *type, int brace_level)
Definition: variable.c:10
static PgChecksumMode mode
Definition: pg_checksums.c:56
char * c
void mmerror(int error_code, enum errortype type, const char *error,...) pg_attribute_printf(3
char * mm_strdup(const char *string)
Definition: type.c:25
void void mmfatal(int error_code, const char *error,...) pg_attribute_printf(2
#define PARSE_ERROR
FILE * base_yyout
void * mm_alloc(size_t size)
Definition: type.c:13
char * name
Definition: type.h:12
struct ECPGtype * type
Definition: type.h:13
struct ECPGstruct_member * next
Definition: type.h:14
Definition: type.h:18
char * type_name
Definition: type.h:20
char * struct_sizeof
Definition: type.h:24
enum ECPGttype type
Definition: type.h:19
struct ECPGstruct_member * members
Definition: type.h:30
char * size
Definition: type.h:22
struct ECPGtype * element
Definition: type.h:28
union ECPGtype::@159 u
int counter
Definition: type.h:32
struct variable * indicator
Definition: type.h:187
struct arguments * next
Definition: type.h:188
struct variable * variable
Definition: type.h:186
Definition: type.h:137
struct arguments * argsinsert
Definition: type.h:143
struct cursor * next
Definition: type.h:147
struct arguments * argsresult
Definition: type.h:145
enum ECPGttype type_enum
Definition: type.h:123
Definition: type.h:158
int brace_level
Definition: type.h:162
char * name
Definition: type.h:159
struct ECPGstruct_member * struct_member_list
Definition: type.h:161
struct typedefs * next
Definition: type.h:163
struct this_type * type
Definition: type.h:160
char * name
Definition: type.h:178
struct variable * next
enum ECPGttype type
int brace_level
Definition: type.h:180
struct ECPGtype * ECPGmake_array_type(struct ECPGtype *type, char *size)
Definition: type.c:111
struct ECPGtype * ECPGmake_simple_type(enum ECPGttype type, char *size, int counter)
Definition: type.c:96
void ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype *type, const int brace_level, const char *ind_name, struct ECPGtype *ind_type, const int ind_brace_level, const char *prefix, const char *ind_prefix, char *arr_str_size, const char *struct_sizeof, const char *ind_struct_sizeof)
Definition: type.c:241
struct ECPGtype * ECPGmake_struct_type(struct ECPGstruct_member *rm, enum ECPGttype type, char *type_name, char *struct_sizeof)
Definition: type.c:121
void ECPGfree_type(struct ECPGtype *type)
Definition: type.c:655
@ ET_ERROR
Definition: type.h:207
const char * type
const char * name