587 lines
14 KiB
C
Raw Normal View History

/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/variable.c,v 1.45 2008/05/16 15:20:04 petere Exp $ */
2006-07-30 10:24:10 +00:00
#include "postgres_fe.h"
2000-02-17 19:48:58 +00:00
#include "extern.h"
static struct variable *allvariables = NULL;
2000-02-17 19:48:58 +00:00
struct variable *
new_variable(const char *name, struct ECPGtype * type, int brace_level)
2000-02-17 19:48:58 +00:00
{
struct variable *p = (struct variable *) mm_alloc(sizeof(struct variable));
2000-02-17 19:48:58 +00:00
p->name = mm_strdup(name);
p->type = type;
p->brace_level = brace_level;
2000-02-17 19:48:58 +00:00
p->next = allvariables;
allvariables = p;
2000-02-17 19:48:58 +00:00
return (p);
2000-02-17 19:48:58 +00:00
}
static struct variable *
find_struct_member(char *name, char *str, struct ECPGstruct_member * members, int brace_level)
2000-02-17 19:48:58 +00:00
{
char *next = strpbrk(++str, ".-["),
2003-08-04 00:43:34 +00:00
*end,
c = '\0';
2000-02-17 19:48:58 +00:00
if (next != NULL)
{
c = *next;
*next = '\0';
}
2000-02-17 19:48:58 +00:00
for (; members; members = members->next)
2000-02-17 19:48:58 +00:00
{
if (strcmp(members->name, str) == 0)
2000-02-17 19:48:58 +00:00
{
if (next == NULL)
2000-02-17 19:48:58 +00:00
{
/* found the end */
switch (members->type->type)
{
case ECPGt_array:
return (new_variable(name, ECPGmake_array_type(ECPGmake_simple_type(members->type->u.element->type, members->type->u.element->size, members->type->u.element->lineno), members->type->size), brace_level));
case ECPGt_struct:
case ECPGt_union:
return (new_variable(name, ECPGmake_struct_type(members->type->u.members, members->type->type, members->type->struct_sizeof), brace_level));
default:
return (new_variable(name, ECPGmake_simple_type(members->type->type, members->type->size, members->type->lineno), brace_level));
}
2000-02-17 19:48:58 +00:00
}
else
2000-02-17 19:48:58 +00:00
{
*next = c;
if (c == '[')
{
2003-08-04 00:43:34 +00:00
int count;
2003-08-04 00:43:34 +00:00
/*
2005-10-15 02:49:52 +00:00
* We don't care about what's inside the array braces so
* just eat up the character
2003-08-04 00:43:34 +00:00
*/
for (count = 1, end = next + 1; count; end++)
{
2003-08-04 00:43:34 +00:00
switch (*end)
{
case '[':
count++;
break;
case ']':
count--;
break;
default:
break;
}
}
}
2003-08-04 00:43:34 +00:00
else
end = next;
switch (*end)
{
2005-10-15 02:49:52 +00:00
case '\0': /* found the end, but this time it has to be
* an array element */
2003-08-04 00:43:34 +00:00
if (members->type->type != ECPGt_array)
mmerror(PARSE_ERROR, ET_FATAL, "incorrectly formed variable %s", name);
2003-08-04 00:43:34 +00:00
switch (members->type->u.element->type)
{
case ECPGt_array:
return (new_variable(name, ECPGmake_array_type(ECPGmake_simple_type(members->type->u.element->u.element->type, members->type->u.element->u.element->size, members->type->u.element->u.element->lineno), members->type->u.element->size), brace_level));
2003-08-04 00:43:34 +00:00
case ECPGt_struct:
case ECPGt_union:
return (new_variable(name, ECPGmake_struct_type(members->type->u.element->u.members, members->type->u.element->type, members->type->u.element->struct_sizeof), brace_level));
default:
return (new_variable(name, ECPGmake_simple_type(members->type->u.element->type, members->type->u.element->size, members->type->u.element->lineno), brace_level));
2003-08-04 00:43:34 +00:00
}
break;
case '-':
return (find_struct_member(name, end, members->type->u.element->u.members, brace_level));
break;
case '.':
if (members->type->type == ECPGt_array)
2003-08-04 00:43:34 +00:00
return (find_struct_member(name, end, members->type->u.element->u.members, brace_level));
else
return (find_struct_member(name, end, members->type->u.members, brace_level));
2003-08-04 00:43:34 +00:00
break;
default:
mmerror(PARSE_ERROR, ET_FATAL, "incorrectly formed variable %s", name);
2003-08-04 00:43:34 +00:00
break;
}
2000-02-17 19:48:58 +00:00
}
}
}
return (NULL);
2000-02-17 19:48:58 +00:00
}
static struct variable *
2003-05-29 12:00:22 +00:00
find_struct(char *name, char *next, char *end)
2000-02-17 19:48:58 +00:00
{
struct variable *p;
char c = *next;
/* first get the mother structure entry */
*next = '\0';
p = find_variable(name);
if (c == '-')
2000-02-17 19:48:58 +00:00
{
if (p->type->type != ECPGt_array)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is not a pointer", name);
if (p->type->u.element->type != ECPGt_struct && p->type->u.element->type != ECPGt_union)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is not a pointer to a structure or a union", name);
2003-05-29 12:00:22 +00:00
/* restore the name, we will need it later */
*next = c;
return find_struct_member(name, ++end, p->type->u.element->u.members, p->brace_level);
2000-02-17 19:48:58 +00:00
}
else
{
2003-05-29 12:00:22 +00:00
if (next == end)
{
2003-05-29 12:00:22 +00:00
if (p->type->type != ECPGt_struct && p->type->type != ECPGt_union)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is neither a structure nor a union", name);
2003-05-29 12:00:22 +00:00
/* restore the name, we will need it later */
*next = c;
return find_struct_member(name, end, p->type->u.members, p->brace_level);
}
2003-05-29 12:00:22 +00:00
else
{
if (p->type->type != ECPGt_array)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is not an array", name);
2000-02-17 19:48:58 +00:00
2003-05-29 12:00:22 +00:00
if (p->type->u.element->type != ECPGt_struct && p->type->u.element->type != ECPGt_union)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is not a pointer to a structure or a union", name);
2003-05-29 12:00:22 +00:00
/* restore the name, we will need it later */
*next = c;
2000-02-17 19:48:58 +00:00
2003-05-29 12:00:22 +00:00
return find_struct_member(name, end, p->type->u.element->u.members, p->brace_level);
}
}
2000-02-17 19:48:58 +00:00
}
static struct variable *
find_simple(char *name)
2000-02-17 19:48:58 +00:00
{
struct variable *p;
2000-02-17 19:48:58 +00:00
for (p = allvariables; p; p = p->next)
{
if (strcmp(p->name, name) == 0)
return p;
}
2000-02-17 19:48:58 +00:00
return (NULL);
2000-02-17 19:48:58 +00:00
}
/* Note that this function will end the program in case of an unknown */
/* variable */
struct variable *
find_variable(char *name)
2000-02-17 19:48:58 +00:00
{
2003-08-04 00:43:34 +00:00
char *next,
*end;
struct variable *p;
2003-08-04 00:43:34 +00:00
int count;
2003-05-29 12:00:22 +00:00
next = strpbrk(name, ".[-");
if (next)
{
if (*next == '[')
{
2003-08-04 00:43:34 +00:00
/*
2005-10-15 02:49:52 +00:00
* We don't care about what's inside the array braces so just eat
* up the characters
2003-08-04 00:43:34 +00:00
*/
for (count = 1, end = next + 1; count; end++)
2003-05-29 12:00:22 +00:00
{
2003-08-04 00:43:34 +00:00
switch (*end)
{
case '[':
count++;
break;
case ']':
count--;
break;
default:
break;
}
2003-05-29 12:00:22 +00:00
}
2003-08-04 00:43:34 +00:00
if (*end == '.')
p = find_struct(name, next, end);
2003-05-29 12:00:22 +00:00
else
{
2003-08-04 00:43:34 +00:00
char c = *next;
*next = '\0';
p = find_simple(name);
if (p == NULL)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is not declared", name);
*next = c;
switch (p->type->u.element->type)
{
case ECPGt_array:
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->lineno), p->type->u.element->size), p->brace_level));
case ECPGt_struct:
case ECPGt_union:
return (new_variable(name, ECPGmake_struct_type(p->type->u.element->u.members, p->type->u.element->type, p->type->u.element->struct_sizeof), p->brace_level));
default:
return (new_variable(name, ECPGmake_simple_type(p->type->u.element->type, p->type->u.element->size, p->type->u.element->lineno), p->brace_level));
}
2003-05-29 12:00:22 +00:00
}
}
2003-08-04 00:43:34 +00:00
else
p = find_struct(name, next, next);
2003-05-29 12:00:22 +00:00
}
2003-08-04 00:43:34 +00:00
else
p = find_simple(name);
if (p == NULL)
mmerror(PARSE_ERROR, ET_FATAL, "variable %s is not declared", name);
return (p);
2000-02-17 19:48:58 +00:00
}
void
remove_typedefs(int brace_level)
{
struct typedefs *p,
*prev;
for (p = prev = types; p;)
{
if (p->brace_level >= brace_level)
{
/* remove it */
if (p == types)
prev = types = p->next;
else
prev->next = p->next;
2004-08-29 05:07:03 +00:00
if (p->type->type_enum == ECPGt_struct || p->type->type_enum == ECPGt_union)
free(p->struct_member_list);
free(p->type);
free(p->name);
free(p);
if (prev == types)
p = types;
else
p = prev ? prev->next : NULL;
}
else
{
prev = p;
p = prev->next;
}
}
}
2000-02-17 19:48:58 +00:00
void
remove_variables(int brace_level)
{
struct variable *p,
*prev;
2000-02-17 19:48:58 +00:00
for (p = prev = allvariables; p;)
2000-02-17 19:48:58 +00:00
{
if (p->brace_level >= brace_level)
{
/* is it still referenced by a cursor? */
struct cursor *ptr;
2003-08-04 00:43:34 +00:00
for (ptr = cur; ptr != NULL; ptr = ptr->next)
{
2003-08-04 00:43:34 +00:00
struct arguments *varptr,
*prevvar;
for (varptr = prevvar = ptr->argsinsert; varptr != NULL; varptr = varptr->next)
{
if (p == varptr->variable)
{
/* remove from list */
if (varptr == ptr->argsinsert)
ptr->argsinsert = varptr->next;
else
prevvar->next = varptr->next;
}
}
for (varptr = prevvar = ptr->argsresult; varptr != NULL; varptr = varptr->next)
{
if (p == varptr->variable)
{
/* remove from list */
if (varptr == ptr->argsresult)
ptr->argsresult = varptr->next;
else
prevvar->next = varptr->next;
}
}
}
2003-08-04 00:43:34 +00:00
/* remove it */
if (p == allvariables)
prev = allvariables = p->next;
else
prev->next = p->next;
ECPGfree_type(p->type);
free(p->name);
free(p);
if (prev == allvariables)
p = allvariables;
else
p = prev ? prev->next : NULL;
}
else
{
prev = p;
p = prev->next;
}
2000-02-17 19:48:58 +00:00
}
}
/*
* Here are the variables that need to be handled on every request.
* These are of two kinds: input and output.
* I will make two lists for them.
*/
struct arguments *argsinsert = NULL;
struct arguments *argsresult = NULL;
2000-02-17 19:48:58 +00:00
void
reset_variables(void)
{
argsinsert = NULL;
argsresult = NULL;
2000-02-17 19:48:58 +00:00
}
2004-08-29 05:07:03 +00:00
/* Insert a new variable into our request list.
* Note: The list is dumped from the end,
* so we have to add new entries at the beginning */
2000-02-17 19:48:58 +00:00
void
add_variable_to_head(struct arguments ** list, struct variable * var, struct variable * ind)
2000-02-17 19:48:58 +00:00
{
struct arguments *p = (struct arguments *) mm_alloc(sizeof(struct arguments));
p->variable = var;
p->indicator = ind;
p->next = *list;
*list = p;
2000-02-17 19:48:58 +00:00
}
2000-03-09 09:17:16 +00:00
/* Append a new variable to our request list. */
void
add_variable_to_tail(struct arguments ** list, struct variable * var, struct variable * ind)
2000-03-09 09:17:16 +00:00
{
struct arguments *p,
*new = (struct arguments *) mm_alloc(sizeof(struct arguments));
for (p = *list; p && p->next; p = p->next);
new->variable = var;
new->indicator = ind;
new->next = NULL;
if (p)
p->next = new;
else
*list = new;
2000-03-09 09:17:16 +00:00
}
2000-02-17 19:48:58 +00:00
/* Dump out a list of all the variable on this list.
This is a recursive function that works from the end of the list and
deletes the list as we go on.
*/
void
dump_variables(struct arguments * list, int mode)
{
if (list == NULL)
return;
2000-02-17 19:48:58 +00:00
/*
2005-10-15 02:49:52 +00:00
* The list is build up from the beginning so lets first dump the end of
* the list:
*/
2000-02-17 19:48:58 +00:00
dump_variables(list->next, mode);
2000-02-17 19:48:58 +00:00
/* Then the current element and its indicator */
2003-08-04 00:43:34 +00:00
ECPGdump_a_type(yyout, list->variable->name, list->variable->type,
list->indicator->name, list->indicator->type,
NULL, NULL, make_str("0"), NULL, NULL);
2000-02-17 19:48:58 +00:00
/* Then release the list element. */
if (mode != 0)
free(list);
2000-02-17 19:48:58 +00:00
}
void
check_indicator(struct ECPGtype * var)
2000-02-17 19:48:58 +00:00
{
/* make sure this is a valid indicator variable */
switch (var->type)
2000-02-17 19:48:58 +00:00
{
struct ECPGstruct_member *p;
2000-02-17 19:48:58 +00:00
case ECPGt_short:
case ECPGt_int:
case ECPGt_long:
2000-09-19 11:47:16 +00:00
case ECPGt_long_long:
2000-02-17 19:48:58 +00:00
case ECPGt_unsigned_short:
case ECPGt_unsigned_int:
case ECPGt_unsigned_long:
2000-09-19 11:47:16 +00:00
case ECPGt_unsigned_long_long:
2000-02-17 19:48:58 +00:00
break;
case ECPGt_struct:
case ECPGt_union:
for (p = var->u.members; p; p = p->next)
check_indicator(p->type);
2000-02-17 19:48:58 +00:00
break;
case ECPGt_array:
check_indicator(var->u.element);
break;
default:
2001-12-10 14:55:47 +00:00
mmerror(PARSE_ERROR, ET_ERROR, "indicator variable must be integer type");
2000-02-17 19:48:58 +00:00
break;
}
}
struct typedefs *
get_typedef(char *name)
{
struct typedefs *this;
for (this = types; this && strcmp(this->name, name); this = this->next);
if (!this)
mmerror(PARSE_ERROR, ET_FATAL, "invalid datatype \"%s\"", name);
2000-02-17 19:48:58 +00:00
return (this);
2000-02-17 19:48:58 +00:00
}
void
adjust_array(enum ECPGttype type_enum, char **dimension, char **length, char *type_dimension, char *type_index, int pointer_len, bool type_definition)
2000-02-17 19:48:58 +00:00
{
if (atoi(type_index) >= 0)
2000-02-17 19:48:58 +00:00
{
if (atoi(*length) >= 0)
mmerror(PARSE_ERROR, ET_FATAL, "no multidimensional array support");
2000-02-17 19:48:58 +00:00
*length = type_index;
}
if (atoi(type_dimension) >= 0)
2000-02-17 19:48:58 +00:00
{
if (atoi(*dimension) >= 0 && atoi(*length) >= 0)
mmerror(PARSE_ERROR, ET_FATAL, "no multidimensional array support");
2000-02-17 19:48:58 +00:00
if (atoi(*dimension) >= 0)
2000-02-17 19:48:58 +00:00
*length = *dimension;
*dimension = type_dimension;
}
2002-09-04 20:31:48 +00:00
if (pointer_len > 2)
mmerror(PARSE_ERROR, ET_FATAL, "no multilevel (more than 2) pointer supported %d", pointer_len);
2003-05-29 12:00:22 +00:00
2002-09-04 20:31:48 +00:00
if (pointer_len > 1 && type_enum != ECPGt_char && type_enum != ECPGt_unsigned_char)
mmerror(PARSE_ERROR, ET_FATAL, "no pointer to pointer supported for this type");
if (pointer_len > 1 && (atoi(*length) >= 0 || atoi(*dimension) >= 0))
mmerror(PARSE_ERROR, ET_FATAL, "no multidimensional array support");
2000-02-17 19:48:58 +00:00
if (atoi(*length) >= 0 && atoi(*dimension) >= 0 && pointer_len)
mmerror(PARSE_ERROR, ET_FATAL, "no multidimensional array support");
2000-02-17 19:48:58 +00:00
switch (type_enum)
{
case ECPGt_struct:
case ECPGt_union:
/* pointer has to get dimension 0 */
if (pointer_len)
{
*length = *dimension;
*dimension = make_str("0");
}
if (atoi(*length) >= 0)
mmerror(PARSE_ERROR, ET_FATAL, "no multidimensional array support for structures");
break;
case ECPGt_varchar:
/* pointer has to get dimension 0 */
if (pointer_len)
*dimension = make_str("0");
/* one index is the string length */
if (atoi(*length) < 0)
{
*length = *dimension;
*dimension = make_str("-1");
}
break;
case ECPGt_char:
case ECPGt_unsigned_char:
/* char ** */
2002-09-04 20:31:48 +00:00
if (pointer_len == 2)
{
*length = *dimension = make_str("0");
break;
}
2002-09-04 20:31:48 +00:00
/* pointer has to get length 0 */
2002-09-04 20:31:48 +00:00
if (pointer_len == 1)
*length = make_str("0");
/* one index is the string length */
if (atoi(*length) < 0)
{
2003-08-04 00:43:34 +00:00
/*
2005-10-15 02:49:52 +00:00
* make sure we return length = -1 for arrays without given
* bounds
2003-08-04 00:43:34 +00:00
*/
if (atoi(*dimension) < 0 && !type_definition)
2004-08-29 05:07:03 +00:00
/*
2004-08-29 05:07:03 +00:00
* do not change this for typedefs since it will be
* changed later on when the variable is defined
*/
*length = make_str("1");
else if (strcmp(*dimension, "0") == 0)
*length = make_str("-1");
2003-08-04 00:43:34 +00:00
else
*length = *dimension;
2003-08-04 00:43:34 +00:00
*dimension = make_str("-1");
}
break;
default:
/* a pointer has dimension = 0 */
if (pointer_len)
{
*length = *dimension;
*dimension = make_str("0");
}
if (atoi(*length) >= 0)
mmerror(PARSE_ERROR, ET_FATAL, "no multidimensional array support for simple data types");
break;
2000-02-17 19:48:58 +00:00
}
}