Make better use of existing enums in plpgsql
authorPeter Eisentraut <peter_e@gmx.net>
Thu, 18 Aug 2016 16:00:00 +0000 (12:00 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Fri, 9 Sep 2016 16:00:00 +0000 (12:00 -0400)
plpgsql.h defines a number of enums, but most of the code passes them
around as ints.  Update structs and function prototypes to take enum
types instead.  This clarifies the struct definitions in plpgsql.h in
particular.

Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>
src/pl/plpgsql/src/pl_comp.c
src/pl/plpgsql/src/pl_exec.c
src/pl/plpgsql/src/pl_funcs.c
src/pl/plpgsql/src/plpgsql.h

index 4ceb402c929c861460fdea40d57a110c8933c14f..18948c8595fd46c59eed71ef5983c847966931c3 100644 (file)
@@ -93,7 +93,7 @@ static PLpgSQL_function *do_compile(FunctionCallInfo fcinfo,
           PLpgSQL_func_hashkey *hashkey,
           bool forValidator);
 static void plpgsql_compile_error_callback(void *arg);
-static void add_parameter_name(int itemtype, int itemno, const char *name);
+static void add_parameter_name(PLpgSQL_nsitem_type itemtype, int itemno, const char *name);
 static void add_dummy_return(PLpgSQL_function *function);
 static Node *plpgsql_pre_column_ref(ParseState *pstate, ColumnRef *cref);
 static Node *plpgsql_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var);
@@ -410,7 +410,7 @@ do_compile(FunctionCallInfo fcinfo,
                char        argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
                PLpgSQL_type *argdtype;
                PLpgSQL_variable *argvariable;
-               int         argitemtype;
+               PLpgSQL_nsitem_type argitemtype;
 
                /* Create $n name for variable */
                snprintf(buf, sizeof(buf), "$%d", i + 1);
@@ -946,7 +946,7 @@ plpgsql_compile_error_callback(void *arg)
  * Add a name for a function parameter to the function's namespace
  */
 static void
-add_parameter_name(int itemtype, int itemno, const char *name)
+add_parameter_name(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
 {
    /*
     * Before adding the name, check for duplicates.  We need this even though
index 2f8b6ff2f2befd350723cffea44cd0316b24fa3e..6141b7ab492945494e2cf19147e702f37b34286b 100644 (file)
@@ -1557,7 +1557,7 @@ exec_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
 
    CHECK_FOR_INTERRUPTS();
 
-   switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+   switch (stmt->cmd_type)
    {
        case PLPGSQL_STMT_BLOCK:
            rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
index 27ebebce1e654d1521be595d2bc2c8cd9f1d419e..e3cd9c0fc760777cb289f1f4068619c3cdb33e04 100644 (file)
@@ -51,7 +51,7 @@ plpgsql_ns_init(void)
  * ----------
  */
 void
-plpgsql_ns_push(const char *label, enum PLpgSQL_label_types label_type)
+plpgsql_ns_push(const char *label, PLpgSQL_label_type label_type)
 {
    if (label == NULL)
        label = "";
@@ -89,7 +89,7 @@ plpgsql_ns_top(void)
  * ----------
  */
 void
-plpgsql_ns_additem(int itemtype, int itemno, const char *name)
+plpgsql_ns_additem(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
 {
    PLpgSQL_nsitem *nse;
 
@@ -231,7 +231,7 @@ plpgsql_ns_find_nearest_loop(PLpgSQL_nsitem *ns_cur)
 const char *
 plpgsql_stmt_typename(PLpgSQL_stmt *stmt)
 {
-   switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+   switch (stmt->cmd_type)
    {
        case PLPGSQL_STMT_BLOCK:
            return _("statement block");
@@ -291,7 +291,7 @@ plpgsql_stmt_typename(PLpgSQL_stmt *stmt)
  * GET DIAGNOSTICS item name as a string, for use in error messages etc.
  */
 const char *
-plpgsql_getdiag_kindname(int kind)
+plpgsql_getdiag_kindname(PLpgSQL_getdiag_kind kind)
 {
    switch (kind)
    {
@@ -367,7 +367,7 @@ static void free_expr(PLpgSQL_expr *expr);
 static void
 free_stmt(PLpgSQL_stmt *stmt)
 {
-   switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+   switch (stmt->cmd_type)
    {
        case PLPGSQL_STMT_BLOCK:
            free_block((PLpgSQL_stmt_block *) stmt);
@@ -791,7 +791,7 @@ static void
 dump_stmt(PLpgSQL_stmt *stmt)
 {
    printf("%3d:", stmt->lineno);
-   switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+   switch (stmt->cmd_type)
    {
        case PLPGSQL_STMT_BLOCK:
            dump_block((PLpgSQL_stmt_block *) stmt);
index b416e50c646174bfd5e1ae0e94fac789c1b7ff68..c84a97b675a27e9f52cdc96ef06093ff3e441ced 100644 (file)
 /*
  * Compiler's namespace item types
  */
-enum
+typedef enum PLpgSQL_nsitem_type
 {
    PLPGSQL_NSTYPE_LABEL,
    PLPGSQL_NSTYPE_VAR,
    PLPGSQL_NSTYPE_ROW,
    PLPGSQL_NSTYPE_REC
-};
+} PLpgSQL_nsitem_type;
 
 /*
  * A PLPGSQL_NSTYPE_LABEL stack entry must be one of these types
  */
-enum PLpgSQL_label_types
+typedef enum PLpgSQL_label_type
 {
    PLPGSQL_LABEL_BLOCK,        /* DECLARE/BEGIN block */
    PLPGSQL_LABEL_LOOP,         /* looping construct */
    PLPGSQL_LABEL_OTHER         /* anything else */
-};
+} PLpgSQL_label_type;
 
 /*
  * Datum array node types
  */
-enum
+typedef enum PLpgSQL_datum_type
 {
    PLPGSQL_DTYPE_VAR,
    PLPGSQL_DTYPE_ROW,
@@ -66,23 +66,23 @@ enum
    PLPGSQL_DTYPE_RECFIELD,
    PLPGSQL_DTYPE_ARRAYELEM,
    PLPGSQL_DTYPE_EXPR
-};
+} PLpgSQL_datum_type;
 
 /*
  * Variants distinguished in PLpgSQL_type structs
  */
-enum
+typedef enum PLpgSQL_type_type
 {
    PLPGSQL_TTYPE_SCALAR,       /* scalar types and domains */
    PLPGSQL_TTYPE_ROW,          /* composite types */
    PLPGSQL_TTYPE_REC,          /* RECORD pseudotype */
    PLPGSQL_TTYPE_PSEUDO        /* other pseudotypes */
-};
+} PLpgSQL_type_type;
 
 /*
  * Execution tree node types
  */
-enum PLpgSQL_stmt_types
+typedef enum PLpgSQL_stmt_type
 {
    PLPGSQL_STMT_BLOCK,
    PLPGSQL_STMT_ASSIGN,
@@ -108,7 +108,7 @@ enum PLpgSQL_stmt_types
    PLPGSQL_STMT_FETCH,
    PLPGSQL_STMT_CLOSE,
    PLPGSQL_STMT_PERFORM
-};
+} PLpgSQL_stmt_type;
 
 /*
  * Execution node return codes
@@ -124,7 +124,7 @@ enum
 /*
  * GET DIAGNOSTICS information items
  */
-enum
+typedef enum PLpgSQL_getdiag_kind
 {
    PLPGSQL_GETDIAG_ROW_COUNT,
    PLPGSQL_GETDIAG_RESULT_OID,
@@ -139,12 +139,12 @@ enum
    PLPGSQL_GETDIAG_MESSAGE_TEXT,
    PLPGSQL_GETDIAG_TABLE_NAME,
    PLPGSQL_GETDIAG_SCHEMA_NAME
-};
+} PLpgSQL_getdiag_kind;
 
 /*
  * RAISE statement options
  */
-enum
+typedef enum PLpgSQL_raise_option_type
 {
    PLPGSQL_RAISEOPTION_ERRCODE,
    PLPGSQL_RAISEOPTION_MESSAGE,
@@ -155,12 +155,12 @@ enum
    PLPGSQL_RAISEOPTION_DATATYPE,
    PLPGSQL_RAISEOPTION_TABLE,
    PLPGSQL_RAISEOPTION_SCHEMA
-};
+} PLpgSQL_raise_option_type;
 
 /*
  * Behavioral modes for plpgsql variable resolution
  */
-typedef enum
+typedef enum PLpgSQL_resolve_option
 {
    PLPGSQL_RESOLVE_ERROR,      /* throw error if ambiguous */
    PLPGSQL_RESOLVE_VARIABLE,   /* prefer plpgsql var to table column */
@@ -179,7 +179,7 @@ typedef struct PLpgSQL_type
 {
    char       *typname;        /* (simple) name of the type */
    Oid         typoid;         /* OID of the data type */
-   int         ttype;          /* PLPGSQL_TTYPE_ code */
+   PLpgSQL_type_type ttype;    /* PLPGSQL_TTYPE_ code */
    int16       typlen;         /* stuff copied from its pg_type entry */
    bool        typbyval;
    char        typtype;
@@ -197,7 +197,7 @@ typedef struct PLpgSQL_type
  */
 typedef struct PLpgSQL_datum
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
 } PLpgSQL_datum;
 
@@ -209,7 +209,7 @@ typedef struct PLpgSQL_datum
  */
 typedef struct PLpgSQL_variable
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    char       *refname;
    int         lineno;
@@ -220,7 +220,7 @@ typedef struct PLpgSQL_variable
  */
 typedef struct PLpgSQL_expr
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    char       *query;
    SPIPlanPtr  plan;
@@ -255,7 +255,7 @@ typedef struct PLpgSQL_expr
  */
 typedef struct PLpgSQL_var
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    char       *refname;
    int         lineno;
@@ -278,7 +278,7 @@ typedef struct PLpgSQL_var
  */
 typedef struct PLpgSQL_row
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    char       *refname;
    int         lineno;
@@ -301,7 +301,7 @@ typedef struct PLpgSQL_row
  */
 typedef struct PLpgSQL_rec
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    char       *refname;
    int         lineno;
@@ -317,7 +317,7 @@ typedef struct PLpgSQL_rec
  */
 typedef struct PLpgSQL_recfield
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    char       *fieldname;
    int         recparentno;    /* dno of parent record */
@@ -328,7 +328,7 @@ typedef struct PLpgSQL_recfield
  */
 typedef struct PLpgSQL_arrayelem
 {
-   int         dtype;
+   PLpgSQL_datum_type dtype;
    int         dno;
    PLpgSQL_expr *subscript;
    int         arrayparentno;  /* dno of parent array variable */
@@ -350,9 +350,9 @@ typedef struct PLpgSQL_arrayelem
  */
 typedef struct PLpgSQL_nsitem
 {
-   int         itemtype;
+   PLpgSQL_nsitem_type itemtype;
    /*
-    * For labels, itemno is a value of enum PLpgSQL_label_types. For other
+    * For labels, itemno is a value of enum PLpgSQL_label_type. For other
     * itemtypes, itemno is the associated PLpgSQL_datum's dno.
     */
    int         itemno;
@@ -365,7 +365,7 @@ typedef struct PLpgSQL_nsitem
  */
 typedef struct PLpgSQL_stmt
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
 } PLpgSQL_stmt;
 
@@ -404,7 +404,7 @@ typedef struct PLpgSQL_exception
  */
 typedef struct PLpgSQL_stmt_block
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    List       *body;           /* List of statements */
@@ -418,7 +418,7 @@ typedef struct PLpgSQL_stmt_block
  */
 typedef struct PLpgSQL_stmt_assign
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    int         varno;
    PLpgSQL_expr *expr;
@@ -429,7 +429,7 @@ typedef struct PLpgSQL_stmt_assign
  */
 typedef struct PLpgSQL_stmt_perform
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *expr;
 } PLpgSQL_stmt_perform;
@@ -439,7 +439,7 @@ typedef struct PLpgSQL_stmt_perform
  */
 typedef struct PLpgSQL_diag_item
 {
-   int         kind;           /* id for diagnostic value desired */
+   PLpgSQL_getdiag_kind kind;  /* id for diagnostic value desired */
    int         target;         /* where to assign it */
 } PLpgSQL_diag_item;
 
@@ -448,7 +448,7 @@ typedef struct PLpgSQL_diag_item
  */
 typedef struct PLpgSQL_stmt_getdiag
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    bool        is_stacked;     /* STACKED or CURRENT diagnostics area? */
    List       *diag_items;     /* List of PLpgSQL_diag_item */
@@ -459,7 +459,7 @@ typedef struct PLpgSQL_stmt_getdiag
  */
 typedef struct PLpgSQL_stmt_if
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *cond;         /* boolean expression for THEN */
    List       *then_body;      /* List of statements */
@@ -482,7 +482,7 @@ typedef struct PLpgSQL_if_elsif
  */
 typedef struct PLpgSQL_stmt_case
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *t_expr;       /* test expression, or NULL if none */
    int         t_varno;        /* var to store test expression value into */
@@ -506,7 +506,7 @@ typedef struct PLpgSQL_case_when
  */
 typedef struct PLpgSQL_stmt_loop
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    List       *body;           /* List of statements */
@@ -517,7 +517,7 @@ typedef struct PLpgSQL_stmt_loop
  */
 typedef struct PLpgSQL_stmt_while
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    PLpgSQL_expr *cond;
@@ -529,7 +529,7 @@ typedef struct PLpgSQL_stmt_while
  */
 typedef struct PLpgSQL_stmt_fori
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    PLpgSQL_var *var;
@@ -547,7 +547,7 @@ typedef struct PLpgSQL_stmt_fori
  */
 typedef struct PLpgSQL_stmt_forq
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    PLpgSQL_rec *rec;
@@ -560,7 +560,7 @@ typedef struct PLpgSQL_stmt_forq
  */
 typedef struct PLpgSQL_stmt_fors
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    PLpgSQL_rec *rec;
@@ -575,7 +575,7 @@ typedef struct PLpgSQL_stmt_fors
  */
 typedef struct PLpgSQL_stmt_forc
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    PLpgSQL_rec *rec;
@@ -591,7 +591,7 @@ typedef struct PLpgSQL_stmt_forc
  */
 typedef struct PLpgSQL_stmt_dynfors
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    PLpgSQL_rec *rec;
@@ -607,7 +607,7 @@ typedef struct PLpgSQL_stmt_dynfors
  */
 typedef struct PLpgSQL_stmt_foreach_a
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    char       *label;
    int         varno;          /* loop target variable */
@@ -621,7 +621,7 @@ typedef struct PLpgSQL_stmt_foreach_a
  */
 typedef struct PLpgSQL_stmt_open
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    int         curvar;
    int         cursor_options;
@@ -637,7 +637,7 @@ typedef struct PLpgSQL_stmt_open
  */
 typedef struct PLpgSQL_stmt_fetch
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_rec *rec;           /* target, as record or row */
    PLpgSQL_row *row;
@@ -654,7 +654,7 @@ typedef struct PLpgSQL_stmt_fetch
  */
 typedef struct PLpgSQL_stmt_close
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    int         curvar;
 } PLpgSQL_stmt_close;
@@ -664,7 +664,7 @@ typedef struct PLpgSQL_stmt_close
  */
 typedef struct PLpgSQL_stmt_exit
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    bool        is_exit;        /* Is this an exit or a continue? */
    char       *label;          /* NULL if it's an unlabelled EXIT/CONTINUE */
@@ -676,7 +676,7 @@ typedef struct PLpgSQL_stmt_exit
  */
 typedef struct PLpgSQL_stmt_return
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *expr;
    int         retvarno;
@@ -687,7 +687,7 @@ typedef struct PLpgSQL_stmt_return
  */
 typedef struct PLpgSQL_stmt_return_next
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *expr;
    int         retvarno;
@@ -698,7 +698,7 @@ typedef struct PLpgSQL_stmt_return_next
  */
 typedef struct PLpgSQL_stmt_return_query
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *query;        /* if static query */
    PLpgSQL_expr *dynquery;     /* if dynamic query (RETURN QUERY EXECUTE) */
@@ -710,7 +710,7 @@ typedef struct PLpgSQL_stmt_return_query
  */
 typedef struct PLpgSQL_stmt_raise
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    int         elog_level;
    char       *condname;       /* condition name, SQLSTATE, or NULL */
@@ -724,7 +724,7 @@ typedef struct PLpgSQL_stmt_raise
  */
 typedef struct PLpgSQL_raise_option
 {
-   int         opt_type;
+   PLpgSQL_raise_option_type opt_type;
    PLpgSQL_expr *expr;
 } PLpgSQL_raise_option;
 
@@ -733,7 +733,7 @@ typedef struct PLpgSQL_raise_option
  */
 typedef struct PLpgSQL_stmt_assert
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *cond;
    PLpgSQL_expr *message;
@@ -744,7 +744,7 @@ typedef struct PLpgSQL_stmt_assert
  */
 typedef struct PLpgSQL_stmt_execsql
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *sqlstmt;
    bool        mod_stmt;       /* is the stmt INSERT/UPDATE/DELETE?  Note:
@@ -760,7 +760,7 @@ typedef struct PLpgSQL_stmt_execsql
  */
 typedef struct PLpgSQL_stmt_dynexecute
 {
-   int         cmd_type;
+   PLpgSQL_stmt_type cmd_type;
    int         lineno;
    PLpgSQL_expr *query;        /* string expression */
    bool        into;           /* INTO supplied? */
@@ -1111,10 +1111,10 @@ extern void plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate,
  */
 extern void plpgsql_ns_init(void);
 extern void plpgsql_ns_push(const char *label,
-               enum PLpgSQL_label_types label_type);
+               PLpgSQL_label_type label_type);
 extern void plpgsql_ns_pop(void);
 extern PLpgSQL_nsitem *plpgsql_ns_top(void);
-extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
+extern void plpgsql_ns_additem(PLpgSQL_nsitem_type itemtype, int itemno, const char *name);
 extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
                  const char *name1, const char *name2,
                  const char *name3, int *names_used);
@@ -1126,7 +1126,7 @@ extern PLpgSQL_nsitem *plpgsql_ns_find_nearest_loop(PLpgSQL_nsitem *ns_cur);
  * Other functions in pl_funcs.c
  */
 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
-extern const char *plpgsql_getdiag_kindname(int kind);
+extern const char *plpgsql_getdiag_kindname(PLpgSQL_getdiag_kind kind);
 extern void plpgsql_free_function_memory(PLpgSQL_function *func);
 extern void plpgsql_dumptree(PLpgSQL_function *func);